Home | History | Annotate | Line # | Download | only in ftp
complete.c revision 1.28
      1  1.28     lukem /*	$NetBSD: complete.c,v 1.28 1999/09/24 05:35:09 lukem Exp $	*/
      2   1.1     lukem 
      3   1.1     lukem /*-
      4  1.21     lukem  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
      5   1.1     lukem  * All rights reserved.
      6   1.1     lukem  *
      7   1.1     lukem  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     lukem  * by Luke Mewburn.
      9   1.1     lukem  *
     10   1.1     lukem  * Redistribution and use in source and binary forms, with or without
     11   1.1     lukem  * modification, are permitted provided that the following conditions
     12   1.1     lukem  * are met:
     13   1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     14   1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     15   1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     lukem  *    documentation and/or other materials provided with the distribution.
     18   1.1     lukem  * 3. All advertising materials mentioning features or use of this software
     19   1.1     lukem  *    must display the following acknowledgement:
     20   1.1     lukem  *        This product includes software developed by the NetBSD
     21   1.1     lukem  *        Foundation, Inc. and its contributors.
     22   1.1     lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1     lukem  *    contributors may be used to endorse or promote products derived
     24   1.1     lukem  *    from this software without specific prior written permission.
     25   1.1     lukem  *
     26   1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.10     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.10     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1     lukem  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1     lukem  */
     38   1.1     lukem 
     39   1.9     lukem #include <sys/cdefs.h>
     40   1.1     lukem #ifndef lint
     41  1.28     lukem __RCSID("$NetBSD: complete.c,v 1.28 1999/09/24 05:35:09 lukem Exp $");
     42   1.1     lukem #endif /* not lint */
     43   1.1     lukem 
     44   1.1     lukem /*
     45   1.1     lukem  * FTP user program - command and file completion routines
     46   1.1     lukem  */
     47   1.1     lukem 
     48  1.13     lukem #include <sys/stat.h>
     49  1.25  christos #include <signal.h>
     50  1.13     lukem 
     51   1.1     lukem #include <ctype.h>
     52   1.1     lukem #include <err.h>
     53   1.1     lukem #include <dirent.h>
     54   1.1     lukem #include <stdio.h>
     55   1.1     lukem #include <stdlib.h>
     56   1.1     lukem #include <string.h>
     57   1.1     lukem 
     58   1.1     lukem #include "ftp_var.h"
     59   1.1     lukem 
     60  1.24       cgd #ifndef NO_EDITCOMPLETE
     61  1.24       cgd 
     62   1.9     lukem static int	     comparstr		__P((const void *, const void *));
     63   1.9     lukem static unsigned char complete_ambiguous	__P((char *, int, StringList *));
     64   1.9     lukem static unsigned char complete_command	__P((char *, int));
     65   1.9     lukem static unsigned char complete_local	__P((char *, int));
     66   1.9     lukem static unsigned char complete_remote	__P((char *, int));
     67   1.9     lukem 
     68   1.1     lukem static int
     69   1.1     lukem comparstr(a, b)
     70   1.1     lukem 	const void *a, *b;
     71   1.1     lukem {
     72  1.26  christos 	return (strcmp(*(const char **)a, *(const char **)b));
     73   1.1     lukem }
     74   1.1     lukem 
     75   1.1     lukem /*
     76   1.1     lukem  * Determine if complete is ambiguous. If unique, insert.
     77   1.1     lukem  * If no choices, error. If unambiguous prefix, insert that.
     78   1.1     lukem  * Otherwise, list choices. words is assumed to be filtered
     79   1.1     lukem  * to only contain possible choices.
     80   1.1     lukem  * Args:
     81   1.1     lukem  *	word	word which started the match
     82   1.1     lukem  *	list	list by default
     83   1.1     lukem  *	words	stringlist containing possible matches
     84  1.13     lukem  * Returns a result as per el_set(EL_ADDFN, ...)
     85   1.1     lukem  */
     86   1.1     lukem static unsigned char
     87   1.1     lukem complete_ambiguous(word, list, words)
     88   1.1     lukem 	char *word;
     89   1.1     lukem 	int list;
     90   1.1     lukem 	StringList *words;
     91   1.1     lukem {
     92   1.3     lukem 	char insertstr[MAXPATHLEN];
     93   1.1     lukem 	char *lastmatch;
     94  1.11     lukem 	int i, j;
     95  1.11     lukem 	size_t matchlen, wordlen;
     96   1.1     lukem 
     97   1.1     lukem 	wordlen = strlen(word);
     98   1.1     lukem 	if (words->sl_cur == 0)
     99   1.6     lukem 		return (CC_ERROR);	/* no choices available */
    100   1.1     lukem 
    101   1.1     lukem 	if (words->sl_cur == 1) {	/* only once choice available */
    102  1.18     lukem 		char *p = words->sl_str[0] + wordlen;
    103  1.18     lukem 		ftpvis(insertstr, sizeof(insertstr), p, strlen(p));
    104  1.18     lukem 		if (el_insertstr(el, insertstr) == -1)
    105   1.6     lukem 			return (CC_ERROR);
    106   1.1     lukem 		else
    107   1.6     lukem 			return (CC_REFRESH);
    108   1.1     lukem 	}
    109   1.1     lukem 
    110   1.1     lukem 	if (!list) {
    111   1.1     lukem 		matchlen = 0;
    112   1.1     lukem 		lastmatch = words->sl_str[0];
    113   1.1     lukem 		matchlen = strlen(lastmatch);
    114   1.1     lukem 		for (i = 1 ; i < words->sl_cur ; i++) {
    115   1.1     lukem 			for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
    116   1.1     lukem 				if (lastmatch[j] != words->sl_str[i][j])
    117   1.1     lukem 					break;
    118   1.1     lukem 			if (j < matchlen)
    119   1.1     lukem 				matchlen = j;
    120   1.1     lukem 		}
    121   1.1     lukem 		if (matchlen > wordlen) {
    122  1.18     lukem 			ftpvis(insertstr, sizeof(insertstr),
    123  1.20     lukem 			    lastmatch + wordlen, matchlen - wordlen);
    124  1.18     lukem 			if (el_insertstr(el, insertstr) == -1)
    125   1.6     lukem 				return (CC_ERROR);
    126  1.21     lukem 			else
    127  1.13     lukem 				return (CC_REFRESH_BEEP);
    128   1.1     lukem 		}
    129   1.1     lukem 	}
    130   1.1     lukem 
    131  1.14     lukem 	putc('\n', ttyout);
    132   1.1     lukem 	qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
    133   1.1     lukem 	list_vertical(words);
    134   1.6     lukem 	return (CC_REDISPLAY);
    135   1.1     lukem }
    136   1.1     lukem 
    137   1.1     lukem /*
    138   1.1     lukem  * Complete a command
    139   1.1     lukem  */
    140   1.1     lukem static unsigned char
    141   1.1     lukem complete_command(word, list)
    142   1.1     lukem 	char *word;
    143   1.1     lukem 	int list;
    144   1.1     lukem {
    145   1.1     lukem 	struct cmd *c;
    146   1.1     lukem 	StringList *words;
    147  1.11     lukem 	size_t wordlen;
    148   1.1     lukem 	unsigned char rv;
    149   1.1     lukem 
    150   1.1     lukem 	words = sl_init();
    151   1.1     lukem 	wordlen = strlen(word);
    152   1.1     lukem 
    153   1.1     lukem 	for (c = cmdtab; c->c_name != NULL; c++) {
    154   1.1     lukem 		if (wordlen > strlen(c->c_name))
    155   1.1     lukem 			continue;
    156   1.1     lukem 		if (strncmp(word, c->c_name, wordlen) == 0)
    157   1.1     lukem 			sl_add(words, c->c_name);
    158   1.1     lukem 	}
    159   1.1     lukem 
    160   1.1     lukem 	rv = complete_ambiguous(word, list, words);
    161  1.13     lukem 	if (rv == CC_REFRESH) {
    162  1.13     lukem 		if (el_insertstr(el, " ") == -1)
    163  1.13     lukem 			rv = CC_ERROR;
    164  1.13     lukem 	}
    165   1.1     lukem 	sl_free(words, 0);
    166   1.6     lukem 	return (rv);
    167   1.1     lukem }
    168   1.1     lukem 
    169   1.1     lukem /*
    170   1.1     lukem  * Complete a local file
    171   1.1     lukem  */
    172   1.1     lukem static unsigned char
    173   1.1     lukem complete_local(word, list)
    174   1.1     lukem 	char *word;
    175   1.1     lukem 	int list;
    176   1.1     lukem {
    177   1.1     lukem 	StringList *words;
    178   1.3     lukem 	char dir[MAXPATHLEN];
    179   1.1     lukem 	char *file;
    180   1.1     lukem 	DIR *dd;
    181   1.1     lukem 	struct dirent *dp;
    182   1.1     lukem 	unsigned char rv;
    183  1.12  christos 	size_t len;
    184   1.1     lukem 
    185   1.1     lukem 	if ((file = strrchr(word, '/')) == NULL) {
    186   1.6     lukem 		dir[0] = '.';
    187   1.6     lukem 		dir[1] = '\0';
    188   1.1     lukem 		file = word;
    189   1.1     lukem 	} else {
    190   1.6     lukem 		if (file == word) {
    191   1.6     lukem 			dir[0] = '/';
    192   1.6     lukem 			dir[1] = '\0';
    193   1.6     lukem 		} else {
    194   1.6     lukem 			(void)strncpy(dir, word, file - word);
    195   1.1     lukem 			dir[file - word] = '\0';
    196   1.1     lukem 		}
    197   1.6     lukem 		file++;
    198  1.17     lukem 	}
    199  1.17     lukem 	if (dir[0] == '~') {
    200  1.17     lukem 		char *p;
    201  1.17     lukem 
    202  1.17     lukem 		p = dir;
    203  1.17     lukem 		if (!globulize(&p))
    204  1.17     lukem 			return (CC_ERROR);
    205  1.17     lukem 		if (p != dir) {
    206  1.17     lukem 			strncpy(dir, p, sizeof(dir));
    207  1.17     lukem 			dir[sizeof(dir)-1] = '\0';
    208  1.17     lukem 			free(p);
    209  1.17     lukem 		}
    210   1.1     lukem 	}
    211   1.1     lukem 
    212   1.1     lukem 	if ((dd = opendir(dir)) == NULL)
    213   1.6     lukem 		return (CC_ERROR);
    214   1.1     lukem 
    215   1.1     lukem 	words = sl_init();
    216   1.1     lukem 
    217  1.12  christos 	len = strlen(file);
    218  1.12  christos 
    219   1.1     lukem 	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
    220   1.1     lukem 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    221   1.1     lukem 			continue;
    222  1.21     lukem 
    223  1.22  christos #if defined(__SVR4) || defined(__linux__)
    224  1.23  christos 		if (len > strlen(dp->d_name))
    225  1.12  christos 			continue;
    226  1.12  christos #else
    227  1.23  christos 		if (len > dp->d_namlen)
    228   1.1     lukem 			continue;
    229  1.12  christos #endif
    230  1.12  christos 		if (strncmp(file, dp->d_name, len) == 0) {
    231   1.1     lukem 			char *tcp;
    232   1.1     lukem 
    233  1.15     lukem 			tcp = xstrdup(dp->d_name);
    234   1.1     lukem 			sl_add(words, tcp);
    235   1.1     lukem 		}
    236   1.1     lukem 	}
    237   1.1     lukem 	closedir(dd);
    238   1.1     lukem 
    239   1.1     lukem 	rv = complete_ambiguous(file, list, words);
    240  1.13     lukem 	if (rv == CC_REFRESH) {
    241  1.13     lukem 		struct stat sb;
    242  1.13     lukem 		char path[MAXPATHLEN];
    243  1.13     lukem 
    244  1.27     lukem 		strlcpy(path, dir,		sizeof(path));
    245  1.27     lukem 		strlcat(path, "/",		sizeof(path));
    246  1.27     lukem 		strlcat(path, words->sl_str[0],	sizeof(path));
    247  1.27     lukem 
    248  1.13     lukem 		if (stat(path, &sb) >= 0) {
    249  1.13     lukem 			char suffix[2] = " ";
    250  1.13     lukem 
    251  1.13     lukem 			if (S_ISDIR(sb.st_mode))
    252  1.13     lukem 				suffix[0] = '/';
    253  1.13     lukem 			if (el_insertstr(el, suffix) == -1)
    254  1.13     lukem 				rv = CC_ERROR;
    255  1.13     lukem 		}
    256  1.13     lukem 	}
    257   1.1     lukem 	sl_free(words, 1);
    258   1.6     lukem 	return (rv);
    259   1.1     lukem }
    260   1.1     lukem 
    261   1.1     lukem /*
    262   1.1     lukem  * Complete a remote file
    263   1.1     lukem  */
    264   1.1     lukem static unsigned char
    265   1.1     lukem complete_remote(word, list)
    266   1.1     lukem 	char *word;
    267   1.1     lukem 	int list;
    268   1.1     lukem {
    269   1.1     lukem 	static StringList *dirlist;
    270   1.3     lukem 	static char	 lastdir[MAXPATHLEN];
    271   1.1     lukem 	StringList	*words;
    272   1.3     lukem 	char		 dir[MAXPATHLEN];
    273   1.1     lukem 	char		*file, *cp;
    274   1.3     lukem 	int		 i;
    275   1.1     lukem 	unsigned char	 rv;
    276   1.1     lukem 
    277  1.16     lukem 	char *dummyargv[] = { "complete", NULL, NULL };
    278  1.16     lukem 	dummyargv[1] = dir;
    279   1.1     lukem 
    280   1.1     lukem 	if ((file = strrchr(word, '/')) == NULL) {
    281   1.6     lukem 		dir[0] = '.';
    282   1.6     lukem 		dir[1] = '\0';
    283   1.1     lukem 		file = word;
    284   1.1     lukem 	} else {
    285   1.3     lukem 		cp = file;
    286   1.3     lukem 		while (*cp == '/' && cp > word)
    287   1.3     lukem 			cp--;
    288   1.8     lukem 		(void)strncpy(dir, word, cp - word + 1);
    289   1.8     lukem 		dir[cp - word + 1] = '\0';
    290   1.1     lukem 		file++;
    291   1.1     lukem 	}
    292   1.1     lukem 
    293   1.1     lukem 	if (dirchange || strcmp(dir, lastdir) != 0) {	/* dir not cached */
    294   1.3     lukem 		char *emesg;
    295   1.3     lukem 
    296   1.1     lukem 		if (dirlist != NULL)
    297   1.1     lukem 			sl_free(dirlist, 1);
    298   1.1     lukem 		dirlist = sl_init();
    299   1.1     lukem 
    300   1.1     lukem 		mflag = 1;
    301   1.3     lukem 		emesg = NULL;
    302   1.3     lukem 		while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
    303   1.1     lukem 			char *tcp;
    304   1.1     lukem 
    305   1.1     lukem 			if (!mflag)
    306   1.1     lukem 				continue;
    307   1.1     lukem 			if (*cp == '\0') {
    308   1.1     lukem 				mflag = 0;
    309   1.1     lukem 				continue;
    310   1.1     lukem 			}
    311   1.8     lukem 			tcp = strrchr(cp, '/');
    312   1.8     lukem 			if (tcp)
    313   1.8     lukem 				tcp++;
    314   1.8     lukem 			else
    315   1.8     lukem 				tcp = cp;
    316  1.15     lukem 			tcp = xstrdup(tcp);
    317   1.1     lukem 			sl_add(dirlist, tcp);
    318   1.1     lukem 		}
    319   1.3     lukem 		if (emesg != NULL) {
    320  1.14     lukem 			fprintf(ttyout, "\n%s\n", emesg);
    321   1.6     lukem 			return (CC_REDISPLAY);
    322   1.3     lukem 		}
    323   1.6     lukem 		(void)strcpy(lastdir, dir);
    324   1.1     lukem 		dirchange = 0;
    325   1.1     lukem 	}
    326   1.1     lukem 
    327   1.1     lukem 	words = sl_init();
    328   1.1     lukem 	for (i = 0; i < dirlist->sl_cur; i++) {
    329   1.1     lukem 		cp = dirlist->sl_str[i];
    330   1.3     lukem 		if (strlen(file) > strlen(cp))
    331   1.1     lukem 			continue;
    332   1.3     lukem 		if (strncmp(file, cp, strlen(file)) == 0)
    333   1.3     lukem 			sl_add(words, cp);
    334   1.1     lukem 	}
    335   1.1     lukem 	rv = complete_ambiguous(file, list, words);
    336   1.1     lukem 	sl_free(words, 0);
    337   1.6     lukem 	return (rv);
    338   1.1     lukem }
    339   1.1     lukem 
    340   1.1     lukem /*
    341   1.1     lukem  * Generic complete routine
    342   1.1     lukem  */
    343   1.1     lukem unsigned char
    344   1.1     lukem complete(el, ch)
    345   1.1     lukem 	EditLine *el;
    346   1.1     lukem 	int ch;
    347   1.1     lukem {
    348   1.1     lukem 	static char word[FTPBUFLEN];
    349   1.1     lukem 	static int lastc_argc, lastc_argo;
    350   1.1     lukem 
    351   1.1     lukem 	struct cmd *c;
    352   1.1     lukem 	const LineInfo *lf;
    353  1.11     lukem 	int celems, dolist;
    354  1.11     lukem 	size_t len;
    355   1.1     lukem 
    356   1.1     lukem 	lf = el_line(el);
    357   1.1     lukem 	len = lf->lastchar - lf->buffer;
    358   1.1     lukem 	if (len >= sizeof(line))
    359   1.6     lukem 		return (CC_ERROR);
    360   1.6     lukem 	(void)strncpy(line, lf->buffer, len);
    361   1.1     lukem 	line[len] = '\0';
    362   1.1     lukem 	cursor_pos = line + (lf->cursor - lf->buffer);
    363   1.1     lukem 	lastc_argc = cursor_argc;	/* remember last cursor pos */
    364   1.1     lukem 	lastc_argo = cursor_argo;
    365   1.1     lukem 	makeargv();			/* build argc/argv of current line */
    366   1.1     lukem 
    367   1.1     lukem 	if (cursor_argo >= sizeof(word))
    368   1.6     lukem 		return (CC_ERROR);
    369   1.1     lukem 
    370   1.1     lukem 	dolist = 0;
    371   1.1     lukem 			/* if cursor and word is same, list alternatives */
    372   1.1     lukem 	if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
    373   1.1     lukem 	    && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
    374   1.1     lukem 		dolist = 1;
    375  1.28     lukem 	else if (cursor_argc < margc)
    376  1.19     lukem 		(void)strncpy(word, margv[cursor_argc], cursor_argo);
    377   1.1     lukem 	word[cursor_argo] = '\0';
    378   1.1     lukem 
    379   1.1     lukem 	if (cursor_argc == 0)
    380   1.6     lukem 		return (complete_command(word, dolist));
    381   1.1     lukem 
    382   1.1     lukem 	c = getcmd(margv[0]);
    383   1.1     lukem 	if (c == (struct cmd *)-1 || c == 0)
    384   1.6     lukem 		return (CC_ERROR);
    385   1.1     lukem 	celems = strlen(c->c_complete);
    386   1.1     lukem 
    387   1.1     lukem 		/* check for 'continuation' completes (which are uppercase) */
    388   1.1     lukem 	if ((cursor_argc > celems) && (celems > 0)
    389  1.12  christos 	    && isupper((unsigned char) c->c_complete[celems-1]))
    390   1.1     lukem 		cursor_argc = celems;
    391   1.1     lukem 
    392   1.1     lukem 	if (cursor_argc > celems)
    393   1.6     lukem 		return (CC_ERROR);
    394   1.1     lukem 
    395   1.1     lukem 	switch (c->c_complete[cursor_argc - 1]) {
    396   1.1     lukem 		case 'l':			/* local complete */
    397   1.1     lukem 		case 'L':
    398   1.6     lukem 			return (complete_local(word, dolist));
    399   1.1     lukem 		case 'r':			/* remote complete */
    400   1.1     lukem 		case 'R':
    401   1.7     lukem 			if (connected != -1) {
    402  1.14     lukem 				fputs("\nMust be logged in to complete.\n",
    403  1.14     lukem 				    ttyout);
    404   1.6     lukem 				return (CC_REDISPLAY);
    405   1.1     lukem 			}
    406   1.6     lukem 			return (complete_remote(word, dolist));
    407   1.1     lukem 		case 'c':			/* command complete */
    408   1.1     lukem 		case 'C':
    409   1.6     lukem 			return (complete_command(word, dolist));
    410   1.1     lukem 		case 'n':			/* no complete */
    411   1.1     lukem 		default:
    412   1.6     lukem 			return (CC_ERROR);
    413   1.1     lukem 	}
    414   1.1     lukem 
    415   1.6     lukem 	return (CC_ERROR);
    416   1.1     lukem }
    417   1.9     lukem 
    418  1.24       cgd #endif /* !NO_EDITCOMPLETE */
    419