From 76bf671666c5d5626287788df4e1e5ea9be7dbb2 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 11 Feb 2023 16:50:24 -0800 Subject: Add -help and -version options Signed-off-by: Alan Coopersmith --- xmore.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/xmore.c b/xmore.c index 4ff53f4..f2f00c4 100644 --- a/xmore.c +++ b/xmore.c @@ -25,6 +25,10 @@ in this Software without prior written authorization from The Open Group. * */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + /* Force ANSI C prototypes from X11 headers */ #ifndef FUNCPROTO #define FUNCPROTO 15 @@ -128,6 +132,15 @@ quitXtProc(Widget w, XtPointer client_data, XtPointer callData) XtCallActionProc(w, "quit", NULL, NULL, 0); } +static void +usage(FILE *out, int exitval) +{ + fprintf(out, + "usage: %s [ x options ] [-help|-version] filename\n", + ProgramName); + exit(exitval); +} + int main( int argc, char *argv[] ) { XtAppContext app; @@ -138,6 +151,22 @@ int main( int argc, char *argv[] ) ProgramName = argv[0]; + /* Handle args that don't require opening a display */ + for (int i = 1; i < argc; i++) { + const char *argn = argv[i]; + /* accept single or double dash for -help & -version */ + if (argn[0] == '-' && argn[1] == '-') { + argn++; + } + if (strcmp (argn, "-help") == 0) { + usage(stdout, EXIT_SUCCESS); + } + if (strcmp (argn, "-version") == 0) { + puts(PACKAGE_STRING); + exit(EXIT_SUCCESS); + } + } + XtSetLanguageProc(NULL, NULL, NULL); toplevel = XtOpenApplication(&app, "XMore", options, XtNumber(options), @@ -151,8 +180,7 @@ int main( int argc, char *argv[] ) fprintf(stderr, " %s", argv[i]); } fputs("\n\n", stderr); - printf("usage: %s [ x options ] filename\n", argv[0]); - exit(EXIT_FAILURE); + usage(stderr, EXIT_FAILURE); } XtGetApplicationResources(toplevel, (XtPointer)&userOptions, resources, -- cgit v1.2.3