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, |
| 82 | #ifndef NDEBUG |
| 83 | CMD_DEBUG, |
| 84 | #endif |
| 85 | }; |
| 86 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 87 | /** |
| 88 | * @brief For each cmd, call the COMMAND.free_func in the variable 'commands'. |
| 89 | */ |
| 90 | void cmd_free(void); |
| 91 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 92 | /* cmd_add.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 93 | |
| 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 | */ |
| 103 | int 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 | */ |
| 112 | int 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 | */ |
| 122 | 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] | 123 | void cmd_add_help(void); |
| 124 | |
| 125 | /* cmd_clear.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * @copydoc cmd_add_opt |
| 129 | */ |
| 130 | int cmd_clear_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 131 | |
| 132 | /** |
| 133 | * @copydoc cmd_add_dep |
| 134 | */ |
| 135 | int 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 | */ |
| 145 | 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] | 146 | void cmd_clear_help(void); |
| 147 | |
| 148 | /* cmd_data.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * @copydoc cmd_add_opt |
| 152 | */ |
| 153 | int cmd_data_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 154 | |
| 155 | /** |
| 156 | * @copydoc cmd_add_dep |
| 157 | */ |
| 158 | int 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 | */ |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 168 | 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] | 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 | */ |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 177 | int cmd_data_process(struct ly_ctx *ctx, struct yl_opt *yo); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 178 | void cmd_data_help(void); |
| 179 | |
| 180 | /* cmd_list.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * @copydoc cmd_add_opt |
| 184 | */ |
| 185 | int cmd_list_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 186 | |
| 187 | /** |
| 188 | * @copydoc cmd_add_dep |
| 189 | */ |
| 190 | int 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 | */ |
| 200 | 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] | 201 | void cmd_list_help(void); |
| 202 | |
Michal Vasko | 538be42 | 2021-10-19 14:06:59 +0200 | [diff] [blame] | 203 | /* cmd_feature.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * @copydoc cmd_add_opt |
| 207 | */ |
| 208 | int cmd_feature_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 209 | |
| 210 | /** |
| 211 | * @copydoc cmd_add_dep |
| 212 | */ |
| 213 | int 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 | */ |
| 223 | int cmd_feature_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
| 224 | |
| 225 | /** |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 226 | * @brief Printing of features ends. |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 227 | * |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 228 | * @param[in] ctx context for libyang. Not used. |
| 229 | * @param[in] yo context for yanglint. |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 230 | * @return 0 on success. |
| 231 | */ |
aPiecek | 6fde6d0 | 2023-06-21 15:06:45 +0200 | [diff] [blame] | 232 | int cmd_feature_fin(struct ly_ctx *ctx, struct yl_opt *yo); |
Michal Vasko | 538be42 | 2021-10-19 14:06:59 +0200 | [diff] [blame] | 233 | void cmd_feature_help(void); |
| 234 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 235 | /* cmd_load.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * @copydoc cmd_add_opt |
| 239 | */ |
| 240 | int cmd_load_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 241 | |
| 242 | /** |
| 243 | * @copydoc cmd_add_dep |
| 244 | */ |
| 245 | int 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 | */ |
| 255 | 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] | 256 | void cmd_load_help(void); |
| 257 | |
| 258 | /* cmd_print.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * @copydoc cmd_add_opt |
| 262 | */ |
| 263 | int cmd_print_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 264 | |
| 265 | /** |
| 266 | * @copydoc cmd_add_dep |
| 267 | */ |
| 268 | int 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 | */ |
| 278 | 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] | 279 | void cmd_print_help(void); |
| 280 | |
| 281 | /* cmd_searchpath.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 282 | |
| 283 | /** |
| 284 | * @copydoc cmd_add_opt |
| 285 | */ |
| 286 | int 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 | */ |
| 296 | 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] | 297 | void cmd_searchpath_help(void); |
| 298 | |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 299 | /* cmd_extdata.c */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * @copydoc cmd_add_opt |
| 303 | */ |
| 304 | int cmd_extdata_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 305 | |
| 306 | /** |
| 307 | * @copydoc cmd_add_dep |
| 308 | */ |
| 309 | int 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 | */ |
| 319 | 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] | 320 | void cmd_extdata_help(void); |
| 321 | void cmd_extdata_free(void); |
| 322 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 323 | /* cmd_help.c */ |
| 324 | |
| 325 | /** |
| 326 | * @copydoc cmd_add_opt |
| 327 | */ |
| 328 | int 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 | */ |
| 338 | int cmd_help_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
| 339 | void cmd_help_help(void); |
| 340 | |
| 341 | /* cmd_verb.c */ |
| 342 | |
| 343 | /** |
| 344 | * @copydoc cmd_add_opt |
| 345 | */ |
| 346 | int cmd_verb_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 347 | |
| 348 | /** |
| 349 | * @copydoc cmd_add_dep |
| 350 | */ |
| 351 | int 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 | */ |
| 361 | int cmd_verb_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv); |
| 362 | void cmd_verb_help(void); |
| 363 | |
| 364 | /* cmd_debug.c */ |
| 365 | |
| 366 | /** |
| 367 | * @copydoc cmd_add_opt |
| 368 | */ |
| 369 | int cmd_debug_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc); |
| 370 | |
| 371 | /** |
| 372 | * @copydoc cmd_add_dep |
| 373 | */ |
| 374 | int 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 | */ |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 384 | 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] | 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 | */ |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 393 | int cmd_debug_setlog(struct ly_ctx *ctx, struct yl_opt *yo); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 394 | void cmd_debug_help(void); |
| 395 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 396 | #endif /* COMMANDS_H_ */ |