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 | * |
David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 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 | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 14 | #include "common.h" |
| 15 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 19 | #include <string.h> |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 20 | |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 21 | #include "context.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 22 | #include "dict.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 23 | #include "xml.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 24 | #include "tree.h" |
| 25 | #include "tree_schema.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 26 | #include "tree_schema_internal.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 27 | #include "parser_yin.h" |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 28 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 29 | /** |
| 30 | * @brief check if given string is URI of yin namespace. |
| 31 | * @param ns Namespace URI to check. |
| 32 | * |
| 33 | * @return true if ns equals YIN_NS_URI false otherwise. |
| 34 | */ |
| 35 | #define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0) |
| 36 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 37 | const char *const yin_attr_list[] = { |
| 38 | [YIN_ARG_NAME] = "name", |
| 39 | [YIN_ARG_TARGET_NODE] = "target-node", |
| 40 | [YIN_ARG_MODULE] = "module", |
| 41 | [YIN_ARG_VALUE] = "value", |
| 42 | [YIN_ARG_TEXT] = "text", |
| 43 | [YIN_ARG_CONDITION] = "condition", |
| 44 | [YIN_ARG_URI] = "uri", |
| 45 | [YIN_ARG_DATE] = "date", |
| 46 | [YIN_ARG_TAG] = "tag", |
| 47 | [YIN_ARG_XMLNS] = "xmlns", |
| 48 | }; |
| 49 | |
David Sedlák | c8dd80e | 2019-06-19 16:08:48 +0200 | [diff] [blame^] | 50 | /** |
| 51 | * @brief check if keyword was matched properly, |
| 52 | * information about namespace is not always fully available when type of keyword is already needed |
| 53 | * this function retroactively checks if kw isn't actually extension instance or unknown element without namespace |
| 54 | * |
| 55 | * @param[in] xml_ctx Xml context. |
| 56 | * @param[in] prefix Keyword prefix. |
| 57 | * @param[in] prefix_len Length of prefix. |
| 58 | * @param[in,out] kw Type of keyword. |
| 59 | */ |
| 60 | void |
| 61 | check_kw_ns(struct lyxml_context *xml_ctx, const char *prefix, size_t prefix_len, enum yang_keyword *kw) |
| 62 | { |
| 63 | const struct lyxml_ns *ns = NULL; |
| 64 | |
| 65 | ns = lyxml_ns_get(xml_ctx, prefix, prefix_len); |
| 66 | |
| 67 | if (!ns) { |
| 68 | *kw = YANG_NONE; |
| 69 | } else { |
| 70 | if (!IS_YIN_NS(ns->uri)) { |
| 71 | *kw = YANG_CUSTOM; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 76 | enum yang_keyword |
David Sedlák | c8dd80e | 2019-06-19 16:08:48 +0200 | [diff] [blame^] | 77 | yin_match_keyword(const char *data, size_t len) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 78 | { |
| 79 | if (!data || len == 0) { |
| 80 | return YANG_NONE; |
| 81 | } |
| 82 | |
| 83 | const char *start = data; |
David Sedlák | 5f8f033 | 2019-06-18 16:34:30 +0200 | [diff] [blame] | 84 | enum yang_keyword kw = lysp_match_kw(NULL, &data); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 85 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 86 | if (data - start == (long int)len) { |
| 87 | return kw; |
| 88 | } else { |
| 89 | return YANG_NONE; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 94 | enum YIN_ARGUMENT |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 95 | yin_match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 96 | { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 97 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 98 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 99 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 100 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 101 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 102 | #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] | 103 | #define IF_ARG_PREFIX_END } |
| 104 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 105 | switch (*name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 106 | case 'x': |
| 107 | already_read += 1; |
| 108 | IF_ARG("mlns", 4, YIN_ARG_XMLNS); |
| 109 | break; |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 110 | case 'c': |
| 111 | already_read += 1; |
| 112 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 113 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 114 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 115 | case 'd': |
| 116 | already_read += 1; |
| 117 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 118 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 119 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 120 | case 'm': |
| 121 | already_read += 1; |
| 122 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 123 | break; |
| 124 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 125 | case 'n': |
| 126 | already_read += 1; |
| 127 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 128 | break; |
| 129 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 130 | case 't': |
| 131 | already_read += 1; |
| 132 | IF_ARG_PREFIX("a", 1) |
| 133 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 134 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 135 | IF_ARG_PREFIX_END |
| 136 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 137 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 138 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 139 | case 'u': |
| 140 | already_read += 1; |
| 141 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 142 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 143 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 144 | case 'v': |
| 145 | already_read += 1; |
| 146 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 150 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 151 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 152 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 153 | } |
| 154 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 155 | #undef IF_ARG |
| 156 | #undef IF_ARG_PREFIX |
| 157 | #undef IF_ARG_PREFIX_END |
| 158 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 159 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 160 | } |
| 161 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 162 | /** |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 163 | * @brief parse yin argument, arg_val is unchanged if argument arg_type wasn't found. |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 164 | * |
| 165 | * @param[in] xml_ctx XML parser context. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 166 | * @param[in,out] data Data to read from. |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 167 | * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments). |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 168 | * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is specified as YIN_ARG_NONE. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 169 | * |
| 170 | * @return LY_ERR values. |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 171 | */ |
| 172 | static LY_ERR |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 173 | yin_parse_attribute(struct lyxml_context *xml_ctx, const char **data, enum YIN_ARGUMENT arg_type, const char **arg_val) |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 174 | { |
| 175 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 176 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 177 | struct yin_arg_record *argument_array = NULL, *argument_record = NULL, *iter = NULL; |
| 178 | const struct lyxml_ns *ns = NULL; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 179 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 180 | /* load all attributes first */ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 181 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 182 | LY_ARRAY_NEW_GOTO(xml_ctx->ctx, argument_array, argument_record, ret, cleanup); |
| 183 | ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len, |
| 184 | &argument_record->name, &argument_record->name_len); |
| 185 | LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 186 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 187 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 188 | argument_record->content = NULL; |
| 189 | argument_record->content_len = 0; |
| 190 | argument_record->dynamic_content = 0; |
| 191 | ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len, |
| 192 | &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 193 | LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup); |
| 194 | } |
| 195 | } |
| 196 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 197 | /* validation of attributes */ |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 198 | LY_ARRAY_FOR(argument_array, struct yin_arg_record, iter) { |
| 199 | ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len); |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 200 | if (ns && IS_YIN_NS(ns->uri)) { |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 201 | arg = yin_match_argument_name(iter->name, iter->name_len); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 202 | if (arg == YIN_ARG_NONE) { |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 203 | continue; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 204 | } else if (arg == arg_type) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 205 | if (iter->dynamic_content) { |
| 206 | *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content); |
| 207 | } else { |
| 208 | *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len); |
| 209 | LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup); |
| 210 | } |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 211 | } else { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 212 | if (iter->dynamic_content) { |
| 213 | free(iter->content); |
| 214 | } |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 215 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name); |
| 216 | ret = LY_EVALID; |
| 217 | goto cleanup; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 222 | cleanup: |
| 223 | LY_ARRAY_FREE(argument_array); |
| 224 | return ret; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 228 | * @brief Parse content of whole element as text. |
| 229 | * |
| 230 | * @param[in] xml_ctx Xml context. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 231 | * @param[in,out] data Data to read from. |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 232 | * @param[out] value Where content of element should be stored. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 233 | * |
| 234 | * @return LY_ERR values. |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 235 | */ |
| 236 | LY_ERR |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 237 | yin_parse_text_element(struct lyxml_context *xml_ctx, const char **data, const char **value) |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 238 | { |
| 239 | LY_ERR ret = LY_SUCCESS; |
| 240 | char *buf = NULL, *out = NULL; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 241 | const char *prefix = NULL, *name = NULL; |
| 242 | size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 243 | int dynamic = 0; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 244 | |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 245 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL); |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 246 | LY_CHECK_RET(ret); |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 247 | LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID); |
| 248 | |
| 249 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 250 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 251 | LY_CHECK_RET(ret); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 252 | if (dynamic) { |
| 253 | *value = lydict_insert_zc(xml_ctx->ctx, buf); |
| 254 | } else { |
| 255 | *value = lydict_insert(xml_ctx->ctx, out, out_len); |
| 256 | LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 257 | } |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 258 | } |
| 259 | |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 260 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 261 | LY_CHECK_RET(ret); |
| 262 | |
| 263 | return LY_SUCCESS; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @brief function to parse meta tags eg. elements with text element as child |
| 268 | * |
| 269 | * @param[in] xml_ctx Xml context. |
| 270 | * @param[in,out] data Data to read from. |
| 271 | * @param[out] value Where the content of meta tag should be stored. |
| 272 | * |
| 273 | * @return LY_ERR values. |
| 274 | */ |
| 275 | LY_ERR |
| 276 | yin_parse_meta_element(struct lyxml_context *xml_ctx, const char **data, const char **value) |
| 277 | { |
| 278 | LY_ERR ret = LY_SUCCESS; |
| 279 | char *buf = NULL, *out = NULL; |
| 280 | const char *prefix = NULL, *name = NULL; |
| 281 | size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0; |
| 282 | int dynamic = 0; |
| 283 | const struct lyxml_ns *ns = NULL; |
| 284 | enum YIN_ARGUMENT arg = YANG_NONE; |
| 285 | |
| 286 | ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL); |
| 287 | LY_CHECK_RET(ret); |
| 288 | LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID); |
| 289 | |
| 290 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 291 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as child of meta element."), LY_EINVAL); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 292 | |
| 293 | /* loop over all child elements and parse them */ |
| 294 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 295 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 296 | LY_CHECK_RET(ret); |
| 297 | if (name) { |
| 298 | ns = lyxml_ns_get(xml_ctx, prefix, prefix_len); |
| 299 | /* check if child element is from yin namespace, elements from other namespaces are silently ignored */ |
| 300 | if (IS_YIN_NS(ns->uri)) { |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 301 | arg = yin_match_argument_name(name, name_len); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 302 | if (arg == YIN_ARG_TEXT) { |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 303 | yin_parse_text_element(xml_ctx, data, value); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 304 | } else { |
| 305 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 306 | return LY_EVALID; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | } else { |
| 311 | /* end of meta element reached */ |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | } |
| 316 | |
David Sedlák | 6b7ac2d | 2019-03-22 14:40:29 +0100 | [diff] [blame] | 317 | return LY_SUCCESS; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 318 | } |
| 319 | |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 320 | /** |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 321 | * @brief Parse revision date. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 322 | * |
| 323 | * @param[in] xml_ctx Xml context. |
| 324 | * @param[in,out] data Data to read from. |
| 325 | * @param[in,out] rev Array to store the parsed value in. |
| 326 | * @param[in,out] exts Extension instances to add to. |
| 327 | * |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 328 | * @return LY_ERR values. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 329 | */ |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 330 | static LY_ERR |
| 331 | yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts) |
| 332 | { |
| 333 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 334 | const char *temp_rev; |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 335 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 336 | if (rev[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 337 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date"); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 338 | return LY_EVALID; |
| 339 | } |
| 340 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 341 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, &temp_rev)) |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 342 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 343 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
| 344 | |
| 345 | strcpy(rev, temp_rev); |
| 346 | lydict_remove(xml_ctx->ctx, temp_rev); |
| 347 | /* TODO extension */ |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 348 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 349 | return ret; |
| 350 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 351 | |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 352 | LY_ERR |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 353 | 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] | 354 | { |
| 355 | LY_ERR ret = LY_SUCCESS; |
| 356 | enum yang_keyword kw; |
| 357 | struct lysp_import *imp; |
| 358 | const char *prefix, *name; |
| 359 | size_t prefix_len, name_len; |
| 360 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 361 | /* allocate sized array for imports */ |
| 362 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 363 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 364 | /* parse import attributes */ |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 365 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name)); |
| 366 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 367 | while ((ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS && name != NULL)) { |
David Sedlák | c8dd80e | 2019-06-19 16:08:48 +0200 | [diff] [blame^] | 368 | kw = yin_match_keyword(name, name_len); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 369 | switch (kw) { |
| 370 | case YANG_PREFIX: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 371 | LY_CHECK_ERR_RET(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID); |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 372 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &imp->prefix)); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 373 | LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)xml_ctx, *imports, module_prefix, &imp->prefix), LY_EVALID); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 374 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 375 | case YANG_DESCRIPTION: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 376 | LY_CHECK_ERR_RET(imp->dsc, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 377 | yin_parse_meta_element(xml_ctx, data, &imp->dsc); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 378 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 379 | case YANG_REFERENCE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 380 | LY_CHECK_ERR_RET(imp->ref, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 381 | yin_parse_meta_element(xml_ctx, data, &imp->ref); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 382 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 383 | case YANG_REVISION_DATE: |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 384 | yin_parse_revision_date(xml_ctx, data, imp->rev, &imp->exts); |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 385 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 386 | case YANG_CUSTOM: |
| 387 | /* TODO parse extension */ |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 388 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 389 | default: |
David Sedlák | 916b190 | 2019-06-06 11:35:05 +0200 | [diff] [blame] | 390 | LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import"); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 391 | return LY_EVALID; |
| 392 | } |
| 393 | } |
| 394 | |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 395 | LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_MISSATTR, "prefix", "import"), LY_EVALID); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 396 | return ret; |
| 397 | } |
| 398 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 399 | LY_ERR |
| 400 | yin_parse_status(struct lyxml_context *xml_ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
| 401 | { |
| 402 | LY_ERR ret = LY_SUCCESS; |
| 403 | enum yang_keyword kw = YANG_NONE; |
| 404 | const char *value = NULL, *prefix = NULL, *name = NULL; |
| 405 | char *out; |
| 406 | size_t prefix_len = 0, name_len = 0, out_len = 0; |
| 407 | int dynamic = 0; |
| 408 | |
| 409 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 410 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 411 | return LY_EVALID; |
| 412 | } |
| 413 | |
| 414 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &value)); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 415 | if (strcmp(value, "current") == 0) { |
| 416 | *flags |= LYS_STATUS_CURR; |
| 417 | } else if (strcmp(value, "deprecated") == 0) { |
| 418 | *flags |= LYS_STATUS_DEPRC; |
| 419 | } else if (strcmp(value, "obsolete") == 0) { |
| 420 | *flags |= LYS_STATUS_OBSLT; |
| 421 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 422 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 423 | lydict_remove(xml_ctx->ctx, value); |
| 424 | return LY_EVALID; |
| 425 | } |
| 426 | lydict_remove(xml_ctx->ctx, value); |
| 427 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 428 | /* TODO check if dynamic was set to 1 in case of error */ |
| 429 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 430 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 431 | /* if there are any xml subelements parse them, unknown text content is silently ignored */ |
| 432 | if (ret == LY_EINVAL) { |
| 433 | /* load subelements */ |
| 434 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 435 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 436 | if (!name) { |
| 437 | break; |
| 438 | } |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 439 | |
David Sedlák | c8dd80e | 2019-06-19 16:08:48 +0200 | [diff] [blame^] | 440 | kw = yin_match_keyword(name, name_len); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 441 | switch (kw) { |
| 442 | case YANG_CUSTOM: |
| 443 | /* TODO parse extension instance */ |
| 444 | break; |
| 445 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 446 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status"); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 447 | } |
| 448 | } |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * @brief Parse the extension statement. |
| 457 | * |
| 458 | * @param[in] xml_ctx Xml context. |
| 459 | * @param[in,out] data Data to read from. |
| 460 | * @param[in,out] extensions Extensions to add to. |
| 461 | * |
| 462 | * @return LY_ERR values. |
| 463 | */ |
| 464 | LY_ERR |
| 465 | yin_parse_extension(struct lyxml_context *xml_ctx, const char **data, struct lysp_ext **extensions) |
| 466 | { |
| 467 | LY_ERR ret = LY_SUCCESS; |
| 468 | struct lysp_ext *ex; |
| 469 | const char *prefix = NULL, *name = NULL; |
| 470 | char *out = NULL; |
| 471 | size_t out_len = 0, prefix_len = 0, name_len = 0; |
| 472 | int dynamic = 0; |
| 473 | enum yang_keyword kw = YANG_NONE; |
| 474 | |
| 475 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *extensions, ex, LY_EMEM); |
| 476 | yin_parse_attribute(xml_ctx, data, YIN_ARG_NAME, &ex->name); |
| 477 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 478 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element."), LY_EINVAL); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 479 | |
| 480 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 481 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 482 | LY_CHECK_RET(ret); |
| 483 | if (!name) { |
| 484 | break; |
| 485 | } |
| 486 | |
David Sedlák | c8dd80e | 2019-06-19 16:08:48 +0200 | [diff] [blame^] | 487 | kw = yin_match_keyword(name, name_len); |
| 488 | yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL); |
| 489 | check_kw_ns(xml_ctx, prefix, prefix_len, &kw); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 490 | switch (kw) { |
| 491 | case YANG_ARGUMENT: |
| 492 | break; |
| 493 | case YANG_DESCRIPTION: |
| 494 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &ex->dsc)); |
| 495 | break; |
| 496 | case YANG_REFERENCE: |
| 497 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &ex->ref)); |
| 498 | break; |
| 499 | case YANG_STATUS: |
| 500 | LY_CHECK_RET(yin_parse_status(xml_ctx, data, &ex->flags, &ex->exts)); |
| 501 | break; |
| 502 | case YANG_CUSTOM: |
| 503 | /* TODO parse extension instance */ |
| 504 | break; |
| 505 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 506 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 507 | return LY_EVALID; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * @brief Parse extension instance. |
| 516 | * |
| 517 | * @param[in] |
| 518 | */ |
| 519 | LY_ERR |
| 520 | yin_parse_extension_instane(struct lyxml_context *xml_ctx, const char *data) |
| 521 | { |
| 522 | |
| 523 | return LY_SUCCESS; |
| 524 | } |
| 525 | |
| 526 | /** |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 527 | * @brief Parse module substatements. |
| 528 | * |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 529 | * @param[in] xml_ctx Xml context. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 530 | * @param[in,out] data Data to read from. |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 531 | * @param[out] mod Parsed module structure. |
David Sedlák | d5318ee | 2019-02-15 10:14:50 +0100 | [diff] [blame] | 532 | * |
| 533 | * @return LY_ERR values. |
| 534 | */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 535 | LY_ERR |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 536 | yin_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] | 537 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 538 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 539 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 540 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 541 | size_t prefix_len, name_len; |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 542 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 543 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 544 | char *buf = NULL, *out = NULL; |
| 545 | size_t buf_len = 0, out_len = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 546 | int dynamic = 0; |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 547 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 548 | yin_parse_attribute(xml_ctx, data, YIN_ARG_NAME, &(*mod)->mod->name); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 549 | LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Missing argument name of a module"), LY_EVALID); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 550 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 551 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(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] | 552 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 553 | /* loop over all elements and parse them */ |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 554 | while (xml_ctx->status != LYXML_END) { |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 555 | #define CHECK_ORDER(SECTION) \ |
| 556 | if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION |
| 557 | |
| 558 | switch (kw) { |
| 559 | /* module header */ |
| 560 | case YANG_NAMESPACE: |
| 561 | case YANG_PREFIX: |
| 562 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 563 | break; |
| 564 | case YANG_YANG_VERSION: |
| 565 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 566 | break; |
| 567 | /* linkage */ |
| 568 | case YANG_INCLUDE: |
| 569 | case YANG_IMPORT: |
| 570 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 571 | break; |
| 572 | /* meta */ |
| 573 | case YANG_ORGANIZATION: |
| 574 | case YANG_CONTACT: |
| 575 | case YANG_DESCRIPTION: |
| 576 | case YANG_REFERENCE: |
| 577 | CHECK_ORDER(Y_MOD_META); |
| 578 | break; |
| 579 | |
| 580 | /* revision */ |
| 581 | case YANG_REVISION: |
| 582 | CHECK_ORDER(Y_MOD_REVISION); |
| 583 | break; |
| 584 | /* body */ |
| 585 | case YANG_ANYDATA: |
| 586 | case YANG_ANYXML: |
| 587 | case YANG_AUGMENT: |
| 588 | case YANG_CHOICE: |
| 589 | case YANG_CONTAINER: |
| 590 | case YANG_DEVIATION: |
| 591 | case YANG_EXTENSION: |
| 592 | case YANG_FEATURE: |
| 593 | case YANG_GROUPING: |
| 594 | case YANG_IDENTITY: |
| 595 | case YANG_LEAF: |
| 596 | case YANG_LEAF_LIST: |
| 597 | case YANG_LIST: |
| 598 | case YANG_NOTIFICATION: |
| 599 | case YANG_RPC: |
| 600 | case YANG_TYPEDEF: |
| 601 | case YANG_USES: |
| 602 | case YANG_CUSTOM: |
| 603 | mod_stmt = Y_MOD_BODY; |
| 604 | break; |
| 605 | default: |
| 606 | /* error will be handled in the next switch */ |
| 607 | break; |
| 608 | } |
| 609 | #undef CHECK_ORDER |
| 610 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 611 | LY_CHECK_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] | 612 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 613 | if (name) { |
David Sedlák | c8dd80e | 2019-06-19 16:08:48 +0200 | [diff] [blame^] | 614 | kw = yin_match_keyword(name, name_len); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 615 | switch (kw) { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 616 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 617 | /* module header */ |
| 618 | case YANG_NAMESPACE: |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 619 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, &(*mod)->mod->ns)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 620 | break; |
| 621 | case YANG_PREFIX: |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 622 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &(*mod)->mod->prefix)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 623 | break; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 624 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 625 | /* linkage */ |
| 626 | case YANG_IMPORT: |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 627 | yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 628 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 629 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 630 | /* meta */ |
| 631 | case YANG_ORGANIZATION: |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 632 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->org)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 633 | break; |
| 634 | case YANG_CONTACT: |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 635 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->contact)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 636 | break; |
| 637 | case YANG_DESCRIPTION: |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 638 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->dsc)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 639 | break; |
| 640 | case YANG_REFERENCE: |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 641 | LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->ref)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 642 | break; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 643 | /* revision */ |
| 644 | |
| 645 | /*body */ |
| 646 | case YANG_EXTENSION: |
| 647 | LY_CHECK_RET(yin_parse_extension(xml_ctx, data, &(*mod)->extensions)); |
| 648 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 649 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 650 | default: |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 651 | return LY_EVALID; |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 652 | break; |
| 653 | } |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 657 | return LY_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 661 | 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] | 662 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 663 | LY_ERR ret = LY_SUCCESS; |
| 664 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 665 | struct lys_parser_ctx parser_ctx; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 666 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 667 | struct lysp_module *mod_p = NULL; |
| 668 | const char *prefix, *name; |
| 669 | size_t prefix_len, name_len; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 670 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 671 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 672 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 673 | xml_ctx->ctx = ctx; |
| 674 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 675 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 676 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 677 | 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] | 678 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | c8dd80e | 2019-06-19 16:08:48 +0200 | [diff] [blame^] | 679 | kw = yin_match_keyword(name, name_len); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 680 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 681 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 682 | ret = LY_EINVAL; |
| 683 | goto cleanup; |
| 684 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 685 | LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 686 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 687 | ret = LY_EVALID; |
| 688 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 689 | } |
| 690 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 691 | /* allocate module */ |
| 692 | mod_p = calloc(1, sizeof *mod_p); |
| 693 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 694 | mod_p->mod = mod; |
| 695 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 696 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 697 | /* parser module substatements */ |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 698 | ret = yin_parse_mod(xml_ctx, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 699 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 700 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 701 | mod_p->parsing = 0; |
| 702 | mod->parsed = mod_p; |
| 703 | |
| 704 | cleanup: |
| 705 | if (ret) { |
| 706 | lysp_module_free(mod_p); |
| 707 | } |
| 708 | |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 709 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 710 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 711 | } |