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;
25 static void help(void) {
26 printf("%s [OPTIONS...] [NAME...]\n\n"
27 "Escape strings for usage in systemd unit names.\n\n"
28 " -h --help Show this help\n"
29 " --version Show package version\n"
30 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
31 " --template=TEMPLATE Insert strings as instance into template\n"
32 " -u --unescape Unescape strings\n"
33 " -m --mangle Mangle strings\n"
34 " -p --path When escaping/unescaping assume the string is a path\n"
35 , program_invocation_short_name
);
38 static int parse_argv(int argc
, char *argv
[]) {
46 static const struct option options
[] = {
47 { "help", no_argument
, NULL
, 'h' },
48 { "version", no_argument
, NULL
, ARG_VERSION
},
49 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
50 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
51 { "unescape", no_argument
, NULL
, 'u' },
52 { "mangle", no_argument
, NULL
, 'm' },
53 { "path", no_argument
, NULL
, 'p' },
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
;
109 assert_not_reached("Unhandled option");
112 if (optind
>= argc
) {
113 log_error("Not enough arguments.");
117 if (arg_template
&& arg_suffix
) {
118 log_error("--suffix= and --template= may not be combined.");
122 if ((arg_template
|| arg_suffix
) && arg_action
!= ACTION_ESCAPE
) {
123 log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
127 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
128 log_error("--path may not be combined with --mangle.");
135 int main(int argc
, char *argv
[]) {
139 log_parse_environment();
142 r
= parse_argv(argc
, argv
);
146 STRV_FOREACH(i
, argv
+ optind
) {
147 _cleanup_free_
char *e
= NULL
;
149 switch (arg_action
) {
153 r
= unit_name_path_escape(*i
, &e
);
155 log_error_errno(r
, "Failed to escape string: %m");
159 e
= unit_name_escape(*i
);
169 r
= unit_name_replace_instance(arg_template
, e
, &x
);
171 log_error_errno(r
, "Failed to replace instance: %m");
177 } else if (arg_suffix
) {
180 x
= strjoin(e
, ".", arg_suffix
);
192 case ACTION_UNESCAPE
:
194 r
= unit_name_path_unescape(*i
, &e
);
196 r
= unit_name_unescape(*i
, &e
);
199 log_error_errno(r
, "Failed to unescape string: %m");
205 r
= unit_name_mangle(*i
, 0, &e
);
207 log_error_errno(r
, "Failed to mangle name: %m");
213 if (i
!= argv
+optind
)
222 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;