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