chiark / gitweb /
service: prefix description with LSB only if script has LSB header, use 'SysV:' otherwise
[elogind.git] / src / unit-name.c
index 2e2948ab1d1f13ed701ee4e793ef5f1e0b90347b..be4e73edccb0e3ae5561977aea456564626c1d69 100644 (file)
@@ -21,8 +21,9 @@
 
 #include <errno.h>
 #include <string.h>
+#include <assert.h>
 
-#include "unit.h"
+#include "util.h"
 #include "unit-name.h"
 
 #define VALID_CHARS                             \
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"            \
         ":-_.\\"
 
-UnitType unit_name_to_type(const char *n) {
-        UnitType t;
-
-        assert(n);
-
-        for (t = 0; t < _UNIT_TYPE_MAX; t++)
-                if (endswith(n, unit_vtable[t]->suffix))
-                        return t;
-
-        return _UNIT_TYPE_INVALID;
-}
-
-bool unit_name_is_valid(const char *n) {
-        UnitType t;
+bool unit_name_is_valid_no_type(const char *n, bool template_ok) {
         const char *e, *i, *at;
 
         /* Valid formats:
@@ -58,13 +46,8 @@ bool unit_name_is_valid(const char *n) {
         if (strlen(n) >= UNIT_NAME_MAX)
                 return false;
 
-        t = unit_name_to_type(n);
-        if (t < 0 || t >= _UNIT_TYPE_MAX)
-                return false;
-
-        assert_se(e = strrchr(n, '.'));
-
-        if (e == n)
+        e = strrchr(n, '.');
+        if (!e || e == n)
                 return false;
 
         for (i = n, at = NULL; i < e; i++) {
@@ -80,7 +63,7 @@ bool unit_name_is_valid(const char *n) {
                 if (at == n)
                         return false;
 
-                if (at[1] == '.')
+                if (!template_ok && at+1 == e)
                         return false;
         }
 
@@ -167,7 +150,7 @@ char *unit_name_change_suffix(const char *n, const char *suffix) {
         size_t a, b;
 
         assert(n);
-        assert(unit_name_is_valid(n));
+        assert(unit_name_is_valid_no_type(n, true));
         assert(suffix);
 
         assert_se(e = strrchr(n, '.'));
@@ -190,7 +173,6 @@ char *unit_name_build(const char *prefix, const char *instance, const char *suff
         assert(unit_prefix_is_valid(prefix));
         assert(!instance || unit_instance_is_valid(instance));
         assert(suffix);
-        assert(unit_name_to_type(suffix) >= 0);
 
         if (!instance)
                 return strappend(prefix, suffix);
@@ -211,7 +193,7 @@ static char* do_escape(const char *f, char *t) {
                 else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
                         *(t++) = '\\';
                         *(t++) = 'x';
-                        *(t++) = hexchar(*f > 4);
+                        *(t++) = hexchar(*f >> 4);
                         *(t++) = hexchar(*f);
                 } else
                         *(t++) = *f;
@@ -226,13 +208,12 @@ char *unit_name_build_escape(const char *prefix, const char *instance, const cha
 
         assert(prefix);
         assert(suffix);
-        assert(unit_name_to_type(suffix) >= 0);
 
         /* Takes a arbitrary string for prefix and instance plus a
          * suffix and makes a nice string suitable as unit name of it,
          * escaping all weird chars on the way.
          *
-         * / becomes ., and all chars not alloweed in a unit name get
+         * / becomes ., and all chars not allowed in a unit name get
          * escaped as \xFF, including \ and ., of course. This
          * escaping is hence reversible.
          *
@@ -291,13 +272,13 @@ char *unit_name_unescape(const char *f) {
                 else if (*f == '\\') {
                         int a, b;
 
-                        if ((a = unhexchar(f[1])) < 0 ||
-                            (b = unhexchar(f[2])) < 0) {
-                                /* Invalid escape code, let's take it literal then */
+                        if (f[1] != 'x' || (a = unhexchar(f[2])) < 0 ||
+                                       (b = unhexchar(f[3])) < 0) {
+                               /* Invalid escape code, let's take it literal then */
                                 *(t++) = '\\';
                         } else {
                                 *(t++) = (char) ((a << 4) | b);
-                                f += 2;
+                                f += 3;
                         }
                 } else
                         *(t++) = *f;
@@ -396,6 +377,30 @@ char *unit_name_from_path(const char *path, const char *suffix) {
         return r;
 }
 
+char *unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix) {
+        char *p, *r;
+
+        assert(path);
+        assert(suffix);
+
+        if (!(p = strdup(path)))
+                return NULL;
+
+        path_kill_slashes(p);
+
+        path = p[0] == '/' ? p + 1 : p;
+
+        if (path[0] == 0) {
+                free(p);
+                return unit_name_build_escape(prefix, "-", suffix);
+        }
+
+        r = unit_name_build_escape(prefix, path, suffix);
+        free(p);
+
+        return r;
+}
+
 char *unit_name_to_path(const char *name) {
         char *w, *e;
 
@@ -422,3 +427,26 @@ char *unit_name_to_path(const char *name) {
 
         return e;
 }
+
+char *unit_name_path_unescape(const char *f) {
+        char *e;
+
+        assert(f);
+
+        if (!(e = unit_name_unescape(f)))
+                return NULL;
+
+        if (e[0] != '/') {
+                char *w;
+
+                w = strappend("/", e);
+                free(e);
+
+                if (!w)
+                        return NULL;
+
+                e = w;
+        }
+
+        return e;
+}