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