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