Merge remote-tracking branch 'upstream/libyang2' into libyang2
diff --git a/src/common.h b/src/common.h
index a214f4d..380ec6e 100644
--- a/src/common.h
+++ b/src/common.h
@@ -15,6 +15,7 @@
#ifndef LY_COMMON_H_
#define LY_COMMON_H_
+#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
diff --git a/src/log.h b/src/log.h
index b5d6834..95232bc 100644
--- a/src/log.h
+++ b/src/log.h
@@ -148,7 +148,8 @@
LY_EEXIST, /**< Item already exists */
LY_EINT, /**< Internal error */
LY_EVALID, /**< Validation failure */
- LY_EPLUGIN /**< Error reported by a plugin */
+ LY_EPLUGIN, /**< Error reported by a plugin */
+ LY_EDENIED /**< Operation is not allowed */
} LY_ERR;
/**
diff --git a/src/parser_yang.c b/src/parser_yang.c
index 805ef4f..f46b901 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -1469,26 +1469,24 @@
{
LY_ERR ret = 0;
char *buf, *word;
- size_t count, word_len;
+ const char **item;
+ size_t word_len;
enum yang_keyword kw;
/* allocate new pointer */
- for (count = 1; (*texts) && (*texts)[count - 1]; ++count);
- *texts = realloc(*texts, (1 + count) * sizeof **texts);
- LY_CHECK_ERR_RET(!*texts, LOGMEM(ctx->ctx), LY_EMEM);
+ LYSP_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
/* get value */
ret = get_argument(ctx, data, arg, &word, &buf, &word_len);
LY_CHECK_RET(ret);
- INSERT_WORD(ctx, buf, (*texts)[count - 1], word, word_len);
- (*texts)[count] = NULL; /* NULL-termination of the array */
+ INSERT_WORD(ctx, buf, *item, word, word_len);
YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
LY_CHECK_RET(ret);
switch (kw) {
case YANG_CUSTOM:
- ret = parse_ext(ctx, data, word, word_len, substmt, count - 1, exts);
+ ret = parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts);
LY_CHECK_RET(ret);
break;
default:
diff --git a/src/tree_schema.c b/src/tree_schema.c
index 6275235..73c4fad 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -13,6 +13,7 @@
*/
#define _DEFAULT_SOURCE
+#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/limits.h>
@@ -29,7 +30,8 @@
#define FREE_ARRAY(CTX, ARRAY, FUNC) {uint64_t c__; LY_ARRAY_FOR(ARRAY, c__){FUNC(CTX, LY_ARRAY_INDEX(ARRAY, c__), dict);}free(ARRAY);}
#define FREE_MEMBER(CTX, MEMBER, FUNC) if (MEMBER) {FUNC(CTX, MEMBER, dict);free(MEMBER);}
-#define FREE_STRING(CTX, STRING) if (dict && STRING) {lydict_remove(CTX, STRING);}
+#define FREE_STRING(CTX, STRING, DICT) if (DICT && STRING) {lydict_remove(CTX, STRING);}
+#define FREE_STRINGS(CTX, ARRAY, DICT) {uint64_t c__; LY_ARRAY_FOR(ARRAY, c__){FREE_STRING(CTX, *(LY_ARRAY_INDEX(ARRAY, c__, const char*)), DICT);}free(ARRAY);}
static void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp, int dict);
static void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node, int dict);
@@ -46,8 +48,8 @@
{
struct lysp_stmt *child, *next;
- FREE_STRING(ctx, stmt->stmt);
- FREE_STRING(ctx, stmt->arg);
+ FREE_STRING(ctx, stmt->stmt, dict);
+ FREE_STRING(ctx, stmt->arg, dict);
LY_LIST_FOR_SAFE(stmt->child, next, child) {
lysp_stmt_free(ctx, child, dict);
@@ -61,8 +63,8 @@
{
struct lysp_stmt *stmt, *next;
- FREE_STRING(ctx, ext->name);
- FREE_STRING(ctx, ext->argument);
+ FREE_STRING(ctx, ext->name, dict);
+ FREE_STRING(ctx, ext->argument, dict);
LY_LIST_FOR_SAFE(ext->child, next, stmt) {
lysp_stmt_free(ctx, stmt, dict);
@@ -72,122 +74,102 @@
static void
lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import, int dict)
{
- FREE_STRING(ctx, import->name);
- FREE_STRING(ctx, import->prefix);
- FREE_STRING(ctx, import->dsc);
- FREE_STRING(ctx, import->ref);
+ FREE_STRING(ctx, import->name, dict);
+ FREE_STRING(ctx, import->prefix, dict);
+ FREE_STRING(ctx, import->dsc, dict);
+ FREE_STRING(ctx, import->ref, dict);
FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
- {
- void *p__;
- int64_t c__;
- for(p__ = ((void*)((uint32_t*)((import->exts) + 0) + 1)), c__ = 0;
- (import->exts) && c__ < (*(uint32_t*)(import->exts));
- p__ = ((void*)((uint32_t*)((import->exts) + c__) + 1))) {
- lysp_ext_instance_free(ctx, p__, dict);
- }
- free(import->exts);
- }
}
static void
lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include, int dict)
{
- FREE_STRING(ctx, include->name);
- FREE_STRING(ctx, include->dsc);
- FREE_STRING(ctx, include->ref);
+ FREE_STRING(ctx, include->name, dict);
+ FREE_STRING(ctx, include->dsc, dict);
+ FREE_STRING(ctx, include->ref, dict);
FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
}
static void
lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev, int dict)
{
- FREE_STRING(ctx, rev->dsc);
- FREE_STRING(ctx, rev->ref);
+ FREE_STRING(ctx, rev->dsc, dict);
+ FREE_STRING(ctx, rev->ref, dict);
FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
}
static void
lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext, int dict)
{
- FREE_STRING(ctx, ext->name);
- FREE_STRING(ctx, ext->argument);
- FREE_STRING(ctx, ext->dsc);
- FREE_STRING(ctx, ext->ref);
+ FREE_STRING(ctx, ext->name, dict);
+ FREE_STRING(ctx, ext->argument, dict);
+ FREE_STRING(ctx, ext->dsc, dict);
+ FREE_STRING(ctx, ext->ref, dict);
FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
}
static void
lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat, int dict)
{
- unsigned int u;
- FREE_STRING(ctx, feat->name);
- for (u = 0; feat->iffeatures && feat->iffeatures[u]; ++u) {
- FREE_STRING(ctx, feat->iffeatures[u]);
- }
- free(feat->iffeatures);
- FREE_STRING(ctx, feat->dsc);
- FREE_STRING(ctx, feat->ref);
+ FREE_STRING(ctx, feat->name, dict);
+ FREE_STRINGS(ctx, feat->iffeatures, 1);
+ FREE_STRING(ctx, feat->dsc, dict);
+ FREE_STRING(ctx, feat->ref, dict);
FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
}
static void
lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident, int dict)
{
- unsigned int u;
- FREE_STRING(ctx, ident->name);
- for (u = 0; ident->iffeatures && ident->iffeatures[u]; ++u) {
- FREE_STRING(ctx, ident->iffeatures[u]);
+ FREE_STRING(ctx, ident->name, dict);
+ //FREE_STRINGS(ctx, ident->iffeatures, 1);
+ {
+ uint64_t c__;
+ for (c__ = 0; ident->iffeatures && c__ < (*((uint32_t*)(ident->iffeatures))); ++c__) {
+ if (1 && ((void*)((uint32_t*)((ident->iffeatures) + c__) + 1))) {
+ lydict_remove(ctx, *((char**)((uint32_t*)((ident->iffeatures) + c__) + 1)));
+ };
+ }
+ free(ident->iffeatures);
}
- free(ident->iffeatures);
- for (u = 0; ident->bases && ident->bases[u]; ++u) {
- FREE_STRING(ctx, ident->bases[u]);
- }
- free(ident->bases);
- FREE_STRING(ctx, ident->dsc);
- FREE_STRING(ctx, ident->ref);
+ FREE_STRINGS(ctx, ident->bases, dict);
+ FREE_STRING(ctx, ident->dsc, dict);
+ FREE_STRING(ctx, ident->ref, dict);
FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
}
static void
lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr, int dict)
{
- FREE_STRING(ctx, restr->arg);
- FREE_STRING(ctx, restr->emsg);
- FREE_STRING(ctx, restr->eapptag);
- FREE_STRING(ctx, restr->dsc);
- FREE_STRING(ctx, restr->ref);
+ FREE_STRING(ctx, restr->arg, dict);
+ FREE_STRING(ctx, restr->emsg, dict);
+ FREE_STRING(ctx, restr->eapptag, dict);
+ FREE_STRING(ctx, restr->dsc, dict);
+ FREE_STRING(ctx, restr->ref, dict);
FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
}
static void
lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item, int dict)
{
- unsigned int u;
- FREE_STRING(ctx, item->name);
- FREE_STRING(ctx, item->dsc);
- FREE_STRING(ctx, item->ref);
- for (u = 0; item->iffeatures && item->iffeatures[u]; ++u) {
- FREE_STRING(ctx, item->iffeatures[u]);
- }
- free(item->iffeatures);
+ FREE_STRING(ctx, item->name, dict);
+ FREE_STRING(ctx, item->dsc, dict);
+ FREE_STRING(ctx, item->ref, dict);
+ FREE_STRINGS(ctx, item->iffeatures, 1);
FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
}
static void
lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type, int dict)
{
- unsigned int u;
- FREE_STRING(ctx, type->name);
+ FREE_STRING(ctx, type->name, dict);
FREE_MEMBER(ctx, type->range, lysp_restr_free);
FREE_MEMBER(ctx, type->length, lysp_restr_free);
FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
- FREE_STRING(ctx, type->path);
- for (u = 0; type->bases && type->bases[u]; ++u) {
- FREE_STRING(ctx, type->bases[u]);
- }
- free(type->bases);
+ FREE_STRING(ctx, type->path, dict);
+ FREE_STRINGS(ctx, type->bases, dict);
FREE_ARRAY(ctx, type->types, lysp_type_free);
FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
}
@@ -195,11 +177,11 @@
static void
lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf, int dict)
{
- FREE_STRING(ctx, tpdf->name);
- FREE_STRING(ctx, tpdf->units);
- FREE_STRING(ctx, tpdf->dflt);
- FREE_STRING(ctx, tpdf->dsc);
- FREE_STRING(ctx, tpdf->ref);
+ FREE_STRING(ctx, tpdf->name, dict);
+ FREE_STRING(ctx, tpdf->units, dict);
+ FREE_STRING(ctx, tpdf->dflt, dict);
+ FREE_STRING(ctx, tpdf->dsc, dict);
+ FREE_STRING(ctx, tpdf->ref, dict);
FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
lysp_type_free(ctx, &tpdf->type, dict);
}
@@ -222,14 +204,10 @@
static void
lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action, int dict)
{
- unsigned int u;
- FREE_STRING(ctx, action->name);
- FREE_STRING(ctx, action->dsc);
- FREE_STRING(ctx, action->ref);
- for (u = 0; action->iffeatures && action->iffeatures[u]; ++u) {
- FREE_STRING(ctx, action->iffeatures[u]);
- }
- free(action->iffeatures);
+ FREE_STRING(ctx, action->name, dict);
+ FREE_STRING(ctx, action->dsc, dict);
+ FREE_STRING(ctx, action->ref, dict);
+ FREE_STRINGS(ctx, action->iffeatures, 1);
FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
FREE_MEMBER(ctx, action->input, lysp_action_inout_free);
@@ -240,16 +218,12 @@
static void
lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif, int dict)
{
- unsigned int u;
struct lysp_node *node, *next;
- FREE_STRING(ctx, notif->name);
- FREE_STRING(ctx, notif->dsc);
- FREE_STRING(ctx, notif->ref);
- for (u = 0; notif->iffeatures && notif->iffeatures[u]; ++u) {
- FREE_STRING(ctx, notif->iffeatures[u]);
- }
- free(notif->iffeatures);
+ FREE_STRING(ctx, notif->name, dict);
+ FREE_STRING(ctx, notif->dsc, dict);
+ FREE_STRING(ctx, notif->ref, dict);
+ FREE_STRINGS(ctx, notif->iffeatures, 1);
FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
@@ -264,9 +238,9 @@
{
struct lysp_node *node, *next;
- FREE_STRING(ctx, grp->name);
- FREE_STRING(ctx, grp->dsc);
- FREE_STRING(ctx, grp->ref);
+ FREE_STRING(ctx, grp->name, dict);
+ FREE_STRING(ctx, grp->dsc, dict);
+ FREE_STRING(ctx, grp->ref, dict);
FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
LY_LIST_FOR_SAFE(grp->data, next, node) {
@@ -280,26 +254,22 @@
static void
lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when, int dict)
{
- FREE_STRING(ctx, when->cond);
- FREE_STRING(ctx, when->dsc);
- FREE_STRING(ctx, when->ref);
+ FREE_STRING(ctx, when->cond, dict);
+ FREE_STRING(ctx, when->dsc, dict);
+ FREE_STRING(ctx, when->ref, dict);
FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
}
static void
lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment, int dict)
{
- unsigned int u;
struct lysp_node *node, *next;
- FREE_STRING(ctx, augment->nodeid);
- FREE_STRING(ctx, augment->dsc);
- FREE_STRING(ctx, augment->ref);
+ FREE_STRING(ctx, augment->nodeid, dict);
+ FREE_STRING(ctx, augment->dsc, dict);
+ FREE_STRING(ctx, augment->ref, dict);
FREE_MEMBER(ctx, augment->when, lysp_when_free);
- for (u = 0; augment->iffeatures && augment->iffeatures[u]; ++u) {
- FREE_STRING(ctx, augment->iffeatures[u]);
- }
- free(augment->iffeatures);
+ FREE_STRINGS(ctx, augment->iffeatures, 1);
LY_LIST_FOR_SAFE(augment->child, next, node) {
lysp_node_free(ctx, node, dict);
}
@@ -311,7 +281,6 @@
static void
lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d, int dict)
{
- unsigned int u;
struct lysp_deviate_add *add = (struct lysp_deviate_add*)d;
struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl*)d;
@@ -322,21 +291,15 @@
break;
case LYS_DEV_ADD:
case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
- FREE_STRING(ctx, add->units);
+ FREE_STRING(ctx, add->units, dict);
FREE_ARRAY(ctx, add->musts, lysp_restr_free);
- for (u = 0; add->uniques && add->uniques[u]; ++u) {
- FREE_STRING(ctx, add->uniques[u]);
- }
- free(add->uniques);
- for (u = 0; add->dflts && add->dflts[u]; ++u) {
- FREE_STRING(ctx, add->dflts[u]);
- }
- free(add->dflts);
+ FREE_STRINGS(ctx, add->uniques, dict);
+ FREE_STRINGS(ctx, add->dflts, dict);
break;
case LYS_DEV_REPLACE:
FREE_MEMBER(ctx, rpl->type, lysp_type_free);
- FREE_STRING(ctx, rpl->units);
- FREE_STRING(ctx, rpl->dflt);
+ FREE_STRING(ctx, rpl->units, dict);
+ FREE_STRING(ctx, rpl->dflt, dict);
break;
default:
LOGINT(ctx);
@@ -349,9 +312,9 @@
{
struct lysp_deviate *next, *iter;
- FREE_STRING(ctx, dev->nodeid);
- FREE_STRING(ctx, dev->dsc);
- FREE_STRING(ctx, dev->ref);
+ FREE_STRING(ctx, dev->nodeid, dict);
+ FREE_STRING(ctx, dev->dsc, dict);
+ FREE_STRING(ctx, dev->ref, dict);
LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
lysp_deviate_free(ctx, iter, dict);
free(iter);
@@ -362,43 +325,32 @@
static void
lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref, int dict)
{
- unsigned int u;
- FREE_STRING(ctx, ref->nodeid);
- FREE_STRING(ctx, ref->dsc);
- FREE_STRING(ctx, ref->ref);
- for (u = 0; ref->iffeatures && ref->iffeatures[u]; ++u) {
- FREE_STRING(ctx, ref->iffeatures[u]);
- }
- free(ref->iffeatures);
+ FREE_STRING(ctx, ref->nodeid, dict);
+ FREE_STRING(ctx, ref->dsc, dict);
+ FREE_STRING(ctx, ref->ref, dict);
+ FREE_STRINGS(ctx, ref->iffeatures, 1);
FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
- FREE_STRING(ctx, ref->presence);
- for (u = 0; ref->dflts && ref->dflts[u]; ++u) {
- FREE_STRING(ctx, ref->dflts[u]);
- }
- free(ref->dflts);
+ FREE_STRING(ctx, ref->presence, dict);
+ FREE_STRINGS(ctx, ref->dflts, dict);
FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
}
static void
lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node, int dict)
{
- unsigned int u;
struct lysp_node *child, *next;
- FREE_STRING(ctx, node->name);
- FREE_STRING(ctx, node->dsc);
- FREE_STRING(ctx, node->ref);
+ FREE_STRING(ctx, node->name, dict);
+ FREE_STRING(ctx, node->dsc, dict);
+ FREE_STRING(ctx, node->ref, dict);
FREE_MEMBER(ctx, node->when, lysp_when_free);
- for (u = 0; node->iffeatures && node->iffeatures[u]; ++u) {
- FREE_STRING(ctx, node->iffeatures[u]);
- }
- free(node->iffeatures);
+ FREE_STRINGS(ctx, node->iffeatures, dict);
FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
switch(node->nodetype) {
case LYS_CONTAINER:
FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->musts, lysp_restr_free);
- FREE_STRING(ctx, ((struct lysp_node_container*)node)->presence);
+ FREE_STRING(ctx, ((struct lysp_node_container*)node)->presence, dict);
FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->typedefs, lysp_tpdf_free);
FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->groupings, lysp_grp_free);
LY_LIST_FOR_SAFE(((struct lysp_node_container*)node)->child, next, child) {
@@ -410,21 +362,18 @@
case LYS_LEAF:
FREE_ARRAY(ctx, ((struct lysp_node_leaf*)node)->musts, lysp_restr_free);
lysp_type_free(ctx, &((struct lysp_node_leaf*)node)->type, dict);
- FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->units);
- FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->dflt);
+ FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->units, dict);
+ FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->dflt, dict);
break;
case LYS_LEAFLIST:
FREE_ARRAY(ctx, ((struct lysp_node_leaflist*)node)->musts, lysp_restr_free);
lysp_type_free(ctx, &((struct lysp_node_leaflist*)node)->type, dict);
- FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->units);
- for (u = 0; ((struct lysp_node_leaflist*)node)->dflts && ((struct lysp_node_leaflist*)node)->dflts[u]; ++u) {
- FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->dflts[u]);
- }
- free(((struct lysp_node_leaflist*)node)->dflts);
+ FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->units, dict);
+ FREE_STRINGS(ctx, ((struct lysp_node_leaflist*)node)->dflts, dict);
break;
case LYS_LIST:
FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->musts, lysp_restr_free);
- FREE_STRING(ctx, ((struct lysp_node_list*)node)->key);
+ FREE_STRING(ctx, ((struct lysp_node_list*)node)->key, dict);
FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->typedefs, lysp_tpdf_free);
FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->groupings, lysp_grp_free);
LY_LIST_FOR_SAFE(((struct lysp_node_list*)node)->child, next, child) {
@@ -432,16 +381,13 @@
}
FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->actions, lysp_action_free);
FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->notifs, lysp_notif_free);
- for (u = 0; ((struct lysp_node_list*)node)->uniques && ((struct lysp_node_list*)node)->uniques[u]; ++u) {
- FREE_STRING(ctx, ((struct lysp_node_list*)node)->uniques[u]);
- }
- free(((struct lysp_node_list*)node)->uniques);
+ FREE_STRINGS(ctx, ((struct lysp_node_list*)node)->uniques, dict);
break;
case LYS_CHOICE:
LY_LIST_FOR_SAFE(((struct lysp_node_choice*)node)->child, next, child) {
lysp_node_free(ctx, child, dict);
}
- FREE_STRING(ctx, ((struct lysp_node_choice*)node)->dflt);
+ FREE_STRING(ctx, ((struct lysp_node_choice*)node)->dflt, dict);
break;
case LYS_CASE:
LY_LIST_FOR_SAFE(((struct lysp_node_case*)node)->child, next, child) {
@@ -472,18 +418,18 @@
LY_CHECK_ARG_RET(NULL, module,);
ctx = module->ctx;
- FREE_STRING(ctx, module->name);
- FREE_STRING(ctx, module->filepath);
- FREE_STRING(ctx, module->ns); /* or belongs-to */
- FREE_STRING(ctx, module->prefix);
+ FREE_STRING(ctx, module->name, dict);
+ FREE_STRING(ctx, module->filepath, dict);
+ FREE_STRING(ctx, module->ns, dict); /* or belongs-to */
+ FREE_STRING(ctx, module->prefix, dict);
FREE_ARRAY(ctx, module->imports, lysp_import_free);
FREE_ARRAY(ctx, module->includes, lysp_include_free);
- FREE_STRING(ctx, module->org);
- FREE_STRING(ctx, module->contact);
- FREE_STRING(ctx, module->dsc);
- FREE_STRING(ctx, module->ref);
+ FREE_STRING(ctx, module->org, dict);
+ FREE_STRING(ctx, module->contact, dict);
+ FREE_STRING(ctx, module->dsc, dict);
+ FREE_STRING(ctx, module->ref, dict);
FREE_ARRAY(ctx, module->revs, lysp_revision_free);
FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
@@ -506,14 +452,24 @@
API void
lysp_module_free(struct lysp_module *module)
{
- lysp_module_free_(module, 1);
+ if (module) {
+ lysp_module_free_(module, 1);
+ }
+}
+
+static void
+lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff, int UNUSED(dict))
+{
+ free(iff->features);
+ free(iff->expr);
}
static void
lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat, int dict)
{
- FREE_STRING(ctx, feat->name);
-
+ FREE_STRING(ctx, feat->name, dict);
+ FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free);
+ free(feat->depfeatures);
}
static void
@@ -524,9 +480,9 @@
LY_CHECK_ARG_RET(NULL, module,);
ctx = module->ctx;
- FREE_STRING(ctx, module->name);
- FREE_STRING(ctx, module->ns);
- FREE_STRING(ctx, module->prefix);
+ FREE_STRING(ctx, module->name, dict);
+ FREE_STRING(ctx, module->ns, dict);
+ FREE_STRING(ctx, module->prefix, dict);
FREE_ARRAY(ctx, module->features, lysc_feature_free);
@@ -538,7 +494,9 @@
API void
lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
{
- lysc_module_free_(module, 1);
+ if (module) {
+ lysc_module_free_(module, 1);
+ }
}
void
@@ -553,30 +511,512 @@
free(module);
}
+struct iff_stack {
+ int size;
+ int index; /* first empty item */
+ uint8_t *stack;
+};
+
static LY_ERR
-lys_compile_iffeature(struct lysc_ctx *ctx, const char *iff_p, struct lysc_iffeature **iffeatures)
+iff_stack_push(struct iff_stack *stack, uint8_t value)
{
- struct lysc_iffeature *iff;
-
- if ((ctx->mod->version != 2) && ((iff_p[0] == '(') || strchr(iff_p, ' '))) {
- LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path,LY_VCODE_INVAL, strlen(iff_p), iff_p, "if-feature");
- return LY_EVALID;
+ if (stack->index == stack->size) {
+ stack->size += 4;
+ stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
+ LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
}
-
- LYSP_ARRAY_NEW_RET(ctx->mod->ctx, *iffeatures, iff, LY_EMEM);
-
+ stack->stack[stack->index++] = value;
return LY_SUCCESS;
}
-static LY_ERR
-lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature **features)
+static uint8_t
+iff_stack_pop(struct iff_stack *stack)
{
- struct lysc_feature *feature;
+ stack->index--;
+ return stack->stack[stack->index];
+}
+
+static void
+iff_stack_clean(struct iff_stack *stack)
+{
+ stack->size = 0;
+ free(stack->stack);
+}
+
+static void
+iff_setop(uint8_t *list, uint8_t op, int pos)
+{
+ uint8_t *item;
+ uint8_t mask = 3;
+
+ assert(pos >= 0);
+ assert(op <= 3); /* max 2 bits */
+
+ item = &list[pos / 4];
+ mask = mask << 2 * (pos % 4);
+ *item = (*item) & ~mask;
+ *item = (*item) | (op << 2 * (pos % 4));
+}
+
+static uint8_t
+iff_getop(uint8_t *list, int pos)
+{
+ uint8_t *item;
+ uint8_t mask = 3, result;
+
+ assert(pos >= 0);
+
+ item = &list[pos / 4];
+ result = (*item) & (mask << 2 * (pos % 4));
+ return result >> 2 * (pos % 4);
+}
+
+#define LYS_IFF_LP 0x04 /* ( */
+#define LYS_IFF_RP 0x08 /* ) */
+
+API int
+lysc_feature_value(const struct lysc_feature *feature)
+{
+ LY_CHECK_ARG_RET(NULL, feature, -1);
+ return feature->flags & LYS_FENABLED ? 1 : 0;
+}
+
+static struct lysc_feature *
+lysc_feature_find(struct lysc_module *mod, const char *name, size_t len)
+{
+ size_t i;
+ struct lysc_feature *f;
+
+ for (i = 0; i < len; ++i) {
+ if (name[i] == ':') {
+ /* we have a prefixed feature */
+ mod = lysc_module_find_prefix(mod, name, i);
+ LY_CHECK_RET(!mod, NULL);
+
+ name = &name[i + 1];
+ len = len - i - 1;
+ }
+ }
+
+ /* we have the correct module, get the feature */
+ LY_ARRAY_FOR(mod->features, i) {
+ f = LY_ARRAY_INDEX(mod->features, i);
+ if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
+ return f;
+ }
+ }
+
+ return NULL;
+}
+
+static int
+lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
+{
+ uint8_t op;
+ int a, b;
+
+ op = iff_getop(iff->expr, *index_e);
+ (*index_e)++;
+
+ switch (op) {
+ case LYS_IFF_F:
+ /* resolve feature */
+ return lysc_feature_value(*LY_ARRAY_INDEX(iff->features, (*index_f)++, struct lysc_feature*));
+ case LYS_IFF_NOT:
+ /* invert result */
+ return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
+ case LYS_IFF_AND:
+ case LYS_IFF_OR:
+ a = lysc_iffeature_value_(iff, index_e, index_f);
+ b = lysc_iffeature_value_(iff, index_e, index_f);
+ if (op == LYS_IFF_AND) {
+ return a && b;
+ } else { /* LYS_IFF_OR */
+ return a || b;
+ }
+ }
+
+ return 0;
+}
+
+API int
+lysc_iffeature_value(const struct lysc_iffeature *iff)
+{
+ int index_e = 0, index_f = 0;
+
+ LY_CHECK_ARG_RET(NULL, iff, -1);
+
+ if (iff->expr) {
+ return lysc_iffeature_value_(iff, &index_e, &index_f);
+ }
+ return 0;
+}
+
+/*
+ * op: 1 - enable, 0 - disable
+ */
+/**
+ * @brief Enable/Disable the specified feature in the module.
+ *
+ * If the feature is already set to the desired value, LY_SUCCESS is returned.
+ * By changing the feature, also all the feature which depends on it via their
+ * if-feature statements are again evaluated (disabled if a if-feature statemen
+ * evaluates to false).
+ *
+ * @param[in] mod Compiled module where to set (search for) the feature.
+ * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
+ * set all the features in the module.
+ * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
+ * @return LY_ERR value.
+ */
+static LY_ERR
+lys_feature_change(const struct lysc_module *mod, const char *name, int value)
+{
+ int all = 0;
+ unsigned int u;
+ struct lysc_feature *f, **df;
+ struct lysc_iffeature *iff;
+ struct ly_set *changed;
+
+ if (!mod->features) {
+ LOGERR(mod->ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
+ return LY_EINVAL;
+ }
+
+ if (!strcmp(name, "*")) {
+ /* enable all */
+ all = 1;
+ }
+ changed = ly_set_new();
+
+ for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
+ f = LY_ARRAY_INDEX(mod->features, u);
+ if (all || !strcmp(f->name, name)) {
+ if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
+ if (all) {
+ /* skip already set features */
+ continue;
+ } else {
+ /* feature already set correctly */
+ ly_set_free(changed, NULL);
+ return LY_SUCCESS;
+ }
+ }
+
+ if (value) { /* enable */
+ /* check referenced features if they are enabled */
+ LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
+ if (!lysc_iffeature_value(iff)) {
+ if (all) {
+ LOGWRN(mod->ctx,
+ "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
+ f->name);
+ goto next;
+ } else {
+ LOGERR(mod->ctx, LY_EDENIED,
+ "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
+ f->name);
+ ly_set_free(changed, NULL);
+ return LY_EDENIED;
+ }
+ }
+ }
+ /* enable the feature */
+ f->flags |= LYS_FENABLED;
+ } else { /* disable */
+ /* disable the feature */
+ f->flags &= ~LYS_FENABLED;
+ }
+
+ /* remember the changed feature */
+ ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
+
+ if (!all) {
+ /* stop in case changing a single feature */
+ break;
+ }
+ }
+next:
+ ;
+ }
+
+ if (!all && !changed->count) {
+ LOGERR(mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
+ ly_set_free(changed, NULL);
+ return LY_EINVAL;
+ }
+
+ /* reflect change(s) in the dependent features */
+ for (u = 0; u < changed->count; ++u) {
+ /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
+ * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
+ * is not done - by default, features are disabled and must be explicitely enabled. */
+ f = changed->objs[u];
+ LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
+ if (!((*df)->flags & LYS_FENABLED)) {
+ /* not enabled, nothing to do */
+ continue;
+ }
+ /* check the feature's if-features which could change by the previous change of our feature */
+ LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
+ if (!lysc_iffeature_value(iff)) {
+ /* the feature must be disabled now */
+ (*df)->flags &= ~LYS_FENABLED;
+ /* add the feature into the list of changed features */
+ ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
+ break;
+ }
+ }
+ }
+ }
+
+ ly_set_free(changed, NULL);
+ return LY_SUCCESS;
+}
+
+API LY_ERR
+lys_feature_enable(struct lys_module *module, const char *feature)
+{
+ LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
+
+ return lys_feature_change(module->compiled, feature, 1);
+}
+
+API LY_ERR
+lys_feature_disable(struct lys_module *module, const char *feature)
+{
+ LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
+
+ return lys_feature_change(module->compiled, feature, 0);
+}
+
+API int
+lys_feature_value(const struct lys_module *module, const char *feature)
+{
+ struct lysc_feature *f;
+ struct lysc_module *mod;
+ unsigned int u;
+
+ LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
+ mod = module->compiled;
+
+ /* search for the specified feature */
+ for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
+ f = LY_ARRAY_INDEX(mod->features, u);
+ if (!strcmp(f->name, feature)) {
+ if (f->flags & LYS_FENABLED) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ }
+
+ /* feature definition not found */
+ return -1;
+}
+
+static LY_ERR
+lys_compile_iffeature(struct lysc_ctx *ctx, const char *value, int UNUSED(options), struct lysc_iffeature *iff, struct lysc_feature *parent)
+{
+ const char *c = value;
+ int r, rc = EXIT_FAILURE;
+ int i, j, last_not, checkversion = 0;
+ unsigned int f_size = 0, expr_size = 0, f_exp = 1;
+ uint8_t op;
+ struct iff_stack stack = {0, 0, NULL};
+ struct lysc_feature *f, **df;
+
+ assert(c);
+
+ /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
+ for (i = j = last_not = 0; c[i]; i++) {
+ if (c[i] == '(') {
+ j++;
+ checkversion = 1;
+ continue;
+ } else if (c[i] == ')') {
+ j--;
+ continue;
+ } else if (isspace(c[i])) {
+ checkversion = 1;
+ continue;
+ }
+
+ if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
+ if (c[i + r] == '\0') {
+ LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
+ "Invalid value \"%s\" of if-feature - unexpected end of expression.", value);
+ return LY_EVALID;
+ } else if (!isspace(c[i + r])) {
+ /* feature name starting with the not/and/or */
+ last_not = 0;
+ f_size++;
+ } else if (c[i] == 'n') { /* not operation */
+ if (last_not) {
+ /* double not */
+ expr_size = expr_size - 2;
+ last_not = 0;
+ } else {
+ last_not = 1;
+ }
+ } else { /* and, or */
+ f_exp++;
+ /* not a not operation */
+ last_not = 0;
+ }
+ i += r;
+ } else {
+ f_size++;
+ last_not = 0;
+ }
+ expr_size++;
+
+ while (!isspace(c[i])) {
+ if (!c[i] || c[i] == ')') {
+ i--;
+ break;
+ }
+ i++;
+ }
+ }
+ if (j || f_exp != f_size) {
+ /* not matching count of ( and ) */
+ LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
+ "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", value);
+ return LY_EVALID;
+ }
+
+ if (checkversion || expr_size > 1) {
+ /* check that we have 1.1 module */
+ if (ctx->mod->version != LYS_VERSION_1_1) {
+ LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
+ "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", value);
+ return LY_EVALID;
+ }
+ }
+
+ /* allocate the memory */
+ iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
+ iff->features = malloc(sizeof(uint32_t) + (f_size * sizeof *iff->features));
+ stack.stack = malloc(expr_size * sizeof *stack.stack);
+ LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr || !iff->features, LOGMEM(ctx->mod->ctx), error);
+
+ *((uint32_t*)iff->features) = f_size;
+ stack.size = expr_size;
+ f_size--; expr_size--; /* used as indexes from now */
+
+ for (i--; i >= 0; i--) {
+ if (c[i] == ')') {
+ /* push it on stack */
+ iff_stack_push(&stack, LYS_IFF_RP);
+ continue;
+ } else if (c[i] == '(') {
+ /* pop from the stack into result all operators until ) */
+ while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
+ iff_setop(iff->expr, op, expr_size--);
+ }
+ continue;
+ } else if (isspace(c[i])) {
+ continue;
+ }
+
+ /* end of operator or operand -> find beginning and get what is it */
+ j = i + 1;
+ while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
+ i--;
+ }
+ i++; /* go back by one step */
+
+ if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
+ if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
+ /* double not */
+ iff_stack_pop(&stack);
+ } else {
+ /* not has the highest priority, so do not pop from the stack
+ * as in case of AND and OR */
+ iff_stack_push(&stack, LYS_IFF_NOT);
+ }
+ } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
+ /* as for OR - pop from the stack all operators with the same or higher
+ * priority and store them to the result, then push the AND to the stack */
+ while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
+ op = iff_stack_pop(&stack);
+ iff_setop(iff->expr, op, expr_size--);
+ }
+ iff_stack_push(&stack, LYS_IFF_AND);
+ } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
+ while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
+ op = iff_stack_pop(&stack);
+ iff_setop(iff->expr, op, expr_size--);
+ }
+ iff_stack_push(&stack, LYS_IFF_OR);
+ } else {
+ /* feature name, length is j - i */
+
+ /* add it to the expression */
+ iff_setop(iff->expr, LYS_IFF_F, expr_size--);
+
+ /* now get the link to the feature definition */
+ f = lysc_feature_find(ctx->mod, &c[i], j - i);
+ LY_CHECK_ERR_GOTO(!f,
+ LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
+ "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", value, j - i, &c[i]);
+ rc = LY_EINVAL,
+ error)
+ *(LY_ARRAY_INDEX(iff->features, f_size, struct lysc_feature*)) = f;
+ if (parent) {
+ /* and add itself into the dependants list */
+ //LYSP_ARRAY_NEW_RET(ctx->mod->ctx, f->depfeatures, df, LY_EMEM);
+ if (!(f->depfeatures)) {
+ f->depfeatures = malloc(sizeof(uint32_t) + sizeof *(f->depfeatures));
+ *((uint32_t*)(f->depfeatures)) = 1;
+ } else {
+ ++(*((uint32_t*)(f->depfeatures)));
+ f->depfeatures = ly_realloc(f->depfeatures,
+ sizeof(uint32_t) + (*((uint32_t*)(f->depfeatures)) * sizeof *(f->depfeatures)));
+ if (!(f->depfeatures)) {
+ ly_log(ctx->mod->ctx, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
+ return LY_EMEM;
+ };
+ }
+ (df) = (void*)((uint32_t*)((f->depfeatures) + *((uint32_t*)(f->depfeatures)) - 1) + 1);
+ memset(df, 0, sizeof *(df));
+
+ *df = parent;
+
+ /* TODO check for circular dependency */
+ }
+ f_size--;
+ }
+ }
+ while (stack.index) {
+ op = iff_stack_pop(&stack);
+ iff_setop(iff->expr, op, expr_size--);
+ }
+
+ if (++expr_size || ++f_size) {
+ /* not all expected operators and operands found */
+ LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
+ "Invalid value \"%s\" of if-feature - processing error.", value);
+ rc = LY_EINT;
+ } else {
+ rc = LY_SUCCESS;
+ }
+
+error:
+ /* cleanup */
+ iff_stack_clean(&stack);
+
+ return rc;
+}
+
+static LY_ERR
+lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature)
+{
unsigned int u;
LY_ERR ret;
- LYSP_ARRAY_NEW_RET(ctx->mod->ctx, *features, feature, LY_EMEM);
-
if (options & LYSC_OPT_FREE_SP) {
/* just switch the pointers */
feature->name = feature_p->name;
@@ -586,9 +1026,16 @@
}
feature->flags = feature_p->flags;
- for (u = 0; feature_p->iffeatures && feature_p->iffeatures[u]; ++u) {
- ret = lys_compile_iffeature(ctx, feature_p->iffeatures[u], &feature->iffeatures);
- LY_CHECK_RET(ret);
+ if (feature_p->iffeatures) {
+ /* allocate everything now */
+ feature->iffeatures = calloc(1, sizeof(uint32_t) + (*((uint32_t*)(feature_p->iffeatures)) * sizeof *feature->iffeatures));
+ *((uint32_t*)(feature->iffeatures)) = 0;
+
+ for (u = 0; u < LY_ARRAY_SIZE(feature_p->iffeatures); ++u) {
+ ret = lys_compile_iffeature(ctx, *LY_ARRAY_INDEX(feature_p->iffeatures, u, const char *), options, LY_ARRAY_INDEX(feature->iffeatures, u), feature);
+ LY_CHECK_RET(ret);
+ ++(*((uint32_t*)(feature->iffeatures)));
+ }
}
return LY_SUCCESS;
@@ -599,7 +1046,7 @@
{
struct lysc_ctx ctx = {0};
struct lysc_module *mod_c;
- void *p;
+ unsigned int u;
LY_ERR ret;
LY_CHECK_ARG_RET(NULL, sc, sp, sp->ctx, LY_EINVAL);
@@ -627,9 +1074,14 @@
}
if (sp->features) {
- LY_ARRAY_FOR(sp->features, struct lysp_feature, p) {
- ret = lys_compile_feature(&ctx, p, options, &mod_c->features);
+ /* allocate everything now */
+ mod_c->features = calloc(1, sizeof(uint32_t) + (*((uint32_t*)(sp->features)) * sizeof *mod_c->features));
+ *((uint32_t*)(mod_c->features)) = 0;
+
+ for (u = 0; u < LY_ARRAY_SIZE(sp->features); ++u) {
+ ret = lys_compile_feature(&ctx, LY_ARRAY_INDEX(sp->features, u), options, LY_ARRAY_INDEX(mod_c->features, u));
LY_CHECK_GOTO(ret != LY_SUCCESS, error);
+ ++(*((uint32_t*)(mod_c->features)));
}
}
diff --git a/src/tree_schema.h b/src/tree_schema.h
index cee90f5..2f23541 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -288,7 +288,7 @@
*/
struct lysp_feature {
const char *name; /**< feature name (mandatory) */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
@@ -300,8 +300,8 @@
*/
struct lysp_ident {
const char *name; /**< identity name (mandatory), including possible prefix */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
- const char **bases; /**< list of base identifiers (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
+ const char **bases; /**< list of base identifiers ([sized array](@ref sizedarrays)) */
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
@@ -340,7 +340,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
int64_t value; /**< enum's value or bit's position */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_ and LYS_SET_VALUE
values are allowed */
@@ -359,7 +359,7 @@
struct lysp_type_enum *enums; /**< list of enum-stmts ([sized array](@ref sizedarrays)) - enum */
struct lysp_type_enum *bits; /**< list of bit-stmts ([sized array](@ref sizedarrays)) - bits */
const char *path; /**< path - leafref */
- const char **bases; /**< list of base identifiers (NULL-terminated) - identityref */
+ const char **bases; /**< list of base identifiers ([sized array](@ref sizedarrays)) - identityref */
struct lysp_type *types; /**< list of sub-types ([sized array](@ref sizedarrays)) - union */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
@@ -415,10 +415,10 @@
const char *nodeid; /**< target descendant schema nodeid (mandatory) */
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_restr *musts; /**< list of must restrictions ([sized array](@ref sizedarrays)) */
const char *presence; /**< presence description */
- const char **dflts; /**< list of default values (NULL-terminated) */
+ const char **dflts; /**< list of default values ([sized array](@ref sizedarrays)) */
uint32_t min; /**< min-elements constraint */
uint32_t max; /**< max-elements constraint, 0 means unbounded */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
@@ -433,7 +433,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_node *child; /**< list of data nodes (linked list) */
struct lysp_action *actions; /**< list of actions ([sized array](@ref sizedarrays)) */
struct lysp_notif *notifs; /**< list of notifications ([sized array](@ref sizedarrays)) */
@@ -466,8 +466,8 @@
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
const char *units; /**< units of the values */
struct lysp_restr *musts; /**< list of must restrictions ([sized array](@ref sizedarrays)) */
- const char **uniques; /**< list of uniques specifications (NULL-terminated) */
- const char **dflts; /**< list of default values (NULL-terminated) */
+ const char **uniques; /**< list of uniques specifications ([sized array](@ref sizedarrays)) */
+ const char **dflts; /**< list of default values ([sized array](@ref sizedarrays)) */
uint16_t flags; /**< [schema node flags](@ref snodeflags) */
uint32_t min; /**< min-elements constraint */
uint32_t max; /**< max-elements constraint, 0 means unbounded */
@@ -479,8 +479,8 @@
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
const char *units; /**< units of the values */
struct lysp_restr *musts; /**< list of must restrictions ([sized array](@ref sizedarrays)) */
- const char **uniques; /**< list of uniques specifications (NULL-terminated) */
- const char **dflts; /**< list of default values (NULL-terminated) */
+ const char **uniques; /**< list of uniques specifications ([sized array](@ref sizedarrays)) */
+ const char **dflts; /**< list of default values ([sized array](@ref sizedarrays)) */
uint16_t flags; /**< [schema node flags](@ref snodeflags) */
};
@@ -545,7 +545,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
};
@@ -560,7 +560,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
/* container */
@@ -581,7 +581,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
/* leaf */
@@ -599,14 +599,14 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
/* leaf-list */
struct lysp_restr *musts; /**< list of must restrictions ([sized array](@ref sizedarrays)) */
struct lysp_type type; /**< type of the leaf node (mandatory) */
const char *units; /**< units of the leaf's type */
- const char **dflts; /**< list of default values (NULL-terminated) */
+ const char **dflts; /**< list of default values ([sized array](@ref sizedarrays)) */
uint32_t min; /**< min-elements constraint */
uint32_t max; /**< max-elements constraint, 0 means unbounded */
};
@@ -619,7 +619,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
/* list */
@@ -630,7 +630,7 @@
struct lysp_node *child; /**< list of data nodes (linked list) */
struct lysp_action *actions; /**< list of actions ([sized array](@ref sizedarrays)) */
struct lysp_notif *notifs; /**< list of notifications ([sized array](@ref sizedarrays)) */
- const char **uniques; /**< list of uniques specifications (NULL-terminated) */
+ const char **uniques; /**< list of unique specifications ([sized array](@ref sizedarrays)) */
uint32_t min; /**< min-elements constraint */
uint32_t max; /**< max-elements constraint, 0 means unbounded */
};
@@ -643,7 +643,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
/* choice */
@@ -659,7 +659,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
/* case */
@@ -674,7 +674,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
/* anyxml/anydata */
@@ -689,7 +689,7 @@
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
struct lysp_when *when; /**< when statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
/* uses */
@@ -715,7 +715,7 @@
const char *name; /**< grouping name reference (mandatory) */
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_tpdf *typedefs; /**< list of typedefs ([sized array](@ref sizedarrays)) */
struct lysp_grp *groupings; /**< list of groupings ([sized array](@ref sizedarrays)) */
struct lysp_action_inout *input; /**< RPC's/Action's input */
@@ -731,7 +731,7 @@
const char *name; /**< grouping name reference (mandatory) */
const char *dsc; /**< description statement */
const char *ref; /**< reference statement */
- const char **iffeatures; /**< list of if-feature expressions (NULL-terminated) */
+ const char **iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_restr *musts; /**< list of must restrictions ([sized array](@ref sizedarrays)) */
struct lysp_tpdf *typedefs; /**< list of typedefs ([sized array](@ref sizedarrays)) */
struct lysp_grp *groupings; /**< list of groupings ([sized array](@ref sizedarrays)) */
@@ -800,6 +800,15 @@
void lysp_module_free(struct lysp_module *module);
/**
+ * @brief YANG import-stmt
+ */
+struct lysc_import {
+ struct lysc_module *module; /**< link to the imported module */
+ const char *prefix; /**< prefix for the data from the imported schema (mandatory) */
+ struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
+};
+
+/**
* @brief YANG when-stmt
*/
struct lysc_when {
@@ -812,16 +821,33 @@
*/
struct lysc_feature {
const char *name; /**< feature name (mandatory) */
- struct lysc_feature *depfeatures;/**< list of other features depending on this one ([sized array](@ref sizedarrays)) */
+ struct lysc_feature **depfeatures;/**< list of pointers to other features depending on this one ([sized array](@ref sizedarrays)) */
struct lysc_iffeature *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
struct lysp_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* and
#LYS_FENABLED values allowed */
};
+/**
+ * @defgroup ifftokens if-feature expression tokens
+ * Tokens of if-feature expression used in ::lysc_iffeature#expr
+ *
+ * @{
+ */
+#define LYS_IFF_NOT 0x00 /**< operand "not" */
+#define LYS_IFF_AND 0x01 /**< operand "and" */
+#define LYS_IFF_OR 0x02 /**< operand "or" */
+#define LYS_IFF_F 0x03 /**< feature */
+/**
+ * @}
+ */
+
+/**
+ * @brief Compiled YANG if-feature-stmt
+ */
struct lysc_iffeature {
- struct lysc_feature *features; /**< array of pointers to the features used in expression, size depends on content of expr */
- uint32_t expr[]; /**< 2bits array describing the if-feature expression in prefix format */
+ uint8_t *expr; /**< 2bits array describing the if-feature expression in prefix format, see @ref ifftokens */
+ struct lysc_feature **features; /**< array of pointers to the features used in expression ([sized array](@ref sizedarrays)) */
};
/**
@@ -849,6 +875,7 @@
const char *name; /**< name of the module (mandatory) */
const char *ns; /**< namespace of the module (mandatory) */
const char *prefix; /**< module prefix (mandatory) */
+ struct lysc_import *imports; /**< list of imported modules ([sized array](@ref sizedarrays)) */
struct lysc_feature *features; /**< list of feature definitions ([sized array](@ref sizedarrays)) */
@@ -861,6 +888,25 @@
};
/**
+ * @brief Get how the if-feature statement currently evaluates.
+ *
+ * @param[in] iff Compiled if-feature statement to evaluate.
+ * @return If the statement evaluates to true, 1 is returned. 0 is returned when the statement evaluates to false.
+ */
+int lysc_iffeature_value(const struct lysc_iffeature *iff);
+
+/**
+ * @brief Get the current status of the provided feature.
+ *
+ * @param[in] feature Compiled feature statement to examine.
+ * @return
+ * - 1 if feature is enabled,
+ * - 0 if feature is disabled,
+ * - -1 in case of error (invalid argument)
+ */
+int lysc_feature_value(const struct lysc_feature *feature);
+
+/**
* @brief Available YANG schema tree structures representing YANG module.
*/
struct lys_module {
@@ -868,6 +914,39 @@
struct lysc_module *compiled; /**< Compiled and fully validated YANG schema tree for data parsing */
};
+/**
+ * @brief Enable specified feature in the module
+ *
+ * By default, when the module is loaded by libyang parser, all features are disabled.
+ *
+ * @param[in] module Module where the feature will be enabled.
+ * @param[in] feature Name of the feature to enable. To enable all features at once, use asterisk (`*`) character.
+ * @return LY_ERR value.
+ */
+LY_ERR lys_feature_enable(struct lys_module *module, const char *feature);
+
+/**
+ * @brief Disable specified feature in the module
+ *
+ * By default, when the module is loaded by libyang parser, all features are disabled.
+ *
+ * @param[in] module Module where the feature will be disabled.
+ * @param[in] feature Name of the feature to disable. To disable all features at once, use asterisk (`*`) character.
+ * @return LY_ERR value
+ */
+LY_ERR lys_feature_disable(struct lys_module *module, const char *feature);
+
+/**
+ * @brief Get the current status of the specified feature in the module.
+ *
+ * @param[in] module Module where the feature is defined.
+ * @param[in] feature Name of the feature to inspect.
+ * @return
+ * - 1 if feature is enabled,
+ * - 0 if feature is disabled,
+ * - -1 in case of error (e.g. feature is not defined or invalid arguments)
+ */
+int lys_feature_value(const struct lys_module *module, const char *feature);
/**
* @brief Load a schema into the specified context.
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_helpers.c
index 852b485..c08068a 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_helpers.c
@@ -105,3 +105,25 @@
memcpy(LY_ARRAY_INDEX(revs, r), &rev, sizeof rev);
}
}
+
+struct lysc_module *
+lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len)
+{
+ struct lysc_import *imp;
+
+ assert(mod);
+
+ if (!strncmp(mod->prefix, prefix, len) && mod->prefix[len] == '\0') {
+ /* it is the prefix of the module itself */
+ return mod;
+ }
+
+ /* search in imports */
+ LY_ARRAY_FOR(mod->imports, struct lysc_import, imp) {
+ if (!strncmp(imp->prefix, prefix, len) && mod->prefix[len] == '\0') {
+ return imp->module;
+ }
+ }
+
+ return NULL;
+}
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index 0c3b489..da8789c 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -73,6 +73,16 @@
void lysp_sort_revisions(struct lysp_revision *revs);
/**
+ * @brief Find the module referenced by prefix in the provided mod.
+ *
+ * @param[in] mod Schema module where the prefix was used.
+ * @param[in] prefix Prefix used to reference a module.
+ * @param[in] len Length of the prefix since it is not necessary NULL-terminated.
+ * @return Pointer to the module or NULL if the module is not found.
+ */
+struct lysc_module *lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len);
+
+/**
* @brief Free the schema structure. It just frees, it does not remove the schema from its context.
* @param[in,out] module Schema module structure to free.
* @param[in] private_destructor Function to remove private data from the compiled schema tree.
diff --git a/tests/src/test_tree_schema_compile.c b/tests/src/test_tree_schema_compile.c
index 5f30a9d..8e72276 100644
--- a/tests/src/test_tree_schema_compile.c
+++ b/tests/src/test_tree_schema_compile.c
@@ -75,9 +75,11 @@
const char *str;
struct ly_ctx *ctx;
struct lys_module mod = {0};
+ struct lysc_feature *f;
+ struct lysc_iffeature *iff;
str = "module test {namespace urn:test; prefix t;"
- "feature f1;}";
+ "feature f1;feature f2 {if-feature f1;}}";
assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, NULL));
@@ -89,6 +91,17 @@
assert_non_null(mod.compiled);
assert_ptr_equal(mod.parsed->name, mod.compiled->name);
assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
+ /* features */
+ assert_non_null(mod.compiled->features);
+ assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
+ f = LY_ARRAY_INDEX(mod.compiled->features, 1);
+ assert_non_null(f->iffeatures);
+ assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
+ iff = LY_ARRAY_INDEX(f->iffeatures, 0);
+ assert_non_null(iff->expr);
+ assert_non_null(iff->features);
+ assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
+ assert_ptr_equal(LY_ARRAY_INDEX(mod.compiled->features, 0), *LY_ARRAY_INDEX(iff->features, 0, struct lysc_feature*));
lysc_module_free(mod.compiled, NULL);
@@ -100,6 +113,7 @@
lysc_module_free(mod.compiled, NULL);
mod.compiled = NULL;
+ /* submodules cannot be compiled directly */
str = "submodule test {belongs-to xxx {prefix x;}}";
assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed));
assert_int_equal(LY_EINVAL, lys_compile(mod.parsed, 0, &mod.compiled));
@@ -110,11 +124,89 @@
ly_ctx_destroy(ctx, NULL);
}
+static void
+test_feature(void **state)
+{
+ (void) state; /* unused */
+
+ struct ly_ctx *ctx;
+ struct lys_module mod = {0};
+ const char *str;
+ struct lysc_feature *f, *f1;
+
+ str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
+ "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
+ "feature f4 {if-feature \"f1 or f2\";}\n"
+ "feature f5 {if-feature \"f1 and f2\";}\n"
+ "feature f6 {if-feature \"not f1\";}\n"
+ "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}}";
+
+ assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
+ assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed));
+ assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, 0, &mod.compiled));
+ assert_non_null(mod.compiled);
+ assert_non_null(mod.compiled->features);
+ assert_int_equal(7, LY_ARRAY_SIZE(mod.compiled->features));
+ /* all features are disabled by default */
+ LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
+ assert_int_equal(0, lysc_feature_value(f));
+ }
+ /* enable f1 */
+ assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
+ f1 = LY_ARRAY_INDEX(mod.compiled->features, 0);
+ assert_int_equal(1, lysc_feature_value(f1));
+
+ /* enable f4 */
+ f = LY_ARRAY_INDEX(mod.compiled->features, 3);
+ assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f4"));
+ assert_int_equal(1, lysc_feature_value(f));
+
+ /* enable f5 - no possible since f2 is disabled */
+ f = LY_ARRAY_INDEX(mod.compiled->features, 4);
+ assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f5"));
+ logbuf_assert("Feature \"f5\" cannot be enabled since it is disabled by its if-feature condition(s).");
+ assert_int_equal(0, lysc_feature_value(f));
+
+ /* first enable f2, so f5 can be enabled then */
+ assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
+ assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f5"));
+ assert_int_equal(1, lysc_feature_value(f));
+
+ /* f1 is enabled, so f6 cannot be enabled */
+ f = LY_ARRAY_INDEX(mod.compiled->features, 5);
+ assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
+ logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
+ assert_int_equal(0, lysc_feature_value(f));
+
+ /* so disable f1 - f5 will became also disabled */
+ assert_int_equal(1, lysc_feature_value(f1));
+ f = LY_ARRAY_INDEX(mod.compiled->features, 4);
+ assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
+ assert_int_equal(0, lysc_feature_value(f1));
+ assert_int_equal(0, lysc_feature_value(f));
+ /* while f4 is stille enabled */
+ assert_int_equal(1, lysc_feature_value(LY_ARRAY_INDEX(mod.compiled->features, 3)));
+ /* and finally f6 can be enabled */
+ f = LY_ARRAY_INDEX(mod.compiled->features, 5);
+ assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
+ assert_int_equal(1, lysc_feature_value(f));
+
+ /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
+ assert_int_equal(1, lysc_iffeature_value(LY_ARRAY_INDEX(LY_ARRAY_INDEX(mod.compiled->features, 6, struct lysc_feature)->iffeatures, 0)));
+
+ lysc_module_free(mod.compiled, NULL);
+ lysp_module_free(mod.parsed);
+ ly_ctx_destroy(ctx, NULL);
+}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup(test_module, logger_setup),
+ cmocka_unit_test_setup(test_feature, logger_setup),
};
return cmocka_run_group_tests(tests, NULL, NULL);
diff --git a/tests/src/test_tree_schema_helpers.c b/tests/src/test_tree_schema_helpers.c
index 53cbded..67f4138 100644
--- a/tests/src/test_tree_schema_helpers.c
+++ b/tests/src/test_tree_schema_helpers.c
@@ -43,39 +43,74 @@
return 0;
}
+#if ENABLE_LOGGER_CHECKING
+# define logbuf_assert(str) assert_string_equal(logbuf, str)
+#else
+# define logbuf_assert(str)
+#endif
+
static void
test_date(void **state)
{
(void) state; /* unused */
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
- assert_string_equal(logbuf, "Invalid argument date (lysp_check_date()).");
+ logbuf_assert("Invalid argument date (lysp_check_date()).");
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
- assert_string_equal(logbuf, "Invalid argument date_len (lysp_check_date()).");
+ logbuf_assert("Invalid argument date_len (lysp_check_date()).");
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"nonsencexx\" of \"date\".");
+ logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"123x-11-11\" of \"date\".");
+ logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018-13-11\" of \"date\".");
+ logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018-11-41\" of \"date\".");
+ logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018-02-29\" of \"date\".");
+ logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018.02-28\" of \"date\".");
+ logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018-02.28\" of \"date\".");
+ logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
}
+static void
+test_revisions(void **state)
+{
+ (void) state; /* unused */
+
+ struct lysp_revision *revs = NULL, *rev;
+
+ /* no error, it just does nothing */
+ lysp_sort_revisions(NULL);
+ logbuf_assert("");
+
+ /* revisions are stored in wrong order - the newest is the last */
+ LYSP_ARRAY_NEW_RET(NULL, revs, rev,);
+ strcpy(rev->rev, "2018-01-01");
+ LYSP_ARRAY_NEW_RET(NULL, revs, rev,);
+ strcpy(rev->rev, "2018-12-31");
+
+ assert_int_equal(2, LY_ARRAY_SIZE(revs));
+ assert_string_equal("2018-01-01", LY_ARRAY_INDEX(revs, 0));
+ assert_string_equal("2018-12-31", LY_ARRAY_INDEX(revs, 1));
+ /* the order should be fixed, so the newest revision will be the first in the array */
+ lysp_sort_revisions(revs);
+ assert_string_equal("2018-12-31", LY_ARRAY_INDEX(revs, 0));
+ assert_string_equal("2018-01-01", LY_ARRAY_INDEX(revs, 1));
+
+ free(revs);
+}
+
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup(test_date, logger_setup),
+ cmocka_unit_test_setup(test_revisions, logger_setup),
};
return cmocka_run_group_tests(tests, NULL, NULL);
diff --git a/tests/src/test_xml.c b/tests/src/test_xml.c
index cbabebb..568e55e 100644
--- a/tests/src/test_xml.c
+++ b/tests/src/test_xml.c
@@ -156,6 +156,9 @@
str += 12;
assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
logbuf_assert("Opening and closing elements tag missmatch (\"element>\"). Line number 1.");
+ str = "</yin:element/>";
+ assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
+ logbuf_assert("Unexpected data \"/>\" in closing element tag. Line number 1.");
lyxml_context_clear(&ctx);
/* UTF8 characters */