1 /* $OpenBSD: filecomplete.c,v 1.12 2016/04/11 20:43:33 schwarze Exp $ */
2 /* $NetBSD: filecomplete.c,v 1.22 2010/12/02 04:42:46 dholland Exp $ */
5 * Copyright (c) 1997 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
48 #include "filecomplete.h"
50 static const wchar_t break_chars
[] = L
" \t\n\"\\'`@$><=;|&{(";
52 /********************************/
53 /* completion functions */
56 * does tilde expansion of strings of type ``~user/foo''
57 * if ``user'' isn't valid user name or ``txt'' doesn't start
58 * w/ '~', returns pointer to strdup()ed copy of ``txt''
60 * it's the caller's responsibility to free() the returned string
63 fn_tilde_expand(const char *txt
)
65 struct passwd pwres
, *pass
;
67 size_t tempsz
, len
= 0;
73 temp
= strchr(txt
+ 1, '/');
75 temp
= strdup(txt
+ 1);
79 len
= temp
- txt
+ 1; /* text until string after slash */
83 (void)strncpy(temp
, txt
+ 1, len
- 2);
87 if (getpwuid_r(getuid(), &pwres
, pwbuf
, sizeof(pwbuf
), &pass
) != 0)
90 if (getpwnam_r(temp
, &pwres
, pwbuf
, sizeof(pwbuf
), &pass
) != 0)
93 free(temp
); /* value no more needed */
97 /* update pointer txt to point at string immedially following */
101 tempsz
= strlen(pass
->pw_dir
) + 1 + strlen(txt
) + 1;
102 temp
= malloc(tempsz
);
105 (void)snprintf(temp
, tempsz
, "%s/%s", pass
->pw_dir
, txt
);
112 * return first found file name starting by the ``text'' or NULL if no
113 * such file can be found
114 * value of ``state'' is ignored
116 * it's the caller's responsibility to free the returned string
119 fn_filename_completion_function(const char *text
, int state
)
121 static DIR *dir
= NULL
;
122 static char *filename
= NULL
, *dirname
= NULL
, *dirpath
= NULL
;
123 static size_t filename_len
= 0;
124 struct dirent
*entry
;
128 if (state
== 0 || dir
== NULL
) {
129 temp
= strrchr(text
, '/');
131 size_t sz
= strlen(temp
+ 1) + 1;
134 nptr
= realloc(filename
, sz
);
141 (void)strlcpy(filename
, temp
, sz
);
142 len
= temp
- text
; /* including last slash */
144 nptr
= realloc(dirname
, len
+ 1);
151 (void)strncpy(dirname
, text
, len
);
158 filename
= strdup(text
);
159 if (filename
== NULL
)
171 /* support for ``~user'' syntax */
175 if (dirname
== NULL
) {
176 if ((dirname
= strdup("")) == NULL
)
178 dirpath
= strdup("./");
179 } else if (*dirname
== '~')
180 dirpath
= fn_tilde_expand(dirname
);
182 dirpath
= strdup(dirname
);
187 dir
= opendir(dirpath
);
189 return NULL
; /* cannot open the directory */
191 /* will be used in cycle */
192 filename_len
= filename
? strlen(filename
) : 0;
196 while ((entry
= readdir(dir
)) != NULL
) {
198 if (entry
->d_name
[0] == '.' && (!entry
->d_name
[1]
199 || (entry
->d_name
[1] == '.' && !entry
->d_name
[2])))
201 if (filename_len
== 0)
203 /* otherwise, get first entry where first */
204 /* filename_len characters are equal */
205 if (entry
->d_name
[0] == filename
[0]
206 #if HAVE_STRUCT_DIRENT_D_NAMLEN
207 && entry
->d_namlen
>= filename_len
209 && strlen(entry
->d_name
) >= filename_len
211 && strncmp(entry
->d_name
, filename
,
216 if (entry
) { /* match found */
218 #if HAVE_STRUCT_DIRENT_D_NAMLEN
219 len
= entry
->d_namlen
;
221 len
= strlen(entry
->d_name
);
224 tempsz
= strlen(dirname
) + len
+ 1;
225 temp
= malloc(tempsz
);
228 (void)snprintf(temp
, tempsz
, "%s%s", dirname
, entry
->d_name
);
240 append_char_function(const char *name
)
243 char *expname
= *name
== '~' ? fn_tilde_expand(name
) : NULL
;
244 const char *rs
= " ";
246 if (stat(expname
? expname
: name
, &stbuf
) == -1)
248 if (S_ISDIR(stbuf
.st_mode
))
256 * returns list of completions for text given
257 * non-static for readline.
259 char ** completion_matches(const char *, char *(*)(const char *, int));
261 completion_matches(const char *text
, char *(*genfunc
)(const char *, int))
263 char **match_list
= NULL
, *retstr
, *prevstr
;
264 size_t match_list_len
, max_equal
, which
, i
;
269 while ((retstr
= (*genfunc
) (text
, (int)matches
)) != NULL
) {
270 /* allow for list terminator here */
271 if (matches
+ 3 >= match_list_len
) {
273 while (matches
+ 3 >= match_list_len
)
274 match_list_len
<<= 1;
275 nmatch_list
= reallocarray(match_list
,
276 match_list_len
, sizeof(char *));
277 if (nmatch_list
== NULL
) {
281 match_list
= nmatch_list
;
284 match_list
[++matches
] = retstr
;
288 return NULL
; /* nothing found */
290 /* find least denominator and insert it to match_list[0] */
292 prevstr
= match_list
[1];
293 max_equal
= strlen(prevstr
);
294 for (; which
<= matches
; which
++) {
295 for (i
= 0; i
< max_equal
&&
296 prevstr
[i
] == match_list
[which
][i
]; i
++)
301 retstr
= malloc(max_equal
+ 1);
302 if (retstr
== NULL
) {
306 (void)strncpy(retstr
, match_list
[1], max_equal
);
307 retstr
[max_equal
] = '\0';
308 match_list
[0] = retstr
;
310 /* add NULL as last pointer to the array */
311 match_list
[matches
+ 1] = NULL
;
317 * Sort function for qsort(). Just wrapper around strcasecmp().
320 _fn_qsort_string_compare(const void *i1
, const void *i2
)
322 const char *s1
= ((const char * const *)i1
)[0];
323 const char *s2
= ((const char * const *)i2
)[0];
325 return strcasecmp(s1
, s2
);
329 * Display list of strings in columnar format on readline's output stream.
330 * 'matches' is list of strings, 'num' is number of strings in 'matches',
331 * 'width' is maximum length of string in 'matches'.
333 * matches[0] is not one of the match strings, but it is counted in
334 * num, so the strings are matches[1] *through* matches[num-1].
337 fn_display_match_list (EditLine
*el
, char **matches
, size_t num
, size_t width
)
339 size_t line
, lines
, col
, cols
, thisguy
;
340 int screenwidth
= el
->el_terminal
.t_size
.h
;
342 /* Ignore matches[0]. Avoid 1-based array logic below. */
347 * Find out how many entries can be put on one line; count
348 * with one space between strings the same way it's printed.
350 cols
= screenwidth
/ (width
+ 1);
354 /* how many lines of output, rounded up */
355 lines
= (num
+ cols
- 1) / cols
;
357 /* Sort the items. */
358 qsort(matches
, num
, sizeof(char *), _fn_qsort_string_compare
);
361 * On the ith line print elements i, i+lines, i+lines*2, etc.
363 for (line
= 0; line
< lines
; line
++) {
364 for (col
= 0; col
< cols
; col
++) {
365 thisguy
= line
+ col
* lines
;
368 (void)fprintf(el
->el_outfile
, "%s%-*s",
369 col
== 0 ? "" : " ", (int)width
, matches
[thisguy
]);
371 (void)fprintf(el
->el_outfile
, "\n");
376 * Complete the word at or before point,
377 * 'what_to_do' says what to do with the completion.
378 * \t means do standard completion.
379 * `?' means list the possible completions.
380 * `*' means insert all of the possible completions.
381 * `!' means to do standard completion, and list all possible completions if
382 * there is more than one.
384 * Note: '*' support is not implemented
385 * '!' could never be invoked
388 fn_complete(EditLine
*el
,
389 char *(*complet_func
)(const char *, int),
390 char **(*attempted_completion_function
)(const char *, int, int),
391 const wchar_t *word_break
, const wchar_t *special_prefixes
,
392 const char *(*app_func
)(const char *), size_t query_items
,
393 int *completion_type
, int *over
, int *point
, int *end
)
398 const wchar_t *ctemp
;
400 int what_to_do
= '\t';
401 int retval
= CC_NORM
;
403 if (el
->el_state
.lastcmd
== el
->el_state
.thiscmd
)
406 /* readline's rl_complete() has to be told what we did... */
407 if (completion_type
!= NULL
)
408 *completion_type
= what_to_do
;
411 complet_func
= fn_filename_completion_function
;
413 app_func
= append_char_function
;
415 /* We now look backwards for the start of a filename/variable word */
418 while (ctemp
> li
->buffer
419 && !wcschr(word_break
, ctemp
[-1])
420 && (!special_prefixes
|| !wcschr(special_prefixes
, ctemp
[-1]) ) )
423 len
= li
->cursor
- ctemp
;
424 temp
= reallocarray(NULL
, len
+ 1, sizeof(*temp
));
425 (void)wcsncpy(temp
, ctemp
, len
);
428 /* these can be used by function called in completion_matches() */
429 /* or (*attempted_completion_function)() */
431 *point
= (int)(li
->cursor
- li
->buffer
);
433 *end
= (int)(li
->lastchar
- li
->buffer
);
435 if (attempted_completion_function
) {
436 int cur_off
= (int)(li
->cursor
- li
->buffer
);
437 matches
= (*attempted_completion_function
) (
438 ct_encode_string(temp
, &el
->el_scratch
),
439 (int)(cur_off
- len
), cur_off
);
442 if (!attempted_completion_function
||
443 (over
!= NULL
&& !*over
&& !matches
))
444 matches
= completion_matches(
445 ct_encode_string(temp
, &el
->el_scratch
), complet_func
);
452 size_t matches_num
, maxlen
, match_len
, match_display
=1;
456 * Only replace the completed string with common part of
457 * possible matches if there is possible completion.
459 if (matches
[0][0] != '\0') {
460 el_deletestr(el
, (int) len
);
462 ct_decode_string(matches
[0], &el
->el_scratch
));
465 if (what_to_do
== '?')
466 goto display_matches
;
468 if (matches
[2] == NULL
&& strcmp(matches
[0], matches
[1]) == 0) {
470 * We found exact match. Add a space after
471 * it, unless we do filename completion and the
472 * object is a directory.
475 ct_decode_string((*app_func
)(matches
[0]),
477 } else if (what_to_do
== '!') {
480 * More than one match and requested to list possible
484 for(i
= 1, maxlen
= 0; matches
[i
]; i
++) {
485 match_len
= strlen(matches
[i
]);
486 if (match_len
> maxlen
)
489 /* matches[1] through matches[i-1] are available */
492 /* newline to get on next line from command line */
493 (void)fprintf(el
->el_outfile
, "\n");
496 * If there are too many items, ask user for display
499 if (matches_num
> query_items
) {
500 (void)fprintf(el
->el_outfile
,
501 "Display all %zu possibilities? (y or n) ",
503 (void)fflush(el
->el_outfile
);
504 if (getc(stdin
) != 'y')
506 (void)fprintf(el
->el_outfile
, "\n");
511 * Interface of this function requires the
512 * strings be matches[1..num-1] for compat.
513 * We have matches_num strings not counting
514 * the prefix in matches[0], so we need to
515 * add 1 to matches_num for the call.
517 fn_display_match_list(el
, matches
,
518 matches_num
+1, maxlen
);
520 retval
= CC_REDISPLAY
;
521 } else if (matches
[0][0]) {
523 * There was some common match, but the name was
524 * not complete enough. Next tab will print possible
529 /* lcd is not a valid object - further specification */
535 /* free elements of array and the array itself */
536 for (i
= 0; matches
[i
]; i
++)
546 * el-compatible wrapper around rl_complete; needed for key binding
550 _el_fn_complete(EditLine
*el
, int ch
__attribute__((__unused__
)))
552 return (unsigned char)fn_complete(el
, NULL
, NULL
,
553 break_chars
, NULL
, NULL
, 100,
554 NULL
, NULL
, NULL
, NULL
);