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"
51 #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace2/plugins"
52 #define HOME_ENV_VAR "HOME"
53 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace2/plugins"
55 static const char *bt_common_color_code_reset
= "";
56 static const char *bt_common_color_code_bold
= "";
57 static const char *bt_common_color_code_fg_default
= "";
58 static const char *bt_common_color_code_fg_red
= "";
59 static const char *bt_common_color_code_fg_green
= "";
60 static const char *bt_common_color_code_fg_yellow
= "";
61 static const char *bt_common_color_code_fg_blue
= "";
62 static const char *bt_common_color_code_fg_magenta
= "";
63 static const char *bt_common_color_code_fg_cyan
= "";
64 static const char *bt_common_color_code_fg_light_gray
= "";
65 static const char *bt_common_color_code_bg_default
= "";
66 static const char *bt_common_color_code_bg_red
= "";
67 static const char *bt_common_color_code_bg_green
= "";
68 static const char *bt_common_color_code_bg_yellow
= "";
69 static const char *bt_common_color_code_bg_blue
= "";
70 static const char *bt_common_color_code_bg_magenta
= "";
71 static const char *bt_common_color_code_bg_cyan
= "";
72 static const char *bt_common_color_code_bg_light_gray
= "";
75 void __attribute__((constructor
)) bt_common_color_ctor(void)
77 if (bt_common_colors_supported()) {
78 bt_common_color_code_reset
= BT_COMMON_COLOR_RESET
;
79 bt_common_color_code_bold
= BT_COMMON_COLOR_BOLD
;
80 bt_common_color_code_fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
81 bt_common_color_code_fg_red
= BT_COMMON_COLOR_FG_RED
;
82 bt_common_color_code_fg_green
= BT_COMMON_COLOR_FG_GREEN
;
83 bt_common_color_code_fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
84 bt_common_color_code_fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
85 bt_common_color_code_fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
86 bt_common_color_code_fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
87 bt_common_color_code_fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
88 bt_common_color_code_bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
89 bt_common_color_code_bg_red
= BT_COMMON_COLOR_BG_RED
;
90 bt_common_color_code_bg_green
= BT_COMMON_COLOR_BG_GREEN
;
91 bt_common_color_code_bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
92 bt_common_color_code_bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
93 bt_common_color_code_bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
94 bt_common_color_code_bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
95 bt_common_color_code_bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
100 const char *bt_common_get_system_plugin_path(void)
102 return SYSTEM_PLUGIN_PATH
;
107 bool bt_common_is_setuid_setgid(void)
111 #else /* __MINGW32__ */
113 bool bt_common_is_setuid_setgid(void)
115 return (geteuid() != getuid() || getegid() != getgid());
117 #endif /* __MINGW32__ */
121 const char *bt_get_home_dir(int log_level
)
123 return g_get_home_dir();
125 #else /* __MINGW32__ */
127 char *bt_secure_getenv(const char *name
, int log_level
)
129 if (bt_common_is_setuid_setgid()) {
130 BT_LOGD("Disregarding environment variable for setuid/setgid binary: "
131 "name=\"%s\"", name
);
138 const char *bt_get_home_dir(int log_level
)
143 val
= bt_secure_getenv(HOME_ENV_VAR
, log_level
);
147 /* Fallback on password file. */
148 pwd
= getpwuid(getuid());
156 #endif /* __MINGW32__ */
159 char *bt_common_get_home_plugin_path(int log_level
)
162 const char *home_dir
;
165 home_dir
= bt_get_home_dir(log_level
);
170 length
= strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1;
172 if (length
>= PATH_MAX
) {
173 BT_LOGW("Home directory path is too long: "
174 "length=%zu, max-length=%u", length
, PATH_MAX
);
178 path
= malloc(PATH_MAX
);
183 strcpy(path
, home_dir
);
184 strcat(path
, HOME_PLUGIN_SUBPATH
);
191 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
196 size_t init_dirs_len
;
199 init_dirs_len
= dirs
->len
;
202 /* Nothing to append */
207 end
= paths
+ strlen(paths
);
211 const char *next_sep
;
213 next_sep
= strchr(at
, G_SEARCHPATH_SEPARATOR
);
214 if (next_sep
== at
) {
216 * Empty path: try next character (supported
217 * to conform to the typical parsing of $PATH).
221 } else if (!next_sep
) {
222 /* No more separator: use the remaining */
223 next_sep
= paths
+ strlen(paths
);
226 path
= g_string_new(NULL
);
231 g_string_append_len(path
, at
, next_sep
- at
);
233 g_ptr_array_add(dirs
, path
);
241 /* Remove the new entries in dirs */
242 while (dirs
->len
> init_dirs_len
) {
243 g_ptr_array_remove_index(dirs
, init_dirs_len
);
251 bool isarealtty(int fd
)
254 struct stat tty_stats
;
261 if (fstat(fd
, &tty_stats
) == 0) {
262 if (!S_ISCHR(tty_stats
.st_mode
)) {
263 /* Not a character device: not a TTY */
275 bool bt_common_colors_supported(void)
277 static bool supports_colors
= false;
278 static bool supports_colors_set
= false;
279 const char *term_env_var
;
280 const char *term_color_env_var
;
282 if (supports_colors_set
) {
286 supports_colors_set
= true;
289 * `BABELTRACE_TERM_COLOR` environment variable always overrides
290 * the automatic color support detection.
292 term_color_env_var
= getenv("BABELTRACE_TERM_COLOR");
293 if (term_color_env_var
) {
294 if (g_ascii_strcasecmp(term_color_env_var
, "always") == 0) {
296 supports_colors
= true;
297 } else if (g_ascii_strcasecmp(term_color_env_var
, "never") == 0) {
298 /* Force no colors */
303 /* We need a compatible, known terminal */
304 term_env_var
= getenv("TERM");
309 if (strncmp(term_env_var
, "xterm", 5) != 0 &&
310 strncmp(term_env_var
, "rxvt", 4) != 0 &&
311 strncmp(term_env_var
, "konsole", 7) != 0 &&
312 strncmp(term_env_var
, "gnome", 5) != 0 &&
313 strncmp(term_env_var
, "screen", 5) != 0 &&
314 strncmp(term_env_var
, "tmux", 4) != 0 &&
315 strncmp(term_env_var
, "putty", 5) != 0) {
319 /* Both standard output and error streams need to be TTYs */
320 if (!isarealtty(STDOUT_FILENO
) || !isarealtty(STDERR_FILENO
)) {
324 supports_colors
= true;
327 return supports_colors
;
331 const char *bt_common_color_reset(void)
333 return bt_common_color_code_reset
;
337 const char *bt_common_color_bold(void)
339 return bt_common_color_code_bold
;
343 const char *bt_common_color_fg_default(void)
345 return bt_common_color_code_fg_default
;
349 const char *bt_common_color_fg_red(void)
351 return bt_common_color_code_fg_red
;
355 const char *bt_common_color_fg_green(void)
357 return bt_common_color_code_fg_green
;
361 const char *bt_common_color_fg_yellow(void)
363 return bt_common_color_code_fg_yellow
;
367 const char *bt_common_color_fg_blue(void)
369 return bt_common_color_code_fg_blue
;
373 const char *bt_common_color_fg_magenta(void)
375 return bt_common_color_code_fg_magenta
;
379 const char *bt_common_color_fg_cyan(void)
381 return bt_common_color_code_fg_cyan
;
385 const char *bt_common_color_fg_light_gray(void)
387 return bt_common_color_code_fg_light_gray
;
391 const char *bt_common_color_bg_default(void)
393 return bt_common_color_code_bg_default
;
397 const char *bt_common_color_bg_red(void)
399 return bt_common_color_code_bg_red
;
403 const char *bt_common_color_bg_green(void)
405 return bt_common_color_code_bg_green
;
409 const char *bt_common_color_bg_yellow(void)
411 return bt_common_color_code_bg_yellow
;
415 const char *bt_common_color_bg_blue(void)
417 return bt_common_color_code_bg_blue
;
421 const char *bt_common_color_bg_magenta(void)
423 return bt_common_color_code_bg_magenta
;
427 const char *bt_common_color_bg_cyan(void)
429 return bt_common_color_code_bg_cyan
;
433 const char *bt_common_color_bg_light_gray(void)
435 return bt_common_color_code_bg_light_gray
;
439 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
440 const char *end_chars
, size_t *end_pos
)
442 GString
*output
= g_string_new(NULL
);
445 const char *end_char
;
451 for (ch
= input
; *ch
!= '\0'; ch
++) {
453 bool continue_loop
= false;
456 /* `\` at the end of the string: append `\` */
457 g_string_append_c(output
, *ch
);
462 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
463 if (ch
[1] == *es_char
) {
465 * `\` followed by an escapable
466 * character: append the escaped
469 g_string_append_c(output
, ch
[1]);
471 continue_loop
= true;
481 * `\` followed by a non-escapable character:
482 * append `\` and the character.
484 g_string_append_c(output
, *ch
);
485 g_string_append_c(output
, ch
[1]);
489 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
490 if (*ch
== *end_char
) {
492 * End character found:
493 * terminate this loop.
499 /* Normal character: append */
500 g_string_append_c(output
, *ch
);
506 *end_pos
= ch
- input
;
513 g_string_free(output
, TRUE
);
521 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
523 GString
*output
= g_string_new(NULL
);
525 bool no_quote
= true;
531 if (strlen(input
) == 0) {
532 if (with_single_quotes
) {
533 g_string_assign(output
, "''");
539 for (ch
= input
; *ch
!= '\0'; ch
++) {
542 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
543 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
544 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
552 g_string_assign(output
, input
);
556 if (with_single_quotes
) {
557 g_string_assign(output
, "'");
560 for (ch
= input
; *ch
!= '\0'; ch
++) {
562 g_string_append(output
, "'\"'\"'");
564 g_string_append_c(output
, *ch
);
568 if (with_single_quotes
) {
569 g_string_append_c(output
, '\'');
577 bool bt_common_string_is_printable(const char *input
)
580 bool printable
= true;
583 for (ch
= input
; *ch
!= '\0'; ch
++) {
584 if (!isprint(*ch
) && *ch
!= '\n' && *ch
!= '\r' &&
585 *ch
!= '\t' && *ch
!= '\v') {
596 void bt_common_destroy_lttng_live_url_parts(
597 struct bt_common_lttng_live_url_parts
*parts
)
604 g_string_free(parts
->proto
, TRUE
);
608 if (parts
->hostname
) {
609 g_string_free(parts
->hostname
, TRUE
);
610 parts
->hostname
= NULL
;
613 if (parts
->target_hostname
) {
614 g_string_free(parts
->target_hostname
, TRUE
);
615 parts
->target_hostname
= NULL
;
618 if (parts
->session_name
) {
619 g_string_free(parts
->session_name
, TRUE
);
620 parts
->session_name
= NULL
;
628 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
629 const char *url
, char *error_buf
, size_t error_buf_size
)
631 struct bt_common_lttng_live_url_parts parts
;
632 const char *at
= url
;
636 memset(&parts
, 0, sizeof(parts
));
640 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
641 if (!parts
.proto
|| parts
.proto
->len
== 0) {
643 snprintf(error_buf
, error_buf_size
, "Missing protocol");
649 if (strcmp(parts
.proto
->str
, "net") == 0) {
650 g_string_assign(parts
.proto
, "net4");
653 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
654 strcmp(parts
.proto
->str
, "net6") != 0) {
656 snprintf(error_buf
, error_buf_size
,
657 "Unknown protocol: `%s`", parts
.proto
->str
);
663 if (at
[end_pos
] != ':') {
665 snprintf(error_buf
, error_buf_size
,
666 "Expecting `:` after `%s`", parts
.proto
->str
);
675 if (strncmp(at
, "://", 3) != 0) {
677 snprintf(error_buf
, error_buf_size
,
678 "Expecting `://` after protocol");
687 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
688 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
690 snprintf(error_buf
, error_buf_size
, "Missing hostname");
696 if (at
[end_pos
] == ':') {
701 port
= bt_common_string_until(at
, "", "/", &end_pos
);
702 if (!port
|| port
->len
== 0) {
704 snprintf(error_buf
, error_buf_size
, "Missing port");
710 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
712 snprintf(error_buf
, error_buf_size
,
713 "Invalid port: `%s`", port
->str
);
716 g_string_free(port
, TRUE
);
720 g_string_free(port
, TRUE
);
722 if (parts
.port
< 0 || parts
.port
>= 65536) {
724 snprintf(error_buf
, error_buf_size
,
725 "Invalid port: %d", parts
.port
);
732 if (at
[end_pos
] == '\0') {
739 if (strncmp(at
, "/host/", 6) != 0) {
741 snprintf(error_buf
, error_buf_size
,
742 "Expecting `/host/` after hostname or port");
750 /* Target hostname */
751 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
752 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
754 snprintf(error_buf
, error_buf_size
,
755 "Missing target hostname");
761 if (at
[end_pos
] == '\0') {
768 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
769 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
771 snprintf(error_buf
, error_buf_size
,
772 "Missing session name");
778 if (at
[end_pos
] == '/') {
780 snprintf(error_buf
, error_buf_size
,
781 "Unexpected `/` after session name (`%s`)",
782 parts
.session_name
->str
);
791 bt_common_destroy_lttng_live_url_parts(&parts
);
798 void bt_common_normalize_star_glob_pattern(char *pattern
)
802 bool got_star
= false;
806 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
810 /* Avoid consecutive stars. */
817 /* Copy backslash character. */
832 /* Copy single character. */
842 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
844 return (p
- pattern
) == pattern_len
|| *p
== '\0';
848 * Globbing matching function with the star feature only (`?` and
849 * character sets are not supported). This matches `candidate` (plain
850 * string) against `pattern`. A literal star can be escaped with `\` in
853 * `pattern_len` or `candidate_len` can be greater than the actual
854 * string length of `pattern` or `candidate` if the string is
858 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
859 const char *candidate
, size_t candidate_len
) {
860 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
861 bool got_a_star
= false;
868 * The concept here is to retry a match in the specific case
869 * where we already got a star. The retry position for the
870 * pattern is just after the most recent star, and the retry
871 * position for the candidate is the character following the
872 * last try's first character.
876 * candidate: hi ev every onyx one
878 * pattern: hi*every*one
881 * candidate: hi ev every onyx one
883 * pattern: hi*every*one
886 * candidate: hi ev every onyx one
888 * pattern: hi*every*one
891 * candidate: hi ev every onyx one
893 * pattern: hi*every*one
896 * candidate: hi ev every onyx one
898 * pattern: hi*every*one
901 * candidate: hi ev every onyx one
903 * pattern: hi*every*one
906 * candidate: hi ev every onyx one
908 * pattern: hi*every*one
911 * candidate: hi ev every onyx one
913 * pattern: hi*every*one
916 * candidate: hi ev every onyx one
918 * pattern: hi*every*one
921 * candidate: hi ev every onyx one
923 * pattern: hi*every*one
926 * candidate: hi ev every onyx one
928 * pattern: hi*every*one
931 * candidate: hi ev every onyx one
933 * pattern: hi*every*one
936 * candidate: hi ev every onyx one
938 * pattern: hi*every*one
941 * candidate: hi ev every onyx one
943 * pattern: hi*every*one
946 * candidate: hi ev every onyx one
948 * pattern: hi*every*one
951 * candidate: hi ev every onyx one
953 * pattern: hi*every*one
956 * candidate: hi ev every onyx one
958 * pattern: hi*every*one
961 * candidate: hi ev every onyx one
963 * pattern: hi*every*one
966 * candidate: hi ev every onyx one
968 * pattern: hi*every*one
971 * candidate: hi ev every onyx one
973 * pattern: hi*every*one
976 * candidate: hi ev every onyx one
978 * pattern: hi*every*one
981 * candidate: hi ev every onyx one
983 * pattern: hi*every*one
986 * candidate: hi ev every onyx one
988 * pattern: hi*every*one
991 * candidate: hi ev every onyx one
993 * pattern: hi*every*one
996 * candidate: hi ev every onyx one
998 * pattern: hi*every*one
1001 * candidate: hi ev every onyx one
1003 * pattern: hi*every*one
1006 * candidate: hi ev every onyx one
1008 * pattern: hi*every*one
1011 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
1014 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1015 goto end_of_pattern
;
1023 * Our first try starts at the current candidate
1024 * character and after the star in the pattern.
1029 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
1031 * Star at the end of the pattern at
1032 * this point: automatic match.
1039 /* Go to escaped character. */
1043 * Fall through the default case which compares
1044 * the escaped character now.
1048 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
1051 /* Character mismatch OR end of pattern. */
1054 * We didn't get any star yet,
1055 * so this first mismatch
1056 * automatically makes the whole
1063 * Next try: next candidate character,
1064 * original pattern character (following
1065 * the most recent star).
1073 /* Next pattern and candidate characters. */
1079 * We checked every candidate character and we're still in a
1080 * success state: the only pattern character allowed to remain
1083 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1088 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1093 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1096 GString
*norm_path
= NULL
;
1100 tmp
= _fullpath(NULL
, path
, PATH_MAX
);
1105 norm_path
= g_string_new(tmp
);
1113 g_string_free(norm_path
, TRUE
);
1124 void append_path_parts(const char *path
, GPtrArray
*parts
)
1126 const char *ch
= path
;
1127 const char *last
= path
;
1130 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1131 if (ch
- last
> 0) {
1132 GString
*part
= g_string_new(NULL
);
1135 g_string_append_len(part
, last
, ch
- last
);
1136 g_ptr_array_add(parts
, part
);
1151 void destroy_gstring(void *gstring
)
1153 (void) g_string_free(gstring
, TRUE
);
1157 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1161 GPtrArray
*parts
= NULL
;
1164 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1169 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1174 if (path
[0] != G_DIR_SEPARATOR
) {
1175 /* Relative path: start with working directory */
1177 append_path_parts(wd
, parts
);
1179 gchar
*cd
= g_get_current_dir();
1181 append_path_parts(cd
, parts
);
1186 /* Append parts of the path parameter */
1187 append_path_parts(path
, parts
);
1189 /* Resolve special `..` and `.` parts */
1190 for (i
= 0; i
< parts
->len
; i
++) {
1191 GString
*part
= g_ptr_array_index(parts
, i
);
1193 if (strcmp(part
->str
, "..") == 0) {
1196 * First part of absolute path is `..`:
1202 /* Remove `..` and previous part */
1203 g_ptr_array_remove_index(parts
, i
- 1);
1204 g_ptr_array_remove_index(parts
, i
- 1);
1206 } else if (strcmp(part
->str
, ".") == 0) {
1208 g_ptr_array_remove_index(parts
, i
);
1213 /* Create normalized path with what's left */
1214 for (i
= 0; i
< parts
->len
; i
++) {
1215 GString
*part
= g_ptr_array_index(parts
, i
);
1217 g_string_append(norm_path
, part
->str
);
1219 if (i
< parts
->len
- 1) {
1220 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1228 g_string_free(norm_path
, TRUE
);
1234 g_ptr_array_free(parts
, TRUE
);
1242 size_t bt_common_get_page_size(int log_level
)
1246 page_size
= bt_sysconf(_SC_PAGESIZE
);
1247 if (page_size
< 0) {
1248 BT_LOGF("Cannot get system's page size: ret=%d",
1256 #define BUF_STD_APPEND(...) \
1258 char _tmp_fmt[64]; \
1260 size_t _size = buf_size - (size_t) (*buf_ch - buf); \
1261 size_t _tmp_fmt_size = (size_t) (fmt_ch - *out_fmt_ch); \
1262 strncpy(_tmp_fmt, *out_fmt_ch, _tmp_fmt_size); \
1263 _tmp_fmt[_tmp_fmt_size] = '\0'; \
1264 _count = snprintf(*buf_ch, _size, _tmp_fmt, __VA_ARGS__); \
1265 BT_ASSERT(_count >= 0); \
1266 *buf_ch += MIN(_count, _size); \
1269 #define BUF_STD_APPEND_SINGLE_ARG(_type) \
1271 _type _arg = va_arg(*args, _type); \
1272 BUF_STD_APPEND(_arg); \
1275 static inline void handle_conversion_specifier_std(char *buf
, char **buf_ch
,
1276 size_t buf_size
, const char **out_fmt_ch
, va_list *args
)
1278 const char *fmt_ch
= *out_fmt_ch
;
1279 enum LENGTH_MODIFIER
{
1287 } length_mod
= LENGTH_MOD_NONE
;
1292 if (*fmt_ch
== '%') {
1318 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1326 if (*fmt_ch
== '.') {
1330 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1338 /* format (PRI*64) */
1339 if (strncmp(fmt_ch
, PRId64
, sizeof(PRId64
) - 1) == 0) {
1340 fmt_ch
+= sizeof(PRId64
) - 1;
1341 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1343 } else if (strncmp(fmt_ch
, PRIu64
, sizeof(PRIu64
) - 1) == 0) {
1344 fmt_ch
+= sizeof(PRIu64
) - 1;
1345 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1347 } else if (strncmp(fmt_ch
, PRIx64
, sizeof(PRIx64
) - 1) == 0) {
1348 fmt_ch
+= sizeof(PRIx64
) - 1;
1349 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1351 } else if (strncmp(fmt_ch
, PRIX64
, sizeof(PRIX64
) - 1) == 0) {
1352 fmt_ch
+= sizeof(PRIX64
) - 1;
1353 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1355 } else if (strncmp(fmt_ch
, PRIo64
, sizeof(PRIo64
) - 1) == 0) {
1356 fmt_ch
+= sizeof(PRIo64
) - 1;
1357 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1359 } else if (strncmp(fmt_ch
, PRIi64
, sizeof(PRIi64
) - 1) == 0) {
1360 fmt_ch
+= sizeof(PRIi64
) - 1;
1361 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1368 length_mod
= LENGTH_MOD_H
;
1371 if (*fmt_ch
== 'h') {
1372 length_mod
= LENGTH_MOD_HH
;
1378 length_mod
= LENGTH_MOD_LOW_L
;
1381 if (*fmt_ch
== 'l') {
1382 length_mod
= LENGTH_MOD_LOW_LL
;
1388 length_mod
= LENGTH_MOD_UP_L
;
1392 length_mod
= LENGTH_MOD_Z
;
1405 switch (length_mod
) {
1406 case LENGTH_MOD_NONE
:
1407 BUF_STD_APPEND_SINGLE_ARG(int);
1409 case LENGTH_MOD_LOW_L
:
1410 BUF_STD_APPEND_SINGLE_ARG(wint_t);
1420 switch (length_mod
) {
1421 case LENGTH_MOD_NONE
:
1422 BUF_STD_APPEND_SINGLE_ARG(char *);
1424 case LENGTH_MOD_LOW_L
:
1425 BUF_STD_APPEND_SINGLE_ARG(wchar_t *);
1435 switch (length_mod
) {
1436 case LENGTH_MOD_NONE
:
1439 BUF_STD_APPEND_SINGLE_ARG(int);
1441 case LENGTH_MOD_LOW_L
:
1442 BUF_STD_APPEND_SINGLE_ARG(long);
1444 case LENGTH_MOD_LOW_LL
:
1445 BUF_STD_APPEND_SINGLE_ARG(long long);
1448 BUF_STD_APPEND_SINGLE_ARG(size_t);
1460 switch (length_mod
) {
1461 case LENGTH_MOD_NONE
:
1464 BUF_STD_APPEND_SINGLE_ARG(unsigned int);
1466 case LENGTH_MOD_LOW_L
:
1467 BUF_STD_APPEND_SINGLE_ARG(unsigned long);
1469 case LENGTH_MOD_LOW_LL
:
1470 BUF_STD_APPEND_SINGLE_ARG(unsigned long long);
1473 BUF_STD_APPEND_SINGLE_ARG(size_t);
1487 switch (length_mod
) {
1488 case LENGTH_MOD_NONE
:
1489 BUF_STD_APPEND_SINGLE_ARG(double);
1491 case LENGTH_MOD_UP_L
:
1492 BUF_STD_APPEND_SINGLE_ARG(long double);
1501 if (length_mod
== LENGTH_MOD_NONE
) {
1502 BUF_STD_APPEND_SINGLE_ARG(void *);
1512 *out_fmt_ch
= fmt_ch
;
1516 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
1518 bt_common_handle_custom_specifier_func handle_specifier
,
1519 void *priv_data
, const char *fmt
, va_list *args
)
1521 const char *fmt_ch
= fmt
;
1527 while (*fmt_ch
!= '\0') {
1530 BT_ASSERT(fmt_ch
[1] != '\0');
1532 if (fmt_ch
[1] == intro
) {
1533 handle_specifier(priv_data
, &buf_ch
,
1534 buf_size
- (size_t) (buf_ch
- buf
),
1537 handle_conversion_specifier_std(buf
, &buf_ch
,
1538 buf_size
, &fmt_ch
, args
);
1541 if (buf_ch
>= buf
+ buf_size
- 1) {
1548 if (buf_ch
>= buf
+ buf_size
- 1) {
1560 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
1562 bt_common_handle_custom_specifier_func handle_specifier
,
1563 void *priv_data
, const char *fmt
, ...)
1566 va_start(args
, fmt
);
1567 bt_common_custom_vsnprintf(buf
, buf_size
, intro
, handle_specifier
,
1568 priv_data
, fmt
, &args
);
1573 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
)
1582 BT_ASSERT(digits_per_group
> 0);
1583 BT_ASSERT(sep
!= '\0');
1585 /* Compute new length of `str` */
1586 orig_len
= strlen(str
);
1587 BT_ASSERT(orig_len
> 0);
1588 sep_count
= (orig_len
- 1) / digits_per_group
;
1589 new_len
= strlen(str
) + sep_count
;
1592 * Do the work in place. Have the reading pointer `rd` start at
1593 * the end of the original string, and the writing pointer `wr`
1594 * start at the end of the new string, making sure to also put a
1595 * null character there.
1597 rd
= str
+ orig_len
- 1;
1603 * Here's what the process looks like (3 digits per group):
1607 * Destination: 12345678#8
1612 * Destination: 1234567878
1617 * Destination: 1234567678
1622 * Destination: 123456,678
1627 * Destination: 123455,678
1632 * Destination: 123445,678
1637 * Destination: 123345,678
1642 * Destination: 12,345,678
1647 * Destination: 12,345,678
1652 * Destination: 12,345,678
1655 while (rd
!= str
- 1) {
1656 if (i
== digits_per_group
) {
1658 * Time to append the separator: decrement `wr`,
1659 * but keep `rd` as is.
1667 /* Copy read-side character to write-side character */