1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Michael Biebl
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "unit-name.h"
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;
40 static void help(void) {
41 printf("%s [OPTIONS...] [NAME...]\n\n"
42 "Show system and user paths.\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
);
53 static int parse_argv(int argc
, char *argv
[]) {
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' },
77 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
87 puts(SYSTEMD_FEATURES
);
92 if (unit_type_from_string(optarg
) < 0) {
93 log_error("Invalid unit suffix type %s.", optarg
);
102 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
)) {
103 log_error("Template name %s is not valid.", optarg
);
107 arg_template
= optarg
;
111 arg_action
= ACTION_UNESCAPE
;
115 arg_action
= ACTION_MANGLE
;
126 assert_not_reached("Unhandled option");
129 if (optind
>= argc
) {
130 log_error("Not enough arguments.");
134 if (arg_template
&& arg_suffix
) {
135 log_error("--suffix= and --template= may not be combined.");
139 if ((arg_template
|| arg_suffix
) && arg_action
!= ACTION_ESCAPE
) {
140 log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
144 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
145 log_error("--path may not be combined with --mangle.");
152 int main(int argc
, char *argv
[]) {
156 log_parse_environment();
159 r
= parse_argv(argc
, argv
);
163 STRV_FOREACH(i
, argv
+ optind
) {
164 _cleanup_free_
char *e
= NULL
;
166 switch (arg_action
) {
170 r
= unit_name_path_escape(*i
, &e
);
172 log_error_errno(r
, "Failed to escape string: %m");
176 e
= unit_name_escape(*i
);
186 r
= unit_name_replace_instance(arg_template
, e
, &x
);
188 log_error_errno(r
, "Failed to replace instance: %m");
194 } else if (arg_suffix
) {
197 x
= strjoin(e
, ".", arg_suffix
, NULL
);
209 case ACTION_UNESCAPE
:
211 r
= unit_name_path_unescape(*i
, &e
);
213 r
= unit_name_unescape(*i
, &e
);
216 log_error_errno(r
, "Failed to unescape string: %m");
222 r
= unit_name_mangle(*i
, UNIT_NAME_NOGLOB
, &e
);
224 log_error_errno(r
, "Failed to mangle name: %m");
230 if (i
!= argv
+optind
)
239 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;