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