]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
edit: Move history save file specification to caller
authorJouni Malinen <j@w1.fi>
Sun, 21 Nov 2010 09:43:09 +0000 (11:43 +0200)
committerJouni Malinen <j@w1.fi>
Sun, 21 Nov 2010 09:43:09 +0000 (11:43 +0200)
src/utils/edit.c
src/utils/edit.h
src/utils/edit_readline.c
src/utils/edit_simple.c
wlantest/wlantest_cli.c
wpa_supplicant/wpa_cli.c

index a2572c7a7fb0bbf6a46bc2da684a2d641ae083d3..96cf736fae350f3f871125ca75a6de7f64d53a4c 100644 (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;
-}
index d674a49d6c0fcc3a7de1986ad188b27073f7d760..fc4474bd6ece7167fe81400bc808d116bee1b4ee 100644 (file)
 
 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 */
index 72d584d574f52f01436c6c240e17a1539a3dbeb1..1fef7b9c0e62c98c58d5c29b2b91202cb3f9be69 100644 (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,34 +115,20 @@ 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);
-                               stifle_history(100);
-                       }
-               }
+       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;
-}
index c31dc2ea89784eaf470ab40f036bb3a75f1b4643..61fb24e23d55fb23d3f33a5ef348557f529e95f9 100644 (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))
-{
-}
index df96bd3516645be00954484296d856e0617f64ab..c0f6f5489350e5d8ff719b40c8d02c62ce697f73 100644 (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();
 }
 
index 4bea0ec158522176a97639e20a062ead3ded93ec..7de05eeb38a121c9a2681f6ba07bf2fb0c505cba 100644 (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();
 }