2 * Copyright 2016-2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include "common/assert.h"
30 #include <babeltrace2/babeltrace.h>
31 #include "common/common.h"
33 #include <sys/types.h>
34 #include "babeltrace2-cfg.h"
35 #include "babeltrace2-cfg-cli-args.h"
36 #include "babeltrace2-cfg-cli-args-connect.h"
37 #include "common/version.h"
39 /* INI-style parsing FSM states */
40 enum ini_parsing_fsm_state
{
41 /* Expect a map key (identifier) */
44 /* Expect an equal character (`=`) */
50 /* Expect a comma character (`,`) */
54 /* INI-style parsing state variables */
55 struct ini_parsing_state
{
56 /* Lexical scanner (owned by this) */
59 /* Output map value object being filled (owned by this) */
62 /* Next expected FSM state */
63 enum ini_parsing_fsm_state expecting
;
65 /* Last decoded map key (owned by this) */
66 GString
*last_map_key
;
68 /* Complete INI-style string to parse */
71 /* Error buffer (weak) */
76 * Appends an "expecting token" error to the INI-style parsing state's
80 void ini_append_error_expecting(struct ini_parsing_state
*state
,
81 GScanner
*scanner
, const char *expecting
)
86 g_string_append_printf(state
->ini_error
, "Expecting %s:\n", expecting
);
88 /* Only append error if there's one line */
89 if (strchr(state
->arg
, '\n') || strlen(state
->arg
) == 0) {
93 g_string_append_printf(state
->ini_error
, "\n %s\n", state
->arg
);
94 pos
= g_scanner_cur_position(scanner
) + 4;
96 if (!g_scanner_eof(scanner
)) {
100 for (i
= 0; i
< pos
; ++i
) {
101 g_string_append_printf(state
->ini_error
, " ");
104 g_string_append_printf(state
->ini_error
, "^\n\n");
108 void ini_append_oom_error(GString
*error
)
111 g_string_append(error
, "Out of memory\n");
115 * Parses the next token as an unsigned integer.
118 bt_value
*ini_parse_uint(struct ini_parsing_state
*state
)
120 bt_value
*value
= NULL
;
121 GTokenType token_type
= g_scanner_get_next_token(state
->scanner
);
123 if (token_type
!= G_TOKEN_INT
) {
124 ini_append_error_expecting(state
, state
->scanner
,
129 value
= bt_value_integer_unsigned_create_init(
130 state
->scanner
->value
.v_int64
);
137 * Parses the next token as a number and returns its negation.
140 bt_value
*ini_parse_neg_number(struct ini_parsing_state
*state
)
142 bt_value
*value
= NULL
;
143 GTokenType token_type
= g_scanner_get_next_token(state
->scanner
);
145 switch (token_type
) {
148 /* Negative integer */
149 uint64_t int_val
= state
->scanner
->value
.v_int64
;
151 if (int_val
> (((uint64_t) INT64_MAX
) + 1)) {
152 g_string_append_printf(state
->ini_error
,
153 "Integer value -%" PRIu64
" is outside the range of a 64-bit signed integer\n",
156 value
= bt_value_integer_signed_create_init(
157 -((int64_t) int_val
));
163 /* Negative floating point number */
164 value
= bt_value_real_create_init(
165 -state
->scanner
->value
.v_float
);
168 ini_append_error_expecting(state
, state
->scanner
, "value");
175 static bt_value
*ini_parse_value(struct ini_parsing_state
*state
);
178 * Parses the current and following tokens as an array. Arrays are
179 * formatted as an opening `[`, a list of comma-separated values, and a
180 * closing `]`. For convenience, this function supports an optional
181 * trailing comma after the last value.
183 * The current token of the parser must be the opening square bracket
184 * (`[`) of the array.
187 bt_value
*ini_parse_array(struct ini_parsing_state
*state
)
189 bt_value
*array_value
;
190 GTokenType token_type
;
192 /* The `[` character must have already been ingested */
193 BT_ASSERT(g_scanner_cur_token(state
->scanner
) == G_TOKEN_CHAR
);
194 BT_ASSERT(g_scanner_cur_value(state
->scanner
).v_char
== '[');
196 array_value
= bt_value_array_create ();
198 ini_append_oom_error(state
->ini_error
);
202 token_type
= g_scanner_get_next_token(state
->scanner
);
204 /* While the current token is not a `]` */
205 while (!(token_type
== G_TOKEN_CHAR
&&
206 g_scanner_cur_value(state
->scanner
).v_char
== ']')) {
207 bt_value
*item_value
;
208 bt_value_array_append_element_status append_status
;
210 /* Parse the item... */
211 item_value
= ini_parse_value(state
);
216 /* ... and add it to the result array */
217 append_status
= bt_value_array_append_element(array_value
,
219 BT_VALUE_PUT_REF_AND_RESET(item_value
);
220 if (append_status
< 0) {
225 * Ingest the token following the value. It should be
226 * either a comma or closing square bracket.
228 token_type
= g_scanner_get_next_token(state
->scanner
);
229 if (token_type
== G_TOKEN_CHAR
&&
230 g_scanner_cur_value(state
->scanner
).v_char
== ',') {
232 * Ingest the token following the comma. If it
233 * happens to be a closing square bracket, exit
234 * the loop and we are done (we allow trailing
235 * commas). Otherwise, we are ready for the next
236 * ini_parse_value() call.
238 token_type
= g_scanner_get_next_token(state
->scanner
);
239 } else if (token_type
!= G_TOKEN_CHAR
||
240 g_scanner_cur_value(state
->scanner
).v_char
!= ']') {
241 ini_append_error_expecting(state
, state
->scanner
,
250 BT_VALUE_PUT_REF_AND_RESET(array_value
);
257 * Parses the current token (and the following ones if needed) as a
258 * value, returning it as a `bt_value *`.
261 bt_value
*ini_parse_value(struct ini_parsing_state
*state
)
263 bt_value
*value
= NULL
;
264 GTokenType token_type
= state
->scanner
->token
;
266 switch (token_type
) {
268 if (state
->scanner
->value
.v_char
== '-') {
269 /* Negative number */
270 value
= ini_parse_neg_number(state
);
271 } else if (state
->scanner
->value
.v_char
== '+') {
272 /* Unsigned integer */
273 value
= ini_parse_uint(state
);
274 } else if (state
->scanner
->value
.v_char
== '[') {
276 value
= ini_parse_array(state
);
278 ini_append_error_expecting(state
, state
->scanner
, "value");
285 /* Positive, signed integer */
286 uint64_t int_val
= state
->scanner
->value
.v_int64
;
288 if (int_val
> INT64_MAX
) {
289 g_string_append_printf(state
->ini_error
,
290 "Integer value %" PRIu64
" is outside the range of a 64-bit signed integer\n",
294 value
= bt_value_integer_signed_create_init(
301 /* Positive floating point number */
302 value
= bt_value_real_create_init(state
->scanner
->value
.v_float
);
306 value
= bt_value_string_create_init(state
->scanner
->value
.v_string
);
308 case G_TOKEN_IDENTIFIER
:
311 * Using symbols would be appropriate here, but said
312 * symbols are allowed as map key, so it's easier to
313 * consider everything an identifier.
315 * If one of the known symbols is not recognized here,
316 * then fall back to creating a string value.
318 const char *id
= state
->scanner
->value
.v_identifier
;
320 if (strcmp(id
, "null") == 0 || strcmp(id
, "NULL") == 0 ||
321 strcmp(id
, "nul") == 0) {
322 value
= bt_value_null
;
323 } else if (strcmp(id
, "true") == 0 || strcmp(id
, "TRUE") == 0 ||
324 strcmp(id
, "yes") == 0 ||
325 strcmp(id
, "YES") == 0) {
326 value
= bt_value_bool_create_init(true);
327 } else if (strcmp(id
, "false") == 0 ||
328 strcmp(id
, "FALSE") == 0 ||
329 strcmp(id
, "no") == 0 ||
330 strcmp(id
, "NO") == 0) {
331 value
= bt_value_bool_create_init(false);
333 value
= bt_value_string_create_init(id
);
338 /* Unset return value variable will trigger the error */
339 ini_append_error_expecting(state
, state
->scanner
, "value");
348 * Handles the current state of the INI parser.
350 * Returns 0 to continue, 1 to end, or a negative value on error.
353 int ini_handle_state(struct ini_parsing_state
*state
)
356 GTokenType token_type
;
357 bt_value
*value
= NULL
;
359 token_type
= g_scanner_get_next_token(state
->scanner
);
360 if (token_type
== G_TOKEN_EOF
) {
361 if (state
->expecting
!= INI_EXPECT_COMMA
) {
362 switch (state
->expecting
) {
363 case INI_EXPECT_EQUAL
:
364 ini_append_error_expecting(state
,
365 state
->scanner
, "`=`");
367 case INI_EXPECT_VALUE
:
368 ini_append_error_expecting(state
,
369 state
->scanner
, "value");
371 case INI_EXPECT_MAP_KEY
:
372 ini_append_error_expecting(state
,
373 state
->scanner
, "unquoted map key");
386 switch (state
->expecting
) {
387 case INI_EXPECT_MAP_KEY
:
388 if (token_type
!= G_TOKEN_IDENTIFIER
) {
389 ini_append_error_expecting(state
, state
->scanner
,
394 g_string_assign(state
->last_map_key
,
395 state
->scanner
->value
.v_identifier
);
397 if (bt_value_map_has_entry(state
->params
,
398 state
->last_map_key
->str
)) {
399 g_string_append_printf(state
->ini_error
,
400 "Duplicate parameter key: `%s`\n",
401 state
->last_map_key
->str
);
405 state
->expecting
= INI_EXPECT_EQUAL
;
407 case INI_EXPECT_EQUAL
:
408 if (token_type
!= G_TOKEN_CHAR
) {
409 ini_append_error_expecting(state
,
410 state
->scanner
, "'='");
414 if (state
->scanner
->value
.v_char
!= '=') {
415 ini_append_error_expecting(state
,
416 state
->scanner
, "'='");
420 state
->expecting
= INI_EXPECT_VALUE
;
422 case INI_EXPECT_VALUE
:
424 value
= ini_parse_value(state
);
429 state
->expecting
= INI_EXPECT_COMMA
;
432 case INI_EXPECT_COMMA
:
433 if (token_type
!= G_TOKEN_CHAR
) {
434 ini_append_error_expecting(state
,
435 state
->scanner
, "','");
439 if (state
->scanner
->value
.v_char
!= ',') {
440 ini_append_error_expecting(state
,
441 state
->scanner
, "','");
445 state
->expecting
= INI_EXPECT_MAP_KEY
;
457 if (bt_value_map_insert_entry(state
->params
,
458 state
->last_map_key
->str
, value
)) {
459 /* Only override return value on error */
465 BT_VALUE_PUT_REF_AND_RESET(value
);
470 * Converts an INI-style argument to an equivalent map value object.
472 * Return value is owned by the caller.
475 bt_value
*cli_value_from_arg(const char *arg
, GString
*ini_error
)
477 /* Lexical scanner configuration */
478 GScannerConfig scanner_config
= {
479 /* Skip whitespaces */
480 .cset_skip_characters
= " \t\n",
482 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
483 .cset_identifier_first
=
487 .cset_identifier_nth
=
492 /* "hello" and "Hello" two different keys */
493 .case_sensitive
= TRUE
,
496 .cpair_comment_single
= NULL
,
497 .skip_comment_multi
= TRUE
,
498 .skip_comment_single
= TRUE
,
499 .scan_comment_multi
= FALSE
,
502 * Do scan identifiers, including 1-char identifiers,
503 * but NULL is a normal identifier.
505 .scan_identifier
= TRUE
,
506 .scan_identifier_1char
= TRUE
,
507 .scan_identifier_NULL
= FALSE
,
510 * No specific symbols: null and boolean "symbols" are
511 * scanned as plain identifiers.
513 .scan_symbols
= FALSE
,
514 .symbol_2_token
= FALSE
,
515 .scope_0_fallback
= FALSE
,
518 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
519 * integers prefixed with "$".
525 .scan_hex_dollar
= FALSE
,
527 /* Convert scanned numbers to integer tokens */
528 .numbers_2_int
= TRUE
,
530 /* Support both integers and floating point numbers */
531 .int_2_float
= FALSE
,
533 /* Scan integers as 64-bit signed integers */
536 /* Only scan double-quoted strings */
537 .scan_string_sq
= FALSE
,
538 .scan_string_dq
= TRUE
,
540 /* Do not converter identifiers to string tokens */
541 .identifier_2_string
= FALSE
,
543 /* Scan characters as `G_TOKEN_CHAR` token */
544 .char_2_token
= FALSE
,
546 struct ini_parsing_state state
= {
549 .expecting
= INI_EXPECT_MAP_KEY
,
551 .ini_error
= ini_error
,
554 BT_ASSERT(ini_error
);
555 g_string_assign(ini_error
, "");
556 state
.params
= bt_value_map_create();
558 ini_append_oom_error(ini_error
);
562 state
.scanner
= g_scanner_new(&scanner_config
);
563 if (!state
.scanner
) {
564 ini_append_oom_error(ini_error
);
568 state
.last_map_key
= g_string_new(NULL
);
569 if (!state
.last_map_key
) {
570 ini_append_oom_error(ini_error
);
574 /* Let the scan begin */
575 g_scanner_input_text(state
.scanner
, arg
, strlen(arg
));
578 int ret
= ini_handle_state(&state
);
583 } else if (ret
> 0) {
592 BT_VALUE_PUT_REF_AND_RESET(state
.params
);
596 g_scanner_destroy(state
.scanner
);
599 if (state
.last_map_key
) {
600 g_string_free(state
.last_map_key
, TRUE
);