]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev-builtin-hwdb: convert to OPTION macros
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Tue, 28 Apr 2026 11:24:40 +0000 (13:24 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Wed, 29 Apr 2026 13:28:05 +0000 (15:28 +0200)
Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
src/udev/udev-builtin-hwdb.c

index 082af2e6031bd75d89eddd5320cb16823becc6d6..4817c3af24e209a35537677077f2b13a831c5dc2 100644 (file)
@@ -1,7 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <fnmatch.h>
-#include <getopt.h>
 #include <stdio.h>
 
 #include "sd-hwdb.h"
@@ -9,6 +8,7 @@
 #include "alloc-util.h"
 #include "device-util.h"
 #include "hwdb-util.h"
+#include "options.h"
 #include "parse-util.h"
 #include "string-util.h"
 #include "udev-builtin.h"
@@ -128,13 +128,6 @@ next:
 }
 
 static int builtin_hwdb(UdevEvent *event, int argc, char *argv[]) {
-        static const struct option options[] = {
-                { "filter", required_argument, NULL, 'f' },
-                { "device", required_argument, NULL, 'd' },
-                { "subsystem", required_argument, NULL, 's' },
-                { "lookup-prefix", required_argument, NULL, 'p' },
-                {}
-        };
         const char *filter = NULL, *device = NULL, *subsystem = NULL, *prefix = NULL;
         _cleanup_(sd_device_unrefp) sd_device *srcdev = NULL;
         sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
@@ -143,35 +136,34 @@ static int builtin_hwdb(UdevEvent *event, int argc, char *argv[]) {
         if (!hwdb)
                 return -EINVAL;
 
-        for (;;) {
-                int option;
+        OptionParser opts = { argc, argv, .namespace = "udev-builtin-hwdb" };
 
-                option = getopt_long(argc, argv, "f:d:s:p:", options, NULL);
-                if (option == -1)
-                        break;
+        FOREACH_OPTION(c, &opts, /* on_error= */ return c)
+                switch (c) {
+
+                OPTION_NAMESPACE("udev-builtin-hwdb"): {}
 
-                switch (option) {
-                case 'f':
-                        filter = optarg;
+                OPTION('f', "filter", "FILTER", NULL):
+                        filter = opts.arg;
                         break;
 
-                case 'd':
-                        device = optarg;
+                OPTION('d', "device", "DEVICE", NULL):
+                        device = opts.arg;
                         break;
 
-                case 's':
-                        subsystem = optarg;
+                OPTION('s', "subsystem", "SUBSYSTEM", NULL):
+                        subsystem = opts.arg;
                         break;
 
-                case 'p':
-                        prefix = optarg;
+                OPTION('p', "lookup-prefix", "PREFIX", NULL):
+                        prefix = opts.arg;
                         break;
                 }
-        }
 
         /* query a specific key given as argument */
-        if (argv[optind]) {
-                r = udev_builtin_hwdb_lookup(event, prefix, argv[optind], filter);
+        char *modalias = option_parser_get_arg(&opts, 0);
+        if (modalias) {
+                r = udev_builtin_hwdb_lookup(event, prefix, modalias, filter);
                 if (r < 0)
                         return log_device_debug_errno(dev, r, "Failed to look up hwdb: %m");
                 if (r == 0)