summary refs log tree commit diff
path: root/bin/cash/libedit/history.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-01-19 00:04:06 -0500
committerJune McEnroe <june@causal.agency>2019-01-19 00:04:06 -0500
commit8f97b95aee84aba6d4c8869a7a9eb98e446e9b32 (patch)
treea4a23ac94bcc3d1582c7982e363ea0bda4434d6a /bin/cash/libedit/history.c
parentAdd H_SAVE_INCR function to libedit (diff)
downloadsrc-8f97b95aee84aba6d4c8869a7a9eb98e446e9b32.tar.gz
src-8f97b95aee84aba6d4c8869a7a9eb98e446e9b32.zip
Take event number for save incr
ev cannot be used for input, as the first thing history() does is clear
it.
Diffstat (limited to '')
-rw-r--r--bin/cash/libedit/history.c32
1 files changed, 22 insertions, 10 deletions
diff --git a/bin/cash/libedit/history.c b/bin/cash/libedit/history.c
index bb5a654f..a8967088 100644
--- a/bin/cash/libedit/history.c
+++ b/bin/cash/libedit/history.c
@@ -894,8 +894,9 @@ history_save(TYPE(History) *h, const char *fname)
 
 
 static int
-history_save_fp_incr(TYPE(History) *h, TYPE(HistEvent) *ev, FILE *fp)
+history_save_fp_incr(TYPE(History) *h, FILE *fp, int num)
 {
+	TYPE(HistEvent) ev;
 	char *line = NULL;
 	char *ptr;
 	const char *str;
@@ -907,6 +908,12 @@ history_save_fp_incr(TYPE(History) *h, TYPE(HistEvent) *ev, FILE *fp)
 	static ct_buffer_t conv;
 #endif
 
+	for (retval = HCURR(h, &ev); retval != -1; retval = HNEXT(h, &ev))
+		if (ev.num == num)
+			break;
+	if (retval == -1)
+		goto done;
+
 	if (flock(fileno(fp), LOCK_EX) == -1)
 		goto done;
 	if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
@@ -914,8 +921,12 @@ history_save_fp_incr(TYPE(History) *h, TYPE(HistEvent) *ev, FILE *fp)
 
 	if (fseek(fp, 0, SEEK_SET) == -1)
 		goto done;
-	if ((sz = getline(&line, &llen, fp)) == -1)
-		goto done;
+	if ((sz = getline(&line, &llen, fp)) == -1) {
+		if (ferror(fp))
+			goto done;
+		else
+			sz = 0;
+	}
 	if (strncmp(line, hist_cookie, (size_t)sz) != 0)
 		goto done;
 	if (fseek(fp, 0, SEEK_END) == -1)
@@ -927,10 +938,10 @@ history_save_fp_incr(TYPE(History) *h, TYPE(HistEvent) *ev, FILE *fp)
 	ptr = h_malloc((max_size = 1024) * sizeof(*ptr));
 	if (ptr == NULL)
 		goto done;
-	for (i = 0, retval = HPREV(h, ev);
+	for (i = 0, retval = 0;
 		retval != -1;
-		retval = HPREV(h, ev), i++) {
-		str = ct_encode_string(ev->str, &conv);
+		retval = HPREV(h, &ev), i++) {
+		str = ct_encode_string(ev.str, &conv);
 		len = strlen(str) * 4 + 1;
 		if (len > max_size) {
 			char *nptr;
@@ -948,13 +959,14 @@ history_save_fp_incr(TYPE(History) *h, TYPE(HistEvent) *ev, FILE *fp)
 oomem:
 	h_free(ptr);
 done:
+	free(line);
 	(void) flock(fileno(fp), LOCK_UN);
 	return i;
 }
 
 
 static int
-history_save_incr(TYPE(History) *h, TYPE(HistEvent) *ev, const char *fname)
+history_save_incr(TYPE(History) *h, const char *fname, int num)
 {
 	FILE *fp;
 	int i;
@@ -962,7 +974,7 @@ history_save_incr(TYPE(History) *h, TYPE(HistEvent) *ev, const char *fname)
 	if ((fp = fopen(fname, "a+")) == NULL)
 		return -1;
 
-	i = history_save_fp_incr(h, ev, fp);
+	i = history_save_fp_incr(h, fp, num);
 
 	(void) fclose(fp);
 	return i;
@@ -1150,7 +1162,7 @@ FUNW(history)(TYPE(History) *h, TYPE(HistEvent) *ev, int fun, ...)
 		break;
 
 	case H_SAVE_INCR:
-		retval = history_save_incr(h, ev, va_arg(va, const char *));
+		retval = history_save_incr(h, va_arg(va, const char *), va_arg(va, int));
 		if (retval == -1)
 			he_seterrev(ev, _HE_HIST_WRITE);
 		break;
@@ -1162,7 +1174,7 @@ FUNW(history)(TYPE(History) *h, TYPE(HistEvent) *ev, int fun, ...)
 		break;
 
 	case H_SAVE_FP_INCR:
-		retval = history_save_fp_incr(h, ev, va_arg(va, FILE *));
+		retval = history_save_fp_incr(h, va_arg(va, FILE *), va_arg(va, int));
 		if (retval == -1)
 			he_seterrev(ev, _HE_HIST_WRITE);
 		break;