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
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
//! Port of `Src/Zle/compctl.c` — the legacy `compctl` builtin and its
//! supporting completion machinery (predates compsys).
//!
//! Global matcher. // c:33
//! Default completion infos // c:38
//! Hash table for completion info for commands // c:43
//! List of pattern compctls // c:48
//! Main entry point for the `compctl' builtin // c:1558
//!
//! This file ports the type definitions, constants, and the free functions
//! of `compctl.c` (`makecomplist*`, `bin_compctl`, `printcompctl`, ...).
//!
//! Citations: every fn comment references `Src/Zle/compctl.c:<line>` so
//! drift can be checked against the upstream snapshot.
#![allow(dead_code)]
#![allow(clippy::too_many_arguments)]
use crate::ported::builtin::findcmd;
use crate::ported::pattern::{patcompile, pattry};
use crate::ported::utils::errflag;
use crate::ported::zle::comp_h::{Aminfo, Ccmakedat, Cmlist};
use crate::ported::zle::compctl_h::{
Compcond, CompcondData, Compctl, CCT_CURPAT, CCT_CURPRE, CCT_CURSTR, CCT_CURSUB, CCT_CURSUBC,
CCT_CURSUF, CCT_NUMWORDS, CCT_POS, CCT_QUOTE, CCT_RANGEPAT, CCT_RANGESTR, CCT_WORDPAT,
CCT_WORDSTR, CC_ALGLOB, CC_ALREG, CC_ARRAYS, CC_BINDINGS, CC_BUILTINS, CC_CCCONT, CC_COMMPATH,
CC_DEFCONT, CC_DELETE, CC_DIRS, CC_DISCMDS, CC_ENVVARS, CC_EXCMDS, CC_EXPANDEXPL, CC_EXTCMDS,
CC_FILES, CC_INTVARS, CC_JOBS, CC_NAMED, CC_NOSORT, CC_OPTIONS, CC_PARAMS, CC_PATCONT,
CC_QUOTEFLAG, CC_READONLYS, CC_REMOVE, CC_RESWDS, CC_RUNNING, CC_SCALARS, CC_SHFUNCS,
CC_SPECIALS, CC_STOPPED, CC_UNIQALL, CC_UNIQCON, CC_USERS, CC_VARS, CC_XORCONT,
};
use crate::ported::zle::complete::parse_cmatcher;
// Deduped completion globals — canonical homes are complete.c's
// `mod_export` declarations (complete.rs). `autoq` is deduped to
// zle_tricky.rs (C: zle_tricky.c:137) via the `zle_tricky::*` glob
// import below. These replace former compctl-private copies.
use crate::ported::zle::complete::{COMPCURRENT, COMPQSTACK, COMPWORDS};
#[allow(unused_imports)]
use crate::ported::zle::{
deltochar::*, textobjects::*, zle_hist::*, zle_main::*, zle_misc::*, zle_move::*,
zle_params::*, zle_refresh::*, zle_tricky::*, zle_utils::*, zle_vi::*, zle_word::*,
};
use crate::ported::zsh_h::PAT_HEAPDUP;
use crate::ported::zsh_h::{
IN_NOTHING, QT_BACKSLASH, QT_BACKTICK, QT_DOLLARS, QT_DOUBLE, QT_NONE, QT_SINGLE,
};
use crate::ported::zle::zle_tricky::{INBACKT, INSTRING};
use crate::ported::zsh_h::{Bnullkeep, Nularg, Qstring};
use std::collections::HashMap;
use std::os::unix::fs::PermissionsExt;
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};
// Re-export the canonical `compctl.h` ports from compctl_h.rs so
// callers within compctl.rs reference the legit names. The four
// types (Compctlp/Patcomp/Compcond/Compctl + CompcondData) are
// direct ports of the C structs declared in Src/Zle/compctl.h.
// --- AUTO: cross-zle hoisted-fn use glob ---
#[allow(unused_imports)]
#[allow(unused_imports)]
// =====================================================================
// COMP_* — `compctl` operation flags from `Src/Zle/compctl.c:53-60`.
// Encode the command-line operation requested by `compctl`'s flag
// arguments (`-L`, `-C`, `-D`, `-T`, `-M`).
// =====================================================================
/// Port of `COMP_LIST` from `Src/Zle/compctl.c:53`. `-L` flag — list
/// existing compctl bindings.
pub const COMP_LIST: i32 = 1 << 0; // c:53
/// Port of `COMP_COMMAND` from `compctl.c:54`. `-C` — operate on the
/// command-completion table.
pub const COMP_COMMAND: i32 = 1 << 1; // c:54
/// Port of `COMP_DEFAULT` from `compctl.c:55`. `-D` — operate on the
/// default-completion entry.
pub const COMP_DEFAULT: i32 = 1 << 2; // c:55
/// Port of `COMP_FIRST` from `compctl.c:56`. `-T` — operate on the
/// first-completion entry.
pub const COMP_FIRST: i32 = 1 << 3; // c:56
/// Port of `COMP_REMOVE` from `compctl.c:57`. `+` prefix or remove op.
pub const COMP_REMOVE: i32 = 1 << 4; // c:57
/// Port of `COMP_LISTMATCH` from `compctl.c:58`. `-L -M` combination.
pub const COMP_LISTMATCH: i32 = 1 << 5; // c:58
/// Port of `COMP_SPECIAL` from `compctl.c:60`. Mask covering all
/// "special" entry-point flags.
pub const COMP_SPECIAL: i32 = COMP_COMMAND | COMP_DEFAULT | COMP_FIRST; // c:60
// =================================================================
// Free ported — start of compctl.c proper
// =================================================================
/// Initialize the `compctltab` hash table.
/// Port of `createcompctltable()` from Src/Zle/compctl.c:70. The C
/// version wires hash function pointers (hasher, addnode, getnode,
/// printnode, freenode); Rust uses a plain HashMap so the wiring
/// reduces to allocation.
pub(crate) fn createcompctltable() {
let mut g = COMPCTL_TAB.write().unwrap();
*g = Some(HashMap::new());
let mut p = PATCOMPS.write().unwrap();
p.clear();
}
/// Free a `compctlp` hash node.
/// Port of `freecompctlp(HashNode hn)` from Src/Zle/compctl.c:92. Rust's Arc
/// drop handles the inner Compctl free; this is the entry the C
/// hash table calls back when removing a node.
/// WARNING: param names don't match C — Rust=() vs C=(hn)
pub(crate) fn freecompctlp(name: &str) {
let mut g = COMPCTL_TAB.write().unwrap();
if let Some(map) = g.as_mut() {
map.remove(name);
}
}
/// Port of `void freecompctl(Compctl cc)` from Src/Zle/compctl.c:103.
/// ```c
/// void
/// freecompctl(Compctl cc)
/// {
/// if (cc == &cc_default ||
/// cc == &cc_first ||
/// cc == &cc_compos ||
/// --cc->refc > 0)
/// return;
/// zsfree(cc->keyvar);
/// zsfree(cc->glob);
/// zsfree(cc->str);
/// zsfree(cc->func);
/// zsfree(cc->explain);
/// zsfree(cc->ylist);
/// zsfree(cc->prefix);
/// zsfree(cc->suffix);
/// zsfree(cc->hpat);
/// zsfree(cc->gname);
/// zsfree(cc->subcmd);
/// zsfree(cc->substr);
/// if (cc->cond) freecompcond(cc->cond);
/// if (cc->ext) {
/// Compctl n, m;
/// n = cc->ext;
/// do { m = (Compctl)(n->next); freecompctl(n); n = m; } while (n);
/// }
/// if (cc->xor && cc->xor != &cc_default)
/// freecompctl(cc->xor);
/// if (cc->matcher) freecmatcher(cc->matcher);
/// zsfree(cc->mstr);
/// zfree(cc, sizeof(struct compctl));
/// }
/// ```
pub(crate) fn freecompctl(cc: Arc<Compctl>) {
// c:103
// c:105-109 — sentinel + refc-decrement early return. The Rust
// Arc carries refcounting natively; `Arc::strong_count > 1`
// mirrors the C `--cc->refc > 0` test exactly.
// c:105-107 — pointer-equality vs cc_default/cc_first/cc_compos.
let cc_default_ref = CC_DEFAULT.lock().unwrap().clone();
let cc_first_ref = CC_FIRST.lock().unwrap().clone();
let cc_compos_ref = CC_COMPOS.lock().unwrap().clone();
let is_sentinel = cc_default_ref.as_ref().is_some_and(|s| Arc::ptr_eq(s, &cc))
|| cc_first_ref.as_ref().is_some_and(|s| Arc::ptr_eq(s, &cc))
|| cc_compos_ref.as_ref().is_some_and(|s| Arc::ptr_eq(s, &cc));
if is_sentinel || Arc::strong_count(&cc) > 1 {
// c:108 --cc->refc > 0
return; // c:109
}
// c:111-122 — zsfree every owned string field. Rust's `Drop` on
// `Option<String>` handles each of these when `cc` falls out of
// scope at end of fn. Mirror the C order explicitly so the audit
// trail matches; the `let _` bindings force-evaluate each field
// for parity with `zsfree(NULL)` being a defined no-op.
let _ = &cc.keyvar; // c:111 zsfree(keyvar)
let _ = &cc.glob; // c:112 zsfree(glob)
let _ = &cc.str; // c:113 zsfree(str)
let _ = &cc.func; // c:114 zsfree(func)
let _ = &cc.explain; // c:115 zsfree(explain)
let _ = &cc.ylist; // c:116 zsfree(ylist)
let _ = &cc.prefix; // c:117 zsfree(prefix)
let _ = &cc.suffix; // c:118 zsfree(suffix)
let _ = &cc.hpat; // c:119 zsfree(hpat)
let _ = &cc.gname; // c:120 zsfree(gname)
let _ = &cc.subcmd; // c:121 zsfree(subcmd)
let _ = &cc.substr; // c:122 zsfree(substr)
// c:123-124 — `if (cc->cond) freecompcond(cc->cond);`
if let Some(cond) = cc.cond.as_deref() {
// c:123
freecompcond((*cond).clone()); // c:124
}
// c:125-135 — recursive ext-chain walk.
if cc.ext.is_some() {
// c:125
let mut n: Option<Arc<Compctl>> = cc.ext.clone(); // c:128 n = cc->ext
while let Some(node) = n.take() {
// c:129-134 do { ... } while (n)
let m = node.next.clone(); // c:130 m = n->next
freecompctl(node); // c:131 freecompctl(n)
n = m; // c:132 n = m
}
}
// c:136-137 — `if (cc->xor && cc->xor != &cc_default) freecompctl(cc->xor);`
if let Some(xor) = cc.xor.clone() {
// c:136 cc->xor
let xor_is_default = cc_default_ref
.as_ref()
.is_some_and(|s| Arc::ptr_eq(s, &xor));
if !xor_is_default {
// c:136 cc->xor != &cc_default
freecompctl(xor); // c:137
}
}
// c:138-139 — `if (cc->matcher) freecmatcher(cc->matcher);`
// freecmatcher isn't ported as a free fn (see freecmatcher port
// below in this file or matcher Drop). Rust Box::drop on the
// matcher field covers this when `cc` drops.
let _ = &cc.matcher; // c:138-139
// c:140 — zsfree(cc->mstr);
let _ = &cc.mstr; // c:140
// c:141 — `zfree(cc, sizeof(struct compctl));`
// Arc::drop at end of scope handles the box-free. Mirror the
// explicit C call site for audit parity.
drop(cc); // c:141
}
/// Free a `compcond` spec.
/// Port of `freecompcond(void *a)` from Src/Zle/compctl.c:146. C walks the
/// or/and chain, freeing per-type union data. Rust's enum + Box
/// drop the chain automatically; this is the entry kept for ABI
/// parity with the C source.
/// WARNING: param names don't match C — Rust=() vs C=(a)
pub(crate) fn freecompcond(cc: Compcond) {
// c:146
// c:148-186 — walk `or` chain; for each `or` node, walk its `and`
// chain freeing per-type union data, then `zfree(c, sizeof(struct
// compcond))`. Rust Box+Vec+String drop subsumes every per-field
// `zsfree`/`free` call, but the structural walk is preserved so
// the chain is consumed in the same order C frees it (top-down,
// or-chain outer / and-chain inner).
let mut or_cur: Option<Box<Compcond>> = Some(Box::new(cc)); // c:151 for (c = cc; c; c = or)
while let Some(mut or_node) = or_cur {
let next_or = or_node.or.take(); // c:152 or = c->or
let mut and_cur: Option<Box<Compcond>> = Some(or_node); // c:153 for (; c; c = and)
while let Some(mut and_node) = and_cur {
let next_and = and_node.and.take(); // c:154 and = c->and
// c:155-184 — per-typ union frees. Box/Vec/String Drop on
// and_node going out of scope handles every variant
// (CCT_POS / CCT_NUMWORDS / CCT_CURSUF / CCT_CURPRE /
// CCT_RANGESTR / CCT_RANGEPAT / default).
let _ = and_node.u; // c:155-184 zsfree per-variant
and_cur = next_and; // c:185 c = and
}
or_cur = next_or; // c:151 c = or
}
}
/// Direct port of `static Cmlist cpcmlist(Cmlist l)` from
/// Src/Zle/compctl.c:291. Deep-copies a Cmlist linked list, using
/// `cpcmatcher` for each matcher's chain. Returns the new head.
pub(crate) fn cpcmlist(
// c:291
mut l: Option<&Cmlist>,
) -> Option<std::sync::Arc<Cmlist>> {
// c:293-301 — C appends through `p = &(n->next)`. `Cmlist` nodes are
// refcounted (comp.h:148) and therefore immutable once linked, so the
// port collects the copies front-to-back and links them back-to-front;
// the resulting chain is identical and the `*mut` tail cursor (and its
// `unsafe`) is gone.
let mut nodes: Vec<(Box<crate::ported::zle::comp_h::Cmatcher>, String)> = Vec::new();
while let Some(src) = l {
// c:295 while (l)
let matcher_chain = crate::ported::zle::complete::cpcmatcher(
// c:298 cpcmatcher
Some(&*src.matcher),
)
.expect("cpcmatcher returned None for non-null source");
nodes.push((matcher_chain, src.str.clone())); // c:298-299 ztrdup
l = src.next.as_deref(); // c:311 l = l->next
}
let mut head: Option<std::sync::Arc<Cmlist>> = None; // c:293 r = NULL
for (matcher, str) in nodes.into_iter().rev() {
head = Some(std::sync::Arc::new(Cmlist {
// c:296 zalloc
next: head, // c:297
matcher, // c:298
str, // c:299
}));
}
head // c:311 return r
}
// `cclist` — flag for listing/command/default/first completion.
// Port of file-static `int cclist;` at Src/Zle/compctl.c:63.
// Bucket-1 per PORT_PLAN.md — per-completion-call scratch state,
// thread_local so concurrent completion invocations don't race.
thread_local! {
static CCLIST: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
}
// `showmask` — mask determining what to print.
// Port of file-static `unsigned long showmask;` at Src/Zle/compctl.c:66.
// Bucket-1 per PORT_PLAN.md.
thread_local! {
static SHOWMASK: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
/// Direct port of `static int set_gmatcher(char *name, char **argv)` from
/// Src/Zle/compctl.c:311. Parses each argv entry as a cmatcher
/// spec, builds a fresh Cmlist chain, frees the old CMATCHER and
/// installs the new one via cpcmlist.
pub(crate) fn set_gmatcher(name: &str, argv: &[String]) -> i32 {
// c:311
// c:314-325 — same back-to-front link as `cpcmlist` above, for the same
// reason: an `Arc`-linked node cannot be appended to after it is shared.
let mut parsed: Vec<(Box<crate::ported::zle::comp_h::Cmatcher>, String)> = Vec::new();
for word in argv {
// c:317 while (*argv)
let m = match parse_cmatcher(name, word) {
Some(m) => m, // c:319 parse_cmatcher
None => return 1, // c:319 == pcm_err
};
parsed.push((m, word.clone())); // c:322-323
}
let mut head: Option<std::sync::Arc<Cmlist>> = None; // c:314 l = NULL
for (matcher, str) in parsed.into_iter().rev() {
head = Some(std::sync::Arc::new(Cmlist {
// c:320 zhalloc
next: head, // c:321
matcher, // c:322
str, // c:323
}));
}
// freecmlist(cmatcher) — Drop on the Arc handles the C free path. // c:336
let new_list = cpcmlist(head.as_deref()); // c:336 cpcmlist(l)
if let Ok(mut guard) = CMATCHER.write() {
*guard = new_list;
}
1 // c:336
}
/// Direct port of `static int get_gmatcher(char *name, char **argv)` from
/// Src/Zle/compctl.c:336. Looks for a leading `-M` flag followed
/// by matcher specs (no `-`-prefixed args), then forwards to
/// `set_gmatcher` and translates its return into 0/1/2.
pub(crate) fn get_gmatcher(name: &str, argv: &[String]) -> i32 {
// c:336
if argv.first().map(|s| s.as_str()) != Some("-M") {
// c:336
return 0; // c:349
}
let rest = &argv[1..]; // c:339 p = ++argv
for w in rest {
// c:341 while (*p)
if w.starts_with('-') {
// c:342
return 0; // c:357
}
}
if set_gmatcher(name, rest) != 0 {
// c:357
return 2; // c:357
}
1 // c:357
}
/// Direct port of `void print_gmatcher(int ac)` from
/// `Src/Zle/compctl.c:357`. Prints the global matcher chain (the
/// CMATCHER list) as `compctl -M 'str1' 'str2' ...` when `ac` is
/// non-zero, or `MATCH 'str1' ...` otherwise. Used by `compctl -L`.
pub(crate) fn print_gmatcher(ac: i32) {
// c:357
let guard = CMATCHER.read().ok();
let head = match guard.as_ref().and_then(|g| g.as_deref()) {
Some(h) => h,
None => return, // c:361 if (cmatcher)
};
let prefix = if ac != 0 { "compctl -M" } else { "MATCH" }; // c:362
print!("{}", prefix);
let mut cur: Option<&Cmlist> = Some(head);
while let Some(p) = cur {
// c:364
print!(" '{}'", p.str); // c:365
cur = p.next.as_deref();
}
println!(); // c:369
}
/// Get a compctl from arg vector — main compctl-spec parser.
/// Port of `get_compctl(char *name, char ***av, Compctl cc, int first, int isdef, int cl)` from Src/Zle/compctl.c:377 (~480 lines).
///
/// Parses the basic flags for `compctl`: `first` says we are not in
/// extended completion, `hx` says we are in an or (`+`) completion
/// (needed because the initial compctl of default/command completion is
/// special). `cct` is a temporary that only holds the flags parsed so
/// far; `cc_assign` copies it into the compctl being built.
///
/// C walks `argv` as a `char **` and re-points `*argv` inside the
/// current word; the port keeps the word index `ai`, the word's chars
/// `cur` and the position `cp` inside it. The xor (`+`) chain C grows
/// through `cc->xor` is collected in `nodes` and linked into `Arc`s
/// once parsing is done, because a shared `Arc<Compctl>` is immutable.
///
/// On success the consumed words are drained off the front of `av`
/// (C: `*av = argv`) and 0 is returned; 1 on a parse error.
pub(crate) fn get_compctl(
name: &str,
av: &mut Vec<String>,
cc: &mut Compctl,
first: bool,
mut isdef: bool,
cl: i32,
) -> i32 {
use crate::ported::utils::zwarnnam;
let alen = av.len() as isize;
let mut ai: isize = 0; // C: argv
let mut ready = 0; // c:384
let mut hx = false; // c:384
// c:392-404 — `compctl + foo ...' becomes a default compctl by removing
// it from the hash table.
if first
&& av.first().is_some_and(|a| a == "+")
&& !av
.get(1)
.is_some_and(|a| a.starts_with('-') && a.len() > 1)
{
ai += 1;
if av.get(ai as usize).is_some_and(|a| a.starts_with('-')) {
ai += 1;
}
av.drain(0..(ai as usize).min(av.len()));
if cl != 0 {
return 1;
} else {
CCLIST.with(|c| c.set(COMP_REMOVE));
return 0;
}
}
// c:406-407 — struct compctl cct; cct.mask2 = CC_CCCONT;
let mut cct = Compctl::default();
cct.mask2 = CC_CCCONT;
// `cc` plus the xor'd compctls that follow it (C: cc = cc->xor).
let mut nodes: Vec<Compctl> = vec![std::mem::take(cc)];
let mut xor_default = false; // C: cc->xor = &cc_default
let mut root_special = false; // cc_assign pointed the root at a special target
// The word being walked: C's `*argv` (chars after the leading `-`).
let mut cur: Vec<char> = Vec::new();
let mut cp: usize = 0;
// The `if (**argv) ... else if (!argv[1]) warn, return 1; else *++argv`
// idiom shared by every flag taking an argument. Evaluates to
// Some(arg), or None after warning when the argument is missing.
macro_rules! flag_arg {
($arg:expr, $msg:literal) => {{
if cp < cur.len() {
let v: String = cur[cp..].iter().collect();
cp = cur.len(); // *argv = argv_end
Some(v)
} else if ai + 1 >= alen {
zwarnnam(name, &format!($msg, $arg));
None
} else {
ai += 1;
cur.clear(); // *argv = argv_end on the new word
cp = 0;
Some(av[ai as usize].clone())
}
}};
}
let atoi = |s: &str| -> i32 {
let t = s.trim_start();
let (neg, digits) = match t.strip_prefix('-') {
Some(r) => (true, r),
None => (false, t.strip_prefix('+').unwrap_or(t)),
};
let n: i32 = digits
.chars()
.take_while(|c| c.is_ascii_digit())
.collect::<String>()
.parse()
.unwrap_or(0);
if neg {
-n
} else {
n
}
};
// c:412 — loop through the flags until we have no more: those with
// arguments are not properly allocated yet, we just hang on to the
// argument that was passed.
while ready == 0
&& ai < alen
&& av[ai as usize].starts_with('-')
&& (av[ai as usize].len() > 1 || !first)
{
if av[ai as usize].len() == 1 {
av[ai as usize] = "-+".to_string(); // c:413-414
}
cur = av[ai as usize].chars().skip(1).collect(); // c:415 ++*argv
cp = 0;
while ready == 0 && cp < cur.len() {
let arg = cur[cp]; // c:418 unmeta_one(*argv, &sz)
cp += 1;
match arg {
'f' => cct.mask |= CC_FILES, // c:419
'c' => cct.mask |= CC_COMMPATH, // c:422
'm' => cct.mask |= CC_EXTCMDS, // c:425
'w' => cct.mask |= CC_RESWDS, // c:428
'o' => cct.mask |= CC_OPTIONS, // c:431
'v' => cct.mask |= CC_VARS, // c:434
'b' => cct.mask |= CC_BINDINGS, // c:437
'A' => cct.mask |= CC_ARRAYS, // c:440
'I' => cct.mask |= CC_INTVARS, // c:443
'F' => cct.mask |= CC_SHFUNCS, // c:446
'p' => cct.mask |= CC_PARAMS, // c:449
'E' => cct.mask |= CC_ENVVARS, // c:452
'j' => cct.mask |= CC_JOBS, // c:455
'r' => cct.mask |= CC_RUNNING, // c:458
'z' => cct.mask |= CC_STOPPED, // c:461
'B' => cct.mask |= CC_BUILTINS, // c:464
'a' => cct.mask |= CC_ALREG | CC_ALGLOB, // c:467
'R' => cct.mask |= CC_ALREG, // c:470
'G' => cct.mask |= CC_ALGLOB, // c:473
'u' => cct.mask |= CC_USERS, // c:476
'd' => cct.mask |= CC_DISCMDS, // c:479
'e' => cct.mask |= CC_EXCMDS, // c:482
'N' => cct.mask |= CC_SCALARS, // c:485
'O' => cct.mask |= CC_READONLYS, // c:488
'Z' => cct.mask |= CC_SPECIALS, // c:491
'q' => cct.mask |= CC_REMOVE, // c:494
'U' => cct.mask |= CC_DELETE, // c:497
'n' => cct.mask |= CC_NAMED, // c:500
'Q' => cct.mask |= CC_QUOTEFLAG, // c:503
'/' => cct.mask |= CC_DIRS, // c:506
't' => {
// c:509
if cl != 0 {
zwarnnam(name, &format!("bad option: -{}", arg));
return 1;
}
let Some(p) = flag_arg!(arg, "retry specification expected after -{}") else {
return 1;
};
match p.chars().next() {
Some('+') => cct.mask2 = CC_XORCONT, // c:528
Some('n') => cct.mask2 = 0, // c:531
Some('-') => cct.mask2 = CC_PATCONT, // c:534
Some('x') => cct.mask2 = CC_DEFCONT, // c:537
other => {
zwarnnam(
name,
&format!(
"invalid retry specification character `{}'",
other.map(String::from).unwrap_or_default()
),
);
return 1;
}
}
if p.chars().count() > 1 {
let rest: String = p.chars().skip(1).collect();
zwarnnam(
name,
&format!("too many retry specification characters: `{}'", rest),
);
return 1;
}
}
'k' => {
// c:553
let Some(v) = flag_arg!(arg, "variable name expected after -{}") else {
return 1;
};
cct.keyvar = Some(v);
}
'K' => {
// c:565
let Some(v) = flag_arg!(arg, "function name expected after -{}") else {
return 1;
};
cct.func = Some(v);
}
'Y' | 'X' => {
// c:577 — Y sets CC_EXPANDEXPL, X clears it; then `expl:`
if arg == 'Y' {
cct.mask |= CC_EXPANDEXPL;
} else {
cct.mask &= !CC_EXPANDEXPL;
}
let Some(v) = flag_arg!(arg, "string expected after -{}") else {
return 1;
};
cct.explain = Some(v);
}
'y' => {
// c:594 — C warns but does not return here.
if let Some(v) = flag_arg!(arg, "function/variable expected after -{}") {
cct.ylist = Some(v);
}
}
'P' => {
// c:606
let Some(v) = flag_arg!(arg, "string expected after -{}") else {
return 1;
};
cct.prefix = Some(v);
}
'S' => {
// c:618
let Some(v) = flag_arg!(arg, "string expected after -{}") else {
return 1;
};
cct.suffix = Some(v);
}
'g' => {
// c:630
let Some(v) = flag_arg!(arg, "glob pattern expected after -{}") else {
return 1;
};
cct.glob = Some(v);
}
's' => {
// c:642
let Some(v) = flag_arg!(arg, "command string expected after -{}") else {
return 1;
};
cct.str = Some(v);
}
'l' => {
// c:655
if cl != 0 {
zwarnnam(name, &format!("bad option: -{}", arg));
return 1;
}
let Some(v) = flag_arg!(arg, "command name expected after -{}") else {
return 1;
};
cct.subcmd = Some(v);
}
'h' => {
// c:670
if cl != 0 {
zwarnnam(name, &format!("bad option: -{}", arg));
return 1;
}
let Some(v) = flag_arg!(arg, "command name expected after -{}") else {
return 1;
};
cct.substr = Some(v);
}
'W' => {
// c:685
let Some(v) = flag_arg!(arg, "path expected after -{}") else {
return 1;
};
cct.withd = Some(v);
}
'J' => {
// c:697
let Some(v) = flag_arg!(arg, "group name expected after -{}") else {
return 1;
};
cct.gname = Some(v);
}
'V' => {
// c:709
let Some(v) = flag_arg!(arg, "group name expected after -{}") else {
return 1;
};
cct.gname = Some(v);
cct.mask2 |= CC_NOSORT;
}
'1' => {
// c:722
cct.mask2 |= CC_UNIQALL;
cct.mask2 &= !CC_UNIQCON;
}
'2' => {
// c:726
cct.mask2 |= CC_UNIQCON;
cct.mask2 &= !CC_UNIQALL;
}
'M' => {
// c:730
if (CCLIST.with(|c| c.get()) & COMP_LIST) != 0 {
CCLIST.with(|c| c.set(c.get() | COMP_LISTMATCH));
} else {
let Some(v) = flag_arg!(arg, "matching specification expected after -{}")
else {
return 1;
};
match parse_cmatcher(name, &v) {
None => {
cct.matcher = None;
cct.mstr = None;
return 1;
}
Some(m) => {
cct.matcher = Some(m);
cct.mstr = Some(v);
}
}
}
}
'H' => {
// c:757
if cp < cur.len() {
cct.hnum = atoi(&cur[cp..].iter().collect::<String>());
} else if ai + 1 < alen {
ai += 1;
cct.hnum = atoi(&av[ai as usize]);
} else {
zwarnnam(name, &format!("number expected after -{}", arg));
return 1;
}
if ai + 1 >= alen {
zwarnnam(name, &format!("missing pattern after -{}", arg));
return 1;
}
ai += 1;
let mut hpat = av[ai as usize].clone();
if cct.hnum < 1 {
cct.hnum = 0;
}
if hpat == "*" {
hpat = String::new();
}
cct.hpat = Some(hpat);
cur.clear(); // *argv = argv_end
cp = 0;
}
'C' => {
// c:777
if cl != 0 {
zwarnnam(name, &format!("bad option: -{}", arg));
return 1;
}
if first && !hx {
CCLIST.with(|c| c.set(c.get() | COMP_COMMAND));
} else {
zwarnnam(name, "misplaced command completion (-C) flag");
return 1;
}
}
'D' => {
// c:789
if cl != 0 {
zwarnnam(name, &format!("bad option: -{}", arg));
return 1;
}
if first && !hx {
isdef = true;
CCLIST.with(|c| c.set(c.get() | COMP_DEFAULT));
} else {
zwarnnam(name, "misplaced default completion (-D) flag");
return 1;
}
}
'T' => {
// c:802
if cl != 0 {
zwarnnam(name, &format!("bad option: -{}", arg));
return 1;
}
if first && !hx {
CCLIST.with(|c| c.set(c.get() | COMP_FIRST));
} else {
zwarnnam(name, "misplaced first completion (-T) flag");
return 1;
}
}
'L' => {
// c:814
if cl != 0 {
zwarnnam(name, &format!("bad option: -{}", arg));
return 1;
}
if !first || hx {
zwarnnam(name, "invalid use of -L flag");
return 1;
}
CCLIST.with(|c| c.set(c.get() | COMP_LIST));
}
'x' => {
// c:826
if cl != 0 {
zwarnnam(name, "extended completion not allowed");
return 1;
}
if ai + 1 >= alen {
zwarnnam(name, &format!("condition expected after -{}", arg));
return 1;
}
if first {
ai += 1; // argv++
let mut sub: Vec<String> = av[ai as usize..].to_vec();
if get_xcompctl(name, &mut sub, &mut cct, isdef) != 0 {
cct.ext = None; // if (cct.ext) freecompctl(cct.ext)
return 1;
}
// get_xcompctl leaves in `sub` what follows the last
// word it consumed; `ai` is left on that word (C:
// `*av = argv - 1`) so the `*++argv` below steps past it.
ai = alen - sub.len() as isize - 1;
ready = 2;
cur.clear();
cp = 0;
} else {
zwarnnam(name, "recursive extended completion not allowed");
return 1;
}
}
_ => {
// c:845
if !first && (arg == '-' || arg == '+') && cp >= cur.len() {
// *argv -= sz + 1, argv--, ready = 1
ai -= 1;
ready = 1;
} else {
zwarnnam(name, &format!("bad option: -{}", arg));
return 1;
}
}
}
}
ai += 1; // c:857 *++argv
if ai < alen
&& (ready == 0 || ready == 2)
&& av[ai as usize].starts_with('+')
&& av[ai as usize].len() == 1
{
if cl != 0 {
zwarnnam(name, "xor'ed completion invalid");
return 1;
}
// c:866 — There's an alternative (+) completion: assign what we
// have so far before moving on to that.
let reass = first && !hx;
let special = reass
&& (CCLIST.with(|c| c.get()) & COMP_LIST) == 0
&& (CCLIST.with(|c| c.get()) & COMP_SPECIAL) != 0;
if cc_assign(name, nodes.last_mut().unwrap(), &cct, reass) != 0 {
return 1;
}
root_special |= special;
hx = true;
ready = 0;
ai += 1;
if ai >= alen
|| !av[ai as usize].starts_with('-')
|| av[ai as usize] == "-"
|| av[ai as usize] == "--"
{
// No argument to +, which means do default completion.
if isdef {
zwarnnam(name, "recursive xor'd default completions not allowed");
} else {
xor_default = true; // cc->xor = &cc_default
}
} else {
// More flags follow: prepare to loop again.
nodes.push(Compctl::default()); // cc->xor = zshcalloc; cc = cc->xor
cct = Compctl::default();
cct.mask2 = CC_CCCONT;
}
}
}
if ready == 0 && ai < alen && av[ai as usize].starts_with('-') {
ai += 1; // c:889
}
if (cct.mask & (CC_EXCMDS | CC_DISCMDS)) == 0 {
cct.mask |= CC_EXCMDS; // c:892
}
// c:895 — assign the last set of flags we parsed.
let reass = first && !hx;
let special = reass
&& (CCLIST.with(|c| c.get()) & COMP_LIST) == 0
&& (CCLIST.with(|c| c.get()) & COMP_SPECIAL) != 0;
if cc_assign(name, nodes.last_mut().unwrap(), &cct, reass) != 0 {
return 1;
}
root_special |= special;
// Link the xor chain back to front, ending in cc_default if `+` had
// no flags after it.
let mut tail: Option<Arc<Compctl>> = if xor_default {
CC_DEFAULT.lock().unwrap().clone()
} else {
None
};
while nodes.len() > 1 {
let mut n = nodes.pop().unwrap();
n.xor = tail.take();
tail = Some(Arc::new(n));
}
let mut root = nodes.pop().unwrap();
root.xor = tail;
if root_special {
// cc_assign pointed `cc` at cc_compos / cc_default / cc_first, so
// the parsed chain belongs to that static.
let cclist = CCLIST.with(|c| c.get());
let slot = if (cclist & COMP_COMMAND) != 0 {
&CC_COMPOS
} else if (cclist & COMP_DEFAULT) != 0 {
&CC_DEFAULT
} else {
&CC_FIRST
};
*slot.lock().unwrap() = Some(Arc::new(root));
} else {
*cc = root;
}
av.drain(0..(ai.max(0) as usize).min(av.len())); // c:899 *av = argv
0
}
/// Parse the `-x` extended-condition compctl form.
/// Port of `get_xcompctl(char *name, char ***av, Compctl cc, int isdef)` from Src/Zle/compctl.c:909 (~260 lines).
///
/// Handles the `-x ... --` part of compctl: each condition string is
/// something like `s[...][...], p[...]` (or'ed with `,`, and'ed by
/// juxtaposition), followed by the flags that go with it, up to a `--`.
/// The conditions become `Compcond` chains on the compctls linked
/// through `cc.ext` / `next`.
///
/// C marks the active `]` and `,` of a condition by overwriting them
/// with `\200` and `\201` in the argument string; the port does the
/// same in a char buffer, with two non-characters as the markers.
///
/// On success `av` is left holding what follows the last consumed word
/// (C: `*av = argv - 1`, which the caller steps past); 1 is returned
/// on a parse error.
pub(crate) fn get_xcompctl(name: &str, av: &mut Vec<String>, cc: &mut Compctl, isdef: bool) -> i32 {
use crate::ported::utils::zwarnnam;
const END: char = '\u{10fffe}'; // C: '\200' — the active `]`
const COMMA: char = '\u{10ffff}'; // C: '\201' — the active `,`
let mut argv: Vec<String> = std::mem::take(av);
let mut ready = false; // c:913
let mut nodes: Vec<Compctl> = Vec::new(); // C: the cc->ext / next chain
let atoi = |s: &str| -> i32 {
let t = s.trim_start();
let (neg, digits) = match t.strip_prefix('-') {
Some(r) => (true, r),
None => (false, t.strip_prefix('+').unwrap_or(t)),
};
let n: i32 = digits
.chars()
.take_while(|c| c.is_ascii_digit())
.collect::<String>()
.parse()
.unwrap_or(0);
if neg {
-n
} else {
n
}
};
while !ready {
if argv.is_empty() {
zwarnnam(name, "missing command names");
return 1;
}
// c:920 — o keeps track of or's, m remembers the starting
// condition, c is the current condition being parsed. `groups`
// holds one and-chain per or'ed condition; its last entry's last
// node is `c`.
let mut groups: Vec<Vec<Compcond>> = vec![vec![Compcond::default()]];
let mut tb: Vec<char> = argv[0].chars().collect();
let mut t: usize = 0;
let ch = |tb: &Vec<char>, i: usize| -> char { tb.get(i).copied().unwrap_or('\0') };
// c:922 — loop over each condition: something like 's[...][...], p[...]'
while ch(&tb, t) != '\0' {
while ch(&tb, t) == ' ' {
t += 1;
}
// c:926 — first get the condition code
let typ = match ch(&tb, t) {
'q' => CCT_QUOTE, // c:927
's' => CCT_CURSUF, // c:930
'S' => CCT_CURPRE, // c:933
'p' => CCT_POS, // c:936
'c' => CCT_CURSTR, // c:939
'C' => CCT_CURPAT, // c:942
'w' => CCT_WORDSTR, // c:945
'W' => CCT_WORDPAT, // c:948
'n' => CCT_CURSUB, // c:951
'N' => CCT_CURSUBC, // c:954
'm' => CCT_NUMWORDS, // c:957
'r' => CCT_RANGESTR, // c:960
'R' => CCT_RANGEPAT, // c:963
other => {
let shown = if other == '\0' { String::new() } else { other.to_string() };
zwarnnam(name, &format!("unknown condition code: {}", shown));
return 1;
}
};
// c:974 — now get the arguments in square brackets
if ch(&tb, t + 1) != '[' {
zwarnnam(
name,
&format!("expected condition after condition code: {}", ch(&tb, t)),
);
return 1;
}
t += 1;
// c:985 — first count how many or'd arguments there are,
// marking the active ]'s and ,'s with unprintable characters.
let mut n: i32 = 0;
let mut l: i32 = 0;
let mut tt = t;
while ch(&tb, tt) == '[' {
l = 1;
tt += 1;
while ch(&tb, tt) != '\0' && l != 0 {
if ch(&tb, tt) == '\\' && ch(&tb, tt + 1) != '\0' {
tt += 1;
} else if ch(&tb, tt) == '[' {
l += 1;
} else if ch(&tb, tt) == ']' {
l -= 1;
} else if l == 1 && ch(&tb, tt) == ',' {
tb[tt] = COMMA;
}
tt += 1;
}
if tt > 0 && ch(&tb, tt - 1) == ']' {
tb[tt - 1] = END;
}
n += 1;
}
if l != 0 {
zwarnnam(name, "error after condition code: [");
return 1;
}
let mut c = Compcond::default();
c.typ = typ;
c.n = n;
// c:1009 — allocate space for all the arguments of the conditions
let mut ra: Vec<i32> = Vec::new();
let mut rb: Vec<i32> = Vec::new();
let mut sp: Vec<i32> = Vec::new();
let mut ss: Vec<String> = Vec::new();
let mut la: Vec<String> = Vec::new();
let mut lb: Vec<Option<String>> = Vec::new();
// c:1028 — now loop over the actual arguments
let mut l: i32 = 0;
while ch(&tb, t) == '[' {
t += 1;
while ch(&tb, t) != '\0' && ch(&tb, t) == ' ' {
t += 1;
}
let mut tt = t;
if typ == CCT_POS || typ == CCT_NUMWORDS {
// p[...] or m[...]: one or two numbers expected
while ch(&tb, t) != '\0' && ch(&tb, t) != COMMA && ch(&tb, t) != END {
t += 1;
}
let sav = ch(&tb, t);
if sav == '\0' {
zwarnnam(name, "error in condition");
return 1;
}
let a = atoi(&tb[tt..t].iter().collect::<String>());
ra.push(a);
// Second argument is optional: see if it's there
if sav == END {
rb.push(a); // no: copy first argument
} else {
t += 1;
tt = t;
while ch(&tb, t) != '\0' && ch(&tb, t) != END {
t += 1;
}
if ch(&tb, t) == '\0' {
zwarnnam(name, "error in condition");
return 1;
}
rb.push(atoi(&tb[tt..t].iter().collect::<String>()));
}
} else if typ == CCT_CURSUF || typ == CCT_CURPRE || typ == CCT_QUOTE {
// -s[..] or -S[..]: single string expected
while ch(&tb, t) != '\0' && ch(&tb, t) != END {
if ch(&tb, t) == COMMA {
tb[t] = ',';
}
t += 1;
}
if ch(&tb, t) == '\0' {
zwarnnam(name, "error in condition");
return 1;
}
ss.push(tb[tt..t].iter().collect());
} else if typ == CCT_RANGESTR || typ == CCT_RANGEPAT {
// -r[..,..] or -R[..,..]: two strings expected
while ch(&tb, t) != '\0' && ch(&tb, t) != COMMA && ch(&tb, t) != END {
t += 1;
}
if ch(&tb, t) == '\0' {
zwarnnam(name, "error in condition");
return 1;
}
let hc = ch(&tb, t) == COMMA;
la.push(tb[tt..t].iter().collect());
if hc {
t += 1;
tt = t;
// any more commas are text, not active
while ch(&tb, t) != '\0' && ch(&tb, t) != END {
if ch(&tb, t) == COMMA {
tb[t] = ',';
}
t += 1;
}
if ch(&tb, t) == '\0' {
zwarnnam(name, "error in condition");
return 1;
}
lb.push(Some(tb[tt..t].iter().collect())); // c:1098
} else {
lb.push(None); // c:1101
}
} else {
// remaining patterns are number followed by string
while ch(&tb, t) != '\0' && ch(&tb, t) != END && ch(&tb, t) != COMMA {
t += 1;
}
if ch(&tb, t) == '\0' || ch(&tb, t) == END {
zwarnnam(name, "error in condition");
return 1;
}
sp.push(atoi(&tb[tt..t].iter().collect::<String>()));
t += 1;
tt = t;
while ch(&tb, t) != '\0' && ch(&tb, t) != END {
if ch(&tb, t) == COMMA {
tb[t] = ',';
}
t += 1;
}
if ch(&tb, t) == '\0' {
zwarnnam(name, "error in condition");
return 1;
}
ss.push(tb[tt..t].iter().collect());
}
l += 1;
t += 1; // for (...; l++, t++)
}
let _ = l;
c.u = if typ == CCT_POS || typ == CCT_NUMWORDS {
CompcondData::R { a: ra, b: rb }
} else if typ == CCT_RANGESTR || typ == CCT_RANGEPAT {
CompcondData::L { a: la, b: lb }
} else if typ == CCT_CURSUF || typ == CCT_CURPRE || typ == CCT_QUOTE {
CompcondData::S {
p: Vec::new(),
s: ss,
}
} else {
CompcondData::S { p: sp, s: ss }
};
*groups.last_mut().unwrap().last_mut().unwrap() = c;
while ch(&tb, t) == ' ' {
t += 1;
}
if ch(&tb, t) == ',' {
// Another condition to `or'
groups.push(vec![Compcond::default()]);
t += 1;
} else if ch(&tb, t) != '\0' {
// Another condition to `and'
groups.last_mut().unwrap().push(Compcond::default());
}
}
// Link the or / and chains: each group head's `or` is the next
// group head, each node's `and` the next node of its group.
let mut m: Option<Box<Compcond>> = None;
for group in groups.into_iter().rev() {
let mut head: Option<Box<Compcond>> = None;
for mut node in group.into_iter().rev() {
node.and = head.take();
head = Some(Box::new(node));
}
let mut h = head.unwrap();
h.or = m.take();
m = Some(h);
}
// c:1137 — assign condition to current compctl
let mut next = Compctl::default();
next.cond = m;
argv.remove(0);
// End of the condition; get the flags that go with it.
if get_compctl(name, &mut argv, &mut next, false, isdef, 0) != 0 {
return 1;
}
nodes.push(next);
if argv.is_empty() && (CCLIST.with(|c| c.get()) & COMP_SPECIAL) != 0 {
// default, first, or command completion finished
ready = true;
} else {
// see if we are looking for more conditions or are ready to
// return (ready = true)
if argv.is_empty()
|| !argv[0].starts_with('-')
|| ((argv[0].len() == 1 || argv[0].chars().nth(1) == Some('+')) && argv.len() < 2)
{
zwarnnam(name, "missing command names");
return 1;
}
if argv[0] == "--" {
ready = true;
} else if argv[0] == "-+" && argv.get(1).is_some_and(|a| a == "--") {
ready = true;
argv.remove(0);
}
argv.remove(0);
}
}
// Link the compctls through `next` and hang them off cc.ext.
let mut tail: Option<Arc<Compctl>> = None;
for mut n in nodes.into_iter().rev() {
n.next = tail.take();
tail = Some(Arc::new(n));
}
cc.ext = tail;
// c:1167 — save position at end of parsing
*av = argv;
0
}
/// Copy the flags parsed into `cct` onto the compctl being built.
/// Port of `cc_assign(char *name, Compctl *ccptr, Compctl cct, int reass)` from Src/Zle/compctl.c:1174 (~75 lines).
///
/// With `reass` (the first compctl of a command line that is not just
/// listing) `-C`/`-D`/`-T` retarget `*ccptr` to the static
/// `cc_compos` / `cc_default` / `cc_first`, whose old `ext` / `xor`
/// chains are dropped by `cc_reassign`. The statics are `Arc`s here, so
/// `*ccptr` becomes a working copy of the static; `get_compctl` stores
/// the finished chain back into the static once it is complete.
pub(crate) fn cc_assign(name: &str, ccptr: &mut Compctl, cct: &Compctl, reass: bool) -> i32 {
let cclist = CCLIST.with(|c| c.get());
// Handle assignment of new default or command completion
if reass && (cclist & COMP_LIST) == 0 {
// if not listing
if cclist == (COMP_COMMAND | COMP_DEFAULT)
|| cclist == (COMP_COMMAND | COMP_FIRST)
|| cclist == (COMP_DEFAULT | COMP_FIRST)
|| cclist == COMP_SPECIAL
{
crate::ported::utils::zwarnnam(name, "can't set -D, -T, and -C simultaneously");
// ... because the following code wouldn't work.
return 1;
}
let slot = if (cclist & COMP_COMMAND) != 0 {
Some(&CC_COMPOS) // c:1190 command
} else if (cclist & COMP_DEFAULT) != 0 {
Some(&CC_DEFAULT) // c:1194 default
} else if (cclist & COMP_FIRST) != 0 {
Some(&CC_FIRST) // c:1198 first
} else {
None
};
if let Some(slot) = slot {
*ccptr = slot
.lock()
.unwrap()
.as_ref()
.map(|a| (**a).clone())
.unwrap_or_default();
cc_reassign(ccptr);
}
}
// c:1205-1247 — free the old compctl's strings and copy over the new
// stuff (Drop frees; clone allocates).
ccptr.mask = cct.mask;
ccptr.mask2 = cct.mask2;
ccptr.keyvar = cct.keyvar.clone();
ccptr.glob = cct.glob.clone();
ccptr.str = cct.str.clone();
ccptr.func = cct.func.clone();
ccptr.explain = cct.explain.clone();
ccptr.ylist = cct.ylist.clone();
ccptr.prefix = cct.prefix.clone();
ccptr.suffix = cct.suffix.clone();
ccptr.subcmd = cct.subcmd.clone();
ccptr.substr = cct.substr.clone();
ccptr.withd = cct.withd.clone();
ccptr.gname = cct.gname.clone();
ccptr.hpat = cct.hpat.clone();
ccptr.hnum = cct.hnum;
ccptr.matcher = crate::ported::zle::complete::cpcmatcher(cct.matcher.as_deref());
ccptr.mstr = cct.mstr.clone();
// careful with extended completion: it's already allocated
ccptr.ext = cct.ext.clone();
0
}
/// Free a new default or command completion's chains, keeping the node.
/// Port of `cc_reassign(Compctl cc)` from Src/Zle/compctl.c:1253.
///
/// C builds a temporary compctl carrying `cc->xor` / `cc->ext`, frees
/// it (which frees both chains) and nulls them on `cc`, so the
/// statically allocated `cc_compos` / `cc_default` / `cc_first` itself
/// is never freed. Dropping the `Arc`s does the freeing here.
pub(crate) fn cc_reassign(cc: &mut Compctl) {
cc.ext = None;
cc.xor = None;
}
/// Test whether the given string is a pattern.
/// Port of `compctl_name_pat(char **p)` from Src/Zle/compctl.c:1275.
///
/// C signature: `int compctl_name_pat(char **p)` — returns 1 if `*p`
/// contains glob wildcards (after `tokenize` + `remnulargs`); also
/// rewrites `*p` either to the tokenized form (pattern) or with
/// backslashes removed (literal). Rust port: returns `(is_pattern,
/// new_text)` tuple since we can't mutate a `&str` in-place.
///
/// Pattern detection is `haswilds()` on the tokenized, remnulargs-cleaned copy.
/// WARNING: param names don't match C — Rust=() vs C=(p)
pub(crate) fn compctl_name_pat(p: &str) -> (bool, String) {
let mut s = p.to_string(); // c:1277 char *s = *p;
crate::ported::glob::tokenize(&mut s); // c:1279 tokenize(s = dupstring(s));
crate::ported::glob::remnulargs(&mut s); // c:1280
if crate::ported::pattern::haswilds(&s) {
// c:1282
(true, s) // c:1283-1284 *p = s; return 1;
} else {
(false, crate::ported::zle::compcore::rembslash(p)) // c:1286 *p = rembslash(*p);
}
}
/// Delete a pattern compctl by name.
/// Port of `delpatcomp(char *n)` from Src/Zle/compctl.c:1294.
pub(crate) fn delpatcomp(n: &str) {
// c:1294
let mut patcomps = PATCOMPS.write().unwrap();
// c:1296 — Patcomp p, q;
// c:1298 — for (q = 0, p = patcomps; p; q = p, p = p->next)
for i in 0..patcomps.len() {
// c:1299 — if (!strcmp(n, p->pat)) {
if patcomps[i].0 == n {
// c:1300-1303 — splice: if (q) q->next = p->next; else patcomps = p->next;
// c:1304 — zsfree(p->pat); (Rust: Drop handles)
// c:1305 — freecompctl(p->cc); (Rust: Drop handles Arc<Compctl>)
// c:1306 — free(p); (Rust: Vec::remove drops)
patcomps.remove(i);
// c:1308 — break;
break;
}
}
}
/// Process the parsed compctl into the table.
/// Port of `compctl_process_cc(char **s, Compctl cc)` from Src/Zle/compctl.c:1315 —
/// installs the spec into compctltab (or patcomps when the name is a
/// pattern), or removes entries when COMP_REMOVE is set (the `+` form).
/// WARNING: param names don't match C — Rust=(cc) vs C=(s, cc)
pub(crate) fn compctl_process_cc(s: &[String], cc: Arc<Compctl>) {
if (CCLIST.with(|c| c.get()) & COMP_REMOVE) != 0 {
// Delete entries for the commands listed
for name in s {
let (ispat, n) = compctl_name_pat(name);
if ispat {
delpatcomp(&n);
} else if let Some(map) = COMPCTL_TAB.write().unwrap().as_mut() {
map.remove(&n);
}
}
} else {
// Add the compctl just read to the hash table
for name in s {
let (ispat, n) = compctl_name_pat(name);
if ispat {
delpatcomp(&n);
PATCOMPS.write().unwrap().insert(0, (n, cc.clone())); // pc->next = patcomps
} else {
let mut g = COMPCTL_TAB.write().unwrap();
g.get_or_insert_with(HashMap::new).insert(n, cc.clone());
}
}
}
}
/// Print a `compctl`.
/// Port of `printcompctl(char *s, Compctl cc, int printflags, int ispat)` from Src/Zle/compctl.c:1359 (~190 lines).
///
/// Emits the `compctl` command line (`-L` style) or the plain listing
/// entry that re-creates the spec: the flag letters, the flags taking
/// an argument, the `-x 'cond' flags ... --` extended part and the `+`
/// xor chain. `s` is the command name (or pattern), NULL (`None`) for
/// the recursive calls that print only the flags.
///
/// Output goes through `std::io::stdout()` directly (C: `printf` /
/// `putchar` on `stdout`).
pub(crate) fn printcompctl(s: Option<&str>, cc: &Compctl, printflags: i32, ispat: bool) {
use crate::ported::zsh_h::{PRINT_LIST, PRINT_TYPE};
use std::io::Write;
macro_rules! out {
($($a:tt)*) => {{
let _ = write!(std::io::stdout(), $($a)*);
}};
}
// c:1399 printqt — the quote-escaping shared with the other printers.
let printqt = |q: &str| {
let rcquotes = crate::ported::zsh_h::isset(crate::ported::zsh_h::RCQUOTES);
for ch in q.chars() {
if ch == '\'' {
out!("{}", if rcquotes { "''" } else { "'\\''" });
} else {
out!("{}", ch);
}
}
};
// builtin.c printif: ` -c ` followed by the quoted string, if any.
let printif = |q: &Option<String>, c: char| {
if let Some(q) = q {
out!(" -{} {}", c, crate::ported::utils::quotedzputs(q));
}
};
let is_static = |slot: &Mutex<Option<Arc<Compctl>>>| {
slot.lock()
.unwrap()
.as_ref()
.is_some_and(|a| std::ptr::eq(Arc::as_ptr(a), cc))
};
const CSS: &str = "fcqovbAIFpEjrzBRGudeNOZUnQmw/"; // c:1361
const MSS: &[u8] = b" pcCwWsSnNmrRq"; // c:1362
let mut t: u64 = 0x7fffffff; // c:1363
let mut flags: u64 = cc.mask;
let flags2: u64 = cc.mask2;
// Printflags is used outside the standard compctl commands
if (printflags & PRINT_LIST) != 0 {
CCLIST.with(|c| c.set(c.get() | COMP_LIST));
} else if (printflags & PRINT_TYPE) != 0 {
CCLIST.with(|c| c.set(c.get() & !COMP_LIST));
}
if (flags & CC_EXCMDS) != 0 && (flags & CC_DISCMDS) == 0 {
flags &= !CC_EXCMDS;
}
// If showmask is non-zero, then print only those commands with that
// flag set.
let showmask = SHOWMASK.with(|c| c.get());
if showmask != 0 && (flags & showmask) == 0 {
return;
}
// Temporarily clear showmask in case we make recursive calls to
// printcompctl.
let oldshowmask = showmask;
SHOWMASK.with(|c| c.set(0));
// print either command name or start of compctl command itself
if let Some(s) = s {
if (CCLIST.with(|c| c.get()) & COMP_LIST) != 0 {
out!("compctl");
if is_static(&CC_COMPOS) {
out!(" -C");
}
if is_static(&CC_DEFAULT) {
out!(" -D");
}
if is_static(&CC_FIRST) {
out!(" -T");
}
} else if ispat {
let p = crate::ported::lex::untokenize(s);
out!("{}", crate::ported::utils::quotedzputs(&p));
} else {
out!(
"{}",
crate::ported::utils::quotedzputs(&crate::ported::utils::quotestring(
s,
crate::ported::zsh_h::QT_BACKSLASH
))
);
}
}
// loop through flags w/o args that are set, printing them if so
if (flags & t) != 0 || (flags2 & (CC_UNIQALL | CC_UNIQCON)) != 0 {
out!(" -");
if (flags & (CC_ALREG | CC_ALGLOB)) == (CC_ALREG | CC_ALGLOB) {
out!("a");
flags &= !(CC_ALREG | CC_ALGLOB);
}
for ch in CSS.chars() {
if (flags & t & 1) != 0 {
out!("{}", ch);
}
flags >>= 1;
t >>= 1;
}
if (flags2 & CC_UNIQALL) != 0 {
out!("1");
} else if (flags2 & CC_UNIQCON) != 0 {
out!("2");
}
}
if (flags2 & (CC_XORCONT | CC_PATCONT | CC_DEFCONT)) != 0 {
out!(" -t");
if (flags2 & CC_XORCONT) != 0 {
out!("+");
}
if (flags2 & CC_PATCONT) != 0 {
out!("-");
}
if (flags2 & CC_DEFCONT) != 0 {
out!("x");
}
} else if (flags2 & CC_CCCONT) == 0 {
out!(" -tn");
}
// now flags with arguments
printif(&cc.mstr, 'M');
if (flags2 & CC_NOSORT) != 0 {
printif(&cc.gname, 'V');
} else {
printif(&cc.gname, 'J');
}
printif(&cc.keyvar, 'k');
printif(&cc.func, 'K');
printif(
&cc.explain,
if (cc.mask & CC_EXPANDEXPL) != 0 { 'Y' } else { 'X' },
);
printif(&cc.ylist, 'y');
printif(&cc.prefix, 'P');
printif(&cc.suffix, 'S');
printif(&cc.glob, 'g');
printif(&cc.str, 's');
printif(&cc.subcmd, 'l');
printif(&cc.substr, 'h');
printif(&cc.withd, 'W');
if let Some(hpat) = &cc.hpat {
out!(" -H {} {}", cc.hnum, crate::ported::utils::quotedzputs(hpat));
}
// now the -x ... -- extended completion part
if cc.ext.is_some() {
let mut cc2: Option<Arc<Compctl>> = cc.ext.clone();
out!(" -x");
while let Some(c2) = cc2 {
// loop over conditions
out!(" '");
let mut c: Option<&Compcond> = c2.cond.as_deref();
while let Some(cn) = c {
// loop over or's
let o = cn.or.as_deref();
let mut a: Option<&Compcond> = Some(cn);
while let Some(cx) = a {
// loop over and's
out!("{}", MSS[cx.typ as usize] as char);
for i in 0..cx.n as usize {
// for all [...]'s of a given condition
out!("[");
match (&cx.u, cx.typ) {
(CompcondData::R { a, b }, ty) if ty == CCT_POS || ty == CCT_NUMWORDS => {
out!("{},{}", a[i], b[i]);
}
(CompcondData::S { s, .. }, ty)
if ty == CCT_CURSUF || ty == CCT_CURPRE || ty == CCT_QUOTE =>
{
printqt(&s[i]);
}
(CompcondData::L { a, b }, ty)
if ty == CCT_RANGESTR || ty == CCT_RANGEPAT =>
{
printqt(&a[i]);
out!(",");
printqt(b[i].as_deref().unwrap_or("")); // c:1492
}
(CompcondData::S { p, s }, _) => {
out!("{},", p[i]);
printqt(&s[i]);
}
_ => {}
}
out!("]");
}
a = cx.and.as_deref();
if a.is_some() {
out!(" ");
}
}
c = o;
if c.is_some() {
out!(" , ");
}
}
out!("'");
// now print the flags for the current condition (C: cc2->cond
// is cleared around the call)
let mut flagsonly = (*c2).clone();
flagsonly.cond = None;
printcompctl(None, &flagsonly, 0, false);
cc2 = c2.next.clone();
if cc2.is_some() {
out!(" -");
}
}
if (CCLIST.with(|c| c.get()) & COMP_LIST) != 0 {
out!(" --");
}
}
if let Some(xor) = &cc.xor {
// print xor'd (+) completions
out!(" +");
// `cc->xor != &cc_default`; a stale link to a replaced cc_default
// is told apart by the static's refc.
let xor_is_default = xor.refc == 10000
|| CC_DEFAULT
.lock()
.unwrap()
.as_ref()
.is_some_and(|d| Arc::ptr_eq(d, xor));
if !xor_is_default {
printcompctl(None, xor, 0, false);
}
}
if let Some(s) = s {
if (CCLIST.with(|c| c.get()) & COMP_LIST) != 0
&& !is_static(&CC_COMPOS)
&& !is_static(&CC_DEFAULT)
&& !is_static(&CC_FIRST)
{
if s.starts_with('-') || s.starts_with('+') {
out!(" -");
}
out!(" ");
let p = crate::ported::lex::untokenize(s);
if ispat {
out!("{}", crate::ported::utils::quotedzputs(&p));
} else {
out!(
"{}",
crate::ported::utils::quotedzputs(&crate::ported::utils::quotestring(
&p,
crate::ported::zsh_h::QT_BACKSLASH
))
);
}
}
out!("\n");
}
SHOWMASK.with(|c| c.set(oldshowmask));
}
/// Port of `printcompctlp(HashNode hn, int printflags)` from Src/Zle/compctl.c:1550 —
/// the ScanFunc needed for use by scanhashtable().
/// WARNING: param names don't match C — Rust=(name, hn, printflags) vs C=(hn, printflags)
pub(crate) fn printcompctlp(name: &str, hn: &Compctl, printflags: i32) {
printcompctl(Some(name), hn, printflags, false);
}
/// `compctl` builtin entry point.
/// Port of `bin_compctl(char *name, char **argv, UNUSED(Options ops), UNUSED(int func))` from Src/Zle/compctl.c:1562 (~110 lines).
/// WARNING: param names don't match C — Rust=(argv) vs C=(name, argv, ops, func)
pub fn bin_compctl(
name: &str,
argv: &[String],
_ops: &crate::ported::zsh_h::options,
_func: i32,
) -> i32 {
let mut argv: Vec<String> = argv.to_vec();
let mut ret: i32 = 0;
let mut cc = Compctl::default();
// clear static flags
CCLIST.with(|c| c.set(0));
SHOWMASK.with(|c| c.set(0));
// Parse all the arguments
if !argv.is_empty() {
// Let's see if this is a global matcher definition.
let gret = get_gmatcher(name, &argv);
if gret != 0 {
return gret - 1;
}
if get_compctl(name, &mut argv, &mut cc, true, false, 0) != 0 {
return 1;
}
// remember flags for printing
let mut showmask = cc.mask;
if (showmask & CC_EXCMDS) != 0 && (showmask & CC_DISCMDS) == 0 {
showmask &= !CC_EXCMDS;
}
SHOWMASK.with(|c| c.set(showmask));
}
let cclist = CCLIST.with(|c| c.get());
// The statics cc_compos / cc_default / cc_first, as printed below.
let cc_compos = CC_COMPOS.lock().unwrap().clone().unwrap_or_default();
let cc_default = CC_DEFAULT.lock().unwrap().clone().unwrap_or_default();
let cc_first = CC_FIRST.lock().unwrap().clone().unwrap_or_default();
// If no commands and no -C, -T, or -D, print all the compctl's. If
// some flags (other than -C, -T, or -D) were given, then only print
// compctl containing those flags.
if argv.is_empty() && (cclist & (COMP_SPECIAL | COMP_LISTMATCH)) == 0 {
let pats = PATCOMPS.read().unwrap().clone();
for (pat, pcc) in &pats {
printcompctl(Some(pat), pcc, 0, true);
}
let tab: Vec<(String, Arc<Compctl>)> = {
let g = COMPCTL_TAB.read().unwrap();
let mut v: Vec<(String, Arc<Compctl>)> = g
.as_ref()
.map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
.unwrap_or_default();
v.sort_by(|a, b| a.0.cmp(&b.0)); // scanhashtable(.., sorted = 1, ..)
v
};
for (n, tcc) in &tab {
printcompctlp(n, tcc, 0);
}
let listing = (cclist & COMP_LIST) != 0;
printcompctl(Some(if listing { "" } else { "COMMAND" }), &cc_compos, 0, false);
printcompctl(Some(if listing { "" } else { "DEFAULT" }), &cc_default, 0, false);
printcompctl(Some(if listing { "" } else { "FIRST" }), &cc_first, 0, false);
print_gmatcher((cclist & COMP_LIST) as i32);
return ret;
}
// If we're listing and we've made it to here, then there are
// arguments or a COMP_SPECIAL flag (-D, -C, -T), so print only those.
if (cclist & COMP_LIST) != 0 {
SHOWMASK.with(|c| c.set(0));
for ptr in &argv {
let (ispat, n) = compctl_name_pat(ptr);
let mut found = false;
if ispat {
let pats = PATCOMPS.read().unwrap().clone();
if let Some((pat, pcc)) = pats.iter().find(|(pat, _)| *pat == n) {
printcompctl(Some(pat), pcc, 0, true);
found = true;
}
} else {
let hn = COMPCTL_TAB
.read()
.unwrap()
.as_ref()
.and_then(|m| m.get(&n).cloned());
if let Some(hn) = hn {
printcompctlp(&n, &hn, 0);
found = true;
}
}
if !found {
crate::ported::utils::zwarnnam(name, &format!("no compctl defined for {}", n));
ret = 1;
}
}
if (cclist & COMP_COMMAND) != 0 {
printcompctl(Some(""), &cc_compos, 0, false);
}
if (cclist & COMP_DEFAULT) != 0 {
printcompctl(Some(""), &cc_default, 0, false);
}
if (cclist & COMP_FIRST) != 0 {
printcompctl(Some(""), &cc_first, 0, false);
}
if (cclist & COMP_LISTMATCH) != 0 {
print_gmatcher(COMP_LIST);
}
return ret;
}
// Assign the compctl to the commands given
if !argv.is_empty() {
if (cclist & COMP_SPECIAL) != 0 {
// Ideally we'd handle this properly, setting both the special
// and normal completions. For the moment, this is better than
// silently failing.
crate::ported::utils::zwarnnam(name, "extraneous commands ignored");
} else {
compctl_process_cc(&argv, Arc::new(cc));
}
}
ret
}
/// Port of `CFN_FIRST` from `compctl.c:1672`. Internal flag for
/// `printcompctl` — skip the cc_first per-table override.
pub const CFN_FIRST: i32 = 1; // c:1672
/// Port of `CFN_DEFAULT` from `compctl.c:1673`. Skip cc_default.
pub const CFN_DEFAULT: i32 = 2; // c:1673
/// `compcall` builtin entry point.
/// Port of `bin_compcall(char *name, UNUSED(char **argv), Options ops, UNUSED(int func))` from Src/Zle/compctl.c:1676.
///
/// Re-invokes the completion machinery from inside a `-K` function.
/// Per c:1680, `incompfunc` must be 1 (we're inside a completion
/// function); else error. Then dispatches to makecomplistctl with
/// CFN_FIRST / CFN_DEFAULT bits cleared per `-T` / `-D` opts.
///
/// CFN_* bits (c:1672-1673):
/// CFN_FIRST = 1 — skip cc_first
/// CFN_DEFAULT = 2 — skip cc_default
/// WARNING: param names don't match C — Rust=(argv) vs C=(name, argv, ops, func)
pub fn bin_compcall(
name: &str,
argv: &[String],
ops: &crate::ported::zsh_h::options,
_func: i32,
) -> i32 {
// C: c:1680-1683 — incompfunc check
let incompfunc = INCOMPFUNC.load(std::sync::atomic::Ordering::Relaxed);
if incompfunc != 1 {
// c:1681 — `zwarnnam(name, "can only be called from completion function")`.
crate::ported::utils::zwarnnam(name, "can only be called from completion function");
return 1; // c:1682
}
// C: c:1686-1687 — option flags. The BUILTIN spec is
// `BUILTIN("compcall", 0, bin_compcall, 0, 0, 0, "TD", NULL)`
// (c:4006), so the real builtin path arrives with `-T`/`-D` already
// parsed into `ops`. `_default` (Completion/Zsh/Context/_default sh:12
// `compcall "$opt[@]"`) reaches this port through a direct Rust call
// that has no flag-parsing shim in front of it, so accept the same
// letters spelled out in `argv` too.
let t_set = crate::ported::zsh_h::OPT_ISSET(ops, b'T') || argv.iter().any(|a| a == "-T");
let d_set = crate::ported::zsh_h::OPT_ISSET(ops, b'D') || argv.iter().any(|a| a == "-D");
let flags = (if t_set { 0 } else { CFN_FIRST }) // c:1686
| (if d_set { 0 } else { CFN_DEFAULT }); // c:1687
// c:1689 — `return ret`. The status is the CONTRACT: `compcall`
// reports non-zero when a compctl was found and used, and
// `_default` sh:12 (`compcall "$opt[@]" || return 0`) reads it to
// decide whether the compctl engine already answered or `_files`
// still has to run. Dropping it and returning a flat 0 made every
// caller take the "nothing was found" branch.
makecomplistctl(flags) // c:1689
}
/// Hook for completion-list build start.
/// Port of `ccmakehookfn(UNUSED(Hookdef dummy), struct ccmakedat *dat)` from Src/Zle/compctl.c:1763.
///
/// Registered on `compctl_make` by `boot_`. Runs `makecomplistglobal`
/// once per entry of the global `-M` matcher chain (or once with no
/// matcher when there is none), stopping at the first pass that yields
/// matches. `dat` points at a [`Ccmakedat`]; `dat.lst` is set to 0 when
/// matches were produced, 1 otherwise.
pub(crate) fn ccmakehookfn(_dummy: *mut crate::ported::zsh_h::hookdef, dat: *mut std::ffi::c_void) -> i32 {
use crate::ported::zle::compcore as cc;
use std::sync::atomic::Ordering;
// SAFETY: the hook dispatcher passes the `Ccmakedat` it owns (c:1042).
let dat: &mut Ccmakedat = unsafe { &mut *(dat as *mut Ccmakedat) };
let os = dat.str.clone().unwrap_or_default(); // c:1765 os = s = dat->str
let incmd = dat.incmd; // c:1766
let lst = dat.lst; // c:1766
let onm = cc::nmatches.load(Ordering::Relaxed); // c:1769
let odm = cc::diffmatches.load(Ordering::Relaxed); // c:1769
let osi = crate::ported::utils::movefd(0); // c:1769
crate::ported::signals_h::queue_signals(); // c:1772
/* We build a copy of the list of matchers to use to make sure that this
* works even if a shell function called from the completion code changes
* the global matchers. */
// The Cmlist nodes are immutable and shared by `Arc`, so holding the
// head is the copy.
let mut m: Option<Arc<Cmlist>> = CMATCHER.read().ok().and_then(|g| g.clone()); // c:1778
if let Ok(mut mlist) = cc::matchers
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
{
let mut cur = m.clone();
while let Some(node) = cur {
mlist.push(node.matcher.clone()); // c:1790 addlinknode(matchers, m->matcher)
cur = node.next.clone();
}
}
/* Walk through the global matchers. */
loop {
// c:1802
if let Ok(mut g) = cc::bmatchers.get_or_init(|| Mutex::new(None)).lock() {
*g = None; // c:1803 bmatchers = NULL
}
if let Some(mm) = m.as_ref() {
// c:1804
let ms = Arc::new(Cmlist {
next: None, // c:1805
matcher: mm.matcher.clone(), // c:1806
str: String::new(),
});
if let Ok(mut g) = cc::mstack.get_or_init(|| Mutex::new(None)).lock() {
*g = Some(ms); // c:1807 mstack = &ms
}
/* Store the matchers used in the bmatchers list which is used
* when building new parts for the string to insert into the
* line. */
crate::ported::zle::compmatch::add_bmatchers(Some(&*mm.matcher)); // c:1813
} else if let Ok(mut g) = cc::mstack.get_or_init(|| Mutex::new(None)).lock() {
*g = None; // c:1815 mstack = NULL
}
if let Ok(mut g) = cc::ainfo.get_or_init(|| Mutex::new(None)).lock() {
*g = Some(Aminfo::default()); // c:1817 ainfo = hcalloc(...)
}
if let Ok(mut g) = cc::fainfo.get_or_init(|| Mutex::new(None)).lock() {
*g = Some(Aminfo::default()); // c:1818 fainfo = hcalloc(...)
}
if let Ok(mut g) = cc::freecl.get_or_init(|| Mutex::new(None)).lock() {
*g = None; // c:1820 freecl = NULL
}
if crate::ported::zle::zle_tricky::VALIDLIST.load(Ordering::Relaxed) == 0 {
crate::ported::zle::zle_tricky::LASTAMBIG.store(0, Ordering::Relaxed); // c:1823
}
if let Ok(mut g) = cc::amatches.get_or_init(|| Mutex::new(Vec::new())).lock() {
g.clear(); // c:1824 amatches = NULL
}
cc::mnum.store(0, Ordering::Relaxed); // c:1825
cc::unambig_mnum.store(-1, Ordering::Relaxed); // c:1826
if let Ok(mut g) = cc::isuf.get_or_init(|| Mutex::new(String::new())).lock() {
g.clear(); // c:1827 isuf = NULL
}
cc::insmnum.store(
crate::ported::zle::zle_main::ZMOD.lock().map(|g| g.mult).unwrap_or(1),
Ordering::Relaxed,
); // c:1828 insmnum = zmult
cc::oldlist.store(0, Ordering::Relaxed); // c:1834
cc::oldins.store(0, Ordering::Relaxed); // c:1834
cc::begcmgroup(Some("default"), 0); // c:1835
crate::ported::zle::zle_tricky::MENUCMP.store(0, Ordering::Relaxed); // c:1836
cc::menuacc.store(0, Ordering::Relaxed); // c:1836
cc::newmatches.store(0, Ordering::Relaxed); // c:1836
cc::onlyexpl.store(0, Ordering::Relaxed); // c:1836
CCUSED.with(|r| r.borrow_mut().clear()); // c:1838 ccused = newlinklist()
let s = crate::ported::mem::dupstring(&os); // c:1841
makecomplistglobal(&s, incmd != 0, lst, 0); // c:1842
cc::endcmgroup(None); // c:1843
let have_amatches = cc::amatches
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.map(|g| !g.is_empty())
.unwrap_or(false);
if have_amatches && cc::oldlist.load(Ordering::Relaxed) == 0 {
// c:1845 — remember the compctls used for the next cycle.
*LASTCCUSED.lock().unwrap() = CCUSED.with(|r| r.borrow().clone()); // c:1846-1851
}
if cc::oldlist.load(Ordering::Relaxed) != 0 {
// c:1857
cc::nmatches.store(onm, Ordering::Relaxed); // c:1858
cc::diffmatches.store(odm, Ordering::Relaxed); // c:1859
crate::ported::zle::zle_tricky::VALIDLIST.store(1, Ordering::Relaxed); // c:1860
let last = cc::lastmatches
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.map(|g| g.clone())
.unwrap_or_default();
if let Ok(mut g) = cc::amatches.get_or_init(|| Mutex::new(Vec::new())).lock() {
*g = last; // c:1861 amatches = lastmatches
}
let last_l = cc::lastlmatches
.get_or_init(|| Mutex::new(None))
.lock()
.ok()
.and_then(|g| g.clone());
if let Ok(mut g) = cc::lmatches.get_or_init(|| Mutex::new(None)).lock() {
*g = last_l; // c:1866 lmatches = lastlmatches
}
let pm = cc::pmatches
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.map(|mut g| std::mem::take(&mut *g))
.unwrap_or_default();
if !pm.is_empty() {
// c:1867
cc::freematches(pm, 1); // c:1868
cc::hasperm.store(0, Ordering::Relaxed); // c:1870
}
crate::ported::utils::redup(osi, 0); // c:1872
dat.lst = 0; // c:1874
crate::ported::signals_h::unqueue_signals(); // c:1875
return 0; // c:1876
}
let old_last = cc::lastmatches
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.map(|mut g| std::mem::take(&mut *g))
.unwrap_or_default();
if !old_last.is_empty() {
// c:1878
cc::freematches(old_last, 1); // c:1879
}
cc::permmatches(1); // c:1882
let pm = cc::pmatches
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.map(|mut g| std::mem::take(&mut *g))
.unwrap_or_default();
if let Ok(mut g) = cc::amatches.get_or_init(|| Mutex::new(Vec::new())).lock() {
*g = pm.clone(); // c:1883 amatches = pmatches
}
cc::lastpermmnum.store(cc::permmnum.load(Ordering::Relaxed), Ordering::Relaxed); // c:1884
cc::lastpermgnum.store(cc::permgnum.load(Ordering::Relaxed), Ordering::Relaxed); // c:1885
if let Ok(mut g) = cc::lastmatches.get_or_init(|| Mutex::new(Vec::new())).lock() {
*g = pm; // c:1887 lastmatches = pmatches
}
let lm = cc::lmatches
.get_or_init(|| Mutex::new(None))
.lock()
.ok()
.and_then(|g| g.clone());
if let Ok(mut g) = cc::lastlmatches.get_or_init(|| Mutex::new(None)).lock() {
*g = lm; // c:1888 lastlmatches = lmatches
}
// c:1889 pmatches = NULL (taken above)
cc::hasperm.store(0, Ordering::Relaxed); // c:1890
cc::hasoldlist.store(1, Ordering::Relaxed); // c:1891
if cc::nmatches.load(Ordering::Relaxed) != 0 && errflag.load(Ordering::Relaxed) == 0 {
// c:1893
crate::ported::zle::zle_tricky::VALIDLIST.store(1, Ordering::Relaxed); // c:1894
crate::ported::utils::redup(osi, 0); // c:1896
dat.lst = 0; // c:1898
crate::ported::signals_h::unqueue_signals(); // c:1899
return 0; // c:1900
}
// c:1902 — `if (!m || !(m = m->next)) break;`
m = match m.and_then(|node| node.next.clone()) {
Some(next) => Some(next),
None => break,
};
errflag.fetch_and(!crate::ported::utils::ERRFLAG_ERROR, Ordering::Relaxed); // c:1905
}
crate::ported::utils::redup(osi, 0); // c:1907
dat.lst = 1; // c:1908
crate::ported::signals_h::unqueue_signals(); // c:1910
0 // c:1912
}
/// Hook for completion-list build cleanup.
/// Port of `cccleanuphookfn(UNUSED(Hookdef dummy), UNUSED(void *dat))` from Src/Zle/compctl.c:1910.
///
/// Called via `addhookfunc("compctl_cleanup", cccleanuphookfn)` at
/// boot_. The C body nulls the ccused/ccstack file-statics.
pub(crate) fn cccleanuphookfn(
_dummy: *mut crate::ported::zsh_h::hookdef,
_dat: *mut std::ffi::c_void,
) -> i32 {
CCUSED.with(|r| r.borrow_mut().clear()); // c:1912 ccused = NULL
CCSTACK.with(|r| r.borrow_mut().clear()); // c:1912 ccstack = NULL
0
}
/// Direct port of `void maketildelist(void)` from `Src/Zle/compctl.c:2055`.
/// Fills the named-directory table and adds every entry as a match.
/// The C body is:
/// ```c
/// nameddirtab->filltable(nameddirtab);
/// scanhashtable(nameddirtab, 0, 0, 0, addhnmatch, 0);
/// ```
/// `addhnmatch` formats the entry name with a leading `~`. The Rust
/// port iterates the live `nameddirtab` from `hashnameddir.rs` and
/// calls `addmatch` for each `~name`.
pub(crate) fn maketildelist() {
// c:2055
// c:2058 — `nameddirtab->filltable(nameddirtab)` adds every username
// from the passwd database (getpwent) to the named-dir table, so bare
// `~<Tab>` offers `~user` for all users plus any `hash -d` named dirs.
crate::ported::hashnameddir::fillnameddirtable();
// c:2060 — scanhashtable(nameddirtab, 0, (addwhat==-1) ? 0 : ND_USERNAME,
// …): with addwhat==-1 (bare `~` / CC_NAMED) include named dirs AND
// usernames; otherwise restrict to usernames.
let only_users = ADDWHAT.with(|c| c.get()) != -1;
let uname_bit = crate::ported::zsh_h::ND_USERNAME as i32;
let entries: Vec<String> = crate::ported::hashnameddir::nameddirtab()
.lock()
.ok()
.map(|t| {
t.iter()
.filter(|(_, nd)| !only_users || (nd.node.flags & uname_bit) != 0)
.map(|(n, _)| n.clone())
.collect()
})
.unwrap_or_default();
// c:2060 — scanhashtable callback `addhnmatch` (compctl.c:2092) adds the
// bare name; the leading `~` comes from the caller's `ipre = "~"` (c:3404),
// so the name matches the file prefix (`~roo` → fpre `roo` → `root`) and
// the `~` is re-attached on insertion.
for name in entries {
addhnmatch(&name, 0);
}
}
// Are we inside a completion function? Mirrors the C `incompfunc`
// global from Src/Zle/zle_tricky.c:46 — there is exactly ONE such
// global in C, set to 1 by `callcompfunc` (c:838) before the user's
// completion widget runs.
//
// This file used to keep its own private thread-local of the same
// name, which shadowed `zle::complete::INCOMPFUNC` (the one
// `callcompfunc` actually writes). It was therefore always 0 during
// a real completion, so `compcall` took its `incompfunc != 1` error
// path — `_default`'s compctl bridge could never add matches and
// `rustup <TAB>` lost the whole compctl-side candidate list zsh
// shows. Alias the single global instead.
use crate::ported::zle::complete::INCOMPFUNC;
/// `compctl -K`'s bound `compctlread` callback.
/// Port of `compctlread(char *name, char **args, Options ops, char *reply)` from Src/Zle/compctl.c:190.
///
/// The function reads input for the `read` builtin invoked from
/// inside a completion function (e.g. `compctl -K myfunc` calls
/// `read -l` etc.). Replaces fallback_compctlread when the compctl
/// module is loaded. Dispatches on -l/-n/-e/-E/-A:
/// -l → the current line goes to `reply` as a scalar
/// -ln → the cursor offset into the line (1-based)
/// -n → the cursor position within the current word (1-based)
/// -A → the words of the line go to `reply` as one array
/// -e/-E → print instead of / in addition to assigning
pub(crate) fn compctlread(
name: &str,
args: &[String],
ops: &crate::ported::zsh_h::options,
reply: &str,
) -> i32 {
use crate::ported::zsh_h::OPT_ISSET;
use std::io::Write;
/* only allowed to be called for completion */
if !INCOMPCTLFUNC.with(|c| c.get()) {
// c:195
crate::ported::utils::zwarnnam(name, "option valid only in functions called via compctl");
return 1; // c:197
}
crate::ported::zle::zle_h::METACHECK(); // c:200
let zlemetacs = crate::ported::zle::compcore::ZLEMETACS.load(std::sync::atomic::Ordering::Relaxed);
let clwpos = crate::ported::zle::zle_tricky::CLWPOS.load(std::sync::atomic::Ordering::Relaxed);
let print_it = OPT_ISSET(ops, b'e') || OPT_ISSET(ops, b'E');
let mut stdout = std::io::stdout();
if OPT_ISSET(ops, b'l') {
// c:202
/*
* -ln gives the index of the word the cursor is currently on, which
* is available in zlemetacs (but remember that Zsh counts from one,
* not zero!)
*/
if OPT_ISSET(ops, b'n') {
// c:208
if print_it {
let _ = writeln!(stdout, "{}", zlemetacs + 1); // c:212
}
if !OPT_ISSET(ops, b'e') {
crate::ported::params::setsparam(reply, &(zlemetacs + 1).to_string()); // c:215
}
return 0; // c:217
}
/* without -n, the current line is assigned to the given parameter as a
scalar */
let zlemetaline = crate::ported::zle::compcore::ZLEMETALINE
.get()
.and_then(|m| m.lock().ok().map(|g| g.clone()))
.unwrap_or_default();
if print_it {
// c:221
crate::ported::utils::zputs(&zlemetaline, &mut stdout); // c:222
let _ = writeln!(stdout); // c:223
}
if !OPT_ISSET(ops, b'e') {
crate::ported::params::setsparam(reply, &zlemetaline); // c:226
}
} else {
let clwords: Vec<String> = crate::ported::zle::zle_tricky::CLWORDS
.lock()
.map(|g| g.clone())
.unwrap_or_default();
let clwnum = clwords.len();
/* -cn gives the current cursor position within the current word, which
is available in clwpos (but remember that Zsh counts from one, not
zero!) */
if OPT_ISSET(ops, b'n') {
// c:236
if print_it {
let _ = writeln!(stdout, "{}", clwpos + 1); // c:240
}
if !OPT_ISSET(ops, b'e') {
crate::ported::params::setsparam(reply, &(clwpos + 1).to_string()); // c:243
}
return 0; // c:245
}
/* without -n, the words of the current line are assigned to the given
parameters separately */
if OPT_ISSET(ops, b'A') && !OPT_ISSET(ops, b'e') {
// c:249
/* the -A option means that one array is specified, instead of
many parameters */
crate::ported::params::setaparam(reply, clwords.clone()); // c:255-258
return 0; // c:259
}
if print_it {
// c:261
for w in &clwords {
crate::ported::utils::zputs(w, &mut stdout); // c:263
let _ = writeln!(stdout); // c:264
}
if OPT_ISSET(ops, b'e') {
return 0; // c:268
}
}
let mut reply = reply.to_string();
let mut rest = args;
let mut i = 0usize;
while i < clwnum && !rest.is_empty() {
// c:271
crate::ported::params::setsparam(&reply, &clwords[i]);
reply = rest[0].clone();
rest = &rest[1..];
i += 1;
}
let buf = if i < clwnum {
// c:274 — the remaining words, space-joined, go to the last name.
clwords[i..].join(" ")
} else {
String::new() // c:287
};
crate::ported::params::setsparam(&reply, &buf); // c:288
}
0 // c:290
}
// True iff we're inside a function called via compctl -K. Mirrors
// the C `incompctlfunc` global from Src/Zle/zle_main.c:54
// (`mod_export int incompctlfunc`). Per PORT_PLAN.md bucket-1: each
// worker thread runs its own completion, so the in-compctl-fn flag
// is per-evaluator — `thread_local!` preserves zsh's per-process
// semantic per-worker without cross-thread leakage.
thread_local! {
pub(crate) static INCOMPCTLFUNC: std::cell::Cell<bool> =
const { std::cell::Cell::new(false) };
}
/// Hash-pattern match for `compctl -x` n[…] / N[…] conditions.
/// Port of `getcpat(char *str, int cpatindex, char *cpat, int class)` from Src/Zle/compctl.c:2068.
///
/// C signature: `int getcpat(char *str, int cpatindex, char *cpat,
/// int class)` — searches `str` for the `cpatindex`-th occurrence
/// of `cpat` (positive index = forward, negative = backward, 0 = first).
/// `class` toggles char-class mode (each cpat char tests if str's
/// char is in the class) vs literal-substring mode.
///
/// Returns the 1-based index of the match end, or -1 if not found.
/// WARNING: param names don't match C — Rust=(cpatindex, cpat, class) vs C=(str, cpatindex, cpat, class)
pub(crate) fn getcpat(str: &str, cpatindex: i32, cpat: &str, class: i32) -> i32 {
// C: c:2073 — empty string → -1
if str.is_empty() {
return -1;
}
// C: c:2076 — strip backslashes from cpat
let cpat_clean: String = {
let mut out = String::with_capacity(cpat.len());
let mut chars = cpat.chars().peekable();
while let Some(c) = chars.next() {
if c == '\\' {
if let Some(&nx) = chars.peek() {
out.push(nx);
chars.next();
continue;
}
}
out.push(c);
}
out
};
// C: c:2078-2081 — index normalization
let (mut idx, backward) = if cpatindex == 0 {
(1_i32, false)
} else if cpatindex < 0 {
(-cpatindex, true)
} else {
(cpatindex, false)
};
let str_chars: Vec<char> = str.chars().collect();
let cpat_chars: Vec<char> = cpat_clean.chars().collect();
let n = str_chars.len();
// C: c:2083-2095 — the search loop, walks forward or backward.
let positions: Vec<usize> = if backward {
(0..n).rev().collect()
} else {
(0..n).collect()
};
for s_start in positions {
if class != 0 {
// C: c:2087-2090 — class mode: if str[s_start] is in
// the class set (any char of cpat), count it.
let sc = str_chars[s_start];
if cpat_chars.iter().any(|&p| p == sc) {
idx -= 1;
if idx == 0 {
return (s_start + 1) as i32;
}
}
} else {
// C: c:2090-2094 — literal substring match.
let mut t = s_start;
let mut p = 0;
while t < n && p < cpat_chars.len() && str_chars[t] == cpat_chars[p] {
t += 1;
p += 1;
}
if p == cpat_chars.len() {
idx -= 1;
if idx == 0 {
return t as i32;
}
}
}
}
-1
}
/// Dump every entry of a hash table as a match.
/// Port of `dumphashtable(HashTable ht, int what)` from Src/Zle/compctl.c:2106.
///
/// C body: sets `addwhat = what`, iterates every node in `ht->nodes`,
/// calls `addmatch(dupstring(hn->nam), (char *) hn)`. Rust takes the
/// table's (name, node) pairs since the hash-table abstractions differ.
/// WARNING: param names don't match C — Rust=(what) vs C=(ht, what)
pub(crate) fn dumphashtable<I: IntoIterator<Item = (String, AddmatchNode)>>(ht: I, what: i32) {
// C: c:2111 — set addwhat global before the iteration
ADDWHAT.with(|c| c.set(what));
for (nam, hn) in ht {
addmatch(&nam, Some(&hn));
}
}
/// `addwhat` special-value constants — port of the negative-int
/// dispatch values documented in Src/Zle/compctl.c:1940-1951:
/// ADDWHAT_FILES_OTHER = -1 (other file specs: ~/=...)
/// ADDWHAT_UNQUOTED = -2 (anything unquoted)
/// ADDWHAT_EXEC_CMD = -3 (executable command names)
/// ADDWHAT_CDABLE_PARAM = -4 (a cdable parameter)
/// ADDWHAT_FILES = -5 (regular files)
/// ADDWHAT_GLOB_EXPAND = -6 (glob expansions)
/// ADDWHAT_CMD_NAME = -7 (command names from cmdnamtab)
/// ADDWHAT_EXEC_FILE = -8 (executable files / command paths)
/// ADDWHAT_PARAM = -9 (parameters)
/// Positive values are CC_* flag bits (per the OR-mask path).
// `addwhat` accept-thread values are C bare literals (Src/Zle/compctl.c:1941-1949):
// -1 files other / -2 unquoted / -3 exec cmd / -4 cdable param /
// -5 files / -6 glob expand / -7 cmd name / -8 exec file / -9 param
// C uses bare integer comparisons inline; the Rust port follows.
// File-thread `addwhat` global. Port of file-static `int addwhat;`
// from Src/Zle/compctl.c:1749. Set by the dispatcher before each
// addmatch / dumphashtable call to communicate the source kind.
thread_local! { static ADDWHAT: std::cell::Cell<i32> = const { std::cell::Cell::new(0) }; }
// Per-completion match list. Port of file-static `LinkList` of
// matches in zle_tricky.c. The Rust port keeps a per-call Vec so
// addmatch can accumulate results without touching ZLE globals.
thread_local! { static MATCH_LIST: std::cell::RefCell<Vec<String>> = const { std::cell::RefCell::new(Vec::new()) }; }
// =====================================================================
// compctl candidate-construction file-statics.
// Direct port of the file-statics at `Src/Zle/compctl.c:1734-1749`.
// These are DATA — the prefix/suffix strings + their lengths that the
// `makecomplistflags` preamble (c:3070-3403) computes and that
// `addmatch` (c:1925) reads back when it builds a real `Cmatch` via
// `comp_match` + `add_match_data`. Kept `thread_local` so parallel
// completion calls don't race, matching the ADDWHAT / PRPRE ports.
// (The C names `prpre`, `ipre`, `ripre`, `isuf`, `mflags`, `ispattern`,
// `haspattern`, `hasmatched`, `comppatmatch`, `curexpl` live in their
// canonical homes — PRPRE here, the rest in compcore.rs — and are read
// through those; only the compctl-private statics are declared here.)
// =====================================================================
thread_local! {
// c:1734-1738 — char* line/real/path/file prefix+suffix statics.
static LPRE: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static LSUF: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static RPRE: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static RSUF: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static PPRE: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static PSUF: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static LPPRE: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static LPSUF: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static FPRE: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static FSUF: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static QFPRE: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static QFSUF: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static QRPRE: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static QRSUF: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static QLPRE: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
static QLSUF: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
// glob.c:153 `char *glob_pre, *glob_suf;` — the file prefix/suffix the -g
// globbing code uses to reject names early; NULL (None) unless set below.
static GLOB_PRE: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
static GLOB_SUF: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
// c:1739 — int length statics.
static LPL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
static LSL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
static RPL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
static RSL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
static FPL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
static FSL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
static LPPL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
static LPSL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
// c:1740 — noreal (word has no real prefix/suffix to glob-expand).
static NOREAL: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
// c:1745 — ic: the special leading char (Tilde/Equals) or '\0'.
static IC: std::cell::Cell<char> = const { std::cell::Cell::new('\0') };
// Compiled real/file patterns (compctl.c file-statics `patcomp`,
// `filecomp`). None unless the word is treated as a glob pattern.
static PATCOMP: std::cell::RefCell<Option<crate::ported::pattern::Patprog>> =
const { std::cell::RefCell::new(None) };
static FILECOMP: std::cell::RefCell<Option<crate::ported::pattern::Patprog>> =
const { std::cell::RefCell::new(None) };
// `curcc` (c:1749 region) — the compctl whose -P/-S prefix/suffix
// `add_match_data` re-inserts around each match.
static CURCC: std::cell::RefCell<Option<Arc<Compctl>>> =
const { std::cell::RefCell::new(None) };
}
/// The `HashNode` that `dumphashtable` hands to `addmatch` as C's `char *t`
/// (cast back to `HashNode` / `Param` at c:1953-1954). `Hash` carries the
/// `struct hashnode` head every table node starts with (alias, reswd,
/// shfunc, builtin, cmdnam, nameddir, thingy, option); `Param` the full
/// `struct param`, whose `level` and scalar `gsu` the `-4`/`-9` and
/// parameter-flag predicates read.
pub(crate) enum AddmatchNode {
Hash(crate::ported::zsh_h::hashnode),
Param(crate::ported::zsh_h::param),
}
/// Add a match to the completion match list.
/// Port of `addmatch(char *s, char *t)` from Src/Zle/compctl.c:1925 (~130 lines).
///
/// The C body switches over `addwhat` (file static) that:
/// - addwhat ∈ {-1, -5, -6, -7, -8, CC_FILES} → file thread: fignore
/// check, then comp_match against the (q)file prefix/suffix with
/// `filecomp`; carries CMF_FILE.
/// - addwhat ∈ {CC_QUOTEFLAG, -2, -3, -4, -9} or addwhat > 0 →
/// accept thread: comp_match against the quoted-real prefix/suffix
/// with `patcomp`, falling back to the plain real prefix/suffix.
/// - else → reject.
/// On a successful comp_match it calls `add_match_data` to build and
/// register the real `Cmatch` (str/orig/ipre/ripre/isuf/pre/prpre/
/// ppre/psuf/suf, flags | isfile, exact) — the same registration path
/// (`matches`/`fmatches`, `ainfo`, `mnum`) the compsys `compadd` uses.
///
/// The prefix/suffix statics comp_match/add_match_data read here are
/// populated by the `makecomplistflags` preamble (c:3070-3403). `t` is the
/// C `char *t` that `dumphashtable` passes as the `HashNode` being added
/// (NULL from every other caller); the per-node predicates of the accept
/// thread (c:1991-2014: DISABLED, PM_* flags, `pm->level`, the `-4`
/// scalar getfn) are evaluated here on it. `MATCH_LIST` is retained as a
/// Rust-only observability mirror for the higher-level tests.
pub(crate) fn addmatch(s: &str, t: Option<&AddmatchNode>) {
use crate::ported::zsh_h::{
param, ALIAS_GLOBAL, DISABLED, PM_ARRAY, PM_EXPORTED, PM_INTEGER, PM_READONLY, PM_SCALAR,
PM_SPECIAL, PM_UNSET, PM_TYPE,
};
use crate::ported::zle::comp_h::{Cline, CMF_FILE};
use crate::ported::zle::compcore::{add_match_data, multiquote, tildequote};
use crate::ported::zle::compmatch::comp_match;
use std::sync::atomic::Ordering;
let aw = ADDWHAT.with(|c| c.get());
// c:1953-1954 — `hn = (HashNode) t; pm = (Param) t;`
let (hflags, pm): (i32, Option<¶m>) = match t {
Some(AddmatchNode::Hash(h)) => (h.flags, None),
Some(AddmatchNode::Param(p)) => (p.node.flags, Some(p)),
None => (0, None),
};
let pm_level = pm.map_or(0, |p| p.level);
let awu = aw as u64;
let hf = hflags as u32;
let mut isfile: i32 = 0; // c:1928
let mut isalt: i32 = 0; // c:1928
let mut isexact: i32 = 0; // c:1928
let mut lc: Option<Box<Cline>> = None; // c:1932
let ms: Option<String>;
// c:1935-1938 — the brace curpos snapshot (`qpos` while quoting, else
// `pos`) feeds comp_match's brace-list handling. The Rust `comp_match`
// port ignores its brace-list pointers (`_bpl`/`_bsl`) and
// `add_match_data` reads `qpos` directly off BRBEG/BREND, so the
// `curpos` assignment has no consumer and is intentionally omitted.
let ppre = PPRE.with(|r| r.borrow().clone());
let psuf = PSUF.with(|r| r.borrow().clone());
if aw == -1 || aw == -5 || aw == -6 || aw == CC_FILES as i32 || aw == -7 || aw == -8 {
// c:1957 — file thread.
let ppl = ppre.len() as i32; // c:1959
let psl = psuf.len() as i32; // c:1959
// c:1966-1976 — fignore check: skip files whose suffix is listed
// in $fignore (only for real filenames with no path suffix).
if (aw == CC_FILES as i32 || aw == -5) && psuf.is_empty() {
let sl = s.len();
if let Some(fign) = crate::ported::params::getaparam("fignore") {
for pt in &fign {
let filell = pt.len();
if filell < sl && s.ends_with(pt.as_str()) {
isalt = 1; // c:1975
break;
}
}
}
}
// c:1977-1984 — comp_match against the file prefix/suffix.
if aw == CC_FILES as i32 || aw == -6 || aw == -5 || aw == -8 {
let pfx = tildequote(&QFPRE.with(|r| r.borrow().clone()), 1);
let sfx = multiquote(&QFSUF.with(|r| r.borrow().clone()), 1);
let qu = if !ppre.is_empty() { 1 } else { 2 }; // c:1980
ms = FILECOMP.with(|fc| {
let fb = fc.borrow();
comp_match(
&pfx,
&sfx,
s,
fb.as_ref(),
Some(&mut lc),
qu,
None,
ppl,
None,
psl,
&mut isexact,
)
});
} else {
let pfx = multiquote(&FPRE.with(|r| r.borrow().clone()), 1);
let sfx = multiquote(&FSUF.with(|r| r.borrow().clone()), 1);
ms = FILECOMP.with(|fc| {
let fb = fc.borrow();
comp_match(
&pfx,
&sfx,
s,
fb.as_ref(),
Some(&mut lc),
0,
None,
ppl,
None,
psl,
&mut isexact,
)
});
}
if ms.is_none() {
return; // c:1985-1986
}
// c:1988-1989 — -7 (command names) requires the name to resolve.
if aw == -7 && findcmd(s, 0, 0).is_none() {
return;
}
isfile = CMF_FILE; // c:1990
} else if aw == CC_QUOTEFLAG as i32
|| aw == -2
|| (aw == -3 && (hflags & DISABLED) == 0) // c:1991
|| (aw == -4
&& pm.is_some_and(|p| {
PM_TYPE(p.node.flags as u32) == PM_SCALAR // c:1992
&& p.level == 0
&& p.gsu_s
.as_ref()
.map_or_else(|| crate::ported::params::strgetfn(p), |g| (g.getfn)(p))
.starts_with('/')
}))
|| (aw == -9 && (hf & PM_UNSET) == 0 && pm_level == 0) // c:1995
|| (aw > 0
&& (((hf & PM_UNSET) == 0 // c:1997
&& (((awu & CC_ARRAYS) != 0 && (hf & PM_ARRAY) != 0) // c:1998
|| ((awu & CC_INTVARS) != 0 && (hf & PM_INTEGER) != 0) // c:1999
|| ((awu & CC_ENVVARS) != 0 && (hf & PM_EXPORTED) != 0) // c:2000
|| ((awu & CC_SCALARS) != 0 && (hf & PM_SCALAR) != 0) // c:2001
|| ((awu & CC_READONLYS) != 0 && (hf & PM_READONLY) != 0) // c:2002
|| ((awu & CC_SPECIALS) != 0 && (hf & PM_SPECIAL) != 0) // c:2003
|| ((awu & CC_PARAMS) != 0 && (hf & PM_EXPORTED) == 0)) // c:2004
&& pm_level == 0)
|| ((((awu & CC_SHFUNCS) != 0) // c:2006
|| ((awu & CC_BUILTINS) != 0) // c:2007
|| ((awu & CC_EXTCMDS) != 0) // c:2008
|| ((awu & CC_RESWDS) != 0) // c:2009
|| ((awu & CC_ALREG) != 0 && (hflags & ALIAS_GLOBAL) == 0) // c:2010
|| ((awu & CC_ALGLOB) != 0 && (hflags & ALIAS_GLOBAL) != 0)) // c:2011
&& (((awu & CC_DISCMDS) != 0 && (hflags & DISABLED) != 0) // c:2012
|| ((awu & CC_EXCMDS) != 0 && (hflags & DISABLED) == 0))) // c:2013
|| ((awu & CC_BINDINGS) != 0 && (hflags & DISABLED) == 0))) // c:2014
{
// c:2015-2041 — Match the word against the real prefix/suffix,
// trying the quoted pattern-driven form first, then the plain form.
let (p1s, s1s, p2s, s2s) = if aw == CC_QUOTEFLAG as i32 {
// c:2018-2019
(
QRPRE.with(|r| r.borrow().clone()),
QRSUF.with(|r| r.borrow().clone()),
RPRE.with(|r| r.borrow().clone()),
RSUF.with(|r| r.borrow().clone()),
)
} else {
// c:2021-2022
(
QLPRE.with(|r| r.borrow().clone()),
QLSUF.with(|r| r.borrow().clone()),
LPRE.with(|r| r.borrow().clone()),
LSUF.with(|r| r.borrow().clone()),
)
};
let p1 = multiquote(&p1s, 1); // c:2024
let s1 = multiquote(&s1s, 1); // c:2024
let p2 = multiquote(&p2s, 1); // c:2025
let s2 = multiquote(&s2s, 1); // c:2025
let qflag = (aw == CC_QUOTEFLAG as i32) as i32; // c:2030
let p1l = p1.len() as i32;
let s1l = s1.len() as i32;
// c:2029 — patcomp-driven match first.
let first = PATCOMP.with(|pc| {
let pb = pc.borrow();
comp_match(
&p1,
&s1,
s,
pb.as_ref(),
Some(&mut lc),
qflag,
None,
p1l,
None,
s1l,
&mut isexact,
)
});
if first.is_some() {
ms = first;
} else {
// c:2035 — fall back to the plain real prefix/suffix.
let p2l = p2.len() as i32;
let s2l = s2.len() as i32;
ms = comp_match(
&p2,
&s2,
s,
None,
Some(&mut lc),
qflag,
None,
p2l,
None,
s2l,
&mut isexact,
);
if ms.is_none() {
return; // c:2039
}
}
} else {
return; // c:2015 else — drop the match.
}
let ms = match ms {
Some(m) => m,
None => return, // c:2042-2043
};
// c:2046-2049 — when inserting braces always use the quoted length;
// omitted here for the same reason as the c:1935-1938 snapshot above
// (no brace-list consumer in the Rust comp_match/add_match_data port).
// c:2051-2057 — build the real Cmatch from the preamble statics.
let ipre_v = crate::ported::zle::compcore::ipre
.get()
.and_then(|m| m.lock().ok().map(|g| g.clone()))
.unwrap_or_default();
let ripre_v = crate::ported::zle::compcore::ripre
.get()
.and_then(|m| m.lock().ok().map(|g| g.clone()))
.unwrap_or_default();
let isuf_v = ISUF.lock().map(|g| g.clone()).unwrap_or_default();
let prpre_v = PRPRE.with(|r| r.borrow().clone()).unwrap_or_default();
// c:2052/2056 — C passes `curcc->prefix` / `curcc->suffix` STRAIGHT through,
// NULL and all; add_match_data stores them unconditionally (compcore.c:2943-
// 2944) so "no prefix" stays distinct from "empty prefix". Flattening the
// Options here with unwrap_or_default() erased that distinction before it
// could reach the match.
let (pre_v, suf_v): (Option<String>, Option<String>) = CURCC.with(|r| {
r.borrow()
.as_ref()
.map(|cc| (cc.prefix.clone(), cc.suffix.clone()))
.unwrap_or((None, None))
});
// c:2054-2055 — path prefix/suffix only travel with real filenames.
let lppre_v = if isfile != 0 {
LPPRE.with(|r| r.borrow().clone())
} else {
String::new()
};
let lpsuf_v = if isfile != 0 {
LPSUF.with(|r| r.borrow().clone())
} else {
String::new()
};
let mflags_v = crate::ported::zle::compcore::mflags.load(Ordering::Relaxed);
// c:2044 — C discards the return value; a NULL (a match rejected by the
// c:2999 brace filter) simply means nothing was registered.
let _ = add_match_data(
isalt, // c:2051 alt (fignore alternative)
&ms, // str — the matched string
s, // orig
lc, // line — the Cline built by comp_match
&ipre_v, // ipre
&ripre_v, // ripre
&isuf_v, // isuf
pre_v.as_deref(), // pre — curcc->prefix (NULL-able, c:2052)
&prpre_v, // prpre
&lppre_v, // ppre — path prefix (files only)
None, // pline
&lpsuf_v, // psuf — path suffix (files only)
None, // sline
suf_v.as_deref(), // suf — curcc->suffix (NULL-able, c:2056)
mflags_v | isfile, // c:2057 flags
isexact,
);
// Rust observability mirror (unit-test / inspection sink; not part of
// the C source, which registers only into the global match list via
// add_match_data above). Kept so the makecomplistflags-level tests can
// still observe which names were accepted.
MATCH_LIST.with(|r| r.borrow_mut().push(s.to_string()));
}
/// Hash-node → match adapter for scanhashtable callbacks.
/// Port of `addhnmatch(HashNode hn, UNUSED(int flags))` from Src/Zle/compctl.c:2122.
///
/// Trivial wrapper: ignores `flags` and forwards the node name to
/// addmatch with `t=NULL`. Used by maketildelist's scanhashtable
/// invocation (c:2060).
/// WARNING: param names don't match C — Rust=(_flags) vs C=(hn, flags)
pub(crate) fn addhnmatch(name: &str, _flags: i32) {
addmatch(name, None);
}
/// Perform expansion on the given string and return the result.
/// During this errors are not reported.
/// Port of `getreal(char *str)` from Src/Zle/compctl.c:2132.
/// WARNING: param names don't match C — Rust=(str_in) vs C=(str)
pub(crate) fn getreal(str_in: &str) -> String {
// c:2132
let ne = *crate::ported::utils::noerrs_lock().lock().unwrap(); // c:2135
let mut l: crate::ported::subst::LinkList = // c:2134 newlinklist()
vec![str_in.to_string()].into_iter().collect(); // c:2138 addlinknode(l, dupstring(str))
*crate::ported::utils::noerrs_lock().lock().unwrap() = 1; // c:2137
let mut ret_flags: i32 = 0;
crate::ported::subst::prefork(&mut l, 0, &mut ret_flags); // c:2139
*crate::ported::utils::noerrs_lock().lock().unwrap() = ne; // c:2140
// c:2141-2143 — if (!errflag && nonempty(l) && peekfirst(l) && peekfirst(l)[0])
// return dupstring(peekfirst(l));
if errflag.load(std::sync::atomic::Ordering::Relaxed) == 0 {
if let Some(first) = l.into_iter().next() {
if !first.is_empty() {
return first;
}
}
}
// c:2144 — errflag &= ~ERRFLAG_ERROR;
errflag.fetch_and(
!crate::ported::utils::ERRFLAG_ERROR,
std::sync::atomic::Ordering::Relaxed,
);
str_in.to_string() // c:2146
}
/// This reads a directory and adds the files to the list of matches. The
/// parameters say which files should be added.
/// Port of `gen_matches_files(int dirs, int execs, int all)` from Src/Zle/compctl.c:2161.
pub(crate) fn gen_matches_files(mut dirs: i32, mut execs: i32, mut all: i32) {
use crate::ported::zsh_h::{GLOBDOTS, NULLGLOB, Outpar, Star};
use std::sync::atomic::Ordering;
let psuf = PSUF.with(|r| r.borrow().clone());
let mut ns: bool = false; // c:2167
let ng = crate::ported::zsh_h::isset(NULLGLOB); // c:2167
let aw = ADDWHAT.with(|c| c.get()); // c:2167
crate::ported::options::opt_state_set(&crate::ported::zsh_h::opt_name(NULLGLOB), true); // c:2169
if !psuf.is_empty() {
// c:2171 — if there is a path suffix, check if it doesn't have a `*'
// or `)' at the end (this is used to determine if we should use
// globbing).
let q = psuf.chars().next_back(); // c:2174
ns = !(q == Some(Star) || q == Some(Outpar)); // c:2176
// And generate only directory names.
dirs = 1; // c:2180
all = 0;
execs = 0;
}
// c:2183 — open directory.
let pathpref: Option<String> = PRPRE.with(|r| r.borrow().clone()).filter(|p| !p.is_empty());
let pathpreflen = pathpref.as_ref().map_or(0, |p| p.len());
if pathpreflen > libc::PATH_MAX as usize {
crate::ported::options::opt_state_set(&crate::ported::zsh_h::opt_name(NULLGLOB), ng);
return; // c:2189
}
if let Ok(mut d) = std::fs::read_dir(pathpref.as_deref().unwrap_or(".")) {
// c:2194
// c:2196 — if we search only special files, prepare a path buffer for stat.
let mut p: String = if all == 0 && pathpreflen != 0 {
pathpref.clone().unwrap_or_default()
} else {
String::new()
};
let fpre = FPRE.with(|r| r.borrow().clone());
let fpl = FPL.with(|c| c.get()) as usize;
let fsuf = FSUF.with(|r| r.borrow().clone());
let fsl = FSL.with(|c| c.get()) as usize;
let mstack_set = crate::ported::zle::compcore::mstack
.get()
.and_then(|m| m.lock().ok().map(|g| g.is_some()))
.unwrap_or(false);
// c:2202 — fine, now read the directory.
while let Some(n) = crate::ported::utils::zreaddir(&mut d, 1) {
if errflag.load(Ordering::Relaxed) != 0 {
break;
}
// c:2204 — ignore files beginning with `.' unless the thing we
// found on the command line also starts with a dot or GLOBDOTS
// is set.
if !n.starts_with('.') || fpre.starts_with('.') || crate::ported::zsh_h::isset(GLOBDOTS)
{
ADDWHAT.with(|c| c.set(if execs != 0 { -8 } else { -5 })); // c:2206
let test: bool;
let filecomp_matched = FILECOMP.with(|fc| {
fc.borrow().as_ref().map(|prog| pattry(prog, &n))
});
if let Some(t) = filecomp_matched {
// c:2207 — if we have a pattern for the filename check, use it.
test = t; // c:2209
} else {
// c:2211 — otherwise use the prefix and suffix strings directly.
let nb = n.as_bytes();
let mut t = nb.len() >= fsl // c:2212 e = n + strlen(n) - fsl
&& nb.starts_with(&fpre.as_bytes()[..fpl.min(fpre.len())]) // c:2213
&& nb[nb.len() - fsl..] == *fsuf.as_bytes(); // c:2214
if !t && mstack_set {
t = true; // c:2216
ADDWHAT.with(|c| c.set(CC_FILES as i32)); // c:2217
}
test = t;
}
// c:2221 — filename didn't match?
if !test {
continue;
}
let mut buf: Option<std::fs::Metadata> = None;
if all == 0 {
// c:2223 — we still have to check the file type, so prepare
// the path buffer by appending the filename.
if n.len() + pathpreflen + 1 > libc::PATH_MAX as usize {
continue; // c:2230
}
p.truncate(pathpreflen);
p.push_str(&n); // c:2232
// c:2234 — and do the stat.
match std::fs::metadata(&p) {
Ok(m) => buf = Some(m),
Err(_) => continue, // c:2235
}
}
let ok = all != 0
|| buf.as_ref().is_some_and(|b| {
(dirs != 0 && b.is_dir())
|| (execs != 0 && b.is_file() && (b.permissions().mode() & 0o111) != 0)
}); // c:2238-2241
if ok {
// c:2243 — if we want all files or the file has the right type...
if !psuf.is_empty() {
// c:2245 — we have to test for a path suffix.
let o = p.len(); // c:2247
if o + psuf.len() > libc::PATH_MAX as usize {
continue; // c:2250
}
// c:2252 — append it to the path buffer.
p.push_str(&psuf);
// c:2255 — do we have to use globbing?
let cpm: Option<String> = crate::ported::zle::compcore::comppatmatch
.get()
.and_then(|m| m.lock().ok())
.map(|g| g.clone());
let cpm_nonempty = cpm.as_deref().is_some_and(|c| !c.is_empty());
let tt: bool;
if crate::ported::zle::compcore::ispattern.load(Ordering::Relaxed) != 0
|| (ns && cpm_nonempty)
{
// c:2257 — yes, so append a `*' if needed.
if ns && cpm.as_deref().is_some_and(|c| c.starts_with('*')) {
p.push(Star); // c:2260-2263
}
// c:2266 — do the globbing...
crate::ported::glob::remnulargs(&mut p);
let mut l: crate::ported::subst::LinkList =
vec![p.clone()].into_iter().collect();
crate::ported::subst::globlist(&mut l, 0);
// c:2269 — and see if that produced a filename.
tt = l.into_iter().next().is_some();
} else {
// c:2275 — otherwise just check, if we have access
// to the file.
tt = std::ffi::CString::new(p.as_str())
.map(|c| unsafe { libc::access(c.as_ptr(), libc::F_OK) } == 0)
.unwrap_or(false);
}
p.truncate(o); // c:2279
if tt {
// c:2280 — ok, we can add the filename to the list
// of matches.
addmatch(&n, None);
}
} else {
// c:2284 — we want all files, so just add the name to
// the matches.
addmatch(&n, None);
}
}
}
}
}
crate::ported::options::opt_state_set(&crate::ported::zsh_h::opt_name(NULLGLOB), ng); // c:2287
ADDWHAT.with(|c| c.set(aw)); // c:2288
}
/// Line-context dispatch — global completion entry.
/// Port of `makecomplistglobal(char *os, int incmd, UNUSED(int lst), int flags)` from Src/Zle/compctl.c:2401.
///
/// Looks at `linwhat` (IN_ENV / IN_MATH / IN_COND / IN_REDIR / else)
/// and dispatches to the appropriate compctl spec:
/// IN_ENV → cc_default (parameter values)
/// IN_MATH → cc_dummy (params or assoc keys)
/// IN_COND → cc_dummy with -o/-nt/-ot/-ef logic
/// IN_REDIR → cc_default (redirections)
/// default → makecomplistcmd (per-command lookup)
pub(crate) fn makecomplistglobal(os: &str, incmd: bool, _lst: i32, flags: i32) -> i32 {
use std::sync::atomic::Ordering;
// c:2406 — reset ccont.
CCONT.with(|c| c.set(CC_CCCONT));
// c:2407 — clear cc_dummy.suffix.
if let Some(d) = CC_DUMMY.lock().unwrap().as_mut() {
if let Some(inner) = std::sync::Arc::get_mut(d) {
inner.suffix = None;
}
}
const CFN_FIRST: i32 = 1;
const CFN_DEFAULT: i32 = 2;
// Seed CMDSTR (the command whose argument is being completed) from the
// parsed command line. C sets `cmdstr` in get_comp_string; the Rust
// port keeps it thread-local in this module and only makecomplistctl
// (the `compcall` driver) set it, so the ccmakehookfn → makecomplistcmd
// path saw it as None and bailed at `None => return ret` before ever
// reaching cc_default — argument/file completion (`ls <Tab>`) produced
// no matches. For command position (incmd) there is no outer command
// context, so leave it None (makecomplistcmd uses cc_compos there).
if !incmd {
let cmd_word = crate::ported::zle::complete::COMPWORDS
.get()
.and_then(|m| m.lock().ok())
.and_then(|g| g.first().cloned());
CMDSTR.with(|r| *r.borrow_mut() = cmd_word);
}
let lw = crate::ported::zle::compcore::linwhat.load(Ordering::Relaxed);
let in_env = lw == crate::ported::zle::compcore::IN_ENV_LW; // c:2409
let in_math = lw == crate::ported::zle::compcore::IN_MATH_LW; // c:2415
let in_cond = lw == crate::ported::zle::compcore::IN_COND_LW; // c:2429
let mut cc: Option<Arc<Compctl>> = None;
if in_env {
// c:2409
if (flags & CFN_DEFAULT) == 0 {
// c:2411
cc = CC_DEFAULT.lock().unwrap().clone(); // c:2412
}
} else if in_math {
// c:2415
if (flags & CFN_DEFAULT) == 0 {
let mut dummy_inner = Compctl::default();
dummy_inner.mask = CC_PARAMS; // c:2424
dummy_inner.refc = 10000; // c:2427
cc = Some(Arc::new(dummy_inner));
}
} else if in_cond {
// c:2429
if (flags & CFN_DEFAULT) == 0 {
let lwpos = *CLWPOS.lock().unwrap() as usize;
let words = CLWORDS.lock().unwrap().clone();
let prev = if lwpos > 0 {
words.get(lwpos - 1).cloned()
} else {
None
};
let prev_s = prev.as_deref().unwrap_or("");
let mask = if prev_s == "-o" {
// c:2435
CC_OPTIONS
} else if (prev_s.starts_with('-') && prev_s.len() == 2)
|| prev_s == "-nt"
|| prev_s == "-ot"
|| prev_s == "-ef"
// c:2436
{
CC_FILES
} else {
CC_FILES | CC_PARAMS // c:2440
};
let mut dummy_inner = Compctl::default();
dummy_inner.mask = mask;
dummy_inner.refc = 10000;
cc = Some(Arc::new(dummy_inner));
}
} else {
// c:2453 — default: per-command lookup via makecomplistcmd.
return makecomplistcmd(os, incmd, flags);
}
if let Some(cc) = cc {
// c:2458 — cc_first first.
if (flags & CFN_FIRST) == 0 {
if let Some(cc_first) = CC_FIRST.lock().unwrap().clone() {
makecomplistcc(&cc_first, os, incmd); // c:2459
if (CCONT.with(|c| c.get()) & CC_CCCONT) == 0 {
// c:2461
return 0;
}
}
}
makecomplistcc(&cc, os, incmd); // c:2464
return 1; // c:2465
}
0 // c:2467
}
/// Per-command compctl lookup + dispatch.
/// Port of `makecomplistcmd(char *os, int incmd, int flags)` from Src/Zle/compctl.c:2474.
///
/// Resolves the compctl for cmdstr by:
/// 1. If !CFN_FIRST: run cc_first first; bail if !CC_CCCONT
/// 2. Run pattern compctls (makecomplistpc); bail if !CC_CCCONT
/// 3. If cmdstr starts with `=`, expand path
/// 4. Lookup cmdstr in compctltab — try full name then trailing
/// pathname component (after remlpaths)
/// 5. If incmd: use cc_compos
/// 6. Else if no match: cc_default (unless CFN_DEFAULT)
/// 7. Call makecomplistcc(cc, os, incmd)
/// WARNING: param names don't match C — Rust=(incmd, flags) vs C=(os, incmd, flags)
pub(crate) fn makecomplistcmd(os: &str, incmd: bool, flags: i32) -> i32 {
const CFN_FIRST: i32 = 1;
const CFN_DEFAULT: i32 = 2;
let mut ret: i32 = 0;
// C: c:2482 — first try cc_first
if (flags & CFN_FIRST) == 0 {
if let Some(cc_first) = CC_FIRST.lock().unwrap().clone() {
makecomplistcc(&cc_first, os, incmd);
if (CCONT.with(|c| c.get()) & CC_CCCONT) == 0 {
return 0;
}
}
}
// C: c:2491 — pattern compctls
let cmdstr = CMDSTR.with(|r| r.borrow().clone());
if cmdstr.is_some() {
ret |= makecomplistpc(os, incmd);
if (CCONT.with(|c| c.get()) & CC_CCCONT) == 0 {
return ret;
}
}
// C: c:2509 — incmd path uses cc_compos
let cc = if incmd {
CC_COMPOS.lock().unwrap().clone()
} else {
// C: c:2511-2519 — lookup compctltab[cmdstr]
let name = match &cmdstr {
Some(s) => s.clone(),
None => return ret,
};
let table = COMPCTL_TAB.read().unwrap();
let from_table = table.as_ref().and_then(|m| m.get(&name).cloned());
drop(table);
match from_table {
Some(c) => Some(c),
None => {
if (flags & CFN_DEFAULT) != 0 {
return ret;
}
ret |= 1;
CC_DEFAULT.lock().unwrap().clone()
}
}
};
if let Some(c) = cc {
makecomplistcc(&c, os, incmd);
}
ret
}
/// Per-compctl entry — track usage + dispatch the OR chain.
/// Port of `makecomplistcc(Compctl cc, char *s, int incmd)` from Src/Zle/compctl.c:2558.
///
/// Bumps refc on cc, adds it to ccused list, resets ccont, calls
/// makecomplistor. The ccused list lets later cleanup free all
/// compctls used during a single completion.
/// WARNING: param names don't match C — Rust=(s, incmd) vs C=(cc, s, incmd)
pub(crate) fn makecomplistcc(cc: &Arc<Compctl>, s: &str, incmd: bool) {
// C: c:2560 — refc++ (Arc handles this)
let _ = cc.clone();
// C: c:2562 — initialize ccused list
CCUSED.with(|r| r.borrow_mut().push(cc.clone()));
// C: c:2565 — reset ccont
CCONT.with(|c| c.set(0));
// C: c:2567 — dispatch OR chain
makecomplistor(cc, s, incmd, 0, 0);
}
// Pre-cursor directory path (`prpre` global). Port of file-static
// `char *prpre` at Src/Zle/compctl.c:1736 — the directory portion
// of the path component the cursor is in, expanded for `opendir`.
// Set by the completion driver before calling gen_matches_files.
thread_local! { static PRPRE: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) }; }
/// Find a node in a linked list by data-pointer equality.
/// Port of `findnode(LinkList list, void *dat)` from Src/Zle/compctl.c:2288.
///
/// C signature: `LinkNode findnode(LinkList list, void *dat)` —
/// walks `list` looking for the node whose data pointer == `dat`.
/// Returns the matching node or NULL.
///
/// Rust generic over `T: PartialEq` — returns the index of the
/// matching element, or None.
/// WARNING: param names don't match C — Rust=(dat) vs C=(list, dat)
pub(crate) fn findnode<T: PartialEq>(list: &[T], dat: &T) -> Option<usize> {
list.iter().position(|x| x == dat)
}
// `cdepth` recursion guard. Port of file-static `int cdepth = 0;`
// at Src/Zle/compctl.c:2300.
thread_local! { static CDEPTH: std::cell::Cell<i32> = const { std::cell::Cell::new(0) }; }
/// Port of `MAX_CDEPTH` from `Src/Zle/compctl.c:2302`. Maximum
/// recursion depth — prevents infinite recursion between compctl-
/// driven completion and the wrapper.
pub const MAX_CDEPTH: i32 = 16; // c:2302
// `ccont` continuation flags. Port of file-static `unsigned long
// ccont;` at Src/Zle/compctl.c:1714. Bitmask of CC_CCCONT/etc.
// controlling whether the dispatch loop continues to next compctl.
thread_local! { static CCONT: std::cell::Cell<u64> = const { std::cell::Cell::new(0) }; }
/// Build the completion list — top-level dispatch.
/// Port of `makecomplistctl(int flags)` from Src/Zle/compctl.c:2305.
///
/// Entry point used by bin_compcall and the completion driver.
/// The C body:
/// 1. Recursion guard (cdepth >= MAX_CDEPTH → return 0)
/// 2. SWITCHHEAPS to the compheap (Rust uses the global allocator)
/// 3. Save lots of state (cmdstr, clwords, instring, qipre/qisuf,
/// isuf, autoq, offs)
/// 4. Set up new state from compquote / compqiprefix / compqisuffix /
/// compisuffix / compwords / compcurrent
/// 5. Set incompfunc=2 (deeper-nested marker)
/// 6. Call makecomplistglobal(str, !clwpos, COMP_COMPLETE, flags)
/// 7. Restore state
/// 8. cdepth-- and return
///
/// This Rust port keeps the recursion guard + flag dispatch + the
/// makecomplistglobal call. The compfunc state save/restore relies
/// on ZLE-tricky globals (clwords, etc.) that aren't ported here.
pub(crate) fn makecomplistctl(flags: i32) -> i32 {
// c:2318 — recursion guard.
let cdepth = CDEPTH.with(|c| c.get());
if cdepth == MAX_CDEPTH {
// c:2318
return 0;
}
CDEPTH.with(|c| c.set(cdepth + 1)); // c:2321
// c:2323-2324 — comp_str(&lip, &lp, 0): the word being completed
// plus the ignored-prefix (`lip`) and prefix (`lp`) lengths.
let (str_in, lip, lp) = crate::ported::zle::compcore::comp_str(false);
// c:2325-2327 — save the state makecomplistctl overwrites.
let os = CMDSTR.with(|r| r.borrow().clone());
let ow = CLWORDS.lock().unwrap().clone();
let on = *CLWNUM.lock().unwrap();
let op = *CLWPOS.lock().unwrap();
let ois = INSTRING.load(Ordering::Relaxed);
let oib = INBACKT.load(Ordering::Relaxed);
let oisuf = ISUF.lock().unwrap().clone();
let oqp = QIPRE.get_or_init(|| Mutex::new(String::new())).lock().unwrap().clone();
let oqs = QISUF.get_or_init(|| Mutex::new(String::new())).lock().unwrap().clone();
let oaq = AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.clone();
let ooffs = crate::ported::zle::compcore::OFFS.load(std::sync::atomic::Ordering::Relaxed);
// c:2330-2361 — quote-context setup driven by `compquote`.
let compquote = crate::ported::zle::complete::COMPQUOTE
.get_or_init(|| Mutex::new(String::new()))
.lock()
.map(|g| g.clone())
.unwrap_or_default();
match compquote.chars().next() {
Some('`') => {
// c:2331-2338 — backtick: instring/inbackt cleared, no autoq.
INSTRING.store(QT_NONE, Ordering::Relaxed);
INBACKT.store(0, Ordering::Relaxed);
*AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = String::new();
}
Some(c) if c == '\'' || c == '"' || c == '$' => {
// c:2340-2355 — single/double/dollar quoting.
INSTRING.store(
match c {
'\'' => QT_SINGLE,
'"' => QT_DOUBLE,
_ => QT_DOLLARS,
},
Ordering::Relaxed,
);
INBACKT.store(0, Ordering::Relaxed);
// c:2354 — autoq = (compquote == '$' ? compquote+1 : compquote).
*AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = if c == '$' {
compquote[1..].to_string()
} else {
compquote.clone()
};
}
_ => {
// c:2357-2360 — no quoting context.
INSTRING.store(QT_NONE, Ordering::Relaxed);
INBACKT.store(0, Ordering::Relaxed);
*AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = String::new();
}
}
// c:2362-2363 — qipre/qisuf from the compctl driver params.
*QIPRE.get_or_init(|| Mutex::new(String::new())).lock().unwrap() = COMPQIPREFIX.lock().unwrap().clone();
*QISUF.get_or_init(|| Mutex::new(String::new())).lock().unwrap() = COMPQISUFFIX.lock().unwrap().clone();
// c:2364-2366 — isuf = remnulargs(ctokenize(compisuffix)).
let mut isuf_v = crate::ported::zle::compcore::ctokenize(&COMPISUFFIX.lock().unwrap().clone());
crate::ported::glob::remnulargs(&mut isuf_v);
*ISUF.lock().unwrap() = isuf_v;
// c:2367-2377 — install compwords into clwords, each tokenized.
let compwords = COMPWORDS
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.unwrap()
.clone();
*CLWNUM.lock().unwrap() = compwords.len() as i32; // c:2367
*CLWPOS.lock().unwrap() = (COMPCURRENT.load(std::sync::atomic::Ordering::Relaxed) as i32) - 1; // c:2368
CMDSTR.with(|r| *r.borrow_mut() = compwords.first().cloned()); // c:2369
let clw: Vec<String> = compwords
.iter()
.map(|w| {
let mut t = w.clone(); // c:2372
crate::ported::glob::tokenize(&mut t); // c:2373
crate::ported::glob::remnulargs(&mut t); // c:2374
t
})
.collect();
*CLWORDS.lock().unwrap() = clw;
// c:2378 — offs = lip + lp.
crate::ported::zle::compcore::OFFS.store(lip + lp, std::sync::atomic::Ordering::Relaxed);
// c:2379-2381 — incompfunc = 2 during the nested list build, 1 after.
INCOMPFUNC.store(2, std::sync::atomic::Ordering::Relaxed);
let incmd = *CLWPOS.lock().unwrap() == 0; // c:2380 — `!clwpos`
let ret = makecomplistglobal(
&str_in,
incmd,
crate::ported::zle::zle_h::COMP_COMPLETE,
flags,
); // c:2380
INCOMPFUNC.store(1, std::sync::atomic::Ordering::Relaxed); // c:2381
// c:2382-2396 — restore the saved state.
*ISUF.lock().unwrap() = oisuf;
*QIPRE.get_or_init(|| Mutex::new(String::new())).lock().unwrap() = oqp;
*QISUF.get_or_init(|| Mutex::new(String::new())).lock().unwrap() = oqs;
INSTRING.store(ois, Ordering::Relaxed);
INBACKT.store(oib, Ordering::Relaxed);
*AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = oaq;
crate::ported::zle::compcore::OFFS.store(ooffs, std::sync::atomic::Ordering::Relaxed);
CMDSTR.with(|r| *r.borrow_mut() = os);
*CLWORDS.lock().unwrap() = ow;
*CLWNUM.lock().unwrap() = on;
*CLWPOS.lock().unwrap() = op;
CDEPTH.with(|c| c.set(c.get() - 1)); // c:2398
ret // c:2400
}
/// Top-level per-compctl dispatch.
/// Port of `makecomplistlist(Compctl cc, char *s, int incmd, int compadd)` from Src/Zle/compctl.c:2615.
///
/// Routes to either makecomplistext (for -x extended conditions)
/// or makecomplistflags (for the regular flag-mask compctl).
/// WARNING: param names don't match C — Rust=(s, incmd, compadd) vs C=(ylist)
pub(crate) fn makecomplistlist(cc: &Arc<Compctl>, s: &str, incmd: bool, compadd: i32) {
use crate::ported::zle::compcore::{OFFS, WB, WE, ZLEMETACS};
let oloffs = OFFS.load(Ordering::Relaxed); // c:2621
let owe = WE.load(Ordering::Relaxed);
let owb = WB.load(Ordering::Relaxed);
let ocs = ZLEMETACS.load(Ordering::Relaxed);
if cc.ext.is_some() {
// Handle extended completion.
makecomplistext(cc, s, incmd);
} else {
// Only normal flags.
makecomplistflags(cc, s.to_string(), incmd, compadd);
}
// Reset some information variables for the next try.
errflag.fetch_and(!crate::ported::utils::ERRFLAG_ERROR, Ordering::Relaxed);
OFFS.store(oloffs, Ordering::Relaxed);
WB.store(owb, Ordering::Relaxed);
WE.store(owe, Ordering::Relaxed);
ZLEMETACS.store(ocs, Ordering::Relaxed);
}
/// Extended (`-x`) completion list builder.
/// Port of `makecomplistext(Compctl occ, char *os, int incmd)` from Src/Zle/compctl.c:2640.
///
/// Walks cc.ext chain (the per-condition compctls), evaluates each
/// condition against the current line state, and dispatches to
/// makecomplistflags for the first matching condition's spec.
/// WARNING: param names don't match C — Rust=(os, incmd) vs C=(Equals)
pub(crate) fn makecomplistext(occ: &Arc<Compctl>, os: &str, incmd: bool) {
use crate::ported::glob::tokenize;
use crate::ported::lex::untokenize_ztokens as untokenize;
use crate::ported::zle::compcore::rembslash;
// c:2655 — ins = (instring != QT_NONE ? instring : (inbackt ? QT_BACKTICK : 0)).
let ins = {
let is = INSTRING.load(Ordering::Relaxed);
if is != QT_NONE {
is
} else if INBACKT.load(Ordering::Relaxed) != 0 {
QT_BACKTICK
} else {
0
}
};
let complete_in_word = crate::ported::options::opt_state_get("COMPLETEINWORD").unwrap_or(false);
let mut m = 0; // c:2652
let mut d = 0; // c:2652
// c:2658 — loop over the patterns separated by `-`s (the `ext`
// chain, walked via each compctl's `next` sibling).
let mut compc_opt = occ.ext.clone();
while let Some(compc) = compc_opt {
let mut compadd = 0i32; // c:2659
let mut t; // c:2659
*BRANGE.lock().unwrap() = 0; // c:2650 compadd = t = brange = 0
*ERANGE.lock().unwrap() = *CLWNUM.lock().unwrap() - 1; // c:2651
// c:2662 — loop over OR'ed patterns: `for (cc = compc->cond;
// cc && !t; cc = or)`. `t` starts 0 for the first OR arm.
t = 0;
let mut cc_opt: Option<&Compcond> = compc.cond.as_deref();
while let Some(cc0) = cc_opt {
if t != 0 {
break; // c:2662 — `cc && !t`
}
let or = cc0.or.as_deref(); // c:2663 — save the OR sibling.
// c:2665 — loop over AND'ed patterns: `for (t = 1;
// cc && t; cc = cc->and)`.
t = 1;
let mut and_opt: Option<&Compcond> = Some(cc0);
while let Some(cc) = and_opt {
if t == 0 {
break;
}
let clwnum = *CLWNUM.lock().unwrap();
let clwpos = *CLWPOS.lock().unwrap();
// c:2667 — loop over the [...] pairs: `for (t = i = 0;
// i < cc->n && !t; i++)`.
t = 0;
let mut i = 0i32;
while i < cc.n && t == 0 {
// c:2669-2670 — reset the range for this pair.
*BRANGE.lock().unwrap() = 0;
*ERANGE.lock().unwrap() = clwnum - 1;
let iu = i as usize;
match cc.typ {
// c:2672 — CCT_QUOTE.
x if x == CCT_QUOTE => {
if let CompcondData::S { s, .. } = &cc.u {
let c0 = s.get(iu).and_then(|x| x.chars().next());
t = match c0 {
Some('s') if ins == QT_SINGLE => 1,
Some('d') if ins == QT_DOUBLE => 1,
Some('b') if ins == QT_BACKTICK => 1,
_ => 0,
};
}
}
// c:2677 / c:2680 — CCT_POS / CCT_NUMWORDS.
x if x == CCT_POS || x == CCT_NUMWORDS => {
let tt = if x == CCT_POS { clwpos } else { clwnum }; // c:2678/2681
if let CompcondData::R { a, b } = &cc.u {
let mut av = *a.get(iu).unwrap_or(&0); // c:2683
if av < 0 {
av += clwnum;
}
let mut bv = *b.get(iu).unwrap_or(&0); // c:2685
if bv < 0 {
bv += clwnum;
}
if x == CCT_POS {
// c:2688 — brange = a, erange = b.
*BRANGE.lock().unwrap() = av;
*ERANGE.lock().unwrap() = bv;
}
t = if tt >= av && tt <= bv { 1 } else { 0 }; // c:2689
}
}
// c:2691 — CCT_CURSUF / CCT_CURPRE.
x if x == CCT_CURSUF || x == CCT_CURPRE => {
if let CompcondData::S { s, .. } = &cc.u {
// c:2693 — s = clwpos < clwnum ? os : "".
let mut sv = if clwpos < clwnum {
os.to_string()
} else {
String::new()
};
sv = untokenize(&sv); // c:2694
if complete_in_word {
// c:2695 — s[offs] = '\0'.
let off = crate::ported::zle::compcore::OFFS
.load(std::sync::atomic::Ordering::Relaxed)
.max(0)
as usize;
if off <= sv.len() && sv.is_char_boundary(off) {
sv.truncate(off);
}
}
let sc = rembslash(s.get(iu).map(|x| x.as_str()).unwrap_or("")); // c:2696
let a = sc.len(); // c:2697
if sv.len() >= a && sv.starts_with(&sc) {
// c:2698 — !strncmp(s, sc, a).
compadd = if x == CCT_CURSUF { a as i32 } else { 0 }; // c:2699
t = 1;
}
}
}
// c:2703 — CCT_CURSUB / CCT_CURSUBC.
x if x == CCT_CURSUB || x == CCT_CURSUBC => {
if clwpos < 0 || clwpos >= clwnum {
t = 0; // c:2706
} else if let CompcondData::S { p, s } = &cc.u {
let mut sv = untokenize(os); // c:2708-2709
if complete_in_word {
let off = crate::ported::zle::compcore::OFFS
.load(std::sync::atomic::Ordering::Relaxed)
.max(0)
as usize; // c:2710
if off <= sv.len() && sv.is_char_boundary(off) {
sv.truncate(off);
}
}
let a = getcpat(
&sv,
*p.get(iu).unwrap_or(&0),
s.get(iu).map(|x| x.as_str()).unwrap_or(""),
if x == CCT_CURSUBC { 1 } else { 0 },
); // c:2711
if a != -1 {
compadd = a; // c:2716
t = 1;
}
}
}
// c:2720 / c:2724 — CCT_CURPAT/CURSTR / WORDPAT/WORDSTR.
x if x == CCT_CURPAT
|| x == CCT_CURSTR
|| x == CCT_WORDPAT
|| x == CCT_WORDSTR =>
{
// c:2722/2726 — tt = clwpos (cur*) or 0 (word*).
let tt = if x == CCT_CURPAT || x == CCT_CURSTR {
clwpos
} else {
0
};
if let CompcondData::S { p, s } = &cc.u {
let mut a = tt + *p.get(iu).unwrap_or(&0); // c:2728
if a < 0 {
a += clwnum;
}
let sv0 = if a < 0 || a >= clwnum {
String::new()
} else {
CLWORDS
.lock()
.unwrap()
.get(a as usize)
.cloned()
.unwrap_or_default()
}; // c:2730
let sv = untokenize(&sv0); // c:2732
let patstr = s.get(iu).map(|x| x.as_str()).unwrap_or("");
if x == CCT_CURPAT || x == CCT_WORDPAT {
// c:2736-2738 — pattern match.
let mut ss = patstr.to_string();
tokenize(&mut ss);
t = patcompile(&ss, PAT_HEAPDUP as i32, None)
.map_or(false, |pp| pattry(&pp, &sv))
as i32;
} else {
// c:2740 — !strcmp(s, rembslash(...)).
t = (sv == rembslash(patstr)) as i32;
}
}
}
// c:2742 — CCT_RANGESTR / CCT_RANGEPAT.
x if x == CCT_RANGESTR || x == CCT_RANGEPAT => {
let is_pat = x == CCT_RANGEPAT;
if let CompcondData::L { a, b } = &cc.u {
let astr = a.get(iu).cloned().unwrap_or_default();
let bstr: Option<String> = b.get(iu).cloned().flatten();
let words = CLWORDS.lock().unwrap().clone();
// c:2744 — for RANGEPAT tokenize a[i] once.
let sc_a = if is_pat {
let mut z = astr.clone();
tokenize(&mut z);
z
} else {
rembslash(&astr) // c:2749
};
// c:2746 — scan backwards from clwpos-1.
let mut j = clwpos - 1;
while j > 0 {
let sv = untokenize(
words.get(j as usize).map(|x| x.as_str()).unwrap_or(""),
); // c:2747
let matched = if is_pat {
patcompile(&sc_a, PAT_HEAPDUP as i32, None)
.map_or(false, |pp| pattry(&pp, &sv))
} else {
sv.len() >= sc_a.len() && sv.starts_with(&sc_a)
}; // c:2750-2753
if matched {
*BRANGE.lock().unwrap() = j + 1; // c:2755
t = 1;
break;
}
j -= 1;
}
// c:2761 — if matched and there's an upper bound.
if let (true, Some(bstr)) = (t != 0, bstr) {
// c:2761 — `t && cc->u.l.b[i]`: an explicit empty `r[a,]` is non-NULL.
let sc_b = if is_pat {
let mut z = bstr.clone();
tokenize(&mut z);
z
} else {
rembslash(&bstr)
};
let mut k = j + 1; // c:2764
while k < clwnum {
let sv = untokenize(
words.get(k as usize).map(|x| x.as_str()).unwrap_or(""),
);
let matched = if is_pat {
patcompile(&sc_b, PAT_HEAPDUP as i32, None)
.map_or(false, |pp| pattry(&pp, &sv))
} else {
sv.len() >= sc_b.len() && sv.starts_with(&sc_b)
};
if matched {
*ERANGE.lock().unwrap() = k - 1; // c:2773
t = if clwpos <= k - 1 { 1 } else { 0 }; // c:2774
break;
}
k += 1;
}
}
}
}
_ => {}
}
i += 1;
}
and_opt = cc.and.as_deref();
}
cc_opt = or;
}
if t != 0 {
// c:2786 — the patterns matched, use the flags.
m = 1;
CCONT.with(|c| c.set(c.get() & !(CC_PATCONT | CC_DEFCONT))); // c:2789
makecomplistor(&compc, os, incmd, compadd, 1); // c:2790
if d == 0 && (CCONT.with(|c| c.get()) & CC_DEFCONT) != 0 {
// c:2791
d = 1;
*BRANGE.lock().unwrap() = 0; // c:2794
*ERANGE.lock().unwrap() = *CLWNUM.lock().unwrap() - 1; // c:2795
makecomplistflags(occ, os.to_string(), incmd, 0); // c:2796
}
if (CCONT.with(|c| c.get()) & CC_PATCONT) == 0 {
break; // c:2798
}
}
compc_opt = compc.next.clone();
}
// c:2802 — if no pattern matched, use the standard flags.
if m == 0 {
*BRANGE.lock().unwrap() = 0; // c:2805
*ERANGE.lock().unwrap() = *CLWNUM.lock().unwrap() - 1; // c:2806
makecomplistflags(occ, os.to_string(), incmd, 0); // c:2807
}
}
// `cmdstr` — current command word being completed.
// Port of file-static `char *cmdstr` (zle_tricky.c). Set by the
// completion driver before invoking makecomplistcmd.
thread_local! { static CMDSTR: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) }; }
/// C body (c:2532-2552):
/// ```c
/// s = ((shfunctab->getnode(shfunctab, cmdstr) ||
/// builtintab->getnode(builtintab, cmdstr)) ? NULL :
/// findcmd(cmdstr, 1, 0));
/// for (pc = patcomps; pc; pc = pc->next) {
/// if ((pat = patcompile(pc->pat, PAT_STATIC, NULL)) &&
/// (pattry(pat, cmdstr) ||
/// (s && pattry(pat, s)))) {
/// makecomplistcc(pc->cc, os, incmd);
/// ret |= 2;
/// if (!(ccont & CC_CCCONT))
/// return ret;
/// }
/// }
/// return ret;
/// ```
/// Port of `makecomplistpc(char *os, int incmd)` from `Src/Zle/compctl.c:2530`.
/// WARNING: param names don't match C — Rust=(incmd) vs C=(os, incmd)
pub(crate) fn makecomplistpc(os: &str, incmd: bool) -> i32 {
// c:2530
let mut ret: i32 = 0; // c:2530
let cmdstr = match CMDSTR.with(|r| r.borrow().clone()) {
// c:2533
Some(s) => s,
None => return 0,
};
// c:2537-2540 — `s = (shfunctab[cmdstr] || builtintab[cmdstr]) ?
// NULL : findcmd(cmdstr, 1, 0);` — only resolve via $PATH when
// cmdstr is neither a defined function nor a builtin.
let is_function = crate::ported::hashtable::shfunctab_lock()
.read()
.map(|t| t.contains_key(&cmdstr))
.unwrap_or(false);
let is_builtin = crate::ported::builtin::BUILTINS
.iter()
.any(|b| b.node.nam == cmdstr);
let s_resolved: Option<String> = if is_function || is_builtin {
// c:2537
None // c:2538 NULL
} else {
findcmd(&cmdstr, 1, 0) // c:2540
};
let pats = PATCOMPS.read().unwrap().clone();
for (pat, cc) in &pats {
// c:2542
// c:2543 — patcompile(pc->pat) compiles the pattern once.
// c:2544-2545 — pattry(prog, cmdstr) || (s && pattry(prog, s)).
let matches = patcompile(
&{
let mut __pat_tok = (pat).to_string();
crate::ported::glob::tokenize(&mut __pat_tok);
__pat_tok
},
PAT_HEAPDUP as i32,
None,
)
.map_or(false, |prog| {
pattry(&prog, &cmdstr) // c:2544
|| s_resolved.as_deref()
.map(|sr| pattry(&prog, sr)) // c:2545
.unwrap_or(false)
});
if matches {
makecomplistcc(cc, os, incmd); // c:2546
ret |= 2; // c:2547
if (CCONT.with(|c| c.get()) & CC_CCCONT) == 0 {
// c:2548
return ret; // c:2549
}
}
}
ret // c:2558
}
/// Re-lex a quoted completion string and complete its current word.
/// Port of `sep_comp_string(char *ss, char *s, int noffs)` from
/// Src/Zle/compctl.c:2813.
///
/// `ss` is the text before the string (the command and whatever else the
/// outer line holds), `s` the string being completed and `noffs` the
/// cursor offset inside `s`. A synthetic line `ss ' ' s[..noffs] 'x'
/// s[noffs..]` is run through the real lexer (the dummy `x` marks the
/// cursor so `gotword` sets `wb`/`we` around the cursor word); the words
/// it yields replace `clwords`, `qipre`/`qisuf` receive the quoted text
/// before and after the word, and `makecomplistcmd` is called on the
/// word. Offsets are CHARACTER offsets throughout (`inbufct`, `wb`, `we`
/// and `zlemetacs` are character based in this port).
pub(crate) fn sep_comp_string(ss: &str, s: &str, noffs: i32) -> i32 {
// c:2813
use crate::ported::hist::{strinbeg, strinend};
use crate::ported::lex::{
ctxtlex, noaliases, set_noaliases, set_tok, set_tokstr, tok, tokstr, untokenize_ztokens as untokenize, LEX_INPUT, LEX_LEXFLAGS,
LEX_LEXSTOP, LEX_POS, LEX_UNGET_BUF, LEX_UNGET_HPTR, LEX_UNGET_RAW,
};
use crate::ported::zle::compcore::{
check_param, multiquote, rembslash, ADDEDX, OFFS, WB, WE, ZLEMETACS, ZLEMETALINE,
ZLEMETALL,
};
use crate::ported::zsh_h::{ENDINPUT, LEXERR, LEXFLAGS_ZLE, STRING_LEX};
use std::sync::atomic::Ordering;
let mut foo: Vec<String> = Vec::new(); // c:2815 LinkList foo
let owe = WE.load(Ordering::SeqCst); // c:2817
let mut owb = WB.load(Ordering::SeqCst);
let ocs = ZLEMETACS.load(Ordering::SeqCst);
let ne = *crate::ported::utils::noerrs_lock().lock().unwrap();
let mut sl = ss.chars().count() as i32; // c:2818
let mut got = false;
let mut i: i32 = 0;
let mut cur: i32 = -1;
let oll = ZLEMETALL.load(Ordering::SeqCst);
let ois = INSTRING.load(Ordering::SeqCst); // c:2819
let oib = INBACKT.load(Ordering::SeqCst);
let ona = noaliases();
let ol = ZLEMETALINE
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.clone(); // c:2820
let oaq = AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.clone();
let mut swb: i32 = 0; // c:2823
let mut swe: i32 = 0;
let mut soffs: i32 = 0;
let mut ns: String = String::new(); // c:2824 ns = NULL
// c:2828-2829 — put the string in the lexer buffer and call the lexer
// to get the words we have to expand.
ADDEDX.store(1, Ordering::SeqCst); // c:2830
crate::ported::utils::set_noerrs(1); // c:2831
crate::ported::context::zcontext_save(); // c:2832
LEX_LEXFLAGS.set(LEXFLAGS_ZLE); // c:2833
let s_chars: Vec<char> = s.chars().collect();
let noffs_u = (noffs.max(0) as usize).min(s_chars.len());
let tl = sl + 3 + s_chars.len() as i32; // c:2834
let mut tmp = String::with_capacity(ss.len() + 3 + s.len());
tmp.push_str(ss); // c:2835
tmp.push(' '); // c:2836
tmp.extend(&s_chars[..noffs_u]); // c:2837
let mut scs = sl + 1 + noffs_u as i32; // c:2838
ZLEMETACS.store(scs, Ordering::SeqCst);
tmp.push('x');
tmp.extend(&s_chars[noffs_u..]); // c:2839
let remq = COMPQSTACK
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.chars()
.next()
.map_or(false, |c| c as i32 == QT_BACKSLASH); // c:2840
if remq {
tmp = rembslash(&tmp); // c:2841
}
// !!! RUST-ONLY (no C counterpart) !!! — the lexer reads its own
// `LEX_INPUT` window and `LEX_UNGET_*` queues ahead of the input
// stack; park them so the nested lex reads only the pushed frame and
// leaks nothing into the caller (same isolation `getcurcmd` and the
// `compctl -s` arm apply).
let saved_lex_input = LEX_INPUT.with_borrow_mut(std::mem::take);
let saved_lex_pos = LEX_POS.replace(0);
let saved_unget = LEX_UNGET_BUF.with_borrow_mut(std::mem::take);
let saved_unget_hptr = LEX_UNGET_HPTR.with_borrow_mut(std::mem::take);
let saved_unget_raw = LEX_UNGET_RAW.with_borrow_mut(std::mem::take);
let saved_unget_srccap =
crate::funcdef_capture::LEX_UNGET_SRCCAP.with_borrow_mut(std::mem::take);
LEX_LEXSTOP.set(false);
crate::ported::input::inpush(&dupstrspace(&tmp), 0, None); // c:2842
*ZLEMETALINE
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = tmp; // c:2843
ZLEMETALL.store(tl - 1, Ordering::SeqCst); // c:2844
strinbeg(0); // c:2845
set_noaliases(true); // c:2846
loop {
// c:2847 do {
ctxtlex(); // c:2848
let mut ts = tokstr();
if tok() == LEXERR {
// c:2849
let tsv = match ts.as_mut() {
Some(v) => v,
None => break, // c:2852-2853
};
// c:2854-2856 — count the Snull/Dnull markers.
let j = tsv.chars().filter(|&c| c == Snull || c == Dnull).count();
if j & 1 == 1 {
// c:2857
set_tok(STRING_LEX); // c:2858 (the global: zshlex returns early on LEXERR)
if tsv.ends_with(' ') {
tsv.pop(); // c:2859-2860 p[-1] = '\0'
}
set_tokstr(Some(tsv.clone()));
}
}
if tok() == ENDINPUT || tok() == LEXERR {
break; // c:2863-2864
}
let mut p: Option<String> = match ts {
Some(v) if !v.is_empty() => Some(v), // c:2865-2866
_ => None, // c:2868
};
if !got && LEX_LEXFLAGS.get() == 0 {
// c:2869 — no current word in substr when `p` is None.
got = true; // c:2871
cur = i; // c:2872
swb = WB.load(Ordering::SeqCst) - 1; // c:2873
swe = WE.load(Ordering::SeqCst) - 1; // c:2874
soffs = ZLEMETACS.load(Ordering::SeqCst) - swb; // c:2875
if let Some(pv) = p.as_mut() {
// c:2876 — chuck(p + soffs): drop the dummy `x`. The
// chucked string is the very one `foo` holds.
if soffs >= 0 {
if let Some((bi, _)) = pv.char_indices().nth(soffs as usize) {
pv.remove(bi);
}
}
ns = pv.clone(); // c:2877
}
}
if let Some(pv) = p {
foo.push(pv); // c:2866 addlinknode(foo, p)
}
i += 1; // c:2879
if tok() == ENDINPUT || tok() == LEXERR {
break; // c:2880 while (tok != ENDINPUT && tok != LEXERR)
}
}
set_noaliases(ona); // c:2881
strinend(); // c:2882
crate::ported::input::inpop(); // c:2883
LEX_INPUT.with_borrow_mut(|b| *b = saved_lex_input);
LEX_POS.set(saved_lex_pos);
LEX_UNGET_BUF.with_borrow_mut(|b| *b = saved_unget);
LEX_UNGET_HPTR.with_borrow_mut(|b| *b = saved_unget_hptr);
LEX_UNGET_RAW.with_borrow_mut(|b| *b = saved_unget_raw);
crate::funcdef_capture::LEX_UNGET_SRCCAP.with_borrow_mut(|b| *b = saved_unget_srccap);
errflag.fetch_and(!crate::ported::utils::ERRFLAG_ERROR, Ordering::Relaxed); // c:2884
crate::ported::utils::set_noerrs(ne); // c:2885
crate::ported::context::zcontext_restore(); // c:2886
WB.store(owb, Ordering::SeqCst); // c:2887
WE.store(owe, Ordering::SeqCst); // c:2888
ZLEMETACS.store(ocs, Ordering::SeqCst); // c:2889
*ZLEMETALINE
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = ol; // c:2890
ZLEMETALL.store(oll, Ordering::SeqCst); // c:2891
if cur < 0 || i < 1 {
return 1; // c:2893
}
owb = OFFS.load(Ordering::SeqCst); // c:2894
OFFS.store(soffs, Ordering::SeqCst); // c:2895
if check_param(&ns, false, true, false).is_some() {
// c:2896
ns = ns
.chars()
.map(|c| match c {
Dnull => '"', // c:2898-2899
Snull => '\'', // c:2900-2901
c => c,
})
.collect();
}
OFFS.store(owb, Ordering::SeqCst); // c:2903
let mut ts: Vec<char> = untokenize(&ns).chars().collect(); // c:2905
let ns_c: Vec<char> = ns.chars().collect();
let cqs: Vec<char> = COMPQSTACK
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.chars()
.collect();
let first = ns_c.first().copied();
if first == Some(Snull)
|| first == Some(Dnull)
|| ((first == Some(Stringg) || first == Some(Qstring)) && ns_c.get(1) == Some(&Snull))
{
// c:2907-2908
let mut tsptr: usize = 0; // c:2909
let mut nsptr: usize = 0;
match first {
Some(Snull) => INSTRING.store(QT_SINGLE, Ordering::SeqCst), // c:2911-2912
Some(Dnull) => INSTRING.store(QT_DOUBLE, Ordering::SeqCst), // c:2915-2916
_ => {
INSTRING.store(QT_DOLLARS, Ordering::SeqCst); // c:2920
nsptr += 1; // c:2921
tsptr += 1; // c:2922
}
}
INBACKT.store(0, Ordering::SeqCst); // c:2926
swb += 1; // c:2927
if ns_c.last() == Some(&ns_c[nsptr]) && ns_c.get(nsptr + 1).is_some() {
swe -= 1; // c:2928-2929
}
tsptr += 1; // c:2930 sav = *++tsptr
let head: String = ts[..tsptr.min(ts.len())].iter().collect(); // c:2931
let has_q1 = cqs.get(1).map_or(false, |&c| c != '\0');
*AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = if has_q1 {
String::new()
} else {
multiquote(&head, 1)
}; // c:2932
ts.drain(..tsptr.min(ts.len())); // c:2933 ts = tsptr
} else {
INSTRING.store(QT_NONE, Ordering::SeqCst); // c:2935
AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.clear(); // c:2936
}
// c:2938-2959 — walk the tokenized word dropping the null markers,
// keeping `scs`, `soffs` and `swb` in step.
let mut np = ns_c;
let mut pi: usize = 0;
let mut ci: i32 = swb;
while pi < np.len() {
let c = np[pi];
let has_next = np.get(pi + 1).is_some();
if matches!(c, Snull | Dnull | Bnull | Bnullkeep | Nularg) {
// c:2939 inull(*p)
if ci < scs {
// c:2940
soffs -= 1; // c:2941
if remq && c == Bnull && has_next {
swb -= 2; // c:2942-2943
}
}
if has_next || c != Bnull {
// c:2945
if c == Bnull {
if scs == ci + 1 {
scs += 1; // c:2947-2948
soffs += 1;
}
} else {
let was = ci;
ci -= 1; // c:2950 scs > i--
if scs > was {
scs -= 1; // c:2951
}
}
} else if scs == swe {
scs -= 1; // c:2954-2955
}
np.remove(pi); // c:2957 chuck(p--)
} else {
pi += 1;
}
ci += 1;
}
let mut ns: String = ts.iter().collect(); // c:2960
if INSTRING.load(Ordering::SeqCst) != QT_NONE && cqs.iter().any(|&c| c as i32 == QT_BACKSLASH)
{
// c:2962
let rl = ns.chars().count() as i32; // c:2963
let ql = multiquote(&ns, cqs.get(1).map_or(0, |&c| (c != '\0') as i32))
.chars()
.count() as i32;
if ql > rl {
swb -= ql - rl; // c:2966
}
}
let ix = (swb - sl - 1).clamp(0, s_chars.len() as i32) as usize; // c:2968
let head: String = s_chars[..ix].iter().collect(); // c:2969
let qipre_v = QIPRE
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.clone();
let qisuf_v = QISUF
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.clone();
let qp = format!("{}{}", qipre_v, multiquote(&head, 0)); // c:2970
if swe < swb {
swe = swb; // c:2972-2973
}
swe -= sl + 1; // c:2974
sl = s_chars.len() as i32; // c:2975
if swe > sl {
// c:2976
swe = sl; // c:2977
let keep = (swe - swb + 1).max(0) as usize;
if ns.chars().count() > keep {
ns = ns.chars().take(keep).collect(); // c:2979
}
}
let tail: String = s_chars[(swe.max(0) as usize).min(s_chars.len())..]
.iter()
.collect();
let qs = format!("{}{}", multiquote(&tail, 0), qisuf_v); // c:2981
sl = ns.chars().count() as i32; // c:2982
if soffs > sl {
soffs = sl; // c:2983-2984
}
{
// c:2986-3031
let ow = CLWORDS.lock().unwrap().clone(); // c:2987
let os = CMDSTR.with(|r| r.borrow().clone());
let oqp = qipre_v;
let oqs = qisuf_v;
let oqst: String = cqs.iter().collect(); // c:2988
let olws = *CLWSIZE.lock().unwrap(); // c:2989
let olwn = *CLWNUM.lock().unwrap();
let olwp = *CLWPOS.lock().unwrap();
let obr = *BRANGE.lock().unwrap(); // c:2990
let oer = *ERANGE.lock().unwrap();
let oof = OFFS.load(Ordering::SeqCst);
let occ = CCONT.with(|c| c.get()); // c:2991
// c:2993-2996 — compnewchar is the new innermost quoting level.
let ins = INSTRING.load(Ordering::SeqCst);
let compnewchar = if ins != QT_NONE { ins } else { QT_BACKSLASH };
let mut nq = String::new();
nq.push(char::from_u32(compnewchar as u32).unwrap_or('\\'));
nq.push_str(&oqst);
*COMPQSTACK
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = nq;
// c:2998-3004 — clwords = the lexed words, untokenized.
let words: Vec<String> = foo.iter().map(|w| untokenize(w)).collect();
*CLWSIZE.lock().unwrap() = words.len() as i32; // c:2998
*CLWNUM.lock().unwrap() = words.len() as i32;
let cmd0 = words.first().cloned(); // c:3006
*CLWORDS.lock().unwrap() = words; // c:2999-3004
*CLWPOS.lock().unwrap() = cur; // c:3005
CMDSTR.with(|r| *r.borrow_mut() = cmd0);
*BRANGE.lock().unwrap() = 0; // c:3007
*ERANGE.lock().unwrap() = *CLWNUM.lock().unwrap() - 1; // c:3008
*QIPRE
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = qp; // c:3009
*QISUF
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = qs; // c:3010
OFFS.store(soffs, Ordering::SeqCst); // c:3011
CCONT.with(|c| c.set(CC_CCCONT)); // c:3012
let _ = makecomplistcmd(&ns, cur == 0, CFN_FIRST); // c:3013
CCONT.with(|c| c.set(occ)); // c:3014
OFFS.store(oof, Ordering::SeqCst); // c:3015
CMDSTR.with(|r| *r.borrow_mut() = os); // c:3016-3017
*CLWORDS.lock().unwrap() = ow; // c:3018-3019
*CLWSIZE.lock().unwrap() = olws; // c:3020
*CLWNUM.lock().unwrap() = olwn; // c:3021
*CLWPOS.lock().unwrap() = olwp; // c:3022
*BRANGE.lock().unwrap() = obr; // c:3023
*ERANGE.lock().unwrap() = oer; // c:3024
*QIPRE
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = oqp; // c:3025-3026
*QISUF
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = oqs; // c:3027-3028
*COMPQSTACK
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = oqst; // c:3029-3030
}
*AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = oaq; // c:3032
INSTRING.store(ois, Ordering::SeqCst); // c:3033
INBACKT.store(oib, Ordering::SeqCst); // c:3034
0 // c:3036
}
// `ccused` — per-completion list of compctls used. Port of
// file-static `LinkList ccused` at Src/Zle/compctl.c:2574.
thread_local! { static CCUSED: std::cell::RefCell<Vec<Arc<Compctl>>> = const { std::cell::RefCell::new(Vec::new()) }; }
// `ccstack` — the compctls currently being expanded; guards against a
// compctl referring back to itself. Port of file-static `LinkList ccstack`
// at Src/Zle/compctl.c:1706.
thread_local! { static CCSTACK: std::cell::RefCell<Vec<Arc<Compctl>>> = const { std::cell::RefCell::new(Vec::new()) }; }
/// Walk the xor chain of compctls.
/// Port of `makecomplistor(Compctl cc, char *s, int incmd, int compadd, int sub)` from Src/Zle/compctl.c:2574.
///
/// C body:
/// - Loop over xors (cc->xor chain)
/// - For each, call makecomplistlist
/// - Track newly-added matches (mn diff)
/// - Stop based on ccont bits (CC_PATCONT, CC_DEFCONT, CC_XORCONT)
/// WARNING: param names don't match C — Rust=(s, incmd, compadd, sub) vs C=(cc, s, incmd, compadd, sub)
pub(crate) fn makecomplistor(cc: &Arc<Compctl>, s: &str, incmd: bool, compadd: i32, sub: i32) {
use crate::ported::zle::zle_tricky::USEMENU;
let mut um = USEMENU.load(Ordering::Relaxed); // c:2583
let mut cc: Option<Arc<Compctl>> = Some(cc.clone());
// Loop over xors.
loop {
let mn = crate::ported::zle::compcore::mnum.load(Ordering::Relaxed); // c:2587
// Loop over ors.
loop {
// Reset the range information if we are not in a sub-list.
if sub == 0 {
*BRANGE.lock().unwrap() = 0; // c:2593
*ERANGE.lock().unwrap() = *CLWNUM.lock().unwrap() - 1; // c:2594
}
USEMENU.store(0, Ordering::Relaxed); // c:2596
let cur = cc.take().unwrap();
makecomplistlist(&cur, s, incmd, compadd); // c:2597
um |= USEMENU.load(Ordering::Relaxed); // c:2598
let ct = cur.mask2 & CC_XORCONT; // c:2600
// cc = cc->xor; a link to the old cc_default (refc 10000, the
// static marker) means the current cc_default.
cc = cur.xor.as_ref().map(|x| {
if x.refc == 10000 {
CC_DEFAULT.lock().unwrap().clone().unwrap_or_else(|| x.clone())
} else {
x.clone()
}
});
if !(cc.is_some() && ct != 0) {
break; // c:2603 while (cc && ct)
}
}
// Stop if we got some matches.
if mn != crate::ported::zle::compcore::mnum.load(Ordering::Relaxed) {
break; // c:2607
}
if cc.is_some() {
CCONT.with(|c| c.set(c.get() & !(CC_DEFCONT | CC_PATCONT))); // c:2609
if sub == 0 {
CCONT.with(|c| c.set(c.get() & !CC_CCCONT)); // c:2611
}
}
if cc.is_none() {
break; // c:2613 while (cc)
}
}
USEMENU.store(um, Ordering::Relaxed); // c:2615
}
/// The flag-driven completion-list builder — workhorse fn.
/// Port of `makecomplistflags(Compctl cc, char *s, int incmd, int compadd)` from Src/Zle/compctl.c:3042 (~500 lines).
///
/// Opens with the c:3070-3403 PREAMBLE, which computes the line/real/
/// path/file prefix+suffix statics (LPRE/RPRE/PPRE/FPRE/… and their
/// quoted forms) that `addmatch` reads back when building each Cmatch,
/// then dispatches the per-CC_* generation arms below. See the preamble
/// header inside the fn for the sub-parts it does not build.
///
/// Walks the bits of cc.mask and cc.mask2, dispatching per CC_* bit
/// to the matching generator:
/// CC_FILES → gen_matches_files (regular files)
/// CC_DIRS → gen_matches_files(1, 0, 0)
/// CC_COMMPATH → command-path completion
/// CC_OPTIONS → option completion
/// CC_VARS → dumphashtable(paramtab, CC_VARS)
/// CC_BINDINGS → bindings (zle widgets)
/// CC_ARRAYS → param table filtered to PM_ARRAY
/// CC_INTVARS → param table filtered to PM_INTEGER
/// CC_SHFUNCS → shfunctab
/// CC_PARAMS → paramtab non-exported
/// CC_ENVVARS → paramtab PM_EXPORTED
/// CC_JOBS / CC_RUNNING / CC_STOPPED → job table filters
/// CC_BUILTINS → builtintab
/// CC_USERS → /etc/passwd users (or named-dir filltable)
/// CC_DISCMDS / CC_EXCMDS → cmdnamtab filtered by DISABLED bit
/// CC_RESWDS → reserved-word table
/// CC_NAMED → named-directory table
/// CC_DIRS → directory matches
/// ... and more
///
/// Plus arg-taking flags:
/// cc.glob → globlist expansion
/// cc.str → string-arg expansion via singsub
/// cc.func → call user function (compctl -K)
/// cc.keyvar → read array variable for matches
/// cc.hpat → history-pattern matches
///
/// Per-flag generators dispatched directly: CC_FILES → file walker,
/// CC_DIRS → dir-only walker, CC_NAMED → maketildelist, CC_VARS &
/// friends → paramtab walk filtered by PM_*, CC_SHFUNCS →
/// shfunctab walk, CC_BUILTINS → builtin table walk, CC_DISCMDS /
/// CC_EXCMDS / CC_EXTCMDS → cmdnamtab walk, CC_RESWDS → reswdtab
/// walk. cc.func → shfunc_call dispatch, cc.glob → addmatch, cc.str
/// → singsub-expanded addmatch.
pub(crate) fn makecomplistflags(cc: &Arc<Compctl>, mut s: String, incmd: bool, compadd: i32) {
use crate::ported::string::dupstrpfx;
use crate::ported::utils::quotestring;
use crate::ported::zle::comp_h::{Cexpl, CGF_NOSORT, CGF_UNIQALL, CGF_UNIQCON, CMF_REMOVE};
use crate::ported::zle::compcore::{begcmgroup, check_param, endcmgroup, rembslash};
use crate::ported::zsh_h::{Dash, Equals, Star, Stringg, Tick, Tilde};
use crate::ported::zsh_h::{
ALIAS_GLOBAL, DISABLED, PM_ARRAY, PM_EXPORTED, PM_INTEGER, PM_READONLY, PM_SCALAR,
PM_SPECIAL, PM_UNSET,
};
use std::sync::atomic::Ordering;
// c:3045-3047 — `int ... um = usemenu ...; int mn = mnum, ohp = haspattern;
// char ... *os = dupstring(s);`
let um = crate::ported::zle::zle_tricky::USEMENU.load(Ordering::Relaxed);
let mn = crate::ported::zle::compcore::mnum.load(Ordering::Relaxed);
let ohp = crate::ported::zle::compcore::haspattern.load(Ordering::Relaxed);
let os = s.clone();
// c:3049 — `ccont |= (cc->mask2 & (CC_CCCONT | CC_DEFCONT | CC_PATCONT));`
// is applied further down together with the rest of the state setup.
// c:3052-3057 — refuse to expand a compctl that is already being
// expanded; otherwise push it on `ccstack`.
if INCOMPFUNC.load(Ordering::Relaxed) != 1
&& CCSTACK.with(|r| r.borrow().iter().any(|c| Arc::ptr_eq(c, cc)))
{
return;
}
CCSTACK.with(|r| r.borrow_mut().push(cc.clone()));
// c:3998-4000 — undone on every exit: `uremnode(ccstack, firstnode(ccstack));
// if (cc->matcher) mstack = mstack->next;`
struct CcstackPop {
matcher_pushed: bool,
}
impl Drop for CcstackPop {
fn drop(&mut self) {
CCSTACK.with(|r| {
r.borrow_mut().pop();
});
if self.matcher_pushed {
if let Some(cell) = crate::ported::zle::compcore::mstack.get() {
if let Ok(mut g) = cell.lock() {
*g = g.take().and_then(|top| top.next.clone());
}
}
}
}
}
let mut ccstack_pop = CcstackPop { matcher_pushed: false };
// c:3066-3068 — "Go to the end of the word if complete_in_word is not
// set."
//
// if (unset(COMPLETEINWORD) && zlemetacs != we)
// zlemetacs = we, offs = strlen(s);
//
// This ran BEFORE the c:3070 `s = dupstring(s)` the preamble below
// starts at, and the port began one statement late, so it was missing
// outright. With the option off — the default — a mid-word TAB left
// `offs` pointing at the cursor, `rpre`/`rsuf` split the word there, and
// every candidate had to match the text to the RIGHT of the cursor as a
// suffix. `compctl -k '(alpha beta gamma)' foo` with the line `foo al|p`
// therefore produced no matches at all and the line was left untouched,
// where zsh moves the cursor to the end of `alp` and inserts `alpha`.
//
// `offs` is a CHARACTER index everywhere in this port (`zle_tricky.rs`
// stores `zlemetacs - wb`, both character counts), so C's byte-wise
// `strlen(s)` becomes `s.chars().count()`.
{
use crate::ported::zle::compcore::{OFFS, WE, ZLEMETACS};
let we = WE.load(Ordering::Relaxed);
if !crate::ported::zsh_h::isset(crate::ported::zsh_h::COMPLETEINWORD)
&& ZLEMETACS.load(Ordering::Relaxed) != we
{
ZLEMETACS.store(we, Ordering::Relaxed);
OFFS.store(s.chars().count() as i32, Ordering::Relaxed);
}
}
// =================================================================
// makecomplistflags preamble — Src/Zle/compctl.c:3070-3403.
// Computes the prefix/suffix file-statics that `addmatch` reads back
// when constructing each Cmatch. This is a faithful port of the
// state-SETUP half of makecomplistflags; the per-flag GENERATION
// half stays in the arms further down. Reads/writes the canonical
// ZLE globals (offs/ipre/ripre/mflags/hasmatched in compcore.rs)
// plus the compctl-private prefix statics declared near ADDWHAT.
// =================================================================
let incompfunc = INCOMPFUNC.load(std::sync::atomic::Ordering::Relaxed);
let instr = INSTRING.load(Ordering::Relaxed);
// Port of the `quotename(s)` macro (c:1757). NB: C's
// `quotestring("", QT_BACKSLASH)` returns "" (the `''` form is only
// emitted under QT_BACKSLASH_SHOWNULL, utils.c:6165), but the Rust
// `utils::quotestring` currently returns "''" for a plain-backslash
// empty string — a divergence in that fn that would poison every
// empty prefix/suffix. Guard the empty case so the compctl statics
// match C until utils::quotestring is corrected.
let quotename = |x: &str| -> String {
if x.is_empty() {
String::new()
} else {
quotestring(
x,
if instr == QT_NONE {
QT_BACKSLASH
} else {
instr
},
)
}
};
let mut delit = false; // c:3071 delit = 0
crate::ported::zle::zle_tricky::USEMENU.store(um, Ordering::Relaxed); // c:3072 usemenu = um
// c:3073-3076 — reset compiled patterns and every prefix static.
PATCOMP.with(|r| *r.borrow_mut() = None);
FILECOMP.with(|r| *r.borrow_mut() = None);
LPRE.with(|r| r.borrow_mut().clear());
LSUF.with(|r| r.borrow_mut().clear());
RPRE.with(|r| r.borrow_mut().clear());
RSUF.with(|r| r.borrow_mut().clear());
PPRE.with(|r| r.borrow_mut().clear());
PSUF.with(|r| r.borrow_mut().clear());
LPPRE.with(|r| r.borrow_mut().clear());
LPSUF.with(|r| r.borrow_mut().clear());
FPRE.with(|r| r.borrow_mut().clear());
FSUF.with(|r| r.borrow_mut().clear());
QFPRE.with(|r| r.borrow_mut().clear());
QFSUF.with(|r| r.borrow_mut().clear());
QRPRE.with(|r| r.borrow_mut().clear());
QRSUF.with(|r| r.borrow_mut().clear());
QLPRE.with(|r| r.borrow_mut().clear());
QLSUF.with(|r| r.borrow_mut().clear());
if let Some(m) = crate::ported::zle::compcore::ipre.get() {
if let Ok(mut g) = m.lock() {
g.clear();
}
}
if let Some(m) = crate::ported::zle::compcore::ripre.get() {
if let Ok(mut g) = m.lock() {
g.clear();
}
}
// c:3078 — curcc = cc.
CURCC.with(|r| *r.borrow_mut() = Some(cc.clone()));
// c:3080-3083 — mflags reset + group flags from mask2.
crate::ported::zle::compcore::mflags.store(0, Ordering::Relaxed);
let gflags = (if (cc.mask2 & CC_NOSORT) != 0 {
CGF_NOSORT
} else {
0
}) | (if (cc.mask2 & CC_UNIQALL) != 0 {
CGF_UNIQALL
} else {
0
}) | (if (cc.mask2 & CC_UNIQCON) != 0 {
CGF_UNIQCON
} else {
0
});
// c:3084-3091 — start a fresh match group for -J/-V (gname) or -y.
if cc.gname.is_some() {
endcmgroup(None);
begcmgroup(cc.gname.as_deref(), gflags);
}
if cc.ylist.is_some() {
endcmgroup(None);
begcmgroup(None, gflags);
}
// c:3092-3093 — CC_REMOVE contributes CMF_REMOVE to mflags.
if (cc.mask & CC_REMOVE) != 0 {
crate::ported::zle::compcore::mflags.fetch_or(CMF_REMOVE, Ordering::Relaxed);
}
// c:3094-3098 — explanation accumulator.
let cell = crate::ported::zle::compcore::curexpl.get_or_init(|| Mutex::new(None));
if let Ok(mut g) = cell.lock() {
*g = if cc.explain.is_some() {
Some(Cexpl::default())
} else {
None
};
}
// c:3101-3113 — compadd chars are ignored at the start of the word
// and become the ignored-prefix `ipre`.
let mut offs = crate::ported::zle::compcore::OFFS.load(Ordering::Relaxed);
if compadd > 0 {
// C counts bytes of the tokenized word, where a token is ONE byte; a
// token is a multi-byte private-use char here, so count it as one.
let mut ca = 0usize;
let mut counted = 0i32;
for (i, ch) in s.char_indices() {
if counted >= compadd {
break;
}
counted += if crate::token_char::itok_char(ch) {
1
} else {
ch.len_utf8() as i32
};
ca = i + ch.len_utf8();
}
let ip = crate::ported::lex::untokenize_ztokens(&s[..ca]);
let cell = crate::ported::zle::compcore::ipre.get_or_init(|| Mutex::new(String::new()));
if let Ok(mut g) = cell.lock() {
*g = ip;
}
crate::ported::zle::compcore::WB.fetch_add(compadd, std::sync::atomic::Ordering::Relaxed);
s = s[ca..].to_string();
offs -= compadd;
if offs < 0 {
// c:3108-3111 — compadd bigger than our word prefix: bail.
return;
}
}
// c:3115-3127 — the compctl's own matcher goes on the matcher stack.
if let Some(m) = cc.matcher.as_ref() {
let cell = crate::ported::zle::compcore::mstack.get_or_init(|| Mutex::new(None));
if let Ok(mut g) = cell.lock() {
let prev = g.take(); // c:3116 ms.next = mstack
*g = Some(Arc::new(Cmlist {
next: prev,
matcher: m.clone(), // c:3117 ms.matcher = cc->matcher
str: String::new(),
})); // c:3118 mstack = &ms
}
ccstack_pop.matcher_pushed = true;
if crate::ported::zle::compcore::mnum.load(Ordering::Relaxed) == 0 {
crate::ported::zle::compmatch::add_bmatchers(Some(&**m)); // c:3121
}
if let Ok(mut mlist) = crate::ported::zle::compcore::matchers
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
{
mlist.push(m.clone()); // c:3124 addlinknode(matchers, cc->matcher)
}
}
if crate::ported::zle::compcore::mnum.load(Ordering::Relaxed) != 0
&& (crate::ported::zle::compcore::mstack
.get()
.and_then(|c| c.lock().ok().map(|g| g.is_some()))
.unwrap_or(false)
|| crate::ported::zle::compcore::bmatchers
.get()
.and_then(|c| c.lock().ok().map(|g| g.is_some()))
.unwrap_or(false))
{
crate::ported::zle::compmatch::update_bmatchers(); // c:3127
}
// c:3130-3143 — -P prefix: skip the part already typed on the line.
if let Some(prefix) = cc.prefix.as_ref() {
if !s.is_empty() {
let dp = rembslash(prefix);
let sd = crate::ported::lex::untokenize_ztokens(&s);
let mut pl = crate::ported::zle::zle_tricky::pfxlen(&dp, &sd).min(s.len());
while pl > 0 && !s.is_char_boundary(pl) {
pl -= 1;
}
s = s[pl..].to_string();
offs -= pl as i32;
}
}
// c:3145-3162 — -S suffix: if the suffix is already on the word, drop it.
if let Some(suffix) = cc.suffix.as_ref() {
let mut sdup = rembslash(suffix);
// c:3150-3151 — ignore trailing spaces on the suffix.
while sdup.ends_with(' ') {
sdup.pop();
}
let sd = crate::ported::lex::untokenize_ztokens(&s);
let sl = sdup.len();
let suffixll = sd.len();
if !sd.is_empty()
&& suffixll >= sl
&& (offs as usize) <= suffixll - sl
&& sd.ends_with(sdup.as_str())
{
let cut = suffixll - sl;
if cut <= s.len() && s.is_char_boundary(cut) {
s.truncate(cut);
}
}
}
// c:3163-3165 — leading `~`/`=` special char. get_comp_string returns
// the word untokenized, so `~`/`=` arrive as their literal bytes rather
// than the Tilde/Equals tokens C tests here; normalize the literals to
// the tokens so the ic==Tilde (username) / ic==Equals (command) arms fire.
let ic_char = s.chars().next().unwrap_or('\0');
let mut ic = if incompfunc != 0 {
'\0'
} else if ic_char == Tilde || ic_char == '~' {
Tilde
} else if ic_char == Equals || ic_char == '=' {
Equals
} else {
'\0'
};
// c:3160-3167 — parameter-name completion redirect. When check_param
// detects a `$…` context it advances `s` past the `$`, decrements offs
// (inside check_param, via OFFS), and swaps the active compctl to
// cc_dummy with mask CC_PARAMS | CC_ENVVARS so the flag-walk below
// dumps parameter names. Without the swap, `echo $HOM<Tab>` fell
// through to cc_default (CC_FILES) and produced nothing.
let param_cc: Option<Arc<Compctl>> = if incompfunc == 0 {
let saved_wb = crate::ported::zle::compcore::WB.load(Ordering::Relaxed);
crate::ported::zle::compcore::OFFS.store(offs, Ordering::Relaxed);
if let Some(p) = check_param(&s, true, false, true) {
let mut p = p.min(s.len());
while p > 0 && !s.is_char_boundary(p) {
p -= 1;
}
s = s[p..].to_string(); // c:3162 — s = p
offs = crate::ported::zle::compcore::OFFS.load(Ordering::Relaxed);
// check_param advances wb past the `$` sigil (the `$` becomes the
// ignored prefix `ipre`). add_match_data still splices ipre into
// every match's line cline (Src/Zle/compcore.c:2827), so the
// inserted string carries the `$`. In the compsys path callcompfunc
// resets `wb = parwb` (c:739) to re-anchor at the `$`; the compctl
// path has no such reset, so do it here — otherwise the on-line `$`
// plus the cline's `$` doubled (`$HOM<Tab>` → `$$HOME`). Keep wb at
// the sigil so the ambiguous-insert region covers it.
crate::ported::zle::compcore::WB.store(saved_wb, Ordering::Relaxed);
// c:3164-3166 — cc = &cc_dummy; mask = CC_PARAMS | CC_ENVVARS.
Some(Arc::new(Compctl {
refc: 10000,
mask: CC_PARAMS | CC_ENVVARS,
..Default::default()
}))
} else {
None
}
} else {
None
};
let cc: &Arc<Compctl> = param_cc.as_ref().unwrap_or(cc);
let ooffs = offs; // c:3171 ooffs = offs
// c:3177-3183 — CC_DELETE blanks the word entirely.
if (cc.mask & CC_DELETE) != 0 {
delit = true;
s.clear();
offs = 0;
if crate::ported::zsh_h::isset(crate::ported::zsh_h::AUTOMENU) {
crate::ported::zle::zle_tricky::USEMENU.store(1, Ordering::Relaxed); // c:3181
}
}
crate::ported::zle::compcore::OFFS.store(offs, Ordering::Relaxed);
// c:3185-3213 — line prefix / suffix around the cursor offset.
let cut = {
let mut c = (offs.max(0) as usize).min(s.len());
while c > 0 && !s.is_char_boundary(c) {
c -= 1;
}
c
};
// c:3186-3201 — `comppatmatch` (a possibly-empty string, NULL when unset)
// turns the Dash token back into a literal `-` in the line prefix.
let cpm: Option<String> = crate::ported::zle::compcore::comppatmatch
.get()
.and_then(|m| m.lock().ok())
.map(|g| g.clone());
let cpm_nonempty = cpm.as_deref().is_some_and(|c| !c.is_empty()); // `comppatmatch && *comppatmatch`
let lpre_s: String = if cpm.is_some() {
s[..cut].chars().map(|c| if c == Dash { '-' } else { c }).collect()
} else {
s[..cut].to_string()
};
let lsuf_s = s[cut..].to_string();
LPL.with(|c| c.set(lpre_s.len() as i32));
LSL.with(|c| c.set(lsuf_s.len() as i32));
let qlpre_s = quotename(&lpre_s);
let qlsuf_s = quotename(&lsuf_s);
// c:3216-3223 — a `~` with a `/` after it is a plain path, not special.
if ic == Tilde && lpre_s.contains('/') {
ic = '\0';
}
IC.with(|c| c.set(ic));
// c:3224-3236 — real prefix/suffix: getreal-expand when the word
// contains a substitution/backtick token or begins with `~`/`=`,
// otherwise a plain copy.
let ispar = crate::ported::zle::compcore::ispar.load(Ordering::Relaxed);
let mut noreal = if delit { 0 } else { 1 };
let lpre_has_tok = lpre_s.chars().any(|c| c == Stringg || c == Tick);
let tt: &str = if ic != '\0' && ispar == 0 && !lpre_s.is_empty() {
&lpre_s[lpre_s.chars().next().map(|c| c.len_utf8()).unwrap_or(0)..]
} else {
&lpre_s
};
let first_lpre = lpre_s.chars().next();
let mut rpre_s = if lpre_has_tok || first_lpre == Some(Tilde) || first_lpre == Some(Equals) {
noreal = 0;
getreal(tt)
} else if (first_lpre == Some('~') && lpre_s.contains('/')) || first_lpre == Some('=') {
// get_comp_string returns the word untokenized, so a leading `~`/`=`
// arrives as its literal byte rather than the Tilde/Equals token that
// getreal's filesub/equalsubst needs to expand. Tokenize a COPY for
// the expansion only — mutating `s` itself would shift every byte
// offset (the tokens are multi-byte in UTF-8) and corrupt fpre. rpre
// becomes the expanded filesystem path (`~/f` → `$HOME/f`) used for
// opendir, while lppre keeps the literal `~/` that stays on the line.
//
// Only a tilde with a `/` is a PATH to expand (`~/f`, `~user/f`). A
// bare `~` or `~user` (no slash) is a username-completion context in
// zsh (maketildelist), NOT an expansion — getreal-expanding it errors
// ("no such user") or wrongly replaces `~` with $HOME on the line.
noreal = 0;
let mut tok = tt.to_string();
crate::ported::glob::tokenize(&mut tok);
getreal(&tok)
} else {
tt.to_string()
};
let qrpre_s = quotename(&rpre_s);
let lsuf_has_tok = lsuf_s.chars().any(|c| c == Stringg || c == Tick);
let mut rsuf_s = if lsuf_has_tok {
noreal = 0;
getreal(&lsuf_s)
} else {
lsuf_s.clone()
};
let qrsuf_s = quotename(&rsuf_s);
NOREAL.with(|c| c.set(noreal));
// c:3238-3261 — is the word a pattern? The loops transcribe C's: `rpre`
// is scanned backwards (c:3239-3248), `rsuf` forwards (c:3250-3259), both
// while still tokenized, recording the last slash of `rpre` (s1), the first
// slash of `rsuf` (s2) and how many slashes were seen (sf1/sf2).
let mut ispattern_v: i32 = 0;
let mut s1_t: Option<usize> = None;
let mut sf1: i32 = 0;
for (idx, ch) in rpre_s.char_indices().rev() {
if ispattern_v == 3 && sf1 != 0 {
break; // c:3242 `(ispattern != 3 || !sf1)`
}
if crate::token_char::itok_char(ch) && (idx > 0 || (ch != Equals && ch != Tilde)) {
ispattern_v |= if sf1 != 0 { 1 } else { 2 }; // c:3244
} else if ch == '/' {
sf1 += 1; // c:3246
if s1_t.is_none() {
s1_t = Some(idx); // c:3247
}
}
}
let mut s2_t: Option<usize> = None;
let mut sf2: i32 = 0;
let mut t: i32 = 0;
for (idx, ch) in rsuf_s.char_indices() {
if t != 0 && sf2 != 0 {
break; // c:3251 `(!t || !sf2)`
}
if crate::token_char::itok_char(ch) {
t |= if sf2 != 0 { 4 } else { 2 }; // c:3253
} else if ch == '/' {
sf2 += 1; // c:3255
if s2_t.is_none() {
s2_t = Some(idx); // c:3256
}
}
}
ispattern_v |= t; // c:3260
// c:3263-3265 — never treat the word as a pattern when asked not to do
// glob completion.
if !cpm_nonempty {
ispattern_v = 0;
}
// c:3267-3282 — the word is a pattern: compile `rpre` `*` `rsuf`.
let mut patcomp_set = false;
if ispattern_v != 0 {
let mut p = rpre_s.clone(); // c:3270
if !rpre_s.is_empty()
&& !rpre_s.ends_with(Star)
&& cpm.as_deref().is_some_and(|c| c.starts_with('*'))
{
p.push(Star); // c:3273
}
p.push_str(&rsuf_s); // c:3274 / c:3277
let prog = patcompile(&p, 0, None); // c:3278
patcomp_set = prog.is_some();
PATCOMP.with(|r| *r.borrow_mut() = prog);
crate::ported::zle::compcore::haspattern.store(1, Ordering::Relaxed); // c:3279
}
crate::ported::zle::compcore::ispattern.store(ispattern_v, Ordering::Relaxed);
// c:3281-3293 — without a pattern the prefix/suffix are plain text
// (`untokenize`); with one only the Dash token is turned back into `-`.
// The slash offsets are remapped through the same transformation so they
// stay valid indices into the converted strings.
let rpre_t = rpre_s;
let rsuf_t = rsuf_s;
let fix = |tok: &str| -> String {
if patcomp_set {
tok.chars().map(|c| if c == Dash { '-' } else { c }).collect()
} else {
crate::ported::lex::untokenize_ztokens(tok)
}
};
let s1: Option<usize> = s1_t.map(|i| fix(&rpre_t[..i]).len());
let s2: Option<usize> = s2_t.map(|i| fix(&rsuf_t[..i]).len());
let rpre_s = fix(&rpre_t);
let rsuf_s = fix(&rsuf_t);
RPL.with(|c| c.set(rpre_s.len() as i32));
RSL.with(|c| c.set(rsuf_s.len() as i32));
// c:3295-3296 — untokenize the line prefix/suffix.
let lpre_s = crate::ported::lex::untokenize_ztokens(&lpre_s);
let lsuf_s = crate::ported::lex::untokenize_ztokens(&lsuf_s);
// Commit the real/line statics.
LPRE.with(|r| *r.borrow_mut() = lpre_s.clone());
LSUF.with(|r| *r.borrow_mut() = lsuf_s.clone());
RPRE.with(|r| *r.borrow_mut() = rpre_s.clone());
RSUF.with(|r| *r.borrow_mut() = rsuf_s.clone());
QLPRE.with(|r| *r.borrow_mut() = qlpre_s);
QLSUF.with(|r| *r.borrow_mut() = qlsuf_s);
QRPRE.with(|r| *r.borrow_mut() = qrpre_s);
QRSUF.with(|r| *r.borrow_mut() = qrsuf_s);
// c:3298-3299 — a non-delete completion counts as "has matched".
if (cc.mask & CC_DELETE) == 0 {
crate::ported::zle::compcore::hasmatched.store(1, Ordering::Relaxed);
}
// c:3050 — ccont gets the mask2 continuation bits.
CCONT.with(|c| c.set(c.get() | (cc.mask2 & (CC_CCCONT | CC_DEFCONT | CC_PATCONT))));
// c:3301-3639 — file completion: derive the path/file prefix+suffix, then
// generate the matches.
let mut ppre = String::new();
let mut psuf = String::new();
if (cc.mask & (CC_FILES | CC_DIRS | CC_COMMPATH)) != 0 || cc.glob.is_some() {
// c:3303-3316 — path prefix/suffix; `s1 = rpre`/`s2 = rsuf + rsl` when
// there was no slash.
let ppre_s = match s1 {
Some(idx) => rpre_s[..idx + 1].to_string(),
None => String::new(),
};
let psuf_s = match s2 {
Some(idx) => rsuf_s[idx..].to_string(),
None => String::new(),
};
// c:3318-3374 — lppre/lpsuf: the directory portion of the word as it
// is on the line, with the brace insertions (`brbeg`/`brend`) and the
// quoted ignored prefix/suffix taken out.
let meta = crate::ported::zle::compcore::ZLEMETALINE
.get()
.and_then(|m| m.lock().ok().map(|g| g.clone()))
.unwrap_or_default();
let wb = crate::ported::zle::compcore::WB.load(Ordering::Relaxed).max(0) as usize;
let we = crate::ported::zle::compcore::WE.load(Ordering::Relaxed).max(0) as usize;
let cs = crate::ported::zle::compcore::ZLEMETACS.load(Ordering::Relaxed).max(0) as usize;
let lppre_s = if cs != wb {
// c:3320-3326 — `zlemetaline + wb + strlen(qipre) - (qipre starts
// with a quote)`, cut at the cursor.
let qi = QIPRE.get_or_init(|| Mutex::new(String::new())).lock().unwrap().clone();
let skip = if qi.is_empty() {
0
} else {
qi.len() - usize::from(qi.starts_with('\'') || qi.starts_with('"'))
};
let mut lp = meta.get((wb + skip).min(cs)..cs).unwrap_or("").to_string();
// c:3327-3332 — `for (bp = brbeg; bp; bp = bp->next)
// strcpy(lppre + bp->qpos, lppre + bp->qpos + strlen(bp->str));`
if let Some(g) = crate::ported::zle::compcore::BRBEG
.get()
.and_then(|m| m.lock().ok())
{
let mut bp = g.as_deref();
while let Some(b) = bp {
let q = b.qpos.max(0) as usize;
let e = (q + b.str.as_ref().map_or(0, |x| x.len())).min(lp.len());
if q <= e && lp.is_char_boundary(q) && lp.is_char_boundary(e) {
lp.replace_range(q..e, "");
}
bp = b.next.as_deref();
}
}
if let Some(p) = lp.rfind('/') {
lp.truncate(p + 1); // c:3334-3335
lp
} else if sf1 == 0 {
String::new() // c:3337-3338
} else {
ppre_s.clone() // c:3340-3341
}
} else {
String::new() // c:3344-3345
};
let lpsuf_s = if cs != we {
// c:3347-3354 — the line text from the cursor to the end of the
// word, minus the quoted ignored suffix.
let qs = QISUF.get_or_init(|| Mutex::new(String::new())).lock().unwrap().clone();
let mut end = we as i64;
if !qs.is_empty() {
end -= qs.len() as i64 - i64::from(qs.ends_with('\'') || qs.ends_with('"'));
}
let mut ls = meta.get(cs..end.max(0) as usize).unwrap_or("").to_string();
// c:3355-3362 — `for (bp = brend; bp; bp = bp->next) { p2 = lpsuf +
// (we - zlemetacs) - bp->qpos; p1 = p2 - strlen(bp->str);
// memmove(p1, p2, strlen(p2) + 1); }`
if let Some(g) = crate::ported::zle::compcore::BREND
.get()
.and_then(|m| m.lock().ok())
{
let mut bp = g.as_deref();
while let Some(b) = bp {
let p2 = (we as i64 - cs as i64 - b.qpos as i64).max(0) as usize;
let p1 = p2.saturating_sub(b.str.as_ref().map_or(0, |x| x.len()));
if p2 <= ls.len() && ls.is_char_boundary(p1) && ls.is_char_boundary(p2) {
ls.replace_range(p1..p2, "");
}
bp = b.next.as_deref();
}
}
match ls.find('/') {
Some(i) => ls[i..].to_string(), // c:3363 strchr(lpsuf, '/')
None if sf2 != 0 => psuf_s.clone(), // c:3363 `&& sf2`
None => String::new(),
}
} else {
String::new() // c:3367-3368
};
// c:3372-3376 — the file prefix and suffix.
let s_first = s.as_bytes().first().copied();
let cs_eq_wb = cs == wb; // c:3374 `zlemetacs == wb`
let s1_is_start = s1.is_none() || s1 == Some(0);
let start_cond = s1_is_start || ic != '\0';
let mut fpre_s = if start_cond && (s_first != Some(b'/') || cs_eq_wb) {
match s1 {
Some(idx) => rpre_s[idx..].to_string(),
None => rpre_s.clone(),
}
} else {
match s1 {
Some(idx) => rpre_s[idx + 1..].to_string(),
None => {
let skip = rpre_s.chars().next().map(|c| c.len_utf8()).unwrap_or(0);
rpre_s[skip..].to_string()
}
}
};
let mut fsuf_s = match s2 {
Some(idx) => dupstrpfx(&rsuf_s, idx),
None => rsuf_s.clone(),
};
let qfpre_s = quotename(&fpre_s);
let qfsuf_s = quotename(&fsuf_s);
// c:3382-3395 — a glob pattern in the file part: compile
// `fpre` `*` `fsuf` into filecomp.
let mut filecomp_set = false;
if cpm_nonempty && (ispattern_v & 2) != 0 {
let mut p = fpre_s.clone(); // c:3388
if (fpre_s.is_empty() || !fpre_s.ends_with(Star))
&& !fsuf_s.starts_with(Star)
&& cpm.as_deref().is_some_and(|c| c.starts_with('*'))
{
p.push(Star); // c:3390
}
p.push_str(&fsuf_s); // c:3392
let prog = patcompile(&p, 0, None); // c:3393
filecomp_set = prog.is_some();
FILECOMP.with(|r| *r.borrow_mut() = prog);
}
if !filecomp_set {
fpre_s = crate::ported::lex::untokenize_ztokens(&fpre_s); // c:3397
fsuf_s = crate::ported::lex::untokenize_ztokens(&fsuf_s); // c:3398
FPL.with(|c| c.set(fpre_s.len() as i32)); // c:3400
FSL.with(|c| c.set(fsuf_s.len() as i32)); // c:3401
}
LPPL.with(|c| c.set(lppre_s.len() as i32));
LPSL.with(|c| c.set(lpsuf_s.len() as i32));
PPRE.with(|r| *r.borrow_mut() = ppre_s.clone());
PSUF.with(|r| *r.borrow_mut() = psuf_s.clone());
FPRE.with(|r| *r.borrow_mut() = fpre_s);
FSUF.with(|r| *r.borrow_mut() = fsuf_s);
QFPRE.with(|r| *r.borrow_mut() = qfpre_s);
QFSUF.with(|r| *r.borrow_mut() = qfsuf_s);
LPPRE.with(|r| *r.borrow_mut() = lppre_s);
LPSUF.with(|r| *r.borrow_mut() = lpsuf_s);
ppre = ppre_s;
psuf = psuf_s;
ADDWHAT.with(|c| c.set(-1)); // c:3403
let saved_prpre = PRPRE.with(|r| r.borrow().clone());
if ic == Tilde {
// c:3405-3411 — usernames + named directories. `ipre = "~"` so
// each bare name matches the file prefix and gets the `~` back.
let oi = crate::ported::zle::compcore::ipre
.get_or_init(|| std::sync::Mutex::new(String::new()))
.lock()
.map(|g| g.clone())
.unwrap_or_default();
if let Ok(mut g) = crate::ported::zle::compcore::ipre
.get_or_init(|| std::sync::Mutex::new(String::new()))
.lock()
{
*g = format!("~{}", oi); // c:3408 — dyncat("~", ipre)
}
maketildelist();
if let Ok(mut g) = crate::ported::zle::compcore::ipre.get().unwrap().lock() {
*g = oi; // c:3410 — restore
}
} else if ic == Equals {
// c:3412-3423 — command names (cmdnamtab, addwhat -7) + regular
// aliases (addwhat -2). `ipre = "="` so the `=` stays on the line.
let oi = crate::ported::zle::compcore::ipre
.get_or_init(|| std::sync::Mutex::new(String::new()))
.lock()
.map(|g| g.clone())
.unwrap_or_default();
if let Ok(mut g) = crate::ported::zle::compcore::ipre.get().unwrap().lock() {
*g = format!("={}", oi); // c:3417 — dyncat("=", ipre)
}
if crate::ported::zsh_h::isset(crate::ported::zsh_h::HASHLISTALL) {
let path = crate::ported::params::getaparam("path").unwrap_or_default();
crate::ported::hashtable::fillcmdnamtable(&path);
}
let cmds: Vec<(String, AddmatchNode)> = crate::ported::hashtable::cmdnamtab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, v)| (n.clone(), AddmatchNode::Hash(v.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(cmds, -7);
let aliases: Vec<(String, AddmatchNode)> = crate::ported::hashtable::aliastab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, v)| (n.clone(), AddmatchNode::Hash(v.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(aliases, -2);
if let Ok(mut g) = crate::ported::zle::compcore::ipre.get().unwrap().lock() {
*g = oi; // c:3427 — restore
}
} else if (ispattern_v & 1) != 0 {
// c:3429-3458 — normal file completion, but with pattern matching
// in the directory part: glob `lpre` `*` `lsuf` `*`.
let ng = crate::ported::zsh_h::isset(crate::ported::zsh_h::NULLGLOB);
crate::ported::options::opt_state_set(
&crate::ported::zsh_h::opt_name(crate::ported::zsh_h::NULLGLOB),
true,
); // c:3436
ADDWHAT.with(|c| c.set(0)); // c:3438
let lpre_u = LPRE.with(|r| r.borrow().clone());
let lsuf_u = LSUF.with(|r| r.borrow().clone());
let mut p = lpre_u.clone(); // c:3440
if !lsuf_u.starts_with('*') && !lpre_u.is_empty() && !lpre_u.ends_with('*') {
p.push('*'); // c:3442
}
p.push_str(&lsuf_u); // c:3444
if !lsuf_u.is_empty() && !lsuf_u.ends_with('*') && !lsuf_u.ends_with(')') {
p.push('*'); // c:3446
}
// c:3449-3452 — do the globbing.
crate::ported::glob::tokenize(&mut p);
crate::ported::glob::remnulargs(&mut p);
let mut l: crate::ported::subst::LinkList = vec![p].into_iter().collect();
crate::ported::subst::globlist(&mut l, 0);
// c:3454-3461 — and add the resulting words.
let found: Vec<String> = l.into_iter().collect();
if !found.is_empty() {
crate::ported::zle::compcore::mflags
.fetch_or(crate::ported::zle::comp_h::CMF_FILE, Ordering::Relaxed);
for n in &found {
addmatch(n, None);
}
crate::ported::zle::compcore::mflags
.fetch_and(!crate::ported::zle::comp_h::CMF_FILE, Ordering::Relaxed);
}
crate::ported::options::opt_state_set(
&crate::ported::zsh_h::opt_name(crate::ported::zsh_h::NULLGLOB),
ng,
); // c:3463
} else {
// c:3465-3633 — no pattern matching: one pass per directory.
ADDWHAT.with(|c| c.set(CC_FILES as i32)); // c:3469
// c:3471-3498 — `compctl -W`: the directories to look in, from the
// named array/word list, each as `<dir>/<ppre>`.
let mut dirs: Option<Vec<String>> = None;
if let Some(withd) = cc.withd.as_ref() {
if let Some(pp) = crate::ported::zle::compcore::get_user_var(Some(withd.as_str())) {
dirs = Some(pp.iter().map(|d| format!("{}/{}", d, ppre)).collect());
}
}
let dirs: Vec<String> = dirs.unwrap_or_else(|| {
vec![match cc.withd.as_ref() {
Some(withd) => format!("{}/{}", withd, ppre), // c:3503-3507
None => ppre.clone(), // c:3509
}]
});
for dir in dirs {
// c:3513 — `prpre = *dirs;`
PRPRE.with(|r| *r.borrow_mut() = if dir.is_empty() { None } else { Some(dir.clone()) });
if sf2 != 0 {
// c:3515-3517 — we are in the path, so add only directories.
gen_matches_files(1, 0, 0);
} else {
if (cc.mask & CC_FILES) != 0 {
// c:3520 — add all files.
gen_matches_files(0, 0, 1);
} else if (cc.mask & CC_COMMPATH) != 0 {
// c:3523 — completion of command paths.
if sf1 != 0 || cc.withd.is_some() {
// c:3526 — a path prefix: directories and executables.
gen_matches_files(1, 1, 0);
} else {
// c:3531-3542 — no path prefix: the things reachable via
// $path, i.e. the current directory if "." or "" is in it.
let path = crate::ported::params::getaparam("path").unwrap_or_default();
if path.iter().any(|p| p.is_empty() || p == ".") {
let pp = PRPRE.with(|r| r.borrow().clone());
PRPRE.with(|r| *r.borrow_mut() = Some("./".to_string())); // c:3539
gen_matches_files(1, 1, 0); // c:3540
PRPRE.with(|r| *r.borrow_mut() = pp); // c:3541
}
}
} else if (cc.mask & CC_DIRS) != 0 {
gen_matches_files(1, 0, 0); // c:3545
}
// c:3547-3629 — the compctl has a glob pattern (compctl -g).
if let Some(globstr) = cc.glob.as_ref() {
let pl = dir.len(); // c:3550 pl = strlen(prpre)
let ne = std::mem::replace(
&mut *crate::ported::utils::noerrs_lock().lock().unwrap(),
1,
); // c:3552-3562 noerrs = 1
let md = crate::ported::zsh_h::isset(crate::ported::zsh_h::MARKDIRS);
// c:3538-3543 — these are used in the globbing code to make things
// a bit faster.
let mstack_set = crate::ported::zle::compcore::mstack
.get()
.and_then(|m| m.lock().ok().map(|g| g.is_some()))
.unwrap_or(false);
if ispattern_v != 0 || mstack_set {
GLOB_PRE.with(|r| *r.borrow_mut() = None);
GLOB_SUF.with(|r| *r.borrow_mut() = None);
} else {
GLOB_PRE.with(|r| *r.borrow_mut() = Some(FPRE.with(|f| f.borrow().clone())));
GLOB_SUF.with(|r| *r.borrow_mut() = Some(FSUF.with(|f| f.borrow().clone())));
}
ADDWHAT.with(|c| c.set(-6)); // c:3564
crate::ported::options::opt_state_set(
&crate::ported::zsh_h::opt_name(crate::ported::zsh_h::MARKDIRS),
false,
); // c:3568
// c:3571-3627 — the -g string may hold several blank-
// separated patterns.
let mut rest: &str = globstr.as_str();
loop {
// c:3578-3584 — skip blanks; stop at the end.
rest = rest.trim_start_matches(|c: char| {
c.is_ascii() && crate::ported::ztype_h::inblank(c as u8)
});
if rest.is_empty() {
break;
}
// c:3585-3587 — find the blank ending the pattern.
let b = rest.as_bytes();
let mut i = rest.chars().next().map_or(0, |c| c.len_utf8());
while i < b.len() && !crate::ported::ztype_h::inblank(b[i]) {
if b[i] == b'\\' && i + 1 < b.len() {
i += 1;
}
i += 1;
}
let i = i.min(b.len());
// c:3589-3594 — get the pattern string.
let mut g = rest[..i].to_string();
rest = &rest[i..];
crate::ported::glob::tokenize(&mut g);
if g.starts_with('=')
&& crate::ported::zsh_h::isset(crate::ported::zsh_h::EQUALSOPT)
{
g.replace_range(..1, &Equals.to_string()); // c:3592
}
if g.starts_with('~') {
g.replace_range(..1, &Tilde.to_string()); // c:3594
}
crate::ported::glob::remnulargs(&mut g); // c:3595
let first = g.chars().next();
let l_word: String;
if (first == Some(Equals) || first == Some(Tilde)) && cc.withd.is_none() {
// c:3596-3601 — `~`/`=` at the beginning: expand it.
g = crate::ported::subst::filesub(&g, 0);
l_word = g.clone();
} else if first == Some('/') && cc.withd.is_none() {
// c:3602-3605 — a full path: add it unchanged.
l_word = g.clone();
} else {
// c:3606-3615 — a simple pattern: append it to the
// path we have on the command line.
l_word = format!("{}{}", dir, g);
}
// c:3617-3621 — do the globbing.
let ng = crate::ported::zsh_h::isset(crate::ported::zsh_h::NULLGLOB);
crate::ported::options::opt_state_set(
&crate::ported::zsh_h::opt_name(crate::ported::zsh_h::NULLGLOB),
true,
);
let mut l: crate::ported::subst::LinkList =
vec![l_word].into_iter().collect();
crate::ported::subst::globlist(&mut l, 0);
crate::ported::options::opt_state_set(
&crate::ported::zsh_h::opt_name(crate::ported::zsh_h::NULLGLOB),
ng,
);
// c:3623-3659 — get the results.
let results: Vec<String> = l.into_iter().collect();
if let Some(first_res) = results.first() {
if !first_res.chars().any(crate::token_char::itok_char) {
let gf = g.chars().next();
if gf == Some(Equals)
|| gf == Some(Tilde)
|| gf == Some('/')
|| cc.withd.is_some()
{
// c:3632-3641 — started with `~`, `=` or `/`
// (or an initial directory was given): add
// the result only if it matches the line.
for p2 in &results {
if crate::ported::utils::strpfx(&dir, p2) {
addmatch(&p2[pl..], None);
}
}
} else {
// c:3642-3654 — otherwise ignore the path we
// prepended to the pattern.
for p3 in &results {
let mut ns = sf1;
let mut start = 0;
for (ix, ch) in p3.char_indices() {
if ns == 0 {
break;
}
start = ix + ch.len_utf8();
if ch == '/' {
ns -= 1;
}
}
addmatch(&p3[start..], None);
}
}
}
}
}
GLOB_PRE.with(|r| *r.borrow_mut() = None); // c:3665
GLOB_SUF.with(|r| *r.borrow_mut() = None);
*crate::ported::utils::noerrs_lock().lock().unwrap() = ne; // c:3667
crate::ported::options::opt_state_set(
&crate::ported::zsh_h::opt_name(crate::ported::zsh_h::MARKDIRS),
md,
); // c:3668
}
}
}
}
// c:3637-3639 — `lppre = lpsuf = NULL; lppl = lpsl = 0;` and (c:3630)
// `prpre = NULL`.
PRPRE.with(|r| *r.borrow_mut() = saved_prpre);
LPPRE.with(|r| r.borrow_mut().clear());
LPSUF.with(|r| r.borrow_mut().clear());
LPPL.with(|c| c.set(0));
LPSL.with(|c| c.set(0));
}
// c:3641-3646 — change the `~`/`=` tokens to the real characters so that
// things starting with them get added.
if ic != '\0' {
let pfx = if ic == Tilde { "~" } else { "=" };
RPRE.with(|r| {
let mut b = r.borrow_mut();
*b = format!("{}{}", pfx, *b);
});
RPL.with(|c| c.set(c.get() + 1));
QRPRE.with(|r| {
let mut b = r.borrow_mut();
*b = format!("{}{}", pfx, *b);
});
}
// ===================== end preamble =====================
let s: &str = &s;
// CC_NAMED — c:3664 `dumphashtable(nameddirtab, addwhat)`. maketildelist
// now emits bare names (the `~` comes from ipre), so set ipre = "~" here
// too, matching the tilde arm above.
if (cc.mask & CC_NAMED) != 0 {
ADDWHAT.with(|c| c.set(-1));
let oi = crate::ported::zle::compcore::ipre
.get_or_init(|| std::sync::Mutex::new(String::new()))
.lock()
.map(|g| g.clone())
.unwrap_or_default();
if let Ok(mut g) = crate::ported::zle::compcore::ipre.get().unwrap().lock() {
*g = format!("~{}", oi);
}
maketildelist();
if let Ok(mut g) = crate::ported::zle::compcore::ipre.get().unwrap().lock() {
*g = oi;
}
}
// c:3648-3661 — command completion: add alias names, reserved words,
// shell functions, builtins and external command names (cmdnamtab).
// Gated on no leading `~`/`=` (ic), no path prefix and no path suffix.
// All dumped with addwhat=-3 so the matches carry no file-type marker.
if ic == '\0' && (cc.mask & CC_COMMPATH) != 0 && ppre.is_empty() && psuf.is_empty() {
// c:3651 — regular + global aliases.
let aliases: Vec<(String, AddmatchNode)> = crate::ported::hashtable::aliastab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, v)| (n.clone(), AddmatchNode::Hash(v.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(aliases, -3);
// c:3652 — reserved words.
let reswds: Vec<(String, AddmatchNode)> = crate::ported::hashtable::reswdtab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, v)| (n.clone(), AddmatchNode::Hash(v.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(reswds, -3);
// c:3653 — shell functions.
let funcs: Vec<(String, AddmatchNode)> = crate::ported::hashtable::shfunctab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, v)| (n.clone(), AddmatchNode::Hash(v.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(funcs, -3);
// c:3654 — builtins. C walks `builtintab`, which holds only the
// builtins of currently-LOADED modules; zshrs keeps one flat
// static `BUILTINS` slice, so the membership test has to be
// made explicitly. `builtin_in_builtintab` is the same
// predicate `scanbuiltins` uses for the `builtins` magic assoc
// (Src/Modules/parameter.c:823), so the two namespaces cannot
// drift. Without it this dump offered 44 module builtins
// (`strftime`, `zstat`, `zpty`, `zf_chmod`, `pcre_match`, …)
// that `${(k)builtins}` did not list and that real zsh only
// exposes after the owning `zmodload`.
// The DISABLED bit of a builtin lives in `BUILTINS_DISABLED` (the
// `builtintab` static is immutable), so fold it into the node flags.
let bi_dis = crate::ported::builtin::BUILTINS_DISABLED
.lock()
.unwrap()
.clone();
let mut builtins: Vec<(String, AddmatchNode)> = crate::ported::builtin::BUILTINS
.iter()
.filter(|b| crate::ext_builtins::builtin_in_builtintab(&b.node.nam))
.map(|b| {
let mut hn = b.node.clone();
if bi_dis.contains(&hn.nam) {
hn.flags |= DISABLED;
}
(hn.nam.clone(), AddmatchNode::Hash(hn))
})
.collect();
// zshrs extension builtins dispatch in-process but have no
// entry in the C-port BUILTINS table, so command-position
// completion would never offer `doctor`, `peach`, `zassert_eq`,
// etc. `hide_ext_builtins()` (shared with the `builtins` magic
// assoc in the Src/Modules/parameter.c port, so the two
// namespaces can't drift) drops them under `--zsh` strict
// emulation and under `ZSHRS_HIDE_EXT_BUILTINS` — the parity
// harnesses' measurement knob, which changes NOTHING else:
// the names still dispatch and still resolve via `whence`.
if !crate::ext_builtins::hide_ext_builtins() {
builtins.extend(crate::ext_builtins::EXT_BUILTIN_NAMES.iter().map(|s| {
let hn = crate::ported::zsh_h::hashnode {
next: None,
nam: (*s).to_string(),
flags: 0,
};
((*s).to_string(), AddmatchNode::Hash(hn))
}));
}
// Commands a cdylib registered via `znative load` / `zmodload -R`
// live only in the plugin host registry; offer them in command
// position like the extension builtins above.
builtins.extend(crate::plugin_host::command_names().into_iter().map(|n| {
let hn = crate::ported::zsh_h::hashnode {
next: None,
nam: n.clone(),
flags: 0,
};
(n, AddmatchNode::Hash(hn))
}));
dumphashtable(builtins, -3);
// c:3655-3657 — external commands; HASHLISTALL (default on) bulk-
// hashes $path first so every reachable command is offered.
if crate::ported::zsh_h::isset(crate::ported::zsh_h::HASHLISTALL) {
let path = crate::ported::params::getaparam("path").unwrap_or_default();
crate::ported::hashtable::fillcmdnamtable(&path);
}
let cmds: Vec<(String, AddmatchNode)> = crate::ported::hashtable::cmdnamtab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, v)| (n.clone(), AddmatchNode::Hash(v.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(cmds, -3);
// c:3662-3664 — and parameter names if autocd and cdablevars are set:
// scalar top-level parameters whose value starts with `/` (addmatch -4).
if crate::ported::zsh_h::isset(crate::ported::zsh_h::AUTOCD)
&& crate::ported::zsh_h::isset(crate::ported::zsh_h::CDABLEVARS)
{
let names: Vec<(String, AddmatchNode)> = {
let tab = crate::ported::params::paramtab().read().unwrap();
tab.iter()
.map(|(n, pm)| (n.clone(), AddmatchNode::Param((**pm).clone())))
.collect()
};
dumphashtable(names, -4);
}
}
// c:3668 — oaw = addwhat = (cc->mask & CC_QUOTEFLAG) ? -2 : CC_QUOTEFLAG.
// The "default" addwhat used by the name-table arms below; arms
// that override it (CC_VARS, CC_BINDINGS) reset back to `oaw`.
let oaw: i32 = if (cc.mask & CC_QUOTEFLAG) != 0 {
-2
} else {
CC_QUOTEFLAG as i32
};
ADDWHAT.with(|c| c.set(oaw));
// c:3673 — CC_OPTIONS: add setopt option names.
if (cc.mask & CC_OPTIONS) != 0 {
let names: Vec<(String, AddmatchNode)> = crate::ported::options::OPTIONTAB
.iter()
.map(|o| {
let hn = crate::ported::zsh_h::hashnode {
next: None,
nam: o.to_string(),
flags: 0,
};
(o.to_string(), AddmatchNode::Hash(hn))
})
.collect();
dumphashtable(names, oaw);
}
// c:3676 — CC_VARS: parameter names (addwhat -9). C's addmatch
// filters unset params and non-top-level (`pm->level`) ones; we
// apply the same predicate while building the name list.
if (cc.mask & CC_VARS) != 0 {
let names: Vec<(String, AddmatchNode)> = {
let tab = crate::ported::params::paramtab().read().unwrap();
tab.iter()
.map(|(n, pm)| (n.clone(), AddmatchNode::Param((**pm).clone())))
.collect()
};
dumphashtable(names, -9);
ADDWHAT.with(|c| c.set(oaw)); // c:3679
}
// c:3681 — CC_BINDINGS: zle widget (thingy) names.
if (cc.mask & CC_BINDINGS) != 0 {
let names: Vec<(String, AddmatchNode)> = crate::ported::zle::zle_thingy::thingytab()
.lock()
.map(|t| {
t.iter()
.map(|(n, th)| {
let hn = crate::ported::zsh_h::hashnode {
next: None,
nam: n.clone(),
flags: th.flags,
};
(n.clone(), AddmatchNode::Hash(hn))
})
.collect()
})
.unwrap_or_default();
dumphashtable(names, CC_BINDINGS as i32);
ADDWHAT.with(|c| c.set(oaw)); // c:3684
}
// c:3686 — cc.keyvar (compctl -k): names from a parameter / word list.
if let Some(kv) = cc.keyvar.as_ref() {
if let Some(usr) = crate::ported::zle::compcore::get_user_var(Some(kv.as_str())) {
ADDWHAT.with(|c| c.set(oaw));
for u in usr {
addmatch(&u, None);
}
}
}
// c:3695 — CC_USERS: user names (maketildelist fills the table).
if (cc.mask & CC_USERS) != 0 {
maketildelist();
ADDWHAT.with(|c| c.set(oaw)); // c:3698
}
// c:3700-3736 — cc.func (compctl -K user-fn) → call shfunc for
// matches. Build args = [func-name, lpre, lsuf] and dispatch via
// doshfunc; the user shfunc populates `$reply` array which we
// then walk via addmatch.
if let Some(func_name) = cc.func.as_ref() {
if let Some(mut shfunc) = crate::ported::utils::getshfunc(func_name) {
// c:3702-3717 — `addlinknode(args, cc->func);` then, with the
// word deleted, the ORIGINAL line split at `ooffs` (untokenized),
// otherwise `lpre` and `lsuf`.
let lv = crate::ported::builtin::LASTVAL.load(std::sync::atomic::Ordering::Relaxed); // c:3701
let (arg_pre, arg_suf) = if delit {
let mut at = ooffs.max(0) as usize;
at = at.min(os.len());
while at > 0 && !os.is_char_boundary(at) {
at -= 1;
}
(
crate::ported::lex::untokenize_ztokens(&os[..at]),
crate::ported::lex::untokenize_ztokens(&os[at..]),
)
} else {
(
LPRE.with(|r| r.borrow().clone()),
LSUF.with(|r| r.borrow().clone()),
)
};
let largs: Vec<String> = vec![func_name.clone(), arg_pre.clone(), arg_suf.clone()];
// c:3722-3724 — `if (incompfunc != 1) incompctlfunc = 1;
// sfcontext = SFC_COMPLETE;`.
let in_compfunc = INCOMPFUNC.load(std::sync::atomic::Ordering::Relaxed);
if in_compfunc != 1 {
INCOMPCTLFUNC.with(|c| c.set(true));
}
let osc = crate::ported::builtin::SFCONTEXT.swap(
crate::ported::zsh_h::SFC_COMPLETE,
std::sync::atomic::Ordering::Relaxed,
);
// c:3725 — `doshfunc(shfunc, args, 1);`.
let name_for_body = func_name.clone();
let body_args: Vec<String> = vec![arg_pre, arg_suf];
let body_runner = move || -> i32 {
crate::ported::exec::run_function_body(&name_for_body, &body_args).unwrap_or(0)
};
let _ = crate::ported::exec::doshfunc(&mut shfunc, largs, true, body_runner);
// c:3726-3727 — `sfcontext = osc; incompctlfunc = 0;`.
crate::ported::builtin::SFCONTEXT.store(osc, std::sync::atomic::Ordering::Relaxed);
INCOMPCTLFUNC.with(|c| c.set(false));
// c:3729-3731 — `if ((r = get_user_var("reply"))) while
// (*r) addmatch(*r++, NULL);`.
if let Some(reply) = crate::ported::params::getaparam("reply") {
// c:3735-3737 — addwhat is still `oaw` here, so the
// reply words are accepted by addmatch (setting it to 0
// would make addmatch drop every match).
ADDWHAT.with(|c| c.set(oaw));
for m in reply {
addmatch(&m, None);
}
}
crate::ported::builtin::LASTVAL.store(lv, std::sync::atomic::Ordering::Relaxed); // c:3737 lastval = lv
}
}
// c:3745-3762 — `compctl -j/-r/-z`: job names.
if (cc.mask & (CC_JOBS | CC_RUNNING | CC_STOPPED)) != 0 {
let texts: Vec<(String, bool)> = crate::ported::jobs::JOBTAB
.get()
.and_then(|m| m.lock().ok())
.map(|tab| {
tab.iter()
.filter(|j| {
(j.stat & crate::ported::zsh_h::STAT_INUSE) != 0
&& j.procs.first().is_some_and(|p| !p.text.is_empty())
})
.map(|j| {
(
j.procs[0].text.clone(),
(j.stat & crate::ported::zsh_h::STAT_STOPPED) != 0,
)
})
.collect()
})
.unwrap_or_default();
for (text, stopped) in texts {
if (cc.mask & CC_JOBS) != 0
|| (stopped && (cc.mask & CC_STOPPED) != 0)
|| (!stopped && (cc.mask & CC_RUNNING) != 0)
{
addmatch(&text, None); // c:3758
}
}
}
// c:3870-3906 — cc.ylist (compctl -y) → explanation source.
// If ylist starts with `$` or `(`, read from a parameter; else
// treat as a shfunc that receives the current match list and
// populates `$reply` with per-match explanation strings.
if let Some(ylist) = cc.ylist.as_ref() {
let is_var_ref = ylist.starts_with('$') || ylist.starts_with('(');
if !is_var_ref {
if let Some(mut shfunc) = crate::ported::utils::getshfunc(ylist) {
// c:3886-3895 — `addlinknode(args, cc->ylist);` then
// for each match append the prefix+str+suffix string.
// Match list isn't threaded here yet so we pass just
// the ylist function name; full match-list plumbing
// is a follow-up tied to the Cmatch port.
let largs: Vec<String> = vec![ylist.clone()];
// c:3898-3900 — same SFC_COMPLETE / incompctlfunc=1
// bracketing as cc.func above.
let in_compfunc = INCOMPFUNC.load(std::sync::atomic::Ordering::Relaxed);
if in_compfunc != 1 {
INCOMPCTLFUNC.with(|c| c.set(true));
}
let osc = crate::ported::builtin::SFCONTEXT.swap(
crate::ported::zsh_h::SFC_COMPLETE,
std::sync::atomic::Ordering::Relaxed,
);
// c:3901 — `doshfunc(shfunc, args, 1);`.
let name_for_body = ylist.clone();
let body_runner = move || -> i32 {
crate::ported::exec::run_function_body(&name_for_body, &[]).unwrap_or(0)
};
let _ = crate::ported::exec::doshfunc(&mut shfunc, largs, true, body_runner);
crate::ported::builtin::SFCONTEXT.store(osc, std::sync::atomic::Ordering::Relaxed);
INCOMPCTLFUNC.with(|c| c.set(false));
}
}
}
// c:3758-3801 — cc.str (compctl -s): lex the string into words, then
// fully expand (prefork + globlist) and add every resulting word.
if let Some(cstr) = &cc.str {
use crate::ported::hist::{strinbeg, strinend};
use crate::ported::lex::{
ctxtlex, noaliases, set_noaliases, tok, tokstr, LEX_INPUT, LEX_LEXFLAGS, LEX_LEXSTOP,
LEX_POS, LEX_UNGET_BUF, LEX_UNGET_HPTR, LEX_UNGET_RAW,
};
use crate::ported::zsh_h::{ENDINPUT, LEXERR, LEXFLAGS_ZLE};
let mut foo: Vec<String> = Vec::new(); // c:3760 LinkList foo
let mut first = true; // c:3762
let ng = crate::ported::zsh_h::isset(crate::ported::zsh_h::NULLGLOB); // c:3762
let oowe = crate::ported::zle::compcore::WE.load(Ordering::Relaxed); // c:3762
let oowb = crate::ported::zle::compcore::WB.load(Ordering::Relaxed); // c:3762
let ona = noaliases(); // c:3763
crate::ported::options::opt_state_set(
&crate::ported::zsh_h::opt_name(crate::ported::zsh_h::NULLGLOB),
true,
); // c:3767
// c:3771 — put the string in the lexer buffer and call the lexer to
// get the words we have to expand.
crate::ported::context::zcontext_save();
// The lexer reads its own `LEX_INPUT` window and `LEX_UNGET_*`
// queues ahead of the input stack; park them so the nested lex
// reads only the pushed frame (same isolation sep_comp_string's
// caller applies in zle_tricky.rs).
let saved_lex_input = LEX_INPUT.with_borrow_mut(std::mem::take);
let saved_lex_pos = LEX_POS.replace(0);
let saved_unget = LEX_UNGET_BUF.with_borrow_mut(std::mem::take);
let saved_unget_hptr = LEX_UNGET_HPTR.with_borrow_mut(std::mem::take);
let saved_unget_raw = LEX_UNGET_RAW.with_borrow_mut(std::mem::take);
let saved_unget_srccap =
crate::funcdef_capture::LEX_UNGET_SRCCAP.with_borrow_mut(std::mem::take);
LEX_LEXSTOP.set(false);
LEX_LEXFLAGS.set(LEXFLAGS_ZLE); // c:3772
let tmpbuf = format!("foo {}", cstr); // c:3773-3774 KLUDGE!
crate::ported::input::inpush(&tmpbuf, 0, None); // c:3775
strinbeg(0); // c:3776
set_noaliases(true); // c:3777
loop {
ctxtlex(); // c:3779
let t = tok();
if t == ENDINPUT || t == LEXERR {
break; // c:3781
}
if !first {
if let Some(ts) = tokstr() {
if !ts.is_empty() {
foo.push(ts); // c:3783
}
}
}
first = false; // c:3784
}
set_noaliases(ona); // c:3786
strinend(); // c:3787
crate::ported::input::inpop(); // c:3788
LEX_INPUT.with_borrow_mut(|b| *b = saved_lex_input);
LEX_POS.set(saved_lex_pos);
LEX_UNGET_BUF.with_borrow_mut(|b| *b = saved_unget);
LEX_UNGET_HPTR.with_borrow_mut(|b| *b = saved_unget_hptr);
LEX_UNGET_RAW.with_borrow_mut(|b| *b = saved_unget_raw);
crate::funcdef_capture::LEX_UNGET_SRCCAP.with_borrow_mut(|b| *b = saved_unget_srccap);
errflag.fetch_and(
!crate::ported::utils::ERRFLAG_ERROR,
Ordering::Relaxed,
); // c:3789
crate::ported::context::zcontext_restore(); // c:3790
// c:3792 — fine, now do full expansion.
let mut l: crate::ported::subst::LinkList = foo.into_iter().collect();
let mut ret_flags: i32 = 0;
crate::ported::subst::prefork(&mut l, 0, &mut ret_flags);
if errflag.load(Ordering::Relaxed) == 0 {
// c:3793
crate::ported::subst::globlist(&mut l, 0); // c:3794
if errflag.load(Ordering::Relaxed) == 0 {
// c:3795-3798 — and add the resulting words as matches.
let words: Vec<String> = l.into_iter().collect();
for w in &words {
addmatch(w, None);
}
}
}
crate::ported::options::opt_state_set(
&crate::ported::zsh_h::opt_name(crate::ported::zsh_h::NULLGLOB),
ng,
); // c:3799
crate::ported::zle::compcore::WE.store(oowe, Ordering::Relaxed); // c:3800
crate::ported::zle::compcore::WB.store(oowb, Ordering::Relaxed); // c:3801
}
// c:3803-3841 — cc.hpat (compctl -H): pull words out of the
// history that match the pattern.
if let Some(hpat) = cc.hpat.as_ref() {
use crate::ported::zsh_h::{GETHIST_UPWARD, HIST_FOREIGN};
// c:3810-3816 — parse the pattern unless it is the null string.
let pprog = if !hpat.is_empty() {
let mut thpat = hpat.clone();
crate::ported::glob::tokenize(&mut thpat);
patcompile(&thpat, PAT_HEAPDUP as i32, None)
} else {
None
};
// c:3806-3819 — n = number of history lines to search (0 → all).
let mut n = cc.hnum;
if n == 0 {
n = -1;
}
let cur = crate::ported::hist::curhist.load(std::sync::atomic::Ordering::Relaxed);
let start = crate::ported::hist::addhistnum(cur, -1, HIST_FOREIGN as i32); // c:3807
let mut he_ev = crate::ported::hist::gethistent(start, GETHIST_UPWARD); // c:3808
ADDWHAT.with(|c| c.set(oaw));
// c:3822 — `while (n-- && he)`.
while n != 0 {
let ev = match he_ev {
Some(e) => e,
None => break,
};
n -= 1;
if let Some(he) = crate::ported::hist::gethist(ev) {
let nam = &he.node.nam;
// c:3824 — iterate the words of this line back-to-front.
for iw in (0..he.nwords).rev() {
let bi = (iw * 2) as usize;
if bi + 1 >= he.words.len() {
continue;
}
let hstart = he.words[bi] as usize; // c:3825
let hend = he.words[bi + 1] as usize; // c:3826
if hstart > hend
|| hend > nam.len()
|| !nam.is_char_boundary(hstart)
|| !nam.is_char_boundary(hend)
{
continue;
}
let word = &nam[hstart..hend];
// c:3831-3833 — skip words starting with a quote or `$`.
let c0 = word.chars().next();
if !matches!(c0, Some('\'') | Some('"') | Some('`') | Some('$'))
&& pprog.as_ref().map_or(true, |pp| pattry(pp, word))
{
addmatch(word, None); // c:3834
}
}
}
he_ev = crate::ported::hist::up_histent(ev); // c:3838
}
}
// c:3842-3845 — parameter flavours (arrays/ints/exports/etc.). The
// per-node predicate is evaluated by addmatch on each paramtab node.
{
let t = cc.mask
& (CC_ARRAYS
| CC_INTVARS
| CC_ENVVARS
| CC_SCALARS
| CC_READONLYS
| CC_SPECIALS
| CC_PARAMS);
if t != 0 {
let names: Vec<(String, AddmatchNode)> = {
let tab = crate::ported::params::paramtab().read().unwrap();
tab.iter()
.map(|(n, pm)| (n.clone(), AddmatchNode::Param((**pm).clone())))
.collect()
};
dumphashtable(names, t as i32); // c:3845
}
}
// c:3846-3860 — every command-table arm passes
// `t | (cc->mask & (CC_DISCMDS|CC_EXCMDS))`; addmatch applies the
// enable/disable predicate (c:2012-2013) per node.
let dis_ex = (cc.mask & (CC_DISCMDS | CC_EXCMDS)) as i32;
// c:3846-3848 — CC_SHFUNCS: shell function names.
if (cc.mask & CC_SHFUNCS) != 0 {
let names: Vec<(String, AddmatchNode)> = crate::ported::hashtable::shfunctab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, f)| (n.clone(), AddmatchNode::Hash(f.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(names, (cc.mask & CC_SHFUNCS) as i32 | dis_ex);
}
// c:3849-3851 — CC_BUILTINS: builtin command names.
if (cc.mask & CC_BUILTINS) != 0 {
let bi_dis = crate::ported::builtin::BUILTINS_DISABLED
.lock()
.unwrap()
.clone();
let names: Vec<(String, AddmatchNode)> = crate::ported::builtin::BUILTINS
.iter()
.filter(|b| crate::ext_builtins::builtin_in_builtintab(&b.node.nam))
.map(|b| {
let mut hn = b.node.clone();
if bi_dis.contains(&hn.nam) {
hn.flags |= DISABLED;
}
(hn.nam.clone(), AddmatchNode::Hash(hn))
})
.collect();
dumphashtable(names, (cc.mask & CC_BUILTINS) as i32 | dis_ex);
}
// c:3852-3857 — CC_EXTCMDS: external command names (cmdnamtab).
if (cc.mask & CC_EXTCMDS) != 0 {
let names: Vec<(String, AddmatchNode)> = crate::ported::hashtable::cmdnamtab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, c)| (n.clone(), AddmatchNode::Hash(c.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(names, (cc.mask & CC_EXTCMDS) as i32 | dis_ex);
}
// c:3858-3860 — CC_RESWDS: reserved words.
if (cc.mask & CC_RESWDS) != 0 {
let names: Vec<(String, AddmatchNode)> = crate::ported::hashtable::reswdtab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, r)| (n.clone(), AddmatchNode::Hash(r.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(names, (cc.mask & CC_RESWDS) as i32 | dis_ex);
}
// c:3861-3863 — CC_ALREG / CC_ALGLOB: regular / global aliases.
if (cc.mask & (CC_ALREG | CC_ALGLOB)) != 0 {
let names: Vec<(String, AddmatchNode)> = crate::ported::hashtable::aliastab_lock()
.read()
.map(|tab| {
tab.iter()
.map(|(n, a)| (n.clone(), AddmatchNode::Hash(a.node.clone())))
.collect()
})
.unwrap_or_default();
dumphashtable(names, (cc.mask & (CC_ALREG | CC_ALGLOB)) as i32 | dis_ex);
}
// c:3930-3992 — `compctl -l`: handle sub-completion.
if let Some(subcmd) = cc.subcmd.as_ref() {
let ow = CLWORDS.lock().unwrap().clone(); // c:3932 ow = clwords
let ocmd = CMDSTR.with(|r| r.borrow().clone()); // c:3932 os = cmdstr
let oldn = *CLWNUM.lock().unwrap(); // c:3933
let oldp = *CLWPOS.lock().unwrap(); // c:3933
let occ = CCONT.with(|c| c.get()); // c:3934
CCONT.with(|c| c.set(CC_CCCONT)); // c:3936
// c:3938-3947 — restrict the words-array.
let mut brange = *BRANGE.lock().unwrap();
let mut erange = *ERANGE.lock().unwrap();
if brange >= oldn {
brange = oldn - 1;
}
if brange < 1 {
brange = 1;
}
if erange >= oldn {
erange = oldn - 1;
}
if erange < 1 {
erange = 1;
}
*BRANGE.lock().unwrap() = brange;
*ERANGE.lock().unwrap() = erange;
let mut clwnum = erange - brange + 1; // c:3948
let mut clwpos = oldp - brange; // c:3949
let br = brange as usize; // c:3950
let mut incmd_sub = incmd;
let new_words: Vec<String>;
let new_cmd: Option<String>;
if !subcmd.is_empty() {
// c:3953-3963 — put the command name given to the flag into
// the array (in place of clwords[br - 1]) and start there.
clwpos += 1;
clwnum += 1;
incmd_sub = false;
let mut w: Vec<String> = ow.get(br - 1..).unwrap_or(&[]).to_vec();
if let Some(first) = w.first_mut() {
*first = subcmd.clone(); // c:3960
} else {
w.push(subcmd.clone());
}
new_words = w;
new_cmd = Some(subcmd.clone()); // c:3961
} else {
// c:3964-3968
new_words = ow.get(br..).unwrap_or(&[]).to_vec();
new_cmd = ow.get(br).cloned();
incmd_sub = clwpos == 0;
}
*CLWORDS.lock().unwrap() = new_words;
CMDSTR.with(|r| *r.borrow_mut() = new_cmd);
*CLWNUM.lock().unwrap() = clwnum;
*CLWPOS.lock().unwrap() = clwpos;
// c:3970 — produce the matches.
makecomplistcmd(s, incmd_sub, CFN_FIRST);
// c:3972-3984 — and restore the things we changed.
*CLWORDS.lock().unwrap() = ow;
CMDSTR.with(|r| *r.borrow_mut() = ocmd);
*CLWNUM.lock().unwrap() = oldn;
*CLWPOS.lock().unwrap() = oldp;
CCONT.with(|c| c.set(occ));
}
// c:3986-3987 — `if (cc->substr) sep_comp_string(cc->substr, s, offs);`
if let Some(substr) = cc.substr.as_ref() {
sep_comp_string(substr, s, offs);
}
// c:3987-3992 — `uremnode(ccstack, ...)` and `mstack = mstack->next` are
// done by `ccstack_pop` on drop.
if mn == crate::ported::zle::compcore::mnum.load(Ordering::Relaxed) {
crate::ported::zle::compcore::haspattern.store(ohp, Ordering::Relaxed); // c:3994
}
}
/// Setup hook — port of `setup_(UNUSED(Module m))` from Src/Zle/compctl.c:4014.
///
/// Wires `compctlreadptr` to compctlread, creates the compctltab,
/// initializes the special targets:
/// cc_compos.mask = CC_COMMPATH
/// cc_default.refc = 10000 (sentinel "never free")
/// cc_default.mask = CC_FILES
/// cc_first.refc = 10000
/// cc_first.mask2 = CC_CCCONT
/// Clears lastccused.
pub(crate) fn setup_() -> i32 {
*COMPCTLREAD_INSTALLED.lock().unwrap() = true;
createcompctltable();
*CC_COMPOS.lock().unwrap() = Some(Arc::new(Compctl {
mask: CC_COMMPATH, // c:4018
..Default::default()
}));
*CC_DEFAULT.lock().unwrap() = Some(Arc::new(Compctl {
refc: 10000, // c:4020
mask: CC_FILES, // c:4021
..Default::default()
}));
*CC_FIRST.lock().unwrap() = Some(Arc::new(Compctl {
refc: 10000, // c:4023
mask2: CC_CCCONT, // c:4025
..Default::default()
}));
*LASTCCUSED.lock().unwrap() = Vec::new(); // c:4034
0
}
// =================================================================
// zle_tricky.c state required by sep_comp_string and the
// completion-driver hooks. Ports of the file-statics in
// Src/Zle/zle_tricky.c that compctl reads/writes during the
// completion flow. Each is a `Mutex<...>` singleton matching the
// C global's name + type (translated to Rust idioms).
// =================================================================
// `we` / `wb` — word end / begin positions (1-based byte offsets
// into zlemetaline). Port of `int wb, we;` at Src/Zle/zle_tricky.c.
// WB/WE deduped to the canonical `compcore::{WB, WE}` (lex.c:120); the
// former private thread_local shadows were removed so the `cs == wb`
// comparison and the sep_comp_string save/restore see the same globals
// the lexer writes.
// `zlemetacs` — cursor position (byte offset). Deduped: reads/writes the
// canonical `compcore::ZLEMETACS` (lex.c:104) instead of a private
// thread-local shadow. Same C variable, same meaning.
/// Features hook — port of `features_(UNUSED(Module m), UNUSED(char ***features))` from Src/Zle/compctl.c:4034.
///
/// Returns the list of feature strings the module exposes. zsh C
/// uses `featuresarray(m, &module_features)` which reads
/// `module_features.bn_size` (line 4005 — 2 builtins: compctl,
/// compcall). Rust returns the explicit list.
pub(crate) fn features_() -> Vec<String> {
vec!["b:compctl".to_string(), "b:compcall".to_string()]
}
/// Enables hook — port of `enables_(UNUSED(Module m), UNUSED(int **enables))` from Src/Zle/compctl.c:4042.
///
/// C delegates to `handlefeatures(m, &module_features, enables)`
/// which writes the per-feature enable bits to `*enables`. Rust
/// returns a per-feature bool vector — entries currently default
/// to enabled (1). Wiring to the module-load runtime is a separate
/// concern.
pub(crate) fn enables_() -> Vec<i32> {
vec![1, 1]
}
/// Boot hook — port of `boot_(UNUSED(Module m))` from Src/Zle/compctl.c:4049.
///
/// Registers the two completion-driver hooks via
/// `addhookfunc("compctl_make", ccmakehookfn)` and
/// `addhookfunc("compctl_cleanup", cccleanuphookfn)`. Rust hooks
/// dispatch via the same names; the actual hook registry is in
/// src/ported/module.rs.
pub(crate) fn boot_() -> i32 {
// c:4051-4052
crate::ported::module::addhookfunc("compctl_make", ccmakehookfn);
crate::ported::module::addhookfunc("compctl_cleanup", cccleanuphookfn);
0
}
/// `instring` — quoting context. Port of `int instring;`. The QT_*
/// values are the C enum at `Src/zsh.h:253-292` (ported in zsh_h.rs).
/// Cleanup hook — port of `cleanup_(UNUSED(Module m))` from Src/Zle/compctl.c:4058.
///
/// Reverses boot_: removes the two hooks, then disables features
/// via `setfeatureenables(m, &module_features, NULL)`.
pub(crate) fn cleanup_() -> i32 {
// c:4060-4062
crate::ported::module::deletehookfunc("compctl_make", ccmakehookfn);
crate::ported::module::deletehookfunc("compctl_cleanup", cccleanuphookfn);
0
}
/// Finish hook — port of `finish_(UNUSED(Module m))` from Src/Zle/compctl.c:4067.
///
/// Tears down the compctltab hash table, frees lastccused, restores
/// `compctlreadptr` to the fallback. Rust drops the table on Mutex
/// reset; lastccused frees via Vec::clear; compctlreadptr is the
/// COMPCTLREAD_INSTALLED bool.
pub(crate) fn finish_() -> i32 {
*COMPCTL_TAB.write().unwrap() = None; // c:4067 deletehashtable
LASTCCUSED.lock().unwrap().clear(); // c:4071-4072 freelinklist
*COMPCTLREAD_INSTALLED.lock().unwrap() = false; // c:4074
0
}
// =================================================================
// Type definitions — port of Src/Zle/compctl.h:32-115
// =================================================================
// Compcond/CompcondData/Compctl/Patcomp/Compctlp ported in
// compctl_h.rs (Src/Zle/compctl.h:39-115). Imported above.
// =================================================================
// Globals — port of Src/Zle/compctl.c:36-66
// =================================================================
/// Global cmatcher list. Port of file-static `Cmlist cmatcher;` at
/// Src/Zle/compctl.c:36. Bucket-2 user-registered registry per
/// PORT_PLAN.md — `compctl -M` writes via `freecmlist + cpcmlist`,
/// every completion call reads. `RwLock` lets parallel completion
/// reads proceed without serialising on a mutex.
pub(crate) static CMATCHER: std::sync::RwLock<Option<std::sync::Arc<Cmlist>>> =
std::sync::RwLock::new(None); // c:36
/// `compctltab` hash table — name → Compctl.
/// Port of `HashTable compctltab;` at Src/Zle/compctl.c:46.
/// Bucket-2 user-registered registry: `compctl name args` writes,
/// every completion call reads. `RwLock` per PORT_PLAN.md.
static COMPCTL_TAB: std::sync::RwLock<Option<HashMap<String, Arc<Compctl>>>> =
std::sync::RwLock::new(None);
/// Pattern-compctl list. Port of `Patcomp patcomps;` at
/// Src/Zle/compctl.c:51. Bucket-2 user-registered registry:
/// `compctl -p` writes, every pattern-completion call reads.
/// `RwLock` per PORT_PLAN.md.
static PATCOMPS: std::sync::RwLock<Vec<(String, Arc<Compctl>)>> =
std::sync::RwLock::new(Vec::new());
// `zlemetaline` — the actual line buffer. Deduped: reads/writes the
// canonical `compcore::ZLEMETALINE` (lex.c:103) instead of a private
// module static shadow. Same C variable, same meaning.
/// `noerrs` / `noaliases` — lexer error/alias-suppression flags.
// `noerrs` is ONE variable in C (`mod_export int noerrs;`, Src/exec.c:117).
// This file used to declare a THIRD private copy of it, which meant
// `getreal`'s suppression window (c:Src/Zle/compctl.c:2144, whose comment at
// c:2135 is "During this errors are not reported") wrote a cell nothing read:
// `zerr`/`zwarn`/`zerrnam`/`zwarnnam` all consult
// `crate::ported::utils::noerrs_lock()` (utils.rs:221/244/262/279). A
// diagnostic raised by the expansion inside `getreal` therefore printed where
// zsh is silent. Every use below now goes to the single shared storage.
// `noaliases` is `lex::noaliases()`; `instring`/`inbackt` are the canonical
// `zle_tricky::{INSTRING, INBACKT}` (one C variable each, zle_tricky.c:419).
// `autoq` and `compqstack` were formerly compctl-private copies; they
// are now deduped to the canonical `zle_tricky::AUTOQ` (glob-imported)
// and `complete::COMPQSTACK` respectively. C declares one of each.
// `qipre` / `qisuf` — quoted ignored prefix/suffix. ONE variable pair in C
// (`Src/Zle/zle_tricky.c:137`), ported as `zle_tricky::QIPRE` / `QISUF`
// (glob-imported above); compctl.rs used to keep a private second copy that
// only `makecomplistctl`/`sep_comp_string` wrote, so the live
// `get_comp_string` values never reached it.
/// `compqiprefix` / `compqisuffix` / `compisuffix` — completion-context
/// state from the user's compfunc. Port of those file-statics.
static COMPQIPREFIX: Mutex<String> = Mutex::new(String::new());
static COMPQISUFFIX: Mutex<String> = Mutex::new(String::new());
static COMPISUFFIX: Mutex<String> = Mutex::new(String::new());
/// `isuf` — the ignored (tokenized) suffix. Port of the `mod_export
/// char *isuf;` from `Src/Zle/compcore.c:118`. `makecomplistctl`
/// derives it from `compisuffix`; `add_match_data` consumes it.
static ISUF: Mutex<String> = Mutex::new(String::new());
// `compwords` / `compcurrent` were formerly compctl-private copies;
// deduped to the canonical `complete::COMPWORDS` / `complete::COMPCURRENT`
// (imported above). C declares one of each in complete.c.
/// `clwords` / `clwsize` / `clwnum` / `clwpos` — current line word
/// array + sizes used by the completion code.
static CLWORDS: Mutex<Vec<String>> = Mutex::new(Vec::new());
static CLWSIZE: Mutex<i32> = Mutex::new(0);
static CLWNUM: Mutex<i32> = Mutex::new(0);
static CLWPOS: Mutex<i32> = Mutex::new(0);
// `offs` — completion offset into the current word — is ONE variable in C
// (`mod_export int offs;`, Src/Zle/zle_tricky.c:88), ported as
// `compcore::OFFS`. compctl.rs used to keep a private second copy, which
// split the write side from the read side: `makecomplistctl` set the
// private one (c:2378 `offs = lip + lp`) while `makecomplistflags` read the
// canonical one for its line prefix/suffix split (c:3186 `lpl = offs`;
// c:3211-3212 `lsuf = s + offs`). The cursor therefore landed at the START
// of the word inside every `compcall`, so `lpre` came out empty and `lsuf`
// held the whole word — and `addmatch`'s `addwhat == -3` arm
// (c:Src/Zle/compctl.c:2021-2022, which matches names against
// `lpre`/`lsuf`) anchored on the SUFFIX. `compcall` with the word `-`
// offered every name ENDING in `-` (`-`, `_2to3-`) instead of the none that
// start with it. Same dedup the `clwords`/`compwords` note above describes.
/// `brange` / `erange` — `-l` word-range begin/end.
static BRANGE: Mutex<i32> = Mutex::new(0);
static ERANGE: Mutex<i32> = Mutex::new(0);
/// `linwhat` — line-context kind. Port of `mod_export int linwhat`
/// from `Src/Zle/compcore.c:91`. Values are the `IN_*` enum at
/// `Src/zsh.h:2321-2332` (ported in zsh_h.rs). NB: dead code is
/// fake — the previous Rust `linwhat_kind` mod had `IN_ENV=1` and
/// an invented `IN_REDIR=4`; both wrong vs the real C enum.
static LINWHAT: Mutex<i32> = Mutex::new(IN_NOTHING);
/// `linredir` — non-zero when completing inside a redirection.
static LINREDIR: Mutex<i32> = Mutex::new(0);
/// `insubscr` — non-zero inside an array subscript context.
static INSUBSCR: Mutex<i32> = Mutex::new(0);
/// Inull-token chars from Src/zsh.h. These are the byte values
/// the lexer uses to mark suppressed quoted-region boundaries
/// (Snull = single-quote, Dnull = double-quote, Bnull = backslash,
/// String/Qstring = `$`/`'$'` markers).
pub const Snull: char = '\u{e19d}'; // Single-quote null
pub const Dnull: char = '\u{e19e}'; // Double-quote null
pub const Bnull: char = '\u{e19f}'; // Backslash null
pub const Stringg: char = '\u{e185}'; // META-$
// =================================================================
// Module boot/cleanup hooks — port of compctl.c:4000+
// =================================================================
/// Storage for the special compctl targets — `cc_compos` (command
/// completion), `cc_default` (default completion), `cc_first`
/// (first completion). Port of the file-static C declarations at
/// Src/Zle/compctl.c:41 — `struct compctl cc_compos, cc_default,
/// cc_first, cc_dummy;`. setup_ initializes the masks; tests +
/// real-completion paths read them.
pub(crate) static CC_COMPOS: Mutex<Option<Arc<Compctl>>> = Mutex::new(None);
pub(crate) static CC_DEFAULT: Mutex<Option<Arc<Compctl>>> = Mutex::new(None);
pub(crate) static CC_FIRST: Mutex<Option<Arc<Compctl>>> = Mutex::new(None);
pub(crate) static CC_DUMMY: Mutex<Option<Arc<Compctl>>> = Mutex::new(None);
/// Last-used compctl tracking list. Port of `LinkList lastccused`
/// at Src/Zle/compctl.c:1702. setup_ initializes to empty; finish_
/// frees its contents.
static LASTCCUSED: Mutex<Vec<Arc<Compctl>>> = Mutex::new(Vec::new());
/// Pointer to compctlread (vs fallback_compctlread). Port of the
/// `CompctlReadFn compctlreadptr` indirect dispatch at
/// Src/Modules/zle/compctl.c:4016. setup_ installs this; finish_
/// restores the fallback.
static COMPCTLREAD_INSTALLED: Mutex<bool> = Mutex::new(false);
/// Direct port of `#define inull(X) zistype(X,INULL)` from
/// `Src/ztype.h:62`. Tests whether `c` is one of the parser's
/// "inull" token chars (the high-bit token bytes the lexer
/// produces).
fn inull(c: char) -> bool {
// c:62
matches!(c, Snull | Dnull | Bnull | Bnullkeep | Nularg)
}
#[cfg(test)]
mod tests {
use super::*;
/// Serialize tests that touch the singleton state — `cargo test`
/// runs tests in parallel and the static `COMPCTL_TAB` / `CCLIST`
/// would interleave. The parking_lot variant would deadlock-free
/// across panics; std::sync::Mutex is fine since each test runs
/// quickly and panics propagate.
static TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[test]
fn createcompctltable_initializes_table() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
createcompctltable();
let g = COMPCTL_TAB.read().unwrap();
assert!(g.is_some());
assert_eq!(g.as_ref().unwrap().len(), 0);
}
#[test]
fn cc_assign_copies_cct_flags_into_ccptr() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
CCLIST.with(|c| c.set(0));
let cct = Compctl {
mask: CC_FILES,
func: Some("fn1".to_string()),
mstr: None,
..Default::default()
};
let mut dst = Compctl::default();
assert_eq!(cc_assign("ls", &mut dst, &cct, false), 0);
assert_eq!(dst.mask, CC_FILES);
assert_eq!(dst.func.as_deref(), Some("fn1"));
}
#[test]
fn freecompctlp_removes_entry() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
createcompctltable();
CCLIST.with(|c| c.set(0));
compctl_process_cc(&["rm".to_string()], Arc::new(Compctl::default()));
assert!(COMPCTL_TAB.read().unwrap().as_ref().unwrap().contains_key("rm"));
freecompctlp("rm");
let g = COMPCTL_TAB.read().unwrap();
assert!(!g.as_ref().unwrap().contains_key("rm"));
}
#[test]
fn cc_flags_bit_layout_matches_c_compctlh() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// Spot-check that the bit values match the C constants.
assert_eq!(CC_FILES, 1);
assert_eq!(CC_COMMPATH, 2);
assert_eq!(CC_OPTIONS, 8);
assert_eq!(CC_JOBS, 1 << 11);
}
#[test]
fn cct_constants_match_c_compctlh() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
assert_eq!(CCT_POS, 1);
assert_eq!(CCT_CURPAT, 3);
assert_eq!(CCT_QUOTE, 13);
}
#[test]
fn comp_op_special_combines_command_default_first() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
assert_eq!(COMP_SPECIAL, COMP_COMMAND | COMP_DEFAULT | COMP_FIRST);
}
#[test]
fn cc_flags2_constants_match_c_compctlh() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
assert_eq!(CC_NOSORT, 1);
assert_eq!(CC_CCCONT, 4);
assert_eq!(CC_UNIQALL, 1 << 6);
}
#[test]
fn get_compctl_simple_flag_chars_set_mask() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// `compctl -fcv ls` — files + commpath + vars
let mut argv = vec!["-fcv".to_string(), "ls".to_string()];
let mut cc = Compctl::default();
let r = get_compctl("compctl", &mut argv, &mut cc, true, false, 0);
assert_eq!(r, 0);
assert_ne!(cc.mask & CC_FILES, 0);
assert_ne!(cc.mask & CC_COMMPATH, 0);
assert_ne!(cc.mask & CC_VARS, 0);
// `ls` should remain in argv
assert_eq!(argv, vec!["ls".to_string()]);
}
#[test]
fn get_compctl_combined_a_sets_alreg_and_alglob() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let mut argv = vec!["-a".to_string(), "ls".to_string()];
let mut cc = Compctl::default();
get_compctl("compctl", &mut argv, &mut cc, true, false, 0);
assert_ne!(cc.mask & CC_ALREG, 0);
assert_ne!(cc.mask & CC_ALGLOB, 0);
}
#[test]
fn get_compctl_arg_taking_K_captures_function_name() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let mut argv = vec![
"-K".to_string(),
"_my_completer".to_string(),
"myfunc".to_string(),
];
let mut cc = Compctl::default();
get_compctl("compctl", &mut argv, &mut cc, true, false, 0);
assert_eq!(cc.func.as_deref(), Some("_my_completer"));
assert_eq!(argv, vec!["myfunc".to_string()]);
}
#[test]
fn get_compctl_inline_arg_K_captures_function_name() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// `-K_my_func` → the K flag char with inline arg
let mut argv = vec!["-K_my_func".to_string(), "myfunc".to_string()];
let mut cc = Compctl::default();
get_compctl("compctl", &mut argv, &mut cc, true, false, 0);
assert_eq!(cc.func.as_deref(), Some("_my_func"));
}
#[test]
fn get_compctl_P_S_capture_prefix_suffix() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let mut argv = vec![
"-P".to_string(),
"before-".to_string(),
"-S".to_string(),
"-after".to_string(),
"cmd".to_string(),
];
let mut cc = Compctl::default();
get_compctl("compctl", &mut argv, &mut cc, true, false, 0);
assert_eq!(cc.prefix.as_deref(), Some("before-"));
assert_eq!(cc.suffix.as_deref(), Some("-after"));
}
#[test]
fn get_compctl_1_2_set_uniq_flags() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let mut argv = vec!["-1".to_string(), "ls".to_string()];
let mut cc = Compctl::default();
get_compctl("compctl", &mut argv, &mut cc, true, false, 0);
assert_ne!(cc.mask2 & CC_UNIQALL, 0);
assert_eq!(cc.mask2 & CC_UNIQCON, 0);
}
#[test]
fn get_compctl_V_implies_NOSORT() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let mut argv = vec!["-V".to_string(), "mygroup".to_string(), "cmd".to_string()];
let mut cc = Compctl::default();
get_compctl("compctl", &mut argv, &mut cc, true, false, 0);
assert_eq!(cc.gname.as_deref(), Some("mygroup"));
assert_ne!(cc.mask2 & CC_NOSORT, 0);
}
#[test]
fn bin_compctl_install_then_lookup_via_table() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
createcompctltable();
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let r = bin_compctl("compctl", &["-f".to_string(), "mycmd".to_string()], &ops, 0);
assert_eq!(r, 0);
let g = COMPCTL_TAB.read().unwrap();
assert!(g.as_ref().unwrap().contains_key("mycmd"));
let cc = g.as_ref().unwrap().get("mycmd").unwrap();
assert_ne!(cc.mask & CC_FILES, 0);
}
#[test]
fn compctl_name_pat_detects_glob_wildcards() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// Glob-meta chars present → pattern.
let (is_pat, _) = compctl_name_pat("ls*");
assert!(is_pat);
let (is_pat, _) = compctl_name_pat("foo?bar");
assert!(is_pat);
let (is_pat, _) = compctl_name_pat("[abc]");
assert!(is_pat);
}
#[test]
fn compctl_name_pat_strips_backslashes_from_literal() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let (is_pat, out) = compctl_name_pat("\\$home");
assert!(!is_pat);
// Backslash dropped, `$` kept.
assert_eq!(out, "$home");
}
#[test]
fn delpatcomp_removes_matching_pattern() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut p = PATCOMPS.write().unwrap();
p.push(("foo*".to_string(), Arc::new(Compctl::default())));
p.push(("bar*".to_string(), Arc::new(Compctl::default())));
drop(p);
delpatcomp("foo*");
let p = PATCOMPS.read().unwrap();
assert_eq!(p.len(), 1);
assert_eq!(p[0].0, "bar*");
}
/// `delpatcomp` MUST remove only the FIRST match (c:1308 `break`)
/// when duplicate names exist. C uses the explicit `break` after
/// the first hit. A regression that switched to `Vec::retain`
/// would remove ALL matches — semantically different, observable
/// when users register multiple compctl entries under the same
/// pattern (`compctl -p 'foo*' ...` twice for layered configs).
/// `delpatcomp` MUST remove only the FIRST match (c:1308 `break`)
/// when duplicate names exist. C uses the explicit `break` after
/// the first hit. A regression that switched to `Vec::retain`
/// would remove ALL matches — semantically different, observable
/// when users register multiple compctl entries under the same
/// pattern (`compctl -p 'foo*' ...` twice for layered configs).
#[test]
fn delpatcomp_removes_only_first_match_when_duplicates() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Clear shared PATCOMPS state — other tests may have left
// entries that would interfere with the duplicate-count.
{
let mut p = PATCOMPS.write().unwrap();
p.clear();
// Three entries under the same pattern name — distinguishable
// by the embedded Compctl::keyvar (used as a tag).
for tag in ["a", "b", "c"] {
p.push((
"dup*".to_string(),
Arc::new(Compctl {
keyvar: Some(tag.to_string()),
..Compctl::default()
}),
));
}
}
delpatcomp("dup*");
let p = PATCOMPS.read().unwrap();
assert_eq!(
p.len(),
2,
"c:1308 — only ONE entry removed; got len={}",
p.len()
);
// The remaining entries must be the 2nd and 3rd (b then c) —
// the first ("a") was the one removed.
assert_eq!(
p[0].1.keyvar.as_deref(),
Some("b"),
"remaining first entry must be the second-inserted (b)"
);
assert_eq!(
p[1].1.keyvar.as_deref(),
Some("c"),
"remaining second entry must be the third-inserted (c)"
);
}
#[test]
fn get_compctl_dash_c_stores_into_cc_compos() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
CCLIST.with(|c| c.set(0));
let mut argv: Vec<String> = vec!["-C".into(), "-f".into()];
let mut cc = Compctl::default();
assert_eq!(get_compctl("compctl", &mut argv, &mut cc, true, false, 0), 0);
assert_eq!(CCLIST.with(|c| c.get()), COMP_COMMAND);
let compos = CC_COMPOS.lock().unwrap().clone().unwrap();
assert_eq!(compos.mask, CC_FILES | CC_EXCMDS);
// The caller's own compctl is untouched, as C's `cc` pointer is.
assert_eq!(cc.mask, 0);
CCLIST.with(|c| c.set(0));
}
#[test]
fn get_compctl_dash_d_stores_into_cc_default_keeping_refc() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
CCLIST.with(|c| c.set(0));
let mut argv: Vec<String> = vec!["-D".into(), "-g".into(), "*.c".into()];
let mut cc = Compctl::default();
assert_eq!(get_compctl("compctl", &mut argv, &mut cc, true, false, 0), 0);
let def = CC_DEFAULT.lock().unwrap().clone().unwrap();
assert_eq!(def.glob.as_deref(), Some("*.c"));
assert_eq!(def.refc, 10000);
CCLIST.with(|c| c.set(0));
setup_();
}
#[test]
fn setup_initializes_special_targets_and_table() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
// cc_compos has CC_COMMPATH set
let cc_compos = CC_COMPOS.lock().unwrap().clone();
assert!(cc_compos.is_some());
assert_eq!(cc_compos.unwrap().mask, CC_COMMPATH);
// cc_default has CC_FILES + refc=10000 sentinel
let cc_default = CC_DEFAULT.lock().unwrap().clone();
assert!(cc_default.is_some());
let cc_default = cc_default.unwrap();
assert_eq!(cc_default.mask, CC_FILES);
assert_eq!(cc_default.refc, 10000);
// cc_first has CC_CCCONT in mask2
let cc_first = CC_FIRST.lock().unwrap().clone();
assert!(cc_first.is_some());
assert_eq!(cc_first.unwrap().mask2, CC_CCCONT);
// table exists
assert!(COMPCTL_TAB.read().unwrap().is_some());
// compctlread installed
assert!(*COMPCTLREAD_INSTALLED.lock().unwrap());
}
#[test]
fn finish_tears_down_state() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
finish_();
// Table cleared
assert!(COMPCTL_TAB.read().unwrap().is_none());
// compctlread restored
assert!(!*COMPCTLREAD_INSTALLED.lock().unwrap());
// lastccused cleared
assert_eq!(LASTCCUSED.lock().unwrap().len(), 0);
}
#[test]
fn features_returns_two_builtins() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let f = features_();
assert_eq!(f, vec!["b:compctl".to_string(), "b:compcall".to_string()]);
}
#[test]
fn enables_returns_two_enabled_bits() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let e = enables_();
assert_eq!(e, vec![1, 1]);
}
#[test]
fn bin_compcall_outside_compfunc_errors() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
INCOMPFUNC.store(0, std::sync::atomic::Ordering::Relaxed);
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let r = bin_compcall("compcall", &[], &ops, 0);
assert_eq!(r, 1);
}
#[test]
fn bin_compcall_inside_compfunc_succeeds() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
INCOMPFUNC.store(1, std::sync::atomic::Ordering::Relaxed);
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let r = bin_compcall("compcall", &["-T".to_string()], &ops, 0);
assert_eq!(r, 0);
// Reset
INCOMPFUNC.store(0, std::sync::atomic::Ordering::Relaxed);
}
#[test]
fn compctlread_outside_compctl_func_errors() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
INCOMPCTLFUNC.with(|c| c.set(false));
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let r = compctlread("compctlread", &[], &ops, "reply");
assert_eq!(r, 1);
}
/// c:1763 / c:1910 — both compctl hooks have the `Hookfn` shape that
/// `addhookfunc` takes.
#[test]
fn compctl_hooks_have_hookfn_signature() {
let _: crate::ported::zsh_h::Hookfn = ccmakehookfn;
let _: crate::ported::zsh_h::Hookfn = cccleanuphookfn;
}
#[test]
fn cccleanuphookfn_returns_zero() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// Trivial — no state to verify, just that it doesn't panic.
assert_eq!(cccleanuphookfn(std::ptr::null_mut(), std::ptr::null_mut()), 0);
}
// Helpers for the addmatch tests: snapshot the real match registry so
// we can assert `add_match_data` actually built and registered a
// Cmatch (not just that a name landed in the observability mirror).
fn mnum_now() -> i32 {
crate::ported::zle::compcore::mnum.load(std::sync::atomic::Ordering::Relaxed)
}
fn clear_matches() {
crate::comp_match_handles::matches_arc()
.lock()
.unwrap()
.clear();
}
fn last_match_orig() -> Option<String> {
crate::comp_match_handles::matches_arc()
.lock()
.unwrap()
.last()
.and_then(|cm| cm.orig.clone())
}
// Reset the compctl prefix/suffix file-statics to empty so an
// addmatch test that drives addmatch directly (without running the
// makecomplistflags preamble) sees a clean, thread-local-stale-free
// matcher context.
fn reset_compctl_statics() {
PATCOMP.with(|r| *r.borrow_mut() = None);
FILECOMP.with(|r| *r.borrow_mut() = None);
for st in [
&LPRE, &LSUF, &RPRE, &RSUF, &PPRE, &PSUF, &LPPRE, &LPSUF, &FPRE, &FSUF, &QFPRE, &QFSUF,
&QRPRE, &QRSUF, &QLPRE, &QLSUF,
] {
st.with(|r| r.borrow_mut().clear());
}
crate::ported::zle::compcore::mflags.store(0, std::sync::atomic::Ordering::Relaxed);
}
#[test]
fn addmatch_rejects_unset_addwhat() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// C: c:2015 — the `else` arm drops the match (returns without
// calling add_match_data) when addwhat is 0: no Cmatch is built
// and mnum does not move.
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
MATCH_LIST.with(|r| r.borrow_mut().clear());
clear_matches();
reset_compctl_statics();
let before = mnum_now();
ADDWHAT.with(|c| c.set(0));
addmatch("dropped", None);
assert!(
MATCH_LIST.with(|r| r.borrow().is_empty()),
"addwhat=0 should drop the match"
);
assert_eq!(mnum_now(), before, "no Cmatch may be registered");
assert_eq!(
crate::comp_match_handles::matches_arc()
.lock()
.unwrap()
.len(),
0,
"match registry stays empty"
);
}
#[test]
fn addmatch_accepts_files_kind() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// c:1957-1990 file thread → comp_match + add_match_data builds a
// real Cmatch per accepted filename. With empty prefix/suffix
// statics comp_match matches trivially, so both names register.
MATCH_LIST.with(|r| r.borrow_mut().clear());
clear_matches();
reset_compctl_statics();
let before = mnum_now();
ADDWHAT.with(|c| c.set(-5));
addmatch("foo.txt", None);
addmatch("bar.txt", None);
assert_eq!(mnum_now(), before + 2, "two Cmatches registered");
let reg = crate::comp_match_handles::matches_arc()
.lock()
.unwrap()
.clone();
assert_eq!(reg.len(), 2);
assert_eq!(reg[0].orig.as_deref(), Some("foo.txt"));
assert_eq!(reg[1].orig.as_deref(), Some("bar.txt"));
// c:1990 — file-thread matches carry CMF_FILE in their flags.
use crate::ported::zle::comp_h::CMF_FILE;
assert_ne!(reg[0].flags & CMF_FILE, 0, "file matches flagged CMF_FILE");
}
#[test]
fn addmatch_accepts_param_kind() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// c:1995 — addwhat == -9 (parameter) → accept thread. The name is
// matched against the (empty) real prefix/suffix and a real
// Cmatch is produced; unlike the file thread it is not CMF_FILE.
MATCH_LIST.with(|r| r.borrow_mut().clear());
clear_matches();
reset_compctl_statics();
let before = mnum_now();
ADDWHAT.with(|c| c.set(-9));
addmatch("HOME", None);
assert_eq!(mnum_now(), before + 1, "one Cmatch registered");
assert_eq!(last_match_orig().as_deref(), Some("HOME"));
use crate::ported::zle::comp_h::CMF_FILE;
let reg = crate::comp_match_handles::matches_arc()
.lock()
.unwrap()
.clone();
assert_eq!(reg[0].flags & CMF_FILE, 0, "param match is not a file");
}
#[test]
fn addmatch_accepts_cc_files_positive_mask() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// c:1957 — a positive addwhat that includes CC_FILES routes down
// the file thread and registers a real Cmatch.
MATCH_LIST.with(|r| r.borrow_mut().clear());
clear_matches();
reset_compctl_statics();
let before = mnum_now();
ADDWHAT.with(|c| c.set(CC_FILES as i32));
addmatch("foo", None);
assert_eq!(mnum_now(), before + 1);
assert_eq!(last_match_orig().as_deref(), Some("foo"));
}
#[test]
fn getcpat_finds_first_substring() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// Search "abcabc" for "bc" first occurrence → position 3
// (1-based, points past the matched substring).
let r = getcpat("abcabc", 1, "bc", 0);
assert_eq!(r, 3);
}
#[test]
fn getcpat_finds_second_substring() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// Search "abcabc" for the 2nd "bc" → position 6.
let r = getcpat("abcabc", 2, "bc", 0);
assert_eq!(r, 6);
}
#[test]
fn getcpat_negative_index_searches_backward() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// Backward search "abcabc" for last "bc" → position 5.
let r = getcpat("abcabc", -1, "bc", 0);
assert!(r >= 0, "should find match (got {})", r);
}
#[test]
fn getcpat_class_mode_matches_any_char_in_set() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// Search "abcdef" for any of {b, d, f} — class mode.
// First match at index 1 (b).
let r = getcpat("abcdef", 1, "bdf", 1);
assert_eq!(r, 2); // 1-based position of 'b'
}
#[test]
fn getcpat_not_found_returns_negative_one() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let r = getcpat("hello", 1, "xyz", 0);
assert_eq!(r, -1);
}
#[test]
fn getcpat_strips_backslashes_in_pattern() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// `\$` in pattern should be treated as literal `$`.
let r = getcpat("foo$bar", 1, "\\$", 0);
assert_eq!(r, 4); // 1-based pos right after the `$`
}
#[test]
fn dumphashtable_calls_addmatch_per_entry() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
MATCH_LIST.with(|r| r.borrow_mut().clear());
let entries: Vec<(String, AddmatchNode)> = ["alpha", "beta", "gamma"]
.iter()
.map(|n| {
let hn = crate::ported::zsh_h::hashnode {
next: None,
nam: n.to_string(),
flags: 0,
};
(n.to_string(), AddmatchNode::Hash(hn))
})
.collect();
dumphashtable(entries, -5);
let m = MATCH_LIST.with(|r| r.borrow().clone());
assert_eq!(m.len(), 3);
}
#[test]
fn addhnmatch_forwards_to_addmatch() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
MATCH_LIST.with(|r| r.borrow_mut().clear());
ADDWHAT.with(|c| c.set(-5));
addhnmatch("xyz", 0);
let m = MATCH_LIST.with(|r| r.borrow().clone());
assert_eq!(m.len(), 1);
assert_eq!(m[0], "xyz");
}
#[test]
fn makecomplistctl_recursion_guard() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Force depth to MAX
CDEPTH.with(|c| c.set(MAX_CDEPTH));
let r = makecomplistctl(0);
assert_eq!(r, 0);
// Reset for other tests.
CDEPTH.with(|c| c.set(0));
}
#[test]
fn makecomplistflags_cc_files_invokes_gen_matches() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
MATCH_LIST.with(|r| r.borrow_mut().clear());
// Set prpre to a known dir we can read.
PRPRE.with(|r| *r.borrow_mut() = Some(".".to_string()));
let cc = Arc::new(Compctl {
mask: CC_FILES,
..Default::default()
});
makecomplistflags(&cc, String::new(), false, 0);
// Should have at least picked up Cargo.toml or similar from pwd.
let m = MATCH_LIST.with(|r| r.borrow().clone());
assert!(!m.is_empty(), "expected file matches in pwd");
}
#[test]
fn makecomplistflags_cc_str_expansion_emits_one_match() {
let _g = crate::test_util::global_state_lock();
crate::ported::utils::inittyptab();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
MATCH_LIST.with(|r| r.borrow_mut().clear());
let cc = Arc::new(Compctl {
str: Some("hardcoded".to_string()),
..Default::default()
});
makecomplistflags(&cc, String::new(), false, 0);
let m = MATCH_LIST.with(|r| r.borrow().clone());
assert_eq!(m.len(), 1);
assert_eq!(m[0], "hardcoded");
}
#[test]
fn makecomplistor_walks_xor_chain() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
MATCH_LIST.with(|r| r.borrow_mut().clear());
// Build cc1 with str "first", xor → cc2 with str "second"
let cc2 = Arc::new(Compctl {
str: Some("second".to_string()),
..Default::default()
});
let cc1 = Arc::new(Compctl {
str: Some("first".to_string()),
mask2: CC_XORCONT, // c:2600 ct: continue into the xor even with matches
xor: Some(cc2),
..Default::default()
});
makecomplistor(&cc1, "", false, 0, 0);
let m = MATCH_LIST.with(|r| r.borrow().clone());
assert_eq!(m.len(), 2);
assert_eq!(m[0], "first");
assert_eq!(m[1], "second");
}
#[test]
fn makecomplistcc_pushes_to_ccused() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
CCUSED.with(|r| r.borrow_mut().clear());
let cc = Arc::new(Compctl::default());
makecomplistcc(&cc, "", false);
let used = CCUSED.with(|r| r.borrow().clone());
assert_eq!(used.len(), 1);
}
#[test]
fn makecomplistpc_iterates_patcomps() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Verify makecomplistpc returns 0 when cmdstr is unset
// (its early-bail path) — full pattern-match test requires
// VM context for glob_match_static.
CMDSTR.with(|r| *r.borrow_mut() = None);
let r = makecomplistpc("", false);
assert_eq!(r, 0);
}
#[test]
fn findnode_returns_index_of_match() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let list = vec!["a".to_string(), "b".to_string(), "c".to_string()];
assert_eq!(findnode(&list, &"b".to_string()), Some(1));
assert_eq!(findnode(&list, &"z".to_string()), None);
}
#[test]
fn cc_assign_rejects_conflicting_special_targets() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
createcompctltable();
CCLIST.with(|c| c.set(COMP_COMMAND | COMP_DEFAULT));
let mut dst = Compctl::default();
// Rejected: -C and -D together can't both be targeted.
assert_eq!(cc_assign("compctl", &mut dst, &Compctl::default(), true), 1);
CCLIST.with(|c| c.set(0));
}
#[test]
fn compctl_process_cc_remove_deletes_named_entries() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
createcompctltable();
CCLIST.with(|c| c.set(0));
compctl_process_cc(
&["foo".to_string(), "bar".to_string()],
Arc::new(Compctl::default()),
);
CCLIST.with(|c| c.set(COMP_REMOVE));
compctl_process_cc(&["foo".to_string()], Arc::new(Compctl::default()));
let g = COMPCTL_TAB.read().unwrap();
let map = g.as_ref().unwrap();
assert!(!map.contains_key("foo"));
assert!(map.contains_key("bar"));
// Reset cclist for other tests.
CCLIST.with(|c| c.set(0));
}
#[test]
fn sep_comp_string_returns_zero_or_one() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// C compctl.c:2806-3030 contract — sep_comp_string only returns
// 0 (success / dispatched) or 1 (bail, no cursor word).
let r = sep_comp_string("", "", 0);
assert!(r == 0 || r == 1, "expected 0 or 1, got {}", r);
}
#[test]
fn sep_comp_string_round_trips_zle_state() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Start from an empty compctl registry. Whatever spec an earlier
// test in the same binary left behind is reachable from the nested
// `makecomplistcmd` dispatch this function ends with (c:3013), and a
// spec that fires runs `makecomplistflags`, whose first act is to
// move the cursor to the end of the word (c:3066-3068) — a move C
// makes too, and deliberately does not undo. The saved-state
// assertions below are about what `sep_comp_string` ITSELF restores
// (c:2887-2891), so the dispatch has to have nothing to dispatch to
// for them to mean anything.
createcompctltable();
*CC_DEFAULT.lock().unwrap_or_else(|e| e.into_inner()) = None;
*CC_FIRST.lock().unwrap_or_else(|e| e.into_inner()) = None;
*CC_COMPOS.lock().unwrap_or_else(|e| e.into_inner()) = None;
// Pre-set zle_tricky.c globals; sep_comp_string must restore them
// on exit (C compctl.c:2810-2813 save / 2941-2950 restore).
use crate::ported::zle::compcore::{WB, WE, ZLEMETACS as CS_G, ZLEMETALINE as LINE_G};
use std::sync::atomic::Ordering;
WE.store(42, Ordering::Relaxed);
WB.store(7, Ordering::Relaxed);
CS_G.store(11, Ordering::Relaxed);
crate::ported::zle::compcore::ZLEMETALL.store(99, Ordering::Relaxed);
INSTRING.store(QT_DOUBLE, Ordering::Relaxed);
INBACKT.store(1, Ordering::Relaxed);
crate::ported::lex::set_noaliases(true);
*crate::ported::utils::noerrs_lock().lock().unwrap() = 0;
*LINE_G
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = "hello".to_string();
*AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = "Q".to_string();
let _ = sep_comp_string("", "x", 0);
assert_eq!(WE.load(Ordering::Relaxed), 42);
assert_eq!(WB.load(Ordering::Relaxed), 7);
assert_eq!(CS_G.load(Ordering::Relaxed), 11);
assert_eq!(crate::ported::zle::compcore::ZLEMETALL.load(Ordering::Relaxed), 99);
assert_eq!(INSTRING.load(Ordering::Relaxed), QT_DOUBLE);
assert_eq!(INBACKT.load(Ordering::Relaxed), 1);
assert!(crate::ported::lex::noaliases());
assert_eq!(*crate::ported::utils::noerrs_lock().lock().unwrap(), 0);
assert_eq!(
*LINE_G
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap(),
"hello"
);
assert_eq!(
*AUTOQ
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap(),
"Q"
);
}
/// `compctl -x COND1 -s TAG1 - COND2 -s TAG2 -- foo`, built directly:
/// every `(cond, tag)` pair becomes one extended-completion entry whose
/// only effect is to add `tag` as a match when `cond` holds. Runs
/// `sep_comp_string(ss, s, noffs)` and returns the matches the nested
/// dispatch (c:3013) produced, i.e. which conditions held for the word
/// array the re-lex built.
fn sep_probe(conds: &[(Compcond, &str)], ss: &str, s: &str, noffs: i32) -> Vec<String> {
createcompctltable();
*CC_DEFAULT.lock().unwrap_or_else(|e| e.into_inner()) = None;
*CC_FIRST.lock().unwrap_or_else(|e| e.into_inner()) = None;
*CC_COMPOS.lock().unwrap_or_else(|e| e.into_inner()) = None;
let mut ext: Option<Arc<Compctl>> = None;
for (cond, tag) in conds.iter().rev() {
ext = Some(Arc::new(Compctl {
cond: Some(Box::new(cond.clone())),
str: Some(tag.to_string()),
next: ext,
..Default::default()
}));
}
COMPCTL_TAB
.write()
.unwrap()
.get_or_insert_with(HashMap::new)
.insert("foo".to_string(), Arc::new(Compctl { ext, ..Default::default() }));
MATCH_LIST.with(|r| r.borrow_mut().clear());
reset_compctl_statics();
CLWORDS.lock().unwrap().clear();
assert_eq!(sep_comp_string(ss, s, noffs), 0);
MATCH_LIST.with(|r| r.borrow().clone())
}
/// `p[n]` — the cursor is on word `n`.
fn cond_pos(n: i32) -> Compcond {
Compcond {
typ: CCT_POS,
n: 1,
u: CompcondData::R { a: vec![n], b: vec![n] },
..Default::default()
}
}
/// `p[0,9999]` — always true.
fn cond_pos_any() -> Compcond {
Compcond {
typ: CCT_POS,
n: 1,
u: CompcondData::R { a: vec![0], b: vec![9999] },
..Default::default()
}
}
/// `w[idx,str]` / `c[off,str]` — word `idx` (or cursor word + `off`) is `str`.
fn cond_word(typ: i32, idx: i32, s: &str) -> Compcond {
Compcond {
typ,
n: 1,
u: CompcondData::S { p: vec![idx], s: vec![s.to_string()] },
..Default::default()
}
}
/// Run `sep_comp_string` with the cursor/`wb`/`we` globals parked at 0
/// (so c:3066 does not move the cursor to the word end) and report
/// which `p[n]` entry of the extended-completion chain fired, i.e. the
/// `clwpos` the nested dispatch saw (the command word is word 0).
fn sep_clwpos(ss: &str, s: &str, noffs: i32) -> Option<i32> {
let conds: Vec<(Compcond, String)> =
(0..8).map(|p| (cond_pos(p), format!("h{p}"))).collect();
let chain: Vec<(Compcond, &str)> =
conds.iter().map(|(c, t)| (c.clone(), t.as_str())).collect();
sep_probe(&chain, ss, s, noffs)
.first()
.and_then(|t| t[1..].parse().ok())
}
/// Whether `cond` holds for the word array `sep_comp_string` built.
fn sep_cond(cond: Compcond, ss: &str, s: &str, noffs: i32) -> bool {
!sep_probe(&[(cond, "hit")], ss, s, noffs).is_empty()
}
/// c:2847-2880 — the cursor word is the one whose lexed range holds the
/// dummy `x`, wherever the cursor sits in the string.
#[test]
fn sep_comp_string_cursor_word_index() {
let _g = crate::test_util::global_state_lock();
crate::ported::utils::inittyptab();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// `foo h h h` with the cursor in each of the three words of `s`
// (every word is `h` so the `hN` tag always passes the prefix filter).
assert_eq!(sep_clwpos("foo", "h h h", 1), Some(1));
assert_eq!(sep_clwpos("foo", "h h h", 3), Some(2));
assert_eq!(sep_clwpos("foo", "h h h", 5), Some(3));
assert_eq!(sep_clwpos("foo", "a b h", 5), Some(3));
// Cursor right after a separator: the empty word being started.
assert_eq!(sep_clwpos("foo", "a b ", 4), Some(3));
// More words in `ss` shift the index.
assert_eq!(sep_clwpos("foo -f -g", "h", 1), Some(3));
}
/// c:2998-3006 — `clwords` is the lexed words, untokenized, with the
/// cursor word's dummy `x` removed (c:2876 `chuck(p + soffs)`).
#[test]
fn sep_comp_string_words_array_contents() {
let _g = crate::test_util::global_state_lock();
crate::ported::utils::inittyptab();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
assert!(sep_cond(cond_word(CCT_WORDSTR, 1, "a"), "foo", "a h c", 3));
assert!(sep_cond(cond_word(CCT_WORDSTR, 2, "h"), "foo", "a h c", 3));
assert!(sep_cond(cond_word(CCT_WORDSTR, 3, "c"), "foo", "a h c", 3));
assert!(!sep_cond(cond_word(CCT_WORDSTR, 2, "hx"), "foo", "a h c", 3));
// c[-1,a]: the word before the cursor word.
assert!(sep_cond(cond_word(CCT_CURSTR, -1, "a"), "foo", "a h c", 3));
assert!(sep_cond(cond_word(CCT_CURSTR, -2, "foo"), "foo", "a h c", 3));
assert!(!sep_cond(cond_word(CCT_CURSTR, -1, "foo"), "foo", "a h c", 3));
// Words of `ss` are lexed too, so a flag in `ss` is word 1.
assert!(sep_cond(cond_word(CCT_CURSTR, -1, "-f"), "foo -f", "h", 1));
// The dummy `x` is dropped from the cursor word wherever it sits
// (cursor after the `h` of `hit`; the tag `hit` still matches).
assert!(sep_cond(cond_word(CCT_WORDSTR, 1, "hit"), "foo", "hit", 1));
assert!(!sep_cond(cond_word(CCT_WORDSTR, 1, "hxit"), "foo", "hit", 1));
}
/// c:2907-2937 — a leading quote makes the whole quoted string one word
/// and sets `instring` for the dispatch; `autoq` follows it.
#[test]
fn sep_comp_string_quoted_words_and_instring() {
let _g = crate::test_util::global_state_lock();
crate::ported::utils::inittyptab();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// `'x y' h`: the single-quoted string is ONE word (word 1), the
// cursor word is the next one.
assert_eq!(sep_clwpos("foo", "'x y' h", 7), Some(2));
assert!(sep_cond(cond_word(CCT_WORDSTR, 1, "'x y'"), "foo", "'x y' h", 7));
assert_eq!(sep_clwpos("foo", "\"x y\" h", 7), Some(2));
// Unterminated quote (c:2849-2862): LEXERR with an odd number of
// quote markers is turned back into a STRING, so `'x h` is one word.
assert_eq!(sep_clwpos("foo", "'x h", 4), Some(1));
assert_eq!(sep_clwpos("foo", "a \"x h", 6), Some(2));
// instring during the dispatch (c:2907-2924): q[s] / q[d] / none.
let q = |c: &str| Compcond {
typ: CCT_QUOTE,
n: 1,
u: CompcondData::S { p: vec![0], s: vec![c.to_string()] },
..Default::default()
};
assert!(sep_cond(q("s"), "foo", "'x h", 4));
assert!(!sep_cond(q("d"), "foo", "'x h", 4));
assert!(sep_cond(q("d"), "foo", "\"x h", 4));
assert!(!sep_cond(q("s"), "foo", "\"x h", 4));
assert!(!sep_cond(q("s"), "foo", "x h", 3));
assert!(!sep_cond(q("d"), "foo", "x h", 3));
}
/// c:2938-2981 — the cursor offset inside the word (`offs`) and the
/// prefix/suffix `makecomplistflags` splits it into, after the quote
/// markers before the cursor are dropped (`soffs--` per inull).
#[test]
fn sep_comp_string_offs_follows_quote_markers() {
let _g = crate::test_util::global_state_lock();
crate::ported::utils::inittyptab();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let split = |s: &str, n: i32| {
let _ = sep_probe(&[(cond_pos_any(), "zz")], "foo", s, n);
(
LPRE.with(|r| r.borrow().clone()),
LSUF.with(|r| r.borrow().clone()),
)
};
// Unquoted: the cursor splits the word where it sits.
assert_eq!(split("a hx", 3), ("h".to_string(), "x".to_string()));
assert_eq!(split("a hx", 4), ("hx".to_string(), String::new()));
// Quoted: the opening quote is not part of the word (soffs--), so
// the same cursor-after-`hx` position yields the same prefix.
assert_eq!(split("a 'hx'", 5), ("hx".to_string(), "'".to_string()));
assert_eq!(split("a 'hx'", 4), ("h".to_string(), "x'".to_string()));
assert_eq!(split("a \"hx\" b", 5), ("hx".to_string(), "\"".to_string()));
}
/// c:2840-2841 — with a backslash-quoting level on `compqstack`,
/// `rembslash` is applied to the synthetic line, so `a\ b` is TWO words.
#[test]
fn sep_comp_string_remq_strips_backslashes() {
let _g = crate::test_util::global_state_lock();
crate::ported::utils::inittyptab();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Without a backslash level, `a\ b` stays one word.
COMPQSTACK
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.clear();
assert_eq!(sep_clwpos("foo", "a\\ b h", 6), Some(2));
// With QT_BACKSLASH on top the backslash is removed first.
*COMPQSTACK
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap() = char::from_u32(QT_BACKSLASH as u32).unwrap().to_string();
assert_eq!(sep_clwpos("foo", "a\\ b h", 6), Some(3));
// c:3029-3030 — the stack is restored after the dispatch.
assert_eq!(
*COMPQSTACK
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap(),
char::from_u32(QT_BACKSLASH as u32).unwrap().to_string()
);
COMPQSTACK
.get_or_init(|| Mutex::new(String::new()))
.lock()
.unwrap()
.clear();
}
/// c:2881-2891, c:3025-3034 — nothing the re-lex or the dispatch touches
/// leaks out: lexer globals, noerrs/noaliases/errflag, and the
/// qipre/qisuf/autoq/compqstack/instring set, on both the balanced and
/// the unterminated-quote path.
#[test]
fn sep_comp_string_restores_lexer_and_quote_state() {
let _g = crate::test_util::global_state_lock();
crate::ported::utils::inittyptab();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
for (s, n) in [("a h c", 3), ("'x y' h", 7), ("'x h", 4), ("a \"x h", 6)] {
crate::ported::lex::set_noaliases(false);
*crate::ported::utils::noerrs_lock().lock().unwrap() = 0;
crate::ported::lex::LEX_LEXFLAGS.set(0);
*QIPRE.get_or_init(|| Mutex::new(String::new())).lock().unwrap() = "QP".to_string();
*QISUF.get_or_init(|| Mutex::new(String::new())).lock().unwrap() = "QS".to_string();
*AUTOQ.get_or_init(|| Mutex::new(String::new())).lock().unwrap() = "AQ".to_string();
INSTRING.store(QT_NONE, Ordering::Relaxed);
INBACKT.store(0, Ordering::Relaxed);
let _ = sep_probe(&[(cond_pos_any(), "zz")], "foo", s, n);
assert!(!crate::ported::lex::noaliases(), "noaliases leaked for {s:?}");
assert_eq!(*crate::ported::utils::noerrs_lock().lock().unwrap(), 0);
assert_eq!(crate::ported::lex::LEX_LEXFLAGS.get(), 0, "lexflags leaked for {s:?}");
assert_eq!(errflag.load(Ordering::Relaxed) & crate::ported::utils::ERRFLAG_ERROR, 0);
assert_eq!(*QIPRE.get().unwrap().lock().unwrap(), "QP");
assert_eq!(*QISUF.get().unwrap().lock().unwrap(), "QS");
assert_eq!(*AUTOQ.get().unwrap().lock().unwrap(), "AQ");
assert_eq!(INSTRING.load(Ordering::Relaxed), QT_NONE);
assert_eq!(INBACKT.load(Ordering::Relaxed), 0);
assert_eq!(*CLWORDS.lock().unwrap(), Vec::<String>::new());
}
}
#[test]
fn inull_recognises_marker_chars() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// ztype.h:62 INULL covers Snull..Nularg (zsh.h:193-206); String/Qstring
// are not null tokens.
assert!(inull(Snull));
assert!(inull(Dnull));
assert!(inull(Bnull));
assert!(inull(Bnullkeep));
assert!(inull(Nularg));
assert!(!inull(Stringg));
assert!(!inull(Qstring));
assert!(!inull('a'));
assert!(!inull(' '));
}
#[test]
fn qt_constants_match_c_zsh_h() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// C: enum at Src/zsh.h:253-292 — QT_NONE / QT_BACKSLASH /
// QT_SINGLE / QT_DOUBLE / QT_DOLLARS / QT_BACKTICK in that
// declaration order, so values are 0..5.
assert_eq!(QT_NONE, 0);
assert_eq!(QT_BACKSLASH, 1);
assert_eq!(QT_SINGLE, 2);
assert_eq!(QT_DOUBLE, 3);
assert_eq!(QT_DOLLARS, 4);
assert_eq!(QT_BACKTICK, 5);
}
// ─── zsh-corpus pins for inull / QT_* ──────────────────────────
/// `inull` recognises every null-token in the set.
#[test]
fn compctl_corpus_inull_recognises_null_tokens() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
assert!(inull(Snull));
assert!(inull(Dnull));
assert!(inull(Bnull));
assert!(inull(Bnullkeep));
assert!(inull(Nularg));
assert!(!inull(Stringg));
assert!(!inull(Qstring));
}
/// `inull` rejects ordinary printable chars.
#[test]
fn compctl_corpus_inull_rejects_printables() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
for c in ['a', 'Z', '0', ' ', '!', '~'] {
assert!(!inull(c), "{c:?} should NOT be inull");
}
}
/// All QT_* constants are pairwise distinct.
#[test]
fn compctl_corpus_qt_pairwise_distinct() {
let qs = [
QT_NONE,
QT_BACKSLASH,
QT_SINGLE,
QT_DOUBLE,
QT_DOLLARS,
QT_BACKTICK,
];
for (i, a) in qs.iter().enumerate() {
for b in &qs[i + 1..] {
assert_ne!(a, b);
}
}
}
/// All QT_* in [0, 5] range.
#[test]
fn compctl_corpus_qt_all_within_range() {
for q in [
QT_NONE,
QT_BACKSLASH,
QT_SINGLE,
QT_DOUBLE,
QT_DOLLARS,
QT_BACKTICK,
] {
assert!((0..=5).contains(&q), "QT_* value {q} out of [0,5]");
}
}
// ═══════════════════════════════════════════════════════════════════
// Additional C-parity tests for Src/Zle/compctl.c
// c:92 createcompctltable / c:104 freecompctlp / c:1052 compctl_name_pat
// c:1080 delpatcomp / c:1303 bin_compctl / c:1435 bin_compcall /
// c:1485 ccmakehookfn / c:1573 cccleanuphookfn / c:1589 maketildelist /
// c:1948 gen_matches_files
// ═══════════════════════════════════════════════════════════════════
/// c:92 — `createcompctltable` is idempotent (safe to call multiple times).
#[test]
fn createcompctltable_idempotent() {
let _g = crate::test_util::global_state_lock();
for _ in 0..5 {
createcompctltable();
}
}
/// c:104 — `freecompctlp("")` empty name is safe.
#[test]
fn freecompctlp_empty_name_no_panic() {
let _g = crate::test_util::global_state_lock();
freecompctlp("");
}
/// c:104 — `freecompctlp(unknown)` safe.
#[test]
fn freecompctlp_unknown_name_no_panic() {
let _g = crate::test_util::global_state_lock();
freecompctlp("__never_a_real_compctl_xyz__");
}
/// c:1052 — `compctl_name_pat("")` returns (bool, String) type.
#[test]
fn compctl_name_pat_returns_tuple_type() {
let _: (bool, String) = compctl_name_pat("");
}
/// c:1052 — `compctl_name_pat` is pure.
#[test]
fn compctl_name_pat_is_pure() {
for s in ["", "name", "*pat*", "with[brackets]"] {
let first = compctl_name_pat(s);
for _ in 0..3 {
assert_eq!(
compctl_name_pat(s),
first,
"compctl_name_pat({:?}) must be pure",
s
);
}
}
}
/// c:1080 — `delpatcomp("")` empty name is safe.
#[test]
fn delpatcomp_empty_name_no_panic() {
let _g = crate::test_util::global_state_lock();
delpatcomp("");
}
/// c:1573 — `cccleanuphookfn` returns i32 (type pin).
#[test]
fn cccleanuphookfn_returns_i32_type() {
let _: i32 = cccleanuphookfn(std::ptr::null_mut(), std::ptr::null_mut());
}
/// c:1573 — `cccleanuphookfn` is idempotent.
#[test]
fn cccleanuphookfn_idempotent() {
for _ in 0..5 {
let _ = cccleanuphookfn(std::ptr::null_mut(), std::ptr::null_mut());
}
}
// ═══════════════════════════════════════════════════════════════════
// Additional C-parity tests for Src/Zle/compctl.c
// c:320 set_gmatcher / c:356 get_gmatcher / c:381 print_gmatcher /
// c:1303 bin_compctl / c:1435 bin_compcall / c:1589 maketildelist
// ═══════════════════════════════════════════════════════════════════
/// c:320 — `set_gmatcher` returns i32 (compile-time type pin).
#[test]
fn set_gmatcher_returns_i32_type() {
let _g = crate::test_util::global_state_lock();
let _: i32 = set_gmatcher("", &[]);
}
/// c:356 — `get_gmatcher` returns i32 (compile-time type pin).
#[test]
fn get_gmatcher_returns_i32_type() {
let _g = crate::test_util::global_state_lock();
let _: i32 = get_gmatcher("", &[]);
}
/// c:381 — `print_gmatcher(0)` is safe.
#[test]
fn print_gmatcher_safe_for_zero() {
let _g = crate::test_util::global_state_lock();
print_gmatcher(0);
}
/// c:1589 — `maketildelist` is safe (no panic / no side effects
/// outside the tilde-completion buffer).
#[test]
fn maketildelist_safe_no_panic() {
let _g = crate::test_util::global_state_lock();
maketildelist();
}
/// c:1589 — `maketildelist` is idempotent.
#[test]
fn maketildelist_idempotent() {
let _g = crate::test_util::global_state_lock();
for _ in 0..5 {
maketildelist();
}
}
/// c:320 — `set_gmatcher` is deterministic for stable input.
#[test]
fn set_gmatcher_is_deterministic() {
let _g = crate::test_util::global_state_lock();
let first = set_gmatcher("test", &[]);
for _ in 0..3 {
assert_eq!(
set_gmatcher("test", &[]),
first,
"set_gmatcher must be deterministic"
);
}
}
/// c:356 — `get_gmatcher` is deterministic for stable input.
#[test]
fn get_gmatcher_is_deterministic() {
let _g = crate::test_util::global_state_lock();
let first = get_gmatcher("test", &[]);
for _ in 0..3 {
assert_eq!(
get_gmatcher("test", &[]),
first,
"get_gmatcher must be deterministic"
);
}
}
/// c:1303 — `bin_compctl` no-args returns i32 (compile-time type pin).
#[test]
fn bin_compctl_returns_i32_type() {
let _g = crate::test_util::global_state_lock();
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let _: i32 = bin_compctl("compctl", &[], &ops, 0);
}
/// c:1435 — `bin_compcall` no-args returns i32.
#[test]
fn bin_compcall_returns_i32_type() {
let _g = crate::test_util::global_state_lock();
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let _: i32 = bin_compcall("compcall", &[], &ops, 0);
}
/// c:1573 — `cccleanuphookfn(std::ptr::null_mut(), std::ptr::null_mut())` is deterministic.
#[test]
fn cccleanuphookfn_is_deterministic() {
let first = cccleanuphookfn(std::ptr::null_mut(), std::ptr::null_mut());
for _ in 0..3 {
assert_eq!(
cccleanuphookfn(std::ptr::null_mut(), std::ptr::null_mut()),
first,
"cccleanuphookfn must be deterministic"
);
}
}
/// c:381 — `print_gmatcher` for various indices is safe (no panic).
#[test]
fn print_gmatcher_full_index_range_safe() {
let _g = crate::test_util::global_state_lock();
for ac in [-1i32, 0, 1, 10, 100, i32::MAX, i32::MIN] {
print_gmatcher(ac);
}
}
/// c:1052 — `compctl_name_pat(empty)` returns (false, empty).
#[test]
fn compctl_name_pat_empty_returns_false_empty() {
let (is_pat, name) = compctl_name_pat("");
assert!(!is_pat, "empty input not a pattern");
assert_eq!(name, "", "empty input → empty name");
}
// ═══════════════════════════════════════════════════════════════════
// Additional C-parity tests for Src/Zle/compctl.c
// c:92 createcompctltable / c:104 freecompctlp / c:320 set_gmatcher /
// c:356 get_gmatcher / c:1052 compctl_name_pat / c:1080 delpatcomp /
// c:1303 bin_compctl / c:1435 bin_compcall / c:1485 ccmakehookfn
// ═══════════════════════════════════════════════════════════════════
/// c:92 — `createcompctltable` is idempotent (alt 10-call).
#[test]
fn createcompctltable_idempotent_10_call_alt() {
let _g = crate::test_util::global_state_lock();
for _ in 0..10 {
createcompctltable();
}
}
/// c:104 — `freecompctlp(empty)` safe.
#[test]
fn freecompctlp_empty_no_panic() {
let _g = crate::test_util::global_state_lock();
freecompctlp("");
}
/// c:104 — `freecompctlp("__never__")` for unknown name safe (alt pin).
#[test]
fn freecompctlp_unknown_name_no_panic_alt() {
let _g = crate::test_util::global_state_lock();
freecompctlp("__never_real_compctl_xyz__");
}
/// c:320 — `set_gmatcher` returns i32 (compile-time pin, alt).
#[test]
fn set_gmatcher_returns_i32_type_alt() {
let _g = crate::test_util::global_state_lock();
let _: i32 = set_gmatcher("test", &[]);
}
/// c:356 — `get_gmatcher` returns i32 (compile-time pin, alt).
#[test]
fn get_gmatcher_returns_i32_type_alt() {
let _g = crate::test_util::global_state_lock();
let _: i32 = get_gmatcher("test", &[]);
}
/// c:1052 — `compctl_name_pat` returns (bool, String) tuple.
#[test]
fn compctl_name_pat_returns_bool_string_tuple_type() {
let _: (bool, String) = compctl_name_pat("");
}
/// c:1052 — `compctl_name_pat` is deterministic.
#[test]
fn compctl_name_pat_deterministic() {
for s in ["", "abc", "*", "(pattern)", "name"] {
let a = compctl_name_pat(s);
let b = compctl_name_pat(s);
assert_eq!(a, b, "compctl_name_pat({:?}) must be pure", s);
}
}
/// c:1080 — `delpatcomp("")` empty pattern safe.
#[test]
fn delpatcomp_empty_no_panic() {
let _g = crate::test_util::global_state_lock();
delpatcomp("");
}
/// c:1080 — `delpatcomp("__never__")` unknown pattern safe.
#[test]
fn delpatcomp_unknown_no_panic() {
let _g = crate::test_util::global_state_lock();
delpatcomp("__never_real_pattern_xyz__");
}
/// c:1303 — `bin_compctl` non-negative exit code.
#[test]
fn bin_compctl_exit_code_non_negative() {
let _g = crate::test_util::global_state_lock();
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let r = bin_compctl("compctl", &[], &ops, 0);
assert!(r >= 0, "bin_compctl exit code must be ≥ 0, got {}", r);
}
/// c:1435 — `bin_compcall` non-negative exit code.
#[test]
fn bin_compcall_exit_code_non_negative() {
let _g = crate::test_util::global_state_lock();
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let r = bin_compcall("compcall", &[], &ops, 0);
assert!(r >= 0, "bin_compcall exit code must be ≥ 0, got {}", r);
}
// ═══════════════════════════════════════════════════════════════════
// gen_matches_files (Src/Zle/compctl.c:2161) against a temp directory.
// ═══════════════════════════════════════════════════════════════════
/// Builds `<tmp>/{alpha.txt,alpine.rs,beta.txt,.hidden,runme(+x),adir/inner,bdir/,.hdir/}`
/// and returns the directory path with a trailing slash (the `prpre`).
fn files_fixture(tag: &str) -> String {
use std::os::unix::fs::PermissionsExt;
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let root = std::env::temp_dir().join(format!(
"zshrs_gmf_{}_{}_{}",
tag,
std::process::id(),
nanos
));
std::fs::create_dir_all(root.join("adir")).unwrap();
std::fs::create_dir_all(root.join("bdir")).unwrap();
std::fs::create_dir_all(root.join(".hdir")).unwrap();
for f in ["alpha.txt", "alpine.rs", "beta.txt", ".hidden", "runme", "adir/inner"] {
std::fs::write(root.join(f), b"x").unwrap();
}
for f in ["alpha.txt", "alpine.rs", "beta.txt", ".hidden", "adir/inner"] {
std::fs::set_permissions(root.join(f), std::fs::Permissions::from_mode(0o644)).unwrap();
}
std::fs::set_permissions(root.join("runme"), std::fs::Permissions::from_mode(0o755))
.unwrap();
format!("{}/", root.display())
}
/// Runs gen_matches_files(dirs, execs, all) with the given file
/// prefix/suffix and returns the sorted accepted names.
fn run_gen_matches_files(
prpre: &str,
fpre: &str,
fsuf: &str,
psuf: &str,
dirs: i32,
execs: i32,
all: i32,
) -> Vec<String> {
clear_matches();
MATCH_LIST.with(|r| r.borrow_mut().clear());
reset_compctl_statics();
crate::ported::zle::compcore::ispattern.store(0, std::sync::atomic::Ordering::Relaxed);
FPRE.with(|r| *r.borrow_mut() = fpre.to_string());
QFPRE.with(|r| *r.borrow_mut() = fpre.to_string());
FPL.with(|c| c.set(fpre.len() as i32));
FSUF.with(|r| *r.borrow_mut() = fsuf.to_string());
QFSUF.with(|r| *r.borrow_mut() = fsuf.to_string());
FSL.with(|c| c.set(fsuf.len() as i32));
PSUF.with(|r| *r.borrow_mut() = psuf.to_string());
PRPRE.with(|r| *r.borrow_mut() = Some(prpre.to_string()));
gen_matches_files(dirs, execs, all);
PRPRE.with(|r| *r.borrow_mut() = None);
let mut got: Vec<String> = crate::comp_match_handles::matches_arc()
.lock()
.unwrap()
.iter()
.filter_map(|cm| cm.orig.clone())
.collect();
got.sort();
got
}
fn drop_fixture(prpre: &str) {
let _ = std::fs::remove_dir_all(prpre.trim_end_matches('/'));
}
#[test]
fn gen_matches_files_all_filters_by_file_prefix() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = files_fixture("pre");
let got = run_gen_matches_files(&dir, "al", "", "", 0, 0, 1);
drop_fixture(&dir);
assert_eq!(got, vec!["alpha.txt".to_string(), "alpine.rs".to_string()]);
}
#[test]
fn gen_matches_files_all_filters_by_file_suffix() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = files_fixture("suf");
let got = run_gen_matches_files(&dir, "", ".txt", "", 0, 0, 1);
drop_fixture(&dir);
assert_eq!(got, vec!["alpha.txt".to_string(), "beta.txt".to_string()]);
}
#[test]
fn gen_matches_files_dotfiles_only_with_dot_prefix() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = files_fixture("dot");
// c:2204 — `*n != '.' || *fpre == '.' || isset(GLOBDOTS)`.
let hidden_off = run_gen_matches_files(&dir, "", "", "", 0, 0, 1);
let hidden_on = run_gen_matches_files(&dir, ".", "", "", 0, 0, 1);
drop_fixture(&dir);
assert!(
!hidden_off.iter().any(|n| n.starts_with('.')),
"dotfiles listed without a dot prefix: {:?}",
hidden_off
);
assert_eq!(hidden_on, vec![".hdir".to_string(), ".hidden".to_string()]);
}
#[test]
fn gen_matches_files_dirs_only() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = files_fixture("dirs");
// compctl -/ : gen_matches_files(1, 0, 0).
let got = run_gen_matches_files(&dir, "", "", "", 1, 0, 0);
drop_fixture(&dir);
assert_eq!(got, vec!["adir".to_string(), "bdir".to_string()]);
}
#[test]
fn gen_matches_files_execs_only_skips_plain_files_and_dirs() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = files_fixture("exe");
let got = run_gen_matches_files(&dir, "", "", "", 0, 1, 0);
drop_fixture(&dir);
assert_eq!(got, vec!["runme".to_string()]);
}
#[test]
fn gen_matches_files_path_suffix_keeps_only_dirs_containing_it() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = files_fixture("psuf");
// c:2171 — a path suffix forces dirs=1, all=execs=0 and each
// directory is kept only if `<dir>/<name><psuf>` is accessible.
let got = run_gen_matches_files(&dir, "", "", "/inner", 0, 0, 1);
drop_fixture(&dir);
assert_eq!(got, vec!["adir".to_string()]);
}
#[test]
fn gen_matches_files_uses_filecomp_pattern_when_set() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = files_fixture("pat");
// c:2207 — with a compiled `filecomp` the name is tested with pattry.
let mut pat = "al*.txt".to_string();
crate::ported::glob::tokenize(&mut pat);
let prog = patcompile(&pat, 0, None);
assert!(prog.is_some(), "al*.txt must compile");
clear_matches();
reset_compctl_statics();
FILECOMP.with(|r| *r.borrow_mut() = prog);
PRPRE.with(|r| *r.borrow_mut() = Some(dir.clone()));
gen_matches_files(0, 0, 1);
PRPRE.with(|r| *r.borrow_mut() = None);
FILECOMP.with(|r| *r.borrow_mut() = None);
let got: Vec<String> = crate::comp_match_handles::matches_arc()
.lock()
.unwrap()
.iter()
.filter_map(|cm| cm.orig.clone())
.collect();
drop_fixture(&dir);
assert_eq!(got, vec!["alpha.txt".to_string()]);
}
#[test]
fn gen_matches_files_restores_addwhat_and_nullglob() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = files_fixture("rst");
ADDWHAT.with(|c| c.set(-6));
let ng = crate::ported::zsh_h::isset(crate::ported::zsh_h::NULLGLOB);
let _ = run_gen_matches_files(&dir, "", "", "", 1, 0, 0);
drop_fixture(&dir);
// c:2287-2288 — NULLGLOB and addwhat are put back.
assert_eq!(ADDWHAT.with(|c| c.get()), -6);
assert_eq!(crate::ported::zsh_h::isset(crate::ported::zsh_h::NULLGLOB), ng);
}
#[test]
fn compctl_name_pat_tokenizes_pattern_result() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
// c:1279-1284 — the pattern case returns the tokenized copy, so the
// glob metacharacter is the Star token rather than a literal `*`.
let (is_pat, out) = compctl_name_pat("ls*");
assert!(is_pat);
assert_eq!(out, format!("ls{}", crate::ported::zsh_h::Star));
}
// ---------------------------------------------------------------
// compctl -x: grammar, listing and evaluation
// ---------------------------------------------------------------
fn sv(a: &[&str]) -> Vec<String> {
a.iter().map(|s| s.to_string()).collect()
}
/// Parse `compctl ARGS` the way bin_compctl does and return the
/// root compctl plus the words left over (the command names).
fn parse_spec(args: &[&str]) -> (i32, Compctl, Vec<String>) {
CCLIST.with(|c| c.set(0));
let mut argv = sv(args);
let mut cc = Compctl::default();
let r = get_compctl("compctl", &mut argv, &mut cc, true, false, 0);
(r, cc, argv)
}
fn test_ops() -> crate::ported::zsh_h::options {
crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
}
}
/// What `compctl ARGS` prints, captured from fd 1.
fn compctl_output(args: &[&str]) -> String {
let ops = test_ops();
let argv = sv(args);
crate::compsys::ported::shared::capture_builtin_stdout(false, || {
bin_compctl("compctl", &argv, &ops, 0);
})
}
#[test]
fn get_compctl_x_links_one_compctl_per_condition() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let (r, cc, rest) = parse_spec(&[
"-f", "-x", "s[-]", "-/", "-", "p[1,2]", "-g", "*.c", "--", "cmd",
]);
assert_eq!(r, 0);
assert_eq!(rest, sv(&["cmd"]));
assert_ne!(cc.mask & CC_FILES, 0);
let n1 = cc.ext.clone().expect("first -x compctl");
let c1 = n1.cond.as_deref().unwrap();
assert_eq!(c1.typ, CCT_CURSUF);
assert_eq!(c1.n, 1);
assert!(matches!(&c1.u, CompcondData::S { s, .. } if s == &sv(&["-"])));
assert_ne!(n1.mask & CC_DIRS, 0);
let n2 = n1.next.clone().expect("second -x compctl");
let c2 = n2.cond.as_deref().unwrap();
assert_eq!(c2.typ, CCT_POS);
assert!(matches!(&c2.u, CompcondData::R { a, b } if a == &vec![1] && b == &vec![2]));
assert_eq!(n2.glob.as_deref(), Some("*.c"));
assert!(n2.next.is_none());
}
#[test]
fn get_xcompctl_and_or_structure_and_multiple_brackets() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// `a b , c`: a AND b, OR c.
let (r, cc, _) = parse_spec(&["-x", "c[-1,-f] w[1,foo] , C[0,x*]", "-f", "--", "cmd"]);
assert_eq!(r, 0);
let n = cc.ext.clone().unwrap();
let a = n.cond.as_deref().unwrap();
assert_eq!(a.typ, CCT_CURSTR);
assert!(matches!(&a.u, CompcondData::S { p, s } if p == &vec![-1] && s == &sv(&["-f"])));
let b = a.and.as_deref().unwrap();
assert_eq!(b.typ, CCT_WORDSTR);
assert!(b.and.is_none());
assert!(a.or.is_some());
let c = a.or.as_deref().unwrap();
assert_eq!(c.typ, CCT_CURPAT);
assert!(matches!(&c.u, CompcondData::S { p, s } if p == &vec![0] && s == &sv(&["x*"])));
assert!(c.and.is_none() && c.or.is_none());
// Several [..] groups; commas and nested brackets inside a string.
let (r, cc, _) = parse_spec(&["-x", "s[a,b][c[d]e] r[x,y][z]", "-f", "--", "cmd"]);
assert_eq!(r, 0);
let n = cc.ext.clone().unwrap();
let s = n.cond.as_deref().unwrap();
assert_eq!((s.typ, s.n), (CCT_CURSUF, 2));
assert!(matches!(&s.u, CompcondData::S { s, .. } if s == &sv(&["a,b", "c[d]e"])));
let rr = s.and.as_deref().unwrap();
assert_eq!((rr.typ, rr.n), (CCT_RANGESTR, 2));
assert!(matches!(&rr.u,
CompcondData::L { a, b } if a == &sv(&["x", "z"]) && b == &vec![Some("y".to_string()), None]));
}
/// c:1098-1101 — `r[a,]` stores an empty (non-NULL) upper bound; `r[a]` stores NULL.
#[test]
fn get_xcompctl_range_empty_upper_bound_is_not_null() {
let (r, cc, _) = parse_spec(&["-x", "r[a,][b]", "-f", "--", "cmd"]);
assert_eq!(r, 0);
let n = cc.ext.clone().unwrap();
let s = n.cond.as_deref().unwrap();
assert!(matches!(&s.u,
CompcondData::L { b, .. } if b == &vec![Some(String::new()), None]));
}
#[test]
fn get_xcompctl_rejects_malformed_conditions() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// unknown condition code
assert_eq!(parse_spec(&["-x", "z[1]", "-f", "--", "cmd"]).0, 1);
// code not followed by `[`
assert_eq!(parse_spec(&["-x", "p1", "-f", "--", "cmd"]).0, 1);
// unterminated bracket
assert_eq!(parse_spec(&["-x", "p[1", "-f", "--", "cmd"]).0, 1);
// `n[..]` needs `index,string`
assert_eq!(parse_spec(&["-x", "n[1]", "-f", "--", "cmd"]).0, 1);
// no condition word after -x
assert_eq!(parse_spec(&["-x"]).0, 1);
// flags with no `--` and no command names
assert_eq!(parse_spec(&["-x", "p[1]", "-f"]).0, 1);
// -x is not recursive
assert_eq!(
parse_spec(&["-x", "p[1]", "-x", "p[2]", "-f", "--", "--", "cmd"]).0,
1
);
}
#[test]
fn get_compctl_xor_chain_and_default_xor() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
let (r, cc, rest) = parse_spec(&["-f", "+", "-g", "*.c", "cmd"]);
assert_eq!(r, 0);
assert_eq!(rest, sv(&["cmd"]));
assert_ne!(cc.mask & CC_FILES, 0);
let x = cc.xor.clone().expect("xor'd compctl");
assert_eq!(x.glob.as_deref(), Some("*.c"));
assert!(x.xor.is_none());
// `+` with no flags after it: the xor is cc_default.
let (r, cc, rest) = parse_spec(&["-f", "+", "cmd"]);
assert_eq!(r, 0);
assert_eq!(rest, sv(&["cmd"]));
let x = cc.xor.clone().unwrap();
assert!(Arc::ptr_eq(&x, CC_DEFAULT.lock().unwrap().as_ref().unwrap()));
}
#[test]
fn compctl_l_prints_extended_spec_round_trip() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
let ops = test_ops();
let spec = sv(&[
"-x", "s[-]", "-f", "-", "c[-1,-f]", "-/", "--", "xcmd",
]);
assert_eq!(bin_compctl("compctl", &spec, &ops, 0), 0);
assert_eq!(
compctl_output(&["-L", "xcmd"]),
"compctl -x 's[-]' -f - 'c[-1,-f]' -/ -- xcmd\n"
);
// Feeding the printed form back in prints the same thing.
let again = sv(&[
"-x", "s[-]", "-f", "-", "c[-1,-f]", "-/", "--", "ycmd",
]);
assert_eq!(bin_compctl("compctl", &again, &ops, 0), 0);
assert_eq!(
compctl_output(&["-L", "ycmd"]).replace("ycmd", "xcmd"),
compctl_output(&["-L", "xcmd"])
);
CCLIST.with(|c| c.set(0));
}
#[test]
fn compctl_l_prints_all_condition_kinds() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
let ops = test_ops();
let spec = sv(&[
"-x",
"p[2] , m[3,4] q[s] S[x] , n[-1,=] N[2,:] , C[0,*.c] W[1,a*] w[2,b] , r[a,b] R[c*,d*]",
"-g",
"*.h",
"--",
"kcmd",
]);
assert_eq!(bin_compctl("compctl", &spec, &ops, 0), 0);
assert_eq!(
compctl_output(&["-L", "kcmd"]),
"compctl -x 'p[2,2] , m[3,4] q[s] S[x] , n[-1,=] N[2,:] , C[0,*.c] W[1,a*] w[2,b] , r[a,b] R[c*,d*]' -g '*.h' -- kcmd\n"
);
CCLIST.with(|c| c.set(0));
}
#[test]
fn compctl_d_with_extended_condition_sets_and_lists_cc_default() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
let ops = test_ops();
// `-D` ends the argument list without command names, with or
// without the closing `--`.
for spec in [
sv(&["-D", "-f", "-x", "p[1]", "-/"]),
sv(&["-D", "-f", "-x", "p[1]", "-/", "--"]),
] {
setup_();
assert_eq!(bin_compctl("compctl", &spec, &ops, 0), 0);
let def = CC_DEFAULT.lock().unwrap().clone().unwrap();
assert_eq!(def.refc, 10000);
assert_ne!(def.mask & CC_FILES, 0);
let ext = def.ext.clone().expect("cc_default.ext");
assert_eq!(ext.cond.as_deref().unwrap().typ, CCT_POS);
assert_ne!(ext.mask & CC_DIRS, 0);
assert_eq!(
compctl_output(&["-L", "-D"]),
"compctl -D -f -x 'p[1,1]' -/ --\n"
);
}
// -C and -T go to their own statics.
setup_();
assert_eq!(
bin_compctl("compctl", &sv(&["-C", "-x", "p[1]", "-c"]), &ops, 0),
0
);
assert!(CC_COMPOS.lock().unwrap().as_ref().unwrap().ext.is_some());
assert!(CC_DEFAULT.lock().unwrap().as_ref().unwrap().ext.is_none());
assert_eq!(
bin_compctl("compctl", &sv(&["-T", "-x", "p[1]", "-f"]), &ops, 0),
0
);
assert!(CC_FIRST.lock().unwrap().as_ref().unwrap().ext.is_some());
assert_eq!(
compctl_output(&["-L", "-T"]),
"compctl -T -x 'p[1,1]' -f --\n"
);
// -D and -T together cannot be set in one command.
assert_eq!(
bin_compctl("compctl", &sv(&["-D", "-T", "-f"]), &ops, 0),
1
);
CCLIST.with(|c| c.set(0));
setup_();
}
#[test]
fn compctl_l_prints_xor_chain() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
setup_();
let ops = test_ops();
assert_eq!(
bin_compctl("compctl", &sv(&["-f", "+", "-g", "*.c", "zcmd"]), &ops, 0),
0
);
assert_eq!(
compctl_output(&["-L", "zcmd"]),
"compctl -f + -g '*.c' zcmd\n"
);
assert_eq!(
bin_compctl("compctl", &sv(&["-f", "+", "zdef"]), &ops, 0),
0
);
assert_eq!(compctl_output(&["-L", "zdef"]), "compctl -f + zdef\n");
CCLIST.with(|c| c.set(0));
}
/// Run the compctl built from `args` against a synthetic line and
/// return the candidates it produced. `-s WORD` flags are the
/// observable: each spec contributes its own WORD.
fn run_extended(args: &[&str], words: &[&str], pos: i32, os: &str) -> Vec<String> {
let (r, cc, _) = parse_spec(args);
assert_eq!(r, 0, "spec {:?} failed to parse", args);
*CLWORDS.lock().unwrap() = sv(words);
*CLWNUM.lock().unwrap() = words.len() as i32;
*CLWPOS.lock().unwrap() = pos;
// the cursor sits at the end of the cursor word
crate::ported::zle::compcore::OFFS.store(os.len() as i32, Ordering::Relaxed);
CCONT.with(|c| c.set(0));
MATCH_LIST.with(|r| r.borrow_mut().clear());
let mut tos = os.to_string(); // the cursor word arrives tokenized
crate::ported::glob::tokenize(&mut tos);
makecomplistext(&Arc::new(cc), &tos, false);
MATCH_LIST.with(|r| r.borrow().clone())
}
#[test]
fn makecomplistext_position_condition() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let spec = ["-s", "dflt", "-x", "p[1]", "-s", "one", "-", "p[2,3]", "-s", "two", "--", "c"];
assert_eq!(run_extended(&spec, &["c", ""], 1, ""), sv(&["one"]));
assert_eq!(run_extended(&spec, &["c", "a", ""], 2, ""), sv(&["two"]));
assert_eq!(run_extended(&spec, &["c", "a", "b", ""], 3, ""), sv(&["two"]));
assert_eq!(
run_extended(&spec, &["c", "a", "b", "d", ""], 4, ""),
sv(&["dflt"])
);
// Negative bounds count from the end: p[-1] is the last word.
let neg = ["-s", "dflt", "-x", "p[-1]", "-s", "last", "--", "c"];
assert_eq!(run_extended(&neg, &["c", "a", ""], 2, ""), sv(&["last"]));
assert_eq!(run_extended(&neg, &["c", "a", ""], 1, ""), sv(&["dflt"]));
}
#[test]
fn makecomplistext_word_conditions() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// c[-1,-f]: the word before the cursor word is `-f`.
let c = ["-s", "dflt", "-x", "c[-1,-f]", "-s", "hit", "--", "c"];
assert_eq!(run_extended(&c, &["c", "-f", ""], 2, ""), sv(&["hit"]));
assert_eq!(run_extended(&c, &["c", "-g", ""], 2, ""), sv(&["dflt"]));
// w[1,foo]: the absolute word 1 is `foo`.
let w = ["-s", "dflt", "-x", "w[1,foo]", "-s", "hit", "--", "c"];
assert_eq!(run_extended(&w, &["c", "foo", ""], 2, ""), sv(&["hit"]));
assert_eq!(run_extended(&w, &["c", "bar", ""], 2, ""), sv(&["dflt"]));
// m[3]: exactly three words on the line.
let m = ["-s", "dflt", "-x", "m[3]", "-s", "hit", "--", "c"];
assert_eq!(run_extended(&m, &["c", "a", ""], 2, ""), sv(&["hit"]));
assert_eq!(run_extended(&m, &["c", ""], 1, ""), sv(&["dflt"]));
}
#[test]
fn makecomplistext_pattern_conditions() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// C[-1,*.c]: previous word matches the glob.
let cp = ["-s", "dflt", "-x", "C[-1,*.c]", "-s", "hit", "--", "c"];
assert_eq!(run_extended(&cp, &["c", "x.c", ""], 2, ""), sv(&["hit"]));
assert_eq!(run_extended(&cp, &["c", "x.h", ""], 2, ""), sv(&["dflt"]));
// W[1,f*]: absolute word 1 matches the glob.
let wp = ["-s", "dflt", "-x", "W[1,f*]", "-s", "hit", "--", "c"];
assert_eq!(run_extended(&wp, &["c", "foo", ""], 2, ""), sv(&["hit"]));
assert_eq!(run_extended(&wp, &["c", "boo", ""], 2, ""), sv(&["dflt"]));
}
#[test]
fn makecomplistext_or_and_chains() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// `p[1] c[-1,c]` needs both; `, p[3]` is the alternative.
let spec = ["-s", "dflt", "-x", "p[1] c[-1,c] , p[3]", "-s", "hit", "--", "c"];
assert_eq!(run_extended(&spec, &["c", ""], 1, ""), sv(&["hit"]));
assert_eq!(run_extended(&spec, &["c", "a", "b", ""], 3, ""), sv(&["hit"]));
// pos 2 satisfies neither arm
assert_eq!(run_extended(&spec, &["c", "a", ""], 2, ""), sv(&["dflt"]));
// pos 1 but the previous word is not `c`
assert_eq!(run_extended(&spec, &["x", ""], 1, ""), sv(&["dflt"]));
}
#[test]
fn makecomplistext_range_condition_bounds() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let spec = ["-s", "dflt", "-x", "r[-f,--]", "-s", "hit", "--", "c"];
// `-f` seen, no closing `--` yet: the cursor is inside the range.
assert_eq!(run_extended(&spec, &["c", "-f", "x", ""], 3, ""), sv(&["hit"]));
// `-f` ... `--` already closed before the cursor.
assert_eq!(
run_extended(&spec, &["c", "-f", "x", "--", ""], 4, ""),
sv(&["dflt"])
);
// no `-f` at all
assert_eq!(run_extended(&spec, &["c", "x", ""], 2, ""), sv(&["dflt"]));
}
#[test]
fn makecomplistext_quote_condition_follows_instring() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let spec = ["-s", "dflt", "-x", "q[s]", "-s", "hit", "--", "c"];
let old = INSTRING.load(Ordering::Relaxed);
INSTRING.store(QT_SINGLE, Ordering::Relaxed);
let in_single = run_extended(&spec, &["c", ""], 1, "");
INSTRING.store(QT_DOUBLE, Ordering::Relaxed);
let in_double = run_extended(&spec, &["c", ""], 1, "");
INSTRING.store(old, Ordering::Relaxed);
assert_eq!(in_single, sv(&["hit"]));
assert_eq!(in_double, sv(&["dflt"]));
}
#[test]
fn makecomplistext_prefix_suffix_conditions() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// s[-] and S[-]: the current word starts with `-`. `s` also strips
// the prefix it matched (compadd), `S` does not.
let s = ["-s", "dflt", "-x", "s[-]", "-s", "hit", "--", "c"];
assert_eq!(run_extended(&s, &["c", "-"], 1, "-"), sv(&["hit"]));
assert_eq!(run_extended(&s, &["c", "d"], 1, "d"), sv(&["dflt"]));
let upper = ["-s", "xdflt", "-x", "S[d]", "-s", "dhit", "--", "c"];
assert_eq!(run_extended(&upper, &["c", "d"], 1, "d"), sv(&["dhit"]));
assert_eq!(run_extended(&upper, &["c", "x"], 1, "x"), sv(&["xdflt"]));
}
#[test]
fn makecomplistext_cursub_condition() {
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// n[1,=]: the cursor word contains an `=`; the text after it is
// what gets completed.
let spec = ["-s", "dflt", "-x", "n[1,=]", "-s", "hit", "--", "c"];
assert_eq!(run_extended(&spec, &["c", "a="], 1, "a="), sv(&["hit"]));
assert_eq!(run_extended(&spec, &["c", "d"], 1, "d"), sv(&["dflt"]));
}
// ---------------------------------------------------------------
// addmatch: the HashNode predicates of c:1991-2014
// ---------------------------------------------------------------
/// Number of matches registered by `addmatch(name, node)` under `addwhat`.
fn added_by(addwhat: i32, name: &str, node: Option<&AddmatchNode>) -> i64 {
clear_matches();
reset_compctl_statics();
let before = mnum_now();
ADDWHAT.with(|c| c.set(addwhat));
addmatch(name, node);
mnum_now() as i64 - before as i64
}
fn hash_node(nam: &str, flags: i32) -> AddmatchNode {
AddmatchNode::Hash(crate::ported::zsh_h::hashnode {
next: None,
nam: nam.to_string(),
flags,
})
}
fn param_node(nam: &str, flags: u32, level: i32, val: Option<&str>) -> AddmatchNode {
AddmatchNode::Param(crate::ported::zsh_h::param {
node: crate::ported::zsh_h::hashnode {
next: None,
nam: nam.to_string(),
flags: flags as i32,
},
u_str: val.map(String::from),
level,
..Default::default()
})
}
#[test]
fn addmatch_command_names_skip_disabled_nodes() {
use crate::ported::zsh_h::DISABLED;
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// c:1991 — addwhat -3 requires the node not be DISABLED.
assert_eq!(added_by(-3, "ls", Some(&hash_node("ls", 0))), 1);
assert_eq!(added_by(-3, "ls", Some(&hash_node("ls", DISABLED))), 0);
}
#[test]
fn addmatch_enable_disable_selection_for_command_tables() {
use crate::ported::zsh_h::DISABLED;
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let ex = CC_SHFUNCS as i32 | CC_EXCMDS as i32; // c:2012-2013
let dis = CC_SHFUNCS as i32 | CC_DISCMDS as i32;
assert_eq!(added_by(ex, "f", Some(&hash_node("f", 0))), 1);
assert_eq!(added_by(ex, "f", Some(&hash_node("f", DISABLED))), 0);
assert_eq!(added_by(dis, "f", Some(&hash_node("f", DISABLED))), 1);
assert_eq!(added_by(dis, "f", Some(&hash_node("f", 0))), 0);
// Aliases: CC_ALREG excludes global aliases, CC_ALGLOB wants them.
let reg = CC_ALREG as i32 | CC_EXCMDS as i32;
let glob = CC_ALGLOB as i32 | CC_EXCMDS as i32;
let galias = crate::ported::zsh_h::ALIAS_GLOBAL;
assert_eq!(added_by(reg, "a", Some(&hash_node("a", 0))), 1);
assert_eq!(added_by(reg, "a", Some(&hash_node("a", galias))), 0);
assert_eq!(added_by(glob, "a", Some(&hash_node("a", galias))), 1);
assert_eq!(added_by(glob, "a", Some(&hash_node("a", 0))), 0);
// Bindings: only the DISABLED bit matters (c:2014).
assert_eq!(added_by(CC_BINDINGS as i32, "w", Some(&hash_node("w", 0))), 1);
assert_eq!(
added_by(CC_BINDINGS as i32, "w", Some(&hash_node("w", DISABLED))),
0
);
// A command-table mask alone (no CC_EXCMDS / CC_DISCMDS) adds nothing.
assert_eq!(added_by(CC_SHFUNCS as i32, "f", Some(&hash_node("f", 0))), 0);
}
#[test]
fn addmatch_parameter_flavours_check_flags_unset_and_level() {
use crate::ported::zsh_h::{PM_ARRAY, PM_EXPORTED, PM_INTEGER, PM_READONLY, PM_UNSET};
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// c:1995 — addwhat -9: set, top-level parameters.
assert_eq!(added_by(-9, "p", Some(¶m_node("p", 0, 0, Some("v")))), 1);
assert_eq!(
added_by(-9, "p", Some(¶m_node("p", PM_UNSET, 0, None))),
0
);
assert_eq!(added_by(-9, "p", Some(¶m_node("p", 0, 1, Some("v")))), 0);
// c:1998-2004 — flavour masks need the matching PM_* flag.
let arrays = CC_ARRAYS as i32;
assert_eq!(added_by(arrays, "a", Some(¶m_node("a", PM_ARRAY, 0, None))), 1);
assert_eq!(added_by(arrays, "a", Some(¶m_node("a", 0, 0, None))), 0);
assert_eq!(
added_by(arrays, "a", Some(¶m_node("a", PM_ARRAY, 1, None))),
0,
"pm->level must be 0"
);
let ints = CC_INTVARS as i32;
assert_eq!(added_by(ints, "i", Some(¶m_node("i", PM_INTEGER, 0, None))), 1);
let envs = CC_ENVVARS as i32;
assert_eq!(added_by(envs, "e", Some(¶m_node("e", PM_EXPORTED, 0, None))), 1);
assert_eq!(added_by(envs, "e", Some(¶m_node("e", 0, 0, None))), 0);
let ros = CC_READONLYS as i32;
assert_eq!(added_by(ros, "r", Some(¶m_node("r", PM_READONLY, 0, None))), 1);
// CC_PARAMS is the non-exported ones.
let params = CC_PARAMS as i32;
assert_eq!(added_by(params, "x", Some(¶m_node("x", 0, 0, None))), 1);
assert_eq!(
added_by(params, "x", Some(¶m_node("x", PM_EXPORTED, 0, None))),
0
);
// An unset parameter is never offered, whatever its flavour.
assert_eq!(
added_by(arrays, "a", Some(¶m_node("a", PM_ARRAY | PM_UNSET, 0, None))),
0
);
}
#[test]
fn addmatch_cdable_param_needs_scalar_with_absolute_path_value() {
use crate::ported::zsh_h::PM_INTEGER;
let _g = crate::test_util::global_state_lock();
let _g = zle_test_setup();
let _g = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// c:1992-1994 — addwhat -4: top-level scalar whose value starts with `/`.
assert_eq!(added_by(-4, "d", Some(¶m_node("d", 0, 0, Some("/usr")))), 1);
assert_eq!(added_by(-4, "d", Some(¶m_node("d", 0, 0, Some("usr")))), 0);
assert_eq!(added_by(-4, "d", Some(¶m_node("d", 0, 0, None))), 0);
assert_eq!(added_by(-4, "d", Some(¶m_node("d", 0, 1, Some("/usr")))), 0);
assert_eq!(
added_by(-4, "d", Some(¶m_node("d", PM_INTEGER, 0, Some("/usr")))),
0
);
}
}