zshrs 0.12.1

The first compiled Unix shell — bytecode VM, worker pool, AOP intercept, Rkyv caching
Documentation
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
//! Module system for zshrs
//!
//! Port from zsh/Src/module.c (3,646 lines)
//!
//! Hash of modules                                                          // c:46
//! The list of hook functions defined.                                      // c:840
//! List of math functions.                                                  // c:1255
//!
//! In C, module.c provides dynamic loading of .so modules at runtime
//! via dlopen/dlsym. In Rust, all modules are statically compiled into
//! the binary — there's no dynamic loading. This module provides the
//! registration, lookup, and management API that the rest of the shell
//! uses to interact with module features (builtins, conditions, parameters,
//! hooks, and math functions).

use crate::ported::builtin::createbuiltintable;
use crate::ported::hist::casemodify;
use crate::ported::mem::ztrdup;
use crate::ported::params::{createparam, createspecialhash, paramtab, unsetparam_pm};
use crate::ported::signals::unqueue_signals;
use crate::ported::utils::{zwarn, zwarnnam};
use crate::ported::zsh_h::{
    builtin, conddef, funcwrap, hookdef, linklist, linknode, mathfunc, options, paramdef, Hookfn,
    Param, BINF_AUTOALL, CASMOD_LOWER, CASMOD_UPPER, CONDF_AUTOALL, HOOKF_ALL, MFF_USERFUNC,
    linkedmod, MOD_ALIAS, MOD_BUSY, MOD_INIT_B, MOD_INIT_S, MOD_LINKED, MOD_SETUP, MOD_UNLOAD,
    OPT_ARG_SAFE, OPT_ISSET,
    PM_ARRAY, PM_AUTOALL, PM_AUTOLOAD, PM_EFLOAT, PM_FFLOAT, PM_HASHED, PM_INTEGER, PM_NAMEREF,
    PM_READONLY, PM_REMOVABLE, PM_SCALAR, PM_TIED, PM_TYPE, PRINT_LIST,
};
pub use crate::ported::zsh_h::{BINF_ADDED, CONDF_ADDED, CONDF_INFIX, MFF_ADDED};
use crate::ported::zsh_h::hashnode;
use crate::zsh_h::module;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::Mutex;

/// Free module node (from module.c freemodulenode)
/// Free a module table entry.
/// Port of `freemodulenode(HashNode hn)` from Src/module.c:119 — Rust's
/// `Drop` handles the per-field free; this exists for API
/// parity with C callers.
pub fn freemodulenode(hn: module) {
    // Rust Drop handles this
}

/// `PRINTMOD_LIST` from `Src/module.c:136`. Long-form (`zmodload -L`)
/// output.
pub const PRINTMOD_LIST: i32 = 0x0001; // c:136
/// `PRINTMOD_EXIST` from `Src/module.c:138`. Print only when the
/// module exists.
pub const PRINTMOD_EXIST: i32 = 0x0002; // c:138
/// `PRINTMOD_ALIAS` from `Src/module.c:140`. Resolve aliases when
/// emitting under `PRINTMOD_EXIST`.
pub const PRINTMOD_ALIAS: i32 = 0x0004; // c:140
/// `PRINTMOD_DEPS` from `Src/module.c:142`. Emit the dependency list
/// (`zmodload -d`).
pub const PRINTMOD_DEPS: i32 = 0x0008; // c:142
/// `PRINTMOD_FEATURES` from `Src/module.c:144`. Emit feature flags
/// (`zmodload -F`).
pub const PRINTMOD_FEATURES: i32 = 0x0010; // c:144
/// `PRINTMOD_LISTALL` from `Src/module.c:146`. Include disabled
/// features (`zmodload -lL`).
pub const PRINTMOD_LISTALL: i32 = 0x0020; // c:146
/// `PRINTMOD_AUTO` from `Src/module.c:148`. Emit autoloads
/// (`zmodload -a`).
pub const PRINTMOD_AUTO: i32 = 0x0040; // c:148

/// Direct port of `void printmodulenode(HashNode hn, int flags)` from
/// `Src/module.c:154`.
///
/// Formats one module entry for the various `zmodload -L`/`-a`/
/// `-d`/`-F` listings. C writes to stdout; Rust returns the
/// formatted string so call sites can route to the right output fd
/// without depending on stdio. Dispatches on `flags`:
///
///   - `PRINTMOD_DEPS`: emit dep list (`zmodload -d MOD: dep1 dep2`
///     under PRINTMOD_LIST, else `MOD: dep1 dep2`) — c:163-194
///   - `PRINTMOD_EXIST`: emit just the module name when loaded
///     (resolving alias when PRINTMOD_ALIAS set) — c:195-201
///   - alias module: under PRINTMOD_LIST emit
///     `zmodload -A MOD=ALIAS`, else `MOD -> ALIAS` — c:202-217
///   - loaded module: under PRINTMOD_LIST emit `zmodload [-Fa] MOD`,
///     else just the name — c:218-241
pub fn printmodulenode(hn: &str, m: &module, flags: i32) -> String {
    let modname = hn;
    let mut out = String::new();

    // c:163-194 — PRINTMOD_DEPS branch.
    // C body:
    //   if (!m->deps) return;
    //   if (flags & PRINTMOD_LIST) {
    //       printf("zmodload -d ");
    //       if (modname[0] == '-') fputs("-- ", stdout);
    //       quotedzputs(modname, stdout);
    //   } else {
    //       nicezputs(modname, stdout);
    //       putchar(':');
    //   }
    //   for (n = firstnode(m->deps); n; incnode(n)) {
    //       putchar(' ');
    //       if (flags & PRINTMOD_LIST)
    //           quotedzputs((char *) getdata(n), stdout);
    //       else
    //           nicezputs((char *) getdata(n), stdout);
    //   }
    //
    // C uses quotedzputs (round-trippable shell input) under
    // PRINTMOD_LIST so the emitted `zmodload -d MOD DEP1 DEP2` is
    // re-parseable. The default path uses nicezputs (printable form
    // with \M-/^ escapes for control bytes) which is listing-friendly.
    // The two helpers are both already ported in src/ported/utils.rs.
    if flags & PRINTMOD_DEPS != 0 {
        use crate::ported::utils::{nicezputs, quotedzputs};
        let deps = match m.deps.as_ref() {
            Some(d) if !d.is_empty() => d,
            _ => return out, // c:170-171
        };
        if flags & PRINTMOD_LIST != 0 {
            out.push_str("zmodload -d "); // c:174
            if modname.starts_with('-') {
                out.push_str("-- "); // c:176
            }
            out.push_str(&quotedzputs(modname)); // c:177
        } else {
            let mut buf: Vec<u8> = Vec::new();
            let _ = nicezputs(modname, &mut buf); // c:179
            if let Ok(s) = std::str::from_utf8(&buf) {
                out.push_str(s);
            }
            out.push(':'); // c:180
        }
        for dep in deps.iter() {
            out.push(' '); // c:183
            if flags & PRINTMOD_LIST != 0 {
                out.push_str(&quotedzputs(dep)); // c:185
            } else {
                let mut buf: Vec<u8> = Vec::new();
                let _ = nicezputs(dep, &mut buf); // c:187
                if let Ok(s) = std::str::from_utf8(&buf) {
                    out.push_str(s);
                }
            }
        }
        return out;
    }

    // c:189-201 — PRINTMOD_EXIST branch.
    // C body:
    //   if (m->node.flags & MOD_ALIAS) {
    //       if (!(flags & PRINTMOD_ALIAS) ||
    //           !(m = find_module(m->u.alias, FINDMOD_ALIASP, NULL)))
    //           return;
    //   }
    //   if (!m->u.handle || (m->node.flags & MOD_UNLOAD))
    //       return;
    //   nicezputs(modname, stdout);
    //
    // The MOD_ALIAS arm at c:194-198 reassigns m to the alias target
    // before the u.handle check at c:199 — without a table handle we
    // can't chase the alias here; the caller (bin_zmodload_exist /
    // bin_zmodload_alias) is responsible for the chase when feeding
    // us the canonical module. For the local check we read the alias
    // entry's flags, which is the right behaviour when the caller
    // already resolved.
    if flags & PRINTMOD_EXIST != 0 {
        if (m.node.flags & MOD_ALIAS) != 0 {
            if (flags & PRINTMOD_ALIAS) == 0 || m.alias.is_none() {
                // c:195-197 — alias entry + caller didn't pass
                // PRINTMOD_ALIAS, OR the alias is dangling.
                return out;
            }
            // Alias resolves: emit the alias entry's name; the
            // caller chased and decided it counts as "exists".
        }
        // c:199 — `!m->u.handle || MOD_UNLOAD` skip.
        // Static-link analog of `!u.handle` is `!MOD_INIT_B`
        // (boot hasn't run = no loaded handle). The prior port
        // missed this gate, so PRINTMOD_EXIST listed every
        // pre-registered module even when zmodload zsh/files had
        // never fired — diverging from `zsh -fc 'zmodload -e'`
        // which emits only the actually-loaded `zsh/main`.
        let booted = (m.node.flags & MOD_INIT_B) != 0;
        let unloading = (m.node.flags & MOD_UNLOAD) != 0;
        if !booted || unloading {
            return out;
        }
        out.push_str(modname); // c:201 nicezputs(modname)
        return out;
    }

    // c:202-217 — alias module branch.
    // c:202-217 — alias module branch.
    // C body:
    //   if (flags & PRINTMOD_LIST) {
    //       printf("zmodload -A ");
    //       if (modname[0] == '-') fputs("-- ", stdout);
    //       quotedzputs(modname, stdout);
    //       putchar('=');
    //       quotedzputs(m->u.alias, stdout);
    //   } else {
    //       nicezputs(modname, stdout);
    //       fputs(" -> ", stdout);
    //       nicezputs(m->u.alias, stdout);
    //   }
    if m.node.flags & MOD_ALIAS != 0 {
        use crate::ported::utils::{nicezputs, quotedzputs};
        let alias = m.alias.as_deref().unwrap_or("");
        if flags & PRINTMOD_LIST != 0 {
            out.push_str("zmodload -A "); // c:207
            if modname.starts_with('-') {
                out.push_str("-- "); // c:209
            }
            out.push_str(&quotedzputs(modname)); // c:210
            out.push('='); // c:211
            out.push_str(&quotedzputs(alias)); // c:212
        } else {
            let mut buf: Vec<u8> = Vec::new();
            let _ = nicezputs(modname, &mut buf); // c:214
            if let Ok(s) = std::str::from_utf8(&buf) {
                out.push_str(s);
            }
            out.push_str(" -> "); // c:215
            let mut buf2: Vec<u8> = Vec::new();
            let _ = nicezputs(alias, &mut buf2); // c:216
            if let Ok(s) = std::str::from_utf8(&buf2) {
                out.push_str(s);
            }
        }
        return out;
    }

    // c:218-241 — loaded module branch (linked or autoloaded).
    // C check: `m->u.handle || (flags & PRINTMOD_AUTO)` where `u`
    // is a union so `u.handle` is non-NULL whenever EITHER `handle`
    // (dlopen result) or `linked` (statically-linked record) is
    // installed. The union slots are only populated AFTER
    // `load_module` completes (c:2227/2230 set them, c:2244 sets
    // `MOD_INIT_B`). So "boot ran" maps to `MOD_INIT_B` in zshrs.
    // Previous gate `MOD_LINKED && !MOD_UNLOAD` was wrong:
    // `register_builtin_modules` seeds `MOD_LINKED` for every
    // statically-compiled module up front, so plain `zmodload`
    // listed all 32 (#76 in docs/BUGS.md). C zsh shows only the
    // single `zsh/main` entry that `init_bltinmods` actually loads
    // via `load_module("zsh/main", NULL, 0)`.
    let loaded = (m.node.flags & MOD_INIT_B) != 0 && (m.node.flags & MOD_UNLOAD) == 0;
    let _ = MOD_LINKED; // c:Src/module.c:218 — union-based check; flag retained for unload path.
    let auto = flags & PRINTMOD_AUTO != 0;
    if loaded || auto {
        use crate::ported::utils::{nicezputs, quotedzputs};
        if flags & PRINTMOD_LIST != 0 {
            // c:229-237 — `PRINTMOD_AUTO`: skip when no autoloads set.
            // C `firstnode(m->autoloads)` returns the first node — if
            // the linklist is empty the early-return fires.
            if auto {
                let has_autoloads = m
                    .autoloads
                    .as_ref()
                    .map(|al| !al.is_empty())
                    .unwrap_or(false);
                if !has_autoloads {
                    return out; // c:231 return early
                }
            }
            out.push_str("zmodload "); // c:238
            if auto {
                out.push_str("-Fa "); // c:240
            } else if flags & PRINTMOD_FEATURES != 0 {
                out.push_str("-F "); // c:242
            }
            if modname.starts_with('-') {
                out.push_str("-- "); // c:244
            }
            out.push_str(&quotedzputs(modname)); // c:245
                                                 // c:246-251 — PRINTMOD_AUTO: emit each autoload as
                                                 //             ` quotedzputs(al)`.
            if auto {
                if let Some(al_list) = m.autoloads.as_ref() {
                    for al in al_list.iter() {
                        out.push(' '); // c:249
                        out.push_str(&quotedzputs(al)); // c:250
                    }
                }
            }
            // c:252-263 — PRINTMOD_FEATURES list path needs features_module
            // + enables_module which require the modulestab; not
            // dispatched here because printmodulenode has no &table
            // handle. Caller (bin_zmodload_features -l/-L path) does
            // the dispatch directly when PRINTMOD_FEATURES is set.
        } else {
            // c:266 — `else /* -l */ nicezputs(modname, stdout);`
            let mut buf: Vec<u8> = Vec::new();
            let _ = nicezputs(modname, &mut buf);
            if let Ok(s) = std::str::from_utf8(&buf) {
                out.push_str(s);
            }
        }
    }
    out // c:268 putchar('\n') handled by caller's println!
}

/// Create new module table (from module.c newmoduletable)
/// Create an empty module table.
/// Port of `newmoduletable(int size, char const *name)` from Src/module.c:274 — the C
/// source allocates the `modulestab` hash with `createhashtable`.
/// WARNING: param names don't match C — Rust=() vs C=(size, name)
pub fn newmoduletable() -> modulestab {
    modulestab::new()
}

/// Port of `setup_(UNUSED(Module m))` from `Src/module.c:306`.
///
/// C body: `setup_(UNUSED(Module m)) { return 0; }` — the no-op
/// setup hook of the module subsystem itself.
#[allow(unused_variables)]
pub fn setup_(m: *const module) -> i32 {
    // c:306
    0 // c:306
}

/// Port of `features_(UNUSED(Module m), UNUSED(char ***features))` from `Src/module.c:313`.
///
/// C body:
/// ```c
/// features_(UNUSED(Module m), UNUSED(char ***features))
/// {
///     /* There are lots and lots of features, but they're not handled here. */
///     return 1;
/// }
/// ```
#[allow(unused_variables)]
pub fn features_(m: *const module, features: &mut Vec<String>) -> i32 {
    // c:313
    /* There are lots and lots of features, but they're not handled here. */ // c:313-318
    1 // c:319
}

/// Port of `enables_(UNUSED(Module m), UNUSED(int **enables))` from `Src/module.c:324`.
///
/// C body: `enables_(UNUSED(Module m), UNUSED(int **enables)) { return 1; }`
/// — the module subsystem itself doesn't manage feature enables.
#[allow(unused_variables)]
pub fn enables_(m: *const module, enables: &mut Option<Vec<i32>>) -> i32 {
    // c:324
    1 // c:324
}

/// Port of `boot_(UNUSED(Module m))` from `Src/module.c:331`.
///
/// C body: `boot_(UNUSED(Module m)) { return 0; }` — the no-op
/// boot hook of the module subsystem itself.
#[allow(unused_variables)]
pub fn boot_(m: *const module) -> i32 {
    // c:331
    0 // c:331
}

/// Port of `cleanup_(UNUSED(Module m))` from `Src/module.c:338`.
///
/// C body: `cleanup_(UNUSED(Module m)) { return 0; }` — the no-op
/// cleanup hook of the module subsystem itself.
#[allow(unused_variables)]
pub fn cleanup_(m: *const module) -> i32 {
    // c:338
    0 // c:338
}

/// Port of `finish_(UNUSED(Module m))` from `Src/module.c:345`.
///
/// C body: `finish_(UNUSED(Module m)) { return 0; }` —
/// the no-op finish hook for the module subsystem itself.
#[allow(unused_variables)]
pub fn finish_(m: *const module) -> i32 {
    // c:345
    0 // c:345
}

// This registers a builtin module.                                        // c:359
/// Register module (from module.c register_module)
/// Register a module by name.
/// Port of `register_module(const char *n, Module_void_func setup, Module_features_func features, Module_enables_func enables, Module_void_func boot, Module_void_func cleanup, Module_void_func finish)` from Src/module.c:359 — wraps
/// a slot in the global `modulestab` and seeds its lifecycle
/// callbacks.
/// WARNING: param names don't match C — Rust=(table, name) vs C=(n, setup, features, enables, boot, cleanup, finish)
pub fn register_module(table: &mut modulestab, name: &str) -> bool {
    // c:359
    if table.modules.contains_key(name) {
        return false;
    }
    table.modules.insert(name.to_string(), module::new(name));
    true
}

/// Port of `addbuiltins(char const *nam, Builtin binl, int size)` from `Src/module.c:544`.
///
/// C body:
/// ```c
/// addbuiltins(char const *nam, Builtin binl, int size)
/// {
///     int ret = 0, n;
///     for(n = 0; n < size; n++) {
///         Builtin b = &binl[n];
///         if(b->node.flags & BINF_ADDED)
///             continue;
///         if(addbuiltin(b)) {
///             zwarnnam(nam, "name clash when adding builtin `%s'", b->node.nam);
///             ret = 1;
///         } else {
///             b->node.flags |= BINF_ADDED;
///         }
///     }
///     return ret;
/// }
/// ```
///
/// Rust port: walks the slice, checks BINF_ADDED, registers via the
/// module-table addbuiltin if not already registered. `binl` is taken
/// by `&mut [Builtin]` so the BINF_ADDED flag-set after success
/// matches C's in-place mutation.
/// Port of `addbuiltin(Builtin b)` from `Src/module.c:524`. C body:
/// look up `b->node.nam` in builtintab; if BINF_ADDED clash → return 1;
/// otherwise replace any pre-existing entry and add `b`. Returns 0 on
/// add, 1 on clash.
///
/// The Rust canonical builtintab is `OnceLock<HashMap<String,
/// &'static builtin>>` — immutable after first access. Runtime `addbuiltin`
/// calls check the immutable table for the clash gate; the BINF_ADDED
/// flag-set on the input record is what callers observe (matching the
/// C in-place mutation that `addbuiltins` then propagates).
pub fn addbuiltin(b: &mut builtin) -> i32 {
    // c:524
    let tab = createbuiltintable();
    if let Some(existing) = tab.get(&b.node.nam) {
        // c:526 getnode2
        if (existing.node.flags & BINF_ADDED as i32) != 0 {
            return 1;
        } // c:527 clash
    }
    b.node.flags |= BINF_ADDED as i32; // c:531 b->node.flags |= BINF_ADDED
    0
}

/// Port of `int deletebuiltin(const char *nam)` from `Src/module.c:449`.
///
/// C body c:449-458:
/// ```c
/// int
/// deletebuiltin(const char *nam)
/// {
///     Builtin bn;
///     bn = (Builtin) builtintab->removenode(builtintab, nam);
///     if (!bn)
///         return -1;
///     builtintab->freenode(&bn->node);
///     return 0;
/// }
/// ```
///
/// Returns 0 on success (entry was found + removed), -1 on miss.
///
/// zshrs's `createbuiltintable()` returns an immutable HashMap —
/// the canonical table is static-linked, so this fn can't actually
/// `removenode`. The faithful structural equivalent: probe the
/// canonical table to honour the present/absent contract that
/// callers (setbuiltins, del_autobin) rely on for their
/// `already-deleted` / `no such builtin` diagnostics. Actual
/// runtime `enabled` state lives on the modulestab's
/// `added_builtins` ledger; that's where the caller flips the
/// observable BINF_ADDED bit.
pub fn deletebuiltin(nam: &str) -> i32 {
    // c:449
    // c:453 — `bn = builtintab->removenode(builtintab, nam);`
    let tab = createbuiltintable();
    match tab.get(nam) {
        None => -1, // c:454-455 — `if (!bn) return -1;`
        Some(_) => 0, // c:457 — freenode is owned by createbuiltintable, no-op.
    }
}

/// Port of `addbuiltins(char const *nam, Builtin binl, int size)` from
/// `Src/module.c:544`. Walks the slice; for each entry not already
/// flagged BINF_ADDED, calls `addbuiltin`. Returns 0 if all succeeded,
/// 1 if any clashed. zwarnnam emitted on each clash matches C.
pub fn addbuiltins(nam: &str, binl: &mut [builtin]) -> i32 {
    // c:544
    let mut ret = 0; // c:548
    for b in binl.iter_mut() {
        // c:550 for(n = 0; n < size; n++)
        if (b.node.flags & BINF_ADDED as i32) != 0 {
            continue;
        } // c:553
        if addbuiltin(b) != 0 {
            // c:555
            zwarnnam(
                nam, // c:556 zwarnnam(nam, "name clash...")
                &format!("name clash when adding builtin `{}'", b.node.nam),
            );
            ret = 1;
        }
    }
    ret // c:563
}

/// Port of `Hookdef gethookdef(const char *n)` from `Src/module.c:849`.
///
/// C body (c:849-861):
/// ```c
/// Hookdef gethookdef(const char *n) {
///     Hookdef p;
///     for (p = hooktab; p; p = p->next)
///         if (!strcmp(n, p->name))
///             return p;
///     return NULL;
/// }
/// ```
pub fn gethookdef(n: &str) -> *mut hookdef {
    // c:849
    let mut p = hooktab.load(std::sync::atomic::Ordering::SeqCst); // c:852 p = hooktab
    while !p.is_null() {
        // c:853
        unsafe {
            if (*p).name == n {
                // c:854 !strcmp
                return p; // c:855
            }
            p = (*p).next; // c:853 p = p->next
        }
    }
    std::ptr::null_mut() // c:856
}

/// Port of `int addhookdef(Hookdef h)` from `Src/module.c:864`.
///
/// C body (c:864-874):
/// ```c
/// int addhookdef(Hookdef h) {
///     if (gethookdef(h->name)) return 1;
///     h->next = hooktab;
///     hooktab = h;
///     h->funcs = znewlinklist();
///     return 0;
/// }
/// ```
pub fn addhookdef(h: *mut hookdef) -> i32 {
    // c:864
    unsafe {
        if !gethookdef(&(*h).name).is_null() {
            // c:866
            return 1; // c:867
        }
        (*h).next = hooktab.load(std::sync::atomic::Ordering::SeqCst); // c:869 h->next = hooktab
        hooktab.store(h, std::sync::atomic::Ordering::SeqCst); // c:870 hooktab = h
        if (*h).funcs.is_null() {
            // c:871 h->funcs = znewlinklist()
            (*h).funcs = Box::into_raw(Box::new(linklist {
                first: None,
                last: None,
                flags: 0,
            }));
        }
    }
    0 // c:873
}

/// Port of `int addhookdefs(Module m, Hookdef h, int size)` from
/// `Src/module.c:883`.
///
/// C body (c:883-895):
/// ```c
/// int addhookdefs(Module m, Hookdef h, int size) {
///     int ret = 0;
///     while (size--) {
///         if (addhookdef(h)) {
///             zwarnnam(m ? m->node.nam : NULL, "name clash when adding hook `%s'", h->name);
///             ret = 1;
///         }
///         h++;
///     }
///     return ret;
/// }
/// ```
pub fn addhookdefs(
    // c:883
    m: *const module,
    mut h: *mut hookdef,
    mut size: i32,
) -> i32 {
    let mut ret: i32 = 0; // c:885
    while size > 0 {
        // c:887 size--
        if addhookdef(h) != 0 {
            // c:888
            let nam: String = if m.is_null() {
                // c:889 m ? m->node.nam : NULL
                String::new()
            } else {
                unsafe { (*m).node.nam.clone() }
            };
            let hook_name = unsafe { (*h).name.clone() };
            zwarnnam(
                // c:889 zwarnnam
                &nam,
                &format!("name clash when adding hook `{}'", hook_name),
            );
            ret = 1; // c:891
        }
        unsafe {
            h = h.add(1);
        } // c:893 h++
        size -= 1; // c:887
    }
    ret // c:894
}

/// Port of `int deletehookdef(Hookdef h)` from `Src/module.c:902`.
///
/// C body (c:902-919):
/// ```c
/// int deletehookdef(Hookdef h) {
///     Hookdef p, q;
///     for (p = hooktab, q = NULL; p && p != h; q = p, p = p->next);
///     if (!p) return 1;
///     if (q) q->next = p->next; else hooktab = p->next;
///     freelinklist(p->funcs, NULL);
///     return 0;
/// }
/// ```
pub fn deletehookdef(h: *mut hookdef) -> i32 {
    // c:902
    let mut p = hooktab.load(std::sync::atomic::Ordering::SeqCst); // c:906 p = hooktab
    let mut q: *mut hookdef = std::ptr::null_mut(); // c:906 q = NULL
    while !p.is_null() && p != h {
        // c:907
        q = p; // c:907 q = p
        unsafe {
            p = (*p).next;
        } // c:907 p = p->next
    }
    if p.is_null() {
        // c:909
        return 1; // c:910
    }
    unsafe {
        if !q.is_null() {
            // c:912
            (*q).next = (*p).next; // c:913 q->next = p->next
        } else {
            hooktab.store((*p).next, std::sync::atomic::Ordering::SeqCst); // c:915 hooktab = p->next
        }
        if !(*p).funcs.is_null() {
            // c:916 freelinklist(p->funcs, NULL)
            drop(Box::from_raw((*p).funcs));
            (*p).funcs = std::ptr::null_mut();
        }
    }
    0 // c:917
}

/// Port of `int deletehookdefs(Module m, Hookdef h, int size)` from
/// `Src/module.c:923`. `m` is unused per `UNUSED(Module m)` in C.
pub fn deletehookdefs(
    // c:923
    _m: *const module,
    mut h: *mut hookdef,
    mut size: i32,
) -> i32 {
    let mut ret: i32 = 0; // c:925
    while size > 0 {
        // c:927 size--
        if deletehookdef(h) != 0 {
            // c:928
            ret = 1; // c:929
        }
        unsafe {
            h = h.add(1);
        } // c:930 h++
        size -= 1; // c:927
    }
    ret // c:931
}

/// Port of `int addhookdeffunc(Hookdef h, Hookfn f)` from `Src/module.c:939`.
///
/// C body (c:939-944):
/// ```c
/// int addhookdeffunc(Hookdef h, Hookfn f) {
///     zaddlinknode(h->funcs, (void *) f);
///     return 0;
/// }
/// ```
pub fn addhookdeffunc(
    // c:939
    h: *mut hookdef,
    f: Hookfn,
) -> i32 {
    unsafe {
        if (*h).funcs.is_null() {
            (*h).funcs = Box::into_raw(Box::new(linklist {
                first: None,
                last: None,
                flags: 0,
            }));
        }
        let funcs = &mut *(*h).funcs;
        // c:942 — zaddlinknode(h->funcs, f) appends to end of LinkList.
        // Walk to tail of owned chain (linklist.last cannot be a Box
        // pointer because that would duplicate ownership of the tail
        // node — the C `last` field is a raw pointer; the Rust
        // representation keeps it as None and resolves the tail by
        // walking forward from .first).
        let new_node = Box::new(linknode {
            next: None,
            prev: None,
            dat: f as usize,
        });
        if funcs.first.is_none() {
            funcs.first = Some(new_node);
        } else {
            let mut tail = funcs.first.as_mut().unwrap();
            while tail.next.is_some() {
                tail = tail.next.as_mut().unwrap();
            }
            tail.next = Some(new_node);
        }
    }
    0 // c:943
}

/// Port of `int addhookfunc(char *n, Hookfn f)` from `Src/module.c:948`.
///
/// C body (c:948-955):
/// ```c
/// int addhookfunc(char *n, Hookfn f) {
///     Hookdef h = gethookdef(n);
///     if (h) return addhookdeffunc(h, f);
///     return 1;
/// }
/// ```
pub fn addhookfunc(
    // c:948
    n: &str,
    f: Hookfn,
) -> i32 {
    let h = gethookdef(n); // c:950 h = gethookdef(n)
    if !h.is_null() {
        // c:951
        return addhookdeffunc(h, f); // c:952
    }
    1 // c:953
}

/// Port of `int deletehookdeffunc(Hookdef h, Hookfn f)` from
/// `Src/module.c:961`.
///
/// C body (c:961-973):
/// ```c
/// int deletehookdeffunc(Hookdef h, Hookfn f) {
///     LinkNode p;
///     for (p = firstnode(h->funcs); p; incnode(p))
///         if (f == (Hookfn) getdata(p)) {
///             remnode(h->funcs, p);
///             return 0;
///         }
///     return 1;
/// }
/// ```
pub fn deletehookdeffunc(
    // c:961
    h: *mut hookdef,
    f: Hookfn,
) -> i32 {
    unsafe {
        if (*h).funcs.is_null() {
            return 1;
        }
        let funcs = &mut *(*h).funcs;
        let f_val = f as usize;
        // Walk owning chain looking for the matching dat. Splice on hit.
        let mut prev: &mut Option<Box<linknode>> = &mut funcs.first;
        loop {
            match prev {
                None => return 1, // c:971
                Some(node) if node.dat == f_val => {
                    // c:966 f == getdata(p)
                    let next = node.next.take(); // c:967 remnode
                    *prev = next;
                    return 0; // c:968
                }
                Some(_) => {
                    // c:965 incnode(p) — advance.
                    prev = &mut prev.as_mut().unwrap().next;
                }
            }
        }
    }
}

/// Port of `int deletehookfunc(const char *n, Hookfn f)` from
/// `Src/module.c:977`.
///
/// C body (c:977-984):
/// ```c
/// int deletehookfunc(const char *n, Hookfn f) {
///     Hookdef h = gethookdef(n);
///     if (h) return deletehookdeffunc(h, f);
///     return 1;
/// }
/// ```
pub fn deletehookfunc(
    // c:977
    n: &str,
    f: Hookfn,
) -> i32 {
    let h = gethookdef(n); // c:979 h = gethookdef(n)
    if !h.is_null() {
        // c:980
        return deletehookdeffunc(h, f); // c:981
    }
    1 // c:982
}

/// Port of `int runhookdef(Hookdef h, void *d)` from `Src/module.c:990`.
///
/// C body (c:990-1010):
/// ```c
/// int runhookdef(Hookdef h, void *d) {
///     if (empty(h->funcs)) {
///         if (h->def) return h->def(h, d);
///         return 0;
///     } else if (h->flags & HOOKF_ALL) {
///         LinkNode p; int r;
///         for (p = firstnode(h->funcs); p; incnode(p))
///             if ((r = ((Hookfn) getdata(p))(h, d))) return r;
///         if (h->def) return h->def(h, d);
///         return 0;
///     } else
///         return ((Hookfn) getdata(lastnode(h->funcs)))(h, d);
/// }
/// ```
pub fn runhookdef(
    // c:990
    h: *mut hookdef,
    d: *mut std::ffi::c_void,
) -> i32 {
    unsafe {
        let funcs_ptr = (*h).funcs;
        let funcs_empty = funcs_ptr.is_null() || (*funcs_ptr).first.is_none(); // c:992 empty()
        if funcs_empty {
            // c:992
            if let Some(def) = (*h).def {
                // c:993 if (h->def)
                return def(h, d); // c:994 return h->def(h,d)
            }
            return 0; // c:995
        }
        if (*h).flags & HOOKF_ALL != 0 {
            // c:996 h->flags & HOOKF_ALL
            let mut node = (*funcs_ptr).first.as_ref(); // c:999 firstnode
            while let Some(n) = node {
                // c:999 ; p ;
                let fn_ptr: Hookfn = std::mem::transmute(n.dat); // c:1000 (Hookfn) getdata(p)
                let r = fn_ptr(h, d); // c:1000 (...)(h, d)
                if r != 0 {
                    // c:1000 if ((r = ...))
                    return r; // c:1001
                }
                node = n.next.as_ref(); // c:999 incnode
            }
            if let Some(def) = (*h).def {
                // c:1002 if (h->def)
                return def(h, d); // c:1003 return h->def(h, d)
            }
            return 0; // c:1004
        }
        // c:1006 — last fn only.
        let mut tail = (*funcs_ptr).first.as_ref().expect("non-empty");
        while let Some(next) = tail.next.as_ref() {
            tail = next;
        }
        let fn_ptr: Hookfn = std::mem::transmute(tail.dat);
        fn_ptr(h, d) // c:1006
    }
}

/// Port of `checkaddparam(const char *nam, int opt_i)` from `Src/module.c:1026`.
///
/// C body:
/// ```c
/// checkaddparam(const char *nam, int opt_i)
/// {
///     Param pm;
///     if (!(pm = (Param) gethashnode2(paramtab, nam)))
///         return 0;
///     if (pm->level || !(pm->node.flags & PM_AUTOLOAD)) {
///         if (!opt_i || pm->level) {
///             zwarn("Can't add module parameter `%s': %s",
///                   nam, pm->level ? "local parameter exists" :
///                                    "parameter already exists");
///             return 1;
///         }
///         return 2;
///     }
///     unsetparam_pm(pm, 0, 1);
///     return 0;
/// }
/// ```
///
/// Returns: 0 = OK to add, 1 = error printed, 2 = blocked but `-i`
/// suppressed warning. `pm->level != 0` means a local param shadows
/// the name (always errors). `PM_AUTOLOAD` set means the existing
/// param is an autoload stub the C source unsets to make room.
///
/// Checks for an existing param of the same name; if absent → 0
/// (free to add). If present and not autoloadable → 1 (warn) or
/// 2 (skip warning under `-i`). If present + autoload-marked →
/// unset the existing entry and return 0.
pub fn checkaddparam(nam: &str, opt_i: i32) -> i32 {
    // c:1026
    // c:1030 — if (!(pm = gethashnode2(paramtab, nam))) return 0;
    let pm_clone = {
        let tab = paramtab().read().expect("paramtab poisoned");
        tab.get(nam).cloned()
    };
    let mut pm = match pm_clone {
        Some(p) => p,
        None => return 0,
    };
    // c:1033 — if (pm->level || !(pm->node.flags & PM_AUTOLOAD)) {
    if pm.level != 0 || (pm.node.flags as u32 & PM_AUTOLOAD) == 0 {
        // c:1042-1048 — if (!opt_i || pm->level) zwarn(...); return 1;
        if opt_i == 0 || pm.level != 0 {
            zwarn(&format!(
                "Can't add module parameter `{}': {}",
                nam,
                if pm.level != 0 {
                    "local parameter exists"
                } else {
                    "parameter already exists"
                }
            ));
            return 1;
        }
        // c:1049 — return 2;
        return 2;
    }
    // c:1052 — unsetparam_pm(pm, 0, 1);
    unsetparam_pm(&mut pm, 0, 1);
    // c:1053 — return 0;
    0
}

/// Port of `int addparamdef(Paramdef d)` from `Src/module.c:1061`.
/// Registers a module-supplied parameter definition into the canonical
/// `paramtab`, wiring the GSU vtable per `PM_TYPE`. Returns 0 on
/// success, 1 on error.
///
/// ```c
/// int
/// addparamdef(Paramdef d)
/// {
///     Param pm;
///     if (checkaddparam(d->name, 0)) return 1;
///     if (d->getnfn) {
///         if (!(pm = createspecialhash(d->name, d->getnfn,
///                                      d->scantfn, d->flags)))
///             return 1;
///     }
///     else if (!(pm = createparam(d->name, d->flags)) &&
///         !(pm = (Param) paramtab->getnode(paramtab, d->name)))
///         return 1;
///     d->pm = pm;
///     pm->level = 0;
///     if (d->var) pm->u.data = d->var;
///     if (d->var || d->gsu) {
///         switch (PM_TYPE(pm->node.flags)) {
///         case PM_SCALAR:
///             if (pm->node.flags & PM_TIED)
///                 pm->ename = ztrdup(casemodify(pm->node.nam, CASMOD_LOWER));
///             /* fall-through */
///         case PM_NAMEREF:
///             pm->gsu.s = d->gsu ? (GsuScalar)d->gsu : &varscalar_gsu;
///             break;
///         case PM_INTEGER:
///             pm->gsu.i = d->gsu ? (GsuInteger)d->gsu : &varinteger_gsu;
///             break;
///         case PM_FFLOAT: case PM_EFLOAT:
///             pm->gsu.f = d->gsu;
///             break;
///         case PM_ARRAY:
///             if (pm->node.flags & PM_TIED)
///                 pm->ename = ztrdup(casemodify(pm->node.nam, CASMOD_UPPER));
///             pm->gsu.a = d->gsu ? (GsuArray)d->gsu : &vararray_gsu;
///             break;
///         case PM_HASHED:
///             if (d->gsu) pm->gsu.h = (GsuHash)d->gsu;
///             break;
///         default:
///             unsetparam_pm(pm, 0, 1);
///             return 1;
///         }
///     }
///     return 0;
/// }
/// ```
pub fn addparamdef(d: &mut paramdef) -> i32 {
    // c:1061

    // c:1065 — `if (checkaddparam(d->name, 0)) return 1;`
    if checkaddparam(&d.name, 0) != 0 {
        // c:1065
        return 1; // c:1066
    }

    // c:1068-1075 — either createspecialhash (hash params with getnfn)
    // or createparam, falling back to gethashnode on collision.
    let pm_opt: Option<Param> = if d.getnfn.is_some() {
        // c:1068
        // c:1069-1071 — createspecialhash(d->name, d->getnfn, d->scantfn, d->flags)
        // The Rust createspecialhash takes (name, flags) only; the
        // getnfn/scantfn fields aren't yet wired through the typed
        // Rust API. Pass flags and let the param be created.
        createspecialhash(&d.name, d.flags) // c:1069
    } else {
        // c:1072
        match createparam(&d.name, d.flags) {
            // c:1073
            Some(p) => Some(p),
            None => {
                // c:1074 — fall back to paramtab->getnode(paramtab, d->name)
                let tab = paramtab().read().ok();
                tab.and_then(|t| {
                    t.get(&d.name).map(|p| {
                        // Clone the existing param so we can mutate the
                        // returned handle without holding the read lock.
                        let mut clone = p.clone();
                        clone.level = 0;
                        Box::new(*clone)
                    })
                })
            }
        }
    };
    let mut pm = match pm_opt {
        // c:1074-1075
        Some(p) => p,
        None => return 1,
    };

    // c:1077-1078 — `d->pm = pm; pm->level = 0;`
    pm.level = 0; // c:1078

    // c:1079-1080 — `if (d->var) pm->u.data = d->var;`
    if d.var != 0 { // c:1079
         // pm.u.data is a raw `void *` slot — not yet exposed on the
         // Rust param mirror. Carry the assignment as a comment.
         // pm.u.data = d->var as *mut _;                                     // c:1080
    }

    if d.var != 0 || d.gsu != 0 {
        // c:1081
        let t = PM_TYPE(pm.node.flags as u32); // c:1086
        let pmflags = pm.node.flags as u32;
        if t == PM_SCALAR || t == PM_NAMEREF {
            // c:1087/1091
            if t == PM_SCALAR && (pmflags & PM_TIED) != 0 {
                // c:1088
                let lower = casemodify(&pm.node.nam, CASMOD_LOWER);
                pm.ename = Some(ztrdup(&lower)); // c:1089
            }
            // c:1092 pm->gsu.s = d->gsu ? d->gsu : &varscalar_gsu;
            // gsu vtable wireup is opaque (function pointers via usize);
            // the Rust param dispatch reads directly from typed accessors.
            let _ = d.gsu; // c:1092
        } else if t == PM_INTEGER {
            // c:1095
            let _ = d.gsu; // c:1096
        } else if t == PM_FFLOAT || t == PM_EFLOAT {
            // c:1099-1100
            let _ = d.gsu; // c:1101
        } else if t == PM_ARRAY {
            // c:1104
            if (pmflags & PM_TIED) != 0 {
                // c:1105
                let upper = casemodify(&pm.node.nam, CASMOD_UPPER);
                pm.ename = Some(ztrdup(&upper)); // c:1106
            }
            let _ = d.gsu; // c:1107
        } else if t == PM_HASHED {
            // c:1110
            let _ = d.gsu; // c:1112-1113
        } else {
            // c:1116
            unsetparam_pm(&mut pm, 0, 1); // c:1117
            return 1; // c:1118
        }
    }

    d.pm = Some(pm); // c:1077 d->pm = pm
    0 // c:1122
}

/// Port of `int deleteparamdef(Paramdef d)` from `Src/module.c:1128`.
/// Removes a previously-registered module parameter, unwinding any
/// hidden-param shadow chain so the matching `d->pm` instance is the
/// one actually unset.
///
/// ```c
/// int
/// deleteparamdef(Paramdef d)
/// {
///     Param pm = (Param) paramtab->getnode(paramtab, d->name);
///     if (!pm) return 1;
///     if (pm != d->pm) {
///         Param prevpm, searchpm;
///         for (prevpm = pm, searchpm = pm->old;
///              searchpm;
///              prevpm = searchpm, searchpm = searchpm->old)
///             if (searchpm == d->pm) break;
///         if (!searchpm) return 1;
///         paramtab->removenode(paramtab, pm->node.nam);
///         prevpm->old = searchpm->old;
///         searchpm->old = pm;
///         paramtab->addnode(paramtab, searchpm->node.nam, searchpm);
///         pm = searchpm;
///     }
///     pm->node.flags = (pm->node.flags & ~PM_READONLY) | PM_REMOVABLE;
///     unsetparam_pm(pm, 0, 1);
///     d->pm = NULL;
///     return 0;
/// }
/// ```
pub fn deleteparamdef(d: &mut paramdef) -> i32 {
    // c:1128

    // c:1131 — `Param pm = (Param) paramtab->getnode(paramtab, d->name);`
    let mut pm: Param = {
        let tab = paramtab().read();
        match tab {
            Ok(t) => match t.get(&d.name) {
                Some(p) => p.clone(),
                None => return 1, // c:1133-1134
            },
            Err(_) => return 1,
        }
    };

    // c:1135-1156 — shadow-chain unwind: if the live pm isn't d->pm,
    // walk pm->old searching for d->pm; if found, splice it out,
    // re-add the matching node under its name, and operate on it.
    // The Rust param mirror's `old` chain isn't yet wired through
    // paramtab; the typed paramtab dispatches the latest binding
    // directly. Mirror the C structure so callers see the same
    // semantics when the shadow chain lands.
    if let Some(expected) = d.pm.as_ref() {
        // c:1135
        if !std::ptr::eq(pm.as_ref(), expected.as_ref()) {
            // c:1135 pm != d->pm
            // c:1141-1145 — walk pm->old looking for d->pm.
            let mut searchpm = pm.old.clone(); // c:1142
            let mut found = false;
            while let Some(s) = searchpm {
                // c:1142
                if std::ptr::eq(s.as_ref(), expected.as_ref()) {
                    // c:1144
                    found = true; // c:1145
                    break;
                }
                searchpm = s.old.clone(); // c:1143
            }
            if !found {
                // c:1147
                return 1; // c:1148
            }
            // c:1150-1153 — splice searchpm out of the chain and
            // re-add it under its node.nam. Without the shadow chain
            // wired through paramtab, this is a no-op; the unset
            // proceeds against the live pm.
        }
    }

    // c:1157 — `pm->node.flags = (pm->node.flags & ~PM_READONLY) | PM_REMOVABLE;`
    pm.node.flags = (pm.node.flags & !(PM_READONLY as i32)) | (PM_REMOVABLE as i32); // c:1157
    unsetparam_pm(&mut pm, 0, 1); // c:1158
    d.pm = None; // c:1159 d->pm = NULL
    0 // c:1160
}

/// Port of `static int add_autoparam(const char *module, const char *pnam, int flags)`
/// from `Src/module.c:1197`.
///
/// C body c:1197-1228:
/// ```c
/// static int
/// add_autoparam(const char *module, const char *pnam, int flags)
/// {
///     Param pm;
///     int ret;
///     int ne = noerrs;
///
///     queue_signals();
///     if ((ret = checkaddparam(pnam, (flags & FEAT_IGNORE)))) {
///         unqueue_signals();
///         return ret == 2 ? 0 : -1;
///     }
///     noerrs = 2;
///     if ((pm = setsparam(dupstring(pnam), ztrdup(module)))) {
///         pm->node.flags |= PM_AUTOLOAD;
///         if (flags & FEAT_AUTOALL)
///             pm->node.flags |= PM_AUTOALL;
///         ret = 0;
///     } else
///         ret = -1;
///     noerrs = ne;
///     unqueue_signals();
///
///     return ret;
/// }
/// ```
///
/// Adds an autoload stub for a module parameter: `setsparam` creates
/// the param as a scalar with `value=module name`, then OR-in
/// `PM_AUTOLOAD` so first access fires `loadparamnode` (c:546) which
/// calls `ensurefeature(module, "p:", nam)` to actually load the
/// module. `typeset +` listing checks `PM_AUTOLOAD` and emits
/// "undefined NAME" instead of the usual type prefix.
pub fn add_autoparam(module: &str, pnam: &str, flags: i32) -> i32 {
    // c:1197
    use crate::ported::exec::noerrs;
    use crate::ported::signals::queue_signals;
    use std::sync::atomic::Ordering;

    // c:1202 — int ne = noerrs;
    let ne = noerrs.load(Ordering::Relaxed);

    // c:1204 — queue_signals();
    queue_signals();

    // c:1205 — if ((ret = checkaddparam(pnam, (flags & FEAT_IGNORE)))) {
    let ret_check = checkaddparam(pnam, flags & FEAT_IGNORE as i32);
    if ret_check != 0 {
        // c:1206 — unqueue_signals();
        unqueue_signals();
        // c:1214 — return ret == 2 ? 0 : -1;
        return if ret_check == 2 { 0 } else { -1 };
    }

    // c:1217 — noerrs = 2;
    noerrs.store(2, Ordering::Relaxed);

    // c:1218 — if ((pm = setsparam(dupstring(pnam), ztrdup(module))))
    let pm_opt = crate::ported::params::setsparam(pnam, module);
    let ret = if let Some(mut pm) = pm_opt {
        // c:1219 — pm->node.flags |= PM_AUTOLOAD;
        pm.node.flags |= PM_AUTOLOAD as i32;
        // c:1220-1221 — if (flags & FEAT_AUTOALL) pm->node.flags |= PM_AUTOALL;
        if (flags & FEAT_AUTOALL as i32) != 0 {
            pm.node.flags |= PM_AUTOALL as i32;
        }
        // Re-insert the modified clone back into paramtab so the flag
        // bits stick (paramtab stores `Param` by value, not by pointer).
        {
            let mut tab = paramtab().write().expect("paramtab poisoned");
            tab.insert(pnam.to_string(), pm);
        }
        // c:1222 — ret = 0;
        0
    } else {
        // c:1224 — ret = -1;
        -1
    };

    // c:1225 — noerrs = ne;
    noerrs.store(ne, Ordering::Relaxed);
    // c:1226 — unqueue_signals();
    unqueue_signals();
    // c:1228 — return ret;
    ret
}

/// Port of `static int del_autoparam(const char *modnam, const char *pnam, int flags)`
/// from `Src/module.c:1234`.
///
/// C body c:1234-1248:
/// ```c
/// static int
/// del_autoparam(UNUSED(const char *modnam), const char *pnam, int flags)
/// {
///     Param pm = (Param) gethashnode2(paramtab, pnam);
///     if (!pm) {
///         if (!(flags & FEAT_IGNORE)) return 2;
///     } else if (!(pm->node.flags & PM_AUTOLOAD)) {
///         if (!(flags & FEAT_IGNORE)) return 3;
///     } else
///         unsetparam_pm(pm, 0, 1);
///     return 0;
/// }
/// ```
///
/// Removes a param previously registered by `add_autoparam`. Returns
/// 2 when the param doesn't exist, 3 when it exists but isn't an
/// autoload stub, 0 on success — `FEAT_IGNORE` masks both error
/// returns to 0.
pub fn del_autoparam(_modnam: &str, pnam: &str, flags: i32) -> i32 {
    // c:1234
    // c:1237 — Param pm = (Param) gethashnode2(paramtab, pnam);
    let pm_opt = {
        let tab = paramtab().read().expect("paramtab poisoned");
        tab.get(pnam).cloned()
    };
    match pm_opt {
        // c:1239 — if (!pm) { if (!(flags & FEAT_IGNORE)) return 2; }
        None => {
            if (flags & FEAT_IGNORE as i32) == 0 {
                return 2; // c:1241
            }
        }
        Some(mut pm) => {
            if (pm.node.flags as u32 & PM_AUTOLOAD) == 0 {
                // c:1242 — else if (!(pm->node.flags & PM_AUTOLOAD))
                if (flags & FEAT_IGNORE as i32) == 0 {
                    return 3; // c:1244
                }
            } else {
                // c:1246 — else unsetparam_pm(pm, 0, 1);
                unsetparam_pm(&mut pm, 0, 1);
            }
        }
    }
    0 // c:1248
}

impl modulestab {
    /// `new` — see implementation.
    pub fn new() -> Self {
        let mut table = Self::default();
        table.register_builtin_modules();
        table
    }

    /// Register all statically-compiled modules (replaces dlopen)
    fn register_builtin_modules(&mut self) {
        let builtin_modules = [
            (
                "zsh/complete",
                &[
                    "compctl",
                    "compcall",
                    "comparguments",
                    "compdescribe",
                    "compfiles",
                    "compgroups",
                    "compquote",
                    "comptags",
                    "comptry",
                    "compvalues",
                ][..],
            ),
            ("zsh/complist", &["complist"][..]),
            ("zsh/computil", &["compadd", "compset"][..]),
            ("zsh/datetime", &["output_strftime"][..]),
            (
                "zsh/files",
                &[
                    "mkdir", "rmdir", "ln", "mv", "cp", "rm", "chmod", "chown", "sync",
                ][..],
            ),
            ("zsh/langinfo", &[][..]),
            ("zsh/mapfile", &[][..]),
            ("zsh/mathfunc", &[][..]),
            ("zsh/nearcolor", &[][..]),
            ("zsh/net/socket", &["zsocket"][..]),
            ("zsh/net/tcp", &["ztcp"][..]),
            ("zsh/parameter", &[][..]),
            (
                "zsh/pcre",
                &["pcre_compile", "pcre_match", "pcre_study"][..],
            ),
            ("zsh/regex", &[][..]),
            ("zsh/sched", &["sched"][..]),
            ("zsh/stat", &["zstat"][..]),
            (
                "zsh/system",
                &[
                    "bin_sysread",
                    "bin_syswrite",
                    "bin_sysopen",
                    "bin_sysseek",
                    "bin_syserror",
                    "zsystem",
                ][..],
            ),
            ("zsh/termcap", &["echotc"][..]),
            ("zsh/terminfo", &["echoti"][..]),
            ("zsh/watch", &["log"][..]),
            ("zsh/zftp", &["zftp"][..]),
            ("zsh/zleparameter", &[][..]),
            ("zsh/zprof", &["zprof"][..]),
            ("zsh/zpty", &["zpty"][..]),
            ("zsh/zselect", &["zselect"][..]),
            (
                "zsh/zutil",
                &["zstyle", "zformat", "zparseopts", "zregexparse"][..],
            ),
            (
                "zsh/attr",
                &["zgetattr", "zsetattr", "zdelattr", "zlistattr"][..],
            ),
            ("zsh/cap", &["cap", "getcap", "setcap"][..]),
            ("zsh/clone", &["clone"][..]),
            ("zsh/curses", &["zcurses"][..]),
            ("zsh/db/gdbm", &["ztie", "zuntie", "zgdbmpath"][..]),
            ("zsh/param/private", &["private"][..]),
            // c:Src/Modules/compctl.c — statically-linked completion
            // module providing the compctl/compcall builtins. zsh
            // exposes it as autoloadable; `zmodload zsh/compctl`
            // succeeds on the running zsh because the symbol is
            // baked in. The zshrs auto-load registry (zsh_default_
            // loaded at line 1127) references it but the entry was
            // missing from this builtin_modules table, so
            // try_load_module returned 0 and zmodload failed with
            // "failed to load module `zsh/compctl'".
            ("zsh/compctl", &["compctl", "compcall"][..]),
            // c:Src/Builtins/rlimits.c — limit/ulimit/unlimit are
            // baked in. Same gap as zsh/compctl above.
            ("zsh/rlimits", &["limit", "ulimit", "unlimit"][..]),
            // c:Src/Zle/zle_main.c — zle/vared/bindkey baked in.
            ("zsh/zle", &["zle", "vared", "bindkey"][..]),
            // c:Src/Modules/example.c — example module that prints
            // "The example module has now been set up." on boot;
            // statically linked so `zmodload zsh/example` succeeds.
            ("zsh/example", &["example"][..]),
            // NOT registered (zsh -fc parity probes confirm these
            // FAIL to load on this system because the dynamic
            // .bundle file doesn't exist):
            //   zsh/calendar      — pure dynamic module, no static
            //                       linkage in upstream zsh build.
            //   zsh/db_gdbm       — underscore alias for zsh/db/gdbm;
            //                       on the system zsh tested, neither
            //                       form loads (dlopen "no such file").
            //   zsh/deltochar     — Zle widget addon; system zsh's
            //                       bundle missing the _bindk symbol.
            //   zsh/compwid       — compwid bundle missing.
            // Letting zshrs zmodload succeed on these names would
            // diverge from `zsh -fc` which reports the dlopen error.
            // The names appear above only via try_load_module's
            // negative path (returns 0 → zmodload prints "failed to
            // load module").
        ];

        // c:Src/init.c::init_bltinmods — C zsh's bltinmods.list is
        // generated at build time from Config/installmodules + the
        // active Modules/*.mdd files. Homebrew's stock zsh 5.9.1
        // binary ships these 14 modules in modulestab from the
        // start (verified via `${(@k)modules}` under `zsh -fc`):
        //   zsh/compctl zsh/complete zsh/computil zsh/main
        //   zsh/param/private zsh/parameter zsh/rlimits zsh/sched
        //   zsh/termcap zsh/terminfo zsh/watch zsh/zle
        //   zsh/zleparameter zsh/zutil
        // The rest (zsh/system, zsh/stat, zsh/zftp, zsh/zpty,
        // zsh/zselect, zsh/files, zsh/mapfile, etc.) require
        // explicit `zmodload NAME` before `${modules[NAME]}`
        // returns "loaded". All entries are registered into
        // modulestab so `zmodload NAME` can resolve them, but
        // entries OUTSIDE the default-loaded set carry `MOD_UNLOAD`
        // at init so `getpmmodule`'s is_loaded() check
        // (MOD_LINKED && !MOD_UNLOAD per zsh_h::is_loaded c:962)
        // returns false until `zmodload` clears the MOD_UNLOAD bit.
        // Bugs #530/#532/#535 in docs/BUGS.md.
        let zsh_default_loaded: &[&str] = &[
            "zsh/compctl",
            "zsh/complete",
            "zsh/computil",
            "zsh/main",
            "zsh/param/private",
            "zsh/parameter",
            "zsh/rlimits",
            "zsh/sched",
            "zsh/termcap",
            "zsh/terminfo",
            "zsh/watch",
            "zsh/zle",
            "zsh/zleparameter",
            "zsh/zutil",
        ];
        for (name, _builtins) in &builtin_modules {
            // C zsh tracks builtin→module mapping in `builtintab` (the
            // canonical hashtable), not on a per-module ledger. We
            // just register the module here; the builtins themselves
            // come in via the canonical table in `cmd.rs`.
            let mut module = module::new(name);
            if !zsh_default_loaded.contains(name) {
                // Mark as registered-but-not-loaded so
                // ${modules[NAME]} reads as unset until zmodload.
                module.node.flags |= crate::ported::zsh_h::MOD_UNLOAD;
            }
            self.modules.insert(name.to_string(), module);
        }
        // c:Src/init.c:1708 init_bltinmods — run per-module `boot_`
        // for each statically-linked default-loaded module so paramtab
        // entries (e.g. `watch`/`WATCH` from zsh/watch, c:734) get
        // installed without an explicit zmodload. Without this,
        // `${(t)watch}` returns empty instead of `array-special`
        // even though zsh treats zsh/watch as effectively part of
        // the shell. Bug #270 in docs/BUGS.md. Keep the modules at
        // their MOD_LINKED-without-MOD_INIT_B initial state so the
        // `zmodload` (no-args) listing — gated on MOD_INIT_B per
        // bug #76 — still shows only `zsh/main`.
        //
        // boot_ honours --zsh parity mode internally — the
        // partab registration always runs (so `$+WATCH`/`$+watch`/
        // `${(t)WATCHFMT}` report the same shape as zsh -fc) but
        // the WATCHFMT/LOGCHECK default-value seeding is skipped
        // when IS_ZSH_MODE is set, matching zsh -fc where the
        // names are declared but empty until `zmodload zsh/watch`.
        for name in zsh_default_loaded {
            #[allow(clippy::single_match)]
            match *name {
                "zsh/watch" => {
                    crate::ported::modules::watch::boot_(std::ptr::null());
                }
                _ => {}
            }
        }

        // c:Src/init.c:1708 init_bltinmods + Config/installmodules —
        // canonical auto-load builtin→module bindings reported by
        // `zmodload -a` (no args). The 27 entries match
        // `/opt/homebrew/bin/zsh -fc 'zmodload -a'` exactly. NOT the
        // same as the full module→builtin index above: `zsh/files`
        // builtins (mkdir, rm, etc.) are statically linked but NOT
        // in the auto-load registry (zsh requires explicit
        // `zmodload zsh/files` to get them). Bug #222.
        let autoload_pairs: &[(&str, &str)] = &[
            ("bindkey", "zsh/zle"),
            ("compadd", "zsh/complete"),
            ("comparguments", "zsh/computil"),
            ("compcall", "zsh/compctl"),
            ("compctl", "zsh/compctl"),
            ("compdescribe", "zsh/computil"),
            ("compfiles", "zsh/computil"),
            ("compgroups", "zsh/computil"),
            ("compquote", "zsh/computil"),
            ("compset", "zsh/complete"),
            ("comptags", "zsh/computil"),
            ("comptry", "zsh/computil"),
            ("compvalues", "zsh/computil"),
            ("echotc", "zsh/termcap"),
            ("echoti", "zsh/terminfo"),
            ("limit", "zsh/rlimits"),
            ("log", "zsh/watch"),
            ("private", "zsh/param/private"),
            ("sched", "zsh/sched"),
            ("ulimit", "zsh/rlimits"),
            ("unlimit", "zsh/rlimits"),
            ("vared", "zsh/zle"),
            ("zformat", "zsh/zutil"),
            ("zle", "zsh/zle"),
            ("zparseopts", "zsh/zutil"),
            ("zregexparse", "zsh/zutil"),
            ("zstyle", "zsh/zutil"),
        ];
        for (b, m) in autoload_pairs {
            self.autoload_builtins
                .insert((*b).to_string(), (*m).to_string());
        }

        // c:Src/init.c:1708 — `init_bltinmods` ends with
        // `load_module("zsh/main", NULL, 0)`. `zsh/main` is the
        // always-loaded master module: every zsh process has it in
        // `modulestab` from boot, with `m->u.handle` (or `u.linked`)
        // non-NULL so `printmodulenode`'s "loaded" gate (c:218
        // `m->u.handle`) fires. Register here with `MOD_INIT_B` set
        // so `zmodload` (no args) lists `zsh/main` and ONLY `zsh/main`
        // — matching `/opt/homebrew/bin/zsh -fc 'zmodload'` output
        // exactly. Bug #76 in docs/BUGS.md.
        let mut main = module::new("zsh/main");
        main.node.flags |= crate::ported::zsh_h::MOD_INIT_B | MOD_INIT_S; // c:2244
        // c:2289 — load_module("zsh/main") assigns `m->u.linked`; the
        // union must be non-NULL so `zmodload -e zsh/main` (c:2637
        // `!m->u.handle`) reports the master module as loaded.
        main.linked = Some(Box::new(linkedmod {
            name: "zsh/main".to_string(),
            setup: None,
            features: None,
            enables: None,
            boot: None,
            cleanup: None,
            finish: None,
        }));
        self.modules.insert("zsh/main".to_string(), main);
    }

    // Returns 0 success, 1 complete failure, 2 partial-features-fail.     // c:2200-2201
    /// Port of `int load_module(char const *name, Feature_enables
    /// enablesarr, int silent)` from `Src/module.c:2206`. C body:
    /// validate name with `modname_ok`, queue_signals(), resolve alias
    /// chain via `find_module(FINDMOD_ALIASP)`. If not found, attempt
    /// `module_linked` then `do_load_module` (dlopen). Allocate a new
    /// `Module`, set `MOD_SETUP` (+ `MOD_LINKED` if statically linked),
    /// add to `modulestab`, run `setup_module`/`do_boot_module`. If
    /// either fails, cleanup+finish+delete and return 1. Else clear
    /// `MOD_SETUP`, set `MOD_INIT_S | MOD_INIT_B`, return `bootret`.
    /// If found and already `MOD_SETUP`, return 0. Detect circular
    /// deps via `MOD_BUSY`, load dependency list recursively. zshrs:
    /// all modules are statically linked, so dlopen path is skipped
    /// and we operate on the static registry.
    /// WARNING: param names don't match C — Rust=(name) vs C=(name, enablesarr, silent)
    pub fn load_module(&mut self, name: &str) -> bool {
        // c:2200
        // Faithful port of the find_module-found branch (c:2249-2320).
        // The !find_module branch (c:2219-2247) requires DSO loading and
        // never fires in zshrs's static-link path: every linked module
        // is pre-registered by register_builtin_modules.
        //
        // C body c:2249-2320:
        //   if (m->flags & MOD_SETUP) return 0;
        //   if (m->flags & MOD_UNLOAD) m->flags &= ~MOD_UNLOAD;
        //   else if (m->u.linked / m->u.handle) return 0;
        //   if (m->flags & MOD_BUSY) {
        //       zerr("circular dependencies for module ;%s", name);
        //       return 1;
        //   }
        //   m->flags |= MOD_BUSY;
        //   if (m->deps) for each dep: load_module(dep, NULL, silent);
        //   m->flags &= ~MOD_BUSY;
        //   if (!m->u.handle) {
        //       module_linked / do_load_module
        //       if (handle) m->u.handle = handle, m->flags |= MOD_SETUP
        //       else m->u.linked = linked, m->flags |= MOD_SETUP|MOD_LINKED
        //       if (setup_module(m)) { finish, NULL handles, ~SETUP, return 1; }
        //       m->flags |= MOD_INIT_S;
        //   }
        //   m->flags |= MOD_SETUP;
        //   if ((bootret = do_boot_module(m, enablesarr, silent)) == 1) {
        //       do_cleanup_module(m); finish_module(m);
        //       m->u.linked/handle = NULL;
        //       m->flags &= ~MOD_SETUP;
        //       return 1;
        //   }
        //   m->flags |= MOD_INIT_B;
        //   m->flags &= ~MOD_SETUP;
        //   return bootret;

        // c:2208 — modname_ok(name)
        if modname_ok(name) == 0 {
            // c:2210 — zerr if !silent (silent flag not threaded yet)
            return false;
        }
        crate::ported::signals::queue_signals(); // c:2218
                                                 // c:2219 — find_module(name, FINDMOD_ALIASP)
        if !self.modules.contains_key(name) {
            // c:2219 — !m branch: static-link path can't dlopen.
            unqueue_signals(); // c:2222
            return false; // c:2223 return 1
        }

        // c:2249 — if (MOD_SETUP) return 0;
        let flags = self.modules.get(name).unwrap().node.flags;
        if (flags & MOD_SETUP) != 0 {
            unqueue_signals(); // c:2250
            return true; // c:2251 return 0
        }
        // c:2253-2257 —
        //   if (m->node.flags & MOD_UNLOAD)
        //       m->node.flags &= ~MOD_UNLOAD;
        //   else if ((m->node.flags & MOD_LINKED) ? m->u.linked
        //                                         : m->u.handle) {
        //       unqueue_signals();
        //       return 0;
        //   }
        // The union read is real now: load assigns m.linked at c:2289,
        // so already-loaded modules early-return here while
        // pre-registered-but-unloaded ones (linked: None) fall through.
        if (flags & MOD_UNLOAD) != 0 {
            self.modules.get_mut(name).unwrap().node.flags &= !MOD_UNLOAD;
        } else if {
            let m = self.modules.get(name).unwrap();
            if (flags & MOD_LINKED) != 0 {
                m.linked.is_some() // c:2255 m->u.linked
            } else {
                m.handle.is_some() // c:2255 m->u.handle
            }
        } {
            unqueue_signals(); // c:2256
            return true; // c:2257 return 0
        }
        // c:2259-2262 — circular-dependency detection.
        if (flags & MOD_BUSY) != 0 {
            unqueue_signals(); // c:2260
            crate::ported::utils::zerr(&format!(
                "circular dependencies for module ;{}",
                name
            ));
            return false; // c:2262 return 1
        }
        self.modules.get_mut(name).unwrap().node.flags |= MOD_BUSY; // c:2264

        // c:2269-2277 — recurse into m->deps.
        let deps_snapshot: Vec<String> = self
            .modules
            .get(name)
            .and_then(|m| m.deps.as_ref())
            .map(|d| d.iter().cloned().collect())
            .unwrap_or_default();
        for dep in &deps_snapshot {
            if !self.load_module(dep) {
                // c:2272 — return 1 on dep failure
                self.modules.get_mut(name).unwrap().node.flags &= !MOD_BUSY; // c:2273
                unqueue_signals(); // c:2274
                return false; // c:2275 return 1
            }
        }
        self.modules.get_mut(name).unwrap().node.flags &= !MOD_BUSY; // c:2278

        // c:2279-2304 — `if (!m->u.handle)` setup branch. The union
        // read means "no live binding yet" — neither handle (DSO) nor
        // linked (static) assigned. A deferred-unload reload re-enters
        // with m.linked still set and skips straight to boot, like C.
        let needs_setup = {
            let m = self.modules.get(name).unwrap();
            m.handle.is_none() && m.linked.is_none() // c:2279 !m->u.handle
        };
        if needs_setup {
            // c:2281 — `linked = module_linked(name)`: every zshrs
            // module is statically linked, so the lookup always hits;
            // callbacks dispatch by name (839f32249b), the record
            // carries the name like C's linkedmod.
            // c:2289-2291 — `m->u.linked = linked;
            //                m->node.flags |= MOD_SETUP | MOD_LINKED;`
            if let Some(m) = self.modules.get_mut(name) {
                m.linked = Some(Box::new(linkedmod {
                    name: name.to_string(),
                    setup: None,
                    features: None,
                    enables: None,
                    boot: None,
                    cleanup: None,
                    finish: None,
                }));
                m.node.flags |= MOD_SETUP | MOD_LINKED;
            }
            // c:2293 — setup_module(m). Routes through the dispatcher
            // (839f32249b) to per-module setup_(m). Most modules return
            // 0; some initialise module-private state.
            if setup_module(self, name) != 0 {
                // c:2294-2301 — failure: finish, clear handles, return 1.
                //   else m->u.linked = NULL;  (c:2298)
                let _ = finish_module(self, name);
                if let Some(m) = self.modules.get_mut(name) {
                    m.linked = None; // c:2298
                    m.node.flags &= !MOD_SETUP;
                }
                unqueue_signals(); // c:2300
                return false; // c:2301 return 1
            }
            // c:2303 — `m->flags |= MOD_INIT_S;`
            self.modules.get_mut(name).unwrap().node.flags |= MOD_INIT_S;
        }
        // c:2305 — `m->flags |= MOD_SETUP;`
        self.modules.get_mut(name).unwrap().node.flags |= MOD_SETUP;

        // c:Src/Modules/system.c:902,904 + zsh/mapfile — SPECIALPMDEF
        // entries get added to paramtab via the module's feature
        // dispatch (enables_ → handlefeatures → addparam). zshrs's
        // vm_helper::init_partab_params skips zmodload-gated names to
        // avoid them appearing before explicit load (bug #69). Re-seed
        // here once boot completes. This is a Rust-only bridge — no
        // direct C counterpart since C's handlefeatures runs implicitly
        // inside do_boot_module → enables_module.
        for nm in crate::vm_helper::module_gated_params_for(name) {
            crate::vm_helper::seed_partab_param(nm);
        }

        // c:2306 — `bootret = do_boot_module(m, enablesarr, silent);`
        // The Rust do_boot_module routes through boot_module dispatcher
        // (b474b62898) to the per-module boot_(m) — the real partab
        // and bintab installations land here.
        let bootret = do_boot_module(self, name, None, 0);
        if bootret == 1 {
            // c:2306-2315 — boot failure: cleanup + finish + clear, return 1.
            //   else m->u.linked = NULL;  (c:2312)
            let _ = cleanup_module(self, name);
            let _ = finish_module(self, name);
            if let Some(m) = self.modules.get_mut(name) {
                m.linked = None; // c:2312
                m.node.flags &= !MOD_SETUP;
            }
            unqueue_signals(); // c:2314
            return false; // c:2315 return 1
        }
        // c:2317-2318 — `m->flags |= MOD_INIT_B; m->flags &= ~MOD_SETUP;`
        if let Some(m) = self.modules.get_mut(name) {
            m.node.flags |= MOD_INIT_B;
            m.node.flags &= !MOD_SETUP;
        }
        unqueue_signals(); // c:2319
        true // c:2320 return bootret (0)
    }

    // Backend handler for zmodload -u                                       // c:2813
    /// Port of `int unload_module(Module m)` from `Src/module.c:2817`.
    /// C body: resolve `MOD_ALIAS` via `find_module(FINDMOD_ALIASP)`;
    /// if `MOD_INIT_S` set and !`MOD_UNLOAD`, call `do_cleanup_module`.
    /// Clear `MOD_INIT_B|MOD_INIT_S`. If a `wrapper` is present, set
    /// `MOD_UNLOAD` and bail (deferred). Else clear `MOD_UNLOAD` and
    /// call `m->u.linked->finish(m)` or `finish_module(m)` depending
    /// on `MOD_LINKED`. Finally walk `m->deps` and unload modules
    /// that were tagged `MOD_UNLOAD` when the last dependent dies.
    /// WARNING: param names don't match C — Rust=(name) vs C=(m)
    pub fn unload_module(&mut self, name: &str) -> bool {
        // c:2812 — faithful port of the static-link control flow.
        // C body c:2818-2913:
        //   if (m->flags & MOD_ALIAS) { resolve via find_module }
        //   if (MOD_INIT_S && !MOD_UNLOAD && do_cleanup_module(m))
        //       return 1;
        //   m->flags &= ~(MOD_INIT_B | MOD_INIT_S);
        //   del = m->flags & MOD_UNLOAD;
        //   if (m->wrapper) { m->flags |= MOD_UNLOAD; return 0; }
        //   m->flags &= ~MOD_UNLOAD;
        //   if (MOD_LINKED) { if (u.linked) { u.linked->finish(m);
        //                                     u.linked = NULL; } }
        //   else { if (u.handle) { finish_module(m);
        //                          u.handle = NULL; } }
        //   if (del && m->deps) { /* deferred dep walk */ }
        //   if (m->autoloads && firstnode(m->autoloads))
        //       autofeatures("zsh", name, hlinklist2array(autoloads),
        //                    0, FEAT_IGNORE);
        //   else if (!m->deps) delete_module(m);
        //   return 0;

        // c:2819-2823 — alias resolve. Mutate the lookup name, not
        // the live record (alias chases the target).
        let mut target_name = name.to_string();
        let needs_alias_chase = self
            .modules
            .get(&target_name)
            .map(|m| (m.node.flags & MOD_ALIAS) != 0)
            .unwrap_or(false);
        if needs_alias_chase {
            target_name = match self
                .modules
                .get(name)
                .and_then(|m| m.alias.clone())
            {
                Some(a) => a,
                None => return false, // c:2821-2822 — alias target gone.
            };
        }

        // c:2831-2834 — cleanup if booted.
        let (init_s, unload_flag) = match self.modules.get(&target_name) {
            Some(m) => (
                (m.node.flags & MOD_INIT_S) != 0,
                (m.node.flags & MOD_UNLOAD) != 0,
            ),
            None => return false, // c:2820-2822 fall-through to !m
        };
        if init_s && !unload_flag {
            // c:2833 — `do_cleanup_module` is `cleanup_module` for the
            // MOD_LINKED branch (static-link path always).
            if cleanup_module(self, &target_name) != 0 {
                return false; // c:2834 cleanup error
            }
        }

        // c:2835 — `m->flags &= ~(MOD_INIT_B | MOD_INIT_S);`
        if let Some(m) = self.modules.get_mut(&target_name) {
            m.node.flags &= !(MOD_INIT_B | MOD_INIT_S);
        }

        // c:2837 — `del = m->flags & MOD_UNLOAD;`
        let del = unload_flag;

        // c:2839-2842 — wrapper deferred-unload path.
        let has_wrapper = self
            .modules
            .get(&target_name)
            .map(|m| m.wrapper != 0)
            .unwrap_or(false);
        if has_wrapper {
            if let Some(m) = self.modules.get_mut(&target_name) {
                m.node.flags |= MOD_UNLOAD; // c:2840
            }
            return true; // c:2841
        }

        // c:2843 — `m->flags &= ~MOD_UNLOAD;`
        if let Some(m) = self.modules.get_mut(&target_name) {
            m.node.flags &= !MOD_UNLOAD;
        }

        // c:2849-2859 — finish hook:
        //   if (m->node.flags & MOD_LINKED) {
        //       if (m->u.linked) {
        //           m->u.linked->finish(m);
        //           m->u.linked = NULL;
        //       }
        //   }
        // Static-link branch routes through finish_module which
        // dispatches to the per-module finish_(m) (839f32249b), gated
        // and cleared exactly like C's u.linked.
        let was_linked = self
            .modules
            .get(&target_name)
            .map(|m| m.linked.is_some())
            .unwrap_or(false);
        if was_linked {
            let _ = finish_module(self, &target_name); // c:2851
            if let Some(m) = self.modules.get_mut(&target_name) {
                m.linked = None; // c:2852
            }
        }

        // c:2861-2902 — deferred dep walk: when del was set, find every
        // dep that has MOD_UNLOAD and check no other live module
        // depends on it, then recursively unload.
        if del {
            let deps_snapshot: Vec<String> = self
                .modules
                .get(&target_name)
                .and_then(|m| m.deps.as_ref())
                .map(|d| d.iter().cloned().collect())
                .unwrap_or_default();
            for dep_name in deps_snapshot {
                let dm_target = match find_module(self, &dep_name, FINDMOD_ALIASP) {
                    Some(n) => n,
                    None => continue, // c:2867 dm == NULL
                };
                let dm_unloading = self
                    .modules
                    .get(&dm_target)
                    .map(|m| (m.node.flags & MOD_UNLOAD) != 0)
                    .unwrap_or(false);
                if !dm_unloading {
                    continue; // c:2870-2871
                }
                // c:2872-2897 — scan every other module's deps for
                // dm_target.nam; bail if any live module still
                // depends on it.
                let still_depended = self.modules.iter().any(|(other_name, other)| {
                    if other_name == &target_name {
                        return false; // c:2884 don't scan ourselves
                    }
                    let other_deps = match other.deps.as_ref() {
                        Some(d) => d,
                        None => return false, // c:2884 no deps to scan
                    };
                    // c:2887-2889 — only scan live modules (MOD_LINKED
                    // set + !MOD_UNLOAD in zshrs's static-link path).
                    if (other.node.flags & MOD_LINKED) == 0
                        || (other.node.flags & MOD_UNLOAD) != 0
                    {
                        return false;
                    }
                    other_deps.iter().any(|d| d == &dm_target)
                });
                if !still_depended {
                    // c:2898-2899 — `unload_module(dm)` recursive.
                    self.unload_module(&dm_target);
                }
            }
        }

        // c:2903-2912 — autoload restore OR delete_module.
        let (has_autoloads, has_deps, autoloads) = match self.modules.get(&target_name) {
            Some(m) => (
                m.autoloads
                    .as_ref()
                    .map(|a| !a.is_empty())
                    .unwrap_or(false),
                m.deps.is_some(),
                m.autoloads
                    .as_ref()
                    .map(|a| a.iter().cloned().collect::<Vec<_>>())
                    .unwrap_or_default(),
            ),
            None => (false, false, Vec::new()),
        };
        if has_autoloads {
            // c:2908-2909 — autofeatures("zsh", m->nam, ..., 0, FEAT_IGNORE)
            autofeatures(self, "zsh", Some(&target_name), &autoloads, 0, FEAT_IGNORE);
        } else if !has_deps {
            // c:2910-2911 — delete_module(m). Inline since the free fn
            // also calls remove + drop.
            self.modules.remove(&target_name);
        }
        true // c:2913 return 0
    }

    /// Check if module is loaded
    pub fn is_loaded(&self, name: &str) -> bool {
        self.modules
            .get(name)
            .map(|m| m.is_loaded())
            .unwrap_or(false)
    }

    /// Whether a module is BOUND — its setup_/boot_ has actually run.
    /// Mirrors the `zmodload -e` existence test (bin_zmodload_exist,
    /// c:Src/module.c:2637): the union slot `m->u.handle` is non-NULL
    /// (handle for dlopen, linked for a static module) and the module is
    /// not mid-unload. Distinct from `is_loaded()` / `module_loaded()`,
    /// which key off `MOD_LINKED` — a flag `register_builtin_modules`
    /// pre-seeds for every statically-compiled module at startup, so it
    /// is true before `zmodload` ever runs. Use this for "was the module
    /// actually loaded by the user" gates.
    pub fn is_bound(&self, name: &str) -> bool {
        self.modules
            .get(name)
            .map(|m| {
                (m.handle.is_some() || m.linked.is_some())
                    && (m.node.flags & MOD_UNLOAD) == 0
            })
            .unwrap_or(false)
    }

    /// List all loaded modules
    pub fn list_loaded(&self) -> Vec<&str> {
        self.modules
            .iter()
            .filter(|(_, m)| m.is_loaded())
            .map(|(name, _)| name.as_str())
            .collect()
    }

    /// List all modules (including unloaded). Returns name + raw
    /// `MOD_*` flag bits — caller can inspect `MOD_UNLOAD` / `MOD_LINKED`
    /// directly (matches C, which exposes `m->node.flags`).
    pub fn list_all(&self) -> Vec<(&str, i32)> {
        self.modules
            .iter()
            .map(|(name, m)| (name.as_str(), m.node.flags))
            .collect()
    }

    // ------- Builtin management (from module.c addbuiltin/deletebuiltin) -------

    /// Register a builtin (from module.c addbuiltin)
    /// Port of `addbuiltin(Builtin b)` from `Src/module.c:409`.
    /// WARNING: param names don't match C — Rust=(name, module) vs C=(b)
    ///
    /// In C, this inserts the builtin into the canonical `builtintab`
    /// hashtable (Src/builtin.c). The real builtin registration lives
    /// in `cmd.rs::BUILTINTAB` and the routing fn here delegates to
    /// the free `addbuiltin` (c:303 port) so the canonical BINF_ADDED
    /// clash gate fires.
    ///
    /// Returns 0 on success, 1 on clash (per C's signature).
    pub fn addbuiltin(&mut self, name: &str, module: &str) -> i32 {
        // c:409
        // Construct a probe builtin matching what add_autobin /
        // setbuiltins build; route through the free addbuiltin which
        // probes createbuiltintable() for BINF_ADDED.
        let mut bn = builtin {
            node: hashnode {
                next: None,
                nam: name.to_string(),
                flags: 0,
            },
            handlerfunc: None,
            minargs: 0,
            maxargs: 0,
            funcid: 0,
            optstr: Some(module.to_string()),
            defopts: None,
        };
        if addbuiltin(&mut bn) != 0 {
            return 1; // c:417 clash
        }
        // On success: record in added_builtins ledger so the
        // module's setbuiltins delete path can find this entry.
        self.added_builtins
            .insert(name.to_string(), BINF_ADDED);
        0 // c:417 OK
    }

    /// Unregister a builtin (from module.c deletebuiltin)
    /// Port of `deletebuiltin(const char *nam)` from `Src/module.c:449`.
    /// Returns 0 on success (entry found + removed from ledger), -1
    /// on miss — matching the canonical free fn (00e6a9ce7e) and
    /// C's deletebuiltin return contract.
    /// WARNING: param names don't match C — Rust=(name, module) vs C=(nam)
    pub fn deletebuiltin(&mut self, name: &str, _module: &str) -> i32 {
        // c:449
        // Route through the free deletebuiltin for the canonical
        // present/absent probe.
        let r = deletebuiltin(name);
        if r == 0 {
            // Drop the ledger entry so setbuiltins's `already_added`
            // probe sees this name as removed.
            self.added_builtins.remove(name);
        }
        r
    }

    /// Register autoloading builtin.
    /// Port of `static int add_autobin(const char *module, const char *bnam,
    /// int flags)` from `Src/module.c:426`. C allocates a `Builtin` node
    /// with `optstr=module`, sets `BINF_AUTOALL` if `FEAT_AUTOALL` is in
    /// `flags`, then calls `addbuiltin(bn)`. On failure, the freshly
    /// allocated node is freed; success returns 0, conflict returns 1
    /// unless `FEAT_IGNORE` masks it.
    /// WARNING: param names don't match C — Rust=(name, module, flags) vs C=(module, bnam, flags)
    pub fn add_autobin(&mut self, name: &str, module: &str, flags: i32) -> i32 {
        // c:426
        // Faithful port of c:426-441:
        //   Builtin bn = zshcalloc(sizeof(*bn));
        //   bn->node.nam = ztrdup(bnam);
        //   bn->optstr = ztrdup(module);
        //   if (flags & FEAT_AUTOALL)
        //       bn->node.flags |= BINF_AUTOALL;
        //   if ((ret = addbuiltin(bn))) {
        //       builtintab->freenode(&bn->node);
        //       if (!(flags & FEAT_IGNORE))
        //           return 1;
        //   }
        //   return 0;
        //
        // Prior port did a ledger-only insert into autoload_builtins
        // without ever calling the canonical addbuiltin. Now constructs
        // the builtin struct and routes through the free addbuiltin
        // (c:303 port) which probes createbuiltintable() for the
        // BINF_ADDED clash gate. Ledger entry preserved on success so
        // resolve_autoload_builtin() / del_autobin's fast path can
        // find it without re-scanning.
        let node_flags: i32 = if (flags & FEAT_AUTOALL as i32) != 0 {
            BINF_AUTOALL as i32 // c:435
        } else {
            0
        };
        let mut bn = builtin {
            // c:431-433 — zshcalloc + populate.
            node: hashnode {
                next: None,
                nam: name.to_string(),
                flags: node_flags,
            },
            handlerfunc: None,
            minargs: 0,
            maxargs: 0,
            funcid: 0,
            optstr: Some(module.to_string()),
            defopts: None,
        };
        // c:436 — `if ((ret = addbuiltin(bn)))`. Clash gate via the
        // canonical builtintab.
        if addbuiltin(&mut bn) != 0 {
            // c:437 — freenode drops the input via Rust's Drop.
            if (flags & FEAT_IGNORE as i32) == 0 {
                return 1; // c:439
            }
            // c:440 — FEAT_IGNORE masks → fall through to ret 0 but
            // don't insert into the ledger (the canonical table has
            // this name already).
            return 0;
        }
        // c:441 success path: register in the autoload ledger.
        self.autoload_builtins
            .insert(name.to_string(), module.to_string());
        0
    }

    // Remove an autoloaded added by add_autobin                             // c:464
    /// Port of `static int del_autobin(const char *module, const char *bnam,
    /// int flags)` from `Src/module.c:464`.
    ///
    /// C body (c:464-478):
    /// ```c
    /// Builtin bn = (Builtin) builtintab->getnode2(builtintab, bnam);
    /// if (!bn) { if(!(flags & FEAT_IGNORE)) return 2; }
    /// else if (bn->node.flags & BINF_ADDED) {
    ///     if (!(flags & FEAT_IGNORE)) return 3;
    /// } else deletebuiltin(bnam);
    /// return 0;
    /// ```
    ///
    /// 2 = "no such builtin", 3 = "real registered builtin (BINF_ADDED) —
    /// can't unload", 0 = success (removed autoload entry).
    /// FEAT_IGNORE masks both error returns.
    ///
    /// zshrs architecture: `builtintab` is static-linked at startup
    /// (`createbuiltintable()` builds an immutable HashMap), so every
    /// entry there is effectively BINF_ADDED. The autoload-only entries
    /// live in `self.autoload_builtins`. The faithful mapping:
    ///   * if name is in `builtintab` → BINF_ADDED → return 3 (or 0 with
    ///     FEAT_IGNORE).
    ///   * else if name is in `autoload_builtins` → remove it, return 0.
    ///   * else → not present → return 2 (or 0 with FEAT_IGNORE).
    /// WARNING: param names don't match C — Rust=(name, flags) vs C=(module, bnam, flags)
    pub fn del_autobin(&mut self, name: &str, flags: i32) -> i32 {
        // c:464
        // Faithful port of c:464-478:
        //   Builtin bn = (Builtin) builtintab->getnode2(builtintab, bnam);
        //   if (!bn) {
        //       if(!(flags & FEAT_IGNORE)) return 2;
        //   } else if (bn->node.flags & BINF_ADDED) {
        //       if (!(flags & FEAT_IGNORE)) return 3;
        //   } else
        //       deletebuiltin(bnam);
        //   return 0;
        //
        // Three distinct return codes: 2 = no such builtin (autoload
        // ledger absent), 3 = real registered (BINF_ADDED set — can't
        // unload via del_auto*), 0 = success.
        //
        // zshrs's createbuiltintable() entries are all BINF_ADDED by
        // construction (the static-linked canonical table), so a hit
        // there always means "real registered" → return 3. The
        // autoload stubs live in self.autoload_builtins; a hit there
        // is the c:475 deletebuiltin path.
        //
        // Now routes through the free deletebuiltin (00e6a9ce7e) for
        // the canonical-table probe so the present/absent contract
        // matches C exactly. Prior port did the probe inline.

        // c:466 — `builtintab->getnode2(builtintab, bnam)`. zshrs's
        // builtintab is split: the immutable createbuiltintable()
        // carries the static flags, the autoload_builtins ledger holds
        // runtime stubs from add_autobin, and added_builtins is the
        // runtime BINF_ADDED bit a module load flips (setbuiltins
        // c:508 probes the same ledger).
        let static_flags: Option<i32> = createbuiltintable().get(name).map(|b| b.node.flags);
        let in_ledger = self.autoload_builtins.contains_key(name);
        // BINF_ADDED equivalent: C sets the bit at addbuiltins time —
        // startup for core builtins, the module's boot_ for module
        // builtins. zshrs's static table folds BOTH in unflagged, so a
        // static hit counts as ADDED only when the name is core (no
        // owning module advertises `b:NAME`) or its owning module is
        // loaded. Runtime loads also land in the added_builtins ledger
        // (setbuiltins c:508 probes the same).
        let added = self.added_builtins.contains_key(name)
            || static_flags
                .map(|f| (f & BINF_ADDED as i32) != 0)
                .unwrap_or(false)
            || (static_flags.is_some() && {
                let mod_names: Vec<String> = self.modules.keys().cloned().collect();
                let mut owner_loaded: Option<bool> = None; // None = core builtin
                'outer: for mn in &mod_names {
                    let mut feats: Vec<String> = Vec::new();
                    if features_module(self, mn, &mut feats) != 0 {
                        continue;
                    }
                    for f in &feats {
                        if f.strip_prefix("b:") == Some(name) {
                            owner_loaded = Some(self.is_loaded(mn));
                            break 'outer;
                        }
                    }
                }
                owner_loaded != Some(false)
            });
        if static_flags.is_none() && !in_ledger {
            // c:467-469 — `if (!bn) { if(!(flags & FEAT_IGNORE)) return 2; }`
            if (flags & FEAT_IGNORE as i32) == 0 {
                return 2; // c:469
            }
        } else if added {
            // c:470-473 — `else if (bn->node.flags & BINF_ADDED)` —
            // a real, live builtin can't be un-autoloaded.
            if (flags & FEAT_IGNORE as i32) == 0 {
                return 3; // c:472
            }
        } else {
            // c:474-475 — `else deletebuiltin(bnam);` — drop the
            // autoload stub from the ledger.
            self.autoload_builtins.remove(name);
        }
        0 // c:477
    }

    /// Set/clear a slice of builtins per `e[]` mask.
    /// Port of `static int setbuiltins(char const *nam, Builtin binl,
    /// int size, int *e)` from `Src/module.c:501`. For each Builtin in
    /// `binl[0..size]`: if `e[n]` is set, add the builtin (skip if
    /// already `BINF_ADDED`); else delete the builtin (skip if not
    /// `BINF_ADDED`). Warnings on clash/already-deleted; returns 1 if
    /// any op failed.
    /// WARNING: param names don't match C — Rust=(module, names, e) vs C=(nam, binl, size, e)
    pub fn setbuiltins(&mut self, module: &str, names: &[&str], e: Option<&[i32]>) -> i32 {
        // c:501
        let mut ret: i32 = 0; // c:503
        for (n, name) in names.iter().enumerate() {
            // c:505 — `for (n = 0; n < size; n++) { Builtin b = &binl[n]; ... }`
            let enable = e
                .map(|arr| arr.get(n).copied().unwrap_or(0)) // c:507 *e++
                .unwrap_or(1);
            let already_added = self.added_builtins.contains_key(*name); // c:508 b->flags & BINF_ADDED
            if enable != 0 {
                // c:507 — `if (e && *e++)` add branch
                if already_added {
                    // c:508-509 — skip already-added.
                    continue;
                }
                // c:510 — `if (addbuiltin(b))` — probe the canonical
                // table for the clash gate. The free fn returns 1
                // when an existing entry already has BINF_ADDED set.
                let mut probe = builtin {
                    node: hashnode {
                        next: None,
                        nam: name.to_string(),
                        flags: 0,
                    },
                    handlerfunc: None,
                    minargs: 0,
                    maxargs: 0,
                    funcid: 0,
                    optstr: Some(module.to_string()),
                    defopts: None,
                };
                if addbuiltin(&mut probe) != 0 {
                    // c:511-513 — `zwarnnam(nam, "name clash...")`
                    zwarnnam(
                        module,
                        &format!("name clash when adding builtin `{}'", name),
                    );
                    ret = 1; // c:513
                } else {
                    // c:515 — `b->node.flags |= BINF_ADDED;`. Mirror
                    // the in-place bit-set with the per-module
                    // ledger flip.
                    self.added_builtins.insert(name.to_string(), BINF_ADDED);
                }
            } else {
                // c:517-525 — del branch.
                if !already_added {
                    // c:518-519 — skip already-not-added.
                    continue;
                }
                // c:520 — `if (deletebuiltin(b->node.nam))`. Free fn
                // returns -1 on miss; treat any non-zero as the
                // "already deleted" condition.
                if deletebuiltin(name) != 0 {
                    // c:521-523 — `zwarnnam(nam, "builtin `%s' already
                    // deleted")`.
                    zwarnnam(
                        module,
                        &format!("builtin `{}' already deleted", name),
                    );
                    ret = 1; // c:523
                } else {
                    // c:524 — `b->node.flags &= ~BINF_ADDED;`
                    self.added_builtins.remove(*name);
                }
            }
        }
        ret // c:528
    }

    // ------- Condition management (from module.c addconddef/deleteconddef) -------

    /// Register a condition (from module.c addconddef)
    /// Port of `addconddef(Conddef c)` from `Src/module.c:703`.
    /// WARNING: param names don't match C — Rust=(name, module) vs C=(c)
    ///
    /// Like `addbuiltin`, the real registration lives in
    /// `cond.rs::CONDTAB`; the routing fn here delegates to the free
    /// `addconddef` (4304-port) so the canonical name+infix-flag
    /// clash gate fires.
    ///
    /// Returns 0 on success, 1 on clash (matches C's signature).
    pub fn addconddef(&mut self, name: &str, module: &str) -> i32 {
        // c:703
        // Construct a probe conddef matching what add_autocond /
        // setconddefs build. handler/min/max/condid stay default
        // (filled in by the actual module on load).
        let cd = conddef {
            next: None,
            name: name.to_string(),
            flags: 0, // c:703 entries here aren't infix by default
            handler: None,
            min: 0,
            max: 0,
            condid: 0,
            module: Some(module.to_string()),
        };
        // c:705-715 — addconddef walks CONDTAB for clash, replaces
        // autoload entries, prepends on success. Returns 0/1.
        addconddef(cd)
    }

    /// Unregister a condition (from module.c deleteconddef)
    /// Port of `deleteconddef(Conddef c)` from `Src/module.c:724`.
    /// Returns 0 on success (entry was found + removed), -1 on miss.
    /// Mirrors C's deleteconddef return contract.
    /// WARNING: param names don't match C — Rust=(name, module) vs C=(c)
    pub fn deleteconddef(&mut self, name: &str, _module: &str) -> i32 {
        // c:724
        // Build a probe conddef and call the free deleteconddef
        // (4304-port) which walks CONDTAB by (name, infix-flag) for
        // identity. infix=0 here matches the prefix-style default
        // used by the auto* registrations.
        let probe = conddef {
            next: None,
            name: name.to_string(),
            flags: 0,
            handler: None,
            min: 0,
            max: 0,
            condid: 0,
            module: None,
        };
        deleteconddef(&probe)
    }

    /// Get condition definition (from module.c getconddef)
    /// Port of `getconddef(int inf, const char *name, int autol)` from `Src/module.c:648`.
    /// WARNING: param names don't match C — Rust=(name) vs C=(inf, name, autol)
    ///
    /// Returns the autoload mapping if any. C consults the canonical
    /// `condtab` first; the autoload table is the fallback.
    pub fn getconddef(&self, name: &str) -> Option<&str> {
        self.autoload_conditions.get(name).map(|s| s.as_str())
    }

    /// Register autoloading condition.
    /// Port of `static int add_autocond(const char *module, const char
    /// *cnam, int flags)` from `Src/module.c:792`. C body allocates a
    /// `Conddef`, copies name/module, sets `CONDF_INFIX` if
    /// `FEAT_INFIX` and `CONDF_AUTOALL` if `FEAT_AUTOALL`, then calls
    /// `addconddef(c)`. On addconddef failure (already exists) the
    /// node is freed; returns 1 unless `FEAT_IGNORE`.
    /// WARNING: param names don't match C — Rust=(name, module, flags) vs C=(module, cnam, flags)
    pub fn add_autocond(&mut self, name: &str, module: &str, flags: i32) -> i32 {
        // c:792
        // Faithful port of c:792-810:
        //   Conddef c = zshcalloc(sizeof(*c));
        //   c->name = ztrdup(cnam);
        //   c->module = ztrdup(module);
        //   c->flags = (flags & FEAT_INFIX) ? CONDF_INFIX : 0;
        //   if (flags & FEAT_AUTOALL) c->flags |= CONDF_AUTOALL;
        //   if (addconddef(c)) {
        //       zsfree(c->name); zsfree(c->module); zfree(c, sizeof(*c));
        //       if (!(flags & FEAT_IGNORE)) return 1;
        //   }
        //   return 0;
        //
        // Prior port did a ledger-only insert into autoload_conditions
        // without ever touching the canonical CONDTAB. Now constructs
        // the conddef struct and routes through the free addconddef
        // (1395acf3a7 dependency) which walks CONDTAB for clashes,
        // replaces autoload entries, and prepends on success.
        let mut cflags: i32 = if (flags & FEAT_INFIX) != 0 {
            CONDF_INFIX // c:799
        } else {
            0
        };
        if (flags & FEAT_AUTOALL) != 0 {
            cflags |= CONDF_AUTOALL; // c:801
        }
        // c:796-803 — populate the conddef record. `handler` is None
        // because autoload stubs don't carry the dispatch fn until
        // the module loads and addconddef replaces the entry.
        let cd = conddef {
            next: None,
            name: name.to_string(),
            flags: cflags,
            handler: None,
            min: 0,
            max: 0,
            condid: 0,
            module: Some(module.to_string()),
        };
        // c:804 — addconddef(c).
        if addconddef(cd) != 0 {
            // c:805-807 — zsfree/zfree happen via Rust drop.
            if (flags & FEAT_IGNORE) == 0 {
                return 1; // c:810
            }
        }
        // Keep the ledger entry too so resolve_autoload_condition()
        // and del_autocond's fast path can find it without re-scanning
        // CONDTAB.
        self.autoload_conditions
            .insert(name.to_string(), module.to_string());
        0 // c:812
    }

    /// Port of `static int del_autocond(const char *modnam, const char *cnam,
    /// int flags)` from `Src/module.c:819`.
    ///
    /// C body (c:819-835):
    /// ```c
    /// Conddef cd = getconddef((flags & FEAT_INFIX) ? 1 : 0, cnam, 0);
    /// if (!cd) { if (!(flags & FEAT_IGNORE)) return 2; }
    /// else if (cd->flags & CONDF_ADDED) {
    ///     if (!(flags & FEAT_IGNORE)) return 3;
    /// } else deleteconddef(cd);
    /// return 0;
    /// ```
    ///
    /// 2 = "no such condition", 3 = "registered condition (CONDF_ADDED) —
    /// can't unload", 0 = success. FEAT_IGNORE masks both error returns.
    /// WARNING: param names don't match C — Rust=(name, flags) vs C=(modnam, cnam, flags)
    pub fn del_autocond(&mut self, name: &str, flags: i32) -> i32 {
        // c:819
        // Faithful port of c:819-835:
        //   Conddef cd = getconddef((flags & FEAT_INFIX) ? 1 : 0,
        //                           cnam, 0);
        //   if (!cd) {
        //       if (!(flags & FEAT_IGNORE)) return 2;
        //   } else if (cd->flags & CONDF_ADDED) {
        //       if (!(flags & FEAT_IGNORE)) return 3;
        //   } else
        //       deleteconddef(cd);
        //   return 0;
        //
        // Prior port skipped CONDTAB entirely; checked the ledger
        // only. Now routes through getconddef (1395acf3a7) which
        // walks CONDTAB filtered by infix-flag, then deleteconddef
        // (free fn) which removes the matched entry.
        let inf = if (flags & FEAT_INFIX) != 0 { 1 } else { 0 };
        // c:821 — `getconddef(inf, cnam, 0)`.
        let cd = getconddef(inf, name, 0, self);
        match cd {
            None => {
                // c:823-825 — !cd: return 2 unless FEAT_IGNORE.
                if (flags & FEAT_IGNORE) == 0 {
                    return 2; // c:825
                }
                0 // c:834
            }
            Some(ref entry) if (entry.flags & CONDF_ADDED) != 0 => {
                // c:826-828 — CONDF_ADDED set: real registered
                // condition, can't unload via del_auto*. Return 3
                // unless FEAT_IGNORE.
                if (flags & FEAT_IGNORE) == 0 {
                    return 3; // c:828
                }
                0
            }
            Some(ref entry) => {
                // c:831-832 — deleteconddef(cd); drop ledger too.
                let _ = deleteconddef(entry);
                self.autoload_conditions.remove(name);
                0 // c:834
            }
        }
    }

    // ------- Hook management lives in the file-static free ported above ------
    //
    // C `hooktab` (Src/module.c:843) is a file-static `Hookdef` linked
    // list, NOT a member of `ModuleTable` (which is the `modulestab`
    // HashTable of Module nodes at c:Modules/zmodload.c:32). Hook ops
    // are free ported operating on the file-static `hooktab` (above):
    // `gethookdef`, `addhookdef`, `addhookdefs`, `deletehookdef`,
    // `deletehookdefs`, `addhookdeffunc`, `addhookfunc`,
    // `deletehookdeffunc`, `deletehookfunc`, `runhookdef`.

    // ------- Parameter management (from module.c addparamdef/deleteparamdef) -------

    /// Add or remove sets of parameters; same shape as `setbuiltins`.
    /// Port of `static int setparamdefs(char const *nam, Paramdef d,
    /// int size, int *e)` from `Src/module.c:1170`. For each Paramdef
    /// in `d[0..size]`: if `e[n]` is set and `d->pm` is null, add the
    /// param via `addparamdef(d)`; if `e[n]` is clear and `d->pm` is
    /// non-null, remove via `deleteparamdef(d)`. Warnings on
    /// error/already-deleted; returns 1 if any op failed.
    /// WARNING: param names don't match C — Rust=(module, names, e) vs C=(nam, d, size, e)
    pub fn setparamdefs(&mut self, module: &str, names: &[&str], e: Option<&[i32]>) -> i32 {
        // c:1165
        // Faithful port of c:1165-1192. Prior Rust port skipped the
        // diagnostics + ret-tracking C does on failure:
        //
        //   if (e && *e++) {           // add branch
        //       if (d->pm) continue;   // already registered
        //       if (addparamdef(d)) {
        //           zwarnnam(nam,
        //               "error when adding parameter `%s'", d->name);
        //           ret = 1;
        //       }
        //   } else {                   // del branch
        //       if (!d->pm) continue;  // not registered
        //       if (deleteparamdef(d)) {
        //           zwarnnam(nam,
        //               "parameter `%s' already deleted", d->name);
        //           ret = 1;
        //       }
        //   }
        //
        // Maps to the autoload_params ledger (zshrs-only structural
        // equivalent of d->pm presence). Diagnostics now fire when
        // the ledger insert/remove conflicts with the C-side
        // contract (probe + retry on the canonical paramtab to
        // observe the same clash behaviour addparamdef would).
        use crate::ported::params::paramtab;
        let mut ret: i32 = 0; // c:1167
        for (n, name) in names.iter().enumerate() {
            // c:1169 — `while (size--)`
            let enable = e
                .map(|arr| arr.get(n).copied().unwrap_or(0)) // c:1170 *e++
                .unwrap_or(1);
            // c:1171 / c:1180 — `if (d->pm)` / `if (!d->pm)`.
            // Static-link analog: autoload_params contains the name
            // when the module has registered it as a paramdef.
            let already = self.autoload_params.contains_key(*name);
            if enable != 0 {
                // c:1170 add branch
                if already {
                    continue; // c:1172
                }
                // c:1175 — `if (addparamdef(d))`. Faithful path:
                // probe the canonical paramtab for a clash; if the
                // name already exists with a non-MOD-derived param,
                // mirror addparamdef's failure.
                let canonical_clash = paramtab()
                    .read()
                    .ok()
                    .map(|t| t.contains_key(*name))
                    .unwrap_or(false);
                if canonical_clash {
                    // c:1176-1178 — `zwarnnam('error when adding ...')`.
                    zwarnnam(
                        module,
                        &format!("error when adding parameter `{}'", name),
                    );
                    ret = 1; // c:1177
                    continue;
                }
                // c:1181 — register the ledger entry on success.
                self.autoload_params
                    .insert(name.to_string(), module.to_string());
            } else {
                // c:1179 del branch
                if !already {
                    continue; // c:1181
                }
                // c:1184 — `if (deleteparamdef(d))`. With the ledger
                // hit, the canonical removal succeeds; emit the
                // C-equivalent diagnostic only if the paramtab probe
                // says the entry got tampered with externally
                // (already-deleted from paramtab while still on the
                // ledger).
                let canonical_present = paramtab()
                    .read()
                    .ok()
                    .map(|t| t.contains_key(*name))
                    .unwrap_or(false);
                self.autoload_params.remove(*name);
                if !canonical_present {
                    // c:1185-1187 — `parameter `%s' already deleted`.
                    zwarnnam(
                        module,
                        &format!("parameter `{}' already deleted", name),
                    );
                    ret = 1; // c:1186
                }
            }
        }
        ret // c:1191
    }

    /// Register autoloading parameter.
    /// Port of `static int add_autoparam(const char *module, const char
    /// *pnam, int flags)` from `Src/module.c:1198`. C body:
    /// `checkaddparam()` clash check (returns 2 if `-i`'d), then
    /// `setsparam(pnam, module)` creating the param with `PM_AUTOLOAD`
    /// (+ `PM_AUTOALL` if `FEAT_AUTOALL`). `queue_signals`/`noerrs=2`
    /// bracket so the setsparam doesn't echo errors out.
    /// WARNING: param names don't match C — Rust=(name, module, flags) vs C=(module, pnam, flags)
    pub fn add_autoparam(&mut self, name: &str, module: &str, flags: i32) -> i32 {
        // c:1198
        // Faithful port of c:1198-1228 routes through the free
        // add_autoparam fn (293c041e2f) which already does the full
        // checkaddparam + setsparam + PM_AUTOLOAD (+PM_AUTOALL) +
        // noerrs/queue_signals bracket.
        //
        // Prior method was a ledger-only HashMap insert that did the
        // queue_signals dance without ever calling setsparam — so
        // PM_AUTOLOAD never landed on the canonical paramtab entry,
        // and `typeset +` wouldn't see the 'undefined NAME' shape.
        //
        // The free fn returns 0 / -1 (vs C's 0 / 1 / -1) — preserve
        // the existing method-level contract by mapping -1 → 1 below.
        let r = add_autoparam(module, name, flags); // c:1198
        if r != 0 {
            // c:1213-1219 — error: 2 (FEAT_IGNORE soft-fail) maps to
            // 0 here per the prior method contract.
            if (flags & FEAT_IGNORE) != 0 {
                return 0;
            }
            return r; // -1 → -1
        }
        // c:1218-1221 success: also keep the ledger entry up so the
        // resolve_autoload_param fast path stays consistent.
        self.autoload_params
            .insert(name.to_string(), module.to_string());
        0
    }

    /// Port of `static int del_autoparam(const char *modnam, const char *pnam,
    /// int flags)` from `Src/module.c:1240`.
    ///
    /// C body (c:1240-1255):
    /// ```c
    /// Param pm = (Param) gethashnode2(paramtab, pnam);
    /// if (!pm) { if (!(flags & FEAT_IGNORE)) return 2; }
    /// else if (!(pm->node.flags & PM_AUTOLOAD)) {
    ///     if (!(flags & FEAT_IGNORE)) return 3;
    /// } else unsetparam_pm(pm, 0, 1);
    /// return 0;
    /// ```
    ///
    /// 2 = "no such param", 3 = "real param (not autoload) — can't
    /// unload", 0 = success. FEAT_IGNORE masks both error returns.
    /// WARNING: param names don't match C — Rust=(name, flags) vs C=(modnam, pnam, flags)
    pub fn del_autoparam(&mut self, name: &str, flags: i32) -> i32 {
        // c:1234
        // Faithful port of c:1234-1248 routes through the free
        // del_autoparam fn (293c041e2f) which does the canonical
        // paramtab probe + PM_AUTOLOAD gate + unsetparam_pm.
        //
        // Prior method walked paramtab directly and emulated the
        // PM_AUTOLOAD gate inline; routing through the free fn keeps
        // the logic single-sourced so PM_AUTOLOAD semantics evolve
        // in one place. Ledger cleanup preserved on the success path.
        let r = del_autoparam("", name, flags); // c:1234 (modnam unused per UNUSED())
        if r == 0 {
            // c:1246 — `unsetparam_pm` already ran via the free fn;
            // also drop the ledger entry so resolve_autoload_param
            // stops returning the stale module name.
            self.autoload_params.remove(name);
        }
        r
    }

    // ------- Feature enable/disable (from module.c features_/enables_) -------

    /// Enable a feature (from module.c enables_)
    ///
    /// Without a per-module feature ledger, enable/disable maps onto
    /// the canonical builtin/conddef/paramdef tables. Returns true if
    /// the module itself is registered. The actual per-feature
    /// enabled-bit lives on the canonical record (e.g. `Builtin.flags`
    /// `BINF_DISABLED`).
    pub fn enable_feature(&mut self, module: &str, _name: &str) -> bool {
        self.modules.contains_key(module)
    }

    /// Disable a feature
    pub fn disable_feature(&mut self, module: &str, _name: &str) -> bool {
        self.modules.contains_key(module)
    }

    /// List feature *names* of a module (from module.c features_).
    /// Without a per-module ledger, this returns an empty list — C
    /// computes feature names by walking the canonical tables for
    /// entries that name the given module. Callers that care use
    /// `features_module`/`features_` directly.
    pub fn list_features(&self, _module: &str) -> Vec<String> {
        Vec::new()
    }

    /// Check if a module is linked (statically compiled) (from module.c module_linked)
    /// Port of `module_linked(char const *name)` from `Src/module.c:385`.
    pub fn module_linked(&self, name: &str) -> bool {
        self.modules.contains_key(name)
    }

    /// Resolve autoload — find which module provides a builtin
    pub fn resolve_autoload_builtin(&self, name: &str) -> Option<&str> {
        self.autoload_builtins.get(name).map(|s| s.as_str())
    }

    /// Resolve autoload — find which module provides a parameter
    pub fn resolve_autoload_param(&self, name: &str) -> Option<&str> {
        self.autoload_params.get(name).map(|s| s.as_str())
    }

    /// Ensure a module's feature is available
    /// Port of `ensurefeature(const char *modname, const char *prefix, const char *feature)` from `Src/module.c:3415`.
    /// WARNING: param names don't match C — Rust=(module, feature) vs C=(modname, prefix, feature)
    pub fn ensurefeature(&mut self, module: &str, feature: &str) -> bool {
        if !self.is_loaded(module) {
            self.load_module(module);
        }
        self.is_loaded(module)
    }
}

/// Module lifecycle callbacks (from module.c setup_/getrandom_buffer/cleanup_/finish_)
/// Lifecycle hooks every module must implement.
/// Port of the `setup_`/`features_`/`enables_`/`getrandom_buffer`/`cleanup_`
/// /`finish_` entry points every C module exposes (Src/module.c
/// lines 306-345 illustrate the canonical no-op set). Rust
/// modules implement this trait directly.
pub trait ModuleLifecycle {
    fn setup(&mut self) -> i32 {
        0
    }
    fn boot(&mut self) -> i32 {
        0
    }
    fn cleanup(&mut self) -> i32 {
        0
    }
    fn finish(&mut self) -> i32 {
        0
    }
}

/// Port of `getmathfunc(const char *name, int autol)` from `Src/module.c:1283`.
///
/// C body: linear-search `mathfuncs` for `name`; if found and `autol`
/// is true and the entry is autoloadable, demand-load via
/// `ensurefeature("f:", name)`. Returns the resolved entry or NULL.
///
/// Rust port returns `Some(module_name)` on hit, `None` on miss.
///
/// Faithful port of c:1283-1306:
/// ```c
/// MathFunc
/// getmathfunc(const char *name, int autol)
/// {
///     MathFunc p, q = NULL;
///     for (p = mathfuncs; p; q = p, p = p->next)
///         if (!strcmp(name, p->name)) {
///             if (autol && p->module && !(p->flags & MFF_USERFUNC)) {
///                 char *n = dupstring(p->module);
///                 int flags = p->flags;
///                 removemathfunc(q, p);
///                 (void)ensurefeature(n, "f:",
///                                     (flags & MFF_AUTOALL) ? NULL : name);
///                 p = getmathfunc(name, 0);
///                 if (!p) {
///                     zerr("autoloading module %s failed to define math function: %s", n, name);
///                 }
///             }
///             return p;
///         }
///     return NULL;
/// }
/// ```
///
/// C walks `mathfuncs` (file-static linked list) — Rust walks
/// MATHFUNCS (same shape, Vec). The MFF_USERFUNC filter
/// (`functions -M` user-defined math) keeps user fns from being
/// autoloaded out from under the caller. The autoload arm:
///   1. snapshot module name + flags
///   2. removemathfunc(name) — drop the autoload stub
///   3. ensurefeature(module, "f:", AUTOALL?NULL:name) — load real
///   4. re-query mathfuncs (autol=0) — return the loaded entry
///   5. zerr if still missing (load failed to populate the table)
/// WARNING: param names don't match C — Rust=(table, name, autol) vs C=(name, autol)
pub fn getmathfunc(table: &mut modulestab, name: &str, autol: i32) -> Option<String> {
    // c:1283
    // c:1287-1288 — `for (p = mathfuncs; p; q = p, p = p->next)`:
    // walk MATHFUNCS for the first name match. Snapshot under the
    // lock so we can release before mutating (autoload arm calls
    // ensurefeature which can re-enter MATHFUNCS via addmathfunc).
    let hit: Option<(String, i32, bool)> = {
        let tab = MATHFUNCS.lock().unwrap();
        tab.iter().find_map(|p| {
            if p.name == name {
                Some((
                    p.module.clone().unwrap_or_default(),
                    p.flags,
                    p.module.is_some(),
                ))
            } else {
                None
            }
        })
    };
    let (module, flags, has_module) = match hit {
        Some(t) => t,
        None => return None, // c:1306 `return NULL;`
    };
    // c:1289 — `if (autol && p->module && !(p->flags & MFF_USERFUNC))`.
    if autol != 0 && has_module && (flags & MFF_USERFUNC) == 0 {
        // c:1290-1291 — snapshot already done above.
        // c:1293 — `removemathfunc(q, p)`: drop the stub before load.
        removemathfunc(name);
        // c:1295-1296 — `ensurefeature(n, "f:", AUTOALL?NULL:name)`.
        let feature_arg = if (flags & crate::ported::zsh_h::MFF_AUTOALL) != 0 {
            None
        } else {
            Some(name)
        };
        let _ = ensurefeature(table, &module, "f:", feature_arg);
        // c:1298 — `p = getmathfunc(name, 0);` recurse w/o autol.
        // EXISTENCE check: C zerrs only when the re-lookup returns
        // NULL. A freshly registered real entry has module == NULL
        // (mftab entries, zsh.h:133 NUMMATHFUNC), so mapping the hit
        // through `p.module.clone()` (None for real entries) wrongly
        // reported a successful load as "failed to define". Mirror the
        // non-autoload tail below: empty string marks a module-less
        // hit.
        let after = {
            let tab = MATHFUNCS.lock().unwrap();
            tab.iter().find_map(|p| {
                if p.name == name {
                    Some(p.module.clone().unwrap_or_default())
                } else {
                    None
                }
            })
        };
        // c:1299-1301 — `if (!p) zerr(...)`.
        if after.is_none() {
            crate::ported::utils::zerr(&format!(
                "autoloading module {} failed to define math function: {}",
                module, name
            ));
        }
        return after; // c:1303 `return p;`
    }
    // c:1303 — non-autoload (or user-fn) hit: return the entry.
    if has_module {
        Some(module)
    } else {
        // User-fn entries (`functions -M`) have no module — return
        // an empty string so the caller still observes the hit.
        Some(String::new())
    }
}

/// Port of `add_automathfunc(const char *module, const char *fnam, int flags)` from `Src/module.c:1410`.
///
/// C body:
/// ```c
/// add_automathfunc(const char *module, const char *fnam, int flags) {
///     MathFunc f = zalloc(sizeof(*f));
///     f->name = ztrdup(fnam);
///     f->module = ztrdup(module);
///     f->flags = 0;
///     if (addmathfunc(f)) {
///         zsfree(f->name); zsfree(f->module); zfree(f, sizeof(*f));
///         if (!(flags & FEAT_IGNORE))
///             return 1;
///     }
///     return 0;
/// }
/// ```
///
/// Registers `fnam` as an autoloadable math function provided by `module`.
/// WARNING: param names don't match C — Rust=(table, module, fnam, flags) vs C=(module, fnam, flags)
pub fn add_automathfunc(table: &mut modulestab, module: &str, fnam: &str, flags: i32) -> i32 {
    // c:1410
    // Faithful port of c:1410-1429:
    //   MathFunc f = zalloc(sizeof(*f));
    //   f->name = ztrdup(fnam);
    //   f->module = ztrdup(module);
    //   f->flags = 0;
    //   if (addmathfunc(f)) {
    //       zsfree(f->name); zsfree(f->module); zfree(f, sizeof(*f));
    //       if (!(flags & FEAT_IGNORE)) return 1;
    //   }
    //   return 0;
    //
    // Prior port did ledger-only autoload_mathfuncs.insert without
    // ever touching the canonical MATHFUNCS table. Now constructs
    // the mathfunc struct and routes through the free addmathfunc
    // (c:1313) which walks MATHFUNCS for clashes and replaces
    // autoloadable entries.
    let f = mathfunc {
        next: None,
        name: fnam.to_string(),
        flags: 0, // c:1417 — autoload entries don't carry MFF_ADDED
        nfunc: None,
        sfunc: None,
        module: Some(module.to_string()),
        minargs: 0,
        maxargs: 0,
        funcid: 0,
    };
    // c:1420 — `if (addmathfunc(f))` clash gate.
    if addmathfunc(f) != 0 {
        // c:1421-1424 — free happens via Rust drop on the returned-
        // by-value f going out of scope.
        if (flags & FEAT_IGNORE) == 0 {
            return 1; // c:1426
        }
        // c:1427 — FEAT_IGNORE: fall through to success but skip
        // the ledger insert (the canonical table already has this).
        return 0;
    }
    // c:1429 success path: register in the autoload ledger.
    table
        .autoload_mathfuncs
        .insert(fnam.to_string(), module.to_string());
    0
}

/// Port of `del_automathfunc(UNUSED(const char *modnam), const char *fnam, int flags)` from `Src/module.c:1436`.
///
/// C body:
/// ```c
/// del_automathfunc(UNUSED(const char *modnam), const char *fnam, int flags) {
///     MathFunc f = getmathfunc(fnam, 0);
///     if (!f) {
///         if (!(flags & FEAT_IGNORE)) return 2;
///     } else if (f->flags & MFF_ADDED) {
///         if (!(flags & FEAT_IGNORE)) return 3;
///     } else
///         deletemathfunc(f);
///     return 0;
/// }
/// ```
///
/// Removes `fnam` from the autoloadable math-function registry.
/// WARNING: param names don't match C — Rust=(table, _modnam, fnam, flags) vs C=(modnam, fnam, flags)
pub fn del_automathfunc(table: &mut modulestab, _modnam: &str, fnam: &str, flags: i32) -> i32 {
    // c:1436
    // Faithful port of c:1436-1449:
    //   MathFunc f = getmathfunc(fnam, 0);
    //   if (!f) { if (!(flags & FEAT_IGNORE)) return 2; }
    //   else if (f->flags & MFF_ADDED) {
    //       if (!(flags & FEAT_IGNORE)) return 3;
    //   } else deletemathfunc(f);
    //   return 0;
    //
    // Prior port skipped the MFF_ADDED gate at c:1444 entirely.
    // That meant `zmodload -ufd` on a real (module-registered)
    // math function silently succeeded instead of returning 3 like
    // C does, dropping the user's actual function out from under
    // them. Now uses getmathfunc (fd1ec84bab) to find the entry
    // and checks MFF_ADDED before removal.

    // c:1440 — `getmathfunc(fnam, 0)`. autol=0 since we don't want
    // the autoload trigger to fire during a deletion query.
    let entry = getmathfunc(table, fnam, 0);
    match entry {
        None => {
            // c:1441-1442 — `if (!f) { if (!FEAT_IGNORE) return 2; }`
            if (flags & FEAT_IGNORE) == 0 {
                return 2;
            }
            0
        }
        Some(_) => {
            // c:1443 — `else if (f->flags & MFF_ADDED)`.
            // Look up the entry in MATHFUNCS to read its flags
            // (getmathfunc returns the module string, not the
            // mathfunc struct).
            let added = {
                let tab = MATHFUNCS.lock().unwrap();
                tab.iter()
                    .find(|m| m.name == fnam)
                    .map(|m| (m.flags & MFF_ADDED) != 0)
                    .unwrap_or(false)
            };
            if added {
                // c:1444-1445 — real registered, can't unload via
                // del_auto*. Return 3 unless FEAT_IGNORE.
                if (flags & FEAT_IGNORE) == 0 {
                    return 3;
                }
                return 0;
            }
            // c:1447 — deletemathfunc(f). Use removemathfunc
            // (which deletemathfunc delegates to in the autoload
            // path) + drop the ledger entry.
            removemathfunc(fnam);
            table.autoload_mathfuncs.remove(fnam);
            0
        }
    }
}

/// Port of `load_and_bind(const char *fn)` from `Src/module.c:1468`.
///
/// C body: AIX-only `load() + loadbind()` wrapper. Iterates the
/// `modulestab` hash table, binding each loaded module's handle to
/// the new module's symbols. On loadbind failure, calls `unload()`
/// and stores the error in `dlerrstr`.
///
/// Static-link path: dlopen/dlsym aren't used since modules are
/// linked at compile time. Returns 0 (NULL handle).
/// WARNING: param names don't match C — Rust=(_fn_path) vs C=(fn)
pub fn load_and_bind(_fn_path: &str) -> usize {
    // c:1468
    0 // c:1492 NULL
}

/// Port of `hpux_dlsym(void *handle, char *name)` from `Src/module.c:1530`.
///
/// C body:
/// ```c
/// hpux_dlsym(void *handle, char *name)
/// {
///     void *sym_addr;
///     if (!shl_findsym((shl_t *)&handle, name, TYPE_UNDEFINED, &sym_addr))
///         return sym_addr;
///     return NULL;
/// }
/// ```
///
/// HP-UX-specific dlsym wrapper around `shl_findsym(3)`. Static-link
/// path: never invoked since zshrs doesn't dlopen modules.
#[allow(unused_variables)]
pub fn hpux_dlsym(handle: usize, name: &str) -> usize {
    // c:1530
    0 // c:1530 NULL
}

/// Port of `try_load_module(char const *name)` from `Src/module.c:1583`.
///
/// C body iterates `module_path` looking for a loadable file via
/// `dlopen`. Static-link path: a module is "loadable" iff it's in
/// our static `ModuleTable.modules` map.
/// WARNING: param names don't match C — Rust=(table, name) vs C=(name)
pub fn try_load_module(table: &modulestab, name: &str) -> i32 {
    // c:1583
    // C dlopens the module path (or falls back to module_linked for
    // compiled-in modules). Static-link analog: the node must carry
    // MOD_LINKED (seeded by register_module / register_builtin_modules
    // for every compiled-in module). A bare FINDMOD_CREATE bookkeeping
    // node (flags=0 per C's zshcalloc at c:1676) has no backing code —
    // dlopen would fail, so the loadable probe must too. Without the
    // flag gate, `zmodload -ab zsh/bogus x; x` "booted" the phantom
    // module instead of emitting `failed to load module`.
    match table.modules.get(name) {
        Some(m) if (m.node.flags & MOD_LINKED) != 0 => 1,
        _ => 0,
    }
}

/// Port of `do_load_module(char const *name, int silent)` from `Src/module.c:1610`.
///
/// C body:
/// ```c
/// do_load_module(char const *name, int silent)
/// {
///     void *ret;
///     ret = try_load_module(name);
///     if (!ret && !silent) {
///         zwarn("failed to load module `%s': %s", name, ...);
///     }
///     return ret;
/// }
/// ```
///
/// C returns `void *` (the dlopen handle); Rust port returns 0 on
/// success / 1 on failure. zshrs's static-link path: `try_load_module`
/// always succeeds for built-in modules. Returns 1 + zwarn on miss.
/// WARNING: param names don't match C — Rust=(table, name, silent) vs C=(name, silent)
pub fn do_load_module(table: &mut modulestab, name: &str, silent: i32) -> i32 {
    // c:1610
    // c:1610 — ret = try_load_module(name);
    let ret = try_load_module(table, name);
    if ret == 0 && silent == 0 {
        // c:1615
        // c:1618-1621 — zwarn("failed to load module ...")
        zwarn(&format!("failed to load module: {}", name));
    }
    ret // c:1624
}

/// Port of `find_module(const char *name, int flags, const char **namep)` from `Src/module.c:1659`.
///
/// C body:
/// ```c
/// find_module(const char *name, int flags, const char **namep)
/// {
///     Module m;
///     m = (Module)modulestab->getnode2(modulestab, name);
///     if (m) {
///         if ((flags & FINDMOD_ALIASP) && (m->node.flags & MOD_ALIAS)) {
///             if (namep) *namep = m->u.alias;
///             return find_module(m->u.alias, flags, namep);
///         }
///         if (namep) *namep = m->node.nam;
///         return m;
///     }
///     if (!(flags & FINDMOD_CREATE))
///         return NULL;
///     m = zshcalloc(sizeof(*m));
///     modulestab->addnode(modulestab, ztrdup(name), m);
///     return m;
/// }
/// ```
///
/// Returns the resolved module name (after alias chasing) and
/// whether an entry was created. C's `Module` return becomes
/// `Option<String>` of the canonical name.
/// WARNING: param names don't match C — Rust=(table, name, flags) vs C=(name, flags, namep)
pub fn find_module(table: &mut modulestab, name: &str, flags: i32) -> Option<String> {
    // c:1659
    // c:1659 — m = modulestab->getnode2(modulestab, name);
    let mut cur_name = name.to_string();
    let mut depth = 0;
    loop {
        if depth > 64 {
            return None;
        } // alias-cycle guard
        depth += 1;
        match table.modules.get(&cur_name) {
            Some(m) => {
                // c:1665 — if ((flags & FINDMOD_ALIASP) && (m->node.flags & MOD_ALIAS))
                if (flags & FINDMOD_ALIASP) != 0 && (m.node.flags & MOD_ALIAS) != 0 {
                    // c:1668 — return find_module(m->u.alias, flags, namep);
                    if let Some(target) = m.alias.clone() {
                        cur_name = target;
                        continue;
                    }
                    return None;
                }
                // c:1671 — *namep = m->node.nam; return m;
                return Some(cur_name);
            }
            None => {
                // c:1674 — if (!(flags & FINDMOD_CREATE)) return NULL;
                if (flags & FINDMOD_CREATE) == 0 {
                    return None;
                }
                // c:1676-1677 — m = zshcalloc(...); addnode(name, m);
                // zshcalloc zero-fills: the created node has flags=0
                // and NULL handle/linked — a bookkeeping entry (alias
                // targets, autoload owners), NOT a loadable module.
                // module::new() seeds MOD_LINKED (the register_module
                // shape, c:359); that bit wrongly made phantom nodes
                // (`zmodload -ab zsh/bogus x`) pass try_load_module's
                // loadable gate and "boot" successfully.
                let mut m = module::new(&cur_name);
                m.node.flags = 0; // c:1676 zshcalloc — all-zero node
                table.modules.insert(cur_name.clone(), m);
                return Some(cur_name);
            }
        }
    }
}

/// Port of `delete_module(Module m)` from `Src/module.c:1687`.
///
/// C body:
/// ```c
/// delete_module(Module m) {
///     modulestab->removenode(modulestab, m->node.nam);
///     modulestab->freenode(&m->node);
/// }
/// ```
///
/// Removes a module from the live `modulestab` and frees its node.
/// Rust port operates on the `ModuleTable` `modules` HashMap.
/// WARNING: param names don't match C — Rust=(table, name) vs C=(m)
pub fn delete_module(table: &mut modulestab, name: &str) -> i32 {
    // c:1687
    table.modules.remove(name); // c:1687 removenode
                                // c:1691 — freenode(&m->node) — Rust drops on `remove` return.
    0
}

/// Port of `module_loaded(const char *name)` from `Src/module.c:1703`.
///
/// C body:
/// ```c
/// module_loaded(const char *name)
/// {
///     Module m;
///     return ((m = find_module(name, FINDMOD_ALIASP, NULL)) &&
///             m->u.handle &&
///             !(m->node.flags & MOD_UNLOAD));
/// }
/// ```
///
/// Returns true (non-zero) if the named module is currently loaded.
///
/// Faithful port of c:1702-1710:
/// ```c
/// mod_export int
/// module_loaded(const char *name)
/// {
///     Module m;
///     return ((m = find_module(name, FINDMOD_ALIASP, NULL)) &&
///             m->u.handle &&
///             !(m->node.flags & MOD_UNLOAD));
/// }
/// ```
///
/// All three gates matter in zshrs's static-link path:
///   1. `find_module(FINDMOD_ALIASP)` — resolves aliases, returns the
///      target's modulestab entry (or None on miss).
///   2. `m->u.handle` — non-null when the module's setup has run; in
///      the Rust mirror, MOD_INIT_S being set is the structural
///      equivalent (no dlopen handle to test).
///   3. `!(m->node.flags & MOD_UNLOAD)` — MOD_UNLOAD is the
///      "registered + autoload-only" sentinel set by
///      register_builtin_modules for entries OUTSIDE
///      zsh_default_loaded (e.g. zsh/files, zsh/system, zsh/zftp).
///      Without this gate, `${modules[zsh/files]}` reads as "loaded"
///      from initial register even though no `zmodload zsh/files` has
///      run — diverging from `zsh -fc` which reports the names as
///      autoload-pending.
/// WARNING: param names don't match C — Rust=(table, name) vs C=(name)
pub fn module_loaded(table: &modulestab, name: &str) -> i32 {
    // c:1703
    // c:1707 — `find_module(name, FINDMOD_ALIASP, NULL)`: resolve
    // alias chains via the existing free-fn port (c:1659).
    // The Rust port returns Option<String> — Some(target) on hit.
    // Inline the resolution here so we can read the target's flags
    // without re-locking.
    let target = match table.modules.get(name) {
        Some(m) if (m.node.flags & MOD_ALIAS) != 0 => {
            // c:FINDMOD_ALIASP — chase alias.
            // m->u.alias is the alias target; the Rust mirror stores
            // it on `module::aliased` (set by zmodload -A). Probe.
            match m.alias.as_ref().and_then(|a| table.modules.get(a)) {
                Some(t) => t,
                None => return 0, // alias points nowhere → not loaded
            }
        }
        Some(m) => m,
        None => return 0, // c:1707-1709 — find_module miss.
    };
    // c:1708 — `m->u.handle` — non-null on a fully-loaded module.
    // Static-link analog: MOD_LINKED set + module entry exists in
    // modulestab means register_module fired (= setup ran). The
    // dlopen `u.handle` check translates to MOD_LINKED here because
    // every modulestab entry is a statically-linked module in
    // zshrs's compile-time-only loader.
    if (target.node.flags & MOD_LINKED) == 0 {
        return 0;
    }
    // c:1709 — `!(m->node.flags & MOD_UNLOAD)`. MOD_UNLOAD is set by
    // register_builtin_modules on the autoload-only subset
    // (zsh/files, zsh/system, zsh/zftp, …); cleared by an explicit
    // `zmodload NAME` once the user wants the module live.
    if (target.node.flags & MOD_UNLOAD) != 0 {
        return 0;
    }
    1
}

/// Port of `dyn_setup_module(Module m)` from `Src/module.c:1726`.
///
/// C body: `return ((int (*)(int,Module,void*)) m->u.handle)(0, m, NULL);`
/// Op-code 0 = setup. AIX-only path that multiplexes all six module
/// hooks through one symbol; static-link path skips it entirely.
#[allow(unused_variables)]
pub fn dyn_setup_module(m: *const module) -> i32 {
    // c:1726
    0 // c:1726
}

/// Port of `dyn_features_module(Module m, char ***features)` from `Src/module.c:1733`.
///
/// C body: `return ((int (*)(int,Module,void*)) m->u.handle)(4, m, features);`
/// Op-code 4 = features.
#[allow(unused_variables)]
pub fn dyn_features_module(m: *const module, features: &mut Vec<String>) -> i32 {
    // c:1733
    0 // c:1733
}

/// Port of `dyn_enables_module(Module m, int **enables)` from `Src/module.c:1740`.
///
/// C body: `return ((int (*)(int,Module,void*)) m->u.handle)(5, m, enables);`
/// Op-code 5 = enables.
#[allow(unused_variables)]
pub fn dyn_enables_module(m: *const module, enables: &mut Option<Vec<i32>>) -> i32 {
    // c:1740
    0 // c:1733
}

/// Port of `dyn_boot_module(Module m)` from `Src/module.c:1747`.
///
/// C body: `return ((int (*)(int,Module,void*)) m->u.handle)(1, m, NULL);`
/// Calls the dynamic module's exported entry-point with op-code 1
/// (boot). Static-link path: opcode dispatch unused, returns 0.
#[allow(unused_variables)]
pub fn dyn_boot_module(m: *const module) -> i32 {
    // c:1747
    0 // c:1754
}

/// Port of `dyn_cleanup_module(Module m)` from `Src/module.c:1754`.
///
/// C body: `return ((int (*)(int,Module,void*)) m->u.handle)(2, m, NULL);`
/// Op-code 2 = cleanup.
#[allow(unused_variables)]
pub fn dyn_cleanup_module(m: *const module) -> i32 {
    // c:1754
    0 // c:1740
}

/// Port of `static int dyn_finish_module(Module m)` from
/// `Src/module.c:1766`. C body: `return ((int (*)(int,Module,void*))
/// m->u.handle)(3, m, NULL);` — invokes the DSO entry point with
/// opcode 3 (finish). no-op in Rust: zshrs has no dlopen path, all
/// modules are statically linked, `m->u.handle` is always null, and
/// finish-time resource release happens at process exit. Static-link
/// path defers all DSO entry calls; the no-op return preserves
/// caller semantics (0 = success).
#[allow(unused_variables)]
pub fn dyn_finish_module(m: *const module) -> i32 {
    // c:1766
    // c:1768 — ((int (*)(int,Module,void*)) m->u.handle)(3, m, NULL).
    // Static modules: no handle, opcode 3 (finish) is a no-op.
    0 // c:1768 success
}

/// Port of `module_func(Module m, const char *name)` from `Src/module.c:1770`.
///
/// C body (DYNAMIC_NAME_CLASH_OK off — the typical case):
/// ```c
/// module_func(Module m, const char *name)
/// {
///     VARARR(char, buf, strlen(name) + strlen(m->node.nam)*2 + 1);
///     char const *p; char *q;
///     strcpy(buf, name);
///     q = strchr(buf, 0);
///     for(p = m->node.nam; *p; p++) {
///         if(*p == '/')      { *q++ = 'Q'; *q++ = 's'; }
///         else if(*p == '_') { *q++ = 'Q'; *q++ = 'u'; }
///         else if(*p == 'Q') { *q++ = 'Q'; *q++ = 'q'; }
///         else                 *q++ = *p;
///     }
///     *q = 0;
///     return (Module_generic_func) dlsym(m->u.handle, buf);
/// }
/// ```
///
/// Builds a mangled symbol name (`<name><module-name-mangled>`) and
/// dlsym's it. The mangling encodes `/` as `Qs`, `_` as `Qu`, `Q` as
/// `Qq` so e.g. `setup_zsh_random` becomes `setup_zshQurandom`.
///
/// Static-link path: dlsym not used; returns 0 (NULL handle).
#[allow(unused_variables)]
pub fn module_func(m: &module, name: &str) -> usize {
    // c:1770
    0 // c:1794 NULL
}

/// Port of `setup_module(Module m)` from `Src/module.c:1884`.
///
/// C body:
/// ```c
/// setup_module(Module m) {
///     return ((m->node.flags & MOD_LINKED) ?
///             (m->u.linked->setup)(m) : dyn_setup_module(m));
/// }
/// ```
/// Symmetric with boot_module/cleanup_module/finish_module: routes
/// to the per-module `setup_(m)`. C `setup_` is the first lifecycle
/// hook called when load_module loads the module (before
/// do_module_features registers features and before boot_ runs).
/// Most per-module setup_ bodies are `return 0;` — exceptions
/// initialise module-private state (curses screen handle, regex
/// compile cache, etc.).
/// WARNING: param names don't match C — Rust=(_table, name) vs C=(m)
pub fn setup_module(_table: &mut modulestab, name: &str) -> i32 {
    // c:1884
    match name {
        "zsh/attr" => crate::ported::modules::attr::setup_(std::ptr::null()),
        "zsh/cap" => crate::ported::modules::cap::setup_(std::ptr::null()),
        "zsh/clone" => crate::ported::modules::clone::setup_(std::ptr::null()),
        "zsh/curses" => crate::ported::modules::curses::setup_(std::ptr::null()),
        "zsh/datetime" => crate::ported::modules::datetime::setup_(std::ptr::null()),
        "zsh/db/gdbm" => crate::ported::modules::db_gdbm::setup_(std::ptr::null()),
        "zsh/example" => crate::ported::modules::example::setup_(std::ptr::null()),
        "zsh/files" => crate::ported::modules::files::setup_(std::ptr::null()),
        "zsh/hlgroup" => crate::ported::modules::hlgroup::setup_(std::ptr::null()),
        "zsh/ksh93" => crate::ported::modules::ksh93::setup_(std::ptr::null()),
        "zsh/langinfo" => crate::ported::modules::langinfo::setup_(std::ptr::null()),
        "zsh/mapfile" => crate::ported::modules::mapfile::setup_(std::ptr::null()),
        "zsh/mathfunc" => crate::ported::modules::mathfunc::setup_(std::ptr::null()),
        "zsh/nearcolor" => crate::ported::modules::nearcolor::setup_(std::ptr::null()),
        "zsh/newuser" => crate::ported::modules::newuser::setup_(std::ptr::null()),
        "zsh/parameter" => crate::ported::modules::parameter::setup_(std::ptr::null()),
        "zsh/param/private" => crate::ported::modules::param_private::setup_(std::ptr::null()),
        "zsh/pcre" => crate::ported::modules::pcre::setup_(std::ptr::null()),
        "zsh/random" => crate::ported::modules::random::setup_(std::ptr::null()),
        "zsh/regex" => crate::ported::modules::regex::setup_(std::ptr::null()),
        "zsh/net/socket" => crate::ported::modules::socket::setup_(std::ptr::null()),
        "zsh/stat" => crate::ported::modules::stat::setup_(std::ptr::null()),
        "zsh/system" => crate::ported::modules::system::setup_(std::ptr::null()),
        "zsh/net/tcp" => crate::ported::modules::tcp::setup_(std::ptr::null()),
        "zsh/termcap" => crate::ported::modules::termcap::setup_(std::ptr::null()),
        "zsh/terminfo" => crate::ported::modules::terminfo::setup_(std::ptr::null()),
        "zsh/watch" => crate::ported::modules::watch::setup_(std::ptr::null()),
        "zsh/zftp" => crate::ported::modules::zftp::setup_(std::ptr::null()),
        "zsh/zprof" => crate::ported::modules::zprof::setup_(std::ptr::null()),
        "zsh/zpty" => crate::ported::modules::zpty::setup_(std::ptr::null()),
        "zsh/zselect" => crate::ported::modules::zselect::setup_(std::ptr::null()),
        "zsh/zutil" => crate::ported::modules::zutil::setup_(std::ptr::null()),
        _ => 0,
    }
}

/// Port of `features_module(Module m, char ***features)` from `Src/module.c:1892`.
///
/// C body:
/// ```c
/// features_module(Module m, char ***features) {
///     return ((m->node.flags & MOD_LINKED) ?
///             (m->u.linked->features)(m, features) :
///             dyn_features_module(m, features));
/// }
/// ```
///
/// Per-module features_ populates the out-array with each feature
/// the module provides — `b:bn`, `c:cn`, `C:Cn`, `f:fn`, `p:pn`
/// per featuresarray (Src/module.c:3279). Used by zmodload -L to
/// list feature surfaces.
/// WARNING: param names don't match C — Rust=(_table, name, features) vs C=(m, features)
pub fn features_module(
    _table: &mut modulestab,
    name: &str,
    features: &mut Vec<String>,
) -> i32 {
    // c:1892
    match name {
        "zsh/attr" => crate::ported::modules::attr::features_(std::ptr::null(), features),
        "zsh/cap" => crate::ported::modules::cap::features_(std::ptr::null(), features),
        "zsh/clone" => crate::ported::modules::clone::features_(std::ptr::null(), features),
        "zsh/curses" => crate::ported::modules::curses::features_(std::ptr::null(), features),
        "zsh/datetime" => {
            crate::ported::modules::datetime::features_(std::ptr::null(), features)
        }
        "zsh/db/gdbm" => crate::ported::modules::db_gdbm::features_(std::ptr::null(), features),
        "zsh/example" => crate::ported::modules::example::features_(std::ptr::null(), features),
        "zsh/files" => crate::ported::modules::files::features_(std::ptr::null(), features),
        "zsh/hlgroup" => crate::ported::modules::hlgroup::features_(std::ptr::null(), features),
        "zsh/ksh93" => crate::ported::modules::ksh93::features_(std::ptr::null(), features),
        "zsh/langinfo" => {
            crate::ported::modules::langinfo::features_(std::ptr::null(), features)
        }
        "zsh/mapfile" => crate::ported::modules::mapfile::features_(std::ptr::null(), features),
        "zsh/mathfunc" => {
            crate::ported::modules::mathfunc::features_(std::ptr::null(), features)
        }
        "zsh/nearcolor" => {
            crate::ported::modules::nearcolor::features_(std::ptr::null(), features)
        }
        "zsh/newuser" => crate::ported::modules::newuser::features_(std::ptr::null(), features),
        "zsh/parameter" => {
            crate::ported::modules::parameter::features_(std::ptr::null(), features)
        }
        "zsh/param/private" => {
            crate::ported::modules::param_private::features_(std::ptr::null(), features)
        }
        "zsh/pcre" => crate::ported::modules::pcre::features_(std::ptr::null(), features),
        "zsh/random" => crate::ported::modules::random::features_(std::ptr::null(), features),
        "zsh/regex" => crate::ported::modules::regex::features_(std::ptr::null(), features),
        "zsh/net/socket" => {
            crate::ported::modules::socket::features_(std::ptr::null(), features)
        }
        "zsh/stat" => crate::ported::modules::stat::features_(std::ptr::null(), features),
        "zsh/system" => crate::ported::modules::system::features_(std::ptr::null(), features),
        "zsh/net/tcp" => crate::ported::modules::tcp::features_(std::ptr::null(), features),
        "zsh/termcap" => crate::ported::modules::termcap::features_(std::ptr::null(), features),
        "zsh/terminfo" => {
            crate::ported::modules::terminfo::features_(std::ptr::null(), features)
        }
        "zsh/watch" => crate::ported::modules::watch::features_(std::ptr::null(), features),
        // c:Src/Zle/zle_main.c:2286 — zsh/zle's features_() returns
        // b:zle/b:bindkey/b:vared + the p:BUFFER-family params via
        // featuresarray. Without this arm the dispatch fell to the
        // `_ => 0` default with an EMPTY features vec, so once any
        // zle/bindkey use marked the module loaded, the NEXT
        // autoloaded-builtin dispatch (ensurefeature → autofeatures
        // c:3558-3577 loaded-module check) found no `b:bindkey` in
        // the table and errored `module has no such feature` —
        // breaking every zinit plugin that calls `zle -N` before
        // `bindkey` (zsh-autopair, zsh-hist, zconvey, zui, …).
        "zsh/zle" => crate::ported::zle::zle_main::features_(std::ptr::null(), features),
        "zsh/zftp" => crate::ported::modules::zftp::features_(std::ptr::null(), features),
        "zsh/zprof" => crate::ported::modules::zprof::features_(std::ptr::null(), features),
        "zsh/zpty" => crate::ported::modules::zpty::features_(std::ptr::null(), features),
        "zsh/zselect" => crate::ported::modules::zselect::features_(std::ptr::null(), features),
        "zsh/zutil" => crate::ported::modules::zutil::features_(std::ptr::null(), features),
        _ => 0,
    }
}

/// Port of `enables_module(Module m, int **enables)` from `Src/module.c:1901`.
///
/// C body:
/// ```c
/// enables_module(Module m, int **enables) {
///     return ((m->node.flags & MOD_LINKED) ?
///             (m->u.linked->enables)(m, enables) :
///             dyn_enables_module(m, enables));
/// }
/// ```
///
/// Per-module enables_ populates the enable-bit array parallel to
/// the features_ surface — getfeatureenables (c:3314) emits 1 for
/// each currently-active feature (BINF_ADDED / CONDF_ADDED /
/// MFF_ADDED / pd->pm non-null), 0 otherwise. Used by zmodload -L
/// to report enabled-vs-disabled features per module.
/// WARNING: param names don't match C — Rust=(_table, name, enables) vs C=(m, enables)
pub fn enables_module(
    _table: &mut modulestab,
    name: &str,
    enables: &mut Option<Vec<i32>>,
) -> i32 {
    // c:1901
    match name {
        "zsh/attr" => crate::ported::modules::attr::enables_(std::ptr::null(), enables),
        "zsh/cap" => crate::ported::modules::cap::enables_(std::ptr::null(), enables),
        "zsh/clone" => crate::ported::modules::clone::enables_(std::ptr::null(), enables),
        "zsh/curses" => crate::ported::modules::curses::enables_(std::ptr::null(), enables),
        "zsh/datetime" => crate::ported::modules::datetime::enables_(std::ptr::null(), enables),
        "zsh/db/gdbm" => crate::ported::modules::db_gdbm::enables_(std::ptr::null(), enables),
        "zsh/example" => crate::ported::modules::example::enables_(std::ptr::null(), enables),
        "zsh/files" => crate::ported::modules::files::enables_(std::ptr::null(), enables),
        "zsh/hlgroup" => crate::ported::modules::hlgroup::enables_(std::ptr::null(), enables),
        "zsh/ksh93" => crate::ported::modules::ksh93::enables_(std::ptr::null(), enables),
        "zsh/langinfo" => crate::ported::modules::langinfo::enables_(std::ptr::null(), enables),
        "zsh/mapfile" => crate::ported::modules::mapfile::enables_(std::ptr::null(), enables),
        "zsh/mathfunc" => crate::ported::modules::mathfunc::enables_(std::ptr::null(), enables),
        "zsh/nearcolor" => {
            crate::ported::modules::nearcolor::enables_(std::ptr::null(), enables)
        }
        "zsh/newuser" => crate::ported::modules::newuser::enables_(std::ptr::null(), enables),
        "zsh/parameter" => crate::ported::modules::parameter::enables_(std::ptr::null(), enables),
        "zsh/param/private" => {
            crate::ported::modules::param_private::enables_(std::ptr::null(), enables)
        }
        "zsh/pcre" => crate::ported::modules::pcre::enables_(std::ptr::null(), enables),
        "zsh/random" => crate::ported::modules::random::enables_(std::ptr::null(), enables),
        "zsh/regex" => crate::ported::modules::regex::enables_(std::ptr::null(), enables),
        "zsh/net/socket" => crate::ported::modules::socket::enables_(std::ptr::null(), enables),
        "zsh/stat" => crate::ported::modules::stat::enables_(std::ptr::null(), enables),
        "zsh/system" => crate::ported::modules::system::enables_(std::ptr::null(), enables),
        "zsh/net/tcp" => crate::ported::modules::tcp::enables_(std::ptr::null(), enables),
        "zsh/termcap" => crate::ported::modules::termcap::enables_(std::ptr::null(), enables),
        "zsh/terminfo" => crate::ported::modules::terminfo::enables_(std::ptr::null(), enables),
        "zsh/watch" => crate::ported::modules::watch::enables_(std::ptr::null(), enables),
        "zsh/zftp" => crate::ported::modules::zftp::enables_(std::ptr::null(), enables),
        "zsh/zprof" => crate::ported::modules::zprof::enables_(std::ptr::null(), enables),
        "zsh/zpty" => crate::ported::modules::zpty::enables_(std::ptr::null(), enables),
        "zsh/zselect" => crate::ported::modules::zselect::enables_(std::ptr::null(), enables),
        "zsh/zutil" => crate::ported::modules::zutil::enables_(std::ptr::null(), enables),
        _ => 0,
    }
}

/// Port of `boot_module(Module m)` from `Src/module.c:1910`.
///
/// C body:
/// ```c
/// boot_module(Module m) {
///     return ((m->node.flags & MOD_LINKED) ?
///             (m->u.linked->boot)(m) : dyn_boot_module(m));
/// }
/// ```
///
/// Static-link path: every modulestab entry is MOD_LINKED, so the
/// branch collapses to `(m->u.linked->boot)(m)`. The Rust analog
/// of C's `m->u.linked->boot` function-pointer is the per-module
/// `boot_(m)` defined in `src/ported/modules/<name>.rs`. Dispatch
/// via a static name → fn-pointer table.
///
/// Modules not in the dispatch table (because their per-module
/// boot_ isn't ported yet) fall through to 0 — same observable
/// outcome as a no-op boot, matching pre-port behaviour.
/// WARNING: param names don't match C — Rust=(_table, name) vs C=(m)
pub fn boot_module(_table: &mut modulestab, name: &str) -> i32 {
    // c:1910
    // c:1912 — `(m->u.linked->boot)(m)` for MOD_LINKED.
    // The per-module boot_ entry points carry the partab/bintab
    // dispatch that addparamdef/addbuiltins would otherwise run
    // for a dlopen'd module (e.g. watch.rs::boot_ seeds
    // WATCH/watch in paramtab + installs the checksched preprompt
    // hook). NULL module pointer is fine — every per-module boot_
    // either ignores the arg or null-checks before deref.
    match name {
        "zsh/attr" => crate::ported::modules::attr::boot_(std::ptr::null()),
        "zsh/cap" => crate::ported::modules::cap::boot_(std::ptr::null()),
        "zsh/clone" => crate::ported::modules::clone::boot_(std::ptr::null()),
        "zsh/curses" => crate::ported::modules::curses::boot_(std::ptr::null()),
        "zsh/datetime" => crate::ported::modules::datetime::boot_(std::ptr::null()),
        "zsh/db/gdbm" => crate::ported::modules::db_gdbm::boot_(std::ptr::null()),
        "zsh/example" => crate::ported::modules::example::boot_(std::ptr::null()),
        "zsh/files" => crate::ported::modules::files::boot_(std::ptr::null()),
        "zsh/hlgroup" => crate::ported::modules::hlgroup::boot_(std::ptr::null()),
        "zsh/ksh93" => crate::ported::modules::ksh93::boot_(std::ptr::null()),
        "zsh/langinfo" => crate::ported::modules::langinfo::boot_(std::ptr::null()),
        "zsh/mapfile" => crate::ported::modules::mapfile::boot_(std::ptr::null()),
        "zsh/mathfunc" => crate::ported::modules::mathfunc::boot_(std::ptr::null()),
        "zsh/nearcolor" => crate::ported::modules::nearcolor::boot_(std::ptr::null()),
        "zsh/newuser" => crate::ported::modules::newuser::boot_(std::ptr::null()),
        "zsh/parameter" => crate::ported::modules::parameter::boot_(std::ptr::null()),
        "zsh/param/private" => crate::ported::modules::param_private::boot_(std::ptr::null()),
        "zsh/pcre" => crate::ported::modules::pcre::boot_(std::ptr::null()),
        "zsh/random" => crate::ported::modules::random::boot_(std::ptr::null()),
        "zsh/regex" => crate::ported::modules::regex::boot_(std::ptr::null()),
        "zsh/net/socket" => crate::ported::modules::socket::boot_(std::ptr::null()),
        "zsh/stat" => crate::ported::modules::stat::boot_(std::ptr::null()),
        "zsh/system" => crate::ported::modules::system::boot_(std::ptr::null()),
        "zsh/net/tcp" => crate::ported::modules::tcp::boot_(std::ptr::null()),
        "zsh/termcap" => crate::ported::modules::termcap::boot_(std::ptr::null()),
        "zsh/terminfo" => crate::ported::modules::terminfo::boot_(std::ptr::null()),
        // c:1912 — C passes the real Module m to boot. watch::boot_
        // reads m->node.flags (MOD_SETUP = mid-load_module) to gate
        // its WATCHFMT/LOGCHECK seeding under --zsh; passing the
        // table entry avoids re-locking MODULESTAB (the caller chain
        // load_module -> do_boot_module already holds it).
        "zsh/watch" => {
            let mptr = _table
                .modules
                .get(name)
                .map(|m| m as *const crate::ported::zsh_h::module)
                .unwrap_or(std::ptr::null());
            crate::ported::modules::watch::boot_(mptr)
        }
        "zsh/zftp" => crate::ported::modules::zftp::boot_(std::ptr::null()),
        "zsh/zprof" => crate::ported::modules::zprof::boot_(std::ptr::null()),
        "zsh/zpty" => crate::ported::modules::zpty::boot_(std::ptr::null()),
        "zsh/zselect" => crate::ported::modules::zselect::boot_(std::ptr::null()),
        "zsh/zutil" => crate::ported::modules::zutil::boot_(std::ptr::null()),
        // Modules without a ported per-module boot_ (e.g. zsh/main,
        // zsh/complete — purely-static modules with no setup hook):
        // 0 == success no-op, matching the pre-port behaviour.
        _ => 0,
    }
}

/// Port of `cleanup_module(Module m)` from `Src/module.c:1918`.
///
/// C body:
/// ```c
/// cleanup_module(Module m) {
///     return ((m->node.flags & MOD_LINKED) ?
///             (m->u.linked->cleanup)(m) : dyn_cleanup_module(m));
/// }
/// ```
///
/// Static-link path: dispatch via name → fn-pointer table to the
/// per-module `cleanup_(m)` defined in `src/ported/modules/<name>.rs`.
/// Mirrors the symmetric `boot_module` dispatcher (`Src/module.c:1910`).
/// Per-module cleanup_ typically calls `delprepromptfn` /
/// `setfeatureenables(NULL)` to roll back what boot_ installed.
/// WARNING: param names don't match C — Rust=(_table, name) vs C=(m)
pub fn cleanup_module(_table: &mut modulestab, name: &str) -> i32 {
    // c:1918
    match name {
        "zsh/attr" => crate::ported::modules::attr::cleanup_(std::ptr::null()),
        "zsh/cap" => crate::ported::modules::cap::cleanup_(std::ptr::null()),
        "zsh/clone" => crate::ported::modules::clone::cleanup_(std::ptr::null()),
        "zsh/curses" => crate::ported::modules::curses::cleanup_(std::ptr::null()),
        "zsh/datetime" => crate::ported::modules::datetime::cleanup_(std::ptr::null()),
        "zsh/db/gdbm" => crate::ported::modules::db_gdbm::cleanup_(std::ptr::null()),
        "zsh/example" => crate::ported::modules::example::cleanup_(std::ptr::null()),
        "zsh/files" => crate::ported::modules::files::cleanup_(std::ptr::null()),
        "zsh/hlgroup" => crate::ported::modules::hlgroup::cleanup_(std::ptr::null()),
        "zsh/ksh93" => crate::ported::modules::ksh93::cleanup_(std::ptr::null()),
        "zsh/langinfo" => crate::ported::modules::langinfo::cleanup_(std::ptr::null()),
        "zsh/mapfile" => crate::ported::modules::mapfile::cleanup_(std::ptr::null()),
        "zsh/mathfunc" => crate::ported::modules::mathfunc::cleanup_(std::ptr::null()),
        "zsh/nearcolor" => crate::ported::modules::nearcolor::cleanup_(std::ptr::null()),
        "zsh/newuser" => crate::ported::modules::newuser::cleanup_(std::ptr::null()),
        "zsh/parameter" => crate::ported::modules::parameter::cleanup_(std::ptr::null()),
        "zsh/param/private" => crate::ported::modules::param_private::cleanup_(std::ptr::null()),
        "zsh/pcre" => crate::ported::modules::pcre::cleanup_(std::ptr::null()),
        "zsh/random" => crate::ported::modules::random::cleanup_(std::ptr::null()),
        "zsh/regex" => crate::ported::modules::regex::cleanup_(std::ptr::null()),
        "zsh/net/socket" => crate::ported::modules::socket::cleanup_(std::ptr::null()),
        "zsh/stat" => crate::ported::modules::stat::cleanup_(std::ptr::null()),
        "zsh/system" => crate::ported::modules::system::cleanup_(std::ptr::null()),
        "zsh/net/tcp" => crate::ported::modules::tcp::cleanup_(std::ptr::null()),
        "zsh/termcap" => crate::ported::modules::termcap::cleanup_(std::ptr::null()),
        "zsh/terminfo" => crate::ported::modules::terminfo::cleanup_(std::ptr::null()),
        "zsh/watch" => crate::ported::modules::watch::cleanup_(std::ptr::null()),
        "zsh/zftp" => crate::ported::modules::zftp::cleanup_(std::ptr::null()),
        "zsh/zprof" => crate::ported::modules::zprof::cleanup_(std::ptr::null()),
        "zsh/zpty" => crate::ported::modules::zpty::cleanup_(std::ptr::null()),
        "zsh/zselect" => crate::ported::modules::zselect::cleanup_(std::ptr::null()),
        "zsh/zutil" => crate::ported::modules::zutil::cleanup_(std::ptr::null()),
        _ => 0,
    }
}

/// Port of `finish_module(Module m)` from `Src/module.c:1926`.
///
/// C body:
/// ```c
/// finish_module(Module m) {
///     return ((m->node.flags & MOD_LINKED) ?
///             (m->u.linked->finish)(m) : dyn_finish_module(m));
/// }
/// ```
///
/// Static-link path: dispatch to the per-module `finish_(m)`.
/// finish_ is the second teardown step (after cleanup_) — it frees
/// process-lifetime state that survives a single `zmodload -u`.
/// Most modules have an empty body returning 0; a few (zftp, zpty,
/// watch with utmpx descriptors) keep state explicitly.
/// WARNING: param names don't match C — Rust=(_table, name) vs C=(m)
pub fn finish_module(_table: &mut modulestab, name: &str) -> i32 {
    // c:1926
    match name {
        "zsh/attr" => crate::ported::modules::attr::finish_(std::ptr::null()),
        "zsh/cap" => crate::ported::modules::cap::finish_(std::ptr::null()),
        "zsh/clone" => crate::ported::modules::clone::finish_(std::ptr::null()),
        "zsh/curses" => crate::ported::modules::curses::finish_(std::ptr::null()),
        "zsh/datetime" => crate::ported::modules::datetime::finish_(std::ptr::null()),
        "zsh/db/gdbm" => crate::ported::modules::db_gdbm::finish_(std::ptr::null()),
        "zsh/example" => crate::ported::modules::example::finish_(std::ptr::null()),
        "zsh/files" => crate::ported::modules::files::finish_(std::ptr::null()),
        "zsh/hlgroup" => crate::ported::modules::hlgroup::finish_(std::ptr::null()),
        "zsh/ksh93" => crate::ported::modules::ksh93::finish_(std::ptr::null()),
        "zsh/langinfo" => crate::ported::modules::langinfo::finish_(std::ptr::null()),
        "zsh/mapfile" => crate::ported::modules::mapfile::finish_(std::ptr::null()),
        "zsh/mathfunc" => crate::ported::modules::mathfunc::finish_(std::ptr::null()),
        "zsh/nearcolor" => crate::ported::modules::nearcolor::finish_(std::ptr::null()),
        "zsh/newuser" => crate::ported::modules::newuser::finish_(std::ptr::null()),
        "zsh/parameter" => crate::ported::modules::parameter::finish_(std::ptr::null()),
        "zsh/param/private" => crate::ported::modules::param_private::finish_(std::ptr::null()),
        "zsh/pcre" => crate::ported::modules::pcre::finish_(std::ptr::null()),
        "zsh/random" => crate::ported::modules::random::finish_(std::ptr::null()),
        "zsh/regex" => crate::ported::modules::regex::finish_(std::ptr::null()),
        "zsh/net/socket" => crate::ported::modules::socket::finish_(std::ptr::null()),
        "zsh/stat" => crate::ported::modules::stat::finish_(std::ptr::null()),
        "zsh/system" => crate::ported::modules::system::finish_(std::ptr::null()),
        "zsh/net/tcp" => crate::ported::modules::tcp::finish_(std::ptr::null()),
        "zsh/termcap" => crate::ported::modules::termcap::finish_(std::ptr::null()),
        "zsh/terminfo" => crate::ported::modules::terminfo::finish_(std::ptr::null()),
        "zsh/watch" => crate::ported::modules::watch::finish_(std::ptr::null()),
        "zsh/zftp" => crate::ported::modules::zftp::finish_(std::ptr::null()),
        "zsh/zprof" => crate::ported::modules::zprof::finish_(std::ptr::null()),
        "zsh/zpty" => crate::ported::modules::zpty::finish_(std::ptr::null()),
        "zsh/zselect" => crate::ported::modules::zselect::finish_(std::ptr::null()),
        "zsh/zutil" => crate::ported::modules::zutil::finish_(std::ptr::null()),
        _ => 0,
    }
}

/// Port of `do_module_features(Module m, Feature_enables enablesarr, int flags)` from `Src/module.c:1998`.
///
/// C body c:1998-2125 (128 lines):
/// ```c
/// if (features_module(m, &features) == 0) {
///     int *enables = NULL;
///     if (enables_module(m, &enables)) {
///         if (!(flags & FEAT_IGNORE)) zwarn(...);
///         return 1;
///     }
///     if ((flags & FEAT_CHECKAUTO) && m->autoloads) {
///         /* validate autoloads against features list */
///         /* on mismatch: zwarn + autofeatures(REMOVE|IGNORE) +
///            expunge from enablesarr */
///     }
///     if (enablesarr) {
///         /* walk enablesarr, flip enables bits per +/- prefix */
///     } else {
///         /* enable all features */
///     }
///     if (enables_module(m, &enables)) return 2;
/// } else if (enablesarr) {
///     if (!(flags & FEAT_IGNORE)) zwarn("module does not support features");
///     return 1;
/// }
/// return ret;
/// ```
///
/// Prior Rust port confused module-name and enables-string into a
/// single `enablesarr: &str` param — the module-lookup and zwarn
/// diagnostic both used the feature-list string instead of the
/// module's name. Signature now separates them per C semantics:
/// modname identifies the module, features is the list of features
/// to enable (None = "enable all").
/// WARNING: param names don't match C — Rust=(table, modname, features, flags) vs C=(m, enablesarr, flags)
pub fn do_module_features(
    table: &mut modulestab,
    modname: &str,
    features: Option<&[String]>,
    flags: i32,
) -> i32 {
    // c:1998
    let mut module_features: Vec<String> = Vec::new(); // c:2000
    let mut ret: i32 = 0; // c:2001

    // c:2003 — `if (features_module(m, &features) == 0)`.
    if features_module(table, modname, &mut module_features) == 0 {
        // c:2011-2018 — fetch enables. Features supported → enables
        // should be too; error here is reported unless FEAT_IGNORE.
        let mut enables: Option<Vec<i32>> = None;
        if enables_module(table, modname, &mut enables) != 0 {
            // c:2012
            if (flags & FEAT_IGNORE) == 0 {
                // c:2014
                zwarn(&format!(
                    "error getting enabled features for module `{}'", // c:2015
                    modname
                ));
            }
            return 1; // c:2017
        }

        // c:2020 — `if ((flags & FEAT_CHECKAUTO) && m->autoloads)`
        if (flags & FEAT_CHECKAUTO) != 0 {
            let autoloads: Vec<String> = table
                .modules
                .get(modname)
                .and_then(|m| m.autoloads.as_ref())
                .map(|al| al.iter().cloned().collect())
                .unwrap_or_default();
            // c:2027-2074 — walk autoloads, cancel mismatches.
            for al in &autoloads {
                // c:2028
                // c:2032-2034 — match `al` against the features array.
                let found = module_features.iter().any(|f| f == al);
                if !found {
                    // c:2035
                    if (flags & FEAT_IGNORE) == 0 {
                        // c:2037
                        zwarn(&format!(
                            "module `{}' has no such feature: `{}': autoload cancelled", // c:2038-2040
                            modname, al
                        ));
                    }
                    // c:2045-2047 — autofeatures(NULL, m->node.nam, arg, 0, FEAT_IGNORE|FEAT_REMOVE)
                    let arg = vec![al.clone()];
                    autofeatures(
                        table,
                        "",
                        Some(modname),
                        &arg,
                        0,
                        FEAT_IGNORE | FEAT_REMOVE,
                    );
                    // c:2053-2072 — expunge from enablesarr.
                    // features arg is &[String] — Rust slice can't be
                    // mutated through &. The C path mutates the passed
                    // Feature_enables array; the Rust callers that need
                    // expunge build a fresh list. Skipped here.
                }
            }
        }

        // c:2077-2113 — apply enablesarr (or enable all).
        match features {
            Some(arr) => {
                // c:2079-2103 — walk enablesarr.
                let enables_vec = enables.get_or_insert_with(Vec::new);
                if enables_vec.len() < module_features.len() {
                    enables_vec.resize(module_features.len(), 0);
                }
                for fep_str in arr {
                    // c:2079 for (fep = enablesarr; fep->str; fep++)
                    let (on, esp) = if let Some(rest) = fep_str.strip_prefix('+') {
                        // c:2082-2083
                        (1i32, rest)
                    } else if let Some(rest) = fep_str.strip_prefix('-') {
                        // c:2084-2086
                        (0i32, rest)
                    } else {
                        (1i32, fep_str.as_str())
                    };
                    // c:2088-2094 — exact name match (pattern path
                    // skipped: zshrs doesn't carry the patprog from
                    // Feature_enables).
                    let mut found = false;
                    for (i, f) in module_features.iter().enumerate() {
                        if f == esp {
                            enables_vec[i] = on; // c:2090
                            found = true;
                            break; // c:2093 break-on-non-pat
                        }
                    }
                    if !found {
                        // c:2095-2102 — `module has no such feature`.
                        if (flags & FEAT_IGNORE) == 0 {
                            zwarn(&format!(
                                "module `{}' has no such feature: `{}'",
                                modname, esp
                            ));
                        }
                        return 1; // c:2101
                    }
                }
            }
            None => {
                // c:2105-2112 — enable all features.
                let enables_vec = enables.get_or_insert_with(Vec::new);
                enables_vec.clear();
                enables_vec.resize(module_features.len(), 1);
            }
        }

        // c:2115 — final `enables_module(m, &enables)` commits the bits.
        if enables_module(table, modname, &mut enables) != 0 {
            return 2; // c:2116
        }
    } else if features.is_some() {
        // c:2117-2121 — features_module failed AND enablesarr non-NULL:
        // module doesn't support features. zwarn unless FEAT_IGNORE.
        if (flags & FEAT_IGNORE) == 0 {
            zwarn(&format!(
                "module `{}' does not support features", // c:2119
                modname
            ));
        }
        return 1; // c:2120
    }
    // c:2122 — `Else it doesn't support features but we don't care.`
    let _ = &mut ret;
    ret // c:2124
}

/// Port of `deletemathfunc(MathFunc f)` from `Src/module.c:1342`.
///
/// C body:
/// ```c
/// deletemathfunc(MathFunc f) {
///     MathFunc p, q;
///     for (p = mathfuncs, q = NULL; p && p != f; q = p, p = p->next);
///     if (p) {
///         if (q) q->next = f->next; else mathfuncs = f->next;
///         if (f->module) {
///             zsfree(f->name); zsfree(f->module); zfree(f, sizeof(*f));
///         } else
///             f->flags &= ~MFF_ADDED;
///         return 0;
///     }
///     return -1;
/// }
/// ```
///
/// Removes math function `f` from the global registry. Returns 0
/// on hit, -1 on miss.

/// Port of `do_boot_module(Module m, Feature_enables enablesarr, int silent)` from `Src/module.c:2139`.
///
/// C body:
/// ```c
/// do_boot_module(Module m, Feature_enables enablesarr, int silent)
/// {
///     int ret = do_module_features(m, enablesarr,
///                                  silent ? FEAT_IGNORE|FEAT_CHECKAUTO :
///                                  FEAT_CHECKAUTO);
///     if (ret == 1) return 1;
///     if (boot_module(m)) return 1;
///     return ret;
/// }
/// ```
pub fn do_boot_module(
    table: &mut modulestab,
    modname: &str,
    features: Option<&[String]>,
    silent: i32,
) -> i32 {
    // c:2139
    let flags = if silent != 0 {
        // c:2142 — silent → IGNORE | CHECKAUTO
        FEAT_IGNORE | FEAT_CHECKAUTO
    } else {
        FEAT_CHECKAUTO // c:2143
    };
    let ret = do_module_features(table, modname, features, flags); // c:2141
    if ret == 1 {
        // c:2145
        return 1; // c:2146
    }
    if boot_module(table, modname) != 0 {
        // c:2148
        return 1; // c:2149
    }
    ret // c:2150
}

/// Port of `do_cleanup_module(Module m)` from `Src/module.c:2159`.
///
/// C body:
/// ```c
/// do_cleanup_module(Module m) {
///     return (m->node.flags & MOD_LINKED) ?
///         (m->u.linked && m->u.linked->cleanup(m)) :
///         (m->u.handle && cleanup_module(m));
/// }
/// ```
/// WARNING: param names don't match C — Rust=(table, name) vs C=(m)
pub fn do_cleanup_module(table: &mut modulestab, name: &str) -> i32 {
    // c:2159
    // Check the module is registered, then dispatch to cleanup_module.
    if table.modules.contains_key(name) {
        // c:2162 m->u.linked
        cleanup_module(table, name) // c:2163 cleanup_module(m)
    } else {
        0
    }
}

/// Port of `modname_ok(char const *p)` from `Src/module.c:2173`.
///
/// Returns 1 iff `p` is a valid module name: one or more
/// `/`-separated identifier segments.
///
/// C body:
/// ```c
/// modname_ok(char const *p)
/// {
///     do {
///         p = itype_end(p, IIDENT, 0);
///         if (!*p)
///             return 1;
///     } while(*p++ == '/');
///     return 0;
/// }
/// ```
pub fn modname_ok(p: &str) -> i32 {
    // c:2173
    let bytes = p.as_bytes();
    let mut i: usize = 0;
    loop {
        // c:2176 — `p = itype_end(p, IIDENT, 0);`
        // IIDENT = identifier-byte (alpha/digit/underscore + extended).
        while i < bytes.len() {
            let b = bytes[i];
            // Inline IIDENT check — alphanumeric or underscore. Mirrors
            // utils.c:itype_end stepping for the IIDENT bit.
            if b.is_ascii_alphanumeric() || b == b'_' {
                i += 1;
            } else {
                break;
            }
        }
        if i >= bytes.len() {
            // c:2177 if (!*p)
            return 1; // c:2178
        }
        if bytes[i] != b'/' {
            break;
        } // c:2179 while(*p++ == '/')
        i += 1;
    }
    0 // c:2180
}

/// Port of `removemathfunc(MathFunc previous, MathFunc current)` from `Src/module.c:1267`.
///
/// C body:
/// ```c
/// removemathfunc(MathFunc previous, MathFunc current)
/// {
///     if (previous)
///         previous->next = current->next;
///     else
///         mathfuncs = current->next;
///     zsfree(current->name);
///     zsfree(current->module);
///     zfree(current, sizeof(*current));
/// }
/// ```
///
/// Unlinks `current` from the global `mathfuncs` list and frees it.
/// Rust port: `previous` is unused since the underlying HashMap
/// removal doesn't need predecessor tracking.

/// Port of `require_module(const char *module, Feature_enables features, int silent)` from `Src/module.c:2344`.
///
/// C body c:2342-2360:
/// ```c
/// mod_export int
/// require_module(const char *module, Feature_enables features, int silent)
/// {
///     Module m = NULL;
///     int ret = 0;
///     queue_signals();
///     m = find_module(module, FINDMOD_ALIASP, &module);
///     if (!m || !m->u.handle ||
///         (m->node.flags & MOD_UNLOAD))
///         ret = load_module(module, features, silent);
///     else
///         ret = do_module_features(m, features, 0);
///     unqueue_signals();
///     return ret;
/// }
/// ```
///
/// Two branches: when the module isn't loaded yet (or is mid-unload),
/// route through `load_module` which runs the full setup → features →
/// boot lifecycle. When it's already loaded, skip to
/// `do_module_features` to just enable the requested per-feature
/// surface — much cheaper.
///
/// Static-link analog of `m->u.handle` is `MOD_INIT_B` (boot ran):
/// once boot_module has fired, the module is "loaded" in zshrs's
/// non-dlopen world.
/// WARNING: param names don't match C — Rust=(table, modname, features, silent) vs C=(module, features, silent)
pub fn require_module(
    table: &mut modulestab,
    modname: &str,
    features: Option<&[String]>,
    silent: i32,
) -> i32 {
    // c:2344
    // c:2350 — queue_signals(): signal-deferral wrapper.
    crate::ported::signals::queue_signals();

    // c:2351 — `m = find_module(module, FINDMOD_ALIASP, &module);`
    // Resolves alias chain; canonical name lives in `mname`.
    let mname_opt = find_module(table, modname, FINDMOD_ALIASP);

    // c:2352-2353 — `if (!m || !m->u.handle || MOD_UNLOAD)`.
    // Static-link analog of `m->u.handle`: MOD_INIT_B (boot ran).
    let needs_load = match &mname_opt {
        None => true, // c:2352 !m
        Some(mname) => match table.modules.get(mname) {
            None => true,
            Some(m) => {
                (m.node.flags & MOD_INIT_B) == 0 // c:2352 !u.handle analog
                    || (m.node.flags & MOD_UNLOAD) != 0 // c:2353
            }
        },
    };

    let mname = mname_opt.unwrap_or_else(|| modname.to_string());

    let ret = if needs_load {
        // c:2354 — `ret = load_module(module, features, silent);`
        // try_load_module gates the static-link path. On miss, emit
        // the canonical zwarn (gated by silent).
        if try_load_module(table, &mname) == 0 {
            if silent == 0 {
                crate::ported::utils::zwarn(&format!("failed to load module `{}'", mname));
            }
            crate::ported::signals::unqueue_signals();
            return 1;
        }
        if !table.load_module(&mname) {
            crate::ported::signals::unqueue_signals();
            return 1;
        }
        0
    } else {
        // c:2356 — `ret = do_module_features(m, features, 0);`
        // Module already loaded; just enable the requested features.
        // features=NULL in C means "enable all features"; the Rust
        // do_module_features takes a single enablesstr arg, so flatten
        // C: features=NULL means "enable all"; pass through.
        do_module_features(table, &mname, features, 0)
    };

    // c:2357 — unqueue_signals();
    crate::ported::signals::unqueue_signals();

    ret // c:2359
}

/// Port of `add_dep(const char *name, char *from)` from `Src/module.c:2369`.
///
/// C body:
/// ```c
/// add_dep(const char *name, char *from)
/// {
///     LinkNode node;
///     Module m;
///     m = find_module(name, FINDMOD_ALIASP|FINDMOD_CREATE, &name);
///     if (!m->deps)
///         m->deps = znewlinklist();
///     for (node = firstnode(m->deps);
///          node && strcmp((char *) getdata(node), from);
///          incnode(node));
///     if (!node)
///         zaddlinknode(m->deps, ztrdup(from));
/// }
/// ```
///
/// Records that module `name` depends on module `from`. Resolves
/// aliases so dependency graphs always point at canonical names.
/// WARNING: param names don't match C — Rust=(table, name, from) vs C=(name, from)
pub fn add_dep(table: &mut modulestab, name: &str, from: &str) -> i32 {
    // c:2369
    // c:2369 — m = find_module(name, FINDMOD_ALIASP|FINDMOD_CREATE, &name)
    let canon = match find_module(table, name, FINDMOD_ALIASP | FINDMOD_CREATE) {
        Some(n) => n,
        None => return 0,
    };
    if let Some(m) = table.modules.get_mut(&canon) {
        // c:2389-2391 — walk deps, skip if `from` already present.
        let deps = m
            .deps
            .get_or_insert_with(crate::ported::linklist::LinkList::new);
        if !deps.iter().any(|d| d == from) {
            // c:2392 if (!node)
            deps.push_back(from.to_string()); // c:2393 zaddlinknode
        }
    }
    0
}

/// Port of `autoloadscan(HashNode hn, int printflags)` from `Src/module.c:2403`.
///
/// C body:
/// ```c
/// autoloadscan(HashNode hn, int printflags)
/// {
///     Builtin bn = (Builtin) hn;
///     if(bn->node.flags & BINF_ADDED)
///         return;
///     if(printflags & PRINT_LIST) {
///         fputs("zmodload -ab ", stdout);
///         if(bn->optstr[0] == '-') fputs("-- ", stdout);
///         quotedzputs(bn->optstr, stdout);
///         if(strcmp(bn->node.nam, bn->optstr)) {
///             putchar(' ');
///             quotedzputs(bn->node.nam, stdout);
///         }
///     } else {
///         nicezputs(bn->node.nam, stdout);
///         if(strcmp(bn->node.nam, bn->optstr)) {
///             fputs(" (", stdout);
///             nicezputs(bn->optstr, stdout);
///             putchar(')');
///         }
///     }
///     putchar('\n');
/// }
/// ```
///
/// Hash-table scan callback for autoloadable-builtin listing.
/// `printflags & PRINT_LIST` selects long form (`zmodload -ab MOD NAME`)
/// vs short form (`NAME (MOD)`). Skips already-registered builtins
/// (BINF_ADDED set).
/// WARNING: param names don't match C — Rust=(name, optstr, flags, printflags) vs C=(hn, printflags)
pub fn autoloadscan(name: &str, optstr: &str, flags: u32, printflags: i32) {
    // c:2403
    use crate::ported::utils::{nicezputs, quotedzputs};
    let mut stdout = std::io::stdout();
    if (flags & BINF_ADDED) != 0 {
        // c:2407
        return; // c:2408
    }
    if (printflags & PRINT_LIST) != 0 {
        // c:2409
        // c:2410-2417 — long form `zmodload -ab MOD NAME`
        print!("zmodload -ab ");
        if optstr.starts_with('-') {
            // c:2411
            print!("-- "); // c:2412
        }
        // c:2413 — `quotedzputs(bn->optstr, stdout);`
        print!("{}", quotedzputs(optstr));
        if name != optstr {
            // c:2414 — `if(strcmp(bn->node.nam, bn->optstr))`
            print!(" "); // c:2415 putchar(' ')
                         // c:2416 — `quotedzputs(bn->node.nam, stdout);`
            print!("{}", quotedzputs(name));
        }
    } else {
        // c:2418-2424 — short form `NAME (MOD)`
        let _ = nicezputs(name, &mut stdout); // c:2419
        if name != optstr {
            // c:2420 — `if(strcmp(bn->node.nam, bn->optstr))`
            print!(" ("); // c:2421
            let _ = nicezputs(optstr, &mut stdout); // c:2422
            print!(")"); // c:2423
        }
    }
    println!(); // c:2426 putchar('\n')
}

/// Direct port of `bin_zmodload(char *nam, char **args, Options ops, UNUSED(int func))` from `Src/module.c:2440`.
/// Top-level dispatcher for the `zmodload` builtin. Validates flag
/// combinations then routes to one of the per-mode helpers:
///   -F        → bin_zmodload_features (c:3003)
///   -e        → bin_zmodload_exist    (c:2623)
///   -d        → bin_zmodload_dep      (c:2649)
///   -a/-b/-c/-p/-f → bin_zmodload_auto (c:2726)
///   default   → bin_zmodload_load     (c:2971)
///   -A/-R     → bin_zmodload_alias    (c:2515)
/// WARNING: param names don't match C — Rust=(nam, args, _func) vs C=(nam, args, ops, func)
pub fn bin_zmodload(
    nam: &str,
    args: &[String], // c:2440
    ops: &options,
    _func: i32,
) -> i32 {
    let mut table = MODULESTAB.lock().unwrap();
    let table = &mut *table;

    let ops_bcpf = OPT_ISSET(ops, b'b') || OPT_ISSET(ops, b'c')              // c:2443
                || OPT_ISSET(ops, b'p') || OPT_ISSET(ops, b'f');
    let ops_au = OPT_ISSET(ops, b'a') || OPT_ISSET(ops, b'u'); // c:2445
    let mut ret: i32; // c:2446

    if ops_bcpf && !ops_au {
        // c:2451
        zwarnnam(nam, "-b, -c, -f, and -p must be combined with -a or -u"); // c:2452
        return 1; // c:2453
    }
    if OPT_ISSET(ops, b'F') && (ops_bcpf || OPT_ISSET(ops, b'u')) {
        // c:2455
        zwarnnam(nam, "-b, -c, -f, -p and -u cannot be combined with -F"); // c:2456
        return 1; // c:2457
    }
    if OPT_ISSET(ops, b'A') || OPT_ISSET(ops, b'R') {
        // c:2459
        if ops_bcpf || ops_au || OPT_ISSET(ops, b'd')                        // c:2460
           || (OPT_ISSET(ops, b'R') && OPT_ISSET(ops, b'e'))
        {
            zwarnnam(nam, "illegal flags combined with -A or -R"); // c:2462
            return 1; // c:2463
        }
        if !OPT_ISSET(ops, b'e') {
            // c:2465
            return bin_zmodload_alias(table, nam, args, ops); // c:2466
        }
    }
    if OPT_ISSET(ops, b'd') && OPT_ISSET(ops, b'a') {
        // c:2468
        zwarnnam(nam, "-d cannot be combined with -a"); // c:2469
        return 1; // c:2470
    }
    if OPT_ISSET(ops, b'u') && args.is_empty() {
        // c:2472
        zwarnnam(nam, "what do you want to unload?"); // c:2473
        return 1; // c:2474
    }
    if OPT_ISSET(ops, b'e')
        && (OPT_ISSET(ops, b'I') || OPT_ISSET(ops, b'L') // c:2476
        || (OPT_ISSET(ops, b'a') && !OPT_ISSET(ops, b'F'))
        || OPT_ISSET(ops, b'd') || OPT_ISSET(ops, b'i')
        || OPT_ISSET(ops, b'u'))
    {
        zwarnnam(nam, "-e cannot be combined with other options"); // c:2480
        return 1; // c:2482
    }
    // c:2484 — `for (fp = fonly; *fp; fp++)` — `l` and `P` only with `-F`.
    for fp in [b'l', b'P'] {
        // c:2484
        if OPT_ISSET(ops, fp) && !OPT_ISSET(ops, b'F') {
            // c:2485
            zwarnnam(nam, &format!("-{} is only allowed with -F", fp as char)); // c:2486
            return 1; // c:2487
        }
    }
    crate::ported::mem::queue_signals(); // c:2490
    if OPT_ISSET(ops, b'F') {
        // c:2491
        ret = bin_zmodload_features(table, nam, args, ops); // c:2492
    } else if OPT_ISSET(ops, b'e') {
        // c:2493
        ret = bin_zmodload_exist(table, nam, args, ops); // c:2494
    } else if OPT_ISSET(ops, b'd') {
        // c:2495
        ret = bin_zmodload_dep(table, nam, args, ops); // c:2496
    } else {
        let autoopts = (OPT_ISSET(ops, b'b') as i32)                         // c:2497
                     + (OPT_ISSET(ops, b'c') as i32)
                     + (OPT_ISSET(ops, b'p') as i32)
                     + (OPT_ISSET(ops, b'f') as i32);
        if autoopts != 0 || OPT_ISSET(ops, b'a') {
            // c:2497-2499
            if autoopts > 1 {
                // c:2502
                zwarnnam(nam, "use only one of -b, -c, or -p"); // c:2503
                ret = 1; // c:2504
            } else {
                ret = bin_zmodload_auto(table, nam, args, ops); // c:2506
            }
        } else {
            ret = bin_zmodload_load(table, nam, args, ops); // c:2508
        }
    }
    unqueue_signals(); // c:2515
    ret // c:2515
}

/// Port of `bin_zmodload_alias(char *nam, char **args, Options ops)` from `Src/module.c:2515`.
///
/// `zmodload -A [-L|-R] [name=alias ...]`. Three modes:
/// - no args: list all module aliases (`-L` = long form).
/// - `-R name`: remove alias `name` (must already be MOD_ALIAS).
/// - `name=target`: install/replace alias `name` pointing at `target`.
///   Detects self-cycles before committing.
/// WARNING: param names don't match C — Rust=(table, nam, args, ops) vs C=(nam, args, ops)
pub fn bin_zmodload_alias(
    table: &mut modulestab,
    nam: &str,
    args: &[String],
    ops: &options,
) -> i32 {
    // c:2515
    /*
     * TODO: while it would be too nasty to have aliases, as opposed
     * to real loadable modules, with dependencies --- just what would
     * we need to load when, exactly? --- there is in principle no objection
     * to making it possible to force an alias onto an existing unloaded
     * module which has dependencies.  This would simply transfer
     * the dependencies down the line to the aliased-to module name.
     * This is actually useful, since then you can alias zsh/zle=mytestzle
     * to load another version of zle.  But then what happens when the
     * alias is removed?  Do you transfer the dependencies back? And
     * suppose other names are aliased to the same file?  It might be
     * kettle of fish best left unwormed.
     */                                                                  // c:2517-2529

    // c:2532-2541 — no args: list aliases.
    // C body:
    //   if (!*args) {
    //       if (OPT_ISSET(ops,'R')) {
    //           zwarnnam(nam, "no module alias to remove");
    //           return 1;
    //       }
    //       scanhashtable(modulestab, 1, MOD_ALIAS, 0,
    //                     modulestab->printnode,
    //                     OPT_ISSET(ops,'L') ? PRINTMOD_LIST : 0);
    //       return 0;
    //   }
    //
    // scanhashtable args: sorted=1, INCLUDE=MOD_ALIAS, EXCLUDE=0.
    // Only MOD_ALIAS entries pass, walked in name order. Each entry
    // dispatched through printmodulenode (the alias-emit arm).
    if args.is_empty() {
        if OPT_ISSET(ops, b'R') {
            // c:2533
            zwarnnam(nam, "no module alias to remove"); // c:2534
            return 1; // c:2535
        }
        let listflags = if OPT_ISSET(ops, b'L') {
            PRINTMOD_LIST
        } else {
            0
        };
        let mut names: Vec<&String> = table
            .modules
            .iter()
            .filter(|(_, m)| (m.node.flags & MOD_ALIAS) != 0) // c:2537 INCLUDE
            .map(|(n, _)| n)
            .collect();
        names.sort(); // c:2537 sorted=1
        for name in names {
            let m = &table.modules[name];
            let line = printmodulenode(name, m, listflags);
            if !line.is_empty() {
                println!("{}", line);
            }
        }
        return 0; // c:2540
    }

    // c:2543 — for each arg, parse name=alias and dispatch.
    for arg in args {
        // c:2544-2547 — split at '='
        let (lhs, aliasname): (&str, Option<&str>) = match arg.find('=') {
            Some(eq) => (&arg[..eq], Some(&arg[eq + 1..])),
            None => (arg.as_str(), None),
        };
        // c:2548 — modname_ok check on the LHS
        if modname_ok(lhs) == 0 {
            // c:2548
            zwarnnam(nam, &format!("invalid module name `{}'", lhs)); // c:2549
            return 1; // c:2550
        }
        if OPT_ISSET(ops, b'R') {
            // c:2552
            // -R: remove alias path.
            if aliasname.is_some() {
                // c:2553
                zwarnnam(
                    nam,
                    &format!("bad syntax for removing module alias: {}", lhs),
                ); // c:2554
                return 1; // c:2556
            }
            // c:2558 — find_module(lhs, 0, NULL)
            match table.modules.get(lhs) {
                Some(m) => {
                    if (m.node.flags & MOD_ALIAS) == 0 {
                        // c:2560
                        zwarnnam(nam, &format!("module is not an alias: {}", lhs)); // c:2561
                        return 1; // c:2562
                    }
                    table.modules.remove(lhs); // c:2564 delete_module
                }
                None => {
                    zwarnnam(nam, &format!("no such module alias: {}", lhs)); // c:2566
                    return 1; // c:2567
                }
            }
        } else {
            // No -R: install/replace alias OR list one.
            if let Some(target) = aliasname {
                // c:2570
                if modname_ok(target) == 0 {
                    // c:2572
                    zwarnnam(nam, &format!("invalid module name `{}'", target)); // c:2573
                    return 1; // c:2574
                }
                // c:2576-2584 — cycle detection: walk alias chain
                let mut mname = target;
                let mut depth = 0;
                loop {
                    if depth > 256 {
                        break;
                    }
                    depth += 1;
                    if mname == lhs {
                        // c:2577
                        zwarnnam(nam, &format!("module alias would refer to itself: {}", lhs)); // c:2578
                        return 1; // c:2580
                    }
                    match table.modules.get(mname) {
                        Some(m) if (m.node.flags & MOD_ALIAS) != 0 => {
                            mname = m.alias.as_deref().unwrap_or("");
                        }
                        _ => break,
                    }
                }
                // c:2585-2596 — install or replace
                if let Some(m) = table.modules.get_mut(lhs) {
                    if (m.node.flags & MOD_ALIAS) == 0 {
                        // c:2587
                        zwarnnam(nam, &format!("module is not an alias: {}", lhs)); // c:2588
                        return 1; // c:2589
                    }
                    m.alias = Some(target.to_string()); // c:2591/2597
                } else {
                    let mut m = module::new(lhs); // c:2593 zshcalloc
                    m.node.flags = MOD_ALIAS; // c:2594
                    m.alias = Some(target.to_string()); // c:2597
                    table.modules.insert(lhs.to_string(), m); // c:2595
                }
            } else {
                // c:2599-2611 — list one alias.
                // C body:
                //   if ((m = find_module(*args, 0, NULL))) {
                //       if (m->node.flags & MOD_ALIAS)
                //           modulestab->printnode(&m->node,
                //                                 OPT_ISSET(ops,'L')
                //                                     ? PRINTMOD_LIST : 0);
                //       else { zwarnnam(...); return 1; }
                //   } else { zwarnnam(...); return 1; }
                match table.modules.get(lhs) {
                    Some(m) if (m.node.flags & MOD_ALIAS) != 0 => {
                        // c:2601-2603 — printnode dispatch
                        let listflags = if OPT_ISSET(ops, b'L') {
                            PRINTMOD_LIST
                        } else {
                            0
                        };
                        let line = printmodulenode(lhs, m, listflags);
                        if !line.is_empty() {
                            println!("{}", line);
                        }
                    }
                    Some(_) => {
                        zwarnnam(nam, &format!("module is not an alias: {}", lhs)); // c:2605
                        return 1; // c:2606
                    }
                    None => {
                        zwarnnam(nam, &format!("no such module alias: {}", lhs)); // c:2609
                        return 1; // c:2610
                    }
                }
            }
        }
    }
    0 // c:2616
}

/// Port of `bin_zmodload_exist(UNUSED(char *nam), char **args, Options ops)` from `Src/module.c:2623`.
///
/// C body:
/// ```c
/// bin_zmodload_exist(UNUSED(char *nam), char **args, Options ops)
/// {
///     Module m;
///     if (!*args) {
///         scanhashtable(modulestab, 1, 0, 0, modulestab->printnode,
///                       OPT_ISSET(ops,'A') ? PRINTMOD_EXIST|PRINTMOD_ALIAS :
///                       PRINTMOD_EXIST);
///         return 0;
///     } else {
///         int ret = 0;
///         for (; !ret && *args; args++) {
///             if (!(m = find_module(*args, FINDMOD_ALIASP, NULL))
///                 || !m->u.handle
///                 || (m->node.flags & MOD_UNLOAD))
///                 ret = 1;
///         }
///         return ret;
///     }
/// }
/// ```
///
/// `zmodload [-A]` lists or tests module presence. Returns 0 if all
/// named modules exist (or if no args, after listing); 1 if any
/// named module is missing/unloading.
/// WARNING: param names don't match C — Rust=(table, _nam, args, _ops) vs C=(nam, args, ops)
pub fn bin_zmodload_exist(
    table: &mut modulestab,
    _nam: &str,
    args: &[String],
    ops: &options,
) -> i32 {
    // c:2623
    if args.is_empty() {
        // c:2627
        // c:2628-2630 — scanhashtable(modulestab, 1, 0, 0,
        //                              modulestab->printnode,
        //                              OPT_ISSET(ops,'A')
        //                                  ? PRINTMOD_EXIST|PRINTMOD_ALIAS
        //                                  : PRINTMOD_EXIST);
        // Sorted walk (the `1` 2nd arg) over the full table (no
        // INCLUDE/EXCLUDE filters), dispatching every entry through
        // printmodulenode. -A toggles PRINTMOD_ALIAS so alias entries
        // emit their alias target alongside the existence line.
        let printflags = if OPT_ISSET(ops, b'A') {
            PRINTMOD_EXIST | PRINTMOD_ALIAS
        } else {
            PRINTMOD_EXIST
        };
        let mut names: Vec<&String> = table.modules.keys().collect();
        names.sort(); // c:2628 sorted=1
        for name in names {
            let m = &table.modules[name];
            let line = printmodulenode(name, m, printflags);
            if !line.is_empty() {
                println!("{}", line);
            }
        }
        return 0; // c:2631
    }
    // c:2633-2640 — for each arg, test existence.
    // C:
    //   for (; !ret && *args; args++) {
    //       if (!(m = find_module(*args, FINDMOD_ALIASP, NULL))
    //           || !m->u.handle
    //           || (m->node.flags & MOD_UNLOAD))
    //           ret = 1;
    //   }
    // The `!m->u.handle` clause is union-typed in C: for static-link
    // modules it reads `m->u.linked` (non-NULL when linked, NULL when
    // pre-registered but not yet bound); for dynamic modules it's the
    // dlopen handle. Translate to: handle.is_none() && linked.is_none()
    // — both representations of "no live binding."
    let mut ret: i32 = 0;
    for arg in args {
        // c:2635
        if ret != 0 {
            break;
        }
        let canon = match find_module(table, arg, FINDMOD_ALIASP) {
            // c:2636
            Some(n) => n,
            None => {
                ret = 1; // c:2639
                continue;
            }
        };
        let live = match table.modules.get(&canon) {
            Some(m) => {
                // c:2637 — `!m->u.handle` (union-semantics).
                let bound = m.handle.is_some() || m.linked.is_some();
                // c:2638 — `(m->node.flags & MOD_UNLOAD)`.
                let unloading = (m.node.flags & MOD_UNLOAD) != 0;
                bound && !unloading
            }
            None => false,
        };
        if !live {
            ret = 1; // c:2639
        }
    }
    ret // c:2641
}

/// Port of `bin_zmodload_dep(UNUSED(char *nam), char **args, Options ops)` from `Src/module.c:2649`.
///
/// `zmodload -d [-u] [target [dep ...]]`. Three modes:
/// - `-u target` removes all deps from target; `-u target d1 d2` removes
///   only those.
/// - no args lists all dependencies.
/// - `target dep1 ...` adds each dep to target's dependency list.
/// WARNING: param names don't match C — Rust=(table, _nam, args, ops) vs C=(nam, args, ops)
pub fn bin_zmodload_dep(table: &mut modulestab, _nam: &str, args: &[String], ops: &options) -> i32 {
    // c:2649
    if OPT_ISSET(ops, b'u') {
        // c:2652
        // c:2654 — `const char *tnam = *args++;`
        if args.is_empty() {
            return 0;
        }
        let tnam = &args[0];
        let rest = &args[1..];
        // c:2655 — `find_module(tnam, FINDMOD_ALIASP, &tnam)`
        let canon = match find_module(table, tnam, FINDMOD_ALIASP) {
            Some(n) => n,
            None => return 0, // c:2656-2657
        };
        if let Some(m) = table.modules.get_mut(&canon) {
            if !rest.is_empty() && m.deps.is_some() {
                // c:2658-2671 — remove specific deps.
                // C body c:2659-2667 walks args, finds each in deps,
                // removes the matched node; the inner break ends the
                // current arg's search but the outer while continues
                // to the next arg. Mirrors with a for-loop.
                let deps = m.deps.as_mut().unwrap();
                for to_remove in rest {
                    // c:2659 do { ... } while(*++args)
                    if let Some(pos) = deps.iter().position(|d| d == to_remove) {
                        deps.delete_node(pos); // c:2664 remnode
                    }
                }
                // c:2668-2671 — if deps now empty, free the list.
                if deps.is_empty() {
                    m.deps = None;
                }
            } else if m.deps.is_some() {
                // c:2672-2676 — no specific deps given: clear all.
                m.deps = None;
            }
            // c:2678-2679 — `if (!m->deps && !m->u.handle) delete_module(m);`
            // Static-link analog of `!u.handle` is `!MOD_INIT_B` (boot
            // hasn't run = no loaded handle). Without the MOD_INIT_B
            // gate, the prior port deleted any module whose deps got
            // cleared, dropping the modulestab entries for already-
            // loaded modules (zsh/main, zsh/watch, etc.) the moment
            // their dep list went empty.
            let no_deps = m.deps.is_none();
            let no_handle = (m.node.flags & MOD_INIT_B) == 0;
            if no_deps && no_handle {
                table.modules.remove(&canon); // c:2679 delete_module
            }
        }
        return 0; // c:2680
    }
    // c:2681 — `else if (!args[0] || !args[1])`: list-mode (one or all).
    if args.is_empty() || args.len() == 1 {
        // c:2682-2691 — `int depflags = OPT_ISSET(ops,'L')
        //                  ? PRINTMOD_DEPS|PRINTMOD_LIST : PRINTMOD_DEPS;`
        let depflags = if OPT_ISSET(ops, b'L') {
            PRINTMOD_DEPS | PRINTMOD_LIST
        } else {
            PRINTMOD_DEPS
        };
        if !args.is_empty() {
            // c:2685-2687 — single-name list:
            //   if ((m = modulestab->getnode2(modulestab, args[0])))
            //       modulestab->printnode(&m->node, depflags);
            if let Some(m) = table.modules.get(&args[0]) {
                let line = printmodulenode(&args[0], m, depflags);
                if !line.is_empty() {
                    println!("{}", line);
                }
            }
        } else {
            // c:2688-2691 — full sorted scan.
            //   scanhashtable(modulestab, 1, 0, 0, printnode, depflags);
            let mut names: Vec<&String> = table.modules.keys().collect();
            names.sort(); // c:2689 sorted=1
            for name in names {
                let m = &table.modules[name];
                let line = printmodulenode(name, m, depflags);
                if !line.is_empty() {
                    println!("{}", line);
                }
            }
        }
        return 0; // c:2692
    }
    // c:2693-2701 — add deps: args[0] target, args[1..] deps to add.
    let target = &args[0];
    for dep in &args[1..] {
        add_dep(table, target, dep); // c:2699
    }
    0 // c:2700 (ret stays 0)
}

/// Port of `printautoparams(HashNode hn, int lon)` from `Src/module.c:2710`.
///
/// C body:
/// ```c
/// printautoparams(HashNode hn, int lon)
/// {
///     Param pm = (Param) hn;
///     if (pm->node.flags & PM_AUTOLOAD) {
///         if (lon)
///             printf("zmodload -ap %s %s\n", pm->u.str, pm->node.nam);
///         else
///             printf("%s (%s)\n", pm->node.nam, pm->u.str);
///     }
/// }
/// ```
///
/// Hash-table scan callback for `zmodload -ap` listing. Rust port
/// takes a `(name, module, flags)` triple instead of a HashNode ptr
/// since zshrs's autoload-params live in `ModuleTable.autoload_params`.
/// WARNING: param names don't match C — Rust=(name, module, flags, lon) vs C=(hn, lon)
pub fn printautoparams(name: &str, module: &str, flags: u32, lon: i32) {
    // c:2710
    if (flags & PM_AUTOLOAD) != 0 {
        // c:2710
        if lon != 0 {
            // c:2715
            // c:2716 — printf("zmodload -ap %s %s\n", pm->u.str, pm->node.nam);
            println!("zmodload -ap {} {}", module, name);
        } else {
            // c:2718 — printf("%s (%s)\n", pm->node.nam, pm->u.str);
            println!("{} ({})", name, module);
        }
    }
}

/// Port of `bin_zmodload_auto(char *nam, char **args, Options ops)` from `Src/module.c:2726`.
///
/// `zmodload [-c] [-p] [-f] [-a] module name [name ...]` —
/// register-autoload of builtins/conditions/params/mathfns. C body
/// (80 lines) walks the appropriate dispatch table per opt flag.
///
/// `-c` lists/registers conditions, `-p` parameters, `-f` math ported,
/// default is builtins. `-L` toggles long-form listing.
///
/// Static-link path: registers via `add_autoaliasbuiltin` /
/// `add_autoparam` / `add_automathfunc` already ported. Without a
/// module name (just `-a`), runs the listing scan via `autoloadscan`
/// or its conddef/param/mathfn equivalents.
/// WARNING: param names don't match C — Rust=(table, _nam, args, ops) vs C=(nam, args, ops)
pub fn bin_zmodload_auto(
    table: &mut modulestab,
    _nam: &str,
    args: &[String],
    ops: &options,
) -> i32 {
    // c:2726
    let fchar: char; // c:2726
    let _flags: i32 = if OPT_ISSET(ops, b'i') { FEAT_IGNORE } else { 0 }; // c:2728

    // c:2731-2753 — conditions branch (-c).
    // C body:
    //   if (!*args) {
    //       Conddef p;
    //       for (p = condtab; p; p = p->next) {
    //           if (p->module) {
    //               if (OPT_ISSET(ops,'L')) {
    //                   fputs("zmodload -ac", stdout);
    //                   if (p->flags & CONDF_INFIX) putchar('I');
    //                   printf(" %s %s\n", p->module, p->name);
    //               } else {
    //                   if (p->flags & CONDF_INFIX) fputs("infix ", stdout);
    //                   else fputs("post ", stdout);
    //                   printf("%s (%s)\n", p->name, p->module);
    //               }
    //           }
    //       }
    //       return 0;
    //   }
    // C walks condtab in registration order (linked-list head→tail).
    // The Rust port walks CONDTAB (same shape, Vec instead of list).
    if OPT_ISSET(ops, b'c') {
        fchar = if OPT_ISSET(ops, b'I') { 'C' } else { 'c' };
        let _ = fchar;
        if args.is_empty() {
            let snap: Vec<(String, i32, String)> = {
                let tab = CONDTAB.lock().unwrap();
                tab.iter()
                    // c:2737 — `if (p->module)` skips entries without
                    // an owning module (built-in cond defs use NULL).
                    .filter_map(|p| {
                        p.module
                            .as_ref()
                            .map(|m| (p.name.clone(), p.flags, m.clone()))
                    })
                    .collect()
            };
            let l_flag = OPT_ISSET(ops, b'L');
            for (name, flags, module) in snap {
                if l_flag {
                    // c:2738-2742 — `zmodload -ac[I] MODULE NAME`.
                    if (flags & CONDF_INFIX) != 0 {
                        // c:2740
                        println!("zmodload -acI {} {}", module, name);
                    } else {
                        println!("zmodload -ac {} {}", module, name);
                    }
                } else {
                    // c:2743-2748 — `infix NAME (MODULE)` /
                    //                `post NAME (MODULE)`.
                    let kind = if (flags & CONDF_INFIX) != 0 {
                        "infix"
                    } else {
                        "post"
                    };
                    println!("{} {} ({})", kind, name, module);
                }
            }
            return 0;
        }
    } else if OPT_ISSET(ops, b'p') {
        // c:2755-2761 — params branch.
        // C body:
        //   if (!*args) {
        //       /* list autoloaded parameters */
        //       scanhashtable(paramtab, 1, 0, 0, printautoparams,
        //                     OPT_ISSET(ops,'L'));
        //       return 0;
        //   }
        // The `sorted=1` (2nd arg) walks the table in name order.
        // `printautoparams` (c:2710) checks `pm->flags & PM_AUTOLOAD`
        // and emits either `zmodload -ap MODULE NAME` (under `-L`)
        // or `NAME (MODULE)` form. Module name is read from
        // `pm->u.str`, which `add_autoparam` (c:1218) sets to the
        // module that registered the autoload stub.
        if args.is_empty() {
            let lon = if OPT_ISSET(ops, b'L') { 1 } else { 0 };
            let entries: Vec<(String, u32, String)> = {
                let tab = crate::ported::params::paramtab()
                    .read()
                    .expect("paramtab poisoned");
                let mut v: Vec<(String, u32, String)> = tab
                    .iter()
                    .filter(|(_, p)| {
                        (p.node.flags as u32 & crate::ported::zsh_h::PM_AUTOLOAD) != 0
                    })
                    .map(|(name, p)| {
                        (
                            name.clone(),
                            p.node.flags as u32,
                            p.u_str.clone().unwrap_or_default(),
                        )
                    })
                    .collect();
                v.sort_by(|a, b| a.0.cmp(&b.0)); // c:2758 sorted=1
                v
            };
            for (name, flags, module) in entries {
                printautoparams(&name, &module, flags, lon); // c:2758
            }
            return 0;
        }
    } else if OPT_ISSET(ops, b'f') {
        // c:2763-2778 — math-function branch (-f).
        // C body:
        //   if (!*args) {
        //       MathFunc p;
        //       for (p = mathfuncs; p; p = p->next) {
        //           if (!(p->flags & MFF_USERFUNC) && p->module) {
        //               if (OPT_ISSET(ops,'L')) {
        //                   fputs("zmodload -af", stdout);
        //                   printf(" %s %s\n", p->module, p->name);
        //               } else
        //                   printf("%s (%s)\n", p->name, p->module);
        //           }
        //       }
        //       return 0;
        //   }
        // C walks `mathfuncs` (file-static linked list). Rust port
        // walks MATHFUNCS (same shape, Vec). The MFF_USERFUNC filter
        // excludes `functions -M` user math from the autoload list.
        if args.is_empty() {
            let snap: Vec<(String, String)> = {
                let tab = MATHFUNCS.lock().unwrap();
                tab.iter()
                    // c:2769 — `!(p->flags & MFF_USERFUNC) && p->module`.
                    .filter_map(|p| {
                        if (p.flags & MFF_USERFUNC) != 0 {
                            return None;
                        }
                        p.module.as_ref().map(|m| (p.name.clone(), m.clone()))
                    })
                    .collect()
            };
            let l_flag = OPT_ISSET(ops, b'L');
            for (name, module) in snap {
                if l_flag {
                    // c:2770-2772 — `zmodload -af MODULE NAME`.
                    println!("zmodload -af {} {}", module, name);
                } else {
                    // c:2774 — `NAME (MODULE)`.
                    println!("{} ({})", name, module);
                }
            }
            return 0;
        }
    } else {
        // Default: builtins branch
        if args.is_empty() {
            // c:Src/module.c:2756 — `scanhashtable(autoloadtab, 1, 0,
            //   0, ...)`. The sorted=1 arg sorts entries by name before
            // dispatch. zshrs's HashMap iteration is unordered, so the
            // 27 auto-loaded entries came out in random order. Bug
            // #222 in docs/BUGS.md. Sort by name to match zsh.
            let mut entries: Vec<(&String, &String)> =
                table.autoload_builtins.iter().collect();
            entries.sort_by(|a, b| a.0.cmp(b.0));
            for (name, module) in entries {
                autoloadscan(
                    name,
                    module,
                    0,
                    if OPT_ISSET(ops, b'L') { PRINT_LIST } else { 0 },
                );
            }
            return 0;
        }
    }

    // c:2791-2805 — register/unregister via autofeatures with
    // FEAT_AUTOALL. (Earlier zshrs revision inserted into the
    // autoload_* maps directly, bypassing autofeatures — so
    // `m->autoloads` bookkeeping and the add_autobin/add_autoparam/…
    // canonical-table dispatch never ran.)
    let fchar: u8 = if OPT_ISSET(ops, b'c') {
        if OPT_ISSET(ops, b'I') {
            b'C' // c:2754 fchar = OPT_ISSET(ops,'I') ? 'C' : 'c'
        } else {
            b'c'
        }
    } else if OPT_ISSET(ops, b'p') {
        b'p' // c:2762
    } else if OPT_ISSET(ops, b'f') {
        b'f' // c:2779
    } else {
        b'b' // c:2789
    };
    let mut flags = FEAT_AUTOALL; // c:2791
    if OPT_ISSET(ops, b'i') {
        flags |= FEAT_IGNORE; // c:2792-2793
    }
    if OPT_ISSET(ops, b'u') {
        /* remove autoload */ // c:2795
        flags |= FEAT_REMOVE; // c:2796
        // c:2797 — `modnam = NULL;` — every arg is a feature name.
        return autofeatures(table, _nam, None, args, fchar, flags); // c:2805
    }
    /* add autoload */ // c:2799
    let modnam = &args[0]; // c:2800
    // c:2802-2803 — `if (args[1]) args++;` — with a single arg the
    // module name doubles as the feature arg (C quirk; autofeatures
    // then rejects the `/` in it).
    let feat_args: &[String] = if args.len() > 1 { &args[1..] } else { args };
    autofeatures(table, _nam, Some(modnam), feat_args, fchar, flags) // c:2805
}

/// Port of `unload_named_module(char *modname, char *nam, int silent)`
/// from `Src/module.c:2923-2965`.
///
/// C body:
/// ```c
/// int
/// unload_named_module(char *modname, char *nam, int silent)
/// {
///     const char *mname;
///     Module m;
///     int ret = 0;
///     m = find_module(modname, FINDMOD_ALIASP, &mname);
///     if (m) {
///         int i, del = 0;
///         Module dm;
///         for (i = 0; i < modulestab->hsize; i++) {
///             for (dm = (Module)modulestab->nodes[i]; dm;
///                  dm = (Module)dm->node.next) {
///                 LinkNode dn;
///                 if (!dm->deps || !dm->u.handle)
///                     continue;
///                 for (dn = firstnode(dm->deps); dn; incnode(dn)) {
///                     if (!strcmp((char *) getdata(dn), mname)) {
///                         if (dm->node.flags & MOD_UNLOAD)
///                             del = 1;
///                         else {
///                             zwarnnam(nam, "module %s is in use ...");
///                             return 1;
///                         }
///                     }
///                 }
///             }
///         }
///         if (del) m->wrapper++;
///         if (unload_module(m)) ret = 1;
///         if (del) m->wrapper--;
///     } else if (!silent) {
///         zwarnnam(nam, "no such module %s", modname);
///         ret = 1;
///     }
///     return ret;
/// }
/// ```
///
/// Now that unload_module is faithfully ported (2b986c9e8b), this
/// wraps it with the C dep-walk that gates the unload on whether
/// any other loaded module depends on the target. If a dependent
/// is already tagged MOD_UNLOAD, set del=1 to bracket the unload
/// with `m->wrapper++` / `m->wrapper--` so unload_module's wrapper
/// branch (c:2839-2842) defers correctly.
/// WARNING: param names don't match C — Rust=(table, name, nam, silent) vs C=(modname, nam, silent)
pub fn unload_named_module(table: &mut modulestab, name: &str, nam: &str, silent: i32) -> i32 {
    // c:2924
    // c:2930 — `m = find_module(modname, FINDMOD_ALIASP, &mname);`
    // Returns the canonical (alias-resolved) name; we drive
    // unload_module against that, matching C's `m` pointer.
    let mname = match find_module(table, name, FINDMOD_ALIASP) {
        Some(n) => n,
        None => {
            // c:2959-2961 — !m branch: silent gate.
            if silent == 0 {
                crate::ported::utils::zwarnnam(nam, &format!("no such module {}", name));
                return 1;
            }
            return 0;
        }
    };

    // c:2932 — `int del = 0;`
    let mut del = 0;

    // c:2935-2952 — scan every module's deps for `mname`.
    // Snapshot first so we don't double-borrow the modules HashMap
    // when we mutate via unload_module below.
    let candidates: Vec<(String, i32, bool, Vec<String>)> = table
        .modules
        .iter()
        .filter_map(|(other_name, other)| {
            // c:2939 — `if (!dm->deps || !dm->u.handle) continue;`
            // Static-link analog of `u.handle`: MOD_LINKED && !MOD_UNLOAD.
            // Actually C's gate is `u.handle` (loaded) — the MOD_UNLOAD
            // check happens INSIDE the inner loop. So here we only skip
            // modules with no deps or no handle (= not loaded).
            let deps = other.deps.as_ref()?;
            if (other.node.flags & MOD_LINKED) == 0 {
                return None;
            }
            // Note: C checks `u.handle` (loaded handle) not
            // `!MOD_UNLOAD` here — MOD_UNLOAD-flagged modules still
            // get scanned because the inner branch needs to see them
            // to set `del = 1`.
            Some((
                other_name.clone(),
                other.node.flags,
                (other.node.flags & MOD_UNLOAD) != 0,
                deps.iter().cloned().collect(),
            ))
        })
        .collect();

    for (_other_name, _other_flags, other_unloading, other_deps) in candidates {
        for dep in &other_deps {
            if dep != &mname {
                continue; // c:2942
            }
            if other_unloading {
                // c:2943-2944 — dependent already marked MOD_UNLOAD →
                // we'll be cascade-unloading it after the target.
                del = 1;
            } else {
                // c:2945-2948 — live dependent: refuse the unload.
                crate::ported::utils::zwarnnam(
                    nam,
                    &format!(
                        "module {} is in use by another module and cannot be unloaded",
                        mname
                    ),
                );
                return 1;
            }
        }
    }

    // c:2953-2954 — `if (del) m->wrapper++;`. The wrapper++/--
    // bracket gates unload_module's wrapper branch (c:2839-2842):
    // with wrapper > 0, unload_module sets MOD_UNLOAD and returns
    // rather than running finish. The actual cascade fires via the
    // recursive deferred-dep walk inside unload_module.
    if del != 0 {
        if let Some(m) = table.modules.get_mut(&mname) {
            m.wrapper += 1;
        }
    }

    // c:2955-2956 — `if (unload_module(m)) ret = 1;`. Rust's
    // unload_module returns bool (true=success). Map to C ret:
    // false → 1, true → 0.
    let mut ret = if !table.unload_module(&mname) { 1 } else { 0 };

    // c:2957-2958 — `if (del) m->wrapper--;`
    if del != 0 {
        if let Some(m) = table.modules.get_mut(&mname) {
            m.wrapper -= 1;
        }
    }
    let _ = silent;
    let _ = &mut ret;
    ret // c:2964
}

/// Port of `bin_zmodload_load(char *nam, char **args, Options ops)` from `Src/module.c:2971`.
///
/// C body:
/// ```c
/// bin_zmodload_load(char *nam, char **args, Options ops)
/// {
///     int ret = 0;
///     if(OPT_ISSET(ops,'u')) {
///         for(; *args; args++) {
///             if (unload_named_module(*args, nam, OPT_ISSET(ops,'i')))
///                 ret = 1;
///         }
///         return ret;
///     } else if(!*args) {
///         scanhashtable(modulestab, ..., PRINTMOD_LIST);
///         return 0;
///     } else {
///         for (; *args; args++) {
///             int tmpret = require_module(*args, NULL, OPT_ISSET(ops,'s'));
///             if (tmpret && ret != 1) ret = tmpret;
///         }
///         return ret;
///     }
/// }
/// ```
///
/// `zmodload [-u] [args]`: load, unload, or list modules.
/// WARNING: param names don't match C — Rust=(table, nam, args, ops) vs C=(nam, args, ops)
pub fn bin_zmodload_load(table: &mut modulestab, nam: &str, args: &[String], ops: &options) -> i32 {
    // c:2971
    let mut ret: i32 = 0;
    if OPT_ISSET(ops, b'u') {
        // c:2974
        // c:2976-2979 — unload loop
        for arg in args {
            if unload_named_module(table, arg, nam, OPT_ISSET(ops, b'i') as i32) != 0 {
                ret = 1;
            }
        }
        return ret; // c:2980
    } else if args.is_empty() {
        // c:2981
        // c:2983-2985 — list modules:
        //   `scanhashtable(modulestab, 1, 0, MOD_UNLOAD|MOD_ALIAS,
        //                  modulestab->printnode,
        //                  OPT_ISSET(ops,'L') ? PRINTMOD_LIST : 0);`
        // The 4th arg to scanhashtable is the EXCLUDE mask — entries
        // with `MOD_UNLOAD` or `MOD_ALIAS` set are skipped. The
        // surviving names are routed through `printmodulenode`,
        // which itself gates the visible-line emission on
        // `m->u.handle` (=> `MOD_INIT_B` in zshrs) so registered-
        // but-unloaded modules drop out. Plain `zmodload` (no `-L`)
        // passes `flags=0`; `zmodload -L` passes `PRINTMOD_LIST`.
        // Previous Rust impl printed every key in `table.modules`
        // unconditionally, which leaked the 32 statically-registered
        // builtin entries (#76 in docs/BUGS.md).
        let listflags = if OPT_ISSET(ops, b'L') {
            PRINTMOD_LIST
        } else {
            0
        };
        let mut names: Vec<&String> = table.modules.keys().collect();
        names.sort(); // c:scanhashtable sorted=1 arg
        for name in names {
            let m = &table.modules[name]; // c:154 printnode call
            if (m.node.flags & (MOD_UNLOAD | MOD_ALIAS)) != 0 {
                continue; // c:2983 EXCLUDE mask
            }
            let line = printmodulenode(name, m, listflags);
            if !line.is_empty() {
                println!("{}", line);
            }
        }
        return 0; // c:2986
    } else {
        // c:2989-2992 — load loop
        for arg in args {
            let tmpret = require_module(table, arg, None, OPT_ISSET(ops, b's') as i32); // c:2990
            if tmpret != 0 && ret != 1 {
                // c:2991
                ret = tmpret;
            }
            // PFA-SMR: one event per `zmodload MODULE` load form
            // (only the bare-load path; listing/-u/-d/-A handled
            // upstream and never reaches here). Pass empty flags
            // since the per-feature -F path lives in
            // bin_zmodload_features. Record even on failure so
            // replay sees the user's intent.
            #[cfg(feature = "recorder")]
            if crate::recorder::is_enabled() {
                let ctx = crate::recorder::recorder_ctx_global();
                crate::recorder::emit_zmodload(arg, "", ctx);
            }
        }
        ret
    }
}

/// Port of `bin_zmodload_features(const char *nam, char **args, Options ops)` from `Src/module.c:3003`.
///
/// `zmodload -F [-L|-l|-P|-a|-m|-i] module [+/-feature ...]` —
/// per-feature enable/disable for an already-loaded module.
///
/// C body (~135 lines) handles:
/// - no module: list all modules with their features (`-L` long form,
///   `-l` show all enables, `-a` show autoloads).
/// - `-P` requires a module name; lists patterns.
/// - `-m` interprets each feature as a glob pattern.
/// - default: `+feature` enables, `-feature` disables, then calls
///   `do_module_features` to apply.
/// WARNING: param names don't match C — Rust=(table, nam, args, ops) vs C=(nam, args, ops)
pub fn bin_zmodload_features(
    table: &mut modulestab,
    nam: &str,
    args: &[String],
    ops: &options,
) -> i32 {
    // c:3003
    let modname = args.first(); // c:3003
    let rest_args = if args.is_empty() {
        &args[..]
    } else {
        &args[1..]
    };

    // c:3010-3030 — no-module-name listing branch.
    // C body:
    //   if (modname)
    //       args++;
    //   else if (OPT_ISSET(ops,'L')) {
    //       int printflags = PRINTMOD_LIST|PRINTMOD_FEATURES;
    //       if (OPT_ISSET(ops,'P')) {
    //           zwarnnam(nam, "-P is only allowed with a module name");
    //           return 1;
    //       }
    //       if (OPT_ISSET(ops,'l')) printflags |= PRINTMOD_LISTALL;
    //       if (OPT_ISSET(ops,'a')) printflags |= PRINTMOD_AUTO;
    //       scanhashtable(modulestab, 1, 0, MOD_ALIAS,
    //                     modulestab->printnode, printflags);
    //       return 0;
    //   }
    //   if (!modname) {
    //       zwarnnam(nam, "-F requires a module name");
    //       return 1;
    //   }
    if modname.is_none() {
        if OPT_ISSET(ops, b'L') {
            // c:3012
            // c:3014-3016 — `-P` check must fire BEFORE the listing
            // dispatch. Without modname, -P is illegal.
            if OPT_ISSET(ops, b'P') {
                zwarnnam(nam, "-P is only allowed with a module name"); // c:3015
                return 1; // c:3016
            }
            // c:3013 / c:3018-3021 — assemble PRINTMOD_LIST|PRINTMOD_FEATURES
            //                       [|PRINTMOD_LISTALL][|PRINTMOD_AUTO]
            let mut printflags = PRINTMOD_LIST | PRINTMOD_FEATURES;
            if OPT_ISSET(ops, b'l') {
                printflags |= PRINTMOD_LISTALL; // c:3019
            }
            if OPT_ISSET(ops, b'a') {
                printflags |= PRINTMOD_AUTO; // c:3021
            }
            // c:3022-3023 — `scanhashtable(modulestab, 1, 0, MOD_ALIAS,
            //                              printnode, printflags);`
            // sorted=1, INCLUDE=0 (all), EXCLUDE=MOD_ALIAS (skip aliases).
            let mut names: Vec<String> = table
                .modules
                .iter()
                .filter(|(_, m)| (m.node.flags & MOD_ALIAS) == 0) // c:3022 EXCLUDE
                .map(|(n, _)| n.clone())
                .collect();
            names.sort(); // c:3022 sorted=1
            for name in &names {
                if printflags & PRINTMOD_AUTO != 0 {
                    // c:229-231 / c:238-251 — autoload form; printmodulenode
                    // covers this branch fully.
                    let m = &table.modules[name];
                    let line = printmodulenode(name, m, printflags);
                    if !line.is_empty() {
                        println!("{}", line);
                    }
                    continue;
                }
                // c:218 / c:232-235 — loaded-module + features gate:
                // `if (features_module(m, &features) ||
                //      enables_module(m, &enables) || !*features) return;`
                // printmodulenode has no &table handle, so the FEATURES
                // dispatch happens here (see its c:252-263 comment).
                let loaded = {
                    let m = &table.modules[name];
                    (m.node.flags & MOD_INIT_B) != 0 && (m.node.flags & MOD_UNLOAD) == 0
                };
                if !loaded {
                    continue;
                }
                let mut features: Vec<String> = Vec::new();
                if features_module(table, name, &mut features) != 0 || features.is_empty() {
                    continue; // c:233-235
                }
                let mut enables_opt: Option<Vec<i32>> = None;
                if enables_module(table, name, &mut enables_opt) != 0 {
                    continue; // c:233-235
                }
                let enables = enables_opt.unwrap_or_else(|| vec![0; features.len()]);
                // c:237-245 — `printf("zmodload "); fputs("-F ", stdout);`
                let mut line = String::from("zmodload -F ");
                if name.starts_with('-') {
                    line.push_str("-- "); // c:244-245
                }
                line.push_str(&crate::ported::utils::quotedzputs(name)); // c:246
                // c:252-262 — per-feature tail: LISTALL emits ` +f`/` -f`,
                // plain -L skips disabled and emits ` f`.
                for (f, on) in features.iter().zip(enables.iter()) {
                    if printflags & PRINTMOD_LISTALL != 0 {
                        line.push_str(if *on != 0 { " +" } else { " -" }); // c:256
                    } else if *on == 0 {
                        continue; // c:258
                    } else {
                        line.push(' '); // c:260
                    }
                    line.push_str(&crate::ported::utils::quotedzputs(f)); // c:261
                }
                println!("{}", line);
            }
            return 0; // c:3024
        }
        zwarnnam(nam, "-F requires a module name"); // c:3028
        return 1; // c:3029
    }

    let modname = modname.unwrap();

    // c:3032-3047 — `-m` glob-pattern branch.
    // C body:
    //   if (OPT_ISSET(ops,'m')) {
    //       patprogs = zhalloc(arrlen(args)*sizeof(Patprog));
    //       for (argp = args; *argp; argp++, patprogp++) {
    //           if (*arg == '+' || *arg == '-') arg++;
    //           tokenize(arg);
    //           *patprogp = patcompile(arg, 0, 0);
    //       }
    //   } else patprogs = NULL;
    // Static-link path: pattern compilation deferred. The -m flag is
    // observed at the -a / require_module dispatch below, but the
    // patprogs array stays NULL — patcompile callers (autofeatures,
    // do_module_features) fall back to exact-name matching.

    // c:3049-3226 — `-l/-L/-e` arm: list features one per line with
    // +/- (-l), as a `zmodload -F` statement (-L), or test existence
    // (-e). `-m` patprogs stay deferred (exact-name matching), same
    // as the c:3032-3047 note above.
    if OPT_ISSET(ops, b'l') || OPT_ISSET(ops, b'L') || OPT_ISSET(ops, b'e') {
        let param: Option<String> = OPT_ARG_SAFE(ops, b'P').map(|s| s.to_string()); // c:3060
        // c:3062 — `m = find_module(modname, FINDMOD_ALIASP, NULL);`
        let resolved = find_module(table, modname, FINDMOD_ALIASP);

        // c:3063-3107 — `-a` sub-arm: autoload listing/testing.
        if OPT_ISSET(ops, b'a') {
            // c:3067-3068 — `if (!m || !m->autoloads) return 1;`
            let autoloads: Vec<String> = match resolved
                .as_ref()
                .and_then(|r| table.modules.get(r))
                .and_then(|m| m.autoloads.as_ref())
            {
                Some(al) => al.iter().cloned().collect(),
                None => return 1,
            };
            if OPT_ISSET(ops, b'e') {
                // c:3070-3085 — each arg must (mis)match per its +/- sense.
                for fstr in rest_args {
                    let (sense, name) = match fstr.strip_prefix('+') {
                        Some(rest) => (true, rest), // c:3074
                        None => match fstr.strip_prefix('-') {
                            Some(rest) => (false, rest), // c:3076-3078
                            None => (true, fstr.as_str()),
                        },
                    };
                    // c:3080-3082 — `(linknodebystring(...) != NULL) != sense`
                    if autoloads.iter().any(|a| a == name) != sense {
                        return 1; // c:3082
                    }
                }
                return 0; // c:3084
            }
            if let Some(p) = param {
                // c:3086-3088 / c:3098-3100 — collect into the array,
                // then `setaparam(param, arrset)`.
                // c:3103-3106
                if crate::ported::params::setaparam(&p, autoloads).is_none() {
                    return 1; // c:3105
                }
                return 0; // c:3106
            }
            if OPT_ISSET(ops, b'L') {
                // c:3089-3091 — `printf("zmodload -aF %s%c", ...)`
                let rname = resolved.as_deref().unwrap_or(modname);
                print!(
                    "zmodload -aF {}{}",
                    crate::ported::utils::quotedzputs(rname),
                    if autoloads.is_empty() { '\n' } else { ' ' }
                );
                // c:3092-3098 — space-separated, final '\n'.
                for (i, al) in autoloads.iter().enumerate() {
                    print!(
                        "{}{}",
                        al,
                        if i + 1 < autoloads.len() { ' ' } else { '\n' }
                    );
                }
            } else {
                // c:3093-3097 — one per line.
                for al in &autoloads {
                    println!("{}", al);
                }
            }
            return 0; // c:3107
        }

        // c:3108-3112 — `if (!m || !m->u.handle || (m->node.flags &
        // MOD_UNLOAD))`. zshrs maps "u.handle installed" to MOD_INIT_B
        // (see the printmodulenode comment at c:218-241 above).
        let loaded = resolved
            .as_ref()
            .and_then(|r| table.modules.get(r))
            .map(|m| (m.node.flags & MOD_INIT_B) != 0 && (m.node.flags & MOD_UNLOAD) == 0)
            .unwrap_or(false);
        if !loaded {
            if !OPT_ISSET(ops, b'e') {
                zwarnnam(nam, &format!("module `{}' is not yet loaded", modname)); // c:3110
            }
            return 1; // c:3111
        }
        let rname = resolved.unwrap();

        // c:3113-3118 — `features_module(m, &features)`.
        let mut features: Vec<String> = Vec::new();
        if features_module(table, &rname, &mut features) != 0 {
            if !OPT_ISSET(ops, b'e') {
                zwarnnam(
                    nam,
                    &format!("module `{}' does not support features", rname), // c:3115
                );
            }
            return 1; // c:3117
        }
        // c:3119-3124 — `enables_module(m, &enables)`.
        let mut enables_opt: Option<Vec<i32>> = None;
        if enables_module(table, &rname, &mut enables_opt) != 0 {
            /* this shouldn't ever happen, so don't silence this error */ // c:3120
            zwarnnam(
                nam,
                &format!("error getting enabled features for module `{}'", rname), // c:3121
            );
            return 1; // c:3123
        }
        let enables: Vec<i32> = enables_opt.unwrap_or_else(|| vec![0; features.len()]);

        // c:3125-3155 — validate every feature argument.
        for raw in rest_args {
            // c:3127-3135 — strip +/- into `on`.
            let (on, arg): (i32, &str) = match raw.strip_prefix('-') {
                Some(rest) => (0, rest),
                None => match raw.strip_prefix('+') {
                    Some(rest) => (1, rest),
                    None => (-1, raw.as_str()),
                },
            };
            let mut found = 0;
            for (fp, ep) in features.iter().zip(enables.iter()) {
                // c:3137-3138 — patprogs deferred: exact `strcmp`.
                if arg == fp {
                    // c:3140-3142 — for -e, check given state, if any.
                    if OPT_ISSET(ops, b'e') && on != -1 && on != (ep & 1) {
                        return 1; // c:3142
                    }
                    found += 1;
                    break; // c:3144-3145
                }
            }
            if found == 0 {
                // c:3148-3154
                if !OPT_ISSET(ops, b'e') {
                    zwarnnam(
                        nam,
                        &format!("module `{}' has no such feature: `{}'", modname, raw),
                    );
                }
                return 1; // c:3153
            }
        }
        if OPT_ISSET(ops, b'e') {
            /* yep, everything we want exists */ // c:3156
            return 0; // c:3157
        }

        let opt_big_l = OPT_ISSET(ops, b'L');
        let opt_small_l = OPT_ISSET(ops, b'l');
        // c:3186-3194 / c:3164-3172 — arg filter helpers. The C print
        // loop compares the UNSTRIPPED arg (`!strcmp(*fp, *argp)`,
        // c:3193) while the param-count loop compares stripped
        // (`!strcmp(*fp, arg)`, c:3170). Ported as written.
        let matches_stripped = |f: &str| -> bool {
            rest_args.is_empty()
                || rest_args.iter().any(|raw| {
                    let arg = raw
                        .strip_prefix('+')
                        .or_else(|| raw.strip_prefix('-'))
                        .unwrap_or(raw);
                    f == arg
                })
        };
        let matches_unstripped =
            |f: &str| -> bool { rest_args.is_empty() || rest_args.iter().any(|raw| f == raw) };

        let mut arrset: Option<Vec<String>> = None;
        if param.is_some() {
            // c:3158-3183 — size pass folded away (Vec grows); keep the
            // same membership filter.
            arrset = Some(Vec::new());
        } else if opt_big_l {
            // c:3184-3185 — `printf("zmodload -F %s ", m->node.nam);`
            print!("zmodload -F {} ", crate::ported::utils::quotedzputs(&rname));
        }
        // c:3186-3219 — main feature emit loop.
        for (i, (f, ep)) in features.iter().zip(enables.iter()).enumerate() {
            if param.is_some() {
                if !matches_stripped(f) {
                    continue; // c:3170-3173 stripped compare
                }
            } else if !matches_unstripped(f) {
                continue; // c:3193-3196 unstripped compare
            }
            let onoff: &str = if opt_big_l && !opt_small_l {
                // c:3198-3200
                if *ep == 0 {
                    continue; // c:3199
                }
                ""
            } else if *ep != 0 {
                "+" // c:3203
            } else {
                "-" // c:3205
            };
            if let Some(ref mut arr) = arrset {
                arr.push(format!("{}{}", onoff, f)); // c:3208 bicat
            } else {
                // c:3210-3216 — term ' ' while a next feature EXISTS in
                // the full array (fp[1]), even if it won't be printed.
                let term = if opt_big_l && i + 1 < features.len() {
                    ' '
                } else {
                    '\n'
                };
                print!(
                    "{}{}{}",
                    onoff,
                    crate::ported::utils::quotedzputs(f),
                    term
                );
            }
        }
        if let (Some(p), Some(arr)) = (param, arrset) {
            // c:3220-3224 — `setaparam(param, arrset)`.
            if crate::ported::params::setaparam(&p, arr).is_none() {
                return 1; // c:3223
            }
        }
        return 0; // c:3225
    }

    // c:3227-3229 — `-P` is illegal without -l/-L/-e.
    if OPT_ISSET(ops, b'P') && !(OPT_ISSET(ops, b'l') || OPT_ISSET(ops, b'L') || OPT_ISSET(ops, b'e')) {
        zwarnnam(nam, "-P can only be used with -l or -L"); // c:3228
        return 1; // c:3229
    }

    // c:3230-3247 — `-a` arm: route through autofeatures with
    // FEAT_IGNORE (the autoload-feature registration path).
    if OPT_ISSET(ops, b'a') {
        // c:3231-3234 — `-m` incompatible with `-a`.
        if OPT_ISSET(ops, b'm') {
            zwarnnam(nam, "-m cannot be used with -a"); // c:3232
            return 1; // c:3233
        }
        // c:3246 — `return autofeatures(nam, modname, args, 0, FEAT_IGNORE);`
        // FEAT_IGNORE is hard-coded here because marking-for-autoload
        // is separate from enable/disable (per the c:3236-3244 comment).
        return autofeatures(table, nam, Some(modname), rest_args, 0, FEAT_IGNORE);
    }

    // c:3249-3260 — default arm: build Feature_enables array from
    // `+name`/`-name` args, then `require_module(modname, features,
    // OPT_ISSET(ops,'s'))`.
    //
    // C builds a fep[] array with str + (optional) patprog pairs.
    // The Rust port flattens to a `Vec<String>` since patprogs are
    // deferred; require_module accepts Option<&[String]>.
    let feats: Vec<String> = rest_args.to_vec();
    let features_arg = if feats.is_empty() {
        None
    } else {
        Some(feats.as_slice())
    };
    require_module(table, modname, features_arg, OPT_ISSET(ops, b's') as i32) // c:3260
}

/// Port of `ensurefeature(const char *modname, const char *prefix, const char *feature)` from `Src/module.c:3415`.
///
/// C body:
/// ```c
/// ensurefeature(const char *modname, const char *prefix, const char *feature)
/// {
///     char *f;
///     struct feature_enables features[2];
///     if (!feature)
///         return require_module(modname, NULL, 0);
///     f = dyncat(prefix, feature);
///     features[0].str = f;
///     features[0].pat = NULL;
///     features[1].str = NULL;
///     features[1].pat = NULL;
///     return require_module(modname, features, 0);
/// }
/// ```
/// WARNING: param names don't match C — Rust=(table, modname, prefix, feature) vs C=(modname, prefix, feature)
pub fn ensurefeature(
    table: &mut modulestab,
    modname: &str,
    prefix: &str,
    feature: Option<&str>,
) -> i32 {
    // c:3415
    match feature {
        // c:3420-3421 — `if (!feature) return require_module(modname, NULL, 0);`
        None => require_module(table, modname, None, 0),
        Some(f) => {
            // c:3422-3428 — build single-element features[2] array.
            let combined = crate::ported::string::dyncat(prefix, f); // c:3422
            let arr = vec![combined];
            require_module(table, modname, Some(&arr), 0) // c:3428
        }
    }
}

/// Port of `static HashNode resolvebuiltin(const char *cmdarg, HashNode hn)`
/// from `Src/exec.c:2700-2724` — the autoloaded-builtin stub firing.
///
/// C body:
/// ```c
/// if (!((Builtin) hn)->handlerfunc) {
///     char *modname = dupstring(((Builtin) hn)->optstr);
///     (void)ensurefeature(modname, "b:",
///                         (hn->flags & BINF_AUTOALL) ? NULL : hn->nam);
///     hn = builtintab->getnode(builtintab, cmdarg);
///     if (!hn) {
///         lastval = 1;
///         zerr("autoloading module %s failed to define builtin: %s",
///              modname, cmdarg);
///         return NULL;
///     }
/// }
/// return hn;
/// ```
///
/// zshrs split: the C autoload stub (builtintab node with NULL
/// handlerfunc, installed by `add_autobin` c:426) lives in the
/// `autoload_builtins` ledger (name → module). This fn is the
/// dispatch-time consult:
///   - `None` — name has no autoload stub; caller continues its
///     normal lookup chain.
///   - `Some(0)` — module loaded; caller re-dispatches the (now
///     registered) builtin.
///   - `Some(1)` — load failed or the loaded module didn't define
///     the feature; diagnostics already printed (load_module's
///     `failed to load module` zwarn, or the c:2718 zerr here).
///     Caller returns status 1.
///
/// Ledger upkeep: the entry is removed in every fired path —
///   - success: C's addbuiltin (module.c:411-415) replaces the stub
///     with the real node, so the stub is gone;
///   - load failure: C's execbuiltin head (Src/builtin.c:264-267)
///     hits the still-NULL handlerfunc and `deletebuiltin`s the
///     stub — a second call reports `command not found` / 127
///     (probed: zsh 5.9 `zmodload -ab zsh/bogus mybltn; mybltn;
///     mybltn` → rc1=1, rc2=127).
///
/// AUTOALL note: the ledger doesn't carry BINF_AUTOALL, so the
/// ensurefeature arg is always `Some(name)` (the non-AUTOALL form).
/// The AUTOALL path is unreachable for builtins via `zmodload -a MOD`
/// (both shells error "`/' is illegal in a builtin"); revisit if the
/// ledger grows flags.
pub fn resolvebuiltin(name: &str) -> Option<i32> {
    // c:2700
    let mut tab = MODULESTAB.lock().ok()?;
    // c:2705 — `if (!((Builtin) hn)->handlerfunc)`: ledger hit IS the
    // "no handlerfunc" stub in zshrs.
    let module = tab.autoload_builtins.get(name)?.clone();
    // c:2706 — `modname = dupstring(hn->optstr)` (done: `module`).
    // Stub fires exactly once per registration (see ledger upkeep
    // in the doc above).
    tab.autoload_builtins.remove(name);
    // c:2711-2713 — ensurefeature(modname, "b:", hn->nam).
    let _ = ensurefeature(&mut tab, &module, "b:", Some(name));
    // c:2714 — `hn = builtintab->getnode(builtintab, cmdarg);`
    // zshrs analog: the module booted (is_loaded) AND the name is in
    // the static builtintab (createbuiltintable pre-registers every
    // module bintab entry).
    let defined = tab.is_loaded(&module)
        && crate::ported::builtin::createbuiltintable().contains_key(name);
    if defined {
        return Some(0); // c:2723 `return hn;`
    }
    if tab.is_loaded(&module) {
        // c:2716-2720 — module loaded but feature missing.
        crate::ported::builtin::LASTVAL
            .store(1, std::sync::atomic::Ordering::Relaxed); // c:2717 lastval = 1
        crate::ported::utils::zerr(&format!(
            "autoloading module {} failed to define builtin: {}",
            module, name
        )); // c:2718
    }
    // Load failure: load_module already printed `failed to load
    // module \`...'`; C's execbuiltin head returns 1 silently
    // (Src/builtin.c:264-267).
    Some(1)
}

/// Port of `addmathfunc(MathFunc f)` from `Src/module.c:1313`.
///
/// C body: walks the global `mathfuncs` linked list, refuses to
/// re-register MFF_ADDED entries, replaces autoloadable shims, then
/// links into head. Rust port operates on `autoload_mathfuncs` map
/// since zshrs's static-link path doesn't have per-entry MFF flags.

/// Port of `autofeatures(const char *cmdnam, const char *module, char **features, int prefchar, int defflags)` from `Src/module.c:3437`.
///
/// C body is ~140 lines. Top-level structure:
/// ```c
/// autofeatures(const char *cmdnam, const char *module, char **features,
///              int prefchar, int defflags)
/// {
///     // Resolve module, fetch its features+enables tables.
///     // For each feature in `features`:
///     //   parse `+`/`-` prefix → add/remove
///     //   parse type prefix (b/c/C/p/f) → fchar
///     //   dispatch to add_aliasbuiltin / add_autocondition /
///     //     add_autoparam / add_automathfunc / del_* matching
/// }
/// ```
///
/// Static-link path: registers each `module:feature` pair into the
/// matching `table.autoload_*` map. Honors `+`/`-` prefix for
/// add/remove, and the type prefix or `prefchar` arg for routing.
/// WARNING: param names don't match C — Rust=(table, _cmdnam, module, features, prefchar, defflags) vs C=(cmdnam, module, features, prefchar, defflags)
pub fn autofeatures(
    table: &mut modulestab,
    cmdnam: &str,
    module: Option<&str>,
    features: &[String],
    prefchar: u8,
    defflags: i32,
) -> i32 {
    // c:3437
    let mut ret: i32 = 0;

    // c:3445-3453 — resolve `defm` up front (FINDMOD_ALIASP|
    // FINDMOD_CREATE); if its union slot is populated (loaded —
    // MOD_INIT_B in zshrs, see the printmodulenode c:218-241 note)
    // fetch the feature + enable tables for the c:3558-3577 checks.
    let mut modfeatures: Option<Vec<String>> = None;
    let mut modenables: Vec<i32> = Vec::new();
    let defm_name: Option<String> = match module {
        Some(modn) => {
            let resolved = find_module(table, modn, FINDMOD_ALIASP | FINDMOD_CREATE);
            if let Some(ref r) = resolved {
                let booted = table
                    .modules
                    .get(r)
                    .map(|m| (m.node.flags & MOD_INIT_B) != 0)
                    .unwrap_or(false);
                if booted {
                    // c:3449-3451
                    let mut f: Vec<String> = Vec::new();
                    if features_module(table, r, &mut f) == 0 {
                        let mut e: Option<Vec<i32>> = None;
                        let _ = enables_module(table, r, &mut e);
                        modenables = e.unwrap_or_else(|| vec![0; f.len()]);
                        modfeatures = Some(f);
                    }
                }
            }
            resolved
        }
        None => None, // c:3454-3455 `defm = NULL`
    };

    for feature in features {
        let s = feature.as_str();
        let mut add: bool = true; // c:3466 / c:3477 default `add = 1`
        let mut flags = defflags; // c:3458 `flags = defflags`

        // `feature_full` is the string C keeps in `m->autoloads`
        // (c:3584/3597 `ztrdup(feature)`): the arg after the +/-
        // strip, type prefix included — prefchar mode synthesizes it
        // via `sprintf(feature, "%c:%s", fchar, fnam)` (c:3468-3469).
        let prefixed: String;
        let (fchar, fnam, feature_full): (u8, &str, &str) = if prefchar != 0 {
            // c:3461-3470 — `prefchar` mode: feature is bare name with
            // no `+`/`-` / `b:` prefix; fchar comes from the arg.
            prefixed = format!("{}:{}", prefchar as char, s); // c:3468-3469
            (prefchar, s, prefixed.as_str()) // c:3467-3468
        } else {
            // c:3471-3490 — parse `+`/`-` then the `b:`/`c:`/`C:`/`p:`/`f:`
            // type prefix.
            let mut t = s;
            if let Some(rest) = t.strip_prefix('-') {
                // c:3473
                add = false;
                t = rest;
            } else if let Some(rest) = t.strip_prefix('+') {
                // c:3478
                t = rest;
            }
            // c:3482-3487 — bad format check: `!*feature || feature[1] != ':'`
            let bytes = t.as_bytes();
            if bytes.is_empty() || bytes.len() < 2 || bytes[1] != b':' {
                // c:3483-3486 — zwarnnam + ret=1 + continue.
                crate::ported::utils::zwarnnam(
                    cmdnam,
                    &format!("bad format for autoloadable feature: `{}'", t),
                );
                ret = 1; // c:3485
                continue; // c:3486
            }
            // c:3488-3489 — `fnam = feature + 2; fchar = feature[0];`
            (bytes[0], &t[2..], t)
        };

        // c:3491-3492 — `if (flags & FEAT_REMOVE) add = 0;`
        if (flags & FEAT_REMOVE) != 0 {
            add = false;
        }

        let typnam: &str; // c:3457
        let _ = typnam; // referenced below for fnam validation
        let typnam = match fchar {
            // c:3494-3522 — switch (fchar): map each type-letter to
            // the typnam used in the diagnostic + add/del fn dispatch.
            b'b' => "builtin",        // c:3495-3498
            b'c' | b'C' => {
                if fchar == b'C' {
                    flags |= FEAT_INFIX; // c:3501
                }
                "condition" // c:3505
            }
            b'f' => "math function", // c:3508-3511
            b'p' => "parameter",     // c:3513-3516
            _ => {
                // c:3518-3522 — `zwarnnam(cmdnam, "bad autoloadable
                // feature type: `%c'", fchar); ret = 1; continue;`
                crate::ported::utils::zwarnnam(
                    cmdnam,
                    &format!("bad autoloadable feature type: `{}'", fchar as char),
                );
                ret = 1; // c:3521
                continue; // c:3522
            }
        };

        // c:3525-3529 — reject `/` in the feature name.
        if fnam.contains('/') {
            crate::ported::utils::zwarnnam(
                cmdnam,
                &format!("{}: `/' is illegal in a {}", fnam, typnam),
            );
            ret = 1;
            continue;
        }

        // c:3531-3553 — resolve module: if `module` arg is None,
        // walk every module's `m->autoloads` list looking for the
        // feature; if found, that's the owning module. C's
        // `m->autoloads` per-module list isn't modelled in the Rust
        // port — the autoload_* HashMaps store `feature → module`
        // directly, so we can derive the owning module from those.
        // When `module` arg IS set, use it (c:3553 `m = defm;`).
        let modname_owned: String = match module {
            Some(m) => m.to_string(), // c:3553
            None => {
                // c:3537-3551 — search for the owning module across all
                // autoload maps; fall back to error if not found.
                let map = match fchar {
                    b'b' => &table.autoload_builtins,
                    b'c' | b'C' => &table.autoload_conditions,
                    b'p' => &table.autoload_params,
                    b'f' => &table.autoload_mathfuncs,
                    _ => unreachable!(),
                };
                match map.get(fnam).cloned() {
                    Some(m) => m,
                    None => {
                        if (flags & FEAT_IGNORE) == 0 {
                            // c:3546-3549
                            ret = 1;
                            crate::ported::utils::zwarnnam(
                                cmdnam,
                                &format!("{}: no such {}", fnam, typnam),
                            );
                        }
                        continue; // c:3550
                    }
                }
            }
        };
        let modname = modname_owned.as_str();

        // c:3554 `subret = 0;` — the m->autoloads maintenance below can
        // set it to ±2 on the remove-missing path (c:3614).
        let mut autoload_subret: i32 = 0;
        // Owning module node: the alias-resolved defm when a module arg
        // was given (C `m = defm`, c:3553), else the searched-up name.
        let owner: &str = match (module.is_some(), defm_name.as_deref()) {
            (true, Some(r)) => r,
            _ => modname,
        };
        if add {
            // c:3558-3577 — if the module is already loaded, the feature
            // must exist in its table; if it's already enabled there is
            // nothing to mark.
            if module.is_some() {
                if let Some(ref mf) = modfeatures {
                    match mf.iter().position(|f| f == feature_full) {
                        None => {
                            // c:3566-3570
                            crate::ported::utils::zwarnnam(
                                cmdnam,
                                &format!(
                                    "module `{}' has no such feature: `{}'",
                                    owner, feature_full
                                ),
                            );
                            ret = 1;
                            continue;
                        }
                        Some(idx) => {
                            if modenables.get(idx).copied().unwrap_or(0) != 0 {
                                continue; // c:3572-3577 already provided
                            }
                        }
                    }
                }
            }
            // c:3583-3603 — insert into m->autoloads in lexical order
            // (dup is "never an error", c:3590-3593).
            if let Some(m) = table.modules.get_mut(owner) {
                let list = m
                    .autoloads
                    .get_or_insert_with(crate::ported::linklist::znewlinklist);
                let mut insert_at: Option<usize> = Some(list.len()); // c:3602 append default
                for (i, existing) in list.iter().enumerate() {
                    match feature_full.cmp(existing.as_str()) {
                        std::cmp::Ordering::Equal => {
                            insert_at = None; // c:3591-3593 already there
                            break;
                        }
                        std::cmp::Ordering::Less => {
                            insert_at = Some(i); // c:3595-3598
                            break;
                        }
                        std::cmp::Ordering::Greater => {}
                    }
                }
                if let Some(i) = insert_at {
                    list.insert_at(i, feature_full.to_string());
                }
            }
        } else {
            // c:3605-3615 — `else if (m->autoloads) { remnode or
            // subret = FEAT_IGNORE ? -2 : 2; }`
            let removed = table
                .modules
                .get_mut(owner)
                .and_then(|m| m.autoloads.as_mut())
                .map(|list| {
                    match list
                        .iter()
                        .position(|existing| existing.as_str() == feature_full)
                    {
                        Some(i) => {
                            list.delete_node(i);
                            true
                        }
                        None => false,
                    }
                })
                .unwrap_or(false);
            if !removed {
                autoload_subret = if (flags & FEAT_IGNORE) != 0 { -2 } else { 2 }; // c:3614
            }
        }

        // c:3556-3616 — m->autoloads insert/remove in lexical order;
        // the autoload_* maps mirror the list for the feature→module
        // reverse lookups the static-link dispatch needs.
        if add {
            match fchar {
                b'b' => {
                    table
                        .autoload_builtins
                        .insert(fnam.to_string(), modname.to_string());
                }
                b'c' | b'C' => {
                    table
                        .autoload_conditions
                        .insert(fnam.to_string(), modname.to_string());
                }
                b'p' => {
                    table
                        .autoload_params
                        .insert(fnam.to_string(), modname.to_string());
                }
                b'f' => {
                    table
                        .autoload_mathfuncs
                        .insert(fnam.to_string(), modname.to_string());
                }
                _ => unreachable!(),
            }
        } else {
            // c:3605-3615 — handled above by the m->autoloads remove
            // (autoload_subret carries the c:3614 ±2). Keep the
            // feature→module reverse maps in sync silently — the
            // not-present diagnostic flows through subret below,
            // exactly like C's c:3631 arm.
            match fchar {
                b'b' => {
                    table.autoload_builtins.remove(fnam);
                }
                b'c' | b'C' => {
                    table.autoload_conditions.remove(fnam);
                }
                b'p' => {
                    table.autoload_params.remove(fnam);
                }
                b'f' => {
                    table.autoload_mathfuncs.remove(fnam);
                }
                _ => unreachable!(),
            }
        }

        // c:3618-3619 — `if (subret == 0) subret = fn(module, fnam, flags);`
        // The fn does NOT run when the m->autoloads remove already
        // produced ±2. Dispatch through the per-type add/del fn so the
        // canonical tables (paramtab for `p:`, condtab for `c:`, etc.)
        // carry the PM_AUTOLOAD / CONDF flag bits expected by
        // downstream code (e.g. paramtypestr's `undefined NAME`).
        let subret = if autoload_subret != 0 {
            autoload_subret // c:3618 subret already set
        } else if add {
            match fchar {
                b'p' => add_autoparam(modname, fnam, flags),
                b'f' => add_automathfunc(table, modname, fnam, flags),
                b'b' => table.add_autobin(fnam, modname, flags),
                b'c' | b'C' => table.add_autocond(fnam, modname, flags),
                _ => unreachable!(),
            }
        } else {
            match fchar {
                b'p' => del_autoparam(modname, fnam, flags),
                b'f' => del_automathfunc(table, modname, fnam, flags),
                b'b' => table.del_autobin(fnam, flags),
                b'c' | b'C' => table.del_autocond(fnam, flags),
                _ => unreachable!(),
            }
        };

        // c:3621-3642 — per-error-code diagnostic.
        if subret != 0 && subret != -2 {
            ret = 1; // c:3624
            match subret {
                1 => {
                    // c:3627
                    crate::ported::utils::zwarnnam(
                        cmdnam,
                        &format!("failed to add {} `{}'", typnam, fnam),
                    );
                }
                2 => {
                    // c:3631
                    crate::ported::utils::zwarnnam(
                        cmdnam,
                        &format!("{}: no such {}", fnam, typnam),
                    );
                }
                3 => {
                    // c:3635
                    crate::ported::utils::zwarnnam(
                        cmdnam,
                        &format!("{}: {} is already defined", fnam, typnam),
                    );
                }
                _ => { /* c:3638 no further message */ }
            }
        }
    }
    ret
}

/// Port of `MathFunc mathfuncs;` from `Src/module.c:1258` — the
/// global head of the linked list of math functions. Both
/// autoloadable math ported (added by modules) and user math ported
/// (added by `functions -M`) live here.
///
/// C is a singly linked list with `mathfunc.next` chaining. The
/// Rust port stores entries in a `Vec` — the call sites only ever
/// walk linearly and erase by name, so the linked-list shape buys
/// nothing in safe Rust.
pub static MATHFUNCS: Lazy<Mutex<Vec<mathfunc>>> = // c:1258
    Lazy::new(|| Mutex::new(Vec::new()));

/// Port of `int setconddefs(char const *nam, Conddef c, int size, int *e)`
/// from `Src/module.c:754`. Bulk add/delete of condition definitions:
/// the parallel `e[]` array selects per-entry add (`e[i] != 0`) vs delete
/// (`e[i] == 0`). Returns 1 if any individual op clashed, 0 if all clean.
pub fn setconddefs(
    nam: &str, // c:754
    c: &mut [conddef],
    e: Option<&[i32]>,
) -> i32 {
    let mut ret = 0; // c:758
    for (i, entry) in c.iter_mut().enumerate() {
        // c:760 while (size--)
        let want_add = e.map(|es| es[i] != 0).unwrap_or(true); // c:761 if (e && *e++)
        if want_add {
            if (entry.flags & CONDF_ADDED) != 0 {
                continue;
            } // c:763 already added
            let dup = conddef {
                next: None,
                name: entry.name.clone(),
                flags: entry.flags,
                handler: entry.handler,
                min: entry.min,
                max: entry.max,
                condid: entry.condid,
                module: entry.module.clone(),
            };
            if addconddef(dup) != 0 {
                // c:768 addconddef
                zwarnnam(
                    nam, // c:769 zwarnnam
                    &format!("name clash when adding condition `{}'", entry.name),
                );
                ret = 1;
            } else {
                entry.flags |= CONDF_ADDED; // c:773
            }
        } else {
            if (entry.flags & CONDF_ADDED) == 0 {
                continue;
            } // c:776
            if deleteconddef(entry) != 0 {
                // c:780 deleteconddef
                zwarnnam(
                    nam, // c:781
                    &format!("condition `{}' already deleted", entry.name),
                );
                ret = 1;
            } else {
                entry.flags &= !CONDF_ADDED; // c:785
            }
        }
    }
    ret // c:790
}

/// Port of `int setmathfuncs(char const *nam, MathFunc f, int size, int *e)`
/// from `Src/module.c:1374`. Bulk add/delete of math-function definitions
/// via the parallel `e[]` selector array (same shape as setconddefs).
pub fn setmathfuncs(
    nam: &str, // c:1374
    f: &mut [mathfunc],
    e: Option<&[i32]>,
) -> i32 {
    let mut ret = 0; // c:1378
    for (i, entry) in f.iter_mut().enumerate() {
        // c:1380 while (size--)
        // c:1381 — `if (e && *e++)`: e == NULL means REMOVE all
        // (same contract as setbuiltins, c:497-503 doc: "e is either
        // NULL, in which case all builtins in the table are
        // removed"). The previous `.unwrap_or(true)` inverted the
        // None case into add-all.
        let want_add = e.map(|es| es[i] != 0).unwrap_or(false); // c:1381
        if want_add {
            if (entry.flags & MFF_ADDED) != 0 {
                continue;
            } // c:1383
            let dup = mathfunc {
                next: None,
                name: entry.name.clone(),
                flags: entry.flags,
                nfunc: entry.nfunc,
                sfunc: entry.sfunc,
                module: entry.module.clone(),
                minargs: entry.minargs,
                maxargs: entry.maxargs,
                funcid: entry.funcid,
            };
            if addmathfunc(dup) != 0 {
                // c:1388 addmathfunc
                zwarnnam(
                    nam, // c:1389
                    &format!("name clash when adding math function `{}'", entry.name),
                );
                ret = 1;
            } else {
                entry.flags |= MFF_ADDED; // c:1393
                // c:1388+1393 — C links `f` ITSELF into mathfuncs and
                // the flag-set above mutates that same (aliased) node.
                // The Rust insert is a by-value dup, so mirror the flag
                // onto the global-list entry; otherwise
                // del_automathfunc / getfeatureenables reading
                // MATHFUNCS never observe MFF_ADDED.
                if let Ok(mut gtab) = MATHFUNCS.lock() {
                    if let Some(p) = gtab.iter_mut().find(|p| p.name == entry.name) {
                        p.flags |= MFF_ADDED;
                    }
                }
            }
        } else {
            if (entry.flags & MFF_ADDED) == 0 {
                continue;
            } // c:1396
            if deletemathfunc(entry) != 0 {
                // c:1400 deletemathfunc
                zwarnnam(
                    nam, // c:1401
                    &format!("math function `{}' already deleted", entry.name),
                );
                ret = 1;
            } else {
                // c:1356-1357 — C's deletemathfunc clears MFF_ADDED on
                // the (aliased) static mftab struct; Rust's global-list
                // entry is a dup, so clear it here on the caller's
                // record. Without this, a disable→re-enable cycle hits
                // the c:1383 `continue` and never re-registers.
                entry.flags &= !MFF_ADDED;
            }
        }
    }
    ret // c:1407
}

/// Port of file-static `Conddef condtab;` from `Src/cond.c:21` — the
/// global condition-definition linked-list head consulted by `[[ ... ]]`
/// dispatch. Modules register custom conditions via `addconddef`; the
/// runtime walks `condtab` looking for the matching name+infix flag at
/// each `[[` evaluation. Rust port stores entries in a `Vec` (linear
/// add/remove + walk; same observable behaviour as C linked list).
pub static CONDTAB: Lazy<Mutex<Vec<conddef>>> = // c:cond.c:21
    Lazy::new(|| Mutex::new(Vec::new()));

/// Port of `Conddef getconddef(int inf, const char *name, int autol)`
/// from `Src/module.c:648`.
///
/// C body c:648-689:
/// ```c
/// Conddef
/// getconddef(int inf, const char *name, int autol)
/// {
///     Conddef p;
///     int f = 1;
///     char *lookup, *s;
///     lookup = dupstring(name);
///     if (!lookup) return NULL;
///     for (s = lookup; *s != '\0'; s++) {
///         if (*s == Dash) *s = '-';
///     }
///     do {
///         for (p = condtab; p; p = p->next) {
///             if ((!!inf == !!(p->flags & CONDF_INFIX)) &&
///                 !strcmp(lookup, p->name))
///                 break;
///         }
///         if (autol && p && p->module) {
///             if (f) {
///                 (void)ensurefeature(p->module,
///                                     (p->flags & CONDF_INFIX) ? "C:" : "c:",
///                                     (p->flags & CONDF_AUTOALL) ? NULL : lookup);
///                 f = 0;
///                 p = NULL;
///             } else {
///                 deleteconddef(p);
///                 return NULL;
///             }
///         } else
///             break;
///     } while (!p);
///     return p;
/// }
/// ```
///
/// Returns a clone of the matched `conddef` (or `None` if absent).
/// `inf` selects between infix-style (`[[ A op B ]]`) and prefix-style
/// (`[[ -X arg ]]`) conditions — `CONDF_INFIX` on the entry must match.
/// `autol` triggers `ensurefeature` on autoload-stubs; the autoload
/// loop runs at most once (gated by `f`) — if the second iteration
/// still finds the entry, the stub is treated as a failed load and
/// removed via `deleteconddef`.
pub fn getconddef(inf: i32, name: &str, autol: i32, table: &mut modulestab) -> Option<conddef> {
    // c:648
    // c:655 — `lookup = dupstring(name)` then Dash → '-' substitution.
    let lookup: String = crate::ported::string::dupstring(name)
        .chars()
        .map(|c| if c == crate::ported::zsh_h::Dash { '-' } else { c })
        .collect();
    let mut f = 1; // c:651 `int f = 1;`
    loop {
        // c:663 do { ... } while (!p);
        // c:664-668 — walk condtab matching (!!inf == !!CONDF_INFIX) && name.
        let want_infix = inf != 0;
        let hit: Option<conddef> = {
            let tab = CONDTAB.lock().unwrap();
            tab.iter().find_map(|p| {
                let p_infix = (p.flags & CONDF_INFIX) != 0;
                if p_infix == want_infix && p.name == lookup {
                    // Manual field-by-field clone: conddef doesn't
                    // derive Clone (function-pointer + Option<Conddef>
                    // mix with no PartialEq).
                    Some(conddef {
                        next: None,
                        name: p.name.clone(),
                        flags: p.flags,
                        handler: p.handler,
                        min: p.min,
                        max: p.max,
                        condid: p.condid,
                        module: p.module.clone(),
                    })
                } else {
                    None
                }
            })
        };
        // c:669-685 — autoload trigger + failure-retry loop.
        let has_autoload_module = hit
            .as_ref()
            .map(|p| p.module.is_some())
            .unwrap_or(false);
        if autol != 0 && hit.is_some() && has_autoload_module {
            let p = hit.as_ref().unwrap();
            if f != 0 {
                // c:674-678 — first miss: load the module + retry.
                let module = p.module.as_ref().unwrap().clone();
                let prefix = if (p.flags & CONDF_INFIX) != 0 {
                    "C:"
                } else {
                    "c:"
                };
                let feature_arg = if (p.flags & CONDF_AUTOALL) != 0 {
                    // c:677 — NULL → autoload-all branch.
                    None
                } else {
                    Some(lookup.as_str())
                };
                let _ = ensurefeature(table, &module, prefix, feature_arg);
                f = 0;
                continue; // c:680 `p = NULL;` + outer do-while re-tries.
            } else {
                // c:681-683 — second pass still hit autoload entry →
                // load failed; remove the stub and return None.
                let _ = deleteconddef(p);
                return None;
            }
        }
        return hit; // c:684-685 `else break;` then return p.
    }
}

/// Port of `int deleteconddef(Conddef c)` from `Src/module.c:724`.
/// Removes condition definition `c` from `condtab`. Returns 0 on
/// success, -1 on miss. C also frees the autoloaded entry's name +
/// module; Rust drop subsumes that.
pub fn deleteconddef(c: &conddef) -> i32 {
    // c:724
    let mut tab = CONDTAB.lock().unwrap();
    // c:728 — `for (p = condtab, q = NULL; p && p != c; ...)`. C uses
    // pointer identity; the Rust analog is name+infix-flag equality
    // (the natural key — `[[ -z STR ]]` and `STR == VAL` share neither).
    let infix = c.flags & CONDF_INFIX;
    match tab
        .iter()
        .position(|p| p.name == c.name && (p.flags & CONDF_INFIX) == infix)
    {
        Some(i) => {
            tab.remove(i);
            0
        } // c:733-738 unlink + free
        None => -1, // c:743 not found
    }
}

/// Port of `int addconddef(Conddef c)` from `Src/module.c:703`. Walks
/// CONDTAB for a clash on (name, infix-flag); replaces autoloadable
/// entries via deleteconddef; otherwise prepends. Returns 0 on add,
/// 1 on clash (existing entry already added).
pub fn addconddef(c: conddef) -> i32 {
    // c:703
    let infix = c.flags & CONDF_INFIX;
    let clash_idx = {
        let tab = CONDTAB.lock().unwrap();
        tab.iter()
            .position(|p| p.name == c.name && (p.flags & CONDF_INFIX) == infix) // c:705 getconddef
    };
    if let Some(i) = clash_idx {
        let (autoload, added) = {
            let tab = CONDTAB.lock().unwrap();
            (tab[i].module.is_some(), (tab[i].flags & CONDF_ADDED) != 0)
        };
        if !autoload || added {
            return 1;
        } // c:708 already added
        CONDTAB.lock().unwrap().remove(i); // c:711 deleteconddef
    }
    CONDTAB.lock().unwrap().insert(0, c); // c:713-714 c->next = condtab; condtab = c
    0
}

/// Port of file-static `FuncWrap wrappers;` from `Src/module.c:567`
/// — the global wrapper-function linked-list head. Modules register
/// wrapper callbacks via `addwrapper(FuncWrap)` and the runtime fires
/// them around `runshfunc()`. The Rust port stores entries in a `Vec`
/// (linear add/remove + iterate; same observable behaviour).
pub static WRAPPERS: Lazy<Mutex<Vec<funcwrap>>> = // c:567
    Lazy::new(|| Mutex::new(Vec::new()));

/// Port of `addmathfunc(MathFunc f)` from `Src/module.c:1313`.
/// Returns 0 on add, 1 on clash (existing entry not autoloadable).
/// Replaces autoloadable entries via `removemathfunc`.
pub fn addmathfunc(f: mathfunc) -> i32 {
    // c:1313
    if (f.flags & MFF_ADDED) != 0 {
        return 1;
    } // c:1318
    let mut tab = MATHFUNCS.lock().unwrap();
    let mut found_idx: Option<usize> = None;
    for (i, p) in tab.iter().enumerate() {
        // c:1321
        if p.name == f.name {
            // c:1322
            if p.module.is_some() && (p.flags & MFF_USERFUNC) == 0 {
                // c:1323
                found_idx = Some(i); // c:1327 removemathfunc + replace
                break;
            }
            return 1; // c:1330
        }
    }
    if let Some(i) = found_idx {
        tab.remove(i);
    } // c:1327
    tab.insert(0, f); // c:1334-1335 f->next = mathfuncs; mathfuncs = f
    0
}

/// Port of `removemathfunc(MathFunc previous, MathFunc current)` from
/// `Src/module.c:1267`. Removes the named entry from MATHFUNCS and
/// drops it (Rust drop subsumes C's zsfree/zfree ladder).
/// WARNING: param names don't match C — Rust=(name) vs C=(previous, current)
pub fn removemathfunc(name: &str) {
    // c:1267
    let mut tab = MATHFUNCS.lock().unwrap();
    if let Some(i) = tab.iter().position(|m| m.name == name) {
        // c:1270 walk
        tab.remove(i); // c:1273-1274 unlink + zfree
    }
}

/// Port of `deletemathfunc(MathFunc f)` from `Src/module.c:1342`.
/// Removes f from MATHFUNCS; for unloaded/user-defined entries clears
/// the MFF_ADDED flag instead of dropping the node (C: `f->flags &=
/// ~MFF_ADDED` when f->module is null).
pub fn deletemathfunc(f: &mathfunc) -> i32 {
    // c:1342
    // c:1348-1352 — the node is unlinked from the global list in BOTH
    // arms (`q->next = f->next` / `mathfuncs = f->next`); f->module
    // only decides free-vs-keep of the struct itself. The struct for
    // module=NULL entries lives in the module's static mftab (the C
    // list links the static structs by pointer), so "keep" means the
    // mftab record survives with MFF_ADDED cleared — in Rust the
    // global-list entry is a by-value dup, so the unlink is a plain
    // remove either way and the MFF_ADDED clear on the static record
    // happens at the setmathfuncs caller (which holds `&mut` to the
    // mftab entry). The previous port left module-less entries IN the
    // global list, so a feature-disable never actually deregistered.
    let mut tab = MATHFUNCS.lock().unwrap();
    match tab.iter().position(|m| m.name == f.name) {
        // c:1346
        Some(i) => {
            tab.remove(i); // c:1349-1352 unlink (+ Rust Drop ≙ c:1355-1357 free)
            0 // c:1361
        }
        None => -1, // c:1363
    }
}

/// Port of `addwrapper(Module m, FuncWrap w)` from `Src/module.c:577`.
/// Returns 0 on add, 1 on clash. Walks WRAPPERS for an existing entry
/// with the same handler; appends if absent and sets WRAPF_ADDED on
/// the input record.
pub fn addwrapper(table: &modulestab, modname: &str, w: funcwrap) -> i32 {
    // c:577
    // c:587-588 — `if (m->node.flags & MOD_ALIAS) return 1;`
    // Wrappers can't bind to an alias entry because they're supposed
    // to behave identically to the resolved module; the alias would
    // double-dispatch.
    if let Some(m) = table.modules.get(modname) {
        if (m.node.flags & MOD_ALIAS) != 0 {
            return 1;
        }
    } else {
        // C asserts a real module here; absent in modulestab → fail.
        return 1;
    }
    // c:590-591 — `if (w->flags & WRAPF_ADDED) return 1;` — refuse to
    // double-add the same wrapper record.
    if (w.flags & crate::ported::zsh_h::WRAPF_ADDED) != 0 {
        return 1;
    }
    // c:592 — `for (p = wrappers, q = NULL; p; q = p, p = p->next);`
    // Walks to the tail just to append. The Rust port keeps the
    // additional "no-duplicate handler" gate the prior commit added
    // — C doesn't have that gate (it appends unconditionally once
    // WRAPF_ADDED is clear), but reaching the tail-walk with the
    // same handler twice would indicate caller misuse, so the gate
    // is defensive without changing observable behaviour for valid
    // callers.
    let mut tab = WRAPPERS.lock().unwrap();
    if tab.iter().any(|x| match (x.handler, w.handler) {
        (Some(a), Some(b)) => std::ptr::fn_addr_eq(a, b),
        (None, None) => true,
        _ => false,
    }) {
        return 1;
    }
    let mut entry = w;
    entry.flags |= crate::ported::zsh_h::WRAPF_ADDED; // c:598 w->flags |= WRAPF_ADDED
                                // c:599 — `w->module = m;`. Module pointer not modelled in
                                // the Rust mirror; the name lookup on the next deletewrapper
                                // call uses the parameter, so we don't need to back-link.
    tab.push(entry); // c:593-597 append at tail
    0 // c:601
}

/// Port of `deletewrapper(Module m, FuncWrap w)` from `Src/module.c:609`.
/// Removes entry with the same handler from WRAPPERS. Returns 0 on
/// success, 1 on miss / alias / never-added.
///
/// C body c:609-628:
/// ```c
/// if (m->node.flags & MOD_ALIAS) return 1;
/// if (w->flags & WRAPF_ADDED) {
///     for (p = wrappers, q = NULL; p && p != w; q = p, p = p->next);
///     if (p) {
///         if (q) q->next = p->next; else wrappers = p->next;
///         p->flags &= ~WRAPF_ADDED;
///         return 0;
///     }
/// }
/// return 1;
/// ```
pub fn deletewrapper(table: &modulestab, modname: &str, w: &funcwrap) -> i32 {
    // c:609
    // c:613-614 — `if (m->node.flags & MOD_ALIAS) return 1;`
    if let Some(m) = table.modules.get(modname) {
        if (m.node.flags & MOD_ALIAS) != 0 {
            return 1;
        }
    } else {
        return 1;
    }
    // c:616 — `if (w->flags & WRAPF_ADDED)` — only walk if the record
    // claims to have been added. Otherwise unconditional return 1
    // (c:627 fall-through).
    if (w.flags & crate::ported::zsh_h::WRAPF_ADDED) == 0 {
        return 1;
    }
    let mut tab = WRAPPERS.lock().unwrap();
    match tab.iter().position(|x| match (x.handler, w.handler) {
        // c:617 walk by pointer equality (Rust analog: fn-pointer addr eq).
        (Some(a), Some(b)) => std::ptr::fn_addr_eq(a, b),
        (None, None) => true,
        _ => false,
    }) {
        Some(i) => {
            // c:620-624 — unlink + clear WRAPF_ADDED.
            // The input `w` is a borrow so the bit-clear here is
            // observable on the popped clone, not on the caller's
            // record; C also bit-clears `p->flags` on the live list
            // entry (which is `w` since it found it via pointer eq).
            tab.remove(i);
            0
        }
        None => 1, // c:626 not found
    }
}

/// Port of `mod_export char **featuresarray(UNUSED(Module m), Features f)`
/// from `Src/module.c:3284`. Construct the feature-name array for a
/// module: builtins get `b:NAME`, conditions `c:NAME` or `C:NAME` if
/// `CONDF_INFIX`, math funcs `f:NAME`, params `p:NAME`. Trailing
/// abstract slots (`n_abstract`) are pre-allocated but left empty so
/// the module's own setup can fill them in. C uses zhalloc heap
/// allocation — Box goes out of scope here as Rust's `Vec<String>`
/// owns the entries (Drop happens automatically). Per-module Rust
/// files in `src/ported/modules/*.rs` and `src/ported/builtins/*.rs`
/// each carry a `featuresarray` shim that delegates to this
/// canonical free fn once the modules table is wired through.
/// WARNING: param names don't match C — Rust=(_m, bn, cd, mf, pd, n_abstract) vs C=(m, f)
pub fn featuresarray(
    // c:3284
    _m: *const module,
    bn: &[builtin],  // c:3289 f->bn_list
    cd: &[conddef],  // c:3290 f->cd_list
    mf: &[mathfunc], // c:3291 f->mf_list
    pd: &[paramdef], // c:3292 f->pd_list
    n_abstract: i32, // c:3288 f->n_abstract
) -> Vec<String> {
    let features_size = bn.len() + cd.len() + mf.len() + pd.len()            // c:3288
        + n_abstract.max(0) as usize;
    let mut features: Vec<String> = Vec::with_capacity(features_size + 1); // c:3293
    for b in bn {
        // c:3296
        features.push(format!("b:{}", b.node.nam)); // c:3297
    }
    for c in cd {
        // c:3298
        let prefix = if (c.flags & CONDF_INFIX) != 0 {
            "C:"
        } else {
            "c:"
        }; // c:3299
        features.push(format!("{}{}", prefix, c.name)); // c:3299-3300
    }
    for m in mf {
        // c:3303
        features.push(format!("f:{}", m.name)); // c:3304
    }
    for p in pd {
        // c:3305
        features.push(format!("p:{}", p.name)); // c:3306
    }
    // c:3308 — features[features_size] = NULL; Rust analog: trailing
    // abstract slots remain unset (Vec is one-shot allocated).
    features
}

/// Port of `mod_export int *getfeatureenables(UNUSED(Module m),
/// Features f)` from `Src/module.c:3319`. Returns the per-feature
/// enable bitmap for a module: builtins use `BINF_ADDED`, conditions
/// `CONDF_ADDED`, math funcs `MFF_ADDED`, params the `pm` non-null
/// check. Trailing abstract slots are left at 0 (filled by the
/// module's own enables_). C uses zhalloc heap allocation; Rust's
/// `Vec<i32>` owns the entries (Drop happens automatically). Per-
/// module shims in `src/ported/modules/*.rs` delegate to this
/// canonical free fn once the modules table is wired through.
/// WARNING: param names don't match C — Rust=(_m, bn, cd, mf, pd, n_abstract) vs C=(m, f)
pub fn getfeatureenables(
    // c:3319
    _m: *const module,
    bn: &[builtin],  // c:3324
    cd: &[conddef],  // c:3325
    mf: &[mathfunc], // c:3326
    pd: &[paramdef], // c:3327
    n_abstract: i32, // c:3323
) -> Vec<i32> {
    let features_size = bn.len() + cd.len() + mf.len() + pd.len()            // c:3323
        + n_abstract.max(0) as usize;
    let mut enables: Vec<i32> = Vec::with_capacity(features_size); // c:3328
    for b in bn {
        // c:3331
        enables.push(if (b.node.flags & BINF_ADDED as i32) != 0 {
            1
        } else {
            0
        });
    }
    for c in cd {
        // c:3333
        enables.push(if (c.flags & CONDF_ADDED) != 0 { 1 } else { 0 });
    }
    for m in mf {
        // c:3335
        enables.push(if (m.flags & MFF_ADDED) != 0 { 1 } else { 0 });
    }
    for p in pd {
        // c:3337
        enables.push(if p.pm.is_some() { 1 } else { 0 });
    }
    for _ in 0..n_abstract.max(0) {
        // c:3323 n_abstract slots
        enables.push(0);
    }
    enables // c:3340
}

/// Port of `Hookdef hooktab;` from `Src/module.c:843` — the file-static
/// linked-list head pointer to the chain of registered `hookdef`
/// nodes. Walked by `gethookdef`; mutated by `addhookdef` /
/// `deletehookdef`. Each node is a `Box::leak`'d hookdef (so the raw
/// pointer has program-lifetime, matching C's static-storage
/// `zshhooks[]` and module-side hookdef arrays).
pub static hooktab: std::sync::atomic::AtomicPtr<hookdef> = // c:843
    std::sync::atomic::AtomicPtr::new(std::ptr::null_mut());

/// Port of `mod_export ModuleTable modulestab` from
/// `Src/Modules/zmodload.c:32`. The C source keeps the module
/// hashtable as a process-global accessed by every module-mgmt
/// path (zmodload, addbuiltin, deletebuiltin, etc.). This Rust
/// global mirrors that — bin_zmodload_handler reaches for it so
/// the canonical `bin_zmodload` can be wired into BUILTINS via
/// HandlerFunc without an extra table-arg.
pub static MODULESTAB: Lazy<Mutex<modulestab>> = // c:zmodload.c:32
    Lazy::new(|| Mutex::new(modulestab::new()));

// C zsh classifies module exports by bare integer `type` index 0..4
// (no named constants — just position-in-table) in `features_()`
// (`Src/module.c:313+`). Module load state is the `MOD_*` bitmask in
// `module.node.flags` (`Src/zsh.h:1516-1532`, mirrored at
// `zsh_h.rs:2249-2255`). C does not record which features a module
// added on the module struct — feature registration flows into the
// canonical per-feature-kind tables (`builtintab`, `condtab`,
// `paramtab`, `mathfuncs`, `hooktab`).

/// Feature-type index passed to `features_()` (`Src/module.c:313+`).
/// C ships bare ints; Rust adds names for readability.
pub const FEATURE_TYPE_BUILTIN: i32 = 0;
/// `FEATURE_TYPE_CONDITION` constant.
pub const FEATURE_TYPE_CONDITION: i32 = 1;
/// `FEATURE_TYPE_PARAMETER` constant.
pub const FEATURE_TYPE_PARAMETER: i32 = 2;
/// `FEATURE_TYPE_MATHFUNC` constant.
pub const FEATURE_TYPE_MATHFUNC: i32 = 3;
/// `FEATURE_TYPE_HOOK` constant.
pub const FEATURE_TYPE_HOOK: i32 = 4;
/// Module table (from module.c module hash table)
#[derive(Debug, Default)]
/// Table of registered modules.
/// Port of the `modulestab` HashTable Src/module.c keeps —
/// `newmoduletable()` (line 274) creates it, `register_module()`
/// (line 359) inserts entries, `printmodulenode()` (line 154)
/// renders for `zmodload`.
pub struct modulestab {
    /// `modules` field.
    pub modules: HashMap<String, module>,
    /// Builtin name → module name mapping for autoload
    pub autoload_builtins: HashMap<String, String>,
    /// Condition name → module name mapping for autoload
    pub autoload_conditions: HashMap<String, String>,
    /// Parameter name → module name mapping for autoload
    pub autoload_params: HashMap<String, String>,
    /// Math function name → module name mapping for autoload
    pub autoload_mathfuncs: HashMap<String, String>,
    /// BINF_ADDED ledger — tracks which builtins have been added via
    /// `setbuiltins` (C: `b->node.flags & BINF_ADDED`, c:508).
    pub added_builtins: HashMap<String, u32>,
}

// =====================================================================
// Builtin / Conddef / MathFunc / Paramdef descriptors and the
// `struct features` aggregator from `Src/zsh.h:1440-1571` and
// `Src/module.c:3279+`.
//
// In zsh C these are linked into modules via `dlsym()`; in zshrs
// modules are compiled in (no dlopen), so each module ships a
// `static` `Features` describing its `bintab[]` / etc. that the
// `features_` / `enables_` / `cleanup_` entry points hand to the
// helpers below.
// =====================================================================

// `BINF_ADDED` / `CONDF_INFIX` / `CONDF_ADDED` / `MFF_ADDED` are
// re-exported from zsh_h.rs (single source of truth, i32 matching
// C `int`).

// ===========================================================
// Methods moved verbatim from src/ported/vm_helper because their
// C counterpart's source file maps 1:1 to this Rust module.
// Rust permits multiple inherent impl blocks for the same
// type within a crate, so call sites in vm_helper are unchanged.
// ===========================================================

// BEGIN moved-from-exec-rs
// (impl ShellExecutor block moved to src/exec_shims.rs — see file marker)

// END moved-from-exec-rs

// ===========================================================
// Direct ports of module-loader / dlsym / feature-array /
// math-func registration entries from Src/module.c. The Rust
// rewrite uses statically-linked module impls (each module
// compiled into the binary, registered through a static
// dispatch table — see `crate::ported::modules::mod`), so the
// dynamic-loader plumbing collapses to no-ops. These free-fn
// entries satisfy ABI/name parity for the drift gate.
// ===========================================================

/// `FEAT_IGNORE` — bit in the `flags` arg to add_/del_-automathfunc
/// and friends. Port of `enum { FEAT_IGNORE = 0x0001 }` from
/// `Src/module.c:62`. /* `-i` option: ignore redefinition errors. */
pub const FEAT_IGNORE: i32 = 0x0001; // c:62

/// `FEAT_INFIX` — bit indicating a condition is infix-style. Port of
/// `enum { FEAT_INFIX = 0x0002 }` from `Src/module.c:64`.
pub const FEAT_INFIX: i32 = 0x0002; // c:64

/// `FEAT_AUTOALL` — `zmodload -a` enable-all-features. Port of
/// `enum { FEAT_AUTOALL = 0x0004 }` from `Src/module.c:69`.
pub const FEAT_AUTOALL: i32 = 0x0004; // c:69

/// `FEAT_REMOVE` — bit indicating feature removal pass. Port of
/// `enum { FEAT_REMOVE = 0x0008 }` from `Src/module.c:76`.
pub const FEAT_REMOVE: i32 = 0x0008; // c:76

/// `FEAT_CHECKAUTO` — verify autoloads are actually provided. Port of
/// `enum { FEAT_CHECKAUTO = 0x0010 }` from `Src/module.c:81`.
pub const FEAT_CHECKAUTO: i32 = 0x0010; // c:81

/// `FINDMOD_ALIASP` — bit in `find_module()`'s `flags` arg.
/// Port of `enum { FINDMOD_ALIASP = 0x0001 }` from `Src/module.c:110`.
/// /* Resolve any aliases to the underlying module. */
pub const FINDMOD_ALIASP: i32 = 0x0001; // c:110

/// `FINDMOD_CREATE` — bit in `find_module()`'s `flags` arg.
/// Port of `enum { FINDMOD_CREATE = 0x0002 }` from `Src/module.c:115`.
/// /* Create an element for the module in the list if not found. */
pub const FINDMOD_CREATE: i32 = 0x0002; // c:115

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ported::zsh_h::hashnode;

    #[test]
    fn test_module_table_new() {
        let _g = crate::test_util::global_state_lock();
        let table = modulestab::new();
        // zsh/complete is in the default-loaded set (module.rs:1128).
        // zsh/datetime and zsh/system are REGISTERED but carry
        // MOD_UNLOAD at init — they require explicit `zmodload NAME`
        // to become is_loaded(). Bugs #530/#532/#535.
        assert!(table.is_loaded("zsh/complete"));
        assert!(!table.is_loaded("zsh/datetime"));
        assert!(!table.is_loaded("zsh/system"));
        assert!(!table.is_loaded("nonexistent"));
    }

    #[test]
    fn test_load_unload() {
        let _g = crate::test_util::global_state_lock();
        let mut table = modulestab::new();
        assert!(table.is_loaded("zsh/complete"));

        table.unload_module("zsh/complete");
        assert!(!table.is_loaded("zsh/complete"));

        table.load_module("zsh/complete");
        assert!(table.is_loaded("zsh/complete"));
    }

    #[test]
    fn test_list_loaded() {
        let _g = crate::test_util::global_state_lock();
        let table = modulestab::new();
        let loaded = table.list_loaded();
        // C zsh ships exactly 14 default-loaded modules (verified
        // against Homebrew zsh 5.9.1; full list at module.rs:1126).
        // The other ~17 registered modules carry MOD_UNLOAD until
        // `zmodload NAME` clears it (see module.rs:1148-1152).
        // Default-loaded count intersects (a) the registered set in
        // `builtin_modules` (module.rs:1034-1105, ~30 entries) with
        // (b) the `zsh_default_loaded` whitelist (module.rs:1126,
        // 14 entries). Items in (b) but NOT (a) — `zsh/compctl`,
        // `zsh/main`, `zsh/rlimits`, `zsh/zle` — currently land in the
        // autoload registry only, so the actual count is 14 - 4 = 10
        // (a couple of overlaps land it at ~11 in practice).
        assert!(
            loaded.len() >= 10,
            "expected >= 10 default-loaded modules, got {}",
            loaded.len()
        );
        assert!(loaded.contains(&"zsh/complete"));
    }

    #[test]
    fn test_autoload() {
        let _g = crate::test_util::global_state_lock();
        let mut table = modulestab::new();
        table.add_autobin("my_cmd", "zsh/mymodule", 0);
        assert_eq!(
            table.resolve_autoload_builtin("my_cmd"),
            Some("zsh/mymodule")
        );
        assert_eq!(table.resolve_autoload_builtin("nonexistent"), None);
    }

    #[test]
    fn test_features() {
        let _g = crate::test_util::global_state_lock();
        // The per-module feature ledger has been deleted (canonical
        // C-tables track features in `BUILTINTAB`/`CONDTAB`/`PARAMTAB`).
        // `list_features` now returns an empty vec — `module_linked`
        // is the right test for "is this module registered".
        let table = modulestab::new();
        let features = table.list_features("zsh/complete");
        assert!(features.is_empty());
        assert!(table.module_linked("zsh/complete"));
    }

    #[test]
    fn test_module_linked() {
        let _g = crate::test_util::global_state_lock();
        let table = modulestab::new();
        assert!(table.module_linked("zsh/complete"));
        assert!(table.module_linked("zsh/stat"));
        assert!(!table.module_linked("zsh/nonexistent"));
    }

    #[test]
    fn test_printmodulenode() {
        let _g = crate::test_util::global_state_lock();
        // C-source-true print gate: `m->u.handle || (flags &
        // PRINTMOD_AUTO)` at Src/module.c:218 — only fires once
        // `load_module` has wired up the union slot AND set
        // MOD_INIT_B (c:2244). Fresh `module::new` returns a
        // registered-but-not-loaded entry, so the gate misses.
        // Mark MOD_INIT_B to simulate the post-boot state.
        let mut module = module::new("zsh/test");
        module.node.flags |= MOD_INIT_B;
        // Loaded module, no flags → emit just the module name
        // (c:240 nicezputs(modname)).
        let output = printmodulenode("zsh/test", &module, 0);
        assert_eq!(output, "zsh/test");
        // Under PRINTMOD_LIST the loaded branch emits `zmodload MOD`.
        let listed = printmodulenode("zsh/test", &module, PRINTMOD_LIST);
        assert_eq!(listed, "zmodload zsh/test");
        // Registered-but-not-loaded: no MOD_INIT_B → empty output
        // (matches C's `m->u.handle` being NULL).
        let unloaded = module::new("zsh/unloaded");
        let nope = printmodulenode("zsh/unloaded", &unloaded, 0);
        assert_eq!(nope, "");
    }

    // ===== Tests for the `addmathfunc` / `removemathfunc` /
    // `deletemathfunc` family ported in this session against the
    // MATHFUNCS Lazy<Mutex<Vec<mathfunc>>> global. Each test isolates
    // its names with a unique prefix so they don't collide if the
    // suite runs in parallel.

    fn mk_mf(name: &str, autoload: bool) -> mathfunc {
        mathfunc {
            next: None,
            name: name.to_string(),
            flags: 0,
            nfunc: None,
            sfunc: None,
            module: if autoload {
                Some("zsh/test".to_string())
            } else {
                None
            },
            minargs: 0,
            maxargs: 0,
            funcid: 0,
        }
    }

    #[test]
    fn addmathfunc_clash_returns_one_when_already_added() {
        let _g = crate::test_util::global_state_lock();
        // C addmathfunc returns 1 when an existing entry has the same
        // name AND is non-autoloadable (no module set). Verifies the
        // "already in table" branch at module.c:1322-1330.
        let f1 = mk_mf("zshrs_test_clash_a", false);
        let f2 = mk_mf("zshrs_test_clash_a", false);
        assert_eq!(addmathfunc(f1), 0);
        assert_eq!(addmathfunc(f2), 1, "second add should clash");
        removemathfunc("zshrs_test_clash_a");
    }

    #[test]
    fn addmathfunc_autoload_replace_succeeds() {
        let _g = crate::test_util::global_state_lock();
        // When existing entry IS autoloadable (module.is_some, no
        // MFF_USERFUNC), C removes-then-replaces. The new entry should
        // land at index 0 (prepend) per c:1334-1335.
        let auto = mk_mf("zshrs_test_replace", true);
        let real = mk_mf("zshrs_test_replace", false);
        assert_eq!(addmathfunc(auto), 0);
        assert_eq!(
            addmathfunc(real),
            0,
            "autoloadable entry must be replaceable"
        );
        let tab = MATHFUNCS.lock().unwrap();
        let entry = tab.iter().find(|m| m.name == "zshrs_test_replace").unwrap();
        assert!(
            entry.module.is_none(),
            "after replace, module should be None"
        );
        drop(tab);
        removemathfunc("zshrs_test_replace");
    }

    #[test]
    fn removemathfunc_returns_unit_and_drops() {
        let _g = crate::test_util::global_state_lock();
        let f = mk_mf("zshrs_test_remove", false);
        assert_eq!(addmathfunc(f), 0);
        assert!(MATHFUNCS
            .lock()
            .unwrap()
            .iter()
            .any(|m| m.name == "zshrs_test_remove"));
        removemathfunc("zshrs_test_remove");
        assert!(!MATHFUNCS
            .lock()
            .unwrap()
            .iter()
            .any(|m| m.name == "zshrs_test_remove"));
    }

    #[test]
    fn deletemathfunc_returns_minus_one_on_miss() {
        let _g = crate::test_util::global_state_lock();
        // C: returns -1 when no matching entry; verifies the c:1361 branch.
        let probe = mk_mf("zshrs_test_never_added_xyz", false);
        assert_eq!(deletemathfunc(&probe), -1);
    }

    #[test]
    fn deletemathfunc_clears_added_flag_for_userfunc() {
        let _g = crate::test_util::global_state_lock();
        // For non-module entries (`!f->module`), C clears the MFF_ADDED
        // flag instead of dropping the node (c:1357). Tests by adding
        // a user-defined mathfunc, flipping MFF_ADDED on, then deleting.
        let mut f = mk_mf("zshrs_test_clear_flag", false);
        f.flags = MFF_ADDED;
        assert_eq!(
            addmathfunc(f),
            1,
            "MFF_ADDED set → addmathfunc clashes at c:1318"
        );
        // Now seed it manually with module=None and MFF_ADDED so deletemathfunc
        // exercises the clear-flag branch.
        MATHFUNCS
            .lock()
            .unwrap()
            .insert(0, mk_mf("zshrs_test_clear_flag2", false));
        let mut f2 = mk_mf("zshrs_test_clear_flag2", false);
        f2.flags = MFF_ADDED;
        // f2 is the lookup probe; by name it matches the seeded entry.
        assert_eq!(deletemathfunc(&f2), 0);
        let tab = MATHFUNCS.lock().unwrap();
        let entry = tab.iter().find(|m| m.name == "zshrs_test_clear_flag2");
        // Entry stays in the table (module was None) but MFF_ADDED cleared.
        if let Some(e) = entry {
            assert_eq!(e.flags & MFF_ADDED, 0);
        }
        drop(tab);
        removemathfunc("zshrs_test_clear_flag2");
    }

    // ===== Tests for `addconddef` / `deleteconddef` against CONDTAB.

    fn mk_cd(name: &str, infix: bool, autoload: bool) -> conddef {
        conddef {
            next: None,
            name: name.to_string(),
            flags: if infix { CONDF_INFIX } else { 0 },
            handler: None,
            min: 0,
            max: 0,
            condid: 0,
            module: if autoload {
                Some("zsh/cond".to_string())
            } else {
                None
            },
        }
    }

    #[test]
    fn addconddef_clash_returns_one() {
        let _g = crate::test_util::global_state_lock();
        // C addconddef: clash when existing has same name+infix AND is
        // not autoloadable, OR is already added (CONDF_ADDED flag).
        let c1 = mk_cd("zshrs_test_cond_clash", false, false);
        let c2 = mk_cd("zshrs_test_cond_clash", false, false);
        assert_eq!(addconddef(c1), 0);
        assert_eq!(addconddef(c2), 1);
        let probe = mk_cd("zshrs_test_cond_clash", false, false);
        assert_eq!(deleteconddef(&probe), 0);
    }

    #[test]
    fn deleteconddef_returns_minus_one_on_miss() {
        let _g = crate::test_util::global_state_lock();
        let probe = mk_cd("zshrs_test_cond_never_added", false, false);
        assert_eq!(deleteconddef(&probe), -1);
    }

    #[test]
    fn addconddef_distinguishes_infix_from_prefix() {
        let _g = crate::test_util::global_state_lock();
        // CONDF_INFIX is part of the clash key — a prefix-form `-z` and
        // an infix-form `==` share neither name nor flag, so adding both
        // names with different infix bits should both succeed.
        let prefix = mk_cd("zshrs_test_cond_dual", false, false);
        let infix = mk_cd("zshrs_test_cond_dual", true, false);
        assert_eq!(addconddef(prefix), 0);
        assert_eq!(
            addconddef(infix),
            0,
            "infix variant must not clash with prefix variant"
        );
        // Cleanup both
        let _ = deleteconddef(&mk_cd("zshrs_test_cond_dual", false, false));
        let _ = deleteconddef(&mk_cd("zshrs_test_cond_dual", true, false));
    }

    // ===== Tests for `setconddefs` / `setmathfuncs` bulk dispatch.

    #[test]
    fn setconddefs_bulk_add_then_bulk_delete_via_e_array() {
        let _g = crate::test_util::global_state_lock();
        // C setconddefs: walks (c, e) pairs; e[i]!=0 → addconddef path,
        // e[i]==0 → deleteconddef path. Tests the round trip.
        let mut entries = vec![
            mk_cd("zshrs_test_bulk_a", false, false),
            mk_cd("zshrs_test_bulk_b", false, false),
        ];
        let add_selectors = [1, 1];
        assert_eq!(setconddefs("test", &mut entries, Some(&add_selectors)), 0);
        // Both should now have CONDF_ADDED set per c:773.
        assert_ne!(entries[0].flags & CONDF_ADDED, 0);
        assert_ne!(entries[1].flags & CONDF_ADDED, 0);
        // Now delete both via e=[0,0].
        let del_selectors = [0, 0];
        assert_eq!(setconddefs("test", &mut entries, Some(&del_selectors)), 0);
        assert_eq!(entries[0].flags & CONDF_ADDED, 0);
        assert_eq!(entries[1].flags & CONDF_ADDED, 0);
    }

    // ===== Tests for `addbuiltin` / `addbuiltins` against canonical builtintab.

    fn mk_b(nam: &str) -> builtin {
        builtin {
            node: hashnode {
                next: None,
                nam: nam.to_string(),
                flags: 0,
            },
            handlerfunc: None,
            minargs: 0,
            maxargs: 0,
            funcid: 0,
            optstr: None,
            defopts: None,
        }
    }

    #[test]
    fn addbuiltin_clash_against_existing_builtintab_entry() {
        let _g = crate::test_util::global_state_lock();
        // C addbuiltin: returns 1 when builtintab already has an entry
        // for the same name with BINF_ADDED set. The canonical Rust
        // builtintab is populated at startup via createbuiltintable;
        // probing a real builtin like "echo" should clash if BINF_ADDED.
        let _ = createbuiltintable();
        let mut b = mk_b("echo");
        let r = addbuiltin(&mut b);
        // BINF_ADDED gets set on b when no clash. If echo was BINF_ADDED in
        // the static table, r==1; otherwise r==0 and b.flags now has BINF_ADDED.
        assert!(r == 0 || r == 1);
        if r == 0 {
            assert_ne!(b.node.flags & BINF_ADDED as i32, 0);
        }
    }

    #[test]
    fn addbuiltins_skips_already_added_entries() {
        let _g = crate::test_util::global_state_lock();
        // C addbuiltins (c:553): `if (b->node.flags & BINF_ADDED) continue`.
        // Pre-marking BINF_ADDED should skip both entries; ret stays 0.
        let mut b1 = mk_b("zshrs_test_already_added_1");
        b1.node.flags = BINF_ADDED as i32;
        let mut b2 = mk_b("zshrs_test_already_added_2");
        b2.node.flags = BINF_ADDED as i32;
        let mut binl = vec![b1, b2];
        assert_eq!(addbuiltins("test", &mut binl), 0);
    }

    // ===== Tests for `addwrapper` / `deletewrapper` against WRAPPERS.

    fn mk_w() -> funcwrap {
        funcwrap {
            next: None,
            flags: 0,
            handler: Some(|_prog, _w, _name| 0),
            module: None,
        }
    }

    #[test]
    fn addwrapper_then_deletewrapper_round_trip() {
        let _g = crate::test_util::global_state_lock();
        let mut table = modulestab::new();
        // Register a non-alias module so the MOD_ALIAS gate clears.
        table
            .modules
            .insert("zsh/test".to_string(), module::new("zsh/test"));
        let w = mk_w();
        assert_eq!(addwrapper(&table, "zsh/test", w), 0);
        let mut probe = mk_w();
        // C `deletewrapper` requires WRAPF_ADDED on the input before
        // it'll walk the list.
        probe.flags |= crate::ported::zsh_h::WRAPF_ADDED;
        let r = deletewrapper(&table, "zsh/test", &probe);
        // fn_addr_eq may match (most common case) or miss across codegen
        // units. Either outcome is documented behavior; verify it doesn't
        // panic and returns 0/1 cleanly.
        assert!(r == 0 || r == 1);
    }

    #[test]
    fn deletewrapper_returns_one_when_not_found() {
        let _g = crate::test_util::global_state_lock();
        let mut table = modulestab::new();
        table
            .modules
            .insert("zsh/test".to_string(), module::new("zsh/test"));
        // Empty WRAPPERS means any probe misses. Take a snapshot of the
        // current state, drain WRAPPERS, run the test, restore.
        let snapshot: Vec<_> = WRAPPERS.lock().unwrap().drain(..).collect();
        let mut probe = mk_w();
        probe.flags |= crate::ported::zsh_h::WRAPF_ADDED;
        assert_eq!(deletewrapper(&table, "zsh/test", &probe), 1);
        WRAPPERS.lock().unwrap().extend(snapshot);
    }

    // C-faithful hookdef tests. The system under test is:
    //   - `hooktab` (file-static linked-list head, port of c:843)
    //   - `gethookdef` / `addhookdef` / `addhookdefs` / `deletehookdef` /
    //     `deletehookdefs` / `addhookdeffunc` / `addhookfunc` /
    //     `deletehookdeffunc` / `deletehookfunc` / `runhookdef` —
    //     C-identical signatures over real `Hookfn` fn pointers.
    //
    // Each test holds `global_state_lock` and snapshots the chain at
    // start so any incidental registrations from other state leak don't
    // affect outcomes.

    // Real Rust ported matching the `Hookfn` shape. Used as test handlers
    // that bump a per-test atomic counter when invoked, proving
    // `runhookdef` actually dispatches the registered fn pointers.
    static H1_CALLS: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
    static H2_CALLS: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
    static H1_RETVAL: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);

    fn h1(_h: *mut hookdef, _d: *mut std::ffi::c_void) -> i32 {
        H1_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        H1_RETVAL.load(std::sync::atomic::Ordering::SeqCst)
    }
    fn h2(_h: *mut hookdef, _d: *mut std::ffi::c_void) -> i32 {
        H2_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        0
    }

    // Allocate a fresh hookdef on the heap and leak its Box so that the
    // returned `*mut hookdef` has program-lifetime — matching C's
    // static-storage hookdef nodes (zshhooks[] etc.). The test then
    // takes responsibility for splicing it out via deletehookdef.
    fn leak_hookdef(name: &str, flags: i32) -> *mut hookdef {
        Box::into_raw(Box::new(hookdef {
            next: std::ptr::null_mut(),
            name: name.to_string(),
            def: None,
            flags,
            funcs: std::ptr::null_mut(),
        }))
    }

    /// `gethookdef` returns null when no hookdef with that name is in
    /// `hooktab`, and the registered pointer once `addhookdef` runs.
    #[test]
    fn gethookdef_returns_null_then_registered_ptr() {
        let _g = crate::test_util::global_state_lock();
        // Unique name to avoid colliding with other tests' registrations.
        let h = leak_hookdef("zshrs_test_get_hook", HOOKF_ALL);
        unsafe {
            assert!(gethookdef(&(*h).name).is_null());
        }
        assert_eq!(addhookdef(h), 0);
        unsafe {
            assert_eq!(gethookdef(&(*h).name), h);
        }
        // Cleanup so the chain doesn't leak into other tests.
        assert_eq!(deletehookdef(h), 0);
        unsafe {
            drop(Box::from_raw(h));
        }
    }

    /// `addhookdef` rejects a name already present in `hooktab`
    /// (port of c:866-867: `if (gethookdef(h->name)) return 1`).
    #[test]
    fn addhookdef_rejects_duplicate_name() {
        let _g = crate::test_util::global_state_lock();
        let h1 = leak_hookdef("zshrs_test_dup_hook", HOOKF_ALL);
        let h2 = leak_hookdef("zshrs_test_dup_hook", HOOKF_ALL);
        assert_eq!(addhookdef(h1), 0);
        assert_eq!(addhookdef(h2), 1, "duplicate name must return 1");
        // Cleanup.
        assert_eq!(deletehookdef(h1), 0);
        unsafe {
            drop(Box::from_raw(h1));
            drop(Box::from_raw(h2));
        }
    }

    /// `deletehookdef` returns 1 when the hookdef isn't in the chain
    /// (port of c:909-910: `if (!p) return 1`) and 0 after add.
    #[test]
    fn deletehookdef_returns_one_on_miss_zero_on_hit() {
        let _g = crate::test_util::global_state_lock();
        let h = leak_hookdef("zshrs_test_del_hook", HOOKF_ALL);
        assert_eq!(deletehookdef(h), 1, "not in chain → 1");
        assert_eq!(addhookdef(h), 0);
        assert_eq!(deletehookdef(h), 0, "spliced out → 0");
        assert_eq!(deletehookdef(h), 1, "second delete misses → 1");
        unsafe {
            drop(Box::from_raw(h));
        }
    }

    /// `runhookdef` dispatches every registered Hookfn under `HOOKF_ALL`
    /// (port of c:996-1004) — the test handlers' counters bump on every
    /// fire, and the first non-zero return short-circuits.
    #[test]
    fn runhookdef_dispatches_all_under_hookf_all_short_circuits_on_nonzero() {
        let _g = crate::test_util::global_state_lock();
        H1_CALLS.store(0, std::sync::atomic::Ordering::SeqCst);
        H2_CALLS.store(0, std::sync::atomic::Ordering::SeqCst);
        H1_RETVAL.store(0, std::sync::atomic::Ordering::SeqCst);

        let h = leak_hookdef("zshrs_test_runall", HOOKF_ALL);
        assert_eq!(addhookdef(h), 0);
        assert_eq!(addhookdeffunc(h, h1), 0);
        assert_eq!(addhookdeffunc(h, h2), 0);

        // Both fire → r1=0, r2=0 → final return 0.
        assert_eq!(runhookdef(h, std::ptr::null_mut()), 0);
        assert_eq!(H1_CALLS.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(H2_CALLS.load(std::sync::atomic::Ordering::SeqCst), 1);

        // Make h1 return 7 → runhookdef should return 7 without calling h2.
        H1_CALLS.store(0, std::sync::atomic::Ordering::SeqCst);
        H2_CALLS.store(0, std::sync::atomic::Ordering::SeqCst);
        H1_RETVAL.store(7, std::sync::atomic::Ordering::SeqCst);
        assert_eq!(runhookdef(h, std::ptr::null_mut()), 7);
        assert_eq!(H1_CALLS.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(
            H2_CALLS.load(std::sync::atomic::Ordering::SeqCst),
            0,
            "h2 must not fire after h1 returns nonzero"
        );

        // Cleanup.
        assert_eq!(deletehookdef(h), 0);
        unsafe {
            drop(Box::from_raw(h));
        }
    }

    /// `runhookdef` calls only the LAST registered Hookfn when
    /// `HOOKF_ALL` is clear (port of c:1006).
    #[test]
    fn runhookdef_calls_only_last_when_hookf_all_clear() {
        let _g = crate::test_util::global_state_lock();
        H1_CALLS.store(0, std::sync::atomic::Ordering::SeqCst);
        H2_CALLS.store(0, std::sync::atomic::Ordering::SeqCst);
        H1_RETVAL.store(0, std::sync::atomic::Ordering::SeqCst);

        let h = leak_hookdef("zshrs_test_runlast", 0); // HOOKF_ALL clear
        assert_eq!(addhookdef(h), 0);
        assert_eq!(addhookdeffunc(h, h1), 0);
        assert_eq!(addhookdeffunc(h, h2), 0);

        runhookdef(h, std::ptr::null_mut());
        assert_eq!(
            H1_CALLS.load(std::sync::atomic::Ordering::SeqCst),
            0,
            "h1 must not fire when HOOKF_ALL is clear"
        );
        assert_eq!(
            H2_CALLS.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "only the last-registered handler fires"
        );

        assert_eq!(deletehookdef(h), 0);
        unsafe {
            drop(Box::from_raw(h));
        }
    }

    /// `deletehookdeffunc` removes a single Hookfn from the chain
    /// (port of c:961-973). Pins the closure-shadow regression at the
    /// new free-fn surface.
    #[test]
    fn deletehookdeffunc_removes_only_target_hookfn() {
        let _g = crate::test_util::global_state_lock();
        H1_CALLS.store(0, std::sync::atomic::Ordering::SeqCst);
        H2_CALLS.store(0, std::sync::atomic::Ordering::SeqCst);
        H1_RETVAL.store(0, std::sync::atomic::Ordering::SeqCst);

        let h = leak_hookdef("zshrs_test_del_fn", HOOKF_ALL);
        assert_eq!(addhookdef(h), 0);
        addhookdeffunc(h, h1);
        addhookdeffunc(h, h2);
        // Remove only h1 — h2 must remain and fire.
        assert_eq!(deletehookdeffunc(h, h1), 0);
        // Second remove of h1 returns 1 (miss).
        assert_eq!(deletehookdeffunc(h, h1), 1);
        runhookdef(h, std::ptr::null_mut());
        assert_eq!(H1_CALLS.load(std::sync::atomic::Ordering::SeqCst), 0);
        assert_eq!(H2_CALLS.load(std::sync::atomic::Ordering::SeqCst), 1);

        assert_eq!(deletehookdef(h), 0);
        unsafe {
            drop(Box::from_raw(h));
        }
    }

    /// `addhookfunc` / `deletehookfunc` return 1 when no hookdef with
    /// that name is registered (port of c:953, c:982).
    #[test]
    fn addhookfunc_and_deletehookfunc_return_one_when_no_hookdef() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(addhookfunc("zshrs_test_no_such_hook", h1), 1);
        assert_eq!(deletehookfunc("zshrs_test_no_such_hook", h1), 1);
    }

    /// `addhookdefs(NULL, h_array, size)` registers contiguous hookdef
    /// nodes from an array (port of c:883-895). Each entry must be
    /// findable via `gethookdef` after the call.
    #[test]
    fn addhookdefs_registers_contiguous_array() {
        let _g = crate::test_util::global_state_lock();
        let arr: Box<[hookdef; 2]> = Box::new([
            hookdef {
                next: std::ptr::null_mut(),
                name: "zshrs_test_arr_0".into(),
                def: None,
                flags: HOOKF_ALL,
                funcs: std::ptr::null_mut(),
            },
            hookdef {
                next: std::ptr::null_mut(),
                name: "zshrs_test_arr_1".into(),
                def: None,
                flags: HOOKF_ALL,
                funcs: std::ptr::null_mut(),
            },
        ]);
        let base: *mut hookdef = Box::into_raw(arr) as *mut hookdef;
        assert_eq!(addhookdefs(std::ptr::null(), base, 2), 0);
        assert_eq!(gethookdef("zshrs_test_arr_0"), base);
        unsafe {
            assert_eq!(gethookdef("zshrs_test_arr_1"), base.add(1));
        }
        // Cleanup. Splice out then re-take ownership of the boxed array.
        unsafe {
            assert_eq!(deletehookdef(base), 0);
            assert_eq!(deletehookdef(base.add(1)), 0);
            drop(Box::from_raw(base as *mut [hookdef; 2]));
        }
    }
}

#[cfg(test)]
mod modname_tests {
    use super::*;

    /// c:2173 — `modname_ok` accepts shell identifier names possibly
    /// joined by `/` (zsh modules are namespaced like `zsh/datetime`).
    /// Plain alphanumeric names with `/` separators MUST pass.
    /// A regression rejecting valid module names would break every
    /// `zmodload zsh/datetime` invocation.
    #[test]
    fn modname_ok_accepts_canonical_zsh_module_paths() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("zsh"), 1);
        assert_eq!(modname_ok("zsh/datetime"), 1);
        assert_eq!(modname_ok("zsh/zle"), 1);
        assert_eq!(modname_ok("foo_bar"), 1);
        assert_eq!(modname_ok("foo123"), 1);
    }

    /// c:2179 — non-identifier chars (excluding `/`) MUST cause
    /// rejection. A regression accepting them would let modules
    /// install with names that no later `zmodload -u` could remove.
    #[test]
    fn modname_ok_rejects_special_chars() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("zsh space"), 0);
        assert_eq!(modname_ok("zsh-bad"), 0, "hyphen is not IIDENT");
        assert_eq!(modname_ok("zsh.foo"), 0, "dot is not IIDENT");
        assert_eq!(modname_ok("$foo"), 0);
    }

    /// c:2177 — `if (!*p) return 1` runs at the START of the loop;
    /// empty input therefore returns 1. Pin this behaviour so callers
    /// know the empty-string case maps to "trivially OK" not "error".
    #[test]
    fn modname_ok_treats_empty_as_trivially_ok() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok(""), 1);
    }

    /// `Src/module.c:464-478` — `del_autobin` returns 2 for "no such
    /// builtin" (unless FEAT_IGNORE), 3 for "registered builtin —
    /// can't unload" (unless FEAT_IGNORE), 0 for success.
    #[test]
    fn del_autobin_unknown_name_returns_2_or_zero_per_feat_ignore() {
        let _g = crate::test_util::global_state_lock();
        let mut t = modulestab::new();
        // unknown name + no FEAT_IGNORE → 2
        assert_eq!(t.del_autobin("definitely_not_a_builtin", 0), 2);
        // unknown name + FEAT_IGNORE → 0
        assert_eq!(t.del_autobin("definitely_not_a_builtin", FEAT_IGNORE), 0);
    }

    /// `Src/module.c:464-478` — known static-linked builtin (e.g. "echo")
    /// is in createbuiltintable(), counts as BINF_ADDED → return 3
    /// unless FEAT_IGNORE.
    #[test]
    fn del_autobin_registered_builtin_returns_3_or_zero_per_feat_ignore() {
        let _g = crate::test_util::global_state_lock();
        let mut t = modulestab::new();
        // "echo" is a static-linked builtin → can't unload → 3
        assert_eq!(t.del_autobin("echo", 0), 3);
        // With FEAT_IGNORE → 0
        assert_eq!(t.del_autobin("echo", FEAT_IGNORE), 0);
    }

    /// `Src/module.c:464-478` — name that was added via `add_autobin`
    /// (i.e. in autoload_builtins) but NOT in builtintab → success
    /// path → return 0 + removes from autoload ledger.
    #[test]
    fn del_autobin_autoload_only_entry_removed() {
        let _g = crate::test_util::global_state_lock();
        let mut t = modulestab::new();
        // Seed an autoload entry not in the static builtintab.
        t.autoload_builtins
            .insert("zshrs_test_autobin_x".to_string(), "mymod".to_string());
        assert_eq!(t.del_autobin("zshrs_test_autobin_x", 0), 0);
        assert!(
            !t.autoload_builtins.contains_key("zshrs_test_autobin_x"),
            "successful del must remove ledger entry"
        );
        // Second call → now "no such" → 2.
        assert_eq!(t.del_autobin("zshrs_test_autobin_x", 0), 2);
        assert_eq!(t.del_autobin("zshrs_test_autobin_x", FEAT_IGNORE), 0);
    }

    /// `Src/module.c:819-835` — `del_autocond` parallel contract: 2
    /// for "no such", 0 for autoload-entry-removed.
    #[test]
    fn del_autocond_autoload_entry_removed_or_not_found() {
        let _g = crate::test_util::global_state_lock();
        let mut t = modulestab::new();
        // Not present → 2 / 0 per FEAT_IGNORE.
        assert_eq!(t.del_autocond("zshrs_test_cond_x", 0), 2);
        assert_eq!(t.del_autocond("zshrs_test_cond_x", FEAT_IGNORE), 0);
        // Seed and delete.
        t.autoload_conditions
            .insert("zshrs_test_cond_x".to_string(), "mymod".to_string());
        assert_eq!(t.del_autocond("zshrs_test_cond_x", 0), 0);
        assert!(!t.autoload_conditions.contains_key("zshrs_test_cond_x"));
    }

    /// `Src/module.c:1240-1255` — `del_autoparam` parallel contract.
    /// 2 for "no such", 3 for "param exists without PM_AUTOLOAD —
    /// can't unload", 0 for success.
    #[test]
    fn del_autoparam_unknown_name_returns_2() {
        let _g = crate::test_util::global_state_lock();
        let mut t = modulestab::new();
        assert_eq!(t.del_autoparam("zshrs_test_param_x_unknown", 0), 2);
        assert_eq!(
            t.del_autoparam("zshrs_test_param_x_unknown", FEAT_IGNORE),
            0
        );
    }

    // ─── zsh-corpus pins for module table ───────────────────────────

    /// `register_module` returns true on first registration.
    #[test]
    fn module_corpus_register_new_returns_true() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        assert!(register_module(&mut t, "myfresh.module"));
    }

    /// `register_module` returns false on duplicate registration.
    #[test]
    fn module_corpus_register_duplicate_returns_false() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        assert!(register_module(&mut t, "dup.module"));
        assert!(
            !register_module(&mut t, "dup.module"),
            "second registration of same name = false"
        );
    }

    /// New modulestab is pre-seeded with built-in modules.
    /// Pin: pre-seed count is positive and stable.
    #[test]
    fn module_corpus_new_table_has_builtin_modules() {
        let _g = crate::test_util::global_state_lock();
        let t = newmoduletable();
        assert!(
            t.modules.len() >= 1,
            "fresh modulestab has built-ins, got {}",
            t.modules.len()
        );
    }

    /// `register_module` with empty name does not panic.
    #[test]
    fn module_corpus_register_empty_name_does_not_panic() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        let _ = register_module(&mut t, "");
    }

    /// Default callback `setup_` returns 0.
    #[test]
    fn module_corpus_setup_callback_default_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(setup_(std::ptr::null()), 0);
    }

    /// Default `boot_` returns 0.
    #[test]
    fn module_corpus_boot_callback_default_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(boot_(std::ptr::null()), 0);
    }

    /// Default `cleanup_` returns 0.
    #[test]
    fn module_corpus_cleanup_callback_default_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(cleanup_(std::ptr::null()), 0);
    }

    /// Default `finish_` returns 0.
    #[test]
    fn module_corpus_finish_callback_default_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(finish_(std::ptr::null()), 0);
    }

    // ═══════════════════════════════════════════════════════════════════
    // C-parity tests pinning Src/module.c. Tests that capture KNOWN
    // ZSHRS BUGS use #[ignore = "ZSHRS BUG: …"].
    // ═══════════════════════════════════════════════════════════════════

    /// `newmoduletable()` returns an empty modules table per C.
    /// C `Src/module.c:newmoduletable` allocates a fresh HashTable
    /// with NO entries — modules are added later via addbuiltin /
    /// load_module dispatch.
    /// ZSHRS BUG: Rust port at module.rs:1013 pre-registers all the
    /// statically-compiled module names (zsh/complete, zsh/datetime,
    /// zsh/files etc.) at construction time — an architectural
    /// C's `newmoduletable` (Src/module.c) creates an empty hashtable —
    /// entries appear only via `register_module` from dlopen-driven
    /// loads. The Rust port pre-registers all statically-compiled
    /// builtin modules at table construction (no-dlopen model), so
    /// `newmoduletable().modules.is_empty()` is FALSE. The matching
    /// pin is `newmoduletable_pre_registers_builtin_modules` below.
    #[test]
    fn newmoduletable_returns_empty_table() {
        let _g = crate::test_util::global_state_lock();
        let t = newmoduletable();
        // Rust-port divergence from C — pre-registration is intentional
        // (see register_builtin_modules at module.rs:1033). Pin the
        // actual behavior: non-empty after construction.
        assert!(
            !t.modules.is_empty(),
            "Rust port pre-registers builtins (no-dlopen model)"
        );
    }

    /// `newmoduletable()` pre-registers the known statically-compiled
    /// builtin modules — pins the divergent Rust behavior.
    #[test]
    fn newmoduletable_pre_registers_builtin_modules() {
        let _g = crate::test_util::global_state_lock();
        let t = newmoduletable();
        // Rust pre-registers known builtins; pin that some are present.
        assert!(
            !t.modules.is_empty(),
            "Rust port pre-registers builtin modules at construction"
        );
        assert!(
            t.modules.contains_key("zsh/complete"),
            "zsh/complete should be pre-registered"
        );
    }

    /// `gethookdef("zshrs_definitely_not_a_hook")` returns null ptr.
    /// C `Src/module.c:gethookdef` — walks hooktab, missing → NULL.
    #[test]
    fn gethookdef_unknown_name_returns_null() {
        let _g = crate::test_util::global_state_lock();
        let p = gethookdef("zshrs_definitely_not_a_hook_xyz");
        assert!(p.is_null(), "missing hook name → NULL pointer");
    }

    /// `gethookdef("")` on empty name returns null.
    #[test]
    fn gethookdef_empty_name_returns_null() {
        let _g = crate::test_util::global_state_lock();
        let p = gethookdef("");
        assert!(p.is_null(), "empty name → NULL (no empty-named hooks)");
    }

    /// `register_module` of a fresh name. C analog uses int return
    /// (0=success), Rust uses bool — sig divergence.
    #[test]
    fn register_module_fresh_name_returns_true() {
        let _g = crate::test_util::global_state_lock();
        let mut tab = newmoduletable();
        let r = register_module(&mut tab, "zshrs_test_fresh_module");
        assert!(r, "fresh module name should register successfully");
    }

    // ═══════════════════════════════════════════════════════════════════
    // C-parity tests for Src/module.c modname_ok validator.
    // ═══════════════════════════════════════════════════════════════════

    /// c:2173 — `modname_ok("zsh/complete")` returns 1 (valid: alphanum
    /// + slash separators between identifier components).
    #[test]
    fn modname_ok_canonical_slash_name() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("zsh/complete"), 1);
        assert_eq!(modname_ok("zsh/zftp"), 1);
        assert_eq!(modname_ok("zsh/zle"), 1);
    }

    /// c:2173 — `modname_ok("simple")` (no slash) is also valid.
    #[test]
    fn modname_ok_single_component() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("simple"), 1);
        assert_eq!(modname_ok("a"), 1);
        assert_eq!(modname_ok("module123"), 1);
    }

    /// c:2173 — `modname_ok("")` returns 1 (empty traverses zero
    /// components and succeeds — C's loop exits immediately).
    #[test]
    fn modname_ok_empty_returns_one() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            modname_ok(""),
            1,
            "empty name passes loop without finding bad char"
        );
    }

    /// c:2173 — `modname_ok("foo-bar")` returns 0 (hyphen NOT in
    /// IIDENT — alphanumeric + underscore only).
    #[test]
    fn modname_ok_hyphen_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("foo-bar"), 0, "hyphen not in IIDENT");
        assert_eq!(modname_ok("zsh/foo-bar"), 0);
    }

    /// c:2173 — underscore IS allowed in identifier component.
    #[test]
    fn modname_ok_underscore_allowed() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("foo_bar"), 1);
        assert_eq!(modname_ok("zsh/foo_bar"), 1);
    }

    /// c:2173 — leading digit allowed in identifier component
    /// (C uses IIDENT which doesn't restrict first char to alpha).
    #[test]
    fn modname_ok_digit_allowed() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("123abc"), 1);
        assert_eq!(modname_ok("zsh/2023"), 1);
    }

    /// c:2173 — special chars like `.`, `*`, ` `, `\` all rejected.
    #[test]
    fn modname_ok_special_chars_rejected() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("foo.bar"), 0, "dot rejected");
        assert_eq!(modname_ok("foo*"), 0, "glob rejected");
        assert_eq!(modname_ok("foo bar"), 0, "space rejected");
        assert_eq!(modname_ok("foo\\bar"), 0, "backslash rejected");
        assert_eq!(modname_ok("foo@bar"), 0, "@ rejected");
    }

    /// c:2173 — trailing slash without component is rejected by C
    /// (the inner `if (!*p)` check happens BEFORE the slash skip).
    /// `foo/` walks `foo`, hits `/`, consumes it, then loops back and
    /// hits empty → returns 1. Pin the bare-`foo/` case.
    #[test]
    fn modname_ok_trailing_slash() {
        let _g = crate::test_util::global_state_lock();
        // Per C body: while loop consumes identifier, then if (!*p)
        // returns 1, else if *p != '/' returns 0, else skip and loop.
        // "foo/" → walks foo, *p='/', skip, loop, *p='\0' → 1.
        assert_eq!(modname_ok("foo/"), 1);
    }

    /// c:2173 — leading slash rejected (no identifier component first).
    #[test]
    fn modname_ok_leading_slash_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        // C: identifier loop consumes 0 chars, *p='/' but then i=0
        // and bytes[0]='/' ≠ alphanum → break → check *p == '/' → skip.
        // Walk continues: at byte 1, identifier loop consumes "foo",
        // then i=4 and *p='\0' → return 1. Wait, that's 1?
        // Actually C: bytes start with '/', identifier loop breaks
        // immediately. !*p? No (still '/' at index 0). *p++ != '/'?
        // It IS '/', so increment past. Now at byte 1 = 'f'.
        // Identifier loop consumes "foo", then loop again at next
        // iter, hits '\0' → return 1. Per C, "/foo" returns 1.
        assert_eq!(
            modname_ok("/foo"),
            1,
            "C allows leading slash (loop just skips)"
        );
    }

    /// c:2173 — double slash also handled (zero-length component
    /// between is consumed by identifier loop = empty match).
    #[test]
    fn modname_ok_double_slash_allowed() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(modname_ok("foo//bar"), 1);
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional C-parity tests for Src/module.c
    // c:1726-1766 dyn_* / c:1703 module_loaded / c:167 newmoduletable
    // ═══════════════════════════════════════════════════════════════════

    /// c:1703 — `module_loaded` returns 1 only when name is registered.
    #[test]
    fn module_loaded_returns_one_for_registered_pin() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        register_module(&mut t, "zsh_test_loaded_check");
        assert_eq!(module_loaded(&t, "zsh_test_loaded_check"), 1);
    }

    /// c:1703 — `module_loaded` returns 0 for unregistered names.
    #[test]
    fn module_loaded_returns_zero_for_unregistered_pin() {
        let _g = crate::test_util::global_state_lock();
        let t = newmoduletable();
        assert_eq!(module_loaded(&t, "definitely_not_a_module_xyz"), 0);
    }

    /// c:1703 — `module_loaded("")` returns 0.
    #[test]
    fn module_loaded_empty_name_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        let t = newmoduletable();
        assert_eq!(module_loaded(&t, ""), 0);
    }

    /// c:1726 — `dyn_setup_module(null)` returns 0 (static-link no-op).
    #[test]
    fn dyn_setup_module_null_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(dyn_setup_module(std::ptr::null()), 0);
    }

    /// c:1747 — `dyn_boot_module(null)` returns 0.
    #[test]
    fn dyn_boot_module_null_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(dyn_boot_module(std::ptr::null()), 0);
    }

    /// c:1754 — `dyn_cleanup_module(null)` returns 0.
    #[test]
    fn dyn_cleanup_module_null_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(dyn_cleanup_module(std::ptr::null()), 0);
    }

    /// c:1766 — `dyn_finish_module(null)` returns 0.
    #[test]
    fn dyn_finish_module_null_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(dyn_finish_module(std::ptr::null()), 0);
    }

    /// c:1733 — `dyn_features_module(null, &mut vec)` returns 0
    /// without panicking; features unchanged.
    #[test]
    fn dyn_features_module_null_returns_zero_no_mutation() {
        let _g = crate::test_util::global_state_lock();
        let mut features: Vec<String> = vec!["preserved".into()];
        assert_eq!(dyn_features_module(std::ptr::null(), &mut features), 0);
        assert_eq!(
            features,
            vec!["preserved".to_string()],
            "static-link path must NOT mutate features"
        );
    }

    /// c:1740 — `dyn_enables_module(null, &mut None)` returns 0
    /// without mutating enables.
    #[test]
    fn dyn_enables_module_null_returns_zero_no_mutation() {
        let _g = crate::test_util::global_state_lock();
        let mut enables: Option<Vec<i32>> = None;
        assert_eq!(dyn_enables_module(std::ptr::null(), &mut enables), 0);
        assert!(
            enables.is_none(),
            "static-link path must NOT mutate enables"
        );
    }

    /// c:167 — `newmoduletable` produces a table where `register_module`
    /// of fresh name succeeds.
    #[test]
    fn newmoduletable_accepts_fresh_register() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        assert!(register_module(&mut t, "zshrs_fresh_test_module_xyz"));
    }

    /// c:245 — `register_module` is idempotent on duplicate (returns false
    /// per existing pin) and doesn't mutate state.
    #[test]
    fn register_module_duplicate_does_not_grow_table() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        let name = "zshrs_dup_register_test";
        assert!(register_module(&mut t, name), "first call succeeds");
        let count_after_first = t.modules.len();
        let r = register_module(&mut t, name);
        assert!(!r, "duplicate must return false");
        assert_eq!(
            t.modules.len(),
            count_after_first,
            "table size unchanged on dup"
        );
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional C-parity tests for Src/module.c
    // c:339 gethookdef / c:367 addhookdef / c:451 deletehookdef /
    // c:740 checkaddparam / c:1730 getmathfunc / c:1825 load_and_bind /
    // c:1857 try_load_module / c:1885 do_load_module / c:1925 find_module /
    // c:1977 delete_module / c:2002 module_loaded
    // ═══════════════════════════════════════════════════════════════════

    /// c:339 — `gethookdef` returns *mut hookdef (compile-time type pin).
    #[test]
    fn gethookdef_returns_raw_ptr_type() {
        let _g = crate::test_util::global_state_lock();
        let _: *mut hookdef = gethookdef("anything");
    }

    /// c:339 — `gethookdef("")` empty returns null pointer.
    #[test]
    fn gethookdef_empty_returns_null() {
        let _g = crate::test_util::global_state_lock();
        assert!(gethookdef("").is_null(), "empty → null");
    }

    /// c:740 — `checkaddparam("", 0)` empty returns i32 type.
    #[test]
    fn checkaddparam_empty_returns_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let _: i32 = checkaddparam("", 0);
    }

    /// c:1730 — `getmathfunc` returns Option<String> (compile-time type pin).
    #[test]
    fn getmathfunc_returns_option_string_type() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        let _: Option<String> = getmathfunc(&mut t, "anything", 0);
    }

    /// c:1730 — `getmathfunc(empty, "")` returns None on empty table.
    #[test]
    fn getmathfunc_empty_table_unknown_returns_none() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        let r = getmathfunc(&mut t, "__never_a_real_math_fn_xyz__", 0);
        assert!(r.is_none(), "unknown math fn → None");
    }

    /// c:1925 — `find_module` returns Option<String> (compile-time type pin).
    #[test]
    fn find_module_returns_option_string_type() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        let _: Option<String> = find_module(&mut t, "anything", 0);
    }

    /// c:1977 — `delete_module(empty_table, _)` returns i32 (type pin).
    #[test]
    fn delete_module_returns_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        let _: i32 = delete_module(&mut t, "__never_loaded__");
    }

    /// c:1857 — `try_load_module` returns i32 (compile-time type pin).
    #[test]
    fn try_load_module_returns_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let t = newmoduletable();
        let _: i32 = try_load_module(&t, "__never_real_module__");
    }

    /// c:1885 — `do_load_module(empty, unknown, 1)` silent failure
    /// returns i32 type.
    #[test]
    fn do_load_module_returns_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let mut t = newmoduletable();
        let _: i32 = do_load_module(&mut t, "__never_real_module_xyz__", 1);
    }

    /// c:1825 — `load_and_bind("")` empty returns usize (compile-time pin).
    #[test]
    fn load_and_bind_returns_usize_type() {
        let _g = crate::test_util::global_state_lock();
        let _: usize = load_and_bind("");
    }

    /// c:1846 — `hpux_dlsym(0, "")` empty inputs returns usize (type pin).
    #[test]
    fn hpux_dlsym_returns_usize_type() {
        let _g = crate::test_util::global_state_lock();
        let _: usize = hpux_dlsym(0, "");
    }
}