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> |
| 4 | * @brief YIN parser. |
| 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 | |
| 18 | #include <stdio.h> |
David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 21 | #include "log.h" |
| 22 | #include "xml.h" |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 23 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 24 | /* list of yin attribute strings */ |
| 25 | extern const char *const yin_attr_list[]; |
| 26 | #define yin_attr2str(STMT) yin_attr_list[STMT] |
| 27 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 28 | #define YIN_NS_URI "urn:ietf:params:xml:ns:yang:yin:1" |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame^] | 29 | #define name2fullname(name, prefix_len) (prefix_len != 0 ? name - (prefix_len + 1) : name) |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 30 | #define namelen2fulllen(name_len, prefix_len) (prefix_len != 0 ? name_len + prefix_len + 1 : name_len) |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 31 | |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 32 | enum YIN_ARGUMENT { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 33 | 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] | 34 | YIN_ARG_NAME, /**< argument name */ |
| 35 | YIN_ARG_TARGET_NODE, /**< argument target-node */ |
| 36 | YIN_ARG_MODULE, /**< argument module */ |
| 37 | YIN_ARG_VALUE, /**< argument value */ |
| 38 | YIN_ARG_TEXT, /**< argument text */ |
| 39 | YIN_ARG_CONDITION, /**< argument condition */ |
| 40 | YIN_ARG_URI, /**< argument uri */ |
| 41 | YIN_ARG_DATE, /**< argument data */ |
| 42 | YIN_ARG_TAG, /**< argument tag */ |
| 43 | YIN_ARG_XMLNS, /**< argument xmlns */ |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 44 | YIN_ARG_NONE, /**< empty (special value) */ |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 47 | /** |
| 48 | * @brief structure to store instance of xml attribute |
| 49 | */ |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 50 | struct yin_arg_record { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 51 | const char *prefix; /**< start of prefix */ |
| 52 | size_t prefix_len; /**< length of prefix */ |
| 53 | const char *name; /**< start of name */ |
| 54 | size_t name_len; /**< length of name */ |
| 55 | char *content; /**< start of content */ |
| 56 | size_t content_len; /**< length of content */ |
| 57 | int dynamic_content; /**< is set to 1 iff content is dynamically allocated 0 otherwise */ |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * @brief Match argument name. |
| 62 | * |
| 63 | * @param[in] name String representing name. |
| 64 | * @param[in] len Lenght of the name. |
| 65 | * |
David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 66 | * @return YIN_ARGUMENT value. |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 67 | */ |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 68 | 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] | 69 | |
| 70 | /** |
| 71 | * @brief Parse content of whole element as text. |
| 72 | * |
| 73 | * @param[in] xml_ctx Xml context. |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 74 | * @param[in] args Sized array of arguments of current element. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 75 | * @param[in,out] data Data to read from. |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 76 | * @param[out] value Where content of element should be stored. |
David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 77 | * |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 78 | * @return LY_ERR values. |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 79 | */ |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 80 | LY_ERR yin_parse_text_element(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, |
| 81 | const char **value); |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 82 | |
David Sedlák | d9d3a31 | 2019-06-04 09:47:10 +0200 | [diff] [blame] | 83 | /** |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 84 | * @brief Parse import element. |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 85 | * |
| 86 | * @param[in] xml_ctx Xml context. |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 87 | * @param[in] args Sized array of arguments of current element. |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 88 | * @param[in] module_prefix Prefix of the module to check prefix collisions. |
David Sedlák | b666bcc | 2019-06-05 15:00:05 +0200 | [diff] [blame] | 89 | * @param[in,out] data Dta to read from. |
| 90 | * @param[in,out] imports Parsed imports to add to. |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 91 | * |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 92 | * @return LY_ERR values. |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 93 | */ |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 94 | LY_ERR yin_parse_import(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char *module_prefix, |
| 95 | const char **data, struct lysp_import **imports); |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 96 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 97 | /** |
| 98 | * @brief match yang keyword from yin data |
| 99 | * |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 100 | * @param[in] xml_ctx Xml context. |
| 101 | * @param[in] name Name Start of keyword name |
| 102 | * @param[in] name_len Lenght of keyword name. |
| 103 | * @param[in] prefix Start of keyword prefix. |
| 104 | * @param[in] prefix_len lenght of prefix. |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 105 | * |
| 106 | * @return yang_keyword values. |
| 107 | */ |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 108 | enum yang_keyword yin_match_keyword(struct lyxml_context *xml_ctx, const char *name, size_t name_len, |
| 109 | const char *prefix, size_t prefix_len); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 110 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 111 | /** |
| 112 | * @brief Parse status statement. |
| 113 | * |
| 114 | * @param[in] xml_ctx Xml context. |
| 115 | * @param[in,out] data Data to read from. |
| 116 | * @param[in,out] flags Flags to add to. |
| 117 | * @param[in,out] exts Extension instances to add to. |
| 118 | * |
| 119 | * @return LY_ERR values. |
| 120 | */ |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 121 | LY_ERR yin_parse_status(struct lyxml_context *xml_ctx, struct yin_arg_record **status_args, const char **data, |
| 122 | uint16_t *flags, struct lysp_ext_instance **exts); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * @brief parse yin argument, arg_val is unchanged if argument arg_type wasn't found. |
| 126 | * |
| 127 | * @param[in] xml_ctx XML parser context. |
| 128 | * @param[in,out] data Data to read from. |
| 129 | * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments). |
| 130 | * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is specified as YIN_ARG_NONE. |
| 131 | * |
| 132 | * @return LY_ERR values. |
| 133 | */ |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 134 | LY_ERR yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, |
| 135 | enum YIN_ARGUMENT arg_type, const char **arg_val); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 136 | |
| 137 | /** |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 138 | * @brief Load all attributes from current element. Caller is supposed to free args array. |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 139 | * |
| 140 | * @param[in,out] xml_ctx Xml context. |
| 141 | * @param[in,out] data Data to read from, always moved to currently handled position. |
| 142 | * @param[out] args Sized array of attributes. |
| 143 | * |
| 144 | * @return LY_ERR values. |
| 145 | */ |
| 146 | LY_ERR yin_load_attributes(struct lyxml_context *xml_ctx, const char **data, struct yin_arg_record **args); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 147 | |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 148 | /** |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 149 | * @brief Parse yin-elemenet element. |
| 150 | * |
| 151 | * @param[in,out] xml_ctx Xml context. |
| 152 | * @param[in] attrs Sized array of element attributes. |
| 153 | * @param[in,out] data Data to read from, always moved to currently handled position. |
| 154 | * @param[in,out] flags Flags to add to. |
| 155 | * @prama[in,out] extensions Extension instance to add to. |
| 156 | * |
| 157 | * @return LY_ERR values. |
| 158 | */ |
| 159 | LY_ERR yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, |
| 160 | uint16_t *flags, struct lysp_ext **extensions); |
| 161 | |
| 162 | /** |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 163 | * @brief Parse the extension statement. |
| 164 | * |
| 165 | * @param[in] xml_ctx Xml context. |
| 166 | * @param[in] extension_args Arguments of extension element. |
| 167 | * @param[in,out] data Data to read from. |
| 168 | * @param[in,out] extensions Extensions to add to. |
| 169 | * |
| 170 | * @return LY_ERR values. |
| 171 | */ |
| 172 | LY_ERR yin_parse_extension(struct lyxml_context *xml_ctx, struct yin_arg_record **extension_args, |
| 173 | const char **data, struct lysp_ext **extensions); |
| 174 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 175 | /** |
| 176 | * @brief Parse instance of extension. |
| 177 | * |
| 178 | * @param[in,out] xml_ctx Xml context. |
| 179 | * @param[in] attrs Sized array of attributes. |
| 180 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 181 | * @param[in] ext_name Name of the extension element. |
| 182 | * @param[in] ext_name_len Length of extension name. |
| 183 | * @param[in] insubstmt Type of the parrent element. |
| 184 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 185 | * @param[out] exts exts Extension instances to add to. |
| 186 | * |
| 187 | * @return LY_ERR values. |
| 188 | */ |
| 189 | LY_ERR yin_parse_extension_instance(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, |
| 190 | const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt, |
| 191 | uint32_t insubstmt_index, struct lysp_ext_instance **exts); |
| 192 | |
| 193 | /** |
| 194 | * @brief Parse yin element into generic structure. |
| 195 | * |
| 196 | * @param[in,out] xml_ctx Xml context. |
| 197 | * @param[in] name Name of element. |
| 198 | * @param[in] name_len Length of elements Name. |
| 199 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 200 | * @param[out] element Where the element structure should be stored. |
| 201 | * |
| 202 | * @return LY_ERR values. |
| 203 | */ |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 204 | LY_ERR yin_parse_element_generic(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix, |
| 205 | size_t prefix_len, const char **data, struct lysp_stmt **element); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 206 | |
David Sedlák | 2b626ee | 2019-06-03 16:40:18 +0200 | [diff] [blame] | 207 | #endif /* LY_PARSER_YIN_H_*/ |