Common: add internal bt_common_custom_vsnprintf()
[babeltrace.git] / include / babeltrace / common-internal.h
1 #ifndef BABELTRACE_COMMON_INTERNAL_H
2 #define BABELTRACE_COMMON_INTERNAL_H
3
4 #include <stdbool.h>
5 #include <babeltrace/babeltrace-internal.h>
6 #include <stdarg.h>
7
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
27 struct 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
37 /*
38 * Checks if the current process has setuid or setgid access rights.
39 * Returns `true` if so.
40 */
41 BT_HIDDEN
42 bool bt_common_is_setuid_setgid(void);
43
44 /*
45 * Returns the system-wide plugin path, e.g.
46 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
47 */
48 BT_HIDDEN
49 const char *bt_common_get_system_plugin_path(void);
50
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 */
56 BT_HIDDEN
57 char *bt_common_get_home_plugin_path(void);
58
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 */
64 BT_HIDDEN
65 int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
66
67 /*
68 * Returns `true` if terminal color codes are supported for this
69 * process.
70 */
71 BT_HIDDEN
72 bool bt_common_colors_supported(void);
73
74 BT_HIDDEN
75 const char *bt_common_color_reset(void);
76
77 BT_HIDDEN
78 const char *bt_common_color_bold(void);
79
80 BT_HIDDEN
81 const char *bt_common_color_fg_default(void);
82
83 BT_HIDDEN
84 const char *bt_common_color_fg_red(void);
85
86 BT_HIDDEN
87 const char *bt_common_color_fg_green(void);
88
89 BT_HIDDEN
90 const char *bt_common_color_fg_yellow(void);
91
92 BT_HIDDEN
93 const char *bt_common_color_fg_blue(void);
94
95 BT_HIDDEN
96 const char *bt_common_color_fg_magenta(void);
97
98 BT_HIDDEN
99 const char *bt_common_color_fg_cyan(void);
100
101 BT_HIDDEN
102 const char *bt_common_color_fg_light_gray(void);
103
104 BT_HIDDEN
105 const char *bt_common_color_bg_default(void);
106
107 BT_HIDDEN
108 const char *bt_common_color_bg_red(void);
109
110 BT_HIDDEN
111 const char *bt_common_color_bg_green(void);
112
113 BT_HIDDEN
114 const char *bt_common_color_bg_yellow(void);
115
116 BT_HIDDEN
117 const char *bt_common_color_bg_blue(void);
118
119 BT_HIDDEN
120 const char *bt_common_color_bg_magenta(void);
121
122 BT_HIDDEN
123 const char *bt_common_color_bg_cyan(void);
124
125 BT_HIDDEN
126 const char *bt_common_color_bg_light_gray(void);
127
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 */
134 BT_HIDDEN
135 GString *bt_common_string_until(const char *input, const char *escapable_chars,
136 const char *end_chars, size_t *end_pos);
137
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 */
145 BT_HIDDEN
146 GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
147
148 /*
149 * Returns `true` if `input` is a string made only of printable
150 * characters.
151 */
152 BT_HIDDEN
153 bool bt_common_string_is_printable(const char *input);
154
155 /*
156 * Destroys the parts of an LTTng live URL as returned by
157 * bt_common_parse_lttng_live_url().
158 */
159 BT_HIDDEN
160 void bt_common_destroy_lttng_live_url_parts(
161 struct bt_common_lttng_live_url_parts *parts);
162
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 */
169 BT_HIDDEN
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);
172
173 /*
174 * Normalizes (in place) a star globbing pattern to be used with
175 * bt_common_star_glob_match(). This function always succeeds.
176 */
177 BT_HIDDEN
178 void bt_common_normalize_star_glob_pattern(char *pattern);
179
180 /*
181 * Returns `true` if `candidate` (of size `candidate_len`) matches
182 * the star globbing pattern `pattern` (of size `pattern_len`).
183 */
184 BT_HIDDEN
185 bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
186 const char *candidate, size_t candidate_len);
187
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 */
200 BT_HIDDEN
201 GString *bt_common_normalize_path(const char *path, const char *wd);
202
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);
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 */
255 BT_HIDDEN
256 void 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 */
264 BT_HIDDEN
265 void 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
270 /*
271 * Returns the system page size.
272 */
273 BT_HIDDEN
274 size_t bt_common_get_page_size(void);
275
276 #endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.035927 seconds and 5 git commands to generate.