]>
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" |
f392dfb5 | 12 | #include "fd-util.h" |
090a9c1e | 13 | #include "fileio.h" |
07630cea | 14 | #include "gunicode.h" |
c30a49b2 | 15 | #include "locale-util.h" |
11c3a366 | 16 | #include "macro.h" |
090a9c1e | 17 | #include "memory-util.h" |
2485b7e2 | 18 | #include "memstream-util.h" |
f5c6b4f4 | 19 | #include "path-util.h" |
b11d6a7b | 20 | #include "string-util.h" |
46bf625a | 21 | #include "strv.h" |
b4766d5f | 22 | #include "terminal-util.h" |
07630cea | 23 | #include "utf8.h" |
07630cea | 24 | |
07630cea LP |
25 | char* first_word(const char *s, const char *word) { |
26 | size_t sl, wl; | |
27 | const char *p; | |
28 | ||
29 | assert(s); | |
30 | assert(word); | |
31 | ||
32 | /* Checks if the string starts with the specified word, either | |
33 | * followed by NUL or by whitespace. Returns a pointer to the | |
34 | * NUL or the first character after the whitespace. */ | |
35 | ||
36 | sl = strlen(s); | |
37 | wl = strlen(word); | |
38 | ||
39 | if (sl < wl) | |
40 | return NULL; | |
41 | ||
42 | if (wl == 0) | |
43 | return (char*) s; | |
44 | ||
45 | if (memcmp(s, word, wl) != 0) | |
46 | return NULL; | |
47 | ||
48 | p = s + wl; | |
49 | if (*p == 0) | |
50 | return (char*) p; | |
51 | ||
52 | if (!strchr(WHITESPACE, *p)) | |
53 | return NULL; | |
54 | ||
55 | p += strspn(p, WHITESPACE); | |
56 | return (char*) p; | |
57 | } | |
58 | ||
07630cea LP |
59 | char *strnappend(const char *s, const char *suffix, size_t b) { |
60 | size_t a; | |
61 | char *r; | |
62 | ||
63 | if (!s && !suffix) | |
64 | return strdup(""); | |
65 | ||
66 | if (!s) | |
67 | return strndup(suffix, b); | |
68 | ||
69 | if (!suffix) | |
70 | return strdup(s); | |
71 | ||
72 | assert(s); | |
73 | assert(suffix); | |
74 | ||
75 | a = strlen(s); | |
fd4e991d | 76 | if (b > SIZE_MAX - a) |
07630cea LP |
77 | return NULL; |
78 | ||
79 | r = new(char, a+b+1); | |
80 | if (!r) | |
81 | return NULL; | |
82 | ||
83 | memcpy(r, s, a); | |
84 | memcpy(r+a, suffix, b); | |
85 | r[a+b] = 0; | |
86 | ||
87 | return r; | |
88 | } | |
89 | ||
605405c6 | 90 | char *strjoin_real(const char *x, ...) { |
07630cea | 91 | va_list ap; |
6ced0770 | 92 | size_t l = 1; |
07630cea LP |
93 | char *r, *p; |
94 | ||
95 | va_start(ap, x); | |
020003f2 RV |
96 | for (const char *t = x; t; t = va_arg(ap, const char *)) { |
97 | size_t n; | |
07630cea | 98 | |
020003f2 RV |
99 | n = strlen(t); |
100 | if (n > SIZE_MAX - l) { | |
101 | va_end(ap); | |
102 | return NULL; | |
07630cea | 103 | } |
020003f2 RV |
104 | l += n; |
105 | } | |
07630cea LP |
106 | va_end(ap); |
107 | ||
6ced0770 | 108 | p = r = new(char, l); |
07630cea LP |
109 | if (!r) |
110 | return NULL; | |
111 | ||
020003f2 RV |
112 | va_start(ap, x); |
113 | for (const char *t = x; t; t = va_arg(ap, const char *)) | |
114 | p = stpcpy(p, t); | |
115 | va_end(ap); | |
07630cea | 116 | |
020003f2 | 117 | *p = 0; |
07630cea LP |
118 | |
119 | return r; | |
120 | } | |
121 | ||
122 | char *strstrip(char *s) { | |
7546145e LP |
123 | if (!s) |
124 | return NULL; | |
125 | ||
0a6ffc5c | 126 | /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */ |
07630cea | 127 | |
0a6ffc5c | 128 | return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE); |
07630cea LP |
129 | } |
130 | ||
131 | char *delete_chars(char *s, const char *bad) { | |
132 | char *f, *t; | |
133 | ||
7546145e LP |
134 | /* Drops all specified bad characters, regardless where in the string */ |
135 | ||
136 | if (!s) | |
137 | return NULL; | |
138 | ||
139 | if (!bad) | |
140 | bad = WHITESPACE; | |
07630cea LP |
141 | |
142 | for (f = s, t = s; *f; f++) { | |
143 | if (strchr(bad, *f)) | |
144 | continue; | |
145 | ||
146 | *(t++) = *f; | |
147 | } | |
148 | ||
149 | *t = 0; | |
150 | ||
151 | return s; | |
152 | } | |
153 | ||
7546145e | 154 | char *delete_trailing_chars(char *s, const char *bad) { |
a01080ce | 155 | char *c = s; |
7546145e LP |
156 | |
157 | /* Drops all specified bad characters, at the end of the string */ | |
158 | ||
159 | if (!s) | |
160 | return NULL; | |
161 | ||
162 | if (!bad) | |
163 | bad = WHITESPACE; | |
164 | ||
a01080ce | 165 | for (char *p = s; *p; p++) |
7546145e LP |
166 | if (!strchr(bad, *p)) |
167 | c = p + 1; | |
168 | ||
169 | *c = 0; | |
170 | ||
171 | return s; | |
172 | } | |
173 | ||
61cecfa0 | 174 | char *truncate_nl_full(char *s, size_t *ret_len) { |
175 | size_t n; | |
176 | ||
07630cea LP |
177 | assert(s); |
178 | ||
61cecfa0 | 179 | n = strcspn(s, NEWLINE); |
180 | s[n] = '\0'; | |
181 | if (ret_len) | |
182 | *ret_len = n; | |
07630cea LP |
183 | return s; |
184 | } | |
185 | ||
b577e3d5 LP |
186 | char ascii_tolower(char x) { |
187 | ||
188 | if (x >= 'A' && x <= 'Z') | |
189 | return x - 'A' + 'a'; | |
190 | ||
191 | return x; | |
192 | } | |
193 | ||
846b8fc3 LP |
194 | char ascii_toupper(char x) { |
195 | ||
196 | if (x >= 'a' && x <= 'z') | |
197 | return x - 'a' + 'A'; | |
198 | ||
199 | return x; | |
200 | } | |
201 | ||
07630cea | 202 | char *ascii_strlower(char *t) { |
07630cea LP |
203 | assert(t); |
204 | ||
a01080ce | 205 | for (char *p = t; *p; p++) |
b577e3d5 LP |
206 | *p = ascii_tolower(*p); |
207 | ||
208 | return t; | |
209 | } | |
210 | ||
846b8fc3 | 211 | char *ascii_strupper(char *t) { |
846b8fc3 LP |
212 | assert(t); |
213 | ||
a01080ce | 214 | for (char *p = t; *p; p++) |
846b8fc3 LP |
215 | *p = ascii_toupper(*p); |
216 | ||
217 | return t; | |
218 | } | |
219 | ||
b577e3d5 | 220 | char *ascii_strlower_n(char *t, size_t n) { |
b577e3d5 LP |
221 | if (n <= 0) |
222 | return t; | |
223 | ||
a01080ce | 224 | for (size_t i = 0; i < n; i++) |
b577e3d5 | 225 | t[i] = ascii_tolower(t[i]); |
07630cea LP |
226 | |
227 | return t; | |
228 | } | |
522d85ae LP |
229 | |
230 | int ascii_strcasecmp_n(const char *a, const char *b, size_t n) { | |
231 | ||
232 | for (; n > 0; a++, b++, n--) { | |
233 | int x, y; | |
234 | ||
235 | x = (int) (uint8_t) ascii_tolower(*a); | |
236 | y = (int) (uint8_t) ascii_tolower(*b); | |
237 | ||
238 | if (x != y) | |
239 | return x - y; | |
240 | } | |
241 | ||
242 | return 0; | |
243 | } | |
c1749834 LP |
244 | |
245 | int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) { | |
246 | int r; | |
247 | ||
248 | r = ascii_strcasecmp_n(a, b, MIN(n, m)); | |
249 | if (r != 0) | |
250 | return r; | |
251 | ||
6dd91b36 | 252 | return CMP(n, m); |
c1749834 | 253 | } |
07630cea LP |
254 | |
255 | bool chars_intersect(const char *a, const char *b) { | |
07630cea | 256 | /* Returns true if any of the chars in a are in b. */ |
a01080ce | 257 | for (const char *p = a; *p; p++) |
07630cea LP |
258 | if (strchr(b, *p)) |
259 | return true; | |
260 | ||
261 | return false; | |
262 | } | |
263 | ||
264 | bool string_has_cc(const char *p, const char *ok) { | |
07630cea LP |
265 | assert(p); |
266 | ||
267 | /* | |
268 | * Check if a string contains control characters. If 'ok' is | |
269 | * non-NULL it may be a string containing additional CCs to be | |
270 | * considered OK. | |
271 | */ | |
272 | ||
a01080ce | 273 | for (const char *t = p; *t; t++) { |
07630cea LP |
274 | if (ok && strchr(ok, *t)) |
275 | continue; | |
276 | ||
6302d386 | 277 | if (char_is_cc(*t)) |
07630cea LP |
278 | return true; |
279 | } | |
280 | ||
281 | return false; | |
282 | } | |
283 | ||
8409f688 ZJS |
284 | static int write_ellipsis(char *buf, bool unicode) { |
285 | if (unicode || is_locale_utf8()) { | |
286 | buf[0] = 0xe2; /* tri-dot ellipsis: … */ | |
287 | buf[1] = 0x80; | |
288 | buf[2] = 0xa6; | |
289 | } else { | |
290 | buf[0] = '.'; | |
291 | buf[1] = '.'; | |
292 | buf[2] = '.'; | |
293 | } | |
294 | ||
295 | return 3; | |
296 | } | |
297 | ||
cb558ab2 ZJS |
298 | static size_t ansi_sequence_length(const char *s, size_t len) { |
299 | assert(s); | |
300 | ||
301 | if (len < 2) | |
302 | return 0; | |
303 | ||
304 | if (s[0] != 0x1B) /* ASCII 27, aka ESC, aka Ctrl-[ */ | |
305 | return 0; /* Not the start of a sequence */ | |
306 | ||
307 | if (s[1] == 0x5B) { /* [, start of CSI sequence */ | |
308 | size_t i = 2; | |
309 | ||
310 | if (i == len) | |
311 | return 0; | |
312 | ||
313 | while (s[i] >= 0x30 && s[i] <= 0x3F) /* Parameter bytes */ | |
314 | if (++i == len) | |
315 | return 0; | |
316 | while (s[i] >= 0x20 && s[i] <= 0x2F) /* Intermediate bytes */ | |
317 | if (++i == len) | |
318 | return 0; | |
319 | if (s[i] >= 0x40 && s[i] <= 0x7E) /* Final byte */ | |
320 | return i + 1; | |
321 | return 0; /* Bad sequence */ | |
322 | ||
323 | } else if (s[1] >= 0x40 && s[1] <= 0x5F) /* other non-CSI Fe sequence */ | |
324 | return 2; | |
325 | ||
326 | return 0; /* Bad escape? */ | |
327 | } | |
328 | ||
329 | static bool string_has_ansi_sequence(const char *s, size_t len) { | |
330 | const char *t = s; | |
331 | ||
332 | while ((t = memchr(s, 0x1B, len - (t - s)))) | |
333 | if (ansi_sequence_length(t, len - (t - s)) > 0) | |
334 | return true; | |
335 | return false; | |
336 | } | |
337 | ||
338 | static size_t previous_ansi_sequence(const char *s, size_t length, const char **ret_where) { | |
339 | /* Locate the previous ANSI sequence and save its start in *ret_where and return length. */ | |
340 | ||
341 | for (size_t i = length - 2; i > 0; i--) { /* -2 because at least two bytes are needed */ | |
342 | size_t slen = ansi_sequence_length(s + (i - 1), length - (i - 1)); | |
343 | if (slen == 0) | |
344 | continue; | |
345 | ||
346 | *ret_where = s + (i - 1); | |
347 | return slen; | |
348 | } | |
349 | ||
350 | *ret_where = NULL; | |
351 | return 0; | |
352 | } | |
353 | ||
07630cea | 354 | static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) { |
9924aef6 ZJS |
355 | size_t x, need_space, suffix_len; |
356 | char *t; | |
07630cea LP |
357 | |
358 | assert(s); | |
359 | assert(percent <= 100); | |
f5fbe71d | 360 | assert(new_length != SIZE_MAX); |
07630cea | 361 | |
c30a49b2 | 362 | if (old_length <= new_length) |
07630cea LP |
363 | return strndup(s, old_length); |
364 | ||
c30a49b2 LP |
365 | /* Special case short ellipsations */ |
366 | switch (new_length) { | |
367 | ||
368 | case 0: | |
369 | return strdup(""); | |
370 | ||
371 | case 1: | |
372 | if (is_locale_utf8()) | |
373 | return strdup("…"); | |
374 | else | |
375 | return strdup("."); | |
376 | ||
377 | case 2: | |
378 | if (!is_locale_utf8()) | |
379 | return strdup(".."); | |
380 | ||
381 | break; | |
382 | ||
383 | default: | |
384 | break; | |
385 | } | |
386 | ||
387 | /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one | |
388 | * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage, | |
389 | * either for the UTF-8 encoded character or for three ASCII characters. */ | |
390 | need_space = is_locale_utf8() ? 1 : 3; | |
391 | ||
9924aef6 ZJS |
392 | t = new(char, new_length+3); |
393 | if (!t) | |
07630cea LP |
394 | return NULL; |
395 | ||
c30a49b2 | 396 | assert(new_length >= need_space); |
07630cea | 397 | |
c30a49b2 LP |
398 | x = ((new_length - need_space) * percent + 50) / 100; |
399 | assert(x <= new_length - need_space); | |
07630cea | 400 | |
9924aef6 ZJS |
401 | memcpy(t, s, x); |
402 | write_ellipsis(t + x, false); | |
403 | suffix_len = new_length - x - need_space; | |
404 | memcpy(t + x + 3, s + old_length - suffix_len, suffix_len); | |
405 | *(t + x + 3 + suffix_len) = '\0'; | |
07630cea | 406 | |
9924aef6 | 407 | return t; |
07630cea LP |
408 | } |
409 | ||
410 | char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) { | |
c30a49b2 | 411 | size_t x, k, len, len2; |
07630cea | 412 | const char *i, *j; |
c932fb71 | 413 | int r; |
07630cea | 414 | |
c30a49b2 LP |
415 | /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up |
416 | * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8 | |
417 | * strings. | |
418 | * | |
419 | * Ellipsation is done in a locale-dependent way: | |
420 | * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...") | |
421 | * 2. Otherwise, a unicode ellipsis is used ("…") | |
422 | * | |
423 | * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or | |
424 | * the current locale is UTF-8. | |
425 | */ | |
426 | ||
07630cea LP |
427 | assert(s); |
428 | assert(percent <= 100); | |
ddbc9319 | 429 | |
f5fbe71d | 430 | if (new_length == SIZE_MAX) |
ddbc9319 LP |
431 | return strndup(s, old_length); |
432 | ||
c30a49b2 LP |
433 | if (new_length == 0) |
434 | return strdup(""); | |
07630cea | 435 | |
cb558ab2 ZJS |
436 | bool has_ansi_seq = string_has_ansi_sequence(s, old_length); |
437 | ||
438 | /* If no multibyte characters or ANSI sequences, use ascii_ellipsize_mem for speed */ | |
439 | if (!has_ansi_seq && ascii_is_valid_n(s, old_length)) | |
07630cea LP |
440 | return ascii_ellipsize_mem(s, old_length, new_length, percent); |
441 | ||
cb558ab2 | 442 | x = (new_length - 1) * percent / 100; |
c30a49b2 | 443 | assert(x <= new_length - 1); |
07630cea LP |
444 | |
445 | k = 0; | |
cb558ab2 ZJS |
446 | for (i = s; i < s + old_length; ) { |
447 | size_t slen = has_ansi_seq ? ansi_sequence_length(i, old_length - (i - s)) : 0; | |
448 | if (slen > 0) { | |
449 | i += slen; | |
450 | continue; /* ANSI sequences don't take up any space in output */ | |
451 | } | |
07630cea | 452 | |
cb558ab2 | 453 | char32_t c; |
c932fb71 SL |
454 | r = utf8_encoded_to_unichar(i, &c); |
455 | if (r < 0) | |
07630cea | 456 | return NULL; |
07630cea | 457 | |
cb558ab2 ZJS |
458 | int w = unichar_iswide(c) ? 2 : 1; |
459 | if (k + w > x) | |
9924aef6 | 460 | break; |
cb558ab2 ZJS |
461 | |
462 | k += w; | |
463 | i += r; | |
9924aef6 | 464 | } |
07630cea | 465 | |
cb558ab2 ZJS |
466 | const char *ansi_start = s + old_length; |
467 | size_t ansi_len = 0; | |
468 | ||
469 | for (const char *t = j = s + old_length; t > i && k < new_length; ) { | |
c932fb71 | 470 | char32_t c; |
9924aef6 | 471 | int w; |
cb558ab2 ZJS |
472 | const char *tt; |
473 | ||
474 | if (has_ansi_seq && ansi_start >= t) | |
475 | /* Figure out the previous ANSI sequence, if any */ | |
476 | ansi_len = previous_ansi_sequence(s, t - s, &ansi_start); | |
07630cea | 477 | |
cb558ab2 ZJS |
478 | /* If the sequence extends all the way to the current position, skip it. */ |
479 | if (has_ansi_seq && ansi_len > 0 && ansi_start + ansi_len == t) { | |
480 | t = ansi_start; | |
481 | continue; | |
482 | } | |
483 | ||
484 | tt = utf8_prev_char(t); | |
485 | r = utf8_encoded_to_unichar(tt, &c); | |
c932fb71 | 486 | if (r < 0) |
07630cea | 487 | return NULL; |
9924aef6 ZJS |
488 | |
489 | w = unichar_iswide(c) ? 2 : 1; | |
cb558ab2 | 490 | if (k + w > new_length) |
9924aef6 | 491 | break; |
cb558ab2 ZJS |
492 | |
493 | k += w; | |
494 | j = t = tt; /* j should always point to the first "real" character */ | |
07630cea | 495 | } |
07630cea | 496 | |
cb558ab2 ZJS |
497 | /* We don't actually need to ellipsize */ |
498 | if (i >= j) | |
9924aef6 | 499 | return memdup_suffix0(s, old_length); |
07630cea | 500 | |
cb558ab2 ZJS |
501 | if (k >= new_length) { |
502 | /* Make space for ellipsis, if required and possible. We know that the edge character is not | |
503 | * part of an ANSI sequence (because then we'd skip it). If the last character we looked at | |
504 | * was wide, we don't need to make space. */ | |
505 | if (j < s + old_length) | |
506 | j = utf8_next_char(j); | |
507 | else if (i > s) | |
508 | i = utf8_prev_char(i); | |
509 | } | |
07630cea LP |
510 | |
511 | len = i - s; | |
512 | len2 = s + old_length - j; | |
cb558ab2 ZJS |
513 | |
514 | /* If we have ANSI, allow the same length as the source string + ellipsis. It'd be too involved to | |
515 | * figure out what exact space is needed. Strings with ANSI sequences are most likely to be fairly | |
516 | * short anyway. */ | |
517 | size_t alloc_len = has_ansi_seq ? old_length + 3 + 1 : len + 3 + len2 + 1; | |
518 | ||
519 | char *e = new(char, alloc_len); | |
07630cea LP |
520 | if (!e) |
521 | return NULL; | |
522 | ||
523 | /* | |
cb558ab2 | 524 | printf("old_length=%zu new_length=%zu x=%zu len=%zu len2=%zu k=%zu\n", |
07630cea LP |
525 | old_length, new_length, x, len, len2, k); |
526 | */ | |
527 | ||
cb558ab2 | 528 | memcpy_safe(e, s, len); |
8409f688 | 529 | write_ellipsis(e + len, true); |
cb558ab2 ZJS |
530 | |
531 | char *dst = e + len + 3; | |
532 | ||
533 | if (has_ansi_seq) | |
534 | /* Copy over any ANSI sequences in full */ | |
535 | for (const char *p = s + len; p < j; ) { | |
536 | size_t slen = ansi_sequence_length(p, j - p); | |
537 | if (slen > 0) { | |
be492020 | 538 | dst = mempcpy(dst, p, slen); |
cb558ab2 ZJS |
539 | p += slen; |
540 | } else | |
541 | p = utf8_next_char(p); | |
542 | } | |
543 | ||
544 | memcpy_safe(dst, j, len2); | |
545 | dst[len2] = '\0'; | |
07630cea LP |
546 | |
547 | return e; | |
548 | } | |
549 | ||
8409f688 ZJS |
550 | char *cellescape(char *buf, size_t len, const char *s) { |
551 | /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII | |
552 | * characters are copied as they are, everything else is escaped. The result | |
553 | * is different then if escaping and ellipsization was performed in two | |
554 | * separate steps, because each sequence is either stored in full or skipped. | |
555 | * | |
556 | * This function should be used for logging about strings which expected to | |
557 | * be plain ASCII in a safe way. | |
558 | * | |
559 | * An ellipsis will be used if s is too long. It was always placed at the | |
560 | * very end. | |
561 | */ | |
562 | ||
a01080ce | 563 | size_t i = 0, last_char_width[4] = {}, k = 0; |
61f6e276 LP |
564 | |
565 | assert(len > 0); /* at least a terminating NUL */ | |
8409f688 | 566 | |
61f6e276 LP |
567 | for (;;) { |
568 | char four[4]; | |
569 | int w; | |
8409f688 | 570 | |
61f6e276 | 571 | if (*s == 0) /* terminating NUL detected? then we are done! */ |
8409f688 | 572 | goto done; |
61f6e276 LP |
573 | |
574 | w = cescape_char(*s, four); | |
575 | if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's | |
576 | * ellipsize at the previous location */ | |
577 | break; | |
578 | ||
579 | /* OK, there was space, let's add this escaped character to the buffer */ | |
580 | memcpy(buf + i, four, w); | |
581 | i += w; | |
582 | ||
583 | /* And remember its width in the ring buffer */ | |
584 | last_char_width[k] = w; | |
585 | k = (k + 1) % 4; | |
586 | ||
587 | s++; | |
8409f688 ZJS |
588 | } |
589 | ||
61f6e276 LP |
590 | /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4 |
591 | * characters ideally, but the buffer is shorter than that in the first place take what we can get */ | |
a01080ce | 592 | for (size_t j = 0; j < ELEMENTSOF(last_char_width); j++) { |
61f6e276 LP |
593 | |
594 | if (i + 4 <= len) /* nice, we reached our space goal */ | |
595 | break; | |
596 | ||
597 | k = k == 0 ? 3 : k - 1; | |
598 | if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */ | |
599 | break; | |
600 | ||
601 | assert(i >= last_char_width[k]); | |
602 | i -= last_char_width[k]; | |
8409f688 ZJS |
603 | } |
604 | ||
61f6e276 LP |
605 | if (i + 4 <= len) /* yay, enough space */ |
606 | i += write_ellipsis(buf + i, false); | |
607 | else if (i + 3 <= len) { /* only space for ".." */ | |
608 | buf[i++] = '.'; | |
609 | buf[i++] = '.'; | |
610 | } else if (i + 2 <= len) /* only space for a single "." */ | |
611 | buf[i++] = '.'; | |
612 | else | |
613 | assert(i + 1 <= len); | |
614 | ||
8409f688 ZJS |
615 | done: |
616 | buf[i] = '\0'; | |
617 | return buf; | |
618 | } | |
619 | ||
07630cea LP |
620 | char* strshorten(char *s, size_t l) { |
621 | assert(s); | |
622 | ||
47b33c7d | 623 | if (strnlen(s, l+1) > l) |
07630cea LP |
624 | s[l] = 0; |
625 | ||
626 | return s; | |
627 | } | |
628 | ||
2812017c | 629 | int strgrowpad0(char **s, size_t l) { |
8e479584 LP |
630 | size_t sz; |
631 | ||
2812017c DDM |
632 | assert(s); |
633 | ||
8e479584 LP |
634 | if (*s) { |
635 | sz = strlen(*s) + 1; | |
636 | if (sz >= l) /* never shrink */ | |
637 | return 0; | |
638 | } else | |
639 | sz = 0; | |
640 | ||
2812017c DDM |
641 | char *q = realloc(*s, l); |
642 | if (!q) | |
643 | return -ENOMEM; | |
8e479584 | 644 | |
2812017c DDM |
645 | *s = q; |
646 | ||
2812017c DDM |
647 | memzero(*s + sz, l - sz); |
648 | return 0; | |
649 | } | |
650 | ||
07630cea | 651 | char *strreplace(const char *text, const char *old_string, const char *new_string) { |
319a4f4b | 652 | size_t l, old_len, new_len; |
9d73565a | 653 | char *t, *ret = NULL; |
07630cea | 654 | const char *f; |
07630cea | 655 | |
07630cea LP |
656 | assert(old_string); |
657 | assert(new_string); | |
658 | ||
9d73565a LP |
659 | if (!text) |
660 | return NULL; | |
661 | ||
07630cea LP |
662 | old_len = strlen(old_string); |
663 | new_len = strlen(new_string); | |
664 | ||
665 | l = strlen(text); | |
319a4f4b | 666 | if (!GREEDY_REALLOC(ret, l+1)) |
07630cea LP |
667 | return NULL; |
668 | ||
669 | f = text; | |
9d73565a | 670 | t = ret; |
07630cea | 671 | while (*f) { |
07630cea LP |
672 | size_t d, nl; |
673 | ||
674 | if (!startswith(f, old_string)) { | |
675 | *(t++) = *(f++); | |
676 | continue; | |
677 | } | |
678 | ||
9d73565a | 679 | d = t - ret; |
07630cea | 680 | nl = l - old_len + new_len; |
9d73565a | 681 | |
319a4f4b | 682 | if (!GREEDY_REALLOC(ret, nl + 1)) |
9d73565a | 683 | return mfree(ret); |
07630cea LP |
684 | |
685 | l = nl; | |
9d73565a | 686 | t = ret + d; |
07630cea LP |
687 | |
688 | t = stpcpy(t, new_string); | |
689 | f += old_len; | |
690 | } | |
691 | ||
692 | *t = 0; | |
9d73565a | 693 | return ret; |
07630cea LP |
694 | } |
695 | ||
6fb05690 LP |
696 | static void advance_offsets( |
697 | ssize_t diff, | |
698 | size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */ | |
699 | size_t shift[static 2], | |
700 | size_t size) { | |
701 | ||
b4766d5f ZJS |
702 | if (!offsets) |
703 | return; | |
704 | ||
6fb05690 LP |
705 | assert(shift); |
706 | ||
b4766d5f ZJS |
707 | if ((size_t) diff < offsets[0]) |
708 | shift[0] += size; | |
709 | if ((size_t) diff < offsets[1]) | |
710 | shift[1] += size; | |
711 | } | |
712 | ||
713 | char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) { | |
62a3fc6d | 714 | const char *begin = NULL; |
07630cea LP |
715 | enum { |
716 | STATE_OTHER, | |
717 | STATE_ESCAPE, | |
695a944c LP |
718 | STATE_CSI, |
719 | STATE_CSO, | |
07630cea | 720 | } state = STATE_OTHER; |
2485b7e2 YW |
721 | _cleanup_(memstream_done) MemStream m = {}; |
722 | size_t isz, shift[2] = {}, n_carriage_returns = 0; | |
723 | FILE *f; | |
07630cea LP |
724 | |
725 | assert(ibuf); | |
726 | assert(*ibuf); | |
727 | ||
695a944c LP |
728 | /* This does three things: |
729 | * | |
730 | * 1. Replaces TABs by 8 spaces | |
731 | * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences | |
732 | * 3. Strips ANSI operating system sequences (CSO), i.e. ESC ']' … BEL sequences | |
62a3fc6d ZJS |
733 | * 4. Strip trailing \r characters (since they would "move the cursor", but have no |
734 | * other effect). | |
695a944c | 735 | * |
2fe21124 ZJS |
736 | * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as |
737 | * are any other special characters. Truncated ANSI sequences are left-as is too. This call is | |
738 | * supposed to suppress the most basic formatting noise, but nothing else. | |
695a944c LP |
739 | * |
740 | * Why care for CSO sequences? Well, to undo what terminal_urlify() and friends generate. */ | |
07630cea LP |
741 | |
742 | isz = _isz ? *_isz : strlen(*ibuf); | |
743 | ||
2fe21124 ZJS |
744 | /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we |
745 | * created f here and it doesn't leave our scope. */ | |
2485b7e2 | 746 | f = memstream_init(&m); |
07630cea LP |
747 | if (!f) |
748 | return NULL; | |
749 | ||
62a3fc6d | 750 | for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) { |
07630cea LP |
751 | |
752 | switch (state) { | |
753 | ||
754 | case STATE_OTHER: | |
755 | if (i >= *ibuf + isz) /* EOT */ | |
756 | break; | |
62a3fc6d ZJS |
757 | |
758 | if (*i == '\r') { | |
759 | n_carriage_returns++; | |
760 | break; | |
761 | } else if (*i == '\n') | |
762 | /* Ignore carriage returns before new line */ | |
763 | n_carriage_returns = 0; | |
764 | for (; n_carriage_returns > 0; n_carriage_returns--) | |
765 | fputc('\r', f); | |
766 | ||
767 | if (*i == '\x1B') | |
07630cea | 768 | state = STATE_ESCAPE; |
b4766d5f | 769 | else if (*i == '\t') { |
0d536673 | 770 | fputs(" ", f); |
b4766d5f ZJS |
771 | advance_offsets(i - *ibuf, highlight, shift, 7); |
772 | } else | |
0d536673 | 773 | fputc(*i, f); |
b4766d5f | 774 | |
07630cea LP |
775 | break; |
776 | ||
777 | case STATE_ESCAPE: | |
62a3fc6d ZJS |
778 | assert(n_carriage_returns == 0); |
779 | ||
07630cea | 780 | if (i >= *ibuf + isz) { /* EOT */ |
0d536673 | 781 | fputc('\x1B', f); |
b4766d5f | 782 | advance_offsets(i - *ibuf, highlight, shift, 1); |
07630cea | 783 | break; |
695a944c LP |
784 | } else if (*i == '[') { /* ANSI CSI */ |
785 | state = STATE_CSI; | |
786 | begin = i + 1; | |
787 | } else if (*i == ']') { /* ANSI CSO */ | |
788 | state = STATE_CSO; | |
07630cea LP |
789 | begin = i + 1; |
790 | } else { | |
0d536673 LP |
791 | fputc('\x1B', f); |
792 | fputc(*i, f); | |
b4766d5f | 793 | advance_offsets(i - *ibuf, highlight, shift, 1); |
07630cea LP |
794 | state = STATE_OTHER; |
795 | } | |
796 | ||
797 | break; | |
798 | ||
695a944c | 799 | case STATE_CSI: |
62a3fc6d | 800 | assert(n_carriage_returns == 0); |
07630cea | 801 | |
695a944c LP |
802 | if (i >= *ibuf + isz || /* EOT … */ |
803 | !strchr("01234567890;m", *i)) { /* … or invalid chars in sequence */ | |
0d536673 LP |
804 | fputc('\x1B', f); |
805 | fputc('[', f); | |
b4766d5f | 806 | advance_offsets(i - *ibuf, highlight, shift, 2); |
07630cea LP |
807 | state = STATE_OTHER; |
808 | i = begin-1; | |
809 | } else if (*i == 'm') | |
810 | state = STATE_OTHER; | |
695a944c LP |
811 | |
812 | break; | |
813 | ||
814 | case STATE_CSO: | |
62a3fc6d | 815 | assert(n_carriage_returns == 0); |
695a944c LP |
816 | |
817 | if (i >= *ibuf + isz || /* EOT … */ | |
818 | (*i != '\a' && (uint8_t) *i < 32U) || (uint8_t) *i > 126U) { /* … or invalid chars in sequence */ | |
819 | fputc('\x1B', f); | |
820 | fputc(']', f); | |
821 | advance_offsets(i - *ibuf, highlight, shift, 2); | |
822 | state = STATE_OTHER; | |
823 | i = begin-1; | |
824 | } else if (*i == '\a') | |
825 | state = STATE_OTHER; | |
826 | ||
07630cea LP |
827 | break; |
828 | } | |
829 | } | |
830 | ||
2485b7e2 YW |
831 | char *obuf; |
832 | if (memstream_finalize(&m, &obuf, _isz) < 0) | |
f392dfb5 | 833 | return NULL; |
07630cea | 834 | |
6fb05690 | 835 | free_and_replace(*ibuf, obuf); |
07630cea | 836 | |
b4766d5f ZJS |
837 | if (highlight) { |
838 | highlight[0] += shift[0]; | |
839 | highlight[1] += shift[1]; | |
840 | } | |
841 | ||
6fb05690 | 842 | return *ibuf; |
07630cea LP |
843 | } |
844 | ||
c2bc710b | 845 | char *strextend_with_separator_internal(char **x, const char *separator, ...) { |
bb8ad9ea | 846 | size_t f, l, l_separator; |
c2bc710b LP |
847 | bool need_separator; |
848 | char *nr, *p; | |
bb8ad9ea | 849 | va_list ap; |
07630cea LP |
850 | |
851 | assert(x); | |
852 | ||
7bf7ce28 | 853 | l = f = strlen_ptr(*x); |
07630cea | 854 | |
bb8ad9ea LP |
855 | need_separator = !isempty(*x); |
856 | l_separator = strlen_ptr(separator); | |
857 | ||
858 | va_start(ap, separator); | |
07630cea LP |
859 | for (;;) { |
860 | const char *t; | |
861 | size_t n; | |
862 | ||
863 | t = va_arg(ap, const char *); | |
864 | if (!t) | |
865 | break; | |
866 | ||
867 | n = strlen(t); | |
bb8ad9ea LP |
868 | |
869 | if (need_separator) | |
870 | n += l_separator; | |
871 | ||
c2bc710b | 872 | if (n >= SIZE_MAX - l) { |
07630cea LP |
873 | va_end(ap); |
874 | return NULL; | |
875 | } | |
876 | ||
877 | l += n; | |
bb8ad9ea | 878 | need_separator = true; |
07630cea LP |
879 | } |
880 | va_end(ap); | |
881 | ||
bb8ad9ea LP |
882 | need_separator = !isempty(*x); |
883 | ||
2a4e1fd0 | 884 | nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1)); |
c2bc710b | 885 | if (!nr) |
07630cea LP |
886 | return NULL; |
887 | ||
c2bc710b LP |
888 | *x = nr; |
889 | p = nr + f; | |
07630cea | 890 | |
bb8ad9ea | 891 | va_start(ap, separator); |
07630cea LP |
892 | for (;;) { |
893 | const char *t; | |
894 | ||
895 | t = va_arg(ap, const char *); | |
896 | if (!t) | |
897 | break; | |
898 | ||
bb8ad9ea LP |
899 | if (need_separator && separator) |
900 | p = stpcpy(p, separator); | |
901 | ||
07630cea | 902 | p = stpcpy(p, t); |
bb8ad9ea LP |
903 | |
904 | need_separator = true; | |
07630cea LP |
905 | } |
906 | va_end(ap); | |
907 | ||
c2bc710b | 908 | assert(p == nr + l); |
bb8ad9ea | 909 | |
07630cea | 910 | *p = 0; |
07630cea | 911 | |
c2bc710b | 912 | return p; |
07630cea LP |
913 | } |
914 | ||
6b13ca8a YW |
915 | int strextendf_with_separator(char **x, const char *separator, const char *format, ...) { |
916 | size_t m, a, l_separator; | |
e9b88a6d LP |
917 | va_list ap; |
918 | int l; | |
919 | ||
920 | /* Appends a formatted string to the specified string. Don't use this in inner loops, since then | |
921 | * we'll spend a tonload of time in determining the length of the string passed in, over and over | |
922 | * again. */ | |
923 | ||
924 | assert(x); | |
925 | assert(format); | |
926 | ||
6b13ca8a YW |
927 | l_separator = isempty(*x) ? 0 : strlen_ptr(separator); |
928 | ||
e9b88a6d LP |
929 | /* Let's try to use the allocated buffer, if there's room at the end still. Otherwise let's extend by 64 chars. */ |
930 | if (*x) { | |
931 | m = strlen(*x); | |
6df28e1f | 932 | a = MALLOC_SIZEOF_SAFE(*x); |
e9b88a6d LP |
933 | assert(a >= m + 1); |
934 | } else | |
935 | m = a = 0; | |
936 | ||
6b13ca8a | 937 | if (a - m < 17 + l_separator) { /* if there's less than 16 chars space, then enlarge the buffer first */ |
e9b88a6d LP |
938 | char *n; |
939 | ||
6b13ca8a YW |
940 | if (_unlikely_(l_separator > SIZE_MAX - 64)) /* overflow check #1 */ |
941 | return -ENOMEM; | |
942 | if (_unlikely_(m > SIZE_MAX - 64 - l_separator)) /* overflow check #2 */ | |
e9b88a6d LP |
943 | return -ENOMEM; |
944 | ||
6b13ca8a | 945 | n = realloc(*x, m + 64 + l_separator); |
e9b88a6d LP |
946 | if (!n) |
947 | return -ENOMEM; | |
948 | ||
949 | *x = n; | |
6df28e1f | 950 | a = MALLOC_SIZEOF_SAFE(*x); |
e9b88a6d LP |
951 | } |
952 | ||
953 | /* Now, let's try to format the string into it */ | |
6b13ca8a | 954 | memcpy_safe(*x + m, separator, l_separator); |
e9b88a6d | 955 | va_start(ap, format); |
6b13ca8a | 956 | l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap); |
e9b88a6d LP |
957 | va_end(ap); |
958 | ||
959 | assert(l >= 0); | |
960 | ||
6b13ca8a | 961 | if ((size_t) l < a - m - l_separator) { |
e9b88a6d LP |
962 | char *n; |
963 | ||
964 | /* Nice! This worked. We are done. But first, let's return the extra space we don't | |
965 | * need. This should be a cheap operation, since we only lower the allocation size here, | |
966 | * never increase. */ | |
6b13ca8a | 967 | n = realloc(*x, m + (size_t) l + l_separator + 1); |
e9b88a6d LP |
968 | if (n) |
969 | *x = n; | |
970 | } else { | |
971 | char *n; | |
972 | ||
973 | /* Wasn't enough. Then let's allocate exactly what we need. */ | |
974 | ||
6b13ca8a | 975 | if (_unlikely_((size_t) l > SIZE_MAX - (l_separator + 1))) /* overflow check #1 */ |
e9b88a6d | 976 | goto oom; |
6b13ca8a | 977 | if (_unlikely_(m > SIZE_MAX - ((size_t) l + l_separator + 1))) /* overflow check #2 */ |
e9b88a6d LP |
978 | goto oom; |
979 | ||
6b13ca8a | 980 | a = m + (size_t) l + l_separator + 1; |
e9b88a6d LP |
981 | n = realloc(*x, a); |
982 | if (!n) | |
983 | goto oom; | |
984 | *x = n; | |
985 | ||
986 | va_start(ap, format); | |
6b13ca8a | 987 | l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap); |
e9b88a6d LP |
988 | va_end(ap); |
989 | ||
6b13ca8a | 990 | assert((size_t) l < a - m - l_separator); |
e9b88a6d LP |
991 | } |
992 | ||
993 | return 0; | |
994 | ||
995 | oom: | |
996 | /* truncate the bytes added after the first vsnprintf() attempt again */ | |
997 | (*x)[m] = 0; | |
998 | return -ENOMEM; | |
999 | } | |
1000 | ||
6b9f6007 LP |
1001 | char *strextendn(char **x, const char *s, size_t l) { |
1002 | assert(x); | |
1003 | assert(s || l == 0); | |
1004 | ||
1005 | if (l == SIZE_MAX) | |
1006 | l = strlen_ptr(s); | |
1007 | else if (l > 0) | |
1008 | l = strnlen(s, l); /* ignore trailing noise */ | |
1009 | ||
1010 | if (l > 0 || !*x) { | |
1011 | size_t q; | |
1012 | char *m; | |
1013 | ||
1014 | q = strlen_ptr(*x); | |
1015 | m = realloc(*x, q + l + 1); | |
1016 | if (!m) | |
1017 | return NULL; | |
1018 | ||
1019 | memcpy_safe(m + q, s, l); | |
1020 | m[q + l] = 0; | |
1021 | ||
1022 | *x = m; | |
1023 | } | |
1024 | ||
1025 | return *x; | |
1026 | } | |
1027 | ||
07630cea | 1028 | char *strrep(const char *s, unsigned n) { |
07630cea | 1029 | char *r, *p; |
fe96c0f8 | 1030 | size_t l; |
07630cea LP |
1031 | |
1032 | assert(s); | |
1033 | ||
1034 | l = strlen(s); | |
1035 | p = r = malloc(l * n + 1); | |
1036 | if (!r) | |
1037 | return NULL; | |
1038 | ||
fe96c0f8 | 1039 | for (unsigned i = 0; i < n; i++) |
07630cea LP |
1040 | p = stpcpy(p, s); |
1041 | ||
1042 | *p = 0; | |
1043 | return r; | |
1044 | } | |
1045 | ||
1046 | int split_pair(const char *s, const char *sep, char **l, char **r) { | |
1047 | char *x, *a, *b; | |
1048 | ||
1049 | assert(s); | |
1050 | assert(sep); | |
1051 | assert(l); | |
1052 | assert(r); | |
1053 | ||
1054 | if (isempty(sep)) | |
1055 | return -EINVAL; | |
1056 | ||
1057 | x = strstr(s, sep); | |
1058 | if (!x) | |
1059 | return -EINVAL; | |
1060 | ||
1061 | a = strndup(s, x - s); | |
1062 | if (!a) | |
1063 | return -ENOMEM; | |
1064 | ||
1065 | b = strdup(x + strlen(sep)); | |
1066 | if (!b) { | |
1067 | free(a); | |
1068 | return -ENOMEM; | |
1069 | } | |
1070 | ||
1071 | *l = a; | |
1072 | *r = b; | |
1073 | ||
1074 | return 0; | |
1075 | } | |
1076 | ||
1077 | int free_and_strdup(char **p, const char *s) { | |
1078 | char *t; | |
1079 | ||
1080 | assert(p); | |
1081 | ||
7f546026 | 1082 | /* Replaces a string pointer with a strdup()ed new string, |
07630cea LP |
1083 | * possibly freeing the old one. */ |
1084 | ||
1085 | if (streq_ptr(*p, s)) | |
1086 | return 0; | |
1087 | ||
1088 | if (s) { | |
1089 | t = strdup(s); | |
1090 | if (!t) | |
1091 | return -ENOMEM; | |
1092 | } else | |
1093 | t = NULL; | |
1094 | ||
d6f2cd67 | 1095 | free_and_replace(*p, t); |
07630cea LP |
1096 | |
1097 | return 1; | |
1098 | } | |
1099 | ||
7f546026 ZJS |
1100 | int free_and_strndup(char **p, const char *s, size_t l) { |
1101 | char *t; | |
1102 | ||
1103 | assert(p); | |
1104 | assert(s || l == 0); | |
1105 | ||
1106 | /* Replaces a string pointer with a strndup()ed new string, | |
1107 | * freeing the old one. */ | |
1108 | ||
1109 | if (!*p && !s) | |
1110 | return 0; | |
1111 | ||
1112 | if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0')) | |
1113 | return 0; | |
1114 | ||
1115 | if (s) { | |
1116 | t = strndup(s, l); | |
1117 | if (!t) | |
1118 | return -ENOMEM; | |
1119 | } else | |
1120 | t = NULL; | |
1121 | ||
1122 | free_and_replace(*p, t); | |
1123 | return 1; | |
1124 | } | |
1125 | ||
f3e2e81d | 1126 | bool string_is_safe(const char *p) { |
f3e2e81d LP |
1127 | if (!p) |
1128 | return false; | |
1129 | ||
839d1b20 LP |
1130 | /* Checks if the specified string contains no quotes or control characters */ |
1131 | ||
a01080ce | 1132 | for (const char *t = p; *t; t++) { |
f3e2e81d LP |
1133 | if (*t > 0 && *t < ' ') /* no control characters */ |
1134 | return false; | |
1135 | ||
1136 | if (strchr(QUOTES "\\\x7f", *t)) | |
1137 | return false; | |
1138 | } | |
1139 | ||
1140 | return true; | |
1141 | } | |
53caaffd LP |
1142 | |
1143 | char* string_erase(char *x) { | |
1144 | if (!x) | |
1145 | return NULL; | |
1146 | ||
1147 | /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we | |
1148 | * used them. */ | |
1149 | explicit_bzero_safe(x, strlen(x)); | |
1150 | return x; | |
1151 | } | |
8dd6491e LP |
1152 | |
1153 | int string_truncate_lines(const char *s, size_t n_lines, char **ret) { | |
1154 | const char *p = s, *e = s; | |
1155 | bool truncation_applied = false; | |
1156 | char *copy; | |
1157 | size_t n = 0; | |
1158 | ||
1159 | assert(s); | |
1160 | ||
1161 | /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if | |
1162 | * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not | |
1163 | * generated either. */ | |
1164 | ||
1165 | for (;;) { | |
1166 | size_t k; | |
1167 | ||
1168 | k = strcspn(p, "\n"); | |
1169 | ||
1170 | if (p[k] == 0) { | |
1171 | if (k == 0) /* final empty line */ | |
1172 | break; | |
1173 | ||
1174 | if (n >= n_lines) /* above threshold */ | |
1175 | break; | |
1176 | ||
1177 | e = p + k; /* last line to include */ | |
1178 | break; | |
1179 | } | |
1180 | ||
1181 | assert(p[k] == '\n'); | |
1182 | ||
1183 | if (n >= n_lines) | |
1184 | break; | |
1185 | ||
1186 | if (k > 0) | |
1187 | e = p + k; | |
1188 | ||
1189 | p += k + 1; | |
1190 | n++; | |
1191 | } | |
1192 | ||
1193 | /* e points after the last character we want to keep */ | |
1194 | if (isempty(e)) | |
1195 | copy = strdup(s); | |
1196 | else { | |
1197 | if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that | |
1198 | * isn't a new-line or a series of them */ | |
1199 | truncation_applied = true; | |
1200 | ||
1201 | copy = strndup(s, e - s); | |
1202 | } | |
1203 | if (!copy) | |
1204 | return -ENOMEM; | |
1205 | ||
1206 | *ret = copy; | |
1207 | return truncation_applied; | |
1208 | } | |
f6857fa6 LP |
1209 | |
1210 | int string_extract_line(const char *s, size_t i, char **ret) { | |
1211 | const char *p = s; | |
1212 | size_t c = 0; | |
1213 | ||
1214 | /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that, | |
1215 | * and == 0 if we are looking at the last line or already beyond the last line. As special | |
1216 | * optimization, if the first line is requested and the string only consists of one line we return | |
1217 | * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very | |
1218 | * common case. */ | |
1219 | ||
1220 | for (;;) { | |
1221 | const char *q; | |
1222 | ||
1223 | q = strchr(p, '\n'); | |
1224 | if (i == c) { | |
1225 | /* The line we are looking for! */ | |
1226 | ||
1227 | if (q) { | |
1228 | char *m; | |
1229 | ||
1230 | m = strndup(p, q - p); | |
1231 | if (!m) | |
1232 | return -ENOMEM; | |
1233 | ||
1234 | *ret = m; | |
1235 | return !isempty(q + 1); /* more coming? */ | |
1236 | } else { | |
1237 | if (p == s) | |
1238 | *ret = NULL; /* Just use the input string */ | |
1239 | else { | |
1240 | char *m; | |
1241 | ||
1242 | m = strdup(p); | |
1243 | if (!m) | |
1244 | return -ENOMEM; | |
1245 | ||
1246 | *ret = m; | |
1247 | } | |
1248 | ||
1249 | return 0; /* The end */ | |
1250 | } | |
1251 | } | |
1252 | ||
1253 | if (!q) { | |
1254 | char *m; | |
1255 | ||
1256 | /* No more lines, return empty line */ | |
1257 | ||
1258 | m = strdup(""); | |
1259 | if (!m) | |
1260 | return -ENOMEM; | |
1261 | ||
1262 | *ret = m; | |
1263 | return 0; /* The end */ | |
1264 | } | |
1265 | ||
1266 | p = q + 1; | |
1267 | c++; | |
1268 | } | |
1269 | } | |
53cd7f33 | 1270 | |
46bf625a | 1271 | int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word) { |
53cd7f33 ZJS |
1272 | /* In the default mode with no separators specified, we split on whitespace and |
1273 | * don't coalesce separators. */ | |
1274 | const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0; | |
1275 | ||
46bf625a ZJS |
1276 | const char *found = NULL; |
1277 | ||
53cd7f33 ZJS |
1278 | for (const char *p = string;;) { |
1279 | _cleanup_free_ char *w = NULL; | |
1280 | int r; | |
1281 | ||
1282 | r = extract_first_word(&p, &w, separators, flags); | |
1283 | if (r < 0) | |
1284 | return r; | |
1285 | if (r == 0) | |
46bf625a ZJS |
1286 | break; |
1287 | ||
1288 | found = strv_find(words, w); | |
1289 | if (found) | |
1290 | break; | |
53cd7f33 | 1291 | } |
46bf625a ZJS |
1292 | |
1293 | if (ret_word) | |
1294 | *ret_word = found; | |
1295 | return !!found; | |
53cd7f33 | 1296 | } |
8034b42c ADT |
1297 | |
1298 | bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) { | |
1299 | if (!s1 && !s2) | |
1300 | return true; | |
1301 | if (!s1 || !s2) | |
1302 | return false; | |
1303 | ||
1304 | if (!ok) | |
1305 | ok = WHITESPACE; | |
1306 | ||
1307 | for (; *s1 && *s2; s1++, s2++) | |
1308 | if (*s1 != *s2) | |
1309 | break; | |
1310 | ||
1311 | return in_charset(s1, ok) && in_charset(s2, ok); | |
1312 | } | |
072f5f9b YW |
1313 | |
1314 | char *string_replace_char(char *str, char old_char, char new_char) { | |
1315 | assert(str); | |
1316 | assert(old_char != '\0'); | |
1317 | assert(new_char != '\0'); | |
1318 | assert(old_char != new_char); | |
1319 | ||
1320 | for (char *p = strchr(str, old_char); p; p = strchr(p + 1, old_char)) | |
1321 | *p = new_char; | |
1322 | ||
1323 | return str; | |
1324 | } | |
146f4482 | 1325 | |
7153213e LP |
1326 | int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret) { |
1327 | char *b; | |
1328 | ||
1329 | assert(s || n == 0); | |
1330 | assert(mode >= 0); | |
1331 | assert(mode < _MAKE_CSTRING_MODE_MAX); | |
1332 | ||
1333 | /* Converts a sized character buffer into a NUL-terminated NUL string, refusing if there are embedded | |
1334 | * NUL bytes. Whether to expect a trailing NUL byte can be specified via 'mode' */ | |
1335 | ||
1336 | if (n == 0) { | |
1337 | if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL) | |
1338 | return -EINVAL; | |
1339 | ||
1340 | if (!ret) | |
1341 | return 0; | |
1342 | ||
1343 | b = new0(char, 1); | |
1344 | } else { | |
1345 | const char *nul; | |
1346 | ||
1347 | nul = memchr(s, 0, n); | |
1348 | if (nul) { | |
1349 | if (nul < s + n - 1 || /* embedded NUL? */ | |
1350 | mode == MAKE_CSTRING_REFUSE_TRAILING_NUL) | |
1351 | return -EINVAL; | |
1352 | ||
1353 | n--; | |
1354 | } else if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL) | |
1355 | return -EINVAL; | |
1356 | ||
1357 | if (!ret) | |
1358 | return 0; | |
1359 | ||
1360 | b = memdup_suffix0(s, n); | |
1361 | } | |
1362 | if (!b) | |
1363 | return -ENOMEM; | |
1364 | ||
1365 | *ret = b; | |
1366 | return 0; | |
1367 | } | |
1368 | ||
146f4482 YW |
1369 | size_t strspn_from_end(const char *str, const char *accept) { |
1370 | size_t n = 0; | |
1371 | ||
1372 | if (isempty(str)) | |
1373 | return 0; | |
1374 | ||
1375 | if (isempty(accept)) | |
1376 | return 0; | |
1377 | ||
1378 | for (const char *p = str + strlen(str); p > str && strchr(accept, p[-1]); p--) | |
1379 | n++; | |
1380 | ||
1381 | return n; | |
1382 | } | |
e8bec624 LP |
1383 | |
1384 | char *strdupspn(const char *a, const char *accept) { | |
1385 | if (isempty(a) || isempty(accept)) | |
1386 | return strdup(""); | |
1387 | ||
1388 | return strndup(a, strspn(a, accept)); | |
1389 | } | |
1390 | ||
1391 | char *strdupcspn(const char *a, const char *reject) { | |
1392 | if (isempty(a)) | |
1393 | return strdup(""); | |
1394 | if (isempty(reject)) | |
1395 | return strdup(a); | |
1396 | ||
1397 | return strndup(a, strcspn(a, reject)); | |
1398 | } | |
7b82d95f LP |
1399 | |
1400 | char *find_line_startswith(const char *haystack, const char *needle) { | |
1401 | char *p; | |
1402 | ||
1403 | assert(haystack); | |
1404 | assert(needle); | |
1405 | ||
1406 | /* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the | |
1407 | * first character after it */ | |
1408 | ||
1409 | p = strstr(haystack, needle); | |
1410 | if (!p) | |
1411 | return NULL; | |
1412 | ||
1413 | if (p > haystack) | |
1414 | while (p[-1] != '\n') { | |
1415 | p = strstr(p + 1, needle); | |
1416 | if (!p) | |
1417 | return NULL; | |
1418 | } | |
1419 | ||
1420 | return p + strlen(needle); | |
1421 | } | |
70cc7ed9 DDM |
1422 | |
1423 | char *startswith_strv(const char *string, char **strv) { | |
1424 | char *found = NULL; | |
1425 | ||
1426 | STRV_FOREACH(i, strv) { | |
1427 | found = startswith(string, *i); | |
1428 | if (found) | |
1429 | break; | |
1430 | } | |
1431 | ||
1432 | return found; | |
1433 | } | |
f5c6b4f4 LP |
1434 | |
1435 | bool version_is_valid(const char *s) { | |
1436 | if (isempty(s)) | |
1437 | return false; | |
1438 | ||
1439 | if (!filename_part_is_valid(s)) | |
1440 | return false; | |
1441 | ||
1442 | /* This is a superset of the characters used by semver. We additionally allow "," and "_". */ | |
1443 | if (!in_charset(s, ALPHANUMERICAL ".,_-+")) | |
1444 | return false; | |
1445 | ||
1446 | return true; | |
1447 | } | |
c46f5680 JB |
1448 | |
1449 | bool version_is_valid_versionspec(const char *s) { | |
1450 | if (!filename_part_is_valid(s)) | |
1451 | return false; | |
1452 | ||
1453 | if (!in_charset(s, ALPHANUMERICAL "-.~^")) | |
1454 | return false; | |
1455 | ||
1456 | return true; | |
1457 | } | |
7ef5b0a4 LP |
1458 | |
1459 | ssize_t strlevenshtein(const char *x, const char *y) { | |
1460 | _cleanup_free_ size_t *t0 = NULL, *t1 = NULL, *t2 = NULL; | |
1461 | size_t xl, yl; | |
1462 | ||
1463 | /* This is inspired from the Linux kernel's Levenshtein implementation */ | |
1464 | ||
1465 | if (streq_ptr(x, y)) | |
1466 | return 0; | |
1467 | ||
1468 | xl = strlen_ptr(x); | |
1469 | if (xl > SSIZE_MAX) | |
1470 | return -E2BIG; | |
1471 | ||
1472 | yl = strlen_ptr(y); | |
1473 | if (yl > SSIZE_MAX) | |
1474 | return -E2BIG; | |
1475 | ||
1476 | if (isempty(x)) | |
1477 | return yl; | |
1478 | if (isempty(y)) | |
1479 | return xl; | |
1480 | ||
1481 | t0 = new0(size_t, yl + 1); | |
1482 | if (!t0) | |
1483 | return -ENOMEM; | |
1484 | t1 = new0(size_t, yl + 1); | |
1485 | if (!t1) | |
1486 | return -ENOMEM; | |
1487 | t2 = new0(size_t, yl + 1); | |
1488 | if (!t2) | |
1489 | return -ENOMEM; | |
1490 | ||
1491 | for (size_t i = 0; i <= yl; i++) | |
1492 | t1[i] = i; | |
1493 | ||
1494 | for (size_t i = 0; i < xl; i++) { | |
1495 | t2[0] = i + 1; | |
1496 | ||
1497 | for (size_t j = 0; j < yl; j++) { | |
1498 | /* Substitution */ | |
1499 | t2[j+1] = t1[j] + (x[i] != y[j]); | |
1500 | ||
1501 | /* Swap */ | |
1502 | if (i > 0 && j > 0 && x[i-1] == y[j] && x[i] == y[j-1] && t2[j+1] > t0[j-1] + 1) | |
1503 | t2[j+1] = t0[j-1] + 1; | |
1504 | ||
1505 | /* Deletion */ | |
1506 | if (t2[j+1] > t1[j+1] + 1) | |
1507 | t2[j+1] = t1[j+1] + 1; | |
1508 | ||
1509 | /* Insertion */ | |
1510 | if (t2[j+1] > t2[j] + 1) | |
1511 | t2[j+1] = t2[j] + 1; | |
1512 | } | |
1513 | ||
1514 | size_t *dummy = t0; | |
1515 | t0 = t1; | |
1516 | t1 = t2; | |
1517 | t2 = dummy; | |
1518 | } | |
1519 | ||
1520 | return t1[yl]; | |
1521 | } |