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 static 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 static 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 static 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 void handle_options(int *argc, char ***argv)
218 const char *arg = (*argv)[0];
220 !strcmp(arg, "--help") ||
221 !strcmp(arg, "--version"))
223 fprintf(stderr, "Unknown option: %s\n", arg);
224 fprintf(stderr, "usage: %s\n",
225 btrfs_cmd_group.usagestr[0]);
231 static const struct cmd_group btrfs_cmd_group = {
232 btrfs_cmd_group_usage, btrfs_cmd_group_info, {
233 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
234 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
235 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
236 { "device", cmd_device, NULL, &device_cmd_group, 0 },
237 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
238 { "check", cmd_check, cmd_check_usage, NULL, 0 },
239 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
240 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
241 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
242 { "property", cmd_property, NULL, &property_cmd_group, 0 },
243 { "send", cmd_send, cmd_send_usage, NULL, 0 },
244 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
245 { "quota", cmd_quota, NULL, "a_cmd_group, 0 },
246 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
247 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
248 { "help", cmd_help, cmd_help_usage, NULL, 0 },
249 { "version", cmd_version, cmd_version_usage, NULL, 0 },
254 int main(int argc, char **argv)
256 const struct cmd_struct *cmd;
259 if ((bname = strrchr(argv[0], '/')) != NULL)
264 if (!strcmp(bname, "btrfsck")) {
269 handle_options(&argc, &argv);
271 if (!prefixcmp(argv[0], "--"))
274 usage_command_group(&btrfs_cmd_group, 0, 0);
279 cmd = parse_command_token(argv[0], &btrfs_cmd_group);
281 handle_help_options_next_level(cmd, argc, argv);
283 crc32c_optimization_init();
285 fixup_argv0(argv, cmd->token);
286 exit(cmd->fn(argc, argv));