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