From a53f6c680d78e41e17e1b6af05d588f5d0083f2d Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sun, 5 Feb 2023 18:38:18 -0800 Subject: atobm, bmtoa, bitmap: Add -help and -version options Signed-off-by: Alan Coopersmith --- BitEdit.c | 19 +++++++++++++++++++ atobm.c | 22 ++++++++++++++++++---- bmtoa.c | 25 ++++++++++++++++++++----- man/bitmap.man | 8 ++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/BitEdit.c b/BitEdit.c index a1c6dab..72ff26d 100644 --- a/BitEdit.c +++ b/BitEdit.c @@ -68,6 +68,8 @@ where options include all standard toolkit options plus:\n\ -stipple filename\n\ -hl color\n\ -fr color\n\ + -help\n\ + -version\n\ \n\ The default WIDTHxHEIGHT is 16x16.\n"; @@ -979,6 +981,23 @@ int main(int argc, char *argv[]) Widget radio_group = NULL; XtPointer radio_data = NULL; + /* Handle args that don't require opening a display */ + for (int a = 1; a < argc; a++) { + const char *argn = argv[a]; + /* accept single or double dash for -help & -version */ + if (argn[0] == '-' && argn[1] == '-') { + argn++; + } + if (strcmp(argn, "-help") == 0) { + fprintf(stderr, "usage: %s %s", argv[0], usage); + exit(0); + } + if (strcmp(argn, "-version") == 0) { + puts(PACKAGE_STRING); + exit(0); + } + } + top_widget = XtInitialize(NULL, "Bitmap", options, XtNumber(options), &argc, argv); diff --git a/atobm.c b/atobm.c index d292876..198e043 100644 --- a/atobm.c +++ b/atobm.c @@ -47,7 +47,7 @@ static void doit(FILE *fp, const char *filename, const char *chars, int xhot, int yhot, const char *name); static void _X_NORETURN _X_COLD -usage (const char *msg) +usage (const char *msg, int exitval) { if (msg) fprintf(stderr, "%s: %s\n", ProgramName, msg); @@ -55,10 +55,12 @@ usage (const char *msg) ProgramName, "where options include:\n" " -chars cc chars to use for 0 and 1 bits, respectively\n" + " -help print this message\n" " -name variable name to use in bitmap file\n" + " -version print version info\n" " -xhot number x position of hotspot\n" " -yhot number y position of hotspot\n"); - exit (1); + exit(exitval); } static void _X_NORETURN _X_COLD @@ -67,7 +69,7 @@ missing_arg (const char *option) char msg[32]; snprintf(msg, sizeof(msg), "%s requires an argument", option); - usage(msg); + usage(msg, 1); } static char * @@ -123,10 +125,21 @@ main (int argc, char *argv[]) if (++i >= argc) missing_arg("-chars"); chars = argv[i]; continue; + case 'h': + if (strcmp(arg, "-help") == 0) { + usage(NULL, 0); + } + goto unknown; case 'n': if (++i >= argc) missing_arg("-name"); name = argv[i]; continue; + case 'v': + if (strcmp(arg, "-version") == 0) { + puts(PACKAGE_STRING); + exit(0); + } + goto unknown; case 'x': if (++i >= argc) missing_arg("-xhot"); xhot = atoi (argv[i]); @@ -136,9 +149,10 @@ main (int argc, char *argv[]) yhot = atoi (argv[i]); continue; default: + unknown: fprintf(stderr, "%s: unrecognized option '%s'\n", ProgramName, argv[i]); - usage (NULL); + usage(NULL, 1); } } else { filename = arg; diff --git a/bmtoa.c b/bmtoa.c index a9de614..da5d0d2 100644 --- a/bmtoa.c +++ b/bmtoa.c @@ -54,13 +54,16 @@ static void print_scanline (unsigned int width, unsigned int height, unsigned const char *data, const char *chars); static void _X_NORETURN -usage (void) +usage (int exitval) { fprintf (stderr, "usage: %s [-options ...] [filename]\n\n%s\n", ProgramName, "where options include:\n" - " -chars cc chars to use for 0 and 1 bits, respectively\n"); - exit (1); + " -chars cc chars to use for 0 and 1 bits, respectively\n" + " -help print this usage message\n" + " -version print version information\n" + ); + exit (exitval); } static char * @@ -133,14 +136,26 @@ main (int argc, char *argv[]) if (++i >= argc) { fprintf(stderr, "%s: -chars requires an argument\n", ProgramName); - usage (); + usage(1); } chars = argv[i]; continue; + case 'h': + if (strcmp(arg, "-help") == 0) { + usage(0); + } + goto unknown; + case 'v': + if (strcmp(arg, "-version") == 0) { + puts(PACKAGE_STRING); + exit(0); + } + goto unknown; default: + unknown: fprintf(stderr, "%s: unrecognized option '%s'\n", ProgramName, argv[i]); - usage (); + usage(1); } } else { filename = arg; diff --git a/man/bitmap.man b/man/bitmap.man index 924b8b3..992d551 100644 --- a/man/bitmap.man +++ b/man/bitmap.man @@ -152,6 +152,14 @@ are allowed. By default, no hotspot information is included. .B \-yhot \fInumber\fP This option specifies the Y coordinate of the hotspot. Only positive values are allowed. By default, no hotspot information is included. +.PP +All three commands also accept these options: +.TP 4 +.B \-help +Prints a usage message and exits. +.TP 4 +.B \-version +Prints version info and exits. .SH USAGE \fIBitmap\fP displays grid in which each square represents a single bit in the picture being edited. Actual size of the bitmap image, as -- cgit v1.2.3