Merge pull request #9061 from poettering/dump-string-table

add new DUMP_STRING_TABLE() macro and make use of it everywhere
This commit is contained in:
Lennart Poettering 2018-05-22 14:28:38 +02:00 committed by GitHub
commit c0d7a4f0cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 124 additions and 70 deletions

View file

@ -230,6 +230,12 @@
technology identifier.</para></listitem> technology identifier.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--list</option></term>
<listitem><para>Output all currently known and detectable container and VM environments.</para></listitem>
</varlistentry>
<xi:include href="standard-options.xml" xpointer="help" /> <xi:include href="standard-options.xml" xpointer="help" />
<xi:include href="standard-options.xml" xpointer="version" /> <xi:include href="standard-options.xml" xpointer="version" />
</variablelist> </variablelist>

View file

@ -102,3 +102,18 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k
_DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static)
#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \ #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \
_DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static)
#define DUMP_STRING_TABLE(name,type,max) \
do { \
type _k; \
flockfile(stdout); \
for (_k = 0; _k < (max); _k++) { \
const char *_t; \
_t = name##_to_string(_k); \
if (!_t) \
continue; \
fputs_unlocked(_t, stdout); \
fputc_unlocked('\n', stdout); \
} \
funlockfile(stdout); \
} while(false)

View file

