aboutsummaryrefslogtreecommitdiff
path: root/libcli.h
diff options
context:
space:
mode:
Diffstat (limited to 'libcli.h')
-rw-r--r--libcli.h61
1 files changed, 44 insertions, 17 deletions
diff --git a/libcli.h b/libcli.h
index e978526..01b334f 100644
--- a/libcli.h
+++ b/libcli.h
@@ -16,7 +16,8 @@ extern "C" {
#define CLI_QUIT -2
#define CLI_ERROR_ARG -3
-#define MAX_HISTORY 256
+#define MAX_HISTORY 5 /* testing, used to be 256 */
+#define HISTORY_CMD_LEN 128
#define PRIVILEGE_UNPRIVILEGED 0
#define PRIVILEGE_PRIVILEGED 15
@@ -30,8 +31,20 @@ extern "C" {
#define PRINT_FILTERED 0x01
#define PRINT_BUFFERED 0x02
-#define CLI_MAX_LINE_LENGTH 4096
-#define CLI_MAX_LINE_WORDS 128
+#define CLI_MAX_LINE_LENGTH 64
+#define CLI_MAX_LINE_WORDS 16
+#define CLI_MAX_CMD_NAME_LEN 32
+
+#define CLI_LOOP_CTRL_CONTINUE 1
+#define CLI_LOOP_CTRL_BREAK 2
+
+enum cli_states {
+ CLI_STATE_LOGIN,
+ CLI_STATE_PASSWORD,
+ CLI_STATE_NORMAL,
+ CLI_STATE_ENABLE_PASSWORD,
+ CLI_STATE_ENABLE
+};
struct cli_def {
int completion_callback;
@@ -42,29 +55,28 @@ struct cli_def {
char *banner;
struct unp *users;
char *enable_password;
- char *history[MAX_HISTORY];
+ char history[MAX_HISTORY][HISTORY_CMD_LEN];
char showprompt;
char *promptchar;
char *hostname;
char *modestring;
int privilege;
int mode;
- int state;
+ enum cli_states state;
struct cli_filter *filters;
void (*print_callback)(struct cli_def *cli, const char *string);
FILE *client;
/* internal buffers */
void *conn;
void *service;
- char *commandname; // temporary buffer for cli_command_name() to prevent leak
- char *buffer;
- unsigned buf_size;
struct timeval timeout_tm;
time_t idle_timeout;
int (*idle_timeout_callback)(struct cli_def *);
time_t last_action;
int telnet_protocol;
void *user_context;
+ int (*read_callback)(struct cli_def *cli, void *buf, const size_t count);
+ int (*write_callback)(struct cli_def *cli, const void *buf, const size_t count);
};
struct cli_filter {
@@ -85,24 +97,31 @@ struct cli_command {
struct cli_command *parent;
};
-struct cli_def *cli_init();
+struct cli_loop_ctx {
+ char cmd[CLI_MAX_LINE_LENGTH];
+ char username[64];
+ int l, restore_cmd_l;
+ int cursor, insertmode;
+ int lastchar, is_telnet_option, skip, esc;
+ signed int in_history;
+ int sockfd;
+};
+
+int cli_init(struct cli_def *cli);
int cli_done(struct cli_def *cli);
-struct cli_command *cli_register_command(struct cli_def *cli, struct cli_command *parent, const char *command,
- int (*callback)(struct cli_def *, const char *, char **, int), int privilege,
- int mode, const char *help);
+void cli_register_command2(struct cli_def *cli, struct cli_command *cmd, struct cli_command *parent);
int cli_unregister_command(struct cli_def *cli, const char *command);
int cli_run_command(struct cli_def *cli, const char *command);
int cli_loop(struct cli_def *cli, int sockfd);
int cli_file(struct cli_def *cli, FILE *fh, int privilege, int mode);
void cli_set_auth_callback(struct cli_def *cli, int (*auth_callback)(const char *, const char *));
void cli_set_enable_callback(struct cli_def *cli, int (*enable_callback)(const char *));
-void cli_allow_user(struct cli_def *cli, const char *username, const char *password);
void cli_allow_enable(struct cli_def *cli, const char *password);
void cli_deny_user(struct cli_def *cli, const char *username);
-void cli_set_banner(struct cli_def *cli, const char *banner);
-void cli_set_hostname(struct cli_def *cli, const char *hostname);
-void cli_set_promptchar(struct cli_def *cli, const char *promptchar);
-void cli_set_modestring(struct cli_def *cli, const char *modestring);
+void cli_set_banner(struct cli_def *cli, char *banner);
+void cli_set_hostname(struct cli_def *cli, char *hostname);
+void cli_set_promptchar(struct cli_def *cli, char *promptchar);
+void cli_set_modestring(struct cli_def *cli, char *modestring);
int cli_set_privilege(struct cli_def *cli, int privilege);
int cli_set_configmode(struct cli_def *cli, int mode, const char *config_desc);
void cli_reprompt(struct cli_def *cli);
@@ -116,6 +135,14 @@ void cli_print_callback(struct cli_def *cli, void (*callback)(struct cli_def *,
void cli_free_history(struct cli_def *cli);
void cli_set_idle_timeout(struct cli_def *cli, unsigned int seconds);
void cli_set_idle_timeout_callback(struct cli_def *cli, unsigned int seconds, int (*callback)(struct cli_def *));
+void cli_read_callback(struct cli_def *cli, int (*callback)(struct cli_def *cli, void *buf, size_t count));
+void cli_write_callback(struct cli_def *cli, int (*callback)(struct cli_def *cli, const void *buf, size_t count));
+
+void cli_loop_start_new_command(struct cli_def *cli, struct cli_loop_ctx *ctx);
+void cli_loop_show_prompt(struct cli_def *cli, struct cli_loop_ctx *ctx);
+int cli_loop_read_next_char(struct cli_def *cli, struct cli_loop_ctx *ctx, unsigned char *c);
+int cli_loop_process_char(struct cli_def *cli, struct cli_loop_ctx *ctx, unsigned char c);
+int cli_loop_process_cmd(struct cli_def *cli, struct cli_loop_ctx *ctx);
// Enable or disable telnet protocol negotiation.
// Note that this is enabled by default and must be changed before cli_loop() is run.