btrfs-progs: move help defines to own header
[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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "volumes.h"
22 #include "crc32c.h"
23 #include "commands.h"
24 #include "utils.h"
25 #include "help.h"
26
27 static const char * const btrfs_cmd_group_usage[] = {
28         "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
29         NULL
30 };
31
32 static const char btrfs_cmd_group_info[] =
33         "Use --help as an argument for information on a specific group or command.";
34
35 static inline const char *skip_prefix(const char *str, const char *prefix)
36 {
37         size_t len = strlen(prefix);
38         return strncmp(str, prefix, len) ? NULL : str + len;
39 }
40
41 static int parse_one_token(const char *arg, const struct cmd_group *grp,
42                            const struct cmd_struct **cmd_ret)
43 {
44         const struct cmd_struct *cmd = grp->commands;
45         const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
46
47         for (; cmd->token; cmd++) {
48                 const char *rest;
49
50                 rest = skip_prefix(arg, cmd->token);
51                 if (!rest) {
52                         if (!prefixcmp(cmd->token, arg)) {
53                                 if (abbrev_cmd) {
54                                         /*
55                                          * If this is abbreviated, it is
56                                          * ambiguous. So when there is no
57                                          * exact match later, we need to
58                                          * error out.
59                                          */
60                                         ambiguous_cmd = abbrev_cmd;
61                                 }
62                                 abbrev_cmd = cmd;
63                         }
64                         continue;
65                 }
66                 if (*rest)
67                         continue;
68
69                 *cmd_ret = cmd;
70                 return 0;
71         }
72
73         if (ambiguous_cmd)
74                 return -2;
75
76         if (abbrev_cmd) {
77                 *cmd_ret = abbrev_cmd;
78                 return 0;
79         }
80
81         return -1;
82 }
83
84 static const struct cmd_struct *
85 parse_command_token(const char *arg, const struct cmd_group *grp)
86 {
87         const struct cmd_struct *cmd = NULL;
88
89         switch(parse_one_token(arg, grp, &cmd)) {
90         case -1:
91                 help_unknown_token(arg, grp);
92         case -2:
93                 help_ambiguous_token(arg, grp);
94         }
95
96         return cmd;
97 }
98
99 static void handle_help_options_next_level(const struct cmd_struct *cmd,
100                 int argc, char **argv)
101 {
102         if (argc < 2)
103                 return;
104
105         if (!strcmp(argv[1], "--help")) {
106                 if (cmd->next) {
107                         argc--;
108                         argv++;
109                         help_command_group(cmd->next, argc, argv);
110                 } else {
111                         usage_command(cmd, 1, 0);
112                 }
113
114                 exit(0);
115         }
116 }
117
118 int handle_command_group(const struct cmd_group *grp, int argc,
119                          char **argv)
120
121 {
122         const struct cmd_struct *cmd;
123
124         argc--;
125         argv++;
126         if (argc < 1) {
127                 usage_command_group(grp, 0, 0);
128                 exit(1);
129         }
130
131         cmd = parse_command_token(argv[0], grp);
132
133         handle_help_options_next_level(cmd, argc, argv);
134
135         fixup_argv0(argv, cmd->token);
136         return cmd->fn(argc, argv);
137 }
138
139 static const struct cmd_group btrfs_cmd_group;
140
141 static const char * const cmd_help_usage[] = {
142         "btrfs help [--full]",
143         "Display help information",
144         "",
145         "--full     display detailed help on every command",
146         NULL
147 };
148
149 static int cmd_help(int argc, char **argv)
150 {
151         help_command_group(&btrfs_cmd_group, argc, argv);
152         return 0;
153 }
154
155 static const char * const cmd_version_usage[] = {
156         "btrfs version",
157         "Display btrfs-progs version",
158         NULL
159 };
160
161 static int cmd_version(int argc, char **argv)
162 {
163         printf("%s\n", PACKAGE_STRING);
164         return 0;
165 }
166
167 static void check_options(int argc, char **argv)
168 {
169         const char *arg;
170
171         if (argc == 0)
172                 return;
173
174         arg = argv[0];
175
176         if (arg[0] != '-' ||
177             !strcmp(arg, "--help") ||
178             !strcmp(arg, "--version"))
179                 return;
180
181         fprintf(stderr, "Unknown option: %s\n", arg);
182         fprintf(stderr, "usage: %s\n",
183                 btrfs_cmd_group.usagestr[0]);
184         exit(129);
185 }
186
187 static const struct cmd_group btrfs_cmd_group = {
188         btrfs_cmd_group_usage, btrfs_cmd_group_info, {
189                 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
190                 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
191                 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
192                 { "device", cmd_device, NULL, &device_cmd_group, 0 },
193                 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
194                 { "check", cmd_check, cmd_check_usage, NULL, 0 },
195                 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
196                 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
197                 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
198                 { "property", cmd_property, NULL, &property_cmd_group, 0 },
199                 { "send", cmd_send, cmd_send_usage, NULL, 0 },
200                 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
201                 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
202                 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
203                 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
204                 { "help", cmd_help, cmd_help_usage, NULL, 0 },
205                 { "version", cmd_version, cmd_version_usage, NULL, 0 },
206                 NULL_CMD_STRUCT
207         },
208 };
209
210 int main(int argc, char **argv)
211 {
212         const struct cmd_struct *cmd;
213         const char *bname;
214         int ret;
215
216         btrfs_config_init();
217
218         if ((bname = strrchr(argv[0], '/')) != NULL)
219                 bname++;
220         else
221                 bname = argv[0];
222
223         if (!strcmp(bname, "btrfsck")) {
224                 argv[0] = "check";
225         } else {
226                 argc--;
227                 argv++;
228                 check_options(argc, argv);
229                 if (argc > 0) {
230                         if (!prefixcmp(argv[0], "--"))
231                                 argv[0] += 2;
232                 } else {
233                         usage_command_group_short(&btrfs_cmd_group);
234                         exit(1);
235                 }
236         }
237
238         cmd = parse_command_token(argv[0], &btrfs_cmd_group);
239
240         handle_help_options_next_level(cmd, argc, argv);
241
242         crc32c_optimization_init();
243
244         fixup_argv0(argv, cmd->token);
245
246         ret = cmd->fn(argc, argv);
247
248         btrfs_close_all_devices();
249
250         exit(ret);
251 }