David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_yin.c |
| 3 | * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz> |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 4 | * @brief YIN parser. |
| 5 | * |
David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +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 |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 13 | */ |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 14 | #include "parser_yin.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 15 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 16 | #include <assert.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 17 | #include <ctype.h> |
| 18 | #include <errno.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <stdbool.h> |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 23 | #include <string.h> |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 26 | #include "context.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 27 | #include "dict.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 28 | #include "path.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "set.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 30 | #include "tree.h" |
| 31 | #include "tree_schema.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 32 | #include "tree_schema_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "xml.h" |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 34 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 35 | /** |
| 36 | * @brief check if given string is URI of yin namespace. |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 37 | * |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 38 | * @param ns Namespace URI to check. |
| 39 | * |
| 40 | * @return true if ns equals YIN_NS_URI false otherwise. |
| 41 | */ |
| 42 | #define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0) |
| 43 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 44 | const char *const yin_attr_list[] = { |
| 45 | [YIN_ARG_NAME] = "name", |
| 46 | [YIN_ARG_TARGET_NODE] = "target-node", |
| 47 | [YIN_ARG_MODULE] = "module", |
| 48 | [YIN_ARG_VALUE] = "value", |
| 49 | [YIN_ARG_TEXT] = "text", |
| 50 | [YIN_ARG_CONDITION] = "condition", |
| 51 | [YIN_ARG_URI] = "uri", |
| 52 | [YIN_ARG_DATE] = "date", |
| 53 | [YIN_ARG_TAG] = "tag", |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 54 | [YIN_ARG_NONE] = "none", |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 57 | enum ly_stmt |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 58 | yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 59 | const char *prefix, size_t prefix_len, enum ly_stmt parrent) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 60 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 61 | const char *start = NULL; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 62 | enum ly_stmt kw = LY_STMT_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 63 | const struct lyxml_ns *ns = NULL; |
| 64 | |
| 65 | if (!name || name_len == 0) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 66 | return LY_STMT_NONE; |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 69 | ns = lyxml_ns_get(ctx->xmlctx, prefix, prefix_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 70 | if (ns) { |
| 71 | if (!IS_YIN_NS(ns->uri)) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 72 | return LY_STMT_EXTENSION_INSTANCE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 73 | } |
| 74 | } else { |
| 75 | /* elements without namespace are automatically unknown */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 76 | return LY_STMT_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 77 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 78 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 79 | start = name; |
| 80 | kw = lysp_match_kw(NULL, &name); |
| 81 | |
| 82 | if (name - start == (long int)name_len) { |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 83 | /* this is done because of collision in yang statement value and yang argument mapped to yin element value */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 84 | if (kw == LY_STMT_VALUE && parrent == LY_STMT_ERROR_MESSAGE) { |
| 85 | return LY_STMT_ARG_VALUE; |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 86 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 87 | return kw; |
| 88 | } else { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 89 | if (strncmp(start, "text", name_len) == 0) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 90 | return LY_STMT_ARG_TEXT; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 91 | } else { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 92 | return LY_STMT_NONE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 93 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 97 | enum yin_argument |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 98 | yin_match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 99 | { |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 100 | enum yin_argument arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 101 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 102 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 103 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 104 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 105 | #define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN; |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 106 | #define IF_ARG_PREFIX_END } |
| 107 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 108 | switch (*name) { |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 109 | case 'c': |
| 110 | already_read += 1; |
| 111 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 112 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 113 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 114 | case 'd': |
| 115 | already_read += 1; |
| 116 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 117 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 118 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 119 | case 'm': |
| 120 | already_read += 1; |
| 121 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 122 | break; |
| 123 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 124 | case 'n': |
| 125 | already_read += 1; |
| 126 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 127 | break; |
| 128 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 129 | case 't': |
| 130 | already_read += 1; |
| 131 | IF_ARG_PREFIX("a", 1) |
| 132 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 133 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 134 | IF_ARG_PREFIX_END |
| 135 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 136 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 137 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 138 | case 'u': |
| 139 | already_read += 1; |
| 140 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 141 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 142 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 143 | case 'v': |
| 144 | already_read += 1; |
| 145 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 146 | break; |
| 147 | } |
| 148 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 149 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 150 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 151 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 152 | } |
| 153 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 154 | #undef IF_ARG |
| 155 | #undef IF_ARG_PREFIX |
| 156 | #undef IF_ARG_PREFIX_END |
| 157 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 158 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 161 | #define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \ |
| 162 | kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \ |
| 163 | kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \ |
| 164 | kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT) |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 165 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 166 | #define HAS_META(kw) (IS_NODE_ELEM(kw) || kw == LY_STMT_IMPORT || kw == LY_STMT_INCLUDE || kw == LY_STMT_INPUT || kw == LY_STMT_OUTPUT) |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 167 | |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 168 | /** |
| 169 | * @brief Free subelems information allocated on heap. |
| 170 | * |
| 171 | * @param[in] count Size of subelems array. |
| 172 | * @param[in] subelems Subelems array to free. |
| 173 | */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 174 | static void |
| 175 | subelems_deallocator(size_t count, struct yin_subelement *subelems) |
| 176 | { |
| 177 | for(size_t i = 0; i < count; ++i) { |
| 178 | if (HAS_META(subelems[i].type)) { |
| 179 | free(subelems[i].dest); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | free(subelems); |
| 184 | } |
| 185 | |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 186 | /** |
| 187 | * @brief Allocate subelems information on heap. |
| 188 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 189 | * @param[in] ctx YIN parser context, used for logging. |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 190 | * @param[in] count Number of subelements. |
| 191 | * @param[in] parent Parent node if any. |
| 192 | * @param[out] result Allocated subelems array. |
| 193 | * |
| 194 | * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure. |
| 195 | */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 196 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 197 | subelems_allocator(struct lys_yin_parser_ctx *ctx, size_t count, struct lysp_node *parent, |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 198 | struct yin_subelement **result, ...) |
| 199 | { |
| 200 | va_list ap; |
| 201 | |
| 202 | *result = calloc(count, sizeof **result); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 203 | LY_CHECK_GOTO(!(*result), mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 204 | |
| 205 | va_start(ap, result); |
| 206 | for (size_t i = 0; i < count; ++i) { |
| 207 | /* TYPE */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 208 | (*result)[i].type = va_arg(ap, enum ly_stmt); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 209 | /* DEST */ |
| 210 | if (IS_NODE_ELEM((*result)[i].type)) { |
| 211 | struct tree_node_meta *node_meta = NULL; |
| 212 | node_meta = calloc(1, sizeof *node_meta); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 213 | LY_CHECK_GOTO(!node_meta, mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 214 | node_meta->parent = parent; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 215 | node_meta->nodes = va_arg(ap, void *); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 216 | (*result)[i].dest = node_meta; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 217 | } else if ((*result)[i].type == LY_STMT_IMPORT) { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 218 | struct import_meta *imp_meta = NULL; |
| 219 | imp_meta = calloc(1, sizeof *imp_meta); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 220 | LY_CHECK_GOTO(!imp_meta, mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 221 | imp_meta->prefix = va_arg(ap, const char *); |
| 222 | imp_meta->imports = va_arg(ap, struct lysp_import **); |
| 223 | (*result)[i].dest = imp_meta; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 224 | } else if ((*result)[i].type == LY_STMT_INCLUDE) { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 225 | struct include_meta *inc_meta = NULL; |
| 226 | inc_meta = calloc(1, sizeof *inc_meta); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 227 | LY_CHECK_GOTO(!inc_meta, mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 228 | inc_meta->name = va_arg(ap, const char *); |
| 229 | inc_meta->includes = va_arg(ap, struct lysp_include **); |
| 230 | (*result)[i].dest = inc_meta; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 231 | } else if ((*result)[i].type == LY_STMT_INPUT || (*result)[i].type == LY_STMT_OUTPUT) { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 232 | struct inout_meta *inout_meta = NULL; |
| 233 | inout_meta = calloc(1, sizeof *inout_meta); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 234 | LY_CHECK_GOTO(!inout_meta, mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 235 | inout_meta->parent = parent; |
| 236 | inout_meta->inout_p = va_arg(ap, struct lysp_action_inout *); |
| 237 | (*result)[i].dest = inout_meta; |
| 238 | } else { |
| 239 | (*result)[i].dest = va_arg(ap, void *); |
| 240 | } |
| 241 | /* FLAGS */ |
| 242 | (*result)[i].flags = va_arg(ap, int); |
| 243 | } |
| 244 | va_end(ap); |
| 245 | |
| 246 | return LY_SUCCESS; |
| 247 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 248 | mem_err: |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 249 | subelems_deallocator(count, *result); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 250 | LOGMEM(ctx->xmlctx->ctx); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 251 | return LY_EMEM; |
| 252 | } |
| 253 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 254 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 255 | yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type) |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 256 | { |
| 257 | int prefix = 0; |
| 258 | unsigned int c; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 259 | size_t utf8_char_len, already_read = 0; |
| 260 | const char *val; |
| 261 | |
| 262 | assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT)); |
| 263 | |
| 264 | val = ctx->xmlctx->value; |
| 265 | while (already_read < ctx->xmlctx->value_len) { |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 266 | LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len), |
| 267 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID); |
| 268 | already_read += utf8_char_len; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 269 | LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT); |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 270 | |
| 271 | switch (val_type) { |
| 272 | case Y_IDENTIF_ARG: |
| 273 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL)); |
| 274 | break; |
| 275 | case Y_PREF_IDENTIF_ARG: |
| 276 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix)); |
| 277 | break; |
| 278 | case Y_STR_ARG: |
| 279 | case Y_MAYBE_STR_ARG: |
| 280 | LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c)); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return LY_SUCCESS; |
| 286 | } |
| 287 | |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 288 | /** |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 289 | * @brief Parse yin attributes. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 290 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 291 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 292 | * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without |
| 293 | * special argument). |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 294 | * @param[out] arg_val Where value of argument should be stored. Can be NULL iff arg_type is specified as YIN_ARG_NONE. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 295 | * @param[in] val_type Type of expected value of attribute. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 296 | * @param[in] current_element Identification of current element, used for logging. |
| 297 | * |
| 298 | * @return LY_ERR values. |
| 299 | */ |
| 300 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 301 | yin_parse_attribute(struct lys_yin_parser_ctx *ctx, enum yin_argument arg_type, const char **arg_val, enum yang_arg val_type, |
| 302 | enum ly_stmt current_element) |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 303 | { |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 304 | enum yin_argument arg = YIN_ARG_UNKNOWN; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 305 | bool found = false; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 306 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 307 | /* validation of attributes */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 308 | while (ctx->xmlctx->status == LYXML_ATTRIBUTE) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 309 | /* yin arguments represented as attributes have no namespace, which in this case means no prefix */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 310 | if (!ctx->xmlctx->prefix) { |
| 311 | arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 312 | if (arg == YIN_ARG_NONE) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 313 | /* skip it */ |
| 314 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 315 | } else if (arg == arg_type) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 316 | LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR, |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 317 | yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 318 | found = true; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 319 | |
| 320 | /* go to value */ |
| 321 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 322 | LY_CHECK_RET(yin_validate_value(ctx, val_type)); |
| 323 | *arg_val = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic); |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 324 | LY_CHECK_RET(!(*arg_val), LY_EMEM); |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 325 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 326 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len, |
| 327 | ctx->xmlctx->name, ly_stmt2str(current_element)); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 328 | return LY_EVALID; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 329 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 330 | } else { |
| 331 | /* skip it */ |
| 332 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 333 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 334 | |
| 335 | /* next attribute */ |
| 336 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 337 | } |
| 338 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 339 | /* anything else than Y_MAYBE_STR_ARG is mandatory */ |
| 340 | if (val_type != Y_MAYBE_STR_ARG && !found) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 341 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.", |
| 342 | yin_attr2str(arg_type), ly_stmt2str(current_element)); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 343 | return LY_EVALID; |
| 344 | } |
| 345 | |
| 346 | return LY_SUCCESS; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 347 | } |
| 348 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 349 | /** |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 350 | * @brief Get record with given type. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 351 | * |
| 352 | * @param[in] type Type of wanted record. |
| 353 | * @param[in] array_size Size of array. |
| 354 | * @param[in] array Searched array. |
| 355 | * |
| 356 | * @return Pointer to desired record on success, NULL if element is not in the array. |
| 357 | */ |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 358 | static struct yin_subelement * |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 359 | get_record(enum ly_stmt type, size_t array_size, struct yin_subelement *array) |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 360 | { |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 361 | for (unsigned int u = 0; u < array_size; ++u) { |
| 362 | if (array[u].type == type) { |
| 363 | return &array[u]; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 364 | } |
| 365 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 369 | /** |
| 370 | * @brief Helper function to check mandatory constraint of subelement. |
| 371 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 372 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 373 | * @param[in] subelem_info Array of information about subelements. |
| 374 | * @param[in] subelem_info_size Size of subelem_info array. |
| 375 | * @param[in] current_element Identification of element that is currently being parsed, used for logging. |
| 376 | * |
| 377 | * @return LY_ERR values. |
| 378 | */ |
| 379 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 380 | yin_check_subelem_mandatory_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 381 | signed char subelem_info_size, enum ly_stmt current_element) |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 382 | { |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 383 | for (signed char i = 0; i < subelem_info_size; ++i) { |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 384 | /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */ |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 385 | if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 386 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM, |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 387 | ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element)); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 388 | return LY_EVALID; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return LY_SUCCESS; |
| 393 | } |
| 394 | |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 395 | /** |
| 396 | * @brief Helper function to check "first" constraint of subelement. |
| 397 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 398 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 399 | * @param[in] subelem_info Array of information about subelements. |
| 400 | * @param[in] subelem_info_size Size of subelem_info array. |
| 401 | * @param[in] current_element Identification of element that is currently being parsed, used for logging. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 402 | * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement, used for logging. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 403 | * |
| 404 | * @return LY_ERR values. |
| 405 | */ |
| 406 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 407 | yin_check_subelem_first_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 408 | signed char subelem_info_size, enum ly_stmt current_element, |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 409 | struct yin_subelement *exp_first) |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 410 | { |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 411 | for (signed char i = 0; i < subelem_info_size; ++i) { |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 412 | if (subelem_info[i].flags & YIN_SUBELEM_PARSED) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 413 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM, |
| 414 | ly_stmt2str(exp_first->type), ly_stmt2str(current_element)); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 415 | return LY_EVALID; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return LY_SUCCESS; |
| 420 | } |
| 421 | |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 422 | /** |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 423 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute, |
| 424 | * for example prefix or namespace element. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 425 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 426 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 427 | * @param[in] kw Type of current element. |
| 428 | * @param[out] value Where value of attribute should be stored. |
| 429 | * @param[in] arg_type Expected type of attribute. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 430 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 431 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 432 | * @return LY_ERR values. |
| 433 | */ |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 434 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 435 | yin_parse_simple_element(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char **value, |
| 436 | enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts) |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 437 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 438 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 439 | LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw)); |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 440 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 441 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 442 | }; |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 443 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 444 | return yin_parse_content(ctx, subelems, 1, kw, NULL, exts); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /** |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 448 | * @brief Parse path element. |
| 449 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 450 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 451 | * @param[in] kw Type of current element. |
| 452 | * @param[out] type Type structure to store parsed value, flags and extension instances. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 453 | * @return LY_ERR values. |
| 454 | */ |
| 455 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 456 | yin_parse_path(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type *type) |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 457 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 458 | LY_ERR ret; |
| 459 | const char *str_path; |
| 460 | |
| 461 | LY_CHECK_RET(yin_parse_simple_element(ctx, kw, &str_path, YIN_ARG_VALUE, Y_STR_ARG, &type->exts)); |
| 462 | ret = ly_path_parse(ctx->xmlctx->ctx, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE, |
| 463 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path); |
| 464 | lydict_remove(ctx->xmlctx->ctx, str_path); |
| 465 | LY_CHECK_RET(ret); |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 466 | type->flags |= LYS_SET_PATH; |
| 467 | |
| 468 | return LY_SUCCESS; |
| 469 | } |
| 470 | |
| 471 | /** |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 472 | * @brief Parse pattern element. |
| 473 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 474 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 475 | * @param[in,out] type Type structure to store parsed value, flags and extension instances. |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 476 | * @return LY_ERR values. |
| 477 | */ |
| 478 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 479 | yin_parse_pattern(struct lys_yin_parser_ctx *ctx, struct lysp_type *type) |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 480 | { |
| 481 | const char *real_value = NULL; |
| 482 | char *saved_value = NULL; |
| 483 | struct lysp_restr *restr; |
| 484 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 485 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM); |
| 486 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 487 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &real_value, Y_STR_ARG, LY_STMT_PATTERN)); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 488 | size_t len = strlen(real_value); |
| 489 | |
| 490 | saved_value = malloc(len + 2); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 491 | LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 492 | memmove(saved_value + 1, real_value, len); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 493 | FREE_STRING(ctx->xmlctx->ctx, real_value); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 494 | saved_value[0] = 0x06; |
| 495 | saved_value[len + 1] = '\0'; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 496 | restr->arg = lydict_insert_zc(ctx->xmlctx->ctx, saved_value); |
| 497 | LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 498 | type->flags |= LYS_SET_PATTERN; |
| 499 | |
| 500 | struct yin_subelement subelems[6] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 501 | {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE}, |
| 502 | {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE}, |
| 503 | {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE}, |
| 504 | {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE}, |
| 505 | {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE}, |
| 506 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 507 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 508 | return yin_parse_content(ctx, subelems, 6, LY_STMT_PATTERN, NULL, &restr->exts); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 509 | } |
| 510 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 511 | /** |
| 512 | * @brief Parse fraction-digits element. |
| 513 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 514 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 515 | * @param[in,out] type Type structure to store value, flags and extension instances. |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 516 | * @return LY_ERR values. |
| 517 | */ |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 518 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 519 | yin_parse_fracdigits(struct lys_yin_parser_ctx *ctx, struct lysp_type *type) |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 520 | { |
| 521 | const char *temp_val = NULL; |
| 522 | char *ptr; |
| 523 | unsigned long int num; |
| 524 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 525 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 526 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_FRACTION_DIGITS)); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 527 | |
| 528 | if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 529 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 530 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 531 | return LY_EVALID; |
| 532 | } |
| 533 | |
| 534 | errno = 0; |
| 535 | num = strtoul(temp_val, &ptr, 10); |
| 536 | if (*ptr != '\0') { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 537 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 538 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 539 | return LY_EVALID; |
| 540 | } |
| 541 | if ((errno == ERANGE) || (num > 18)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 542 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 543 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 544 | return LY_EVALID; |
| 545 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 546 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 547 | type->fraction_digits = num; |
| 548 | type->flags |= LYS_SET_FRDIGITS; |
| 549 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 550 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 551 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 552 | return yin_parse_content(ctx, subelems, 1, LY_STMT_FRACTION_DIGITS, NULL, &type->exts); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 553 | } |
| 554 | |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 555 | /** |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 556 | * @brief Parse enum element. |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 557 | * |
| 558 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 559 | * @param[in,out] type Type structure to store parsed value, flags and extension instances. |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 560 | * @return LY_ERR values. |
| 561 | */ |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 562 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 563 | yin_parse_enum(struct lys_yin_parser_ctx *ctx, struct lysp_type *type) |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 564 | { |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 565 | struct lysp_type_enum *en; |
David Sedlák | 1e69678 | 2019-07-17 15:06:07 +0200 | [diff] [blame] | 566 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 567 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 568 | type->flags |= LYS_SET_ENUM; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 569 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 570 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM)); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 571 | LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name))); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 572 | CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum"); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 573 | CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name); |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 574 | |
| 575 | struct yin_subelement subelems[6] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 576 | {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE}, |
| 577 | {LY_STMT_IF_FEATURE, &en->iffeatures, 0}, |
| 578 | {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE}, |
| 579 | {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE}, |
| 580 | {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE}, |
| 581 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 582 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 583 | return yin_parse_content(ctx, subelems, 6, LY_STMT_ENUM, NULL, &en->exts); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /** |
| 587 | * @brief Parse bit element. |
| 588 | * |
| 589 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 590 | * @param[in,out] type Type structure to store parsed value, flags and extension instances. |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 591 | * @return LY_ERR values. |
| 592 | */ |
| 593 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 594 | yin_parse_bit(struct lys_yin_parser_ctx *ctx, struct lysp_type *type) |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 595 | { |
| 596 | struct lysp_type_enum *en; |
| 597 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 598 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 599 | type->flags |= LYS_SET_BIT; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 600 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 601 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT)); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 602 | CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name); |
| 603 | |
| 604 | struct yin_subelement subelems[6] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 605 | {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE}, |
| 606 | {LY_STMT_IF_FEATURE, &en->iffeatures, 0}, |
| 607 | {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE}, |
| 608 | {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE}, |
| 609 | {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE}, |
| 610 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 611 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 612 | return yin_parse_content(ctx, subelems, 6, LY_STMT_BIT, NULL, &en->exts); |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 613 | } |
| 614 | |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 615 | /** |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 616 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have |
| 617 | * more instances, such as base or if-feature. |
| 618 | * |
| 619 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 620 | * @param[in] kw Type of current element. |
| 621 | * @param[out] values Parsed values to add to. |
| 622 | * @param[in] arg_type Expected type of attribute. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 623 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 624 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 625 | * @return LY_ERR values. |
| 626 | */ |
| 627 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 628 | yin_parse_simple_elements(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char ***values, enum yin_argument arg_type, |
| 629 | enum yang_arg arg_val_type, struct lysp_ext_instance **exts) |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 630 | { |
| 631 | const char **value; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 632 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 633 | LY_ARRAY_SIZE_TYPE index = LY_ARRAY_SIZE(*values) - 1; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 634 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 635 | {LY_STMT_EXTENSION_INSTANCE, &index, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 636 | }; |
| 637 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 638 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 639 | LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw)); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 640 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 641 | return yin_parse_content(ctx, subelems, 1, kw, NULL, exts); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /** |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 645 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute. |
| 646 | * |
| 647 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 648 | * @param[in] kw Type of current element. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 649 | * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 650 | * @param[in] arg_type Expected type of attribute. |
| 651 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 652 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 653 | * @return LY_ERR values. |
| 654 | */ |
| 655 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 656 | yin_parse_simple_elem(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo, |
| 657 | enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts) |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 658 | { |
| 659 | if (subinfo->flags & YIN_SUBELEM_UNIQUE) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 660 | LY_CHECK_RET(yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 661 | arg_type, arg_val_type, exts)); |
| 662 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 663 | LY_CHECK_RET(yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 664 | arg_type, arg_val_type, exts)); |
| 665 | } |
| 666 | |
| 667 | return LY_SUCCESS; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * @brief Parse base element. |
| 672 | * |
| 673 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 674 | * @param[in] parent Identification of parent element. |
| 675 | * @param[out] dest Where parsed values should be stored. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 676 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 677 | * @return LY_ERR values. |
| 678 | */ |
| 679 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 680 | yin_parse_base(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, void *dest, struct lysp_ext_instance **exts) |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 681 | { |
| 682 | struct lysp_type *type = NULL; |
| 683 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 684 | if (parent == LY_STMT_TYPE) { |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 685 | type = (struct lysp_type *)dest; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 686 | LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 687 | Y_PREF_IDENTIF_ARG, exts)); |
| 688 | type->flags |= LYS_SET_BASE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 689 | } else if (parent == LY_STMT_IDENTITY) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 690 | LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 691 | YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts)); |
| 692 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 693 | LOGINT(ctx->xmlctx->ctx); |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 694 | return LY_EINT; |
| 695 | } |
| 696 | |
| 697 | return LY_SUCCESS; |
| 698 | } |
| 699 | |
| 700 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 701 | * @brief Parse require-instance element. |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 702 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 703 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 704 | * @param[out] type Type structure to store value, flag and extensions. |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 705 | * @return LY_ERR values. |
| 706 | */ |
| 707 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 708 | yin_pasrse_reqinstance(struct lys_yin_parser_ctx *ctx, struct lysp_type *type) |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 709 | { |
| 710 | const char *temp_val = NULL; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 711 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 712 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 713 | }; |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 714 | |
| 715 | type->flags |= LYS_SET_REQINST; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 716 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 717 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_REQUIRE_INSTANCE)); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 718 | if (strcmp(temp_val, "true") == 0) { |
| 719 | type->require_instance = 1; |
| 720 | } else if (strcmp(temp_val, "false") != 0) { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 721 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", |
| 722 | "require-instance", "true", "false"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 723 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 724 | return LY_EVALID; |
| 725 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 726 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 727 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 728 | return yin_parse_content(ctx, subelems, 1, LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /** |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 732 | * @brief Parse modifier element. |
| 733 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 734 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 735 | * @param[in,out] pat Value to write to. |
| 736 | * @param[in,out] exts Extension instances to add to. |
| 737 | * |
| 738 | * @return LY_ERR values. |
| 739 | */ |
| 740 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 741 | yin_parse_modifier(struct lys_yin_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts) |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 742 | { |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 743 | assert(**pat == 0x06); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 744 | const char *temp_val; |
| 745 | char *modified_val; |
| 746 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 747 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 748 | }; |
| 749 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 750 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 751 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MODIFIER)); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 752 | if (strcmp(temp_val, "invert-match") != 0) { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 753 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value", |
| 754 | "modifier", "invert-match"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 755 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 756 | return LY_EVALID; |
| 757 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 758 | lydict_remove(ctx->xmlctx->ctx, temp_val); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 759 | |
| 760 | /* allocate new value */ |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 761 | modified_val = malloc(strlen(*pat) + 1); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 762 | LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 763 | strcpy(modified_val, *pat); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 764 | lydict_remove(ctx->xmlctx->ctx, *pat); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 765 | |
| 766 | /* modify the new value */ |
| 767 | modified_val[0] = 0x15; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 768 | *pat = lydict_insert_zc(ctx->xmlctx->ctx, modified_val); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 769 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 770 | return yin_parse_content(ctx, subelems, 1, LY_STMT_MODIFIER, NULL, exts); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /** |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 774 | * @brief Parse a restriction element (length, range or one instance of must). |
| 775 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 776 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 777 | * @param[in] restr_kw Identificaton of element that is being parsed, can be set to LY_STMT_MUST, LY_STMT_LENGTH or LY_STMT_RANGE. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 778 | * @param[in] restr Value to write to. |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 779 | */ |
| 780 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 781 | yin_parse_restriction(struct lys_yin_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr) |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 782 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 783 | assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 784 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 785 | {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE}, |
| 786 | {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE}, |
| 787 | {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE}, |
| 788 | {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE}, |
| 789 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 790 | }; |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 791 | /* argument of must is called condition, but argument of length and range is called value */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 792 | enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE; |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 793 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 794 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 795 | LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg, Y_STR_ARG, restr_kw)); |
| 796 | |
| 797 | return yin_parse_content(ctx, subelems, 5, restr_kw, NULL, &restr->exts); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /** |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 801 | * @brief Parse range element. |
| 802 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 803 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 804 | * @param[out] type Type structure to store parsed value and flags. |
| 805 | * |
| 806 | * @return LY_ERR values. |
| 807 | */ |
| 808 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 809 | yin_parse_range(struct lys_yin_parser_ctx *ctx, struct lysp_type *type) |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 810 | { |
| 811 | type->range = calloc(1, sizeof *type->range); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 812 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
| 813 | LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range)); |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 814 | type->flags |= LYS_SET_RANGE; |
| 815 | |
| 816 | return LY_SUCCESS; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * @brief Parse length element. |
| 821 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 822 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 823 | * @param[out] type Type structure to store parsed value and flags. |
| 824 | * |
| 825 | * @return LY_ERR values. |
| 826 | */ |
| 827 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 828 | yin_parse_length(struct lys_yin_parser_ctx *ctx, struct lysp_type *type) |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 829 | { |
| 830 | type->length = calloc(1, sizeof *type->length); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 831 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
| 832 | LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length)); |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 833 | type->flags |= LYS_SET_LENGTH; |
| 834 | |
| 835 | return LY_SUCCESS; |
| 836 | } |
| 837 | |
| 838 | /** |
David Sedlák | bc9ec9c | 2019-07-11 15:53:55 +0200 | [diff] [blame] | 839 | * @brief Parse must element. |
| 840 | * |
| 841 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bc9ec9c | 2019-07-11 15:53:55 +0200 | [diff] [blame] | 842 | * @param[in,out] restrs Restrictions to add to. |
| 843 | * |
| 844 | * @return LY_ERR values. |
| 845 | */ |
| 846 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 847 | yin_parse_must(struct lys_yin_parser_ctx *ctx, struct lysp_restr **restrs) |
David Sedlák | bc9ec9c | 2019-07-11 15:53:55 +0200 | [diff] [blame] | 848 | { |
| 849 | struct lysp_restr *restr; |
| 850 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 851 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM); |
| 852 | return yin_parse_restriction(ctx, LY_STMT_MUST, restr); |
David Sedlák | bc9ec9c | 2019-07-11 15:53:55 +0200 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /** |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 856 | * @brief Parse position or value element. |
| 857 | * |
| 858 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 859 | * @param[in] kw Type of current element, can be set to LY_STMT_POSITION or LY_STMT_VALUE. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 860 | * @param[out] enm Enum structure to save value, flags and extension instances. |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 861 | * |
| 862 | * @return LY_ERR values. |
| 863 | */ |
| 864 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 865 | yin_parse_value_pos(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type_enum *enm) |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 866 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 867 | assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 868 | const char *temp_val = NULL; |
| 869 | char *ptr; |
| 870 | long int num; |
| 871 | unsigned long int unum; |
| 872 | |
| 873 | /* set value flag */ |
| 874 | enm->flags |= LYS_SET_VALUE; |
| 875 | |
| 876 | /* get attribute value */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 877 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 878 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw)); |
David Sedlák | ae5378f | 2019-07-17 11:37:59 +0200 | [diff] [blame] | 879 | if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '+') || |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 880 | ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 881 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 882 | goto error; |
| 883 | } |
| 884 | |
| 885 | /* convert value */ |
| 886 | errno = 0; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 887 | if (kw == LY_STMT_VALUE) { |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 888 | num = strtol(temp_val, &ptr, 10); |
| 889 | if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 890 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 891 | goto error; |
| 892 | } |
| 893 | } else { |
| 894 | unum = strtoul(temp_val, &ptr, 10); |
| 895 | if (unum > UINT64_C(4294967295)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 896 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 897 | goto error; |
| 898 | } |
| 899 | } |
| 900 | /* check if whole argument value was converted */ |
| 901 | if (*ptr != '\0') { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 902 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | ebcd0eb | 2019-07-16 17:55:12 +0200 | [diff] [blame] | 903 | goto error; |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 904 | } |
| 905 | if (errno == ERANGE) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 906 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 907 | goto error; |
| 908 | } |
| 909 | /* save correctly ternary operator can't be used because num and unum have different signes */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 910 | if (kw == LY_STMT_VALUE) { |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 911 | enm->value = num; |
| 912 | } else { |
| 913 | enm->value = unum; |
| 914 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 915 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 916 | |
| 917 | /* parse subelements */ |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 918 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 919 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 920 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 921 | return yin_parse_content(ctx, subelems, 1, kw, NULL, &enm->exts); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 922 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 923 | error: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 924 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
| 925 | return LY_EVALID; |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 926 | } |
| 927 | |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 928 | /** |
| 929 | * @brief Parse belongs-to element. |
| 930 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 931 | * @param[in] ctx YIN parser context for logging and to store current state. |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 932 | * @param[out] submod Structure of submodule that is being parsed. |
| 933 | * @param[in,out] exts Extension instances to add to. |
| 934 | * |
| 935 | * @return LY_ERR values |
| 936 | */ |
| 937 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 938 | yin_parse_belongs_to(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod, struct lysp_ext_instance **exts) |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 939 | { |
| 940 | struct yin_subelement subelems[2] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 941 | {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 942 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 943 | }; |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 944 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 945 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 946 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO)); |
| 947 | |
| 948 | return yin_parse_content(ctx, subelems, 2, LY_STMT_BELONGS_TO, NULL, exts); |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 949 | } |
| 950 | |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 951 | /** |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 952 | * @brief Function to parse meta tags (description, contact, ...) eg. elements with |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 953 | * text element as child. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 954 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 955 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 956 | * @param[in] elem_type Type of element can be set to LY_STMT_ORGANIZATION or LY_STMT_CONTACT or LY_STMT_DESCRIPTION or LY_STMT_REFERENCE. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 957 | * @param[out] value Where the content of meta element should be stored. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 958 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 959 | * |
| 960 | * @return LY_ERR values. |
| 961 | */ |
| 962 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 963 | yin_parse_meta(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **value, struct lysp_ext_instance **exts) |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 964 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 965 | assert(elem_type == LY_STMT_ORGANIZATION || elem_type == LY_STMT_CONTACT || elem_type == LY_STMT_DESCRIPTION || elem_type == LY_STMT_REFERENCE); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 966 | |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 967 | struct yin_subelement subelems[2] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 968 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
| 969 | {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 970 | }; |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 971 | /* check attributes */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 972 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 973 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 974 | |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 975 | /* parse content */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 976 | return yin_parse_content(ctx, subelems, 2, elem_type, NULL, exts); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /** |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 980 | * @brief Parse error-message element. |
| 981 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 982 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 983 | * @param[out] value Where the content of error-message element should be stored. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 984 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 985 | * |
| 986 | * @return LY_ERR values. |
| 987 | */ |
| 988 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 989 | yin_parse_err_msg(struct lys_yin_parser_ctx *ctx, const char **value, struct lysp_ext_instance **exts) |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 990 | { |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 991 | struct yin_subelement subelems[2] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 992 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
| 993 | {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 994 | }; |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 995 | |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 996 | /* check attributes */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 997 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 998 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, LY_STMT_ERROR_MESSAGE)); |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 999 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1000 | return yin_parse_content(ctx, subelems, 2, LY_STMT_ERROR_MESSAGE, NULL, exts); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | /** |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1004 | * @brief Parse type element. |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1005 | * |
| 1006 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1007 | * @param[in] parent Identification of parent element. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1008 | * @param[in,out] type Type to write to. |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1009 | * |
| 1010 | * @return LY_ERR values. |
| 1011 | */ |
| 1012 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1013 | yin_parse_type(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct yin_subelement *subinfo) |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1014 | { |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1015 | struct lysp_type *type = NULL; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1016 | if (parent == LY_STMT_DEVIATE) { |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1017 | *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1018 | LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1019 | type = *((struct lysp_type **)subinfo->dest); |
| 1020 | } else { |
| 1021 | type = (struct lysp_type *)subinfo->dest; |
| 1022 | } |
| 1023 | /* type as child of another type */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1024 | if (parent == LY_STMT_TYPE) { |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1025 | struct lysp_type *nested_type = NULL; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1026 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM); |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1027 | type->flags |= LYS_SET_TYPE; |
| 1028 | type = nested_type; |
| 1029 | } |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1030 | struct yin_subelement subelems[11] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1031 | {LY_STMT_BASE, type, 0}, |
| 1032 | {LY_STMT_BIT, type, 0}, |
| 1033 | {LY_STMT_ENUM, type, 0}, |
| 1034 | {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE}, |
| 1035 | {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE}, |
| 1036 | {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE}, |
| 1037 | {LY_STMT_PATTERN, type, 0}, |
| 1038 | {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE}, |
| 1039 | {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE}, |
| 1040 | {LY_STMT_TYPE, type}, |
| 1041 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1042 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1043 | |
| 1044 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1045 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE)); |
| 1046 | return yin_parse_content(ctx, subelems, 11, LY_STMT_TYPE, NULL, &type->exts); |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1049 | /** |
| 1050 | * @brief Parse max-elements element. |
| 1051 | * |
| 1052 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1053 | * @param[in,out] max Value to write to. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1054 | * @param[in] flags Flags to write to. |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1055 | * @param[in,out] exts Extension instances to add to. |
| 1056 | * |
| 1057 | * @return LY_ERR values. |
| 1058 | */ |
| 1059 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1060 | yin_parse_maxelements(struct lys_yin_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1061 | { |
| 1062 | const char *temp_val = NULL; |
| 1063 | char *ptr; |
| 1064 | unsigned long int num; |
| 1065 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1066 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1067 | }; |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1068 | |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1069 | *flags |= LYS_SET_MAX; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1070 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1071 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS)); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1072 | if (!temp_val || temp_val[0] == '\0' || temp_val[0] == '0' || (temp_val[0] != 'u' && !isdigit(temp_val[0]))) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1073 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1074 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1075 | return LY_EVALID; |
| 1076 | } |
| 1077 | |
| 1078 | if (strcmp(temp_val, "unbounded")) { |
| 1079 | errno = 0; |
| 1080 | num = strtoul(temp_val, &ptr, 10); |
| 1081 | if (*ptr != '\0') { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1082 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1083 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1084 | return LY_EVALID; |
| 1085 | } |
| 1086 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1087 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1088 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1089 | return LY_EVALID; |
| 1090 | } |
| 1091 | *max = num; |
| 1092 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1093 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
| 1094 | return yin_parse_content(ctx, subelems, 1, LY_STMT_MAX_ELEMENTS, NULL, exts); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1095 | } |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1096 | |
| 1097 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1098 | * @brief Parse min-elements element. |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1099 | * |
| 1100 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1101 | * @param[in,out] min Value to write to. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1102 | * @param[in] flags Flags to write to. |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1103 | * @param[in,out] exts Extension instances to add to. |
| 1104 | * |
| 1105 | * @return LY_ERR values. |
| 1106 | */ |
| 1107 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1108 | yin_parse_minelements(struct lys_yin_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1109 | { |
| 1110 | const char *temp_val = NULL; |
| 1111 | char *ptr; |
| 1112 | unsigned long int num; |
| 1113 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1114 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1115 | }; |
| 1116 | |
| 1117 | *flags |= LYS_SET_MIN; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1118 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1119 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MIN_ELEMENTS)); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1120 | |
| 1121 | if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '0' && temp_val[1] != '\0')) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1122 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1123 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1124 | return LY_EVALID; |
| 1125 | } |
| 1126 | |
| 1127 | errno = 0; |
| 1128 | num = strtoul(temp_val, &ptr, 10); |
| 1129 | if (ptr[0] != 0) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1130 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1131 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1132 | return LY_EVALID; |
| 1133 | } |
| 1134 | if (errno == ERANGE || num > UINT32_MAX) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1135 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1136 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1137 | return LY_EVALID; |
| 1138 | } |
| 1139 | *min = num; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1140 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
| 1141 | return yin_parse_content(ctx, subelems, 1, LY_STMT_MIN_ELEMENTS, NULL, exts); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1144 | /** |
| 1145 | * @brief Parse min-elements or max-elements element. |
| 1146 | * |
| 1147 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1148 | * @param[in] parent Identification of parent element. |
| 1149 | * @param[in] current Identification of current element. |
| 1150 | * @param[in] dest Where the parsed value and flags should be stored. |
| 1151 | * |
| 1152 | * @return LY_ERR values. |
| 1153 | */ |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1154 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1155 | yin_parse_minmax(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, enum ly_stmt current, void *dest) |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1156 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1157 | assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS); |
| 1158 | assert(parent == LY_STMT_LEAF_LIST || parent == LY_STMT_REFINE || parent == LY_STMT_LIST || parent == LY_STMT_DEVIATE); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1159 | uint32_t *lim; |
| 1160 | uint16_t *flags; |
| 1161 | struct lysp_ext_instance **exts; |
| 1162 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1163 | if (parent == LY_STMT_LEAF_LIST) { |
| 1164 | lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1165 | flags = &((struct lysp_node_leaflist *)dest)->flags; |
| 1166 | exts = &((struct lysp_node_leaflist *)dest)->exts; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1167 | } else if (parent == LY_STMT_REFINE) { |
| 1168 | lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1169 | flags = &((struct lysp_refine *)dest)->flags; |
| 1170 | exts = &((struct lysp_refine *)dest)->exts; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1171 | } else if (parent == LY_STMT_LIST) { |
| 1172 | lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1173 | flags = &((struct lysp_node_list *)dest)->flags; |
| 1174 | exts = &((struct lysp_node_list *)dest)->exts; |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 1175 | } else { |
| 1176 | lim = ((struct minmax_dev_meta *)dest)->lim; |
| 1177 | flags = ((struct minmax_dev_meta *)dest)->flags; |
| 1178 | exts = ((struct minmax_dev_meta *)dest)->exts; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1179 | } |
| 1180 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1181 | if (current == LY_STMT_MAX_ELEMENTS) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1182 | LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts)); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1183 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1184 | LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts)); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | return LY_SUCCESS; |
| 1188 | } |
| 1189 | |
| 1190 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1191 | * @brief Parse ordered-by element. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1192 | * |
| 1193 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1194 | * @param[out] flags Flags to write to. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1195 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1196 | * |
| 1197 | * @return LY_ERR values. |
| 1198 | */ |
| 1199 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1200 | yin_parse_orderedby(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1201 | { |
| 1202 | const char *temp_val; |
| 1203 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1204 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1205 | }; |
| 1206 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1207 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1208 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_ORDERED_BY)); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1209 | if (strcmp(temp_val, "system") == 0) { |
| 1210 | *flags |= LYS_ORDBY_SYSTEM; |
| 1211 | } else if (strcmp(temp_val, "user") == 0) { |
| 1212 | *flags |= LYS_ORDBY_USER; |
| 1213 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1214 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", |
| 1215 | "ordered-by", "system", "user"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1216 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1217 | return LY_EVALID; |
| 1218 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1219 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1220 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1221 | return yin_parse_content(ctx, subelems, 1, LY_STMT_ORDERED_BY, NULL, exts); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1225 | * @brief Parse any-data or any-xml element. |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1226 | * |
| 1227 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1228 | * @param[in] any_kw Identification of current element, can be set to LY_STMT_ANYDATA or LY_STMT_ANYXML |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1229 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1230 | * |
| 1231 | * @return LY_ERR values. |
| 1232 | */ |
| 1233 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1234 | yin_parse_any(struct lys_yin_parser_ctx *ctx, enum ly_stmt any_kw, struct tree_node_meta *node_meta) |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1235 | { |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1236 | struct lysp_node_anydata *any; |
| 1237 | |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 1238 | /* create new sibling */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1239 | LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1240 | any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML; |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1241 | any->parent = node_meta->parent; |
| 1242 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1243 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1244 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1245 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw)); |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1246 | |
| 1247 | struct yin_subelement subelems[9] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1248 | {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE}, |
| 1249 | {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE}, |
| 1250 | {LY_STMT_IF_FEATURE, &any->iffeatures, 0}, |
| 1251 | {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE}, |
| 1252 | {LY_STMT_MUST, &any->musts, 0}, |
| 1253 | {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE}, |
| 1254 | {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE}, |
| 1255 | {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE}, |
| 1256 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1257 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1258 | return yin_parse_content(ctx, subelems, 9, any_kw, NULL, &any->exts); |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /** |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1262 | * @brief parse leaf element. |
| 1263 | * |
| 1264 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1265 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1266 | * |
| 1267 | * @return LY_ERR values. |
| 1268 | */ |
| 1269 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1270 | yin_parse_leaf(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta) |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1271 | { |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1272 | struct lysp_node_leaf *leaf; |
| 1273 | |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 1274 | /* create structure new leaf */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1275 | LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM); |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1276 | leaf->nodetype = LYS_LEAF; |
| 1277 | leaf->parent = node_meta->parent; |
| 1278 | |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1279 | /* parser argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1280 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1281 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, LY_STMT_LEAF)); |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1282 | |
| 1283 | /* parse content */ |
| 1284 | struct yin_subelement subelems[12] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1285 | {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE}, |
| 1286 | {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE}, |
| 1287 | {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE}, |
| 1288 | {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0}, |
| 1289 | {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE}, |
| 1290 | {LY_STMT_MUST, &leaf->musts, 0}, |
| 1291 | {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE}, |
| 1292 | {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE}, |
| 1293 | {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY}, |
| 1294 | {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE}, |
| 1295 | {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE}, |
| 1296 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 1297 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1298 | return yin_parse_content(ctx, subelems, 12, LY_STMT_LEAF, NULL, &leaf->exts); |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /** |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1302 | * @brief Parse leaf-list element. |
| 1303 | * |
| 1304 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1305 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1306 | * |
| 1307 | * @return LY_ERR values. |
| 1308 | */ |
| 1309 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1310 | yin_parse_leaflist(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta) |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1311 | { |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1312 | struct lysp_node_leaflist *llist; |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1313 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1314 | LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM); |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 1315 | |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1316 | llist->nodetype = LYS_LEAFLIST; |
| 1317 | llist->parent = node_meta->parent; |
| 1318 | |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1319 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1320 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1321 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, LY_STMT_LEAF_LIST)); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1322 | |
| 1323 | /* parse content */ |
| 1324 | struct yin_subelement subelems[14] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1325 | {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE}, |
| 1326 | {LY_STMT_DEFAULT, &llist->dflts, 0}, |
| 1327 | {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE}, |
| 1328 | {LY_STMT_IF_FEATURE, &llist->iffeatures, 0}, |
| 1329 | {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE}, |
| 1330 | {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE}, |
| 1331 | {LY_STMT_MUST, &llist->musts, 0}, |
| 1332 | {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE}, |
| 1333 | {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE}, |
| 1334 | {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE}, |
| 1335 | {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY}, |
| 1336 | {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE}, |
| 1337 | {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE}, |
| 1338 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1339 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1340 | LY_CHECK_RET(yin_parse_content(ctx, subelems, 14, LY_STMT_LEAF_LIST, NULL, &llist->exts)); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1341 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1342 | /* check invalid combination of subelements */ |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1343 | if ((llist->min) && (llist->dflts)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1344 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1345 | return LY_EVALID; |
| 1346 | } |
| 1347 | if (llist->max && llist->min > llist->max) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1348 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1349 | return LY_EVALID; |
| 1350 | } |
| 1351 | |
| 1352 | return LY_SUCCESS; |
| 1353 | } |
| 1354 | |
| 1355 | /** |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1356 | * @brief Parse typedef element. |
| 1357 | * |
| 1358 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 1359 | * @param[in] typedef_meta Meta information about parent node and typedefs to add to. |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1360 | * |
| 1361 | * @return LY_ERR values. |
| 1362 | */ |
| 1363 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1364 | yin_parse_typedef(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *typedef_meta) |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1365 | { |
| 1366 | struct lysp_tpdf *tpdf; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1367 | struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1368 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1369 | |
| 1370 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1371 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1372 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, LY_STMT_TYPEDEF)); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1373 | |
| 1374 | /* parse content */ |
| 1375 | struct yin_subelement subelems[7] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1376 | {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE}, |
| 1377 | {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE}, |
| 1378 | {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE}, |
| 1379 | {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE}, |
| 1380 | {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY}, |
| 1381 | {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE}, |
| 1382 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1383 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1384 | LY_CHECK_RET(yin_parse_content(ctx, subelems, 7, LY_STMT_TYPEDEF, NULL, &tpdf->exts)); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1385 | |
| 1386 | /* store data for collision check */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1387 | if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) { |
David Sedlák | e8b74df | 2019-08-14 14:18:22 +0200 | [diff] [blame] | 1388 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0) == -1, LY_EMEM); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | return LY_SUCCESS; |
| 1392 | } |
| 1393 | |
| 1394 | /** |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1395 | * @brief Parse refine element. |
| 1396 | * |
| 1397 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1398 | * @param[in,out] refines Refines to add to. |
| 1399 | * |
| 1400 | * @return LY_ERR values. |
| 1401 | */ |
| 1402 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1403 | yin_parse_refine(struct lys_yin_parser_ctx *ctx, struct lysp_refine **refines) |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1404 | { |
| 1405 | struct lysp_refine *rf; |
| 1406 | |
| 1407 | /* allocate new refine */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1408 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM); |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1409 | |
| 1410 | /* parse attribute */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1411 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1412 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE)); |
| 1413 | CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine"); |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1414 | |
| 1415 | /* parse content */ |
| 1416 | struct yin_subelement subelems[11] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1417 | {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE}, |
| 1418 | {LY_STMT_DEFAULT, &rf->dflts, 0}, |
| 1419 | {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE}, |
| 1420 | {LY_STMT_IF_FEATURE, &rf->iffeatures, 0}, |
| 1421 | {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE}, |
| 1422 | {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE}, |
| 1423 | {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE}, |
| 1424 | {LY_STMT_MUST, &rf->musts, 0}, |
| 1425 | {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE}, |
| 1426 | {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE}, |
| 1427 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1428 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1429 | return yin_parse_content(ctx, subelems, 11, LY_STMT_REFINE, NULL, &rf->exts); |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /** |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1433 | * @brief Parse uses element. |
| 1434 | * |
| 1435 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1436 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1437 | * |
| 1438 | * @return LY_ERR values. |
| 1439 | */ |
| 1440 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1441 | yin_parse_uses(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta) |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1442 | { |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1443 | struct lysp_node_uses *uses; |
| 1444 | |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 1445 | /* create new uses */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1446 | LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM); |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1447 | uses->nodetype = LYS_USES; |
| 1448 | uses->parent = node_meta->parent; |
| 1449 | |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1450 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1451 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1452 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, LY_STMT_USES)); |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1453 | |
| 1454 | /* parse content */ |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 1455 | struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments}; |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1456 | struct yin_subelement subelems[8] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1457 | {LY_STMT_AUGMENT, &augments, 0}, |
| 1458 | {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE}, |
| 1459 | {LY_STMT_IF_FEATURE, &uses->iffeatures, 0}, |
| 1460 | {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE}, |
| 1461 | {LY_STMT_REFINE, &uses->refines, 0}, |
| 1462 | {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE}, |
| 1463 | {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE}, |
| 1464 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1465 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1466 | LY_CHECK_RET(yin_parse_content(ctx, subelems, 8, LY_STMT_USES, NULL, &uses->exts)); |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1467 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL)); |
| 1468 | |
| 1469 | return LY_SUCCESS; |
| 1470 | } |
| 1471 | |
| 1472 | /** |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1473 | * @brief Parse revision element. |
| 1474 | * |
| 1475 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1476 | * @param[in,out] revs Parsed revisions to add to. |
| 1477 | * |
| 1478 | * @return LY_ERR values. |
| 1479 | */ |
| 1480 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1481 | yin_parse_revision(struct lys_yin_parser_ctx *ctx, struct lysp_revision **revs) |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1482 | { |
| 1483 | struct lysp_revision *rev; |
| 1484 | const char *temp_date = NULL; |
| 1485 | |
| 1486 | /* allocate new reivison */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1487 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM); |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1488 | |
| 1489 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1490 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1491 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_date, Y_STR_ARG, LY_STMT_REVISION)); |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1492 | /* check value */ |
| 1493 | if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1494 | FREE_STRING(ctx->xmlctx->ctx, temp_date); |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1495 | return LY_EVALID; |
| 1496 | } |
| 1497 | strcpy(rev->date, temp_date); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1498 | FREE_STRING(ctx->xmlctx->ctx, temp_date); |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1499 | |
| 1500 | /* parse content */ |
| 1501 | struct yin_subelement subelems[3] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1502 | {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE}, |
| 1503 | {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE}, |
| 1504 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1505 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1506 | return yin_parse_content(ctx, subelems, 3, LY_STMT_REVISION, NULL, &rev->exts); |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1509 | /** |
| 1510 | * @brief Parse include element. |
| 1511 | * |
| 1512 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1513 | * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to. |
| 1514 | * |
| 1515 | * @return LY_ERR values. |
| 1516 | */ |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1517 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1518 | yin_parse_include(struct lys_yin_parser_ctx *ctx, struct include_meta *inc_meta) |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1519 | { |
| 1520 | struct lysp_include *inc; |
| 1521 | |
| 1522 | /* allocate new include */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1523 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM); |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1524 | |
| 1525 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1526 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1527 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, LY_STMT_INCLUDE)); |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1528 | |
| 1529 | /* submodules share the namespace with the module names, so there must not be |
| 1530 | * a module of the same name in the context, no need for revision matching */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1531 | if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1532 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, inc->name); |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1533 | return LY_EVALID; |
| 1534 | } |
| 1535 | |
| 1536 | /* parse content */ |
| 1537 | struct yin_subelement subelems[4] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1538 | {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2}, |
| 1539 | {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2}, |
| 1540 | {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE}, |
| 1541 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1542 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1543 | return yin_parse_content(ctx, subelems, 4, LY_STMT_INCLUDE, NULL, &inc->exts); |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1544 | } |
| 1545 | |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1546 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1547 | * @brief Parse revision-date element. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1548 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1549 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1550 | * @param[in,out] rev Array to store the parsed value in. |
| 1551 | * @param[in,out] exts Extension instances to add to. |
| 1552 | * |
| 1553 | * @return LY_ERR values. |
| 1554 | */ |
| 1555 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1556 | yin_parse_revision_date(struct lys_yin_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1557 | { |
| 1558 | const char *temp_rev; |
| 1559 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1560 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1561 | }; |
| 1562 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1563 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1564 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1565 | LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1566 | FREE_STRING(ctx->xmlctx->ctx, temp_rev), LY_EVALID); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1567 | |
| 1568 | strcpy(rev, temp_rev); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1569 | FREE_STRING(ctx->xmlctx->ctx, temp_rev); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1570 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1571 | return yin_parse_content(ctx, subelems, 1, LY_STMT_REVISION_DATE, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /** |
| 1575 | * @brief Parse config element. |
| 1576 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1577 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1578 | * @param[in,out] flags Flags to add to. |
| 1579 | * @param[in,out] exts Extension instances to add to. |
| 1580 | * |
| 1581 | * @return LY_ERR values. |
| 1582 | */ |
| 1583 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1584 | yin_parse_config(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1585 | { |
| 1586 | const char *temp_val = NULL; |
| 1587 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1588 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1589 | }; |
| 1590 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1591 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1592 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_CONFIG)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1593 | if (strcmp(temp_val, "true") == 0) { |
| 1594 | *flags |= LYS_CONFIG_W; |
| 1595 | } else if (strcmp(temp_val, "false") == 0) { |
| 1596 | *flags |= LYS_CONFIG_R; |
| 1597 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1598 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config", |
| 1599 | "true", "false"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1600 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1601 | return LY_EVALID; |
| 1602 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1603 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1604 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1605 | return yin_parse_content(ctx, subelems, 1, LY_STMT_CONFIG, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * @brief Parse yang-version element. |
| 1610 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1611 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1612 | * @param[out] version Storage for the parsed information. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1613 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1614 | * |
| 1615 | * @return LY_ERR values. |
| 1616 | */ |
| 1617 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1618 | yin_parse_yangversion(struct lys_yin_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1619 | { |
| 1620 | const char *temp_version = NULL; |
| 1621 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1622 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1623 | }; |
| 1624 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1625 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1626 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, LY_STMT_YANG_VERSION)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1627 | if (strcmp(temp_version, "1.0") == 0) { |
| 1628 | *version = LYS_VERSION_1_0; |
| 1629 | } else if (strcmp(temp_version, "1.1") == 0) { |
| 1630 | *version = LYS_VERSION_1_1; |
| 1631 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1632 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value", |
| 1633 | "yang-version", "1.0", "1.1"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1634 | FREE_STRING(ctx->xmlctx->ctx, temp_version); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1635 | return LY_EVALID; |
| 1636 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1637 | FREE_STRING(ctx->xmlctx->ctx, temp_version); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1638 | ctx->mod_version = *version; |
| 1639 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1640 | return yin_parse_content(ctx, subelems, 1, LY_STMT_YANG_VERSION, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /** |
| 1644 | * @brief Parse import element. |
| 1645 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1646 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1647 | * @param[in,out] imp_meta Meta information about prefix and imports to add to. |
| 1648 | * |
| 1649 | * @return LY_ERR values. |
| 1650 | */ |
| 1651 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1652 | yin_parse_import(struct lys_yin_parser_ctx *ctx, struct import_meta *imp_meta) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1653 | { |
| 1654 | struct lysp_import *imp; |
| 1655 | /* allocate new element in sized array for import */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1656 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1657 | |
| 1658 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1659 | {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE}, |
| 1660 | {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 1661 | {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE}, |
| 1662 | {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE}, |
| 1663 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1664 | }; |
| 1665 | |
| 1666 | /* parse import attributes */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1667 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1668 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT)); |
| 1669 | LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, LY_STMT_IMPORT, NULL, &imp->exts)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1670 | /* check prefix validity */ |
| 1671 | LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID); |
| 1672 | |
| 1673 | return LY_SUCCESS; |
| 1674 | } |
| 1675 | |
| 1676 | /** |
| 1677 | * @brief Parse mandatory element. |
| 1678 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1679 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1680 | * @param[in,out] flags Flags to add to. |
| 1681 | * @param[in,out] exts Extension instances to add to. |
| 1682 | * |
| 1683 | * @return LY_ERR values. |
| 1684 | */ |
| 1685 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1686 | yin_parse_mandatory(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1687 | { |
| 1688 | const char *temp_val = NULL; |
| 1689 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1690 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1691 | }; |
| 1692 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1693 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1694 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MANDATORY)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1695 | if (strcmp(temp_val, "true") == 0) { |
| 1696 | *flags |= LYS_MAND_TRUE; |
| 1697 | } else if (strcmp(temp_val, "false") == 0) { |
| 1698 | *flags |= LYS_MAND_FALSE; |
| 1699 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1700 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", |
| 1701 | "mandatory", "true", "false"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1702 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1703 | return LY_EVALID; |
| 1704 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1705 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1706 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1707 | return yin_parse_content(ctx, subelems, 1, LY_STMT_MANDATORY, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | /** |
| 1711 | * @brief Parse status element. |
| 1712 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1713 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1714 | * @param[in,out] flags Flags to add to. |
| 1715 | * @param[in,out] exts Extension instances to add to. |
| 1716 | * |
| 1717 | * @return LY_ERR values. |
| 1718 | */ |
| 1719 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1720 | yin_parse_status(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1721 | { |
| 1722 | const char *value = NULL; |
| 1723 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1724 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1725 | }; |
| 1726 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1727 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1728 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &value, Y_STR_ARG, LY_STMT_STATUS)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1729 | if (strcmp(value, "current") == 0) { |
| 1730 | *flags |= LYS_STATUS_CURR; |
| 1731 | } else if (strcmp(value, "deprecated") == 0) { |
| 1732 | *flags |= LYS_STATUS_DEPRC; |
| 1733 | } else if (strcmp(value, "obsolete") == 0) { |
| 1734 | *flags |= LYS_STATUS_OBSLT; |
| 1735 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1736 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value", |
| 1737 | "status", "current", "deprecated", "obsolete"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1738 | FREE_STRING(ctx->xmlctx->ctx, value); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1739 | return LY_EVALID; |
| 1740 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1741 | FREE_STRING(ctx->xmlctx->ctx, value); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1742 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1743 | return yin_parse_content(ctx, subelems, 1, LY_STMT_STATUS, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | /** |
| 1747 | * @brief Parse when element. |
| 1748 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1749 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1750 | * @param[out] when_p When pointer to parse to. |
| 1751 | */ |
| 1752 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1753 | yin_parse_when(struct lys_yin_parser_ctx *ctx, struct lysp_when **when_p) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1754 | { |
| 1755 | struct lysp_when *when; |
David Sedlák | a56e001 | 2019-08-15 13:21:25 +0200 | [diff] [blame] | 1756 | LY_ERR ret = LY_SUCCESS; |
| 1757 | |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1758 | when = calloc(1, sizeof *when); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1759 | LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
| 1760 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1761 | ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN); |
David Sedlák | a56e001 | 2019-08-15 13:21:25 +0200 | [diff] [blame] | 1762 | LY_CHECK_ERR_RET(ret, free(when), ret); |
| 1763 | |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1764 | *when_p = when; |
| 1765 | struct yin_subelement subelems[3] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1766 | {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE}, |
| 1767 | {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE}, |
| 1768 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1769 | }; |
| 1770 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1771 | return yin_parse_content(ctx, subelems, 3, LY_STMT_WHEN, NULL, &when->exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | /** |
| 1775 | * @brief Parse yin-elemenet element. |
| 1776 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1777 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1778 | * @param[in,out] data Data to read from, always moved to currently handled position. |
| 1779 | * @param[in,out] flags Flags to add to. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1780 | * @prama[in,out] exts Extension instances to add to. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1781 | * |
| 1782 | * @return LY_ERR values. |
| 1783 | */ |
| 1784 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1785 | yin_parse_yin_element(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1786 | { |
| 1787 | const char *temp_val = NULL; |
| 1788 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1789 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1790 | }; |
| 1791 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1792 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1793 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_YIN_ELEMENT)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1794 | if (strcmp(temp_val, "true") == 0) { |
| 1795 | *flags |= LYS_YINELEM_TRUE; |
| 1796 | } else if (strcmp(temp_val, "false") == 0) { |
| 1797 | *flags |= LYS_YINELEM_FALSE; |
| 1798 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1799 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", |
| 1800 | "yin-element", "true", "false"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1801 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1802 | return LY_EVALID; |
| 1803 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1804 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1805 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1806 | return yin_parse_content(ctx, subelems, 1, LY_STMT_YIN_ELEMENT, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | /** |
| 1810 | * @brief Parse argument element. |
| 1811 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1812 | * @param[in,out] xmlctx Xml context. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1813 | * @param[in,out] arg_meta Meta information about destionation of parsed data. |
| 1814 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1815 | * |
| 1816 | * @return LY_ERR values. |
| 1817 | */ |
| 1818 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1819 | yin_parse_argument(struct lys_yin_parser_ctx *ctx, struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1820 | { |
| 1821 | struct yin_subelement subelems[2] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1822 | {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE}, |
| 1823 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1824 | }; |
| 1825 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1826 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1827 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, LY_STMT_ARGUMENT)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1828 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1829 | return yin_parse_content(ctx, subelems, 2, LY_STMT_ARGUMENT, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | /** |
| 1833 | * @brief Parse the extension statement. |
| 1834 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1835 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1836 | * @param[in,out] extensions Extensions to add to. |
| 1837 | * |
| 1838 | * @return LY_ERR values. |
| 1839 | */ |
| 1840 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1841 | yin_parse_extension(struct lys_yin_parser_ctx *ctx, struct lysp_ext **extensions) |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1842 | { |
| 1843 | struct lysp_ext *ex; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1844 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM); |
| 1845 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1846 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, LY_STMT_EXTENSION)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1847 | |
| 1848 | struct yin_argument_meta arg_info = {&ex->flags, &ex->argument}; |
| 1849 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1850 | {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE}, |
| 1851 | {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE}, |
| 1852 | {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE}, |
| 1853 | {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE}, |
| 1854 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1855 | }; |
| 1856 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1857 | return yin_parse_content(ctx, subelems, 5, LY_STMT_EXTENSION, NULL, &ex->exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | /** |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1861 | * @brief Parse feature element. |
| 1862 | * |
| 1863 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1864 | * @param[in,out] features Features to add to. |
| 1865 | * |
| 1866 | * @return LY_ERR values. |
| 1867 | */ |
| 1868 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1869 | yin_parse_feature(struct lys_yin_parser_ctx *ctx, struct lysp_feature **features) |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1870 | { |
| 1871 | struct lysp_feature *feat; |
| 1872 | |
| 1873 | /* allocate new feature */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1874 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM); |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1875 | |
| 1876 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1877 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1878 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, LY_STMT_FEATURE)); |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1879 | |
| 1880 | /* parse content */ |
| 1881 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1882 | {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE}, |
| 1883 | {LY_STMT_IF_FEATURE, &feat->iffeatures, 0}, |
| 1884 | {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE}, |
| 1885 | {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE}, |
| 1886 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1887 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1888 | return yin_parse_content(ctx, subelems, 5, LY_STMT_FEATURE, NULL, &feat->exts); |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | /** |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 1892 | * @brief Parse identity element. |
| 1893 | * |
| 1894 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 1895 | * @param[in,out] identities Identities to add to. |
| 1896 | * |
| 1897 | * @return LY_ERR values. |
| 1898 | */ |
| 1899 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1900 | yin_parse_identity(struct lys_yin_parser_ctx *ctx, struct lysp_ident **identities) |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 1901 | { |
| 1902 | struct lysp_ident *ident; |
| 1903 | |
| 1904 | /* allocate new identity */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1905 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM); |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 1906 | |
| 1907 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1908 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1909 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, LY_STMT_IDENTITY)); |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 1910 | |
| 1911 | /* parse content */ |
| 1912 | struct yin_subelement subelems[6] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1913 | {LY_STMT_BASE, &ident->bases, 0}, |
| 1914 | {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE}, |
| 1915 | {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2}, |
| 1916 | {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE}, |
| 1917 | {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE}, |
| 1918 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 1919 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1920 | return yin_parse_content(ctx, subelems, 6, LY_STMT_IDENTITY, NULL, &ident->exts); |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | /** |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1924 | * @brief Parse list element. |
| 1925 | * |
| 1926 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1927 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1928 | * |
| 1929 | * @return LY_ERR values. |
| 1930 | */ |
| 1931 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1932 | yin_parse_list(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta) |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1933 | { |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1934 | struct lysp_node_list *list; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 1935 | LY_ERR ret = LY_SUCCESS; |
| 1936 | struct yin_subelement *subelems = NULL; |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1937 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1938 | LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM); |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1939 | list->nodetype = LYS_LIST; |
| 1940 | list->parent = node_meta->parent; |
| 1941 | |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1942 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1943 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 1944 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, LY_STMT_LIST)); |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1945 | |
| 1946 | /* parse list content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 1947 | LY_CHECK_RET(subelems_allocator(ctx, 25, (struct lysp_node *)list, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1948 | LY_STMT_ACTION, &list->actions, 0, |
| 1949 | LY_STMT_ANYDATA, &list->child, 0, |
| 1950 | LY_STMT_ANYXML, &list->child, 0, |
| 1951 | LY_STMT_CHOICE, &list->child, 0, |
| 1952 | LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE, |
| 1953 | LY_STMT_CONTAINER, &list->child, 0, |
| 1954 | LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE, |
| 1955 | LY_STMT_GROUPING, &list->groupings, 0, |
| 1956 | LY_STMT_IF_FEATURE, &list->iffeatures, 0, |
| 1957 | LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE, |
| 1958 | LY_STMT_LEAF, &list->child, 0, |
| 1959 | LY_STMT_LEAF_LIST, &list->child, 0, |
| 1960 | LY_STMT_LIST, &list->child, 0, |
| 1961 | LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE, |
| 1962 | LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE, |
| 1963 | LY_STMT_MUST, &list->musts, 0, |
| 1964 | LY_STMT_NOTIFICATION, &list->notifs, 0, |
| 1965 | LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE, |
| 1966 | LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE, |
| 1967 | LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE, |
| 1968 | LY_STMT_TYPEDEF, &list->typedefs, 0, |
| 1969 | LY_STMT_UNIQUE, &list->uniques, 0, |
| 1970 | LY_STMT_USES, &list->child, 0, |
| 1971 | LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE, |
| 1972 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 1973 | )); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1974 | ret = yin_parse_content(ctx, subelems, 25, LY_STMT_LIST, NULL, &list->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 1975 | subelems_deallocator(25, subelems); |
| 1976 | LY_CHECK_RET(ret); |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1977 | |
| 1978 | /* finalize parent pointers to the reallocated items */ |
| 1979 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs)); |
| 1980 | |
| 1981 | if (list->max && list->min > list->max) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1982 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max); |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 1983 | return LY_EVALID; |
| 1984 | } |
| 1985 | |
| 1986 | return LY_SUCCESS; |
| 1987 | } |
| 1988 | |
| 1989 | /** |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 1990 | * @brief Parse notification element. |
| 1991 | * |
| 1992 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 1993 | * @param[in,out] notif_meta Meta information about parent node and notifications to add to. |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 1994 | * |
| 1995 | * @return LY_ERR values. |
| 1996 | */ |
| 1997 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1998 | yin_parse_notification(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *notif_meta) |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 1999 | { |
| 2000 | struct lysp_notif *notif; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2001 | struct lysp_notif **notifs = (struct lysp_notif **)notif_meta->nodes; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2002 | LY_ERR ret = LY_SUCCESS; |
| 2003 | struct yin_subelement *subelems = NULL; |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2004 | |
| 2005 | /* allocate new notification */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2006 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *notifs, notif, LY_EMEM); |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2007 | notif->nodetype = LYS_NOTIF; |
| 2008 | notif->parent = notif_meta->parent; |
| 2009 | |
| 2010 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2011 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2012 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, ¬if->name, Y_IDENTIF_ARG, LY_STMT_NOTIFICATION)); |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2013 | |
| 2014 | /* parse notification content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2015 | LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)notif, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2016 | LY_STMT_ANYDATA, ¬if->data, 0, |
| 2017 | LY_STMT_ANYXML, ¬if->data, 0, |
| 2018 | LY_STMT_CHOICE, ¬if->data, 0, |
| 2019 | LY_STMT_CONTAINER, ¬if->data, 0, |
| 2020 | LY_STMT_DESCRIPTION, ¬if->dsc, YIN_SUBELEM_UNIQUE, |
| 2021 | LY_STMT_GROUPING, ¬if->groupings, 0, |
| 2022 | LY_STMT_IF_FEATURE, ¬if->iffeatures, 0, |
| 2023 | LY_STMT_LEAF, ¬if->data, 0, |
| 2024 | LY_STMT_LEAF_LIST, ¬if->data, 0, |
| 2025 | LY_STMT_LIST, ¬if->data, 0, |
| 2026 | LY_STMT_MUST, ¬if->musts, YIN_SUBELEM_VER2, |
| 2027 | LY_STMT_REFERENCE, ¬if->ref, YIN_SUBELEM_UNIQUE, |
| 2028 | LY_STMT_STATUS, ¬if->flags, YIN_SUBELEM_UNIQUE, |
| 2029 | LY_STMT_TYPEDEF, ¬if->typedefs, 0, |
| 2030 | LY_STMT_USES, ¬if->data, 0, |
| 2031 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2032 | )); |
| 2033 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2034 | ret = yin_parse_content(ctx, subelems, 16, LY_STMT_NOTIFICATION, NULL, ¬if->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2035 | subelems_deallocator(16, subelems); |
| 2036 | LY_CHECK_RET(ret); |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2037 | |
| 2038 | /* finalize parent pointers to the reallocated items */ |
| 2039 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL)); |
| 2040 | |
| 2041 | return LY_SUCCESS; |
| 2042 | } |
| 2043 | |
| 2044 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2045 | * @brief Parse grouping element. |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2046 | * |
| 2047 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2048 | * @param[in,out] gr_meta Meta information about parent node and groupings to add to. |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2049 | * |
| 2050 | * @return LY_ERR values. |
| 2051 | */ |
| 2052 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2053 | yin_parse_grouping(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *gr_meta) |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2054 | { |
| 2055 | struct lysp_grp *grp; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2056 | struct lysp_grp **grps = (struct lysp_grp **)gr_meta->nodes; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2057 | LY_ERR ret = LY_SUCCESS; |
| 2058 | struct yin_subelement *subelems = NULL; |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2059 | |
| 2060 | /* create new grouping */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2061 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *grps, grp, LY_EMEM); |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2062 | grp->nodetype = LYS_GROUPING; |
| 2063 | grp->parent = gr_meta->parent; |
| 2064 | |
| 2065 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2066 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2067 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, LY_STMT_GROUPING)); |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2068 | |
| 2069 | /* parse grouping content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2070 | LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)grp, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2071 | LY_STMT_ACTION, &grp->actions, 0, |
| 2072 | LY_STMT_ANYDATA, &grp->data, 0, |
| 2073 | LY_STMT_ANYXML, &grp->data, 0, |
| 2074 | LY_STMT_CHOICE, &grp->data, 0, |
| 2075 | LY_STMT_CONTAINER, &grp->data, 0, |
| 2076 | LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE, |
| 2077 | LY_STMT_GROUPING, &grp->groupings, 0, |
| 2078 | LY_STMT_LEAF, &grp->data, 0, |
| 2079 | LY_STMT_LEAF_LIST, &grp->data, 0, |
| 2080 | LY_STMT_LIST, &grp->data, 0, |
| 2081 | LY_STMT_NOTIFICATION, &grp->notifs, 0, |
| 2082 | LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE, |
| 2083 | LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE, |
| 2084 | LY_STMT_TYPEDEF, &grp->typedefs, 0, |
| 2085 | LY_STMT_USES, &grp->data, 0, |
| 2086 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2087 | )); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2088 | ret = yin_parse_content(ctx, subelems, 16, LY_STMT_GROUPING, NULL, &grp->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2089 | subelems_deallocator(16, subelems); |
| 2090 | LY_CHECK_RET(ret); |
| 2091 | |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2092 | /* finalize parent pointers to the reallocated items */ |
| 2093 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs)); |
| 2094 | |
| 2095 | return LY_SUCCESS; |
| 2096 | } |
| 2097 | |
| 2098 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2099 | * @brief Parse container element. |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2100 | * |
| 2101 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 2102 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2103 | * |
| 2104 | * @return LY_ERR values. |
| 2105 | */ |
| 2106 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2107 | yin_parse_container(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta) |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2108 | { |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2109 | struct lysp_node_container *cont; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2110 | LY_ERR ret = LY_SUCCESS; |
| 2111 | struct yin_subelement *subelems = NULL; |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2112 | |
| 2113 | /* create new container */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2114 | LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM); |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2115 | cont->nodetype = LYS_CONTAINER; |
| 2116 | cont->parent = node_meta->parent; |
| 2117 | |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2118 | /* parse aegument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2119 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2120 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, LY_STMT_CONTAINER)); |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2121 | |
| 2122 | /* parse container content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2123 | LY_CHECK_RET(subelems_allocator(ctx, 21, (struct lysp_node *)cont, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2124 | LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2, |
| 2125 | LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2, |
| 2126 | LY_STMT_ANYXML, &cont->child, 0, |
| 2127 | LY_STMT_CHOICE, &cont->child, 0, |
| 2128 | LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE, |
| 2129 | LY_STMT_CONTAINER, &cont->child, 0, |
| 2130 | LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE, |
| 2131 | LY_STMT_GROUPING, &cont->groupings, 0, |
| 2132 | LY_STMT_IF_FEATURE, &cont->iffeatures, 0, |
| 2133 | LY_STMT_LEAF, &cont->child, 0, |
| 2134 | LY_STMT_LEAF_LIST, &cont->child, 0, |
| 2135 | LY_STMT_LIST, &cont->child, 0, |
| 2136 | LY_STMT_MUST, &cont->musts, 0, |
| 2137 | LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2, |
| 2138 | LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE, |
| 2139 | LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE, |
| 2140 | LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE, |
| 2141 | LY_STMT_TYPEDEF, &cont->typedefs, 0, |
| 2142 | LY_STMT_USES, &cont->child, 0, |
| 2143 | LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE, |
| 2144 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2145 | )); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2146 | ret = yin_parse_content(ctx, subelems, 21, LY_STMT_CONTAINER, NULL, &cont->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2147 | subelems_deallocator(21, subelems); |
| 2148 | LY_CHECK_RET(ret); |
| 2149 | |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2150 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs)); |
| 2151 | |
| 2152 | return LY_SUCCESS; |
| 2153 | } |
| 2154 | |
| 2155 | /** |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2156 | * @brief Parse case element. |
| 2157 | * |
| 2158 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 2159 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2160 | * |
| 2161 | * @return LY_ERR values. |
| 2162 | */ |
| 2163 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2164 | yin_parse_case(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta) |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2165 | { |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2166 | struct lysp_node_case *cas; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2167 | LY_ERR ret = LY_SUCCESS; |
| 2168 | struct yin_subelement *subelems = NULL;; |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2169 | |
| 2170 | /* create new case */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2171 | LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM); |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2172 | cas->nodetype = LYS_CASE; |
| 2173 | cas->parent = node_meta->parent; |
| 2174 | |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2175 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2176 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2177 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, LY_STMT_CASE)); |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2178 | |
| 2179 | /* parse case content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2180 | LY_CHECK_RET(subelems_allocator(ctx, 14, (struct lysp_node *)cas, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2181 | LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2, |
| 2182 | LY_STMT_ANYXML, &cas->child, 0, |
| 2183 | LY_STMT_CHOICE, &cas->child, 0, |
| 2184 | LY_STMT_CONTAINER, &cas->child, 0, |
| 2185 | LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE, |
| 2186 | LY_STMT_IF_FEATURE, &cas->iffeatures, 0, |
| 2187 | LY_STMT_LEAF, &cas->child, 0, |
| 2188 | LY_STMT_LEAF_LIST, &cas->child, 0, |
| 2189 | LY_STMT_LIST, &cas->child, 0, |
| 2190 | LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE, |
| 2191 | LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE, |
| 2192 | LY_STMT_USES, &cas->child, 0, |
| 2193 | LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE, |
| 2194 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2195 | )); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2196 | ret = yin_parse_content(ctx, subelems, 14, LY_STMT_CASE, NULL, &cas->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2197 | subelems_deallocator(14, subelems); |
| 2198 | |
| 2199 | return ret; |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | /** |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2203 | * @brief Parse choice element. |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2204 | * |
| 2205 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 2206 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2207 | * |
| 2208 | * @return LY_ERR values. |
| 2209 | */ |
| 2210 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2211 | yin_parse_choice(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta) |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2212 | { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2213 | LY_ERR ret = LY_SUCCESS; |
| 2214 | struct yin_subelement *subelems = NULL; |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2215 | struct lysp_node_choice *choice; |
| 2216 | |
| 2217 | /* create new choice */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2218 | LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM); |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 2219 | |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2220 | choice->nodetype = LYS_CHOICE; |
| 2221 | choice->parent = node_meta->parent; |
| 2222 | |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2223 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2224 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2225 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, LY_STMT_CHOICE)); |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2226 | |
| 2227 | /* parse choice content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2228 | LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)choice, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2229 | LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2, |
| 2230 | LY_STMT_ANYXML, &choice->child, 0, |
| 2231 | LY_STMT_CASE, &choice->child, 0, |
| 2232 | LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2, |
| 2233 | LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE, |
| 2234 | LY_STMT_CONTAINER, &choice->child, 0, |
| 2235 | LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE, |
| 2236 | LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE, |
| 2237 | LY_STMT_IF_FEATURE, &choice->iffeatures, 0, |
| 2238 | LY_STMT_LEAF, &choice->child, 0, |
| 2239 | LY_STMT_LEAF_LIST, &choice->child, 0, |
| 2240 | LY_STMT_LIST, &choice->child, 0, |
| 2241 | LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE, |
| 2242 | LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE, |
| 2243 | LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE, |
| 2244 | LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE, |
| 2245 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2246 | )); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2247 | ret = yin_parse_content(ctx, subelems, 17, LY_STMT_CHOICE, NULL, &choice->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2248 | subelems_deallocator(17, subelems); |
| 2249 | return ret; |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | /** |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2253 | * @brief Parse input or output element. |
| 2254 | * |
| 2255 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2256 | * @param[in] inout_kw Identification of input/output element. |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2257 | * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to. |
| 2258 | * |
| 2259 | * @return LY_ERR values. |
| 2260 | */ |
| 2261 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2262 | yin_parse_inout(struct lys_yin_parser_ctx *ctx, enum ly_stmt inout_kw, struct inout_meta *inout_meta) |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2263 | { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2264 | LY_ERR ret = LY_SUCCESS; |
| 2265 | struct yin_subelement *subelems = NULL; |
| 2266 | |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2267 | /* initiate structure */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2268 | inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT; |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2269 | inout_meta->inout_p->parent = inout_meta->parent; |
| 2270 | |
| 2271 | /* check attributes */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2272 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2273 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw)); |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2274 | |
| 2275 | /* parser input/output content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2276 | LY_CHECK_RET(subelems_allocator(ctx, 12, (struct lysp_node *)inout_meta->inout_p, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2277 | LY_STMT_ANYDATA, &inout_meta->inout_p->data, YIN_SUBELEM_VER2, |
| 2278 | LY_STMT_ANYXML, &inout_meta->inout_p->data, 0, |
| 2279 | LY_STMT_CHOICE, &inout_meta->inout_p->data, 0, |
| 2280 | LY_STMT_CONTAINER, &inout_meta->inout_p->data, 0, |
| 2281 | LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0, |
| 2282 | LY_STMT_LEAF, &inout_meta->inout_p->data, 0, |
| 2283 | LY_STMT_LEAF_LIST, &inout_meta->inout_p->data, 0, |
| 2284 | LY_STMT_LIST, &inout_meta->inout_p->data, 0, |
| 2285 | LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2, |
| 2286 | LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0, |
| 2287 | LY_STMT_USES, &inout_meta->inout_p->data, 0, |
| 2288 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2289 | )); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2290 | ret = yin_parse_content(ctx, subelems, 12, inout_kw, NULL, &inout_meta->inout_p->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2291 | subelems_deallocator(12, subelems); |
| 2292 | LY_CHECK_RET(ret); |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2293 | |
Michal Vasko | b83af8a | 2020-01-06 09:49:22 +0100 | [diff] [blame] | 2294 | if (!inout_meta->inout_p->data) { |
| 2295 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw)); |
| 2296 | return LY_EVALID; |
| 2297 | } |
| 2298 | |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2299 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2300 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_meta->inout_p->groupings, NULL, NULL, NULL)); |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2301 | |
| 2302 | return LY_SUCCESS; |
| 2303 | } |
| 2304 | |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2305 | /** |
| 2306 | * @brief Parse action element. |
| 2307 | * |
| 2308 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2309 | * @param[in] act_meta Meta information about parent node and actions to add to. |
| 2310 | * |
| 2311 | * @return LY_ERR values. |
| 2312 | */ |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2313 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2314 | yin_parse_action(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *act_meta) |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2315 | { |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2316 | struct lysp_action *act, **acts = (struct lysp_action **)act_meta->nodes; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2317 | LY_ERR ret = LY_SUCCESS; |
| 2318 | struct yin_subelement *subelems = NULL; |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2319 | |
| 2320 | /* create new action */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2321 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *acts, act, LY_EMEM); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2322 | act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC; |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2323 | act->parent = act_meta->parent; |
| 2324 | |
| 2325 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2326 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2327 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, LY_STMT_ACTION)); |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2328 | |
| 2329 | /* parse content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2330 | LY_CHECK_RET(subelems_allocator(ctx, 9, (struct lysp_node *)act, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2331 | LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE, |
| 2332 | LY_STMT_GROUPING, &act->groupings, 0, |
| 2333 | LY_STMT_IF_FEATURE, &act->iffeatures, 0, |
| 2334 | LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE, |
| 2335 | LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE, |
| 2336 | LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE, |
| 2337 | LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE, |
| 2338 | LY_STMT_TYPEDEF, &act->typedefs, 0, |
| 2339 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2340 | )); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2341 | ret = (yin_parse_content(ctx, subelems, 9, LY_STMT_ACTION, NULL, &act->exts)); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2342 | subelems_deallocator(9, subelems); |
| 2343 | LY_CHECK_RET(ret); |
| 2344 | |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2345 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL)); |
| 2346 | |
| 2347 | return LY_SUCCESS; |
| 2348 | } |
| 2349 | |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2350 | /** |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2351 | * @brief Parse augment element. |
| 2352 | * |
| 2353 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2354 | * @param[in] aug_meta Meta information about parent node and augments to add to. |
| 2355 | * |
| 2356 | * @return LY_ERR values. |
| 2357 | */ |
| 2358 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2359 | yin_parse_augment(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *aug_meta) |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2360 | { |
| 2361 | struct lysp_augment *aug; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2362 | struct lysp_augment **augs = (struct lysp_augment **)aug_meta->nodes; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2363 | LY_ERR ret = LY_SUCCESS; |
| 2364 | struct yin_subelement *subelems = NULL; |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2365 | |
| 2366 | /* create new augment */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2367 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *augs, aug, LY_EMEM); |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2368 | aug->nodetype = LYS_AUGMENT; |
| 2369 | aug->parent = aug_meta->parent; |
| 2370 | |
| 2371 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2372 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2373 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT)); |
| 2374 | CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment"); |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2375 | |
| 2376 | /* parser augment content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2377 | LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)aug, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2378 | LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2, |
| 2379 | LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2, |
| 2380 | LY_STMT_ANYXML, &aug->child, 0, |
| 2381 | LY_STMT_CASE, &aug->child, 0, |
| 2382 | LY_STMT_CHOICE, &aug->child, 0, |
| 2383 | LY_STMT_CONTAINER, &aug->child, 0, |
| 2384 | LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE, |
| 2385 | LY_STMT_IF_FEATURE, &aug->iffeatures, 0, |
| 2386 | LY_STMT_LEAF, &aug->child, 0, |
| 2387 | LY_STMT_LEAF_LIST, &aug->child, 0, |
| 2388 | LY_STMT_LIST, &aug->child, 0, |
| 2389 | LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2, |
| 2390 | LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE, |
| 2391 | LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE, |
| 2392 | LY_STMT_USES, &aug->child, 0, |
| 2393 | LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE, |
| 2394 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2395 | )); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2396 | ret = yin_parse_content(ctx, subelems, 17, LY_STMT_AUGMENT, NULL, &aug->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2397 | subelems_deallocator(17, subelems); |
| 2398 | LY_CHECK_RET(ret); |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2399 | |
| 2400 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs)); |
| 2401 | |
| 2402 | return LY_SUCCESS; |
| 2403 | } |
| 2404 | |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2405 | /** |
| 2406 | * @brief Parse deviate element. |
| 2407 | * |
| 2408 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2409 | * @param[in] deviates Deviates to add to. |
| 2410 | * |
| 2411 | * @return LY_ERR values. |
| 2412 | */ |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2413 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2414 | yin_parse_deviate(struct lys_yin_parser_ctx *ctx, struct lysp_deviate **deviates) |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2415 | { |
| 2416 | LY_ERR ret = LY_SUCCESS; |
| 2417 | uint8_t dev_mod; |
| 2418 | const char *temp_val; |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 2419 | struct lysp_deviate *d; |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2420 | struct lysp_deviate_add *d_add = NULL; |
| 2421 | struct lysp_deviate_rpl *d_rpl = NULL; |
| 2422 | struct lysp_deviate_del *d_del = NULL; |
| 2423 | |
| 2424 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2425 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2426 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_DEVIATE)); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2427 | |
| 2428 | if (strcmp(temp_val, "not-supported") == 0) { |
| 2429 | dev_mod = LYS_DEV_NOT_SUPPORTED; |
| 2430 | } else if (strcmp(temp_val, "add") == 0) { |
| 2431 | dev_mod = LYS_DEV_ADD; |
| 2432 | } else if (strcmp(temp_val, "replace") == 0) { |
| 2433 | dev_mod = LYS_DEV_REPLACE; |
| 2434 | } else if (strcmp(temp_val, "delete") == 0) { |
| 2435 | dev_mod = LYS_DEV_DELETE; |
| 2436 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 2437 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate", |
| 2438 | "not-supported", "add", "replace", "delete"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2439 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2440 | return LY_EVALID; |
| 2441 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2442 | FREE_STRING(ctx->xmlctx->ctx, temp_val); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2443 | |
| 2444 | if (dev_mod == LYS_DEV_NOT_SUPPORTED) { |
| 2445 | d = calloc(1, sizeof *d); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2446 | LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2447 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2448 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2449 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2450 | ret = yin_parse_content(ctx, subelems, 1, LY_STMT_DEVIATE, NULL, &d->exts); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2451 | |
| 2452 | } else if (dev_mod == LYS_DEV_ADD) { |
| 2453 | d_add = calloc(1, sizeof *d_add); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2454 | LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2455 | d = (struct lysp_deviate *)d_add; |
| 2456 | struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts}; |
| 2457 | struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts}; |
| 2458 | struct yin_subelement subelems[9] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2459 | {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE}, |
| 2460 | {LY_STMT_DEFAULT, &d_add->dflts, 0}, |
| 2461 | {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE}, |
| 2462 | {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE}, |
| 2463 | {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE}, |
| 2464 | {LY_STMT_MUST, &d_add->musts, 0}, |
| 2465 | {LY_STMT_UNIQUE, &d_add->uniques, 0}, |
| 2466 | {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE}, |
| 2467 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2468 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2469 | ret = yin_parse_content(ctx, subelems, 9, LY_STMT_DEVIATE, NULL, &d_add->exts); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2470 | |
| 2471 | } else if (dev_mod == LYS_DEV_REPLACE) { |
| 2472 | d_rpl = calloc(1, sizeof *d_rpl); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2473 | LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2474 | d = (struct lysp_deviate *)d_rpl; |
| 2475 | struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts}; |
| 2476 | struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts}; |
| 2477 | struct yin_subelement subelems[8] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2478 | {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE}, |
| 2479 | {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE}, |
| 2480 | {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE}, |
| 2481 | {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE}, |
| 2482 | {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE}, |
| 2483 | {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE}, |
| 2484 | {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE}, |
| 2485 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2486 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2487 | ret = yin_parse_content(ctx, subelems, 8, LY_STMT_DEVIATE, NULL, &d_rpl->exts); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2488 | |
| 2489 | } else { |
| 2490 | d_del = calloc(1, sizeof *d_del); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2491 | LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2492 | d = (struct lysp_deviate *)d_del; |
| 2493 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2494 | {LY_STMT_DEFAULT, &d_del->dflts, 0}, |
| 2495 | {LY_STMT_MUST, &d_del->musts, 0}, |
| 2496 | {LY_STMT_UNIQUE, &d_del->uniques, 0}, |
| 2497 | {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE}, |
| 2498 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2499 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2500 | ret = yin_parse_content(ctx, subelems, 5, LY_STMT_DEVIATE, NULL, &d_del->exts); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2501 | } |
| 2502 | LY_CHECK_GOTO(ret, cleanup); |
| 2503 | |
| 2504 | d->mod = dev_mod; |
| 2505 | /* insert into siblings */ |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 2506 | LY_LIST_INSERT(deviates, d, next); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2507 | |
| 2508 | return ret; |
| 2509 | |
| 2510 | cleanup: |
| 2511 | free(d); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2512 | return ret; |
| 2513 | } |
| 2514 | |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2515 | /** |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2516 | * @brief Parse deviation element. |
| 2517 | * |
| 2518 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2519 | * @param[in] deviations Deviations to add to. |
| 2520 | * |
| 2521 | * @return LY_ERR values. |
| 2522 | */ |
| 2523 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2524 | yin_parse_deviation(struct lys_yin_parser_ctx *ctx, struct lysp_deviation **deviations) |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2525 | { |
| 2526 | struct lysp_deviation *dev; |
| 2527 | |
| 2528 | /* create new deviation */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2529 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM); |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2530 | |
| 2531 | /* parse argument */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2532 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 2533 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION)); |
| 2534 | CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation"); |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2535 | struct yin_subelement subelems[4] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2536 | {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE}, |
| 2537 | {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY}, |
| 2538 | {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE}, |
| 2539 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2540 | }; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2541 | return yin_parse_content(ctx, subelems, 4, LY_STMT_DEVIATION, NULL, &dev->exts); |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | /** |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2545 | * @brief Map keyword type to substatement info. |
| 2546 | * |
| 2547 | * @param[in] kw Keyword type. |
| 2548 | * |
| 2549 | * @return correct LYEXT_SUBSTMT information. |
| 2550 | */ |
| 2551 | static LYEXT_SUBSTMT |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2552 | kw2lyext_substmt(enum ly_stmt kw) |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2553 | { |
| 2554 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2555 | case LY_STMT_ARGUMENT: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2556 | return LYEXT_SUBSTMT_ARGUMENT; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2557 | case LY_STMT_BASE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2558 | return LYEXT_SUBSTMT_BASE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2559 | case LY_STMT_BELONGS_TO: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2560 | return LYEXT_SUBSTMT_BELONGSTO; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2561 | case LY_STMT_CONTACT: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2562 | return LYEXT_SUBSTMT_CONTACT; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2563 | case LY_STMT_DEFAULT: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2564 | return LYEXT_SUBSTMT_DEFAULT; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2565 | case LY_STMT_DESCRIPTION: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2566 | return LYEXT_SUBSTMT_DESCRIPTION; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2567 | case LY_STMT_ERROR_APP_TAG: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2568 | return LYEXT_SUBSTMT_ERRTAG; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2569 | case LY_STMT_ERROR_MESSAGE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2570 | return LYEXT_SUBSTMT_ERRMSG; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2571 | case LY_STMT_KEY: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2572 | return LYEXT_SUBSTMT_KEY; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2573 | case LY_STMT_NAMESPACE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2574 | return LYEXT_SUBSTMT_NAMESPACE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2575 | case LY_STMT_ORGANIZATION: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2576 | return LYEXT_SUBSTMT_ORGANIZATION; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2577 | case LY_STMT_PATH: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2578 | return LYEXT_SUBSTMT_PATH; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2579 | case LY_STMT_PREFIX: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2580 | return LYEXT_SUBSTMT_PREFIX; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2581 | case LY_STMT_PRESENCE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2582 | return LYEXT_SUBSTMT_PRESENCE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2583 | case LY_STMT_REFERENCE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2584 | return LYEXT_SUBSTMT_REFERENCE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2585 | case LY_STMT_REVISION_DATE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2586 | return LYEXT_SUBSTMT_REVISIONDATE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2587 | case LY_STMT_UNITS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2588 | return LYEXT_SUBSTMT_UNITS; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2589 | case LY_STMT_VALUE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2590 | return LYEXT_SUBSTMT_VALUE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2591 | case LY_STMT_YANG_VERSION: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2592 | return LYEXT_SUBSTMT_VERSION; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2593 | case LY_STMT_MODIFIER: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2594 | return LYEXT_SUBSTMT_MODIFIER; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2595 | case LY_STMT_REQUIRE_INSTANCE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2596 | return LYEXT_SUBSTMT_REQINSTANCE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2597 | case LY_STMT_YIN_ELEMENT: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2598 | return LYEXT_SUBSTMT_YINELEM; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2599 | case LY_STMT_CONFIG: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2600 | return LYEXT_SUBSTMT_CONFIG; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2601 | case LY_STMT_MANDATORY: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2602 | return LYEXT_SUBSTMT_MANDATORY; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2603 | case LY_STMT_ORDERED_BY: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2604 | return LYEXT_SUBSTMT_ORDEREDBY; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2605 | case LY_STMT_STATUS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2606 | return LYEXT_SUBSTMT_STATUS; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2607 | case LY_STMT_FRACTION_DIGITS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2608 | return LYEXT_SUBSTMT_FRACDIGITS; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2609 | case LY_STMT_MAX_ELEMENTS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2610 | return LYEXT_SUBSTMT_MAX; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2611 | case LY_STMT_MIN_ELEMENTS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2612 | return LYEXT_SUBSTMT_MIN; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2613 | case LY_STMT_POSITION: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2614 | return LYEXT_SUBSTMT_POSITION; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2615 | case LY_STMT_UNIQUE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2616 | return LYEXT_SUBSTMT_UNIQUE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2617 | case LY_STMT_IF_FEATURE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2618 | return LYEXT_SUBSTMT_IFFEATURE; |
| 2619 | default: |
| 2620 | return LYEXT_SUBSTMT_SELF; |
| 2621 | } |
| 2622 | } |
| 2623 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2624 | /** |
| 2625 | * @brief map keyword to keyword-group. |
| 2626 | * |
| 2627 | * @param[in] ctx YIN parser context used for logging. |
| 2628 | * @param[in] kw Keyword that is child of module or submodule. |
| 2629 | * @param[out] group Group of keyword. |
| 2630 | * |
| 2631 | * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly. |
| 2632 | */ |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2633 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2634 | kw2kw_group(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group) |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2635 | { |
| 2636 | switch (kw) { |
| 2637 | /* module header */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2638 | case LY_STMT_NONE: |
| 2639 | case LY_STMT_NAMESPACE: |
| 2640 | case LY_STMT_PREFIX: |
| 2641 | case LY_STMT_BELONGS_TO: |
| 2642 | case LY_STMT_YANG_VERSION: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2643 | *group = Y_MOD_MODULE_HEADER; |
| 2644 | break; |
| 2645 | /* linkage */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2646 | case LY_STMT_INCLUDE: |
| 2647 | case LY_STMT_IMPORT: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2648 | *group = Y_MOD_LINKAGE; |
| 2649 | break; |
| 2650 | /* meta */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2651 | case LY_STMT_ORGANIZATION: |
| 2652 | case LY_STMT_CONTACT: |
| 2653 | case LY_STMT_DESCRIPTION: |
| 2654 | case LY_STMT_REFERENCE: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2655 | *group = Y_MOD_META; |
| 2656 | break; |
| 2657 | /* revision */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2658 | case LY_STMT_REVISION: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2659 | *group = Y_MOD_REVISION; |
| 2660 | break; |
| 2661 | /* body */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2662 | case LY_STMT_ANYDATA: |
| 2663 | case LY_STMT_ANYXML: |
| 2664 | case LY_STMT_AUGMENT: |
| 2665 | case LY_STMT_CHOICE: |
| 2666 | case LY_STMT_CONTAINER: |
| 2667 | case LY_STMT_DEVIATION: |
| 2668 | case LY_STMT_EXTENSION: |
| 2669 | case LY_STMT_FEATURE: |
| 2670 | case LY_STMT_GROUPING: |
| 2671 | case LY_STMT_IDENTITY: |
| 2672 | case LY_STMT_LEAF: |
| 2673 | case LY_STMT_LEAF_LIST: |
| 2674 | case LY_STMT_LIST: |
| 2675 | case LY_STMT_NOTIFICATION: |
| 2676 | case LY_STMT_RPC: |
| 2677 | case LY_STMT_TYPEDEF: |
| 2678 | case LY_STMT_USES: |
| 2679 | case LY_STMT_EXTENSION_INSTANCE: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2680 | *group = Y_MOD_BODY; |
| 2681 | break; |
| 2682 | default: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2683 | LOGINT(ctx->xmlctx->ctx); |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2684 | return LY_EINT; |
| 2685 | } |
| 2686 | |
| 2687 | return LY_SUCCESS; |
| 2688 | } |
| 2689 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2690 | /** |
| 2691 | * @brief Check if relative order of two keywords is valid. |
| 2692 | * |
| 2693 | * @param[in] ctx YIN parser context used for logging. |
| 2694 | * @param[in] kw Current keyword. |
| 2695 | * @param[in] next_kw Next keyword. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2696 | * @param[in] parrent Identification of parrent element, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE, |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2697 | * because relative order is required only in module and submodule sub-elements, used for logging. |
| 2698 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2699 | * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid. |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2700 | */ |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2701 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2702 | yin_check_relative_order(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum ly_stmt next_kw, enum ly_stmt parrent) |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2703 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2704 | assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE); |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2705 | enum yang_module_stmt gr, next_gr; |
| 2706 | |
| 2707 | LY_CHECK_RET(kw2kw_group(ctx, kw, &gr)); |
| 2708 | LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr)); |
| 2709 | |
| 2710 | if (gr > next_gr) { |
| 2711 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw)); |
| 2712 | return LY_EVALID; |
| 2713 | } |
| 2714 | |
| 2715 | return LY_SUCCESS; |
| 2716 | } |
| 2717 | |
David Sedlák | c5dd094 | 2019-09-13 10:23:59 +0200 | [diff] [blame] | 2718 | /** |
| 2719 | * @brief Get element name prefixed by full URI of xml namespace. |
| 2720 | * |
| 2721 | * @param[in] ctx YIN parser context used for logging and to get inormation about xml namespaces. |
| 2722 | * @param[in] name Name of element. |
| 2723 | * @param[in] name_len Length of element name. |
| 2724 | * @param[in] prefix Prefix of element. |
| 2725 | * @param[in] prefix_len Length of element prefix. |
| 2726 | * |
| 2727 | * @return Element name prefixed by URI on success, NULL on failure. |
| 2728 | */ |
| 2729 | static const char * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2730 | name2nsname(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len) |
David Sedlák | 068dd35 | 2019-09-13 09:08:22 +0200 | [diff] [blame] | 2731 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2732 | const struct lyxml_ns *ns = lyxml_ns_get(ctx->xmlctx, prefix, prefix_len); |
| 2733 | LY_CHECK_ERR_RET(!ns, LOGINT(ctx->xmlctx->ctx), NULL); |
David Sedlák | 068dd35 | 2019-09-13 09:08:22 +0200 | [diff] [blame] | 2734 | |
Radek Krejci | 0272856 | 2019-09-18 14:13:05 +0200 | [diff] [blame] | 2735 | if (!strcmp(ns->uri, YIN_NS_URI)) { |
| 2736 | /* standard YANG statement in YIN namespace - keep it unprefixed as in case of YANG */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2737 | return lydict_insert(ctx->xmlctx->ctx, name, name_len); |
Radek Krejci | 0272856 | 2019-09-18 14:13:05 +0200 | [diff] [blame] | 2738 | } |
| 2739 | /* some statement in special namespace (extension instance) */ |
David Sedlák | e0ef1c6 | 2019-09-13 10:05:55 +0200 | [diff] [blame] | 2740 | size_t ns_len = strlen(ns->uri); |
| 2741 | size_t len = ns_len + name_len + 1; /* +1 because of ':' delimiter between ns and actual name */ |
| 2742 | |
| 2743 | char *result; |
| 2744 | char *temp; |
| 2745 | temp = result = malloc(sizeof(*temp) * (len + 1)); /* +1 for '\0' terminator */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2746 | LY_CHECK_ERR_RET(!temp, LOGMEM(ctx->xmlctx->ctx), NULL); |
David Sedlák | e0ef1c6 | 2019-09-13 10:05:55 +0200 | [diff] [blame] | 2747 | |
| 2748 | strcpy(result, ns->uri); |
| 2749 | result[ns_len] = ':'; |
| 2750 | temp = &result[ns_len + 1]; |
| 2751 | strncpy(temp, name, name_len); |
| 2752 | result[len] = '\0'; |
| 2753 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2754 | return lydict_insert_zc(ctx->xmlctx->ctx, result); |
David Sedlák | 068dd35 | 2019-09-13 09:08:22 +0200 | [diff] [blame] | 2755 | } |
| 2756 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2757 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2758 | yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size, |
| 2759 | enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts) |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2760 | { |
| 2761 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2762 | enum LYXML_PARSER_STATUS next_status; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2763 | enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE; |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2764 | struct yin_subelement *subelem = NULL; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 2765 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2766 | assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2767 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2768 | if (ctx->xmlctx->ws_only) { |
| 2769 | /* check whether there are any children */ |
| 2770 | LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup); |
| 2771 | } else { |
| 2772 | /* we want to parse the value */ |
| 2773 | next_status = LYXML_ELEM_CLOSE; |
| 2774 | } |
| 2775 | |
| 2776 | if (next_status == LYXML_ELEMENT) { |
| 2777 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
| 2778 | |
| 2779 | /* current element has subelements as content */ |
| 2780 | while (ctx->xmlctx->status == LYXML_ELEMENT) { |
| 2781 | /* match keyword */ |
| 2782 | last_kw = kw; |
| 2783 | kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, |
| 2784 | ctx->xmlctx->prefix_len, current_element); |
| 2785 | |
| 2786 | /* check if this element can be child of current element */ |
| 2787 | subelem = get_record(kw, subelem_info_size, subelem_info); |
| 2788 | if (!subelem) { |
| 2789 | if (current_element == LY_STMT_DEVIATE && isdevsub(kw)) { |
| 2790 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw)); |
| 2791 | } else { |
| 2792 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, |
| 2793 | ctx->xmlctx->name, ly_stmt2str(current_element)); |
| 2794 | } |
| 2795 | ret = LY_EVALID; |
| 2796 | goto cleanup; |
| 2797 | } |
| 2798 | |
| 2799 | /* relative order is required only in module and submodule sub-elements */ |
| 2800 | if (current_element == LY_STMT_MODULE || current_element == LY_STMT_SUBMODULE) { |
| 2801 | ret = yin_check_relative_order(ctx, last_kw, kw, current_element); |
| 2802 | LY_CHECK_GOTO(ret, cleanup); |
| 2803 | } |
| 2804 | |
| 2805 | /* flag check */ |
| 2806 | if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) { |
| 2807 | /* subelement uniquenes */ |
| 2808 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element)); |
| 2809 | return LY_EVALID; |
| 2810 | } |
| 2811 | if (subelem->flags & YIN_SUBELEM_FIRST) { |
| 2812 | /* subelement is supposed to be defined as first subelement */ |
| 2813 | ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem); |
| 2814 | LY_CHECK_GOTO(ret, cleanup); |
| 2815 | } |
| 2816 | if (subelem->flags & YIN_SUBELEM_VER2) { |
| 2817 | /* subelement is supported only in version 1.1 or higher */ |
| 2818 | if (ctx->mod_version < 2) { |
| 2819 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element)); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2820 | ret = LY_EVALID; |
| 2821 | goto cleanup; |
| 2822 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2823 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2824 | /* note that element was parsed for easy uniqueness check in next iterations */ |
| 2825 | subelem->flags |= YIN_SUBELEM_PARSED; |
| 2826 | |
| 2827 | switch (kw) { |
| 2828 | /* call responsible function */ |
| 2829 | case LY_STMT_EXTENSION_INSTANCE: |
| 2830 | ret = yin_parse_extension_instance(ctx, kw2lyext_substmt(current_element), |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2831 | (subelem->dest) ? *((LY_ARRAY_SIZE_TYPE*)subelem->dest) : 0, exts); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2832 | break; |
| 2833 | case LY_STMT_ACTION: |
| 2834 | case LY_STMT_RPC: |
| 2835 | ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest); |
| 2836 | break; |
| 2837 | case LY_STMT_ANYDATA: |
| 2838 | case LY_STMT_ANYXML: |
| 2839 | ret = yin_parse_any(ctx, kw, (struct tree_node_meta *)subelem->dest); |
| 2840 | break; |
| 2841 | case LY_STMT_ARGUMENT: |
| 2842 | ret = yin_parse_argument(ctx, (struct yin_argument_meta *)subelem->dest, exts); |
| 2843 | break; |
| 2844 | case LY_STMT_AUGMENT: |
| 2845 | ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest); |
| 2846 | break; |
| 2847 | case LY_STMT_BASE: |
| 2848 | ret = yin_parse_base(ctx, current_element, subelem->dest, exts); |
| 2849 | break; |
| 2850 | case LY_STMT_BELONGS_TO: |
| 2851 | ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts); |
| 2852 | break; |
| 2853 | case LY_STMT_BIT: |
| 2854 | ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest); |
| 2855 | break; |
| 2856 | case LY_STMT_CASE: |
| 2857 | ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest); |
| 2858 | break; |
| 2859 | case LY_STMT_CHOICE: |
| 2860 | ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest); |
| 2861 | break; |
| 2862 | case LY_STMT_CONFIG: |
| 2863 | ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts); |
| 2864 | break; |
| 2865 | case LY_STMT_CONTACT: |
| 2866 | case LY_STMT_DESCRIPTION: |
| 2867 | case LY_STMT_ORGANIZATION: |
| 2868 | case LY_STMT_REFERENCE: |
| 2869 | ret = yin_parse_meta(ctx, kw, (const char **)subelem->dest, exts); |
| 2870 | break; |
| 2871 | case LY_STMT_CONTAINER: |
| 2872 | ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest); |
| 2873 | break; |
| 2874 | case LY_STMT_DEFAULT: |
| 2875 | case LY_STMT_ERROR_APP_TAG: |
| 2876 | case LY_STMT_KEY: |
| 2877 | case LY_STMT_PRESENCE: |
| 2878 | ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts); |
| 2879 | break; |
| 2880 | case LY_STMT_DEVIATE: |
| 2881 | ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest); |
| 2882 | break; |
| 2883 | case LY_STMT_DEVIATION: |
| 2884 | ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest); |
| 2885 | break; |
| 2886 | case LY_STMT_ENUM: |
| 2887 | ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest); |
| 2888 | break; |
| 2889 | case LY_STMT_ERROR_MESSAGE: |
| 2890 | ret = yin_parse_err_msg(ctx, (const char **)subelem->dest, exts); |
| 2891 | break; |
| 2892 | case LY_STMT_EXTENSION: |
| 2893 | ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest); |
| 2894 | break; |
| 2895 | case LY_STMT_FEATURE: |
| 2896 | ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest); |
| 2897 | break; |
| 2898 | case LY_STMT_FRACTION_DIGITS: |
| 2899 | ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest); |
| 2900 | break; |
| 2901 | case LY_STMT_GROUPING: |
| 2902 | ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest); |
| 2903 | break; |
| 2904 | case LY_STMT_IDENTITY: |
| 2905 | ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest); |
| 2906 | break; |
| 2907 | case LY_STMT_IF_FEATURE: |
| 2908 | case LY_STMT_UNITS: |
| 2909 | ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts); |
| 2910 | break; |
| 2911 | case LY_STMT_IMPORT: |
| 2912 | ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest); |
| 2913 | break; |
| 2914 | case LY_STMT_INCLUDE: |
| 2915 | ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest); |
| 2916 | break; |
| 2917 | case LY_STMT_INPUT: |
| 2918 | case LY_STMT_OUTPUT: |
| 2919 | ret = yin_parse_inout(ctx, kw, (struct inout_meta *)subelem->dest); |
| 2920 | break; |
| 2921 | case LY_STMT_LEAF: |
| 2922 | ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest); |
| 2923 | break; |
| 2924 | case LY_STMT_LEAF_LIST: |
| 2925 | ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest); |
| 2926 | break; |
| 2927 | case LY_STMT_LENGTH: |
| 2928 | ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest); |
| 2929 | break; |
| 2930 | case LY_STMT_LIST: |
| 2931 | ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest); |
| 2932 | break; |
| 2933 | case LY_STMT_MANDATORY: |
| 2934 | ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts); |
| 2935 | break; |
| 2936 | case LY_STMT_MAX_ELEMENTS: |
| 2937 | case LY_STMT_MIN_ELEMENTS: |
| 2938 | ret = yin_parse_minmax(ctx, current_element, kw, subelem->dest); |
| 2939 | break; |
| 2940 | case LY_STMT_MODIFIER: |
| 2941 | ret = yin_parse_modifier(ctx, (const char **)subelem->dest, exts); |
| 2942 | break; |
| 2943 | case LY_STMT_MUST: |
| 2944 | ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest); |
| 2945 | break; |
| 2946 | case LY_STMT_NAMESPACE: |
| 2947 | ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts); |
| 2948 | break; |
| 2949 | case LY_STMT_NOTIFICATION: |
| 2950 | ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest); |
| 2951 | break; |
| 2952 | case LY_STMT_ORDERED_BY: |
| 2953 | ret = yin_parse_orderedby(ctx, (uint16_t *)subelem->dest, exts); |
| 2954 | break; |
| 2955 | case LY_STMT_PATH: |
| 2956 | ret = yin_parse_path(ctx, kw, (struct lysp_type *)subelem->dest); |
| 2957 | break; |
| 2958 | case LY_STMT_PATTERN: |
| 2959 | ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest); |
| 2960 | break; |
| 2961 | case LY_STMT_VALUE: |
| 2962 | case LY_STMT_POSITION: |
| 2963 | ret = yin_parse_value_pos(ctx, kw, (struct lysp_type_enum *)subelem->dest); |
| 2964 | break; |
| 2965 | case LY_STMT_PREFIX: |
| 2966 | ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts); |
| 2967 | break; |
| 2968 | case LY_STMT_RANGE: |
| 2969 | ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest); |
| 2970 | break; |
| 2971 | case LY_STMT_REFINE: |
| 2972 | ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest); |
| 2973 | break; |
| 2974 | case LY_STMT_REQUIRE_INSTANCE: |
| 2975 | ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest); |
| 2976 | break; |
| 2977 | case LY_STMT_REVISION: |
| 2978 | ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest); |
| 2979 | break; |
| 2980 | case LY_STMT_REVISION_DATE: |
| 2981 | ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts); |
| 2982 | break; |
| 2983 | case LY_STMT_STATUS: |
| 2984 | ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts); |
| 2985 | break; |
| 2986 | case LY_STMT_TYPE: |
| 2987 | ret = yin_parse_type(ctx, current_element, subelem); |
| 2988 | break; |
| 2989 | case LY_STMT_TYPEDEF: |
| 2990 | ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest); |
| 2991 | break; |
| 2992 | case LY_STMT_UNIQUE: |
| 2993 | ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts); |
| 2994 | break; |
| 2995 | case LY_STMT_USES: |
| 2996 | ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest); |
| 2997 | break; |
| 2998 | case LY_STMT_WHEN: |
| 2999 | ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest); |
| 3000 | break; |
| 3001 | case LY_STMT_YANG_VERSION: |
| 3002 | ret = yin_parse_yangversion(ctx, (uint8_t *)subelem->dest, exts); |
| 3003 | break; |
| 3004 | case LY_STMT_YIN_ELEMENT: |
| 3005 | ret = yin_parse_yin_element(ctx, (uint16_t *)subelem->dest, exts); |
| 3006 | break; |
| 3007 | case LY_STMT_ARG_TEXT: |
| 3008 | case LY_STMT_ARG_VALUE: |
| 3009 | /* TODO what to do with content/attributes? */ |
| 3010 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
| 3011 | while (ctx->xmlctx->status == LYXML_ATTRIBUTE) { |
| 3012 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
| 3013 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
| 3014 | } |
| 3015 | ret = yin_parse_content(ctx, NULL, 0, kw, (const char **)subelem->dest, NULL); |
| 3016 | break; |
| 3017 | default: |
| 3018 | LOGINT(ctx->xmlctx->ctx); |
| 3019 | ret = LY_EINT; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 3020 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3021 | LY_CHECK_GOTO(ret, cleanup); |
| 3022 | subelem = NULL; |
| 3023 | |
| 3024 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3025 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3026 | } else { |
| 3027 | LY_CHECK_RET(ret); |
| 3028 | /* elements with text or none content */ |
| 3029 | /* save text content, if text_content isn't set, it's just ignored */ |
| 3030 | /* no resources are allocated in this branch, no need to use cleanup label */ |
| 3031 | LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG)); |
| 3032 | if (text_content) { |
| 3033 | *text_content = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic); |
| 3034 | LY_CHECK_RET(!*text_content, LY_EMEM); |
| 3035 | } |
| 3036 | |
| 3037 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3038 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3039 | |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 3040 | /* mandatory subelemnts are checked only after whole element was succesfully parsed */ |
| 3041 | LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element)); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3042 | |
| 3043 | cleanup: |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3044 | return ret; |
| 3045 | } |
| 3046 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 3047 | LY_ERR |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3048 | yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_SIZE_TYPE subelem_index, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3049 | struct lysp_ext_instance **exts) |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3050 | { |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3051 | struct lysp_ext_instance *e; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3052 | struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3053 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3054 | assert(ctx->xmlctx->status == LYXML_ELEMENT); |
| 3055 | |
| 3056 | LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3057 | |
| 3058 | e->yin = 0; |
| 3059 | /* store name and insubstmt info */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3060 | e->name = name2nsname(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len); |
David Sedlák | e0ef1c6 | 2019-09-13 10:05:55 +0200 | [diff] [blame] | 3061 | LY_CHECK_RET(!e->name, LY_EMEM); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 3062 | e->insubstmt = subelem; |
| 3063 | e->insubstmt_index = subelem_index; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3064 | e->yin |= LYS_YIN; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3065 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3066 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3067 | /* store attributes as subelements */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3068 | while (ctx->xmlctx->status == LYXML_ATTRIBUTE) { |
| 3069 | if (!ctx->xmlctx->prefix) { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3070 | new_subelem = calloc(1, sizeof(*new_subelem)); |
| 3071 | if (!e->child) { |
| 3072 | e->child = new_subelem; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3073 | } else { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3074 | last_subelem->next = new_subelem; |
| 3075 | } |
| 3076 | last_subelem = new_subelem; |
| 3077 | |
| 3078 | last_subelem->flags |= LYS_YIN_ATTR; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3079 | last_subelem->stmt = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len); |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3080 | LY_CHECK_RET(!last_subelem->stmt, LY_EMEM); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3081 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3082 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3083 | last_subelem->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic); |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3084 | LY_CHECK_RET(!last_subelem->arg, LY_EMEM); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3085 | } else { |
| 3086 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3087 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3088 | |
| 3089 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3090 | } |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3091 | |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 3092 | /* parse subelements */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3093 | assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT); |
| 3094 | if (ctx->xmlctx->ws_only) { |
| 3095 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3096 | while (ctx->xmlctx->status == LYXML_ELEMENT) { |
| 3097 | LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem)); |
| 3098 | if (!e->child) { |
| 3099 | e->child = new_subelem; |
| 3100 | } else { |
| 3101 | last_subelem->next = new_subelem; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3102 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3103 | last_subelem = new_subelem; |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3104 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3105 | assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE); |
| 3106 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3107 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3108 | } else if (ctx->xmlctx->value_len) { |
| 3109 | /* save text content */ |
| 3110 | e->argument = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic); |
| 3111 | LY_CHECK_RET(!e->argument, LY_EMEM); |
| 3112 | |
| 3113 | /* parser next */ |
| 3114 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | return LY_SUCCESS; |
| 3118 | } |
| 3119 | |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3120 | /** |
| 3121 | * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension. |
| 3122 | * |
| 3123 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3124 | * @param[in] elem_type Type of element that is currently being parsed. |
| 3125 | * @param[out] arg Value to write to. |
| 3126 | * |
| 3127 | * @return LY_ERR values. |
| 3128 | */ |
| 3129 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3130 | yin_parse_extension_instance_arg(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **arg) |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3131 | { |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3132 | enum ly_stmt child; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3133 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3134 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3135 | |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3136 | switch (elem_type) { |
| 3137 | case LY_STMT_ACTION: |
| 3138 | case LY_STMT_ANYDATA: |
| 3139 | case LY_STMT_ANYXML: |
| 3140 | case LY_STMT_ARGUMENT: |
| 3141 | case LY_STMT_BASE: |
| 3142 | case LY_STMT_BIT: |
| 3143 | case LY_STMT_CASE: |
| 3144 | case LY_STMT_CHOICE: |
| 3145 | case LY_STMT_CONTAINER: |
| 3146 | case LY_STMT_ENUM: |
| 3147 | case LY_STMT_EXTENSION: |
| 3148 | case LY_STMT_FEATURE: |
| 3149 | case LY_STMT_GROUPING: |
| 3150 | case LY_STMT_IDENTITY: |
| 3151 | case LY_STMT_IF_FEATURE: |
| 3152 | case LY_STMT_LEAF: |
| 3153 | case LY_STMT_LEAF_LIST: |
| 3154 | case LY_STMT_LIST: |
| 3155 | case LY_STMT_MODULE: |
| 3156 | case LY_STMT_NOTIFICATION: |
| 3157 | case LY_STMT_RPC: |
| 3158 | case LY_STMT_SUBMODULE: |
| 3159 | case LY_STMT_TYPE: |
| 3160 | case LY_STMT_TYPEDEF: |
| 3161 | case LY_STMT_UNITS: |
| 3162 | case LY_STMT_USES: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3163 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3164 | break; |
| 3165 | case LY_STMT_AUGMENT: |
| 3166 | case LY_STMT_DEVIATION: |
| 3167 | case LY_STMT_REFINE: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3168 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3169 | break; |
| 3170 | case LY_STMT_CONFIG: |
| 3171 | case LY_STMT_DEFAULT: |
| 3172 | case LY_STMT_DEVIATE: |
| 3173 | case LY_STMT_ERROR_APP_TAG: |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3174 | case LY_STMT_FRACTION_DIGITS: |
| 3175 | case LY_STMT_KEY: |
| 3176 | case LY_STMT_LENGTH: |
| 3177 | case LY_STMT_MANDATORY: |
| 3178 | case LY_STMT_MAX_ELEMENTS: |
| 3179 | case LY_STMT_MIN_ELEMENTS: |
| 3180 | case LY_STMT_MODIFIER: |
| 3181 | case LY_STMT_ORDERED_BY: |
| 3182 | case LY_STMT_PATH: |
| 3183 | case LY_STMT_PATTERN: |
| 3184 | case LY_STMT_POSITION: |
| 3185 | case LY_STMT_PREFIX: |
| 3186 | case LY_STMT_PRESENCE: |
| 3187 | case LY_STMT_RANGE: |
| 3188 | case LY_STMT_REQUIRE_INSTANCE: |
| 3189 | case LY_STMT_STATUS: |
| 3190 | case LY_STMT_VALUE: |
| 3191 | case LY_STMT_YANG_VERSION: |
| 3192 | case LY_STMT_YIN_ELEMENT: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3193 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3194 | break; |
| 3195 | case LY_STMT_IMPORT: |
| 3196 | case LY_STMT_INCLUDE: |
| 3197 | case LY_STMT_BELONGS_TO: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3198 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3199 | break; |
| 3200 | case LY_STMT_INPUT: |
| 3201 | case LY_STMT_OUTPUT: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3202 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3203 | break; |
| 3204 | case LY_STMT_MUST: |
| 3205 | case LY_STMT_WHEN: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3206 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3207 | break; |
| 3208 | case LY_STMT_NAMESPACE: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3209 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3210 | break; |
| 3211 | case LY_STMT_REVISION: |
| 3212 | case LY_STMT_REVISION_DATE: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3213 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3214 | break; |
| 3215 | case LY_STMT_UNIQUE: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3216 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3217 | break; |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3218 | /* argument is mapped to yin element */ |
| 3219 | case LY_STMT_CONTACT: |
| 3220 | case LY_STMT_DESCRIPTION: |
| 3221 | case LY_STMT_ORGANIZATION: |
| 3222 | case LY_STMT_REFERENCE: |
| 3223 | case LY_STMT_ERROR_MESSAGE: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3224 | /* there shouldn't be any attribute, argument is supposed to be first subelement */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3225 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type)); |
| 3226 | |
| 3227 | /* no content */ |
| 3228 | assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT); |
| 3229 | if (ctx->xmlctx->ws_only) { |
| 3230 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3231 | } |
| 3232 | if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) { |
| 3233 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM, |
| 3234 | elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type)); |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3235 | return LY_EVALID; |
| 3236 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3237 | |
| 3238 | /* parse child element */ |
| 3239 | child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len, elem_type); |
| 3240 | if ((elem_type == LY_STMT_ERROR_MESSAGE && child != LY_STMT_ARG_VALUE) || |
| 3241 | (elem_type != LY_STMT_ERROR_MESSAGE && child != LY_STMT_ARG_TEXT)) { |
| 3242 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name, |
| 3243 | ly_stmt2str(elem_type)); |
| 3244 | return LY_EVALID; |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3245 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3246 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3247 | |
| 3248 | /* no attributes expected? TODO */ |
| 3249 | while (ctx->xmlctx->status == LYXML_ATTRIBUTE) { |
| 3250 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3251 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3252 | } |
| 3253 | |
| 3254 | /* load and save content */ |
| 3255 | *arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic); |
| 3256 | LY_CHECK_RET(!*arg, LY_EMEM); |
| 3257 | |
| 3258 | /* load closing tag of subelement */ |
| 3259 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3260 | |
| 3261 | /* if only subelement was parsed as argument, load also closing tag TODO what? */ |
| 3262 | /*if (ctx->xmlctx->status == LYXML_ELEMENT) { |
| 3263 | LY_CHECK_RET(lyxml_get_element(&ctx->xmlctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 3264 | }*/ |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3265 | break; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3266 | default: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3267 | LOGINT(ctx->xmlctx->ctx); |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3268 | return LY_EINT; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3271 | return LY_SUCCESS; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3275 | yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element) |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3276 | { |
| 3277 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3278 | struct lysp_stmt *last = NULL, *new = NULL; |
| 3279 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3280 | assert(ctx->xmlctx->status == LYXML_ELEMENT); |
David Sedlák | e0ef1c6 | 2019-09-13 10:05:55 +0200 | [diff] [blame] | 3281 | |
| 3282 | /* allocate new structure for element */ |
| 3283 | *element = calloc(1, sizeof(**element)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3284 | LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup); |
| 3285 | (*element)->stmt = name2nsname(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len); |
David Sedlák | e0ef1c6 | 2019-09-13 10:05:55 +0200 | [diff] [blame] | 3286 | LY_CHECK_ERR_GOTO(!(*element)->stmt, ret = LY_EMEM, cleanup); |
David Sedlák | e0ef1c6 | 2019-09-13 10:05:55 +0200 | [diff] [blame] | 3287 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3288 | (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, |
| 3289 | ctx->xmlctx->prefix_len, parent); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3290 | |
| 3291 | last = (*element)->child; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3292 | if ((*element)->kw == LY_STMT_NONE) { |
| 3293 | /* unrecognized element */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3294 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name, |
| 3295 | ly_stmt2str(parent)); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3296 | ret = LY_EVALID; |
| 3297 | goto cleanup; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3298 | } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) { |
| 3299 | /* element is known yang keyword, which means argument can be parsed correctly. */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3300 | ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3301 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3302 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3303 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
| 3304 | |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3305 | /* load attributes in generic way, save all attributes in linked list */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3306 | while (ctx->xmlctx->status == LYXML_ATTRIBUTE) { |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3307 | new = calloc(1, sizeof(*last)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3308 | LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3309 | if (!(*element)->child) { |
| 3310 | /* save first */ |
| 3311 | (*element)->child = new; |
| 3312 | } else { |
| 3313 | last->next = new; |
| 3314 | } |
| 3315 | last = new; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3316 | |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3317 | last->flags |= LYS_YIN_ATTR; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3318 | last->stmt = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len); |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3319 | last->kw = LY_STMT_NONE; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3320 | /* attributes with prefix are ignored */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3321 | if (!ctx->xmlctx->prefix) { |
| 3322 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
| 3323 | |
| 3324 | last->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3325 | LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3326 | } else { |
| 3327 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3328 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3329 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3330 | } |
| 3331 | } |
| 3332 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3333 | if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) { |
| 3334 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
| 3335 | while (ctx->xmlctx->status == LYXML_ELEMENT) { |
| 3336 | /* parse subelements */ |
| 3337 | ret = yin_parse_element_generic(ctx, (*element)->kw, &new); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3338 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3339 | if (!(*element)->child) { |
| 3340 | /* save first */ |
| 3341 | (*element)->child = new; |
| 3342 | } else { |
| 3343 | last->next = new; |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3344 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3345 | last = new; |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3346 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3347 | assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE); |
| 3348 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3349 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3350 | } else { |
| 3351 | /* save element content */ |
| 3352 | if (ctx->xmlctx->value_len) { |
| 3353 | (*element)->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic); |
| 3354 | LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup); |
| 3355 | } |
| 3356 | |
| 3357 | /* read closing tag */ |
| 3358 | LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3359 | } |
| 3360 | |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3361 | cleanup: |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3362 | return ret; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3366 | yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3367 | { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3368 | LY_ERR ret = LY_SUCCESS; |
| 3369 | struct yin_subelement *subelems = NULL; |
David Sedlák | ed809f1 | 2019-09-13 10:15:11 +0200 | [diff] [blame] | 3370 | struct lysp_submodule *dup; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3371 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3372 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3373 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, LY_STMT_MODULE)); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3374 | LY_CHECK_RET(subelems_allocator(ctx, 28, NULL, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3375 | LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2, |
| 3376 | LY_STMT_ANYXML, &mod->data, 0, |
| 3377 | LY_STMT_AUGMENT, &mod->augments, 0, |
| 3378 | LY_STMT_CHOICE, &mod->data, 0, |
| 3379 | LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE, |
| 3380 | LY_STMT_CONTAINER, &mod->data, 0, |
| 3381 | LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE, |
| 3382 | LY_STMT_DEVIATION, &mod->deviations, 0, |
| 3383 | LY_STMT_EXTENSION, &mod->extensions, 0, |
| 3384 | LY_STMT_FEATURE, &mod->features, 0, |
| 3385 | LY_STMT_GROUPING, &mod->groupings, 0, |
| 3386 | LY_STMT_IDENTITY, &mod->identities, 0, |
| 3387 | LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0, |
| 3388 | LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0, |
| 3389 | LY_STMT_LEAF, &mod->data, 0, |
| 3390 | LY_STMT_LEAF_LIST, &mod->data, 0, |
| 3391 | LY_STMT_LIST, &mod->data, 0, |
| 3392 | LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE, |
| 3393 | LY_STMT_NOTIFICATION, &mod->notifs, 0, |
| 3394 | LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE, |
| 3395 | LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE, |
| 3396 | LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE, |
| 3397 | LY_STMT_REVISION, &mod->revs, 0, |
| 3398 | LY_STMT_RPC, &mod->rpcs, 0, |
| 3399 | LY_STMT_TYPEDEF, &mod->typedefs, 0, |
| 3400 | LY_STMT_USES, &mod->data, 0, |
Radek Krejci | cd1c5e3 | 2019-09-06 16:28:42 +0200 | [diff] [blame] | 3401 | LY_STMT_YANG_VERSION, &mod->mod->version, YIN_SUBELEM_UNIQUE, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3402 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3403 | )); |
| 3404 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3405 | ret = yin_parse_content(ctx, subelems, 28, LY_STMT_MODULE, NULL, &mod->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3406 | subelems_deallocator(28, subelems); |
David Sedlák | ed809f1 | 2019-09-13 10:15:11 +0200 | [diff] [blame] | 3407 | LY_CHECK_RET(ret); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3408 | |
David Sedlák | ed809f1 | 2019-09-13 10:15:11 +0200 | [diff] [blame] | 3409 | /* finalize parent pointers to the reallocated items */ |
| 3410 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs)); |
| 3411 | |
| 3412 | /* submodules share the namespace with the module names, so there must not be |
| 3413 | * a submodule of the same name in the context, no need for revision matching */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3414 | dup = ly_ctx_get_submodule(ctx->xmlctx->ctx, NULL, mod->mod->name, NULL); |
David Sedlák | ed809f1 | 2019-09-13 10:15:11 +0200 | [diff] [blame] | 3415 | if (dup) { |
| 3416 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name); |
| 3417 | return LY_EVALID; |
| 3418 | } |
| 3419 | |
| 3420 | return LY_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3424 | yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod) |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 3425 | { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3426 | LY_ERR ret = LY_SUCCESS; |
| 3427 | struct yin_subelement *subelems = NULL; |
David Sedlák | ed809f1 | 2019-09-13 10:15:11 +0200 | [diff] [blame] | 3428 | struct lysp_submodule *dup; |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 3429 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3430 | LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx)); |
| 3431 | LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, LY_STMT_SUBMODULE)); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3432 | LY_CHECK_RET(subelems_allocator(ctx, 27, NULL, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3433 | LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2, |
| 3434 | LY_STMT_ANYXML, &submod->data, 0, |
| 3435 | LY_STMT_AUGMENT, &submod->augments, 0, |
| 3436 | LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE, |
| 3437 | LY_STMT_CHOICE, &submod->data, 0, |
| 3438 | LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE, |
| 3439 | LY_STMT_CONTAINER, &submod->data, 0, |
| 3440 | LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE, |
| 3441 | LY_STMT_DEVIATION, &submod->deviations, 0, |
| 3442 | LY_STMT_EXTENSION, &submod->extensions, 0, |
| 3443 | LY_STMT_FEATURE, &submod->features, 0, |
| 3444 | LY_STMT_GROUPING, &submod->groupings, 0, |
| 3445 | LY_STMT_IDENTITY, &submod->identities, 0, |
| 3446 | LY_STMT_IMPORT, submod->prefix, &submod->imports, 0, |
| 3447 | LY_STMT_INCLUDE, submod->name, &submod->includes, 0, |
| 3448 | LY_STMT_LEAF, &submod->data, 0, |
| 3449 | LY_STMT_LEAF_LIST, &submod->data, 0, |
| 3450 | LY_STMT_LIST, &submod->data, 0, |
| 3451 | LY_STMT_NOTIFICATION, &submod->notifs, 0, |
| 3452 | LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE, |
| 3453 | LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE, |
| 3454 | LY_STMT_REVISION, &submod->revs, 0, |
| 3455 | LY_STMT_RPC, &submod->rpcs, 0, |
| 3456 | LY_STMT_TYPEDEF, &submod->typedefs, 0, |
| 3457 | LY_STMT_USES, &submod->data, 0, |
Radek Krejci | cd1c5e3 | 2019-09-06 16:28:42 +0200 | [diff] [blame] | 3458 | LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3459 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3460 | )); |
| 3461 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3462 | ret = yin_parse_content(ctx, subelems, 27, LY_STMT_SUBMODULE, NULL, &submod->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3463 | subelems_deallocator(27, subelems); |
David Sedlák | ed809f1 | 2019-09-13 10:15:11 +0200 | [diff] [blame] | 3464 | LY_CHECK_RET(ret); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3465 | |
David Sedlák | ed809f1 | 2019-09-13 10:15:11 +0200 | [diff] [blame] | 3466 | /* finalize parent pointers to the reallocated items */ |
| 3467 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs)); |
| 3468 | |
| 3469 | /* submodules share the namespace with the module names, so there must not be |
| 3470 | * a submodule of the same name in the context, no need for revision matching */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3471 | dup = ly_ctx_get_submodule(ctx->xmlctx->ctx, NULL, submod->name, NULL); |
David Sedlák | ed809f1 | 2019-09-13 10:15:11 +0200 | [diff] [blame] | 3472 | if (dup && strcmp(dup->belongsto, submod->belongsto)) { |
| 3473 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name); |
| 3474 | return LY_EVALID; |
| 3475 | } |
| 3476 | |
| 3477 | return LY_SUCCESS; |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3481 | yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx, const char *data, struct lysp_submodule **submod) |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3482 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3483 | enum ly_stmt kw = LY_STMT_NONE; |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3484 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3485 | struct lysp_submodule *mod_p = NULL; |
| 3486 | |
| 3487 | /* create context */ |
| 3488 | *yin_ctx = calloc(1, sizeof **yin_ctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3489 | LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3490 | (*yin_ctx)->format = LYS_IN_YIN; |
| 3491 | LY_CHECK_RET(lyxml_ctx_new(ctx, data, &(*yin_ctx)->xmlctx)); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3492 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3493 | /* map the typedefs and groupings list from main context to the submodule's context */ |
| 3494 | memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 3495 | memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes); |
| 3496 | |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3497 | /* check submodule */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3498 | kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix, |
| 3499 | (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3500 | if (kw == LY_STMT_MODULE) { |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3501 | LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected."); |
| 3502 | ret = LY_EINVAL; |
| 3503 | goto cleanup; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3504 | } else if (kw != LY_STMT_SUBMODULE) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 3505 | LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw)); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3506 | ret = LY_EVALID; |
| 3507 | goto cleanup; |
| 3508 | } |
| 3509 | |
| 3510 | mod_p = calloc(1, sizeof *mod_p); |
| 3511 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 3512 | mod_p->parsing = 1; |
| 3513 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3514 | ret = yin_parse_submod(*yin_ctx, mod_p); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3515 | LY_CHECK_GOTO(ret, cleanup); |
| 3516 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3517 | /* skip possible trailing whitespaces at end of the input */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3518 | data = (*yin_ctx)->xmlctx->input; |
| 3519 | while (*data && isspace(*data)) { |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3520 | data++; |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3521 | } |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3522 | if (*data) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 3523 | LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : ""); |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3524 | ret = LY_EVALID; |
| 3525 | goto cleanup; |
| 3526 | } |
| 3527 | |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3528 | mod_p->parsing = 0; |
| 3529 | *submod = mod_p; |
| 3530 | |
| 3531 | cleanup: |
| 3532 | if (ret) { |
| 3533 | lysp_submodule_free(ctx, mod_p); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3534 | yin_parser_ctx_free(*yin_ctx); |
| 3535 | *yin_ctx = NULL; |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3536 | } |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3537 | return ret; |
| 3538 | } |
| 3539 | |
| 3540 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3541 | yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, const char *data, struct lys_module *mod) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3542 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 3543 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3544 | enum ly_stmt kw = LY_STMT_NONE; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3545 | struct lysp_module *mod_p = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3546 | |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3547 | /* create context */ |
| 3548 | *yin_ctx = calloc(1, sizeof **yin_ctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3549 | LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3550 | (*yin_ctx)->format = LYS_IN_YIN; |
| 3551 | LY_CHECK_RET(lyxml_ctx_new(mod->ctx, data, &(*yin_ctx)->xmlctx)); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 3552 | |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3553 | /* check module */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3554 | kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix, |
| 3555 | (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3556 | if (kw == LY_STMT_SUBMODULE) { |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3557 | LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3558 | ret = LY_EINVAL; |
| 3559 | goto cleanup; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3560 | } else if (kw != LY_STMT_MODULE) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 3561 | LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3562 | ret = LY_EVALID; |
| 3563 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 3564 | } |
| 3565 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3566 | /* allocate module */ |
| 3567 | mod_p = calloc(1, sizeof *mod_p); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3568 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3569 | mod_p->mod = mod; |
| 3570 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 3571 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 3572 | /* parse module substatements */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3573 | ret = yin_parse_mod(*yin_ctx, mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3574 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 3575 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3576 | /* skip possible trailing whitespaces at end of the input */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3577 | data = (*yin_ctx)->xmlctx->input; |
| 3578 | while (*data && isspace(*data)) { |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3579 | data++; |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3580 | } |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3581 | if (*data) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 3582 | LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : ""); |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3583 | ret = LY_EVALID; |
| 3584 | goto cleanup; |
| 3585 | } |
| 3586 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3587 | mod_p->parsing = 0; |
| 3588 | mod->parsed = mod_p; |
| 3589 | |
| 3590 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 3591 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3592 | lysp_module_free(mod_p); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3593 | yin_parser_ctx_free(*yin_ctx); |
| 3594 | *yin_ctx = NULL; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3595 | } |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 3596 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3597 | } |