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 | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 38 | enum YIN_ARGUMENT |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 39 | match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 40 | { |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 41 | enum YIN_ARGUMENT arg = YIN_ARG_NONE; |
| 42 | size_t already_read = 0; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 43 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 44 | #define MOVE_DATA(DATA, COUNT) already_read+=COUNT; |
| 45 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {MOVE_DATA(name, LEN);arg=STMT;} |
| 46 | #define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {MOVE_DATA(name, LEN); |
| 47 | #define IF_ARG_PREFIX_END } |
| 48 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 49 | switch(*name) { |
| 50 | case 'c': |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 51 | MOVE_DATA(name, 1); |
| 52 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 53 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 54 | |
| 55 | case 'd': |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 56 | MOVE_DATA(name, 1); |
| 57 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 58 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 59 | |
| 60 | case 'm': |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 61 | MOVE_DATA(name, 1); |
| 62 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 63 | break; |
| 64 | |
| 65 | case 'n': |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 66 | MOVE_DATA(name, 1); |
| 67 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 68 | break; |
| 69 | |
| 70 | case 't': |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 71 | MOVE_DATA(name, 1); |
| 72 | IF_ARG_PREFIX("a", 1) |
| 73 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 74 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 75 | IF_ARG_PREFIX_END |
| 76 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 77 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 78 | |
| 79 | case 'u': |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 80 | MOVE_DATA(name, 1); |
| 81 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 82 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 83 | |
| 84 | case 'v': |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 85 | MOVE_DATA(name, 1); |
| 86 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 90 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 91 | if (already_read != len) { |
| 92 | arg = YIN_ARG_NONE; |
| 93 | } |
| 94 | |
| 95 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | LY_ERR |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 99 | parser_belongs_to(struct lyxml_context *xml_ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext **extensions) |
| 100 | { |
| 101 | enum yang_keyword kw = YANG_NONE; |
| 102 | LY_ERR ret = LY_SUCCESS; |
| 103 | const char *prefix_out, *name; |
| 104 | size_t prefix_len, name_len; |
| 105 | |
| 106 | char *buf = NULL, *out = NULL; |
| 107 | size_t buf_len = 0, out_len = 0; |
| 108 | int dynamic; |
| 109 | |
| 110 | /* check if belongs-to has argument module */ |
| 111 | ret = lyxml_get_attribute(xml_ctx, data, &prefix_out, &prefix_len, &name, &name_len); |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 112 | LY_CHECK_RET1(ret); |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 113 | if (match_argument_name(name, name_len) != YIN_ARG_MODULE) { |
| 114 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"module\".", name); |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 115 | return LY_EINVAL; |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 116 | } |
| 117 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 118 | /* read content of argument */ |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 119 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 120 | LY_CHECK_RET1(ret); |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 121 | *belongsto = lydict_insert(xml_ctx->ctx, out, out_len); |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 122 | LY_CHECK_ERR_RET(!belongsto, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 123 | |
| 124 | /* read substatements */ |
| 125 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
| 126 | ret = lyxml_get_attribute(xml_ctx, data, &prefix_out, &prefix_len, &name, &name_len); |
| 127 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), ret); |
| 128 | kw = match_keyword(name); |
| 129 | |
| 130 | switch (kw) { |
| 131 | case YANG_PREFIX: |
| 132 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 133 | *prefix = lydict_insert(xml_ctx->ctx, out, out_len); |
| 134 | break; |
| 135 | case YANG_CUSTOM: |
| 136 | /* TODO parse extension */ |
| 137 | break; |
| 138 | default: |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 139 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Unexpected attribute"); |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 140 | return LY_EVALID; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (!prefix) { |
| 145 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing prefix"); |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 146 | return LY_EVALID; |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 147 | } |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 148 | |
| 149 | return LY_SUCCESS; |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | LY_ERR |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 153 | parse_namespace(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p) |
| 154 | { |
| 155 | LY_ERR ret = LY_SUCCESS; |
| 156 | const char *prefix, *name; |
| 157 | size_t prefix_len, name_len; |
| 158 | |
| 159 | char *buf = NULL, *out = NULL; |
| 160 | size_t buf_len = 0, out_len = 0; |
| 161 | int dynamic; |
| 162 | |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 163 | /* check if namespace has argument uri */ |
| 164 | ret = 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] | 165 | LY_CHECK_RET1(ret); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 166 | if (match_argument_name(name, name_len) != YIN_ARG_URI) { |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 167 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"uri\".", name); |
| 168 | return LY_EVALID; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 169 | } |
| 170 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 171 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 172 | LY_CHECK_RET1(ret); |
| 173 | (*mod_p)->ns = lydict_insert(xml_ctx->ctx, out, out_len); |
| 174 | LY_CHECK_ERR_RET(!(*mod_p)->ns, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 175 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 176 | /* namespace can only have one argument */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 177 | ret = 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] | 178 | LY_CHECK_RET1(ret); |
| 179 | if (name) { |
| 180 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Unexpected argument \"%s\".", name); |
| 181 | return LY_EVALID; |
| 182 | } |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 183 | |
| 184 | return LY_SUCCESS; |
| 185 | } |
| 186 | |
| 187 | LY_ERR |
| 188 | parse_prefix(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p) |
| 189 | { |
| 190 | LY_ERR ret = LY_SUCCESS; |
| 191 | const char *prefix, *name; |
| 192 | size_t prefix_len, name_len; |
| 193 | |
| 194 | char *buf = NULL, *out = NULL; |
| 195 | size_t buf_len = 0, out_len = 0; |
| 196 | int dynamic; |
| 197 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 198 | /* check if prefix has argument value */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 199 | ret = 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] | 200 | LY_CHECK_RET1(ret); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 201 | if (match_argument_name(name, name_len) != YIN_ARG_VALUE) { |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 202 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"value\".", name); |
| 203 | return LY_EVALID; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 204 | } |
| 205 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 206 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 207 | LY_CHECK_RET1(ret); |
| 208 | (*mod_p)->prefix = lydict_insert(xml_ctx->ctx, out, out_len); |
| 209 | LY_CHECK_ERR_RET(!(*mod_p)->prefix, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 210 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 211 | /* prefix element can only have one argument */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 212 | ret = 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] | 213 | LY_CHECK_RET1(ret); |
| 214 | if (name) { |
| 215 | LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Unexpected argument \"%s\".", name); |
| 216 | return LY_EVALID; |
| 217 | } |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 218 | return LY_SUCCESS; |
| 219 | } |
| 220 | |
| 221 | LY_ERR |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 222 | parse_submodule(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p) |
| 223 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 224 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 225 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 226 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 227 | size_t prefix_len, name_len; |
| 228 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 229 | char *buf = NULL, *out = NULL; |
| 230 | size_t buf_len = 0, out_len = 0; |
| 231 | int dynamic; |
| 232 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 233 | /* check if module/submodule has argument "name" */ |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 234 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 235 | 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] | 236 | if (match_argument_name(name, name_len) != YIN_ARG_NAME) { |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 237 | 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] | 238 | } |
| 239 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 240 | /* read module name */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 241 | if (xml_ctx->status != LYXML_ATTR_CONTENT) { |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 242 | 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] | 243 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 244 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 245 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 246 | (*mod_p)->name = lydict_insert(xml_ctx->ctx, out, out_len); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 247 | LY_CHECK_ERR_RET(!(*mod_p)->name, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 248 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 249 | /* read all attributes and their content only for testing */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 250 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
| 251 | 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] | 252 | while (xml_ctx->status == LYXML_ATTR_CONTENT) { |
| 253 | 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] | 254 | } |
| 255 | } |
| 256 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 257 | |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 258 | while (xml_ctx->status == LYXML_ELEMENT || xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 259 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 260 | printf("%s\n\n\n", name); |
| 261 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 262 | kw = match_keyword(name); |
| 263 | |
| 264 | switch (kw) { |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 265 | case YANG_NAMESPACE: |
| 266 | ret = parse_namespace(xml_ctx, data, mod_p); |
| 267 | break; |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 268 | case YANG_PREFIX: |
| 269 | ret = parse_prefix(xml_ctx, data, mod_p); |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 270 | /* TODO change lysp_check_prefix function to work with ctx and not parser_ctx */ |
| 271 | //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] | 272 | break; |
| 273 | case YANG_BELONGS_TO: |
| 274 | ret = parser_belongs_to(xml_ctx, data, &(*mod_p)->belongsto, &(*mod_p)->prefix, &(*mod_p)->extensions); |
| 275 | break; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 276 | |
| 277 | default: |
David Sedlák | a5004e6 | 2018-12-16 23:54:47 +0100 | [diff] [blame] | 278 | /* error */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 283 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | LY_ERR |
| 287 | yin_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p) |
| 288 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 289 | LY_ERR ret = LY_SUCCESS; |
| 290 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 291 | struct lyxml_context xml_ctx; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 292 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 293 | memset(&xml_ctx, 0, sizeof xml_ctx); |
| 294 | xml_ctx.ctx = ctx; |
| 295 | xml_ctx.line = 1; |
| 296 | |
| 297 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 298 | size_t prefix_len, name_len; |
| 299 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 300 | /* check if root element is module or submodule */ |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 301 | ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame^] | 302 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 303 | kw = match_keyword(name); |
| 304 | if (kw != YANG_MODULE && kw != YANG_SUBMODULE) { |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 305 | LOGVAL(xml_ctx.ctx, LY_VLOG_LINE, &xml_ctx.line, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", name); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 306 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 307 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 308 | if (kw == YANG_SUBMODULE) { |
| 309 | (*mod_p)->submodule = 1; |
| 310 | } |
| 311 | |
| 312 | ret = parse_submodule(&xml_ctx, &data, mod_p); |
| 313 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 314 | lyxml_context_clear(&xml_ctx); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 315 | return ret; |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame^] | 316 | |
| 317 | error: |
| 318 | lyxml_context_clear(&xml_ctx); |
| 319 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 320 | } |