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