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