edit: Move history save file specification to caller

This commit is contained in:
Jouni Malinen 2010-11-21 11:43:09 +02:00
parent ec9aac9468
commit 8953e9681a
6 changed files with 41 additions and 77 deletions

View file

@ -1045,7 +1045,8 @@ static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
void (*eof_cb)(void *ctx),
void *ctx)
char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
void *ctx, const char *history_file)
{
dl_list_init(&history_list);
history_curr = NULL;
@ -1053,6 +1054,7 @@ int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
edit_cb_ctx = ctx;
edit_cmd_cb = cmd_cb;
edit_eof_cb = eof_cb;
edit_completion_cb = completion_cb;
tcgetattr(STDIN_FILENO, &prevt);
newt = prevt;
@ -1068,7 +1070,8 @@ int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
}
void edit_deinit(void)
void edit_deinit(const char *history_file,
int (*filter_cb)(void *ctx, const char *cmd))
{
struct edit_history *h;
while ((h = dl_list_first(&history_list, struct edit_history, list))) {
@ -1093,14 +1096,3 @@ void edit_redraw(void)
}
fflush(stdout);
}
void edit_set_filter_history_cb(int (*cb)(void *ctx, const char *cmd))
{
}
void edit_set_completion_cb(char ** (*cb)(void *ctx, const char *cmd, int pos))
{
edit_completion_cb = cb;
}

View file

@ -17,12 +17,11 @@
int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
void (*eof_cb)(void *ctx),
void *ctx);
void edit_deinit(void);
char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
void *ctx, const char *history_file);
void edit_deinit(const char *history_file,
int (*filter_cb)(void *ctx, const char *cmd));
void edit_clear_line(void);
void edit_redraw(void);
void edit_set_filter_history_cb(int (*cb)(void *ctx, const char *cmd));
void edit_set_completion_cb(char ** (*cb)(void *ctx, const char *cmd,
int pos));
#endif /* EDIT_H */

View file

@ -24,7 +24,6 @@
static void *edit_cb_ctx;
static void (*edit_cmd_cb)(void *ctx, char *cmd);
static void (*edit_eof_cb)(void *ctx);
static int (*edit_filter_history_cb)(void *ctx, const char *cmd) = NULL;
static char ** (*edit_completion_cb)(void *ctx, const char *cmd, int pos) =
NULL;
@ -116,35 +115,21 @@ static void readline_cmd_handler(char *cmd)
}
static char *readline_hfile = NULL;
int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
void (*eof_cb)(void *ctx),
void *ctx)
char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
void *ctx, const char *history_file)
{
char *home;
edit_cb_ctx = ctx;
edit_cmd_cb = cmd_cb;
edit_eof_cb = eof_cb;
edit_completion_cb = completion_cb;
rl_attempted_completion_function = readline_completion;
home = getenv("HOME");
if (home) {
const char *fname = ".wpa_cli_history";
int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
readline_hfile = os_malloc(hfile_len);
if (readline_hfile) {
int res;
res = os_snprintf(readline_hfile, hfile_len, "%s/%s",
home, fname);
if (res >= 0 && res < hfile_len) {
readline_hfile[hfile_len - 1] = '\0';
read_history(readline_hfile);
if (history_file) {
read_history(history_file);
stifle_history(100);
}
}
}
eloop_register_read_sock(STDIN_FILENO, edit_read_char, NULL, NULL);
@ -154,14 +139,15 @@ int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
}
void edit_deinit(void)
void edit_deinit(const char *history_file,
int (*filter_cb)(void *ctx, const char *cmd))
{
rl_callback_handler_remove();
readline_free_completions();
eloop_unregister_read_sock(STDIN_FILENO);
if (readline_hfile) {
if (history_file) {
/* Save command history, excluding lines that may contain
* passwords. */
HIST_ENTRY *h;
@ -170,8 +156,7 @@ void edit_deinit(void)
char *p = h->line;
while (*p == ' ' || *p == '\t')
p++;
if (edit_filter_history_cb &&
edit_filter_history_cb(edit_cb_ctx, p)) {
if (filter_cb && filter_cb(edit_cb_ctx, p)) {
h = remove_history(where_history());
if (h) {
os_free(h->line);
@ -182,9 +167,7 @@ void edit_deinit(void)
} else
next_history();
}
write_history(readline_hfile);
os_free(readline_hfile);
readline_hfile = NULL;
write_history(history_file);
}
}
@ -199,15 +182,3 @@ void edit_redraw(void)
rl_on_new_line();
rl_redisplay();
}
void edit_set_filter_history_cb(int (*cb)(void *ctx, const char *cmd))
{
edit_filter_history_cb = cb;
}
void edit_set_completion_cb(char ** (*cb)(void *ctx, const char *cmd, int pos))
{
edit_completion_cb = cb;
}

View file

@ -62,7 +62,8 @@ static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
void (*eof_cb)(void *ctx),
void *ctx)
char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
void *ctx, const char *history_file)
{
edit_cb_ctx = ctx;
edit_cmd_cb = cmd_cb;
@ -76,7 +77,8 @@ int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
}
void edit_deinit(void)
void edit_deinit(const char *history_file,
int (*filter_cb)(void *ctx, const char *cmd))
{
eloop_unregister_read_sock(STDIN_FILENO);
}
@ -92,13 +94,3 @@ void edit_redraw(void)
cmdbuf[cmdbuf_pos] = '\0';
printf("\r> %s", cmdbuf);
}
void edit_set_filter_history_cb(int (*cb)(void *ctx, const char *cmd))
{
}
void edit_set_completion_cb(char ** (*cb)(void *ctx, const char *cmd, int pos))
{
}

View file

@ -1090,12 +1090,12 @@ static void wlantest_cli_interactive(int s)
cli.s = s;
eloop_register_signal_terminate(wlantest_cli_eloop_terminate, &cli);
edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb, &cli);
edit_set_completion_cb(wlantest_cli_edit_completion_cb);
edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb,
wlantest_cli_edit_completion_cb, &cli, NULL);
eloop_run();
edit_deinit();
edit_deinit(NULL, NULL);
eloop_destroy();
}

View file

@ -2803,18 +2803,28 @@ static void wpa_cli_edit_eof_cb(void *ctx)
static void wpa_cli_interactive(void)
{
char *home, *hfile = NULL;
printf("\nInteractive mode\n\n");
home = getenv("HOME");
if (home) {
const char *fname = ".wpa_cli_history";
int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
hfile = os_malloc(hfile_len);
if (hfile)
os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
}
eloop_register_signal_terminate(wpa_cli_eloop_terminate, NULL);
edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb, NULL);
edit_set_filter_history_cb(wpa_cli_edit_filter_history_cb);
edit_set_completion_cb(wpa_cli_edit_completion_cb);
edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb,
wpa_cli_edit_completion_cb, NULL, hfile);
eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
eloop_run();
edit_deinit();
edit_deinit(hfile, wpa_cli_edit_filter_history_cb);
os_free(hfile);
eloop_cancel_timeout(wpa_cli_ping, NULL, NULL);
wpa_cli_close_connection();
}