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 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 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 | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 29 | /** |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 30 | * @brief Match argument name. |
| 31 | * |
| 32 | * @param[in] name String representing name. |
| 33 | * @param[in] len Lenght of the name. |
| 34 | * |
| 35 | * @reurn YIN_ARGUMENT value. |
| 36 | */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 37 | enum YIN_ARGUMENT |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 38 | match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 39 | { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 40 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 41 | size_t already_read = 0; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 42 | LY_CHECK_RET(len == 0, YIN_ARG_UNKNOWN); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 43 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 44 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 45 | #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] | 46 | #define IF_ARG_PREFIX_END } |
| 47 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 48 | switch (*name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 49 | case 'x': |
| 50 | already_read += 1; |
| 51 | IF_ARG("mlns", 4, YIN_ARG_XMLNS); |
| 52 | break; |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 53 | case 'c': |
| 54 | already_read += 1; |
| 55 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 56 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 57 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 58 | case 'd': |
| 59 | already_read += 1; |
| 60 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 61 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 62 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 63 | case 'm': |
| 64 | already_read += 1; |
| 65 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 66 | break; |
| 67 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 68 | case 'n': |
| 69 | already_read += 1; |
| 70 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 71 | break; |
| 72 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 73 | case 't': |
| 74 | already_read += 1; |
| 75 | IF_ARG_PREFIX("a", 1) |
| 76 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 77 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 78 | IF_ARG_PREFIX_END |
| 79 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 80 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 81 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 82 | case 'u': |
| 83 | already_read += 1; |
| 84 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 85 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 86 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 87 | case 'v': |
| 88 | already_read += 1; |
| 89 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 93 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 94 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 95 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 96 | } |
| 97 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 98 | #undef IF_ARG |
| 99 | #undef IF_ARG_PREFIX |
| 100 | #undef IF_ARG_PREFIX_END |
| 101 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 102 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 103 | } |
| 104 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 105 | /** |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 106 | * @brief parse yin argument |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 107 | * |
| 108 | * @param[in] xml_ctx XML parser context. |
| 109 | * @param[in, out] data Data to read from. |
| 110 | * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments). |
| 111 | * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is spedified as YIN_ARG_NONE. |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 112 | */ |
| 113 | static LY_ERR |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 114 | 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] | 115 | { |
| 116 | LY_ERR ret = LY_SUCCESS; |
| 117 | const char *prefix, *name; |
| 118 | size_t prefix_len, name_len; |
| 119 | |
| 120 | char *buf = NULL, *out = NULL; |
| 121 | size_t buf_len = 0, out_len = 0; |
| 122 | int dynamic; |
| 123 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
| 124 | |
| 125 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
| 126 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 127 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 128 | |
| 129 | arg = match_argument_name(name, name_len); |
| 130 | |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 131 | if (arg == arg_type) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 132 | LY_CHECK_RET(ret); |
| 133 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 134 | LY_CHECK_RET(ret); |
| 135 | *arg_val = lydict_insert(xml_ctx->ctx, out, out_len); |
| 136 | LY_CHECK_ERR_RET(!(*arg_val), LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 137 | } else if (arg_type == YIN_ARG_NONE) { |
| 138 | continue; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 139 | } else { |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 140 | /* unrecognized or unexpected attribute */ |
| 141 | if (name) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 142 | LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid argument in namespace element"); |
| 143 | return LY_EVALID; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return LY_SUCCESS; |
| 149 | } |
| 150 | |
| 151 | /** |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 152 | * @brief Parse content of whole element as text. |
| 153 | * |
| 154 | * @param[in] xml_ctx Xml context. |
| 155 | * @param[in] data Data to read from. |
| 156 | * @param[out] value Where content of element should be stored. |
| 157 | */ |
| 158 | LY_ERR |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 159 | 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] | 160 | { |
| 161 | LY_ERR ret = LY_SUCCESS; |
| 162 | char *buf = NULL, *out = NULL; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 163 | size_t buf_len = 0, out_len = 0; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 164 | int dynamic; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 165 | |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 166 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 167 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 168 | LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID); |
| 169 | |
| 170 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 171 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 172 | LY_CHECK_RET(ret); |
| 173 | *value = lydict_insert(xml_ctx->ctx, out, out_len); |
| 174 | LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 175 | } |
| 176 | |
David Sedlák | 6b7ac2d | 2019-03-22 14:40:29 +0100 | [diff] [blame] | 177 | return LY_SUCCESS; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 178 | } |
| 179 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 180 | /** |
| 181 | * @brief Parse namespace statement. |
| 182 | * |
| 183 | * @param[in] xml_ctx xml context. |
| 184 | * @param[in, out] data Data to read from. |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 185 | * @param[in, out] namespace Where namespace value should be stored. |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 186 | * |
| 187 | * @return LY_ERR values. |
| 188 | */ |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 189 | LY_ERR |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 190 | parse_namespace(struct lyxml_context *xml_ctx, const char **data, const char **namespace) |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 191 | { |
| 192 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 193 | |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 194 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, namespace); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 195 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 196 | |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 197 | return LY_SUCCESS; |
| 198 | } |
| 199 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 200 | /** |
| 201 | * @brief Parse prefix statement. |
| 202 | * |
| 203 | * @param[in] xml_ctx Xml context. |
| 204 | * @param[in, out] data Data to reda from. |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 205 | * @param[out] prefix Where to store prefix value. |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 206 | * |
| 207 | * @return LY_ERR values. |
| 208 | */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 209 | LY_ERR |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 210 | parse_prefix(struct lyxml_context *xml_ctx, const char **data, const char **prefix) |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 211 | { |
| 212 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 213 | |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 214 | /* parse attributes */ |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 215 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, prefix); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 216 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 217 | |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 218 | return ret; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 219 | } |
| 220 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 221 | static LY_ERR |
| 222 | yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts) |
| 223 | { |
| 224 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 225 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 226 | if (rev[0]) { |
| 227 | LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "revision-date"); |
| 228 | return LY_EVALID; |
| 229 | } |
| 230 | |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 231 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, (const char **)&rev); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 232 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 233 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, rev, strlen(rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 234 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 235 | return ret; |
| 236 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 237 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 238 | static LY_ERR |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 239 | 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] | 240 | { |
| 241 | LY_ERR ret = LY_SUCCESS; |
| 242 | enum yang_keyword kw; |
| 243 | struct lysp_import *imp; |
| 244 | const char *prefix, *name; |
| 245 | size_t prefix_len, name_len; |
| 246 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 247 | /* allocate sized array for imports */ |
| 248 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 249 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 250 | /* parse import attributes */ |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 251 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 252 | while ((ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS && name != NULL)) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 253 | kw = match_keyword(name, name_len, prefix_len); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 254 | switch (kw) { |
| 255 | case YANG_PREFIX: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 256 | LY_CHECK_ERR_RET(imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID); |
| 257 | parse_prefix(xml_ctx, data, &imp->prefix); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 258 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 259 | case YANG_DESCRIPTION: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 260 | LY_CHECK_ERR_RET(imp->dsc, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID); |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 261 | parse_text_element(xml_ctx, data, &imp->dsc); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 262 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 263 | case YANG_REFERENCE: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 264 | LY_CHECK_ERR_RET(imp->ref, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID); |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 265 | parse_text_element(xml_ctx, data, &imp->ref); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 266 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 267 | case YANG_REVISION_DATE: |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 268 | yin_parse_revision_date(xml_ctx, data, imp->rev, &imp->exts); |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 269 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 270 | case YANG_CUSTOM: |
| 271 | /* TODO parse extension */ |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 272 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 273 | default: |
| 274 | /* TODO log error */ |
| 275 | return LY_EVALID; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* TODO add log macro and log error */ |
| 280 | LY_CHECK_RET(!imp->prefix); |
| 281 | return ret; |
| 282 | } |
| 283 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 284 | /** |
| 285 | * @brief Parse module substatements. |
| 286 | * |
| 287 | * @param[in] xml_ctx xml context. |
| 288 | * @param[in, out] data Data to read from. |
| 289 | * @param[out] mod Parsed module structure |
| 290 | * |
| 291 | * @return LY_ERR values. |
| 292 | */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 293 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 294 | 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] | 295 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 296 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 297 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 298 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 299 | size_t prefix_len, name_len; |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 300 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 301 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 302 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 303 | char *buf = NULL, *out = NULL; |
| 304 | size_t buf_len = 0, out_len = 0; |
| 305 | int dynamic; |
| 306 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 307 | /* parse module attributes */ |
| 308 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
| 309 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 310 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 311 | |
| 312 | arg = match_argument_name(name, name_len); |
| 313 | |
| 314 | switch (arg) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 315 | case YIN_ARG_NAME: |
| 316 | /* check for multiple definitions of name */ |
| 317 | LY_CHECK_ERR_RET((*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Duplicit definition of module name \"%s\"", (*mod)->mod->name), LY_EEXIST); |
| 318 | |
| 319 | /* read module name */ |
| 320 | if (xml_ctx->status != LYXML_ATTR_CONTENT) { |
| 321 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing value of argument \"name\"."); |
| 322 | } |
| 323 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 324 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 325 | (*mod)->mod->name = lydict_insert(xml_ctx->ctx, out, out_len); |
| 326 | LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 327 | break; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 328 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 329 | default: |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 330 | /* unrecognized attribute */ |
| 331 | /* unrecognized or unexpected attribute */ |
| 332 | /* TODO probably still can be from extension */ |
| 333 | if (arg != YIN_ARG_UNKNOWN || name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 334 | LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid argument in module element"); |
| 335 | return LY_EVALID; |
| 336 | } |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 337 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 338 | break; |
| 339 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 340 | } |
| 341 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 342 | LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Missing argument name of a module", (*mod)->mod->name), LY_ENOTFOUND); |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 343 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 344 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 345 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_YANG(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] | 346 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 347 | /* loop over all elements and parse them */ |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 348 | while (xml_ctx->status != LYXML_END) { |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 349 | /* TODO ADD error log to macro */ |
| 350 | #define CHECK_ORDER(SECTION) \ |
| 351 | if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION |
| 352 | |
| 353 | switch (kw) { |
| 354 | /* module header */ |
| 355 | case YANG_NAMESPACE: |
| 356 | case YANG_PREFIX: |
| 357 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 358 | break; |
| 359 | case YANG_YANG_VERSION: |
| 360 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 361 | break; |
| 362 | /* linkage */ |
| 363 | case YANG_INCLUDE: |
| 364 | case YANG_IMPORT: |
| 365 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 366 | break; |
| 367 | /* meta */ |
| 368 | case YANG_ORGANIZATION: |
| 369 | case YANG_CONTACT: |
| 370 | case YANG_DESCRIPTION: |
| 371 | case YANG_REFERENCE: |
| 372 | CHECK_ORDER(Y_MOD_META); |
| 373 | break; |
| 374 | |
| 375 | /* revision */ |
| 376 | case YANG_REVISION: |
| 377 | CHECK_ORDER(Y_MOD_REVISION); |
| 378 | break; |
| 379 | /* body */ |
| 380 | case YANG_ANYDATA: |
| 381 | case YANG_ANYXML: |
| 382 | case YANG_AUGMENT: |
| 383 | case YANG_CHOICE: |
| 384 | case YANG_CONTAINER: |
| 385 | case YANG_DEVIATION: |
| 386 | case YANG_EXTENSION: |
| 387 | case YANG_FEATURE: |
| 388 | case YANG_GROUPING: |
| 389 | case YANG_IDENTITY: |
| 390 | case YANG_LEAF: |
| 391 | case YANG_LEAF_LIST: |
| 392 | case YANG_LIST: |
| 393 | case YANG_NOTIFICATION: |
| 394 | case YANG_RPC: |
| 395 | case YANG_TYPEDEF: |
| 396 | case YANG_USES: |
| 397 | case YANG_CUSTOM: |
| 398 | mod_stmt = Y_MOD_BODY; |
| 399 | break; |
| 400 | default: |
| 401 | /* error will be handled in the next switch */ |
| 402 | break; |
| 403 | } |
| 404 | #undef CHECK_ORDER |
| 405 | |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 406 | 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] | 407 | LY_CHECK_RET(ret != LY_SUCCESS, LY_EMEM); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 408 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 409 | if (name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 410 | kw = match_keyword(name, name_len, prefix_len); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 411 | switch (kw) { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 412 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 413 | /* module header */ |
| 414 | case YANG_NAMESPACE: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 415 | LY_CHECK_RET(parse_namespace(xml_ctx, data, &(*mod)->mod->ns)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 416 | break; |
| 417 | case YANG_PREFIX: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 418 | LY_CHECK_RET(parse_prefix(xml_ctx, data, &(*mod)->mod->prefix)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 419 | break; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 420 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 421 | /* linkage */ |
| 422 | case YANG_IMPORT: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 423 | yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 424 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 425 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 426 | /* meta */ |
| 427 | case YANG_ORGANIZATION: |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 428 | LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->org)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 429 | break; |
| 430 | case YANG_CONTACT: |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 431 | LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->contact)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 432 | break; |
| 433 | case YANG_DESCRIPTION: |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 434 | LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->dsc)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 435 | break; |
| 436 | case YANG_REFERENCE: |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 437 | LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->ref)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 438 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 439 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 440 | default: |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 441 | return LY_EVALID; |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 442 | break; |
| 443 | } |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 447 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 448 | } |
| 449 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 450 | /** |
| 451 | * @brief Parse yin submodule. |
| 452 | * |
| 453 | * @param[in] ctx Context of YANG schemas. |
| 454 | * @param[in] data Data to read from. |
| 455 | * @param[out] submod Module to write to. |
| 456 | * |
| 457 | * @return LY_ERR values. |
| 458 | */ |
| 459 | LY_ERR |
| 460 | yin_parse_submodule(struct ly_ctx *ctx, const char *data, struct lysp_submodule **submod) |
| 461 | { |
| 462 | LY_ERR ret = LY_SUCCESS; |
| 463 | enum yang_keyword kw = YANG_NONE; |
| 464 | struct lyxml_context xml_ctx; |
| 465 | struct lysp_submodule *mod_p = NULL; |
| 466 | const char *prefix, *name; |
| 467 | size_t prefix_len, name_len; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 468 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 469 | /* initialize xml context */ |
| 470 | memset(&xml_ctx, 0, sizeof xml_ctx); |
| 471 | xml_ctx.ctx = ctx; |
| 472 | xml_ctx.line = 1; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 473 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 474 | /* check submodule */ |
| 475 | ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 476 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 477 | kw = match_keyword(name, name_len, prefix_len); |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 478 | if (kw == YANG_MODULE) { |
| 479 | LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected."); |
| 480 | ret = LY_EINVAL; |
| 481 | goto cleanup; |
| 482 | } else if (kw != YANG_SUBMODULE) { |
David Sedlák | 0a875b4 | 2019-03-07 22:24:05 +0100 | [diff] [blame] | 483 | LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
| 484 | ly_stmt2str(kw)); |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 485 | ret = LY_EVALID; |
| 486 | goto cleanup; |
| 487 | } |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 488 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 489 | /* allocate module */ |
| 490 | mod_p = calloc(1, sizeof *mod_p); |
| 491 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 492 | mod_p->parsing = 1; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 493 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 494 | /* parser submodule substatements */ |
| 495 | //ret = parse_submod(&xml_ctx, &data, mod_p); |
| 496 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 497 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 498 | mod_p->parsing = 0; |
| 499 | *submod = mod_p; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 500 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 501 | cleanup: |
| 502 | if (ret) { |
| 503 | lysp_submodule_free(ctx, mod_p); |
| 504 | } |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 505 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 506 | lyxml_context_clear(&xml_ctx); |
| 507 | return ret; |
| 508 | } |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 509 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 510 | /** |
| 511 | * @brief Parse yin module. |
| 512 | * |
| 513 | * @param[in] ctx Context of YANG schemas. |
| 514 | * @param[in] data Data to read from. |
| 515 | * @param[out] mod Module to write to. |
| 516 | * |
| 517 | * @return LY_ERR values. |
| 518 | */ |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 519 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 520 | 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] | 521 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 522 | LY_ERR ret = LY_SUCCESS; |
| 523 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame^] | 524 | struct lys_parser_ctx parser_ctx; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 525 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 526 | struct lysp_module *mod_p = NULL; |
| 527 | const char *prefix, *name; |
| 528 | size_t prefix_len, name_len; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 529 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 530 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 531 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 532 | xml_ctx->ctx = ctx; |
| 533 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 534 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 535 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 536 | 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] | 537 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 538 | kw = match_keyword(name, name_len, prefix_len); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 539 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 540 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 541 | ret = LY_EINVAL; |
| 542 | goto cleanup; |
| 543 | } else if (kw != YANG_MODULE) { |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 544 | LOGVAL_YANG(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
David Sedlák | 0a875b4 | 2019-03-07 22:24:05 +0100 | [diff] [blame] | 545 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 546 | ret = LY_EVALID; |
| 547 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 548 | } |
| 549 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 550 | /* allocate module */ |
| 551 | mod_p = calloc(1, sizeof *mod_p); |
| 552 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 553 | mod_p->mod = mod; |
| 554 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 555 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 556 | /* parser module substatements */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 557 | ret = parse_mod(xml_ctx, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 558 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 559 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 560 | mod_p->parsing = 0; |
| 561 | mod->parsed = mod_p; |
| 562 | |
| 563 | cleanup: |
| 564 | if (ret) { |
| 565 | lysp_module_free(mod_p); |
| 566 | } |
| 567 | |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 568 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 569 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 570 | } |