Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file common.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang's yanglint tool - common functions and definitions for both interactive and non-interactive mode. |
| 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 COMMON_H_ |
| 16 | #define COMMON_H_ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | #include "libyang.h" |
| 22 | |
| 23 | #define PROMPT "> " |
| 24 | |
| 25 | /** |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame^] | 26 | * @brief Default context creation options. |
| 27 | */ |
| 28 | #define YL_DEFAULT_CTX_OPTIONS LY_CTX_NO_YANGLIBRARY |
| 29 | |
| 30 | /** |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 31 | * @brief log error message |
| 32 | */ |
| 33 | #define YLMSG_E(MSG, ...) \ |
Radek Krejci | 0d7809b | 2020-11-26 12:50:12 +0100 | [diff] [blame] | 34 | fprintf(stderr, "YANGLINT[E]: " MSG, ##__VA_ARGS__) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @brief log warning message |
| 38 | */ |
| 39 | #define YLMSG_W(MSG, ...) \ |
| 40 | fprintf(stderr, "YANGLINT[W]: " MSG, ##__VA_ARGS__) |
| 41 | |
| 42 | /** |
| 43 | * @brief Storage for the list of the features (their names) in a specific YANG module. |
| 44 | */ |
| 45 | struct schema_features { |
| 46 | char *module; |
| 47 | char **features; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * @brief Data connected with a file provided on a command line as a file path. |
| 52 | */ |
| 53 | struct cmdline_file { |
| 54 | struct ly_in *in; |
| 55 | const char *path; |
| 56 | LYD_FORMAT format; |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * @brief Free the schema features list (struct schema_features *) |
| 61 | * @param[in,out] flist The (struct schema_features *) to free. |
| 62 | */ |
| 63 | void free_features(void *flist); |
| 64 | |
| 65 | /** |
| 66 | * @brief Get the list of features connected with the specific YANG module. |
| 67 | * |
| 68 | * @param[in] fset The set of features information (struct schema_features *). |
| 69 | * @param[in] module Name of the YANG module which features should be found. |
| 70 | * @param[out] features Pointer to the list of features being returned. |
| 71 | */ |
| 72 | void get_features(struct ly_set *fset, const char *module, const char ***features); |
| 73 | |
| 74 | /** |
| 75 | * @brief Parse features being specified for the specific YANG module. |
| 76 | * |
| 77 | * Format of the input @p fstring is as follows: <module_name>:[<feature>,]* |
| 78 | * |
| 79 | * @param[in] fstring Input string to be parsed. |
| 80 | * @param[in, out] fset Features information set (of struct schema_features *). The set is being filled. |
| 81 | */ |
| 82 | int parse_features(const char *fstring, struct ly_set *fset); |
| 83 | |
| 84 | /** |
| 85 | * @brief Parse path of a schema module file into the directory and module name. |
| 86 | * |
| 87 | * @param[in] path Schema module file path to be parsed. |
| 88 | * @param[out] dir Pointer to the directory path where the file resides. Caller is expected to free the returned string. |
| 89 | * @param[out] module Pointer to the name of the module (without file suffixes or revision information) specified by the |
| 90 | * @path. Caller is expected to free the returned string. |
| 91 | * @return 0 on success |
| 92 | * @return -1 on error |
| 93 | */ |
| 94 | int parse_schema_path(const char *path, char **dir, char **module); |
| 95 | |
| 96 | /** |
| 97 | * @brief Get input handler for the specified path. |
| 98 | * |
| 99 | * Using the @p format_schema and @p format_data the type of the file can be limited (by providing NULL) or it can be |
| 100 | * got known if both types are possible. |
| 101 | * |
| 102 | * @param[in] filepath Path of the file to open. |
| 103 | * @param[out] format_schema Format of the schema detected from the file name. If NULL specified, the schema formats are |
| 104 | * prohibited and such files are refused. |
| 105 | * @param[out] format_data Format of the data detected from the file name. If NULL specified, the data formats are |
| 106 | * prohibited and such files are refused. |
| 107 | * @param[out] in Created input handler referring the file behind the @p filepath. |
| 108 | * @return 0 on success. |
| 109 | * @return -1 on failure. |
| 110 | */ |
| 111 | int get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in); |
| 112 | |
| 113 | /** |
| 114 | * @brief Free the command line file data (struct cmdline_file *) |
| 115 | * @param[in,out] cmdline_file The (struct cmdline_file *) to free. |
| 116 | */ |
| 117 | void free_cmdline_file(void *cmdline_file); |
| 118 | |
| 119 | /** |
| 120 | * @brief Create and fill the command line file data (struct cmdline_file *). |
| 121 | * @param[in] set Optional parameter in case the record is supposed to be added into a set. |
| 122 | * @param[in] in Input file handler. |
| 123 | * @param[in] path Filepath of the file. |
| 124 | * @param[in] format Format of the data file. |
| 125 | * @return The created command line file structure. |
| 126 | * @return NULL on failure |
| 127 | */ |
| 128 | struct cmdline_file *fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format); |
| 129 | |
| 130 | /** |
| 131 | * @brief Helper function to prepare argc, argv pair from a command line string. |
| 132 | * |
| 133 | * @param[in] cmdline Complete command line string. |
| 134 | * @param[out] argc_p Pointer to store argc value. |
| 135 | * @param[out] argv_p Pointer to store argv vector. |
| 136 | * @return 0 on success, non-zero on failure. |
| 137 | */ |
| 138 | int parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]); |
| 139 | |
| 140 | /** |
| 141 | * @brief Destructor for the argument vector prepared by ::parse_cmdline(). |
| 142 | * |
| 143 | * @param[in,out] argv Argument vector to destroy. |
| 144 | */ |
| 145 | void free_cmdline(char *argv[]); |
| 146 | |
| 147 | /** |
| 148 | * @brief Get expected format of the @p filename's content according to the @p filename's suffix. |
| 149 | * @param[in] filename Name of the file to examine. |
| 150 | * @param[out] schema Pointer to a variable to store the expected input schema format. Do not provide the pointer in case a |
| 151 | * schema format is not expected. |
| 152 | * @param[out] data Pointer to a variable to store the expected input data format. Do not provide the pointer in case a data |
| 153 | * format is not expected. |
| 154 | * @return zero in case a format was successfully detected. |
| 155 | * @return nonzero in case it is not possible to get valid format from the @p filename. |
| 156 | */ |
| 157 | int get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data); |
| 158 | |
| 159 | /** |
| 160 | * @brief Print list of schemas in the context. |
| 161 | * |
| 162 | * @param[in] out Output handler where to print. |
| 163 | * @param[in] ctx Context to print. |
| 164 | * @param[in] outformat Optional output format. If not specified (:LYD_UNKNOWN), a simple list with single module per line |
| 165 | * is printed. Otherwise, the ietf-yang-library data are printed in the specified format. |
| 166 | * @return zero in case the data successfully printed. |
| 167 | * @return nonzero in case of error. |
| 168 | */ |
| 169 | int print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat); |
| 170 | |
| 171 | /** |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 172 | * @brief Process the input data files - parse, validate and print according to provided options. |
| 173 | * |
| 174 | * @param[in] ctx libyang context with schema. |
| 175 | * @param[in] data_type The type of data in the input files, can be 0 for standard data tree and ::LYD_VALIDATE_OP values for |
| 176 | * the operations. |
| 177 | * @param[in] merge Flag if the data should be merged before validation. |
| 178 | * @param[in] format Data format for printing. |
| 179 | * @param[in] out The output handler for printing. |
| 180 | * @param[in] options_parse Parser options. |
| 181 | * @param[in] options_validate Validation options. |
| 182 | * @param[in] options_print Printer options. |
| 183 | * @param[in] operational_f Optional operational datastore file information for the case of an extended validation of |
| 184 | * operation(s). |
| 185 | * @param[in] inputs Set of file informations of input data files. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 186 | * @param[in] xpath The set of XPaths to be evaluated on the processed data tree, basic information about the resulting set |
| 187 | * is printed. Alternative to data printing. |
| 188 | * return LY_ERR value. |
| 189 | */ |
| 190 | LY_ERR process_data(struct ly_ctx *ctx, uint8_t data_type, uint8_t merge, LYD_FORMAT format, struct ly_out *out, |
| 191 | uint32_t options_parse, uint32_t options_validate, uint32_t options_print, |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 192 | struct cmdline_file *operational_f, struct ly_set *inputs, struct ly_set *xpaths); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 193 | |
| 194 | #endif /* COMMON_H_ */ |