cmd: Tidy up -help output
[openafs.git] / src / cmd / cmd.c
index 652b56b..204508d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
 
-#include <sys/types.h>
 #include <ctype.h>
-#include "cmd.h"
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
 #include <assert.h>
 
+#include "cmd.h"
+
 /* declaration of private token type */
 struct cmd_token {
     struct cmd_token *next;
@@ -30,6 +28,8 @@ static struct cmd_syndesc *allSyntax = 0;
 static int noOpcodes = 0;
 static int (*beforeProc) (struct cmd_syndesc * ts, void *beforeRock) = NULL;
 static int (*afterProc) (struct cmd_syndesc * ts, void *afterRock) = NULL;
+static int enablePositional = 1;
+static int enableAbbreviation = 1;
 static void *beforeRock, *afterRock;
 static char initcmd_opcode[] = "initcmd";      /*Name of initcmd opcode */
 
@@ -75,6 +75,7 @@ FindType(struct cmd_syndesc *as, char *aname)
     size_t cmdlen;
     int ambig;
     int best;
+    struct cmd_item *alias;
 
     /* Allow --long-style options. */
     if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
@@ -91,8 +92,17 @@ FindType(struct cmd_syndesc *as, char *aname)
            return i;
        if (strlen(as->parms[i].name) < cmdlen)
            continue;
+
+       /* Check for aliases, which must be full matches */
+       alias = as->parms[i].aliases;
+       while (alias != NULL) {
+           if (strcmp(alias->data, aname) == 0)
+               return i;
+           alias = alias->next;
+       }
+
        /* A hidden option must be a full match (no best matches) */
-       if (as->parms[i].flags & CMD_HIDE)
+       if (as->parms[i].flags & CMD_HIDE || !enableAbbreviation)
            continue;
 
        if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
@@ -150,23 +160,20 @@ FindSyntax(char *aname, int *aambig)
 }
 
 /* print the help for a single parameter */
-static void
-PrintParmHelp(struct cmd_parmdesc *aparm)
+static char *
+ParmHelpString(struct cmd_parmdesc *aparm)
 {
+    char *str;
     if (aparm->type == CMD_FLAG) {
-#ifdef notdef
-       /* doc people don't like seeing this information */
-       if (aparm->help)
-           printf(" (%s)", aparm->help);
-#endif
-    } else if (aparm->help) {
-       printf(" <%s>", aparm->help);
-       if (aparm->type == CMD_LIST)
-           printf("+");
-    } else if (aparm->type == CMD_SINGLE)
-       printf(" <arg>");
-    else if (aparm->type == CMD_LIST)
-       printf(" <arg>+");
+       return strdup("");
+    } else {
+       asprintf(&str, " %s<%s>%s%s",
+                aparm->type == CMD_SINGLE_OR_FLAG?"[":"",
+                aparm->help?aparm->help:"arg",
+                aparm->type == CMD_LIST?"+":"",
+                aparm->type == CMD_SINGLE_OR_FLAG?"]":"");
+       return str;
+    }
 }
 
 extern char *AFSVersion;
@@ -183,30 +190,48 @@ PrintSyntax(struct cmd_syndesc *as)
 {
     int i;
     struct cmd_parmdesc *tp;
+    char *str;
+    size_t len;
+    size_t xtralen;
 
     /* now print usage, from syntax table */
     if (noOpcodes)
-       printf("Usage: %s", as->a0name);
+       asprintf(&str, "Usage: %s", as->a0name);
     else {
        if (!strcmp(as->name, initcmd_opcode))
-           printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
+           asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name);
        else
-           printf("Usage: %s%s", NName(as->a0name, " "), as->name);
+           asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name);
     }
 
