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