summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2023-02-11 18:26:12 -0800
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-02-11 18:26:12 -0800
commit55e90e61eb0ec32b6998ba3c562df3897aebbb43 (patch)
treeb6208cf91c742a29aeca1d9b4e7e854c559ddc3a
parente9e78bf8f2de870fbf90cc247721df16805ba3da (diff)
Make -help & -version work without requiring an open display
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--dsimple.c5
-rw-r--r--dsimple.h2
-rw-r--r--xwd.c33
3 files changed, 26 insertions, 14 deletions
diff --git a/dsimple.c b/dsimple.c
index 8882822..2546072 100644
--- a/dsimple.c
+++ b/dsimple.c
@@ -77,7 +77,7 @@ Get_Display_Name(int *pargc, /* MODIFIED */
if (!strcmp(arg, "-display") || !strcmp(arg, "-d")) {
if (++i >= argc)
- usage("-display requires an argument");
+ usage("-display requires an argument", EXIT_FAILURE);
displayname = argv[i];
*pargc -= 2;
@@ -184,7 +184,8 @@ Select_Window_Args(int *rargc, char **argv)
#define OPTION argv[0]
#define NXTOPTP ++argv, --argc>0
-#define NXTOPT(arg) if (++argv, --argc==0) usage(arg " requires an argument")
+#define NXTOPT(arg) if (++argv, --argc==0) \
+ usage(arg " requires an argument", EXIT_FAILURE)
#define COPYOPT nargv++[0]=OPTION, nargc++
while (NXTOPTP) {
diff --git a/dsimple.h b/dsimple.h
index 416e96a..1a24313 100644
--- a/dsimple.h
+++ b/dsimple.h
@@ -61,7 +61,7 @@ Display *Open_Display(const char *);
void Setup_Display_And_Screen(int *, char **);
void Close_Display(void);
Window Select_Window_Args(int *, char **);
-void usage(const char *errmsg) _X_NORETURN _X_COLD;
+void usage(const char *errmsg, int exitval) _X_NORETURN _X_COLD;
#define X_USAGE "[host:display]" /* X arguments handled by
Get_Display_Name */
diff --git a/xwd.c b/xwd.c
index 0ea4168..98ac506 100644
--- a/xwd.c
+++ b/xwd.c
@@ -148,6 +148,23 @@ main(int argc, char **argv)
INIT_NAME;
+ /* Handle args that don't require opening a display */
+ for (int n = 1; n < argc; n++) {
+ const char *argn = argv[n];
+ /* accept single or double dash for -help & -version */
+ if (argn[0] == '-' && argn[1] == '-') {
+ argn++;
+ }
+ if (strcmp (argn, "-help") == 0) {
+ usage(NULL, EXIT_SUCCESS);
+ exit(0);
+ }
+ if (strcmp (argn, "-version") == 0) {
+ puts(PACKAGE_STRING);
+ exit(EXIT_SUCCESS);
+ }
+ }
+
Setup_Display_And_Screen(&argc, argv);
/* Get window select on command line, if any */
@@ -162,11 +179,9 @@ main(int argc, char **argv)
debug = True;
continue;
}
- if (!strcmp(argv[i], "-help"))
- usage(NULL);
if (!strcmp(argv[i], "-out")) {
if (++i >= argc)
- usage("-out requires an argument");
+ usage("-out requires an argument", EXIT_FAILURE);
if (!(out_file = fopen(argv[i], "wb")))
Fatal_Error("Can't open output file as specified.");
standard_out = False;
@@ -186,7 +201,7 @@ main(int argc, char **argv)
}
if (!strcmp(argv[i], "-add")) {
if (++i >= argc)
- usage("-add requires an argument");
+ usage("-add requires an argument", EXIT_FAILURE);
add_pixel_value = parse_long(argv[i]);
continue;
}
@@ -198,13 +213,9 @@ main(int argc, char **argv)
silent = True;
continue;
}
- if (!strcmp(argv[i], "-version")) {
- puts(PACKAGE_STRING);
- exit(0);
- }
fprintf(stderr, "%s: unrecognized argument '%s'\n",
program_name, argv[i]);
- usage(NULL);
+ usage(NULL, EXIT_FAILURE);
}
#ifdef WIN32
if (standard_out)
@@ -546,7 +557,7 @@ Window_Dump(Window window, FILE *out)
* Report the syntax for calling xwd.
*/
void
-usage(const char *errmsg)
+usage(const char *errmsg, int exitval)
{
if (errmsg != NULL)
fprintf(stderr, "%s: %s\n", program_name, errmsg);
@@ -572,7 +583,7 @@ usage(const char *errmsg)
" -icmap Use the first colormap of the screen\n"
" -screen Send the request against the root window\n"
" -silent Don't ring any bells\n", stderr);
- exit(1);
+ exit(exitval);
}
/*