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