Home | History | Annotate | Line # | Download | only in ftp
complete.c revision 1.12
      1 /*	$NetBSD: complete.c,v 1.12 1998/05/20 00:53:57 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 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 #ifndef SMALL
     40 
     41 #include <sys/cdefs.h>
     42 #ifndef lint
     43 __RCSID("$NetBSD: complete.c,v 1.12 1998/05/20 00:53:57 christos Exp $");
     44 #endif /* not lint */
     45 
     46 /*
     47  * FTP user program - command and file completion routines
     48  */
     49 
     50 #include <ctype.h>
     51 #include <err.h>
     52 #include <dirent.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 
     57 #include "ftp_var.h"
     58 
     59 static int	     comparstr		__P((const void *, const void *));
     60 static unsigned char complete_ambiguous	__P((char *, int, StringList *));
     61 static unsigned char complete_command	__P((char *, int));
     62 static unsigned char complete_local	__P((char *, int));
     63 static unsigned char complete_remote	__P((char *, int));
     64 
     65 static int
     66 comparstr(a, b)
     67 	const void *a, *b;
     68 {
     69 	return (strcmp(*(char **)a, *(char **)b));
     70 }
     71 
     72 /*
     73  * Determine if complete is ambiguous. If unique, insert.
     74  * If no choices, error. If unambiguous prefix, insert that.
     75  * Otherwise, list choices. words is assumed to be filtered
     76  * to only contain possible choices.
     77  * Args:
     78  *	word	word which started the match
     79  *	list	list by default
     80  *	words	stringlist containing possible matches
     81  */
     82 static unsigned char
     83 complete_ambiguous(word, list, words)
     84 	char *word;
     85 	int list;
     86 	StringList *words;
     87 {
     88 	char insertstr[MAXPATHLEN];
     89 	char *lastmatch;
     90 	int i, j;
     91 	size_t matchlen, wordlen;
     92 
     93 	wordlen = strlen(word);
     94 	if (words->sl_cur == 0)
     95 		return (CC_ERROR);	/* no choices available */
     96 
     97 	if (words->sl_cur == 1) {	/* only once choice available */
     98 		(void)strcpy(insertstr, words->sl_str[0]);
     99 		if (el_insertstr(el, insertstr + wordlen) == -1)
    100 			return (CC_ERROR);
    101 		else
    102 			return (CC_REFRESH);
    103 	}
    104 
    105 	if (!list) {
    106 		matchlen = 0;
    107 		lastmatch = words->sl_str[0];
    108 		matchlen = strlen(lastmatch);
    109 		for (i = 1 ; i < words->sl_cur ; i++) {
    110 			for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
    111 				if (lastmatch[j] != words->sl_str[i][j])
    112 					break;
    113 			if (j < matchlen)
    114 				matchlen = j;
    115 		}
    116 		if (matchlen > wordlen) {
    117 			(void)strncpy(insertstr, lastmatch, matchlen);
    118 			insertstr[matchlen] = '\0';
    119 			if (el_insertstr(el, insertstr + wordlen) == -1)
    120 				return (CC_ERROR);
    121 			else
    122 					/*
    123 					 * XXX: really want CC_REFRESH_BEEP
    124 					 */
    125 				return (CC_REFRESH);
    126 		}
    127 	}
    128 
    129 	putchar('\n');
    130 	qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
    131 	list_vertical(words);
    132 	return (CC_REDISPLAY);
    133 }
    134 
    135 /*
    136  * Complete a command
    137  */
    138 static unsigned char
    139 complete_command(word, list)
    140 	char *word;
    141 	int list;
    142 {
    143 	struct cmd *c;
    144 	StringList *words;
    145 	size_t wordlen;
    146 	unsigned char rv;
    147 
    148 	words = sl_init();
    149 	wordlen = strlen(word);
    150 
    151 	for (c = cmdtab; c->c_name != NULL; c++) {
    152 		if (wordlen > strlen(c->c_name))
    153 			continue;
    154 		if (strncmp(word, c->c_name, wordlen) == 0)
    155 			sl_add(words, c->c_name);
    156 	}
    157 
    158 	rv = complete_ambiguous(word, list, words);
    159 	sl_free(words, 0);
    160 	return (rv);
    161 }
    162 
    163 /*
    164  * Complete a local file
    165  */
    166 static unsigned char
    167 complete_local(word, list)
    168 	char *word;
    169 	int list;
    170 {
    171 	StringList *words;
    172 	char dir[MAXPATHLEN];
    173 	char *file;
    174 	DIR *dd;
    175 	struct dirent *dp;
    176 	unsigned char rv;
    177 	size_t len;
    178 
    179 	if ((file = strrchr(word, '/')) == NULL) {
    180 		dir[0] = '.';
    181 		dir[1] = '\0';
    182 		file = word;
    183 	} else {
    184 		if (file == word) {
    185 			dir[0] = '/';
    186 			dir[1] = '\0';
    187 		} else {
    188 			(void)strncpy(dir, word, file - word);
    189 			dir[file - word] = '\0';
    190 		}
    191 		file++;
    192 	}
    193 
    194 	if ((dd = opendir(dir)) == NULL)
    195 		return (CC_ERROR);
    196 
    197 	words = sl_init();
    198 
    199 	len = strlen(file);
    200 
    201 	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
    202 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    203 			continue;
    204 
    205 #ifndef __SVR4
    206 		if (len > dp->d_namlen)
    207 			continue;
    208 #else
    209 		if (len > strlen(dp->d_name))
    210 			continue;
    211 #endif
    212 		if (strncmp(file, dp->d_name, len) == 0) {
    213 			char *tcp;
    214 
    215 			tcp = strdup(dp->d_name);
    216 			if (tcp == NULL)
    217 				errx(1, "Can't allocate memory for local dir");
    218 			sl_add(words, tcp);
    219 		}
    220 	}
    221 	closedir(dd);
    222 
    223 	rv = complete_ambiguous(file, list, words);
    224 	sl_free(words, 1);
    225 	return (rv);
    226 }
    227 
    228 /*
    229  * Complete a remote file
    230  */
    231 static unsigned char
    232 complete_remote(word, list)
    233 	char *word;
    234 	int list;
    235 {
    236 	static StringList *dirlist;
    237 	static char	 lastdir[MAXPATHLEN];
    238 	StringList	*words;
    239 	char		 dir[MAXPATHLEN];
    240 	char		*file, *cp;
    241 	int		 i;
    242 	unsigned char	 rv;
    243 
    244 	char *dummyargv[] = { "complete", dir, NULL };
    245 
    246 	if ((file = strrchr(word, '/')) == NULL) {
    247 		dir[0] = '.';
    248 		dir[1] = '\0';
    249 		file = word;
    250 	} else {
    251 		cp = file;
    252 		while (*cp == '/' && cp > word)
    253 			cp--;
    254 		(void)strncpy(dir, word, cp - word + 1);
    255 		dir[cp - word + 1] = '\0';
    256 		file++;
    257 	}
    258 
    259 	if (dirchange || strcmp(dir, lastdir) != 0) {	/* dir not cached */
    260 		char *emesg;
    261 
    262 		if (dirlist != NULL)
    263 			sl_free(dirlist, 1);
    264 		dirlist = sl_init();
    265 
    266 		mflag = 1;
    267 		emesg = NULL;
    268 		while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
    269 			char *tcp;
    270 
    271 			if (!mflag)
    272 				continue;
    273 			if (*cp == '\0') {
    274 				mflag = 0;
    275 				continue;
    276 			}
    277 			tcp = strrchr(cp, '/');
    278 			if (tcp)
    279 				tcp++;
    280 			else
    281 				tcp = cp;
    282 			tcp = strdup(tcp);
    283 			if (tcp == NULL)
    284 				errx(1, "Can't allocate memory for remote dir");
    285 			sl_add(dirlist, tcp);
    286 		}
    287 		if (emesg != NULL) {
    288 			printf("\n%s\n", emesg);
    289 			return (CC_REDISPLAY);
    290 		}
    291 		(void)strcpy(lastdir, dir);
    292 		dirchange = 0;
    293 	}
    294 
    295 	words = sl_init();
    296 	for (i = 0; i < dirlist->sl_cur; i++) {
    297 		cp = dirlist->sl_str[i];
    298 		if (strlen(file) > strlen(cp))
    299 			continue;
    300 		if (strncmp(file, cp, strlen(file)) == 0)
    301 			sl_add(words, cp);
    302 	}
    303 	rv = complete_ambiguous(file, list, words);
    304 	sl_free(words, 0);
    305 	return (rv);
    306 }
    307 
    308 /*
    309  * Generic complete routine
    310  */
    311 unsigned char
    312 complete(el, ch)
    313 	EditLine *el;
    314 	int ch;
    315 {
    316 	static char word[FTPBUFLEN];
    317 	static int lastc_argc, lastc_argo;
    318 
    319 	struct cmd *c;
    320 	const LineInfo *lf;
    321 	int celems, dolist;
    322 	size_t len;
    323 
    324 	lf = el_line(el);
    325 	len = lf->lastchar - lf->buffer;
    326 	if (len >= sizeof(line))
    327 		return (CC_ERROR);
    328 	(void)strncpy(line, lf->buffer, len);
    329 	line[len] = '\0';
    330 	cursor_pos = line + (lf->cursor - lf->buffer);
    331 	lastc_argc = cursor_argc;	/* remember last cursor pos */
    332 	lastc_argo = cursor_argo;
    333 	makeargv();			/* build argc/argv of current line */
    334 
    335 	if (cursor_argo >= sizeof(word))
    336 		return (CC_ERROR);
    337 
    338 	dolist = 0;
    339 			/* if cursor and word is same, list alternatives */
    340 	if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
    341 	    && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
    342 		dolist = 1;
    343 	else
    344 	    (void)strncpy(word, margv[cursor_argc], cursor_argo);
    345 	word[cursor_argo] = '\0';
    346 
    347 	if (cursor_argc == 0)
    348 		return (complete_command(word, dolist));
    349 
    350 	c = getcmd(margv[0]);
    351 	if (c == (struct cmd *)-1 || c == 0)
    352 		return (CC_ERROR);
    353 	celems = strlen(c->c_complete);
    354 
    355 		/* check for 'continuation' completes (which are uppercase) */
    356 	if ((cursor_argc > celems) && (celems > 0)
    357 	    && isupper((unsigned char) c->c_complete[celems-1]))
    358 		cursor_argc = celems;
    359 
    360 	if (cursor_argc > celems)
    361 		return (CC_ERROR);
    362 
    363 	switch (c->c_complete[cursor_argc - 1]) {
    364 		case 'l':			/* local complete */
    365 		case 'L':
    366 			return (complete_local(word, dolist));
    367 		case 'r':			/* remote complete */
    368 		case 'R':
    369 			if (connected != -1) {
    370 				puts("\nMust be logged in to complete.");
    371 				return (CC_REDISPLAY);
    372 			}
    373 			return (complete_remote(word, dolist));
    374 		case 'c':			/* command complete */
    375 		case 'C':
    376 			return (complete_command(word, dolist));
    377 		case 'n':			/* no complete */
    378 		default:
    379 			return (CC_ERROR);
    380 	}
    381 
    382 	return (CC_ERROR);
    383 }
    384 
    385 #endif /* !SMALL */
    386