+    len = strlen(str);
+    printf("%s", str);
+    free(str);
+
     for (i = 0; i < CMD_MAXPARMS; i++) {
        tp = &as->parms[i];
        if (tp->type == 0)
            continue;           /* seeked over slot */
        if (tp->flags & CMD_HIDE)
            continue;           /* skip hidden options */
-       printf(" ");
-       if (tp->flags & CMD_OPTIONAL)
-           printf("[");
-       printf("%s", tp->name);
-       PrintParmHelp(tp);
-       if (tp->flags & CMD_OPTIONAL)
-           printf("]");
+       /* Work out if we can fit what we want to on this line, or if we need to
+        * start a new one */
+       str = ParmHelpString(tp);
+       xtralen = 1 + strlen(tp->name) + strlen(str) +
+                 ((tp->flags & CMD_OPTIONAL)? 2: 0);
+
+       if (len + xtralen > 78) {
+           printf("\n        ");
+           len = 8;
+       }
+
+       printf(" %s%s%s%s",
+              tp->flags & CMD_OPTIONAL?"[":"",
+              tp->name,
+              str,
+              tp->flags & CMD_OPTIONAL?"]":"");
+       free(str);
+       len+=xtralen;
     }
     printf("\n");
 }
@@ -391,15 +416,15 @@ cmd_CreateSyntax(char *aname,
 
     /* can't have two cmds in no opcode mode */
     if (noOpcodes)
-       return (struct cmd_syndesc *)0;
+       return NULL;
 
-    td = (struct cmd_syndesc *)calloc(1, sizeof(struct cmd_syndesc));
+    td = calloc(1, sizeof(struct cmd_syndesc));
     assert(td);
     td->aliasOf = td;          /* treat aliasOf as pointer to real command, no matter what */
 
     /* copy in name, etc */
     if (aname) {
-       td->name = (char *)malloc(strlen(aname) + 1);
+       td->name = malloc(strlen(aname) + 1);
        assert(td->name);
        strcpy(td->name, aname);
     } else {
@@ -411,7 +436,7 @@ cmd_CreateSyntax(char *aname,
        if (ahelp == (char *)CMD_HIDDEN) {
            td->flags |= CMD_HIDDEN;
        } else {
-           td->help = (char *)malloc(strlen(ahelp) + 1);
+           td->help = malloc(strlen(ahelp) + 1);
            assert(td->help);
            strcpy(td->help, ahelp);
        }
@@ -434,10 +459,10 @@ cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
 {
     struct cmd_syndesc *td;
 
-    td = (struct cmd_syndesc *)malloc(sizeof(struct cmd_syndesc));
+    td = malloc(sizeof(struct cmd_syndesc));
     assert(td);
     memcpy(td, as, sizeof(struct cmd_syndesc));
-    td->name = (char *)malloc(strlen(aname) + 1);
+    td->name = malloc(strlen(aname) + 1);
     assert(td->name);
     strcpy(td->name, aname);
     td->flags |= CMD_ALIAS;
@@ -454,6 +479,18 @@ cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
     return 0;                  /* all done */
 }
 
+void
+cmd_DisablePositionalCommands(void)
+{
+    enablePositional = 0;
+}
+
+void
+cmd_DisableAbbreviations(void)
+{
+    enableAbbreviation = 0;
+}
+
 int
 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
 {
@@ -471,27 +508,59 @@ cmd_Seek(struct cmd_syndesc *as, int apos)
 }
 
 int
-cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
-           afs_int32 aflags, char *ahelp)
+cmd_AddParmAtOffset(struct cmd_syndesc *as, char *aname, int atype,
+                   afs_int32 aflags, char *ahelp, int ref)
 {
     struct cmd_parmdesc *tp;
 
-    if (as->nParms >= CMD_MAXPARMS)
+    if (ref >= CMD_MAXPARMS)
        return CMD_EXCESSPARMS;
-    tp = &as->parms[as->nParms++];
+    tp = &as->parms[ref];
 
-    tp->name = (char *)malloc(strlen(aname) + 1);
+    tp->name = malloc(strlen(aname) + 1);
     assert(tp->name);
     strcpy(tp->name, aname);
     tp->type = atype;
     tp->flags = aflags;
     tp->items = NULL;
     if (ahelp) {
-       tp->help = (char *)malloc(strlen(ahelp) + 1);
+       tp->help = malloc(strlen(ahelp) + 1);
        assert(tp->help);
        strcpy(tp->help, ahelp);
     } else
        tp->help = NULL;
+
+    tp->aliases = NULL;
+
+    if (as->nParms <= ref)
+       as->nParms = ref+1;
+
+    return 0;
+}
+
+int
+cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
+           afs_int32 aflags, char *ahelp)
+{
+    if (as->nParms >= CMD_MAXPARMS)
+       return CMD_EXCESSPARMS;
+
+    return cmd_AddParmAtOffset(as, aname, atype, aflags, ahelp, as->nParms++);
+}
+
+int
+cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
+{
+    struct cmd_item *item;
+
+    if (pos > as->nParms)
+       return CMD_EXCESSPARMS;
+
+    item = calloc(1, sizeof(struct cmd_item));
+    item->data = strdup(alias);
+    item->next = as->parms[pos].aliases;
+    as->parms[pos].aliases = item;
+
     return 0;
 }
 
@@ -500,9 +569,9 @@ static int
 AddItem(struct cmd_parmdesc *aparm, char *aval)
 {
     struct cmd_item *ti, *ni;
-    ti = (struct cmd_item *)calloc(1, sizeof(struct cmd_item));
+    ti = calloc(1, sizeof(struct cmd_item));
     assert(ti);
-    ti->data = (char *)malloc(strlen(aval) + 1);
+    ti->data = malloc(strlen(aval) + 1);
     assert(ti->data);
     strcpy(ti->data, aval);
     /* now put ti at the *end* of the list */
@@ -547,6 +616,10 @@ ResetSyntax(struct cmd_syndesc *as)
     tp = as->parms;
     for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
        switch (tp->type) {
+       case CMD_SINGLE_OR_FLAG:
+           if (tp->items == &dummy)
+               break;
+           /* Deliberately fall through here */
        case CMD_SINGLE:
        case CMD_LIST:
            /* free whole list in both cases, just for fun */
@@ -587,7 +660,9 @@ SetupExpandsFlag(struct cmd_syndesc *as)
     return 0;
 }
 
-/*Take the current argv & argc and alter them so that the initialization opcode is made to appear.  This is used in cases where the initialization opcode is implicitly invoked.*/
+/* Take the current argv & argc and alter them so that the initialization
+ * opcode is made to appear.  This is used in cases where the initialization
+ * opcode is implicitly invoked.*/
 static char **
 InsertInitOpcode(int *aargc, char **aargv)
 {
@@ -595,16 +670,17 @@ InsertInitOpcode(int *aargc, char **aargv)
     char *pinitopcode;         /*Ptr to space for name of init opcode */
     int i;                     /*Loop counter */
 
-    /*Allocate the new argv array, plus one for the new opcode, plus one more for the trailing null pointer */
-    newargv = (char **)malloc(((*aargc) + 2) * sizeof(char *));
+    /* Allocate the new argv array, plus one for the new opcode, plus one
+     * more for the trailing null pointer */
+    newargv = malloc(((*aargc) + 2) * sizeof(char *));
     if (!newargv) {
        fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
                aargv[0], *aargc);
        return (NULL);
     }
 
-    /*Create space for the initial opcode & fill it in */
-    pinitopcode = (char *)malloc(sizeof(initcmd_opcode));
+    /* Create space for the initial opcode & fill it in */
+    pinitopcode = malloc(sizeof(initcmd_opcode));
     if (!pinitopcode) {
        fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
        free(newargv);
@@ -612,17 +688,19 @@ InsertInitOpcode(int *aargc, char **aargv)
     }
     strcpy(pinitopcode, initcmd_opcode);
 
-    /*Move all the items in the old argv into the new argv, in their proper places */
+    /* Move all the items in the old argv into the new argv, in their
+     * proper places */
     for (i = *aargc; i > 1; i--)
        newargv[i] = aargv[i - 1];
 
-    /*Slip in the opcode and the trailing null pointer, and bump the argument count up by one for the new opcode */
+    /* Slip in the opcode and the trailing null pointer, and bump the
+     * argument count up by one for the new opcode */
     newargv[0] = aargv[0];
     newargv[1] = pinitopcode;
     (*aargc)++;
     newargv[*aargc] = NULL;
 
-    /*Return the happy news */
+    /* Return the happy news */
     return (newargv);
 
 }                              /*InsertInitOpcode */
@@ -644,44 +722,57 @@ NoParmsOK(struct cmd_syndesc *as)
     return 1;
 }
 
