2 * Babeltrace common functions
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #define BT_LOG_OUTPUT_LEVEL log_level
26 #define BT_LOG_TAG "COMMON"
27 #include "logging/log.h"
32 #include <sys/types.h>
35 #include "common/assert.h"
43 #include "common/macros.h"
44 #include "common/common.h"
45 #include "compat/unistd.h"
49 #include <sys/ioctl.h>
52 #define SYSTEM_PLUGIN_PATH BABELTRACE_PLUGINS_DIR
53 #define HOME_ENV_VAR "HOME"
54 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace2/plugins"
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_fg_bright_red
= "";
67 static const char *bt_common_color_code_fg_bright_green
= "";
68 static const char *bt_common_color_code_fg_bright_yellow
= "";
69 static const char *bt_common_color_code_fg_bright_blue
= "";
70 static const char *bt_common_color_code_fg_bright_magenta
= "";
71 static const char *bt_common_color_code_fg_bright_cyan
= "";
72 static const char *bt_common_color_code_fg_bright_light_gray
= "";
73 static const char *bt_common_color_code_bg_default
= "";
74 static const char *bt_common_color_code_bg_red
= "";
75 static const char *bt_common_color_code_bg_green
= "";
76 static const char *bt_common_color_code_bg_yellow
= "";
77 static const char *bt_common_color_code_bg_blue
= "";
78 static const char *bt_common_color_code_bg_magenta
= "";
79 static const char *bt_common_color_code_bg_cyan
= "";
80 static const char *bt_common_color_code_bg_light_gray
= "";
83 void __attribute__((constructor
)) bt_common_color_ctor(void)
85 if (bt_common_colors_supported()) {
86 const char *term_env_var
;
87 const char *bright_means_bold_env_var
;
88 bool bright_means_bold
= true;
90 bt_common_color_code_reset
= BT_COMMON_COLOR_RESET
;
91 bt_common_color_code_bold
= BT_COMMON_COLOR_BOLD
;
92 bt_common_color_code_fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
93 bt_common_color_code_fg_red
= BT_COMMON_COLOR_FG_RED
;
94 bt_common_color_code_fg_green
= BT_COMMON_COLOR_FG_GREEN
;
95 bt_common_color_code_fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
96 bt_common_color_code_fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
97 bt_common_color_code_fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
98 bt_common_color_code_fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
99 bt_common_color_code_fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
102 * Check whether or not the terminal supports having
103 * bold foreground colors which do _not_ become bright
104 * colors, that is, the lines
106 * $ echo -e "\033[31mTHIS\n\033[1mTHAT\033[0m"
108 * have the _same_ color, but `THAT` uses a bold font.
110 * This is the case of the kitty terminal emulator.
112 * It's also possible with GNOME Terminal since 3.27.2
113 * and xfce4-terminal since 0.8.7 (and GNOME VTE since
114 * 0.51.2), but it's user-configurable. Since we don't
115 * have this configuration value here, assume it's not
116 * the case to support old versions of GNOME Terminal.
118 * Any user can set the
119 * `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` environment
120 * variable to `0` to use the bright foreground color
121 * codes instead of making the normal foreground color
127 * `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` is `0`:
128 * Output bright colors using dedicated SGR codes
132 * Output bright colors with bold + SGR codes 30 to
135 term_env_var
= getenv("TERM");
136 BT_ASSERT(term_env_var
);
138 if (strcmp(term_env_var
, "xterm-kitty") == 0) {
140 * The kitty terminal emulator supports
141 * non-bright bold foreground colors.
143 bright_means_bold
= false;
146 bright_means_bold_env_var
=
147 getenv("BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD");
149 if (bright_means_bold_env_var
) {
151 !(strcmp(bright_means_bold_env_var
, "0") == 0);
154 if (bright_means_bold
) {
155 bt_common_color_code_fg_bright_red
= BT_COMMON_COLOR_FG_BOLD_RED
;
156 bt_common_color_code_fg_bright_green
= BT_COMMON_COLOR_FG_BOLD_GREEN
;
157 bt_common_color_code_fg_bright_yellow
= BT_COMMON_COLOR_FG_BOLD_YELLOW
;
158 bt_common_color_code_fg_bright_blue
= BT_COMMON_COLOR_FG_BOLD_BLUE
;
159 bt_common_color_code_fg_bright_magenta
= BT_COMMON_COLOR_FG_BOLD_MAGENTA
;
160 bt_common_color_code_fg_bright_cyan
= BT_COMMON_COLOR_FG_BOLD_CYAN
;
161 bt_common_color_code_fg_bright_light_gray
= BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY
;
163 bt_common_color_code_fg_bright_red
= BT_COMMON_COLOR_FG_BRIGHT_RED
;
164 bt_common_color_code_fg_bright_green
= BT_COMMON_COLOR_FG_BRIGHT_GREEN
;
165 bt_common_color_code_fg_bright_yellow
= BT_COMMON_COLOR_FG_BRIGHT_YELLOW
;
166 bt_common_color_code_fg_bright_blue
= BT_COMMON_COLOR_FG_BRIGHT_BLUE
;
167 bt_common_color_code_fg_bright_magenta
= BT_COMMON_COLOR_FG_BRIGHT_MAGENTA
;
168 bt_common_color_code_fg_bright_cyan
= BT_COMMON_COLOR_FG_BRIGHT_CYAN
;
169 bt_common_color_code_fg_bright_light_gray
= BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY
;
172 bt_common_color_code_bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
173 bt_common_color_code_bg_red
= BT_COMMON_COLOR_BG_RED
;
174 bt_common_color_code_bg_green
= BT_COMMON_COLOR_BG_GREEN
;
175 bt_common_color_code_bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
176 bt_common_color_code_bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
177 bt_common_color_code_bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
178 bt_common_color_code_bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
179 bt_common_color_code_bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
184 const char *bt_common_get_system_plugin_path(void)
186 return SYSTEM_PLUGIN_PATH
;
191 bool bt_common_is_setuid_setgid(void)
195 #else /* __MINGW32__ */
197 bool bt_common_is_setuid_setgid(void)
199 return (geteuid() != getuid() || getegid() != getgid());
201 #endif /* __MINGW32__ */
205 const char *bt_get_home_dir(int log_level
)
207 return g_get_home_dir();
209 #else /* __MINGW32__ */
211 char *bt_secure_getenv(const char *name
, int log_level
)
213 if (bt_common_is_setuid_setgid()) {
214 BT_LOGD("Disregarding environment variable for setuid/setgid binary: "
215 "name=\"%s\"", name
);
222 const char *bt_get_home_dir(int log_level
)
227 val
= bt_secure_getenv(HOME_ENV_VAR
, log_level
);
231 /* Fallback on password file. */
232 pwd
= getpwuid(getuid());
240 #endif /* __MINGW32__ */
243 char *bt_common_get_home_plugin_path(int log_level
)
246 const char *home_dir
;
249 home_dir
= bt_get_home_dir(log_level
);
254 length
= strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1;
256 if (length
>= PATH_MAX
) {
257 BT_LOGW("Home directory path is too long: "
258 "length=%zu, max-length=%u", length
, PATH_MAX
);
262 path
= malloc(PATH_MAX
);
267 strcpy(path
, home_dir
);
268 strcat(path
, HOME_PLUGIN_SUBPATH
);
275 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
280 size_t init_dirs_len
;
283 init_dirs_len
= dirs
->len
;
286 /* Nothing to append */
291 end
= paths
+ strlen(paths
);
295 const char *next_sep
;
297 next_sep
= strchr(at
, G_SEARCHPATH_SEPARATOR
);
298 if (next_sep
== at
) {
300 * Empty path: try next character (supported
301 * to conform to the typical parsing of $PATH).
305 } else if (!next_sep
) {
306 /* No more separator: use the remaining */
307 next_sep
= paths
+ strlen(paths
);
310 path
= g_string_new(NULL
);
315 g_string_append_len(path
, at
, next_sep
- at
);
317 g_ptr_array_add(dirs
, path
);
325 /* Remove the new entries in dirs */
326 while (dirs
->len
> init_dirs_len
) {
327 g_ptr_array_remove_index(dirs
, init_dirs_len
);
335 bool isarealtty(int fd
)
338 struct stat tty_stats
;
345 if (fstat(fd
, &tty_stats
) == 0) {
346 if (!S_ISCHR(tty_stats
.st_mode
)) {
347 /* Not a character device: not a TTY */
359 bool bt_common_colors_supported(void)
361 static bool supports_colors
= false;
362 static bool supports_colors_set
= false;
363 const char *term_env_var
;
364 const char *term_color_env_var
;
366 if (supports_colors_set
) {
370 supports_colors_set
= true;
373 * `BABELTRACE_TERM_COLOR` environment variable always overrides
374 * the automatic color support detection.
376 term_color_env_var
= getenv("BABELTRACE_TERM_COLOR");
377 if (term_color_env_var
) {
378 if (g_ascii_strcasecmp(term_color_env_var
, "always") == 0) {
380 supports_colors
= true;
381 } else if (g_ascii_strcasecmp(term_color_env_var
, "never") == 0) {
382 /* Force no colors */
387 /* We need a compatible, known terminal */
388 term_env_var
= getenv("TERM");
393 if (strncmp(term_env_var
, "xterm", 5) != 0 &&
394 strncmp(term_env_var
, "rxvt", 4) != 0 &&
395 strncmp(term_env_var
, "konsole", 7) != 0 &&
396 strncmp(term_env_var
, "gnome", 5) != 0 &&
397 strncmp(term_env_var
, "screen", 5) != 0 &&
398 strncmp(term_env_var
, "tmux", 4) != 0 &&
399 strncmp(term_env_var
, "putty", 5) != 0) {
403 /* Both standard output and error streams need to be TTYs */
404 if (!isarealtty(STDOUT_FILENO
) || !isarealtty(STDERR_FILENO
)) {
408 supports_colors
= true;
411 return supports_colors
;
415 const char *bt_common_color_reset(void)
417 return bt_common_color_code_reset
;
421 const char *bt_common_color_bold(void)
423 return bt_common_color_code_bold
;
427 const char *bt_common_color_fg_default(void)
429 return bt_common_color_code_fg_default
;
433 const char *bt_common_color_fg_red(void)
435 return bt_common_color_code_fg_red
;
439 const char *bt_common_color_fg_green(void)
441 return bt_common_color_code_fg_green
;
445 const char *bt_common_color_fg_yellow(void)
447 return bt_common_color_code_fg_yellow
;
451 const char *bt_common_color_fg_blue(void)
453 return bt_common_color_code_fg_blue
;
457 const char *bt_common_color_fg_magenta(void)
459 return bt_common_color_code_fg_magenta
;
463 const char *bt_common_color_fg_cyan(void)
465 return bt_common_color_code_fg_cyan
;
469 const char *bt_common_color_fg_light_gray(void)
471 return bt_common_color_code_fg_light_gray
;
475 const char *bt_common_color_fg_bright_red(void)
477 return bt_common_color_code_fg_bright_red
;
481 const char *bt_common_color_fg_bright_green(void)
483 return bt_common_color_code_fg_bright_green
;
487 const char *bt_common_color_fg_bright_yellow(void)
489 return bt_common_color_code_fg_bright_yellow
;
493 const char *bt_common_color_fg_bright_blue(void)
495 return bt_common_color_code_fg_bright_blue
;
499 const char *bt_common_color_fg_bright_magenta(void)
501 return bt_common_color_code_fg_bright_magenta
;
505 const char *bt_common_color_fg_bright_cyan(void)
507 return bt_common_color_code_fg_bright_cyan
;
511 const char *bt_common_color_fg_bright_light_gray(void)
513 return bt_common_color_code_fg_bright_light_gray
;
517 const char *bt_common_color_bg_default(void)
519 return bt_common_color_code_bg_default
;
523 const char *bt_common_color_bg_red(void)
525 return bt_common_color_code_bg_red
;
529 const char *bt_common_color_bg_green(void)
531 return bt_common_color_code_bg_green
;
535 const char *bt_common_color_bg_yellow(void)
537 return bt_common_color_code_bg_yellow
;
541 const char *bt_common_color_bg_blue(void)
543 return bt_common_color_code_bg_blue
;
547 const char *bt_common_color_bg_magenta(void)
549 return bt_common_color_code_bg_magenta
;
553 const char *bt_common_color_bg_cyan(void)
555 return bt_common_color_code_bg_cyan
;
559 const char *bt_common_color_bg_light_gray(void)
561 return bt_common_color_code_bg_light_gray
;
565 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
566 const char *end_chars
, size_t *end_pos
)
568 GString
*output
= g_string_new(NULL
);
571 const char *end_char
;
577 for (ch
= input
; *ch
!= '\0'; ch
++) {
579 bool continue_loop
= false;
582 /* `\` at the end of the string: append `\` */
583 g_string_append_c(output
, *ch
);
588 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
589 if (ch
[1] == *es_char
) {
591 * `\` followed by an escapable
592 * character: append the escaped
595 g_string_append_c(output
, ch
[1]);
597 continue_loop
= true;
607 * `\` followed by a non-escapable character:
608 * append `\` and the character.
610 g_string_append_c(output
, *ch
);
611 g_string_append_c(output
, ch
[1]);
615 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
616 if (*ch
== *end_char
) {
618 * End character found:
619 * terminate this loop.
625 /* Normal character: append */
626 g_string_append_c(output
, *ch
);
632 *end_pos
= ch
- input
;
639 g_string_free(output
, TRUE
);
648 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
650 GString
*output
= g_string_new(NULL
);
652 bool no_quote
= true;
658 if (strlen(input
) == 0) {
659 if (with_single_quotes
) {
660 g_string_assign(output
, "''");
666 for (ch
= input
; *ch
!= '\0'; ch
++) {
669 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
670 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
671 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
679 g_string_assign(output
, input
);
683 if (with_single_quotes
) {
684 g_string_assign(output
, "'");
687 for (ch
= input
; *ch
!= '\0'; ch
++) {
689 g_string_append(output
, "'\"'\"'");
691 g_string_append_c(output
, *ch
);
695 if (with_single_quotes
) {
696 g_string_append_c(output
, '\'');
704 bool bt_common_string_is_printable(const char *input
)
707 bool printable
= true;
708 BT_ASSERT_DBG(input
);
710 for (ch
= input
; *ch
!= '\0'; ch
++) {
711 if (!isprint(*ch
) && *ch
!= '\n' && *ch
!= '\r' &&
712 *ch
!= '\t' && *ch
!= '\v') {
723 void bt_common_destroy_lttng_live_url_parts(
724 struct bt_common_lttng_live_url_parts
*parts
)
731 g_string_free(parts
->proto
, TRUE
);
735 if (parts
->hostname
) {
736 g_string_free(parts
->hostname
, TRUE
);
737 parts
->hostname
= NULL
;
740 if (parts
->target_hostname
) {
741 g_string_free(parts
->target_hostname
, TRUE
);
742 parts
->target_hostname
= NULL
;
745 if (parts
->session_name
) {
746 g_string_free(parts
->session_name
, TRUE
);
747 parts
->session_name
= NULL
;
755 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
756 const char *url
, char *error_buf
, size_t error_buf_size
)
758 struct bt_common_lttng_live_url_parts parts
;
759 const char *at
= url
;
763 memset(&parts
, 0, sizeof(parts
));
767 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
768 if (!parts
.proto
|| parts
.proto
->len
== 0) {
770 snprintf(error_buf
, error_buf_size
, "Missing protocol");
776 if (strcmp(parts
.proto
->str
, "net") == 0) {
777 g_string_assign(parts
.proto
, "net4");
780 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
781 strcmp(parts
.proto
->str
, "net6") != 0) {
783 snprintf(error_buf
, error_buf_size
,
784 "Unknown protocol: `%s`", parts
.proto
->str
);
790 if (at
[end_pos
] != ':') {
792 snprintf(error_buf
, error_buf_size
,
793 "Expecting `:` after `%s`", parts
.proto
->str
);
802 if (strncmp(at
, "://", 3) != 0) {
804 snprintf(error_buf
, error_buf_size
,
805 "Expecting `://` after protocol");
815 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
816 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
818 snprintf(error_buf
, error_buf_size
, "Missing hostname");
824 if (at
[end_pos
] == ':') {
829 port
= bt_common_string_until(at
, "", "/", &end_pos
);
830 if (!port
|| port
->len
== 0) {
832 snprintf(error_buf
, error_buf_size
, "Missing port");
838 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
840 snprintf(error_buf
, error_buf_size
,
841 "Invalid port: `%s`", port
->str
);
844 g_string_free(port
, TRUE
);
848 g_string_free(port
, TRUE
);
850 if (parts
.port
< 0 || parts
.port
>= 65536) {
852 snprintf(error_buf
, error_buf_size
,
853 "Invalid port: %d", parts
.port
);
860 if (at
[end_pos
] == '\0') {
861 /* Relay daemon hostname and ports provided only */
868 if (strncmp(at
, "/host/", 6) != 0) {
870 snprintf(error_buf
, error_buf_size
,
871 "Expecting `/host/` after hostname or port");
879 /* Target hostname */
880 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
881 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
883 snprintf(error_buf
, error_buf_size
,
884 "Missing target hostname");
890 if (at
[end_pos
] == '\0') {
892 snprintf(error_buf
, error_buf_size
,
893 "Missing `/` after target hostname (`%s`)",
894 parts
.target_hostname
->str
);
904 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
905 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
907 snprintf(error_buf
, error_buf_size
,
908 "Missing session name");
914 if (at
[end_pos
] == '/') {
916 snprintf(error_buf
, error_buf_size
,
917 "Unexpected `/` after session name (`%s`)",
918 parts
.session_name
->str
);
927 bt_common_destroy_lttng_live_url_parts(&parts
);
934 void bt_common_normalize_star_glob_pattern(char *pattern
)
938 bool got_star
= false;
942 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
946 /* Avoid consecutive stars. */
953 /* Copy backslash character. */
968 /* Copy single character. */
978 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
980 return (p
- pattern
) == pattern_len
|| *p
== '\0';
984 * Globbing matching function with the star feature only (`?` and
985 * character sets are not supported). This matches `candidate` (plain
986 * string) against `pattern`. A literal star can be escaped with `\` in
989 * `pattern_len` or `candidate_len` can be greater than the actual
990 * string length of `pattern` or `candidate` if the string is
994 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
995 const char *candidate
, size_t candidate_len
) {
996 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
997 bool got_a_star
= false;
1004 * The concept here is to retry a match in the specific case
1005 * where we already got a star. The retry position for the
1006 * pattern is just after the most recent star, and the retry
1007 * position for the candidate is the character following the
1008 * last try's first character.
1012 * candidate: hi ev every onyx one
1014 * pattern: hi*every*one
1017 * candidate: hi ev every onyx one
1019 * pattern: hi*every*one
1022 * candidate: hi ev every onyx one
1024 * pattern: hi*every*one
1027 * candidate: hi ev every onyx one
1029 * pattern: hi*every*one
1032 * candidate: hi ev every onyx one
1034 * pattern: hi*every*one
1037 * candidate: hi ev every onyx one
1039 * pattern: hi*every*one
1042 * candidate: hi ev every onyx one
1044 * pattern: hi*every*one
1047 * candidate: hi ev every onyx one
1049 * pattern: hi*every*one
1052 * candidate: hi ev every onyx one
1054 * pattern: hi*every*one
1057 * candidate: hi ev every onyx one
1059 * pattern: hi*every*one
1062 * candidate: hi ev every onyx one
1064 * pattern: hi*every*one
1067 * candidate: hi ev every onyx one
1069 * pattern: hi*every*one
1072 * candidate: hi ev every onyx one
1074 * pattern: hi*every*one
1077 * candidate: hi ev every onyx one
1079 * pattern: hi*every*one
1082 * candidate: hi ev every onyx one
1084 * pattern: hi*every*one
1087 * candidate: hi ev every onyx one
1089 * pattern: hi*every*one
1092 * candidate: hi ev every onyx one
1094 * pattern: hi*every*one
1097 * candidate: hi ev every onyx one
1099 * pattern: hi*every*one
1102 * candidate: hi ev every onyx one
1104 * pattern: hi*every*one
1107 * candidate: hi ev every onyx one
1109 * pattern: hi*every*one
1112 * candidate: hi ev every onyx one
1114 * pattern: hi*every*one
1117 * candidate: hi ev every onyx one
1119 * pattern: hi*every*one
1122 * candidate: hi ev every onyx one
1124 * pattern: hi*every*one
1127 * candidate: hi ev every onyx one
1129 * pattern: hi*every*one
1132 * candidate: hi ev every onyx one
1134 * pattern: hi*every*one
1137 * candidate: hi ev every onyx one
1139 * pattern: hi*every*one
1142 * candidate: hi ev every onyx one
1144 * pattern: hi*every*one
1147 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
1150 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1151 goto end_of_pattern
;
1159 * Our first try starts at the current candidate
1160 * character and after the star in the pattern.
1165 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
1167 * Star at the end of the pattern at
1168 * this point: automatic match.
1175 /* Go to escaped character. */
1179 * Fall through the default case which compares
1180 * the escaped character now.
1184 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
1187 /* Character mismatch OR end of pattern. */
1190 * We didn't get any star yet,
1191 * so this first mismatch
1192 * automatically makes the whole
1199 * Next try: next candidate character,
1200 * original pattern character (following
1201 * the most recent star).
1209 /* Next pattern and candidate characters. */
1215 * We checked every candidate character and we're still in a
1216 * success state: the only pattern character allowed to remain
1219 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1224 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1229 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1232 GString
*norm_path
= NULL
;
1236 tmp
= _fullpath(NULL
, path
, PATH_MAX
);
1241 norm_path
= g_string_new(tmp
);
1249 g_string_free(norm_path
, TRUE
);
1258 void append_path_parts(const char *path
, GPtrArray
*parts
)
1260 const char *ch
= path
;
1261 const char *last
= path
;
1264 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1265 if (ch
- last
> 0) {
1266 GString
*part
= g_string_new(NULL
);
1269 g_string_append_len(part
, last
, ch
- last
);
1270 g_ptr_array_add(parts
, part
);
1285 void destroy_gstring(void *gstring
)
1287 (void) g_string_free(gstring
, TRUE
);
1291 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1295 GPtrArray
*parts
= NULL
;
1298 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1303 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1308 if (path
[0] != G_DIR_SEPARATOR
) {
1309 /* Relative path: start with working directory */
1311 append_path_parts(wd
, parts
);
1313 gchar
*cd
= g_get_current_dir();
1315 append_path_parts(cd
, parts
);
1320 /* Append parts of the path parameter */
1321 append_path_parts(path
, parts
);
1323 /* Resolve special `..` and `.` parts */
1324 for (i
= 0; i
< parts
->len
; i
++) {
1325 GString
*part
= g_ptr_array_index(parts
, i
);
1327 if (strcmp(part
->str
, "..") == 0) {
1330 * First part of absolute path is `..`:
1336 /* Remove `..` and previous part */
1337 g_ptr_array_remove_index(parts
, i
- 1);
1338 g_ptr_array_remove_index(parts
, i
- 1);
1340 } else if (strcmp(part
->str
, ".") == 0) {
1342 g_ptr_array_remove_index(parts
, i
);
1347 /* Create normalized path with what's left */
1348 for (i
= 0; i
< parts
->len
; i
++) {
1349 GString
*part
= g_ptr_array_index(parts
, i
);
1351 g_string_append(norm_path
, part
->str
);
1353 if (i
< parts
->len
- 1) {
1354 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1362 g_string_free(norm_path
, TRUE
);
1368 g_ptr_array_free(parts
, TRUE
);
1376 size_t bt_common_get_page_size(int log_level
)
1380 page_size
= bt_sysconf(_SC_PAGESIZE
);
1381 if (page_size
< 0) {
1382 BT_LOGF("Cannot get system's page size: ret=%d",
1390 #define BUF_STD_APPEND(...) \
1392 char _tmp_fmt[64]; \
1394 size_t _size = buf_size - (size_t) (*buf_ch - buf); \
1395 size_t _tmp_fmt_size = (size_t) (fmt_ch - *out_fmt_ch); \
1396 strncpy(_tmp_fmt, *out_fmt_ch, _tmp_fmt_size); \
1397 _tmp_fmt[_tmp_fmt_size] = '\0'; \
1398 _count = snprintf(*buf_ch, _size, _tmp_fmt, __VA_ARGS__); \
1399 BT_ASSERT_DBG(_count >= 0); \
1400 *buf_ch += MIN(_count, _size); \
1403 #define BUF_STD_APPEND_SINGLE_ARG(_type) \
1405 _type _arg = va_arg(*args, _type); \
1406 BUF_STD_APPEND(_arg); \
1409 static inline void handle_conversion_specifier_std(char *buf
, char **buf_ch
,
1410 size_t buf_size
, const char **out_fmt_ch
, va_list *args
)
1412 const char *fmt_ch
= *out_fmt_ch
;
1413 enum LENGTH_MODIFIER
{
1421 } length_mod
= LENGTH_MOD_NONE
;
1426 if (*fmt_ch
== '%') {
1452 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1460 if (*fmt_ch
== '.') {
1464 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1472 /* format (PRI*64) */
1473 if (strncmp(fmt_ch
, PRId64
, sizeof(PRId64
) - 1) == 0) {
1474 fmt_ch
+= sizeof(PRId64
) - 1;
1475 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1477 } else if (strncmp(fmt_ch
, PRIu64
, sizeof(PRIu64
) - 1) == 0) {
1478 fmt_ch
+= sizeof(PRIu64
) - 1;
1479 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1481 } else if (strncmp(fmt_ch
, PRIx64
, sizeof(PRIx64
) - 1) == 0) {
1482 fmt_ch
+= sizeof(PRIx64
) - 1;
1483 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1485 } else if (strncmp(fmt_ch
, PRIX64
, sizeof(PRIX64
) - 1) == 0) {
1486 fmt_ch
+= sizeof(PRIX64
) - 1;
1487 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1489 } else if (strncmp(fmt_ch
, PRIo64
, sizeof(PRIo64
) - 1) == 0) {
1490 fmt_ch
+= sizeof(PRIo64
) - 1;
1491 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1493 } else if (strncmp(fmt_ch
, PRIi64
, sizeof(PRIi64
) - 1) == 0) {
1494 fmt_ch
+= sizeof(PRIi64
) - 1;
1495 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1502 length_mod
= LENGTH_MOD_H
;
1505 if (*fmt_ch
== 'h') {
1506 length_mod
= LENGTH_MOD_HH
;
1512 length_mod
= LENGTH_MOD_LOW_L
;
1515 if (*fmt_ch
== 'l') {
1516 length_mod
= LENGTH_MOD_LOW_LL
;
1522 length_mod
= LENGTH_MOD_UP_L
;
1526 length_mod
= LENGTH_MOD_Z
;
1539 switch (length_mod
) {
1540 case LENGTH_MOD_NONE
:
1541 case LENGTH_MOD_LOW_L
:
1542 BUF_STD_APPEND_SINGLE_ARG(int);
1552 switch (length_mod
) {
1553 case LENGTH_MOD_NONE
:
1554 BUF_STD_APPEND_SINGLE_ARG(char *);
1556 case LENGTH_MOD_LOW_L
:
1557 BUF_STD_APPEND_SINGLE_ARG(wchar_t *);
1567 switch (length_mod
) {
1568 case LENGTH_MOD_NONE
:
1571 BUF_STD_APPEND_SINGLE_ARG(int);
1573 case LENGTH_MOD_LOW_L
:
1574 BUF_STD_APPEND_SINGLE_ARG(long);
1576 case LENGTH_MOD_LOW_LL
:
1577 BUF_STD_APPEND_SINGLE_ARG(long long);
1580 BUF_STD_APPEND_SINGLE_ARG(size_t);
1592 switch (length_mod
) {
1593 case LENGTH_MOD_NONE
:
1596 BUF_STD_APPEND_SINGLE_ARG(unsigned int);
1598 case LENGTH_MOD_LOW_L
:
1599 BUF_STD_APPEND_SINGLE_ARG(unsigned long);
1601 case LENGTH_MOD_LOW_LL
:
1602 BUF_STD_APPEND_SINGLE_ARG(unsigned long long);
1605 BUF_STD_APPEND_SINGLE_ARG(size_t);
1619 switch (length_mod
) {
1620 case LENGTH_MOD_NONE
:
1621 BUF_STD_APPEND_SINGLE_ARG(double);
1623 case LENGTH_MOD_UP_L
:
1624 BUF_STD_APPEND_SINGLE_ARG(long double);
1633 if (length_mod
== LENGTH_MOD_NONE
) {
1634 BUF_STD_APPEND_SINGLE_ARG(void *);
1644 *out_fmt_ch
= fmt_ch
;
1648 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
1650 bt_common_handle_custom_specifier_func handle_specifier
,
1651 void *priv_data
, const char *fmt
, va_list *args
)
1653 const char *fmt_ch
= fmt
;
1659 while (*fmt_ch
!= '\0') {
1662 BT_ASSERT_DBG(fmt_ch
[1] != '\0');
1664 if (fmt_ch
[1] == intro
) {
1665 handle_specifier(priv_data
, &buf_ch
,
1666 buf_size
- (size_t) (buf_ch
- buf
),
1669 handle_conversion_specifier_std(buf
, &buf_ch
,
1670 buf_size
, &fmt_ch
, args
);
1673 if (buf_ch
>= buf
+ buf_size
- 1) {
1680 if (buf_ch
>= buf
+ buf_size
- 1) {
1692 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
1694 bt_common_handle_custom_specifier_func handle_specifier
,
1695 void *priv_data
, const char *fmt
, ...)
1698 va_start(args
, fmt
);
1699 bt_common_custom_vsnprintf(buf
, buf_size
, intro
, handle_specifier
,
1700 priv_data
, fmt
, &args
);
1705 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
)
1714 BT_ASSERT_DBG(digits_per_group
> 0);
1715 BT_ASSERT_DBG(sep
!= '\0');
1717 /* Compute new length of `str` */
1718 orig_len
= strlen(str
);
1719 BT_ASSERT_DBG(orig_len
> 0);
1720 sep_count
= (orig_len
- 1) / digits_per_group
;
1721 new_len
= strlen(str
) + sep_count
;
1724 * Do the work in place. Have the reading pointer `rd` start at
1725 * the end of the original string, and the writing pointer `wr`
1726 * start at the end of the new string, making sure to also put a
1727 * null character there.
1729 rd
= str
+ orig_len
- 1;
1735 * Here's what the process looks like (3 digits per group):
1739 * Destination: 12345678#8
1744 * Destination: 1234567878
1749 * Destination: 1234567678
1754 * Destination: 123456,678
1759 * Destination: 123455,678
1764 * Destination: 123445,678
1769 * Destination: 123345,678
1774 * Destination: 12,345,678
1779 * Destination: 12,345,678
1784 * Destination: 12,345,678
1787 while (rd
!= str
- 1) {
1788 if (i
== digits_per_group
) {
1790 * Time to append the separator: decrement `wr`,
1791 * but keep `rd` as is.
1799 /* Copy read-side character to write-side character */
1808 GString
*bt_common_fold(const char *str
, unsigned int total_length
,
1809 unsigned int indent
)
1811 const unsigned int content_length
= total_length
- indent
;
1812 GString
*folded
= g_string_new(NULL
);
1813 GString
*tmp_line
= g_string_new(NULL
);
1814 gchar
**lines
= NULL
;
1815 gchar
**line_words
= NULL
;
1816 gchar
* const *line
;
1820 BT_ASSERT_DBG(indent
< total_length
);
1821 BT_ASSERT_DBG(tmp_line
);
1822 BT_ASSERT_DBG(folded
);
1824 if (strlen(str
) == 0) {
1825 /* Empty input string: empty output string */
1830 lines
= g_strsplit(str
, "\n", 0);
1831 BT_ASSERT_DBG(lines
);
1833 /* For each source line */
1834 for (line
= lines
; *line
; line
++) {
1835 gchar
* const *word
;
1838 * Append empty line without indenting if source line is
1841 if (strlen(*line
) == 0) {
1842 g_string_append_c(folded
, '\n');
1847 line_words
= g_strsplit(*line
, " ", 0);
1848 BT_ASSERT_DBG(line_words
);
1851 * Indent for first line (we know there's at least one
1852 * word at this point).
1854 for (i
= 0; i
< indent
; i
++) {
1855 g_string_append_c(folded
, ' ');
1858 /* Append words, folding when necessary */
1859 g_string_assign(tmp_line
, "");
1861 for (word
= line_words
; *word
; word
++) {
1863 * `tmp_line->len > 0` is in the condition so
1864 * that words that are larger than
1865 * `content_length` are at least written on
1868 * `tmp_line->len - 1` because the temporary
1869 * line always contains a trailing space which
1870 * won't be part of the line if we fold.
1872 if (tmp_line
->len
> 0 &&
1873 tmp_line
->len
- 1 + strlen(*word
) >= content_length
) {
1874 /* Fold (without trailing space) */
1875 g_string_append_len(folded
,
1876 tmp_line
->str
, tmp_line
->len
- 1);
1877 g_string_append_c(folded
, '\n');
1879 /* Indent new line */
1880 for (i
= 0; i
< indent
; i
++) {
1881 g_string_append_c(folded
, ' ');
1884 g_string_assign(tmp_line
, "");
1887 /* Append current word and space to temporary line */
1888 g_string_append(tmp_line
, *word
);
1889 g_string_append_c(tmp_line
, ' ');
1892 /* Append last line if any, without trailing space */
1893 if (tmp_line
->len
> 0) {
1894 g_string_append_len(folded
, tmp_line
->str
,
1898 /* Append source newline */
1899 g_string_append_c(folded
, '\n');
1901 /* Free array of this line's words */
1902 g_strfreev(line_words
);
1906 /* Remove trailing newline if any */
1907 if (folded
->str
[folded
->len
- 1] == '\n') {
1908 g_string_truncate(folded
, folded
->len
- 1);
1916 BT_ASSERT_DBG(!line_words
);
1919 g_string_free(tmp_line
, TRUE
);
1927 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
)
1929 /* Not supported on Windows yet */
1932 #else /* __MINGW32__ */
1934 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
)
1937 struct winsize winsize
;
1939 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &winsize
) < 0) {
1945 *width
= (unsigned int) winsize
.ws_col
;
1949 *height
= (unsigned int) winsize
.ws_row
;
1955 #endif /* __MINGW32__ */
1958 int bt_common_g_string_append_printf(GString
*str
, const char *fmt
, ...)
1961 gsize len
, allocated_len
, available_len
;
1964 /* str->len excludes \0. */
1966 /* Explicitly exclude \0. */
1967 allocated_len
= str
->allocated_len
- 1;
1968 available_len
= allocated_len
- len
;
1970 str
->len
= allocated_len
;
1972 print_len
= vsnprintf(str
->str
+ len
, available_len
+ 1, fmt
, ap
);
1974 if (print_len
< 0) {
1977 if (G_UNLIKELY(available_len
< print_len
)) {
1979 g_string_set_size(str
, len
+ print_len
);
1981 print_len
= vsprintf(str
->str
+ len
, fmt
, ap
);
1984 str
->len
= len
+ print_len
;
1990 int bt_common_append_file_content_to_g_string(GString
*str
, FILE *fp
)
1992 const size_t chunk_size
= 4096;
1996 gsize orig_len
= str
->len
;
2000 buf
= g_malloc(chunk_size
);
2016 read_len
= fread(buf
, 1, chunk_size
, fp
);
2017 g_string_append_len(str
, buf
, read_len
);
2022 /* Remove what was appended */
2023 g_string_truncate(str
, orig_len
);
2031 void bt_common_abort(void)
2033 static const char * const exec_on_abort_env_name
=
2034 "BABELTRACE_EXEC_ON_ABORT";
2035 const char *env_exec_on_abort
;
2037 env_exec_on_abort
= getenv(exec_on_abort_env_name
);
2038 if (env_exec_on_abort
) {
2039 if (bt_common_is_setuid_setgid()) {
2043 (void) g_spawn_command_line_sync(env_exec_on_abort
,
2044 NULL
, NULL
, NULL
, NULL
);