Revision: 76891
          http://sourceforge.net/p/brlcad/code/76891
Author:   starseeker
Date:     2020-08-22 16:03:27 +0000 (Sat, 22 Aug 2020)
Log Message:
-----------
Spotted a couple of programs in usage.sh output that are treating '-h' and '-?' 
as filenames and creating a '-?' ouput file.  Add basic option handling so they 
interpret -h and -? as help requests.

Modified Paths:
--------------
    brlcad/trunk/src/conv/asc/asc2dsp.c
    brlcad/trunk/src/conv/asc/g2asc.c

Modified: brlcad/trunk/src/conv/asc/asc2dsp.c
===================================================================
--- brlcad/trunk/src/conv/asc/asc2dsp.c 2020-08-22 15:53:17 UTC (rev 76890)
+++ brlcad/trunk/src/conv/asc/asc2dsp.c 2020-08-22 16:03:27 UTC (rev 76891)
@@ -45,6 +45,7 @@
 #include "bnetwork.h"
 #include "bu/app.h"
 #include "bu/log.h"
+#include "bu/opt.h"
 
 
 static char usage[] = "\
@@ -90,8 +91,8 @@
     /* now output it */
     ret = fwrite(&netshort, sizeof(uint16_t), 1, fpo);
     if (UNLIKELY(ret != 1)) {
-       perror("fwrite failed");
-       bu_bomb("output_netshort: write error");
+       perror("fwrite failed");
+       bu_bomb("output_netshort: write error");
     }
 
     /* prep buffer for next value */
@@ -104,7 +105,7 @@
 {
     FILE *fpi = NULL;
     FILE *fpo = NULL;
-    int d;
+    int need_help = 0;
 
     char buf[BUFSZ] = {0};
     unsigned nchars = 0;
@@ -111,16 +112,37 @@
 
     bu_setprogname(argv[0]);
 
-    if (argc != 3)
+    struct bu_opt_desc d[3];
+    BU_OPT(d[0], "h", "help",        "",         NULL,        &need_help, 
"Print help   and exit");
+    BU_OPT(d[1], "?", "",            "",         NULL,        &need_help, "");
+    BU_OPT_NULL(d[2]);
+
+    /* Skip first arg */
+    argv++; argc--;
+    struct bu_vls optparse_msg = BU_VLS_INIT_ZERO;
+    int uac = bu_opt_parse(&optparse_msg, argc, (const char **)argv, d);
+
+    if (uac == -1) {
+       bu_exit(EXIT_FAILURE, "%s", bu_vls_addr(&optparse_msg));
+    }
+    bu_vls_free(&optparse_msg);
+
+    argc = uac;
+
+    if (need_help) {
+       bu_exit(EXIT_SUCCESS, "%s", usage);
+    }
+
+    if (argc != 2)
        bu_exit(1, "%s", usage);
 
-    fpi = fopen(argv[1], "r");
+    fpi = fopen(argv[0], "r");
     if (!fpi)
-       perror(argv[1]);
+       perror(argv[0]);
 
-    fpo = fopen(argv[2], "wb");
+    fpo = fopen(argv[1], "wb");
     if (!fpo)
-       perror(argv[2]);
+       perror(argv[1]);
     if (fpi == NULL || fpo == NULL) {
        bu_exit(1, "asc2dsp: can't open files.");
     }
@@ -128,24 +150,25 @@
     buf[0] = '\0';
     nchars = 0;
 
-    while ((d = fgetc(fpi)) != EOF) {
-      unsigned char c = (unsigned char)d;
-      if (isspace(c)) {
-         /* may be end of a chunk of digits indicating need to process buffer 
*/
-         /* there should be nchars > 0 if anything is there */
-         if (nchars) {
-             /* note that the following call resets the buffer and nchars */
-             output_netshort(buf, &nchars, fpo);
-         }
-         continue;
-      } else if (c < '0' || c > '9') {
-         /* invalid char--bail */
-         bu_log("asc2dsp: invalid char '%c'\n", c);
-         bu_exit(1, "asc2dsp: FATAL");
-      }
+    int cg;
+    while ((cg = fgetc(fpi)) != EOF) {
+       unsigned char c = (unsigned char) cg;
+       if (isspace(c)) {
+           /* may be end of a chunk of digits indicating need to process 
buffer */
+           /* there should be nchars > 0 if anything is there */
+           if (nchars) {
+               /* note that the following call resets the buffer and nchars */
+               output_netshort(buf, &nchars, fpo);
+           }
+           continue;
+       } else if (c < '0' || c > '9') {
+           /* invalid char--bail */
+           bu_log("asc2dsp: invalid char '%c'\n", c);
+           bu_exit(1, "asc2dsp: FATAL");
+       }
 
-      /* copy the 0-9 to the buffer's next spot */
-      buf[nchars++] = c;
+       /* copy the 0-9 to the buffer's next spot */
+       buf[nchars++] = c;
     }
     fclose(fpi);
 
@@ -155,7 +178,7 @@
     }
     fclose(fpo);
 
-    printf("See output file '%s'.\n", argv[2]);
+    printf("See output file '%s'.\n", argv[1]);
 
     exit(0);
 }

