Home | History | Annotate | Line # | Download | only in libedit
readline.c revision 1.36
      1  1.36  christos /*	$NetBSD: readline.c,v 1.36 2003/10/09 00:42:28 christos Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5   1.1  christos  * All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  christos  * by Jaromir Dolecek.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  christos  *    documentation and/or other materials provided with the distribution.
     18   1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19   1.1  christos  *    must display the following acknowledgement:
     20   1.1  christos  *	This product includes software developed by the NetBSD
     21   1.1  christos  *	Foundation, Inc. and its contributors.
     22   1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1  christos  *    contributors may be used to endorse or promote products derived
     24   1.1  christos  *    from this software without specific prior written permission.
     25   1.1  christos  *
     26   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  christos  */
     38   1.1  christos 
     39  1.20  christos #include "config.h"
     40   1.1  christos #if !defined(lint) && !defined(SCCSID)
     41  1.36  christos __RCSID("$NetBSD: readline.c,v 1.36 2003/10/09 00:42:28 christos Exp $");
     42   1.1  christos #endif /* not lint && not SCCSID */
     43   1.1  christos 
     44   1.1  christos #include <sys/types.h>
     45   1.1  christos #include <sys/stat.h>
     46   1.1  christos #include <stdio.h>
     47   1.1  christos #include <dirent.h>
     48   1.1  christos #include <string.h>
     49   1.1  christos #include <pwd.h>
     50   1.1  christos #include <ctype.h>
     51   1.1  christos #include <stdlib.h>
     52   1.1  christos #include <unistd.h>
     53   1.1  christos #include <limits.h>
     54  1.35  christos #include <vis.h>
     55  1.27  christos #ifdef HAVE_ALLOCA_H
     56  1.27  christos #include <alloca.h>
     57  1.27  christos #endif
     58   1.1  christos #include "histedit.h"
     59  1.17  jdolecek #include "readline/readline.h"
     60   1.1  christos #include "el.h"
     61  1.12  jdolecek #include "fcns.h"		/* for EL_NUM_FCNS */
     62   1.1  christos 
     63   1.1  christos /* for rl_complete() */
     64  1.11     lukem #define	TAB		'\r'
     65   1.1  christos 
     66   1.1  christos /* see comment at the #ifdef for sense of this */
     67  1.11     lukem #define	GDB_411_HACK
     68   1.1  christos 
     69   1.1  christos /* readline compatibility stuff - look at readline sources/documentation */
     70   1.1  christos /* to see what these variables mean */
     71  1.11     lukem const char *rl_library_version = "EditLine wrapper";
     72  1.20  christos static char empty[] = { '\0' };
     73  1.20  christos static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };
     74  1.20  christos static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
     75  1.20  christos     '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
     76  1.20  christos char *rl_readline_name = empty;
     77  1.11     lukem FILE *rl_instream = NULL;
     78  1.11     lukem FILE *rl_outstream = NULL;
     79  1.11     lukem int rl_point = 0;
     80  1.11     lukem int rl_end = 0;
     81  1.11     lukem char *rl_line_buffer = NULL;
     82  1.35  christos VFunction *rl_linefunc = NULL;
     83  1.11     lukem 
     84  1.11     lukem int history_base = 1;		/* probably never subject to change */
     85  1.11     lukem int history_length = 0;
     86  1.11     lukem int max_input_history = 0;
     87  1.11     lukem char history_expansion_char = '!';
     88  1.11     lukem char history_subst_char = '^';
     89  1.20  christos char *history_no_expand_chars = expand_chars;
     90  1.11     lukem Function *history_inhibit_expansion_function = NULL;
     91  1.11     lukem 
     92  1.11     lukem int rl_inhibit_completion = 0;
     93  1.11     lukem int rl_attempted_completion_over = 0;
     94  1.20  christos char *rl_basic_word_break_characters = break_chars;
     95  1.11     lukem char *rl_completer_word_break_characters = NULL;
     96  1.11     lukem char *rl_completer_quote_characters = NULL;
     97  1.34  christos Function *rl_completion_entry_function = NULL;
     98  1.11     lukem CPPFunction *rl_attempted_completion_function = NULL;
     99  1.32  christos Function *rl_pre_input_hook = NULL;
    100  1.32  christos Function *rl_startup1_hook = NULL;
    101  1.32  christos Function *rl_getc_function = NULL;
    102  1.32  christos char *rl_terminal_name = NULL;
    103  1.32  christos int rl_already_prompted = 0;
    104  1.32  christos int rl_filename_completion_desired = 0;
    105  1.32  christos int rl_ignore_completion_duplicates = 0;
    106  1.32  christos VFunction *rl_redisplay_function = NULL;
    107  1.33  christos Function *rl_startup_hook = NULL;
    108  1.34  christos VFunction *rl_completion_display_matches_hook = NULL;
    109  1.33  christos VFunction *rl_prep_term_function = NULL;
    110  1.33  christos VFunction *rl_deprep_term_function = NULL;
    111   1.1  christos 
    112  1.12  jdolecek /*
    113  1.32  christos  * The current prompt string.
    114  1.32  christos  */
    115  1.32  christos char *rl_prompt = NULL;
    116  1.32  christos /*
    117  1.12  jdolecek  * This is set to character indicating type of completion being done by
    118  1.12  jdolecek  * rl_complete_internal(); this is available for application completion
    119  1.12  jdolecek  * functions.
    120  1.12  jdolecek  */
    121  1.12  jdolecek int rl_completion_type = 0;
    122  1.12  jdolecek 
    123  1.12  jdolecek /*
    124  1.12  jdolecek  * If more than this number of items results from query for possible
    125  1.12  jdolecek  * completions, we ask user if they are sure to really display the list.
    126  1.12  jdolecek  */
    127  1.12  jdolecek int rl_completion_query_items = 100;
    128  1.12  jdolecek 
    129  1.12  jdolecek /*
    130  1.15  jdolecek  * List of characters which are word break characters, but should be left
    131  1.15  jdolecek  * in the parsed text when it is passed to the completion function.
    132  1.15  jdolecek  * Shell uses this to help determine what kind of completing to do.
    133  1.12  jdolecek  */
    134  1.15  jdolecek char *rl_special_prefixes = (char *)NULL;
    135  1.15  jdolecek 
    136  1.15  jdolecek /*
    137  1.15  jdolecek  * This is the character appended to the completed words if at the end of
    138  1.15  jdolecek  * the line. Default is ' ' (a space).
    139  1.15  jdolecek  */
    140  1.15  jdolecek int rl_completion_append_character = ' ';
    141  1.15  jdolecek 
    142  1.15  jdolecek /* stuff below is used internally by libedit for readline emulation */
    143  1.15  jdolecek 
    144  1.15  jdolecek /* if not zero, non-unique completions always show list of possible matches */
    145  1.12  jdolecek static int _rl_complete_show_all = 0;
    146  1.12  jdolecek 
    147   1.1  christos static History *h = NULL;
    148   1.1  christos static EditLine *e = NULL;
    149  1.35  christos static Function *map[256];
    150  1.12  jdolecek static int el_rl_complete_cmdnum = 0;
    151   1.1  christos 
    152   1.1  christos /* internal functions */
    153  1.11     lukem static unsigned char	 _el_rl_complete(EditLine *, int);
    154  1.11     lukem static char		*_get_prompt(EditLine *);
    155  1.11     lukem static HIST_ENTRY	*_move_history(int);
    156  1.11     lukem static int		 _history_search_gen(const char *, int, int);
    157  1.11     lukem static int		 _history_expand_command(const char *, size_t, char **);
    158  1.11     lukem static char		*_rl_compat_sub(const char *, const char *,
    159  1.11     lukem 			    const char *, int);
    160  1.11     lukem static int		 rl_complete_internal(int);
    161  1.12  jdolecek static int		 _rl_qsort_string_compare(const void *, const void *);
    162   1.1  christos 
    163   1.5  christos 
    164   1.5  christos /* ARGSUSED */
    165   1.1  christos static char *
    166  1.30  christos _get_prompt(EditLine *el __attribute__((__unused__)))
    167   1.1  christos {
    168  1.32  christos 	rl_already_prompted = 1;
    169  1.32  christos 	return (rl_prompt);
    170   1.1  christos }
    171   1.1  christos 
    172  1.11     lukem 
    173   1.1  christos /*
    174   1.1  christos  * generic function for moving around history
    175   1.1  christos  */
    176   1.2  christos static HIST_ENTRY *
    177  1.11     lukem _move_history(int op)
    178   1.1  christos {
    179   1.1  christos 	HistEvent ev;
    180   1.1  christos 	static HIST_ENTRY rl_he;
    181   1.1  christos 
    182   1.1  christos 	if (history(h, &ev, op) != 0)
    183   1.1  christos 		return (HIST_ENTRY *) NULL;
    184   1.1  christos 
    185   1.1  christos 	rl_he.line = ev.str;
    186   1.1  christos 	rl_he.data = "";
    187   1.1  christos 
    188  1.11     lukem 	return (&rl_he);
    189   1.1  christos }
    190   1.1  christos 
    191   1.1  christos 
    192   1.7    simonb /*
    193   1.7    simonb  * READLINE compatibility stuff
    194   1.1  christos  */
    195   1.1  christos 
    196   1.1  christos /*
    197   1.1  christos  * initialize rl compat stuff
    198   1.1  christos  */
    199   1.1  christos int
    200  1.11     lukem rl_initialize(void)
    201   1.1  christos {
    202   1.1  christos 	HistEvent ev;
    203   1.1  christos 	const LineInfo *li;
    204  1.12  jdolecek 	int i;
    205  1.18  christos 	int editmode = 1;
    206  1.18  christos 	struct termios t;
    207   1.1  christos 
    208   1.1  christos 	if (e != NULL)
    209   1.1  christos 		el_end(e);
    210   1.1  christos 	if (h != NULL)
    211   1.1  christos 		history_end(h);
    212   1.1  christos 
    213   1.1  christos 	if (!rl_instream)
    214   1.1  christos 		rl_instream = stdin;
    215   1.1  christos 	if (!rl_outstream)
    216   1.1  christos 		rl_outstream = stdout;
    217  1.18  christos 
    218  1.18  christos 	/*
    219  1.18  christos 	 * See if we don't really want to run the editor
    220  1.18  christos 	 */
    221  1.18  christos 	if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0)
    222  1.18  christos 		editmode = 0;
    223  1.18  christos 
    224   1.4  christos 	e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);
    225  1.18  christos 
    226  1.18  christos 	if (!editmode)
    227  1.18  christos 		el_set(e, EL_EDITMODE, 0);
    228   1.1  christos 
    229   1.1  christos 	h = history_init();
    230   1.1  christos 	if (!e || !h)
    231  1.11     lukem 		return (-1);
    232   1.1  christos 
    233   1.4  christos 	history(h, &ev, H_SETSIZE, INT_MAX);	/* unlimited */
    234   1.1  christos 	history_length = 0;
    235   1.1  christos 	max_input_history = INT_MAX;
    236   1.1  christos 	el_set(e, EL_HIST, history, h);
    237   1.1  christos 
    238   1.1  christos 	/* for proper prompt printing in readline() */
    239  1.32  christos 	rl_prompt = strdup("");
    240  1.32  christos 	if (rl_prompt == NULL) {
    241  1.23  christos 		history_end(h);
    242  1.23  christos 		el_end(e);
    243  1.23  christos 		return -1;
    244  1.23  christos 	}
    245   1.1  christos 	el_set(e, EL_PROMPT, _get_prompt);
    246   1.1  christos 	el_set(e, EL_SIGNAL, 1);
    247   1.1  christos 
    248   1.1  christos 	/* set default mode to "emacs"-style and read setting afterwards */
    249   1.1  christos 	/* so this can be overriden */
    250   1.1  christos 	el_set(e, EL_EDITOR, "emacs");
    251  1.32  christos 	if (rl_terminal_name != NULL)
    252  1.32  christos 		el_set(e, EL_TERMINAL, rl_terminal_name);
    253  1.32  christos 	else
    254  1.32  christos 		el_get(e, EL_TERMINAL, &rl_terminal_name);
    255   1.1  christos 
    256  1.12  jdolecek 	/*
    257  1.35  christos 	 * Word completion - this has to go AFTER rebinding keys
    258  1.12  jdolecek 	 * to emacs-style.
    259  1.12  jdolecek 	 */
    260   1.1  christos 	el_set(e, EL_ADDFN, "rl_complete",
    261  1.35  christos 	    "ReadLine compatible completion function",
    262  1.11     lukem 	    _el_rl_complete);
    263   1.1  christos 	el_set(e, EL_BIND, "^I", "rl_complete", NULL);
    264  1.12  jdolecek 	/*
    265  1.12  jdolecek 	 * Find out where the rl_complete function was added; this is
    266  1.12  jdolecek 	 * used later to detect that lastcmd was also rl_complete.
    267  1.12  jdolecek 	 */
    268  1.12  jdolecek 	for(i=EL_NUM_FCNS; i < e->el_map.nfunc; i++) {
    269  1.12  jdolecek 		if (e->el_map.func[i] == _el_rl_complete) {
    270  1.12  jdolecek 			el_rl_complete_cmdnum = i;
    271  1.12  jdolecek 			break;
    272  1.12  jdolecek 		}
    273  1.12  jdolecek 	}
    274  1.12  jdolecek 
    275   1.1  christos 	/* read settings from configuration file */
    276   1.1  christos 	el_source(e, NULL);
    277   1.1  christos 
    278  1.12  jdolecek 	/*
    279  1.12  jdolecek 	 * Unfortunately, some applications really do use rl_point
    280  1.12  jdolecek 	 * and rl_line_buffer directly.
    281  1.12  jdolecek 	 */
    282   1.1  christos 	li = el_line(e);
    283  1.20  christos 	/* a cheesy way to get rid of const cast. */
    284  1.20  christos 	rl_line_buffer = memchr(li->buffer, *li->buffer, 1);
    285   1.1  christos 	rl_point = rl_end = 0;
    286   1.1  christos 
    287  1.32  christos 	if (rl_startup_hook)
    288  1.32  christos 		(*rl_startup_hook)(NULL, 0);
    289  1.32  christos 
    290  1.11     lukem 	return (0);
    291   1.1  christos }
    292   1.1  christos 
    293  1.11     lukem 
    294   1.1  christos /*
    295   1.1  christos  * read one line from input stream and return it, chomping
    296   1.1  christos  * trailing newline (if there is any)
    297   1.1  christos  */
    298   1.1  christos char *
    299   1.1  christos readline(const char *prompt)
    300   1.1  christos {
    301   1.1  christos 	HistEvent ev;
    302   1.3   thorpej 	int count;
    303   1.1  christos 	const char *ret;
    304  1.20  christos 	char *buf;
    305   1.1  christos 
    306   1.1  christos 	if (e == NULL || h == NULL)
    307   1.1  christos 		rl_initialize();
    308   1.1  christos 
    309   1.9  jdolecek 	/* update prompt accordingly to what has been passed */
    310  1.11     lukem 	if (!prompt)
    311  1.11     lukem 		prompt = "";
    312  1.32  christos 	if (strcmp(rl_prompt, prompt) != 0) {
    313  1.32  christos 		free(rl_prompt);
    314  1.32  christos 		rl_prompt = strdup(prompt);
    315  1.32  christos 		if (rl_prompt == NULL)
    316  1.23  christos 			return NULL;
    317   1.1  christos 	}
    318  1.32  christos 
    319  1.32  christos 	if (rl_pre_input_hook)
    320  1.32  christos 		(*rl_pre_input_hook)(NULL, 0);
    321  1.32  christos 
    322  1.32  christos 	rl_already_prompted = 0;
    323  1.32  christos 
    324   1.1  christos 	/* get one line from input stream */
    325   1.1  christos 	ret = el_gets(e, &count);
    326   1.1  christos 
    327   1.1  christos 	if (ret && count > 0) {
    328   1.5  christos 		int lastidx;
    329   1.1  christos 
    330  1.20  christos 		buf = strdup(ret);
    331  1.23  christos 		if (buf == NULL)
    332  1.23  christos 			return NULL;
    333   1.1  christos 		lastidx = count - 1;
    334  1.20  christos 		if (buf[lastidx] == '\n')
    335  1.20  christos 			buf[lastidx] = '\0';
    336   1.1  christos 	} else
    337  1.20  christos 		buf = NULL;
    338   1.1  christos 
    339   1.1  christos 	history(h, &ev, H_GETSIZE);
    340   1.1  christos 	history_length = ev.num;
    341   1.1  christos 
    342  1.20  christos 	return buf;
    343   1.1  christos }
    344   1.1  christos 
    345   1.1  christos /*
    346   1.1  christos  * history functions
    347   1.1  christos  */
    348   1.1  christos 
    349   1.1  christos /*
    350   1.1  christos  * is normally called before application starts to use
    351   1.1  christos  * history expansion functions
    352   1.1  christos  */
    353   1.1  christos void
    354  1.11     lukem using_history(void)
    355   1.1  christos {
    356   1.1  christos 	if (h == NULL || e == NULL)
    357   1.1  christos 		rl_initialize();
    358   1.1  christos }
    359   1.1  christos 
    360  1.11     lukem 
    361   1.1  christos /*
    362   1.1  christos  * substitute ``what'' with ``with'', returning resulting string; if
    363  1.29       wiz  * globally == 1, substitutes all occurrences of what, otherwise only the
    364   1.1  christos  * first one
    365   1.1  christos  */
    366  1.11     lukem static char *
    367  1.11     lukem _rl_compat_sub(const char *str, const char *what, const char *with,
    368  1.11     lukem     int globally)
    369  1.11     lukem {
    370  1.25  christos 	char *result;
    371  1.11     lukem 	const char *temp, *new;
    372  1.30  christos 	size_t len, with_len, what_len, add;
    373  1.11     lukem 	size_t size, i;
    374   1.1  christos 
    375   1.1  christos 	result = malloc((size = 16));
    376  1.23  christos 	if (result == NULL)
    377  1.23  christos 		return NULL;
    378   1.1  christos 	temp = str;
    379   1.1  christos 	with_len = strlen(with);
    380   1.1  christos 	what_len = strlen(what);
    381   1.1  christos 	len = 0;
    382   1.1  christos 	do {
    383   1.1  christos 		new = strstr(temp, what);
    384   1.1  christos 		if (new) {
    385   1.1  christos 			i = new - temp;
    386   1.1  christos 			add = i + with_len;
    387   1.1  christos 			if (i + add + 1 >= size) {
    388  1.23  christos 				char *nresult;
    389   1.1  christos 				size += add + 1;
    390  1.23  christos 				nresult = realloc(result, size);
    391  1.23  christos 				if (nresult == NULL) {
    392  1.23  christos 					free(result);
    393  1.23  christos 					return NULL;
    394  1.23  christos 				}
    395  1.23  christos 				result = nresult;
    396   1.1  christos 			}
    397  1.11     lukem 			(void) strncpy(&result[len], temp, i);
    398   1.1  christos 			len += i;
    399  1.11     lukem 			(void) strcpy(&result[len], with);	/* safe */
    400   1.1  christos 			len += with_len;
    401   1.1  christos 			temp = new + what_len;
    402   1.1  christos 		} else {
    403   1.1  christos 			add = strlen(temp);
    404   1.1  christos 			if (len + add + 1 >= size) {
    405  1.23  christos 				char *nresult;
    406   1.1  christos 				size += add + 1;
    407  1.23  christos 				nresult = realloc(result, size);
    408  1.23  christos 				if (nresult == NULL) {
    409  1.23  christos 					free(result);
    410  1.23  christos 					return NULL;
    411  1.23  christos 				}
    412  1.23  christos 				result = nresult;
    413   1.1  christos 			}
    414  1.11     lukem 			(void) strcpy(&result[len], temp);	/* safe */
    415   1.1  christos 			len += add;
    416   1.1  christos 			temp = NULL;
    417   1.1  christos 		}
    418  1.10  jdolecek 	} while (temp && globally);
    419   1.1  christos 	result[len] = '\0';
    420   1.1  christos 
    421  1.11     lukem 	return (result);
    422   1.1  christos }
    423   1.1  christos 
    424  1.11     lukem 
    425   1.1  christos /*
    426   1.1  christos  * the real function doing history expansion - takes as argument command
    427   1.1  christos  * to do and data upon which the command should be executed
    428   1.1  christos  * does expansion the way I've understood readline documentation
    429   1.1  christos  * word designator ``%'' isn't supported (yet ?)
    430   1.1  christos  *
    431   1.1  christos  * returns 0 if data was not modified, 1 if it was and 2 if the string
    432   1.1  christos  * should be only printed and not executed; in case of error,
    433   1.1  christos  * returns -1 and *result points to NULL
    434   1.1  christos  * it's callers responsibility to free() string returned in *result
    435   1.1  christos  */
    436   1.1  christos static int
    437  1.11     lukem _history_expand_command(const char *command, size_t cmdlen, char **result)
    438  1.11     lukem {
    439  1.11     lukem 	char **arr, *tempcmd, *line, *search = NULL, *cmd;
    440  1.11     lukem 	const char *event_data = NULL;
    441  1.11     lukem 	static char *from = NULL, *to = NULL;
    442  1.11     lukem 	int start = -1, end = -1, max, i, idx;
    443  1.11     lukem 	int h_on = 0, t_on = 0, r_on = 0, e_on = 0, p_on = 0, g_on = 0;
    444  1.11     lukem 	int event_num = 0, retval;
    445  1.11     lukem 	size_t cmdsize;
    446   1.1  christos 
    447   1.1  christos 	*result = NULL;
    448   1.1  christos 
    449   1.5  christos 	cmd = alloca(cmdlen + 1);
    450  1.11     lukem 	(void) strncpy(cmd, command, cmdlen);
    451   1.5  christos 	cmd[cmdlen] = 0;
    452   1.1  christos 
    453   1.1  christos 	idx = 1;
    454   1.1  christos 	/* find out which event to take */
    455   1.1  christos 	if (cmd[idx] == history_expansion_char) {
    456   1.1  christos 		event_num = history_length;
    457   1.1  christos 		idx++;
    458   1.1  christos 	} else {
    459  1.11     lukem 		int off, num;
    460   1.5  christos 		size_t len;
    461   1.1  christos 		off = idx;
    462   1.1  christos 		while (cmd[off] && !strchr(":^$*-%", cmd[off]))
    463   1.1  christos 			off++;
    464   1.1  christos 		num = atoi(&cmd[idx]);
    465   1.1  christos 		if (num != 0) {
    466   1.1  christos 			event_num = num;
    467   1.1  christos 			if (num < 0)
    468   1.1  christos 				event_num += history_length + 1;
    469   1.1  christos 		} else {
    470   1.1  christos 			int prefix = 1, curr_num;
    471   1.1  christos 			HistEvent ev;
    472   1.1  christos 
    473   1.1  christos 			len = off - idx;
    474   1.1  christos 			if (cmd[idx] == '?') {
    475   1.1  christos 				idx++, len--;
    476   1.1  christos 				if (cmd[off - 1] == '?')
    477   1.1  christos 					len--;
    478   1.1  christos 				else if (cmd[off] != '\n' && cmd[off] != '\0')
    479  1.11     lukem 					return (-1);
    480   1.1  christos 				prefix = 0;
    481   1.1  christos 			}
    482   1.1  christos 			search = alloca(len + 1);
    483  1.11     lukem 			(void) strncpy(search, &cmd[idx], len);
    484   1.1  christos 			search[len] = '\0';
    485   1.1  christos 
    486   1.1  christos 			if (history(h, &ev, H_CURR) != 0)
    487  1.11     lukem 				return (-1);
    488   1.1  christos 			curr_num = ev.num;
    489   1.1  christos 
    490   1.1  christos 			if (prefix)
    491   1.1  christos 				retval = history_search_prefix(search, -1);
    492   1.1  christos 			else
    493   1.1  christos 				retval = history_search(search, -1);
    494   1.1  christos 
    495   1.1  christos 			if (retval == -1) {
    496   1.1  christos 				fprintf(rl_outstream, "%s: Event not found\n",
    497  1.11     lukem 				    search);
    498  1.11     lukem 				return (-1);
    499   1.1  christos 			}
    500   1.1  christos 			if (history(h, &ev, H_CURR) != 0)
    501  1.11     lukem 				return (-1);
    502   1.1  christos 			event_data = ev.str;
    503   1.1  christos 
    504   1.1  christos 			/* roll back to original position */
    505   1.1  christos 			history(h, &ev, H_NEXT_EVENT, curr_num);
    506   1.1  christos 		}
    507   1.1  christos 		idx = off;
    508   1.1  christos 	}
    509   1.1  christos 
    510   1.1  christos 	if (!event_data && event_num >= 0) {
    511   1.2  christos 		HIST_ENTRY *rl_he;
    512   1.1  christos 		rl_he = history_get(event_num);
    513   1.1  christos 		if (!rl_he)
    514  1.11     lukem 			return (0);
    515   1.1  christos 		event_data = rl_he->line;
    516   1.1  christos 	} else
    517  1.11     lukem 		return (-1);
    518   1.1  christos 
    519   1.1  christos 	if (cmd[idx] != ':')
    520  1.11     lukem 		return (-1);
    521   1.1  christos 	cmd += idx + 1;
    522   1.1  christos 
    523   1.1  christos 	/* recognize cmd */
    524   1.1  christos 	if (*cmd == '^')
    525   1.1  christos 		start = end = 1, cmd++;
    526   1.1  christos 	else if (*cmd == '$')
    527   1.1  christos 		start = end = -1, cmd++;
    528   1.1  christos 	else if (*cmd == '*')
    529   1.1  christos 		start = 1, end = -1, cmd++;
    530   1.4  christos 	else if (isdigit((unsigned char) *cmd)) {
    531   1.1  christos 		const char *temp;
    532   1.1  christos 		int shifted = 0;
    533   1.1  christos 
    534   1.1  christos 		start = atoi(cmd);
    535   1.1  christos 		temp = cmd;
    536   1.4  christos 		for (; isdigit((unsigned char) *cmd); cmd++);
    537   1.1  christos 		if (temp != cmd)
    538   1.1  christos 			shifted = 1;
    539   1.1  christos 		if (shifted && *cmd == '-') {
    540   1.4  christos 			if (!isdigit((unsigned char) *(cmd + 1)))
    541   1.1  christos 				end = -2;
    542   1.1  christos 			else {
    543   1.1  christos 				end = atoi(cmd + 1);
    544   1.4  christos 				for (; isdigit((unsigned char) *cmd); cmd++);
    545   1.1  christos 			}
    546   1.1  christos 		} else if (shifted && *cmd == '*')
    547   1.1  christos 			end = -1, cmd++;
    548   1.1  christos 		else if (shifted)
    549   1.1  christos 			end = start;
    550   1.1  christos 	}
    551   1.1  christos 	if (*cmd == ':')
    552   1.1  christos 		cmd++;
    553   1.1  christos 
    554   1.1  christos 	line = strdup(event_data);
    555  1.23  christos 	if (line == NULL)
    556  1.28  christos 		return 0;
    557   1.1  christos 	for (; *cmd; cmd++) {
    558   1.1  christos 		if (*cmd == ':')
    559   1.1  christos 			continue;
    560   1.1  christos 		else if (*cmd == 'h')
    561   1.1  christos 			h_on = 1 | g_on, g_on = 0;
    562   1.1  christos 		else if (*cmd == 't')
    563   1.1  christos 			t_on = 1 | g_on, g_on = 0;
    564   1.1  christos 		else if (*cmd == 'r')
    565   1.1  christos 			r_on = 1 | g_on, g_on = 0;
    566   1.1  christos 		else if (*cmd == 'e')
    567   1.1  christos 			e_on = 1 | g_on, g_on = 0;
    568   1.1  christos 		else if (*cmd == 'p')
    569   1.1  christos 			p_on = 1 | g_on, g_on = 0;
    570   1.1  christos 		else if (*cmd == 'g')
    571   1.1  christos 			g_on = 2;
    572   1.1  christos 		else if (*cmd == 's' || *cmd == '&') {
    573  1.11     lukem 			char *what, *with, delim;
    574  1.30  christos 			size_t len, from_len;
    575   1.5  christos 			size_t size;
    576   1.1  christos 
    577   1.1  christos 			if (*cmd == '&' && (from == NULL || to == NULL))
    578   1.1  christos 				continue;
    579   1.1  christos 			else if (*cmd == 's') {
    580   1.1  christos 				delim = *(++cmd), cmd++;
    581   1.1  christos 				size = 16;
    582   1.5  christos 				what = realloc(from, size);
    583  1.23  christos 				if (what == NULL) {
    584  1.23  christos 					free(from);
    585  1.28  christos 					return 0;
    586  1.23  christos 				}
    587   1.1  christos 				len = 0;
    588   1.1  christos 				for (; *cmd && *cmd != delim; cmd++) {
    589   1.1  christos 					if (*cmd == '\\'
    590   1.1  christos 					    && *(cmd + 1) == delim)
    591   1.1  christos 						cmd++;
    592  1.23  christos 					if (len >= size) {
    593  1.23  christos 						char *nwhat;
    594  1.23  christos 						nwhat = realloc(what,
    595   1.1  christos 						    (size <<= 1));
    596  1.23  christos 						if (nwhat == NULL) {
    597  1.23  christos 							free(what);
    598  1.28  christos 							return 0;
    599  1.23  christos 						}
    600  1.23  christos 						what = nwhat;
    601  1.23  christos 					}
    602   1.1  christos 					what[len++] = *cmd;
    603   1.1  christos 				}
    604   1.1  christos 				what[len] = '\0';
    605   1.1  christos 				from = what;
    606   1.1  christos 				if (*what == '\0') {
    607   1.1  christos 					free(what);
    608  1.23  christos 					if (search) {
    609   1.1  christos 						from = strdup(search);
    610  1.23  christos 						if (from == NULL)
    611  1.28  christos 							return 0;
    612  1.23  christos 					} else {
    613   1.1  christos 						from = NULL;
    614  1.11     lukem 						return (-1);
    615   1.1  christos 					}
    616   1.1  christos 				}
    617   1.1  christos 				cmd++;	/* shift after delim */
    618   1.1  christos 				if (!*cmd)
    619   1.1  christos 					continue;
    620   1.1  christos 
    621   1.1  christos 				size = 16;
    622   1.5  christos 				with = realloc(to, size);
    623  1.23  christos 				if (with == NULL) {
    624  1.23  christos 					free(to);
    625  1.23  christos 					return -1;
    626  1.23  christos 				}
    627   1.1  christos 				len = 0;
    628   1.1  christos 				from_len = strlen(from);
    629   1.1  christos 				for (; *cmd && *cmd != delim; cmd++) {
    630   1.1  christos 					if (len + from_len + 1 >= size) {
    631  1.23  christos 						char *nwith;
    632   1.1  christos 						size += from_len + 1;
    633  1.23  christos 						nwith = realloc(with, size);
    634  1.23  christos 						if (nwith == NULL) {
    635  1.23  christos 							free(with);
    636  1.23  christos 							return -1;
    637  1.23  christos 						}
    638  1.23  christos 						with = nwith;
    639   1.1  christos 					}
    640   1.1  christos 					if (*cmd == '&') {
    641   1.1  christos 						/* safe */
    642  1.11     lukem 						(void) strcpy(&with[len], from);
    643   1.1  christos 						len += from_len;
    644   1.1  christos 						continue;
    645   1.1  christos 					}
    646   1.1  christos 					if (*cmd == '\\'
    647   1.1  christos 					    && (*(cmd + 1) == delim
    648   1.1  christos 						|| *(cmd + 1) == '&'))
    649   1.1  christos 						cmd++;
    650   1.1  christos 					with[len++] = *cmd;
    651   1.1  christos 				}
    652   1.1  christos 				with[len] = '\0';
    653   1.1  christos 				to = with;
    654   1.1  christos 
    655   1.7    simonb 				tempcmd = _rl_compat_sub(line, from, to,
    656   1.1  christos 				    (g_on) ? 1 : 0);
    657  1.24  christos 				if (tempcmd) {
    658  1.24  christos 					free(line);
    659  1.24  christos 					line = tempcmd;
    660  1.24  christos 				}
    661   1.1  christos 				g_on = 0;
    662   1.1  christos 			}
    663   1.1  christos 		}
    664   1.1  christos 	}
    665   1.1  christos 
    666   1.1  christos 	arr = history_tokenize(line);
    667   1.1  christos 	free(line);		/* no more needed */
    668   1.1  christos 	if (arr && *arr == NULL)
    669   1.1  christos 		free(arr), arr = NULL;
    670   1.1  christos 	if (!arr)
    671  1.11     lukem 		return (-1);
    672   1.1  christos 
    673   1.1  christos 	/* find out max valid idx to array of array */
    674   1.1  christos 	max = 0;
    675   1.1  christos 	for (i = 0; arr[i]; i++)
    676   1.1  christos 		max++;
    677   1.1  christos 	max--;
    678   1.1  christos 
    679   1.1  christos 	/* set boundaries to something relevant */
    680   1.1  christos 	if (start < 0)
    681   1.1  christos 		start = 1;
    682   1.1  christos 	if (end < 0)
    683   1.1  christos 		end = max - ((end < -1) ? 1 : 0);
    684   1.1  christos 
    685   1.1  christos 	/* check boundaries ... */
    686   1.1  christos 	if (start > max || end > max || start > end)
    687  1.11     lukem 		return (-1);
    688   1.1  christos 
    689   1.1  christos 	for (i = 0; i <= max; i++) {
    690  1.11     lukem 		char *temp;
    691   1.1  christos 		if (h_on && (i == 1 || h_on > 1) &&
    692   1.1  christos 		    (temp = strrchr(arr[i], '/')))
    693   1.1  christos 			*(temp + 1) = '\0';
    694   1.1  christos 		if (t_on && (i == 1 || t_on > 1) &&
    695   1.1  christos 		    (temp = strrchr(arr[i], '/')))
    696  1.11     lukem 			(void) strcpy(arr[i], temp + 1);
    697   1.1  christos 		if (r_on && (i == 1 || r_on > 1) &&
    698   1.1  christos 		    (temp = strrchr(arr[i], '.')))
    699   1.1  christos 			*temp = '\0';
    700   1.1  christos 		if (e_on && (i == 1 || e_on > 1) &&
    701   1.1  christos 		    (temp = strrchr(arr[i], '.')))
    702  1.11     lukem 			(void) strcpy(arr[i], temp);
    703   1.1  christos 	}
    704   1.1  christos 
    705   1.5  christos 	cmdsize = 1, cmdlen = 0;
    706  1.23  christos 	if ((tempcmd = malloc(cmdsize)) == NULL)
    707  1.28  christos 		return 0;
    708   1.1  christos 	for (i = start; start <= i && i <= end; i++) {
    709  1.11     lukem 		int arr_len;
    710   1.1  christos 
    711   1.1  christos 		arr_len = strlen(arr[i]);
    712   1.5  christos 		if (cmdlen + arr_len + 1 >= cmdsize) {
    713  1.23  christos 			char *ntempcmd;
    714   1.5  christos 			cmdsize += arr_len + 1;
    715  1.23  christos 			ntempcmd = realloc(tempcmd, cmdsize);
    716  1.23  christos 			if (ntempcmd == NULL) {
    717  1.23  christos 				free(tempcmd);
    718  1.28  christos 				return 0;
    719  1.23  christos 			}
    720  1.23  christos 			tempcmd = ntempcmd;
    721   1.1  christos 		}
    722  1.11     lukem 		(void) strcpy(&tempcmd[cmdlen], arr[i]);	/* safe */
    723   1.5  christos 		cmdlen += arr_len;
    724   1.5  christos 		tempcmd[cmdlen++] = ' ';	/* add a space */
    725   1.5  christos 	}
    726   1.5  christos 	while (cmdlen > 0 && isspace((unsigned char) tempcmd[cmdlen - 1]))
    727   1.5  christos 		cmdlen--;
    728   1.5  christos 	tempcmd[cmdlen] = '\0';
    729   1.1  christos 
    730   1.5  christos 	*result = tempcmd;
    731   1.1  christos 
    732   1.1  christos 	for (i = 0; i <= max; i++)
    733   1.1  christos 		free(arr[i]);
    734   1.1  christos 	free(arr), arr = (char **) NULL;
    735   1.1  christos 	return (p_on) ? 2 : 1;
    736   1.1  christos }
    737   1.1  christos 
    738  1.11     lukem 
    739   1.1  christos /*
    740   1.1  christos  * csh-style history expansion
    741   1.1  christos  */
    742   1.1  christos int
    743  1.11     lukem history_expand(char *str, char **output)
    744  1.11     lukem {
    745  1.11     lukem 	int i, retval = 0, idx;
    746  1.11     lukem 	size_t size;
    747  1.11     lukem 	char *temp, *result;
    748   1.1  christos 
    749   1.1  christos 	if (h == NULL || e == NULL)
    750   1.1  christos 		rl_initialize();
    751   1.1  christos 
    752   1.1  christos 	*output = strdup(str);	/* do it early */
    753  1.23  christos 	if (*output == NULL)
    754  1.28  christos 		return 0;
    755   1.1  christos 
    756   1.1  christos 	if (str[0] == history_subst_char) {
    757   1.1  christos 		/* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
    758   1.1  christos 		temp = alloca(4 + strlen(str) + 1);
    759   1.1  christos 		temp[0] = temp[1] = history_expansion_char;
    760   1.1  christos 		temp[2] = ':';
    761   1.1  christos 		temp[3] = 's';
    762  1.11     lukem 		(void) strcpy(temp + 4, str);
    763   1.1  christos 		str = temp;
    764   1.1  christos 	}
    765  1.11     lukem #define	ADD_STRING(what, len) 						\
    766   1.1  christos 	{								\
    767  1.23  christos 		if (idx + len + 1 > size) {				\
    768  1.23  christos 			char *nresult = realloc(result, (size += len + 1));\
    769  1.23  christos 			if (nresult == NULL) {				\
    770  1.23  christos 				free(*output);				\
    771  1.28  christos 				return 0;				\
    772  1.23  christos 			}						\
    773  1.23  christos 			result = nresult;				\
    774  1.23  christos 		}							\
    775   1.1  christos 		(void)strncpy(&result[idx], what, len);			\
    776   1.1  christos 		idx += len;						\
    777   1.1  christos 		result[idx] = '\0';					\
    778   1.1  christos 	}
    779   1.1  christos 
    780   1.1  christos 	result = NULL;
    781   1.1  christos 	size = idx = 0;
    782   1.1  christos 	for (i = 0; str[i];) {
    783   1.5  christos 		int start, j, loop_again;
    784   1.5  christos 		size_t len;
    785   1.1  christos 
    786   1.1  christos 		loop_again = 1;
    787   1.1  christos 		start = j = i;
    788   1.1  christos loop:
    789   1.1  christos 		for (; str[j]; j++) {
    790   1.1  christos 			if (str[j] == '\\' &&
    791   1.1  christos 			    str[j + 1] == history_expansion_char) {
    792  1.11     lukem 				(void) strcpy(&str[j], &str[j + 1]);
    793   1.1  christos 				continue;
    794   1.1  christos 			}
    795   1.1  christos 			if (!loop_again) {
    796   1.1  christos 				if (str[j] == '?') {
    797   1.1  christos 					while (str[j] && str[++j] != '?');
    798   1.1  christos 					if (str[j] == '?')
    799   1.1  christos 						j++;
    800   1.4  christos 				} else if (isspace((unsigned char) str[j]))
    801   1.1  christos 					break;
    802   1.1  christos 			}
    803   1.1  christos 			if (str[j] == history_expansion_char
    804   1.1  christos 			    && !strchr(history_no_expand_chars, str[j + 1])
    805   1.1  christos 			    && (!history_inhibit_expansion_function ||
    806  1.11     lukem 			    (*history_inhibit_expansion_function)(str, j) == 0))
    807   1.1  christos 				break;
    808   1.1  christos 		}
    809   1.1  christos 
    810   1.1  christos 		if (str[j] && str[j + 1] != '#' && loop_again) {
    811   1.1  christos 			i = j;
    812   1.1  christos 			j++;
    813   1.1  christos 			if (str[j] == history_expansion_char)
    814   1.1  christos 				j++;
    815   1.1  christos 			loop_again = 0;
    816   1.1  christos 			goto loop;
    817   1.1  christos 		}
    818   1.1  christos 		len = i - start;
    819   1.1  christos 		temp = &str[start];
    820   1.1  christos 		ADD_STRING(temp, len);
    821   1.1  christos 
    822   1.1  christos 		if (str[i] == '\0' || str[i] != history_expansion_char
    823   1.1  christos 		    || str[i + 1] == '#') {
    824   1.1  christos 			len = j - i;
    825   1.1  christos 			temp = &str[i];
    826   1.1  christos 			ADD_STRING(temp, len);
    827   1.1  christos 			if (start == 0)
    828   1.1  christos 				retval = 0;
    829   1.1  christos 			else
    830   1.1  christos 				retval = 1;
    831   1.1  christos 			break;
    832   1.1  christos 		}
    833  1.11     lukem 		retval = _history_expand_command(&str[i], (size_t) (j - i),
    834  1.11     lukem 		    &temp);
    835   1.1  christos 		if (retval != -1) {
    836   1.1  christos 			len = strlen(temp);
    837   1.1  christos 			ADD_STRING(temp, len);
    838   1.1  christos 		}
    839   1.1  christos 		i = j;
    840   1.1  christos 	}			/* for(i ...) */
    841   1.1  christos 
    842   1.1  christos 	if (retval == 2) {
    843   1.1  christos 		add_history(temp);
    844   1.1  christos #ifdef GDB_411_HACK
    845   1.1  christos 		/* gdb 4.11 has been shipped with readline, where */
    846   1.1  christos 		/* history_expand() returned -1 when the line	  */
    847   1.1  christos 		/* should not be executed; in readline 2.1+	  */
    848   1.1  christos 		/* it should return 2 in such a case		  */
    849   1.1  christos 		retval = -1;
    850   1.1  christos #endif
    851   1.1  christos 	}
    852   1.1  christos 	free(*output);
    853   1.1  christos 	*output = result;
    854   1.1  christos 
    855  1.11     lukem 	return (retval);
    856   1.1  christos }
    857   1.1  christos 
    858  1.11     lukem 
    859   1.1  christos /*
    860   1.9  jdolecek  * Parse the string into individual tokens, similarily to how shell would do it.
    861   1.1  christos  */
    862   1.1  christos char **
    863  1.11     lukem history_tokenize(const char *str)
    864   1.1  christos {
    865  1.11     lukem 	int size = 1, result_idx = 0, i, start;
    866   1.5  christos 	size_t len;
    867   1.1  christos 	char **result = NULL, *temp, delim = '\0';
    868   1.1  christos 
    869   1.1  christos 	for (i = 0; str[i]; i++) {
    870   1.4  christos 		while (isspace((unsigned char) str[i]))
    871   1.1  christos 			i++;
    872   1.1  christos 		start = i;
    873   1.1  christos 		for (; str[i]; i++) {
    874   1.9  jdolecek 			if (str[i] == '\\') {
    875  1.14  jdolecek 				if (str[i+1] != '\0')
    876   1.9  jdolecek 					i++;
    877   1.9  jdolecek 			} else if (str[i] == delim)
    878   1.1  christos 				delim = '\0';
    879   1.1  christos 			else if (!delim &&
    880  1.11     lukem 				    (isspace((unsigned char) str[i]) ||
    881  1.11     lukem 				strchr("()<>;&|$", str[i])))
    882   1.1  christos 				break;
    883   1.1  christos 			else if (!delim && strchr("'`\"", str[i]))
    884   1.1  christos 				delim = str[i];
    885   1.1  christos 		}
    886   1.1  christos 
    887   1.1  christos 		if (result_idx + 2 >= size) {
    888  1.23  christos 			char **nresult;
    889   1.1  christos 			size <<= 1;
    890  1.23  christos 			nresult = realloc(result, size * sizeof(char *));
    891  1.23  christos 			if (nresult == NULL) {
    892  1.23  christos 				free(result);
    893  1.23  christos 				return NULL;
    894  1.23  christos 			}
    895  1.23  christos 			result = nresult;
    896   1.1  christos 		}
    897   1.1  christos 		len = i - start;
    898   1.1  christos 		temp = malloc(len + 1);
    899  1.23  christos 		if (temp == NULL) {
    900  1.23  christos 			free(result);
    901  1.23  christos 			return NULL;
    902  1.23  christos 		}
    903  1.11     lukem 		(void) strncpy(temp, &str[start], len);
    904   1.1  christos 		temp[len] = '\0';
    905   1.1  christos 		result[result_idx++] = temp;
    906   1.1  christos 		result[result_idx] = NULL;
    907   1.1  christos 	}
    908   1.1  christos 
    909  1.11     lukem 	return (result);
    910   1.1  christos }
    911   1.1  christos 
    912  1.11     lukem 
    913   1.1  christos /*
    914   1.1  christos  * limit size of history record to ``max'' events
    915   1.1  christos  */
    916   1.1  christos void
    917  1.11     lukem stifle_history(int max)
    918   1.1  christos {
    919   1.1  christos 	HistEvent ev;
    920   1.1  christos 
    921   1.1  christos 	if (h == NULL || e == NULL)
    922   1.1  christos 		rl_initialize();
    923   1.1  christos 
    924   1.4  christos 	if (history(h, &ev, H_SETSIZE, max) == 0)
    925   1.1  christos 		max_input_history = max;
    926   1.1  christos }
    927   1.1  christos 
    928  1.11     lukem 
    929   1.1  christos /*
    930   1.1  christos  * "unlimit" size of history - set the limit to maximum allowed int value
    931   1.1  christos  */
    932   1.1  christos int
    933  1.11     lukem unstifle_history(void)
    934   1.1  christos {
    935   1.1  christos 	HistEvent ev;
    936   1.1  christos 	int omax;
    937   1.1  christos 
    938   1.4  christos 	history(h, &ev, H_SETSIZE, INT_MAX);
    939   1.1  christos 	omax = max_input_history;
    940   1.1  christos 	max_input_history = INT_MAX;
    941  1.11     lukem 	return (omax);		/* some value _must_ be returned */
    942   1.1  christos }
    943   1.1  christos 
    944  1.11     lukem 
    945   1.1  christos int
    946  1.11     lukem history_is_stifled(void)
    947   1.1  christos {
    948  1.11     lukem 
    949   1.1  christos 	/* cannot return true answer */
    950   1.1  christos 	return (max_input_history != INT_MAX);
    951   1.1  christos }
    952   1.1  christos 
    953  1.11     lukem 
    954   1.1  christos /*
    955   1.1  christos  * read history from a file given
    956   1.1  christos  */
    957   1.1  christos int
    958  1.11     lukem read_history(const char *filename)
    959   1.1  christos {
    960   1.1  christos 	HistEvent ev;
    961   1.1  christos 
    962   1.1  christos 	if (h == NULL || e == NULL)
    963   1.1  christos 		rl_initialize();
    964  1.11     lukem 	return (history(h, &ev, H_LOAD, filename));
    965   1.1  christos }
    966   1.1  christos 
    967  1.11     lukem 
    968   1.1  christos /*
    969   1.1  christos  * write history to a file given
    970   1.1  christos  */
    971   1.1  christos int
    972  1.11     lukem write_history(const char *filename)
    973   1.1  christos {
    974   1.1  christos 	HistEvent ev;
    975   1.1  christos 
    976   1.1  christos 	if (h == NULL || e == NULL)
    977   1.1  christos 		rl_initialize();
    978  1.11     lukem 	return (history(h, &ev, H_SAVE, filename));
    979   1.1  christos }
    980   1.1  christos 
    981  1.11     lukem 
    982   1.1  christos /*
    983   1.1  christos  * returns history ``num''th event
    984   1.1  christos  *
    985   1.1  christos  * returned pointer points to static variable
    986   1.1  christos  */
    987   1.2  christos HIST_ENTRY *
    988  1.11     lukem history_get(int num)
    989   1.1  christos {
    990   1.1  christos 	static HIST_ENTRY she;
    991   1.1  christos 	HistEvent ev;
    992   1.1  christos 	int i = 1, curr_num;
    993   1.1  christos 
    994   1.1  christos 	if (h == NULL || e == NULL)
    995   1.1  christos 		rl_initialize();
    996   1.1  christos 
    997   1.1  christos 	/* rewind to beginning */
    998   1.1  christos 	if (history(h, &ev, H_CURR) != 0)
    999  1.11     lukem 		return (NULL);
   1000   1.1  christos 	curr_num = ev.num;
   1001   1.1  christos 	if (history(h, &ev, H_LAST) != 0)
   1002  1.11     lukem 		return (NULL);	/* error */
   1003   1.1  christos 	while (i < num && history(h, &ev, H_PREV) == 0)
   1004   1.1  christos 		i++;
   1005   1.1  christos 	if (i != num)
   1006  1.11     lukem 		return (NULL);	/* not so many entries */
   1007   1.1  christos 
   1008   1.1  christos 	she.line = ev.str;
   1009   1.1  christos 	she.data = NULL;
   1010   1.1  christos 
   1011   1.1  christos 	/* rewind history to the same event it was before */
   1012   1.1  christos 	(void) history(h, &ev, H_FIRST);
   1013   1.1  christos 	(void) history(h, &ev, H_NEXT_EVENT, curr_num);
   1014   1.1  christos 
   1015  1.11     lukem 	return (&she);
   1016   1.1  christos }
   1017   1.1  christos 
   1018  1.11     lukem 
   1019   1.1  christos /*
   1020   1.1  christos  * add the line to history table
   1021   1.1  christos  */
   1022   1.1  christos int
   1023  1.11     lukem add_history(const char *line)
   1024   1.1  christos {
   1025   1.1  christos 	HistEvent ev;
   1026   1.1  christos 
   1027   1.1  christos 	if (h == NULL || e == NULL)
   1028   1.1  christos 		rl_initialize();
   1029   1.1  christos 
   1030   1.1  christos 	(void) history(h, &ev, H_ENTER, line);
   1031   1.1  christos 	if (history(h, &ev, H_GETSIZE) == 0)
   1032   1.1  christos 		history_length = ev.num;
   1033   1.1  christos 
   1034   1.1  christos 	return (!(history_length > 0));	/* return 0 if all is okay */
   1035   1.1  christos }
   1036   1.1  christos 
   1037  1.11     lukem 
   1038   1.1  christos /*
   1039   1.1  christos  * clear the history list - delete all entries
   1040   1.1  christos  */
   1041   1.1  christos void
   1042  1.11     lukem clear_history(void)
   1043   1.1  christos {
   1044   1.1  christos 	HistEvent ev;
   1045  1.11     lukem 
   1046   1.1  christos 	history(h, &ev, H_CLEAR);
   1047   1.1  christos }
   1048   1.1  christos 
   1049  1.11     lukem 
   1050   1.1  christos /*
   1051   1.1  christos  * returns offset of the current history event
   1052   1.1  christos  */
   1053   1.1  christos int
   1054  1.11     lukem where_history(void)
   1055   1.1  christos {
   1056   1.1  christos 	HistEvent ev;
   1057   1.1  christos 	int curr_num, off;
   1058   1.1  christos 
   1059   1.1  christos 	if (history(h, &ev, H_CURR) != 0)
   1060  1.11     lukem 		return (0);
   1061   1.1  christos 	curr_num = ev.num;
   1062   1.1  christos 
   1063   1.1  christos 	history(h, &ev, H_FIRST);
   1064   1.1  christos 	off = 1;
   1065   1.1  christos 	while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
   1066   1.1  christos 		off++;
   1067   1.1  christos 
   1068  1.11     lukem 	return (off);
   1069   1.1  christos }
   1070   1.1  christos 
   1071  1.11     lukem 
   1072   1.1  christos /*
   1073   1.1  christos  * returns current history event or NULL if there is no such event
   1074   1.1  christos  */
   1075   1.2  christos HIST_ENTRY *
   1076  1.11     lukem current_history(void)
   1077   1.1  christos {
   1078  1.11     lukem 
   1079  1.11     lukem 	return (_move_history(H_CURR));
   1080   1.1  christos }
   1081   1.1  christos 
   1082  1.11     lukem 
   1083   1.1  christos /*
   1084   1.1  christos  * returns total number of bytes history events' data are using
   1085   1.1  christos  */
   1086   1.1  christos int
   1087  1.11     lukem history_total_bytes(void)
   1088   1.1  christos {
   1089   1.1  christos 	HistEvent ev;
   1090   1.1  christos 	int curr_num, size;
   1091   1.1  christos 
   1092   1.1  christos 	if (history(h, &ev, H_CURR) != 0)
   1093  1.11     lukem 		return (-1);
   1094   1.1  christos 	curr_num = ev.num;
   1095   1.1  christos 
   1096   1.1  christos 	history(h, &ev, H_FIRST);
   1097   1.1  christos 	size = 0;
   1098   1.1  christos 	do
   1099   1.1  christos 		size += strlen(ev.str);
   1100   1.1  christos 	while (history(h, &ev, H_NEXT) == 0);
   1101   1.1  christos 
   1102   1.1  christos 	/* get to the same position as before */
   1103   1.1  christos 	history(h, &ev, H_PREV_EVENT, curr_num);
   1104   1.1  christos 
   1105  1.11     lukem 	return (size);
   1106   1.1  christos }
   1107   1.1  christos 
   1108  1.11     lukem 
   1109   1.1  christos /*
   1110   1.1  christos  * sets the position in the history list to ``pos''
   1111   1.1  christos  */
   1112   1.1  christos int
   1113  1.11     lukem history_set_pos(int pos)
   1114   1.1  christos {
   1115   1.1  christos 	HistEvent ev;
   1116   1.1  christos 	int off, curr_num;
   1117   1.1  christos 
   1118   1.1  christos 	if (pos > history_length || pos < 0)
   1119  1.11     lukem 		return (-1);
   1120   1.1  christos 
   1121   1.1  christos 	history(h, &ev, H_CURR);
   1122   1.1  christos 	curr_num = ev.num;
   1123   1.1  christos 	history(h, &ev, H_FIRST);
   1124   1.1  christos 	off = 0;
   1125   1.1  christos 	while (off < pos && history(h, &ev, H_NEXT) == 0)
   1126   1.1  christos 		off++;
   1127   1.1  christos 
   1128   1.1  christos 	if (off != pos) {	/* do a rollback in case of error */
   1129   1.1  christos 		history(h, &ev, H_FIRST);
   1130   1.1  christos 		history(h, &ev, H_NEXT_EVENT, curr_num);
   1131  1.11     lukem 		return (-1);
   1132   1.1  christos 	}
   1133  1.11     lukem 	return (0);
   1134   1.1  christos }
   1135   1.1  christos 
   1136  1.11     lukem 
   1137   1.1  christos /*
   1138   1.1  christos  * returns previous event in history and shifts pointer accordingly
   1139   1.1  christos  */
   1140   1.2  christos HIST_ENTRY *
   1141  1.11     lukem previous_history(void)
   1142   1.1  christos {
   1143  1.11     lukem 
   1144  1.11     lukem 	return (_move_history(H_PREV));
   1145   1.1  christos }
   1146   1.1  christos 
   1147  1.11     lukem 
   1148   1.1  christos /*
   1149   1.1  christos  * returns next event in history and shifts pointer accordingly
   1150   1.1  christos  */
   1151   1.2  christos HIST_ENTRY *
   1152  1.11     lukem next_history(void)
   1153   1.1  christos {
   1154  1.11     lukem 
   1155  1.11     lukem 	return (_move_history(H_NEXT));
   1156   1.1  christos }
   1157   1.1  christos 
   1158  1.11     lukem 
   1159   1.1  christos /*
   1160   1.1  christos  * generic history search function
   1161   1.1  christos  */
   1162   1.1  christos static int
   1163  1.11     lukem _history_search_gen(const char *str, int direction, int pos)
   1164  1.11     lukem {
   1165  1.11     lukem 	HistEvent ev;
   1166  1.11     lukem 	const char *strp;
   1167  1.11     lukem 	int curr_num;
   1168   1.1  christos 
   1169   1.1  christos 	if (history(h, &ev, H_CURR) != 0)
   1170  1.11     lukem 		return (-1);
   1171   1.1  christos 	curr_num = ev.num;
   1172   1.1  christos 
   1173   1.1  christos 	for (;;) {
   1174   1.1  christos 		strp = strstr(ev.str, str);
   1175   1.1  christos 		if (strp && (pos < 0 || &ev.str[pos] == strp))
   1176   1.1  christos 			return (int) (strp - ev.str);
   1177   1.1  christos 		if (history(h, &ev, direction < 0 ? H_PREV : H_NEXT) != 0)
   1178   1.1  christos 			break;
   1179   1.1  christos 	}
   1180   1.1  christos 
   1181   1.1  christos 	history(h, &ev, direction < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
   1182   1.1  christos 
   1183  1.11     lukem 	return (-1);
   1184   1.1  christos }
   1185   1.1  christos 
   1186  1.11     lukem 
   1187   1.1  christos /*
   1188   1.1  christos  * searches for first history event containing the str
   1189   1.1  christos  */
   1190   1.1  christos int
   1191  1.11     lukem history_search(const char *str, int direction)
   1192   1.1  christos {
   1193  1.11     lukem 
   1194  1.11     lukem 	return (_history_search_gen(str, direction, -1));
   1195   1.1  christos }
   1196   1.1  christos 
   1197  1.11     lukem 
   1198   1.1  christos /*
   1199   1.1  christos  * searches for first history event beginning with str
   1200   1.1  christos  */
   1201   1.1  christos int
   1202  1.11     lukem history_search_prefix(const char *str, int direction)
   1203   1.1  christos {
   1204  1.11     lukem 
   1205  1.11     lukem 	return (_history_search_gen(str, direction, 0));
   1206   1.1  christos }
   1207   1.1  christos 
   1208  1.11     lukem 
   1209   1.1  christos /*
   1210   1.1  christos  * search for event in history containing str, starting at offset
   1211   1.1  christos  * abs(pos); continue backward, if pos<0, forward otherwise
   1212   1.1  christos  */
   1213   1.5  christos /* ARGSUSED */
   1214   1.1  christos int
   1215  1.30  christos history_search_pos(const char *str,
   1216  1.30  christos 		   int direction __attribute__((__unused__)), int pos)
   1217   1.1  christos {
   1218  1.11     lukem 	HistEvent ev;
   1219  1.11     lukem 	int curr_num, off;
   1220   1.1  christos 
   1221   1.1  christos 	off = (pos > 0) ? pos : -pos;
   1222   1.1  christos 	pos = (pos > 0) ? 1 : -1;
   1223   1.1  christos 
   1224   1.1  christos 	if (history(h, &ev, H_CURR) != 0)
   1225  1.11     lukem 		return (-1);
   1226   1.1  christos 	curr_num = ev.num;
   1227   1.1  christos 
   1228   1.1  christos 	if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
   1229  1.11     lukem 		return (-1);
   1230   1.1  christos 
   1231   1.1  christos 
   1232   1.1  christos 	for (;;) {
   1233   1.1  christos 		if (strstr(ev.str, str))
   1234  1.11     lukem 			return (off);
   1235   1.1  christos 		if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
   1236   1.1  christos 			break;
   1237   1.1  christos 	}
   1238   1.1  christos 
   1239   1.1  christos 	/* set "current" pointer back to previous state */
   1240   1.1  christos 	history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
   1241   1.1  christos 
   1242  1.11     lukem 	return (-1);
   1243   1.1  christos }
   1244   1.1  christos 
   1245   1.1  christos 
   1246   1.1  christos /********************************/
   1247  1.35  christos /* completion functions	*/
   1248   1.1  christos 
   1249   1.1  christos /*
   1250   1.1  christos  * does tilde expansion of strings of type ``~user/foo''
   1251   1.1  christos  * if ``user'' isn't valid user name or ``txt'' doesn't start
   1252   1.1  christos  * w/ '~', returns pointer to strdup()ed copy of ``txt''
   1253   1.1  christos  *
   1254   1.1  christos  * it's callers's responsibility to free() returned string
   1255   1.1  christos  */
   1256   1.2  christos char *
   1257  1.11     lukem tilde_expand(char *txt)
   1258   1.1  christos {
   1259  1.11     lukem 	struct passwd *pass;
   1260  1.11     lukem 	char *temp;
   1261  1.11     lukem 	size_t len = 0;
   1262   1.1  christos 
   1263   1.1  christos 	if (txt[0] != '~')
   1264  1.11     lukem 		return (strdup(txt));
   1265   1.1  christos 
   1266   1.1  christos 	temp = strchr(txt + 1, '/');
   1267  1.23  christos 	if (temp == NULL) {
   1268   1.1  christos 		temp = strdup(txt + 1);
   1269  1.23  christos 		if (temp == NULL)
   1270  1.23  christos 			return NULL;
   1271  1.23  christos 	} else {
   1272   1.1  christos 		len = temp - txt + 1;	/* text until string after slash */
   1273   1.1  christos 		temp = malloc(len);
   1274  1.23  christos 		if (temp == NULL)
   1275  1.23  christos 			return NULL;
   1276  1.11     lukem 		(void) strncpy(temp, txt + 1, len - 2);
   1277   1.1  christos 		temp[len - 2] = '\0';
   1278   1.1  christos 	}
   1279   1.1  christos 	pass = getpwnam(temp);
   1280   1.1  christos 	free(temp);		/* value no more needed */
   1281   1.1  christos 	if (pass == NULL)
   1282  1.11     lukem 		return (strdup(txt));
   1283   1.1  christos 
   1284   1.1  christos 	/* update pointer txt to point at string immedially following */
   1285   1.1  christos 	/* first slash */
   1286   1.1  christos 	txt += len;
   1287   1.1  christos 
   1288   1.1  christos 	temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1);
   1289  1.23  christos 	if (temp == NULL)
   1290  1.23  christos 		return NULL;
   1291  1.11     lukem 	(void) sprintf(temp, "%s/%s", pass->pw_dir, txt);
   1292   1.1  christos 
   1293  1.11     lukem 	return (temp);
   1294   1.1  christos }
   1295   1.1  christos 
   1296  1.11     lukem 
   1297   1.1  christos /*
   1298   1.1  christos  * return first found file name starting by the ``text'' or NULL if no
   1299   1.1  christos  * such file can be found
   1300   1.1  christos  * value of ``state'' is ignored
   1301   1.1  christos  *
   1302   1.1  christos  * it's caller's responsibility to free returned string
   1303   1.1  christos  */
   1304  1.11     lukem char *
   1305  1.11     lukem filename_completion_function(const char *text, int state)
   1306  1.11     lukem {
   1307  1.11     lukem 	static DIR *dir = NULL;
   1308  1.11     lukem 	static char *filename = NULL, *dirname = NULL;
   1309  1.11     lukem 	static size_t filename_len = 0;
   1310  1.11     lukem 	struct dirent *entry;
   1311  1.11     lukem 	char *temp;
   1312  1.11     lukem 	size_t len;
   1313   1.1  christos 
   1314   1.1  christos 	if (state == 0 || dir == NULL) {
   1315   1.1  christos 		temp = strrchr(text, '/');
   1316   1.1  christos 		if (temp) {
   1317  1.23  christos 			char *nptr;
   1318   1.1  christos 			temp++;
   1319  1.23  christos 			nptr = realloc(filename, strlen(temp) + 1);
   1320  1.23  christos 			if (nptr == NULL) {
   1321  1.23  christos 				free(filename);
   1322  1.23  christos 				return NULL;
   1323  1.23  christos 			}
   1324  1.23  christos 			filename = nptr;
   1325  1.11     lukem 			(void) strcpy(filename, temp);
   1326   1.1  christos 			len = temp - text;	/* including last slash */
   1327  1.23  christos 			nptr = realloc(dirname, len + 1);
   1328  1.23  christos 			if (nptr == NULL) {
   1329  1.23  christos 				free(filename);
   1330  1.23  christos 				return NULL;
   1331  1.23  christos 			}
   1332  1.23  christos 			dirname = nptr;
   1333  1.11     lukem 			(void) strncpy(dirname, text, len);
   1334   1.1  christos 			dirname[len] = '\0';
   1335   1.1  christos 		} else {
   1336   1.1  christos 			filename = strdup(text);
   1337  1.23  christos 			if (filename == NULL)
   1338  1.23  christos 				return NULL;
   1339   1.1  christos 			dirname = NULL;
   1340   1.1  christos 		}
   1341   1.1  christos 
   1342   1.1  christos 		/* support for ``~user'' syntax */
   1343   1.1  christos 		if (dirname && *dirname == '~') {
   1344  1.23  christos 			char *nptr;
   1345   1.1  christos 			temp = tilde_expand(dirname);
   1346  1.23  christos 			if (temp == NULL)
   1347  1.23  christos 				return NULL;
   1348  1.23  christos 			nptr = realloc(dirname, strlen(temp) + 1);
   1349  1.23  christos 			if (nptr == NULL) {
   1350  1.23  christos 				free(dirname);
   1351  1.23  christos 				return NULL;
   1352  1.23  christos 			}
   1353  1.23  christos 			dirname = nptr;
   1354  1.11     lukem 			(void) strcpy(dirname, temp);	/* safe */
   1355  1.12  jdolecek 			free(temp);	/* no longer needed */
   1356   1.1  christos 		}
   1357   1.1  christos 		/* will be used in cycle */
   1358   1.1  christos 		filename_len = strlen(filename);
   1359   1.1  christos 		if (filename_len == 0)
   1360  1.11     lukem 			return (NULL);	/* no expansion possible */
   1361   1.1  christos 
   1362  1.26  christos 		if (dir != NULL) {
   1363  1.26  christos 			(void)closedir(dir);
   1364  1.26  christos 			dir = NULL;
   1365  1.26  christos 		}
   1366   1.1  christos 		dir = opendir(dirname ? dirname : ".");
   1367   1.1  christos 		if (!dir)
   1368  1.11     lukem 			return (NULL);	/* cannot open the directory */
   1369   1.1  christos 	}
   1370   1.1  christos 	/* find the match */
   1371   1.5  christos 	while ((entry = readdir(dir)) != NULL) {
   1372   1.1  christos 		/* otherwise, get first entry where first */
   1373   1.1  christos 		/* filename_len characters are equal	  */
   1374   1.1  christos 		if (entry->d_name[0] == filename[0]
   1375   1.6  christos #if defined(__SVR4) || defined(__linux__)
   1376   1.6  christos 		    && strlen(entry->d_name) >= filename_len
   1377   1.6  christos #else
   1378   1.1  christos 		    && entry->d_namlen >= filename_len
   1379   1.4  christos #endif
   1380   1.1  christos 		    && strncmp(entry->d_name, filename,
   1381  1.11     lukem 			filename_len) == 0)
   1382   1.1  christos 			break;
   1383   1.1  christos 	}
   1384   1.1  christos 
   1385   1.1  christos 	if (entry) {		/* match found */
   1386   1.1  christos 
   1387  1.11     lukem 		struct stat stbuf;
   1388   1.6  christos #if defined(__SVR4) || defined(__linux__)
   1389   1.6  christos 		len = strlen(entry->d_name) +
   1390   1.6  christos #else
   1391   1.1  christos 		len = entry->d_namlen +
   1392   1.4  christos #endif
   1393  1.11     lukem 		    ((dirname) ? strlen(dirname) : 0) + 1 + 1;
   1394   1.1  christos 		temp = malloc(len);
   1395  1.23  christos 		if (temp == NULL)
   1396  1.23  christos 			return NULL;
   1397  1.11     lukem 		(void) sprintf(temp, "%s%s",
   1398  1.11     lukem 		    dirname ? dirname : "", entry->d_name);	/* safe */
   1399   1.1  christos 
   1400   1.1  christos 		/* test, if it's directory */
   1401   1.1  christos 		if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
   1402   1.1  christos 			strcat(temp, "/");	/* safe */
   1403  1.26  christos 	} else {
   1404  1.26  christos 		(void)closedir(dir);
   1405  1.26  christos 		dir = NULL;
   1406   1.1  christos 		temp = NULL;
   1407  1.26  christos 	}
   1408   1.1  christos 
   1409  1.11     lukem 	return (temp);
   1410   1.1  christos }
   1411   1.1  christos 
   1412  1.11     lukem 
   1413   1.1  christos /*
   1414   1.1  christos  * a completion generator for usernames; returns _first_ username
   1415   1.1  christos  * which starts with supplied text
   1416   1.1  christos  * text contains a partial username preceded by random character
   1417   1.1  christos  * (usually '~'); state is ignored
   1418   1.1  christos  * it's callers responsibility to free returned value
   1419   1.1  christos  */
   1420  1.11     lukem char *
   1421  1.11     lukem username_completion_function(const char *text, int state)
   1422   1.1  christos {
   1423  1.11     lukem 	struct passwd *pwd;
   1424   1.1  christos 
   1425   1.1  christos 	if (text[0] == '\0')
   1426  1.11     lukem 		return (NULL);
   1427   1.1  christos 
   1428   1.1  christos 	if (*text == '~')
   1429   1.1  christos 		text++;
   1430   1.1  christos 
   1431   1.1  christos 	if (state == 0)
   1432   1.1  christos 		setpwent();
   1433   1.1  christos 
   1434   1.1  christos 	while ((pwd = getpwent()) && text[0] == pwd->pw_name[0]
   1435  1.11     lukem 	    && strcmp(text, pwd->pw_name) == 0);
   1436   1.1  christos 
   1437   1.1  christos 	if (pwd == NULL) {
   1438   1.1  christos 		endpwent();
   1439  1.11     lukem 		return (NULL);
   1440   1.1  christos 	}
   1441  1.11     lukem 	return (strdup(pwd->pw_name));
   1442   1.1  christos }
   1443   1.1  christos 
   1444  1.11     lukem 
   1445   1.1  christos /*
   1446   1.1  christos  * el-compatible wrapper around rl_complete; needed for key binding
   1447   1.1  christos  */
   1448   1.5  christos /* ARGSUSED */
   1449   1.1  christos static unsigned char
   1450  1.30  christos _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch)
   1451   1.1  christos {
   1452   1.1  christos 	return (unsigned char) rl_complete(0, ch);
   1453   1.1  christos }
   1454   1.1  christos 
   1455  1.11     lukem 
   1456   1.1  christos /*
   1457  1.35  christos  * returns list of completions for text given
   1458   1.1  christos  */
   1459  1.11     lukem char **
   1460  1.11     lukem completion_matches(const char *text, CPFunction *genfunc)
   1461  1.11     lukem {
   1462  1.11     lukem 	char **match_list = NULL, *retstr, *prevstr;
   1463  1.12  jdolecek 	size_t match_list_len, max_equal, which, i;
   1464  1.30  christos 	size_t matches;
   1465   1.1  christos 
   1466   1.1  christos 	if (h == NULL || e == NULL)
   1467   1.1  christos 		rl_initialize();
   1468   1.1  christos 
   1469   1.1  christos 	matches = 0;
   1470  1.12  jdolecek 	match_list_len = 1;
   1471  1.30  christos 	while ((retstr = (*genfunc) (text, (int)matches)) != NULL) {
   1472  1.24  christos 		/* allow for list terminator here */
   1473  1.31  christos 		if (matches + 3 >= match_list_len) {
   1474  1.23  christos 			char **nmatch_list;
   1475  1.31  christos 			while (matches + 3 >= match_list_len)
   1476  1.31  christos 				match_list_len <<= 1;
   1477  1.23  christos 			nmatch_list = realloc(match_list,
   1478  1.12  jdolecek 			    match_list_len * sizeof(char *));
   1479  1.23  christos 			if (nmatch_list == NULL) {
   1480  1.23  christos 				free(match_list);
   1481  1.23  christos 				return NULL;
   1482  1.23  christos 			}
   1483  1.23  christos 			match_list = nmatch_list;
   1484  1.23  christos 
   1485   1.1  christos 		}
   1486   1.1  christos 		match_list[++matches] = retstr;
   1487   1.1  christos 	}
   1488   1.1  christos 
   1489   1.1  christos 	if (!match_list)
   1490  1.23  christos 		return NULL;	/* nothing found */
   1491   1.1  christos 
   1492   1.1  christos 	/* find least denominator and insert it to match_list[0] */
   1493   1.1  christos 	which = 2;
   1494   1.1  christos 	prevstr = match_list[1];
   1495   1.5  christos 	max_equal = strlen(prevstr);
   1496  1.12  jdolecek 	for (; which <= matches; which++) {
   1497   1.1  christos 		for (i = 0; i < max_equal &&
   1498   1.1  christos 		    prevstr[i] == match_list[which][i]; i++)
   1499   1.1  christos 			continue;
   1500   1.1  christos 		max_equal = i;
   1501   1.1  christos 	}
   1502   1.1  christos 
   1503   1.1  christos 	retstr = malloc(max_equal + 1);
   1504  1.24  christos 	if (retstr == NULL) {
   1505  1.24  christos 		free(match_list);
   1506  1.23  christos 		return NULL;
   1507  1.24  christos 	}
   1508  1.11     lukem 	(void) strncpy(retstr, match_list[1], max_equal);
   1509   1.1  christos 	retstr[max_equal] = '\0';
   1510   1.1  christos 	match_list[0] = retstr;
   1511   1.1  christos 
   1512   1.1  christos 	/* add NULL as last pointer to the array */
   1513   1.1  christos 	match_list[matches + 1] = (char *) NULL;
   1514   1.1  christos 
   1515  1.11     lukem 	return (match_list);
   1516   1.1  christos }
   1517   1.1  christos 
   1518  1.12  jdolecek /*
   1519  1.12  jdolecek  * Sort function for qsort(). Just wrapper around strcasecmp().
   1520  1.12  jdolecek  */
   1521  1.12  jdolecek static int
   1522  1.12  jdolecek _rl_qsort_string_compare(i1, i2)
   1523  1.12  jdolecek 	const void *i1, *i2;
   1524  1.12  jdolecek {
   1525  1.22   thorpej 	const char *s1 = ((const char * const *)i1)[0];
   1526  1.22   thorpej 	const char *s2 = ((const char * const *)i2)[0];
   1527  1.12  jdolecek 
   1528  1.12  jdolecek 	return strcasecmp(s1, s2);
   1529  1.12  jdolecek }
   1530  1.11     lukem 
   1531   1.1  christos /*
   1532  1.12  jdolecek  * Display list of strings in columnar format on readline's output stream.
   1533  1.12  jdolecek  * 'matches' is list of strings, 'len' is number of strings in 'matches',
   1534  1.12  jdolecek  * 'max' is maximum length of string in 'matches'.
   1535  1.12  jdolecek  */
   1536  1.12  jdolecek void
   1537  1.12  jdolecek rl_display_match_list (matches, len, max)
   1538  1.12  jdolecek      char **matches;
   1539  1.12  jdolecek      int len, max;
   1540  1.12  jdolecek {
   1541  1.12  jdolecek 	int i, idx, limit, count;
   1542  1.12  jdolecek 	int screenwidth = e->el_term.t_size.h;
   1543  1.12  jdolecek 
   1544  1.13  jdolecek 	/*
   1545  1.13  jdolecek 	 * Find out how many entries can be put on one line, count
   1546  1.13  jdolecek 	 * with two spaces between strings.
   1547  1.13  jdolecek 	 */
   1548  1.13  jdolecek 	limit = screenwidth / (max + 2);
   1549  1.12  jdolecek 	if (limit == 0)
   1550  1.12  jdolecek 		limit = 1;
   1551  1.12  jdolecek 
   1552  1.12  jdolecek 	/* how many lines of output */
   1553  1.12  jdolecek 	count = len / limit;
   1554  1.13  jdolecek 	if (count * limit < len)
   1555  1.13  jdolecek 		count++;
   1556  1.12  jdolecek 
   1557  1.12  jdolecek 	/* Sort the items if they are not already sorted. */
   1558  1.16  christos 	qsort(&matches[1], (size_t)(len - 1), sizeof(char *),
   1559  1.16  christos 	    _rl_qsort_string_compare);
   1560  1.12  jdolecek 
   1561  1.12  jdolecek 	idx = 1;
   1562  1.13  jdolecek 	for(; count > 0; count--) {
   1563  1.12  jdolecek 		for(i=0; i < limit && matches[idx]; i++, idx++)
   1564  1.13  jdolecek 			fprintf(e->el_outfile, "%-*s  ", max, matches[idx]);
   1565  1.12  jdolecek 		fprintf(e->el_outfile, "\n");
   1566  1.12  jdolecek 	}
   1567  1.12  jdolecek }
   1568  1.12  jdolecek 
   1569  1.12  jdolecek /*
   1570  1.12  jdolecek  * Complete the word at or before point, called by rl_complete()
   1571  1.12  jdolecek  * 'what_to_do' says what to do with the completion.
   1572  1.12  jdolecek  * `?' means list the possible completions.
   1573  1.12  jdolecek  * TAB means do standard completion.
   1574  1.12  jdolecek  * `*' means insert all of the possible completions.
   1575  1.12  jdolecek  * `!' means to do standard completion, and list all possible completions if
   1576  1.12  jdolecek  * there is more than one.
   1577  1.12  jdolecek  *
   1578  1.12  jdolecek  * Note: '*' support is not implemented
   1579   1.1  christos  */
   1580   1.1  christos static int
   1581  1.11     lukem rl_complete_internal(int what_to_do)
   1582   1.1  christos {
   1583  1.34  christos 	Function *complet_func;
   1584   1.1  christos 	const LineInfo *li;
   1585  1.12  jdolecek 	char *temp, **matches;
   1586  1.12  jdolecek 	const char *ctemp;
   1587  1.11     lukem 	size_t len;
   1588   1.1  christos 
   1589  1.12  jdolecek 	rl_completion_type = what_to_do;
   1590  1.12  jdolecek 
   1591   1.1  christos 	if (h == NULL || e == NULL)
   1592   1.1  christos 		rl_initialize();
   1593   1.1  christos 
   1594   1.1  christos 	complet_func = rl_completion_entry_function;
   1595   1.1  christos 	if (!complet_func)
   1596  1.34  christos 		complet_func = (Function *)(void *)filename_completion_function;
   1597   1.1  christos 
   1598  1.15  jdolecek 	/* We now look backwards for the start of a filename/variable word */
   1599   1.1  christos 	li = el_line(e);
   1600  1.16  christos 	ctemp = (const char *) li->cursor;
   1601  1.15  jdolecek 	while (ctemp > li->buffer
   1602  1.15  jdolecek 	    && !strchr(rl_basic_word_break_characters, ctemp[-1])
   1603  1.15  jdolecek 	    && (!rl_special_prefixes
   1604  1.15  jdolecek 			|| !strchr(rl_special_prefixes, ctemp[-1]) ) )
   1605  1.12  jdolecek 		ctemp--;
   1606  1.12  jdolecek 
   1607  1.12  jdolecek 	len = li->cursor - ctemp;
   1608  1.12  jdolecek 	temp = alloca(len + 1);
   1609  1.12  jdolecek 	(void) strncpy(temp, ctemp, len);
   1610   1.1  christos 	temp[len] = '\0';
   1611   1.1  christos 
   1612   1.1  christos 	/* these can be used by function called in completion_matches() */
   1613   1.1  christos 	/* or (*rl_attempted_completion_function)() */
   1614   1.1  christos 	rl_point = li->cursor - li->buffer;
   1615   1.1  christos 	rl_end = li->lastchar - li->buffer;
   1616   1.1  christos 
   1617   1.1  christos 	if (!rl_attempted_completion_function)
   1618  1.34  christos 		matches = completion_matches(temp, (CPFunction *)complet_func);
   1619   1.1  christos 	else {
   1620  1.11     lukem 		int end = li->cursor - li->buffer;
   1621  1.12  jdolecek 		matches = (*rl_attempted_completion_function) (temp, (int)
   1622  1.11     lukem 		    (end - len), end);
   1623   1.1  christos 	}
   1624   1.1  christos 
   1625  1.12  jdolecek 	if (matches) {
   1626  1.12  jdolecek 		int i, retval = CC_REFRESH;
   1627  1.12  jdolecek 		int matches_num, maxlen, match_len, match_display=1;
   1628   1.1  christos 
   1629  1.14  jdolecek 		/*
   1630  1.14  jdolecek 		 * Only replace the completed string with common part of
   1631  1.14  jdolecek 		 * possible matches if there is possible completion.
   1632  1.14  jdolecek 		 */
   1633  1.14  jdolecek 		if (matches[0][0] != '\0') {
   1634  1.14  jdolecek 			el_deletestr(e, (int) len);
   1635  1.14  jdolecek 			el_insertstr(e, matches[0]);
   1636  1.14  jdolecek 		}
   1637  1.12  jdolecek 
   1638  1.12  jdolecek 		if (what_to_do == '?')
   1639  1.12  jdolecek 			goto display_matches;
   1640  1.12  jdolecek 
   1641  1.12  jdolecek 		if (matches[2] == NULL && strcmp(matches[0], matches[1]) == 0) {
   1642  1.12  jdolecek 			/*
   1643  1.12  jdolecek 			 * We found exact match. Add a space after
   1644  1.35  christos 			 * it, unless we do filename completion and the
   1645  1.12  jdolecek 			 * object is a directory.
   1646  1.12  jdolecek 			 */
   1647  1.12  jdolecek 			size_t alen = strlen(matches[0]);
   1648  1.34  christos 			if ((complet_func !=
   1649  1.34  christos 			    (Function *)filename_completion_function
   1650  1.15  jdolecek 			      || (alen > 0 && (matches[0])[alen - 1] != '/'))
   1651  1.15  jdolecek 			    && rl_completion_append_character) {
   1652  1.15  jdolecek 				char buf[2];
   1653  1.15  jdolecek 				buf[0] = rl_completion_append_character;
   1654  1.15  jdolecek 				buf[1] = '\0';
   1655  1.15  jdolecek 				el_insertstr(e, buf);
   1656  1.15  jdolecek 			}
   1657  1.12  jdolecek 		} else if (what_to_do == '!') {
   1658  1.12  jdolecek     display_matches:
   1659  1.12  jdolecek 			/*
   1660  1.12  jdolecek 			 * More than one match and requested to list possible
   1661  1.12  jdolecek 			 * matches.
   1662  1.12  jdolecek 			 */
   1663  1.12  jdolecek 
   1664  1.12  jdolecek 			for(i=1, maxlen=0; matches[i]; i++) {
   1665  1.12  jdolecek 				match_len = strlen(matches[i]);
   1666  1.12  jdolecek 				if (match_len > maxlen)
   1667  1.12  jdolecek 					maxlen = match_len;
   1668  1.12  jdolecek 			}
   1669  1.12  jdolecek 			matches_num = i - 1;
   1670  1.12  jdolecek 
   1671  1.12  jdolecek 			/* newline to get on next line from command line */
   1672  1.12  jdolecek 			fprintf(e->el_outfile, "\n");
   1673  1.12  jdolecek 
   1674  1.12  jdolecek 			/*
   1675  1.12  jdolecek 			 * If there are too many items, ask user for display
   1676  1.12  jdolecek 			 * confirmation.
   1677  1.12  jdolecek 			 */
   1678  1.12  jdolecek 			if (matches_num > rl_completion_query_items) {
   1679  1.12  jdolecek 				fprintf(e->el_outfile,
   1680  1.12  jdolecek 				"Display all %d possibilities? (y or n) ",
   1681  1.12  jdolecek 					matches_num);
   1682  1.12  jdolecek 				fflush(e->el_outfile);
   1683  1.12  jdolecek 				if (getc(stdin) != 'y')
   1684  1.12  jdolecek 					match_display = 0;
   1685  1.12  jdolecek 				fprintf(e->el_outfile, "\n");
   1686  1.12  jdolecek 			}
   1687  1.12  jdolecek 
   1688  1.12  jdolecek 			if (match_display)
   1689  1.12  jdolecek 				rl_display_match_list(matches, matches_num,
   1690  1.12  jdolecek 					maxlen);
   1691  1.12  jdolecek 			retval = CC_REDISPLAY;
   1692  1.19  jdolecek 		} else if (matches[0][0]) {
   1693  1.19  jdolecek 			/*
   1694  1.19  jdolecek 			 * There was some common match, but the name was
   1695  1.19  jdolecek 			 * not complete enough. Next tab will print possible
   1696  1.19  jdolecek 			 * completions.
   1697  1.19  jdolecek 			 */
   1698  1.19  jdolecek 			el_beep(e);
   1699  1.12  jdolecek 		} else {
   1700   1.1  christos 			/* lcd is not a valid object - further specification */
   1701   1.1  christos 			/* is needed */
   1702   1.4  christos 			el_beep(e);
   1703  1.12  jdolecek 			retval = CC_NORM;
   1704  1.12  jdolecek 		}
   1705   1.1  christos 
   1706   1.1  christos 		/* free elements of array and the array itself */
   1707  1.12  jdolecek 		for (i = 0; matches[i]; i++)
   1708  1.12  jdolecek 			free(matches[i]);
   1709  1.12  jdolecek 		free(matches), matches = NULL;
   1710   1.1  christos 
   1711  1.12  jdolecek 		return (retval);
   1712   1.1  christos 	}
   1713  1.11     lukem 	return (CC_NORM);
   1714   1.1  christos }
   1715   1.1  christos 
   1716  1.11     lukem 
   1717   1.1  christos /*
   1718   1.1  christos  * complete word at current point
   1719   1.1  christos  */
   1720   1.1  christos int
   1721  1.11     lukem rl_complete(int ignore, int invoking_key)
   1722   1.1  christos {
   1723   1.1  christos 	if (h == NULL || e == NULL)
   1724   1.1  christos 		rl_initialize();
   1725   1.1  christos 
   1726   1.1  christos 	if (rl_inhibit_completion) {
   1727   1.1  christos 		rl_insert(ignore, invoking_key);
   1728  1.11     lukem 		return (CC_REFRESH);
   1729  1.12  jdolecek 	} else if (e->el_state.lastcmd == el_rl_complete_cmdnum)
   1730  1.12  jdolecek 		return rl_complete_internal('?');
   1731  1.12  jdolecek 	else if (_rl_complete_show_all)
   1732  1.12  jdolecek 		return rl_complete_internal('!');
   1733  1.12  jdolecek 	else
   1734  1.12  jdolecek 		return (rl_complete_internal(TAB));
   1735   1.1  christos }
   1736   1.1  christos 
   1737  1.11     lukem 
   1738   1.1  christos /*
   1739   1.7    simonb  * misc other functions
   1740   1.1  christos  */
   1741   1.1  christos 
   1742   1.1  christos /*
   1743   1.1  christos  * bind key c to readline-type function func
   1744   1.1  christos  */
   1745   1.1  christos int
   1746  1.11     lukem rl_bind_key(int c, int func(int, int))
   1747   1.1  christos {
   1748  1.11     lukem 	int retval = -1;
   1749   1.1  christos 
   1750   1.1  christos 	if (h == NULL || e == NULL)
   1751   1.1  christos 		rl_initialize();
   1752   1.1  christos 
   1753   1.1  christos 	if (func == rl_insert) {
   1754   1.1  christos 		/* XXX notice there is no range checking of ``c'' */
   1755   1.1  christos 		e->el_map.key[c] = ED_INSERT;
   1756   1.1  christos 		retval = 0;
   1757   1.1  christos 	}
   1758  1.11     lukem 	return (retval);
   1759   1.1  christos }
   1760   1.1  christos 
   1761  1.11     lukem 
   1762   1.1  christos /*
   1763   1.1  christos  * read one key from input - handles chars pushed back
   1764   1.1  christos  * to input stream also
   1765   1.1  christos  */
   1766   1.1  christos int
   1767  1.11     lukem rl_read_key(void)
   1768   1.1  christos {
   1769  1.11     lukem 	char fooarr[2 * sizeof(int)];
   1770   1.1  christos 
   1771   1.1  christos 	if (e == NULL || h == NULL)
   1772   1.1  christos 		rl_initialize();
   1773   1.1  christos 
   1774  1.11     lukem 	return (el_getc(e, fooarr));
   1775   1.1  christos }
   1776   1.1  christos 
   1777  1.11     lukem 
   1778   1.1  christos /*
   1779   1.1  christos  * reset the terminal
   1780   1.1  christos  */
   1781   1.5  christos /* ARGSUSED */
   1782   1.1  christos void
   1783  1.30  christos rl_reset_terminal(const char *p __attribute__((__unused__)))
   1784   1.1  christos {
   1785  1.11     lukem 
   1786   1.1  christos 	if (h == NULL || e == NULL)
   1787   1.1  christos 		rl_initialize();
   1788   1.1  christos 	el_reset(e);
   1789   1.1  christos }
   1790   1.1  christos 
   1791  1.11     lukem 
   1792   1.1  christos /*
   1793   1.1  christos  * insert character ``c'' back into input stream, ``count'' times
   1794   1.1  christos  */
   1795   1.1  christos int
   1796  1.11     lukem rl_insert(int count, int c)
   1797   1.1  christos {
   1798  1.11     lukem 	char arr[2];
   1799   1.1  christos 
   1800   1.1  christos 	if (h == NULL || e == NULL)
   1801   1.1  christos 		rl_initialize();
   1802   1.1  christos 
   1803   1.1  christos 	/* XXX - int -> char conversion can lose on multichars */
   1804   1.1  christos 	arr[0] = c;
   1805   1.1  christos 	arr[1] = '\0';
   1806   1.1  christos 
   1807   1.1  christos 	for (; count > 0; count--)
   1808   1.1  christos 		el_push(e, arr);
   1809   1.1  christos 
   1810  1.11     lukem 	return (0);
   1811  1.35  christos }
   1812  1.35  christos 
   1813  1.35  christos /*ARGSUSED*/
   1814  1.35  christos int
   1815  1.35  christos rl_newline(int count, int c)
   1816  1.35  christos {
   1817  1.35  christos 	/*
   1818  1.35  christos 	 * Readline-4.0 appears to ignore the args.
   1819  1.35  christos 	 */
   1820  1.35  christos 	return rl_insert(1, '\n');
   1821  1.35  christos }
   1822  1.35  christos 
   1823  1.35  christos /*ARGSUSED*/
   1824  1.35  christos static unsigned char
   1825  1.35  christos rl_bind_wrapper(EditLine *el, unsigned char c)
   1826  1.35  christos {
   1827  1.35  christos 	if (map[c] == NULL)
   1828  1.35  christos 	    return CC_ERROR;
   1829  1.35  christos 	(*map[c])(NULL, c);
   1830  1.35  christos 	return CC_NORM;
   1831  1.35  christos }
   1832  1.35  christos 
   1833  1.35  christos int
   1834  1.35  christos rl_add_defun(const char *name, Function *fun, int c)
   1835  1.35  christos {
   1836  1.35  christos 	char dest[8];
   1837  1.35  christos 	if (c >= sizeof(map) / sizeof(map[0]) || c < 0)
   1838  1.35  christos 		return -1;
   1839  1.35  christos 	map[(unsigned char)c] = fun;
   1840  1.35  christos 	el_set(e, EL_ADDFN, name, name, rl_bind_wrapper);
   1841  1.35  christos 	el_set(e, EL_BIND, vis(dest, c, VIS_WHITE, 0), name);
   1842  1.35  christos 	return 0;
   1843  1.35  christos }
   1844  1.35  christos 
   1845  1.35  christos void
   1846  1.35  christos rl_callback_read_char()
   1847  1.35  christos {
   1848  1.36  christos 	int count = 0, done = 0;
   1849  1.35  christos 	const char *buf = el_gets(e, &count);
   1850  1.35  christos 	char *wbuf;
   1851  1.35  christos 
   1852  1.35  christos 	if (buf == NULL || count-- <= 0)
   1853  1.35  christos 		return;
   1854  1.36  christos 	if (count == 0 && buf[0] == CTRL('d'))
   1855  1.36  christos 		done = 1;
   1856  1.36  christos 	if (buf[count] == '\n' || buf[count] == '\r')
   1857  1.36  christos 		done = 2;
   1858  1.36  christos 
   1859  1.36  christos 	if (done && rl_linefunc != NULL) {
   1860  1.35  christos 		el_set(e, EL_UNBUFFERED, 0);
   1861  1.36  christos 		if (done == 2) {
   1862  1.36  christos 		    if ((wbuf = strdup(buf)) != NULL)
   1863  1.36  christos 			wbuf[count] = '\0';
   1864  1.36  christos 		} else
   1865  1.36  christos 			wbuf = NULL;
   1866  1.35  christos 		(*(void (*)(const char *))rl_linefunc)(wbuf);
   1867  1.35  christos 	}
   1868  1.35  christos }
   1869  1.35  christos 
   1870  1.35  christos void
   1871  1.35  christos rl_callback_handler_install (const char *prompt, VFunction *linefunc)
   1872  1.35  christos {
   1873  1.35  christos 	if (e == NULL) {
   1874  1.35  christos 		rl_initialize();
   1875  1.35  christos 	}
   1876  1.35  christos 	if (rl_prompt)
   1877  1.35  christos 		free(rl_prompt);
   1878  1.35  christos 	rl_prompt = prompt ? strdup(strchr(prompt, *prompt)) : NULL;
   1879  1.35  christos 	rl_linefunc = linefunc;
   1880  1.35  christos 	el_set(e, EL_UNBUFFERED, 1);
   1881  1.35  christos }
   1882  1.35  christos 
   1883  1.35  christos void
   1884  1.35  christos rl_callback_handler_remove(void)
   1885  1.35  christos {
   1886  1.35  christos 	el_set(e, EL_UNBUFFERED, 0);
   1887  1.35  christos }
   1888  1.35  christos 
   1889  1.35  christos void
   1890  1.35  christos rl_redisplay(void)
   1891  1.35  christos {
   1892  1.35  christos 	char a[2];
   1893  1.35  christos 	a[0] = CTRL('r');
   1894  1.35  christos 	a[1] = '\0';
   1895  1.35  christos 	el_push(e, a);
   1896  1.35  christos }
   1897  1.35  christos 
   1898  1.35  christos int
   1899  1.35  christos rl_get_previous_history(int count, int key)
   1900  1.35  christos {
   1901  1.35  christos 	char a[2];
   1902  1.35  christos 	a[0] = key;
   1903  1.35  christos 	a[1] = '\0';
   1904  1.35  christos 	while (count--)
   1905  1.35  christos 		el_push(e, a);
   1906  1.35  christos 	return 0;
   1907   1.1  christos }
   1908