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 | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 20 | |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 21 | #include "context.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 22 | #include "dict.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 23 | #include "xml.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 24 | #include "tree.h" |
| 25 | #include "tree_schema.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 26 | #include "tree_schema_internal.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 27 | #include "parser_yin.h" |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 28 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 29 | /** |
| 30 | * @brief check if given string is URI of yin namespace. |
| 31 | * @param ns Namespace URI to check. |
| 32 | * |
| 33 | * @return true if ns equals YIN_NS_URI false otherwise. |
| 34 | */ |
| 35 | #define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0) |
| 36 | |
David Sedlák | 9bc6587 | 2019-06-24 20:21:08 +0200 | [diff] [blame] | 37 | /** |
| 38 | * @brief loop over all subelements, automatically skip useles data and |
| 39 | * unify inconsistencies in xml parser status when loading full vs self closing tags. |
| 40 | * Use YIN_READ_SUBELEMS_END at the end. |
| 41 | * |
| 42 | * @param[in,out] CTX Xml parser context. |
| 43 | * @param[in,out] DATA Data to read from, always moved to currently handeled character. |
| 44 | * @param[in,out] RET Name of variable to store return values. |
| 45 | * @param[in,out] CLEANUP_LABEL Goto label that will be used in case of error. |
| 46 | * @param[in,out] KW Name of variable to store type of current subelement. |
| 47 | * @param[in] TMP Name of variable to store temporary values (type yin_arg_record). |
| 48 | * @param[in] SUBELEM_ARGS Name of variable to store array of subelements. |
| 49 | */ |
| 50 | #define YIN_READ_SUBELEMS_START(CTX, DATA, RET, CLEANUP_LABEL, KW, TMP, SUBELEM_ARGS)\ |
| 51 | if (xml_ctx->status == LYXML_ELEM_CONTENT) {\ |
| 52 | ret = lyxml_get_string(CTX, DATA, &TMP.content, &TMP.content_len, &TMP.content, &TMP.content_len, &TMP.dynamic_content);\ |
| 53 | /* unknown element text content is ignored */\ |
| 54 | if (ret == LY_EINVAL) {\ |
| 55 | while (CTX->status == LYXML_ELEMENT) {\ |
| 56 | RET = lyxml_get_element(CTX, DATA, &TMP.prefix, &TMP.prefix_len, &TMP.name, &TMP.name_len);\ |
| 57 | LY_CHECK_GOTO(RET, CLEANUP_LABEL);\ |
| 58 | RET = yin_load_attributes(CTX, DATA, &SUBELEM_ARGS);\ |
| 59 | LY_CHECK_GOTO(RET, CLEANUP_LABEL);\ |
| 60 | KW = yin_match_keyword(CTX, TMP.name, TMP.name_len, TMP.prefix, TMP.prefix_len);\ |
| 61 | if (!TMP.name) {\ |
| 62 | /* end of yin-element element reached */\ |
| 63 | break;\ |
| 64 | } |
| 65 | /** |
| 66 | * Closing part of macro YIN_READ_SUBELEM_START |
| 67 | * |
| 68 | * @param[in] CTX Xml parser context. |
| 69 | * @param[in,out] DATA Data to read from, always moved to currently handeled character. |
| 70 | * @param[in,out] TMP Name of variable to store temporary values (type yin_arg_record). |
| 71 | */ |
| 72 | #define YIN_READ_SUBELEMS_END(CTX, DATA, TMP)\ |
| 73 | }\ |
| 74 | } else {\ |
| 75 | /* load closing element */\ |
| 76 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));\ |
| 77 | LY_CHECK_RET(name, LY_EINVAL);\ |
| 78 | }\ |
| 79 | } |
| 80 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 81 | const char *const yin_attr_list[] = { |
| 82 | [YIN_ARG_NAME] = "name", |
| 83 | [YIN_ARG_TARGET_NODE] = "target-node", |
| 84 | [YIN_ARG_MODULE] = "module", |
| 85 | [YIN_ARG_VALUE] = "value", |
| 86 | [YIN_ARG_TEXT] = "text", |
| 87 | [YIN_ARG_CONDITION] = "condition", |
| 88 | [YIN_ARG_URI] = "uri", |
| 89 | [YIN_ARG_DATE] = "date", |
| 90 | [YIN_ARG_TAG] = "tag", |
| 91 | [YIN_ARG_XMLNS] = "xmlns", |
| 92 | }; |
| 93 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 94 | enum yang_keyword |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 95 | 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] | 96 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 97 | const char *start = NULL; |
| 98 | enum yang_keyword kw = YANG_NONE; |
| 99 | const struct lyxml_ns *ns = NULL; |
| 100 | |
| 101 | if (!name || name_len == 0) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 102 | return YANG_NONE; |
| 103 | } |
| 104 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 105 | ns = lyxml_ns_get(xml_ctx, prefix, prefix_len); |
| 106 | if (ns) { |
| 107 | if (!IS_YIN_NS(ns->uri)) { |
| 108 | return YANG_CUSTOM; |
| 109 | } |
| 110 | } else { |
| 111 | /* elements without namespace are automatically unknown */ |
| 112 | return YANG_NONE; |
| 113 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 114 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 115 | |
| 116 | start = name; |
| 117 | kw = lysp_match_kw(NULL, &name); |
| 118 | |
| 119 | if (name - start == (long int)name_len) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 120 | return kw; |
| 121 | } else { |
| 122 | return YANG_NONE; |
| 123 | } |
| 124 | } |
| 125 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 126 | enum YIN_ARGUMENT |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 127 | yin_match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 128 | { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 129 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 130 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 131 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 132 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 133 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 134 | #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] | 135 | #define IF_ARG_PREFIX_END } |
| 136 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 137 | switch (*name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 138 | case 'x': |
| 139 | already_read += 1; |
| 140 | IF_ARG("mlns", 4, YIN_ARG_XMLNS); |
| 141 | break; |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 142 | case 'c': |
| 143 | already_read += 1; |
| 144 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 145 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 146 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 147 | case 'd': |
| 148 | already_read += 1; |
| 149 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 150 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 151 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 152 | case 'm': |
| 153 | already_read += 1; |
| 154 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 155 | break; |
| 156 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 157 | case 'n': |
| 158 | already_read += 1; |
| 159 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 160 | break; |
| 161 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 162 | case 't': |
| 163 | already_read += 1; |
| 164 | IF_ARG_PREFIX("a", 1) |
| 165 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 166 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 167 | IF_ARG_PREFIX_END |
| 168 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 169 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 170 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 171 | case 'u': |
| 172 | already_read += 1; |
| 173 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 174 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 175 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 176 | case 'v': |
| 177 | already_read += 1; |
| 178 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 182 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 183 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 184 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 185 | } |
| 186 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 187 | #undef IF_ARG |
| 188 | #undef IF_ARG_PREFIX |
| 189 | #undef IF_ARG_PREFIX_END |
| 190 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 191 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 192 | } |
| 193 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 194 | static void free_arg_rec(struct lyxml_context *xml_ctx, struct yin_arg_record *record) { |
| 195 | (void)xml_ctx; /* unused */ |
| 196 | if (record->dynamic_content) { |
| 197 | free(record->content); |
| 198 | } |
| 199 | } |
| 200 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 201 | LY_ERR |
| 202 | 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] | 203 | { |
| 204 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 205 | struct yin_arg_record *argument_record = NULL; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 206 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 207 | /* load all attributes first */ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 208 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 209 | 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] | 210 | ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len, |
| 211 | &argument_record->name, &argument_record->name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 212 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 213 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 214 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 215 | argument_record->content = NULL; |
| 216 | argument_record->content_len = 0; |
| 217 | argument_record->dynamic_content = 0; |
| 218 | ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len, |
| 219 | &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 220 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 224 | cleanup: |
| 225 | if (ret != LY_SUCCESS) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 226 | FREE_ARRAY(xml_ctx, *args, free_arg_rec); |
| 227 | *args = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 228 | } |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | LY_ERR |
| 233 | yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type, const char **arg_val) |
| 234 | { |
| 235 | LY_ERR ret = LY_SUCCESS; |
| 236 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
| 237 | struct yin_arg_record *iter = NULL; |
| 238 | const struct lyxml_ns *ns = NULL; |
| 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 | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 242 | ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 243 | /* yin arguments represented as attributes have no namespace, which in this case means no prefix */ |
| 244 | if (!iter->prefix) { |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 245 | arg = yin_match_argument_name(iter->name, iter->name_len); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 246 | if (arg == YIN_ARG_NONE) { |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 247 | continue; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 248 | } else if (arg == arg_type) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 249 | if (iter->dynamic_content) { |
| 250 | *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 251 | LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup); |
| 252 | /* string is no longer supposed to be freed when the sized array is freed */ |
| 253 | iter->dynamic_content = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 254 | } else { |
| 255 | *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len); |
| 256 | LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup); |
| 257 | } |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 258 | } else { |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 259 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name); |
| 260 | ret = LY_EVALID; |
| 261 | goto cleanup; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 266 | cleanup: |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 267 | return ret; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 268 | } |
| 269 | |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 270 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 271 | 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] | 272 | { |
| 273 | LY_ERR ret = LY_SUCCESS; |
| 274 | char *buf = NULL, *out = NULL; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 275 | const char *prefix = NULL, *name = NULL; |
| 276 | 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] | 277 | int dynamic = 0; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 278 | |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 279 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 280 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 281 | LY_CHECK_RET(ret); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 282 | if (dynamic) { |
| 283 | *value = lydict_insert_zc(xml_ctx->ctx, buf); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 284 | LY_CHECK_RET(!(*value), LY_EMEM); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 285 | } else { |
| 286 | *value = lydict_insert(xml_ctx->ctx, out, out_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 287 | LY_CHECK_RET(!(*value), LY_EMEM); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 288 | } |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 289 | } |
| 290 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 291 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 292 | /* probably should never happen, mixed content should be caught by lyxml_get_element call but |
| 293 | * I will just leave it here for now to be sure */ |
| 294 | LY_CHECK_RET(name, LY_EVALID); |
| 295 | return ret; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 296 | } |
| 297 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 298 | /** |
| 299 | * @brief function to parse meta tags eg. elements with text element as child |
| 300 | * |
| 301 | * @param[in] xml_ctx Xml context. |
| 302 | * @param[in] args Sized array of arguments of current elements. |
| 303 | * @param[in,out] data Data to read from. |
| 304 | * @param[out] value Where the content of meta tag should be stored. |
| 305 | * |
| 306 | * @return LY_ERR values. |
| 307 | */ |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 308 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 309 | 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] | 310 | { |
| 311 | LY_ERR ret = LY_SUCCESS; |
| 312 | char *buf = NULL, *out = NULL; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 313 | const char *prefix = NULL, *name = NULL; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 314 | size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0; |
| 315 | int dynamic = 0; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 316 | enum YIN_ARGUMENT arg = YANG_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 317 | struct yin_arg_record *subelem_args = NULL; |
| 318 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 319 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 320 | 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] | 321 | |
| 322 | 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] | 323 | 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] | 324 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 325 | /* first element should be argument element <text> */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 326 | 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] | 327 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 328 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 329 | arg = yin_match_argument_name(name, name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 330 | LY_CHECK_ERR_GOTO(arg != YIN_ARG_TEXT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element."); |
| 331 | ret = LY_EINVAL, cleanup); |
| 332 | ret = yin_parse_text_element(xml_ctx, &subelem_args, data, value); |
| 333 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 334 | |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 335 | /* loop over all child elements and parse them */ |
| 336 | while (xml_ctx->status == LYXML_ELEMENT) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 337 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 338 | LY_CHECK_GOTO(ret, cleanup); |
| 339 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 340 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 341 | 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] | 342 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 343 | if (!name) { |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 344 | /* end of meta element reached */ |
| 345 | break; |
| 346 | } |
| 347 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 348 | switch (kw) { |
| 349 | case YANG_CUSTOM: |
| 350 | // TODO parse extension instance |
| 351 | break; |
| 352 | |
| 353 | default: |
| 354 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 355 | return LY_EVALID; |
| 356 | } |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 357 | } |
| 358 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 359 | cleanup: |
| 360 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 361 | return ret; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 362 | } |
| 363 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 364 | /** |
| 365 | * @brief Parse revision date. |
| 366 | * |
| 367 | * @param[in] xml_ctx Xml context. |
| 368 | * @param[in] args Sized array of arguments of current element. |
| 369 | * @param[in,out] data Data to read from. |
| 370 | * @param[in,out] rev Array to store the parsed value in. |
| 371 | * @param[in,out] exts Extension instances to add to. |
| 372 | * |
| 373 | * @return LY_ERR values. |
| 374 | */ |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 375 | static LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 376 | 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] | 377 | { |
| 378 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 379 | const char *temp_rev, *prefix = NULL, *name = NULL; |
| 380 | char *out; |
| 381 | struct yin_arg_record *subelem_args = NULL; |
| 382 | size_t prefix_len = 0, name_len = 0; |
| 383 | enum yang_keyword kw = YANG_NONE; |
| 384 | int dynamic; |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 385 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 386 | if (rev[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 387 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date"); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 388 | return LY_EVALID; |
| 389 | } |
| 390 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 391 | 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] | 392 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 393 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
| 394 | |
| 395 | strcpy(rev, temp_rev); |
| 396 | lydict_remove(xml_ctx->ctx, temp_rev); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 397 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 398 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 399 | ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic); |
| 400 | /* unknown element text content is ignored */ |
| 401 | if (ret == LY_EINVAL) { |
| 402 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 403 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 404 | LY_CHECK_GOTO(ret, cleanup); |
| 405 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 406 | LY_CHECK_GOTO(ret, cleanup); |
| 407 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
| 408 | |
| 409 | if (!name) { |
| 410 | /* end of revisin-date element reached */ |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | switch (kw) { |
| 415 | case YANG_CUSTOM: |
| 416 | // TODO parse extension instance |
| 417 | break; |
| 418 | |
| 419 | default: |
| 420 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 421 | return LY_EVALID; |
| 422 | } |
| 423 | } |
| 424 | } else { |
| 425 | /* load closing element */ |
| 426 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 427 | LY_CHECK_RET(name, LY_EINVAL); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | cleanup: |
| 432 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 433 | return ret; |
| 434 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 435 | |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 436 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 437 | 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] | 438 | { |
| 439 | LY_ERR ret = LY_SUCCESS; |
| 440 | enum yang_keyword kw; |
| 441 | struct lysp_import *imp; |
| 442 | const char *prefix, *name; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 443 | char *out; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 444 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 445 | size_t prefix_len, name_len; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 446 | int dynamic; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 447 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 448 | /* allocate new element in sized array for import */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 449 | 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] | 450 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 451 | /* parse import attributes */ |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 452 | LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name) != LY_SUCCESS, validation_err); |
| 453 | |
| 454 | ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic); |
| 455 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after import element"), LY_EINVAL); |
| 456 | |
| 457 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 458 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 459 | LY_CHECK_GOTO(ret, validation_err); |
| 460 | if (!name) { |
| 461 | /* end of import element reached */ |
| 462 | break; |
| 463 | } |
| 464 | 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] | 465 | 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] | 466 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 467 | switch (kw) { |
| 468 | case YANG_PREFIX: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 469 | LY_CHECK_ERR_GOTO(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), validation_err); |
| 470 | LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, &subelem_args, YIN_ARG_VALUE, &imp->prefix) != LY_SUCCESS, validation_err); |
| 471 | 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] | 472 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 473 | case YANG_DESCRIPTION: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 474 | 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] | 475 | 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] | 476 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 477 | case YANG_REFERENCE: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 478 | 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] | 479 | 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] | 480 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 481 | case YANG_REVISION_DATE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 482 | 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] | 483 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 484 | case YANG_CUSTOM: |
| 485 | /* TODO parse extension */ |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 486 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 487 | default: |
David Sedlák | 916b190 | 2019-06-06 11:35:05 +0200 | [diff] [blame] | 488 | 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] | 489 | goto validation_err; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 490 | } |
David Sedlák | 4af209e | 2019-06-21 14:24:50 +0200 | [diff] [blame] | 491 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 492 | subelem_args = NULL; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 493 | } |
| 494 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 495 | 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] | 496 | return ret; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 497 | |
| 498 | validation_err: |
David Sedlák | 4af209e | 2019-06-21 14:24:50 +0200 | [diff] [blame] | 499 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 500 | return LY_EVALID; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 501 | } |
| 502 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 503 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 504 | 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] | 505 | { |
| 506 | LY_ERR ret = LY_SUCCESS; |
| 507 | enum yang_keyword kw = YANG_NONE; |
| 508 | const char *value = NULL, *prefix = NULL, *name = NULL; |
| 509 | char *out; |
| 510 | size_t prefix_len = 0, name_len = 0, out_len = 0; |
| 511 | int dynamic = 0; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 512 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 513 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 514 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 515 | return LY_EVALID; |
| 516 | } |
| 517 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 518 | 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] | 519 | if (strcmp(value, "current") == 0) { |
| 520 | *flags |= LYS_STATUS_CURR; |
| 521 | } else if (strcmp(value, "deprecated") == 0) { |
| 522 | *flags |= LYS_STATUS_DEPRC; |
| 523 | } else if (strcmp(value, "obsolete") == 0) { |
| 524 | *flags |= LYS_STATUS_OBSLT; |
| 525 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 526 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 527 | lydict_remove(xml_ctx->ctx, value); |
| 528 | return LY_EVALID; |
| 529 | } |
| 530 | lydict_remove(xml_ctx->ctx, value); |
| 531 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 532 | /* TODO check if dynamic was set to 1 in case of error */ |
| 533 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 534 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 535 | /* if there are any xml subelements parse them, unknown text content is silently ignored */ |
| 536 | if (ret == LY_EINVAL) { |
| 537 | /* load subelements */ |
| 538 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 539 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 540 | if (!name) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 541 | /* end of status reached */ |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 542 | break; |
| 543 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 544 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 545 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 546 | 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] | 547 | switch (kw) { |
| 548 | case YANG_CUSTOM: |
| 549 | /* TODO parse extension instance */ |
| 550 | break; |
| 551 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 552 | 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] | 553 | ret = LY_EVALID; |
| 554 | goto cleanup; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 555 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 556 | |
| 557 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 558 | subelem_args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 559 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 560 | } else { |
| 561 | /* load closing element */ |
| 562 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 563 | LY_CHECK_RET(name, LY_EINVAL); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | return ret; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 568 | |
| 569 | cleanup: |
| 570 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 571 | return ret; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 572 | } |
| 573 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 574 | LY_ERR |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 575 | 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) |
| 576 | { |
| 577 | LY_ERR ret = LY_SUCCESS; |
| 578 | const char *temp_val = NULL, *prefix, *name; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 579 | size_t prefix_len, name_len; |
| 580 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 581 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 9bc6587 | 2019-06-24 20:21:08 +0200 | [diff] [blame] | 582 | struct yin_arg_record temp_record; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 583 | |
| 584 | if (*flags & LYS_YINELEM_MASK) { |
| 585 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "yin-element"); |
| 586 | return LY_EVALID; |
| 587 | } |
| 588 | |
| 589 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_VALUE, &temp_val)); |
| 590 | if (strcmp(temp_val, "true") == 0) { |
| 591 | *flags |= LYS_YINELEM_TRUE; |
| 592 | } else if (strcmp(temp_val, "false") == 0) { |
| 593 | *flags |= LYS_YINELEM_FALSE; |
| 594 | } else { |
| 595 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element"); |
| 596 | lydict_remove(xml_ctx->ctx, temp_val); |
| 597 | return LY_EVALID; |
| 598 | } |
| 599 | lydict_remove(xml_ctx->ctx, temp_val); |
| 600 | |
David Sedlák | 9bc6587 | 2019-06-24 20:21:08 +0200 | [diff] [blame] | 601 | YIN_READ_SUBELEMS_START(xml_ctx, data, ret, cleanup, kw, temp_record, subelem_args); |
| 602 | if (!name) { |
| 603 | /* end of yin-element element reached */ |
| 604 | break; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 605 | } |
David Sedlák | 9bc6587 | 2019-06-24 20:21:08 +0200 | [diff] [blame] | 606 | |
| 607 | switch (kw) { |
| 608 | case YANG_CUSTOM: |
| 609 | // TODO parse extension instance |
| 610 | break; |
| 611 | |
| 612 | default: |
| 613 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 614 | return LY_EVALID; |
| 615 | } |
| 616 | YIN_READ_SUBELEMS_END(xml_ctx, data, temp_record); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 617 | |
| 618 | cleanup: |
| 619 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | LY_ERR |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 624 | yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, |
| 625 | uint16_t *flags, const char **argument, struct lysp_ext **extensions) |
| 626 | { |
| 627 | LY_ERR ret = LY_SUCCESS; |
| 628 | const char *name, *prefix; |
| 629 | size_t name_len, prefix_len; |
| 630 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 631 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 9bc6587 | 2019-06-24 20:21:08 +0200 | [diff] [blame] | 632 | struct yin_arg_record temp_record; |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 633 | |
| 634 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_NAME, argument)); |
| 635 | |
David Sedlák | 9bc6587 | 2019-06-24 20:21:08 +0200 | [diff] [blame] | 636 | YIN_READ_SUBELEMS_START(xml_ctx, data, ret, cleanup, kw, temp_record, subelem_args); |
| 637 | if (!name) { |
| 638 | /* end of argument element reached */ |
| 639 | break; |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 640 | } |
David Sedlák | 9bc6587 | 2019-06-24 20:21:08 +0200 | [diff] [blame] | 641 | |
| 642 | switch (kw) { |
| 643 | case YANG_YIN_ELEMENT: |
| 644 | yin_parse_yin_element_element(xml_ctx, &subelem_args, data, flags, extensions); |
| 645 | break; |
| 646 | case YANG_CUSTOM: |
| 647 | // TODO parse extension instance |
| 648 | break; |
| 649 | |
| 650 | default: |
| 651 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 652 | return LY_EVALID; |
| 653 | } |
| 654 | YIN_READ_SUBELEMS_END(xml_ctx, data, temp_record); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 655 | |
| 656 | cleanup: |
| 657 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 662 | 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] | 663 | { |
| 664 | LY_ERR ret = LY_SUCCESS; |
| 665 | struct lysp_ext *ex; |
| 666 | const char *prefix = NULL, *name = NULL; |
| 667 | char *out = NULL; |
| 668 | size_t out_len = 0, prefix_len = 0, name_len = 0; |
| 669 | int dynamic = 0; |
| 670 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 671 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 672 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 673 | LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *extensions, ex, ret, cleanup); |
| 674 | ret = yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name); |
| 675 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 676 | 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] | 677 | LY_CHECK_ERR_GOTO(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element."); |
| 678 | ret = LY_EINVAL, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 679 | |
| 680 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 681 | 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] | 682 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 683 | if (!name) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 684 | /* end of extension element reached */ |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 685 | break; |
| 686 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 687 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 688 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 689 | 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] | 690 | switch (kw) { |
| 691 | case YANG_ARGUMENT: |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 692 | 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] | 693 | break; |
| 694 | case YANG_DESCRIPTION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 695 | ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc); |
| 696 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 697 | break; |
| 698 | case YANG_REFERENCE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 699 | ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref); |
| 700 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 701 | break; |
| 702 | case YANG_STATUS: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 703 | ret = yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts); |
| 704 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 705 | break; |
| 706 | case YANG_CUSTOM: |
| 707 | /* TODO parse extension instance */ |
| 708 | break; |
| 709 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 710 | 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] | 711 | ret = LY_EVALID; |
| 712 | goto cleanup; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 713 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 714 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 715 | subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 716 | } |
| 717 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 718 | cleanup: |
| 719 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 720 | return ret; |
| 721 | } |
| 722 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 723 | /** |
| 724 | * @brief Parse module substatements. |
| 725 | * |
| 726 | * @param[in] xml_ctx Xml context. |
| 727 | * @param[in,out] data Data to read from. |
| 728 | * @param[out] mod Parsed module structure. |
| 729 | * |
| 730 | * @return LY_ERR values. |
| 731 | */ |
| 732 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 733 | 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] | 734 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 735 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 736 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 737 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 738 | size_t prefix_len, name_len; |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 739 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 740 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 741 | char *buf = NULL, *out = NULL; |
| 742 | size_t buf_len = 0, out_len = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 743 | int dynamic = 0; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 744 | struct yin_arg_record *substmt_args = NULL; |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 745 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 746 | 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] | 747 | 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] | 748 | 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] | 749 | 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] | 750 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 751 | /* loop over all elements and parse them */ |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 752 | while (xml_ctx->status != LYXML_END) { |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 753 | #define CHECK_ORDER(SECTION) \ |
| 754 | if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION |
| 755 | |
| 756 | switch (kw) { |
| 757 | /* module header */ |
| 758 | case YANG_NAMESPACE: |
| 759 | case YANG_PREFIX: |
| 760 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 761 | break; |
| 762 | case YANG_YANG_VERSION: |
| 763 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 764 | break; |
| 765 | /* linkage */ |
| 766 | case YANG_INCLUDE: |
| 767 | case YANG_IMPORT: |
| 768 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 769 | break; |
| 770 | /* meta */ |
| 771 | case YANG_ORGANIZATION: |
| 772 | case YANG_CONTACT: |
| 773 | case YANG_DESCRIPTION: |
| 774 | case YANG_REFERENCE: |
| 775 | CHECK_ORDER(Y_MOD_META); |
| 776 | break; |
| 777 | |
| 778 | /* revision */ |
| 779 | case YANG_REVISION: |
| 780 | CHECK_ORDER(Y_MOD_REVISION); |
| 781 | break; |
| 782 | /* body */ |
| 783 | case YANG_ANYDATA: |
| 784 | case YANG_ANYXML: |
| 785 | case YANG_AUGMENT: |
| 786 | case YANG_CHOICE: |
| 787 | case YANG_CONTAINER: |
| 788 | case YANG_DEVIATION: |
| 789 | case YANG_EXTENSION: |
| 790 | case YANG_FEATURE: |
| 791 | case YANG_GROUPING: |
| 792 | case YANG_IDENTITY: |
| 793 | case YANG_LEAF: |
| 794 | case YANG_LEAF_LIST: |
| 795 | case YANG_LIST: |
| 796 | case YANG_NOTIFICATION: |
| 797 | case YANG_RPC: |
| 798 | case YANG_TYPEDEF: |
| 799 | case YANG_USES: |
| 800 | case YANG_CUSTOM: |
| 801 | mod_stmt = Y_MOD_BODY; |
| 802 | break; |
| 803 | default: |
| 804 | /* error will be handled in the next switch */ |
| 805 | break; |
| 806 | } |
| 807 | #undef CHECK_ORDER |
| 808 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 809 | 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] | 810 | if (name) { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 811 | LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args)); |
| 812 | 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] | 813 | switch (kw) { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 814 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 815 | /* module header */ |
| 816 | case YANG_NAMESPACE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 817 | ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns); |
| 818 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 819 | break; |
| 820 | case YANG_PREFIX: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 821 | ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix); |
| 822 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 823 | break; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 824 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 825 | /* linkage */ |
| 826 | case YANG_IMPORT: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 827 | ret = yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports); |
| 828 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 829 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 830 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 831 | /* meta */ |
| 832 | case YANG_ORGANIZATION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 833 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org); |
| 834 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 835 | break; |
| 836 | case YANG_CONTACT: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 837 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact); |
| 838 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 839 | break; |
| 840 | case YANG_DESCRIPTION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 841 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc); |
| 842 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 843 | break; |
| 844 | case YANG_REFERENCE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 845 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref); |
| 846 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 847 | break; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 848 | /* revision */ |
| 849 | |
| 850 | /*body */ |
| 851 | case YANG_EXTENSION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 852 | ret = yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions); |
| 853 | LY_CHECK_GOTO(ret, error); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 854 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 855 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 856 | default: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 857 | ret = LY_EVALID; |
| 858 | goto error; |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 859 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 860 | FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 861 | substmt_args = NULL; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 865 | return LY_SUCCESS; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 866 | |
| 867 | error: |
| 868 | FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec); |
| 869 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 873 | 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] | 874 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 875 | LY_ERR ret = LY_SUCCESS; |
| 876 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 877 | struct lys_parser_ctx parser_ctx; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 878 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 879 | struct lysp_module *mod_p = NULL; |
| 880 | const char *prefix, *name; |
| 881 | size_t prefix_len, name_len; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 882 | struct yin_arg_record *args = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 883 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 884 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 885 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 886 | xml_ctx->ctx = ctx; |
| 887 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 888 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 889 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 890 | 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] | 891 | LY_CHECK_GOTO(ret, cleanup); |
| 892 | ret = yin_load_attributes(xml_ctx, &data, &args); |
| 893 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 894 | 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] | 895 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 896 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 897 | ret = LY_EINVAL; |
| 898 | goto cleanup; |
| 899 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 900 | 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] | 901 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 902 | ret = LY_EVALID; |
| 903 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 904 | } |
| 905 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 906 | /* allocate module */ |
| 907 | mod_p = calloc(1, sizeof *mod_p); |
| 908 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 909 | mod_p->mod = mod; |
| 910 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 911 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 912 | /* parse module substatements */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 913 | ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 914 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 915 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 916 | mod_p->parsing = 0; |
| 917 | mod->parsed = mod_p; |
| 918 | |
| 919 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 920 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 921 | lysp_module_free(mod_p); |
| 922 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 923 | FREE_ARRAY(xml_ctx, args, free_arg_rec); |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 924 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 925 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 926 | } |