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/>.
26 #include "alloc-util.h"
28 #include "string-util.h"
30 #include "unit-name.h"
36 } arg_action
= ACTION_ESCAPE
;
37 static const char *arg_suffix
= NULL
;
38 static const char *arg_template
= NULL
;
39 static bool arg_path
= false;
41 static void help(void) {
42 printf("%s [OPTIONS...] [NAME...]\n\n"
43 "Show system and user paths.\n\n"
44 " -h --help Show this help\n"
45 " --version Show package version\n"
46 " --suffix=SUFFIX Unit suffix to append to escaped strings\n"
47 " --template=TEMPLATE Insert strings as instance into template\n"
48 " -u --unescape Unescape strings\n"
49 " -m --mangle Mangle strings\n"
50 " -p --path When escaping/unescaping assume the string is a path\n"
51 , program_invocation_short_name
);
54 static int parse_argv(int argc
, char *argv
[]) {
62 static const struct option options
[] = {
63 { "help", no_argument
, NULL
, 'h' },
64 { "version", no_argument
, NULL
, ARG_VERSION
},
65 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
66 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
67 { "unescape", no_argument
, NULL
, 'u' },
68 { "mangle", no_argument
, NULL
, 'm' },
69 { "path", no_argument
, NULL
, 'p' },
78 while ((c
= getopt_long(argc
, argv
, "hump", options
, NULL
)) >= 0)
91 if (unit_type_from_string(optarg
) < 0) {
92 log_error("Invalid unit suffix type %s.", optarg
);
101 if (!unit_name_is_valid(optarg
, UNIT_NAME_TEMPLATE
)) {
102 log_error("Template name %s is not valid.", optarg
);
106 arg_template
= optarg
;
110 arg_action
= ACTION_UNESCAPE
;
114 arg_action
= ACTION_MANGLE
;
125 assert_not_reached("Unhandled option");
128 if (optind
>= argc
) {
129 log_error("Not enough arguments.");
133 if (arg_template
&& arg_suffix
) {
134 log_error("--suffix= and --template= may not be combined.");
138 if ((arg_template
|| arg_suffix
) && arg_action
!= ACTION_ESCAPE
) {
139 log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
143 if (arg_path
&& !IN_SET(arg_action
, ACTION_ESCAPE
, ACTION_UNESCAPE
)) {
144 log_error("--path may not be combined with --mangle.");
151 int main(int argc
, char *argv
[]) {
155 log_parse_environment();
158 r
= parse_argv(argc
, argv
);
162 STRV_FOREACH(i
, argv
+ optind
) {
163 _cleanup_free_
char *e
= NULL
;
165 switch (arg_action
) {
169 r
= unit_name_path_escape(*i
, &e
);
171 log_error_errno(r
, "Failed to escape string: %m");
175 e
= unit_name_escape(*i
);
185 r
= unit_name_replace_instance(arg_template
, e
, &x
);
187 log_error_errno(r
, "Failed to replace instance: %m");
193 } else if (arg_suffix
) {
196 x
= strjoin(e
, ".", arg_suffix
, NULL
);
208 case ACTION_UNESCAPE
:
210 r
= unit_name_path_unescape(*i
, &e
);
212 r
= unit_name_unescape(*i
, &e
);
215 log_error_errno(r
, "Failed to unescape string: %m");
221 r
= unit_name_mangle(*i
, UNIT_NAME_NOGLOB
, &e
);
223 log_error_errno(r
, "Failed to mangle name: %m");
229 if (i
!= argv
+optind
)
238 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;