blob: 9cf816dbbb85168c3c2d95eec3b1cee8d854b506 [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @author Radek Krejci <rkrejci@cesnet.cz>
aPieceka83b8e02023-06-07 15:25:16 +02005 * @author Adam Piecek <piecek@cesnet.cz>
Radek Krejcie9f13b12020-11-09 17:42:04 +01006 * @brief libyang's yanglint tool commands header
7 *
aPieceka83b8e02023-06-07 15:25:16 +02008 * Copyright (c) 2015-2023 CESNET, z.s.p.o.
Radek Krejcie9f13b12020-11-09 17:42:04 +01009 *
10 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * https://opensource.org/licenses/BSD-3-Clause
15 */
16
17#ifndef COMMANDS_H_
18#define COMMANDS_H_
19
20#include "libyang.h"
21
aPieceka83b8e02023-06-07 15:25:16 +020022struct yl_opt;
23
Radek Krejcie9f13b12020-11-09 17:42:04 +010024/**
25 * @brief command information
aPieceka83b8e02023-06-07 15:25:16 +020026 *
27 * Callback functions are in the order they should be called.
28 * First, the 'opt_func' should be called, which parses arguments from the command line and sets flags or pointers in
29 * the struct yl_opt. This type of function is for interactive mode and is optional.
30 * Then the 'dep_func' callback can check the struct yl_opt settings. Other items that depend on them can also be
31 * set. There is also an possibility for controlling the number of positional arguments and its implications.
32 * The most important callback is 'exec_func' where the command itself is executed. This function can even replace the
33 * entire libyang context. The function parameters are mainly found in the yl_opt structure. Optionally, the function
34 * can be called with a positional argument obtained from the command line. Some 'exec_func' are adapted to be called
35 * from non-interactive yanglint mode.
36 * The 'fun_func' complements the 'exec_func'. In some cases, the command execution must be divided into two stages.
37 * For example, the 'exec_func' is used to fill some items in the yl_opt structure from the positional
38 * arguments and then the 'fin_func' is used to perform the final action.
Radek Krejcie9f13b12020-11-09 17:42:04 +010039 */
40typedef struct {
aPieceka83b8e02023-06-07 15:25:16 +020041 /* User printable name of the function. */
42 char *name;
Michal Vasko26bbb272022-08-02 14:54:33 +020043
aPieceka83b8e02023-06-07 15:25:16 +020044 /* Convert command line options to the data struct yl_opt. */
45 int (*opt_func)(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
46 /* Additionally set dependent items and perform error checking. */
47 int (*dep_func)(struct yl_opt *yo, int posc);
48 /* Execute command. */
49 int (*exec_func)(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
50 /* Finish execution of command. */
51 int (*fin_func)(struct ly_ctx *ctx, struct yl_opt *yo);
52 /* Display command help. */
53 void (*help_func)(void);
54 /* Freeing global variables allocated by the command. */
55 void (*free_func)(void);
56 /* Documentation for this function. */
57 char *helpstring;
58 /* Option characters used in function getopt_long. */
59 char *optstring;
Radek Krejcie9f13b12020-11-09 17:42:04 +010060} COMMAND;
61
62/**
63 * @brief The list of available commands.
64 */
65extern COMMAND commands[];
66
aPiecek15cc4cf2023-04-17 15:23:21 +020067/**
68 * @brief Index for global variable ::commands.
69 */
70enum COMMAND_INDEX {
71 CMD_HELP = 0,
72 CMD_ADD,
73 CMD_LOAD,
74 CMD_PRINT,
75 CMD_DATA,
76 CMD_LIST,
77 CMD_FEATURE,
78 CMD_SEARCHPATH,
aPiecek647f62e2023-05-18 10:55:58 +020079 CMD_EXTDATA,
aPiecek15cc4cf2023-04-17 15:23:21 +020080 CMD_CLEAR,
81 CMD_VERB,
82#ifndef NDEBUG
83 CMD_DEBUG,
84#endif
85};
86
aPiecek647f62e2023-05-18 10:55:58 +020087/**
88 * @brief For each cmd, call the COMMAND.free_func in the variable 'commands'.
89 */
90void cmd_free(void);
91
Radek Krejcie9f13b12020-11-09 17:42:04 +010092/* cmd_add.c */
aPieceka83b8e02023-06-07 15:25:16 +020093
94/**
95 * @brief Parse the arguments of an interactive command.
96 *
97 * @param[out] yo Context for yanglint.
98 * @param[in] cmdline String containing command line arguments.
99 * @param[out] posv Pointer to argv to a section of positional arguments.
100 * @param[out] posc Number of positional arguments.
101 * @return 0 on success.
102 */
103int cmd_add_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
104
105/**
106 * @brief Check the options and set dependent items in @p yo.
107 *
108 * @param[in,out] yo context for yanglint.
109 * @param[in] posc number of positional arguments.
110 * @return 0 on success.
111 */
112int cmd_add_dep(struct yl_opt *yo, int posc);
113
114/**
115 * @brief Parse and compile a new module using filepath.
116 *
117 * @param[in,out] ctx Context for libyang.
118 * @param[in,out] yo Context for yanglint.
119 * @param[in] posv Path to the file where the new module is located.
120 * @return 0 on success.
121 */
122int cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100123void cmd_add_help(void);
124
125/* cmd_clear.c */
aPieceka83b8e02023-06-07 15:25:16 +0200126
127/**
128 * @copydoc cmd_add_opt
129 */
130int cmd_clear_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
131
132/**
133 * @copydoc cmd_add_dep
134 */
135int cmd_clear_dep(struct yl_opt *yo, int posc);
136
137/**
138 * @brief Clear libyang context.
139 *
140 * @param[in,out] ctx context for libyang that will be replaced with an empty one.
141 * @param[in,out] yo context for yanglint.
142 * @param[in] posv not used.
143 * @return 0 on success.
144 */
145int cmd_clear_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100146void cmd_clear_help(void);
147
148/* cmd_data.c */
aPieceka83b8e02023-06-07 15:25:16 +0200149
150/**
151 * @copydoc cmd_add_opt
152 */
153int cmd_data_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
154
155/**
156 * @copydoc cmd_add_dep
157 */
158int cmd_data_dep(struct yl_opt *yo, int posc);
159
160/**
161 * @brief Store data file for later processing.
162 *
163 * @param[in,out] ctx context for libyang.
164 * @param[in,out] yo context for yanglint.
165 * @param[in] posv Path to the file where the data is located.
166 * @return 0 on success.
167 */
168int cmd_data_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
169
170/**
171 * @brief Parse, validate and optionally print data instances.
172 *
173 * @param[in] ctx Context for libyang.
174 * @param[in] yo Context of yanglint. All necessary parameters should already be set.
175 * @return 0 on success.
176 */
177int cmd_data_fin(struct ly_ctx *ctx, struct yl_opt *yo);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100178void cmd_data_help(void);
179
180/* cmd_list.c */
aPieceka83b8e02023-06-07 15:25:16 +0200181
182/**
183 * @copydoc cmd_add_opt
184 */
185int cmd_list_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
186
187/**
188 * @copydoc cmd_add_dep
189 */
190int cmd_list_dep(struct yl_opt *yo, int posc);
191
192/**
193 * @brief Print the list of modules in the current context.
194 *
195 * @param[in,out] ctx context for libyang.
196 * @param[in,out] yo context for yanglint.
197 * @param[in] posv Not used.
198 * @return 0 on success.
199 */
200int cmd_list_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100201void cmd_list_help(void);
202
Michal Vasko538be422021-10-19 14:06:59 +0200203/* cmd_feature.c */
aPieceka83b8e02023-06-07 15:25:16 +0200204
205/**
206 * @copydoc cmd_add_opt
207 */
208int cmd_feature_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
209
210/**
211 * @copydoc cmd_add_dep
212 */
213int cmd_feature_dep(struct yl_opt *yo, int posc);
214
215/**
216 * @brief Print the features the modules.
217 *
218 * @param[in,out] ctx context for libyang.
219 * @param[in,out] yo context for yanglint.
220 * @param[in] posv Name of the module which features are to be printed.
221 * @return 0 on success.
222 */
223int cmd_feature_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
224
225/**
226 * @brief Generate features for the command 'add'.
227 *
228 * @param[in,out] ctx context for libyang.
229 * @param[in,out] yo context for yanglint. All necessary parameters should already be set.
230 * @return 0 on success.
231 */
232int cmd_feature_fin(struct ly_ctx *ctx, struct yl_opt *yo);
Michal Vasko538be422021-10-19 14:06:59 +0200233void cmd_feature_help(void);
234
Radek Krejcie9f13b12020-11-09 17:42:04 +0100235/* cmd_load.c */
aPieceka83b8e02023-06-07 15:25:16 +0200236
237/**
238 * @copydoc cmd_add_opt
239 */
240int cmd_load_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
241
242/**
243 * @copydoc cmd_add_dep
244 */
245int cmd_load_dep(struct yl_opt *yo, int posc);
246
247/**
248 * @brief Parse and compile a new module using module name.
249 *
250 * @param[in,out] ctx context for libyang.
251 * @param[in,out] yo context for yanglint.
252 * @param[in] posv Name of the module to be loaded into the context.
253 * @return 0 on success.
254 */
255int cmd_load_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100256void cmd_load_help(void);
257
258/* cmd_print.c */
aPieceka83b8e02023-06-07 15:25:16 +0200259
260/**
261 * @copydoc cmd_add_opt
262 */
263int cmd_print_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
264
265/**
266 * @copydoc cmd_add_dep
267 */
268int cmd_print_dep(struct yl_opt *yo, int posc);
269
270/**
271 * @brief Print a schema module.
272 *
273 * @param[in,out] ctx context for libyang.
274 * @param[in,out] yo context for yanglint.
275 * @param[in] posv Name of the module to be printed. Can be NULL in the case of printing a node.
276 * @return 0 on success.
277 */
278int cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100279void cmd_print_help(void);
280
281/* cmd_searchpath.c */
aPieceka83b8e02023-06-07 15:25:16 +0200282
283/**
284 * @copydoc cmd_add_opt
285 */
286int cmd_searchpath_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
287
288/**
289 * @brief Set the paths of directories where to search schema modules.
290 *
291 * @param[in,out] ctx context for libyang.
292 * @param[in,out] yo context for yanglint.
293 * @param[in] posv Path to the directory. Can be NULL in the case of printing a current searchdirs.
294 * @return 0 on success.
295 */
296int cmd_searchpath_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100297void cmd_searchpath_help(void);
298
aPiecek647f62e2023-05-18 10:55:58 +0200299/* cmd_extdata.c */
aPieceka83b8e02023-06-07 15:25:16 +0200300
301/**
302 * @copydoc cmd_add_opt
303 */
304int cmd_extdata_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
305
306/**
307 * @copydoc cmd_add_dep
308 */
309int cmd_extdata_dep(struct yl_opt *yo, int posc);
310
311/**
312 * @brief Set path to the file required by the extension.
313 *
314 * @param[in,out] ctx context for libyang.
315 * @param[in,out] yo context for yanglint.
316 * @param[in] posv Path to the directory. Can be NULL in the case of printing a current path.
317 * @return 0 on success.
318 */
319int cmd_extdata_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
aPiecek647f62e2023-05-18 10:55:58 +0200320void cmd_extdata_help(void);
321void cmd_extdata_free(void);
322
aPieceka83b8e02023-06-07 15:25:16 +0200323/* cmd_help.c */
324
325/**
326 * @copydoc cmd_add_opt
327 */
328int cmd_help_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
329
330/**
331 * @brief Print help.
332 *
333 * @param[in,out] ctx context for libyang.
334 * @param[in,out] yo context for yanglint.
335 * @param[in] posv Name of the command which help message is to be printed. Can be NULL.
336 * @return 0 on success.
337 */
338int cmd_help_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
339void cmd_help_help(void);
340
341/* cmd_verb.c */
342
343/**
344 * @copydoc cmd_add_opt
345 */
346int cmd_verb_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
347
348/**
349 * @copydoc cmd_add_dep
350 */
351int cmd_verb_dep(struct yl_opt *yo, int posc);
352
353/**
354 * @brief Set the verbose level.
355 *
356 * @param[in,out] ctx context for libyang.
357 * @param[in,out] yo context for yanglint.
358 * @param[in] posv Name of the verbose level to be set.
359 * @return 0 on success.
360 */
361int cmd_verb_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
362void cmd_verb_help(void);
363
364/* cmd_debug.c */
365
366/**
367 * @copydoc cmd_add_opt
368 */
369int cmd_debug_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
370
371/**
372 * @copydoc cmd_add_dep
373 */
374int cmd_debug_dep(struct yl_opt *yo, int posc);
375
376/**
377 * @brief Store the type of debug messages for later processing.
378 *
379 * @param[in,out] ctx context for libyang.
380 * @param[in,out] yo context for yanglint.
381 * @param[in] posv Name of the debug type to be set.
382 * @return 0 on success.
383 */
384int cmd_debug_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
385
386/**
387 * @brief Set debug logging.
388 *
389 * @param[in,out] ctx context for libyang.
390 * @param[in,out] yo context for yanglint. All necessary parameters should already be set.
391 * @return 0 on success.
392 */
393int cmd_debug_fin(struct ly_ctx *ctx, struct yl_opt *yo);
394void cmd_debug_help(void);
395
Radek Krejcie9f13b12020-11-09 17:42:04 +0100396#endif /* COMMANDS_H_ */