Final batch of conversions to option and verb macros (#42127)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include "memory-util.h"
|
||||
#include "shared-forward.h"
|
||||
|
||||
/* Option namespace/group explanation:
|
||||
@@ -240,10 +239,10 @@ int option_parse(
|
||||
|
||||
/* Iterate over options. Don't forget to handle errors (negative c)! */
|
||||
#define FOREACH_OPTION(c, state) \
|
||||
for (int c; (c = option_parse(ALIGN_PTR(__start_SYSTEMD_OPTIONS), __stop_SYSTEMD_OPTIONS, state)) != 0; )
|
||||
for (int c; (c = option_parse(__start_SYSTEMD_OPTIONS, __stop_SYSTEMD_OPTIONS, state)) != 0; )
|
||||
|
||||
#define FOREACH_OPTION_OR_RETURN(c, state) \
|
||||
for (int c; (c = option_parse(ALIGN_PTR(__start_SYSTEMD_OPTIONS), __stop_SYSTEMD_OPTIONS, state)) != 0; ) \
|
||||
for (int c; (c = option_parse(__start_SYSTEMD_OPTIONS, __stop_SYSTEMD_OPTIONS, state)) != 0; ) \
|
||||
if (c < 0) \
|
||||
return c; \
|
||||
else
|
||||
@@ -268,7 +267,7 @@ int _option_parser_get_help_table_full(
|
||||
const char *group,
|
||||
Table **ret);
|
||||
#define option_parser_get_help_table_full(namespace, group, ret) \
|
||||
_option_parser_get_help_table_full(ALIGN_PTR(__start_SYSTEMD_OPTIONS), __stop_SYSTEMD_OPTIONS, namespace, group, ret)
|
||||
_option_parser_get_help_table_full(__start_SYSTEMD_OPTIONS, __stop_SYSTEMD_OPTIONS, namespace, group, ret)
|
||||
#define option_parser_get_help_table_ns(ns, ret) \
|
||||
option_parser_get_help_table_full(ns, /* group= */ NULL, ret)
|
||||
#define option_parser_get_help_table_group(group, ret) \
|
||||
|
||||
@@ -31,7 +31,7 @@ int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
|
||||
*ret = r;
|
||||
return r;
|
||||
} else {
|
||||
/* s may be NULL. This is controlled by getopt_long() parameters. */
|
||||
/* s may be NULL. This is controlled by OPTION flags. */
|
||||
if (ret)
|
||||
*ret = true;
|
||||
return true;
|
||||
@@ -42,10 +42,10 @@ int parse_tristate_argument_with_auto(const char *optname, const char *s, int *r
|
||||
int r;
|
||||
|
||||
assert(optname);
|
||||
assert(s); /* We refuse NULL optarg here, since that would be ambiguous on cmdline:
|
||||
for --enable-a[=BOOL], --enable-a is intuitively interpreted as true rather than "auto"
|
||||
(parse_boolean_argument() does exactly that). IOW, tristate options should require
|
||||
arguments. */
|
||||
assert(s); /* We refuse NULL arg here, since that would be ambiguous on cmdline: for
|
||||
* --enable-a[=BOOL], --enable-a is intuitively interpreted as true rather than "auto"
|
||||
* (parse_boolean_argument() does exactly that). IOW, tristate options should require
|
||||
* arguments. */
|
||||
|
||||
r = parse_tristate_full(s, "auto", ret);
|
||||
if (r < 0)
|
||||
|
||||
+2
-4
@@ -380,20 +380,18 @@ int run_test_table(const TestFunc *start, const TestFunc *end) {
|
||||
_cleanup_strv_free_ char **tests = NULL;
|
||||
int r = EXIT_SUCCESS;
|
||||
bool ran = false;
|
||||
const char *e;
|
||||
|
||||
if (!start)
|
||||
return r;
|
||||
|
||||
e = getenv("TESTFUNCS");
|
||||
const char *e = getenv("TESTFUNCS");
|
||||
if (e) {
|
||||
r = strv_split_full(&tests, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse $TESTFUNCS: %m");
|
||||
}
|
||||
|
||||
for (const TestFunc *t = ALIGN_PTR(start); t + 1 <= end; t = ALIGN_PTR(t + 1)) {
|
||||
|
||||
for (const TestFunc *t = start; t + 1 <= end; t++) {
|
||||
if (tests && !strv_contains(tests, t->name))
|
||||
continue;
|
||||
|
||||
|
||||
+8
-18
@@ -1,7 +1,5 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <getopt.h>
|
||||
|
||||
#include "env-util.h"
|
||||
#include "format-table.h"
|
||||
#include "log.h"
|
||||
@@ -66,6 +64,9 @@ static bool verb_is_metadata(const Verb *verb) {
|
||||
|
||||
const Verb* verbs_find_verb(const char *name, const Verb verbs[], const Verb verbs_end[]) {
|
||||
assert(verbs);
|
||||
assert(verbs_end > verbs);
|
||||
assert((uintptr_t) verbs % sizeof(void*) == 0);
|
||||
assert(verbs[0].verb);
|
||||
|
||||
for (const Verb *verb = verbs; verb < verbs_end; verb++) {
|
||||
if (verb_is_metadata(verb))
|
||||
@@ -79,11 +80,12 @@ const Verb* verbs_find_verb(const char *name, const Verb verbs[], const Verb ver
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int _dispatch_verb_with_args(char **args, const Verb verbs[], const Verb verbs_end[], void *userdata) {
|
||||
int _dispatch_verb(char **args, const Verb verbs[], const Verb verbs_end[], void *userdata) {
|
||||
int r;
|
||||
|
||||
assert(verbs);
|
||||
assert(verbs_end > verbs);
|
||||
assert((uintptr_t) verbs % sizeof(void*) == 0);
|
||||
assert(verbs[0].verb);
|
||||
|
||||
const char *name = args ? args[0] : NULL;
|
||||
@@ -146,21 +148,6 @@ int _dispatch_verb_with_args(char **args, const Verb verbs[], const Verb verbs_e
|
||||
return verb->dispatch(left, args, verb->data, userdata);
|
||||
}
|
||||
|
||||
int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
|
||||
/* getopt wrapper for _dispatch_verb_with_args.
|
||||
* TBD: remove this function when all programs with verbs have been converted. */
|
||||
|
||||
assert(argc >= 0);
|
||||
assert(argv);
|
||||
assert(argc >= optind);
|
||||
|
||||
size_t n = 0;
|
||||
while (verbs[n].verb)
|
||||
n++;
|
||||
|
||||
return _dispatch_verb_with_args(strv_skip(argv, optind), verbs, verbs + n, userdata);
|
||||
}
|
||||
|
||||
#define VERB_SYNOPSIS_WIDTH_SANE 25
|
||||
|
||||
static const char* find_point_to_break(const char *s, size_t max_width) {
|
||||
@@ -234,6 +221,9 @@ int _verbs_get_help_table(
|
||||
Table **ret) {
|
||||
int r;
|
||||
|
||||
assert(verbs);
|
||||
assert(verbs_end > verbs);
|
||||
assert((uintptr_t) verbs % sizeof(void*) == 0);
|
||||
assert(ret);
|
||||
|
||||
_cleanup_(table_unrefp) Table *table = table_new("verb", "help");
|
||||
|
||||
+4
-6
@@ -79,11 +79,9 @@ bool should_bypass(const char *env_prefix);
|
||||
|
||||
const Verb* verbs_find_verb(const char *name, const Verb verbs[], const Verb verbs_end[]);
|
||||
|
||||
int _dispatch_verb_with_args(char **args, const Verb verbs[], const Verb verbs_end[], void *userdata);
|
||||
#define dispatch_verb_with_args(args, userdata) \
|
||||
_dispatch_verb_with_args(args, ALIGN_PTR(__start_SYSTEMD_VERBS), __stop_SYSTEMD_VERBS, userdata)
|
||||
|
||||
int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata);
|
||||
int _dispatch_verb(char **args, const Verb verbs[], const Verb verbs_end[], void *userdata);
|
||||
#define dispatch_verb(args, userdata) \
|
||||
_dispatch_verb(args, __start_SYSTEMD_VERBS, __stop_SYSTEMD_VERBS, userdata)
|
||||
|
||||
int _verbs_get_help_table(
|
||||
const Verb verbs[],
|
||||
@@ -91,7 +89,7 @@ int _verbs_get_help_table(
|
||||
const char *group,
|
||||
Table **ret);
|
||||
#define verbs_get_help_table_group(group, ret) \
|
||||
_verbs_get_help_table(ALIGN_PTR(__start_SYSTEMD_VERBS), __stop_SYSTEMD_VERBS, group, ret)
|
||||
_verbs_get_help_table(__start_SYSTEMD_VERBS, __stop_SYSTEMD_VERBS, group, ret)
|
||||
#define verbs_get_help_table(ret) \
|
||||
verbs_get_help_table_group(/* group= */ NULL, ret)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user