1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "hwdb-util.h"
6 #include "udevadm.h"
7 #include "util.h"
8
9 static const char *arg_test = NULL;
10 static const char *arg_root = NULL;
11 static const char *arg_hwdb_bin_dir = NULL;
12 static bool arg_update = false;
13 static bool arg_strict = false;
14
help(void)15 static int help(void) {
16 printf("%s hwdb [OPTIONS]\n\n"
17 " -h --help Print this message\n"
18 " -V --version Print version of the program\n"
19 " -u --update Update the hardware database\n"
20 " -s --strict When updating, return non-zero exit value on any parsing error\n"
21 " --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
22 " -t --test=MODALIAS Query database and print result\n"
23 " -r --root=PATH Alternative root path in the filesystem\n\n"
24 "NOTE:\n"
25 "The sub-command 'hwdb' is deprecated, and is left for backwards compatibility.\n"
26 "Please use systemd-hwdb instead.\n",
27 program_invocation_short_name);
28
29 return 0;
30 }
31
parse_argv(int argc,char * argv[])32 static int parse_argv(int argc, char *argv[]) {
33 enum {
34 ARG_USR = 0x100,
35 };
36
37 static const struct option options[] = {
38 { "update", no_argument, NULL, 'u' },
39 { "usr", no_argument, NULL, ARG_USR },
40 { "strict", no_argument, NULL, 's' },
41 { "test", required_argument, NULL, 't' },
42 { "root", required_argument, NULL, 'r' },
43 { "version", no_argument, NULL, 'V' },
44 { "help", no_argument, NULL, 'h' },
45 {}
46 };
47
48 int c;
49
50 while ((c = getopt_long(argc, argv, "ust:r:Vh", options, NULL)) >= 0)
51 switch (c) {
52 case 'u':
53 arg_update = true;
54 break;
55 case ARG_USR:
56 arg_hwdb_bin_dir = UDEVLIBEXECDIR;
57 break;
58 case 's':
59 arg_strict = true;
60 break;
61 case 't':
62 arg_test = optarg;
63 break;
64 case 'r':
65 arg_root = optarg;
66 break;
67 case 'V':
68 return print_version();
69 case 'h':
70 return help();
71 case '?':
72 return -EINVAL;
73 default:
74 assert_not_reached();
75 }
76
77 return 1;
78 }
79
hwdb_main(int argc,char * argv[],void * userdata)80 int hwdb_main(int argc, char *argv[], void *userdata) {
81 int r;
82
83 r = parse_argv(argc, argv);
84 if (r <= 0)
85 return r;
86
87 if (!arg_update && !arg_test)
88 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
89 "Either --update or --test must be used.");
90
91 if (arg_update) {
92 r = hwdb_update(arg_root, arg_hwdb_bin_dir, arg_strict, true);
93 if (r < 0)
94 return r;
95 }
96
97 if (arg_test)
98 return hwdb_query(arg_test);
99
100 return 0;
101 }
102