2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-07-18 19:07:51 +02:00
|
|
|
#pragma once
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2015-10-22 20:12:31 +02:00
|
|
|
#include <fnmatch.h>
|
2010-04-21 22:15:06 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <stddef.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <stdio.h>
|
2010-04-21 22:15:06 +02:00
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include "alloc-util.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "extract-word.h"
|
2019-03-28 17:28:48 +01:00
|
|
|
#include "hashmap.h"
|
2015-11-30 21:43:37 +01:00
|
|
|
#include "macro.h"
|
2018-09-26 15:17:40 +02:00
|
|
|
#include "string-util.h"
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2019-12-16 07:51:04 +01:00
|
|
|
char *strv_find(char * const *l, const char *name) _pure_;
|
2020-04-30 15:07:45 +02:00
|
|
|
char *strv_find_case(char * const *l, const char *name) _pure_;
|
2019-12-16 07:51:04 +01:00
|
|
|
char *strv_find_prefix(char * const *l, const char *name) _pure_;
|
|
|
|
char *strv_find_startswith(char * const *l, const char *name) _pure_;
|
2010-06-16 05:06:02 +02:00
|
|
|
|
2020-04-30 15:07:45 +02:00
|
|
|
#define strv_contains(l, s) (!!strv_find((l), (s)))
|
|
|
|
#define strv_contains_case(l, s) (!!strv_find_case((l), (s)))
|
|
|
|
|
2015-03-17 12:20:31 +01:00
|
|
|
char **strv_free(char **l);
|
2013-10-14 04:59:26 +02:00
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
|
2013-04-16 05:25:57 +02:00
|
|
|
#define _cleanup_strv_free_ _cleanup_(strv_freep)
|
|
|
|
|
2015-10-15 16:02:35 +02:00
|
|
|
char **strv_free_erase(char **l);
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free_erase);
|
|
|
|
#define _cleanup_strv_free_erase_ _cleanup_(strv_free_erasep)
|
|
|
|
|
2013-04-26 01:47:43 +02:00
|
|
|
char **strv_copy(char * const *l);
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
size_t strv_length(char * const *l) _pure_;
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2019-12-16 07:51:04 +01:00
|
|
|
int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates);
|
|
|
|
int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix);
|
2020-10-21 18:14:37 +02:00
|
|
|
int strv_prepend(char ***l, const char *value);
|
2013-01-11 01:04:11 +01:00
|
|
|
int strv_extend(char ***l, const char *value);
|
2014-03-06 20:28:32 +01:00
|
|
|
int strv_extendf(char ***l, const char *format, ...) _printf_(2,0);
|
2016-04-07 15:43:59 +02:00
|
|
|
int strv_extend_front(char ***l, const char *value);
|
2013-03-22 17:44:15 +01:00
|
|
|
int strv_push(char ***l, char *value);
|
2014-12-03 18:31:51 +01:00
|
|
|
int strv_push_pair(char ***l, char *a, char *b);
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
int strv_insert(char ***l, size_t position, char *value);
|
2018-02-01 12:50:18 +01:00
|
|
|
|
|
|
|
static inline int strv_push_prepend(char ***l, char *value) {
|
|
|
|
return strv_insert(l, 0, value);
|
|
|
|
}
|
|
|
|
|
2014-03-04 15:20:51 +01:00
|
|
|
int strv_consume(char ***l, char *value);
|
2014-12-03 18:31:51 +01:00
|
|
|
int strv_consume_pair(char ***l, char *a, char *b);
|
2014-07-02 12:23:36 +02:00
|
|
|
int strv_consume_prepend(char ***l, char *value);
|
2010-01-26 04:18:44 +01:00
|
|
|
|
2010-02-13 01:04:44 +01:00
|
|
|
char **strv_remove(char **l, const char *s);
|
|
|
|
char **strv_uniq(char **l);
|
2019-12-16 07:51:04 +01:00
|
|
|
bool strv_is_uniq(char * const *l);
|
2010-02-13 01:04:44 +01:00
|
|
|
|
2020-01-14 10:29:50 +01:00
|
|
|
int strv_compare(char * const *a, char * const *b);
|
|
|
|
static inline bool strv_equal(char * const *a, char * const *b) {
|
|
|
|
return strv_compare(a, b) == 0;
|
|
|
|
}
|
2014-11-27 16:08:46 +01:00
|
|
|
|
2018-10-31 17:03:50 +01:00
|
|
|
char **strv_new_internal(const char *x, ...) _sentinel_;
|
2013-04-26 01:47:43 +02:00
|
|
|
char **strv_new_ap(const char *x, va_list ap);
|
2018-10-31 17:03:50 +01:00
|
|
|
#define strv_new(...) strv_new_internal(__VA_ARGS__, NULL)
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2020-10-21 14:31:54 +02:00
|
|
|
#define STRV_IGNORE ((const char *) POINTER_MAX)
|
2016-06-27 23:26:07 +02:00
|
|
|
|
2012-05-23 03:43:29 +02:00
|
|
|
static inline const char* STRV_IFNOTNULL(const char *x) {
|
2016-06-27 23:26:07 +02:00
|
|
|
return x ? x : STRV_IGNORE;
|
2012-05-23 03:43:29 +02:00
|
|
|
}
|
|
|
|
|
2013-03-25 02:30:32 +01:00
|
|
|
static inline bool strv_isempty(char * const *l) {
|
2010-02-13 01:04:44 +01:00
|
|
|
return !l || !*l;
|
|
|
|
}
|
|
|
|
|
2013-04-26 01:47:43 +02:00
|
|
|
char **strv_split_newlines(const char *s);
|
2010-02-13 01:04:44 +01:00
|
|
|
|
2020-08-03 17:52:01 +02:00
|
|
|
int strv_split_full(char ***t, const char *s, const char *separators, ExtractFlags flags);
|
2020-07-31 16:21:14 +02:00
|
|
|
static inline char **strv_split(const char *s, const char *separators) {
|
|
|
|
char **ret;
|
|
|
|
int r;
|
|
|
|
|
2020-08-03 17:52:01 +02:00
|
|
|
r = strv_split_full(&ret, s, separators, 0);
|
2020-07-31 16:21:14 +02:00
|
|
|
if (r < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-10 23:44:34 +01:00
|
|
|
|
2020-07-07 18:12:48 +02:00
|
|
|
/* Given a string containing white-space separated tuples of words themselves separated by ':',
|
|
|
|
* returns a vector of strings. If the second element in a tuple is missing, the corresponding
|
|
|
|
* string in the vector is an empty string. */
|
|
|
|
int strv_split_colon_pairs(char ***t, const char *s);
|
|
|
|
|
2020-09-24 13:06:52 +02:00
|
|
|
char *strv_join_full(char * const *l, const char *separator, const char *prefix, bool escape_separtor);
|
2019-12-16 07:51:04 +01:00
|
|
|
static inline char *strv_join(char * const *l, const char *separator) {
|
2020-09-24 13:06:52 +02:00
|
|
|
return strv_join_full(l, separator, NULL, false);
|
2018-09-11 06:22:31 +02:00
|
|
|
}
|
2010-02-13 01:04:44 +01:00
|
|
|
|
2011-02-23 01:12:07 +01:00
|
|
|
char **strv_parse_nulstr(const char *s, size_t l);
|
2013-02-11 23:48:36 +01:00
|
|
|
char **strv_split_nulstr(const char *s);
|
2019-12-16 07:51:04 +01:00
|
|
|
int strv_make_nulstr(char * const *l, char **p, size_t *n);
|
2011-02-23 01:12:07 +01:00
|
|
|
|
2020-03-24 11:28:25 +01:00
|
|
|
static inline int strv_from_nulstr(char ***a, const char *nulstr) {
|
|
|
|
char **t;
|
|
|
|
|
|
|
|
t = strv_split_nulstr(nulstr);
|
|
|
|
if (!t)
|
|
|
|
return -ENOMEM;
|
|
|
|
*a = t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-16 07:51:04 +01:00
|
|
|
bool strv_overlap(char * const *a, char * const *b) _pure_;
|
2011-08-23 00:37:35 +02:00
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
#define STRV_FOREACH(s, l) \
|
2010-01-27 22:38:21 +01:00
|
|
|
for ((s) = (l); (s) && *(s); (s)++)
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2016-08-26 19:18:15 +02:00
|
|
|
#define STRV_FOREACH_BACKWARDS(s, l) \
|
|
|
|
for (s = ({ \
|
2020-05-10 18:33:16 +02:00
|
|
|
typeof(l) _l = l; \
|
2016-08-26 19:18:15 +02:00
|
|
|
_l ? _l + strv_length(_l) - 1U : NULL; \
|
|
|
|
}); \
|
|
|
|
(l) && ((s) >= (l)); \
|
|
|
|
(s)--)
|
2012-10-19 04:52:25 +02:00
|
|
|
|
2013-01-12 04:24:12 +01:00
|
|
|
#define STRV_FOREACH_PAIR(x, y, l) \
|
2020-04-22 19:32:09 +02:00
|
|
|
for ((x) = (l), (y) = (x) ? (x+1) : NULL; (x) && *(x) && *(y); (x) += 2, (y) = (x + 1))
|
2013-01-12 04:24:12 +01:00
|
|
|
|
2012-10-19 04:52:25 +02:00
|
|
|
char **strv_sort(char **l);
|
2019-12-16 07:51:04 +01:00
|
|
|
void strv_print(char * const *l);
|
2013-10-29 19:53:43 +01:00
|
|
|
|
|
|
|
#define strv_from_stdarg_alloca(first) \
|
|
|
|
({ \
|
|
|
|
char **_l; \
|
|
|
|
\
|
|
|
|
if (!first) \
|
2013-10-29 20:09:16 +01:00
|
|
|
_l = (char**) &first; \
|
2013-10-29 19:53:43 +01:00
|
|
|
else { \
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
size_t _n; \
|
2013-10-29 19:53:43 +01:00
|
|
|
va_list _ap; \
|
|
|
|
\
|
|
|
|
_n = 1; \
|
|
|
|
va_start(_ap, first); \
|
|
|
|
while (va_arg(_ap, char*)) \
|
|
|
|
_n++; \
|
|
|
|
va_end(_ap); \
|
|
|
|
\
|
|
|
|
_l = newa(char*, _n+1); \
|
|
|
|
_l[_n = 0] = (char*) first; \
|
|
|
|
va_start(_ap, first); \
|
|
|
|
for (;;) { \
|
|
|
|
_l[++_n] = va_arg(_ap, char*); \
|
|
|
|
if (!_l[_n]) \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
va_end(_ap); \
|
|
|
|
} \
|
|
|
|
_l; \
|
|
|
|
})
|
2014-03-05 03:34:48 +01:00
|
|
|
|
|
|
|
#define STR_IN_SET(x, ...) strv_contains(STRV_MAKE(__VA_ARGS__), x)
|
2016-09-25 02:13:28 +02:00
|
|
|
#define STRPTR_IN_SET(x, ...) \
|
|
|
|
({ \
|
|
|
|
const char* _x = (x); \
|
|
|
|
_x && strv_contains(STRV_MAKE(__VA_ARGS__), _x); \
|
|
|
|
})
|
2014-03-19 00:54:41 +01:00
|
|
|
|
2020-04-30 15:07:45 +02:00
|
|
|
#define STRCASE_IN_SET(x, ...) strv_contains_case(STRV_MAKE(__VA_ARGS__), x)
|
|
|
|
#define STRCASEPTR_IN_SET(x, ...) \
|
|
|
|
({ \
|
|
|
|
const char* _x = (x); \
|
|
|
|
_x && strv_contains_case(STRV_MAKE(__VA_ARGS__), _x); \
|
|
|
|
})
|
|
|
|
|
2018-11-23 16:27:15 +01:00
|
|
|
#define STARTSWITH_SET(p, ...) \
|
|
|
|
({ \
|
|
|
|
const char *_p = (p); \
|
|
|
|
char *_found = NULL, **_i; \
|
|
|
|
STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) { \
|
|
|
|
_found = startswith(_p, *_i); \
|
|
|
|
if (_found) \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
_found; \
|
|
|
|
})
|
|
|
|
|
2019-08-30 14:10:42 +02:00
|
|
|
#define ENDSWITH_SET(p, ...) \
|
|
|
|
({ \
|
|
|
|
const char *_p = (p); \
|
|
|
|
char *_found = NULL, **_i; \
|
|
|
|
STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) { \
|
|
|
|
_found = endswith(_p, *_i); \
|
|
|
|
if (_found) \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
_found; \
|
|
|
|
})
|
|
|
|
|
2019-01-16 00:13:38 +01:00
|
|
|
#define FOREACH_STRING(x, y, ...) \
|
|
|
|
for (char **_l = STRV_MAKE(({ x = y; }), ##__VA_ARGS__); \
|
|
|
|
x; \
|
|
|
|
x = *(++_l))
|
2014-12-19 01:31:59 +01:00
|
|
|
|
|
|
|
char **strv_reverse(char **l);
|
2015-06-23 12:57:41 +02:00
|
|
|
char **strv_shell_escape(char **l, const char *bad);
|
2015-02-14 00:37:43 +01:00
|
|
|
|
2020-01-12 12:14:31 +01:00
|
|
|
bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *matched_pos);
|
2020-01-12 12:26:26 +01:00
|
|
|
static inline bool strv_fnmatch(char* const* patterns, const char *s) {
|
|
|
|
return strv_fnmatch_full(patterns, s, 0, NULL);
|
2020-01-12 12:14:31 +01:00
|
|
|
}
|
2015-02-14 00:37:43 +01:00
|
|
|
|
2015-02-16 20:04:36 +01:00
|
|
|
static inline bool strv_fnmatch_or_empty(char* const* patterns, const char *s, int flags) {
|
2015-02-14 00:37:43 +01:00
|
|
|
assert(s);
|
|
|
|
return strv_isempty(patterns) ||
|
2020-01-12 12:26:26 +01:00
|
|
|
strv_fnmatch_full(patterns, s, flags, NULL);
|
2015-02-14 00:37:43 +01:00
|
|
|
}
|
2015-09-23 01:01:26 +02:00
|
|
|
|
|
|
|
char ***strv_free_free(char ***l);
|
2018-02-16 09:56:29 +01:00
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(char***, strv_free_free);
|
2015-09-24 12:23:21 +02:00
|
|
|
|
|
|
|
char **strv_skip(char **l, size_t n);
|
2015-10-04 17:36:19 +02:00
|
|
|
|
|
|
|
int strv_extend_n(char ***l, const char *value, size_t n);
|
networkd: rework Domains= setting
Previously, .network files only knew a vaguely defined "Domains=" concept, for which the documentation declared it was
the "DNS domain" for the network connection, without specifying what that means.
With this the Domains setting is reworked, so that there are now "routing" domains and "search" domains. The former are
to be used by resolved to route DNS request to specific network interfaces, the latter is to be used for searching
single-label hostnames with (in addition to being used for routing). Both settings are configured in the "Domains="
setting. Normal domain names listed in it are now considered search domains (for compatibility with existing setups),
while those prefixed with "~" are considered routing domains only. To route all lookups to a specific interface the
routing domain "." may be used, referring to the root domain. An alternative syntax for this is the "*", as was already
implemented before using the "wildcard" domain concept.
This commit adds proper parsers for this new logic, and exposes this via the sd-network API. This information is not
used by resolved yet, this will be added in a later commit.
2016-01-25 19:46:00 +01:00
|
|
|
|
2019-12-16 07:51:04 +01:00
|
|
|
int fputstrv(FILE *f, char * const *l, const char *separator, bool *space);
|
2017-11-14 23:21:57 +01:00
|
|
|
|
|
|
|
#define strv_free_and_replace(a, b) \
|
|
|
|
({ \
|
|
|
|
strv_free(a); \
|
|
|
|
(a) = (b); \
|
|
|
|
(b) = NULL; \
|
|
|
|
0; \
|
|
|
|
})
|
2019-03-28 17:28:48 +01:00
|
|
|
|
|
|
|
extern const struct hash_ops string_strv_hash_ops;
|
2020-05-28 17:27:11 +02:00
|
|
|
int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS);
|
|
|
|
int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS);
|
|
|
|
#define string_strv_hashmap_put(h, k, v) _string_strv_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS)
|
|
|
|
#define string_strv_ordered_hashmap_put(h, k, v) _string_strv_ordered_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS)
|