blob: 189798c1a1c3336d879cfb72d7fb71b420bc5e60 [file] [log] [blame]
Haibo Huang892b8b32019-11-25 15:32:55 -08001/* $NetBSD: filecomplete.c,v 1.61 2019/10/09 14:31:07 christos Exp $ */
Todd Fiala12e21682013-10-30 10:02:25 -07002
3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jaromir Dolecek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
33
34#if !defined(lint) && !defined(SCCSID)
Haibo Huang892b8b32019-11-25 15:32:55 -080035__RCSID("$NetBSD: filecomplete.c,v 1.61 2019/10/09 14:31:07 christos Exp $");
Todd Fiala12e21682013-10-30 10:02:25 -070036#endif /* not lint && not SCCSID */
37
38#include <sys/types.h>
39#include <sys/stat.h>
Todd Fiala12e21682013-10-30 10:02:25 -070040#include <dirent.h>
Todd Fiala12e21682013-10-30 10:02:25 -070041#include <errno.h>
42#include <fcntl.h>
Haibo Huang5691f772019-08-28 15:21:20 -070043#include <limits.h>
44#include <pwd.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
Todd Fiala12e21682013-10-30 10:02:25 -070049
50#include "el.h"
Todd Fiala12e21682013-10-30 10:02:25 -070051#include "filecomplete.h"
52
Haibo Huang5691f772019-08-28 15:21:20 -070053static const wchar_t break_chars[] = L" \t\n\"\\'`@$><=;|&{(";
Todd Fiala12e21682013-10-30 10:02:25 -070054
55/********************************/
56/* completion functions */
57
58/*
59 * does tilde expansion of strings of type ``~user/foo''
60 * if ``user'' isn't valid user name or ``txt'' doesn't start
61 * w/ '~', returns pointer to strdup()ed copy of ``txt''
62 *
Haibo Huang5691f772019-08-28 15:21:20 -070063 * it's the caller's responsibility to free() the returned string
Todd Fiala12e21682013-10-30 10:02:25 -070064 */
65char *
66fn_tilde_expand(const char *txt)
67{
68#if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
69 struct passwd pwres;
70 char pwbuf[1024];
71#endif
72 struct passwd *pass;
73 char *temp;
74 size_t len = 0;
75
76 if (txt[0] != '~')
77 return strdup(txt);
78
79 temp = strchr(txt + 1, '/');
80 if (temp == NULL) {
81 temp = strdup(txt + 1);
82 if (temp == NULL)
83 return NULL;
84 } else {
85 /* text until string after slash */
86 len = (size_t)(temp - txt + 1);
Haibo Huang892b8b32019-11-25 15:32:55 -080087 temp = el_calloc(len, sizeof(*temp));
Todd Fiala12e21682013-10-30 10:02:25 -070088 if (temp == NULL)
89 return NULL;
Haibo Huang892b8b32019-11-25 15:32:55 -080090 (void)strlcpy(temp, txt + 1, len - 1);
Todd Fiala12e21682013-10-30 10:02:25 -070091 }
92 if (temp[0] == 0) {
93#ifdef HAVE_GETPW_R_POSIX
Haibo Huang5691f772019-08-28 15:21:20 -070094 if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
Todd Fiala12e21682013-10-30 10:02:25 -070095 &pass) != 0)
Haibo Huang5691f772019-08-28 15:21:20 -070096 pass = NULL;
Todd Fiala12e21682013-10-30 10:02:25 -070097#elif HAVE_GETPW_R_DRAFT
98 pass = getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf));
99#else
100 pass = getpwuid(getuid());
101#endif
102 } else {
103#ifdef HAVE_GETPW_R_POSIX
104 if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
105 pass = NULL;
106#elif HAVE_GETPW_R_DRAFT
107 pass = getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf));
108#else
109 pass = getpwnam(temp);
110#endif
111 }
112 el_free(temp); /* value no more needed */
113 if (pass == NULL)
114 return strdup(txt);
115
116 /* update pointer txt to point at string immedially following */
117 /* first slash */
118 txt += len;
119
120 len = strlen(pass->pw_dir) + 1 + strlen(txt) + 1;
Haibo Huang892b8b32019-11-25 15:32:55 -0800121 temp = el_calloc(len, sizeof(*temp));
Todd Fiala12e21682013-10-30 10:02:25 -0700122 if (temp == NULL)
123 return NULL;
124 (void)snprintf(temp, len, "%s/%s", pass->pw_dir, txt);
125
126 return temp;
127}
128
Haibo Huang5691f772019-08-28 15:21:20 -0700129static int
130needs_escaping(char c)
131{
132 switch (c) {
133 case '\'':
134 case '"':
135 case '(':
136 case ')':
137 case '\\':
138 case '<':
139 case '>':
140 case '$':
141 case '#':
142 case ' ':
143 case '\n':
144 case '\t':
145 case '?':
146 case ';':
147 case '`':
148 case '@':
149 case '=':
150 case '|':
151 case '{':
152 case '}':
153 case '&':
154 case '*':
155 case '[':
156 return 1;
157 default:
158 return 0;
159 }
160}
161
Haibo Huang892b8b32019-11-25 15:32:55 -0800162static int
163needs_dquote_escaping(char c)
164{
165 switch (c) {
166 case '"':
167 case '\\':
168 case '`':
169 case '$':
170 return 1;
171 default:
172 return 0;
173 }
174}
175
176
177static wchar_t *
178unescape_string(const wchar_t *string, size_t length)
179{
180 size_t i;
181 size_t j = 0;
182 wchar_t *unescaped = el_calloc(length + 1, sizeof(*string));
183 if (unescaped == NULL)
184 return NULL;
185 for (i = 0; i < length ; i++) {
186 if (string[i] == '\\')
187 continue;
188 unescaped[j++] = string[i];
189 }
190 unescaped[j] = 0;
191 return unescaped;
192}
193
Haibo Huang5691f772019-08-28 15:21:20 -0700194static char *
Haibo Huang892b8b32019-11-25 15:32:55 -0800195escape_filename(EditLine * el, const char *filename, int single_match,
196 const char *(*app_func)(const char *))
Haibo Huang5691f772019-08-28 15:21:20 -0700197{
198 size_t original_len = 0;
199 size_t escaped_character_count = 0;
200 size_t offset = 0;
201 size_t newlen;
202 const char *s;
203 char c;
204 size_t s_quoted = 0; /* does the input contain a single quote */
205 size_t d_quoted = 0; /* does the input contain a double quote */
206 char *escaped_str;
207 wchar_t *temp = el->el_line.buffer;
Haibo Huang892b8b32019-11-25 15:32:55 -0800208 const char *append_char = NULL;
209
210 if (filename == NULL)
211 return NULL;
Haibo Huang5691f772019-08-28 15:21:20 -0700212
213 while (temp != el->el_line.cursor) {
214 /*
Haibo Huang892b8b32019-11-25 15:32:55 -0800215 * If we see a single quote but have not seen a double quote
216 * so far set/unset s_quote
Haibo Huang5691f772019-08-28 15:21:20 -0700217 */
218 if (temp[0] == '\'' && !d_quoted)
219 s_quoted = !s_quoted;
220 /*
221 * vice versa to the above condition
222 */
223 else if (temp[0] == '"' && !s_quoted)
224 d_quoted = !d_quoted;
225 temp++;
226 }
227
228 /* Count number of special characters so that we can calculate
229 * number of extra bytes needed in the new string
230 */
231 for (s = filename; *s; s++, original_len++) {
232 c = *s;
233 /* Inside a single quote only single quotes need escaping */
234 if (s_quoted && c == '\'') {
235 escaped_character_count += 3;
236 continue;
237 }
238 /* Inside double quotes only ", \, ` and $ need escaping */
Haibo Huang892b8b32019-11-25 15:32:55 -0800239 if (d_quoted && needs_dquote_escaping(c)) {
Haibo Huang5691f772019-08-28 15:21:20 -0700240 escaped_character_count++;
241 continue;
242 }
243 if (!s_quoted && !d_quoted && needs_escaping(c))
244 escaped_character_count++;
245 }
246
247 newlen = original_len + escaped_character_count + 1;
Haibo Huang892b8b32019-11-25 15:32:55 -0800248 if (s_quoted || d_quoted)
249 newlen++;
250
251 if (single_match && app_func)
252 newlen++;
253
Haibo Huang5691f772019-08-28 15:21:20 -0700254 if ((escaped_str = el_malloc(newlen)) == NULL)
255 return NULL;
256
257 for (s = filename; *s; s++) {
258 c = *s;
259 if (!needs_escaping(c)) {
260 /* no escaping is required continue as usual */
261 escaped_str[offset++] = c;
262 continue;
263 }
264
265 /* single quotes inside single quotes require special handling */
266 if (c == '\'' && s_quoted) {
267 escaped_str[offset++] = '\'';
268 escaped_str[offset++] = '\\';
269 escaped_str[offset++] = '\'';
270 escaped_str[offset++] = '\'';
271 continue;
272 }
273
274 /* Otherwise no escaping needed inside single quotes */
275 if (s_quoted) {
276 escaped_str[offset++] = c;
277 continue;
278 }
279
280 /* No escaping needed inside a double quoted string either
281 * unless we see a '$', '\', '`', or '"' (itself)
282 */
Haibo Huang892b8b32019-11-25 15:32:55 -0800283 if (d_quoted && !needs_dquote_escaping(c)) {
Haibo Huang5691f772019-08-28 15:21:20 -0700284 escaped_str[offset++] = c;
285 continue;
286 }
287
288 /* If we reach here that means escaping is actually needed */
289 escaped_str[offset++] = '\\';
290 escaped_str[offset++] = c;
291 }
292
Haibo Huang892b8b32019-11-25 15:32:55 -0800293 if (single_match && app_func) {
294 escaped_str[offset] = 0;
295 append_char = app_func(escaped_str);
296 /* we want to append space only if we are not inside quotes */
297 if (append_char[0] == ' ') {
298 if (!s_quoted && !d_quoted)
299 escaped_str[offset++] = append_char[0];
300 } else
301 escaped_str[offset++] = append_char[0];
302 }
303
304 /* close the quotes if single match and the match is not a directory */
305 if (single_match && (append_char && append_char[0] == ' ')) {
306 if (s_quoted)
307 escaped_str[offset++] = '\'';
308 else if (d_quoted)
309 escaped_str[offset++] = '"';
310 }
Haibo Huang5691f772019-08-28 15:21:20 -0700311
312 escaped_str[offset] = 0;
313 return escaped_str;
314}
Todd Fiala12e21682013-10-30 10:02:25 -0700315
316/*
317 * return first found file name starting by the ``text'' or NULL if no
318 * such file can be found
319 * value of ``state'' is ignored
320 *
Haibo Huang5691f772019-08-28 15:21:20 -0700321 * it's the caller's responsibility to free the returned string
Todd Fiala12e21682013-10-30 10:02:25 -0700322 */
323char *
324fn_filename_completion_function(const char *text, int state)
325{
326 static DIR *dir = NULL;
327 static char *filename = NULL, *dirname = NULL, *dirpath = NULL;
328 static size_t filename_len = 0;
329 struct dirent *entry;
330 char *temp;
331 size_t len;
332
333 if (state == 0 || dir == NULL) {
334 temp = strrchr(text, '/');
335 if (temp) {
336 char *nptr;
337 temp++;
338 nptr = el_realloc(filename, (strlen(temp) + 1) *
339 sizeof(*nptr));
340 if (nptr == NULL) {
341 el_free(filename);
342 filename = NULL;
343 return NULL;
344 }
345 filename = nptr;
346 (void)strcpy(filename, temp);
347 len = (size_t)(temp - text); /* including last slash */
348
349 nptr = el_realloc(dirname, (len + 1) *
350 sizeof(*nptr));
351 if (nptr == NULL) {
352 el_free(dirname);
353 dirname = NULL;
354 return NULL;
355 }
356 dirname = nptr;
Haibo Huang892b8b32019-11-25 15:32:55 -0800357 (void)strlcpy(dirname, text, len + 1);
Todd Fiala12e21682013-10-30 10:02:25 -0700358 } else {
359 el_free(filename);
360 if (*text == 0)
361 filename = NULL;
362 else {
363 filename = strdup(text);
364 if (filename == NULL)
365 return NULL;
366 }
367 el_free(dirname);
368 dirname = NULL;
369 }
370
371 if (dir != NULL) {
372 (void)closedir(dir);
373 dir = NULL;
374 }
375
376 /* support for ``~user'' syntax */
377
378 el_free(dirpath);
379 dirpath = NULL;
380 if (dirname == NULL) {
381 if ((dirname = strdup("")) == NULL)
382 return NULL;
383 dirpath = strdup("./");
384 } else if (*dirname == '~')
385 dirpath = fn_tilde_expand(dirname);
386 else
387 dirpath = strdup(dirname);
388
389 if (dirpath == NULL)
390 return NULL;
391
392 dir = opendir(dirpath);
393 if (!dir)
394 return NULL; /* cannot open the directory */
395
396 /* will be used in cycle */
397 filename_len = filename ? strlen(filename) : 0;
398 }
399
400 /* find the match */
401 while ((entry = readdir(dir)) != NULL) {
402 /* skip . and .. */
403 if (entry->d_name[0] == '.' && (!entry->d_name[1]
404 || (entry->d_name[1] == '.' && !entry->d_name[2])))
405 continue;
406 if (filename_len == 0)
407 break;
408 /* otherwise, get first entry where first */
409 /* filename_len characters are equal */
410 if (entry->d_name[0] == filename[0]
411 /* Some dirents have d_namlen, but it is not portable. */
412 && strlen(entry->d_name) >= filename_len
413 && strncmp(entry->d_name, filename,
414 filename_len) == 0)
415 break;
416 }
417
418 if (entry) { /* match found */
419
420 /* Some dirents have d_namlen, but it is not portable. */
421 len = strlen(entry->d_name);
422
423 len = strlen(dirname) + len + 1;
Haibo Huang892b8b32019-11-25 15:32:55 -0800424 temp = el_calloc(len, sizeof(*temp));
Todd Fiala12e21682013-10-30 10:02:25 -0700425 if (temp == NULL)
426 return NULL;
427 (void)snprintf(temp, len, "%s%s", dirname, entry->d_name);
428 } else {
429 (void)closedir(dir);
430 dir = NULL;
431 temp = NULL;
432 }
433
434 return temp;
435}
436
437
438static const char *
439append_char_function(const char *name)
440{
441 struct stat stbuf;
442 char *expname = *name == '~' ? fn_tilde_expand(name) : NULL;
443 const char *rs = " ";
444
445 if (stat(expname ? expname : name, &stbuf) == -1)
446 goto out;
447 if (S_ISDIR(stbuf.st_mode))
448 rs = "/";
449out:
450 if (expname)
451 el_free(expname);
452 return rs;
453}
454/*
455 * returns list of completions for text given
456 * non-static for readline.
457 */
458char ** completion_matches(const char *, char *(*)(const char *, int));
459char **
460completion_matches(const char *text, char *(*genfunc)(const char *, int))
461{
462 char **match_list = NULL, *retstr, *prevstr;
463 size_t match_list_len, max_equal, which, i;
464 size_t matches;
465
466 matches = 0;
467 match_list_len = 1;
468 while ((retstr = (*genfunc) (text, (int)matches)) != NULL) {
469 /* allow for list terminator here */
470 if (matches + 3 >= match_list_len) {
471 char **nmatch_list;
472 while (matches + 3 >= match_list_len)
473 match_list_len <<= 1;
474 nmatch_list = el_realloc(match_list,
475 match_list_len * sizeof(*nmatch_list));
476 if (nmatch_list == NULL) {
477 el_free(match_list);
478 return NULL;
479 }
480 match_list = nmatch_list;
481
482 }
483 match_list[++matches] = retstr;
484 }
485
486 if (!match_list)
487 return NULL; /* nothing found */
488
489 /* find least denominator and insert it to match_list[0] */
490 which = 2;
491 prevstr = match_list[1];
492 max_equal = strlen(prevstr);
493 for (; which <= matches; which++) {
494 for (i = 0; i < max_equal &&
495 prevstr[i] == match_list[which][i]; i++)
496 continue;
497 max_equal = i;
498 }
499
Haibo Huang892b8b32019-11-25 15:32:55 -0800500 retstr = el_calloc(max_equal + 1, sizeof(*retstr));
Todd Fiala12e21682013-10-30 10:02:25 -0700501 if (retstr == NULL) {
502 el_free(match_list);
503 return NULL;
504 }
Haibo Huang892b8b32019-11-25 15:32:55 -0800505 (void)strlcpy(retstr, match_list[1], max_equal + 1);
Todd Fiala12e21682013-10-30 10:02:25 -0700506 match_list[0] = retstr;
507
508 /* add NULL as last pointer to the array */
509 match_list[matches + 1] = NULL;
510
511 return match_list;
512}
513
514/*
515 * Sort function for qsort(). Just wrapper around strcasecmp().
516 */
517static int
518_fn_qsort_string_compare(const void *i1, const void *i2)
519{
520 const char *s1 = ((const char * const *)i1)[0];
521 const char *s2 = ((const char * const *)i2)[0];
522
523 return strcasecmp(s1, s2);
524}
525
526/*
527 * Display list of strings in columnar format on readline's output stream.
528 * 'matches' is list of strings, 'num' is number of strings in 'matches',
529 * 'width' is maximum length of string in 'matches'.
530 *
531 * matches[0] is not one of the match strings, but it is counted in
532 * num, so the strings are matches[1] *through* matches[num-1].
533 */
534void
Haibo Huang5691f772019-08-28 15:21:20 -0700535fn_display_match_list(EditLine * el, char **matches, size_t num, size_t width,
536 const char *(*app_func) (const char *))
Todd Fiala12e21682013-10-30 10:02:25 -0700537{
538 size_t line, lines, col, cols, thisguy;
539 int screenwidth = el->el_terminal.t_size.h;
Haibo Huang5691f772019-08-28 15:21:20 -0700540 if (app_func == NULL)
541 app_func = append_char_function;
Todd Fiala12e21682013-10-30 10:02:25 -0700542
543 /* Ignore matches[0]. Avoid 1-based array logic below. */
544 matches++;
545 num--;
546
547 /*
548 * Find out how many entries can be put on one line; count
549 * with one space between strings the same way it's printed.
550 */
Haibo Huang892b8b32019-11-25 15:32:55 -0800551 cols = (size_t)screenwidth / (width + 2);
Todd Fiala12e21682013-10-30 10:02:25 -0700552 if (cols == 0)
553 cols = 1;
554
555 /* how many lines of output, rounded up */
556 lines = (num + cols - 1) / cols;
557
558 /* Sort the items. */
559 qsort(matches, num, sizeof(char *), _fn_qsort_string_compare);
560
561 /*
562 * On the ith line print elements i, i+lines, i+lines*2, etc.
563 */
564 for (line = 0; line < lines; line++) {
565 for (col = 0; col < cols; col++) {
566 thisguy = line + col * lines;
567 if (thisguy >= num)
568 break;
Haibo Huang5691f772019-08-28 15:21:20 -0700569 (void)fprintf(el->el_outfile, "%s%s%s",
570 col == 0 ? "" : " ", matches[thisguy],
Haibo Huang892b8b32019-11-25 15:32:55 -0800571 (*app_func)(matches[thisguy]));
Haibo Huang5691f772019-08-28 15:21:20 -0700572 (void)fprintf(el->el_outfile, "%-*s",
573 (int) (width - strlen(matches[thisguy])), "");
Todd Fiala12e21682013-10-30 10:02:25 -0700574 }
575 (void)fprintf(el->el_outfile, "\n");
576 }
577}
578
Haibo Huang5691f772019-08-28 15:21:20 -0700579static wchar_t *
580find_word_to_complete(const wchar_t * cursor, const wchar_t * buffer,
581 const wchar_t * word_break, const wchar_t * special_prefixes, size_t * length)
582{
583 /* We now look backwards for the start of a filename/variable word */
584 const wchar_t *ctemp = cursor;
Haibo Huang5691f772019-08-28 15:21:20 -0700585 size_t len;
Haibo Huang5691f772019-08-28 15:21:20 -0700586
587 /* if the cursor is placed at a slash or a quote, we need to find the
588 * word before it
589 */
590 if (ctemp > buffer) {
591 switch (ctemp[-1]) {
592 case '\\':
593 case '\'':
594 case '"':
Haibo Huang5691f772019-08-28 15:21:20 -0700595 ctemp--;
596 break;
597 default:
Haibo Huang892b8b32019-11-25 15:32:55 -0800598 break;
Haibo Huang5691f772019-08-28 15:21:20 -0700599 }
Haibo Huang892b8b32019-11-25 15:32:55 -0800600 }
Haibo Huang5691f772019-08-28 15:21:20 -0700601
Haibo Huang892b8b32019-11-25 15:32:55 -0800602 for (;;) {
603 if (ctemp <= buffer)
604 break;
605 if (wcschr(word_break, ctemp[-1])) {
606 if (ctemp - buffer >= 2 && ctemp[-2] == '\\') {
607 ctemp -= 2;
608 continue;
609 } else if (ctemp - buffer >= 2 &&
610 (ctemp[-2] == '\'' || ctemp[-2] == '"')) {
611 ctemp--;
612 continue;
613 } else
614 break;
615 }
616 if (special_prefixes && wcschr(special_prefixes, ctemp[-1]))
617 break;
Haibo Huang5691f772019-08-28 15:21:20 -0700618 ctemp--;
Haibo Huang892b8b32019-11-25 15:32:55 -0800619 }
Haibo Huang5691f772019-08-28 15:21:20 -0700620
Haibo Huang892b8b32019-11-25 15:32:55 -0800621 len = (size_t) (cursor - ctemp);
622 if (len == 1 && (ctemp[0] == '\'' || ctemp[0] == '"')) {
623 len = 0;
624 ctemp++;
625 }
Haibo Huang5691f772019-08-28 15:21:20 -0700626 *length = len;
Haibo Huang892b8b32019-11-25 15:32:55 -0800627 wchar_t *unescaped_word = unescape_string(ctemp, len);
628 if (unescaped_word == NULL)
629 return NULL;
630 return unescaped_word;
Haibo Huang5691f772019-08-28 15:21:20 -0700631}
632
Todd Fiala12e21682013-10-30 10:02:25 -0700633/*
634 * Complete the word at or before point,
635 * 'what_to_do' says what to do with the completion.
636 * \t means do standard completion.
637 * `?' means list the possible completions.
638 * `*' means insert all of the possible completions.
639 * `!' means to do standard completion, and list all possible completions if
640 * there is more than one.
641 *
642 * Note: '*' support is not implemented
643 * '!' could never be invoked
644 */
645int
646fn_complete(EditLine *el,
647 char *(*complet_func)(const char *, int),
648 char **(*attempted_completion_function)(const char *, int, int),
Haibo Huang5691f772019-08-28 15:21:20 -0700649 const wchar_t *word_break, const wchar_t *special_prefixes,
Todd Fiala12e21682013-10-30 10:02:25 -0700650 const char *(*app_func)(const char *), size_t query_items,
651 int *completion_type, int *over, int *point, int *end)
652{
Haibo Huang5691f772019-08-28 15:21:20 -0700653 const LineInfoW *li;
654 wchar_t *temp;
655 char **matches;
Haibo Huang892b8b32019-11-25 15:32:55 -0800656 char *completion;
Todd Fiala12e21682013-10-30 10:02:25 -0700657 size_t len;
658 int what_to_do = '\t';
659 int retval = CC_NORM;
660
661 if (el->el_state.lastcmd == el->el_state.thiscmd)
662 what_to_do = '?';
663
664 /* readline's rl_complete() has to be told what we did... */
665 if (completion_type != NULL)
666 *completion_type = what_to_do;
667
668 if (!complet_func)
669 complet_func = fn_filename_completion_function;
670 if (!app_func)
671 app_func = append_char_function;
672
Haibo Huang5691f772019-08-28 15:21:20 -0700673 li = el_wline(el);
674 temp = find_word_to_complete(li->cursor,
675 li->buffer, word_break, special_prefixes, &len);
676 if (temp == NULL)
677 goto out;
Todd Fiala12e21682013-10-30 10:02:25 -0700678
679 /* these can be used by function called in completion_matches() */
680 /* or (*attempted_completion_function)() */
Haibo Huang5691f772019-08-28 15:21:20 -0700681 if (point != NULL)
Todd Fiala12e21682013-10-30 10:02:25 -0700682 *point = (int)(li->cursor - li->buffer);
683 if (end != NULL)
684 *end = (int)(li->lastchar - li->buffer);
685
686 if (attempted_completion_function) {
687 int cur_off = (int)(li->cursor - li->buffer);
688 matches = (*attempted_completion_function)(
689 ct_encode_string(temp, &el->el_scratch),
690 cur_off - (int)len, cur_off);
691 } else
Haibo Huang5691f772019-08-28 15:21:20 -0700692 matches = NULL;
693 if (!attempted_completion_function ||
Todd Fiala12e21682013-10-30 10:02:25 -0700694 (over != NULL && !*over && !matches))
695 matches = completion_matches(
696 ct_encode_string(temp, &el->el_scratch), complet_func);
697
698 if (over != NULL)
699 *over = 0;
700
701 if (matches) {
702 int i;
703 size_t matches_num, maxlen, match_len, match_display=1;
Haibo Huang5691f772019-08-28 15:21:20 -0700704 int single_match = matches[2] == NULL &&
705 (matches[1] == NULL || strcmp(matches[0], matches[1]) == 0);
Todd Fiala12e21682013-10-30 10:02:25 -0700706
707 retval = CC_REFRESH;
Haibo Huang5691f772019-08-28 15:21:20 -0700708
Todd Fiala12e21682013-10-30 10:02:25 -0700709 if (matches[0][0] != '\0') {
Haibo Huang892b8b32019-11-25 15:32:55 -0800710 el_deletestr(el, (int)len);
711 if (!attempted_completion_function)
712 completion = escape_filename(el, matches[0],
713 single_match, app_func);
714 else
715 completion = strdup(matches[0]);
716 if (completion == NULL)
717 goto out;
Haibo Huang5691f772019-08-28 15:21:20 -0700718 if (single_match) {
Haibo Huang892b8b32019-11-25 15:32:55 -0800719 /* We found exact match. Add a space after it,
720 * unless we do filename completion and the
721 * object is a directory. Also do necessary
722 * escape quoting
Haibo Huang5691f772019-08-28 15:21:20 -0700723 */
Haibo Huang5691f772019-08-28 15:21:20 -0700724 el_winsertstr(el,
Haibo Huang892b8b32019-11-25 15:32:55 -0800725 ct_decode_string(completion, &el->el_scratch));
Haibo Huang5691f772019-08-28 15:21:20 -0700726 } else {
Haibo Huang892b8b32019-11-25 15:32:55 -0800727 /* Only replace the completed string with
728 * common part of possible matches if there is
729 * possible completion.
Haibo Huang5691f772019-08-28 15:21:20 -0700730 */
731 el_winsertstr(el,
Haibo Huang892b8b32019-11-25 15:32:55 -0800732 ct_decode_string(completion, &el->el_scratch));
Haibo Huang5691f772019-08-28 15:21:20 -0700733 }
Haibo Huang892b8b32019-11-25 15:32:55 -0800734 free(completion);
Todd Fiala12e21682013-10-30 10:02:25 -0700735 }
736
Todd Fiala12e21682013-10-30 10:02:25 -0700737
Haibo Huang5691f772019-08-28 15:21:20 -0700738 if (!single_match && (what_to_do == '!' || what_to_do == '?')) {
Todd Fiala12e21682013-10-30 10:02:25 -0700739 /*
740 * More than one match and requested to list possible
741 * matches.
742 */
743
744 for(i = 1, maxlen = 0; matches[i]; i++) {
745 match_len = strlen(matches[i]);
746 if (match_len > maxlen)
747 maxlen = match_len;
748 }
749 /* matches[1] through matches[i-1] are available */
750 matches_num = (size_t)(i - 1);
Haibo Huang5691f772019-08-28 15:21:20 -0700751
Todd Fiala12e21682013-10-30 10:02:25 -0700752 /* newline to get on next line from command line */
753 (void)fprintf(el->el_outfile, "\n");
754
755 /*
756 * If there are too many items, ask user for display
757 * confirmation.
758 */
759 if (matches_num > query_items) {
760 (void)fprintf(el->el_outfile,
761 "Display all %zu possibilities? (y or n) ",
762 matches_num);
763 (void)fflush(el->el_outfile);
764 if (getc(stdin) != 'y')
765 match_display = 0;
766 (void)fprintf(el->el_outfile, "\n");
767 }
768
769 if (match_display) {
770 /*
771 * Interface of this function requires the
772 * strings be matches[1..num-1] for compat.
773 * We have matches_num strings not counting
774 * the prefix in matches[0], so we need to
775 * add 1 to matches_num for the call.
776 */
777 fn_display_match_list(el, matches,
Haibo Huang5691f772019-08-28 15:21:20 -0700778 matches_num+1, maxlen, app_func);
Todd Fiala12e21682013-10-30 10:02:25 -0700779 }
780 retval = CC_REDISPLAY;
781 } else if (matches[0][0]) {
782 /*
783 * There was some common match, but the name was
784 * not complete enough. Next tab will print possible
785 * completions.
786 */
787 el_beep(el);
788 } else {
789 /* lcd is not a valid object - further specification */
790 /* is needed */
791 el_beep(el);
792 retval = CC_NORM;
793 }
794
795 /* free elements of array and the array itself */
796 for (i = 0; matches[i]; i++)
797 el_free(matches[i]);
798 el_free(matches);
799 matches = NULL;
800 }
Haibo Huang5691f772019-08-28 15:21:20 -0700801
802out:
Todd Fiala12e21682013-10-30 10:02:25 -0700803 el_free(temp);
804 return retval;
805}
806
807/*
808 * el-compatible wrapper around rl_complete; needed for key binding
809 */
810/* ARGSUSED */
811unsigned char
812_el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
813{
814 return (unsigned char)fn_complete(el, NULL, NULL,
815 break_chars, NULL, NULL, (size_t)100,
816 NULL, NULL, NULL, NULL);
817}