blob: d66ae4de9160b46dd84ef5531b99a302ba41d4de [file] [log] [blame]
aPieceka83b8e02023-06-07 15:25:16 +02001/**
2 * @file yl_opt.h
3 * @author Adam Piecek <piecek@cesnet.cz>
4 * @brief Settings options for the libyang context.
5 *
6 * Copyright (c) 2020 - 2023 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 YL_OPT_H_
16#define YL_OPT_H_
17
18#include "parser_data.h" /* enum lyd_type */
19#include "printer_schema.h" /* LYS_OUTFORMAT */
20#include "set.h" /* ly_set */
21
22/**
23 * @brief Data connected with a file provided on a command line as a file path.
24 */
25struct cmdline_file {
26 struct ly_in *in;
27 const char *path;
28 LYD_FORMAT format;
29};
30
31/**
32 * @brief Create and fill the command line file data (struct cmdline_file *).
33 * @param[in] set Optional parameter in case the record is supposed to be added into a set.
34 * @param[in] in Input file handler.
35 * @param[in] path Filepath of the file.
36 * @param[in] format Format of the data file.
37 * @return The created command line file structure.
38 * @return NULL on failure
39 */
40struct cmdline_file *fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format);
41
42/**
43 * @brief Free the command line file data items.
44 * @param[in,out] rec record to free.
45 */
46void free_cmdline_file_items(struct cmdline_file *rec);
47
48/**
49 * @brief Free the command line file data (struct cmdline_file *).
50 * @param[in,out] cmdline_file The (struct cmdline_file *) to free.
51 */
52void free_cmdline_file(void *cmdline_file);
53
54/**
55 * @brief Context structure to hold and pass variables in a structured form.
56 */
57struct yl_opt {
58 /* Set to 1 if yanglint running in the interactive mode */
59 ly_bool interactive;
60 ly_bool last_one;
61
62 /* libyang context for the run */
63 char *yang_lib_file;
64 uint16_t ctx_options;
65
66 /* prepared output (--output option or stdout by default) */
67 ly_bool out_stdout;
68 struct ly_out *out;
69
70 char *searchpaths;
71 ly_bool searchdir_unset;
72
73 /* options flags */
74 uint8_t list; /* -l option to print list of schemas */
75
76 /* line length for 'tree' format */
77 size_t line_length; /* --tree-line-length */
78
79 uint32_t dbg_groups;
80
81 /*
82 * schema
83 */
84 /* set schema modules' features via --features option (struct schema_features *) */
85 struct ly_set schema_features;
86
87 /* set of loaded schema modules (struct lys_module *) */
88 struct ly_set schema_modules;
89
90 /* options to parse and print schema modules */
91 uint32_t schema_parse_options;
92 uint32_t schema_print_options;
93
94 /* specification of printing schema node subtree, option --schema-node */
95 char *schema_node_path;
aPieceka83b8e02023-06-07 15:25:16 +020096 char *submodule;
97
98 /* name of file containing explicit context passed to callback
99 * for schema-mount extension. This also causes a callback to
100 * be registered.
101 */
102 char *schema_context_filename;
103 ly_bool extdata_unset;
104
105 /* value of --format in case of schema format */
106 LYS_OUTFORMAT schema_out_format;
107 ly_bool feature_param_format;
108 ly_bool feature_print_all;
aPieceka83b8e02023-06-07 15:25:16 +0200109
110 /*
111 * data
112 */
113 /* various options based on --type option */
114 enum lyd_type data_type;
115 uint32_t data_parse_options;
116 uint32_t data_validate_options;
117 uint32_t data_print_options;
118
119 /* flag for --merge option */
120 uint8_t data_merge;
121
122 /* value of --format in case of data format */
123 LYD_FORMAT data_out_format;
124
125 /* value of --in-format in case of data format */
126 LYD_FORMAT data_in_format;
127
128 /* input data files (struct cmdline_file *) */
129 struct ly_set data_inputs;
130
131 /* storage for --operational */
132 struct cmdline_file data_operational;
133
134 /* storage for --reply-rpc */
135 struct cmdline_file reply_rpc;
136
137 /* storage for --data-xpath */
138 struct ly_set data_xpath;
139
140 char **argv;
141};
142
143/**
144 * @brief Erase all values in @p opt.
145 *
146 * The yl_opt.interactive item is not deleted.
147 *
148 * @param[in,out] yo Option context to erase.
149 */
150void yl_opt_erase(struct yl_opt *yo);
151
152/**
aPiecek113e0f02023-06-09 08:47:48 +0200153 * @brief Update @p yo according to the @p arg of the schema --format parameter.
154 *
155 * @param[in] arg Format parameter argument (for example yang, yin, ...).
156 * @param[out] yo yanglint options used to update.
157 * @return 0 on success.
158 */
159int yl_opt_update_schema_out_format(const char *arg, struct yl_opt *yo);
160
161/**
162 * @brief Update @p yo according to the @p arg of the data --format parameter.
163 *
164 * @param[in] arg Format parameter argument (for example xml, json, ...).
165 * @param[out] yo yanglint options used to update.
166 * @return 0 on success.
167 */
168int yl_opt_update_data_out_format(const char *arg, struct yl_opt *yo);
169
170/**
171 * @brief Update @p yo according to the @p arg of the general --format parameter.
172 *
173 * @param[in] arg Format parameter argument (for example yang, xml, ...).
174 * @param[out] yo yanglint options used to update.
175 * @return 0 on success.
176 */
177int yl_opt_update_out_format(const char *arg, struct yl_opt *yo);
178
179/**
aPiecek3167f382023-06-09 09:23:10 +0200180 * @brief Update @p yo according to the @p arg of the data --type parameter.
181 *
182 * @param[in] arg Format parameter argument (for example config, rpc, ...).
183 * @param[out] yo yanglint options used to update.
184 * @return 0 on success.
185 */
186int yl_opt_update_data_type(const char *arg, struct yl_opt *yo);
187
188/**
aPiecek20cc2fe2023-06-09 09:32:28 +0200189 * @brief Update @p yo according to the @p arg of the data --default parameter.
190 *
191 * @param[in] arg Format parameter argument (for example all, trim, ...).
192 * @param[out] yo yanglint options used to update.
193 * @return 0 on success.
194 */
195int yo_opt_update_data_default(const char *arg, struct yl_opt *yo);
196
197/**
aPiecekb5dff492023-06-09 09:38:08 +0200198 * @brief Update @p yo according to the @p arg of the data --in-format parameter.
199 *
200 * @param[in] arg Format parameter argument (for example xml, json, ...).
201 * @param[out] yo yanglint options used to update.
202 * @return 0 on success.
203 */
204int yo_opt_update_data_in_format(const char *arg, struct yl_opt *yo);
205
206/**
aPiecek7f22c102023-06-09 09:48:44 +0200207 * @brief Update @p yo according to the --make-implemented parameter.
208 *
209 * @param[in,out] yo yanglint options used to update.
210 */
211void yo_opt_update_make_implemented(struct yl_opt *yo);
212
213/**
aPiecek88ae15e2023-06-09 10:20:17 +0200214 * @brief Update @p yo according to the --disable-searchdir parameter.
215 *
216 * @param[in,out] yo yanglint options used to update.
217 */
218void yo_opt_update_disable_searchdir(struct yl_opt *yo);
219
220/**
aPieceka83b8e02023-06-07 15:25:16 +0200221 * @brief Helper function to prepare argc, argv pair from a command line string.
222 *
223 * @param[in] cmdline Complete command line string.
224 * @param[out] argc_p Pointer to store argc value.
225 * @param[out] argv_p Pointer to store argv vector.
226 * @return 0 on success, non-zero on failure.
227 */
228int parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]);
229
230/**
231 * @brief Destructor for the argument vector prepared by ::parse_cmdline().
232 *
233 * @param[in,out] argv Argument vector to destroy.
234 */
235void free_cmdline(char *argv[]);
236
237#endif /* YL_OPT_H_ */