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 | 0025034 | 2019-06-21 14:19:39 +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 | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 50 | enum yang_keyword |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 51 | 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] | 52 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 53 | const char *start = NULL; |
| 54 | enum yang_keyword kw = YANG_NONE; |
| 55 | const struct lyxml_ns *ns = NULL; |
| 56 | |
| 57 | if (!name || name_len == 0) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 58 | return YANG_NONE; |
| 59 | } |
| 60 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 61 | ns = lyxml_ns_get(xml_ctx, prefix, prefix_len); |
| 62 | if (ns) { |
| 63 | if (!IS_YIN_NS(ns->uri)) { |
| 64 | return YANG_CUSTOM; |
| 65 | } |
| 66 | } else { |
| 67 | /* elements without namespace are automatically unknown */ |
| 68 | return YANG_NONE; |
| 69 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 70 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 71 | |
| 72 | start = name; |
| 73 | kw = lysp_match_kw(NULL, &name); |
| 74 | |
| 75 | if (name - start == (long int)name_len) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 76 | return kw; |
| 77 | } else { |
| 78 | return YANG_NONE; |
| 79 | } |
| 80 | } |
| 81 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 82 | enum YIN_ARGUMENT |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 83 | yin_match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 84 | { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 85 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 86 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 87 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 88 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 89 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 90 | #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] | 91 | #define IF_ARG_PREFIX_END } |
| 92 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 93 | switch (*name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 94 | case 'x': |
| 95 | already_read += 1; |
| 96 | IF_ARG("mlns", 4, YIN_ARG_XMLNS); |
| 97 | break; |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 98 | case 'c': |
| 99 | already_read += 1; |
| 100 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 101 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 102 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 103 | case 'd': |
| 104 | already_read += 1; |
| 105 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 106 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 107 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 108 | case 'm': |
| 109 | already_read += 1; |
| 110 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 111 | break; |
| 112 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 113 | case 'n': |
| 114 | already_read += 1; |
| 115 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 116 | break; |
| 117 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 118 | case 't': |
| 119 | already_read += 1; |
| 120 | IF_ARG_PREFIX("a", 1) |
| 121 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 122 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 123 | IF_ARG_PREFIX_END |
| 124 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 125 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 126 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 127 | case 'u': |
| 128 | already_read += 1; |
| 129 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 130 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 131 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 132 | case 'v': |
| 133 | already_read += 1; |
| 134 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 138 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 139 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 140 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 141 | } |
| 142 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 143 | #undef IF_ARG |
| 144 | #undef IF_ARG_PREFIX |
| 145 | #undef IF_ARG_PREFIX_END |
| 146 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 147 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 148 | } |
| 149 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 150 | static void free_arg_rec(struct lyxml_context *xml_ctx, struct yin_arg_record *record) { |
| 151 | (void)xml_ctx; /* unused */ |
| 152 | if (record->dynamic_content) { |
| 153 | free(record->content); |
| 154 | } |
| 155 | } |
| 156 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 157 | LY_ERR |
| 158 | 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] | 159 | { |
| 160 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 161 | struct yin_arg_record *argument_record = NULL; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 162 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 163 | /* load all attributes first */ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 164 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 165 | 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] | 166 | ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len, |
| 167 | &argument_record->name, &argument_record->name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 168 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 169 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 170 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 171 | argument_record->content = NULL; |
| 172 | argument_record->content_len = 0; |
| 173 | argument_record->dynamic_content = 0; |
| 174 | ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len, |
| 175 | &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 176 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 180 | cleanup: |
| 181 | if (ret != LY_SUCCESS) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 182 | FREE_ARRAY(xml_ctx, *args, free_arg_rec); |
| 183 | *args = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 184 | } |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | LY_ERR |
| 189 | yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type, const char **arg_val) |
| 190 | { |
| 191 | LY_ERR ret = LY_SUCCESS; |
| 192 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
| 193 | struct yin_arg_record *iter = NULL; |
| 194 | const struct lyxml_ns *ns = NULL; |
| 195 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 196 | /* validation of attributes */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 197 | LY_ARRAY_FOR(*args, struct yin_arg_record, iter) { |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 198 | ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 199 | /* yin arguments represented as attributes have no namespace, which in this case means no prefix */ |
| 200 | if (!iter->prefix) { |
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); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 207 | LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup); |
| 208 | /* string is no longer supposed to be freed when the sized array is freed */ |
| 209 | iter->dynamic_content = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 210 | } else { |
| 211 | *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len); |
| 212 | LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup); |
| 213 | } |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 214 | } else { |
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: |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 223 | return ret; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 224 | } |
| 225 | |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 226 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 227 | 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] | 228 | { |
| 229 | LY_ERR ret = LY_SUCCESS; |
| 230 | char *buf = NULL, *out = NULL; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 231 | const char *prefix = NULL, *name = NULL; |
| 232 | 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] | 233 | int dynamic = 0; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 234 | |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 235 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 236 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 237 | LY_CHECK_RET(ret); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 238 | if (dynamic) { |
| 239 | *value = lydict_insert_zc(xml_ctx->ctx, buf); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 240 | LY_CHECK_RET(!(*value), LY_EMEM); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 241 | } else { |
| 242 | *value = lydict_insert(xml_ctx->ctx, out, out_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 243 | LY_CHECK_RET(!(*value), LY_EMEM); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 244 | } |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 245 | } |
| 246 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 247 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 248 | /* probably should never happen, mixed content should be caught by lyxml_get_element call but |
| 249 | * I will just leave it here for now to be sure */ |
| 250 | LY_CHECK_RET(name, LY_EVALID); |
| 251 | return ret; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 252 | } |
| 253 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 254 | /** |
| 255 | * @brief function to parse meta tags eg. elements with text element as child |
| 256 | * |
| 257 | * @param[in] xml_ctx Xml context. |
| 258 | * @param[in] args Sized array of arguments of current elements. |
| 259 | * @param[in,out] data Data to read from. |
| 260 | * @param[out] value Where the content of meta tag should be stored. |
| 261 | * |
| 262 | * @return LY_ERR values. |
| 263 | */ |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 264 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 265 | 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] | 266 | { |
| 267 | LY_ERR ret = LY_SUCCESS; |
| 268 | char *buf = NULL, *out = NULL; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 269 | const char *prefix = NULL, *name = NULL; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 270 | size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0; |
| 271 | int dynamic = 0; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 272 | enum YIN_ARGUMENT arg = YANG_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 273 | struct yin_arg_record *subelem_args = NULL; |
| 274 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 275 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 276 | LY_CHECK_ERR_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected content of meta element."), LY_EVALID); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 277 | |
| 278 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 279 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element."), LY_EINVAL); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 280 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 281 | /* first element should be argument element <text> */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 282 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 283 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 284 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 285 | arg = yin_match_argument_name(name, name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 286 | LY_CHECK_ERR_GOTO(arg != YIN_ARG_TEXT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element."); |
| 287 | ret = LY_EINVAL, cleanup); |
| 288 | ret = yin_parse_text_element(xml_ctx, &subelem_args, data, value); |
| 289 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 290 | |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 291 | /* loop over all child elements and parse them */ |
| 292 | while (xml_ctx->status == LYXML_ELEMENT) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 293 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 294 | LY_CHECK_GOTO(ret, cleanup); |
| 295 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 296 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 297 | 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] | 298 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 299 | if (!name) { |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 300 | /* end of meta element reached */ |
| 301 | break; |
| 302 | } |
| 303 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 304 | switch (kw) { |
| 305 | case YANG_CUSTOM: |
| 306 | // TODO parse extension instance |
| 307 | break; |
| 308 | |
| 309 | default: |
| 310 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 311 | return LY_EVALID; |
| 312 | } |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 313 | } |
| 314 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 315 | cleanup: |
| 316 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 317 | return ret; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 318 | } |
| 319 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 320 | /** |
| 321 | * @brief Parse revision date. |
| 322 | * |
| 323 | * @param[in] xml_ctx Xml context. |
| 324 | * @param[in] args Sized array of arguments of current element. |
| 325 | * @param[in,out] data Data to read from. |
| 326 | * @param[in,out] rev Array to store the parsed value in. |
| 327 | * @param[in,out] exts Extension instances to add to. |
| 328 | * |
| 329 | * @return LY_ERR values. |
| 330 | */ |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 331 | static LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 332 | 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] | 333 | { |
| 334 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 335 | const char *temp_rev, *prefix = NULL, *name = NULL; |
| 336 | char *out; |
| 337 | struct yin_arg_record *subelem_args = NULL; |
| 338 | size_t prefix_len = 0, name_len = 0; |
| 339 | enum yang_keyword kw = YANG_NONE; |
| 340 | int dynamic; |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 341 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 342 | if (rev[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 343 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date"); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 344 | return LY_EVALID; |
| 345 | } |
| 346 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 347 | 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] | 348 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 349 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
| 350 | |
| 351 | strcpy(rev, temp_rev); |
| 352 | lydict_remove(xml_ctx->ctx, temp_rev); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 353 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 354 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 355 | ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic); |
| 356 | /* unknown element text content is ignored */ |
| 357 | if (ret == LY_EINVAL) { |
| 358 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 359 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 360 | LY_CHECK_GOTO(ret, cleanup); |
| 361 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 362 | LY_CHECK_GOTO(ret, cleanup); |
| 363 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
| 364 | |
| 365 | if (!name) { |
| 366 | /* end of revisin-date element reached */ |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | switch (kw) { |
| 371 | case YANG_CUSTOM: |
| 372 | // TODO parse extension instance |
| 373 | break; |
| 374 | |
| 375 | default: |
| 376 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 377 | return LY_EVALID; |
| 378 | } |
| 379 | } |
| 380 | } else { |
| 381 | /* load closing element */ |
| 382 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 383 | LY_CHECK_RET(name, LY_EINVAL); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | cleanup: |
| 388 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 389 | return ret; |
| 390 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 391 | |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 392 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 393 | 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] | 394 | { |
| 395 | LY_ERR ret = LY_SUCCESS; |
| 396 | enum yang_keyword kw; |
| 397 | struct lysp_import *imp; |
| 398 | const char *prefix, *name; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 399 | char *out; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 400 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 401 | size_t prefix_len, name_len; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 402 | int dynamic; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 403 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 404 | /* allocate new element in sized array for import */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 405 | 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] | 406 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 407 | /* parse import attributes */ |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 408 | LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name) != LY_SUCCESS, validation_err); |
| 409 | |
| 410 | ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic); |
| 411 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after import element"), LY_EINVAL); |
| 412 | |
| 413 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 414 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 415 | LY_CHECK_GOTO(ret, validation_err); |
| 416 | if (!name) { |
| 417 | /* end of import element reached */ |
| 418 | break; |
| 419 | } |
| 420 | LY_CHECK_GOTO(yin_load_attributes(xml_ctx, data, &subelem_args), validation_err); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 421 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 422 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 423 | switch (kw) { |
| 424 | case YANG_PREFIX: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 425 | LY_CHECK_ERR_GOTO(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), validation_err); |
| 426 | LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, &subelem_args, YIN_ARG_VALUE, &imp->prefix) != LY_SUCCESS, validation_err); |
| 427 | 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] | 428 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 429 | case YANG_DESCRIPTION: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 430 | LY_CHECK_ERR_GOTO(imp->dsc, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "description"), validation_err); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 431 | LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->dsc), validation_err); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 432 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 433 | case YANG_REFERENCE: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 434 | LY_CHECK_ERR_GOTO(imp->ref, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "reference"), validation_err); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 435 | LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->ref), validation_err); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 436 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 437 | case YANG_REVISION_DATE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 438 | LY_CHECK_GOTO(yin_parse_revision_date(xml_ctx, &subelem_args, data, imp->rev, &imp->exts), validation_err); |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 439 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 440 | case YANG_CUSTOM: |
| 441 | /* TODO parse extension */ |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 442 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 443 | default: |
David Sedlák | 916b190 | 2019-06-06 11:35:05 +0200 | [diff] [blame] | 444 | 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] | 445 | goto validation_err; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 446 | } |
David Sedlák | 4af209e | 2019-06-21 14:24:50 +0200 | [diff] [blame] | 447 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 448 | subelem_args = NULL; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 449 | } |
| 450 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 451 | LY_CHECK_ERR_GOTO(!imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_MISSATTR, "prefix", "import"), validation_err); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 452 | return ret; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 453 | |
| 454 | validation_err: |
David Sedlák | 4af209e | 2019-06-21 14:24:50 +0200 | [diff] [blame] | 455 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 456 | return LY_EVALID; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 457 | } |
| 458 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 459 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 460 | 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] | 461 | { |
| 462 | LY_ERR ret = LY_SUCCESS; |
| 463 | enum yang_keyword kw = YANG_NONE; |
| 464 | const char *value = NULL, *prefix = NULL, *name = NULL; |
| 465 | char *out; |
| 466 | size_t prefix_len = 0, name_len = 0, out_len = 0; |
| 467 | int dynamic = 0; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 468 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 469 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 470 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 471 | return LY_EVALID; |
| 472 | } |
| 473 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 474 | 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] | 475 | if (strcmp(value, "current") == 0) { |
| 476 | *flags |= LYS_STATUS_CURR; |
| 477 | } else if (strcmp(value, "deprecated") == 0) { |
| 478 | *flags |= LYS_STATUS_DEPRC; |
| 479 | } else if (strcmp(value, "obsolete") == 0) { |
| 480 | *flags |= LYS_STATUS_OBSLT; |
| 481 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 482 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 483 | lydict_remove(xml_ctx->ctx, value); |
| 484 | return LY_EVALID; |
| 485 | } |
| 486 | lydict_remove(xml_ctx->ctx, value); |
| 487 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 488 | /* TODO check if dynamic was set to 1 in case of error */ |
| 489 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 490 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 491 | /* if there are any xml subelements parse them, unknown text content is silently ignored */ |
| 492 | if (ret == LY_EINVAL) { |
| 493 | /* load subelements */ |
| 494 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 495 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 496 | if (!name) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 497 | /* end of status reached */ |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 498 | break; |
| 499 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 500 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 501 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 502 | 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] | 503 | switch (kw) { |
| 504 | case YANG_CUSTOM: |
| 505 | /* TODO parse extension instance */ |
| 506 | break; |
| 507 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 508 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status"); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 509 | ret = LY_EVALID; |
| 510 | goto cleanup; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 511 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 512 | |
| 513 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 514 | subelem_args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 515 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 516 | } else { |
| 517 | /* load closing element */ |
| 518 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 519 | LY_CHECK_RET(name, LY_EINVAL); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | return ret; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 524 | |
| 525 | cleanup: |
| 526 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 527 | return ret; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 528 | } |
| 529 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 530 | LY_ERR |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 531 | yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags, struct lysp_ext **extensions) |
| 532 | { |
| 533 | LY_ERR ret = LY_SUCCESS; |
| 534 | const char *temp_val = NULL, *prefix, *name; |
| 535 | char *out; |
| 536 | size_t prefix_len, name_len; |
| 537 | struct yin_arg_record *subelem_args = NULL; |
| 538 | int dynamic; |
| 539 | enum yang_keyword kw = YANG_NONE; |
| 540 | |
| 541 | if (*flags & LYS_YINELEM_MASK) { |
| 542 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "yin-element"); |
| 543 | return LY_EVALID; |
| 544 | } |
| 545 | |
| 546 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_VALUE, &temp_val)); |
| 547 | if (strcmp(temp_val, "true") == 0) { |
| 548 | *flags |= LYS_YINELEM_TRUE; |
| 549 | } else if (strcmp(temp_val, "false") == 0) { |
| 550 | *flags |= LYS_YINELEM_FALSE; |
| 551 | } else { |
| 552 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element"); |
| 553 | lydict_remove(xml_ctx->ctx, temp_val); |
| 554 | return LY_EVALID; |
| 555 | } |
| 556 | lydict_remove(xml_ctx->ctx, temp_val); |
| 557 | |
| 558 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 559 | ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic); |
| 560 | /* unknown element text content is ignored */ |
| 561 | if (ret == LY_EINVAL) { |
| 562 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 563 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 564 | LY_CHECK_GOTO(ret, cleanup); |
| 565 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 566 | LY_CHECK_GOTO(ret, cleanup); |
| 567 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
| 568 | |
| 569 | if (!name) { |
| 570 | /* end of yin-element element reached */ |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | switch (kw) { |
| 575 | case YANG_CUSTOM: |
| 576 | // TODO parse extension instance |
| 577 | break; |
| 578 | |
| 579 | default: |
| 580 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 581 | return LY_EVALID; |
| 582 | } |
| 583 | } |
| 584 | } else { |
| 585 | /* load closing element */ |
| 586 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 587 | LY_CHECK_RET(name, LY_EINVAL); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | cleanup: |
| 592 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | LY_ERR |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame^] | 597 | yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, |
| 598 | uint16_t *flags, const char **argument, struct lysp_ext **extensions) |
| 599 | { |
| 600 | LY_ERR ret = LY_SUCCESS; |
| 601 | const char *name, *prefix; |
| 602 | size_t name_len, prefix_len; |
| 603 | struct yin_arg_record *subelem_args = NULL; |
| 604 | char *out; |
| 605 | int dynamic; |
| 606 | enum yang_keyword kw = YANG_NONE; |
| 607 | |
| 608 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_NAME, argument)); |
| 609 | |
| 610 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 611 | ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic); |
| 612 | /* unknown element text content is ignored */ |
| 613 | if (ret == LY_EINVAL) { |
| 614 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 615 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 616 | LY_CHECK_GOTO(ret, cleanup); |
| 617 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 618 | LY_CHECK_GOTO(ret, cleanup); |
| 619 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
| 620 | |
| 621 | if (!name) { |
| 622 | /* end of yin-element element reached */ |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | switch (kw) { |
| 627 | case YANG_YIN_ELEMENT: |
| 628 | yin_parse_yin_element_element(xml_ctx, &subelem_args, data, flags, extensions); |
| 629 | break; |
| 630 | case YANG_CUSTOM: |
| 631 | // TODO parse extension instance |
| 632 | break; |
| 633 | |
| 634 | default: |
| 635 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 636 | return LY_EVALID; |
| 637 | } |
| 638 | } |
| 639 | } else { |
| 640 | /* load closing element */ |
| 641 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 642 | LY_CHECK_RET(name, LY_EINVAL); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | cleanup: |
| 647 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 652 | 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] | 653 | { |
| 654 | LY_ERR ret = LY_SUCCESS; |
| 655 | struct lysp_ext *ex; |
| 656 | const char *prefix = NULL, *name = NULL; |
| 657 | char *out = NULL; |
| 658 | size_t out_len = 0, prefix_len = 0, name_len = 0; |
| 659 | int dynamic = 0; |
| 660 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 661 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 662 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 663 | LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *extensions, ex, ret, cleanup); |
| 664 | ret = yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name); |
| 665 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 666 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 667 | LY_CHECK_ERR_GOTO(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element."); |
| 668 | ret = LY_EINVAL, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 669 | |
| 670 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 671 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 672 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 673 | if (!name) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 674 | /* end of extension element reached */ |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 675 | break; |
| 676 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 677 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 678 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 679 | 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] | 680 | switch (kw) { |
| 681 | case YANG_ARGUMENT: |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame^] | 682 | ret = yin_parse_argument_element(xml_ctx, &subelem_args, data, &ex->flags, &ex->argument, extensions); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 683 | break; |
| 684 | case YANG_DESCRIPTION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 685 | ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc); |
| 686 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 687 | break; |
| 688 | case YANG_REFERENCE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 689 | ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref); |
| 690 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 691 | break; |
| 692 | case YANG_STATUS: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 693 | ret = yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts); |
| 694 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 695 | break; |
| 696 | case YANG_CUSTOM: |
| 697 | /* TODO parse extension instance */ |
| 698 | break; |
| 699 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 700 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension"); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 701 | ret = LY_EVALID; |
| 702 | goto cleanup; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 703 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 704 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 705 | subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 706 | } |
| 707 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 708 | cleanup: |
| 709 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 710 | return ret; |
| 711 | } |
| 712 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 713 | /** |
| 714 | * @brief Parse module substatements. |
| 715 | * |
| 716 | * @param[in] xml_ctx Xml context. |
| 717 | * @param[in,out] data Data to read from. |
| 718 | * @param[out] mod Parsed module structure. |
| 719 | * |
| 720 | * @return LY_ERR values. |
| 721 | */ |
| 722 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 723 | 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] | 724 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 725 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 726 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 727 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 728 | size_t prefix_len, name_len; |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 729 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 730 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 731 | char *buf = NULL, *out = NULL; |
| 732 | size_t buf_len = 0, out_len = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 733 | int dynamic = 0; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 734 | struct yin_arg_record *substmt_args = NULL; |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 735 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 736 | 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] | 737 | 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] | 738 | 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] | 739 | 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] | 740 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 741 | /* loop over all elements and parse them */ |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 742 | while (xml_ctx->status != LYXML_END) { |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 743 | #define CHECK_ORDER(SECTION) \ |
| 744 | if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION |
| 745 | |
| 746 | switch (kw) { |
| 747 | /* module header */ |
| 748 | case YANG_NAMESPACE: |
| 749 | case YANG_PREFIX: |
| 750 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 751 | break; |
| 752 | case YANG_YANG_VERSION: |
| 753 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 754 | break; |
| 755 | /* linkage */ |
| 756 | case YANG_INCLUDE: |
| 757 | case YANG_IMPORT: |
| 758 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 759 | break; |
| 760 | /* meta */ |
| 761 | case YANG_ORGANIZATION: |
| 762 | case YANG_CONTACT: |
| 763 | case YANG_DESCRIPTION: |
| 764 | case YANG_REFERENCE: |
| 765 | CHECK_ORDER(Y_MOD_META); |
| 766 | break; |
| 767 | |
| 768 | /* revision */ |
| 769 | case YANG_REVISION: |
| 770 | CHECK_ORDER(Y_MOD_REVISION); |
| 771 | break; |
| 772 | /* body */ |
| 773 | case YANG_ANYDATA: |
| 774 | case YANG_ANYXML: |
| 775 | case YANG_AUGMENT: |
| 776 | case YANG_CHOICE: |
| 777 | case YANG_CONTAINER: |
| 778 | case YANG_DEVIATION: |
| 779 | case YANG_EXTENSION: |
| 780 | case YANG_FEATURE: |
| 781 | case YANG_GROUPING: |
| 782 | case YANG_IDENTITY: |
| 783 | case YANG_LEAF: |
| 784 | case YANG_LEAF_LIST: |
| 785 | case YANG_LIST: |
| 786 | case YANG_NOTIFICATION: |
| 787 | case YANG_RPC: |
| 788 | case YANG_TYPEDEF: |
| 789 | case YANG_USES: |
| 790 | case YANG_CUSTOM: |
| 791 | mod_stmt = Y_MOD_BODY; |
| 792 | break; |
| 793 | default: |
| 794 | /* error will be handled in the next switch */ |
| 795 | break; |
| 796 | } |
| 797 | #undef CHECK_ORDER |
| 798 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 799 | 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] | 800 | if (name) { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 801 | LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args)); |
| 802 | 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] | 803 | switch (kw) { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 804 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 805 | /* module header */ |
| 806 | case YANG_NAMESPACE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 807 | ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns); |
| 808 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 809 | break; |
| 810 | case YANG_PREFIX: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 811 | ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix); |
| 812 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 813 | break; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 814 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 815 | /* linkage */ |
| 816 | case YANG_IMPORT: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 817 | ret = yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports); |
| 818 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 819 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 820 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 821 | /* meta */ |
| 822 | case YANG_ORGANIZATION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 823 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org); |
| 824 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 825 | break; |
| 826 | case YANG_CONTACT: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 827 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact); |
| 828 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 829 | break; |
| 830 | case YANG_DESCRIPTION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 831 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc); |
| 832 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 833 | break; |
| 834 | case YANG_REFERENCE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 835 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref); |
| 836 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 837 | break; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 838 | /* revision */ |
| 839 | |
| 840 | /*body */ |
| 841 | case YANG_EXTENSION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 842 | ret = yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions); |
| 843 | LY_CHECK_GOTO(ret, error); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 844 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 845 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 846 | default: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 847 | ret = LY_EVALID; |
| 848 | goto error; |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 849 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 850 | FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 851 | substmt_args = NULL; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 855 | return LY_SUCCESS; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 856 | |
| 857 | error: |
| 858 | FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec); |
| 859 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 863 | 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] | 864 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 865 | LY_ERR ret = LY_SUCCESS; |
| 866 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 867 | struct lys_parser_ctx parser_ctx; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 868 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 869 | struct lysp_module *mod_p = NULL; |
| 870 | const char *prefix, *name; |
| 871 | size_t prefix_len, name_len; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 872 | struct yin_arg_record *args = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 873 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 874 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 875 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 876 | xml_ctx->ctx = ctx; |
| 877 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 878 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 879 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 880 | ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 881 | LY_CHECK_GOTO(ret, cleanup); |
| 882 | ret = yin_load_attributes(xml_ctx, &data, &args); |
| 883 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 884 | 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] | 885 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 886 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 887 | ret = LY_EINVAL; |
| 888 | goto cleanup; |
| 889 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 890 | 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] | 891 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 892 | ret = LY_EVALID; |
| 893 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 894 | } |
| 895 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 896 | /* allocate module */ |
| 897 | mod_p = calloc(1, sizeof *mod_p); |
| 898 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 899 | mod_p->mod = mod; |
| 900 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 901 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 902 | /* parse module substatements */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 903 | ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 904 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 905 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 906 | mod_p->parsing = 0; |
| 907 | mod->parsed = mod_p; |
| 908 | |
| 909 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 910 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 911 | lysp_module_free(mod_p); |
| 912 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 913 | FREE_ARRAY(xml_ctx, args, free_arg_rec); |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 914 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 915 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 916 | } |