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.
25 static char argv0_buf
[ARGV0_BUF_SIZE
];
27 #define USAGE_SHORT 1U
29 #define USAGE_OPTIONS 4U
30 #define USAGE_LISTING 8U
32 static int do_usage_one_command(const char * const *usagestr
,
33 unsigned int flags
, FILE *outf
)
37 if (!usagestr
|| !*usagestr
)
40 fprintf(outf
, "%s%s\n", (flags
& USAGE_LISTING
) ? " " : "usage: ",
43 /* a short one-line description (mandatory) */
44 if ((flags
& USAGE_SHORT
) == 0)
49 if (flags
& USAGE_LISTING
)
54 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
56 /* a long (possibly multi-line) description (optional) */
57 if (!*usagestr
|| ((flags
& USAGE_LONG
) == 0))
62 while (*usagestr
&& **usagestr
)
63 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
65 /* options (optional) */
66 if (!*usagestr
|| ((flags
& USAGE_OPTIONS
) == 0))
70 * options (if present) should always (even if there is no long
71 * description) be prepended with an empty line, skip it
77 fprintf(outf
, "%*s%s\n", pad
, "", *usagestr
++);
82 static int usage_command_internal(const char * const *usagestr
,
83 const char *token
, int full
, int lst
,
86 unsigned int flags
= USAGE_SHORT
;
90 flags
|= USAGE_LONG
| USAGE_OPTIONS
;
92 flags
|= USAGE_LISTING
;
94 ret
= do_usage_one_command(usagestr
, flags
, outf
);
97 fprintf(outf
, "No usage for '%s'\n", token
);
100 fprintf(outf
, "No short description for '%s'\n", token
);
107 static void usage_command_usagestr(const char * const *usagestr
,
108 const char *token
, int full
, int err
)
110 FILE *outf
= err
? stderr
: stdout
;
113 ret
= usage_command_internal(usagestr
, token
, full
, 0, outf
);
118 void usage_command(const struct cmd_struct
*cmd
, int full
, int err
)
120 usage_command_usagestr(cmd
->usagestr
, cmd
->token
, full
, err
);
123 void usage(const char * const *usagestr
)
125 usage_command_usagestr(usagestr
, NULL
, 1, 1);
129 static void usage_command_group_internal(const struct cmd_group
*grp
, int full
,
132 const struct cmd_struct
*cmd
= grp
->commands
;
135 for (; cmd
->token
; cmd
++) {
139 if (full
&& cmd
!= grp
->commands
)
148 usage_command_internal(cmd
->usagestr
, cmd
->token
, full
,
153 /* this is an entry point to a nested command group */
155 if (!full
&& cmd
!= grp
->commands
)
158 usage_command_group_internal(cmd
->next
, full
, outf
);
165 void usage_command_group(const struct cmd_group
*grp
, int full
, int err
)
167 const char * const *usagestr
= grp
->usagestr
;
168 FILE *outf
= err
? stderr
: stdout
;
170 if (usagestr
&& *usagestr
) {
171 fprintf(outf
, "usage: %s\n", *usagestr
++);
173 fprintf(outf
, " or: %s\n", *usagestr
++);
177 usage_command_group_internal(grp
, full
, outf
);
181 fprintf(outf
, "%s\n", grp
->infostr
);
184 void help_unknown_token(const char *arg
, const struct cmd_group
*grp
)
186 fprintf(stderr
, "%s: unknown token '%s'\n", argv0_buf
, arg
);
187 usage_command_group(grp
, 0, 1);
191 void help_ambiguous_token(const char *arg
, const struct cmd_group
*grp
)
193 const struct cmd_struct
*cmd
= grp
->commands
;
195 fprintf(stderr
, "%s: ambiguous token '%s'\n", argv0_buf
, arg
);
196 fprintf(stderr
, "\nDid you mean one of these ?\n");
198 for (; cmd
->token
; cmd
++) {
199 if (!prefixcmp(cmd
->token
, arg
))
200 fprintf(stderr
, "\t%s\n", cmd
->token
);
206 void help_command_group(const struct cmd_group
*grp
, int argc
, char **argv
)
211 if (!strcmp(argv
[1], "--full"))
215 usage_command_group(grp
, full
, 0);