From: Simon Marchi Date: Wed, 4 Dec 2019 23:07:44 +0000 (-0500) Subject: Use argpar from upstream X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=65966041723ea2cdd82de44916fcca4589673a03 Use argpar from upstream Now that argpar is maintained in a separate repository, sync the code with it and remove tests from this code base. The code was sync'ed with the argpar repository at commit: f46b510674785c70781a3de06c02888faded5db9 (HEAD -> master, Strip trailing spaces Change-Id: Ie805db224f2d3890decbed26a1c80c105d083293 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/2616 --- diff --git a/configure.ac b/configure.ac index 3c2955bf..b1edd9ee 100644 --- a/configure.ac +++ b/configure.ac @@ -783,7 +783,6 @@ AC_CONFIG_FILES([ src/string-format/Makefile tests/bitfield/Makefile tests/ctf-writer/Makefile - tests/argpar/Makefile tests/lib/Makefile tests/lib/test-plugin-plugins/Makefile tests/Makefile diff --git a/src/argpar/Makefile.am b/src/argpar/Makefile.am index 63e97769..175526ab 100644 --- a/src/argpar/Makefile.am +++ b/src/argpar/Makefile.am @@ -1,3 +1,3 @@ -noinst_LTLIBRARIES = libbabeltrace2-argpar.la +noinst_LTLIBRARIES = libargpar.la -libbabeltrace2_argpar_la_SOURCES = argpar.c argpar.h +libargpar_la_SOURCES = argpar.c argpar.h diff --git a/src/argpar/argpar.c b/src/argpar/argpar.c index 7f4e46b3..8cc20cd7 100644 --- a/src/argpar/argpar.c +++ b/src/argpar/argpar.c @@ -20,52 +20,211 @@ * SOFTWARE. */ +#include +#include #include +#include #include #include -#include - -#include "common/assert.h" -#include "common/common.h" #include "argpar.h" +#define argpar_realloc(_ptr, _type, _nmemb) ((_type *) realloc(_ptr, (_nmemb) * sizeof(_type))) +#define argpar_calloc(_type, _nmemb) ((_type *) calloc((_nmemb), sizeof(_type))) +#define argpar_zalloc(_type) argpar_calloc(_type, 1) + +#define ARGPAR_ASSERT(_cond) assert(_cond) + +static +char *argpar_vasprintf(const char *fmt, va_list args) +{ + int len1, len2; + char *str; + va_list args2; + + va_copy(args2, args); + + len1 = vsnprintf(NULL, 0, fmt, args); + if (len1 < 0) { + str = NULL; + goto end; + } + + str = malloc(len1 + 1); + if (!str) { + goto end; + } + + len2 = vsnprintf(str, len1 + 1, fmt, args2); + + ARGPAR_ASSERT(len1 == len2); + +end: + return str; +} + + +static +char *argpar_asprintf(const char *fmt, ...) +{ + va_list args; + char *str; + + va_start(args, fmt); + str = argpar_vasprintf(fmt, args); + va_end(args); + + return str; +} + static -void destroy_item(struct bt_argpar_item * const item) +bool argpar_string_append_printf(char **str, const char *fmt, ...) +{ + char *new_str = NULL; + char *addendum; + bool success; + va_list args; + + ARGPAR_ASSERT(str); + + va_start(args, fmt); + addendum = argpar_vasprintf(fmt, args); + va_end(args); + + if (!addendum) { + success = false; + goto end; + } + + new_str = argpar_asprintf("%s%s", *str ? *str : "", addendum); + if (!new_str) { + success = false; + goto end; + } + + free(*str); + *str = new_str; + + success = true; + +end: + free(addendum); + + return success; +} + +static +void destroy_item(struct argpar_item * const item) { if (!item) { goto end; } - if (item->type == BT_ARGPAR_ITEM_TYPE_OPT) { - struct bt_argpar_item_opt * const opt_item = (void *) item; + if (item->type == ARGPAR_ITEM_TYPE_OPT) { + struct argpar_item_opt * const opt_item = (void *) item; - g_free((void *) opt_item->arg); + free((void *) opt_item->arg); } - g_free(item); + free(item); end: return; } static -struct bt_argpar_item_opt *create_opt_item( - const struct bt_argpar_opt_descr * const descr, +bool push_item(struct argpar_item_array * const array, + struct argpar_item * const item) +{ + bool success; + + ARGPAR_ASSERT(array); + ARGPAR_ASSERT(item); + + if (array->n_items == array->n_alloc) { + unsigned int new_n_alloc = array->n_alloc * 2; + struct argpar_item **new_items; + + new_items = argpar_realloc(array->items, + struct argpar_item *, new_n_alloc); + if (!new_items) { + success = false; + goto end; + } + + array->n_alloc = new_n_alloc; + array->items = new_items; + } + + array->items[array->n_items] = item; + array->n_items++; + + success = true; + +end: + return success; +} + +static +void destroy_item_array(struct argpar_item_array * const array) +{ + if (array) { + unsigned int i; + + for (i = 0; i < array->n_items; i++) { + destroy_item(array->items[i]); + } + + free(array->items); + free(array); + } +} + +static +struct argpar_item_array *new_item_array(void) +{ + struct argpar_item_array *ret; + const int initial_size = 10; + + ret = argpar_zalloc(struct argpar_item_array); + if (!ret) { + goto end; + } + + ret->items = argpar_calloc(struct argpar_item *, initial_size); + if (!ret->items) { + goto error; + } + + ret->n_alloc = initial_size; + + goto end; + +error: + destroy_item_array(ret); + ret = NULL; + +end: + return ret; +} + +static +struct argpar_item_opt *create_opt_item( + const struct argpar_opt_descr * const descr, const char * const arg) { - struct bt_argpar_item_opt *opt_item = - g_new0(struct bt_argpar_item_opt, 1); + struct argpar_item_opt *opt_item = + argpar_zalloc(struct argpar_item_opt); if (!opt_item) { goto end; } - opt_item->base.type = BT_ARGPAR_ITEM_TYPE_OPT; + opt_item->base.type = ARGPAR_ITEM_TYPE_OPT; opt_item->descr = descr; if (arg) { - opt_item->arg = g_strdup(arg); + opt_item->arg = strdup(arg); if (!opt_item->arg) { goto error; } @@ -82,18 +241,18 @@ end: } static -struct bt_argpar_item_non_opt *create_non_opt_item(const char * const arg, +struct argpar_item_non_opt *create_non_opt_item(const char * const arg, const unsigned int orig_index, const unsigned int non_opt_index) { - struct bt_argpar_item_non_opt * const non_opt_item = - g_new0(struct bt_argpar_item_non_opt, 1); + struct argpar_item_non_opt * const non_opt_item = + argpar_zalloc(struct argpar_item_non_opt); if (!non_opt_item) { goto end; } - non_opt_item->base.type = BT_ARGPAR_ITEM_TYPE_NON_OPT; + non_opt_item->base.type = ARGPAR_ITEM_TYPE_NON_OPT; non_opt_item->arg = arg; non_opt_item->orig_index = orig_index; non_opt_item->non_opt_index = non_opt_index; @@ -103,11 +262,11 @@ end: } static -const struct bt_argpar_opt_descr *find_descr( - const struct bt_argpar_opt_descr * const descrs, +const struct argpar_opt_descr *find_descr( + const struct argpar_opt_descr * const descrs, const char short_name, const char * const long_name) { - const struct bt_argpar_opt_descr *descr; + const struct argpar_opt_descr *descr; for (descr = descrs; descr->short_name || descr->long_name; descr++) { if (short_name && descr->short_name && @@ -134,28 +293,28 @@ enum parse_orig_arg_opt_ret { static enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts, const char * const next_orig_arg, - const struct bt_argpar_opt_descr * const descrs, - struct bt_argpar_parse_ret * const parse_ret, + const struct argpar_opt_descr * const descrs, + struct argpar_parse_ret * const parse_ret, bool * const used_next_orig_arg) { enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; const char *short_opt_ch = short_opts; if (strlen(short_opts) == 0) { - g_string_append(parse_ret->error, "Invalid argument"); + argpar_string_append_printf(&parse_ret->error, "Invalid argument"); goto error; } while (*short_opt_ch) { const char *opt_arg = NULL; - const struct bt_argpar_opt_descr *descr; - struct bt_argpar_item_opt *opt_item; + const struct argpar_opt_descr *descr; + struct argpar_item_opt *opt_item; /* Find corresponding option descriptor */ descr = find_descr(descrs, *short_opt_ch, NULL); if (!descr) { ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT; - g_string_append_printf(parse_ret->error, + argpar_string_append_printf(&parse_ret->error, "Unknown option `-%c`", *short_opt_ch); goto error; } @@ -176,7 +335,7 @@ enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts, * expected. */ if (!opt_arg || (short_opt_ch[1] && strlen(opt_arg) == 0)) { - g_string_append_printf(parse_ret->error, + argpar_string_append_printf(&parse_ret->error, "Missing required argument for option `-%c`", *short_opt_ch); *used_next_orig_arg = false; @@ -190,7 +349,9 @@ enum parse_orig_arg_opt_ret parse_short_opts(const char * const short_opts, goto error; } - g_ptr_array_add(parse_ret->items, opt_item); + if (!push_item(parse_ret->items, &opt_item->base)) { + goto error; + } if (descr->with_arg) { /* Option has an argument: no more options */ @@ -215,14 +376,14 @@ end: static enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, const char * const next_orig_arg, - const struct bt_argpar_opt_descr * const descrs, - struct bt_argpar_parse_ret * const parse_ret, + const struct argpar_opt_descr * const descrs, + struct argpar_parse_ret * const parse_ret, bool * const used_next_orig_arg) { const size_t max_len = 127; enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; - const struct bt_argpar_opt_descr *descr; - struct bt_argpar_item_opt *opt_item; + const struct argpar_opt_descr *descr; + struct argpar_item_opt *opt_item; /* Option's argument, if any */ const char *opt_arg = NULL; @@ -237,7 +398,8 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, const char *long_opt_name = long_opt_arg; if (strlen(long_opt_arg) == 0) { - g_string_append(parse_ret->error, "Invalid argument"); + argpar_string_append_printf(&parse_ret->error, + "Invalid argument"); goto error; } @@ -248,7 +410,7 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, /* Isolate the option name */ if (long_opt_name_size > max_len) { - g_string_append_printf(parse_ret->error, + argpar_string_append_printf(&parse_ret->error, "Invalid argument `--%s`", long_opt_arg); goto error; } @@ -261,7 +423,7 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, /* Find corresponding option descriptor */ descr = find_descr(descrs, '\0', long_opt_name); if (!descr) { - g_string_append_printf(parse_ret->error, + argpar_string_append_printf(&parse_ret->error, "Unknown option `--%s`", long_opt_name); ret = PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT; goto error; @@ -275,7 +437,7 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, } else { /* `--long-opt arg` style */ if (!next_orig_arg) { - g_string_append_printf(parse_ret->error, + argpar_string_append_printf(&parse_ret->error, "Missing required argument for option `--%s`", long_opt_name); goto error; @@ -292,7 +454,10 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, goto error; } - g_ptr_array_add(parse_ret->items, opt_item); + if (!push_item(parse_ret->items, &opt_item->base)) { + goto error; + } + goto end; error: @@ -307,13 +472,13 @@ end: static enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg, const char * const next_orig_arg, - const struct bt_argpar_opt_descr * const descrs, - struct bt_argpar_parse_ret * const parse_ret, + const struct argpar_opt_descr * const descrs, + struct argpar_parse_ret * const parse_ret, bool * const used_next_orig_arg) { enum parse_orig_arg_opt_ret ret = PARSE_ORIG_ARG_OPT_RET_OK; - BT_ASSERT(orig_arg[0] == '-'); + ARGPAR_ASSERT(orig_arg[0] == '-'); if (orig_arg[1] == '-') { /* Long option */ @@ -331,45 +496,41 @@ enum parse_orig_arg_opt_ret parse_orig_arg_opt(const char * const orig_arg, } static -void prepend_while_parsing_arg_to_error(GString * const error, +bool prepend_while_parsing_arg_to_error(char **error, const unsigned int i, const char * const arg) { - /* 🙁 There's no g_string_prepend_printf()! */ - GString * const tmp_str = g_string_new(NULL); + char *new_error; + bool success; - BT_ASSERT(error); - BT_ASSERT(arg); + ARGPAR_ASSERT(error); + ARGPAR_ASSERT(*error); - if (!tmp_str) { + new_error = argpar_asprintf("While parsing argument #%u (`%s`): %s", + i + 1, arg, *error); + if (!new_error) { + success = false; goto end; } - g_string_append_printf(tmp_str, "While parsing argument #%u (`%s`): %s", - i + 1, arg, error->str); - g_string_assign(error, tmp_str->str); - g_string_free(tmp_str, TRUE); + free(*error); + *error = new_error; + success = true; end: - return; + return success; } -BT_HIDDEN -struct bt_argpar_parse_ret bt_argpar_parse(unsigned int argc, +ARGPAR_HIDDEN +struct argpar_parse_ret argpar_parse(unsigned int argc, const char * const *argv, - const struct bt_argpar_opt_descr * const descrs, + const struct argpar_opt_descr * const descrs, bool fail_on_unknown_opt) { - struct bt_argpar_parse_ret parse_ret = { 0 }; + struct argpar_parse_ret parse_ret = { 0 }; unsigned int i; unsigned int non_opt_index = 0; - parse_ret.error = g_string_new(NULL); - if (!parse_ret.error) { - goto error; - } - - parse_ret.items = g_ptr_array_new_with_free_func( - (GDestroyNotify) destroy_item); + parse_ret.items = new_item_array(); if (!parse_ret.items) { goto error; } @@ -383,7 +544,7 @@ struct bt_argpar_parse_ret bt_argpar_parse(unsigned int argc, if (orig_arg[0] != '-') { /* Non-option argument */ - struct bt_argpar_item_non_opt *non_opt_item = + struct argpar_item_non_opt *non_opt_item = create_non_opt_item(orig_arg, i, non_opt_index); if (!non_opt_item) { @@ -391,7 +552,11 @@ struct bt_argpar_parse_ret bt_argpar_parse(unsigned int argc, } non_opt_index++; - g_ptr_array_add(parse_ret.items, non_opt_item); + + if (!push_item(parse_ret.items, &non_opt_item->base)) { + goto error; + } + continue; } @@ -402,11 +567,11 @@ struct bt_argpar_parse_ret bt_argpar_parse(unsigned int argc, case PARSE_ORIG_ARG_OPT_RET_OK: break; case PARSE_ORIG_ARG_OPT_RET_ERROR_UNKNOWN_OPT: - BT_ASSERT(!used_next_orig_arg); + ARGPAR_ASSERT(!used_next_orig_arg); if (fail_on_unknown_opt) { prepend_while_parsing_arg_to_error( - parse_ret.error, i, orig_arg); + &parse_ret.error, i, orig_arg); goto error; } @@ -416,15 +581,15 @@ struct bt_argpar_parse_ret bt_argpar_parse(unsigned int argc, * unknown option. */ parse_ret.ingested_orig_args = i; - g_string_free(parse_ret.error, TRUE); + free(parse_ret.error); parse_ret.error = NULL; goto end; case PARSE_ORIG_ARG_OPT_RET_ERROR: prepend_while_parsing_arg_to_error( - parse_ret.error, i, orig_arg); + &parse_ret.error, i, orig_arg); goto error; default: - bt_common_abort(); + abort(); } if (used_next_orig_arg) { @@ -433,33 +598,27 @@ struct bt_argpar_parse_ret bt_argpar_parse(unsigned int argc, } parse_ret.ingested_orig_args = argc; - g_string_free(parse_ret.error, TRUE); + free(parse_ret.error); parse_ret.error = NULL; goto end; error: - if (parse_ret.items) { - /* That's how we indicate that an error occured */ - g_ptr_array_free(parse_ret.items, TRUE); - parse_ret.items = NULL; - } + /* That's how we indicate that an error occured */ + destroy_item_array(parse_ret.items); + parse_ret.items = NULL; end: return parse_ret; } -BT_HIDDEN -void bt_argpar_parse_ret_fini(struct bt_argpar_parse_ret *ret) +ARGPAR_HIDDEN +void argpar_parse_ret_fini(struct argpar_parse_ret *ret) { - BT_ASSERT(ret); + ARGPAR_ASSERT(ret); - if (ret->items) { - g_ptr_array_free(ret->items, TRUE); - ret->items = NULL; - } + destroy_item_array(ret->items); + ret->items = NULL; - if (ret->error) { - g_string_free(ret->error, TRUE); - ret->error = NULL; - } + free(ret->error); + ret->error = NULL; } diff --git a/src/argpar/argpar.h b/src/argpar/argpar.h index 85a663dc..7a50f453 100644 --- a/src/argpar/argpar.h +++ b/src/argpar/argpar.h @@ -23,16 +23,26 @@ * SOFTWARE. */ -#include #include -#include "common/macros.h" - /* Sentinel for an option descriptor array */ -#define BT_ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false } +#define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false } + +/* + * ARGPAR_HIDDEN: if argpar is used in some shared library, we don't want them + * to be exported by that library, so mark them as "hidden". + * + * On Windows, symbols are local unless explicitly exported, + * see https://gcc.gnu.org/wiki/Visibility + */ +#if defined(_WIN32) || defined(__CYGWIN__) +#define ARGPAR_HIDDEN +#else +#define ARGPAR_HIDDEN __attribute__((visibility("hidden"))) +#endif /* Option descriptor */ -struct bt_argpar_opt_descr { +struct argpar_opt_descr { /* Numeric ID for this option */ const int id; @@ -47,33 +57,33 @@ struct bt_argpar_opt_descr { }; /* Item type */ -enum bt_argpar_item_type { +enum argpar_item_type { /* Option */ - BT_ARGPAR_ITEM_TYPE_OPT, + ARGPAR_ITEM_TYPE_OPT, /* Non-option */ - BT_ARGPAR_ITEM_TYPE_NON_OPT, + ARGPAR_ITEM_TYPE_NON_OPT, }; /* Base item */ -struct bt_argpar_item { - enum bt_argpar_item_type type; +struct argpar_item { + enum argpar_item_type type; }; /* Option item */ -struct bt_argpar_item_opt { - struct bt_argpar_item base; +struct argpar_item_opt { + struct argpar_item base; /* Corresponding descriptor */ - const struct bt_argpar_opt_descr *descr; + const struct argpar_opt_descr *descr; /* Argument, or `NULL` if none */ const char *arg; }; /* Non-option item */ -struct bt_argpar_item_non_opt { - struct bt_argpar_item base; +struct argpar_item_non_opt { + struct argpar_item base; /* * Complete argument, pointing to one of the entries of the @@ -88,13 +98,24 @@ struct bt_argpar_item_non_opt { unsigned int non_opt_index; }; -/* What is returned by bt_argpar_parse() */ -struct bt_argpar_parse_ret { - /* Array of `struct bt_argpar_item *`, or `NULL` on error */ - GPtrArray *items; +struct argpar_item_array { + /* Array of `struct argpar_item *`, or `NULL` on error */ + struct argpar_item **items; + + /* Number of used slots in `items`. */ + unsigned int n_items; + + /* Number of allocated slots in `items`. */ + unsigned int n_alloc; +}; + +/* What is returned by argpar_parse() */ +struct argpar_parse_ret { + /* Array of `struct argpar_item *`, or `NULL` on error */ + struct argpar_item_array *items; /* Error string, or `NULL` if none */ - GString *error; + char *error; /* Number of original arguments (`argv`) ingested */ unsigned int ingested_orig_args; @@ -102,7 +123,7 @@ struct bt_argpar_parse_ret { /* * Parses the arguments `argv` of which the count is `argc` using the - * sentinel-terminated (use `BT_ARGPAR_OPT_DESCR_SENTINEL`) option + * sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`) option * descriptor array `descrs`. * * This function considers ALL the elements of `argv`, including the @@ -142,9 +163,9 @@ struct bt_argpar_parse_ret { * contains one entry for each instance). * * On success, this function returns an array of items - * (`struct bt_argpar_item *`). Each item is to be casted to the - * appropriate type (`struct bt_argpar_item_opt *` or - * `struct bt_argpar_item_non_opt *`) depending on its type. + * (`struct argpar_item *`). Each item is to be casted to the + * appropriate type (`struct argpar_item_opt *` or + * `struct argpar_item_non_opt *`) depending on its type. * * The returned array contains the items in the same order that the * arguments were parsed, including non-option arguments. This means, @@ -192,21 +213,21 @@ struct bt_argpar_parse_ret { * the `error` string member contains details about the error. * * You can finalize the returned structure with - * bt_argpar_parse_ret_fini(). + * argpar_parse_ret_fini(). */ -BT_HIDDEN -struct bt_argpar_parse_ret bt_argpar_parse(unsigned int argc, +ARGPAR_HIDDEN +struct argpar_parse_ret argpar_parse(unsigned int argc, const char * const *argv, - const struct bt_argpar_opt_descr *descrs, + const struct argpar_opt_descr *descrs, bool fail_on_unknown_opt); /* - * Finalizes what is returned by bt_argpar_parse(). + * Finalizes what is returned by argpar_parse(). * - * It is safe to call bt_argpar_parse() multiple times with the same + * It is safe to call argpar_parse() multiple times with the same * structure. */ -BT_HIDDEN -void bt_argpar_parse_ret_fini(struct bt_argpar_parse_ret *ret); +ARGPAR_HIDDEN +void argpar_parse_ret_fini(struct argpar_parse_ret *ret); #endif /* BABELTRACE_ARGPAR_H */ diff --git a/src/cli/Makefile.am b/src/cli/Makefile.am index fe4a8e26..55bad125 100644 --- a/src/cli/Makefile.am +++ b/src/cli/Makefile.am @@ -52,7 +52,7 @@ babeltrace2_bin_LDFLAGS = $(LD_NO_AS_NEEDED) # not discard the plugins since the CLI does not use their symbols # directly). babeltrace2_bin_LDADD = \ - $(top_builddir)/src/argpar/libbabeltrace2-argpar.la \ + $(top_builddir)/src/argpar/libargpar.la \ $(top_builddir)/src/autodisc/libbabeltrace2-autodisc.la \ $(top_builddir)/src/param-parse/libbabeltrace2-param-parse.la \ $(top_builddir)/src/string-format/libbabeltrace2-string-format.la \ diff --git a/src/cli/babeltrace2-cfg-cli-args.c b/src/cli/babeltrace2-cfg-cli-args.c index f0c64a3f..f80a38b5 100644 --- a/src/cli/babeltrace2-cfg-cli-args.c +++ b/src/cli/babeltrace2-cfg-cli-args.c @@ -1276,21 +1276,21 @@ void print_expected_params_format(FILE *fp) static bool help_option_is_specified( - const struct bt_argpar_parse_ret *argpar_parse_ret) + const struct argpar_parse_ret *argpar_parse_ret) { int i; bool specified = false; - for (i = 0; i < argpar_parse_ret->items->len; i++) { - struct bt_argpar_item *argpar_item = - g_ptr_array_index(argpar_parse_ret->items, i); - struct bt_argpar_item_opt *argpar_item_opt; + for (i = 0; i < argpar_parse_ret->items->n_items; i++) { + struct argpar_item *argpar_item = + argpar_parse_ret->items->items[i]; + struct argpar_item_opt *argpar_item_opt; - if (argpar_item->type != BT_ARGPAR_ITEM_TYPE_OPT) { + if (argpar_item->type != ARGPAR_ITEM_TYPE_OPT) { continue; } - argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item; + argpar_item_opt = (struct argpar_item_opt *) argpar_item; if (argpar_item_opt->descr->id == OPT_HELP) { specified = true; break; @@ -1319,10 +1319,10 @@ void print_help_usage(FILE *fp) } static -const struct bt_argpar_opt_descr help_options[] = { +const struct argpar_opt_descr help_options[] = { /* id, short_name, long_name, with_arg */ { OPT_HELP, 'h', "help", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; /* @@ -1338,8 +1338,8 @@ struct bt_config *bt_config_help_from_args(int argc, const char *argv[], { struct bt_config *cfg = NULL; char *plugin_name = NULL, *comp_cls_name = NULL; - struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; - struct bt_argpar_item_non_opt *non_opt; + struct argpar_parse_ret argpar_parse_ret = { 0 }; + struct argpar_item_non_opt *non_opt; GString *substring = NULL; size_t end_pos; @@ -1350,11 +1350,11 @@ struct bt_config *bt_config_help_from_args(int argc, const char *argv[], } /* Parse options */ - argpar_parse_ret = bt_argpar_parse(argc, argv, help_options, true); + argpar_parse_ret = argpar_parse(argc, argv, help_options, true); if (argpar_parse_ret.error) { BT_CLI_LOGE_APPEND_CAUSE( "While parsing `help` command's command-line arguments: %s", - argpar_parse_ret.error->str); + argpar_parse_ret.error); goto error; } @@ -1365,24 +1365,24 @@ struct bt_config *bt_config_help_from_args(int argc, const char *argv[], goto end; } - if (argpar_parse_ret.items->len == 0) { + if (argpar_parse_ret.items->n_items == 0) { BT_CLI_LOGE_APPEND_CAUSE( "Missing plugin name or component class descriptor."); goto error; - } else if (argpar_parse_ret.items->len > 1) { + } else if (argpar_parse_ret.items->n_items > 1) { /* * At this point we know there are least two non-option * arguments because we don't reach here with `--help`, * the only option. */ - non_opt = argpar_parse_ret.items->pdata[1]; + non_opt = (struct argpar_item_non_opt *) argpar_parse_ret.items->items[1]; BT_CLI_LOGE_APPEND_CAUSE( "Extraneous command-line argument specified to `help` command: `%s`.", non_opt->arg); goto error; } - non_opt = argpar_parse_ret.items->pdata[0]; + non_opt = (struct argpar_item_non_opt *) argpar_parse_ret.items->items[0]; /* Look for unescaped dots in the argument. */ substring = bt_common_string_until(non_opt->arg, ".\\", ".", &end_pos); @@ -1429,7 +1429,7 @@ end: g_string_free(substring, TRUE); } - bt_argpar_parse_ret_fini(&argpar_parse_ret); + argpar_parse_ret_fini(&argpar_parse_ret); return cfg; } @@ -1452,11 +1452,11 @@ void print_query_usage(FILE *fp) } static -const struct bt_argpar_opt_descr query_options[] = { +const struct argpar_opt_descr query_options[] = { /* id, short_name, long_name, with_arg */ { OPT_HELP, 'h', "help", false }, { OPT_PARAMS, 'p', "params", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; /* @@ -1475,7 +1475,7 @@ struct bt_config *bt_config_query_from_args(int argc, const char *argv[], const char *component_class_spec = NULL; const char *query_object = NULL; GString *error_str = NULL; - struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; + struct argpar_parse_ret argpar_parse_ret = { 0 }; bt_value *params = bt_value_map_create(); if (!params) { @@ -1496,11 +1496,11 @@ struct bt_config *bt_config_query_from_args(int argc, const char *argv[], } /* Parse options */ - argpar_parse_ret = bt_argpar_parse(argc, argv, query_options, true); + argpar_parse_ret = argpar_parse(argc, argv, query_options, true); if (argpar_parse_ret.error) { BT_CLI_LOGE_APPEND_CAUSE( "While parsing `query` command's command-line arguments: %s", - argpar_parse_ret.error->str); + argpar_parse_ret.error); goto error; } @@ -1511,13 +1511,13 @@ struct bt_config *bt_config_query_from_args(int argc, const char *argv[], goto end; } - for (i = 0; i < argpar_parse_ret.items->len; i++) { - struct bt_argpar_item *argpar_item = - g_ptr_array_index(argpar_parse_ret.items, i); + for (i = 0; i < argpar_parse_ret.items->n_items; i++) { + struct argpar_item *argpar_item = + argpar_parse_ret.items->items[i]; - if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) { - struct bt_argpar_item_opt *argpar_item_opt = - (struct bt_argpar_item_opt *) argpar_item; + if (argpar_item->type == ARGPAR_ITEM_TYPE_OPT) { + struct argpar_item_opt *argpar_item_opt = + (struct argpar_item_opt *) argpar_item; const char *arg = argpar_item_opt->arg; switch (argpar_item_opt->descr->id) { @@ -1546,8 +1546,8 @@ struct bt_config *bt_config_query_from_args(int argc, const char *argv[], goto error; } } else { - struct bt_argpar_item_non_opt *argpar_item_non_opt - = (struct bt_argpar_item_non_opt *) argpar_item; + struct argpar_item_non_opt *argpar_item_non_opt + = (struct argpar_item_non_opt *) argpar_item; /* * We need exactly two non-option arguments @@ -1598,7 +1598,7 @@ error: BT_OBJECT_PUT_REF_AND_RESET(cfg); end: - bt_argpar_parse_ret_fini(&argpar_parse_ret); + argpar_parse_ret_fini(&argpar_parse_ret); if (error_str) { g_string_free(error_str, TRUE); @@ -1626,10 +1626,10 @@ void print_list_plugins_usage(FILE *fp) } static -const struct bt_argpar_opt_descr list_plugins_options[] = { +const struct argpar_opt_descr list_plugins_options[] = { /* id, short_name, long_name, with_arg */ { OPT_HELP, 'h', "help", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; /* @@ -1643,7 +1643,7 @@ struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[], int *retcode, const bt_value *plugin_paths) { struct bt_config *cfg = NULL; - struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; + struct argpar_parse_ret argpar_parse_ret = { 0 }; *retcode = 0; cfg = bt_config_list_plugins_create(plugin_paths); @@ -1652,11 +1652,11 @@ struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[], } /* Parse options */ - argpar_parse_ret = bt_argpar_parse(argc, argv, list_plugins_options, true); + argpar_parse_ret = argpar_parse(argc, argv, list_plugins_options, true); if (argpar_parse_ret.error) { BT_CLI_LOGE_APPEND_CAUSE( "While parsing `list-plugins` command's command-line arguments: %s", - argpar_parse_ret.error->str); + argpar_parse_ret.error); goto error; } @@ -1667,14 +1667,14 @@ struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[], goto end; } - if (argpar_parse_ret.items->len > 0) { + if (argpar_parse_ret.items->n_items > 0) { /* * At this point we know there's at least one non-option * argument because we don't reach here with `--help`, * the only option. */ - struct bt_argpar_item_non_opt *non_opt = - argpar_parse_ret.items->pdata[0]; + struct argpar_item_non_opt *non_opt = + (struct argpar_item_non_opt *) argpar_parse_ret.items->items[0]; BT_CLI_LOGE_APPEND_CAUSE( "Extraneous command-line argument specified to `list-plugins` command: `%s`.", @@ -1689,7 +1689,7 @@ error: BT_OBJECT_PUT_REF_AND_RESET(cfg); end: - bt_argpar_parse_ret_fini(&argpar_parse_ret); + argpar_parse_ret_fini(&argpar_parse_ret); return cfg; } @@ -1787,10 +1787,10 @@ struct bt_config *bt_config_run_from_args(int argc, const char *argv[], long retry_duration = -1; bt_value_map_extend_status extend_status; GString *error_str = NULL; - struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; + struct argpar_parse_ret argpar_parse_ret = { 0 }; int i; - static const struct bt_argpar_opt_descr run_options[] = { + static const struct argpar_opt_descr run_options[] = { { OPT_BASE_PARAMS, 'b', "base-params", true }, { OPT_COMPONENT, 'c', "component", true }, { OPT_CONNECT, 'x', "connect", true }, @@ -1799,7 +1799,7 @@ struct bt_config *bt_config_run_from_args(int argc, const char *argv[], { OPT_PARAMS, 'p', "params", true }, { OPT_RESET_BASE_PARAMS, 'r', "reset-base-params", false }, { OPT_RETRY_DURATION, '\0', "retry-duration", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; *retcode = 0; @@ -1841,11 +1841,11 @@ struct bt_config *bt_config_run_from_args(int argc, const char *argv[], } /* Parse options */ - argpar_parse_ret = bt_argpar_parse(argc, argv, run_options, true); + argpar_parse_ret = argpar_parse(argc, argv, run_options, true); if (argpar_parse_ret.error) { BT_CLI_LOGE_APPEND_CAUSE( "While parsing `run` command's command-line arguments: %s", - argpar_parse_ret.error->str); + argpar_parse_ret.error); goto error; } @@ -1856,23 +1856,23 @@ struct bt_config *bt_config_run_from_args(int argc, const char *argv[], goto end; } - for (i = 0; i < argpar_parse_ret.items->len; i++) { - struct bt_argpar_item *argpar_item = - g_ptr_array_index(argpar_parse_ret.items, i); - struct bt_argpar_item_opt *argpar_item_opt; + for (i = 0; i < argpar_parse_ret.items->n_items; i++) { + struct argpar_item *argpar_item = + argpar_parse_ret.items->items[i]; + struct argpar_item_opt *argpar_item_opt; const char *arg; /* This command does not accept non-option arguments.*/ - if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) { - struct bt_argpar_item_non_opt *argpar_nonopt_item = - (struct bt_argpar_item_non_opt *) argpar_item; + if (argpar_item->type == ARGPAR_ITEM_TYPE_NON_OPT) { + struct argpar_item_non_opt *argpar_nonopt_item = + (struct argpar_item_non_opt *) argpar_item; BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`", argpar_nonopt_item->arg); goto error; } - argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item; + argpar_item_opt = (struct argpar_item_opt *) argpar_item; arg = argpar_item_opt->arg; switch (argpar_item_opt->descr->id) { @@ -2052,7 +2052,7 @@ end: g_string_free(error_str, TRUE); } - bt_argpar_parse_ret_fini(&argpar_parse_ret); + argpar_parse_ret_fini(&argpar_parse_ret); BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp); BT_VALUE_PUT_REF_AND_RESET(cur_base_params); BT_VALUE_PUT_REF_AND_RESET(instance_names); @@ -2219,7 +2219,7 @@ void print_convert_usage(FILE *fp) } static -const struct bt_argpar_opt_descr convert_options[] = { +const struct argpar_opt_descr convert_options[] = { /* id, short_name, long_name, with_arg */ { OPT_BEGIN, 'b', "begin", true }, { OPT_CLOCK_CYCLES, '\0', "clock-cycles", false }, @@ -2255,7 +2255,7 @@ const struct bt_argpar_opt_descr convert_options[] = { { OPT_STREAM_INTERSECTION, '\0', "stream-intersection", false }, { OPT_TIMERANGE, '\0', "timerange", true }, { OPT_VERBOSE, 'v', "verbose", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; static @@ -3187,7 +3187,7 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], char *output = NULL; struct auto_source_discovery auto_disc = { NULL }; GString *auto_disc_comp_name = NULL; - struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; + struct argpar_parse_ret argpar_parse_ret = { 0 }; GString *name_gstr = NULL; GString *component_arg_for_run = NULL; bt_value *live_inputs_array_val = NULL; @@ -3310,11 +3310,11 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], * arguments if needed to automatically name unnamed component * instances. */ - argpar_parse_ret = bt_argpar_parse(argc, argv, convert_options, true); + argpar_parse_ret = argpar_parse(argc, argv, convert_options, true); if (argpar_parse_ret.error) { BT_CLI_LOGE_APPEND_CAUSE( "While parsing `convert` command's command-line arguments: %s", - argpar_parse_ret.error->str); + argpar_parse_ret.error); goto error; } @@ -3325,17 +3325,17 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], goto end; } - for (i = 0; i < argpar_parse_ret.items->len; i++) { - struct bt_argpar_item *argpar_item = - g_ptr_array_index(argpar_parse_ret.items, i); - struct bt_argpar_item_opt *argpar_item_opt; + for (i = 0; i < argpar_parse_ret.items->n_items; i++) { + struct argpar_item *argpar_item = + argpar_parse_ret.items->items[i]; + struct argpar_item_opt *argpar_item_opt; char *name = NULL; char *plugin_name = NULL; char *comp_cls_name = NULL; const char *arg; - if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) { - argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item; + if (argpar_item->type == ARGPAR_ITEM_TYPE_OPT) { + argpar_item_opt = (struct argpar_item_opt *) argpar_item; arg = argpar_item_opt->arg; switch (argpar_item_opt->descr->id) { @@ -3556,13 +3556,13 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], argpar_item_opt->descr->id); goto error; } - } else if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) { - struct bt_argpar_item_non_opt *argpar_item_non_opt; + } else if (argpar_item->type == ARGPAR_ITEM_TYPE_NON_OPT) { + struct argpar_item_non_opt *argpar_item_non_opt; bt_value_array_append_element_status append_status; current_item_type = CONVERT_CURRENT_ITEM_TYPE_NON_OPT; - argpar_item_non_opt = (struct bt_argpar_item_non_opt *) argpar_item; + argpar_item_non_opt = (struct argpar_item_non_opt *) argpar_item; append_status = bt_value_array_append_string_element(non_opts, argpar_item_non_opt->arg); @@ -3593,17 +3593,17 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], * arguments into implicit component instances for the run * command. */ - for (i = 0; i < argpar_parse_ret.items->len; i++) { - struct bt_argpar_item *argpar_item = - g_ptr_array_index(argpar_parse_ret.items, i); - struct bt_argpar_item_opt *argpar_item_opt; + for (i = 0; i < argpar_parse_ret.items->n_items; i++) { + struct argpar_item *argpar_item = + argpar_parse_ret.items->items[i]; + struct argpar_item_opt *argpar_item_opt; const char *arg; - if (argpar_item->type != BT_ARGPAR_ITEM_TYPE_OPT) { + if (argpar_item->type != ARGPAR_ITEM_TYPE_OPT) { continue; } - argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item; + argpar_item_opt = (struct argpar_item_opt *) argpar_item; arg = argpar_item_opt->arg; switch (argpar_item_opt->descr->id) { @@ -4384,7 +4384,7 @@ error: BT_OBJECT_PUT_REF_AND_RESET(cfg); end: - bt_argpar_parse_ret_fini(&argpar_parse_ret); + argpar_parse_ret_fini(&argpar_parse_ret); free(output); @@ -4476,11 +4476,11 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], const char **command_argv = NULL; const char *command_name = NULL; int default_log_level = -1; - struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; + struct argpar_parse_ret argpar_parse_ret = { 0 }; bt_value *plugin_paths = NULL; /* Top-level option descriptions. */ - static const struct bt_argpar_opt_descr descrs[] = { + static const struct argpar_opt_descr descrs[] = { { OPT_DEBUG, 'd', "debug", false }, { OPT_HELP, 'h', "help", false }, { OPT_LOG_LEVEL, 'l', "log-level", true }, @@ -4489,7 +4489,7 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false }, { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false }, { OPT_PLUGIN_PATH, '\0', "plugin-path", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; enum command_type { @@ -4537,24 +4537,24 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], /* Skip first argument, the name of the program. */ top_level_argc = argc - 1; top_level_argv = argv + 1; - argpar_parse_ret = bt_argpar_parse(top_level_argc, top_level_argv, + argpar_parse_ret = argpar_parse(top_level_argc, top_level_argv, descrs, false); if (argpar_parse_ret.error) { BT_CLI_LOGE_APPEND_CAUSE( "While parsing command-line arguments: %s", - argpar_parse_ret.error->str); + argpar_parse_ret.error); goto error; } - for (i = 0; i < argpar_parse_ret.items->len; i++) { - struct bt_argpar_item *item; + for (i = 0; i < argpar_parse_ret.items->n_items; i++) { + struct argpar_item *item; - item = g_ptr_array_index(argpar_parse_ret.items, i); + item = argpar_parse_ret.items->items[i]; - if (item->type == BT_ARGPAR_ITEM_TYPE_OPT) { - struct bt_argpar_item_opt *item_opt = - (struct bt_argpar_item_opt *) item; + if (item->type == ARGPAR_ITEM_TYPE_OPT) { + struct argpar_item_opt *item_opt = + (struct argpar_item_opt *) item; switch (item_opt->descr->id) { case OPT_DEBUG: @@ -4599,9 +4599,9 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], print_gen_usage(stdout); goto end; } - } else if (item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) { - struct bt_argpar_item_non_opt *item_non_opt = - (struct bt_argpar_item_non_opt *) item; + } else if (item->type == ARGPAR_ITEM_TYPE_NON_OPT) { + struct argpar_item_non_opt *item_non_opt = + (struct argpar_item_non_opt *) item; /* * First unknown argument: is it a known command * name? @@ -4733,7 +4733,7 @@ error: *retcode = 1; end: - bt_argpar_parse_ret_fini(&argpar_parse_ret); + argpar_parse_ret_fini(&argpar_parse_ret); bt_value_put_ref(plugin_paths); return config; } diff --git a/tests/Makefile.am b/tests/Makefile.am index c36feb87..c5f62e11 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,7 +4,6 @@ SUBDIRS = \ bitfield \ ctf-writer \ plugins \ - argpar \ param-validation # Directories added to EXTRA_DIST will be recursively copied to the distribution. @@ -65,9 +64,6 @@ if ENABLE_PYTHON_BINDINGS TESTS_BINDINGS += bindings/python/bt2/test_python_bt2 endif -TESTS_ARGPAR = \ - argpar/test_argpar - TESTS_CLI = \ cli/convert/test_convert_args \ cli/test_help \ @@ -168,7 +164,6 @@ LOG_DRIVER = env AM_TAP_AWK='$(AWK)' \ $(SHELL) $(srcdir)/utils/tap-driver.sh TESTS_NO_BITFIELD = \ - $(TESTS_ARGPAR) \ $(TESTS_BINDINGS) \ $(TESTS_CLI) \ $(TESTS_CTF_WRITER) \ @@ -184,7 +179,6 @@ check-$(1): $(MAKE) $(AM_MAKEFLAGS) TESTS="$2" check endef -$(eval $(call check_target,argpar,$(TESTS_ARGPAR))) $(eval $(call check_target,bindings,$(TESTS_BINDINGS))) $(eval $(call check_target,bitfield,$(TESTS_BITFIELD))) $(eval $(call check_target,cli,$(TESTS_CLI))) diff --git a/tests/argpar/Makefile.am b/tests/argpar/Makefile.am deleted file mode 100644 index 2b635216..00000000 --- a/tests/argpar/Makefile.am +++ /dev/null @@ -1,9 +0,0 @@ -AM_CPPFLAGS += -I$(top_srcdir)/tests/utils - -noinst_PROGRAMS = test_argpar -test_argpar_SOURCES = test_argpar.c -test_argpar_LDADD = \ - $(top_builddir)/tests/utils/tap/libtap.la \ - $(top_builddir)/src/common/libbabeltrace2-common.la \ - $(top_builddir)/src/logging/libbabeltrace2-logging.la \ - $(top_builddir)/src/argpar/libbabeltrace2-argpar.la diff --git a/tests/argpar/test_argpar.c b/tests/argpar/test_argpar.c deleted file mode 100644 index fa915d87..00000000 --- a/tests/argpar/test_argpar.c +++ /dev/null @@ -1,641 +0,0 @@ -/* - * Copyright (c) 2019 Philippe Proulx - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; under version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include -#include -#include - -#include "tap/tap.h" -#include "common/assert.h" - -#include "argpar/argpar.h" - -/* - * Tests that the command line `cmdline`, with non-quoted - * space-delimited arguments, once parsed given the option descriptors - * `descrs` and the option `fail_on_unknown_opt`, succeeds and gives the - * expected command line `expected_cmd_line` and number of ingested - * original arguments `expected_ingested_orig_args`. - * - * The resulting command-line is built from the resulting arguments, - * space-delimiting each argument, preferring the `--long-opt=arg` style - * over the `-s arg` style, and using the `arg` form for non-option - * arguments where `A` is the original argument index and `B` is the - * non-option argument index. - */ -static -void test_succeed(const char *cmdline, - const char *expected_cmd_line, - const struct bt_argpar_opt_descr *descrs, - unsigned int expected_ingested_orig_args) -{ - struct bt_argpar_parse_ret parse_ret; - GString *res_str = g_string_new(NULL); - gchar **argv = g_strsplit(cmdline, " ", 0); - unsigned int i; - - BT_ASSERT(argv); - BT_ASSERT(res_str); - parse_ret = bt_argpar_parse(g_strv_length(argv), - (const char * const *) argv, descrs, false); - ok(parse_ret.items, - "bt_argpar_parse() succeeds for command line `%s`", cmdline); - ok(!parse_ret.error, - "bt_argpar_parse() does not write an error for command line `%s`", cmdline); - ok(parse_ret.ingested_orig_args == expected_ingested_orig_args, - "bt_argpar_parse() returns the correct number of ingested " - "original arguments for command line `%s`", cmdline); - if (parse_ret.ingested_orig_args != expected_ingested_orig_args) { - diag("Expected: %u Got: %u", expected_ingested_orig_args, - parse_ret.ingested_orig_args); - } - - if (!parse_ret.items) { - fail("bt_argpar_parse() returns the expected parsed arguments " - "for command line `%s`", cmdline); - goto end; - } - - for (i = 0; i < parse_ret.items->len; i++) { - const struct bt_argpar_item *arg = parse_ret.items->pdata[i]; - - switch (arg->type) { - case BT_ARGPAR_ITEM_TYPE_OPT: - { - const struct bt_argpar_item_opt *arg_opt = - (const void *) arg; - - if (arg_opt->descr->long_name) { - g_string_append_printf(res_str, "--%s", - arg_opt->descr->long_name); - - if (arg_opt->arg) { - g_string_append_printf(res_str, "=%s", - arg_opt->arg); - } - - g_string_append_c(res_str, ' '); - } else if (arg_opt->descr->short_name) { - g_string_append_printf(res_str, "-%c", - arg_opt->descr->short_name); - - if (arg_opt->arg) { - g_string_append_printf(res_str, " %s", - arg_opt->arg); - } - - g_string_append_c(res_str, ' '); - } - - break; - } - case BT_ARGPAR_ITEM_TYPE_NON_OPT: - { - const struct bt_argpar_item_non_opt *arg_non_opt = - (const void *) arg; - - g_string_append_printf(res_str, "%s<%u,%u> ", - arg_non_opt->arg, arg_non_opt->orig_index, - arg_non_opt->non_opt_index); - break; - } - default: - abort(); - } - } - - if (res_str->len > 0) { - g_string_truncate(res_str, res_str->len - 1); - } - - ok(strcmp(expected_cmd_line, res_str->str) == 0, - "bt_argpar_parse() returns the expected parsed arguments " - "for command line `%s`", cmdline); - if (strcmp(expected_cmd_line, res_str->str) != 0) { - diag("Expected: `%s`", expected_cmd_line); - diag("Got: `%s`", res_str->str); - } - -end: - bt_argpar_parse_ret_fini(&parse_ret); - g_string_free(res_str, TRUE); - g_strfreev(argv); -} - -static -void succeed_tests(void) -{ - /* No arguments */ - { - const struct bt_argpar_opt_descr descrs[] = { - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "", - "", - descrs, 0); - } - - /* Single long option */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "salut", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--salut", - "--salut", - descrs, 1); - } - - /* Single short option */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'f', NULL, false }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-f", - "-f", - descrs, 1); - } - - /* Short and long option (aliases) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'f', "flaw", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-f --flaw", - "--flaw --flaw", - descrs, 2); - } - - /* Long option with argument (space form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "tooth", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--tooth 67", - "--tooth=67", - descrs, 2); - } - - /* Long option with argument (equal form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "polish", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--polish=brick", - "--polish=brick", - descrs, 1); - } - - /* Short option with argument (space form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-c chilly", - "-c chilly", - descrs, 2); - } - - /* Short option with argument (glued form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-cchilly", - "-c chilly", - descrs, 1); - } - - /* Short and long option (aliases) with argument (all forms) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'd', "dry", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--dry=rate -dthing --dry street --dry=shape", - "--dry=rate --dry=thing --dry=street --dry=shape", - descrs, 5); - } - - /* Many short options, last one with argument (glued form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'd', NULL, false }, - { 0, 'e', NULL, false }, - { 0, 'f', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-defmeow", - "-d -e -f meow", - descrs, 1); - } - - /* Many options */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'd', NULL, false }, - { 0, 'e', "east", true }, - { 0, '\0', "mind", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-d --mind -destart --mind --east cough -d --east=itch", - "-d --mind -d --east=start --mind --east=cough -d --east=itch", - descrs, 8); - } - - /* Single non-option argument */ - { - const struct bt_argpar_opt_descr descrs[] = { - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "kilojoule", - "kilojoule<0,0>", - descrs, 1); - } - - /* Two non-option arguments */ - { - const struct bt_argpar_opt_descr descrs[] = { - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "kilojoule mitaine", - "kilojoule<0,0> mitaine<1,1>", - descrs, 2); - } - - /* Single non-option argument mixed with options */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'd', NULL, false }, - { 0, '\0', "squeeze", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-d sprout yes --squeeze little bag -d", - "-d sprout<1,0> yes<2,1> --squeeze=little bag<5,2> -d", - descrs, 7); - } - - /* Unknown short option (space form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'd', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-d salut -e -d meow", - "-d salut", - descrs, 2); - } - - /* Unknown short option (glued form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'd', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-dsalut -e -d meow", - "-d salut", - descrs, 1); - } - - /* Unknown long option (space form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "sink", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--sink party --food --sink impulse", - "--sink=party", - descrs, 2); - } - - /* Unknown long option (equal form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "sink", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--sink=party --food --sink=impulse", - "--sink=party", - descrs, 1); - } - - /* Unknown option before non-option argument */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--thumb=party --food bateau --thumb waves", - "--thumb=party", - descrs, 1); - } - - /* Unknown option after non-option argument */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--thumb=party wound --food --thumb waves", - "--thumb=party wound<1,0>", - descrs, 2); - } - - /* Valid `---opt` */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "-fuel", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "---fuel=three", - "---fuel=three", - descrs, 1); - } - - /* Long option containing `=` in argument (equal form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "zebra", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--zebra=three=yes", - "--zebra=three=yes", - descrs, 1); - } - - /* Short option's argument starting with `-` (glued form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'z', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-z-will", - "-z -will", - descrs, 1); - } - - /* Short option's argument starting with `-` (space form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'z', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-z -will", - "-z -will", - descrs, 2); - } - - /* Long option's argument starting with `-` (space form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "janine", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--janine -sutto", - "--janine=-sutto", - descrs, 2); - } - - /* Long option's argument starting with `-` (equal form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "janine", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--janine=-sutto", - "--janine=-sutto", - descrs, 1); - } - - /* Long option's empty argument (equal form) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'f', NULL, false }, - { 0, '\0', "yeah", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-f --yeah= -f", - "-f --yeah= -f", - descrs, 3); - } -} - -/* - * Tests that the command line `cmdline`, with non-quoted - * space-delimited arguments, once parsed given the option descriptors - * `descrs`, fails and gives the expected error `expected_error`. - */ -static -void test_fail(const char *cmdline, const char *expected_error, - const struct bt_argpar_opt_descr *descrs) -{ - struct bt_argpar_parse_ret parse_ret; - gchar **argv = g_strsplit(cmdline, " ", 0); - - parse_ret = bt_argpar_parse(g_strv_length(argv), - (const char * const *) argv, descrs, true); - ok(!parse_ret.items, - "bt_argpar_parse() fails for command line `%s`", cmdline); - ok(parse_ret.error, - "bt_argpar_parse() writes an error string for command line `%s`", - cmdline); - if (parse_ret.items) { - fail("bt_argpar_parse() writes the expected error string"); - goto end; - } - - ok(strcmp(expected_error, parse_ret.error->str) == 0, - "bt_argpar_parse() writes the expected error string " - "for command line `%s`", cmdline); - if (strcmp(expected_error, parse_ret.error->str) != 0) { - diag("Expected: `%s`", expected_error); - diag("Got: `%s`", parse_ret.error->str); - } - -end: - bt_argpar_parse_ret_fini(&parse_ret); - g_strfreev(argv); -} - -static -void fail_tests(void) -{ - /* Unknown long option */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "--thumb=party --meow", - "While parsing argument #2 (`--meow`): Unknown option `--meow`", - descrs); - } - - /* Unknown short option */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "--thumb=party -x", - "While parsing argument #2 (`-x`): Unknown option `-x`", - descrs); - } - - /* Missing long option argument */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "--thumb", - "While parsing argument #1 (`--thumb`): Missing required argument for option `--thumb`", - descrs); - } - - /* Missing short option argument */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'k', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "-k", - "While parsing argument #1 (`-k`): Missing required argument for option `-k`", - descrs); - } - - /* Missing short option argument (multiple glued) */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'a', NULL, false }, - { 0, 'b', NULL, false }, - { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "-abc", - "While parsing argument #1 (`-abc`): Missing required argument for option `-c`", - descrs); - } - - /* Invalid `-` */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'a', NULL, false }, - { 0, 'b', NULL, false }, - { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "-ab - -c", - "While parsing argument #2 (`-`): Invalid argument", - descrs); - } - - /* Invalid `--` */ - { - const struct bt_argpar_opt_descr descrs[] = { - { 0, 'a', NULL, false }, - { 0, 'b', NULL, false }, - { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "-ab -- -c", - "While parsing argument #2 (`--`): Invalid argument", - descrs); - } -} - -int main(void) -{ - plan_tests(129); - succeed_tests(); - fail_tests(); - return exit_status(); -}