xscsize.c: Quick lick of paint.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 10 Jan 2016 13:01:01 +0000 (13:01 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 10 Jan 2016 13:52:56 +0000 (13:52 +0000)
  * Use `pquis'.  (Wow.  This program is really old if it didn't know
    about `pquis'.)

  * Lay out the help text as one big string literal.

  * Lay out the option-parsing switch more compactly.

  * Notice and complain about positional arguments.

xscsize.c

index 15abcc0..19e5cca 100644 (file)
--- a/xscsize.c
+++ b/xscsize.c
 /*----- Main code ---------------------------------------------------------*/
 
 static void version(FILE *fp)
-  { fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); }
+  { pquis(fp, "$ (xtoys version " VERSION ")\n"); }
 
 static void usage(FILE *fp)
-  { fprintf(fp, "Usage: %s [-bcx] [-d display]\n", QUIS); }
+  { pquis(fp, "Usage: $ [-bcx] [-d DISPLAY]\n"); }
 
 static void help(FILE *fp)
 {
   version(fp);
   fputc('\n', fp);
   usage(stdout);
-  fputs("\n"
-"Reads the size of the X root window and outputs it in a form suitable\n"
-"for use as a shell assignment statement, defining variables XWIDTH and\n"
-"XHEIGHT.\n"
-"\n"
-"Options:\n"
-"\n"
-"-h, --help            Display this help text\n"
-"-u, --usage           Display a short usage summary\n"
-"-v, --version         Display the program's version number\n"
-"\n"
-"-d, --display=DISPLAY Choose X display to connect to\n"
-"-b, --bourne-shell    Output text suitable for a Bourne shell\n"
-"-c, --c-shell         Output text suitable for a C shell\n"
-"-x, --export          Export the variables into the environment\n",
+  fputs("\n\
+Reads the size of the X root window and outputs it in a form suitable\n\
+for use as a shell assignment statement, defining variables XWIDTH and\n\
+XHEIGHT.\n\
+\n\
+Options:\n\
+\n\
+-h, --help             Display this help text\n\
+-u, --usage            Display a short usage summary\n\
+-v, --version          Display the program's version number\n\
+\n\
+-d, --display=DISPLAY  Choose X display to connect to\n\
+-b, --bourne-shell     Output text suitable for a Bourne shell\n\
+-c, --c-shell          Output text suitable for a C shell\n\
+-x, --export           Export the variables into the environment\n",
        fp);
 }
 
@@ -79,6 +79,7 @@ int main(int argc, char *argv[])
 #define f_csh 2u
 #define f_shell 3u
 #define f_export 4u
+#define f_bogus 8u
 
   /* --- Parse command line options --- */
 
@@ -96,42 +97,23 @@ int main(int argc, char *argv[])
       {        0,              0,              0,      0 }
     };
 
-    int i = getopt_long(argc, argv, "huv d:bcx", opt, 0);
-    if (i < 0)
-      break;
+    int i = getopt_long(argc, argv, "huv" "d:bcx", opt, 0);
+    if (i < 0) break;
     switch (i) {
-      case 'h':
-       help(stdout);
-       exit(0);
-       break;
-      case 'u':
-       usage(stdout);
-       exit(0);
-       break;
-      case 'v':
-       version(stdout);
-       exit(0);
-       break;
-
-      case 'd':
-       display = optarg;
-       break;
-      case 'b':
-       f |= f_sh;
-       break;
-      case 'c':
-       f |= f_csh;
-       break;
-      case 'x':
-       f |= f_export;
-       break;
-      default:
-       usage(stderr);
-       exit(EXIT_FAILURE);
-       break;
+      case 'h': help(stdout); exit(0); break;
+      case 'u': usage(stdout); exit(0); break;
+      case 'v': version(stdout); exit(0); break;
+      case 'd': display = optarg; break;
+      case 'b': f |= f_sh; break;
+      case 'c': f |= f_csh; break;
+      case 'x': f |= f_export; break;
+      default: f |= f_bogus; break;
     }
   }
 
+  if (optind < argc) f |= f_bogus;
+  if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); }
+
   /* --- Sort out the shell type --- *
    *
    * If the shell name contains the string `csh' then assume it's a C shell.