Module Name:    src
Committed By:   christos
Date:           Mon Sep  7 21:24:34 UTC 2009

Modified Files:
        src/lib/libedit: histedit.h history.c readline.c
        src/lib/libedit/readline: readline.h

Log Message:
apply apple patches from:
http://opensource.apple.com/source/libedit/libedit-11/patches/


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/lib/libedit/histedit.h
cvs rdiff -u -r1.33 -r1.34 src/lib/libedit/history.c
cvs rdiff -u -r1.84 -r1.85 src/lib/libedit/readline.c
cvs rdiff -u -r1.29 -r1.30 src/lib/libedit/readline/readline.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libedit/histedit.h
diff -u src/lib/libedit/histedit.h:1.40 src/lib/libedit/histedit.h:1.41
--- src/lib/libedit/histedit.h:1.40	Mon May 11 14:33:30 2009
+++ src/lib/libedit/histedit.h	Mon Sep  7 17:24:33 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: histedit.h,v 1.40 2009/05/11 18:33:30 christos Exp $	*/
+/*	$NetBSD: histedit.h,v 1.41 2009/09/07 21:24:33 christos Exp $	*/
 
 /*-
  * Copyright (c) 1992, 1993
@@ -204,6 +204,10 @@
 #define	H_SETUNIQUE	20	/* , int);		*/
 #define	H_GETUNIQUE	21	/* , void);		*/
 #define	H_DEL		22	/* , int);		*/
+#define	H_NEXT_EVDATA	23	/* , const int, histdata_t *);	*/
+#define	H_DELDATA	24	/* , int, histdata_t *);*/
+#define	H_REPLACE	25	/* , const char *, histdata_t);	*/
+
 
 
 /*

Index: src/lib/libedit/history.c
diff -u src/lib/libedit/history.c:1.33 src/lib/libedit/history.c:1.34
--- src/lib/libedit/history.c:1.33	Fri Feb  6 09:40:32 2009
+++ src/lib/libedit/history.c	Mon Sep  7 17:24:33 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: history.c,v 1.33 2009/02/06 14:40:32 sketch Exp $	*/
+/*	$NetBSD: history.c,v 1.34 2009/09/07 21:24:33 christos Exp $	*/
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)history.c	8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: history.c,v 1.33 2009/02/06 14:40:32 sketch Exp $");
+__RCSID("$NetBSD: history.c,v 1.34 2009/09/07 21:24:33 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -121,6 +121,7 @@
  */
 typedef struct hentry_t {
 	HistEvent ev;		/* What we return		 */
+	void *data;		/* data				 */
 	struct hentry_t *next;	/* Next entry			 */
 	struct hentry_t *prev;	/* Previous entry		 */
 } hentry_t;
@@ -150,6 +151,9 @@
 private int history_def_insert(history_t *, HistEvent *, const char *);
 private void history_def_delete(history_t *, HistEvent *, hentry_t *);
 
+private int history_deldata_nth(history_t *, HistEvent *, int, void **);
+private int history_set_nth(ptr_t, HistEvent *, int);
+
 #define	history_def_setsize(p, num)(void) (((history_t *)p)->max = (num))
 #define	history_def_getsize(p)  (((history_t *)p)->cur)
 #define	history_def_getunique(p) (((((history_t *)p)->flags) & H_UNIQUE) != 0)
@@ -340,6 +344,31 @@
 }
 
 
+/* history_set_nth():
+ *	Default function to set the current event in the history to the
+ *	n-th one.
+ */
+private int
+history_set_nth(ptr_t p, HistEvent *ev, int n)
+{
+	history_t *h = (history_t *) p;
+
+	if (h->cur == 0) {
+		he_seterrev(ev, _HE_EMPTY_LIST);
+		return (-1);
+	}
+	for (h->cursor = h->list.prev; h->cursor != &h->list;
+	    h->cursor = h->cursor->prev)
+		if (n-- <= 0)
+			break;
+	if (h->cursor == &h->list) {
+		he_seterrev(ev, _HE_NOT_FOUND);
+		return (-1);
+	}
+	return (0);
+}
+
+
 /* history_def_add():
  *	Append string to element
  */
