From 3215d510887ef179080b86758b9296f35078a6c9 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 6 Apr 2022 12:14:12 +0200 Subject: [PATCH] blkpr: cleanup --help output and man page Signed-off-by: Karel Zak --- sys-utils/blkpr.8.adoc | 10 ++++--- sys-utils/blkpr.c | 65 +++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/sys-utils/blkpr.8.adoc b/sys-utils/blkpr.8.adoc index dc8bb38932..482bdec188 100644 --- a/sys-utils/blkpr.8.adoc +++ b/sys-utils/blkpr.8.adoc @@ -23,7 +23,8 @@ The _device_ argument is the pathname of the block device. == OPTIONS *-o*, *--operation* _operation_:: -The operation of persistent reservations, supported [register|reserve|release|preempt|preempt-abort|clear]. +The operation of persistent reservations, supported operations are *register*, *reserve*, *release*, *preempt*, +*preempt-abort*, and *clear*. *-k*, *--key* _key_:: The key the operation should operate on. @@ -32,10 +33,11 @@ The key the operation should operate on. The old key the operation should operate on. *-f*, *--flag* _flag_:: -Supported flag [ignore-key]. +Supported flag is *ignore-key*. -*-t*, *--type*:: -Supported type [write-exclusive|exclusive-access|write-exclusive-reg-only|exclusive-access-reg-only|write-exclusive-all-regs|exclusive-access-all-regs] +*-t*, *--type* _type_:: +Supported types are *write-exclusive*, *exclusive-access*, *write-exclusive-reg-only*, +*exclusive-access-reg-only*, *write-exclusive-all-regs*, and *exclusive-access-all-regs*. *-V*, *--version*:: Display version information and exit. diff --git a/sys-utils/blkpr.c b/sys-utils/blkpr.c index 3f8c19343f..02e5037fec 100644 --- a/sys-utils/blkpr.c +++ b/sys-utils/blkpr.c @@ -19,8 +19,6 @@ * This program uses IOC_PR_XXX ioctl to do persistent reservations * operation on a block device if the device supports it. */ - - #include #include #include @@ -64,31 +62,18 @@ static struct type_string pr_flag[] = { {PR_FL_IGNORE_KEY, "ignore-key"} }; -static char *all_type_string(struct type_string *ts, int nmem) +static void print_type(FILE *out, struct type_string *ts, size_t nmem) { - char *prtypes, *tmp; - size_t total = 0, length; - int i; - - for (i = 0; i < nmem; i++) { - total += (strlen(ts[i].str) + 1); - } + size_t i; - tmp = prtypes = xmalloc(total); for (i = 0; i < nmem; i++) { - strcpy(tmp, ts[i].str); - length = strlen(ts[i].str); - - tmp[length++] = '|'; - tmp += length; + fprintf(out, "%s", ts[i].str); + fputs(i + 2 < nmem ? ", " : + i + 1 < nmem ? _(", and ") : "\n", out); } - - /* strip the last '|' */ - prtypes[total - 1] = '\0'; - - return prtypes; } + static int parse_type_by_str(struct type_string *ts, int nmem, char *pattern) { int i; @@ -102,17 +87,18 @@ static int parse_type_by_str(struct type_string *ts, int nmem, char *pattern) return -1; } -#define SUPPORTED(XX) \ - static char *supported_##XX(void) \ - { return all_type_string(XX, ARRAY_SIZE(XX)); } + +#define PRINT_SUPPORTED(XX) \ + static void print_##XX(FILE *out) \ + { print_type(out, XX, ARRAY_SIZE(XX)); } #define PARSE(XX) \ static int parse_##XX(char *pattern) \ { return parse_type_by_str(XX, ARRAY_SIZE(XX), pattern); } -SUPPORTED(pr_type); -SUPPORTED(pr_operation); -SUPPORTED(pr_flag); +PRINT_SUPPORTED(pr_type); +PRINT_SUPPORTED(pr_operation); +PRINT_SUPPORTED(pr_flag); PARSE(pr_type); PARSE(pr_operation); @@ -176,6 +162,7 @@ static int do_pr(char *path, uint64_t key, uint64_t oldkey, int op, int type, in static void __attribute__((__noreturn__)) usage(void) { FILE *out = stdout; + fputs(USAGE_HEADER, out); fprintf(out, _(" %s [options] \n"), program_invocation_short_name); @@ -184,17 +171,29 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_("Persistent reservations on a device.\n"), out); fputs(USAGE_OPTIONS, out); - fprintf(out, _(" -o, --operation supported operation [%s]\n"), supported_pr_operation()); - fputs(_(" -k, --key key to operate\n"), out); - fputs(_(" -K, --oldkey old key to operate\n"), out); - fprintf(out, _(" -f, --flag supported flag [%s]\n"), supported_pr_flag()); - fprintf(out, _(" -t, --type supported type [%s]\n"), supported_pr_type()); + fputs(_(" -o, --operation operation of persistent reservations\n"), out); + fputs(_(" -k, --key key to operate\n"), out); + fputs(_(" -K, --oldkey old key to operate\n"), out); + fputs(_(" -f, --flag operation flag\n"), out); + fputs(_(" -t, --type operation type\n"), out); fputs(USAGE_SEPARATOR, out); - printf(USAGE_HELP_OPTIONS(21)); + printf(USAGE_HELP_OPTIONS(26)); fputs(USAGE_ARGUMENTS, out); + fputs(_(" is an operation, available operations:\n"), out); + fputs(" ", out); + print_pr_operation(out); + + fputs(_(" is an operation flag, available flags:\n"), out); + fputs(" ", out); + print_pr_flag(out); + + fputs(_(" is an operation type, available types:\n"), out); + fputs(" ", out); + print_pr_type(out); + printf(USAGE_MAN_TAIL("blkpr(8)")); exit(EXIT_SUCCESS); } -- 2.47.2