1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Michael Biebl
12 #include "alloc-util.h"
14 #include "string-util.h"
16 #include "unit-name.h"
22 } arg_action
= ACTION_ESCAPE
;
23 static const char *arg_suffix
= NULL
;
24 static const char *arg_template
= NULL
;
25 static bool arg_path
= false;
27 static void help(void) {
28 printf("%s [OPTIONS...] [NAME...]\n\n"
29 "Escape strings for usage in systemd unit names.\n\n"
30 " -h --help Show this help\n"
31 " --version Show package version\n"
32 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
33 " --template=TEMPLATE Insert strings as instance into template\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' },
64 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
77 if (unit_type_from_string(optarg
) < 0) {
78 log_error("Invalid unit suffix type %s.", optarg
);
87 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
)) {
88 log_error("Template name %s is not valid.", optarg
);
92 arg_template
= optarg
;
96 arg_action
= ACTION_UNESCAPE
;
100 arg_action
= ACTION_MANGLE
;
111 assert_not_reached("Unhandled option");
114 if (optind
>= argc
) {
115 log_error("Not enough arguments.");
119 if (arg_template
&& arg_suffix
) {
120 log_error("--suffix= and --template= may not be combined.");
124 if ((arg_template
|| arg_suffix
) && arg_action
!= ACTION_ESCAPE
) {
125 log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
129 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
130 log_error("--path may not be combined with --mangle.");
137 int main(int argc
, char *argv
[]) {
141 log_parse_environment();
144 r
= parse_argv(argc
, argv
);
148 STRV_FOREACH(i
, argv
+ optind
) {
149 _cleanup_free_
char *e
= NULL
;
151 switch (arg_action
) {
155 r
= unit_name_path_escape(*i
, &e
);
157 log_error_errno(r
, "Failed to escape string: %m");
161 e
= unit_name_escape(*i
);
171 r
= unit_name_replace_instance(arg_template
, e
, &x
);
173 log_error_errno(r
, "Failed to replace instance: %m");
179 } else if (arg_suffix
) {
182 x
= strjoin(e
, ".", arg_suffix
);
194 case ACTION_UNESCAPE
:
196 r
= unit_name_path_unescape(*i
, &e
);
198 r
= unit_name_unescape(*i
, &e
);
201 log_error_errno(r
, "Failed to unescape string: %m");
207 r
= unit_name_mangle(*i
, 0, &e
);
209 log_error_errno(r
, "Failed to mangle name: %m");
215 if (i
!= argv
+optind
)
224 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;