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 | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 22 | #include <errno.h> |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 23 | #include <ctype.h> |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 24 | #include <stdarg.h> |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 25 | |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 26 | #include "context.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 27 | #include "dict.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 28 | #include "xml.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 29 | #include "tree.h" |
| 30 | #include "tree_schema.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 31 | #include "tree_schema_internal.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 32 | #include "parser_yin.h" |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 33 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 34 | /** |
| 35 | * @brief check if given string is URI of yin namespace. |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 36 | * |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 37 | * @param ns Namespace URI to check. |
| 38 | * |
| 39 | * @return true if ns equals YIN_NS_URI false otherwise. |
| 40 | */ |
| 41 | #define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0) |
| 42 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 43 | const char *const yin_attr_list[] = { |
| 44 | [YIN_ARG_NAME] = "name", |
| 45 | [YIN_ARG_TARGET_NODE] = "target-node", |
| 46 | [YIN_ARG_MODULE] = "module", |
| 47 | [YIN_ARG_VALUE] = "value", |
| 48 | [YIN_ARG_TEXT] = "text", |
| 49 | [YIN_ARG_CONDITION] = "condition", |
| 50 | [YIN_ARG_URI] = "uri", |
| 51 | [YIN_ARG_DATE] = "date", |
| 52 | [YIN_ARG_TAG] = "tag", |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 53 | [YIN_ARG_NONE] = "none", |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 56 | enum ly_stmt |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 57 | yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 58 | const char *prefix, size_t prefix_len, enum ly_stmt parrent) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 59 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 60 | const char *start = NULL; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 61 | enum ly_stmt kw = LY_STMT_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 62 | const struct lyxml_ns *ns = NULL; |
| 63 | |
| 64 | if (!name || name_len == 0) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 65 | return LY_STMT_NONE; |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 66 | } |
| 67 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 68 | ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 69 | if (ns) { |
| 70 | if (!IS_YIN_NS(ns->uri)) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 71 | return LY_STMT_EXTENSION_INSTANCE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 72 | } |
| 73 | } else { |
| 74 | /* elements without namespace are automatically unknown */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 75 | return LY_STMT_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 76 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 77 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 78 | start = name; |
| 79 | kw = lysp_match_kw(NULL, &name); |
| 80 | |
| 81 | if (name - start == (long int)name_len) { |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 82 | /* this is done because of collision in yang statement value and yang argument mapped to yin element value */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 83 | if (kw == LY_STMT_VALUE && parrent == LY_STMT_ERROR_MESSAGE) { |
| 84 | return LY_STMT_ARG_VALUE; |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 85 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 86 | return kw; |
| 87 | } else { |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 88 | if (strncmp(start, "text", name_len) == 0) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 89 | return LY_STMT_ARG_TEXT; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 90 | } else { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 91 | return LY_STMT_NONE; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 92 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 96 | enum yin_argument |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 97 | yin_match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 98 | { |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 99 | enum yin_argument arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 100 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 101 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 102 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 103 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 104 | #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] | 105 | #define IF_ARG_PREFIX_END } |
| 106 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 107 | switch (*name) { |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 108 | case 'c': |
| 109 | already_read += 1; |
| 110 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 111 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 112 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 113 | case 'd': |
| 114 | already_read += 1; |
| 115 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 116 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 117 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 118 | case 'm': |
| 119 | already_read += 1; |
| 120 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 121 | break; |
| 122 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 123 | case 'n': |
| 124 | already_read += 1; |
| 125 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 126 | break; |
| 127 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 128 | case 't': |
| 129 | already_read += 1; |
| 130 | IF_ARG_PREFIX("a", 1) |
| 131 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 132 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 133 | IF_ARG_PREFIX_END |
| 134 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 135 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 136 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 137 | case 'u': |
| 138 | already_read += 1; |
| 139 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 140 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 141 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 142 | case 'v': |
| 143 | already_read += 1; |
| 144 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 148 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 149 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 150 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 151 | } |
| 152 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 153 | #undef IF_ARG |
| 154 | #undef IF_ARG_PREFIX |
| 155 | #undef IF_ARG_PREFIX_END |
| 156 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 157 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 158 | } |
| 159 | |
David Sedlák | 4f03b93 | 2019-07-26 13:01:47 +0200 | [diff] [blame] | 160 | void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 161 | (void)ctx; /* unused */ |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 162 | if (record && record->dynamic_content) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 163 | free(record->content); |
| 164 | } |
| 165 | } |
| 166 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 167 | #define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \ |
| 168 | kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \ |
| 169 | kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \ |
| 170 | kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT) |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 171 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 172 | #define HAS_META(kw) (IS_NODE_ELEM(kw) || kw == LY_STMT_IMPORT || kw == LY_STMT_INCLUDE || kw == LY_STMT_INPUT || kw == LY_STMT_OUTPUT) |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 173 | |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 174 | /** |
| 175 | * @brief Free subelems information allocated on heap. |
| 176 | * |
| 177 | * @param[in] count Size of subelems array. |
| 178 | * @param[in] subelems Subelems array to free. |
| 179 | */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 180 | static void |
| 181 | subelems_deallocator(size_t count, struct yin_subelement *subelems) |
| 182 | { |
| 183 | for(size_t i = 0; i < count; ++i) { |
| 184 | if (HAS_META(subelems[i].type)) { |
| 185 | free(subelems[i].dest); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | free(subelems); |
| 190 | } |
| 191 | |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 192 | /** |
| 193 | * @brief Allocate subelems information on heap. |
| 194 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 195 | * @param[in] ctx YIN parser context, used for logging. |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 196 | * @param[in] count Number of subelements. |
| 197 | * @param[in] parent Parent node if any. |
| 198 | * @param[out] result Allocated subelems array. |
| 199 | * |
| 200 | * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure. |
| 201 | */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 202 | static LY_ERR |
| 203 | subelems_allocator(struct yin_parser_ctx *ctx, size_t count, struct lysp_node *parent, |
| 204 | struct yin_subelement **result, ...) |
| 205 | { |
| 206 | va_list ap; |
| 207 | |
| 208 | *result = calloc(count, sizeof **result); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 209 | LY_CHECK_GOTO(!(*result), mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 210 | |
| 211 | va_start(ap, result); |
| 212 | for (size_t i = 0; i < count; ++i) { |
| 213 | /* TYPE */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 214 | (*result)[i].type = va_arg(ap, enum ly_stmt); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 215 | /* DEST */ |
| 216 | if (IS_NODE_ELEM((*result)[i].type)) { |
| 217 | struct tree_node_meta *node_meta = NULL; |
| 218 | node_meta = calloc(1, sizeof *node_meta); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 219 | LY_CHECK_GOTO(!node_meta, mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 220 | node_meta->parent = parent; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 221 | node_meta->nodes = va_arg(ap, void *); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 222 | (*result)[i].dest = node_meta; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 223 | } else if ((*result)[i].type == LY_STMT_IMPORT) { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 224 | struct import_meta *imp_meta = NULL; |
| 225 | imp_meta = calloc(1, sizeof *imp_meta); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 226 | LY_CHECK_GOTO(!imp_meta, mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 227 | imp_meta->prefix = va_arg(ap, const char *); |
| 228 | imp_meta->imports = va_arg(ap, struct lysp_import **); |
| 229 | (*result)[i].dest = imp_meta; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 230 | } else if ((*result)[i].type == LY_STMT_INCLUDE) { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 231 | struct include_meta *inc_meta = NULL; |
| 232 | inc_meta = calloc(1, sizeof *inc_meta); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 233 | LY_CHECK_GOTO(!inc_meta, mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 234 | inc_meta->name = va_arg(ap, const char *); |
| 235 | inc_meta->includes = va_arg(ap, struct lysp_include **); |
| 236 | (*result)[i].dest = inc_meta; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 237 | } else if ((*result)[i].type == LY_STMT_INPUT || (*result)[i].type == LY_STMT_OUTPUT) { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 238 | struct inout_meta *inout_meta = NULL; |
| 239 | inout_meta = calloc(1, sizeof *inout_meta); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 240 | LY_CHECK_GOTO(!inout_meta, mem_err); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 241 | inout_meta->parent = parent; |
| 242 | inout_meta->inout_p = va_arg(ap, struct lysp_action_inout *); |
| 243 | (*result)[i].dest = inout_meta; |
| 244 | } else { |
| 245 | (*result)[i].dest = va_arg(ap, void *); |
| 246 | } |
| 247 | /* FLAGS */ |
| 248 | (*result)[i].flags = va_arg(ap, int); |
| 249 | } |
| 250 | va_end(ap); |
| 251 | |
| 252 | return LY_SUCCESS; |
| 253 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 254 | mem_err: |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 255 | subelems_deallocator(count, *result); |
| 256 | LOGMEM(ctx->xml_ctx.ctx); |
| 257 | return LY_EMEM; |
| 258 | } |
| 259 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 260 | LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 261 | yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs) |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 262 | { |
| 263 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 264 | struct yin_arg_record *argument_record = NULL; |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 265 | const char *prefix, *name; |
| 266 | size_t prefix_len, name_len; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 267 | |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 268 | /* load all attributes */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 269 | while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) { |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 270 | ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 271 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 272 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 273 | if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) { |
| 274 | LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup); |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 275 | argument_record->name = name; |
| 276 | argument_record->name_len = name_len; |
| 277 | argument_record->prefix = prefix; |
| 278 | argument_record->prefix_len = prefix_len; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 279 | ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len, |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 280 | &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content); |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 281 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 285 | cleanup: |
| 286 | if (ret != LY_SUCCESS) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 287 | FREE_ARRAY(ctx, *attrs, free_arg_rec); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 288 | *attrs = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 289 | } |
| 290 | return ret; |
| 291 | } |
| 292 | |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 293 | LY_ERR |
| 294 | yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len) |
| 295 | { |
| 296 | int prefix = 0; |
| 297 | unsigned int c; |
| 298 | size_t utf8_char_len; |
| 299 | size_t already_read = 0; |
| 300 | while (already_read < len) { |
| 301 | LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len), |
| 302 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID); |
| 303 | already_read += utf8_char_len; |
| 304 | LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT); |
| 305 | |
| 306 | switch (val_type) { |
| 307 | case Y_IDENTIF_ARG: |
| 308 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL)); |
| 309 | break; |
| 310 | case Y_PREF_IDENTIF_ARG: |
| 311 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix)); |
| 312 | break; |
| 313 | case Y_STR_ARG: |
| 314 | case Y_MAYBE_STR_ARG: |
| 315 | LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c)); |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return LY_SUCCESS; |
| 321 | } |
| 322 | |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 323 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 324 | * @brief Parse yin attribute. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 325 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 326 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 327 | * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes. |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 328 | * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without |
| 329 | * special argument). |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 330 | * @param[out] arg_val Where value of argument should be stored. Can be NULL iff arg_type is specified as YIN_ARG_NONE. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 331 | * @param[in] val_type Type of expected value of attribute. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 332 | * @param[in] current_element Identification of current element, used for logging. |
| 333 | * |
| 334 | * @return LY_ERR values. |
| 335 | */ |
| 336 | static LY_ERR |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 337 | yin_parse_attribute(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, enum yin_argument arg_type, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 338 | const char **arg_val, enum yang_arg val_type, enum ly_stmt current_element) |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 339 | { |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 340 | enum yin_argument arg = YIN_ARG_UNKNOWN; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 341 | struct yin_arg_record *iter = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 342 | bool found = false; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 343 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 344 | /* validation of attributes */ |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 345 | LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 346 | /* yin arguments represented as attributes have no namespace, which in this case means no prefix */ |
| 347 | if (!iter->prefix) { |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 348 | arg = yin_match_argument_name(iter->name, iter->name_len); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 349 | if (arg == YIN_ARG_NONE) { |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 350 | continue; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 351 | } else if (arg == arg_type) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 352 | LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR, |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 353 | yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 354 | found = true; |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 355 | LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len)); |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 356 | INSERT_STRING(ctx->xml_ctx.ctx, *arg_val, iter->dynamic_content, iter->content, iter->content_len); |
| 357 | iter->dynamic_content = 0; |
| 358 | LY_CHECK_RET(!(*arg_val), LY_EMEM); |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 359 | } else { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 360 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, iter->name_len, iter->name, ly_stmt2str(current_element)); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 361 | return LY_EVALID; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 366 | /* anything else than Y_MAYBE_STR_ARG is mandatory */ |
| 367 | if (val_type != Y_MAYBE_STR_ARG && !found) { |
David Sedlák | 9c40a92 | 2019-07-08 17:04:43 +0200 | [diff] [blame] | 368 | LOGVAL_PARSER((struct lys_parser_ctx *)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] | 369 | return LY_EVALID; |
| 370 | } |
| 371 | |
| 372 | return LY_SUCCESS; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 373 | } |
| 374 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 375 | /** |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 376 | * @brief Get record with given type. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 377 | * |
| 378 | * @param[in] type Type of wanted record. |
| 379 | * @param[in] array_size Size of array. |
| 380 | * @param[in] array Searched array. |
| 381 | * |
| 382 | * @return Pointer to desired record on success, NULL if element is not in the array. |
| 383 | */ |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 384 | static struct yin_subelement * |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 385 | get_record(enum ly_stmt type, size_t array_size, struct yin_subelement *array) |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 386 | { |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 387 | for (unsigned int u = 0; u < array_size; ++u) { |
| 388 | if (array[u].type == type) { |
| 389 | return &array[u]; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 390 | } |
| 391 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 392 | return NULL; |
| 393 | } |
| 394 | |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 395 | /** |
| 396 | * @brief Helper function to check mandatory constraint of subelement. |
| 397 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 398 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 399 | * @param[in] subelem_info Array of information about subelements. |
| 400 | * @param[in] subelem_info_size Size of subelem_info array. |
| 401 | * @param[in] current_element Identification of element that is currently being parsed, used for logging. |
| 402 | * |
| 403 | * @return LY_ERR values. |
| 404 | */ |
| 405 | static LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 406 | yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 407 | signed char subelem_info_size, enum ly_stmt current_element) |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 408 | { |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 409 | for (signed char i = 0; i < subelem_info_size; ++i) { |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 410 | /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */ |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 411 | if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 412 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM, |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 413 | ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element)); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 414 | return LY_EVALID; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return LY_SUCCESS; |
| 419 | } |
| 420 | |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 421 | /** |
| 422 | * @brief Helper function to check "first" constraint of subelement. |
| 423 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 424 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 425 | * @param[in] subelem_info Array of information about subelements. |
| 426 | * @param[in] subelem_info_size Size of subelem_info array. |
| 427 | * @param[in] current_element Identification of element that is currently being parsed, used for logging. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 428 | * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement, used for logging. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 429 | * |
| 430 | * @return LY_ERR values. |
| 431 | */ |
| 432 | static LY_ERR |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 433 | yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 434 | signed char subelem_info_size, enum ly_stmt current_element, |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 435 | struct yin_subelement *exp_first) |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 436 | { |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 437 | for (signed char i = 0; i < subelem_info_size; ++i) { |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 438 | if (subelem_info[i].flags & YIN_SUBELEM_PARSED) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 439 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM, |
| 440 | ly_stmt2str(exp_first->type), ly_stmt2str(current_element)); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 441 | return LY_EVALID; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return LY_SUCCESS; |
| 446 | } |
| 447 | |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 448 | /** |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 449 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute, |
| 450 | * for example prefix or namespace element. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 451 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 452 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 453 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 454 | * @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] | 455 | * @param[in] kw Type of current element. |
| 456 | * @param[out] value Where value of attribute should be stored. |
| 457 | * @param[in] arg_type Expected type of attribute. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 458 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 459 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 460 | * |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 461 | * @return LY_ERR values. |
| 462 | */ |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 463 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 464 | yin_parse_simple_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt kw, |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 465 | const char **value, enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts) |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 466 | { |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 467 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw)); |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 468 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 469 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 470 | }; |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 471 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 472 | return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 476 | * @brief Parse path element. |
| 477 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 478 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 479 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 480 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 481 | * @param[in] kw Type of current element. |
| 482 | * @param[out] type Type structure to store parsed value, flags and extension instances. |
| 483 | * |
| 484 | * @return LY_ERR values. |
| 485 | */ |
| 486 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 487 | yin_parse_path(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt kw, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 488 | struct lysp_type *type) |
| 489 | { |
| 490 | LY_CHECK_RET(yin_parse_simple_element(ctx, attrs, data, kw, &type->path, |
| 491 | YIN_ARG_VALUE, Y_STR_ARG, &type->exts)); |
| 492 | type->flags |= LYS_SET_PATH; |
| 493 | |
| 494 | return LY_SUCCESS; |
| 495 | } |
| 496 | |
| 497 | /** |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 498 | * @brief Parse pattern element. |
| 499 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 500 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 501 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 502 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 503 | * @param[in,out] type Type structure to store parsed value, flags and extension instances. |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 504 | * |
| 505 | * @return LY_ERR values. |
| 506 | */ |
| 507 | static LY_ERR |
| 508 | yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 509 | struct lysp_type *type) |
| 510 | { |
| 511 | const char *real_value = NULL; |
| 512 | char *saved_value = NULL; |
| 513 | struct lysp_restr *restr; |
| 514 | |
| 515 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 516 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, LY_STMT_PATTERN)); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 517 | size_t len = strlen(real_value); |
| 518 | |
| 519 | saved_value = malloc(len + 2); |
| 520 | LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
| 521 | memmove(saved_value + 1, real_value, len); |
| 522 | FREE_STRING(ctx->xml_ctx.ctx, real_value); |
| 523 | saved_value[0] = 0x06; |
| 524 | saved_value[len + 1] = '\0'; |
| 525 | restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value); |
| 526 | LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
| 527 | type->flags |= LYS_SET_PATTERN; |
| 528 | |
| 529 | struct yin_subelement subelems[6] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 530 | {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE}, |
| 531 | {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE}, |
| 532 | {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE}, |
| 533 | {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE}, |
| 534 | {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE}, |
| 535 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 536 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 537 | return yin_parse_content(ctx, subelems, 6, data, LY_STMT_PATTERN, NULL, &restr->exts); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 538 | } |
| 539 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 540 | /** |
| 541 | * @brief Parse fraction-digits element. |
| 542 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 543 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 544 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 545 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 546 | * @param[in,out] type Type structure to store value, flags and extension instances. |
| 547 | * |
| 548 | * @return LY_ERR values. |
| 549 | */ |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 550 | static LY_ERR |
| 551 | yin_parse_fracdigits(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 552 | struct lysp_type *type) |
| 553 | { |
| 554 | const char *temp_val = NULL; |
| 555 | char *ptr; |
| 556 | unsigned long int num; |
| 557 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 558 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_FRACTION_DIGITS)); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 559 | |
| 560 | if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 561 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits"); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 562 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 563 | return LY_EVALID; |
| 564 | } |
| 565 | |
| 566 | errno = 0; |
| 567 | num = strtoul(temp_val, &ptr, 10); |
| 568 | if (*ptr != '\0') { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 569 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits"); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 570 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 571 | return LY_EVALID; |
| 572 | } |
| 573 | if ((errno == ERANGE) || (num > 18)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 574 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits"); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 575 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 576 | return LY_EVALID; |
| 577 | } |
David Sedlák | 2ab5d8e | 2019-07-16 11:19:41 +0200 | [diff] [blame] | 578 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 579 | type->fraction_digits = num; |
| 580 | type->flags |= LYS_SET_FRDIGITS; |
| 581 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 582 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 583 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 584 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_FRACTION_DIGITS, NULL, &type->exts); |
David Sedlák | f75d55e | 2019-07-12 16:52:50 +0200 | [diff] [blame] | 585 | } |
| 586 | |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 587 | /** |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 588 | * @brief Parse enum element. |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 589 | * |
| 590 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 591 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 592 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 593 | * @param[in,out] type Type structure to store parsed value, flags and extension instances. |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 594 | * |
| 595 | * @return LY_ERR values. |
| 596 | */ |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 597 | static LY_ERR |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 598 | yin_parse_enum(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_type *type) |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 599 | { |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 600 | struct lysp_type_enum *en; |
David Sedlák | 1e69678 | 2019-07-17 15:06:07 +0200 | [diff] [blame] | 601 | |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 602 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->enums, en, LY_EMEM); |
| 603 | type->flags |= LYS_SET_ENUM; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 604 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM)); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 605 | LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name))); |
| 606 | YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum"); |
| 607 | CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name); |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 608 | |
| 609 | struct yin_subelement subelems[6] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 610 | {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE}, |
| 611 | {LY_STMT_IF_FEATURE, &en->iffeatures, 0}, |
| 612 | {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE}, |
| 613 | {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE}, |
| 614 | {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE}, |
| 615 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 616 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 617 | return yin_parse_content(ctx, subelems, 6, data, LY_STMT_ENUM, NULL, &en->exts); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /** |
| 621 | * @brief Parse bit element. |
| 622 | * |
| 623 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 624 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 625 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 626 | * @param[in,out] type Type structure to store parsed value, flags and extension instances. |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 627 | * |
| 628 | * @return LY_ERR values. |
| 629 | */ |
| 630 | static LY_ERR |
| 631 | yin_parse_bit(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 632 | struct lysp_type *type) |
| 633 | { |
| 634 | struct lysp_type_enum *en; |
| 635 | |
| 636 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->bits, en, LY_EMEM); |
| 637 | type->flags |= LYS_SET_BIT; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 638 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT)); |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 639 | CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name); |
| 640 | |
| 641 | struct yin_subelement subelems[6] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 642 | {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE}, |
| 643 | {LY_STMT_IF_FEATURE, &en->iffeatures, 0}, |
| 644 | {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE}, |
| 645 | {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE}, |
| 646 | {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE}, |
| 647 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 648 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 649 | return yin_parse_content(ctx, subelems, 6, data, LY_STMT_BIT, NULL, &en->exts); |
David Sedlák | ca36c42 | 2019-07-12 12:47:55 +0200 | [diff] [blame] | 650 | } |
| 651 | |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 652 | /** |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 653 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have |
| 654 | * more instances, such as base or if-feature. |
| 655 | * |
| 656 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | bba38e5 | 2019-07-09 15:20:01 +0200 | [diff] [blame] | 657 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 658 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 659 | * @param[in] kw Type of current element. |
| 660 | * @param[out] values Parsed values to add to. |
| 661 | * @param[in] arg_type Expected type of attribute. |
David Sedlák | 292763b | 2019-07-09 11:10:53 +0200 | [diff] [blame] | 662 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 663 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 664 | * |
| 665 | * @return LY_ERR values. |
| 666 | */ |
| 667 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 668 | yin_parse_simple_elements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt kw, |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 669 | const char ***values, enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts) |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 670 | { |
| 671 | const char **value; |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 672 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM); |
David Sedlák | cb5d83f | 2019-07-09 09:32:53 +0200 | [diff] [blame] | 673 | uint32_t index = LY_ARRAY_SIZE(*values) - 1; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 674 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 675 | {LY_STMT_EXTENSION_INSTANCE, &index, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 676 | }; |
| 677 | |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 678 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw)); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 679 | |
| 680 | return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts); |
| 681 | } |
| 682 | |
| 683 | /** |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 684 | * @brief Parse simple element without any special constraints and argument mapped to yin attribute. |
| 685 | * |
| 686 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 687 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 688 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 689 | * @param[in] kw Type of current element. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 690 | * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 691 | * @param[in] arg_type Expected type of attribute. |
| 692 | * @param[in] arg_val_type Type of expected value of attribute. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 693 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 694 | * |
| 695 | * @return LY_ERR values. |
| 696 | */ |
| 697 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 698 | yin_parse_simple_elem(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt kw, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 699 | struct yin_subelement *subinfo, enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts) |
| 700 | { |
| 701 | if (subinfo->flags & YIN_SUBELEM_UNIQUE) { |
| 702 | LY_CHECK_RET(yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subinfo->dest, |
| 703 | arg_type, arg_val_type, exts)); |
| 704 | } else { |
| 705 | LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subinfo->dest, |
| 706 | arg_type, arg_val_type, exts)); |
| 707 | } |
| 708 | |
| 709 | return LY_SUCCESS; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * @brief Parse base element. |
| 714 | * |
| 715 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 716 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 717 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 718 | * @param[in] parent Identification of parent element. |
| 719 | * @param[out] dest Where parsed values should be stored. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 720 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 721 | * |
| 722 | * @return LY_ERR values. |
| 723 | */ |
| 724 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 725 | yin_parse_base(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt parent, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 726 | void *dest, struct lysp_ext_instance **exts) |
| 727 | { |
| 728 | struct lysp_type *type = NULL; |
| 729 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 730 | if (parent == LY_STMT_TYPE) { |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 731 | type = (struct lysp_type *)dest; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 732 | LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, LY_STMT_BASE, &type->bases, YIN_ARG_NAME, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 733 | Y_PREF_IDENTIF_ARG, exts)); |
| 734 | type->flags |= LYS_SET_BASE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 735 | } else if (parent == LY_STMT_IDENTITY) { |
| 736 | LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, LY_STMT_BASE, (const char ***)dest, |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 737 | YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts)); |
| 738 | } else { |
| 739 | LOGINT(ctx->xml_ctx.ctx); |
| 740 | return LY_EINT; |
| 741 | } |
| 742 | |
| 743 | return LY_SUCCESS; |
| 744 | } |
| 745 | |
| 746 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 747 | * @brief Parse require-instance element. |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 748 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 749 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 750 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 751 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 752 | * @prama[out] type Type structure to store value, flag and extensions. |
| 753 | * |
| 754 | * @return LY_ERR values. |
| 755 | */ |
| 756 | static LY_ERR |
| 757 | yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, |
| 758 | const char **data, struct lysp_type *type) |
| 759 | { |
| 760 | const char *temp_val = NULL; |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 761 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 762 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 763 | }; |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 764 | |
| 765 | type->flags |= LYS_SET_REQINST; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 766 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_REQUIRE_INSTANCE)); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 767 | if (strcmp(temp_val, "true") == 0) { |
| 768 | type->require_instance = 1; |
| 769 | } else if (strcmp(temp_val, "false") != 0) { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 770 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", |
| 771 | "require-instance", "true", "false"); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 772 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 773 | return LY_EVALID; |
| 774 | } |
| 775 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 776 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 777 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /** |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 781 | * @brief Parse modifier element. |
| 782 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 783 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 784 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 785 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 786 | * @param[in,out] pat Value to write to. |
| 787 | * @param[in,out] exts Extension instances to add to. |
| 788 | * |
| 789 | * @return LY_ERR values. |
| 790 | */ |
| 791 | static LY_ERR |
| 792 | yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 793 | const char **pat, struct lysp_ext_instance **exts) |
| 794 | { |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 795 | assert(**pat == 0x06); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 796 | const char *temp_val; |
| 797 | char *modified_val; |
| 798 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 799 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 800 | }; |
| 801 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 802 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MODIFIER)); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 803 | if (strcmp(temp_val, "invert-match") != 0) { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 804 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value", |
| 805 | "modifier", "invert-match"); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 806 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 807 | return LY_EVALID; |
| 808 | } |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 809 | lydict_remove(ctx->xml_ctx.ctx, temp_val); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 810 | |
| 811 | /* allocate new value */ |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 812 | modified_val = malloc(strlen(*pat) + 1); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 813 | LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 814 | strcpy(modified_val, *pat); |
| 815 | lydict_remove(ctx->xml_ctx.ctx, *pat); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 816 | |
| 817 | /* modify the new value */ |
| 818 | modified_val[0] = 0x15; |
| 819 | *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val); |
| 820 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 821 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_MODIFIER, NULL, exts); |
David Sedlák | ce77bf5 | 2019-07-11 16:59:31 +0200 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /** |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 825 | * @brief Parse a restriction element (length, range or one instance of must). |
| 826 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 827 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 828 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 829 | * @param[in,out] data Data to read from, always moved to currently handled character. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 830 | * @param[in] restr_kw Identificaton of element that is being parsed, can be set to LY_STMT_MUST, LY_STMT_LENGTH or LY_STMT_RANGE. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 831 | * @param[in] restr Value to write to. |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 832 | */ |
| 833 | static LY_ERR |
| 834 | yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 835 | enum ly_stmt restr_kw, struct lysp_restr *restr) |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 836 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 837 | assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 838 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 839 | {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE}, |
| 840 | {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE}, |
| 841 | {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE}, |
| 842 | {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE}, |
| 843 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 844 | }; |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 845 | /* argument of must is called condition, but argument of length and range is called value */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 846 | enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE; |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 847 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw)); |
| 848 | |
| 849 | return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts); |
| 850 | } |
| 851 | |
| 852 | /** |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 853 | * @brief Parse range element. |
| 854 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 855 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 856 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 857 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 858 | * @param[out] type Type structure to store parsed value and flags. |
| 859 | * |
| 860 | * @return LY_ERR values. |
| 861 | */ |
| 862 | static LY_ERR |
| 863 | yin_parse_range(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, |
| 864 | const char **data, struct lysp_type *type) |
| 865 | { |
| 866 | type->range = calloc(1, sizeof *type->range); |
| 867 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 868 | LY_CHECK_RET(yin_parse_restriction(ctx, attrs, data, LY_STMT_RANGE, type->range)); |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 869 | type->flags |= LYS_SET_RANGE; |
| 870 | |
| 871 | return LY_SUCCESS; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * @brief Parse length element. |
| 876 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 877 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 878 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 879 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 880 | * @param[out] type Type structure to store parsed value and flags. |
| 881 | * |
| 882 | * @return LY_ERR values. |
| 883 | */ |
| 884 | static LY_ERR |
| 885 | yin_parse_length(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, |
| 886 | const char **data, struct lysp_type *type) |
| 887 | { |
| 888 | type->length = calloc(1, sizeof *type->length); |
| 889 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 890 | LY_CHECK_RET(yin_parse_restriction(ctx, attrs, data, LY_STMT_LENGTH, type->length)); |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 891 | type->flags |= LYS_SET_LENGTH; |
| 892 | |
| 893 | return LY_SUCCESS; |
| 894 | } |
| 895 | |
| 896 | /** |
David Sedlák | bc9ec9c | 2019-07-11 15:53:55 +0200 | [diff] [blame] | 897 | * @brief Parse must element. |
| 898 | * |
| 899 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 900 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 901 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 902 | * @param[in,out] restrs Restrictions to add to. |
| 903 | * |
| 904 | * @return LY_ERR values. |
| 905 | */ |
| 906 | static LY_ERR |
| 907 | yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs) |
| 908 | { |
| 909 | struct lysp_restr *restr; |
| 910 | |
| 911 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 912 | return yin_parse_restriction(ctx, attrs, data, LY_STMT_MUST, restr); |
David Sedlák | bc9ec9c | 2019-07-11 15:53:55 +0200 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /** |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 916 | * @brief Parse position or value element. |
| 917 | * |
| 918 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 919 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 920 | * @param[in,out] data Data to read from, always moved to currently handled character. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 921 | * @param[in] kw Type of current element, can be set to LY_STMT_POSITION or LY_STMT_VALUE. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 922 | * @param[out] enm Enum structure to save value, flags and extension instances. |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 923 | * |
| 924 | * @return LY_ERR values. |
| 925 | */ |
| 926 | static LY_ERR |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 927 | yin_parse_value_pos(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 928 | enum ly_stmt kw, struct lysp_type_enum *enm) |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 929 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 930 | assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 931 | const char *temp_val = NULL; |
| 932 | char *ptr; |
| 933 | long int num; |
| 934 | unsigned long int unum; |
| 935 | |
| 936 | /* set value flag */ |
| 937 | enm->flags |= LYS_SET_VALUE; |
| 938 | |
| 939 | /* get attribute value */ |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 940 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw)); |
David Sedlák | ae5378f | 2019-07-17 11:37:59 +0200 | [diff] [blame] | 941 | if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '+') || |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 942 | ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 943 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 944 | goto error; |
| 945 | } |
| 946 | |
| 947 | /* convert value */ |
| 948 | errno = 0; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 949 | if (kw == LY_STMT_VALUE) { |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 950 | num = strtol(temp_val, &ptr, 10); |
| 951 | if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 952 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 953 | goto error; |
| 954 | } |
| 955 | } else { |
| 956 | unum = strtoul(temp_val, &ptr, 10); |
| 957 | if (unum > UINT64_C(4294967295)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 958 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 959 | goto error; |
| 960 | } |
| 961 | } |
| 962 | /* check if whole argument value was converted */ |
| 963 | if (*ptr != '\0') { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 964 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | ebcd0eb | 2019-07-16 17:55:12 +0200 | [diff] [blame] | 965 | goto error; |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 966 | } |
| 967 | if (errno == ERANGE) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 968 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", ly_stmt2str(kw)); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 969 | goto error; |
| 970 | } |
| 971 | /* save correctly ternary operator can't be used because num and unum have different signes */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 972 | if (kw == LY_STMT_VALUE) { |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 973 | enm->value = num; |
| 974 | } else { |
| 975 | enm->value = unum; |
| 976 | } |
| 977 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 978 | |
| 979 | /* parse subelements */ |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 980 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 981 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 982 | }; |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 983 | return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts); |
| 984 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 985 | error: |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 986 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 987 | return LY_EVALID; |
| 988 | } |
| 989 | |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 990 | |
| 991 | /** |
| 992 | * @brief Parse belongs-to element. |
| 993 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 994 | * @param[in] ctx YIN parser context for logging and to store current state. |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 995 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 996 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 997 | * @param[out] submod Structure of submodule that is being parsed. |
| 998 | * @param[in,out] exts Extension instances to add to. |
| 999 | * |
| 1000 | * @return LY_ERR values |
| 1001 | */ |
| 1002 | static LY_ERR |
| 1003 | yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1004 | struct lysp_submodule *submod, struct lysp_ext_instance **exts) |
| 1005 | { |
| 1006 | struct yin_subelement subelems[2] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1007 | {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 1008 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 1009 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1010 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO)); |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 1011 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1012 | return yin_parse_content(ctx, subelems, 2, data, LY_STMT_BELONGS_TO, NULL, exts); |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 1015 | /** |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1016 | * @brief Function to parse meta tags (description, contact, ...) eg. elements with |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1017 | * text element as child. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 1018 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1019 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 1020 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 1021 | * @param[in,out] data Data to read from, always moved to currently handled character. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1022 | * @param[in] elem_type Type of element can be set to LY_STMT_ORGANIZATION or LY_STMT_CONTACT or LY_STMT_DESCRIPTION or LY_STMT_REFERENCE. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 1023 | * @param[out] value Where the content of meta element should be stored. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1024 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 1025 | * |
| 1026 | * @return LY_ERR values. |
| 1027 | */ |
| 1028 | static LY_ERR |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 1029 | yin_parse_meta(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1030 | enum ly_stmt elem_type, const char **value, struct lysp_ext_instance **exts) |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 1031 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1032 | assert(elem_type == LY_STMT_ORGANIZATION || elem_type == LY_STMT_CONTACT || elem_type == LY_STMT_DESCRIPTION || elem_type == LY_STMT_REFERENCE); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 1033 | |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1034 | struct yin_subelement subelems[2] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1035 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
| 1036 | {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1037 | }; |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 1038 | /* check attributes */ |
| 1039 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 1040 | |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 1041 | /* parse content */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 1042 | return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts); |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /** |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1046 | * @brief Parse error-message element. |
| 1047 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1048 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 1049 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1050 | * @param[in,out] data Data to read from. |
| 1051 | * @param[out] value Where the content of error-message element should be stored. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1052 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1053 | * |
| 1054 | * @return LY_ERR values. |
| 1055 | */ |
| 1056 | static LY_ERR |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 1057 | yin_parse_err_msg(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 1058 | const char **value, struct lysp_ext_instance **exts) |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1059 | { |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1060 | struct yin_subelement subelems[2] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1061 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
| 1062 | {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST} |
David Sedlák | 968ac34 | 2019-07-11 15:17:59 +0200 | [diff] [blame] | 1063 | }; |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1064 | |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 1065 | /* check attributes */ |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 1066 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, LY_STMT_ERROR_MESSAGE)); |
David Sedlák | df2a973 | 2019-08-07 13:23:16 +0200 | [diff] [blame] | 1067 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1068 | return yin_parse_content(ctx, subelems, 2, data, LY_STMT_ERROR_MESSAGE, NULL, exts); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /** |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1072 | * @brief Parse type element. |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1073 | * |
| 1074 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1075 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1076 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1077 | * @param[in] parent Identification of parent element. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1078 | * @param[in,out] type Type to write to. |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1079 | * |
| 1080 | * @return LY_ERR values. |
| 1081 | */ |
| 1082 | static LY_ERR |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1083 | yin_parse_type(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1084 | enum ly_stmt parent, struct yin_subelement *subinfo) |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1085 | { |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1086 | struct lysp_type *type = NULL; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1087 | if (parent == LY_STMT_DEVIATE) { |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1088 | *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest); |
| 1089 | LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
| 1090 | type = *((struct lysp_type **)subinfo->dest); |
| 1091 | } else { |
| 1092 | type = (struct lysp_type *)subinfo->dest; |
| 1093 | } |
| 1094 | /* type as child of another type */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1095 | if (parent == LY_STMT_TYPE) { |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 1096 | struct lysp_type *nested_type = NULL; |
| 1097 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->types, nested_type, LY_EMEM); |
| 1098 | type->flags |= LYS_SET_TYPE; |
| 1099 | type = nested_type; |
| 1100 | } |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1101 | struct yin_subelement subelems[11] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1102 | {LY_STMT_BASE, type, 0}, |
| 1103 | {LY_STMT_BIT, type, 0}, |
| 1104 | {LY_STMT_ENUM, type, 0}, |
| 1105 | {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE}, |
| 1106 | {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE}, |
| 1107 | {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE}, |
| 1108 | {LY_STMT_PATTERN, type, 0}, |
| 1109 | {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE}, |
| 1110 | {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE}, |
| 1111 | {LY_STMT_TYPE, type}, |
| 1112 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1113 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1114 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE)); |
| 1115 | return yin_parse_content(ctx, subelems, 11, data, LY_STMT_TYPE, NULL, &type->exts); |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1118 | /** |
| 1119 | * @brief Parse max-elements element. |
| 1120 | * |
| 1121 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1122 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1123 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1124 | * @param[in,out] max Value to write to. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1125 | * @param[in] flags Flags to write to. |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1126 | * @param[in,out] exts Extension instances to add to. |
| 1127 | * |
| 1128 | * @return LY_ERR values. |
| 1129 | */ |
| 1130 | static LY_ERR |
| 1131 | yin_parse_maxelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *max, |
| 1132 | uint16_t *flags, struct lysp_ext_instance **exts) |
| 1133 | { |
| 1134 | const char *temp_val = NULL; |
| 1135 | char *ptr; |
| 1136 | unsigned long int num; |
| 1137 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1138 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1139 | }; |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1140 | |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1141 | *flags |= LYS_SET_MAX; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1142 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS)); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1143 | if (!temp_val || temp_val[0] == '\0' || temp_val[0] == '0' || (temp_val[0] != 'u' && !isdigit(temp_val[0]))) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1144 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements"); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1145 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1146 | return LY_EVALID; |
| 1147 | } |
| 1148 | |
| 1149 | if (strcmp(temp_val, "unbounded")) { |
| 1150 | errno = 0; |
| 1151 | num = strtoul(temp_val, &ptr, 10); |
| 1152 | if (*ptr != '\0') { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1153 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements"); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1154 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1155 | return LY_EVALID; |
| 1156 | } |
| 1157 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1158 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements"); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1159 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1160 | return LY_EVALID; |
| 1161 | } |
| 1162 | *max = num; |
| 1163 | } |
| 1164 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1165 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_MAX_ELEMENTS, NULL, exts); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1166 | } |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1167 | |
| 1168 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1169 | * @brief Parse min-elements element. |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1170 | * |
| 1171 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1172 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1173 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1174 | * @param[in,out] min Value to write to. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1175 | * @param[in] flags Flags to write to. |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1176 | * @param[in,out] exts Extension instances to add to. |
| 1177 | * |
| 1178 | * @return LY_ERR values. |
| 1179 | */ |
| 1180 | static LY_ERR |
| 1181 | yin_parse_minelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *min, |
| 1182 | uint16_t *flags, struct lysp_ext_instance **exts) |
| 1183 | { |
| 1184 | const char *temp_val = NULL; |
| 1185 | char *ptr; |
| 1186 | unsigned long int num; |
| 1187 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1188 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1189 | }; |
| 1190 | |
| 1191 | *flags |= LYS_SET_MIN; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1192 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MIN_ELEMENTS)); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1193 | |
| 1194 | if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '0' && temp_val[1] != '\0')) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1195 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements"); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1196 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1197 | return LY_EVALID; |
| 1198 | } |
| 1199 | |
| 1200 | errno = 0; |
| 1201 | num = strtoul(temp_val, &ptr, 10); |
| 1202 | if (ptr[0] != 0) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1203 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements"); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1204 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1205 | return LY_EVALID; |
| 1206 | } |
| 1207 | if (errno == ERANGE || num > UINT32_MAX) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1208 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements"); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1209 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1210 | return LY_EVALID; |
| 1211 | } |
| 1212 | *min = num; |
| 1213 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1214 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_MIN_ELEMENTS, NULL, exts); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1217 | /** |
| 1218 | * @brief Parse min-elements or max-elements element. |
| 1219 | * |
| 1220 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1221 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1222 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1223 | * @param[in] parent Identification of parent element. |
| 1224 | * @param[in] current Identification of current element. |
| 1225 | * @param[in] dest Where the parsed value and flags should be stored. |
| 1226 | * |
| 1227 | * @return LY_ERR values. |
| 1228 | */ |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1229 | static LY_ERR |
| 1230 | yin_parse_minmax(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1231 | enum ly_stmt parent, enum ly_stmt current, void *dest) |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1232 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1233 | assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS); |
| 1234 | assert(parent == LY_STMT_LEAF_LIST || parent == LY_STMT_REFINE || parent == LY_STMT_LIST || parent == LY_STMT_DEVIATE); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1235 | uint32_t *lim; |
| 1236 | uint16_t *flags; |
| 1237 | struct lysp_ext_instance **exts; |
| 1238 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1239 | if (parent == LY_STMT_LEAF_LIST) { |
| 1240 | lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1241 | flags = &((struct lysp_node_leaflist *)dest)->flags; |
| 1242 | exts = &((struct lysp_node_leaflist *)dest)->exts; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1243 | } else if (parent == LY_STMT_REFINE) { |
| 1244 | lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1245 | flags = &((struct lysp_refine *)dest)->flags; |
| 1246 | exts = &((struct lysp_refine *)dest)->exts; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1247 | } else if (parent == LY_STMT_LIST) { |
| 1248 | lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1249 | flags = &((struct lysp_node_list *)dest)->flags; |
| 1250 | exts = &((struct lysp_node_list *)dest)->exts; |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 1251 | } else { |
| 1252 | lim = ((struct minmax_dev_meta *)dest)->lim; |
| 1253 | flags = ((struct minmax_dev_meta *)dest)->flags; |
| 1254 | exts = ((struct minmax_dev_meta *)dest)->exts; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1257 | if (current == LY_STMT_MAX_ELEMENTS) { |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1258 | LY_CHECK_RET(yin_parse_maxelements(ctx, attrs, data, lim, flags, exts)); |
| 1259 | } else { |
| 1260 | LY_CHECK_RET(yin_parse_minelements(ctx, attrs, data, lim, flags, exts)); |
| 1261 | } |
| 1262 | |
| 1263 | return LY_SUCCESS; |
| 1264 | } |
| 1265 | |
| 1266 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1267 | * @brief Parse ordered-by element. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1268 | * |
| 1269 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1270 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1271 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1272 | * @param[out] flags Flags to write to. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1273 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1274 | * |
| 1275 | * @return LY_ERR values. |
| 1276 | */ |
| 1277 | static LY_ERR |
| 1278 | yin_parse_orderedby(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1279 | uint16_t *flags, struct lysp_ext_instance **exts) |
| 1280 | { |
| 1281 | const char *temp_val; |
| 1282 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1283 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1284 | }; |
| 1285 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1286 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_ORDERED_BY)); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1287 | if (strcmp(temp_val, "system") == 0) { |
| 1288 | *flags |= LYS_ORDBY_SYSTEM; |
| 1289 | } else if (strcmp(temp_val, "user") == 0) { |
| 1290 | *flags |= LYS_ORDBY_USER; |
| 1291 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1292 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", |
| 1293 | "ordered-by", "system", "user"); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1294 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1295 | return LY_EVALID; |
| 1296 | } |
| 1297 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1298 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1299 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_ORDERED_BY, NULL, exts); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1303 | * @brief Parse any-data or any-xml element. |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1304 | * |
| 1305 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1306 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1307 | * @param[in,out] data Data to read from, always moved to currently handled character. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1308 | * @param[in] any_kw Identification of current element, can be set to LY_STMT_ANYDATA or LY_STMT_ANYXML |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1309 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1310 | * |
| 1311 | * @return LY_ERR values. |
| 1312 | */ |
| 1313 | static LY_ERR |
| 1314 | yin_parse_any(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1315 | enum ly_stmt any_kw, struct tree_node_meta *node_meta) |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1316 | { |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1317 | struct lysp_node_anydata *any; |
| 1318 | |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 1319 | /* create new sibling */ |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1320 | LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, any, next); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1321 | any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML; |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1322 | any->parent = node_meta->parent; |
| 1323 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1324 | /* parse argument */ |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1325 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw)); |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1326 | |
| 1327 | struct yin_subelement subelems[9] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1328 | {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE}, |
| 1329 | {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE}, |
| 1330 | {LY_STMT_IF_FEATURE, &any->iffeatures, 0}, |
| 1331 | {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE}, |
| 1332 | {LY_STMT_MUST, &any->musts, 0}, |
| 1333 | {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE}, |
| 1334 | {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE}, |
| 1335 | {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE}, |
| 1336 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1337 | }; |
| 1338 | return yin_parse_content(ctx, subelems, 9, data, any_kw, NULL, &any->exts); |
| 1339 | } |
| 1340 | |
| 1341 | /** |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1342 | * @brief parse leaf element. |
| 1343 | * |
| 1344 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1345 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1346 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1347 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1348 | * |
| 1349 | * @return LY_ERR values. |
| 1350 | */ |
| 1351 | static LY_ERR |
| 1352 | yin_parse_leaf(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1353 | struct tree_node_meta *node_meta) |
| 1354 | { |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1355 | struct lysp_node_leaf *leaf; |
| 1356 | |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 1357 | /* create structure new leaf */ |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1358 | LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, leaf, next); |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1359 | leaf->nodetype = LYS_LEAF; |
| 1360 | leaf->parent = node_meta->parent; |
| 1361 | |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1362 | /* parser argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1363 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, LY_STMT_LEAF)); |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1364 | |
| 1365 | /* parse content */ |
| 1366 | struct yin_subelement subelems[12] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1367 | {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE}, |
| 1368 | {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE}, |
| 1369 | {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE}, |
| 1370 | {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0}, |
| 1371 | {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE}, |
| 1372 | {LY_STMT_MUST, &leaf->musts, 0}, |
| 1373 | {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE}, |
| 1374 | {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE}, |
| 1375 | {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY}, |
| 1376 | {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE}, |
| 1377 | {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE}, |
| 1378 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 1379 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1380 | return yin_parse_content(ctx, subelems, 12, data, LY_STMT_LEAF, NULL, &leaf->exts); |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /** |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1384 | * @brief Parse leaf-list element. |
| 1385 | * |
| 1386 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1387 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1388 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1389 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1390 | * |
| 1391 | * @return LY_ERR values. |
| 1392 | */ |
| 1393 | static LY_ERR |
| 1394 | yin_parse_leaflist(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1395 | struct tree_node_meta *node_meta) |
| 1396 | { |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1397 | struct lysp_node_leaflist *llist; |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1398 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1399 | LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, llist, next); |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 1400 | |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1401 | llist->nodetype = LYS_LEAFLIST; |
| 1402 | llist->parent = node_meta->parent; |
| 1403 | |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1404 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1405 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, LY_STMT_LEAF_LIST)); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1406 | |
| 1407 | /* parse content */ |
| 1408 | struct yin_subelement subelems[14] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1409 | {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE}, |
| 1410 | {LY_STMT_DEFAULT, &llist->dflts, 0}, |
| 1411 | {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE}, |
| 1412 | {LY_STMT_IF_FEATURE, &llist->iffeatures, 0}, |
| 1413 | {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE}, |
| 1414 | {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE}, |
| 1415 | {LY_STMT_MUST, &llist->musts, 0}, |
| 1416 | {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE}, |
| 1417 | {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE}, |
| 1418 | {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE}, |
| 1419 | {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY}, |
| 1420 | {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE}, |
| 1421 | {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE}, |
| 1422 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1423 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1424 | LY_CHECK_RET(yin_parse_content(ctx, subelems, 14, data, LY_STMT_LEAF_LIST, NULL, &llist->exts)); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1425 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1426 | /* check invalid combination of subelements */ |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1427 | if ((llist->min) && (llist->dflts)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1428 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1429 | return LY_EVALID; |
| 1430 | } |
| 1431 | if (llist->max && llist->min > llist->max) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1432 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1433 | return LY_EVALID; |
| 1434 | } |
| 1435 | |
| 1436 | return LY_SUCCESS; |
| 1437 | } |
| 1438 | |
| 1439 | /** |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1440 | * @brief Parse typedef element. |
| 1441 | * |
| 1442 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1443 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1444 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 1445 | * @param[in] typedef_meta Meta information about parent node and typedefs to add to. |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1446 | * |
| 1447 | * @return LY_ERR values. |
| 1448 | */ |
| 1449 | static LY_ERR |
| 1450 | yin_parse_typedef(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 1451 | struct tree_node_meta *typedef_meta) |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1452 | { |
| 1453 | struct lysp_tpdf *tpdf; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1454 | struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes; |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 1455 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *tpdfs, tpdf, LY_EMEM); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1456 | |
| 1457 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1458 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, LY_STMT_TYPEDEF)); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1459 | |
| 1460 | /* parse content */ |
| 1461 | struct yin_subelement subelems[7] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1462 | {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE}, |
| 1463 | {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE}, |
| 1464 | {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE}, |
| 1465 | {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE}, |
| 1466 | {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY}, |
| 1467 | {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE}, |
| 1468 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1469 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1470 | LY_CHECK_RET(yin_parse_content(ctx, subelems, 7, data, LY_STMT_TYPEDEF, NULL, &tpdf->exts)); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1471 | |
| 1472 | /* store data for collision check */ |
| 1473 | if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) { |
David Sedlák | e8b74df | 2019-08-14 14:18:22 +0200 | [diff] [blame] | 1474 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0) == -1, LY_EMEM); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | return LY_SUCCESS; |
| 1478 | } |
| 1479 | |
| 1480 | /** |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1481 | * @brief Parse refine element. |
| 1482 | * |
| 1483 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1484 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1485 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1486 | * @param[in,out] refines Refines to add to. |
| 1487 | * |
| 1488 | * @return LY_ERR values. |
| 1489 | */ |
| 1490 | static LY_ERR |
| 1491 | yin_parse_refine(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1492 | struct lysp_refine **refines) |
| 1493 | { |
| 1494 | struct lysp_refine *rf; |
| 1495 | |
| 1496 | /* allocate new refine */ |
| 1497 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *refines, rf, LY_EMEM); |
| 1498 | |
| 1499 | /* parse attribute */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1500 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE)); |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1501 | YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(rf->nodeid), "refine"); |
| 1502 | |
| 1503 | /* parse content */ |
| 1504 | struct yin_subelement subelems[11] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1505 | {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE}, |
| 1506 | {LY_STMT_DEFAULT, &rf->dflts, 0}, |
| 1507 | {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE}, |
| 1508 | {LY_STMT_IF_FEATURE, &rf->iffeatures, 0}, |
| 1509 | {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE}, |
| 1510 | {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE}, |
| 1511 | {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE}, |
| 1512 | {LY_STMT_MUST, &rf->musts, 0}, |
| 1513 | {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE}, |
| 1514 | {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE}, |
| 1515 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1516 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1517 | return yin_parse_content(ctx, subelems, 11, data, LY_STMT_REFINE, NULL, &rf->exts); |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /** |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1521 | * @brief Parse uses element. |
| 1522 | * |
| 1523 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1524 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1525 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 1526 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1527 | * |
| 1528 | * @return LY_ERR values. |
| 1529 | */ |
| 1530 | static LY_ERR |
| 1531 | yin_parse_uses(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1532 | struct tree_node_meta *node_meta) |
| 1533 | { |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1534 | struct lysp_node_uses *uses; |
| 1535 | |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 1536 | /* create new uses */ |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1537 | LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, uses, next); |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1538 | uses->nodetype = LYS_USES; |
| 1539 | uses->parent = node_meta->parent; |
| 1540 | |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1541 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1542 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, LY_STMT_USES)); |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1543 | |
| 1544 | /* parse content */ |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 1545 | struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments}; |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1546 | struct yin_subelement subelems[8] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1547 | {LY_STMT_AUGMENT, &augments, 0}, |
| 1548 | {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE}, |
| 1549 | {LY_STMT_IF_FEATURE, &uses->iffeatures, 0}, |
| 1550 | {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE}, |
| 1551 | {LY_STMT_REFINE, &uses->refines, 0}, |
| 1552 | {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE}, |
| 1553 | {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE}, |
| 1554 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1555 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1556 | LY_CHECK_RET(yin_parse_content(ctx, subelems, 8, data, LY_STMT_USES, NULL, &uses->exts)); |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 1557 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL)); |
| 1558 | |
| 1559 | return LY_SUCCESS; |
| 1560 | } |
| 1561 | |
| 1562 | /** |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1563 | * @brief Parse revision element. |
| 1564 | * |
| 1565 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1566 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1567 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1568 | * @param[in,out] revs Parsed revisions to add to. |
| 1569 | * |
| 1570 | * @return LY_ERR values. |
| 1571 | */ |
| 1572 | static LY_ERR |
| 1573 | yin_parse_revision(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1574 | struct lysp_revision **revs) |
| 1575 | { |
| 1576 | struct lysp_revision *rev; |
| 1577 | const char *temp_date = NULL; |
| 1578 | |
| 1579 | /* allocate new reivison */ |
| 1580 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *revs, rev, LY_EMEM); |
| 1581 | |
| 1582 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1583 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_date, Y_STR_ARG, LY_STMT_REVISION)); |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1584 | /* check value */ |
| 1585 | if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) { |
| 1586 | FREE_STRING(ctx->xml_ctx.ctx, temp_date); |
| 1587 | return LY_EVALID; |
| 1588 | } |
| 1589 | strcpy(rev->date, temp_date); |
| 1590 | FREE_STRING(ctx->xml_ctx.ctx, temp_date); |
| 1591 | |
| 1592 | /* parse content */ |
| 1593 | struct yin_subelement subelems[3] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1594 | {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE}, |
| 1595 | {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE}, |
| 1596 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1597 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1598 | return yin_parse_content(ctx, subelems, 3, data, LY_STMT_REVISION, NULL, &rev->exts); |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1601 | /** |
| 1602 | * @brief Parse include element. |
| 1603 | * |
| 1604 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1605 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1606 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1607 | * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to. |
| 1608 | * |
| 1609 | * @return LY_ERR values. |
| 1610 | */ |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1611 | static LY_ERR |
| 1612 | yin_parse_include(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1613 | struct include_meta *inc_meta) |
| 1614 | { |
| 1615 | struct lysp_include *inc; |
| 1616 | |
| 1617 | /* allocate new include */ |
| 1618 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *inc_meta->includes, inc, LY_EMEM); |
| 1619 | |
| 1620 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1621 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, LY_STMT_INCLUDE)); |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1622 | |
| 1623 | /* submodules share the namespace with the module names, so there must not be |
| 1624 | * a module of the same name in the context, no need for revision matching */ |
| 1625 | if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xml_ctx.ctx, inc->name)) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 1626 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, inc->name); |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1627 | return LY_EVALID; |
| 1628 | } |
| 1629 | |
| 1630 | /* parse content */ |
| 1631 | struct yin_subelement subelems[4] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1632 | {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2}, |
| 1633 | {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2}, |
| 1634 | {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE}, |
| 1635 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1636 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1637 | return yin_parse_content(ctx, subelems, 4, data, LY_STMT_INCLUDE, NULL, &inc->exts); |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 1638 | } |
| 1639 | |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 1640 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1641 | * @brief Parse revision-date element. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1642 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1643 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1644 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of revision-date element. |
| 1645 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1646 | * @param[in,out] rev Array to store the parsed value in. |
| 1647 | * @param[in,out] exts Extension instances to add to. |
| 1648 | * |
| 1649 | * @return LY_ERR values. |
| 1650 | */ |
| 1651 | static LY_ERR |
| 1652 | yin_parse_revision_date(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, char *rev, |
| 1653 | struct lysp_ext_instance **exts) |
| 1654 | { |
| 1655 | const char *temp_rev; |
| 1656 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1657 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1658 | }; |
| 1659 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1660 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1661 | LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, |
| 1662 | FREE_STRING(ctx->xml_ctx.ctx, temp_rev), LY_EVALID); |
| 1663 | |
| 1664 | strcpy(rev, temp_rev); |
| 1665 | FREE_STRING(ctx->xml_ctx.ctx, temp_rev); |
| 1666 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1667 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_REVISION_DATE, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /** |
| 1671 | * @brief Parse config element. |
| 1672 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1673 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1674 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element. |
| 1675 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1676 | * @param[in,out] flags Flags to add to. |
| 1677 | * @param[in,out] exts Extension instances to add to. |
| 1678 | * |
| 1679 | * @return LY_ERR values. |
| 1680 | */ |
| 1681 | static LY_ERR |
| 1682 | yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags, |
| 1683 | struct lysp_ext_instance **exts) |
| 1684 | { |
| 1685 | const char *temp_val = NULL; |
| 1686 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1687 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1688 | }; |
| 1689 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1690 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_CONFIG)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1691 | if (strcmp(temp_val, "true") == 0) { |
| 1692 | *flags |= LYS_CONFIG_W; |
| 1693 | } else if (strcmp(temp_val, "false") == 0) { |
| 1694 | *flags |= LYS_CONFIG_R; |
| 1695 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1696 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config", |
| 1697 | "true", "false"); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1698 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1699 | return LY_EVALID; |
| 1700 | } |
| 1701 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1702 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1703 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_CONFIG, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /** |
| 1707 | * @brief Parse yang-version element. |
| 1708 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1709 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1710 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of yang-version element. |
| 1711 | * @param[in] data Data to read from, always moved to currently handled character. |
| 1712 | * @param[out] version Storage for the parsed information. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1713 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1714 | * |
| 1715 | * @return LY_ERR values. |
| 1716 | */ |
| 1717 | static LY_ERR |
| 1718 | yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version, |
| 1719 | struct lysp_ext_instance **exts) |
| 1720 | { |
| 1721 | const char *temp_version = NULL; |
| 1722 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1723 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1724 | }; |
| 1725 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1726 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, LY_STMT_YANG_VERSION)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1727 | if (strcmp(temp_version, "1.0") == 0) { |
| 1728 | *version = LYS_VERSION_1_0; |
| 1729 | } else if (strcmp(temp_version, "1.1") == 0) { |
| 1730 | *version = LYS_VERSION_1_1; |
| 1731 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1732 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value", |
| 1733 | "yang-version", "1.0", "1.1"); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1734 | FREE_STRING(ctx->xml_ctx.ctx, temp_version); |
| 1735 | return LY_EVALID; |
| 1736 | } |
| 1737 | FREE_STRING(ctx->xml_ctx.ctx, temp_version); |
| 1738 | ctx->mod_version = *version; |
| 1739 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1740 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_YANG_VERSION, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | /** |
| 1744 | * @brief Parse import element. |
| 1745 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1746 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1747 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element. |
| 1748 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1749 | * @param[in,out] imp_meta Meta information about prefix and imports to add to. |
| 1750 | * |
| 1751 | * @return LY_ERR values. |
| 1752 | */ |
| 1753 | static LY_ERR |
| 1754 | yin_parse_import(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct import_meta *imp_meta) |
| 1755 | { |
| 1756 | struct lysp_import *imp; |
| 1757 | /* allocate new element in sized array for import */ |
| 1758 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *imp_meta->imports, imp, LY_EMEM); |
| 1759 | |
| 1760 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1761 | {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE}, |
| 1762 | {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}, |
| 1763 | {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE}, |
| 1764 | {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE}, |
| 1765 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1766 | }; |
| 1767 | |
| 1768 | /* parse import attributes */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1769 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT)); |
| 1770 | LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, LY_STMT_IMPORT, NULL, &imp->exts)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1771 | /* check prefix validity */ |
| 1772 | LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID); |
| 1773 | |
| 1774 | return LY_SUCCESS; |
| 1775 | } |
| 1776 | |
| 1777 | /** |
| 1778 | * @brief Parse mandatory element. |
| 1779 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1780 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1781 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of status element. |
| 1782 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1783 | * @param[in,out] flags Flags to add to. |
| 1784 | * @param[in,out] exts Extension instances to add to. |
| 1785 | * |
| 1786 | * @return LY_ERR values. |
| 1787 | */ |
| 1788 | static LY_ERR |
| 1789 | yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags, |
| 1790 | struct lysp_ext_instance **exts) |
| 1791 | { |
| 1792 | const char *temp_val = NULL; |
| 1793 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1794 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1795 | }; |
| 1796 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1797 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MANDATORY)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1798 | if (strcmp(temp_val, "true") == 0) { |
| 1799 | *flags |= LYS_MAND_TRUE; |
| 1800 | } else if (strcmp(temp_val, "false") == 0) { |
| 1801 | *flags |= LYS_MAND_FALSE; |
| 1802 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1803 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", |
| 1804 | "mandatory", "true", "false"); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1805 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1806 | return LY_EVALID; |
| 1807 | } |
| 1808 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1809 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1810 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_MANDATORY, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | /** |
| 1814 | * @brief Parse status element. |
| 1815 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1816 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1817 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of status element. |
| 1818 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1819 | * @param[in,out] flags Flags to add to. |
| 1820 | * @param[in,out] exts Extension instances to add to. |
| 1821 | * |
| 1822 | * @return LY_ERR values. |
| 1823 | */ |
| 1824 | static LY_ERR |
| 1825 | yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags, |
| 1826 | struct lysp_ext_instance **exts) |
| 1827 | { |
| 1828 | const char *value = NULL; |
| 1829 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1830 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1831 | }; |
| 1832 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1833 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &value, Y_STR_ARG, LY_STMT_STATUS)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1834 | if (strcmp(value, "current") == 0) { |
| 1835 | *flags |= LYS_STATUS_CURR; |
| 1836 | } else if (strcmp(value, "deprecated") == 0) { |
| 1837 | *flags |= LYS_STATUS_DEPRC; |
| 1838 | } else if (strcmp(value, "obsolete") == 0) { |
| 1839 | *flags |= LYS_STATUS_OBSLT; |
| 1840 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1841 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value", |
| 1842 | "status", "current", "deprecated", "obsolete"); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1843 | FREE_STRING(ctx->xml_ctx.ctx, value); |
| 1844 | return LY_EVALID; |
| 1845 | } |
| 1846 | FREE_STRING(ctx->xml_ctx.ctx, value); |
| 1847 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1848 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_STATUS, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | /** |
| 1852 | * @brief Parse when element. |
| 1853 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1854 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1855 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of when element. |
| 1856 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1857 | * @param[out] when_p When pointer to parse to. |
| 1858 | */ |
| 1859 | static LY_ERR |
| 1860 | yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p) |
| 1861 | { |
| 1862 | struct lysp_when *when; |
David Sedlák | a56e001 | 2019-08-15 13:21:25 +0200 | [diff] [blame] | 1863 | LY_ERR ret = LY_SUCCESS; |
| 1864 | |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1865 | when = calloc(1, sizeof *when); |
| 1866 | LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1867 | ret = yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN); |
David Sedlák | a56e001 | 2019-08-15 13:21:25 +0200 | [diff] [blame] | 1868 | LY_CHECK_ERR_RET(ret, free(when), ret); |
| 1869 | |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1870 | *when_p = when; |
| 1871 | struct yin_subelement subelems[3] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1872 | {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE}, |
| 1873 | {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE}, |
| 1874 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1875 | }; |
| 1876 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1877 | return yin_parse_content(ctx, subelems, 3, data, LY_STMT_WHEN, NULL, &when->exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /** |
| 1881 | * @brief Parse yin-elemenet element. |
| 1882 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1883 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1884 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of yin-element element. |
| 1885 | * @param[in,out] data Data to read from, always moved to currently handled position. |
| 1886 | * @param[in,out] flags Flags to add to. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1887 | * @prama[in,out] exts Extension instances to add to. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1888 | * |
| 1889 | * @return LY_ERR values. |
| 1890 | */ |
| 1891 | static LY_ERR |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 1892 | yin_parse_yin_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1893 | uint16_t *flags, struct lysp_ext_instance **exts) |
| 1894 | { |
| 1895 | const char *temp_val = NULL; |
| 1896 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1897 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1898 | }; |
| 1899 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1900 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_YIN_ELEMENT)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1901 | if (strcmp(temp_val, "true") == 0) { |
| 1902 | *flags |= LYS_YINELEM_TRUE; |
| 1903 | } else if (strcmp(temp_val, "false") == 0) { |
| 1904 | *flags |= LYS_YINELEM_FALSE; |
| 1905 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 1906 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", |
| 1907 | "yin-element", "true", "false"); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1908 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1909 | return LY_EVALID; |
| 1910 | } |
| 1911 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 1912 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1913 | return yin_parse_content(ctx, subelems, 1, data, LY_STMT_YIN_ELEMENT, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | /** |
| 1917 | * @brief Parse argument element. |
| 1918 | * |
| 1919 | * @param[in,out] xml_ctx Xml context. |
| 1920 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of argument element. |
| 1921 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1922 | * @param[in,out] arg_meta Meta information about destionation of parsed data. |
| 1923 | * @param[in,out] exts Extension instances to add to. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1924 | * |
| 1925 | * @return LY_ERR values. |
| 1926 | */ |
| 1927 | static LY_ERR |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 1928 | yin_parse_argument(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1929 | struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts) |
| 1930 | { |
| 1931 | struct yin_subelement subelems[2] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1932 | {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE}, |
| 1933 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1934 | }; |
| 1935 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1936 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, LY_STMT_ARGUMENT)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1937 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1938 | return yin_parse_content(ctx, subelems, 2, data, LY_STMT_ARGUMENT, NULL, exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | /** |
| 1942 | * @brief Parse the extension statement. |
| 1943 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 1944 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1945 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension element. |
| 1946 | * @param[in,out] data Data to read from. |
| 1947 | * @param[in,out] extensions Extensions to add to. |
| 1948 | * |
| 1949 | * @return LY_ERR values. |
| 1950 | */ |
| 1951 | static LY_ERR |
| 1952 | yin_parse_extension(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_ext **extensions) |
| 1953 | { |
| 1954 | struct lysp_ext *ex; |
| 1955 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1956 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, LY_STMT_EXTENSION)); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1957 | |
| 1958 | struct yin_argument_meta arg_info = {&ex->flags, &ex->argument}; |
| 1959 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1960 | {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE}, |
| 1961 | {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE}, |
| 1962 | {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE}, |
| 1963 | {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE}, |
| 1964 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1965 | }; |
| 1966 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1967 | return yin_parse_content(ctx, subelems, 5, data, LY_STMT_EXTENSION, NULL, &ex->exts); |
David Sedlák | dfbbb44 | 2019-08-06 16:33:21 +0200 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /** |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1971 | * @brief Parse feature element. |
| 1972 | * |
| 1973 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 1974 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 1975 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1976 | * @param[in,out] features Features to add to. |
| 1977 | * |
| 1978 | * @return LY_ERR values. |
| 1979 | */ |
| 1980 | static LY_ERR |
| 1981 | yin_parse_feature(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 1982 | struct lysp_feature **features) |
| 1983 | { |
| 1984 | struct lysp_feature *feat; |
| 1985 | |
| 1986 | /* allocate new feature */ |
| 1987 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *features, feat, LY_EMEM); |
| 1988 | |
| 1989 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1990 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, LY_STMT_FEATURE)); |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1991 | |
| 1992 | /* parse content */ |
| 1993 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1994 | {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE}, |
| 1995 | {LY_STMT_IF_FEATURE, &feat->iffeatures, 0}, |
| 1996 | {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE}, |
| 1997 | {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE}, |
| 1998 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 1999 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2000 | return yin_parse_content(ctx, subelems, 5, data, LY_STMT_FEATURE, NULL, &feat->exts); |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | /** |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 2004 | * @brief Parse identity element. |
| 2005 | * |
| 2006 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2007 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2008 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2009 | * @param[in,out] identities Identities to add to. |
| 2010 | * |
| 2011 | * @return LY_ERR values. |
| 2012 | */ |
| 2013 | static LY_ERR |
| 2014 | yin_parse_identity(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 2015 | struct lysp_ident **identities) |
| 2016 | { |
| 2017 | struct lysp_ident *ident; |
| 2018 | |
| 2019 | /* allocate new identity */ |
| 2020 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *identities, ident, LY_EMEM); |
| 2021 | |
| 2022 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2023 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, LY_STMT_IDENTITY)); |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 2024 | |
| 2025 | /* parse content */ |
| 2026 | struct yin_subelement subelems[6] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2027 | {LY_STMT_BASE, &ident->bases, 0}, |
| 2028 | {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE}, |
| 2029 | {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2}, |
| 2030 | {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE}, |
| 2031 | {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE}, |
| 2032 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 2033 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2034 | return yin_parse_content(ctx, subelems, 6, data, LY_STMT_IDENTITY, NULL, &ident->exts); |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | /** |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2038 | * @brief Parse list element. |
| 2039 | * |
| 2040 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2041 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2042 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 2043 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2044 | * |
| 2045 | * @return LY_ERR values. |
| 2046 | */ |
| 2047 | static LY_ERR |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2048 | yin_parse_list(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 2049 | struct tree_node_meta *node_meta) |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2050 | { |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2051 | struct lysp_node_list *list; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2052 | LY_ERR ret = LY_SUCCESS; |
| 2053 | struct yin_subelement *subelems = NULL; |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2054 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2055 | LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, list, next); |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2056 | list->nodetype = LYS_LIST; |
| 2057 | list->parent = node_meta->parent; |
| 2058 | |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2059 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2060 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, LY_STMT_LIST)); |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2061 | |
| 2062 | /* parse list content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2063 | LY_CHECK_RET(subelems_allocator(ctx, 25, (struct lysp_node *)list, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2064 | LY_STMT_ACTION, &list->actions, 0, |
| 2065 | LY_STMT_ANYDATA, &list->child, 0, |
| 2066 | LY_STMT_ANYXML, &list->child, 0, |
| 2067 | LY_STMT_CHOICE, &list->child, 0, |
| 2068 | LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE, |
| 2069 | LY_STMT_CONTAINER, &list->child, 0, |
| 2070 | LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE, |
| 2071 | LY_STMT_GROUPING, &list->groupings, 0, |
| 2072 | LY_STMT_IF_FEATURE, &list->iffeatures, 0, |
| 2073 | LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE, |
| 2074 | LY_STMT_LEAF, &list->child, 0, |
| 2075 | LY_STMT_LEAF_LIST, &list->child, 0, |
| 2076 | LY_STMT_LIST, &list->child, 0, |
| 2077 | LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE, |
| 2078 | LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE, |
| 2079 | LY_STMT_MUST, &list->musts, 0, |
| 2080 | LY_STMT_NOTIFICATION, &list->notifs, 0, |
| 2081 | LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE, |
| 2082 | LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE, |
| 2083 | LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE, |
| 2084 | LY_STMT_TYPEDEF, &list->typedefs, 0, |
| 2085 | LY_STMT_UNIQUE, &list->uniques, 0, |
| 2086 | LY_STMT_USES, &list->child, 0, |
| 2087 | LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE, |
| 2088 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2089 | )); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2090 | ret = yin_parse_content(ctx, subelems, 25, data, LY_STMT_LIST, NULL, &list->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2091 | subelems_deallocator(25, subelems); |
| 2092 | LY_CHECK_RET(ret); |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2093 | |
| 2094 | /* finalize parent pointers to the reallocated items */ |
| 2095 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs)); |
| 2096 | |
| 2097 | if (list->max && list->min > list->max) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 2098 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max); |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 2099 | return LY_EVALID; |
| 2100 | } |
| 2101 | |
| 2102 | return LY_SUCCESS; |
| 2103 | } |
| 2104 | |
| 2105 | /** |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2106 | * @brief Parse notification element. |
| 2107 | * |
| 2108 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2109 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2110 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2111 | * @param[in,out] notif_meta Meta information about parent node and notifications to add to. |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2112 | * |
| 2113 | * @return LY_ERR values. |
| 2114 | */ |
| 2115 | static LY_ERR |
| 2116 | yin_parse_notification(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2117 | struct tree_node_meta *notif_meta) |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2118 | { |
| 2119 | struct lysp_notif *notif; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2120 | struct lysp_notif **notifs = (struct lysp_notif **)notif_meta->nodes; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2121 | LY_ERR ret = LY_SUCCESS; |
| 2122 | struct yin_subelement *subelems = NULL; |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2123 | |
| 2124 | /* allocate new notification */ |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2125 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *notifs, notif, LY_EMEM); |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2126 | notif->nodetype = LYS_NOTIF; |
| 2127 | notif->parent = notif_meta->parent; |
| 2128 | |
| 2129 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2130 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, ¬if->name, Y_IDENTIF_ARG, LY_STMT_NOTIFICATION)); |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2131 | |
| 2132 | /* parse notification content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2133 | LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)notif, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2134 | LY_STMT_ANYDATA, ¬if->data, 0, |
| 2135 | LY_STMT_ANYXML, ¬if->data, 0, |
| 2136 | LY_STMT_CHOICE, ¬if->data, 0, |
| 2137 | LY_STMT_CONTAINER, ¬if->data, 0, |
| 2138 | LY_STMT_DESCRIPTION, ¬if->dsc, YIN_SUBELEM_UNIQUE, |
| 2139 | LY_STMT_GROUPING, ¬if->groupings, 0, |
| 2140 | LY_STMT_IF_FEATURE, ¬if->iffeatures, 0, |
| 2141 | LY_STMT_LEAF, ¬if->data, 0, |
| 2142 | LY_STMT_LEAF_LIST, ¬if->data, 0, |
| 2143 | LY_STMT_LIST, ¬if->data, 0, |
| 2144 | LY_STMT_MUST, ¬if->musts, YIN_SUBELEM_VER2, |
| 2145 | LY_STMT_REFERENCE, ¬if->ref, YIN_SUBELEM_UNIQUE, |
| 2146 | LY_STMT_STATUS, ¬if->flags, YIN_SUBELEM_UNIQUE, |
| 2147 | LY_STMT_TYPEDEF, ¬if->typedefs, 0, |
| 2148 | LY_STMT_USES, ¬if->data, 0, |
| 2149 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2150 | )); |
| 2151 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2152 | ret = yin_parse_content(ctx, subelems, 16, data, LY_STMT_NOTIFICATION, NULL, ¬if->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2153 | subelems_deallocator(16, subelems); |
| 2154 | LY_CHECK_RET(ret); |
David Sedlák | 031b9e7 | 2019-07-23 15:19:37 +0200 | [diff] [blame] | 2155 | |
| 2156 | /* finalize parent pointers to the reallocated items */ |
| 2157 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL)); |
| 2158 | |
| 2159 | return LY_SUCCESS; |
| 2160 | } |
| 2161 | |
| 2162 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2163 | * @brief Parse grouping element. |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2164 | * |
| 2165 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2166 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2167 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2168 | * @param[in,out] gr_meta Meta information about parent node and groupings to add to. |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2169 | * |
| 2170 | * @return LY_ERR values. |
| 2171 | */ |
| 2172 | static LY_ERR |
| 2173 | yin_parse_grouping(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2174 | struct tree_node_meta *gr_meta) |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2175 | { |
| 2176 | struct lysp_grp *grp; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2177 | struct lysp_grp **grps = (struct lysp_grp **)gr_meta->nodes; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2178 | LY_ERR ret = LY_SUCCESS; |
| 2179 | struct yin_subelement *subelems = NULL; |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2180 | |
| 2181 | /* create new grouping */ |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2182 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *grps, grp, LY_EMEM); |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2183 | grp->nodetype = LYS_GROUPING; |
| 2184 | grp->parent = gr_meta->parent; |
| 2185 | |
| 2186 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2187 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, LY_STMT_GROUPING)); |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2188 | |
| 2189 | /* parse grouping content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2190 | LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)grp, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2191 | LY_STMT_ACTION, &grp->actions, 0, |
| 2192 | LY_STMT_ANYDATA, &grp->data, 0, |
| 2193 | LY_STMT_ANYXML, &grp->data, 0, |
| 2194 | LY_STMT_CHOICE, &grp->data, 0, |
| 2195 | LY_STMT_CONTAINER, &grp->data, 0, |
| 2196 | LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE, |
| 2197 | LY_STMT_GROUPING, &grp->groupings, 0, |
| 2198 | LY_STMT_LEAF, &grp->data, 0, |
| 2199 | LY_STMT_LEAF_LIST, &grp->data, 0, |
| 2200 | LY_STMT_LIST, &grp->data, 0, |
| 2201 | LY_STMT_NOTIFICATION, &grp->notifs, 0, |
| 2202 | LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE, |
| 2203 | LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE, |
| 2204 | LY_STMT_TYPEDEF, &grp->typedefs, 0, |
| 2205 | LY_STMT_USES, &grp->data, 0, |
| 2206 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2207 | )); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2208 | ret = yin_parse_content(ctx, subelems, 16, data, LY_STMT_GROUPING, NULL, &grp->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2209 | subelems_deallocator(16, subelems); |
| 2210 | LY_CHECK_RET(ret); |
| 2211 | |
David Sedlák | e3ce9ef | 2019-07-23 16:34:30 +0200 | [diff] [blame] | 2212 | /* finalize parent pointers to the reallocated items */ |
| 2213 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs)); |
| 2214 | |
| 2215 | return LY_SUCCESS; |
| 2216 | } |
| 2217 | |
| 2218 | /** |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2219 | * @brief Parse container element. |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2220 | * |
| 2221 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2222 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2223 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 2224 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2225 | * |
| 2226 | * @return LY_ERR values. |
| 2227 | */ |
| 2228 | static LY_ERR |
| 2229 | yin_parse_container(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 2230 | struct tree_node_meta *node_meta) |
| 2231 | { |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2232 | struct lysp_node_container *cont; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2233 | LY_ERR ret = LY_SUCCESS; |
| 2234 | struct yin_subelement *subelems = NULL; |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2235 | |
| 2236 | /* create new container */ |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2237 | LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, cont, next); |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2238 | cont->nodetype = LYS_CONTAINER; |
| 2239 | cont->parent = node_meta->parent; |
| 2240 | |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2241 | /* parse aegument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2242 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, LY_STMT_CONTAINER)); |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2243 | |
| 2244 | /* parse container content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2245 | LY_CHECK_RET(subelems_allocator(ctx, 21, (struct lysp_node *)cont, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2246 | LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2, |
| 2247 | LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2, |
| 2248 | LY_STMT_ANYXML, &cont->child, 0, |
| 2249 | LY_STMT_CHOICE, &cont->child, 0, |
| 2250 | LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE, |
| 2251 | LY_STMT_CONTAINER, &cont->child, 0, |
| 2252 | LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE, |
| 2253 | LY_STMT_GROUPING, &cont->groupings, 0, |
| 2254 | LY_STMT_IF_FEATURE, &cont->iffeatures, 0, |
| 2255 | LY_STMT_LEAF, &cont->child, 0, |
| 2256 | LY_STMT_LEAF_LIST, &cont->child, 0, |
| 2257 | LY_STMT_LIST, &cont->child, 0, |
| 2258 | LY_STMT_MUST, &cont->musts, 0, |
| 2259 | LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2, |
| 2260 | LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE, |
| 2261 | LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE, |
| 2262 | LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE, |
| 2263 | LY_STMT_TYPEDEF, &cont->typedefs, 0, |
| 2264 | LY_STMT_USES, &cont->child, 0, |
| 2265 | LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE, |
| 2266 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2267 | )); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2268 | ret = yin_parse_content(ctx, subelems, 21, data, LY_STMT_CONTAINER, NULL, &cont->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2269 | subelems_deallocator(21, subelems); |
| 2270 | LY_CHECK_RET(ret); |
| 2271 | |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2272 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs)); |
| 2273 | |
| 2274 | return LY_SUCCESS; |
| 2275 | } |
| 2276 | |
| 2277 | /** |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2278 | * @brief Parse case element. |
| 2279 | * |
| 2280 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2281 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2282 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 2283 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2284 | * |
| 2285 | * @return LY_ERR values. |
| 2286 | */ |
| 2287 | static LY_ERR |
| 2288 | yin_parse_case(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 2289 | struct tree_node_meta *node_meta) |
| 2290 | { |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2291 | struct lysp_node_case *cas; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2292 | LY_ERR ret = LY_SUCCESS; |
| 2293 | struct yin_subelement *subelems = NULL;; |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2294 | |
| 2295 | /* create new case */ |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2296 | LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, cas, next); |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2297 | cas->nodetype = LYS_CASE; |
| 2298 | cas->parent = node_meta->parent; |
| 2299 | |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2300 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2301 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, LY_STMT_CASE)); |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2302 | |
| 2303 | /* parse case content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2304 | LY_CHECK_RET(subelems_allocator(ctx, 14, (struct lysp_node *)cas, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2305 | LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2, |
| 2306 | LY_STMT_ANYXML, &cas->child, 0, |
| 2307 | LY_STMT_CHOICE, &cas->child, 0, |
| 2308 | LY_STMT_CONTAINER, &cas->child, 0, |
| 2309 | LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE, |
| 2310 | LY_STMT_IF_FEATURE, &cas->iffeatures, 0, |
| 2311 | LY_STMT_LEAF, &cas->child, 0, |
| 2312 | LY_STMT_LEAF_LIST, &cas->child, 0, |
| 2313 | LY_STMT_LIST, &cas->child, 0, |
| 2314 | LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE, |
| 2315 | LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE, |
| 2316 | LY_STMT_USES, &cas->child, 0, |
| 2317 | LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE, |
| 2318 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2319 | )); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2320 | ret = yin_parse_content(ctx, subelems, 14, data, LY_STMT_CASE, NULL, &cas->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2321 | subelems_deallocator(14, subelems); |
| 2322 | |
| 2323 | return ret; |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | /** |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2327 | * @brief Parse choice element. |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2328 | * |
| 2329 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2330 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2331 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | ad83cf9 | 2019-08-13 12:53:53 +0200 | [diff] [blame] | 2332 | * @param[in] node_meta Meta information about parent node and siblings to add to. |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2333 | * |
| 2334 | * @return LY_ERR values. |
| 2335 | */ |
| 2336 | LY_ERR |
| 2337 | yin_parse_choice(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 2338 | struct tree_node_meta *node_meta) |
| 2339 | { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2340 | LY_ERR ret = LY_SUCCESS; |
| 2341 | struct yin_subelement *subelems = NULL; |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2342 | struct lysp_node_choice *choice; |
| 2343 | |
| 2344 | /* create new choice */ |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2345 | LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, choice, next); |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 2346 | |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2347 | choice->nodetype = LYS_CHOICE; |
| 2348 | choice->parent = node_meta->parent; |
| 2349 | |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2350 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2351 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, LY_STMT_CHOICE)); |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2352 | |
| 2353 | /* parse choice content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2354 | LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)choice, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2355 | LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2, |
| 2356 | LY_STMT_ANYXML, &choice->child, 0, |
| 2357 | LY_STMT_CASE, &choice->child, 0, |
| 2358 | LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2, |
| 2359 | LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE, |
| 2360 | LY_STMT_CONTAINER, &choice->child, 0, |
| 2361 | LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE, |
| 2362 | LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE, |
| 2363 | LY_STMT_IF_FEATURE, &choice->iffeatures, 0, |
| 2364 | LY_STMT_LEAF, &choice->child, 0, |
| 2365 | LY_STMT_LEAF_LIST, &choice->child, 0, |
| 2366 | LY_STMT_LIST, &choice->child, 0, |
| 2367 | LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE, |
| 2368 | LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE, |
| 2369 | LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE, |
| 2370 | LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE, |
| 2371 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2372 | )); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2373 | ret = yin_parse_content(ctx, subelems, 17, data, LY_STMT_CHOICE, NULL, &choice->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2374 | subelems_deallocator(17, subelems); |
| 2375 | return ret; |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | /** |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2379 | * @brief Parse input or output element. |
| 2380 | * |
| 2381 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2382 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2383 | * @param[in,out] data Data to read from, always moved to currently handled character. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2384 | * @param[in] inout_kw Identification of input/output element. |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2385 | * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to. |
| 2386 | * |
| 2387 | * @return LY_ERR values. |
| 2388 | */ |
| 2389 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2390 | yin_parse_inout(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt inout_kw, |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2391 | struct inout_meta *inout_meta) |
| 2392 | { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2393 | LY_ERR ret = LY_SUCCESS; |
| 2394 | struct yin_subelement *subelems = NULL; |
| 2395 | |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2396 | /* initiate structure */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2397 | inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT; |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2398 | inout_meta->inout_p->parent = inout_meta->parent; |
| 2399 | |
| 2400 | /* check attributes */ |
| 2401 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw)); |
| 2402 | |
| 2403 | /* parser input/output content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2404 | LY_CHECK_RET(subelems_allocator(ctx, 12, (struct lysp_node *)inout_meta->inout_p, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2405 | LY_STMT_ANYDATA, &inout_meta->inout_p->data, YIN_SUBELEM_VER2, |
| 2406 | LY_STMT_ANYXML, &inout_meta->inout_p->data, 0, |
| 2407 | LY_STMT_CHOICE, &inout_meta->inout_p->data, 0, |
| 2408 | LY_STMT_CONTAINER, &inout_meta->inout_p->data, 0, |
| 2409 | LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0, |
| 2410 | LY_STMT_LEAF, &inout_meta->inout_p->data, 0, |
| 2411 | LY_STMT_LEAF_LIST, &inout_meta->inout_p->data, 0, |
| 2412 | LY_STMT_LIST, &inout_meta->inout_p->data, 0, |
| 2413 | LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2, |
| 2414 | LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0, |
| 2415 | LY_STMT_USES, &inout_meta->inout_p->data, 0, |
| 2416 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2417 | )); |
| 2418 | ret = yin_parse_content(ctx, subelems, 12, data, inout_kw, NULL, &inout_meta->inout_p->exts); |
| 2419 | subelems_deallocator(12, subelems); |
| 2420 | LY_CHECK_RET(ret); |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2421 | |
| 2422 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2423 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_meta->inout_p->groupings, NULL, NULL, NULL)); |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2424 | |
| 2425 | return LY_SUCCESS; |
| 2426 | } |
| 2427 | |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2428 | /** |
| 2429 | * @brief Parse action element. |
| 2430 | * |
| 2431 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2432 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2433 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2434 | * @param[in] act_meta Meta information about parent node and actions to add to. |
| 2435 | * |
| 2436 | * @return LY_ERR values. |
| 2437 | */ |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2438 | static LY_ERR |
| 2439 | yin_parse_action(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2440 | struct tree_node_meta *act_meta) |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2441 | { |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2442 | struct lysp_action *act, **acts = (struct lysp_action **)act_meta->nodes; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2443 | LY_ERR ret = LY_SUCCESS; |
| 2444 | struct yin_subelement *subelems = NULL; |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2445 | |
| 2446 | /* create new action */ |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2447 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *acts, act, LY_EMEM); |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2448 | act->nodetype = LYS_ACTION; |
| 2449 | act->parent = act_meta->parent; |
| 2450 | |
| 2451 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2452 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, LY_STMT_ACTION)); |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2453 | |
| 2454 | /* parse content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2455 | LY_CHECK_RET(subelems_allocator(ctx, 9, (struct lysp_node *)act, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2456 | LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE, |
| 2457 | LY_STMT_GROUPING, &act->groupings, 0, |
| 2458 | LY_STMT_IF_FEATURE, &act->iffeatures, 0, |
| 2459 | LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE, |
| 2460 | LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE, |
| 2461 | LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE, |
| 2462 | LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE, |
| 2463 | LY_STMT_TYPEDEF, &act->typedefs, 0, |
| 2464 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2465 | )); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2466 | ret = (yin_parse_content(ctx, subelems, 9, data, LY_STMT_ACTION, NULL, &act->exts)); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2467 | subelems_deallocator(9, subelems); |
| 2468 | LY_CHECK_RET(ret); |
| 2469 | |
David Sedlák | 85d0eca | 2019-07-24 15:15:21 +0200 | [diff] [blame] | 2470 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL)); |
| 2471 | |
| 2472 | return LY_SUCCESS; |
| 2473 | } |
| 2474 | |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 2475 | /** |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2476 | * @brief Parse augment element. |
| 2477 | * |
| 2478 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2479 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2480 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2481 | * @param[in] aug_meta Meta information about parent node and augments to add to. |
| 2482 | * |
| 2483 | * @return LY_ERR values. |
| 2484 | */ |
| 2485 | static LY_ERR |
| 2486 | yin_parse_augment(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2487 | struct tree_node_meta *aug_meta) |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2488 | { |
| 2489 | struct lysp_augment *aug; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2490 | struct lysp_augment **augs = (struct lysp_augment **)aug_meta->nodes; |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2491 | LY_ERR ret = LY_SUCCESS; |
| 2492 | struct yin_subelement *subelems = NULL; |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2493 | |
| 2494 | /* create new augment */ |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2495 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *augs, aug, LY_EMEM); |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2496 | aug->nodetype = LYS_AUGMENT; |
| 2497 | aug->parent = aug_meta->parent; |
| 2498 | |
| 2499 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2500 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT)); |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2501 | YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment"); |
| 2502 | |
| 2503 | /* parser augment content */ |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2504 | LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)aug, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2505 | LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2, |
| 2506 | LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2, |
| 2507 | LY_STMT_ANYXML, &aug->child, 0, |
| 2508 | LY_STMT_CASE, &aug->child, 0, |
| 2509 | LY_STMT_CHOICE, &aug->child, 0, |
| 2510 | LY_STMT_CONTAINER, &aug->child, 0, |
| 2511 | LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE, |
| 2512 | LY_STMT_IF_FEATURE, &aug->iffeatures, 0, |
| 2513 | LY_STMT_LEAF, &aug->child, 0, |
| 2514 | LY_STMT_LEAF_LIST, &aug->child, 0, |
| 2515 | LY_STMT_LIST, &aug->child, 0, |
| 2516 | LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2, |
| 2517 | LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE, |
| 2518 | LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE, |
| 2519 | LY_STMT_USES, &aug->child, 0, |
| 2520 | LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE, |
| 2521 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2522 | )); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2523 | ret = yin_parse_content(ctx, subelems, 17, data, LY_STMT_AUGMENT, NULL, &aug->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 2524 | subelems_deallocator(17, subelems); |
| 2525 | LY_CHECK_RET(ret); |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2526 | |
| 2527 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs)); |
| 2528 | |
| 2529 | return LY_SUCCESS; |
| 2530 | } |
| 2531 | |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2532 | /** |
| 2533 | * @brief Parse deviate element. |
| 2534 | * |
| 2535 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2536 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2537 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2538 | * @param[in] deviates Deviates to add to. |
| 2539 | * |
| 2540 | * @return LY_ERR values. |
| 2541 | */ |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2542 | static LY_ERR |
| 2543 | yin_parse_deviate(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 2544 | struct lysp_deviate **deviates) |
| 2545 | { |
| 2546 | LY_ERR ret = LY_SUCCESS; |
| 2547 | uint8_t dev_mod; |
| 2548 | const char *temp_val; |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 2549 | struct lysp_deviate *d; |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2550 | struct lysp_deviate_add *d_add = NULL; |
| 2551 | struct lysp_deviate_rpl *d_rpl = NULL; |
| 2552 | struct lysp_deviate_del *d_del = NULL; |
| 2553 | |
| 2554 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2555 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_DEVIATE)); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2556 | |
| 2557 | if (strcmp(temp_val, "not-supported") == 0) { |
| 2558 | dev_mod = LYS_DEV_NOT_SUPPORTED; |
| 2559 | } else if (strcmp(temp_val, "add") == 0) { |
| 2560 | dev_mod = LYS_DEV_ADD; |
| 2561 | } else if (strcmp(temp_val, "replace") == 0) { |
| 2562 | dev_mod = LYS_DEV_REPLACE; |
| 2563 | } else if (strcmp(temp_val, "delete") == 0) { |
| 2564 | dev_mod = LYS_DEV_DELETE; |
| 2565 | } else { |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 2566 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate", |
| 2567 | "not-supported", "add", "replace", "delete"); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2568 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 2569 | return LY_EVALID; |
| 2570 | } |
| 2571 | FREE_STRING(ctx->xml_ctx.ctx, temp_val); |
| 2572 | |
| 2573 | if (dev_mod == LYS_DEV_NOT_SUPPORTED) { |
| 2574 | d = calloc(1, sizeof *d); |
| 2575 | LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
| 2576 | struct yin_subelement subelems[1] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2577 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0} |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2578 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2579 | ret = yin_parse_content(ctx, subelems, 1, data, LY_STMT_DEVIATE, NULL, &d->exts); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2580 | |
| 2581 | } else if (dev_mod == LYS_DEV_ADD) { |
| 2582 | d_add = calloc(1, sizeof *d_add); |
| 2583 | LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
| 2584 | d = (struct lysp_deviate *)d_add; |
| 2585 | struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts}; |
| 2586 | struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts}; |
| 2587 | struct yin_subelement subelems[9] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2588 | {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE}, |
| 2589 | {LY_STMT_DEFAULT, &d_add->dflts, 0}, |
| 2590 | {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE}, |
| 2591 | {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE}, |
| 2592 | {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE}, |
| 2593 | {LY_STMT_MUST, &d_add->musts, 0}, |
| 2594 | {LY_STMT_UNIQUE, &d_add->uniques, 0}, |
| 2595 | {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE}, |
| 2596 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2597 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2598 | ret = yin_parse_content(ctx, subelems, 9, data, LY_STMT_DEVIATE, NULL, &d_add->exts); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2599 | |
| 2600 | } else if (dev_mod == LYS_DEV_REPLACE) { |
| 2601 | d_rpl = calloc(1, sizeof *d_rpl); |
| 2602 | LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
| 2603 | d = (struct lysp_deviate *)d_rpl; |
| 2604 | struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts}; |
| 2605 | struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts}; |
| 2606 | struct yin_subelement subelems[8] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2607 | {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE}, |
| 2608 | {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE}, |
| 2609 | {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE}, |
| 2610 | {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE}, |
| 2611 | {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE}, |
| 2612 | {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE}, |
| 2613 | {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE}, |
| 2614 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2615 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2616 | ret = yin_parse_content(ctx, subelems, 8, data, LY_STMT_DEVIATE, NULL, &d_rpl->exts); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2617 | |
| 2618 | } else { |
| 2619 | d_del = calloc(1, sizeof *d_del); |
| 2620 | LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
| 2621 | d = (struct lysp_deviate *)d_del; |
| 2622 | struct yin_subelement subelems[5] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2623 | {LY_STMT_DEFAULT, &d_del->dflts, 0}, |
| 2624 | {LY_STMT_MUST, &d_del->musts, 0}, |
| 2625 | {LY_STMT_UNIQUE, &d_del->uniques, 0}, |
| 2626 | {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE}, |
| 2627 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2628 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2629 | ret = yin_parse_content(ctx, subelems, 5, data, LY_STMT_DEVIATE, NULL, &d_del->exts); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2630 | } |
| 2631 | LY_CHECK_GOTO(ret, cleanup); |
| 2632 | |
| 2633 | d->mod = dev_mod; |
| 2634 | /* insert into siblings */ |
David Sedlák | 8d552d6 | 2019-08-06 15:29:05 +0200 | [diff] [blame] | 2635 | LY_LIST_INSERT(deviates, d, next); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2636 | |
| 2637 | return ret; |
| 2638 | |
| 2639 | cleanup: |
| 2640 | free(d); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2641 | return ret; |
| 2642 | } |
| 2643 | |
David Sedlák | 992fb7c | 2019-07-24 16:51:01 +0200 | [diff] [blame] | 2644 | /** |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2645 | * @brief Parse deviation element. |
| 2646 | * |
| 2647 | * @param[in,out] ctx YIN parser context for logging and to store current state. |
| 2648 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element. |
| 2649 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2650 | * @param[in] deviations Deviations to add to. |
| 2651 | * |
| 2652 | * @return LY_ERR values. |
| 2653 | */ |
| 2654 | static LY_ERR |
| 2655 | yin_parse_deviation(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, |
| 2656 | struct lysp_deviation **deviations) |
| 2657 | { |
| 2658 | struct lysp_deviation *dev; |
| 2659 | |
| 2660 | /* create new deviation */ |
| 2661 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *deviations, dev, LY_EMEM); |
| 2662 | |
| 2663 | /* parse argument */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2664 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION)); |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2665 | YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation"); |
| 2666 | struct yin_subelement subelems[4] = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2667 | {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE}, |
| 2668 | {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY}, |
| 2669 | {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE}, |
| 2670 | {LY_STMT_EXTENSION_INSTANCE, NULL, 0}, |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2671 | }; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2672 | return yin_parse_content(ctx, subelems, 4, data, LY_STMT_DEVIATION, NULL, &dev->exts); |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | /** |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2676 | * @brief Map keyword type to substatement info. |
| 2677 | * |
| 2678 | * @param[in] kw Keyword type. |
| 2679 | * |
| 2680 | * @return correct LYEXT_SUBSTMT information. |
| 2681 | */ |
| 2682 | static LYEXT_SUBSTMT |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2683 | kw2lyext_substmt(enum ly_stmt kw) |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2684 | { |
| 2685 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2686 | case LY_STMT_ARGUMENT: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2687 | return LYEXT_SUBSTMT_ARGUMENT; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2688 | case LY_STMT_BASE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2689 | return LYEXT_SUBSTMT_BASE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2690 | case LY_STMT_BELONGS_TO: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2691 | return LYEXT_SUBSTMT_BELONGSTO; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2692 | case LY_STMT_CONTACT: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2693 | return LYEXT_SUBSTMT_CONTACT; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2694 | case LY_STMT_DEFAULT: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2695 | return LYEXT_SUBSTMT_DEFAULT; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2696 | case LY_STMT_DESCRIPTION: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2697 | return LYEXT_SUBSTMT_DESCRIPTION; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2698 | case LY_STMT_ERROR_APP_TAG: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2699 | return LYEXT_SUBSTMT_ERRTAG; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2700 | case LY_STMT_ERROR_MESSAGE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2701 | return LYEXT_SUBSTMT_ERRMSG; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2702 | case LY_STMT_KEY: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2703 | return LYEXT_SUBSTMT_KEY; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2704 | case LY_STMT_NAMESPACE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2705 | return LYEXT_SUBSTMT_NAMESPACE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2706 | case LY_STMT_ORGANIZATION: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2707 | return LYEXT_SUBSTMT_ORGANIZATION; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2708 | case LY_STMT_PATH: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2709 | return LYEXT_SUBSTMT_PATH; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2710 | case LY_STMT_PREFIX: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2711 | return LYEXT_SUBSTMT_PREFIX; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2712 | case LY_STMT_PRESENCE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2713 | return LYEXT_SUBSTMT_PRESENCE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2714 | case LY_STMT_REFERENCE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2715 | return LYEXT_SUBSTMT_REFERENCE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2716 | case LY_STMT_REVISION_DATE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2717 | return LYEXT_SUBSTMT_REVISIONDATE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2718 | case LY_STMT_UNITS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2719 | return LYEXT_SUBSTMT_UNITS; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2720 | case LY_STMT_VALUE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2721 | return LYEXT_SUBSTMT_VALUE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2722 | case LY_STMT_YANG_VERSION: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2723 | return LYEXT_SUBSTMT_VERSION; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2724 | case LY_STMT_MODIFIER: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2725 | return LYEXT_SUBSTMT_MODIFIER; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2726 | case LY_STMT_REQUIRE_INSTANCE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2727 | return LYEXT_SUBSTMT_REQINSTANCE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2728 | case LY_STMT_YIN_ELEMENT: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2729 | return LYEXT_SUBSTMT_YINELEM; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2730 | case LY_STMT_CONFIG: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2731 | return LYEXT_SUBSTMT_CONFIG; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2732 | case LY_STMT_MANDATORY: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2733 | return LYEXT_SUBSTMT_MANDATORY; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2734 | case LY_STMT_ORDERED_BY: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2735 | return LYEXT_SUBSTMT_ORDEREDBY; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2736 | case LY_STMT_STATUS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2737 | return LYEXT_SUBSTMT_STATUS; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2738 | case LY_STMT_FRACTION_DIGITS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2739 | return LYEXT_SUBSTMT_FRACDIGITS; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2740 | case LY_STMT_MAX_ELEMENTS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2741 | return LYEXT_SUBSTMT_MAX; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2742 | case LY_STMT_MIN_ELEMENTS: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2743 | return LYEXT_SUBSTMT_MIN; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2744 | case LY_STMT_POSITION: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2745 | return LYEXT_SUBSTMT_POSITION; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2746 | case LY_STMT_UNIQUE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2747 | return LYEXT_SUBSTMT_UNIQUE; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2748 | case LY_STMT_IF_FEATURE: |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 2749 | return LYEXT_SUBSTMT_IFFEATURE; |
| 2750 | default: |
| 2751 | return LYEXT_SUBSTMT_SELF; |
| 2752 | } |
| 2753 | } |
| 2754 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2755 | /** |
| 2756 | * @brief map keyword to keyword-group. |
| 2757 | * |
| 2758 | * @param[in] ctx YIN parser context used for logging. |
| 2759 | * @param[in] kw Keyword that is child of module or submodule. |
| 2760 | * @param[out] group Group of keyword. |
| 2761 | * |
| 2762 | * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly. |
| 2763 | */ |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2764 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2765 | kw2kw_group(struct yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group) |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2766 | { |
| 2767 | switch (kw) { |
| 2768 | /* module header */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2769 | case LY_STMT_NONE: |
| 2770 | case LY_STMT_NAMESPACE: |
| 2771 | case LY_STMT_PREFIX: |
| 2772 | case LY_STMT_BELONGS_TO: |
| 2773 | case LY_STMT_YANG_VERSION: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2774 | *group = Y_MOD_MODULE_HEADER; |
| 2775 | break; |
| 2776 | /* linkage */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2777 | case LY_STMT_INCLUDE: |
| 2778 | case LY_STMT_IMPORT: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2779 | *group = Y_MOD_LINKAGE; |
| 2780 | break; |
| 2781 | /* meta */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2782 | case LY_STMT_ORGANIZATION: |
| 2783 | case LY_STMT_CONTACT: |
| 2784 | case LY_STMT_DESCRIPTION: |
| 2785 | case LY_STMT_REFERENCE: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2786 | *group = Y_MOD_META; |
| 2787 | break; |
| 2788 | /* revision */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2789 | case LY_STMT_REVISION: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2790 | *group = Y_MOD_REVISION; |
| 2791 | break; |
| 2792 | /* body */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2793 | case LY_STMT_ANYDATA: |
| 2794 | case LY_STMT_ANYXML: |
| 2795 | case LY_STMT_AUGMENT: |
| 2796 | case LY_STMT_CHOICE: |
| 2797 | case LY_STMT_CONTAINER: |
| 2798 | case LY_STMT_DEVIATION: |
| 2799 | case LY_STMT_EXTENSION: |
| 2800 | case LY_STMT_FEATURE: |
| 2801 | case LY_STMT_GROUPING: |
| 2802 | case LY_STMT_IDENTITY: |
| 2803 | case LY_STMT_LEAF: |
| 2804 | case LY_STMT_LEAF_LIST: |
| 2805 | case LY_STMT_LIST: |
| 2806 | case LY_STMT_NOTIFICATION: |
| 2807 | case LY_STMT_RPC: |
| 2808 | case LY_STMT_TYPEDEF: |
| 2809 | case LY_STMT_USES: |
| 2810 | case LY_STMT_EXTENSION_INSTANCE: |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2811 | *group = Y_MOD_BODY; |
| 2812 | break; |
| 2813 | default: |
| 2814 | LOGINT(ctx->xml_ctx.ctx); |
| 2815 | return LY_EINT; |
| 2816 | } |
| 2817 | |
| 2818 | return LY_SUCCESS; |
| 2819 | } |
| 2820 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2821 | /** |
| 2822 | * @brief Check if relative order of two keywords is valid. |
| 2823 | * |
| 2824 | * @param[in] ctx YIN parser context used for logging. |
| 2825 | * @param[in] kw Current keyword. |
| 2826 | * @param[in] next_kw Next keyword. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2827 | * @param[in] parrent Identification of parrent element, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE, |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2828 | * because relative order is required only in module and submodule sub-elements, used for logging. |
| 2829 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 2830 | * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid. |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2831 | */ |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2832 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2833 | yin_check_relative_order(struct yin_parser_ctx *ctx, enum ly_stmt kw, enum ly_stmt next_kw, enum ly_stmt parrent) |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2834 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2835 | assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE); |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2836 | enum yang_module_stmt gr, next_gr; |
| 2837 | |
| 2838 | LY_CHECK_RET(kw2kw_group(ctx, kw, &gr)); |
| 2839 | LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr)); |
| 2840 | |
| 2841 | if (gr > next_gr) { |
| 2842 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw)); |
| 2843 | return LY_EVALID; |
| 2844 | } |
| 2845 | |
| 2846 | return LY_SUCCESS; |
| 2847 | } |
| 2848 | |
David Sedlák | 068dd35 | 2019-09-13 09:08:22 +0200 | [diff] [blame^] | 2849 | char * |
| 2850 | name2nsname(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len) |
| 2851 | { |
| 2852 | const struct lyxml_ns *ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len); |
| 2853 | size_t len = strlen(ns->uri) + name_len + 1; |
| 2854 | |
| 2855 | char *temp = malloc(sizeof(*temp) * len); |
| 2856 | strcpy(temp, ns->uri); |
| 2857 | strncat(temp, name, name_len); |
| 2858 | |
| 2859 | return lydict_insert_zc(ctx->xml_ctx.ctx, temp); |
| 2860 | } |
| 2861 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2862 | LY_ERR |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 2863 | yin_parse_content(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2864 | const char **data, enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts) |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2865 | { |
| 2866 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 2867 | char *out = NULL; |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2868 | const char *prefix, *name; |
| 2869 | size_t out_len = 0, prefix_len, name_len; |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 2870 | int dynamic = 0; |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2871 | struct yin_arg_record *attrs = NULL; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2872 | enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE; |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2873 | struct yin_subelement *subelem = NULL; |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 2874 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 2875 | if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) { |
| 2876 | ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2877 | /* current element has subelements as content */ |
| 2878 | if (ret == LY_EINVAL) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 2879 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2880 | ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2881 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2882 | if (!name) { |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2883 | /* end of current element reached */ |
| 2884 | break; |
| 2885 | } |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2886 | ret = yin_load_attributes(ctx, data, &attrs); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2887 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2888 | last_kw = kw; |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2889 | kw = yin_match_keyword(ctx, name, name_len, prefix, prefix_len, current_element); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2890 | |
| 2891 | /* check if this element can be child of current element */ |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2892 | subelem = get_record(kw, subelem_info_size, subelem_info); |
| 2893 | if (!subelem) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2894 | if (current_element == LY_STMT_DEVIATE && isdevsub(kw)) { |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2895 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw)); |
| 2896 | } else { |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 2897 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, ly_stmt2str(current_element)); |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2898 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2899 | ret = LY_EVALID; |
| 2900 | goto cleanup; |
| 2901 | } |
| 2902 | |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 2903 | /* relative order is required only in module and submodule sub-elements */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2904 | if (current_element == LY_STMT_MODULE || current_element == LY_STMT_SUBMODULE) { |
David Sedlák | e6cd89e | 2019-08-07 12:46:02 +0200 | [diff] [blame] | 2905 | ret = yin_check_relative_order(ctx, last_kw, kw, current_element); |
| 2906 | LY_CHECK_GOTO(ret, cleanup); |
| 2907 | } |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 2908 | |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2909 | /* flag check */ |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2910 | if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) { |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2911 | /* subelement uniquenes */ |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 2912 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element)); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 2913 | return LY_EVALID; |
| 2914 | } |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2915 | if (subelem->flags & YIN_SUBELEM_FIRST) { |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2916 | /* subelement is supposed to be defined as first subelement */ |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2917 | ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem); |
David Sedlák | 66d7c84 | 2019-07-11 15:06:04 +0200 | [diff] [blame] | 2918 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 2919 | } |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 2920 | if (subelem->flags & YIN_SUBELEM_VER2) { |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2921 | /* subelement is supported only in version 1.1 or higher */ |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 2922 | if (ctx->mod_version < 2) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 2923 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element)); |
David Sedlák | 9bb1c04 | 2019-07-22 16:45:37 +0200 | [diff] [blame] | 2924 | ret = LY_EVALID; |
| 2925 | goto cleanup; |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 2926 | } |
| 2927 | } |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2928 | /* note that element was parsed for easy uniqueness check in next iterations */ |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2929 | subelem->flags |= YIN_SUBELEM_PARSED; |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 2930 | |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2931 | switch (kw) { |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2932 | /* call responsible function */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2933 | case LY_STMT_EXTENSION_INSTANCE: |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 2934 | ret = yin_parse_extension_instance(ctx, attrs, data, name2fname(name, prefix_len), |
| 2935 | len2flen(name_len, prefix_len), |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2936 | kw2lyext_substmt(current_element), |
| 2937 | (subelem->dest) ? *((uint32_t*)subelem->dest) : 0, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2938 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2939 | case LY_STMT_ACTION: |
| 2940 | case LY_STMT_RPC: |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2941 | ret = yin_parse_action(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2942 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2943 | case LY_STMT_ANYDATA: |
| 2944 | case LY_STMT_ANYXML: |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 2945 | ret = yin_parse_any(ctx, attrs, data, kw, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2946 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2947 | case LY_STMT_ARGUMENT: |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 2948 | ret = yin_parse_argument(ctx, attrs, data, (struct yin_argument_meta *)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2949 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2950 | case LY_STMT_AUGMENT: |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 2951 | ret = yin_parse_augment(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2952 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2953 | case LY_STMT_BASE: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 2954 | ret = yin_parse_base(ctx, attrs, data, current_element, subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2955 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2956 | case LY_STMT_BELONGS_TO: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2957 | ret = yin_parse_belongs_to(ctx, attrs, data, (struct lysp_submodule *)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2958 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2959 | case LY_STMT_BIT: |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 2960 | ret = yin_parse_bit(ctx, attrs, data, (struct lysp_type *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2961 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2962 | case LY_STMT_CASE: |
David Sedlák | 5379d39 | 2019-07-24 10:42:03 +0200 | [diff] [blame] | 2963 | ret = yin_parse_case(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2964 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2965 | case LY_STMT_CHOICE: |
David Sedlák | b7abcfa | 2019-07-24 12:33:35 +0200 | [diff] [blame] | 2966 | ret = yin_parse_choice(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2967 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2968 | case LY_STMT_CONFIG: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2969 | ret = yin_parse_config(ctx, attrs, data, (uint16_t *)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2970 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2971 | case LY_STMT_CONTACT: |
| 2972 | case LY_STMT_DESCRIPTION: |
| 2973 | case LY_STMT_ORGANIZATION: |
| 2974 | case LY_STMT_REFERENCE: |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 2975 | ret = yin_parse_meta(ctx, attrs, data, kw, (const char **)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2976 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2977 | case LY_STMT_CONTAINER: |
David Sedlák | f111bcb | 2019-07-23 17:15:51 +0200 | [diff] [blame] | 2978 | ret = yin_parse_container(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2979 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2980 | case LY_STMT_DEFAULT: |
| 2981 | case LY_STMT_ERROR_APP_TAG: |
| 2982 | case LY_STMT_KEY: |
| 2983 | case LY_STMT_PRESENCE: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 2984 | ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2985 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2986 | case LY_STMT_DEVIATE: |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 2987 | ret = yin_parse_deviate(ctx, attrs, data, (struct lysp_deviate **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2988 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2989 | case LY_STMT_DEVIATION: |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 2990 | ret = yin_parse_deviation(ctx, attrs, data, (struct lysp_deviation **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2991 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2992 | case LY_STMT_ENUM: |
David Sedlák | 43801c9 | 2019-08-05 15:58:54 +0200 | [diff] [blame] | 2993 | ret = yin_parse_enum(ctx, attrs, data, (struct lysp_type *)subelem->dest); |
| 2994 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2995 | case LY_STMT_ERROR_MESSAGE: |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 2996 | ret = yin_parse_err_msg(ctx, attrs, data, (const char **)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 2997 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2998 | case LY_STMT_EXTENSION: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2999 | ret = yin_parse_extension(ctx, attrs, data, (struct lysp_ext **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3000 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3001 | case LY_STMT_FEATURE: |
David Sedlák | 5e13dea | 2019-07-22 16:06:45 +0200 | [diff] [blame] | 3002 | ret = yin_parse_feature(ctx, attrs, data, (struct lysp_feature **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3003 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3004 | case LY_STMT_FRACTION_DIGITS: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3005 | ret = yin_parse_fracdigits(ctx, attrs, data, (struct lysp_type *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3006 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3007 | case LY_STMT_GROUPING: |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 3008 | ret = yin_parse_grouping(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3009 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3010 | case LY_STMT_IDENTITY: |
David Sedlák | 28794f2 | 2019-07-22 16:45:00 +0200 | [diff] [blame] | 3011 | ret = yin_parse_identity(ctx, attrs, data, (struct lysp_ident **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3012 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3013 | case LY_STMT_IF_FEATURE: |
| 3014 | case LY_STMT_UNITS: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 3015 | ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3016 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3017 | case LY_STMT_IMPORT: |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 3018 | ret = yin_parse_import(ctx, attrs, data, (struct import_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3019 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3020 | case LY_STMT_INCLUDE: |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 3021 | ret = yin_parse_include(ctx, attrs, data, (struct include_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3022 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3023 | case LY_STMT_INPUT: |
| 3024 | case LY_STMT_OUTPUT: |
David Sedlák | 05404f6 | 2019-07-24 14:11:53 +0200 | [diff] [blame] | 3025 | ret = yin_parse_inout(ctx, attrs, data, kw, (struct inout_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3026 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3027 | case LY_STMT_LEAF: |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 3028 | ret = yin_parse_leaf(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3029 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3030 | case LY_STMT_LEAF_LIST: |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 3031 | ret = yin_parse_leaflist(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3032 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3033 | case LY_STMT_LENGTH: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 3034 | ret = yin_parse_length(ctx, attrs, data, (struct lysp_type *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3035 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3036 | case LY_STMT_LIST: |
David Sedlák | af536aa | 2019-07-23 13:42:23 +0200 | [diff] [blame] | 3037 | ret = yin_parse_list(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3038 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3039 | case LY_STMT_MANDATORY: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3040 | ret = yin_parse_mandatory(ctx, attrs, data, (uint16_t *)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3041 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3042 | case LY_STMT_MAX_ELEMENTS: |
| 3043 | case LY_STMT_MIN_ELEMENTS: |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 3044 | ret = yin_parse_minmax(ctx, attrs, data, current_element, kw, subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3045 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3046 | case LY_STMT_MODIFIER: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3047 | ret = yin_parse_modifier(ctx, attrs, data, (const char **)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3048 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3049 | case LY_STMT_MUST: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3050 | ret = yin_parse_must(ctx, attrs, data, (struct lysp_restr **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3051 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3052 | case LY_STMT_NAMESPACE: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 3053 | ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3054 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3055 | case LY_STMT_NOTIFICATION: |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 3056 | ret = yin_parse_notification(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3057 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3058 | case LY_STMT_ORDERED_BY: |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 3059 | ret = yin_parse_orderedby(ctx, attrs, data, (uint16_t *)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3060 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3061 | case LY_STMT_PATH: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 3062 | ret = yin_parse_path(ctx, attrs, data, kw, (struct lysp_type *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3063 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3064 | case LY_STMT_PATTERN: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3065 | ret = yin_parse_pattern(ctx, attrs, data, (struct lysp_type *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3066 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3067 | case LY_STMT_VALUE: |
| 3068 | case LY_STMT_POSITION: |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 3069 | ret = yin_parse_value_pos(ctx, attrs, data, kw, (struct lysp_type_enum *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3070 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3071 | case LY_STMT_PREFIX: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 3072 | ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3073 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3074 | case LY_STMT_RANGE: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 3075 | ret = yin_parse_range(ctx, attrs, data, (struct lysp_type *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3076 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3077 | case LY_STMT_REFINE: |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 3078 | ret = yin_parse_refine(ctx, attrs, data, (struct lysp_refine **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3079 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3080 | case LY_STMT_REQUIRE_INSTANCE: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3081 | ret = yin_pasrse_reqinstance(ctx, attrs, data, (struct lysp_type *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3082 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3083 | case LY_STMT_REVISION: |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 3084 | ret = yin_parse_revision(ctx, attrs, data, (struct lysp_revision **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3085 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3086 | case LY_STMT_REVISION_DATE: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3087 | ret = yin_parse_revision_date(ctx, attrs, data, (char *)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3088 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3089 | case LY_STMT_STATUS: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3090 | ret = yin_parse_status(ctx, attrs, data, (uint16_t *)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3091 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3092 | case LY_STMT_TYPE: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 3093 | ret = yin_parse_type(ctx, attrs, data, current_element, subelem); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3094 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3095 | case LY_STMT_TYPEDEF: |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 3096 | ret = yin_parse_typedef(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3097 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3098 | case LY_STMT_UNIQUE: |
David Sedlák | 6542aed | 2019-08-14 10:47:43 +0200 | [diff] [blame] | 3099 | ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3100 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3101 | case LY_STMT_USES: |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 3102 | ret = yin_parse_uses(ctx, attrs, data, (struct tree_node_meta *)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3103 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3104 | case LY_STMT_WHEN: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3105 | ret = yin_parse_when(ctx, attrs, data, (struct lysp_when **)subelem->dest); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3106 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3107 | case LY_STMT_YANG_VERSION: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3108 | ret = yin_parse_yangversion(ctx, attrs, data, (uint8_t *)subelem->dest, exts); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3109 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3110 | case LY_STMT_YIN_ELEMENT: |
David Sedlák | df2691f | 2019-08-15 13:32:27 +0200 | [diff] [blame] | 3111 | ret = yin_parse_yin_element(ctx, attrs, data, (uint16_t *)subelem->dest, exts); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 3112 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3113 | case LY_STMT_ARG_TEXT: |
| 3114 | case LY_STMT_ARG_VALUE: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3115 | ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem->dest, NULL); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3116 | break; |
| 3117 | default: |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 3118 | LOGINT(ctx->xml_ctx.ctx); |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3119 | ret = LY_EINT; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3120 | } |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 3121 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3122 | FREE_ARRAY(ctx, attrs, free_arg_rec); |
| 3123 | attrs = NULL; |
| 3124 | subelem = NULL; |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3125 | } |
| 3126 | } else { |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3127 | LY_CHECK_RET(ret); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3128 | /* elements with text or none content */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 3129 | /* save text content, if text_content isn't set, it's just ignored */ |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 3130 | /* no resources are allocated in this branch, no need to use cleanup label */ |
David Sedlák | 3b4df84 | 2019-07-17 11:39:46 +0200 | [diff] [blame] | 3131 | LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG, out, out_len)); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 3132 | if (text_content) { |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3133 | INSERT_STRING(ctx->xml_ctx.ctx, *text_content, dynamic, out, out_len); |
| 3134 | LY_CHECK_RET(!*text_content, LY_EMEM); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 3135 | } |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3136 | /* load closing element */ |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 3137 | LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3138 | } |
| 3139 | } |
David Sedlák | 8b75446 | 2019-07-25 16:22:13 +0200 | [diff] [blame] | 3140 | /* mandatory subelemnts are checked only after whole element was succesfully parsed */ |
| 3141 | LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element)); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3142 | |
| 3143 | cleanup: |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 3144 | FREE_ARRAY(ctx, attrs, free_arg_rec); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 3145 | return ret; |
| 3146 | } |
| 3147 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 3148 | LY_ERR |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 3149 | yin_parse_extension_instance(struct yin_parser_ctx *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] | 3150 | 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] | 3151 | { |
| 3152 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3153 | char *out; |
| 3154 | const char *name, *prefix; |
| 3155 | size_t out_len, prefix_len, name_len; |
| 3156 | int dynamic; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3157 | struct lysp_ext_instance *e; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3158 | struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL; |
| 3159 | struct yin_arg_record *iter; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3160 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 3161 | LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3162 | |
| 3163 | e->yin = 0; |
| 3164 | /* store name and insubstmt info */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 3165 | e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 3166 | e->insubstmt = subelem; |
| 3167 | e->insubstmt_index = subelem_index; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3168 | e->yin |= LYS_YIN; |
| 3169 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3170 | /* store attributes as subelements */ |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 3171 | LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3172 | if (!iter->prefix) { |
| 3173 | new_subelem = calloc(1, sizeof(*new_subelem)); |
| 3174 | if (!e->child) { |
| 3175 | e->child = new_subelem; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3176 | } else { |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3177 | last_subelem->next = new_subelem; |
| 3178 | } |
| 3179 | last_subelem = new_subelem; |
| 3180 | |
| 3181 | last_subelem->flags |= LYS_YIN_ATTR; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 3182 | last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len); |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3183 | LY_CHECK_RET(!last_subelem->stmt, LY_EMEM); |
| 3184 | |
| 3185 | INSERT_STRING(ctx->xml_ctx.ctx, last_subelem->arg, iter->dynamic_content, iter->content, iter->content_len); |
| 3186 | LY_CHECK_RET(!last_subelem->arg, LY_EMEM); |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3187 | } |
| 3188 | } |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3189 | |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 3190 | /* parse subelements */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 3191 | if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) { |
| 3192 | ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3193 | if (ret == LY_EINVAL) { |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 3194 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
| 3195 | LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3196 | if (!name) { |
| 3197 | /* end of extension instance reached */ |
| 3198 | break; |
| 3199 | } |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3200 | LY_CHECK_RET(yin_parse_element_generic(ctx, name, name_len, prefix, prefix_len, LY_STMT_EXTENSION_INSTANCE, data, &new_subelem)); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3201 | if (!e->child) { |
| 3202 | e->child = new_subelem; |
| 3203 | } else { |
| 3204 | last_subelem->next = new_subelem; |
| 3205 | } |
| 3206 | last_subelem = new_subelem; |
| 3207 | } |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 3208 | } else { |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3209 | if (out_len != 0) { |
| 3210 | /* save text content */ |
| 3211 | LY_CHECK_RET(ret); |
| 3212 | |
| 3213 | INSERT_STRING(ctx->xml_ctx.ctx, e->argument, dynamic, out, out_len); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 3214 | LY_CHECK_RET(!e->argument, LY_EMEM); |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3215 | |
| 3216 | /* load closing element */ |
| 3217 | LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 3218 | LY_CHECK_RET(name, LY_EINT); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 3219 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3220 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | return LY_SUCCESS; |
| 3224 | } |
| 3225 | |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3226 | /** |
| 3227 | * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension. |
| 3228 | * |
| 3229 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
| 3230 | * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension instance. |
| 3231 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3232 | * @param[in] elem_type Type of element that is currently being parsed. |
| 3233 | * @param[out] arg Value to write to. |
| 3234 | * |
| 3235 | * @return LY_ERR values. |
| 3236 | */ |
| 3237 | static LY_ERR |
| 3238 | yin_parse_arg_inext_known(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt elem_type, |
| 3239 | const char **arg) |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3240 | { |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3241 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3242 | char *out = NULL; |
| 3243 | const char *name, *prefix; |
| 3244 | size_t out_len, name_len, prefix_len; |
| 3245 | int dynamic; |
| 3246 | enum ly_stmt child; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3247 | |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3248 | switch (elem_type) { |
| 3249 | case LY_STMT_ACTION: |
| 3250 | case LY_STMT_ANYDATA: |
| 3251 | case LY_STMT_ANYXML: |
| 3252 | case LY_STMT_ARGUMENT: |
| 3253 | case LY_STMT_BASE: |
| 3254 | case LY_STMT_BIT: |
| 3255 | case LY_STMT_CASE: |
| 3256 | case LY_STMT_CHOICE: |
| 3257 | case LY_STMT_CONTAINER: |
| 3258 | case LY_STMT_ENUM: |
| 3259 | case LY_STMT_EXTENSION: |
| 3260 | case LY_STMT_FEATURE: |
| 3261 | case LY_STMT_GROUPING: |
| 3262 | case LY_STMT_IDENTITY: |
| 3263 | case LY_STMT_IF_FEATURE: |
| 3264 | case LY_STMT_LEAF: |
| 3265 | case LY_STMT_LEAF_LIST: |
| 3266 | case LY_STMT_LIST: |
| 3267 | case LY_STMT_MODULE: |
| 3268 | case LY_STMT_NOTIFICATION: |
| 3269 | case LY_STMT_RPC: |
| 3270 | case LY_STMT_SUBMODULE: |
| 3271 | case LY_STMT_TYPE: |
| 3272 | case LY_STMT_TYPEDEF: |
| 3273 | case LY_STMT_UNITS: |
| 3274 | case LY_STMT_USES: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3275 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3276 | break; |
| 3277 | case LY_STMT_AUGMENT: |
| 3278 | case LY_STMT_DEVIATION: |
| 3279 | case LY_STMT_REFINE: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3280 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3281 | break; |
| 3282 | case LY_STMT_CONFIG: |
| 3283 | case LY_STMT_DEFAULT: |
| 3284 | case LY_STMT_DEVIATE: |
| 3285 | case LY_STMT_ERROR_APP_TAG: |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3286 | case LY_STMT_FRACTION_DIGITS: |
| 3287 | case LY_STMT_KEY: |
| 3288 | case LY_STMT_LENGTH: |
| 3289 | case LY_STMT_MANDATORY: |
| 3290 | case LY_STMT_MAX_ELEMENTS: |
| 3291 | case LY_STMT_MIN_ELEMENTS: |
| 3292 | case LY_STMT_MODIFIER: |
| 3293 | case LY_STMT_ORDERED_BY: |
| 3294 | case LY_STMT_PATH: |
| 3295 | case LY_STMT_PATTERN: |
| 3296 | case LY_STMT_POSITION: |
| 3297 | case LY_STMT_PREFIX: |
| 3298 | case LY_STMT_PRESENCE: |
| 3299 | case LY_STMT_RANGE: |
| 3300 | case LY_STMT_REQUIRE_INSTANCE: |
| 3301 | case LY_STMT_STATUS: |
| 3302 | case LY_STMT_VALUE: |
| 3303 | case LY_STMT_YANG_VERSION: |
| 3304 | case LY_STMT_YIN_ELEMENT: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3305 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3306 | break; |
| 3307 | case LY_STMT_IMPORT: |
| 3308 | case LY_STMT_INCLUDE: |
| 3309 | case LY_STMT_BELONGS_TO: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3310 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3311 | break; |
| 3312 | case LY_STMT_INPUT: |
| 3313 | case LY_STMT_OUTPUT: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3314 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3315 | break; |
| 3316 | case LY_STMT_MUST: |
| 3317 | case LY_STMT_WHEN: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3318 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3319 | break; |
| 3320 | case LY_STMT_NAMESPACE: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3321 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3322 | break; |
| 3323 | case LY_STMT_REVISION: |
| 3324 | case LY_STMT_REVISION_DATE: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3325 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3326 | break; |
| 3327 | case LY_STMT_UNIQUE: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3328 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3329 | break; |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3330 | /* argument is mapped to yin element */ |
| 3331 | case LY_STMT_CONTACT: |
| 3332 | case LY_STMT_DESCRIPTION: |
| 3333 | case LY_STMT_ORGANIZATION: |
| 3334 | case LY_STMT_REFERENCE: |
| 3335 | case LY_STMT_ERROR_MESSAGE: |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3336 | /* there shouldn't be any attribute, argument is supposed to be first subelement */ |
| 3337 | LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type)); |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3338 | ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3339 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM, |
| 3340 | elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type)), |
| 3341 | LY_EVALID); |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3342 | LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 3343 | child = yin_match_keyword(ctx, name, name_len, prefix, prefix_len, elem_type); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3344 | if ((elem_type == LY_STMT_ERROR_MESSAGE && child == LY_STMT_ARG_VALUE) || |
| 3345 | (elem_type != LY_STMT_ERROR_MESSAGE && child == LY_STMT_ARG_TEXT)) { |
| 3346 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, ly_stmt2str(elem_type)); |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3347 | return LY_EVALID; |
| 3348 | } |
| 3349 | /* load and save content */ |
| 3350 | LY_CHECK_RET(lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic)); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3351 | INSERT_STRING(ctx->xml_ctx.ctx, *arg, dynamic, out, out_len); |
| 3352 | LY_CHECK_RET(!*arg, LY_EMEM); |
| 3353 | /* load closing tag of subelement */ |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3354 | LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3355 | /* if only subelement was parsed as argument, load also closing tag */ |
| 3356 | if (ctx->xml_ctx.status == LYXML_ELEMENT) { |
| 3357 | LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 3358 | } |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3359 | break; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3360 | default: |
| 3361 | LOGINT(ctx->xml_ctx.ctx); |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3362 | return LY_EINT; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3363 | } |
| 3364 | |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3365 | return LY_SUCCESS; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | LY_ERR |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3369 | yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len, enum ly_stmt parent, |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 3370 | const char **data, struct lysp_stmt **element) |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3371 | { |
| 3372 | LY_ERR ret = LY_SUCCESS; |
| 3373 | const char *temp_prefix, *temp_name; |
| 3374 | char *out = NULL; |
Radek Krejci | 6df08da | 2019-09-09 14:07:21 +0200 | [diff] [blame] | 3375 | size_t out_len, temp_name_len, temp_prefix_len; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3376 | int dynamic; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3377 | struct lysp_stmt *last = NULL, *new = NULL; |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3378 | struct yin_arg_record *attrs = NULL; |
| 3379 | struct yin_arg_record *iter = NULL; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3380 | |
| 3381 | /* allocate new structure for element */ |
| 3382 | *element = calloc(1, sizeof(**element)); |
David Sedlák | c005d89 | 2019-08-16 09:27:01 +0200 | [diff] [blame] | 3383 | LY_CHECK_ERR_RET(!(*element), LOGMEM(ctx->xml_ctx.ctx), LY_EMEM); |
Radek Krejci | 994fc34 | 2019-09-09 14:20:49 +0200 | [diff] [blame] | 3384 | (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, prefix ? prefix : name, prefix_len ? prefix_len + name_len + 1 : name_len); |
David Sedlák | c005d89 | 2019-08-16 09:27:01 +0200 | [diff] [blame] | 3385 | LY_CHECK_RET(!(*element)->stmt, LY_EMEM); |
Radek Krejci | 6df08da | 2019-09-09 14:07:21 +0200 | [diff] [blame] | 3386 | /* TODO map prefix to module name */ |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3387 | |
| 3388 | ret = yin_load_attributes(ctx, data, &attrs); |
| 3389 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3390 | (*element)->kw = yin_match_keyword(ctx, name, name_len, prefix, prefix_len, parent); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3391 | |
| 3392 | last = (*element)->child; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3393 | if ((*element)->kw == LY_STMT_NONE) { |
| 3394 | /* unrecognized element */ |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3395 | LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, ly_stmt2str(parent)); |
| 3396 | ret = LY_EVALID; |
| 3397 | goto cleanup; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3398 | } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) { |
| 3399 | /* element is known yang keyword, which means argument can be parsed correctly. */ |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3400 | ret = yin_parse_arg_inext_known(ctx, attrs, data, (*element)->kw, &(*element)->arg); |
| 3401 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3402 | } else { |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3403 | /* load attributes in generic way, save all attributes in linked list */ |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3404 | LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) { |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3405 | new = calloc(1, sizeof(*last)); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3406 | LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup); |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3407 | if (!(*element)->child) { |
| 3408 | /* save first */ |
| 3409 | (*element)->child = new; |
| 3410 | } else { |
| 3411 | last->next = new; |
| 3412 | } |
| 3413 | last = new; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3414 | |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3415 | last->flags |= LYS_YIN_ATTR; |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3416 | last->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len); |
| 3417 | last->kw = LY_STMT_NONE; |
David Sedlák | e6284fc | 2019-09-10 11:57:02 +0200 | [diff] [blame] | 3418 | /* attributes with prefix are ignored */ |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3419 | if (!iter->prefix) { |
| 3420 | INSERT_STRING(ctx->xml_ctx.ctx, last->arg, iter->dynamic_content, iter->content, iter->content_len); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3421 | LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup); |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3422 | /* string is no longer supposed to be freed when the array is freed */ |
| 3423 | iter->dynamic_content = 0; |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3424 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3425 | } |
| 3426 | } |
| 3427 | |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3428 | /* parse content of element if any */ |
| 3429 | if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) { |
| 3430 | ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 3431 | if (ret == LY_EINVAL) { |
| 3432 | while (ctx->xml_ctx.status == LYXML_ELEMENT) { |
| 3433 | /* parse subelements */ |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3434 | ret = lyxml_get_element(&ctx->xml_ctx, data, &temp_prefix, &temp_prefix_len, &temp_name, &temp_name_len); |
| 3435 | LY_CHECK_GOTO(ret , cleanup); |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3436 | if (!temp_name) { |
| 3437 | /* end of element reached */ |
| 3438 | break; |
| 3439 | } |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3440 | ret = yin_parse_element_generic(ctx, temp_name, temp_name_len, temp_prefix, temp_prefix_len, (*element)->kw, data, &new); |
| 3441 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3442 | if (!(*element)->child) { |
| 3443 | /* save first */ |
| 3444 | (*element)->child = new; |
| 3445 | } else { |
| 3446 | last->next = new; |
| 3447 | } |
| 3448 | last = new; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3449 | } |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3450 | ret = LY_SUCCESS; |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3451 | } else { |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3452 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3453 | /* save element content */ |
| 3454 | if (out_len != 0) { |
| 3455 | INSERT_STRING(ctx->xml_ctx.ctx, (*element)->arg, dynamic, out, out_len); |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3456 | LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup); |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3457 | } |
David Sedlák | 169cc52 | 2019-08-15 13:23:45 +0200 | [diff] [blame] | 3458 | |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3459 | /* read closing tag */ |
David Sedlák | aa98bba | 2019-09-12 11:52:14 +0200 | [diff] [blame] | 3460 | ret = lyxml_get_element(&ctx->xml_ctx, data, &temp_prefix, &temp_prefix_len, &temp_name, &temp_name_len); |
| 3461 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | a82ecff | 2019-08-16 10:51:48 +0200 | [diff] [blame] | 3462 | } |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 3463 | } |
| 3464 | |
David Sedlák | b0ca07d | 2019-09-11 11:54:05 +0200 | [diff] [blame] | 3465 | cleanup: |
| 3466 | FREE_ARRAY(ctx, attrs, free_arg_rec); |
| 3467 | return ret; |
David Sedlák | 1e2cdd0 | 2019-06-27 14:17:43 +0200 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | LY_ERR |
David Sedlák | 4f03b93 | 2019-07-26 13:01:47 +0200 | [diff] [blame] | 3471 | yin_parse_mod(struct yin_parser_ctx *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] | 3472 | { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3473 | LY_ERR ret = LY_SUCCESS; |
| 3474 | struct yin_subelement *subelems = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3475 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3476 | LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, LY_STMT_MODULE)); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3477 | LY_CHECK_RET(subelems_allocator(ctx, 28, NULL, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3478 | LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2, |
| 3479 | LY_STMT_ANYXML, &mod->data, 0, |
| 3480 | LY_STMT_AUGMENT, &mod->augments, 0, |
| 3481 | LY_STMT_CHOICE, &mod->data, 0, |
| 3482 | LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE, |
| 3483 | LY_STMT_CONTAINER, &mod->data, 0, |
| 3484 | LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE, |
| 3485 | LY_STMT_DEVIATION, &mod->deviations, 0, |
| 3486 | LY_STMT_EXTENSION, &mod->extensions, 0, |
| 3487 | LY_STMT_FEATURE, &mod->features, 0, |
| 3488 | LY_STMT_GROUPING, &mod->groupings, 0, |
| 3489 | LY_STMT_IDENTITY, &mod->identities, 0, |
| 3490 | LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0, |
| 3491 | LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0, |
| 3492 | LY_STMT_LEAF, &mod->data, 0, |
| 3493 | LY_STMT_LEAF_LIST, &mod->data, 0, |
| 3494 | LY_STMT_LIST, &mod->data, 0, |
| 3495 | LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE, |
| 3496 | LY_STMT_NOTIFICATION, &mod->notifs, 0, |
| 3497 | LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE, |
| 3498 | LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE, |
| 3499 | LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE, |
| 3500 | LY_STMT_REVISION, &mod->revs, 0, |
| 3501 | LY_STMT_RPC, &mod->rpcs, 0, |
| 3502 | LY_STMT_TYPEDEF, &mod->typedefs, 0, |
| 3503 | LY_STMT_USES, &mod->data, 0, |
Radek Krejci | cd1c5e3 | 2019-09-06 16:28:42 +0200 | [diff] [blame] | 3504 | LY_STMT_YANG_VERSION, &mod->mod->version, YIN_SUBELEM_UNIQUE, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3505 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3506 | )); |
| 3507 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3508 | ret = yin_parse_content(ctx, subelems, 28, data, LY_STMT_MODULE, NULL, &mod->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3509 | subelems_deallocator(28, subelems); |
| 3510 | |
| 3511 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | LY_ERR |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 3515 | yin_parse_submod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs, const char **data, struct lysp_submodule *submod) |
| 3516 | { |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3517 | LY_ERR ret = LY_SUCCESS; |
| 3518 | struct yin_subelement *subelems = NULL; |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 3519 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3520 | LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, LY_STMT_SUBMODULE)); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3521 | LY_CHECK_RET(subelems_allocator(ctx, 27, NULL, &subelems, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3522 | LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2, |
| 3523 | LY_STMT_ANYXML, &submod->data, 0, |
| 3524 | LY_STMT_AUGMENT, &submod->augments, 0, |
| 3525 | LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE, |
| 3526 | LY_STMT_CHOICE, &submod->data, 0, |
| 3527 | LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE, |
| 3528 | LY_STMT_CONTAINER, &submod->data, 0, |
| 3529 | LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE, |
| 3530 | LY_STMT_DEVIATION, &submod->deviations, 0, |
| 3531 | LY_STMT_EXTENSION, &submod->extensions, 0, |
| 3532 | LY_STMT_FEATURE, &submod->features, 0, |
| 3533 | LY_STMT_GROUPING, &submod->groupings, 0, |
| 3534 | LY_STMT_IDENTITY, &submod->identities, 0, |
| 3535 | LY_STMT_IMPORT, submod->prefix, &submod->imports, 0, |
| 3536 | LY_STMT_INCLUDE, submod->name, &submod->includes, 0, |
| 3537 | LY_STMT_LEAF, &submod->data, 0, |
| 3538 | LY_STMT_LEAF_LIST, &submod->data, 0, |
| 3539 | LY_STMT_LIST, &submod->data, 0, |
| 3540 | LY_STMT_NOTIFICATION, &submod->notifs, 0, |
| 3541 | LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE, |
| 3542 | LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE, |
| 3543 | LY_STMT_REVISION, &submod->revs, 0, |
| 3544 | LY_STMT_RPC, &submod->rpcs, 0, |
| 3545 | LY_STMT_TYPEDEF, &submod->typedefs, 0, |
| 3546 | LY_STMT_USES, &submod->data, 0, |
Radek Krejci | cd1c5e3 | 2019-09-06 16:28:42 +0200 | [diff] [blame] | 3547 | LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3548 | LY_STMT_EXTENSION_INSTANCE, NULL, 0 |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3549 | )); |
| 3550 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3551 | ret = yin_parse_content(ctx, subelems, 27, data, LY_STMT_SUBMODULE, NULL, &submod->exts); |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 3552 | subelems_deallocator(27, subelems); |
| 3553 | |
| 3554 | return ret; |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | LY_ERR |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3558 | yin_parse_submodule(struct yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx, const char *data, struct lysp_submodule **submod) |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3559 | { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3560 | enum ly_stmt kw = LY_STMT_NONE; |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3561 | LY_ERR ret = LY_SUCCESS; |
| 3562 | const char *prefix, *name; |
| 3563 | size_t prefix_len, name_len; |
| 3564 | struct yin_arg_record *attrs = NULL; |
| 3565 | struct lysp_submodule *mod_p = NULL; |
| 3566 | |
| 3567 | /* create context */ |
| 3568 | *yin_ctx = calloc(1, sizeof **yin_ctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3569 | LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3570 | (*yin_ctx)->xml_ctx.ctx = ctx; |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 3571 | (*yin_ctx)->pos_type = LY_VLOG_LINE; |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3572 | (*yin_ctx)->xml_ctx.line = 1; |
| 3573 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3574 | /* map the typedefs and groupings list from main context to the submodule's context */ |
| 3575 | memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 3576 | memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes); |
| 3577 | |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3578 | /* check submodule */ |
| 3579 | ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 3580 | LY_CHECK_GOTO(ret, cleanup); |
| 3581 | ret = yin_load_attributes(*yin_ctx, &data, &attrs); |
| 3582 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3583 | kw = yin_match_keyword(*yin_ctx, name, name_len, prefix, prefix_len, LY_STMT_NONE); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3584 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3585 | if (kw == LY_STMT_MODULE) { |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3586 | LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected."); |
| 3587 | ret = LY_EINVAL; |
| 3588 | goto cleanup; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3589 | } else if (kw != LY_STMT_SUBMODULE) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 3590 | LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw)); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3591 | ret = LY_EVALID; |
| 3592 | goto cleanup; |
| 3593 | } |
| 3594 | |
| 3595 | mod_p = calloc(1, sizeof *mod_p); |
| 3596 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 3597 | mod_p->parsing = 1; |
| 3598 | |
| 3599 | ret = yin_parse_submod(*yin_ctx, attrs, &data, mod_p); |
| 3600 | LY_CHECK_GOTO(ret, cleanup); |
| 3601 | |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3602 | name = NULL; |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3603 | /* skip possible trailing whitespaces at end of the input */ |
| 3604 | while(*data && isspace(*data)) { |
| 3605 | data++; |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3606 | } |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3607 | if (*data) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 3608 | LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : ""); |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3609 | ret = LY_EVALID; |
| 3610 | goto cleanup; |
| 3611 | } |
| 3612 | |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3613 | mod_p->parsing = 0; |
| 3614 | *submod = mod_p; |
| 3615 | |
| 3616 | cleanup: |
| 3617 | if (ret) { |
| 3618 | lysp_submodule_free(ctx, mod_p); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3619 | yin_parser_ctx_free(*yin_ctx); |
| 3620 | *yin_ctx = NULL; |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | FREE_ARRAY(*yin_ctx, attrs, free_arg_rec); |
| 3624 | return ret; |
| 3625 | } |
| 3626 | |
| 3627 | LY_ERR |
| 3628 | yin_parse_module(struct yin_parser_ctx **yin_ctx, const char *data, struct lys_module *mod) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3629 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 3630 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3631 | enum ly_stmt kw = LY_STMT_NONE; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3632 | struct lysp_module *mod_p = NULL; |
| 3633 | const char *prefix, *name; |
| 3634 | size_t prefix_len, name_len; |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 3635 | struct yin_arg_record *attrs = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3636 | |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3637 | /* create context */ |
| 3638 | *yin_ctx = calloc(1, sizeof **yin_ctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3639 | LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3640 | (*yin_ctx)->xml_ctx.ctx = mod->ctx; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3641 | (*yin_ctx)->pos_type = LY_VLOG_LINE; |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3642 | (*yin_ctx)->xml_ctx.line = 1; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 3643 | |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3644 | /* check module */ |
| 3645 | ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 3646 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3647 | ret = yin_load_attributes(*yin_ctx, &data, &attrs); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 3648 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3649 | kw = yin_match_keyword(*yin_ctx, name, name_len, prefix, prefix_len, LY_STMT_NONE); |
| 3650 | if (kw == LY_STMT_SUBMODULE) { |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3651 | LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3652 | ret = LY_EINVAL; |
| 3653 | goto cleanup; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3654 | } else if (kw != LY_STMT_MODULE) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 3655 | LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3656 | ret = LY_EVALID; |
| 3657 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 3658 | } |
| 3659 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3660 | /* allocate module */ |
| 3661 | mod_p = calloc(1, sizeof *mod_p); |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3662 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3663 | mod_p->mod = mod; |
| 3664 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 3665 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 3666 | /* parse module substatements */ |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3667 | ret = yin_parse_mod(*yin_ctx, attrs, &data, mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3668 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 3669 | |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3670 | name = NULL; |
| 3671 | /* skip possible trailing whitespaces at end of the input */ |
| 3672 | while(*data && isspace(*data)) { |
| 3673 | data++; |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3674 | } |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 3675 | if (*data) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 3676 | LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : ""); |
David Sedlák | 6d781b6 | 2019-08-02 15:22:52 +0200 | [diff] [blame] | 3677 | ret = LY_EVALID; |
| 3678 | goto cleanup; |
| 3679 | } |
| 3680 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3681 | mod_p->parsing = 0; |
| 3682 | mod->parsed = mod_p; |
| 3683 | |
| 3684 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 3685 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3686 | lysp_module_free(mod_p); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 3687 | yin_parser_ctx_free(*yin_ctx); |
| 3688 | *yin_ctx = NULL; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 3689 | } |
David Sedlák | 8985a14 | 2019-07-31 16:43:06 +0200 | [diff] [blame] | 3690 | FREE_ARRAY(*yin_ctx, attrs, free_arg_rec); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 3691 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 3692 | } |