1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
10 #include "pretty-print.h"
11 #include "string-util.h"
13 #include "unit-name.h"
19 } arg_action
= ACTION_ESCAPE
;
20 static const char *arg_suffix
= NULL
;
21 static const char *arg_template
= NULL
;
22 static bool arg_path
= false;
23 static bool arg_instance
= false;
25 static int help(void) {
26 _cleanup_free_
char *link
= NULL
;
29 r
= terminal_urlify_man("systemd-escape", "1", &link
);
33 printf("%s [OPTIONS...] [NAME...]\n\n"
34 "Escape strings for usage in systemd unit names.\n\n"
35 " -h --help Show this help\n"
36 " --version Show package version\n"
37 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
38 " --template=TEMPLATE Insert strings as instance into template\n"
39 " --instance With --unescape, show just the instance part\n"
40 " -u --unescape Unescape strings\n"
41 " -m --mangle Mangle strings\n"
42 " -p --path When escaping/unescaping assume the string is a path\n"
43 "\nSee the %s for details.\n"
44 , program_invocation_short_name
51 static int parse_argv(int argc
, char *argv
[]) {
59 static const struct option options
[] = {
60 { "help", no_argument
, NULL
, 'h' },
61 { "version", no_argument
, NULL
, ARG_VERSION
},
62 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
63 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
64 { "unescape", no_argument
, NULL
, 'u' },
65 { "mangle", no_argument
, NULL
, 'm' },
66 { "path", no_argument
, NULL
, 'p' },
67 { "instance", no_argument
, NULL
, 'i' },
76 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
88 if (unit_type_from_string(optarg
) < 0) {
89 log_error("Invalid unit suffix type %s.", optarg
);
98 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
)) {
99 log_error("Template name %s is not valid.", optarg
);
103 arg_template
= optarg
;
107 arg_action
= ACTION_UNESCAPE
;
111 arg_action
= ACTION_MANGLE
;
126 assert_not_reached("Unhandled option");
129 if (optind
>= argc
) {
130 log_error("Not enough arguments.");
134 if (arg_template
&& arg_suffix
) {
135 log_error("--suffix= and --template= may not be combined.");
139 if ((arg_template
|| arg_suffix
) && arg_action
== ACTION_MANGLE
) {
140 log_error("--suffix= and --template= are not compatible with --mangle.");
144 if (arg_suffix
&& arg_action
== ACTION_UNESCAPE
) {
145 log_error("--suffix is not compatible with --unescape.");
149 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
150 log_error("--path may not be combined with --mangle.");
154 if (arg_instance
&& arg_action
!= ACTION_UNESCAPE
) {
155 log_error("--instance must be used in conjunction with --unescape.");
159 if (arg_instance
&& arg_template
) {
160 log_error("--instance may not be combined with --template.");
167 static int run(int argc
, char *argv
[]) {
171 log_parse_environment();
174 r
= parse_argv(argc
, argv
);
178 STRV_FOREACH(i
, argv
+ optind
) {
179 _cleanup_free_
char *e
= NULL
;
181 switch (arg_action
) {
185 r
= unit_name_path_escape(*i
, &e
);
187 return log_error_errno(r
, "Failed to escape string: %m");
189 e
= unit_name_escape(*i
);
197 r
= unit_name_replace_instance(arg_template
, e
, &x
);
199 return log_error_errno(r
, "Failed to replace instance: %m");
201 free_and_replace(e
, x
);
202 } else if (arg_suffix
) {
205 x
= strjoin(e
, ".", arg_suffix
);
209 free_and_replace(e
, x
);
214 case ACTION_UNESCAPE
: {
215 _cleanup_free_
char *name
= NULL
;
217 if (arg_template
|| arg_instance
) {
218 _cleanup_free_
char *template = NULL
;
220 r
= unit_name_to_instance(*i
, &name
);
222 return log_error_errno(r
, "Failed to extract instance: %m");
224 return log_error("Unit %s is missing the instance name.", *i
);
226 r
= unit_name_template(*i
, &template);
228 return log_error_errno(r
, "Failed to extract template: %m");
229 if (arg_template
&& !streq(arg_template
, template))
230 return log_error("Unit %s template %s does not match specified template %s.",
231 *i
, template, arg_template
);
239 r
= unit_name_path_unescape(name
, &e
);
241 r
= unit_name_unescape(name
, &e
);
243 return log_error_errno(r
, "Failed to unescape string: %m");
249 r
= unit_name_mangle(*i
, 0, &e
);
251 return log_error_errno(r
, "Failed to mangle name: %m");
256 if (i
!= argv
+ optind
)
267 DEFINE_MAIN_FUNCTION(run
);