cli: Refactor command line value parsing code
[babeltrace.git] / cli / babeltrace-cfg-cli-args.c
index 920d2a3f5041f981de78a5646fb55566ddd7dd72..a86d1096e0bf51209fc63816f694f0db4824e1f3 100644 (file)
@@ -70,9 +70,6 @@ enum ini_parsing_fsm_state {
        /* Expect a value */
        INI_EXPECT_VALUE,
 
-       /* Expect a negative number value */
-       INI_EXPECT_VALUE_NUMBER_NEG,
-
        /* Expect a comma character (',') */
        INI_EXPECT_COMMA,
 };
@@ -188,6 +185,122 @@ void ini_append_error_expecting(struct ini_parsing_state *state,
        g_string_append_printf(state->ini_error, "^\n\n");
 }
 
+/* Parse the next token as a number and return its negation. */
+static
+bt_value *ini_parse_neg_number(struct ini_parsing_state *state)
+{
+       bt_value *value = NULL;
+       GTokenType token_type = g_scanner_get_next_token(state->scanner);
+
+       switch (token_type) {
+       case G_TOKEN_INT:
+       {
+               /* Negative integer */
+               uint64_t int_val = state->scanner->value.v_int64;
+
+               if (int_val > (1ULL << 63) - 1) {
+                       g_string_append_printf(state->ini_error,
+                               "Integer value -%" PRIu64 " is outside the range of a 64-bit signed integer\n",
+                               int_val);
+               } else {
+                       value = bt_value_integer_create_init(-((int64_t) int_val));
+               }
+
+               break;
+       }
+       case G_TOKEN_FLOAT:
+               /* Negative floating point number */
+               value = bt_value_real_create_init(-state->scanner->value.v_float);
+               break;
+       default:
+               ini_append_error_expecting(state, state->scanner, "value");
+               break;
+       }
+
+       return value;
+}
+
+/*
+ * Parse the current token (and the following ones if needed) as a value, return
+ * it as a bt_value.
+ */
+static
+bt_value *ini_parse_value(struct ini_parsing_state *state)
+{
+       bt_value *value = NULL;
+       GTokenType token_type = state->scanner->token;
+
+       switch (token_type) {
+       case G_TOKEN_CHAR:
+               if (state->scanner->value.v_char == '-') {
+                       /* Negative number */
+                       value = ini_parse_neg_number(state);
+               } else {
+                       ini_append_error_expecting(state, state->scanner, "value");
+               }
+               break;
+       case G_TOKEN_INT:
+       {
+               /* Positive integer */
+               uint64_t int_val = state->scanner->value.v_int64;
+
+               if (int_val > (1ULL << 63) - 1) {
+                       g_string_append_printf(state->ini_error,
+                               "Integer value %" PRIu64 " is outside the range of a 64-bit signed integer\n",
+                               int_val);
+               } else {
+                       value = bt_value_integer_create_init((int64_t)int_val);
+               }
+               break;
+       }
+       case G_TOKEN_FLOAT:
+               /* Positive floating point number */
+               value = bt_value_real_create_init(state->scanner->value.v_float);
+               break;
+       case G_TOKEN_STRING:
+               /* Quoted string */
+               value = bt_value_string_create_init(state->scanner->value.v_string);
+               break;
+       case G_TOKEN_IDENTIFIER:
+       {
+               /*
+                * Using symbols would be appropriate here,
+                * but said symbols are allowed as map key,
+                * so it's easier to consider everything an
+                * identifier.
+                *
+                * If one of the known symbols is not
+                * recognized here, then fall back to creating
+                * a string value.
+                */
+               const char *id = state->scanner->value.v_identifier;
+
+               if (!strcmp(id, "null") || !strcmp(id, "NULL") ||
+                               !strcmp(id, "nul")) {
+                       value = bt_value_null;
+               } else if (!strcmp(id, "true") || !strcmp(id, "TRUE") ||
+                               !strcmp(id, "yes") ||
+                               !strcmp(id, "YES")) {
+                       value = bt_value_bool_create_init(true);
+               } else if (!strcmp(id, "false") ||
+                               !strcmp(id, "FALSE") ||
+                               !strcmp(id, "no") ||
+                               !strcmp(id, "NO")) {
+                       value = bt_value_bool_create_init(false);
+               } else {
+                       value = bt_value_string_create_init(id);
+               }
+               break;
+       }
+       default:
+               /* Unset value variable will trigger the error */
+               ini_append_error_expecting(state, state->scanner, "value");
+               break;
+       }
+
+       return value;
+}
+
 static
 int ini_handle_state(struct ini_parsing_state *state)
 {
@@ -204,7 +317,6 @@ int ini_handle_state(struct ini_parsing_state *state)
                                        state->scanner, "'='");
                                break;
                        case INI_EXPECT_VALUE:
