btrfs-progs: misc-test: use raid1 for data to enable mount with -o degraded
[btrfs-progs-unstable/devel.git] / help.c
blob99097db84ec4a02c9f74edd821f41899909520b3
1 /*
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.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <getopt.h>
23 #include "commands.h"
24 #include "utils.h"
25 #include "help.h"
27 #define USAGE_SHORT 1U
28 #define USAGE_LONG 2U
29 #define USAGE_OPTIONS 4U
30 #define USAGE_LISTING 8U
32 static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
34 const char *get_argv0_buf(void)
36 return argv0_buf;
39 void fixup_argv0(char **argv, const char *token)
41 int len = strlen(argv0_buf);
43 snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
44 argv[0] = argv0_buf;
47 void set_argv0(char **argv)
49 strncpy(argv0_buf, argv[0], sizeof(argv0_buf));
50 argv0_buf[sizeof(argv0_buf) - 1] = 0;
53 int check_argc_exact(int nargs, int expected)
55 if (nargs < expected)
56 fprintf(stderr, "%s: too few arguments\n", argv0_buf);
57 if (nargs > expected)
58 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
60 return nargs != expected;
63 int check_argc_min(int nargs, int expected)
65 if (nargs < expected) {
66 fprintf(stderr, "%s: too few arguments\n", argv0_buf);
67 return 1;
70 return 0;
73 int check_argc_max(int nargs, int expected)
75 if (nargs > expected) {
76 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
77 return 1;
80 return 0;
84 * Preprocess @argv with getopt_long to reorder options and consume the "--"
85 * option separator.
86 * Unknown short and long options are reported, optionally the @usage is printed
87 * before exit.
89 void clean_args_no_options(int argc, char *argv[], const char * const *usagestr)
91 static const struct option long_options[] = {
92 {NULL, 0, NULL, 0}
95 while (1) {
96 int c = getopt_long(argc, argv, "", long_options, NULL);
98 if (c < 0)
99 break;
101 switch (c) {
102 default:
103 if (usagestr)
104 usage(usagestr);
110 * Same as clean_args_no_options but pass through arguments that could look
111 * like short options. Eg. reisze which takes a negative resize argument like
112 * '-123M' .
114 * This accepts only two forms:
115 * - "-- option1 option2 ..."
116 * - "option1 option2 ..."
118 void clean_args_no_options_relaxed(int argc, char *argv[], const char * const *usagestr)
120 if (argc <= 1)
121 return;
123 if (strcmp(argv[1], "--") == 0)
124 optind = 2;
127 static int do_usage_one_command(const char * const *usagestr,
128 unsigned int flags, FILE *outf)
130 int pad = 4;
132 if (!usagestr || !*usagestr)
133 return -1;
135 fprintf(outf, "%s%s", (flags & USAGE_LISTING) ? " " : "usage: ",
136 *usagestr++);
138 /* a short one-line description (mandatory) */
139 if ((flags & USAGE_SHORT) == 0)
140 return 0;
141 else if (!*usagestr)
142 return -2;
143 fputc('\n', outf);
145 if (flags & USAGE_LISTING)
146 pad = 8;
147 else
148 fputc('\n', outf);
150 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
152 /* a long (possibly multi-line) description (optional) */
153 if (!*usagestr || ((flags & USAGE_LONG) == 0))
154 return 0;
156 if (**usagestr)
157 fputc('\n', outf);
158 while (*usagestr && **usagestr)
159 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
161 /* options (optional) */
162 if (!*usagestr || ((flags & USAGE_OPTIONS) == 0))
163 return 0;
166 * options (if present) should always (even if there is no long
167 * description) be prepended with an empty line, skip it
169 usagestr++;
171 fputc('\n', outf);
172 while (*usagestr)
173 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
175 return 0;
178 static int usage_command_internal(const char * const *usagestr,
179 const char *token, int full, int lst,
180 int alias, FILE *outf)
182 unsigned int flags = 0;
183 int ret;
185 if (!alias)
186 flags |= USAGE_SHORT;
187 if (full)
188 flags |= USAGE_LONG | USAGE_OPTIONS;
189 if (lst)
190 flags |= USAGE_LISTING;
192 ret = do_usage_one_command(usagestr, flags, outf);
193 switch (ret) {
194 case -1:
195 fprintf(outf, "No usage for '%s'\n", token);
196 break;
197 case -2:
198 fprintf(outf, "No short description for '%s'\n", token);
199 break;
202 return ret;
205 static void usage_command_usagestr(const char * const *usagestr,
206 const char *token, int full, int err)
208 FILE *outf = err ? stderr : stdout;
209 int ret;
211 ret = usage_command_internal(usagestr, token, full, 0, 0, outf);
212 if (!ret)
213 fputc('\n', outf);
216 void usage_command(const struct cmd_struct *cmd, int full, int err)
218 usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
221 __attribute__((noreturn))
222 void usage(const char * const *usagestr)
224 usage_command_usagestr(usagestr, NULL, 1, 1);
225 exit(1);
228 static void usage_command_group_internal(const struct cmd_group *grp, int full,
229 FILE *outf)
231 const struct cmd_struct *cmd = grp->commands;
232 int do_sep = 0;
234 for (; cmd->token; cmd++) {
235 if (cmd->flags & CMD_HIDDEN)
236 continue;
238 if (full && cmd != grp->commands)
239 fputc('\n', outf);
241 if (!cmd->next) {
242 if (do_sep) {
243 fputc('\n', outf);
244 do_sep = 0;
247 usage_command_internal(cmd->usagestr, cmd->token, full,
248 1, cmd->flags & CMD_ALIAS, outf);
249 if (cmd->flags & CMD_ALIAS)
250 putchar('\n');
251 continue;
254 /* this is an entry point to a nested command group */
256 if (!full && cmd != grp->commands)
257 fputc('\n', outf);
259 usage_command_group_internal(cmd->next, full, outf);
261 if (!full)
262 do_sep = 1;
266 void usage_command_group_short(const struct cmd_group *grp)
268 const char * const *usagestr = grp->usagestr;
269 FILE *outf = stdout;
270 const struct cmd_struct *cmd;
272 if (usagestr && *usagestr) {
273 fprintf(outf, "usage: %s\n", *usagestr++);
274 while (*usagestr)
275 fprintf(outf, " or: %s\n", *usagestr++);
278 fputc('\n', outf);
280 fprintf(outf, "Command groups:\n");
281 for (cmd = grp->commands; cmd->token; cmd++) {
282 if (cmd->flags & CMD_HIDDEN)
283 continue;
285 if (!cmd->next)
286 continue;
288 fprintf(outf, " %-16s %s\n", cmd->token, cmd->next->infostr);
291 fprintf(outf, "\nCommands:\n");
292 for (cmd = grp->commands; cmd->token; cmd++) {
293 if (cmd->flags & CMD_HIDDEN)
294 continue;
296 if (cmd->next)
297 continue;
299 fprintf(outf, " %-16s %s\n", cmd->token, cmd->usagestr[1]);
302 fputc('\n', outf);
303 fprintf(stderr, "For an overview of a given command use 'btrfs command --help'\n");
304 fprintf(stderr, "or 'btrfs [command...] --help --full' to print all available options.\n");
305 fprintf(stderr, "Any command name can be shortened as far as it stays unambiguous,\n");
306 fprintf(stderr, "however it is recommended to use full command names in scripts.\n");
307 fprintf(stderr, "All command groups have their manual page named 'btrfs-<group>'.\n");
310 void usage_command_group(const struct cmd_group *grp, int full, int err)
312 const char * const *usagestr = grp->usagestr;
313 FILE *outf = err ? stderr : stdout;
315 if (usagestr && *usagestr) {
316 fprintf(outf, "usage: %s\n", *usagestr++);
317 while (*usagestr)
318 fprintf(outf, " or: %s\n", *usagestr++);
321 fputc('\n', outf);
322 usage_command_group_internal(grp, full, outf);
323 fputc('\n', outf);
325 if (grp->infostr)
326 fprintf(outf, "%s\n", grp->infostr);
329 __attribute__((noreturn))
330 void help_unknown_token(const char *arg, const struct cmd_group *grp)
332 fprintf(stderr, "%s: unknown token '%s'\n", get_argv0_buf(), arg);
333 usage_command_group(grp, 0, 1);
334 exit(1);
337 __attribute__((noreturn))
338 void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
340 const struct cmd_struct *cmd = grp->commands;
342 fprintf(stderr, "%s: ambiguous token '%s'\n", get_argv0_buf(), arg);
343 fprintf(stderr, "\nDid you mean one of these ?\n");
345 for (; cmd->token; cmd++) {
346 if (!prefixcmp(cmd->token, arg))
347 fprintf(stderr, "\t%s\n", cmd->token);
350 exit(1);
353 void help_command_group(const struct cmd_group *grp, int argc, char **argv)
355 int full = 0;
357 if (argc > 1) {
358 if (!strcmp(argv[1], "--full"))
359 full = 1;
362 usage_command_group(grp, full, 0);