-/* Call the appropriate function, or return syntax error code.  Note: if no opcode is specified, an initialization routine exists, and it has NOT been called before, we invoke the special initialization opcode*/
+/* Add help, apropos commands once */
+static void
+initSyntax(void)
+{
+    struct cmd_syndesc *ts;
+
+    if (!noOpcodes) {
+       ts = cmd_CreateSyntax("help", HelpProc, NULL,
+                             "get help on commands");
+       cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
+       cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
+
+       ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
+                             "search by help text");
+       cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
+       ts = cmd_CreateSyntax("version", VersionProc, NULL,
+                             (char *)CMD_HIDDEN);
+       ts = cmd_CreateSyntax("-version", VersionProc, NULL,
+                             (char *)CMD_HIDDEN);
+       ts = cmd_CreateSyntax("-help", HelpProc, NULL,
+                             (char *)CMD_HIDDEN);
+       ts = cmd_CreateSyntax("--version", VersionProc, NULL,
+                             (char *)CMD_HIDDEN);
+       ts = cmd_CreateSyntax("--help", HelpProc, NULL,
+                             (char *)CMD_HIDDEN);
+    }
+}
+
+/* Call the appropriate function, or return syntax error code.  Note: if
+ * no opcode is specified, an initialization routine exists, and it has
+ * NOT been called before, we invoke the special initialization opcode
+ */
 int
