]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
7584d236 | 2 | |
6553db60 | 3 | #include "alloc-util.h" |
a8fbdf54 | 4 | #include "install.h" |
1cf40697 | 5 | #include "install-printf.h" |
7584d236 | 6 | #include "specifier.h" |
6e3136b2 | 7 | #include "string-util.h" |
7584d236 | 8 | #include "unit-name.h" |
7584d236 | 9 | |
de61a04b | 10 | static int specifier_prefix_and_instance(char specifier, const void *data, const char *root, const void *userdata, char **ret) { |
0047d54d | 11 | const InstallInfo *i = ASSERT_PTR(userdata); |
cfd559c9 ZJS |
12 | _cleanup_free_ char *prefix = NULL; |
13 | int r; | |
14 | ||
cfd559c9 ZJS |
15 | r = unit_name_to_prefix_and_instance(i->name, &prefix); |
16 | if (r < 0) | |
17 | return r; | |
18 | ||
19 | if (endswith(prefix, "@") && i->default_instance) { | |
20 | char *ans; | |
21 | ||
605405c6 | 22 | ans = strjoin(prefix, i->default_instance); |
cfd559c9 ZJS |
23 | if (!ans) |
24 | return -ENOMEM; | |
25 | *ret = ans; | |
ae2a15bc LP |
26 | } else |
27 | *ret = TAKE_PTR(prefix); | |
cfd559c9 ZJS |
28 | |
29 | return 0; | |
30 | } | |
31 | ||
de61a04b | 32 | static int specifier_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) { |
0047d54d | 33 | const InstallInfo *i = ASSERT_PTR(userdata); |
19f6d710 | 34 | |
cfd559c9 ZJS |
35 | if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance) |
36 | return unit_name_replace_instance(i->name, i->default_instance, ret); | |
37 | ||
bef2c63a | 38 | return strdup_to(ret, i->name); |
7584d236 ZJS |
39 | } |
40 | ||
de61a04b | 41 | static int specifier_prefix(char specifier, const void *data, const char *root, const void *userdata, char **ret) { |
0047d54d | 42 | const InstallInfo *i = ASSERT_PTR(userdata); |
7584d236 | 43 | |
7410616c | 44 | return unit_name_to_prefix(i->name, ret); |
7584d236 ZJS |
45 | } |
46 | ||
de61a04b | 47 | static int specifier_instance(char specifier, const void *data, const char *root, const void *userdata, char **ret) { |
0047d54d | 48 | const InstallInfo *i = ASSERT_PTR(userdata); |
7584d236 ZJS |
49 | char *instance; |
50 | int r; | |
51 | ||
7584d236 ZJS |
52 | r = unit_name_to_instance(i->name, &instance); |
53 | if (r < 0) | |
19f6d710 LP |
54 | return r; |
55 | ||
6e3136b2 | 56 | if (isempty(instance)) { |
87e4e28d | 57 | r = free_and_strdup(&instance, strempty(i->default_instance)); |
402a81c7 ZJS |
58 | if (r < 0) |
59 | return r; | |
19f6d710 LP |
60 | } |
61 | ||
62 | *ret = instance; | |
63 | return 0; | |
7584d236 ZJS |
64 | } |
65 | ||
de61a04b | 66 | static int specifier_last_component(char specifier, const void *data, const char *root, const void *userdata, char **ret) { |
250e9fad ZJS |
67 | _cleanup_free_ char *prefix = NULL; |
68 | char *dash; | |
69 | int r; | |
70 | ||
601dc59b ZJS |
71 | assert(ret); |
72 | ||
de61a04b | 73 | r = specifier_prefix(specifier, data, root, userdata, &prefix); |
250e9fad ZJS |
74 | if (r < 0) |
75 | return r; | |
76 | ||
77 | dash = strrchr(prefix, '-'); | |
bef2c63a ZJS |
78 | if (dash) |
79 | return strdup_to(ret, dash + 1); | |
250e9fad | 80 | |
bef2c63a | 81 | *ret = TAKE_PTR(prefix); |
250e9fad ZJS |
82 | return 0; |
83 | } | |
84 | ||
172e9cc3 | 85 | int install_name_printf( |
4870133b | 86 | RuntimeScope scope, |
0047d54d | 87 | const InstallInfo *info, |
172e9cc3 | 88 | const char *format, |
172e9cc3 | 89 | char **ret) { |
e93387f3 | 90 | /* This is similar to unit_name_printf() */ |
7584d236 ZJS |
91 | |
92 | const Specifier table[] = { | |
e93387f3 YW |
93 | { 'i', specifier_instance, NULL }, |
94 | { 'j', specifier_last_component, NULL }, | |
cfd559c9 | 95 | { 'n', specifier_name, NULL }, |
7584d236 ZJS |
96 | { 'N', specifier_prefix_and_instance, NULL }, |
97 | { 'p', specifier_prefix, NULL }, | |
2824aa07 LP |
98 | |
99 | COMMON_SYSTEM_SPECIFIERS, | |
e93387f3 | 100 | |
172e9cc3 | 101 | COMMON_CREDS_SPECIFIERS(scope), |
6aaa8c2f | 102 | {} |
7584d236 ZJS |
103 | }; |
104 | ||
38e8a6c7 | 105 | assert(info); |
7584d236 | 106 | assert(format); |
19f6d710 | 107 | assert(ret); |
7584d236 | 108 | |
38e8a6c7 | 109 | return specifier_printf(format, UNIT_NAME_MAX, table, info->root, info, ret); |
7584d236 | 110 | } |