Home | History | Annotate | Line # | Download | only in mail
complete.c revision 1.14
      1  1.14  christos /*	$NetBSD: complete.c,v 1.14 2007/10/29 23:20:38 christos Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.9  christos  * Copyright (c) 1997-2000,2005,2006 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.11  christos /*
     40  1.11  christos  * Most of this is derived or copied from src/usr.bin/ftp/complete.c (1.41).
     41  1.11  christos  */
     42   1.1  christos 
     43   1.9  christos #ifdef USE_EDITLINE
     44   1.9  christos #undef NO_EDITCOMPLETE
     45   1.1  christos 
     46   1.1  christos #include <sys/cdefs.h>
     47   1.1  christos #ifndef lint
     48  1.14  christos __RCSID("$NetBSD: complete.c,v 1.14 2007/10/29 23:20:38 christos Exp $");
     49   1.1  christos #endif /* not lint */
     50   1.1  christos 
     51   1.1  christos /*
     52   1.1  christos  * FTP user program - command and file completion routines
     53   1.1  christos  */
     54   1.1  christos 
     55  1.11  christos #include <assert.h>
     56   1.1  christos #include <ctype.h>
     57   1.1  christos #include <err.h>
     58   1.1  christos #include <dirent.h>
     59   1.1  christos #include <glob.h>
     60   1.1  christos #include <stdio.h>
     61   1.1  christos #include <stdlib.h>
     62   1.1  christos #include <string.h>
     63   1.1  christos #include <stringlist.h>
     64   1.9  christos #include <util.h>
     65   1.1  christos 
     66  1.11  christos #include <sys/param.h>
     67  1.11  christos #include <sys/stat.h>
     68  1.11  christos 
     69   1.1  christos #include "rcv.h"			/* includes "glob.h" */
     70   1.1  christos #include "extern.h"
     71   1.1  christos #include "complete.h"
     72   1.9  christos #ifdef MIME_SUPPORT
     73   1.9  christos #include "mime.h"
     74   1.9  christos #endif
     75  1.11  christos #ifdef THREAD_SUPPORT
     76  1.11  christos #include "thread.h"
     77  1.11  christos #endif
     78  1.11  christos 
     79  1.11  christos #define BELL 0x7
     80   1.1  christos 
     81   1.1  christos /*
     82   1.1  christos  * Global variables
     83   1.1  christos  */
     84   1.1  christos static int doglob = 1;			/* glob local file names */
     85   1.1  christos 
     86   1.1  christos #define ttyout stdout
     87   1.1  christos #define ttywidth screenwidth		/* in "glob.h" */
     88   1.1  christos #define ttyheight screenheight		/* in "glob.h" */
     89   1.1  christos 
     90   1.1  christos /************************************************************************/
     91   1.9  christos /* from src/usr.bin/ftp/utils.h (1.135) - begin */
     92   1.1  christos 
     93   1.1  christos /*
     94   1.1  christos  * List words in stringlist, vertically arranged
     95   1.1  christos  */
     96   1.1  christos static void
     97   1.1  christos list_vertical(StringList *sl)
     98   1.1  christos {
     99  1.11  christos 	int i, j, k;
    100   1.1  christos 	int columns, lines;
    101   1.1  christos 	char *p;
    102   1.1  christos 	size_t w, width;
    103   1.1  christos 
    104   1.1  christos 	width = 0;
    105   1.1  christos 
    106   1.9  christos 	for (i = 0; i < sl->sl_cur; i++) {
    107   1.1  christos 		w = strlen(sl->sl_str[i]);
    108   1.1  christos 		if (w > width)
    109   1.1  christos 			width = w;
    110   1.1  christos 	}
    111   1.1  christos 	width = (width + 8) &~ 7;
    112   1.1  christos 
    113   1.1  christos 	columns = ttywidth / width;
    114   1.1  christos 	if (columns == 0)
    115   1.1  christos 		columns = 1;
    116   1.1  christos 	lines = (sl->sl_cur + columns - 1) / columns;
    117  1.11  christos 	k = 0;
    118   1.1  christos 	for (i = 0; i < lines; i++) {
    119   1.1  christos 		for (j = 0; j < columns; j++) {
    120   1.1  christos 			p = sl->sl_str[j * lines + i];
    121   1.1  christos 			if (p)
    122   1.9  christos 				(void)fputs(p, ttyout);
    123   1.1  christos 			if (j * lines + i + lines >= sl->sl_cur) {
    124   1.9  christos 				(void)putc('\n', ttyout);
    125   1.1  christos 				break;
    126   1.1  christos 			}
    127   1.1  christos 			if (p) {
    128   1.1  christos 				w = strlen(p);
    129   1.1  christos 				while (w < width) {
    130   1.1  christos 					w = (w + 8) &~ 7;
    131   1.1  christos 					(void)putc('\t', ttyout);
    132   1.1  christos 				}
    133   1.1  christos 			}
    134   1.1  christos 		}
    135  1.11  christos 		if (ttyheight > 2 && ++k == ttyheight - 2) {
    136  1.11  christos 			int ch;
    137  1.11  christos 			k = 0;
    138  1.11  christos 			(void)fputs("--more--", ttyout);
    139  1.11  christos 			while ((ch = getchar()) != EOF && ch != ' ' && ch != 'q')
    140  1.11  christos 				(void)putc(BELL, ttyout);
    141  1.11  christos 			(void)fputs("\r        \r", ttyout);
    142  1.11  christos 			if (ch == 'q')
    143  1.11  christos 				break;
    144  1.11  christos 		}
    145   1.1  christos 	}
    146   1.1  christos }
    147   1.1  christos 
    148   1.1  christos /*
    149   1.1  christos  * Copy characters from src into dst, \ quoting characters that require it
    150   1.1  christos  */
    151   1.1  christos static void
    152   1.1  christos ftpvis(char *dst, size_t dstlen, const char *src, size_t srclen)
    153   1.1  christos {
    154   1.1  christos 	int	di, si;
    155   1.1  christos 
    156   1.1  christos 	for (di = si = 0;
    157   1.1  christos 	    src[si] != '\0' && di < dstlen && si < srclen;
    158   1.1  christos 	    di++, si++) {
    159   1.1  christos 		switch (src[si]) {
    160   1.1  christos 		case '\\':
    161   1.1  christos 		case ' ':
    162   1.1  christos 		case '\t':
    163   1.1  christos 		case '\r':
    164   1.1  christos 		case '\n':
    165   1.1  christos 		case '"':
    166   1.1  christos 			dst[di++] = '\\';
    167   1.1  christos 			if (di >= dstlen)
    168   1.1  christos 				break;
    169   1.1  christos 			/* FALLTHROUGH */
    170   1.1  christos 		default:
    171   1.1  christos 			dst[di] = src[si];
    172   1.1  christos 		}
    173   1.1  christos 	}
    174   1.1  christos 	dst[di] = '\0';
    175   1.1  christos }
    176   1.1  christos 
    177   1.1  christos /*
    178   1.1  christos  * sl_init() with inbuilt error checking
    179   1.1  christos  */
    180   1.1  christos static StringList *
    181   1.9  christos mail_sl_init(void)
    182   1.1  christos {
    183   1.1  christos 	StringList *p;
    184   1.1  christos 
    185   1.1  christos 	p = sl_init();
    186   1.1  christos 	if (p == NULL)
    187   1.1  christos 		err(1, "Unable to allocate memory for stringlist");
    188  1.11  christos 	return p;
    189   1.1  christos }
    190   1.1  christos 
    191   1.1  christos 
    192   1.1  christos /*
    193   1.1  christos  * sl_add() with inbuilt error checking
    194   1.1  christos  */
    195   1.1  christos static void
    196   1.9  christos mail_sl_add(StringList *sl, char *i)
    197   1.1  christos {
    198   1.1  christos 
    199   1.1  christos 	if (sl_add(sl, i) == -1)
    200   1.1  christos 		err(1, "Unable to add `%s' to stringlist", i);
    201   1.1  christos }
    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.9  christos 		return estrdup(pattern);
    220   1.1  christos 
    221   1.1  christos 	flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
    222   1.9  christos 	(void)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.11  christos 		return NULL;
    227   1.1  christos 	}
    228   1.9  christos 	p = estrdup(gl.gl_pathv[0]);
    229   1.1  christos 	globfree(&gl);
    230  1.11  christos 	return p;
    231   1.1  christos }
    232   1.1  christos 
    233   1.9  christos /* from src/usr.bin/ftp/utils.h (1.135) - end */
    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.11  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.9  christos  *	dolist	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.9  christos complete_ambiguous(EditLine *el, char *word, int dolist, 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.11  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.11  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.11  christos 			return CC_ERROR;
    272   1.1  christos 		else
    273  1.11  christos 			return CC_REFRESH;
    274   1.1  christos 	}
    275   1.1  christos 
    276   1.9  christos 	if (!dolist) {
    277   1.1  christos 		matchlen = 0;
    278   1.1  christos 		lastmatch = words->sl_str[0];
    279   1.1  christos 		matchlen = strlen(lastmatch);
    280   1.9  christos 		for (i = 1; i < words->sl_cur; i++) {
    281   1.9  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.11  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.11  christos 				return CC_ERROR;
    292   1.1  christos 			else
    293  1.11  christos 				return CC_REFRESH_BEEP;
    294   1.1  christos 		}
    295   1.1  christos 	}
    296   1.1  christos 
    297   1.9  christos 	(void)putc('\n', ttyout);
    298   1.1  christos 	qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
    299  1.11  christos 
    300   1.1  christos 	list_vertical(words);
    301  1.11  christos 	return CC_REDISPLAY;
    302   1.1  christos }
    303   1.1  christos 
    304   1.1  christos /*
    305   1.1  christos  * Complete a mail command.
    306   1.1  christos  */
    307   1.1  christos static unsigned char
    308   1.9  christos complete_command(EditLine *el, char *word, int dolist)
    309   1.1  christos {
    310   1.1  christos 	const struct cmd *c;
    311   1.1  christos 	StringList *words;
    312   1.1  christos 	size_t wordlen;
    313   1.1  christos 	unsigned char rv;
    314   1.1  christos 
    315   1.9  christos 	words = mail_sl_init();
    316   1.1  christos 	wordlen = strlen(word);
    317   1.1  christos 
    318   1.1  christos 	for (c = cmdtab; c->c_name != NULL; c++) {
    319   1.1  christos 		if (wordlen > strlen(c->c_name))
    320   1.1  christos 			continue;
    321   1.1  christos 		if (strncmp(word, c->c_name, wordlen) == 0)
    322   1.9  christos 			mail_sl_add(words, __UNCONST(c->c_name));
    323   1.1  christos 	}
    324   1.1  christos 
    325   1.9  christos 	rv = complete_ambiguous(el, word, dolist, words);
    326   1.1  christos 	if (rv == CC_REFRESH) {
    327   1.1  christos 		if (el_insertstr(el, " ") == -1)
    328   1.1  christos 			rv = CC_ERROR;
    329   1.1  christos 	}
    330   1.1  christos 	sl_free(words, 0);
    331  1.11  christos 	return rv;
    332   1.1  christos }
    333   1.1  christos 
    334   1.1  christos /*
    335   1.1  christos  * Complete a local filename.
    336   1.1  christos  */
    337   1.1  christos static unsigned char
    338   1.9  christos complete_filename(EditLine *el, char *word, int dolist)
    339   1.1  christos {
    340   1.1  christos 	StringList *words;
    341   1.1  christos 	char dir[MAXPATHLEN];
    342   1.1  christos 	char *fname;
    343   1.1  christos 	DIR *dd;
    344   1.1  christos 	struct dirent *dp;
    345   1.1  christos 	unsigned char rv;
    346   1.1  christos 	size_t len;
    347   1.1  christos 
    348   1.1  christos 	if ((fname = strrchr(word, '/')) == NULL) {
    349   1.1  christos 		dir[0] = '.';
    350   1.1  christos 		dir[1] = '\0';
    351   1.1  christos 		fname = word;
    352   1.1  christos 	} else {
    353   1.1  christos 		if (fname == word) {
    354   1.1  christos 			dir[0] = '/';
    355   1.1  christos 			dir[1] = '\0';
    356   1.9  christos 		} else {
    357   1.9  christos 			len = fname - word + 1;
    358   1.9  christos 			(void)estrlcpy(dir, word, sizeof(dir));
    359   1.9  christos 			dir[len] = '\0';
    360   1.9  christos 		}
    361   1.1  christos 		fname++;
    362   1.1  christos 	}
    363   1.1  christos 	if (dir[0] == '~') {
    364   1.1  christos 		char *p;
    365   1.1  christos 
    366   1.1  christos 		if ((p = globulize(dir)) == NULL)
    367  1.11  christos 			return CC_ERROR;
    368   1.9  christos 		(void)estrlcpy(dir, p, sizeof(dir));
    369   1.1  christos 		free(p);
    370   1.1  christos 	}
    371   1.1  christos 
    372   1.1  christos 	if ((dd = opendir(dir)) == NULL)
    373  1.11  christos 		return CC_ERROR;
    374   1.1  christos 
    375   1.9  christos 	words = mail_sl_init();
    376   1.1  christos 	len = strlen(fname);
    377   1.1  christos 
    378   1.1  christos 	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
    379   1.1  christos 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    380   1.1  christos 			continue;
    381   1.1  christos 
    382   1.1  christos #if defined(DIRENT_MISSING_D_NAMLEN)
    383   1.1  christos 		if (len > strlen(dp->d_name))
    384   1.1  christos 			continue;
    385   1.1  christos #else
    386   1.1  christos 		if (len > dp->d_namlen)
    387   1.1  christos 			continue;
    388   1.1  christos #endif
    389   1.1  christos 		if (strncmp(fname, dp->d_name, len) == 0) {
    390   1.1  christos 			char *tcp;
    391   1.1  christos 
    392   1.9  christos 			tcp = estrdup(dp->d_name);
    393   1.9  christos 			mail_sl_add(words, tcp);
    394   1.1  christos 		}
    395   1.1  christos 	}
    396   1.9  christos 	(void)closedir(dd);
    397   1.1  christos 
    398   1.9  christos 	rv = complete_ambiguous(el, fname, dolist, words);
    399   1.1  christos 	if (rv == CC_REFRESH) {
    400   1.1  christos 		struct stat sb;
    401   1.1  christos 		char path[MAXPATHLEN];
    402   1.1  christos 
    403   1.9  christos 		(void)estrlcpy(path, dir,		sizeof(path));
    404   1.9  christos 		(void)estrlcat(path, "/",		sizeof(path));
    405   1.9  christos 		(void)estrlcat(path, words->sl_str[0],	sizeof(path));
    406   1.1  christos 
    407   1.1  christos 		if (stat(path, &sb) >= 0) {
    408   1.1  christos 			char suffix[2] = " ";
    409   1.1  christos 
    410   1.1  christos 			if (S_ISDIR(sb.st_mode))
    411   1.1  christos 				suffix[0] = '/';
    412   1.1  christos 			if (el_insertstr(el, suffix) == -1)
    413   1.1  christos 				rv = CC_ERROR;
    414   1.1  christos 		}
    415   1.1  christos 	}
    416   1.1  christos 	sl_free(words, 1);
    417  1.11  christos 	return rv;
    418   1.1  christos }
    419   1.1  christos 
    420   1.1  christos static int
    421   1.1  christos find_execs(char *word, char *path, StringList *list)
    422   1.1  christos {
    423   1.1  christos 	char *sep;
    424   1.1  christos 	char *dir=path;
    425   1.1  christos 	DIR *dd;
    426   1.1  christos 	struct dirent *dp;
    427   1.9  christos 	size_t len = strlen(word);
    428   1.1  christos 	uid_t uid = getuid();
    429   1.1  christos 	gid_t gid = getgid();
    430   1.1  christos 
    431   1.9  christos 	for (sep = dir; sep; dir = sep + 1) {
    432   1.1  christos 		if ((sep=strchr(dir, ':')) != NULL) {
    433   1.1  christos 			*sep=0;
    434   1.1  christos 		}
    435  1.13  christos 
    436   1.1  christos 		if ((dd = opendir(dir)) == NULL) {
    437   1.1  christos 			perror("dir");
    438   1.1  christos 			return -1;
    439   1.1  christos 		}
    440  1.13  christos 
    441   1.1  christos 		for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
    442   1.1  christos 
    443   1.1  christos 			if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    444   1.1  christos 				continue;
    445  1.13  christos 
    446   1.1  christos #if defined(DIRENT_MISSING_D_NAMLEN)
    447   1.1  christos 			if (len > strlen(dp->d_name))
    448   1.1  christos 				continue;
    449   1.1  christos #else
    450   1.1  christos 			if (len > dp->d_namlen)
    451   1.1  christos 				continue;
    452   1.1  christos #endif
    453   1.1  christos 
    454   1.1  christos 			if (strncmp(word, dp->d_name, len) == 0) {
    455   1.1  christos 				struct stat sb;
    456   1.1  christos 				char pathname[ MAXPATHLEN ];
    457   1.1  christos 				unsigned mask;
    458   1.1  christos 
    459   1.9  christos 				(void)snprintf(pathname, sizeof(pathname),
    460   1.9  christos 				    "%s/%s", dir, dp->d_name);
    461   1.1  christos 				if (stat(pathname, &sb) != 0) {
    462   1.1  christos 					perror(pathname);
    463   1.1  christos 					continue;
    464   1.1  christos 				}
    465   1.1  christos 
    466   1.1  christos 				mask = 0001;
    467   1.1  christos 				if (sb.st_uid == uid)	mask |= 0100;
    468   1.1  christos 				if (sb.st_gid == gid)	mask |= 0010;
    469   1.1  christos 
    470   1.1  christos 				if ((sb.st_mode & mask) != 0) {
    471   1.1  christos 					char *tcp;
    472   1.9  christos 					tcp = estrdup(dp->d_name);
    473   1.9  christos 					mail_sl_add(list, tcp);
    474   1.1  christos 				}
    475   1.1  christos 			}
    476  1.13  christos 
    477   1.1  christos 		}
    478   1.1  christos 
    479   1.9  christos 		(void)closedir(dd);
    480   1.1  christos 	}
    481   1.1  christos 
    482   1.1  christos 	return 0;
    483   1.1  christos }
    484   1.1  christos 
    485   1.1  christos 
    486   1.1  christos /*
    487   1.1  christos  * Complete a local executable
    488   1.1  christos  */
    489   1.1  christos static unsigned char
    490   1.9  christos complete_executable(EditLine *el, char *word, int dolist)
    491   1.1  christos {
    492   1.1  christos 	StringList *words;
    493   1.1  christos 	char dir[ MAXPATHLEN ];
    494   1.1  christos 	char *fname;
    495   1.1  christos 	unsigned char rv;
    496   1.9  christos 	size_t len;
    497   1.1  christos 	int error;
    498   1.1  christos 
    499   1.1  christos 	if ((fname = strrchr(word, '/')) == NULL) {
    500   1.1  christos 		dir[0] = '\0';		/* walk the path */
    501   1.1  christos 		fname = word;
    502   1.1  christos 	} else {
    503   1.1  christos 		if (fname == word) {
    504   1.1  christos 			dir[0] = '/';
    505   1.1  christos 			dir[1] = '\0';
    506   1.1  christos 		} else {
    507   1.9  christos 			len = fname - word;
    508   1.9  christos 			(void)strncpy(dir, word, len);
    509   1.1  christos 			dir[fname - word] = '\0';
    510   1.1  christos 		}
    511   1.1  christos 		fname++;
    512   1.1  christos 	}
    513   1.1  christos 
    514   1.1  christos 	words = sl_init();
    515   1.1  christos 
    516   1.1  christos 	if (*dir == '\0') {		/* walk path */
    517   1.1  christos 		char *env;
    518   1.1  christos 		char *path;
    519   1.1  christos 		env = getenv("PATH");
    520   1.1  christos 		len = strlen(env);
    521   1.1  christos 		path = salloc(len + 1);
    522   1.9  christos 		(void)strcpy(path, env);
    523   1.1  christos 		error = find_execs(word, path, words);
    524   1.1  christos 	}
    525   1.1  christos 	else {		/* check specified dir only */
    526   1.1  christos 		error = find_execs(word, dir, words);
    527   1.1  christos 	}
    528   1.1  christos 	if (error != 0)
    529   1.1  christos 		return CC_ERROR;
    530   1.1  christos 
    531   1.9  christos 	rv = complete_ambiguous(el, fname, dolist, words);
    532  1.11  christos 	if (rv == CC_REFRESH) {
    533   1.1  christos 		if (el_insertstr(el, " ") == -1)
    534   1.1  christos 			rv = CC_ERROR;
    535  1.11  christos 	}
    536   1.1  christos 	sl_free(words, 1);
    537   1.1  christos 
    538  1.11  christos 	return rv;
    539   1.1  christos }
    540   1.1  christos 
    541   1.1  christos 
    542  1.11  christos static unsigned char
    543  1.11  christos complete_set(EditLine *el, char *word, int dolist)
    544   1.1  christos {
    545   1.1  christos 	struct var *vp;
    546  1.11  christos 	const char **ap;
    547  1.11  christos 	const char **p;
    548   1.1  christos 	int h;
    549   1.1  christos 	int s;
    550   1.9  christos 	size_t len = strlen(word);
    551   1.1  christos 	StringList *words;
    552   1.1  christos 	unsigned char rv;
    553   1.1  christos 
    554   1.1  christos 	words = sl_init();
    555   1.1  christos 
    556   1.1  christos 	/* allocate space for variables table */
    557  1.10  christos 	s = 1;
    558  1.10  christos 	for (h = 0; h < HSHSIZE; h++)
    559   1.1  christos 		for (vp = variables[h]; vp != NULL; vp = vp->v_link)
    560   1.1  christos 			s++;
    561  1.14  christos 	ap = salloc(s * sizeof(*ap));
    562   1.1  christos 
    563  1.10  christos 	/* save the pointers */
    564   1.1  christos 	for (h = 0, p = ap; h < HSHSIZE; h++)
    565   1.1  christos 		for (vp = variables[h]; vp != NULL; vp = vp->v_link)
    566   1.1  christos 			*p++ = vp->v_name;
    567   1.1  christos 	*p = NULL;
    568   1.1  christos 	sort(ap);
    569  1.10  christos 	for (p = ap; *p != NULL; p++)
    570  1.10  christos 		if (len == 0 || strncmp(*p, word, len) == 0)
    571  1.10  christos 			mail_sl_add(words, estrdup(*p));
    572   1.1  christos 
    573   1.9  christos 	rv = complete_ambiguous(el, word, dolist, words);
    574   1.1  christos 
    575   1.1  christos 	sl_free(words, 1);
    576   1.1  christos 
    577  1.11  christos 	return rv;
    578   1.1  christos }
    579   1.1  christos 
    580   1.1  christos 
    581   1.1  christos static unsigned char
    582   1.9  christos complete_alias(EditLine *el, char *word, int dolist)
    583   1.1  christos {
    584   1.1  christos 	struct grouphead *gh;
    585  1.11  christos 	const char **ap;
    586  1.11  christos 	const char **p;
    587   1.1  christos 	int h;
    588   1.1  christos 	int s;
    589   1.9  christos 	size_t len = strlen(word);
    590   1.1  christos 	StringList *words;
    591   1.1  christos 	unsigned char rv;
    592   1.1  christos 
    593   1.1  christos 	words = sl_init();
    594   1.1  christos 
    595   1.1  christos 	/* allocate space for alias table */
    596  1.10  christos 	s = 1;
    597  1.10  christos 	for (h = 0; h < HSHSIZE; h++)
    598   1.1  christos 		for (gh = groups[h]; gh != NULL; gh = gh->g_link)
    599   1.1  christos 			s++;
    600  1.11  christos 	ap = salloc(s * sizeof(*ap));
    601  1.13  christos 
    602   1.1  christos 	/* save pointers */
    603  1.10  christos 	p = ap;
    604  1.10  christos 	for (h = 0; h < HSHSIZE; h++)
    605   1.1  christos 		for (gh = groups[h]; gh != NULL; gh = gh->g_link)
    606   1.1  christos 			*p++ = gh->g_name;
    607  1.11  christos 
    608   1.1  christos 	*p = NULL;
    609  1.11  christos 
    610  1.10  christos 	sort(ap);
    611  1.10  christos 	for (p = ap; *p != NULL; p++)
    612  1.10  christos 		if (len == 0 || strncmp(*p, word, len) == 0)
    613  1.10  christos 			mail_sl_add(words, estrdup(*p));
    614  1.13  christos 
    615  1.10  christos 	rv = complete_ambiguous(el, word, dolist, words);
    616  1.11  christos 	if (rv == CC_REFRESH) {
    617  1.10  christos 		if (el_insertstr(el, " ") == -1)
    618  1.10  christos 			rv = CC_ERROR;
    619  1.11  christos 	}
    620  1.10  christos 	sl_free(words, 1);
    621  1.11  christos 	return rv;
    622  1.10  christos }
    623  1.10  christos 
    624  1.10  christos 
    625  1.11  christos static unsigned char
    626  1.11  christos complete_smopts(EditLine *el, char *word, int dolist)
    627  1.10  christos {
    628  1.10  christos 	struct grouphead *gh;
    629  1.10  christos 	struct smopts_s *sp;
    630  1.11  christos 	const char **ap;
    631  1.11  christos 	const char **p;
    632  1.10  christos 	int h;
    633  1.10  christos 	int s1;
    634  1.10  christos 	int s2;
    635  1.10  christos 	size_t len;
    636  1.10  christos 	StringList *words;
    637  1.10  christos 	unsigned char rv;
    638   1.1  christos 
    639  1.10  christos 	len = strlen(word);
    640  1.10  christos 	words = sl_init();
    641   1.1  christos 
    642  1.10  christos 	/* count the entries in the smoptstbl and groups (alias) tables */
    643  1.10  christos 	s1 = 1;
    644  1.10  christos 	s2 = 1;
    645  1.10  christos 	for (h = 0; h < HSHSIZE; h++) {
    646  1.10  christos 		for (sp = smoptstbl[h]; sp != NULL; sp = sp->s_link)
    647  1.10  christos 			s1++;
    648  1.10  christos 		for (gh = groups[h]; gh != NULL; gh = gh->g_link)
    649  1.10  christos 			s2++;
    650   1.1  christos 	}
    651   1.1  christos 
    652  1.10  christos 	/* allocate sufficient space for the pointers */
    653  1.14  christos 	ap = salloc(MAX(s1, s2) * sizeof(*ap));
    654  1.10  christos 
    655  1.10  christos 	/*
    656  1.10  christos 	 * First do the smoptstbl pointers. (case _insensitive_)
    657  1.10  christos 	 */
    658  1.10  christos 	p = ap;
    659  1.10  christos 	for (h = 0; h < HSHSIZE; h++)
    660  1.10  christos 		for (sp = smoptstbl[h]; sp != NULL; sp = sp->s_link)
    661  1.10  christos 			*p++ = sp->s_name;
    662  1.10  christos 	*p = NULL;
    663  1.10  christos 	sort(ap);
    664  1.10  christos 	for (p = ap; *p != NULL; p++)
    665  1.10  christos 		if (len == 0 || strncasecmp(*p, word, len) == 0)
    666  1.10  christos 			mail_sl_add(words, estrdup(*p));
    667  1.10  christos 
    668  1.10  christos 	/*
    669  1.10  christos 	 * Now do the groups (alias) pointers. (case sensitive)
    670  1.10  christos 	 */
    671  1.10  christos 	p = ap;
    672  1.10  christos 	for (h = 0; h < HSHSIZE; h++)
    673  1.10  christos 		for (gh = groups[h]; gh != NULL; gh = gh->g_link)
    674  1.10  christos 			*p++ = gh->g_name;
    675  1.10  christos 	*p = NULL;
    676  1.10  christos 	sort(ap);
    677  1.10  christos 	for (p = ap; *p != NULL; p++)
    678  1.10  christos 		if (len == 0 || strncmp(*p, word, len) == 0)
    679  1.10  christos 			mail_sl_add(words, estrdup(*p));
    680  1.10  christos 
    681   1.9  christos 	rv = complete_ambiguous(el, word, dolist, words);
    682   1.1  christos 
    683   1.1  christos 	sl_free(words, 1);
    684   1.1  christos 
    685  1.11  christos 	return rv;
    686   1.1  christos }
    687   1.1  christos 
    688   1.1  christos 
    689  1.11  christos #ifdef THREAD_SUPPORT
    690  1.11  christos static unsigned char
    691  1.11  christos complete_thread_key(EditLine *el, char *word, int dolist)
    692   1.1  christos {
    693  1.11  christos 	const char **ap;
    694  1.11  christos 	const char **p;
    695  1.11  christos 	const char *name;
    696  1.11  christos 	size_t len;
    697  1.11  christos 	StringList *words;
    698  1.11  christos 	unsigned char rv;
    699  1.11  christos 	int cnt;
    700  1.11  christos 	const void *cookie;
    701   1.1  christos 
    702  1.11  christos 	len = strlen(word);
    703  1.11  christos 	words = sl_init();
    704   1.1  christos 
    705  1.11  christos 	/* count the entries in the table */
    706  1.11  christos 	/* XXX - have a function return this rather than counting? */
    707  1.11  christos 	cnt = 1;	/* count the NULL terminator */
    708  1.11  christos 	cookie = NULL;
    709  1.11  christos 	while (thread_next_key_name(&cookie) != NULL)
    710  1.11  christos 		cnt++;
    711   1.1  christos 
    712  1.11  christos 	/* allocate sufficient space for the pointers */
    713  1.14  christos 	ap = salloc(cnt * sizeof(*ap));
    714   1.1  christos 
    715  1.11  christos 	/* load the array */
    716  1.11  christos 	p = ap;
    717  1.11  christos 	cookie = NULL;
    718  1.11  christos 	while ((name = thread_next_key_name(&cookie)) != NULL)
    719  1.11  christos 		*p++ = name;
    720  1.11  christos 	*p = NULL;
    721  1.11  christos 	sort(ap);
    722  1.11  christos 	for (p = ap; *p != NULL; p++)
    723  1.11  christos 		if (len == 0 || strncmp(*p, word, len) == 0)
    724  1.11  christos 			mail_sl_add(words, estrdup(*p));
    725   1.1  christos 
    726  1.11  christos 	rv = complete_ambiguous(el, word, dolist, words);
    727   1.1  christos 
    728  1.11  christos 	sl_free(words, 1);
    729   1.1  christos 
    730  1.11  christos 	return rv;
    731   1.1  christos }
    732  1.11  christos #endif /* THREAD_SUPPORT */
    733   1.1  christos 
    734   1.9  christos /* from /usr/src/usr.bin/ftp/main.c(1.101) - end */
    735   1.1  christos /************************************************************************/
    736   1.1  christos 
    737   1.9  christos /* Some people like to bind file completion to CTRL-D.  In emacs mode,
    738   1.9  christos  * CTRL-D is also used to delete the current character, we have to
    739   1.9  christos  * special case this situation.
    740   1.9  christos  */
    741   1.9  christos #define EMACS_CTRL_D_BINDING_HACK
    742   1.9  christos 
    743   1.9  christos #ifdef EMACS_CTRL_D_BINDING_HACK
    744   1.9  christos static int
    745   1.9  christos is_emacs_mode(EditLine *el)
    746   1.9  christos {
    747   1.9  christos 	char *mode;
    748   1.9  christos 	if (el_get(el, EL_EDITOR, &mode) == -1)
    749   1.9  christos 		return 0;
    750   1.9  christos 	return equal(mode, "emacs");
    751   1.9  christos }
    752   1.9  christos 
    753   1.9  christos static int
    754  1.11  christos emacs_ctrl_d(EditLine *el, const LineInfo *lf, int ch)
    755   1.9  christos {
    756   1.9  christos 	static char delunder[3] = { CTRL('f'), CTRL('h'), '\0' };
    757   1.9  christos 	if (ch == CTRL('d') && is_emacs_mode(el)) {	/* CTRL-D is special */
    758  1.13  christos 		if (lf->buffer == lf->lastchar)
    759  1.11  christos 			return CC_EOF;
    760  1.11  christos 		if (lf->cursor != lf->lastchar) { /* delete without using ^D */
    761   1.9  christos 			el_push(el, delunder); /* ^F^H */
    762  1.11  christos 			return CC_NORM;
    763   1.9  christos 		}
    764   1.9  christos 	}
    765   1.9  christos 	return -1;
    766   1.9  christos }
    767   1.9  christos #endif /* EMACS_CTRL_D_BINDING_HACK */
    768   1.1  christos 
    769   1.1  christos /*
    770  1.11  christos  * Check if this is the second request made for this line indicating
    771  1.11  christos  * the need to list all the completion possibilities.
    772  1.11  christos  */
    773  1.11  christos static int
    774  1.11  christos get_dolist(const LineInfo *lf)
    775  1.11  christos {
    776  1.11  christos 	static char last_line[LINESIZE];
    777  1.11  christos 	static char *last_cursor_pos;
    778  1.11  christos 	char *cursor_pos;
    779  1.11  christos 	int dolist;
    780  1.11  christos 	size_t len;
    781  1.11  christos 
    782  1.11  christos 	len = lf->lastchar - lf->buffer;
    783  1.11  christos 	if (len >= sizeof(last_line) - 1)
    784  1.11  christos 		return -1;
    785  1.11  christos 
    786  1.11  christos 	cursor_pos = last_line + (lf->cursor - lf->buffer);
    787  1.11  christos 	dolist =
    788  1.11  christos 	    cursor_pos == last_cursor_pos &&
    789  1.11  christos 	    strncmp(last_line, lf->buffer, len) == 0;
    790  1.11  christos 
    791  1.11  christos 	(void)strlcpy(last_line, lf->buffer, len + 1);
    792  1.11  christos 	last_cursor_pos = cursor_pos;
    793  1.11  christos 
    794  1.11  christos 	return dolist;
    795  1.11  christos }
    796  1.11  christos 
    797  1.11  christos /*
    798  1.11  christos  * Take the full line (lf) including the command and split it into a
    799  1.11  christos  * sub-line (returned) and a completion context (cmplarray).
    800  1.11  christos  */
    801  1.11  christos static LineInfo *
    802  1.11  christos split_line(const char **cmplarray, const LineInfo *lf)
    803  1.11  christos {
    804  1.11  christos 	static LineInfo li;
    805  1.11  christos 	const struct cmd *c;
    806  1.11  christos 	char *cmdname;
    807  1.11  christos 	char line[LINESIZE];
    808  1.11  christos 	char *cp;
    809  1.11  christos 	size_t len;
    810  1.11  christos 
    811  1.11  christos 	len = lf->cursor - lf->buffer;
    812  1.11  christos 	if (len + 1 > sizeof(line))
    813  1.11  christos 		return NULL;
    814  1.11  christos 
    815  1.11  christos 	(void)strlcpy(line, lf->buffer, len + 1);
    816  1.11  christos 
    817  1.11  christos 	li.cursor   = line + len;
    818  1.11  christos 	li.lastchar = line + len;
    819  1.13  christos 
    820  1.13  christos 	cp = skip_WSP(line);
    821  1.11  christos 	cmdname = get_cmdname(cp);
    822  1.11  christos 	cp += strlen(cmdname);
    823  1.11  christos 
    824  1.11  christos 	if (cp == li.cursor) {
    825  1.11  christos 		*cmplarray = "c";
    826  1.11  christos 		li.buffer = cmdname;
    827  1.11  christos 		return &li;
    828  1.11  christos 	}
    829  1.11  christos 
    830  1.11  christos 	c = lex(cmdname);
    831  1.11  christos 	if (c == NULL)
    832  1.11  christos 		return NULL;
    833  1.13  christos 
    834  1.11  christos 	*cmplarray = c->c_complete;
    835  1.11  christos 	if (c->c_pipe) {
    836  1.11  christos 		char *cp2;
    837  1.11  christos 		if ((cp2 = shellpr(cp)) != NULL) {
    838  1.11  christos 			cp = cp2;
    839  1.13  christos # define XX(a)  ((a) + ((a)[1] == '>' ? 2 : 1))
    840  1.11  christos 			while ((cp2 = shellpr(XX(cp))) != NULL)
    841  1.11  christos 				cp = cp2;
    842  1.13  christos 
    843  1.11  christos 			if (*cp == '|') {
    844  1.11  christos 				*cmplarray = "xF";
    845  1.13  christos 				cp = skip_WSP(cp + 1);
    846  1.11  christos 			}
    847  1.11  christos 			else {
    848  1.11  christos 				assert(*cp == '>');
    849  1.13  christos 				cp = skip_WSP(XX(cp));
    850  1.11  christos 				*cmplarray = "f";
    851  1.11  christos 			}
    852  1.11  christos # undef XX
    853  1.11  christos 		}
    854  1.11  christos 	}
    855  1.11  christos 	li.buffer = cp;
    856  1.11  christos 	return &li;
    857  1.11  christos }
    858  1.11  christos 
    859  1.11  christos /*
    860  1.11  christos  * Split a sub-line and a completion context into a word and a
    861  1.11  christos  * completion type.  Use the editline tokenizer to handle the quoting
    862  1.11  christos  * and splitting.
    863  1.11  christos  */
    864  1.11  christos static char *
    865  1.11  christos split_word(int *cmpltype, const char *cmplarray, LineInfo *li)
    866  1.11  christos {
    867  1.11  christos 	static Tokenizer *t = NULL;
    868  1.11  christos 	const char **argv;
    869  1.11  christos 	char *word;
    870  1.11  christos 	int argc;
    871  1.11  christos 	int cursorc;
    872  1.11  christos 	int cursoro;
    873  1.11  christos 	int arraylen;
    874  1.11  christos 
    875  1.11  christos 	if (t != NULL)
    876  1.11  christos 		tok_reset(t);
    877  1.11  christos 	else {
    878  1.11  christos 		if ((t = tok_init(NULL)) == NULL)
    879  1.11  christos 			err(EXIT_FAILURE, "tok_init");
    880  1.11  christos 	}
    881  1.11  christos 	if (tok_line(t, li, &argc, &argv, &cursorc, &cursoro) == -1)
    882  1.11  christos 		err(EXIT_FAILURE, "tok_line");
    883  1.13  christos 
    884  1.11  christos 	if (cursorc >= argc)
    885  1.11  christos 		word = __UNCONST("");
    886  1.11  christos 	else {
    887  1.11  christos 		word = salloc((size_t)cursoro + 1);
    888  1.11  christos 		(void)strlcpy(word, argv[cursorc], (size_t)cursoro + 1);
    889  1.11  christos 	}
    890  1.11  christos 
    891  1.11  christos 	/* check for 'continuation' completes (which are uppercase) */
    892  1.11  christos 	arraylen = strlen(cmplarray);
    893  1.11  christos 	if (cursorc >= arraylen &&
    894  1.11  christos 	    arraylen > 0 &&
    895  1.11  christos 	    isupper((unsigned char)cmplarray[arraylen - 1]))
    896  1.11  christos 		cursorc = arraylen - 1;
    897  1.13  christos 
    898  1.11  christos 	if (cursorc >= arraylen)
    899  1.11  christos 		return NULL;
    900  1.13  christos 
    901  1.11  christos 	*cmpltype = cmplarray[cursorc];
    902  1.11  christos 	return word;
    903  1.11  christos }
    904  1.11  christos 
    905  1.11  christos /*
    906  1.11  christos  * A generic complete routine for the mail command line.
    907   1.1  christos  */
    908   1.1  christos static unsigned char
    909   1.9  christos mail_complete(EditLine *el, int ch)
    910   1.1  christos {
    911  1.11  christos 	LineInfo *li;
    912   1.1  christos 	const LineInfo *lf;
    913  1.11  christos 	const char *cmplarray;
    914  1.11  christos 	int dolist;
    915  1.11  christos 	int cmpltype;
    916  1.11  christos 	char *word;
    917   1.1  christos 
    918   1.1  christos 	lf = el_line(el);
    919   1.9  christos 
    920   1.9  christos #ifdef EMACS_CTRL_D_BINDING_HACK
    921   1.9  christos 	{
    922   1.9  christos 		int cc_ret;
    923  1.11  christos 		if ((cc_ret = emacs_ctrl_d(el, lf, ch)) != -1)
    924   1.9  christos 			return cc_ret;
    925   1.1  christos 	}
    926   1.9  christos #endif /* EMACS_CTRL_D_BINDING_HACK */
    927   1.9  christos 
    928  1.11  christos 	if ((dolist = get_dolist(lf)) == -1)
    929  1.11  christos 		return CC_ERROR;
    930   1.9  christos 
    931  1.11  christos 	if ((li = split_line(&cmplarray, lf)) == NULL)
    932  1.11  christos 		return CC_ERROR;
    933   1.1  christos 
    934  1.11  christos 	if ((word = split_word(&cmpltype, cmplarray, li)) == NULL)
    935  1.11  christos 		return CC_ERROR;
    936   1.1  christos 
    937   1.1  christos 	switch (cmpltype) {
    938  1.11  christos 	case 'a':			/* alias complete */
    939  1.11  christos 	case 'A':
    940  1.11  christos 		return complete_alias(el, word, dolist);
    941  1.13  christos 
    942  1.11  christos 	case 'c':			/* command complete */
    943  1.11  christos 	case 'C':
    944  1.11  christos 		return complete_command(el, word, dolist);
    945  1.13  christos 
    946  1.11  christos 	case 'f':			/* filename complete */
    947  1.11  christos 	case 'F':
    948  1.11  christos 		return complete_filename(el, word, dolist);
    949  1.13  christos 
    950  1.11  christos 	case 'm':
    951  1.11  christos 	case 'M':
    952  1.11  christos 		return complete_smopts(el, word, dolist);
    953  1.13  christos 
    954  1.11  christos 	case 'n':			/* no complete */
    955  1.11  christos 	case 'N':			/* no complete */
    956  1.11  christos 		return CC_ERROR;
    957  1.13  christos 
    958  1.11  christos 	case 's':
    959  1.11  christos 	case 'S':
    960  1.11  christos 		return complete_set(el, word, dolist);
    961  1.11  christos #ifdef THREAD_SUPPORT
    962  1.11  christos 	case 't':
    963  1.11  christos 	case 'T':
    964  1.11  christos 		return complete_thread_key(el, word, dolist);
    965  1.10  christos #endif
    966   1.1  christos 		case 'x':			/* executable complete */
    967  1.11  christos 	case 'X':
    968  1.11  christos 		return complete_executable(el, word, dolist);
    969  1.13  christos 
    970  1.11  christos 	default:
    971  1.11  christos 		warnx("unknown complete type `%c'", cmpltype);
    972  1.11  christos #if 0
    973  1.11  christos 		assert(/*CONSTCOND*/0);
    974  1.11  christos #endif
    975  1.11  christos 		return CC_ERROR;
    976   1.1  christos 	}
    977   1.1  christos 	/* NOTREACHED */
    978   1.1  christos }
    979   1.1  christos 
    980   1.1  christos 
    981   1.9  christos /*
    982  1.11  christos  * A generic file completion routine.
    983   1.9  christos  */
    984   1.9  christos static unsigned char
    985   1.9  christos file_complete(EditLine *el, int ch)
    986   1.9  christos {
    987   1.9  christos 	static char word[LINESIZE];
    988   1.9  christos 	const LineInfo *lf;
    989  1.11  christos 	size_t word_len;
    990  1.11  christos 	int dolist;
    991   1.9  christos 
    992   1.9  christos 	lf = el_line(el);
    993   1.9  christos 
    994   1.9  christos #ifdef EMACS_CTRL_D_BINDING_HACK
    995   1.9  christos 	{
    996   1.9  christos 		int cc_ret;
    997  1.11  christos 		if ((cc_ret = emacs_ctrl_d(el, lf, ch)) != -1)
    998   1.9  christos 			return cc_ret;
    999   1.9  christos 	}
   1000   1.9  christos #endif /* EMACS_CTRL_D_BINDING_HACK */
   1001   1.9  christos 
   1002  1.11  christos 	word_len = lf->cursor - lf->buffer;
   1003  1.11  christos 	if (word_len + 1 > sizeof(word))
   1004  1.11  christos 		return CC_ERROR;
   1005   1.9  christos 
   1006   1.9  christos 	(void)strlcpy(word, lf->buffer, word_len + 1);	/* do not use estrlcpy here! */
   1007  1.11  christos 
   1008  1.11  christos 	if ((dolist = get_dolist(lf)) == -1)
   1009  1.11  christos 		return CC_ERROR;
   1010  1.11  christos 
   1011  1.11  christos 	return complete_filename(el, word, dolist);
   1012   1.9  christos }
   1013   1.1  christos 
   1014   1.1  christos 
   1015   1.9  christos #ifdef MIME_SUPPORT
   1016   1.9  christos /*
   1017  1.11  christos  * Complete mime_transfer_encoding type.
   1018   1.9  christos  */
   1019   1.9  christos static unsigned char
   1020   1.9  christos mime_enc_complete(EditLine *el, int ch)
   1021   1.1  christos {
   1022   1.9  christos 	static char word[LINESIZE];
   1023   1.9  christos 	StringList *words;
   1024   1.9  christos 	unsigned char rv;
   1025   1.9  christos 	const LineInfo *lf;
   1026  1.11  christos 	size_t word_len;
   1027  1.11  christos 	int dolist;
   1028   1.9  christos 
   1029   1.9  christos 	lf = el_line(el);
   1030   1.1  christos 
   1031   1.9  christos #ifdef EMACS_CTRL_D_BINDING_HACK
   1032   1.9  christos 	{
   1033   1.9  christos 		int cc_ret;
   1034  1.11  christos 		if ((cc_ret = emacs_ctrl_d(el, lf, ch)) != -1)
   1035   1.9  christos 			return cc_ret;
   1036   1.9  christos 	}
   1037   1.9  christos #endif /* EMACS_CTRL_D_BINDING_HACK */
   1038   1.1  christos 
   1039  1.11  christos 	word_len = lf->cursor - lf->buffer;
   1040  1.11  christos 	if (word_len >= sizeof(word) - 1)
   1041  1.11  christos 		return CC_ERROR;
   1042   1.1  christos 
   1043   1.9  christos 	words = mail_sl_init();
   1044   1.9  christos 	{
   1045   1.9  christos 		const char *ename;
   1046   1.9  christos 		const void *cookie;
   1047   1.9  christos 		cookie = NULL;
   1048   1.9  christos 		for (ename = mime_next_encoding_name(&cookie);
   1049   1.9  christos 		     ename;
   1050   1.9  christos 		     ename = mime_next_encoding_name(&cookie))
   1051   1.9  christos 			if (word_len == 0 ||
   1052   1.9  christos 			    strncmp(lf->buffer, ename, word_len) == 0) {
   1053   1.9  christos 				char *cp;
   1054   1.9  christos 				cp = estrdup(ename);
   1055   1.9  christos 				mail_sl_add(words, cp);
   1056   1.9  christos 			}
   1057   1.9  christos 	}
   1058  1.11  christos 	(void)strlcpy(word, lf->buffer, word_len + 1);
   1059   1.1  christos 
   1060  1.11  christos 	if ((dolist = get_dolist(lf)) == -1)
   1061  1.11  christos 		return CC_ERROR;
   1062  1.11  christos 
   1063  1.11  christos 	rv = complete_ambiguous(el, word, dolist, words);
   1064   1.1  christos 
   1065   1.9  christos 	sl_free(words, 1);
   1066  1.11  christos 	return rv;
   1067   1.1  christos }
   1068   1.9  christos #endif /* MIME_SUPPORT */
   1069   1.9  christos 
   1070  1.11  christos 
   1071   1.9  christos /*************************************************************************
   1072   1.9  christos  * Our public interface to el_gets():
   1073   1.9  christos  *
   1074   1.9  christos  * init_editline()
   1075   1.9  christos  *    Initializes of all editline and completion data strutures.
   1076   1.9  christos  *
   1077   1.9  christos  * my_gets()
   1078   1.9  christos  *    Returns the next line of input as a NULL termnated string without
   1079   1.9  christos  *    the trailing newline.
   1080  1.11  christos  *
   1081  1.11  christos  * my_getline()
   1082  1.11  christos  *    Same as my_gets(), but strips leading and trailing whitespace
   1083  1.11  christos  *    and returns an empty line if it gets a SIGINT.
   1084   1.9  christos  */
   1085   1.1  christos 
   1086   1.9  christos static const char *el_prompt;
   1087   1.9  christos 
   1088  1.13  christos /*ARGSUSED*/
   1089   1.9  christos static const char *
   1090   1.9  christos show_prompt(EditLine *e __unused)
   1091   1.9  christos {
   1092   1.9  christos 	return el_prompt;
   1093   1.9  christos }
   1094   1.1  christos 
   1095  1.11  christos PUBLIC char *
   1096   1.9  christos my_gets(el_mode_t *em, const char *prompt, char *string)
   1097   1.1  christos {
   1098   1.9  christos 	int cnt;
   1099   1.9  christos 	size_t len;
   1100   1.9  christos 	const char *buf;
   1101   1.9  christos 	HistEvent ev;
   1102   1.3  christos 	static char line[LINE_MAX];
   1103   1.1  christos 
   1104   1.9  christos 	el_prompt = prompt;
   1105   1.1  christos 
   1106   1.1  christos 	if (string)
   1107   1.9  christos 		el_push(em->el, string);
   1108   1.9  christos 
   1109   1.9  christos 	buf = el_gets(em->el, &cnt);
   1110   1.1  christos 
   1111   1.3  christos 	if (buf == NULL || cnt <= 0) {
   1112   1.3  christos 		if (cnt == 0)
   1113   1.9  christos 			(void)putc('\n', stdout);
   1114   1.9  christos 		return NULL;
   1115   1.3  christos 	}
   1116   1.1  christos 
   1117  1.11  christos 	if (buf[cnt - 1] == '\n')
   1118  1.11  christos 		cnt--;	/* trash the trailing LF */
   1119   1.9  christos 	len = MIN(sizeof(line) - 1, cnt);
   1120   1.9  christos 	(void)memcpy(line, buf, len);
   1121   1.1  christos 	line[cnt] = '\0';
   1122   1.1  christos 
   1123   1.9  christos 	/* enter non-empty lines into history */
   1124   1.9  christos 	if (em->hist) {
   1125   1.9  christos 		const char *p;
   1126  1.13  christos 		p = skip_WSP(line);
   1127   1.9  christos 		if (*p && history(em->hist, &ev, H_ENTER, line) == 0)
   1128   1.9  christos 			(void)printf("Failed history entry: %s", line);
   1129   1.9  christos 	}
   1130   1.1  christos 	return line;
   1131   1.1  christos }
   1132   1.1  christos 
   1133  1.11  christos #ifdef MIME_SUPPORT
   1134  1.11  christos /* XXX - do we really want this here? */
   1135  1.11  christos 
   1136  1.11  christos static jmp_buf intjmp;
   1137  1.11  christos /*ARGSUSED*/
   1138  1.11  christos static void
   1139  1.11  christos sigint(int signum __unused)
   1140  1.11  christos {
   1141  1.11  christos 	siglongjmp(intjmp, 1);
   1142  1.11  christos }
   1143  1.11  christos 
   1144  1.11  christos PUBLIC char *
   1145  1.11  christos my_getline(el_mode_t *em, const char *prompt, const char *str)
   1146  1.11  christos {
   1147  1.11  christos 	sig_t saveint;
   1148  1.11  christos 	char *cp;
   1149  1.11  christos 	char *line;
   1150  1.11  christos 
   1151  1.11  christos 	saveint = signal(SIGINT, sigint);
   1152  1.11  christos 	if (sigsetjmp(intjmp, 1)) {
   1153  1.11  christos 		(void)signal(SIGINT, saveint);
   1154  1.11  christos 		(void)putc('\n', stdout);
   1155  1.11  christos 		return __UNCONST("");
   1156  1.11  christos 	}
   1157  1.11  christos 
   1158  1.11  christos 	line = my_gets(em, prompt, __UNCONST(str));
   1159  1.11  christos 	/* LINTED */
   1160  1.11  christos 	line = line ? savestr(line) : __UNCONST("");
   1161  1.11  christos 
   1162  1.11  christos 	(void)signal(SIGINT, saveint);
   1163  1.11  christos 
   1164  1.11  christos 	/* strip trailing white space */
   1165  1.11  christos 	for (cp = line + strlen(line) - 1;
   1166  1.13  christos 	     cp >= line && is_WSP(*cp); cp--)
   1167  1.11  christos 		*cp = '\0';
   1168  1.11  christos 
   1169  1.11  christos 	/* skip leading white space */
   1170  1.13  christos 	cp = skip_WSP(line);
   1171  1.11  christos 
   1172  1.11  christos 	return cp;
   1173  1.11  christos }
   1174  1.11  christos #endif /* MIME_SUPPORT */
   1175  1.11  christos 
   1176   1.9  christos static el_mode_t
   1177   1.9  christos init_el_mode(
   1178   1.9  christos 	const char *el_editor,
   1179   1.9  christos 	unsigned char (*completer)(EditLine *, int),
   1180   1.9  christos 	struct name *keys,
   1181   1.9  christos 	int history_size)
   1182   1.9  christos {
   1183   1.9  christos 	el_mode_t em;
   1184   1.9  christos 	(void)memset(&em, 0, sizeof(em));
   1185   1.9  christos 
   1186  1.12  christos 	if ((em.el = el_init(getprogname(), stdin, stdout, stderr)) == NULL) {
   1187  1.12  christos 		warn("el_init");
   1188  1.12  christos 		return em;
   1189  1.12  christos 	}
   1190   1.1  christos 
   1191   1.9  christos 	(void)el_set(em.el, EL_PROMPT, show_prompt);
   1192  1.12  christos 	(void)el_set(em.el, EL_SIGNAL, 1); /* editline handles the signals. */
   1193  1.13  christos 
   1194   1.9  christos 	if (el_editor)
   1195   1.9  christos 		(void)el_set(em.el, EL_EDITOR, el_editor);
   1196   1.9  christos 
   1197   1.9  christos 	if (completer) {
   1198   1.9  christos 		struct name *np;
   1199   1.9  christos 		(void)el_set(em.el, EL_ADDFN, "mail-complete",
   1200   1.9  christos 		    "Context sensitive argument completion", completer);
   1201   1.9  christos 		for (np = keys; np; np = np->n_flink)
   1202   1.9  christos 			(void)el_set(em.el, EL_BIND, np->n_name,
   1203   1.9  christos 			    "mail-complete", NULL);
   1204   1.9  christos 	}
   1205   1.9  christos 
   1206   1.9  christos 	if (history_size) {
   1207   1.9  christos 		HistEvent ev;
   1208  1.12  christos 		if ((em.hist = history_init()) == NULL) {
   1209  1.12  christos 			warn("history_init");
   1210  1.12  christos 			return em;
   1211  1.12  christos 		}
   1212  1.12  christos 		if (history(em.hist, &ev, H_SETSIZE, history_size) == -1)
   1213   1.9  christos 			(void)printf("history: %s\n", ev.str);
   1214   1.9  christos 		(void)el_set(em.el, EL_HIST, history, em.hist);
   1215   1.9  christos 	}
   1216   1.9  christos 
   1217   1.9  christos 	(void)el_source(em.el, NULL);		/* read ~/.editrc */
   1218   1.9  christos 
   1219   1.9  christos 	return em;
   1220   1.1  christos }
   1221   1.1  christos 
   1222   1.9  christos 
   1223   1.9  christos struct el_modes_s elm = {
   1224   1.9  christos 	.command  = { .el = NULL, .hist = NULL, },
   1225   1.9  christos 	.string   = { .el = NULL, .hist = NULL, },
   1226   1.9  christos 	.filec    = { .el = NULL, .hist = NULL, },
   1227   1.9  christos #ifdef MIME_SUPPORT
   1228   1.9  christos 	.mime_enc = { .el = NULL, .hist = NULL, },
   1229   1.9  christos #endif
   1230   1.9  christos };
   1231   1.9  christos 
   1232  1.11  christos PUBLIC void
   1233   1.9  christos init_editline(void)
   1234   1.1  christos {
   1235   1.9  christos 	const char *mode;
   1236   1.9  christos 	int hist_size;
   1237   1.9  christos 	struct name *keys;
   1238   1.9  christos 	char *cp;
   1239   1.9  christos 
   1240   1.9  christos 	mode = value(ENAME_EL_EDITOR);
   1241  1.13  christos 
   1242   1.9  christos 	cp = value(ENAME_EL_HISTORY_SIZE);
   1243   1.9  christos 	hist_size = cp ? atoi(cp) : 0;
   1244   1.1  christos 
   1245   1.9  christos 	cp = value(ENAME_EL_COMPLETION_KEYS);
   1246   1.9  christos 	keys = cp && *cp ? lexpand(cp, 0) : NULL;
   1247   1.1  christos 
   1248   1.9  christos 	elm.command  = init_el_mode(mode, mail_complete,     keys, hist_size);
   1249   1.9  christos 	elm.filec    = init_el_mode(mode, file_complete,     keys, 0);
   1250   1.9  christos 	elm.string   = init_el_mode(mode, NULL,              NULL, 0);
   1251   1.9  christos #ifdef MIME_SUPPORT
   1252   1.9  christos 	elm.mime_enc = init_el_mode(mode, mime_enc_complete, keys, 0);
   1253   1.9  christos #endif
   1254   1.1  christos 	return;
   1255   1.1  christos }
   1256   1.1  christos 
   1257   1.9  christos #endif /* USE_EDITLINE */
   1258