parser CHANGE import statement
create new function lyp_check_import, which is used in YIN and YANG parser
diff --git a/src/parser.c b/src/parser.c
index 60899ac..6c06f02 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -1387,6 +1387,65 @@
return EXIT_FAILURE;
}
+int
+lyp_check_import(struct lys_module *module, const char *value, int line, struct lys_import *imp)
+{
+ int count;
+
+ /* check for circular import, store it if passed */
+ if (!module->ctx->models.parsing) {
+ count = 0;
+ } else {
+ for (count = 0; module->ctx->models.parsing[count]; ++count) {
+ if (ly_strequal(value, module->ctx->models.parsing[count], 1)) {
+ LOGERR(LY_EVALID, "Circular import dependency on the module \"%s\".", value);
+ goto error;
+ }
+ }
+ }
+ ++count;
+ module->ctx->models.parsing =
+ ly_realloc(module->ctx->models.parsing, (count + 1) * sizeof *module->ctx->models.parsing);
+ if (!module->ctx->models.parsing) {
+ LOGMEM;
+ goto error;
+ }
+ module->ctx->models.parsing[count - 1] = value;
+ module->ctx->models.parsing[count] = NULL;
+
+ /* try to load the module */
+ imp->module = (struct lys_module *)ly_ctx_get_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL);
+ if (!imp->module) {
+ /* whether to use a user callback is decided in the function */
+ imp->module = (struct lys_module *)ly_ctx_load_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL);
+ }
+
+ /* remove the new module name now that its parsing is finished (even if failed) */
+ if (module->ctx->models.parsing[count] || !ly_strequal(module->ctx->models.parsing[count - 1], value, 1)) {
+ LOGINT;
+ }
+ --count;
+ if (count) {
+ module->ctx->models.parsing[count] = NULL;
+ } else {
+ free(module->ctx->models.parsing);
+ module->ctx->models.parsing = NULL;
+ }
+
+ /* check the result */
+ if (!imp->module) {
+ LOGVAL(LYE_INARG, line, LY_VLOG_NONE, NULL, value, "import");
+ LOGERR(LY_EVALID, "Importing \"%s\" module into \"%s\" failed.", value, module->name);
+ goto error;
+ }
+
+ return EXIT_SUCCESS;
+
+error:
+
+ return EXIT_FAILURE;
+}
+
/* Propagate imports and includes into the main module */
int
lyp_propagate_submodule(struct lys_module *module, struct lys_submodule *submodule, int line)
diff --git a/src/parser.h b/src/parser.h
index 0d06d91..b66c9a7 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -105,6 +105,8 @@
int lyp_check_include(struct lys_module *module, struct lys_submodule *submodule, const char *value,
int line, struct lys_include *inc, struct unres_schema *unres);
+int lyp_check_import(struct lys_module *module, const char *value, int line, struct lys_import *imp);
+
/**
* @brief Propagate imports and includes into the main module
*
diff --git a/src/parser_yang.c b/src/parser_yang.c
index a3c5ff3..3521882 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -108,70 +108,28 @@
int
yang_fill_import(struct lys_module *module, struct lys_import *imp, char *value, int line)
{
- int count, i;
+ const char *exp;
+ int rc, i;
- /* check for circular import, store it if passed */
- if (!module->ctx->models.parsing) {
- count = 0;
- } else {
- for (count = 0; module->ctx->models.parsing[count]; ++count) {
- if (value == module->ctx->models.parsing[count]) {
- LOGERR(LY_EVALID, "Circular import dependency on the module \"%s\".", value);
- goto error;
- }
- }
- }
- ++count;
- module->ctx->models.parsing =
- ly_realloc(module->ctx->models.parsing, (count + 1) * sizeof *module->ctx->models.parsing);
- if (!module->ctx->models.parsing) {
- LOGMEM;
+ exp = lydict_insert_zc(module->ctx, value);
+ rc = lyp_check_import(module, exp, line, imp);
+ lydict_remove(module->ctx, exp);
+ if (rc) {
goto error;
}
- module->ctx->models.parsing[count - 1] = value;
- module->ctx->models.parsing[count] = NULL;
-
- /* try to load the module */
- imp->module = (struct lys_module *)ly_ctx_get_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL);
- if (!imp->module) {
- /* whether to use a user callback is decided in the function */
- imp->module = (struct lys_module *)ly_ctx_load_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL);
- }
-
- /* remove the new module name now that its parsing is finished (even if failed) */
- if (module->ctx->models.parsing[count] || (module->ctx->models.parsing[count - 1] != value)) {
- LOGINT;
- }
- --count;
- if (count) {
- module->ctx->models.parsing[count] = NULL;
- } else {
- free(module->ctx->models.parsing);
- module->ctx->models.parsing = NULL;
- }
-
- /* check the result */
- if (!imp->module) {
- LOGERR(LY_EVALID, "Importing \"%s\" module into \"%s\" failed.", value, module->name);
- goto error;
- }
-
- module->imp_size++;
/* check duplicities in imported modules */
- for (i = 0; i < module->imp_size - 1; i++) {
- if (!strcmp(module->imp[i].module->name, module->imp[module->imp_size - 1].module->name)) {
+ for (i = 0; i < module->imp_size; i++) {
+ if (!strcmp(module->imp[i].module->name, module->imp[module->imp_size].module->name)) {
LOGVAL(LYE_SPEC, line, LY_VLOG_NONE, NULL, "Importing module \"%s\" repeatedly.", module->imp[i].module->name);
goto error;
}
}
-
- free(value);
+ module->imp_size++;
return EXIT_SUCCESS;
- error:
-
- free(value);
+error:
+ module->imp_size++;
return EXIT_FAILURE;
}
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 13b1704..44f2ee8 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -2312,7 +2312,6 @@
{
struct lyxml_elem *child;
const char *value;
- int count;
LY_TREE_FOR(yin->child, child) {
if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
@@ -2350,54 +2349,7 @@
GETVAL(value, yin, "module");
- /* check for circular import, store it if passed */
- if (!module->ctx->models.parsing) {
- count = 0;
- } else {
- for (count = 0; module->ctx->models.parsing[count]; ++count) {
- if (ly_strequal(value, module->ctx->models.parsing[count], 1)) {
- LOGERR(LY_EVALID, "Circular import dependency on the module \"%s\".", value);
- goto error;
- }
- }
- }
- ++count;
- module->ctx->models.parsing =
- ly_realloc(module->ctx->models.parsing, (count + 1) * sizeof *module->ctx->models.parsing);
- if (!module->ctx->models.parsing) {
- LOGMEM;
- goto error;
- }
- module->ctx->models.parsing[count - 1] = value;
- module->ctx->models.parsing[count] = NULL;
-
- /* try to load the module */
- imp->module = (struct lys_module *)ly_ctx_get_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL);
- if (!imp->module) {
- /* whether to use a user callback is decided in the function */
- imp->module = (struct lys_module *)ly_ctx_load_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL);
- }
-
- /* remove the new module name now that its parsing is finished (even if failed) */
- if (module->ctx->models.parsing[count] || !ly_strequal(module->ctx->models.parsing[count - 1], value, 1)) {
- LOGINT;
- }
- --count;
- if (count) {
- module->ctx->models.parsing[count] = NULL;
- } else {
- free(module->ctx->models.parsing);
- module->ctx->models.parsing = NULL;
- }
-
- /* check the result */
- if (!imp->module) {
- LOGVAL(LYE_INARG, LOGLINE(yin), LY_VLOG_NONE, NULL, value, yin->name);
- LOGERR(LY_EVALID, "Importing \"%s\" module into \"%s\" failed.", value, module->name);
- goto error;
- }
-
- return EXIT_SUCCESS;
+ return lyp_check_import(module, value, LOGLINE(yin), imp);
error:
diff --git a/src/yang.y b/src/yang.y
index dc12a35..c9e197f 100644
--- a/src/yang.y
+++ b/src/yang.y
@@ -3656,7 +3656,7 @@
}
*tmp = ':';
} else {
- /* check */
+ /* check identifier */
if (lyp_check_identifier(s, LY_IDENT_SIMPLE, yylineno, trg, NULL)) {
free(s);
YYERROR;