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