blob: c4cc3b1da3e7b7903ae014ee118a3779321cf70a [file] [log] [blame]
David Sedlákb1ce3f82019-06-05 14:37:26 +02001/**
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ák2b626ee2019-06-03 16:40:18 +020014
15#ifndef LY_PARSER_YIN_H_
16#define LY_PARSER_YIN_H_
17
18#include <stdio.h>
David Sedlákb1ce3f82019-06-05 14:37:26 +020019#include <stdlib.h>
20
David Sedlák2b626ee2019-06-03 16:40:18 +020021#include "log.h"
22#include "xml.h"
David Sedlák2b626ee2019-06-03 16:40:18 +020023
David Sedlákf6251182019-06-06 10:22:13 +020024/* list of yin attribute strings */
25extern const char *const yin_attr_list[];
26#define yin_attr2str(STMT) yin_attr_list[STMT]
27
David Sedlák2b214ac2019-06-06 16:11:03 +020028#define YIN_NS_URI "urn:ietf:params:xml:ns:yang:yin:1"
David Sedlákd6e56892019-07-01 15:40:24 +020029#define name2fullname(name, prefix_len) (prefix_len != 0 ? name - (prefix_len + 1) : name)
David Sedlákf250ecf2019-07-01 11:02:05 +020030#define namelen2fulllen(name_len, prefix_len) (prefix_len != 0 ? name_len + prefix_len + 1 : name_len)
David Sedlák2b214ac2019-06-06 16:11:03 +020031
David Sedlák2b626ee2019-06-03 16:40:18 +020032enum YIN_ARGUMENT {
David Sedlák1bccdfa2019-06-17 15:55:27 +020033 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
David Sedlák2b626ee2019-06-03 16:40:18 +020034 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ák1bccdfa2019-06-17 15:55:27 +020044 YIN_ARG_NONE, /**< empty (special value) */
David Sedlák7ff55a92019-06-17 11:11:41 +020045};
46
David Sedlák1bccdfa2019-06-17 15:55:27 +020047/**
48 * @brief structure to store instance of xml attribute
49 */
David Sedlák7ff55a92019-06-17 11:11:41 +020050struct yin_arg_record {
David Sedlák1bccdfa2019-06-17 15:55:27 +020051 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ák2b626ee2019-06-03 16:40:18 +020058};
59
David Sedlák3ffbc522019-07-02 17:49:28 +020060/* flags to encode cardinality of subelement */
61#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
62#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
David Sedlák21f87cd2019-07-03 16:53:23 +020063#define YIN_SUBELEM_FIRST 0x08 /**< is set when subelement is actually yang argument mapped to yin element */
64
65#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
David Sedlák3ffbc522019-07-02 17:49:28 +020066 time to simply check validity of given constraints */
67
David Sedlák619db942019-07-03 14:47:30 +020068#define YIN_ARG_MANDATORY 0x01 /**< is set when attribute is mandatory */
69#define YIN_ARG_IDENTIFIER 0x02 /**< is set when argument value is supposed to be identifier */
70
David Sedlák3ffbc522019-07-02 17:49:28 +020071struct yin_subelement {
72 enum yang_keyword type; /**< type of keyword */
73 void *dest; /**< meta infromation passed to responsible function (information about where parsed subelement should be stored) */
74 uint8_t flags; /**< describes carianlity of subelement can be set to YIN_SUBELEM_MANDATORY and YIN_SUBELEM_UNIQUE */
75};
76
77struct sized_string {
78 const char *value;
79 size_t len;
80};
81
David Sedlák3ffbc522019-07-02 17:49:28 +020082struct yin_argument_meta {
83 uint16_t *flags; /**< */
84 const char **argument; /**< */
85};
86
David Sedlák2b626ee2019-06-03 16:40:18 +020087/**
88 * @brief Match argument name.
89 *
90 * @param[in] name String representing name.
91 * @param[in] len Lenght of the name.
92 *
David Sedlákb1ce3f82019-06-05 14:37:26 +020093 * @return YIN_ARGUMENT value.
David Sedlák2b626ee2019-06-03 16:40:18 +020094 */
David Sedlák060b00e2019-06-19 11:12:06 +020095enum YIN_ARGUMENT yin_match_argument_name(const char *name, size_t len);
David Sedlák2b626ee2019-06-03 16:40:18 +020096
97/**
David Sedlák3ffbc522019-07-02 17:49:28 +020098 * @brief function to parse meta tags eg. elements with text element as child
99 *
100 * @param[in] xml_ctx Xml context.
101 * @param[in] args Sized array of arguments of current element.
102 * @param[in,out] data Data to read from.
103 * @param[out] value Where the content of meta tag should be stored.
104 *
105 * @return LY_ERR values.
106 */
107LY_ERR yin_parse_meta_element(struct lyxml_context *xml_ctx, const char **data, enum yang_keyword elem_type,
108 const char **value, struct lysp_ext_instance **exts);
109
110/**
David Sedlák619db942019-07-03 14:47:30 +0200111 * @brief Map keyword type to substatement info.
112 *
113 * @param[in] kw Keyword type.
114 *
115 * @return correct LYEXT_SUBSTMT information.
116 */
117LYEXT_SUBSTMT kw2lyext_substmt(enum yang_keyword kw);
118
119/**
David Sedlák555c7202019-07-04 12:14:12 +0200120 * @brief Generic function for content parsing
David Sedlák2b626ee2019-06-03 16:40:18 +0200121 *
David Sedlák555c7202019-07-04 12:14:12 +0200122 * @param[in,out] xml_ctx Xml context.
123 * @param[in] subelem_info array of valid subelement types and meta information,
124 * array must be ordered by subelem_info->type in ascending order.
125 * @param[in] subelem_info_size Size of subelem_info array.
126 * @param[in,out] data Data to read from, always moved to currently handled character.
127 * @param[in] current_element Type of current element.
128 * @param[out] Where the text content of element should be stored. Text content is ignored if set to NULL.
129 * @param[in] exts Extension instance to add to. Can be null if element cannot have extension as subelement.
David Sedlák2b214ac2019-06-06 16:11:03 +0200130 * @return LY_ERR values.
David Sedlák2b626ee2019-06-03 16:40:18 +0200131 */
David Sedlák555c7202019-07-04 12:14:12 +0200132LY_ERR yin_parse_content(struct lyxml_context *xml_ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
133 const char **data, enum yang_keyword current_element, const char **text_content,
134 struct lysp_ext_instance **exts);
David Sedlák2b626ee2019-06-03 16:40:18 +0200135
David Sedlákd9d3a312019-06-04 09:47:10 +0200136/**
David Sedlák619db942019-07-03 14:47:30 +0200137 * @brief Parse simple element without any special constaints and argument mapped to yin attribute.
138 *
139 * @param[in] xml_ctx Xml context.
140 * @param[in,out] data Data to read from, always moved to currently handled character.
141 * @param[out] value Where value of attribute should be stored.
142 *
143 * @return LY_ERR values.
144 */
145LY_ERR yin_parse_simple_element(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **data,
146 enum yang_keyword kw, const char **value, enum YIN_ARGUMENT arg_type,
147 uint8_t argument_flags, struct lysp_ext_instance **exts);
148
149/**
David Sedlák2b214ac2019-06-06 16:11:03 +0200150 * @brief Parse import element.
David Sedlákda63c082019-06-04 13:52:23 +0200151 *
152 * @param[in] xml_ctx Xml context.
David Sedlák8f7a1172019-06-20 14:42:18 +0200153 * @param[in] args Sized array of arguments of current element.
David Sedlákda63c082019-06-04 13:52:23 +0200154 * @param[in] module_prefix Prefix of the module to check prefix collisions.
David Sedlák3ffbc522019-07-02 17:49:28 +0200155 * @param[in,out] data Data to read from.
David Sedlákb666bcc2019-06-05 15:00:05 +0200156 * @param[in,out] imports Parsed imports to add to.
David Sedlákda63c082019-06-04 13:52:23 +0200157 *
David Sedlák2b214ac2019-06-06 16:11:03 +0200158 * @return LY_ERR values.
David Sedlákda63c082019-06-04 13:52:23 +0200159 */
David Sedlák619db942019-07-03 14:47:30 +0200160LY_ERR yin_parse_import(struct lyxml_context *xml_ctx, struct yin_arg_record **args,
161 const char **data, struct lysp_module *mod);
David Sedlák2b626ee2019-06-03 16:40:18 +0200162
David Sedlák1bccdfa2019-06-17 15:55:27 +0200163/**
164 * @brief match yang keyword from yin data
165 *
David Sedlák8f7a1172019-06-20 14:42:18 +0200166 * @param[in] xml_ctx Xml context.
167 * @param[in] name Name Start of keyword name
168 * @param[in] name_len Lenght of keyword name.
169 * @param[in] prefix Start of keyword prefix.
170 * @param[in] prefix_len lenght of prefix.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200171 *
172 * @return yang_keyword values.
173 */
David Sedlák554e36d2019-06-20 16:00:04 +0200174enum yang_keyword yin_match_keyword(struct lyxml_context *xml_ctx, const char *name, size_t name_len,
175 const char *prefix, size_t prefix_len);
David Sedlák1bccdfa2019-06-17 15:55:27 +0200176
David Sedlákb6e65972019-06-19 10:44:13 +0200177/**
178 * @brief Parse status statement.
179 *
180 * @param[in] xml_ctx Xml context.
181 * @param[in,out] data Data to read from.
182 * @param[in,out] flags Flags to add to.
183 * @param[in,out] exts Extension instances to add to.
184 *
185 * @return LY_ERR values.
186 */
David Sedlák554e36d2019-06-20 16:00:04 +0200187LY_ERR yin_parse_status(struct lyxml_context *xml_ctx, struct yin_arg_record **status_args, const char **data,
188 uint16_t *flags, struct lysp_ext_instance **exts);
David Sedlák8f7a1172019-06-20 14:42:18 +0200189
190/**
David Sedlák3ffbc522019-07-02 17:49:28 +0200191 * @brief Parse yin argument, arg_val is unchanged if argument arg_type wasn't found.
David Sedlák8f7a1172019-06-20 14:42:18 +0200192 *
193 * @param[in] xml_ctx XML parser context.
194 * @param[in,out] data Data to read from.
195 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments).
196 * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is specified as YIN_ARG_NONE.
David Sedlák619db942019-07-03 14:47:30 +0200197 * @param[in] flags Used to define constraints like mandatory of given attribute can be set to YIN_ATTR_MANDATORY.
198 * @param[in] current_element Identification of current element, used for logging.
David Sedlák8f7a1172019-06-20 14:42:18 +0200199 *
200 * @return LY_ERR values.
201 */
David Sedlák619db942019-07-03 14:47:30 +0200202LY_ERR yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type,
203 const char **arg_val, uint8_t flags, enum yang_keyword current_element);
David Sedlák8f7a1172019-06-20 14:42:18 +0200204
205/**
David Sedlák3ffbc522019-07-02 17:49:28 +0200206 * @brief Parse prefix element.
207 *
208 * @param[in] xml_ctx Xml parser context.
209 * @param[in] attrs Attributes of prefix element.
210 * @param[in,out] prefix Where the value of prefix should be stored.
211 * @param[in,out] data Data to read from, always moved to currently handled character.
212 * @param[in] exts Extension instance to add to.
213 *
214 * @return LY_ERR values.
215 */
216LY_ERR yin_parse_prefix(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **prefix,
217 const char **data, struct lysp_ext_instance **exts);
218
219/**
220 * @brief Parse revision date.
221 *
222 * @param[in] xml_ctx Xml context.
223 * @param[in] args Sized array of arguments of current element.
224 * @param[in,out] data Data to read from.
225 * @param[in,out] rev Array to store the parsed value in.
226 * @param[in,out] exts Extension instances to add to.
227 *
228 * @return LY_ERR values.
229 */
230LY_ERR yin_parse_revision_date(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data,
231 char *rev, struct lysp_ext_instance **exts);
232
233/**
David Sedlák2721d3d2019-06-21 15:37:41 +0200234 * @brief Load all attributes from current element. Caller is supposed to free args array.
David Sedlák8f7a1172019-06-20 14:42:18 +0200235 *
236 * @param[in,out] xml_ctx Xml context.
237 * @param[in,out] data Data to read from, always moved to currently handled position.
238 * @param[out] args Sized array of attributes.
239 *
240 * @return LY_ERR values.
241 */
242LY_ERR yin_load_attributes(struct lyxml_context *xml_ctx, const char **data, struct yin_arg_record **args);
David Sedlákb6e65972019-06-19 10:44:13 +0200243
David Sedlák554e36d2019-06-20 16:00:04 +0200244/**
David Sedlák2721d3d2019-06-21 15:37:41 +0200245 * @brief Parse yin-elemenet element.
246 *
247 * @param[in,out] xml_ctx Xml context.
248 * @param[in] attrs Sized array of element attributes.
249 * @param[in,out] data Data to read from, always moved to currently handled position.
250 * @param[in,out] flags Flags to add to.
David Sedlák3ffbc522019-07-02 17:49:28 +0200251 * @prama[in,out] exts Extension instance to add to.
David Sedlák2721d3d2019-06-21 15:37:41 +0200252 *
253 * @return LY_ERR values.
254 */
David Sedlák3ffbc522019-07-02 17:49:28 +0200255LY_ERR yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **data,
256 uint16_t *flags, struct lysp_ext_instance **exts);
257
258/**
259 * @brief Parse argument element.
260 *
261 * @param[in,out] xml_ctx Xml context.
262 * @param[in] attrs Attributes of this element.
263 * @param[in,out] data Data to read from, always moved to currently handled character.
264 * @param[in,out] arg_meta Meta information about destionation af prased data.
265 * @param[in,out] exts Extension instance to add to.
266 *
267 * @return LY_ERR values.
268 */
269LY_ERR yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
270 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts);
David Sedlák2721d3d2019-06-21 15:37:41 +0200271
272/**
David Sedlák554e36d2019-06-20 16:00:04 +0200273 * @brief Parse the extension statement.
274 *
275 * @param[in] xml_ctx Xml context.
276 * @param[in] extension_args Arguments of extension element.
277 * @param[in,out] data Data to read from.
278 * @param[in,out] extensions Extensions to add to.
279 *
280 * @return LY_ERR values.
281 */
282LY_ERR yin_parse_extension(struct lyxml_context *xml_ctx, struct yin_arg_record **extension_args,
283 const char **data, struct lysp_ext **extensions);
284
David Sedlákb1a78352019-06-28 16:16:29 +0200285/**
286 * @brief Parse instance of extension.
287 *
288 * @param[in,out] xml_ctx Xml context.
289 * @param[in] attrs Sized array of attributes.
290 * @param[in,out] data Data to read from, always moved to currently handled character.
291 * @param[in] ext_name Name of the extension element.
292 * @param[in] ext_name_len Length of extension name.
David Sedlák3ffbc522019-07-02 17:49:28 +0200293 * @param[in,out] metadata Meta information about extension instance passed from caller function.
David Sedlákb1a78352019-06-28 16:16:29 +0200294 *
295 * @return LY_ERR values.
296 */
297LY_ERR yin_parse_extension_instance(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák619db942019-07-03 14:47:30 +0200298 const char *ext_name, int ext_name_len, LYEXT_SUBSTMT subelem,
299 uint32_t subelem_index, struct lysp_ext_instance **exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200300
301/**
302 * @brief Parse yin element into generic structure.
303 *
304 * @param[in,out] xml_ctx Xml context.
305 * @param[in] name Name of element.
306 * @param[in] name_len Length of elements Name.
307 * @param[in,out] data Data to read from, always moved to currently handled character.
308 * @param[out] element Where the element structure should be stored.
309 *
310 * @return LY_ERR values.
311 */
David Sedlákf250ecf2019-07-01 11:02:05 +0200312LY_ERR yin_parse_element_generic(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix,
313 size_t prefix_len, const char **data, struct lysp_stmt **element);
David Sedlákb1a78352019-06-28 16:16:29 +0200314
David Sedlák2b626ee2019-06-03 16:40:18 +0200315#endif /* LY_PARSER_YIN_H_*/