1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Michael Biebl
10 #include "alloc-util.h"
12 #include "string-util.h"
14 #include "unit-name.h"
20 } arg_action
= ACTION_ESCAPE
;
21 static const char *arg_suffix
= NULL
;
22 static const char *arg_template
= NULL
;
23 static bool arg_path
= false;
24 static bool arg_instance
= false;
26 static void help(void) {
27 printf("%s [OPTIONS...] [NAME...]\n\n"
28 "Escape strings for usage in systemd unit names.\n\n"
29 " -h --help Show this help\n"
30 " --version Show package version\n"
31 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
32 " --template=TEMPLATE Insert strings as instance into template\n"
33 " --instance With --unescape, show just the instance part\n"
34 " -u --unescape Unescape strings\n"
35 " -m --mangle Mangle strings\n"
36 " -p --path When escaping/unescaping assume the string is a path\n"
37 , program_invocation_short_name
);
40 static int parse_argv(int argc
, char *argv
[]) {
48 static const struct option options
[] = {
49 { "help", no_argument
, NULL
, 'h' },
50 { "version", no_argument
, NULL
, ARG_VERSION
},
51 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
52 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
53 { "unescape", no_argument
, NULL
, 'u' },
54 { "mangle", no_argument
, NULL
, 'm' },
55 { "path", no_argument
, NULL
, 'p' },
56 { "instance", no_argument
, NULL
, 'i' },
65 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
78 if (unit_type_from_string(optarg
) < 0) {
79 log_error("Invalid unit suffix type %s.", optarg
);
88 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
)) {
89 log_error("Template name %s is not valid.", optarg
);
93 arg_template
= optarg
;
97 arg_action
= ACTION_UNESCAPE
;
101 arg_action
= ACTION_MANGLE
;
116 assert_not_reached("Unhandled option");
119 if (optind
>= argc
) {
120 log_error("Not enough arguments.");
124 if (arg_template
&& arg_suffix
) {
125 log_error("--suffix= and --template= may not be combined.");
129 if ((arg_template
|| arg_suffix
) && arg_action
== ACTION_MANGLE
) {
130 log_error("--suffix= and --template= are not compatible with --mangle.");
134 if (arg_suffix
&& arg_action
== ACTION_UNESCAPE
) {
135 log_error("--suffix is not compatible with --unescape.");
139 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
140 log_error("--path may not be combined with --mangle.");
144 if (arg_instance
&& arg_action
!= ACTION_UNESCAPE
) {
145 log_error("--instance must be used in conjunction with --unescape.");
149 if (arg_instance
&& arg_template
) {
150 log_error("--instance may not be combined with --template.");
157 int main(int argc
, char *argv
[]) {
161 log_parse_environment();
164 r
= parse_argv(argc
, argv
);
168 STRV_FOREACH(i
, argv
+ optind
) {
169 _cleanup_free_
char *e
= NULL
;
171 switch (arg_action
) {
175 r
= unit_name_path_escape(*i
, &e
);
177 log_error_errno(r
, "Failed to escape string: %m");
181 e
= unit_name_escape(*i
);
191 r
= unit_name_replace_instance(arg_template
, e
, &x
);
193 log_error_errno(r
, "Failed to replace instance: %m");
199 } else if (arg_suffix
) {
202 x
= strjoin(e
, ".", arg_suffix
);
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 log_error_errno(r
, "Failed to extract instance: %m");
226 log_error("Unit %s is missing the instance name.", *i
);
230 r
= unit_name_template(*i
, &template);
232 log_error_errno(r
, "Failed to extract template: %m");
235 if (arg_template
&& !streq(arg_template
, template)) {
236 log_error("Unit %s template %s does not match specified template %s.", *i
, template, arg_template
);
249 r
= unit_name_path_unescape(name
, &e
);
251 r
= unit_name_unescape(name
, &e
);
254 log_error_errno(r
, "Failed to unescape string: %m");
261 r
= unit_name_mangle(*i
, 0, &e
);
263 log_error_errno(r
, "Failed to mangle name: %m");
269 if (i
!= argv
+optind
)
278 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;