1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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
,
50 static int parse_argv(int argc
, char *argv
[]) {
58 static const struct option options
[] = {
59 { "help", no_argument
, NULL
, 'h' },
60 { "version", no_argument
, NULL
, ARG_VERSION
},
61 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
62 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
63 { "unescape", no_argument
, NULL
, 'u' },
64 { "mangle", no_argument
, NULL
, 'm' },
65 { "path", no_argument
, NULL
, 'p' },
66 { "instance", no_argument
, NULL
, 'i' },
75 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
86 UnitType t
= unit_type_from_string(optarg
);
88 return log_error_errno(t
, "Invalid unit suffix type \"%s\".", optarg
);
95 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
))
96 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
97 "Template name %s is not valid.", optarg
);
99 arg_template
= optarg
;
103 arg_action
= ACTION_UNESCAPE
;
107 arg_action
= ACTION_MANGLE
;
122 assert_not_reached("Unhandled option");
126 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
127 "Not enough arguments.");
129 if (arg_template
&& arg_suffix
)
130 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
131 "--suffix= and --template= may not be combined.");
133 if ((arg_template
|| arg_suffix
) && arg_action
== ACTION_MANGLE
)
134 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
135 "--suffix= and --template= are not compatible with --mangle.");
137 if (arg_suffix
&& arg_action
== ACTION_UNESCAPE
)
138 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
139 "--suffix is not compatible with --unescape.");
141 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
))
142 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
143 "--path may not be combined with --mangle.");
145 if (arg_instance
&& arg_action
!= ACTION_UNESCAPE
)
146 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
147 "--instance must be used in conjunction with --unescape.");
149 if (arg_instance
&& arg_template
)
150 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
151 "--instance may not be combined with --template.");
156 static int run(int argc
, char *argv
[]) {
162 r
= parse_argv(argc
, argv
);
166 STRV_FOREACH(i
, argv
+ optind
) {
167 _cleanup_free_
char *e
= NULL
;
169 switch (arg_action
) {
173 r
= unit_name_path_escape(*i
, &e
);
175 return log_error_errno(r
, "Failed to escape string: %m");
177 e
= unit_name_escape(*i
);
185 r
= unit_name_replace_instance(arg_template
, e
, &x
);
187 return log_error_errno(r
, "Failed to replace instance: %m");
189 free_and_replace(e
, x
);
190 } else if (arg_suffix
) {
193 x
= strjoin(e
, ".", arg_suffix
);
197 free_and_replace(e
, x
);
202 case ACTION_UNESCAPE
: {
203 _cleanup_free_
char *name
= NULL
;
205 if (arg_template
|| arg_instance
) {
206 _cleanup_free_
char *template = NULL
;
208 r
= unit_name_to_instance(*i
, &name
);
210 return log_error_errno(r
, "Failed to extract instance: %m");
212 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
213 "Unit %s is missing the instance name.", *i
);
215 r
= unit_name_template(*i
, &template);
217 return log_error_errno(r
, "Failed to extract template: %m");
218 if (arg_template
&& !streq(arg_template
, template))
219 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
220 "Unit %s template %s does not match specified template %s.",
221 *i
, template, arg_template
);
229 r
= unit_name_path_unescape(name
, &e
);
231 r
= unit_name_unescape(name
, &e
);
233 return log_error_errno(r
, "Failed to unescape string: %m");
239 r
= unit_name_mangle(*i
, 0, &e
);
241 return log_error_errno(r
, "Failed to mangle name: %m");
246 if (i
!= argv
+ optind
)
257 DEFINE_MAIN_FUNCTION(run
);