summaryrefslogtreecommitdiffstats
path: root/tools/lint/cmd_add.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 04:23:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 04:23:18 +0000
commitb90161ccd3b318f3314a23cb10c387651ad35831 (patch)
treea47dc087160299ce02d728cbf031d84af6281537 /tools/lint/cmd_add.c
parentAdding upstream version 2.1.30. (diff)
downloadlibyang2-b90161ccd3b318f3314a23cb10c387651ad35831.tar.xz
libyang2-b90161ccd3b318f3314a23cb10c387651ad35831.zip
Adding upstream version 2.1.148.upstream/2.1.148upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/lint/cmd_add.c')
-rw-r--r--tools/lint/cmd_add.c186
1 files changed, 110 insertions, 76 deletions
diff --git a/tools/lint/cmd_add.c b/tools/lint/cmd_add.c
index bbfdfd6..9f10711 100644
--- a/tools/lint/cmd_add.c
+++ b/tools/lint/cmd_add.c
@@ -2,9 +2,10 @@
* @file cmd_add.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Adam Piecek <piecek@cesnet.cz>
* @brief 'add' 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.
@@ -17,8 +18,8 @@
#include "cmd.h"
+#include <assert.h>
#include <getopt.h>
-#include <libgen.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -26,6 +27,8 @@
#include "libyang.h"
#include "common.h"
+#include "yl_opt.h"
+#include "yl_schema_features.h"
void
cmd_add_help(void)
@@ -44,133 +47,164 @@ cmd_add_help(void)
" -i, --make-implemented\n"
" Make the imported modules \"referenced\" from any loaded\n"
" <schema> module also implemented. If specified a second time,\n"
- " all the modules are set implemented.\n");
+ " all the modules are set implemented.\n"
+ " -X, --extended-leafref\n"
+ " Allow usage of deref() XPath function within leafref.\n");
}
-void
-cmd_add(struct ly_ctx **ctx, const char *cmdline)
+int
+cmd_add_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[] = {
{"disable-searchdir", no_argument, NULL, 'D'},
{"features", required_argument, NULL, 'F'},
{"help", no_argument, NULL, 'h'},
{"make-implemented", no_argument, NULL, 'i'},
+ {"extended-leafref", no_argument, NULL, 'X'},
{NULL, 0, NULL, 0}
};
- uint16_t options_ctx = 0;
- const char *all_features[] = {"*", NULL};
- struct ly_set fset = {0};
- if (parse_cmdline(cmdline, &argc, &argv)) {
- goto cleanup;
+ if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
+ return rc;
}
- while ((opt = getopt_long(argc, argv, "D:F:hi", options, &opt_index)) != -1) {
+ while ((opt = getopt_long(argc, yo->argv, commands[CMD_ADD].optstring, options, &opt_index)) != -1) {
switch (opt) {
case 'D': /* --disable--search */
- if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
- YLMSG_W("The -D option specified too many times.\n");
- }
- if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
- options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
- options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
- } else {
- options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
+ if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
+ YLMSG_W("The -D option specified too many times.");
}
+ yo_opt_update_disable_searchdir(yo);
break;
case 'F': /* --features */
- if (parse_features(optarg, &fset)) {
- goto cleanup;
+ if (parse_features(optarg, &yo->schema_features)) {
+ return 1;
}
break;
case 'h':
cmd_add_help();
- goto cleanup;
+ return 1;
case 'i': /* --make-implemented */
- if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
- options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
- options_ctx |= LY_CTX_ALL_IMPLEMENTED;
- } else {
- options_ctx |= LY_CTX_REF_IMPLEMENTED;
- }
+ yo_opt_update_make_implemented(yo);
+ break;
+
+ case 'X': /* --extended-leafref */
+ yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
break;
default:
- YLMSG_E("Unknown option.\n");
- goto cleanup;
+ YLMSG_E("Unknown option.");
+ return 1;
}
}
- if (argc == optind) {
+ *posv = &yo->argv[optind];
+ *posc = argc - optind;
+
+ return 0;
+}
+
+int
+cmd_add_dep(struct yl_opt *yo, int posc)
+{
+ if (yo->interactive && !posc) {
/* no argument */
cmd_add_help();
- goto cleanup;
+ return 1;
}
-
- if (!fset.count) {
+ if (!yo->schema_features.count) {
/* no features, enable all of them */
- options_ctx |= LY_CTX_ENABLE_IMP_FEATURES;
+ yo->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES;
}
- if (options_ctx) {
- ly_ctx_set_options(*ctx, options_ctx);
- }
+ return 0;
+}
- for (int i = 0; i < argc - optind; i++) {
- /* process the schema module files */
- LY_ERR ret;
- uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
- char *dir, *module;
- const char **features = NULL;
- struct ly_in *in = NULL;
+static int
+store_parsed_module(const char *filepath, struct lys_module *mod, struct yl_opt *yo)
+{
+ assert(!yo->interactive);
- if (parse_schema_path(argv[optind + i], &dir, &module)) {
- goto cleanup;
+ if (yo->schema_out_format || yo->feature_param_format) {
+ if (ly_set_add(&yo->schema_modules, (void *)mod, 1, NULL)) {
+ YLMSG_E("Storing parsed schema module (%s) for print failed.", filepath);
+ return 1;
}
+ }
- /* add temporarily also the path of the module itself */
- if (ly_ctx_set_searchdir(*ctx, dirname(dir)) == LY_EEXIST) {
- path_unset = 0;
- }
+ return 0;
+}
- /* get features list for this module */
- if (!fset.count) {
- features = all_features;
- } else {
- get_features(&fset, module, &features);
- }
+int
+cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
+{
+ const char *all_features[] = {"*", NULL};
+ LY_ERR ret;
+ uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
+ char *dir, *modname = NULL;
+ const char **features = NULL;
+ struct ly_in *in = NULL;
+ struct lys_module *mod;
+
+ assert(posv);
- /* temporary cleanup */
- free(dir);
- free(module);
+ if (yo->ctx_options) {
+ ly_ctx_set_options(*ctx, yo->ctx_options);
+ }
+
+ if (parse_schema_path(posv, &dir, &modname)) {
+ return 1;
+ }
- /* prepare input handler */
- ret = ly_in_new_filepath(argv[optind + i], 0, &in);
- if (ret) {
+ if (!yo->interactive) {
+ /* The module should already be parsed if yang_lib_file was set. */
+ if (yo->yang_lib_file && (mod = ly_ctx_get_module_implemented(*ctx, modname))) {
+ ret = store_parsed_module(posv, mod, yo);
goto cleanup;
}
+ /* parse module */
+ }
+
+ /* add temporarily also the path of the module itself */
+ if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) {
+ path_unset = 0;
+ }
+
+ /* get features list for this module */
+ if (!yo->schema_features.count) {
+ features = all_features;
+ } else {
+ get_features(&yo->schema_features, modname, &features);
+ }
+
+ /* prepare input handler */
+ ret = ly_in_new_filepath(posv, 0, &in);
+ if (ret) {
+ goto cleanup;
+ }
- /* parse the file */
- ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, NULL);
- ly_in_free(in, 1);
- ly_ctx_unset_searchdir_last(*ctx, path_unset);
+ /* parse the file */
+ ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, &mod);
+ ly_in_free(in, 1);
+ ly_ctx_unset_searchdir_last(*ctx, path_unset);
+ if (ret) {
+ goto cleanup;
+ }
- if (ret) {
- /* libyang printed the error messages */
+ if (!yo->interactive) {
+ if ((ret = store_parsed_module(posv, mod, yo))) {
goto cleanup;
}
}
cleanup:
- if (options_ctx) {
- ly_ctx_unset_options(*ctx, options_ctx);
- }
- ly_set_erase(&fset, free_features);
- free_cmdline(argv);
+ free(dir);
+ free(modname);
+
+ return ret;
}