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