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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
|
/** \file wildcard.c
Fish needs it's own globbing implementation to support
tab-expansion of globbed parameters. Also provides recursive
wildcards using **.
*/
#include "config.h"
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <wchar.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "complete.h"
#include "common.h"
#include "wildcard.h"
#include "complete.h"
#include "reader.h"
#include "expand.h"
#include "exec.h"
#include "halloc_util.h"
/**
This flag is set in the flags parameter of wildcard_expand if the
call is part of a recursiv wildcard search. It is used to make sure
that the contents of subdirectories are only searched once.
*/
#define WILDCARD_RECURSIVE 64
/**
The maximum length of a filename token. This is a fallback value,
an attempt to find the true value using patchconf is always made.
*/
#define MAX_FILE_LENGTH 1024
/**
The command to run to get a description from a file suffix
*/
#define SUFFIX_CMD_STR L"mimedb 2>/dev/null -fd "
/**
Description for generic executable
*/
#define COMPLETE_EXEC_DESC _( L"Executable" )
/**
Description for link to executable
*/
#define COMPLETE_EXEC_LINK_DESC _( L"Executable link" )
/**
Description for regular file
*/
#define COMPLETE_FILE_DESC _( L"File" )
/**
Description for character device
*/
#define COMPLETE_CHAR_DESC _( L"Character device" )
/**
Description for block device
*/
#define COMPLETE_BLOCK_DESC _( L"Block device" )
/**
Description for fifo buffer
*/
#define COMPLETE_FIFO_DESC _( L"Fifo" )
/**
Description for symlink
*/
#define COMPLETE_SYMLINK_DESC _( L"Symbolic link" )
/**
Description for symlink
*/
#define COMPLETE_DIRECTORY_SYMLINK_DESC _( L"Symbolic link to directory" )
/**
Description for Rotten symlink
*/
#define COMPLETE_ROTTEN_SYMLINK_DESC _( L"Rotten symbolic link" )
/**
Description for symlink loop
*/
#define COMPLETE_LOOP_SYMLINK_DESC _( L"Symbolic link loop" )
/**
Description for socket files
*/
#define COMPLETE_SOCKET_DESC _( L"Socket" )
/**
Description for directories
*/
#define COMPLETE_DIRECTORY_DESC _( L"Directory" )
/** Hashtable containing all descriptions that describe an executable */
static hash_table_t *suffix_hash=0;
/**
Push the specified argument to the list if an identical string is
not already in the list. This function iterates over the list,
which is quite slow if the list is large. It might make sense to
use a hashtable for this.
*/
static void al_push_check( array_list_t *l, const wchar_t *newv )
{
int i;
for( i = 0; i < al_get_count(l); i++ )
{
if( !wcscmp( (const wchar_t *)al_get(l, i), newv ) )
{
free( (void *)newv );
return;
}
}
al_push( l, newv );
}
/**
Free hash key and hash value
*/
static void clear_hash_entry( void *key, void *data )
{
free( (void *)key );
free( (void *)data );
}
int wildcard_has( const wchar_t *str, int internal )
{
wchar_t prev=0;
if( !str )
{
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
return 0;
}
if( internal )
{
for( ; *str; str++ )
{
if( ( *str == ANY_CHAR ) || (*str == ANY_STRING) || (*str == ANY_STRING_RECURSIVE) )
return 1;
prev = *str;
}
}
else
{
for( ; *str; str++ )
{
if( ( (*str == L'*' ) || (*str == L'?' ) ) && (prev != L'\\') )
return 1;
prev = *str;
}
}
return 0;
}
/**
Check whether the string str matches the wildcard string wc.
\param str String to be matched.
\param wc The wildcard.
\param is_first Whether files beginning with dots should not be matched against wildcards.
*/
static int wildcard_match2( const wchar_t *str,
const wchar_t *wc,
int is_first )
{
if( *str == 0 && *wc==0 )
return 1;
if( *wc == ANY_STRING || *wc == ANY_STRING_RECURSIVE)
{
/* Ignore hidden file */
if( is_first && *str == L'.' )
{
return 0;
}
/* Try all submatches */
do
{
if( wildcard_match2( str, wc+1, 0 ) )
return 1;
}
while( *(str++) != 0 );
return 0;
}
else if( *str == 0 )
{
/*
End of string, but not end of wildcard, and the next wildcard
element is not a '*', so this is not a match.
*/
return 0;
}
if( *wc == ANY_CHAR )
{
if( is_first && *str == L'.' )
{
return 0;
}
return wildcard_match2( str+1, wc+1, 0 );
}
if( *wc == *str )
return wildcard_match2( str+1, wc+1, 0 );
return 0;
}
/**
Matches the string against the wildcard, and if the wildcard is a
possible completion of the string, the remainder of the string is
inserted into the array_list_t.
*/
static int wildcard_complete_internal( const wchar_t *orig,
const wchar_t *str,
const wchar_t *wc,
int is_first,
const wchar_t *desc,
const wchar_t *(*desc_func)(const wchar_t *),
std::vector<completion_t> &out,
int flags )
{
if( !wc || !str || !orig)
{
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
return 0;
}
if( *wc == 0 &&
( ( *str != L'.') || (!is_first)) )
{
wchar_t *out_completion = 0;
const wchar_t *out_desc = desc;
if( flags & COMPLETE_NO_CASE )
{
out_completion = wcsdup( orig );
}
else
{
out_completion = wcsdup( str );
}
if( wcschr( str, PROG_COMPLETE_SEP ) )
{
/*
This completion has an embedded description, du not use the generic description
*/
wchar_t *sep;
sep = wcschr(out_completion, PROG_COMPLETE_SEP );
*sep = 0;
out_desc = sep + 1;
}
else
{
if( desc_func )
{
/*
A descripton generating function is specified, call
it. If it returns something, use that as the
description.
*/
const wchar_t *func_desc = desc_func( orig );
if( func_desc )
out_desc = func_desc;
}
}
if( out_completion )
{
completion_allocate( out,
out_completion,
out_desc,
flags );
}
free ( out_completion );
return 1;
}
if( *wc == ANY_STRING )
{
int res=0;
/* Ignore hidden file */
if( is_first && str[0] == L'.' )
return 0;
/* Try all submatches */
do
{
res |= wildcard_complete_internal( orig, str, wc+1, 0, desc, desc_func, out, flags );
if( res )
break;
}
while( *str++ != 0 );
return res;
}
else if( *wc == ANY_CHAR )
{
return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags );
}
else if( *wc == *str )
{
return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags );
}
else if( towlower(*wc) == towlower(*str) )
{
return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags | COMPLETE_NO_CASE );
}
return 0;
}
int wildcard_complete( const wchar_t *str,
const wchar_t *wc,
const wchar_t *desc,
const wchar_t *(*desc_func)(const wchar_t *),
std::vector<completion_t> &out,
int flags )
{
int res;
res = wildcard_complete_internal( str, str, wc, 1, desc, desc_func, out, flags );
return res;
}
int wildcard_match( const wchar_t *str, const wchar_t *wc )
{
return wildcard_match2( str, wc, 1 );
}
/**
Creates a path from the specified directory and filename.
*/
static wchar_t *make_path( const wchar_t *base_dir, const wchar_t *name )
{
wchar_t *long_name;
int base_len = wcslen( base_dir );
if( !(long_name= (wchar_t *)malloc( sizeof(wchar_t)*(base_len+wcslen(name)+1) )))
{
DIE_MEM();
}
wcscpy( long_name, base_dir );
wcscpy(&long_name[base_len], name );
return long_name;
}
/**
Return a description of a file based on its suffix. This function
does not perform any caching, it directly calls the mimedb command
to do a lookup.
*/
static wchar_t *complete_get_desc_suffix_internal( const wchar_t *suff_orig )
{
wchar_t *suff = wcsdup( suff_orig );
wchar_t *cmd = wcsdupcat( SUFFIX_CMD_STR, suff );
if( !suff || !cmd )
DIE_MEM();
wcstring_list_t lst;
wcstring desc;
if( exec_subshell2( cmd, lst ) != -1 )
{
if( lst.size()>0 )
{
const wcstring & ln = lst.at(0);
if( ln.size() > 0 && ln != L"unknown" )
{
desc = ln;
/*
I have decided I prefer to have the description
begin in uppercase and the whole universe will just
have to accept it. Hah!
*/
desc[0]=towupper(desc[0]);
}
}
}
free(cmd);
if( ! desc.size() )
{
desc = COMPLETE_FILE_DESC;
}
wchar_t *tmp = wcsdup(desc.c_str());
hash_put( suffix_hash, suff, tmp );
return tmp;
}
/**
Free the suffix_hash hash table and all memory used by it.
*/
static void complete_get_desc_destroy_suffix_hash()
{
if( suffix_hash )
{
hash_foreach( suffix_hash, &clear_hash_entry );
hash_destroy( suffix_hash );
free( suffix_hash );
}
}
/**
Use the mimedb command to look up a description for a given suffix
*/
static const wchar_t *complete_get_desc_suffix( const wchar_t *suff_orig )
{
int len;
wchar_t *suff;
wchar_t *pos;
wchar_t *tmp;
wchar_t *desc;
len = wcslen(suff_orig );
if( len == 0 )
return COMPLETE_FILE_DESC;
if( !suffix_hash )
{
suffix_hash = (hash_table_t *)malloc( sizeof( hash_table_t) );
if( !suffix_hash )
DIE_MEM();
hash_init( suffix_hash, &hash_wcs_func, &hash_wcs_cmp );
halloc_register_function_void( global_context, &complete_get_desc_destroy_suffix_hash );
}
suff = wcsdup(suff_orig);
/*
Drop characters that are commonly used as backup suffixes from the suffix
*/
for( pos=suff; *pos; pos++ )
{
if( wcschr( L"?;#~@&", *pos ) )
{
*pos=0;
break;
}
}
tmp = escape( suff, 1 );
free(suff);
suff = tmp;
desc = (wchar_t *)hash_get( suffix_hash, suff );
if( !desc )
{
desc = complete_get_desc_suffix_internal( suff );
}
free( suff );
return desc;
}
/**
Obtain a description string for the file specified by the filename.
The returned value is a string constant and should not be free'd.
\param filename The file for which to find a description string
\param lstat_res The result of calling lstat on the file
\param lbuf The struct buf output of calling lstat on the file
\param stat_res The result of calling stat on the file
\param buf The struct buf output of calling stat on the file
\param err The errno value after a failed stat call on the file.
*/
static const wchar_t *file_get_desc( const wchar_t *filename,
int lstat_res,
struct stat lbuf,
int stat_res,
struct stat buf,
int err )
{
const wchar_t *suffix;
CHECK( filename, 0 );
if( !lstat_res )
{
if( S_ISLNK(lbuf.st_mode))
{
if( !stat_res )
{
if( S_ISDIR(buf.st_mode) )
{
return COMPLETE_DIRECTORY_SYMLINK_DESC;
}
else
{
if( ( buf.st_mode & S_IXUSR ) ||
( buf.st_mode & S_IXGRP ) ||
( buf.st_mode & S_IXOTH ) )
{
if( waccess( filename, X_OK ) == 0 )
{
/*
Weird group permissions and other such
issues make it non-trivial to find out
if we can actually execute a file using
the result from stat. It is much safer
to use the access function, since it
tells us exactly what we want to know.
*/
return COMPLETE_EXEC_LINK_DESC;
}
}
}
return COMPLETE_SYMLINK_DESC;
}
else
{
switch( err )
{
case ENOENT:
{
return COMPLETE_ROTTEN_SYMLINK_DESC;
}
case ELOOP:
{
return COMPLETE_LOOP_SYMLINK_DESC;
}
}
/*
On unknown errors we do nothing. The file will be
given the default 'File' description or one based on the suffix.
*/
}
}
else if( S_ISCHR(buf.st_mode) )
{
return COMPLETE_CHAR_DESC;
}
else if( S_ISBLK(buf.st_mode) )
{
return COMPLETE_BLOCK_DESC;
}
else if( S_ISFIFO(buf.st_mode) )
{
return COMPLETE_FIFO_DESC;
}
else if( S_ISSOCK(buf.st_mode))
{
return COMPLETE_SOCKET_DESC;
}
else if( S_ISDIR(buf.st_mode) )
{
return COMPLETE_DIRECTORY_DESC;
}
else
{
if( ( buf.st_mode & S_IXUSR ) ||
( buf.st_mode & S_IXGRP ) ||
( buf.st_mode & S_IXOTH ) )
{
if( waccess( filename, X_OK ) == 0 )
{
/*
Weird group permissions and other such issues
make it non-trivial to find out if we can
actually execute a file using the result from
stat. It is much safer to use the access
function, since it tells us exactly what we want
to know.
*/
return COMPLETE_EXEC_DESC;
}
}
}
}
suffix = wcsrchr( filename, L'.' );
if( suffix != 0 && !wcsrchr( suffix, L'/' ) )
{
return complete_get_desc_suffix( suffix );
}
return COMPLETE_FILE_DESC ;
}
/**
Add the specified filename if it matches the specified wildcard.
If the filename matches, first get the description of the specified
filename. If this is a regular file, append the filesize to the
description.
\param list the list to add he completion to
\param fullname the full filename of the file
\param completion the completion part of the file name
\param wc the wildcard to match against
\param is_cmd whether we are performing command completion
*/
static void wildcard_completion_allocate( std::vector<completion_t> &list,
const wchar_t *fullname,
const wchar_t *completion,
const wchar_t *wc,
int is_cmd )
{
const wchar_t *desc;
struct stat buf, lbuf;
static string_buffer_t *sb = 0;
int free_completion = 0;
int flags = 0;
int stat_res, lstat_res;
int stat_errno=0;
long long sz;
if( !sb )
{
sb = sb_halloc( global_context );
}
else
{
sb_clear( sb );
}
CHECK( fullname, );
sb_clear( sb );
/*
If the file is a symlink, we need to stat both the file itself
_and_ the destination file. But we try to avoid this with
non-symlinks by first doing an lstat, and if the file is not a
link we copy the results over to the regular stat buffer.
*/
if( ( lstat_res = lwstat( fullname, &lbuf ) ) )
{
sz=-1;
stat_res = lstat_res;
}
else
{
if( S_ISLNK(lbuf.st_mode))
{
if( ( stat_res = wstat( fullname, &buf ) ) )
{
sz=-1;
}
else
{
sz = (long long)buf.st_size;
}
/*
In order to differentiate between e.g. rotten symlinks
and symlink loops, we also need to know the error status of wstat.
*/
stat_errno = errno;
}
else
{
stat_res = lstat_res;
memcpy( &buf, &lbuf, sizeof( struct stat ) );
sz = (long long)buf.st_size;
}
}
desc = file_get_desc( fullname, lstat_res, lbuf, stat_res, buf, stat_errno );
if( sz >= 0 && S_ISDIR(buf.st_mode) )
{
free_completion = 1;
flags = flags | COMPLETE_NO_SPACE;
completion = wcsdupcat( completion, L"/" );
sb_append( sb, desc );
}
else
{
sb_append( sb, desc, L", ", NULL );
sb_format_size( sb, sz );
}
wildcard_complete( completion, wc, (wchar_t *)sb->buff, 0, list, flags );
if( free_completion )
free( (void *)completion );
}
/**
Test if the file specified by the given filename matches the
expansion flags specified. flags can be a combination of
EXECUTABLES_ONLY and DIRECTORIES_ONLY.
*/
static int test_flags( const wchar_t *filename,
int flags )
{
if( flags & DIRECTORIES_ONLY )
{
struct stat buf;
if( wstat( filename, &buf ) == -1 )
{
return 0;
}
if( !S_ISDIR( buf.st_mode ) )
{
return 0;
}
}
if( flags & EXECUTABLES_ONLY )
{
if ( waccess( filename, X_OK ) != 0)
return 0;
}
return 1;
}
/**
The real implementation of wildcard expansion is in this
function. Other functions are just wrappers around this one.
This function traverses the relevant directory tree looking for
matches, and recurses when needed to handle wildcrards spanning
multiple components and recursive wildcards.
*/
static int wildcard_expand_internal( const wchar_t *wc,
const wchar_t *base_dir,
int flags,
std::vector<completion_t> &out )
{
/* Points to the end of the current wildcard segment */
const wchar_t *wc_end;
/* Variables for traversing a directory */
DIR *dir;
/* The result returned */
int res = 0;
/* Length of the directory to search in */
int base_len;
/* Variables for testing for presense of recursive wildcards */
const wchar_t *wc_recursive;
int is_recursive;
/* Sligtly mangled version of base_dir */
const wchar_t *dir_string;
/* Description for completions */
string_buffer_t sb_desc;
// debug( 3, L"WILDCARD_EXPAND %ls in %ls", wc, base_dir );
if( reader_interrupted() )
{
return -1;
}
if( !wc || !base_dir )
{
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
return 0;
}
if( flags & ACCEPT_INCOMPLETE )
{
/*
Avoid excessive number of returned matches for wc ending with a *
*/
int len = wcslen(wc);
if( len && (wc[len-1]==ANY_STRING) )
{
wchar_t * foo = wcsdup( wc );
foo[len-1]=0;
int res = wildcard_expand_internal( foo, base_dir, flags, out );
free( foo );
return res;
}
}
/*
Initialize various variables
*/
dir_string = base_dir[0]==L'\0'?L".":base_dir;
if( !(dir = wopendir( dir_string )))
{
return 0;
}
wc_end = wcschr(wc,L'/');
base_len = wcslen( base_dir );
/*
Test for recursive match string in current segment
*/
wc_recursive = wcschr( wc, ANY_STRING_RECURSIVE );
is_recursive = ( wc_recursive && (!wc_end || wc_recursive < wc_end));
if( flags & ACCEPT_INCOMPLETE )
sb_init( &sb_desc );
/*
Is this segment of the wildcard the last?
*/
if( !wc_end )
{
/*
Wildcard segment is the last segment,
Insert all matching files/directories
*/
if( wc[0]=='\0' )
{
/*
The last wildcard segment is empty. Insert everything if
completing, the directory itself otherwise.
*/
if( flags & ACCEPT_INCOMPLETE )
{
wcstring next;
while(wreaddir(dir, next))
{
if( next[0] != L'.' )
{
const wchar_t *name = next.c_str();
const wchar_t *long_name = make_path( base_dir, name );
if( test_flags( long_name, flags ) )
{
wildcard_completion_allocate( out,
long_name,
name,
L"",
flags & EXECUTABLES_ONLY );
}
free( (void *)long_name );
}
}
}
else
{
res = 1;
completion_t data_to_push;
data_to_push.completion = base_dir;
if ( std::find( out.begin(), out.end(), data_to_push ) != out.end() ){
out.push_back( data_to_push);
}
}
}
else
{
/*
This is the last wildcard segment, and it is not empty. Match files/directories.
*/
wcstring next;
while(wreaddir(dir, next))
{
const wchar_t *name = next.c_str();
if( flags & ACCEPT_INCOMPLETE )
{
wchar_t *long_name = make_path( base_dir, name );
/*
Test for matches before stating file, so as to minimize the number of calls to the much slower stat function
*/
std::vector<completion_t> test;
if( wildcard_complete( name,
wc,
L"",
0,
test,
0 ) )
{
if( test_flags( long_name, flags ) )
{
wildcard_completion_allocate( out,
long_name,
name,
wc,
flags & EXECUTABLES_ONLY );
}
}
free( long_name );
}
else
{
if( wildcard_match2( name, wc, 1 ) )
{
wchar_t *long_name = make_path( base_dir, name );
int skip = 0;
if( is_recursive )
{
/*
In recursive mode, we are only
interested in adding files -directories
will be added in the next pass.
*/
struct stat buf;
if( !wstat( long_name, &buf ) )
{
skip = S_ISDIR(buf.st_mode);
}
}
if( skip )
{
free( long_name );
}
else
{
completion_t data_to_push;
data_to_push.completion = long_name;
out.push_back( data_to_push );
}
res = 1;
}
}
}
}
}
if( wc_end || is_recursive )
{
/*
Wilcard segment is not the last segment. Recursively call
wildcard_expand for all matching subdirectories.
*/
/*
wc_str is the part of the wildcarded string from the
beginning to the first slash
*/
wchar_t *wc_str;
/*
new_dir is a scratch area containing the full path to a
file/directory we are iterating over
*/
wchar_t *new_dir;
/*
The maximum length of a file element
*/
long ln=MAX_FILE_LENGTH;
char * narrow_dir_string = wcs2str( dir_string );
/*
In recursive mode, we look through the direcotry twice. If
so, this rewind is needed.
*/
rewinddir( dir );
if( narrow_dir_string )
{
/*
Find out how long the filename can be in a worst case
scenario
*/
ln = pathconf( narrow_dir_string, _PC_NAME_MAX );
/*
If not specified, use som large number as fallback
*/
if( ln < 0 )
ln = MAX_FILE_LENGTH;
free( narrow_dir_string );
}
new_dir= (wchar_t *)malloc( sizeof(wchar_t)*(base_len+ln+2) );
wc_str = wc_end?wcsndup(wc, wc_end-wc):wcsdup(wc);
if( (!new_dir) || (!wc_str) )
{
DIE_MEM();
}
wcscpy( new_dir, base_dir );
wcstring next;
while (wreaddir(dir, next))
{
const wchar_t *name = next.c_str();
/*
Test if the file/directory name matches the whole
wildcard element, i.e. regular matching.
*/
int whole_match = wildcard_match2( name, wc_str, 1 );
int partial_match = 0;
/*
If we are doing recursive matching, also check if this
directory matches the part up to the recusrive
wildcard, if so, then we can search all subdirectories
for matches.
*/
if( is_recursive )
{
const wchar_t *end = wcschr( wc, ANY_STRING_RECURSIVE );
wchar_t *wc_sub = wcsndup( wc, end-wc+1);
partial_match = wildcard_match2( name, wc_sub, 1 );
free( wc_sub );
}
if( whole_match || partial_match )
{
int new_len;
struct stat buf;
char *dir_str;
int stat_res;
int new_res;
wcscpy(&new_dir[base_len], name );
dir_str = wcs2str( new_dir );
if( dir_str )
{
stat_res = stat( dir_str, &buf );
free( dir_str );
if( !stat_res )
{
if( S_ISDIR(buf.st_mode) )
{
new_len = wcslen( new_dir );
new_dir[new_len] = L'/';
new_dir[new_len+1] = L'\0';
/*
Regular matching
*/
if( whole_match )
{
const wchar_t *new_wc = L"";
if( wc_end )
{
new_wc=wc_end+1;
/*
Accept multiple '/' as a single direcotry separator
*/
while(*new_wc==L'/')
{
new_wc++;
}
}
new_res = wildcard_expand_internal( new_wc,
new_dir,
flags,
out );
if( new_res == -1 )
{
res = -1;
break;
}
res |= new_res;
}
/*
Recursive matching
*/
if( partial_match )
{
new_res = wildcard_expand_internal( wcschr( wc, ANY_STRING_RECURSIVE ),
new_dir,
flags | WILDCARD_RECURSIVE,
out );
if( new_res == -1 )
{
res = -1;
break;
}
res |= new_res;
}
}
}
}
}
}
free( wc_str );
free( new_dir );
}
closedir( dir );
if( flags & ACCEPT_INCOMPLETE )
{
sb_destroy( &sb_desc );
}
return res;
}
int wildcard_expand( const wchar_t *wc,
const wchar_t *base_dir,
int flags,
std::vector<completion_t> &out )
{
int c = out.size();
int res = wildcard_expand_internal( wc, base_dir, flags, out );
int i;
if( flags & ACCEPT_INCOMPLETE )
{
wchar_t *wc_base=L"";
wchar_t *wc_base_ptr = const_cast<wchar_t*>(wcsrchr( wc, L'/' ));
string_buffer_t sb;
if( wc_base_ptr )
{
wc_base = wcsndup( wc, (wc_base_ptr-wc)+1 );
}
sb_init( &sb );
for( i=c; i<out.size(); i++ )
{
completion_t &c = out.at( i );
if( c.flags & COMPLETE_NO_CASE )
{
sb_clear( &sb );
sb_printf( &sb, L"%ls%ls%ls", base_dir, wc_base, c.completion.c_str() );
c.completion = (wchar_t *)sb.buff;
}
}
sb_destroy( &sb );
if( wc_base_ptr )
{
free( wc_base );
}
}
return res;
}
int wildcard_expand_string(const wcstring &wc, const wcstring &base_dir, int flags, std::vector<completion_t> &outputs )
{
std::vector<completion_t> lst;
// al_init(&lst);
int res = wildcard_expand(wc.c_str(), base_dir.c_str(), flags, lst);
int i, max = lst.size();
for (i=0; i < max; i++) {
outputs.push_back( lst.at(i));
}
// al_destroy(&lst);
return res;
}
|