Home | History | Annotate | Line # | Download | only in mail
complete.c revision 1.8
      1  1.8  christos /*	$NetBSD: complete.c,v 1.8 2006/10/02 16:43:31 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1997-2000,2005 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 Luke Mewburn.
      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.1  christos /* Most of this is derived or copied from src/usr.bin/ftp/complete.c (1.41). */
     40  1.1  christos 
     41  1.1  christos 
     42  1.1  christos #ifdef USE_READLINE
     43  1.1  christos 
     44  1.1  christos #include <sys/cdefs.h>
     45  1.1  christos #ifndef lint
     46  1.8  christos __RCSID("$NetBSD: complete.c,v 1.8 2006/10/02 16:43:31 christos Exp $");
     47  1.1  christos #endif /* not lint */
     48  1.1  christos 
     49  1.1  christos /*
     50  1.1  christos  * FTP user program - command and file completion routines
     51  1.1  christos  */
     52  1.1  christos 
     53  1.1  christos #include <sys/stat.h>
     54  1.1  christos 
     55  1.1  christos #include <ctype.h>
     56  1.1  christos #include <err.h>
     57  1.1  christos #include <dirent.h>
     58  1.1  christos #include <glob.h>
     59  1.1  christos #include <stdio.h>
     60  1.1  christos #include <stdlib.h>
     61  1.1  christos #include <string.h>
     62  1.1  christos #include <histedit.h>
     63  1.1  christos #include <sys/param.h>
     64  1.1  christos #include <stringlist.h>
     65  1.1  christos 
     66  1.1  christos #include "rcv.h"			/* includes "glob.h" */
     67  1.1  christos #include "extern.h"
     68  1.1  christos #include "complete.h"
     69  1.1  christos 
     70  1.1  christos /*
     71  1.1  christos  * Global variables
     72  1.1  christos  */
     73  1.1  christos static int doglob = 1;			/* glob local file names */
     74  1.1  christos 
     75  1.1  christos #define ttyout stdout
     76  1.1  christos #define ttywidth screenwidth		/* in "glob.h" */
     77  1.1  christos #define ttyheight screenheight		/* in "glob.h" */
     78  1.1  christos 
     79  1.1  christos 
     80  1.1  christos /* This should find the first command that matches the name given or
     81  1.1  christos  * NULL if none.  Use the routine from mail in lex.c.
     82  1.1  christos  */
     83  1.1  christos #define getcmd(w) lex(w)
     84  1.1  christos 
     85  1.1  christos 
     86  1.1  christos /************************************************************************/
     87  1.1  christos /* from src/usr.bin/ftp/utils.h (1.135) */
     88  1.1  christos 
     89  1.1  christos /*
     90  1.1  christos  * List words in stringlist, vertically arranged
     91  1.1  christos  */
     92  1.1  christos static void
     93  1.1  christos list_vertical(StringList *sl)
     94  1.1  christos {
     95  1.1  christos 	int i, j;
     96  1.1  christos 	int columns, lines;
     97  1.1  christos 	char *p;
     98  1.1  christos 	size_t w, width;
     99  1.1  christos 
    100  1.1  christos 	width = 0;
    101  1.1  christos 
    102  1.1  christos 	for (i = 0 ; i < sl->sl_cur ; i++) {
    103  1.1  christos 		w = strlen(sl->sl_str[i]);
    104  1.1  christos 		if (w > width)
    105  1.1  christos 			width = w;
    106  1.1  christos 	}
    107  1.1  christos 	width = (width + 8) &~ 7;
    108  1.1  christos 
    109  1.1  christos 	columns = ttywidth / width;
    110  1.1  christos 	if (columns == 0)
    111  1.1  christos 		columns = 1;
    112  1.1  christos 	lines = (sl->sl_cur + columns - 1) / columns;
    113  1.1  christos 	for (i = 0; i < lines; i++) {
    114  1.1  christos 		for (j = 0; j < columns; j++) {
    115  1.1  christos 			p = sl->sl_str[j * lines + i];
    116  1.1  christos 			if (p)
    117  1.1  christos 				fputs(p, ttyout);
    118  1.1  christos 			if (j * lines + i + lines >= sl->sl_cur) {
    119  1.1  christos 				putc('\n', ttyout);
    120  1.1  christos 				break;
    121  1.1  christos 			}
    122  1.1  christos 			if (p) {
    123  1.1  christos 				w = strlen(p);
    124  1.1  christos 				while (w < width) {
    125  1.1  christos 					w = (w + 8) &~ 7;
    126  1.1  christos 					(void)putc('\t', ttyout);
    127  1.1  christos 				}
    128  1.1  christos 			}
    129  1.1  christos 		}
    130  1.1  christos 	}
    131  1.1  christos }
    132  1.1  christos 
    133  1.1  christos /*
    134  1.1  christos  * Copy characters from src into dst, \ quoting characters that require it
    135  1.1  christos  */
    136  1.1  christos static void
    137  1.1  christos ftpvis(char *dst, size_t dstlen, const char *src, size_t srclen)
    138  1.1  christos {
    139  1.1  christos 	int	di, si;
    140  1.1  christos 
    141  1.1  christos 	for (di = si = 0;
    142  1.1  christos 	    src[si] != '\0' && di < dstlen && si < srclen;
    143  1.1  christos 	    di++, si++) {
    144  1.1  christos 		switch (src[si]) {
    145  1.1  christos 		case '\\':
    146  1.1  christos 		case ' ':
    147  1.1  christos 		case '\t':
    148  1.1  christos 		case '\r':
    149  1.1  christos 		case '\n':
    150  1.1  christos 		case '"':
    151  1.1  christos 			dst[di++] = '\\';
    152  1.1  christos 			if (di >= dstlen)
    153  1.1  christos 				break;
    154  1.1  christos 			/* FALLTHROUGH */
    155  1.1  christos 		default:
    156  1.1  christos 			dst[di] = src[si];
    157  1.1  christos 		}
    158  1.1  christos 	}
    159  1.1  christos 	dst[di] = '\0';
    160  1.1  christos }
    161  1.1  christos 
    162  1.1  christos /*
    163  1.1  christos  * sl_init() with inbuilt error checking
    164  1.1  christos  */
    165  1.1  christos static StringList *
    166  1.1  christos ftp_sl_init(void)
    167  1.1  christos {
    168  1.1  christos 	StringList *p;
    169  1.1  christos 
    170  1.1  christos 	p = sl_init();
    171  1.1  christos 	if (p == NULL)
    172  1.1  christos 		err(1, "Unable to allocate memory for stringlist");
    173  1.1  christos 	return (p);
    174  1.1  christos }
    175  1.1  christos 
    176  1.1  christos 
    177  1.1  christos /*
    178  1.1  christos  * sl_add() with inbuilt error checking
    179  1.1  christos  */
    180  1.1  christos static void
    181  1.1  christos ftp_sl_add(StringList *sl, char *i)
    182  1.1  christos {
    183  1.1  christos 
    184  1.1  christos 	if (sl_add(sl, i) == -1)
    185  1.1  christos 		err(1, "Unable to add `%s' to stringlist", i);
    186  1.1  christos }
    187  1.1  christos 
    188  1.1  christos /*
    189  1.1  christos  * strdup() with inbuilt error checking
    190  1.1  christos  */
    191  1.1  christos static char *
    192  1.1  christos ftp_strdup(const char *str)
    193  1.1  christos {
    194  1.1  christos 	char *s;
    195  1.1  christos 
    196  1.1  christos 	if (str == NULL)
    197  1.1  christos 		errx(1, "ftp_strdup() called with NULL argument");
    198  1.1  christos 	s = strdup(str);
    199  1.1  christos 	if (s == NULL)
    200  1.1  christos 		err(1, "Unable to allocate memory for string copy");
    201  1.1  christos 	return (s);
    202  1.1  christos }
    203  1.1  christos 
    204  1.1  christos /*
    205  1.1  christos  * Glob a local file name specification with the expectation of a single
    206  1.1  christos  * return value. Can't control multiple values being expanded from the
    207  1.1  christos  * expression, we return only the first.
    208  1.1  christos  * Returns NULL on error, or a pointer to a buffer containing the filename
    209  1.1  christos  * that's the caller's responsiblity to free(3) when finished with.
    210  1.1  christos  */
    211  1.1  christos static char *
    212  1.1  christos globulize(const char *pattern)
    213  1.1  christos {
    214  1.1  christos 	glob_t gl;
    215  1.1  christos 	int flags;
    216  1.1  christos 	char *p;
    217  1.1  christos 
    218  1.1  christos 	if (!doglob)
    219  1.1  christos 		return (ftp_strdup(pattern));
    220  1.1  christos 
    221  1.1  christos 	flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
    222  1.1  christos 	memset(&gl, 0, sizeof(gl));
    223  1.1  christos 	if (glob(pattern, flags, NULL, &gl) || gl.gl_pathc == 0) {
    224  1.1  christos 		warnx("%s: not found", pattern);
    225  1.1  christos 		globfree(&gl);
    226  1.1  christos 		return (NULL);
    227  1.1  christos 	}
    228  1.1  christos 	p = ftp_strdup(gl.gl_pathv[0]);
    229  1.1  christos 	globfree(&gl);
    230  1.1  christos 	return (p);
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos /* from src/usr.bin/ftp/utils.h (1.135) */
    234  1.1  christos /************************************************************************/
    235  1.1  christos 
    236  1.1  christos static int
    237  1.1  christos comparstr(const void *a, const void *b)
    238  1.1  christos {
    239  1.1  christos 	return (strcmp(*(const char * const *)a, *(const char * const *)b));
    240  1.1  christos }
    241  1.1  christos 
    242  1.1  christos /*
    243  1.1  christos  * Determine if complete is ambiguous. If unique, insert.
    244  1.1  christos  * If no choices, error. If unambiguous prefix, insert that.
    245  1.1  christos  * Otherwise, list choices. words is assumed to be filtered
    246  1.1  christos  * to only contain possible choices.
    247  1.1  christos  * Args:
    248  1.1  christos  *	word	word which started the match
    249  1.1  christos  *	list	list by default
    250  1.1  christos  *	words	stringlist containing possible matches
    251  1.1  christos  * Returns a result as per el_set(EL_ADDFN, ...)
    252  1.1  christos  */
    253  1.1  christos static unsigned char
    254  1.1  christos complete_ambiguous(EditLine *el, char *word, int list, StringList *words)
    255  1.1  christos {
    256  1.1  christos 	char insertstr[MAXPATHLEN];
    257  1.1  christos 	char *lastmatch, *p;
    258  1.1  christos 	int i, j;
    259  1.1  christos 	size_t matchlen, wordlen;
    260  1.1  christos 
    261  1.1  christos 	wordlen = strlen(word);
    262  1.1  christos 	if (words->sl_cur == 0)
    263  1.1  christos 		return (CC_ERROR);	/* no choices available */
    264  1.1  christos 
    265  1.1  christos 	if (words->sl_cur == 1) {	/* only once choice available */
    266  1.1  christos 		p = words->sl_str[0] + wordlen;
    267  1.1  christos 		if (*p == '\0')		/* at end of word? */
    268  1.1  christos 			return (CC_REFRESH);
    269  1.1  christos 		ftpvis(insertstr, sizeof(insertstr), p, strlen(p));
    270  1.1  christos 		if (el_insertstr(el, insertstr) == -1)
    271  1.1  christos 			return (CC_ERROR);
    272  1.1  christos 		else
    273  1.1  christos 			return (CC_REFRESH);
    274  1.1  christos 	}
    275  1.1  christos 
    276  1.1  christos 	if (!list) {
    277  1.1  christos 		matchlen = 0;
    278  1.1  christos 		lastmatch = words->sl_str[0];
    279  1.1  christos 		matchlen = strlen(lastmatch);
    280  1.1  christos 		for (i = 1 ; i < words->sl_cur ; i++) {
    281  1.1  christos 			for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
    282  1.1  christos 				if (lastmatch[j] != words->sl_str[i][j])
    283  1.1  christos 					break;
    284  1.1  christos 			if (j < matchlen)
    285  1.1  christos 				matchlen = j;
    286  1.1  christos 		}
    287  1.1  christos 		if (matchlen > wordlen) {
    288  1.1  christos 			ftpvis(insertstr, sizeof(insertstr),
    289  1.1  christos 			    lastmatch + wordlen, matchlen - wordlen);
    290  1.1  christos 			if (el_insertstr(el, insertstr) == -1)
    291  1.1  christos 				return (CC_ERROR);
    292  1.1  christos 			else
    293  1.1  christos 				return (CC_REFRESH_BEEP);
    294  1.1  christos 		}
    295  1.1  christos 	}
    296  1.1  christos 
    297  1.1  christos 	putc('\n', ttyout);
    298  1.1  christos 	qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
    299  1.1  christos 	list_vertical(words);
    300  1.1  christos 	return (CC_REDISPLAY);
    301  1.1  christos }
    302  1.1  christos 
    303  1.1  christos /*
    304  1.1  christos  * Complete a mail command.
    305  1.1  christos  */
    306  1.1  christos static unsigned char
    307  1.1  christos complete_command(EditLine *el, char *word, int list)
    308  1.1  christos {
    309  1.1  christos 	const struct cmd *c;
    310  1.1  christos 	StringList *words;
    311  1.1  christos 	size_t wordlen;
    312  1.1  christos 	unsigned char rv;
    313  1.1  christos 
    314  1.1  christos 	words = ftp_sl_init();
    315  1.1  christos 	wordlen = strlen(word);
    316  1.1  christos 
    317  1.1  christos 	for (c = cmdtab; c->c_name != NULL; c++) {
    318  1.1  christos 		if (wordlen > strlen(c->c_name))
    319  1.1  christos 			continue;
    320  1.1  christos 		if (strncmp(word, c->c_name, wordlen) == 0)
    321  1.1  christos 			ftp_sl_add(words, __UNCONST(c->c_name));
    322  1.1  christos 	}
    323  1.1  christos 
    324  1.1  christos 	rv = complete_ambiguous(el, word, list, words);
    325  1.1  christos 	if (rv == CC_REFRESH) {
    326  1.1  christos 		if (el_insertstr(el, " ") == -1)
    327  1.1  christos 			rv = CC_ERROR;
    328  1.1  christos 	}
    329  1.1  christos 	sl_free(words, 0);
    330  1.1  christos 	return (rv);
    331  1.1  christos }
    332  1.1  christos 
    333  1.1  christos /*
    334  1.1  christos  * Complete a local filename.
    335  1.1  christos  */
    336  1.1  christos static unsigned char
    337  1.1  christos complete_filename(EditLine *el, char *word, int list)
    338  1.1  christos {
    339  1.1  christos 	StringList *words;
    340  1.1  christos 	char dir[MAXPATHLEN];
    341  1.1  christos 	char *fname;
    342  1.1  christos 	DIR *dd;
    343  1.1  christos 	struct dirent *dp;
    344  1.1  christos 	unsigned char rv;
    345  1.1  christos 	size_t len;
    346  1.1  christos 
    347  1.1  christos 	if ((fname = strrchr(word, '/')) == NULL) {
    348  1.1  christos 		dir[0] = '.';
    349  1.1  christos 		dir[1] = '\0';
    350  1.1  christos 		fname = word;
    351  1.1  christos 	} else {
    352  1.1  christos 		if (fname == word) {
    353  1.1  christos 			dir[0] = '/';
    354  1.1  christos 			dir[1] = '\0';
    355  1.1  christos 		} else
    356  1.1  christos 			(void)strlcpy(dir, word, fname - word + 1);
    357  1.1  christos 		fname++;
    358  1.1  christos 	}
    359  1.1  christos 	if (dir[0] == '~') {
    360  1.1  christos 		char *p;
    361  1.1  christos 
    362  1.1  christos 		if ((p = globulize(dir)) == NULL)
    363  1.1  christos 			return (CC_ERROR);
    364  1.1  christos 		(void)strlcpy(dir, p, sizeof(dir));
    365  1.1  christos 		free(p);
    366  1.1  christos 	}
    367  1.1  christos 
    368  1.1  christos 	if ((dd = opendir(dir)) == NULL)
    369  1.1  christos 		return (CC_ERROR);
    370  1.1  christos 
    371  1.1  christos 	words = ftp_sl_init();
    372  1.1  christos 	len = strlen(fname);
    373  1.1  christos 
    374  1.1  christos 	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
    375  1.1  christos 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    376  1.1  christos 			continue;
    377  1.1  christos 
    378  1.1  christos #if defined(DIRENT_MISSING_D_NAMLEN)
    379  1.1  christos 		if (len > strlen(dp->d_name))
    380  1.1  christos 			continue;
    381  1.1  christos #else
    382  1.1  christos 		if (len > dp->d_namlen)
    383  1.1  christos 			continue;
    384  1.1  christos #endif
    385  1.1  christos 		if (strncmp(fname, dp->d_name, len) == 0) {
    386  1.1  christos 			char *tcp;
    387  1.1  christos 
    388  1.1  christos 			tcp = ftp_strdup(dp->d_name);
    389  1.1  christos 			ftp_sl_add(words, tcp);
    390  1.1  christos 		}
    391  1.1  christos 	}
    392  1.1  christos 	closedir(dd);
    393  1.1  christos 
    394  1.1  christos 	rv = complete_ambiguous(el, fname, list, words);
    395  1.1  christos 	if (rv == CC_REFRESH) {
    396  1.1  christos 		struct stat sb;
    397  1.1  christos 		char path[MAXPATHLEN];
    398  1.1  christos 
    399  1.1  christos 		(void)strlcpy(path, dir,		sizeof(path));
    400  1.1  christos 		(void)strlcat(path, "/",		sizeof(path));
    401  1.1  christos 		(void)strlcat(path, words->sl_str[0],	sizeof(path));
    402  1.1  christos 
    403  1.1  christos 		if (stat(path, &sb) >= 0) {
    404  1.1  christos 			char suffix[2] = " ";
    405  1.1  christos 
    406  1.1  christos 			if (S_ISDIR(sb.st_mode))
    407  1.1  christos 				suffix[0] = '/';
    408  1.1  christos 			if (el_insertstr(el, suffix) == -1)
    409  1.1  christos 				rv = CC_ERROR;
    410  1.1  christos 		}
    411  1.1  christos 	}
    412  1.1  christos 	sl_free(words, 1);
    413  1.1  christos 	return (rv);
    414  1.1  christos }
    415  1.1  christos 
    416  1.1  christos static int
    417  1.1  christos find_execs(char *word, char *path, StringList *list)
    418  1.1  christos {
    419  1.1  christos 	char *sep;
    420  1.1  christos 	char *dir=path;
    421  1.1  christos 	DIR *dd;
    422  1.1  christos 	struct dirent *dp;
    423  1.1  christos 	int len = strlen(word);
    424  1.1  christos 	uid_t uid = getuid();
    425  1.1  christos 	gid_t gid = getgid();
    426  1.1  christos 
    427  1.1  christos 	for (sep=dir ; sep ; dir=sep+1) {
    428  1.1  christos 		if ((sep=strchr(dir, ':')) != NULL) {
    429  1.1  christos 			*sep=0;
    430  1.1  christos 		}
    431  1.1  christos 
    432  1.1  christos 		if ((dd = opendir(dir)) == NULL) {
    433  1.1  christos 			perror("dir");
    434  1.1  christos 			return -1;
    435  1.1  christos 		}
    436  1.1  christos 
    437  1.1  christos 		for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
    438  1.1  christos 
    439  1.1  christos 			if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    440  1.1  christos 				continue;
    441  1.1  christos 
    442  1.1  christos #if defined(DIRENT_MISSING_D_NAMLEN)
    443  1.1  christos 			if (len > strlen(dp->d_name))
    444  1.1  christos 				continue;
    445  1.1  christos #else
    446  1.1  christos 			if (len > dp->d_namlen)
    447  1.1  christos 				continue;
    448  1.1  christos #endif
    449  1.1  christos 
    450  1.1  christos 			if (strncmp(word, dp->d_name, len) == 0) {
    451  1.1  christos 				struct stat sb;
    452  1.1  christos 				char pathname[ MAXPATHLEN ];
    453  1.1  christos 				unsigned mask;
    454  1.1  christos 
    455  1.1  christos 				snprintf(pathname, sizeof(pathname), "%s/%s", dir, dp->d_name);
    456  1.1  christos 				if (stat(pathname, &sb) != 0) {
    457  1.1  christos 					perror(pathname);
    458  1.1  christos 					continue;
    459  1.1  christos 				}
    460  1.1  christos 
    461  1.1  christos 				mask = 0001;
    462  1.1  christos 				if (sb.st_uid == uid)	mask |= 0100;
    463  1.1  christos 				if (sb.st_gid == gid)	mask |= 0010;
    464  1.1  christos 
    465  1.1  christos 				if ((sb.st_mode & mask) != 0) {
    466  1.1  christos 					char *tcp;
    467  1.1  christos 					tcp = strdup(dp->d_name);
    468  1.1  christos 					sl_add(list, tcp);
    469  1.1  christos 				}
    470  1.1  christos 			}
    471  1.1  christos 
    472  1.1  christos 		}
    473  1.1  christos 
    474  1.1  christos 		closedir(dd);
    475  1.1  christos 	}
    476  1.1  christos 
    477  1.1  christos 	return 0;
    478  1.1  christos }
    479  1.1  christos 
    480  1.1  christos 
    481  1.1  christos /*
    482  1.1  christos  * Complete a local executable
    483  1.1  christos  */
    484  1.1  christos static unsigned char
    485  1.1  christos complete_executable(EditLine *el, char *word, int list)
    486  1.1  christos {
    487  1.1  christos 	StringList *words;
    488  1.1  christos 	char dir[ MAXPATHLEN ];
    489  1.1  christos 	char *fname;
    490  1.1  christos 	unsigned char rv;
    491  1.1  christos 	int error;
    492  1.1  christos 
    493  1.1  christos 	if ((fname = strrchr(word, '/')) == NULL) {
    494  1.1  christos 		dir[0] = '\0';		/* walk the path */
    495  1.1  christos 		fname = word;
    496  1.1  christos 	} else {
    497  1.1  christos 		if (fname == word) {
    498  1.1  christos 			dir[0] = '/';
    499  1.1  christos 			dir[1] = '\0';
    500  1.1  christos 		} else {
    501  1.1  christos 			(void)strncpy(dir, word, fname - word);
    502  1.1  christos 			dir[fname - word] = '\0';
    503  1.1  christos 		}
    504  1.1  christos 		fname++;
    505  1.1  christos 	}
    506  1.1  christos 
    507  1.1  christos 	words = sl_init();
    508  1.1  christos 
    509  1.1  christos 	if (*dir == '\0') {		/* walk path */
    510  1.1  christos 		char *env;
    511  1.1  christos 		char *path;
    512  1.1  christos 		int len;
    513  1.1  christos 		env = getenv("PATH");
    514  1.1  christos 		len = strlen(env);
    515  1.1  christos 		path = salloc(len + 1);
    516  1.1  christos 		strcpy(path, env);
    517  1.1  christos 		error = find_execs(word, path, words);
    518  1.1  christos 	}
    519  1.1  christos 	else {		/* check specified dir only */
    520  1.1  christos 		error = find_execs(word, dir, words);
    521  1.1  christos 	}
    522  1.1  christos 	if (error != 0)
    523  1.1  christos 		return CC_ERROR;
    524  1.1  christos 
    525  1.1  christos 	rv = complete_ambiguous(el, fname, list, words);
    526  1.1  christos 
    527  1.1  christos 	if (rv == CC_REFRESH)
    528  1.1  christos 		if (el_insertstr(el, " ") == -1)
    529  1.1  christos 			rv = CC_ERROR;
    530  1.1  christos 
    531  1.1  christos 	sl_free(words, 1);
    532  1.1  christos 
    533  1.1  christos 	return (rv);
    534  1.1  christos }
    535  1.1  christos 
    536  1.1  christos 
    537  1.1  christos static unsigned
    538  1.1  christos char complete_set(EditLine *el, char *word, int list)
    539  1.1  christos {
    540  1.1  christos 	struct var *vp;
    541  1.1  christos 	char **ap;
    542  1.1  christos 	char **p;
    543  1.1  christos 	int h;
    544  1.1  christos 	int s;
    545  1.1  christos 	int len = strlen(word);
    546  1.1  christos 	StringList *words;
    547  1.1  christos 	unsigned char rv;
    548  1.1  christos 
    549  1.1  christos 	words = sl_init();
    550  1.1  christos 
    551  1.1  christos 	/* allocate space for variables table */
    552  1.1  christos 	for (h = 0, s = 1; h < HSHSIZE; h++)
    553  1.1  christos 		for (vp = variables[h]; vp != NULL; vp = vp->v_link)
    554  1.1  christos 			s++;
    555  1.1  christos 	ap = (char **) salloc(s * sizeof *ap);
    556  1.1  christos 
    557  1.1  christos 	/* save pointers */
    558  1.1  christos 	for (h = 0, p = ap; h < HSHSIZE; h++)
    559  1.1  christos 		for (vp = variables[h]; vp != NULL; vp = vp->v_link)
    560  1.1  christos 			*p++ = vp->v_name;
    561  1.1  christos 	*p = NULL;
    562  1.1  christos 
    563  1.1  christos 	/* sort pointers */
    564  1.1  christos 	sort(ap);
    565  1.1  christos 
    566  1.1  christos 	/* complete on list */
    567  1.1  christos 	for (p = ap; *p != NULL; p++) {
    568  1.1  christos 		if (len == 0 || strncmp(*p, word, len) == 0) {
    569  1.1  christos 			char *tcp;
    570  1.1  christos 			tcp = strdup(*p);
    571  1.1  christos 			sl_add(words, tcp);
    572  1.1  christos 		}
    573  1.1  christos 	}
    574  1.1  christos 
    575  1.1  christos 	rv = complete_ambiguous(el, word, list, words);
    576  1.1  christos 
    577  1.1  christos 	sl_free(words, 1);
    578  1.1  christos 
    579  1.1  christos 	return(rv);
    580  1.1  christos }
    581  1.1  christos 
    582  1.1  christos 
    583  1.1  christos 
    584  1.1  christos 
    585  1.1  christos 
    586  1.1  christos static unsigned char
    587  1.1  christos complete_alias(EditLine *el, char *word, int list)
    588  1.1  christos {
    589  1.1  christos 	struct grouphead *gh;
    590  1.1  christos 	char **ap;
    591  1.1  christos 	char **p;
    592  1.1  christos 	int h;
    593  1.1  christos 	int s;
    594  1.1  christos 	int len = strlen(word);
    595  1.1  christos 	StringList *words;
    596  1.1  christos 	unsigned char rv;
    597  1.1  christos 
    598  1.1  christos 	words = sl_init();
    599  1.1  christos 
    600  1.1  christos 	/* allocate space for alias table */
    601  1.1  christos 	for (h = 0, s = 1; h < HSHSIZE; h++)
    602  1.1  christos 		for (gh = groups[h]; gh != NULL; gh = gh->g_link)
    603  1.1  christos 			s++;
    604  1.1  christos 	ap = (char **) salloc(s * sizeof *ap);
    605  1.1  christos 
    606  1.1  christos 	/* save pointers */
    607  1.1  christos 	for (h = 0, p = ap; h < HSHSIZE; h++)
    608  1.1  christos 		for (gh = groups[h]; gh != NULL; gh = gh->g_link)
    609  1.1  christos 			*p++ = gh->g_name;
    610  1.1  christos 	*p = NULL;
    611  1.1  christos 
    612  1.1  christos 	/* sort pointers */
    613  1.1  christos 	sort(ap);
    614  1.1  christos 
    615  1.1  christos 	/* complete on list */
    616  1.1  christos 	for (p = ap; *p != NULL; p++) {
    617  1.1  christos 		if (len == 0 || strncmp(*p, word, len) == 0) {
    618  1.1  christos 			char *tcp;
    619  1.1  christos 			tcp = strdup(*p);
    620  1.1  christos 			sl_add(words, tcp);
    621  1.1  christos 		}
    622  1.1  christos 	}
    623  1.1  christos 
    624  1.1  christos 	rv = complete_ambiguous(el, word, list, words);
    625  1.1  christos 	if (rv == CC_REFRESH)
    626  1.1  christos 		if (el_insertstr(el, " ") == -1)
    627  1.1  christos 			rv = CC_ERROR;
    628  1.1  christos 
    629  1.1  christos 	sl_free(words, 1);
    630  1.1  christos 
    631  1.1  christos 	return(rv);
    632  1.1  christos }
    633  1.1  christos 
    634  1.1  christos 
    635  1.1  christos 
    636  1.1  christos static char	*stringbase;	/* current scan point in line buffer */
    637  1.1  christos static char	*argbase;	/* current storage point in arg buffer */
    638  1.1  christos static StringList *marg_sl;	/* stringlist containing margv */
    639  1.1  christos static int	margc;		/* count of arguments on input line */
    640  1.1  christos #define margv (marg_sl->sl_str)	/* args parsed from input line */
    641  1.1  christos 
    642  1.1  christos static char *cursor_pos;
    643  1.1  christos static int   cursor_argc;
    644  1.1  christos static int   cursor_argo;
    645  1.1  christos 
    646  1.1  christos static void
    647  1.1  christos init_complete(void)
    648  1.1  christos {
    649  1.1  christos 	marg_sl = sl_init();
    650  1.1  christos 	return;
    651  1.1  christos }
    652  1.1  christos 
    653  1.1  christos 
    654  1.1  christos 
    655  1.1  christos /************************************************************************/
    656  1.1  christos /* from /usr/src/usr.bin/ftp/main.c(1.101) */
    657  1.1  christos 
    658  1.1  christos static int   slrflag;
    659  1.1  christos static char *altarg;		/* argv[1] with no shell-like preprocessing  */
    660  1.1  christos 
    661  1.1  christos #ifdef NO_EDITCOMPLETE
    662  1.1  christos #define	INC_CHKCURSOR(x)	(x)++
    663  1.1  christos #else  /* !NO_EDITCOMPLETE */
    664  1.1  christos #define	INC_CHKCURSOR(x)				\
    665  1.1  christos 	do {						\
    666  1.1  christos 		(x)++ ;					\
    667  1.1  christos 		if (x == cursor_pos) {			\
    668  1.1  christos 			cursor_argc = margc;		\
    669  1.1  christos 			cursor_argo = ap - argbase;	\
    670  1.1  christos 			cursor_pos  = NULL;		\
    671  1.1  christos 		}					\
    672  1.1  christos 	} while(0)
    673  1.1  christos #endif /* !NO_EDITCOMPLETE */
    674  1.1  christos 
    675  1.1  christos /*
    676  1.1  christos  * Parse string into argbuf;
    677  1.1  christos  * implemented with FSM to
    678  1.1  christos  * handle quoting and strings
    679  1.1  christos  */
    680  1.1  christos static char *
    681  1.1  christos slurpstring(void)
    682  1.1  christos {
    683  1.1  christos 	int got_one = 0;
    684  1.1  christos 	char *sb = stringbase;
    685  1.1  christos 	char *ap = argbase;
    686  1.1  christos 	char *tmp = argbase;		/* will return this if token found */
    687  1.1  christos 
    688  1.1  christos 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
    689  1.1  christos 		switch (slrflag) {	/* and $ as token for macro invoke */
    690  1.1  christos 			case 0:
    691  1.1  christos 				slrflag++;
    692  1.1  christos 				INC_CHKCURSOR(stringbase);
    693  1.1  christos 				return __UNCONST((*sb == '!') ? "!" : "$");
    694  1.1  christos 				/* NOTREACHED */
    695  1.1  christos 			case 1:
    696  1.1  christos 				slrflag++;
    697  1.1  christos 				altarg = stringbase;
    698  1.1  christos 				break;
    699  1.1  christos 			default:
    700  1.1  christos 				break;
    701  1.1  christos 		}
    702  1.1  christos 	}
    703  1.1  christos 
    704  1.1  christos S0:
    705  1.1  christos 	switch (*sb) {
    706  1.1  christos 
    707  1.1  christos 	case '\0':
    708  1.1  christos 		goto OUT;
    709  1.1  christos 
    710  1.1  christos 	case ' ':
    711  1.1  christos 	case '\t':
    712  1.1  christos 		INC_CHKCURSOR(sb);
    713  1.1  christos 		goto S0;
    714  1.1  christos 
    715  1.1  christos 	default:
    716  1.1  christos 		switch (slrflag) {
    717  1.1  christos 			case 0:
    718  1.1  christos 				slrflag++;
    719  1.1  christos 				break;
    720  1.1  christos 			case 1:
    721  1.1  christos 				slrflag++;
    722  1.1  christos 				altarg = sb;
    723  1.1  christos 				break;
    724  1.1  christos 			default:
    725  1.1  christos 				break;
    726  1.1  christos 		}
    727  1.1  christos 		goto S1;
    728  1.1  christos 	}
    729  1.1  christos 
    730  1.1  christos S1:
    731  1.1  christos 	switch (*sb) {
    732  1.1  christos 
    733  1.1  christos 	case ' ':
    734  1.1  christos 	case '\t':
    735  1.1  christos 	case '\0':
    736  1.1  christos 		goto OUT;	/* end of token */
    737  1.1  christos 
    738  1.1  christos 	case '\\':
    739  1.1  christos 		INC_CHKCURSOR(sb);
    740  1.1  christos 		goto S2;	/* slurp next character */
    741  1.1  christos 
    742  1.1  christos 	case '"':
    743  1.1  christos 		INC_CHKCURSOR(sb);
    744  1.1  christos 		goto S3;	/* slurp quoted string */
    745  1.1  christos 
    746  1.1  christos 	default:
    747  1.1  christos 		/* the first arg (command) is special - see execute() in lex.c */
    748  1.8  christos 		if (margc == 0 && got_one &&
    749  1.8  christos 		    strchr(" \t0123456789$^.:/-+*'\"", *sb))
    750  1.1  christos 			goto OUT;
    751  1.1  christos 
    752  1.1  christos 		*ap = *sb;	/* add character to token */
    753  1.1  christos 		ap++;
    754  1.1  christos 		INC_CHKCURSOR(sb);
    755  1.1  christos 		got_one = 1;
    756  1.1  christos 		goto S1;
    757  1.1  christos 	}
    758  1.1  christos 
    759  1.1  christos S2:
    760  1.1  christos 	switch (*sb) {
    761  1.1  christos 
    762  1.1  christos 	case '\0':
    763  1.1  christos 		goto OUT;
    764  1.1  christos 
    765  1.1  christos 	default:
    766  1.1  christos 		*ap = *sb;
    767  1.1  christos 		ap++;
    768  1.1  christos 		INC_CHKCURSOR(sb);
    769  1.1  christos 		got_one = 1;
    770  1.1  christos 		goto S1;
    771  1.1  christos 	}
    772  1.1  christos 
    773  1.1  christos S3:
    774  1.1  christos 	switch (*sb) {
    775  1.1  christos 
    776  1.1  christos 	case '\0':
    777  1.1  christos 		goto OUT;
    778  1.1  christos 
    779  1.1  christos 	case '"':
    780  1.1  christos 		INC_CHKCURSOR(sb);
    781  1.1  christos 		goto S1;
    782  1.1  christos 
    783  1.1  christos 	default:
    784  1.1  christos 		*ap = *sb;
    785  1.1  christos 		ap++;
    786  1.1  christos 		INC_CHKCURSOR(sb);
    787  1.1  christos 		got_one = 1;
    788  1.1  christos 		goto S3;
    789  1.1  christos 	}
    790  1.1  christos 
    791  1.1  christos OUT:
    792  1.1  christos 	if (got_one)
    793  1.1  christos 		*ap++ = '\0';
    794  1.1  christos 	argbase = ap;			/* update storage pointer */
    795  1.1  christos 	stringbase = sb;		/* update scan pointer */
    796  1.1  christos 	if (got_one) {
    797  1.1  christos 		return (tmp);
    798  1.1  christos 	}
    799  1.1  christos 	switch (slrflag) {
    800  1.1  christos 		case 0:
    801  1.1  christos 			slrflag++;
    802  1.1  christos 			break;
    803  1.1  christos 		case 1:
    804  1.1  christos 			slrflag++;
    805  1.1  christos 			altarg = NULL;
    806  1.1  christos 			break;
    807  1.1  christos 		default:
    808  1.1  christos 			break;
    809  1.1  christos 	}
    810  1.1  christos 	return (NULL);
    811  1.1  christos }
    812  1.1  christos 
    813  1.1  christos 
    814  1.1  christos /*
    815  1.1  christos  * Slice a string up into argc/argv.
    816  1.1  christos  */
    817  1.1  christos static void
    818  1.1  christos makeargv(char *line)
    819  1.1  christos {
    820  1.1  christos 	static char argbuf[ LINESIZE ];	/* argument storage buffer */
    821  1.1  christos 	char *argp;
    822  1.1  christos 
    823  1.1  christos 	stringbase = line;		/* scan from first of buffer */
    824  1.1  christos 	argbase = argbuf;		/* store from first of buffer */
    825  1.1  christos 	slrflag = 0;
    826  1.1  christos 	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
    827  1.1  christos 	for (margc = 0; ; margc++) {
    828  1.1  christos 		argp = slurpstring();
    829  1.1  christos 		ftp_sl_add(marg_sl, argp);
    830  1.1  christos 		if (argp == NULL)
    831  1.1  christos 			break;
    832  1.1  christos 	}
    833  1.1  christos #ifndef NO_EDITCOMPLETE
    834  1.1  christos 	if (cursor_pos == line) {
    835  1.1  christos 		cursor_argc = 0;
    836  1.1  christos 		cursor_argo = 0;
    837  1.1  christos 	} else if (cursor_pos != NULL) {
    838  1.1  christos 		cursor_argc = margc;
    839  1.1  christos 		cursor_argo = strlen(margv[margc-1]);
    840  1.1  christos 	}
    841  1.1  christos #endif /* !NO_EDITCOMPLETE */
    842  1.1  christos }
    843  1.1  christos 
    844  1.1  christos /* from /usr/src/usr.bin/ftp/main.c(1.101) */
    845  1.1  christos /************************************************************************/
    846  1.1  christos 
    847  1.1  christos 
    848  1.1  christos /*
    849  1.1  christos  * Generic complete routine
    850  1.1  christos  */
    851  1.1  christos static unsigned char
    852  1.1  christos complete(EditLine *el, int ch)
    853  1.1  christos {
    854  1.1  christos 	static char line[LINESIZE];	/* input line buffer */
    855  1.1  christos 	static char word[LINESIZE];
    856  1.1  christos 	static int lastc_argc, lastc_argo;
    857  1.1  christos 
    858  1.1  christos 	const struct cmd *c;
    859  1.1  christos 	const LineInfo *lf;
    860  1.1  christos 	int celems, dolist, cmpltype;
    861  1.1  christos 	size_t len;
    862  1.1  christos 
    863  1.1  christos 	lf = el_line(el);
    864  1.1  christos 	len = lf->lastchar - lf->buffer;
    865  1.1  christos #if 1
    866  1.1  christos 	if (ch == 04) {	/* CTRL-D is special */
    867  1.1  christos 		if (len == 0)
    868  1.1  christos 			return (CC_EOF);
    869  1.1  christos 		if (lf->lastchar != lf->cursor) {
    870  1.1  christos 			el_push(el, __UNCONST("")); /* delete current char without using ^D */
    871  1.1  christos 			return (CC_NORM);
    872  1.1  christos 		}
    873  1.1  christos 	}
    874  1.1  christos #endif
    875  1.1  christos 	if (len >= sizeof(line))
    876  1.1  christos 		return (CC_ERROR);
    877  1.1  christos 	(void)strlcpy(line, lf->buffer, len + 1);
    878  1.1  christos 	cursor_pos = line + (lf->cursor - lf->buffer);
    879  1.1  christos 	lastc_argc = cursor_argc;	/* remember last cursor pos */
    880  1.1  christos 	lastc_argo = cursor_argo;
    881  1.1  christos 	makeargv(line);			/* build argc/argv of current line */
    882  1.1  christos 
    883  1.1  christos 	if (cursor_argo >= sizeof(word))
    884  1.1  christos 		return (CC_ERROR);
    885  1.1  christos 
    886  1.1  christos 	dolist = 0;
    887  1.1  christos 	/* if cursor and word are the same, list alternatives */
    888  1.1  christos 	if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
    889  1.1  christos 	    && strncmp(word, margv[cursor_argc] ? margv[cursor_argc] : "",
    890  1.1  christos 		       cursor_argo) == 0)
    891  1.1  christos 		dolist = 1;
    892  1.1  christos 	else if (cursor_argc < margc)
    893  1.1  christos 		(void)strlcpy(word, margv[cursor_argc], cursor_argo + 1);
    894  1.1  christos 	word[cursor_argo] = '\0';
    895  1.1  christos 
    896  1.1  christos 	if (cursor_argc == 0)
    897  1.1  christos 		return (complete_command(el, word, dolist));
    898  1.1  christos 
    899  1.1  christos 	c = getcmd(margv[0]);
    900  1.1  christos 	if (c == NULL)
    901  1.1  christos 		return (CC_ERROR);
    902  1.1  christos 	celems = strlen(c->c_complete);
    903  1.1  christos 
    904  1.1  christos 		/* check for 'continuation' completes (which are uppercase) */
    905  1.1  christos 	if ((cursor_argc > celems) && (celems > 0)
    906  1.1  christos 	    && isupper((unsigned char) c->c_complete[celems-1]))
    907  1.1  christos 		cursor_argc = celems;
    908  1.1  christos 
    909  1.1  christos 	if (cursor_argc > celems)
    910  1.1  christos 		return (CC_ERROR);
    911  1.1  christos 
    912  1.1  christos 	cmpltype = c->c_complete[cursor_argc - 1];
    913  1.1  christos 	switch (cmpltype) {
    914  1.1  christos 		case 'a':			/* alias complete */
    915  1.1  christos 		case 'A':
    916  1.1  christos 			return (complete_alias(el, word, dolist));
    917  1.1  christos 
    918  1.1  christos 		case 'c':			/* command complete */
    919  1.1  christos 		case 'C':
    920  1.1  christos 			return (complete_command(el, word, dolist));
    921  1.1  christos 
    922  1.1  christos 		case 'f':			/* filename complete */
    923  1.1  christos 		case 'F':
    924  1.1  christos 			return (complete_filename(el, word, dolist));
    925  1.1  christos 
    926  1.1  christos 		case 'n':			/* no complete */
    927  1.1  christos 		case 'N':			/* no complete */
    928  1.1  christos 			return (CC_ERROR);
    929  1.1  christos 
    930  1.1  christos 		case 's':
    931  1.1  christos 		case 'S':
    932  1.1  christos 			return (complete_set(el, word, dolist));
    933  1.1  christos 
    934  1.1  christos 		case 'x':			/* executable complete */
    935  1.1  christos 		case 'X':
    936  1.1  christos 			return (complete_executable(el, word, dolist));
    937  1.1  christos 
    938  1.1  christos 		default:
    939  1.1  christos 			errx(1, "unknown complete type `%c'", cmpltype);
    940  1.1  christos 			return (CC_ERROR);
    941  1.1  christos 	}
    942  1.1  christos 	/* NOTREACHED */
    943  1.1  christos }
    944  1.1  christos 
    945  1.1  christos 
    946  1.1  christos /*************************************************************************/
    947  1.1  christos /* Most of this was originally taken directly from the readline manpage. */
    948  1.1  christos 
    949  1.1  christos static struct {
    950  1.3  christos 	EditLine *el;		/* editline(3) with completion and history */
    951  1.3  christos 	EditLine *elo;		/* editline(3) editline only, no completion */
    952  1.1  christos 	History  *hist;		/* editline(3) history structure */
    953  1.1  christos 	const char *prompt;	/* prompt */
    954  1.1  christos } rl_global = {
    955  1.1  christos 	.el = NULL,
    956  1.1  christos 	.hist = NULL,
    957  1.1  christos 	.prompt = NULL
    958  1.1  christos };
    959  1.1  christos 
    960  1.1  christos char *
    961  1.1  christos rl_gets(const char *prompt)
    962  1.1  christos {
    963  1.1  christos 	int cnt;
    964  1.1  christos 	const char *buf;
    965  1.1  christos 	HistEvent ev;
    966  1.3  christos 	static char line[LINE_MAX];
    967  1.1  christos 
    968  1.1  christos 	rl_global.prompt = prompt;
    969  1.1  christos 	buf = el_gets(rl_global.el, &cnt);
    970  1.1  christos 
    971  1.1  christos 	if (buf == NULL || cnt <= 0)
    972  1.1  christos 		return NULL;
    973  1.1  christos 
    974  1.1  christos 	/* enter the line into history */
    975  1.1  christos 	if (history(rl_global.hist, &ev, H_ENTER, buf) == 0)
    976  1.1  christos 		printf("Failed history entry: %s", buf);
    977  1.1  christos #ifdef DEBUG
    978  1.1  christos 	else
    979  1.1  christos 		printf("history entry: %s\n", ev.str);
    980  1.1  christos #endif
    981  1.1  christos 
    982  1.1  christos 	cnt--;	/* trash the trailing LF */
    983  1.5  christos 	cnt = MIN(sizeof(line) - 1, cnt);
    984  1.6  christos 	(void)memcpy(line, buf, cnt);
    985  1.1  christos 	line[cnt] = '\0';
    986  1.1  christos 
    987  1.1  christos 	return line;
    988  1.1  christos }
    989  1.1  christos 
    990  1.1  christos 
    991  1.3  christos /*
    992  1.3  christos  * Edit a line containing string, with no history or completion.
    993  1.3  christos  */
    994  1.1  christos char *
    995  1.1  christos rl_getline(const char *prompt, char *string)
    996  1.1  christos {
    997  1.3  christos 	static char line[LINE_MAX];
    998  1.1  christos 	const char *buf;
    999  1.1  christos 	int cnt;
   1000  1.1  christos 
   1001  1.1  christos 	rl_global.prompt = prompt;
   1002  1.1  christos 
   1003  1.1  christos 	if (string)
   1004  1.3  christos 		el_push(rl_global.elo, string);
   1005  1.1  christos 
   1006  1.3  christos 	buf = el_gets(rl_global.elo, &cnt);
   1007  1.3  christos 	if (buf == NULL || cnt <= 0) {
   1008  1.3  christos 		if (cnt == 0)
   1009  1.3  christos 			fputc('\n', stdout);
   1010  1.3  christos 	  	line[0] = '\0';
   1011  1.3  christos 		return line;
   1012  1.3  christos 	}
   1013  1.1  christos 
   1014  1.1  christos 	cnt--;
   1015  1.6  christos 	cnt = MIN(sizeof(line) - 1, cnt);
   1016  1.6  christos 	(void)memcpy(line, buf, cnt);
   1017  1.1  christos 	line[cnt] = '\0';
   1018  1.1  christos 
   1019  1.1  christos 	return line;
   1020  1.1  christos }
   1021  1.1  christos 
   1022  1.1  christos 
   1023  1.1  christos static const char *
   1024  1.1  christos show_prompt(EditLine *e __attribute__((unused)))
   1025  1.1  christos {
   1026  1.1  christos 	return rl_global.prompt;
   1027  1.1  christos }
   1028  1.1  christos 
   1029  1.1  christos void
   1030  1.1  christos init_readline(void)
   1031  1.1  christos {
   1032  1.1  christos 	HistEvent ev;
   1033  1.1  christos 	const char *el_editor;
   1034  1.2  christos 	const char *el_history_size;
   1035  1.2  christos 	char *el_completion_keys;
   1036  1.3  christos 
   1037  1.7  christos 	rl_global.hist = history_init();	/* init the builtin history */
   1038  1.7  christos 	el_history_size = value("el_history_size");
   1039  1.7  christos 	if (el_history_size == NULL)
   1040  1.7  christos 		el_history_size = "0";
   1041  1.2  christos 	if (history(rl_global.hist, &ev, H_SETSIZE, atoi(el_history_size)))
   1042  1.1  christos 		printf("history: %s\n", ev.str);
   1043  1.3  christos 
   1044  1.3  christos 	rl_global.el  = el_init(getprogname(), stdin, stdout, stderr);
   1045  1.3  christos 	rl_global.elo = el_init(getprogname(), stdin, stdout, stderr);
   1046  1.3  christos 
   1047  1.1  christos 	el_editor = value("el_editor");
   1048  1.3  christos 	if (el_editor) {
   1049  1.1  christos 		el_set(rl_global.el, EL_EDITOR, el_editor);
   1050  1.3  christos 		el_set(rl_global.elo, EL_EDITOR, el_editor);
   1051  1.3  christos 	}
   1052  1.1  christos 
   1053  1.1  christos 	el_set(rl_global.el, EL_PROMPT, show_prompt);
   1054  1.3  christos 	el_set(rl_global.elo, EL_PROMPT, show_prompt);
   1055  1.1  christos 	el_set(rl_global.el, EL_HIST, history, rl_global.hist);	/* use history */
   1056  1.1  christos 	el_source(rl_global.el, NULL);				/* read ~/.editrc */
   1057  1.1  christos 
   1058  1.1  christos 	/* add local file completion, bind to TAB */
   1059  1.1  christos 	el_set(rl_global.el, EL_ADDFN, "mail-complete",
   1060  1.3  christos 	    "Context sensitive argument completion",
   1061  1.3  christos 	    complete);
   1062  1.3  christos 
   1063  1.2  christos 	el_completion_keys = value("el_completion_keys");
   1064  1.2  christos 	if (el_completion_keys && *el_completion_keys) {
   1065  1.1  christos 		struct name *np, *nq;
   1066  1.2  christos 		np = lexpand(el_completion_keys, 0);
   1067  1.1  christos 		for (nq = np ; nq ; nq = nq->n_flink)
   1068  1.1  christos 			el_set(rl_global.el, EL_BIND, nq->n_name, "mail-complete", NULL);
   1069  1.1  christos 	}
   1070  1.1  christos 
   1071  1.1  christos 	init_complete();
   1072  1.1  christos 
   1073  1.1  christos 	el_set(rl_global.el, EL_SIGNAL, 1);
   1074  1.4  christos 	el_set(rl_global.elo, EL_SIGNAL, 1);
   1075  1.1  christos 
   1076  1.1  christos 	return;
   1077  1.1  christos }
   1078  1.1  christos 
   1079  1.1  christos /************************************************************************/
   1080  1.1  christos #endif /* USE_READLINE */
   1081