2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
24 static char argv0_buf[ARGV0_BUF_SIZE];
26 #define USAGE_SHORT 1U
28 #define USAGE_OPTIONS 4U
29 #define USAGE_LISTING 8U
31 static int do_usage_one_command(const char * const *usagestr,
32 unsigned int flags, FILE *outf)
36 if (!usagestr || !*usagestr)
39 fprintf(outf, "%s%s\n", (flags & USAGE_LISTING) ? " " : "usage: ",
42 /* a short one-line description (mandatory) */
43 if ((flags & USAGE_SHORT) == 0)
48 if (flags & USAGE_LISTING)
53 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
55 /* a long (possibly multi-line) description (optional) */
56 if (!*usagestr || ((flags & USAGE_LONG) == 0))
61 while (*usagestr && **usagestr)
62 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
64 /* options (optional) */
65 if (!*usagestr || ((flags & USAGE_OPTIONS) == 0))
69 * options (if present) should always (even if there is no long
70 * description) be prepended with an empty line, skip it
76 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
81 static int usage_command_internal(const char * const *usagestr,
82 const char *token, int full, int lst,
85 unsigned int flags = USAGE_SHORT;
89 flags |= USAGE_LONG | USAGE_OPTIONS;
91 flags |= USAGE_LISTING;
93 ret = do_usage_one_command(usagestr, flags, outf);
96 fprintf(outf, "No usage for '%s'\n", token);
99 fprintf(outf, "No short description for '%s'\n", token);
106 static void usage_command_usagestr(const char * const *usagestr,
107 const char *token, int full, int err)
109 FILE *outf = err ? stderr : stdout;
112 ret = usage_command_internal(usagestr, token, full, 0, outf);
117 void usage_command(const struct cmd_struct *cmd, int full, int err)
119 usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
122 void usage(const char * const *usagestr)
124 usage_command_usagestr(usagestr, NULL, 1, 1);
128 static void usage_command_group_internal(const struct cmd_group *grp, int full,
131 const struct cmd_struct *cmd = grp->commands;
134 for (; cmd->token; cmd++) {
138 if (full && cmd != grp->commands)
147 usage_command_internal(cmd->usagestr, cmd->token, full,
152 /* this is an entry point to a nested command group */
154 if (!full && cmd != grp->commands)
157 usage_command_group_internal(cmd->next, full, outf);
164 void usage_command_group(const struct cmd_group *grp, int full, int err)
166 const char * const *usagestr = grp->usagestr;
167 FILE *outf = err ? stderr : stdout;
169 if (usagestr && *usagestr) {
170 fprintf(outf, "usage: %s\n", *usagestr++);
172 fprintf(outf, " or: %s\n", *usagestr++);
176 usage_command_group_internal(grp, full, outf);
180 fprintf(outf, "%s\n", grp->infostr);
183 void help_unknown_token(const char *arg, const struct cmd_group *grp)
185 fprintf(stderr, "%s: unknown token '%s'\n", argv0_buf, arg);
186 usage_command_group(grp, 0, 1);
190 void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
192 const struct cmd_struct *cmd = grp->commands;
194 fprintf(stderr, "%s: ambiguous token '%s'\n", argv0_buf, arg);
195 fprintf(stderr, "\nDid you mean one of these ?\n");
197 for (; cmd->token; cmd++) {
198 if (!prefixcmp(cmd->token, arg))
199 fprintf(stderr, "\t%s\n", cmd->token);
205 void help_command_group(const struct cmd_group *grp, int argc, char **argv)
210 if (!strcmp(argv[1], "--full"))
214 usage_command_group(grp, full, 0);