1 /** 2 * @file parser.h 3 * @author Radek Krejci <rkrejci@cesnet.cz> 4 * @brief Parsers for libyang 5 * 6 * Copyright (c) 2015 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_H_ 16 #define LY_PARSER_H_ 17 18 #include <pcre.h> 19 #include <sys/mman.h> 20 21 #include "libyang.h" 22 #include "tree_schema.h" 23 #include "tree_internal.h" 24 25 #ifdef __APPLE__ 26 # ifndef MAP_ANONYMOUS 27 # define MAP_ANONYMOUS MAP_ANON 28 # endif 29 #endif 30 31 /** 32 * @defgroup yin YIN format support 33 * @{ 34 */ 35 struct lys_module *yin_read_module(struct ly_ctx *ctx, const char *data, const char *revision, int implement); 36 struct lys_submodule *yin_read_submodule(struct lys_module *module, const char *data, struct unres_schema *unres); 37 38 /**@} yin */ 39 40 /** 41 * @defgroup xmldata XML data format support 42 * @{ 43 */ 44 struct lyd_node *xml_read_data(struct ly_ctx *ctx, const char *data, int options); 45 46 /**@} xmldata */ 47 48 /** 49 * @defgroup jsondata JSON data format support 50 * @{ 51 */ 52 struct lyd_node *lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *rpc_act, 53 const struct lyd_node *data_tree, const char *yang_data_name); 54 55 /**@} jsondata */ 56 57 /** 58 * @defgroup lybdata LYB data format support 59 * @{ 60 */ 61 struct lyd_node *lyd_parse_lyb(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *data_tree, 62 const char *yang_data_name, int *parsed); 63 64 /**@} lybdata */ 65 66 /** 67 * internal options values for schema parsers 68 */ 69 #define LYS_PARSE_OPT_CFG_NOINHERIT 0x01 /**< do not inherit config flag */ 70 #define LYS_PARSE_OPT_CFG_IGNORE 0x02 /**< ignore config flag (in rpc, actions, notifications) */ 71 #define LYS_PARSE_OPT_CFG_MASK 0x03 72 #define LYS_PARSE_OPT_INGRP 0x04 /**< flag to know that parser is inside a grouping */ 73 74 /* list of YANG statement strings */ 75 extern const char *ly_stmt_str[]; 76 77 enum LY_IDENT { 78 LY_IDENT_SIMPLE, /* only syntax rules */ 79 LY_IDENT_FEATURE, 80 LY_IDENT_IDENTITY, 81 LY_IDENT_TYPE, 82 LY_IDENT_NODE, 83 LY_IDENT_NAME, /* uniqueness across the siblings */ 84 LY_IDENT_PREFIX, 85 LY_IDENT_EXTENSION 86 }; 87 int lyp_yin_fill_ext(void *parent, LYEXT_PAR parent_type, LYEXT_SUBSTMT substmt, uint8_t substmt_index, 88 struct lys_module *module, struct lyxml_elem *yin, struct lys_ext_instance ***ext, 89 uint8_t *ext_size, struct unres_schema *unres); 90 91 int lyp_yin_parse_complex_ext(struct lys_module *mod, struct lys_ext_instance_complex *ext, 92 struct lyxml_elem *yin, struct unres_schema *unres); 93 int lyp_yin_parse_subnode_ext(struct lys_module *mod, void *elem, LYEXT_PAR elem_type, 94 struct lyxml_elem *yin, LYEXT_SUBSTMT type, uint8_t i, struct unres_schema *unres); 95 96 struct lys_type *lyp_get_next_union_type(struct lys_type *type, struct lys_type *prev_type, int *found); 97 98 /* return: 0 - ret set, ok; 1 - ret not set, no log, unknown meta; -1 - ret not set, log, fatal error */ 99 int lyp_fill_attr(struct ly_ctx *ctx, struct lyd_node *parent, const char *module_ns, const char *module_name, 100 const char *attr_name, const char *attr_value, struct lyxml_elem *xml, struct lyd_attr **ret); 101 102 int lyp_check_edit_attr(struct ly_ctx *ctx, struct lyd_attr *attr, struct lyd_node *parent, int *editbits); 103 104 struct lys_type *lyp_parse_value(struct lys_type *type, const char **value_, struct lyxml_elem *xml, 105 struct lyd_node_leaf_list *leaf, struct lyd_attr *attr, struct lys_module *local_mod, 106 int store, int dflt); 107 108 int lyp_check_length_range(struct ly_ctx *ctx, const char *expr, struct lys_type *type); 109 110 int lyp_check_pattern(struct ly_ctx *ctx, const char *pattern, pcre **pcre_precomp); 111 int lyp_precompile_pattern(struct ly_ctx *ctx, const char *pattern, pcre** pcre_cmp, pcre_extra **pcre_std); 112 113 int fill_yin_type(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_type *type, 114 int tpdftype, struct unres_schema *unres); 115 116 int lyp_check_status(uint16_t flags1, struct lys_module *mod1, const char *name1, 117 uint16_t flags2, struct lys_module *mod2, const char *name2, 118 const struct lys_node *node); 119 120 void lyp_del_includedup(struct lys_module *mod, int free_subs); 121 122 int dup_typedef_check(const char *type, struct lys_tpdf *tpdf, int size); 123 124 int dup_identities_check(const char *id, struct lys_module *module); 125 126 /** 127 * @brief Get know if the node is part of the RPC/action's input/output 128 * 129 * @param node Schema node to be examined. 130 * @return 1 for true, 0 for false 131 */ 132 int lyp_is_rpc_action(struct lys_node *node); 133 134 /** 135 * @brief Check validity of data parser options. 136 * 137 * @param options Parser options to be checked. 138 * @param func name of the function where called 139 * @return 0 for ok, 1 when multiple data types bits are set, or incompatible options are used together. 140 */ 141 int lyp_data_check_options(struct ly_ctx *ctx, int options, const char *func); 142 143 int lyp_check_identifier(struct ly_ctx *ctx, const char *id, enum LY_IDENT type, struct lys_module *module, struct lys_node *parent); 144 int lyp_check_date(struct ly_ctx *ctx, const char *date); 145 int lyp_check_mandatory_augment(struct lys_node_augment *node, const struct lys_node *target); 146 int lyp_check_mandatory_choice(struct lys_node *node); 147 148 int lyp_check_include(struct lys_module *module, const char *value, 149 struct lys_include *inc, struct unres_schema *unres); 150 int lyp_check_include_missing(struct lys_module *main_module); 151 int lyp_check_import(struct lys_module *module, const char *value, struct lys_import *imp); 152 int lyp_check_circmod_add(struct lys_module *module); 153 void lyp_check_circmod_pop(struct ly_ctx *ctx); 154 155 void lyp_sort_revisions(struct lys_module *module); 156 int lyp_rfn_apply_ext(struct lys_module *module); 157 int lyp_deviation_apply_ext(struct lys_module *module); 158 int lyp_mand_check_ext(struct lys_ext_instance_complex *ext, const char *ext_name); 159 int lyp_deviate_inherit_config_r(struct lys_node *node); 160 161 const char *lyp_get_yang_data_template_name(const struct lyd_node *node); 162 const struct lys_node *lyp_get_yang_data_template(const struct lys_module *module, const char *yang_data_name, int yang_data_name_len); 163 164 void lyp_ext_instance_rm(struct ly_ctx *ctx, struct lys_ext_instance ***ext, uint8_t *size, uint8_t index); 165 166 /** 167 * @brief Get extension instances from given parent node 168 * 169 * @param[in] ctx Context with the modules 170 * @param[in] elem Parent of the extension instances 171 * @param[in] elem_type Parent type of the extension instances 172 * @param[out] ext_list Address of the extension instance pointer array 173 * @param[out] ext_size Address of size of the extension instance pointer array 174 * @param[out] stmt Short name of the extension instance parent 175 * @return 0 for success, -1 for failure 176 */ 177 int lyp_get_ext_list(struct ly_ctx *ctx, void *elem, LYEXT_PAR elem_type, 178 struct lys_ext_instance ****ext_list, uint8_t **ext_size, const char **stmt); 179 180 /** 181 * @brief Reduce extension instance pointer array 182 * 183 * @param[in] ext Address of the extension instance pointer array 184 * @param[in] new_size New size of the extension instance pointer array 185 * @param[in] orig_size Origin size of the extension instance pointer array 186 * @return 0 for success, -1 for failure 187 */ 188 void lyp_reduce_ext_list(struct lys_ext_instance ***ext, uint8_t new_size, uint8_t orig_size); 189 190 /** 191 * @brief Propagate imports and includes into the main module 192 * 193 * @param module Main module 194 * @param inc Filled include structure 195 * @return 0 for success, 1 for failure 196 */ 197 int lyp_propagate_submodule(struct lys_module *module, struct lys_include *inc); 198 199 /* return: -1 = error, 0 = success, 1 = already there (if it was disabled, it is enabled first) */ 200 int lyp_ctx_check_module(struct lys_module *module); 201 202 int lyp_ctx_add_module(struct lys_module *module); 203 204 /** 205 * @brief Add annotations definitions of attributes and URL config used in ietf-netconf RPCs. 206 */ 207 int lyp_add_ietf_netconf_annotations_config(struct lys_module *mod); 208 209 /** 210 * @brief mmap() wrapper for parsers. To unmap, use lyp_munmap(). 211 * 212 * @param[in] prot The desired memory protection as in case of mmap(). 213 * @param[in] fd File descriptor for getting data. 214 * @param[in] addsize Number of additional bytes to be allocated (and zeroed) after the implicitly added 215 * string-terminating NULL byte. 216 * @param[out] length length of the allocated memory. 217 * @param[out] addr Pointer to the memory where the file data is mapped. 218 * @return 0 on success, non-zero on error. 219 */ 220 int lyp_mmap(struct ly_ctx *ctx, int fd, size_t addsize, size_t *length, void **addr); 221 222 /** 223 * @brief Unmap function for the data mapped by lyp_mmap() 224 */ 225 int lyp_munmap(void *addr, size_t length); 226 227 /** 228 * Store UTF-8 character specified as 4byte integer into the dst buffer. 229 * Returns number of written bytes (4 max), expects that dst has enough space. 230 * 231 * UTF-8 mapping: 232 * 00000000 -- 0000007F: 0xxxxxxx 233 * 00000080 -- 000007FF: 110xxxxx 10xxxxxx 234 * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx 235 * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 236 * 237 */ 238 unsigned int pututf8(struct ly_ctx *ctx, char *dst, int32_t value); 239 unsigned int copyutf8(struct ly_ctx *ctx, char *dst, const char *src); 240 241 /** 242 * @brief Find a module. First, imports from \p module with matching \p prefix, \p name, or both are checked, 243 * \p module itself is also compared, and lastly a callback is used if allowed. 244 * 245 * @param[in] module Module with imports. 246 * @param[in] prefix Module prefix to search for. 247 * @param[in] pref_len Module \p prefix length. If 0, the whole prefix is used, if not NULL. 248 * @param[in] name Module name to search for. 249 * @param[in] name_len Module \p name length. If 0, the whole name is used, if not NULL. 250 * @param[in] in_data Whether to use data callback if not found after trying all the rest. 251 * Import callback is never used because there is no use-case for that. 252 * 253 * @return Matching module, NULL if not found. 254 */ 255 const struct lys_module *lyp_get_module(const struct lys_module *module, const char *prefix, int pref_len, 256 const char *name, int name_len, int in_data); 257 258 /** 259 * @brief Find an import from \p module with matching namespace, the \p module itself is also considered. 260 * 261 * @param[in] module Module with imports. 262 * @param[in] ns Namespace to be found. 263 */ 264 const struct lys_module *lyp_get_import_module_ns(const struct lys_module *module, const char *ns); 265 266 /* 267 * Internal functions implementing YANG (extension and user type) plugin support 268 * - implemented in plugins.c 269 */ 270 271 /** 272 * @brief If available, get the extension plugin for the specified extension 273 * 274 * @param[in] name Name of the extension 275 * @param[in] module Name of the extension's module 276 * @param[in] revision Revision of the extension's module 277 * @return pointer to the extension plugin structure, NULL if no plugin available 278 */ 279 struct lyext_plugin *ext_get_plugin(const char *name, const char *module, const char *revision); 280 281 /** 282 * @brief Try to store a value as a user type defined by a plugin. 283 * 284 * @param[in] mod Module of the type. 285 * @param[in] type_name Type (typedef) name. 286 * @param[in,out] value_str Stored string value, can be overwritten by the user store callback. 287 * @param[in,out] value Filled value to be overwritten by the user store callback. 288 * @return 0 on successful storing, 1 if the type is not a user type, -1 on error. 289 */ 290 int lytype_store(const struct lys_module *mod, const char *type_name, const char **value_str, lyd_val *value); 291 292 /** 293 * @brief Free a user type stored value. 294 * 295 * @param[in] type Type of the value. 296 * @param[in] value Value union to free. 297 * @param[in] value_str String value of the value. 298 */ 299 void lytype_free(const struct lys_type *type, lyd_val value, const char *value_str); 300 301 #endif /* LY_PARSER_H_ */ 302