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