diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 50d7d660b..cb739df13 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -18,8 +18,10 @@ #ifdef HAVE_READLINE #include #include +#include #endif #include + #include "usart_defs.h" #include "util_posix.h" #include "proxgui.h" @@ -134,6 +136,23 @@ static int check_comm(void) { } return 0; } +static void flush_history(void) { +#ifdef HAVE_READLINE + if (session.history_path) { + write_history(session.history_path); + free(session.history_path); + } +#endif +} + +#ifdef HAVE_READLINE +struct sigaction old_action; +static void terminate_handler(int signum) { + sigaction(SIGINT, &old_action, NULL); + flush_history(); + kill(0, SIGINT); +} +#endif // first slot is always NULL, indicating absence of script when idx=0 static FILE *cmdscriptfile[MAX_NESTED_CMDSCRIPT + 1] = {0}; @@ -211,16 +230,26 @@ main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop) { } #ifdef HAVE_READLINE - char *my_history_path = NULL; - if (searchHomeFilePath(&my_history_path, NULL, PROXHISTORY, true) != PM3_SUCCESS) { + session.history_path = NULL; + if (searchHomeFilePath(&session.history_path, NULL, PROXHISTORY, true) != PM3_SUCCESS) { PrintAndLogEx(ERR, "No history will be recorded"); - my_history_path = NULL; + session.history_path = NULL; } else { - read_history(my_history_path); + + struct sigaction action; + memset(&action, 0, sizeof(action)); + action.sa_handler = &terminate_handler; + sigaction(SIGINT, &action, &old_action); + + rl_catch_signals = 1; + rl_set_signals(); + read_history(session.history_path); } #endif + // loops every time enter is pressed... while (1) { + bool printprompt = false; if (session.pm3_present) { if (conn.send_via_fpc_usart == false) @@ -391,11 +420,9 @@ check_script: pop_cmdscriptfile(); #ifdef HAVE_READLINE - if (my_history_path) { - write_history(my_history_path); - free(my_history_path); - } + flush_history(); #endif + if (cmd) { free(cmd); cmd = NULL; diff --git a/client/src/ui.h b/client/src/ui.h index 60064c0aa..7a5b7911d 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -43,6 +43,7 @@ typedef struct { // char *defaultPaths[spItemCount]; // Array should allow loop searching for files clientdebugLevel_t client_debug_level; // uint8_t device_debug_level; + char *history_path; } session_arg_t; extern session_arg_t session;