From: Zbigniew Jędrzejewski-Szmek Date: Mon, 15 Feb 2021 18:32:42 +0000 (+0100) Subject: tree-wide: add a helper to parse boolean optarg X-Git-Tag: v248-rc1~68^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=599c7c545f689f7d19a73238eecc69bf94fa6a74;p=thirdparty%2Fsystemd.git tree-wide: add a helper to parse boolean optarg This nicely covers the case when optarg is optional. The same parser can be used when the option string passed to getopt_long() requires a parameter and when it doesn't. The error messages are made consistent. Also fixes a log error c&p in --crash-reboot message. --- diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index c25d11e0de3..55d32fee22a 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -37,6 +37,7 @@ #include "main-func.h" #include "nulstr-util.h" #include "pager.h" +#include "parse-argument.h" #include "parse-util.h" #include "path-util.h" #include "pretty-print.h" @@ -2346,29 +2347,15 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_MAN: - if (optarg) { - r = parse_boolean(optarg); - if (r < 0) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Failed to parse --man= argument."); - - arg_man = r; - } else - arg_man = true; - + r = parse_boolean_argument("--man", optarg, &arg_man); + if (r < 0) + return r; break; case ARG_GENERATORS: - if (optarg) { - r = parse_boolean(optarg); - if (r < 0) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Failed to parse --generators= argument."); - - arg_generators = r; - } else - arg_generators = true; - + r = parse_boolean_argument("--generators", optarg, &arg_generators); + if (r < 0) + return r; break; case ARG_ITERATIONS: diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 6a492ebd02f..99ff5bf5619 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -2473,27 +2473,22 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_EXPECT_REPLY: - r = parse_boolean(optarg); + r = parse_boolean_argument("--expect-reply=", optarg, &arg_expect_reply); if (r < 0) - return log_error_errno(r, "Failed to parse --expect-reply= parameter '%s': %m", optarg); - - arg_expect_reply = r; + return r; break; case ARG_AUTO_START: - r = parse_boolean(optarg); + r = parse_boolean_argument("--auto-start=", optarg, &arg_auto_start); if (r < 0) - return log_error_errno(r, "Failed to parse --auto-start= parameter '%s': %m", optarg); - - arg_auto_start = r; + return r; break; case ARG_ALLOW_INTERACTIVE_AUTHORIZATION: - r = parse_boolean(optarg); + r = parse_boolean_argument("--allow-interactive-authorization=", optarg, + &arg_allow_interactive_authorization); if (r < 0) - return log_error_errno(r, "Failed to parse --allow-interactive-authorization= parameter '%s': %m", optarg); - - arg_allow_interactive_authorization = r; + return r; break; case ARG_TIMEOUT: @@ -2504,19 +2499,15 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_AUGMENT_CREDS: - r = parse_boolean(optarg); + r = parse_boolean_argument("--augment-creds=", optarg, &arg_augment_creds); if (r < 0) - return log_error_errno(r, "Failed to parse --augment-creds= parameter '%s': %m", optarg); - - arg_augment_creds = r; + return r; break; case ARG_WATCH_BIND: - r = parse_boolean(optarg); + r = parse_boolean_argument("--watch-bind=", optarg, &arg_watch_bind); if (r < 0) - return log_error_errno(r, "Failed to parse --watch-bind= parameter '%s': %m", optarg); - - arg_watch_bind = r; + return r; break; case 'j': diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index d0fa69ff88d..cbae9897a50 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -19,6 +19,7 @@ #include "hashmap.h" #include "main-func.h" #include "missing_sched.h" +#include "parse-argument.h" #include "parse-util.h" #include "path-util.h" #include "pretty-print.h" @@ -867,12 +868,11 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_RECURSIVE: - r = parse_boolean(optarg); + r = parse_boolean_argument("--recursive=", optarg, &arg_recursive); if (r < 0) - return log_error_errno(r, "Failed to parse --recursive= argument '%s': %m", optarg); + return r; - arg_recursive = r; - arg_recursive_unset = r == 0; + arg_recursive_unset = !r; break; case 'M': diff --git a/src/core/main.c b/src/core/main.c index 8c7bcc2d3d9..1a385c61d4e 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -938,15 +938,9 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_DUMP_CORE: - if (!optarg) - arg_dump_core = true; - else { - r = parse_boolean(optarg); - if (r < 0) - return log_error_errno(r, "Failed to parse dump core boolean: \"%s\": %m", - optarg); - arg_dump_core = r; - } + r = parse_boolean_argument("--dump-core", optarg, &arg_dump_core); + if (r < 0) + return r; break; case ARG_CRASH_CHVT: @@ -957,27 +951,15 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_CRASH_SHELL: - if (!optarg) - arg_crash_shell = true; - else { - r = parse_boolean(optarg); - if (r < 0) - return log_error_errno(r, "Failed to parse crash shell boolean: \"%s\": %m", - optarg); - arg_crash_shell = r; - } + r = parse_boolean_argument("--crash-shell", optarg, &arg_crash_shell); + if (r < 0) + return r; break; case ARG_CRASH_REBOOT: - if (!optarg) - arg_crash_reboot = true; - else { - r = parse_boolean(optarg); - if (r < 0) - return log_error_errno(r, "Failed to parse crash shell boolean: \"%s\": %m", - optarg); - arg_crash_reboot = r; - } + r = parse_boolean_argument("--crash-reboot", optarg, &arg_crash_reboot); + if (r < 0) + return r; break; case ARG_CONFIRM_SPAWN: @@ -990,11 +972,9 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_SERVICE_WATCHDOGS: - r = parse_boolean(optarg); + r = parse_boolean_argument("--service-watchdogs=", optarg, &arg_service_watchdogs); if (r < 0) - return log_error_errno(r, "Failed to parse service watchdogs boolean: \"%s\": %m", - optarg); - arg_service_watchdogs = r; + return r; break; case ARG_SHOW_STATUS: diff --git a/src/journal/cat.c b/src/journal/cat.c index 6599e64296a..4ccc5e0a330 100644 --- a/src/journal/cat.c +++ b/src/journal/cat.c @@ -12,6 +12,7 @@ #include "alloc-util.h" #include "fd-util.h" #include "main-func.h" +#include "parse-argument.h" #include "parse-util.h" #include "pretty-print.h" #include "string-util.h" @@ -67,7 +68,7 @@ static int parse_argv(int argc, char *argv[]) { {} }; - int c; + int c, r; assert(argc >= 0); assert(argv); @@ -104,16 +105,11 @@ static int parse_argv(int argc, char *argv[]) { "Failed to parse stderr priority value."); break; - case ARG_LEVEL_PREFIX: { - int k; - - k = parse_boolean(optarg); - if (k < 0) - return log_error_errno(k, "Failed to parse level prefix value."); - - arg_level_prefix = k; + case ARG_LEVEL_PREFIX: + r = parse_boolean_argument("--level-prefix=", optarg, &arg_level_prefix); + if (r < 0) + return r; break; - } case '?': return -EINVAL; diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index 8b6fc520c5e..5a8e8b1143c 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -23,6 +23,7 @@ #include "mount-util.h" #include "mountpoint-util.h" #include "pager.h" +#include "parse-argument.h" #include "parse-util.h" #include "path-util.h" #include "pretty-print.h" @@ -265,11 +266,9 @@ static int parse_argv(int argc, char *argv[]) { } case ARG_FSCK: - r = parse_boolean(optarg); + r = parse_boolean_argument("--fsck=", optarg, &arg_fsck); if (r < 0) - return log_error_errno(r, "Failed to parse --fsck= argument: %s", optarg); - - arg_fsck = r; + return r; break; case ARG_DESCRIPTION: diff --git a/src/partition/repart.c b/src/partition/repart.c index 78c31187ea4..ac905cab531 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -44,8 +44,8 @@ #include "mkdir.h" #include "mkfs-util.h" #include "mount-util.h" -#include "parse-util.h" #include "parse-argument.h" +#include "parse-util.h" #include "path-util.h" #include "pretty-print.h" #include "proc-cmdline.h" @@ -3575,11 +3575,9 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_DRY_RUN: - r = parse_boolean(optarg); + r = parse_boolean_argument("--dry-run=", optarg, &arg_dry_run); if (r < 0) - return log_error_errno(r, "Failed to parse --dry-run= parameter: %s", optarg); - - dry_run = r; + return r; break; case ARG_EMPTY: @@ -3604,11 +3602,9 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_DISCARD: - r = parse_boolean(optarg); + r = parse_boolean_argument("--discard=", optarg, &arg_discard); if (r < 0) - return log_error_errno(r, "Failed to parse --discard= parameter: %s", optarg); - - arg_discard = r; + return r; break; case ARG_FACTORY_RESET: diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index e7ee7354287..ec572cabf08 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -23,6 +23,7 @@ #include "missing_network.h" #include "netlink-util.h" #include "pager.h" +#include "parse-argument.h" #include "parse-util.h" #include "pretty-print.h" #include "resolvconf-compat.h" @@ -2766,11 +2767,9 @@ static int compat_parse_argv(int argc, char *argv[]) { break; case ARG_LEGEND: - r = parse_boolean(optarg); + r = parse_boolean_argument("--legend=", optarg, &arg_legend); if (r < 0) - return log_error_errno(r, "Failed to parse --legend= argument"); - - arg_legend = r; + return r; break; case 'p': @@ -3062,11 +3061,9 @@ static int native_parse_argv(int argc, char *argv[]) { break; case ARG_LEGEND: - r = parse_boolean(optarg); + r = parse_boolean_argument("--legend=", optarg, &arg_legend); if (r < 0) - return log_error_errno(r, "Failed to parse --legend= argument"); - - arg_legend = r; + return r; break; case 'p': diff --git a/src/shared/parse-argument.c b/src/shared/parse-argument.c index cd1d0dde21a..89951c4b3d9 100644 --- a/src/shared/parse-argument.c +++ b/src/shared/parse-argument.c @@ -10,6 +10,25 @@ /* All functions in this file emit warnigs. */ +int parse_boolean_argument(const char *optname, const char *s, bool *ret) { + int r; + + /* Returns the result through *ret and the return value. */ + + if (s) { + r = parse_boolean(s); + if (r < 0) + return log_error_errno(r, "Failed to parse boolean argument to %s: %s.", optname, s); + + *ret = r; + return r; + } else { + /* s may be NULL. This is controlled by getopt_long() parameters. */ + *ret = true; + return true; + } +} + int parse_json_argument(const char *s, JsonFormatFlags *ret) { assert(s); assert(ret); diff --git a/src/shared/parse-argument.h b/src/shared/parse-argument.h index 28b58cc2e29..adad65e9025 100644 --- a/src/shared/parse-argument.h +++ b/src/shared/parse-argument.h @@ -3,6 +3,7 @@ #include "json.h" +int parse_boolean_argument(const char *optname, const char *s, bool *ret); int parse_json_argument(const char *s, JsonFormatFlags *ret); int parse_path_argument(const char *path, bool suppress_root, char **arg); int parse_signal_argument(const char *s, int *ret);