summary refs log tree commit diff
path: root/bin
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
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/editline.312
-rw-r--r--bin/cash/libedit/histedit.h4
-rw-r--r--bin/cash/libedit/history.c32
3 files changed, 30 insertions, 18 deletions
diff --git a/bin/cash/libedit/editline.3 b/bin/cash/libedit/editline.3
index 96ea5282..a27e24ce 100644
--- a/bin/cash/libedit/editline.3
+++ b/bin/cash/libedit/editline.3
@@ -838,9 +838,9 @@ Load the history list stored in
 .It Dv H_SAVE , Fa "const char *file"
 Save the history list to
 .Fa file .
-.It Dv H_SAVE_INCR , Fa "const char *file"
-Append the history list since the previous event
-.Fa ev
+.It Dv H_SAVE_INCR , Fa "const char *file" , Fa "int e"
+Append the history list since the event numbered
+.Fa e
 to
 .Fa file .
 .It Dv H_SAVE_FP , Fa "FILE *fp"
@@ -848,9 +848,9 @@ Save the history list to the opened
 .Ft FILE
 pointer
 .Fa fp .
-.It Dv H_SAVE_FP_INCR , Fa "FILE *fp"
-Append the history list since the previous event
-.Fa ev
+.It Dv H_SAVE_FP_INCR , Fa "FILE *fp" , Fa "int e"
+Append the history list since the event numbered
+.Fa e
 to the opened
 .Ft FILE
 pointer
diff --git a/bin/cash/libedit/histedit.h b/bin/cash/libedit/histedit.h
index a6bdee48..ecb42686 100644
--- a/bin/cash/libedit/histedit.h
+++ b/bin/cash/libedit/histedit.h
@@ -226,8 +226,8 @@ int		history(History *, HistEvent *, int, ...);
 #define	H_DELDATA	24	/* , int, histdata_t *);*/
 #define	H_REPLACE	25	/* , const char *, histdata_t);	*/
 #define	H_SAVE_FP	26	/* , FILE *);		*/
-#define H_SAVE_INCR	27	/* , const char *);	*/
-#define H_SAVE_FP_INCR	28	/* , FILE *);		*/
+#define H_SAVE_INCR	27	/* , const char *, int);	*/
+#define H_SAVE_FP_INCR	28	/* , FILE *, int);	*/
 
 
 
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;