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