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. |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 283 | * @param[in, out] namespace Where namespace value should be stored. |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 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 | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 288 | parse_namespace(struct lyxml_context *xml_ctx, const char **data, const char **namespace) |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 289 | { |
| 290 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 291 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 292 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, namespace, "namespace"); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 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 | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 305 | * @param[out] prefix Where to store prefix value. |
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 | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 310 | parse_prefix(struct lyxml_context *xml_ctx, const char **data, const char **prefix) |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 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 | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 315 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, 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 | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 322 | static LY_ERR |
| 323 | yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts) |
| 324 | { |
| 325 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 326 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 327 | if (rev[0]) { |
| 328 | LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "revision-date"); |
| 329 | return LY_EVALID; |
| 330 | } |
| 331 | |
| 332 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, (const char **)&rev, "revision-date"); |
| 333 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
| 334 | LY_CHECK_RET(lysp_check_date((struct ly_parser_ctx *)xml_ctx, rev, strlen(rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
| 335 | |
| 336 | ret = lyxml_ns_rm(xml_ctx, "import"); |
| 337 | return ret; |
| 338 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 339 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 340 | static LY_ERR |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 341 | 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] | 342 | { |
| 343 | LY_ERR ret = LY_SUCCESS; |
| 344 | enum yang_keyword kw; |
| 345 | struct lysp_import *imp; |
| 346 | const char *prefix, *name; |
| 347 | size_t prefix_len, name_len; |
| 348 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 349 | /* allocate sized array for imports */ |
| 350 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 351 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 352 | /* parse import attributes */ |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 353 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name, "import"); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 354 | 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] | 355 | kw = match_keyword(name, name_len, prefix_len); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 356 | switch (kw) { |
| 357 | case YANG_PREFIX: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 358 | LY_CHECK_ERR_RET(imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID); |
| 359 | parse_prefix(xml_ctx, data, &imp->prefix); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 360 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 361 | case YANG_DESCRIPTION: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 362 | LY_CHECK_ERR_RET(imp->dsc, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID); |
| 363 | parse_text_element(xml_ctx, "description", data, &imp->dsc); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 364 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 365 | case YANG_REFERENCE: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 366 | LY_CHECK_ERR_RET(imp->ref, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID); |
| 367 | parse_text_element(xml_ctx, "reference", data, &imp->ref); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 368 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 369 | case YANG_REVISION_DATE: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 370 | yin_parse_revision_date(xml_ctx, data, &imp->rev, &imp->exts); |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame^] | 371 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 372 | case YANG_CUSTOM: |
| 373 | /* TODO parse extension */ |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame^] | 374 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 375 | default: |
| 376 | /* TODO log error */ |
| 377 | return LY_EVALID; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /* TODO add log macro and log error */ |
| 382 | LY_CHECK_RET(!imp->prefix); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 383 | ret = lyxml_ns_rm(xml_ctx, "import"); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 384 | return ret; |
| 385 | } |
| 386 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 387 | /** |
| 388 | * @brief Parse module substatements. |
| 389 | * |
| 390 | * @param[in] xml_ctx xml context. |
| 391 | * @param[in, out] data Data to read from. |
| 392 | * @param[out] mod Parsed module structure |
| 393 | * |
| 394 | * @return LY_ERR values. |
| 395 | */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 396 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 397 | 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] | 398 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 399 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 400 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 401 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 402 | size_t prefix_len, name_len; |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 403 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 404 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 405 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 406 | char *buf = NULL, *out = NULL; |
| 407 | size_t buf_len = 0, out_len = 0; |
| 408 | int dynamic; |
| 409 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 410 | /* parse module attributes */ |
| 411 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
| 412 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 413 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 414 | |
| 415 | arg = match_argument_name(name, name_len); |
| 416 | |
| 417 | switch (arg) { |
| 418 | case YIN_ARG_XMLNS: |
| 419 | parse_xmlns(xml_ctx, data, prefix, prefix_len, "module"); |
| 420 | break; |
| 421 | case YIN_ARG_NAME: |
| 422 | /* check for multiple definitions of name */ |
| 423 | 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); |
| 424 | |
| 425 | /* read module name */ |
| 426 | if (xml_ctx->status != LYXML_ATTR_CONTENT) { |
| 427 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing value of argument \"name\"."); |
| 428 | } |
| 429 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 430 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 431 | (*mod)->mod->name = lydict_insert(xml_ctx->ctx, out, out_len); |
| 432 | LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 433 | break; |
| 434 | default: |
| 435 | /* unrecognized attribute, still can be namespace definition eg. xmlns:foo=.... */ |
| 436 | if (match_argument_name(prefix, prefix_len) == YIN_ARG_XMLNS) { |
| 437 | /* in this case prefix of namespace is actually name of attribute */ |
| 438 | parse_xmlns(xml_ctx, data, name, name_len, "module"); |
| 439 | } else { |
| 440 | /* unrecognized or unexpected attribute */ |
| 441 | LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid argument in module element"); |
| 442 | return LY_EVALID; |
| 443 | } |
| 444 | break; |
| 445 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 446 | } |
| 447 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 448 | 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] | 449 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 450 | 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] | 451 | 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] | 452 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 453 | /* loop over all elements and parse them */ |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 454 | while (xml_ctx->status != LYXML_END) { |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 455 | /* TODO ADD error log to macro */ |
| 456 | #define CHECK_ORDER(SECTION) \ |
| 457 | if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION |
| 458 | |
| 459 | switch (kw) { |
| 460 | /* module header */ |
| 461 | case YANG_NAMESPACE: |
| 462 | case YANG_PREFIX: |
| 463 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 464 | break; |
| 465 | case YANG_YANG_VERSION: |
| 466 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 467 | break; |
| 468 | /* linkage */ |
| 469 | case YANG_INCLUDE: |
| 470 | case YANG_IMPORT: |
| 471 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 472 | break; |
| 473 | /* meta */ |
| 474 | case YANG_ORGANIZATION: |
| 475 | case YANG_CONTACT: |
| 476 | case YANG_DESCRIPTION: |
| 477 | case YANG_REFERENCE: |
| 478 | CHECK_ORDER(Y_MOD_META); |
| 479 | break; |
| 480 | |
| 481 | /* revision */ |
| 482 | case YANG_REVISION: |
| 483 | CHECK_ORDER(Y_MOD_REVISION); |
| 484 | break; |
| 485 | /* body */ |
| 486 | case YANG_ANYDATA: |
| 487 | case YANG_ANYXML: |
| 488 | case YANG_AUGMENT: |
| 489 | case YANG_CHOICE: |
| 490 | case YANG_CONTAINER: |
| 491 | case YANG_DEVIATION: |
| 492 | case YANG_EXTENSION: |
| 493 | case YANG_FEATURE: |
| 494 | case YANG_GROUPING: |
| 495 | case YANG_IDENTITY: |
| 496 | case YANG_LEAF: |
| 497 | case YANG_LEAF_LIST: |
| 498 | case YANG_LIST: |
| 499 | case YANG_NOTIFICATION: |
| 500 | case YANG_RPC: |
| 501 | case YANG_TYPEDEF: |
| 502 | case YANG_USES: |
| 503 | case YANG_CUSTOM: |
| 504 | mod_stmt = Y_MOD_BODY; |
| 505 | break; |
| 506 | default: |
| 507 | /* error will be handled in the next switch */ |
| 508 | break; |
| 509 | } |
| 510 | #undef CHECK_ORDER |
| 511 | |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 512 | 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] | 513 | LY_CHECK_RET(ret != LY_SUCCESS, LY_EMEM); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 514 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 515 | if (name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 516 | kw = match_keyword(name, name_len, prefix_len); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 517 | switch (kw) { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 518 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 519 | /* module header */ |
| 520 | case YANG_NAMESPACE: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 521 | LY_CHECK_RET(parse_namespace(xml_ctx, data, &(*mod)->mod->ns)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 522 | break; |
| 523 | case YANG_PREFIX: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 524 | LY_CHECK_RET(parse_prefix(xml_ctx, data, &(*mod)->mod->prefix)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 525 | break; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 526 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 527 | /* linkage */ |
| 528 | case YANG_IMPORT: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 529 | yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 530 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 531 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 532 | /* meta */ |
| 533 | case YANG_ORGANIZATION: |
David Sedlák | 6b7ac2d | 2019-03-22 14:40:29 +0100 | [diff] [blame] | 534 | 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] | 535 | break; |
| 536 | case YANG_CONTACT: |
David Sedlák | 6b7ac2d | 2019-03-22 14:40:29 +0100 | [diff] [blame] | 537 | 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] | 538 | break; |
| 539 | case YANG_DESCRIPTION: |
David Sedlák | 6b7ac2d | 2019-03-22 14:40:29 +0100 | [diff] [blame] | 540 | 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] | 541 | break; |
| 542 | case YANG_REFERENCE: |
David Sedlák | 6b7ac2d | 2019-03-22 14:40:29 +0100 | [diff] [blame] | 543 | 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] | 544 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 545 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 546 | default: |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 547 | return LY_EVALID; |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 548 | break; |
| 549 | } |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 553 | lyxml_ns_rm(xml_ctx, "module"); |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 554 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 555 | } |
| 556 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 557 | /** |
| 558 | * @brief Parse yin submodule. |
| 559 | * |
| 560 | * @param[in] ctx Context of YANG schemas. |
| 561 | * @param[in] data Data to read from. |
| 562 | * @param[out] submod Module to write to. |
| 563 | * |
| 564 | * @return LY_ERR values. |
| 565 | */ |
| 566 | LY_ERR |
| 567 | yin_parse_submodule(struct ly_ctx *ctx, const char *data, struct lysp_submodule **submod) |
| 568 | { |
| 569 | LY_ERR ret = LY_SUCCESS; |
| 570 | enum yang_keyword kw = YANG_NONE; |
| 571 | struct lyxml_context xml_ctx; |
| 572 | struct lysp_submodule *mod_p = NULL; |
| 573 | const char *prefix, *name; |
| 574 | size_t prefix_len, name_len; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 575 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 576 | /* initialize xml context */ |
| 577 | memset(&xml_ctx, 0, sizeof xml_ctx); |
| 578 | xml_ctx.ctx = ctx; |
| 579 | xml_ctx.line = 1; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 580 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 581 | /* check submodule */ |
| 582 | ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 583 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 584 | kw = match_keyword(name, name_len, prefix_len); |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 585 | if (kw == YANG_MODULE) { |
| 586 | LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected."); |
| 587 | ret = LY_EINVAL; |
| 588 | goto cleanup; |
| 589 | } else if (kw != YANG_SUBMODULE) { |
David Sedlák | 0a875b4 | 2019-03-07 22:24:05 +0100 | [diff] [blame] | 590 | LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
| 591 | ly_stmt2str(kw)); |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 592 | ret = LY_EVALID; |
| 593 | goto cleanup; |
| 594 | } |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 595 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 596 | /* allocate module */ |
| 597 | mod_p = calloc(1, sizeof *mod_p); |
| 598 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 599 | mod_p->parsing = 1; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 600 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 601 | /* parser submodule substatements */ |
| 602 | //ret = parse_submod(&xml_ctx, &data, mod_p); |
| 603 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 604 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 605 | mod_p->parsing = 0; |
| 606 | *submod = mod_p; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 607 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 608 | cleanup: |
| 609 | if (ret) { |
| 610 | lysp_submodule_free(ctx, mod_p); |
| 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 | lyxml_context_clear(&xml_ctx); |
| 614 | return ret; |
| 615 | } |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 616 | |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 617 | /** |
| 618 | * @brief Parse yin module. |
| 619 | * |
| 620 | * @param[in] ctx Context of YANG schemas. |
| 621 | * @param[in] data Data to read from. |
| 622 | * @param[out] mod Module to write to. |
| 623 | * |
| 624 | * @return LY_ERR values. |
| 625 | */ |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 626 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 627 | 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] | 628 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 629 | LY_ERR ret = LY_SUCCESS; |
| 630 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 631 | struct ly_parser_ctx parser_ctx; |
| 632 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 633 | struct lysp_module *mod_p = NULL; |
| 634 | const char *prefix, *name; |
| 635 | size_t prefix_len, name_len; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 636 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 637 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 638 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 639 | xml_ctx->ctx = ctx; |
| 640 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 641 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 642 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 643 | 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] | 644 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 645 | kw = match_keyword(name, name_len, prefix_len); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 646 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 647 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 648 | ret = LY_EINVAL; |
| 649 | goto cleanup; |
| 650 | } else if (kw != YANG_MODULE) { |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 651 | LOGVAL_YANG(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
David Sedlák | 0a875b4 | 2019-03-07 22:24:05 +0100 | [diff] [blame] | 652 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 653 | ret = LY_EVALID; |
| 654 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 655 | } |
| 656 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 657 | /* allocate module */ |
| 658 | mod_p = calloc(1, sizeof *mod_p); |
| 659 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 660 | mod_p->mod = mod; |
| 661 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 662 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 663 | /* parser module substatements */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 664 | ret = parse_mod(xml_ctx, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 665 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 666 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 667 | mod_p->parsing = 0; |
| 668 | mod->parsed = mod_p; |
| 669 | |
| 670 | cleanup: |
| 671 | if (ret) { |
| 672 | lysp_module_free(mod_p); |
| 673 | } |
| 674 | |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 675 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 676 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 677 | } |