xref: /minix/lib/libedit/filecomplete.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: filecomplete.c,v 1.34 2014/10/18 15:07:02 riz Exp $	*/
23e1db26aSLionel Sambuc 
33e1db26aSLionel Sambuc /*-
43e1db26aSLionel Sambuc  * Copyright (c) 1997 The NetBSD Foundation, Inc.
53e1db26aSLionel Sambuc  * All rights reserved.
63e1db26aSLionel Sambuc  *
73e1db26aSLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
83e1db26aSLionel Sambuc  * by Jaromir Dolecek.
93e1db26aSLionel Sambuc  *
103e1db26aSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
113e1db26aSLionel Sambuc  * modification, are permitted provided that the following conditions
123e1db26aSLionel Sambuc  * are met:
133e1db26aSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
143e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
153e1db26aSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
163e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
173e1db26aSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
183e1db26aSLionel Sambuc  *
193e1db26aSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203e1db26aSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213e1db26aSLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223e1db26aSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233e1db26aSLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243e1db26aSLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253e1db26aSLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263e1db26aSLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273e1db26aSLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283e1db26aSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293e1db26aSLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
303e1db26aSLionel Sambuc  */
313e1db26aSLionel Sambuc 
323e1db26aSLionel Sambuc #include "config.h"
333e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: filecomplete.c,v 1.34 2014/10/18 15:07:02 riz Exp $");
353e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
363e1db26aSLionel Sambuc 
373e1db26aSLionel Sambuc #include <sys/types.h>
383e1db26aSLionel Sambuc #include <sys/stat.h>
393e1db26aSLionel Sambuc #include <stdio.h>
403e1db26aSLionel Sambuc #include <dirent.h>
413e1db26aSLionel Sambuc #include <string.h>
423e1db26aSLionel Sambuc #include <pwd.h>
433e1db26aSLionel Sambuc #include <ctype.h>
443e1db26aSLionel Sambuc #include <stdlib.h>
453e1db26aSLionel Sambuc #include <unistd.h>
463e1db26aSLionel Sambuc #include <limits.h>
473e1db26aSLionel Sambuc #include <errno.h>
483e1db26aSLionel Sambuc #include <fcntl.h>
493e1db26aSLionel Sambuc 
503e1db26aSLionel Sambuc #include "el.h"
513e1db26aSLionel Sambuc #include "fcns.h"		/* for EL_NUM_FCNS */
523e1db26aSLionel Sambuc #include "histedit.h"
533e1db26aSLionel Sambuc #include "filecomplete.h"
543e1db26aSLionel Sambuc 
553e1db26aSLionel Sambuc static const Char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@',
563e1db26aSLionel Sambuc     '$', '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
573e1db26aSLionel Sambuc 
583e1db26aSLionel Sambuc 
593e1db26aSLionel Sambuc /********************************/
603e1db26aSLionel Sambuc /* completion functions */
613e1db26aSLionel Sambuc 
623e1db26aSLionel Sambuc /*
633e1db26aSLionel Sambuc  * does tilde expansion of strings of type ``~user/foo''
643e1db26aSLionel Sambuc  * if ``user'' isn't valid user name or ``txt'' doesn't start
653e1db26aSLionel Sambuc  * w/ '~', returns pointer to strdup()ed copy of ``txt''
663e1db26aSLionel Sambuc  *
67*0a6a1f1dSLionel Sambuc  * it's the caller's responsibility to free() the returned string
683e1db26aSLionel Sambuc  */
693e1db26aSLionel Sambuc char *
fn_tilde_expand(const char * txt)703e1db26aSLionel Sambuc fn_tilde_expand(const char *txt)
713e1db26aSLionel Sambuc {
723e1db26aSLionel Sambuc #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
733e1db26aSLionel Sambuc 	struct passwd pwres;
743e1db26aSLionel Sambuc 	char pwbuf[1024];
753e1db26aSLionel Sambuc #endif
763e1db26aSLionel Sambuc 	struct passwd *pass;
773e1db26aSLionel Sambuc 	char *temp;
783e1db26aSLionel Sambuc 	size_t len = 0;
793e1db26aSLionel Sambuc 
803e1db26aSLionel Sambuc 	if (txt[0] != '~')
813e1db26aSLionel Sambuc 		return strdup(txt);
823e1db26aSLionel Sambuc 
833e1db26aSLionel Sambuc 	temp = strchr(txt + 1, '/');
843e1db26aSLionel Sambuc 	if (temp == NULL) {
853e1db26aSLionel Sambuc 		temp = strdup(txt + 1);
863e1db26aSLionel Sambuc 		if (temp == NULL)
873e1db26aSLionel Sambuc 			return NULL;
883e1db26aSLionel Sambuc 	} else {
893e1db26aSLionel Sambuc 		/* text until string after slash */
903e1db26aSLionel Sambuc 		len = (size_t)(temp - txt + 1);
913e1db26aSLionel Sambuc 		temp = el_malloc(len * sizeof(*temp));
923e1db26aSLionel Sambuc 		if (temp == NULL)
933e1db26aSLionel Sambuc 			return NULL;
943e1db26aSLionel Sambuc 		(void)strncpy(temp, txt + 1, len - 2);
953e1db26aSLionel Sambuc 		temp[len - 2] = '\0';
963e1db26aSLionel Sambuc 	}
973e1db26aSLionel Sambuc 	if (temp[0] == 0) {
983e1db26aSLionel Sambuc #ifdef HAVE_GETPW_R_POSIX
993e1db26aSLionel Sambuc  		if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
1003e1db26aSLionel Sambuc 		    &pass) != 0)
1013e1db26aSLionel Sambuc  			pass = NULL;
1023e1db26aSLionel Sambuc #elif HAVE_GETPW_R_DRAFT
1033e1db26aSLionel Sambuc 		pass = getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf));
1043e1db26aSLionel Sambuc #else
1053e1db26aSLionel Sambuc 		pass = getpwuid(getuid());
1063e1db26aSLionel Sambuc #endif
1073e1db26aSLionel Sambuc 	} else {
1083e1db26aSLionel Sambuc #ifdef HAVE_GETPW_R_POSIX
1093e1db26aSLionel Sambuc 		if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
1103e1db26aSLionel Sambuc 			pass = NULL;
1113e1db26aSLionel Sambuc #elif HAVE_GETPW_R_DRAFT
1123e1db26aSLionel Sambuc 		pass = getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf));
1133e1db26aSLionel Sambuc #else
1143e1db26aSLionel Sambuc 		pass = getpwnam(temp);
1153e1db26aSLionel Sambuc #endif
1163e1db26aSLionel Sambuc 	}
1173e1db26aSLionel Sambuc 	el_free(temp);		/* value no more needed */
1183e1db26aSLionel Sambuc 	if (pass == NULL)
1193e1db26aSLionel Sambuc 		return strdup(txt);
1203e1db26aSLionel Sambuc 
1213e1db26aSLionel Sambuc 	/* update pointer txt to point at string immedially following */
1223e1db26aSLionel Sambuc 	/* first slash */
1233e1db26aSLionel Sambuc 	txt += len;
1243e1db26aSLionel Sambuc 
1253e1db26aSLionel Sambuc 	len = strlen(pass->pw_dir) + 1 + strlen(txt) + 1;
1263e1db26aSLionel Sambuc 	temp = el_malloc(len * sizeof(*temp));
1273e1db26aSLionel Sambuc 	if (temp == NULL)
1283e1db26aSLionel Sambuc 		return NULL;
1293e1db26aSLionel Sambuc 	(void)snprintf(temp, len, "%s/%s", pass->pw_dir, txt);
1303e1db26aSLionel Sambuc 
1313e1db26aSLionel Sambuc 	return temp;
1323e1db26aSLionel Sambuc }
1333e1db26aSLionel Sambuc 
1343e1db26aSLionel Sambuc 
1353e1db26aSLionel Sambuc /*
1363e1db26aSLionel Sambuc  * return first found file name starting by the ``text'' or NULL if no
1373e1db26aSLionel Sambuc  * such file can be found
1383e1db26aSLionel Sambuc  * value of ``state'' is ignored
1393e1db26aSLionel Sambuc  *
140*0a6a1f1dSLionel Sambuc  * it's the caller's responsibility to free the returned string
1413e1db26aSLionel Sambuc  */
1423e1db26aSLionel Sambuc char *
fn_filename_completion_function(const char * text,int state)1433e1db26aSLionel Sambuc fn_filename_completion_function(const char *text, int state)
1443e1db26aSLionel Sambuc {
1453e1db26aSLionel Sambuc 	static DIR *dir = NULL;
1463e1db26aSLionel Sambuc 	static char *filename = NULL, *dirname = NULL, *dirpath = NULL;
1473e1db26aSLionel Sambuc 	static size_t filename_len = 0;
1483e1db26aSLionel Sambuc 	struct dirent *entry;
1493e1db26aSLionel Sambuc 	char *temp;
1503e1db26aSLionel Sambuc 	size_t len;
1513e1db26aSLionel Sambuc 
1523e1db26aSLionel Sambuc 	if (state == 0 || dir == NULL) {
1533e1db26aSLionel Sambuc 		temp = strrchr(text, '/');
1543e1db26aSLionel Sambuc 		if (temp) {
1553e1db26aSLionel Sambuc 			char *nptr;
1563e1db26aSLionel Sambuc 			temp++;
1573e1db26aSLionel Sambuc 			nptr = el_realloc(filename, (strlen(temp) + 1) *
1583e1db26aSLionel Sambuc 			    sizeof(*nptr));
1593e1db26aSLionel Sambuc 			if (nptr == NULL) {
1603e1db26aSLionel Sambuc 				el_free(filename);
1613e1db26aSLionel Sambuc 				filename = NULL;
1623e1db26aSLionel Sambuc 				return NULL;
1633e1db26aSLionel Sambuc 			}
1643e1db26aSLionel Sambuc 			filename = nptr;
1653e1db26aSLionel Sambuc 			(void)strcpy(filename, temp);
1663e1db26aSLionel Sambuc 			len = (size_t)(temp - text);	/* including last slash */
1673e1db26aSLionel Sambuc 
1683e1db26aSLionel Sambuc 			nptr = el_realloc(dirname, (len + 1) *
1693e1db26aSLionel Sambuc 			    sizeof(*nptr));
1703e1db26aSLionel Sambuc 			if (nptr == NULL) {
1713e1db26aSLionel Sambuc 				el_free(dirname);
1723e1db26aSLionel Sambuc 				dirname = NULL;
1733e1db26aSLionel Sambuc 				return NULL;
1743e1db26aSLionel Sambuc 			}
1753e1db26aSLionel Sambuc 			dirname = nptr;
1763e1db26aSLionel Sambuc 			(void)strncpy(dirname, text, len);
1773e1db26aSLionel Sambuc 			dirname[len] = '\0';
1783e1db26aSLionel Sambuc 		} else {
1793e1db26aSLionel Sambuc 			el_free(filename);
1803e1db26aSLionel Sambuc 			if (*text == 0)
1813e1db26aSLionel Sambuc 				filename = NULL;
1823e1db26aSLionel Sambuc 			else {
1833e1db26aSLionel Sambuc 				filename = strdup(text);
1843e1db26aSLionel Sambuc 				if (filename == NULL)
1853e1db26aSLionel Sambuc 					return NULL;
1863e1db26aSLionel Sambuc 			}
1873e1db26aSLionel Sambuc 			el_free(dirname);
1883e1db26aSLionel Sambuc 			dirname = NULL;
1893e1db26aSLionel Sambuc 		}
1903e1db26aSLionel Sambuc 
1913e1db26aSLionel Sambuc 		if (dir != NULL) {
1923e1db26aSLionel Sambuc 			(void)closedir(dir);
1933e1db26aSLionel Sambuc 			dir = NULL;
1943e1db26aSLionel Sambuc 		}
1953e1db26aSLionel Sambuc 
1963e1db26aSLionel Sambuc 		/* support for ``~user'' syntax */
1973e1db26aSLionel Sambuc 
1983e1db26aSLionel Sambuc 		el_free(dirpath);
1993e1db26aSLionel Sambuc 		dirpath = NULL;
2003e1db26aSLionel Sambuc 		if (dirname == NULL) {
2013e1db26aSLionel Sambuc 			if ((dirname = strdup("")) == NULL)
2023e1db26aSLionel Sambuc 				return NULL;
2033e1db26aSLionel Sambuc 			dirpath = strdup("./");
2043e1db26aSLionel Sambuc 		} else if (*dirname == '~')
2053e1db26aSLionel Sambuc 			dirpath = fn_tilde_expand(dirname);
2063e1db26aSLionel Sambuc 		else
2073e1db26aSLionel Sambuc 			dirpath = strdup(dirname);
2083e1db26aSLionel Sambuc 
2093e1db26aSLionel Sambuc 		if (dirpath == NULL)
2103e1db26aSLionel Sambuc 			return NULL;
2113e1db26aSLionel Sambuc 
2123e1db26aSLionel Sambuc 		dir = opendir(dirpath);
2133e1db26aSLionel Sambuc 		if (!dir)
2143e1db26aSLionel Sambuc 			return NULL;	/* cannot open the directory */
2153e1db26aSLionel Sambuc 
2163e1db26aSLionel Sambuc 		/* will be used in cycle */
2173e1db26aSLionel Sambuc 		filename_len = filename ? strlen(filename) : 0;
2183e1db26aSLionel Sambuc 	}
2193e1db26aSLionel Sambuc 
2203e1db26aSLionel Sambuc 	/* find the match */
2213e1db26aSLionel Sambuc 	while ((entry = readdir(dir)) != NULL) {
2223e1db26aSLionel Sambuc 		/* skip . and .. */
2233e1db26aSLionel Sambuc 		if (entry->d_name[0] == '.' && (!entry->d_name[1]
2243e1db26aSLionel Sambuc 		    || (entry->d_name[1] == '.' && !entry->d_name[2])))
2253e1db26aSLionel Sambuc 			continue;
2263e1db26aSLionel Sambuc 		if (filename_len == 0)
2273e1db26aSLionel Sambuc 			break;
2283e1db26aSLionel Sambuc 		/* otherwise, get first entry where first */
2293e1db26aSLionel Sambuc 		/* filename_len characters are equal	  */
2303e1db26aSLionel Sambuc 		if (entry->d_name[0] == filename[0]
2313e1db26aSLionel Sambuc #if HAVE_STRUCT_DIRENT_D_NAMLEN
2323e1db26aSLionel Sambuc 		    && entry->d_namlen >= filename_len
2333e1db26aSLionel Sambuc #else
2343e1db26aSLionel Sambuc 		    && strlen(entry->d_name) >= filename_len
2353e1db26aSLionel Sambuc #endif
2363e1db26aSLionel Sambuc 		    && strncmp(entry->d_name, filename,
2373e1db26aSLionel Sambuc 			filename_len) == 0)
2383e1db26aSLionel Sambuc 			break;
2393e1db26aSLionel Sambuc 	}
2403e1db26aSLionel Sambuc 
2413e1db26aSLionel Sambuc 	if (entry) {		/* match found */
2423e1db26aSLionel Sambuc 
2433e1db26aSLionel Sambuc #if HAVE_STRUCT_DIRENT_D_NAMLEN
2443e1db26aSLionel Sambuc 		len = entry->d_namlen;
2453e1db26aSLionel Sambuc #else
2463e1db26aSLionel Sambuc 		len = strlen(entry->d_name);
2473e1db26aSLionel Sambuc #endif
2483e1db26aSLionel Sambuc 
2493e1db26aSLionel Sambuc 		len = strlen(dirname) + len + 1;
2503e1db26aSLionel Sambuc 		temp = el_malloc(len * sizeof(*temp));
2513e1db26aSLionel Sambuc 		if (temp == NULL)
2523e1db26aSLionel Sambuc 			return NULL;
2533e1db26aSLionel Sambuc 		(void)snprintf(temp, len, "%s%s", dirname, entry->d_name);
2543e1db26aSLionel Sambuc 	} else {
2553e1db26aSLionel Sambuc 		(void)closedir(dir);
2563e1db26aSLionel Sambuc 		dir = NULL;
2573e1db26aSLionel Sambuc 		temp = NULL;
2583e1db26aSLionel Sambuc 	}
2593e1db26aSLionel Sambuc 
2603e1db26aSLionel Sambuc 	return temp;
2613e1db26aSLionel Sambuc }
2623e1db26aSLionel Sambuc 
2633e1db26aSLionel Sambuc 
2643e1db26aSLionel Sambuc static const char *
append_char_function(const char * name)2653e1db26aSLionel Sambuc append_char_function(const char *name)
2663e1db26aSLionel Sambuc {
2673e1db26aSLionel Sambuc 	struct stat stbuf;
2683e1db26aSLionel Sambuc 	char *expname = *name == '~' ? fn_tilde_expand(name) : NULL;
2693e1db26aSLionel Sambuc 	const char *rs = " ";
2703e1db26aSLionel Sambuc 
2713e1db26aSLionel Sambuc 	if (stat(expname ? expname : name, &stbuf) == -1)
2723e1db26aSLionel Sambuc 		goto out;
2733e1db26aSLionel Sambuc 	if (S_ISDIR(stbuf.st_mode))
2743e1db26aSLionel Sambuc 		rs = "/";
2753e1db26aSLionel Sambuc out:
2763e1db26aSLionel Sambuc 	if (expname)
2773e1db26aSLionel Sambuc 		el_free(expname);
2783e1db26aSLionel Sambuc 	return rs;
2793e1db26aSLionel Sambuc }
2803e1db26aSLionel Sambuc /*
2813e1db26aSLionel Sambuc  * returns list of completions for text given
2823e1db26aSLionel Sambuc  * non-static for readline.
2833e1db26aSLionel Sambuc  */
2843e1db26aSLionel Sambuc char ** completion_matches(const char *, char *(*)(const char *, int));
2853e1db26aSLionel Sambuc char **
completion_matches(const char * text,char * (* genfunc)(const char *,int))2863e1db26aSLionel Sambuc completion_matches(const char *text, char *(*genfunc)(const char *, int))
2873e1db26aSLionel Sambuc {
2883e1db26aSLionel Sambuc 	char **match_list = NULL, *retstr, *prevstr;
2893e1db26aSLionel Sambuc 	size_t match_list_len, max_equal, which, i;
2903e1db26aSLionel Sambuc 	size_t matches;
2913e1db26aSLionel Sambuc 
2923e1db26aSLionel Sambuc 	matches = 0;
2933e1db26aSLionel Sambuc 	match_list_len = 1;
2943e1db26aSLionel Sambuc 	while ((retstr = (*genfunc) (text, (int)matches)) != NULL) {
2953e1db26aSLionel Sambuc 		/* allow for list terminator here */
2963e1db26aSLionel Sambuc 		if (matches + 3 >= match_list_len) {
2973e1db26aSLionel Sambuc 			char **nmatch_list;
2983e1db26aSLionel Sambuc 			while (matches + 3 >= match_list_len)
2993e1db26aSLionel Sambuc 				match_list_len <<= 1;
3003e1db26aSLionel Sambuc 			nmatch_list = el_realloc(match_list,
3013e1db26aSLionel Sambuc 			    match_list_len * sizeof(*nmatch_list));
3023e1db26aSLionel Sambuc 			if (nmatch_list == NULL) {
3033e1db26aSLionel Sambuc 				el_free(match_list);
3043e1db26aSLionel Sambuc 				return NULL;
3053e1db26aSLionel Sambuc 			}
3063e1db26aSLionel Sambuc 			match_list = nmatch_list;
3073e1db26aSLionel Sambuc 
3083e1db26aSLionel Sambuc 		}
3093e1db26aSLionel Sambuc 		match_list[++matches] = retstr;
3103e1db26aSLionel Sambuc 	}
3113e1db26aSLionel Sambuc 
3123e1db26aSLionel Sambuc 	if (!match_list)
3133e1db26aSLionel Sambuc 		return NULL;	/* nothing found */
3143e1db26aSLionel Sambuc 
3153e1db26aSLionel Sambuc 	/* find least denominator and insert it to match_list[0] */
3163e1db26aSLionel Sambuc 	which = 2;
3173e1db26aSLionel Sambuc 	prevstr = match_list[1];
3183e1db26aSLionel Sambuc 	max_equal = strlen(prevstr);
3193e1db26aSLionel Sambuc 	for (; which <= matches; which++) {
3203e1db26aSLionel Sambuc 		for (i = 0; i < max_equal &&
3213e1db26aSLionel Sambuc 		    prevstr[i] == match_list[which][i]; i++)
3223e1db26aSLionel Sambuc 			continue;
3233e1db26aSLionel Sambuc 		max_equal = i;
3243e1db26aSLionel Sambuc 	}
3253e1db26aSLionel Sambuc 
3263e1db26aSLionel Sambuc 	retstr = el_malloc((max_equal + 1) * sizeof(*retstr));
3273e1db26aSLionel Sambuc 	if (retstr == NULL) {
3283e1db26aSLionel Sambuc 		el_free(match_list);
3293e1db26aSLionel Sambuc 		return NULL;
3303e1db26aSLionel Sambuc 	}
3313e1db26aSLionel Sambuc 	(void)strncpy(retstr, match_list[1], max_equal);
3323e1db26aSLionel Sambuc 	retstr[max_equal] = '\0';
3333e1db26aSLionel Sambuc 	match_list[0] = retstr;
3343e1db26aSLionel Sambuc 
3353e1db26aSLionel Sambuc 	/* add NULL as last pointer to the array */
3363e1db26aSLionel Sambuc 	match_list[matches + 1] = NULL;
3373e1db26aSLionel Sambuc 
3383e1db26aSLionel Sambuc 	return match_list;
3393e1db26aSLionel Sambuc }
3403e1db26aSLionel Sambuc 
3413e1db26aSLionel Sambuc /*
3423e1db26aSLionel Sambuc  * Sort function for qsort(). Just wrapper around strcasecmp().
3433e1db26aSLionel Sambuc  */
3443e1db26aSLionel Sambuc static int
_fn_qsort_string_compare(const void * i1,const void * i2)3453e1db26aSLionel Sambuc _fn_qsort_string_compare(const void *i1, const void *i2)
3463e1db26aSLionel Sambuc {
3473e1db26aSLionel Sambuc 	const char *s1 = ((const char * const *)i1)[0];
3483e1db26aSLionel Sambuc 	const char *s2 = ((const char * const *)i2)[0];
3493e1db26aSLionel Sambuc 
3503e1db26aSLionel Sambuc 	return strcasecmp(s1, s2);
3513e1db26aSLionel Sambuc }
3523e1db26aSLionel Sambuc 
3533e1db26aSLionel Sambuc /*
3543e1db26aSLionel Sambuc  * Display list of strings in columnar format on readline's output stream.
3553e1db26aSLionel Sambuc  * 'matches' is list of strings, 'num' is number of strings in 'matches',
3563e1db26aSLionel Sambuc  * 'width' is maximum length of string in 'matches'.
3573e1db26aSLionel Sambuc  *
3583e1db26aSLionel Sambuc  * matches[0] is not one of the match strings, but it is counted in
3593e1db26aSLionel Sambuc  * num, so the strings are matches[1] *through* matches[num-1].
3603e1db26aSLionel Sambuc  */
3613e1db26aSLionel Sambuc void
fn_display_match_list(EditLine * el,char ** matches,size_t num,size_t width)3623e1db26aSLionel Sambuc fn_display_match_list (EditLine *el, char **matches, size_t num, size_t width)
3633e1db26aSLionel Sambuc {
3643e1db26aSLionel Sambuc 	size_t line, lines, col, cols, thisguy;
3653e1db26aSLionel Sambuc 	int screenwidth = el->el_terminal.t_size.h;
3663e1db26aSLionel Sambuc 
3673e1db26aSLionel Sambuc 	/* Ignore matches[0]. Avoid 1-based array logic below. */
3683e1db26aSLionel Sambuc 	matches++;
3693e1db26aSLionel Sambuc 	num--;
3703e1db26aSLionel Sambuc 
3713e1db26aSLionel Sambuc 	/*
3723e1db26aSLionel Sambuc 	 * Find out how many entries can be put on one line; count
3733e1db26aSLionel Sambuc 	 * with one space between strings the same way it's printed.
3743e1db26aSLionel Sambuc 	 */
3753e1db26aSLionel Sambuc 	cols = (size_t)screenwidth / (width + 1);
3763e1db26aSLionel Sambuc 	if (cols == 0)
3773e1db26aSLionel Sambuc 		cols = 1;
3783e1db26aSLionel Sambuc 
3793e1db26aSLionel Sambuc 	/* how many lines of output, rounded up */
3803e1db26aSLionel Sambuc 	lines = (num + cols - 1) / cols;
3813e1db26aSLionel Sambuc 
3823e1db26aSLionel Sambuc 	/* Sort the items. */
3833e1db26aSLionel Sambuc 	qsort(matches, num, sizeof(char *), _fn_qsort_string_compare);
3843e1db26aSLionel Sambuc 
3853e1db26aSLionel Sambuc 	/*
3863e1db26aSLionel Sambuc 	 * On the ith line print elements i, i+lines, i+lines*2, etc.
3873e1db26aSLionel Sambuc 	 */
3883e1db26aSLionel Sambuc 	for (line = 0; line < lines; line++) {
3893e1db26aSLionel Sambuc 		for (col = 0; col < cols; col++) {
3903e1db26aSLionel Sambuc 			thisguy = line + col * lines;
3913e1db26aSLionel Sambuc 			if (thisguy >= num)
3923e1db26aSLionel Sambuc 				break;
3933e1db26aSLionel Sambuc 			(void)fprintf(el->el_outfile, "%s%-*s",
3943e1db26aSLionel Sambuc 			    col == 0 ? "" : " ", (int)width, matches[thisguy]);
3953e1db26aSLionel Sambuc 		}
3963e1db26aSLionel Sambuc 		(void)fprintf(el->el_outfile, "\n");
3973e1db26aSLionel Sambuc 	}
3983e1db26aSLionel Sambuc }
3993e1db26aSLionel Sambuc 
4003e1db26aSLionel Sambuc /*
4013e1db26aSLionel Sambuc  * Complete the word at or before point,
4023e1db26aSLionel Sambuc  * 'what_to_do' says what to do with the completion.
4033e1db26aSLionel Sambuc  * \t   means do standard completion.
4043e1db26aSLionel Sambuc  * `?' means list the possible completions.
4053e1db26aSLionel Sambuc  * `*' means insert all of the possible completions.
4063e1db26aSLionel Sambuc  * `!' means to do standard completion, and list all possible completions if
4073e1db26aSLionel Sambuc  * there is more than one.
4083e1db26aSLionel Sambuc  *
4093e1db26aSLionel Sambuc  * Note: '*' support is not implemented
4103e1db26aSLionel Sambuc  *       '!' could never be invoked
4113e1db26aSLionel Sambuc  */
4123e1db26aSLionel Sambuc int
fn_complete(EditLine * el,char * (* complet_func)(const char *,int),char ** (* attempted_completion_function)(const char *,int,int),const Char * word_break,const Char * special_prefixes,const char * (* app_func)(const char *),size_t query_items,int * completion_type,int * over,int * point,int * end)4133e1db26aSLionel Sambuc fn_complete(EditLine *el,
4143e1db26aSLionel Sambuc 	char *(*complet_func)(const char *, int),
4153e1db26aSLionel Sambuc 	char **(*attempted_completion_function)(const char *, int, int),
4163e1db26aSLionel Sambuc 	const Char *word_break, const Char *special_prefixes,
4173e1db26aSLionel Sambuc 	const char *(*app_func)(const char *), size_t query_items,
4183e1db26aSLionel Sambuc 	int *completion_type, int *over, int *point, int *end)
4193e1db26aSLionel Sambuc {
4203e1db26aSLionel Sambuc 	const TYPE(LineInfo) *li;
4213e1db26aSLionel Sambuc 	Char *temp;
4223e1db26aSLionel Sambuc         char **matches;
4233e1db26aSLionel Sambuc 	const Char *ctemp;
4243e1db26aSLionel Sambuc 	size_t len;
4253e1db26aSLionel Sambuc 	int what_to_do = '\t';
4263e1db26aSLionel Sambuc 	int retval = CC_NORM;
4273e1db26aSLionel Sambuc 
4283e1db26aSLionel Sambuc 	if (el->el_state.lastcmd == el->el_state.thiscmd)
4293e1db26aSLionel Sambuc 		what_to_do = '?';
4303e1db26aSLionel Sambuc 
4313e1db26aSLionel Sambuc 	/* readline's rl_complete() has to be told what we did... */
4323e1db26aSLionel Sambuc 	if (completion_type != NULL)
4333e1db26aSLionel Sambuc 		*completion_type = what_to_do;
4343e1db26aSLionel Sambuc 
4353e1db26aSLionel Sambuc 	if (!complet_func)
4363e1db26aSLionel Sambuc 		complet_func = fn_filename_completion_function;
4373e1db26aSLionel Sambuc 	if (!app_func)
4383e1db26aSLionel Sambuc 		app_func = append_char_function;
4393e1db26aSLionel Sambuc 
4403e1db26aSLionel Sambuc 	/* We now look backwards for the start of a filename/variable word */
4413e1db26aSLionel Sambuc 	li = FUN(el,line)(el);
4423e1db26aSLionel Sambuc 	ctemp = li->cursor;
4433e1db26aSLionel Sambuc 	while (ctemp > li->buffer
4443e1db26aSLionel Sambuc 	    && !Strchr(word_break, ctemp[-1])
4453e1db26aSLionel Sambuc 	    && (!special_prefixes || !Strchr(special_prefixes, ctemp[-1]) ) )
4463e1db26aSLionel Sambuc 		ctemp--;
4473e1db26aSLionel Sambuc 
4483e1db26aSLionel Sambuc 	len = (size_t)(li->cursor - ctemp);
4493e1db26aSLionel Sambuc 	temp = el_malloc((len + 1) * sizeof(*temp));
4503e1db26aSLionel Sambuc 	(void)Strncpy(temp, ctemp, len);
4513e1db26aSLionel Sambuc 	temp[len] = '\0';
4523e1db26aSLionel Sambuc 
4533e1db26aSLionel Sambuc 	/* these can be used by function called in completion_matches() */
4543e1db26aSLionel Sambuc 	/* or (*attempted_completion_function)() */
4553e1db26aSLionel Sambuc 	if (point != 0)
4563e1db26aSLionel Sambuc 		*point = (int)(li->cursor - li->buffer);
4573e1db26aSLionel Sambuc 	if (end != NULL)
4583e1db26aSLionel Sambuc 		*end = (int)(li->lastchar - li->buffer);
4593e1db26aSLionel Sambuc 
4603e1db26aSLionel Sambuc 	if (attempted_completion_function) {
4613e1db26aSLionel Sambuc 		int cur_off = (int)(li->cursor - li->buffer);
4623e1db26aSLionel Sambuc 		matches = (*attempted_completion_function)(
4633e1db26aSLionel Sambuc 		    ct_encode_string(temp, &el->el_scratch),
4643e1db26aSLionel Sambuc 		    cur_off - (int)len, cur_off);
4653e1db26aSLionel Sambuc 	} else
4663e1db26aSLionel Sambuc 		matches = 0;
4673e1db26aSLionel Sambuc 	if (!attempted_completion_function ||
4683e1db26aSLionel Sambuc 	    (over != NULL && !*over && !matches))
4693e1db26aSLionel Sambuc 		matches = completion_matches(
4703e1db26aSLionel Sambuc 		    ct_encode_string(temp, &el->el_scratch), complet_func);
4713e1db26aSLionel Sambuc 
4723e1db26aSLionel Sambuc 	if (over != NULL)
4733e1db26aSLionel Sambuc 		*over = 0;
4743e1db26aSLionel Sambuc 
4753e1db26aSLionel Sambuc 	if (matches) {
4763e1db26aSLionel Sambuc 		int i;
4773e1db26aSLionel Sambuc 		size_t matches_num, maxlen, match_len, match_display=1;
4783e1db26aSLionel Sambuc 
4793e1db26aSLionel Sambuc 		retval = CC_REFRESH;
4803e1db26aSLionel Sambuc 		/*
4813e1db26aSLionel Sambuc 		 * Only replace the completed string with common part of
4823e1db26aSLionel Sambuc 		 * possible matches if there is possible completion.
4833e1db26aSLionel Sambuc 		 */
4843e1db26aSLionel Sambuc 		if (matches[0][0] != '\0') {
4853e1db26aSLionel Sambuc 			el_deletestr(el, (int) len);
4863e1db26aSLionel Sambuc 			FUN(el,insertstr)(el,
4873e1db26aSLionel Sambuc 			    ct_decode_string(matches[0], &el->el_scratch));
4883e1db26aSLionel Sambuc 		}
4893e1db26aSLionel Sambuc 
4903e1db26aSLionel Sambuc 		if (what_to_do == '?')
4913e1db26aSLionel Sambuc 			goto display_matches;
4923e1db26aSLionel Sambuc 
493*0a6a1f1dSLionel Sambuc 		if (matches[2] == NULL &&
494*0a6a1f1dSLionel Sambuc 		    (matches[1] == NULL || strcmp(matches[0], matches[1]) == 0)) {
4953e1db26aSLionel Sambuc 			/*
4963e1db26aSLionel Sambuc 			 * We found exact match. Add a space after
4973e1db26aSLionel Sambuc 			 * it, unless we do filename completion and the
4983e1db26aSLionel Sambuc 			 * object is a directory.
4993e1db26aSLionel Sambuc 			 */
5003e1db26aSLionel Sambuc 			FUN(el,insertstr)(el,
5013e1db26aSLionel Sambuc 			    ct_decode_string((*app_func)(matches[0]),
5023e1db26aSLionel Sambuc 			    &el->el_scratch));
5033e1db26aSLionel Sambuc 		} else if (what_to_do == '!') {
5043e1db26aSLionel Sambuc     display_matches:
5053e1db26aSLionel Sambuc 			/*
5063e1db26aSLionel Sambuc 			 * More than one match and requested to list possible
5073e1db26aSLionel Sambuc 			 * matches.
5083e1db26aSLionel Sambuc 			 */
5093e1db26aSLionel Sambuc 
5103e1db26aSLionel Sambuc 			for(i = 1, maxlen = 0; matches[i]; i++) {
5113e1db26aSLionel Sambuc 				match_len = strlen(matches[i]);
5123e1db26aSLionel Sambuc 				if (match_len > maxlen)
5133e1db26aSLionel Sambuc 					maxlen = match_len;
5143e1db26aSLionel Sambuc 			}
5153e1db26aSLionel Sambuc 			/* matches[1] through matches[i-1] are available */
5163e1db26aSLionel Sambuc 			matches_num = (size_t)(i - 1);
5173e1db26aSLionel Sambuc 
5183e1db26aSLionel Sambuc 			/* newline to get on next line from command line */
5193e1db26aSLionel Sambuc 			(void)fprintf(el->el_outfile, "\n");
5203e1db26aSLionel Sambuc 
5213e1db26aSLionel Sambuc 			/*
5223e1db26aSLionel Sambuc 			 * If there are too many items, ask user for display
5233e1db26aSLionel Sambuc 			 * confirmation.
5243e1db26aSLionel Sambuc 			 */
5253e1db26aSLionel Sambuc 			if (matches_num > query_items) {
5263e1db26aSLionel Sambuc 				(void)fprintf(el->el_outfile,
5273e1db26aSLionel Sambuc 				    "Display all %zu possibilities? (y or n) ",
5283e1db26aSLionel Sambuc 				    matches_num);
5293e1db26aSLionel Sambuc 				(void)fflush(el->el_outfile);
5303e1db26aSLionel Sambuc 				if (getc(stdin) != 'y')
5313e1db26aSLionel Sambuc 					match_display = 0;
5323e1db26aSLionel Sambuc 				(void)fprintf(el->el_outfile, "\n");
5333e1db26aSLionel Sambuc 			}
5343e1db26aSLionel Sambuc 
5353e1db26aSLionel Sambuc 			if (match_display) {
5363e1db26aSLionel Sambuc 				/*
5373e1db26aSLionel Sambuc 				 * Interface of this function requires the
5383e1db26aSLionel Sambuc 				 * strings be matches[1..num-1] for compat.
5393e1db26aSLionel Sambuc 				 * We have matches_num strings not counting
5403e1db26aSLionel Sambuc 				 * the prefix in matches[0], so we need to
5413e1db26aSLionel Sambuc 				 * add 1 to matches_num for the call.
5423e1db26aSLionel Sambuc 				 */
5433e1db26aSLionel Sambuc 				fn_display_match_list(el, matches,
5443e1db26aSLionel Sambuc 				    matches_num+1, maxlen);
5453e1db26aSLionel Sambuc 			}
5463e1db26aSLionel Sambuc 			retval = CC_REDISPLAY;
5473e1db26aSLionel Sambuc 		} else if (matches[0][0]) {
5483e1db26aSLionel Sambuc 			/*
5493e1db26aSLionel Sambuc 			 * There was some common match, but the name was
5503e1db26aSLionel Sambuc 			 * not complete enough. Next tab will print possible
5513e1db26aSLionel Sambuc 			 * completions.
5523e1db26aSLionel Sambuc 			 */
5533e1db26aSLionel Sambuc 			el_beep(el);
5543e1db26aSLionel Sambuc 		} else {
5553e1db26aSLionel Sambuc 			/* lcd is not a valid object - further specification */
5563e1db26aSLionel Sambuc 			/* is needed */
5573e1db26aSLionel Sambuc 			el_beep(el);
5583e1db26aSLionel Sambuc 			retval = CC_NORM;
5593e1db26aSLionel Sambuc 		}
5603e1db26aSLionel Sambuc 
5613e1db26aSLionel Sambuc 		/* free elements of array and the array itself */
5623e1db26aSLionel Sambuc 		for (i = 0; matches[i]; i++)
5633e1db26aSLionel Sambuc 			el_free(matches[i]);
5643e1db26aSLionel Sambuc 		el_free(matches);
5653e1db26aSLionel Sambuc 		matches = NULL;
5663e1db26aSLionel Sambuc 	}
5673e1db26aSLionel Sambuc 	el_free(temp);
5683e1db26aSLionel Sambuc 	return retval;
5693e1db26aSLionel Sambuc }
5703e1db26aSLionel Sambuc 
5713e1db26aSLionel Sambuc /*
5723e1db26aSLionel Sambuc  * el-compatible wrapper around rl_complete; needed for key binding
5733e1db26aSLionel Sambuc  */
5743e1db26aSLionel Sambuc /* ARGSUSED */
5753e1db26aSLionel Sambuc unsigned char
_el_fn_complete(EditLine * el,int ch)5763e1db26aSLionel Sambuc _el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
5773e1db26aSLionel Sambuc {
5783e1db26aSLionel Sambuc 	return (unsigned char)fn_complete(el, NULL, NULL,
5793e1db26aSLionel Sambuc 	    break_chars, NULL, NULL, (size_t)100,
5803e1db26aSLionel Sambuc 	    NULL, NULL, NULL, NULL);
5813e1db26aSLionel Sambuc }
582