bs: persist REPL command history across sessions
Load and save readline history in $HOME/.bs_explorer_history (capped at 1000 entries), so up-arrow recalls commands from previous sessions. Saved on exit/quit, EOF and Ctrl-C.
This commit is contained in:
26
bs/main.c
26
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user