HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/basic/strv.c

35,576 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: faf413e8baa38e2bd2e5dc5f6cb0fed3fc9c3432

Lines 1001–1358 of 1,358

                int flags,
                size_t *ret_matched_pos) {
 
        assert(s);
 
        if (patterns)
                for (size_t i = 0; patterns[i]; i++)
                        /* NB: We treat all fnmatch() errors as equivalent to FNM_NOMATCH, i.e. if fnmatch() fails to
                         * process the pattern for some reason we'll consider this equivalent to non-matching. */
                        if (fnmatch(patterns[i], s, flags) == 0) {
                                if (ret_matched_pos)
                                        *ret_matched_pos = i;
                                return true;
                        }
 
        if (ret_matched_pos)
                *ret_matched_pos = SIZE_MAX;
 
        return false;
}
 
char** strv_skip(char **l, size_t n) {
        while (n > 0) {
                if (strv_isempty(l))
                        return NULL;
 
                l++, n--;
        }
 
        /* To simplify callers, always return NULL instead of a zero-item array. */
        if (strv_isempty(l))
                return NULL;
        return l;
}
 
int strv_extend_n(char ***l, const char *value, size_t n) {
        size_t i, k;
        char **nl;
 
        assert(l);
 
        if (!value)
                return 0;
        if (n == 0)
                return 0;
 
        /* Adds the value n times to l */
 
        k = strv_length(*l);
        if (n >= SIZE_MAX - k)
                return -ENOMEM;
 
        nl = reallocarray(*l, GREEDY_ALLOC_ROUND_UP(k + n + 1), sizeof(char *));
        if (!nl)
                return -ENOMEM;
 
        *l = nl;
 
        for (i = k; i < k + n; i++) {
                nl[i] = strdup(value);
                if (!nl[i])
                        goto rollback;
        }
        nl[i] = NULL;
 
        return 0;
 
rollback:
        for (size_t j = k; j < i; j++)
                free(nl[j]);
        nl[k] = NULL;
 
        return -ENOMEM;
}
 
int strv_extend_assignment(char ***l, const char *lhs, const char *rhs) {
        char *j;
 
        assert(l);
        assert(lhs);
 
        if (!rhs) /* value is optional, in which case we suppress the field */
                return 0;
 
        j = strjoin(lhs, "=", rhs);
        if (!j)
                return -ENOMEM;
 
        return strv_consume(l, j);
}
 
