]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c2f1db8f | 2 | #pragma once |
60918275 | 3 | |
84ac7bea | 4 | #include <fnmatch.h> |
15ae422b LP |
5 | #include <stdarg.h> |
6 | #include <stdbool.h> | |
11c3a366 | 7 | #include <stddef.h> |
15ae422b | 8 | |
11c3a366 | 9 | #include "alloc-util.h" |
93cc7779 | 10 | #include "extract-word.h" |
11c3a366 | 11 | #include "macro.h" |
8059aa9c | 12 | #include "string-util.h" |
1ca208fb | 13 | #include "util.h" |
60918275 | 14 | |
44a6b1b6 ZJS |
15 | char *strv_find(char **l, const char *name) _pure_; |
16 | char *strv_find_prefix(char **l, const char *name) _pure_; | |
28849dba | 17 | char *strv_find_startswith(char **l, const char *name) _pure_; |
a4bfb399 | 18 | |
33c2ce7b | 19 | char **strv_free(char **l); |
14bf2c9d | 20 | DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free); |
dfb33a97 LP |
21 | #define _cleanup_strv_free_ _cleanup_(strv_freep) |
22 | ||
ab84f5b9 ZJS |
23 | char **strv_free_erase(char **l); |
24 | DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free_erase); | |
25 | #define _cleanup_strv_free_erase_ _cleanup_(strv_free_erasep) | |
26 | ||
dd9c7723 TG |
27 | void strv_clear(char **l); |
28 | ||
b231b547 | 29 | char **strv_copy(char * const *l); |
da6053d0 | 30 | size_t strv_length(char * const *l) _pure_; |
60918275 | 31 | |
e287086b | 32 | int strv_extend_strv(char ***a, char **b, bool filter_duplicates); |
e3e45d4f | 33 | int strv_extend_strv_concat(char ***a, char **b, const char *suffix); |
5926ccca | 34 | int strv_extend(char ***l, const char *value); |
20e265d4 | 35 | int strv_extendf(char ***l, const char *format, ...) _printf_(2,0); |
4f4afc88 | 36 | int strv_extend_front(char ***l, const char *value); |
4468addc | 37 | int strv_push(char ***l, char *value); |
98940a3c | 38 | int strv_push_pair(char ***l, char *a, char *b); |
da6053d0 | 39 | int strv_insert(char ***l, size_t position, char *value); |
6e888894 ZJS |
40 | |
41 | static inline int strv_push_prepend(char ***l, char *value) { | |
42 | return strv_insert(l, 0, value); | |
43 | } | |
44 | ||
6e18964d | 45 | int strv_consume(char ***l, char *value); |
98940a3c | 46 | int strv_consume_pair(char ***l, char *a, char *b); |
9a00f57a | 47 | int strv_consume_prepend(char ***l, char *value); |
034c6ed7 | 48 | |
5f9a22c3 LP |
49 | char **strv_remove(char **l, const char *s); |
50 | char **strv_uniq(char **l); | |
e1dd6790 | 51 | bool strv_is_uniq(char **l); |
5f9a22c3 | 52 | |
0f84a72e DH |
53 | bool strv_equal(char **a, char **b); |
54 | ||
5f9a22c3 | 55 | #define strv_contains(l, s) (!!strv_find((l), (s))) |
cba8922f | 56 | |
bea1a013 | 57 | char **strv_new_internal(const char *x, ...) _sentinel_; |
b231b547 | 58 | char **strv_new_ap(const char *x, va_list ap); |
bea1a013 | 59 | #define strv_new(...) strv_new_internal(__VA_ARGS__, NULL) |
60918275 | 60 | |
f9d14060 AK |
61 | #define STRV_IGNORE ((const char *) -1) |
62 | ||
07719a21 | 63 | static inline const char* STRV_IFNOTNULL(const char *x) { |
f9d14060 | 64 | return x ? x : STRV_IGNORE; |
07719a21 LP |
65 | } |
66 | ||
2fd9ae2e | 67 | static inline bool strv_isempty(char * const *l) { |
5f9a22c3 LP |
68 | return !l || !*l; |
69 | } | |
70 | ||
8059aa9c | 71 | char **strv_split_full(const char *s, const char *separator, SplitFlags flags); |
af0b60b3 | 72 | static inline char **strv_split(const char *s, const char *separator) { |
8059aa9c | 73 | return strv_split_full(s, separator, 0); |
af0b60b3 | 74 | } |
b231b547 | 75 | char **strv_split_newlines(const char *s); |
5f9a22c3 | 76 | |
8adaf7bd | 77 | int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags); |
f88e6be5 | 78 | |
2b9a7d2e YW |
79 | char *strv_join_prefix(char **l, const char *separator, const char *prefix); |
80 | static inline char *strv_join(char **l, const char *separator) { | |
81 | return strv_join_prefix(l, separator, NULL); | |
82 | } | |
5f9a22c3 | 83 | |
21bc923a | 84 | char **strv_parse_nulstr(const char *s, size_t l); |
fabe5c0e | 85 | char **strv_split_nulstr(const char *s); |
e287086b | 86 | int strv_make_nulstr(char **l, char **p, size_t *n); |
21bc923a | 87 | |
44a6b1b6 | 88 | bool strv_overlap(char **a, char **b) _pure_; |
0c85a4f3 | 89 | |
60918275 | 90 | #define STRV_FOREACH(s, l) \ |
cba8922f | 91 | for ((s) = (l); (s) && *(s); (s)++) |
60918275 | 92 | |
4a39c774 LP |
93 | #define STRV_FOREACH_BACKWARDS(s, l) \ |
94 | for (s = ({ \ | |
95 | char **_l = l; \ | |
96 | _l ? _l + strv_length(_l) - 1U : NULL; \ | |
97 | }); \ | |
98 | (l) && ((s) >= (l)); \ | |
99 | (s)--) | |
857a493d | 100 | |
246aa6dd | 101 | #define STRV_FOREACH_PAIR(x, y, l) \ |
961e4526 | 102 | for ((x) = (l), (y) = (x+1); (x) && *(x) && *(y); (x) += 2, (y) = (x + 1)) |
246aa6dd | 103 | |
857a493d | 104 | char **strv_sort(char **l); |
7c2d8094 | 105 | void strv_print(char **l); |
250a918d | 106 | |
897e7561 LP |
107 | #define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL })) |
108 | ||
109 | #define STRV_MAKE_EMPTY ((char*[1]) { NULL }) | |
110 | ||
250a918d LP |
111 | #define strv_from_stdarg_alloca(first) \ |
112 | ({ \ | |
113 | char **_l; \ | |
114 | \ | |
115 | if (!first) \ | |
897e7561 | 116 | _l = (char**) &first; \ |
250a918d | 117 | else { \ |
da6053d0 | 118 | size_t _n; \ |
250a918d LP |
119 | va_list _ap; \ |
120 | \ | |
121 | _n = 1; \ | |
122 | va_start(_ap, first); \ | |
123 | while (va_arg(_ap, char*)) \ | |
124 | _n++; \ | |
125 | va_end(_ap); \ | |
126 | \ | |
127 | _l = newa(char*, _n+1); \ | |
128 | _l[_n = 0] = (char*) first; \ | |
129 | va_start(_ap, first); \ | |
130 | for (;;) { \ | |
131 | _l[++_n] = va_arg(_ap, char*); \ | |
132 | if (!_l[_n]) \ | |
133 | break; \ | |
134 | } \ | |
135 | va_end(_ap); \ | |
136 | } \ | |
137 | _l; \ | |
138 | }) | |
53ede806 LP |
139 | |
140 | #define STR_IN_SET(x, ...) strv_contains(STRV_MAKE(__VA_ARGS__), x) | |
c7bf9d51 ZJS |
141 | #define STRPTR_IN_SET(x, ...) \ |
142 | ({ \ | |
143 | const char* _x = (x); \ | |
144 | _x && strv_contains(STRV_MAKE(__VA_ARGS__), _x); \ | |
145 | }) | |
c4a7b2c5 | 146 | |
52f15520 LP |
147 | #define STARTSWITH_SET(p, ...) \ |
148 | ({ \ | |
149 | const char *_p = (p); \ | |
150 | char *_found = NULL, **_i; \ | |
151 | STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) { \ | |
152 | _found = startswith(_p, *_i); \ | |
153 | if (_found) \ | |
154 | break; \ | |
155 | } \ | |
156 | _found; \ | |
157 | }) | |
158 | ||
66a64081 LP |
159 | #define FOREACH_STRING(x, y, ...) \ |
160 | for (char **_l = STRV_MAKE(({ x = y; }), ##__VA_ARGS__); \ | |
161 | x; \ | |
162 | x = *(++_l)) | |
e1dd6790 LP |
163 | |
164 | char **strv_reverse(char **l); | |
04c14b25 | 165 | char **strv_shell_escape(char **l, const char *bad); |
bceccd5e | 166 | |
2404701e | 167 | bool strv_fnmatch(char* const* patterns, const char *s, int flags); |
bceccd5e | 168 | |
2404701e | 169 | static inline bool strv_fnmatch_or_empty(char* const* patterns, const char *s, int flags) { |
bceccd5e ZJS |
170 | assert(s); |
171 | return strv_isempty(patterns) || | |
2404701e | 172 | strv_fnmatch(patterns, s, flags); |
bceccd5e | 173 | } |
fe382237 LP |
174 | |
175 | char ***strv_free_free(char ***l); | |
9ecdba8c | 176 | DEFINE_TRIVIAL_CLEANUP_FUNC(char***, strv_free_free); |
e3ead6bb LP |
177 | |
178 | char **strv_skip(char **l, size_t n); | |
8dd4c05b LP |
179 | |
180 | int strv_extend_n(char ***l, const char *value, size_t n); | |
3df9bec5 LP |
181 | |
182 | int fputstrv(FILE *f, char **l, const char *separator, bool *space); | |
b302a50d LP |
183 | |
184 | #define strv_free_and_replace(a, b) \ | |
185 | ({ \ | |
186 | strv_free(a); \ | |
187 | (a) = (b); \ | |
188 | (b) = NULL; \ | |
189 | 0; \ | |
190 | }) |