@@ -368,6 +397,24 @@
 }
 
 
+private int
+history_deldata_nth(history_t *h, HistEvent *ev,
+    int num, void **data)
+{
+	if (history_set_nth(h, ev, num) != 0)
+		return (-1);
+	/* magic value to skip delete (just set to n-th history) */
+	if (data == (void **)-1)
+		return (0);
+	ev->str = strdup(h->cursor->ev.str);
+	ev->num = h->cursor->ev.num;
+	if (data)
+		*data = h->cursor->data;
+	history_def_delete(h, ev, h->cursor);
+	return (0);
+}
+
+
 /* history_def_del():
  *	Delete element hp of the h list
  */
@@ -397,8 +444,11 @@
 	HistEventPrivate *evp = (void *)&hp->ev;
 	if (hp == &h->list)
 		abort();
-	if (h->cursor == hp)
+	if (h->cursor == hp) {
 		h->cursor = hp->prev;
+		if (h->cursor == &h->list)
+			h->cursor = hp->next;
+	}
 	hp->prev->next = hp->next;
 	hp->next->prev = hp->prev;
 	h_free((ptr_t) evp->str);
@@ -421,6 +471,7 @@
 		h_free((ptr_t)h->cursor);
 		goto oomem;
 	}
+	h->cursor->data = NULL;
 	h->cursor->ev.num = ++h->eventid;
 	h->cursor->next = h->list.next;
 	h->cursor->prev = &h->list;
@@ -792,6 +843,23 @@
 }
 
 
+private int
+history_next_evdata(History *h, HistEvent *ev, int num, void **d)
+{
+	int retval;
+
+	for (retval = HCURR(h, ev); retval != -1; retval = HPREV(h, ev))
+		if (num-- <= 0) {
+			if (d)
+				*d = ((history_t *)h->h_ref)->cursor->data;
+			return (0);
+		}
+
+	he_seterrev(ev, _HE_NOT_FOUND);
+	return (-1);
+}
+
+
 /* history_next_event():
  *	Find the next event, with number given
  */
@@ -981,11 +1049,42 @@
 		retval = 0;
 		break;
 
+	case H_NEXT_EVDATA:
+	{
+		int num = va_arg(va, int);
+		void **d = va_arg(va, void **);
+		retval = history_next_evdata(h, ev, num, d);
+		break;
+	}
+
+	case H_DELDATA:
+	{
+		int num = va_arg(va, int);
+		void **d = va_arg(va, void **);
+		retval = history_deldata_nth((history_t *)h->h_ref, ev, num, d);
+		break;
+	}
+
+	case H_REPLACE: /* only use after H_NEXT_EVDATA */
+	{
+		const char *line = va_arg(va, const char *);
+		void *d = va_arg(va, void *);
+		const char *s;
+		if(!line || !(s = strdup(line))) {
+			retval = -1;
+			break;
+		}
+		((history_t *)h->h_ref)->cursor->ev.str = s;
+		((history_t *)h->h_ref)->cursor->data = d;
+		retval = 0;
+		break;
+	}
+
 	default:
 		retval = -1;
 		he_seterrev(ev, _HE_UNKNOWN);
 		break;
 	}
 	va_end(va);
-	return (retval);
+	return retval;
 }

Index: src/lib/libedit/readline.c
diff -u src/lib/libedit/readline.c:1.84 src/lib/libedit/readline.c:1.85
--- src/lib/libedit/readline.c:1.84	Wed Jul 22 11:57:40 2009
+++ src/lib/libedit/readline.c	Mon Sep  7 17:24:33 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: readline.c,v 1.84 2009/07/22 15:57:40 christos Exp $	*/
+/*	$NetBSD: readline.c,v 1.85 2009/09/07 21:24:33 christos Exp $	*/
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include "config.h"
 #if !defined(lint) && !defined(SCCSID)
