-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathcomplete.c
More file actions
1640 lines (1480 loc) · 54.3 KB
/
Copy pathcomplete.c
File metadata and controls
1640 lines (1480 loc) · 54.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Yash: yet another shell */
/* complete.c: command line completion */
/* (C) 2007-2026 magicant */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "../common.h"
#include "complete.h"
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#if HAVE_GETGRENT
# include <grp.h>
#endif
#if HAVE_GETTEXT
# include <libintl.h>
#endif
#if HAVE_GETHOSTENT
# include <netdb.h>
#endif
#if HAVE_GETPWENT
# include <pwd.h>
#endif
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include <sys/stat.h>
#include "../builtin.h"
#include "../exec.h"
#include "../expand.h"
#include "../hashtable.h"
#include "../option.h"
#include "../parser.h"
#include "../path.h"
#include "../plist.h"
#include "../redir.h"
#include "../sig.h"
#include "../strbuf.h"
#include "../util.h"
#include "../variable.h"
#include "../xfnmatch.h"
#include "../yash.h"
#include "compparse.h"
#include "display.h"
#include "editing.h"
#include "lineedit.h"
#include "terminfo.h"
#if HAVE_WCSCASECMP
# ifndef wcscasecmp
extern int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
__attribute__((nonnull));
# endif
#endif
#if HAVE_GETPWENT
# ifndef setpwent
extern void setpwent(void);
# endif
# ifndef getpwent
extern struct passwd *getpwent(void);
# endif
# ifndef endpwent
extern void endpwent(void);
# endif
#endif
#if HAVE_GETGRENT
# if 0 /* avoid conflict on BSD */
extern void setgrent(void);
# endif
# ifndef getgrent
extern struct group *getgrent(void);
# endif
# ifndef endgrent
extern void endgrent(void);
# endif
#endif
#if HAVE_GETHOSTENT
# if 0 /* avoid conflict on SunOS */
extern void sethostent(int);
# endif
# ifndef gethostent
extern struct hostent *gethostent(void);
# endif
# if 0 /* avoid conflict on SunOS */
extern void endhostent(void);
# endif
#endif
static void select_candidate(void selector(int offset), int offset)
__attribute__((nonnull));
static void select_candidate_by_offset(int offset);
static void free_candidate(void *c)
__attribute__((nonnull));
static void free_context(le_context_T *ctxt);
static void sort_candidates(void);
static int sort_candidates_cmp(const void *cp1, const void *cp2)
__attribute__((nonnull));
static void print_context_info(const le_context_T *ctxt)
__attribute__((nonnull));
static void print_compopt_info(const le_compopt_T *compopt)
__attribute__((nonnull));
static void execute_completion_function(void);
static void complete_command_default(void);
static void simple_completion(le_candgentype_T type);
static void generate_candidates(const le_compopt_T *compopt)
__attribute__((nonnull));
static bool le_match_patterns(const le_comppattern_T *ps, const char *s)
__attribute__((nonnull(2)));
static bool le_wmatch_patterns(const le_comppattern_T *ps, const wchar_t *s)
__attribute__((nonnull(2)));
static void generate_file_candidates(const le_compopt_T *compopt)
__attribute__((nonnull));
static void generate_external_command_candidates(const le_compopt_T *compopt)
__attribute__((nonnull));
static void generate_keyword_candidates(const le_compopt_T *compopt)
__attribute__((nonnull));
static void generate_logname_candidates(const le_compopt_T *compopt)
__attribute__((nonnull));
static void generate_group_candidates(const le_compopt_T *compopt)
__attribute__((nonnull));
static void generate_host_candidates(const le_compopt_T *compopt)
__attribute__((nonnull));
static void generate_candidates_from_words(
le_candtype_T type, void *const *words, const wchar_t *description,
const le_compopt_T *compopt)
__attribute__((nonnull(2,4)));
static void word_completion(size_t count, ...)
__attribute__((nonnull));
static size_t get_common_prefix_length(void)
__attribute__((pure));
static void update_main_buffer(bool subst, bool finish);
static void insert_to_main_buffer(wchar_t c);
static bool need_subst(void);
static void substitute_source_word_all(void);
static void quote(xwcsbuf_T *restrict buf,
const wchar_t *restrict s, le_quote_T quotetype)
__attribute__((nonnull));
/* The current completion context. */
static le_context_T *ctxt = NULL;
/* A list that contains the current completion candidates.
* The elements pointed to by `le_candidates.contains[*]' are of type
* `le_candidate_T'. */
plist_T le_candidates = { .contents = NULL };
/* The index of the currently selected candidate in `le_candidates'.
* When no candidate is selected, the index is `le_candidates.length'. */
size_t le_selected_candidate_index;
/* The length of the longest common prefix of the current candidates.
* The value is ((size_t) -1) when not computed. */
static size_t common_prefix_length;
/* Performs command line completion.
* Existing candidates are deleted, if any, and candidates are computed from
* the current command line.
* `lecr' is called after candidate generation. */
void le_complete(le_compresult_T lecr)
{
if (shopt_le_compdebug) {
/* If the `le-compdebug' option is set, the command line is temporarily
* cleared during completion.
* Note that `shopt_le_compdebug' is referenced only here. During the
* completion, we check the value of `le_state' to test if the option
* is set. The value of `shopt_le_compdebug' might be changed by a
* candidate generator code. */
le_display_finalize();
le_restore_terminal();
le_state = LE_STATE_SUSPENDED | LE_STATE_COMPLETING;
le_compdebug("completion start");
} else {
le_state |= LE_STATE_COMPLETING;
le_allow_terminal_signal(true);
}
le_complete_cleanup();
pl_init(&le_candidates);
common_prefix_length = (size_t) -1;
ctxt = le_get_context();
if (le_state_is_compdebug)
print_context_info(ctxt);
execute_completion_function();
sort_candidates();
le_compdebug("total of %zu candidate(s)", le_candidates.length);
/* display the results */
lecr();
if (le_state_is_compdebug) {
le_compdebug("completion end");
le_setupterm(true);
le_set_terminal();
} else {
assert((le_state & (LE_STATE_ACTIVE | LE_STATE_COMPLETING))
== (LE_STATE_ACTIVE | LE_STATE_COMPLETING));
le_allow_terminal_signal(false);
/* the terminal size may have been changed during completion, so we
* re-check the terminal state here. */
le_display_clear(false);
le_setupterm(true);
}
le_state = LE_STATE_ACTIVE;
}
/* An `le_compresult_T' function that does nothing. */
void lecr_nop(void)
{
}
/* An `le_compresult_T' function for `cmd_complete'. */
void lecr_normal(void)
{
if (le_candidates.length == 0) {
le_selected_candidate_index = 0;
} else if (ctxt->substsrc || need_subst()) {
le_selected_candidate_index = 0;
substitute_source_word_all();
le_complete_cleanup();
} else if (le_candidates.length == 1) {
le_selected_candidate_index = 0;
update_main_buffer(false, true);
le_complete_cleanup();
} else {
le_selected_candidate_index = le_candidates.length;
le_display_make_rawvalues();
update_main_buffer(false, false);
}
}
/* An `le_compresult_T' function for `cmd_vi_complete_all'. */
void lecr_substitute_all_candidates(void)
{
le_selected_candidate_index = 0;
if (le_candidates.length == 0) {
lebuf_print_alert(true);
} else {
substitute_source_word_all();
}
le_complete_cleanup();
}
/* An `le_compresult_T' function for `cmd_vi_complete_max'. */
void lecr_longest_common_prefix(void)
{
le_selected_candidate_index = 0;
if (le_candidates.length == 0) {
lebuf_print_alert(true);
} else {
bool subst = ctxt->substsrc || need_subst();
if (le_candidates.length > 1) {
le_selected_candidate_index = le_candidates.length;
update_main_buffer(subst, false);
} else {
update_main_buffer(subst, true);
}
}
le_complete_cleanup();
}
/* Updates `le_selected_candidate_index' by calling `selector' with the argument
* `offset'. The `selector' function must update `le_selected_candidate_index'
* according to the given `offset'.
* The main buffer contents is also updated so that it contains the value of the
* new selected candidate.
* If there are no candidates, `le_complete' is called to produce candidates. */
void select_candidate(void selector(int offset), int offset)
{
if (le_candidates.contents == NULL) {
le_complete(lecr_normal);
return;
} else if (le_candidates.length == 0) {
return;
}
selector(offset);
update_main_buffer(false, false);
}
/* Increases `le_selected_candidate_index' by `offset', selecting the `offset'th
* next candidate. If there are no candidates, simply calls `le_complete' to
* produce candidates. */
void le_complete_select_candidate(int offset)
{
select_candidate(select_candidate_by_offset, offset);
}
void select_candidate_by_offset(int offset)
{
assert(le_selected_candidate_index <= le_candidates.length);
if (offset >= 0) {
offset %= le_candidates.length + 1;
le_selected_candidate_index += offset;
le_selected_candidate_index %= le_candidates.length + 1;
} else {
offset = -offset % (le_candidates.length + 1);
if ((size_t) offset <= le_selected_candidate_index)
le_selected_candidate_index -= offset;
else
le_selected_candidate_index += le_candidates.length - offset + 1;
}
assert(le_selected_candidate_index <= le_candidates.length);
}
/* Selects the first candidate of the `offset'th next column.
* If there are no candidates, simply calls `le_complete' to produce candidates.
*/
void le_complete_select_column(int offset)
{
select_candidate(le_display_select_column, offset);
}
/* Selects the first candidate of the `offset'th next page.
* If there are no candidates, simply calls `le_complete' to produce candidates.
*/
void le_complete_select_page(int offset)
{
select_candidate(le_display_select_page, offset);
}
/* If `index' is not positive, performs completion and lists candidates.
* Otherwise, substitutes the source word with the `index'th candidate and
* cleans up.
* Returns true iff the source word was successfully substituted. */
bool le_complete_fix_candidate(int index)
{
if (le_candidates.contents == NULL) {
le_complete(lecr_nop);
le_selected_candidate_index = le_candidates.length;
le_display_make_rawvalues();
}
if (le_candidates.length == 0) {
lebuf_print_alert(true);
return false;
}
if (index <= 0)
return false;
unsigned uindex = (unsigned) index - 1;
if (uindex >= le_candidates.length) {
lebuf_print_alert(true);
return false;
}
le_selected_candidate_index = uindex;
bool subst = ctxt->substsrc;
if (!subst) {
const le_candidate_T *cand =
le_candidates.contents[le_selected_candidate_index];
subst = (matchwcsprefix(cand->origvalue, ctxt->src) == NULL);
}
update_main_buffer(subst, true);
le_complete_cleanup();
return true;
}
/* Clears the current candidates. */
void le_complete_cleanup(void)
{
le_display_complete_cleanup();
if (le_candidates.contents != NULL) {
plfree(pl_toary(&le_candidates), free_candidate);
le_candidates.contents = NULL;
}
free_context(ctxt);
ctxt = NULL;
}
/* Frees a completion candidate.
* The argument must point to a `le_candidate_T' value. */
void free_candidate(void *c)
{
le_candidate_T *cand = c;
free(cand->origvalue);
free(cand->rawvalue.raw);
free(cand->desc);
free(cand->rawdesc.raw);
free(cand);
}
/* Frees the specified `le_context_T' data. */
void free_context(le_context_T *ctxt)
{
if (ctxt != NULL) {
plfree(ctxt->pwords, free);
free(ctxt->src);
free(ctxt->pattern);
free(ctxt);
}
}
/* Sorts the candidates in the candidate list and removes duplicates. */
void sort_candidates(void)
{
qsort(le_candidates.contents,
le_candidates.length, sizeof *le_candidates.contents,
sort_candidates_cmp);
if (le_candidates.length >= 2) {
for (size_t i = le_candidates.length - 1; i > 0; i--) {
le_candidate_T *cand1 = le_candidates.contents[i];
le_candidate_T *cand2 = le_candidates.contents[i - 1];
// XXX case-sensitive
if (wcscoll(cand1->origvalue, cand2->origvalue) == 0) {
free_candidate(cand1);
pl_remove(&le_candidates, i, 1);
}
}
}
}
int sort_candidates_cmp(const void *cp1, const void *cp2)
{
const le_candidate_T *cand1 = *(const le_candidate_T **) cp1;
const le_candidate_T *cand2 = *(const le_candidate_T **) cp2;
const wchar_t *v1 = cand1->origvalue;
const wchar_t *v2 = cand2->origvalue;
/* Candidates that start with hyphens are sorted in a special order so that
* short options come before long options. Such candidates are sorted case-
* insensitively. */
if (v1[0] == L'-' || v2[0] == L'-') {
while (*v1 == L'-' && *v2 == L'-')
v1++, v2++;
if (*v1 == L'-')
return 1;
if (*v2 == L'-')
return -1;
#if HAVE_WCSCASECMP
int cmp = wcscasecmp(v1, v2);
if (cmp != 0)
return cmp;
#endif
}
return wcscoll(v1, v2);
// XXX case-sensitive
}
/* Prints the formatted string to the standard error if the completion debugging
* option is on.
* The string is preceded by "[compdebug] " and followed by a newline. */
void le_compdebug(const char *format, ...)
{
if (!le_state_is_compdebug)
return;
fputs("[compdebug] ", stderr);
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fputc('\n', stderr);
}
/* Prints information on the specified context if the `compdebug' option is
* enabled. */
void print_context_info(const le_context_T *ctxt)
{
const char *s DUMMY_INIT(NULL);
switch (ctxt->quote) {
case QUOTE_NONE: s = "none"; break;
case QUOTE_NORMAL: s = "normal"; break;
case QUOTE_NORMAL_ESCAPED: s = "normal escaped"; break;
case QUOTE_SINGLE: s = "single"; break;
case QUOTE_DOUBLE: s = "double"; break;
case QUOTE_DOUBLE_ESCAPED: s = "double escaped"; break;
}
le_compdebug("quote type: %s", s);
switch (ctxt->type & CTXT_MASK) {
case CTXT_NORMAL: s = "normal"; break;
case CTXT_COMMAND: s = "command"; break;
case CTXT_ARGUMENT: s = "argument"; break;
case CTXT_TILDE: s = "tilde"; break;
case CTXT_VAR: s = "variable"; break;
case CTXT_ARITH: s = "arithmetic"; break;
case CTXT_ASSIGN: s = "assignment"; break;
case CTXT_REDIR: s = "redirection"; break;
case CTXT_REDIR_FD: s = "redirection (fd)"; break;
case CTXT_FOR_IN: s = "\"in\" or \"do\""; break;
case CTXT_FOR_DO: s = "\"do\""; break;
case CTXT_CASE_IN: s = "\"in\""; break;
case CTXT_FUNCTION: s = "function name"; break;
}
le_compdebug("context type: %s%s%s%s", s,
ctxt->type & CTXT_EBRACED ? " (in brace expn)" : "",
ctxt->type & CTXT_VBRACED ? " (in variable)" : "",
ctxt->type & CTXT_QUOTED ? " (quoted)" : "");
for (int i = 0; i < ctxt->pwordc; i++)
le_compdebug("preceding word %d: \"%ls\"",
i + 1, (const wchar_t *) ctxt->pwords[i]);
le_compdebug("target word: \"%ls\"", ctxt->src);
le_compdebug(" as pattern: \"%ls\"", ctxt->pattern);
}
/* Prints information on the specified `compopt' if the `compdebug' option is
* enabled. */
void print_compopt_info(const le_compopt_T *compopt)
{
if (!le_state_is_compdebug)
return;
le_compdebug("target word without prefix: \"%ls\"", compopt->src);
for (const le_comppattern_T *p = compopt->patterns; p != NULL; p = p->next){
const char *s DUMMY_INIT(NULL);
switch (p->type) {
case CPT_ACCEPT: s = "accept"; break;
case CPT_REJECT: s = "reject"; break;
}
le_compdebug("pattern: \"%ls\" (%s)", p->pattern, s);
}
if (compopt->suffix != NULL)
le_compdebug("suffix: \"%ls\"", compopt->suffix);
if (!compopt->terminate)
le_compdebug("completed word will not be terminated");
}
/********** Completion Function Execution **********/
/* name of the file that is autoloaded in the first completion */
#define INIT_COMPFILE "completion/INIT"
/* completion function names */
#define COMMAND_COMPFUNC "completion//command"
#define ARGUMENT_COMPFUNC "completion//argument"
/* Loads and executes completion function to generate candidates. */
void execute_completion_function(void)
{
static bool once = false;
if (!once) {
once = true;
autoload_completion_function_file(L"" INIT_COMPFILE, NULL);
}
switch (ctxt->type & CTXT_MASK) {
case CTXT_NORMAL:
case CTXT_ASSIGN:
case CTXT_REDIR:
simple_completion(CGT_FILE);
break;
case CTXT_COMMAND:
if (!call_completion_function(L"" COMMAND_COMPFUNC))
complete_command_default();
break;
case CTXT_ARGUMENT:
if (!call_completion_function(L"" ARGUMENT_COMPFUNC))
simple_completion(CGT_FILE);
break;
case CTXT_TILDE:
simple_completion(CGT_LOGNAME | CGT_DIRSTACK);
break;
case CTXT_VAR:
simple_completion(CGT_VARIABLE);
break;
case CTXT_ARITH:
simple_completion(CGT_SCALAR);
break;
case CTXT_REDIR_FD:
break;
case CTXT_FOR_IN:
word_completion(2, L"in", L"do");
break;
case CTXT_FOR_DO:
word_completion(1, L"do");
break;
case CTXT_CASE_IN:
word_completion(1, L"in");
break;
case CTXT_FUNCTION:
simple_completion(CGT_FUNCTION);
break;
}
}
/* Sets special local variables $WORDS and $TARGETWORD in the current variable
* environment. Also sets the $IFS variable to the default value. */
void set_completion_variables(void)
{
set_array(L VAR_WORDS, ctxt->pwordc, pldup(ctxt->pwords, copyaswcs),
SCOPE_LOCAL, false);
set_variable(L VAR_TARGETWORD, xwcsdup(ctxt->src), SCOPE_LOCAL, false);
set_variable(L VAR_IFS, xwcsdup(DEFAULT_IFS), SCOPE_LOCAL, false);
}
/* Performs command name completion in the default settings. */
void complete_command_default(void)
{
le_comppattern_T pattern1, pattern2;
le_compopt_T compopt;
pattern1.type = CPT_ACCEPT;
pattern1.pattern = ctxt->pattern;
pattern1.cpattern = NULL;
pattern2.next = NULL;
pattern2.type = CPT_REJECT;
pattern2.pattern = L"*/*";
pattern2.cpattern = NULL;
compopt.ctxt = ctxt;
compopt.src = ctxt->src;
compopt.patterns = &pattern1;
pattern1.next = NULL;
compopt.type = CGT_DIRECTORY;
compopt.suffix = L"/";
compopt.terminate = false;
print_compopt_info(&compopt);
generate_file_candidates(&compopt);
compopt.suffix = NULL;
compopt.terminate = true;
if (wcschr(ctxt->src, L'/') != NULL) {
// pattern1.next = NULL;
compopt.type = CGT_EXECUTABLE;
} else {
pattern1.next = &pattern2;
compopt.type = CGT_COMMAND;
if (ctxt->quote == QUOTE_NORMAL && wcschr(ctxt->pattern, L'\\') == NULL)
compopt.type |= CGT_KEYWORD | CGT_NALIAS;
}
print_compopt_info(&compopt);
generate_candidates(&compopt);
}
/********** Completion Candidate Generation **********/
/* Perform completion for the specified candidate type(s). */
void simple_completion(le_candgentype_T type)
{
le_comppattern_T pattern = {
.type = CPT_ACCEPT,
.pattern = ctxt->pattern,
};
le_compopt_T compopt = {
.ctxt = ctxt,
.type = type,
.src = ctxt->src,
.patterns = &pattern,
.suffix = NULL,
.terminate = true,
};
print_compopt_info(&compopt);
generate_candidates(&compopt);
}
/* Calls all candidate generation functions.
* `cpattern's in `compopt->patterns' are freed in this function but are NOT
* set to NULL. */
void generate_candidates(const le_compopt_T *compopt)
{
generate_file_candidates(compopt);
generate_builtin_candidates(compopt);
generate_external_command_candidates(compopt);
generate_function_candidates(compopt);
generate_keyword_candidates(compopt);
generate_alias_candidates(compopt);
generate_variable_candidates(compopt);
generate_job_candidates(compopt);
generate_signal_candidates(compopt);
generate_logname_candidates(compopt);
generate_group_candidates(compopt);
generate_host_candidates(compopt);
generate_bindkey_candidates(compopt);
generate_dirstack_candidates(compopt);
for (const le_comppattern_T *p = compopt->patterns; p != NULL; p = p->next)
xfnm_free(p->cpattern);
}
/* Adds the specified value as a completion candidate to the candidate list.
* The ignored prefix in `ctxt->origsrc' is prepended to the candidate value.
* A description for the candidate can be given as `desc', which may be NULL
* when no description is provided.
* Arguments `value' and `desc' must be freeable strings, which are used as the
* candidate value and description, respectively.
* This function must NOT be used for a CT_FILE candidate.
* If `value' is NULL, this function does nothing (except freeing `desc'). */
void le_new_candidate(le_candtype_T type, wchar_t *value, wchar_t *desc,
const le_compopt_T *compopt)
{
if (value == NULL) {
free(desc);
return;
}
if (desc != NULL && (desc[0] == L'\0' || wcscmp(value, desc) == 0)) {
/* ignore useless description */
free(desc);
desc = NULL;
}
le_candidate_T *cand = xmalloc(sizeof *cand);
cand->type = type;
cand->value = value;
cand->rawvalue.raw = NULL;
cand->rawvalue.width = 0;
cand->desc = desc;
cand->rawdesc.raw = NULL;
cand->rawdesc.width = 0;
le_add_candidate(cand, compopt);
}
/* Adds the specified candidate to the candidate list.
* The le_candidate_T structure must have been properly initialized, except for
* the `origvalue' and `terminate' members, which are initialized in this
* function.
* This function treats the prefix and suffix specified in the "complete"
* built-in invocation. */
void le_add_candidate(le_candidate_T *cand, const le_compopt_T *compopt)
{
xwcsbuf_T buf;
wb_initwith(&buf, cand->value);
/* prepend prefix */
const wchar_t *origsrc = compopt->ctxt->src;
size_t prefixlength = compopt->src - origsrc;
if (prefixlength != 0)
wb_ninsert_force(&buf, 0, origsrc, prefixlength);
/* append suffix */
bool allowterminate = true;
if ((cand->type == CT_FILE) && S_ISDIR(cand->appendage.filestat.mode) &&
!(compopt->type & CGT_DIRECTORY)) {
wb_wccat(&buf, L'/');
allowterminate = false;
} else if (compopt->suffix != NULL) {
wb_cat(&buf, compopt->suffix);
}
cand->origvalue = wb_towcs(&buf);
cand->value = &cand->origvalue[prefixlength];
cand->terminate = compopt->terminate && allowterminate;
if (le_state_is_compdebug) {
const char *typestr = NULL;
switch (cand->type) {
case CT_WORD: typestr = "word"; break;
case CT_FILE: typestr = "file"; break;
case CT_COMMAND: typestr = "command"; break;
case CT_ALIAS: typestr = "alias"; break;
case CT_OPTION: typestr = "option"; break;
case CT_VAR: typestr = "variable"; break;
case CT_JOB: typestr = "job"; break;
case CT_SIG: typestr = "signal"; break;
case CT_LOGNAME: typestr = "user name"; break;
case CT_GRP: typestr = "group name"; break;
case CT_HOSTNAME: typestr = "host name"; break;
case CT_BINDKEY: typestr = "lineedit command"; break;
}
le_compdebug("new %s candidate \"%ls\"", typestr, cand->origvalue);
if (cand->desc != NULL)
le_compdebug(" (desc: %ls)", cand->desc);
if (!cand->terminate)
le_compdebug(" (no termination)");
}
pl_add(&le_candidates, cand);
}
/* Compiles `pattern's in `compopt->patterns' into `cpattern's if not yet
* compiled. Returns true iff successful. */
bool le_compile_cpatterns(const le_compopt_T *compopt)
{
for (le_comppattern_T *p = compopt->patterns; p != NULL; p = p->next) {
if (p->cpattern != NULL)
continue;
p->cpattern = xfnm_compile(p->pattern, XFNM_HEADONLY | XFNM_TAILONLY);
if (p->cpattern == NULL) {
le_compdebug("failed to compile pattern \"%ls\"", p->pattern);
return false;
}
}
return true;
}
/* Perform pattern matching for multibyte string `s' using patterns `ps'.
* The patterns must have been compiled. Returns true iff successful. */
bool le_match_patterns(const le_comppattern_T *ps, const char *s)
{
for (; ps != NULL; ps = ps->next){
bool match = (xfnm_match(ps->cpattern, s) == 0);
switch (ps->type) {
case CPT_ACCEPT:
if (!match)
return false;
break;
case CPT_REJECT:
if (match)
return false;
break;
}
}
return true;
}
/* Perform pattern matching for multibyte string `s' using patterns in
* `compopt->patterns'. The patterns must have been compiled.
* Returns true iff successful. */
bool le_match_comppatterns(const le_compopt_T *compopt, const char *s)
{
return le_match_patterns(compopt->patterns, s);
}
/* Perform pattern matching for wide string `s' using patterns `ps'.
* The patterns must have been compiled. Returns true iff successful. */
bool le_wmatch_patterns(const le_comppattern_T *ps, const wchar_t *s)
{
for (; ps != NULL; ps = ps->next) {
bool match = (xfnm_wmatch(ps->cpattern, s).start != (size_t) -1);
switch (ps->type) {
case CPT_ACCEPT:
if (!match)
return false;
break;
case CPT_REJECT:
if (match)
return false;
break;
}
}
return true;
}
/* Perform pattern matching for wide string `s' using patterns in
* `compopt->patterns'. The patterns must have been compiled.
* Returns true iff successful. */
bool le_wmatch_comppatterns(const le_compopt_T *compopt, const wchar_t *s)
{
return le_wmatch_patterns(compopt->patterns, s);
}
/* Generates file name candidates.
* The CGT_FILE, CGT_DIRECTORY, and CGT_EXECUTABLE flags specify what candidate
* to generate. The other flags are ignored. */
void generate_file_candidates(const le_compopt_T *compopt)
{
if (!(compopt->type & (CGT_FILE | CGT_DIRECTORY | CGT_EXECUTABLE)))
return;
le_compdebug("adding filename candidates");
if (!le_compile_cpatterns(compopt))
return;
enum wglobflags_T flags = 0;
// if (shopt_nocaseglob) flags |= WGLB_CASEFOLD; XXX case-sensitive
if (shopt_dotglob) flags |= WGLB_PERIOD;
if (shopt_extendedglob) flags |= WGLB_RECDIR;
const le_comppattern_T *p = compopt->patterns;
assert(p->type == CPT_ACCEPT);
/* generate candidates by wglob */
plist_T list;
wglob(p->pattern, flags, pl_init(&list));
p = p->next;
/* check pathnames in `list' and add them to the candidate list */
for (size_t i = 0; i < list.length; i++) {
wchar_t *name = list.contents[i];
if (p != NULL) {
const wchar_t *basename = wcsrchr(name, L'/');
if (basename == NULL)
basename = name;
if (!le_wmatch_patterns(p, basename)) {
free(name);
continue;
}
}
char *mbsname = malloc_wcstombs(name);
struct stat st;
if (mbsname != NULL &&
(stat(mbsname, &st) >= 0 || lstat(mbsname, &st) >= 0)) {
bool executable = S_ISREG(st.st_mode) && is_executable(mbsname);
if ((compopt->type & CGT_FILE)
|| ((compopt->type & CGT_DIRECTORY) && S_ISDIR(st.st_mode))
|| ((compopt->type & CGT_EXECUTABLE) && executable)) {
le_candidate_T *cand = xmalloc(sizeof *cand);
cand->type = CT_FILE;
cand->value = name;
cand->rawvalue.raw = NULL;
cand->rawvalue.width = 0;
cand->desc = NULL;
cand->rawdesc.raw = NULL;
cand->rawdesc.width = 0;
cand->appendage.filestat.is_executable = executable;
cand->appendage.filestat.mode = st.st_mode;
cand->appendage.filestat.nlink = st.st_nlink;
cand->appendage.filestat.size = st.st_size;
le_add_candidate(cand, compopt);
name = NULL;
}
}
free(name);
free(mbsname);
}
pl_destroy(&list);
}
/* Generates candidates that are the names of external commands matching the
* pattern.
* If CGT_EXTCOMMAND is not in `type', this function does nothing. */
void generate_external_command_candidates(const le_compopt_T *compopt)
{
if (!(compopt->type & CGT_EXTCOMMAND))
return;
le_compdebug("adding external command name candidates");
if (!le_compile_cpatterns(compopt))
return;
char *const *paths = get_path_array(PA_PATH);
xstrbuf_T path;
if (paths == NULL)
return;
sb_init(&path);
for (const char *dirpath; (dirpath = *paths) != NULL; paths++) {
DIR *dir = opendir(dirpath);
struct dirent *de;
size_t dirpathlen;
if (dir == NULL)
continue;
sb_cat(&path, dirpath);
if (path.length > 0 && path.contents[path.length - 1] != '/')
sb_ccat(&path, '/');
dirpathlen = path.length;
while ((de = readdir(dir)) != NULL) {
if (!le_match_comppatterns(compopt, de->d_name))
continue;
sb_cat(&path, de->d_name);
if (is_executable_regular(path.contents))
le_new_candidate(CT_COMMAND,
malloc_mbstowcs(de->d_name), NULL, compopt);
sb_truncate(&path, dirpathlen);
}
sb_clear(&path);
closedir(dir);
}
sb_destroy(&path);
}
/* Generates candidates that are keywords matching the pattern. */
void generate_keyword_candidates(const le_compopt_T *compopt)
{
if (!(compopt->type & CGT_KEYWORD))
return;
le_compdebug("adding keyword candidates");
if (!le_compile_cpatterns(compopt))
return;
static const wchar_t *keywords[] = {
L"case", L"do", L"done", L"elif", L"else", L"esac", L"fi", L"for",
L"function", L"if", L"then", L"until", L"while", NULL,
// XXX "select" is not currently supported
};
for (const wchar_t **k = keywords; *k != NULL; k++)
if (le_wmatch_comppatterns(compopt, *k))
le_new_candidate(CT_COMMAND, xwcsdup(*k), NULL, compopt);
}
/* Generates candidates to complete a user name matching the pattern. */
void generate_logname_candidates(const le_compopt_T *compopt)
{