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: |
| 498 | ret = yin_parse_text_element(xml_ctx, data, (const char **)subelem_info_rec->dest); |
| 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 | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 551 | LY_ERR |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 552 | yin_parse_text_element(struct lyxml_context *xml_ctx, const char **data, const char **value) |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 553 | { |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 554 | return yin_parse_content(xml_ctx, NULL, 0, data, YIN_TEXT, value, NULL); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 555 | } |
| 556 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 557 | LYEXT_SUBSTMT |
| 558 | kw2lyext_substmt(enum yang_keyword kw) |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 559 | { |
| 560 | switch (kw) { |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 561 | case YANG_ARGUMENT: |
| 562 | return LYEXT_SUBSTMT_ARGUMENT; |
| 563 | case YANG_BASE: |
| 564 | return LYEXT_SUBSTMT_BASE; |
| 565 | case YANG_BELONGS_TO: |
| 566 | return LYEXT_SUBSTMT_BELONGSTO; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 567 | case YANG_CONTACT: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 568 | return LYEXT_SUBSTMT_CONTACT; |
| 569 | case YANG_DEFAULT: |
| 570 | return LYEXT_SUBSTMT_DEFAULT; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 571 | case YANG_DESCRIPTION: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 572 | return LYEXT_SUBSTMT_DESCRIPTION; |
| 573 | case YANG_ERROR_APP_TAG: |
| 574 | return LYEXT_SUBSTMT_ERRTAG; |
| 575 | case YANG_ERROR_MESSAGE: |
| 576 | return LYEXT_SUBSTMT_ERRMSG; |
| 577 | case YANG_KEY: |
| 578 | return LYEXT_SUBSTMT_KEY; |
| 579 | case YANG_NAMESPACE: |
| 580 | return LYEXT_SUBSTMT_NAMESPACE; |
| 581 | case YANG_ORGANIZATION: |
| 582 | return LYEXT_SUBSTMT_ORGANIZATION; |
| 583 | case YANG_PATH: |
| 584 | return LYEXT_SUBSTMT_PATH; |
| 585 | case YANG_PREFIX: |
| 586 | return LYEXT_SUBSTMT_PREFIX; |
| 587 | case YANG_PRESENCE: |
| 588 | return LYEXT_SUBSTMT_PRESENCE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 589 | case YANG_REFERENCE: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 590 | return LYEXT_SUBSTMT_REFERENCE; |
| 591 | case YANG_REVISION_DATE: |
| 592 | return LYEXT_SUBSTMT_REVISIONDATE; |
| 593 | case YANG_UNITS: |
| 594 | return LYEXT_SUBSTMT_UNITS; |
| 595 | case YANG_VALUE: |
| 596 | return LYEXT_SUBSTMT_VALUE; |
| 597 | case YANG_YANG_VERSION: |
| 598 | return LYEXT_SUBSTMT_VERSION; |
| 599 | case YANG_MODIFIER: |
| 600 | return LYEXT_SUBSTMT_MODIFIER; |
| 601 | case YANG_REQUIRE_INSTANCE: |
| 602 | return LYEXT_SUBSTMT_REQINSTANCE; |
| 603 | case YANG_YIN_ELEMENT: |
| 604 | return LYEXT_SUBSTMT_YINELEM; |
| 605 | case YANG_CONFIG: |
| 606 | return LYEXT_SUBSTMT_CONFIG; |
| 607 | case YANG_MANDATORY: |
| 608 | return LYEXT_SUBSTMT_MANDATORY; |
| 609 | case YANG_ORDERED_BY: |
| 610 | return LYEXT_SUBSTMT_ORDEREDBY; |
| 611 | case YANG_STATUS: |
| 612 | return LYEXT_SUBSTMT_STATUS; |
| 613 | case YANG_FRACTION_DIGITS: |
| 614 | return LYEXT_SUBSTMT_FRACDIGITS; |
| 615 | case YANG_MAX_ELEMENTS: |
| 616 | return LYEXT_SUBSTMT_MAX; |
| 617 | case YANG_MIN_ELEMENTS: |
| 618 | return LYEXT_SUBSTMT_MIN; |
| 619 | case YANG_POSITION: |
| 620 | return LYEXT_SUBSTMT_POSITION; |
| 621 | case YANG_UNIQUE: |
| 622 | return LYEXT_SUBSTMT_UNIQUE; |
| 623 | case YANG_IF_FEATURE: |
| 624 | return LYEXT_SUBSTMT_IFFEATURE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 625 | default: |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 626 | return LYEXT_SUBSTMT_SELF; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
| 630 | /** |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 631 | * @brief function to parse meta tags eg. elements with text element as child |
| 632 | * |
| 633 | * @param[in] xml_ctx Xml context. |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 634 | * @param[in] args Sized array of arguments of current element. |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 635 | * @param[in,out] data Data to read from. |
| 636 | * @param[out] value Where the content of meta tag should be stored. |
| 637 | * |
| 638 | * @return LY_ERR values. |
| 639 | */ |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 640 | LY_ERR |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 641 | yin_parse_meta_element(struct lyxml_context *xml_ctx, const char **data, enum yang_keyword elem_type, |
| 642 | const char **value, struct lysp_ext_instance **exts) |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 643 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 644 | 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^] | 645 | |
| 646 | 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] | 647 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 648 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 649 | 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] | 650 | } |
| 651 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 652 | LY_ERR |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 653 | yin_parse_revision_date(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, char *rev, |
| 654 | struct lysp_ext_instance **exts) |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 655 | { |
| 656 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 657 | const char *temp_rev; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 658 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 659 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 660 | 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] | 661 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 662 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
| 663 | |
| 664 | strcpy(rev, temp_rev); |
| 665 | lydict_remove(xml_ctx->ctx, temp_rev); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 666 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 667 | return yin_parse_content(xml_ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts); |
| 668 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 669 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 670 | LY_ERR |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 671 | yin_parse_prefix(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **prefix, const char **data, |
| 672 | struct lysp_ext_instance **exts) |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 673 | { |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 674 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
| 675 | 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] | 676 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 677 | 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] | 678 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 679 | |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 680 | LY_ERR |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 681 | 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] | 682 | { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 683 | struct lysp_import *imp; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 684 | /* allocate new element in sized array for import */ |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 685 | 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] | 686 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 687 | struct yin_subelement subelems[5] = {{YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 688 | {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE}, |
| 689 | {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE}, |
| 690 | {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 691 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 692 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 693 | /* parse import attributes */ |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 694 | 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] | 695 | 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] | 696 | /* check prefix validity */ |
| 697 | 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] | 698 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 699 | 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] | 700 | } |
| 701 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 702 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 703 | 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] | 704 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 705 | const char *value = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 706 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 707 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 708 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 709 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 710 | return LY_EVALID; |
| 711 | } |
| 712 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 713 | 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] | 714 | if (strcmp(value, "current") == 0) { |
| 715 | *flags |= LYS_STATUS_CURR; |
| 716 | } else if (strcmp(value, "deprecated") == 0) { |
| 717 | *flags |= LYS_STATUS_DEPRC; |
| 718 | } else if (strcmp(value, "obsolete") == 0) { |
| 719 | *flags |= LYS_STATUS_OBSLT; |
| 720 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 721 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 722 | lydict_remove(xml_ctx->ctx, value); |
| 723 | return LY_EVALID; |
| 724 | } |
| 725 | lydict_remove(xml_ctx->ctx, value); |
| 726 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 727 | 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] | 728 | } |
| 729 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 730 | LY_ERR |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 731 | yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **data, |
| 732 | uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 733 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 734 | const char *temp_val = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 735 | struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 736 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 737 | 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] | 738 | if (strcmp(temp_val, "true") == 0) { |
| 739 | *flags |= LYS_YINELEM_TRUE; |
| 740 | } else if (strcmp(temp_val, "false") == 0) { |
| 741 | *flags |= LYS_YINELEM_FALSE; |
| 742 | } else { |
| 743 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element"); |
| 744 | lydict_remove(xml_ctx->ctx, temp_val); |
| 745 | return LY_EVALID; |
| 746 | } |
| 747 | lydict_remove(xml_ctx->ctx, temp_val); |
| 748 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 749 | 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] | 750 | } |
| 751 | |
| 752 | LY_ERR |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 753 | 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] | 754 | 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] | 755 | { |
| 756 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 757 | char *out; |
| 758 | const char *name, *prefix; |
| 759 | size_t out_len, prefix_len, name_len; |
| 760 | int dynamic; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 761 | struct lysp_ext_instance *e; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 762 | struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL; |
| 763 | struct yin_arg_record *iter; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 764 | |
| 765 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *exts, e, LY_EMEM); |
| 766 | |
| 767 | e->yin = 0; |
| 768 | /* store name and insubstmt info */ |
| 769 | 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] | 770 | e->insubstmt = subelem; |
| 771 | e->insubstmt_index = subelem_index; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 772 | e->yin |= LYS_YIN; |
| 773 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 774 | /* store attributes as subelements */ |
| 775 | LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) { |
| 776 | if (!iter->prefix) { |
| 777 | new_subelem = calloc(1, sizeof(*new_subelem)); |
| 778 | if (!e->child) { |
| 779 | e->child = new_subelem; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 780 | } else { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 781 | last_subelem->next = new_subelem; |
| 782 | } |
| 783 | last_subelem = new_subelem; |
| 784 | |
| 785 | last_subelem->flags |= LYS_YIN_ATTR; |
| 786 | last_subelem->stmt = lydict_insert(xml_ctx->ctx, iter->name, iter->name_len); |
| 787 | LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 788 | if (iter->dynamic_content) { |
| 789 | last_subelem->arg = lydict_insert_zc(xml_ctx->ctx, iter->content); |
| 790 | LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 791 | } else { |
| 792 | last_subelem->arg = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len); |
| 793 | 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] | 794 | } |
| 795 | } |
| 796 | } |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 797 | |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 798 | /* parse subelements */ |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 799 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 800 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 801 | if (ret == LY_EINVAL) { |
| 802 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 803 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 804 | if (!name) { |
| 805 | /* end of extension instance reached */ |
| 806 | break; |
| 807 | } |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 808 | 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] | 809 | if (!e->child) { |
| 810 | e->child = new_subelem; |
| 811 | } else { |
| 812 | last_subelem->next = new_subelem; |
| 813 | } |
| 814 | last_subelem = new_subelem; |
| 815 | } |
| 816 | } |
| 817 | } else { |
| 818 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 819 | LY_CHECK_RET(name, LY_EINVAL); |
| 820 | } |
| 821 | |
| 822 | return LY_SUCCESS; |
| 823 | } |
| 824 | |
| 825 | LY_ERR |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 826 | yin_parse_element_generic(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix, |
| 827 | size_t prefix_len, const char **data, struct lysp_stmt **element) |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 828 | { |
| 829 | LY_ERR ret = LY_SUCCESS; |
| 830 | const char *temp_prefix, *temp_name; |
| 831 | char *out = NULL; |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 832 | size_t out_len, temp_name_len, temp_prefix_len; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 833 | int dynamic; |
| 834 | struct yin_arg_record *subelem_args = NULL; |
| 835 | struct lysp_stmt *last = NULL, *new = NULL; |
| 836 | |
| 837 | /* allocate new structure for element */ |
| 838 | *element = calloc(1, sizeof(**element)); |
| 839 | (*element)->stmt = lydict_insert(xml_ctx->ctx, name, name_len); |
| 840 | LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 841 | |
| 842 | last = (*element)->child; |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 843 | /* load attributes */ |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 844 | while(xml_ctx->status == LYXML_ATTRIBUTE) { |
| 845 | /* add new element to linked-list */ |
| 846 | new = calloc(1, sizeof(*last)); |
| 847 | LY_CHECK_ERR_GOTO(ret, LOGMEM(xml_ctx->ctx), err); |
| 848 | if (!(*element)->child) { |
| 849 | /* save first */ |
| 850 | (*element)->child = new; |
| 851 | } else { |
| 852 | last->next = new; |
| 853 | } |
| 854 | last = new; |
| 855 | |
| 856 | last->flags |= LYS_YIN_ATTR; |
| 857 | ret = lyxml_get_attribute(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len); |
| 858 | LY_CHECK_GOTO(ret, err); |
| 859 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 860 | LY_CHECK_GOTO(ret, err); |
| 861 | last->stmt = lydict_insert(xml_ctx->ctx, temp_name, temp_name_len); |
| 862 | LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err); |
| 863 | /* attributes with prefix are ignored */ |
| 864 | if (!temp_prefix) { |
| 865 | if (dynamic) { |
| 866 | last->arg = lydict_insert_zc(xml_ctx->ctx, out); |
| 867 | if (!last->arg) { |
| 868 | free(out); |
| 869 | LOGMEM(xml_ctx->ctx); |
| 870 | ret = LY_EMEM; |
| 871 | goto err; |
| 872 | } |
| 873 | } else { |
| 874 | last->arg = lydict_insert(xml_ctx->ctx, out, out_len); |
| 875 | LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /* parse content of element */ |
| 881 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 882 | if (ret == LY_EINVAL) { |
| 883 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 884 | /* parse subelements */ |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 885 | 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] | 886 | LY_CHECK_GOTO(ret, err); |
| 887 | if (!name) { |
| 888 | /* end of element reached */ |
| 889 | break; |
| 890 | } |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 891 | 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] | 892 | LY_CHECK_GOTO(ret, err); |
| 893 | last = last->next; |
| 894 | } |
| 895 | } else { |
| 896 | /* save element content */ |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 897 | if (out_len != 0) { |
| 898 | if (dynamic) { |
| 899 | (*element)->arg = lydict_insert_zc(xml_ctx->ctx, out); |
| 900 | if (!(*element)->arg) { |
| 901 | free(out); |
| 902 | LOGMEM(xml_ctx->ctx); |
| 903 | ret = LY_EMEM; |
| 904 | goto err; |
| 905 | } |
| 906 | } else { |
| 907 | (*element)->arg = lydict_insert(xml_ctx->ctx, out, out_len); |
| 908 | 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] | 909 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 910 | } |
| 911 | /* read closing tag */ |
| 912 | ret = lyxml_get_element(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len); |
| 913 | LY_CHECK_GOTO(ret, err); |
| 914 | } |
| 915 | |
| 916 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 917 | return LY_SUCCESS; |
| 918 | |
| 919 | err: |
| 920 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 921 | return ret; |
| 922 | } |
| 923 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 924 | /** |
| 925 | * @brief Parse argument element. |
| 926 | * |
| 927 | * @param[in,out] xml_ctx Xml context. |
| 928 | * @param[in] attrs Attributes of this element. |
| 929 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 930 | * @param[in,out] arg_meta Meta information about destionation af prased data. |
| 931 | * @param[in,out] exts Extension instance to add to. |
| 932 | * |
| 933 | * @return LY_ERR values. |
| 934 | */ |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 935 | LY_ERR |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 936 | 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] | 937 | struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts) |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 938 | { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 939 | 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] | 940 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 941 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 942 | 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] | 943 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 944 | 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] | 945 | } |
| 946 | |
| 947 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 948 | 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] | 949 | { |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 950 | struct lysp_ext *ex; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 951 | LY_ARRAY_NEW_RET(xml_ctx->ctx, *extensions, ex, LY_EMEM); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 952 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name, YIN_ARG_MANDATORY, YANG_EXTENSION)); |
| 953 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 954 | struct yin_argument_meta arg_info = {&ex->flags, &ex->argument}; |
| 955 | struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE}, |
| 956 | {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE}, |
| 957 | {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE}, |
| 958 | {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 959 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 960 | |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 961 | 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] | 962 | } |
| 963 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 964 | /** |
| 965 | * @brief Parse module substatements. |
| 966 | * |
| 967 | * @param[in] xml_ctx Xml context. |
| 968 | * @param[in,out] data Data to read from. |
| 969 | * @param[out] mod Parsed module structure. |
| 970 | * |
| 971 | * @return LY_ERR values. |
| 972 | */ |
| 973 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 974 | 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] | 975 | { |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 976 | struct yin_subelement subelems[8] = {{YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE}, |
| 977 | {YANG_IMPORT, *mod, 0}, |
| 978 | {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE}, |
| 979 | {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE}, |
| 980 | {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 981 | {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE}, |
| 982 | {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 983 | /* TODO add YANG_EXTENSION */ |
| 984 | {YANG_CUSTOM, NULL, 0}}; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 985 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 986 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 987 | 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] | 988 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 989 | 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] | 990 | } |
| 991 | |
| 992 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 993 | 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] | 994 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 995 | LY_ERR ret = LY_SUCCESS; |
| 996 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 997 | struct lys_parser_ctx parser_ctx; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 998 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 999 | struct lysp_module *mod_p = NULL; |
| 1000 | const char *prefix, *name; |
| 1001 | size_t prefix_len, name_len; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1002 | struct yin_arg_record *args = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1003 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1004 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 1005 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 1006 | xml_ctx->ctx = ctx; |
| 1007 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1008 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1009 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 1010 | 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] | 1011 | LY_CHECK_GOTO(ret, cleanup); |
| 1012 | ret = yin_load_attributes(xml_ctx, &data, &args); |
| 1013 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1014 | 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] | 1015 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1016 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 1017 | ret = LY_EINVAL; |
| 1018 | goto cleanup; |
| 1019 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1020 | 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] | 1021 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1022 | ret = LY_EVALID; |
| 1023 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1026 | /* allocate module */ |
| 1027 | mod_p = calloc(1, sizeof *mod_p); |
| 1028 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 1029 | mod_p->mod = mod; |
| 1030 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 1031 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1032 | /* parse module substatements */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1033 | ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1034 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 1035 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1036 | mod_p->parsing = 0; |
| 1037 | mod->parsed = mod_p; |
| 1038 | |
| 1039 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1040 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 1041 | lysp_module_free(mod_p); |
| 1042 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 1043 | FREE_ARRAY(xml_ctx, args, free_arg_rec); |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 1044 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 1045 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1046 | } |