Debian Codegraph @

src/shared/parse-argument.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "ansi-color.h"
5 #include "bus-util.h"
6 #include "format-table.h"
7 #include "hostname-util.h"
8 #include "log.h"
9 #include "parse-argument.h"
10 #include "parse-util.h"
11 #include "path-util.h"
12 #include "signal-util.h"
13 #include "string-table.h"
14 #include "string-util.h"
15
16 /* All functions in this file emit warnings. */
17
18 int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
19 int r;
20
21 /* Returns the result through *ret and the return value. */
22
23 assert(optname);
24
25 if (s) {
26 r = parse_boolean(s);
27 if (r < 0)
28 return log_error_errno(r, "Failed to parse boolean argument to '%s': %s", optname, s);
29
30 if (ret)
31 *ret = r;
32 return r;
33 } else {
34 /* s may be NULL. This is controlled by OPTION flags. */
35 if (ret)
36 *ret = true;
37 return true;
38 }
39 }
40
41 int parse_tristate_argument_with_auto(const char *optname, const char *s, int *ret) {
42 int r;
43
44 assert(optname);
45 assert(s); /* We refuse NULL arg here, since that would be ambiguous on cmdline: for
46 * --enable-a[=BOOL], --enable-a is intuitively interpreted as true rather than "auto"
47 * (parse_boolean_argument() does exactly that). IOW, tristate options should require
48 * arguments. */
49
50 r = parse_tristate_full(s, "auto", ret);
51 if (r < 0)
52 return log_error_errno(r, "Failed to parse tristate argument to '%s': %s", optname, s);
53
54 return 0;
55 }
56
57 int parse_json_argument(const char *s, sd_json_format_flags_t *ret) {
58 assert(s);
59 assert(ret);
60
61 if (streq(s, "pretty"))
62 *ret = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
63 else if (streq(s, "short"))
64 *ret = SD_JSON_FORMAT_NEWLINE;
65 else if (streq(s, "off"))
66 *ret = SD_JSON_FORMAT_OFF;
67 else if (streq(s, "help")) {
68 puts("pretty\n"
69 "short\n"
70 "off");
71 return 0; /* 0 means → we showed a brief help, exit now */
72 } else
73 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown argument to --json= switch: %s", s);
74
75 return 1; /* 1 means → properly parsed */
76 }
77
78 int parse_path_argument(const char *path, bool suppress_root, char **arg) {
79 char *p;
80 int r;
81
82 assert(arg);
83
84 /*
85 * This function is intended to be used in command line parsers, to handle paths that are passed
86 * in. It makes the path absolute, and reduces it to NULL if omitted or root (the latter optionally).
87 *
88 * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON SUCCESS!
89 * Hence, do not pass in uninitialized pointers.
90 */
91
92 if (isempty(path)) {
93 *arg = mfree(*arg);
94 return 0;
95 }
96
97 r = path_make_absolute_cwd(path, &p);
98 if (r < 0)
99 return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
100
101 path_simplify(p);
102 if (suppress_root && empty_or_root(p))
103 p = mfree(p);
104
105 return free_and_replace(*arg, p);
106 }
107
108 int parse_signal_argument(const char *s, int *ret) {
109 int r;
110
111 assert(s);
112 assert(ret);
113
114 if (streq(s, "help"))
115 return DUMP_STRING_TABLE(signal, int, _NSIG);
116
117 if (streq(s, "list")) {
118 _cleanup_(table_unrefp) Table *table = NULL;
119
120 table = table_new("signal", "name");
121 if (!table)
122 return log_oom();
123
124 for (int i = 1; i < _NSIG; i++) {
125 r = table_add_many(
126 table,
127 TABLE_INT, i,
128 TABLE_SIGNAL, i);
129 if (r < 0)
130 return table_log_add_error(r);
131 }
132
133 return table_print_or_warn(table);
134 }
135
136 r = signal_from_string(s);
137 if (r < 0)
138 return log_error_errno(r, "Failed to parse signal string \"%s\".", s);
139
140 *ret = r;
141 return 1; /* work to do */
142 }
143
144 int parse_machine_argument(const char *s, const char **ret_host, BusTransport *ret_transport) {
145 int r;
146
147 assert(s);
148 assert(ret_host);
149
150 r = machine_spec_valid(s);
151 if (r < 0)
152 return log_error_errno(r, "Failed to validate --machine= argument '%s': %m", s);
153 if (r == 0)
154 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --machine= specified: %s", s);
155
156 *ret_host = s;
157
158 if (ret_transport)
159 *ret_transport = BUS_TRANSPORT_MACHINE;
160
161 return 0;
162 }
163
164 int parse_background_argument(const char *s, char **arg) {
165 if (!isempty(s) && !looks_like_ansi_color_code(s))
166 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --background= argument: %s", s);
167
168 return free_and_strdup_warn(arg, s);
169 }