btrfs-progs: remove unused flags for btrfs_path
[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
21 #include "commands.h"
22 #include "utils.h"
23
24 static char argv0_buf[ARGV0_BUF_SIZE];
25
26 #define USAGE_SHORT             1U
27 #define USAGE_LONG              2U
28 #define USAGE_OPTIONS           4U
29 #define USAGE_LISTING           8U
30
31 static int do_usage_one_command(const char * const *usagestr,
32                                 unsigned int flags, FILE *outf)
33 {
34         int pad = 4;
35
36         if (!usagestr || !*usagestr)
37                 return -1;
38
39         fprintf(outf, "%s%s\n", (flags & USAGE_LISTING) ? "    " : "usage: ",
40                 *usagestr++);
41
42         /* a short one-line description (mandatory) */
43         if ((flags & USAGE_SHORT) == 0)
44                 return 0;
45         else if (!*usagestr)
46                 return -2;
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                                   FILE *outf)
84 {
85         unsigned int flags = USAGE_SHORT;
86         int ret;
87
88         if (full)
89                 flags |= USAGE_LONG | USAGE_OPTIONS;
90         if (lst)
91                 flags |= USAGE_LISTING;
92
93         ret = do_usage_one_command(usagestr, flags, outf);
94         switch (ret) {
95         case -1:
96                 fprintf(outf, "No usage for '%s'\n", token);
97                 break;
98         case -2:
99                 fprintf(outf, "No short description for '%s'\n", token);
100                 break;
101         }
102
103         return ret;
104 }
105
106 static void usage_command_usagestr(const char * const *usagestr,
107                                    const char *token, int full, int err)
108 {
109         FILE *outf = err ? stderr : stdout;
110         int ret;
111
112         ret = usage_command_internal(usagestr, token, full, 0, outf);
113         if (!ret)
114                 fputc('\n', outf);
115 }
116
117 void usage_command(const struct cmd_struct *cmd, int full, int err)
118 {
119         usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
120 }
121
122 void usage(const char * const *usagestr)
123 {
124         usage_command_usagestr(usagestr, NULL, 1, 1);
125         exit(1);
126 }
127
128 static void usage_command_group_internal(const struct cmd_group *grp, int full,
129                                          FILE *outf)
130 {
131         const struct cmd_struct *cmd = grp->commands;
132         int do_sep = 0;
133
134         for (; cmd->token; cmd++) {
135                 if (cmd->hidden)
136                         continue;
137
138                 if (full && cmd != grp->commands)
139                         fputc('\n', outf);
140
141                 if (!cmd->next) {
142                         if (do_sep) {
143                                 fputc('\n', outf);
144                                 do_sep = 0;
145                         }
146
147                         usage_command_internal(cmd->usagestr, cmd->token, full,
148                                                1, outf);
149                         continue;
150                 }
151
152                 /* this is an entry point to a nested command group */
153
154                 if (!full && cmd != grp->commands)
155                         fputc('\n', outf);
156
157                 usage_command_group_internal(cmd->next, full, outf);
158
159                 if (!full)
160                         do_sep = 1;
161         }
162 }
163
164 void usage_command_group(const struct cmd_group *grp, int full, int err)
165 {
166         const char * const *usagestr = grp->usagestr;
167         FILE *outf = err ? stderr : stdout;
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         usage_command_group_internal(grp, full, outf);
177         fputc('\n', outf);
178
179         if (grp->infostr)
180                 fprintf(outf, "%s\n", grp->infostr);
181 }
182
183 void help_unknown_token(const char *arg, const struct cmd_group *grp)
184 {
185         fprintf(stderr, "%s: unknown token '%s'\n", argv0_buf, arg);
186         usage_command_group(grp, 0, 1);
187         exit(1);
188 }
189
190 void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
191 {
192         const struct cmd_struct *cmd = grp->commands;
193
194         fprintf(stderr, "%s: ambiguous token '%s'\n", argv0_buf, arg);
195         fprintf(stderr, "\nDid you mean one of these ?\n");
196
197         for (; cmd->token; cmd++) {
198                 if (!prefixcmp(cmd->token, arg))
199                         fprintf(stderr, "\t%s\n", cmd->token);
200         }
201
202         exit(1);
203 }
204
205 void help_command_group(const struct cmd_group *grp, int argc, char **argv)
206 {
207         int full = 0;
208
209         if (argc > 1) {
210                 if (!strcmp(argv[1], "--full"))
211                         full = 1;
212         }
213
214         usage_command_group(grp, full, 0);
215 }