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 | f824ad5 | 2018-10-14 23:58:15 +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 | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 37 | const char *const yin_attr_list[] = { |
| 38 | [YIN_ARG_NAME] = "name", |
| 39 | [YIN_ARG_TARGET_NODE] = "target-node", |
| 40 | [YIN_ARG_MODULE] = "module", |
| 41 | [YIN_ARG_VALUE] = "value", |
| 42 | [YIN_ARG_TEXT] = "text", |
| 43 | [YIN_ARG_CONDITION] = "condition", |
| 44 | [YIN_ARG_URI] = "uri", |
| 45 | [YIN_ARG_DATE] = "date", |
| 46 | [YIN_ARG_TAG] = "tag", |
| 47 | [YIN_ARG_XMLNS] = "xmlns", |
| 48 | }; |
| 49 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 50 | enum yang_keyword |
| 51 | yin_match_keyword(const char *data, size_t len, size_t prefix_len) |
| 52 | { |
| 53 | if (!data || len == 0) { |
| 54 | return YANG_NONE; |
| 55 | } |
| 56 | |
| 57 | const char *start = data; |
David Sedlák | 5f8f033 | 2019-06-18 16:34:30 +0200 | [diff] [blame] | 58 | enum yang_keyword kw = lysp_match_kw(NULL, &data); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 59 | |
| 60 | |
| 61 | if (prefix_len != 0) { |
| 62 | return YANG_CUSTOM; |
| 63 | } |
| 64 | |
| 65 | if (data - start == (long int)len) { |
| 66 | return kw; |
| 67 | } else { |
| 68 | return YANG_NONE; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 73 | enum YIN_ARGUMENT |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 74 | match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 75 | { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 76 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 77 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 78 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 79 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 80 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 81 | #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] | 82 | #define IF_ARG_PREFIX_END } |
| 83 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 84 | switch (*name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 85 | case 'x': |
| 86 | already_read += 1; |
| 87 | IF_ARG("mlns", 4, YIN_ARG_XMLNS); |
| 88 | break; |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 89 | case 'c': |
| 90 | already_read += 1; |
| 91 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 92 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 93 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 94 | case 'd': |
| 95 | already_read += 1; |
| 96 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 97 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 98 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 99 | case 'm': |
| 100 | already_read += 1; |
| 101 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 102 | break; |
| 103 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 104 | case 'n': |
| 105 | already_read += 1; |
| 106 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 107 | break; |
| 108 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 109 | case 't': |
| 110 | already_read += 1; |
| 111 | IF_ARG_PREFIX("a", 1) |
| 112 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 113 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 114 | IF_ARG_PREFIX_END |
| 115 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 116 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 117 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 118 | case 'u': |
| 119 | already_read += 1; |
| 120 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 121 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 122 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 123 | case 'v': |
| 124 | already_read += 1; |
| 125 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 129 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 130 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 131 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 132 | } |
| 133 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 134 | #undef IF_ARG |
| 135 | #undef IF_ARG_PREFIX |
| 136 | #undef IF_ARG_PREFIX_END |
| 137 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 138 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 139 | } |
| 140 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 141 | /** |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 142 | * @brief parse yin argument, arg_val is unchanged if argument arg_type wasn't found. |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 143 | * |
| 144 | * @param[in] xml_ctx XML parser context. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 145 | * @param[in,out] data Data to read from. |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 146 | * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments). |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 147 | * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is specified as YIN_ARG_NONE. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 148 | * |
| 149 | * @return LY_ERR values. |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 150 | */ |
| 151 | static LY_ERR |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 152 | yin_parse_attribute(struct lyxml_context *xml_ctx, const char **data, enum YIN_ARGUMENT arg_type, const char **arg_val) |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 153 | { |
| 154 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 155 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 156 | struct yin_arg_record *argument_array = NULL, *argument_record = NULL, *iter = NULL; |
| 157 | const struct lyxml_ns *ns = NULL; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 158 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 159 | /* load all attributes first */ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 160 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 161 | LY_ARRAY_NEW_GOTO(xml_ctx->ctx, argument_array, argument_record, ret, cleanup); |
| 162 | ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len, |
| 163 | &argument_record->name, &argument_record->name_len); |
| 164 | LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 165 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 166 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 167 | argument_record->content = NULL; |
| 168 | argument_record->content_len = 0; |
| 169 | argument_record->dynamic_content = 0; |
| 170 | ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len, |
| 171 | &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 172 | LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup); |
| 173 | } |
| 174 | } |
| 175 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 176 | /* validation of attributes */ |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 177 | LY_ARRAY_FOR(argument_array, struct yin_arg_record, iter) { |
| 178 | ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len); |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 179 | if (ns && IS_YIN_NS(ns->uri)) { |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 180 | arg = match_argument_name(iter->name, iter->name_len); |
| 181 | if (arg == YIN_ARG_NONE) { |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 182 | continue; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 183 | } else if (arg == arg_type) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 184 | if (iter->dynamic_content) { |
| 185 | *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content); |
| 186 | } else { |
| 187 | *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len); |
| 188 | LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup); |
| 189 | } |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 190 | } else { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 191 | if (iter->dynamic_content) { |
| 192 | free(iter->content); |
| 193 | } |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 194 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name); |
| 195 | ret = LY_EVALID; |
| 196 | goto cleanup; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 201 | cleanup: |
| 202 | LY_ARRAY_FREE(argument_array); |
| 203 | return ret; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 207 | * @brief Parse content of whole element as text. |
| 208 | * |
| 209 | * @param[in] xml_ctx Xml context. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 210 | * @param[in,out] data Data to read from. |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 211 | * @param[out] value Where content of element should be stored. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 212 | * |
| 213 | * @return LY_ERR values. |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 214 | */ |
| 215 | LY_ERR |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 216 | parse_text_element(struct lyxml_context *xml_ctx, const char **data, const char **value) |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 217 | { |
| 218 | LY_ERR ret = LY_SUCCESS; |
| 219 | char *buf = NULL, *out = NULL; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 220 | const char *prefix = NULL, *name = NULL; |
| 221 | 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] | 222 | int dynamic = 0; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 223 | |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 224 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL); |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 225 | LY_CHECK_RET(ret); |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 226 | LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID); |
| 227 | |
| 228 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 229 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 230 | LY_CHECK_RET(ret); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 231 | if (dynamic) { |
| 232 | *value = lydict_insert_zc(xml_ctx->ctx, buf); |
| 233 | } else { |
| 234 | *value = lydict_insert(xml_ctx->ctx, out, out_len); |
| 235 | LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 236 | } |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 237 | } |
| 238 | |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 239 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 240 | LY_CHECK_RET(ret); |
| 241 | |
| 242 | return LY_SUCCESS; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * @brief function to parse meta tags eg. elements with text element as child |
| 247 | * |
| 248 | * @param[in] xml_ctx Xml context. |
| 249 | * @param[in,out] data Data to read from. |
| 250 | * @param[out] value Where the content of meta tag should be stored. |
| 251 | * |
| 252 | * @return LY_ERR values. |
| 253 | */ |
| 254 | LY_ERR |
| 255 | yin_parse_meta_element(struct lyxml_context *xml_ctx, const char **data, const char **value) |
| 256 | { |
| 257 | LY_ERR ret = LY_SUCCESS; |
| 258 | char *buf = NULL, *out = NULL; |
| 259 | const char *prefix = NULL, *name = NULL; |
| 260 | size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0; |
| 261 | int dynamic = 0; |
| 262 | const struct lyxml_ns *ns = NULL; |
| 263 | enum YIN_ARGUMENT arg = YANG_NONE; |
| 264 | |
| 265 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL); |
| 266 | LY_CHECK_RET(ret); |
| 267 | LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID); |
| 268 | |
| 269 | 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^] | 270 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as child of meta element."), LY_EINVAL); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 271 | |
| 272 | /* loop over all child elements and parse them */ |
| 273 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 274 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 275 | LY_CHECK_RET(ret); |
| 276 | if (name) { |
| 277 | ns = lyxml_ns_get(xml_ctx, prefix, prefix_len); |
| 278 | /* check if child element is from yin namespace, elements from other namespaces are silently ignored */ |
| 279 | if (IS_YIN_NS(ns->uri)) { |
| 280 | arg = match_argument_name(name, name_len); |
| 281 | if (arg == YIN_ARG_TEXT) { |
| 282 | parse_text_element(xml_ctx, data, value); |
| 283 | } else { |
| 284 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 285 | return LY_EVALID; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | } else { |
| 290 | /* end of meta element reached */ |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | |
David Sedlák | 6b7ac2d | 2019-03-22 14:40:29 +0100 | [diff] [blame] | 296 | return LY_SUCCESS; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 297 | } |
| 298 | |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 299 | /** |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 300 | * @brief Parse revision date. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 301 | * |
| 302 | * @param[in] xml_ctx Xml context. |
| 303 | * @param[in,out] data Data to read from. |
| 304 | * @param[in,out] rev Array to store the parsed value in. |
| 305 | * @param[in,out] exts Extension instances to add to. |
| 306 | * |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 307 | * @return LY_ERR values. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 308 | */ |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 309 | static LY_ERR |
| 310 | yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts) |
| 311 | { |
| 312 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 313 | const char *temp_rev; |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 314 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 315 | if (rev[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 316 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date"); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 317 | return LY_EVALID; |
| 318 | } |
| 319 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 320 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, &temp_rev)) |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 321 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 322 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
| 323 | |
| 324 | strcpy(rev, temp_rev); |
| 325 | lydict_remove(xml_ctx->ctx, temp_rev); |
| 326 | /* TODO extension */ |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 327 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 328 | return ret; |
| 329 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 330 | |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 331 | LY_ERR |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 332 | yin_parse_import(struct lyxml_context *xml_ctx, const char *module_prefix, const char **data, struct lysp_import **imports) |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 333 | { |
| 334 | LY_ERR ret = LY_SUCCESS; |
| 335 | enum yang_keyword kw; |
| 336 | struct lysp_import *imp; |
| 337 | const char *prefix, *name; |
| 338 | size_t prefix_len, name_len; |
| 339 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 340 | /* allocate sized array for imports */ |
| 341 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 342 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 343 | /* parse import attributes */ |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 344 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name)); |
| 345 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 346 | while ((ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS && name != NULL)) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 347 | kw = yin_match_keyword(name, name_len, prefix_len); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 348 | switch (kw) { |
| 349 | case YANG_PREFIX: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 350 | LY_CHECK_ERR_RET(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID); |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 351 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &imp->prefix)); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 352 | LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)xml_ctx, *imports, module_prefix, &imp->prefix), LY_EVALID); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 353 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 354 | case YANG_DESCRIPTION: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 355 | LY_CHECK_ERR_RET(imp->dsc, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 356 | yin_parse_meta_element(xml_ctx, data, &imp->dsc); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 357 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 358 | case YANG_REFERENCE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 359 | LY_CHECK_ERR_RET(imp->ref, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 360 | yin_parse_meta_element(xml_ctx, data, &imp->ref); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 361 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 362 | case YANG_REVISION_DATE: |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 363 | yin_parse_revision_date(xml_ctx, data, imp->rev, &imp->exts); |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 364 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 365 | case YANG_CUSTOM: |
| 366 | /* TODO parse extension */ |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 367 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 368 | default: |
David Sedlák | 916b190 | 2019-06-06 11:35:05 +0200 | [diff] [blame] | 369 | LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import"); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 370 | return LY_EVALID; |
| 371 | } |
| 372 | } |
| 373 | |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 374 | LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_MISSATTR, "prefix", "import"), LY_EVALID); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 375 | return ret; |
| 376 | } |
| 377 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 378 | LY_ERR |
| 379 | yin_parse_status(struct lyxml_context *xml_ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
| 380 | { |
| 381 | LY_ERR ret = LY_SUCCESS; |
| 382 | enum yang_keyword kw = YANG_NONE; |
| 383 | const char *value = NULL, *prefix = NULL, *name = NULL; |
| 384 | char *out; |
| 385 | size_t prefix_len = 0, name_len = 0, out_len = 0; |
| 386 | int dynamic = 0; |
| 387 | |
| 388 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 389 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 390 | return LY_EVALID; |
| 391 | } |
| 392 | |
| 393 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &value)); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 394 | if (strcmp(value, "current") == 0) { |
| 395 | *flags |= LYS_STATUS_CURR; |
| 396 | } else if (strcmp(value, "deprecated") == 0) { |
| 397 | *flags |= LYS_STATUS_DEPRC; |
| 398 | } else if (strcmp(value, "obsolete") == 0) { |
| 399 | *flags |= LYS_STATUS_OBSLT; |
| 400 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 401 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 402 | lydict_remove(xml_ctx->ctx, value); |
| 403 | return LY_EVALID; |
| 404 | } |
| 405 | lydict_remove(xml_ctx->ctx, value); |
| 406 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 407 | /* TODO check if dynamic was set to 1 in case of error */ |
| 408 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 409 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 410 | /* if there are any xml subelements parse them, unknown text content is silently ignored */ |
| 411 | if (ret == LY_EINVAL) { |
| 412 | /* load subelements */ |
| 413 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 414 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 415 | if (!name) { |
| 416 | break; |
| 417 | } |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 418 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 419 | kw = yin_match_keyword(name, name_len, prefix_len); |
| 420 | switch (kw) { |
| 421 | case YANG_CUSTOM: |
| 422 | /* TODO parse extension instance */ |
| 423 | break; |
| 424 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 425 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status"); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 426 | } |
| 427 | } |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * @brief Parse the extension statement. |
| 436 | * |
| 437 | * @param[in] xml_ctx Xml context. |
| 438 | * @param[in,out] data Data to read from. |
| 439 | * @param[in,out] extensions Extensions to add to. |
| 440 | * |
| 441 | * @return LY_ERR values. |
| 442 | */ |
| 443 | LY_ERR |
| 444 | yin_parse_extension(struct lyxml_context *xml_ctx, const char **data, struct lysp_ext **extensions) |
| 445 | { |
| 446 | LY_ERR ret = LY_SUCCESS; |
| 447 | struct lysp_ext *ex; |
| 448 | const char *prefix = NULL, *name = NULL; |
| 449 | char *out = NULL; |
| 450 | size_t out_len = 0, prefix_len = 0, name_len = 0; |
| 451 | int dynamic = 0; |
| 452 | enum yang_keyword kw = YANG_NONE; |
| 453 | |
| 454 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *extensions, ex, LY_EMEM); |
| 455 | yin_parse_attribute(xml_ctx, data, YIN_ARG_NAME, &ex->name); |
| 456 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 457 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element."), LY_EINVAL); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 458 | |
| 459 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 460 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 461 | LY_CHECK_RET(ret); |
| 462 | if (!name) { |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | kw = yin_match_keyword(name, name_len, prefix_len); |
| 467 | switch (kw) { |
| 468 | case YANG_ARGUMENT: |
| 469 | break; |
| 470 | case YANG_DESCRIPTION: |
| 471 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &ex->dsc)); |
| 472 | break; |
| 473 | case YANG_REFERENCE: |
| 474 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &ex->ref)); |
| 475 | break; |
| 476 | case YANG_STATUS: |
| 477 | LY_CHECK_RET(yin_parse_status(xml_ctx, data, &ex->flags, &ex->exts)); |
| 478 | break; |
| 479 | case YANG_CUSTOM: |
| 480 | /* TODO parse extension instance */ |
| 481 | break; |
| 482 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 483 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 484 | return LY_EVALID; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Parse extension instance. |
| 493 | * |
| 494 | * @param[in] |
| 495 | */ |
| 496 | LY_ERR |
| 497 | yin_parse_extension_instane(struct lyxml_context *xml_ctx, const char *data) |
| 498 | { |
| 499 | |
| 500 | return LY_SUCCESS; |
| 501 | } |
| 502 | |
| 503 | /** |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 504 | * @brief Parse module substatements. |
| 505 | * |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 506 | * @param[in] xml_ctx Xml context. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 507 | * @param[in,out] data Data to read from. |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 508 | * @param[out] mod Parsed module structure. |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 509 | * |
| 510 | * @return LY_ERR values. |
| 511 | */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 512 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 513 | parse_mod(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 514 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 515 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 516 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 517 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 518 | size_t prefix_len, name_len; |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 519 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 520 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 521 | char *buf = NULL, *out = NULL; |
| 522 | size_t buf_len = 0, out_len = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 523 | int dynamic = 0; |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 524 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 525 | yin_parse_attribute(xml_ctx, data, YIN_ARG_NAME, &(*mod)->mod->name); |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 526 | |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 527 | 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] | 528 | 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^] | 529 | 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] | 530 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 531 | /* loop over all elements and parse them */ |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 532 | while (xml_ctx->status != LYXML_END) { |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 533 | #define CHECK_ORDER(SECTION) \ |
| 534 | if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION |
| 535 | |
| 536 | switch (kw) { |
| 537 | /* module header */ |
| 538 | case YANG_NAMESPACE: |
| 539 | case YANG_PREFIX: |
| 540 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 541 | break; |
| 542 | case YANG_YANG_VERSION: |
| 543 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 544 | break; |
| 545 | /* linkage */ |
| 546 | case YANG_INCLUDE: |
| 547 | case YANG_IMPORT: |
| 548 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 549 | break; |
| 550 | /* meta */ |
| 551 | case YANG_ORGANIZATION: |
| 552 | case YANG_CONTACT: |
| 553 | case YANG_DESCRIPTION: |
| 554 | case YANG_REFERENCE: |
| 555 | CHECK_ORDER(Y_MOD_META); |
| 556 | break; |
| 557 | |
| 558 | /* revision */ |
| 559 | case YANG_REVISION: |
| 560 | CHECK_ORDER(Y_MOD_REVISION); |
| 561 | break; |
| 562 | /* body */ |
| 563 | case YANG_ANYDATA: |
| 564 | case YANG_ANYXML: |
| 565 | case YANG_AUGMENT: |
| 566 | case YANG_CHOICE: |
| 567 | case YANG_CONTAINER: |
| 568 | case YANG_DEVIATION: |
| 569 | case YANG_EXTENSION: |
| 570 | case YANG_FEATURE: |
| 571 | case YANG_GROUPING: |
| 572 | case YANG_IDENTITY: |
| 573 | case YANG_LEAF: |
| 574 | case YANG_LEAF_LIST: |
| 575 | case YANG_LIST: |
| 576 | case YANG_NOTIFICATION: |
| 577 | case YANG_RPC: |
| 578 | case YANG_TYPEDEF: |
| 579 | case YANG_USES: |
| 580 | case YANG_CUSTOM: |
| 581 | mod_stmt = Y_MOD_BODY; |
| 582 | break; |
| 583 | default: |
| 584 | /* error will be handled in the next switch */ |
| 585 | break; |
| 586 | } |
| 587 | #undef CHECK_ORDER |
| 588 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 589 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 590 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 591 | if (name) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 592 | kw = yin_match_keyword(name, name_len, prefix_len); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 593 | switch (kw) { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 594 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 595 | /* module header */ |
| 596 | case YANG_NAMESPACE: |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 597 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, &(*mod)->mod->ns)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 598 | break; |
| 599 | case YANG_PREFIX: |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 600 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &(*mod)->mod->prefix)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 601 | break; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 602 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 603 | /* linkage */ |
| 604 | case YANG_IMPORT: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 605 | yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 606 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 607 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 608 | /* meta */ |
| 609 | case YANG_ORGANIZATION: |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 610 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->org)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 611 | break; |
| 612 | case YANG_CONTACT: |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 613 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->contact)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 614 | break; |
| 615 | case YANG_DESCRIPTION: |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 616 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->dsc)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 617 | break; |
| 618 | case YANG_REFERENCE: |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 619 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->ref)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 620 | break; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 621 | /* revision */ |
| 622 | |
| 623 | /*body */ |
| 624 | case YANG_EXTENSION: |
| 625 | LY_CHECK_RET(yin_parse_extension(xml_ctx, data, &(*mod)->extensions)); |
| 626 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 627 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 628 | default: |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 629 | return LY_EVALID; |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 630 | break; |
| 631 | } |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 635 | return LY_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 639 | 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] | 640 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 641 | LY_ERR ret = LY_SUCCESS; |
| 642 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 643 | struct lys_parser_ctx parser_ctx; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 644 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 645 | struct lysp_module *mod_p = NULL; |
| 646 | const char *prefix, *name; |
| 647 | size_t prefix_len, name_len; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 648 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 649 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 650 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 651 | xml_ctx->ctx = ctx; |
| 652 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 653 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 654 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 655 | ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 656 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 657 | kw = yin_match_keyword(name, name_len, prefix_len); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 658 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 659 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 660 | ret = LY_EINVAL; |
| 661 | goto cleanup; |
| 662 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame^] | 663 | 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] | 664 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 665 | ret = LY_EVALID; |
| 666 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 667 | } |
| 668 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 669 | /* allocate module */ |
| 670 | mod_p = calloc(1, sizeof *mod_p); |
| 671 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 672 | mod_p->mod = mod; |
| 673 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 674 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 675 | /* parser module substatements */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 676 | ret = parse_mod(xml_ctx, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 677 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 678 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 679 | mod_p->parsing = 0; |
| 680 | mod->parsed = mod_p; |
| 681 | |
| 682 | cleanup: |
| 683 | if (ret) { |
| 684 | lysp_module_free(mod_p); |
| 685 | } |
| 686 | |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 687 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 688 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 689 | } |