int fputstrv(FILE *f, char * const *l, const char *separator, bool *space) {
        bool b = false;
        int r;
 
        assert(f);
 
        /* Like fputs(), but for strv, and with a less stupid argument order */
 
        if (!space)
                space = &b;
 
        STRV_FOREACH(s, l) {
                r = fputs_with_separator(f, *s, separator, space);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
void string_strv_hashmap_remove(Hashmap *h, const char *key, const char *value) {
        assert(key);
 
        if (value) {
                char **l = hashmap_get(h, key);
                if (!l)
                        return;
 
                strv_remove(l, value);
                if (!strv_isempty(l))
                        return;
        }
 
        _unused_ _cleanup_free_ char *key_free = NULL;
        strv_free(hashmap_remove2(h, key, (void**) &key_free));
}
 
void string_strv_ordered_hashmap_remove(OrderedHashmap *h, const char *key, const char *value) {
        string_strv_hashmap_remove(PLAIN_HASHMAP(h), key, value);
}
 
static int string_strv_hashmap_put_internal(Hashmap *h, const char *key, const char *value) {
        char **l;
        int r;
 
        assert(h);
        assert(key);
        assert(value);
 
        l = hashmap_get(h, key);
        if (l) {
                /* A list for this key already exists, let's append to it if it is not listed yet */
                if (strv_contains(l, value))
                        return 0;
 
                r = strv_extend(&l, value);
                if (r < 0)
                        return r;
 
                assert_se(hashmap_update(h, key, l) >= 0);
        } else {
                /* No list for this key exists yet, create one */
                _cleanup_strv_free_ char **l2 = NULL;
                _cleanup_free_ char *t = NULL;
 
                t = strdup(key);
                if (!t)
                        return -ENOMEM;
 
                r = strv_extend(&l2, value);
                if (r < 0)
                        return r;
 
                r = hashmap_put(h, t, l2);
                if (r < 0)
                        return r;
 
                TAKE_PTR(t);
                TAKE_PTR(l2);
        }
 
        return 1;
}
 
int string_strv_hashmap_put(Hashmap **h, const char *key, const char *value) {
        int r;
 
        assert(h);
        assert(key);
        assert(value);
 
        r = hashmap_ensure_allocated(h, &string_hash_ops_free_strv_free);
        if (r < 0)
                return r;
 
        return string_strv_hashmap_put_internal(*h, key, value);
}
 
int string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value) {
        int r;
 
        assert(h);
        assert(key);
        assert(value);
 
        r = ordered_hashmap_ensure_allocated(h, &string_hash_ops_free_strv_free);
        if (r < 0)
                return r;
 
        return string_strv_hashmap_put_internal(PLAIN_HASHMAP(*h), key, value);
}
 
static int rebreak_flush_line(
                char ***broken,
                const char *start,
                const char *end,
                bool in_prefix,
                const char *whitespace_begin,
                const char *whitespace_end) {
 
        assert(broken);
        assert(start);
        assert(end >= start);
 
        if (in_prefix) /* Never seen anything non-whitespace? Generate empty line! */
                return strv_extend(broken, "");
 
        if (whitespace_begin && !whitespace_end) /* Ends in whitespace? Chop it off! */
                end = whitespace_begin;
 
        _cleanup_free_ char *truncated = strndup(start, end - start);
        if (!truncated)
                return -ENOMEM;
 
        return strv_consume(broken, TAKE_PTR(truncated));
}
 
int strv_rebreak_lines(char **l, size_t width, char ***ret) {
        _cleanup_strv_free_ char **broken = NULL;
        int r;
 
        assert(ret);
 
        /* Implements a simple UTF-8 line breaking algorithm
         *
         * Goes through all entries in *l, and line-breaks each line that is longer than the specified
         * character width. Breaks at the end of words/beginning of whitespace. Lines that do not contain whitespace are not
         * broken. Retains whitespace at beginning of lines, removes it at end of lines. Newlines embedded
         * in an entry are hard line breaks, i.e. result in a separate output entry each. */
 
        if (width == SIZE_MAX) { /* NOP? */
                broken = strv_copy(l);
                if (!broken)
                        return -ENOMEM;
 
                *ret = TAKE_PTR(broken);
                return 0;
        }
 
        STRV_FOREACH(i, l) {
                const char *start = *i, *whitespace_begin = NULL, *whitespace_end = NULL;
                bool in_prefix = true; /* still in the whitespace in the beginning of the line? */
                bool after_newline = false; /* did we just break at a newline embedded in the input? */
                size_t w = 0;
 
                for (const char *p = start; *p != 0; ) {
                        if (strchr(NEWLINE, *p)) {
                                /* A newline embedded in the input is a hard line break: flush out what we
                                 * collected so far as its own line, and continue with a new one right after
                                 * it. */
                                r = rebreak_flush_line(&broken, start, p, in_prefix, whitespace_begin, whitespace_end);
                                if (r < 0)
                                        return r;
 
                                p += p[0] == '\r' && p[1] == '\n' ? 2 : 1; /* Treat CRLF as a single break */
                                start = p;
                                in_prefix = true;
                                after_newline = true;
                                whitespace_begin = whitespace_end = NULL;
                                w = 0;
                                continue;
                        } else if (strchr(WHITESPACE, *p)) {
                                if (!in_prefix && (!whitespace_begin || whitespace_end)) {
                                        whitespace_begin = p;
                                        whitespace_end = NULL;
                                }
                        } else {
                                if (whitespace_begin && !whitespace_end)
                                        whitespace_end = p;
 
                                in_prefix = false;
                        }
 
                        /* Measure with the same helper utf8_console_width() uses. Our callers mix the
                         * two: sd-varlink-idl.c and verbs.c size their indent with utf8_console_width()
                         * and then break the body here, so if we accepted and measured what that one
                         * refuses we'd be computing a single width from two ideas of what valid UTF-8
                         * is. */
                        int cw = utf8_char_console_width(p);
                        if (cw < 0)
                                return log_debug_errno(cw, "Line to break contains invalid UTF-8, refusing: %m");
 
                        w += cw;
 
                        if (w > width && whitespace_begin && whitespace_end) {
                                _cleanup_free_ char *truncated = NULL;
 
                                truncated = strndup(start, whitespace_begin - start);
                                if (!truncated)
                                        return -ENOMEM;
 
                                r = strv_consume(&broken, TAKE_PTR(truncated));
                                if (r < 0)
                                        return r;
 
                                /* Continue with the next line, starting at the first character after the
                                 * whitespace we broke at. Note, that character has not been measured for
                                 * the new line yet, hence reset the width and reprocess it. */
                                p = start = whitespace_end;
                                after_newline = false;
                                whitespace_begin = whitespace_end = NULL;
                                w = 0;
                                continue;
                        }
 
                        p = utf8_next_char(p);
                }
 
                /* Process rest of the line, except if all that's left after the last newline we already
                 * flushed out above is whitespace (possibly nothing): a trailing newline terminates the
                 * last line, it doesn't add a new, empty one. */
                assert(start);
                if (after_newline && in_prefix)
                        continue;
 
                r = rebreak_flush_line(&broken, start, start + strlen(start), in_prefix, whitespace_begin, whitespace_end);
                if (r < 0)
                        return r;
        }
 
        *ret = TAKE_PTR(broken);
        return 0;
}
 
char** strv_filter_prefix(char * const *l, const char *prefix) {
 
        /* Allocates a copy of 'l', but only copies over entries starting with 'prefix' */
 
        if (isempty(prefix))
                return strv_copy(l);
 
        _cleanup_strv_free_ char **f = NULL;
        size_t sz = 0;
 
        STRV_FOREACH(i, l) {
                if (!startswith(*i, prefix))
                        continue;
 
                if (strv_extend_with_size(&f, &sz, *i) < 0)
                        return NULL;
        }
 
        return TAKE_PTR(f);
}
 
const char* const strv_empty[] = { NULL };