Home | History | Annotate | Line # | Download | only in ftp
complete.c revision 1.38
      1  1.38     lukem /*	$NetBSD: complete.c,v 1.38 2000/05/01 10:35:17 lukem Exp $	*/
      2   1.1     lukem 
      3   1.1     lukem /*-
      4  1.38     lukem  * Copyright (c) 1997-2000 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.33     lukem  *	This product includes software developed by the NetBSD
     21  1.33     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.38     lukem __RCSID("$NetBSD: complete.c,v 1.38 2000/05/01 10:35:17 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.13     lukem 
     50   1.1     lukem #include <ctype.h>
     51   1.1     lukem #include <err.h>
     52   1.1     lukem #include <dirent.h>
     53   1.1     lukem #include <stdio.h>
     54   1.1     lukem #include <stdlib.h>
     55   1.1     lukem #include <string.h>
     56   1.1     lukem 
     57   1.1     lukem #include "ftp_var.h"
     58   1.1     lukem 
     59  1.24       cgd #ifndef NO_EDITCOMPLETE
     60  1.24       cgd 
     61  1.38     lukem static int	     comparstr		(const void *, const void *);
     62  1.38     lukem static unsigned char complete_ambiguous	(char *, int, StringList *);
     63  1.38     lukem static unsigned char complete_command	(char *, int);
     64  1.38     lukem static unsigned char complete_local	(char *, int);
     65  1.38     lukem static unsigned char complete_option	(char *, int);
     66  1.38     lukem static unsigned char complete_remote	(char *, int);
     67   1.9     lukem 
     68   1.1     lukem static int
     69  1.38     lukem comparstr(const void *a, const void *b)
     70   1.1     lukem {
     71  1.26  christos 	return (strcmp(*(const char **)a, *(const char **)b));
     72   1.1     lukem }
     73   1.1     lukem 
     74   1.1     lukem /*
     75   1.1     lukem  * Determine if complete is ambiguous. If unique, insert.
     76   1.1     lukem  * If no choices, error. If unambiguous prefix, insert that.
     77   1.1     lukem  * Otherwise, list choices. words is assumed to be filtered
     78   1.1     lukem  * to only contain possible choices.
     79   1.1     lukem  * Args:
     80   1.1     lukem  *	word	word which started the match
     81   1.1     lukem  *	list	list by default
     82   1.1     lukem  *	words	stringlist containing possible matches
     83  1.13     lukem  * Returns a result as per el_set(EL_ADDFN, ...)
     84   1.1     lukem  */
     85   1.1     lukem static unsigned char
     86  1.38     lukem complete_ambiguous(char *word, int list, StringList *words)
     87   1.1     lukem {
     88   1.3     lukem 	char insertstr[MAXPATHLEN];
     89  1.37     lukem 	char *lastmatch, *p;
     90  1.11     lukem 	int i, j;
     91  1.11     lukem 	size_t matchlen, wordlen;
     92   1.1     lukem 
     93   1.1     lukem 	wordlen = strlen(word);
     94   1.1     lukem 	if (words->sl_cur == 0)
     95   1.6     lukem 		return (CC_ERROR);	/* no choices available */
     96   1.1     lukem 
     97   1.1     lukem 	if (words->sl_cur == 1) {	/* only once choice available */
     98  1.37     lukem 		p = words->sl_str[0] + wordlen;
     99  1.37     lukem 		if (*p == '\0')		/* at end of word? */
    100  1.37     lukem 			return (CC_REFRESH);
    101  1.18     lukem 		ftpvis(insertstr, sizeof(insertstr), p, strlen(p));
    102  1.18     lukem 		if (el_insertstr(el, insertstr) == -1)
    103   1.6     lukem 			return (CC_ERROR);
    104   1.1     lukem 		else
    105   1.6     lukem 			return (CC_REFRESH);
    106   1.1     lukem 	}
    107   1.1     lukem 
    108   1.1     lukem 	if (!list) {
    109   1.1     lukem 		matchlen = 0;
    110   1.1     lukem 		lastmatch = words->sl_str[0];
    111   1.1     lukem 		matchlen = strlen(lastmatch);
    112   1.1     lukem 		for (i = 1 ; i < words->sl_cur ; i++) {
    113   1.1     lukem 			for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
    114   1.1     lukem 				if (lastmatch[j] != words->sl_str[i][j])
    115   1.1     lukem 					break;
    116   1.1     lukem 			if (j < matchlen)
    117   1.1     lukem 				matchlen = j;
    118   1.1     lukem 		}
    119   1.1     lukem 		if (matchlen > wordlen) {
    120  1.18     lukem 			ftpvis(insertstr, sizeof(insertstr),
    121  1.20     lukem 			    lastmatch + wordlen, matchlen - wordlen);
    122  1.18     lukem 			if (el_insertstr(el, insertstr) == -1)
    123   1.6     lukem 				return (CC_ERROR);
    124  1.21     lukem 			else
    125  1.13     lukem 				return (CC_REFRESH_BEEP);
    126   1.1     lukem 		}
    127   1.1     lukem 	}
    128   1.1     lukem 
    129  1.14     lukem 	putc('\n', ttyout);
    130   1.1     lukem 	qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
    131   1.1     lukem 	list_vertical(words);
    132   1.6     lukem 	return (CC_REDISPLAY);
    133   1.1     lukem }
    134   1.1     lukem 
    135   1.1     lukem /*
    136   1.1     lukem  * Complete a command
    137   1.1     lukem  */
    138   1.1     lukem static unsigned char
    139  1.38     lukem complete_command(char *word, int list)
    140   1.1     lukem {
    141   1.1     lukem 	struct cmd *c;
    142   1.1     lukem 	StringList *words;
    143  1.11     lukem 	size_t wordlen;
    144   1.1     lukem 	unsigned char rv;
    145   1.1     lukem 
    146  1.36     lukem 	words = xsl_init();
    147   1.1     lukem 	wordlen = strlen(word);
    148   1.1     lukem 
    149   1.1     lukem 	for (c = cmdtab; c->c_name != NULL; c++) {
    150   1.1     lukem 		if (wordlen > strlen(c->c_name))
    151   1.1     lukem 			continue;
    152   1.1     lukem 		if (strncmp(word, c->c_name, wordlen) == 0)
    153  1.36     lukem 			xsl_add(words, c->c_name);
    154   1.1     lukem 	}
    155   1.1     lukem 
    156   1.1     lukem 	rv = complete_ambiguous(word, list, words);
    157  1.13     lukem 	if (rv == CC_REFRESH) {
    158  1.13     lukem 		if (el_insertstr(el, " ") == -1)
    159  1.13     lukem 			rv = CC_ERROR;
    160  1.13     lukem 	}
    161   1.1     lukem 	sl_free(words, 0);
    162   1.6     lukem 	return (rv);
    163   1.1     lukem }
    164   1.1     lukem 
    165   1.1     lukem /*
    166   1.1     lukem  * Complete a local file
    167   1.1     lukem  */
    168   1.1     lukem static unsigned char
    169  1.38     lukem complete_local(char *word, int list)
    170   1.1     lukem {
    171   1.1     lukem 	StringList *words;
    172   1.3     lukem 	char dir[MAXPATHLEN];
    173   1.1     lukem 	char *file;
    174   1.1     lukem 	DIR *dd;
    175   1.1     lukem 	struct dirent *dp;
    176   1.1     lukem 	unsigned char rv;
    177  1.12  christos 	size_t len;
    178   1.1     lukem 
    179   1.1     lukem 	if ((file = strrchr(word, '/')) == NULL) {
    180   1.6     lukem 		dir[0] = '.';
    181   1.6     lukem 		dir[1] = '\0';
    182   1.1     lukem 		file = word;
    183   1.1     lukem 	} else {
    184   1.6     lukem 		if (file == word) {
    185   1.6     lukem 			dir[0] = '/';
    186   1.6     lukem 			dir[1] = '\0';
    187  1.32     lukem 		} else
    188  1.32     lukem 			(void)strlcpy(dir, word, file - word + 1);
    189   1.6     lukem 		file++;
    190  1.17     lukem 	}
    191  1.17     lukem 	if (dir[0] == '~') {
    192  1.17     lukem 		char *p;
    193  1.17     lukem 
    194  1.32     lukem 		if ((p = globulize(dir)) == NULL)
    195  1.17     lukem 			return (CC_ERROR);
    196  1.32     lukem 		(void)strlcpy(dir, p, sizeof(dir));
    197  1.32     lukem 		free(p);
    198   1.1     lukem 	}
    199   1.1     lukem 
    200   1.1     lukem 	if ((dd = opendir(dir)) == NULL)
    201   1.6     lukem 		return (CC_ERROR);
    202   1.1     lukem 
    203  1.36     lukem 	words = xsl_init();
    204  1.12  christos 	len = strlen(file);
    205  1.12  christos 
    206   1.1     lukem 	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
    207   1.1     lukem 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    208   1.1     lukem 			continue;
    209  1.21     lukem 
    210  1.30     lukem #if defined(DIRENT_MISSING_D_NAMLEN)
    211  1.23  christos 		if (len > strlen(dp->d_name))
    212  1.12  christos 			continue;
    213  1.12  christos #else
    214  1.23  christos 		if (len > dp->d_namlen)
    215   1.1     lukem 			continue;
    216  1.12  christos #endif
    217  1.12  christos 		if (strncmp(file, dp->d_name, len) == 0) {
    218   1.1     lukem 			char *tcp;
    219   1.1     lukem 
    220  1.15     lukem 			tcp = xstrdup(dp->d_name);
    221  1.36     lukem 			xsl_add(words, tcp);
    222   1.1     lukem 		}
    223   1.1     lukem 	}
    224   1.1     lukem 	closedir(dd);
    225   1.1     lukem 
    226   1.1     lukem 	rv = complete_ambiguous(file, list, words);
    227  1.13     lukem 	if (rv == CC_REFRESH) {
    228  1.13     lukem 		struct stat sb;
    229  1.13     lukem 		char path[MAXPATHLEN];
    230  1.13     lukem 
    231  1.32     lukem 		(void)strlcpy(path, dir,		sizeof(path));
    232  1.32     lukem 		(void)strlcat(path, "/",		sizeof(path));
    233  1.32     lukem 		(void)strlcat(path, words->sl_str[0],	sizeof(path));
    234  1.27     lukem 
    235  1.13     lukem 		if (stat(path, &sb) >= 0) {
    236  1.13     lukem 			char suffix[2] = " ";
    237  1.13     lukem 
    238  1.13     lukem 			if (S_ISDIR(sb.st_mode))
    239  1.13     lukem 				suffix[0] = '/';
    240  1.13     lukem 			if (el_insertstr(el, suffix) == -1)
    241  1.13     lukem 				rv = CC_ERROR;
    242  1.13     lukem 		}
    243  1.13     lukem 	}
    244   1.1     lukem 	sl_free(words, 1);
    245   1.6     lukem 	return (rv);
    246   1.1     lukem }
    247  1.34     lukem /*
    248  1.34     lukem  * Complete an option
    249  1.34     lukem  */
    250  1.34     lukem static unsigned char
    251  1.38     lukem complete_option(char *word, int list)
    252  1.34     lukem {
    253  1.34     lukem 	struct option *o;
    254  1.34     lukem 	StringList *words;
    255  1.34     lukem 	size_t wordlen;
    256  1.34     lukem 	unsigned char rv;
    257  1.34     lukem 
    258  1.36     lukem 	words = xsl_init();
    259  1.34     lukem 	wordlen = strlen(word);
    260  1.34     lukem 
    261  1.34     lukem 	for (o = optiontab; o->name != NULL; o++) {
    262  1.34     lukem 		if (wordlen > strlen(o->name))
    263  1.34     lukem 			continue;
    264  1.34     lukem 		if (strncmp(word, o->name, wordlen) == 0)
    265  1.36     lukem 			xsl_add(words, o->name);
    266  1.34     lukem 	}
    267  1.34     lukem 
    268  1.34     lukem 	rv = complete_ambiguous(word, list, words);
    269  1.34     lukem 	if (rv == CC_REFRESH) {
    270  1.34     lukem 		if (el_insertstr(el, " ") == -1)
    271  1.34     lukem 			rv = CC_ERROR;
    272  1.34     lukem 	}
    273  1.34     lukem 	sl_free(words, 0);
    274  1.34     lukem 	return (rv);
    275  1.34     lukem }
    276   1.1     lukem 
    277   1.1     lukem /*
    278   1.1     lukem  * Complete a remote file
    279   1.1     lukem  */
    280   1.1     lukem static unsigned char
    281  1.38     lukem complete_remote(char *word, int list)
    282   1.1     lukem {
    283   1.1     lukem 	static StringList *dirlist;
    284   1.3     lukem 	static char	 lastdir[MAXPATHLEN];
    285   1.1     lukem 	StringList	*words;
    286   1.3     lukem 	char		 dir[MAXPATHLEN];
    287   1.1     lukem 	char		*file, *cp;
    288   1.3     lukem 	int		 i;
    289   1.1     lukem 	unsigned char	 rv;
    290   1.1     lukem 
    291  1.16     lukem 	char *dummyargv[] = { "complete", NULL, NULL };
    292  1.16     lukem 	dummyargv[1] = dir;
    293   1.1     lukem 
    294   1.1     lukem 	if ((file = strrchr(word, '/')) == NULL) {
    295  1.35     lukem 		dir[0] = '\0';
    296   1.1     lukem 		file = word;
    297   1.1     lukem 	} else {
    298   1.3     lukem 		cp = file;
    299   1.3     lukem 		while (*cp == '/' && cp > word)
    300   1.3     lukem 			cp--;
    301  1.32     lukem 		(void)strlcpy(dir, word, cp - word + 2);
    302   1.1     lukem 		file++;
    303   1.1     lukem 	}
    304   1.1     lukem 
    305  1.35     lukem 	if (dirchange || dirlist == NULL ||
    306  1.35     lukem 	    strcmp(dir, lastdir) != 0) {		/* dir not cached */
    307   1.3     lukem 		char *emesg;
    308   1.3     lukem 
    309   1.1     lukem 		if (dirlist != NULL)
    310   1.1     lukem 			sl_free(dirlist, 1);
    311  1.36     lukem 		dirlist = xsl_init();
    312   1.1     lukem 
    313   1.1     lukem 		mflag = 1;
    314   1.3     lukem 		emesg = NULL;
    315   1.3     lukem 		while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
    316   1.1     lukem 			char *tcp;
    317   1.1     lukem 
    318   1.1     lukem 			if (!mflag)
    319   1.1     lukem 				continue;
    320   1.1     lukem 			if (*cp == '\0') {
    321   1.1     lukem 				mflag = 0;
    322   1.1     lukem 				continue;
    323   1.1     lukem 			}
    324   1.8     lukem 			tcp = strrchr(cp, '/');
    325   1.8     lukem 			if (tcp)
    326   1.8     lukem 				tcp++;
    327   1.8     lukem 			else
    328   1.8     lukem 				tcp = cp;
    329  1.15     lukem 			tcp = xstrdup(tcp);
    330  1.36     lukem 			xsl_add(dirlist, tcp);
    331   1.1     lukem 		}
    332   1.3     lukem 		if (emesg != NULL) {
    333  1.14     lukem 			fprintf(ttyout, "\n%s\n", emesg);
    334   1.6     lukem 			return (CC_REDISPLAY);
    335   1.3     lukem 		}
    336  1.32     lukem 		(void)strlcpy(lastdir, dir, sizeof(lastdir));
    337   1.1     lukem 		dirchange = 0;
    338   1.1     lukem 	}
    339   1.1     lukem 
    340  1.36     lukem 	words = xsl_init();
    341   1.1     lukem 	for (i = 0; i < dirlist->sl_cur; i++) {
    342   1.1     lukem 		cp = dirlist->sl_str[i];
    343   1.3     lukem 		if (strlen(file) > strlen(cp))
    344   1.1     lukem 			continue;
    345   1.3     lukem 		if (strncmp(file, cp, strlen(file)) == 0)
    346  1.36     lukem 			xsl_add(words, cp);
    347   1.1     lukem 	}
    348   1.1     lukem 	rv = complete_ambiguous(file, list, words);
    349   1.1     lukem 	sl_free(words, 0);
    350   1.6     lukem 	return (rv);
    351   1.1     lukem }
    352   1.1     lukem 
    353   1.1     lukem /*
    354   1.1     lukem  * Generic complete routine
    355   1.1     lukem  */
    356   1.1     lukem unsigned char
    357  1.38     lukem complete(EditLine *el, int ch)
    358   1.1     lukem {
    359   1.1     lukem 	static char word[FTPBUFLEN];
    360   1.1     lukem 	static int lastc_argc, lastc_argo;
    361   1.1     lukem 
    362   1.1     lukem 	struct cmd *c;
    363   1.1     lukem 	const LineInfo *lf;
    364  1.34     lukem 	int celems, dolist, cmpltype;
    365  1.11     lukem 	size_t len;
    366   1.1     lukem 
    367   1.1     lukem 	lf = el_line(el);
    368   1.1     lukem 	len = lf->lastchar - lf->buffer;
    369   1.1     lukem 	if (len >= sizeof(line))
    370   1.6     lukem 		return (CC_ERROR);
    371  1.32     lukem 	(void)strlcpy(line, lf->buffer, len + 1);
    372   1.1     lukem 	cursor_pos = line + (lf->cursor - lf->buffer);
    373   1.1     lukem 	lastc_argc = cursor_argc;	/* remember last cursor pos */
    374   1.1     lukem 	lastc_argo = cursor_argo;
    375   1.1     lukem 	makeargv();			/* build argc/argv of current line */
    376   1.1     lukem 
    377   1.1     lukem 	if (cursor_argo >= sizeof(word))
    378   1.6     lukem 		return (CC_ERROR);
    379   1.1     lukem 
    380   1.1     lukem 	dolist = 0;
    381   1.1     lukem 			/* if cursor and word is same, list alternatives */
    382   1.1     lukem 	if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
    383  1.36     lukem 	    && strncmp(word, margv[cursor_argc] ? margv[cursor_argc] : "",
    384  1.36     lukem 			cursor_argo) == 0)
    385   1.1     lukem 		dolist = 1;
    386  1.28     lukem 	else if (cursor_argc < margc)
    387  1.32     lukem 		(void)strlcpy(word, margv[cursor_argc], cursor_argo + 1);
    388   1.1     lukem 	word[cursor_argo] = '\0';
    389   1.1     lukem 
    390   1.1     lukem 	if (cursor_argc == 0)
    391   1.6     lukem 		return (complete_command(word, dolist));
    392   1.1     lukem 
    393   1.1     lukem 	c = getcmd(margv[0]);
    394   1.1     lukem 	if (c == (struct cmd *)-1 || c == 0)
    395   1.6     lukem 		return (CC_ERROR);
    396   1.1     lukem 	celems = strlen(c->c_complete);
    397   1.1     lukem 
    398   1.1     lukem 		/* check for 'continuation' completes (which are uppercase) */
    399   1.1     lukem 	if ((cursor_argc > celems) && (celems > 0)
    400  1.12  christos 	    && isupper((unsigned char) c->c_complete[celems-1]))
    401   1.1     lukem 		cursor_argc = celems;
    402   1.1     lukem 
    403   1.1     lukem 	if (cursor_argc > celems)
    404   1.6     lukem 		return (CC_ERROR);
    405   1.1     lukem 
    406  1.34     lukem 	cmpltype = c->c_complete[cursor_argc - 1];
    407  1.34     lukem 	switch (cmpltype) {
    408  1.34     lukem 		case 'c':			/* command complete */
    409  1.34     lukem 		case 'C':
    410  1.34     lukem 			return (complete_command(word, dolist));
    411   1.1     lukem 		case 'l':			/* local complete */
    412   1.1     lukem 		case 'L':
    413   1.6     lukem 			return (complete_local(word, dolist));
    414  1.34     lukem 		case 'n':			/* no complete */
    415  1.34     lukem 		case 'N':			/* no complete */
    416  1.34     lukem 			return (CC_ERROR);
    417  1.34     lukem 		case 'o':			/* local complete */
    418  1.34     lukem 		case 'O':
    419  1.34     lukem 			return (complete_option(word, dolist));
    420   1.1     lukem 		case 'r':			/* remote complete */
    421   1.1     lukem 		case 'R':
    422   1.7     lukem 			if (connected != -1) {
    423  1.14     lukem 				fputs("\nMust be logged in to complete.\n",
    424  1.14     lukem 				    ttyout);
    425   1.6     lukem 				return (CC_REDISPLAY);
    426   1.1     lukem 			}
    427   1.6     lukem 			return (complete_remote(word, dolist));
    428   1.1     lukem 		default:
    429  1.34     lukem 			errx(1, "unknown complete type `%c'", cmpltype);
    430   1.6     lukem 			return (CC_ERROR);
    431   1.1     lukem 	}
    432  1.29     lukem 	/* NOTREACHED */
    433   1.1     lukem }
    434   1.9     lukem 
    435  1.24       cgd #endif /* !NO_EDITCOMPLETE */
    436