From: David Tardon Date: Fri, 26 Sep 2025 13:47:30 +0000 (+0200) Subject: vpick-tool: use table to parse --print= X-Git-Tag: v259-rc1~386^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=292928e964d785e5bdf68b924dfdfe323f800a92;p=thirdparty%2Fsystemd.git vpick-tool: use table to parse --print= --- diff --git a/src/vpick/vpick-tool.c b/src/vpick/vpick-tool.c index c58bcb9d3c4..2ed0762de20 100644 --- a/src/vpick/vpick-tool.c +++ b/src/vpick/vpick-tool.c @@ -13,16 +13,12 @@ #include "path-util.h" #include "pretty-print.h" #include "stat-util.h" +#include "string-table.h" #include "string-util.h" #include "strv.h" #include "vpick.h" -static char *arg_filter_basename = NULL; -static char *arg_filter_version = NULL; -static Architecture arg_filter_architecture = _ARCHITECTURE_INVALID; -static char *arg_filter_suffix = NULL; -static uint32_t arg_filter_type_mask = 0; -static enum { +typedef enum { PRINT_PATH, PRINT_FILENAME, PRINT_VERSION, @@ -30,14 +26,34 @@ static enum { PRINT_ARCHITECTURE, PRINT_TRIES, PRINT_ALL, + _PRINT_MAX, _PRINT_INVALID = -EINVAL, -} arg_print = _PRINT_INVALID; +} Print; + +static char *arg_filter_basename = NULL; +static char *arg_filter_version = NULL; +static Architecture arg_filter_architecture = _ARCHITECTURE_INVALID; +static char *arg_filter_suffix = NULL; +static uint32_t arg_filter_type_mask = 0; +static Print arg_print = _PRINT_INVALID; static PickFlags arg_flags = PICK_ARCHITECTURE|PICK_TRIES; STATIC_DESTRUCTOR_REGISTER(arg_filter_basename, freep); STATIC_DESTRUCTOR_REGISTER(arg_filter_version, freep); STATIC_DESTRUCTOR_REGISTER(arg_filter_suffix, freep); +static const char *print_table[_PRINT_MAX] = { + [PRINT_PATH] = "path", + [PRINT_FILENAME] = "filename", + [PRINT_VERSION] = "version", + [PRINT_TYPE] = "type", + [PRINT_ARCHITECTURE] = "architecture", + [PRINT_TRIES] = "tries", + [PRINT_ALL] = "all", +}; + +DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(print, Print); + static int help(void) { _cleanup_free_ char *link = NULL; int r; @@ -173,22 +189,12 @@ static int parse_argv(int argc, char *argv[]) { break; case 'p': - if (streq(optarg, "path")) - arg_print = PRINT_PATH; - else if (streq(optarg, "filename")) - arg_print = PRINT_FILENAME; - else if (streq(optarg, "version")) - arg_print = PRINT_VERSION; - else if (streq(optarg, "type")) - arg_print = PRINT_TYPE; - else if (STR_IN_SET(optarg, "arch", "architecture")) + if (streq(optarg, "arch")) /* accept abbreviation too */ arg_print = PRINT_ARCHITECTURE; - else if (streq(optarg, "tries")) - arg_print = PRINT_TRIES; - else if (streq(optarg, "all")) - arg_print = PRINT_ALL; else - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown --print= argument: %s", optarg); + arg_print = print_from_string(optarg); + if (arg_print < 0) + return log_error_errno(arg_print, "Unknown --print= argument: %s", optarg); break;