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