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 | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 16 | #include <assert.h> |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 20 | #include <string.h> |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 21 | #include <stdbool.h> |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 22 | |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 23 | #include "context.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 24 | #include "dict.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 25 | #include "xml.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 26 | #include "tree.h" |
| 27 | #include "tree_schema.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 28 | #include "tree_schema_internal.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 29 | #include "parser_yin.h" |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 30 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 31 | /** |
| 32 | * @brief check if given string is URI of yin namespace. |
| 33 | * @param ns Namespace URI to check. |
| 34 | * |
| 35 | * @return true if ns equals YIN_NS_URI false otherwise. |
| 36 | */ |
| 37 | #define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0) |
| 38 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 39 | const char *const yin_attr_list[] = { |
| 40 | [YIN_ARG_NAME] = "name", |
| 41 | [YIN_ARG_TARGET_NODE] = "target-node", |
| 42 | [YIN_ARG_MODULE] = "module", |
| 43 | [YIN_ARG_VALUE] = "value", |
| 44 | [YIN_ARG_TEXT] = "text", |
| 45 | [YIN_ARG_CONDITION] = "condition", |
| 46 | [YIN_ARG_URI] = "uri", |
| 47 | [YIN_ARG_DATE] = "date", |
| 48 | [YIN_ARG_TAG] = "tag", |
| 49 | [YIN_ARG_XMLNS] = "xmlns", |
| 50 | }; |
| 51 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 52 | enum yang_keyword |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 53 | 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] | 54 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 55 | const char *start = NULL; |
| 56 | enum yang_keyword kw = YANG_NONE; |
| 57 | const struct lyxml_ns *ns = NULL; |
| 58 | |
| 59 | if (!name || name_len == 0) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 60 | return YANG_NONE; |
| 61 | } |
| 62 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 63 | ns = lyxml_ns_get(xml_ctx, prefix, prefix_len); |
| 64 | if (ns) { |
| 65 | if (!IS_YIN_NS(ns->uri)) { |
| 66 | return YANG_CUSTOM; |
| 67 | } |
| 68 | } else { |
| 69 | /* elements without namespace are automatically unknown */ |
| 70 | return YANG_NONE; |
| 71 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 72 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 73 | |
| 74 | start = name; |
| 75 | kw = lysp_match_kw(NULL, &name); |
| 76 | |
| 77 | if (name - start == (long int)name_len) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 78 | return kw; |
| 79 | } else { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 80 | if (strncmp(start, "text", name_len) == 0) { |
| 81 | return YIN_TEXT; |
| 82 | } else if (strncmp(start, "value", name_len) == 0) { |
| 83 | return YIN_VALUE; |
| 84 | } else { |
| 85 | return YANG_NONE; |
| 86 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 90 | enum YIN_ARGUMENT |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 91 | yin_match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 92 | { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 93 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 94 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 95 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 96 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 97 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 98 | #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] | 99 | #define IF_ARG_PREFIX_END } |
| 100 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 101 | switch (*name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 102 | case 'x': |
| 103 | already_read += 1; |
| 104 | IF_ARG("mlns", 4, YIN_ARG_XMLNS); |
| 105 | break; |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 106 | case 'c': |
| 107 | already_read += 1; |
| 108 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 109 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 110 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 111 | case 'd': |
| 112 | already_read += 1; |
| 113 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 114 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 115 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 116 | case 'm': |
| 117 | already_read += 1; |
| 118 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 119 | break; |
| 120 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 121 | case 'n': |
| 122 | already_read += 1; |
| 123 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 124 | break; |
| 125 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 126 | case 't': |
| 127 | already_read += 1; |
| 128 | IF_ARG_PREFIX("a", 1) |
| 129 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 130 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 131 | IF_ARG_PREFIX_END |
| 132 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
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 'u': |
| 136 | already_read += 1; |
| 137 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 138 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 139 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 140 | case 'v': |
| 141 | already_read += 1; |
| 142 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 146 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 147 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 148 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 149 | } |
| 150 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 151 | #undef IF_ARG |
| 152 | #undef IF_ARG_PREFIX |
| 153 | #undef IF_ARG_PREFIX_END |
| 154 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 155 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 156 | } |
| 157 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 158 | static void free_arg_rec(struct lyxml_context *xml_ctx, struct yin_arg_record *record) { |
| 159 | (void)xml_ctx; /* unused */ |
| 160 | if (record->dynamic_content) { |
| 161 | free(record->content); |
| 162 | } |
| 163 | } |
| 164 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 165 | LY_ERR |
| 166 | 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] | 167 | { |
| 168 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 169 | struct yin_arg_record *argument_record = NULL; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 170 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 171 | /* load all attributes first */ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 172 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 173 | 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] | 174 | ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len, |
| 175 | &argument_record->name, &argument_record->name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 176 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 177 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 178 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 179 | argument_record->content = NULL; |
| 180 | argument_record->content_len = 0; |
| 181 | argument_record->dynamic_content = 0; |
| 182 | ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len, |
| 183 | &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 184 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 188 | cleanup: |
| 189 | if (ret != LY_SUCCESS) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 190 | FREE_ARRAY(xml_ctx, *args, free_arg_rec); |
| 191 | *args = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 192 | } |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | LY_ERR |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 197 | yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type, |
| 198 | const char **arg_val, uint8_t flags, enum yang_keyword current_element) |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 199 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 200 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
| 201 | struct yin_arg_record *iter = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 202 | bool found = false; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 203 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 204 | /* validation of attributes */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 205 | LY_ARRAY_FOR(*args, struct yin_arg_record, iter) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 206 | /* yin arguments represented as attributes have no namespace, which in this case means no prefix */ |
| 207 | if (!iter->prefix) { |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 208 | arg = yin_match_argument_name(iter->name, iter->name_len); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 209 | if (arg == YIN_ARG_NONE) { |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 210 | continue; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 211 | } else if (arg == arg_type) { |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 212 | found = true; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 213 | if (iter->dynamic_content) { |
| 214 | *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 215 | LY_CHECK_RET(!(*arg_val), LY_EMEM); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 216 | /* string is no longer supposed to be freed when the sized array is freed */ |
| 217 | iter->dynamic_content = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 218 | } else { |
| 219 | *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 220 | LY_CHECK_RET(!(*arg_val), LY_EMEM); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 221 | } |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 222 | } else { |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 223 | LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\" of %s element.", iter->name_len, iter->name, ly_stmt2str(current_element)); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 224 | return LY_EVALID; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 229 | if (flags & YIN_ARG_MANDATORY && !found) { |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 230 | LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute \"%s\" of %s element.", yin_attr2str(arg_type), ly_stmt2str(current_element)); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 231 | return LY_EVALID; |
| 232 | } |
| 233 | |
| 234 | return LY_SUCCESS; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 235 | } |
| 236 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 237 | /** |
| 238 | * @brief get record with given type. |
| 239 | * |
| 240 | * @param[in] type Type of wanted record. |
| 241 | * @param[in] array_size Size of array. |
| 242 | * @param[in] array Searched array. |
| 243 | * |
| 244 | * @return Pointer to desired record on success, NULL if element is not in the array. |
| 245 | */ |
| 246 | struct yin_subelement * |
| 247 | get_record(enum yang_keyword type, size_t array_size, struct yin_subelement *array) |
| 248 | { |
| 249 | for (size_t i = 0; i < array_size; ++i) { |
| 250 | if (array[i].type == type) { |
| 251 | return &array[i]; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return NULL; |
| 256 | } |
| 257 | |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 258 | LY_ERR |
| 259 | yin_check_subelem_mandatory_constraint(struct lyxml_context *xml_ctx, struct yin_subelement *subelem_info, |
| 260 | size_t subelem_info_size, enum yang_keyword current_element) |
| 261 | { |
| 262 | for (size_t i = 0; i < subelem_info_size; ++i) { |
| 263 | /* if there is element that is mandatory and isn't parsed log error and rturn LY_EVALID */ |
| 264 | if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) { |
| 265 | LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Missing mandatory subelement %s of %s element", ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element)); |
| 266 | return LY_EVALID; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return LY_SUCCESS; |
| 271 | } |
| 272 | |
| 273 | LY_ERR |
| 274 | yin_check_subelem_first_constraint(struct lyxml_context *xml_ctx, struct yin_subelement *subelem_info, |
| 275 | size_t subelem_info_size, enum yang_keyword current_element, struct yin_subelement *exp_first) |
| 276 | { |
| 277 | for (size_t i = 0; i < subelem_info_size; ++i) { |
| 278 | if (subelem_info[i].flags & YIN_SUBELEM_PARSED) { |
| 279 | LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Subelement %s of %s element must be defined as first subelement.", exp_first->type, current_element); |
| 280 | return LY_EVALID; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return LY_SUCCESS; |
| 285 | } |
| 286 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 287 | /** |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 288 | * @brief Generic function for content parsing |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 289 | * |
| 290 | * @param[in,out] xml_ctx Xml context. |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 291 | * @param[in] subelem_info array of valid subelement types and meta information, |
| 292 | * array must be ordered by subelem_info->type in ascending order. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 293 | * @param[in] subelem_info_size Size of subelem_info array. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 294 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 295 | * @param[in] current_element Type of current element. |
| 296 | * @param[out] Where the text content of element should be stored. Text content is ignored if set to NULL. |
| 297 | * @param[in] exts Extension instance to add to. Can be null if element cannot have extension as subelement. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 298 | * @return LY_ERR values. |
| 299 | */ |
| 300 | LY_ERR |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 301 | yin_parse_content(struct lyxml_context *xml_ctx, struct yin_subelement *subelem_info, size_t subelem_info_size, |
| 302 | const char **data, enum yang_keyword current_element, const char **text_content, struct lysp_ext_instance **exts) |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 303 | { |
| 304 | LY_ERR ret = LY_SUCCESS; |
| 305 | struct sized_string prefix, name; |
| 306 | char *out; |
| 307 | size_t out_len; |
| 308 | int dynamic; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 309 | struct yin_arg_record *subelem_attrs = NULL; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 310 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 311 | struct yin_subelement *subelem_info_rec = NULL; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 312 | |
| 313 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 314 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 315 | /* current element has subelements as content */ |
| 316 | if (ret == LY_EINVAL) { |
| 317 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 318 | ret = lyxml_get_element(xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 319 | LY_CHECK_GOTO(ret, cleanup); |
| 320 | if (!name.value) { |
| 321 | /* end of current element reached */ |
| 322 | break; |
| 323 | } |
| 324 | ret = yin_load_attributes(xml_ctx, data, &subelem_attrs); |
| 325 | LY_CHECK_GOTO(ret, cleanup); |
| 326 | kw = yin_match_keyword(xml_ctx, name.value, name.len, prefix.value, prefix.len); |
| 327 | |
| 328 | /* check if this element can be child of current element */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 329 | subelem_info_rec = get_record(kw, subelem_info_size, subelem_info); |
| 330 | if (!subelem_info_rec) { |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 331 | LOGVAL_PARSER(xml_ctx, LY_VCODE_UNEXP_SUBELEM, name.len, name.value, ly_stmt2str(current_element)); |
| 332 | ret = LY_EVALID; |
| 333 | goto cleanup; |
| 334 | } |
| 335 | |
| 336 | /* TODO macro to check order */ |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 337 | /* if element is unique and already defined log error */ |
| 338 | if (subelem_info_rec->flags & YIN_SUBELEM_UNIQUE & YIN_SUBELEM_PARSED) { |
| 339 | LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Redefinition of %s element in %s element.", kw, current_element); |
| 340 | return LY_EVALID; |
| 341 | } |
| 342 | if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) { |
| 343 | yin_check_subelem_first_constraint(xml_ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec); |
| 344 | } |
| 345 | subelem_info_rec->flags |= YIN_SUBELEM_PARSED; |
| 346 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 347 | switch (kw) { |
| 348 | case YANG_CUSTOM: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 349 | /* TODO write function to calculate index instead of hardcoded 0 */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 350 | ret = yin_parse_extension_instance(xml_ctx, &subelem_attrs, data, name2fullname(name.value, prefix.len), |
| 351 | namelen2fulllen(name.len, prefix.len), |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 352 | kw2lyext_substmt(current_element), 0, exts); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 353 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 354 | break; |
| 355 | case YANG_ACTION: |
| 356 | break; |
| 357 | case YANG_ANYDATA: |
| 358 | break; |
| 359 | case YANG_ANYXML: |
| 360 | break; |
| 361 | case YANG_ARGUMENT: |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 362 | ret = yin_parse_argument_element(xml_ctx, &subelem_attrs, data, (struct yin_argument_meta *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 363 | break; |
| 364 | case YANG_AUGMENT: |
| 365 | break; |
| 366 | case YANG_BASE: |
| 367 | break; |
| 368 | case YANG_BELONGS_TO: |
| 369 | break; |
| 370 | case YANG_BIT: |
| 371 | break; |
| 372 | case YANG_CASE: |
| 373 | break; |
| 374 | case YANG_CHOICE: |
| 375 | break; |
| 376 | case YANG_CONFIG: |
| 377 | break; |
| 378 | case YANG_CONTACT: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 379 | case YANG_DESCRIPTION: |
| 380 | case YANG_ORGANIZATION: |
| 381 | case YANG_REFERENCE: |
| 382 | ret = yin_parse_meta_element(xml_ctx, data, kw, (const char **)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 383 | break; |
| 384 | case YANG_CONTAINER: |
| 385 | break; |
| 386 | case YANG_DEFAULT: |
| 387 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 388 | case YANG_DEVIATE: |
| 389 | break; |
| 390 | case YANG_DEVIATION: |
| 391 | break; |
| 392 | case YANG_ENUM: |
| 393 | break; |
| 394 | case YANG_ERROR_APP_TAG: |
| 395 | break; |
| 396 | case YANG_ERROR_MESSAGE: |
| 397 | break; |
| 398 | case YANG_EXTENSION: |
| 399 | break; |
| 400 | case YANG_FEATURE: |
| 401 | break; |
| 402 | case YANG_FRACTION_DIGITS: |
| 403 | break; |
| 404 | case YANG_GROUPING: |
| 405 | break; |
| 406 | case YANG_IDENTITY: |
| 407 | break; |
| 408 | case YANG_IF_FEATURE: |
| 409 | break; |
| 410 | case YANG_IMPORT: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 411 | ret = yin_parse_import(xml_ctx, &subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 412 | break; |
| 413 | case YANG_INCLUDE: |
| 414 | break; |
| 415 | case YANG_INPUT: |
| 416 | break; |
| 417 | case YANG_KEY: |
| 418 | break; |
| 419 | case YANG_LEAF: |
| 420 | break; |
| 421 | case YANG_LEAF_LIST: |
| 422 | break; |
| 423 | case YANG_LENGTH: |
| 424 | break; |
| 425 | case YANG_LIST: |
| 426 | break; |
| 427 | case YANG_MANDATORY: |
| 428 | break; |
| 429 | case YANG_MAX_ELEMENTS: |
| 430 | break; |
| 431 | case YANG_MIN_ELEMENTS: |
| 432 | break; |
| 433 | case YANG_MODIFIER: |
| 434 | break; |
| 435 | case YANG_MODULE: |
| 436 | break; |
| 437 | case YANG_MUST: |
| 438 | break; |
| 439 | case YANG_NAMESPACE: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 440 | ret = yin_parse_simple_element(xml_ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest, |
| 441 | YIN_ARG_URI, YIN_ARG_MANDATORY, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 442 | break; |
| 443 | case YANG_NOTIFICATION: |
| 444 | break; |
| 445 | case YANG_ORDERED_BY: |
| 446 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 447 | case YANG_OUTPUT: |
| 448 | break; |
| 449 | case YANG_PATH: |
| 450 | break; |
| 451 | case YANG_PATTERN: |
| 452 | break; |
| 453 | case YANG_POSITION: |
| 454 | break; |
| 455 | case YANG_PREFIX: |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 456 | ret = yin_parse_prefix(xml_ctx, subelem_attrs, (const char **)subelem_info_rec->dest, data, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 457 | break; |
| 458 | case YANG_PRESENCE: |
| 459 | break; |
| 460 | case YANG_RANGE: |
| 461 | break; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 462 | case YANG_REFINE: |
| 463 | break; |
| 464 | case YANG_REQUIRE_INSTANCE: |
| 465 | break; |
| 466 | case YANG_REVISION: |
| 467 | break; |
| 468 | case YANG_REVISION_DATE: |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 469 | ret = yin_parse_revision_date(xml_ctx, &subelem_attrs, data, (char *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 470 | break; |
| 471 | case YANG_RPC: |
| 472 | break; |
| 473 | case YANG_STATUS: |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 474 | ret = yin_parse_status(xml_ctx, &subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 475 | break; |
| 476 | case YANG_SUBMODULE: |
| 477 | break; |
| 478 | case YANG_TYPE: |
| 479 | break; |
| 480 | case YANG_TYPEDEF: |
| 481 | break; |
| 482 | case YANG_UNIQUE: |
| 483 | break; |
| 484 | case YANG_UNITS: |
| 485 | break; |
| 486 | case YANG_USES: |
| 487 | break; |
| 488 | case YANG_VALUE: |
| 489 | break; |
| 490 | case YANG_WHEN: |
| 491 | break; |
| 492 | case YANG_YANG_VERSION: |
| 493 | break; |
| 494 | case YANG_YIN_ELEMENT: |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 495 | ret = yin_parse_yin_element_element(xml_ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts); |
| 496 | break; |
| 497 | case YIN_TEXT: |
David Sedlák | 72f449d | 2019-07-04 09:03:59 +0200 | [diff] [blame^] | 498 | ret = yin_parse_content(xml_ctx, NULL, 0, data, YIN_TEXT, (const char **)subelem_info_rec->dest, NULL); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 499 | break; |
| 500 | case YIN_VALUE: |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 501 | break; |
| 502 | default: |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 503 | LOGINT(xml_ctx->ctx); |
| 504 | return LY_EINT; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 505 | } |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 506 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 507 | FREE_ARRAY(xml_ctx, subelem_attrs, free_arg_rec); |
| 508 | subelem_attrs = NULL; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 509 | subelem_info_rec = NULL; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 510 | } |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 511 | LY_CHECK_RET(yin_check_subelem_mandatory_constraint(xml_ctx, subelem_info, subelem_info_size, current_element)); |
| 512 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 513 | } else { |
| 514 | /* elements with text or none content */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 515 | /* save text content, if text_content isn't set, it's just ignored */ |
| 516 | if (text_content) { |
| 517 | if (dynamic) { |
| 518 | *text_content = lydict_insert_zc(xml_ctx->ctx, out); |
| 519 | if (!*text_content) { |
| 520 | free(out); |
| 521 | return LY_EMEM; |
| 522 | } |
| 523 | } else { |
| 524 | if (out_len == 0) { |
| 525 | *text_content = NULL; |
| 526 | } else { |
| 527 | *text_content = lydict_insert(xml_ctx->ctx, out, out_len); |
| 528 | } |
| 529 | } |
| 530 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 531 | /* load closing element */ |
| 532 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len)); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | cleanup: |
| 537 | FREE_ARRAY(xml_ctx, subelem_attrs, free_arg_rec); |
| 538 | return ret; |
| 539 | } |
| 540 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 541 | LY_ERR |
| 542 | yin_parse_simple_element(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw, |
| 543 | const char **value, enum YIN_ARGUMENT arg_type, uint8_t argument_flags, struct lysp_ext_instance **exts) |
| 544 | { |
| 545 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, &attrs, arg_type, value, argument_flags, kw)); |
| 546 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
| 547 | |
| 548 | return yin_parse_content(xml_ctx, subelems, 1, data, kw, NULL, exts); |
| 549 | } |
| 550 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 551 | LYEXT_SUBSTMT |
| 552 | kw2lyext_substmt(enum yang_keyword kw) |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 553 | { |
| 554 | switch (kw) { |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 555 | case YANG_ARGUMENT: |
| 556 | return LYEXT_SUBSTMT_ARGUMENT; |
| 557 | case YANG_BASE: |
| 558 | return LYEXT_SUBSTMT_BASE; |
| 559 | case YANG_BELONGS_TO: |
| 560 | return LYEXT_SUBSTMT_BELONGSTO; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 561 | case YANG_CONTACT: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 562 | return LYEXT_SUBSTMT_CONTACT; |
| 563 | case YANG_DEFAULT: |
| 564 | return LYEXT_SUBSTMT_DEFAULT; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 565 | case YANG_DESCRIPTION: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 566 | return LYEXT_SUBSTMT_DESCRIPTION; |
| 567 | case YANG_ERROR_APP_TAG: |
| 568 | return LYEXT_SUBSTMT_ERRTAG; |
| 569 | case YANG_ERROR_MESSAGE: |
| 570 | return LYEXT_SUBSTMT_ERRMSG; |
| 571 | case YANG_KEY: |
| 572 | return LYEXT_SUBSTMT_KEY; |
| 573 | case YANG_NAMESPACE: |
| 574 | return LYEXT_SUBSTMT_NAMESPACE; |
| 575 | case YANG_ORGANIZATION: |
| 576 | return LYEXT_SUBSTMT_ORGANIZATION; |
| 577 | case YANG_PATH: |
| 578 | return LYEXT_SUBSTMT_PATH; |
| 579 | case YANG_PREFIX: |
| 580 | return LYEXT_SUBSTMT_PREFIX; |
| 581 | case YANG_PRESENCE: |
| 582 | return LYEXT_SUBSTMT_PRESENCE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 583 | case YANG_REFERENCE: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 584 | return LYEXT_SUBSTMT_REFERENCE; |
| 585 | case YANG_REVISION_DATE: |
| 586 | return LYEXT_SUBSTMT_REVISIONDATE; |
| 587 | case YANG_UNITS: |
| 588 | return LYEXT_SUBSTMT_UNITS; |
| 589 | case YANG_VALUE: |
| 590 | return LYEXT_SUBSTMT_VALUE; |
| 591 | case YANG_YANG_VERSION: |
| 592 | return LYEXT_SUBSTMT_VERSION; |
| 593 | case YANG_MODIFIER: |
| 594 | return LYEXT_SUBSTMT_MODIFIER; |
| 595 | case YANG_REQUIRE_INSTANCE: |
| 596 | return LYEXT_SUBSTMT_REQINSTANCE; |
| 597 | case YANG_YIN_ELEMENT: |
| 598 | return LYEXT_SUBSTMT_YINELEM; |
| 599 | case YANG_CONFIG: |
| 600 | return LYEXT_SUBSTMT_CONFIG; |
| 601 | case YANG_MANDATORY: |
| 602 | return LYEXT_SUBSTMT_MANDATORY; |
| 603 | case YANG_ORDERED_BY: |
| 604 | return LYEXT_SUBSTMT_ORDEREDBY; |
| 605 | case YANG_STATUS: |
| 606 | return LYEXT_SUBSTMT_STATUS; |
| 607 | case YANG_FRACTION_DIGITS: |
| 608 | return LYEXT_SUBSTMT_FRACDIGITS; |
| 609 | case YANG_MAX_ELEMENTS: |
| 610 | return LYEXT_SUBSTMT_MAX; |
| 611 | case YANG_MIN_ELEMENTS: |
| 612 | return LYEXT_SUBSTMT_MIN; |
| 613 | case YANG_POSITION: |
| 614 | return LYEXT_SUBSTMT_POSITION; |
| 615 | case YANG_UNIQUE: |
| 616 | return LYEXT_SUBSTMT_UNIQUE; |
| 617 | case YANG_IF_FEATURE: |
| 618 | return LYEXT_SUBSTMT_IFFEATURE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 619 | default: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 620 | return LYEXT_SUBSTMT_SELF; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | /** |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 625 | * @brief function to parse meta tags eg. elements with text element as child |
| 626 | * |
| 627 | * @param[in] xml_ctx Xml context. |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 628 | * @param[in] args Sized array of arguments of current element. |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 629 | * @param[in,out] data Data to read from. |
| 630 | * @param[out] value Where the content of meta tag should be stored. |
| 631 | * |
| 632 | * @return LY_ERR values. |
| 633 | */ |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 634 | LY_ERR |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 635 | yin_parse_meta_element(struct lyxml_context *xml_ctx, const char **data, enum yang_keyword elem_type, |
| 636 | const char **value, struct lysp_ext_instance **exts) |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 637 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 638 | assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 639 | |
| 640 | struct yin_subelement subelems[2] = {{YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 641 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 642 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 643 | return yin_parse_content(xml_ctx, subelems, 2, data, elem_type, NULL, exts); |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 644 | } |
| 645 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 646 | LY_ERR |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 647 | yin_parse_revision_date(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, char *rev, |
| 648 | struct lysp_ext_instance **exts) |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 649 | { |
| 650 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 651 | const char *temp_rev; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 652 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 653 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 654 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, args, YIN_ARG_DATE, &temp_rev, YIN_ARG_MANDATORY, YANG_REVISION_DATE)); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 655 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 656 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
| 657 | |
| 658 | strcpy(rev, temp_rev); |
| 659 | lydict_remove(xml_ctx->ctx, temp_rev); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 660 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 661 | return yin_parse_content(xml_ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts); |
| 662 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 663 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 664 | LY_ERR |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 665 | yin_parse_prefix(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **prefix, const char **data, |
| 666 | struct lysp_ext_instance **exts) |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 667 | { |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 668 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
| 669 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, &attrs, YIN_ARG_VALUE, prefix, YIN_ARG_MANDATORY, YANG_PREFIX)); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 670 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 671 | return yin_parse_content(xml_ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 672 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 673 | |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 674 | LY_ERR |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 675 | yin_parse_import(struct lyxml_context *xml_ctx, struct yin_arg_record **import_args, const char **data, struct lysp_module *mod) |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 676 | { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 677 | struct lysp_import *imp; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 678 | /* allocate new element in sized array for import */ |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 679 | LY_ARRAY_NEW_RET(xml_ctx->ctx, mod->imports, imp, LY_EMEM); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 680 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 681 | struct yin_subelement subelems[5] = {{YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 682 | {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE}, |
| 683 | {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE}, |
| 684 | {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 685 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 686 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 687 | /* parse import attributes */ |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 688 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name, YIN_ARG_MANDATORY, YANG_IMPORT)); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 689 | LY_CHECK_RET(yin_parse_content(xml_ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts)); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 690 | /* check prefix validity */ |
| 691 | LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)xml_ctx, mod->imports, mod->mod->prefix, &imp->prefix), LY_EVALID); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 692 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 693 | return yin_parse_content(xml_ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 694 | } |
| 695 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 696 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 697 | 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] | 698 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 699 | const char *value = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 700 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 701 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 702 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 703 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 704 | return LY_EVALID; |
| 705 | } |
| 706 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 707 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, status_args, YIN_ARG_VALUE, &value, YIN_ARG_MANDATORY, YANG_STATUS)); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 708 | if (strcmp(value, "current") == 0) { |
| 709 | *flags |= LYS_STATUS_CURR; |
| 710 | } else if (strcmp(value, "deprecated") == 0) { |
| 711 | *flags |= LYS_STATUS_DEPRC; |
| 712 | } else if (strcmp(value, "obsolete") == 0) { |
| 713 | *flags |= LYS_STATUS_OBSLT; |
| 714 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 715 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 716 | lydict_remove(xml_ctx->ctx, value); |
| 717 | return LY_EVALID; |
| 718 | } |
| 719 | lydict_remove(xml_ctx->ctx, value); |
| 720 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 721 | return yin_parse_content(xml_ctx, subelems, 1, data, YANG_STATUS, NULL, exts); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 722 | } |
| 723 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 724 | LY_ERR |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 725 | yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **data, |
| 726 | uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 727 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 728 | const char *temp_val = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 729 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 730 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 731 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, &attrs, YIN_ARG_VALUE, &temp_val, YIN_ARG_MANDATORY, YANG_YIN_ELEMENT)); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 732 | if (strcmp(temp_val, "true") == 0) { |
| 733 | *flags |= LYS_YINELEM_TRUE; |
| 734 | } else if (strcmp(temp_val, "false") == 0) { |
| 735 | *flags |= LYS_YINELEM_FALSE; |
| 736 | } else { |
| 737 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element"); |
| 738 | lydict_remove(xml_ctx->ctx, temp_val); |
| 739 | return LY_EVALID; |
| 740 | } |
| 741 | lydict_remove(xml_ctx->ctx, temp_val); |
| 742 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 743 | return yin_parse_content(xml_ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | LY_ERR |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 747 | yin_parse_extension_instance(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, const char *ext_name, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 748 | int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts) |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 749 | { |
| 750 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 751 | char *out; |
| 752 | const char *name, *prefix; |
| 753 | size_t out_len, prefix_len, name_len; |
| 754 | int dynamic; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 755 | struct lysp_ext_instance *e; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 756 | struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL; |
| 757 | struct yin_arg_record *iter; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 758 | |
| 759 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *exts, e, LY_EMEM); |
| 760 | |
| 761 | e->yin = 0; |
| 762 | /* store name and insubstmt info */ |
| 763 | e->name = lydict_insert(xml_ctx->ctx, ext_name, ext_name_len); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 764 | e->insubstmt = subelem; |
| 765 | e->insubstmt_index = subelem_index; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 766 | e->yin |= LYS_YIN; |
| 767 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 768 | /* store attributes as subelements */ |
| 769 | LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) { |
| 770 | if (!iter->prefix) { |
| 771 | new_subelem = calloc(1, sizeof(*new_subelem)); |
| 772 | if (!e->child) { |
| 773 | e->child = new_subelem; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 774 | } else { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 775 | last_subelem->next = new_subelem; |
| 776 | } |
| 777 | last_subelem = new_subelem; |
| 778 | |
| 779 | last_subelem->flags |= LYS_YIN_ATTR; |
| 780 | last_subelem->stmt = lydict_insert(xml_ctx->ctx, iter->name, iter->name_len); |
| 781 | LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 782 | if (iter->dynamic_content) { |
| 783 | last_subelem->arg = lydict_insert_zc(xml_ctx->ctx, iter->content); |
| 784 | LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 785 | } else { |
| 786 | last_subelem->arg = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len); |
| 787 | LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | } |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 791 | |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 792 | /* parse subelements */ |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 793 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 794 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 795 | if (ret == LY_EINVAL) { |
| 796 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 797 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 798 | if (!name) { |
| 799 | /* end of extension instance reached */ |
| 800 | break; |
| 801 | } |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 802 | LY_CHECK_RET(yin_parse_element_generic(xml_ctx, name, name_len, prefix, prefix_len, data, &new_subelem)); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 803 | if (!e->child) { |
| 804 | e->child = new_subelem; |
| 805 | } else { |
| 806 | last_subelem->next = new_subelem; |
| 807 | } |
| 808 | last_subelem = new_subelem; |
| 809 | } |
| 810 | } |
| 811 | } else { |
| 812 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 813 | LY_CHECK_RET(name, LY_EINVAL); |
| 814 | } |
| 815 | |
| 816 | return LY_SUCCESS; |
| 817 | } |
| 818 | |
| 819 | LY_ERR |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 820 | yin_parse_element_generic(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix, |
| 821 | size_t prefix_len, const char **data, struct lysp_stmt **element) |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 822 | { |
| 823 | LY_ERR ret = LY_SUCCESS; |
| 824 | const char *temp_prefix, *temp_name; |
| 825 | char *out = NULL; |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 826 | size_t out_len, temp_name_len, temp_prefix_len; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 827 | int dynamic; |
| 828 | struct yin_arg_record *subelem_args = NULL; |
| 829 | struct lysp_stmt *last = NULL, *new = NULL; |
| 830 | |
| 831 | /* allocate new structure for element */ |
| 832 | *element = calloc(1, sizeof(**element)); |
| 833 | (*element)->stmt = lydict_insert(xml_ctx->ctx, name, name_len); |
| 834 | LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 835 | |
| 836 | last = (*element)->child; |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 837 | /* load attributes */ |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 838 | while(xml_ctx->status == LYXML_ATTRIBUTE) { |
| 839 | /* add new element to linked-list */ |
| 840 | new = calloc(1, sizeof(*last)); |
| 841 | LY_CHECK_ERR_GOTO(ret, LOGMEM(xml_ctx->ctx), err); |
| 842 | if (!(*element)->child) { |
| 843 | /* save first */ |
| 844 | (*element)->child = new; |
| 845 | } else { |
| 846 | last->next = new; |
| 847 | } |
| 848 | last = new; |
| 849 | |
| 850 | last->flags |= LYS_YIN_ATTR; |
| 851 | ret = lyxml_get_attribute(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len); |
| 852 | LY_CHECK_GOTO(ret, err); |
| 853 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 854 | LY_CHECK_GOTO(ret, err); |
| 855 | last->stmt = lydict_insert(xml_ctx->ctx, temp_name, temp_name_len); |
| 856 | LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err); |
| 857 | /* attributes with prefix are ignored */ |
| 858 | if (!temp_prefix) { |
| 859 | if (dynamic) { |
| 860 | last->arg = lydict_insert_zc(xml_ctx->ctx, out); |
| 861 | if (!last->arg) { |
| 862 | free(out); |
| 863 | LOGMEM(xml_ctx->ctx); |
| 864 | ret = LY_EMEM; |
| 865 | goto err; |
| 866 | } |
| 867 | } else { |
| 868 | last->arg = lydict_insert(xml_ctx->ctx, out, out_len); |
| 869 | LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err); |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | /* parse content of element */ |
| 875 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 876 | if (ret == LY_EINVAL) { |
| 877 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 878 | /* parse subelements */ |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 879 | ret = lyxml_get_element(xml_ctx, data, &temp_prefix, &temp_prefix_len, &temp_name, &temp_name_len); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 880 | LY_CHECK_GOTO(ret, err); |
| 881 | if (!name) { |
| 882 | /* end of element reached */ |
| 883 | break; |
| 884 | } |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 885 | ret = yin_parse_element_generic(xml_ctx, temp_name, temp_name_len, temp_prefix, temp_prefix_len, data, &last->next); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 886 | LY_CHECK_GOTO(ret, err); |
| 887 | last = last->next; |
| 888 | } |
| 889 | } else { |
| 890 | /* save element content */ |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 891 | if (out_len != 0) { |
| 892 | if (dynamic) { |
| 893 | (*element)->arg = lydict_insert_zc(xml_ctx->ctx, out); |
| 894 | if (!(*element)->arg) { |
| 895 | free(out); |
| 896 | LOGMEM(xml_ctx->ctx); |
| 897 | ret = LY_EMEM; |
| 898 | goto err; |
| 899 | } |
| 900 | } else { |
| 901 | (*element)->arg = lydict_insert(xml_ctx->ctx, out, out_len); |
| 902 | LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 903 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 904 | } |
| 905 | /* read closing tag */ |
| 906 | ret = lyxml_get_element(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len); |
| 907 | LY_CHECK_GOTO(ret, err); |
| 908 | } |
| 909 | |
| 910 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 911 | return LY_SUCCESS; |
| 912 | |
| 913 | err: |
| 914 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 915 | return ret; |
| 916 | } |
| 917 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 918 | /** |
| 919 | * @brief Parse argument element. |
| 920 | * |
| 921 | * @param[in,out] xml_ctx Xml context. |
| 922 | * @param[in] attrs Attributes of this element. |
| 923 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 924 | * @param[in,out] arg_meta Meta information about destionation af prased data. |
| 925 | * @param[in,out] exts Extension instance to add to. |
| 926 | * |
| 927 | * @return LY_ERR values. |
| 928 | */ |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 929 | LY_ERR |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 930 | yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 931 | struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts) |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 932 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 933 | struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 934 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 935 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 936 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_NAME, arg_meta->argument, YIN_ARG_MANDATORY, YANG_ARGUMENT)); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 937 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 938 | return yin_parse_content(xml_ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 942 | 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] | 943 | { |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 944 | struct lysp_ext *ex; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 945 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *extensions, ex, LY_EMEM); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 946 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name, YIN_ARG_MANDATORY, YANG_EXTENSION)); |
| 947 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 948 | struct yin_argument_meta arg_info = {&ex->flags, &ex->argument}; |
| 949 | struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE}, |
| 950 | {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE}, |
| 951 | {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE}, |
| 952 | {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 953 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 954 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 955 | return yin_parse_content(xml_ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 956 | } |
| 957 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 958 | /** |
| 959 | * @brief Parse module substatements. |
| 960 | * |
| 961 | * @param[in] xml_ctx Xml context. |
| 962 | * @param[in,out] data Data to read from. |
| 963 | * @param[out] mod Parsed module structure. |
| 964 | * |
| 965 | * @return LY_ERR values. |
| 966 | */ |
| 967 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 968 | 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] | 969 | { |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 970 | struct yin_subelement subelems[8] = {{YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE}, |
| 971 | {YANG_IMPORT, *mod, 0}, |
| 972 | {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE}, |
| 973 | {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE}, |
| 974 | {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 975 | {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE}, |
| 976 | {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 977 | /* TODO add YANG_EXTENSION */ |
| 978 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 979 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 980 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 981 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name, YIN_ARG_MANDATORY, YANG_MODULE)); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 982 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 983 | return yin_parse_content(xml_ctx, subelems, 8, data, YANG_MODULE, NULL, &(*mod)->exts); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 987 | 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] | 988 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 989 | LY_ERR ret = LY_SUCCESS; |
| 990 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 991 | struct lys_parser_ctx parser_ctx; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 992 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 993 | struct lysp_module *mod_p = NULL; |
| 994 | const char *prefix, *name; |
| 995 | size_t prefix_len, name_len; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 996 | struct yin_arg_record *args = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 997 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 998 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 999 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 1000 | xml_ctx->ctx = ctx; |
| 1001 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1002 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1003 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 1004 | 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] | 1005 | LY_CHECK_GOTO(ret, cleanup); |
| 1006 | ret = yin_load_attributes(xml_ctx, &data, &args); |
| 1007 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1008 | 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] | 1009 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1010 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 1011 | ret = LY_EINVAL; |
| 1012 | goto cleanup; |
| 1013 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1014 | 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] | 1015 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1016 | ret = LY_EVALID; |
| 1017 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1020 | /* allocate module */ |
| 1021 | mod_p = calloc(1, sizeof *mod_p); |
| 1022 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 1023 | mod_p->mod = mod; |
| 1024 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1025 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1026 | /* parse module substatements */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1027 | ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1028 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 1029 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1030 | mod_p->parsing = 0; |
| 1031 | mod->parsed = mod_p; |
| 1032 | |
| 1033 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1034 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1035 | lysp_module_free(mod_p); |
| 1036 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1037 | FREE_ARRAY(xml_ctx, args, free_arg_rec); |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 1038 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 1039 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1040 | } |