lib: metadata: transform fast path precond. checks to BT_ASSERT_PRE()
[babeltrace.git] / include / babeltrace / common-internal.h
CommitLineData
1670bffd
PP
1#ifndef BABELTRACE_COMMON_INTERNAL_H
2#define BABELTRACE_COMMON_INTERNAL_H
3
c55a9f58 4#include <stdbool.h>
1670bffd 5#include <babeltrace/babeltrace-internal.h>
85cd02cf 6#include <stdarg.h>
1670bffd 7
ad96d936
PP
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"
26
db0f160a
PP
27struct bt_common_lttng_live_url_parts {
28 GString *proto;
29 GString *hostname;
30 GString *target_hostname;
31 GString *session_name;
32
33 /* -1 means default port */
34 int port;
35};
36
6eecdc5f
PP
37/*
38 * Checks if the current process has setuid or setgid access rights.
39 * Returns `true` if so.
40 */
1670bffd
PP
41BT_HIDDEN
42bool bt_common_is_setuid_setgid(void);
43
6eecdc5f
PP
44/*
45 * Returns the system-wide plugin path, e.g.
46 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
47 */
1670bffd
PP
48BT_HIDDEN
49const char *bt_common_get_system_plugin_path(void);
50
6eecdc5f
PP
51/*
52 * Returns the user plugin path, e.g.
53 * `/home/user/.local/lib/babeltrace/plugins`. You need to free the
54 * return value.
55 */
1670bffd
PP
56BT_HIDDEN
57char *bt_common_get_home_plugin_path(void);
58
6eecdc5f
PP
59/*
60 * Appends the list of directories in `paths` to the array `dirs`.
61 * `paths` is a list of directories separated by `:`. Returns 0 on
62 * success.
63 */
1670bffd
PP
64BT_HIDDEN
65int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
66
6eecdc5f
PP
67/*
68 * Returns `true` if terminal color codes are supported for this
69 * process.
70 */
ad96d936
PP
71BT_HIDDEN
72bool bt_common_colors_supported(void);
73
290725f7
PP
74BT_HIDDEN
75const char *bt_common_color_reset(void);
76
77BT_HIDDEN
78const char *bt_common_color_bold(void);
79
80BT_HIDDEN
81const char *bt_common_color_fg_default(void);
82
83BT_HIDDEN
84const char *bt_common_color_fg_red(void);
85
86BT_HIDDEN
87const char *bt_common_color_fg_green(void);
88
89BT_HIDDEN
90const char *bt_common_color_fg_yellow(void);
91
92BT_HIDDEN
93const char *bt_common_color_fg_blue(void);
94
95BT_HIDDEN
96const char *bt_common_color_fg_magenta(void);
97
98BT_HIDDEN
99const char *bt_common_color_fg_cyan(void);
100
101BT_HIDDEN
102const char *bt_common_color_fg_light_gray(void);
103
104BT_HIDDEN
105const char *bt_common_color_bg_default(void);
106
107BT_HIDDEN
108const char *bt_common_color_bg_red(void);
109
110BT_HIDDEN
111const char *bt_common_color_bg_green(void);
112
113BT_HIDDEN
114const char *bt_common_color_bg_yellow(void);
115
116BT_HIDDEN
117const char *bt_common_color_bg_blue(void);
118
119BT_HIDDEN
120const char *bt_common_color_bg_magenta(void);
121
122BT_HIDDEN
123const char *bt_common_color_bg_cyan(void);
124
125BT_HIDDEN
126const char *bt_common_color_bg_light_gray(void);
127
6eecdc5f
PP
128/*
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.
133 */
db0f160a
PP
134BT_HIDDEN
135GString *bt_common_string_until(const char *input, const char *escapable_chars,
136 const char *end_chars, size_t *end_pos);
137
6eecdc5f
PP
138/*
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.
144 */
db0f160a 145BT_HIDDEN
36b405c6 146GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
db0f160a 147
6eecdc5f
PP
148/*
149 * Returns `true` if `input` is a string made only of printable
150 * characters.
151 */
db0f160a
PP
152BT_HIDDEN
153bool bt_common_string_is_printable(const char *input);
154
6eecdc5f
PP
155/*
156 * Destroys the parts of an LTTng live URL as returned by
157 * bt_common_parse_lttng_live_url().
158 */
db0f160a
PP
159BT_HIDDEN
160void bt_common_destroy_lttng_live_url_parts(
161 struct bt_common_lttng_live_url_parts *parts);
162
6eecdc5f
PP
163/*
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().
168 */
db0f160a
PP
169BT_HIDDEN
170struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
171 const char *url, char *error_buf, size_t error_buf_size);
172
6eecdc5f
PP
173/*
174 * Normalizes (in place) a star globbing pattern to be used with
175 * bt_common_star_glob_match(). This function always succeeds.
176 */
9009cc24
PP
177BT_HIDDEN
178void bt_common_normalize_star_glob_pattern(char *pattern);
179
6eecdc5f
PP
180/*
181 * Returns `true` if `candidate` (of size `candidate_len`) matches
182 * the star globbing pattern `pattern` (of size `pattern_len`).
183 */
9009cc24
PP
184BT_HIDDEN
185bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
186 const char *candidate, size_t candidate_len);
187
6eecdc5f
PP
188/*
189 * Normalizes the path `path`:
190 *
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
193 * if `wd` is NULL).
194 * * Removes consecutive and trailing slashes.
195 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
196 * * Does NOT resolve symbolic links.
197 *
198 * The caller owns the returned GString.
199 */
e49a18d1
PP
200BT_HIDDEN
201GString *bt_common_normalize_path(const char *path, const char *wd);
202
85cd02cf
PP
203typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data,
204 char **buf, size_t avail_size, const char **fmt, va_list *args);
205
206/*
207 * This is a custom vsnprintf() which handles the standard conversion
208 * specifier as well as custom ones.
209 *
210 * `fmt` is a typical printf()-style format string, with the following
211 * limitations:
212 *
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
219 * `PRIi64`.
220 *
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.
225 *
226 * When a custom conversion specifier is encountered in `fmt`,
227 * the function calls `handle_specifier`. This callback receives:
228 *
229 * `priv_data`:
230 * Custom, private data.
231 *
232 * `buf`:
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().
237 *
238 * `avail_size`:
239 * Number of bytes left in whole buffer from the `*buf` point.
240 *
241 * `fmt`:
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.
246 *
247 * `args`:
248 * Variable argument list. Use va_arg() to get new arguments from
249 * this list and update it at the same time.
250 *
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).
254 */
255BT_HIDDEN
256void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
257 char intro,
258 bt_common_handle_custom_specifier_func handle_specifier,
259 void *priv_data, const char *fmt, va_list *args);
260
261/*
262 * Variadic form of bt_common_custom_vsnprintf().
263 */
264BT_HIDDEN
265void bt_common_custom_snprintf(char *buf, size_t buf_size,
266 char intro,
267 bt_common_handle_custom_specifier_func handle_specifier,
268 void *priv_data, const char *fmt, ...);
269
108e5a1e 270/*
85cd02cf 271 * Returns the system page size.
108e5a1e
MJ
272 */
273BT_HIDDEN
274size_t bt_common_get_page_size(void);
275
1670bffd 276#endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.042734 seconds and 4 git commands to generate.