43ff24c
From ee228789816679b6fff19c7c2f637eb0a1a3fcc4 Mon Sep 17 00:00:00 2001
43ff24c
From: Lennart Poettering <lennart@poettering.net>
43ff24c
Date: Mon, 7 Jul 2014 22:23:00 +0200
43ff24c
Subject: [PATCH] escape: beef up new systemd-escape tool
43ff24c
43ff24c
Add various options for making it easy unescape, or mangle, or format as
43ff24c
template instance or append a suffix.
43ff24c
43ff24c
(cherry picked from commit a1948c7bfeb87b54bc7715a44490c01593ee6e23)
43ff24c
43ff24c
Conflicts:
43ff24c
	.gitignore
43ff24c
---
43ff24c
 .gitignore          |   1 +
43ff24c
 src/escape/Makefile |   1 +
43ff24c
 src/escape/escape.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++---
43ff24c
 3 files changed, 206 insertions(+), 11 deletions(-)
43ff24c
 create mode 120000 src/escape/Makefile
43ff24c
43ff24c
diff --git a/.gitignore b/.gitignore
43ff24c
index 9523ea027e..e08aa52aee 100644
43ff24c
--- a/.gitignore
43ff24c
+++ b/.gitignore
43ff24c
@@ -64,6 +64,7 @@
43ff24c
 /systemd-delta
43ff24c
 /systemd-detect-virt
43ff24c
 /systemd-efi-boot-generator
43ff24c
+/systemd-escape
43ff24c
 /systemd-fsck
43ff24c
 /systemd-fstab-generator
43ff24c
 /systemd-getty-generator
43ff24c
diff --git a/src/escape/Makefile b/src/escape/Makefile
43ff24c
new file mode 120000
43ff24c
index 0000000000..d0b0e8e008
43ff24c
--- /dev/null
43ff24c
+++ b/src/escape/Makefile
43ff24c
@@ -0,0 +1 @@
43ff24c
+../Makefile
43ff24c
\ No newline at end of file
43ff24c
diff --git a/src/escape/escape.c b/src/escape/escape.c
43ff24c
index 0a59a05e28..ae0c183eca 100644
43ff24c
--- a/src/escape/escape.c
43ff24c
+++ b/src/escape/escape.c
43ff24c
@@ -21,26 +21,219 @@
43ff24c
 
43ff24c
 #include <stdio.h>
43ff24c
 #include <stdlib.h>
43ff24c
+#include <getopt.h>
43ff24c
 
43ff24c
 #include "log.h"
43ff24c
 #include "unit-name.h"
43ff24c
+#include "build.h"
43ff24c
+#include "strv.h"
43ff24c
 
