xref: /netbsd-src/lib/libedit/readline.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: readline.c,v 1.147 2018/06/09 17:41:55 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jaromir Dolecek.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "config.h"
33 #if !defined(lint) && !defined(SCCSID)
34 __RCSID("$NetBSD: readline.c,v 1.147 2018/06/09 17:41:55 christos Exp $");
35 #endif /* not lint && not SCCSID */
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <ctype.h>
40 #include <dirent.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <pwd.h>
45 #include <setjmp.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <vis.h>
52 
53 #include "readline/readline.h"
54 #include "el.h"
55 #include "fcns.h"
56 #include "filecomplete.h"
57 
58 void rl_prep_terminal(int);
59 void rl_deprep_terminal(void);
60 
61 /* for rl_complete() */
62 #define TAB		'\r'
63 
64 /* see comment at the #ifdef for sense of this */
65 /* #define GDB_411_HACK */
66 
67 /* readline compatibility stuff - look at readline sources/documentation */
68 /* to see what these variables mean */
69 const char *rl_library_version = "EditLine wrapper";
70 int rl_readline_version = RL_READLINE_VERSION;
71 static char empty[] = { '\0' };
72 static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };
73 static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
74     '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
75 char *rl_readline_name = empty;
76 FILE *rl_instream = NULL;
77 FILE *rl_outstream = NULL;
78 int rl_point = 0;
79 int rl_end = 0;
80 char *rl_line_buffer = NULL;
81 rl_vcpfunc_t *rl_linefunc = NULL;
82 int rl_done = 0;
83 rl_hook_func_t *rl_event_hook = NULL;
84 KEYMAP_ENTRY_ARRAY emacs_standard_keymap,
85     emacs_meta_keymap,
86     emacs_ctlx_keymap;
87 /*
88  * The following is not implemented; we always catch signals in the
89  * libedit fashion: set handlers on entry to el_gets() and clear them
90  * on the way out. This simplistic approach works for most cases; if
91  * it does not work for your application, please let us know.
92  */
93 int rl_catch_signals = 1;
94 int rl_catch_sigwinch = 1;
95 
96 int history_base = 1;		/* probably never subject to change */
97 int history_length = 0;
98 int history_offset = 0;
99 int max_input_history = 0;
100 char history_expansion_char = '!';
101 char history_subst_char = '^';
102 char *history_no_expand_chars = expand_chars;
103 Function *history_inhibit_expansion_function = NULL;
104 char *history_arg_extract(int start, int end, const char *str);
105 
106 int rl_inhibit_completion = 0;
107 int rl_attempted_completion_over = 0;
108 char *rl_basic_word_break_characters = break_chars;
109 char *rl_completer_word_break_characters = NULL;
110 char *rl_completer_quote_characters = NULL;
111 rl_compentry_func_t *rl_completion_entry_function = NULL;
112 char *(*rl_completion_word_break_hook)(void) = NULL;
113 rl_completion_func_t *rl_attempted_completion_function = NULL;
114 Function *rl_pre_input_hook = NULL;
115 Function *rl_startup1_hook = NULL;
116 int (*rl_getc_function)(FILE *) = NULL;
117 char *rl_terminal_name = NULL;
118 int rl_already_prompted = 0;
119 int rl_filename_completion_desired = 0;
120 int rl_ignore_completion_duplicates = 0;
121 int readline_echoing_p = 1;
122 int _rl_print_completions_horizontally = 0;
123 VFunction *rl_redisplay_function = NULL;
124 Function *rl_startup_hook = NULL;
125 int rl_did_startup_hook = 0;
126 VFunction *rl_completion_display_matches_hook = NULL;
127 VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal;
128 VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal;
129 KEYMAP_ENTRY_ARRAY emacs_meta_keymap;
130 
131 /*
132  * The current prompt string.
133  */
134 char *rl_prompt = NULL;
135 /*
136  * This is set to character indicating type of completion being done by
137  * rl_complete_internal(); this is available for application completion
138  * functions.
139  */
140 int rl_completion_type = 0;
141 
142 /*
143  * If more than this number of items results from query for possible
144  * completions, we ask user if they are sure to really display the list.
145  */
146 int rl_completion_query_items = 100;
147 
148 /*
149  * List of characters which are word break characters, but should be left
150  * in the parsed text when it is passed to the completion function.
151  * Shell uses this to help determine what kind of completing to do.
152  */
153 char *rl_special_prefixes = NULL;
154 
155 /*
156  * This is the character appended to the completed words if at the end of
157  * the line. Default is ' ' (a space).
158  */
159 int rl_completion_append_character = ' ';
160 
161 /* stuff below is used internally by libedit for readline emulation */
162 
163 static History *h = NULL;
164 static EditLine *e = NULL;
165 static rl_command_func_t *map[256];
166 static jmp_buf topbuf;
167 
168 /* internal functions */
169 static unsigned char	 _el_rl_complete(EditLine *, int);
170 static unsigned char	 _el_rl_tstp(EditLine *, int);
171 static char		*_get_prompt(EditLine *);
172 static int		 _getc_function(EditLine *, wchar_t *);
173 static int		 _history_expand_command(const char *, size_t, size_t,
174     char **);
175 static char		*_rl_compat_sub(const char *, const char *,
176     const char *, int);
177 static int		 _rl_event_read_char(EditLine *, wchar_t *);
178 static void		 _rl_update_pos(void);
179 
180 static HIST_ENTRY rl_he;
181 
182 /* ARGSUSED */
183 static char *
184 _get_prompt(EditLine *el __attribute__((__unused__)))
185 {
186 	rl_already_prompted = 1;
187 	return rl_prompt;
188 }
189 
190 
191 /*
192  * read one key from user defined input function
193  */
194 static int
195 /*ARGSUSED*/
196 _getc_function(EditLine *el __attribute__((__unused__)), wchar_t *c)
197 {
198 	int i;
199 
200 	i = (*rl_getc_function)(rl_instream);
201 	if (i == -1)
202 		return 0;
203 	*c = (wchar_t)i;
204 	return 1;
205 }
206 
207 static void
208 _resize_fun(EditLine *el, void *a)
209 {
210 	const LineInfo *li;
211 	char **ap = a;
212 
213 	li = el_line(el);
214 	/* a cheesy way to get rid of const cast. */
215 	*ap = memchr(li->buffer, *li->buffer, (size_t)1);
216 }
217 
218 static const char *
219 _default_history_file(void)
220 {
221 	struct passwd *p;
222 	static char *path;
223 	size_t len;
224 
225 	if (path)
226 		return path;
227 
228 	if ((p = getpwuid(getuid())) == NULL)
229 		return NULL;
230 
231 	len = strlen(p->pw_dir) + sizeof("/.history");
232 	if ((path = malloc(len)) == NULL)
233 		return NULL;
234 
235 	(void)snprintf(path, len, "%s/.history", p->pw_dir);
236 	return path;
237 }
238 
239 /*
240  * READLINE compatibility stuff
241  */
242 
243 /*
244  * Set the prompt
245  */
246 int
247 rl_set_prompt(const char *prompt)
248 {
249 	char *p;
250 
251 	if (!prompt)
252 		prompt = "";
253 	if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0)
254 		return 0;
255 	if (rl_prompt)
256 		el_free(rl_prompt);
257 	rl_prompt = strdup(prompt);
258 	if (rl_prompt == NULL)
259 		return -1;
260 
261 	while ((p = strchr(rl_prompt, RL_PROMPT_END_IGNORE)) != NULL)
262 		*p = RL_PROMPT_START_IGNORE;
263 
264 	return 0;
265 }
266 
267 /*
268  * initialize rl compat stuff
269  */
270 int
271 rl_initialize(void)
272 {
273 	HistEvent ev;
274 	int editmode = 1;
275 	struct termios t;
276 
277 	if (e != NULL)
278 		el_end(e);
279 	if (h != NULL)
280 		history_end(h);
281 
282 	if (!rl_instream)
283 		rl_instream = stdin;
284 	if (!rl_outstream)
285 		rl_outstream = stdout;
286 
287 	/*
288 	 * See if we don't really want to run the editor
289 	 */
290 	if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0)
291 		editmode = 0;
292 
293 	e = el_init_internal(rl_readline_name, rl_instream, rl_outstream,
294 	    stderr, fileno(rl_instream), fileno(rl_outstream), fileno(stderr),
295 	    NO_RESET);
296 
297 	if (!editmode)
298 		el_set(e, EL_EDITMODE, 0);
299 
300 	h = history_init();
301 	if (!e || !h)
302 		return -1;
303 
304 	history(h, &ev, H_SETSIZE, INT_MAX);	/* unlimited */
305 	history_length = 0;
306 	max_input_history = INT_MAX;
307 	el_set(e, EL_HIST, history, h);
308 
309 	/* Setup resize function */
310 	el_set(e, EL_RESIZE, _resize_fun, &rl_line_buffer);
311 
312 	/* setup getc function if valid */
313 	if (rl_getc_function)
314 		el_set(e, EL_GETCFN, _getc_function);
315 
316 	/* for proper prompt printing in readline() */
317 	if (rl_set_prompt("") == -1) {
318 		history_end(h);
319 		el_end(e);
320 		return -1;
321 	}
322 	el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE);
323 	el_set(e, EL_SIGNAL, rl_catch_signals);
324 
325 	/* set default mode to "emacs"-style and read setting afterwards */
326 	/* so this can be overridden */
327 	el_set(e, EL_EDITOR, "emacs");
328 	if (rl_terminal_name != NULL)
329 		el_set(e, EL_TERMINAL, rl_terminal_name);
330 	else
331 		el_get(e, EL_TERMINAL, &rl_terminal_name);
332 
333 	/*
334 	 * Word completion - this has to go AFTER rebinding keys
335 	 * to emacs-style.
336 	 */
337 	el_set(e, EL_ADDFN, "rl_complete",
338 	    "ReadLine compatible completion function",
339 	    _el_rl_complete);
340 	el_set(e, EL_BIND, "^I", "rl_complete", NULL);
341 
342 	/*
343 	 * Send TSTP when ^Z is pressed.
344 	 */
345 	el_set(e, EL_ADDFN, "rl_tstp",
346 	    "ReadLine compatible suspend function",
347 	    _el_rl_tstp);
348 	el_set(e, EL_BIND, "^Z", "rl_tstp", NULL);
349 
350 	/*
351 	 * Set some readline compatible key-bindings.
352 	 */
353 	el_set(e, EL_BIND, "^R", "em-inc-search-prev", NULL);
354 
355 	/*
356 	 * Allow the use of Home/End keys.
357 	 */
358 	el_set(e, EL_BIND, "\\e[1~", "ed-move-to-beg", NULL);
359 	el_set(e, EL_BIND, "\\e[4~", "ed-move-to-end", NULL);
360 	el_set(e, EL_BIND, "\\e[7~", "ed-move-to-beg", NULL);
361 	el_set(e, EL_BIND, "\\e[8~", "ed-move-to-end", NULL);
362 	el_set(e, EL_BIND, "\\e[H", "ed-move-to-beg", NULL);
363 	el_set(e, EL_BIND, "\\e[F", "ed-move-to-end", NULL);
364 
365 	/*
366 	 * Allow the use of the Delete/Insert keys.
367 	 */
368 	el_set(e, EL_BIND, "\\e[3~", "ed-delete-next-char", NULL);
369 	el_set(e, EL_BIND, "\\e[2~", "ed-quoted-insert", NULL);
370 
371 	/*
372 	 * Ctrl-left-arrow and Ctrl-right-arrow for word moving.
373 	 */
374 	el_set(e, EL_BIND, "\\e[1;5C", "em-next-word", NULL);
375 	el_set(e, EL_BIND, "\\e[1;5D", "ed-prev-word", NULL);
376 	el_set(e, EL_BIND, "\\e[5C", "em-next-word", NULL);
377 	el_set(e, EL_BIND, "\\e[5D", "ed-prev-word", NULL);
378 	el_set(e, EL_BIND, "\\e\\e[C", "em-next-word", NULL);
379 	el_set(e, EL_BIND, "\\e\\e[D", "ed-prev-word", NULL);
380 
381 	/* read settings from configuration file */
382 	el_source(e, NULL);
383 
384 	/*
385 	 * Unfortunately, some applications really do use rl_point
386 	 * and rl_line_buffer directly.
387 	 */
388 	_resize_fun(e, &rl_line_buffer);
389 	_rl_update_pos();
390 
391 	tty_end(e, TCSADRAIN);
392 
393 	return 0;
394 }
395 
396 
397 /*
398  * read one line from input stream and return it, chomping
399  * trailing newline (if there is any)
400  */
401 char *
402 readline(const char *p)
403 {
404 	HistEvent ev;
405 	const char * volatile prompt = p;
406 	int count;
407 	const char *ret;
408 	char *buf;
409 	static int used_event_hook;
410 
411 	if (e == NULL || h == NULL)
412 		rl_initialize();
413 	if (rl_did_startup_hook == 0 && rl_startup_hook) {
414 		rl_did_startup_hook = 1;
415 		(*rl_startup_hook)(NULL, 0);
416 	}
417 	tty_init(e);
418 
419 
420 	rl_done = 0;
421 
422 	(void)setjmp(topbuf);
423 	buf = NULL;
424 
425 	/* update prompt accordingly to what has been passed */
426 	if (rl_set_prompt(prompt) == -1)
427 		goto out;
428 
429 	if (rl_pre_input_hook)
430 		(*rl_pre_input_hook)(NULL, 0);
431 
432 	if (rl_event_hook && !(e->el_flags & NO_TTY)) {
433 		el_set(e, EL_GETCFN, _rl_event_read_char);
434 		used_event_hook = 1;
435 	}
436 
437 	if (!rl_event_hook && used_event_hook) {
438 		el_set(e, EL_GETCFN, EL_BUILTIN_GETCFN);
439 		used_event_hook = 0;
440 	}
441 
442 	rl_already_prompted = 0;
443 
444 	/* get one line from input stream */
445 	ret = el_gets(e, &count);
446 
447 	if (ret && count > 0) {
448 		int lastidx;
449 
450 		buf = strdup(ret);
451 		if (buf == NULL)
452 			goto out;
453 		lastidx = count - 1;
454 		if (buf[lastidx] == '\n')
455 			buf[lastidx] = '\0';
456 	} else
457 		buf = NULL;
458 
459 	history(h, &ev, H_GETSIZE);
460 	history_length = ev.num;
461 
462 out:
463 	tty_end(e, TCSADRAIN);
464 	return buf;
465 }
466 
467 /*
468  * history functions
469  */
470 
471 /*
472  * is normally called before application starts to use
473  * history expansion functions
474  */
475 void
476 using_history(void)
477 {
478 	if (h == NULL || e == NULL)
479 		rl_initialize();
480 	history_offset = history_length;
481 }
482 
483 
484 /*
485  * substitute ``what'' with ``with'', returning resulting string; if
486  * globally == 1, substitutes all occurrences of what, otherwise only the
487  * first one
488  */
489 static char *
490 _rl_compat_sub(const char *str, const char *what, const char *with,
491     int globally)
492 {
493 	const	char	*s;
494 	char	*r, *result;
495 	size_t	len, with_len, what_len;
496 
497 	len = strlen(str);
498 	with_len = strlen(with);
499 	what_len = strlen(what);
500 
501 	/* calculate length we need for result */
502 	s = str;
503 	while (*s) {
504 		if (*s == *what && !strncmp(s, what, what_len)) {
505 			len += with_len - what_len;
506 			if (!globally)
507 				break;
508 			s += what_len;
509 		} else
510 			s++;
511 	}
512 	r = result = el_malloc((len + 1) * sizeof(*r));
513 	if (result == NULL)
514 		return NULL;
515 	s = str;
516 	while (*s) {
517 		if (*s == *what && !strncmp(s, what, what_len)) {
518 			(void)strncpy(r, with, with_len);
519 			r += with_len;
520 			s += what_len;
521 			if (!globally) {
522 				(void)strcpy(r, s);
523 				return result;
524 			}
525 		} else
526 			*r++ = *s++;
527 	}
528 	*r = '\0';
529 	return result;
530 }
531 
532 static	char	*last_search_pat;	/* last !?pat[?] search pattern */
533 static	char	*last_search_match;	/* last !?pat[?] that matched */
534 
535 const char *
536 get_history_event(const char *cmd, int *cindex, int qchar)
537 {
538 	int idx, sign, sub, num, begin, ret;
539 	size_t len;
540 	char	*pat;
541 	const char *rptr;
542 	HistEvent ev;
543 
544 	idx = *cindex;
545 	if (cmd[idx++] != history_expansion_char)
546 		return NULL;
547 
548 	/* find out which event to take */
549 	if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') {
550 		if (history(h, &ev, H_FIRST) != 0)
551 			return NULL;
552 		*cindex = cmd[idx]? (idx + 1):idx;
553 		return ev.str;
554 	}
555 	sign = 0;
556 	if (cmd[idx] == '-') {
557 		sign = 1;
558 		idx++;
559 	}
560 
561 	if ('0' <= cmd[idx] && cmd[idx] <= '9') {
562 		HIST_ENTRY *he;
563 
564 		num = 0;
565 		while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') {
566 			num = num * 10 + cmd[idx] - '0';
567 			idx++;
568 		}
569 		if (sign)
570 			num = history_length - num + history_base;
571 
572 		if (!(he = history_get(num)))
573 			return NULL;
574 
575 		*cindex = idx;
576 		return he->line;
577 	}
578 	sub = 0;
579 	if (cmd[idx] == '?') {
580 		sub = 1;
581 		idx++;
582 	}
583 	begin = idx;
584 	while (cmd[idx]) {
585 		if (cmd[idx] == '\n')
586 			break;
587 		if (sub && cmd[idx] == '?')
588 			break;
589 		if (!sub && (cmd[idx] == ':' || cmd[idx] == ' '
590 				    || cmd[idx] == '\t' || cmd[idx] == qchar))
591 			break;
592 		idx++;
593 	}
594 	len = (size_t)idx - (size_t)begin;
595 	if (sub && cmd[idx] == '?')
596 		idx++;
597 	if (sub && len == 0 && last_search_pat && *last_search_pat)
598 		pat = last_search_pat;
599 	else if (len == 0)
600 		return NULL;
601 	else {
602 		if ((pat = el_malloc((len + 1) * sizeof(*pat))) == NULL)
603 			return NULL;
604 		(void)strncpy(pat, cmd + begin, len);
605 		pat[len] = '\0';
606 	}
607 
608 	if (history(h, &ev, H_CURR) != 0) {
609 		if (pat != last_search_pat)
610 			el_free(pat);
611 		return NULL;
612 	}
613 	num = ev.num;
614 
615 	if (sub) {
616 		if (pat != last_search_pat) {
617 			if (last_search_pat)
618 				el_free(last_search_pat);
619 			last_search_pat = pat;
620 		}
621 		ret = history_search(pat, -1);
622 	} else
623 		ret = history_search_prefix(pat, -1);
624 
625 	if (ret == -1) {
626 		/* restore to end of list on failed search */
627 		history(h, &ev, H_FIRST);
628 		(void)fprintf(rl_outstream, "%s: Event not found\n", pat);
629 		if (pat != last_search_pat)
630 			el_free(pat);
631 		return NULL;
632 	}
633 
634 	if (sub && len) {
635 		if (last_search_match && last_search_match != pat)
636 			el_free(last_search_match);
637 		last_search_match = pat;
638 	}
639 
640 	if (pat != last_search_pat)
641 		el_free(pat);
642 
643 	if (history(h, &ev, H_CURR) != 0)
644 		return NULL;
645 	*cindex = idx;
646 	rptr = ev.str;
647 
648 	/* roll back to original position */
649 	(void)history(h, &ev, H_SET, num);
650 
651 	return rptr;
652 }
653 
654 /*
655  * the real function doing history expansion - takes as argument command
656  * to do and data upon which the command should be executed
657  * does expansion the way I've understood readline documentation
658  *
659  * returns 0 if data was not modified, 1 if it was and 2 if the string
660  * should be only printed and not executed; in case of error,
661  * returns -1 and *result points to NULL
662  * it's the caller's responsibility to free() the string returned in *result
663  */
664 static int
665 _history_expand_command(const char *command, size_t offs, size_t cmdlen,
666     char **result)
667 {
668 	char *tmp, *search = NULL, *aptr;
669 	const char *ptr, *cmd;
670 	static char *from = NULL, *to = NULL;
671 	int start, end, idx, has_mods = 0;
672 	int p_on = 0, g_on = 0;
673 
674 	*result = NULL;
675 	aptr = NULL;
676 	ptr = NULL;
677 
678 	/* First get event specifier */
679 	idx = 0;
680 
681 	if (strchr(":^*$", command[offs + 1])) {
682 		char str[4];
683 		/*
684 		* "!:" is shorthand for "!!:".
685 		* "!^", "!*" and "!$" are shorthand for
686 		* "!!:^", "!!:*" and "!!:$" respectively.
687 		*/
688 		str[0] = str[1] = '!';
689 		str[2] = '0';
690 		ptr = get_history_event(str, &idx, 0);
691 		idx = (command[offs + 1] == ':')? 1:0;
692 		has_mods = 1;
693 	} else {
694 		if (command[offs + 1] == '#') {
695 			/* use command so far */
696 			if ((aptr = el_malloc((offs + 1) * sizeof(*aptr)))
697 			    == NULL)
698 				return -1;
699 			(void)strncpy(aptr, command, offs);
700 			aptr[offs] = '\0';
701 			idx = 1;
702 		} else {
703 			int	qchar;
704 
705 			qchar = (offs > 0 && command[offs - 1] == '"')? '"':0;
706 			ptr = get_history_event(command + offs, &idx, qchar);
707 		}
708 		has_mods = command[offs + (size_t)idx] == ':';
709 	}
710 
711 	if (ptr == NULL && aptr == NULL)
712 		return -1;
713 
714 	if (!has_mods) {
715 		*result = strdup(aptr ? aptr : ptr);
716 		if (aptr)
717 			el_free(aptr);
718 		if (*result == NULL)
719 			return -1;
720 		return 1;
721 	}
722 
723 	cmd = command + offs + idx + 1;
724 
725 	/* Now parse any word designators */
726 
727 	if (*cmd == '%')	/* last word matched by ?pat? */
728 		tmp = strdup(last_search_match? last_search_match:"");
729 	else if (strchr("^*$-0123456789", *cmd)) {
730 		start = end = -1;
731 		if (*cmd == '^')
732 			start = end = 1, cmd++;
733 		else if (*cmd == '$')
734 			start = -1, cmd++;
735 		else if (*cmd == '*')
736 			start = 1, cmd++;
737 	       else if (*cmd == '-' || isdigit((unsigned char) *cmd)) {
738 			start = 0;
739 			while (*cmd && '0' <= *cmd && *cmd <= '9')
740 				start = start * 10 + *cmd++ - '0';
741 
742 			if (*cmd == '-') {
743 				if (isdigit((unsigned char) cmd[1])) {
744 					cmd++;
745 					end = 0;
746 					while (*cmd && '0' <= *cmd && *cmd <= '9')
747 						end = end * 10 + *cmd++ - '0';
748 				} else if (cmd[1] == '$') {
749 					cmd += 2;
750 					end = -1;
751 				} else {
752 					cmd++;
753 					end = -2;
754 				}
755 			} else if (*cmd == '*')
756 				end = -1, cmd++;
757 			else
758 				end = start;
759 		}
760 		tmp = history_arg_extract(start, end, aptr? aptr:ptr);
761 		if (tmp == NULL) {
762 			(void)fprintf(rl_outstream, "%s: Bad word specifier",
763 			    command + offs + idx);
764 			if (aptr)
765 				el_free(aptr);
766 			return -1;
767 		}
768 	} else
769 		tmp = strdup(aptr? aptr:ptr);
770 
771 	if (aptr)
772 		el_free(aptr);
773 
774 	if (*cmd == '\0' || ((size_t)(cmd - (command + offs)) >= cmdlen)) {
775 		*result = tmp;
776 		return 1;
777 	}
778 
779 	for (; *cmd; cmd++) {
780 		if (*cmd == ':')
781 			continue;
782 		else if (*cmd == 'h') {		/* remove trailing path */
783 			if ((aptr = strrchr(tmp, '/')) != NULL)
784 				*aptr = '\0';
785 		} else if (*cmd == 't') {	/* remove leading path */
786 			if ((aptr = strrchr(tmp, '/')) != NULL) {
787 				aptr = strdup(aptr + 1);
788 				el_free(tmp);
789 				tmp = aptr;
790 			}
791 		} else if (*cmd == 'r') {	/* remove trailing suffix */
792 			if ((aptr = strrchr(tmp, '.')) != NULL)
793 				*aptr = '\0';
794 		} else if (*cmd == 'e') {	/* remove all but suffix */
795 			if ((aptr = strrchr(tmp, '.')) != NULL) {
796 				aptr = strdup(aptr);
797 				el_free(tmp);
798 				tmp = aptr;
799 			}
800 		} else if (*cmd == 'p')		/* print only */
801 			p_on = 1;
802 		else if (*cmd == 'g')
803 			g_on = 2;
804 		else if (*cmd == 's' || *cmd == '&') {
805 			char *what, *with, delim;
806 			size_t len, from_len;
807 			size_t size;
808 
809 			if (*cmd == '&' && (from == NULL || to == NULL))
810 				continue;
811 			else if (*cmd == 's') {
812 				delim = *(++cmd), cmd++;
813 				size = 16;
814 				what = el_realloc(from, size * sizeof(*what));
815 				if (what == NULL) {
816 					el_free(from);
817 					el_free(tmp);
818 					return 0;
819 				}
820 				len = 0;
821 				for (; *cmd && *cmd != delim; cmd++) {
822 					if (*cmd == '\\' && cmd[1] == delim)
823 						cmd++;
824 					if (len >= size) {
825 						char *nwhat;
826 						nwhat = el_realloc(what,
827 						    (size <<= 1) *
828 						    sizeof(*nwhat));
829 						if (nwhat == NULL) {
830 							el_free(what);
831 							el_free(tmp);
832 							return 0;
833 						}
834 						what = nwhat;
835 					}
836 					what[len++] = *cmd;
837 				}
838 				what[len] = '\0';
839 				from = what;
840 				if (*what == '\0') {
841 					el_free(what);
842 					if (search) {
843 						from = strdup(search);
844 						if (from == NULL) {
845 							el_free(tmp);
846 							return 0;
847 						}
848 					} else {
849 						from = NULL;
850 						el_free(tmp);
851 						return -1;
852 					}
853 				}
854 				cmd++;	/* shift after delim */
855 				if (!*cmd)
856 					continue;
857 
858 				size = 16;
859 				with = el_realloc(to, size * sizeof(*with));
860 				if (with == NULL) {
861 					el_free(to);
862 					el_free(tmp);
863 					return -1;
864 				}
865 				len = 0;
866 				from_len = strlen(from);
867 				for (; *cmd && *cmd != delim; cmd++) {
868 					if (len + from_len + 1 >= size) {
869 						char *nwith;
870 						size += from_len + 1;
871 						nwith = el_realloc(with,
872 						    size * sizeof(*nwith));
873 						if (nwith == NULL) {
874 							el_free(with);
875 							el_free(tmp);
876 							return -1;
877 						}
878 						with = nwith;
879 					}
880 					if (*cmd == '&') {
881 						/* safe */
882 						(void)strcpy(&with[len], from);
883 						len += from_len;
884 						continue;
885 					}
886 					if (*cmd == '\\'
887 					    && (*(cmd + 1) == delim
888 						|| *(cmd + 1) == '&'))
889 						cmd++;
890 					with[len++] = *cmd;
891 				}
892 				with[len] = '\0';
893 				to = with;
894 			}
895 
896 			aptr = _rl_compat_sub(tmp, from, to, g_on);
897 			if (aptr) {
898 				el_free(tmp);
899 				tmp = aptr;
900 			}
901 			g_on = 0;
902 		}
903 	}
904 	*result = tmp;
905 	return p_on? 2:1;
906 }
907 
908 
909 /*
910  * csh-style history expansion
911  */
912 int
913 history_expand(char *str, char **output)
914 {
915 	int ret = 0;
916 	size_t idx, i, size;
917 	char *tmp, *result;
918 
919 	if (h == NULL || e == NULL)
920 		rl_initialize();
921 
922 	if (history_expansion_char == 0) {
923 		*output = strdup(str);
924 		return 0;
925 	}
926 
927 	*output = NULL;
928 	if (str[0] == history_subst_char) {
929 		/* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
930 		*output = el_malloc((strlen(str) + 4 + 1) * sizeof(**output));
931 		if (*output == NULL)
932 			return 0;
933 		(*output)[0] = (*output)[1] = history_expansion_char;
934 		(*output)[2] = ':';
935 		(*output)[3] = 's';
936 		(void)strcpy((*output) + 4, str);
937 		str = *output;
938 	} else {
939 		*output = strdup(str);
940 		if (*output == NULL)
941 			return 0;
942 	}
943 
944 #define ADD_STRING(what, len, fr)					\
945 	{								\
946 		if (idx + len + 1 > size) {				\
947 			char *nresult = el_realloc(result,		\
948 			    (size += len + 1) * sizeof(*nresult));	\
949 			if (nresult == NULL) {				\
950 				el_free(*output);			\
951 				if (/*CONSTCOND*/fr)			\
952 					el_free(tmp);			\
953 				return 0;				\
954 			}						\
955 			result = nresult;				\
956 		}							\
957 		(void)strncpy(&result[idx], what, len);			\
958 		idx += len;						\
959 		result[idx] = '\0';					\
960 	}
961 
962 	result = NULL;
963 	size = idx = 0;
964 	tmp = NULL;
965 	for (i = 0; str[i];) {
966 		int qchar, loop_again;
967 		size_t len, start, j;
968 
969 		qchar = 0;
970 		loop_again = 1;
971 		start = j = i;
972 loop:
973 		for (; str[j]; j++) {
974 			if (str[j] == '\\' &&
975 			    str[j + 1] == history_expansion_char) {
976 				len = strlen(&str[j + 1]) + 1;
977 				memmove(&str[j], &str[j + 1], len);
978 				continue;
979 			}
980 			if (!loop_again) {
981 				if (isspace((unsigned char) str[j])
982 				    || str[j] == qchar)
983 					break;
984 			}
985 			if (str[j] == history_expansion_char
986 			    && !strchr(history_no_expand_chars, str[j + 1])
987 			    && (!history_inhibit_expansion_function ||
988 			    (*history_inhibit_expansion_function)(str,
989 			    (int)j) == 0))
990 				break;
991 		}
992 
993 		if (str[j] && loop_again) {
994 			i = j;
995 			qchar = (j > 0 && str[j - 1] == '"' )? '"':0;
996 			j++;
997 			if (str[j] == history_expansion_char)
998 				j++;
999 			loop_again = 0;
1000 			goto loop;
1001 		}
1002 		len = i - start;
1003 		ADD_STRING(&str[start], len, 0);
1004 
1005 		if (str[i] == '\0' || str[i] != history_expansion_char) {
1006 			len = j - i;
1007 			ADD_STRING(&str[i], len, 0);
1008 			if (start == 0)
1009 				ret = 0;
1010 			else
1011 				ret = 1;
1012 			break;
1013 		}
1014 		ret = _history_expand_command (str, i, (j - i), &tmp);
1015 		if (ret > 0 && tmp) {
1016 			len = strlen(tmp);
1017 			ADD_STRING(tmp, len, 1);
1018 		}
1019 		if (tmp) {
1020 			el_free(tmp);
1021 			tmp = NULL;
1022 		}
1023 		i = j;
1024 	}
1025 
1026 	/* ret is 2 for "print only" option */
1027 	if (ret == 2) {
1028 		add_history(result);
1029 #ifdef GDB_411_HACK
1030 		/* gdb 4.11 has been shipped with readline, where */
1031 		/* history_expand() returned -1 when the line	  */
1032 		/* should not be executed; in readline 2.1+	  */
1033 		/* it should return 2 in such a case		  */
1034 		ret = -1;
1035 #endif
1036 	}
1037 	el_free(*output);
1038 	*output = result;
1039 
1040 	return ret;
1041 }
1042 
1043 /*
1044 * Return a string consisting of arguments of "str" from "start" to "end".
1045 */
1046 char *
1047 history_arg_extract(int start, int end, const char *str)
1048 {
1049 	size_t  i, len, max;
1050 	char	**arr, *result = NULL;
1051 
1052 	arr = history_tokenize(str);
1053 	if (!arr)
1054 		return NULL;
1055 	if (arr && *arr == NULL)
1056 		goto out;
1057 
1058 	for (max = 0; arr[max]; max++)
1059 		continue;
1060 	max--;
1061 
1062 	if (start == '$')
1063 		start = (int)max;
1064 	if (end == '$')
1065 		end = (int)max;
1066 	if (end < 0)
1067 		end = (int)max + end + 1;
1068 	if (start < 0)
1069 		start = end;
1070 
1071 	if (start < 0 || end < 0 || (size_t)start > max ||
1072 	    (size_t)end > max || start > end)
1073 		goto out;
1074 
1075 	for (i = (size_t)start, len = 0; i <= (size_t)end; i++)
1076 		len += strlen(arr[i]) + 1;
1077 	len++;
1078 	result = el_malloc(len * sizeof(*result));
1079 	if (result == NULL)
1080 		goto out;
1081 
1082 	for (i = (size_t)start, len = 0; i <= (size_t)end; i++) {
1083 		(void)strcpy(result + len, arr[i]);
1084 		len += strlen(arr[i]);
1085 		if (i < (size_t)end)
1086 			result[len++] = ' ';
1087 	}
1088 	result[len] = '\0';
1089 
1090 out:
1091 	for (i = 0; arr[i]; i++)
1092 		el_free(arr[i]);
1093 	el_free(arr);
1094 
1095 	return result;
1096 }
1097 
1098 /*
1099  * Parse the string into individual tokens,
1100  * similar to how shell would do it.
1101  */
1102 char **
1103 history_tokenize(const char *str)
1104 {
1105 	int size = 1, idx = 0, i, start;
1106 	size_t len;
1107 	char **result = NULL, *temp, delim = '\0';
1108 
1109 	for (i = 0; str[i];) {
1110 		while (isspace((unsigned char) str[i]))
1111 			i++;
1112 		start = i;
1113 		for (; str[i];) {
1114 			if (str[i] == '\\') {
1115 				if (str[i+1] != '\0')
1116 					i++;
1117 			} else if (str[i] == delim)
1118 				delim = '\0';
1119 			else if (!delim &&
1120 				    (isspace((unsigned char) str[i]) ||
1121 				strchr("()<>;&|$", str[i])))
1122 				break;
1123 			else if (!delim && strchr("'`\"", str[i]))
1124 				delim = str[i];
1125 			if (str[i])
1126 				i++;
1127 		}
1128 
1129 		if (idx + 2 >= size) {
1130 			char **nresult;
1131 			size <<= 1;
1132 			nresult = el_realloc(result, (size_t)size * sizeof(*nresult));
1133 			if (nresult == NULL) {
1134 				el_free(result);
1135 				return NULL;
1136 			}
1137 			result = nresult;
1138 		}
1139 		len = (size_t)i - (size_t)start;
1140 		temp = el_malloc((size_t)(len + 1) * sizeof(*temp));
1141 		if (temp == NULL) {
1142 			for (i = 0; i < idx; i++)
1143 				el_free(result[i]);
1144 			el_free(result);
1145 			return NULL;
1146 		}
1147 		(void)strncpy(temp, &str[start], len);
1148 		temp[len] = '\0';
1149 		result[idx++] = temp;
1150 		result[idx] = NULL;
1151 		if (str[i])
1152 			i++;
1153 	}
1154 	return result;
1155 }
1156 
1157 
1158 /*
1159  * limit size of history record to ``max'' events
1160  */
1161 void
1162 stifle_history(int max)
1163 {
1164 	HistEvent ev;
1165 	HIST_ENTRY *he;
1166 
1167 	if (h == NULL || e == NULL)
1168 		rl_initialize();
1169 
1170 	if (history(h, &ev, H_SETSIZE, max) == 0) {
1171 		max_input_history = max;
1172 		if (history_length > max)
1173 			history_base = history_length - max;
1174 		while (history_length > max) {
1175 			he = remove_history(0);
1176 			el_free(he->data);
1177 			el_free((void *)(unsigned long)he->line);
1178 			el_free(he);
1179 		}
1180 	}
1181 }
1182 
1183 
1184 /*
1185  * "unlimit" size of history - set the limit to maximum allowed int value
1186  */
1187 int
1188 unstifle_history(void)
1189 {
1190 	HistEvent ev;
1191 	int omax;
1192 
1193 	history(h, &ev, H_SETSIZE, INT_MAX);
1194 	omax = max_input_history;
1195 	max_input_history = INT_MAX;
1196 	return omax;		/* some value _must_ be returned */
1197 }
1198 
1199 
1200 int
1201 history_is_stifled(void)
1202 {
1203 
1204 	/* cannot return true answer */
1205 	return max_input_history != INT_MAX;
1206 }
1207 
1208 static const char _history_tmp_template[] = "/tmp/.historyXXXXXX";
1209 
1210 int
1211 history_truncate_file (const char *filename, int nlines)
1212 {
1213 	int ret = 0;
1214 	FILE *fp, *tp;
1215 	char template[sizeof(_history_tmp_template)];
1216 	char buf[4096];
1217 	int fd;
1218 	char *cp;
1219 	off_t off;
1220 	int count = 0;
1221 	ssize_t left = 0;
1222 
1223 	if (filename == NULL && (filename = _default_history_file()) == NULL)
1224 		return errno;
1225 	if ((fp = fopen(filename, "r+")) == NULL)
1226 		return errno;
1227 	strcpy(template, _history_tmp_template);
1228 	if ((fd = mkstemp(template)) == -1) {
1229 		ret = errno;
1230 		goto out1;
1231 	}
1232 
1233 	if ((tp = fdopen(fd, "r+")) == NULL) {
1234 		close(fd);
1235 		ret = errno;
1236 		goto out2;
1237 	}
1238 
1239 	for(;;) {
1240 		if (fread(buf, sizeof(buf), (size_t)1, fp) != 1) {
1241 			if (ferror(fp)) {
1242 				ret = errno;
1243 				break;
1244 			}
1245 			if (fseeko(fp, (off_t)sizeof(buf) * count, SEEK_SET) ==
1246 			    (off_t)-1) {
1247 				ret = errno;
1248 				break;
1249 			}
1250 			left = (ssize_t)fread(buf, (size_t)1, sizeof(buf), fp);
1251 			if (ferror(fp)) {
1252 				ret = errno;
1253 				break;
1254 			}
1255 			if (left == 0) {
1256 				count--;
1257 				left = sizeof(buf);
1258 			} else if (fwrite(buf, (size_t)left, (size_t)1, tp)
1259 			    != 1) {
1260 				ret = errno;
1261 				break;
1262 			}
1263 			fflush(tp);
1264 			break;
1265 		}
1266 		if (fwrite(buf, sizeof(buf), (size_t)1, tp) != 1) {
1267 			ret = errno;
1268 			break;
1269 		}
1270 		count++;
1271 	}
1272 	if (ret)
1273 		goto out3;
1274 	cp = buf + left - 1;
1275 	if(*cp != '\n')
1276 		cp++;
1277 	for(;;) {
1278 		while (--cp >= buf) {
1279 			if (*cp == '\n') {
1280 				if (--nlines == 0) {
1281 					if (++cp >= buf + sizeof(buf)) {
1282 						count++;
1283 						cp = buf;
1284 					}
1285 					break;
1286 				}
1287 			}
1288 		}
1289 		if (nlines <= 0 || count == 0)
1290 			break;
1291 		count--;
1292 		if (fseeko(tp, (off_t)sizeof(buf) * count, SEEK_SET) < 0) {
1293 			ret = errno;
1294 			break;
1295 		}
1296 		if (fread(buf, sizeof(buf), (size_t)1, tp) != 1) {
1297 			if (ferror(tp)) {
1298 				ret = errno;
1299 				break;
1300 			}
1301 			ret = EAGAIN;
1302 			break;
1303 		}
1304 		cp = buf + sizeof(buf);
1305 	}
1306 
1307 	if (ret || nlines > 0)
1308 		goto out3;
1309 
1310 	if (fseeko(fp, (off_t)0, SEEK_SET) == (off_t)-1) {
1311 		ret = errno;
1312 		goto out3;
1313 	}
1314 
1315 	if (fseeko(tp, (off_t)sizeof(buf) * count + (cp - buf), SEEK_SET) ==
1316 	    (off_t)-1) {
1317 		ret = errno;
1318 		goto out3;
1319 	}
1320 
1321 	for(;;) {
1322 		if ((left = (ssize_t)fread(buf, (size_t)1, sizeof(buf), tp)) == 0) {
1323 			if (ferror(fp))
1324 				ret = errno;
1325 			break;
1326 		}
1327 		if (fwrite(buf, (size_t)left, (size_t)1, fp) != 1) {
1328 			ret = errno;
1329 			break;
1330 		}
1331 	}
1332 	fflush(fp);
1333 	if((off = ftello(fp)) > 0)
1334 		(void)ftruncate(fileno(fp), off);
1335 out3:
1336 	fclose(tp);
1337 out2:
1338 	unlink(template);
1339 out1:
1340 	fclose(fp);
1341 
1342 	return ret;
1343 }
1344 
1345 
1346 /*
1347  * read history from a file given
1348  */
1349 int
1350 read_history(const char *filename)
1351 {
1352 	HistEvent ev;
1353 
1354 	if (h == NULL || e == NULL)
1355 		rl_initialize();
1356 	if (filename == NULL && (filename = _default_history_file()) == NULL)
1357 		return errno;
1358 	errno = 0;
1359 	if (history(h, &ev, H_LOAD, filename) == -1)
1360 	    return errno ? errno : EINVAL;
1361 	if (history(h, &ev, H_GETSIZE) == 0)
1362 		history_length = ev.num;
1363 	if (history_length < 0)
1364 		return EINVAL;
1365 	return 0;
1366 }
1367 
1368 
1369 /*
1370  * write history to a file given
1371  */
1372 int
1373 write_history(const char *filename)
1374 {
1375 	HistEvent ev;
1376 
1377 	if (h == NULL || e == NULL)
1378 		rl_initialize();
1379 	if (filename == NULL && (filename = _default_history_file()) == NULL)
1380 		return errno;
1381 	return history(h, &ev, H_SAVE, filename) == -1 ?
1382 	    (errno ? errno : EINVAL) : 0;
1383 }
1384 
1385 int
1386 append_history(int n, const char *filename)
1387 {
1388 	HistEvent ev;
1389 	FILE *fp;
1390 
1391 	if (h == NULL || e == NULL)
1392 		rl_initialize();
1393 	if (filename == NULL && (filename = _default_history_file()) == NULL)
1394 		return errno;
1395 
1396 	if ((fp = fopen(filename, "a")) == NULL)
1397 		return errno;
1398 
1399 	if (history(h, &ev, H_NSAVE_FP, (size_t)n,  fp) == -1) {
1400 		int serrno = errno ? errno : EINVAL;
1401 		fclose(fp);
1402 		return serrno;
1403 	}
1404 	fclose(fp);
1405 	return 0;
1406 }
1407 
1408 /*
1409  * returns history ``num''th event
1410  *
1411  * returned pointer points to static variable
1412  */
1413 HIST_ENTRY *
1414 history_get(int num)
1415 {
1416 	static HIST_ENTRY she;
1417 	HistEvent ev;
1418 	int curr_num;
1419 
1420 	if (h == NULL || e == NULL)
1421 		rl_initialize();
1422 
1423 	if (num < history_base)
1424 		return NULL;
1425 
1426 	/* save current position */
1427 	if (history(h, &ev, H_CURR) != 0)
1428 		return NULL;
1429 	curr_num = ev.num;
1430 
1431 	/*
1432 	 * use H_DELDATA to set to nth history (without delete) by passing
1433 	 * (void **)-1  -- as in history_set_pos
1434 	 */
1435 	if (history(h, &ev, H_DELDATA, num - history_base, (void **)-1) != 0)
1436 		goto out;
1437 
1438 	/* get current entry */
1439 	if (history(h, &ev, H_CURR) != 0)
1440 		goto out;
1441 	if (history(h, &ev, H_NEXT_EVDATA, ev.num, &she.data) != 0)
1442 		goto out;
1443 	she.line = ev.str;
1444 
1445 	/* restore pointer to where it was */
1446 	(void)history(h, &ev, H_SET, curr_num);
1447 
1448 	return &she;
1449 
1450 out:
1451 	/* restore pointer to where it was */
1452 	(void)history(h, &ev, H_SET, curr_num);
1453 	return NULL;
1454 }
1455 
1456 
1457 /*
1458  * add the line to history table
1459  */
1460 int
1461 add_history(const char *line)
1462 {
1463 	HistEvent ev;
1464 
1465 	if (h == NULL || e == NULL)
1466 		rl_initialize();
1467 
1468 	if (history(h, &ev, H_ENTER, line) == -1)
1469 		return 0;
1470 
1471 	(void)history(h, &ev, H_GETSIZE);
1472 	if (ev.num == history_length)
1473 		history_base++;
1474 	else
1475 		history_length = ev.num;
1476 	return 0;
1477 }
1478 
1479 
1480 /*
1481  * remove the specified entry from the history list and return it.
1482  */
1483 HIST_ENTRY *
1484 remove_history(int num)
1485 {
1486 	HIST_ENTRY *he;
1487 	HistEvent ev;
1488 
1489 	if (h == NULL || e == NULL)
1490 		rl_initialize();
1491 
1492 	if ((he = el_malloc(sizeof(*he))) == NULL)
1493 		return NULL;
1494 
1495 	if (history(h, &ev, H_DELDATA, num, &he->data) != 0) {
1496 		el_free(he);
1497 		return NULL;
1498 	}
1499 
1500 	he->line = ev.str;
1501 	if (history(h, &ev, H_GETSIZE) == 0)
1502 		history_length = ev.num;
1503 
1504 	return he;
1505 }
1506 
1507 
1508 /*
1509  * replace the line and data of the num-th entry
1510  */
1511 HIST_ENTRY *
1512 replace_history_entry(int num, const char *line, histdata_t data)
1513 {
1514 	HIST_ENTRY *he;
1515 	HistEvent ev;
1516 	int curr_num;
1517 
1518 	if (h == NULL || e == NULL)
1519 		rl_initialize();
1520 
1521 	/* save current position */
1522 	if (history(h, &ev, H_CURR) != 0)
1523 		return NULL;
1524 	curr_num = ev.num;
1525 
1526 	/* start from the oldest */
1527 	if (history(h, &ev, H_LAST) != 0)
1528 		return NULL;	/* error */
1529 
1530 	if ((he = el_malloc(sizeof(*he))) == NULL)
1531 		return NULL;
1532 
1533 	/* look forwards for event matching specified offset */
1534 	if (history(h, &ev, H_NEXT_EVDATA, num, &he->data))
1535 		goto out;
1536 
1537 	he->line = strdup(ev.str);
1538 	if (he->line == NULL)
1539 		goto out;
1540 
1541 	if (history(h, &ev, H_REPLACE, line, data))
1542 		goto out;
1543 
1544 	/* restore pointer to where it was */
1545 	if (history(h, &ev, H_SET, curr_num))
1546 		goto out;
1547 
1548 	return he;
1549 out:
1550 	el_free(he);
1551 	return NULL;
1552 }
1553 
1554 /*
1555  * clear the history list - delete all entries
1556  */
1557 void
1558 clear_history(void)
1559 {
1560 	HistEvent ev;
1561 
1562 	if (h == NULL || e == NULL)
1563 		rl_initialize();
1564 
1565 	(void)history(h, &ev, H_CLEAR);
1566 	history_offset = history_length = 0;
1567 }
1568 
1569 
1570 /*
1571  * returns offset of the current history event
1572  */
1573 int
1574 where_history(void)
1575 {
1576 	return history_offset;
1577 }
1578 
1579 static HIST_ENTRY **_history_listp;
1580 static HIST_ENTRY *_history_list;
1581 
1582 HIST_ENTRY **
1583 history_list(void)
1584 {
1585 	HistEvent ev;
1586 	HIST_ENTRY **nlp, *nl;
1587 	int i;
1588 
1589 	if (history(h, &ev, H_LAST) != 0)
1590 		return NULL;
1591 
1592 	if ((nlp = el_realloc(_history_listp,
1593 	    (size_t)history_length * sizeof(*nlp))) == NULL)
1594 		return NULL;
1595 	_history_listp = nlp;
1596 
1597 	if ((nl = el_realloc(_history_list,
1598 	    (size_t)history_length * sizeof(*nl))) == NULL)
1599 		return NULL;
1600 	_history_list = nl;
1601 
1602 	i = 0;
1603 	do {
1604 		_history_listp[i] = &_history_list[i];
1605 		_history_list[i].line = ev.str;
1606 		_history_list[i].data = NULL;
1607 		if (i++ == history_length)
1608 			abort();
1609 	} while (history(h, &ev, H_PREV) == 0);
1610 	return _history_listp;
1611 }
1612 
1613 /*
1614  * returns current history event or NULL if there is no such event
1615  */
1616 HIST_ENTRY *
1617 current_history(void)
1618 {
1619 	HistEvent ev;
1620 
1621 	if (history(h, &ev, H_PREV_EVENT, history_offset + 1) != 0)
1622 		return NULL;
1623 
1624 	rl_he.line = ev.str;
1625 	rl_he.data = NULL;
1626 	return &rl_he;
1627 }
1628 
1629 
1630 /*
1631  * returns total number of bytes history events' data are using
1632  */
1633 int
1634 history_total_bytes(void)
1635 {
1636 	HistEvent ev;
1637 	int curr_num;
1638 	size_t size;
1639 
1640 	if (history(h, &ev, H_CURR) != 0)
1641 		return -1;
1642 	curr_num = ev.num;
1643 
1644 	(void)history(h, &ev, H_FIRST);
1645 	size = 0;
1646 	do
1647 		size += strlen(ev.str) * sizeof(*ev.str);
1648 	while (history(h, &ev, H_NEXT) == 0);
1649 
1650 	/* get to the same position as before */
1651 	history(h, &ev, H_PREV_EVENT, curr_num);
1652 
1653 	return (int)size;
1654 }
1655 
1656 
1657 /*
1658  * sets the position in the history list to ``pos''
1659  */
1660 int
1661 history_set_pos(int pos)
1662 {
1663 	if (pos >= history_length || pos < 0)
1664 		return 0;
1665 
1666 	history_offset = pos;
1667 	return 1;
1668 }
1669 
1670 
1671 /*
1672  * returns previous event in history and shifts pointer accordingly
1673  * Note that readline and editline define directions in opposite ways.
1674  */
1675 HIST_ENTRY *
1676 previous_history(void)
1677 {
1678 	HistEvent ev;
1679 
1680 	if (history_offset == 0)
1681 		return NULL;
1682 
1683 	if (history(h, &ev, H_LAST) != 0)
1684 		return NULL;
1685 
1686 	history_offset--;
1687 	return current_history();
1688 }
1689 
1690 
1691 /*
1692  * returns next event in history and shifts pointer accordingly
1693  */
1694 HIST_ENTRY *
1695 next_history(void)
1696 {
1697 	HistEvent ev;
1698 
1699 	if (history_offset >= history_length)
1700 		return NULL;
1701 
1702 	if (history(h, &ev, H_LAST) != 0)
1703 		return NULL;
1704 
1705 	history_offset++;
1706 	return current_history();
1707 }
1708 
1709 
1710 /*
1711  * searches for first history event containing the str
1712  */
1713 int
1714 history_search(const char *str, int direction)
1715 {
1716 	HistEvent ev;
1717 	const char *strp;
1718 	int curr_num;
1719 
1720 	if (history(h, &ev, H_CURR) != 0)
1721 		return -1;
1722 	curr_num = ev.num;
1723 
1724 	for (;;) {
1725 		if ((strp = strstr(ev.str, str)) != NULL)
1726 			return (int)(strp - ev.str);
1727 		if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)
1728 			break;
1729 	}
1730 	(void)history(h, &ev, H_SET, curr_num);
1731 	return -1;
1732 }
1733 
1734 
1735 /*
1736  * searches for first history event beginning with str
1737  */
1738 int
1739 history_search_prefix(const char *str, int direction)
1740 {
1741 	HistEvent ev;
1742 
1743 	return (history(h, &ev, direction < 0 ?
1744 	    H_PREV_STR : H_NEXT_STR, str));
1745 }
1746 
1747 
1748 /*
1749  * search for event in history containing str, starting at offset
1750  * abs(pos); continue backward, if pos<0, forward otherwise
1751  */
1752 /* ARGSUSED */
1753 int
1754 history_search_pos(const char *str,
1755 		   int direction __attribute__((__unused__)), int pos)
1756 {
1757 	HistEvent ev;
1758 	int curr_num, off;
1759 
1760 	off = (pos > 0) ? pos : -pos;
1761 	pos = (pos > 0) ? 1 : -1;
1762 
1763 	if (history(h, &ev, H_CURR) != 0)
1764 		return -1;
1765 	curr_num = ev.num;
1766 
1767 	if (!history_set_pos(off) || history(h, &ev, H_CURR) != 0)
1768 		return -1;
1769 
1770 	for (;;) {
1771 		if (strstr(ev.str, str))
1772 			return off;
1773 		if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
1774 			break;
1775 	}
1776 
1777 	/* set "current" pointer back to previous state */
1778 	(void)history(h, &ev,
1779 	    pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
1780 
1781 	return -1;
1782 }
1783 
1784 
1785 /********************************/
1786 /* completion functions */
1787 
1788 char *
1789 tilde_expand(char *name)
1790 {
1791 	return fn_tilde_expand(name);
1792 }
1793 
1794 char *
1795 filename_completion_function(const char *name, int state)
1796 {
1797 	return fn_filename_completion_function(name, state);
1798 }
1799 
1800 /*
1801  * a completion generator for usernames; returns _first_ username
1802  * which starts with supplied text
1803  * text contains a partial username preceded by random character
1804  * (usually '~'); state resets search from start (??? should we do that anyway)
1805  * it's the caller's responsibility to free the returned value
1806  */
1807 char *
1808 username_completion_function(const char *text, int state)
1809 {
1810 #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
1811 	struct passwd pwres;
1812 	char pwbuf[1024];
1813 #endif
1814 	struct passwd *pass = NULL;
1815 
1816 	if (text[0] == '\0')
1817 		return NULL;
1818 
1819 	if (*text == '~')
1820 		text++;
1821 
1822 	if (state == 0)
1823 		setpwent();
1824 
1825 	while (
1826 #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
1827 	    getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pass) == 0 && pass != NULL
1828 #else
1829 	    (pass = getpwent()) != NULL
1830 #endif
1831 	    && text[0] == pass->pw_name[0]
1832 	    && strcmp(text, pass->pw_name) == 0)
1833 		continue;
1834 
1835 	if (pass == NULL) {
1836 		endpwent();
1837 		return NULL;
1838 	}
1839 	return strdup(pass->pw_name);
1840 }
1841 
1842 
1843 /*
1844  * el-compatible wrapper to send TSTP on ^Z
1845  */
1846 /* ARGSUSED */
1847 static unsigned char
1848 _el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__)))
1849 {
1850 	(void)kill(0, SIGTSTP);
1851 	return CC_NORM;
1852 }
1853 
1854 static const char *
1855 /*ARGSUSED*/
1856 _rl_completion_append_character_function(const char *dummy
1857     __attribute__((__unused__)))
1858 {
1859 	static char buf[2];
1860 	buf[0] = (char)rl_completion_append_character;
1861 	buf[1] = '\0';
1862 	return buf;
1863 }
1864 
1865 
1866 /*
1867  * Display list of strings in columnar format on readline's output stream.
1868  * 'matches' is list of strings, 'len' is number of strings in 'matches',
1869  * 'max' is maximum length of string in 'matches'.
1870  */
1871 void
1872 rl_display_match_list(char **matches, int len, int max)
1873 {
1874 
1875 	fn_display_match_list(e, matches, (size_t)len, (size_t)max,
1876 		_rl_completion_append_character_function);
1877 }
1878 
1879 /*
1880  * complete word at current point
1881  */
1882 /* ARGSUSED */
1883 int
1884 rl_complete(int ignore __attribute__((__unused__)), int invoking_key)
1885 {
1886 	static ct_buffer_t wbreak_conv, sprefix_conv;
1887 	char *breakchars;
1888 
1889 	if (h == NULL || e == NULL)
1890 		rl_initialize();
1891 
1892 	if (rl_inhibit_completion) {
1893 		char arr[2];
1894 		arr[0] = (char)invoking_key;
1895 		arr[1] = '\0';
1896 		el_insertstr(e, arr);
1897 		return CC_REFRESH;
1898 	}
1899 
1900 	if (rl_completion_word_break_hook != NULL)
1901 		breakchars = (*rl_completion_word_break_hook)();
1902 	else
1903 		breakchars = rl_basic_word_break_characters;
1904 
1905 	_rl_update_pos();
1906 
1907 	/* Just look at how many global variables modify this operation! */
1908 	return fn_complete(e,
1909 	    (rl_compentry_func_t *)rl_completion_entry_function,
1910 	    rl_attempted_completion_function,
1911 	    ct_decode_string(rl_basic_word_break_characters, &wbreak_conv),
1912 	    ct_decode_string(breakchars, &sprefix_conv),
1913 	    _rl_completion_append_character_function,
1914 	    (size_t)rl_completion_query_items,
1915 	    &rl_completion_type, &rl_attempted_completion_over,
1916 	    &rl_point, &rl_end);
1917 
1918 
1919 }
1920 
1921 
1922 /* ARGSUSED */
1923 static unsigned char
1924 _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch)
1925 {
1926 	return (unsigned char)rl_complete(0, ch);
1927 }
1928 
1929 /*
1930  * misc other functions
1931  */
1932 
1933 /*
1934  * bind key c to readline-type function func
1935  */
1936 int
1937 rl_bind_key(int c, rl_command_func_t *func)
1938 {
1939 	int retval = -1;
1940 
1941 	if (h == NULL || e == NULL)
1942 		rl_initialize();
1943 
1944 	if (func == rl_insert) {
1945 		/* XXX notice there is no range checking of ``c'' */
1946 		e->el_map.key[c] = ED_INSERT;
1947 		retval = 0;
1948 	}
1949 	return retval;
1950 }
1951 
1952 
1953 /*
1954  * read one key from input - handles chars pushed back
1955  * to input stream also
1956  */
1957 int
1958 rl_read_key(void)
1959 {
1960 	char fooarr[2 * sizeof(int)];
1961 
1962 	if (e == NULL || h == NULL)
1963 		rl_initialize();
1964 
1965 	return el_getc(e, fooarr);
1966 }
1967 
1968 
1969 /*
1970  * reset the terminal
1971  */
1972 /* ARGSUSED */
1973 void
1974 rl_reset_terminal(const char *p __attribute__((__unused__)))
1975 {
1976 
1977 	if (h == NULL || e == NULL)
1978 		rl_initialize();
1979 	el_reset(e);
1980 }
1981 
1982 
1983 /*
1984  * insert character ``c'' back into input stream, ``count'' times
1985  */
1986 int
1987 rl_insert(int count, int c)
1988 {
1989 	char arr[2];
1990 
1991 	if (h == NULL || e == NULL)
1992 		rl_initialize();
1993 
1994 	/* XXX - int -> char conversion can lose on multichars */
1995 	arr[0] = (char)c;
1996 	arr[1] = '\0';
1997 
1998 	for (; count > 0; count--)
1999 		el_push(e, arr);
2000 
2001 	return 0;
2002 }
2003 
2004 int
2005 rl_insert_text(const char *text)
2006 {
2007 	if (!text || *text == 0)
2008 		return 0;
2009 
2010 	if (h == NULL || e == NULL)
2011 		rl_initialize();
2012 
2013 	if (el_insertstr(e, text) < 0)
2014 		return 0;
2015 	return (int)strlen(text);
2016 }
2017 
2018 /*ARGSUSED*/
2019 int
2020 rl_newline(int count __attribute__((__unused__)),
2021     int c __attribute__((__unused__)))
2022 {
2023 	/*
2024 	 * Readline-4.0 appears to ignore the args.
2025 	 */
2026 	return rl_insert(1, '\n');
2027 }
2028 
2029 /*ARGSUSED*/
2030 static unsigned char
2031 rl_bind_wrapper(EditLine *el __attribute__((__unused__)), unsigned char c)
2032 {
2033 	if (map[c] == NULL)
2034 	    return CC_ERROR;
2035 
2036 	_rl_update_pos();
2037 
2038 	(*map[c])(1, c);
2039 
2040 	/* If rl_done was set by the above call, deal with it here */
2041 	if (rl_done)
2042 		return CC_EOF;
2043 
2044 	return CC_NORM;
2045 }
2046 
2047 int
2048 rl_add_defun(const char *name, rl_command_func_t *fun, int c)
2049 {
2050 	char dest[8];
2051 	if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0)
2052 		return -1;
2053 	map[(unsigned char)c] = fun;
2054 	el_set(e, EL_ADDFN, name, name, rl_bind_wrapper);
2055 	vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0);
2056 	el_set(e, EL_BIND, dest, name, NULL);
2057 	return 0;
2058 }
2059 
2060 void
2061 rl_callback_read_char(void)
2062 {
2063 	int count = 0, done = 0;
2064 	const char *buf = el_gets(e, &count);
2065 	char *wbuf;
2066 
2067 	if (buf == NULL || count-- <= 0)
2068 		return;
2069 	if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF])
2070 		done = 1;
2071 	if (buf[count] == '\n' || buf[count] == '\r')
2072 		done = 2;
2073 
2074 	if (done && rl_linefunc != NULL) {
2075 		el_set(e, EL_UNBUFFERED, 0);
2076 		if (done == 2) {
2077 			if ((wbuf = strdup(buf)) != NULL)
2078 				wbuf[count] = '\0';
2079 		} else
2080 			wbuf = NULL;
2081 		(*(void (*)(const char *))rl_linefunc)(wbuf);
2082 		el_set(e, EL_UNBUFFERED, 1);
2083 	}
2084 }
2085 
2086 void
2087 rl_callback_handler_install(const char *prompt, rl_vcpfunc_t *linefunc)
2088 {
2089 	if (e == NULL) {
2090 		rl_initialize();
2091 	}
2092 	(void)rl_set_prompt(prompt);
2093 	rl_linefunc = linefunc;
2094 	el_set(e, EL_UNBUFFERED, 1);
2095 }
2096 
2097 void
2098 rl_callback_handler_remove(void)
2099 {
2100 	rl_linefunc = NULL;
2101 	el_end(e);
2102 	e = NULL;
2103 }
2104 
2105 void
2106 rl_redisplay(void)
2107 {
2108 	char a[2];
2109 	a[0] = (char)e->el_tty.t_c[TS_IO][C_REPRINT];
2110 	a[1] = '\0';
2111 	el_push(e, a);
2112 }
2113 
2114 int
2115 rl_get_previous_history(int count, int key)
2116 {
2117 	char a[2];
2118 	a[0] = (char)key;
2119 	a[1] = '\0';
2120 	while (count--)
2121 		el_push(e, a);
2122 	return 0;
2123 }
2124 
2125 void
2126 /*ARGSUSED*/
2127 rl_prep_terminal(int meta_flag __attribute__((__unused__)))
2128 {
2129 	el_set(e, EL_PREP_TERM, 1);
2130 }
2131 
2132 void
2133 rl_deprep_terminal(void)
2134 {
2135 	el_set(e, EL_PREP_TERM, 0);
2136 }
2137 
2138 int
2139 rl_read_init_file(const char *s)
2140 {
2141 	return el_source(e, s);
2142 }
2143 
2144 int
2145 rl_parse_and_bind(const char *line)
2146 {
2147 	const char **argv;
2148 	int argc;
2149 	Tokenizer *tok;
2150 
2151 	tok = tok_init(NULL);
2152 	tok_str(tok, line, &argc, &argv);
2153 	argc = el_parse(e, argc, argv);
2154 	tok_end(tok);
2155 	return argc ? 1 : 0;
2156 }
2157 
2158 int
2159 rl_variable_bind(const char *var, const char *value)
2160 {
2161 	/*
2162 	 * The proper return value is undocument, but this is what the
2163 	 * readline source seems to do.
2164 	 */
2165 	return el_set(e, EL_BIND, "", var, value, NULL) == -1 ? 1 : 0;
2166 }
2167 
2168 void
2169 rl_stuff_char(int c)
2170 {
2171 	char buf[2];
2172 
2173 	buf[0] = (char)c;
2174 	buf[1] = '\0';
2175 	el_insertstr(e, buf);
2176 }
2177 
2178 static int
2179 _rl_event_read_char(EditLine *el, wchar_t *wc)
2180 {
2181 	char	ch;
2182 	int	n;
2183 	ssize_t num_read = 0;
2184 
2185 	ch = '\0';
2186 	*wc = L'\0';
2187 	while (rl_event_hook) {
2188 
2189 		(*rl_event_hook)();
2190 
2191 #if defined(FIONREAD)
2192 		if (ioctl(el->el_infd, FIONREAD, &n) < 0)
2193 			return -1;
2194 		if (n)
2195 			num_read = read(el->el_infd, &ch, (size_t)1);
2196 		else
2197 			num_read = 0;
2198 #elif defined(F_SETFL) && defined(O_NDELAY)
2199 		if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0)
2200 			return -1;
2201 		if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0)
2202 			return -1;
2203 		num_read = read(el->el_infd, &ch, 1);
2204 		if (fcntl(el->el_infd, F_SETFL, n))
2205 			return -1;
2206 #else
2207 		/* not non-blocking, but what you gonna do? */
2208 		num_read = read(el->el_infd, &ch, 1);
2209 		return -1;
2210 #endif
2211 
2212 		if (num_read < 0 && errno == EAGAIN)
2213 			continue;
2214 		if (num_read == 0)
2215 			continue;
2216 		break;
2217 	}
2218 	if (!rl_event_hook)
2219 		el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN);
2220 	*wc = (wchar_t)ch;
2221 	return (int)num_read;
2222 }
2223 
2224 static void
2225 _rl_update_pos(void)
2226 {
2227 	const LineInfo *li = el_line(e);
2228 
2229 	rl_point = (int)(li->cursor - li->buffer);
2230 	rl_end = (int)(li->lastchar - li->buffer);
2231 }
2232 
2233 void
2234 rl_get_screen_size(int *rows, int *cols)
2235 {
2236 	if (rows)
2237 		el_get(e, EL_GETTC, "li", rows, (void *)0);
2238 	if (cols)
2239 		el_get(e, EL_GETTC, "co", cols, (void *)0);
2240 }
2241 
2242 void
2243 rl_set_screen_size(int rows, int cols)
2244 {
2245 	char buf[64];
2246 	(void)snprintf(buf, sizeof(buf), "%d", rows);
2247 	el_set(e, EL_SETTC, "li", buf, NULL);
2248 	(void)snprintf(buf, sizeof(buf), "%d", cols);
2249 	el_set(e, EL_SETTC, "co", buf, NULL);
2250 }
2251 
2252 char **
2253 rl_completion_matches(const char *str, rl_compentry_func_t *fun)
2254 {
2255 	size_t len, max, i, j, min;
2256 	char **list, *match, *a, *b;
2257 
2258 	len = 1;
2259 	max = 10;
2260 	if ((list = el_malloc(max * sizeof(*list))) == NULL)
2261 		return NULL;
2262 
2263 	while ((match = (*fun)(str, (int)(len - 1))) != NULL) {
2264 		list[len++] = match;
2265 		if (len == max) {
2266 			char **nl;
2267 			max += 10;
2268 			if ((nl = el_realloc(list, max * sizeof(*nl))) == NULL)
2269 				goto out;
2270 			list = nl;
2271 		}
2272 	}
2273 	if (len == 1)
2274 		goto out;
2275 	list[len] = NULL;
2276 	if (len == 2) {
2277 		if ((list[0] = strdup(list[1])) == NULL)
2278 			goto out;
2279 		return list;
2280 	}
2281 	qsort(&list[1], len - 1, sizeof(*list),
2282 	    (int (*)(const void *, const void *)) strcmp);
2283 	min = SIZE_MAX;
2284 	for (i = 1, a = list[i]; i < len - 1; i++, a = b) {
2285 		b = list[i + 1];
2286 		for (j = 0; a[j] && a[j] == b[j]; j++)
2287 			continue;
2288 		if (min > j)
2289 			min = j;
2290 	}
2291 	if (min == 0 && *str) {
2292 		if ((list[0] = strdup(str)) == NULL)
2293 			goto out;
2294 	} else {
2295 		if ((list[0] = el_malloc((min + 1) * sizeof(*list[0]))) == NULL)
2296 			goto out;
2297 		(void)memcpy(list[0], list[1], min);
2298 		list[0][min] = '\0';
2299 	}
2300 	return list;
2301 
2302 out:
2303 	el_free(list);
2304 	return NULL;
2305 }
2306 
2307 char *
2308 rl_filename_completion_function (const char *text, int state)
2309 {
2310 	return fn_filename_completion_function(text, state);
2311 }
2312 
2313 void
2314 rl_forced_update_display(void)
2315 {
2316 	el_set(e, EL_REFRESH);
2317 }
2318 
2319 int
2320 _rl_abort_internal(void)
2321 {
2322 	el_beep(e);
2323 	longjmp(topbuf, 1);
2324 	/*NOTREACHED*/
2325 }
2326 
2327 int
2328 _rl_qsort_string_compare(char **s1, char **s2)
2329 {
2330 	return strcoll(*s1, *s2);
2331 }
2332 
2333 HISTORY_STATE *
2334 history_get_history_state(void)
2335 {
2336 	HISTORY_STATE *hs;
2337 
2338 	if ((hs = el_malloc(sizeof(*hs))) == NULL)
2339 		return NULL;
2340 	hs->length = history_length;
2341 	return hs;
2342 }
2343 
2344 int
2345 /*ARGSUSED*/
2346 rl_kill_text(int from __attribute__((__unused__)),
2347     int to __attribute__((__unused__)))
2348 {
2349 	return 0;
2350 }
2351 
2352 Keymap
2353 rl_make_bare_keymap(void)
2354 {
2355 	return NULL;
2356 }
2357 
2358 Keymap
2359 rl_get_keymap(void)
2360 {
2361 	return NULL;
2362 }
2363 
2364 void
2365 /*ARGSUSED*/
2366 rl_set_keymap(Keymap k __attribute__((__unused__)))
2367 {
2368 }
2369 
2370 int
2371 /*ARGSUSED*/
2372 rl_generic_bind(int type __attribute__((__unused__)),
2373     const char * keyseq __attribute__((__unused__)),
2374     const char * data __attribute__((__unused__)),
2375     Keymap k __attribute__((__unused__)))
2376 {
2377 	return 0;
2378 }
2379 
2380 int
2381 /*ARGSUSED*/
2382 rl_bind_key_in_map(int key __attribute__((__unused__)),
2383     rl_command_func_t *fun __attribute__((__unused__)),
2384     Keymap k __attribute__((__unused__)))
2385 {
2386 	return 0;
2387 }
2388 
2389 /* unsupported, but needed by python */
2390 void
2391 rl_cleanup_after_signal(void)
2392 {
2393 }
2394 
2395 int
2396 rl_on_new_line(void)
2397 {
2398 	return 0;
2399 }
2400 
2401 void
2402 rl_free_line_state(void)
2403 {
2404 }
2405 
2406 int
2407 /*ARGSUSED*/
2408 rl_set_keyboard_input_timeout(int u __attribute__((__unused__)))
2409 {
2410 	return 0;
2411 }
2412 
2413 void
2414 rl_resize_terminal(void)
2415 {
2416 	el_resize(e);
2417 }
2418