Home | History | Annotate | Line # | Download | only in libedit
filecomplete.c revision 1.56
      1 /*	$NetBSD: filecomplete.c,v 1.56 2019/07/23 10:18:52 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jaromir Dolecek.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "config.h"
     33 #if !defined(lint) && !defined(SCCSID)
     34 __RCSID("$NetBSD: filecomplete.c,v 1.56 2019/07/23 10:18:52 christos Exp $");
     35 #endif /* not lint && not SCCSID */
     36 
     37 #include <sys/types.h>
     38 #include <sys/stat.h>
     39 #include <dirent.h>
     40 #include <errno.h>
     41 #include <fcntl.h>
     42 #include <limits.h>
     43 #include <pwd.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 
     49 #include "el.h"
     50 #include "filecomplete.h"
     51 
     52 static const wchar_t break_chars[] = L" \t\n\"\\'`@$><=;|&{(";
     53 
     54 /********************************/
     55 /* completion functions */
     56 
     57 /*
     58  * does tilde expansion of strings of type ``~user/foo''
     59  * if ``user'' isn't valid user name or ``txt'' doesn't start
     60  * w/ '~', returns pointer to strdup()ed copy of ``txt''
     61  *
     62  * it's the caller's responsibility to free() the returned string
     63  */
     64 char *
     65 fn_tilde_expand(const char *txt)
     66 {
     67 #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
     68 	struct passwd pwres;
     69 	char pwbuf[1024];
     70 #endif
     71 	struct passwd *pass;
     72 	char *temp;
     73 	size_t len = 0;
     74 
     75 	if (txt[0] != '~')
     76 		return strdup(txt);
     77 
     78 	temp = strchr(txt + 1, '/');
     79 	if (temp == NULL) {
     80 		temp = strdup(txt + 1);
     81 		if (temp == NULL)
     82 			return NULL;
     83 	} else {
     84 		/* text until string after slash */
     85 		len = (size_t)(temp - txt + 1);
     86 		temp = el_calloc(len, sizeof(*temp));
     87 		if (temp == NULL)
     88 			return NULL;
     89 		(void)strncpy(temp, txt + 1, len - 2);
     90 		temp[len - 2] = '\0';
     91 	}
     92 	if (temp[0] == 0) {
     93 #ifdef HAVE_GETPW_R_POSIX
     94 		if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
     95 		    &pass) != 0)
     96 			pass = NULL;
     97 #elif HAVE_GETPW_R_DRAFT
     98 		pass = getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf));
     99 #else
    100 		pass = getpwuid(getuid());
    101 #endif
    102 	} else {
    103 #ifdef HAVE_GETPW_R_POSIX
    104 		if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
    105 			pass = NULL;
    106 #elif HAVE_GETPW_R_DRAFT
    107 		pass = getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf));
    108 #else
    109 		pass = getpwnam(temp);
    110 #endif
    111 	}
    112 	el_free(temp);		/* value no more needed */
    113 	if (pass == NULL)
    114 		return strdup(txt);
    115 
    116 	/* update pointer txt to point at string immedially following */
    117 	/* first slash */
    118 	txt += len;
    119 
    120 	len = strlen(pass->pw_dir) + 1 + strlen(txt) + 1;
    121 	temp = el_calloc(len, sizeof(*temp));
    122 	if (temp == NULL)
    123 		return NULL;
    124 	(void)snprintf(temp, len, "%s/%s", pass->pw_dir, txt);
    125 
    126 	return temp;
    127 }
    128 
    129 static int
    130 needs_escaping(char c)
    131 {
    132 	switch (c) {
    133 	case '\'':
    134 	case '"':
    135 	case '(':
    136 	case ')':
    137 	case '\\':
    138 	case '<':
    139 	case '>':
    140 	case '$':
    141 	case '#':
    142 	case ' ':
    143 	case '\n':
    144 	case '\t':
    145 	case '?':
    146 	case ';':
    147 	case '`':
    148 	case '@':
    149 	case '=':
    150 	case '|':
    151 	case '{':
    152 	case '}':
    153 	case '&':
    154 	case '*':
    155 	case '[':
    156 		return 1;
    157 	default:
    158 		return 0;
    159 	}
    160 }
    161 
    162 static int
    163 needs_dquote_escaping(char c)
    164 {
    165 	switch (c) {
    166 	case '"':
    167 	case '\\':
    168 	case '`':
    169 	case '$':
    170 		return 1;
    171 	default:
    172 		return 0;
    173 	}
    174 }
    175 
    176 
    177 static wchar_t *
    178 unescape_string(const wchar_t *string, size_t length)
    179 {
    180 	size_t i;
    181 	size_t j = 0;
    182 	wchar_t *unescaped = el_calloc(length + 1, sizeof(*string));
    183 	if (unescaped == NULL)
    184 		return NULL;
    185 	for (i = 0; i < length ; i++) {
    186 		if (string[i] == '\\')
    187 			continue;
    188 		unescaped[j++] = string[i];
    189 	}
    190 	unescaped[j] = 0;
    191 	return unescaped;
    192 }
    193 
    194 static char *
    195 escape_filename(EditLine * el, const char *filename)
    196 {
    197 	size_t original_len = 0;
    198 	size_t escaped_character_count = 0;
    199 	size_t offset = 0;
    200 	size_t newlen;
    201 	const char *s;
    202 	char c;
    203 	size_t s_quoted = 0;	/* does the input contain a single quote */
    204 	size_t d_quoted = 0;	/* does the input contain a double quote */
    205 	char *escaped_str;
    206 	wchar_t *temp = el->el_line.buffer;
    207 
    208 	if (filename == NULL)
    209 		return NULL;
    210 
    211 	while (temp != el->el_line.cursor) {
    212 		/*
    213 		 * If we see a single quote but have not seen a double quote
    214 		 * so far set/unset s_quote
    215 		 */
    216 		if (temp[0] == '\'' && !d_quoted)
    217 			s_quoted = !s_quoted;
    218 		/*
    219 		 * vice versa to the above condition
    220 		 */
    221 		else if (temp[0] == '"' && !s_quoted)
    222 			d_quoted = !d_quoted;
    223 		temp++;
    224 	}
    225 
    226 	/* Count number of special characters so that we can calculate
    227 	 * number of extra bytes needed in the new string
    228 	 */
    229 	for (s = filename; *s; s++, original_len++) {
    230 		c = *s;
    231 		/* Inside a single quote only single quotes need escaping */
    232 		if (s_quoted && c == '\'') {
    233 			escaped_character_count += 3;
    234 			continue;
    235 		}
    236 		/* Inside double quotes only ", \, ` and $ need escaping */
    237 		if (d_quoted && needs_dquote_escaping(c)) {
    238 			escaped_character_count++;
    239 			continue;
    240 		}
    241 		if (!s_quoted && !d_quoted && needs_escaping(c))
    242 			escaped_character_count++;
    243 	}
    244 
    245 	newlen = original_len + escaped_character_count + 1;
    246 	if (s_quoted || d_quoted)
    247 		newlen++;
    248 
    249 	if ((escaped_str = el_malloc(newlen)) == NULL)
    250 		return NULL;
    251 
    252 	for (s = filename; *s; s++) {
    253 		c = *s;
    254 		if (!needs_escaping(c)) {
    255 			/* no escaping is required continue as usual */
    256 			escaped_str[offset++] = c;
    257 			continue;
    258 		}
    259 
    260 		/* single quotes inside single quotes require special handling */
    261 		if (c == '\'' && s_quoted) {
    262 			escaped_str[offset++] = '\'';
    263 			escaped_str[offset++] = '\\';
    264 			escaped_str[offset++] = '\'';
    265 			escaped_str[offset++] = '\'';
    266 			continue;
    267 		}
    268 
    269 		/* Otherwise no escaping needed inside single quotes */
    270 		if (s_quoted) {
    271 			escaped_str[offset++] = c;
    272 			continue;
    273 		}
    274 
    275 		/* No escaping needed inside a double quoted string either
    276 		 * unless we see a '$', '\', '`', or '"' (itself)
    277 		 */
    278 		if (d_quoted && !needs_dquote_escaping(c)) {
    279 			escaped_str[offset++] = c;
    280 			continue;
    281 		}
    282 
    283 		/* If we reach here that means escaping is actually needed */
    284 		escaped_str[offset++] = '\\';
    285 		escaped_str[offset++] = c;
    286 	}
    287 
    288 	/* close the quotes */
    289 	if (s_quoted)
    290 		escaped_str[offset++] = '\'';
    291 	else if (d_quoted)
    292 		escaped_str[offset++] = '"';
    293 
    294 	escaped_str[offset] = 0;
    295 	return escaped_str;
    296 }
    297 
    298 /*
    299  * return first found file name starting by the ``text'' or NULL if no
    300  * such file can be found
    301  * value of ``state'' is ignored
    302  *
    303  * it's the caller's responsibility to free the returned string
    304  */
    305 char *
    306 fn_filename_completion_function(const char *text, int state)
    307 {
    308 	static DIR *dir = NULL;
    309 	static char *filename = NULL, *dirname = NULL, *dirpath = NULL;
    310 	static size_t filename_len = 0;
    311 	struct dirent *entry;
    312 	char *temp;
    313 	size_t len;
    314 
    315 	if (state == 0 || dir == NULL) {
    316 		temp = strrchr(text, '/');
    317 		if (temp) {
    318 			char *nptr;
    319 			temp++;
    320 			nptr = el_realloc(filename, (strlen(temp) + 1) *
    321 			    sizeof(*nptr));
    322 			if (nptr == NULL) {
    323 				el_free(filename);
    324 				filename = NULL;
    325 				return NULL;
    326 			}
    327 			filename = nptr;
    328 			(void)strcpy(filename, temp);
    329 			len = (size_t)(temp - text);	/* including last slash */
    330 
    331 			nptr = el_realloc(dirname, (len + 1) *
    332 			    sizeof(*nptr));
    333 			if (nptr == NULL) {
    334 				el_free(dirname);
    335 				dirname = NULL;
    336 				return NULL;
    337 			}
    338 			dirname = nptr;
    339 			(void)strncpy(dirname, text, len);
    340 			dirname[len] = '\0';
    341 		} else {
    342 			el_free(filename);
    343 			if (*text == 0)
    344 				filename = NULL;
    345 			else {
    346 				filename = strdup(text);
    347 				if (filename == NULL)
    348 					return NULL;
    349 			}
    350 			el_free(dirname);
    351 			dirname = NULL;
    352 		}
    353 
    354 		if (dir != NULL) {
    355 			(void)closedir(dir);
    356 			dir = NULL;
    357 		}
    358 
    359 		/* support for ``~user'' syntax */
    360 
    361 		el_free(dirpath);
    362 		dirpath = NULL;
    363 		if (dirname == NULL) {
    364 			if ((dirname = strdup("")) == NULL)
    365 				return NULL;
    366 			dirpath = strdup("./");
    367 		} else if (*dirname == '~')
    368 			dirpath = fn_tilde_expand(dirname);
    369 		else
    370 			dirpath = strdup(dirname);
    371 
    372 		if (dirpath == NULL)
    373 			return NULL;
    374 
    375 		dir = opendir(dirpath);
    376 		if (!dir)
    377 			return NULL;	/* cannot open the directory */
    378 
    379 		/* will be used in cycle */
    380 		filename_len = filename ? strlen(filename) : 0;
    381 	}
    382 
    383 	/* find the match */
    384 	while ((entry = readdir(dir)) != NULL) {
    385 		/* skip . and .. */
    386 		if (entry->d_name[0] == '.' && (!entry->d_name[1]
    387 		    || (entry->d_name[1] == '.' && !entry->d_name[2])))
    388 			continue;
    389 		if (filename_len == 0)
    390 			break;
    391 		/* otherwise, get first entry where first */
    392 		/* filename_len characters are equal	  */
    393 		if (entry->d_name[0] == filename[0]
    394 #if HAVE_STRUCT_DIRENT_D_NAMLEN
    395 		    && entry->d_namlen >= filename_len
    396 #else
    397 		    && strlen(entry->d_name) >= filename_len
    398 #endif
    399 		    && strncmp(entry->d_name, filename,
    400 			filename_len) == 0)
    401 			break;
    402 	}
    403 
    404 	if (entry) {		/* match found */
    405 
    406 #if HAVE_STRUCT_DIRENT_D_NAMLEN
    407 		len = entry->d_namlen;
    408 #else
    409 		len = strlen(entry->d_name);
    410 #endif
    411 
    412 		len = strlen(dirname) + len + 1;
    413 		temp = el_calloc(len, sizeof(*temp));
    414 		if (temp == NULL)
    415 			return NULL;
    416 		(void)snprintf(temp, len, "%s%s", dirname, entry->d_name);
    417 	} else {
    418 		(void)closedir(dir);
    419 		dir = NULL;
    420 		temp = NULL;
    421 	}
    422 
    423 	return temp;
    424 }
    425 
    426 
    427 static const char *
    428 append_char_function(const char *name)
    429 {
    430 	struct stat stbuf;
    431 	char *expname = *name == '~' ? fn_tilde_expand(name) : NULL;
    432 	const char *rs = " ";
    433 
    434 	if (stat(expname ? expname : name, &stbuf) == -1)
    435 		goto out;
    436 	if (S_ISDIR(stbuf.st_mode))
    437 		rs = "/";
    438 out:
    439 	if (expname)
    440 		el_free(expname);
    441 	return rs;
    442 }
    443 /*
    444  * returns list of completions for text given
    445  * non-static for readline.
    446  */
    447 char ** completion_matches(const char *, char *(*)(const char *, int));
    448 char **
    449 completion_matches(const char *text, char *(*genfunc)(const char *, int))
    450 {
    451 	char **match_list = NULL, *retstr, *prevstr;
    452 	size_t match_list_len, max_equal, which, i;
    453 	size_t matches;
    454 
    455 	matches = 0;
    456 	match_list_len = 1;
    457 	while ((retstr = (*genfunc) (text, (int)matches)) != NULL) {
    458 		/* allow for list terminator here */
    459 		if (matches + 3 >= match_list_len) {
    460 			char **nmatch_list;
    461 			while (matches + 3 >= match_list_len)
    462 				match_list_len <<= 1;
    463 			nmatch_list = el_realloc(match_list,
    464 			    match_list_len * sizeof(*nmatch_list));
    465 			if (nmatch_list == NULL) {
    466 				el_free(match_list);
    467 				return NULL;
    468 			}
    469 			match_list = nmatch_list;
    470 
    471 		}
    472 		match_list[++matches] = retstr;
    473 	}
    474 
    475 	if (!match_list)
    476 		return NULL;	/* nothing found */
    477 
    478 	/* find least denominator and insert it to match_list[0] */
    479 	which = 2;
    480 	prevstr = match_list[1];
    481 	max_equal = strlen(prevstr);
    482 	for (; which <= matches; which++) {
    483 		for (i = 0; i < max_equal &&
    484 		    prevstr[i] == match_list[which][i]; i++)
    485 			continue;
    486 		max_equal = i;
    487 	}
    488 
    489 	retstr = el_calloc(max_equal + 1, sizeof(*retstr));
    490 	if (retstr == NULL) {
    491 		el_free(match_list);
    492 		return NULL;
    493 	}
    494 	(void)strncpy(retstr, match_list[1], max_equal);
    495 	retstr[max_equal] = '\0';
    496 	match_list[0] = retstr;
    497 
    498 	/* add NULL as last pointer to the array */
    499 	match_list[matches + 1] = NULL;
    500 
    501 	return match_list;
    502 }
    503 
    504 /*
    505  * Sort function for qsort(). Just wrapper around strcasecmp().
    506  */
    507 static int
    508 _fn_qsort_string_compare(const void *i1, const void *i2)
    509 {
    510 	const char *s1 = ((const char * const *)i1)[0];
    511 	const char *s2 = ((const char * const *)i2)[0];
    512 
    513 	return strcasecmp(s1, s2);
    514 }
    515 
    516 /*
    517  * Display list of strings in columnar format on readline's output stream.
    518  * 'matches' is list of strings, 'num' is number of strings in 'matches',
    519  * 'width' is maximum length of string in 'matches'.
    520  *
    521  * matches[0] is not one of the match strings, but it is counted in
    522  * num, so the strings are matches[1] *through* matches[num-1].
    523  */
    524 void
    525 fn_display_match_list(EditLine * el, char **matches, size_t num, size_t width,
    526     const char *(*app_func) (const char *))
    527 {
    528 	size_t line, lines, col, cols, thisguy;
    529 	int screenwidth = el->el_terminal.t_size.h;
    530 	if (app_func == NULL)
    531 		app_func = append_char_function;
    532 
    533 	/* Ignore matches[0]. Avoid 1-based array logic below. */
    534 	matches++;
    535 	num--;
    536 
    537 	/*
    538 	 * Find out how many entries can be put on one line; count
    539 	 * with one space between strings the same way it's printed.
    540 	 */
    541 	cols = (size_t)screenwidth / (width + 1);
    542 	if (cols == 0)
    543 		cols = 1;
    544 
    545 	/* how many lines of output, rounded up */
    546 	lines = (num + cols - 1) / cols;
    547 
    548 	/* Sort the items. */
    549 	qsort(matches, num, sizeof(char *), _fn_qsort_string_compare);
    550 
    551 	/*
    552 	 * On the ith line print elements i, i+lines, i+lines*2, etc.
    553 	 */
    554 	for (line = 0; line < lines; line++) {
    555 		for (col = 0; col < cols; col++) {
    556 			thisguy = line + col * lines;
    557 			if (thisguy >= num)
    558 				break;
    559 			(void)fprintf(el->el_outfile, "%s%s%s",
    560 			    col == 0 ? "" : " ", matches[thisguy],
    561 				append_char_function(matches[thisguy]));
    562 			(void)fprintf(el->el_outfile, "%-*s",
    563 				(int) (width - strlen(matches[thisguy])), "");
    564 		}
    565 		(void)fprintf(el->el_outfile, "\n");
    566 	}
    567 }
    568 
    569 static wchar_t *
    570 find_word_to_complete(const wchar_t * cursor, const wchar_t * buffer,
    571     const wchar_t * word_break, const wchar_t * special_prefixes, size_t * length)
    572 {
    573 	/* We now look backwards for the start of a filename/variable word */
    574 	const wchar_t *ctemp = cursor;
    575 	size_t len;
    576 
    577 	/* if the cursor is placed at a slash or a quote, we need to find the
    578 	 * word before it
    579 	 */
    580 	if (ctemp > buffer) {
    581 		switch (ctemp[-1]) {
    582 		case '\\':
    583 		case '\'':
    584 		case '"':
    585 			ctemp--;
    586 			break;
    587 		default:
    588 			break;
    589 		}
    590 	}
    591 
    592 	for (;;) {
    593 		if (ctemp <= buffer)
    594 			break;
    595 		if (wcschr(word_break, ctemp[-1])) {
    596 			if (ctemp - buffer >= 2 && ctemp[-2] == '\\') {
    597 				ctemp -= 2;
    598 				continue;
    599 			} else
    600 				break;
    601 		}
    602 		if (special_prefixes && wcschr(special_prefixes, ctemp[-1]))
    603 			break;
    604 		ctemp--;
    605 	}
    606 
    607 	len = (size_t) (cursor - ctemp);
    608 	*length = len;
    609 	wchar_t *unescaped_word = unescape_string(ctemp, len);
    610 	if (unescaped_word == NULL)
    611 		return NULL;
    612 	return unescaped_word;
    613 }
    614 
    615 /*
    616  * Complete the word at or before point,
    617  * 'what_to_do' says what to do with the completion.
    618  * \t   means do standard completion.
    619  * `?' means list the possible completions.
    620  * `*' means insert all of the possible completions.
    621  * `!' means to do standard completion, and list all possible completions if
    622  * there is more than one.
    623  *
    624  * Note: '*' support is not implemented
    625  *       '!' could never be invoked
    626  */
    627 int
    628 fn_complete(EditLine *el,
    629 	char *(*complet_func)(const char *, int),
    630 	char **(*attempted_completion_function)(const char *, int, int),
    631 	const wchar_t *word_break, const wchar_t *special_prefixes,
    632 	const char *(*app_func)(const char *), size_t query_items,
    633 	int *completion_type, int *over, int *point, int *end)
    634 {
    635 	const LineInfoW *li;
    636 	wchar_t *temp;
    637 	char **matches;
    638 	char *completion;
    639 	size_t len;
    640 	int what_to_do = '\t';
    641 	int retval = CC_NORM;
    642 
    643 	if (el->el_state.lastcmd == el->el_state.thiscmd)
    644 		what_to_do = '?';
    645 
    646 	/* readline's rl_complete() has to be told what we did... */
    647 	if (completion_type != NULL)
    648 		*completion_type = what_to_do;
    649 
    650 	if (!complet_func)
    651 		complet_func = fn_filename_completion_function;
    652 	if (!app_func)
    653 		app_func = append_char_function;
    654 
    655 	li = el_wline(el);
    656 	temp = find_word_to_complete(li->cursor,
    657 	    li->buffer, word_break, special_prefixes, &len);
    658 	if (temp == NULL)
    659 		goto out;
    660 
    661 	/* these can be used by function called in completion_matches() */
    662 	/* or (*attempted_completion_function)() */
    663 	if (point != NULL)
    664 		*point = (int)(li->cursor - li->buffer);
    665 	if (end != NULL)
    666 		*end = (int)(li->lastchar - li->buffer);
    667 
    668 	if (attempted_completion_function) {
    669 		int cur_off = (int)(li->cursor - li->buffer);
    670 		matches = (*attempted_completion_function)(
    671 		    ct_encode_string(temp, &el->el_scratch),
    672 		    cur_off - (int)len, cur_off);
    673 	} else
    674 		matches = NULL;
    675 	if (!attempted_completion_function ||
    676 	    (over != NULL && !*over && !matches))
    677 		matches = completion_matches(
    678 		    ct_encode_string(temp, &el->el_scratch), complet_func);
    679 
    680 	if (over != NULL)
    681 		*over = 0;
    682 
    683 	if (matches) {
    684 		int i;
    685 		size_t matches_num, maxlen, match_len, match_display=1;
    686 		int single_match = matches[2] == NULL &&
    687 			(matches[1] == NULL || strcmp(matches[0], matches[1]) == 0);
    688 
    689 		retval = CC_REFRESH;
    690 
    691 		if (matches[0][0] != '\0') {
    692 			el_deletestr(el, (int) len);
    693 			if (!attempted_completion_function)
    694 				completion = escape_filename(el, matches[0]);
    695 			else
    696 				completion = strdup(matches[0]);
    697 			if (completion == NULL)
    698 				goto out;
    699 			if (single_match) {
    700 				/*
    701 				 * We found exact match. Add a space after
    702 				 * it, unless we do filename completion and the
    703 				 * object is a directory. Also do necessary escape quoting
    704 				 */
    705 				el_winsertstr(el,
    706 					ct_decode_string(completion, &el->el_scratch));
    707 				el_winsertstr(el,
    708 						ct_decode_string((*app_func)(completion),
    709 							&el->el_scratch));
    710 			} else {
    711 				/*
    712 				 * Only replace the completed string with common part of
    713 				 * possible matches if there is possible completion.
    714 				 */
    715 				el_winsertstr(el,
    716 					ct_decode_string(completion, &el->el_scratch));
    717 			}
    718 			free(completion);
    719 		}
    720 
    721 
    722 		if (!single_match && (what_to_do == '!' || what_to_do == '?')) {
    723 			/*
    724 			 * More than one match and requested to list possible
    725 			 * matches.
    726 			 */
    727 
    728 			for(i = 1, maxlen = 0; matches[i]; i++) {
    729 				match_len = strlen(matches[i]);
    730 				if (match_len > maxlen)
    731 					maxlen = match_len;
    732 			}
    733 			/* matches[1] through matches[i-1] are available */
    734 			matches_num = (size_t)(i - 1);
    735 
    736 			/* newline to get on next line from command line */
    737 			(void)fprintf(el->el_outfile, "\n");
    738 
    739 			/*
    740 			 * If there are too many items, ask user for display
    741 			 * confirmation.
    742 			 */
    743 			if (matches_num > query_items) {
    744 				(void)fprintf(el->el_outfile,
    745 				    "Display all %zu possibilities? (y or n) ",
    746 				    matches_num);
    747 				(void)fflush(el->el_outfile);
    748 				if (getc(stdin) != 'y')
    749 					match_display = 0;
    750 				(void)fprintf(el->el_outfile, "\n");
    751 			}
    752 
    753 			if (match_display) {
    754 				/*
    755 				 * Interface of this function requires the
    756 				 * strings be matches[1..num-1] for compat.
    757 				 * We have matches_num strings not counting
    758 				 * the prefix in matches[0], so we need to
    759 				 * add 1 to matches_num for the call.
    760 				 */
    761 				fn_display_match_list(el, matches,
    762 				    matches_num+1, maxlen, app_func);
    763 			}
    764 			retval = CC_REDISPLAY;
    765 		} else if (matches[0][0]) {
    766 			/*
    767 			 * There was some common match, but the name was
    768 			 * not complete enough. Next tab will print possible
    769 			 * completions.
    770 			 */
    771 			el_beep(el);
    772 		} else {
    773 			/* lcd is not a valid object - further specification */
    774 			/* is needed */
    775 			el_beep(el);
    776 			retval = CC_NORM;
    777 		}
    778 
    779 		/* free elements of array and the array itself */
    780 		for (i = 0; matches[i]; i++)
    781 			el_free(matches[i]);
    782 		el_free(matches);
    783 		matches = NULL;
    784 	}
    785 
    786 out:
    787 	el_free(temp);
    788 	return retval;
    789 }
    790 
    791 /*
    792  * el-compatible wrapper around rl_complete; needed for key binding
    793  */
    794 /* ARGSUSED */
    795 unsigned char
    796 _el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
    797 {
    798 	return (unsigned char)fn_complete(el, NULL, NULL,
    799 	    break_chars, NULL, NULL, (size_t)100,
    800 	    NULL, NULL, NULL, NULL);
    801 }
    802