blob: 7faf7e1873dab2191104e840c2f177d6c7d25a9b [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>
David Sedlákc5b20842019-08-13 10:18:31 +02004 * @brief YIN parser header file.
David Sedlákb1ce3f82019-06-05 14:37:26 +02005 *
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ák26ea1432019-08-14 13:42:23 +020028#define VALID_VALS1 " Only valid value is \"%s\"."
29#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
30#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
31#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
32
David Sedlák4ffcec82019-07-25 15:10:21 +020033/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
Radek Krejcid6b76452019-09-03 17:03:03 +020034#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
35 kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \
36 kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \
37 kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
David Sedlák4ffcec82019-07-25 15:10:21 +020038
David Sedlák169cc522019-08-15 13:23:45 +020039/**
40 * @brief insert string into dictionary and store as target.
41 *
42 * @param[in] CTX libyang context.
43 * @param[out] TARGET variable where to store the pointer to the inserted value.
44 * @param[in] DYNAMIC Set to 1 if STR is dynamically allocated, 0 otherwise. If set to 1, zerocopy version of lydict_insert is used.
45 * @param[in] STR string to store.
46 * @param[in] LEN length of the string in WORD to store.
47 */
48#define INSERT_STRING(CTX, TARGET, DYNAMIC, STR, LEN) \
David Sedlák26281532019-08-16 10:56:01 +020049 if (DYNAMIC) {(TARGET) = lydict_insert_zc(CTX, STR);} \
50 else {(TARGET) = lydict_insert(CTX, LEN ? STR : "", LEN);}
David Sedlák169cc522019-08-15 13:23:45 +020051
David Sedlákc5b20842019-08-13 10:18:31 +020052enum yin_argument {
David Sedlák1bccdfa2019-06-17 15:55:27 +020053 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
David Sedlák2b626ee2019-06-03 16:40:18 +020054 YIN_ARG_NAME, /**< argument name */
55 YIN_ARG_TARGET_NODE, /**< argument target-node */
56 YIN_ARG_MODULE, /**< argument module */
57 YIN_ARG_VALUE, /**< argument value */
58 YIN_ARG_TEXT, /**< argument text */
59 YIN_ARG_CONDITION, /**< argument condition */
60 YIN_ARG_URI, /**< argument uri */
61 YIN_ARG_DATE, /**< argument data */
62 YIN_ARG_TAG, /**< argument tag */
David Sedlák1bccdfa2019-06-17 15:55:27 +020063 YIN_ARG_NONE, /**< empty (special value) */
David Sedlák7ff55a92019-06-17 11:11:41 +020064};
65
David Sedlák1bccdfa2019-06-17 15:55:27 +020066/**
67 * @brief structure to store instance of xml attribute
68 */
David Sedlák7ff55a92019-06-17 11:11:41 +020069struct yin_arg_record {
David Sedlák1bccdfa2019-06-17 15:55:27 +020070 const char *prefix; /**< start of prefix */
71 size_t prefix_len; /**< length of prefix */
72 const char *name; /**< start of name */
73 size_t name_len; /**< length of name */
74 char *content; /**< start of content */
75 size_t content_len; /**< length of content */
76 int dynamic_content; /**< is set to 1 iff content is dynamically allocated 0 otherwise */
David Sedlák2b626ee2019-06-03 16:40:18 +020077};
78
David Sedlákc5b20842019-08-13 10:18:31 +020079/* flags to set constraints of subelements */
David Sedlák3ffbc522019-07-02 17:49:28 +020080#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
81#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
David Sedláke1a30302019-07-10 13:49:38 +020082#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
David Sedlák0c2bab92019-07-22 15:33:19 +020083#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
David Sedlák21f87cd2019-07-03 16:53:23 +020084
85#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 +020086 time to simply check validity of given constraints */
87
88struct yin_subelement {
Radek Krejcid6b76452019-09-03 17:03:03 +020089 enum ly_stmt type; /**< type of keyword */
David Sedlák4a650532019-07-10 11:55:18 +020090 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
David Sedlákc5b20842019-08-13 10:18:31 +020091 uint8_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST and YIN_SUBELEM_VER2 */
David Sedlák3ffbc522019-07-02 17:49:28 +020092};
93
David Sedlákb4e44562019-07-04 15:42:12 +020094/* Meta information passed to yin_parse_argument function,
95 holds information about where content of argument element will be stored. */
David Sedlák3ffbc522019-07-02 17:49:28 +020096struct yin_argument_meta {
David Sedlákb4e44562019-07-04 15:42:12 +020097 uint16_t *flags; /**< Argument flags */
98 const char **argument; /**< Argument value */
David Sedlák3ffbc522019-07-02 17:49:28 +020099};
100
David Sedlák6881b512019-08-13 12:52:00 +0200101/**
102 * @brief Meta information passed to functions working with tree_schema,
103 * that require additional information about parent node.
104 */
David Sedlák8a83bbb2019-07-18 14:46:00 +0200105struct tree_node_meta {
106 struct lysp_node *parent; /**< parent node */
David Sedlákbf8a2b72019-08-14 16:48:10 +0200107 struct lysp_node **nodes; /**< linked list of siblings */
David Sedlák8a83bbb2019-07-18 14:46:00 +0200108};
109
David Sedlák6881b512019-08-13 12:52:00 +0200110/**
111 * @brief Meta information passed to yin_parse_import function.
112 */
David Sedlák298ff6d2019-07-26 14:29:03 +0200113struct import_meta {
114 const char *prefix; /**< module prefix. */
115 struct lysp_import **imports; /**< imports to add to. */
116};
117
David Sedlák6881b512019-08-13 12:52:00 +0200118/**
119 * @brief Meta information passed to yin_parse_include function.
120 */
121struct include_meta {
122 const char *name; /**< Module/submodule name. */
123 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
124};
125
126/**
David Sedlák81497a32019-08-13 16:56:26 +0200127 * @brief Meta information passed to yin_parse_inout function.
David Sedlák6881b512019-08-13 12:52:00 +0200128 */
129struct inout_meta {
130 struct lysp_node *parent; /**< Parent node. */
131 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
132};
133
134/**
135 * @brief Meta information passed to yin_parse_minmax function.
136 */
David Sedlák4ffcec82019-07-25 15:10:21 +0200137struct minmax_dev_meta {
David Sedlák6881b512019-08-13 12:52:00 +0200138 uint32_t *lim; /**< min/max value to write to. */
139 uint16_t *flags; /**< min/max flags to write to. */
140 struct lysp_ext_instance **exts; /**< extension instances to add to. */
David Sedlák4ffcec82019-07-25 15:10:21 +0200141};
142
David Sedlák2b626ee2019-06-03 16:40:18 +0200143/**
144 * @brief Match argument name.
145 *
146 * @param[in] name String representing name.
147 * @param[in] len Lenght of the name.
148 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200149 * @return yin_argument values.
David Sedlák2b626ee2019-06-03 16:40:18 +0200150 */
David Sedlákc5b20842019-08-13 10:18:31 +0200151enum yin_argument yin_match_argument_name(const char *name, size_t len);
David Sedlák2b626ee2019-06-03 16:40:18 +0200152
153/**
David Sedlák555c7202019-07-04 12:14:12 +0200154 * @brief Generic function for content parsing
David Sedlák2b626ee2019-06-03 16:40:18 +0200155 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200156 * @param[in,out] ctx Yin parser context for logging and to store current state.
Radek Krejci12b1c152019-09-05 16:20:48 +0200157 * @param[in] subelem_info array of valid subelement types and meta information
David Sedlák555c7202019-07-04 12:14:12 +0200158 * @param[in] subelem_info_size Size of subelem_info array.
159 * @param[in,out] data Data to read from, always moved to currently handled character.
160 * @param[in] current_element Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200161 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
162 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
David Sedlákc5b20842019-08-13 10:18:31 +0200163 *
David Sedlák2b214ac2019-06-06 16:11:03 +0200164 * @return LY_ERR values.
David Sedlák2b626ee2019-06-03 16:40:18 +0200165 */
Radek Krejci12b1c152019-09-05 16:20:48 +0200166LY_ERR yin_parse_content(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Radek Krejcid6b76452019-09-03 17:03:03 +0200167 const char **data, enum ly_stmt current_element, const char **text_content,
David Sedlák555c7202019-07-04 12:14:12 +0200168 struct lysp_ext_instance **exts);
David Sedlák2b626ee2019-06-03 16:40:18 +0200169
David Sedlákd9d3a312019-06-04 09:47:10 +0200170/**
David Sedlák4a650532019-07-10 11:55:18 +0200171 * @brief Check that val is valid UTF8 character sequence of val_type.
172 * Doesn't check empty string, only character validity.
173 *
174 * @param[in] ctx Yin parser context for logging.
175 * @param[in] val_type Type of the input string to select method of checking character validity.
176 * @param[in] val Input to validate.
177 * @param[in] len Length of input.
178 *
179 * @return LY_ERR values.
180 */
181LY_ERR yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len);
182
183/**
David Sedlákb4e44562019-07-04 15:42:12 +0200184 * @brief Match yang keyword from yin data.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200185 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200186 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200187 * @param[in] name Start of keyword name
David Sedlák8f7a1172019-06-20 14:42:18 +0200188 * @param[in] name_len Lenght of keyword name.
189 * @param[in] prefix Start of keyword prefix.
David Sedlákc5b20842019-08-13 10:18:31 +0200190 * @param[in] prefix_len Lenght of prefix.
Radek Krejcid6b76452019-09-03 17:03:03 +0200191 * @param[in] parrent Identification of parrent element, use LY_STMT_NONE for elements without parrent.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200192 *
193 * @return yang_keyword values.
194 */
Radek Krejcid6b76452019-09-03 17:03:03 +0200195enum ly_stmt yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
David Sedlákaa98bba2019-09-12 11:52:14 +0200196 const char *prefix, size_t prefix_len, enum ly_stmt parrent);
David Sedlák1bccdfa2019-06-17 15:55:27 +0200197
David Sedlákb6e65972019-06-19 10:44:13 +0200198/**
David Sedlákc5b20842019-08-13 10:18:31 +0200199 * @brief Load all attributes of element into ([sized array](@ref sizedarrays)). Caller is suposed to free the array.
David Sedlák8f7a1172019-06-20 14:42:18 +0200200 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200201 * @param[in,out] ctx Yin parser context for logging and to store current state.
202 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbba38e52019-07-09 15:20:01 +0200203 * @param[out] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlák8f7a1172019-06-20 14:42:18 +0200204 *
205 * @return LY_ERR values.
206 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200207LY_ERR yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs);
David Sedlákb6e65972019-06-19 10:44:13 +0200208
David Sedlák554e36d2019-06-20 16:00:04 +0200209/**
David Sedlákb1a78352019-06-28 16:16:29 +0200210 * @brief Parse instance of extension.
211 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200212 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200213 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension instance.
David Sedlákb1a78352019-06-28 16:16:29 +0200214 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedláke0ef1c62019-09-13 10:05:55 +0200215 * @param[in] name Name of the extension element.
216 * @param[in] name_len Length of extension name.
217 * @param[in] prefix Prefix of extension name.
218 * @param[in] prefix_len Length of extension prefix.
David Sedlákb4e44562019-07-04 15:42:12 +0200219 * @param[in] subelem Type of the keyword this extension instance is a subelement of.
220 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
221 * @param[in,out] exts Extension instance to add to.
David Sedlákb1a78352019-06-28 16:16:29 +0200222 *
223 * @return LY_ERR values.
224 */
David Sedlák1f90d252019-07-10 17:09:32 +0200225LY_ERR yin_parse_extension_instance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedláke0ef1c62019-09-13 10:05:55 +0200226 const char *ext_name, size_t ext_name_len, const char *ext_prefix, size_t ext_prefix_len,
227 LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200228
229/**
230 * @brief Parse yin element into generic structure.
231 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200232 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb1a78352019-06-28 16:16:29 +0200233 * @param[in] name Name of element.
234 * @param[in] name_len Length of elements Name.
David Sedlák071f7662019-09-12 02:02:51 +0200235 * @param[in] prefix Prefix of element.
236 * @param[in] prefix_len Length of elements prefix.
237 * @param[in] parent Identification of parent element.
David Sedlákb1a78352019-06-28 16:16:29 +0200238 * @param[in,out] data Data to read from, always moved to currently handled character.
239 * @param[out] element Where the element structure should be stored.
240 *
241 * @return LY_ERR values.
242 */
David Sedláke0ef1c62019-09-13 10:05:55 +0200243LY_ERR yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix,
244 size_t prefix_len, enum ly_stmt parent, const char **data, struct lysp_stmt **element);
David Sedlákb1a78352019-06-28 16:16:29 +0200245
David Sedlák4f03b932019-07-26 13:01:47 +0200246/**
David Sedlák298ff6d2019-07-26 14:29:03 +0200247 * @brief Parse module element.
David Sedlák4f03b932019-07-26 13:01:47 +0200248 *
249 * @param[in,out] ctx Yin parser context for logging and to store current state.
250 * @param[in] mod_attrs Attributes of module element.
251 * @param[in,out] data Data to read from.
252 * @param[out] mod Parsed module structure.
253 *
254 * @return LY_ERR values.
255 */
David Sedlák298ff6d2019-07-26 14:29:03 +0200256LY_ERR yin_parse_mod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs,
257 const char **data, struct lysp_module *mod);
David Sedlák4f03b932019-07-26 13:01:47 +0200258
259
260/**
David Sedlák298ff6d2019-07-26 14:29:03 +0200261 * @brief Parse submodule element.
262 *
263 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200264 * @param[in] mod_attrs Attributes of submodule element.
David Sedlák298ff6d2019-07-26 14:29:03 +0200265 * @param[in,out] data Data to read from.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200266 * @param[out] submod Parsed submodule structure.
David Sedlák298ff6d2019-07-26 14:29:03 +0200267 *
268 * @return LY_ERR values.
269 */
270LY_ERR yin_parse_submod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs,
271 const char **data, struct lysp_submodule *submod);
272
273/**
David Sedlák4f03b932019-07-26 13:01:47 +0200274 * @brief free argument record, content loaded from lyxml_get_string() can be
275 * dynamically allocated in some cases so it must be also freed.
276 *
David Sedlákc5b20842019-08-13 10:18:31 +0200277 * @param ctx unused just to fulfill signature of callback for FREE_ARRAY.
278 * @param[in] record Record to free.
David Sedlák4f03b932019-07-26 13:01:47 +0200279 */
280void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record);
281
David Sedlák2b626ee2019-06-03 16:40:18 +0200282#endif /* LY_PARSER_YIN_H_*/