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 | /** |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 31 | * @brief Default data parsing flags. |
| 32 | */ |
| 33 | #define YL_DEFAULT_DATA_PARSE_OPTIONS LYD_PARSE_STRICT |
| 34 | |
| 35 | /** |
Michal Vasko | 05eaf83 | 2023-02-10 09:21:46 +0100 | [diff] [blame] | 36 | * @brief Default data validation flags. |
| 37 | */ |
| 38 | #define YL_DEFAULT_DATA_VALIDATE_OPTIONS LYD_VALIDATE_MULTI_ERROR |
| 39 | |
| 40 | /** |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 41 | * @brief log error message |
| 42 | */ |
Michal Vasko | 151ae6c | 2021-09-23 08:23:51 +0200 | [diff] [blame] | 43 | #define YLMSG_E(...) \ |
| 44 | fprintf(stderr, "YANGLINT[E]: " __VA_ARGS__) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * @brief log warning message |
| 48 | */ |
Michal Vasko | 151ae6c | 2021-09-23 08:23:51 +0200 | [diff] [blame] | 49 | #define YLMSG_W(...) \ |
| 50 | fprintf(stderr, "YANGLINT[W]: " __VA_ARGS__) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 51 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 52 | #ifndef _WIN32 |
| 53 | # define PATH_SEPARATOR ":" |
| 54 | #else |
| 55 | # define PATH_SEPARATOR ";" |
| 56 | #endif |
| 57 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 58 | /** |
| 59 | * @brief Storage for the list of the features (their names) in a specific YANG module. |
| 60 | */ |
| 61 | struct schema_features { |
Michal Vasko | a9a9861 | 2021-11-22 10:00:27 +0100 | [diff] [blame] | 62 | char *mod_name; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 63 | char **features; |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 64 | ly_bool applied; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * @brief Data connected with a file provided on a command line as a file path. |
| 69 | */ |
| 70 | struct cmdline_file { |
| 71 | struct ly_in *in; |
| 72 | const char *path; |
| 73 | LYD_FORMAT format; |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * @brief Free the schema features list (struct schema_features *) |
| 78 | * @param[in,out] flist The (struct schema_features *) to free. |
| 79 | */ |
| 80 | void free_features(void *flist); |
| 81 | |
| 82 | /** |
| 83 | * @brief Get the list of features connected with the specific YANG module. |
| 84 | * |
| 85 | * @param[in] fset The set of features information (struct schema_features *). |
| 86 | * @param[in] module Name of the YANG module which features should be found. |
| 87 | * @param[out] features Pointer to the list of features being returned. |
| 88 | */ |
| 89 | void get_features(struct ly_set *fset, const char *module, const char ***features); |
| 90 | |
| 91 | /** |
| 92 | * @brief Parse features being specified for the specific YANG module. |
| 93 | * |
| 94 | * Format of the input @p fstring is as follows: <module_name>:[<feature>,]* |
| 95 | * |
| 96 | * @param[in] fstring Input string to be parsed. |
| 97 | * @param[in, out] fset Features information set (of struct schema_features *). The set is being filled. |
| 98 | */ |
| 99 | int parse_features(const char *fstring, struct ly_set *fset); |
| 100 | |
| 101 | /** |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 102 | * @brief Collect all features of a module. |
| 103 | * |
| 104 | * @param[in] mod Module to be searched for features. |
| 105 | * @param[out] set Set in which the features will be stored. |
| 106 | * @return 0 on success. |
| 107 | * @return 1 on error. |
| 108 | */ |
| 109 | int collect_features(const struct lys_module *mod, struct ly_set *set); |
| 110 | |
| 111 | /** |
| 112 | * @brief Print all features of a single module. |
| 113 | * |
| 114 | * @param[in] out The output handler for printing. |
| 115 | * @param[in] mod Module which contains the features. |
| 116 | * @param[in] set Set which holds the features. |
| 117 | */ |
| 118 | void print_features(struct ly_out *out, const struct lys_module *mod, const struct ly_set *set); |
| 119 | |
| 120 | /** |
| 121 | * @brief Generate a string, which will contain features paramater. |
| 122 | * |
| 123 | * @param[in] mod Module, for which the string will be generated. |
| 124 | * @param[in] set Set containing the features. |
| 125 | * @param[out] features_param String which will contain the output. |
| 126 | * @return 0 on success. |
| 127 | * @return 1 on error. |
| 128 | */ |
| 129 | int generate_features_output(const struct lys_module *mod, const struct ly_set *set, char **features_param); |
| 130 | |
| 131 | /** |
| 132 | * @brief Print all features of all implemented modules. |
| 133 | * |
| 134 | * @param[in] out The output handler for printing. |
| 135 | * @param[in] ctx Libyang context. |
| 136 | * @param[in] generate_features Flag expressing whether to generate features parameter. |
| 137 | * @param[out] features_param String, which will contain the output if the above flag is set. |
| 138 | * @return 0 on success. |
| 139 | * @return 1 on error. |
| 140 | */ |
| 141 | int print_all_features(struct ly_out *out, const struct ly_ctx *ctx, ly_bool generate_features, char **features_param); |
| 142 | |
| 143 | /** |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 144 | * @brief Parse path of a schema module file into the directory and module name. |
| 145 | * |
| 146 | * @param[in] path Schema module file path to be parsed. |
| 147 | * @param[out] dir Pointer to the directory path where the file resides. Caller is expected to free the returned string. |
| 148 | * @param[out] module Pointer to the name of the module (without file suffixes or revision information) specified by the |
| 149 | * @path. Caller is expected to free the returned string. |
| 150 | * @return 0 on success |
| 151 | * @return -1 on error |
| 152 | */ |
| 153 | int parse_schema_path(const char *path, char **dir, char **module); |
| 154 | |
| 155 | /** |
| 156 | * @brief Get input handler for the specified path. |
| 157 | * |
| 158 | * Using the @p format_schema and @p format_data the type of the file can be limited (by providing NULL) or it can be |
| 159 | * got known if both types are possible. |
| 160 | * |
| 161 | * @param[in] filepath Path of the file to open. |
| 162 | * @param[out] format_schema Format of the schema detected from the file name. If NULL specified, the schema formats are |
| 163 | * prohibited and such files are refused. |
| 164 | * @param[out] format_data Format of the data detected from the file name. If NULL specified, the data formats are |
| 165 | * prohibited and such files are refused. |
| 166 | * @param[out] in Created input handler referring the file behind the @p filepath. |
| 167 | * @return 0 on success. |
| 168 | * @return -1 on failure. |
| 169 | */ |
| 170 | int get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in); |
| 171 | |
| 172 | /** |
| 173 | * @brief Free the command line file data (struct cmdline_file *) |
| 174 | * @param[in,out] cmdline_file The (struct cmdline_file *) to free. |
| 175 | */ |
| 176 | void free_cmdline_file(void *cmdline_file); |
| 177 | |
| 178 | /** |
| 179 | * @brief Create and fill the command line file data (struct cmdline_file *). |
| 180 | * @param[in] set Optional parameter in case the record is supposed to be added into a set. |
| 181 | * @param[in] in Input file handler. |
| 182 | * @param[in] path Filepath of the file. |
| 183 | * @param[in] format Format of the data file. |
| 184 | * @return The created command line file structure. |
| 185 | * @return NULL on failure |
| 186 | */ |
| 187 | struct cmdline_file *fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format); |
| 188 | |
| 189 | /** |
| 190 | * @brief Helper function to prepare argc, argv pair from a command line string. |
| 191 | * |
| 192 | * @param[in] cmdline Complete command line string. |
| 193 | * @param[out] argc_p Pointer to store argc value. |
| 194 | * @param[out] argv_p Pointer to store argv vector. |
| 195 | * @return 0 on success, non-zero on failure. |
| 196 | */ |
| 197 | int parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]); |
| 198 | |
| 199 | /** |
| 200 | * @brief Destructor for the argument vector prepared by ::parse_cmdline(). |
| 201 | * |
| 202 | * @param[in,out] argv Argument vector to destroy. |
| 203 | */ |
| 204 | void free_cmdline(char *argv[]); |
| 205 | |
| 206 | /** |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 207 | * @brief Get schema format of the @p filename's content according to the @p filename's suffix. |
| 208 | * |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 209 | * @param[in] filename Name of the file to examine. |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 210 | * @return Detected schema input format. |
| 211 | */ |
| 212 | LYS_INFORMAT get_schema_format(const char *filename); |
| 213 | |
| 214 | /** |
| 215 | * @brief Get data format of the @p filename's content according to the @p filename's suffix. |
| 216 | * |
| 217 | * @param[in] filename Name of the file to examine. |
| 218 | * @return Detected data input format. |
| 219 | */ |
| 220 | LYD_FORMAT get_data_format(const char *filename); |
| 221 | |
| 222 | /** |
| 223 | * @brief Get format of the @p filename's content according to the @p filename's suffix. |
| 224 | * |
| 225 | * Either the @p schema or @p data parameter is set. |
| 226 | * |
| 227 | * @param[in] filename Name of the file to examine. |
| 228 | * @param[out] schema_form Pointer to a variable to store the input schema format. |
| 229 | * @param[out] data_form Pointer to a variable to store the expected input data format. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 230 | * @return zero in case a format was successfully detected. |
| 231 | * @return nonzero in case it is not possible to get valid format from the @p filename. |
| 232 | */ |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 233 | int get_format(const char *filepath, LYS_INFORMAT *schema_form, LYD_FORMAT *data_form); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * @brief Print list of schemas in the context. |
| 237 | * |
| 238 | * @param[in] out Output handler where to print. |
| 239 | * @param[in] ctx Context to print. |
| 240 | * @param[in] outformat Optional output format. If not specified (:LYD_UNKNOWN), a simple list with single module per line |
| 241 | * is printed. Otherwise, the ietf-yang-library data are printed in the specified format. |
| 242 | * @return zero in case the data successfully printed. |
| 243 | * @return nonzero in case of error. |
| 244 | */ |
| 245 | int print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat); |
| 246 | |
| 247 | /** |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 248 | * @brief Process the input data files - parse, validate and print according to provided options. |
| 249 | * |
| 250 | * @param[in] ctx libyang context with schema. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 251 | * @param[in] data_type The type of data in the input files. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 252 | * @param[in] merge Flag if the data should be merged before validation. |
| 253 | * @param[in] format Data format for printing. |
| 254 | * @param[in] out The output handler for printing. |
| 255 | * @param[in] options_parse Parser options. |
| 256 | * @param[in] options_validate Validation options. |
| 257 | * @param[in] options_print Printer options. |
| 258 | * @param[in] operational_f Optional operational datastore file information for the case of an extended validation of |
| 259 | * operation(s). |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 260 | * @param[in] rpc_f Source RPC operation file information for parsing NETCONF rpc-reply. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 261 | * @param[in] inputs Set of file informations of input data files. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 262 | * @param[in] xpath The set of XPaths to be evaluated on the processed data tree, basic information about the resulting set |
| 263 | * is printed. Alternative to data printing. |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 264 | * @return LY_ERR value. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 265 | */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 266 | LY_ERR process_data(struct ly_ctx *ctx, enum lyd_type data_type, uint8_t merge, LYD_FORMAT format, struct ly_out *out, |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 267 | uint32_t options_parse, uint32_t options_validate, uint32_t options_print, struct cmdline_file *operational_f, |
| 268 | struct cmdline_file *rpc_f, struct ly_set *inputs, struct ly_set *xpaths); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 269 | |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 270 | /** |
| 271 | * @brief Get the node specified by the path. |
| 272 | * |
| 273 | * @param[in] ctx libyang context with schema. |
| 274 | * @param[in] schema_path Path to the wanted node. |
| 275 | * @return Pointer to the schema node specified by the path on success, NULL otherwise. |
| 276 | */ |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 277 | const struct lysc_node *find_schema_path(const struct ly_ctx *ctx, const char *schema_path); |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 278 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 279 | /** |
| 280 | * @brief General callback providing run-time extension instance data. |
| 281 | * |
| 282 | * @param[in] ext Compiled extension instance. |
| 283 | * @param[in] user_data User-supplied callback data. |
| 284 | * @param[out] ext_data Provided extension instance data. |
| 285 | * @param[out] ext_data_free Whether the extension instance should free @p ext_data or not. |
| 286 | * @return LY_ERR value. |
| 287 | */ |
| 288 | LY_ERR ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free); |
| 289 | |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 290 | /** |
| 291 | * @brief Concatenation of paths into one string. |
| 292 | * |
| 293 | * @param[in,out] searchpaths Collection of paths in the single string. Paths are delimited by colon ":" |
| 294 | * (on Windows, used semicolon ";" instead). |
| 295 | * @param[in] path Path to add. |
| 296 | * @return LY_ERR value. |
| 297 | */ |
| 298 | LY_ERR searchpath_strcat(char **searchpaths, const char *path); |
| 299 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 300 | #endif /* COMMON_H_ */ |