Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 2 | * @file in.c |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 4 | * @brief libyang input functions. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 17 | #include "in.h" |
| 18 | #include "in_internal.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 19 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <limits.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 23 | #include <stdint.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 26 | #include <string.h> |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 27 | #include <unistd.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 28 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 29 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 30 | #include "compat.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 31 | #include "dict.h" |
| 32 | #include "log.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 33 | #include "parser_data.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 34 | #include "parser_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 35 | #include "set.h" |
| 36 | #include "tree_data.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 37 | #include "tree_data_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 38 | #include "tree_schema.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 39 | #include "tree_schema_internal.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 40 | |
| 41 | API LY_IN_TYPE |
| 42 | ly_in_type(const struct ly_in *in) |
| 43 | { |
| 44 | LY_CHECK_ARG_RET(NULL, in, LY_IN_ERROR); |
| 45 | return in->type; |
| 46 | } |
| 47 | |
| 48 | API LY_ERR |
| 49 | ly_in_new_fd(int fd, struct ly_in **in) |
| 50 | { |
| 51 | size_t length; |
| 52 | char *addr; |
| 53 | |
| 54 | LY_CHECK_ARG_RET(NULL, fd >= 0, in, LY_EINVAL); |
| 55 | |
| 56 | LY_CHECK_RET(ly_mmap(NULL, fd, &length, (void **)&addr)); |
| 57 | if (!addr) { |
| 58 | LOGERR(NULL, LY_EINVAL, "Empty input file."); |
| 59 | return LY_EINVAL; |
| 60 | } |
| 61 | |
| 62 | *in = calloc(1, sizeof **in); |
| 63 | LY_CHECK_ERR_RET(!*in, LOGMEM(NULL); ly_munmap(addr, length), LY_EMEM); |
| 64 | |
| 65 | (*in)->type = LY_IN_FD; |
| 66 | (*in)->method.fd = fd; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 67 | (*in)->current = (*in)->start = (*in)->func_start = addr; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 68 | (*in)->length = length; |
| 69 | |
| 70 | return LY_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | API int |
| 74 | ly_in_fd(struct ly_in *in, int fd) |
| 75 | { |
| 76 | int prev_fd; |
| 77 | size_t length; |
| 78 | const char *addr; |
| 79 | |
| 80 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FD, -1); |
| 81 | |
| 82 | prev_fd = in->method.fd; |
| 83 | |
| 84 | if (fd != -1) { |
| 85 | LY_CHECK_RET(ly_mmap(NULL, fd, &length, (void **)&addr), -1); |
| 86 | if (!addr) { |
| 87 | LOGERR(NULL, LY_EINVAL, "Empty input file."); |
| 88 | return -1; |
| 89 | } |
| 90 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 91 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 92 | |
| 93 | in->method.fd = fd; |
| 94 | in->current = in->start = addr; |
| 95 | in->length = length; |
| 96 | } |
| 97 | |
| 98 | return prev_fd; |
| 99 | } |
| 100 | |
| 101 | API LY_ERR |
| 102 | ly_in_new_file(FILE *f, struct ly_in **in) |
| 103 | { |
| 104 | LY_CHECK_ARG_RET(NULL, f, in, LY_EINVAL); |
| 105 | |
| 106 | LY_CHECK_RET(ly_in_new_fd(fileno(f), in)); |
| 107 | |
| 108 | /* convert the LY_IN_FD input handler into the LY_IN_FILE */ |
| 109 | (*in)->type = LY_IN_FILE; |
| 110 | (*in)->method.f = f; |
| 111 | |
| 112 | return LY_SUCCESS; |
| 113 | } |
| 114 | |
| 115 | API FILE * |
| 116 | ly_in_file(struct ly_in *in, FILE *f) |
| 117 | { |
| 118 | FILE *prev_f; |
| 119 | |
| 120 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FILE, NULL); |
| 121 | |
| 122 | prev_f = in->method.f; |
| 123 | |
| 124 | if (f) { |
| 125 | /* convert LY_IN_FILE handler into LY_IN_FD to be able to update it via ly_in_fd() */ |
| 126 | in->type = LY_IN_FD; |
| 127 | in->method.fd = fileno(prev_f); |
| 128 | if (ly_in_fd(in, fileno(f)) == -1) { |
| 129 | in->type = LY_IN_FILE; |
| 130 | in->method.f = prev_f; |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | /* if success, convert the result back */ |
| 135 | in->type = LY_IN_FILE; |
| 136 | in->method.f = f; |
| 137 | } |
| 138 | |
| 139 | return prev_f; |
| 140 | } |
| 141 | |
| 142 | API LY_ERR |
| 143 | ly_in_new_memory(const char *str, struct ly_in **in) |
| 144 | { |
| 145 | LY_CHECK_ARG_RET(NULL, str, in, LY_EINVAL); |
| 146 | |
| 147 | *in = calloc(1, sizeof **in); |
| 148 | LY_CHECK_ERR_RET(!*in, LOGMEM(NULL), LY_EMEM); |
| 149 | |
| 150 | (*in)->type = LY_IN_MEMORY; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 151 | (*in)->start = (*in)->current = (*in)->func_start = str; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 152 | |
| 153 | return LY_SUCCESS; |
| 154 | } |
| 155 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 156 | API const char * |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 157 | ly_in_memory(struct ly_in *in, const char *str) |
| 158 | { |
| 159 | const char *data; |
| 160 | |
| 161 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_MEMORY, NULL); |
| 162 | |
| 163 | data = in->current; |
| 164 | |
| 165 | if (str) { |
| 166 | in->start = in->current = str; |
| 167 | } |
| 168 | |
| 169 | return data; |
| 170 | } |
| 171 | |
| 172 | API LY_ERR |
| 173 | ly_in_reset(struct ly_in *in) |
| 174 | { |
| 175 | LY_CHECK_ARG_RET(NULL, in, LY_EINVAL); |
| 176 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 177 | in->current = in->func_start = in->start; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 178 | return LY_SUCCESS; |
| 179 | } |
| 180 | |
| 181 | API LY_ERR |
| 182 | ly_in_new_filepath(const char *filepath, size_t len, struct ly_in **in) |
| 183 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 184 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 185 | char *fp; |
| 186 | int fd; |
| 187 | |
| 188 | LY_CHECK_ARG_RET(NULL, filepath, in, LY_EINVAL); |
| 189 | |
| 190 | if (len) { |
| 191 | fp = strndup(filepath, len); |
| 192 | } else { |
| 193 | fp = strdup(filepath); |
| 194 | } |
| 195 | |
| 196 | fd = open(fp, O_RDONLY); |
Michal Vasko | f2eb8af | 2020-07-14 12:22:40 +0200 | [diff] [blame] | 197 | LY_CHECK_ERR_RET(fd == -1, LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", fp, strerror(errno)); free(fp), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 198 | LY_ESYS); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 199 | |
| 200 | LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, in), free(fp), ret); |
| 201 | |
| 202 | /* convert the LY_IN_FD input handler into the LY_IN_FILE */ |
| 203 | (*in)->type = LY_IN_FILEPATH; |
| 204 | (*in)->method.fpath.fd = fd; |
| 205 | (*in)->method.fpath.filepath = fp; |
| 206 | |
| 207 | return LY_SUCCESS; |
| 208 | } |
| 209 | |
| 210 | API const char * |
| 211 | ly_in_filepath(struct ly_in *in, const char *filepath, size_t len) |
| 212 | { |
| 213 | int fd, prev_fd; |
| 214 | char *fp = NULL; |
| 215 | |
| 216 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FILEPATH, filepath ? NULL : ((void *)-1)); |
| 217 | |
| 218 | if (!filepath) { |
| 219 | return in->method.fpath.filepath; |
| 220 | } |
| 221 | |
| 222 | if (len) { |
| 223 | fp = strndup(filepath, len); |
| 224 | } else { |
| 225 | fp = strdup(filepath); |
| 226 | } |
| 227 | |
| 228 | /* replace filepath */ |
| 229 | fd = open(fp, O_RDONLY); |
| 230 | LY_CHECK_ERR_RET(!fd, LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", fp, strerror(errno)); free(fp), NULL); |
| 231 | |
| 232 | /* convert LY_IN_FILEPATH handler into LY_IN_FD to be able to update it via ly_in_fd() */ |
| 233 | in->type = LY_IN_FD; |
| 234 | prev_fd = ly_in_fd(in, fd); |
| 235 | LY_CHECK_ERR_RET(prev_fd == -1, in->type = LY_IN_FILEPATH; free(fp), NULL); |
| 236 | |
| 237 | /* and convert the result back */ |
| 238 | in->type = LY_IN_FILEPATH; |
| 239 | close(prev_fd); |
| 240 | free(in->method.fpath.filepath); |
| 241 | in->method.fpath.fd = fd; |
| 242 | in->method.fpath.filepath = fp; |
| 243 | |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | void |
| 248 | lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath) |
| 249 | { |
| 250 | char path[PATH_MAX]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 251 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 252 | #ifndef __APPLE__ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 253 | char proc_path[32]; |
| 254 | int len; |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 255 | #endif |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 256 | |
| 257 | LY_CHECK_ARG_RET(NULL, ctx, in, filepath, ); |
| 258 | if (*filepath) { |
| 259 | /* filepath already set */ |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | switch (in->type) { |
| 264 | case LY_IN_FILEPATH: |
| 265 | if (realpath(in->method.fpath.filepath, path) != NULL) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 266 | lydict_insert(ctx, path, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 267 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 268 | lydict_insert(ctx, in->method.fpath.filepath, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | break; |
| 272 | case LY_IN_FD: |
| 273 | #ifdef __APPLE__ |
| 274 | if (fcntl(in->method.fd, F_GETPATH, path) != -1) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 275 | lydict_insert(ctx, path, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 276 | } |
| 277 | #else |
| 278 | /* get URI if there is /proc */ |
| 279 | sprintf(proc_path, "/proc/self/fd/%d", in->method.fd); |
| 280 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 281 | lydict_insert(ctx, path, len, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 282 | } |
| 283 | #endif |
| 284 | break; |
| 285 | case LY_IN_MEMORY: |
| 286 | case LY_IN_FILE: |
| 287 | /* nothing to do */ |
| 288 | break; |
| 289 | default: |
| 290 | LOGINT(ctx); |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | |
| 296 | API void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 297 | ly_in_free(struct ly_in *in, ly_bool destroy) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 298 | { |
| 299 | if (!in) { |
| 300 | return; |
| 301 | } else if (in->type == LY_IN_ERROR) { |
| 302 | LOGINT(NULL); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | if (destroy) { |
| 307 | if (in->type == LY_IN_MEMORY) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 308 | free((char *)in->start); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 309 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 310 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 311 | |
| 312 | if (in->type == LY_IN_FILE) { |
| 313 | fclose(in->method.f); |
| 314 | } else { |
| 315 | close(in->method.fd); |
| 316 | |
| 317 | if (in->type == LY_IN_FILEPATH) { |
| 318 | free(in->method.fpath.filepath); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } else if (in->type != LY_IN_MEMORY) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 323 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 324 | |
| 325 | if (in->type == LY_IN_FILEPATH) { |
| 326 | close(in->method.fpath.fd); |
| 327 | free(in->method.fpath.filepath); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | free(in); |
| 332 | } |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 333 | |
| 334 | LY_ERR |
| 335 | ly_in_read(struct ly_in *in, void *buf, size_t count) |
| 336 | { |
| 337 | if (in->length && (in->length - (in->current - in->start) < count)) { |
| 338 | /* EOF */ |
| 339 | return LY_EDENIED; |
| 340 | } |
| 341 | |
| 342 | memcpy(buf, in->current, count); |
| 343 | in->current += count; |
| 344 | return LY_SUCCESS; |
| 345 | } |
| 346 | |
| 347 | API size_t |
| 348 | ly_in_parsed(const struct ly_in *in) |
| 349 | { |
| 350 | return in->current - in->func_start; |
| 351 | } |
| 352 | |
| 353 | LY_ERR |
| 354 | ly_in_skip(struct ly_in *in, size_t count) |
| 355 | { |
| 356 | if (in->length && (in->length - (in->current - in->start) < count)) { |
| 357 | /* EOF */ |
| 358 | return LY_EDENIED; |
| 359 | } |
| 360 | |
| 361 | in->current += count; |
| 362 | return LY_SUCCESS; |
| 363 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 364 | |
| 365 | void |
| 366 | lyd_ctx_free(struct lyd_ctx *lydctx) |
| 367 | { |
| 368 | ly_set_erase(&lydctx->unres_node_type, NULL); |
| 369 | ly_set_erase(&lydctx->unres_meta_type, NULL); |
| 370 | ly_set_erase(&lydctx->when_check, NULL); |
| 371 | } |
| 372 | |
| 373 | LY_ERR |
| 374 | lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode) |
| 375 | { |
| 376 | /* alternatively, we could provide line for the error messages, but it doesn't work for the LYB format */ |
| 377 | |
| 378 | if ((lydctx->parse_options & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
| 379 | LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LY_VCODE_INNODE, "state", snode->name); |
| 380 | return LY_EVALID; |
| 381 | } |
| 382 | |
| 383 | if (snode->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 384 | if (lydctx->int_opts & LYD_INTOPT_RPC) { |
| 385 | if (lydctx->op_node) { |
| 386 | LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 387 | lys_nodetype2str(snode->nodetype), snode->name, |
| 388 | lys_nodetype2str(lydctx->op_node->schema->nodetype), lydctx->op_node->schema->name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 389 | return LY_EVALID; |
| 390 | } |
| 391 | } else { |
| 392 | LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 393 | lys_nodetype2str(snode->nodetype), snode->name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 394 | return LY_EVALID; |
| 395 | } |
| 396 | } else if (snode->nodetype == LYS_NOTIF) { |
| 397 | if (lydctx->int_opts & LYD_INTOPT_NOTIF) { |
| 398 | if (lydctx->op_node) { |
| 399 | LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 400 | lys_nodetype2str(snode->nodetype), snode->name, |
| 401 | lys_nodetype2str(lydctx->op_node->schema->nodetype), lydctx->op_node->schema->name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 402 | return LY_EVALID; |
| 403 | } |
| 404 | } else { |
| 405 | LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 406 | lys_nodetype2str(snode->nodetype), snode->name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 407 | return LY_EVALID; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return LY_SUCCESS; |
| 412 | } |
| 413 | |
| 414 | LY_ERR |
| 415 | lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const char *value, size_t value_len, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 416 | ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 417 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 418 | ly_bool incomplete; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 419 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 420 | LY_CHECK_RET(lyd_create_term(schema, value, value_len, dynamic, format, prefix_data, hints, &incomplete, node)); |
| 421 | |
| 422 | if (incomplete && !(lydctx->parse_options & LYD_PARSE_ONLY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 423 | LY_CHECK_RET(ly_set_add(&lydctx->unres_node_type, *node, 1, NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 424 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 425 | return LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | LY_ERR |
| 429 | lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 430 | const char *name, size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, |
| 431 | void *prefix_data, uint32_t hints) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 432 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 433 | ly_bool incomplete; |
Michal Vasko | b68571a | 2020-11-06 17:18:41 +0100 | [diff] [blame] | 434 | struct lyd_meta *first = NULL; |
| 435 | |
| 436 | if (meta && *meta) { |
| 437 | /* remember the first metadata */ |
| 438 | first = *meta; |
| 439 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 440 | |
| 441 | LY_CHECK_RET(lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, dynamic, format, prefix_data, |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame^] | 442 | hints, 0, &incomplete)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 443 | |
| 444 | if (incomplete && !(lydctx->parse_options & LYD_PARSE_ONLY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 445 | LY_CHECK_RET(ly_set_add(&lydctx->unres_meta_type, *meta, 1, NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 446 | } |
Michal Vasko | b68571a | 2020-11-06 17:18:41 +0100 | [diff] [blame] | 447 | |
| 448 | if (first) { |
| 449 | /* always return the first metadata */ |
| 450 | *meta = first; |
| 451 | } |
| 452 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 453 | return LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 454 | } |