btrfs-progs: kerncompat: disconnect assert and warning messages
[platform/upstream/btrfs-progs.git] / help.c
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.
5  *
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.
10  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <limits.h>
21
22 #include "commands.h"
23 #include "utils.h"
24
25 #define USAGE_SHORT             1U
26 #define USAGE_LONG              2U
27 #define USAGE_OPTIONS           4U
28 #define USAGE_LISTING           8U
29
30 static int do_usage_one_command(const char * const *usagestr,
31                                 unsigned int flags, FILE *outf)
32 {
33         int pad = 4;
34
35         if (!usagestr || !*usagestr)
36                 return -1;
37
38         fprintf(outf, "%s%s", (flags & USAGE_LISTING) ? "    " : "usage: ",
39                 *usagestr++);
40
41         /* a short one-line description (mandatory) */
42         if ((flags & USAGE_SHORT) == 0)
43                 return 0;
44         else if (!*usagestr)
45                 return -2;
46         fputc('\n', outf);
47
48         if (flags & USAGE_LISTING)
49                 pad = 8;
50         else
51                 fputc('\n', outf);
52
53         fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
54
55         /* a long (possibly multi-line) description (optional) */
56         if (!*usagestr || ((flags & USAGE_LONG) == 0))
57                 return 0;
58
59         if (**usagestr)
60                 fputc('\n', outf);
61         while (*usagestr && **usagestr)
62                 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
63
64         /* options (optional) */
65         if (!*usagestr || ((flags & USAGE_OPTIONS) == 0))
66                 return 0;
67
68         /*
69          * options (if present) should always (even if there is no long
70          * description) be prepended with an empty line, skip it
71          */
72         usagestr++;
73
74         fputc('\n', outf);
75         while (*usagestr)
76                 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
77
78         return 0;
79 }
80
81 static int usage_command_internal(const char * const *usagestr,
82                                   const char *token, int full, int lst,
83                                   int alias, FILE *outf)
84 {
85         unsigned int flags = 0;
86         int ret;
87
88         if (!alias)
89                 flags |= USAGE_SHORT;
90         if (full)
91                 flags |= USAGE_LONG | USAGE_OPTIONS;
92         if (lst)
93                 flags |= USAGE_LISTING;
94
95         ret = do_usage_one_command(usagestr, flags, outf);
96         switch (ret) {
97         case -1:
98                 fprintf(outf, "No usage for '%s'\n", token);
99                 break;
100         case -2:
101                 fprintf(outf, "No short description for '%s'\n", token);
102                 break;
103         }
104
105         return ret;
106 }
107
108 static void usage_command_usagestr(const char * const *usagestr,
109                                    const char *token, int full, int err)
110 {
111         FILE *outf = err ? stderr : stdout;
112         int ret;
113
114         ret = usage_command_internal(usagestr, token, full, 0, 0, outf);
115         if (!ret)
116                 fputc('\n', outf);
117 }
118
119 void usage_command(const struct cmd_struct *cmd, int full, int err)
120 {
121         usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
122 }
123
124 void usage(const char * const *usagestr)
125 {
126         usage_command_usagestr(usagestr, NULL, 1, 1);
127         exit(1);
128 }
129
130 static void usage_command_group_internal(const struct cmd_group *grp, int full,
131                                          FILE *outf)
132 {
133         const struct cmd_struct *cmd = grp->commands;
134         int do_sep = 0;
135
136         for (; cmd->token; cmd++) {
137                 if (cmd->flags & CMD_HIDDEN)
138                         continue;
139
140                 if (full && cmd != grp->commands)
141                         fputc('\n', outf);
142
143                 if (!cmd->next) {
144                         if (do_sep) {
145                                 fputc('\n', outf);
146                                 do_sep = 0;
147                         }
148
149                         usage_command_internal(cmd->usagestr, cmd->token, full,
150                                                1, cmd->flags & CMD_ALIAS, outf);
151                         if (cmd->flags & CMD_ALIAS)
152                                 putchar('\n');
153                         continue;
154                 }
155
156                 /* this is an entry point to a nested command group */
157
158                 if (!full && cmd != grp->commands)
159                         fputc('\n', outf);
160
161                 usage_command_group_internal(cmd->next, full, outf);
162
163                 if (!full)
164                         do_sep = 1;
165         }
166 }
167
168 void usage_command_group_short(const struct cmd_group *grp)
169 {
170         const char * const *usagestr = grp->usagestr;
171         FILE *outf = stdout;
172         const struct cmd_struct *cmd;
173
174         if (usagestr && *usagestr) {
175                 fprintf(outf, "usage: %s\n", *usagestr++);
176                 while (*usagestr)
177                         fprintf(outf, "   or: %s\n", *usagestr++);
178         }
179
180         fputc('\n', outf);
181
182         fprintf(outf, "Command groups:\n");
183         for (cmd = grp->commands; cmd->token; cmd++) {
184                 if (cmd->flags & CMD_HIDDEN)
185                         continue;
186
187                 if (!cmd->next)
188                         continue;
189
190                 fprintf(outf, "  %-16s  %s\n", cmd->token, cmd->next->infostr);
191         }
192
193         fprintf(outf, "\nCommands:\n");
194         for (cmd = grp->commands; cmd->token; cmd++) {
195                 if (cmd->flags & CMD_HIDDEN)
196                         continue;
197
198                 if (cmd->next)
199                         continue;
200
201                 fprintf(outf, "  %-16s  %s\n", cmd->token, cmd->usagestr[1]);
202         }
203
204         fputc('\n', outf);
205         fprintf(stderr, "For an overview of a given command use 'btrfs command --help'\n");
206         fprintf(stderr, "or 'btrfs [command...] --help --full' to print all available options.\n");
207         fprintf(stderr, "Any command name can be shortened as far as it stays unambiguous,\n");
208         fprintf(stderr, "however it is recommended to use full command names in scripts.\n");
209         fprintf(stderr, "All command groups have their manual page named 'btrfs-<group>'.\n");
210 }
211
212 void usage_command_group(const struct cmd_group *grp, int full, int err)
213 {
214         const char * const *usagestr = grp->usagestr;
215         FILE *outf = err ? stderr : stdout;
216
217         if (usagestr && *usagestr) {
218                 fprintf(outf, "usage: %s\n", *usagestr++);
219                 while (*usagestr)
220                         fprintf(outf, "   or: %s\n", *usagestr++);
221         }
222
223         fputc('\n', outf);
224         usage_command_group_internal(grp, full, outf);
225         fputc('\n', outf);
226
227         if (grp->infostr)
228                 fprintf(outf, "%s\n", grp->infostr);
229 }
230
231 void help_unknown_token(const char *arg, const struct cmd_group *grp)
232 {
233         fprintf(stderr, "%s: unknown token '%s'\n", get_argv0_buf(), arg);
234         usage_command_group(grp, 0, 1);
235         exit(1);
236 }
237
238 void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
239 {
240         const struct cmd_struct *cmd = grp->commands;
241
242         fprintf(stderr, "%s: ambiguous token '%s'\n", get_argv0_buf(), arg);
243         fprintf(stderr, "\nDid you mean one of these ?\n");
244
245         for (; cmd->token; cmd++) {
246                 if (!prefixcmp(cmd->token, arg))
247                         fprintf(stderr, "\t%s\n", cmd->token);
248         }
249
250         exit(1);
251 }
252
253 void help_command_group(const struct cmd_group *grp, int argc, char **argv)
254 {
255         int full = 0;
256
257         if (argc > 1) {
258                 if (!strcmp(argv[1], "--full"))
259                         full = 1;
260         }
261
262         usage_command_group(grp, full, 0);
263 }
264
265 int prefixcmp(const char *str, const char *prefix)
266 {
267         for (; ; str++, prefix++)
268                 if (!*prefix)
269                         return 0;
270                 else if (*str != *prefix)
271                         return (unsigned char)*prefix - (unsigned char)*str;
272 }
273