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