| 1 | #ifndef LIBCLI_H |
| 2 | #define LIBCLI_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2001 Richard Almeida & Ibex Ltd ([email protected]) |
| 6 | * |
| 7 | * Copyright (C) 2015 Tomasz Kaźmierczak ([email protected]) |
| 8 | * - added command completion |
| 9 | * (c) fenugrec 2014-2022 |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * ****************************************** |
| 17 | * |
| 18 | * Generic CLI processor library |
| 19 | * |
| 20 | * Split out from freediag code 2022/10 |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include <stdio.h> /* For FILE */ |
| 25 | |
| 26 | /** Extra configs should go in this external header |
| 27 | */ |
| 28 | #include "libcli_conf.h" |
| 29 | |
| 30 | /** Macros that have an effect on libcli and can be defined in libcli_conf.h : |
| 31 | */ |
| 32 | // #define HAVE_LIBREADLINE //self-explanatory |
| 33 | // #define ASSERT_FAIL(x) custom_assert(x) |
| 34 | |
| 35 | |
| 36 | /** Return values from the commands */ |
| 37 | enum cli_retval { |
| 38 | CMD_OK=0, /* OK */ |
| 39 | CMD_USAGE, /* Bad usage, print usage info */ |
| 40 | CMD_FAILED, /* Cmd failed */ |
| 41 | CMD_EXIT, /* Exit called */ |
| 42 | CMD_UP /* Go up one level in command tree */ |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | /******* Command table definitions ********/ |
| 47 | |
| 48 | /** Command descriptor |
| 49 | * |
| 50 | * command table consists of an array of these |
| 51 | * */ |
| 52 | struct cmd_tbl_entry { |
| 53 | const char *command; /* Command name */ |
| 54 | const char *usage; /* Usage info */ |
| 55 | const char *help; /* Help Text */ |
| 56 | enum cli_retval (*routine)(int argc, char **argv); /* Command Routine */ |
| 57 | const int flags; /* Flag, see below */ |
| 58 | |
| 59 | const struct cmd_tbl_entry *sub_cmd_tbl; /* If specified, command is a sub-table */ |
| 60 | }; |
| 61 | |
| 62 | // optional : add built-in commands with default implementations |
| 63 | #define CLI_TBL_BUILTINS \ |
| 64 | { "up", "up", "Return to previous menu level", cmd_up, 0, NULL}, \ |
| 65 | { "exit", "exit", "Exits program", cmd_exit, 0, NULL}, \ |
| 66 | { "quit", "quit", "Exits program", cmd_exit, CLI_CMD_HIDDEN, NULL} |
| 67 | |
| 68 | #define CLI_TBL_END { NULL, NULL, NULL, NULL, 0, NULL} //must be last element of every command table |
| 69 | |
| 70 | /** list of customizable callbacks. |
| 71 | * Unused entries can be set to NULL |
| 72 | */ |
| 73 | struct cli_callbacks { |
| 74 | void (*cli_logcmd)(int argc, char **argv); // optional, called for each processed command |
| 75 | void (*cli_atexit)(void); //optional, will be called after a CMD_EXIT |
| 76 | }; |
| 77 | |
| 78 | #define CLI_CMD_HIDDEN (1 << 0) /* Hidden command */ |
| 79 | #define CLI_CMD_FILEARG (1 << 1) /* Command accepts a filename as an argument*/ |
| 80 | #define CLI_CMD_CUSTOM (1 << 2) /* Command handles other subcommands not in the subtable, max 1 per table */ |
| 81 | |
| 82 | |
| 83 | |
| 84 | /************************** public funcs ***************/ |
| 85 | |
| 86 | /** Change default callbacks |
| 87 | */ |
| 88 | void cli_set_callbacks(const struct cli_callbacks *); |
| 89 | |
| 90 | /** Start an interactive CLI session |
| 91 | * |
| 92 | * @param name prompt string |
| 93 | * @param initscript optional; file to source commands from on init |
| 94 | * @param cmdtable |
| 95 | */ |
| 96 | void cli_enter(const char *name, const char *initscript, const struct cmd_tbl_entry *cmdtable); |
| 97 | |
| 98 | |
| 99 | |
| 100 | /** Prompt for some input. |
| 101 | * |
| 102 | * @param prompt : optional |
| 103 | * @return a new 0-terminated string with trailing CR/LF stripped, NULL if no more input |
| 104 | * |
| 105 | * Caller must free returned buffer. Used if we don't |
| 106 | * have readline, and when reading init or command files. |
| 107 | * No line editing or history. |
| 108 | */ |
| 109 | char *cli_basic_get_input(const char *prompt, FILE *instream); |
| 110 | |
| 111 | |
| 112 | /** builtin command to move up a level */ |
| 113 | enum cli_retval cmd_up(int argc, char **argv); |
| 114 | |
| 115 | /** builtin command to exit CLI entirely from any level */ |
| 116 | enum cli_retval cmd_exit(int argc, char **argv); |
| 117 | |
| 118 | /** builtin command to run commands from an external file */ |
| 119 | enum cli_retval cmd_source(int argc, char **argv); |
| 120 | |
| 121 | /** builtin help : print list of commands in given table, |
| 122 | * or give detailed help about specified command |
| 123 | */ |
| 124 | enum cli_retval cli_help_basic(int argc, char **argv, const struct cmd_tbl_entry *cmd_table); |
| 125 | |
| 126 | #endif |
| 127 | |