blob: ed9f9ebd8c1af2fa9bd81d89e65e313c04a9a236 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file common.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
aPieceka83b8e02023-06-07 15:25:16 +02004 * @author Adam Piecek <piecek@cesnet.cz>
Radek Krejcie9f13b12020-11-09 17:42:04 +01005 * @brief libyang's yanglint tool - common functions and definitions for both interactive and non-interactive mode.
6 *
aPieceka83b8e02023-06-07 15:25:16 +02007 * Copyright (c) 2023 CESNET, z.s.p.o.
Radek Krejcie9f13b12020-11-09 17:42:04 +01008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#ifndef COMMON_H_
17#define COMMON_H_
18
19#include <stdint.h>
20#include <stdio.h>
21
22#include "libyang.h"
23
24#define PROMPT "> "
25
26/**
Michal Vaskoc431a0a2021-01-25 14:31:58 +010027 * @brief Default context creation options.
28 */
29#define YL_DEFAULT_CTX_OPTIONS LY_CTX_NO_YANGLIBRARY
30
31/**
Michal Vasko667ce6b2021-01-25 15:00:27 +010032 * @brief Default data parsing flags.
33 */
34#define YL_DEFAULT_DATA_PARSE_OPTIONS LYD_PARSE_STRICT
35
36/**
Michal Vasko05eaf832023-02-10 09:21:46 +010037 * @brief Default data validation flags.
38 */
39#define YL_DEFAULT_DATA_VALIDATE_OPTIONS LYD_VALIDATE_MULTI_ERROR
40
41/**
Radek Krejcie9f13b12020-11-09 17:42:04 +010042 * @brief log error message
43 */
Michal Vasko151ae6c2021-09-23 08:23:51 +020044#define YLMSG_E(...) \
45 fprintf(stderr, "YANGLINT[E]: " __VA_ARGS__)
Radek Krejcie9f13b12020-11-09 17:42:04 +010046
47/**
48 * @brief log warning message
49 */
Michal Vasko151ae6c2021-09-23 08:23:51 +020050#define YLMSG_W(...) \
51 fprintf(stderr, "YANGLINT[W]: " __VA_ARGS__)
Radek Krejcie9f13b12020-11-09 17:42:04 +010052
aPiecek912f3d52022-10-12 10:02:33 +020053#ifndef _WIN32
54# define PATH_SEPARATOR ":"
55#else
56# define PATH_SEPARATOR ";"
57#endif
58
aPieceka83b8e02023-06-07 15:25:16 +020059struct cmdline_file;
60
Radek Krejcie9f13b12020-11-09 17:42:04 +010061/**
62 * @brief Storage for the list of the features (their names) in a specific YANG module.
63 */
64struct schema_features {
Michal Vaskoa9a98612021-11-22 10:00:27 +010065 char *mod_name;
Radek Krejcie9f13b12020-11-09 17:42:04 +010066 char **features;
Michal Vasko686d8fc2021-11-22 10:03:23 +010067 ly_bool applied;
Radek Krejcie9f13b12020-11-09 17:42:04 +010068};
69
70/**
Radek Krejcie9f13b12020-11-09 17:42:04 +010071 * @brief Free the schema features list (struct schema_features *)
72 * @param[in,out] flist The (struct schema_features *) to free.
73 */
74void free_features(void *flist);
75
76/**
77 * @brief Get the list of features connected with the specific YANG module.
78 *
79 * @param[in] fset The set of features information (struct schema_features *).
80 * @param[in] module Name of the YANG module which features should be found.
81 * @param[out] features Pointer to the list of features being returned.
82 */
aPieceka83b8e02023-06-07 15:25:16 +020083void get_features(const struct ly_set *fset, const char *module, const char ***features);
Radek Krejcie9f13b12020-11-09 17:42:04 +010084
85/**
86 * @brief Parse features being specified for the specific YANG module.
87 *
88 * Format of the input @p fstring is as follows: <module_name>:[<feature>,]*
89 *
90 * @param[in] fstring Input string to be parsed.
91 * @param[in, out] fset Features information set (of struct schema_features *). The set is being filled.
92 */
93int parse_features(const char *fstring, struct ly_set *fset);
94
95/**
roman300b8782022-08-11 12:49:21 +020096 * @brief Collect all features of a module.
97 *
98 * @param[in] mod Module to be searched for features.
99 * @param[out] set Set in which the features will be stored.
100 * @return 0 on success.
101 * @return 1 on error.
102 */
103int collect_features(const struct lys_module *mod, struct ly_set *set);
104
105/**
106 * @brief Print all features of a single module.
107 *
108 * @param[in] out The output handler for printing.
109 * @param[in] mod Module which contains the features.
110 * @param[in] set Set which holds the features.
111 */
112void print_features(struct ly_out *out, const struct lys_module *mod, const struct ly_set *set);
113
114/**
115 * @brief Generate a string, which will contain features paramater.
116 *
117 * @param[in] mod Module, for which the string will be generated.
118 * @param[in] set Set containing the features.
119 * @param[out] features_param String which will contain the output.
120 * @return 0 on success.
121 * @return 1 on error.
122 */
123int generate_features_output(const struct lys_module *mod, const struct ly_set *set, char **features_param);
124
125/**
126 * @brief Print all features of all implemented modules.
127 *
128 * @param[in] out The output handler for printing.
129 * @param[in] ctx Libyang context.
130 * @param[in] generate_features Flag expressing whether to generate features parameter.
131 * @param[out] features_param String, which will contain the output if the above flag is set.
132 * @return 0 on success.
133 * @return 1 on error.
134 */
135int print_all_features(struct ly_out *out, const struct ly_ctx *ctx, ly_bool generate_features, char **features_param);
136
137/**
Radek Krejcie9f13b12020-11-09 17:42:04 +0100138 * @brief Parse path of a schema module file into the directory and module name.
139 *
140 * @param[in] path Schema module file path to be parsed.
141 * @param[out] dir Pointer to the directory path where the file resides. Caller is expected to free the returned string.
142 * @param[out] module Pointer to the name of the module (without file suffixes or revision information) specified by the
143 * @path. Caller is expected to free the returned string.
144 * @return 0 on success
145 * @return -1 on error
146 */
147int parse_schema_path(const char *path, char **dir, char **module);
148
149/**
150 * @brief Get input handler for the specified path.
151 *
152 * Using the @p format_schema and @p format_data the type of the file can be limited (by providing NULL) or it can be
153 * got known if both types are possible.
154 *
155 * @param[in] filepath Path of the file to open.
156 * @param[out] format_schema Format of the schema detected from the file name. If NULL specified, the schema formats are
157 * prohibited and such files are refused.
158 * @param[out] format_data Format of the data detected from the file name. If NULL specified, the data formats are
159 * prohibited and such files are refused.
160 * @param[out] in Created input handler referring the file behind the @p filepath.
161 * @return 0 on success.
162 * @return -1 on failure.
163 */
164int get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in);
165
166/**
aPiecekef0a3392023-05-16 10:34:32 +0200167 * @brief Get schema format of the @p filename's content according to the @p filename's suffix.
168 *
Radek Krejcie9f13b12020-11-09 17:42:04 +0100169 * @param[in] filename Name of the file to examine.
aPiecekef0a3392023-05-16 10:34:32 +0200170 * @return Detected schema input format.
171 */
172LYS_INFORMAT get_schema_format(const char *filename);
173
174/**
175 * @brief Get data format of the @p filename's content according to the @p filename's suffix.
176 *
177 * @param[in] filename Name of the file to examine.
178 * @return Detected data input format.
179 */
180LYD_FORMAT get_data_format(const char *filename);
181
182/**
183 * @brief Get format of the @p filename's content according to the @p filename's suffix.
184 *
185 * Either the @p schema or @p data parameter is set.
186 *
187 * @param[in] filename Name of the file to examine.
188 * @param[out] schema_form Pointer to a variable to store the input schema format.
189 * @param[out] data_form Pointer to a variable to store the expected input data format.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100190 * @return zero in case a format was successfully detected.
191 * @return nonzero in case it is not possible to get valid format from the @p filename.
192 */
aPiecekef0a3392023-05-16 10:34:32 +0200193int get_format(const char *filepath, LYS_INFORMAT *schema_form, LYD_FORMAT *data_form);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100194
195/**
196 * @brief Print list of schemas in the context.
197 *
198 * @param[in] out Output handler where to print.
199 * @param[in] ctx Context to print.
200 * @param[in] outformat Optional output format. If not specified (:LYD_UNKNOWN), a simple list with single module per line
201 * is printed. Otherwise, the ietf-yang-library data are printed in the specified format.
202 * @return zero in case the data successfully printed.
203 * @return nonzero in case of error.
204 */
205int print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat);
206
207/**
Radek Krejcie9f13b12020-11-09 17:42:04 +0100208 * @brief Process the input data files - parse, validate and print according to provided options.
209 *
210 * @param[in] ctx libyang context with schema.
Michal Vaskoe0665742021-02-11 11:08:44 +0100211 * @param[in] data_type The type of data in the input files.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100212 * @param[in] merge Flag if the data should be merged before validation.
213 * @param[in] format Data format for printing.
214 * @param[in] out The output handler for printing.
215 * @param[in] options_parse Parser options.
216 * @param[in] options_validate Validation options.
217 * @param[in] options_print Printer options.
218 * @param[in] operational_f Optional operational datastore file information for the case of an extended validation of
219 * operation(s).
Michal Vasko3f08fb92022-04-21 09:52:35 +0200220 * @param[in] rpc_f Source RPC operation file information for parsing NETCONF rpc-reply.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100221 * @param[in] inputs Set of file informations of input data files.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100222 * @param[in] xpath The set of XPaths to be evaluated on the processed data tree, basic information about the resulting set
223 * is printed. Alternative to data printing.
Michal Vasko3f08fb92022-04-21 09:52:35 +0200224 * @return LY_ERR value.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100225 */
Michal Vaskoe0665742021-02-11 11:08:44 +0100226LY_ERR process_data(struct ly_ctx *ctx, enum lyd_type data_type, uint8_t merge, LYD_FORMAT format, struct ly_out *out,
Michal Vasko3f08fb92022-04-21 09:52:35 +0200227 uint32_t options_parse, uint32_t options_validate, uint32_t options_print, struct cmdline_file *operational_f,
228 struct cmdline_file *rpc_f, struct ly_set *inputs, struct ly_set *xpaths);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100229
romanf00089c2022-10-06 16:01:31 +0200230/**
231 * @brief Get the node specified by the path.
232 *
233 * @param[in] ctx libyang context with schema.
234 * @param[in] schema_path Path to the wanted node.
235 * @return Pointer to the schema node specified by the path on success, NULL otherwise.
236 */
stewegd8e2fc92023-05-31 09:52:56 +0200237const struct lysc_node *find_schema_path(const struct ly_ctx *ctx, const char *schema_path);
romanf00089c2022-10-06 16:01:31 +0200238
aPiecek647f62e2023-05-18 10:55:58 +0200239/**
240 * @brief General callback providing run-time extension instance data.
241 *
242 * @param[in] ext Compiled extension instance.
243 * @param[in] user_data User-supplied callback data.
244 * @param[out] ext_data Provided extension instance data.
245 * @param[out] ext_data_free Whether the extension instance should free @p ext_data or not.
246 * @return LY_ERR value.
247 */
248LY_ERR ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free);
249
aPiecek21c1bc82023-05-18 15:38:31 +0200250/**
251 * @brief Concatenation of paths into one string.
252 *
253 * @param[in,out] searchpaths Collection of paths in the single string. Paths are delimited by colon ":"
254 * (on Windows, used semicolon ";" instead).
255 * @param[in] path Path to add.
256 * @return LY_ERR value.
257 */
258LY_ERR searchpath_strcat(char **searchpaths, const char *path);
259
Radek Krejcie9f13b12020-11-09 17:42:04 +0100260#endif /* COMMON_H_ */