blob: b2c4f1567b28f7d8a9b6eb5495dc2830b47e0392 [file] [log] [blame]
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001/**
2 * @file parser_internal.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Internal structures and functions for libyang parsers
5 *
6 * Copyright (c) 2020 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 */
14
15#ifndef LY_PARSER_INTERNAL_H_
16#define LY_PARSER_INTERNAL_H_
17
18#include "parser.h"
19#include "tree_schema_internal.h"
20
21/**
Radek Krejci7931b192020-06-25 17:05:03 +020022 * @brief Mask for checking LYD_PARSE_ options (@ref dataparseroptions)
23 */
24#define LYD_PARSE_OPTS_MASK 0xFFFF0000
25
26/**
27 * @brief Mask for checking LYD_VALIDATEP_ options (@ref datavalidationoptions)
28 */
29#define LYD_VALIDATE_OPTS_MASK 0x0000FFFF
30
Radek Krejci1798aae2020-07-14 13:26:06 +020031struct lyd_ctx;
32/**
33 * @brief Callback for lyd_ctx to free the structure
34 *
35 * @param[in] ctx Data parser context to free.
36 */
37typedef void (*lyd_ctx_free_clb)(struct lyd_ctx *ctx);
38
39/**
40 * @brief Internal (common) context for YANG data parsers.
41 */
42struct lyd_ctx {
43 uint32_t parse_options; /**< various @ref dataparseroptions. */
44 uint32_t validate_options; /**< various @ref datavalidationoptions. */
45 uint32_t int_opts; /**< internal data parser options */
46 uint32_t path_len; /**< used bytes in the path buffer */
47#define LYD_PARSER_BUFSIZE 4078
48 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
49 struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */
50 struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */
51 struct ly_set when_check; /**< set of nodes with "when" conditions */
52 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
53
54 /* callbacks */
55 lyd_ctx_free_clb free; /* destructor */
56 ly_resolve_prefix_clb resolve_prefix;
57
58 struct {
59 const struct ly_ctx *ctx;
60 uint64_t line; /* current line */
61 struct ly_in *in; /* input structure */
62 } *data_ctx; /* generic pointer supposed to map to and access (common part of) XML/JSON/... parser contexts */
63};
64
Radek Krejci7931b192020-06-25 17:05:03 +020065/**
Radek Krejcif0e1ba52020-05-22 15:14:35 +020066 * @brief Parser input structure specifying where the data are read.
67 */
68struct ly_in {
Michal Vasko63f3d842020-07-08 10:10:14 +020069 LY_IN_TYPE type; /**< type of the output to select the output method */
70 const char *current; /**< Current position in the input data */
71 const char *func_start; /**< Input data position when the last parser function was executed */
72 const char *start; /**< Input data start */
73 size_t length; /**< mmap() length (if used) */
Radek Krejcif0e1ba52020-05-22 15:14:35 +020074 union {
Michal Vasko63f3d842020-07-08 10:10:14 +020075 int fd; /**< file descriptor for LY_IN_FD type */
76 FILE *f; /**< file structure for LY_IN_FILE and LY_IN_FILEPATH types */
Radek Krejcif0e1ba52020-05-22 15:14:35 +020077 struct {
Michal Vasko63f3d842020-07-08 10:10:14 +020078 int fd; /**< file descriptor for LY_IN_FILEPATH */
79 char *filepath; /**< stored original filepath */
80 } fpath; /**< filepath structure for LY_IN_FILEPATH */
81 } method; /**< type-specific information about the output */
Radek Krejcif0e1ba52020-05-22 15:14:35 +020082};
83
84/**
Radek Krejci1798aae2020-07-14 13:26:06 +020085 * @brief Common part of the lyd_ctx_free_t callbacks.
86 */
87void lyd_ctx_free(struct lyd_ctx *);
88
89/**
Michal Vasko63f3d842020-07-08 10:10:14 +020090 * @brief Read bytes from an input.
91 *
92 * @param[in] in Input structure.
93 * @param[in] buf Destination buffer.
94 * @param[in] count Number of bytes to read.
95 * @return LY_SUCCESS on success,
96 * @return LY_EDENIED on EOF.
97 */
98LY_ERR ly_in_read(struct ly_in *in, void *buf, size_t count);
99
100/**
101 * @brief Just skip bytes in an input.
102 *
103 * @param[in] in Input structure.
104 * @param[in] count Number of bytes to skip.
105 * @return LY_SUCCESS on success,
106 * @return LY_EDENIED on EOF.
107 */
108LY_ERR ly_in_skip(struct ly_in *in, size_t count);
109
110/**
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200111 * @brief Parse submodule from YANG data.
112 * @param[in,out] ctx Parser context.
113 * @param[in] ly_ctx Context of YANG schemas.
114 * @param[in] main_ctx Parser context of main module.
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200116 * @param[out] submod Pointer to the parsed submodule structure.
117 * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID.
118 */
119LY_ERR yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
Michal Vasko63f3d842020-07-08 10:10:14 +0200120 struct ly_in *in, struct lysp_submodule **submod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200121
122/**
123 * @brief Parse module from YANG data.
124 * @param[in] ctx Parser context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 * @param[in] in Input structure.
126 * @param[in,out] mod Prepared module structure where the parsed information, including the parsed
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200127 * module structure, will be filled in.
128 * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID.
129 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200130LY_ERR yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200131
132/**
133 * @brief Parse module from YIN data.
134 *
135 * @param[in,out] yin_ctx Context created during parsing, is used to finalize lysp_model after it's completly parsed.
Michal Vasko63f3d842020-07-08 10:10:14 +0200136 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200137 * @param[in,out] mod Prepared module structure where the parsed information, including the parsed
138 * module structure, will be filled in.
139 *
140 * @return LY_ERR values.
141 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200142LY_ERR yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200143
144/**
145 * @brief Parse submodule from YIN data.
146 *
147 * @param[in,out] yin_ctx Context created during parsing, is used to finalize lysp_model after it's completly parsed.
148 * @param[in] ctx Libyang context.
149 * @param[in] main_ctx Parser context of main module.
Michal Vasko63f3d842020-07-08 10:10:14 +0200150 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200151 * @param[in,out] submod Submodule structure where the parsed information, will be filled in.
152 * @return LY_ERR values.
153 */
154LY_ERR yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx,
Michal Vasko63f3d842020-07-08 10:10:14 +0200155 struct ly_in *in, struct lysp_submodule **submod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200156
Radek Krejci1798aae2020-07-14 13:26:06 +0200157/**
158 * @brief Check that a data node representing the @p snode is suitable based on options.
159 *
160 * @param[in] lydctx Common data parsers context.
161 * @param[in] snode Schema node to check.
162 * @return LY_SUCCESS or LY_EVALID
163 */
164LY_ERR lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode);
165
166/**
167 * @brief Wrapper around lyd_create_term() for data parsers.
168 *
169 * @param[in] lydctx Data parser context.
170 * @param[in] value_hints Data parser's hint for the value's type.
171 */
172LY_ERR lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const char *value, size_t value_len,
173 int *dynamic, int value_hints, ly_resolve_prefix_clb get_prefix, void *prefix_data,
174 LYD_FORMAT format, struct lyd_node **node);
175
176/**
177 * @brief Wrapper around lyd_create_meta() for data parsers.
178 *
179 * @param[in] lydctx Data parser context.
180 * @param[in] value_hints [Value hint](@ref lydvalueparseopts) from the parser regarding the value type.
181 */
182LY_ERR lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod,
183 const char *name, size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hints,
184 ly_resolve_prefix_clb resolve_prefix, void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode);
185
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200186#endif /* LY_PARSER_INTERNAL_H_ */