blob: 82a41729268f0cb2e55d49f51763352246647a7f [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;
96 const struct lysc_node *schema_node;
97 char *submodule;
98
99 /* name of file containing explicit context passed to callback
100 * for schema-mount extension. This also causes a callback to
101 * be registered.
102 */
103 char *schema_context_filename;
104 ly_bool extdata_unset;
105
106 /* value of --format in case of schema format */
107 LYS_OUTFORMAT schema_out_format;
108 ly_bool feature_param_format;
109 ly_bool feature_print_all;
110 char *features_output;
111
112 /*
113 * data
114 */
115 /* various options based on --type option */
116 enum lyd_type data_type;
117 uint32_t data_parse_options;
118 uint32_t data_validate_options;
119 uint32_t data_print_options;
120
121 /* flag for --merge option */
122 uint8_t data_merge;
123
124 /* value of --format in case of data format */
125 LYD_FORMAT data_out_format;
126
127 /* value of --in-format in case of data format */
128 LYD_FORMAT data_in_format;
129
130 /* input data files (struct cmdline_file *) */
131 struct ly_set data_inputs;
132
133 /* storage for --operational */
134 struct cmdline_file data_operational;
135
136 /* storage for --reply-rpc */
137 struct cmdline_file reply_rpc;
138
139 /* storage for --data-xpath */
140 struct ly_set data_xpath;
141
142 char **argv;
143};
144
145/**
146 * @brief Erase all values in @p opt.
147 *
148 * The yl_opt.interactive item is not deleted.
149 *
150 * @param[in,out] yo Option context to erase.
151 */
152void yl_opt_erase(struct yl_opt *yo);
153
154/**
aPiecek113e0f02023-06-09 08:47:48 +0200155 * @brief Update @p yo according to the @p arg of the schema --format parameter.
156 *
157 * @param[in] arg Format parameter argument (for example yang, yin, ...).
158 * @param[out] yo yanglint options used to update.
159 * @return 0 on success.
160 */
161int yl_opt_update_schema_out_format(const char *arg, struct yl_opt *yo);
162
163/**
164 * @brief Update @p yo according to the @p arg of the data --format parameter.
165 *
166 * @param[in] arg Format parameter argument (for example xml, json, ...).
167 * @param[out] yo yanglint options used to update.
168 * @return 0 on success.
169 */
170int yl_opt_update_data_out_format(const char *arg, struct yl_opt *yo);
171
172/**
173 * @brief Update @p yo according to the @p arg of the general --format parameter.
174 *
175 * @param[in] arg Format parameter argument (for example yang, xml, ...).
176 * @param[out] yo yanglint options used to update.
177 * @return 0 on success.
178 */
179int yl_opt_update_out_format(const char *arg, struct yl_opt *yo);
180
181/**
aPiecek3167f382023-06-09 09:23:10 +0200182 * @brief Update @p yo according to the @p arg of the data --type parameter.
183 *
184 * @param[in] arg Format parameter argument (for example config, rpc, ...).
185 * @param[out] yo yanglint options used to update.
186 * @return 0 on success.
187 */
188int yl_opt_update_data_type(const char *arg, struct yl_opt *yo);
189
190/**
aPiecek20cc2fe2023-06-09 09:32:28 +0200191 * @brief Update @p yo according to the @p arg of the data --default parameter.
192 *
193 * @param[in] arg Format parameter argument (for example all, trim, ...).
194 * @param[out] yo yanglint options used to update.
195 * @return 0 on success.
196 */
197int yo_opt_update_data_default(const char *arg, struct yl_opt *yo);
198
199/**
aPieceka83b8e02023-06-07 15:25:16 +0200200 * @brief Helper function to prepare argc, argv pair from a command line string.
201 *
202 * @param[in] cmdline Complete command line string.
203 * @param[out] argc_p Pointer to store argc value.
204 * @param[out] argv_p Pointer to store argv vector.
205 * @return 0 on success, non-zero on failure.
206 */
207int parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]);
208
209/**
210 * @brief Destructor for the argument vector prepared by ::parse_cmdline().
211 *
212 * @param[in,out] argv Argument vector to destroy.
213 */
214void free_cmdline(char *argv[]);
215
216#endif /* YL_OPT_H_ */