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