blob: 7c6a8adae5d2dc5c51b484954796a14bb85fb931 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
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 Vaskoc431a0a2021-01-25 14:31:58 +010026 * @brief Default context creation options.
27 */
28#define YL_DEFAULT_CTX_OPTIONS LY_CTX_NO_YANGLIBRARY
29
30/**
Michal Vasko667ce6b2021-01-25 15:00:27 +010031 * @brief Default data parsing flags.
32 */
33#define YL_DEFAULT_DATA_PARSE_OPTIONS LYD_PARSE_STRICT
34
35/**
Radek Krejcie9f13b12020-11-09 17:42:04 +010036 * @brief log error message
37 */
Michal Vasko151ae6c2021-09-23 08:23:51 +020038#define YLMSG_E(...) \
39 fprintf(stderr, "YANGLINT[E]: " __VA_ARGS__)
Radek Krejcie9f13b12020-11-09 17:42:04 +010040
41/**
42 * @brief log warning message
43 */
Michal Vasko151ae6c2021-09-23 08:23:51 +020044#define YLMSG_W(...) \
45 fprintf(stderr, "YANGLINT[W]: " __VA_ARGS__)
Radek Krejcie9f13b12020-11-09 17:42:04 +010046
aPiecek912f3d52022-10-12 10:02:33 +020047#ifndef _WIN32
48# define PATH_SEPARATOR ":"
49#else
50# define PATH_SEPARATOR ";"
51#endif
52
Radek Krejcie9f13b12020-11-09 17:42:04 +010053/**
54 * @brief Storage for the list of the features (their names) in a specific YANG module.
55 */
56struct schema_features {
Michal Vaskoa9a98612021-11-22 10:00:27 +010057 char *mod_name;
Radek Krejcie9f13b12020-11-09 17:42:04 +010058 char **features;
Michal Vasko686d8fc2021-11-22 10:03:23 +010059 ly_bool applied;
Radek Krejcie9f13b12020-11-09 17:42:04 +010060};
61
62/**
63 * @brief Data connected with a file provided on a command line as a file path.
64 */
65struct cmdline_file {
66 struct ly_in *in;
67 const char *path;
68 LYD_FORMAT format;
69};
70
71/**
72 * @brief Free the schema features list (struct schema_features *)
73 * @param[in,out] flist The (struct schema_features *) to free.
74 */
75void free_features(void *flist);
76
77/**
78 * @brief Get the list of features connected with the specific YANG module.
79 *
80 * @param[in] fset The set of features information (struct schema_features *).
81 * @param[in] module Name of the YANG module which features should be found.
82 * @param[out] features Pointer to the list of features being returned.
83 */
84void get_features(struct ly_set *fset, const char *module, const char ***features);
85
86/**
87 * @brief Parse features being specified for the specific YANG module.
88 *
89 * Format of the input @p fstring is as follows: <module_name>:[<feature>,]*
90 *
91 * @param[in] fstring Input string to be parsed.
92 * @param[in, out] fset Features information set (of struct schema_features *). The set is being filled.
93 */
94int parse_features(const char *fstring, struct ly_set *fset);
95
96/**
roman300b8782022-08-11 12:49:21 +020097 * @brief Collect all features of a module.
98 *
99 * @param[in] mod Module to be searched for features.
100 * @param[out] set Set in which the features will be stored.
101 * @return 0 on success.
102 * @return 1 on error.
103 */
104int collect_features(const struct lys_module *mod, struct ly_set *set);
105
106/**
107 * @brief Print all features of a single module.
108 *
109 * @param[in] out The output handler for printing.
110 * @param[in] mod Module which contains the features.
111 * @param[in] set Set which holds the features.
112 */
113void print_features(struct ly_out *out, const struct lys_module *mod, const struct ly_set *set);
114
115/**
116 * @brief Generate a string, which will contain features paramater.
117 *
118 * @param[in] mod Module, for which the string will be generated.
119 * @param[in] set Set containing the features.
120 * @param[out] features_param String which will contain the output.
121 * @return 0 on success.
122 * @return 1 on error.
123 */
124int generate_features_output(const struct lys_module *mod, const struct ly_set *set, char **features_param);
125
126/**
127 * @brief Print all features of all implemented modules.
128 *
129 * @param[in] out The output handler for printing.
130 * @param[in] ctx Libyang context.
131 * @param[in] generate_features Flag expressing whether to generate features parameter.
132 * @param[out] features_param String, which will contain the output if the above flag is set.
133 * @return 0 on success.
134 * @return 1 on error.
135 */
136int print_all_features(struct ly_out *out, const struct ly_ctx *ctx, ly_bool generate_features, char **features_param);
137
138/**
Radek Krejcie9f13b12020-11-09 17:42:04 +0100139 * @brief Parse path of a schema module file into the directory and module name.
140 *
141 * @param[in] path Schema module file path to be parsed.
142 * @param[out] dir Pointer to the directory path where the file resides. Caller is expected to free the returned string.
143 * @param[out] module Pointer to the name of the module (without file suffixes or revision information) specified by the
144 * @path. Caller is expected to free the returned string.
145 * @return 0 on success
146 * @return -1 on error
147 */
148int parse_schema_path(const char *path, char **dir, char **module);
149
150/**
151 * @brief Get input handler for the specified path.
152 *
153 * Using the @p format_schema and @p format_data the type of the file can be limited (by providing NULL) or it can be
154 * got known if both types are possible.
155 *
156 * @param[in] filepath Path of the file to open.
157 * @param[out] format_schema Format of the schema detected from the file name. If NULL specified, the schema formats are
158 * prohibited and such files are refused.
159 * @param[out] format_data Format of the data detected from the file name. If NULL specified, the data formats are
160 * prohibited and such files are refused.
161 * @param[out] in Created input handler referring the file behind the @p filepath.
162 * @return 0 on success.
163 * @return -1 on failure.
164 */
165int get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in);
166
167/**
168 * @brief Free the command line file data (struct cmdline_file *)
169 * @param[in,out] cmdline_file The (struct cmdline_file *) to free.
170 */
171void free_cmdline_file(void *cmdline_file);
172
173/**
174 * @brief Create and fill the command line file data (struct cmdline_file *).
175 * @param[in] set Optional parameter in case the record is supposed to be added into a set.
176 * @param[in] in Input file handler.
177 * @param[in] path Filepath of the file.
178 * @param[in] format Format of the data file.
179 * @return The created command line file structure.
180 * @return NULL on failure
181 */
182struct cmdline_file *fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format);
183
184/**
185 * @brief Helper function to prepare argc, argv pair from a command line string.
186 *
187 * @param[in] cmdline Complete command line string.
188 * @param[out] argc_p Pointer to store argc value.
189 * @param[out] argv_p Pointer to store argv vector.
190 * @return 0 on success, non-zero on failure.
191 */
192int parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]);
193
194/**
195 * @brief Destructor for the argument vector prepared by ::parse_cmdline().
196 *
197 * @param[in,out] argv Argument vector to destroy.
198 */
199void free_cmdline(char *argv[]);
200
201/**
202 * @brief Get expected format of the @p filename's content according to the @p filename's suffix.
203 * @param[in] filename Name of the file to examine.
204 * @param[out] schema Pointer to a variable to store the expected input schema format. Do not provide the pointer in case a
205 * schema format is not expected.
206 * @param[out] data Pointer to a variable to store the expected input data format. Do not provide the pointer in case a data
207 * format is not expected.
208 * @return zero in case a format was successfully detected.
209 * @return nonzero in case it is not possible to get valid format from the @p filename.
210 */
211int get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data);
212
213/**
214 * @brief Print list of schemas in the context.
215 *
216 * @param[in] out Output handler where to print.
217 * @param[in] ctx Context to print.
218 * @param[in] outformat Optional output format. If not specified (:LYD_UNKNOWN), a simple list with single module per line
219 * is printed. Otherwise, the ietf-yang-library data are printed in the specified format.
220 * @return zero in case the data successfully printed.
221 * @return nonzero in case of error.
222 */
223int print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat);
224
225/**
Radek Krejcie9f13b12020-11-09 17:42:04 +0100226 * @brief Process the input data files - parse, validate and print according to provided options.
227 *
228 * @param[in] ctx libyang context with schema.
Michal Vaskoe0665742021-02-11 11:08:44 +0100229 * @param[in] data_type The type of data in the input files.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100230 * @param[in] merge Flag if the data should be merged before validation.
231 * @param[in] format Data format for printing.
232 * @param[in] out The output handler for printing.
233 * @param[in] options_parse Parser options.
234 * @param[in] options_validate Validation options.
235 * @param[in] options_print Printer options.
236 * @param[in] operational_f Optional operational datastore file information for the case of an extended validation of
237 * operation(s).
Michal Vasko3f08fb92022-04-21 09:52:35 +0200238 * @param[in] rpc_f Source RPC operation file information for parsing NETCONF rpc-reply.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100239 * @param[in] inputs Set of file informations of input data files.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100240 * @param[in] xpath The set of XPaths to be evaluated on the processed data tree, basic information about the resulting set
241 * is printed. Alternative to data printing.
Michal Vasko3f08fb92022-04-21 09:52:35 +0200242 * @return LY_ERR value.
Radek Krejcie9f13b12020-11-09 17:42:04 +0100243 */
Michal Vaskoe0665742021-02-11 11:08:44 +0100244LY_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 +0200245 uint32_t options_parse, uint32_t options_validate, uint32_t options_print, struct cmdline_file *operational_f,
246 struct cmdline_file *rpc_f, struct ly_set *inputs, struct ly_set *xpaths);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100247
romanf00089c2022-10-06 16:01:31 +0200248/**
249 * @brief Get the node specified by the path.
250 *
251 * @param[in] ctx libyang context with schema.
252 * @param[in] schema_path Path to the wanted node.
253 * @return Pointer to the schema node specified by the path on success, NULL otherwise.
254 */
255const struct lysc_node * find_schema_path(const struct ly_ctx *ctx, const char *schema_path);
256
Radek Krejcie9f13b12020-11-09 17:42:04 +0100257#endif /* COMMON_H_ */