David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_yin.h |
| 3 | * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz> |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 4 | * @brief YIN parser header file. |
David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 14 | |
| 15 | #ifndef LY_PARSER_YIN_H_ |
| 16 | #define LY_PARSER_YIN_H_ |
| 17 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include <stdint.h> |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 20 | |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 21 | #include "log.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include "tree.h" |
| 23 | #include "tree_schema.h" |
| 24 | #include "tree_schema_internal.h" |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 25 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 26 | /* list of yin attribute strings */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 27 | extern const char * const yin_attr_list[]; |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 28 | #define yin_attr2str(STMT) yin_attr_list[STMT] |
| 29 | |
David Sedlák | 26ea143 | 2019-08-14 13:42:23 +0200 | [diff] [blame] | 30 | #define VALID_VALS1 " Only valid value is \"%s\"." |
| 31 | #define VALID_VALS2 " Valid values are \"%s\" and \"%s\"." |
| 32 | #define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"." |
| 33 | #define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"." |
| 34 | |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 35 | /* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 36 | #define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \ |
| 37 | kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \ |
| 38 | kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \ |
| 39 | kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE) |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 40 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 41 | enum yin_argument { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 42 | YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */ |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 43 | YIN_ARG_NAME, /**< argument name */ |
| 44 | YIN_ARG_TARGET_NODE, /**< argument target-node */ |
| 45 | YIN_ARG_MODULE, /**< argument module */ |
| 46 | YIN_ARG_VALUE, /**< argument value */ |
| 47 | YIN_ARG_TEXT, /**< argument text */ |
| 48 | YIN_ARG_CONDITION, /**< argument condition */ |
| 49 | YIN_ARG_URI, /**< argument uri */ |
| 50 | YIN_ARG_DATE, /**< argument data */ |
| 51 | YIN_ARG_TAG, /**< argument tag */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 52 | YIN_ARG_NONE /**< empty (special value) */ |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 55 | /* flags to set constraints of subelements */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 56 | #define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */ |
| 57 | #define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */ |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 58 | #define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */ |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame] | 59 | #define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */ |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 60 | |
| 61 | #define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 62 | time to simply check validity of given constraints */ |
| 63 | |
| 64 | struct yin_subelement { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 65 | enum ly_stmt type; /**< type of keyword */ |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 66 | void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 67 | uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 70 | /* Meta information passed to yin_parse_argument function, |
| 71 | holds information about where content of argument element will be stored. */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 72 | struct yin_argument_meta { |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 73 | uint16_t *flags; /**< Argument flags */ |
| 74 | const char **argument; /**< Argument value */ |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 77 | /** |
| 78 | * @brief Meta information passed to functions working with tree_schema, |
| 79 | * that require additional information about parent node. |
| 80 | */ |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 81 | struct tree_node_meta { |
| 82 | struct lysp_node *parent; /**< parent node */ |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 83 | struct lysp_node **nodes; /**< linked list of siblings */ |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 86 | /** |
| 87 | * @brief Meta information passed to yin_parse_import function. |
| 88 | */ |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 89 | struct import_meta { |
| 90 | const char *prefix; /**< module prefix. */ |
| 91 | struct lysp_import **imports; /**< imports to add to. */ |
| 92 | }; |
| 93 | |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 94 | /** |
| 95 | * @brief Meta information passed to yin_parse_include function. |
| 96 | */ |
| 97 | struct include_meta { |
| 98 | const char *name; /**< Module/submodule name. */ |
| 99 | struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */ |
| 100 | }; |
| 101 | |
| 102 | /** |
David Sedlák | 81497a3 | 2019-08-13 16:56:26 +0200 | [diff] [blame] | 103 | * @brief Meta information passed to yin_parse_inout function. |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 104 | */ |
| 105 | struct inout_meta { |
| 106 | struct lysp_node *parent; /**< Parent node. */ |
| 107 | struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */ |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * @brief Meta information passed to yin_parse_minmax function. |
| 112 | */ |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 113 | struct minmax_dev_meta { |
David Sedlák | 6881b51 | 2019-08-13 12:52:00 +0200 | [diff] [blame] | 114 | uint32_t *lim; /**< min/max value to write to. */ |
| 115 | uint16_t *flags; /**< min/max flags to write to. */ |
| 116 | struct lysp_ext_instance **exts; /**< extension instances to add to. */ |
David Sedlák | 4ffcec8 | 2019-07-25 15:10:21 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 119 | /** |
| 120 | * @brief Match argument name. |
| 121 | * |
| 122 | * @param[in] name String representing name. |
| 123 | * @param[in] len Lenght of the name. |
| 124 | * |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 125 | * @return yin_argument values. |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 126 | */ |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 127 | enum yin_argument yin_match_argument_name(const char *name, size_t len); |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 128 | |
| 129 | /** |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 130 | * @brief Generic function for content parsing |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 131 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 132 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
Radek Krejci | 12b1c15 | 2019-09-05 16:20:48 +0200 | [diff] [blame] | 133 | * @param[in] subelem_info array of valid subelement types and meta information |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 134 | * @param[in] subelem_info_size Size of subelem_info array. |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 135 | * @param[in] current_element Type of current element. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 136 | * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL. |
| 137 | * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements. |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 138 | * |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 139 | * @return LY_ERR values. |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 140 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 141 | LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 142 | enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts); |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 143 | |
David Sedlák | d9d3a31 | 2019-06-04 09:47:10 +0200 | [diff] [blame] | 144 | /** |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 145 | * @brief Check that val is valid UTF8 character sequence of val_type. |
| 146 | * Doesn't check empty string, only character validity. |
| 147 | * |
| 148 | * @param[in] ctx Yin parser context for logging. |
| 149 | * @param[in] val_type Type of the input string to select method of checking character validity. |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 150 | * |
| 151 | * @return LY_ERR values. |
| 152 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 153 | LY_ERR yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type); |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 154 | |
| 155 | /** |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 156 | * @brief Match yang keyword from yin data. |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 157 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 158 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 159 | * @param[in] name Start of keyword name |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 160 | * @param[in] name_len Lenght of keyword name. |
| 161 | * @param[in] prefix Start of keyword prefix. |
David Sedlák | c5b2084 | 2019-08-13 10:18:31 +0200 | [diff] [blame] | 162 | * @param[in] prefix_len Lenght of prefix. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 163 | * @param[in] parrent Identification of parrent element, use LY_STMT_NONE for elements without parrent. |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 164 | * |
| 165 | * @return yang_keyword values. |
| 166 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 167 | enum ly_stmt yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 168 | const char *prefix, size_t prefix_len, enum ly_stmt parrent); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 169 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 170 | /** |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 171 | * @brief Parse instance of extension. |
| 172 | * |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 173 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
David Sedlák | b4e4456 | 2019-07-04 15:42:12 +0200 | [diff] [blame] | 174 | * @param[in] subelem Type of the keyword this extension instance is a subelement of. |
| 175 | * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of |
| 176 | * @param[in,out] exts Extension instance to add to. |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 177 | * |
| 178 | * @return LY_ERR values. |
| 179 | */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 180 | LY_ERR yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 181 | struct lysp_ext_instance **exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * @brief Parse yin element into generic structure. |
| 185 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 186 | * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state. |
David Sedlák | 071f766 | 2019-09-12 02:02:51 +0200 | [diff] [blame] | 187 | * @param[in] parent Identification of parent element. |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 188 | * @param[out] element Where the element structure should be stored. |
| 189 | * |
| 190 | * @return LY_ERR values. |
| 191 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 192 | LY_ERR yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 193 | |
David Sedlák | 4f03b93 | 2019-07-26 13:01:47 +0200 | [diff] [blame] | 194 | /** |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 195 | * @brief Parse module element. |
David Sedlák | 4f03b93 | 2019-07-26 13:01:47 +0200 | [diff] [blame] | 196 | * |
| 197 | * @param[in,out] ctx Yin parser context for logging and to store current state. |
David Sedlák | 4f03b93 | 2019-07-26 13:01:47 +0200 | [diff] [blame] | 198 | * @param[out] mod Parsed module structure. |
| 199 | * |
| 200 | * @return LY_ERR values. |
| 201 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 202 | LY_ERR yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod); |
David Sedlák | 4f03b93 | 2019-07-26 13:01:47 +0200 | [diff] [blame] | 203 | |
| 204 | /** |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 205 | * @brief Parse submodule element. |
| 206 | * |
| 207 | * @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] | 208 | * @param[in] mod_attrs Attributes of submodule element. |
David Sedlák | bf8a2b7 | 2019-08-14 16:48:10 +0200 | [diff] [blame] | 209 | * @param[out] submod Parsed submodule structure. |
David Sedlák | 298ff6d | 2019-07-26 14:29:03 +0200 | [diff] [blame] | 210 | * |
| 211 | * @return LY_ERR values. |
| 212 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 213 | LY_ERR yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod); |
David Sedlák | 4f03b93 | 2019-07-26 13:01:47 +0200 | [diff] [blame] | 214 | |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 215 | #endif /* LY_PARSER_YIN_H_*/ |