summaryrefslogtreecommitdiffstats
path: root/src/parser_common.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 /src/parser_common.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 'src/parser_common.c')
-rw-r--r--src/parser_common.c176
1 files changed, 155 insertions, 21 deletions
diff --git a/src/parser_common.c b/src/parser_common.c
index 6fe068b..3215275 100644
--- a/src/parser_common.c
+++ b/src/parser_common.c
@@ -46,6 +46,7 @@
#include "parser_data.h"
#include "path.h"
#include "plugins_exts/metadata.h"
+#include "schema_compile_node.h"
#include "schema_features.h"
#include "set.h"
#include "tree.h"
@@ -65,6 +66,50 @@ lyd_ctx_free(struct lyd_ctx *lydctx)
}
LY_ERR
+lyd_parser_notif_eventtime_validate(const struct lyd_node *node)
+{
+ LY_ERR rc = LY_SUCCESS;
+ struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node);
+ struct lysc_ctx cctx;
+ const struct lys_module *mod;
+ LY_ARRAY_COUNT_TYPE u;
+ struct ly_err_item *err = NULL;
+ struct lysp_type *type_p = NULL;
+ struct lysc_pattern **patterns = NULL;
+ const char *value;
+
+ LYSC_CTX_INIT_CTX(cctx, ctx);
+
+ /* get date-and-time parsed type */
+ mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
+ assert(mod);
+ LY_ARRAY_FOR(mod->parsed->typedefs, u) {
+ if (!strcmp(mod->parsed->typedefs[u].name, "date-and-time")) {
+ type_p = &mod->parsed->typedefs[u].type;
+ break;
+ }
+ }
+ assert(type_p);
+
+ /* compile patterns */
+ assert(type_p->patterns);
+ LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
+
+ /* validate */
+ value = lyd_get_value(node);
+ rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
+
+cleanup:
+ FREE_ARRAY(&cctx.free_ctx, patterns, lysc_pattern_free);
+ if (rc && err) {
+ LOGVAL_ERRITEM(ctx, err);
+ ly_err_free(err);
+ LOGVAL(ctx, LYVE_DATA, "Invalid \"eventTime\" in the notification.");
+ }
+ return rc;
+}
+
+LY_ERR
lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op)
{
const struct lyd_node *iter;
@@ -112,6 +157,63 @@ lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, stru
return LY_SUCCESS;
}
+const struct lysc_node *
+lyd_parser_node_schema(const struct lyd_node *node)
+{
+ uint32_t i;
+ const struct lyd_node *iter;
+ const struct lysc_node *schema = NULL;
+ const struct lys_module *mod;
+
+ if (!node) {
+ return NULL;
+ } else if (node->schema) {
+ /* simplest case */
+ return node->schema;
+ }
+
+ /* find the first schema node in the parsed nodes */
+ i = ly_log_location_dnode_count();
+ if (i) {
+ do {
+ --i;
+ if (ly_log_location_dnode(i)->schema) {
+ /* this node is processed */
+ schema = ly_log_location_dnode(i)->schema;
+ ++i;
+ break;
+ }
+ } while (i);
+ }
+
+ /* get schema node of an opaque node */
+ do {
+ /* get next data node */
+ if (i == ly_log_location_dnode_count()) {
+ iter = node;
+ } else {
+ iter = ly_log_location_dnode(i);
+ }
+ assert(!iter->schema);
+
+ /* get module */
+ mod = lyd_node_module(iter);
+ if (!mod) {
+ /* unknown module, no schema node */
+ schema = NULL;
+ break;
+ }
+
+ /* get schema node */
+ schema = lys_find_child(schema, mod, LYD_NAME(iter), 0, 0, 0);
+
+ /* move to the descendant */
+ ++i;
+ } while (schema && (iter != node));
+
+ return schema;
+}
+
LY_ERR
lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode)
{
@@ -180,9 +282,16 @@ LY_ERR
lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const void *value, size_t value_len,
ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node)
{
+ LY_ERR r;
ly_bool incomplete;
- LY_CHECK_RET(lyd_create_term(schema, value, value_len, dynamic, format, prefix_data, hints, &incomplete, node));
+ if ((r = lyd_create_term(schema, value, value_len, 1, dynamic, format, prefix_data, hints, &incomplete, node))) {
+ if (lydctx->data_ctx->ctx != schema->module->ctx) {
+ /* move errors to the main context */
+ ly_err_move(schema->module->ctx, (struct ly_ctx *)lydctx->data_ctx->ctx);
+ }
+ return r;
+ }
if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL));
@@ -195,6 +304,8 @@ lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct l
const char *name, size_t name_len, const void *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node)
{
+ LY_ERR rc = LY_SUCCESS;
+ char *dpath = NULL, *path = NULL;
ly_bool incomplete;
struct lyd_meta *first = NULL;
@@ -203,11 +314,20 @@ lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct l
first = *meta;
}
- LY_CHECK_RET(lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, dynamic, format, prefix_data,
- hints, ctx_node, 0, &incomplete));
+ /* generate path to the metadata */
+ LY_CHECK_RET(ly_vlog_build_data_path(lydctx->data_ctx->ctx, &dpath));
+ if (asprintf(&path, "%s/@%s:%.*s", dpath, mod->name, (int)name_len, name) == -1) {
+ LOGMEM(lydctx->data_ctx->ctx);
+ rc = LY_EMEM;
+ goto cleanup;
+ }
+ LOG_LOCSET(NULL, NULL, path, NULL);
+
+ LY_CHECK_GOTO(rc = lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, 1, dynamic, format,
+ prefix_data, hints, ctx_node, 0, &incomplete), cleanup);
if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
- LY_CHECK_RET(ly_set_add(&lydctx->meta_types, *meta, 1, NULL));
+ LY_CHECK_GOTO(rc = ly_set_add(&lydctx->meta_types, *meta, 1, NULL), cleanup);
}
if (first) {
@@ -215,7 +335,11 @@ lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct l
*meta = first;
}
- return LY_SUCCESS;
+cleanup:
+ LOG_LOCBACK(0, 0, 1, 0);
+ free(dpath);
+ free(path);
+ return rc;
}
LY_ERR
@@ -387,6 +511,16 @@ lysp_stmt_validate_value(struct lysp_ctx *ctx, enum yang_arg val_type, const cha
uint32_t c;
size_t utf8_char_len;
+ if (!val) {
+ if (val_type == Y_MAYBE_STR_ARG) {
+ /* fine */
+ return LY_SUCCESS;
+ }
+
+ LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Missing an expected string.");
+ return LY_EVALID;
+ }
+
while (*val) {
LY_CHECK_ERR_RET(ly_getutf8(&val, &c, &utf8_char_len),
LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
@@ -605,7 +739,7 @@ lysp_stmt_text_fields(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, const
static LY_ERR
lysp_stmt_status(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
if (*flags & LYS_STATUS_MASK) {
LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
@@ -698,7 +832,7 @@ lysp_stmt_when(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_w
static LY_ERR
lysp_stmt_config(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
if (*flags & LYS_CONFIG_MASK) {
LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
@@ -744,7 +878,7 @@ static LY_ERR
lysp_stmt_mandatory(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags,
struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
if (*flags & LYS_MAND_MASK) {
LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
@@ -914,7 +1048,7 @@ static LY_ERR
lysp_stmt_type_enum_value_pos(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, int64_t *value, uint16_t *flags,
struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
char *ptr = NULL;
long long num = 0;
unsigned long long unum = 0;
@@ -949,7 +1083,7 @@ lysp_stmt_type_enum_value_pos(struct lysp_ctx *ctx, const struct lysp_stmt *stmt
}
}
/* we have not parsed the whole argument */
- if ((size_t)(ptr - stmt->arg) != arg_len) {
+ if (ptr - stmt->arg != arg_len) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw));
goto error;
}
@@ -1056,7 +1190,7 @@ lysp_stmt_type_fracdigits(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, ui
struct lysp_ext_instance **exts)
{
char *ptr;
- size_t arg_len;
+ int arg_len;
unsigned long long num;
if (*fracdig) {
@@ -1074,7 +1208,7 @@ lysp_stmt_type_fracdigits(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, ui
errno = 0;
num = strtoull(stmt->arg, &ptr, LY_BASE_DEC);
/* we have not parsed the whole argument */
- if ((size_t)(ptr - stmt->arg) != arg_len) {
+ if (ptr - stmt->arg != arg_len) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits");
return LY_EVALID;
}
@@ -1112,7 +1246,7 @@ static LY_ERR
lysp_stmt_type_reqinstance(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *reqinst, uint16_t *flags,
struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
if (*flags & LYS_SET_REQINST) {
LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
@@ -1155,7 +1289,7 @@ static LY_ERR
lysp_stmt_type_pattern_modifier(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, const char **pat,
struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
char *buf;
if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
@@ -1338,7 +1472,7 @@ lysp_stmt_yangver(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *v
} else if (!strcmp(stmt->arg, "1.1")) {
*version = LYS_VERSION_1_1;
} else {
- LOGVAL_PARSER(ctx, LY_VCODE_INVAL, strlen(stmt->arg), stmt->arg, "yang-version");
+ LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)strlen(stmt->arg), stmt->arg, "yang-version");
return LY_EVALID;
}
@@ -1418,7 +1552,7 @@ lysp_stmt_yinelem(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *
} else if (!strcmp(stmt->arg, "false")) {
*flags |= LYS_YINELEM_FALSE;
} else {
- LOGVAL_PARSER(ctx, LY_VCODE_INVAL, strlen(stmt->arg), stmt->arg, "yin-element");
+ LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)strlen(stmt->arg), stmt->arg, "yin-element");
return LY_EVALID;
}
@@ -1946,7 +2080,7 @@ static LY_ERR
lysp_stmt_maxelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32_t *max, uint16_t *flags,
struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
char *ptr;
unsigned long long num;
@@ -1969,7 +2103,7 @@ lysp_stmt_maxelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32
errno = 0;
num = strtoull(stmt->arg, &ptr, LY_BASE_DEC);
/* we have not parsed the whole argument */
- if ((size_t)(ptr - stmt->arg) != arg_len) {
+ if (ptr - stmt->arg != arg_len) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "max-elements");
return LY_EVALID;
}
@@ -2012,7 +2146,7 @@ static LY_ERR
lysp_stmt_minelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32_t *min, uint16_t *flags,
struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
char *ptr;
unsigned long long num;
@@ -2034,7 +2168,7 @@ lysp_stmt_minelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32
errno = 0;
num = strtoull(stmt->arg, &ptr, LY_BASE_DEC);
/* we have not parsed the whole argument */
- if ((size_t)(ptr - stmt->arg) != arg_len) {
+ if (ptr - stmt->arg != arg_len) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "min-elements");
return LY_EVALID;
}
@@ -2070,7 +2204,7 @@ lysp_stmt_minelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32
static LY_ERR
lysp_stmt_orderedby(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts)
{
- size_t arg_len;
+ int arg_len;
if (*flags & LYS_ORDBY_MASK) {
LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");