-cmd_Dispatch(int argc, char **argv)
+cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
 {
     char *pname;
-    struct cmd_syndesc *ts;
+    struct cmd_syndesc *ts = NULL;
     struct cmd_parmdesc *tparm;
     afs_int32 i, j;
     int curType;
     int positional;
     int ambig;
+    int code = 0;
     static int initd = 0;      /*Is this the first time this routine has been called? */
     static int initcmdpossible = 1;    /*Should be consider parsing the initial command? */
 
+    *outsyntax = NULL;
+
     if (!initd) {
        initd = 1;
-       /* Add help, apropos commands once */
-       if (!noOpcodes) {
-           ts = cmd_CreateSyntax("help", HelpProc, (char *)0,
-                                 "get help on commands");
-           cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
-           cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
-
-           ts = cmd_CreateSyntax("apropos", AproposProc, (char *)0,
-                                 "search by help text");
-           cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED,
-                       "help string");
-           ts = cmd_CreateSyntax("version", VersionProc, (char *)0,
-                                 (char *)CMD_HIDDEN);
-           ts = cmd_CreateSyntax("-version", VersionProc, (char *)0,
-                                 (char *)CMD_HIDDEN);
-           ts = cmd_CreateSyntax("-help", HelpProc, (char *)0,
-                                 (char *)CMD_HIDDEN);
-           ts = cmd_CreateSyntax("--version", VersionProc, (char *)0,
-                                 (char *)CMD_HIDDEN);
-           ts = cmd_CreateSyntax("--help", HelpProc, (char *)0,
-                                 (char *)CMD_HIDDEN);
-       }
+       initSyntax();
     }
 
     /*Remember the program name */
