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