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