X-Git-Url: http://git.openafs.org/?p=openafs.git;a=blobdiff_plain;f=src%2Fcmd%2Fcmd.c;h=10b2b1bae93a8625c00b7fecddcfb0b8f2495cc7;hp=903dc94dc2b087078d3cbd20363a50dd49bece74;hb=297c479989efb6bd9d4011a43d6c0dc92596761b;hpb=bd1248ca3988edb230174ff34c3ff79bedcf559e diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index 903dc94..10b2b1b 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -171,11 +171,12 @@ ParmHelpString(struct cmd_parmdesc *aparm) if (aparm->type == CMD_FLAG) { 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?"]":""); + if (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?"]":"") < 0) + return NULL; return str; } } @@ -201,18 +202,14 @@ PrintSyntax(struct cmd_syndesc *as) /* now print usage, from syntax table */ if (noOpcodes) - asprintf(&str, "Usage: %s", as->a0name); + len = printf("Usage: %s", as->a0name); else { if (!strcmp(as->name, initcmd_opcode)) - asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name); + len = printf("Usage: %s[%s]", NName(as->a0name, " "), as->name); else - asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name); + len = printf("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) @@ -223,6 +220,10 @@ PrintSyntax(struct cmd_syndesc *as) /* The parameter name is the real name, plus any aliases */ if (!tp->aliases) { name = strdup(tp->name); + if (!name) { + fprintf(stderr, "Out of memory.\n"); + return; + } } else { size_t namelen; struct cmd_item *alias; @@ -231,6 +232,10 @@ PrintSyntax(struct cmd_syndesc *as) namelen+=strlen(alias->data) + 3; name = malloc(namelen); + if (!name) { + fprintf(stderr, "Out of memory.\n"); + return; + } strlcpy(name, tp->name, namelen); for (alias = tp->aliases; alias != NULL; alias = alias->next) { @@ -242,6 +247,11 @@ PrintSyntax(struct cmd_syndesc *as) /* 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); + if (!str) { + fprintf(stderr, "Out of memory.\n"); + free(name); + return; + } xtralen = 1 + strlen(name) + strlen(str) + ((tp->flags & CMD_OPTIONAL)? 2: 0); @@ -360,11 +370,35 @@ HelpProc(struct cmd_syndesc *as, void *arock) int code = 0; if (as->parms[0].items == 0) { - printf("%sCommands are:\n", NName(as->a0name, ": ")); + struct cmd_syndesc *initcmd = NULL; + int count = 0; + + /* + * Print the usage of the initcmd command when it is the only + * non-hidden, explicit command, otherwise, list the all the commands. + */ for (ts = allSyntax; ts; ts = ts->next) { - if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN)) + if (ts->flags & (CMD_ALIAS | CMD_HIDDEN | CMD_IMPLICIT)) { + continue; /* skip aliases, hidden, and implied commands */ + } + if (strcmp(ts->name, initcmd_opcode) == 0) { + initcmd = ts; /* save the initcmd */ continue; - printf("%-15s %s\n", ts->name, (ts->help ? ts->help : "")); + } + count++; + } + if (initcmd && count == 0) { + initcmd->a0name = as->a0name; + PrintAliases(initcmd); + PrintSyntax(initcmd); + PrintFlagHelp(initcmd); + } else { + printf("%sCommands are:\n", NName(as->a0name, ": ")); + for (ts = allSyntax; ts; ts = ts->next) { + if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN)) + continue; + printf("%-15s %s\n", ts->name, (ts->help ? ts->help : "")); + } } } else { /* print out individual help topics */ @@ -433,10 +467,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 + * \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 +492,15 @@ cmd_CreateSyntax(char *aname, if (noOpcodes) return NULL; + /* Allow only valid cmd flags. */ + if (aflags & ~(CMD_HIDDEN | CMD_IMPLICIT)) { + 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 +511,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 +564,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 +800,20 @@ initSyntax(void) struct cmd_syndesc *ts; if (!noOpcodes) { - ts = cmd_CreateSyntax("help", HelpProc, NULL, + ts = cmd_CreateSyntax("help", HelpProc, NULL, CMD_IMPLICIT, "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, CMD_IMPLICIT, "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, CMD_IMPLICIT, + "show version"); + cmd_CreateSyntax("-version", VersionProc, NULL, (CMD_HIDDEN | CMD_IMPLICIT), NULL); + cmd_CreateSyntax("-help", HelpProc, NULL, (CMD_HIDDEN | CMD_IMPLICIT), NULL); + cmd_CreateSyntax("--version", VersionProc, NULL, (CMD_HIDDEN | CMD_IMPLICIT), NULL); + cmd_CreateSyntax("--help", HelpProc, NULL, (CMD_HIDDEN | CMD_IMPLICIT), NULL); } } @@ -1018,7 +1055,7 @@ cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax) /* Display full help syntax if we don't have subcommands */ if (noOpcodes) PrintFlagHelp(ts); - code = CMD_USAGE; + code = CMD_HELP; goto out; } @@ -1061,8 +1098,12 @@ cmd_Dispatch(int argc, char **argv) int code; code = cmd_Parse(argc, argv, &ts); - if (code) + if (code) { + if (code == CMD_HELP) { + code = 0; /* displaying help is not an error */ + } return code; + } /* * Before calling the beforeProc and afterProc and all the implications @@ -1265,7 +1306,8 @@ _get_file_string(struct cmd_syndesc *syn, int pos, const char **str) /* First, try the command_subcommand form */ if (syn->name != NULL && commandName != NULL) { - asprintf(§ion, "%s_%s", commandName, syn->name); + if (asprintf(§ion, "%s_%s", commandName, syn->name) < 0) + return ENOMEM; *str = cmd_RawConfigGetString(globalConfig, NULL, section, optionName, NULL); free(section); @@ -1358,9 +1400,6 @@ cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value) const char *str; int code; - if (*value) - free(*value); - code = _get_config_string(syn, pos, &str); if (code) return code; @@ -1368,6 +1407,8 @@ cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value) if (str == NULL) return CMD_MISSING; + if (*value) + free(*value); *value = strdup(str); return 0; @@ -1379,6 +1420,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) @@ -1403,8 +1445,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 */ @@ -1413,8 +1456,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; }