diff --git a/bs/main.c b/bs/main.c index 5481aa1..1d1c626 100644 --- a/bs/main.c +++ b/bs/main.c @@ -16,10 +16,34 @@ static jtag_core *jc = NULL; static script_ctx *sctx = NULL; +#define HISTORY_MAX_ENTRIES 1000 +static char history_file[1024] = ""; + +/* Persist the REPL command history across sessions in + * $HOME/.bs_explorer_history (skipped if HOME is unset). */ +static void init_history(void) +{ + const char *home = getenv("HOME"); + if (home && *home) { + snprintf(history_file, sizeof(history_file), "%s/.bs_explorer_history", home); + using_history(); + read_history(history_file); + } +} + +static void save_history(void) +{ + if (history_file[0]) { + write_history(history_file); + history_truncate_file(history_file, HISTORY_MAX_ENTRIES); + } +} + static void handle_sigint(int sig) { (void)sig; fputs("\n", stdout); + save_history(); bsexp_deinit(jc, sctx); exit(0); } @@ -91,6 +115,7 @@ int main(void) signal(SIGINT, handle_sigint); rl_attempted_completion_function = bs_completion; rl_readline_name = "bs_explorer"; + init_history(); print_banner(); @@ -116,6 +141,7 @@ int main(void) fputs("\n", stdout); } + save_history(); bsexp_deinit(jc, sctx); return 0; }