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