2 This file is part of systemd.
4 Copyright 2014 Michael Biebl
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
26 #include "string-util.h"
28 #include "unit-name.h"
34 } arg_action
= ACTION_ESCAPE
;
35 static const char *arg_suffix
= NULL
;
36 static const char *arg_template
= NULL
;
37 static bool arg_path
= false;
39 static void help(void) {
40 printf("%s [OPTIONS...] [NAME...]\n\n"
41 "Escape strings for usage in systemd unit names.\n\n"
42 " -h --help Show this help\n"
43 " --version Show package version\n"
44 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
45 " --template=TEMPLATE Insert strings as instance into template\n"
46 " -u --unescape Unescape strings\n"
47 " -m --mangle Mangle strings\n"
48 " -p --path When escaping/unescaping assume the string is a path\n"
49 , program_invocation_short_name
);
52 static int parse_argv(int argc
, char *argv
[]) {
60 static const struct option options
[] = {
61 { "help", no_argument
, NULL
, 'h' },
62 { "version", no_argument
, NULL
, ARG_VERSION
},
63 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
64 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
65 { "unescape", no_argument
, NULL
, 'u' },
66 { "mangle", no_argument
, NULL
, 'm' },
67 { "path", no_argument
, NULL
, 'p' },
76 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
89 if (unit_type_from_string(optarg
) < 0) {
90 log_error("Invalid unit suffix type %s.", optarg
);
99 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
)) {
100 log_error("Template name %s is not valid.", optarg
);
104 arg_template
= optarg
;
108 arg_action
= ACTION_UNESCAPE
;
112 arg_action
= ACTION_MANGLE
;
123 assert_not_reached("Unhandled option");
126 if (optind
>= argc
) {
127 log_error("Not enough arguments.");
131 if (arg_template
&& arg_suffix
) {
132 log_error("--suffix= and --template= may not be combined.");
136 if ((arg_template
|| arg_suffix
) && arg_action
!= ACTION_ESCAPE
) {
137 log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
141 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
142 log_error("--path may not be combined with --mangle.");
149 int main(int argc
, char *argv
[]) {
153 log_parse_environment();
156 r
= parse_argv(argc
, argv
);
160 STRV_FOREACH(i
, argv
+ optind
) {
161 _cleanup_free_
char *e
= NULL
;
163 switch (arg_action
) {
167 r
= unit_name_path_escape(*i
, &e
);
169 log_error_errno(r
, "Failed to escape string: %m");
173 e
= unit_name_escape(*i
);
183 r
= unit_name_replace_instance(arg_template
, e
, &x
);
185 log_error_errno(r
, "Failed to replace instance: %m");
191 } else if (arg_suffix
) {
194 x
= strjoin(e
, ".", arg_suffix
);
206 case ACTION_UNESCAPE
:
208 r
= unit_name_path_unescape(*i
, &e
);
210 r
= unit_name_unescape(*i
, &e
);
213 log_error_errno(r
, "Failed to unescape string: %m");
219 r
= unit_name_mangle(*i
, UNIT_NAME_NOGLOB
, &e
);
221 log_error_errno(r
, "Failed to mangle name: %m");
227 if (i
!= argv
+optind
)
236 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;