1 #ifndef BABELTRACE_COMMON_INTERNAL_H
2 #define BABELTRACE_COMMON_INTERNAL_H
5 #include <babeltrace/babeltrace-internal.h>
8 #define BT_COMMON_COLOR_RESET "\033[0m"
9 #define BT_COMMON_COLOR_BOLD "\033[1m"
10 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
11 #define BT_COMMON_COLOR_FG_RED "\033[31m"
12 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
13 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
14 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
15 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
16 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
17 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
18 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
19 #define BT_COMMON_COLOR_BG_RED "\033[41m"
20 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
21 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
22 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
23 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
24 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
25 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
27 struct bt_common_lttng_live_url_parts
{
30 GString
*target_hostname
;
31 GString
*session_name
;
33 /* -1 means default port */
38 * Checks if the current process has setuid or setgid access rights.
39 * Returns `true` if so.
42 bool bt_common_is_setuid_setgid(void);
45 * Returns the system-wide plugin path, e.g.
46 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
49 const char *bt_common_get_system_plugin_path(void);
52 * Returns the user plugin path, e.g.
53 * `/home/user/.local/lib/babeltrace/plugins`. You need to free the
57 char *bt_common_get_home_plugin_path(void);
60 * Appends the list of directories in `paths` to the array `dirs`.
61 * `paths` is a list of directories separated by `:`. Returns 0 on
65 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
68 * Returns `true` if terminal color codes are supported for this
72 bool bt_common_colors_supported(void);
75 const char *bt_common_color_reset(void);
78 const char *bt_common_color_bold(void);
81 const char *bt_common_color_fg_default(void);
84 const char *bt_common_color_fg_red(void);
87 const char *bt_common_color_fg_green(void);
90 const char *bt_common_color_fg_yellow(void);
93 const char *bt_common_color_fg_blue(void);
96 const char *bt_common_color_fg_magenta(void);
99 const char *bt_common_color_fg_cyan(void);
102 const char *bt_common_color_fg_light_gray(void);
105 const char *bt_common_color_bg_default(void);
108 const char *bt_common_color_bg_red(void);
111 const char *bt_common_color_bg_green(void);
114 const char *bt_common_color_bg_yellow(void);
117 const char *bt_common_color_bg_blue(void);
120 const char *bt_common_color_bg_magenta(void);
123 const char *bt_common_color_bg_cyan(void);
126 const char *bt_common_color_bg_light_gray(void);
129 * Returns the substring from `input` to the first character found
130 * in the list of characters `end_chars`, unescaping any character
131 * found in `escapable_chars`, and sets `*end_pos` to the position of
132 * the end (from `input`). The caller owns the returned GString.
135 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
136 const char *end_chars
, size_t *end_pos
);
139 * Returns the quoted version of `input` for a shell. If
140 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
141 * and suffix to the returned string; otherwise the caller should
142 * prepend and append them manually, although they are not always
143 * required. The caller owns the returned GString.
146 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
149 * Returns `true` if `input` is a string made only of printable
153 bool bt_common_string_is_printable(const char *input
);
156 * Destroys the parts of an LTTng live URL as returned by
157 * bt_common_parse_lttng_live_url().
160 void bt_common_destroy_lttng_live_url_parts(
161 struct bt_common_lttng_live_url_parts
*parts
);
164 * Parses the LTTng live URL `url` and returns its different parts.
165 * If there's an error, writes the error message into `*error_buf`
166 * up to `error_buf_size` bytes. You must destroy the returned value
167 * with bt_common_destroy_lttng_live_url_parts().
170 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
171 const char *url
, char *error_buf
, size_t error_buf_size
);
174 * Normalizes (in place) a star globbing pattern to be used with
175 * bt_common_star_glob_match(). This function always succeeds.
178 void bt_common_normalize_star_glob_pattern(char *pattern
);
181 * Returns `true` if `candidate` (of size `candidate_len`) matches
182 * the star globbing pattern `pattern` (of size `pattern_len`).
185 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
186 const char *candidate
, size_t candidate_len
);
189 * Normalizes the path `path`:
191 * * If it's a relative path, converts it to an absolute path using
192 * `wd` as the working directory (or the current working directory
194 * * Removes consecutive and trailing slashes.
195 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
196 * * Does NOT resolve symbolic links.
198 * The caller owns the returned GString.
201 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
203 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
204 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
207 * This is a custom vsnprintf() which handles the standard conversion
208 * specifier as well as custom ones.
210 * `fmt` is a typical printf()-style format string, with the following
213 * * The `*` width specifier is not accepted.
214 * * The `*` precision specifier is not accepted.
215 * * The `j` and `t` length modifiers are not accepted.
216 * * The `n` format specifier is not accepted.
217 * * The format specifiers defined in <inttypes.h> are not accepted
218 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
221 * `intro` specifies which special character immediately following an
222 * introductory `%` character in `fmt` is used to indicate a custom
223 * conversion specifier. For example, if `intro` is '@', then any `%@`
224 * sequence in `fmt` is the beginning of a custom conversion specifier.
226 * When a custom conversion specifier is encountered in `fmt`,
227 * the function calls `handle_specifier`. This callback receives:
230 * Custom, private data.
233 * Address of the current buffer pointer. `*buf` is the position to
234 * append new data. The callback must update `*buf` when appending
235 * new data. The callback must ensure not to write passed the whole
236 * buffer passed to bt_common_custom_vsnprintf().
239 * Number of bytes left in whole buffer from the `*buf` point.
242 * Address of the current format string pointer. `*fmt` points to
243 * the introductory `%` character, which is followed by the
244 * character `intro`. The callback must update `*fmt` so that it
245 * points after the whole custom conversion specifier.
248 * Variable argument list. Use va_arg() to get new arguments from
249 * this list and update it at the same time.
251 * Because this is an internal utility, this function and its callback
252 * do not return error codes: they abort when there's any error (bad
253 * format string, for example).
256 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
258 bt_common_handle_custom_specifier_func handle_specifier
,
259 void *priv_data
, const char *fmt
, va_list *args
);
262 * Variadic form of bt_common_custom_vsnprintf().
265 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
267 bt_common_handle_custom_specifier_func handle_specifier
,
268 void *priv_data
, const char *fmt
, ...);
271 * Returns the system page size.
274 size_t bt_common_get_page_size(void);
276 #endif /* BABELTRACE_COMMON_INTERNAL_H */