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