-__RCSID("$NetBSD: readline.c,v 1.84 2009/07/22 15:57:40 christos Exp $");
+__RCSID("$NetBSD: readline.c,v 1.85 2009/09/07 21:24:33 christos Exp $");
 #endif /* not lint && not SCCSID */
 
 #include <sys/types.h>
@@ -70,6 +70,7 @@
 /* readline compatibility stuff - look at readline sources/documentation */
 /* to see what these variables mean */
 const char *rl_library_version = "EditLine wrapper";
+int rl_readline_version = RL_READLINE_VERSION;
 static char empty[] = { '\0' };
 static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };
 static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
@@ -118,6 +119,7 @@
 VFunction *rl_completion_display_matches_hook = NULL;
 VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal;
 VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal;
+KEYMAP_ENTRY_ARRAY emacs_meta_keymap;
 
 /*
  * The current prompt string.
@@ -214,6 +216,22 @@
 	return 1;
 }
 
+static const char _dothistory[] = "/.history";
+
+static const char *
+_default_history_file(void)
+{
+	struct passwd *p;
+	static char path[PATH_MAX];
+
+	if (*path)
+		return path;
+	if ((p = getpwuid(getuid())) == NULL)
+		return NULL;
+	strlcpy(path, p->pw_dir, PATH_MAX);
+	strlcat(path, _dothistory, PATH_MAX);
+	return path;
+}
 
 /*
  * READLINE compatibility stuff
@@ -648,9 +666,11 @@
 		return(-1);
 
 	if (!has_mods) {
-		*result = strdup(aptr? aptr : ptr);
+		*result = strdup(aptr ? aptr : ptr);
 		if (aptr)
 			free(aptr);
+		if (*result == NULL)
+			return -1;
 		return(1);
 	}
 
@@ -1125,6 +1145,142 @@
 	return (max_input_history != INT_MAX);
 }
 
+static const char _history_tmp_template[] = "/tmp/.historyXXXXXX";
+
+int
+history_truncate_file (const char *filename, int nlines)
+{
+	int ret = 0;
+	FILE *fp, *tp;
+	char template[sizeof(_history_tmp_template)];
+	char buf[4096];
+	int fd;
+	char *cp;
+	off_t off;
+	int count = 0;
+	ssize_t left = 0;
+
+	if (filename == NULL && (filename = _default_history_file()) == NULL)
+		return errno;
+	if ((fp = fopen(filename, "r+")) == NULL)
+		return errno;
+	strcpy(template, _history_tmp_template);
+	if ((fd = mkstemp(template)) == -1) {
+		ret = errno;
+		goto out1;
+	}
+
+	if ((tp = fdopen(fd, "r+")) == NULL) {
+		close(fd);
+		ret = errno;
+		goto out2;
+	}
+
+	for(;;) {
+		if (fread(buf, sizeof(buf), 1, fp) != 1) {
+			if (ferror(fp)) {
+				ret = errno;
+				break;
+			}
+			if (fseeko(fp, (off_t)sizeof(buf) * count, SEEK_SET) ==
+			    (off_t)-1) {
+				ret = errno;
+				break;
+			}
+			left = fread(buf, 1, sizeof(buf), fp);
+			if (ferror(fp)) {
+				ret = errno;
+				break;
+			}
+			if (left == 0) {
+				count--;
+				left = sizeof(buf);
+			} else if (fwrite(buf, (size_t)left, 1, tp) != 1) {
+				ret = errno;
+				break;
+			}
+			fflush(tp);
+			break;
+		}
+		if (fwrite(buf, sizeof(buf), 1, tp) != 1) {
+			ret = errno;
+			break;
+		}
+		count++;
+	}
+	if (ret)
+		goto out3;
+	cp = buf + left - 1;
+	if(*cp != '\n')
+		cp++;
+	for(;;) {
+		while (--cp >= buf) {
+			if (*cp == '\n') {
+				if (--nlines == 0) {
+					if (++cp >= buf + sizeof(buf)) {
+						count++;
+						cp = buf;
+					}
+					break;
+				}
+			}
+		}
+		if (nlines <= 0 || count == 0)
+			break;
+		count--;
+		if (fseeko(tp, (off_t)sizeof(buf) * count, SEEK_SET) < 0) {
+			ret = errno;
+			break;
+		}
+		if (fread(buf, sizeof(buf), 1, tp) != 1) {
+			if (ferror(tp)) {
+				ret = errno;
+				break;
+			}
+			ret = EAGAIN;
+			break;
+		}
+		cp = buf + sizeof(buf);
+	}
+
+	if (ret || nlines > 0)
+		goto out3;
+
+	if (fseeko(fp, 0, SEEK_SET) == (off_t)-1) {
+		ret = errno;
+		goto out3;
+	}
+
+	if (fseeko(tp, (off_t)sizeof(buf) * count + (cp - buf), SEEK_SET) ==
+	    (off_t)-1) {
+		ret = errno;
+		goto out3;
+	}
+
+	for(;;) {
+		if ((left = fread(buf, 1, sizeof(buf), tp)) == 0) {
+			if (ferror(fp))
+				ret = errno;
+			break;
+		}
+		if (fwrite(buf, (size_t)left, 1, fp) != 1) {
+			ret = errno;
+			break;
+		}
+	}
+	fflush(fp);
+	if((off = ftello(fp)) > 0)
+		(void)ftruncate(fileno(fp), off);
+out3:
+	fclose(tp);
+out2:
+	unlink(template);
+out1:
+	fclose(fp);
+
+	return ret;
+}
+
 
 /*
  * read history from a file given
@@ -1136,7 +1292,9 @@
 
 	if (h == NULL || e == NULL)
 		rl_initialize();
-	return (history(h, &ev, H_LOAD, filename) == -1);
+	if (filename == NULL && (filename = _default_history_file()) == NULL)
+		return errno;
+	return (history(h, &ev, H_LOAD, filename) == -1 ? (errno ? errno : EINVAL) : 0);
 }
 
 
@@ -1150,7 +1308,9 @@
 
 	if (h == NULL || e == NULL)
 		rl_initialize();
-	return (history(h, &ev, H_SAVE, filename) == -1);
+	if (filename == NULL && (filename = _default_history_file()) == NULL)
+		return errno;
+	return (history(h, &ev, H_SAVE, filename) == -1 ? (errno ? errno : EINVAL) : 0);
 }
 
 
@@ -1174,16 +1334,15 @@
 		return (NULL);
 	curr_num = ev.num;
 
-	/* start from most recent */
-	if (history(h, &ev, H_FIRST) != 0)
+	/* start from the oldest */
+	if (history(h, &ev, H_LAST) != 0)
 		return (NULL);	/* error */
 
