]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/escape/escape.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / escape / escape.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Michael Biebl
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "alloc-util.h"
26 #include "log.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "unit-name.h"
30
31 static enum {
32 ACTION_ESCAPE,
33 ACTION_UNESCAPE,
34 ACTION_MANGLE
35 } arg_action = ACTION_ESCAPE;
36 static const char *arg_suffix = NULL;
37 static const char *arg_template = NULL;
38 static bool arg_path = false;
39
40 static void help(void) {
41 printf("%s [OPTIONS...] [NAME...]\n\n"
42 "Escape strings for usage in systemd unit names.\n\n"
43 " -h --help Show this help\n"
44 " --version Show package version\n"
45 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
46 " --template=TEMPLATE Insert strings as instance into template\n"
47 " -u --unescape Unescape strings\n"
48 " -m --mangle Mangle strings\n"
49 " -p --path When escaping/unescaping assume the string is a path\n"
50 , program_invocation_short_name);
51 }
52
53 static int parse_argv(int argc, char *argv[]) {
54
55 enum {
56 ARG_VERSION = 0x100,
57 ARG_SUFFIX,
58 ARG_TEMPLATE
59 };
60
61 static const struct option options[] = {
62 { "help", no_argument, NULL, 'h' },
63 { "version", no_argument, NULL, ARG_VERSION },
64 { "suffix", required_argument, NULL, ARG_SUFFIX },
65 { "template", required_argument, NULL, ARG_TEMPLATE },
66 { "unescape", no_argument, NULL, 'u' },
67 { "mangle", no_argument, NULL, 'm' },
68 { "path", no_argument, NULL, 'p' },
69 {}
70 };
71
72 int c;
73
74 assert(argc >= 0);
75 assert(argv);
76
77 while ((c = getopt_long(argc, argv, "hump", options, NULL)) >= 0)
78
79 switch (c) {
80
81 case 'h':
82 help();
83 return 0;
84
85 case ARG_VERSION:
86 return version();
87
88 case ARG_SUFFIX:
89
90 if (unit_type_from_string(optarg) < 0) {
91 log_error("Invalid unit suffix type %s.", optarg);
92 return -EINVAL;
93 }
94
95 arg_suffix = optarg;
96 break;
97
98 case ARG_TEMPLATE:
99
100 if (!unit_name_is_valid(optarg, UNIT_NAME_TEMPLATE)) {
101 log_error("Template name %s is not valid.", optarg);
102 return -EINVAL;
103 }
104
105 arg_template = optarg;
106 break;
107
108 case 'u':
109 arg_action = ACTION_UNESCAPE;
110 break;
111
112 case 'm':
113 arg_action = ACTION_MANGLE;
114 break;
115
116 case 'p':
117 arg_path = true;
118 break;
119
120 case '?':
121 return -EINVAL;
122
123 default:
124 assert_not_reached("Unhandled option");
125 }
126
127 if (optind >= argc) {
128 log_error("Not enough arguments.");
129 return -EINVAL;
130 }
131
132 if (arg_template && arg_suffix) {
133 log_error("--suffix= and --template= may not be combined.");
134 return -EINVAL;
135 }
136
137 if ((arg_template || arg_suffix) && arg_action != ACTION_ESCAPE) {
138 log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
139 return -EINVAL;
140 }
141
142 if (arg_path && !IN_SET(arg_action, ACTION_ESCAPE, ACTION_UNESCAPE)) {
143 log_error("--path may not be combined with --mangle.");
144 return -EINVAL;
145 }
146
147 return 1;
148 }
149
150 int main(int argc, char *argv[]) {
151 char **i;
152 int r;
153
154 log_parse_environment();
155 log_open();
156
157 r = parse_argv(argc, argv);
158 if (r <= 0)
159 goto finish;
160
161 STRV_FOREACH(i, argv + optind) {
162 _cleanup_free_ char *e = NULL;
163
164 switch (arg_action) {
165
166 case ACTION_ESCAPE:
167 if (arg_path) {
168 r = unit_name_path_escape(*i, &e);
169 if (r < 0) {
170 log_error_errno(r, "Failed to escape string: %m");
171 goto finish;
172 }
173 } else {
174 e = unit_name_escape(*i);
175 if (!e) {
176 r = log_oom();
177 goto finish;
178 }
179 }
180
181 if (arg_template) {
182 char *x;
183
184 r = unit_name_replace_instance(arg_template, e, &x);
185 if (r < 0) {
186 log_error_errno(r, "Failed to replace instance: %m");
187 goto finish;
188 }
189
190 free(e);
191 e = x;
192 } else if (arg_suffix) {
193 char *x;
194
195 x = strjoin(e, ".", arg_suffix);
196 if (!x) {
197 r = log_oom();
198 goto finish;
199 }
200
201 free(e);
202 e = x;
203 }
204
205 break;
206
207 case ACTION_UNESCAPE:
208 if (arg_path)
209 r = unit_name_path_unescape(*i, &e);
210 else
211 r = unit_name_unescape(*i, &e);
212
213 if (r < 0) {
214 log_error_errno(r, "Failed to unescape string: %m");
215 goto finish;
216 }
217 break;
218
219 case ACTION_MANGLE:
220 r = unit_name_mangle(*i, UNIT_NAME_NOGLOB, &e);
221 if (r < 0) {
222 log_error_errno(r, "Failed to mangle name: %m");
223 goto finish;
224 }
225 break;
226 }
227
228 if (i != argv+optind)
229 fputc(' ', stdout);
230
231 fputs(e, stdout);
232 }
233
234 fputc('\n', stdout);
235
236 finish:
237 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
238 }