1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
13 #include "format-util.h"
14 #include "install-printf.h"
17 #include "specifier.h"
18 #include "string-util.h"
19 #include "unit-name.h"
20 #include "user-util.h"
22 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
23 const UnitFileInstallInfo
*i
= userdata
;
24 _cleanup_free_
char *prefix
= NULL
;
29 r
= unit_name_to_prefix_and_instance(i
->name
, &prefix
);
33 if (endswith(prefix
, "@") && i
->default_instance
) {
36 ans
= strjoin(prefix
, i
->default_instance
);
41 *ret
= TAKE_PTR(prefix
);
46 static int specifier_name(char specifier
, void *data
, void *userdata
, char **ret
) {
47 const UnitFileInstallInfo
*i
= userdata
;
52 if (unit_name_is_valid(i
->name
, UNIT_NAME_TEMPLATE
) && i
->default_instance
)
53 return unit_name_replace_instance(i
->name
, i
->default_instance
, ret
);
55 ans
= strdup(i
->name
);
62 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
63 const UnitFileInstallInfo
*i
= userdata
;
67 return unit_name_to_prefix(i
->name
, ret
);
70 static int specifier_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
71 const UnitFileInstallInfo
*i
= userdata
;
77 r
= unit_name_to_instance(i
->name
, &instance
);
81 if (isempty(instance
)) {
82 r
= free_and_strdup(&instance
, strempty(i
->default_instance
));
91 int install_full_printf(UnitFileInstallInfo
*i
, const char *format
, char **ret
) {
93 /* This is similar to unit_full_printf() but does not support
94 * anything path-related.
96 * %n: the full id of the unit (foo@bar.waldo)
97 * %N: the id of the unit without the suffix (foo@bar)
98 * %p: the prefix (foo)
99 * %i: the instance (bar)
101 * %U the UID of the running user
102 * %u the username of running user
103 * %m the machine ID of the running system
104 * %H the host name of the running system
105 * %b the boot ID of the running system
106 * %v `uname -r` of the running system
109 const Specifier table
[] = {
110 { 'n', specifier_name
, NULL
},
111 { 'N', specifier_prefix_and_instance
, NULL
},
112 { 'p', specifier_prefix
, NULL
},
113 { 'i', specifier_instance
, NULL
},
115 { 'U', specifier_user_id
, NULL
},
116 { 'u', specifier_user_name
, NULL
},
118 { 'm', specifier_machine_id
, NULL
},
119 { 'H', specifier_host_name
, NULL
},
120 { 'b', specifier_boot_id
, NULL
},
121 { 'v', specifier_kernel_release
, NULL
},
129 return specifier_printf(format
, table
, i
, ret
);