@ -217,7 +217,7 @@ static int parse_argv(int argc, char *argv[]) {
case 'o': case 'o':
if (arg_output) { if (arg_output) {
log_error("cannot set output more than once"); log_error("Cannot set output more than once.");
return -EINVAL; return -EINVAL;
} }
@ -241,7 +241,7 @@ static int parse_argv(int argc, char *argv[]) {
case 'F': case 'F':
if (arg_field) { if (arg_field) {
log_error("cannot use --field/-F more than once"); log_error("Cannot use --field/-F more than once.");
return -EINVAL; return -EINVAL;
} }
arg_field = optarg; arg_field = optarg;

View file

@ -10,6 +10,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include "string-table.h"
#include "util.h" #include "util.h"
#include "virt.h" #include "virt.h"
@ -32,6 +33,7 @@ static void help(void) {
" -r --chroot Detect whether we are run in a chroot() environment\n" " -r --chroot Detect whether we are run in a chroot() environment\n"
" --private-users Only detect whether we are running in a user namespace\n" " --private-users Only detect whether we are running in a user namespace\n"
" -q --quiet Don't output anything, just set return value\n" " -q --quiet Don't output anything, just set return value\n"
" --list List all known and detectable types of virtualization\n"
, program_invocation_short_name); , program_invocation_short_name);
} }
@ -40,6 +42,7 @@ static int parse_argv(int argc, char *argv[]) {
enum { enum {
ARG_VERSION = 0x100, ARG_VERSION = 0x100,
ARG_PRIVATE_USERS, ARG_PRIVATE_USERS,
ARG_LIST,
}; };
static const struct option options[] = { static const struct option options[] = {
@ -50,6 +53,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "chroot", no_argument, NULL, 'r' }, { "chroot", no_argument, NULL, 'r' },
{ "private-users", no_argument, NULL, ARG_PRIVATE_USERS }, { "private-users", no_argument, NULL, ARG_PRIVATE_USERS },
{ "quiet", no_argument, NULL, 'q' }, { "quiet", no_argument, NULL, 'q' },
{ "list", no_argument, NULL, ARG_LIST },
{} {}
}; };
@ -89,6 +93,10 @@ static int parse_argv(int argc, char *argv[]) {
arg_mode = ONLY_CHROOT; arg_mode = ONLY_CHROOT;
break; break;
case ARG_LIST:
DUMP_STRING_TABLE(virtualization, int, _VIRTUALIZATION_MAX);
return 0;
case '?': case '?':
return -EINVAL; return -EINVAL;

View file

@ -58,11 +58,12 @@
#include "rlimit-util.h" #include "rlimit-util.h"
#include "set.h" #include "set.h"
#include "sigbus.h" #include "sigbus.h"
#include "string-table.h"
#include "strv.h" #include "strv.h"
#include "syslog-util.h" #include "syslog-util.h"
#include "terminal-util.h" #include "terminal-util.h"
#include "udev.h"
#include "udev-util.h" #include "udev-util.h"
#include "udev.h"
#include "unit-name.h" #include "unit-name.h"
#include "user-util.h" #include "user-util.h"
@ -509,6 +510,11 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case 'o': case 'o':
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
return 0;
}
arg_output = output_mode_from_string(optarg); arg_output = output_mode_from_string(optarg);
if (arg_output < 0) { if (arg_output < 0) {
log_error("Unknown output format '%s'.", optarg); log_error("Unknown output format '%s'.", optarg);

View file

@ -29,6 +29,7 @@
#include "sigbus.h" #include "sigbus.h"
#include "signal-util.h" #include "signal-util.h"
#include "spawn-polkit-agent.h" #include "spawn-polkit-agent.h"
#include "string-table.h"
#include "strv.h" #include "strv.h"
#include "sysfs-show.h" #include "sysfs-show.h"
#include "terminal-util.h" #include "terminal-util.h"
@ -1441,6 +1442,11 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case 'o': case 'o':
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
return 0;
}
arg_output = output_mode_from_string(optarg); arg_output = output_mode_from_string(optarg);
if (arg_output < 0) { if (arg_output < 0) {
log_error("Unknown output '%s'.", optarg); log_error("Unknown output '%s'.", optarg);
@ -1465,6 +1471,11 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case 's': case 's':
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(signal, int, _NSIG);
return 0;
}
arg_signal = signal_from_string(optarg); arg_signal = signal_from_string(optarg);
if (arg_signal < 0) { if (arg_signal < 0) {
log_error("Failed to parse signal string %s.", optarg); log_error("Failed to parse signal string %s.", optarg);

View file

@ -46,6 +46,7 @@
#include "signal-util.h" #include "signal-util.h"
#include "spawn-polkit-agent.h" #include "spawn-polkit-agent.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "string-table.h"
#include "strv.h" #include "strv.h"
#include "terminal-util.h" #include "terminal-util.h"
#include "unit-name.h" #include "unit-name.h"
@ -2859,6 +2860,11 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case 'o': case 'o':
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
return 0;
}
arg_output = output_mode_from_string(optarg); arg_output = output_mode_from_string(optarg);
if (arg_output < 0) { if (arg_output < 0) {
log_error("Unknown output '%s'.", optarg); log_error("Unknown output '%s'.", optarg);
@ -2879,6 +2885,11 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case 's': case 's':
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(signal, int, _NSIG);
return 0;
}
arg_signal = signal_from_string(optarg); arg_signal = signal_from_string(optarg);
if (arg_signal < 0) { if (arg_signal < 0) {
log_error("Failed to parse signal string %s.", optarg); log_error("Failed to parse signal string %s.", optarg);
@ -2913,6 +2924,11 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case ARG_VERIFY: case ARG_VERIFY:
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(import_verify, ImportVerify, _IMPORT_VERIFY_MAX);
return 0;
}
arg_verify = import_verify_from_string(optarg); arg_verify = import_verify_from_string(optarg);
if (arg_verify < 0) { if (arg_verify < 0) {
log_error("Failed to parse --verify= setting: %s", optarg); log_error("Failed to parse --verify= setting: %s", optarg);

View file

@ -90,6 +90,7 @@
#include "socket-util.h" #include "socket-util.h"
#include "stat-util.h" #include "stat-util.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "string-table.h"
#include "string-util.h" #include "string-util.h"
#include "strv.h" #include "strv.h"
#include "terminal-util.h" #include "terminal-util.h"
@ -916,7 +917,10 @@ static int parse_argv(int argc, char *argv[]) {
if (!optarg) if (!optarg)
arg_volatile_mode = VOLATILE_YES; arg_volatile_mode = VOLATILE_YES;
else { else if (streq(optarg, "help")) {
DUMP_STRING_TABLE(volatile_mode, VolatileMode, _VOLATILE_MODE_MAX);
return 0;
} else {
VolatileMode m; VolatileMode m;
m = volatile_mode_from_string(optarg); m = volatile_mode_from_string(optarg);
@ -1024,6 +1028,11 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case ARG_KILL_SIGNAL: case ARG_KILL_SIGNAL:
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(signal, int, _NSIG);
return 0;
}
arg_kill_signal = signal_from_string(optarg); arg_kill_signal = signal_from_string(optarg);
if (arg_kill_signal < 0) { if (arg_kill_signal < 0) {
log_error("Cannot parse signal: %s", optarg); log_error("Cannot parse signal: %s", optarg);
@ -1153,6 +1162,11 @@ static int parse_argv(int argc, char *argv[]) {
char *name; char *name;
int rl; int rl;
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(rlimit, int, _RLIMIT_MAX);
return 0;
}
eq = strchr(optarg, '='); eq = strchr(optarg, '=');
if (!eq) { if (!eq) {
log_error("--rlimit= expects an '=' assignment."); log_error("--rlimit= expects an '=' assignment.");

View file

@ -27,6 +27,7 @@
#include "resolvectl.h" #include "resolvectl.h"
#include "resolved-def.h" #include "resolved-def.h"
#include "resolved-dns-packet.h" #include "resolved-dns-packet.h"
#include "string-table.h"
#include "strv.h" #include "strv.h"
#include "terminal-util.h" #include "terminal-util.h"
#include "verbs.h" #include "verbs.h"
@ -2232,29 +2233,17 @@ static void help_protocol_types(void) {
} }
static void help_dns_types(void) { static void help_dns_types(void) {
const char *t;
int i;
if (arg_legend) if (arg_legend)
puts("Known DNS RR types:"); puts("Known DNS RR types:");
for (i = 0; i < _DNS_TYPE_MAX; i++) {
t = dns_type_to_string(i); DUMP_STRING_TABLE(dns_type, int, _DNS_TYPE_MAX);
if (t)
puts(t);
}
} }
static void help_dns_classes(void) { static void help_dns_classes(void) {
const char *t;
int i;
if (arg_legend) if (arg_legend)
puts("Known DNS RR classes:"); puts("Known DNS RR classes:");
for (i = 0; i < _DNS_CLASS_MAX; i++) {
t = dns_class_to_string(i); DUMP_STRING_TABLE(dns_class, int, _DNS_CLASS_MAX);
if (t)
puts(t);
}
} }
static void compat_help(void) { static void compat_help(void) {

View file

@ -11,27 +11,10 @@
#include "macro.h" #include "macro.h"
#include "parse-util.h" #include "parse-util.h"
#include "proc-cmdline.h" #include "proc-cmdline.h"
#include "string-table.h"
#include "string-util.h" #include "string-util.h"
#include "volatile-util.h" #include "volatile-util.h"
VolatileMode volatile_mode_from_string(const char *s) {
int b;
if (isempty(s))
return _VOLATILE_MODE_INVALID;
b = parse_boolean(s);
if (b > 0)
return VOLATILE_YES;
if (b == 0)
return VOLATILE_NO;
if (streq(s, "state"))
return VOLATILE_STATE;
return _VOLATILE_MODE_INVALID;
}
int query_volatile_mode(VolatileMode *ret) { int query_volatile_mode(VolatileMode *ret) {
_cleanup_free_ char *mode = NULL; _cleanup_free_ char *mode = NULL;
VolatileMode m = VOLATILE_NO; VolatileMode m = VOLATILE_NO;
@ -56,3 +39,11 @@ finish:
*ret = m; *ret = m;
return r; return r;
} }
static const char* const volatile_mode_table[_VOLATILE_MODE_MAX] = {
[VOLATILE_NO] = "no",
[VOLATILE_YES] = "yes",
[VOLATILE_STATE] = "state",
};
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(volatile_mode, VolatileMode, VOLATILE_YES);

View file

@ -16,5 +16,6 @@ typedef enum VolatileMode {
} VolatileMode; } VolatileMode;
VolatileMode volatile_mode_from_string(const char *s); VolatileMode volatile_mode_from_string(const char *s);
const char* volatile_mode_to_string(VolatileMode m);
int query_volatile_mode(VolatileMode *ret); int query_volatile_mode(VolatileMode *ret);

View file

@ -7199,81 +7199,64 @@ static void runlevel_help(void) {
} }
static void help_types(void) { static void help_types(void) {
int i;
if (!arg_no_legend) if (!arg_no_legend)
puts("Available unit types:"); puts("Available unit types:");
for (i = 0; i < _UNIT_TYPE_MAX; i++)
puts(unit_type_to_string(i)); DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX);
} }
static void help_states(void) { static void help_states(void) {
int i;
if (!arg_no_legend) if (!arg_no_legend)
puts("Available unit load states:"); puts("Available unit load states:");
for (i = 0; i < _UNIT_LOAD_STATE_MAX; i++) DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX);
puts(unit_load_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable unit active states:"); puts("\nAvailable unit active states:");
for (i = 0; i < _UNIT_ACTIVE_STATE_MAX; i++) DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX);
puts(unit_active_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable automount unit substates:"); puts("\nAvailable automount unit substates:");
for (i = 0; i < _AUTOMOUNT_STATE_MAX; i++) DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX);
puts(automount_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable device unit substates:"); puts("\nAvailable device unit substates:");
for (i = 0; i < _DEVICE_STATE_MAX; i++) DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX);
puts(device_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable mount unit substates:"); puts("\nAvailable mount unit substates:");
for (i = 0; i < _MOUNT_STATE_MAX; i++) DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX);
puts(mount_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable path unit substates:"); puts("\nAvailable path unit substates:");
for (i = 0; i < _PATH_STATE_MAX; i++) DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX);
puts(path_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable scope unit substates:"); puts("\nAvailable scope unit substates:");
for (i = 0; i < _SCOPE_STATE_MAX; i++) DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX);
puts(scope_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable service unit substates:"); puts("\nAvailable service unit substates:");
for (i = 0; i < _SERVICE_STATE_MAX; i++) DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX);
puts(service_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable slice unit substates:"); puts("\nAvailable slice unit substates:");
for (i = 0; i < _SLICE_STATE_MAX; i++) DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX);
puts(slice_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable socket unit substates:"); puts("\nAvailable socket unit substates:");
for (i = 0; i < _SOCKET_STATE_MAX; i++) DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX);
puts(socket_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable swap unit substates:"); puts("\nAvailable swap unit substates:");
for (i = 0; i < _SWAP_STATE_MAX; i++) DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX);
puts(swap_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable target unit substates:"); puts("\nAvailable target unit substates:");
for (i = 0; i < _TARGET_STATE_MAX; i++) DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX);
puts(target_state_to_string(i));
if (!arg_no_legend) if (!arg_no_legend)
puts("\nAvailable timer unit substates:"); puts("\nAvailable timer unit substates:");
for (i = 0; i < _TIMER_STATE_MAX; i++) DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX);
puts(timer_state_to_string(i));
} }
static int systemctl_parse_argv(int argc, char *argv[]) { static int systemctl_parse_argv(int argc, char *argv[]) {
@ -7569,6 +7552,11 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
break; break;
case 's': case 's':
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(signal, int, _NSIG);
return 0;
}
arg_signal = signal_from_string(optarg); arg_signal = signal_from_string(optarg);
if (arg_signal < 0) { if (arg_signal < 0) {
log_error("Failed to parse signal string %s.", optarg); log_error("Failed to parse signal string %s.", optarg);
@ -7602,6 +7590,11 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
break; break;
case 'o': case 'o':
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
return 0;
}
arg_output = output_mode_from_string(optarg); arg_output = output_mode_from_string(optarg);
if (arg_output < 0) { if (arg_output < 0) {
log_error("Unknown output '%s'.", optarg); log_error("Unknown output '%s'.", optarg);
@ -7659,6 +7652,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
break; break;
case ARG_PRESET_MODE: case ARG_PRESET_MODE:
if (streq(optarg, "help")) {
DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX);
return 0;
}
arg_preset_mode = unit_file_preset_mode_from_string(optarg); arg_preset_mode = unit_file_preset_mode_from_string(optarg);
if (arg_preset_mode < 0) { if (arg_preset_mode < 0) {