btrfs-progs: pretty print key in extent_item
[btrfs-progs-unstable/devel.git] / btrfs.c
blob687acec78304dcd8813b65a88d69fd83a702c942
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 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include "crc32c.h"
23 #include "commands.h"
24 #include "version.h"
26 static const char * const btrfs_cmd_group_usage[] = {
27 "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
28 NULL
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++)
45 if (!*prefix)
46 return 0;
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++) {
58 const char *rest;
60 rest = skip_prefix(arg, cmd->token);
61 if (!rest) {
62 if (!prefixcmp(cmd->token, arg)) {
63 if (abbrev_cmd) {
65 * If this is abbreviated, it is
66 * ambiguous. So when there is no
67 * exact match later, we need to
68 * error out.
70 ambiguous_cmd = abbrev_cmd;
72 abbrev_cmd = cmd;
74 continue;
76 if (*rest)
77 continue;
79 *cmd_ret = cmd;
80 return 0;
83 if (ambiguous_cmd)
84 return -2;
86 if (abbrev_cmd) {
87 *cmd_ret = abbrev_cmd;
88 return 0;
91 return -1;
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)) {
100 case -1:
101 help_unknown_token(arg, grp);
102 case -2:
103 help_ambiguous_token(arg, grp);
106 return cmd;
109 void handle_help_options_next_level(const struct cmd_struct *cmd,
110 int argc, char **argv)
112 if (argc < 2)
113 return;
115 if (!strcmp(argv[1], "--help")) {
116 if (cmd->next) {
117 argc--;
118 argv++;
119 help_command_group(cmd->next, argc, argv);
120 } else {
121 usage_command(cmd, 1, 0);
124 exit(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);
133 argv[0] = argv0_buf;
136 int handle_command_group(const struct cmd_group *grp, int argc,
137 char **argv)
140 const struct cmd_struct *cmd;
142 argc--;
143 argv++;
144 if (argc < 1) {
145 usage_command_group(grp, 0, 0);
146 exit(1);
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);
171 return 1;
174 return 0;
177 int check_argc_max(int nargs, int expected)
179 if (nargs > expected) {
180 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
181 return 1;
184 return 0;
187 const struct cmd_group btrfs_cmd_group;
189 static const char * const cmd_help_usage[] = {
190 "btrfs help [--full]",
191 "Dislay help information",
193 "--full display detailed help on every command",
194 NULL
197 static int cmd_help(int argc, char **argv)
199 help_command_group(&btrfs_cmd_group, argc, argv);
200 return 0;
203 static const char * const cmd_version_usage[] = {
204 "btrfs version",
205 "Display btrfs-progs version",
206 NULL
209 static int cmd_version(int argc, char **argv)
211 printf("%s\n", BTRFS_BUILD_VERSION);
212 return 0;
215 static int handle_options(int *argc, char ***argv)
217 char **orig_argv = *argv;
219 while (*argc > 0) {
220 const char *arg = (*argv)[0];
221 if (arg[0] != '-')
222 break;
224 if (!strcmp(arg, "--help")) {
225 break;
226 } else if (!strcmp(arg, "--version")) {
227 break;
228 } else {
229 fprintf(stderr, "Unknown option: %s\n", arg);
230 fprintf(stderr, "usage: %s\n",
231 btrfs_cmd_group.usagestr[0]);
232 exit(129);
235 (*argv)++;
236 (*argc)--;
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 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
250 { "send", cmd_send, NULL, &send_cmd_group, 0 },
251 { "receive", cmd_receive, NULL, &receive_cmd_group, 0 },
252 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
253 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
254 { "help", cmd_help, cmd_help_usage, NULL, 0 },
255 { "version", cmd_version, cmd_version_usage, NULL, 0 },
256 { 0, 0, 0, 0, 0 }
260 int main(int argc, char **argv)
262 const struct cmd_struct *cmd;
264 crc32c_optimization_init();
266 argc--;
267 argv++;
268 handle_options(&argc, &argv);
269 if (argc > 0) {
270 if (!prefixcmp(argv[0], "--"))
271 argv[0] += 2;
272 } else {
273 usage_command_group(&btrfs_cmd_group, 0, 0);
274 exit(1);
277 cmd = parse_command_token(argv[0], &btrfs_cmd_group);
279 handle_help_options_next_level(cmd, argc, argv);
281 fixup_argv0(argv, cmd->token);
282 exit(cmd->fn(argc, argv));