xref: /dflybsd-src/contrib/tnftp/src/complete.c (revision 1612048d3f35192eeb39f3a28148604bef0d5b6e)
1*3a184c67SAntonio Huete Jimenez /*	$NetBSD: complete.c,v 1.11 2020/07/04 09:59:07 lukem Exp $	*/
2*3a184c67SAntonio Huete Jimenez /*	from	NetBSD: complete.c,v 1.47 2019/01/28 12:04:16 christos Exp	*/
36cdfca03SJohn Marino 
46cdfca03SJohn Marino /*-
56cdfca03SJohn Marino  * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
66cdfca03SJohn Marino  * All rights reserved.
76cdfca03SJohn Marino  *
86cdfca03SJohn Marino  * This code is derived from software contributed to The NetBSD Foundation
96cdfca03SJohn Marino  * by Luke Mewburn.
106cdfca03SJohn Marino  *
116cdfca03SJohn Marino  * Redistribution and use in source and binary forms, with or without
126cdfca03SJohn Marino  * modification, are permitted provided that the following conditions
136cdfca03SJohn Marino  * are met:
146cdfca03SJohn Marino  * 1. Redistributions of source code must retain the above copyright
156cdfca03SJohn Marino  *    notice, this list of conditions and the following disclaimer.
166cdfca03SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
176cdfca03SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
186cdfca03SJohn Marino  *    documentation and/or other materials provided with the distribution.
196cdfca03SJohn Marino  *
206cdfca03SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
216cdfca03SJohn Marino  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
226cdfca03SJohn Marino  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
236cdfca03SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
246cdfca03SJohn Marino  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
256cdfca03SJohn Marino  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
266cdfca03SJohn Marino  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
276cdfca03SJohn Marino  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
286cdfca03SJohn Marino  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
296cdfca03SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
306cdfca03SJohn Marino  * POSSIBILITY OF SUCH DAMAGE.
316cdfca03SJohn Marino  */
326cdfca03SJohn Marino 
336cdfca03SJohn Marino #include "tnftp.h"
346cdfca03SJohn Marino 
356cdfca03SJohn Marino #if 0	/* tnftp */
366cdfca03SJohn Marino 
376cdfca03SJohn Marino #include <sys/cdefs.h>
386cdfca03SJohn Marino #ifndef lint
39*3a184c67SAntonio Huete Jimenez __RCSID(" NetBSD: complete.c,v 1.47 2019/01/28 12:04:16 christos Exp  ");
406cdfca03SJohn Marino #endif /* not lint */
416cdfca03SJohn Marino 
426cdfca03SJohn Marino /*
436cdfca03SJohn Marino  * FTP user program - command and file completion routines
446cdfca03SJohn Marino  */
456cdfca03SJohn Marino 
466cdfca03SJohn Marino #include <sys/stat.h>
476cdfca03SJohn Marino 
486cdfca03SJohn Marino #include <ctype.h>
496cdfca03SJohn Marino #include <err.h>
506cdfca03SJohn Marino #include <dirent.h>
516cdfca03SJohn Marino #include <stdio.h>
526cdfca03SJohn Marino #include <stdlib.h>
536cdfca03SJohn Marino #include <string.h>
546cdfca03SJohn Marino 
556cdfca03SJohn Marino #endif	/* tnftp */
566cdfca03SJohn Marino 
576cdfca03SJohn Marino #include "ftp_var.h"
586cdfca03SJohn Marino 
596cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
606cdfca03SJohn Marino 
616cdfca03SJohn Marino static int	     comparstr		(const void *, const void *);
626cdfca03SJohn Marino static unsigned char complete_ambiguous	(char *, int, StringList *);
636cdfca03SJohn Marino static unsigned char complete_command	(char *, int);
646cdfca03SJohn Marino static unsigned char complete_local	(char *, int);
656cdfca03SJohn Marino static unsigned char complete_option	(char *, int);
666cdfca03SJohn Marino static unsigned char complete_remote	(char *, int);
676cdfca03SJohn Marino 
686cdfca03SJohn Marino static int
comparstr(const void * a,const void * b)696cdfca03SJohn Marino comparstr(const void *a, const void *b)
706cdfca03SJohn Marino {
716cdfca03SJohn Marino 	return (strcmp(*(const char * const *)a, *(const char * const *)b));
726cdfca03SJohn Marino }
736cdfca03SJohn Marino 
746cdfca03SJohn Marino /*
756cdfca03SJohn Marino  * Determine if complete is ambiguous. If unique, insert.
766cdfca03SJohn Marino  * If no choices, error. If unambiguous prefix, insert that.
776cdfca03SJohn Marino  * Otherwise, list choices. words is assumed to be filtered
786cdfca03SJohn Marino  * to only contain possible choices.
796cdfca03SJohn Marino  * Args:
806cdfca03SJohn Marino  *	word	word which started the match
816cdfca03SJohn Marino  *	list	list by default
826cdfca03SJohn Marino  *	words	stringlist containing possible matches
836cdfca03SJohn Marino  * Returns a result as per el_set(EL_ADDFN, ...)
846cdfca03SJohn Marino  */
856cdfca03SJohn Marino static unsigned char
complete_ambiguous(char * word,int list,StringList * words)866cdfca03SJohn Marino complete_ambiguous(char *word, int list, StringList *words)
876cdfca03SJohn Marino {
886cdfca03SJohn Marino 	char insertstr[MAXPATHLEN];
896cdfca03SJohn Marino 	char *lastmatch, *p;
906cdfca03SJohn Marino 	size_t i, j;
916cdfca03SJohn Marino 	size_t matchlen, wordlen;
926cdfca03SJohn Marino 
936cdfca03SJohn Marino 	wordlen = strlen(word);
946cdfca03SJohn Marino 	if (words->sl_cur == 0)
956cdfca03SJohn Marino 		return (CC_ERROR);	/* no choices available */
966cdfca03SJohn Marino 
976cdfca03SJohn Marino 	if (words->sl_cur == 1) {	/* only once choice available */
986cdfca03SJohn Marino 		p = words->sl_str[0] + wordlen;
996cdfca03SJohn Marino 		if (*p == '\0')		/* at end of word? */
1006cdfca03SJohn Marino 			return (CC_REFRESH);
1016cdfca03SJohn Marino 		ftpvis(insertstr, sizeof(insertstr), p, strlen(p));
1026cdfca03SJohn Marino 		if (el_insertstr(el, insertstr) == -1)
1036cdfca03SJohn Marino 			return (CC_ERROR);
1046cdfca03SJohn Marino 		else
1056cdfca03SJohn Marino 			return (CC_REFRESH);
1066cdfca03SJohn Marino 	}
1076cdfca03SJohn Marino 
1086cdfca03SJohn Marino 	if (!list) {
1096cdfca03SJohn Marino 		lastmatch = words->sl_str[0];
1106cdfca03SJohn Marino 		matchlen = strlen(lastmatch);
1116cdfca03SJohn Marino 		for (i = 1 ; i < words->sl_cur ; i++) {
1126cdfca03SJohn Marino 			for (j = wordlen; j < strlen(words->sl_str[i]); j++)
1136cdfca03SJohn Marino 				if (lastmatch[j] != words->sl_str[i][j])
1146cdfca03SJohn Marino 					break;
1156cdfca03SJohn Marino 			if (j < matchlen)
1166cdfca03SJohn Marino 				matchlen = j;
1176cdfca03SJohn Marino 		}
1186cdfca03SJohn Marino 		if (matchlen > wordlen) {
1196cdfca03SJohn Marino 			ftpvis(insertstr, sizeof(insertstr),
1206cdfca03SJohn Marino 			    lastmatch + wordlen, matchlen - wordlen);
1216cdfca03SJohn Marino 			if (el_insertstr(el, insertstr) == -1)
1226cdfca03SJohn Marino 				return (CC_ERROR);
1236cdfca03SJohn Marino 			else
1246cdfca03SJohn Marino 				return (CC_REFRESH_BEEP);
1256cdfca03SJohn Marino 		}
1266cdfca03SJohn Marino 	}
1276cdfca03SJohn Marino 
1286cdfca03SJohn Marino 	putc('\n', ttyout);
1296cdfca03SJohn Marino 	qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
1306cdfca03SJohn Marino 	list_vertical(words);
1316cdfca03SJohn Marino 	return (CC_REDISPLAY);
1326cdfca03SJohn Marino }
1336cdfca03SJohn Marino 
1346cdfca03SJohn Marino /*
1356cdfca03SJohn Marino  * Complete a command
1366cdfca03SJohn Marino  */
1376cdfca03SJohn Marino static unsigned char
complete_command(char * word,int list)1386cdfca03SJohn Marino complete_command(char *word, int list)
1396cdfca03SJohn Marino {
1406cdfca03SJohn Marino 	struct cmd *c;
1416cdfca03SJohn Marino 	StringList *words;
1426cdfca03SJohn Marino 	size_t wordlen;
1436cdfca03SJohn Marino 	unsigned char rv;
1446cdfca03SJohn Marino 
1456cdfca03SJohn Marino 	words = ftp_sl_init();
1466cdfca03SJohn Marino 	wordlen = strlen(word);
1476cdfca03SJohn Marino 
1486cdfca03SJohn Marino 	for (c = cmdtab; c->c_name != NULL; c++) {
1496cdfca03SJohn Marino 		if (wordlen > strlen(c->c_name))
1506cdfca03SJohn Marino 			continue;
1516cdfca03SJohn Marino 		if (strncmp(word, c->c_name, wordlen) == 0)
1526cdfca03SJohn Marino 			ftp_sl_add(words, ftp_strdup(c->c_name));
1536cdfca03SJohn Marino 	}
1546cdfca03SJohn Marino 
1556cdfca03SJohn Marino 	rv = complete_ambiguous(word, list, words);
1566cdfca03SJohn Marino 	if (rv == CC_REFRESH) {
1576cdfca03SJohn Marino 		if (el_insertstr(el, " ") == -1)
1586cdfca03SJohn Marino 			rv = CC_ERROR;
1596cdfca03SJohn Marino 	}
1606cdfca03SJohn Marino 	sl_free(words, 1);
1616cdfca03SJohn Marino 	return (rv);
1626cdfca03SJohn Marino }
1636cdfca03SJohn Marino 
1646cdfca03SJohn Marino /*
1656cdfca03SJohn Marino  * Complete a local file
1666cdfca03SJohn Marino  */
1676cdfca03SJohn Marino static unsigned char
complete_local(char * word,int list)1686cdfca03SJohn Marino complete_local(char *word, int list)
1696cdfca03SJohn Marino {
1706cdfca03SJohn Marino 	StringList *words;
1716cdfca03SJohn Marino 	char dir[MAXPATHLEN];
1726cdfca03SJohn Marino 	char *file;
1736cdfca03SJohn Marino 	DIR *dd;
1746cdfca03SJohn Marino 	struct dirent *dp;
1756cdfca03SJohn Marino 	unsigned char rv;
1766cdfca03SJohn Marino 	size_t len;
1776cdfca03SJohn Marino 
1786cdfca03SJohn Marino 	if ((file = strrchr(word, '/')) == NULL) {
1796cdfca03SJohn Marino 		dir[0] = '.';
1806cdfca03SJohn Marino 		dir[1] = '\0';
1816cdfca03SJohn Marino 		file = word;
1826cdfca03SJohn Marino 	} else {
1836cdfca03SJohn Marino 		if (file == word) {
1846cdfca03SJohn Marino 			dir[0] = '/';
1856cdfca03SJohn Marino 			dir[1] = '\0';
1866cdfca03SJohn Marino 		} else
1876cdfca03SJohn Marino 			(void)strlcpy(dir, word, file - word + 1);
1886cdfca03SJohn Marino 		file++;
1896cdfca03SJohn Marino 	}
1906cdfca03SJohn Marino 	if (dir[0] == '~') {
1916cdfca03SJohn Marino 		char *p;
1926cdfca03SJohn Marino 
1936cdfca03SJohn Marino 		if ((p = globulize(dir)) == NULL)
1946cdfca03SJohn Marino 			return (CC_ERROR);
1956cdfca03SJohn Marino 		(void)strlcpy(dir, p, sizeof(dir));
1966cdfca03SJohn Marino 		free(p);
1976cdfca03SJohn Marino 	}
1986cdfca03SJohn Marino 
1996cdfca03SJohn Marino 	if ((dd = opendir(dir)) == NULL)
2006cdfca03SJohn Marino 		return (CC_ERROR);
2016cdfca03SJohn Marino 
2026cdfca03SJohn Marino 	words = ftp_sl_init();
2036cdfca03SJohn Marino 	len = strlen(file);
2046cdfca03SJohn Marino 
2056cdfca03SJohn Marino 	for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
2066cdfca03SJohn Marino 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
2076cdfca03SJohn Marino 			continue;
2086cdfca03SJohn Marino 
2096cdfca03SJohn Marino #if defined(DIRENT_MISSING_D_NAMLEN)
2106cdfca03SJohn Marino 		if (len > strlen(dp->d_name))
2116cdfca03SJohn Marino 			continue;
2126cdfca03SJohn Marino #else
2136cdfca03SJohn Marino 		if (len > dp->d_namlen)
2146cdfca03SJohn Marino 			continue;
2156cdfca03SJohn Marino #endif
2166cdfca03SJohn Marino 		if (strncmp(file, dp->d_name, len) == 0) {
2176cdfca03SJohn Marino 			char *tcp;
2186cdfca03SJohn Marino 
2196cdfca03SJohn Marino 			tcp = ftp_strdup(dp->d_name);
2206cdfca03SJohn Marino 			ftp_sl_add(words, tcp);
2216cdfca03SJohn Marino 		}
2226cdfca03SJohn Marino 	}
2236cdfca03SJohn Marino 	closedir(dd);
2246cdfca03SJohn Marino 
2256cdfca03SJohn Marino 	rv = complete_ambiguous(file, list, words);
2266cdfca03SJohn Marino 	if (rv == CC_REFRESH) {
2276cdfca03SJohn Marino 		struct stat sb;
2286cdfca03SJohn Marino 		char path[MAXPATHLEN];
2296cdfca03SJohn Marino 
2306cdfca03SJohn Marino 		(void)strlcpy(path, dir,		sizeof(path));
2316cdfca03SJohn Marino 		(void)strlcat(path, "/",		sizeof(path));
2326cdfca03SJohn Marino 		(void)strlcat(path, words->sl_str[0],	sizeof(path));
2336cdfca03SJohn Marino 
2346cdfca03SJohn Marino 		if (stat(path, &sb) >= 0) {
2356cdfca03SJohn Marino 			char suffix[2] = " ";
2366cdfca03SJohn Marino 
2376cdfca03SJohn Marino 			if (S_ISDIR(sb.st_mode))
2386cdfca03SJohn Marino 				suffix[0] = '/';
2396cdfca03SJohn Marino 			if (el_insertstr(el, suffix) == -1)
2406cdfca03SJohn Marino 				rv = CC_ERROR;
2416cdfca03SJohn Marino 		}
2426cdfca03SJohn Marino 	}
2436cdfca03SJohn Marino 	sl_free(words, 1);
2446cdfca03SJohn Marino 	return (rv);
2456cdfca03SJohn Marino }
2466cdfca03SJohn Marino /*
2476cdfca03SJohn Marino  * Complete an option
2486cdfca03SJohn Marino  */
2496cdfca03SJohn Marino static unsigned char
complete_option(char * word,int list)2506cdfca03SJohn Marino complete_option(char *word, int list)
2516cdfca03SJohn Marino {
2526cdfca03SJohn Marino 	struct option *o;
2536cdfca03SJohn Marino 	StringList *words;
2546cdfca03SJohn Marino 	size_t wordlen;
2556cdfca03SJohn Marino 	unsigned char rv;
2566cdfca03SJohn Marino 
2576cdfca03SJohn Marino 	words = ftp_sl_init();
2586cdfca03SJohn Marino 	wordlen = strlen(word);
2596cdfca03SJohn Marino 
2606cdfca03SJohn Marino 	for (o = optiontab; o->name != NULL; o++) {
2616cdfca03SJohn Marino 		if (wordlen > strlen(o->name))
2626cdfca03SJohn Marino 			continue;
2636cdfca03SJohn Marino 		if (strncmp(word, o->name, wordlen) == 0)
2646cdfca03SJohn Marino 			ftp_sl_add(words, ftp_strdup(o->name));
2656cdfca03SJohn Marino 	}
2666cdfca03SJohn Marino 
2676cdfca03SJohn Marino 	rv = complete_ambiguous(word, list, words);
2686cdfca03SJohn Marino 	if (rv == CC_REFRESH) {
2696cdfca03SJohn Marino 		if (el_insertstr(el, " ") == -1)
2706cdfca03SJohn Marino 			rv = CC_ERROR;
2716cdfca03SJohn Marino 	}
2726cdfca03SJohn Marino 	sl_free(words, 1);
2736cdfca03SJohn Marino 	return (rv);
2746cdfca03SJohn Marino }
2756cdfca03SJohn Marino 
2766cdfca03SJohn Marino /*
2776cdfca03SJohn Marino  * Complete a remote file
2786cdfca03SJohn Marino  */
2796cdfca03SJohn Marino static unsigned char
complete_remote(char * word,int list)2806cdfca03SJohn Marino complete_remote(char *word, int list)
2816cdfca03SJohn Marino {
2826cdfca03SJohn Marino 	static StringList *dirlist;
2836cdfca03SJohn Marino 	static char	 lastdir[MAXPATHLEN];
2846cdfca03SJohn Marino 	StringList	*words;
2856cdfca03SJohn Marino 	char		 dir[MAXPATHLEN];
2866cdfca03SJohn Marino 	char		*file, *cp;
2876cdfca03SJohn Marino 	size_t		 i;
2886cdfca03SJohn Marino 	unsigned char	 rv;
2896cdfca03SJohn Marino 	char		 cmdbuf[MAX_C_NAME];
2906cdfca03SJohn Marino 	char		*dummyargv[3] = { NULL, NULL, NULL };
2916cdfca03SJohn Marino 
2926cdfca03SJohn Marino 	(void)strlcpy(cmdbuf, "complete", sizeof(cmdbuf));
2936cdfca03SJohn Marino 	dummyargv[0] = cmdbuf;
2946cdfca03SJohn Marino 	dummyargv[1] = dir;
2956cdfca03SJohn Marino 
2966cdfca03SJohn Marino 	if ((file = strrchr(word, '/')) == NULL) {
2976cdfca03SJohn Marino 		dir[0] = '\0';
2986cdfca03SJohn Marino 		file = word;
2996cdfca03SJohn Marino 	} else {
3006cdfca03SJohn Marino 		cp = file;
3016cdfca03SJohn Marino 		while (*cp == '/' && cp > word)
3026cdfca03SJohn Marino 			cp--;
3036cdfca03SJohn Marino 		(void)strlcpy(dir, word, cp - word + 2);
3046cdfca03SJohn Marino 		file++;
3056cdfca03SJohn Marino 	}
3066cdfca03SJohn Marino 
3076cdfca03SJohn Marino 	if (dirchange || dirlist == NULL ||
3086cdfca03SJohn Marino 	    strcmp(dir, lastdir) != 0) {		/* dir not cached */
3096cdfca03SJohn Marino 		const char *emesg;
3106cdfca03SJohn Marino 
3116cdfca03SJohn Marino 		if (dirlist != NULL)
3126cdfca03SJohn Marino 			sl_free(dirlist, 1);
3136cdfca03SJohn Marino 		dirlist = ftp_sl_init();
3146cdfca03SJohn Marino 
3156cdfca03SJohn Marino 		mflag = 1;
3166cdfca03SJohn Marino 		emesg = NULL;
3176cdfca03SJohn Marino 		while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
3186cdfca03SJohn Marino 			char *tcp;
3196cdfca03SJohn Marino 
3206cdfca03SJohn Marino 			if (!mflag)
3216cdfca03SJohn Marino 				continue;
3226cdfca03SJohn Marino 			if (*cp == '\0') {
3236cdfca03SJohn Marino 				mflag = 0;
3246cdfca03SJohn Marino 				continue;
3256cdfca03SJohn Marino 			}
3266cdfca03SJohn Marino 			tcp = strrchr(cp, '/');
3276cdfca03SJohn Marino 			if (tcp)
3286cdfca03SJohn Marino 				tcp++;
3296cdfca03SJohn Marino 			else
3306cdfca03SJohn Marino 				tcp = cp;
3316cdfca03SJohn Marino 			tcp = ftp_strdup(tcp);
3326cdfca03SJohn Marino 			ftp_sl_add(dirlist, tcp);
3336cdfca03SJohn Marino 		}
3346cdfca03SJohn Marino 		if (emesg != NULL) {
3356cdfca03SJohn Marino 			fprintf(ttyout, "\n%s\n", emesg);
3366cdfca03SJohn Marino 			return (CC_REDISPLAY);
3376cdfca03SJohn Marino 		}
3386cdfca03SJohn Marino 		(void)strlcpy(lastdir, dir, sizeof(lastdir));
3396cdfca03SJohn Marino 		dirchange = 0;
3406cdfca03SJohn Marino 	}
3416cdfca03SJohn Marino 
3426cdfca03SJohn Marino 	words = ftp_sl_init();
3436cdfca03SJohn Marino 	for (i = 0; i < dirlist->sl_cur; i++) {
3446cdfca03SJohn Marino 		cp = dirlist->sl_str[i];
3456cdfca03SJohn Marino 		if (strlen(file) > strlen(cp))
3466cdfca03SJohn Marino 			continue;
3476cdfca03SJohn Marino 		if (strncmp(file, cp, strlen(file)) == 0)
3486cdfca03SJohn Marino 			ftp_sl_add(words, cp);
3496cdfca03SJohn Marino 	}
3506cdfca03SJohn Marino 	rv = complete_ambiguous(file, list, words);
3516cdfca03SJohn Marino 	sl_free(words, 0);
3526cdfca03SJohn Marino 	return (rv);
3536cdfca03SJohn Marino }
3546cdfca03SJohn Marino 
3556cdfca03SJohn Marino /*
3566cdfca03SJohn Marino  * Generic complete routine
3576cdfca03SJohn Marino  */
3586cdfca03SJohn Marino unsigned char
complete(EditLine * cel,int ch)3596cdfca03SJohn Marino complete(EditLine *cel, int ch)
3606cdfca03SJohn Marino {
3616cdfca03SJohn Marino 	static char word[FTPBUFLEN];
3626cdfca03SJohn Marino 	static size_t lastc_argc, lastc_argo;
3636cdfca03SJohn Marino 
3646cdfca03SJohn Marino 	struct cmd *c;
3656cdfca03SJohn Marino 	const LineInfo *lf;
3666cdfca03SJohn Marino 	int dolist, cmpltype;
3676cdfca03SJohn Marino 	size_t celems, len;
3686cdfca03SJohn Marino 
3696cdfca03SJohn Marino 	lf = el_line(cel);
3706cdfca03SJohn Marino 	len = lf->lastchar - lf->buffer;
3716cdfca03SJohn Marino 	if (len >= sizeof(line))
3726cdfca03SJohn Marino 		return (CC_ERROR);
3736cdfca03SJohn Marino 	(void)strlcpy(line, lf->buffer, len + 1);
3746cdfca03SJohn Marino 	cursor_pos = line + (lf->cursor - lf->buffer);
3756cdfca03SJohn Marino 	lastc_argc = cursor_argc;	/* remember last cursor pos */
3766cdfca03SJohn Marino 	lastc_argo = cursor_argo;
3776cdfca03SJohn Marino 	makeargv();			/* build argc/argv of current line */
3786cdfca03SJohn Marino 
3796cdfca03SJohn Marino 	if (cursor_argo >= sizeof(word))
3806cdfca03SJohn Marino 		return (CC_ERROR);
3816cdfca03SJohn Marino 
3826cdfca03SJohn Marino 	dolist = 0;
3836cdfca03SJohn Marino 			/* if cursor and word is same, list alternatives */
3846cdfca03SJohn Marino 	if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
3856cdfca03SJohn Marino 	    && strncmp(word, margv[cursor_argc] ? margv[cursor_argc] : "",
3866cdfca03SJohn Marino 			cursor_argo) == 0)
3876cdfca03SJohn Marino 		dolist = 1;
3886cdfca03SJohn Marino 	else if (cursor_argc < (size_t)margc)
3896cdfca03SJohn Marino 		(void)strlcpy(word, margv[cursor_argc], cursor_argo + 1);
3906cdfca03SJohn Marino 	word[cursor_argo] = '\0';
3916cdfca03SJohn Marino 
3926cdfca03SJohn Marino 	if (cursor_argc == 0)
3936cdfca03SJohn Marino 		return (complete_command(word, dolist));
3946cdfca03SJohn Marino 
3956cdfca03SJohn Marino 	c = getcmd(margv[0]);
3966cdfca03SJohn Marino 	if (c == (struct cmd *)-1 || c == 0)
3976cdfca03SJohn Marino 		return (CC_ERROR);
3986cdfca03SJohn Marino 	celems = strlen(c->c_complete);
3996cdfca03SJohn Marino 
4006cdfca03SJohn Marino 		/* check for 'continuation' completes (which are uppercase) */
4016cdfca03SJohn Marino 	if ((cursor_argc > celems) && (celems > 0)
4026cdfca03SJohn Marino 	    && isupper((unsigned char) c->c_complete[celems-1]))
4036cdfca03SJohn Marino 		cursor_argc = celems;
4046cdfca03SJohn Marino 
4056cdfca03SJohn Marino 	if (cursor_argc > celems)
4066cdfca03SJohn Marino 		return (CC_ERROR);
4076cdfca03SJohn Marino 
4086cdfca03SJohn Marino 	cmpltype = c->c_complete[cursor_argc - 1];
4096cdfca03SJohn Marino 	switch (cmpltype) {
4106cdfca03SJohn Marino 		case 'c':			/* command complete */
4116cdfca03SJohn Marino 		case 'C':
4126cdfca03SJohn Marino 			return (complete_command(word, dolist));
4136cdfca03SJohn Marino 		case 'l':			/* local complete */
4146cdfca03SJohn Marino 		case 'L':
4156cdfca03SJohn Marino 			return (complete_local(word, dolist));
4166cdfca03SJohn Marino 		case 'n':			/* no complete */
4176cdfca03SJohn Marino 		case 'N':			/* no complete */
4186cdfca03SJohn Marino 			return (CC_ERROR);
4196cdfca03SJohn Marino 		case 'o':			/* local complete */
4206cdfca03SJohn Marino 		case 'O':
4216cdfca03SJohn Marino 			return (complete_option(word, dolist));
4226cdfca03SJohn Marino 		case 'r':			/* remote complete */
4236cdfca03SJohn Marino 		case 'R':
4246cdfca03SJohn Marino 			if (connected != -1) {
4256cdfca03SJohn Marino 				fputs("\nMust be logged in to complete.\n",
4266cdfca03SJohn Marino 				    ttyout);
4276cdfca03SJohn Marino 				return (CC_REDISPLAY);
4286cdfca03SJohn Marino 			}
4296cdfca03SJohn Marino 			return (complete_remote(word, dolist));
4306cdfca03SJohn Marino 		default:
4316cdfca03SJohn Marino 			errx(1, "complete: unknown complete type `%c'",
4326cdfca03SJohn Marino 			    cmpltype);
4336cdfca03SJohn Marino 			return (CC_ERROR);
4346cdfca03SJohn Marino 	}
4356cdfca03SJohn Marino 	/* NOTREACHED */
4366cdfca03SJohn Marino }
4376cdfca03SJohn Marino 
4386cdfca03SJohn Marino #endif /* !NO_EDITCOMPLETE */
439