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 inline const char *skip_prefix(const char *str, const char *prefix)
36 size_t len = strlen(prefix);
37 return strncmp(str, prefix, len) ? NULL : str + len;
40 int prefixcmp(const char *str, const char *prefix)
42 for (; ; str++, prefix++)
45 else if (*str != *prefix)
46 return (unsigned char)*prefix - (unsigned char)*str;
49 static int parse_one_token(const char *arg, const struct cmd_group *grp,
50 const struct cmd_struct **cmd_ret)
52 const struct cmd_struct *cmd = grp->commands;
53 const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
55 for (; cmd->token; cmd++) {
58 rest = skip_prefix(arg, cmd->token);
60 if (!prefixcmp(cmd->token, arg)) {
63 * If this is abbreviated, it is
64 * ambiguous. So when there is no
65 * exact match later, we need to
68 ambiguous_cmd = abbrev_cmd;
85 *cmd_ret = abbrev_cmd;
92 static const struct cmd_struct *
93 parse_command_token(const char *arg, const struct cmd_group *grp)
95 const struct cmd_struct *cmd = NULL;
97 switch(parse_one_token(arg, grp, &cmd)) {
99 help_unknown_token(arg, grp);
101 help_ambiguous_token(arg, grp);
107 static void handle_help_options_next_level(const struct cmd_struct *cmd,
108 int argc, char **argv)
113 if (!strcmp(argv[1], "--help")) {
117 help_command_group(cmd->next, argc, argv);
119 usage_command(cmd, 1, 0);
126 int handle_command_group(const struct cmd_group *grp, int argc,
130 const struct cmd_struct *cmd;
135 usage_command_group(grp, 0, 0);
139 cmd = parse_command_token(argv[0], grp);
141 handle_help_options_next_level(cmd, argc, argv);
143 fixup_argv0(argv, cmd->token);
144 return cmd->fn(argc, argv);
147 static const struct cmd_group btrfs_cmd_group;
149 static const char * const cmd_help_usage[] = {
150 "btrfs help [--full]",
151 "Display help information",
153 "--full display detailed help on every command",
157 static int cmd_help(int argc, char **argv)
159 help_command_group(&btrfs_cmd_group, argc, argv);
163 static const char * const cmd_version_usage[] = {
165 "Display btrfs-progs version",
169 static int cmd_version(int argc, char **argv)
171 printf("%s\n", PACKAGE_STRING);
175 static void check_options(int argc, char **argv)
185 !strcmp(arg, "--help") ||
186 !strcmp(arg, "--version"))
189 fprintf(stderr, "Unknown option: %s\n", arg);
190 fprintf(stderr, "usage: %s\n",
191 btrfs_cmd_group.usagestr[0]);
195 static const struct cmd_group btrfs_cmd_group = {
196 btrfs_cmd_group_usage, btrfs_cmd_group_info, {
197 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
198 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
199 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
200 { "device", cmd_device, NULL, &device_cmd_group, 0 },
201 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
202 { "check", cmd_check, cmd_check_usage, NULL, 0 },
203 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
204 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
205 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
206 { "property", cmd_property, NULL, &property_cmd_group, 0 },
207 { "send", cmd_send, cmd_send_usage, NULL, 0 },
208 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
209 { "quota", cmd_quota, NULL, "a_cmd_group, 0 },
210 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
211 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
212 { "help", cmd_help, cmd_help_usage, NULL, 0 },
213 { "version", cmd_version, cmd_version_usage, NULL, 0 },
218 int main(int argc, char **argv)
220 const struct cmd_struct *cmd;
224 if ((bname = strrchr(argv[0], '/')) != NULL)
229 if (!strcmp(bname, "btrfsck")) {
234 check_options(argc, argv);
236 if (!prefixcmp(argv[0], "--"))
239 usage_command_group_short(&btrfs_cmd_group);
244 cmd = parse_command_token(argv[0], &btrfs_cmd_group);
246 handle_help_options_next_level(cmd, argc, argv);
248 crc32c_optimization_init();
250 fixup_argv0(argv, cmd->token);
252 ret = cmd->fn(argc, argv);
254 btrfs_close_all_devices();