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
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
|
/* bashline.c -- Bash's interface to the readline library. */
/* Copyright (C) 1987-2020 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash 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.
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#if defined (READLINE)
#include "bashtypes.h"
#include "posixstat.h"
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#if defined (HAVE_GRP_H)
# include <grp.h>
#endif
#if defined (HAVE_NETDB_H)
# include <netdb.h>
#endif
#include <signal.h>
#include <stdio.h>
#include "chartypes.h"
#include "bashansi.h"
#include "bashintl.h"
#include "shell.h"
#include "input.h"
#include "parser.h"
#include "builtins.h"
#include "bashhist.h"
#include "bashline.h"
#include "execute_cmd.h"
#include "findcmd.h"
#include "pathexp.h"
#include "shmbutil.h"
#include "trap.h"
#include "flags.h"
#if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
# include <mbstr.h> /* mbschr */
#endif
#include "builtins/common.h"
#include "builtins/builtext.h" /* for read_builtin */
#include <readline/rlconf.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <readline/rlmbutil.h>
#include <glob/glob.h>
#if defined (ALIAS)
# include "alias.h"
#endif
#if defined (PROGRAMMABLE_COMPLETION)
# include "pcomplete.h"
#endif
/* These should agree with the defines for emacs_mode and vi_mode in
rldefs.h, even though that's not a public readline header file. */
#ifndef EMACS_EDITING_MODE
# define NO_EDITING_MODE -1
# define EMACS_EDITING_MODE 1
# define VI_EDITING_MODE 0
#endif
/* Copied from rldefs.h, since that's not a public readline header file. */
#ifndef FUNCTION_TO_KEYMAP
#if defined (CRAY)
# define FUNCTION_TO_KEYMAP(map, key) (Keymap)((int)map[key].function)
# define KEYMAP_TO_FUNCTION(data) (rl_command_func_t *)((int)(data))
#else
# define FUNCTION_TO_KEYMAP(map, key) (Keymap)(map[key].function)
# define KEYMAP_TO_FUNCTION(data) (rl_command_func_t *)(data)
#endif
#endif
#define RL_BOOLEAN_VARIABLE_VALUE(s) ((s)[0] == 'o' && (s)[1] == 'n' && (s)[2] == '\0')
#if defined (BRACE_COMPLETION)
extern int bash_brace_completion PARAMS((int, int));
#endif /* BRACE_COMPLETION */
/* To avoid including curses.h/term.h/termcap.h and that whole mess. */
#ifdef _MINIX
extern int tputs PARAMS((const char *string, int nlines, void (*outx)(int)));
#else
extern int tputs PARAMS((const char *string, int nlines, int (*outx)(int)));
#endif
/* Forward declarations */
/* Functions bound to keys in Readline for Bash users. */
static int shell_expand_line PARAMS((int, int));
static int display_shell_version PARAMS((int, int));
static int bash_ignore_filenames PARAMS((char **));
static int bash_ignore_everything PARAMS((char **));
static int bash_progcomp_ignore_filenames PARAMS((char **));
#if defined (BANG_HISTORY)
static char *history_expand_line_internal PARAMS((char *));
static int history_expand_line PARAMS((int, int));
static int tcsh_magic_space PARAMS((int, int));
#endif /* BANG_HISTORY */
#ifdef ALIAS
static int alias_expand_line PARAMS((int, int));
#endif
#if defined (BANG_HISTORY) && defined (ALIAS)
static int history_and_alias_expand_line PARAMS((int, int));
#endif
static int bash_forward_shellword PARAMS((int, int));
static int bash_backward_shellword PARAMS((int, int));
static int bash_kill_shellword PARAMS((int, int));
static int bash_backward_kill_shellword PARAMS((int, int));
static int bash_transpose_shellwords PARAMS((int, int));
/* Helper functions for Readline. */
static char *restore_tilde PARAMS((char *, char *));
static char *maybe_restore_tilde PARAMS((char *, char *));
static char *bash_filename_rewrite_hook PARAMS((char *, int));
static void bash_directory_expansion PARAMS((char **));
static int bash_filename_stat_hook PARAMS((char **));
static int bash_command_name_stat_hook PARAMS((char **));
static int bash_directory_completion_hook PARAMS((char **));
static int filename_completion_ignore PARAMS((char **));
static int bash_push_line PARAMS((void));
static int executable_completion PARAMS((const char *, int));
static rl_icppfunc_t *save_directory_hook PARAMS((void));
static void restore_directory_hook PARAMS((rl_icppfunc_t));
static int directory_exists PARAMS((const char *, int));
static void cleanup_expansion_error PARAMS((void));
static void maybe_make_readline_line PARAMS((char *));
static void set_up_new_line PARAMS((char *));
static int check_redir PARAMS((int));
static char **attempt_shell_completion PARAMS((const char *, int, int));
static char *variable_completion_function PARAMS((const char *, int));
static char *hostname_completion_function PARAMS((const char *, int));
static char *command_subst_completion_function PARAMS((const char *, int));
static void build_history_completion_array PARAMS((void));
static char *history_completion_generator PARAMS((const char *, int));
static int dynamic_complete_history PARAMS((int, int));
static int bash_dabbrev_expand PARAMS((int, int));
static void initialize_hostname_list PARAMS((void));
static void add_host_name PARAMS((char *));
static void snarf_hosts_from_file PARAMS((char *));
static char **hostnames_matching PARAMS((char *));
static void _ignore_completion_names PARAMS((char **, sh_ignore_func_t *));
static int name_is_acceptable PARAMS((const char *));
static int test_for_directory PARAMS((const char *));
static int test_for_canon_directory PARAMS((const char *));
static int return_zero PARAMS((const char *));
static char *bash_dequote_filename PARAMS((char *, int));
static char *quote_word_break_chars PARAMS((char *));
static void set_filename_bstab PARAMS((const char *));
static char *bash_quote_filename PARAMS((char *, int, char *));
#ifdef _MINIX
static void putx PARAMS((int));
#else
static int putx PARAMS((int));
#endif
static int readline_get_char_offset PARAMS((int));
static void readline_set_char_offset PARAMS((int, int *));
static Keymap get_cmd_xmap_from_edit_mode PARAMS((void));
static Keymap get_cmd_xmap_from_keymap PARAMS((Keymap));
static void init_unix_command_map PARAMS((void));
static int isolate_sequence PARAMS((char *, int, int, int *));
static int set_saved_history PARAMS((void));
#if defined (ALIAS)
static int posix_edit_macros PARAMS((int, int));
#endif
static int bash_event_hook PARAMS((void));
#if defined (PROGRAMMABLE_COMPLETION)
static int find_cmd_start PARAMS((int));
static int find_cmd_end PARAMS((int));
static char *find_cmd_name PARAMS((int, int *, int *));
static char *prog_complete_return PARAMS((const char *, int));
static char **prog_complete_matches;
#endif
extern int no_symbolic_links;
extern STRING_INT_ALIST word_token_alist[];
/* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
completion functions which indicate what type of completion should be
done (at or before point) that can be bound to key sequences with
the readline library. */
#define SPECIFIC_COMPLETION_FUNCTIONS
#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
static int bash_specific_completion PARAMS((int, rl_compentry_func_t *));
static int bash_complete_filename_internal PARAMS((int));
static int bash_complete_username_internal PARAMS((int));
static int bash_complete_hostname_internal PARAMS((int));
static int bash_complete_variable_internal PARAMS((int));
static int bash_complete_command_internal PARAMS((int));
static int bash_complete_filename PARAMS((int, int));
static int bash_possible_filename_completions PARAMS((int, int));
static int bash_complete_username PARAMS((int, int));
static int bash_possible_username_completions PARAMS((int, int));
static int bash_complete_hostname PARAMS((int, int));
static int bash_possible_hostname_completions PARAMS((int, int));
static int bash_complete_variable PARAMS((int, int));
static int bash_possible_variable_completions PARAMS((int, int));
static int bash_complete_command PARAMS((int, int));
static int bash_possible_command_completions PARAMS((int, int));
static int completion_glob_pattern PARAMS((char *));
static char *glob_complete_word PARAMS((const char *, int));
static int bash_glob_completion_internal PARAMS((int));
static int bash_glob_complete_word PARAMS((int, int));
static int bash_glob_expand_word PARAMS((int, int));
static int bash_glob_list_expansions PARAMS((int, int));
#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
static int edit_and_execute_command PARAMS((int, int, int, char *));
#if defined (VI_MODE)
static int vi_edit_and_execute_command PARAMS((int, int));
static int bash_vi_complete PARAMS((int, int));
#endif
static int emacs_edit_and_execute_command PARAMS((int, int));
/* Non-zero once initialize_readline () has been called. */
int bash_readline_initialized = 0;
/* If non-zero, we do hostname completion, breaking words at `@' and
trying to complete the stuff after the `@' from our own internal
host list. */
int perform_hostname_completion = 1;
/* If non-zero, we don't do command completion on an empty line. */
int no_empty_command_completion;
/* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the
only possible matches. Set to 0 if you want to match filenames if they
are the only possible matches, even if FIGNORE says to. */
int force_fignore = 1;
/* Perform spelling correction on directory names during word completion */
int dircomplete_spelling = 0;
/* Expand directory names during word/filename completion. */
#if DIRCOMPLETE_EXPAND_DEFAULT
int dircomplete_expand = 1;
int dircomplete_expand_relpath = 1;
#else
int dircomplete_expand = 0;
int dircomplete_expand_relpath = 0;
#endif
/* When non-zero, perform `normal' shell quoting on completed filenames
even when the completed name contains a directory name with a shell
variable reference, so dollar signs in a filename get quoted appropriately.
Set to zero to remove dollar sign (and braces or parens as needed) from
the set of characters that will be quoted. */
int complete_fullquote = 1;
static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:";
static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:";
/* )) */
static const char *default_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{~"; /*}*/
static char *custom_filename_quote_characters = 0;
static char filename_bstab[256];
static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL;
static int dot_in_path = 0;
/* Set to non-zero when dabbrev-expand is running */
static int dabbrev_expand_active = 0;
/* What kind of quoting is performed by bash_quote_filename:
COMPLETE_DQUOTE = double-quoting the filename
COMPLETE_SQUOTE = single_quoting the filename
COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
*/
#define COMPLETE_DQUOTE 1
#define COMPLETE_SQUOTE 2
#define COMPLETE_BSQUOTE 3
static int completion_quoting_style = COMPLETE_BSQUOTE;
/* Flag values for the final argument to bash_default_completion */
#define DEFCOMP_CMDPOS 1
static rl_command_func_t *vi_tab_binding = rl_complete;
/* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
Called when the shell is put into or out of `posix' mode. */
void
posix_readline_initialize (on_or_off)
int on_or_off;
{
static char kseq[2] = { CTRL ('I'), 0 }; /* TAB */
if (on_or_off)
rl_variable_bind ("comment-begin", "#");
#if defined (VI_MODE)
if (on_or_off)
{
vi_tab_binding = rl_function_of_keyseq (kseq, vi_insertion_keymap, (int *)NULL);
rl_bind_key_in_map (CTRL ('I'), rl_insert, vi_insertion_keymap);
}
else
{
if (rl_function_of_keyseq (kseq, vi_insertion_keymap, (int *)NULL) == rl_insert)
rl_bind_key_in_map (CTRL ('I'), vi_tab_binding, vi_insertion_keymap);
}
#endif
}
void
reset_completer_word_break_chars ()
{
rl_completer_word_break_characters = perform_hostname_completion ? savestring (bash_completer_word_break_characters) : savestring (bash_nohostname_word_break_characters);
}
/* When this function returns, rl_completer_word_break_characters points to
dynamically allocated memory. */
int
enable_hostname_completion (on_or_off)
int on_or_off;
{
int old_value;
char *at, *nv, *nval;
old_value = perform_hostname_completion;
if (on_or_off)
{
perform_hostname_completion = 1;
rl_special_prefixes = "$@";
}
else
{
perform_hostname_completion = 0;
rl_special_prefixes = "$";
}
/* Now we need to figure out how to appropriately modify and assign
rl_completer_word_break_characters depending on whether we want
hostname completion on or off. */
/* If this is the first time this has been called
(bash_readline_initialized == 0), use the sames values as before, but
allocate new memory for rl_completer_word_break_characters. */
if (bash_readline_initialized == 0 &&
(rl_completer_word_break_characters == 0 ||
rl_completer_word_break_characters == rl_basic_word_break_characters))
{
if (on_or_off)
rl_completer_word_break_characters = savestring (bash_completer_word_break_characters);
else
rl_completer_word_break_characters = savestring (bash_nohostname_word_break_characters);
}
else
{
/* See if we have anything to do. */
at = strchr (rl_completer_word_break_characters, '@');
if ((at == 0 && on_or_off == 0) || (at != 0 && on_or_off != 0))
return old_value;
/* We have something to do. Do it. */
nval = (char *)xmalloc (strlen (rl_completer_word_break_characters) + 1 + on_or_off);
if (on_or_off == 0)
{
/* Turn it off -- just remove `@' from word break chars. We want
to remove all occurrences of `@' from the char list, so we loop
rather than just copy the rest of the list over AT. */
for (nv = nval, at = rl_completer_word_break_characters; *at; )
if (*at != '@')
*nv++ = *at++;
else
at++;
*nv = '\0';
}
else
{
nval[0] = '@';
strcpy (nval + 1, rl_completer_word_break_characters);
}
free (rl_completer_word_break_characters);
rl_completer_word_break_characters = nval;
}
return (old_value);
}
/* Called once from parse.y if we are going to use readline. */
void
initialize_readline ()
{
rl_command_func_t *func;
char kseq[2];
if (bash_readline_initialized)
return;
rl_terminal_name = get_string_value ("TERM");
rl_instream = stdin;
rl_outstream = stderr;
/* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = "Bash";
/* Add bindable names before calling rl_initialize so they may be
referenced in the various inputrc files. */
rl_add_defun ("shell-expand-line", shell_expand_line, -1);
#ifdef BANG_HISTORY
rl_add_defun ("history-expand-line", history_expand_line, -1);
rl_add_defun ("magic-space", tcsh_magic_space, -1);
#endif
rl_add_defun ("shell-forward-word", bash_forward_shellword, -1);
rl_add_defun ("shell-backward-word", bash_backward_shellword, -1);
rl_add_defun ("shell-kill-word", bash_kill_shellword, -1);
rl_add_defun ("shell-backward-kill-word", bash_backward_kill_shellword, -1);
rl_add_defun ("shell-transpose-words", bash_transpose_shellwords, -1);
#ifdef ALIAS
rl_add_defun ("alias-expand-line", alias_expand_line, -1);
# ifdef BANG_HISTORY
rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line, -1);
# endif
#endif
/* Backwards compatibility. */
rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1);
rl_add_defun ("display-shell-version", display_shell_version, -1);
rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command, -1);
#if defined (BRACE_COMPLETION)
rl_add_defun ("complete-into-braces", bash_brace_completion, -1);
#endif
#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
rl_add_defun ("complete-filename", bash_complete_filename, -1);
rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1);
rl_add_defun ("complete-username", bash_complete_username, -1);
rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1);
rl_add_defun ("complete-hostname", bash_complete_hostname, -1);
rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1);
rl_add_defun ("complete-variable", bash_complete_variable, -1);
rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1);
rl_add_defun ("complete-command", bash_complete_command, -1);
rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1);
rl_add_defun ("glob-complete-word", bash_glob_complete_word, -1);
rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1);
rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1);
#endif
rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1);
rl_add_defun ("dabbrev-expand", bash_dabbrev_expand, -1);
/* Bind defaults before binding our custom shell keybindings. */
if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0)
rl_initialize ();
/* Bind up our special shell functions. */
rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line, emacs_meta_keymap);
#ifdef BANG_HISTORY
rl_bind_key_if_unbound_in_map ('^', history_expand_line, emacs_meta_keymap);
#endif
rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version, emacs_ctlx_keymap);
/* In Bash, the user can switch editing modes with "set -o [vi emacs]",
so it is not necessary to allow C-M-j for context switching. Turn
off this occasionally confusing behaviour. */
kseq[0] = CTRL('J');
kseq[1] = '\0';
func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
if (func == rl_vi_editing_mode)
rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
kseq[0] = CTRL('M');
func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
if (func == rl_vi_editing_mode)
rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap);
#if defined (VI_MODE)
kseq[0] = CTRL('E');
func = rl_function_of_keyseq (kseq, vi_movement_keymap, (int *)NULL);
if (func == rl_emacs_editing_mode)
rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap);
#endif
#if defined (BRACE_COMPLETION)
rl_bind_key_if_unbound_in_map ('{', bash_brace_completion, emacs_meta_keymap); /*}*/
#endif /* BRACE_COMPLETION */
#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
rl_bind_key_if_unbound_in_map ('/', bash_complete_filename, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions, emacs_ctlx_keymap);
/* Have to jump through hoops here because there is a default binding for
M-~ (rl_tilde_expand) */
kseq[0] = '~';
kseq[1] = '\0';
func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
if (func == 0 || func == rl_tilde_expand)
rl_bind_keyseq_in_map (kseq, bash_complete_username, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map ('~', bash_possible_username_completions, emacs_ctlx_keymap);
rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map ('@', bash_possible_hostname_completions, emacs_ctlx_keymap);
rl_bind_key_if_unbound_in_map ('$', bash_complete_variable, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map ('$', bash_possible_variable_completions, emacs_ctlx_keymap);
rl_bind_key_if_unbound_in_map ('!', bash_complete_command, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions, emacs_ctlx_keymap);
rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word, emacs_ctlx_keymap);
rl_bind_key_if_unbound_in_map ('g', bash_glob_list_expansions, emacs_ctlx_keymap);
#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
kseq[0] = TAB;
kseq[1] = '\0';
func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
if (func == 0 || func == rl_tab_insert)
rl_bind_key_in_map (TAB, dynamic_complete_history, emacs_meta_keymap);
/* Tell the completer that we want a crack first. */
rl_attempted_completion_function = attempt_shell_completion;
/* Tell the completer that we might want to follow symbolic links or
do other expansion on directory names. */
set_directory_hook ();
rl_filename_rewrite_hook = bash_filename_rewrite_hook;
rl_filename_stat_hook = bash_filename_stat_hook;
/* Tell the filename completer we want a chance to ignore some names. */
rl_ignore_some_completions_function = filename_completion_ignore;
/* Bind C-xC-e to invoke emacs and run result as commands. */
rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command, emacs_ctlx_keymap);
#if defined (VI_MODE)
rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command, vi_movement_keymap);
# if defined (ALIAS)
rl_bind_key_if_unbound_in_map ('@', posix_edit_macros, vi_movement_keymap);
# endif
rl_bind_key_in_map ('\\', bash_vi_complete, vi_movement_keymap);
rl_bind_key_in_map ('*', bash_vi_complete, vi_movement_keymap);
rl_bind_key_in_map ('=', bash_vi_complete, vi_movement_keymap);
#endif
rl_completer_quote_characters = "'\"";
/* This sets rl_completer_word_break_characters and rl_special_prefixes
to the appropriate values, depending on whether or not hostname
completion is enabled. */
enable_hostname_completion (perform_hostname_completion);
/* characters that need to be quoted when appearing in filenames. */
rl_filename_quote_characters = default_filename_quote_characters;
set_filename_bstab (rl_filename_quote_characters);
rl_filename_quoting_function = bash_quote_filename;
rl_filename_dequoting_function = bash_dequote_filename;
rl_char_is_quoted_p = char_is_quoted;
/* Add some default bindings for the "shellwords" functions, roughly
parallelling the default word bindings in emacs mode. */
rl_bind_key_if_unbound_in_map (CTRL('B'), bash_backward_shellword, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map (CTRL('D'), bash_kill_shellword, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map (CTRL('F'), bash_forward_shellword, emacs_meta_keymap);
rl_bind_key_if_unbound_in_map (CTRL('T'), bash_transpose_shellwords, emacs_meta_keymap);
#if 0
/* This is superfluous and makes it impossible to use tab completion in
vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix()
should already have called posix_readline_initialize() when
posixly_correct was set. */
if (posixly_correct)
posix_readline_initialize (1);
#endif
bash_readline_initialized = 1;
}
void
bashline_reinitialize ()
{
bash_readline_initialized = 0;
}
void
bashline_set_event_hook ()
{
rl_signal_event_hook = bash_event_hook;
}
void
bashline_reset_event_hook ()
{
rl_signal_event_hook = 0;
}
/* On Sun systems at least, rl_attempted_completion_function can end up
getting set to NULL, and rl_completion_entry_function set to do command
word completion if Bash is interrupted while trying to complete a command
word. This just resets all the completion functions to the right thing.
It's called from throw_to_top_level(). */
void
bashline_reset ()
{
tilde_initialize ();
rl_attempted_completion_function = attempt_shell_completion;
rl_completion_entry_function = NULL;
rl_ignore_some_completions_function = filename_completion_ignore;
rl_filename_quote_characters = default_filename_quote_characters;
set_filename_bstab (rl_filename_quote_characters);
set_directory_hook ();
rl_filename_stat_hook = bash_filename_stat_hook;
bashline_reset_event_hook ();
rl_sort_completion_matches = 1;
}
/* Contains the line to push into readline. */
static char *push_to_readline = (char *)NULL;
/* Push the contents of push_to_readline into the
readline buffer. */
static int
bash_push_line ()
{
if (push_to_readline)
{
rl_insert_text (push_to_readline);
free (push_to_readline);
push_to_readline = (char *)NULL;
rl_startup_hook = old_rl_startup_hook;
}
return 0;
}
/* Call this to set the initial text for the next line to read
from readline. */
int
bash_re_edit (line)
char *line;
{
FREE (push_to_readline);
push_to_readline = savestring (line);
old_rl_startup_hook = rl_startup_hook;
rl_startup_hook = bash_push_line;
return (0);
}
static int
display_shell_version (count, c)
int count, c;
{
rl_crlf ();
show_shell_version (0);
putc ('\r', rl_outstream);
fflush (rl_outstream);
rl_on_new_line ();
rl_redisplay ();
return 0;
}
/* **************************************************************** */
/* */
/* Readline Stuff */
/* */
/* **************************************************************** */
/* If the user requests hostname completion, then simply build a list
of hosts, and complete from that forever more, or at least until
HOSTFILE is unset. */
/* THIS SHOULD BE A STRINGLIST. */
/* The kept list of hostnames. */
static char **hostname_list = (char **)NULL;
/* The physical size of the above list. */
static int hostname_list_size;
/* The number of hostnames in the above list. */
static int hostname_list_length;
/* Whether or not HOSTNAME_LIST has been initialized. */
int hostname_list_initialized = 0;
/* Initialize the hostname completion table. */
static void
initialize_hostname_list ()
{
char *temp;
temp = get_string_value ("HOSTFILE");
if (temp == 0)
temp = get_string_value ("hostname_completion_file");
if (temp == 0)
temp = DEFAULT_HOSTS_FILE;
snarf_hosts_from_file (temp);
if (hostname_list)
hostname_list_initialized++;
}
/* Add NAME to the list of hosts. */
static void
add_host_name (name)
char *name;
{
if (hostname_list_length + 2 > hostname_list_size)
{
hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32);
hostname_list = strvec_resize (hostname_list, hostname_list_size);
}
hostname_list[hostname_list_length++] = savestring (name);
hostname_list[hostname_list_length] = (char *)NULL;
}
#define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
static void
snarf_hosts_from_file (filename)
char *filename;
{
FILE *file;
char *temp, buffer[256], name[256];
register int i, start;
file = fopen (filename, "r");
if (file == 0)
return;
while (temp = fgets (buffer, 255, file))
{
/* Skip to first character. */
for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++)
;
/* If comment or blank line, ignore. */
if (buffer[i] == '\0' || buffer[i] == '#')
continue;
/* If `preprocessor' directive, do the include. */
if (strncmp (buffer + i, "$include ", 9) == 0)
{
char *incfile, *t;
/* Find start of filename. */
for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++)
;
/* Find end of filename. */
for (t = incfile; *t && cr_whitespace (*t) == 0; t++)
;
*t = '\0';
snarf_hosts_from_file (incfile);
continue;
}
/* Skip internet address if present. */
if (DIGIT (buffer[i]))
for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++);
/* Gobble up names. Each name is separated with whitespace. */
while (buffer[i])
{
for (; cr_whitespace (buffer[i]); i++)
;
if (buffer[i] == '\0' || buffer[i] == '#')
break;
/* Isolate the current word. */
for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++)
;
if (i == start)
continue;
strncpy (name, buffer + start, i - start);
name[i - start] = '\0';
add_host_name (name);
}
}
fclose (file);
}
/* Return the hostname list. */
char **
get_hostname_list ()
{
if (hostname_list_initialized == 0)
initialize_hostname_list ();
return (hostname_list);
}
void
clear_hostname_list ()
{
register int i;
if (hostname_list_initialized == 0)
return;
for (i = 0; i < hostname_list_length; i++)
free (hostname_list[i]);
hostname_list_length = hostname_list_initialized = 0;
}
/* Return a NULL terminated list of hostnames which begin with TEXT.
Initialize the hostname list the first time if necessary.
The array is malloc ()'ed, but not the individual strings. */
static char **
hostnames_matching (text)
char *text;
{
register int i, len, nmatch, rsize;
char **result;
if (hostname_list_initialized == 0)
initialize_hostname_list ();
if (hostname_list_initialized == 0)
return ((char **)NULL);
/* Special case. If TEXT consists of nothing, then the whole list is
what is desired. */
if (*text == '\0')
{
result = strvec_create (1 + hostname_list_length);
for (i = 0; i < hostname_list_length; i++)
result[i] = hostname_list[i];
result[i] = (char *)NULL;
return (result);
}
/* Scan until found, or failure. */
len = strlen (text);
result = (char **)NULL;
for (i = nmatch = rsize = 0; i < hostname_list_length; i++)
{
if (STREQN (text, hostname_list[i], len) == 0)
continue;
/* OK, it matches. Add it to the list. */
if (nmatch >= (rsize - 1))
{
rsize = (rsize + 16) - (rsize % 16);
result = strvec_resize (result, rsize);
}
result[nmatch++] = hostname_list[i];
}
if (nmatch)
result[nmatch] = (char *)NULL;
return (result);
}
/* This vi mode command causes VI_EDIT_COMMAND to be run on the current
command being entered (if no explicit argument is given), otherwise on
a command from the history file. */
#define VI_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-vi}}\""
#define EMACS_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-emacs}}\""
#define POSIX_VI_EDIT_COMMAND "fc -e vi"
static int
edit_and_execute_command (count, c, editing_mode, edit_command)
int count, c, editing_mode;
char *edit_command;
{
char *command, *metaval;
int r, rrs, metaflag;
sh_parser_state_t ps;
rrs = rl_readline_state;
saved_command_line_count = current_command_line_count;
/* Accept the current line. */
rl_newline (1, c);
if (rl_explicit_arg)
{
command = (char *)xmalloc (strlen (edit_command) + 8);
sprintf (command, "%s %d", edit_command, count);
}
else
{
/* Take the command we were just editing, add it to the history file,
then call fc to operate on it. We have to add a dummy command to
the end of the history because fc ignores the last command (assumes
it's supposed to deal with the command before the `fc'). */
/* This breaks down when using command-oriented history and are not
finished with the command, so we should not ignore the last command */
using_history ();
current_command_line_count++; /* for rl_newline above */
bash_add_history (rl_line_buffer);
current_command_line_count = 0; /* for dummy history entry */
bash_add_history ("");
history_lines_this_session++;
using_history ();
command = savestring (edit_command);
}
metaval = rl_variable_value ("input-meta");
metaflag = RL_BOOLEAN_VARIABLE_VALUE (metaval);
if (rl_deprep_term_function)
(*rl_deprep_term_function) ();
rl_clear_signals ();
save_parser_state (&ps);
r = parse_and_execute (command, (editing_mode == VI_EDITING_MODE) ? "v" : "C-xC-e", SEVAL_NOHIST);
restore_parser_state (&ps);
/* if some kind of reset_parser was called, undo it. */
reset_readahead_token ();
if (rl_prep_term_function)
(*rl_prep_term_function) (metaflag);
rl_set_signals ();
current_command_line_count = saved_command_line_count;
/* Now erase the contents of the current line and undo the effects of the
rl_accept_line() above. We don't even want to make the text we just
executed available for undoing. */
rl_line_buffer[0] = '\0'; /* XXX */
rl_point = rl_end = 0;
rl_done = 0;
rl_readline_state = rrs;
#if defined (VI_MODE)
if (editing_mode == VI_EDITING_MODE)
rl_vi_insertion_mode (1, c);
#endif
rl_forced_update_display ();
return r;
}
#if defined (VI_MODE)
static int
vi_edit_and_execute_command (count, c)
int count, c;
{
if (posixly_correct)
return (edit_and_execute_command (count, c, VI_EDITING_MODE, POSIX_VI_EDIT_COMMAND));
else
return (edit_and_execute_command (count, c, VI_EDITING_MODE, VI_EDIT_COMMAND));
}
#endif /* VI_MODE */
static int
emacs_edit_and_execute_command (count, c)
int count, c;
{
return (edit_and_execute_command (count, c, EMACS_EDITING_MODE, EMACS_EDIT_COMMAND));
}
#if defined (ALIAS)
static int
posix_edit_macros (count, key)
int count, key;
{
int c;
char alias_name[3], *alias_value, *macro;
c = rl_read_key ();
alias_name[0] = '_';
alias_name[1] = c;
alias_name[2] = '\0';
alias_value = get_alias_value (alias_name);
if (alias_value && *alias_value)
{
macro = savestring (alias_value);
rl_push_macro_input (macro);
}
return 0;
}
#endif
/* Bindable commands that move `shell-words': that is, sequences of
non-unquoted-metacharacters. */
#define WORDDELIM(c) (shellmeta(c) || shellblank(c))
static int
bash_forward_shellword (count, key)
int count, key;
{
size_t slen;
int c, p;
DECLARE_MBSTATE;
if (count < 0)
return (bash_backward_shellword (-count, key));
/* The tricky part of this is deciding whether or not the first character
we're on is an unquoted metacharacter. Not completely handled yet. */
/* XXX - need to test this stuff with backslash-escaped shell
metacharacters and unclosed single- and double-quoted strings. */
p = rl_point;
slen = rl_end;
while (count)
{
if (p == rl_end)
{
rl_point = rl_end;
return 0;
}
/* Are we in a quoted string? If we are, move to the end of the quoted
string and continue the outer loop. We only want quoted strings, not
backslash-escaped characters, but char_is_quoted doesn't
differentiate. */
if (char_is_quoted (rl_line_buffer, p) && p > 0 && rl_line_buffer[p-1] != '\\')
{
do
ADVANCE_CHAR (rl_line_buffer, slen, p);
while (p < rl_end && char_is_quoted (rl_line_buffer, p));
count--;
continue;
}
/* Rest of code assumes we are not in a quoted string. */
/* Move forward until we hit a non-metacharacter. */
while (p < rl_end && (c = rl_line_buffer[p]) && WORDDELIM (c))
{
switch (c)
{
default:
ADVANCE_CHAR (rl_line_buffer, slen, p);
continue; /* straight back to loop, don't increment p */
case '\\':
if (p < rl_end && rl_line_buffer[p])
ADVANCE_CHAR (rl_line_buffer, slen, p);
break;
case '\'':
p = skip_to_delim (rl_line_buffer, ++p, "'", SD_NOJMP);
break;
case '"':
p = skip_to_delim (rl_line_buffer, ++p, "\"", SD_NOJMP);
break;
}
if (p < rl_end)
p++;
}
if (rl_line_buffer[p] == 0 || p == rl_end)
{
rl_point = rl_end;
rl_ding ();
return 0;
}
/* Now move forward until we hit a non-quoted metacharacter or EOL */
while (p < rl_end && (c = rl_line_buffer[p]) && WORDDELIM (c) == 0)
{
switch (c)
{
default:
ADVANCE_CHAR (rl_line_buffer, slen, p);
continue; /* straight back to loop, don't increment p */
case '\\':
if (p < rl_end && rl_line_buffer[p])
ADVANCE_CHAR (rl_line_buffer, slen, p);
break;
case '\'':
p = skip_to_delim (rl_line_buffer, ++p, "'", SD_NOJMP);
break;
case '"':
p = skip_to_delim (rl_line_buffer, ++p, "\"", SD_NOJMP);
break;
}
if (p < rl_end)
p++;
}
if (p == rl_end || rl_line_buffer[p] == 0)
{
rl_point = rl_end;
return (0);
}
count--;
}
rl_point = p;
return (0);
}
static int
bash_backward_shellword (count, key)
int count, key;
{
size_t slen;
int c, p, prev_p;
DECLARE_MBSTATE;
if (count < 0)
return (bash_forward_shellword (-count, key));
p = rl_point;
slen = rl_end;
while (count)
{
if (p == 0)
{
rl_point = 0;
return 0;
}
/* Move backward until we hit a non-metacharacter. We want to deal
with the characters before point, so we move off a word if we're
at its first character. */
BACKUP_CHAR (rl_line_buffer, slen, p);
while (p > 0)
{
c = rl_line_buffer[p];
if (WORDDELIM (c) == 0 || char_is_quoted (rl_line_buffer, p))
break;
BACKUP_CHAR (rl_line_buffer, slen, p);
}
if (p == 0)
{
rl_point = 0;
return 0;
}
/* Now move backward until we hit a metacharacter or BOL. Leave point
at the start of the shellword or at BOL. */
prev_p = p;
while (p > 0)
{
c = rl_line_buffer[p];
if (WORDDELIM (c) && char_is_quoted (rl_line_buffer, p) == 0)
{
p = prev_p;
break;
}
prev_p = p;
BACKUP_CHAR (rl_line_buffer, slen, p);
}
count--;
}
rl_point = p;
return 0;
}
static int
bash_kill_shellword (count, key)
int count, key;
{
int p;
if (count < 0)
return (bash_backward_kill_shellword (-count, key));
p = rl_point;
bash_forward_shellword (count, key);
if (rl_point != p)
rl_kill_text (p, rl_point);
rl_point = p;
if (rl_editing_mode == EMACS_EDITING_MODE) /* 1 == emacs_mode */
rl_mark = rl_point;
return 0;
}
static int
bash_backward_kill_shellword (count, key)
int count, key;
{
int p;
if (count < 0)
return (bash_kill_shellword (-count, key));
p = rl_point;
bash_backward_shellword (count, key);
if (rl_point != p)
rl_kill_text (p, rl_point);
if (rl_editing_mode == EMACS_EDITING_MODE) /* 1 == emacs_mode */
rl_mark = rl_point;
return 0;
}
static int
bash_transpose_shellwords (count, key)
int count, key;
{
char *word1, *word2;
int w1_beg, w1_end, w2_beg, w2_end;
int orig_point = rl_point;
if (count == 0)
return 0;
/* Find the two shell words. */
bash_forward_shellword (count, key);
w2_end = rl_point;
bash_backward_shellword (1, key);
w2_beg = rl_point;
bash_backward_shellword (count, key);
w1_beg = rl_point;
bash_forward_shellword (1, key);
w1_end = rl_point;
/* check that there really are two words. */
if ((w1_beg == w2_beg) || (w2_beg < w1_end))
{
rl_ding ();
rl_point = orig_point;
return 1;
}
/* Get the text of the words. */
word1 = rl_copy_text (w1_beg, w1_end);
word2 = rl_copy_text (w2_beg, w2_end);
/* We are about to do many insertions and deletions. Remember them
as one operation. */
rl_begin_undo_group ();
/* Do the stuff at word2 first, so that we don't have to worry
about word1 moving. */
rl_point = w2_beg;
rl_delete_text (w2_beg, w2_end);
rl_insert_text (word1);
rl_point = w1_beg;
rl_delete_text (w1_beg, w1_end);
rl_insert_text (word2);
/* This is exactly correct since the text before this point has not
changed in length. */
rl_point = w2_end;
/* I think that does it. */
rl_end_undo_group ();
xfree (word1);
xfree (word2);
return 0;
}
/* **************************************************************** */
/* */
/* How To Do Shell Completion */
/* */
/* **************************************************************** */
#define COMMAND_SEPARATORS ";|&{(`"
/* )} */
#define COMMAND_SEPARATORS_PLUS_WS ";|&{(` \t"
/* )} */
/* check for redirections and other character combinations that are not
command separators */
static int
check_redir (ti)
int ti;
{
register int this_char, prev_char;
/* Handle the two character tokens `>&', `<&', and `>|'.
We are not in a command position after one of these. */
this_char = rl_line_buffer[ti];
prev_char = (ti > 0) ? rl_line_buffer[ti - 1] : 0;
if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) ||
(this_char == '|' && prev_char == '>'))
return (1);
else if (this_char == '{' && prev_char == '$') /*}*/
return (1);
#if 0 /* Not yet */
else if (this_char == '(' && prev_char == '$') /*)*/
return (1);
else if (this_char == '(' && prev_char == '<') /*)*/
return (1);
#if defined (EXTENDED_GLOB)
else if (extended_glob && this_char == '(' && prev_char == '!') /*)*/
return (1);
#endif
#endif
else if (char_is_quoted (rl_line_buffer, ti))
return (1);
return (0);
}
#if defined (PROGRAMMABLE_COMPLETION)
/*
* XXX - because of the <= start test, and setting os = s+1, this can
* potentially return os > start. This is probably not what we want to
* happen, but fix later after 2.05a-release.
*/
static int
find_cmd_start (start)
int start;
{
register int s, os, ns;
os = 0;
/* Flags == SD_NOJMP only because we want to skip over command substitutions
in assignment statements. Have to test whether this affects `standalone'
command substitutions as individual words. */
while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE/*|SD_NOSKIPCMD*/)) <= start) &&
rl_line_buffer[s])
{
/* Handle >| token crudely; treat as > not | */
if (s > 0 && rl_line_buffer[s] == '|' && rl_line_buffer[s-1] == '>')
{
ns = skip_to_delim (rl_line_buffer, s+1, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE/*|SD_NOSKIPCMD*/);
if (ns > start || rl_line_buffer[ns] == 0)
return os;
os = ns+1;
continue;
}
/* The only reserved word in COMMAND_SEPARATORS is `{', so handle that
specially, making sure it's in a spot acceptable for reserved words */
if (s >= os && rl_line_buffer[s] == '{')
{
int pc, nc; /* index of previous non-whitespace, next char */
for (pc = (s > os) ? s - 1 : os; pc > os && whitespace(rl_line_buffer[pc]); pc--)
;
nc = rl_line_buffer[s+1];
/* must be preceded by a command separator or be the first non-
whitespace character since the last command separator, and
followed by a shell break character (not another `{') to be a reserved word. */
if ((pc > os && (rl_line_buffer[s-1] == '{' || strchr (COMMAND_SEPARATORS, rl_line_buffer[pc]) == 0)) ||
(shellbreak(nc) == 0)) /* }} */
{
/* Not a reserved word, look for another delim */
ns = skip_to_delim (rl_line_buffer, s+1, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE/*|SD_NOSKIPCMD*/);
if (ns > start || rl_line_buffer[ns] == 0)
return os;
os = ns+1;
continue;
}
}
os = s+1;
}
return os;
}
static int
find_cmd_end (end)
int end;
{
register int e;
e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE);
return e;
}
static char *
find_cmd_name (start, sp, ep)
int start;
int *sp, *ep;
{
char *name;
register int s, e;
for (s = start; whitespace (rl_line_buffer[s]); s++)
;
/* skip until a shell break character */
e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n", SD_NOJMP|SD_COMPLETE);
name = substring (rl_line_buffer, s, e);
if (sp)
*sp = s;
if (ep)
*ep = e;
return (name);
}
static char *
prog_complete_return (text, matchnum)
const char *text;
int matchnum;
{
static int ind;
if (matchnum == 0)
ind = 0;
if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0)
return (char *)NULL;
return (prog_complete_matches[ind++]);
}
#endif /* PROGRAMMABLE_COMPLETION */
/* Try and catch completion attempts that are syntax errors or otherwise
invalid. */
static int
invalid_completion (text, ind)
const char *text;
int ind;
{
int pind;
/* If we don't catch these here, the next clause will */
if (ind > 0 && rl_line_buffer[ind] == '(' && /*)*/
member (rl_line_buffer[ind-1], "$<>"))
return 0;
pind = ind - 1;
while (pind > 0 && whitespace (rl_line_buffer[pind]))
pind--;
/* If we have only whitespace preceding a paren, it's valid */
if (ind >= 0 && pind <= 0 && rl_line_buffer[ind] == '(') /*)*/
return 0;
/* Flag the invalid completions, which are mostly syntax errors */
if (ind > 0 && rl_line_buffer[ind] == '(' && /*)*/
member (rl_line_buffer[pind], COMMAND_SEPARATORS) == 0)
return 1;
return 0;
}
/* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
at START and END. Return an array of matches, or NULL if none. */
static char **
attempt_shell_completion (text, start, end)
const char *text;
int start, end;
{
int in_command_position, ti, qc, dflags;
char **matches, *command_separator_chars;
#if defined (PROGRAMMABLE_COMPLETION)
int have_progcomps, was_assignment;
COMPSPEC *iw_compspec;
#endif
command_separator_chars = COMMAND_SEPARATORS;
matches = (char **)NULL;
rl_ignore_some_completions_function = filename_completion_ignore;
rl_filename_quote_characters = default_filename_quote_characters;
set_filename_bstab (rl_filename_quote_characters);
set_directory_hook ();
rl_filename_stat_hook = bash_filename_stat_hook;
rl_sort_completion_matches = 1; /* sort by default */
/* Determine if this could be a command word. It is if it appears at
the start of the line (ignoring preceding whitespace), or if it
appears after a character that separates commands. It cannot be a
command word if we aren't at the top-level prompt. */
ti = start - 1;
qc = -1;
while ((ti > -1) && (whitespace (rl_line_buffer[ti])))
ti--;
#if 1
/* If this is an open quote, maybe we're trying to complete a quoted
command name. */
if (ti >= 0 && (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\''))
{
qc = rl_line_buffer[ti];
ti--;
while (ti > -1 && (whitespace (rl_line_buffer[ti])))
ti--;
}
#endif
in_command_position = 0;
if (ti < 0)
{
/* Only do command completion at the start of a line when we
are prompting at the top level. */
if (current_prompt_string == ps1_prompt)
in_command_position++;
else if (parser_in_command_position ())
in_command_position++;
}
else if (member (rl_line_buffer[ti], command_separator_chars))
{
in_command_position++;
if (check_redir (ti) == 1)
in_command_position = 0;
}
else
{
/* This still could be in command position. It is possible
that all of the previous words on the line are variable
assignments. */
}
if (in_command_position && invalid_completion (text, ti))
{
rl_attempted_completion_over = 1;
return ((char **)NULL);
}
/* Check that we haven't incorrectly flagged a closed command substitution
as indicating we're in a command position. */
if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' &&
*text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0)
in_command_position = 0;
/* Special handling for command substitution. If *TEXT is a backquote,
it can be the start or end of an old-style command substitution, or
unmatched. If it's unmatched, both calls to unclosed_pair will
succeed. Don't bother if readline found a single quote and we are
completing on the substring. */
if (*text == '`' && rl_completion_quote_character != '\'' &&
(in_command_position || (unclosed_pair (rl_line_buffer, start, "`") &&
unclosed_pair (rl_line_buffer, end, "`"))))
matches = rl_completion_matches (text, command_subst_completion_function);
#if defined (PROGRAMMABLE_COMPLETION)
/* Attempt programmable completion. */
have_progcomps = prog_completion_enabled && (progcomp_size () > 0);
iw_compspec = progcomp_search (INITIALWORD);
if (matches == 0 &&
(in_command_position == 0 || text[0] == '\0' || (in_command_position && iw_compspec)) &&
current_prompt_string == ps1_prompt)
{
int s, e, s1, e1, os, foundcs;
char *n;
/* XXX - don't free the members */
if (prog_complete_matches)
free (prog_complete_matches);
prog_complete_matches = (char **)NULL;
os = start;
n = 0;
was_assignment = 0;
s = find_cmd_start (os);
e = find_cmd_end (end);
do
{
/* Don't read past the end of rl_line_buffer */
if (s > rl_end)
{
s1 = s = e1;
break;
}
/* Or past point if point is within an assignment statement */
else if (was_assignment && s > rl_point)
{
s1 = s = e1;
break;
}
/* Skip over assignment statements preceding a command name. If we
don't find a command name at all, we can perform command name
completion. If we find a partial command name, we should perform
command name completion on it. */
FREE (n);
n = find_cmd_name (s, &s1, &e1);
s = e1 + 1;
}
while (was_assignment = assignment (n, 0));
s = s1; /* reset to index where name begins */
/* s == index of where command name begins (reset above)
e == end of current command, may be end of line
s1 = index of where command name begins
e1 == index of where command name ends
start == index of where word to be completed begins
end == index of where word to be completed ends
if (s == start) we are doing command word completion for sure
if (e1 == end) we are at the end of the command name and completing it */
if (start == 0 && end == 0 && e != 0 && text[0] == '\0') /* beginning of non-empty line */
foundcs = 0;
else if (start == end && start == s1 && e != 0 && e1 > end) /* beginning of command name, leading whitespace */
foundcs = 0;
else if (e == 0 && e == s && text[0] == '\0' && have_progcomps) /* beginning of empty line */
prog_complete_matches = programmable_completions (EMPTYCMD, text, s, e, &foundcs);
else if (start == end && text[0] == '\0' && s1 > start && whitespace (rl_line_buffer[start]))
foundcs = 0; /* whitespace before command name */
else if (e > s && was_assignment == 0 && e1 == end && rl_line_buffer[e] == 0 && whitespace (rl_line_buffer[e-1]) == 0)
{
/* not assignment statement, but still want to perform command
completion if we are composing command word. */
foundcs = 0;
in_command_position = s == start && STREQ (n, text); /* XXX */
}
else if (e > s && was_assignment == 0 && have_progcomps)
{
prog_complete_matches = programmable_completions (n, text, s, e, &foundcs);
/* command completion if programmable completion fails */
/* If we have a completion for the initial word, we can prefer that */
in_command_position = s == start && (iw_compspec || STREQ (n, text)); /* XXX */
if (iw_compspec && in_command_position)
foundcs = 0;
}
/* empty command name following command separator */
else if (s >= e && n[0] == '\0' && text[0] == '\0' && start > 0 &&
was_assignment == 0 && member (rl_line_buffer[start-1], COMMAND_SEPARATORS))
{
foundcs = 0;
in_command_position = 1;
}
else if (s >= e && n[0] == '\0' && text[0] == '\0' && start > 0)
{
foundcs = 0; /* empty command name following optional assignments */
in_command_position += was_assignment;
}
else if (s == start && e == end && STREQ (n, text) && start > 0)
{
foundcs = 0; /* partial command name following assignments */
in_command_position = 1;
}
else
foundcs = 0;
/* If we have defined a compspec for the initial (command) word, call
it and process the results like any other programmable completion. */
if (in_command_position && have_progcomps && foundcs == 0 && iw_compspec)
prog_complete_matches = programmable_completions (INITIALWORD, text, s, e, &foundcs);
FREE (n);
/* XXX - if we found a COMPSPEC for the command, just return whatever
the programmable completion code returns, and disable the default
filename completion that readline will do unless the COPT_DEFAULT
option has been set with the `-o default' option to complete or
compopt. */
if (foundcs)
{
pcomp_set_readline_variables (foundcs, 1);
/* Turn what the programmable completion code returns into what
readline wants. I should have made compute_lcd_of_matches
external... */
matches = rl_completion_matches (text, prog_complete_return);
if ((foundcs & COPT_DEFAULT) == 0)
rl_attempted_completion_over = 1; /* no default */
if (matches || ((foundcs & COPT_BASHDEFAULT) == 0))
return (matches);
}
}
#endif
if (matches == 0)
{
dflags = 0;
if (in_command_position)
dflags |= DEFCOMP_CMDPOS;
matches = bash_default_completion (text, start, end, qc, dflags);
}
return matches;
}
char **
bash_default_completion (text, start, end, qc, compflags)
const char *text;
int start, end, qc, compflags;
{
char **matches, *t;
matches = (char **)NULL;
/* New posix-style command substitution or variable name? */
if (*text == '$')
{
if (qc != '\'' && text[1] == '(') /* ) */
matches = rl_completion_matches (text, command_subst_completion_function);
else
{
matches = rl_completion_matches (text, variable_completion_function);
/* If a single match, see if it expands to a directory name and append
a slash if it does. This requires us to expand the variable name,
so we don't want to display errors if the variable is unset. This
can happen with dynamic variables whose value has never been
requested. */
if (matches && matches[0] && matches[1] == 0)
{
t = savestring (matches[0]);
bash_filename_stat_hook (&t);
/* doesn't use test_for_directory because that performs tilde
expansion */
if (file_isdir (t))
rl_completion_append_character = '/';
free (t);
}
}
}
/* If the word starts in `~', and there is no slash in the word, then
try completing this word as a username. */
if (matches == 0 && *text == '~' && mbschr (text, '/') == 0)
matches = rl_completion_matches (text, rl_username_completion_function);
/* Another one. Why not? If the word starts in '@', then look through
the world of known hostnames for completion first. */
if (matches == 0 && perform_hostname_completion && *text == '@')
matches = rl_completion_matches (text, hostname_completion_function);
/* And last, (but not least) if this word is in a command position, then
complete over possible command names, including aliases, functions,
and command names. */
if (matches == 0 && (compflags & DEFCOMP_CMDPOS))
{
/* If END == START and text[0] == 0, we are trying to complete an empty
command word. */
if (no_empty_command_completion && end == start && text[0] == '\0')
{
matches = (char **)NULL;
rl_ignore_some_completions_function = bash_ignore_everything;
}
else
{
#define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x))
dot_in_path = 0;
matches = rl_completion_matches (text, command_word_completion_function);
/* If we are attempting command completion and nothing matches, we
do not want readline to perform filename completion for us. We
still want to be able to complete partial pathnames, so set the
completion ignore function to something which will remove
filenames and leave directories in the match list. */
if (matches == (char **)NULL)
rl_ignore_some_completions_function = bash_ignore_filenames;
else if (matches[1] == 0 && CMD_IS_DIR(matches[0]) && dot_in_path == 0)
/* If we found a single match, without looking in the current
directory (because it's not in $PATH), but the found name is
also a command in the current directory, suppress appending any
terminating character, since it's ambiguous. */
{
rl_completion_suppress_append = 1;
rl_filename_completion_desired = 0;
}
else if (matches[0] && matches[1] && STREQ (matches[0], matches[1]) && CMD_IS_DIR (matches[0]))
/* There are multiple instances of the same match (duplicate
completions haven't yet been removed). In this case, all of
the matches will be the same, and the duplicate removal code
will distill them all down to one. We turn on
rl_completion_suppress_append for the same reason as above.
Remember: we only care if there's eventually a single unique
completion. If there are multiple completions this won't
make a difference and the problem won't occur. */
{
rl_completion_suppress_append = 1;
rl_filename_completion_desired = 0;
}
}
}
/* This could be a globbing pattern, so try to expand it using pathname
expansion. */
if (!matches && completion_glob_pattern ((char *)text))
{
matches = rl_completion_matches (text, glob_complete_word);
/* A glob expression that matches more than one filename is problematic.
If we match more than one filename, punt. */
if (matches && matches[1] && rl_completion_type == TAB)
{
strvec_dispose (matches);
matches = (char **)0;
}
else if (matches && matches[1] && rl_completion_type == '!')
{
rl_completion_suppress_append = 1;
rl_filename_completion_desired = 0;
}
}
return (matches);
}
static int
bash_command_name_stat_hook (name)
char **name;
{
char *cname, *result;
/* If it's not something we're going to look up in $PATH, just call the
normal filename stat hook. */
if (absolute_program (*name))
return (bash_filename_stat_hook (name));
cname = *name;
/* XXX - we could do something here with converting aliases, builtins,
and functions into something that came out as executable, but we don't. */
result = search_for_command (cname, 0);
if (result)
{
*name = result;
return 1;
}
return 0;
}
static int
executable_completion (filename, searching_path)
const char *filename;
int searching_path;
{
char *f;
int r;
f = savestring (filename);
bash_directory_completion_hook (&f);
r = searching_path ? executable_file (f) : executable_or_directory (f);
free (f);
return r;
}
/* This is the function to call when the word to complete is in a position
where a command word can be found. It grovels $PATH, looking for commands
that match. It also scans aliases, function names, and the shell_builtin
table. */
char *
command_word_completion_function (hint_text, state)
const char *hint_text;
int state;
{
static char *hint = (char *)NULL;
static char *path = (char *)NULL;
static char *val = (char *)NULL;
static char *filename_hint = (char *)NULL;
static char *fnhint = (char *)NULL;
static char *dequoted_hint = (char *)NULL;
static char *directory_part = (char *)NULL;
static char **glob_matches = (char **)NULL;
static int path_index, hint_len, istate, igncase;
static int mapping_over, local_index, searching_path, hint_is_dir;
static int old_glob_ignore_case, globpat;
static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
#if defined (ALIAS)
static alias_t **alias_list = (alias_t **)NULL;
#endif /* ALIAS */
char *temp, *cval;
/* We have to map over the possibilities for command words. If we have
no state, then make one just for that purpose. */
if (state == 0)
{
rl_filename_stat_hook = bash_command_name_stat_hook;
if (dequoted_hint && dequoted_hint != hint)
free (dequoted_hint);
if (hint)
free (hint);
mapping_over = searching_path = 0;
hint_is_dir = CMD_IS_DIR (hint_text);
val = (char *)NULL;
temp = rl_variable_value ("completion-ignore-case");
igncase = RL_BOOLEAN_VARIABLE_VALUE (temp);
if (glob_matches)
{
free (glob_matches);
glob_matches = (char **)NULL;
}
globpat = completion_glob_pattern ((char *)hint_text);
/* If this is an absolute program name, do not check it against
aliases, reserved words, functions or builtins. We must check
whether or not it is unique, and, if so, whether that filename
is executable. */
if (globpat || absolute_program (hint_text))
{
/* Perform tilde expansion on what's passed, so we don't end up
passing filenames with tildes directly to stat(). The rest of
the shell doesn't do variable expansion on the word following
the tilde, so we don't do it here even if direxpand is set. */
if (*hint_text == '~')
{
hint = bash_tilde_expand (hint_text, 0);
directory_part = savestring (hint_text);
temp = strchr (directory_part, '/');
if (temp)
*temp = 0;
else
{
free (directory_part);
directory_part = (char *)NULL;
}
}
else if (dircomplete_expand)
{
hint = savestring (hint_text);
bash_directory_completion_hook (&hint);
}
else
hint = savestring (hint_text);
dequoted_hint = hint;
/* If readline's completer found a quote character somewhere, but
didn't set the quote character, there must have been a quote
character embedded in the filename. It can't be at the start of
the filename, so we need to dequote the filename before we look
in the file system for it. */
if (rl_completion_found_quote && rl_completion_quote_character == 0)
{
dequoted_hint = bash_dequote_filename (hint, 0);
free (hint);
hint = dequoted_hint;
}
hint_len = strlen (hint);
if (filename_hint)
free (filename_hint);
fnhint = filename_hint = savestring (hint);
istate = 0;
if (globpat)
{
mapping_over = 5;
goto globword;
}
else
{
if (dircomplete_expand && path_dot_or_dotdot (filename_hint))
{
dircomplete_expand = 0;
set_directory_hook ();
dircomplete_expand = 1;
}
mapping_over = 4;
goto inner;
}
}
dequoted_hint = hint = savestring (hint_text);
hint_len = strlen (hint);
if (rl_completion_found_quote && rl_completion_quote_character == 0)
dequoted_hint = bash_dequote_filename (hint, 0);
path = get_string_value ("PATH");
path_index = dot_in_path = 0;
/* Initialize the variables for each type of command word. */
local_index = 0;
if (varlist)
free (varlist);
varlist = all_visible_functions ();
#if defined (ALIAS)
if (alias_list)
free (alias_list);
alias_list = all_aliases ();
#endif /* ALIAS */
}
/* mapping_over says what we are currently hacking. Note that every case
in this list must fall through when there are no more possibilities. */
switch (mapping_over)
{
case 0: /* Aliases come first. */
#if defined (ALIAS)
while (alias_list && alias_list[local_index])
{
register char *alias;
alias = alias_list[local_index++]->name;
if (igncase == 0 && (STREQN (alias, hint, hint_len)))
return (savestring (alias));
else if (igncase && strncasecmp (alias, hint, hint_len) == 0)
return (savestring (alias));
}
#endif /* ALIAS */
local_index = 0;
mapping_over++;
case 1: /* Then shell reserved words. */
{
while (word_token_alist[local_index].word)
{
register char *reserved_word;
reserved_word = word_token_alist[local_index++].word;
if (STREQN (reserved_word, hint, hint_len))
return (savestring (reserved_word));
}
local_index = 0;
mapping_over++;
}
case 2: /* Then function names. */
while (varlist && varlist[local_index])
{
register char *varname;
varname = varlist[local_index++]->name;
/* Honor completion-ignore-case for shell function names. */
if (igncase == 0 && (STREQN (varname, hint, hint_len)))
return (savestring (varname));
else if (igncase && strncasecmp (varname, hint, hint_len) == 0)
return (savestring (varname));
}
local_index = 0;
mapping_over++;
case 3: /* Then shell builtins. */
for (; local_index < num_shell_builtins; local_index++)
{
/* Ignore it if it doesn't have a function pointer or if it
is not currently enabled. */
if (!shell_builtins[local_index].function ||
(shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0)
continue;
if (STREQN (shell_builtins[local_index].name, hint, hint_len))
{
int i = local_index++;
return (savestring (shell_builtins[i].name));
}
}
local_index = 0;
mapping_over++;
}
globword:
/* Limited support for completing command words with globbing chars. Only
a single match (multiple matches that end up reducing the number of
characters in the common prefix are bad) will ever be returned on
regular completion. */
if (globpat)
{
if (state == 0)
{
glob_ignore_case = igncase;
glob_matches = shell_glob_filename (hint, 0);
glob_ignore_case = old_glob_ignore_case;
if (GLOB_FAILED (glob_matches) || glob_matches == 0)
{
glob_matches = (char **)NULL;
return ((char *)NULL);
}
local_index = 0;
if (glob_matches[1] && rl_completion_type == TAB) /* multiple matches are bad */
return ((char *)NULL);
}
while (val = glob_matches[local_index++])
{
if (executable_or_directory (val))
{
if (*hint_text == '~' && directory_part)
{
temp = maybe_restore_tilde (val, directory_part);
free (val);
val = temp;
}
return (val);
}
free (val);
}
glob_ignore_case = old_glob_ignore_case;
return ((char *)NULL);
}
/* If the text passed is a directory in the current directory, return it
as a possible match. Executables in directories in the current
directory can be specified using relative pathnames and successfully
executed even when `.' is not in $PATH. */
if (hint_is_dir)
{
hint_is_dir = 0; /* only return the hint text once */
return (savestring (hint_text));
}
/* Repeatedly call filename_completion_function while we have
members of PATH left. Question: should we stat each file?
Answer: we call executable_file () on each file. */
outer:
istate = (val != (char *)NULL);
if (istate == 0)
{
char *current_path;
/* Get the next directory from the path. If there is none, then we
are all done. */
if (path == 0 || path[path_index] == 0 ||
(current_path = extract_colon_unit (path, &path_index)) == 0)
return ((char *)NULL);
searching_path = 1;
if (*current_path == 0)
{
free (current_path);
current_path = savestring (".");
}
if (*current_path == '~')
{
char *t;
t = bash_tilde_expand (current_path, 0);
free (current_path);
current_path = t;
}
if (current_path[0] == '.' && current_path[1] == '\0')
dot_in_path = 1;
if (fnhint && fnhint != filename_hint)
free (fnhint);
if (filename_hint)
free (filename_hint);
filename_hint = sh_makepath (current_path, hint, 0);
/* Need a quoted version (though it doesn't matter much in most
cases) because rl_filename_completion_function dequotes the
filename it gets, assuming that it's been quoted as part of
the input line buffer. */
if (strpbrk (filename_hint, "\"'\\"))
fnhint = sh_backslash_quote (filename_hint, filename_bstab, 0);
else
fnhint = filename_hint;
free (current_path); /* XXX */
}
inner:
val = rl_filename_completion_function (fnhint, istate);
if (mapping_over == 4 && dircomplete_expand)
set_directory_hook ();
istate = 1;
if (val == 0)
{
/* If the hint text is an absolute program, then don't bother
searching through PATH. */
if (absolute_program (hint))
return ((char *)NULL);
goto outer;
}
else
{
int match, freetemp;
if (absolute_program (hint))
{
if (igncase == 0)
match = strncmp (val, hint, hint_len) == 0;
else
match = strncasecmp (val, hint, hint_len) == 0;
/* If we performed tilde expansion, restore the original
filename. */
if (*hint_text == '~')
temp = maybe_restore_tilde (val, directory_part);
else
temp = savestring (val);
freetemp = 1;
}
else
{
temp = strrchr (val, '/');
if (temp)
{
temp++;
if (igncase == 0)
freetemp = match = strncmp (temp, hint, hint_len) == 0;
else
freetemp = match = strncasecmp (temp, hint, hint_len) == 0;
if (match)
temp = savestring (temp);
}
else
freetemp = match = 0;
}
/* If we have found a match, and it is an executable file, return it.
We don't return directory names when searching $PATH, since the
bash execution code won't find executables in directories which
appear in directories in $PATH when they're specified using
relative pathnames. */
#if 0
/* If we're not searching $PATH and we have a relative pathname, we
need to re-canonicalize it before testing whether or not it's an
executable or a directory so the shell treats .. relative to $PWD
according to the physical/logical option. The shell already
canonicalizes the directory name in order to tell readline where
to look, so not doing it here will be inconsistent. */
/* XXX -- currently not used -- will introduce more inconsistency,
since shell does not canonicalize ../foo before passing it to
shell_execve(). */
if (match && searching_path == 0 && *val == '.')
{
char *t, *t1;
t = get_working_directory ("command-word-completion");
t1 = make_absolute (val, t);
free (t);
cval = sh_canonpath (t1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
}
else
#endif
cval = val;
if (match && executable_completion ((searching_path ? val : cval), searching_path))
{
if (cval != val)
free (cval);
free (val);
val = ""; /* So it won't be NULL. */
return (temp);
}
else
{
if (freetemp)
free (temp);
if (cval != val)
free (cval);
free (val);
goto inner;
}
}
}
/* Completion inside an unterminated command substitution. */
static char *
command_subst_completion_function (text, state)
const char *text;
int state;
{
static char **matches = (char **)NULL;
static const char *orig_start;
static char *filename_text = (char *)NULL;
static int cmd_index, start_len;
char *value;
if (state == 0)
{
if (filename_text)
free (filename_text);
orig_start = text;
if (*text == '`')
text++;
else if (*text == '$' && text[1] == '(') /* ) */
text += 2;
/* If the text was quoted, suppress any quote character that the
readline completion code would insert. */
rl_completion_suppress_quote = 1;
start_len = text - orig_start;
filename_text = savestring (text);
if (matches)
free (matches);
/*
* At this point we can entertain the idea of re-parsing
* `filename_text' into a (possibly incomplete) command name and
* arguments, and doing completion based on that. This is
* currently very rudimentary, but it is a small improvement.
*/
for (value = filename_text + strlen (filename_text) - 1; value > filename_text; value--)
if (whitespace (*value) || member (*value, COMMAND_SEPARATORS))
break;
if (value <= filename_text)
matches = rl_completion_matches (filename_text, command_word_completion_function);
else
{
value++;
start_len += value - filename_text;
if (whitespace (value[-1]))
matches = rl_completion_matches (value, rl_filename_completion_function);
else
matches = rl_completion_matches (value, command_word_completion_function);
}
/* If there is more than one match, rl_completion_matches has already
put the lcd in matches[0]. Skip over it. */
cmd_index = matches && matches[0] && matches[1];
/* If there's a single match and it's a directory, set the append char
to the expected `/'. Otherwise, don't append anything. */
if (matches && matches[0] && matches[1] == 0 && test_for_directory (matches[0]))
rl_completion_append_character = '/';
else
rl_completion_suppress_append = 1;
}
if (matches == 0 || matches[cmd_index] == 0)
{
rl_filename_quoting_desired = 0; /* disable quoting */
return ((char *)NULL);
}
else
{
value = (char *)xmalloc (1 + start_len + strlen (matches[cmd_index]));
if (start_len == 1)
value[0] = *orig_start;
else
strncpy (value, orig_start, start_len);
strcpy (value + start_len, matches[cmd_index]);
cmd_index++;
return (value);
}
}
/* Okay, now we write the entry_function for variable completion. */
static char *
variable_completion_function (text, state)
const char *text;
int state;
{
static char **varlist = (char **)NULL;
static int varlist_index;
static char *varname = (char *)NULL;
static int first_char, first_char_loc;
if (!state)
{
if (varname)
free (varname);
first_char_loc = 0;
first_char = text[0];
if (first_char == '$')
first_char_loc++;
if (text[first_char_loc] == '{')
first_char_loc++;
varname = savestring (text + first_char_loc);
if (varlist)
strvec_dispose (varlist);
varlist = all_variables_matching_prefix (varname);
varlist_index = 0;
}
if (!varlist || !varlist[varlist_index])
{
return ((char *)NULL);
}
else
{
char *value;
value = (char *)xmalloc (4 + strlen (varlist[varlist_index]));
if (first_char_loc)
{
value[0] = first_char;
if (first_char_loc == 2)
value[1] = '{';
}
strcpy (value + first_char_loc, varlist[varlist_index]);
if (first_char_loc == 2)
strcat (value, "}");
varlist_index++;
return (value);
}
}
/* How about a completion function for hostnames? */
static char *
hostname_completion_function (text, state)
const char *text;
int state;
{
static char **list = (char **)NULL;
static int list_index = 0;
static int first_char, first_char_loc;
/* If we don't have any state, make some. */
if (state == 0)
{
FREE (list);
list = (char **)NULL;
first_char_loc = 0;
first_char = *text;
if (first_char == '@')
first_char_loc++;
list = hostnames_matching ((char *)text+first_char_loc);
list_index = 0;
}
if (list && list[list_index])
{
char *t;
t = (char *)xmalloc (2 + strlen (list[list_index]));
*t = first_char;
strcpy (t + first_char_loc, list[list_index]);
list_index++;
return (t);
}
return ((char *)NULL);
}
/*
* A completion function for service names from /etc/services (or wherever).
*/
char *
bash_servicename_completion_function (text, state)
const char *text;
int state;
{
#if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT)
return ((char *)NULL);
#else
static char *sname = (char *)NULL;
static struct servent *srvent;
static int snamelen;
char *value;
char **alist, *aentry;
int afound;
if (state == 0)
{
FREE (sname);
sname = savestring (text);
snamelen = strlen (sname);
setservent (0);
}
while (srvent = getservent ())
{
afound = 0;
if (snamelen == 0 || (STREQN (sname, srvent->s_name, snamelen)))
break;
/* Not primary, check aliases */
for (alist = srvent->s_aliases; *alist; alist++)
{
aentry = *alist;
if (STREQN (sname, aentry, snamelen))
{
afound = 1;
break;
}
}
if (afound)
break;
}
if (srvent == 0)
{
endservent ();
return ((char *)NULL);
}
value = afound ? savestring (aentry) : savestring (srvent->s_name);
return value;
#endif
}
/*
* A completion function for group names from /etc/group (or wherever).
*/
char *
bash_groupname_completion_function (text, state)
const char *text;
int state;
{
#if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
return ((char *)NULL);
#else
static char *gname = (char *)NULL;
static struct group *grent;
static int gnamelen;
char *value;
if (state == 0)
{
FREE (gname);
gname = savestring (text);
gnamelen = strlen (gname);
setgrent ();
}
while (grent = getgrent ())
{
if (gnamelen == 0 || (STREQN (gname, grent->gr_name, gnamelen)))
break;
}
if (grent == 0)
{
endgrent ();
return ((char *)NULL);
}
value = savestring (grent->gr_name);
return (value);
#endif
}
/* Functions to perform history and alias expansions on the current line. */
#if defined (BANG_HISTORY)
/* Perform history expansion on the current line. If no history expansion
is done, pre_process_line() returns what it was passed, so we need to
allocate a new line here. */
static char *
history_expand_line_internal (line)
char *line;
{
char *new_line;
int old_verify;
old_verify = hist_verify;
hist_verify = 0;
new_line = pre_process_line (line, 0, 0);
hist_verify = old_verify;
return (new_line == line) ? savestring (line) : new_line;
}
#endif
/* There was an error in expansion. Let the preprocessor print
the error here. */
static void
cleanup_expansion_error ()
{
char *to_free;
#if defined (BANG_HISTORY)
int old_verify;
old_verify = hist_verify;
hist_verify = 0;
#endif
fprintf (rl_outstream, "\r\n");
to_free = pre_process_line (rl_line_buffer, 1, 0);
#if defined (BANG_HISTORY)
hist_verify = old_verify;
#endif
if (to_free != rl_line_buffer)
FREE (to_free);
putc ('\r', rl_outstream);
rl_forced_update_display ();
}
/* If NEW_LINE differs from what is in the readline line buffer, add an
undo record to get from the readline line buffer contents to the new
line and make NEW_LINE the current readline line. */
static void
maybe_make_readline_line (new_line)
char *new_line;
{
if (new_line && strcmp (new_line, rl_line_buffer) != 0)
{
rl_point = rl_end;
rl_add_undo (UNDO_BEGIN, 0, 0, 0);
rl_delete_text (0, rl_point);
rl_point = rl_end = rl_mark = 0;
rl_insert_text (new_line);
rl_add_undo (UNDO_END, 0, 0, 0);
}
}
/* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
static void
set_up_new_line (new_line)
char *new_line;
{
int old_point, at_end;
old_point = rl_point;
at_end = rl_point == rl_end;
/* If the line was history and alias expanded, then make that
be one thing to undo. */
maybe_make_readline_line (new_line);
free (new_line);
/* Place rl_point where we think it should go. */
if (at_end)
rl_point = rl_end;
else if (old_point < rl_end)
{
rl_point = old_point;
if (!whitespace (rl_line_buffer[rl_point]))
rl_forward_word (1, 0);
}
}
#if defined (ALIAS)
/* Expand aliases in the current readline line. */
static int
alias_expand_line (count, ignore)
int count, ignore;
{
char *new_line;
new_line = alias_expand (rl_line_buffer);
if (new_line)
{
set_up_new_line (new_line);
return (0);
}
else
{
cleanup_expansion_error ();
return (1);
}
}
#endif
#if defined (BANG_HISTORY)
/* History expand the line. */
static int
history_expand_line (count, ignore)
int count, ignore;
{
char *new_line;
new_line = history_expand_line_internal (rl_line_buffer);
if (new_line)
{
set_up_new_line (new_line);
return (0);
}
else
{
cleanup_expansion_error ();
return (1);
}
}
/* Expand history substitutions in the current line and then insert a
space (hopefully close to where we were before). */
static int
tcsh_magic_space (count, ignore)
int count, ignore;
{
int dist_from_end, old_point;
old_point = rl_point;
dist_from_end = rl_end - rl_point;
if (history_expand_line (count, ignore) == 0)
{
/* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
This works if all expansions were before rl_point or if no expansions
were performed. */
rl_point = (old_point == 0) ? old_point : rl_end - dist_from_end;
rl_insert (1, ' ');
return (0);
}
else
return (1);
}
#endif /* BANG_HISTORY */
/* History and alias expand the line. */
static int
history_and_alias_expand_line (count, ignore)
int count, ignore;
{
char *new_line;
new_line = 0;
#if defined (BANG_HISTORY)
new_line = history_expand_line_internal (rl_line_buffer);
#endif
#if defined (ALIAS)
if (new_line)
{
char *alias_line;
alias_line = alias_expand (new_line);
free (new_line);
new_line = alias_line;
}
#endif /* ALIAS */
if (new_line)
{
set_up_new_line (new_line);
return (0);
}
else
{
cleanup_expansion_error ();
return (1);
}
}
/* History and alias expand the line, then perform the shell word
expansions by calling expand_string. This can't use set_up_new_line()
because we want the variable expansions as a separate undo'able
set of operations. */
static int
shell_expand_line (count, ignore)
int count, ignore;
{
char *new_line;
WORD_LIST *expanded_string;
WORD_DESC *w;
new_line = 0;
#if defined (BANG_HISTORY)
new_line = history_expand_line_internal (rl_line_buffer);
#endif
#if defined (ALIAS)
if (new_line)
{
char *alias_line;
alias_line = alias_expand (new_line);
free (new_line);
new_line = alias_line;
}
#endif /* ALIAS */
if (new_line)
{
int old_point = rl_point;
int at_end = rl_point == rl_end;
/* If the line was history and alias expanded, then make that
be one thing to undo. */
maybe_make_readline_line (new_line);
free (new_line);
/* If there is variable expansion to perform, do that as a separate
operation to be undone. */
#if 1
w = alloc_word_desc ();
w->word = savestring (rl_line_buffer);
w->flags = rl_explicit_arg ? (W_NOPROCSUB|W_NOCOMSUB) : 0;
expanded_string = expand_word (w, rl_explicit_arg ? Q_HERE_DOCUMENT : 0);
dispose_word (w);
#else
new_line = savestring (rl_line_buffer);
expanded_string = expand_string (new_line, 0);
FREE (new_line);
#endif
if (expanded_string == 0)
{
new_line = (char *)xmalloc (1);
new_line[0] = '\0';
}
else
{
new_line = string_list (expanded_string);
dispose_words (expanded_string);
}
maybe_make_readline_line (new_line);
free (new_line);
/* Place rl_point where we think it should go. */
if (at_end)
rl_point = rl_end;
else if (old_point < rl_end)
{
rl_point = old_point;
if (!whitespace (rl_line_buffer[rl_point]))
rl_forward_word (1, 0);
}
return 0;
}
else
{
cleanup_expansion_error ();
return 1;
}
}
/* If FIGNORE is set, then don't match files with the given suffixes when
completing filenames. If only one of the possibilities has an acceptable
suffix, delete the others, else just return and let the completer
signal an error. It is called by the completer when real
completions are done on filenames by the completer's internal
function, not for completion lists (M-?) and not on "other"
completion types, such as hostnames or commands. */
static struct ignorevar fignore =
{
"FIGNORE",
(struct ign *)0,
0,
(char *)0,
(sh_iv_item_func_t *) 0,
};
static void
_ignore_completion_names (names, name_func)
char **names;
sh_ignore_func_t *name_func;
{
char **newnames;
int idx, nidx;
char **oldnames;
int oidx;
/* If there is only one completion, see if it is acceptable. If it is
not, free it up. In any case, short-circuit and return. This is a
special case because names[0] is not the prefix of the list of names
if there is only one completion; it is the completion itself. */
if (names[1] == (char *)0)
{
if (force_fignore)
if ((*name_func) (names[0]) == 0)
{
free (names[0]);
names[0] = (char *)NULL;
}
return;
}
/* Allocate space for array to hold list of pointers to matching
filenames. The pointers are copied back to NAMES when done. */
for (nidx = 1; names[nidx]; nidx++)
;
newnames = strvec_create (nidx + 1);
if (force_fignore == 0)
{
oldnames = strvec_create (nidx - 1);
oidx = 0;
}
newnames[0] = names[0];
for (idx = nidx = 1; names[idx]; idx++)
{
if ((*name_func) (names[idx]))
newnames[nidx++] = names[idx];
else if (force_fignore == 0)
oldnames[oidx++] = names[idx];
else
free (names[idx]);
}
newnames[nidx] = (char *)NULL;
/* If none are acceptable then let the completer handle it. */
if (nidx == 1)
{
if (force_fignore)
{
free (names[0]);
names[0] = (char *)NULL;
}
else
free (oldnames);
free (newnames);
return;
}
if (force_fignore == 0)
{
while (oidx)
free (oldnames[--oidx]);
free (oldnames);
}
/* If only one is acceptable, copy it to names[0] and return. */
if (nidx == 2)
{
free (names[0]);
names[0] = newnames[1];
names[1] = (char *)NULL;
free (newnames);
return;
}
/* Copy the acceptable names back to NAMES, set the new array end,
and return. */
for (nidx = 1; newnames[nidx]; nidx++)
names[nidx] = newnames[nidx];
names[nidx] = (char *)NULL;
free (newnames);
}
static int
name_is_acceptable (name)
const char *name;
{
struct ign *p;
int nlen;
for (nlen = strlen (name), p = fignore.ignores; p->val; p++)
{
if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len]))
return (0);
}
return (1);
}
#if 0
static int
ignore_dot_names (name)
char *name;
{
return (name[0] != '.');
}
#endif
static int
filename_completion_ignore (names)
char **names;
{
#if 0
if (glob_dot_filenames == 0)
_ignore_completion_names (names, ignore_dot_names);
#endif
setup_ignore_patterns (&fignore);
if (fignore.num_ignores == 0)
return 0;
_ignore_completion_names (names, name_is_acceptable);
return 0;
}
/* Return 1 if NAME is a directory. NAME undergoes tilde expansion. */
static int
test_for_directory (name)
const char *name;
{
char *fn;
int r;
fn = bash_tilde_expand (name, 0);
r = file_isdir (fn);
free (fn);
return (r);
}
static int
test_for_canon_directory (name)
const char *name;
{
char *fn;
int r;
fn = (*name == '~') ? bash_tilde_expand (name, 0) : savestring (name);
bash_filename_stat_hook (&fn);
r = file_isdir (fn);
free (fn);
return (r);
}
/* Remove files from NAMES, leaving directories. */
static int
bash_ignore_filenames (names)
char **names;
{
_ignore_completion_names (names, test_for_directory);
return 0;
}
static int
bash_progcomp_ignore_filenames (names)
char **names;
{
_ignore_completion_names (names, test_for_canon_directory);
return 0;
}
static int
return_zero (name)
const char *name;
{
return 0;
}
static int
bash_ignore_everything (names)
char **names;
{
_ignore_completion_names (names, return_zero);
return 0;
}
/* Replace a tilde-prefix in VAL with a `~', assuming the user typed it. VAL
is an expanded filename. DIRECTORY_PART is the tilde-prefix portion
of the un-tilde-expanded version of VAL (what the user typed). */
static char *
restore_tilde (val, directory_part)
char *val, *directory_part;
{
int l, vl, dl2, xl;
char *dh2, *expdir, *ret, *v;
vl = strlen (val);
/* We need to duplicate the expansions readline performs on the directory
portion before passing it to our completion function. */
dh2 = directory_part ? bash_dequote_filename (directory_part, 0) : 0;
bash_directory_expansion (&dh2);
dl2 = strlen (dh2);
expdir = bash_tilde_expand (directory_part, 0);
xl = strlen (expdir);
if (*directory_part == '~' && STREQ (directory_part, expdir))
{
/* tilde expansion failed, so what should we return? we use what the
user typed. */
v = mbschr (val, '/');
vl = STRLEN (v);
ret = (char *)xmalloc (xl + vl + 2);
strcpy (ret, directory_part);
if (v && *v)
strcpy (ret + xl, v);
free (dh2);
free (expdir);
return ret;
}
free (expdir);
/*
dh2 = unexpanded but dequoted tilde-prefix
dl2 = length of tilde-prefix
expdir = tilde-expanded tilde-prefix
xl = length of expanded tilde-prefix
l = length of remainder after tilde-prefix
*/
l = (vl - xl) + 1;
if (l <= 0)
{
free (dh2);
return (savestring (val)); /* XXX - just punt */
}
ret = (char *)xmalloc (dl2 + 2 + l);
strcpy (ret, dh2);
strcpy (ret + dl2, val + xl);
free (dh2);
return (ret);
}
static char *
maybe_restore_tilde (val, directory_part)
char *val, *directory_part;
{
rl_icppfunc_t *save;
char *ret;
save = (dircomplete_expand == 0) ? save_directory_hook () : (rl_icppfunc_t *)0;
ret = restore_tilde (val, directory_part);
if (save)
restore_directory_hook (save);
return ret;
}
/* Simulate the expansions that will be performed by
rl_filename_completion_function. This must be called with the address of
a pointer to malloc'd memory. */
static void
bash_directory_expansion (dirname)
char **dirname;
{
char *d, *nd;
d = savestring (*dirname);
if ((rl_directory_rewrite_hook) && (*rl_directory_rewrite_hook) (&d))
{
free (*dirname);
*dirname = d;
}
else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&d))
{
free (*dirname);
*dirname = d;
}
else if (rl_completion_found_quote)
{
nd = bash_dequote_filename (d, rl_completion_quote_character);
free (*dirname);
free (d);
*dirname = nd;
}
}
/* If necessary, rewrite directory entry */
static char *
bash_filename_rewrite_hook (fname, fnlen)
char *fname;
int fnlen;
{
char *conv;
conv = fnx_fromfs (fname, fnlen);
if (conv != fname)
conv = savestring (conv);
return conv;
}
/* Functions to save and restore the appropriate directory hook */
/* This is not static so the shopt code can call it */
void
set_directory_hook ()
{
if (dircomplete_expand)
{
rl_directory_completion_hook = bash_directory_completion_hook;
rl_directory_rewrite_hook = (rl_icppfunc_t *)0;
}
else
{
rl_directory_rewrite_hook = bash_directory_completion_hook;
rl_directory_completion_hook = (rl_icppfunc_t *)0;
}
}
static rl_icppfunc_t *
save_directory_hook ()
{
rl_icppfunc_t *ret;
if (dircomplete_expand)
{
ret = rl_directory_completion_hook;
rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
}
else
{
ret = rl_directory_rewrite_hook;
rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
}
return ret;
}
static void
restore_directory_hook (hookf)
rl_icppfunc_t *hookf;
{
if (dircomplete_expand)
rl_directory_completion_hook = hookf;
else
rl_directory_rewrite_hook = hookf;
}
/* Check whether not DIRNAME, with any trailing slash removed, exists. If
SHOULD_DEQUOTE is non-zero, we dequote the directory name first. */
static int
directory_exists (dirname, should_dequote)
const char *dirname;
int should_dequote;
{
char *new_dirname;
int dirlen, r;
struct stat sb;
/* We save the string and chop the trailing slash because stat/lstat behave
inconsistently if one is present. */
new_dirname = should_dequote ? bash_dequote_filename ((char *)dirname, rl_completion_quote_character) : savestring (dirname);
dirlen = STRLEN (new_dirname);
if (new_dirname[dirlen - 1] == '/')
new_dirname[dirlen - 1] = '\0';
#if defined (HAVE_LSTAT)
r = lstat (new_dirname, &sb) == 0;
#else
r = stat (new_dirname, &sb) == 0;
#endif
free (new_dirname);
return (r);
}
/* Expand a filename before the readline completion code passes it to stat(2).
The filename will already have had tilde expansion performed. */
static int
bash_filename_stat_hook (dirname)
char **dirname;
{
char *local_dirname, *new_dirname, *t;
int should_expand_dirname, return_value;
int global_nounset;
WORD_LIST *wl;
local_dirname = *dirname;
should_expand_dirname = return_value = 0;
if (t = mbschr (local_dirname, '$'))
should_expand_dirname = '$';
else if (t = mbschr (local_dirname, '`')) /* XXX */
should_expand_dirname = '`';
if (should_expand_dirname && directory_exists (local_dirname, 0))
should_expand_dirname = 0;
if (should_expand_dirname)
{
new_dirname = savestring (local_dirname);
/* no error messages, and expand_prompt_string doesn't longjmp so we don't
have to worry about restoring this setting. */
global_nounset = unbound_vars_is_error;
unbound_vars_is_error = 0;
wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB|W_NOPROCSUB|W_COMPLETE); /* does the right thing */
unbound_vars_is_error = global_nounset;
if (wl)
{
free (new_dirname);
new_dirname = string_list (wl);
/* Tell the completer we actually expanded something and change
*dirname only if we expanded to something non-null -- stat
behaves unpredictably when passed null or empty strings */
if (new_dirname && *new_dirname)
{
free (local_dirname); /* XXX */
local_dirname = *dirname = new_dirname;
return_value = STREQ (local_dirname, *dirname) == 0;
}
else
free (new_dirname);
dispose_words (wl);
}
else
free (new_dirname);
}
/* This is very similar to the code in bash_directory_completion_hook below,
but without spelling correction and not worrying about whether or not
we change relative pathnames. */
if (no_symbolic_links == 0 && (local_dirname[0] != '.' || local_dirname[1]))
{
char *temp1, *temp2;
t = get_working_directory ("symlink-hook");
temp1 = make_absolute (local_dirname, t);
free (t);
temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
/* If we can't canonicalize, bail. */
if (temp2 == 0)
{
free (temp1);
return return_value;
}
free (local_dirname);
*dirname = temp2;
free (temp1);
}
return (return_value);
}
/* Handle symbolic link references and other directory name
expansions while hacking completion. This should return 1 if it modifies
the DIRNAME argument, 0 otherwise. It should make sure not to modify
DIRNAME if it returns 0. */
static int
bash_directory_completion_hook (dirname)
char **dirname;
{
char *local_dirname, *new_dirname, *t;
int return_value, should_expand_dirname, nextch, closer;
WORD_LIST *wl;
return_value = should_expand_dirname = nextch = closer = 0;
local_dirname = *dirname;
if (t = mbschr (local_dirname, '$'))
{
should_expand_dirname = '$';
nextch = t[1];
/* Deliberately does not handle the deprecated $[...] arithmetic
expansion syntax */
if (nextch == '(')
closer = ')';
else if (nextch == '{')
closer = '}';
else
nextch = 0;
if (closer)
{
int p;
char delims[2];
delims[0] = closer; delims[1] = 0;
p = skip_to_delim (t, 1, delims, SD_NOJMP|SD_COMPLETE);
if (t[p] != closer)
should_expand_dirname = 0;
}
}
else if (local_dirname[0] == '~')
should_expand_dirname = '~';
else
{
t = mbschr (local_dirname, '`');
if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0)
should_expand_dirname = '`';
}
if (should_expand_dirname && directory_exists (local_dirname, 1))
should_expand_dirname = 0;
if (should_expand_dirname)
{
new_dirname = savestring (local_dirname);
wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB|W_NOPROCSUB|W_COMPLETE); /* does the right thing */
if (wl)
{
*dirname = string_list (wl);
/* Tell the completer to replace the directory name only if we
actually expanded something. */
return_value = STREQ (local_dirname, *dirname) == 0;
free (local_dirname);
free (new_dirname);
dispose_words (wl);
local_dirname = *dirname;
/* XXX - change rl_filename_quote_characters here based on
should_expand_dirname/nextch/closer. This is the only place
custom_filename_quote_characters is modified. */
if (rl_filename_quote_characters && *rl_filename_quote_characters)
{
int i, j, c;
i = strlen (default_filename_quote_characters);
custom_filename_quote_characters = xrealloc (custom_filename_quote_characters, i+1);
for (i = j = 0; c = default_filename_quote_characters[i]; i++)
{
if (c == should_expand_dirname || c == nextch || c == closer)
continue;
custom_filename_quote_characters[j++] = c;
}
custom_filename_quote_characters[j] = '\0';
rl_filename_quote_characters = custom_filename_quote_characters;
set_filename_bstab (rl_filename_quote_characters);
}
}
else
{
free (new_dirname);
free (local_dirname);
*dirname = (char *)xmalloc (1);
**dirname = '\0';
return 1;
}
}
else
{
/* Dequote the filename even if we don't expand it. */
new_dirname = bash_dequote_filename (local_dirname, rl_completion_quote_character);
return_value = STREQ (local_dirname, new_dirname) == 0;
free (local_dirname);
local_dirname = *dirname = new_dirname;
}
/* no_symbolic_links == 0 -> use (default) logical view of the file system.
local_dirname[0] == '.' && local_dirname[1] == '/' means files in the
current directory (./).
local_dirname[0] == '.' && local_dirname[1] == 0 means relative pathnames
in the current directory (e.g., lib/sh).
XXX - should we do spelling correction on these? */
/* This is test as it was in bash-4.2: skip relative pathnames in current
directory. Change test to
(local_dirname[0] != '.' || (local_dirname[1] && local_dirname[1] != '/'))
if we want to skip paths beginning with ./ also. */
if (no_symbolic_links == 0 && (local_dirname[0] != '.' || local_dirname[1]))
{
char *temp1, *temp2;
int len1, len2;
/* If we have a relative path
(local_dirname[0] != '/' && local_dirname[0] != '.')
that is canonical after appending it to the current directory, then
temp1 = temp2+'/'
That is,
strcmp (temp1, temp2) == 0
after adding a slash to temp2 below. It should be safe to not
change those.
*/
t = get_working_directory ("symlink-hook");
temp1 = make_absolute (local_dirname, t);
free (t);
temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
/* Try spelling correction if initial canonicalization fails. Make
sure we are set to replace the directory name with the results so
subsequent directory checks don't fail. */
if (temp2 == 0 && dircomplete_spelling && dircomplete_expand)
{
temp2 = dirspell (temp1);
if (temp2)
{
free (temp1);
temp1 = temp2;
temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
return_value |= temp2 != 0;
}
}
/* If we can't canonicalize, bail. */
if (temp2 == 0)
{
free (temp1);
return return_value;
}
len1 = strlen (temp1);
if (temp1[len1 - 1] == '/')
{
len2 = strlen (temp2);
if (len2 > 2) /* don't append `/' to `/' or `//' */
{
temp2 = (char *)xrealloc (temp2, len2 + 2);
temp2[len2] = '/';
temp2[len2 + 1] = '\0';
}
}
/* dircomplete_expand_relpath == 0 means we want to leave relative
pathnames that are unchanged by canonicalization alone.
*local_dirname != '/' && *local_dirname != '.' == relative pathname
(consistent with general.c:absolute_pathname())
temp1 == temp2 (after appending a slash to temp2) means the pathname
is not changed by canonicalization as described above. */
if (dircomplete_expand_relpath || ((local_dirname[0] != '/' && local_dirname[0] != '.') && STREQ (temp1, temp2) == 0))
return_value |= STREQ (local_dirname, temp2) == 0;
free (local_dirname);
*dirname = temp2;
free (temp1);
}
return (return_value);
}
static char **history_completion_array = (char **)NULL;
static int harry_size;
static int harry_len;
static void
build_history_completion_array ()
{
register int i, j;
HIST_ENTRY **hlist;
char **tokens;
/* First, clear out the current dynamic history completion list. */
if (harry_size)
{
strvec_dispose (history_completion_array);
history_completion_array = (char **)NULL;
harry_size = 0;
harry_len = 0;
}
/* Next, grovel each line of history, making each shell-sized token
a separate entry in the history_completion_array. */
hlist = history_list ();
if (hlist)
{
for (i = 0; hlist[i]; i++)
;
for ( --i; i >= 0; i--)
{
/* Separate each token, and place into an array. */
tokens = history_tokenize (hlist[i]->line);
for (j = 0; tokens && tokens[j]; j++)
{
if (harry_len + 2 > harry_size)
history_completion_array = strvec_resize (history_completion_array, harry_size += 10);
history_completion_array[harry_len++] = tokens[j];
history_completion_array[harry_len] = (char *)NULL;
}
free (tokens);
}
/* Sort the complete list of tokens. */
if (dabbrev_expand_active == 0)
qsort (history_completion_array, harry_len, sizeof (char *), (QSFUNC *)strvec_strcmp);
}
}
static char *
history_completion_generator (hint_text, state)
const char *hint_text;
int state;
{
static int local_index, len;
static const char *text;
/* If this is the first call to the generator, then initialize the
list of strings to complete over. */
if (state == 0)
{
if (dabbrev_expand_active) /* This is kind of messy */
rl_completion_suppress_append = 1;
local_index = 0;
build_history_completion_array ();
text = hint_text;
len = strlen (text);
}
while (history_completion_array && history_completion_array[local_index])
{
/* XXX - should this use completion-ignore-case? */
if (strncmp (text, history_completion_array[local_index++], len) == 0)
return (savestring (history_completion_array[local_index - 1]));
}
return ((char *)NULL);
}
static int
dynamic_complete_history (count, key)
int count, key;
{
int r;
rl_compentry_func_t *orig_func;
rl_completion_func_t *orig_attempt_func;
rl_compignore_func_t *orig_ignore_func;
orig_func = rl_completion_entry_function;
orig_attempt_func = rl_attempted_completion_function;
orig_ignore_func = rl_ignore_some_completions_function;
rl_completion_entry_function = history_completion_generator;
rl_attempted_completion_function = (rl_completion_func_t *)NULL;
rl_ignore_some_completions_function = filename_completion_ignore;
/* XXX - use rl_completion_mode here? */
if (rl_last_func == dynamic_complete_history)
r = rl_complete_internal ('?');
else
r = rl_complete_internal (TAB);
rl_completion_entry_function = orig_func;
rl_attempted_completion_function = orig_attempt_func;
rl_ignore_some_completions_function = orig_ignore_func;
return r;
}
static int
bash_dabbrev_expand (count, key)
int count, key;
{
int r, orig_suppress, orig_sort;
rl_compentry_func_t *orig_func;
rl_completion_func_t *orig_attempt_func;
rl_compignore_func_t *orig_ignore_func;
orig_func = rl_menu_completion_entry_function;
orig_attempt_func = rl_attempted_completion_function;
orig_ignore_func = rl_ignore_some_completions_function;
orig_suppress = rl_completion_suppress_append;
orig_sort = rl_sort_completion_matches;
rl_menu_completion_entry_function = history_completion_generator;
rl_attempted_completion_function = (rl_completion_func_t *)NULL;
rl_ignore_some_completions_function = filename_completion_ignore;
rl_filename_completion_desired = 0;
rl_completion_suppress_append = 1;
rl_sort_completion_matches = 0;
/* XXX - use rl_completion_mode here? */
dabbrev_expand_active = 1;
if (rl_last_func == bash_dabbrev_expand)
rl_last_func = rl_menu_complete;
r = rl_menu_complete (count, key);
dabbrev_expand_active = 0;
rl_last_func = bash_dabbrev_expand;
rl_menu_completion_entry_function = orig_func;
rl_attempted_completion_function = orig_attempt_func;
rl_ignore_some_completions_function = orig_ignore_func;
rl_completion_suppress_append = orig_suppress;
rl_sort_completion_matches = orig_sort;
return r;
}
#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
static int
bash_complete_username (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_username_internal (rl_completion_mode (bash_complete_username));
}
static int
bash_possible_username_completions (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_username_internal ('?');
}
static int
bash_complete_username_internal (what_to_do)
int what_to_do;
{
return bash_specific_completion (what_to_do, rl_username_completion_function);
}
static int
bash_complete_filename (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename));
}
static int
bash_possible_filename_completions (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_filename_internal ('?');
}
static int
bash_complete_filename_internal (what_to_do)
int what_to_do;
{
rl_compentry_func_t *orig_func;
rl_completion_func_t *orig_attempt_func;
rl_icppfunc_t *orig_dir_func;
rl_compignore_func_t *orig_ignore_func;
/*const*/ char *orig_rl_completer_word_break_characters;
int r;
orig_func = rl_completion_entry_function;
orig_attempt_func = rl_attempted_completion_function;
orig_ignore_func = rl_ignore_some_completions_function;
orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
orig_dir_func = save_directory_hook ();
rl_completion_entry_function = rl_filename_completion_function;
rl_attempted_completion_function = (rl_completion_func_t *)NULL;
rl_ignore_some_completions_function = filename_completion_ignore;
rl_completer_word_break_characters = " \t\n\"\'";
r = rl_complete_internal (what_to_do);
rl_completion_entry_function = orig_func;
rl_attempted_completion_function = orig_attempt_func;
rl_ignore_some_completions_function = orig_ignore_func;
rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
restore_directory_hook (orig_dir_func);
return r;
}
static int
bash_complete_hostname (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname));
}
static int
bash_possible_hostname_completions (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_hostname_internal ('?');
}
static int
bash_complete_variable (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable));
}
static int
bash_possible_variable_completions (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_variable_internal ('?');
}
static int
bash_complete_command (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_command_internal (rl_completion_mode (bash_complete_command));
}
static int
bash_possible_command_completions (ignore, ignore2)
int ignore, ignore2;
{
return bash_complete_command_internal ('?');
}
static int
bash_complete_hostname_internal (what_to_do)
int what_to_do;
{
return bash_specific_completion (what_to_do, hostname_completion_function);
}
static int
bash_complete_variable_internal (what_to_do)
int what_to_do;
{
return bash_specific_completion (what_to_do, variable_completion_function);
}
static int
bash_complete_command_internal (what_to_do)
int what_to_do;
{
return bash_specific_completion (what_to_do, command_word_completion_function);
}
static int
completion_glob_pattern (string)
char *string;
{
return (glob_pattern_p (string) == 1);
}
static char *globtext;
static char *globorig;
static char *
glob_complete_word (text, state)
const char *text;
int state;
{
static char **matches = (char **)NULL;
static int ind;
int glen;
char *ret, *ttext;
if (state == 0)
{
rl_filename_completion_desired = 1;
FREE (matches);
if (globorig != globtext)
FREE (globorig);
FREE (globtext);
ttext = bash_tilde_expand (text, 0);
if (rl_explicit_arg)
{
globorig = savestring (ttext);
glen = strlen (ttext);
globtext = (char *)xmalloc (glen + 2);
strcpy (globtext, ttext);
globtext[glen] = '*';
globtext[glen+1] = '\0';
}
else
globtext = globorig = savestring (ttext);
if (ttext != text)
free (ttext);
matches = shell_glob_filename (globtext, 0);
if (GLOB_FAILED (matches))
matches = (char **)NULL;
ind = 0;
}
ret = matches ? matches[ind] : (char *)NULL;
ind++;
return ret;
}
static int
bash_glob_completion_internal (what_to_do)
int what_to_do;
{
return bash_specific_completion (what_to_do, glob_complete_word);
}
/* A special quoting function so we don't end up quoting globbing characters
in the word if there are no matches or multiple matches. */
static char *
bash_glob_quote_filename (s, rtype, qcp)
char *s;
int rtype;
char *qcp;
{
if (globorig && qcp && *qcp == '\0' && STREQ (s, globorig))
return (savestring (s));
else
return (bash_quote_filename (s, rtype, qcp));
}
static int
bash_glob_complete_word (count, key)
int count, key;
{
int r;
rl_quote_func_t *orig_quoting_function;
if (rl_editing_mode == EMACS_EDITING_MODE)
rl_explicit_arg = 1; /* force `*' append */
orig_quoting_function = rl_filename_quoting_function;
rl_filename_quoting_function = bash_glob_quote_filename;
r = bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word));
rl_filename_quoting_function = orig_quoting_function;
return r;
}
static int
bash_glob_expand_word (count, key)
int count, key;
{
return bash_glob_completion_internal ('*');
}
static int
bash_glob_list_expansions (count, key)
int count, key;
{
return bash_glob_completion_internal ('?');
}
static int
bash_specific_completion (what_to_do, generator)
int what_to_do;
rl_compentry_func_t *generator;
{
rl_compentry_func_t *orig_func;
rl_completion_func_t *orig_attempt_func;
rl_compignore_func_t *orig_ignore_func;
int r;
orig_func = rl_completion_entry_function;
orig_attempt_func = rl_attempted_completion_function;
orig_ignore_func = rl_ignore_some_completions_function;
rl_completion_entry_function = generator;
rl_attempted_completion_function = NULL;
rl_ignore_some_completions_function = orig_ignore_func;
r = rl_complete_internal (what_to_do);
rl_completion_entry_function = orig_func;
rl_attempted_completion_function = orig_attempt_func;
rl_ignore_some_completions_function = orig_ignore_func;
return r;
}
#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
#if defined (VI_MODE)
/* Completion, from vi mode's point of view. This is a modified version of
rl_vi_complete which uses the bash globbing code to implement what POSIX
specifies, which is to append a `*' and attempt filename generation (which
has the side effect of expanding any globbing characters in the word). */
static int
bash_vi_complete (count, key)
int count, key;
{
#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
int p, r;
char *t;
if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
{
if (!whitespace (rl_line_buffer[rl_point + 1]))
rl_vi_end_word (1, 'E');
rl_point++;
}
/* Find boundaries of current word, according to vi definition of a
`bigword'. */
t = 0;
if (rl_point > 0)
{
p = rl_point;
rl_vi_bWord (1, 'B');
r = rl_point;
rl_point = p;
p = r;
t = substring (rl_line_buffer, p, rl_point);
}
if (t && completion_glob_pattern (t) == 0)
rl_explicit_arg = 1; /* XXX - force glob_complete_word to append `*' */
FREE (t);
if (key == '*') /* Expansion and replacement. */
r = bash_glob_expand_word (count, key);
else if (key == '=') /* List possible completions. */
r = bash_glob_list_expansions (count, key);
else if (key == '\\') /* Standard completion */
r = bash_glob_complete_word (count, key);
else
r = rl_complete (0, key);
if (key == '*' || key == '\\')
rl_vi_start_inserting (key, 1, 1);
return (r);
#else
return rl_vi_complete (count, key);
#endif /* !SPECIFIC_COMPLETION_FUNCTIONS */
}
#endif /* VI_MODE */
/* Filename quoting for completion. */
/* A function to strip unquoted quote characters (single quotes, double
quotes, and backslashes). It allows single quotes to appear
within double quotes, and vice versa. It should be smarter. */
static char *
bash_dequote_filename (text, quote_char)
char *text;
int quote_char;
{
char *ret, *p, *r;
int l, quoted;
l = strlen (text);
ret = (char *)xmalloc (l + 1);
for (quoted = quote_char, p = text, r = ret; p && *p; p++)
{
/* Allow backslash-escaped characters to pass through unscathed. */
if (*p == '\\')
{
/* Backslashes are preserved within single quotes. */
if (quoted == '\'')
*r++ = *p;
/* Backslashes are preserved within double quotes unless the
character is one that is defined to be escaped */
else if (quoted == '"' && ((sh_syntaxtab[(unsigned char)p[1]] & CBSDQUOTE) == 0))
*r++ = *p;
*r++ = *++p;
if (*p == '\0')
return ret; /* XXX - was break; */
continue;
}
/* Close quote. */
if (quoted && *p == quoted)
{
quoted = 0;
continue;
}
/* Open quote. */
if (quoted == 0 && (*p == '\'' || *p == '"'))
{
quoted = *p;
continue;
}
*r++ = *p;
}
*r = '\0';
return ret;
}
/* Quote characters that the readline completion code would treat as
word break characters with backslashes. Pass backslash-quoted
characters through without examination. */
static char *
quote_word_break_chars (text)
char *text;
{
char *ret, *r, *s;
int l;
l = strlen (text);
ret = (char *)xmalloc ((2 * l) + 1);
for (s = text, r = ret; *s; s++)
{
/* Pass backslash-quoted characters through, including the backslash. */
if (*s == '\\')
{
*r++ = '\\';
*r++ = *++s;
if (*s == '\0')
break;
continue;
}
/* OK, we have an unquoted character. Check its presence in
rl_completer_word_break_characters. */
if (mbschr (rl_completer_word_break_characters, *s))
*r++ = '\\';
/* XXX -- check for standalone tildes here and backslash-quote them */
if (s == text && *s == '~' && file_exists (text))
*r++ = '\\';
*r++ = *s;
}
*r = '\0';
return ret;
}
/* Use characters in STRING to populate the table of characters that should
be backslash-quoted. The table will be used for sh_backslash_quote from
this file. */
static void
set_filename_bstab (string)
const char *string;
{
const char *s;
memset (filename_bstab, 0, sizeof (filename_bstab));
for (s = string; s && *s; s++)
filename_bstab[(unsigned char)*s] = 1;
}
/* Quote a filename using double quotes, single quotes, or backslashes
depending on the value of completion_quoting_style. If we're
completing using backslashes, we need to quote some additional
characters (those that readline treats as word breaks), so we call
quote_word_break_chars on the result. This returns newly-allocated
memory. */
static char *
bash_quote_filename (s, rtype, qcp)
char *s;
int rtype;
char *qcp;
{
char *rtext, *mtext, *ret;
int rlen, cs;
rtext = (char *)NULL;
/* If RTYPE == MULT_MATCH, it means that there is
more than one match. In this case, we do not add
the closing quote or attempt to perform tilde
expansion. If RTYPE == SINGLE_MATCH, we try
to perform tilde expansion, because single and double
quotes inhibit tilde expansion by the shell. */
cs = completion_quoting_style;
/* Might need to modify the default completion style based on *qcp,
since it's set to any user-provided opening quote. We also change
to single-quoting if there is no user-provided opening quote and
the word being completed contains newlines, since those are not
quoted correctly using backslashes (a backslash-newline pair is
special to the shell parser). */
if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && mbschr (s, '\n'))
cs = COMPLETE_SQUOTE;
else if (*qcp == '"')
cs = COMPLETE_DQUOTE;
else if (*qcp == '\'')
cs = COMPLETE_SQUOTE;
#if defined (BANG_HISTORY)
else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE &&
history_expansion_inhibited == 0 && mbschr (s, '!'))
cs = COMPLETE_BSQUOTE;
if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE &&
history_expansion_inhibited == 0 && mbschr (s, '!'))
{
cs = COMPLETE_BSQUOTE;
*qcp = '\0';
}
#endif
/* Don't tilde-expand backslash-quoted filenames, since only single and
double quotes inhibit tilde expansion. */
mtext = s;
if (mtext[0] == '~' && rtype == SINGLE_MATCH && cs != COMPLETE_BSQUOTE)
mtext = bash_tilde_expand (s, 0);
switch (cs)
{
case COMPLETE_DQUOTE:
rtext = sh_double_quote (mtext);
break;
case COMPLETE_SQUOTE:
rtext = sh_single_quote (mtext);
break;
case COMPLETE_BSQUOTE:
rtext = sh_backslash_quote (mtext, complete_fullquote ? 0 : filename_bstab, 0);
break;
}
if (mtext != s)
free (mtext);
/* We may need to quote additional characters: those that readline treats
as word breaks that are not quoted by backslash_quote. */
if (rtext && cs == COMPLETE_BSQUOTE)
{
mtext = quote_word_break_chars (rtext);
free (rtext);
rtext = mtext;
}
/* Leave the opening quote intact. The readline completion code takes
care of avoiding doubled opening quotes. */
if (rtext)
{
rlen = strlen (rtext);
ret = (char *)xmalloc (rlen + 1);
strcpy (ret, rtext);
}
else
{
ret = (char *)xmalloc (rlen = 1);
ret[0] = '\0';
}
/* If there are multiple matches, cut off the closing quote. */
if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE)
ret[rlen - 1] = '\0';
free (rtext);
return ret;
}
/* Support for binding readline key sequences to Unix commands. Each editing
mode has a separate Unix command keymap. */
static Keymap emacs_std_cmd_xmap;
#if defined (VI_MODE)
static Keymap vi_insert_cmd_xmap;
static Keymap vi_movement_cmd_xmap;
#endif
#ifdef _MINIX
static void
#else
static int
#endif
putx(c)
int c;
{
int x;
x = putc (c, rl_outstream);
#ifndef _MINIX
return x;
#endif
}
static int
readline_get_char_offset (ind)
int ind;
{
int r, old_ch;
r = ind;
#if defined (HANDLE_MULTIBYTE)
if (locale_mb_cur_max > 1)
{
old_ch = rl_line_buffer[ind];
rl_line_buffer[ind] = '\0';
r = MB_STRLEN (rl_line_buffer);
rl_line_buffer[ind] = old_ch;
}
#endif
return r;
}
static void
readline_set_char_offset (ind, varp)
int ind;
int *varp;
{
int i;
i = ind;
#if defined (HANDLE_MULTIBYTE)
if (i > 0 && locale_mb_cur_max > 1)
i = _rl_find_next_mbchar (rl_line_buffer, 0, i, 0); /* XXX */
#endif
if (i != *varp)
{
if (i > rl_end)
i = rl_end;
else if (i < 0)
i = 0;
*varp = i;
}
}
int
bash_execute_unix_command (count, key)
int count; /* ignored */
int key;
{
int type;
register int i, r;
intmax_t mi;
sh_parser_state_t ps;
char *cmd, *value, *ce, old_ch;
SHELL_VAR *v;
char ibuf[INT_STRLEN_BOUND(int) + 1];
Keymap cmd_xmap;
/* First, we need to find the right command to execute. This is tricky,
because we might have already indirected into another keymap, so we
have to walk cmd_xmap using the entire key sequence. */
cmd_xmap = get_cmd_xmap_from_keymap (rl_get_keymap ());
cmd = (char *)rl_function_of_keyseq_len (rl_executing_keyseq, rl_key_sequence_length, cmd_xmap, &type);
if (type == ISKMAP && (type = ((Keymap) cmd)[ANYOTHERKEY].type) == ISMACR)
cmd = (char*)((Keymap) cmd)[ANYOTHERKEY].function;
if (cmd == 0 || type != ISMACR)
{
rl_crlf ();
internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
rl_forced_update_display ();
return 1;
}
ce = rl_get_termcap ("ce");
if (ce) /* clear current line */
{
rl_clear_visible_line ();
fflush (rl_outstream);
}
else
rl_crlf (); /* move to a new line */
v = bind_variable ("READLINE_LINE", rl_line_buffer, 0);
if (v)
VSETATTR (v, att_exported);
i = readline_get_char_offset (rl_point);
value = inttostr (i, ibuf, sizeof (ibuf));
v = bind_int_variable ("READLINE_POINT", value, 0);
if (v)
VSETATTR (v, att_exported);
i = readline_get_char_offset (rl_mark);
value = inttostr (i, ibuf, sizeof (ibuf));
v = bind_int_variable ("READLINE_MARK", value, 0);
if (v)
VSETATTR (v, att_exported);
array_needs_making = 1;
save_parser_state (&ps);
rl_clear_signals ();
r = parse_and_execute (savestring (cmd), "bash_execute_unix_command", SEVAL_NOHIST);
rl_set_signals ();
restore_parser_state (&ps);
v = find_variable ("READLINE_LINE");
maybe_make_readline_line (v ? value_cell (v) : 0);
v = find_variable ("READLINE_POINT");
if (v && legal_number (value_cell (v), &mi))
readline_set_char_offset (mi, &rl_point);
v = find_variable ("READLINE_MARK");
if (v && legal_number (value_cell (v), &mi))
readline_set_char_offset (mi, &rl_mark);
check_unbind_variable ("READLINE_LINE");
check_unbind_variable ("READLINE_POINT");
check_unbind_variable ("READLINE_MARK");
array_needs_making = 1;
/* and restore the readline buffer and display after command execution. */
/* If we clear the last line of the prompt above, redraw only that last
line. If the command returns 124, we redraw unconditionally as in
previous versions. */
if (ce && r != 124)
rl_redraw_prompt_last_line ();
else
rl_forced_update_display ();
return 0;
}
int
print_unix_command_map ()
{
Keymap save, cmd_xmap;
save = rl_get_keymap ();
cmd_xmap = get_cmd_xmap_from_keymap (save);
rl_set_keymap (cmd_xmap);
rl_macro_dumper (1);
rl_set_keymap (save);
return 0;
}
static void
init_unix_command_map ()
{
emacs_std_cmd_xmap = rl_make_bare_keymap ();
emacs_std_cmd_xmap[CTRL('X')].type = ISKMAP;
emacs_std_cmd_xmap[CTRL('X')].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap ());
emacs_std_cmd_xmap[ESC].type = ISKMAP;
emacs_std_cmd_xmap[ESC].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap ());
#if defined (VI_MODE)
vi_insert_cmd_xmap = rl_make_bare_keymap ();
vi_movement_cmd_xmap = rl_make_bare_keymap ();
#endif
}
static Keymap
get_cmd_xmap_from_edit_mode ()
{
if (emacs_std_cmd_xmap == 0)
init_unix_command_map ();
switch (rl_editing_mode)
{
case EMACS_EDITING_MODE:
return emacs_std_cmd_xmap;
#if defined (VI_MODE)
case VI_EDITING_MODE:
return (get_cmd_xmap_from_keymap (rl_get_keymap ()));
#endif
default:
return (Keymap)NULL;
}
}
static Keymap
get_cmd_xmap_from_keymap (kmap)
Keymap kmap;
{
if (emacs_std_cmd_xmap == 0)
init_unix_command_map ();
if (kmap == emacs_standard_keymap)
return emacs_std_cmd_xmap;
else if (kmap == emacs_meta_keymap)
return (FUNCTION_TO_KEYMAP (emacs_std_cmd_xmap, ESC));
else if (kmap == emacs_ctlx_keymap)
return (FUNCTION_TO_KEYMAP (emacs_std_cmd_xmap, CTRL('X')));
#if defined (VI_MODE)
else if (kmap == vi_insertion_keymap)
return vi_insert_cmd_xmap;
else if (kmap == vi_movement_keymap)
return vi_movement_cmd_xmap;
#endif
else
return (Keymap)NULL;
}
static int
isolate_sequence (string, ind, need_dquote, startp)
char *string;
int ind, need_dquote, *startp;
{
register int i;
int c, passc, delim;
for (i = ind; string[i] && whitespace (string[i]); i++)
;
/* NEED_DQUOTE means that the first non-white character *must* be `"'. */
if (need_dquote && string[i] != '"')
{
builtin_error (_("%s: first non-whitespace character is not `\"'"), string);
return -1;
}
/* We can have delimited strings even if NEED_DQUOTE == 0, like the command
string to bind the key sequence to. */
delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0;
if (startp)
*startp = delim ? ++i : i;
for (passc = 0; c = string[i]; i++)
{
if (passc)
{
passc = 0;
continue;
}
if (c == '\\')
{
passc++;
continue;
}
if (c == delim)
break;
}
if (delim && string[i] != delim)
{
builtin_error (_("no closing `%c' in %s"), delim, string);
return -1;
}
return i;
}
int
bind_keyseq_to_unix_command (line)
char *line;
{
Keymap kmap, cmd_xmap;
char *kseq, *value;
int i, kstart;
kmap = rl_get_keymap ();
/* We duplicate some of the work done by rl_parse_and_bind here, but
this code only has to handle `"keyseq": ["]command["]' and can
generate an error for anything else. */
i = isolate_sequence (line, 0, 1, &kstart);
if (i < 0)
return -1;
/* Create the key sequence string to pass to rl_generic_bind */
kseq = substring (line, kstart, i);
for ( ; line[i] && line[i] != ':'; i++)
;
if (line[i] != ':')
{
builtin_error (_("%s: missing colon separator"), line);
FREE (kseq);
return -1;
}
i = isolate_sequence (line, i + 1, 0, &kstart);
if (i < 0)
{
FREE (kseq);
return -1;
}
/* Create the value string containing the command to execute. */
value = substring (line, kstart, i);
/* Save the command to execute and the key sequence in the CMD_XMAP */
cmd_xmap = get_cmd_xmap_from_keymap (kmap);
rl_generic_bind (ISMACR, kseq, value, cmd_xmap);
/* and bind the key sequence in the current keymap to a function that
understands how to execute from CMD_XMAP */
rl_bind_keyseq_in_map (kseq, bash_execute_unix_command, kmap);
free (kseq);
return 0;
}
int
unbind_unix_command (kseq)
char *kseq;
{
Keymap cmd_xmap;
cmd_xmap = get_cmd_xmap_from_keymap (rl_get_keymap ());
if (rl_bind_keyseq_in_map (kseq, (rl_command_func_t *)NULL, cmd_xmap) != 0)
{
builtin_error (_("`%s': cannot unbind in command keymap"), kseq);
return 0;
}
return 1;
}
/* Used by the programmable completion code. Complete TEXT as a filename,
but return only directories as matches. Dequotes the filename before
attempting to find matches. */
char **
bash_directory_completion_matches (text)
const char *text;
{
char **m1;
char *dfn;
int qc;
qc = rl_dispatching ? rl_completion_quote_character : 0;
/* If rl_completion_found_quote != 0, rl_completion_matches will call the
filename dequoting function, causing the directory name to be dequoted
twice. */
if (rl_dispatching && rl_completion_found_quote == 0)
dfn = bash_dequote_filename ((char *)text, qc);
else
dfn = (char *)text;
m1 = rl_completion_matches (dfn, rl_filename_completion_function);
if (dfn != text)
free (dfn);
if (m1 == 0 || m1[0] == 0)
return m1;
/* We don't bother recomputing the lcd of the matches, because it will just
get thrown away by the programmable completion code and recomputed
later. */
(void)bash_progcomp_ignore_filenames (m1);
return m1;
}
char *
bash_dequote_text (text)
const char *text;
{
char *dtxt;
int qc;
qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
dtxt = bash_dequote_filename ((char *)text, qc);
return (dtxt);
}
/* This event hook is designed to be called after readline receives a signal
that interrupts read(2). It gives reasonable responsiveness to interrupts
and fatal signals without executing too much code in a signal handler
context. */
static int
bash_event_hook ()
{
int sig;
/* XXX - see if we need to do anything here if sigterm_received == 1,
we probably don't want to reset the event hook since we will not be
jumping to the top level */
if (sigterm_received)
{
/* RESET_SIGTERM; */
return 0;
}
sig = 0;
if (terminating_signal)
sig = terminating_signal;
else if (interrupt_state)
sig = SIGINT;
else if (sigalrm_seen)
sig = SIGALRM;
else
sig = first_pending_trap ();
/* If we're going to longjmp to top_level, make sure we clean up readline.
check_signals will call QUIT, which will eventually longjmp to top_level,
calling run_interrupt_trap along the way. The check for sigalrm_seen is
to clean up the read builtin's state. */
if (terminating_signal || interrupt_state || sigalrm_seen)
rl_cleanup_after_signal ();
bashline_reset_event_hook ();
/* posix mode SIGINT during read -e. We only get here if SIGINT is trapped. */
if (posixly_correct && this_shell_builtin == read_builtin && sig == 2)
{
last_command_exit_value = 128|SIGINT;
throw_to_top_level ();
}
check_signals_and_traps (); /* XXX */
return 0;
}
#endif /* READLINE */
|