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 | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 169 | if (record->dynamic_content) { |
| 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; |
| 532 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->enums, en, LY_EMEM); |
| 533 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, enum_kw)); |
| 534 | type->flags |= (enum_kw == YANG_ENUM) ? LYS_SET_ENUM : LYS_SET_BIT; |
| 535 | if (enum_kw == YANG_ENUM) { |
| 536 | 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] | 537 | 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] | 538 | } |
| 539 | CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, ly_stmt2str(enum_kw), en->name); |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 540 | |
| 541 | struct yin_subelement subelems[6] = { |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 542 | {YANG_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE}, |
| 543 | {YANG_IF_FEATURE, &en->iffeatures, 0}, |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 544 | {YANG_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE}, |
| 545 | {YANG_STATUS, &en->flags, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 546 | {(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] | 547 | {YANG_CUSTOM, NULL, 0} |
| 548 | }; |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 549 | 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] | 550 | } |
| 551 | |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 552 | /** |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 553 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have |
| 554 | * more instances, such as base or if-feature. |
| 555 | * |
| 556 | * @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] | 557 | * @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] | 558 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 559 | * @param[in] kw Type of current element. |
| 560 | * @param[out] values Parsed values to add to. |
| 561 | * @param[in] arg_type Expected type of attribute. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 562 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 563 | * @param[in,out] exts Extension instance to add to. |
| 564 | * |
| 565 | * @return LY_ERR values. |
| 566 | */ |
| 567 | static LY_ERR |
| 568 | 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] | 569 | 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] | 570 | { |
| 571 | const char **value; |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 572 | 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] | 573 | uint32_t index = LY_ARRAY_SIZE(*values) - 1; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 574 | struct yin_subelement subelems[1] = { |
| 575 | {YANG_CUSTOM, &index, 0} |
| 576 | }; |
| 577 | |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 578 | 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] | 579 | |
| 580 | return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts); |
| 581 | } |
| 582 | |
| 583 | /** |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 584 | * @brief Parse require instance element. |
| 585 | * |
| 586 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
| 587 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 588 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 589 | * @prama[out] type Type structure to store value, flag and extensions. |
| 590 | * |
| 591 | * @return LY_ERR values. |
| 592 | */ |
| 593 | static LY_ERR |
| 594 | yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, |
| 595 | const char **data, struct lysp_type *type) |
| 596 | { |
| 597 | const char *temp_val = NULL; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 598 | struct yin_subelement subelems[1] = { |
| 599 | {YANG_CUSTOM, NULL, 0} |
| 600 | }; |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 601 | |
| 602 | type->flags |= LYS_SET_REQINST; |
| 603 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_REQUIRE_INSTANCE)); |
| 604 | if (strcmp(temp_val, "true") == 0) { |
| 605 | type->require_instance = 1; |
| 606 | } else if (strcmp(temp_val, "false") != 0) { |
| 607 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "require-instance"); |
| 608 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 609 | return LY_EVALID; |
| 610 | } |
| 611 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 612 | |
| 613 | return yin_parse_content(ctx, subelems, 1, data, YANG_REQUIRE_INSTANCE, NULL, &type->exts); |
| 614 | } |
| 615 | |
| 616 | /** |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 617 | * @brief Parse modifier element. |
| 618 | * |
| 619 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
| 620 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 621 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 622 | * @param[in,out] pat Value to write to. |
| 623 | * @param[in,out] exts Extension instances to add to. |
| 624 | * |
| 625 | * @return LY_ERR values. |
| 626 | */ |
| 627 | static LY_ERR |
| 628 | yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 629 | const char **pat, struct lysp_ext_instance **exts) |
| 630 | { |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 631 | assert(**pat == 0x06); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 632 | const char *temp_val; |
| 633 | char *modified_val; |
| 634 | struct yin_subelement subelems[1] = { |
| 635 | {YANG_CUSTOM, NULL, 0} |
| 636 | }; |
| 637 | |
| 638 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MODIFIER)); |
| 639 | if (strcmp(temp_val, "invert-match") != 0) { |
| 640 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "modifier"); |
| 641 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 642 | return LY_EVALID; |
| 643 | } |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 644 | lydict_remove(ctx->xml_ctx.ctx, temp_val); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 645 | |
| 646 | /* allocate new value */ |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 647 | modified_val = malloc(strlen(*pat) + 1); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 648 | 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] | 649 | strcpy(modified_val, *pat); |
| 650 | lydict_remove(ctx->xml_ctx.ctx, *pat); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 651 | |
| 652 | /* modify the new value */ |
| 653 | modified_val[0] = 0x15; |
| 654 | *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val); |
| 655 | |
| 656 | return yin_parse_content(ctx, subelems, 1, data, YANG_MODIFIER, NULL, exts); |
| 657 | } |
| 658 | |
| 659 | /** |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 660 | * @brief Parse a restriction element (length, range or one instance of must). |
| 661 | * |
| 662 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
| 663 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 664 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 665 | * @param[in] restr_kw Identificaton of element that is being parsed, can be set to YANG_MUST, YANG_LENGTH or YANG_RANGE. |
| 666 | * @param[in] |
| 667 | */ |
| 668 | static LY_ERR |
| 669 | yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 670 | enum yang_keyword restr_kw, struct lysp_restr *restr) |
| 671 | { |
| 672 | assert(restr_kw == YANG_MUST || restr_kw == YANG_LENGTH || restr_kw == YANG_RANGE); |
| 673 | struct yin_subelement subelems[5] = { |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 674 | {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE}, |
| 675 | {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE}, |
| 676 | {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE}, |
| 677 | {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE}, |
| 678 | {YANG_CUSTOM, NULL, 0} |
| 679 | }; |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 680 | /* argument of must is called condition, but argument of length and range is called value */ |
| 681 | enum YIN_ARGUMENT arg_type = (restr_kw == YANG_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE; |
| 682 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw)); |
| 683 | |
| 684 | return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts); |
| 685 | } |
| 686 | |
| 687 | /** |
David Sedlák | bc9ec9c | 2019-07-11 15:53:55 +0200 | [diff] [blame] | 688 | * @brief Parse must element. |
| 689 | * |
| 690 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 691 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 692 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 693 | * @param[in,out] restrs Restrictions to add to. |
| 694 | * |
| 695 | * @return LY_ERR values. |
| 696 | */ |
| 697 | static LY_ERR |
| 698 | yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs) |
| 699 | { |
| 700 | struct lysp_restr *restr; |
| 701 | |
| 702 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM); |
| 703 | return yin_parse_restriction(ctx, attrs, data, YANG_MUST, restr); |
| 704 | } |
| 705 | |
| 706 | /** |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 707 | * @brief Parse position or value element. |
| 708 | * |
| 709 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 710 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 711 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 712 | * @param[in] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE. |
| 713 | * @param[out] enm Enum structure to save value, flags and extensions. |
| 714 | * |
| 715 | * @return LY_ERR values. |
| 716 | */ |
| 717 | static LY_ERR |
| 718 | yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 719 | enum yang_keyword kw, struct lysp_type_enum *enm) |
| 720 | { |
| 721 | assert(kw == YANG_POSITION || kw == YANG_VALUE); |
| 722 | const char *temp_val = NULL; |
| 723 | char *ptr; |
| 724 | long int num; |
| 725 | unsigned long int unum; |
| 726 | |
| 727 | /* set value flag */ |
| 728 | enm->flags |= LYS_SET_VALUE; |
| 729 | |
| 730 | /* get attribute value */ |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 731 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw)); |
David Sedlák | ebcd0eb | 2019-07-16 17:55:12 +0200 | [diff] [blame] | 732 | if (!temp_val || (temp_val[0] == '+') || ((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] | 733 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw)); |
| 734 | goto error; |
| 735 | } |
| 736 | |
| 737 | /* convert value */ |
| 738 | errno = 0; |
| 739 | if (kw == YANG_VALUE) { |
| 740 | num = strtol(temp_val, &ptr, 10); |
| 741 | if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) { |
| 742 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw)); |
| 743 | goto error; |
| 744 | } |
| 745 | } else { |
| 746 | unum = strtoul(temp_val, &ptr, 10); |
| 747 | if (unum > UINT64_C(4294967295)) { |
| 748 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw)); |
| 749 | goto error; |
| 750 | } |
| 751 | } |
| 752 | /* check if whole argument value was converted */ |
| 753 | if (*ptr != '\0') { |
| 754 | 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] | 755 | goto error; |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 756 | } |
| 757 | if (errno == ERANGE) { |
| 758 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, ly_stmt2str(kw)); |
| 759 | goto error; |
| 760 | } |
| 761 | /* save correctly ternary operator can't be used because num and unum have different signes */ |
| 762 | if (kw == YANG_VALUE) { |
| 763 | enm->value = num; |
| 764 | } else { |
| 765 | enm->value = unum; |
| 766 | } |
| 767 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 768 | |
| 769 | /* parse subelements */ |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 770 | struct yin_subelement subelems[1] = { |
| 771 | {YANG_CUSTOM, NULL, 0} |
| 772 | }; |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 773 | return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts); |
| 774 | |
| 775 | error: |
| 776 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 777 | return LY_EVALID; |
| 778 | } |
| 779 | |
| 780 | /** |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 781 | * @brief Function to parse meta tags (description, contact, ...) eg. elements with |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 782 | * text element as child |
| 783 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 784 | * @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] | 785 | * @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] | 786 | * @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] | 787 | * @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] | 788 | * @param[in,out] exts Extension instance to add to. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 789 | * |
| 790 | * @return LY_ERR values. |
| 791 | */ |
| 792 | static LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 793 | 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] | 794 | const char **value, struct lysp_ext_instance **exts) |
| 795 | { |
| 796 | assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE); |
| 797 | |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 798 | struct yin_subelement subelems[2] = { |
| 799 | {YANG_CUSTOM, NULL, 0}, |
| 800 | {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST} |
| 801 | }; |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 802 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 803 | 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] | 804 | } |
| 805 | |
| 806 | /** |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 807 | * @brief Parse error-message element. |
| 808 | * |
| 809 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
| 810 | * @param[in,out] data Data to read from. |
| 811 | * @param[out] value Where the content of error-message element should be stored. |
| 812 | * @param[in,out] exts Extension instance to add to. |
| 813 | * |
| 814 | * @return LY_ERR values. |
| 815 | */ |
| 816 | static LY_ERR |
| 817 | yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value, |
| 818 | struct lysp_ext_instance **exts) |
| 819 | { |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 820 | struct yin_subelement subelems[2] = { |
| 821 | {YANG_CUSTOM, NULL, 0}, |
| 822 | {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST} |
| 823 | }; |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 824 | |
| 825 | return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts); |
| 826 | } |
| 827 | |
| 828 | /** |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 829 | * @brief Map keyword type to substatement info. |
| 830 | * |
| 831 | * @param[in] kw Keyword type. |
| 832 | * |
| 833 | * @return correct LYEXT_SUBSTMT information. |
| 834 | */ |
| 835 | static LYEXT_SUBSTMT |
| 836 | kw2lyext_substmt(enum yang_keyword kw) |
| 837 | { |
| 838 | switch (kw) { |
| 839 | case YANG_ARGUMENT: |
| 840 | return LYEXT_SUBSTMT_ARGUMENT; |
| 841 | case YANG_BASE: |
| 842 | return LYEXT_SUBSTMT_BASE; |
| 843 | case YANG_BELONGS_TO: |
| 844 | return LYEXT_SUBSTMT_BELONGSTO; |
| 845 | case YANG_CONTACT: |
| 846 | return LYEXT_SUBSTMT_CONTACT; |
| 847 | case YANG_DEFAULT: |
| 848 | return LYEXT_SUBSTMT_DEFAULT; |
| 849 | case YANG_DESCRIPTION: |
| 850 | return LYEXT_SUBSTMT_DESCRIPTION; |
| 851 | case YANG_ERROR_APP_TAG: |
| 852 | return LYEXT_SUBSTMT_ERRTAG; |
| 853 | case YANG_ERROR_MESSAGE: |
| 854 | return LYEXT_SUBSTMT_ERRMSG; |
| 855 | case YANG_KEY: |
| 856 | return LYEXT_SUBSTMT_KEY; |
| 857 | case YANG_NAMESPACE: |
| 858 | return LYEXT_SUBSTMT_NAMESPACE; |
| 859 | case YANG_ORGANIZATION: |
| 860 | return LYEXT_SUBSTMT_ORGANIZATION; |
| 861 | case YANG_PATH: |
| 862 | return LYEXT_SUBSTMT_PATH; |
| 863 | case YANG_PREFIX: |
| 864 | return LYEXT_SUBSTMT_PREFIX; |
| 865 | case YANG_PRESENCE: |
| 866 | return LYEXT_SUBSTMT_PRESENCE; |
| 867 | case YANG_REFERENCE: |
| 868 | return LYEXT_SUBSTMT_REFERENCE; |
| 869 | case YANG_REVISION_DATE: |
| 870 | return LYEXT_SUBSTMT_REVISIONDATE; |
| 871 | case YANG_UNITS: |
| 872 | return LYEXT_SUBSTMT_UNITS; |
| 873 | case YANG_VALUE: |
| 874 | return LYEXT_SUBSTMT_VALUE; |
| 875 | case YANG_YANG_VERSION: |
| 876 | return LYEXT_SUBSTMT_VERSION; |
| 877 | case YANG_MODIFIER: |
| 878 | return LYEXT_SUBSTMT_MODIFIER; |
| 879 | case YANG_REQUIRE_INSTANCE: |
| 880 | return LYEXT_SUBSTMT_REQINSTANCE; |
| 881 | case YANG_YIN_ELEMENT: |
| 882 | return LYEXT_SUBSTMT_YINELEM; |
| 883 | case YANG_CONFIG: |
| 884 | return LYEXT_SUBSTMT_CONFIG; |
| 885 | case YANG_MANDATORY: |
| 886 | return LYEXT_SUBSTMT_MANDATORY; |
| 887 | case YANG_ORDERED_BY: |
| 888 | return LYEXT_SUBSTMT_ORDEREDBY; |
| 889 | case YANG_STATUS: |
| 890 | return LYEXT_SUBSTMT_STATUS; |
| 891 | case YANG_FRACTION_DIGITS: |
| 892 | return LYEXT_SUBSTMT_FRACDIGITS; |
| 893 | case YANG_MAX_ELEMENTS: |
| 894 | return LYEXT_SUBSTMT_MAX; |
| 895 | case YANG_MIN_ELEMENTS: |
| 896 | return LYEXT_SUBSTMT_MIN; |
| 897 | case YANG_POSITION: |
| 898 | return LYEXT_SUBSTMT_POSITION; |
| 899 | case YANG_UNIQUE: |
| 900 | return LYEXT_SUBSTMT_UNIQUE; |
| 901 | case YANG_IF_FEATURE: |
| 902 | return LYEXT_SUBSTMT_IFFEATURE; |
| 903 | default: |
| 904 | return LYEXT_SUBSTMT_SELF; |
| 905 | } |
| 906 | } |
| 907 | |
David Sedlák | 9c40a92 | 2019-07-08 17:04:43 +0200 | [diff] [blame] | 908 | /** |
| 909 | * @brief Parse belongs-to element. |
| 910 | * |
| 911 | * @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] | 912 | * @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] | 913 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 914 | * @param[out] submod Structure of submodule that is being parsed. |
| 915 | * @param[in,out] exts Extension instances to add to. |
| 916 | * |
| 917 | * @return LY_ERR values |
| 918 | */ |
| 919 | static LY_ERR |
| 920 | yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 921 | struct lysp_submodule *submod, struct lysp_ext_instance **exts) |
| 922 | { |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 923 | struct yin_subelement subelems[2] = { |
| 924 | {YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 925 | {YANG_CUSTOM, NULL, 0} |
| 926 | }; |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 927 | 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] | 928 | |
| 929 | return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts); |
| 930 | } |
| 931 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 932 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 933 | 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] | 934 | 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] | 935 | { |
| 936 | LY_ERR ret = LY_SUCCESS; |
| 937 | struct sized_string prefix, name; |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 938 | char *out = NULL; |
| 939 | size_t out_len = 0; |
| 940 | int dynamic = 0; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 941 | struct yin_arg_record *subelem_attrs = NULL; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 942 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 943 | struct yin_subelement *subelem_info_rec = NULL; |
David Sedlák | cb5d83f | 2019-07-09 09:32:53 +0200 | [diff] [blame] | 944 | uint32_t index = 0; |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 945 | struct lysp_type *type; |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 946 | assert(is_ordered(subelem_info, subelem_info_size)); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 947 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 948 | if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) { |
| 949 | 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] | 950 | /* current element has subelements as content */ |
| 951 | if (ret == LY_EINVAL) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 952 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
| 953 | 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] | 954 | LY_CHECK_GOTO(ret, cleanup); |
| 955 | if (!name.value) { |
| 956 | /* end of current element reached */ |
| 957 | break; |
| 958 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 959 | ret = yin_load_attributes(ctx, data, &subelem_attrs); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 960 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 961 | 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] | 962 | |
| 963 | /* check if this element can be child of current element */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 964 | subelem_info_rec = get_record(kw, subelem_info_size, subelem_info); |
| 965 | if (!subelem_info_rec) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 966 | 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] | 967 | ret = LY_EVALID; |
| 968 | goto cleanup; |
| 969 | } |
| 970 | |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 971 | /* TODO check relative order */ |
| 972 | |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 973 | /* if element is unique and already defined log error */ |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 974 | if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 975 | 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] | 976 | return LY_EVALID; |
| 977 | } |
| 978 | if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) { |
David Sedlák | 66d7c84 | 2019-07-11 15:06:04 +0200 | [diff] [blame] | 979 | ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec); |
| 980 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 981 | } |
| 982 | subelem_info_rec->flags |= YIN_SUBELEM_PARSED; |
| 983 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 984 | switch (kw) { |
| 985 | case YANG_CUSTOM: |
David Sedlák | cb5d83f | 2019-07-09 09:32:53 +0200 | [diff] [blame] | 986 | index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0; |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 987 | ret = yin_parse_extension_instance(ctx, subelem_attrs, data, name2fullname(name.value, prefix.len), |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 988 | namelen2fulllen(name.len, prefix.len), |
David Sedlák | cb5d83f | 2019-07-09 09:32:53 +0200 | [diff] [blame] | 989 | kw2lyext_substmt(current_element), index, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 990 | break; |
| 991 | case YANG_ACTION: |
| 992 | break; |
| 993 | case YANG_ANYDATA: |
| 994 | break; |
| 995 | case YANG_ANYXML: |
| 996 | break; |
| 997 | case YANG_ARGUMENT: |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 998 | ret = yin_parse_argument_element(ctx, subelem_attrs, data, (struct yin_argument_meta *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 999 | break; |
| 1000 | case YANG_AUGMENT: |
| 1001 | break; |
| 1002 | case YANG_BASE: |
David Sedlák | a62750b | 2019-07-16 11:21:31 +0200 | [diff] [blame] | 1003 | if (current_element == YANG_TYPE) { |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1004 | type = (struct lysp_type *)subelem_info_rec->dest; |
| 1005 | ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME, |
| 1006 | Y_PREF_IDENTIF_ARG, exts); |
| 1007 | type->flags |= LYS_SET_BASE; |
David Sedlák | a62750b | 2019-07-16 11:21:31 +0200 | [diff] [blame] | 1008 | } else if (current_element == YANG_IDENTITY) { |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1009 | ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest, |
| 1010 | YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts); |
| 1011 | } else { |
| 1012 | LOGINT(ctx->xml_ctx.ctx); |
| 1013 | ret = LY_EINT; |
| 1014 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1015 | break; |
| 1016 | case YANG_BELONGS_TO: |
David Sedlák | 9c40a92 | 2019-07-08 17:04:43 +0200 | [diff] [blame] | 1017 | ret = yin_parse_belongs_to(ctx, subelem_attrs, data, (struct lysp_submodule *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1018 | break; |
| 1019 | case YANG_BIT: |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 1020 | case YANG_ENUM: |
| 1021 | ret = yin_parse_enum_bit(ctx, subelem_attrs, data, kw, (struct lysp_type *)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1022 | break; |
| 1023 | case YANG_CASE: |
| 1024 | break; |
| 1025 | case YANG_CHOICE: |
| 1026 | break; |
| 1027 | case YANG_CONFIG: |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 1028 | ret = yin_parse_config(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1029 | break; |
| 1030 | case YANG_CONTACT: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 1031 | case YANG_DESCRIPTION: |
| 1032 | case YANG_ORGANIZATION: |
| 1033 | case YANG_REFERENCE: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1034 | ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1035 | break; |
| 1036 | case YANG_CONTAINER: |
| 1037 | break; |
| 1038 | case YANG_DEFAULT: |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 1039 | ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest, |
| 1040 | YIN_ARG_VALUE, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1041 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1042 | case YANG_DEVIATE: |
| 1043 | break; |
| 1044 | case YANG_DEVIATION: |
| 1045 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1046 | case YANG_ERROR_APP_TAG: |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 1047 | ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest, |
| 1048 | YIN_ARG_VALUE, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1049 | break; |
| 1050 | case YANG_ERROR_MESSAGE: |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1051 | ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1052 | break; |
| 1053 | case YANG_EXTENSION: |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1054 | ret = yin_parse_extension(ctx, subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1055 | break; |
| 1056 | case YANG_FEATURE: |
| 1057 | break; |
| 1058 | case YANG_FRACTION_DIGITS: |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 1059 | ret = yin_parse_fracdigits(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1060 | break; |
| 1061 | case YANG_GROUPING: |
| 1062 | break; |
| 1063 | case YANG_IDENTITY: |
| 1064 | break; |
| 1065 | case YANG_IF_FEATURE: |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 1066 | ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 1067 | (const char ***)subelem_info_rec->dest, YIN_ARG_NAME, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1068 | break; |
| 1069 | case YANG_IMPORT: |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1070 | ret = yin_parse_import(ctx, subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1071 | break; |
| 1072 | case YANG_INCLUDE: |
| 1073 | break; |
| 1074 | case YANG_INPUT: |
| 1075 | break; |
| 1076 | case YANG_KEY: |
| 1077 | break; |
| 1078 | case YANG_LEAF: |
| 1079 | break; |
| 1080 | case YANG_LEAF_LIST: |
| 1081 | break; |
| 1082 | case YANG_LENGTH: |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 1083 | type = (struct lysp_type *)subelem_info_rec->dest; |
| 1084 | type->length = calloc(1, sizeof *type->length); |
| 1085 | LY_CHECK_ERR_GOTO(!type->length, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup); |
| 1086 | ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->length); |
| 1087 | type->flags |= LYS_SET_LENGTH; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1088 | break; |
| 1089 | case YANG_LIST: |
| 1090 | break; |
| 1091 | case YANG_MANDATORY: |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 1092 | ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1093 | break; |
| 1094 | case YANG_MAX_ELEMENTS: |
| 1095 | break; |
| 1096 | case YANG_MIN_ELEMENTS: |
| 1097 | break; |
| 1098 | case YANG_MODIFIER: |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 1099 | ret = yin_parse_modifier(ctx, subelem_attrs, data, (const char **)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1100 | break; |
| 1101 | case YANG_MODULE: |
| 1102 | break; |
| 1103 | case YANG_MUST: |
David Sedlák | bc9ec9c | 2019-07-11 15:53:55 +0200 | [diff] [blame] | 1104 | ret = yin_parse_must(ctx, subelem_attrs, data, (struct lysp_restr **)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1105 | break; |
| 1106 | case YANG_NAMESPACE: |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 1107 | ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest, |
| 1108 | YIN_ARG_URI, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1109 | break; |
| 1110 | case YANG_NOTIFICATION: |
| 1111 | break; |
| 1112 | case YANG_ORDERED_BY: |
| 1113 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1114 | case YANG_OUTPUT: |
| 1115 | break; |
| 1116 | case YANG_PATH: |
David Sedlák | 5897987 | 2019-07-12 11:42:43 +0200 | [diff] [blame] | 1117 | type = (struct lysp_type *)subelem_info_rec->dest; |
| 1118 | ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, &type->path, |
| 1119 | YIN_ARG_VALUE, Y_STR_ARG, exts); |
| 1120 | type->flags |= LYS_SET_PATH; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1121 | break; |
| 1122 | case YANG_PATTERN: |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 1123 | ret = yin_parse_pattern(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1124 | break; |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 1125 | case YANG_VALUE: |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1126 | case YANG_POSITION: |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 1127 | ret = yin_parse_value_pos_element(ctx, subelem_attrs, data, kw, |
| 1128 | (struct lysp_type_enum *)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1129 | break; |
| 1130 | case YANG_PREFIX: |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 1131 | ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1132 | (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1133 | break; |
| 1134 | case YANG_PRESENCE: |
| 1135 | break; |
| 1136 | case YANG_RANGE: |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 1137 | type = (struct lysp_type *)subelem_info_rec->dest; |
| 1138 | type->range = calloc(1, sizeof *type->range); |
David Sedlák | 66d7c84 | 2019-07-11 15:06:04 +0200 | [diff] [blame] | 1139 | LY_CHECK_ERR_GOTO(!type->range, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 1140 | ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->range); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 1141 | type->flags |= LYS_SET_RANGE; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1142 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1143 | case YANG_REFINE: |
| 1144 | break; |
| 1145 | case YANG_REQUIRE_INSTANCE: |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 1146 | ret = yin_pasrse_reqinstance(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1147 | break; |
| 1148 | case YANG_REVISION: |
| 1149 | break; |
| 1150 | case YANG_REVISION_DATE: |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1151 | ret = yin_parse_revision_date(ctx, subelem_attrs, data, (char *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1152 | break; |
| 1153 | case YANG_RPC: |
| 1154 | break; |
| 1155 | case YANG_STATUS: |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1156 | ret = yin_parse_status(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1157 | break; |
| 1158 | case YANG_SUBMODULE: |
| 1159 | break; |
| 1160 | case YANG_TYPE: |
| 1161 | break; |
| 1162 | case YANG_TYPEDEF: |
| 1163 | break; |
| 1164 | case YANG_UNIQUE: |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 1165 | ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest, |
| 1166 | YIN_ARG_TAG, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1167 | break; |
| 1168 | case YANG_UNITS: |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 1169 | ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest, |
| 1170 | YIN_ARG_NAME, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1171 | break; |
| 1172 | case YANG_USES: |
| 1173 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1174 | case YANG_WHEN: |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 1175 | ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1176 | break; |
| 1177 | case YANG_YANG_VERSION: |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 1178 | ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1179 | break; |
| 1180 | case YANG_YIN_ELEMENT: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1181 | ret = yin_parse_yin_element_element(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1182 | break; |
| 1183 | case YIN_TEXT: |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1184 | case YIN_VALUE: |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1185 | ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem_info_rec->dest, NULL); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1186 | break; |
| 1187 | default: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1188 | LOGINT(ctx->xml_ctx.ctx); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 1189 | return LY_EINT; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1190 | } |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1191 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1192 | FREE_ARRAY(ctx, subelem_attrs, free_arg_rec); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1193 | subelem_attrs = NULL; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1194 | subelem_info_rec = NULL; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1195 | } |
| 1196 | } else { |
| 1197 | /* elements with text or none content */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1198 | /* save text content, if text_content isn't set, it's just ignored */ |
David Sedlák | a62750b | 2019-07-16 11:21:31 +0200 | [diff] [blame] | 1199 | /* TODO add text validation */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1200 | if (text_content) { |
| 1201 | if (dynamic) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1202 | *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1203 | if (!*text_content) { |
| 1204 | free(out); |
| 1205 | return LY_EMEM; |
| 1206 | } |
| 1207 | } else { |
| 1208 | if (out_len == 0) { |
David Sedlák | 9929532 | 2019-07-17 11:34:18 +0200 | [diff] [blame^] | 1209 | *text_content = lydict_insert(ctx->xml_ctx.ctx, "", 0); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1210 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1211 | *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1215 | /* load closing element */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1216 | 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] | 1217 | } |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 1218 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1219 | 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] | 1220 | } |
| 1221 | |
| 1222 | cleanup: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1223 | FREE_ARRAY(ctx, subelem_attrs, free_arg_rec); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 1224 | return ret; |
| 1225 | } |
| 1226 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 1227 | LY_ERR |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1228 | 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] | 1229 | struct lysp_ext_instance **exts) |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 1230 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1231 | const char *temp_rev; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1232 | struct yin_subelement subelems[1] = { |
| 1233 | {YANG_CUSTOM, NULL, 0} |
| 1234 | }; |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 1235 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1236 | 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] | 1237 | LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, |
| 1238 | FREE_STRING(ctx->xml_ctx.ctx, temp_rev), LY_EVALID); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 1239 | |
| 1240 | strcpy(rev, temp_rev); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1241 | FREE_STRING(ctx->xml_ctx.ctx, temp_rev); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 1242 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1243 | 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] | 1244 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1245 | |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 1246 | /** |
| 1247 | * @brief Parse config element. |
| 1248 | * |
| 1249 | * @param[in] ctx Yin parser context for logging and to store current state. |
| 1250 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element. |
| 1251 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1252 | * @param[in,out] flags Flags to add to. |
| 1253 | * @param[in,out] exts Extension instances to add to. |
| 1254 | * |
| 1255 | * @return LY_ERR values. |
| 1256 | */ |
| 1257 | static LY_ERR |
| 1258 | yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags, |
| 1259 | struct lysp_ext_instance **exts) |
| 1260 | { |
| 1261 | const char *temp_val = NULL; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1262 | struct yin_subelement subelems[1] = { |
| 1263 | {YANG_CUSTOM, NULL, 0} |
| 1264 | }; |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 1265 | |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1266 | 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] | 1267 | if (strcmp(temp_val, "true") == 0) { |
| 1268 | *flags |= LYS_CONFIG_W; |
| 1269 | } else if (strcmp(temp_val, "false") == 0) { |
| 1270 | *flags |= LYS_CONFIG_R; |
| 1271 | } else { |
| 1272 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config"); |
| 1273 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1274 | return LY_EVALID; |
| 1275 | } |
| 1276 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1277 | |
| 1278 | return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts); |
| 1279 | } |
| 1280 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1281 | LY_ERR |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 1282 | yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version, |
| 1283 | struct lysp_ext_instance **exts) |
| 1284 | { |
| 1285 | const char *temp_version = NULL; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1286 | struct yin_subelement subelems[1] = { |
| 1287 | {YANG_CUSTOM, NULL, 0} |
| 1288 | }; |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 1289 | |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1290 | 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] | 1291 | if (strcmp(temp_version, "1.0") == 0) { |
| 1292 | *version = LYS_VERSION_1_0; |
| 1293 | } else if (strcmp(temp_version, "1.1") == 0) { |
| 1294 | *version = LYS_VERSION_1_1; |
| 1295 | } else { |
| 1296 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version"); |
| 1297 | FREE_STRING(ctx->xml_ctx.ctx, temp_version); |
| 1298 | return LY_EVALID; |
| 1299 | } |
| 1300 | FREE_STRING(ctx->xml_ctx.ctx, temp_version); |
| 1301 | ctx->mod_version = *version; |
| 1302 | |
| 1303 | return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts); |
| 1304 | } |
| 1305 | |
| 1306 | LY_ERR |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1307 | 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] | 1308 | { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 1309 | struct lysp_import *imp; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1310 | /* allocate new element in sized array for import */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1311 | 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] | 1312 | |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1313 | struct yin_subelement subelems[5] = { |
| 1314 | {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE}, |
| 1315 | {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 1316 | {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE}, |
| 1317 | {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE}, |
| 1318 | {YANG_CUSTOM, NULL, 0} |
| 1319 | }; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 1320 | |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 1321 | /* parse import attributes */ |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1322 | 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] | 1323 | 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] | 1324 | /* check prefix validity */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1325 | 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] | 1326 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1327 | 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] | 1328 | } |
| 1329 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1330 | LY_ERR |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 1331 | yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags, |
| 1332 | struct lysp_ext_instance **exts) |
| 1333 | { |
| 1334 | const char *temp_val = NULL; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1335 | struct yin_subelement subelems[1] = { |
| 1336 | {YANG_CUSTOM, NULL, 0} |
| 1337 | }; |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 1338 | |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1339 | 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] | 1340 | if (strcmp(temp_val, "true") == 0) { |
| 1341 | *flags |= LYS_MAND_TRUE; |
| 1342 | } else if (strcmp(temp_val, "false") == 0) { |
| 1343 | *flags |= LYS_MAND_FALSE; |
| 1344 | } else { |
| 1345 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory"); |
| 1346 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1347 | return LY_EVALID; |
| 1348 | } |
| 1349 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1350 | |
| 1351 | return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts); |
| 1352 | } |
| 1353 | |
| 1354 | LY_ERR |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1355 | 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] | 1356 | struct lysp_ext_instance **exts) |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1357 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1358 | const char *value = NULL; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1359 | struct yin_subelement subelems[1] = { |
| 1360 | {YANG_CUSTOM, NULL, 0} |
| 1361 | }; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1362 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1363 | 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] | 1364 | if (strcmp(value, "current") == 0) { |
| 1365 | *flags |= LYS_STATUS_CURR; |
| 1366 | } else if (strcmp(value, "deprecated") == 0) { |
| 1367 | *flags |= LYS_STATUS_DEPRC; |
| 1368 | } else if (strcmp(value, "obsolete") == 0) { |
| 1369 | *flags |= LYS_STATUS_OBSLT; |
| 1370 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1371 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status"); |
| 1372 | FREE_STRING(ctx->xml_ctx.ctx, value); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1373 | return LY_EVALID; |
| 1374 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1375 | FREE_STRING(ctx->xml_ctx.ctx, value); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1376 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1377 | 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] | 1378 | } |
| 1379 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1380 | LY_ERR |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 1381 | yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p) |
| 1382 | { |
| 1383 | struct lysp_when *when; |
| 1384 | when = calloc(1, sizeof *when); |
| 1385 | 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] | 1386 | 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] | 1387 | *when_p = when; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1388 | struct yin_subelement subelems[3] = { |
| 1389 | {YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE}, |
| 1390 | {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE}, |
| 1391 | {YANG_CUSTOM, NULL, 0} |
| 1392 | }; |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 1393 | |
| 1394 | return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts); |
| 1395 | } |
| 1396 | |
| 1397 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1398 | 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] | 1399 | uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 1400 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1401 | const char *temp_val = NULL; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1402 | struct yin_subelement subelems[1] = { |
| 1403 | {YANG_CUSTOM, NULL, 0} |
| 1404 | }; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 1405 | |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1406 | 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] | 1407 | if (strcmp(temp_val, "true") == 0) { |
| 1408 | *flags |= LYS_YINELEM_TRUE; |
| 1409 | } else if (strcmp(temp_val, "false") == 0) { |
| 1410 | *flags |= LYS_YINELEM_FALSE; |
| 1411 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1412 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element"); |
| 1413 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 1414 | return LY_EVALID; |
| 1415 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1416 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 1417 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1418 | 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] | 1419 | } |
| 1420 | |
| 1421 | LY_ERR |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1422 | 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] | 1423 | 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] | 1424 | { |
| 1425 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1426 | char *out; |
| 1427 | const char *name, *prefix; |
| 1428 | size_t out_len, prefix_len, name_len; |
| 1429 | int dynamic; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1430 | struct lysp_ext_instance *e; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1431 | struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL; |
| 1432 | struct yin_arg_record *iter; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1433 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1434 | 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] | 1435 | |
| 1436 | e->yin = 0; |
| 1437 | /* store name and insubstmt info */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1438 | 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] | 1439 | e->insubstmt = subelem; |
| 1440 | e->insubstmt_index = subelem_index; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1441 | e->yin |= LYS_YIN; |
| 1442 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1443 | /* store attributes as subelements */ |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1444 | LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1445 | if (!iter->prefix) { |
| 1446 | new_subelem = calloc(1, sizeof(*new_subelem)); |
| 1447 | if (!e->child) { |
| 1448 | e->child = new_subelem; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1449 | } else { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1450 | last_subelem->next = new_subelem; |
| 1451 | } |
| 1452 | last_subelem = new_subelem; |
| 1453 | |
| 1454 | last_subelem->flags |= LYS_YIN_ATTR; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1455 | last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len); |
| 1456 | 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] | 1457 | if (iter->dynamic_content) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1458 | last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content); |
| 1459 | 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] | 1460 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1461 | last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len); |
| 1462 | 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] | 1463 | } |
| 1464 | } |
| 1465 | } |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1466 | |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 1467 | /* parse subelements */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1468 | if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) { |
| 1469 | 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] | 1470 | if (ret == LY_EINVAL) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1471 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
| 1472 | 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] | 1473 | if (!name) { |
| 1474 | /* end of extension instance reached */ |
| 1475 | break; |
| 1476 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1477 | 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] | 1478 | if (!e->child) { |
| 1479 | e->child = new_subelem; |
| 1480 | } else { |
| 1481 | last_subelem->next = new_subelem; |
| 1482 | } |
| 1483 | last_subelem = new_subelem; |
| 1484 | } |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 1485 | } else { |
| 1486 | /* save text content */ |
| 1487 | if (dynamic) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1488 | e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 1489 | if (!e->argument) { |
| 1490 | free(out); |
| 1491 | return LY_EMEM; |
| 1492 | } |
| 1493 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1494 | e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 1495 | LY_CHECK_RET(!e->argument, LY_EMEM); |
| 1496 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1497 | 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] | 1498 | LY_CHECK_RET(name, LY_EINT); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1499 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | return LY_SUCCESS; |
| 1503 | } |
| 1504 | |
| 1505 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1506 | 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] | 1507 | size_t prefix_len, const char **data, struct lysp_stmt **element) |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1508 | { |
| 1509 | LY_ERR ret = LY_SUCCESS; |
| 1510 | const char *temp_prefix, *temp_name; |
| 1511 | char *out = NULL; |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 1512 | size_t out_len, temp_name_len, temp_prefix_len; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1513 | int dynamic; |
| 1514 | struct yin_arg_record *subelem_args = NULL; |
| 1515 | struct lysp_stmt *last = NULL, *new = NULL; |
| 1516 | |
| 1517 | /* allocate new structure for element */ |
| 1518 | *element = calloc(1, sizeof(**element)); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1519 | (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len); |
| 1520 | 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] | 1521 | |
| 1522 | last = (*element)->child; |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 1523 | /* load attributes */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1524 | while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1525 | /* add new element to linked-list */ |
| 1526 | new = calloc(1, sizeof(*last)); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1527 | LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1528 | if (!(*element)->child) { |
| 1529 | /* save first */ |
| 1530 | (*element)->child = new; |
| 1531 | } else { |
| 1532 | last->next = new; |
| 1533 | } |
| 1534 | last = new; |
| 1535 | |
| 1536 | last->flags |= LYS_YIN_ATTR; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1537 | 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] | 1538 | LY_CHECK_GOTO(ret, err); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1539 | 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] | 1540 | LY_CHECK_GOTO(ret, err); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1541 | last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len); |
| 1542 | 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] | 1543 | /* attributes with prefix are ignored */ |
| 1544 | if (!temp_prefix) { |
| 1545 | if (dynamic) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1546 | last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1547 | if (!last->arg) { |
| 1548 | free(out); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1549 | LOGMEM(ctx->xml_ctx.ctx); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1550 | ret = LY_EMEM; |
| 1551 | goto err; |
| 1552 | } |
| 1553 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1554 | last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len); |
| 1555 | 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] | 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | /* parse content of element */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1561 | 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] | 1562 | if (ret == LY_EINVAL) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1563 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1564 | /* parse subelements */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1565 | 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] | 1566 | LY_CHECK_GOTO(ret, err); |
| 1567 | if (!name) { |
| 1568 | /* end of element reached */ |
| 1569 | break; |
| 1570 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1571 | 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] | 1572 | LY_CHECK_GOTO(ret, err); |
| 1573 | last = last->next; |
| 1574 | } |
| 1575 | } else { |
| 1576 | /* save element content */ |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 1577 | if (out_len != 0) { |
| 1578 | if (dynamic) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1579 | (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 1580 | if (!(*element)->arg) { |
| 1581 | free(out); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1582 | LOGMEM(ctx->xml_ctx.ctx); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 1583 | ret = LY_EMEM; |
| 1584 | goto err; |
| 1585 | } |
| 1586 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1587 | (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len); |
| 1588 | 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] | 1589 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1590 | } |
| 1591 | /* read closing tag */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1592 | 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] | 1593 | LY_CHECK_GOTO(ret, err); |
| 1594 | } |
| 1595 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1596 | FREE_ARRAY(ctx, subelem_args, free_arg_rec); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1597 | return LY_SUCCESS; |
| 1598 | |
| 1599 | err: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1600 | FREE_ARRAY(ctx, subelem_args, free_arg_rec); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1601 | return ret; |
| 1602 | } |
| 1603 | |
| 1604 | LY_ERR |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1605 | 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] | 1606 | struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts) |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 1607 | { |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1608 | struct yin_subelement subelems[2] = { |
| 1609 | {YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE}, |
| 1610 | {YANG_CUSTOM, NULL, 0} |
| 1611 | }; |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 1612 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1613 | 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] | 1614 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1615 | 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] | 1616 | } |
| 1617 | |
| 1618 | LY_ERR |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1619 | 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] | 1620 | { |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1621 | struct lysp_ext *ex; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1622 | 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] | 1623 | 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] | 1624 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1625 | struct yin_argument_meta arg_info = {&ex->flags, &ex->argument}; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1626 | struct yin_subelement subelems[5] = { |
| 1627 | {YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE}, |
| 1628 | {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE}, |
| 1629 | {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE}, |
| 1630 | {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE}, |
| 1631 | {YANG_CUSTOM, NULL, 0} |
| 1632 | }; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1633 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1634 | 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] | 1635 | } |
| 1636 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1637 | /** |
| 1638 | * @brief Parse module substatements. |
| 1639 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1640 | * @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] | 1641 | * @param[in] mod_attrs Attributes of module element. |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1642 | * @param[in,out] data Data to read from. |
| 1643 | * @param[out] mod Parsed module structure. |
| 1644 | * |
| 1645 | * @return LY_ERR values. |
| 1646 | */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1647 | static LY_ERR |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1648 | 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] | 1649 | { |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1650 | struct yin_subelement subelems[9] = { |
| 1651 | {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE}, |
| 1652 | {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE}, |
| 1653 | {YANG_EXTENSION, &(*mod)->exts, 0}, |
| 1654 | {YANG_IMPORT, *mod, 0}, |
| 1655 | {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 1656 | {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE}, |
| 1657 | {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 1658 | {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE}, |
| 1659 | {YANG_CUSTOM, NULL, 0} |
| 1660 | }; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1661 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1662 | 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] | 1663 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1664 | 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] | 1665 | } |
| 1666 | |
| 1667 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1668 | 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] | 1669 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1670 | LY_ERR ret = LY_SUCCESS; |
| 1671 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1672 | struct lysp_module *mod_p = NULL; |
| 1673 | const char *prefix, *name; |
| 1674 | size_t prefix_len, name_len; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1675 | |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1676 | struct yin_arg_record *attrs = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1677 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1678 | struct yin_parser_ctx yin_ctx; |
| 1679 | |
| 1680 | /* initialize context */ |
| 1681 | memset(&yin_ctx, 0, sizeof yin_ctx); |
| 1682 | yin_ctx.xml_ctx.ctx = ctx; |
| 1683 | yin_ctx.xml_ctx.line = 1; |
| 1684 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1685 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1686 | /* check submodule */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1687 | 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] | 1688 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1689 | ret = yin_load_attributes(&yin_ctx, &data, &attrs); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1690 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1691 | 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] | 1692 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1693 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 1694 | ret = LY_EINVAL; |
| 1695 | goto cleanup; |
| 1696 | } else if (kw != YANG_MODULE) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1697 | 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] | 1698 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1699 | ret = LY_EVALID; |
| 1700 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1701 | } |
| 1702 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1703 | /* allocate module */ |
| 1704 | mod_p = calloc(1, sizeof *mod_p); |
| 1705 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 1706 | mod_p->mod = mod; |
| 1707 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1708 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1709 | /* parse module substatements */ |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1710 | ret = yin_parse_mod(&yin_ctx, attrs, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1711 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 1712 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1713 | mod_p->parsing = 0; |
| 1714 | mod->parsed = mod_p; |
| 1715 | |
| 1716 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1717 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1718 | lysp_module_free(mod_p); |
| 1719 | } |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 1720 | FREE_ARRAY(&yin_ctx, attrs, free_arg_rec); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1721 | lyxml_context_clear(&yin_ctx.xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 1722 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1723 | } |