blob: e9ee5b4920e0d175eff9c4fa3d300c55411c6000 [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,
Michal Vasko07d1b652024-01-16 15:38:17 +010082 CMD_DEBUG
aPiecek15cc4cf2023-04-17 15:23:21 +020083};
84
aPiecek647f62e2023-05-18 10:55:58 +020085/**
86 * @brief For each cmd, call the COMMAND.free_func in the variable 'commands'.
87 */
88void cmd_free(void);
89
Radek Krejcie9f13b12020-11-09 17:42:04 +010090/* cmd_add.c */
aPieceka83b8e02023-06-07 15:25:16 +020091
92/**
93 * @brief Parse the arguments of an interactive command.
94 *
95 * @param[out] yo Context for yanglint.
96 * @param[in] cmdline String containing command line arguments.
97 * @param[out] posv Pointer to argv to a section of positional arguments.
98 * @param[out] posc Number of positional arguments.
99 * @return 0 on success.
100 */
101int cmd_add_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
102
103/**
104 * @brief Check the options and set dependent items in @p yo.
105 *
106 * @param[in,out] yo context for yanglint.
107 * @param[in] posc number of positional arguments.
108 * @return 0 on success.
109 */
110int cmd_add_dep(struct yl_opt *yo, int posc);
111
112/**
113 * @brief Parse and compile a new module using filepath.
114 *
115 * @param[in,out] ctx Context for libyang.
116 * @param[in,out] yo Context for yanglint.
117 * @param[in] posv Path to the file where the new module is located.
118 * @return 0 on success.
119 */
120int cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100121void cmd_add_help(void);
122
123/* cmd_clear.c */
aPieceka83b8e02023-06-07 15:25:16 +0200124
125/**
126 * @copydoc cmd_add_opt
127 */
128int cmd_clear_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
129
130/**
131 * @copydoc cmd_add_dep
132 */
133int cmd_clear_dep(struct yl_opt *yo, int posc);
134
135/**
136 * @brief Clear libyang context.
137 *
138 * @param[in,out] ctx context for libyang that will be replaced with an empty one.
139 * @param[in,out] yo context for yanglint.
140 * @param[in] posv not used.
141 * @return 0 on success.
142 */
143int cmd_clear_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100144void cmd_clear_help(void);
145
146/* cmd_data.c */
aPieceka83b8e02023-06-07 15:25:16 +0200147
148/**
149 * @copydoc cmd_add_opt
150 */
151int cmd_data_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
152
153/**
154 * @copydoc cmd_add_dep
155 */
156int cmd_data_dep(struct yl_opt *yo, int posc);
157
158/**
159 * @brief Store data file for later processing.
160 *
161 * @param[in,out] ctx context for libyang.
162 * @param[in,out] yo context for yanglint.
163 * @param[in] posv Path to the file where the data is located.
164 * @return 0 on success.
165 */
aPiecekcf9e7682023-06-19 14:03:50 +0200166int cmd_data_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
aPieceka83b8e02023-06-07 15:25:16 +0200167
168/**
169 * @brief Parse, validate and optionally print data instances.
170 *
171 * @param[in] ctx Context for libyang.
172 * @param[in] yo Context of yanglint. All necessary parameters should already be set.
173 * @return 0 on success.
174 */
aPiecekcf9e7682023-06-19 14:03:50 +0200175int cmd_data_process(struct ly_ctx *ctx, struct yl_opt *yo);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100176void cmd_data_help(void);
177
178/* cmd_list.c */
aPieceka83b8e02023-06-07 15:25:16 +0200179
180/**
181 * @copydoc cmd_add_opt
182 */
183int cmd_list_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
184
185/**
186 * @copydoc cmd_add_dep
187 */
188int cmd_list_dep(struct yl_opt *yo, int posc);
189
190/**
191 * @brief Print the list of modules in the current context.
192 *
193 * @param[in,out] ctx context for libyang.
194 * @param[in,out] yo context for yanglint.
195 * @param[in] posv Not used.
196 * @return 0 on success.
197 */
198int cmd_list_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100199void cmd_list_help(void);
200
Michal Vasko538be422021-10-19 14:06:59 +0200201/* cmd_feature.c */
aPieceka83b8e02023-06-07 15:25:16 +0200202
203/**
204 * @copydoc cmd_add_opt
205 */
206int cmd_feature_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
207
208/**
209 * @copydoc cmd_add_dep
210 */
211int cmd_feature_dep(struct yl_opt *yo, int posc);
212
213/**
214 * @brief Print the features the modules.
215 *
216 * @param[in,out] ctx context for libyang.
217 * @param[in,out] yo context for yanglint.
218 * @param[in] posv Name of the module which features are to be printed.
219 * @return 0 on success.
220 */
221int cmd_feature_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
222
223/**
aPiecek6fde6d02023-06-21 15:06:45 +0200224 * @brief Printing of features ends.
aPieceka83b8e02023-06-07 15:25:16 +0200225 *
aPiecek6fde6d02023-06-21 15:06:45 +0200226 * @param[in] ctx context for libyang. Not used.
227 * @param[in] yo context for yanglint.
aPieceka83b8e02023-06-07 15:25:16 +0200228 * @return 0 on success.
229 */
aPiecek6fde6d02023-06-21 15:06:45 +0200230int cmd_feature_fin(struct ly_ctx *ctx, struct yl_opt *yo);
Michal Vasko538be422021-10-19 14:06:59 +0200231void cmd_feature_help(void);
232
Radek Krejcie9f13b12020-11-09 17:42:04 +0100233/* cmd_load.c */
aPieceka83b8e02023-06-07 15:25:16 +0200234
235/**
236 * @copydoc cmd_add_opt
237 */
238int cmd_load_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
239
240/**
241 * @copydoc cmd_add_dep
242 */
243int cmd_load_dep(struct yl_opt *yo, int posc);
244
245/**
246 * @brief Parse and compile a new module using module name.
247 *
248 * @param[in,out] ctx context for libyang.
249 * @param[in,out] yo context for yanglint.
250 * @param[in] posv Name of the module to be loaded into the context.
251 * @return 0 on success.
252 */
253int cmd_load_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100254void cmd_load_help(void);
255
256/* cmd_print.c */
aPieceka83b8e02023-06-07 15:25:16 +0200257
258/**
259 * @copydoc cmd_add_opt
260 */
261int cmd_print_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
262
263/**
264 * @copydoc cmd_add_dep
265 */
266int cmd_print_dep(struct yl_opt *yo, int posc);
267
268/**
269 * @brief Print a schema module.
270 *
271 * @param[in,out] ctx context for libyang.
272 * @param[in,out] yo context for yanglint.
273 * @param[in] posv Name of the module to be printed. Can be NULL in the case of printing a node.
274 * @return 0 on success.
275 */
276int cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100277void cmd_print_help(void);
278
279/* cmd_searchpath.c */
aPieceka83b8e02023-06-07 15:25:16 +0200280
281/**
282 * @copydoc cmd_add_opt
283 */
284int cmd_searchpath_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
285
286/**
287 * @brief Set the paths of directories where to search schema modules.
288 *
289 * @param[in,out] ctx context for libyang.
290 * @param[in,out] yo context for yanglint.
291 * @param[in] posv Path to the directory. Can be NULL in the case of printing a current searchdirs.
292 * @return 0 on success.
293 */
294int cmd_searchpath_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100295void cmd_searchpath_help(void);
296
aPiecek647f62e2023-05-18 10:55:58 +0200297/* cmd_extdata.c */
aPieceka83b8e02023-06-07 15:25:16 +0200298
299/**
300 * @copydoc cmd_add_opt
301 */
302int cmd_extdata_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
303
304/**
305 * @copydoc cmd_add_dep
306 */
307int cmd_extdata_dep(struct yl_opt *yo, int posc);
308
309/**
310 * @brief Set path to the file required by the extension.
311 *
312 * @param[in,out] ctx context for libyang.
313 * @param[in,out] yo context for yanglint.
314 * @param[in] posv Path to the directory. Can be NULL in the case of printing a current path.
315 * @return 0 on success.
316 */
317int cmd_extdata_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
aPiecek647f62e2023-05-18 10:55:58 +0200318void cmd_extdata_help(void);
319void cmd_extdata_free(void);
320
aPieceka83b8e02023-06-07 15:25:16 +0200321/* cmd_help.c */
322
323/**
324 * @copydoc cmd_add_opt
325 */
326int cmd_help_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
327
328/**
329 * @brief Print help.
330 *
331 * @param[in,out] ctx context for libyang.
332 * @param[in,out] yo context for yanglint.
333 * @param[in] posv Name of the command which help message is to be printed. Can be NULL.
334 * @return 0 on success.
335 */
336int cmd_help_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
337void cmd_help_help(void);
338
339/* cmd_verb.c */
340
341/**
342 * @copydoc cmd_add_opt
343 */
344int cmd_verb_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
345
346/**
347 * @copydoc cmd_add_dep
348 */
349int cmd_verb_dep(struct yl_opt *yo, int posc);
350
351/**
352 * @brief Set the verbose level.
353 *
354 * @param[in,out] ctx context for libyang.
355 * @param[in,out] yo context for yanglint.
356 * @param[in] posv Name of the verbose level to be set.
357 * @return 0 on success.
358 */
359int cmd_verb_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
360void cmd_verb_help(void);
361
362/* cmd_debug.c */
363
364/**
365 * @copydoc cmd_add_opt
366 */
367int cmd_debug_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc);
368
369/**
370 * @copydoc cmd_add_dep
371 */
372int cmd_debug_dep(struct yl_opt *yo, int posc);
373
374/**
375 * @brief Store the type of debug messages for later processing.
376 *
377 * @param[in,out] ctx context for libyang.
378 * @param[in,out] yo context for yanglint.
379 * @param[in] posv Name of the debug type to be set.
380 * @return 0 on success.
381 */
aPiecekcf9e7682023-06-19 14:03:50 +0200382int cmd_debug_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv);
aPieceka83b8e02023-06-07 15:25:16 +0200383
384/**
385 * @brief Set debug logging.
386 *
387 * @param[in,out] ctx context for libyang.
388 * @param[in,out] yo context for yanglint. All necessary parameters should already be set.
389 * @return 0 on success.
390 */
aPiecekcf9e7682023-06-19 14:03:50 +0200391int cmd_debug_setlog(struct ly_ctx *ctx, struct yl_opt *yo);
aPieceka83b8e02023-06-07 15:25:16 +0200392void cmd_debug_help(void);
393
Radek Krejcie9f13b12020-11-09 17:42:04 +0100394#endif /* COMMANDS_H_ */