1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Michael Biebl
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
27 #include "string-util.h"
29 #include "unit-name.h"
35 } arg_action
= ACTION_ESCAPE
;
36 static const char *arg_suffix
= NULL
;
37 static const char *arg_template
= NULL
;
38 static bool arg_path
= false;
40 static void help(void) {
41 printf("%s [OPTIONS...] [NAME...]\n\n"
42 "Escape strings for usage in systemd unit names.\n\n"
43 " -h --help Show this help\n"
44 " --version Show package version\n"
45 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
46 " --template=TEMPLATE Insert strings as instance into template\n"
47 " -u --unescape Unescape strings\n"
48 " -m --mangle Mangle strings\n"
49 " -p --path When escaping/unescaping assume the string is a path\n"
50 , program_invocation_short_name
);
53 static int parse_argv(int argc
, char *argv
[]) {
61 static const struct option options
[] = {
62 { "help", no_argument
, NULL
, 'h' },
63 { "version", no_argument
, NULL
, ARG_VERSION
},
64 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
65 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
66 { "unescape", no_argument
, NULL
, 'u' },
67 { "mangle", no_argument
, NULL
, 'm' },
68 { "path", no_argument
, NULL
, 'p' },
77 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
90 if (unit_type_from_string(optarg
) < 0) {
91 log_error("Invalid unit suffix type %s.", optarg
);
100 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
)) {
101 log_error("Template name %s is not valid.", optarg
);
105 arg_template
= optarg
;
109 arg_action
= ACTION_UNESCAPE
;
113 arg_action
= ACTION_MANGLE
;
124 assert_not_reached("Unhandled option");
127 if (optind
>= argc
) {
128 log_error("Not enough arguments.");
132 if (arg_template
&& arg_suffix
) {
133 log_error("--suffix= and --template= may not be combined.");
137 if ((arg_template
|| arg_suffix
) && arg_action
!= ACTION_ESCAPE
) {
138 log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
142 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
143 log_error("--path may not be combined with --mangle.");
150 int main(int argc
, char *argv
[]) {
154 log_parse_environment();
157 r
= parse_argv(argc
, argv
);
161 STRV_FOREACH(i
, argv
+ optind
) {
162 _cleanup_free_
char *e
= NULL
;
164 switch (arg_action
) {
168 r
= unit_name_path_escape(*i
, &e
);
170 log_error_errno(r
, "Failed to escape string: %m");
174 e
= unit_name_escape(*i
);
184 r
= unit_name_replace_instance(arg_template
, e
, &x
);
186 log_error_errno(r
, "Failed to replace instance: %m");
192 } else if (arg_suffix
) {
195 x
= strjoin(e
, ".", arg_suffix
);
207 case ACTION_UNESCAPE
:
209 r
= unit_name_path_unescape(*i
, &e
);
211 r
= unit_name_unescape(*i
, &e
);
214 log_error_errno(r
, "Failed to unescape string: %m");
220 r
= unit_name_mangle(*i
, UNIT_NAME_NOGLOB
, &e
);
222 log_error_errno(r
, "Failed to mangle name: %m");
228 if (i
!= argv
+optind
)
237 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;