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