summaryrefslogtreecommitdiffstats
path: root/parser.c
diff options
context:
space:
mode:
authorKaz Kyheku <kaz@kylheku.com>2020-02-18 06:40:15 -0800
committerKaz Kylheku <kaz@kylheku.com>2020-02-18 06:40:15 -0800
commite41669c53b57fa19f614e22a328fdf181d9c8f46 (patch)
treeab774f89fafa02a26ae4cacddde504fa85962fbd /parser.c
parent3080fb5f28e0337b5859f4d6eb19bda9207a65fa (diff)
downloadtxr-e41669c53b57fa19f614e22a328fdf181d9c8f46.tar.gz
txr-e41669c53b57fa19f614e22a328fdf181d9c8f46.tar.bz2
txr-e41669c53b57fa19f614e22a328fdf181d9c8f46.zip
listener: append to .txr_history instead of clobbering.
This patch addresses the problem of history loss that occurs when a user juggles multiple TXR sessions that all clobber the same history file. * linenoise/linenoise.c (struct lino_state): New member, loaded_lines, keeping track of how many of the lines in the history came from loading the history file. Lines between [0] and [loaded_lines - 1] are loaded lines. New lines occur between [loaded_lines] and [history_len - 1]. (lino_hist_add): Reset loaded_lines to zero when creating history for the first time. Not really necessary since the structure starts zero-filled. When a line of history is erased, then it must be a loaded line, unless loaded_lines is zero. Thus, then decrement loaded_lines to account for a loss of a loaded line, but don't decrement below zero. (lino_hist_set_max_len): Setting the max length can cause history to be trimmed, so we must adjust loaded_lines to account for any loaded lines that get discarded. (lino_hist_save): Takes a new parameter which indicates whether to just save the new history by appending it to the given file, or to overwrite the file with the entire history. In either case, once we save the history, we assume that all of our lines are loaded lines and set loaded_lines to hist_len. In the future, this last step will help implement incremental saving mid-way through a sesssion. (lino_hist_load): Error out if there is already a history. With this loaded_lines logic, it really wouldn't make sense to read history more than once. After loading, set loaded_lines to hist_len. * linenoise/linenoise.h (enum lino_file_mode): New enumeration lino_append. (lino_hist_save): Declaration updated. * parser.c (repl): Implement new history saving protocol. The history file is read using a temporary instance of linenoise, which has the effect of trimming it to the required number of lines. This is written to a temporary file, to which the newly entered lines are appended, and which is finally renamed to replace the history file. (lino_mode_str): Add "a" entry corresponding to lino_append. (lino_open): Do the fchmod in the lino_append case also. * txr.1: Documented the new handling of the history file.
Diffstat (limited to 'parser.c')
-rw-r--r--parser.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/parser.c b/parser.c
index 8746f4c8..0c6067e7 100644
--- a/parser.c
+++ b/parser.c
@@ -1505,12 +1505,19 @@ val repl(val bindings, val in_stream, val out_stream, val env)
dyn_env = saved_dyn_env;
if (histfile_w) {
- val histfile_tmp = format(nil, lit("~a/.txr_history.tmp"), home, nao);
- if (lino_hist_save(ls, c_str(histfile_tmp)) == 0)
+ val histfile_tmp = format(nil, lit("~a.tmp"), histfile, nao);
+ const wchar_t *histfile_tmp_w = c_str(histfile_tmp);
+ lino_t *ltmp = lino_make(coerce(mem_t *, in_stream),
+ coerce(mem_t *, out_stream));
+ lino_hist_set_max_len(ltmp, c_num(cdr(hist_len_var)));
+ lino_hist_load(ltmp, histfile_w);
+ lino_hist_save(ltmp, histfile_tmp_w, 0);
+ if (lino_hist_save(ls, histfile_tmp_w, 1) == 0)
rename_path(histfile_tmp, histfile);
else
put_line(lit("** unable to save history file"), out_stream);
gc_hint(histfile_tmp);
+ lino_free(ltmp);
}
free(line_w);
@@ -1649,7 +1656,7 @@ static int lino_feof(mem_t *stream_in)
}
static const wchli_t *lino_mode_str[] = {
- wli("r"), wli("w")
+ wli("r"), wli("w"), wli("a")
};
static mem_t *lino_open(const wchar_t *name_in, lino_file_mode_t mode_in)
@@ -1660,7 +1667,7 @@ static mem_t *lino_open(const wchar_t *name_in, lino_file_mode_t mode_in)
ignerr_begin;
ret = open_file(name, mode);
#if HAVE_CHMOD
- if (mode_in == lino_overwrite)
+ if (mode_in == lino_overwrite || mode_in == lino_append)
(void) fchmod(c_num(stream_fd(ret)), S_IRUSR | S_IWUSR);
#endif
ignerr_end;