cmd: Add parameter aliasing
[openafs.git] / src / cmd / cmd.c
index a85edd5..d8d2107 100644 (file)
@@ -28,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 */
 
@@ -73,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]) {
@@ -89,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) {
@@ -452,6 +464,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)
 {
@@ -469,14 +493,14 @@ 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 = malloc(strlen(aname) + 1);
     assert(tp->name);
@@ -490,6 +514,38 @@ cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
        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;
 }
 
@@ -647,47 +703,57 @@ NoParmsOK(struct cmd_syndesc *as)
     return 1;
 }
 
+/* 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, 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);
-       }
+       initSyntax();
     }
 
     /*Remember the program name */
@@ -697,17 +763,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;
            }
        }
     }
@@ -724,7 +792,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 */
@@ -739,7 +807,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
@@ -748,7 +817,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 */
@@ -765,7 +835,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;
            }
        }                       /*Argv[1] is not a valid opcode */
     }                          /*Opcodes are defined */
@@ -779,7 +850,7 @@ cmd_Dispatch(int argc, char **argv)
      * 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++) {
@@ -798,14 +869,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;
@@ -818,8 +889,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];
 
@@ -841,8 +912,8 @@ cmd_Dispatch(int argc, char **argv)
                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) {
@@ -865,8 +936,8 @@ cmd_Dispatch(int argc, char **argv)
        /* 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 */
@@ -877,19 +948,35 @@ cmd_Dispatch(int argc, char **argv)
        if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
            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
@@ -897,25 +984,33 @@ cmd_Dispatch(int argc, char **argv)
      * 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 */
@@ -1060,3 +1155,64 @@ cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 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;
+    *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 (*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;
+}