From b1c2b8641f584ddeaef872ee6425cadca706e97d Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 7 Sep 2015 20:47:57 -0700 Subject: Load and save repl history. * parser.c (repl): If a HOME environment variable exists, then load the .txr_history file from the user's home directory and save it when exiting. * sysif.c (getenv_wrap): Static function changed to extern. * sysif.h (getenv_wrap): Declared. --- parser.c | 14 ++++++++++++++ sysif.c | 2 +- sysif.h | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/parser.c b/parser.c index 532936f0..6b5a0593 100644 --- a/parser.c +++ b/parser.c @@ -48,6 +48,7 @@ #include "eval.h" #include "stream.h" #include "y.tab.h" +#include "sysif.h" #include "parser.h" #if HAVE_TERMIOS #include "linenoise/linenoise.h" @@ -515,12 +516,18 @@ val repl(val bindings, val in_stream, val out_stream) val result_hash = make_hash(nil, nil, nil); val done = nil; val counter = one; + val home = getenv_wrap(lit("HOME")); + val histfile = if2(home, format(nil, lit("~a/.txr_history"), home, nao)); + char *histfile_u8 = utf8_dup_to(c_str(histfile)); val old_sig_handler = set_sig_handler(num(SIGINT), func_n2(repl_intr)); reg_varl(result_hash_sym, result_hash); lino_set_completion_cb(ls, provide_completions, 0); + if (histfile) + lino_hist_load(ls, histfile_u8); + while (!done) { val prompt = format(nil, lit("~a> "), counter, nao); val prev_counter = counter; @@ -597,9 +604,16 @@ val repl(val bindings, val in_stream, val out_stream) } set_sig_handler(num(SIGINT), old_sig_handler); + + + if (histfile) + lino_hist_save(ls, histfile_u8); + + free(histfile_u8); free(prompt_u8); free(line_u8); lino_free(ls); + gc_hint(histfile); return nil; } diff --git a/sysif.c b/sysif.c index 5197d337..1d858820 100644 --- a/sysif.c +++ b/sysif.c @@ -671,7 +671,7 @@ static val pipe_wrap(void) #endif -static val getenv_wrap(val name) +val getenv_wrap(val name) { char *nameu8 = utf8_dup_to(c_str(name)); char *lookup = getenv(nameu8); diff --git a/sysif.h b/sysif.h index c54ddbad..b2b5ce2f 100644 --- a/sysif.h +++ b/sysif.h @@ -32,4 +32,5 @@ extern val dev_s, ino_s, mode_s, nlink_s, uid_s; extern val gid_s, rdev_s, size_s, blksize_s, blocks_s; extern val atime_s, mtime_s, ctime_s; +val getenv_wrap(val name); void sysif_init(void); -- cgit v1.2.3