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 int 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);
56 static int parse_argv(int argc, char *argv[]) {
64 static const struct option options[] = {
65 { "help", no_argument, NULL, 'h' },
66 { "version", no_argument, NULL, ARG_VERSION },
67 { "suffix", required_argument, NULL, ARG_SUFFIX },
68 { "template", required_argument, NULL, ARG_TEMPLATE },
69 { "unescape", no_argument, NULL, 'u' },
70 { "mangle", no_argument, NULL, 'm' },
71 { "path", no_argument, NULL, 'p' },
80 while ((c = getopt_long(argc, argv, "hump", options, NULL)) >= 0) {
89 puts(SYSTEMD_FEATURES);
94 if (unit_type_from_string(optarg) < 0) {
95 log_error("Invalid unit suffix type %s.", optarg);
104 if (!unit_name_is_valid(optarg, true) || !unit_name_is_template(optarg)) {
105 log_error("Template name %s is not valid.", optarg);
109 arg_template = optarg;
113 arg_action = ACTION_UNESCAPE;
117 arg_action = ACTION_MANGLE;
128 assert_not_reached("Unhandled option");
132 if (optind >= argc) {
133 log_error("Not enough arguments.");
137 if (arg_template && arg_suffix) {
138 log_error("--suffix= and --template= may not be combined.");
142 if ((arg_template || arg_suffix) && arg_action != ACTION_ESCAPE) {
143 log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
147 if (arg_path && !IN_SET(arg_action, ACTION_ESCAPE, ACTION_UNESCAPE)) {
148 log_error("--path may not be combined with --mangle.");
155 int main(int argc, char *argv[]) {
159 log_parse_environment();
162 r = parse_argv(argc, argv);
166 STRV_FOREACH(i, argv + optind) {
167 _cleanup_free_ char *e = NULL;
169 switch (arg_action) {
173 e = unit_name_path_escape(*i);
175 e = unit_name_escape(*i);
185 x = unit_name_replace_instance(arg_template, e);
193 } else if (arg_suffix) {
196 x = strjoin(e, ".", arg_suffix, NULL);
208 case ACTION_UNESCAPE:
210 e = unit_name_path_unescape(*i);
212 e = unit_name_unescape(*i);
221 e = unit_name_mangle(*i, MANGLE_NOGLOB);
229 if (i != argv+optind)
238 return r ? EXIT_FAILURE : EXIT_SUCCESS;