summaryrefslogtreecommitdiffstats
path: root/tools/lint/common.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tools/lint/common.h257
1 files changed, 257 insertions, 0 deletions
diff --git a/tools/lint/common.h b/tools/lint/common.h
new file mode 100644
index 0000000..7c6a8ad
--- /dev/null
+++ b/tools/lint/common.h
@@ -0,0 +1,257 @@
+/**
+ * @file common.h
+ * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @brief libyang's yanglint tool - common functions and definitions for both interactive and non-interactive mode.
+ *
+ * Copyright (c) 2020 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#ifndef COMMON_H_
+#define COMMON_H_
+
+#include <stdint.h>
+#include <stdio.h>
+
+#include "libyang.h"
+
+#define PROMPT "> "
+
+/**
+ * @brief Default context creation options.
+ */
+#define YL_DEFAULT_CTX_OPTIONS LY_CTX_NO_YANGLIBRARY
+
+/**
+ * @brief Default data parsing flags.
+ */
+#define YL_DEFAULT_DATA_PARSE_OPTIONS LYD_PARSE_STRICT
+
+/**
+ * @brief log error message
+ */
+#define YLMSG_E(...) \
+ fprintf(stderr, "YANGLINT[E]: " __VA_ARGS__)
+
+/**
+ * @brief log warning message
+ */
+#define YLMSG_W(...) \
+ fprintf(stderr, "YANGLINT[W]: " __VA_ARGS__)
+
+#ifndef _WIN32
+# define PATH_SEPARATOR ":"
+#else
+# define PATH_SEPARATOR ";"
+#endif
+
+/**
+ * @brief Storage for the list of the features (their names) in a specific YANG module.
+ */
+struct schema_features {
+ char *mod_name;
+ char **features;
+ ly_bool applied;
+};
+
+/**
+ * @brief Data connected with a file provided on a command line as a file path.
+ */
+struct cmdline_file {
+ struct ly_in *in;
+ const char *path;
+ LYD_FORMAT format;
+};
+
+/**
+ * @brief Free the schema features list (struct schema_features *)
+ * @param[in,out] flist The (struct schema_features *) to free.
+ */
+void free_features(void *flist);
+
+/**
+ * @brief Get the list of features connected with the specific YANG module.
+ *
+ * @param[in] fset The set of features information (struct schema_features *).
+ * @param[in] module Name of the YANG module which features should be found.
+ * @param[out] features Pointer to the list of features being returned.
+ */
+void get_features(struct ly_set *fset, const char *module, const char ***features);
+
+/**
+ * @brief Parse features being specified for the specific YANG module.
+ *
+ * Format of the input @p fstring is as follows: <module_name>:[<feature>,]*
+ *
+ * @param[in] fstring Input string to be parsed.
+ * @param[in, out] fset Features information set (of struct schema_features *). The set is being filled.
+ */
+int parse_features(const char *fstring, struct ly_set *fset);
+
+/**
+ * @brief Collect all features of a module.
+ *
+ * @param[in] mod Module to be searched for features.
+ * @param[out] set Set in which the features will be stored.
+ * @return 0 on success.
+ * @return 1 on error.
+ */
+int collect_features(const struct lys_module *mod, struct ly_set *set);
+
+/**
+ * @brief Print all features of a single module.
+ *
+ * @param[in] out The output handler for printing.
+ * @param[in] mod Module which contains the features.
+ * @param[in] set Set which holds the features.
+ */
+void print_features(struct ly_out *out, const struct lys_module *mod, const struct ly_set *set);
+
+/**
+ * @brief Generate a string, which will contain features paramater.
+ *
+ * @param[in] mod Module, for which the string will be generated.
+ * @param[in] set Set containing the features.
+ * @param[out] features_param String which will contain the output.
+ * @return 0 on success.
+ * @return 1 on error.
+ */
+int generate_features_output(const struct lys_module *mod, const struct ly_set *set, char **features_param);
+
+/**
+ * @brief Print all features of all implemented modules.
+ *
+ * @param[in] out The output handler for printing.
+ * @param[in] ctx Libyang context.
+ * @param[in] generate_features Flag expressing whether to generate features parameter.
+ * @param[out] features_param String, which will contain the output if the above flag is set.
+ * @return 0 on success.
+ * @return 1 on error.
+ */
+int print_all_features(struct ly_out *out, const struct ly_ctx *ctx, ly_bool generate_features, char **features_param);
+
+/**
+ * @brief Parse path of a schema module file into the directory and module name.
+ *
+ * @param[in] path Schema module file path to be parsed.
+ * @param[out] dir Pointer to the directory path where the file resides. Caller is expected to free the returned string.
+ * @param[out] module Pointer to the name of the module (without file suffixes or revision information) specified by the
+ * @path. Caller is expected to free the returned string.
+ * @return 0 on success
+ * @return -1 on error
+ */
+int parse_schema_path(const char *path, char **dir, char **module);
+
+/**
+ * @brief Get input handler for the specified path.
+ *
+ * Using the @p format_schema and @p format_data the type of the file can be limited (by providing NULL) or it can be
+ * got known if both types are possible.
+ *
+ * @param[in] filepath Path of the file to open.
+ * @param[out] format_schema Format of the schema detected from the file name. If NULL specified, the schema formats are
+ * prohibited and such files are refused.
+ * @param[out] format_data Format of the data detected from the file name. If NULL specified, the data formats are
+ * prohibited and such files are refused.
+ * @param[out] in Created input handler referring the file behind the @p filepath.
+ * @return 0 on success.
+ * @return -1 on failure.
+ */
+int get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in);
+
+/**
+ * @brief Free the command line file data (struct cmdline_file *)
+ * @param[in,out] cmdline_file The (struct cmdline_file *) to free.
+ */
+void free_cmdline_file(void *cmdline_file);
+
+/**
+ * @brief Create and fill the command line file data (struct cmdline_file *).
+ * @param[in] set Optional parameter in case the record is supposed to be added into a set.
+ * @param[in] in Input file handler.
+ * @param[in] path Filepath of the file.
+ * @param[in] format Format of the data file.
+ * @return The created command line file structure.
+ * @return NULL on failure
+ */
+struct cmdline_file *fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format);
+
+/**
+ * @brief Helper function to prepare argc, argv pair from a command line string.
+ *
+ * @param[in] cmdline Complete command line string.
+ * @param[out] argc_p Pointer to store argc value.
+ * @param[out] argv_p Pointer to store argv vector.
+ * @return 0 on success, non-zero on failure.
+ */
+int parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]);
+
+/**
+ * @brief Destructor for the argument vector prepared by ::parse_cmdline().
+ *
+ * @param[in,out] argv Argument vector to destroy.
+ */
+void free_cmdline(char *argv[]);
+
+/**
+ * @brief Get expected format of the @p filename's content according to the @p filename's suffix.
+ * @param[in] filename Name of the file to examine.
+ * @param[out] schema Pointer to a variable to store the expected input schema format. Do not provide the pointer in case a
+ * schema format is not expected.
+ * @param[out] data Pointer to a variable to store the expected input data format. Do not provide the pointer in case a data
+ * format is not expected.
+ * @return zero in case a format was successfully detected.
+ * @return nonzero in case it is not possible to get valid format from the @p filename.
+ */
+int get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data);
+
+/**
+ * @brief Print list of schemas in the context.
+ *
+ * @param[in] out Output handler where to print.
+ * @param[in] ctx Context to print.
+ * @param[in] outformat Optional output format. If not specified (:LYD_UNKNOWN), a simple list with single module per line
+ * is printed. Otherwise, the ietf-yang-library data are printed in the specified format.
+ * @return zero in case the data successfully printed.
+ * @return nonzero in case of error.
+ */
+int print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat);
+
+/**
+ * @brief Process the input data files - parse, validate and print according to provided options.
+ *
+ * @param[in] ctx libyang context with schema.
+ * @param[in] data_type The type of data in the input files.
+ * @param[in] merge Flag if the data should be merged before validation.
+ * @param[in] format Data format for printing.
+ * @param[in] out The output handler for printing.
+ * @param[in] options_parse Parser options.
+ * @param[in] options_validate Validation options.
+ * @param[in] options_print Printer options.
+ * @param[in] operational_f Optional operational datastore file information for the case of an extended validation of
+ * operation(s).
+ * @param[in] rpc_f Source RPC operation file information for parsing NETCONF rpc-reply.
+ * @param[in] inputs Set of file informations of input data files.
+ * @param[in] xpath The set of XPaths to be evaluated on the processed data tree, basic information about the resulting set
+ * is printed. Alternative to data printing.
+ * @return LY_ERR value.
+ */
+LY_ERR process_data(struct ly_ctx *ctx, enum lyd_type data_type, uint8_t merge, LYD_FORMAT format, struct ly_out *out,
+ uint32_t options_parse, uint32_t options_validate, uint32_t options_print, struct cmdline_file *operational_f,
+ struct cmdline_file *rpc_f, struct ly_set *inputs, struct ly_set *xpaths);
+
+/**
+ * @brief Get the node specified by the path.
+ *
+ * @param[in] ctx libyang context with schema.
+ * @param[in] schema_path Path to the wanted node.
+ * @return Pointer to the schema node specified by the path on success, NULL otherwise.
+ */
+const struct lysc_node * find_schema_path(const struct ly_ctx *ctx, const char *schema_path);
+
+#endif /* COMMON_H_ */