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;
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;
}
-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))) {
}
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;
-}
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 */
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;
}
-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);
- stifle_history(100);
- }
- }
+ if (history_file) {
+ read_history(history_file);
+ stifle_history(100);
}
eloop_register_read_sock(STDIN_FILENO, edit_read_char, NULL, NULL);
}
-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;
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);
} else
next_history();
}
- write_history(readline_hfile);
- os_free(readline_hfile);
- readline_hfile = NULL;
+ write_history(history_file);
}
}
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;
-}
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;
}
-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);
}
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))
-{
-}
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();
}
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();
}