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