diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-05-22 12:06:54 +0200 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2018-08-24 16:47:08 +0200 |
commit | b670ce104933b9aa240eb06089dae9962cf938e3 (patch) | |
tree | d9d424cf32256e4a8859de4be82280144ea17213 /src/basic | |
parent | f313a958abd17a56a09ae76dff59c75dcfdf233a (diff) |
string-table: add new DUMP_STRING_TABLE() macro
The macro is inspired by the other string table macros, and takes the
same arguments in the same order and dumps a string table to stdout.
Since it's typesafe it's nice to implement this as macro rather than
regular function.
This new macro is useful for implementing commands such as "systemctl -t
help" and similar, i.e. wherever we want to dump all values of an enum
to stdout.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/string-table.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/basic/string-table.h b/src/basic/string-table.h index a39206fb9..92825c4d7 100644 --- a/src/basic/string-table.h +++ b/src/basic/string-table.h @@ -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 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \ _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) |