]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-util.c
Add string_contains_word_strv()
[thirdparty/systemd.git] / src / basic / string-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
07630cea 2
11c3a366
TA
3#include <errno.h>
4#include <stdarg.h>
5#include <stdint.h>
6#include <stdio.h>
7#include <stdlib.h>
8
b5efdb8a 9#include "alloc-util.h"
8409f688 10#include "escape.h"
53cd7f33 11#include "extract-word.h"
090a9c1e 12#include "fileio.h"
07630cea 13#include "gunicode.h"
c30a49b2 14#include "locale-util.h"
11c3a366 15#include "macro.h"
090a9c1e 16#include "memory-util.h"
b11d6a7b 17#include "string-util.h"
46bf625a 18#include "strv.h"
b4766d5f 19#include "terminal-util.h"
07630cea
LP
20#include "utf8.h"
21#include "util.h"
07630cea
LP
22
23int strcmp_ptr(const char *a, const char *b) {
07630cea 24 /* Like strcmp(), but tries to make sense of NULL pointers */
6cf3011c 25
07630cea
LP
26 if (a && b)
27 return strcmp(a, b);
6cf3011c
ZJS
28 return CMP(a, b); /* Direct comparison of pointers, one of which is NULL */
29}
07630cea 30
6cf3011c
ZJS
31int strcasecmp_ptr(const char *a, const char *b) {
32 /* Like strcasecmp(), but tries to make sense of NULL pointers */
07630cea 33
6cf3011c
ZJS
34 if (a && b)
35 return strcasecmp(a, b);
36 return CMP(a, b); /* Direct comparison of pointers, one of which is NULL */
07630cea
LP
37}
38
39char* endswith(const char *s, const char *postfix) {
40 size_t sl, pl;
41
42 assert(s);
43 assert(postfix);
44
45 sl = strlen(s);
46 pl = strlen(postfix);
47
48 if (pl == 0)
49 return (char*) s + sl;
50
51 if (sl < pl)
52 return NULL;
53
54 if (memcmp(s + sl - pl, postfix, pl) != 0)
55 return NULL;
56
57 return (char*) s + sl - pl;
58}
59
60char* endswith_no_case(const char *s, const char *postfix) {
61 size_t sl, pl;
62
63 assert(s);
64 assert(postfix);
65
66 sl = strlen(s);
67 pl = strlen(postfix);
68
69 if (pl == 0)
70 return (char*) s + sl;
71
72 if (sl < pl)
73 return NULL;
74
75 if (strcasecmp(s + sl - pl, postfix) != 0)
76 return NULL;
77
78 return (char*) s + sl - pl;
79}
80
81char* first_word(const char *s, const char *word) {
82 size_t sl, wl;
83 const char *p;
84
85 assert(s);
86 assert(word);
87
88 /* Checks if the string starts with the specified word, either
89 * followed by NUL or by whitespace. Returns a pointer to the
90 * NUL or the first character after the whitespace. */
91
92 sl = strlen(s);
93 wl = strlen(word);
94
95 if (sl < wl)
96 return NULL;
97
98 if (wl == 0)
99 return (char*) s;
100
101 if (memcmp(s, word, wl) != 0)
102 return NULL;
103
104 p = s + wl;
105 if (*p == 0)
106 return (char*) p;
107
108 if (!strchr(WHITESPACE, *p))
109 return NULL;
110
111 p += strspn(p, WHITESPACE);
112 return (char*) p;
113}
114
115static size_t strcspn_escaped(const char *s, const char *reject) {
116 bool escaped = false;
117 int n;
118
29965a2a 119 for (n = 0; s[n] != '\0'; n++) {
07630cea
LP
120 if (escaped)
121 escaped = false;
122 else if (s[n] == '\\')
123 escaped = true;
124 else if (strchr(reject, s[n]))
125 break;
126 }
127
29965a2a 128 return n;
07630cea
LP
129}
130
131/* Split a string into words. */
5fed82c6
LP
132const char* split(
133 const char **state,
134 size_t *l,
135 const char *separator,
136 SplitFlags flags) {
137
07630cea
LP
138 const char *current;
139
5fed82c6
LP
140 assert(state);
141 assert(l);
142
143 if (!separator)
144 separator = WHITESPACE;
145
07630cea
LP
146 current = *state;
147
5fed82c6 148 if (*current == '\0') /* already at the end? */
07630cea 149 return NULL;
07630cea 150
5fed82c6
LP
151 current += strspn(current, separator); /* skip leading separators */
152 if (*current == '\0') { /* at the end now? */
07630cea
LP
153 *state = current;
154 return NULL;
155 }
156
5fed82c6
LP
157 if (FLAGS_SET(flags, SPLIT_QUOTES)) {
158
159 if (strchr(QUOTES, *current)) {
160 /* We are looking at a quote */
161 *l = strcspn_escaped(current + 1, CHAR_TO_STR(*current));
162 if (current[*l + 1] != *current ||
163 (current[*l + 2] != 0 && !strchr(separator, current[*l + 2]))) {
164 /* right quote missing or garbage at the end */
165 if (FLAGS_SET(flags, SPLIT_RELAX)) {
166 *state = current + *l + 1 + (current[*l + 1] != '\0');
167 return current + 1;
168 }
169 *state = current;
170 return NULL;
8059aa9c 171 }
5fed82c6
LP
172 *state = current++ + *l + 2;
173
174 } else {
175 /* We are looking at a something that is not a quote */
176 *l = strcspn_escaped(current, separator);
177 if (current[*l] && !strchr(separator, current[*l]) && !FLAGS_SET(flags, SPLIT_RELAX)) {
178 /* unfinished escape */
179 *state = current;
180 return NULL;
181 }
182 *state = current + *l;
07630cea 183 }
07630cea
LP
184 } else {
185 *l = strcspn(current, separator);
186 *state = current + *l;
187 }
188
189 return current;
190}
191
192char *strnappend(const char *s, const char *suffix, size_t b) {
193 size_t a;
194 char *r;
195
196 if (!s && !suffix)
197 return strdup("");
198
199 if (!s)
200 return strndup(suffix, b);
201
202 if (!suffix)
203 return strdup(s);
204
205 assert(s);
206 assert(suffix);
207
208 a = strlen(s);
209 if (b > ((size_t) -1) - a)
210 return NULL;
211
212 r = new(char, a+b+1);
213 if (!r)
214 return NULL;
215
216 memcpy(r, s, a);
217 memcpy(r+a, suffix, b);
218 r[a+b] = 0;
219
220 return r;
221}
222
605405c6 223char *strjoin_real(const char *x, ...) {
07630cea
LP
224 va_list ap;
225 size_t l;
226 char *r, *p;
227
228 va_start(ap, x);
229
230 if (x) {
231 l = strlen(x);
232
233 for (;;) {
234 const char *t;
235 size_t n;
236
237 t = va_arg(ap, const char *);
238 if (!t)
239 break;
240
241 n = strlen(t);
242 if (n > ((size_t) -1) - l) {
243 va_end(ap);
244 return NULL;
245 }
246
247 l += n;
248 }
249 } else
250 l = 0;
251
252 va_end(ap);
253
254 r = new(char, l+1);
255 if (!r)
256 return NULL;
257
258 if (x) {
259 p = stpcpy(r, x);
260
261 va_start(ap, x);
262
263 for (;;) {
264 const char *t;
265
266 t = va_arg(ap, const char *);
267 if (!t)
268 break;
269
270 p = stpcpy(p, t);
271 }
272
273 va_end(ap);
274 } else
275 r[0] = 0;
276
277 return r;
278}
279
280char *strstrip(char *s) {
7546145e
LP
281 if (!s)
282 return NULL;
283
0a6ffc5c 284 /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */
07630cea 285
0a6ffc5c 286 return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE);
07630cea
LP
287}
288
289char *delete_chars(char *s, const char *bad) {
290 char *f, *t;
291
7546145e
LP
292 /* Drops all specified bad characters, regardless where in the string */
293
294 if (!s)
295 return NULL;
296
297 if (!bad)
298 bad = WHITESPACE;
07630cea
LP
299
300 for (f = s, t = s; *f; f++) {
301 if (strchr(bad, *f))
302 continue;
303
304 *(t++) = *f;
305 }
306
307 *t = 0;
308
309 return s;
310}
311
7546145e
LP
312char *delete_trailing_chars(char *s, const char *bad) {
313 char *p, *c = s;
314
315 /* Drops all specified bad characters, at the end of the string */
316
317 if (!s)
318 return NULL;
319
320 if (!bad)
321 bad = WHITESPACE;
322
323 for (p = s; *p; p++)
324 if (!strchr(bad, *p))
325 c = p + 1;
326
327 *c = 0;
328
329 return s;
330}
331
07630cea
LP
332char *truncate_nl(char *s) {
333 assert(s);
334
335 s[strcspn(s, NEWLINE)] = 0;
336 return s;
337}
338
b577e3d5
LP
339char ascii_tolower(char x) {
340
341 if (x >= 'A' && x <= 'Z')
342 return x - 'A' + 'a';
343
344 return x;
345}
346
846b8fc3
LP
347char ascii_toupper(char x) {
348
349 if (x >= 'a' && x <= 'z')
350 return x - 'a' + 'A';
351
352 return x;
353}
354
07630cea
LP
355char *ascii_strlower(char *t) {
356 char *p;
357
358 assert(t);
359
360 for (p = t; *p; p++)
b577e3d5
LP
361 *p = ascii_tolower(*p);
362
363 return t;
364}
365
846b8fc3
LP
366char *ascii_strupper(char *t) {
367 char *p;
368
369 assert(t);
370
371 for (p = t; *p; p++)
372 *p = ascii_toupper(*p);
373
374 return t;
375}
376
b577e3d5
LP
377char *ascii_strlower_n(char *t, size_t n) {
378 size_t i;
379
380 if (n <= 0)
381 return t;
382
383 for (i = 0; i < n; i++)
384 t[i] = ascii_tolower(t[i]);
07630cea
LP
385
386 return t;
387}
522d85ae
LP
388
389int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
390
391 for (; n > 0; a++, b++, n--) {
392 int x, y;
393
394 x = (int) (uint8_t) ascii_tolower(*a);
395 y = (int) (uint8_t) ascii_tolower(*b);
396
397 if (x != y)
398 return x - y;
399 }
400
401 return 0;
402}
c1749834
LP
403
404int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
405 int r;
406
407 r = ascii_strcasecmp_n(a, b, MIN(n, m));
408 if (r != 0)
409 return r;
410
6dd91b36 411 return CMP(n, m);
c1749834 412}
07630cea
LP
413
414bool chars_intersect(const char *a, const char *b) {
415 const char *p;
416
417 /* Returns true if any of the chars in a are in b. */
418 for (p = a; *p; p++)
419 if (strchr(b, *p))
420 return true;
421
422 return false;
423}
424
425bool string_has_cc(const char *p, const char *ok) {
426 const char *t;
427
428 assert(p);
429
430 /*
431 * Check if a string contains control characters. If 'ok' is
432 * non-NULL it may be a string containing additional CCs to be
433 * considered OK.
434 */
435
436 for (t = p; *t; t++) {
437 if (ok && strchr(ok, *t))
438 continue;
439
440 if (*t > 0 && *t < ' ')
441 return true;
442
443 if (*t == 127)
444 return true;
445 }
446
447 return false;
448}
449
8409f688
ZJS
450static int write_ellipsis(char *buf, bool unicode) {
451 if (unicode || is_locale_utf8()) {
452 buf[0] = 0xe2; /* tri-dot ellipsis: … */
453 buf[1] = 0x80;
454 buf[2] = 0xa6;
455 } else {
456 buf[0] = '.';
457 buf[1] = '.';
458 buf[2] = '.';
459 }
460
461 return 3;
462}
463
07630cea 464static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
9924aef6
ZJS
465 size_t x, need_space, suffix_len;
466 char *t;
07630cea
LP
467
468 assert(s);
469 assert(percent <= 100);
c30a49b2 470 assert(new_length != (size_t) -1);
07630cea 471
c30a49b2 472 if (old_length <= new_length)
07630cea
LP
473 return strndup(s, old_length);
474
c30a49b2
LP
475 /* Special case short ellipsations */
476 switch (new_length) {
477
478 case 0:
479 return strdup("");
480
481 case 1:
482 if (is_locale_utf8())
483 return strdup("…");
484 else
485 return strdup(".");
486
487 case 2:
488 if (!is_locale_utf8())
489 return strdup("..");
490
491 break;
492
493 default:
494 break;
495 }
496
497 /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
498 * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
499 * either for the UTF-8 encoded character or for three ASCII characters. */
500 need_space = is_locale_utf8() ? 1 : 3;
501
9924aef6
ZJS
502 t = new(char, new_length+3);
503 if (!t)
07630cea
LP
504 return NULL;
505
c30a49b2 506 assert(new_length >= need_space);
07630cea 507
c30a49b2
LP
508 x = ((new_length - need_space) * percent + 50) / 100;
509 assert(x <= new_length - need_space);
07630cea 510
9924aef6
ZJS
511 memcpy(t, s, x);
512 write_ellipsis(t + x, false);
513 suffix_len = new_length - x - need_space;
514 memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
515 *(t + x + 3 + suffix_len) = '\0';
07630cea 516
9924aef6 517 return t;
07630cea
LP
518}
519
520char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
c30a49b2 521 size_t x, k, len, len2;
07630cea 522 const char *i, *j;
c30a49b2 523 char *e;
c932fb71 524 int r;
07630cea 525
c30a49b2
LP
526 /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
527 * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
528 * strings.
529 *
530 * Ellipsation is done in a locale-dependent way:
531 * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
532 * 2. Otherwise, a unicode ellipsis is used ("…")
533 *
534 * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
535 * the current locale is UTF-8.
536 */
537
07630cea
LP
538 assert(s);
539 assert(percent <= 100);
ddbc9319
LP
540
541 if (new_length == (size_t) -1)
542 return strndup(s, old_length);
543
c30a49b2
LP
544 if (new_length == 0)
545 return strdup("");
07630cea 546
c30a49b2 547 /* If no multibyte characters use ascii_ellipsize_mem for speed */
21e4e3e0 548 if (ascii_is_valid_n(s, old_length))
07630cea
LP
549 return ascii_ellipsize_mem(s, old_length, new_length, percent);
550
c30a49b2
LP
551 x = ((new_length - 1) * percent) / 100;
552 assert(x <= new_length - 1);
07630cea
LP
553
554 k = 0;
9924aef6 555 for (i = s; i < s + old_length; i = utf8_next_char(i)) {
c932fb71 556 char32_t c;
9924aef6 557 int w;
07630cea 558
c932fb71
SL
559 r = utf8_encoded_to_unichar(i, &c);
560 if (r < 0)
07630cea 561 return NULL;
07630cea 562
9924aef6
ZJS
563 w = unichar_iswide(c) ? 2 : 1;
564 if (k + w <= x)
565 k += w;
566 else
567 break;
568 }
07630cea 569
9924aef6 570 for (j = s + old_length; j > i; ) {
c932fb71 571 char32_t c;
9924aef6
ZJS
572 int w;
573 const char *jj;
07630cea 574
9924aef6
ZJS
575 jj = utf8_prev_char(j);
576 r = utf8_encoded_to_unichar(jj, &c);
c932fb71 577 if (r < 0)
07630cea 578 return NULL;
9924aef6
ZJS
579
580 w = unichar_iswide(c) ? 2 : 1;
581 if (k + w <= new_length) {
582 k += w;
583 j = jj;
584 } else
585 break;
07630cea
LP
586 }
587 assert(i <= j);
588
589 /* we don't actually need to ellipsize */
590 if (i == j)
9924aef6 591 return memdup_suffix0(s, old_length);
07630cea 592
9924aef6
ZJS
593 /* make space for ellipsis, if possible */
594 if (j < s + old_length)
595 j = utf8_next_char(j);
596 else if (i > s)
597 i = utf8_prev_char(i);
07630cea
LP
598
599 len = i - s;
600 len2 = s + old_length - j;
601 e = new(char, len + 3 + len2 + 1);
602 if (!e)
603 return NULL;
604
605 /*
606 printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
607 old_length, new_length, x, len, len2, k);
608 */
609
610 memcpy(e, s, len);
8409f688 611 write_ellipsis(e + len, true);
9924aef6
ZJS
612 memcpy(e + len + 3, j, len2);
613 *(e + len + 3 + len2) = '\0';
07630cea
LP
614
615 return e;
616}
617
8409f688
ZJS
618char *cellescape(char *buf, size_t len, const char *s) {
619 /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII
620 * characters are copied as they are, everything else is escaped. The result
621 * is different then if escaping and ellipsization was performed in two
622 * separate steps, because each sequence is either stored in full or skipped.
623 *
624 * This function should be used for logging about strings which expected to
625 * be plain ASCII in a safe way.
626 *
627 * An ellipsis will be used if s is too long. It was always placed at the
628 * very end.
629 */
630
61f6e276
LP
631 size_t i = 0, last_char_width[4] = {}, k = 0, j;
632
633 assert(len > 0); /* at least a terminating NUL */
8409f688 634
61f6e276
LP
635 for (;;) {
636 char four[4];
637 int w;
8409f688 638
61f6e276 639 if (*s == 0) /* terminating NUL detected? then we are done! */
8409f688 640 goto done;
61f6e276
LP
641
642 w = cescape_char(*s, four);
643 if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
644 * ellipsize at the previous location */
645 break;
646
647 /* OK, there was space, let's add this escaped character to the buffer */
648 memcpy(buf + i, four, w);
649 i += w;
650
651 /* And remember its width in the ring buffer */
652 last_char_width[k] = w;
653 k = (k + 1) % 4;
654
655 s++;
8409f688
ZJS
656 }
657
61f6e276
LP
658 /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4
659 * characters ideally, but the buffer is shorter than that in the first place take what we can get */
660 for (j = 0; j < ELEMENTSOF(last_char_width); j++) {
661
662 if (i + 4 <= len) /* nice, we reached our space goal */
663 break;
664
665 k = k == 0 ? 3 : k - 1;
666 if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
667 break;
668
669 assert(i >= last_char_width[k]);
670 i -= last_char_width[k];
8409f688
ZJS
671 }
672
61f6e276
LP
673 if (i + 4 <= len) /* yay, enough space */
674 i += write_ellipsis(buf + i, false);
675 else if (i + 3 <= len) { /* only space for ".." */
676 buf[i++] = '.';
677 buf[i++] = '.';
678 } else if (i + 2 <= len) /* only space for a single "." */
679 buf[i++] = '.';
680 else
681 assert(i + 1 <= len);
682
8409f688
ZJS
683 done:
684 buf[i] = '\0';
685 return buf;
686}
687
07630cea
LP
688char* strshorten(char *s, size_t l) {
689 assert(s);
690
47b33c7d 691 if (strnlen(s, l+1) > l)
07630cea
LP
692 s[l] = 0;
693
694 return s;
695}
696
697char *strreplace(const char *text, const char *old_string, const char *new_string) {
9d73565a
LP
698 size_t l, old_len, new_len, allocated = 0;
699 char *t, *ret = NULL;
07630cea 700 const char *f;
07630cea 701
07630cea
LP
702 assert(old_string);
703 assert(new_string);
704
9d73565a
LP
705 if (!text)
706 return NULL;
707
07630cea
LP
708 old_len = strlen(old_string);
709 new_len = strlen(new_string);
710
711 l = strlen(text);
9d73565a 712 if (!GREEDY_REALLOC(ret, allocated, l+1))
07630cea
LP
713 return NULL;
714
715 f = text;
9d73565a 716 t = ret;
07630cea 717 while (*f) {
07630cea
LP
718 size_t d, nl;
719
720 if (!startswith(f, old_string)) {
721 *(t++) = *(f++);
722 continue;
723 }
724
9d73565a 725 d = t - ret;
07630cea 726 nl = l - old_len + new_len;
9d73565a
LP
727
728 if (!GREEDY_REALLOC(ret, allocated, nl + 1))
729 return mfree(ret);
07630cea
LP
730
731 l = nl;
9d73565a 732 t = ret + d;
07630cea
LP
733
734 t = stpcpy(t, new_string);
735 f += old_len;
736 }
737
738 *t = 0;
9d73565a 739 return ret;
07630cea
LP
740}
741
6fb05690
LP
742static void advance_offsets(
743 ssize_t diff,
744 size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */
745 size_t shift[static 2],
746 size_t size) {
747
b4766d5f
ZJS
748 if (!offsets)
749 return;
750
6fb05690
LP
751 assert(shift);
752
b4766d5f
ZJS
753 if ((size_t) diff < offsets[0])
754 shift[0] += size;
755 if ((size_t) diff < offsets[1])
756 shift[1] += size;
757}
758
759char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
62a3fc6d 760 const char *begin = NULL;
07630cea
LP
761 enum {
762 STATE_OTHER,
763 STATE_ESCAPE,
695a944c
LP
764 STATE_CSI,
765 STATE_CSO,
07630cea
LP
766 } state = STATE_OTHER;
767 char *obuf = NULL;
62a3fc6d 768 size_t osz = 0, isz, shift[2] = {}, n_carriage_returns = 0;
07630cea
LP
769 FILE *f;
770
771 assert(ibuf);
772 assert(*ibuf);
773
695a944c
LP
774 /* This does three things:
775 *
776 * 1. Replaces TABs by 8 spaces
777 * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
778 * 3. Strips ANSI operating system sequences (CSO), i.e. ESC ']' … BEL sequences
62a3fc6d
ZJS
779 * 4. Strip trailing \r characters (since they would "move the cursor", but have no
780 * other effect).
695a944c 781 *
2fe21124
ZJS
782 * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as
783 * are any other special characters. Truncated ANSI sequences are left-as is too. This call is
784 * supposed to suppress the most basic formatting noise, but nothing else.
695a944c
LP
785 *
786 * Why care for CSO sequences? Well, to undo what terminal_urlify() and friends generate. */
07630cea
LP
787
788 isz = _isz ? *_isz : strlen(*ibuf);
789
2fe21124
ZJS
790 /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we
791 * created f here and it doesn't leave our scope. */
792 f = open_memstream_unlocked(&obuf, &osz);
07630cea
LP
793 if (!f)
794 return NULL;
795
62a3fc6d 796 for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
07630cea
LP
797
798 switch (state) {
799
800 case STATE_OTHER:
801 if (i >= *ibuf + isz) /* EOT */
802 break;
62a3fc6d
ZJS
803
804 if (*i == '\r') {
805 n_carriage_returns++;
806 break;
807 } else if (*i == '\n')
808 /* Ignore carriage returns before new line */
809 n_carriage_returns = 0;
810 for (; n_carriage_returns > 0; n_carriage_returns--)
811 fputc('\r', f);
812
813 if (*i == '\x1B')
07630cea 814 state = STATE_ESCAPE;
b4766d5f 815 else if (*i == '\t') {
0d536673 816 fputs(" ", f);
b4766d5f
ZJS
817 advance_offsets(i - *ibuf, highlight, shift, 7);
818 } else
0d536673 819 fputc(*i, f);
b4766d5f 820
07630cea
LP
821 break;
822
823 case STATE_ESCAPE:
62a3fc6d
ZJS
824 assert(n_carriage_returns == 0);
825
07630cea 826 if (i >= *ibuf + isz) { /* EOT */
0d536673 827 fputc('\x1B', f);
b4766d5f 828 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea 829 break;
695a944c
LP
830 } else if (*i == '[') { /* ANSI CSI */
831 state = STATE_CSI;
832 begin = i + 1;
833 } else if (*i == ']') { /* ANSI CSO */
834 state = STATE_CSO;
07630cea
LP
835 begin = i + 1;
836 } else {
0d536673
LP
837 fputc('\x1B', f);
838 fputc(*i, f);
b4766d5f 839 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea
LP
840 state = STATE_OTHER;
841 }
842
843 break;
844
695a944c 845 case STATE_CSI:
62a3fc6d 846 assert(n_carriage_returns == 0);
07630cea 847
695a944c
LP
848 if (i >= *ibuf + isz || /* EOT … */
849 !strchr("01234567890;m", *i)) { /* … or invalid chars in sequence */
0d536673
LP
850 fputc('\x1B', f);
851 fputc('[', f);
b4766d5f 852 advance_offsets(i - *ibuf, highlight, shift, 2);
07630cea
LP
853 state = STATE_OTHER;
854 i = begin-1;
855 } else if (*i == 'm')
856 state = STATE_OTHER;
695a944c
LP
857
858 break;
859
860 case STATE_CSO:
62a3fc6d 861 assert(n_carriage_returns == 0);
695a944c
LP
862
863 if (i >= *ibuf + isz || /* EOT … */
864 (*i != '\a' && (uint8_t) *i < 32U) || (uint8_t) *i > 126U) { /* … or invalid chars in sequence */
865 fputc('\x1B', f);
866 fputc(']', f);
867 advance_offsets(i - *ibuf, highlight, shift, 2);
868 state = STATE_OTHER;
869 i = begin-1;
870 } else if (*i == '\a')
871 state = STATE_OTHER;
872
07630cea
LP
873 break;
874 }
875 }
876
c7e03d2e 877 if (fflush_and_check(f) < 0) {
07630cea 878 fclose(f);
6b430fdb 879 return mfree(obuf);
07630cea 880 }
07630cea
LP
881 fclose(f);
882
6fb05690 883 free_and_replace(*ibuf, obuf);
07630cea
LP
884
885 if (_isz)
886 *_isz = osz;
887
b4766d5f
ZJS
888 if (highlight) {
889 highlight[0] += shift[0];
890 highlight[1] += shift[1];
891 }
892
6fb05690 893 return *ibuf;
07630cea
LP
894}
895
bb8ad9ea
LP
896char *strextend_with_separator(char **x, const char *separator, ...) {
897 bool need_separator;
898 size_t f, l, l_separator;
07630cea 899 char *r, *p;
bb8ad9ea 900 va_list ap;
07630cea
LP
901
902 assert(x);
903
7bf7ce28 904 l = f = strlen_ptr(*x);
07630cea 905
bb8ad9ea
LP
906 need_separator = !isempty(*x);
907 l_separator = strlen_ptr(separator);
908
909 va_start(ap, separator);
07630cea
LP
910 for (;;) {
911 const char *t;
912 size_t n;
913
914 t = va_arg(ap, const char *);
915 if (!t)
916 break;
917
918 n = strlen(t);
bb8ad9ea
LP
919
920 if (need_separator)
921 n += l_separator;
922
07630cea
LP
923 if (n > ((size_t) -1) - l) {
924 va_end(ap);
925 return NULL;
926 }
927
928 l += n;
bb8ad9ea 929 need_separator = true;
07630cea
LP
930 }
931 va_end(ap);
932
bb8ad9ea
LP
933 need_separator = !isempty(*x);
934
07630cea
LP
935 r = realloc(*x, l+1);
936 if (!r)
937 return NULL;
938
939 p = r + f;
940
bb8ad9ea 941 va_start(ap, separator);
07630cea
LP
942 for (;;) {
943 const char *t;
944
945 t = va_arg(ap, const char *);
946 if (!t)
947 break;
948
bb8ad9ea
LP
949 if (need_separator && separator)
950 p = stpcpy(p, separator);
951
07630cea 952 p = stpcpy(p, t);
bb8ad9ea
LP
953
954 need_separator = true;
07630cea
LP
955 }
956 va_end(ap);
957
bb8ad9ea
LP
958 assert(p == r + l);
959
07630cea
LP
960 *p = 0;
961 *x = r;
962
963 return r + l;
964}
965
966char *strrep(const char *s, unsigned n) {
967 size_t l;
968 char *r, *p;
969 unsigned i;
970
971 assert(s);
972
973 l = strlen(s);
974 p = r = malloc(l * n + 1);
975 if (!r)
976 return NULL;
977
978 for (i = 0; i < n; i++)
979 p = stpcpy(p, s);
980
981 *p = 0;
982 return r;
983}
984
985int split_pair(const char *s, const char *sep, char **l, char **r) {
986 char *x, *a, *b;
987
988 assert(s);
989 assert(sep);
990 assert(l);
991 assert(r);
992
993 if (isempty(sep))
994 return -EINVAL;
995
996 x = strstr(s, sep);
997 if (!x)
998 return -EINVAL;
999
1000 a = strndup(s, x - s);
1001 if (!a)
1002 return -ENOMEM;
1003
1004 b = strdup(x + strlen(sep));
1005 if (!b) {
1006 free(a);
1007 return -ENOMEM;
1008 }
1009
1010 *l = a;
1011 *r = b;
1012
1013 return 0;
1014}
1015
1016int free_and_strdup(char **p, const char *s) {
1017 char *t;
1018
1019 assert(p);
1020
7f546026 1021 /* Replaces a string pointer with a strdup()ed new string,
07630cea
LP
1022 * possibly freeing the old one. */
1023
1024 if (streq_ptr(*p, s))
1025 return 0;
1026
1027 if (s) {
1028 t = strdup(s);
1029 if (!t)
1030 return -ENOMEM;
1031 } else
1032 t = NULL;
1033
1034 free(*p);
1035 *p = t;
1036
1037 return 1;
1038}
1039
7f546026
ZJS
1040int free_and_strndup(char **p, const char *s, size_t l) {
1041 char *t;
1042
1043 assert(p);
1044 assert(s || l == 0);
1045
1046 /* Replaces a string pointer with a strndup()ed new string,
1047 * freeing the old one. */
1048
1049 if (!*p && !s)
1050 return 0;
1051
1052 if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
1053 return 0;
1054
1055 if (s) {
1056 t = strndup(s, l);
1057 if (!t)
1058 return -ENOMEM;
1059 } else
1060 t = NULL;
1061
1062 free_and_replace(*p, t);
1063 return 1;
1064}
1065
f3e2e81d
LP
1066bool string_is_safe(const char *p) {
1067 const char *t;
1068
1069 if (!p)
1070 return false;
1071
839d1b20
LP
1072 /* Checks if the specified string contains no quotes or control characters */
1073
f3e2e81d
LP
1074 for (t = p; *t; t++) {
1075 if (*t > 0 && *t < ' ') /* no control characters */
1076 return false;
1077
1078 if (strchr(QUOTES "\\\x7f", *t))
1079 return false;
1080 }
1081
1082 return true;
1083}
53caaffd
LP
1084
1085char* string_erase(char *x) {
1086 if (!x)
1087 return NULL;
1088
1089 /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
1090 * used them. */
1091 explicit_bzero_safe(x, strlen(x));
1092 return x;
1093}
8dd6491e
LP
1094
1095int string_truncate_lines(const char *s, size_t n_lines, char **ret) {
1096 const char *p = s, *e = s;
1097 bool truncation_applied = false;
1098 char *copy;
1099 size_t n = 0;
1100
1101 assert(s);
1102
1103 /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if
1104 * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not
1105 * generated either. */
1106
1107 for (;;) {
1108 size_t k;
1109
1110 k = strcspn(p, "\n");
1111
1112 if (p[k] == 0) {
1113 if (k == 0) /* final empty line */
1114 break;
1115
1116 if (n >= n_lines) /* above threshold */
1117 break;
1118
1119 e = p + k; /* last line to include */
1120 break;
1121 }
1122
1123 assert(p[k] == '\n');
1124
1125 if (n >= n_lines)
1126 break;
1127
1128 if (k > 0)
1129 e = p + k;
1130
1131 p += k + 1;
1132 n++;
1133 }
1134
1135 /* e points after the last character we want to keep */
1136 if (isempty(e))
1137 copy = strdup(s);
1138 else {
1139 if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that
1140 * isn't a new-line or a series of them */
1141 truncation_applied = true;
1142
1143 copy = strndup(s, e - s);
1144 }
1145 if (!copy)
1146 return -ENOMEM;
1147
1148 *ret = copy;
1149 return truncation_applied;
1150}
f6857fa6
LP
1151
1152int string_extract_line(const char *s, size_t i, char **ret) {
1153 const char *p = s;
1154 size_t c = 0;
1155
1156 /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that,
1157 * and == 0 if we are looking at the last line or already beyond the last line. As special
1158 * optimization, if the first line is requested and the string only consists of one line we return
1159 * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very
1160 * common case. */
1161
1162 for (;;) {
1163 const char *q;
1164
1165 q = strchr(p, '\n');
1166 if (i == c) {
1167 /* The line we are looking for! */
1168
1169 if (q) {
1170 char *m;
1171
1172 m = strndup(p, q - p);
1173 if (!m)
1174 return -ENOMEM;
1175
1176 *ret = m;
1177 return !isempty(q + 1); /* more coming? */
1178 } else {
1179 if (p == s)
1180 *ret = NULL; /* Just use the input string */
1181 else {
1182 char *m;
1183
1184 m = strdup(p);
1185 if (!m)
1186 return -ENOMEM;
1187
1188 *ret = m;
1189 }
1190
1191 return 0; /* The end */
1192 }
1193 }
1194
1195 if (!q) {
1196 char *m;
1197
1198 /* No more lines, return empty line */
1199
1200 m = strdup("");
1201 if (!m)
1202 return -ENOMEM;
1203
1204 *ret = m;
1205 return 0; /* The end */
1206 }
1207
1208 p = q + 1;
1209 c++;
1210 }
1211}
53cd7f33 1212
46bf625a 1213int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word) {
53cd7f33
ZJS
1214 /* In the default mode with no separators specified, we split on whitespace and
1215 * don't coalesce separators. */
1216 const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
1217
46bf625a
ZJS
1218 const char *found = NULL;
1219
53cd7f33
ZJS
1220 for (const char *p = string;;) {
1221 _cleanup_free_ char *w = NULL;
1222 int r;
1223
1224 r = extract_first_word(&p, &w, separators, flags);
1225 if (r < 0)
1226 return r;
1227 if (r == 0)
46bf625a
ZJS
1228 break;
1229
1230 found = strv_find(words, w);
1231 if (found)
1232 break;
53cd7f33 1233 }
46bf625a
ZJS
1234
1235 if (ret_word)
1236 *ret_word = found;
1237 return !!found;
53cd7f33 1238}