@@ -691,17 +782,19 @@ cmd_Dispatch(int argc, char **argv)
        if (argc == 1) {
            if (!NoParmsOK(allSyntax)) {
                printf("%s: Type '%s -help' for help\n", pname, pname);
-               return (CMD_USAGE);
+               code = CMD_USAGE;
+               goto out;
            }
        }
     } else {
        if (argc < 2) {
            /* if there is an initcmd, don't print an error message, just
             * setup to use the initcmd below. */
-           if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *)0))) {
+           if (!(initcmdpossible && FindSyntax(initcmd_opcode, NULL))) {
                printf("%s: Type '%s help' or '%s help <topic>' for help\n",
                       pname, pname, pname);
-               return (CMD_USAGE);
+               code = CMD_USAGE;
+               goto out;
            }
        }
     }
@@ -718,7 +811,7 @@ cmd_Dispatch(int argc, char **argv)
                 * see if there is a descriptor for the initialization opcode.
                 * Only try this once. */
                initcmdpossible = 0;
-               ts = FindSyntax(initcmd_opcode, (int *)0);
+               ts = FindSyntax(initcmd_opcode, NULL);
                if (!ts) {
                    /*There is no initialization opcode available, so we declare
                     * an error */
@@ -733,7 +826,8 @@ cmd_Dispatch(int argc, char **argv)
                                "Unrecognized operation '%s'; type '%shelp' for list\n",
                                argv[1], NName(pname, " "));
                    }
-                   return (CMD_UNKNOWNCMD);
+                   code = CMD_UNKNOWNCMD;
+                   goto out;
                } else {
                    /*Found syntax structure for an initialization opcode.  Fix
                     * up argv and argc to relect what the user
@@ -742,7 +836,8 @@ cmd_Dispatch(int argc, char **argv)
                        fprintf(stderr,
                                "%sCan't insert implicit init opcode into command line\n",
                                NName(pname, ": "));
-                       return (CMD_INTERNALERROR);
+                       code = CMD_INTERNALERROR;
+                       goto out;
                    }
                }
            } /*Initial opcode not yet attempted */
@@ -759,19 +854,22 @@ cmd_Dispatch(int argc, char **argv)
                            "Unrecognized operation '%s'; type '%shelp' for list\n",
                            argv[1], NName(pname, " "));
                }
-               return CMD_UNKNOWNCMD;
+               code = CMD_UNKNOWNCMD;
+               goto out;
            }
        }                       /*Argv[1] is not a valid opcode */
     }                          /*Opcodes are defined */
 
-    /* Found the descriptor; start parsing.  curType is the type we're trying to parse */
+    /* Found the descriptor; start parsing.  curType is the type we're
+     * trying to parse */
     curType = 0;
 
     /* We start off parsing in "positional" mode, where tokens are put in
      * slots positionally.  If we find a name that takes args, we go
      * out of positional mode, and from that point on, expect a switch
      * before any particular token. */
-    positional = 1;            /* Are we still in the positional region of the cmd line? */
+
+    positional = enablePositional;     /* Accepting positional cmds ? */
     i = noOpcodes ? 1 : 2;
     SetupExpandsFlag(ts);
     for (; i < argc; i++) {
@@ -790,14 +888,14 @@ cmd_Dispatch(int argc, char **argv)
                else
                    fprintf(stderr, "'%shelp %s' for detailed help\n",
                            NName(argv[0], " "), ts->name);
-               ResetSyntax(ts);
-               return (CMD_UNKNOWNSWITCH);
+               code = CMD_UNKNOWNSWITCH;
+               goto out;
            }
            if (j >= CMD_MAXPARMS) {
                fprintf(stderr, "%sInternal parsing error\n",
                        NName(pname, ": "));
-               ResetSyntax(ts);
-               return (CMD_INTERNALERROR);
+               code = CMD_INTERNALERROR;
+               goto out;
            }
            if (ts->parms[j].type == CMD_FLAG) {
                ts->parms[j].items = &dummy;
@@ -810,8 +908,8 @@ cmd_Dispatch(int argc, char **argv)
            /* Try to fit in this descr */
            if (curType >= CMD_MAXPARMS) {
                fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
-               ResetSyntax(ts);
-               return (CMD_TOOMANY);
+               code = CMD_TOOMANY;
+               goto out;
            }
            tparm = &ts->parms[curType];
 
@@ -829,12 +927,13 @@ cmd_Dispatch(int argc, char **argv)
                continue;
            }
 
