]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udevadm-info: use string table to parse query type 37292/head
authorDavid Tardon <dtardon@redhat.com>
Wed, 7 May 2025 13:05:22 +0000 (15:05 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 9 May 2025 03:14:21 +0000 (12:14 +0900)
src/udev/udevadm-info.c

index f7cbfa487879d857fe0b915a8fb91acad1b9c2c9..f505680dbd70820f8190c6f8ccf8ab3c3dec32b0 100644 (file)
@@ -51,6 +51,8 @@ typedef enum QueryType {
         QUERY_SYMLINK,
         QUERY_PROPERTY,
         QUERY_ALL,
+        _QUERY_TYPE_MAX,
+        _QUERY_TYPE_INVALID = -EINVAL,
 } QueryType;
 
 static char **arg_properties = NULL;
@@ -92,6 +94,16 @@ STATIC_DESTRUCTOR_REGISTER(arg_tag_match, strv_freep);
 /* Put a limit on --tree descent level to not exhaust our stack */
 #define TREE_DEPTH_MAX 64
 
+static const char *query_type_table[_QUERY_TYPE_MAX] = {
+        [QUERY_NAME]     = "name",
+        [QUERY_PATH]     = "path",
+        [QUERY_SYMLINK]  = "symlink",
+        [QUERY_PROPERTY] = "property",
+        [QUERY_ALL]      = "all",
+};
+
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(query_type, QueryType);
+
 static bool skip_attribute(const char *name) {
         assert(name);
 
@@ -1091,19 +1103,13 @@ static int parse_argv(int argc, char *argv[]) {
                 }
 
                 case 'q':
-                        arg_action_type = ACTION_QUERY;
-                        if (streq(optarg, "property") || streq(optarg, "env"))
-                                arg_query = QUERY_PROPERTY;
-                        else if (streq(optarg, "name"))
-                                arg_query = QUERY_NAME;
-                        else if (streq(optarg, "symlink"))
-                                arg_query = QUERY_SYMLINK;
-                        else if (streq(optarg, "path"))
-                                arg_query = QUERY_PATH;
-                        else if (streq(optarg, "all"))
-                                arg_query = QUERY_ALL;
-                        else
-                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "unknown query type");
+                        arg_query = query_type_from_string(optarg);
+                        if (arg_query < 0) {
+                                if (streq(optarg, "env")) /* deprecated */
+                                        arg_query = QUERY_PROPERTY;
+                                else
+                                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "unknown query type");
+                        }
                         break;
 
                 case 'r':