btrfs-progs: alias btrfs device delete to btrfs device remove
[btrfs-progs-unstable/devel.git] / btrfs.c
blob63df377a7ec8f32b71ddc68dc9798f4d67e924b6
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.
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.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "crc32c.h"
22 #include "commands.h"
23 #include "utils.h"
25 static const char * const btrfs_cmd_group_usage[] = {
26 "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
27 NULL
30 static const char btrfs_cmd_group_info[] =
31 "Use --help as an argument for information on a specific group or command.";
33 static inline const char *skip_prefix(const char *str, const char *prefix)
35 size_t len = strlen(prefix);
36 return strncmp(str, prefix, len) ? NULL : str + len;
39 int prefixcmp(const char *str, const char *prefix)
41 for (; ; str++, prefix++)
42 if (!*prefix)
43 return 0;
44 else if (*str != *prefix)
45 return (unsigned char)*prefix - (unsigned char)*str;
48 static int parse_one_token(const char *arg, const struct cmd_group *grp,
49 const struct cmd_struct **cmd_ret)
51 const struct cmd_struct *cmd = grp->commands;
52 const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
54 for (; cmd->token; cmd++) {
55 const char *rest;
57 rest = skip_prefix(arg, cmd->token);
58 if (!rest) {
59 if (!prefixcmp(cmd->token, arg)) {
60 if (abbrev_cmd) {
62 * If this is abbreviated, it is
63 * ambiguous. So when there is no
64 * exact match later, we need to
65 * error out.
67 ambiguous_cmd = abbrev_cmd;
69 abbrev_cmd = cmd;
71 continue;
73 if (*rest)
74 continue;
76 *cmd_ret = cmd;
77 return 0;
80 if (ambiguous_cmd)
81 return -2;
83 if (abbrev_cmd) {
84 *cmd_ret = abbrev_cmd;
85 return 0;
88 return -1;
91 static const struct cmd_struct *
92 parse_command_token(const char *arg, const struct cmd_group *grp)
94 const struct cmd_struct *cmd = NULL;
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);
103 return cmd;
106 static void handle_help_options_next_level(const struct cmd_struct *cmd,
107 int argc, char **argv)
109 if (argc < 2)
110 return;
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);
121 exit(0);
125 int handle_command_group(const struct cmd_group *grp, int argc,
126 char **argv)
129 const struct cmd_struct *cmd;
131 argc--;
132 argv++;
133 if (argc < 1) {
134 usage_command_group(grp, 0, 0);
135 exit(1);
138 cmd = parse_command_token(argv[0], grp);
140 handle_help_options_next_level(cmd, argc, argv);
142 fixup_argv0(argv, cmd->token);
143 return cmd->fn(argc, argv);
146 static const struct cmd_group btrfs_cmd_group;
148 static const char * const cmd_help_usage[] = {
149 "btrfs help [--full]",
150 "Display help information",
152 "--full display detailed help on every command",
153 NULL
156 static int cmd_help(int argc, char **argv)
158 help_command_group(&btrfs_cmd_group, argc, argv);
159 return 0;
162 static const char * const cmd_version_usage[] = {
163 "btrfs version",
164 "Display btrfs-progs version",
165 NULL
168 static int cmd_version(int argc, char **argv)
170 printf("%s\n", PACKAGE_STRING);
171 return 0;
174 static void handle_options(int *argc, char ***argv)
176 if (*argc > 0) {
177 const char *arg = (*argv)[0];
178 if (arg[0] != '-' ||
179 !strcmp(arg, "--help") ||
180 !strcmp(arg, "--version"))
181 return;
182 fprintf(stderr, "Unknown option: %s\n", arg);
183 fprintf(stderr, "usage: %s\n",
184 btrfs_cmd_group.usagestr[0]);
185 exit(129);
187 return;
190 static const struct cmd_group btrfs_cmd_group = {
191 btrfs_cmd_group_usage, btrfs_cmd_group_info, {
192 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
193 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
194 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
195 { "device", cmd_device, NULL, &device_cmd_group, 0 },
196 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
197 { "check", cmd_check, cmd_check_usage, NULL, 0 },
198 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
199 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
200 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
201 { "property", cmd_property, NULL, &property_cmd_group, 0 },
202 { "send", cmd_send, cmd_send_usage, NULL, 0 },
203 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
204 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
205 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
206 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
207 { "help", cmd_help, cmd_help_usage, NULL, 0 },
208 { "version", cmd_version, cmd_version_usage, NULL, 0 },
209 NULL_CMD_STRUCT
213 int main(int argc, char **argv)
215 const struct cmd_struct *cmd;
216 const char *bname;
218 if ((bname = strrchr(argv[0], '/')) != NULL)
219 bname++;
220 else
221 bname = argv[0];
223 if (!strcmp(bname, "btrfsck")) {
224 argv[0] = "check";
225 } else {
226 argc--;
227 argv++;
228 handle_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);
238 cmd = parse_command_token(argv[0], &btrfs_cmd_group);
240 handle_help_options_next_level(cmd, argc, argv);
242 crc32c_optimization_init();
244 fixup_argv0(argv, cmd->token);
245 exit(cmd->fn(argc, argv));