Blame src/parser.h

Packit 8fb591
/**
Packit 8fb591
 * @file parser.h
Packit 8fb591
 * @author Radek Krejci <rkrejci@cesnet.cz>
Packit 8fb591
 * @brief Parsers for libyang
Packit 8fb591
 *
Packit 8fb591
 * Copyright (c) 2015 CESNET, z.s.p.o.
Packit 8fb591
 *
Packit 8fb591
 * This source code is licensed under BSD 3-Clause License (the "License").
Packit 8fb591
 * You may not use this file except in compliance with the License.
Packit 8fb591
 * You may obtain a copy of the License at
Packit 8fb591
 *
Packit 8fb591
 *     https://opensource.org/licenses/BSD-3-Clause
Packit 8fb591
 */
Packit 8fb591
Packit 8fb591
#ifndef LY_PARSER_H_
Packit 8fb591
#define LY_PARSER_H_
Packit 8fb591
Packit 8fb591
#include <pcre.h>
Packit 8fb591
#include <sys/mman.h>
Packit 8fb591
Packit 8fb591
#include "libyang.h"
Packit 8fb591
#include "tree_schema.h"
Packit 8fb591
#include "tree_internal.h"
Packit 8fb591
Packit 8fb591
#ifdef __APPLE__
Packit 8fb591
# ifndef MAP_ANONYMOUS
Packit 8fb591
#  define MAP_ANONYMOUS MAP_ANON
Packit 8fb591
# endif
Packit 8fb591
#endif
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @defgroup yin YIN format support
Packit 8fb591
 * @{
Packit 8fb591
 */
Packit 8fb591
struct lys_module *yin_read_module(struct ly_ctx *ctx, const char *data, const char *revision, int implement);
Packit 8fb591
struct lys_submodule *yin_read_submodule(struct lys_module *module, const char *data,struct unres_schema *unres);
Packit 8fb591
Packit 8fb591
/**@} yin */
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @defgroup xmldata XML data format support
Packit 8fb591
 * @{
Packit 8fb591
 */
Packit 8fb591
struct lyd_node *xml_read_data(struct ly_ctx *ctx, const char *data, int options);
Packit 8fb591
Packit 8fb591
/**@} xmldata */
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @defgroup jsondata JSON data format support
Packit 8fb591
 * @{
Packit 8fb591
 */
Packit 8fb591
struct lyd_node *lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *rpc_act,
Packit 8fb591
                                const struct lyd_node *data_tree, const char *yang_data_name);
Packit 8fb591
Packit 8fb591
/**@} jsondata */
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @defgroup lybdata LYB data format support
Packit 8fb591
 * @{
Packit 8fb591
 */
Packit 8fb591
struct lyd_node *lyd_parse_lyb(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *data_tree,
Packit 8fb591
                               const char *yang_data_name, int *parsed);
Packit 8fb591
Packit 8fb591
/**@} lybdata */
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * internal options values for schema parsers
Packit 8fb591
 */
Packit 8fb591
#define LYS_PARSE_OPT_CFG_NOINHERIT 0x01 /**< do not inherit config flag */
Packit 8fb591
#define LYS_PARSE_OPT_CFG_IGNORE    0x02 /**< ignore config flag (in rpc, actions, notifications) */
Packit 8fb591
#define LYS_PARSE_OPT_CFG_MASK      0x03
Packit 8fb591
#define LYS_PARSE_OPT_INGRP         0x04 /**< flag to know that parser is inside a grouping */
Packit 8fb591
Packit 8fb591
/* list of YANG statement strings */
Packit 8fb591
extern const char *ly_stmt_str[];
Packit 8fb591
Packit 8fb591
enum LY_IDENT {
Packit 8fb591
    LY_IDENT_SIMPLE,   /* only syntax rules */
Packit 8fb591
    LY_IDENT_FEATURE,
Packit 8fb591
    LY_IDENT_IDENTITY,
Packit 8fb591
    LY_IDENT_TYPE,
Packit 8fb591
    LY_IDENT_NODE,
Packit 8fb591
    LY_IDENT_NAME,     /* uniqueness across the siblings */
Packit 8fb591
    LY_IDENT_PREFIX,
Packit 8fb591
    LY_IDENT_EXTENSION
Packit 8fb591
};
Packit 8fb591
int lyp_yin_fill_ext(void *parent, LYEXT_PAR parent_type, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Packit 8fb591
                     struct lys_module *module, struct lyxml_elem *yin, struct lys_ext_instance ***ext,
Packit 8fb591
                     uint8_t ext_index, struct unres_schema *unres);
Packit 8fb591
Packit 8fb591
int lyp_yin_parse_complex_ext(struct lys_module *mod, struct lys_ext_instance_complex *ext,
Packit 8fb591
                              struct lyxml_elem *yin, struct unres_schema *unres);
Packit 8fb591
int lyp_yin_parse_subnode_ext(struct lys_module *mod, void *elem, LYEXT_PAR elem_type,
Packit 8fb591
                              struct lyxml_elem *yin, LYEXT_SUBSTMT type, uint8_t i, struct unres_schema *unres);
Packit 8fb591
Packit 8fb591
struct lys_type *lyp_get_next_union_type(struct lys_type *type, struct lys_type *prev_type, int *found);
Packit 8fb591
Packit 8fb591
/* return: 0 - ret set, ok; 1 - ret not set, no log, unknown meta; -1 - ret not set, log, fatal error */
Packit 8fb591
int lyp_fill_attr(struct ly_ctx *ctx, struct lyd_node *parent, const char *module_ns, const char *module_name,
Packit 8fb591
                  const char *attr_name, const char *attr_value, struct lyxml_elem *xml, int options, struct lyd_attr **ret);
Packit 8fb591
Packit 8fb591
int lyp_check_edit_attr(struct ly_ctx *ctx, struct lyd_attr *attr, struct lyd_node *parent, int *editbits);
Packit 8fb591
Packit 8fb591
struct lys_type *lyp_parse_value(struct lys_type *type, const char **value_, struct lyxml_elem *xml,
Packit 8fb591
                                 struct lyd_node_leaf_list *leaf, struct lyd_attr *attr, struct lys_module *local_mod,
Packit 8fb591
                                 int store, int dflt, int trusted);
Packit 8fb591
Packit 8fb591
int lyp_check_length_range(struct ly_ctx *ctx, const char *expr, struct lys_type *type);
Packit 8fb591
Packit 8fb591
int lyp_check_pattern(struct ly_ctx *ctx, const char *pattern, pcre **pcre_precomp);
Packit 8fb591
int lyp_precompile_pattern(struct ly_ctx *ctx, const char *pattern, pcre** pcre_cmp, pcre_extra **pcre_std);
Packit 8fb591
Packit 8fb591
int fill_yin_type(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_type *type,
Packit 8fb591
                  int tpdftype, struct unres_schema *unres);
Packit 8fb591
Packit 8fb591
int lyp_check_status(uint16_t flags1, struct lys_module *mod1, const char *name1,
Packit 8fb591
                     uint16_t flags2, struct lys_module *mod2, const char *name2,
Packit 8fb591
                     const struct lys_node *node);
Packit 8fb591
Packit 8fb591
void lyp_del_includedup(struct lys_module *mod, int free_subs);
Packit 8fb591
Packit 8fb591
int dup_typedef_check(const char *type, struct lys_tpdf *tpdf, int size);
Packit 8fb591
Packit 8fb591
int dup_identities_check(const char *id, struct lys_module *module);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Get know if the node is part of the RPC/action's input/output
Packit 8fb591
 *
Packit 8fb591
 * @param node Schema node to be examined.
Packit 8fb591
 * @return 1 for true, 0 for false
Packit 8fb591
 */
Packit 8fb591
int lyp_is_rpc_action(struct lys_node *node);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Check validity of data parser options.
Packit 8fb591
 *
Packit 8fb591
 * @param options Parser options to be checked.
Packit 8fb591
 * @param func name of the function where called
Packit 8fb591
 * @return 0 for ok, 1 when multiple data types bits are set, or incompatible options are used together.
Packit 8fb591
 */
Packit 8fb591
int lyp_data_check_options(struct ly_ctx *ctx, int options, const char *func);
Packit 8fb591
Packit 8fb591
int lyp_check_identifier(struct ly_ctx *ctx, const char *id, enum LY_IDENT type, struct lys_module *module, struct lys_node *parent);
Packit 8fb591
int lyp_check_date(struct ly_ctx *ctx, const char *date);
Packit 8fb591
int lyp_check_mandatory_augment(struct lys_node_augment *node, const struct lys_node *target);
Packit 8fb591
int lyp_check_mandatory_choice(struct lys_node *node);
Packit 8fb591
Packit 8fb591
int lyp_check_include(struct lys_module *module, const char *value,
Packit 8fb591
                      struct lys_include *inc, struct unres_schema *unres);
Packit 8fb591
int lyp_check_include_missing(struct lys_module *main_module);
Packit 8fb591
int lyp_check_import(struct lys_module *module, const char *value, struct lys_import *imp);
Packit 8fb591
int lyp_check_circmod_add(struct lys_module *module);
Packit 8fb591
void lyp_check_circmod_pop(struct ly_ctx *ctx);
Packit 8fb591
Packit 8fb591
void lyp_sort_revisions(struct lys_module *module);
Packit 8fb591
int lyp_rfn_apply_ext(struct lys_module *module);
Packit 8fb591
int lyp_deviation_apply_ext(struct lys_module *module);
Packit 8fb591
int lyp_mand_check_ext(struct lys_ext_instance_complex *ext, const char *ext_name);
Packit 8fb591
Packit 8fb591
const char *lyp_get_yang_data_template_name(const struct lyd_node *node);
Packit 8fb591
const struct lys_node *lyp_get_yang_data_template(const struct lys_module *module, const char *yang_data_name, int yang_data_name_len);
Packit 8fb591
Packit 8fb591
void lyp_ext_instance_rm(struct ly_ctx *ctx, struct lys_ext_instance ***ext, uint8_t *size, uint8_t index);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Propagate imports and includes into the main module
Packit 8fb591
 *
Packit 8fb591
 * @param module Main module
Packit 8fb591
 * @param inc Filled include structure
Packit 8fb591
 * @return 0 for success, 1 for failure
Packit 8fb591
 */
Packit 8fb591
int lyp_propagate_submodule(struct lys_module *module, struct lys_include *inc);
Packit 8fb591
Packit 8fb591
/* return: -1 = error, 0 = succes, 1 = already there (if it was disabled, it is enabled first) */
Packit 8fb591
int lyp_ctx_check_module(struct lys_module *module);
Packit 8fb591
Packit 8fb591
int lyp_ctx_add_module(struct lys_module *module);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Add annotations definitions of attributes and URL config used in ietf-netconf RPCs.
Packit 8fb591
 */
Packit 8fb591
int lyp_add_ietf_netconf_annotations_config(struct lys_module *mod);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief mmap() wrapper for parsers. To unmap, use lyp_munmap().
Packit 8fb591
 *
Packit 8fb591
 * @param[in] prot The desired memory protection as in case of mmap().
Packit 8fb591
 * @param[in] fd File descriptor for getting data.
Packit 8fb591
 * @param[in] addsize Number of additional bytes to be allocated (and zeroed) after the implicitly added
Packit 8fb591
 *                    string-terminating NULL byte.
Packit 8fb591
 * @param[out] length length of the allocated memory.
Packit 8fb591
 * @param[out] addr Pointer to the memory where the file data is mapped.
Packit 8fb591
 * @return 0 on success, non-zero on error.
Packit 8fb591
 */
Packit 8fb591
int lyp_mmap(struct ly_ctx *ctx, int fd, size_t addsize, size_t *length, void **addr);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Unmap function for the data mapped by lyp_mmap()
Packit 8fb591
 */
Packit 8fb591
int lyp_munmap(void *addr, size_t length);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * Store UTF-8 character specified as 4byte integer into the dst buffer.
Packit 8fb591
 * Returns number of written bytes (4 max), expects that dst has enough space.
Packit 8fb591
 *
Packit 8fb591
 * UTF-8 mapping:
Packit 8fb591
 * 00000000 -- 0000007F:    0xxxxxxx
Packit 8fb591
 * 00000080 -- 000007FF:    110xxxxx 10xxxxxx
Packit 8fb591
 * 00000800 -- 0000FFFF:    1110xxxx 10xxxxxx 10xxxxxx
Packit 8fb591
 * 00010000 -- 001FFFFF:    11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
Packit 8fb591
 *
Packit 8fb591
 */
Packit 8fb591
unsigned int pututf8(struct ly_ctx *ctx, char *dst, int32_t value);
Packit 8fb591
unsigned int copyutf8(struct ly_ctx *ctx, char *dst, const char *src);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Find a module. First, imports from \p module with matching \p prefix, \p name, or both are checked,
Packit 8fb591
 * \p module itself is also compared, and lastly a callback is used if allowed.
Packit 8fb591
 *
Packit 8fb591
 * @param[in] module Module with imports.
Packit 8fb591
 * @param[in] prefix Module prefix to search for.
Packit 8fb591
 * @param[in] pref_len Module \p prefix length. If 0, the whole prefix is used, if not NULL.
Packit 8fb591
 * @param[in] name Module name to search for.
Packit 8fb591
 * @param[in] name_len Module \p name length. If 0, the whole name is used, if not NULL.
Packit 8fb591
 * @param[in] in_data Whether to use data callback if not found after trying all the rest.
Packit 8fb591
 * Import callback is never used because there is no use-case for that.
Packit 8fb591
 *
Packit 8fb591
 * @return Matching module, NULL if not found.
Packit 8fb591
 */
Packit 8fb591
const struct lys_module *lyp_get_module(const struct lys_module *module, const char *prefix, int pref_len,
Packit 8fb591
                                               const char *name, int name_len, int in_data);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Find an import from \p module with matching namespace, the \p module itself is also considered.
Packit 8fb591
 *
Packit 8fb591
 * @param[in] module Module with imports.
Packit 8fb591
 * @param[in] ns Namespace to be found.
Packit 8fb591
 */
Packit 8fb591
const struct lys_module *lyp_get_import_module_ns(const struct lys_module *module, const char *ns);
Packit 8fb591
Packit 8fb591
/*
Packit 8fb591
 * Internal functions implementing YANG (extension and user type) plugin support
Packit 8fb591
 * - implemented in plugins.c
Packit 8fb591
 */
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief If available, get the extension plugin for the specified extension
Packit 8fb591
 *
Packit 8fb591
 * @param[in] name Name of the extension
Packit 8fb591
 * @param[in] module Name of the extension's module
Packit 8fb591
 * @param[in] revision Revision of the extension's module
Packit 8fb591
 * @return pointer to the extension plugin structure, NULL if no plugin available
Packit 8fb591
 */
Packit 8fb591
struct lyext_plugin *ext_get_plugin(const char *name, const char *module, const char *revision);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Try to store a value as a user type defined by a plugin.
Packit 8fb591
 *
Packit 8fb591
 * @param[in] mod Module of the type.
Packit 8fb591
 * @param[in] type_name Type (typedef) name.
Packit 8fb591
 * @param[in] value_str Value to store as a string.
Packit 8fb591
 * @param[in,out] value Filled value to be overwritten by the user store callback.
Packit 8fb591
 * @return 0 on successful storing, 1 if the type is not a user type, -1 on error.
Packit 8fb591
 */
Packit 8fb591
int lytype_store(const struct lys_module *mod, const char *type_name, const char *value_str, lyd_val *value);
Packit 8fb591
Packit 8fb591
/**
Packit 8fb591
 * @brief Free a user type stored value.
Packit 8fb591
 *
Packit 8fb591
 * @param[in] mod Module of the type.
Packit 8fb591
 * @param[in] type_name Type (typedef) name.
Packit 8fb591
 * @param[in] value Value union to free.
Packit 8fb591
 */
Packit 8fb591
void lytype_free(const struct lys_module *mod, const char *type_name, lyd_val value);
Packit 8fb591
Packit 8fb591
#endif /* LY_PARSER_H_ */