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