Home | History | Annotate | Line # | Download | only in ftp
complete.c revision 1.9
      1 /*	$NetBSD: complete.c,v 1.9 1997/07/20 09:45:43 lukem 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 REGENTS OR CONTRIBUTORS BE
     30  * 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.9 1997/07/20 09:45:43 lukem 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, matchlen, wordlen;
     91 
     92 	wordlen = strlen(word);
     93 	if (words->sl_cur == 0)
     94 		return (CC_ERROR);	/* no choices available */
     95 
     96 	if (words->sl_cur == 1) {	/* only once choice available */
     97 		(void)strcpy(insertstr, words->sl_str[0]);
     98 		if (el_insertstr(el, insertstr + wordlen) == -1)
     99 			return (CC_ERROR);
    100 		else
    101 			return (CC_REFRESH);
    102 	}
    103 
    104 	if (!list) {
    105 		matchlen = 0;
    106 		lastmatch = words->sl_str[0];
    107 		matchlen = strlen(lastmatch);
    108 		for (i = 1 ; i < words->sl_cur ; i++) {
    109 			for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
    110 				if (lastmatch[j] != words->sl_str[i][j])
    111 					break;
    112 			if (j < matchlen)
    113 				matchlen = j;
    114 		}
    115 		if (matchlen > wordlen) {
    116 			(void)strncpy(insertstr, lastmatch, matchlen);
    117 			insertstr[matchlen] = '\0';
    118 			if (el_insertstr(el, insertstr + wordlen) == -1)
    119 				return (CC_ERROR);
    120 			else
    121 					/*
    122 					 * XXX: really want CC_REFRESH_BEEP
    123 					 */
    124 				return (CC_REFRESH);
    125 		}
    126 	}
    127 
    128 	putchar('\n');
    129 	qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
    130 	list_vertical(words);
    131 	return (CC_REDISPLAY);
    132 }
    133 
    134 /*
    135  * Complete a command
    136  */
    137 static unsigned char
    138 complete_command(word, list)
    139 	char *word;
    140 	int list;
    141 {
    142 	struct cmd *c;
    143 	StringList *words;
    144 	int wordlen;
    145 	unsigned char rv;
    146 
    147 	words = sl_init();
    148 	wordlen = strlen(word);
    149 
    150 	for (c = cmdtab; c->c_name != NULL; c++) {
    151 		if (wordlen > strlen(c->c_name))
    152 			continue;
    153 		if (strncmp(word, c->c_name, wordlen) == 0)
    154 			sl_add(words, c->c_name);
    155 	}
    156 
    157 	rv = complete_ambiguous(word, list, words);
    158 	sl_free(words, 0);
    159 	return (rv);
    160 }
    161 
    162 /*
    163  * Complete a local file
    164  */
    165 static unsigned char
    166 complete_local(word, list)
    167 	char *word;
    168 	int list;
    169 {
    170 	StringList *words;
    171 	char dir[MAXPATHLEN];
    172 	char *file;
    173 	DIR *dd;
    174 	struct dirent *dp;
    175 	unsigned char rv;
    176 
    177 	if ((file = strrchr(word, '/')) == NULL) {
    178 		dir[0] = '.';
    179 		dir[1] = '\0';
    180 		file = word;
    181 	} else {
    182 		if (file == word) {
    183 			dir[0] = '/';
    184 			dir[1] = '\0';
    185 		} else {
    186 			(void)strncpy(dir, word, file - word);
    187 			dir[file - word] = '\0';
    188 		}
    189 		file++;
    190 	}
    191 
    192 	if ((dd = opendir(dir)) == NULL)
    193 		return (CC_ERROR);
    194 
    195 	words = sl_init();
    196 
    197 	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
    198 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    199 			continue;
    200 		if (strlen(file) > dp->d_namlen)
    201 			continue;
    202 		if (strncmp(file, dp->d_name, strlen(file)) == 0) {
    203 			char *tcp;
    204 
    205 			tcp = strdup(dp->d_name);
    206 			if (tcp == NULL)
    207 				errx(1, "Can't allocate memory for local dir");
    208 			sl_add(words, tcp);
    209 		}
    210 	}
    211 	closedir(dd);
    212 
    213 	rv = complete_ambiguous(file, list, words);
    214 	sl_free(words, 1);
    215 	return (rv);
    216 }
    217 
    218 /*
    219  * Complete a remote file
    220  */
    221 static unsigned char
    222 complete_remote(word, list)
    223 	char *word;
    224 	int list;
    225 {
    226 	static StringList *dirlist;
    227 	static char	 lastdir[MAXPATHLEN];
    228 	StringList	*words;
    229 	char		 dir[MAXPATHLEN];
    230 	char		*file, *cp;
    231 	int		 i;
    232 	unsigned char	 rv;
    233 
    234 	char *dummyargv[] = { "complete", dir, NULL };
    235 
    236 	if ((file = strrchr(word, '/')) == NULL) {
    237 		dir[0] = '.';
    238 		dir[1] = '\0';
    239 		file = word;
    240 	} else {
    241 		cp = file;
    242 		while (*cp == '/' && cp > word)
    243 			cp--;
    244 		(void)strncpy(dir, word, cp - word + 1);
    245 		dir[cp - word + 1] = '\0';
    246 		file++;
    247 	}
    248 
    249 	if (dirchange || strcmp(dir, lastdir) != 0) {	/* dir not cached */
    250 		char *emesg;
    251 
    252 		if (dirlist != NULL)
    253 			sl_free(dirlist, 1);
    254 		dirlist = sl_init();
    255 
    256 		mflag = 1;
    257 		emesg = NULL;
    258 		while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
    259 			char *tcp;
    260 
    261 			if (!mflag)
    262 				continue;
    263 			if (*cp == '\0') {
    264 				mflag = 0;
    265 				continue;
    266 			}
    267 			tcp = strrchr(cp, '/');
    268 			if (tcp)
    269 				tcp++;
    270 			else
    271 				tcp = cp;
    272 			tcp = strdup(tcp);
    273 			if (tcp == NULL)
    274 				errx(1, "Can't allocate memory for remote dir");
    275 			sl_add(dirlist, tcp);
    276 		}
    277 		if (emesg != NULL) {
    278 			printf("\n%s\n", emesg);
    279 			return (CC_REDISPLAY);
    280 		}
    281 		(void)strcpy(lastdir, dir);
    282 		dirchange = 0;
    283 	}
    284 
    285 	words = sl_init();
    286 	for (i = 0; i < dirlist->sl_cur; i++) {
    287 		cp = dirlist->sl_str[i];
    288 		if (strlen(file) > strlen(cp))
    289 			continue;
    290 		if (strncmp(file, cp, strlen(file)) == 0)
    291 			sl_add(words, cp);
    292 	}
    293 	rv = complete_ambiguous(file, list, words);
    294 	sl_free(words, 0);
    295 	return (rv);
    296 }
    297 
    298 /*
    299  * Generic complete routine
    300  */
    301 unsigned char
    302 complete(el, ch)
    303 	EditLine *el;
    304 	int ch;
    305 {
    306 	static char word[FTPBUFLEN];
    307 	static int lastc_argc, lastc_argo;
    308 
    309 	struct cmd *c;
    310 	const LineInfo *lf;
    311 	int len, celems, dolist;
    312 
    313 	lf = el_line(el);
    314 	len = lf->lastchar - lf->buffer;
    315 	if (len >= sizeof(line))
    316 		return (CC_ERROR);
    317 	(void)strncpy(line, lf->buffer, len);
    318 	line[len] = '\0';
    319 	cursor_pos = line + (lf->cursor - lf->buffer);
    320 	lastc_argc = cursor_argc;	/* remember last cursor pos */
    321 	lastc_argo = cursor_argo;
    322 	makeargv();			/* build argc/argv of current line */
    323 
    324 	if (cursor_argo >= sizeof(word))
    325 		return (CC_ERROR);
    326 
    327 	dolist = 0;
    328 			/* if cursor and word is same, list alternatives */
    329 	if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
    330 	    && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
    331 		dolist = 1;
    332 	else
    333 	    (void)strncpy(word, margv[cursor_argc], cursor_argo);
    334 	word[cursor_argo] = '\0';
    335 
    336 	if (cursor_argc == 0)
    337 		return (complete_command(word, dolist));
    338 
    339 	c = getcmd(margv[0]);
    340 	if (c == (struct cmd *)-1 || c == 0)
    341 		return (CC_ERROR);
    342 	celems = strlen(c->c_complete);
    343 
    344 		/* check for 'continuation' completes (which are uppercase) */
    345 	if ((cursor_argc > celems) && (celems > 0)
    346 	    && isupper(c->c_complete[celems-1]))
    347 		cursor_argc = celems;
    348 
    349 	if (cursor_argc > celems)
    350 		return (CC_ERROR);
    351 
    352 	switch (c->c_complete[cursor_argc - 1]) {
    353 		case 'l':			/* local complete */
    354 		case 'L':
    355 			return (complete_local(word, dolist));
    356 		case 'r':			/* remote complete */
    357 		case 'R':
    358 			if (connected != -1) {
    359 				puts("\nMust be logged in to complete.");
    360 				return (CC_REDISPLAY);
    361 			}
    362 			return (complete_remote(word, dolist));
    363 		case 'c':			/* command complete */
    364 		case 'C':
    365 			return (complete_command(word, dolist));
    366 		case 'n':			/* no complete */
    367 		default:
    368 			return (CC_ERROR);
    369 	}
    370 
    371 	return (CC_ERROR);
    372 }
    373 
    374 #endif /* !SMALL */
    375