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