2 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * $NetBSD: filecomplete.c,v 1.11 2008/04/29 06:53:01 martin Exp $
30 * $DragonFly: src/lib/libedit/filecomplete.c,v 1.3 2008/05/17 22:48:04 pavalos Exp $
35 #include <sys/types.h>
56 #include "fcns.h" /* for EL_NUM_FCNS */
58 #include "filecomplete.h"
60 static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
61 '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
64 /********************************/
65 /* completion functions */
68 * does tilde expansion of strings of type ``~user/foo''
69 * if ``user'' isn't valid user name or ``txt'' doesn't start
70 * w/ '~', returns pointer to strdup()ed copy of ``txt''
72 * it's callers's responsibility to free() returned string
75 fn_tilde_expand(const char *txt)
78 XXX: replace the next line with this line when getpwuid_r or
79 getpwnam_r become available:
80 struct passwd pwres, *pass;
90 temp = strchr(txt + 1, '/');
92 temp = strdup(txt + 1);
96 len = temp - txt + 1; /* text until string after slash */
100 (void)strncpy(temp, txt + 1, len - 2);
101 temp[len - 2] = '\0';
105 XXX: use the following instead of the next line when
106 getpwuid_r is available:
107 if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
110 pass = getpwuid(getuid());
114 XXX: use the following instead of the next line when
115 getpwname_r is available:
116 if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
119 pass = getpwnam(temp);
121 free(temp); /* value no more needed */
123 return (strdup(txt));
125 /* update pointer txt to point at string immedially following */
129 temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1);
132 (void)sprintf(temp, "%s/%s", pass->pw_dir, txt);
139 * return first found file name starting by the ``text'' or NULL if no
140 * such file can be found
141 * value of ``state'' is ignored
143 * it's caller's responsibility to free returned string
146 fn_filename_completion_function(const char *text, int state)
148 static DIR *dir = NULL;
149 static char *filename = NULL, *dirname = NULL, *dirpath = NULL;
150 static size_t filename_len = 0;
151 struct dirent *entry;
155 if (state == 0 || dir == NULL) {
156 temp = strrchr(text, '/');
160 nptr = realloc(filename, strlen(temp) + 1);
166 (void)strcpy(filename, temp);
167 len = temp - text; /* including last slash */
168 nptr = realloc(dirname, len + 1);
174 (void)strncpy(dirname, text, len);
180 filename = strdup(text);
181 if (filename == NULL)
192 /* support for ``~user'' syntax */
195 if (dirname == NULL && (dirname = strdup("./")) == NULL)
199 dirpath = fn_tilde_expand(dirname);
201 dirpath = strdup(dirname);
206 dir = opendir(dirpath);
208 return (NULL); /* cannot open the directory */
210 /* will be used in cycle */
211 filename_len = filename ? strlen(filename) : 0;
215 while ((entry = readdir(dir)) != NULL) {
217 if (entry->d_name[0] == '.' && (!entry->d_name[1]
218 || (entry->d_name[1] == '.' && !entry->d_name[2])))
220 if (filename_len == 0)
222 /* otherwise, get first entry where first */
223 /* filename_len characters are equal */
224 if (entry->d_name[0] == filename[0]
225 #if defined(__SVR4) || defined(__linux__)
226 && strlen(entry->d_name) >= filename_len
228 && entry->d_namlen >= filename_len
230 && strncmp(entry->d_name, filename,
235 if (entry) { /* match found */
237 #if defined(__SVR4) || defined(__linux__)
238 len = strlen(entry->d_name);
240 len = entry->d_namlen;
243 temp = malloc(strlen(dirname) + len + 1);
246 (void)sprintf(temp, "%s%s", dirname, entry->d_name);
258 append_char_function(const char *name)
261 char *expname = *name == '~' ? fn_tilde_expand(name) : NULL;
264 if (stat(expname ? expname : name, &stbuf) == -1)
266 if (S_ISDIR(stbuf.st_mode))
274 * returns list of completions for text given
275 * non-static for readline.
277 char ** completion_matches(const char *, char *(*)(const char *, int));
279 completion_matches(const char *text, char *(*genfunc)(const char *, int))
281 char **match_list = NULL, *retstr, *prevstr;
282 size_t match_list_len, max_equal, which, i;
287 while ((retstr = (*genfunc) (text, (int)matches)) != NULL) {
288 /* allow for list terminator here */
289 if (matches + 3 >= match_list_len) {
291 while (matches + 3 >= match_list_len)
292 match_list_len <<= 1;
293 nmatch_list = realloc(match_list,
294 match_list_len * sizeof(char *));
295 if (nmatch_list == NULL) {
299 match_list = nmatch_list;
302 match_list[++matches] = retstr;
306 return NULL; /* nothing found */
308 /* find least denominator and insert it to match_list[0] */
310 prevstr = match_list[1];
311 max_equal = strlen(prevstr);
312 for (; which <= matches; which++) {
313 for (i = 0; i < max_equal &&
314 prevstr[i] == match_list[which][i]; i++)
319 retstr = malloc(max_equal + 1);
320 if (retstr == NULL) {
324 (void)strncpy(retstr, match_list[1], max_equal);
325 retstr[max_equal] = '\0';
326 match_list[0] = retstr;
328 /* add NULL as last pointer to the array */
329 match_list[matches + 1] = NULL;
335 * Sort function for qsort(). Just wrapper around strcasecmp().
338 _fn_qsort_string_compare(const void *i1, const void *i2)
340 const char *s1 = ((const char * const *)i1)[0];
341 const char *s2 = ((const char * const *)i2)[0];
343 return strcasecmp(s1, s2);
347 * Display list of strings in columnar format on readline's output stream.
348 * 'matches' is list of strings, 'len' is number of strings in 'matches',
349 * 'max' is maximum length of string in 'matches'.
352 fn_display_match_list (EditLine *el, char **matches, int len, int max)
354 int i, idx, limit, count;
355 int screenwidth = el->el_term.t_size.h;
358 * Find out how many entries can be put on one line, count
359 * with two spaces between strings.
361 limit = screenwidth / (max + 2);
365 /* how many lines of output */
367 if (count * limit < len)
370 /* Sort the items if they are not already sorted. */
371 qsort(&matches[1], (size_t)(len - 1), sizeof(char *),
372 _fn_qsort_string_compare);
375 for(; count > 0; count--) {
376 for(i = 0; i < limit && matches[idx]; i++, idx++)
377 (void)fprintf(el->el_outfile, "%-*s ", max,
379 (void)fprintf(el->el_outfile, "\n");
384 * Complete the word at or before point,
385 * 'what_to_do' says what to do with the completion.
386 * \t means do standard completion.
387 * `?' means list the possible completions.
388 * `*' means insert all of the possible completions.
389 * `!' means to do standard completion, and list all possible completions if
390 * there is more than one.
392 * Note: '*' support is not implemented
393 * '!' could never be invoked
396 fn_complete(EditLine *el,
397 char *(*complet_func)(const char *, int),
398 char **(*attempted_completion_function)(const char *, int, int),
399 const char *word_break, const char *special_prefixes,
400 const char *(*app_func)(const char *), int query_items,
401 int *completion_type, int *over, int *point, int *end)
404 char *temp, **matches;
407 int what_to_do = '\t';
408 int retval = CC_NORM;
410 if (el->el_state.lastcmd == el->el_state.thiscmd)
413 /* readline's rl_complete() has to be told what we did... */
414 if (completion_type != NULL)
415 *completion_type = what_to_do;
418 complet_func = fn_filename_completion_function;
420 app_func = append_char_function;
422 /* We now look backwards for the start of a filename/variable word */
424 ctemp = (const char *) li->cursor;
425 while (ctemp > li->buffer
426 && !strchr(word_break, ctemp[-1])
427 && (!special_prefixes || !strchr(special_prefixes, ctemp[-1]) ) )
430 len = li->cursor - ctemp;
431 #if defined(__SSP__) || defined(__SSP_ALL__)
432 temp = malloc(len + 1);
434 temp = alloca(len + 1);
436 (void)strncpy(temp, ctemp, len);
439 /* these can be used by function called in completion_matches() */
440 /* or (*attempted_completion_function)() */
442 *point = li->cursor - li->buffer;
444 *end = li->lastchar - li->buffer;
446 if (attempted_completion_function) {
447 int cur_off = li->cursor - li->buffer;
448 matches = (*attempted_completion_function) (temp,
449 (int)(cur_off - len), cur_off);
452 if (!attempted_completion_function ||
453 (over != NULL && !*over && !matches))
454 matches = completion_matches(temp, complet_func);
461 int matches_num, maxlen, match_len, match_display=1;
465 * Only replace the completed string with common part of
466 * possible matches if there is possible completion.
468 if (matches[0][0] != '\0') {
469 el_deletestr(el, (int) len);
470 el_insertstr(el, matches[0]);
473 if (what_to_do == '?')
474 goto display_matches;
476 if (matches[2] == NULL && strcmp(matches[0], matches[1]) == 0) {
478 * We found exact match. Add a space after
479 * it, unless we do filename completion and the
480 * object is a directory.
482 el_insertstr(el, (*append_char_function)(matches[0]));
483 } else if (what_to_do == '!') {
486 * More than one match and requested to list possible
490 for(i=1, maxlen=0; matches[i]; i++) {
491 match_len = strlen(matches[i]);
492 if (match_len > maxlen)
497 /* newline to get on next line from command line */
498 (void)fprintf(el->el_outfile, "\n");
501 * If there are too many items, ask user for display
504 if (matches_num > query_items) {
505 (void)fprintf(el->el_outfile,
506 "Display all %d possibilities? (y or n) ",
508 (void)fflush(el->el_outfile);
509 if (getc(stdin) != 'y')
511 (void)fprintf(el->el_outfile, "\n");
515 fn_display_match_list(el, matches, matches_num,
517 retval = CC_REDISPLAY;
518 } else if (matches[0][0]) {
520 * There was some common match, but the name was
521 * not complete enough. Next tab will print possible
526 /* lcd is not a valid object - further specification */
532 /* free elements of array and the array itself */
533 for (i = 0; matches[i]; i++)
538 #if defined(__SSP__) || defined(__SSP_ALL__)
545 * el-compatible wrapper around rl_complete; needed for key binding
549 _el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
551 return (unsigned char)fn_complete(el, NULL, NULL,
552 break_chars, NULL, NULL, 100,
553 NULL, NULL, NULL, NULL);