cmd: avoid piggy-backing flags in the help string
[openafs.git] / src / cmd / cmd.c
index 6887454..f97e558 100644 (file)
@@ -433,10 +433,24 @@ SortSyntax(struct cmd_syndesc *as)
     return 0;
 }
 
+/*!
+ * Create a command syntax.
+ *
+ * \note Use cmd_AddParm() or cmd_AddParmAtOffset() to set the
+ *       parameters for the new command.
+ *
+ * \param[in] aname  name used to invoke the command
+ * \param[in] aproc  procedure to be called when command is invoked
+ * \param[in] arock  opaque data pointer to be passed to aproc
+ * \param[in] aflags command option flags (CMD_HIDDEN)
+ * \param[in] ahelp  help string to display for this command
+ *
+ * \return a pointer to the cmd_syndesc or NULL if error.
+ */
 struct cmd_syndesc *
 cmd_CreateSyntax(char *aname,
                 int (*aproc) (struct cmd_syndesc * ts, void *arock),
-                void *arock, char *ahelp)
+                void *arock, afs_uint32 aflags, char *ahelp)
 {
     struct cmd_syndesc *td;
 
@@ -444,9 +458,15 @@ cmd_CreateSyntax(char *aname,
     if (noOpcodes)
        return NULL;
 
+    /* Allow only valid cmd flags. */
+    if (aflags & ~CMD_HIDDEN) {
+       return NULL;
+    }
+
     td = calloc(1, sizeof(struct cmd_syndesc));
     assert(td);
     td->aliasOf = td;          /* treat aliasOf as pointer to real command, no matter what */
+    td->flags = aflags;
 
     /* copy in name, etc */
     if (aname) {
@@ -457,13 +477,8 @@ cmd_CreateSyntax(char *aname,
        noOpcodes = 1;
     }
     if (ahelp) {
-       /* Piggy-back the hidden option onto the help string */
-       if (ahelp == (char *)CMD_HIDDEN) {
-           td->flags |= CMD_HIDDEN;
-       } else {
-           td->help = strdup(ahelp);
-           assert(td->help);
-       }
+       td->help = strdup(ahelp);
+       assert(td->help);
     } else
        td->help = NULL;
     td->proc = aproc;
@@ -515,13 +530,6 @@ cmd_DisableAbbreviations(void)
 }
 
 int
-cmd_IsAdministratorCommand(struct cmd_syndesc *as)
-{
-    as->flags |= CMD_ADMIN;
-    return 0;
-}
-
-int
 cmd_Seek(struct cmd_syndesc *as, int apos)
 {
     if (apos >= CMD_MAXPARMS)
@@ -758,25 +766,20 @@ initSyntax(void)
     struct cmd_syndesc *ts;
 
     if (!noOpcodes) {
-       ts = cmd_CreateSyntax("help", HelpProc, NULL,
+       ts = cmd_CreateSyntax("help", HelpProc, NULL, 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, NULL,
+       ts = cmd_CreateSyntax("apropos", AproposProc, NULL, 0,
                              "search by help text");
        cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
 
-       cmd_CreateSyntax("version", VersionProc, NULL,
-                        (char *)CMD_HIDDEN);
-       cmd_CreateSyntax("-version", VersionProc, NULL,
-                        (char *)CMD_HIDDEN);
-       cmd_CreateSyntax("-help", HelpProc, NULL,
-                        (char *)CMD_HIDDEN);
-       cmd_CreateSyntax("--version", VersionProc, NULL,
-                        (char *)CMD_HIDDEN);
-       cmd_CreateSyntax("--help", HelpProc, NULL,
-                        (char *)CMD_HIDDEN);
+       cmd_CreateSyntax("version", VersionProc, NULL, 0,
+                        "show version");
+       cmd_CreateSyntax("-version", VersionProc, NULL, CMD_HIDDEN, NULL);
+       cmd_CreateSyntax("-help", HelpProc, NULL, CMD_HIDDEN, NULL);
+       cmd_CreateSyntax("--version", VersionProc, NULL, CMD_HIDDEN, NULL);
+       cmd_CreateSyntax("--help", HelpProc, NULL, CMD_HIDDEN, NULL);
     }
 }
 
@@ -1382,6 +1385,7 @@ cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
     const char *str;
     struct cmd_item *item, **last;
     const char *start, *end;
+    size_t len;
     int code;
 
     if (pos > syn->nParms)
@@ -1406,8 +1410,9 @@ cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
     start = str;
     while ((end = strchr(start, ' '))) {
        item = calloc(1, sizeof(struct cmd_item));
-       item->data = malloc(end - start + 1);
-       strlcpy(item->data, start, end - start + 1);
+       len = end - start + 1;
+       item->data = malloc(len);
+       strlcpy(item->data, start, len);
        *last = item;
        last = &item->next;
        for (start = end; *start == ' '; start++); /* skip any whitespace */
@@ -1416,8 +1421,9 @@ cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
     /* Catch the final element */
     if (*start != '\0') {
        item = calloc(1, sizeof(struct cmd_item));
-       item->data = malloc(strlen(start) + 1);
-       strlcpy(item->data, start, strlen(start) + 1);
+       len = strlen(start) + 1;
+       item->data = malloc(len);
+       strlcpy(item->data, start, len);
        *last = item;
     }