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