1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "format-util.h"
9 #include "install-printf.h"
12 #include "specifier.h"
13 #include "string-util.h"
14 #include "unit-name.h"
15 #include "user-util.h"
17 static int specifier_prefix_and_instance(char specifier
, const void *data
, const void *userdata
, char **ret
) {
18 const UnitFileInstallInfo
*i
= userdata
;
19 _cleanup_free_
char *prefix
= NULL
;
24 r
= unit_name_to_prefix_and_instance(i
->name
, &prefix
);
28 if (endswith(prefix
, "@") && i
->default_instance
) {
31 ans
= strjoin(prefix
, i
->default_instance
);
36 *ret
= TAKE_PTR(prefix
);
41 static int specifier_name(char specifier
, const void *data
, const void *userdata
, char **ret
) {
42 const UnitFileInstallInfo
*i
= userdata
;
47 if (unit_name_is_valid(i
->name
, UNIT_NAME_TEMPLATE
) && i
->default_instance
)
48 return unit_name_replace_instance(i
->name
, i
->default_instance
, ret
);
50 ans
= strdup(i
->name
);
57 static int specifier_prefix(char specifier
, const void *data
, const void *userdata
, char **ret
) {
58 const UnitFileInstallInfo
*i
= userdata
;
62 return unit_name_to_prefix(i
->name
, ret
);
65 static int specifier_instance(char specifier
, const void *data
, const void *userdata
, char **ret
) {
66 const UnitFileInstallInfo
*i
= userdata
;
72 r
= unit_name_to_instance(i
->name
, &instance
);
76 if (isempty(instance
)) {
77 r
= free_and_strdup(&instance
, strempty(i
->default_instance
));
86 static int specifier_last_component(char specifier
, const void *data
, const void *userdata
, char **ret
) {
87 _cleanup_free_
char *prefix
= NULL
;
91 r
= specifier_prefix(specifier
, data
, userdata
, &prefix
);
95 dash
= strrchr(prefix
, '-');
97 dash
= strdup(dash
+ 1);
102 *ret
= TAKE_PTR(prefix
);
107 int install_full_printf(const UnitFileInstallInfo
*i
, const char *format
, char **ret
) {
108 /* This is similar to unit_full_printf() but does not support
109 * anything path-related.
111 * %n: the full id of the unit (foo@bar.waldo)
112 * %N: the id of the unit without the suffix (foo@bar)
113 * %p: the prefix (foo)
114 * %i: the instance (bar)
116 * %U the UID of the running user
117 * %u the username of running user
118 * %m the machine ID of the running system
119 * %H the host name of the running system
120 * %b the boot ID of the running system
121 * %v `uname -r` of the running system
124 const Specifier table
[] = {
125 { 'n', specifier_name
, NULL
},
126 { 'N', specifier_prefix_and_instance
, NULL
},
127 { 'p', specifier_prefix
, NULL
},
128 { 'i', specifier_instance
, NULL
},
129 { 'j', specifier_last_component
, NULL
},
131 { 'g', specifier_group_name
, NULL
},
132 { 'G', specifier_group_id
, NULL
},
133 { 'U', specifier_user_id
, NULL
},
134 { 'u', specifier_user_name
, NULL
},
136 { 'm', specifier_machine_id
, NULL
},
137 { 'H', specifier_host_name
, NULL
},
138 { 'b', specifier_boot_id
, NULL
},
139 { 'v', specifier_kernel_release
, NULL
},
147 return specifier_printf(format
, table
, i
, ret
);