Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd.h |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 5 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 6 | * @brief libyang's yanglint tool commands header |
| 7 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 8 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 9 | * |
| 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 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 22 | struct yl_opt; |
| 23 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 24 | /** |
| 25 | * @brief command information |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 26 | * |
| 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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 39 | */ |
| 40 | typedef struct { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 41 | /* User printable name of the function. */ |
| 42 | char *name; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 43 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 44 | /* 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 Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 60 | } COMMAND; |
| 61 | |
| 62 | /** |
| 63 | * @brief The list of available commands. |
| 64 | */ |
| 65 | extern COMMAND commands[]; |
| 66 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 67 | /** |
| 68 | * @brief Index for global variable ::commands. |
| 69 | */ |
| 70 | enum 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, |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 79 | CMD_EXTDATA, |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 80 | CMD_CLEAR, |
| 81 | CMD_VERB, |
Michal Vasko | 07d1b65 | 2024-01-16 15:38:17 +0100 | [diff] [blame] | 82 | CMD_DEBUG |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 85 | /** |
| 86 | * @brief For each cmd, call the COMMAND.free_func in the variable 'commands'. |
| 87 | */ |
| 88 | void cmd_free(void); |
| 89 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 90 | /* cmd_add.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 91 | |
| 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 | */ |
| 101 | int 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 | */ |
| 110 | int 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 | */ |
| 120 | int cmd_add_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 121 | void cmd_add_help(void); |
| 122 | |
| 123 | /* cmd_clear.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * @copydoc cmd_add_opt |
| 127 | */ |
| 128 | int cmd_clear_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 129 | |
| 130 | /** |
| 131 | * @copydoc cmd_add_dep |
| 132 | */ |
| 133 | int 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 | */ |
| 143 | int cmd_clear_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 144 | void cmd_clear_help(void); |
| 145 | |
| 146 | /* cmd_data.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * @copydoc cmd_add_opt |
| 150 | */ |
| 151 | int cmd_data_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 152 | |
| 153 | /** |
| 154 | * @copydoc cmd_add_dep |
| 155 | */ |
| 156 | int 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 | */ |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 166 | int cmd_data_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 167 | |
| 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 | */ |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 175 | int cmd_data_process(struct ly_ctx *ctx, struct yl_opt *yo); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 176 | void cmd_data_help(void); |
| 177 | |
| 178 | /* cmd_list.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * @copydoc cmd_add_opt |
| 182 | */ |
| 183 | int cmd_list_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 184 | |
| 185 | /** |
| 186 | * @copydoc cmd_add_dep |
| 187 | */ |
| 188 | int 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 | */ |
| 198 | int cmd_list_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 199 | void cmd_list_help(void); |
| 200 | |
Michal Vasko | 538be42 | 2021-10-19 14:06:59 +0200 | [diff] [blame] | 201 | /* cmd_feature.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * @copydoc cmd_add_opt |
| 205 | */ |
| 206 | int cmd_feature_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 207 | |
| 208 | /** |
| 209 | * @copydoc cmd_add_dep |
| 210 | */ |
| 211 | int 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 | */ |
| 221 | int cmd_feature_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
| 222 | |
| 223 | /** |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 224 | * @brief Printing of features ends. |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 225 | * |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 226 | * @param[in] ctx context for libyang. Not used. |
| 227 | * @param[in] yo context for yanglint. |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 228 | * @return 0 on success. |
| 229 | */ |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 230 | int cmd_feature_fin(struct ly_ctx *ctx, struct yl_opt *yo); |
Michal Vasko | 538be42 | 2021-10-19 14:06:59 +0200 | [diff] [blame] | 231 | void cmd_feature_help(void); |
| 232 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 233 | /* cmd_load.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * @copydoc cmd_add_opt |
| 237 | */ |
| 238 | int cmd_load_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 239 | |
| 240 | /** |
| 241 | * @copydoc cmd_add_dep |
| 242 | */ |
| 243 | int 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 | */ |
| 253 | int cmd_load_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 254 | void cmd_load_help(void); |
| 255 | |
| 256 | /* cmd_print.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * @copydoc cmd_add_opt |
| 260 | */ |
| 261 | int cmd_print_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 262 | |
| 263 | /** |
| 264 | * @copydoc cmd_add_dep |
| 265 | */ |
| 266 | int 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 | */ |
| 276 | int cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 277 | void cmd_print_help(void); |
| 278 | |
| 279 | /* cmd_searchpath.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * @copydoc cmd_add_opt |
| 283 | */ |
| 284 | int 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 | */ |
| 294 | int cmd_searchpath_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 295 | void cmd_searchpath_help(void); |
| 296 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 297 | /* cmd_extdata.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 298 | |
| 299 | /** |
| 300 | * @copydoc cmd_add_opt |
| 301 | */ |
| 302 | int cmd_extdata_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 303 | |
| 304 | /** |
| 305 | * @copydoc cmd_add_dep |
| 306 | */ |
| 307 | int 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 | */ |
| 317 | int cmd_extdata_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 318 | void cmd_extdata_help(void); |
| 319 | void cmd_extdata_free(void); |
| 320 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 321 | /* cmd_help.c */ |
| 322 | |
| 323 | /** |
| 324 | * @copydoc cmd_add_opt |
| 325 | */ |
| 326 | int 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 | */ |
| 336 | int cmd_help_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
| 337 | void cmd_help_help(void); |
| 338 | |
| 339 | /* cmd_verb.c */ |
| 340 | |
| 341 | /** |
| 342 | * @copydoc cmd_add_opt |
| 343 | */ |
| 344 | int cmd_verb_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 345 | |
| 346 | /** |
| 347 | * @copydoc cmd_add_dep |
| 348 | */ |
| 349 | int 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 | */ |
| 359 | int cmd_verb_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
| 360 | void cmd_verb_help(void); |
| 361 | |
| 362 | /* cmd_debug.c */ |
| 363 | |
| 364 | /** |
| 365 | * @copydoc cmd_add_opt |
| 366 | */ |
| 367 | int cmd_debug_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 368 | |
| 369 | /** |
| 370 | * @copydoc cmd_add_dep |
| 371 | */ |
| 372 | int 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 | */ |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 382 | int cmd_debug_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 383 | |
| 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 | */ |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 391 | int cmd_debug_setlog(struct ly_ctx *ctx, struct yl_opt *yo); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 392 | void cmd_debug_help(void); |
| 393 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 394 | #endif /* COMMANDS_H_ */ |