-	/* look backwards for event matching specified offset */
-	if (history(h, &ev, H_NEXT_EVENT, num + 1))
+	/* look forwards for event matching specified offset */
+	if (history(h, &ev, H_NEXT_EVDATA, num, &she.data))
 		return (NULL);
 
 	she.line = ev.str;
-	she.data = NULL;
 
 	/* restore pointer to where it was */
 	(void)history(h, &ev, H_SET, curr_num);
@@ -1217,26 +1376,75 @@
 HIST_ENTRY *
 remove_history(int num)
 {
-	HIST_ENTRY *she;
+	HIST_ENTRY *he;
 	HistEvent ev;
 
 	if (h == NULL || e == NULL)
 		rl_initialize();
 
-	if (history(h, &ev, H_DEL, num) != 0)
+	if ((he = malloc(sizeof(*he))) == NULL)
 		return NULL;
 
-	if ((she = malloc(sizeof(*she))) == NULL)
+	if (history(h, &ev, H_DELDATA, num, &he->data) != 0) {
+		free(he);
 		return NULL;
+	}
 
-	she->line = ev.str;
-	she->data = NULL;
+	he->line = ev.str;
+	if (history(h, &ev, H_GETSIZE) == 0)
+		history_length = ev.num;
 
-	return she;
+	return he;
 }
 
 
 /*
+ * replace the line and data of the num-th entry
+ */
+HIST_ENTRY *
+replace_history_entry(int num, const char *line, histdata_t data)
+{
+	HIST_ENTRY *he;
+	HistEvent ev;
+	int curr_num;
+
+	if (h == NULL || e == NULL)
+		rl_initialize();
+
+	/* save current position */
+	if (history(h, &ev, H_CURR) != 0)
+		return NULL;
+	curr_num = ev.num;
+
+	/* start from the oldest */
+	if (history(h, &ev, H_LAST) != 0)
+		return NULL;	/* error */
+
+	if ((he = malloc(sizeof(*he))) == NULL)
+		return NULL;
+
+	/* look forwards for event matching specified offset */
+	if (history(h, &ev, H_NEXT_EVDATA, num, &he->data))
+		goto out;
+
+	he->line = strdup(ev.str);
+	if (he->line == NULL)
+		goto out;
+
+	if (history(h, &ev, H_REPLACE, line, data))
+		goto out;
+
+	/* restore pointer to where it was */
+	if (history(h, &ev, H_SET, curr_num))
+		goto out;
+
+	return he;
+out:
+	free(he);
+	return NULL;
+}
+
+/*
  * clear the history list - delete all entries
  */
 void
