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 and following tokens as a map. Maps are
258 * formatted as an opening `{`, a list of comma-separated entries, and a
259 * closing `}`. And entry is a key (an unquoted string), an equal sign and
260 * a value. For convenience, this function supports an optional trailing comma
261 * after the last value.
263 * The current token of the parser must be the opening curly bracket
264 * (`{`) of the array.
267 bt_value
*ini_parse_map(struct ini_parsing_state
*state
)
270 GTokenType token_type
;
273 /* The `{` character must have already been ingested */
274 BT_ASSERT(g_scanner_cur_token(state
->scanner
) == G_TOKEN_CHAR
);
275 BT_ASSERT(g_scanner_cur_value(state
->scanner
).v_char
== '{');
277 map_value
= bt_value_map_create ();
279 ini_append_oom_error(state
->ini_error
);
283 token_type
= g_scanner_get_next_token(state
->scanner
);
285 /* While the current token is not a `}` */
286 while (!(token_type
== G_TOKEN_CHAR
&&
287 g_scanner_cur_value(state
->scanner
).v_char
== '}')) {
288 bt_value
*entry_value
;
289 bt_value_map_insert_entry_status insert_entry_status
;
291 /* Expect map key. */
292 if (token_type
!= G_TOKEN_IDENTIFIER
) {
293 ini_append_error_expecting(state
, state
->scanner
,
299 key
= g_strdup(g_scanner_cur_value(state
->scanner
).v_identifier
);
301 token_type
= g_scanner_get_next_token(state
->scanner
);
303 /* Expect equal sign. */
304 if (token_type
!= G_TOKEN_CHAR
||
305 g_scanner_cur_value(state
->scanner
).v_char
!= '=') {
306 ini_append_error_expecting(state
,
307 state
->scanner
, "'='");
311 token_type
= g_scanner_get_next_token(state
->scanner
);
313 /* Parse the entry value... */
314 entry_value
= ini_parse_value(state
);
319 /* ... and add it to the result map */
320 insert_entry_status
=
321 bt_value_map_insert_entry(map_value
, key
, entry_value
);
322 BT_VALUE_PUT_REF_AND_RESET(entry_value
);
323 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
328 * Ingest the token following the value. It should be
329 * either a comma or closing curly bracket.
331 token_type
= g_scanner_get_next_token(state
->scanner
);
332 if (token_type
== G_TOKEN_CHAR
&&
333 g_scanner_cur_value(state
->scanner
).v_char
== ',') {
335 * Ingest the token following the comma. If it
336 * happens to be a closing curly bracket, exit
337 * the loop and we are done (we allow trailing
338 * commas). Otherwise, we are ready for the next
339 * ini_parse_value() call.
341 token_type
= g_scanner_get_next_token(state
->scanner
);
342 } else if (token_type
!= G_TOKEN_CHAR
||
343 g_scanner_cur_value(state
->scanner
).v_char
!= '}') {
344 ini_append_error_expecting(state
, state
->scanner
,
352 BT_VALUE_PUT_REF_AND_RESET(map_value
);
361 * Parses the current token (and the following ones if needed) as a
362 * value, returning it as a `bt_value *`.
365 bt_value
*ini_parse_value(struct ini_parsing_state
*state
)
367 bt_value
*value
= NULL
;
368 GTokenType token_type
= state
->scanner
->token
;
370 switch (token_type
) {
372 if (state
->scanner
->value
.v_char
== '-') {
373 /* Negative number */
374 value
= ini_parse_neg_number(state
);
375 } else if (state
->scanner
->value
.v_char
== '+') {
376 /* Unsigned integer */
377 value
= ini_parse_uint(state
);
378 } else if (state
->scanner
->value
.v_char
== '[') {
380 value
= ini_parse_array(state
);
381 } else if (state
->scanner
->value
.v_char
== '{') {
383 value
= ini_parse_map(state
);
385 ini_append_error_expecting(state
, state
->scanner
, "value");
392 /* Positive, signed integer */
393 uint64_t int_val
= state
->scanner
->value
.v_int64
;
395 if (int_val
> INT64_MAX
) {
396 g_string_append_printf(state
->ini_error
,
397 "Integer value %" PRIu64
" is outside the range of a 64-bit signed integer\n",
401 value
= bt_value_integer_signed_create_init(
408 /* Positive floating point number */
409 value
= bt_value_real_create_init(state
->scanner
->value
.v_float
);
413 value
= bt_value_string_create_init(state
->scanner
->value
.v_string
);
415 case G_TOKEN_IDENTIFIER
:
418 * Using symbols would be appropriate here, but said
419 * symbols are allowed as map key, so it's easier to
420 * consider everything an identifier.
422 * If one of the known symbols is not recognized here,
423 * then fall back to creating a string value.
425 const char *id
= state
->scanner
->value
.v_identifier
;
427 if (strcmp(id
, "null") == 0 || strcmp(id
, "NULL") == 0 ||
428 strcmp(id
, "nul") == 0) {
429 value
= bt_value_null
;
430 bt_value_get_ref(value
);
431 } else if (strcmp(id
, "true") == 0 || strcmp(id
, "TRUE") == 0 ||
432 strcmp(id
, "yes") == 0 ||
433 strcmp(id
, "YES") == 0) {
434 value
= bt_value_bool_create_init(true);
435 } else if (strcmp(id
, "false") == 0 ||
436 strcmp(id
, "FALSE") == 0 ||
437 strcmp(id
, "no") == 0 ||
438 strcmp(id
, "NO") == 0) {
439 value
= bt_value_bool_create_init(false);
441 value
= bt_value_string_create_init(id
);
446 /* Unset return value variable will trigger the error */
447 ini_append_error_expecting(state
, state
->scanner
, "value");
456 * Handles the current state of the INI parser.
458 * Returns 0 to continue, 1 to end, or a negative value on error.
461 int ini_handle_state(struct ini_parsing_state
*state
)
464 GTokenType token_type
;
465 bt_value
*value
= NULL
;
467 token_type
= g_scanner_get_next_token(state
->scanner
);
468 if (token_type
== G_TOKEN_EOF
) {
469 if (state
->expecting
!= INI_EXPECT_COMMA
) {
470 switch (state
->expecting
) {
471 case INI_EXPECT_EQUAL
:
472 ini_append_error_expecting(state
,
473 state
->scanner
, "`=`");
475 case INI_EXPECT_VALUE
:
476 ini_append_error_expecting(state
,
477 state
->scanner
, "value");
479 case INI_EXPECT_MAP_KEY
:
480 ini_append_error_expecting(state
,
481 state
->scanner
, "unquoted map key");
494 switch (state
->expecting
) {
495 case INI_EXPECT_MAP_KEY
:
496 if (token_type
!= G_TOKEN_IDENTIFIER
) {
497 ini_append_error_expecting(state
, state
->scanner
,
502 g_string_assign(state
->last_map_key
,
503 state
->scanner
->value
.v_identifier
);
505 if (bt_value_map_has_entry(state
->params
,
506 state
->last_map_key
->str
)) {
507 g_string_append_printf(state
->ini_error
,
508 "Duplicate parameter key: `%s`\n",
509 state
->last_map_key
->str
);
513 state
->expecting
= INI_EXPECT_EQUAL
;
515 case INI_EXPECT_EQUAL
:
516 if (token_type
!= G_TOKEN_CHAR
) {
517 ini_append_error_expecting(state
,
518 state
->scanner
, "'='");
522 if (state
->scanner
->value
.v_char
!= '=') {
523 ini_append_error_expecting(state
,
524 state
->scanner
, "'='");
528 state
->expecting
= INI_EXPECT_VALUE
;
530 case INI_EXPECT_VALUE
:
532 value
= ini_parse_value(state
);
537 state
->expecting
= INI_EXPECT_COMMA
;
540 case INI_EXPECT_COMMA
:
541 if (token_type
!= G_TOKEN_CHAR
) {
542 ini_append_error_expecting(state
,
543 state
->scanner
, "','");
547 if (state
->scanner
->value
.v_char
!= ',') {
548 ini_append_error_expecting(state
,
549 state
->scanner
, "','");
553 state
->expecting
= INI_EXPECT_MAP_KEY
;
565 if (bt_value_map_insert_entry(state
->params
,
566 state
->last_map_key
->str
, value
)) {
567 /* Only override return value on error */
573 BT_VALUE_PUT_REF_AND_RESET(value
);
578 * Converts an INI-style argument to an equivalent map value object.
580 * Return value is owned by the caller.
583 bt_value
*cli_value_from_arg(const char *arg
, GString
*ini_error
)
585 /* Lexical scanner configuration */
586 GScannerConfig scanner_config
= {
587 /* Skip whitespaces */
588 .cset_skip_characters
= " \t\n",
590 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
591 .cset_identifier_first
=
595 .cset_identifier_nth
=
600 /* "hello" and "Hello" two different keys */
601 .case_sensitive
= TRUE
,
604 .cpair_comment_single
= NULL
,
605 .skip_comment_multi
= TRUE
,
606 .skip_comment_single
= TRUE
,
607 .scan_comment_multi
= FALSE
,
610 * Do scan identifiers, including 1-char identifiers,
611 * but NULL is a normal identifier.
613 .scan_identifier
= TRUE
,
614 .scan_identifier_1char
= TRUE
,
615 .scan_identifier_NULL
= FALSE
,
618 * No specific symbols: null and boolean "symbols" are
619 * scanned as plain identifiers.
621 .scan_symbols
= FALSE
,
622 .symbol_2_token
= FALSE
,
623 .scope_0_fallback
= FALSE
,
626 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
627 * integers prefixed with "$".
633 .scan_hex_dollar
= FALSE
,
635 /* Convert scanned numbers to integer tokens */
636 .numbers_2_int
= TRUE
,
638 /* Support both integers and floating point numbers */
639 .int_2_float
= FALSE
,
641 /* Scan integers as 64-bit signed integers */
644 /* Only scan double-quoted strings */
645 .scan_string_sq
= FALSE
,
646 .scan_string_dq
= TRUE
,
648 /* Do not converter identifiers to string tokens */
649 .identifier_2_string
= FALSE
,
651 /* Scan characters as `G_TOKEN_CHAR` token */
652 .char_2_token
= FALSE
,
654 struct ini_parsing_state state
= {
657 .expecting
= INI_EXPECT_MAP_KEY
,
659 .ini_error
= ini_error
,
662 BT_ASSERT(ini_error
);
663 g_string_assign(ini_error
, "");
664 state
.params
= bt_value_map_create();
666 ini_append_oom_error(ini_error
);
670 state
.scanner
= g_scanner_new(&scanner_config
);
671 if (!state
.scanner
) {
672 ini_append_oom_error(ini_error
);
676 state
.last_map_key
= g_string_new(NULL
);
677 if (!state
.last_map_key
) {
678 ini_append_oom_error(ini_error
);
682 /* Let the scan begin */
683 g_scanner_input_text(state
.scanner
, arg
, strlen(arg
));
686 int ret
= ini_handle_state(&state
);
691 } else if (ret
> 0) {
700 BT_VALUE_PUT_REF_AND_RESET(state
.params
);
704 g_scanner_destroy(state
.scanner
);
707 if (state
.last_map_key
) {
708 g_string_free(state
.last_map_key
, TRUE
);