1*cdf8408cSAntonio Huete Jimenez /*	$NetBSD: filecomplete.c,v 1.70 2022/03/12 15:29:17 christos Exp $	*/
232fe07f8SJohn Marino 
332fe07f8SJohn Marino /*-
432fe07f8SJohn Marino  * Copyright (c) 1997 The NetBSD Foundation, Inc.
532fe07f8SJohn Marino  * All rights reserved.
632fe07f8SJohn Marino  *
732fe07f8SJohn Marino  * This code is derived from software contributed to The NetBSD Foundation
832fe07f8SJohn Marino  * by Jaromir Dolecek.
932fe07f8SJohn Marino  *
1032fe07f8SJohn Marino  * Redistribution and use in source and binary forms, with or without
1132fe07f8SJohn Marino  * modification, are permitted provided that the following conditions
1232fe07f8SJohn Marino  * are met:
1332fe07f8SJohn Marino  * 1. Redistributions of source code must retain the above copyright
1432fe07f8SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1532fe07f8SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1632fe07f8SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1732fe07f8SJohn Marino  *    documentation and/or other materials provided with the distribution.
1832fe07f8SJohn Marino  *
1932fe07f8SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2032fe07f8SJohn Marino  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2132fe07f8SJohn Marino  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2232fe07f8SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2332fe07f8SJohn Marino  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2432fe07f8SJohn Marino  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2532fe07f8SJohn Marino  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2632fe07f8SJohn Marino  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2732fe07f8SJohn Marino  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2832fe07f8SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2932fe07f8SJohn Marino  * POSSIBILITY OF SUCH DAMAGE.
3032fe07f8SJohn Marino  */
3132fe07f8SJohn Marino 
3232fe07f8SJohn Marino #include "config.h"
3332fe07f8SJohn Marino 
3432fe07f8SJohn Marino #if !defined(lint) && !defined(SCCSID)
35*cdf8408cSAntonio Huete Jimenez __RCSID("$NetBSD: filecomplete.c,v 1.70 2022/03/12 15:29:17 christos Exp $");
3632fe07f8SJohn Marino #endif /* not lint && not SCCSID */
3732fe07f8SJohn Marino 
3832fe07f8SJohn Marino #include <sys/types.h>
3932fe07f8SJohn Marino #include <sys/stat.h>
4032fe07f8SJohn Marino #include <dirent.h>
4132fe07f8SJohn Marino #include <errno.h>
4232fe07f8SJohn Marino #include <fcntl.h>
4312db70c8Szrj #include <limits.h>
4412db70c8Szrj #include <pwd.h>
4512db70c8Szrj #include <stdio.h>
4612db70c8Szrj #include <stdlib.h>
4712db70c8Szrj #include <string.h>
4812db70c8Szrj #include <unistd.h>
4932fe07f8SJohn Marino 
5032fe07f8SJohn Marino #include "el.h"
5132fe07f8SJohn Marino #include "filecomplete.h"
5232fe07f8SJohn Marino 
5312db70c8Szrj static const wchar_t break_chars[] = L" \t\n\"\\'`@$><=;|&{(";
5432fe07f8SJohn Marino 
5532fe07f8SJohn Marino /********************************/
5632fe07f8SJohn Marino /* completion functions */
5732fe07f8SJohn Marino 
5832fe07f8SJohn Marino /*
5932fe07f8SJohn Marino  * does tilde expansion of strings of type ``~user/foo''
6032fe07f8SJohn Marino  * if ``user'' isn't valid user name or ``txt'' doesn't start
6132fe07f8SJohn Marino  * w/ '~', returns pointer to strdup()ed copy of ``txt''
6232fe07f8SJohn Marino  *
63a0c9eb18SJohn Marino  * it's the caller's responsibility to free() the returned string
6432fe07f8SJohn Marino  */
6532fe07f8SJohn Marino char *
fn_tilde_expand(const char * txt)6632fe07f8SJohn Marino fn_tilde_expand(const char *txt)
6732fe07f8SJohn Marino {
6832fe07f8SJohn Marino #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
6932fe07f8SJohn Marino 	struct passwd pwres;
7032fe07f8SJohn Marino 	char pwbuf[1024];
7132fe07f8SJohn Marino #endif
7232fe07f8SJohn Marino 	struct passwd *pass;
73*cdf8408cSAntonio Huete Jimenez 	const char *pos;
7432fe07f8SJohn Marino 	char *temp;
7532fe07f8SJohn Marino 	size_t len = 0;
7632fe07f8SJohn Marino 
7732fe07f8SJohn Marino 	if (txt[0] != '~')
7832fe07f8SJohn Marino 		return strdup(txt);
7932fe07f8SJohn Marino 
80*cdf8408cSAntonio Huete Jimenez 	pos = strchr(txt + 1, '/');
81*cdf8408cSAntonio Huete Jimenez 	if (pos == NULL) {
8232fe07f8SJohn Marino 		temp = strdup(txt + 1);
8332fe07f8SJohn Marino 		if (temp == NULL)
8432fe07f8SJohn Marino 			return NULL;
8532fe07f8SJohn Marino 	} else {
8632fe07f8SJohn Marino 		/* text until string after slash */
87*cdf8408cSAntonio Huete Jimenez 		len = (size_t)(pos - txt + 1);
8860ecde0cSDaniel Fojt 		temp = el_calloc(len, sizeof(*temp));
8932fe07f8SJohn Marino 		if (temp == NULL)
9032fe07f8SJohn Marino 			return NULL;
9160ecde0cSDaniel Fojt 		(void)strlcpy(temp, txt + 1, len - 1);
9232fe07f8SJohn Marino 	}
9332fe07f8SJohn Marino 	if (temp[0] == 0) {
9432fe07f8SJohn Marino #ifdef HAVE_GETPW_R_POSIX
9532fe07f8SJohn Marino 		if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
9632fe07f8SJohn Marino 		    &pass) != 0)
9732fe07f8SJohn Marino 			pass = NULL;
9832fe07f8SJohn Marino #elif HAVE_GETPW_R_DRAFT
9932fe07f8SJohn Marino 		pass = getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf));
10032fe07f8SJohn Marino #else
10132fe07f8SJohn Marino 		pass = getpwuid(getuid());
10232fe07f8SJohn Marino #endif
10332fe07f8SJohn Marino 	} else {
10432fe07f8SJohn Marino #ifdef HAVE_GETPW_R_POSIX
10532fe07f8SJohn Marino 		if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
10632fe07f8SJohn Marino 			pass = NULL;
10732fe07f8SJohn Marino #elif HAVE_GETPW_R_DRAFT
10832fe07f8SJohn Marino 		pass = getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf));
10932fe07f8SJohn Marino #else
11032fe07f8SJohn Marino 		pass = getpwnam(temp);
11132fe07f8SJohn Marino #endif
11232fe07f8SJohn Marino 	}
11332fe07f8SJohn Marino 	el_free(temp);		/* value no more needed */
11432fe07f8SJohn Marino 	if (pass == NULL)
11532fe07f8SJohn Marino 		return strdup(txt);
11632fe07f8SJohn Marino 
11732fe07f8SJohn Marino 	/* update pointer txt to point at string immedially following */
11832fe07f8SJohn Marino 	/* first slash */
11932fe07f8SJohn Marino 	txt += len;
12032fe07f8SJohn Marino 
12132fe07f8SJohn Marino 	len = strlen(pass->pw_dir) + 1 + strlen(txt) + 1;
12260ecde0cSDaniel Fojt 	temp = el_calloc(len, sizeof(*temp));
12332fe07f8SJohn Marino 	if (temp == NULL)
12432fe07f8SJohn Marino 		return NULL;
12532fe07f8SJohn Marino 	(void)snprintf(temp, len, "%s/%s", pass->pw_dir, txt);
12632fe07f8SJohn Marino 
12732fe07f8SJohn Marino 	return temp;
12832fe07f8SJohn Marino }
12932fe07f8SJohn Marino 
130ae19eda8Szrj static int
needs_escaping(wchar_t c)131*cdf8408cSAntonio Huete Jimenez needs_escaping(wchar_t c)
132ae19eda8Szrj {
133ae19eda8Szrj 	switch (c) {
134ae19eda8Szrj 	case '\'':
135ae19eda8Szrj 	case '"':
136ae19eda8Szrj 	case '(':
137ae19eda8Szrj 	case ')':
138ae19eda8Szrj 	case '\\':
139ae19eda8Szrj 	case '<':
140ae19eda8Szrj 	case '>':
141ae19eda8Szrj 	case '$':
142ae19eda8Szrj 	case '#':
143ae19eda8Szrj 	case ' ':
144ae19eda8Szrj 	case '\n':
145ae19eda8Szrj 	case '\t':
146ae19eda8Szrj 	case '?':
147ae19eda8Szrj 	case ';':
148ae19eda8Szrj 	case '`':
149ae19eda8Szrj 	case '@':
150ae19eda8Szrj 	case '=':
151ae19eda8Szrj 	case '|':
152ae19eda8Szrj 	case '{':
153ae19eda8Szrj 	case '}':
154ae19eda8Szrj 	case '&':
155ae19eda8Szrj 	case '*':
156ae19eda8Szrj 	case '[':
157ae19eda8Szrj 		return 1;
158ae19eda8Szrj 	default:
159ae19eda8Szrj 		return 0;
160ae19eda8Szrj 	}
161ae19eda8Szrj }
162ae19eda8Szrj 
16360ecde0cSDaniel Fojt static int
needs_dquote_escaping(char c)16460ecde0cSDaniel Fojt needs_dquote_escaping(char c)
16560ecde0cSDaniel Fojt {
16660ecde0cSDaniel Fojt 	switch (c) {
16760ecde0cSDaniel Fojt 	case '"':
16860ecde0cSDaniel Fojt 	case '\\':
16960ecde0cSDaniel Fojt 	case '`':
17060ecde0cSDaniel Fojt 	case '$':
17160ecde0cSDaniel Fojt 		return 1;
17260ecde0cSDaniel Fojt 	default:
17360ecde0cSDaniel Fojt 		return 0;
17460ecde0cSDaniel Fojt 	}
17560ecde0cSDaniel Fojt }
17660ecde0cSDaniel Fojt 
17760ecde0cSDaniel Fojt 
17860ecde0cSDaniel Fojt static wchar_t *
unescape_string(const wchar_t * string,size_t length)17960ecde0cSDaniel Fojt unescape_string(const wchar_t *string, size_t length)
18060ecde0cSDaniel Fojt {
18160ecde0cSDaniel Fojt 	size_t i;
18260ecde0cSDaniel Fojt 	size_t j = 0;
18360ecde0cSDaniel Fojt 	wchar_t *unescaped = el_calloc(length + 1, sizeof(*string));
18460ecde0cSDaniel Fojt 	if (unescaped == NULL)
18560ecde0cSDaniel Fojt 		return NULL;
18660ecde0cSDaniel Fojt 	for (i = 0; i < length ; i++) {
18760ecde0cSDaniel Fojt 		if (string[i] == '\\')
18860ecde0cSDaniel Fojt 			continue;
18960ecde0cSDaniel Fojt 		unescaped[j++] = string[i];
19060ecde0cSDaniel Fojt 	}
19160ecde0cSDaniel Fojt 	unescaped[j] = 0;
19260ecde0cSDaniel Fojt 	return unescaped;
19360ecde0cSDaniel Fojt }
19460ecde0cSDaniel Fojt 
195ae19eda8Szrj static char *
escape_filename(EditLine * el,const char * filename,int single_match,const char * (* app_func)(const char *))19660ecde0cSDaniel Fojt escape_filename(EditLine * el, const char *filename, int single_match,
19760ecde0cSDaniel Fojt 		const char *(*app_func)(const char *))
198ae19eda8Szrj {
199ae19eda8Szrj 	size_t original_len = 0;
200ae19eda8Szrj 	size_t escaped_character_count = 0;
201ae19eda8Szrj 	size_t offset = 0;
202ae19eda8Szrj 	size_t newlen;
203ae19eda8Szrj 	const char *s;
204ae19eda8Szrj 	char c;
205ae19eda8Szrj 	size_t s_quoted = 0;	/* does the input contain a single quote */
206ae19eda8Szrj 	size_t d_quoted = 0;	/* does the input contain a double quote */
207ae19eda8Szrj 	char *escaped_str;
208ae19eda8Szrj 	wchar_t *temp = el->el_line.buffer;
20960ecde0cSDaniel Fojt 	const char *append_char = NULL;
21060ecde0cSDaniel Fojt 
21160ecde0cSDaniel Fojt 	if (filename == NULL)
21260ecde0cSDaniel Fojt 		return NULL;
213ae19eda8Szrj 
214ae19eda8Szrj 	while (temp != el->el_line.cursor) {
215ae19eda8Szrj 		/*
21660ecde0cSDaniel Fojt 		 * If we see a single quote but have not seen a double quote
217*cdf8408cSAntonio Huete Jimenez 		 * so far set/unset s_quote, unless it is already quoted
218ae19eda8Szrj 		 */
219*cdf8408cSAntonio Huete Jimenez 		if (temp[0] == '\'' && !d_quoted &&
220*cdf8408cSAntonio Huete Jimenez 		    (temp == el->el_line.buffer || temp[-1] != '\\'))
221ae19eda8Szrj 			s_quoted = !s_quoted;
222ae19eda8Szrj 		/*
223ae19eda8Szrj 		 * vice versa to the above condition
224ae19eda8Szrj 		 */
225ae19eda8Szrj 		else if (temp[0] == '"' && !s_quoted)
226ae19eda8Szrj 			d_quoted = !d_quoted;
227ae19eda8Szrj 		temp++;
228ae19eda8Szrj 	}
229ae19eda8Szrj 
230ae19eda8Szrj 	/* Count number of special characters so that we can calculate
231ae19eda8Szrj 	 * number of extra bytes needed in the new string
232ae19eda8Szrj 	 */
233ae19eda8Szrj 	for (s = filename; *s; s++, original_len++) {
234ae19eda8Szrj 		c = *s;
235ae19eda8Szrj 		/* Inside a single quote only single quotes need escaping */
236ae19eda8Szrj 		if (s_quoted && c == '\'') {
237ae19eda8Szrj 			escaped_character_count += 3;
238ae19eda8Szrj 			continue;
239ae19eda8Szrj 		}
240ae19eda8Szrj 		/* Inside double quotes only ", \, ` and $ need escaping */
24160ecde0cSDaniel Fojt 		if (d_quoted && needs_dquote_escaping(c)) {
242ae19eda8Szrj 			escaped_character_count++;
243ae19eda8Szrj 			continue;
244ae19eda8Szrj 		}
245ae19eda8Szrj 		if (!s_quoted && !d_quoted && needs_escaping(c))
246ae19eda8Szrj 			escaped_character_count++;
247ae19eda8Szrj 	}
248ae19eda8Szrj 
249ae19eda8Szrj 	newlen = original_len + escaped_character_count + 1;
25060ecde0cSDaniel Fojt 	if (s_quoted || d_quoted)
25160ecde0cSDaniel Fojt 		newlen++;
25260ecde0cSDaniel Fojt 
25360ecde0cSDaniel Fojt 	if (single_match && app_func)
25460ecde0cSDaniel Fojt 		newlen++;
25560ecde0cSDaniel Fojt 
256ae19eda8Szrj 	if ((escaped_str = el_malloc(newlen)) == NULL)
257ae19eda8Szrj 		return NULL;
258ae19eda8Szrj 
259ae19eda8Szrj 	for (s = filename; *s; s++) {
260ae19eda8Szrj 		c = *s;
261ae19eda8Szrj 		if (!needs_escaping(c)) {
262ae19eda8Szrj 			/* no escaping is required continue as usual */
263ae19eda8Szrj 			escaped_str[offset++] = c;
264ae19eda8Szrj 			continue;
265ae19eda8Szrj 		}
266ae19eda8Szrj 
267ae19eda8Szrj 		/* single quotes inside single quotes require special handling */
268ae19eda8Szrj 		if (c == '\'' && s_quoted) {
269ae19eda8Szrj 			escaped_str[offset++] = '\'';
270ae19eda8Szrj 			escaped_str[offset++] = '\\';
271ae19eda8Szrj 			escaped_str[offset++] = '\'';
272ae19eda8Szrj 			escaped_str[offset++] = '\'';
273ae19eda8Szrj 			continue;
274ae19eda8Szrj 		}
275ae19eda8Szrj 
276ae19eda8Szrj 		/* Otherwise no escaping needed inside single quotes */
277ae19eda8Szrj 		if (s_quoted) {
278ae19eda8Szrj 			escaped_str[offset++] = c;
279ae19eda8Szrj 			continue;
280ae19eda8Szrj 		}
281ae19eda8Szrj 
282ae19eda8Szrj 		/* No escaping needed inside a double quoted string either
283ae19eda8Szrj 		 * unless we see a '$', '\', '`', or '"' (itself)
284ae19eda8Szrj 		 */
28560ecde0cSDaniel Fojt 		if (d_quoted && !needs_dquote_escaping(c)) {
286ae19eda8Szrj 			escaped_str[offset++] = c;
287ae19eda8Szrj 			continue;
288ae19eda8Szrj 		}
289ae19eda8Szrj 
290ae19eda8Szrj 		/* If we reach here that means escaping is actually needed */
291ae19eda8Szrj 		escaped_str[offset++] = '\\';
292ae19eda8Szrj 		escaped_str[offset++] = c;
293ae19eda8Szrj 	}
294ae19eda8Szrj 
29560ecde0cSDaniel Fojt 	if (single_match && app_func) {
29660ecde0cSDaniel Fojt 		escaped_str[offset] = 0;
297*cdf8408cSAntonio Huete Jimenez 		append_char = app_func(filename);
29860ecde0cSDaniel Fojt 		/* we want to append space only if we are not inside quotes */
29960ecde0cSDaniel Fojt 		if (append_char[0] == ' ') {
30060ecde0cSDaniel Fojt 			if (!s_quoted && !d_quoted)
30160ecde0cSDaniel Fojt 				escaped_str[offset++] = append_char[0];
30260ecde0cSDaniel Fojt 		} else
30360ecde0cSDaniel Fojt 			escaped_str[offset++] = append_char[0];
30460ecde0cSDaniel Fojt 	}
30560ecde0cSDaniel Fojt 
30660ecde0cSDaniel Fojt 	/* close the quotes if single match and the match is not a directory */
30760ecde0cSDaniel Fojt 	if (single_match && (append_char && append_char[0] == ' ')) {
308ae19eda8Szrj 		if (s_quoted)
309ae19eda8Szrj 			escaped_str[offset++] = '\'';
310ae19eda8Szrj 		else if (d_quoted)
311ae19eda8Szrj 			escaped_str[offset++] = '"';
31260ecde0cSDaniel Fojt 	}
313ae19eda8Szrj 
314ae19eda8Szrj 	escaped_str[offset] = 0;
315ae19eda8Szrj 	return escaped_str;
316ae19eda8Szrj }
31732fe07f8SJohn Marino 
31832fe07f8SJohn Marino /*
31932fe07f8SJohn Marino  * return first found file name starting by the ``text'' or NULL if no
32032fe07f8SJohn Marino  * such file can be found
32132fe07f8SJohn Marino  * value of ``state'' is ignored
32232fe07f8SJohn Marino  *
323a0c9eb18SJohn Marino  * it's the caller's responsibility to free the returned string
32432fe07f8SJohn Marino  */
32532fe07f8SJohn Marino char *
fn_filename_completion_function(const char * text,int state)32632fe07f8SJohn Marino fn_filename_completion_function(const char *text, int state)
32732fe07f8SJohn Marino {
32832fe07f8SJohn Marino 	static DIR *dir = NULL;
32932fe07f8SJohn Marino 	static char *filename = NULL, *dirname = NULL, *dirpath = NULL;
33032fe07f8SJohn Marino 	static size_t filename_len = 0;
33132fe07f8SJohn Marino 	struct dirent *entry;
33232fe07f8SJohn Marino 	char *temp;
333*cdf8408cSAntonio Huete Jimenez 	const char *pos;
33432fe07f8SJohn Marino 	size_t len;
33532fe07f8SJohn Marino 
33632fe07f8SJohn Marino 	if (state == 0 || dir == NULL) {
337*cdf8408cSAntonio Huete Jimenez 		pos = strrchr(text, '/');
338*cdf8408cSAntonio Huete Jimenez 		if (pos) {
33932fe07f8SJohn Marino 			char *nptr;
340*cdf8408cSAntonio Huete Jimenez 			pos++;
341*cdf8408cSAntonio Huete Jimenez 			nptr = el_realloc(filename, (strlen(pos) + 1) *
34232fe07f8SJohn Marino 			    sizeof(*nptr));
34332fe07f8SJohn Marino 			if (nptr == NULL) {
34432fe07f8SJohn Marino 				el_free(filename);
34532fe07f8SJohn Marino 				filename = NULL;
34632fe07f8SJohn Marino 				return NULL;
34732fe07f8SJohn Marino 			}
34832fe07f8SJohn Marino 			filename = nptr;
349*cdf8408cSAntonio Huete Jimenez 			(void)strcpy(filename, pos);
350*cdf8408cSAntonio Huete Jimenez 			len = (size_t)(pos - text);	/* including last slash */
35132fe07f8SJohn Marino 
35232fe07f8SJohn Marino 			nptr = el_realloc(dirname, (len + 1) *
35332fe07f8SJohn Marino 			    sizeof(*nptr));
35432fe07f8SJohn Marino 			if (nptr == NULL) {
35532fe07f8SJohn Marino 				el_free(dirname);
35632fe07f8SJohn Marino 				dirname = NULL;
35732fe07f8SJohn Marino 				return NULL;
35832fe07f8SJohn Marino 			}
35932fe07f8SJohn Marino 			dirname = nptr;
36060ecde0cSDaniel Fojt 			(void)strlcpy(dirname, text, len + 1);
36132fe07f8SJohn Marino 		} else {
36232fe07f8SJohn Marino 			el_free(filename);
36332fe07f8SJohn Marino 			if (*text == 0)
36432fe07f8SJohn Marino 				filename = NULL;
36532fe07f8SJohn Marino 			else {
36632fe07f8SJohn Marino 				filename = strdup(text);
36732fe07f8SJohn Marino 				if (filename == NULL)
36832fe07f8SJohn Marino 					return NULL;
36932fe07f8SJohn Marino 			}
37032fe07f8SJohn Marino 			el_free(dirname);
37132fe07f8SJohn Marino 			dirname = NULL;
37232fe07f8SJohn Marino 		}
37332fe07f8SJohn Marino 
37432fe07f8SJohn Marino 		if (dir != NULL) {
37532fe07f8SJohn Marino 			(void)closedir(dir);
37632fe07f8SJohn Marino 			dir = NULL;
37732fe07f8SJohn Marino 		}
37832fe07f8SJohn Marino 
37932fe07f8SJohn Marino 		/* support for ``~user'' syntax */
38032fe07f8SJohn Marino 
38132fe07f8SJohn Marino 		el_free(dirpath);
38232fe07f8SJohn Marino 		dirpath = NULL;
38332fe07f8SJohn Marino 		if (dirname == NULL) {
38432fe07f8SJohn Marino 			if ((dirname = strdup("")) == NULL)
38532fe07f8SJohn Marino 				return NULL;
38632fe07f8SJohn Marino 			dirpath = strdup("./");
38732fe07f8SJohn Marino 		} else if (*dirname == '~')
38832fe07f8SJohn Marino 			dirpath = fn_tilde_expand(dirname);
38932fe07f8SJohn Marino 		else
39032fe07f8SJohn Marino 			dirpath = strdup(dirname);
39132fe07f8SJohn Marino 
39232fe07f8SJohn Marino 		if (dirpath == NULL)
39332fe07f8SJohn Marino 			return NULL;
39432fe07f8SJohn Marino 
39532fe07f8SJohn Marino 		dir = opendir(dirpath);
39632fe07f8SJohn Marino 		if (!dir)
39732fe07f8SJohn Marino 			return NULL;	/* cannot open the directory */
39832fe07f8SJohn Marino 
39932fe07f8SJohn Marino 		/* will be used in cycle */
40032fe07f8SJohn Marino 		filename_len = filename ? strlen(filename) : 0;
40132fe07f8SJohn Marino 	}
40232fe07f8SJohn Marino 
40332fe07f8SJohn Marino 	/* find the match */
40432fe07f8SJohn Marino 	while ((entry = readdir(dir)) != NULL) {
40532fe07f8SJohn Marino 		/* skip . and .. */
40632fe07f8SJohn Marino 		if (entry->d_name[0] == '.' && (!entry->d_name[1]
40732fe07f8SJohn Marino 		    || (entry->d_name[1] == '.' && !entry->d_name[2])))
40832fe07f8SJohn Marino 			continue;
40932fe07f8SJohn Marino 		if (filename_len == 0)
41032fe07f8SJohn Marino 			break;
41132fe07f8SJohn Marino 		/* otherwise, get first entry where first */
41232fe07f8SJohn Marino 		/* filename_len characters are equal	  */
41332fe07f8SJohn Marino 		if (entry->d_name[0] == filename[0]
41432fe07f8SJohn Marino           /* Some dirents have d_namlen, but it is not portable. */
41532fe07f8SJohn Marino 		    && strlen(entry->d_name) >= filename_len
41632fe07f8SJohn Marino 		    && strncmp(entry->d_name, filename,
41732fe07f8SJohn Marino 			filename_len) == 0)
41832fe07f8SJohn Marino 			break;
41932fe07f8SJohn Marino 	}
42032fe07f8SJohn Marino 
42132fe07f8SJohn Marino 	if (entry) {		/* match found */
42232fe07f8SJohn Marino 
42332fe07f8SJohn Marino        /* Some dirents have d_namlen, but it is not portable. */
42432fe07f8SJohn Marino 		len = strlen(entry->d_name);
42532fe07f8SJohn Marino 
42632fe07f8SJohn Marino 		len = strlen(dirname) + len + 1;
42760ecde0cSDaniel Fojt 		temp = el_calloc(len, sizeof(*temp));
42832fe07f8SJohn Marino 		if (temp == NULL)
42932fe07f8SJohn Marino 			return NULL;
43032fe07f8SJohn Marino 		(void)snprintf(temp, len, "%s%s", dirname, entry->d_name);
43132fe07f8SJohn Marino 	} else {
43232fe07f8SJohn Marino 		(void)closedir(dir);
43332fe07f8SJohn Marino 		dir = NULL;
43432fe07f8SJohn Marino 		temp = NULL;
43532fe07f8SJohn Marino 	}
43632fe07f8SJohn Marino 
43732fe07f8SJohn Marino 	return temp;
43832fe07f8SJohn Marino }
43932fe07f8SJohn Marino 
44032fe07f8SJohn Marino 
44132fe07f8SJohn Marino static const char *
append_char_function(const char * name)44232fe07f8SJohn Marino append_char_function(const char *name)
44332fe07f8SJohn Marino {
44432fe07f8SJohn Marino 	struct stat stbuf;
44532fe07f8SJohn Marino 	char *expname = *name == '~' ? fn_tilde_expand(name) : NULL;
44632fe07f8SJohn Marino 	const char *rs = " ";
44732fe07f8SJohn Marino 
44832fe07f8SJohn Marino 	if (stat(expname ? expname : name, &stbuf) == -1)
44932fe07f8SJohn Marino 		goto out;
45032fe07f8SJohn Marino 	if (S_ISDIR(stbuf.st_mode))
45132fe07f8SJohn Marino 		rs = "/";
45232fe07f8SJohn Marino out:
45332fe07f8SJohn Marino 	if (expname)
45432fe07f8SJohn Marino 		el_free(expname);
45532fe07f8SJohn Marino 	return rs;
45632fe07f8SJohn Marino }
45732fe07f8SJohn Marino /*
45832fe07f8SJohn Marino  * returns list of completions for text given
45932fe07f8SJohn Marino  * non-static for readline.
46032fe07f8SJohn Marino  */
46132fe07f8SJohn Marino char ** completion_matches(const char *, char *(*)(const char *, int));
46232fe07f8SJohn Marino char **
completion_matches(const char * text,char * (* genfunc)(const char *,int))46332fe07f8SJohn Marino completion_matches(const char *text, char *(*genfunc)(const char *, int))
46432fe07f8SJohn Marino {
46532fe07f8SJohn Marino 	char **match_list = NULL, *retstr, *prevstr;
46632fe07f8SJohn Marino 	size_t match_list_len, max_equal, which, i;
46732fe07f8SJohn Marino 	size_t matches;
46832fe07f8SJohn Marino 
46932fe07f8SJohn Marino 	matches = 0;
47032fe07f8SJohn Marino 	match_list_len = 1;
47132fe07f8SJohn Marino 	while ((retstr = (*genfunc) (text, (int)matches)) != NULL) {
47232fe07f8SJohn Marino 		/* allow for list terminator here */
47332fe07f8SJohn Marino 		if (matches + 3 >= match_list_len) {
47432fe07f8SJohn Marino 			char **nmatch_list;
47532fe07f8SJohn Marino 			while (matches + 3 >= match_list_len)
47632fe07f8SJohn Marino 				match_list_len <<= 1;
47732fe07f8SJohn Marino 			nmatch_list = el_realloc(match_list,
47832fe07f8SJohn Marino 			    match_list_len * sizeof(*nmatch_list));
47932fe07f8SJohn Marino 			if (nmatch_list == NULL) {
48032fe07f8SJohn Marino 				el_free(match_list);
48132fe07f8SJohn Marino 				return NULL;
48232fe07f8SJohn Marino 			}
48332fe07f8SJohn Marino 			match_list = nmatch_list;
48432fe07f8SJohn Marino 
48532fe07f8SJohn Marino 		}
48632fe07f8SJohn Marino 		match_list[++matches] = retstr;
48732fe07f8SJohn Marino 	}
48832fe07f8SJohn Marino 
48932fe07f8SJohn Marino 	if (!match_list)
49032fe07f8SJohn Marino 		return NULL;	/* nothing found */
49132fe07f8SJohn Marino 
49232fe07f8SJohn Marino 	/* find least denominator and insert it to match_list[0] */
49332fe07f8SJohn Marino 	which = 2;
49432fe07f8SJohn Marino 	prevstr = match_list[1];
49532fe07f8SJohn Marino 	max_equal = strlen(prevstr);
49632fe07f8SJohn Marino 	for (; which <= matches; which++) {
49732fe07f8SJohn Marino 		for (i = 0; i < max_equal &&
49832fe07f8SJohn Marino 		    prevstr[i] == match_list[which][i]; i++)
49932fe07f8SJohn Marino 			continue;
50032fe07f8SJohn Marino 		max_equal = i;
50132fe07f8SJohn Marino 	}
50232fe07f8SJohn Marino 
50360ecde0cSDaniel Fojt 	retstr = el_calloc(max_equal + 1, sizeof(*retstr));
50432fe07f8SJohn Marino 	if (retstr == NULL) {
50532fe07f8SJohn Marino 		el_free(match_list);
50632fe07f8SJohn Marino 		return NULL;
50732fe07f8SJohn Marino 	}
50860ecde0cSDaniel Fojt 	(void)strlcpy(retstr, match_list[1], max_equal + 1);
50932fe07f8SJohn Marino 	match_list[0] = retstr;
51032fe07f8SJohn Marino 
51132fe07f8SJohn Marino 	/* add NULL as last pointer to the array */
51232fe07f8SJohn Marino 	match_list[matches + 1] = NULL;
51332fe07f8SJohn Marino 
51432fe07f8SJohn Marino 	return match_list;
51532fe07f8SJohn Marino }
51632fe07f8SJohn Marino 
51732fe07f8SJohn Marino /*
51832fe07f8SJohn Marino  * Sort function for qsort(). Just wrapper around strcasecmp().
51932fe07f8SJohn Marino  */
52032fe07f8SJohn Marino static int
_fn_qsort_string_compare(const void * i1,const void * i2)52132fe07f8SJohn Marino _fn_qsort_string_compare(const void *i1, const void *i2)
52232fe07f8SJohn Marino {
52332fe07f8SJohn Marino 	const char *s1 = ((const char * const *)i1)[0];
52432fe07f8SJohn Marino 	const char *s2 = ((const char * const *)i2)[0];
52532fe07f8SJohn Marino 
52632fe07f8SJohn Marino 	return strcasecmp(s1, s2);
52732fe07f8SJohn Marino }
52832fe07f8SJohn Marino 
52932fe07f8SJohn Marino /*
53032fe07f8SJohn Marino  * Display list of strings in columnar format on readline's output stream.
53132fe07f8SJohn Marino  * 'matches' is list of strings, 'num' is number of strings in 'matches',
53232fe07f8SJohn Marino  * 'width' is maximum length of string in 'matches'.
53332fe07f8SJohn Marino  *
53432fe07f8SJohn Marino  * matches[0] is not one of the match strings, but it is counted in
53532fe07f8SJohn Marino  * num, so the strings are matches[1] *through* matches[num-1].
53632fe07f8SJohn Marino  */
53732fe07f8SJohn Marino void
fn_display_match_list(EditLine * el,char ** matches,size_t num,size_t width,const char * (* app_func)(const char *))538ae19eda8Szrj fn_display_match_list(EditLine * el, char **matches, size_t num, size_t width,
539ae19eda8Szrj     const char *(*app_func) (const char *))
54032fe07f8SJohn Marino {
54132fe07f8SJohn Marino 	size_t line, lines, col, cols, thisguy;
54232fe07f8SJohn Marino 	int screenwidth = el->el_terminal.t_size.h;
543ae19eda8Szrj 	if (app_func == NULL)
544ae19eda8Szrj 		app_func = append_char_function;
54532fe07f8SJohn Marino 
54632fe07f8SJohn Marino 	/* Ignore matches[0]. Avoid 1-based array logic below. */
54732fe07f8SJohn Marino 	matches++;
54832fe07f8SJohn Marino 	num--;
54932fe07f8SJohn Marino 
55032fe07f8SJohn Marino 	/*
55132fe07f8SJohn Marino 	 * Find out how many entries can be put on one line; count
55232fe07f8SJohn Marino 	 * with one space between strings the same way it's printed.
55332fe07f8SJohn Marino 	 */
55460ecde0cSDaniel Fojt 	cols = (size_t)screenwidth / (width + 2);
55532fe07f8SJohn Marino 	if (cols == 0)
55632fe07f8SJohn Marino 		cols = 1;
55732fe07f8SJohn Marino 
55832fe07f8SJohn Marino 	/* how many lines of output, rounded up */
55932fe07f8SJohn Marino 	lines = (num + cols - 1) / cols;
56032fe07f8SJohn Marino 
56132fe07f8SJohn Marino 	/* Sort the items. */
56232fe07f8SJohn Marino 	qsort(matches, num, sizeof(char *), _fn_qsort_string_compare);
56332fe07f8SJohn Marino 
56432fe07f8SJohn Marino 	/*
56532fe07f8SJohn Marino 	 * On the ith line print elements i, i+lines, i+lines*2, etc.
56632fe07f8SJohn Marino 	 */
56732fe07f8SJohn Marino 	for (line = 0; line < lines; line++) {
56832fe07f8SJohn Marino 		for (col = 0; col < cols; col++) {
56932fe07f8SJohn Marino 			thisguy = line + col * lines;
57032fe07f8SJohn Marino 			if (thisguy >= num)
57132fe07f8SJohn Marino 				break;
572ae19eda8Szrj 			(void)fprintf(el->el_outfile, "%s%s%s",
573ae19eda8Szrj 			    col == 0 ? "" : " ", matches[thisguy],
57460ecde0cSDaniel Fojt 				(*app_func)(matches[thisguy]));
575ae19eda8Szrj 			(void)fprintf(el->el_outfile, "%-*s",
576ae19eda8Szrj 				(int) (width - strlen(matches[thisguy])), "");
57732fe07f8SJohn Marino 		}
57832fe07f8SJohn Marino 		(void)fprintf(el->el_outfile, "\n");
57932fe07f8SJohn Marino 	}
58032fe07f8SJohn Marino }
58132fe07f8SJohn Marino 
582ae19eda8Szrj static wchar_t *
find_word_to_complete(const wchar_t * cursor,const wchar_t * buffer,const wchar_t * word_break,const wchar_t * special_prefixes,size_t * length,int do_unescape)583ae19eda8Szrj find_word_to_complete(const wchar_t * cursor, const wchar_t * buffer,
584*cdf8408cSAntonio Huete Jimenez     const wchar_t * word_break, const wchar_t * special_prefixes, size_t * length,
585*cdf8408cSAntonio Huete Jimenez 	int do_unescape)
586ae19eda8Szrj {
587ae19eda8Szrj 	/* We now look backwards for the start of a filename/variable word */
588ae19eda8Szrj 	const wchar_t *ctemp = cursor;
589*cdf8408cSAntonio Huete Jimenez 	wchar_t *temp;
590ae19eda8Szrj 	size_t len;
591ae19eda8Szrj 
592ae19eda8Szrj 	/* if the cursor is placed at a slash or a quote, we need to find the
593ae19eda8Szrj 	 * word before it
594ae19eda8Szrj 	 */
595ae19eda8Szrj 	if (ctemp > buffer) {
596ae19eda8Szrj 		switch (ctemp[-1]) {
597ae19eda8Szrj 		case '\\':
598ae19eda8Szrj 		case '\'':
599ae19eda8Szrj 		case '"':
600ae19eda8Szrj 			ctemp--;
601ae19eda8Szrj 			break;
602ae19eda8Szrj 		default:
60360ecde0cSDaniel Fojt 			break;
604ae19eda8Szrj 		}
60560ecde0cSDaniel Fojt 	}
606ae19eda8Szrj 
60760ecde0cSDaniel Fojt 	for (;;) {
60860ecde0cSDaniel Fojt 		if (ctemp <= buffer)
60960ecde0cSDaniel Fojt 			break;
610*cdf8408cSAntonio Huete Jimenez 		if (ctemp - buffer >= 2 && ctemp[-2] == '\\' &&
611*cdf8408cSAntonio Huete Jimenez 		    needs_escaping(ctemp[-1])) {
61260ecde0cSDaniel Fojt 			ctemp -= 2;
61360ecde0cSDaniel Fojt 			continue;
61460ecde0cSDaniel Fojt 		}
615*cdf8408cSAntonio Huete Jimenez 		if (wcschr(word_break, ctemp[-1]))
616*cdf8408cSAntonio Huete Jimenez 			break;
61760ecde0cSDaniel Fojt 		if (special_prefixes && wcschr(special_prefixes, ctemp[-1]))
61860ecde0cSDaniel Fojt 			break;
61960ecde0cSDaniel Fojt 		ctemp--;
62060ecde0cSDaniel Fojt 	}
621ae19eda8Szrj 
62260ecde0cSDaniel Fojt 	len = (size_t) (cursor - ctemp);
62360ecde0cSDaniel Fojt 	if (len == 1 && (ctemp[0] == '\'' || ctemp[0] == '"')) {
62460ecde0cSDaniel Fojt 		len = 0;
62560ecde0cSDaniel Fojt 		ctemp++;
62660ecde0cSDaniel Fojt 	}
627ae19eda8Szrj 	*length = len;
628*cdf8408cSAntonio Huete Jimenez 	if (do_unescape) {
62960ecde0cSDaniel Fojt 		wchar_t *unescaped_word = unescape_string(ctemp, len);
63060ecde0cSDaniel Fojt 		if (unescaped_word == NULL)
63160ecde0cSDaniel Fojt 			return NULL;
63260ecde0cSDaniel Fojt 		return unescaped_word;
633ae19eda8Szrj 	}
634*cdf8408cSAntonio Huete Jimenez 	temp = el_malloc((len + 1) * sizeof(*temp));
635*cdf8408cSAntonio Huete Jimenez 	(void) wcsncpy(temp, ctemp, len);
636*cdf8408cSAntonio Huete Jimenez 	temp[len] = '\0';
637*cdf8408cSAntonio Huete Jimenez 	return temp;
638*cdf8408cSAntonio Huete Jimenez }
639ae19eda8Szrj 
64032fe07f8SJohn Marino /*
64132fe07f8SJohn Marino  * Complete the word at or before point,
64232fe07f8SJohn Marino  * 'what_to_do' says what to do with the completion.
64332fe07f8SJohn Marino  * \t   means do standard completion.
64432fe07f8SJohn Marino  * `?' means list the possible completions.
64532fe07f8SJohn Marino  * `*' means insert all of the possible completions.
64632fe07f8SJohn Marino  * `!' means to do standard completion, and list all possible completions if
64732fe07f8SJohn Marino  * there is more than one.
64832fe07f8SJohn Marino  *
64932fe07f8SJohn Marino  * Note: '*' support is not implemented
65032fe07f8SJohn Marino  *       '!' could never be invoked
65132fe07f8SJohn Marino  */
65232fe07f8SJohn Marino int
fn_complete2(EditLine * el,char * (* complete_func)(const char *,int),char ** (* attempted_completion_function)(const char *,int,int),const wchar_t * word_break,const wchar_t * special_prefixes,const char * (* app_func)(const char *),size_t query_items,int * completion_type,int * over,int * point,int * end,unsigned int flags)653*cdf8408cSAntonio Huete Jimenez fn_complete2(EditLine *el,
654*cdf8408cSAntonio Huete Jimenez     char *(*complete_func)(const char *, int),
65532fe07f8SJohn Marino     char **(*attempted_completion_function)(const char *, int, int),
65612db70c8Szrj     const wchar_t *word_break, const wchar_t *special_prefixes,
65732fe07f8SJohn Marino     const char *(*app_func)(const char *), size_t query_items,
658*cdf8408cSAntonio Huete Jimenez     int *completion_type, int *over, int *point, int *end,
659*cdf8408cSAntonio Huete Jimenez     unsigned int flags)
66032fe07f8SJohn Marino {
66112db70c8Szrj 	const LineInfoW *li;
66212db70c8Szrj 	wchar_t *temp;
66332fe07f8SJohn Marino 	char **matches;
66460ecde0cSDaniel Fojt 	char *completion;
66532fe07f8SJohn Marino 	size_t len;
66632fe07f8SJohn Marino 	int what_to_do = '\t';
66732fe07f8SJohn Marino 	int retval = CC_NORM;
668*cdf8408cSAntonio Huete Jimenez 	int do_unescape = flags & FN_QUOTE_MATCH;
66932fe07f8SJohn Marino 
67032fe07f8SJohn Marino 	if (el->el_state.lastcmd == el->el_state.thiscmd)
67132fe07f8SJohn Marino 		what_to_do = '?';
67232fe07f8SJohn Marino 
67332fe07f8SJohn Marino 	/* readline's rl_complete() has to be told what we did... */
67432fe07f8SJohn Marino 	if (completion_type != NULL)
67532fe07f8SJohn Marino 		*completion_type = what_to_do;
67632fe07f8SJohn Marino 
677*cdf8408cSAntonio Huete Jimenez 	if (!complete_func)
678*cdf8408cSAntonio Huete Jimenez 		complete_func = fn_filename_completion_function;
67932fe07f8SJohn Marino 	if (!app_func)
68032fe07f8SJohn Marino 		app_func = append_char_function;
68132fe07f8SJohn Marino 
68212db70c8Szrj 	li = el_wline(el);
683ae19eda8Szrj 	temp = find_word_to_complete(li->cursor,
684*cdf8408cSAntonio Huete Jimenez 	    li->buffer, word_break, special_prefixes, &len, do_unescape);
685ae19eda8Szrj 	if (temp == NULL)
686ae19eda8Szrj 		goto out;
68732fe07f8SJohn Marino 
68832fe07f8SJohn Marino 	/* these can be used by function called in completion_matches() */
68932fe07f8SJohn Marino 	/* or (*attempted_completion_function)() */
69012db70c8Szrj 	if (point != NULL)
69132fe07f8SJohn Marino 		*point = (int)(li->cursor - li->buffer);
69232fe07f8SJohn Marino 	if (end != NULL)
69332fe07f8SJohn Marino 		*end = (int)(li->lastchar - li->buffer);
69432fe07f8SJohn Marino 
69532fe07f8SJohn Marino 	if (attempted_completion_function) {
69632fe07f8SJohn Marino 		int cur_off = (int)(li->cursor - li->buffer);
69732fe07f8SJohn Marino 		matches = (*attempted_completion_function)(
69832fe07f8SJohn Marino 		    ct_encode_string(temp, &el->el_scratch),
69932fe07f8SJohn Marino 		    cur_off - (int)len, cur_off);
70032fe07f8SJohn Marino 	} else
70112db70c8Szrj 		matches = NULL;
70232fe07f8SJohn Marino 	if (!attempted_completion_function ||
70332fe07f8SJohn Marino 	    (over != NULL && !*over && !matches))
70432fe07f8SJohn Marino 		matches = completion_matches(
705*cdf8408cSAntonio Huete Jimenez 		    ct_encode_string(temp, &el->el_scratch), complete_func);
70632fe07f8SJohn Marino 
70732fe07f8SJohn Marino 	if (over != NULL)
70832fe07f8SJohn Marino 		*over = 0;
70932fe07f8SJohn Marino 
71060ecde0cSDaniel Fojt 	if (matches == NULL) {
71160ecde0cSDaniel Fojt 		goto out;
71260ecde0cSDaniel Fojt 	}
71332fe07f8SJohn Marino 	int i;
71432fe07f8SJohn Marino 	size_t matches_num, maxlen, match_len, match_display=1;
715ae19eda8Szrj 	int single_match = matches[2] == NULL &&
716ae19eda8Szrj 		(matches[1] == NULL || strcmp(matches[0], matches[1]) == 0);
71732fe07f8SJohn Marino 
71832fe07f8SJohn Marino 	retval = CC_REFRESH;
719ae19eda8Szrj 
720ae19eda8Szrj 	if (matches[0][0] != '\0') {
721ae19eda8Szrj 		el_deletestr(el, (int)len);
722*cdf8408cSAntonio Huete Jimenez 		if (flags & FN_QUOTE_MATCH)
72360ecde0cSDaniel Fojt 			completion = escape_filename(el, matches[0],
72460ecde0cSDaniel Fojt 			    single_match, app_func);
72560ecde0cSDaniel Fojt 		else
72660ecde0cSDaniel Fojt 			completion = strdup(matches[0]);
72760ecde0cSDaniel Fojt 		if (completion == NULL)
728*cdf8408cSAntonio Huete Jimenez 			goto out2;
72960ecde0cSDaniel Fojt 
73032fe07f8SJohn Marino 		/*
73160ecde0cSDaniel Fojt 		 * Replace the completed string with the common part of
73260ecde0cSDaniel Fojt 		 * all possible matches if there is a possible completion.
73332fe07f8SJohn Marino 		 */
73412db70c8Szrj 		el_winsertstr(el,
73560ecde0cSDaniel Fojt 		    ct_decode_string(completion, &el->el_scratch));
73660ecde0cSDaniel Fojt 
737*cdf8408cSAntonio Huete Jimenez 		if (single_match && attempted_completion_function &&
738*cdf8408cSAntonio Huete Jimenez 		    !(flags & FN_QUOTE_MATCH))
739*cdf8408cSAntonio Huete Jimenez 		{
74060ecde0cSDaniel Fojt 			/*
74160ecde0cSDaniel Fojt 			 * We found an exact match. Add a space after
74260ecde0cSDaniel Fojt 			 * it, unless we do filename completion and the
74360ecde0cSDaniel Fojt 			 * object is a directory. Also do necessary
74460ecde0cSDaniel Fojt 			 * escape quoting
74560ecde0cSDaniel Fojt 			 */
74660ecde0cSDaniel Fojt 			el_winsertstr(el, ct_decode_string(
74760ecde0cSDaniel Fojt 			    (*app_func)(completion), &el->el_scratch));
74832fe07f8SJohn Marino 		}
74960ecde0cSDaniel Fojt 		free(completion);
750ae19eda8Szrj 	}
75132fe07f8SJohn Marino 
75232fe07f8SJohn Marino 
753ae19eda8Szrj 	if (!single_match && (what_to_do == '!' || what_to_do == '?')) {
75432fe07f8SJohn Marino 		/*
75532fe07f8SJohn Marino 		 * More than one match and requested to list possible
75632fe07f8SJohn Marino 		 * matches.
75732fe07f8SJohn Marino 		 */
75832fe07f8SJohn Marino 
75932fe07f8SJohn Marino 		for(i = 1, maxlen = 0; matches[i]; i++) {
76032fe07f8SJohn Marino 			match_len = strlen(matches[i]);
76132fe07f8SJohn Marino 			if (match_len > maxlen)
76232fe07f8SJohn Marino 				maxlen = match_len;
76332fe07f8SJohn Marino 		}
76432fe07f8SJohn Marino 		/* matches[1] through matches[i-1] are available */
76532fe07f8SJohn Marino 		matches_num = (size_t)(i - 1);
76632fe07f8SJohn Marino 
76732fe07f8SJohn Marino 		/* newline to get on next line from command line */
76832fe07f8SJohn Marino 		(void)fprintf(el->el_outfile, "\n");
76932fe07f8SJohn Marino 
77032fe07f8SJohn Marino 		/*
77132fe07f8SJohn Marino 		 * If there are too many items, ask user for display
77232fe07f8SJohn Marino 		 * confirmation.
77332fe07f8SJohn Marino 		 */
77432fe07f8SJohn Marino 		if (matches_num > query_items) {
77532fe07f8SJohn Marino 			(void)fprintf(el->el_outfile,
77632fe07f8SJohn Marino 			    "Display all %zu possibilities? (y or n) ",
77732fe07f8SJohn Marino 			    matches_num);
77832fe07f8SJohn Marino 			(void)fflush(el->el_outfile);
77932fe07f8SJohn Marino 			if (getc(stdin) != 'y')
78032fe07f8SJohn Marino 				match_display = 0;
78132fe07f8SJohn Marino 			(void)fprintf(el->el_outfile, "\n");
78232fe07f8SJohn Marino 		}
78332fe07f8SJohn Marino 
78432fe07f8SJohn Marino 		if (match_display) {
78532fe07f8SJohn Marino 			/*
78632fe07f8SJohn Marino 			 * Interface of this function requires the
78732fe07f8SJohn Marino 			 * strings be matches[1..num-1] for compat.
78832fe07f8SJohn Marino 			 * We have matches_num strings not counting
78932fe07f8SJohn Marino 			 * the prefix in matches[0], so we need to
79032fe07f8SJohn Marino 			 * add 1 to matches_num for the call.
79132fe07f8SJohn Marino 			 */
79232fe07f8SJohn Marino 			fn_display_match_list(el, matches,
793ae19eda8Szrj 			    matches_num+1, maxlen, app_func);
79432fe07f8SJohn Marino 		}
79532fe07f8SJohn Marino 		retval = CC_REDISPLAY;
79632fe07f8SJohn Marino 	} else if (matches[0][0]) {
79732fe07f8SJohn Marino 		/*
79832fe07f8SJohn Marino 		 * There was some common match, but the name was
79932fe07f8SJohn Marino 		 * not complete enough. Next tab will print possible
80032fe07f8SJohn Marino 		 * completions.
80132fe07f8SJohn Marino 		 */
80232fe07f8SJohn Marino 		el_beep(el);
80332fe07f8SJohn Marino 	} else {
80432fe07f8SJohn Marino 		/* lcd is not a valid object - further specification */
80532fe07f8SJohn Marino 		/* is needed */
80632fe07f8SJohn Marino 		el_beep(el);
80732fe07f8SJohn Marino 		retval = CC_NORM;
80832fe07f8SJohn Marino 	}
80932fe07f8SJohn Marino 
81032fe07f8SJohn Marino 	/* free elements of array and the array itself */
811*cdf8408cSAntonio Huete Jimenez out2:
81232fe07f8SJohn Marino 	for (i = 0; matches[i]; i++)
81332fe07f8SJohn Marino 		el_free(matches[i]);
81432fe07f8SJohn Marino 	el_free(matches);
81532fe07f8SJohn Marino 	matches = NULL;
816ae19eda8Szrj 
817ae19eda8Szrj out:
81832fe07f8SJohn Marino 	el_free(temp);
81932fe07f8SJohn Marino 	return retval;
82032fe07f8SJohn Marino }
82132fe07f8SJohn Marino 
822*cdf8408cSAntonio Huete Jimenez int
fn_complete(EditLine * el,char * (* complete_func)(const char *,int),char ** (* attempted_completion_function)(const char *,int,int),const wchar_t * word_break,const wchar_t * special_prefixes,const char * (* app_func)(const char *),size_t query_items,int * completion_type,int * over,int * point,int * end)823*cdf8408cSAntonio Huete Jimenez fn_complete(EditLine *el,
824*cdf8408cSAntonio Huete Jimenez     char *(*complete_func)(const char *, int),
825*cdf8408cSAntonio Huete Jimenez     char **(*attempted_completion_function)(const char *, int, int),
826*cdf8408cSAntonio Huete Jimenez     const wchar_t *word_break, const wchar_t *special_prefixes,
827*cdf8408cSAntonio Huete Jimenez     const char *(*app_func)(const char *), size_t query_items,
828*cdf8408cSAntonio Huete Jimenez     int *completion_type, int *over, int *point, int *end)
829*cdf8408cSAntonio Huete Jimenez {
830*cdf8408cSAntonio Huete Jimenez 	return fn_complete2(el, complete_func, attempted_completion_function,
831*cdf8408cSAntonio Huete Jimenez 	    word_break, special_prefixes, app_func, query_items,
832*cdf8408cSAntonio Huete Jimenez 	    completion_type, over, point, end,
833*cdf8408cSAntonio Huete Jimenez 	    attempted_completion_function ? 0 : FN_QUOTE_MATCH);
834*cdf8408cSAntonio Huete Jimenez }
835*cdf8408cSAntonio Huete Jimenez 
83632fe07f8SJohn Marino /*
83732fe07f8SJohn Marino  * el-compatible wrapper around rl_complete; needed for key binding
83832fe07f8SJohn Marino  */
83932fe07f8SJohn Marino /* ARGSUSED */
84032fe07f8SJohn Marino unsigned char
_el_fn_complete(EditLine * el,int ch)84132fe07f8SJohn Marino _el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
84232fe07f8SJohn Marino {
84332fe07f8SJohn Marino 	return (unsigned char)fn_complete(el, NULL, NULL,
84432fe07f8SJohn Marino 	    break_chars, NULL, NULL, (size_t)100,
84532fe07f8SJohn Marino 	    NULL, NULL, NULL, NULL);
84632fe07f8SJohn Marino }
847