X-Git-Url: http://git.openafs.org/?p=openafs.git;a=blobdiff_plain;f=src%2Fcmd%2Fcmd.c;h=10b2b1bae93a8625c00b7fecddcfb0b8f2495cc7;hp=e2e8e3a089f4f0558aa0dfbfb2c68b5bc6d0367a;hb=297c479989efb6bd9d4011a43d6c0dc92596761b;hpb=d527a8082507bf091f89c7964ce152dfce5d4052 diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index e2e8e3a..10b2b1b 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -176,7 +176,7 @@ ParmHelpString(struct cmd_parmdesc *aparm) aparm->help?aparm->help:"arg", aparm->type == CMD_LIST?"+":"", aparm->type == CMD_SINGLE_OR_FLAG?"]":"") < 0) - return "<< OUT OF MEMORY >>"; + return NULL; return str; } } @@ -220,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; @@ -228,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) { @@ -239,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); @@ -357,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 */ @@ -439,7 +476,7 @@ SortSyntax(struct cmd_syndesc *as) * \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] 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. @@ -456,7 +493,7 @@ cmd_CreateSyntax(char *aname, return NULL; /* Allow only valid cmd flags. */ - if (aflags & ~CMD_HIDDEN) { + if (aflags & ~(CMD_HIDDEN | CMD_IMPLICIT)) { return NULL; } @@ -763,20 +800,20 @@ initSyntax(void) struct cmd_syndesc *ts; if (!noOpcodes) { - ts = cmd_CreateSyntax("help", HelpProc, NULL, 0, + ts = cmd_CreateSyntax("help", HelpProc, NULL, CMD_IMPLICIT, "get help on commands"); cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string"); - ts = cmd_CreateSyntax("apropos", AproposProc, NULL, 0, + 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, 0, + cmd_CreateSyntax("version", VersionProc, NULL, CMD_IMPLICIT, "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); + 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); } }