@@ -1245,6 +1453,7 @@
 	HistEvent ev;
 
 	history(h, &ev, H_CLEAR);
+	history_length = 0;
 }
 
 
@@ -1317,13 +1526,17 @@
 	HistEvent ev;
 	int curr_num;
 
-	if (pos > history_length || pos < 0)
+	if (pos >= history_length || pos < 0)
 		return (-1);
 
 	history(h, &ev, H_CURR);
 	curr_num = ev.num;
 
-	if (history(h, &ev, H_SET, pos)) {
+	/*
+	 * use H_DELDATA to set to nth history (without delete) by passing
+	 * (void **)-1
+	 */
+	if (history(h, &ev, H_DELDATA, pos, (void **)-1)) {
 		history(h, &ev, H_SET, curr_num);
 		return(-1);
 	}
@@ -1470,9 +1683,9 @@
 
 	if (pwd == NULL) {
 		endpwent();
-		return (NULL);
+		return NULL;
 	}
-	return (strdup(pwd->pw_name));
+	return strdup(pwd->pw_name);
 }
 
 
@@ -1556,7 +1769,7 @@
  * bind key c to readline-type function func
  */
 int
-rl_bind_key(int c, int func(int, int))
+rl_bind_key(int c, rl_command_func_t *func)
 {
 	int retval = -1;
 
@@ -1623,6 +1836,20 @@
 	return (0);
 }
 
+int
+rl_insert_text(const char *text)
+{
+	if (!text || *text == 0)
+		return (0);
+
+	if (h == NULL || e == NULL)
+		rl_initialize();
+
+	if (el_insertstr(e, text) < 0)
+		return (0);
+	return (int)strlen(text);
+}
+
 /*ARGSUSED*/
 int
 rl_newline(int count, int c)
@@ -1686,7 +1913,7 @@
 		} else
 			wbuf = NULL;
 		(*(void (*)(const char *))rl_linefunc)(wbuf);
-		el_set(e, EL_UNBUFFERED, 1);
+		//el_set(e, EL_UNBUFFERED, 1);
 	}
 }
 
@@ -1933,6 +2160,17 @@
 	return strcoll(*s1, *s2);
 }
 
+HISTORY_STATE *
+history_get_history_state(void)
+{
+	HISTORY_STATE *hs;
+
+	if ((hs = malloc(sizeof(HISTORY_STATE))) == NULL)
+		return (NULL);
+	hs->length = history_length;
+	return (hs);
+}
+
 int
 /*ARGSUSED*/
 rl_kill_text(int from, int to)
@@ -1967,7 +2205,13 @@
 
 int
 /*ARGSUSED*/
-rl_bind_key_in_map(int key, Function *fun, Keymap k)
+rl_bind_key_in_map(int key, rl_command_func_t *fun, Keymap k)
 {
 	return 0;
 }
+
+/* unsupported, but needed by python */
+void
+rl_cleanup_after_signal(void)
+{
+}

