2010-11-14 15:37:43 -05:00
|
|
|
/*
|
|
|
|
* Command line editing and history wrapper for readline
|
|
|
|
* Copyright (c) 2010, Jouni Malinen <j@w1.fi>
|
|
|
|
*
|
2012-02-11 09:46:35 -05:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2010-11-14 15:37:43 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "eloop.h"
|
|
|
|
#include "edit.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void *edit_cb_ctx;
|
|
|
|
static void (*edit_cmd_cb)(void *ctx, char *cmd);
|
|
|
|
static void (*edit_eof_cb)(void *ctx);
|
|
|
|
static char ** (*edit_completion_cb)(void *ctx, const char *cmd, int pos) =
|
|
|
|
NULL;
|
|
|
|
|
|
|
|
static char **pending_completions = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static void readline_free_completions(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (pending_completions == NULL)
|
|
|
|
return;
|
|
|
|
for (i = 0; pending_completions[i]; i++)
|
|
|
|
os_free(pending_completions[i]);
|
|
|
|
os_free(pending_completions);
|
|
|
|
pending_completions = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char * readline_completion_func(const char *text, int state)
|
|
|
|
{
|
|
|
|
static int pos = 0;
|
|
|
|
static size_t len = 0;
|
|
|
|
|
|
|
|
if (pending_completions == NULL) {
|
|
|
|
rl_attempted_completion_over = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == 0) {
|
|
|
|
pos = 0;
|
|
|
|
len = os_strlen(text);
|
|
|
|
}
|
|
|
|
for (; pending_completions[pos]; pos++) {
|
|
|
|
if (strncmp(pending_completions[pos], text, len) == 0)
|
|
|
|
return strdup(pending_completions[pos++]);
|
|
|
|
}
|
|
|
|
|
|
|
|
rl_attempted_completion_over = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char ** readline_completion(const char *text, int start, int end)
|
|
|
|
{
|
|
|
|
readline_free_completions();
|
|
|
|
if (edit_completion_cb)
|
|
|
|
pending_completions = edit_completion_cb(edit_cb_ctx,
|
|
|
|
rl_line_buffer, end);
|
|
|
|
return rl_completion_matches(text, readline_completion_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
|
|
|
|
{
|
|
|
|
rl_callback_read_char();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void trunc_nl(char *str)
|
|
|
|
{
|
|
|
|
char *pos = str;
|
|
|
|
while (*pos != '\0') {
|
|
|
|
if (*pos == '\n') {
|
|
|
|
*pos = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void readline_cmd_handler(char *cmd)
|
|
|
|
{
|
|
|
|
if (cmd && *cmd) {
|
|
|
|
HIST_ENTRY *h;
|
|
|
|
while (next_history())
|
|
|
|
;
|
|
|
|
h = previous_history();
|
|
|
|
if (h == NULL || os_strcmp(cmd, h->line) != 0)
|
|
|
|
add_history(cmd);
|
|
|
|
next_history();
|
|
|
|
}
|
|
|
|
if (cmd == NULL) {
|
|
|
|
edit_eof_cb(edit_cb_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trunc_nl(cmd);
|
|
|
|
edit_cmd_cb(edit_cb_ctx, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
|
|
|
|
void (*eof_cb)(void *ctx),
|
2010-11-21 04:43:09 -05:00
|
|
|
char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
|
|
|
|
void *ctx, const char *history_file)
|
2010-11-14 15:37:43 -05:00
|
|
|
{
|
|
|
|
edit_cb_ctx = ctx;
|
|
|
|
edit_cmd_cb = cmd_cb;
|
|
|
|
edit_eof_cb = eof_cb;
|
2010-11-21 04:43:09 -05:00
|
|
|
edit_completion_cb = completion_cb;
|
2010-11-14 15:37:43 -05:00
|
|
|
|
|
|
|
rl_attempted_completion_function = readline_completion;
|
2010-11-21 04:43:09 -05:00
|
|
|
if (history_file) {
|
|
|
|
read_history(history_file);
|
|
|
|
stifle_history(100);
|
2010-11-14 15:37:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
eloop_register_read_sock(STDIN_FILENO, edit_read_char, NULL, NULL);
|
|
|
|
|
|
|
|
rl_callback_handler_install("> ", readline_cmd_handler);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 04:43:09 -05:00
|
|
|
void edit_deinit(const char *history_file,
|
|
|
|
int (*filter_cb)(void *ctx, const char *cmd))
|
2010-11-14 15:37:43 -05:00
|
|
|
{
|
|
|
|
rl_callback_handler_remove();
|
|
|
|
readline_free_completions();
|
|
|
|
|
|
|
|
eloop_unregister_read_sock(STDIN_FILENO);
|
|
|
|
|
2010-11-21 04:43:09 -05:00
|
|
|
if (history_file) {
|
2010-11-14 15:37:43 -05:00
|
|
|
/* Save command history, excluding lines that may contain
|
|
|
|
* passwords. */
|
|
|
|
HIST_ENTRY *h;
|
|
|
|
history_set_pos(0);
|
|
|
|
while ((h = current_history())) {
|
|
|
|
char *p = h->line;
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
2010-11-21 04:43:09 -05:00
|
|
|
if (filter_cb && filter_cb(edit_cb_ctx, p)) {
|
2010-11-14 15:37:43 -05:00
|
|
|
h = remove_history(where_history());
|
|
|
|
if (h) {
|
|
|
|
os_free(h->line);
|
|
|
|
free(h->data);
|
|
|
|
os_free(h);
|
|
|
|
} else
|
|
|
|
next_history();
|
|
|
|
} else
|
|
|
|
next_history();
|
|
|
|
}
|
2010-11-21 04:43:09 -05:00
|
|
|
write_history(history_file);
|
2010-11-14 15:37:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void edit_clear_line(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void edit_redraw(void)
|
|
|
|
{
|
|
|
|
rl_on_new_line();
|
|
|
|
rl_redisplay();
|
|
|
|
}
|