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 | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 22 | |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 23 | #include "context.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 24 | #include "dict.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 25 | #include "xml.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 26 | #include "tree.h" |
| 27 | #include "tree_schema.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 28 | #include "tree_schema_internal.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 29 | #include "parser_yin.h" |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 30 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 31 | /** |
| 32 | * @brief check if given string is URI of yin namespace. |
| 33 | * @param ns Namespace URI to check. |
| 34 | * |
| 35 | * @return true if ns equals YIN_NS_URI false otherwise. |
| 36 | */ |
| 37 | #define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0) |
| 38 | |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 39 | static LY_ERR |
| 40 | yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags, |
| 41 | struct lysp_ext_instance **exts); |
| 42 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 43 | const char *const yin_attr_list[] = { |
| 44 | [YIN_ARG_NAME] = "name", |
| 45 | [YIN_ARG_TARGET_NODE] = "target-node", |
| 46 | [YIN_ARG_MODULE] = "module", |
| 47 | [YIN_ARG_VALUE] = "value", |
| 48 | [YIN_ARG_TEXT] = "text", |
| 49 | [YIN_ARG_CONDITION] = "condition", |
| 50 | [YIN_ARG_URI] = "uri", |
| 51 | [YIN_ARG_DATE] = "date", |
| 52 | [YIN_ARG_TAG] = "tag", |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 55 | enum yang_keyword |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 56 | yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len, |
| 57 | const char *prefix, size_t prefix_len, enum yang_keyword parrent) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 58 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 59 | const char *start = NULL; |
| 60 | enum yang_keyword kw = YANG_NONE; |
| 61 | const struct lyxml_ns *ns = NULL; |
| 62 | |
| 63 | if (!name || name_len == 0) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 64 | return YANG_NONE; |
| 65 | } |
| 66 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 67 | ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 68 | if (ns) { |
| 69 | if (!IS_YIN_NS(ns->uri)) { |
| 70 | return YANG_CUSTOM; |
| 71 | } |
| 72 | } else { |
| 73 | /* elements without namespace are automatically unknown */ |
| 74 | return YANG_NONE; |
| 75 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 76 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 77 | start = name; |
| 78 | kw = lysp_match_kw(NULL, &name); |
| 79 | |
| 80 | if (name - start == (long int)name_len) { |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 81 | /* this is done because of collision in yang statement value and yang argument mapped to yin element value */ |
| 82 | if (kw == YANG_VALUE && parrent == YANG_ERROR_MESSAGE) { |
| 83 | return YIN_VALUE; |
| 84 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 85 | return kw; |
| 86 | } else { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 87 | if (strncmp(start, "text", name_len) == 0) { |
| 88 | return YIN_TEXT; |
| 89 | } else if (strncmp(start, "value", name_len) == 0) { |
| 90 | return YIN_VALUE; |
| 91 | } else { |
| 92 | return YANG_NONE; |
| 93 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 97 | enum YIN_ARGUMENT |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 98 | yin_match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 99 | { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 100 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 101 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 102 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 103 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 104 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 105 | #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] | 106 | #define IF_ARG_PREFIX_END } |
| 107 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 108 | switch (*name) { |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 109 | case 'c': |
| 110 | already_read += 1; |
| 111 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 112 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 113 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 114 | case 'd': |
| 115 | already_read += 1; |
| 116 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 117 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 118 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 119 | case 'm': |
| 120 | already_read += 1; |
| 121 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 122 | break; |
| 123 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 124 | case 'n': |
| 125 | already_read += 1; |
| 126 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 127 | break; |
| 128 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 129 | case 't': |
| 130 | already_read += 1; |
| 131 | IF_ARG_PREFIX("a", 1) |
| 132 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 133 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 134 | IF_ARG_PREFIX_END |
| 135 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 136 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 137 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 138 | case 'u': |
| 139 | already_read += 1; |
| 140 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 141 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 142 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 143 | case 'v': |
| 144 | already_read += 1; |
| 145 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 146 | break; |
| 147 | } |
| 148 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 149 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 150 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 151 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 152 | } |
| 153 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 154 | #undef IF_ARG |
| 155 | #undef IF_ARG_PREFIX |
| 156 | #undef IF_ARG_PREFIX_END |
| 157 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 158 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 159 | } |
| 160 | |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 161 | /** |
| 162 | * @brief free argument record, content loaded from lyxml_get_string() can be |
| 163 | * dynamically allocated in some cases so it must be also freed. |
| 164 | */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 165 | static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) { |
| 166 | (void)ctx; /* unused */ |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 167 | if (record->dynamic_content) { |
| 168 | free(record->content); |
| 169 | } |
| 170 | } |
| 171 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 172 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 173 | 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] | 174 | { |
| 175 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 176 | struct yin_arg_record *argument_record = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 177 | struct sized_string prefix, name; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 178 | |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 179 | /* load all attributes */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 180 | while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) { |
| 181 | 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] | 182 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 183 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 184 | if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) { |
| 185 | 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] | 186 | argument_record->name = name.value; |
| 187 | argument_record->name_len = name.len; |
| 188 | argument_record->prefix = prefix.value; |
| 189 | argument_record->prefix_len = prefix.len; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 190 | 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] | 191 | &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 192 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 196 | cleanup: |
| 197 | if (ret != LY_SUCCESS) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 198 | FREE_ARRAY(ctx, *attrs, free_arg_rec); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 199 | *attrs = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 200 | } |
| 201 | return ret; |
| 202 | } |
| 203 | |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 204 | LY_ERR |
| 205 | yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len) |
| 206 | { |
| 207 | int prefix = 0; |
| 208 | unsigned int c; |
| 209 | size_t utf8_char_len; |
| 210 | size_t already_read = 0; |
| 211 | while (already_read < len) { |
| 212 | LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len), |
| 213 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID); |
| 214 | already_read += utf8_char_len; |
| 215 | LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT); |
| 216 | |
| 217 | switch (val_type) { |
| 218 | case Y_IDENTIF_ARG: |
| 219 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL)); |
| 220 | break; |
| 221 | case Y_PREF_IDENTIF_ARG: |
| 222 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix)); |
| 223 | break; |
| 224 | case Y_STR_ARG: |
| 225 | case Y_MAYBE_STR_ARG: |
| 226 | LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c)); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return LY_SUCCESS; |
| 232 | } |
| 233 | |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 234 | /** |
| 235 | * @brief Parse yin argument. |
| 236 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 237 | * @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] | 238 | * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 239 | * @param[in,out] data Data to read from. |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 240 | * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without |
| 241 | * special argument). |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 242 | * @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] | 243 | * @param[in] val_type Type of expected value of attribute. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 244 | * @param[in] current_element Identification of current element, used for logging. |
| 245 | * |
| 246 | * @return LY_ERR values. |
| 247 | */ |
| 248 | static LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 249 | 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] | 250 | 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] | 251 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 252 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
| 253 | struct yin_arg_record *iter = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 254 | bool found = false; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 255 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 256 | /* validation of attributes */ |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 257 | LY_ARRAY_FOR(*attrs, struct yin_arg_record, iter) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 258 | /* yin arguments represented as attributes have no namespace, which in this case means no prefix */ |
| 259 | if (!iter->prefix) { |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 260 | arg = yin_match_argument_name(iter->name, iter->name_len); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 261 | if (arg == YIN_ARG_NONE) { |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 262 | continue; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 263 | } else if (arg == arg_type) { |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 264 | LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Duplicit definition of %s attribute in %s element", |
| 265 | yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 266 | found = true; |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 267 | 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] | 268 | if (iter->dynamic_content) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 269 | *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 270 | LY_CHECK_RET(!(*arg_val), LY_EMEM); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 271 | /* string is no longer supposed to be freed when the sized array is freed */ |
| 272 | iter->dynamic_content = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 273 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 274 | *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 275 | LY_CHECK_RET(!(*arg_val), LY_EMEM); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 276 | } |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 277 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 278 | 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] | 279 | return LY_EVALID; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 284 | /* anything else than Y_MAYBE_STR_ARG is mandatory */ |
| 285 | if (val_type != Y_MAYBE_STR_ARG && !found) { |
David Sedlák | 9c40a92 | 2019-07-08 17:04:43 +0200 | [diff] [blame] | 286 | 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] | 287 | return LY_EVALID; |
| 288 | } |
| 289 | |
| 290 | return LY_SUCCESS; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 291 | } |
| 292 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 293 | /** |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 294 | * @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] | 295 | * |
| 296 | * @param[in] type Type of wanted record. |
| 297 | * @param[in] array_size Size of array. |
| 298 | * @param[in] array Searched array. |
| 299 | * |
| 300 | * @return Pointer to desired record on success, NULL if element is not in the array. |
| 301 | */ |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 302 | static struct yin_subelement * |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 303 | 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] | 304 | { |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 305 | signed char left = 0, right = array_size - 1, middle; |
| 306 | |
| 307 | while (left <= right) { |
| 308 | middle = left + (right - left) / 2; |
| 309 | |
| 310 | if (array[middle].type == type) { |
| 311 | return &array[middle]; |
| 312 | } |
| 313 | |
| 314 | if (array[middle].type < type) { |
| 315 | left = middle + 1; |
| 316 | } else { |
| 317 | right = middle - 1; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | return NULL; |
| 322 | } |
| 323 | |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 324 | /** |
| 325 | * @brief Helper function to check mandatory constraint of subelement. |
| 326 | * |
| 327 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
| 328 | * @param[in] subelem_info Array of information about subelements. |
| 329 | * @param[in] subelem_info_size Size of subelem_info array. |
| 330 | * @param[in] current_element Identification of element that is currently being parsed, used for logging. |
| 331 | * |
| 332 | * @return LY_ERR values. |
| 333 | */ |
| 334 | static LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 335 | 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] | 336 | signed char subelem_info_size, enum yang_keyword current_element) |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 337 | { |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 338 | for (signed char i = 0; i < subelem_info_size; ++i) { |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 339 | /* if there is element that is mandatory and isn't parsed log error and rturn LY_EVALID */ |
| 340 | 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] | 341 | 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] | 342 | ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element)); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 343 | return LY_EVALID; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return LY_SUCCESS; |
| 348 | } |
| 349 | |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 350 | /** |
| 351 | * @brief Helper function to check "first" constraint of subelement. |
| 352 | * |
| 353 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
| 354 | * @param[in] subelem_info Array of information about subelements. |
| 355 | * @param[in] subelem_info_size Size of subelem_info array. |
| 356 | * @param[in] current_element Identification of element that is currently being parsed, used for logging. |
| 357 | * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement. |
| 358 | * |
| 359 | * @return LY_ERR values. |
| 360 | */ |
| 361 | static LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 362 | 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] | 363 | signed char subelem_info_size, enum yang_keyword current_element, |
| 364 | struct yin_subelement *exp_first) |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 365 | { |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 366 | for (signed char i = 0; i < subelem_info_size; ++i) { |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 367 | if (subelem_info[i].flags & YIN_SUBELEM_PARSED) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 368 | 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] | 369 | ly_stmt2str(exp_first->type), ly_stmt2str(current_element)); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 370 | return LY_EVALID; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return LY_SUCCESS; |
| 375 | } |
| 376 | |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 377 | /* TODO add something like ifdef NDEBUG around this function, this is supposed to be checked only in debug mode */ |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 378 | /** |
| 379 | * @brief Helper function to check if array of information about subelements is in ascending order. |
| 380 | * |
| 381 | * @param[in] subelem_info Array of information about subelements. |
| 382 | * @param[in] subelem_info_size Size of subelem_info array. |
| 383 | * |
| 384 | * @return True iff subelem_info array is in ascending order, False otherwise. |
| 385 | */ |
| 386 | static bool |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 387 | is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size) |
| 388 | { |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 389 | enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */ |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 390 | |
| 391 | for (signed char i = 0; i < subelem_info_size; ++i) { |
| 392 | if (subelem_info[i].type <= current) { |
| 393 | return false; |
| 394 | } |
| 395 | current = subelem_info[i].type; |
| 396 | } |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 401 | /** |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 402 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute, |
| 403 | * for example prefix or namespace element. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 404 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 405 | * @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] | 406 | * @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] | 407 | * @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] | 408 | * @param[in] kw Type of current element. |
| 409 | * @param[out] value Where value of attribute should be stored. |
| 410 | * @param[in] arg_type Expected type of attribute. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 411 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 412 | * @param[in,out] exts Extension instance to add to. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 413 | * |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 414 | * @return LY_ERR values. |
| 415 | */ |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 416 | static LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 417 | 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] | 418 | 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] | 419 | { |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 420 | LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw)); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 421 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
| 422 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 423 | return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 427 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have |
| 428 | * more instances, such as base or if-feature. |
| 429 | * |
| 430 | * @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] | 431 | * @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] | 432 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 433 | * @param[in] kw Type of current element. |
| 434 | * @param[out] values Parsed values to add to. |
| 435 | * @param[in] arg_type Expected type of attribute. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 436 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 437 | * @param[in,out] exts Extension instance to add to. |
| 438 | * |
| 439 | * @return LY_ERR values. |
| 440 | */ |
| 441 | static LY_ERR |
| 442 | 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] | 443 | 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] | 444 | { |
| 445 | const char **value; |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 446 | 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] | 447 | uint32_t index = LY_ARRAY_SIZE(*values) - 1; |
| 448 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, &index, 0}}; |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 449 | 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] | 450 | |
| 451 | return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts); |
| 452 | } |
| 453 | |
| 454 | /** |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 455 | * @brief Function to parse meta tags (description, contact, ...) eg. elements with |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 456 | * text element as child |
| 457 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 458 | * @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] | 459 | * @param[in,out] data Data to read from. |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 460 | * @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] | 461 | * @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] | 462 | * @param[in,out] exts Extension instance to add to. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 463 | * |
| 464 | * @return LY_ERR values. |
| 465 | */ |
| 466 | static LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 467 | 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] | 468 | const char **value, struct lysp_ext_instance **exts) |
| 469 | { |
| 470 | assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE); |
| 471 | |
| 472 | struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0}, |
| 473 | {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}}; |
| 474 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 475 | 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] | 476 | } |
| 477 | |
| 478 | /** |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 479 | * @brief Parse error-message element. |
| 480 | * |
| 481 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
| 482 | * @param[in,out] data Data to read from. |
| 483 | * @param[out] value Where the content of error-message element should be stored. |
| 484 | * @param[in,out] exts Extension instance to add to. |
| 485 | * |
| 486 | * @return LY_ERR values. |
| 487 | */ |
| 488 | static LY_ERR |
| 489 | yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value, |
| 490 | struct lysp_ext_instance **exts) |
| 491 | { |
| 492 | struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0}, |
| 493 | {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}}; |
| 494 | |
| 495 | return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts); |
| 496 | } |
| 497 | |
| 498 | /** |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 499 | * @brief Map keyword type to substatement info. |
| 500 | * |
| 501 | * @param[in] kw Keyword type. |
| 502 | * |
| 503 | * @return correct LYEXT_SUBSTMT information. |
| 504 | */ |
| 505 | static LYEXT_SUBSTMT |
| 506 | kw2lyext_substmt(enum yang_keyword kw) |
| 507 | { |
| 508 | switch (kw) { |
| 509 | case YANG_ARGUMENT: |
| 510 | return LYEXT_SUBSTMT_ARGUMENT; |
| 511 | case YANG_BASE: |
| 512 | return LYEXT_SUBSTMT_BASE; |
| 513 | case YANG_BELONGS_TO: |
| 514 | return LYEXT_SUBSTMT_BELONGSTO; |
| 515 | case YANG_CONTACT: |
| 516 | return LYEXT_SUBSTMT_CONTACT; |
| 517 | case YANG_DEFAULT: |
| 518 | return LYEXT_SUBSTMT_DEFAULT; |
| 519 | case YANG_DESCRIPTION: |
| 520 | return LYEXT_SUBSTMT_DESCRIPTION; |
| 521 | case YANG_ERROR_APP_TAG: |
| 522 | return LYEXT_SUBSTMT_ERRTAG; |
| 523 | case YANG_ERROR_MESSAGE: |
| 524 | return LYEXT_SUBSTMT_ERRMSG; |
| 525 | case YANG_KEY: |
| 526 | return LYEXT_SUBSTMT_KEY; |
| 527 | case YANG_NAMESPACE: |
| 528 | return LYEXT_SUBSTMT_NAMESPACE; |
| 529 | case YANG_ORGANIZATION: |
| 530 | return LYEXT_SUBSTMT_ORGANIZATION; |
| 531 | case YANG_PATH: |
| 532 | return LYEXT_SUBSTMT_PATH; |
| 533 | case YANG_PREFIX: |
| 534 | return LYEXT_SUBSTMT_PREFIX; |
| 535 | case YANG_PRESENCE: |
| 536 | return LYEXT_SUBSTMT_PRESENCE; |
| 537 | case YANG_REFERENCE: |
| 538 | return LYEXT_SUBSTMT_REFERENCE; |
| 539 | case YANG_REVISION_DATE: |
| 540 | return LYEXT_SUBSTMT_REVISIONDATE; |
| 541 | case YANG_UNITS: |
| 542 | return LYEXT_SUBSTMT_UNITS; |
| 543 | case YANG_VALUE: |
| 544 | return LYEXT_SUBSTMT_VALUE; |
| 545 | case YANG_YANG_VERSION: |
| 546 | return LYEXT_SUBSTMT_VERSION; |
| 547 | case YANG_MODIFIER: |
| 548 | return LYEXT_SUBSTMT_MODIFIER; |
| 549 | case YANG_REQUIRE_INSTANCE: |
| 550 | return LYEXT_SUBSTMT_REQINSTANCE; |
| 551 | case YANG_YIN_ELEMENT: |
| 552 | return LYEXT_SUBSTMT_YINELEM; |
| 553 | case YANG_CONFIG: |
| 554 | return LYEXT_SUBSTMT_CONFIG; |
| 555 | case YANG_MANDATORY: |
| 556 | return LYEXT_SUBSTMT_MANDATORY; |
| 557 | case YANG_ORDERED_BY: |
| 558 | return LYEXT_SUBSTMT_ORDEREDBY; |
| 559 | case YANG_STATUS: |
| 560 | return LYEXT_SUBSTMT_STATUS; |
| 561 | case YANG_FRACTION_DIGITS: |
| 562 | return LYEXT_SUBSTMT_FRACDIGITS; |
| 563 | case YANG_MAX_ELEMENTS: |
| 564 | return LYEXT_SUBSTMT_MAX; |
| 565 | case YANG_MIN_ELEMENTS: |
| 566 | return LYEXT_SUBSTMT_MIN; |
| 567 | case YANG_POSITION: |
| 568 | return LYEXT_SUBSTMT_POSITION; |
| 569 | case YANG_UNIQUE: |
| 570 | return LYEXT_SUBSTMT_UNIQUE; |
| 571 | case YANG_IF_FEATURE: |
| 572 | return LYEXT_SUBSTMT_IFFEATURE; |
| 573 | default: |
| 574 | return LYEXT_SUBSTMT_SELF; |
| 575 | } |
| 576 | } |
| 577 | |
David Sedlák | 9c40a92 | 2019-07-08 17:04:43 +0200 | [diff] [blame] | 578 | |
| 579 | /** |
| 580 | * @brief Parse belongs-to element. |
| 581 | * |
| 582 | * @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] | 583 | * @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] | 584 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 585 | * @param[out] submod Structure of submodule that is being parsed. |
| 586 | * @param[in,out] exts Extension instances to add to. |
| 587 | * |
| 588 | * @return LY_ERR values |
| 589 | */ |
| 590 | static LY_ERR |
| 591 | yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 592 | struct lysp_submodule *submod, struct lysp_ext_instance **exts) |
| 593 | { |
| 594 | struct yin_subelement subelems[2] = {{YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 595 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 596 | 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] | 597 | |
| 598 | return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts); |
| 599 | } |
| 600 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 601 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 602 | 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] | 603 | 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] | 604 | { |
| 605 | LY_ERR ret = LY_SUCCESS; |
| 606 | struct sized_string prefix, name; |
| 607 | char *out; |
| 608 | size_t out_len; |
| 609 | int dynamic; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 610 | struct yin_arg_record *subelem_attrs = NULL; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 611 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 612 | struct yin_subelement *subelem_info_rec = NULL; |
David Sedlák | cb5d83f | 2019-07-09 09:32:53 +0200 | [diff] [blame] | 613 | uint32_t index = 0; |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 614 | struct lysp_type *type; |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 615 | assert(is_ordered(subelem_info, subelem_info_size)); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 616 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 617 | if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) { |
| 618 | 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] | 619 | /* current element has subelements as content */ |
| 620 | if (ret == LY_EINVAL) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 621 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
| 622 | 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] | 623 | LY_CHECK_GOTO(ret, cleanup); |
| 624 | if (!name.value) { |
| 625 | /* end of current element reached */ |
| 626 | break; |
| 627 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 628 | ret = yin_load_attributes(ctx, data, &subelem_attrs); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 629 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 630 | 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] | 631 | |
| 632 | /* check if this element can be child of current element */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 633 | subelem_info_rec = get_record(kw, subelem_info_size, subelem_info); |
| 634 | if (!subelem_info_rec) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 635 | 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] | 636 | ret = LY_EVALID; |
| 637 | goto cleanup; |
| 638 | } |
| 639 | |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 640 | /* TODO check relative order */ |
| 641 | |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 642 | /* if element is unique and already defined log error */ |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 643 | 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] | 644 | 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] | 645 | return LY_EVALID; |
| 646 | } |
| 647 | if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 648 | LY_CHECK_RET(yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec)); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 649 | } |
| 650 | subelem_info_rec->flags |= YIN_SUBELEM_PARSED; |
| 651 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 652 | switch (kw) { |
| 653 | case YANG_CUSTOM: |
David Sedlák | cb5d83f | 2019-07-09 09:32:53 +0200 | [diff] [blame] | 654 | index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 655 | 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] | 656 | namelen2fulllen(name.len, prefix.len), |
David Sedlák | cb5d83f | 2019-07-09 09:32:53 +0200 | [diff] [blame] | 657 | kw2lyext_substmt(current_element), index, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 658 | break; |
| 659 | case YANG_ACTION: |
| 660 | break; |
| 661 | case YANG_ANYDATA: |
| 662 | break; |
| 663 | case YANG_ANYXML: |
| 664 | break; |
| 665 | case YANG_ARGUMENT: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 666 | 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] | 667 | break; |
| 668 | case YANG_AUGMENT: |
| 669 | break; |
| 670 | case YANG_BASE: |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 671 | if (current_element == YANG_IDENTITY) { |
| 672 | type = (struct lysp_type *)subelem_info_rec->dest; |
| 673 | ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME, |
| 674 | Y_PREF_IDENTIF_ARG, exts); |
| 675 | type->flags |= LYS_SET_BASE; |
| 676 | } else if (current_element == YANG_TYPE) { |
| 677 | ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest, |
| 678 | YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts); |
| 679 | } else { |
| 680 | LOGINT(ctx->xml_ctx.ctx); |
| 681 | ret = LY_EINT; |
| 682 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 683 | break; |
| 684 | case YANG_BELONGS_TO: |
David Sedlák | 9c40a92 | 2019-07-08 17:04:43 +0200 | [diff] [blame] | 685 | 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] | 686 | break; |
| 687 | case YANG_BIT: |
| 688 | break; |
| 689 | case YANG_CASE: |
| 690 | break; |
| 691 | case YANG_CHOICE: |
| 692 | break; |
| 693 | case YANG_CONFIG: |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 694 | 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] | 695 | break; |
| 696 | case YANG_CONTACT: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 697 | case YANG_DESCRIPTION: |
| 698 | case YANG_ORGANIZATION: |
| 699 | case YANG_REFERENCE: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 700 | 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] | 701 | break; |
| 702 | case YANG_CONTAINER: |
| 703 | break; |
| 704 | case YANG_DEFAULT: |
| 705 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 706 | case YANG_DEVIATE: |
| 707 | break; |
| 708 | case YANG_DEVIATION: |
| 709 | break; |
| 710 | case YANG_ENUM: |
| 711 | break; |
| 712 | case YANG_ERROR_APP_TAG: |
| 713 | break; |
| 714 | case YANG_ERROR_MESSAGE: |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 715 | 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] | 716 | break; |
| 717 | case YANG_EXTENSION: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 718 | 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] | 719 | break; |
| 720 | case YANG_FEATURE: |
| 721 | break; |
| 722 | case YANG_FRACTION_DIGITS: |
| 723 | break; |
| 724 | case YANG_GROUPING: |
| 725 | break; |
| 726 | case YANG_IDENTITY: |
| 727 | break; |
| 728 | case YANG_IF_FEATURE: |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 729 | ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 730 | (const char ***)subelem_info_rec->dest, YIN_ARG_VALUE, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 731 | break; |
| 732 | case YANG_IMPORT: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 733 | 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] | 734 | break; |
| 735 | case YANG_INCLUDE: |
| 736 | break; |
| 737 | case YANG_INPUT: |
| 738 | break; |
| 739 | case YANG_KEY: |
| 740 | break; |
| 741 | case YANG_LEAF: |
| 742 | break; |
| 743 | case YANG_LEAF_LIST: |
| 744 | break; |
| 745 | case YANG_LENGTH: |
| 746 | break; |
| 747 | case YANG_LIST: |
| 748 | break; |
| 749 | case YANG_MANDATORY: |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 750 | 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] | 751 | break; |
| 752 | case YANG_MAX_ELEMENTS: |
| 753 | break; |
| 754 | case YANG_MIN_ELEMENTS: |
| 755 | break; |
| 756 | case YANG_MODIFIER: |
| 757 | break; |
| 758 | case YANG_MODULE: |
| 759 | break; |
| 760 | case YANG_MUST: |
| 761 | break; |
| 762 | case YANG_NAMESPACE: |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 763 | ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 764 | (const char **)subelem_info_rec->dest, YIN_ARG_URI, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 765 | break; |
| 766 | case YANG_NOTIFICATION: |
| 767 | break; |
| 768 | case YANG_ORDERED_BY: |
| 769 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 770 | case YANG_OUTPUT: |
| 771 | break; |
| 772 | case YANG_PATH: |
| 773 | break; |
| 774 | case YANG_PATTERN: |
| 775 | break; |
| 776 | case YANG_POSITION: |
| 777 | break; |
| 778 | case YANG_PREFIX: |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 779 | ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 780 | (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] | 781 | break; |
| 782 | case YANG_PRESENCE: |
| 783 | break; |
| 784 | case YANG_RANGE: |
| 785 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 786 | case YANG_REFINE: |
| 787 | break; |
| 788 | case YANG_REQUIRE_INSTANCE: |
| 789 | break; |
| 790 | case YANG_REVISION: |
| 791 | break; |
| 792 | case YANG_REVISION_DATE: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 793 | 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] | 794 | break; |
| 795 | case YANG_RPC: |
| 796 | break; |
| 797 | case YANG_STATUS: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 798 | 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] | 799 | break; |
| 800 | case YANG_SUBMODULE: |
| 801 | break; |
| 802 | case YANG_TYPE: |
| 803 | break; |
| 804 | case YANG_TYPEDEF: |
| 805 | break; |
| 806 | case YANG_UNIQUE: |
David Sedlák | d4909d9 | 2019-07-09 16:33:10 +0200 | [diff] [blame] | 807 | ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, |
| 808 | (const char ***)subelem_info_rec->dest, YIN_ARG_TAG, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 809 | break; |
| 810 | case YANG_UNITS: |
| 811 | break; |
| 812 | case YANG_USES: |
| 813 | break; |
| 814 | case YANG_VALUE: |
| 815 | break; |
| 816 | case YANG_WHEN: |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 817 | 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] | 818 | break; |
| 819 | case YANG_YANG_VERSION: |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 820 | 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] | 821 | break; |
| 822 | case YANG_YIN_ELEMENT: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 823 | 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] | 824 | break; |
| 825 | case YIN_TEXT: |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 826 | case YIN_VALUE: |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 827 | 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] | 828 | break; |
| 829 | default: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 830 | LOGINT(ctx->xml_ctx.ctx); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 831 | return LY_EINT; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 832 | } |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 833 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 834 | FREE_ARRAY(ctx, subelem_attrs, free_arg_rec); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 835 | subelem_attrs = NULL; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 836 | subelem_info_rec = NULL; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 837 | } |
| 838 | } else { |
| 839 | /* elements with text or none content */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 840 | /* save text content, if text_content isn't set, it's just ignored */ |
| 841 | if (text_content) { |
| 842 | if (dynamic) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 843 | *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 844 | if (!*text_content) { |
| 845 | free(out); |
| 846 | return LY_EMEM; |
| 847 | } |
| 848 | } else { |
| 849 | if (out_len == 0) { |
| 850 | *text_content = NULL; |
| 851 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 852 | *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 856 | /* load closing element */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 857 | 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] | 858 | } |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 859 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 860 | 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] | 861 | } |
| 862 | |
| 863 | cleanup: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 864 | FREE_ARRAY(ctx, subelem_attrs, free_arg_rec); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 865 | return ret; |
| 866 | } |
| 867 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 868 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 869 | 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] | 870 | struct lysp_ext_instance **exts) |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 871 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 872 | const char *temp_rev; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 873 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 874 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 875 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, YANG_REVISION_DATE)); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 876 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 877 | |
| 878 | strcpy(rev, temp_rev); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 879 | FREE_STRING(ctx->xml_ctx.ctx, temp_rev); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 880 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 881 | 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] | 882 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 883 | |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 884 | /** |
| 885 | * @brief Parse config element. |
| 886 | * |
| 887 | * @param[in] ctx Yin parser context for logging and to store current state. |
| 888 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element. |
| 889 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 890 | * @param[in,out] flags Flags to add to. |
| 891 | * @param[in,out] exts Extension instances to add to. |
| 892 | * |
| 893 | * @return LY_ERR values. |
| 894 | */ |
| 895 | static LY_ERR |
| 896 | yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags, |
| 897 | struct lysp_ext_instance **exts) |
| 898 | { |
| 899 | const char *temp_val = NULL; |
| 900 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
| 901 | |
| 902 | LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG)); |
| 903 | if (strcmp(temp_val, "true") == 0) { |
| 904 | *flags |= LYS_CONFIG_W; |
| 905 | } else if (strcmp(temp_val, "false") == 0) { |
| 906 | *flags |= LYS_CONFIG_R; |
| 907 | } else { |
| 908 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config"); |
| 909 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 910 | return LY_EVALID; |
| 911 | } |
| 912 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 913 | |
| 914 | return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts); |
| 915 | } |
| 916 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 917 | LY_ERR |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 918 | yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version, |
| 919 | struct lysp_ext_instance **exts) |
| 920 | { |
| 921 | const char *temp_version = NULL; |
| 922 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
| 923 | |
| 924 | LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION)); |
| 925 | if (strcmp(temp_version, "1.0") == 0) { |
| 926 | *version = LYS_VERSION_1_0; |
| 927 | } else if (strcmp(temp_version, "1.1") == 0) { |
| 928 | *version = LYS_VERSION_1_1; |
| 929 | } else { |
| 930 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version"); |
| 931 | FREE_STRING(ctx->xml_ctx.ctx, temp_version); |
| 932 | return LY_EVALID; |
| 933 | } |
| 934 | FREE_STRING(ctx->xml_ctx.ctx, temp_version); |
| 935 | ctx->mod_version = *version; |
| 936 | |
| 937 | return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts); |
| 938 | } |
| 939 | |
| 940 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 941 | 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] | 942 | { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 943 | struct lysp_import *imp; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 944 | /* allocate new element in sized array for import */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 945 | 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] | 946 | |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 947 | struct yin_subelement subelems[5] = {{YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE}, |
| 948 | {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 949 | {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE}, |
| 950 | {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 951 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 952 | |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 953 | /* parse import attributes */ |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 954 | 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] | 955 | 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] | 956 | /* check prefix validity */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 957 | 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] | 958 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 959 | 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] | 960 | } |
| 961 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 962 | LY_ERR |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 963 | yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags, |
| 964 | struct lysp_ext_instance **exts) |
| 965 | { |
| 966 | const char *temp_val = NULL; |
| 967 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
| 968 | |
| 969 | LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY)); |
| 970 | if (strcmp(temp_val, "true") == 0) { |
| 971 | *flags |= LYS_MAND_TRUE; |
| 972 | } else if (strcmp(temp_val, "false") == 0) { |
| 973 | *flags |= LYS_MAND_FALSE; |
| 974 | } else { |
| 975 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory"); |
| 976 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 977 | return LY_EVALID; |
| 978 | } |
| 979 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 980 | |
| 981 | return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts); |
| 982 | } |
| 983 | |
| 984 | LY_ERR |
| 985 | yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags, |
| 986 | struct lysp_ext_instance **exts) |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 987 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 988 | const char *value = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 989 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 990 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 991 | 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] | 992 | if (strcmp(value, "current") == 0) { |
| 993 | *flags |= LYS_STATUS_CURR; |
| 994 | } else if (strcmp(value, "deprecated") == 0) { |
| 995 | *flags |= LYS_STATUS_DEPRC; |
| 996 | } else if (strcmp(value, "obsolete") == 0) { |
| 997 | *flags |= LYS_STATUS_OBSLT; |
| 998 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 999 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status"); |
| 1000 | FREE_STRING(ctx->xml_ctx.ctx, value); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1001 | return LY_EVALID; |
| 1002 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1003 | FREE_STRING(ctx->xml_ctx.ctx, value); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1004 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1005 | 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] | 1006 | } |
| 1007 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1008 | LY_ERR |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 1009 | yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p) |
| 1010 | { |
| 1011 | struct lysp_when *when; |
| 1012 | when = calloc(1, sizeof *when); |
| 1013 | LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
| 1014 | yin_parse_attribute(ctx, &attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN); |
| 1015 | *when_p = when; |
| 1016 | struct yin_subelement subelems[3] = {{YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE}, |
| 1017 | {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE}, |
| 1018 | {YANG_CUSTOM, NULL, 0}}; |
| 1019 | |
| 1020 | return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts); |
| 1021 | } |
| 1022 | |
| 1023 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1024 | 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] | 1025 | uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 1026 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1027 | const char *temp_val = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 1028 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 1029 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1030 | 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] | 1031 | if (strcmp(temp_val, "true") == 0) { |
| 1032 | *flags |= LYS_YINELEM_TRUE; |
| 1033 | } else if (strcmp(temp_val, "false") == 0) { |
| 1034 | *flags |= LYS_YINELEM_FALSE; |
| 1035 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1036 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element"); |
| 1037 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 1038 | return LY_EVALID; |
| 1039 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1040 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 1041 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1042 | 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] | 1043 | } |
| 1044 | |
| 1045 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1046 | 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] | 1047 | 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] | 1048 | { |
| 1049 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1050 | char *out; |
| 1051 | const char *name, *prefix; |
| 1052 | size_t out_len, prefix_len, name_len; |
| 1053 | int dynamic; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1054 | struct lysp_ext_instance *e; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1055 | struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL; |
| 1056 | struct yin_arg_record *iter; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1057 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1058 | 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] | 1059 | |
| 1060 | e->yin = 0; |
| 1061 | /* store name and insubstmt info */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1062 | 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] | 1063 | e->insubstmt = subelem; |
| 1064 | e->insubstmt_index = subelem_index; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1065 | e->yin |= LYS_YIN; |
| 1066 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1067 | /* store attributes as subelements */ |
| 1068 | LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) { |
| 1069 | if (!iter->prefix) { |
| 1070 | new_subelem = calloc(1, sizeof(*new_subelem)); |
| 1071 | if (!e->child) { |
| 1072 | e->child = new_subelem; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1073 | } else { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1074 | last_subelem->next = new_subelem; |
| 1075 | } |
| 1076 | last_subelem = new_subelem; |
| 1077 | |
| 1078 | last_subelem->flags |= LYS_YIN_ATTR; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1079 | last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len); |
| 1080 | 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] | 1081 | if (iter->dynamic_content) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1082 | last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content); |
| 1083 | 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] | 1084 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1085 | last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len); |
| 1086 | 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] | 1087 | } |
| 1088 | } |
| 1089 | } |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1090 | |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 1091 | /* parse subelements */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1092 | if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) { |
| 1093 | 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] | 1094 | if (ret == LY_EINVAL) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1095 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
| 1096 | 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] | 1097 | if (!name) { |
| 1098 | /* end of extension instance reached */ |
| 1099 | break; |
| 1100 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1101 | 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] | 1102 | if (!e->child) { |
| 1103 | e->child = new_subelem; |
| 1104 | } else { |
| 1105 | last_subelem->next = new_subelem; |
| 1106 | } |
| 1107 | last_subelem = new_subelem; |
| 1108 | } |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 1109 | } else { |
| 1110 | /* save text content */ |
| 1111 | if (dynamic) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1112 | e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 1113 | if (!e->argument) { |
| 1114 | free(out); |
| 1115 | return LY_EMEM; |
| 1116 | } |
| 1117 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1118 | e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 1119 | LY_CHECK_RET(!e->argument, LY_EMEM); |
| 1120 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1121 | 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] | 1122 | LY_CHECK_RET(name, LY_EINT); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1123 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | return LY_SUCCESS; |
| 1127 | } |
| 1128 | |
| 1129 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1130 | 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] | 1131 | size_t prefix_len, const char **data, struct lysp_stmt **element) |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1132 | { |
| 1133 | LY_ERR ret = LY_SUCCESS; |
| 1134 | const char *temp_prefix, *temp_name; |
| 1135 | char *out = NULL; |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 1136 | size_t out_len, temp_name_len, temp_prefix_len; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1137 | int dynamic; |
| 1138 | struct yin_arg_record *subelem_args = NULL; |
| 1139 | struct lysp_stmt *last = NULL, *new = NULL; |
| 1140 | |
| 1141 | /* allocate new structure for element */ |
| 1142 | *element = calloc(1, sizeof(**element)); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1143 | (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len); |
| 1144 | 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] | 1145 | |
| 1146 | last = (*element)->child; |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 1147 | /* load attributes */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1148 | while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1149 | /* add new element to linked-list */ |
| 1150 | new = calloc(1, sizeof(*last)); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1151 | LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1152 | if (!(*element)->child) { |
| 1153 | /* save first */ |
| 1154 | (*element)->child = new; |
| 1155 | } else { |
| 1156 | last->next = new; |
| 1157 | } |
| 1158 | last = new; |
| 1159 | |
| 1160 | last->flags |= LYS_YIN_ATTR; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1161 | 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] | 1162 | LY_CHECK_GOTO(ret, err); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1163 | 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] | 1164 | LY_CHECK_GOTO(ret, err); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1165 | last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len); |
| 1166 | 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] | 1167 | /* attributes with prefix are ignored */ |
| 1168 | if (!temp_prefix) { |
| 1169 | if (dynamic) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1170 | last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1171 | if (!last->arg) { |
| 1172 | free(out); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1173 | LOGMEM(ctx->xml_ctx.ctx); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1174 | ret = LY_EMEM; |
| 1175 | goto err; |
| 1176 | } |
| 1177 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1178 | last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len); |
| 1179 | 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] | 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* parse content of element */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1185 | 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] | 1186 | if (ret == LY_EINVAL) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1187 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1188 | /* parse subelements */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1189 | 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] | 1190 | LY_CHECK_GOTO(ret, err); |
| 1191 | if (!name) { |
| 1192 | /* end of element reached */ |
| 1193 | break; |
| 1194 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1195 | 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] | 1196 | LY_CHECK_GOTO(ret, err); |
| 1197 | last = last->next; |
| 1198 | } |
| 1199 | } else { |
| 1200 | /* save element content */ |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 1201 | if (out_len != 0) { |
| 1202 | if (dynamic) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1203 | (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 1204 | if (!(*element)->arg) { |
| 1205 | free(out); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1206 | LOGMEM(ctx->xml_ctx.ctx); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 1207 | ret = LY_EMEM; |
| 1208 | goto err; |
| 1209 | } |
| 1210 | } else { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1211 | (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len); |
| 1212 | 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] | 1213 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1214 | } |
| 1215 | /* read closing tag */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1216 | 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] | 1217 | LY_CHECK_GOTO(ret, err); |
| 1218 | } |
| 1219 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1220 | FREE_ARRAY(ctx, subelem_args, free_arg_rec); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1221 | return LY_SUCCESS; |
| 1222 | |
| 1223 | err: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1224 | FREE_ARRAY(ctx, subelem_args, free_arg_rec); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 1225 | return ret; |
| 1226 | } |
| 1227 | |
| 1228 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1229 | 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] | 1230 | struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts) |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 1231 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1232 | struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 1233 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 1234 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1235 | 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] | 1236 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1237 | 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] | 1238 | } |
| 1239 | |
| 1240 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1241 | 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] | 1242 | { |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1243 | struct lysp_ext *ex; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1244 | 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] | 1245 | 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] | 1246 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 1247 | struct yin_argument_meta arg_info = {&ex->flags, &ex->argument}; |
| 1248 | struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE}, |
| 1249 | {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE}, |
| 1250 | {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE}, |
| 1251 | {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 1252 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 1253 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1254 | 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] | 1255 | } |
| 1256 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1257 | /** |
| 1258 | * @brief Parse module substatements. |
| 1259 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1260 | * @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] | 1261 | * @param[in] mod_attrs Attributes of module element. |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1262 | * @param[in,out] data Data to read from. |
| 1263 | * @param[out] mod Parsed module structure. |
| 1264 | * |
| 1265 | * @return LY_ERR values. |
| 1266 | */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1267 | static LY_ERR |
| 1268 | 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] | 1269 | { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1270 | struct yin_subelement subelems[9] = {{YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE}, |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 1271 | {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE}, |
| 1272 | {YANG_EXTENSION, &(*mod)->exts, 0}, |
| 1273 | {YANG_IMPORT, *mod, 0}, |
| 1274 | {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 1275 | {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 1276 | {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 1277 | {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 1278 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1279 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 1280 | 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] | 1281 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1282 | 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] | 1283 | } |
| 1284 | |
| 1285 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1286 | 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] | 1287 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1288 | LY_ERR ret = LY_SUCCESS; |
| 1289 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1290 | struct lysp_module *mod_p = NULL; |
| 1291 | const char *prefix, *name; |
| 1292 | size_t prefix_len, name_len; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1293 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1294 | struct yin_arg_record *args = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1295 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1296 | struct yin_parser_ctx yin_ctx; |
| 1297 | |
| 1298 | /* initialize context */ |
| 1299 | memset(&yin_ctx, 0, sizeof yin_ctx); |
| 1300 | yin_ctx.xml_ctx.ctx = ctx; |
| 1301 | yin_ctx.xml_ctx.line = 1; |
| 1302 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1303 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1304 | /* check submodule */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1305 | 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] | 1306 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1307 | ret = yin_load_attributes(&yin_ctx, &data, &args); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1308 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame^] | 1309 | 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] | 1310 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1311 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 1312 | ret = LY_EINVAL; |
| 1313 | goto cleanup; |
| 1314 | } else if (kw != YANG_MODULE) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1315 | 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] | 1316 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1317 | ret = LY_EVALID; |
| 1318 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1321 | /* allocate module */ |
| 1322 | mod_p = calloc(1, sizeof *mod_p); |
| 1323 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 1324 | mod_p->mod = mod; |
| 1325 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1326 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1327 | /* parse module substatements */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1328 | ret = yin_parse_mod(&yin_ctx, &args, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1329 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 1330 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1331 | mod_p->parsing = 0; |
| 1332 | mod->parsed = mod_p; |
| 1333 | |
| 1334 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1335 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1336 | lysp_module_free(mod_p); |
| 1337 | } |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1338 | FREE_ARRAY(&yin_ctx, args, free_arg_rec); |
| 1339 | lyxml_context_clear(&yin_ctx.xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 1340 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1341 | } |