Merge branch 'qu/find-root-v3-part1' into v3.19.x
[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 static char argv0_buf[ARGV0_BUF_SIZE];
26
27 #define USAGE_SHORT             1U
28 #define USAGE_LONG              2U
29 #define USAGE_OPTIONS           4U
30 #define USAGE_LISTING           8U
31
32 static int do_usage_one_command(const char * const *usagestr,
33                                 unsigned int flags, FILE *outf)
34 {
35         int pad = 4;
36
37         if (!usagestr || !*usagestr)
38                 return -1;
39
40         fprintf(outf, "%s%s\n", (flags & USAGE_LISTING) ? "    " : "usage: ",
41                 *usagestr++);
42
43         /* a short one-line description (mandatory) */
44         if ((flags & USAGE_SHORT) == 0)
45                 return 0;
46         else if (!*usagestr)
47                 return -2;
48
49         if (flags & USAGE_LISTING)
50                 pad = 8;
51         else
52                 fputc('\n', outf);
53
54         fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
55
56         /* a long (possibly multi-line) description (optional) */
57         if (!*usagestr || ((flags & USAGE_LONG) == 0))
58                 return 0;
59
60         if (**usagestr)
61                 fputc('\n', outf);
62         while (*usagestr && **usagestr)
63                 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
64
65         /* options (optional) */
66         if (!*usagestr || ((flags & USAGE_OPTIONS) == 0))
67                 return 0;
68
69         /*
70          * options (if present) should always (even if there is no long
71          * description) be prepended with an empty line, skip it
72          */
73         usagestr++;
74
75         fputc('\n', outf);
76         while (*usagestr)
77                 fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
78
79         return 0;
80 }
81
82 static int usage_command_internal(const char * const *usagestr,
83                                   const char *token, int full, int lst,
84                                   FILE *outf)
85 {
86         unsigned int flags = USAGE_SHORT;
87         int ret;
88
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, 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->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, 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(const struct cmd_group *grp, int full, int err)
166 {
167         const char * const *usagestr = grp->usagestr;
168         FILE *outf = err ? stderr : stdout;
169
170         if (usagestr && *usagestr) {
171                 fprintf(outf, "usage: %s\n", *usagestr++);
172                 while (*usagestr)
173                         fprintf(outf, "   or: %s\n", *usagestr++);
174         }
175
176         fputc('\n', outf);
177         usage_command_group_internal(grp, full, outf);
178         fputc('\n', outf);
179
180         if (grp->infostr)
181                 fprintf(outf, "%s\n", grp->infostr);
182 }
183
184 void help_unknown_token(const char *arg, const struct cmd_group *grp)
185 {
186         fprintf(stderr, "%s: unknown token '%s'\n", argv0_buf, arg);
187         usage_command_group(grp, 0, 1);
188         exit(1);
189 }
190
191 void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
192 {
193         const struct cmd_struct *cmd = grp->commands;
194
195         fprintf(stderr, "%s: ambiguous token '%s'\n", argv0_buf, arg);
196         fprintf(stderr, "\nDid you mean one of these ?\n");
197
198         for (; cmd->token; cmd++) {
199                 if (!prefixcmp(cmd->token, arg))
200                         fprintf(stderr, "\t%s\n", cmd->token);
201         }
202
203         exit(1);
204 }
205
206 void help_command_group(const struct cmd_group *grp, int argc, char **argv)
207 {
208         int full = 0;
209
210         if (argc > 1) {
211                 if (!strcmp(argv[1], "--full"))
212                         full = 1;
213         }
214
215         usage_command_group(grp, full, 0);
216 }