Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1 | /** |
| 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 Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame^] | 19 | #include "plugins_types.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 20 | #include "tree_schema_internal.h" |
| 21 | |
| 22 | /** |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 23 | * @brief Mask for checking LYD_PARSE_ options (@ref dataparseroptions) |
| 24 | */ |
| 25 | #define LYD_PARSE_OPTS_MASK 0xFFFF0000 |
| 26 | |
| 27 | /** |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame^] | 28 | * @brief Mask for checking LYD_VALIDATE_ options (@ref datavalidationoptions) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 29 | */ |
| 30 | #define LYD_VALIDATE_OPTS_MASK 0x0000FFFF |
| 31 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 32 | struct lyd_ctx; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame^] | 33 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 34 | /** |
| 35 | * @brief Callback for lyd_ctx to free the structure |
| 36 | * |
| 37 | * @param[in] ctx Data parser context to free. |
| 38 | */ |
| 39 | typedef void (*lyd_ctx_free_clb)(struct lyd_ctx *ctx); |
| 40 | |
| 41 | /** |
| 42 | * @brief Internal (common) context for YANG data parsers. |
| 43 | */ |
| 44 | struct 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 */ |
| 57 | lyd_ctx_free_clb free; /* destructor */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 58 | |
| 59 | struct { |
| 60 | const struct ly_ctx *ctx; |
| 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 */ |
| 64 | }; |
| 65 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 66 | /** |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 67 | * @brief Parser input structure specifying where the data are read. |
| 68 | */ |
| 69 | struct ly_in { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 70 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 75 | union { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 76 | int fd; /**< file descriptor for LY_IN_FD type */ |
| 77 | FILE *f; /**< file structure for LY_IN_FILE and LY_IN_FILEPATH types */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 78 | struct { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 79 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 86 | * @brief Common part of the lyd_ctx_free_t callbacks. |
| 87 | */ |
| 88 | void lyd_ctx_free(struct lyd_ctx *); |
| 89 | |
| 90 | /** |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 91 | * @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 | */ |
| 99 | LY_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 | */ |
| 109 | LY_ERR ly_in_skip(struct ly_in *in, size_t count); |
| 110 | |
| 111 | /** |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 112 | * @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 Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 116 | * @param[in] in Input structure. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 117 | * @param[out] submod Pointer to the parsed submodule structure. |
| 118 | * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID. |
| 119 | */ |
| 120 | LY_ERR yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx, |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 121 | struct ly_in *in, struct lysp_submodule **submod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * @brief Parse module from YANG data. |
| 125 | * @param[in] ctx Parser context. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 126 | * @param[in] in Input structure. |
| 127 | * @param[in,out] mod Prepared module structure where the parsed information, including the parsed |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 128 | * module structure, will be filled in. |
| 129 | * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID. |
| 130 | */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 131 | LY_ERR yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 132 | |
| 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 Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 137 | * @param[in] in Input structure. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 138 | * @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 Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 143 | LY_ERR yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 144 | |
| 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 Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 151 | * @param[in] in Input structure. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 152 | * @param[in,out] submod Submodule structure where the parsed information, will be filled in. |
| 153 | * @return LY_ERR values. |
| 154 | */ |
| 155 | LY_ERR yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx, |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 156 | struct ly_in *in, struct lysp_submodule **submod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 157 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 158 | /** |
| 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 | */ |
| 165 | LY_ERR lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode); |
| 166 | |
| 167 | /** |
| 168 | * @brief Wrapper around lyd_create_term() for data parsers. |
| 169 | * |
| 170 | * @param[in] lydctx Data parser context. |
| 171 | * @param[in] value_hints Data parser's hint for the value's type. |
| 172 | */ |
| 173 | LY_ERR lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const char *value, size_t value_len, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame^] | 174 | int *dynamic, int value_hints, LY_PREFIX_FORMAT format, void *prefix_data, struct lyd_node **node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 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 | */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame^] | 182 | LY_ERR lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, |
| 183 | const struct lys_module *mod, const char *name, size_t name_len, const char *value, |
| 184 | size_t value_len, int *dynamic, int value_hints, LY_PREFIX_FORMAT format, |
| 185 | void *prefix_data, const struct lysc_node *ctx_snode); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 186 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 187 | #endif /* LY_PARSER_INTERNAL_H_ */ |