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