Home | History | Annotate | Line # | Download | only in libedit
filecomplete.c revision 1.31.18.1
      1  1.31.18.1       tls /*	$NetBSD: filecomplete.c,v 1.31.18.1 2014/08/10 06:51:57 tls Exp $	*/
      2        1.1       dsl 
      3        1.1       dsl /*-
      4        1.1       dsl  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5        1.1       dsl  * All rights reserved.
      6        1.1       dsl  *
      7        1.1       dsl  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1       dsl  * by Jaromir Dolecek.
      9        1.1       dsl  *
     10        1.1       dsl  * Redistribution and use in source and binary forms, with or without
     11        1.1       dsl  * modification, are permitted provided that the following conditions
     12        1.1       dsl  * are met:
     13        1.1       dsl  * 1. Redistributions of source code must retain the above copyright
     14        1.1       dsl  *    notice, this list of conditions and the following disclaimer.
     15        1.1       dsl  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1       dsl  *    notice, this list of conditions and the following disclaimer in the
     17        1.1       dsl  *    documentation and/or other materials provided with the distribution.
     18        1.1       dsl  *
     19        1.1       dsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1       dsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1       dsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1       dsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1       dsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1       dsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1       dsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1       dsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1       dsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1       dsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1       dsl  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1       dsl  */
     31        1.1       dsl 
     32        1.1       dsl #include "config.h"
     33        1.1       dsl #if !defined(lint) && !defined(SCCSID)
     34  1.31.18.1       tls __RCSID("$NetBSD: filecomplete.c,v 1.31.18.1 2014/08/10 06:51:57 tls Exp $");
     35        1.1       dsl #endif /* not lint && not SCCSID */
     36        1.1       dsl 
     37        1.1       dsl #include <sys/types.h>
     38        1.1       dsl #include <sys/stat.h>
     39        1.1       dsl #include <stdio.h>
     40        1.1       dsl #include <dirent.h>
     41        1.1       dsl #include <string.h>
     42        1.1       dsl #include <pwd.h>
     43        1.1       dsl #include <ctype.h>
     44        1.1       dsl #include <stdlib.h>
     45        1.1       dsl #include <unistd.h>
     46        1.1       dsl #include <limits.h>
     47        1.1       dsl #include <errno.h>
     48        1.1       dsl #include <fcntl.h>
     49       1.28  christos 
     50        1.1       dsl #include "el.h"
     51        1.1       dsl #include "fcns.h"		/* for EL_NUM_FCNS */
     52        1.1       dsl #include "histedit.h"
     53        1.1       dsl #include "filecomplete.h"
     54        1.1       dsl 
     55       1.22  dholland static const Char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@',
     56       1.17  christos     '$', '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
     57        1.1       dsl 
     58        1.1       dsl 
     59        1.1       dsl /********************************/
     60        1.1       dsl /* completion functions */
     61        1.1       dsl 
     62        1.1       dsl /*
     63        1.1       dsl  * does tilde expansion of strings of type ``~user/foo''
     64        1.1       dsl  * if ``user'' isn't valid user name or ``txt'' doesn't start
     65        1.1       dsl  * w/ '~', returns pointer to strdup()ed copy of ``txt''
     66        1.1       dsl  *
     67        1.1       dsl  * it's callers's responsibility to free() returned string
     68        1.1       dsl  */
     69        1.1       dsl char *
     70        1.7  christos fn_tilde_expand(const char *txt)
     71        1.1       dsl {
     72       1.25  christos #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
     73       1.25  christos 	struct passwd pwres;
     74       1.25  christos 	char pwbuf[1024];
     75       1.25  christos #endif
     76       1.25  christos 	struct passwd *pass;
     77        1.1       dsl 	char *temp;
     78        1.1       dsl 	size_t len = 0;
     79        1.1       dsl 
     80        1.1       dsl 	if (txt[0] != '~')
     81       1.27  christos 		return strdup(txt);
     82        1.1       dsl 
     83        1.1       dsl 	temp = strchr(txt + 1, '/');
     84        1.1       dsl 	if (temp == NULL) {
     85        1.1       dsl 		temp = strdup(txt + 1);
     86        1.1       dsl 		if (temp == NULL)
     87        1.1       dsl 			return NULL;
     88        1.1       dsl 	} else {
     89       1.30  christos 		/* text until string after slash */
     90       1.30  christos 		len = (size_t)(temp - txt + 1);
     91       1.26  christos 		temp = el_malloc(len * sizeof(*temp));
     92        1.1       dsl 		if (temp == NULL)
     93        1.1       dsl 			return NULL;
     94        1.1       dsl 		(void)strncpy(temp, txt + 1, len - 2);
     95        1.1       dsl 		temp[len - 2] = '\0';
     96        1.1       dsl 	}
     97        1.3       dsl 	if (temp[0] == 0) {
     98       1.24  christos #ifdef HAVE_GETPW_R_POSIX
     99       1.24  christos  		if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
    100       1.24  christos 		    &pass) != 0)
    101       1.24  christos  			pass = NULL;
    102       1.24  christos #elif HAVE_GETPW_R_DRAFT
    103       1.24  christos 		pass = getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf));
    104       1.24  christos #else
    105       1.24  christos 		pass = getpwuid(getuid());
    106       1.24  christos #endif
    107        1.3       dsl 	} else {
    108       1.24  christos #ifdef HAVE_GETPW_R_POSIX
    109        1.3       dsl 		if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
    110        1.3       dsl 			pass = NULL;
    111       1.24  christos #elif HAVE_GETPW_R_DRAFT
    112       1.24  christos 		pass = getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf));
    113       1.24  christos #else
    114       1.24  christos 		pass = getpwnam(temp);
    115       1.24  christos #endif
    116        1.3       dsl 	}
    117       1.26  christos 	el_free(temp);		/* value no more needed */
    118        1.1       dsl 	if (pass == NULL)
    119       1.27  christos 		return strdup(txt);
    120        1.1       dsl 
    121        1.1       dsl 	/* update pointer txt to point at string immedially following */
    122        1.1       dsl 	/* first slash */
    123        1.1       dsl 	txt += len;
    124        1.1       dsl 
    125       1.26  christos 	len = strlen(pass->pw_dir) + 1 + strlen(txt) + 1;
    126       1.26  christos 	temp = el_malloc(len * sizeof(*temp));
    127        1.1       dsl 	if (temp == NULL)
    128        1.1       dsl 		return NULL;
    129       1.26  christos 	(void)snprintf(temp, len, "%s/%s", pass->pw_dir, txt);
    130        1.1       dsl 
    131       1.27  christos 	return temp;
    132        1.1       dsl }
    133        1.1       dsl 
    134        1.1       dsl 
    135        1.1       dsl /*
    136        1.1       dsl  * return first found file name starting by the ``text'' or NULL if no
    137        1.1       dsl  * such file can be found
    138        1.1       dsl  * value of ``state'' is ignored
    139        1.1       dsl  *
    140        1.1       dsl  * it's caller's responsibility to free returned string
    141        1.1       dsl  */
    142        1.2       dsl char *
    143        1.7  christos fn_filename_completion_function(const char *text, int state)
    144        1.1       dsl {
    145        1.1       dsl 	static DIR *dir = NULL;
    146        1.3       dsl 	static char *filename = NULL, *dirname = NULL, *dirpath = NULL;
    147        1.1       dsl 	static size_t filename_len = 0;
    148        1.1       dsl 	struct dirent *entry;
    149        1.1       dsl 	char *temp;
    150        1.1       dsl 	size_t len;
    151        1.1       dsl 
    152        1.1       dsl 	if (state == 0 || dir == NULL) {
    153        1.1       dsl 		temp = strrchr(text, '/');
    154        1.1       dsl 		if (temp) {
    155        1.1       dsl 			char *nptr;
    156        1.1       dsl 			temp++;
    157       1.26  christos 			nptr = el_realloc(filename, (strlen(temp) + 1) *
    158       1.26  christos 			    sizeof(*nptr));
    159        1.1       dsl 			if (nptr == NULL) {
    160       1.26  christos 				el_free(filename);
    161       1.19  christos 				filename = NULL;
    162        1.1       dsl 				return NULL;
    163        1.1       dsl 			}
    164        1.1       dsl 			filename = nptr;
    165        1.1       dsl 			(void)strcpy(filename, temp);
    166       1.30  christos 			len = (size_t)(temp - text);	/* including last slash */
    167       1.19  christos 
    168       1.26  christos 			nptr = el_realloc(dirname, (len + 1) *
    169       1.26  christos 			    sizeof(*nptr));
    170        1.1       dsl 			if (nptr == NULL) {
    171       1.26  christos 				el_free(dirname);
    172       1.19  christos 				dirname = NULL;
    173        1.1       dsl 				return NULL;
    174        1.1       dsl 			}
    175        1.1       dsl 			dirname = nptr;
    176        1.1       dsl 			(void)strncpy(dirname, text, len);
    177        1.1       dsl 			dirname[len] = '\0';
    178        1.1       dsl 		} else {
    179       1.26  christos 			el_free(filename);
    180        1.1       dsl 			if (*text == 0)
    181        1.1       dsl 				filename = NULL;
    182        1.1       dsl 			else {
    183        1.1       dsl 				filename = strdup(text);
    184        1.1       dsl 				if (filename == NULL)
    185        1.1       dsl 					return NULL;
    186        1.1       dsl 			}
    187       1.26  christos 			el_free(dirname);
    188        1.1       dsl 			dirname = NULL;
    189        1.1       dsl 		}
    190        1.1       dsl 
    191        1.1       dsl 		if (dir != NULL) {
    192        1.1       dsl 			(void)closedir(dir);
    193        1.1       dsl 			dir = NULL;
    194        1.1       dsl 		}
    195        1.3       dsl 
    196        1.3       dsl 		/* support for ``~user'' syntax */
    197       1.19  christos 
    198       1.26  christos 		el_free(dirpath);
    199       1.19  christos 		dirpath = NULL;
    200       1.19  christos 		if (dirname == NULL) {
    201       1.19  christos 			if ((dirname = strdup("")) == NULL)
    202       1.19  christos 				return NULL;
    203       1.19  christos 			dirpath = strdup("./");
    204       1.19  christos 		} else if (*dirname == '~')
    205        1.7  christos 			dirpath = fn_tilde_expand(dirname);
    206        1.4  christos 		else
    207        1.4  christos 			dirpath = strdup(dirname);
    208        1.4  christos 
    209        1.4  christos 		if (dirpath == NULL)
    210        1.4  christos 			return NULL;
    211        1.4  christos 
    212        1.3       dsl 		dir = opendir(dirpath);
    213        1.1       dsl 		if (!dir)
    214       1.27  christos 			return NULL;	/* cannot open the directory */
    215        1.3       dsl 
    216        1.3       dsl 		/* will be used in cycle */
    217        1.3       dsl 		filename_len = filename ? strlen(filename) : 0;
    218        1.1       dsl 	}
    219        1.1       dsl 
    220        1.1       dsl 	/* find the match */
    221        1.1       dsl 	while ((entry = readdir(dir)) != NULL) {
    222        1.1       dsl 		/* skip . and .. */
    223        1.1       dsl 		if (entry->d_name[0] == '.' && (!entry->d_name[1]
    224        1.1       dsl 		    || (entry->d_name[1] == '.' && !entry->d_name[2])))
    225        1.1       dsl 			continue;
    226        1.1       dsl 		if (filename_len == 0)
    227        1.1       dsl 			break;
    228        1.1       dsl 		/* otherwise, get first entry where first */
    229        1.1       dsl 		/* filename_len characters are equal	  */
    230        1.1       dsl 		if (entry->d_name[0] == filename[0]
    231       1.13       apb #if HAVE_STRUCT_DIRENT_D_NAMLEN
    232       1.13       apb 		    && entry->d_namlen >= filename_len
    233       1.13       apb #else
    234        1.1       dsl 		    && strlen(entry->d_name) >= filename_len
    235        1.1       dsl #endif
    236        1.1       dsl 		    && strncmp(entry->d_name, filename,
    237        1.1       dsl 			filename_len) == 0)
    238        1.1       dsl 			break;
    239        1.1       dsl 	}
    240        1.1       dsl 
    241        1.1       dsl 	if (entry) {		/* match found */
    242        1.1       dsl 
    243       1.13       apb #if HAVE_STRUCT_DIRENT_D_NAMLEN
    244       1.13       apb 		len = entry->d_namlen;
    245       1.13       apb #else
    246        1.3       dsl 		len = strlen(entry->d_name);
    247        1.1       dsl #endif
    248        1.1       dsl 
    249       1.26  christos 		len = strlen(dirname) + len + 1;
    250       1.26  christos 		temp = el_malloc(len * sizeof(*temp));
    251        1.3       dsl 		if (temp == NULL)
    252        1.3       dsl 			return NULL;
    253       1.26  christos 		(void)snprintf(temp, len, "%s%s", dirname, entry->d_name);
    254        1.1       dsl 	} else {
    255        1.1       dsl 		(void)closedir(dir);
    256        1.1       dsl 		dir = NULL;
    257        1.1       dsl 		temp = NULL;
    258        1.1       dsl 	}
    259        1.1       dsl 
    260       1.27  christos 	return temp;
    261        1.1       dsl }
    262        1.1       dsl 
    263        1.1       dsl 
    264        1.6  christos static const char *
    265        1.6  christos append_char_function(const char *name)
    266        1.6  christos {
    267        1.6  christos 	struct stat stbuf;
    268        1.7  christos 	char *expname = *name == '~' ? fn_tilde_expand(name) : NULL;
    269       1.12  christos 	const char *rs = " ";
    270        1.6  christos 
    271        1.6  christos 	if (stat(expname ? expname : name, &stbuf) == -1)
    272        1.6  christos 		goto out;
    273        1.6  christos 	if (S_ISDIR(stbuf.st_mode))
    274        1.6  christos 		rs = "/";
    275        1.6  christos out:
    276        1.6  christos 	if (expname)
    277       1.26  christos 		el_free(expname);
    278        1.6  christos 	return rs;
    279        1.6  christos }
    280        1.1       dsl /*
    281        1.1       dsl  * returns list of completions for text given
    282        1.5  christos  * non-static for readline.
    283        1.1       dsl  */
    284        1.5  christos char ** completion_matches(const char *, char *(*)(const char *, int));
    285        1.5  christos char **
    286        1.1       dsl completion_matches(const char *text, char *(*genfunc)(const char *, int))
    287        1.1       dsl {
    288        1.1       dsl 	char **match_list = NULL, *retstr, *prevstr;
    289        1.1       dsl 	size_t match_list_len, max_equal, which, i;
    290        1.1       dsl 	size_t matches;
    291        1.1       dsl 
    292        1.1       dsl 	matches = 0;
    293        1.1       dsl 	match_list_len = 1;
    294        1.1       dsl 	while ((retstr = (*genfunc) (text, (int)matches)) != NULL) {
    295        1.1       dsl 		/* allow for list terminator here */
    296        1.1       dsl 		if (matches + 3 >= match_list_len) {
    297        1.1       dsl 			char **nmatch_list;
    298        1.1       dsl 			while (matches + 3 >= match_list_len)
    299        1.1       dsl 				match_list_len <<= 1;
    300       1.26  christos 			nmatch_list = el_realloc(match_list,
    301       1.26  christos 			    match_list_len * sizeof(*nmatch_list));
    302        1.1       dsl 			if (nmatch_list == NULL) {
    303       1.26  christos 				el_free(match_list);
    304        1.1       dsl 				return NULL;
    305        1.1       dsl 			}
    306        1.1       dsl 			match_list = nmatch_list;
    307        1.1       dsl 
    308        1.1       dsl 		}
    309        1.1       dsl 		match_list[++matches] = retstr;
    310        1.1       dsl 	}
    311        1.1       dsl 
    312        1.1       dsl 	if (!match_list)
    313        1.1       dsl 		return NULL;	/* nothing found */
    314        1.1       dsl 
    315        1.1       dsl 	/* find least denominator and insert it to match_list[0] */
    316        1.1       dsl 	which = 2;
    317        1.1       dsl 	prevstr = match_list[1];
    318        1.1       dsl 	max_equal = strlen(prevstr);
    319        1.1       dsl 	for (; which <= matches; which++) {
    320        1.1       dsl 		for (i = 0; i < max_equal &&
    321        1.1       dsl 		    prevstr[i] == match_list[which][i]; i++)
    322        1.1       dsl 			continue;
    323        1.1       dsl 		max_equal = i;
    324        1.1       dsl 	}
    325        1.1       dsl 
    326       1.26  christos 	retstr = el_malloc((max_equal + 1) * sizeof(*retstr));
    327        1.1       dsl 	if (retstr == NULL) {
    328       1.26  christos 		el_free(match_list);
    329        1.1       dsl 		return NULL;
    330        1.1       dsl 	}
    331        1.1       dsl 	(void)strncpy(retstr, match_list[1], max_equal);
    332        1.1       dsl 	retstr[max_equal] = '\0';
    333        1.1       dsl 	match_list[0] = retstr;
    334        1.1       dsl 
    335        1.1       dsl 	/* add NULL as last pointer to the array */
    336       1.31    plunky 	match_list[matches + 1] = NULL;
    337        1.1       dsl 
    338       1.27  christos 	return match_list;
    339        1.1       dsl }
    340        1.1       dsl 
    341        1.1       dsl /*
    342        1.1       dsl  * Sort function for qsort(). Just wrapper around strcasecmp().
    343        1.1       dsl  */
    344        1.1       dsl static int
    345        1.1       dsl _fn_qsort_string_compare(const void *i1, const void *i2)
    346        1.1       dsl {
    347        1.1       dsl 	const char *s1 = ((const char * const *)i1)[0];
    348        1.1       dsl 	const char *s2 = ((const char * const *)i2)[0];
    349        1.1       dsl 
    350        1.1       dsl 	return strcasecmp(s1, s2);
    351        1.1       dsl }
    352        1.1       dsl 
    353        1.1       dsl /*
    354        1.1       dsl  * Display list of strings in columnar format on readline's output stream.
    355       1.21  dholland  * 'matches' is list of strings, 'num' is number of strings in 'matches',
    356       1.21  dholland  * 'width' is maximum length of string in 'matches'.
    357       1.21  dholland  *
    358       1.23  dholland  * matches[0] is not one of the match strings, but it is counted in
    359       1.23  dholland  * num, so the strings are matches[1] *through* matches[num-1].
    360        1.1       dsl  */
    361        1.1       dsl void
    362       1.21  dholland fn_display_match_list (EditLine *el, char **matches, size_t num, size_t width)
    363        1.1       dsl {
    364       1.21  dholland 	size_t line, lines, col, cols, thisguy;
    365       1.24  christos 	int screenwidth = el->el_terminal.t_size.h;
    366        1.1       dsl 
    367       1.21  dholland 	/* Ignore matches[0]. Avoid 1-based array logic below. */
    368       1.21  dholland 	matches++;
    369       1.23  dholland 	num--;
    370       1.21  dholland 
    371       1.21  dholland 	/*
    372       1.21  dholland 	 * Find out how many entries can be put on one line; count
    373       1.21  dholland 	 * with one space between strings the same way it's printed.
    374       1.21  dholland 	 */
    375       1.30  christos 	cols = (size_t)screenwidth / (width + 1);
    376       1.21  dholland 	if (cols == 0)
    377       1.21  dholland 		cols = 1;
    378       1.21  dholland 
    379       1.21  dholland 	/* how many lines of output, rounded up */
    380       1.21  dholland 	lines = (num + cols - 1) / cols;
    381       1.21  dholland 
    382       1.21  dholland 	/* Sort the items. */
    383       1.21  dholland 	qsort(matches, num, sizeof(char *), _fn_qsort_string_compare);
    384       1.21  dholland 
    385        1.1       dsl 	/*
    386       1.21  dholland 	 * On the ith line print elements i, i+lines, i+lines*2, etc.
    387        1.1       dsl 	 */
    388       1.21  dholland 	for (line = 0; line < lines; line++) {
    389       1.21  dholland 		for (col = 0; col < cols; col++) {
    390       1.21  dholland 			thisguy = line + col * lines;
    391       1.21  dholland 			if (thisguy >= num)
    392       1.21  dholland 				break;
    393       1.21  dholland 			(void)fprintf(el->el_outfile, "%s%-*s",
    394       1.21  dholland 			    col == 0 ? "" : " ", (int)width, matches[thisguy]);
    395       1.16  christos 		}
    396        1.1       dsl 		(void)fprintf(el->el_outfile, "\n");
    397        1.1       dsl 	}
    398        1.1       dsl }
    399        1.1       dsl 
    400        1.1       dsl /*
    401        1.1       dsl  * Complete the word at or before point,
    402        1.1       dsl  * 'what_to_do' says what to do with the completion.
    403        1.1       dsl  * \t   means do standard completion.
    404        1.1       dsl  * `?' means list the possible completions.
    405        1.1       dsl  * `*' means insert all of the possible completions.
    406        1.1       dsl  * `!' means to do standard completion, and list all possible completions if
    407        1.1       dsl  * there is more than one.
    408        1.1       dsl  *
    409        1.1       dsl  * Note: '*' support is not implemented
    410        1.1       dsl  *       '!' could never be invoked
    411        1.1       dsl  */
    412        1.1       dsl int
    413        1.1       dsl fn_complete(EditLine *el,
    414        1.1       dsl 	char *(*complet_func)(const char *, int),
    415        1.1       dsl 	char **(*attempted_completion_function)(const char *, int, int),
    416       1.17  christos 	const Char *word_break, const Char *special_prefixes,
    417       1.15  christos 	const char *(*app_func)(const char *), size_t query_items,
    418        1.1       dsl 	int *completion_type, int *over, int *point, int *end)
    419        1.1       dsl {
    420       1.17  christos 	const TYPE(LineInfo) *li;
    421       1.17  christos 	Char *temp;
    422       1.17  christos         char **matches;
    423       1.17  christos 	const Char *ctemp;
    424        1.1       dsl 	size_t len;
    425        1.1       dsl 	int what_to_do = '\t';
    426       1.10  christos 	int retval = CC_NORM;
    427        1.1       dsl 
    428        1.1       dsl 	if (el->el_state.lastcmd == el->el_state.thiscmd)
    429        1.1       dsl 		what_to_do = '?';
    430        1.1       dsl 
    431        1.1       dsl 	/* readline's rl_complete() has to be told what we did... */
    432        1.1       dsl 	if (completion_type != NULL)
    433        1.1       dsl 		*completion_type = what_to_do;
    434        1.1       dsl 
    435        1.1       dsl 	if (!complet_func)
    436        1.7  christos 		complet_func = fn_filename_completion_function;
    437        1.6  christos 	if (!app_func)
    438        1.6  christos 		app_func = append_char_function;
    439        1.1       dsl 
    440        1.1       dsl 	/* We now look backwards for the start of a filename/variable word */
    441       1.17  christos 	li = FUN(el,line)(el);
    442       1.17  christos 	ctemp = li->cursor;
    443        1.1       dsl 	while (ctemp > li->buffer
    444       1.17  christos 	    && !Strchr(word_break, ctemp[-1])
    445       1.17  christos 	    && (!special_prefixes || !Strchr(special_prefixes, ctemp[-1]) ) )
    446        1.1       dsl 		ctemp--;
    447        1.1       dsl 
    448       1.30  christos 	len = (size_t)(li->cursor - ctemp);
    449       1.26  christos 	temp = el_malloc((len + 1) * sizeof(*temp));
    450       1.17  christos 	(void)Strncpy(temp, ctemp, len);
    451        1.1       dsl 	temp[len] = '\0';
    452        1.1       dsl 
    453        1.1       dsl 	/* these can be used by function called in completion_matches() */
    454        1.1       dsl 	/* or (*attempted_completion_function)() */
    455        1.1       dsl 	if (point != 0)
    456       1.14  christos 		*point = (int)(li->cursor - li->buffer);
    457        1.1       dsl 	if (end != NULL)
    458       1.14  christos 		*end = (int)(li->lastchar - li->buffer);
    459        1.1       dsl 
    460        1.1       dsl 	if (attempted_completion_function) {
    461       1.14  christos 		int cur_off = (int)(li->cursor - li->buffer);
    462       1.30  christos 		matches = (*attempted_completion_function)(
    463       1.30  christos 		    ct_encode_string(temp, &el->el_scratch),
    464       1.30  christos 		    cur_off - (int)len, cur_off);
    465        1.1       dsl 	} else
    466        1.1       dsl 		matches = 0;
    467        1.1       dsl 	if (!attempted_completion_function ||
    468        1.8  christos 	    (over != NULL && !*over && !matches))
    469       1.30  christos 		matches = completion_matches(
    470       1.30  christos 		    ct_encode_string(temp, &el->el_scratch), complet_func);
    471        1.1       dsl 
    472        1.1       dsl 	if (over != NULL)
    473        1.1       dsl 		*over = 0;
    474        1.1       dsl 
    475        1.1       dsl 	if (matches) {
    476       1.10  christos 		int i;
    477       1.14  christos 		size_t matches_num, maxlen, match_len, match_display=1;
    478        1.1       dsl 
    479       1.10  christos 		retval = CC_REFRESH;
    480        1.1       dsl 		/*
    481        1.1       dsl 		 * Only replace the completed string with common part of
    482        1.1       dsl 		 * possible matches if there is possible completion.
    483        1.1       dsl 		 */
    484        1.1       dsl 		if (matches[0][0] != '\0') {
    485        1.1       dsl 			el_deletestr(el, (int) len);
    486       1.17  christos 			FUN(el,insertstr)(el,
    487       1.17  christos 			    ct_decode_string(matches[0], &el->el_scratch));
    488        1.1       dsl 		}
    489        1.1       dsl 
    490        1.1       dsl 		if (what_to_do == '?')
    491        1.1       dsl 			goto display_matches;
    492        1.1       dsl 
    493  1.31.18.1       tls 		if (matches[2] == NULL &&
    494  1.31.18.1       tls 		    (matches[1] == NULL || strcmp(matches[0], matches[1]) == 0)) {
    495        1.1       dsl 			/*
    496        1.1       dsl 			 * We found exact match. Add a space after
    497        1.1       dsl 			 * it, unless we do filename completion and the
    498        1.1       dsl 			 * object is a directory.
    499        1.1       dsl 			 */
    500       1.17  christos 			FUN(el,insertstr)(el,
    501       1.17  christos 			    ct_decode_string((*app_func)(matches[0]),
    502       1.17  christos 			    &el->el_scratch));
    503        1.1       dsl 		} else if (what_to_do == '!') {
    504        1.1       dsl     display_matches:
    505        1.1       dsl 			/*
    506        1.1       dsl 			 * More than one match and requested to list possible
    507        1.1       dsl 			 * matches.
    508        1.1       dsl 			 */
    509        1.1       dsl 
    510       1.17  christos 			for(i = 1, maxlen = 0; matches[i]; i++) {
    511        1.1       dsl 				match_len = strlen(matches[i]);
    512        1.1       dsl 				if (match_len > maxlen)
    513        1.1       dsl 					maxlen = match_len;
    514        1.1       dsl 			}
    515       1.21  dholland 			/* matches[1] through matches[i-1] are available */
    516       1.30  christos 			matches_num = (size_t)(i - 1);
    517        1.1       dsl 
    518        1.1       dsl 			/* newline to get on next line from command line */
    519        1.1       dsl 			(void)fprintf(el->el_outfile, "\n");
    520        1.1       dsl 
    521        1.1       dsl 			/*
    522        1.1       dsl 			 * If there are too many items, ask user for display
    523        1.1       dsl 			 * confirmation.
    524        1.1       dsl 			 */
    525        1.1       dsl 			if (matches_num > query_items) {
    526        1.1       dsl 				(void)fprintf(el->el_outfile,
    527       1.14  christos 				    "Display all %zu possibilities? (y or n) ",
    528        1.1       dsl 				    matches_num);
    529        1.1       dsl 				(void)fflush(el->el_outfile);
    530        1.1       dsl 				if (getc(stdin) != 'y')
    531        1.1       dsl 					match_display = 0;
    532        1.1       dsl 				(void)fprintf(el->el_outfile, "\n");
    533        1.1       dsl 			}
    534        1.1       dsl 
    535       1.23  dholland 			if (match_display) {
    536       1.23  dholland 				/*
    537       1.23  dholland 				 * Interface of this function requires the
    538       1.23  dholland 				 * strings be matches[1..num-1] for compat.
    539       1.23  dholland 				 * We have matches_num strings not counting
    540       1.23  dholland 				 * the prefix in matches[0], so we need to
    541       1.23  dholland 				 * add 1 to matches_num for the call.
    542       1.23  dholland 				 */
    543       1.23  dholland 				fn_display_match_list(el, matches,
    544       1.23  dholland 				    matches_num+1, maxlen);
    545       1.23  dholland 			}
    546        1.1       dsl 			retval = CC_REDISPLAY;
    547        1.1       dsl 		} else if (matches[0][0]) {
    548        1.1       dsl 			/*
    549        1.1       dsl 			 * There was some common match, but the name was
    550        1.1       dsl 			 * not complete enough. Next tab will print possible
    551        1.1       dsl 			 * completions.
    552        1.1       dsl 			 */
    553        1.1       dsl 			el_beep(el);
    554        1.1       dsl 		} else {
    555        1.1       dsl 			/* lcd is not a valid object - further specification */
    556        1.1       dsl 			/* is needed */
    557        1.1       dsl 			el_beep(el);
    558        1.1       dsl 			retval = CC_NORM;
    559        1.1       dsl 		}
    560        1.1       dsl 
    561        1.1       dsl 		/* free elements of array and the array itself */
    562        1.1       dsl 		for (i = 0; matches[i]; i++)
    563       1.26  christos 			el_free(matches[i]);
    564       1.26  christos 		el_free(matches);
    565       1.10  christos 		matches = NULL;
    566        1.1       dsl 	}
    567       1.26  christos 	el_free(temp);
    568       1.10  christos 	return retval;
    569        1.1       dsl }
    570        1.1       dsl 
    571        1.1       dsl /*
    572        1.1       dsl  * el-compatible wrapper around rl_complete; needed for key binding
    573        1.1       dsl  */
    574        1.1       dsl /* ARGSUSED */
    575        1.1       dsl unsigned char
    576        1.1       dsl _el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
    577        1.1       dsl {
    578        1.1       dsl 	return (unsigned char)fn_complete(el, NULL, NULL,
    579       1.29  christos 	    break_chars, NULL, NULL, (size_t)100,
    580        1.1       dsl 	    NULL, NULL, NULL, NULL);
    581        1.1       dsl }
    582