]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/parse-argument.c
cd1d0dde21a54cbbe0b0074ab2a504f3a23c3db6
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "format-table.h"
4 #include "parse-argument.h"
6 #include "signal-util.h"
7 #include "stdio-util.h"
8 #include "string-table.h"
9 #include "string-util.h"
11 /* All functions in this file emit warnigs. */
13 int parse_json_argument(const char *s
, JsonFormatFlags
*ret
) {
17 if (streq(s
, "pretty"))
18 *ret
= JSON_FORMAT_PRETTY
|JSON_FORMAT_COLOR_AUTO
;
19 else if (streq(s
, "short"))
20 *ret
= JSON_FORMAT_NEWLINE
;
21 else if (streq(s
, "off"))
22 *ret
= JSON_FORMAT_OFF
;
23 else if (streq(s
, "help")) {
27 return 0; /* 0 means → we showed a brief help, exit now */
29 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown argument to --json= switch: %s", s
);
31 return 1; /* 1 means → properly parsed */
34 int parse_path_argument(const char *path
, bool suppress_root
, char **arg
) {
39 * This function is intended to be used in command line parsers, to handle paths that are passed
40 * in. It makes the path absolute, and reduces it to NULL if omitted or root (the latter optionally).
42 * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON SUCCESS!
43 * Hence, do not pass in uninitialized pointers.
51 r
= path_make_absolute_cwd(path
, &p
);
53 return log_error_errno(r
, "Failed to parse path \"%s\" and make it absolute: %m", path
);
55 path_simplify(p
, false);
56 if (suppress_root
&& empty_or_root(p
))
59 return free_and_replace(*arg
, p
);
62 int parse_signal_argument(const char *s
, int *ret
) {
68 if (streq(s
, "help")) {
69 DUMP_STRING_TABLE(signal
, int, _NSIG
);
73 if (streq(s
, "list")) {
74 _cleanup_(table_unrefp
) Table
*table
= NULL
;
76 table
= table_new("signal", "name");
80 for (int i
= 1; i
< _NSIG
; i
++) {
86 return table_log_add_error(r
);
89 r
= table_print(table
, NULL
);
91 return table_log_print_error(r
);
96 r
= signal_from_string(s
);
98 return log_error_errno(r
, "Failed to parse signal string \"%s\".", s
);
101 return 1; /* work to do */