Debian Codegraph @

src/hwdb/hwdb.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "build.h"
5 #include "format-table.h"
6 #include "hwdb-util.h"
7 #include "label-util.h"
8 #include "log.h"
9 #include "main-func.h"
10 #include "options.h"
11 #include "pretty-print.h"
12 #include "verbs.h"
13
14 static const char *arg_hwdb_bin_dir = NULL;
15 static const char *arg_root = NULL;
16 static bool arg_strict = false;
17
18 VERB(verb_query, "query", "MODALIAS", 2, 2, 0,
19 "Query database and print result");
20 static int verb_query(int argc, char *argv[], uintptr_t _data, void *userdata) {
21 return hwdb_query(argv[1], arg_root);
22 }
23
24 VERB_NOARG(verb_update, "update",
25 "Update the hwdb database");
26 static int verb_update(int argc, char *argv[], uintptr_t _data, void *userdata) {
27 if (hwdb_bypass())
28 return 0;
29
30 return hwdb_update(arg_root, arg_hwdb_bin_dir, arg_strict, false);
31 }
32
33 static int help(void) {
34 _cleanup_free_ char *link = NULL;
35 _cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
36 int r;
37
38 r = terminal_urlify_man("systemd-hwdb", "8", &link);
39 if (r < 0)
40 return log_oom();
41
42 r = verbs_get_help_table(&verbs);
43 if (r < 0)
44 return r;
45
46 r = option_parser_get_help_table(&options);
47 if (r < 0)
48 return r;
49
50 (void) table_sync_column_widths(0, verbs, options);
51
52 printf("%s [OPTIONS...] COMMAND ...\n\n"
53 "%sUpdate or query the hardware database.%s\n"
54 "\n%sCommands:%s\n",
55 program_invocation_short_name,
56 ansi_highlight(),
57 ansi_normal(),
58 ansi_underline(),
59 ansi_normal());
60
61 r = table_print_or_warn(verbs);
62 if (r < 0)
63 return r;
64
65 printf("\n%sOptions:%s\n",
66 ansi_underline(),
67 ansi_normal());
68
69 r = table_print_or_warn(options);
70 if (r < 0)
71 return r;
72
73 printf("\nSee the %s for details.\n", link);
74 return 0;
75 }
76
77 VERB_COMMON_HELP_HIDDEN(help);
78
79 static int parse_argv(int argc, char *argv[], char ***ret_args) {
80 assert(argc >= 0);
81 assert(argv);
82 assert(ret_args);
83
84 OptionParser opts = { argc, argv };
85
86 FOREACH_OPTION_OR_RETURN(c, &opts)
87 switch (c) {
88
89 OPTION_COMMON_HELP:
90 return help();
91
92 OPTION_COMMON_VERSION:
93 return version();
94
95 OPTION('s', "strict", NULL,
96 "When updating, return non-zero exit value on any parsing error"):
97 arg_strict = true;
98 break;
99
100 OPTION('r', "root", "PATH", "Alternative root path in the filesystem"):
101 arg_root = opts.arg;
102 break;
103
104 OPTION_LONG("usr", NULL,
105 "Generate in " UDEVLIBEXECDIR " instead of /etc/udev"):
106 arg_hwdb_bin_dir = UDEVLIBEXECDIR;
107 break;
108 }
109
110 *ret_args = option_parser_get_args(&opts);
111 return 1;
112 }
113
114 static int run(int argc, char *argv[]) {
115 int r;
116
117 log_setup();
118
119 char **args = NULL;
120 r = parse_argv(argc, argv, &args);
121 if (r <= 0)
122 return r;
123
124 r = mac_init();
125 if (r < 0)
126 return r;
127
128 return dispatch_verb(args, NULL);
129 }
130
131 DEFINE_MAIN_FUNCTION(run);