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.
28 static const char * const btrfs_cmd_group_usage
[] = {
29 "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
33 static const char btrfs_cmd_group_info
[] =
34 "Use --help as an argument for information on a specific group or command.";
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 static int parse_one_token(const char *arg
, const struct cmd_group
*grp
,
43 const struct cmd_struct
**cmd_ret
)
45 const struct cmd_struct
*cmd
= grp
->commands
;
46 const struct cmd_struct
*abbrev_cmd
= NULL
, *ambiguous_cmd
= NULL
;
48 for (; cmd
->token
; cmd
++) {
51 rest
= skip_prefix(arg
, cmd
->token
);
53 if (!prefixcmp(cmd
->token
, arg
)) {
56 * If this is abbreviated, it is
57 * ambiguous. So when there is no
58 * exact match later, we need to
61 ambiguous_cmd
= abbrev_cmd
;
78 *cmd_ret
= abbrev_cmd
;
85 static const struct cmd_struct
*
86 parse_command_token(const char *arg
, const struct cmd_group
*grp
)
88 const struct cmd_struct
*cmd
= NULL
;
90 switch(parse_one_token(arg
, grp
, &cmd
)) {
92 help_unknown_token(arg
, grp
);
94 help_ambiguous_token(arg
, grp
);
100 static void handle_help_options_next_level(const struct cmd_struct
*cmd
,
101 int argc
, char **argv
)
106 if (!strcmp(argv
[1], "--help")) {
110 help_command_group(cmd
->next
, argc
, argv
);
112 usage_command(cmd
, 1, 0);
119 int handle_command_group(const struct cmd_group
*grp
, int argc
,
123 const struct cmd_struct
*cmd
;
128 usage_command_group(grp
, 0, 0);
132 cmd
= parse_command_token(argv
[0], grp
);
134 handle_help_options_next_level(cmd
, argc
, argv
);
136 fixup_argv0(argv
, cmd
->token
);
137 return cmd
->fn(argc
, argv
);
140 static const struct cmd_group btrfs_cmd_group
;
142 static const char * const cmd_help_usage
[] = {
143 "btrfs help [--full]",
144 "Display help information",
146 "--full display detailed help on every command",
150 static int cmd_help(int argc
, char **argv
)
152 help_command_group(&btrfs_cmd_group
, argc
, argv
);
156 static const char * const cmd_version_usage
[] = {
158 "Display btrfs-progs version",
162 static int cmd_version(int argc
, char **argv
)
164 printf("%s\n", PACKAGE_STRING
);
169 * Parse global options, between binary name and first non-option argument
170 * after processing all valid options (including those with arguments).
172 * Returns index to argv where parsting stopped, optind is reset to 1
174 static int handle_global_options(int argc
, char **argv
)
176 enum { OPT_HELP
= 256, OPT_VERSION
, OPT_FULL
};
177 static const struct option long_options
[] = {
178 { "help", no_argument
, NULL
, OPT_HELP
},
179 { "version", no_argument
, NULL
, OPT_VERSION
},
180 { "full", no_argument
, NULL
, OPT_FULL
},
192 c
= getopt_long(argc
, argv
, "+", long_options
, NULL
);
197 case OPT_HELP
: break;
198 case OPT_VERSION
: break;
199 case OPT_FULL
: break;
201 fprintf(stderr
, "Unknown global option: %s\n",
213 void handle_special_globals(int shift
, int argc
, char **argv
)
219 for (i
= 0; i
< shift
; i
++) {
220 if (strcmp(argv
[i
], "--help") == 0)
222 else if (strcmp(argv
[i
], "--full") == 0)
228 usage_command_group(&btrfs_cmd_group
, 1, 0);
230 cmd_help(argc
, argv
);
234 for (i
= 0; i
< shift
; i
++)
235 if (strcmp(argv
[i
], "--version") == 0) {
236 cmd_version(argc
, argv
);
241 static const struct cmd_group btrfs_cmd_group
= {
242 btrfs_cmd_group_usage
, btrfs_cmd_group_info
, {
243 { "subvolume", cmd_subvolume
, NULL
, &subvolume_cmd_group
, 0 },
244 { "filesystem", cmd_filesystem
, NULL
, &filesystem_cmd_group
, 0 },
245 { "balance", cmd_balance
, NULL
, &balance_cmd_group
, 0 },
246 { "device", cmd_device
, NULL
, &device_cmd_group
, 0 },
247 { "scrub", cmd_scrub
, NULL
, &scrub_cmd_group
, 0 },
248 { "check", cmd_check
, cmd_check_usage
, NULL
, 0 },
249 { "rescue", cmd_rescue
, NULL
, &rescue_cmd_group
, 0 },
250 { "restore", cmd_restore
, cmd_restore_usage
, NULL
, 0 },
251 { "inspect-internal", cmd_inspect
, NULL
, &inspect_cmd_group
, 0 },
252 { "property", cmd_property
, NULL
, &property_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
;
272 if ((bname
= strrchr(argv
[0], '/')) != NULL
)
277 if (!strcmp(bname
, "btrfsck")) {
282 shift
= handle_global_options(argc
, argv
);
283 handle_special_globals(shift
, argc
, argv
);
284 while (shift
-- > 0) {
289 usage_command_group_short(&btrfs_cmd_group
);
294 cmd
= parse_command_token(argv
[0], &btrfs_cmd_group
);
296 handle_help_options_next_level(cmd
, argc
, argv
);
298 crc32c_optimization_init();
300 fixup_argv0(argv
, cmd
->token
);
302 ret
= cmd
->fn(argc
, argv
);
304 btrfs_close_all_devices();