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.
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.
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.
26 static const char * const btrfs_cmd_group_usage[] = {
27 "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
31 static const char btrfs_cmd_group_info[] =
32 "Use --help as an argument for information on a specific group or command.";
34 char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
36 static inline const char *skip_prefix(const char *str, const char *prefix)
38 size_t len = strlen(prefix);
39 return strncmp(str, prefix, len) ? NULL : str + len;
42 int prefixcmp(const char *str, const char *prefix)
44 for (; ; str++, prefix++)
47 else if (*str != *prefix)
48 return (unsigned char)*prefix - (unsigned char)*str;
51 static int parse_one_token(const char *arg, const struct cmd_group *grp,
52 const struct cmd_struct **cmd_ret)
54 const struct cmd_struct *cmd = grp->commands;
55 const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
57 for (; cmd->token; cmd++) {
60 rest = skip_prefix(arg, cmd->token);
62 if (!prefixcmp(cmd->token, arg)) {
65 * If this is abbreviated, it is
66 * ambiguous. So when there is no
67 * exact match later, we need to
70 ambiguous_cmd = abbrev_cmd;
87 *cmd_ret = abbrev_cmd;
94 static const struct cmd_struct *
95 parse_command_token(const char *arg, const struct cmd_group *grp)
97 const struct cmd_struct *cmd = NULL;
99 switch(parse_one_token(arg, grp, &cmd)) {
101 help_unknown_token(arg, grp);
103 help_ambiguous_token(arg, grp);
109 void handle_help_options_next_level(const struct cmd_struct *cmd,
110 int argc, char **argv)
115 if (!strcmp(argv[1], "--help")) {
119 help_command_group(cmd->next, argc, argv);
121 usage_command(cmd, 1, 0);
128 static void fixup_argv0(char **argv, const char *token)
130 int len = strlen(argv0_buf);
132 snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
136 int handle_command_group(const struct cmd_group *grp, int argc,
140 const struct cmd_struct *cmd;
145 usage_command_group(grp, 0, 0);
149 cmd = parse_command_token(argv[0], grp);
151 handle_help_options_next_level(cmd, argc, argv);
153 fixup_argv0(argv, cmd->token);
154 return cmd->fn(argc, argv);
157 int check_argc_exact(int nargs, int expected)
159 if (nargs < expected)
160 fprintf(stderr, "%s: too few arguments\n", argv0_buf);
161 if (nargs > expected)
162 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
164 return nargs != expected;
167 int check_argc_min(int nargs, int expected)
169 if (nargs < expected) {
170 fprintf(stderr, "%s: too few arguments\n", argv0_buf);
177 int check_argc_max(int nargs, int expected)
179 if (nargs > expected) {
180 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
187 const struct cmd_group btrfs_cmd_group;
189 static const char * const cmd_help_usage[] = {
190 "btrfs help [--full]",
191 "Display help information",
193 "--full display detailed help on every command",
197 static int cmd_help(int argc, char **argv)
199 help_command_group(&btrfs_cmd_group, argc, argv);
203 static const char * const cmd_version_usage[] = {
205 "Display btrfs-progs version",
209 static int cmd_version(int argc, char **argv)
211 printf("%s\n", BTRFS_BUILD_VERSION);
215 static int handle_options(int *argc, char ***argv)
217 char **orig_argv = *argv;
220 const char *arg = (*argv)[0];
224 if (!strcmp(arg, "--help")) {
226 } else if (!strcmp(arg, "--version")) {
229 fprintf(stderr, "Unknown option: %s\n", arg);
230 fprintf(stderr, "usage: %s\n",
231 btrfs_cmd_group.usagestr[0]);
239 return (*argv) - orig_argv;
242 const struct cmd_group btrfs_cmd_group = {
243 btrfs_cmd_group_usage, btrfs_cmd_group_info, {
244 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
245 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
246 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
247 { "device", cmd_device, NULL, &device_cmd_group, 0 },
248 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
249 { "check", cmd_check, cmd_check_usage, NULL, 0 },
250 { "chunk-recover", cmd_chunk_recover, cmd_chunk_recover_usage, NULL, 0},
251 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
252 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
253 { "send", cmd_send, cmd_send_usage, NULL, 0 },
254 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
255 { "quota", cmd_quota, NULL, "a_cmd_group, 0 },
256 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
257 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
258 { "help", cmd_help, cmd_help_usage, NULL, 0 },
259 { "version", cmd_version, cmd_version_usage, NULL, 0 },
264 int main(int argc, char **argv)
266 const struct cmd_struct *cmd;
269 if ((bname = strrchr(argv[0], '/')) != NULL)
274 if (!strcmp(bname, "btrfsck")) {
279 handle_options(&argc, &argv);
281 if (!prefixcmp(argv[0], "--"))
284 usage_command_group(&btrfs_cmd_group, 0, 0);
289 cmd = parse_command_token(argv[0], &btrfs_cmd_group);
291 handle_help_options_next_level(cmd, argc, argv);
293 crc32c_optimization_init();
295 fixup_argv0(argv, cmd->token);
296 exit(cmd->fn(argc, argv));