blob: 794dfc84b0178f4645f0baa2cdf2d67f2e322fb0 [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"
Michal Vaskoc8a230d2020-08-14 12:17:10 +020019#include "plugins_types.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020020#include "tree_schema_internal.h"
21
22/**
Radek Krejci7931b192020-06-25 17:05:03 +020023 * @brief Mask for checking LYD_PARSE_ options (@ref dataparseroptions)
24 */
25#define LYD_PARSE_OPTS_MASK 0xFFFF0000
26
27/**
Michal Vaskoc8a230d2020-08-14 12:17:10 +020028 * @brief Mask for checking LYD_VALIDATE_ options (@ref datavalidationoptions)
Radek Krejci7931b192020-06-25 17:05:03 +020029 */
30#define LYD_VALIDATE_OPTS_MASK 0x0000FFFF
31
Radek Krejci1798aae2020-07-14 13:26:06 +020032struct lyd_ctx;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020033
Radek Krejci1798aae2020-07-14 13:26:06 +020034/**
35 * @brief Callback for lyd_ctx to free the structure
36 *
37 * @param[in] ctx Data parser context to free.
38 */
39typedef void (*lyd_ctx_free_clb)(struct lyd_ctx *ctx);
40
41/**
42 * @brief Internal (common) context for YANG data parsers.
43 */
44struct lyd_ctx {
45 uint32_t parse_options; /**< various @ref dataparseroptions. */
46 uint32_t validate_options; /**< various @ref datavalidationoptions. */
47 uint32_t int_opts; /**< internal data parser options */
48 uint32_t path_len; /**< used bytes in the path buffer */
49#define LYD_PARSER_BUFSIZE 4078
50 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
51 struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */
52 struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */
53 struct ly_set when_check; /**< set of nodes with "when" conditions */
54 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
55
56 /* callbacks */
Radek Krejci0bff0b12020-08-15 18:37:50 +020057 lyd_ctx_free_clb free; /**< destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020058
59 struct {
Radek Krejci0bff0b12020-08-15 18:37:50 +020060 const struct ly_ctx *ctx; /**< libyang context */
61 uint64_t line; /**< current line */
62 struct ly_in *in; /**< input structure */
63 } *data_ctx; /**< generic pointer supposed to map to and access (common part of) XML/JSON/... parser contexts */
Radek Krejci1798aae2020-07-14 13:26:06 +020064};
65
Radek Krejci7931b192020-06-25 17:05:03 +020066/**
Radek Krejcif0e1ba52020-05-22 15:14:35 +020067 * @brief Parser input structure specifying where the data are read.
68 */
69struct ly_in {
Michal Vasko63f3d842020-07-08 10:10:14 +020070 LY_IN_TYPE type; /**< type of the output to select the output method */
71 const char *current; /**< Current position in the input data */
72 const char *func_start; /**< Input data position when the last parser function was executed */
73 const char *start; /**< Input data start */
74 size_t length; /**< mmap() length (if used) */
Radek Krejcif0e1ba52020-05-22 15:14:35 +020075 union {
Michal Vasko63f3d842020-07-08 10:10:14 +020076 int fd; /**< file descriptor for LY_IN_FD type */
77 FILE *f; /**< file structure for LY_IN_FILE and LY_IN_FILEPATH types */
Radek Krejcif0e1ba52020-05-22 15:14:35 +020078 struct {
Michal Vasko63f3d842020-07-08 10:10:14 +020079 int fd; /**< file descriptor for LY_IN_FILEPATH */
80 char *filepath; /**< stored original filepath */
81 } fpath; /**< filepath structure for LY_IN_FILEPATH */
82 } method; /**< type-specific information about the output */
Radek Krejcif0e1ba52020-05-22 15:14:35 +020083};
84
85/**
Radek Krejci1798aae2020-07-14 13:26:06 +020086 * @brief Common part of the lyd_ctx_free_t callbacks.
87 */
88void lyd_ctx_free(struct lyd_ctx *);
89
90/**
Michal Vasko63f3d842020-07-08 10:10:14 +020091 * @brief Read bytes from an input.
92 *
93 * @param[in] in Input structure.
94 * @param[in] buf Destination buffer.
95 * @param[in] count Number of bytes to read.
96 * @return LY_SUCCESS on success,
97 * @return LY_EDENIED on EOF.
98 */
99LY_ERR ly_in_read(struct ly_in *in, void *buf, size_t count);
100
101/**
102 * @brief Just skip bytes in an input.
103 *
104 * @param[in] in Input structure.
105 * @param[in] count Number of bytes to skip.
106 * @return LY_SUCCESS on success,
107 * @return LY_EDENIED on EOF.
108 */
109LY_ERR ly_in_skip(struct ly_in *in, size_t count);
110
111/**
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200112 * @brief Parse submodule from YANG data.
113 * @param[in,out] ctx Parser context.
114 * @param[in] ly_ctx Context of YANG schemas.
115 * @param[in] main_ctx Parser context of main module.
Michal Vasko63f3d842020-07-08 10:10:14 +0200116 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200117 * @param[out] submod Pointer to the parsed submodule structure.
118 * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID.
119 */
120LY_ERR yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +0200121 struct ly_in *in, struct lysp_submodule **submod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200122
123/**
124 * @brief Parse module from YANG data.
125 * @param[in] ctx Parser context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200126 * @param[in] in Input structure.
127 * @param[in,out] mod Prepared module structure where the parsed information, including the parsed
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200128 * module structure, will be filled in.
129 * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID.
130 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200131LY_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 +0200132
133/**
134 * @brief Parse module from YIN data.
135 *
136 * @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 +0200137 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200138 * @param[in,out] mod Prepared module structure where the parsed information, including the parsed
139 * module structure, will be filled in.
140 *
141 * @return LY_ERR values.
142 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200143LY_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 +0200144
145/**
146 * @brief Parse submodule from YIN data.
147 *
148 * @param[in,out] yin_ctx Context created during parsing, is used to finalize lysp_model after it's completly parsed.
149 * @param[in] ctx Libyang context.
150 * @param[in] main_ctx Parser context of main module.
Michal Vasko63f3d842020-07-08 10:10:14 +0200151 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200152 * @param[in,out] submod Submodule structure where the parsed information, will be filled in.
153 * @return LY_ERR values.
154 */
155LY_ERR yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +0200156 struct ly_in *in, struct lysp_submodule **submod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200157
Radek Krejci1798aae2020-07-14 13:26:06 +0200158/**
159 * @brief Check that a data node representing the @p snode is suitable based on options.
160 *
161 * @param[in] lydctx Common data parsers context.
162 * @param[in] snode Schema node to check.
163 * @return LY_SUCCESS or LY_EVALID
164 */
165LY_ERR lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode);
166
167/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200168 * @brief Wrapper around ::lyd_create_term() for data parsers.
Radek Krejci1798aae2020-07-14 13:26:06 +0200169 *
170 * @param[in] lydctx Data parser context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200171 * @param[in] hints Data parser's hint for the value's type.
Radek Krejci1798aae2020-07-14 13:26:06 +0200172 */
173LY_ERR lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200174 ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200175
176/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200177 * @brief Wrapper around ::lyd_create_meta() for data parsers.
Radek Krejci1798aae2020-07-14 13:26:06 +0200178 *
179 * @param[in] lydctx Data parser context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200180 * @param[in] hints [Value hint](@ref lydvalhints) from the parser regarding the value type.
Radek Krejci1798aae2020-07-14 13:26:06 +0200181 */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200182LY_ERR lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta,
Radek Krejci0f969882020-08-21 16:56:47 +0200183 const struct lys_module *mod, const char *name, size_t name_len, const char *value,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200184 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200185
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200186#endif /* LY_PARSER_INTERNAL_H_ */