-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.cc
3042 lines (2666 loc) · 90.6 KB
/
complete.cc
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
/* complete.c -- filename completion for readline. */
/* Copyright (C) 1987-2017 Free Software Foundation, Inc.
This file is part of the GNU Readline Library (Readline), a library
for reading lines of text with interactive input and history editing.
Readline 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 3 of the License, or
(at your option) any later version.
Readline 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 Readline. If not, see <http://www.gnu.org/licenses/>.
*/
#define READLINE_LIBRARY
#if defined(HAVE_CONFIG_H)
#include <config.hh>
#endif
#include <fcntl.h>
#include <sys/types.h>
#if defined(HAVE_SYS_FILE_H)
#include <sys/file.h>
#endif
#include <signal.h>
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if defined(HAVE_STDLIB_H)
#include <stdlib.h>
#else
#include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */
#include <stdio.h>
#include <errno.h>
#if !defined(errno)
extern int errno;
#endif /* !errno */
#if defined(HAVE_PWD_H)
#include <pwd.h>
#endif
#include "posixdir.hh"
#include "posixstat.hh"
/* System-specific feature definitions and include files. */
#include "rldefs.hh"
#include "rlmbutil.hh"
/* Some standard library routines. */
#include "readline.hh"
#include "rlprivate.hh"
#include "xmalloc.hh"
#if defined(COLOR_SUPPORT)
#include "colors.hh"
#endif
#ifdef __STDC__
typedef int QSFUNC(const void*, const void*);
#else
typedef int QSFUNC();
#endif
#ifdef HAVE_LSTAT
#define LSTAT lstat
#else
#define LSTAT stat
#endif
/* Unix version of a hidden file. Could be different on other systems. */
#define HIDDEN_FILE(fname) ((fname)[0] == '.')
/* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
defined. */
#if defined(HAVE_GETPWENT) && \
(!defined(HAVE_GETPW_DECLS) || defined(_POSIX_SOURCE))
extern struct passwd* getpwent PARAMS((void));
#endif /* HAVE_GETPWENT && (!HAVE_GETPW_DECLS || _POSIX_SOURCE) */
/* If non-zero, then this is the address of a function to call when
completing a word would normally display the list of possible matches.
This function is called instead of actually doing the display.
It takes three arguments: (char **matches, int num_matches, int
max_length) where MATCHES is the array of strings that matched,
NUM_MATCHES is the number of strings in that array, and MAX_LENGTH is
the length of the longest string in that array. */
rl_compdisp_func_t* rl_completion_display_matches_hook=
(rl_compdisp_func_t*)NULL;
#if defined(VISIBLE_STATS) || defined(COLOR_SUPPORT)
#if !defined(X_OK)
#define X_OK 1
#endif
#endif
#if defined(VISIBLE_STATS)
static int stat_char PARAMS((char*));
#endif
#if defined(COLOR_SUPPORT)
static int colored_stat_start PARAMS((const char*));
static void colored_stat_end PARAMS((void));
static int colored_prefix_start PARAMS((void));
static void colored_prefix_end PARAMS((void));
#endif
static int path_isdir PARAMS((const char*));
static char* rl_quote_filename PARAMS((char*, int, char*));
static void _rl_complete_sigcleanup PARAMS((int, void*));
static void set_completion_defaults PARAMS((int));
static int get_y_or_n PARAMS((int));
static int _rl_internal_pager PARAMS((int));
static char* printable_part PARAMS((char*));
static int fnwidth PARAMS((const char*));
static int fnprint PARAMS((const char*, int, const char*));
static int print_filename PARAMS((char*, char*, int));
static char** gen_completion_matches
PARAMS((char*, int, int, rl_compentry_func_t*, int, int));
static char** remove_duplicate_matches PARAMS((char**));
static void insert_match PARAMS((char*, int, int, char*));
static int append_to_match PARAMS((char*, int, int, int));
static void insert_all_matches PARAMS((char**, int, char*));
static int complete_fncmp PARAMS((const char*, int, const char*, int));
static void display_matches PARAMS((char**));
static int compute_lcd_of_matches PARAMS((char**, int, const char*));
static int postprocess_matches PARAMS((char***, int));
static int complete_get_screenwidth PARAMS((void));
static char* make_quoted_replacement PARAMS((char*, int, char*));
/* **************************************************************** */
/* */
/* Completion matching, from readline's point of view. */
/* */
/* **************************************************************** */
/* Variables known only to the readline library. */
/* If non-zero, non-unique completions always show the list of matches. */
int _rl_complete_show_all= 0;
/* If non-zero, non-unique completions show the list of matches, unless it
is not possible to do partial completion and modify the line. */
int _rl_complete_show_unmodified= 0;
/* If non-zero, completed directory names have a slash appended. */
int _rl_complete_mark_directories= 1;
/* If non-zero, the symlinked directory completion behavior introduced in
readline-4.2a is disabled, and symlinks that point to directories have
a slash appended (subject to the value of
_rl_complete_mark_directories). This is user-settable via the
mark-symlinked-directories variable. */
int _rl_complete_mark_symlink_dirs= 0;
/* If non-zero, completions are printed horizontally in alphabetical order,
like `ls -x'. */
int _rl_print_completions_horizontally;
/* Non-zero means that case is not significant in filename completion. */
#if (defined(__MSDOS__) && !defined(__DJGPP__)) || \
(defined(_WIN32) && !defined(__CYGWIN__))
int _rl_completion_case_fold= 1;
#else
int _rl_completion_case_fold= 0;
#endif
/* Non-zero means that `-' and `_' are equivalent when comparing filenames
for completion. */
int _rl_completion_case_map= 0;
/* If zero, don't match hidden files (filenames beginning with a `.' on
Unix) when doing filename completion. */
int _rl_match_hidden_files= 1;
/* Length in characters of a common prefix replaced with an ellipsis
(`...') when displaying completion matches. Matches whose printable
portion has more than this number of displaying characters in common
will have the common display prefix replaced with an ellipsis. */
int _rl_completion_prefix_display_length= 0;
/* The readline-private number of screen columns to use when displaying
matches. If < 0 or > _rl_screenwidth, it is ignored. */
int _rl_completion_columns= -1;
#if defined(COLOR_SUPPORT)
/* Non-zero means to use colors to indicate file type when listing possible
completions. The colors used are taken from $LS_COLORS, if set. */
int _rl_colored_stats= 0;
/* Non-zero means to use a color (currently magenta) to indicate the common
prefix of a set of possible word completions. */
int _rl_colored_completion_prefix= 0;
#endif
/* If non-zero, when completing in the middle of a word, don't insert
characters from the match that match characters following point in
the word. This means, for instance, completing when the cursor is
after the `e' in `Makefile' won't result in `Makefilefile'. */
int _rl_skip_completed_text= 0;
/* If non-zero, menu completion displays the common prefix first in the
cycle of possible completions instead of the last. */
int _rl_menu_complete_prefix_first= 0;
/* Global variables available to applications using readline. */
#if defined(VISIBLE_STATS)
/* Non-zero means add an additional character to each filename displayed
during listing completion iff rl_filename_completion_desired which helps
to indicate the type of file being listed. */
int rl_visible_stats= 0;
#endif /* VISIBLE_STATS */
/* If non-zero, then this is the address of a function to call when
completing on a directory name. The function is called with
the address of a string (the current directory name) as an arg. */
rl_icppfunc_t* rl_directory_completion_hook= (rl_icppfunc_t*)NULL;
rl_icppfunc_t* rl_directory_rewrite_hook= (rl_icppfunc_t*)NULL;
rl_icppfunc_t* rl_filename_stat_hook= (rl_icppfunc_t*)NULL;
/* If non-zero, this is the address of a function to call when reading
directory entries from the filesystem for completion and comparing
them to the partial word to be completed. The function should
either return its first argument (if no conversion takes place) or
newly-allocated memory. This can, for instance, convert filenames
between character sets for comparison against what's typed at the
keyboard. The returned value is what is added to the list of
matches. The second argument is the length of the filename to be
converted. */
rl_dequote_func_t* rl_filename_rewrite_hook= (rl_dequote_func_t*)NULL;
/* Non-zero means readline completion functions perform tilde expansion. */
int rl_complete_with_tilde_expansion= 0;
/* Pointer to the generator function for completion_matches ().
NULL means to use rl_filename_completion_function (), the default
filename completer. */
rl_compentry_func_t* rl_completion_entry_function=
(rl_compentry_func_t*)NULL;
/* Pointer to generator function for rl_menu_complete (). NULL means to
*use rl_completion_entry_function (see above). */
rl_compentry_func_t* rl_menu_completion_entry_function=
(rl_compentry_func_t*)NULL;
/* Pointer to alternative function to create matches.
Function is called with TEXT, START, and END.
START and END are indices in RL_LINE_BUFFER saying what the boundaries
of TEXT are.
If this function exists and returns NULL then call the value of
rl_completion_entry_function to try to match, otherwise use the
array of strings returned. */
rl_completion_func_t* rl_attempted_completion_function=
(rl_completion_func_t*)NULL;
/* Non-zero means to suppress normal filename completion after the
user-specified completion function has been called. */
int rl_attempted_completion_over= 0;
/* Set to a character indicating the type of completion being performed
by rl_complete_internal, available for use by application completion
functions. */
int rl_completion_type= 0;
/* Up to this many items will be displayed in response to a
possible-completions call. After that, we ask the user if
she is sure she wants to see them all. A negative value means
don't ask. */
int rl_completion_query_items= 100;
int _rl_page_completions= 1;
/* The basic list of characters that signal a break between words for the
completer routine. The contents of this variable is what breaks words
in the shell, i.e. " \t\n\"\\'`@$><=" */
const char* rl_basic_word_break_characters=
" \t\n\"\\'`@$><=;|&{("; /* }) */
/* List of basic quoting characters. */
const char* rl_basic_quote_characters= "\"'";
/* The list of characters that signal a break between words for
rl_complete_internal. The default list is the contents of
rl_basic_word_break_characters. */
/*const*/ char* rl_completer_word_break_characters= (/*const*/ char*)NULL;
/* Hook function to allow an application to set the completion word
break characters before readline breaks up the line. Allows
position-dependent word break characters. */
rl_cpvfunc_t* rl_completion_word_break_hook= (rl_cpvfunc_t*)NULL;
/* List of characters which can be used to quote a substring of the line.
Completion occurs on the entire substring, and within the substring
rl_completer_word_break_characters are treated as any other character,
unless they also appear within this list. */
const char* rl_completer_quote_characters= (const char*)NULL;
/* List of characters that should be quoted in filenames by the completer.
*/
const char* rl_filename_quote_characters= (const char*)NULL;
/* List of characters that are word break characters, but should be left
in TEXT when it is passed to the completion function. The shell uses
this to help determine what kind of completing to do. */
const char* rl_special_prefixes= (const char*)NULL;
/* If non-zero, then disallow duplicates in the matches. */
int rl_ignore_completion_duplicates= 1;
/* Non-zero means that the results of the matches are to be treated
as filenames. This is ALWAYS zero on entry, and can only be changed
within a completion entry finder function. */
int rl_filename_completion_desired= 0;
/* Non-zero means that the results of the matches are to be quoted using
double quotes (or an application-specific quoting mechanism) if the
filename contains any characters in rl_filename_quote_chars. This is
ALWAYS non-zero on entry, and can only be changed within a completion
entry finder function. */
int rl_filename_quoting_desired= 1;
/* This function, if defined, is called by the completer when real
filename completion is done, after all the matching names have been
generated. It is passed a (char**) known as matches in the code below.
It consists of a NULL-terminated array of pointers to potential
matching strings. The 1st element (matches[0]) is the maximal
substring that is common to all matches. This function can re-arrange
the list of matches as required, but all elements of the array must be
free()'d if they are deleted. The main intent of this function is
to implement FIGNORE a la SunOS csh. */
rl_compignore_func_t* rl_ignore_some_completions_function=
(rl_compignore_func_t*)NULL;
/* Set to a function to quote a filename in an application-specific
fashion. Called with the text to quote, the type of match found (single
or multiple) and a pointer to the quoting character to be used, which
the function can reset if desired. */
rl_quote_func_t* rl_filename_quoting_function= rl_quote_filename;
/* Function to call to remove quoting characters from a filename. Called
before completion is attempted, so the embedded quotes do not interfere
with matching names in the file system. Readline doesn't do anything
with this; it's set only by applications. */
rl_dequote_func_t* rl_filename_dequoting_function=
(rl_dequote_func_t*)NULL;
/* Function to call to decide whether or not a word break character is
quoted. If a character is quoted, it does not break words for the
completer. */
rl_linebuf_func_t* rl_char_is_quoted_p= (rl_linebuf_func_t*)NULL;
/* If non-zero, the completion functions don't append anything except a
possible closing quote. This is set to 0 by rl_complete_internal and
may be changed by an application-specific completion function. */
int rl_completion_suppress_append= 0;
/* Character appended to completed words when at the end of the line. The
default is a space. */
int rl_completion_append_character= ' ';
/* If non-zero, the completion functions don't append any closing quote.
This is set to 0 by rl_complete_internal and may be changed by an
application-specific completion function. */
int rl_completion_suppress_quote= 0;
/* Set to any quote character readline thinks it finds before any
application completion function is called. */
int rl_completion_quote_character;
/* Set to a non-zero value if readline found quoting anywhere in the word
to be completed; set before any application completion function is
called. */
int rl_completion_found_quote;
/* If non-zero, a slash will be appended to completed filenames that are
symbolic links to directory names, subject to the value of the
mark-directories variable (which is user-settable). This exists so
that application completion functions can override the user's preference
(set via the mark-symlinked-directories variable) if appropriate.
It's set to the value of _rl_complete_mark_symlink_dirs in
rl_complete_internal before any application-specific completion
function is called, so without that function doing anything, the user's
preferences are honored. */
int rl_completion_mark_symlink_dirs;
/* If non-zero, inhibit completion (temporarily). */
int rl_inhibit_completion;
/* Set to the last key used to invoke one of the completion functions */
int rl_completion_invoking_key;
/* If non-zero, sort the completion matches. On by default. */
int rl_sort_completion_matches= 1;
/* Variables local to this file. */
/* Local variable states what happened during the last completion attempt.
*/
static int completion_changed_buffer;
/* The result of the query to the user about displaying completion matches
*/
static int completion_y_or_n;
static int _rl_complete_display_matches_interrupt= 0;
/*************************************/
/* */
/* Bindable completion functions */
/* */
/*************************************/
/* Complete the word at or before point. You have supplied the function
that does the initial simple matching selection algorithm (see
rl_completion_matches ()). The default is to do filename completion. */
int rl_complete(int ignore, int invoking_key)
{
rl_completion_invoking_key= invoking_key;
if (rl_inhibit_completion)
return (_rl_insert_char(ignore, invoking_key));
else if (rl_last_func == rl_complete && !completion_changed_buffer)
return (rl_complete_internal('?'));
else if (_rl_complete_show_all)
return (rl_complete_internal('!'));
else if (_rl_complete_show_unmodified)
return (rl_complete_internal('@'));
else
return (rl_complete_internal(TAB));
}
/* List the possible completions. See description of rl_complete (). */
int rl_possible_completions(int ignore, int invoking_key)
{
rl_completion_invoking_key= invoking_key;
return (rl_complete_internal('?'));
}
int rl_insert_completions(int ignore, int invoking_key)
{
rl_completion_invoking_key= invoking_key;
return (rl_complete_internal('*'));
}
/* Return the correct value to pass to rl_complete_internal performing
the same tests as rl_complete. This allows consecutive calls to an
application's completion function to list possible completions and for
an application-specific completion function to honor the
show-all-if-ambiguous readline variable. */
int rl_completion_mode(rl_command_func_t* cfunc)
{
if (rl_last_func == cfunc && !completion_changed_buffer)
return '?';
else if (_rl_complete_show_all)
return '!';
else if (_rl_complete_show_unmodified)
return '@';
else
return TAB;
}
/************************************/
/* */
/* Completion utility functions */
/* */
/************************************/
/* Reset readline state on a signal or other event. */
void _rl_reset_completion_state(void)
{
rl_completion_found_quote = 0;
rl_completion_quote_character= 0;
}
static void _rl_complete_sigcleanup(int sig, void* ptr)
{
if (sig == SIGINT) /* XXX - for now */
{
_rl_free_match_list((char**)ptr);
_rl_complete_display_matches_interrupt= 1;
}
}
/* Set default values for readline word completion. These are the
variables that application completion functions can change or inspect.
*/
static void set_completion_defaults(int what_to_do)
{
/* Only the completion entry function can change these. */
rl_filename_completion_desired= 0;
rl_filename_quoting_desired = 1;
rl_completion_type = what_to_do;
rl_completion_suppress_append= rl_completion_suppress_quote= 0;
rl_completion_append_character = ' ';
/* The completion entry function may optionally change this. */
rl_completion_mark_symlink_dirs= _rl_complete_mark_symlink_dirs;
/* Reset private state. */
_rl_complete_display_matches_interrupt= 0;
}
/* The user must press "y" or "n". Non-zero return means "y" pressed. */
static int get_y_or_n(int for_pager)
{
int c;
/* For now, disable pager in callback mode, until we later convert to
state driven functions. Have to wait until next major version to add
new state definition, since it will change value of RL_STATE_DONE. */
#if defined(READLINE_CALLBACKS)
if (RL_ISSTATE(RL_STATE_CALLBACK))
return 1;
#endif
for (;;)
{
RL_SETSTATE(RL_STATE_MOREINPUT);
c= rl_read_key();
RL_UNSETSTATE(RL_STATE_MOREINPUT);
if (c == 'y' || c == 'Y' || c == ' ')
return (1);
if (c == 'n' || c == 'N' || c == RUBOUT)
return (0);
if (c == ABORT_CHAR || c < 0)
_rl_abort_internal();
if (for_pager && (c == NEWLINE || c == RETURN))
return (2);
if (for_pager && (c == 'q' || c == 'Q'))
return (0);
rl_ding();
}
}
static int _rl_internal_pager(int lines)
{
int i;
fprintf(rl_outstream, "--More--");
fflush(rl_outstream);
i= get_y_or_n(1);
_rl_erase_entire_line();
if (i == 0)
return -1;
else if (i == 2)
return (lines - 1);
else
return 0;
}
static int path_isdir(const char* filename)
{
struct stat finfo;
return (stat(filename, &finfo) == 0 && S_ISDIR(finfo.st_mode));
}
#if defined(VISIBLE_STATS)
/* Return the character which best describes FILENAME.
`@' for symbolic links
`/' for directories
`*' for executables
`=' for sockets
`|' for FIFOs
`%' for character special devices
`#' for block special devices */
static int stat_char(char* filename)
{
struct stat finfo;
int character, r;
char* f;
const char* fn;
/* Short-circuit a //server on cygwin, since that will always behave as
a directory. */
#if __CYGWIN__
if (filename[0] == '/' && filename[1] == '/' &&
strchr(filename + 2, '/') == 0)
return '/';
#endif
f= 0;
if (rl_filename_stat_hook)
{
f= savestring(filename);
(*rl_filename_stat_hook)(&f);
fn= f;
}
else
fn= filename;
#if defined(HAVE_LSTAT) && defined(S_ISLNK)
r= lstat(fn, &finfo);
#else
r= stat(fn, &finfo);
#endif
if (r == -1)
{
xfree(f);
return (0);
}
character= 0;
if (S_ISDIR(finfo.st_mode))
character= '/';
#if defined(S_ISCHR)
else if (S_ISCHR(finfo.st_mode))
character= '%';
#endif /* S_ISCHR */
#if defined(S_ISBLK)
else if (S_ISBLK(finfo.st_mode))
character= '#';
#endif /* S_ISBLK */
#if defined(S_ISLNK)
else if (S_ISLNK(finfo.st_mode))
character= '@';
#endif /* S_ISLNK */
#if defined(S_ISSOCK)
else if (S_ISSOCK(finfo.st_mode))
character= '=';
#endif /* S_ISSOCK */
#if defined(S_ISFIFO)
else if (S_ISFIFO(finfo.st_mode))
character= '|';
#endif
else if (S_ISREG(finfo.st_mode))
{
#if defined(_WIN32) && !defined(__CYGWIN__)
char* ext;
/* Windows doesn't do access and X_OK; check file extension instead */
ext= strrchr(fn, '.');
if (ext &&
(_rl_stricmp(ext, ".exe") == 0 || _rl_stricmp(ext, ".cmd") == 0 ||
_rl_stricmp(ext, ".bat") == 0 || _rl_stricmp(ext, ".com") == 0))
character= '*';
#else
if (access(filename, X_OK) == 0)
character= '*';
#endif
}
xfree(f);
return (character);
}
#endif /* VISIBLE_STATS */
#if defined(COLOR_SUPPORT)
static int colored_stat_start(const char* filename)
{
_rl_set_normal_color();
return (_rl_print_color_indicator(filename));
}
static void colored_stat_end(void)
{
_rl_prep_non_filename_text();
_rl_put_indicator(&_rl_color_indicator[C_CLR_TO_EOL]);
}
static int colored_prefix_start(void)
{
_rl_set_normal_color();
return (_rl_print_prefix_color());
}
static void colored_prefix_end(void)
{
colored_stat_end(); /* for now */
}
#endif
/* Return the portion of PATHNAME that should be output when listing
possible completions. If we are hacking filename completion, we
are only interested in the basename, the portion following the
final slash. Otherwise, we return what we were passed. Since
printing empty strings is not very informative, if we're doing
filename completion, and the basename is the empty string, we look
for the previous slash and return the portion following that. If
there's no previous slash, we just return what we were passed. */
static char* printable_part(char* pathname)
{
char *temp, *x;
if (rl_filename_completion_desired == 0) /* don't need to do anything */
return (pathname);
temp= strrchr(pathname, '/');
#if defined(__MSDOS__) || defined(_WIN32)
if (temp == 0 && ISALPHA((unsigned char)pathname[0]) &&
pathname[1] == ':')
temp= pathname + 1;
#endif
if (temp == 0 || *temp == '\0')
return (pathname);
else if (temp[1] == 0 && temp == pathname)
return (pathname);
/* If the basename is NULL, we might have a pathname like '/usr/src/'.
Look for a previous slash and, if one is found, return the portion
following that slash. If there's no previous slash, just return the
pathname we were passed. */
else if (temp[1] == '\0')
{
for (x= temp - 1; x > pathname; x--)
if (*x == '/')
break;
return ((*x == '/') ? x + 1 : pathname);
}
else
return ++temp;
}
/* Compute width of STRING when displayed on screen by print_filename */
static int fnwidth(const char* string)
{
int width, pos;
#if defined(HANDLE_MULTIBYTE)
mbstate_t ps;
int left, w;
size_t clen;
wchar_t wc;
left= strlen(string) + 1;
memset(&ps, 0, sizeof(mbstate_t));
#endif
width= pos= 0;
while (string[pos])
{
if (CTRL_CHAR(string[pos]) || string[pos] == RUBOUT)
{
width+= 2;
pos++;
}
else
{
#if defined(HANDLE_MULTIBYTE)
clen= mbrtowc(&wc, string + pos, left - pos, &ps);
if (MB_INVALIDCH(clen))
{
width++;
pos++;
memset(&ps, 0, sizeof(mbstate_t));
}
else if (MB_NULLWCH(clen))
break;
else
{
pos+= clen;
w= WCWIDTH(wc);
width+= (w >= 0) ? w : 1;
}
#else
width++;
pos++;
#endif
}
}
return width;
}
#define ELLIPSIS_LEN 3
static int fnprint(const char* to_print,
int prefix_bytes,
const char* real_pathname)
{
int printed_len, w;
const char* s;
int common_prefix_len, print_len;
#if defined(HANDLE_MULTIBYTE)
mbstate_t ps;
const char* end;
size_t tlen;
int width;
wchar_t wc;
print_len= strlen(to_print);
end = to_print + print_len + 1;
memset(&ps, 0, sizeof(mbstate_t));
#else
print_len= strlen(to_print);
#endif
printed_len= common_prefix_len= 0;
/* Don't print only the ellipsis if the common prefix is one of the
possible completions. Only cut off prefix_bytes if we're going to be
printing the ellipsis, which takes precedence over coloring the
completion prefix (see print_filename() below). */
if (_rl_completion_prefix_display_length > 0 &&
prefix_bytes >= print_len)
prefix_bytes= 0;
#if defined(COLOR_SUPPORT)
if (_rl_colored_stats &&
(prefix_bytes == 0 || _rl_colored_completion_prefix <= 0))
colored_stat_start(real_pathname);
#endif
if (prefix_bytes && _rl_completion_prefix_display_length > 0)
{
char ellipsis;
ellipsis= (to_print[prefix_bytes] == '.') ? '_' : '.';
for (w= 0; w < ELLIPSIS_LEN; w++)
putc(ellipsis, rl_outstream);
printed_len= ELLIPSIS_LEN;
}
#if defined(COLOR_SUPPORT)
else if (prefix_bytes && _rl_colored_completion_prefix > 0)
{
common_prefix_len= prefix_bytes;
prefix_bytes = 0;
/* XXX - print color indicator start here */
colored_prefix_start();
}
#endif
s= to_print + prefix_bytes;
while (*s)
{
if (CTRL_CHAR(*s))
{
putc('^', rl_outstream);
putc(UNCTRL(*s), rl_outstream);
printed_len+= 2;
s++;
#if defined(HANDLE_MULTIBYTE)
memset(&ps, 0, sizeof(mbstate_t));
#endif
}
else if (*s == RUBOUT)
{
putc('^', rl_outstream);
putc('?', rl_outstream);
printed_len+= 2;
s++;
#if defined(HANDLE_MULTIBYTE)
memset(&ps, 0, sizeof(mbstate_t));
#endif
}
else
{
#if defined(HANDLE_MULTIBYTE)
tlen= mbrtowc(&wc, s, end - s, &ps);
if (MB_INVALIDCH(tlen))
{
tlen = 1;
width= 1;
memset(&ps, 0, sizeof(mbstate_t));
}
else if (MB_NULLWCH(tlen))
break;
else
{
w = WCWIDTH(wc);
width= (w >= 0) ? w : 1;
}
fwrite(s, 1, tlen, rl_outstream);
s+= tlen;
printed_len+= width;
#else
putc(*s, rl_outstream);
s++;
printed_len++;
#endif
}
if (common_prefix_len > 0 && (s - to_print) >= common_prefix_len)
{
#if defined(COLOR_SUPPORT)
/* printed bytes = s - to_print */
/* printed bytes should never be > but check for paranoia's sake */
colored_prefix_end();
if (_rl_colored_stats)
colored_stat_start(real_pathname); /* XXX - experiment */
#endif
common_prefix_len= 0;
}
}
#if defined(COLOR_SUPPORT)
/* XXX - unconditional for now */
if (_rl_colored_stats)
colored_stat_end();
#endif
return printed_len;
}
/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
are using it, check for and output a single character for `special'
filenames. Return the number of characters we output. */
static int print_filename(char* to_print,
char* full_pathname,
int prefix_bytes)
{
int printed_len, extension_char, slen, tlen;
char *s, c, *new_full_pathname, *dn;
extension_char= 0;
#if defined(COLOR_SUPPORT)
/* Defer printing if we want to prefix with a color indicator */
if (_rl_colored_stats == 0 || rl_filename_completion_desired == 0)
#endif
printed_len= fnprint(to_print, prefix_bytes, to_print);
if (rl_filename_completion_desired &&
(
#if defined(VISIBLE_STATS)
rl_visible_stats ||
#endif
#if defined(COLOR_SUPPORT)
_rl_colored_stats ||
#endif
_rl_complete_mark_directories))
{
/* If to_print != full_pathname, to_print is the basename of the
path passed. In this case, we try to expand the directory
name before checking for the stat character. */
if (to_print != full_pathname)
{
/* Terminate the directory name. */
c = to_print[-1];
to_print[-1]= '\0';
/* If setting the last slash in full_pathname to a NUL results in
full_pathname being the empty string, we are trying to complete
files in the root directory. If we pass a null string to the
bash directory completion hook, for example, it will expand it
to the current directory. We just want the `/'. */
if (full_pathname == 0 || *full_pathname == 0)
dn= "/";
else if (full_pathname[0] != '/')
dn= full_pathname;
else if (full_pathname[1] == 0)
dn= "//"; /* restore trailing slash to `//' */
else if (full_pathname[1] == '/' && full_pathname[2] == 0)
dn= "/"; /* don't turn /// into // */
else
dn= full_pathname;
s= tilde_expand(dn);
if (rl_directory_completion_hook)
(*rl_directory_completion_hook)(&s);
slen = strlen(s);
tlen = strlen(to_print);
new_full_pathname= (char*)xmalloc(slen + tlen + 2);
strcpy(new_full_pathname, s);
if (s[slen - 1] == '/')
slen--;
else
new_full_pathname[slen]= '/';
strcpy(new_full_pathname + slen + 1, to_print);
#if defined(VISIBLE_STATS)
if (rl_visible_stats)
extension_char= stat_char(new_full_pathname);
else
#endif
if (_rl_complete_mark_directories)
{
dn= 0;
if (rl_directory_completion_hook == 0 && rl_filename_stat_hook)
{
dn= savestring(new_full_pathname);
(*rl_filename_stat_hook)(&dn);
xfree(new_full_pathname);
new_full_pathname= dn;
}
if (path_isdir(new_full_pathname))
extension_char= '/';