blob: 6d3af0f9b97cdf268d86aa781735405f627346fe [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
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ákb1ce3f82019-06-05 14:37:26 +020066 * @return YIN_ARGUMENT value.
David Sedlák2b626ee2019-06-03 16:40:18 +020067 */
David Sedlák060b00e2019-06-19 11:12:06 +020068enum YIN_ARGUMENT yin_match_argument_name(const char *name, size_t len);
David Sedlák2b626ee2019-06-03 16:40:18 +020069
70/**
71 * @brief Parse content of whole element as text.
72 *
73 * @param[in] xml_ctx Xml context.
David Sedlák8f7a1172019-06-20 14:42:18 +020074 * @param[in] args Sized array of arguments of current element.
David Sedlákb666bcc2019-06-05 15:00:05 +020075 * @param[in,out] data Data to read from.
David Sedlák2b626ee2019-06-03 16:40:18 +020076 * @param[out] value Where content of element should be stored.
David Sedlákb1ce3f82019-06-05 14:37:26 +020077 *
David Sedlák2b214ac2019-06-06 16:11:03 +020078 * @return LY_ERR values.
David Sedlák2b626ee2019-06-03 16:40:18 +020079 */
David Sedlák554e36d2019-06-20 16:00:04 +020080LY_ERR yin_parse_text_element(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data,
81 const char **value);
David Sedlák2b626ee2019-06-03 16:40:18 +020082
David Sedlákd9d3a312019-06-04 09:47:10 +020083/**
David Sedlák2b214ac2019-06-06 16:11:03 +020084 * @brief Parse import element.
David Sedlákda63c082019-06-04 13:52:23 +020085 *
86 * @param[in] xml_ctx Xml context.
David Sedlák8f7a1172019-06-20 14:42:18 +020087 * @param[in] args Sized array of arguments of current element.
David Sedlákda63c082019-06-04 13:52:23 +020088 * @param[in] module_prefix Prefix of the module to check prefix collisions.
David Sedlákb666bcc2019-06-05 15:00:05 +020089 * @param[in,out] data Dta to read from.
90 * @param[in,out] imports Parsed imports to add to.
David Sedlákda63c082019-06-04 13:52:23 +020091 *
David Sedlák2b214ac2019-06-06 16:11:03 +020092 * @return LY_ERR values.
David Sedlákda63c082019-06-04 13:52:23 +020093 */
David Sedlák554e36d2019-06-20 16:00:04 +020094LY_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ák2b626ee2019-06-03 16:40:18 +020096
David Sedlák1bccdfa2019-06-17 15:55:27 +020097/**
98 * @brief match yang keyword from yin data
99 *
David Sedlák8f7a1172019-06-20 14:42:18 +0200100 * @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ák1bccdfa2019-06-17 15:55:27 +0200105 *
106 * @return yang_keyword values.
107 */
David Sedlák554e36d2019-06-20 16:00:04 +0200108enum 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ák1bccdfa2019-06-17 15:55:27 +0200110
David Sedlákb6e65972019-06-19 10:44:13 +0200111/**
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ák554e36d2019-06-20 16:00:04 +0200121LY_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ák8f7a1172019-06-20 14:42:18 +0200123
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ák554e36d2019-06-20 16:00:04 +0200134LY_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ák8f7a1172019-06-20 14:42:18 +0200136
137/**
David Sedlák2721d3d2019-06-21 15:37:41 +0200138 * @brief Load all attributes from current element. Caller is supposed to free args array.
David Sedlák8f7a1172019-06-20 14:42:18 +0200139 *
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 */
146LY_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 +0200147
David Sedlák554e36d2019-06-20 16:00:04 +0200148/**
David Sedlák2721d3d2019-06-21 15:37:41 +0200149 * @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 */
159LY_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ák554e36d2019-06-20 16:00:04 +0200163 * @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 */
172LY_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ákb1a78352019-06-28 16:16:29 +0200175/**
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 */
189LY_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ákf250ecf2019-07-01 11:02:05 +0200204LY_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ákb1a78352019-06-28 16:16:29 +0200206
David Sedlák2b626ee2019-06-03 16:40:18 +0200207#endif /* LY_PARSER_YIN_H_*/