-           if (tparm->type == CMD_SINGLE) {
+           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);
-                   ResetSyntax(ts);
-                   return (CMD_NOTLIST);
+                   code = CMD_NOTLIST;
+                   goto out;
                }
                AddItem(tparm, argv[i]);        /* Add to end of list */
            } else if (tparm->type == CMD_LIST) {
@@ -849,16 +948,16 @@ cmd_Dispatch(int argc, char **argv)
     /* keep track of this for messages */
     ts->a0name = argv[0];
 
-    /* If we make it here, all the parameters are filled in.  Check to see if this
-     * is a -help version.  Must do this before checking for all required parms,
-     * otherwise it is a real nuisance */
+    /* If we make it here, all the parameters are filled in.  Check to see if
+     * this is a -help version.  Must do this before checking for all
+     * required parms, otherwise it is a real nuisance */
     if (ts->parms[CMD_HELPPARM].items) {
        PrintSyntax(ts);
        /* Display full help syntax if we don't have subcommands */
        if (noOpcodes)
            PrintFlagHelp(ts);
-       ResetSyntax(ts);
-       return 0;
+       code = CMD_USAGE;
+       goto out;
     }
 
     /* Parsing done, see if we have all of our required parameters */
@@ -867,46 +966,75 @@ cmd_Dispatch(int argc, char **argv)
        if (tparm->type == 0)
            continue;           /* Skipped parm slot */
        if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
-           fprintf(stderr, "%s The field '%s' isn't completed properly\n",
+           if (tparm->type == CMD_SINGLE_OR_FLAG) {
+               tparm->items = &dummy;
+           } else {
+               fprintf(stderr, "%s The field '%s' isn't completed properly\n",
                    NName(pname, ": "), tparm->name);
-           ResetSyntax(ts);
-           tparm->flags &= ~CMD_PROCESSED;
-           return (CMD_TOOFEW);
+               code = CMD_TOOFEW;
+               goto out;
+           }
        }
        if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
            fprintf(stderr, "%sMissing required parameter '%s'\n",
                    NName(pname, ": "), tparm->name);
-           ResetSyntax(ts);
-           tparm->flags &= ~CMD_PROCESSED;
-           return (CMD_TOOFEW);
+           code = CMD_TOOFEW;
+           goto out;
        }
        tparm->flags &= ~CMD_PROCESSED;
     }
+    *outsyntax = ts;
+
+out:
+    if (code && ts != NULL)
+       ResetSyntax(ts);
+
+    return code;
+}
+
+int
+cmd_Dispatch(int argc, char **argv)
+{
+    struct cmd_syndesc *ts = NULL;
+    int code;
+
+    code = cmd_Parse(argc, argv, &ts);
+    if (code)
+       return code;
 
     /*
-     * Before calling the beforeProc and afterProc and all the implications 
-     * from those calls, check if the help procedure was called and call it now.
+     * Before calling the beforeProc and afterProc and all the implications
+     * from those calls, check if the help procedure was called and call it
+     * now.
      */
     if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
-       i = (*ts->proc) (ts, ts->rock);
-       ResetSyntax(ts);
-       return (i);
+       code = (*ts->proc) (ts, ts->rock);
+       goto out;
     }
 
     /* Now, we just call the procedure and return */
     if (beforeProc)
-       i = (*beforeProc) (ts, beforeRock);
-    else
-       i = 0;
-    if (i) {
-       ResetSyntax(ts);
-       return (i);
-    }
-    i = (*ts->proc) (ts, ts->rock);
+       code = (*beforeProc) (ts, beforeRock);
+
+    if (code)
+       goto out;
+
+    code = (*ts->proc) (ts, ts->rock);
+
     if (afterProc)
        (*afterProc) (ts, afterRock);
