]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/unit-name.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "unit-name.h"
31 "abcdefghijklmnopqrstuvwxyz" \
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
35 bool unit_name_is_valid_no_type(const char *n
, bool template_ok
) {
36 const char *e
, *i
, *at
;
40 * string@instance.suffix
46 if (strlen(n
) >= UNIT_NAME_MAX
)
53 for (i
= n
, at
= NULL
; i
< e
; i
++) {
58 if (!strchr("@" VALID_CHARS
, *i
))
66 if (!template_ok
&& at
+1 == e
)
73 bool unit_instance_is_valid(const char *i
) {
76 /* The max length depends on the length of the string, so we
77 * don't really check this here. */
82 /* We allow additional @ in the instance string, we do not
83 * allow them in the prefix! */
86 if (!strchr("@" VALID_CHARS
, *i
))
92 bool unit_prefix_is_valid(const char *p
) {
94 /* We don't allow additional @ in the instance string */
100 if (!strchr(VALID_CHARS
, *p
))
106 int unit_name_to_instance(const char *n
, char **instance
) {
113 /* Everything past the first @ and before the last . is the instance */
114 if (!(p
= strchr(n
, '@'))) {
119 assert_se(d
= strrchr(n
, '.'));
122 if (!(i
= strndup(p
+1, d
-p
-1)))
129 char *unit_name_to_prefix_and_instance(const char *n
) {
134 assert_se(d
= strrchr(n
, '.'));
136 return strndup(n
, d
- n
);
139 char *unit_name_to_prefix(const char *n
) {
142 if ((p
= strchr(n
, '@')))
143 return strndup(n
, p
- n
);
145 return unit_name_to_prefix_and_instance(n
);
148 char *unit_name_change_suffix(const char *n
, const char *suffix
) {
153 assert(unit_name_is_valid_no_type(n
, true));
156 assert_se(e
= strrchr(n
, '.'));
160 if (!(r
= new(char, a
+ b
+ 1)))
164 memcpy(r
+a
, suffix
, b
+1);
169 char *unit_name_build(const char *prefix
, const char *instance
, const char *suffix
) {
173 assert(unit_prefix_is_valid(prefix
));
174 assert(!instance
|| unit_instance_is_valid(instance
));
178 return strappend(prefix
, suffix
);
180 if (asprintf(&r
, "%s@%s%s", prefix
, instance
, suffix
) < 0)
186 static char* do_escape(const char *f
, char *t
) {
193 else if (*f
== '-' || *f
== '\\' || !strchr(VALID_CHARS
, *f
)) {
196 *(t
++) = hexchar(*f
>> 4);
197 *(t
++) = hexchar(*f
);
205 char *unit_name_build_escape(const char *prefix
, const char *instance
, const char *suffix
) {
212 /* Takes a arbitrary string for prefix and instance plus a
213 * suffix and makes a nice string suitable as unit name of it,
214 * escaping all weird chars on the way.
216 * / becomes ., and all chars not alloweed in a unit name get
217 * escaped as \xFF, including \ and ., of course. This
218 * escaping is hence reversible.
220 * This is primarily useful to make nice unit names from
221 * strings, but is actually useful for any kind of string.
228 b
= strlen(instance
);
230 if (!(r
= new(char, a
*4 + 1 + b
*4 + c
+ 1)))
233 t
= do_escape(prefix
, r
);
235 t
= do_escape(instance
, t
);
238 if (!(r
= new(char, a
*4 + c
+ 1)))
241 t
= do_escape(prefix
, r
);
248 char *unit_name_escape(const char *f
) {
251 if (!(r
= new(char, strlen(f
)*4+1)))
261 char *unit_name_unescape(const char *f
) {
266 if (!(r
= strdup(f
)))
269 for (t
= r
; *f
; f
++) {
272 else if (*f
== '\\') {
275 if ((a
= unhexchar(f
[1])) < 0 ||
276 (b
= unhexchar(f
[2])) < 0) {
277 /* Invalid escape code, let's take it literal then */
280 *(t
++) = (char) ((a
<< 4) | b
);
292 bool unit_name_is_template(const char *n
) {
297 if (!(p
= strchr(n
, '@')))
303 char *unit_name_replace_instance(const char *f
, const char *i
) {
311 assert_se(e
= strrchr(f
, '.'));
320 if (!(r
= new(char, a
+ 1 + b
+ strlen(e
) + 1)))
323 k
= mempcpy(r
, f
, a
+ 1);
324 k
= mempcpy(k
, i
, b
);
327 if (!(r
= new(char, a
+ strlen(e
) + 1)))
330 k
= mempcpy(r
, f
, a
);
337 char *unit_name_template(const char *f
) {
342 if (!(p
= strchr(f
, '@')))
345 assert_se(e
= strrchr(f
, '.'));
348 if (!(r
= new(char, a
+ strlen(e
) + 1)))
351 strcpy(mempcpy(r
, f
, a
), e
);
356 char *unit_name_from_path(const char *path
, const char *suffix
) {
362 if (!(p
= strdup(path
)))
365 path_kill_slashes(p
);
367 path
= p
[0] == '/' ? p
+ 1 : p
;
371 return strappend("-", suffix
);
374 r
= unit_name_build_escape(path
, NULL
, suffix
);
380 char *unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
) {
386 if (!(p
= strdup(path
)))
389 path_kill_slashes(p
);
391 path
= p
[0] == '/' ? p
+ 1 : p
;
395 return unit_name_build_escape(prefix
, "-", suffix
);
398 r
= unit_name_build_escape(prefix
, path
, suffix
);
404 char *unit_name_to_path(const char *name
) {
409 if (!(w
= unit_name_to_prefix(name
)))
412 e
= unit_name_unescape(w
);
419 w
= strappend("/", e
);