]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
vpick-tool: use table to parse --print=
authorDavid Tardon <dtardon@redhat.com>
Fri, 26 Sep 2025 13:47:30 +0000 (15:47 +0200)
committerDavid Tardon <dtardon@redhat.com>
Thu, 2 Oct 2025 14:13:40 +0000 (16:13 +0200)
src/vpick/vpick-tool.c

index c58bcb9d3c48b30a6db2b702cff7e4bbfc80eb79..2ed0762de20388c10cd37dfbeda40465196520df 100644 (file)
 #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;