]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/escape/escape.c
2b3cff0f3d7397db478041a4a1836643554813bd
[thirdparty/systemd.git] / src / escape / escape.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "alloc-util.h"
8 #include "log.h"
9 #include "main-func.h"
10 #include "pretty-print.h"
11 #include "string-util.h"
12 #include "strv.h"
13 #include "unit-name.h"
14
15 static enum {
16 ACTION_ESCAPE,
17 ACTION_UNESCAPE,
18 ACTION_MANGLE
19 } arg_action = ACTION_ESCAPE;
20 static const char *arg_suffix = NULL;
21 static const char *arg_template = NULL;
22 static bool arg_path = false;
23 static bool arg_instance = false;
24
25 static int help(void) {
26 _cleanup_free_ char *link = NULL;
27 int r;
28
29 r = terminal_urlify_man("systemd-escape", "1", &link);
30 if (r < 0)
31 return log_oom();
32
33 printf("%s [OPTIONS...] [NAME...]\n\n"
34 "Escape strings for usage in systemd unit names.\n\n"
35 " -h --help Show this help\n"
36 " --version Show package version\n"
37 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
38 " --template=TEMPLATE Insert strings as instance into template\n"
39 " --instance With --unescape, show just the instance part\n"
40 " -u --unescape Unescape strings\n"
41 " -m --mangle Mangle strings\n"
42 " -p --path When escaping/unescaping assume the string is a path\n"
43 "\nSee the %s for details.\n"
44 , program_invocation_short_name
45 , link
46 );
47
48 return 0;
49 }
50
51 static int parse_argv(int argc, char *argv[]) {
52
53 enum {
54 ARG_VERSION = 0x100,
55 ARG_SUFFIX,
56 ARG_TEMPLATE
57 };
58
59 static const struct option options[] = {
60 { "help", no_argument, NULL, 'h' },
61 { "version", no_argument, NULL, ARG_VERSION },
62 { "suffix", required_argument, NULL, ARG_SUFFIX },
63 { "template", required_argument, NULL, ARG_TEMPLATE },
64 { "unescape", no_argument, NULL, 'u' },
65 { "mangle", no_argument, NULL, 'm' },
66 { "path", no_argument, NULL, 'p' },
67 { "instance", no_argument, NULL, 'i' },
68 {}
69 };
70
71 int c;
72
73 assert(argc >= 0);
74 assert(argv);
75
76 while ((c = getopt_long(argc, argv, "hump", options, NULL)) >= 0)
77
78 switch (c) {
79
80 case 'h':
81 return help();
82
83 case ARG_VERSION:
84 return version();
85
86 case ARG_SUFFIX:
87
88 if (unit_type_from_string(optarg) < 0) {
89 log_error("Invalid unit suffix type %s.", optarg);
90 return -EINVAL;
91 }
92
93 arg_suffix = optarg;
94 break;
95
96 case ARG_TEMPLATE:
97
98 if (!unit_name_is_valid(optarg, UNIT_NAME_TEMPLATE)) {
99 log_error("Template name %s is not valid.", optarg);
100 return -EINVAL;
101 }
102
103 arg_template = optarg;
104 break;
105
106 case 'u':
107 arg_action = ACTION_UNESCAPE;
108 break;
109
110 case 'm':
111 arg_action = ACTION_MANGLE;
112 break;
113
114 case 'p':
115 arg_path = true;
116 break;
117
118 case 'i':
119 arg_instance = true;
120 break;
121
122 case '?':
123 return -EINVAL;
124
125 default:
126 assert_not_reached("Unhandled option");
127 }
128
129 if (optind >= argc) {
130 log_error("Not enough arguments.");
131 return -EINVAL;
132 }
133
134 if (arg_template && arg_suffix) {
135 log_error("--suffix= and --template= may not be combined.");
136 return -EINVAL;
137 }
138
139 if ((arg_template || arg_suffix) && arg_action == ACTION_MANGLE) {
140 log_error("--suffix= and --template= are not compatible with --mangle.");
141 return -EINVAL;
142 }
143
144 if (arg_suffix && arg_action == ACTION_UNESCAPE) {
145 log_error("--suffix is not compatible with --unescape.");
146 return -EINVAL;
147 }
148
149 if (arg_path && !IN_SET(arg_action, ACTION_ESCAPE, ACTION_UNESCAPE)) {
150 log_error("--path may not be combined with --mangle.");
151 return -EINVAL;
152 }
153
154 if (arg_instance && arg_action != ACTION_UNESCAPE) {
155 log_error("--instance must be used in conjunction with --unescape.");
156 return -EINVAL;
157 }
158
159 if (arg_instance && arg_template) {
160 log_error("--instance may not be combined with --template.");
161 return -EINVAL;
162 }
163
164 return 1;
165 }
166
167 static int run(int argc, char *argv[]) {
168 char **i;
169 int r;
170
171 log_parse_environment();
172 log_open();
173
174 r = parse_argv(argc, argv);
175 if (r <= 0)
176 return r;
177
178 STRV_FOREACH(i, argv + optind) {
179 _cleanup_free_ char *e = NULL;
180
181 switch (arg_action) {
182
183 case ACTION_ESCAPE:
184 if (arg_path) {
185 r = unit_name_path_escape(*i, &e);
186 if (r < 0)
187 return log_error_errno(r, "Failed to escape string: %m");
188 } else {
189 e = unit_name_escape(*i);
190 if (!e)
191 return log_oom();
192 }
193
194 if (arg_template) {
195 char *x;
196
197 r = unit_name_replace_instance(arg_template, e, &x);
198 if (r < 0)
199 return log_error_errno(r, "Failed to replace instance: %m");
200
201 free_and_replace(e, x);
202 } else if (arg_suffix) {
203 char *x;
204
205 x = strjoin(e, ".", arg_suffix);
206 if (!x)
207 return log_oom();
208
209 free_and_replace(e, x);
210 }
211
212 break;
213
214 case ACTION_UNESCAPE: {
215 _cleanup_free_ char *name = NULL;
216
217 if (arg_template || arg_instance) {
218 _cleanup_free_ char *template = NULL;
219
220 r = unit_name_to_instance(*i, &name);
221 if (r < 0)
222 return log_error_errno(r, "Failed to extract instance: %m");
223 if (isempty(name))
224 return log_error("Unit %s is missing the instance name.", *i);
225
226 r = unit_name_template(*i, &template);
227 if (r < 0)
228 return log_error_errno(r, "Failed to extract template: %m");
229 if (arg_template && !streq(arg_template, template))
230 return log_error("Unit %s template %s does not match specified template %s.",
231 *i, template, arg_template);
232 } else {
233 name = strdup(*i);
234 if (!name)
235 return log_oom();
236 }
237
238 if (arg_path)
239 r = unit_name_path_unescape(name, &e);
240 else
241 r = unit_name_unescape(name, &e);
242 if (r < 0)
243 return log_error_errno(r, "Failed to unescape string: %m");
244
245 break;
246 }
247
248 case ACTION_MANGLE:
249 r = unit_name_mangle(*i, 0, &e);
250 if (r < 0)
251 return log_error_errno(r, "Failed to mangle name: %m");
252
253 break;
254 }
255
256 if (i != argv + optind)
257 fputc(' ', stdout);
258
259 fputs(e, stdout);
260 }
261
262 fputc('\n', stdout);
263
264 return 0;
265 }
266
267 DEFINE_MAIN_FUNCTION(run);