Always evaluate BT_ASSERT(); add BT_ASSERT_DBG() for debug mode only
[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_DBG(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_DBG(_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 case LENGTH_MOD_LOW_L:
1417 BUF_STD_APPEND_SINGLE_ARG(int);
1418 break;
1419 default:
1420 abort();
1421 }
1422 break;
1423 }
1424 case 's':
1425 fmt_ch++;
1426
1427 switch (length_mod) {
1428 case LENGTH_MOD_NONE:
1429 BUF_STD_APPEND_SINGLE_ARG(char *);
1430 break;
1431 case LENGTH_MOD_LOW_L:
1432 BUF_STD_APPEND_SINGLE_ARG(wchar_t *);
1433 break;
1434 default:
1435 abort();
1436 }
1437 break;
1438 case 'd':
1439 case 'i':
1440 fmt_ch++;
1441
1442 switch (length_mod) {
1443 case LENGTH_MOD_NONE:
1444 case LENGTH_MOD_H:
1445 case LENGTH_MOD_HH:
1446 BUF_STD_APPEND_SINGLE_ARG(int);
1447 break;
1448 case LENGTH_MOD_LOW_L:
1449 BUF_STD_APPEND_SINGLE_ARG(long);
1450 break;
1451 case LENGTH_MOD_LOW_LL:
1452 BUF_STD_APPEND_SINGLE_ARG(long long);
1453 break;
1454 case LENGTH_MOD_Z:
1455 BUF_STD_APPEND_SINGLE_ARG(size_t);
1456 break;
1457 default:
1458 abort();
1459 }
1460 break;
1461 case 'o':
1462 case 'x':
1463 case 'X':
1464 case 'u':
1465 fmt_ch++;
1466
1467 switch (length_mod) {
1468 case LENGTH_MOD_NONE:
1469 case LENGTH_MOD_H:
1470 case LENGTH_MOD_HH:
1471 BUF_STD_APPEND_SINGLE_ARG(unsigned int);
1472 break;
1473 case LENGTH_MOD_LOW_L:
1474 BUF_STD_APPEND_SINGLE_ARG(unsigned long);
1475 break;
1476 case LENGTH_MOD_LOW_LL:
1477 BUF_STD_APPEND_SINGLE_ARG(unsigned long long);
1478 break;
1479 case LENGTH_MOD_Z:
1480 BUF_STD_APPEND_SINGLE_ARG(size_t);
1481 break;
1482 default:
1483 abort();
1484 }
1485 break;
1486 case 'f':
1487 case 'F':
1488 case 'e':
1489 case 'E':
1490 case 'g':
1491 case 'G':
1492 fmt_ch++;
1493
1494 switch (length_mod) {
1495 case LENGTH_MOD_NONE:
1496 BUF_STD_APPEND_SINGLE_ARG(double);
1497 break;
1498 case LENGTH_MOD_UP_L:
1499 BUF_STD_APPEND_SINGLE_ARG(long double);
1500 break;
1501 default:
1502 abort();
1503 }
1504 break;
1505 case 'p':
1506 fmt_ch++;
1507
1508 if (length_mod == LENGTH_MOD_NONE) {
1509 BUF_STD_APPEND_SINGLE_ARG(void *);
1510 } else {
1511 abort();
1512 }
1513 break;
1514 default:
1515 abort();
1516 }
1517
1518 update_rw_fmt:
1519 *out_fmt_ch = fmt_ch;
1520 }
1521
1522 BT_HIDDEN
1523 void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
1524 char intro,
1525 bt_common_handle_custom_specifier_func handle_specifier,
1526 void *priv_data, const char *fmt, va_list *args)
1527 {
1528 const char *fmt_ch = fmt;
1529 char *buf_ch = buf;
1530
1531 BT_ASSERT_DBG(buf);
1532 BT_ASSERT_DBG(fmt);
1533
1534 while (*fmt_ch != '\0') {
1535 switch (*fmt_ch) {
1536 case '%':
1537 BT_ASSERT_DBG(fmt_ch[1] != '\0');
1538
1539 if (fmt_ch[1] == intro) {
1540 handle_specifier(priv_data, &buf_ch,
1541 buf_size - (size_t) (buf_ch - buf),
1542 &fmt_ch, args);
1543 } else {
1544 handle_conversion_specifier_std(buf, &buf_ch,
1545 buf_size, &fmt_ch, args);
1546 }
1547
1548 if (buf_ch >= buf + buf_size - 1) {
1549 fmt_ch = "";
1550 }
1551 break;
1552 default:
1553 *buf_ch = *fmt_ch;
1554 buf_ch++;
1555 if (buf_ch >= buf + buf_size - 1) {
1556 fmt_ch = "";
1557 }
1558
1559 fmt_ch++;
1560 }
1561 }
1562
1563 *buf_ch = '\0';
1564 }
1565
1566 BT_HIDDEN
1567 void bt_common_custom_snprintf(char *buf, size_t buf_size,
1568 char intro,
1569 bt_common_handle_custom_specifier_func handle_specifier,
1570 void *priv_data, const char *fmt, ...)
1571 {
1572 va_list args;
1573 va_start(args, fmt);
1574 bt_common_custom_vsnprintf(buf, buf_size, intro, handle_specifier,
1575 priv_data, fmt, &args);
1576 va_end(args);
1577 }
1578
1579 BT_HIDDEN
1580 void bt_common_sep_digits(char *str, unsigned int digits_per_group, char sep)
1581 {
1582 const char *rd;
1583 char *wr;
1584 uint64_t i = 0;
1585 uint64_t orig_len;
1586 uint64_t sep_count;
1587 uint64_t new_len;
1588
1589 BT_ASSERT_DBG(digits_per_group > 0);
1590 BT_ASSERT_DBG(sep != '\0');
1591
1592 /* Compute new length of `str` */
1593 orig_len = strlen(str);
1594 BT_ASSERT_DBG(orig_len > 0);
1595 sep_count = (orig_len - 1) / digits_per_group;
1596 new_len = strlen(str) + sep_count;
1597
1598 /*
1599 * Do the work in place. Have the reading pointer `rd` start at
1600 * the end of the original string, and the writing pointer `wr`
1601 * start at the end of the new string, making sure to also put a
1602 * null character there.
1603 */
1604 rd = str + orig_len - 1;
1605 wr = str + new_len;
1606 *wr = '\0';
1607 wr--;
1608
1609 /*
1610 * Here's what the process looks like (3 digits per group):
1611 *
1612 * Source: 12345678
1613 * ^
1614 * Destination: 12345678#8
1615 * ^
1616 *
1617 * Source: 12345678
1618 * ^
1619 * Destination: 1234567878
1620 * ^
1621 *
1622 * Source: 12345678
1623 * ^
1624 * Destination: 1234567678
1625 * ^
1626 *
1627 * Source: 12345678
1628 * ^
1629 * Destination: 123456,678
1630 * ^
1631 *
1632 * Source: 12345678
1633 * ^
1634 * Destination: 123455,678
1635 * ^
1636 *
1637 * Source: 12345678
1638 * ^
1639 * Destination: 123445,678
1640 * ^
1641 *
1642 * Source: 12345678
1643 * ^
1644 * Destination: 123345,678
1645 * ^
1646 *
1647 * Source: 12345678
1648 * ^
1649 * Destination: 12,345,678
1650 * ^
1651 *
1652 * Source: 12345678
1653 * ^
1654 * Destination: 12,345,678
1655 * ^
1656 *
1657 * Source: 12345678
1658 * ^
1659 * Destination: 12,345,678
1660 * ^
1661 */
1662 while (rd != str - 1) {
1663 if (i == digits_per_group) {
1664 /*
1665 * Time to append the separator: decrement `wr`,
1666 * but keep `rd` as is.
1667 */
1668 i = 0;
1669 *wr = sep;
1670 wr--;
1671 continue;
1672 }
1673
1674 /* Copy read-side character to write-side character */
1675 *wr = *rd;
1676 wr--;
1677 rd--;
1678 i++;
1679 }
1680 }
1681
1682 BT_HIDDEN
1683 GString *bt_common_fold(const char *str, unsigned int total_length,
1684 unsigned int indent)
1685 {
1686 const unsigned int content_length = total_length - indent;
1687 GString *folded = g_string_new(NULL);
1688 GString *tmp_line = g_string_new(NULL);
1689 gchar **lines = NULL;
1690 gchar **line_words = NULL;
1691 gchar * const *line;
1692 unsigned int i;
1693
1694 BT_ASSERT_DBG(str);
1695 BT_ASSERT_DBG(indent < total_length);
1696 BT_ASSERT_DBG(tmp_line);
1697 BT_ASSERT_DBG(folded);
1698
1699 if (strlen(str) == 0) {
1700 /* Empty input string: empty output string */
1701 goto end;
1702 }
1703
1704 /* Split lines */
1705 lines = g_strsplit(str, "\n", 0);
1706 BT_ASSERT_DBG(lines);
1707
1708 /* For each source line */
1709 for (line = lines; *line; line++) {
1710 gchar * const *word;
1711
1712 /*
1713 * Append empty line without indenting if source line is
1714 * empty.
1715 */
1716 if (strlen(*line) == 0) {
1717 g_string_append_c(folded, '\n');
1718 continue;
1719 }
1720
1721 /* Split words */
1722 line_words = g_strsplit(*line, " ", 0);
1723 BT_ASSERT_DBG(line_words);
1724
1725 /*
1726 * Indent for first line (we know there's at least one
1727 * word at this point).
1728 */
1729 for (i = 0; i < indent; i++) {
1730 g_string_append_c(folded, ' ');
1731 }
1732
1733 /* Append words, folding when necessary */
1734 g_string_assign(tmp_line, "");
1735
1736 for (word = line_words; *word; word++) {
1737 /*
1738 * `tmp_line->len > 0` is in the condition so
1739 * that words that are larger than
1740 * `content_length` are at least written on
1741 * their own line.
1742 *
1743 * `tmp_line->len - 1` because the temporary
1744 * line always contains a trailing space which
1745 * won't be part of the line if we fold.
1746 */
1747 if (tmp_line->len > 0 &&
1748 tmp_line->len - 1 + strlen(*word) >= content_length) {
1749 /* Fold (without trailing space) */
1750 g_string_append_len(folded,
1751 tmp_line->str, tmp_line->len - 1);
1752 g_string_append_c(folded, '\n');
1753
1754 /* Indent new line */
1755 for (i = 0; i < indent; i++) {
1756 g_string_append_c(folded, ' ');
1757 }
1758
1759 g_string_assign(tmp_line, "");
1760 }
1761
1762 /* Append current word and space to temporary line */
1763 g_string_append(tmp_line, *word);
1764 g_string_append_c(tmp_line, ' ');
1765 }
1766
1767 /* Append last line if any, without trailing space */
1768 if (tmp_line->len > 0) {
1769 g_string_append_len(folded, tmp_line->str,
1770 tmp_line->len - 1);
1771 }
1772
1773 /* Append source newline */
1774 g_string_append_c(folded, '\n');
1775
1776 /* Free array of this line's words */
1777 g_strfreev(line_words);
1778 line_words = NULL;
1779 }
1780
1781 /* Remove trailing newline if any */
1782 if (folded->str[folded->len - 1] == '\n') {
1783 g_string_truncate(folded, folded->len - 1);
1784 }
1785
1786 end:
1787 if (lines) {
1788 g_strfreev(lines);
1789 }
1790
1791 BT_ASSERT_DBG(!line_words);
1792
1793 if (tmp_line) {
1794 g_string_free(tmp_line, TRUE);
1795 }
1796
1797 return folded;
1798 }
1799
1800 #ifdef __MINGW32__
1801 BT_HIDDEN
1802 int bt_common_get_term_size(unsigned int *width, unsigned int *height)
1803 {
1804 /* Not supported on Windows yet */
1805 return -1;
1806 }
1807 #else /* __MINGW32__ */
1808 BT_HIDDEN
1809 int bt_common_get_term_size(unsigned int *width, unsigned int *height)
1810 {
1811 int ret = 0;
1812 struct winsize winsize;
1813
1814 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) < 0) {
1815 ret = -1;
1816 goto end;
1817 }
1818
1819 if (width) {
1820 *width = (unsigned int) winsize.ws_col;
1821 }
1822
1823 if (height) {
1824 *height = (unsigned int) winsize.ws_row;
1825 }
1826
1827 end:
1828 return ret;
1829 }
1830 #endif /* __MINGW32__ */
1831
1832 BT_HIDDEN
1833 int bt_common_g_string_append_printf(GString *str, const char *fmt, ...)
1834 {
1835 va_list ap;
1836 gsize len, allocated_len, available_len;
1837 int print_len;
1838
1839 /* str->len excludes \0. */
1840 len = str->len;
1841 /* Explicitly exclude \0. */
1842 allocated_len = str->allocated_len - 1;
1843 available_len = allocated_len - len;
1844
1845 str->len = allocated_len;
1846 va_start(ap, fmt);
1847 print_len = vsnprintf(str->str + len, available_len + 1, fmt, ap);
1848 va_end(ap);
1849 if (print_len < 0) {
1850 return print_len;
1851 }
1852 if (G_UNLIKELY(available_len < print_len)) {
1853 /* Resize. */
1854 g_string_set_size(str, len + print_len);
1855 va_start(ap, fmt);
1856 print_len = vsprintf(str->str + len, fmt, ap);
1857 va_end(ap);
1858 } else {
1859 str->len = len + print_len;
1860 }
1861 return print_len;
1862 }
1863
1864 BT_HIDDEN
1865 int bt_common_append_file_content_to_g_string(GString *str, FILE *fp)
1866 {
1867 const size_t chunk_size = 4096;
1868 int ret = 0;
1869 char *buf;
1870 size_t read_len;
1871 gsize orig_len = str->len;
1872
1873 BT_ASSERT(str);
1874 BT_ASSERT(fp);
1875 buf = g_malloc(chunk_size);
1876 if (!buf) {
1877 ret = -1;
1878 goto end;
1879 }
1880
1881 while (true) {
1882 if (ferror(fp)) {
1883 ret = -1;
1884 goto end;
1885 }
1886
1887 if (feof(fp)) {
1888 break;
1889 }
1890
1891 read_len = fread(buf, 1, chunk_size, fp);
1892 g_string_append_len(str, buf, read_len);
1893 }
1894
1895 end:
1896 if (ret) {
1897 /* Remove what was appended */
1898 g_string_truncate(str, orig_len);
1899 }
1900
1901 g_free(buf);
1902 return ret;
1903 }
This page took 0.069911 seconds and 4 git commands to generate.