8961475fc5a91dea011205624035db56d5866d3c
[babeltrace.git] / src / common / common.c
1 /*
2 * Babeltrace common functions
3 *
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #define BT_LOG_OUTPUT_LEVEL log_level
26 #define BT_LOG_TAG "COMMON"
27 #include "logging/log.h"
28
29 #include <unistd.h>
30 #include <string.h>
31 #include <inttypes.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include "common/assert.h"
36 #include <stdarg.h>
37 #include <ctype.h>
38 #include <glib.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <wchar.h>
42 #include <stdbool.h>
43 #include "common/macros.h"
44 #include "common/common.h"
45 #include "compat/unistd.h"
46
47 #ifndef __MINGW32__
48 #include <pwd.h>
49 #include <sys/ioctl.h>
50 #endif
51
52 #define SYSTEM_PLUGIN_PATH BABELTRACE_PLUGINS_DIR
53 #define HOME_ENV_VAR "HOME"
54 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace2/plugins"
55
56 static const char *bt_common_color_code_reset = "";
57 static const char *bt_common_color_code_bold = "";
58 static const char *bt_common_color_code_fg_default = "";
59 static const char *bt_common_color_code_fg_red = "";
60 static const char *bt_common_color_code_fg_green = "";
61 static const char *bt_common_color_code_fg_yellow = "";
62 static const char *bt_common_color_code_fg_blue = "";
63 static const char *bt_common_color_code_fg_magenta = "";
64 static const char *bt_common_color_code_fg_cyan = "";
65 static const char *bt_common_color_code_fg_light_gray = "";
66 static const char *bt_common_color_code_bg_default = "";
67 static const char *bt_common_color_code_bg_red = "";
68 static const char *bt_common_color_code_bg_green = "";
69 static const char *bt_common_color_code_bg_yellow = "";
70 static const char *bt_common_color_code_bg_blue = "";
71 static const char *bt_common_color_code_bg_magenta = "";
72 static const char *bt_common_color_code_bg_cyan = "";
73 static const char *bt_common_color_code_bg_light_gray = "";
74
75 static
76 void __attribute__((constructor)) bt_common_color_ctor(void)
77 {
78 if (bt_common_colors_supported()) {
79 bt_common_color_code_reset = BT_COMMON_COLOR_RESET;
80 bt_common_color_code_bold = BT_COMMON_COLOR_BOLD;
81 bt_common_color_code_fg_default = BT_COMMON_COLOR_FG_DEFAULT;
82 bt_common_color_code_fg_red = BT_COMMON_COLOR_FG_RED;
83 bt_common_color_code_fg_green = BT_COMMON_COLOR_FG_GREEN;
84 bt_common_color_code_fg_yellow = BT_COMMON_COLOR_FG_YELLOW;
85 bt_common_color_code_fg_blue = BT_COMMON_COLOR_FG_BLUE;
86 bt_common_color_code_fg_magenta = BT_COMMON_COLOR_FG_MAGENTA;
87 bt_common_color_code_fg_cyan = BT_COMMON_COLOR_FG_CYAN;
88 bt_common_color_code_fg_light_gray = BT_COMMON_COLOR_FG_LIGHT_GRAY;
89 bt_common_color_code_bg_default = BT_COMMON_COLOR_BG_DEFAULT;
90 bt_common_color_code_bg_red = BT_COMMON_COLOR_BG_RED;
91 bt_common_color_code_bg_green = BT_COMMON_COLOR_BG_GREEN;
92 bt_common_color_code_bg_yellow = BT_COMMON_COLOR_BG_YELLOW;
93 bt_common_color_code_bg_blue = BT_COMMON_COLOR_BG_BLUE;
94 bt_common_color_code_bg_magenta = BT_COMMON_COLOR_BG_MAGENTA;
95 bt_common_color_code_bg_cyan = BT_COMMON_COLOR_BG_CYAN;
96 bt_common_color_code_bg_light_gray = BT_COMMON_COLOR_BG_LIGHT_GRAY;
97 }
98 }
99
100 BT_HIDDEN
101 const char *bt_common_get_system_plugin_path(void)
102 {
103 return SYSTEM_PLUGIN_PATH;
104 }
105
106 #ifdef __MINGW32__
107 BT_HIDDEN
108 bool bt_common_is_setuid_setgid(void)
109 {
110 return false;
111 }
112 #else /* __MINGW32__ */
113 BT_HIDDEN
114 bool bt_common_is_setuid_setgid(void)
115 {
116 return (geteuid() != getuid() || getegid() != getgid());
117 }
118 #endif /* __MINGW32__ */
119
120 #ifdef __MINGW32__
121 static
122 const char *bt_get_home_dir(int log_level)
123 {
124 return g_get_home_dir();
125 }
126 #else /* __MINGW32__ */
127 static
128 char *bt_secure_getenv(const char *name, int log_level)
129 {
130 if (bt_common_is_setuid_setgid()) {
131 BT_LOGD("Disregarding environment variable for setuid/setgid binary: "
132 "name=\"%s\"", name);
133 return NULL;
134 }
135 return getenv(name);
136 }
137
138 static
139 const char *bt_get_home_dir(int log_level)
140 {
141 char *val = NULL;
142 struct passwd *pwd;
143
144 val = bt_secure_getenv(HOME_ENV_VAR, log_level);
145 if (val) {
146 goto end;
147 }
148 /* Fallback on password file. */
149 pwd = getpwuid(getuid());
150 if (!pwd) {
151 goto end;
152 }
153 val = pwd->pw_dir;
154 end:
155 return val;
156 }
157 #endif /* __MINGW32__ */
158
159 BT_HIDDEN
160 char *bt_common_get_home_plugin_path(int log_level)
161 {
162 char *path = NULL;
163 const char *home_dir;
164 size_t length;
165
166 home_dir = bt_get_home_dir(log_level);
167 if (!home_dir) {
168 goto end;
169 }
170
171 length = strlen(home_dir) + strlen(HOME_PLUGIN_SUBPATH) + 1;
172
173 if (length >= PATH_MAX) {
174 BT_LOGW("Home directory path is too long: "
175 "length=%zu, max-length=%u", length, PATH_MAX);
176 goto end;
177 }
178
179 path = malloc(PATH_MAX);
180 if (!path) {
181 goto end;
182 }
183
184 strcpy(path, home_dir);
185 strcat(path, HOME_PLUGIN_SUBPATH);
186
187 end:
188 return path;
189 }
190
191 BT_HIDDEN
192 int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs)
193 {
194 int ret = 0;
195 const char *at;
196 const char *end;
197 size_t init_dirs_len;
198
199 BT_ASSERT(dirs);
200 init_dirs_len = dirs->len;
201
202 if (!paths) {
203 /* Nothing to append */
204 goto end;
205 }
206
207 at = paths;
208 end = paths + strlen(paths);
209
210 while (at < end) {
211 GString *path;
212 const char *next_sep;
213
214 next_sep = strchr(at, G_SEARCHPATH_SEPARATOR);
215 if (next_sep == at) {
216 /*
217 * Empty path: try next character (supported
218 * to conform to the typical parsing of $PATH).
219 */
220 at++;
221 continue;
222 } else if (!next_sep) {
223 /* No more separator: use the remaining */
224 next_sep = paths + strlen(paths);
225 }
226
227 path = g_string_new(NULL);
228 if (!path) {
229 goto error;
230 }
231
232 g_string_append_len(path, at, next_sep - at);
233 at = next_sep + 1;
234 g_ptr_array_add(dirs, path);
235 }
236
237 goto end;
238
239 error:
240 ret = -1;
241
242 /* Remove the new entries in dirs */
243 while (dirs->len > init_dirs_len) {
244 g_ptr_array_remove_index(dirs, init_dirs_len);
245 }
246
247 end:
248 return ret;
249 }
250
251 static
252 bool isarealtty(int fd)
253 {
254 bool istty = false;
255 struct stat tty_stats;
256
257 if (!isatty(fd)) {
258 /* Not a TTY */
259 goto end;
260 }
261
262 if (fstat(fd, &tty_stats) == 0) {
263 if (!S_ISCHR(tty_stats.st_mode)) {
264 /* Not a character device: not a TTY */
265 goto end;
266 }
267 }
268
269 istty = true;
270
271 end:
272 return istty;
273 }
274
275 BT_HIDDEN
276 bool bt_common_colors_supported(void)
277 {
278 static bool supports_colors = false;
279 static bool supports_colors_set = false;
280 const char *term_env_var;
281 const char *term_color_env_var;
282
283 if (supports_colors_set) {
284 goto end;
285 }
286
287 supports_colors_set = true;
288
289 /*
290 * `BABELTRACE_TERM_COLOR` environment variable always overrides
291 * the automatic color support detection.
292 */
293 term_color_env_var = getenv("BABELTRACE_TERM_COLOR");
294 if (term_color_env_var) {
295 if (g_ascii_strcasecmp(term_color_env_var, "always") == 0) {
296 /* Force colors */
297 supports_colors = true;
298 } else if (g_ascii_strcasecmp(term_color_env_var, "never") == 0) {
299 /* Force no colors */
300 goto end;
301 }
302 }
303
304 /* We need a compatible, known terminal */
305 term_env_var = getenv("TERM");
306 if (!term_env_var) {
307 goto end;
308 }
309
310 if (strncmp(term_env_var, "xterm", 5) != 0 &&
311 strncmp(term_env_var, "rxvt", 4) != 0 &&
312 strncmp(term_env_var, "konsole", 7) != 0 &&
313 strncmp(term_env_var, "gnome", 5) != 0 &&
314 strncmp(term_env_var, "screen", 5) != 0 &&
315 strncmp(term_env_var, "tmux", 4) != 0 &&
316 strncmp(term_env_var, "putty", 5) != 0) {
317 goto end;
318 }
319
320 /* Both standard output and error streams need to be TTYs */
321 if (!isarealtty(STDOUT_FILENO) || !isarealtty(STDERR_FILENO)) {
322 goto end;
323 }
324
325 supports_colors = true;
326
327 end:
328 return supports_colors;
329 }
330
331 BT_HIDDEN
332 const char *bt_common_color_reset(void)
333 {
334 return bt_common_color_code_reset;
335 }
336
337 BT_HIDDEN
338 const char *bt_common_color_bold(void)
339 {
340 return bt_common_color_code_bold;
341 }
342
343 BT_HIDDEN
344 const char *bt_common_color_fg_default(void)
345 {
346 return bt_common_color_code_fg_default;
347 }
348
349 BT_HIDDEN
350 const char *bt_common_color_fg_red(void)
351 {
352 return bt_common_color_code_fg_red;
353 }
354
355 BT_HIDDEN
356 const char *bt_common_color_fg_green(void)
357 {
358 return bt_common_color_code_fg_green;
359 }
360
361 BT_HIDDEN
362 const char *bt_common_color_fg_yellow(void)
363 {
364 return bt_common_color_code_fg_yellow;
365 }
366
367 BT_HIDDEN
368 const char *bt_common_color_fg_blue(void)
369 {
370 return bt_common_color_code_fg_blue;
371 }
372
373 BT_HIDDEN
374 const char *bt_common_color_fg_magenta(void)
375 {
376 return bt_common_color_code_fg_magenta;
377 }
378
379 BT_HIDDEN
380 const char *bt_common_color_fg_cyan(void)
381 {
382 return bt_common_color_code_fg_cyan;
383 }
384
385 BT_HIDDEN
386 const char *bt_common_color_fg_light_gray(void)
387 {
388 return bt_common_color_code_fg_light_gray;
389 }
390
391 BT_HIDDEN
392 const char *bt_common_color_bg_default(void)
393 {
394 return bt_common_color_code_bg_default;
395 }
396
397 BT_HIDDEN
398 const char *bt_common_color_bg_red(void)
399 {
400 return bt_common_color_code_bg_red;
401 }
402
403 BT_HIDDEN
404 const char *bt_common_color_bg_green(void)
405 {
406 return bt_common_color_code_bg_green;
407 }
408
409 BT_HIDDEN
410 const char *bt_common_color_bg_yellow(void)
411 {
412 return bt_common_color_code_bg_yellow;
413 }
414
415 BT_HIDDEN
416 const char *bt_common_color_bg_blue(void)
417 {
418 return bt_common_color_code_bg_blue;
419 }
420
421 BT_HIDDEN
422 const char *bt_common_color_bg_magenta(void)
423 {
424 return bt_common_color_code_bg_magenta;
425 }
426
427 BT_HIDDEN
428 const char *bt_common_color_bg_cyan(void)
429 {
430 return bt_common_color_code_bg_cyan;
431 }
432
433 BT_HIDDEN
434 const char *bt_common_color_bg_light_gray(void)
435 {
436 return bt_common_color_code_bg_light_gray;
437 }
438
439 BT_HIDDEN
440 GString *bt_common_string_until(const char *input, const char *escapable_chars,
441 const char *end_chars, size_t *end_pos)
442 {
443 GString *output = g_string_new(NULL);
444 const char *ch;
445 const char *es_char;
446 const char *end_char;
447
448 if (!output) {
449 goto error;
450 }
451
452 for (ch = input; *ch != '\0'; ch++) {
453 if (*ch == '\\') {
454 bool continue_loop = false;
455
456 if (ch[1] == '\0') {
457 /* `\` at the end of the string: append `\` */
458 g_string_append_c(output, *ch);
459 ch++;
460 goto set_end_pos;
461 }
462
463 for (es_char = escapable_chars; *es_char != '\0'; es_char++) {
464 if (ch[1] == *es_char) {
465 /*
466 * `\` followed by an escapable
467 * character: append the escaped
468 * character only.
469 */
470 g_string_append_c(output, ch[1]);
471 ch++;
472 continue_loop = true;
473 break;
474 }
475 }
476
477 if (continue_loop) {
478 continue;
479 }
480
481 /*
482 * `\` followed by a non-escapable character:
483 * append `\` and the character.
484 */
485 g_string_append_c(output, *ch);
486 g_string_append_c(output, ch[1]);
487 ch++;
488 continue;
489 } else {
490 for (end_char = end_chars; *end_char != '\0'; end_char++) {
491 if (*ch == *end_char) {
492 /*
493 * End character found:
494 * terminate this loop.
495 */
496 goto set_end_pos;
497 }
498 }
499
500 /* Normal character: append */
501 g_string_append_c(output, *ch);
502 }
503 }
504
505 set_end_pos:
506 if (end_pos) {
507 *end_pos = ch - input;
508 }
509
510 goto end;
511
512 error:
513 if (output) {
514 g_string_free(output, TRUE);
515 output = NULL;
516 }
517
518 end:
519 return output;
520 }
521
522 BT_HIDDEN
523 GString *bt_common_shell_quote(const char *input, bool with_single_quotes)
524 {
525 GString *output = g_string_new(NULL);
526 const char *ch;
527 bool no_quote = true;
528
529 if (!output) {
530 goto end;
531 }
532
533 if (strlen(input) == 0) {
534 if (with_single_quotes) {
535 g_string_assign(output, "''");
536 }
537
538 goto end;
539 }
540
541 for (ch = input; *ch != '\0'; ch++) {
542 const char c = *ch;
543
544 if (!g_ascii_isalpha(c) && !g_ascii_isdigit(c) && c != '_' &&
545 c != '@' && c != '%' && c != '+' && c != '=' &&
546 c != ':' && c != ',' && c != '.' && c != '/' &&
547 c != '-') {
548 no_quote = false;
549 break;
550 }
551 }
552
553 if (no_quote) {
554 g_string_assign(output, input);
555 goto end;
556 }
557
558 if (with_single_quotes) {
559 g_string_assign(output, "'");
560 }
561
562 for (ch = input; *ch != '\0'; ch++) {
563 if (*ch == '\'') {
564 g_string_append(output, "'\"'\"'");
565 } else {
566 g_string_append_c(output, *ch);
567 }
568 }
569
570 if (with_single_quotes) {
571 g_string_append_c(output, '\'');
572 }
573
574 end:
575 return output;
576 }
577
578 BT_HIDDEN
579 bool bt_common_string_is_printable(const char *input)
580 {
581 const char *ch;
582 bool printable = true;
583 BT_ASSERT(input);
584
585 for (ch = input; *ch != '\0'; ch++) {
586 if (!isprint(*ch) && *ch != '\n' && *ch != '\r' &&
587 *ch != '\t' && *ch != '\v') {
588 printable = false;
589 goto end;
590 }
591 }
592
593 end:
594 return printable;
595 }
596
597 BT_HIDDEN
598 void bt_common_destroy_lttng_live_url_parts(
599 struct bt_common_lttng_live_url_parts *parts)
600 {
601 if (!parts) {
602 goto end;
603 }
604
605 if (parts->proto) {
606 g_string_free(parts->proto, TRUE);
607 parts->proto = NULL;
608 }
609
610 if (parts->hostname) {
611 g_string_free(parts->hostname, TRUE);
612 parts->hostname = NULL;
613 }
614
615 if (parts->target_hostname) {
616 g_string_free(parts->target_hostname, TRUE);
617 parts->target_hostname = NULL;
618 }
619
620 if (parts->session_name) {
621 g_string_free(parts->session_name, TRUE);
622 parts->session_name = NULL;
623 }
624
625 end:
626 return;
627 }
628
629 BT_HIDDEN
630 struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
631 const char *url, char *error_buf, size_t error_buf_size)
632 {
633 struct bt_common_lttng_live_url_parts parts;
634 const char *at = url;
635 size_t end_pos;
636
637 BT_ASSERT(url);
638 memset(&parts, 0, sizeof(parts));
639 parts.port = -1;
640
641 /* Protocol */
642 parts.proto = bt_common_string_until(at, "", ":", &end_pos);
643 if (!parts.proto || parts.proto->len == 0) {
644 if (error_buf) {
645 snprintf(error_buf, error_buf_size, "Missing protocol");
646 }
647
648 goto error;
649 }
650
651 if (strcmp(parts.proto->str, "net") == 0) {
652 g_string_assign(parts.proto, "net4");
653 }
654
655 if (strcmp(parts.proto->str, "net4") != 0 &&
656 strcmp(parts.proto->str, "net6") != 0) {
657 if (error_buf) {
658 snprintf(error_buf, error_buf_size,
659 "Unknown protocol: `%s`", parts.proto->str);
660 }
661
662 goto error;
663 }
664
665 if (at[end_pos] != ':') {
666 if (error_buf) {
667 snprintf(error_buf, error_buf_size,
668 "Expecting `:` after `%s`", parts.proto->str);
669 }
670
671 goto error;
672 }
673
674 at += end_pos;
675
676 /* `://` */
677 if (strncmp(at, "://", 3) != 0) {
678 if (error_buf) {
679 snprintf(error_buf, error_buf_size,
680 "Expecting `://` after protocol");
681 }
682
683 goto error;
684 }
685
686 /* Skip `://` */
687 at += 3;
688
689 /* Hostname */
690 parts.hostname = bt_common_string_until(at, "", ":/", &end_pos);
691 if (!parts.hostname || parts.hostname->len == 0) {
692 if (error_buf) {
693 snprintf(error_buf, error_buf_size, "Missing hostname");
694 }
695
696 goto error;
697 }
698
699 if (at[end_pos] == ':') {
700 /* Port */
701 GString *port;
702
703 at += end_pos + 1;
704 port = bt_common_string_until(at, "", "/", &end_pos);
705 if (!port || port->len == 0) {
706 if (error_buf) {
707 snprintf(error_buf, error_buf_size, "Missing port");
708 }
709
710 goto error;
711 }
712
713 if (sscanf(port->str, "%d", &parts.port) != 1) {
714 if (error_buf) {
715 snprintf(error_buf, error_buf_size,
716 "Invalid port: `%s`", port->str);
717 }
718
719 g_string_free(port, TRUE);
720 goto error;
721 }
722
723 g_string_free(port, TRUE);
724
725 if (parts.port < 0 || parts.port >= 65536) {
726 if (error_buf) {
727 snprintf(error_buf, error_buf_size,
728 "Invalid port: %d", parts.port);
729 }
730
731 goto error;
732 }
733 }
734
735 if (at[end_pos] == '\0') {
736 /* Relay daemon hostname and ports provided only */
737 goto end;
738 }
739
740 at += end_pos;
741
742 /* `/host/` */
743 if (strncmp(at, "/host/", 6) != 0) {
744 if (error_buf) {
745 snprintf(error_buf, error_buf_size,
746 "Expecting `/host/` after hostname or port");
747 }
748
749 goto error;
750 }
751
752 at += 6;
753
754 /* Target hostname */
755 parts.target_hostname = bt_common_string_until(at, "", "/", &end_pos);
756 if (!parts.target_hostname || parts.target_hostname->len == 0) {
757 if (error_buf) {
758 snprintf(error_buf, error_buf_size,
759 "Missing target hostname");
760 }
761
762 goto error;
763 }
764
765 if (at[end_pos] == '\0') {
766 if (error_buf) {
767 snprintf(error_buf, error_buf_size,
768 "Missing `/` after target hostname (`%s`)",
769 parts.target_hostname->str);
770 }
771
772 goto error;
773 }
774
775 /* Skip `/` */
776 at += end_pos + 1;
777
778 /* Session name */
779 parts.session_name = bt_common_string_until(at, "", "/", &end_pos);
780 if (!parts.session_name || parts.session_name->len == 0) {
781 if (error_buf) {
782 snprintf(error_buf, error_buf_size,
783 "Missing session name");
784 }
785
786 goto error;
787 }
788
789 if (at[end_pos] == '/') {
790 if (error_buf) {
791 snprintf(error_buf, error_buf_size,
792 "Unexpected `/` after session name (`%s`)",
793 parts.session_name->str);
794 }
795
796 goto error;
797 }
798
799 goto end;
800
801 error:
802 bt_common_destroy_lttng_live_url_parts(&parts);
803
804 end:
805 return parts;
806 }
807
808 BT_HIDDEN
809 void bt_common_normalize_star_glob_pattern(char *pattern)
810 {
811 const char *p;
812 char *np;
813 bool got_star = false;
814
815 BT_ASSERT(pattern);
816
817 for (p = pattern, np = pattern; *p != '\0'; p++) {
818 switch (*p) {
819 case '*':
820 if (got_star) {
821 /* Avoid consecutive stars. */
822 continue;
823 }
824
825 got_star = true;
826 break;
827 case '\\':
828 /* Copy backslash character. */
829 *np = *p;
830 np++;
831 p++;
832
833 if (*p == '\0') {
834 goto end;
835 }
836
837 /* fall-through */
838 default:
839 got_star = false;
840 break;
841 }
842
843 /* Copy single character. */
844 *np = *p;
845 np++;
846 }
847
848 end:
849 *np = '\0';
850 }
851
852 static inline
853 bool at_end_of_pattern(const char *p, const char *pattern, size_t pattern_len)
854 {
855 return (p - pattern) == pattern_len || *p == '\0';
856 }
857
858 /*
859 * Globbing matching function with the star feature only (`?` and
860 * character sets are not supported). This matches `candidate` (plain
861 * string) against `pattern`. A literal star can be escaped with `\` in
862 * `pattern`.
863 *
864 * `pattern_len` or `candidate_len` can be greater than the actual
865 * string length of `pattern` or `candidate` if the string is
866 * null-terminated.
867 */
868 BT_HIDDEN
869 bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
870 const char *candidate, size_t candidate_len) {
871 const char *retry_c = candidate, *retry_p = pattern, *c, *p;
872 bool got_a_star = false;
873
874 retry:
875 c = retry_c;
876 p = retry_p;
877
878 /*
879 * The concept here is to retry a match in the specific case
880 * where we already got a star. The retry position for the
881 * pattern is just after the most recent star, and the retry
882 * position for the candidate is the character following the
883 * last try's first character.
884 *
885 * Example:
886 *
887 * candidate: hi ev every onyx one
888 * ^
889 * pattern: hi*every*one
890 * ^
891 *
892 * candidate: hi ev every onyx one
893 * ^
894 * pattern: hi*every*one
895 * ^
896 *
897 * candidate: hi ev every onyx one
898 * ^
899 * pattern: hi*every*one
900 * ^
901 *
902 * candidate: hi ev every onyx one
903 * ^
904 * pattern: hi*every*one
905 * ^ MISMATCH
906 *
907 * candidate: hi ev every onyx one
908 * ^
909 * pattern: hi*every*one
910 * ^
911 *
912 * candidate: hi ev every onyx one
913 * ^^
914 * pattern: hi*every*one
915 * ^^
916 *
917 * candidate: hi ev every onyx one
918 * ^ ^
919 * pattern: hi*every*one
920 * ^ ^ MISMATCH
921 *
922 * candidate: hi ev every onyx one
923 * ^
924 * pattern: hi*every*one
925 * ^ MISMATCH
926 *
927 * candidate: hi ev every onyx one
928 * ^
929 * pattern: hi*every*one
930 * ^ MISMATCH
931 *
932 * candidate: hi ev every onyx one
933 * ^
934 * pattern: hi*every*one
935 * ^
936 *
937 * candidate: hi ev every onyx one
938 * ^^
939 * pattern: hi*every*one
940 * ^^
941 *
942 * candidate: hi ev every onyx one
943 * ^ ^
944 * pattern: hi*every*one
945 * ^ ^
946 *
947 * candidate: hi ev every onyx one
948 * ^ ^
949 * pattern: hi*every*one
950 * ^ ^
951 *
952 * candidate: hi ev every onyx one
953 * ^ ^
954 * pattern: hi*every*one
955 * ^ ^
956 *
957 * candidate: hi ev every onyx one
958 * ^
959 * pattern: hi*every*one
960 * ^
961 *
962 * candidate: hi ev every onyx one
963 * ^
964 * pattern: hi*every*one
965 * ^ MISMATCH
966 *
967 * candidate: hi ev every onyx one
968 * ^
969 * pattern: hi*every*one
970 * ^
971 *
972 * candidate: hi ev every onyx one
973 * ^^
974 * pattern: hi*every*one
975 * ^^
976 *
977 * candidate: hi ev every onyx one
978 * ^ ^
979 * pattern: hi*every*one
980 * ^ ^ MISMATCH
981 *
982 * candidate: hi ev every onyx one
983 * ^
984 * pattern: hi*every*one
985 * ^ MISMATCH
986 *
987 * candidate: hi ev every onyx one
988 * ^
989 * pattern: hi*every*one
990 * ^ MISMATCH
991 *
992 * candidate: hi ev every onyx one
993 * ^
994 * pattern: hi*every*one
995 * ^ MISMATCH
996 *
997 * candidate: hi ev every onyx one
998 * ^
999 * pattern: hi*every*one
1000 * ^ MISMATCH
1001 *
1002 * candidate: hi ev every onyx one
1003 * ^
1004 * pattern: hi*every*one
1005 * ^
1006 *
1007 * candidate: hi ev every onyx one
1008 * ^^
1009 * pattern: hi*every*one
1010 * ^^
1011 *
1012 * candidate: hi ev every onyx one
1013 * ^ ^
1014 * pattern: hi*every*one
1015 * ^ ^
1016 *
1017 * candidate: hi ev every onyx one
1018 * ^ ^
1019 * pattern: hi*every*one
1020 * ^ ^ SUCCESS
1021 */
1022 while ((c - candidate) < candidate_len && *c != '\0') {
1023 BT_ASSERT(*c);
1024
1025 if (at_end_of_pattern(p, pattern, pattern_len)) {
1026 goto end_of_pattern;
1027 }
1028
1029 switch (*p) {
1030 case '*':
1031 got_a_star = true;
1032
1033 /*
1034 * Our first try starts at the current candidate
1035 * character and after the star in the pattern.
1036 */
1037 retry_c = c;
1038 retry_p = p + 1;
1039
1040 if (at_end_of_pattern(retry_p, pattern, pattern_len)) {
1041 /*
1042 * Star at the end of the pattern at
1043 * this point: automatic match.
1044 */
1045 return true;
1046 }
1047
1048 goto retry;
1049 case '\\':
1050 /* Go to escaped character. */
1051 p++;
1052
1053 /*
1054 * Fall through the default case which compares
1055 * the escaped character now.
1056 */
1057 /* fall-through */
1058 default:
1059 if (at_end_of_pattern(p, pattern, pattern_len) ||
1060 *c != *p) {
1061 end_of_pattern:
1062 /* Character mismatch OR end of pattern. */
1063 if (!got_a_star) {
1064 /*
1065 * We didn't get any star yet,
1066 * so this first mismatch
1067 * automatically makes the whole
1068 * test fail.
1069 */
1070 return false;
1071 }
1072
1073 /*
1074 * Next try: next candidate character,
1075 * original pattern character (following
1076 * the most recent star).
1077 */
1078 retry_c++;
1079 goto retry;
1080 }
1081 break;
1082 }
1083
1084 /* Next pattern and candidate characters. */
1085 c++;
1086 p++;
1087 }
1088
1089 /*
1090 * We checked every candidate character and we're still in a
1091 * success state: the only pattern character allowed to remain
1092 * is a star.
1093 */
1094 if (at_end_of_pattern(p, pattern, pattern_len)) {
1095 return true;
1096 }
1097
1098 p++;
1099 return p[-1] == '*' && at_end_of_pattern(p, pattern, pattern_len);
1100 }
1101
1102 #ifdef __MINGW32__
1103 BT_HIDDEN
1104 GString *bt_common_normalize_path(const char *path, const char *wd)
1105 {
1106 char *tmp;
1107 GString *norm_path = NULL;
1108
1109 BT_ASSERT(path);
1110
1111 tmp = _fullpath(NULL, path, PATH_MAX);
1112 if (!tmp) {
1113 goto error;
1114 }
1115
1116 norm_path = g_string_new(tmp);
1117 if (!norm_path) {
1118 goto error;
1119 }
1120
1121 goto end;
1122 error:
1123 if (norm_path) {
1124 g_string_free(norm_path, TRUE);
1125 norm_path = NULL;
1126 }
1127 end:
1128 free(tmp);
1129 return norm_path;
1130 }
1131 #else
1132 static
1133 void append_path_parts(const char *path, GPtrArray *parts)
1134 {
1135 const char *ch = path;
1136 const char *last = path;
1137
1138 while (true) {
1139 if (*ch == G_DIR_SEPARATOR || *ch == '\0') {
1140 if (ch - last > 0) {
1141 GString *part = g_string_new(NULL);
1142
1143 BT_ASSERT(part);
1144 g_string_append_len(part, last, ch - last);
1145 g_ptr_array_add(parts, part);
1146 }
1147
1148 if (*ch == '\0') {
1149 break;
1150 }
1151
1152 last = ch + 1;
1153 }
1154
1155 ch++;
1156 }
1157 }
1158
1159 static
1160 void destroy_gstring(void *gstring)
1161 {
1162 (void) g_string_free(gstring, TRUE);
1163 }
1164
1165 BT_HIDDEN
1166 GString *bt_common_normalize_path(const char *path, const char *wd)
1167 {
1168 size_t i;
1169 GString *norm_path;
1170 GPtrArray *parts = NULL;
1171
1172 BT_ASSERT(path);
1173 norm_path = g_string_new(G_DIR_SEPARATOR_S);
1174 if (!norm_path) {
1175 goto error;
1176 }
1177
1178 parts = g_ptr_array_new_with_free_func(destroy_gstring);
1179 if (!parts) {
1180 goto error;
1181 }
1182
1183 if (path[0] != G_DIR_SEPARATOR) {
1184 /* Relative path: start with working directory */
1185 if (wd) {
1186 append_path_parts(wd, parts);
1187 } else {
1188 gchar *cd = g_get_current_dir();
1189
1190 append_path_parts(cd, parts);
1191 g_free(cd);
1192 }
1193 }
1194
1195 /* Append parts of the path parameter */
1196 append_path_parts(path, parts);
1197
1198 /* Resolve special `..` and `.` parts */
1199 for (i = 0; i < parts->len; i++) {
1200 GString *part = g_ptr_array_index(parts, i);
1201
1202 if (strcmp(part->str, "..") == 0) {
1203 if (i == 0) {
1204 /*
1205 * First part of absolute path is `..`:
1206 * this is invalid.
1207 */
1208 goto error;
1209 }
1210
1211 /* Remove `..` and previous part */
1212 g_ptr_array_remove_index(parts, i - 1);
1213 g_ptr_array_remove_index(parts, i - 1);
1214 i -= 2;
1215 } else if (strcmp(part->str, ".") == 0) {
1216 /* Remove `.` */
1217 g_ptr_array_remove_index(parts, i);
1218 i -= 1;
1219 }
1220 }
1221
1222 /* Create normalized path with what's left */
1223 for (i = 0; i < parts->len; i++) {
1224 GString *part = g_ptr_array_index(parts, i);
1225
1226 g_string_append(norm_path, part->str);
1227
1228 if (i < parts->len - 1) {
1229 g_string_append_c(norm_path, G_DIR_SEPARATOR);
1230 }
1231 }
1232
1233 goto end;
1234
1235 error:
1236 if (norm_path) {
1237 g_string_free(norm_path, TRUE);
1238 norm_path = NULL;
1239 }
1240
1241 end:
1242 if (parts) {
1243 g_ptr_array_free(parts, TRUE);
1244 }
1245
1246 return norm_path;
1247 }
1248 #endif
1249
1250 BT_HIDDEN
1251 size_t bt_common_get_page_size(int log_level)
1252 {
1253 int page_size;
1254
1255 page_size = bt_sysconf(_SC_PAGESIZE);
1256 if (page_size < 0) {
1257 BT_LOGF("Cannot get system's page size: ret=%d",
1258 page_size);
1259 abort();
1260 }
1261
1262 return page_size;
1263 }
1264
1265 #define BUF_STD_APPEND(...) \
1266 do { \
1267 char _tmp_fmt[64]; \
1268 int _count; \
1269 size_t _size = buf_size - (size_t) (*buf_ch - buf); \
1270 size_t _tmp_fmt_size = (size_t) (fmt_ch - *out_fmt_ch); \
1271 strncpy(_tmp_fmt, *out_fmt_ch, _tmp_fmt_size); \
1272 _tmp_fmt[_tmp_fmt_size] = '\0'; \
1273 _count = snprintf(*buf_ch, _size, _tmp_fmt, __VA_ARGS__); \
1274 BT_ASSERT(_count >= 0); \
1275 *buf_ch += MIN(_count, _size); \
1276 } while (0)
1277
1278 #define BUF_STD_APPEND_SINGLE_ARG(_type) \
1279 do { \
1280 _type _arg = va_arg(*args, _type); \
1281 BUF_STD_APPEND(_arg); \
1282 } while (0)
1283
1284 static inline void handle_conversion_specifier_std(char *buf, char **buf_ch,
1285 size_t buf_size, const char **out_fmt_ch, va_list *args)
1286 {
1287 const char *fmt_ch = *out_fmt_ch;
1288 enum LENGTH_MODIFIER {
1289 LENGTH_MOD_H,
1290 LENGTH_MOD_HH,
1291 LENGTH_MOD_NONE,
1292 LENGTH_MOD_LOW_L,
1293 LENGTH_MOD_LOW_LL,
1294 LENGTH_MOD_UP_L,
1295 LENGTH_MOD_Z,
1296 } length_mod = LENGTH_MOD_NONE;
1297
1298 /* skip '%' */
1299 fmt_ch++;
1300
1301 if (*fmt_ch == '%') {
1302 fmt_ch++;
1303 **buf_ch = '%';
1304 (*buf_ch)++;
1305 goto update_rw_fmt;
1306 }
1307
1308 /* flags */
1309 for (;;) {
1310 switch (*fmt_ch) {
1311 case '-':
1312 case '+':
1313 case ' ':
1314 case '#':
1315 case '0':
1316 case '\'':
1317 fmt_ch++;
1318 continue;
1319 default:
1320 break;
1321 }
1322 break;
1323 }
1324
1325 /* width */
1326 for (;;) {
1327 if (*fmt_ch < '0' || *fmt_ch > '9') {
1328 break;
1329 }
1330
1331 fmt_ch++;
1332 }
1333
1334 /* precision */
1335 if (*fmt_ch == '.') {
1336 fmt_ch++;
1337
1338 for (;;) {
1339 if (*fmt_ch < '0' || *fmt_ch > '9') {
1340 break;
1341 }
1342
1343 fmt_ch++;
1344 }
1345 }
1346
1347 /* format (PRI*64) */
1348 if (strncmp(fmt_ch, PRId64, sizeof(PRId64) - 1) == 0) {
1349 fmt_ch += sizeof(PRId64) - 1;
1350 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1351 goto update_rw_fmt;
1352 } else if (strncmp(fmt_ch, PRIu64, sizeof(PRIu64) - 1) == 0) {
1353 fmt_ch += sizeof(PRIu64) - 1;
1354 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1355 goto update_rw_fmt;
1356 } else if (strncmp(fmt_ch, PRIx64, sizeof(PRIx64) - 1) == 0) {
1357 fmt_ch += sizeof(PRIx64) - 1;
1358 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1359 goto update_rw_fmt;
1360 } else if (strncmp(fmt_ch, PRIX64, sizeof(PRIX64) - 1) == 0) {
1361 fmt_ch += sizeof(PRIX64) - 1;
1362 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1363 goto update_rw_fmt;
1364 } else if (strncmp(fmt_ch, PRIo64, sizeof(PRIo64) - 1) == 0) {
1365 fmt_ch += sizeof(PRIo64) - 1;
1366 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1367 goto update_rw_fmt;
1368 } else if (strncmp(fmt_ch, PRIi64, sizeof(PRIi64) - 1) == 0) {
1369 fmt_ch += sizeof(PRIi64) - 1;
1370 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1371 goto update_rw_fmt;
1372 }
1373
1374 // length modifier
1375 switch (*fmt_ch) {
1376 case 'h':
1377 length_mod = LENGTH_MOD_H;
1378 fmt_ch++;
1379
1380 if (*fmt_ch == 'h') {
1381 length_mod = LENGTH_MOD_HH;
1382 fmt_ch++;
1383 break;
1384 }
1385 break;
1386 case 'l':
1387 length_mod = LENGTH_MOD_LOW_L;
1388 fmt_ch++;
1389
1390 if (*fmt_ch == 'l') {
1391 length_mod = LENGTH_MOD_LOW_LL;
1392 fmt_ch++;
1393 break;
1394 }
1395 break;
1396 case 'L':
1397 length_mod = LENGTH_MOD_UP_L;
1398 fmt_ch++;
1399 break;
1400 case 'z':
1401 length_mod = LENGTH_MOD_Z;
1402 fmt_ch++;
1403 break;
1404 default:
1405 break;
1406 }
1407
1408 // format
1409 switch (*fmt_ch) {
1410 case 'c':
1411 {
1412 fmt_ch++;
1413
1414 switch (length_mod) {
1415 case LENGTH_MOD_NONE:
1416 BUF_STD_APPEND_SINGLE_ARG(int);
1417 break;
1418 case LENGTH_MOD_LOW_L:
1419 BUF_STD_APPEND_SINGLE_ARG(wint_t);
1420 break;
1421 default:
1422 abort();
1423 }
1424 break;
1425 }
1426 case 's':
1427 fmt_ch++;
1428
1429 switch (length_mod) {
1430 case LENGTH_MOD_NONE:
1431 BUF_STD_APPEND_SINGLE_ARG(char *);
1432 break;
1433 case LENGTH_MOD_LOW_L:
1434 BUF_STD_APPEND_SINGLE_ARG(wchar_t *);
1435 break;
1436 default:
1437 abort();
1438 }
1439 break;
1440 case 'd':
1441 case 'i':
1442 fmt_ch++;
1443
1444 switch (length_mod) {
1445 case LENGTH_MOD_NONE:
1446 case LENGTH_MOD_H:
1447 case LENGTH_MOD_HH:
1448 BUF_STD_APPEND_SINGLE_ARG(int);
1449 break;
1450 case LENGTH_MOD_LOW_L:
1451 BUF_STD_APPEND_SINGLE_ARG(long);
1452 break;
1453 case LENGTH_MOD_LOW_LL:
1454 BUF_STD_APPEND_SINGLE_ARG(long long);
1455 break;
1456 case LENGTH_MOD_Z:
1457 BUF_STD_APPEND_SINGLE_ARG(size_t);
1458 break;
1459 default:
1460 abort();
1461 }
1462 break;
1463 case 'o':
1464 case 'x':
1465 case 'X':
1466 case 'u':
1467 fmt_ch++;
1468
1469 switch (length_mod) {
1470 case LENGTH_MOD_NONE:
1471 case LENGTH_MOD_H:
1472 case LENGTH_MOD_HH:
1473 BUF_STD_APPEND_SINGLE_ARG(unsigned int);
1474 break;
1475 case LENGTH_MOD_LOW_L:
1476 BUF_STD_APPEND_SINGLE_ARG(unsigned long);
1477 break;
1478 case LENGTH_MOD_LOW_LL:
1479 BUF_STD_APPEND_SINGLE_ARG(unsigned long long);
1480 break;
1481 case LENGTH_MOD_Z:
1482 BUF_STD_APPEND_SINGLE_ARG(size_t);
1483 break;
1484 default:
1485 abort();
1486 }
1487 break;
1488 case 'f':
1489 case 'F':
1490 case 'e':
1491 case 'E':
1492 case 'g':
1493 case 'G':
1494 fmt_ch++;
1495
1496 switch (length_mod) {
1497 case LENGTH_MOD_NONE:
1498 BUF_STD_APPEND_SINGLE_ARG(double);
1499 break;
1500 case LENGTH_MOD_UP_L:
1501 BUF_STD_APPEND_SINGLE_ARG(long double);
1502 break;
1503 default:
1504 abort();
1505 }
1506 break;
1507 case 'p':
1508 fmt_ch++;
1509
1510 if (length_mod == LENGTH_MOD_NONE) {
1511 BUF_STD_APPEND_SINGLE_ARG(void *);
1512 } else {
1513 abort();
1514 }
1515 break;
1516 default:
1517 abort();
1518 }
1519
1520 update_rw_fmt:
1521 *out_fmt_ch = fmt_ch;
1522 }
1523
1524 BT_HIDDEN
1525 void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
1526 char intro,
1527 bt_common_handle_custom_specifier_func handle_specifier,
1528 void *priv_data, const char *fmt, va_list *args)
1529 {
1530 const char *fmt_ch = fmt;
1531 char *buf_ch = buf;
1532
1533 BT_ASSERT(buf);
1534 BT_ASSERT(fmt);
1535
1536 while (*fmt_ch != '\0') {
1537 switch (*fmt_ch) {
1538 case '%':
1539 BT_ASSERT(fmt_ch[1] != '\0');
1540
1541 if (fmt_ch[1] == intro) {
1542 handle_specifier(priv_data, &buf_ch,
1543 buf_size - (size_t) (buf_ch - buf),
1544 &fmt_ch, args);
1545 } else {
1546 handle_conversion_specifier_std(buf, &buf_ch,
1547 buf_size, &fmt_ch, args);
1548 }
1549
1550 if (buf_ch >= buf + buf_size - 1) {
1551 fmt_ch = "";
1552 }
1553 break;
1554 default:
1555 *buf_ch = *fmt_ch;
1556 buf_ch++;
1557 if (buf_ch >= buf + buf_size - 1) {
1558 fmt_ch = "";
1559 }
1560
1561 fmt_ch++;
1562 }
1563 }
1564
1565 *buf_ch = '\0';
1566 }
1567
1568 BT_HIDDEN
1569 void bt_common_custom_snprintf(char *buf, size_t buf_size,
1570 char intro,
1571 bt_common_handle_custom_specifier_func handle_specifier,
1572 void *priv_data, const char *fmt, ...)
1573 {
1574 va_list args;
1575 va_start(args, fmt);
1576 bt_common_custom_vsnprintf(buf, buf_size, intro, handle_specifier,
1577 priv_data, fmt, &args);
1578 va_end(args);
1579 }
1580
1581 BT_HIDDEN
1582 void bt_common_sep_digits(char *str, unsigned int digits_per_group, char sep)
1583 {
1584 const char *rd;
1585 char *wr;
1586 uint64_t i = 0;
1587 uint64_t orig_len;
1588 uint64_t sep_count;
1589 uint64_t new_len;
1590
1591 BT_ASSERT(digits_per_group > 0);
1592 BT_ASSERT(sep != '\0');
1593
1594 /* Compute new length of `str` */
1595 orig_len = strlen(str);
1596 BT_ASSERT(orig_len > 0);
1597 sep_count = (orig_len - 1) / digits_per_group;
1598 new_len = strlen(str) + sep_count;
1599
1600 /*
1601 * Do the work in place. Have the reading pointer `rd` start at
1602 * the end of the original string, and the writing pointer `wr`
1603 * start at the end of the new string, making sure to also put a
1604 * null character there.
1605 */
1606 rd = str + orig_len - 1;
1607 wr = str + new_len;
1608 *wr = '\0';
1609 wr--;
1610
1611 /*
1612 * Here's what the process looks like (3 digits per group):
1613 *
1614 * Source: 12345678
1615 * ^
1616 * Destination: 12345678#8
1617 * ^
1618 *
1619 * Source: 12345678
1620 * ^
1621 * Destination: 1234567878
1622 * ^
1623 *
1624 * Source: 12345678
1625 * ^
1626 * Destination: 1234567678
1627 * ^
1628 *
1629 * Source: 12345678
1630 * ^
1631 * Destination: 123456,678
1632 * ^
1633 *
1634 * Source: 12345678
1635 * ^
1636 * Destination: 123455,678
1637 * ^
1638 *
1639 * Source: 12345678
1640 * ^
1641 * Destination: 123445,678
1642 * ^
1643 *
1644 * Source: 12345678
1645 * ^
1646 * Destination: 123345,678
1647 * ^
1648 *
1649 * Source: 12345678
1650 * ^
1651 * Destination: 12,345,678
1652 * ^
1653 *
1654 * Source: 12345678
1655 * ^
1656 * Destination: 12,345,678
1657 * ^
1658 *
1659 * Source: 12345678
1660 * ^
1661 * Destination: 12,345,678
1662 * ^
1663 */
1664 while (rd != str - 1) {
1665 if (i == digits_per_group) {
1666 /*
1667 * Time to append the separator: decrement `wr`,
1668 * but keep `rd` as is.
1669 */
1670 i = 0;
1671 *wr = sep;
1672 wr--;
1673 continue;
1674 }
1675
1676 /* Copy read-side character to write-side character */
1677 *wr = *rd;
1678 wr--;
1679 rd--;
1680 i++;
1681 }
1682 }
1683
1684 BT_HIDDEN
1685 GString *bt_common_fold(const char *str, unsigned int total_length,
1686 unsigned int indent)
1687 {
1688 const unsigned int content_length = total_length - indent;
1689 GString *folded = g_string_new(NULL);
1690 GString *tmp_line = g_string_new(NULL);
1691 gchar **lines = NULL;
1692 gchar **line_words = NULL;
1693 gchar * const *line;
1694 unsigned int i;
1695
1696 BT_ASSERT(str);
1697 BT_ASSERT(indent < total_length);
1698 BT_ASSERT(tmp_line);
1699 BT_ASSERT(folded);
1700
1701 if (strlen(str) == 0) {
1702 /* Empty input string: empty output string */
1703 goto end;
1704 }
1705
1706 /* Split lines */
1707 lines = g_strsplit(str, "\n", 0);
1708 BT_ASSERT(lines);
1709
1710 /* For each source line */
1711 for (line = lines; *line; line++) {
1712 gchar * const *word;
1713
1714 /*
1715 * Append empty line without indenting if source line is
1716 * empty.
1717 */
1718 if (strlen(*line) == 0) {
1719 g_string_append_c(folded, '\n');
1720 continue;
1721 }
1722
1723 /* Split words */
1724 line_words = g_strsplit(*line, " ", 0);
1725 BT_ASSERT(line_words);
1726
1727 /*
1728 * Indent for first line (we know there's at least one
1729 * word at this point).
1730 */
1731 for (i = 0; i < indent; i++) {
1732 g_string_append_c(folded, ' ');
1733 }
1734
1735 /* Append words, folding when necessary */
1736 g_string_assign(tmp_line, "");
1737
1738 for (word = line_words; *word; word++) {
1739 /*
1740 * `tmp_line->len > 0` is in the condition so
1741 * that words that are larger than
1742 * `content_length` are at least written on
1743 * their own line.
1744 *
1745 * `tmp_line->len - 1` because the temporary
1746 * line always contains a trailing space which
1747 * won't be part of the line if we fold.
1748 */
1749 if (tmp_line->len > 0 &&
1750 tmp_line->len - 1 + strlen(*word) >= content_length) {
1751 /* Fold (without trailing space) */
1752 g_string_append_len(folded,
1753 tmp_line->str, tmp_line->len - 1);
1754 g_string_append_c(folded, '\n');
1755
1756 /* Indent new line */
1757 for (i = 0; i < indent; i++) {
1758 g_string_append_c(folded, ' ');
1759 }
1760
1761 g_string_assign(tmp_line, "");
1762 }
1763
1764 /* Append current word and space to temporary line */
1765 g_string_append(tmp_line, *word);
1766 g_string_append_c(tmp_line, ' ');
1767 }
1768
1769 /* Append last line if any, without trailing space */
1770 if (tmp_line->len > 0) {
1771 g_string_append_len(folded, tmp_line->str,
1772 tmp_line->len - 1);
1773 }
1774
1775 /* Append source newline */
1776 g_string_append_c(folded, '\n');
1777
1778 /* Free array of this line's words */
1779 g_strfreev(line_words);
1780 line_words = NULL;
1781 }
1782
1783 /* Remove trailing newline if any */
1784 if (folded->str[folded->len - 1] == '\n') {
1785 g_string_truncate(folded, folded->len - 1);
1786 }
1787
1788 end:
1789 if (lines) {
1790 g_strfreev(lines);
1791 }
1792
1793 BT_ASSERT(!line_words);
1794
1795 if (tmp_line) {
1796 g_string_free(tmp_line, TRUE);
1797 }
1798
1799 return folded;
1800 }
1801
1802 #ifdef __MINGW32__
1803 BT_HIDDEN
1804 int bt_common_get_term_size(unsigned int *width, unsigned int *height)
1805 {
1806 /* Not supported on Windows yet */
1807 return -1;
1808 }
1809 #else /* __MINGW32__ */
1810 BT_HIDDEN
1811 int bt_common_get_term_size(unsigned int *width, unsigned int *height)
1812 {
1813 int ret = 0;
1814 struct winsize winsize;
1815
1816 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) < 0) {
1817 ret = -1;
1818 goto end;
1819 }
1820
1821 if (width) {
1822 *width = (unsigned int) winsize.ws_col;
1823 }
1824
1825 if (height) {
1826 *height = (unsigned int) winsize.ws_row;
1827 }
1828
1829 end:
1830 return ret;
1831 }
1832 #endif /* __MINGW32__ */
1833
1834 BT_HIDDEN
1835 int bt_common_g_string_append_printf(GString *str, const char *fmt, ...)
1836 {
1837 va_list ap;
1838 gsize len, allocated_len, available_len;
1839 int print_len;
1840
1841 /* str->len excludes \0. */
1842 len = str->len;
1843 /* Explicitly exclude \0. */
1844 allocated_len = str->allocated_len - 1;
1845 available_len = allocated_len - len;
1846
1847 str->len = allocated_len;
1848 va_start(ap, fmt);
1849 print_len = vsnprintf(str->str + len, available_len + 1, fmt, ap);
1850 va_end(ap);
1851 if (print_len < 0) {
1852 return print_len;
1853 }
1854 if (G_UNLIKELY(available_len < print_len)) {
1855 /* Resize. */
1856 g_string_set_size(str, len + print_len);
1857 va_start(ap, fmt);
1858 print_len = vsprintf(str->str + len, fmt, ap);
1859 va_end(ap);
1860 } else {
1861 str->len = len + print_len;
1862 }
1863 return print_len;
1864 }
This page took 0.104204 seconds and 3 git commands to generate.