blob: d51a8ed5104533f47abdd28c5c47cc4772fd1e67 [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"
29
David Sedlák2b626ee2019-06-03 16:40:18 +020030enum YIN_ARGUMENT {
David Sedlák1bccdfa2019-06-17 15:55:27 +020031 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
David Sedlák2b626ee2019-06-03 16:40:18 +020032 YIN_ARG_NAME, /**< argument name */
33 YIN_ARG_TARGET_NODE, /**< argument target-node */
34 YIN_ARG_MODULE, /**< argument module */
35 YIN_ARG_VALUE, /**< argument value */
36 YIN_ARG_TEXT, /**< argument text */
37 YIN_ARG_CONDITION, /**< argument condition */
38 YIN_ARG_URI, /**< argument uri */
39 YIN_ARG_DATE, /**< argument data */
40 YIN_ARG_TAG, /**< argument tag */
41 YIN_ARG_XMLNS, /**< argument xmlns */
David Sedlák1bccdfa2019-06-17 15:55:27 +020042 YIN_ARG_NONE, /**< empty (special value) */
David Sedlák7ff55a92019-06-17 11:11:41 +020043};
44
David Sedlák1bccdfa2019-06-17 15:55:27 +020045/**
46 * @brief structure to store instance of xml attribute
47 */
David Sedlák7ff55a92019-06-17 11:11:41 +020048struct yin_arg_record {
David Sedlák1bccdfa2019-06-17 15:55:27 +020049 const char *prefix; /**< start of prefix */
50 size_t prefix_len; /**< length of prefix */
51 const char *name; /**< start of name */
52 size_t name_len; /**< length of name */
53 char *content; /**< start of content */
54 size_t content_len; /**< length of content */
55 int dynamic_content; /**< is set to 1 iff content is dynamically allocated 0 otherwise */
David Sedlák2b626ee2019-06-03 16:40:18 +020056};
57
58/**
59 * @brief Match argument name.
60 *
61 * @param[in] name String representing name.
62 * @param[in] len Lenght of the name.
63 *
David Sedlákb1ce3f82019-06-05 14:37:26 +020064 * @return YIN_ARGUMENT value.
David Sedlák2b626ee2019-06-03 16:40:18 +020065 */
David Sedlák060b00e2019-06-19 11:12:06 +020066enum YIN_ARGUMENT yin_match_argument_name(const char *name, size_t len);
David Sedlák2b626ee2019-06-03 16:40:18 +020067
68/**
69 * @brief Parse content of whole element as text.
70 *
71 * @param[in] xml_ctx Xml context.
David Sedlák8f7a1172019-06-20 14:42:18 +020072 * @param[in] args Sized array of arguments of current element.
David Sedlákb666bcc2019-06-05 15:00:05 +020073 * @param[in,out] data Data to read from.
David Sedlák2b626ee2019-06-03 16:40:18 +020074 * @param[out] value Where content of element should be stored.
David Sedlákb1ce3f82019-06-05 14:37:26 +020075 *
David Sedlák2b214ac2019-06-06 16:11:03 +020076 * @return LY_ERR values.
David Sedlák2b626ee2019-06-03 16:40:18 +020077 */
David Sedlák554e36d2019-06-20 16:00:04 +020078LY_ERR yin_parse_text_element(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data,
79 const char **value);
David Sedlák2b626ee2019-06-03 16:40:18 +020080
David Sedlákd9d3a312019-06-04 09:47:10 +020081/**
David Sedlák2b214ac2019-06-06 16:11:03 +020082 * @brief Parse import element.
David Sedlákda63c082019-06-04 13:52:23 +020083 *
84 * @param[in] xml_ctx Xml context.
David Sedlák8f7a1172019-06-20 14:42:18 +020085 * @param[in] args Sized array of arguments of current element.
David Sedlákda63c082019-06-04 13:52:23 +020086 * @param[in] module_prefix Prefix of the module to check prefix collisions.
David Sedlákb666bcc2019-06-05 15:00:05 +020087 * @param[in,out] data Dta to read from.
88 * @param[in,out] imports Parsed imports to add to.
David Sedlákda63c082019-06-04 13:52:23 +020089 *
David Sedlák2b214ac2019-06-06 16:11:03 +020090 * @return LY_ERR values.
David Sedlákda63c082019-06-04 13:52:23 +020091 */
David Sedlák554e36d2019-06-20 16:00:04 +020092LY_ERR yin_parse_import(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char *module_prefix,
93 const char **data, struct lysp_import **imports);
David Sedlák2b626ee2019-06-03 16:40:18 +020094
David Sedlák1bccdfa2019-06-17 15:55:27 +020095/**
96 * @brief match yang keyword from yin data
97 *
David Sedlák8f7a1172019-06-20 14:42:18 +020098 * @param[in] xml_ctx Xml context.
99 * @param[in] name Name Start of keyword name
100 * @param[in] name_len Lenght of keyword name.
101 * @param[in] prefix Start of keyword prefix.
102 * @param[in] prefix_len lenght of prefix.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200103 *
104 * @return yang_keyword values.
105 */
David Sedlák554e36d2019-06-20 16:00:04 +0200106enum yang_keyword yin_match_keyword(struct lyxml_context *xml_ctx, const char *name, size_t name_len,
107 const char *prefix, size_t prefix_len);
David Sedlák1bccdfa2019-06-17 15:55:27 +0200108
David Sedlákb6e65972019-06-19 10:44:13 +0200109/**
110 * @brief Parse status statement.
111 *
112 * @param[in] xml_ctx Xml context.
113 * @param[in,out] data Data to read from.
114 * @param[in,out] flags Flags to add to.
115 * @param[in,out] exts Extension instances to add to.
116 *
117 * @return LY_ERR values.
118 */
David Sedlák554e36d2019-06-20 16:00:04 +0200119LY_ERR yin_parse_status(struct lyxml_context *xml_ctx, struct yin_arg_record **status_args, const char **data,
120 uint16_t *flags, struct lysp_ext_instance **exts);
David Sedlák8f7a1172019-06-20 14:42:18 +0200121
122/**
123 * @brief parse yin argument, arg_val is unchanged if argument arg_type wasn't found.
124 *
125 * @param[in] xml_ctx XML parser context.
126 * @param[in,out] data Data to read from.
127 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments).
128 * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is specified as YIN_ARG_NONE.
129 *
130 * @return LY_ERR values.
131 */
David Sedlák554e36d2019-06-20 16:00:04 +0200132LY_ERR yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args,
133 enum YIN_ARGUMENT arg_type, const char **arg_val);
David Sedlák8f7a1172019-06-20 14:42:18 +0200134
135/**
136 * @brief load all attributes from current element. Caller is supposed to free args array.
137 *
138 * @param[in,out] xml_ctx Xml context.
139 * @param[in,out] data Data to read from, always moved to currently handled position.
140 * @param[out] args Sized array of attributes.
141 *
142 * @return LY_ERR values.
143 */
144LY_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 +0200145
David Sedlák554e36d2019-06-20 16:00:04 +0200146/**
147 * @brief Parse the extension statement.
148 *
149 * @param[in] xml_ctx Xml context.
150 * @param[in] extension_args Arguments of extension element.
151 * @param[in,out] data Data to read from.
152 * @param[in,out] extensions Extensions to add to.
153 *
154 * @return LY_ERR values.
155 */
156LY_ERR yin_parse_extension(struct lyxml_context *xml_ctx, struct yin_arg_record **extension_args,
157 const char **data, struct lysp_ext **extensions);
158
David Sedlák2b626ee2019-06-03 16:40:18 +0200159#endif /* LY_PARSER_YIN_H_*/