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