]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
b1a5a998 MB |
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 | ||
3f6fd1ba | 21 | #include <getopt.h> |
b1a5a998 MB |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
24 | ||
b5efdb8a | 25 | #include "alloc-util.h" |
b1a5a998 | 26 | #include "log.h" |
07630cea | 27 | #include "string-util.h" |
a1948c7b | 28 | #include "strv.h" |
3f6fd1ba | 29 | #include "unit-name.h" |
b1a5a998 | 30 | |
a1948c7b LP |
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 | ||
601185b4 | 40 | static void help(void) { |
a1948c7b | 41 | printf("%s [OPTIONS...] [NAME...]\n\n" |
ab7e3ef5 | 42 | "Escape strings for usage in systemd unit names.\n\n" |
a1948c7b LP |
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" | |
601185b4 ZJS |
49 | " -p --path When escaping/unescaping assume the string is a path\n" |
50 | , program_invocation_short_name); | |
a1948c7b LP |
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 | ||
601185b4 | 77 | while ((c = getopt_long(argc, argv, "hump", options, NULL)) >= 0) |
a1948c7b LP |
78 | |
79 | switch (c) { | |
80 | ||
81 | case 'h': | |
601185b4 ZJS |
82 | help(); |
83 | return 0; | |
a1948c7b LP |
84 | |
85 | case ARG_VERSION: | |
3f6fd1ba | 86 | return version(); |
a1948c7b LP |
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 | ||
7410616c | 100 | if (!unit_name_is_valid(optarg, UNIT_NAME_TEMPLATE)) { |
a1948c7b LP |
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 | } | |
b1a5a998 | 126 | |
a1948c7b LP |
127 | if (optind >= argc) { |
128 | log_error("Not enough arguments."); | |
129 | return -EINVAL; | |
b1a5a998 MB |
130 | } |
131 | ||
a1948c7b LP |
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: | |
7410616c LP |
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 { | |
a1948c7b | 174 | e = unit_name_escape(*i); |
7410616c LP |
175 | if (!e) { |
176 | r = log_oom(); | |
177 | goto finish; | |
178 | } | |
a1948c7b LP |
179 | } |
180 | ||
181 | if (arg_template) { | |
182 | char *x; | |
183 | ||
7410616c LP |
184 | r = unit_name_replace_instance(arg_template, e, &x); |
185 | if (r < 0) { | |
186 | log_error_errno(r, "Failed to replace instance: %m"); | |
a1948c7b LP |
187 | goto finish; |
188 | } | |
189 | ||
190 | free(e); | |
191 | e = x; | |
192 | } else if (arg_suffix) { | |
193 | char *x; | |
194 | ||
605405c6 | 195 | x = strjoin(e, ".", arg_suffix); |
a1948c7b LP |
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) | |
7410616c | 209 | r = unit_name_path_unescape(*i, &e); |
a1948c7b | 210 | else |
7410616c | 211 | r = unit_name_unescape(*i, &e); |
a1948c7b | 212 | |
7410616c LP |
213 | if (r < 0) { |
214 | log_error_errno(r, "Failed to unescape string: %m"); | |
a1948c7b LP |
215 | goto finish; |
216 | } | |
217 | break; | |
218 | ||
219 | case ACTION_MANGLE: | |
7410616c LP |
220 | r = unit_name_mangle(*i, UNIT_NAME_NOGLOB, &e); |
221 | if (r < 0) { | |
222 | log_error_errno(r, "Failed to mangle name: %m"); | |
a1948c7b LP |
223 | goto finish; |
224 | } | |
225 | break; | |
226 | } | |
227 | ||
228 | if (i != argv+optind) | |
229 | fputc(' ', stdout); | |
b1a5a998 | 230 | |
a1948c7b | 231 | fputs(e, stdout); |
b1a5a998 MB |
232 | } |
233 | ||
a1948c7b | 234 | fputc('\n', stdout); |
b1a5a998 | 235 | |
a1948c7b | 236 | finish: |
ff9c82cc | 237 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
b1a5a998 | 238 | } |