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