]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-hwdb.c
udevadm: show only version number for '--version' option
[thirdparty/systemd.git] / src / udev / udevadm-hwdb.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
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
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("Unknown option");
75 }
76
77 return 1;
78 }
79
80 int hwdb_main(int argc, char *argv[], void *userdata) {
81 int r;
82
83 log_set_max_level_realm(LOG_REALM_SYSTEMD, log_get_max_level());
84
85 r = parse_argv(argc, argv);
86 if (r <= 0)
87 return r;
88
89 if (!arg_update && !arg_test) {
90 log_error("Either --update or --test must be used.");
91 return -EINVAL;
92 }
93
94 if (arg_update) {
95 r = hwdb_update(arg_root, arg_hwdb_bin_dir, arg_strict, true);
96 if (r < 0)
97 return r;
98 }
99
100 if (arg_test)
101 return hwdb_query(arg_test);
102
103 return 0;
104 }