Merge branch 'parser' of git://github.com/idryomov/btrfs-progs
[platform/upstream/btrfs-progs.git] / btrfs.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 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "commands.h"
23 #include "version.h"
24
25 static const char btrfs_cmd_group_usage[] =
26         "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]";
27
28 static const char btrfs_cmd_group_info[] =
29         "Use --help as an argument for information on a specific group or command.";
30
31 char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
32
33 static inline const char *skip_prefix(const char *str, const char *prefix)
34 {
35         size_t len = strlen(prefix);
36         return strncmp(str, prefix, len) ? NULL : str + len;
37 }
38
39 int prefixcmp(const char *str, const char *prefix)
40 {
41         for (; ; str++, prefix++)
42                 if (!*prefix)
43                         return 0;
44                 else if (*str != *prefix)
45                         return (unsigned char)*prefix - (unsigned char)*str;
46 }
47
48 static int parse_one_token(const char *arg, const struct cmd_group *grp,
49                            const struct cmd_struct **cmd_ret)
50 {
51         const struct cmd_struct *cmd = grp->commands;
52         const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
53
54         for (; cmd->token; cmd++) {
55                 const char *rest;
56
57                 rest = skip_prefix(arg, cmd->token);
58                 if (!rest) {
59                         if (!prefixcmp(cmd->token, arg)) {
60                                 if (abbrev_cmd) {
61                                         /*
62                                          * If this is abbreviated, it is
63                                          * ambiguous. So when there is no
64                                          * exact match later, we need to
65                                          * error out.
66                                          */
67                                         ambiguous_cmd = abbrev_cmd;
68                                 }
69                                 abbrev_cmd = cmd;
70                         }
71                         continue;
72                 }
73                 if (*rest)
74                         continue;
75
76                 *cmd_ret = cmd;
77                 return 0;
78         }
79
80         if (ambiguous_cmd)
81                 return -2;
82
83         if (abbrev_cmd) {
84                 *cmd_ret = abbrev_cmd;
85                 return 0;
86         }
87
88         return -1;
89 }
90
91 static const struct cmd_struct *
92 parse_command_token(const char *arg, const struct cmd_group *grp)
93 {
94         const struct cmd_struct *cmd;
95
96         switch(parse_one_token(arg, grp, &cmd)) {
97         case -1:
98                 help_unknown_token(arg, grp);
99         case -2:
100                 help_ambiguous_token(arg, grp);
101         }
102
103         return cmd;
104 }
105
106 void handle_help_options_next_level(const struct cmd_struct *cmd,
107                                     int argc, char **argv)
108 {
109         if (argc < 2)
110                 return;
111
112         if (!strcmp(argv[1], "--help")) {
113                 if (cmd->next) {
114                         argc--;
115                         argv++;
116                         help_command_group(cmd->next, argc, argv);
117                 } else {
118                         usage_command(cmd, 1, 0);
119                 }
120
121                 exit(0);
122         }
123 }
124
125 static void fixup_argv0(char **argv, const char *token)
126 {
127         int len = strlen(argv0_buf);
128
129         snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
130         argv[0] = argv0_buf;
131 }
132
133 int handle_command_group(const struct cmd_group *grp, int argc,
134                          char **argv)
135
136 {
137         const struct cmd_struct *cmd;
138
139         argc--;
140         argv++;
141         if (argc < 1) {
142                 usage_command_group(grp, 0, 0);
143                 exit(1);
144         }
145
146         cmd = parse_command_token(argv[0], grp);
147
148         handle_help_options_next_level(cmd, argc, argv);
149
150         fixup_argv0(argv, cmd->token);
151         return cmd->fn(argc, argv);
152 }
153
154 int check_argc_exact(int nargs, int expected)
155 {
156         if (nargs < expected)
157                 fprintf(stderr, "%s: too few arguments\n", argv0_buf);
158         if (nargs > expected)
159                 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
160
161         return nargs != expected;
162 }
163
164 int check_argc_min(int nargs, int expected)
165 {
166         if (nargs < expected) {
167                 fprintf(stderr, "%s: too few arguments\n", argv0_buf);
168                 return 1;
169         }
170
171         return 0;
172 }
173
174 int check_argc_max(int nargs, int expected)
175 {
176         if (nargs > expected) {
177                 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
178                 return 1;
179         }
180
181         return 0;
182 }
183
184 const struct cmd_group btrfs_cmd_group;
185
186 static const char * const cmd_help_usage[] = {
187         "btrfs help [--full]",
188         "Dislay help information",
189         "",
190         "--full     display detailed help on every command",
191         NULL
192 };
193
194 static int cmd_help(int argc, char **argv)
195 {
196         help_command_group(&btrfs_cmd_group, argc, argv);
197         return 0;
198 }
199
200 static const char * const cmd_version_usage[] = {
201         "btrfs version",
202         "Display btrfs-progs version",
203         NULL
204 };
205
206 static int cmd_version(int argc, char **argv)
207 {
208         printf("%s\n", BTRFS_BUILD_VERSION);
209         return 0;
210 }
211
212 static int handle_options(int *argc, char ***argv)
213 {
214         char **orig_argv = *argv;
215
216         while (*argc > 0) {
217                 const char *arg = (*argv)[0];
218                 if (arg[0] != '-')
219                         break;
220
221                 if (!strcmp(arg, "--help")) {
222                         break;
223                 } else if (!strcmp(arg, "--version")) {
224                         break;
225                 } else {
226                         fprintf(stderr, "Unknown option: %s\n", arg);
227                         fprintf(stderr, "usage: %s\n",
228                                 btrfs_cmd_group.usagestr);
229                         exit(129);
230                 }
231
232                 (*argv)++;
233                 (*argc)--;
234         }
235
236         return (*argv) - orig_argv;
237 }
238
239 const struct cmd_group btrfs_cmd_group = {
240         btrfs_cmd_group_usage, btrfs_cmd_group_info, {
241                 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
242                 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
243                 { "device", cmd_device, NULL, &device_cmd_group, 0 },
244                 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
245                 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
246                 { "help", cmd_help, cmd_help_usage, NULL, 0 },
247                 { "version", cmd_version, cmd_version_usage, NULL, 0 },
248                 { 0, 0, 0, 0, 0 }
249         },
250 };
251
252 int main(int argc, char **argv)
253 {
254         const struct cmd_struct *cmd;
255
256         argc--;
257         argv++;
258         handle_options(&argc, &argv);
259         if (argc > 0) {
260                 if (!prefixcmp(argv[0], "--"))
261                         argv[0] += 2;
262         } else {
263                 usage_command_group(&btrfs_cmd_group, 0, 0);
264                 exit(1);
265         }
266
267         cmd = parse_command_token(argv[0], &btrfs_cmd_group);
268
269         handle_help_options_next_level(cmd, argc, argv);
270
271         fixup_argv0(argv, cmd->token);
272         exit(cmd->fn(argc, argv));
273 }