2012-02-04 03:00:17 +08:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-12-13 22:07:25 +08:00
|
|
|
#include <limits.h>
|
2017-01-25 23:35:00 +08:00
|
|
|
#include <getopt.h>
|
2012-02-04 03:00:17 +08:00
|
|
|
|
|
|
|
#include "commands.h"
|
2014-06-30 11:54:11 +08:00
|
|
|
#include "utils.h"
|
2017-01-25 23:35:00 +08:00
|
|
|
#include "help.h"
|
2012-02-04 03:00:17 +08:00
|
|
|
|
|
|
|
#define USAGE_SHORT 1U
|
|
|
|
#define USAGE_LONG 2U
|
|
|
|
#define USAGE_OPTIONS 4U
|
|
|
|
#define USAGE_LISTING 8U
|
|
|
|
|
2017-01-25 23:35:00 +08:00
|
|
|
static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
|
|
|
|
|
|
|
|
const char *get_argv0_buf(void)
|
|
|
|
{
|
|
|
|
return argv0_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fixup_argv0(char **argv, const char *token)
|
|
|
|
{
|
|
|
|
int len = strlen(argv0_buf);
|
|
|
|
|
|
|
|
snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
|
|
|
|
argv[0] = argv0_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_argv0(char **argv)
|
|
|
|
{
|
|
|
|
strncpy(argv0_buf, argv[0], sizeof(argv0_buf));
|
|
|
|
argv0_buf[sizeof(argv0_buf) - 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_argc_exact(int nargs, int expected)
|
|
|
|
{
|
|
|
|
if (nargs < expected)
|
|
|
|
fprintf(stderr, "%s: too few arguments\n", argv0_buf);
|
|
|
|
if (nargs > expected)
|
|
|
|
fprintf(stderr, "%s: too many arguments\n", argv0_buf);
|
|
|
|
|
|
|
|
return nargs != expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_argc_min(int nargs, int expected)
|
|
|
|
{
|
|
|
|
if (nargs < expected) {
|
|
|
|
fprintf(stderr, "%s: too few arguments\n", argv0_buf);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_argc_max(int nargs, int expected)
|
|
|
|
{
|
|
|
|
if (nargs > expected) {
|
|
|
|
fprintf(stderr, "%s: too many arguments\n", argv0_buf);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Preprocess @argv with getopt_long to reorder options and consume the "--"
|
|
|
|
* option separator.
|
|
|
|
* Unknown short and long options are reported, optionally the @usage is printed
|
|
|
|
* before exit.
|
|
|
|
*/
|
|
|
|
void clean_args_no_options(int argc, char *argv[], const char * const *usagestr)
|
|
|
|
{
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int c = getopt_long(argc, argv, "", long_options, NULL);
|
|
|
|
|
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
if (usagestr)
|
|
|
|
usage(usagestr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same as clean_args_no_options but pass through arguments that could look
|
|
|
|
* like short options. Eg. reisze which takes a negative resize argument like
|
|
|
|
* '-123M' .
|
|
|
|
*
|
|
|
|
* This accepts only two forms:
|
|
|
|
* - "-- option1 option2 ..."
|
|
|
|
* - "option1 option2 ..."
|
|
|
|
*/
|
|
|
|
void clean_args_no_options_relaxed(int argc, char *argv[], const char * const *usagestr)
|
|
|
|
{
|
|
|
|
if (argc <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "--") == 0)
|
|
|
|
optind = 2;
|
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static int do_usage_one_command(const char * const *usagestr,
|
|
|
|
unsigned int flags, FILE *outf)
|
|
|
|
{
|
|
|
|
int pad = 4;
|
|
|
|
|
|
|
|
if (!usagestr || !*usagestr)
|
|
|
|
return -1;
|
|
|
|
|
2015-06-26 22:44:29 +08:00
|
|
|
fprintf(outf, "%s%s", (flags & USAGE_LISTING) ? " " : "usage: ",
|
2012-02-04 03:00:17 +08:00
|
|
|
*usagestr++);
|
|
|
|
|
|
|
|
/* a short one-line description (mandatory) */
|
|
|
|
if ((flags & USAGE_SHORT) == 0)
|
|
|
|
return 0;
|
|
|
|
else if (!*usagestr)
|
|
|
|
return -2;
|
2015-06-26 22:44:29 +08:00
|
|
|
fputc('\n', outf);
|
2012-02-04 03:00:17 +08:00
|
|
|
|
|
|
|
if (flags & USAGE_LISTING)
|
|
|
|
pad = 8;
|
|
|
|
else
|
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
|
|
|
|
|
|
|
|
/* a long (possibly multi-line) description (optional) */
|
|
|
|
if (!*usagestr || ((flags & USAGE_LONG) == 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (**usagestr)
|
|
|
|
fputc('\n', outf);
|
|
|
|
while (*usagestr && **usagestr)
|
|
|
|
fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
|
|
|
|
|
|
|
|
/* options (optional) */
|
|
|
|
if (!*usagestr || ((flags & USAGE_OPTIONS) == 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* options (if present) should always (even if there is no long
|
|
|
|
* description) be prepended with an empty line, skip it
|
|
|
|
*/
|
|
|
|
usagestr++;
|
|
|
|
|
|
|
|
fputc('\n', outf);
|
|
|
|
while (*usagestr)
|
|
|
|
fprintf(outf, "%*s%s\n", pad, "", *usagestr++);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usage_command_internal(const char * const *usagestr,
|
|
|
|
const char *token, int full, int lst,
|
2015-06-25 00:09:17 +08:00
|
|
|
int alias, FILE *outf)
|
2012-02-04 03:00:17 +08:00
|
|
|
{
|
2015-06-25 00:09:17 +08:00
|
|
|
unsigned int flags = 0;
|
2012-02-04 03:00:17 +08:00
|
|
|
int ret;
|
|
|
|
|
2015-06-25 00:09:17 +08:00
|
|
|
if (!alias)
|
|
|
|
flags |= USAGE_SHORT;
|
2012-02-04 03:00:17 +08:00
|
|
|
if (full)
|
|
|
|
flags |= USAGE_LONG | USAGE_OPTIONS;
|
|
|
|
if (lst)
|
|
|
|
flags |= USAGE_LISTING;
|
|
|
|
|
|
|
|
ret = do_usage_one_command(usagestr, flags, outf);
|
|
|
|
switch (ret) {
|
|
|
|
case -1:
|
|
|
|
fprintf(outf, "No usage for '%s'\n", token);
|
|
|
|
break;
|
|
|
|
case -2:
|
|
|
|
fprintf(outf, "No short description for '%s'\n", token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage_command_usagestr(const char * const *usagestr,
|
|
|
|
const char *token, int full, int err)
|
|
|
|
{
|
|
|
|
FILE *outf = err ? stderr : stdout;
|
|
|
|
int ret;
|
|
|
|
|
2015-06-25 00:09:17 +08:00
|
|
|
ret = usage_command_internal(usagestr, token, full, 0, 0, outf);
|
2012-02-04 03:00:17 +08:00
|
|
|
if (!ret)
|
|
|
|
fputc('\n', outf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage_command(const struct cmd_struct *cmd, int full, int err)
|
|
|
|
{
|
|
|
|
usage_command_usagestr(cmd->usagestr, cmd->token, full, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage(const char * const *usagestr)
|
|
|
|
{
|
|
|
|
usage_command_usagestr(usagestr, NULL, 1, 1);
|
2013-09-04 23:22:18 +08:00
|
|
|
exit(1);
|
2012-02-04 03:00:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usage_command_group_internal(const struct cmd_group *grp, int full,
|
|
|
|
FILE *outf)
|
|
|
|
{
|
|
|
|
const struct cmd_struct *cmd = grp->commands;
|
|
|
|
int do_sep = 0;
|
|
|
|
|
|
|
|
for (; cmd->token; cmd++) {
|
2015-06-25 00:09:16 +08:00
|
|
|
if (cmd->flags & CMD_HIDDEN)
|
2012-02-04 03:00:17 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (full && cmd != grp->commands)
|
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
if (!cmd->next) {
|
|
|
|
if (do_sep) {
|
|
|
|
fputc('\n', outf);
|
|
|
|
do_sep = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
usage_command_internal(cmd->usagestr, cmd->token, full,
|
2015-06-25 00:09:17 +08:00
|
|
|
1, cmd->flags & CMD_ALIAS, outf);
|
2016-12-09 02:27:50 +08:00
|
|
|
if (cmd->flags & CMD_ALIAS)
|
|
|
|
putchar('\n');
|
2012-02-04 03:00:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is an entry point to a nested command group */
|
|
|
|
|
|
|
|
if (!full && cmd != grp->commands)
|
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
usage_command_group_internal(cmd->next, full, outf);
|
|
|
|
|
|
|
|
if (!full)
|
|
|
|
do_sep = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 00:54:04 +08:00
|
|
|
void usage_command_group_short(const struct cmd_group *grp)
|
|
|
|
{
|
|
|
|
const char * const *usagestr = grp->usagestr;
|
|
|
|
FILE *outf = stdout;
|
|
|
|
const struct cmd_struct *cmd;
|
|
|
|
|
|
|
|
if (usagestr && *usagestr) {
|
|
|
|
fprintf(outf, "usage: %s\n", *usagestr++);
|
|
|
|
while (*usagestr)
|
|
|
|
fprintf(outf, " or: %s\n", *usagestr++);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
fprintf(outf, "Command groups:\n");
|
|
|
|
for (cmd = grp->commands; cmd->token; cmd++) {
|
2015-06-25 00:09:16 +08:00
|
|
|
if (cmd->flags & CMD_HIDDEN)
|
2015-06-09 00:54:04 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!cmd->next)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fprintf(outf, " %-16s %s\n", cmd->token, cmd->next->infostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(outf, "\nCommands:\n");
|
|
|
|
for (cmd = grp->commands; cmd->token; cmd++) {
|
2015-06-25 00:09:16 +08:00
|
|
|
if (cmd->flags & CMD_HIDDEN)
|
2015-06-09 00:54:04 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cmd->next)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fprintf(outf, " %-16s %s\n", cmd->token, cmd->usagestr[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', outf);
|
|
|
|
fprintf(stderr, "For an overview of a given command use 'btrfs command --help'\n");
|
|
|
|
fprintf(stderr, "or 'btrfs [command...] --help --full' to print all available options.\n");
|
|
|
|
fprintf(stderr, "Any command name can be shortened as far as it stays unambiguous,\n");
|
|
|
|
fprintf(stderr, "however it is recommended to use full command names in scripts.\n");
|
|
|
|
fprintf(stderr, "All command groups have their manual page named 'btrfs-<group>'.\n");
|
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
void usage_command_group(const struct cmd_group *grp, int full, int err)
|
|
|
|
{
|
2012-02-08 23:45:54 +08:00
|
|
|
const char * const *usagestr = grp->usagestr;
|
2012-02-04 03:00:17 +08:00
|
|
|
FILE *outf = err ? stderr : stdout;
|
|
|
|
|
2012-02-08 23:45:54 +08:00
|
|
|
if (usagestr && *usagestr) {
|
|
|
|
fprintf(outf, "usage: %s\n", *usagestr++);
|
|
|
|
while (*usagestr)
|
|
|
|
fprintf(outf, " or: %s\n", *usagestr++);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', outf);
|
2012-02-04 03:00:17 +08:00
|
|
|
usage_command_group_internal(grp, full, outf);
|
|
|
|
fputc('\n', outf);
|
|
|
|
|
|
|
|
if (grp->infostr)
|
|
|
|
fprintf(outf, "%s\n", grp->infostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void help_unknown_token(const char *arg, const struct cmd_group *grp)
|
|
|
|
{
|
2015-06-09 05:30:21 +08:00
|
|
|
fprintf(stderr, "%s: unknown token '%s'\n", get_argv0_buf(), arg);
|
2012-02-04 03:00:17 +08:00
|
|
|
usage_command_group(grp, 0, 1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void help_ambiguous_token(const char *arg, const struct cmd_group *grp)
|
|
|
|
{
|
|
|
|
const struct cmd_struct *cmd = grp->commands;
|
|
|
|
|
2015-06-09 05:30:21 +08:00
|
|
|
fprintf(stderr, "%s: ambiguous token '%s'\n", get_argv0_buf(), arg);
|
2012-02-04 03:00:17 +08:00
|
|
|
fprintf(stderr, "\nDid you mean one of these ?\n");
|
|
|
|
|
|
|
|
for (; cmd->token; cmd++) {
|
|
|
|
if (!prefixcmp(cmd->token, arg))
|
|
|
|
fprintf(stderr, "\t%s\n", cmd->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void help_command_group(const struct cmd_group *grp, int argc, char **argv)
|
|
|
|
{
|
|
|
|
int full = 0;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
if (!strcmp(argv[1], "--full"))
|
|
|
|
full = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
usage_command_group(grp, full, 0);
|
|
|
|
}
|
2016-01-14 17:29:13 +08:00
|
|
|
|