Modified: brlcad/trunk/src/conv/asc/g2asc.c
===================================================================
--- brlcad/trunk/src/conv/asc/g2asc.c   2020-08-22 15:53:17 UTC (rev 76890)
+++ brlcad/trunk/src/conv/asc/g2asc.c   2020-08-22 16:03:27 UTC (rev 76891)
@@ -35,6 +35,7 @@
 
 #include "bu/app.h"
 #include "bu/debug.h"
+#include "bu/opt.h"
 #include "bu/units.h"
 #include "vmath.h"
 #include "rt/db4.h"
@@ -112,10 +113,33 @@
 main(int argc, char **argv)
 {
     unsigned i;
+    int need_help = 0;
 
     bu_setprogname(argv[0]);
+    Tcl_FindExecutable(argv[0]);
 
-    if (argc != 3) {
+    struct bu_opt_desc d[3];
+    BU_OPT(d[0], "h", "help",        "",         NULL,        &need_help, 
"Print help   and exit");
+    BU_OPT(d[1], "?", "",            "",         NULL,        &need_help, "");
+    BU_OPT_NULL(d[2]);
+
+    /* Skip first arg */
+    argv++; argc--;
+    struct bu_vls optparse_msg = BU_VLS_INIT_ZERO;
+    int uac = bu_opt_parse(&optparse_msg, argc, (const char **)argv, d);
+
+    if (uac == -1) {
+       bu_exit(EXIT_FAILURE, "%s", bu_vls_addr(&optparse_msg));
+    }
+    bu_vls_free(&optparse_msg);
+
+    argc = uac;
+
+    if (need_help) {
+       bu_exit(EXIT_SUCCESS, "%s", usage);
+    }
+
+    if (argc != 2) {
        bu_exit(1, "%s", usage);
     }
 
@@ -129,30 +153,37 @@
 
     bu_debug = BU_DEBUG_COREDUMP;
 
-    if (argc >= 3) {
-       iname = argv[1];
+    if (argc >= 2) {
+
+       iname = argv[0];
+
        if (BU_STR_EQUAL(iname, "-")) {
            ifp = stdin;
        } else {
            ifp = fopen(iname, "rb");
        }
-       if (!ifp)  perror(iname);
-       if (BU_STR_EQUAL(argv[2], "-")) {
+
+       if (!ifp)
+           perror(iname);
+
+       if (BU_STR_EQUAL(argv[1], "-")) {
            ofp = stdout;
        } else {
-           ofp = fopen(argv[2], "wb");
+           ofp = fopen(argv[1], "wb");
        }
-       if (!ofp)  perror(argv[2]);
+
+       if (!ofp)
+           perror(argv[1]);
+
        if (ifp == NULL || ofp == NULL) {
            bu_exit(1, "g2asc: can't open files.");
        }
     }
+
     if (isatty(fileno(ifp))) {
        bu_exit(1, "%s", usage);
     }
 
-    Tcl_FindExecutable(argv[0]);
-
     /* First, determine what version database this is */
     if (fread((char *)&record, sizeof record, 1, ifp) != 1) {
        bu_exit(2, "g2asc(%s) ERROR, file too short to be BRL-CAD database\n",

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to