cmd: Fix parsing positional args
authorAndrew Deason <adeason@sinenomine.net>
Mon, 23 May 2011 22:11:28 +0000 (17:11 -0500)
committerDerrick Brashear <shadow@dementia.org>
Tue, 24 May 2011 02:53:40 +0000 (19:53 -0700)
If the first parameter of a libcmd syntax is a flag, cmd_Parse was
skipping over positional arguments, since j will be 0 at this point
(the j variable is only used if we're processing an explicit switch).
Effectively revert this area to what it was before
a2f1ca5fd52ac2fb7e68b101bbe3da9878c10474 so such positional parameters
work again.

Also move the j variable to inside the only block in which it is used,
to try and avoid such mistakes in the future.

Change-Id: Ifa52cecf50a3f561c70de351a9ebd026e90eeeab
Reviewed-on: http://gerrit.openafs.org/4711
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementia.org>

src/cmd/cmd.c

index 3b97102..764b95c 100644 (file)
@@ -792,7 +792,6 @@ cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
     struct cmd_syndesc *ts = NULL;
     struct cmd_parmdesc *tparm;
     int i;
-    int j = 0;
     int curType;
     int positional;
     int ambig;
@@ -917,6 +916,7 @@ cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
         * are considered switches.  This allow negative numbers. */
 
        if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
+           int j;
 
            /* Find switch */
            if (strrchr(argv[i], '=') != NULL) {
@@ -989,10 +989,17 @@ cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
                continue;
            }
 
-           if (ts->parms[j].type != CMD_FLAG) {
-               code = AddItem(tparm, argv[i], pname);
-               if (code)
+           if (tparm->type == CMD_SINGLE ||
+               tparm->type == CMD_SINGLE_OR_FLAG) {
+               if (tparm->items) {
+                   fprintf(stderr, "%sToo many values after switch %s\n",
+                           NName(pname, ": "), tparm->name);
+                   code = CMD_NOTLIST;
                    goto out;
+               }
+               AddItem(tparm, argv[i], pname);        /* Add to end of list */
+           } else if (tparm->type == CMD_LIST) {
+               AddItem(tparm, argv[i], pname);        /* Add to end of list */
            }
 
            /* Now, if we're in positional mode, advance to the next item */