chiark / gitweb /
install: allow specifiers in WantedBy/RequiredBy/Alias
[elogind.git] / src / shared / install-printf.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Zbigniew JÄ™drzejewski-Szmek
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <assert.h>
23 #include <stdlib.h>
24
25 #include "specifier.h"
26 #include "unit-name.h"
27 #include "install-printf.h"
28
29 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
30         InstallInfo *i = userdata;
31         assert(i);
32
33         return unit_name_to_prefix_and_instance(i->name);
34 }
35
36 static char *specifier_prefix(char specifier, void *data, void *userdata) {
37         InstallInfo *i = userdata;
38         assert(i);
39
40         return unit_name_to_prefix(i->name);
41 }
42
43 static char *specifier_instance(char specifier, void *data, void *userdata) {
44         InstallInfo *i = userdata;
45         char *instance;
46         int r;
47
48         assert(i);
49
50         r = unit_name_to_instance(i->name, &instance);
51         if (r < 0)
52                 return NULL;
53         if (instance != NULL)
54                 return instance;
55         else
56                 return strdup("");
57 }
58
59 char *install_full_printf(InstallInfo *i, const char *format) {
60
61         /* This is similar to unit_full_printf() but does not support
62          * anything path-related.
63          *
64          * %n: the full id of the unit                 (foo@bar.waldo)
65          * %N: the id of the unit without the suffix   (foo@bar)
66          * %p: the prefix                              (foo)
67          * %i: the instance                            (bar)
68
69          * %U the UID of the configured user or running user
70          * %u the username of the configured user or running user
71          * %m the machine ID of the running system
72          * %H the host name of the running system
73          * %b the boot ID of the running system
74          */
75
76         const Specifier table[] = {
77                 { 'n', specifier_string,              i->name },
78                 { 'N', specifier_prefix_and_instance, NULL },
79                 { 'p', specifier_prefix,              NULL },
80                 { 'i', specifier_instance,            NULL },
81
82 //                { 'U', specifier_user_name,           NULL },
83 //                { 'u', specifier_user_name,           NULL },
84
85                 { 'm', specifier_machine_id,          NULL },
86                 { 'H', specifier_host_name,           NULL },
87                 { 'b', specifier_boot_id,             NULL },
88                 { 0, NULL, NULL }
89         };
90
91         assert(i);
92         assert(format);
93
94         return specifier_printf(format, table, i);
95 }