]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/escape/escape.c
Merge pull request #18355 from DaanDeMeyer/resolved-discover
[thirdparty/systemd.git] / src / escape / escape.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 return 0;
48 }
49
50 static int parse_argv(int argc, char *argv[]) {
51
52 enum {
53 ARG_VERSION = 0x100,
54 ARG_SUFFIX,
55 ARG_TEMPLATE
56 };
57
58 static const struct option options[] = {
59 { "help", no_argument, NULL, 'h' },
60 { "version", no_argument, NULL, ARG_VERSION },
61 { "suffix", required_argument, NULL, ARG_SUFFIX },
62 { "template", required_argument, NULL, ARG_TEMPLATE },
63 { "unescape", no_argument, NULL, 'u' },
64 { "mangle", no_argument, NULL, 'm' },
65 { "path", no_argument, NULL, 'p' },
66 { "instance", no_argument, NULL, 'i' },
67 {}
68 };
69
70 int c;
71
72 assert(argc >= 0);
73 assert(argv);
74
75 while ((c = getopt_long(argc, argv, "hump", options, NULL)) >= 0)
76
77 switch (c) {
78
79 case 'h':
80 return help();
81
82 case ARG_VERSION:
83 return version();
84
85 case ARG_SUFFIX:
86
87 if (unit_type_from_string(optarg) < 0)
88 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
89 "Invalid unit suffix type %s.", optarg);
90
91 arg_suffix = optarg;
92 break;
93
94 case ARG_TEMPLATE:
95
96 if (!unit_name_is_valid(optarg, UNIT_NAME_TEMPLATE))
97 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
98 "Template name %s is not valid.", optarg);
99
100 arg_template = optarg;
101 break;
102
103 case 'u':
104 arg_action = ACTION_UNESCAPE;
105 break;
106
107 case 'm':
108 arg_action = ACTION_MANGLE;
109 break;
110
111 case 'p':
112 arg_path = true;
113 break;
114
115 case 'i':
116 arg_instance = true;
117 break;
118
119 case '?':
120 return -EINVAL;
121
122 default:
123 assert_not_reached("Unhandled option");
124 }
125
126 if (optind >= argc)
127 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
128 "Not enough arguments.");
129
130 if (arg_template && arg_suffix)
131 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
132 "--suffix= and --template= may not be combined.");
133
134 if ((arg_template || arg_suffix) && arg_action == ACTION_MANGLE)
135 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
136 "--suffix= and --template= are not compatible with --mangle.");
137
138 if (arg_suffix && arg_action == ACTION_UNESCAPE)
139 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
140 "--suffix is not compatible with --unescape.");
141
142 if (arg_path && !IN_SET(arg_action, ACTION_ESCAPE, ACTION_UNESCAPE))
143 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
144 "--path may not be combined with --mangle.");
145
146 if (arg_instance && arg_action != ACTION_UNESCAPE)
147 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
148 "--instance must be used in conjunction with --unescape.");
149
150 if (arg_instance && arg_template)
151 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
152 "--instance may not be combined with --template.");
153
154 return 1;
155 }
156
157 static int run(int argc, char *argv[]) {
158 char **i;
159 int r;
160
161 log_setup_cli();
162
163 r = parse_argv(argc, argv);
164 if (r <= 0)
165 return r;
166
167 STRV_FOREACH(i, argv + optind) {
168 _cleanup_free_ char *e = NULL;
169
170 switch (arg_action) {
171
172 case ACTION_ESCAPE:
173 if (arg_path) {
174 r = unit_name_path_escape(*i, &e);
175 if (r < 0)
176 return log_error_errno(r, "Failed to escape string: %m");
177 } else {
178 e = unit_name_escape(*i);
179 if (!e)
180 return log_oom();
181 }
182
183 if (arg_template) {
184 char *x;
185
186 r = unit_name_replace_instance(arg_template, e, &x);
187 if (r < 0)
188 return log_error_errno(r, "Failed to replace instance: %m");
189
190 free_and_replace(e, x);
191 } else if (arg_suffix) {
192 char *x;
193
194 x = strjoin(e, ".", arg_suffix);
195 if (!x)
196 return log_oom();
197
198 free_and_replace(e, x);
199 }
200
201 break;
202
203 case ACTION_UNESCAPE: {
204 _cleanup_free_ char *name = NULL;
205
206 if (arg_template || arg_instance) {
207 _cleanup_free_ char *template = NULL;
208
209 r = unit_name_to_instance(*i, &name);
210 if (r < 0)
211 return log_error_errno(r, "Failed to extract instance: %m");
212 if (isempty(name))
213 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
214 "Unit %s is missing the instance name.", *i);
215
216 r = unit_name_template(*i, &template);
217 if (r < 0)
218 return log_error_errno(r, "Failed to extract template: %m");
219 if (arg_template && !streq(arg_template, template))
220 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
221 "Unit %s template %s does not match specified template %s.",
222 *i, template, arg_template);
223 } else {
224 name = strdup(*i);
225 if (!name)
226 return log_oom();
227 }
228
229 if (arg_path)
230 r = unit_name_path_unescape(name, &e);
231 else
232 r = unit_name_unescape(name, &e);
233 if (r < 0)
234 return log_error_errno(r, "Failed to unescape string: %m");
235
236 break;
237 }
238
239 case ACTION_MANGLE:
240 r = unit_name_mangle(*i, 0, &e);
241 if (r < 0)
242 return log_error_errno(r, "Failed to mangle name: %m");
243
244 break;
245 }
246
247 if (i != argv + optind)
248 fputc(' ', stdout);
249
250 fputs(e, stdout);
251 }
252
253 fputc('\n', stdout);
254
255 return 0;
256 }
257
258 DEFINE_MAIN_FUNCTION(run);