Index: src/lib/libedit/readline/readline.h
diff -u src/lib/libedit/readline/readline.h:1.29 src/lib/libedit/readline/readline.h:1.30
--- src/lib/libedit/readline/readline.h:1.29	Sun Aug 30 20:05:43 2009
+++ src/lib/libedit/readline/readline.h	Mon Sep  7 17:24:34 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: readline.h,v 1.29 2009/08/31 00:05:43 christos Exp $	*/
+/*	$NetBSD: readline.h,v 1.30 2009/09/07 21:24:34 christos Exp $	*/
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -43,10 +43,18 @@
 typedef char	 *CPFunction(const char *, int);
 typedef char	**CPPFunction(const char *, int, int);
 typedef char     *rl_compentry_func_t(const char *, int);
+typedef int	  rl_command_func_t(int, int);
+
+/* only supports length */
+typedef struct {
+	int length;
+} HISTORY_STATE;
+
+typedef void *histdata_t;
 
 typedef struct _hist_entry {
 	const char	*line;
-	const char	*data;
+	histdata_t 	 data;
 } HIST_ENTRY;
 
 typedef struct _keymap_entry {
@@ -80,6 +88,7 @@
 
 #define RUBOUT		0x7f
 #define ABORT_CHAR	CTRL('G')
+#define RL_READLINE_VERSION 	0x0402
 #define RL_PROMPT_START_IGNORE	'\1'
 #define RL_PROMPT_END_IGNORE	'\2'
 
@@ -88,6 +97,7 @@
 extern "C" {
 #endif
 extern const char	*rl_library_version;
+extern int 		rl_readline_version; 
 extern char		*rl_readline_name;
 extern FILE		*rl_instream;
 extern FILE		*rl_outstream;
@@ -141,6 +151,8 @@
 HIST_ENTRY	*current_history(void);
 HIST_ENTRY	*history_get(int);
 HIST_ENTRY	*remove_history(int);
+/*###152 [lint] syntax error 'histdata_t' [249]%%%*/
+HIST_ENTRY	*replace_history_entry(int, const char *, histdata_t);
 int		 history_total_bytes(void);
 int		 history_set_pos(int);
 HIST_ENTRY	*previous_history(void);
@@ -150,6 +162,7 @@
 int		 history_search_pos(const char *, int, int);
 int		 read_history(const char *);
 int		 write_history(const char *);
+int		 history_truncate_file (const char *, int);
 int		 history_expand(char *, char **);
 char	       **history_tokenize(const char *);
 const char	*get_history_event(const char *, int *, int);
@@ -164,8 +177,9 @@
 void		 rl_display_match_list(char **, int, int);
 
 int		 rl_insert(int, int);
+int		 rl_insert_text(const char *);
 void		 rl_reset_terminal(const char *);
-int		 rl_bind_key(int, int (*)(int, int));
+int		 rl_bind_key(int, rl_command_func_t *);
 int		 rl_newline(int, int);
 void		 rl_callback_read_char(void);
 void		 rl_callback_handler_install(const char *, VCPFunction *);
@@ -179,6 +193,7 @@
 int		 rl_variable_bind(const char *, const char *);
 void		 rl_stuff_char(int);
 int		 rl_add_defun(const char *, Function *, int);
+HISTORY_STATE	*history_get_history_state(void);
 void		 rl_get_screen_size(int *, int *);
 void		 rl_set_screen_size(int, int);
 char 		*rl_filename_completion_function (const char *, int);
@@ -196,7 +211,9 @@
 void		 rl_set_keymap(Keymap);
 Keymap		 rl_make_bare_keymap(void);
 int		 rl_generic_bind(int, const char *, const char *, Keymap);
-int		 rl_bind_key_in_map(int, Function *, Keymap);
+int		 rl_bind_key_in_map(int, rl_command_func_t *, Keymap);
+void		 rl_cleanup_after_signal(void);
+void		 rl_free_line_state(void);
 #ifdef __cplusplus
 }
 #endif

Reply via email to