1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
9 #include "string-util.h"
11 #include "unit-name.h"
17 } arg_action
= ACTION_ESCAPE
;
18 static const char *arg_suffix
= NULL
;
19 static const char *arg_template
= NULL
;
20 static bool arg_path
= false;
21 static bool arg_instance
= false;
23 static void help(void) {
24 printf("%s [OPTIONS...] [NAME...]\n\n"
25 "Escape strings for usage in systemd unit names.\n\n"
26 " -h --help Show this help\n"
27 " --version Show package version\n"
28 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
29 " --template=TEMPLATE Insert strings as instance into template\n"
30 " --instance With --unescape, show just the instance part\n"
31 " -u --unescape Unescape strings\n"
32 " -m --mangle Mangle strings\n"
33 " -p --path When escaping/unescaping assume the string is a path\n"
34 , program_invocation_short_name
);
37 static int parse_argv(int argc
, char *argv
[]) {
45 static const struct option options
[] = {
46 { "help", no_argument
, NULL
, 'h' },
47 { "version", no_argument
, NULL
, ARG_VERSION
},
48 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
49 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
50 { "unescape", no_argument
, NULL
, 'u' },
51 { "mangle", no_argument
, NULL
, 'm' },
52 { "path", no_argument
, NULL
, 'p' },
53 { "instance", no_argument
, NULL
, 'i' },
62 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
75 if (unit_type_from_string(optarg
) < 0) {
76 log_error("Invalid unit suffix type %s.", optarg
);
85 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
)) {
86 log_error("Template name %s is not valid.", optarg
);
90 arg_template
= optarg
;
94 arg_action
= ACTION_UNESCAPE
;
98 arg_action
= ACTION_MANGLE
;
113 assert_not_reached("Unhandled option");
116 if (optind
>= argc
) {
117 log_error("Not enough arguments.");
121 if (arg_template
&& arg_suffix
) {
122 log_error("--suffix= and --template= may not be combined.");
126 if ((arg_template
|| arg_suffix
) && arg_action
== ACTION_MANGLE
) {
127 log_error("--suffix= and --template= are not compatible with --mangle.");
131 if (arg_suffix
&& arg_action
== ACTION_UNESCAPE
) {
132 log_error("--suffix is not compatible with --unescape.");
136 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
137 log_error("--path may not be combined with --mangle.");
141 if (arg_instance
&& arg_action
!= ACTION_UNESCAPE
) {
142 log_error("--instance must be used in conjunction with --unescape.");
146 if (arg_instance
&& arg_template
) {
147 log_error("--instance may not be combined with --template.");
154 int main(int argc
, char *argv
[]) {
158 log_parse_environment();
161 r
= parse_argv(argc
, argv
);
165 STRV_FOREACH(i
, argv
+ optind
) {
166 _cleanup_free_
char *e
= NULL
;
168 switch (arg_action
) {
172 r
= unit_name_path_escape(*i
, &e
);
174 log_error_errno(r
, "Failed to escape string: %m");
178 e
= unit_name_escape(*i
);
188 r
= unit_name_replace_instance(arg_template
, e
, &x
);
190 log_error_errno(r
, "Failed to replace instance: %m");
196 } else if (arg_suffix
) {
199 x
= strjoin(e
, ".", arg_suffix
);
211 case ACTION_UNESCAPE
: {
212 _cleanup_free_
char *name
= NULL
;
214 if (arg_template
|| arg_instance
) {
215 _cleanup_free_
char *template = NULL
;
217 r
= unit_name_to_instance(*i
, &name
);
219 log_error_errno(r
, "Failed to extract instance: %m");
223 log_error("Unit %s is missing the instance name.", *i
);
227 r
= unit_name_template(*i
, &template);
229 log_error_errno(r
, "Failed to extract template: %m");
232 if (arg_template
&& !streq(arg_template
, template)) {
233 log_error("Unit %s template %s does not match specified template %s.", *i
, template, arg_template
);
246 r
= unit_name_path_unescape(name
, &e
);
248 r
= unit_name_unescape(name
, &e
);
251 log_error_errno(r
, "Failed to unescape string: %m");
258 r
= unit_name_mangle(*i
, 0, &e
);
260 log_error_errno(r
, "Failed to mangle name: %m");
266 if (i
!= argv
+optind
)
275 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;