about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2023-02-23 04:46:47 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2023-02-23 04:46:47 +0000
commitb8cf28b1dee29d8f72313527d3ff804967a099f2 (patch)
tree224759a8f486c1c63d36123b564c6b79273880b0 /lib
parenteef2e1018122a8e07fd7ed3f9d512ed8f6d66a37 (diff)
downloadnetpbm-mirror-b8cf28b1dee29d8f72313527d3ff804967a099f2.tar.gz
netpbm-mirror-b8cf28b1dee29d8f72313527d3ff804967a099f2.tar.xz
netpbm-mirror-b8cf28b1dee29d8f72313527d3ff804967a099f2.zip
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4504 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib')
-rw-r--r--lib/util/shhopt.c78
1 files changed, 49 insertions, 29 deletions
diff --git a/lib/util/shhopt.c b/lib/util/shhopt.c
index 82221fac..00f9c341 100644
--- a/lib/util/shhopt.c
+++ b/lib/util/shhopt.c
@@ -95,10 +95,12 @@ optStructCount(const optEntry opt[])
 enum Shortlong {SL_SHORT, SL_LONG};
 
 
-static int
+static void
 optMatch(optEntry       const opt[],
          const char *   const targetOpt,
-         enum Shortlong const shortLong) {
+         enum Shortlong const shortLong,
+         bool *         const foundP,
+         int *          const optIndexP) {
 /*------------------------------------------------------------------------
  |  FUNCTION      Find a matching option.
  |
@@ -107,7 +109,9 @@ optMatch(optEntry       const opt[],
  |                           e.g. "verbose" or "height=5"
  |                shortLong  whether to match short option or long
  |
- |  RETURNS       Index to the option if found, -1 if not found.
+ |  RETURNS       *foundP     there is a matching option class the table
+ !                *optIndexP  index in the option class table
+ |                            meaningless if *foundP is false
  |
  |  DESCRIPTION   Short options are matched from the first character in
  |                the given string.
@@ -115,12 +119,12 @@ optMatch(optEntry       const opt[],
  |                Where multiple entries in opt[] match, return the first.
  */
 
-    unsigned int const nopt = optStructCount(opt);
+    unsigned int const optCt = optStructCount(opt);
 
     unsigned int q;
     unsigned int matchlen;
-
-    matchlen = 0;  /* initial value */
+    bool found;
+    unsigned int optIndex;
 
     if (shortLong == SL_LONG) {
         const char * const equalPos = strchr(targetOpt, '=');
@@ -128,24 +132,31 @@ optMatch(optEntry       const opt[],
             matchlen = equalPos - &targetOpt[0];
         else
             matchlen = strlen(targetOpt);
-    }
-    for (q = 0; q < nopt; ++q) {
+    } else
+        matchlen = 0;
+
+    for (q = 0, found = false; q < optCt && !found; ++q) {
         switch (shortLong) {
         case SL_LONG: {
             if (opt[q].longName) {
-                if (strneq(targetOpt, opt[q].longName, matchlen))
-                    return q;
+                if (strneq(targetOpt, opt[q].longName, matchlen)) {
+                    found = true;
+                    optIndex = q;
+                }
             }
         }
         case SL_SHORT: {
             if (opt[q].shortName) {
-                if (targetOpt[0] == opt[q].shortName)
-                    return q;
+                if (targetOpt[0] == opt[q].shortName) {
+                    found = true;
+                    optIndex = q;
+                }
             }
         }
         }
     }
-    return -1;
+    *foundP    = found;
+    *optIndexP = optIndex;
 }
 
 
@@ -565,13 +576,12 @@ pm_optSetFatalFunc(void (*f)(const char *, ...)) {
 void
 pm_optParseOptions(int *argc, char *argv[], optStruct opt[], int allowNegNum)
 {
-    int  ai,        /* argv index. */
-         optarg,    /* argv index of option argument, or -1 if none. */
-         mi,        /* Match index in opt. */
-         done;
-    char *arg,      /* Pointer to argument to an option. */
-         *o,        /* pointer to an option character */
-         *p;
+    int ai;        /* argv index. */
+    int optarg;    /* argv index of option argument, or -1 if none. */
+    int done;
+    char * arg;      /* Pointer to argument to an option. */
+    char * o;        /* pointer to an option character */
+    char * p;
 
     optEntry *opt_table;  /* malloc'ed array */
 
@@ -588,7 +598,7 @@ pm_optParseOptions(int *argc, char *argv[], optStruct opt[], int allowNegNum)
          *  "--" indicates that the rest of the argv-array does not
          *  contain options.
          */
-        if (strcmp(argv[ai], "--") == 0) {
+        if (streq(argv[ai], "--")) {
             argvRemove(argc, argv, ai);
             break;
         }
@@ -596,10 +606,13 @@ pm_optParseOptions(int *argc, char *argv[], optStruct opt[], int allowNegNum)
         if (allowNegNum && argv[ai][0] == '-' && ISDIGIT(argv[ai][1])) {
             ++ai;
             continue;
-        } else if (strncmp(argv[ai], "--", 2) == 0) {
+        } else if (strneq(argv[ai], "--", 2)) {
+            bool found;
+            int mi;
             /* long option */
             /* find matching option */
-            if ((mi = optMatch(opt_table, argv[ai] + 2, SL_LONG)) < 0)
+            optMatch(opt_table, argv[ai] + 2, SL_LONG, &found, &mi);
+            if (!found)
                 optFatal("unrecognized option `%s'", argv[ai]);
 
             /* possibly locate the argument to this option. */
@@ -638,8 +651,11 @@ pm_optParseOptions(int *argc, char *argv[], optStruct opt[], int allowNegNum)
             done = 0;
             optarg = -1;
             while (*o && !done) {
+                bool found;
+                int mi;
                 /* find matching option */
-                if ((mi = optMatch(opt_table, o, SL_SHORT)) < 0)
+                optMatch(opt_table, o, SL_SHORT, &found, &mi);
+                if (!found)
                     optFatal("unrecognized option `-%c'", *o);
 
                 /* does this option take an argument? */
@@ -688,7 +704,6 @@ parse_short_option_token(char *argv[], const int argc, const int ai,
 -----------------------------------------------------------------------------*/
     char *o;  /* A short option character */
     char *arg;
-    int mi;   /* index into option table */
     unsigned char processed_arg;  /* boolean */
         /* We processed an argument to one of the one-character options.
            This necessarily means there are no more options in this token
@@ -700,8 +715,11 @@ parse_short_option_token(char *argv[], const int argc, const int ai,
     o = argv[ai] + 1;
     processed_arg = 0;  /* initial value */
     while (*o && !processed_arg) {
+        bool found;
+        int mi;   /* index into option table */
 		/* find matching option */
-		if ((mi = optMatch(opt_table, o, SL_SHORT)) < 0)
+		optMatch(opt_table, o, SL_SHORT, &found, &mi);
+		if (!found)
 		    optFatal("unrecognized option `-%c'", *o);
 
 		/* does this option take an argument? */
@@ -730,7 +748,7 @@ static void
 fatalUnrecognizedLongOption(const char * const optionName,
                             optEntry     const optTable[]) {
 
-    unsigned int const nopt = optStructCount(optTable);
+    unsigned int const optCt = optStructCount(optTable);
 
     unsigned int q;
 
@@ -739,7 +757,7 @@ fatalUnrecognizedLongOption(const char * const optionName,
     optList[0] = '\0';  /* initial value */
 
     for (q = 0;
-         q < nopt && strlen(optList) + 1 <= sizeof(optList);
+         q < optCt && strlen(optList) + 1 <= sizeof(optList);
          ++q) {
 
         const optEntry * const optEntryP = &optTable[q];
@@ -786,6 +804,7 @@ parse_long_option(char *   const argv[],
          "=".  NULL if no "=" in the token.
          */
     char *arg;     /* The argument of an option; NULL if none */
+    bool found;
     int mi;    /* index into option table */
 
     /* The current token is an option, and its name starts at
@@ -793,7 +812,8 @@ parse_long_option(char *   const argv[],
     */
     *tokens_consumed_p = 1;  /* initial assumption */
     /* find matching option */
-    if ((mi = optMatch(opt_table, &argv[ai][namepos], SL_LONG)) < 0)
+    optMatch(opt_table, &argv[ai][namepos], SL_LONG, &found, &mi);
+    if (!found)
         fatalUnrecognizedLongOption(argv[ai], opt_table);
 
     /* possibly locate the argument to this option. */