From f956eb2d2b08c13e4f105fce5de566b9f2346693 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 4 Oct 2019 15:00:01 -0400 Subject: [PATCH] Move CLI params parsing to its own lib This will allow using it for parameter parsing in a test, for convenience. The sharp-eyed reader will have noticed that this patch also removes unused structures from src/cli/babeltrace2-cfg-cli-args.c. These were likely forgotten there when the parameter parsing code was moved to its own file. Change-Id: Ibc7ecb238905051492718f1d176c0fb5f0c172a8 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/2135 Tested-by: jenkins Reviewed-by: Francis Deslauriers --- configure.ac | 1 + src/Makefile.am | 1 + src/cli/Makefile.am | 5 +-- src/cli/babeltrace2-cfg-cli-args.c | 44 ++----------------- src/param-parse/Makefile.am | 5 +++ .../param-parse.c} | 5 +-- .../param-parse.h} | 8 ++-- 7 files changed, 18 insertions(+), 51 deletions(-) create mode 100644 src/param-parse/Makefile.am rename src/{cli/babeltrace2-cfg-cli-params-arg.c => param-parse/param-parse.c} (98%) rename src/{cli/babeltrace2-cfg-cli-params-arg.h => param-parse/param-parse.h} (85%) diff --git a/configure.ac b/configure.ac index 8e03697d..55726570 100644 --- a/configure.ac +++ b/configure.ac @@ -720,6 +720,7 @@ AC_CONFIG_FILES([ src/plugins/utils/trimmer/Makefile src/py-common/Makefile src/python-plugin-provider/Makefile + src/param-parse/Makefile tests/bitfield/Makefile tests/ctf-writer/Makefile tests/argpar/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 2e9acc59..4323bfc3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,6 +11,7 @@ SUBDIRS = \ lib \ python-plugin-provider \ plugins \ + param-parse \ cli \ bindings diff --git a/src/cli/Makefile.am b/src/cli/Makefile.am index bc7627e9..e5567843 100644 --- a/src/cli/Makefile.am +++ b/src/cli/Makefile.am @@ -30,8 +30,6 @@ babeltrace2_bin_SOURCES = \ babeltrace2-cfg-cli-args-connect.h \ babeltrace2-cfg-cli-args-default.h \ babeltrace2-cfg-cli-args-default.c \ - babeltrace2-cfg-cli-params-arg.c \ - babeltrace2-cfg-cli-params-arg.h \ babeltrace2-log-level.c \ babeltrace2-log-level.h \ babeltrace2-plugins.c \ @@ -60,7 +58,8 @@ babeltrace2_bin_LDADD = \ $(top_builddir)/src/compat/libcompat.la \ $(top_builddir)/src/common/libbabeltrace2-common.la \ $(top_builddir)/src/logging/libbabeltrace2-logging.la \ - $(top_builddir)/src/ctfser/libbabeltrace2-ctfser.la + $(top_builddir)/src/ctfser/libbabeltrace2-ctfser.la \ + $(top_builddir)/src/param-parse/libbabeltrace2-param-parse.la if ENABLE_BUILT_IN_PLUGINS # Takes a plugin name and outputs the needed LDFLAGS to embed it. diff --git a/src/cli/babeltrace2-cfg-cli-args.c b/src/cli/babeltrace2-cfg-cli-args.c index d7d34e46..5df6ce3a 100644 --- a/src/cli/babeltrace2-cfg-cli-args.c +++ b/src/cli/babeltrace2-cfg-cli-args.c @@ -40,49 +40,13 @@ #include "babeltrace2-cfg.h" #include "babeltrace2-cfg-cli-args.h" #include "babeltrace2-cfg-cli-args-connect.h" -#include "babeltrace2-cfg-cli-params-arg.h" +#include "param-parse/param-parse.h" #include "babeltrace2-log-level.h" #include "babeltrace2-plugins.h" #include "babeltrace2-query.h" #include "autodisc/autodisc.h" #include "common/version.h" -/* INI-style parsing FSM states */ -enum ini_parsing_fsm_state { - /* Expect a map key (identifier) */ - INI_EXPECT_MAP_KEY, - - /* Expect an equal character ('=') */ - INI_EXPECT_EQUAL, - - /* Expect a value */ - INI_EXPECT_VALUE, - - /* Expect a comma character (',') */ - INI_EXPECT_COMMA, -}; - -/* INI-style parsing state variables */ -struct ini_parsing_state { - /* Lexical scanner (owned by this) */ - GScanner *scanner; - - /* Output map value object being filled (owned by this) */ - bt_value *params; - - /* Next expected FSM state */ - enum ini_parsing_fsm_state expecting; - - /* Last decoded map key (owned by this) */ - char *last_map_key; - - /* Complete INI-style string to parse (not owned by this) */ - const char *arg; - - /* Error buffer (not owned by this) */ - GString *ini_error; -}; - /* Offset option with "is set" boolean */ struct offset_opt { int64_t value; @@ -1564,7 +1528,7 @@ struct bt_config *bt_config_query_from_args(int argc, const char *argv[], case OPT_PARAMS: { bt_value_put_ref(params); - params = cli_value_from_arg(arg, error_str); + params = bt_param_parse(arg, error_str); if (!params) { BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s", error_str->str); @@ -1963,7 +1927,7 @@ struct bt_config *bt_config_run_from_args(int argc, const char *argv[], goto error; } - params = cli_value_from_arg(arg, error_str); + params = bt_param_parse(arg, error_str); if (!params) { BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s", error_str->str); @@ -1999,7 +1963,7 @@ struct bt_config *bt_config_run_from_args(int argc, const char *argv[], break; case OPT_BASE_PARAMS: { - bt_value *params = cli_value_from_arg(arg, error_str); + bt_value *params = bt_param_parse(arg, error_str); if (!params) { BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s", diff --git a/src/param-parse/Makefile.am b/src/param-parse/Makefile.am new file mode 100644 index 00000000..c3f0a15d --- /dev/null +++ b/src/param-parse/Makefile.am @@ -0,0 +1,5 @@ +noinst_LTLIBRARIES = libbabeltrace2-param-parse.la + +libbabeltrace2_param_parse_la_SOURCES = \ + param-parse.c \ + param-parse.h diff --git a/src/cli/babeltrace2-cfg-cli-params-arg.c b/src/param-parse/param-parse.c similarity index 98% rename from src/cli/babeltrace2-cfg-cli-params-arg.c rename to src/param-parse/param-parse.c index f49bb695..e2385ad8 100644 --- a/src/cli/babeltrace2-cfg-cli-params-arg.c +++ b/src/param-parse/param-parse.c @@ -31,9 +31,6 @@ #include "common/common.h" #include #include -#include "babeltrace2-cfg.h" -#include "babeltrace2-cfg-cli-args.h" -#include "babeltrace2-cfg-cli-args-connect.h" /* INI-style parsing FSM states */ enum ini_parsing_fsm_state { @@ -579,7 +576,7 @@ end: * Return value is owned by the caller. */ BT_HIDDEN -bt_value *cli_value_from_arg(const char *arg, GString *ini_error) +bt_value *bt_param_parse(const char *arg, GString *ini_error) { /* Lexical scanner configuration */ GScannerConfig scanner_config = { diff --git a/src/cli/babeltrace2-cfg-cli-params-arg.h b/src/param-parse/param-parse.h similarity index 85% rename from src/cli/babeltrace2-cfg-cli-params-arg.h rename to src/param-parse/param-parse.h index caab7c44..301b7e22 100644 --- a/src/cli/babeltrace2-cfg-cli-params-arg.h +++ b/src/param-parse/param-parse.h @@ -1,5 +1,5 @@ -#ifndef CLI_BABELTRACE_CFG_CLI_PARAMS_ARG_H -#define CLI_BABELTRACE_CFG_CLI_PARAMS_ARG_H +#ifndef SRC_PARAMS_PARSE_VALUE_PARSE_H +#define SRC_PARAMS_PARSE_VALUE_PARSE_H /* * Copyright 2016-2019 Philippe Proulx @@ -28,6 +28,6 @@ #include "common/macros.h" BT_HIDDEN -bt_value *cli_value_from_arg(const char *arg, GString *ini_error); +bt_value *bt_param_parse(const char *arg, GString *ini_error); -#endif /* CLI_BABELTRACE_CFG_CLI_PARAMS_ARG_H */ +#endif /* SRC_VALUE_PARSE_VALUE_PARSE_H */ -- 2.34.1