chg: when pressing ctrl-c, the command history is saved

This commit is contained in:
iceman1001 2020-06-29 18:10:40 +02:00
commit 2fe42173de
2 changed files with 36 additions and 8 deletions

View file

@ -18,8 +18,10 @@
#ifdef HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>
#endif
#include <ctype.h>
#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;

View file

@ -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;