yanglint REFACTOR parse modules only in cmd_add
diff --git a/tools/lint/cmd_add.c b/tools/lint/cmd_add.c
index 97ef94c..3b2acd4 100644
--- a/tools/lint/cmd_add.c
+++ b/tools/lint/cmd_add.c
@@ -124,27 +124,51 @@
return 0;
}
+static int
+store_parsed_module(const char *filepath, struct lys_module *mod, struct yl_opt *yo)
+{
+ assert(!yo->interactive);
+
+ 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.\n", filepath);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
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, *module;
+ char *dir, *modname = NULL;
const char **features = NULL;
struct ly_in *in = NULL;
+ struct lys_module *mod;
assert(posv);
if (yo->ctx_options) {
ly_ctx_set_options(*ctx, yo->ctx_options);
- yo->ctx_options = 0;
}
- if (parse_schema_path(posv, &dir, &module)) {
+ if (parse_schema_path(posv, &dir, &modname)) {
return 1;
}
+ 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;
@@ -154,23 +178,32 @@
if (!yo->schema_features.count) {
features = all_features;
} else {
- get_features(&yo->schema_features, module, &features);
+ get_features(&yo->schema_features, modname, &features);
}
- /* temporary cleanup */
- free(dir);
- free(module);
-
/* prepare input handler */
ret = ly_in_new_filepath(posv, 0, &in);
if (ret) {
- return 1;
+ goto cleanup;
}
/* parse the file */
- ret = lys_parse(*ctx, in, LYS_IN_UNKNOWN, features, NULL);
+ 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 (!yo->interactive) {
+ if ((ret = store_parsed_module(posv, mod, yo))) {
+ goto cleanup;
+ }
+ }
+
+cleanup:
+ free(dir);
+ free(modname);
return ret;
}
diff --git a/tools/lint/common.c b/tools/lint/common.c
index 205c32a..12b3733 100644
--- a/tools/lint/common.c
+++ b/tools/lint/common.c
@@ -80,7 +80,7 @@
return -1;
}
- if (ly_in_new_filepath(filepath, 0, in)) {
+ if (in && ly_in_new_filepath(filepath, 0, in)) {
YLMSG_E("Unable to process input file.\n");
return -1;
}
diff --git a/tools/lint/common.h b/tools/lint/common.h
index ed9f9eb..2a3bb50 100644
--- a/tools/lint/common.h
+++ b/tools/lint/common.h
@@ -157,7 +157,7 @@
* prohibited and such files are refused.
* @param[out] format_data Format of the data detected from the file name. If NULL specified, the data formats are
* prohibited and such files are refused.
- * @param[out] in Created input handler referring the file behind the @p filepath.
+ * @param[out] in Created input handler referring the file behind the @p filepath. Can be NULL.
* @return 0 on success.
* @return -1 on failure.
*/
diff --git a/tools/lint/main_ni.c b/tools/lint/main_ni.c
index 637c7f4..c9463dd 100644
--- a/tools/lint/main_ni.c
+++ b/tools/lint/main_ni.c
@@ -264,8 +264,7 @@
struct ly_in *in = NULL;
struct schema_features *sf;
struct lys_module *mod;
- const char *all_features[] = {"*", NULL};
- char *dir = NULL, *module = NULL, *filepath = NULL;
+ char *filepath = NULL;
/* Create libyang context. */
if (yo->yang_lib_file) {
@@ -300,69 +299,24 @@
if (!filepath) {
goto error;
}
- if (get_input(filepath, &format_schema, &format_data, &in)) {
+ if (get_input(filepath, &format_schema, &format_data, NULL)) {
goto error;
}
if (format_schema) {
- LY_ERR ret;
- uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
- const char **features;
-
- /* parse the input */
- if (parse_schema_path(filepath, &dir, &module)) {
+ if (cmd_add_exec(ctx, yo, filepath)) {
goto error;
}
-
- mod = NULL;
- if (yo->yang_lib_file) {
- /* just get the module, it should already be parsed */
- mod = ly_ctx_get_module_implemented(*ctx, module);
+ } else {
+ if (ly_in_new_filepath(filepath, 0, &in)) {
+ YLMSG_E("Unable to process input file.\n");
+ goto error;
}
- if (!mod) {
- /* 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, module, &features);
- }
-
- /* parse module */
- ret = lys_parse(*ctx, in, format_schema, features, &mod);
- ly_ctx_unset_searchdir_last(*ctx, path_unset);
- if (ret) {
- YLMSG_E("Parsing schema module \"%s\" failed.\n", filepath);
- goto error;
- }
- }
-
- /* temporary cleanup */
- free(dir);
- dir = NULL;
- free(module);
- module = NULL;
-
- if (yo->schema_out_format || yo->feature_param_format) {
- /* module will be printed */
- if (ly_set_add(&yo->schema_modules, (void *)mod, 1, NULL)) {
- YLMSG_E("Storing parsed schema module (%s) for print failed.\n", filepath);
- goto error;
- }
- }
- } else if (format_data) {
if (!fill_cmdline_file(&yo->data_inputs, in, filepath, format_data)) {
goto error;
}
in = NULL;
}
-
- ly_in_free(in, 1);
- in = NULL;
}
/* check that all specified features were applied, apply now if possible */
@@ -389,8 +343,6 @@
error:
ly_in_free(in, 1);
- free(dir);
- free(module);
return -1;
}