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
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
|
/** \file complete.c
Functions related to tab-completion.
These functions are used for storing and retrieving tab-completion data, as well as for performing tab-completion.
*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <termios.h>
#include <ctype.h>
#include <pwd.h>
#include <signal.h>
#include "config.h"
#include "util.h"
#include "tokenizer.h"
#include "wildcard.h"
#include "proc.h"
#include "parser.h"
#include "function.h"
#include "complete.h"
#include "builtin.h"
#include "env.h"
#include "exec.h"
#include "expand.h"
#include "common.h"
#include "reader.h"
#include "history.h"
#include "intern.h"
#include "wutil.h"
/*
Completion description strings for files
There are a few other completion description strings defined in expand.c
*/
/**
Description for ~USER completion
*/
#define COMPLETE_USER_DESC COMPLETE_SEP_STR L"User home"
/**
Description for short variables. The value is concatenated to this description
*/
#define COMPLETE_VAR_DESC_VAL COMPLETE_SEP_STR L"Variable: "
/**
Description for generic executable
*/
#define COMPLETE_EXEC_DESC COMPLETE_SEP_STR L"Executable"
/**
Description for link to executable
*/
#define COMPLETE_EXEC_LINK_DESC COMPLETE_SEP_STR L"Executable link"
/**
Description for regular file
*/
#define COMPLETE_FILE_DESC COMPLETE_SEP_STR L"File"
/**
Description for character device
*/
#define COMPLETE_CHAR_DESC COMPLETE_SEP_STR L"Character device"
/**
Description for block device
*/
#define COMPLETE_BLOCK_DESC COMPLETE_SEP_STR L"Block device"
/**
Description for fifo buffer
*/
#define COMPLETE_FIFO_DESC COMPLETE_SEP_STR L"Fifo"
/**
Description for symlink
*/
#define COMPLETE_SYMLINK_DESC COMPLETE_SEP_STR L"Symbolic link"
/**
Description for Rotten symlink
*/
#define COMPLETE_ROTTEN_SYMLINK_DESC COMPLETE_SEP_STR L"Rotten symbolic link"
/**
Description for socket
*/
#define COMPLETE_SOCKET_DESC COMPLETE_SEP_STR L"Socket"
/**
Description for directory
*/
#define COMPLETE_DIRECTORY_DESC COMPLETE_SEP_STR L"Directory"
/**
Description for function
*/
#define COMPLETE_FUNCTION_DESC COMPLETE_SEP_STR L"Function"
/**
Description for builtin command
*/
#define COMPLETE_BUILTIN_DESC COMPLETE_SEP_STR L"Builtin"
/**
The command to run to get a description from a file suffix
*/
#define SUFFIX_CMD_STR L"mimedb 2>/dev/null -fd "
/**
The maximum number of commands on which to perform description
lookup. The lookup process is quite time consuming, so this should
be set to a pretty low number.
*/
#define MAX_CMD_DESC_LOOKUP 10
/**
Condition cache value returned from hashtable when this condition
has not yet been tested
*/
#define CC_NOT_TESTED 0
/**
Condition cache value returned from hashtable when the condition is met
*/
#define CC_TRUE L"true"
/**
Condition cache value returned from hashtable when the condition is not met
*/
#define CC_FALSE L"false"
/**
Struct describing a completion option entry.
If short_opt and long_opt are both zero, the comp field must not be
empty and contains a list of arguments to the command.
If either short_opt or long_opt are non-zero, they specify a switch
for the command. If \c comp is also not empty, it contains a list
of arguments that may only follow after the specified switch.
*/
typedef struct complete_entry_opt
{
/** Short style option */
wchar_t short_opt;
/** Long style option */
const wchar_t *long_opt;
/** Arguments to the option */
const wchar_t *comp;
/** Description of the completion */
const wchar_t *desc;
/** Condition under which to use the option */
const wchar_t *condition;
/** Must be one of the values SHARED, NO_FILES, NO_COMMON, EXCLUSIVE. */
int result_mode;
/** Next option in the linked list */
/** True if old style long options are used */
int old_mode;
struct complete_entry_opt *next;
}
complete_entry_opt;
/**
Struct describing a command completion
*/
typedef struct complete_entry
{
/** True if command is a path */
int cmd_type;
/** Command string */
const wchar_t *cmd;
/** String containing all short option characters */
wchar_t *short_opt_str;
/** Linked list of all options */
complete_entry_opt *first_option;
/** Next command completion in the linked list */
struct complete_entry *next;
/** True if no other options than the ones supplied are possible */
int authorative;
}
complete_entry;
/** First node in the linked list of all completion entries */
static complete_entry *first_entry=0;
/** Hashtable containing all descriptions that describe an executable */
static hash_table_t *suffix_hash=0;
/**
Table of completions conditions that have already been tested and
the corresponding test results
*/
static hash_table_t *condition_cache=0;
/**
Set of commands for which completions have already been loaded
*/
static hash_table_t *loaded_completions=0;
void complete_init()
{
}
/**
This command clears the cache of condition tests created by \c condition_test().
*/
static void condition_cache_clear()
{
if( condition_cache )
{
hash_destroy( condition_cache );
free( condition_cache );
condition_cache = 0;
}
}
/**
Test if the specified script returns zero. The result is cached, so
that if multiple completions use the same condition, it needs only
be evaluated once. condition_cache_clear must be called after a
completion run to make sure that there are no stale completions.
*/
static int condition_test( const wchar_t *condition )
{
const void *test_res;
if( !condition || !wcslen(condition) )
{
// fwprintf( stderr, L"No condition specified\n" );
return 1;
}
if( !condition_cache )
{
condition_cache = malloc( sizeof( hash_table_t ) );
if( !condition_cache )
{
die_mem();
}
hash_init( condition_cache,
&hash_wcs_func,
&hash_wcs_cmp );
}
test_res = hash_get( condition_cache, condition );
if (test_res == CC_NOT_TESTED )
{
test_res = exec_subshell( condition, 0 )?CC_FALSE:CC_TRUE;
// debug( 1, L"Eval returns %ls for '%ls'", test_res, condition );
hash_put( condition_cache, condition, test_res );
/*
Restore previous status information
*/
}
if( test_res == CC_TRUE )
{
// debug( 1, L"Use conditional completion on condition %ls", condition );
return 1;
}
// debug( 1, L"Skip conditional completion on condition %ls", condition );
return 0;
}
/**
Recursively free all complete_entry_opt structs and their contents
*/
static void complete_free_opt_recursive( complete_entry_opt *o )
{
if( o->next != 0 )
complete_free_opt_recursive( o->next );
free(o);
}
/**
Free a complete_entry and its contents
*/
static void complete_free_entry( complete_entry *c )
{
// free( c->cmd );
free( c->short_opt_str );
complete_free_opt_recursive( c->first_option );
free( c );
}
/**
Free hash key and hash value
*/
static void clear_hash_entry( const void *key, const void *data )
{
free( (void *)key );
free( (void *)data );
}
/**
Free hash value, but not hash key
*/
static void clear_hash_value( const void *key, const void *data )
{
free( (void *)data );
}
void complete_destroy()
{
complete_entry *i=first_entry, *prev;
while( i )
{
prev = i;
i=i->next;
complete_free_entry( prev );
}
if( suffix_hash )
{
hash_foreach( suffix_hash, &clear_hash_entry );
hash_destroy( suffix_hash );
free( suffix_hash );
}
if( loaded_completions )
{
hash_foreach( loaded_completions,
&clear_hash_value );
hash_destroy( loaded_completions );
free( loaded_completions );
}
}
/**
Search for an exactly matching completion entry
*/
static complete_entry *complete_find_exact_entry( const wchar_t *cmd,
const int cmd_type )
{
complete_entry *i;
for( i=first_entry; i; i=i->next )
{
if( ( wcscmp(cmd, i->cmd)==0) && ( cmd_type == i->cmd_type ))
{
return i;
}
}
return 0;
}
void complete_add( const wchar_t *cmd,
int cmd_type,
wchar_t short_opt,
const wchar_t *long_opt,
int old_mode,
int result_mode,
int authorative,
const wchar_t *condition,
const wchar_t *comp,
const wchar_t *desc )
{
complete_entry *c =
complete_find_exact_entry( cmd, cmd_type );
complete_entry_opt *opt;
wchar_t *tmp;
if( c == 0 )
{
if( !(c = malloc( sizeof(complete_entry) )))
die_mem();
c->next = first_entry;
first_entry = c;
c->first_option = 0;
c->cmd = intern( cmd );
c->cmd_type = cmd_type;
c->short_opt_str = wcsdup(L"");
}
/* wprintf( L"Add completion to option (short %lc, long %ls)\n", short_opt, long_opt );*/
if( !(opt = malloc( sizeof( complete_entry_opt ) )))
die_mem();
opt->next = c->first_option;
c->first_option = opt;
c->authorative = authorative;
if( short_opt != L'\0' )
{
int len = 1 + ((result_mode & NO_COMMON) != 0);
c->short_opt_str =
realloc( c->short_opt_str,
sizeof(wchar_t)*(wcslen( c->short_opt_str ) + 1 + len) );
wcsncat( c->short_opt_str,
&short_opt, 1 );
if( len == 2 )
wcscat( c->short_opt_str, L":" );
}
opt->short_opt = short_opt;
opt->result_mode = result_mode;
opt->old_mode=old_mode;
opt->comp = intern(comp);
opt->condition = intern(condition);
opt->long_opt = intern( long_opt );
if( desc && wcslen( desc ) )
{
tmp = wcsdupcat( COMPLETE_SEP_STR, desc );
opt->desc = intern( tmp );
free( tmp );
}
else
opt->desc = L"";
}
void complete_remove( const wchar_t *cmd,
int cmd_type,
wchar_t short_opt,
const wchar_t *long_opt )
{
complete_entry *e, *eprev=0, *enext=0;
for( e = first_entry; e; e=enext )
{
enext=e->next;
if( (cmd_type == e->cmd_type ) &&
( wcscmp( cmd, e->cmd) == 0 ) )
{
complete_entry_opt *o, *oprev=0, *onext=0;
if(( short_opt == 0 ) && (long_opt == 0 ) )
{
complete_free_opt_recursive( e->first_option );
e->first_option=0;
}
else
{
for( o= e->first_option; o; o=onext )
{
onext=o->next;
if( ( short_opt==o->short_opt ) ||
( wcscmp( long_opt, o->long_opt ) == 0 ) )
{
wchar_t *pos;
/* fwprintf( stderr,
L"remove option -%lc --%ls\n",
o->short_opt?o->short_opt:L' ',
o->long_opt );
*/
if( o->short_opt )
{
pos = wcschr( e->short_opt_str,
o->short_opt );
if( pos )
{
wchar_t *pos2 = pos+1;
while( *pos2 == L':' )
pos2++;
memmove( pos,
pos2,
sizeof(wchar_t)*wcslen(pos2) );
}
}
if( oprev == 0 )
{
e->first_option = o->next;
}
else
{
oprev->next = o->next;
}
free( o );
}
else
oprev = o;
}
}
if( e && (e->first_option == 0) )
{
if( eprev == 0 )
{
first_entry = e->next;
}
else
{
eprev->next = e->next;
}
free( e->short_opt_str );
free( e );
e=0;
}
}
if( e )
eprev = e;
}
}
/**
Find the full path and commandname from a command string. the
result of \c pathp must be freed by the caller, the result of \c
cmdp must not be freed by the caller.
*/
static void parse_cmd_string( const wchar_t *str,
wchar_t **pathp,
wchar_t **cmdp )
{
wchar_t *cmd, *path;
/* Get the path of the command */
path = get_filename( str );
if( path == 0 )
path = wcsdup(L"");
/* Make sure the path is not included in the command */
cmd = wcsrchr( str, L'/' );
if( cmd != 0 )
cmd++;
else
cmd = (wchar_t *)str;
*pathp=path;
*cmdp=cmd;
}
int complete_is_valid_option( const wchar_t *str,
const wchar_t *opt,
array_list_t *errors )
{
complete_entry *i;
complete_entry_opt *o;
wchar_t *cmd, *path;
int found_match = 0;
int authorative = 1;
int opt_found=0;
hash_table_t gnu_match_hash;
int is_gnu_opt=0;
int is_old_opt=0;
int is_short_opt=0;
int is_gnu_exact=0;
int gnu_opt_len=0;
char *short_validated;
/*
Check some generic things like -- and - options.
*/
switch( wcslen(opt ) )
{
case 0:
return 1;
case 1:
return opt[0] == L'-';
case 2:
if( wcscmp( L"--", opt ) == 0 )
return 1;
}
if( opt[0] != L'-' )
{
if( errors )
al_push( errors, wcsdup(L"Option does not begin with a '-'") );
return 0;
}
if( !(short_validated = malloc( wcslen( opt ) )))
die_mem();
memset( short_validated, 0, wcslen( opt ) );
hash_init( &gnu_match_hash,
&hash_wcs_func,
&hash_wcs_cmp );
is_gnu_opt = opt[1]==L'-';
if( is_gnu_opt )
{
wchar_t *opt_end = wcschr(opt, L'=' );
if( opt_end )
gnu_opt_len = (opt_end-opt)-2;
else
gnu_opt_len = wcslen(opt)-2;
// fwprintf( stderr, L"Length %d optend %d\n", gnu_opt_len, opt_end );
}
parse_cmd_string( str, &path, &cmd );
/*
Make sure completions are loaded for the specified command
*/
complete_load( cmd, 0 );
for( i=first_entry; i; i=i->next )
{
wchar_t *match = i->cmd_type?path:cmd;
const wchar_t *a;
if( !wildcard_match( match, i->cmd ) )
continue;
found_match = 1;
if( !i->authorative )
{
authorative = 0;
break;
}
if( is_gnu_opt )
{
for( o = i->first_option; o; o=o->next )
{
if( o->old_mode )
continue;
//fwprintf( stderr, L"Compare \'%ls\' against \'%ls\'\n", &opt[2], o->long_opt );
if( wcsncmp( &opt[2], o->long_opt, gnu_opt_len )==0)
{
//fwprintf( stderr, L"Found gnu match %ls\n", o->long_opt );
hash_put( &gnu_match_hash, o->long_opt, L"" );
if( (wcsncmp( &opt[2],
o->long_opt,
wcslen( o->long_opt) )==0) )
is_gnu_exact=1;
}
}
}
else
{
/* Check for old style options */
for( o = i->first_option; o; o=o->next )
{
if( !o->old_mode )
continue;
if( wcscmp( &opt[1], o->long_opt )==0)
{
// fwprintf( stderr, L"Found old match %ls\n", o->long_opt );
opt_found = 1;
is_old_opt = 1;
break;
}
}
if( is_old_opt )
break;
for( a = &opt[1]; *a; a++ )
{
wchar_t *str_pos = wcschr(i->short_opt_str, *a);
if (str_pos )
{
if( *(str_pos +1)==L':' )
{
/*
This is a short option with an embedded argument,
call complete_is_valid_argument on the argument.
*/
wchar_t nopt[3];
nopt[0]=L'-';
nopt[1]=opt[1];
nopt[2]=L'\0';
//fwprintf( stderr, L"Pos %d, shortopt %lc has argument\n", a-opt, *a );
short_validated[a-opt] =
complete_is_valid_argument( str, nopt, &opt[2]);
}
else
{
// fwprintf( stderr, L"Pos %d, shortopt %lc is ok\n", a-opt, *a );
short_validated[a-opt]=1;
}
}
}
}
}
free( path );
if( authorative )
{
if( !is_gnu_opt && !is_old_opt )
is_short_opt = 1;
if( is_short_opt )
{
int j;
opt_found=1;
for( j=1; j<wcslen(opt); j++)
{
if ( !short_validated[j])
{
// fwprintf( stderr, L"Pos %d, shortopt %lc is not ok\n", j, opt[j] );
if( errors )
{
wchar_t str[2];
str[0] = opt[j];
str[1]=0;
al_push( errors,
wcsdupcat2(L"Unknown option \'", str, L"\'", 0) );
}
opt_found = 0;
break;
}
}
}
if( is_gnu_opt )
{
// fwprintf( stderr, L"Found %d matches\n", hash_get_count( &gnu_match_hash ) );
opt_found = is_gnu_exact || (hash_get_count( &gnu_match_hash )==1);
if( errors && !opt_found )
{
if( hash_get_count( &gnu_match_hash )==0)
{
al_push( errors,
wcsdupcat2(L"Unknown option \'", opt, L"\'", 0) );
}
else
{
al_push( errors,
wcsdupcat2(L"Multiple matches for option \'", opt, L"\'", 0) );
}
}
}
}
hash_destroy( &gnu_match_hash );
free( short_validated );
return (authorative && found_match)?opt_found:1;
}
int complete_is_valid_argument( const wchar_t *str,
const wchar_t *opt,
const wchar_t *arg )
{
return 1;
}
/**
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 = wcslen(suff_orig );
wchar_t *suff;
wchar_t *pos;
if( len == 0 )
return COMPLETE_FILE_DESC;
if( !suffix_hash )
{
suffix_hash = malloc( sizeof( hash_table_t) );
if( !suffix_hash )
die_mem();
hash_init( suffix_hash, &hash_wcs_func, &hash_wcs_cmp );
}
suff = wcsdup(suff_orig);
for( pos=suff; *pos; pos++ )
{
if( wcschr( L"?;#~@&", *pos ) )
{
*pos=0;
break;
}
}
wchar_t *tmp = expand_escape( suff, 0 );
free(suff);
suff = tmp;
wchar_t *desc = (wchar_t *)hash_get( suffix_hash, suff );
if( !desc )
{
wchar_t *cmd = wcsdupcat( SUFFIX_CMD_STR, suff );
if( cmd )
{
array_list_t l;
al_init( &l );
exec_subshell( cmd, &l );
free(cmd);
if( al_get_count( &l )>0 )
{
wchar_t *ln = (wchar_t *)al_get(&l, 0 );
if( wcscmp( ln, L"unknown" ) != 0 )
{
desc = wcsdupcat( COMPLETE_SEP_STR, 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[1]=towupper(desc[1]);
}
}
al_foreach( &l, (void (*)(const void *))&free );
al_destroy( &l );
}
if( !desc )
{
desc = wcsdup(COMPLETE_FILE_DESC);
}
hash_put( suffix_hash, suff!=suff_orig?suff:wcsdup(suff), desc );
}
else
{
free( suff );
}
return desc;
}
const wchar_t *complete_get_desc( const wchar_t *filename )
{
struct stat buf;
const wchar_t *desc = COMPLETE_FILE_DESC;
if( lwstat( filename, &buf )==0)
{
if( waccess( filename, X_OK ) == 0 )
{
desc = COMPLETE_EXEC_DESC;
}
if( S_ISCHR(buf.st_mode) )
desc= COMPLETE_CHAR_DESC;
else if( S_ISBLK(buf.st_mode) )
desc = COMPLETE_BLOCK_DESC;
else if( S_ISFIFO(buf.st_mode) )
desc = COMPLETE_FIFO_DESC;
else if( S_ISLNK(buf.st_mode))
{
struct stat buf2;
desc = COMPLETE_SYMLINK_DESC;
if( waccess( filename, X_OK ) == 0 )
desc = COMPLETE_EXEC_LINK_DESC;
if( wstat( filename, &buf2 ) == 0 )
{
if( S_ISDIR(buf2.st_mode) )
{
desc = L"/" COMPLETE_SYMLINK_DESC;
}
}
else
{
switch( errno )
{
case ENOENT:
desc = COMPLETE_ROTTEN_SYMLINK_DESC;
break;
case EACCES:
break;
default:
wperror( L"stat" );
break;
}
}
}
else if( S_ISSOCK(buf.st_mode))
desc= COMPLETE_SOCKET_DESC;
else if( S_ISDIR(buf.st_mode) )
desc= L"/" COMPLETE_DIRECTORY_DESC;
}
/* else
{
switch( errno )
{
case EACCES:
break;
default:
fprintf( stderr, L"The following error happened on file %ls\n", filename );
wperror( L"lstat" );
break;
}
}
*/
if( desc == COMPLETE_FILE_DESC )
{
wchar_t *suffix = wcsrchr( filename, L'.' );
if( suffix != 0 )
{
if( !wcsrchr( suffix, L'/' ) )
{
desc = complete_get_desc_suffix( suffix );
}
}
}
return desc;
}
/**
Copy any strings in possible_comp which have the specified prefix
to the list comp_out. The prefix may contain wildcards.
There are three ways to specify descriptions for each
completion. Firstly, if a description has already been added to the
completion, it is _not_ replaced. Secondly, if the desc_func
function is specified, use it to determine a dynamic
completion. Thirdly, if none of the above are available, the desc
string is used as a description.
\param comp_out the destination list
\param wc_escaped the prefix, possibly containing wildcards. The wildcard should not have been unescaped, i.e. '*' should be used for any string, not the ANY_STRING character.
\param desc the default description, used for completions with no embedded description
\param desc_func the function that generates a description for those completions witout an embedded description
\param possible_comp the list of possible completions to iterate over
*/
static void copy_strings_with_prefix( array_list_t *comp_out,
const wchar_t *wc_escaped,
const wchar_t *desc,
const wchar_t *(*desc_func)(const wchar_t *),
array_list_t *possible_comp )
{
int i;
wchar_t *wc;
wc = expand_one( wcsdup(wc_escaped), EXPAND_SKIP_SUBSHELL | EXPAND_SKIP_WILDCARDS);
if(!wc)
return;
if( wc[0] == L'~' )
{
wc=expand_tilde(wc);
}
// int str_len = wcslen( str );
for( i=0; i<al_get_count( possible_comp ); i++ )
{
wchar_t *next_str = (wchar_t *)al_get( possible_comp, i );
// fwprintf( stderr, L"try wc %ls against %ls\n", wc, next_str );
wildcard_complete( next_str, wc, desc, desc_func, comp_out );
}
free( wc );
}
/**
If command to complete is short enough, substitute
the description with the whatis information for the executable.
*/
static void complete_cmd_desc( const wchar_t *cmd, array_list_t *comp )
{
int i;
const wchar_t *cmd_start;
int cmd_len;
wchar_t *apropos_cmd=0;
array_list_t list;
hash_table_t lookup;
wchar_t *whatis_path = env_get( L"__fish_whatis_path" );
wchar_t *esc;
if( !cmd )
return;
cmd_start=wcschr(cmd, L'/');
if( !cmd_start )
cmd_start = cmd;
cmd_len = wcslen(cmd_start);
/*
Using apropos with a single-character search term produces far
to many results - require at least two characters if we don't
know the location of the whatis-database.
*/
if( (cmd_len < 2) && (!whatis_path) )
return;
if( wildcard_has( cmd_start, 0 ) )
{
return;
}
esc = expand_escape( cmd_start, 1 );
if( esc )
{
if( whatis_path )
{
apropos_cmd =wcsdupcat2( L"grep ^/dev/null -F <", whatis_path, L" ", esc, 0 );
}
else
{
apropos_cmd = wcsdupcat( L"apropos ^/dev/null ", esc );
}
free(esc);
al_init( &list );
hash_init( &lookup, &hash_wcs_func, &hash_wcs_cmp );
/*
First locate a list of possible descriptions using a single
call to apropos or a direct search if we know the location
of the whatis database. This can take some time on slower
systems with a large set of manuals, but it should be ok
since apropos is only called once.
*/
exec_subshell( apropos_cmd, &list );
/*
Then discard anything that is not a possible completion and put
the result into a hashtable with the completion as key and the
description as value.
Should be reasonably fast, since no memory allocations are needed.
*/
for( i=0; i<al_get_count( &list); i++ )
{
wchar_t *el = (wchar_t *)al_get( &list, i );
wchar_t *key, *key_end, *val_begin;
if( !el )
continue;
//fwprintf( stderr, L"%ls\n", el );
if( wcsncmp( el, cmd_start, cmd_len ) != 0 )
continue;
//fwprintf( stderr, L"%ls\n", el );
key = el + cmd_len;
key_end = wcschr( el, L' ' );
if( !key_end )
{
key_end = wcschr( el, L'\t' );
if( !key_end )
{
continue;
}
}
*key_end = 0;
val_begin=key_end+1;
//fwprintf( stderr, L"Key %ls\n", el );
while( *val_begin != L'-' && *val_begin)
{
val_begin++;
}
if( !val_begin )
{
continue;
}
val_begin++;
while( *val_begin == L' ' || *val_begin == L'\t' )
{
val_begin++;
}
if( !*val_begin )
{
continue;
}
/*
And once again I make sure the first character is uppercased
because I like it that way, and I get to decide these
things.
*/
val_begin[0]=towupper(val_begin[0]);
hash_put( &lookup, key, val_begin );
}
/*
Then do a lookup on every completion and if a match is found,
change to the new description.
This needs to do a reallocation for every description added, but
there shouldn't be that many completions, so it should be ok.
*/
for( i=0; i<al_get_count(comp); i++ )
{
wchar_t *el = (wchar_t *)al_get( comp, i );
wchar_t *cmd_end = wcschr( el,
COMPLETE_SEP );
wchar_t *new_desc;
if( cmd_end )
*cmd_end = 0;
new_desc = (wchar_t *)hash_get( &lookup,
el );
if( new_desc )
{
wchar_t *new_el = wcsdupcat2( el,
COMPLETE_SEP_STR,
new_desc,
0 );
al_set( comp, i, new_el );
free( el );
}
else
{
if( cmd_end )
*cmd_end = COMPLETE_SEP;
}
}
}
hash_destroy( &lookup );
al_foreach( &list,
(void(*)(const void *))&free );
al_destroy( &list );
free( apropos_cmd );
}
/**
Complete the specified command name. Search for executables in the
path, executables defined using an absolute path, functions,
builtins and directories for implicit cd commands.
\param cmd the command string to find completions for
B\param comp the list to add all completions to
*/
static void complete_cmd( const wchar_t *cmd,
array_list_t *comp )
{
wchar_t *path;
wchar_t *path_cpy;
wchar_t *nxt_path;
wchar_t *state;
array_list_t possible_comp;
int i;
array_list_t tmp;
wchar_t *nxt_completion;
wchar_t *cdpath = env_get(L"CDPATH");
wchar_t *cdpath_cpy = wcsdup( cdpath?cdpath:L"." );
if( (wcschr( cmd, L'/') != 0) || (cmd[0] == L'~' ) )
{
array_list_t tmp;
al_init( &tmp );
if( expand_string( wcsdup(cmd),
comp,
ACCEPT_INCOMPLETE | EXECUTABLES_ONLY ) != EXPAND_ERROR )
{
complete_cmd_desc( cmd, comp );
}
al_destroy( &tmp );
}
else
{
path = env_get(L"PATH");
path_cpy = wcsdup( path );
for( nxt_path = wcstok( path_cpy, ARRAY_SEP_STR, &state );
nxt_path != 0;
nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) )
{
nxt_completion = wcsdupcat2( nxt_path,
(nxt_path[wcslen(nxt_path)-1]==L'/'?L"":L"/"),
cmd,
0 );
if( ! nxt_completion )
continue;
al_init( &tmp );
if( expand_string( nxt_completion,
&tmp,
ACCEPT_INCOMPLETE |
EXECUTABLES_ONLY ) != EXPAND_ERROR )
{
for( i=0; i<al_get_count(&tmp); i++ )
{
al_push( comp, al_get( &tmp, i ) );
}
}
al_destroy( &tmp );
}
free( path_cpy );
complete_cmd_desc( cmd, comp );
/*
These return the original strings - don't free them
*/
al_init( &possible_comp );
function_get_names( &possible_comp, cmd[0] == L'_' );
copy_strings_with_prefix( comp, cmd, COMPLETE_FUNCTION_DESC, &function_get_desc, &possible_comp );
al_truncate( &possible_comp, 0 );
builtin_get_names( &possible_comp );
copy_strings_with_prefix( comp, cmd, COMPLETE_BUILTIN_DESC, &builtin_get_desc, &possible_comp );
al_destroy( &possible_comp );
}
// fwprintf( stderr, L"cdpath %ls\n", cdpath_cpy );
/*
Tab complete implicit cd for directories in CDPATH
*/
for( nxt_path = wcstok( cdpath_cpy, ARRAY_SEP_STR, &state );
nxt_path != 0;
nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) )
{
int i;
array_list_t tmp;
wchar_t *nxt_completion=
wcsdupcat2( nxt_path,
(nxt_path[wcslen(nxt_path)-1]==L'/'?L"":L"/"),
cmd,
0 );
if( ! nxt_completion )
continue;
al_init( &tmp );
if( expand_string( nxt_completion,
&tmp,
ACCEPT_INCOMPLETE | DIRECTORIES_ONLY ) != EXPAND_ERROR )
{
for( i=0; i<al_get_count(&tmp); i++ )
{
wchar_t *nxt = (wchar_t *)al_get( &tmp, i );
wchar_t *desc = wcsrchr( nxt, COMPLETE_SEP );
int is_valid = (desc && (wcscmp(desc,
COMPLETE_DIRECTORY_DESC)==0));
if( is_valid )
{
al_push( comp, nxt );
}
else
{
free(nxt);
}
}
}
al_destroy( &tmp );
}
free( cdpath_cpy );
}
/**
Evaluate the argument list (as supplied by complete -a) and insert
any return matching completions. Matching is done using\c
copy_strings_with_prefix, meaning the completion may contain
wildcards. Logically, this is not always the right thing to do, but
I have yet to come up with a case where one would not want this.
\param str The string to complete.
\param args The list of option arguments to be evaluated.
\param desc Description of the completion
\param comp_out The list into which the results will be inserted
*/
static void complete_from_args( const wchar_t *str,
const wchar_t *args,
const wchar_t *desc,
array_list_t *comp_out )
{
array_list_t possible_comp;
al_init( &possible_comp );
eval_args( args, &possible_comp );
debug( 3, L"desc is '%ls', %d long\n", desc, wcslen(desc) );
copy_strings_with_prefix( comp_out, str, desc, 0, &possible_comp );
al_foreach( &possible_comp, (void (*)(const void *))&free );
al_destroy( &possible_comp );
}
/**
Match against an old style long option
*/
static int param_match_old( complete_entry_opt *e,
wchar_t *optstr )
{
return (optstr[0] == L'-') && (wcscmp( e->long_opt, &optstr[1] ) == 0);
}
/**
Match a parameter
*/
static int param_match( complete_entry_opt *e,
wchar_t *optstr )
{
if( e->short_opt != L'\0' &&
e->short_opt == optstr[1] )
return 1;
if( !e->old_mode && (wcsncmp( L"--", optstr, 2 ) == 0 ))
{
if( wcscmp( e->long_opt, &optstr[2] ) == 0 )
{
return 1;
}
}
return 0;
}
/**
Test if a string is an option with an argument, like --color=auto or -I/usr/include
*/
static wchar_t *param_match2( complete_entry_opt *e,
wchar_t *optstr )
{
if( e->short_opt != L'\0' && e->short_opt == optstr[1] )
return &optstr[2];
if( !e->old_mode && (wcsncmp( L"--", optstr, 2 ) == 0) )
{
int len = wcslen( e->long_opt );
if( wcsncmp( e->long_opt, &optstr[2],len ) == 0 )
{
if( optstr[len+2] == L'=' )
return &optstr[len+3];
}
}
return 0;
}
/**
Tests whether a short option is a viable completion
*/
static int short_ok( wchar_t *arg,
wchar_t nextopt,
wchar_t *allopt )
{
wchar_t *ptr;
if( arg[0] != L'-')
return arg[0] == L'\0';
if( arg[1] == L'-' )
return 0;
if( wcschr( arg, nextopt ) != 0 )
return 0;
for( ptr = arg+1; *ptr; ptr++ )
{
wchar_t *tmp = wcschr( allopt, *ptr );
/* Unknown option */
if( tmp == 0 )
{
/*fwprintf( stderr, L"Unknown option %lc", *ptr );*/
return 0;
}
if( *(tmp+1) == L':' )
{
/* fwprintf( stderr, L"Woot %ls", allopt );*/
return 0;
}
}
return 1;
}
void complete_load( wchar_t *cmd,
int reload )
{
const wchar_t *path_var;
array_list_t path_list;
int i;
string_buffer_t path;
time_t *tm;
/*
First check that the specified completion hasn't already been loaded
*/
if( !loaded_completions )
{
loaded_completions = malloc( sizeof( hash_table_t ) );
if( !loaded_completions )
{
die_mem();
}
hash_init( loaded_completions, &hash_wcs_func, &hash_wcs_cmp );
}
/*
Get modification time of file
*/
tm = (time_t *)hash_get( loaded_completions, cmd );
/*
Return if already loaded and we are skipping reloading
*/
if( !reload && tm )
return;
/*
Do we know where to look for completions?
*/
path_var = env_get( L"fish_complete_path" );
if( !path_var )
return;
al_init( &path_list );
sb_init( &path );
expand_variable_array( path_var, &path_list );
/*
Iterate over path searching for suitable completion files
*/
for( i=0; i<al_get_count( &path_list ); i++ )
{
struct stat buf;
wchar_t *next = (wchar_t *)al_get( &path_list, i );
sb_clear( &path );
sb_append2( &path, next, L"/", cmd, L".fish", (void *)0 );
if( (wstat( (wchar_t *)path.buff, &buf )== 0) &&
(waccess( (wchar_t *)path.buff, R_OK ) == 0) )
{
if( !tm || (*tm != buf.st_mtime ) )
{
wchar_t *esc = expand_escape( (wchar_t *)path.buff, 1 );
wchar_t *src_cmd = wcsdupcat( L". ", esc );
/* if( tm )
debug( 0, L"Reload %ls completions, old time was %d, new time is %d",
cmd,
tm?*tm:-1,
buf.st_mtime);
*/
if( !tm )
{
tm = malloc(sizeof(time_t));
if( !tm )
die_mem();
}
*tm = buf.st_mtime;
hash_put( loaded_completions,
intern( cmd ),
tm );
free( esc );
complete_remove( cmd, COMMAND, 0, 0 );
/*
Source the completion file for the specified completion
*/
exec_subshell( src_cmd, 0 );
free(src_cmd);
break;
}
}
}
/*
If no file was found we insert a last modified time of Jan 1, 1970.
This way, the completions_path wont be searched over and over again
when reload is set to 0.
*/
if( !tm )
{
// debug( 0, L"Insert null timestamp for command %ls", cmd );
tm = malloc(sizeof(time_t));
if( !tm )
die_mem();
*tm = 0;
hash_put( loaded_completions, intern( cmd ), tm );
}
sb_destroy( &path );
al_foreach( &path_list, (void (*)(const void *))&free );
al_destroy( &path_list );
}
/**
Find completion for the argument str of command cmd_orig with
previous option popt. Insert results into comp_out. Return 0 if file
completion should be disabled, 1 otherwise.
*/
static int complete_param( wchar_t *cmd_orig,
wchar_t *popt,
wchar_t *str,
array_list_t *comp_out )
{
complete_entry *i;
complete_entry_opt *o;
array_list_t matches;
wchar_t *cmd, *path;
int use_common=1, use_files=1;
parse_cmd_string( cmd_orig, &path, &cmd );
complete_load( cmd, 1 );
al_init( &matches );
for( i=first_entry; i; i=i->next )
{
wchar_t *match = i->cmd_type?path:cmd;
if( ( (!wildcard_match( match, i->cmd ) ) ) )
continue;
/* wprintf( L"Found matching command %ls\n", i->cmd ); */
use_common=1;
if( str[0] == L'-' )
{
/* Check if we are entering a combined option and argument
* (like --color=auto or -I/usr/include) */
for( o = i->first_option; o; o=o->next )
{
wchar_t *arg;
if( (arg=param_match2( o, str ))!=0 && condition_test( o->condition ))
{
/* wprintf( L"Use option with desc %ls\n", o->desc ); */
use_common &= ((o->result_mode & NO_COMMON )==0);
use_files &= ((o->result_mode & NO_FILES )==0);
complete_from_args( arg, o->comp, o->desc, comp_out );
}
}
}
else if( popt[0] == L'-' )
{
/* Check if the previous option has any specified
* arguments to match against */
int found_old = 0;
/*
If we are using old style long options, check for them
first
*/
for( o = i->first_option; o; o=o->next )
{
if( o->old_mode )
{
if( param_match_old( o, popt ) && condition_test( o->condition ))
{
found_old = 1;
use_common &= ((o->result_mode & NO_COMMON )==0);
use_files &= ((o->result_mode & NO_FILES )==0);
complete_from_args( str, o->comp, o->desc, comp_out );
}
}
}
/*
No old style option matched, or we are not using old
style options. We check if any short (or gnu style
options do.
*/
if( !found_old )
{
for( o = i->first_option; o; o=o->next )
{
/*
Gnu-style options with _optional_ arguments must
be specified as a single token, so that it can
be differed from a regular argument.
*/
if( !o->old_mode && wcslen(o->long_opt) && !(o->result_mode & NO_COMMON) )
continue;
if( param_match( o, popt ) && condition_test( o->condition ))
{
use_common &= ((o->result_mode & NO_COMMON )==0);
use_files &= ((o->result_mode & NO_FILES )==0);
complete_from_args( str, o->comp, o->desc, comp_out );
}
}
}
}
if( use_common )
{
for( o = i->first_option; o; o=o->next )
{
/*
If this entry is for the base command,
check if any of the arguments match
*/
if( !condition_test( o->condition ))
continue;
if( (o->short_opt == L'\0' ) && (o->long_opt[0]==L'\0'))
{
use_files &= ((o->result_mode & NO_FILES )==0);
// debug( 0, L"Running argument command %ls", o->comp );
complete_from_args( str, o->comp, o->desc, comp_out );
}
if( wcslen(str) > 0 )
{
/*
Check if the short style option matches
*/
if( o->short_opt != L'\0' &&
short_ok( str, o->short_opt, i->short_opt_str ) )
{
wchar_t *next_opt =
malloc( sizeof(wchar_t)*(2 + wcslen(o->desc)));
if( !next_opt )
die_mem();
next_opt[0]=o->short_opt;
next_opt[1]=L'\0';
wcscat( next_opt, o->desc );
al_push( comp_out, next_opt );
}
/*
Check if the long style option matches
*/
if( o->long_opt[0] != L'\0' )
{
string_buffer_t whole_opt;
sb_init( &whole_opt );
sb_append2( &whole_opt, o->old_mode?L"-":L"--", o->long_opt, (void *)0 );
if( wcsncmp( str, (wchar_t *)whole_opt.buff, wcslen(str) )==0)
{
/*
If the option requires arguments, add
option with an appended '=' . If the
option does not accept arguments, add
option. If option accepts but does not
require arguments, add both.
*/
if( o->old_mode || !(o->result_mode & NO_COMMON ) )
{
al_push( comp_out,
wcsdupcat(&((wchar_t *)whole_opt.buff)[wcslen(str)], o->desc) );
// fwprintf( stderr, L"Add without param %ls\n", o->long_opt );
}
if( !o->old_mode && ( wcslen(o->comp) || (o->result_mode & NO_COMMON ) ) )
{
al_push( comp_out,
wcsdupcat2(&((wchar_t *)whole_opt.buff)[wcslen(str)], L"=", o->desc, 0) );
// fwprintf( stderr, L"Add with param %ls\n", o->long_opt );
}
// fwprintf( stderr, L"Matching long option %ls\n", o->long_opt );
}
sb_destroy( &whole_opt );
}
}
}
}
}
free( path );
return use_files;
}
/**
Perform file completion on the specified string
*/
static void complete_param_expand( wchar_t *str,
array_list_t *comp_out,
int do_file )
{
wchar_t *comp_str;
if( (wcsncmp( str, L"--", 2 )) == 0 && (comp_str = wcschr(str, L'=' ) ) )
{
comp_str++;
}
else
comp_str = str;
debug( 2,
L"expand_string( \"%ls\", comp_out, EXPAND_SKIP_SUBSHELL | ACCEPT_INCOMPLETE | %ls );",
comp_str,
do_file?L"0":L"EXPAND_SKIP_WILDCARDS" );
expand_string( wcsdup(comp_str), comp_out, EXPAND_SKIP_SUBSHELL | ACCEPT_INCOMPLETE | (do_file?0:EXPAND_SKIP_WILDCARDS) );
}
/**
Complete the specified string as an environment variable
*/
static int complete_variable( const wchar_t *var,
array_list_t *comp )
{
int i;
int varlen = wcslen( var );
int res = 0;
array_list_t names;
al_init( &names );
env_get_names( &names, 0 );
/* wprintf( L"Search string %ls\n", var );*/
/* wprintf( L"Got %d variables\n", al_get_count( &names ) );*/
for( i=0; i<al_get_count( &names ); i++ )
{
wchar_t *name = (wchar_t *)al_get( &names, i );
int namelen = wcslen( name );
if( varlen > namelen )
continue;
if( wcsncmp( var, name, varlen) == 0 )
{
wchar_t *value = expand_escape_variable( env_get( name ));
wchar_t *blarg;
/*
Variable description is 'Variable: VALUE
*/
blarg = wcsdupcat2( &name[varlen], COMPLETE_VAR_DESC_VAL, value, 0 );
if( blarg )
{
res =1;
al_push( comp, blarg );
}
free( value );
}
}
al_destroy( &names );
return res;
}
/**
Search the specified string for the \$ sign, try to complete as an environment variable
*/
static int try_complete_variable( const wchar_t *cmd,
array_list_t *comp )
{
int len = wcslen( cmd );
int i;
for( i=len-1; i>=0; i-- )
{
if( cmd[i] == L'$' )
{
/* wprintf( L"Var prefix \'%ls\'\n", &cmd[i+1] );*/
return complete_variable( &cmd[i+1], comp );
}
if( !isalnum(cmd[i]) && cmd[i]!=L'_' )
{
return 0;
}
}
return 0;
}
/**
Try to complete the specified string as a username. This is used by ~USER type expansion.
*/
static int try_complete_user( const wchar_t *cmd,
array_list_t *comp )
{
const wchar_t *first_char=0;
const wchar_t *p;
int mode = 0;
int res = 0;
for( p=cmd; *p; p++ )
{
switch( mode )
{
/*Between parameters*/
case 0:
switch( *p )
{
case L'\"':
mode=2;
p++;
first_char = p;
break;
case L' ':
case L'\t':
case L'\n':
case L'\r':
break;
default:
mode=1;
first_char = p;
}
break;
/*Inside non-quoted parameter*/
case 1:
switch( *p )
{
case L' ':
case L'\t':
case L'\n':
case L'\r':
mode = 0;
break;
}
break;
case 2:
switch( *p )
{
case L'\"':
if( *(p-1) != L'\\' )
mode =0;
break;
}
break;
}
}
if( mode != 0 )
{
if( *first_char ==L'~' )
{
const wchar_t *user_name = first_char+1;
wchar_t *name_end = wcschr( user_name, L'~' );
if( name_end == 0 )
{
struct passwd *pw;
int name_len = wcslen( user_name );
/* wprintf( L"Complete name \'%ls\'\n", user_name );*/
setpwent();
while((pw=getpwent()) != 0)
{
/* wprintf( L"Try %ls\n", pw->pw_name );*/
wchar_t *pw_name = str2wcs( pw->pw_name );
if( pw_name )
{
if( wcsncmp( user_name, pw_name, name_len )==0 )
{
wchar_t *blarg = wcsdupcat2( &pw_name[name_len],
L"/",
COMPLETE_USER_DESC,
0 );
if( blarg != 0 )
{
al_push( comp, blarg );
res=1;
}
}
free( pw_name );
}
}
endpwent();
}
}
}
return res;
}
void complete( const wchar_t *cmd,
array_list_t *comp )
{
wchar_t *begin, *end, *prev_begin, *prev_end, *buff;
tokenizer tok;
wchar_t *current_token=0, *current_command=0, *prev_token=0;
int on_command=0;
int pos;
int old_error_max = error_max;
int done=0;
error_max=0;
/**
If we are completing a variable name or a tilde expansion user
name, we do that and return. No need for any other competions.
*/
if( try_complete_variable( cmd, comp ))
{
done=1;
}
else if( try_complete_user( cmd, comp ))
{
done=1;
}
/*
Set on_command to true if cursor is over a command, and set the
name of the current command, and various other parsing to find
out what we should complete, and how it should be completed.
*/
if( !done )
{
reader_current_subshell_extent( &begin, &end );
if( !begin )
done=1;
}
if( !done )
{
pos = reader_get_cursor_pos()-(begin-reader_get_buffer());
buff = wcsndup( begin, end-begin );
if( !buff )
done=1;
}
if( !done )
{
int had_cmd=0;
int end_loop=0;
tok_init( &tok, buff, TOK_ACCEPT_UNFINISHED );
while( tok_has_next( &tok) && !end_loop )
{
switch( tok_last_type( &tok ) )
{
case TOK_STRING:
if( !had_cmd )
{
if( parser_is_subcommand( tok_last( &tok ) ) )
break;
current_command = wcsdup( tok_last( &tok ) );
on_command = (pos <= tok_get_pos( &tok) + wcslen( tok_last( &tok ) ) );
had_cmd=1;
}
break;
case TOK_END:
case TOK_PIPE:
case TOK_BACKGROUND:
had_cmd=0;
break;
case TOK_ERROR:
end_loop=1;
break;
}
if( tok_get_pos( &tok ) >= pos )
end_loop=1;
tok_next( &tok );
}
tok_destroy( &tok );
free( buff );
/*
Get the string to complete
*/
reader_current_token_extent( &begin, &end, &prev_begin, &prev_end );
current_token = wcsndup( begin, reader_get_cursor_pos()-(begin-reader_get_buffer()) );
prev_token = prev_begin ? wcsndup( prev_begin, prev_end - prev_begin ): wcsdup(L"");
// fwprintf( stderr, L"on_command: %d, %ls %ls\n", on_command, current_compmand, current_token );
if( current_token && current_command && prev_token )
{
if( on_command )
{
/* Complete command filename */
complete_cmd( current_token,
comp );
}
else
{
/*
Complete parameter. Parameter expansion should be
performed against both the globbed and the unglobbed
version of the string, so we create a list containing
all possible versions of the string that is to be
expanded. This is potentially very slow.
*/
int do_file;
do_file = complete_param( current_command, prev_token, current_token, comp );
complete_param_expand( current_token, comp, do_file );
}
}
free( current_token );
free( current_command );
free( prev_token );
}
error_max=old_error_max;
condition_cache_clear();
}
/**
Print the GNU longopt style switch \c opt, and the argument \c
argument to the specified stringbuffer, but only if arguemnt is
non-null and longer than 0 characters.
*/
static void append_switch( string_buffer_t *out,
const wchar_t *opt,
const wchar_t *argument )
{
wchar_t *esc;
if( !argument || argument==L"" )
return;
esc = expand_escape( argument, 1 );
sb_printf( out, L" --%ls %ls", opt, esc );
free(esc);
}
void complete_print( string_buffer_t *out )
{
complete_entry *e;
for( e = first_entry; e; e=e->next )
{
complete_entry_opt *o;
for( o= e->first_option; o; o=o->next )
{
wchar_t *modestr[] =
{
L"",
L" --no-files",
L" --require-parameter",
L" --exclusive"
}
;
sb_printf( out,
L"complete%ls",
modestr[o->result_mode] );
append_switch( out,
e->cmd_type?L"path":L"command",
e->cmd );
if( o->short_opt != 0 )
{
sb_printf( out,
L" --short-option '%lc'",
o->short_opt );
}
append_switch( out,
o->old_mode?L"old-option":L"long-option",
o->long_opt );
append_switch( out,
L"description",
o->desc );
append_switch( out,
L"arguments",
o->comp );
append_switch( out,
L"condition",
o->condition );
sb_printf( out, L"\n" );
}
}
}
|