Fix typo in header line for mdw_DECL_ENVIRON.
[cfd] / mdwopt.c
index 37d6581..1717d2d 100644 (file)
--- a/mdwopt.c
+++ b/mdwopt.c
@@ -1,10 +1,10 @@
 /* -*-c-*-
  *
- * $Id: mdwopt.c,v 1.1 1999/05/05 19:23:47 mdw Exp $
+ * $Id: mdwopt.c,v 1.7 1999/06/18 21:59:46 mdw Exp $
  *
  * Options parsing, similar to GNU @getopt_long@
  *
- * (c) 1996 Mark Wooding
+ * (c) 1996 Straylight/Edgeware
  */
 
 /*----- Licensing notice --------------------------------------------------*
  * GNU Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with `mdwopt'; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * License along with `mdwopt'; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
  */
 
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: mdwopt.c,v $
- * Revision 1.1  1999/05/05 19:23:47  mdw
- * Initial revision
+ * Revision 1.7  1999/06/18 21:59:46  mdw
+ * Fix stupid bug which only read one word from environment variables.
+ *
+ * Revision 1.6  1999/05/20 23:00:42  mdw
+ * Little formatting things.
+ *
+ * Revision 1.5  1999/05/19 20:23:59  mdw
+ * Change naming to match newer mLib conventions.
+ *
+ * Revision 1.4  1999/05/15 10:25:38  mdw
+ * Fix copyright information.
+ *
+ * Revision 1.3  1999/05/14 18:51:42  mdw
+ * Reformat the LGPL notice slightly.
+ *
+ * Revision 1.2  1999/05/13 22:57:23  mdw
+ * Change `-ise' to `-ize' throughout.
+ *
+ * Revision 1.1.1.1  1999/05/05 19:23:47  mdw
+ * New import.  The old CVS repository was lost in a disk disaster.
  *
  * --- Previous lives ---
  *
 mdwopt_data mdwopt_global = {0, 0, 0, 1, 0, 0, 0, 0, 0};
 
 enum {
-  ord__permute = 0,                    /* Permute the options (default) */
-  ord__return = 1,                     /* Return non-option things */
-  ord__posix = 2,                      /* Do POSIX-type hacking */
-  ord__negate = 4                      /* Magic negate-next-thing flag */
+  ORD_PERMUTE = 0,                     /* Permute the options (default) */
+  ORD_RETURN = 1,                      /* Return non-option things */
+  ORD_POSIX = 2,                       /* Do POSIX-type hacking */
+  ORD_NEGATE = 4                       /* Magic negate-next-thing flag */
 };
 
 /*----- Main code ---------------------------------------------------------*/
 
-/* --- @mo__nextWord@ --- *
+/* --- @nextWord@ --- *
  *
  * Arguments:   @int argc@ = number of command line options
  *              @char *argv[]@ = pointer to command line options
@@ -107,7 +126,7 @@ enum {
  *              variable.
  */
 
