diff options
Diffstat (limited to 'tools/lint/cmd_clear.c')
-rw-r--r-- | tools/lint/cmd_clear.c | 139 |
1 files changed, 106 insertions, 33 deletions
diff --git a/tools/lint/cmd_clear.c b/tools/lint/cmd_clear.c index 5eed6ff..4a869af 100644 --- a/tools/lint/cmd_clear.c +++ b/tools/lint/cmd_clear.c @@ -2,9 +2,10 @@ * @file cmd_clear.c * @author Michal Vasko <mvasko@cesnet.cz> * @author Radek Krejci <rkrejci@cesnet.cz> + * @author Adam Piecek <piecek@cesnet.cz> * @brief 'clear' command of the libyang's yanglint tool. * - * Copyright (c) 2015-2020 CESNET, z.s.p.o. + * Copyright (c) 2015-2023 CESNET, z.s.p.o. * * This source code is licensed under BSD 3-Clause License (the "License"). * You may not use this file except in compliance with the License. @@ -24,6 +25,7 @@ #include "libyang.h" #include "common.h" +#include "yl_opt.h" void cmd_clear_help(void) @@ -32,68 +34,139 @@ cmd_clear_help(void) " Replace the current context with an empty one, searchpaths\n" " are not kept.\n\n" " -i, --make-implemented\n" - " Make the imported modules \"referenced\" from any loaded\n" - " module also implemented. If specified a second time, all the\n" - " modules are set implemented.\n" + " When loading a module into the context, the imported 'referenced'\n" + " modules will also be implemented. If specified a second time,\n" + " all the modules will be implemented.\n" " -y, --yang-library\n" " Load and implement internal \"ietf-yang-library\" YANG module.\n" " Note that this module includes definitions of mandatory state\n" - " data that can result in unexpected data validation errors.\n"); -#if 0 - " If <yang-library-data> path specified, load the modules\n" - " according to the provided yang library data.\n" -#endif + " data that can result in unexpected data validation errors.\n" + " -Y FILE, --yang-library-file=FILE\n" + " Parse FILE with \"ietf-yang-library\" data and use them to\n" + " create an exact YANG schema context. Searchpaths defined so far\n" + " are used, but then deleted.\n"); } -void -cmd_clear(struct ly_ctx **ctx, const char *cmdline) +int +cmd_clear_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) { - int argc = 0; - char **argv = NULL; + int rc = 0, argc = 0; int opt, opt_index; struct option options[] = { - {"make-implemented", no_argument, NULL, 'i'}, - {"yang-library", no_argument, NULL, 'y'}, + {"make-implemented", no_argument, NULL, 'i'}, + {"yang-library", no_argument, NULL, 'y'}, + {"yang-library-file", no_argument, NULL, 'Y'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; - uint16_t options_ctx = LY_CTX_NO_YANGLIBRARY; - struct ly_ctx *ctx_new; - if (parse_cmdline(cmdline, &argc, &argv)) { - goto cleanup; + yo->ctx_options = LY_CTX_NO_YANGLIBRARY; + + if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { + return rc; } - while ((opt = getopt_long(argc, argv, "iyh", options, &opt_index)) != -1) { + while ((opt = getopt_long(argc, yo->argv, commands[CMD_CLEAR].optstring, options, &opt_index)) != -1) { switch (opt) { case 'i': - if (options_ctx & LY_CTX_REF_IMPLEMENTED) { - options_ctx &= ~LY_CTX_REF_IMPLEMENTED; - options_ctx |= LY_CTX_ALL_IMPLEMENTED; + if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) { + yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED; + yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED; } else { - options_ctx |= LY_CTX_REF_IMPLEMENTED; + yo->ctx_options |= LY_CTX_REF_IMPLEMENTED; } break; case 'y': - options_ctx &= ~LY_CTX_NO_YANGLIBRARY; + yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY; + break; + case 'Y': + yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY; + yo->yang_lib_file = optarg; + if (!yo->yang_lib_file) { + YLMSG_E("Memory allocation error."); + return 1; + } break; case 'h': cmd_clear_help(); - goto cleanup; + return 1; default: - YLMSG_E("Unknown option.\n"); - goto cleanup; + YLMSG_E("Unknown option."); + return 1; + } + } + + *posv = &yo->argv[optind]; + *posc = argc - optind; + + return rc; +} + +int +cmd_clear_dep(struct yl_opt *yo, int posc) +{ + (void) yo; + + if (posc) { + YLMSG_E("No positional arguments are allowed."); + return 1; + } + + return 0; +} + +/** + * @brief Convert searchpaths into single string. + * + * @param[in] ctx Context with searchpaths. + * @param[out] searchpaths Collection of paths in the single string. Paths are delimited by colon ":" + * (on Windows, used semicolon ";" instead). + * @return LY_ERR value. + */ +static LY_ERR +searchpaths_to_str(const struct ly_ctx *ctx, char **searchpaths) +{ + uint32_t i; + int rc = 0; + const char * const *dirs = ly_ctx_get_searchdirs(ctx); + + for (i = 0; dirs[i]; ++i) { + rc = searchpath_strcat(searchpaths, dirs[i]); + if (!rc) { + break; } } - if (ly_ctx_new(NULL, options_ctx, &ctx_new)) { - YLMSG_W("Failed to create context.\n"); - goto cleanup; + return rc; +} + +int +cmd_clear_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) +{ + (void) posv; + struct ly_ctx *ctx_new = NULL; + + if (yo->yang_lib_file) { + if (searchpaths_to_str(*ctx, &yo->searchpaths)) { + YLMSG_E("Storing searchpaths failed."); + return 1; + } + if (ly_ctx_new_ylpath(yo->searchpaths, yo->yang_lib_file, LYD_UNKNOWN, yo->ctx_options, &ctx_new)) { + YLMSG_E("Unable to create libyang context with yang-library data."); + return 1; + } + } else { + if (ly_ctx_new(NULL, yo->ctx_options, &ctx_new)) { + YLMSG_W("Failed to create context."); + return 1; + } } + /* Global variables in commands are also deleted. */ + cmd_free(); + ly_ctx_destroy(*ctx); *ctx = ctx_new; -cleanup: - free_cmdline(argv); + return 0; } |