| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2015 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #ifndef _CLI_TOOL_H_ |
| 22 | #define _CLI_TOOL_H_ |
| 23 | |
| 24 | #include <stdbool.h> |
| 25 | #include <popt.h> |
| 26 | |
| 27 | struct cli_cmdline; |
| 28 | |
| 29 | typedef errno_t |
| 30 | (*cli_route_fn)(struct cli_cmdline *cmdline); |
| 31 | |
| 32 | #define CLI_CMD_NONE 0x0000 |
| 33 | #define CLI_CMD_REQUIRE_ROOT 0x0001 |
| 34 | #define CLI_CMD_HIDDEN 0x0002 |
| 35 | |
| 36 | #define CLI_TOOL_COMMAND(cmd, msg, flags, fn) {cmd, _(msg), (flags), fn} |
| 37 | #define CLI_TOOL_COMMAND_NOMSG(cmd, flags, fn) {cmd, NULL, (flags), fn} |
| 38 | #define CLI_TOOL_DELIMITER(message) {"", _(message), CLI_CMD_NONE, NULL} |
| 39 | #define CLI_TOOL_LAST {NULL, NULL, CLI_CMD_NONE, NULL} |
| 40 | |
| 41 | struct cli_cmdline { |
| 42 | const char *exec; /* argv[0] */ |
| 43 | const char *command; /* command name */ |
| 44 | int argc; /* rest of arguments */ |
| 45 | const char **argv; |
| 46 | }; |
| 47 | |
| 48 | struct cli_route_cmd { |
| 49 | const char *command; |
| 50 | const char *description; |
| 51 | uint32_t flags; |
| 52 | cli_route_fn fn; |
| 53 | }; |
| 54 | |
| 55 | void cli_tool_usage(const char *tool_name, struct cli_route_cmd *commands); |
| 56 | |
| 57 | typedef errno_t (*cli_popt_fn)(poptContext pc, char option, void *pvt); |
| 58 | |
| 59 | enum cli_tool_opt { |
| 60 | CLI_TOOL_OPT_REQUIRED, |
| 61 | CLI_TOOL_OPT_OPTIONAL |
| 62 | }; |
| 63 | |
| 64 | errno_t cli_tool_popt_ex(struct cli_cmdline *cmdline, |
| 65 | struct poptOption *options, |
| 66 | enum cli_tool_opt require_option, |
| 67 | cli_popt_fn popt_fn, |
| 68 | void *popt_fn_pvt, |
| 69 | const char *fopt_name, |
| 70 | const char *fopt_help, |
| 71 | char **_fopt, |
| 72 | bool allow_more_free_opts, |
| 73 | bool *_opt_set); |
| 74 | |
| 75 | errno_t cli_tool_popt(struct cli_cmdline *cmdline, |
| 76 | struct poptOption *options, |
| 77 | enum cli_tool_opt require_option, |
| 78 | cli_popt_fn popt_fn, |
| 79 | void *popt_fn_pvt); |
| 80 | |
| 81 | int cli_tool_main(int argc, const char **argv, |
| 82 | struct cli_route_cmd *commands, |
| 83 | void *pvt); |
| 84 | |
| 85 | #endif /* _CLI_TOOL_H_ */ |