43ff24c
-int main(int argc, char *argv[]) {
43ff24c
-        char *escaped_name = NULL;
43ff24c
+static enum {
43ff24c
+        ACTION_ESCAPE,
43ff24c
+        ACTION_UNESCAPE,
43ff24c
+        ACTION_MANGLE
43ff24c
+} arg_action = ACTION_ESCAPE;
43ff24c
+static const char *arg_suffix = NULL;
43ff24c
+static const char *arg_template = NULL;
43ff24c
+static bool arg_path = false;
43ff24c
+
43ff24c
+static int help(void) {
43ff24c
+
43ff24c
+        printf("%s [OPTIONS...] [NAME...]\n\n"
43ff24c
+               "Show system and user paths.\n\n"
43ff24c
+               "  -h --help               Show this help\n"
43ff24c
+               "     --version            Show package version\n"
43ff24c
+               "     --suffix=SUFFIX      Unit suffix to append to escaped strings\n"
43ff24c
+               "     --template=TEMPLATE  Insert strings as instance into template\n"
43ff24c
+               "  -u --unescape           Unescape strings\n"
43ff24c
+               "  -m --mangle             Mangle strings\n"
43ff24c
+               "  -p --path               When escaping/unescaping assume the string is a path\n",
43ff24c
+               program_invocation_short_name);
43ff24c
+
43ff24c
+        return 0;
43ff24c
+}
43ff24c
+
43ff24c
+static int parse_argv(int argc, char *argv[]) {
43ff24c
+
43ff24c
+        enum {
43ff24c
+                ARG_VERSION = 0x100,
43ff24c
+                ARG_SUFFIX,
43ff24c
+                ARG_TEMPLATE
43ff24c
+        };
43ff24c
+
43ff24c
+        static const struct option options[] = {
43ff24c
+                { "help",      no_argument,       NULL, 'h'           },
43ff24c
+                { "version",   no_argument,       NULL, ARG_VERSION   },
43ff24c
+                { "suffix",    required_argument, NULL, ARG_SUFFIX    },
43ff24c
+                { "template",  required_argument, NULL, ARG_TEMPLATE  },
43ff24c
+                { "unescape",  no_argument,       NULL, 'u'           },
43ff24c
+                { "mangle",    no_argument,       NULL, 'm'           },
43ff24c
+                { "path",      no_argument,       NULL, 'p'           },
43ff24c
+                {}
43ff24c
+        };
43ff24c
+
43ff24c
+        int c;
43ff24c
+
43ff24c
+        assert(argc >= 0);
43ff24c
+        assert(argv);
43ff24c
+
43ff24c
+        while ((c = getopt_long(argc, argv, "hump", options, NULL)) >= 0) {
43ff24c
+
43ff24c
+                switch (c) {
43ff24c
+
43ff24c
+                case 'h':
43ff24c
+                        return help();
43ff24c
+
43ff24c
+                case ARG_VERSION:
43ff24c
+                        puts(PACKAGE_STRING);
43ff24c
+                        puts(SYSTEMD_FEATURES);
43ff24c
+                        return 0;
43ff24c
+
43ff24c
+                case ARG_SUFFIX:
43ff24c
+
43ff24c
+                        if (unit_type_from_string(optarg) < 0) {
43ff24c
+                                log_error("Invalid unit suffix type %s.", optarg);
43ff24c
+                                return -EINVAL;
43ff24c
+                        }
43ff24c
+
43ff24c
+                        arg_suffix = optarg;
43ff24c
+                        break;
43ff24c
+
43ff24c
+                case ARG_TEMPLATE:
43ff24c
+
43ff24c
+                        if (!unit_name_is_valid(optarg, true) || !unit_name_is_template(optarg)) {
43ff24c
+                                log_error("Template name %s is not valid.", optarg);
43ff24c
+                                return -EINVAL;
43ff24c
+                        }
43ff24c
+
43ff24c
+                        arg_template = optarg;
43ff24c
+                        break;
43ff24c
+
43ff24c
+                case 'u':
43ff24c
+                        arg_action = ACTION_UNESCAPE;
43ff24c
+                        break;
43ff24c
+
43ff24c
+                case 'm':
43ff24c
+                        arg_action = ACTION_MANGLE;
43ff24c
+                        break;
43ff24c
+
43ff24c
+                case 'p':
43ff24c
+                        arg_path = true;
43ff24c
+                        break;
43ff24c
+
43ff24c
+                case '?':
43ff24c
+                        return -EINVAL;
43ff24c
+
43ff24c
+                default:
43ff24c
+                        assert_not_reached("Unhandled option");
43ff24c
+                }
43ff24c
+        }
43ff24c
 
43ff24c
-        if (argc != 2) {
43ff24c
-                log_error("This program requires on argument.");
43ff24c
-                return EXIT_FAILURE;
43ff24c
+        if (optind >= argc) {
43ff24c
+                log_error("Not enough arguments.");
43ff24c
+                return -EINVAL;
43ff24c
         }
43ff24c
 
43ff24c
-        escaped_name = unit_name_escape(argv[1]);
43ff24c
+        if (arg_template && arg_suffix) {
43ff24c
+                log_error("--suffix= and --template= may not be combined.");
43ff24c
+                return -EINVAL;
43ff24c
+        }
43ff24c
+
43ff24c
+        if ((arg_template || arg_suffix) && arg_action != ACTION_ESCAPE) {
43ff24c
+                log_error("--suffix= and --template= are not compatible with --unescape or --mangle.");
43ff24c
+                return -EINVAL;
43ff24c
+        }
43ff24c
+
43ff24c
+        if (arg_path && !IN_SET(arg_action, ACTION_ESCAPE, ACTION_UNESCAPE)) {
43ff24c
+                log_error("--path may not be combined with --mangle.");
43ff24c
+                return -EINVAL;
43ff24c
+        }
43ff24c
+
43ff24c
+        return 1;
43ff24c
+}
43ff24c
+
43ff24c
+int main(int argc, char *argv[]) {
43ff24c
+        char **i;
43ff24c
+        int r;
43ff24c
+
43ff24c
+        log_parse_environment();
43ff24c
+        log_open();
43ff24c
+
43ff24c
+        r = parse_argv(argc, argv);
43ff24c
+        if (r <= 0)
43ff24c
+                goto finish;
43ff24c
+
43ff24c
+        STRV_FOREACH(i, argv + optind) {
43ff24c
+                _cleanup_free_ char *e = NULL;
43ff24c
+
43ff24c
+                switch (arg_action) {
43ff24c
+
43ff24c
+                case ACTION_ESCAPE:
43ff24c
+                        if (arg_path)
43ff24c
+                                e = unit_name_path_escape(*i);
43ff24c
+                        else
43ff24c
+                                e = unit_name_escape(*i);
43ff24c
+
43ff24c
+                        if (!e) {
43ff24c
+                                r = log_oom();
43ff24c
+                                goto finish;
43ff24c
+                        }
43ff24c
+
43ff24c
+                        if (arg_template) {
43ff24c
+                                char *x;
43ff24c
+
43ff24c
+                                x = unit_name_replace_instance(arg_template, e);
43ff24c
+                                if (!x) {
43ff24c
+                                        r = log_oom();
43ff24c
+                                        goto finish;
43ff24c
+                                }
43ff24c
+
43ff24c
+                                free(e);
43ff24c
+                                e = x;
43ff24c
+                        } else if (arg_suffix) {
43ff24c
+                                char *x;
43ff24c
+
43ff24c
+                                x = strjoin(e, ".", arg_suffix, NULL);
43ff24c
+                                if (!x) {
43ff24c
+                                        r = log_oom();
43ff24c
+                                        goto finish;
43ff24c
+                                }
43ff24c
+
43ff24c
+                                free(e);
43ff24c
+                                e = x;
43ff24c
+                        }
43ff24c
+
43ff24c
+                        break;
43ff24c
+
43ff24c
+                case ACTION_UNESCAPE:
43ff24c
+                        if (arg_path)
43ff24c
+                                e = unit_name_path_unescape(*i);
43ff24c
+                        else
43ff24c
+                                e = unit_name_unescape(*i);
43ff24c
+
43ff24c
+                        if (!e) {
43ff24c
+                                r = log_oom();
43ff24c
+                                goto finish;
43ff24c
+                        }
43ff24c
+                        break;
43ff24c
+
43ff24c
+                case ACTION_MANGLE:
43ff24c
+                        e = unit_name_mangle(*i, MANGLE_NOGLOB);
43ff24c
+                        if (!e) {
43ff24c
+                                r = log_oom();
43ff24c
+                                goto finish;
43ff24c
+                        }
43ff24c
+                        break;
43ff24c
+                }
43ff24c
+
43ff24c
+                if (i != argv+optind)
43ff24c
+                        fputc(' ', stdout);
43ff24c
 
43ff24c
-        if (!escaped_name) {
43ff24c
-                log_error("Failed to escape name.");
43ff24c
-                return EXIT_FAILURE;
43ff24c
+                fputs(e, stdout);
43ff24c
         }
43ff24c
 
43ff24c
-        printf("%s", escaped_name);
43ff24c
+        fputc('\n', stdout);
43ff24c
 
43ff24c
-        return EXIT_SUCCESS;
43ff24c
+finish:
43ff24c
+        return r ? EXIT_FAILURE : EXIT_SUCCESS;
43ff24c
 }