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 warnings. */
13 int parse_boolean_argument(const char *optname
, const char *s
, bool *ret
) {
16 /* Returns the result through *ret and the return value. */
21 return log_error_errno(r
, "Failed to parse boolean argument to %s: %s.", optname
, s
);
27 /* s may be NULL. This is controlled by getopt_long() parameters. */
34 int parse_json_argument(const char *s
, sd_json_format_flags_t
*ret
) {
38 if (streq(s
, "pretty"))
39 *ret
= SD_JSON_FORMAT_PRETTY
|SD_JSON_FORMAT_COLOR_AUTO
;
40 else if (streq(s
, "short"))
41 *ret
= SD_JSON_FORMAT_NEWLINE
;
42 else if (streq(s
, "off"))
43 *ret
= SD_JSON_FORMAT_OFF
;
44 else if (streq(s
, "help")) {
48 return 0; /* 0 means → we showed a brief help, exit now */
50 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown argument to --json= switch: %s", s
);
52 return 1; /* 1 means → properly parsed */
55 int parse_path_argument(const char *path
, bool suppress_root
, char **arg
) {
60 * This function is intended to be used in command line parsers, to handle paths that are passed
61 * in. It makes the path absolute, and reduces it to NULL if omitted or root (the latter optionally).
63 * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON SUCCESS!
64 * Hence, do not pass in uninitialized pointers.
72 r
= path_make_absolute_cwd(path
, &p
);
74 return log_error_errno(r
, "Failed to parse path \"%s\" and make it absolute: %m", path
);
77 if (suppress_root
&& empty_or_root(p
))
80 return free_and_replace(*arg
, p
);
83 int parse_signal_argument(const char *s
, int *ret
) {
89 if (streq(s
, "help")) {
90 DUMP_STRING_TABLE(signal
, int, _NSIG
);
94 if (streq(s
, "list")) {
95 _cleanup_(table_unrefp
) Table
*table
= NULL
;
97 table
= table_new("signal", "name");
101 for (int i
= 1; i
< _NSIG
; i
++) {
107 return table_log_add_error(r
);
110 r
= table_print(table
, NULL
);
112 return table_log_print_error(r
);
117 r
= signal_from_string(s
);
119 return log_error_errno(r
, "Failed to parse signal string \"%s\".", s
);
122 return 1; /* work to do */