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;
}
}
/* 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)
/* 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;
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) {
/* 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);
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 */
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;
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) {
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;
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");
- 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,
+ cmd_CreateSyntax("version", VersionProc, NULL, CMD_IMPLICIT,
"show version");
- 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_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);
}
}
/* 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);