-                       case INI_EXPECT_VALUE_NUMBER_NEG:
                                ini_append_error_expecting(state,
                                        state->scanner, "value");
                                break;
@@ -267,117 +379,8 @@ int ini_handle_state(struct ini_parsing_state *state)
                goto success;
        case INI_EXPECT_VALUE:
        {
-               switch (token_type) {
-               case G_TOKEN_CHAR:
-                       if (state->scanner->value.v_char == '-') {
-                               /* Negative number */
-                               state->expecting =
-                                       INI_EXPECT_VALUE_NUMBER_NEG;
-                               goto success;
-                       } else {
-                               ini_append_error_expecting(state,
-                                       state->scanner, "value");
-                               goto error;
-                       }
-                       break;
-               case G_TOKEN_INT:
-               {
-                       /* Positive integer */
-                       uint64_t int_val = state->scanner->value.v_int64;
-
-                       if (int_val > (1ULL << 63) - 1) {
-                               g_string_append_printf(state->ini_error,
-                                       "Integer value %" PRIu64 " is outside the range of a 64-bit signed integer\n",
-                                       int_val);
-                               goto error;
-                       }
-
-                       value = bt_value_integer_create_init((int64_t)int_val);
-                       break;
-               }
-               case G_TOKEN_FLOAT:
-                       /* Positive floating point number */
-                       value = bt_value_real_create_init(state->scanner->value.v_float);
-                       break;
-               case G_TOKEN_STRING:
-                       /* Quoted string */
-                       value = bt_value_string_create_init(state->scanner->value.v_string);
-                       break;
-               case G_TOKEN_IDENTIFIER:
-               {
-                       /*
-                        * Using symbols would be appropriate here,
-                        * but said symbols are allowed as map key,
-                        * so it's easier to consider everything an
-                        * identifier.
-                        *
-                        * If one of the known symbols is not
-                        * recognized here, then fall back to creating
-                        * a string value.
-                        */
-                       const char *id = state->scanner->value.v_identifier;
-
-                       if (!strcmp(id, "null") || !strcmp(id, "NULL") ||
-                                       !strcmp(id, "nul")) {
-                               value = bt_value_null;
-                       } else if (!strcmp(id, "true") || !strcmp(id, "TRUE") ||
-                                       !strcmp(id, "yes") ||
-                                       !strcmp(id, "YES")) {
-                               value = bt_value_bool_create_init(true);
-                       } else if (!strcmp(id, "false") ||
-                                       !strcmp(id, "FALSE") ||
-                                       !strcmp(id, "no") ||
-                                       !strcmp(id, "NO")) {
-                               value = bt_value_bool_create_init(false);
-                       } else {
-                               value = bt_value_string_create_init(id);
-                       }
-                       break;
-               }
-               default:
-                       /* Unset value variable will trigger the error */
-                       break;
-               }
-
-               if (!value) {
-                       ini_append_error_expecting(state,
-                               state->scanner, "value");
-                       goto error;
-               }
-
-               state->expecting = INI_EXPECT_COMMA;
-               goto success;
-       }
-       case INI_EXPECT_VALUE_NUMBER_NEG:
-       {
-               switch (token_type) {
-               case G_TOKEN_INT:
-               {
-                       /* Negative integer */
-                       uint64_t int_val = state->scanner->value.v_int64;
-
-                       if (int_val > (1ULL << 63) - 1) {
-                               g_string_append_printf(state->ini_error,
-                                       "Integer value -%" PRIu64 " is outside the range of a 64-bit signed integer\n",
-                                       int_val);
-                               goto error;
-                       }
-
-                       value = bt_value_integer_create_init(-((int64_t)int_val));
-                       break;
-               }
-               case G_TOKEN_FLOAT:
-                       /* Negative floating point number */
-                       value = bt_value_real_create_init(-state->scanner->value.v_float);
-                       break;
-               default:
-                       /* Unset value variable will trigger the error */
-                       break;
-               }
-
+               value = ini_parse_value(state);
                if (!value) {
-                       ini_append_error_expecting(state,
-                               state->scanner, "value");
                        goto error;
                }
 
@@ -4102,7 +4105,7 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
                        append_implicit_component_param(
                                &implicit_text_args, "clock-gmt", "yes");
                        append_implicit_component_param(
-                               &implicit_trimmer_args, "clock-gmt", "yes");
+                               &implicit_trimmer_args, "gmt", "yes");
                        implicit_text_args.exists = true;
                        break;
                case OPT_CLOCK_OFFSET:
This page took 0.026373 seconds and 4 git commands to generate.