-    ResetSyntax(ts);           /* Reset and free things */
-    return (i);
+out:
+    cmd_FreeOptions(&ts);
+    return code;
+}
+
+void
+cmd_FreeOptions(struct cmd_syndesc **ts)
+{
+    if (*ts != NULL) {
+       ResetSyntax(*ts);
+        *ts = NULL;
+    }
 }
 
 /* free token list returned by parseLine */
@@ -932,9 +1060,10 @@ cmd_FreeArgv(char **argv)
     return 0;
 }
 
-/* copy back the arg list to the argv array, freeing the cmd_tokens as you go; the actual
-    data is still malloc'd, and will be freed when the caller calls cmd_FreeArgv
-    later on */
+/* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
+ * the actual data is still malloc'd, and will be freed when the caller calls
+ * cmd_FreeArgv later on
+ */
 #define INITSTR ""
 static int
 CopyBackArgs(struct cmd_token *alist, char **argv,
@@ -998,8 +1127,8 @@ cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
     int tc;
 
     inToken = 0;               /* not copying token chars at start */
-    first = (struct cmd_token *)0;
-    last = (struct cmd_token *)0;
+    first = NULL;
+    last = NULL;
     inQuote = 0;               /* not in a quoted string */
     while (1) {
        tc = *aline++;
@@ -1010,10 +1139,10 @@ cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
                    return -1;  /* should never get here */
                else
                    *tptr++ = 0;
-               ttok = (struct cmd_token *)malloc(sizeof(struct cmd_token));
+               ttok = malloc(sizeof(struct cmd_token));
                assert(ttok);
-               ttok->next = (struct cmd_token *)0;
-               ttok->key = (char *)malloc(strlen(tbuffer) + 1);
+               ttok->next = NULL;
+               ttok->key = malloc(strlen(tbuffer) + 1);
                assert(ttok->key);
                strcpy(ttok->key, tbuffer);
                if (last) {
@@ -1045,8 +1174,75 @@ cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
        if (tc == 0) {
            /* last token flushed 'cause space(0) --> true */
            if (last)
-               last->next = (struct cmd_token *)0;
+               last->next = NULL;
            return CopyBackArgs(first, argv, an, amaxn);
        }
     }
 }
+
+int
+cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value)
+{
+    if (pos > syn->nParms)
+       return CMD_EXCESSPARMS;
+    if (syn->parms[pos].items == NULL ||
+       syn->parms[pos].items->data == NULL)
+       return CMD_MISSING;
+    if (syn->parms[pos].items == &dummy)
+       return 0;
+
+    *value = strtol(syn->parms[pos].items->data, NULL, 10);
+
+    return 0;
+}
+
+int
+cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
+{
+    if (pos > syn->nParms)
+       return CMD_EXCESSPARMS;
+    if (syn->parms[pos].items == NULL || syn->parms[pos].items->data == NULL)
+       return CMD_MISSING;
+    if (syn->parms[pos].items == &dummy)
+       return 0;
+
+    if (*value)
+       free(*value);
+
+    *value = strdup(syn->parms[pos].items->data);
+
+    return 0;
+}
+
+int
+cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
+{
+    if (pos > syn->nParms)
+       return CMD_EXCESSPARMS;
+    if (syn->parms[pos].items == NULL)
+       return CMD_MISSING;
+
+    *value = syn->parms[pos].items;
+    return 0;
+}
+
+int
+cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value)
+{
+    if (pos > syn->nParms)
+       return CMD_EXCESSPARMS;
+    if (syn->parms[pos].items == NULL)
+       return CMD_MISSING;
+
+    *value = 1;
+    return 0;
+}
+
+int
+cmd_OptionPresent(struct cmd_syndesc *syn, int pos)
+{
+    if (pos > syn->nParms || syn->parms[pos].items == NULL)
+       return 0;
+
+    return 1;
+}