-static char *mo__nextWord(int argc, char *const *argv, mdwopt_data *data)
+static char *nextWord(int argc, char *const *argv, mdwopt_data *data)
 {
   if (data->ind == -1) {
     char *p = data->env;
@@ -117,9 +136,9 @@ static char *mo__nextWord(int argc, char *const *argv, mdwopt_data *data)
     q = p;
     while (*p && !isspace((unsigned char)*p))
       p++;
-    data->env = p;
     if (*p)
       *p++ = 0;
+    data->env = p;
     if (p != q)
       return (q);
     data->env = 0;
@@ -131,7 +150,7 @@ static char *mo__nextWord(int argc, char *const *argv, mdwopt_data *data)
   return (argv[data->next++]);
 }
 
-/* --- @mo__permute@ --- *
+/* --- @permute@ --- *
  *
  * Arguments:   @char *argv[]@ = pointer to command line arguments
  *              @mdwopt_data *data@ = pointer to persistent data
@@ -141,7 +160,7 @@ static char *mo__nextWord(int argc, char *const *argv, mdwopt_data *data)
  * Use:         Moves a command line option into the right place.
  */
 
-static void mo__permute(char *const *argv, mdwopt_data *data)
+static void permute(char *const *argv, mdwopt_data *data)
 {
   char **v = (char **)argv;
   if (data->ind != -1) {
@@ -156,7 +175,7 @@ static void mo__permute(char *const *argv, mdwopt_data *data)
   }
 }
 
-/* --- @mo__findOpt@ --- *
+/* --- @findOpt@ --- *
  *
  * Arguments:   @int o@ = which option to search for
  *              @const char *shortopt@ = short options string to search
@@ -168,7 +187,7 @@ static void mo__permute(char *const *argv, mdwopt_data *data)
  * Use:         Looks up a short option in the given string.
  */
 
-static const char *mo__findOpt(int o, const char *shortopt,
+static const char *findOpt(int o, const char *shortopt,
                               mdwopt_data *data)
 {
   const char *p = shortopt;            /* Point to short opts table */
@@ -176,7 +195,7 @@ static const char *mo__findOpt(int o, const char *shortopt,
     if (!*p)                           /* No more options left */
       return (0);
 
-    if (o != *p || (p[1] != '+' && data->order & ord__negate)) {
+    if (o != *p || (p[1] != '+' && data->order & ORD_NEGATE)) {
       p++;                             /* Skip this option entry */
       while (*p == '+')                        /* Jump a `%|+|%' sign */
        p++;
@@ -253,7 +272,7 @@ static const char *mo__findOpt(int o, const char *shortopt,
  *              usually done by using a `%|+|%' instead of a `%|-|%' to
  *              introduce the option.
  *
- *              Long options, as popularised by the GNU utilities, are given
+ *              Long options, as popularized by the GNU utilities, are given
  *              long-ish memorable names, preceded by a double-dash `%|--|%'.
  *              Since their names are more than a single character, long
  *              options can't be combined in the same way as short options.
@@ -340,7 +359,7 @@ static const char *mo__findOpt(int o, const char *shortopt,
  *              the caller.  A pointer to an argument is stored in
  *              @data->arg@, or @NULL@ is stored if there was no argument.
  *              If a negated option was found, the option character is
- *              returned ORred with @gFlag_negated@ (bit 8 set).
+ *              returned ORred with @OPTF_NEGATED@ (bit 8 set).
  *
  *              Long options are described in a table.  Each entry in the
  *              table is of type @struct option@, and the table is terminated
@@ -358,9 +377,9 @@ static const char *mo__findOpt(int o, const char *shortopt,
  *              table entry.  If @flag@ is nonzero, it points to an integer
  *              to be modified by mdwopt.  Usually the value in the @val@
  *              field is simply stored in the @flag@ variable. If the flag
- *              @gFlag_switch@ is set, however, the value is combined with
+ *              @OPTF_SWITCH@ is set, however, the value is combined with
  *              the existing value of the flags using a bitwise OR.  If
- *              @gFlag_negate@ is set, then the flag bit will be cleared if a
+ *              @OPTF_NEGATE@ is set, then the flag bit will be cleared if a
  *              matching negated long option is found.  The value 0 is
  *              returned.
  *
@@ -373,11 +392,12 @@ static const char *mo__findOpt(int o, const char *shortopt,
  *              Numeric options, if enabled, cause the value `%|#|%' to be
  *              returned, and the numeric value to be stored in @data->opt@.
  *
- *              If the flag @gFlag_envVar@ is set on entry, options will be
+ *              If the flag @OPTF_ENVVAR@ is set on entry, options will be
  *              extracted from an environment variable whose name is built by
- *              capitalising all the letters of the program's name.  (This
+ *              capitalizing all the letters of the program's name.  (This
  *              allows a user to have different default settings for a
- *              program, by calling it through different symbolic links.)  */
+ *              program, by calling it through different symbolic links.)
+ */
 
 int mdwopt(int argc, char *const *argv,
           const char *shortopt,
@@ -398,33 +418,33 @@ int mdwopt(int argc, char *const *argv,
 
   /* --- See if this is the first time --- */
 
-  if (data->ind == 0 || (data->ind == 1 && ~flags & gFlag_noProgName)) {
+  if (data->ind == 0 || (data->ind == 1 && ~flags & OPTF_NOPROGNAME)) {
 
     /* --- Sort out default returning order --- */
 
     if (getenv("_POSIX_OPTION_ORDER") || /* Examine environment for opts */
        getenv("POSIXLY_CORRECT"))      /* To see if we disable features */
-      data->order = ord__posix;                /* If set, use POSIX ordering */
+      data->order = ORD_POSIX;         /* If set, use POSIX ordering */
     else
-      data->order = ord__permute;      /* Otherwise mangle the options */
+      data->order = ORD_PERMUTE;       /* Otherwise mangle the options */
 
     /* --- Now see what the caller actually wants --- */
 
     switch (shortopt[0]) {             /* Look at the first character */
       case '-':                                /* `%|-|%' turns on in-orderness */
-       data->order = ord__return;
+       data->order = ORD_RETURN;
        break;
       case '+':                                /* `%|+|%' turns on POSIXness */
-       data->order = ord__posix;
+       data->order = ORD_POSIX;
        break;
       case '!':                                /* `%|!|%' ignores POSIXness */
-       data->order = ord__permute;
+       data->order = ORD_PERMUTE;
        break;
     }
 
     /* --- Now decide on the program's name --- */
 
-    if (~flags & gFlag_noProgName) {
+    if (~flags & OPTF_NOPROGNAME) {
       p = q = (char *)argv[0];
       while (*p) {
        if (*p++ == PATHSEP)
@@ -442,7 +462,7 @@ int mdwopt(int argc, char *const *argv,
        * support this option.
        */
 
-      if (flags & gFlag_envVar && strlen(data->prog) < 48) {
+      if (flags & OPTF_ENVVAR && strlen(data->prog) < 48) {
 
        char buf[64];
 
@@ -527,7 +547,7 @@ int mdwopt(int argc, char *const *argv,
      * We identify options as being things starting with `%|-|%', and which
      * aren't equal to `%|-|%' or `%|--|%'.  We'll look for options until:
      *
-     *   * We find something which isn't an option AND @order == ord__posix@
+     *   * We find something which isn't an option AND @order == ORD_POSIX@
      *   * We find a `%|--|%'
      *   * We reach the end of the list
      *
@@ -535,16 +555,16 @@ int mdwopt(int argc, char *const *argv,
      */
 
     for (;;) {                         /* Keep looping for a while */
-      p = mo__nextWord(argc, argv, data); /* Get the next word out */
+      p = nextWord(argc, argv, data);  /* Get the next word out */
       if (!p)                          /* If there's no next word */
        return (EOF);                   /* There's no more now */
 
       /* --- See if we've found an option --- */
 
-      if ((p[0] == '-' || (p[0] == '+' && flags & gFlag_negation)) &&
+      if ((p[0] == '-' || (p[0] == '+' && flags & OPTF_NEGATION)) &&
          p[1] != 0) {
        if (strcmp(p, "--") == 0) {     /* If this is the magic marker */
-         mo__permute(argv, data);      /* Stow the magic marker item */
+         permute(argv, data);  /* Stow the magic marker item */
          return (EOF);                 /* There's nothing else to do */
        }
        break;                          /* We've found something! */
@@ -553,13 +573,13 @@ int mdwopt(int argc, char *const *argv,
       /* --- Figure out how to proceed --- */
 
       switch (data->order & 3) {
-       case ord__posix:                /* POSIX option order */
+       case ORD_POSIX:                 /* POSIX option order */
          return (EOF);                 /* This is easy */
          break;
-       case ord__permute:              /* Permute the option order */
+       case ORD_PERMUTE:               /* Permute the option order */
          break;
-       case ord__return:               /* Return each argument */
-         mo__permute(argv, data);      /* Insert word in same place */
+       case ORD_RETURN:                /* Return each argument */
+         permute(argv, data);          /* Insert word in same place */
          data->arg = p;                /* Point to the argument */
          return (0);                   /* Return the value */
       }
@@ -567,7 +587,7 @@ int mdwopt(int argc, char *const *argv,
 
     /* --- We found an option --- */
 
-    mo__permute(argv, data);           /* Do any permuting necessary */
+    permute(argv, data);               /* Do any permuting necessary */
 
     /* --- Check for a numeric option --- *
      *
@@ -575,7 +595,7 @@ int mdwopt(int argc, char *const *argv,
      * sign).  This ought to be enough.
      */
 
-    if (flags & gFlag_numbers && (p[0] == '-' || flags & gFlag_negNumber)) {
+    if (flags & OPTF_NUMBERS && (p[0] == '-' || flags & OPTF_NEGNUMBER)) {
       if (((p[1] == '+' || p[1] == '-') && isdigit((unsigned char)p[2])) ||
          isdigit((unsigned char)p[1])) {
        data->opt = strtol(p + 1, &data->arg, 10);
@@ -583,39 +603,39 @@ int mdwopt(int argc, char *const *argv,
          data->arg++;
        if (!data->arg[0])
          data->arg = 0;
-       return (p[0] == '-' ? '#' : '#' | gFlag_negated);
+       return (p[0] == '-' ? '#' : '#' | OPTF_NEGATED);
       }
     }
 
     /* --- Check for a long option --- */
 
     if (p[0] == '+')
-      data->order |= ord__negate;
+      data->order |= ORD_NEGATE;
 
     if (((p[0] == '-' && p[1] == '-') ||
-        (flags & gFlag_noShorts && !mo__findOpt(p[1], shortopt, data))) &&
-       (~flags & gFlag_noLongs))       /* Is this a long option? */
+        (flags & OPTF_NOSHORTS && !findOpt(p[1], shortopt, data))) &&
+       (~flags & OPTF_NOLONGS))        /* Is this a long option? */
     {
       int match = -1;                  /* Count matches as we go */
 
       if (p[0] == '+') {               /* If it's negated */
-       data->order |= ord__negate;     /* Set the negate flag */
+       data->order |= ORD_NEGATE;      /* Set the negate flag */
        p++;                            /* Point to the main text */
        prefix = "+";                   /* Set the prefix string up */
       } else if (p[1] == '-') {                /* If this is a `%|--|%' option */
-       if ((flags & gFlag_negation) && strncmp(p + 2, "no-", 3) == 0) {
+       if ((flags & OPTF_NEGATION) && strncmp(p + 2, "no-", 3) == 0) {
          p += 5;                       /* Point to main text */
          prefix = "--no-";             /* And set the prefix */
-         data->order |= ord__negate;   /* Set the negatedness flag */
+         data->order |= ORD_NEGATE;    /* Set the negatedness flag */
        } else {
          p += 2;                       /* Point to the main text */
          prefix = "--";                /* Remember the prefix string */
        }
       } else {
-       if ((flags & gFlag_negation) && strncmp(p + 1, "no-", 3) == 0) {
+       if ((flags & OPTF_NEGATION) && strncmp(p + 1, "no-", 3) == 0) {
          p += 4;                       /* Find the text */
          prefix = "-no-";              /* Set the prefix */
-         data->order |= ord__negate;   /* Set negatedness flag */
+         data->order |= ORD_NEGATE;    /* Set negatedness flag */
        } else {
          p++;                          /* Otherwise find the text */
          prefix = "-";                 /* And remember the prefix */
@@ -623,8 +643,8 @@ int mdwopt(int argc, char *const *argv,
       }
 
       for (i = 0; longopts[i].name; i++) { /* Loop through the options */
-       if ((data->order & ord__negate) &&
-           (~longopts[i].has_arg & gFlag_negate))
+       if ((data->order & ORD_NEGATE) &&
+           (~longopts[i].has_arg & OPTF_NEGATE))
          continue;                     /* If neg and opt doesn't allow */
 
        r = (char *) longopts[i].name;  /* Point to the name string */
@@ -652,7 +672,7 @@ int mdwopt(int argc, char *const *argv,
     botched:
       if (match == -1) {               /* If we couldn't find a match */
        if (data->err) {
-         fprintf(stderr, "%s: unrecognised option `%s%s'\n",
+         fprintf(stderr, "%s: unrecognized option `%s%s'\n",
                  data->prog,
                  prefix, p);
        }
@@ -684,7 +704,7 @@ int mdwopt(int argc, char *const *argv,
 
        case required_argument:
          if (!p) {                     /* If no argument given */
-           p = mo__nextWord(argc, argv, data);
+           p = nextWord(argc, argv, data);
 
            if (!p) {                   /* If no more arguments */
              if (data->err) {
@@ -695,7 +715,7 @@ int mdwopt(int argc, char *const *argv,
              return (noarg);
            }
 
-           mo__permute(argv, data);
+           permute(argv, data);
          }
          break;
 
@@ -708,21 +728,21 @@ int mdwopt(int argc, char *const *argv,
       /* --- Do correct things now we have a match --- */
 
       if (longopts[match].flag) {      /* If he has a @flag@ argument */
-       if (longopts[match].has_arg & gFlag_switch) {
-         if (data->order & ord__negate)
+       if (longopts[match].has_arg & OPTF_SWITCH) {
+         if (data->order & ORD_NEGATE)
            *longopts[match].flag &= ~longopts[match].val;
          else
            *longopts[match].flag |= longopts[match].val;
        } else {
-         if (data->order & ord__negate)
+         if (data->order & ORD_NEGATE)
            *longopts[match].flag = 0;
          else
            *longopts[match].flag = longopts[match].val;
        }
        return (0);                     /* And return something */
       } else {
-       if (data->order & ord__negate)
-         return (longopts[match].val | gFlag_negated);
+       if (data->order & ORD_NEGATE)
+         return (longopts[match].val | OPTF_NEGATED);
        else
          return (longopts[match].val);
       }
@@ -732,7 +752,7 @@ int mdwopt(int argc, char *const *argv,
 
     else {
       if (p[0] == '+')                 /* If starts with a `%|+|%' */
-       data->order |= ord__negate;
+       data->order |= ORD_NEGATE;
       data->list = p + 1;              /* Omit leading `%|-|%'/`%|+|%' */
     }
   }
@@ -742,12 +762,12 @@ int mdwopt(int argc, char *const *argv,
   i = *data->list++;                   /* Get the next option letter */
   data->opt = i;                       /* Store this away nicely */
 
-  p = (char *) mo__findOpt(i, shortopt, data);
+  p = (char *) findOpt(i, shortopt, data);
   if (!p) {                            /* No more options left */
     if (data->err) {
       fprintf(stderr, "%s: unknown option `%c%c'\n",
              data->prog,
-             data->order & ord__negate ? '+' : '-',
+             data->order & ORD_NEGATE ? '+' : '-',
              i);
     }
     return ('?');
@@ -764,22 +784,22 @@ int mdwopt(int argc, char *const *argv,
 
       /* --- Same code as before --- */
 
-      q = mo__nextWord(argc, argv, data); /* Read the next word */
+      q = nextWord(argc, argv, data);  /* Read the next word */
       if (!q) {                                /* If no more arguments */
        if (data->err) {
          fprintf(stderr, "%s: option `%c%c' requires an argument\n",
                  data->prog,
-                 data->order & ord__negate ? '+' : '-',
+                 data->order & ORD_NEGATE ? '+' : '-',
                  i);
        }
        return (noarg);
       }
-      mo__permute(argv, data);
+      permute(argv, data);
     }
 
     data->arg = q;
   }
-  return ((data->order & ord__negate) ? i | gFlag_negated : i);
+  return ((data->order & ORD_NEGATE) ? i | OPTF_NEGATED : i);
 }
 
 /*----- That's all, folks -------------------------------------------------*/