zshrs 0.11.18

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
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
//! Utility functions for zshrs
//!
//! Port from zsh/Src/utils.c
//!
//! Provides miscellaneous utilities: error handling, file operations,
//! string utilities, and character classification.

use std::ffi::CString;
use std::fs;
use std::io::{self, Read, Write};
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::os::unix::io::{AsRawFd, RawFd};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::Mutex;
use std::time::UNIX_EPOCH;

use libc::{
    S_IRGRP, S_IROTH, S_IRUSR, S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP,
    S_IXOTH, S_IXUSR,
};
use crate::{DPUTS, DPUTS1};
use crate::init::zleentry;
use crate::params::getsparam_u;
use crate::ported::builtin::{BUILTINS, SFCONTEXT, STOPMSG};
use crate::ported::compat::u9_iswprint;
use crate::ported::hashnameddir::{nameddirtab, removenameddirnode};
use crate::ported::hashtable::shfunctab_lock;
use crate::ported::hist::{bangchar, chrealpath};
use crate::ported::init::SHTTY;
use crate::ported::modules::clone::{coprocin, coprocout, mypgrp};
// SHTTY imported under an alias to avoid collision with the
// `SHTTY: i32` function parameters at fdsettyinfo/fdgettyinfo
// (Rule E — C uses SHTTY as both the global and the parameter name).
use crate::ported::lex::{lineno, untokenize};
use crate::ported::options::{dosetopt, opt_state_set};
use crate::ported::params::{
    assignsparam, convbase as convbase_param, getaparam, getsparam, homesetfn, ifsgetfn, ifssetfn,
    isident, locallevel as LOCALLEVEL, setaparam, setiparam, wordcharsgetfn, wordcharssetfn,
};
use crate::ported::signals::{queue_signals, unqueue_signals};
use crate::ported::string::dupstrpfx;
use crate::ported::zsh_h::{dirsav, hashnode, interact, isset, nameddir, opt_name, unset, AUTONAMEDIRS, BEEP, CSHJUNKIEQUOTES, DEFAULT_IFS, DVORAK, EMULATE_KSH, EMULATE_SH, EMULATION, FDT_EXTERNAL, FDT_FLOCK, FDT_FLOCK_EXEC, FDT_INTERNAL, FDT_MODULE, FDT_UNUSED, LAST_NORMAL_TOK, MULTIBYTE, Marker, Meta, Dash, NICEFLAG_HEAP, NICEFLAG_NODUP, NICEFLAG_QUOTE, Nularg, ND_NOABBREV, ND_USERNAME, OCTALZEROES, PATCHARS, POSIXIDENTIFIERS, PRINTEIGHTBIT, Pound, QT_BACKSLASH, QT_BACKSLASH_PATTERN, QT_BACKSLASH_SHOWNULL, QT_BACKTICK, QT_DOLLARS, QT_DOUBLE, QT_NONE, QT_SINGLE, QT_SINGLE_OPTIONAL, RCQUOTES, SHINSTDIN, Snull, SPECCHARS, XTRACE, ZLE_CMD_TRASH, CHASELINKS, BANGHIST, SFC_SUBST, RMSTARWAIT, GLOBDOTS, jobbing, HISTFLAG_NOEXEC, shfunc};
use crate::ported::zsh_system_h::DEFAULT_WORDCHARS;
use crate::ported::ztype_h::{
    imeta, itok, iwsep, IALNUM, IALPHA, IBLANK, ICNTRL, IDIGIT, IIDENT, IMETA, INBLANK, INULL,
    IPATTERN, ISEP, ISPECIAL, ITOK,
    IUSER, IWORD, IWSEP, TYPTAB, TYPTAB_FLAGS, ZISPRINT,
    ZTF_BANGCHAR, ZTF_INIT, ZTF_INTERACT, ZTF_SP_COMMA,
};

/// Set a wide-char array from a multibyte source string.
/// Port of `set_widearray(char *mb_array, Widechar_array wca)` from `Src/utils.c:69`.
///
/// ```c
/// static void set_widearray(char *mb_array, Widechar_array wca) {
///     if (wca->chars) free(wca->chars);
///     wca->len = 0;
///     if (mb_array) {
///         while (*mb_array) {
///             if (unsigned char *mb_array <= 0x7f) {
///                 *wcptr++ = (wchar_t)*mb_array++;
///                 continue;
///             }
///             mblen = mb_metacharlenconv(mb_array, &wci);
///             if (!mblen) break;
///             if (wci == WEOF) return;  // any non-convertible aborts
///             *wcptr++ = (wchar_t)wci;
///             mb_array += mblen;
///         }
///         wca->chars = malloc(...); wca->len = wcptr - tmpwcs;
///     }
/// }
/// ```
///
/// Build a wide-char array from a metafied multibyte source string.
/// C uses `mb_metacharlenconv()` to walk Meta-encoded sequences;
/// Rust port unmetafies first, then collects chars (the
/// equivalent: walk Unicode codepoints).
///
/// Returns the new vec; caller assigns to the appropriate slot
/// (`WORDCHARS_w`, `IFS_w`, etc.). C aborts on non-convertible
/// chars (returns without setting `wca->chars`); Rust port mirrors
/// by returning empty Vec when conversion fails.
/// WARNING: param names don't match C — Rust=(mb_array) vs C=(mb_array, wca)
// Rust idiom replacement: `unmetafy` + `str::chars` covers the C
// `mbsrtowcs`+`mbstate_t` conversion; the C `wca` out-param drops
// since the Vec is returned by value.
pub fn set_widearray(mb_array: &str) -> Vec<char> {
    let mut bytes = mb_array.as_bytes().to_vec();
    unmetafy(&mut bytes);
    match std::str::from_utf8(&bytes) {
        Ok(s) => s.chars().collect(),
        Err(_) => Vec::new(),
    }
}

// =====================================================================
// Port of `zwarning(const char *cmd, const char *fmt, va_list ap)` from `Src/utils.c:142`.
// =====================================================================

/// Port of `zwarning(const char *cmd, const char *fmt, va_list ap)` from `Src/utils.c:142`.
///
/// Internal helper that builds the diagnostic prefix and emits it +
/// the formatted message to stderr. Direct C body translation:
///
/// ```c
/// if (isatty(2)) zleentry(ZLE_CMD_TRASH);
/// char *prefix = scriptname ? scriptname : (argzero ? argzero : "");
/// if (cmd) {
///     if (unset(SHINSTDIN) || locallevel) {
///         nicezputs(prefix, stderr);
///         fputc(':', stderr);
///     }
///     nicezputs(cmd, stderr);
///     fputc(':', stderr);
/// } else {
///     nicezputs((isset(SHINSTDIN) && !locallevel) ? "zsh" : prefix, stderr);
///     fputc(':', stderr);
/// }
/// zerrmsg(stderr, fmt, ap);
/// ```
/// WARNING: param names don't match C — Rust=(cmd, msg) vs C=(cmd, fmt, ap)
fn zwarning(cmd: Option<&str>, msg: &str) {
    // c:96 — `if (isatty(2)) zleentry(ZLE_CMD_TRASH);`
    // Flush any in-flight ZLE redraw state before the warning lands
    // on stderr — without this, half-painted edit lines bleed into
    // the diagnostic. Previously: `let _ = isatty(2);` (the result
    // was discarded; the canonical zleentry port at init.rs:905 was
    // never actually called from the warning path).
    if unsafe { libc::isatty(2) } != 0 {
        // c:96
        let _ = zleentry(
            // c:96
            ZLE_CMD_TRASH, // c:96
        );
    }
    let scriptname = scriptname_lock().lock().unwrap().clone();
    let argzero = argzero_lock().lock().unwrap().clone();
    let locallevel = LOCALLEVEL.load(Ordering::Relaxed);
    let prefix: String = scriptname.or(argzero).unwrap_or_default();
    let stderr_handle = io::stderr();
    let mut stderr_lock = stderr_handle.lock();
    if let Some(cmd) = cmd {
        // c:107-110 — `if (unset(SHINSTDIN) || locallevel) {
        //                 nicezputs(prefix, stderr); fputc(':', stderr); }`
        if unset(SHINSTDIN) || locallevel != 0 {
            let _ = nicezputs(&prefix, &mut stderr_lock); // c:108
            let _ = stderr_lock.write_all(b":");
        }
        let _ = nicezputs(cmd, &mut stderr_lock); // c:111
        let _ = stderr_lock.write_all(b":");
    } else {
        // c:114 — `nicezputs((isset(SHINSTDIN) && !locallevel) ? "zsh" : prefix, stderr);`
        let to_emit = if isset(SHINSTDIN) && locallevel == 0 {
            "zsh"
        } else {
            prefix.as_str()
        };
        let _ = nicezputs(to_emit, &mut stderr_lock); // c:114
        let _ = stderr_lock.write_all(b":");
    }
    // c:116 — `zerrmsg(stderr, fmt, ap)` — lineno prefix + message.
    // Pre-built `msg: &str` covers C's va_list; zerrmsg port hasn't
    // had its `(file, fmt, ap)` signature wired yet so the lineno
    // prefix + write is inlined here against the same `unset(SHINSTDIN)`
    // gate C uses at c:301.
    let lineno = lineno() as i32;
    if (unset(SHINSTDIN) || locallevel != 0) && lineno != 0 {
        let _ = stderr_lock.write_all(format!("{}: ", lineno).as_bytes());
    } else {
        let _ = stderr_lock.write_all(b" ");
    }
    let _ = stderr_lock.write_all(msg.as_bytes());
    let _ = stderr_lock.write_all(b"\n");
    let _ = stderr_lock.flush();
}

// =====================================================================
// Port of `zerr(VA_ALIST1(const char *fmt))` / `zerrnam` / `zwarn` / `zwarnnam` from utils.c:173
// onward. Each is a thin wrapper: check errflag/noerrs guards, set
// `ERRFLAG_ERROR` on the fatal variants, call `zwarning`.
// =====================================================================

/// Port of `zerr(VA_ALIST1(const char *fmt))` from `Src/utils.c:173`.
///
/// ```c
/// if (errflag || noerrs) {
///     if (noerrs < 2) errflag |= ERRFLAG_ERROR;
///     return;
/// }
/// errflag |= ERRFLAG_ERROR;
/// zwarning(NULL, fmt, ap);
/// ```
/// WARNING: param names don't match C — Rust=(msg) vs C=()
pub fn zerr(msg: &str) {
    // c:173
    let noerrs = *noerrs_lock().lock().unwrap();
    if errflag.load(Ordering::Relaxed) != 0 || noerrs != 0 {
        // c:175
        if noerrs < 2 {
            // c:176
            errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:176
        }
        return; // c:177
    }
    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:194
    zwarning(None, msg); // c:194
}

/// Port of `zerrnam(VA_ALIST2(const char *cmd, const char *fmt))` from `Src/utils.c:194`.
///
/// ```c
/// if (errflag || noerrs) return;
/// errflag |= ERRFLAG_ERROR;
/// zwarning(cmd, fmt, ap);
/// ```
/// WARNING: param names don't match C — Rust=(cmd, msg) vs C=(cmd)
pub fn zerrnam(cmd: &str, msg: &str) {
    // c:194
    let noerrs = *noerrs_lock().lock().unwrap();
    if errflag.load(Ordering::Relaxed) != 0 || noerrs != 0 {
        // c:196
        return;
    }
    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:214
    zwarning(Some(cmd), msg); // c:214
}

/// Port of `zwarn(VA_ALIST1(const char *fmt))` from `Src/utils.c:214`.
///
/// ```c
/// if (errflag || noerrs) return;
/// zwarning(NULL, fmt, ap);
/// ```
/// WARNING: param names don't match C — Rust=(msg) vs C=()
pub fn zwarn(msg: &str) {
    // c:214
    let noerrs = *noerrs_lock().lock().unwrap();
    if errflag.load(Ordering::Relaxed) != 0 || noerrs != 0 {
        // c:216
        return;
    }
    zwarning(None, msg); // c:231
}

/// Port of `zwarnnam(VA_ALIST2(const char *cmd, const char *fmt))` from `Src/utils.c:231`.
///
/// ```c
/// if (errflag || noerrs) return;
/// zwarning(cmd, fmt, ap);
/// ```
/// WARNING: param names don't match C — Rust=(cmd, msg) vs C=(cmd)
pub fn zwarnnam(cmd: &str, msg: &str) {
    // c:231
    let noerrs = *noerrs_lock().lock().unwrap();
    if errflag.load(Ordering::Relaxed) != 0 || noerrs != 0 {
        // c:233
        return;
    }
    zwarning(Some(cmd), msg); // c:235
}

/// Port of `dputs(VA_ALIST1(const char *message))` from `Src/utils.c:253`.
///
/// C body (c:253-270):
/// ```c
/// mod_export void dputs(const char *message)
/// {
///     char *filename;
///     FILE *file;
///     if ((filename = getsparam_u("ZSH_DEBUG_LOG")) != NULL &&
///         (file = fopen(filename, "a")) != NULL) {
///         zerrmsg(file, message, ap);
///         fclose(file);
///     } else
///         zerrmsg(stderr, message, ap);
/// }
/// ```
///
/// Previous Rust port was a FAKE — `eprintln!("BUG: {}", msg)` ignored
/// `$ZSH_DEBUG_LOG` entirely (which is the whole point of dputs — to
/// route debug output to a user-specified log file). Re-port now
/// consults paramtab via `getsparam_u`, opens the log in append mode,
/// and routes the message there; falls back to stderr per c:268.
///
/// Rust signature drift: takes `&str` instead of va_args; callers
/// pre-format via Rust's `format!` (same pattern as `zerrmsg`).
pub fn dputs(msg: &str) {
    // c:253
    // c:263 — `getsparam_u("ZSH_DEBUG_LOG")`. The `_u` variant
    // unmetafies the result (utils.rs's getsparam_u port at
    // params.rs:3831 wraps getsparam + unmeta).
    let log_file = getsparam_u("ZSH_DEBUG_LOG"); // c:263
                                                 // c:264 — `fopen(filename, "a")` — append mode.
    let opened = log_file.as_ref().and_then(|p| {
        // c:264
        fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(p)
            .ok()
    });
    // Shared format logic: lineno prefix + msg + newline, matching
    // zerrmsg at c:296-308. Built once, written to file or stderr.
    let lineno = lineno() as i32;
    let shinstdin = isset(SHINSTDIN);
    let locallevel = LOCALLEVEL.load(Ordering::Relaxed);
    let prefix = if (!shinstdin || locallevel != 0) && lineno != 0 {
        format!("{}: ", lineno)
    } else {
        String::new()
    };
    let line = format!("{}{}\n", prefix, msg);
    if let Some(mut f) = opened {
        // c:265 zerrmsg(file, ...)
        let _ = f.write_all(line.as_bytes()); // c:265
                                              // c:266 — `fclose(file)` — handled by Drop when `f` goes out of scope.
    } else {
        // c:267 else stderr
        let _ = io::stderr().write_all(line.as_bytes()); // c:268 zerrmsg(stderr, ...)
    }
}

// ---------------------------------------------------------------------------
// Remaining 33 missing utils.c functions
// ---------------------------------------------------------------------------

// `zwarning` is defined earlier in this file as the real port of
// utils.c:142 (private helper invoked by `zerr`/`zerrnam`/`zwarn`/
// `zwarnnam`). The duplicate stub previously here has been deleted
// — callers use the four public entry points instead.

/// Port of `void zz_plural_z_alpha(void)` from Src/utils.c:282.
///
/// Cygwin-only no-op symbol the C source emits to work around a
/// dllwrap bug that drops the last alphabetically-sorted exported
/// symbol (zwarnnam). The Rust port has no equivalent linker
/// problem; this is preserved as a no-op for symbol-table parity.
pub fn zz_plural_z_alpha() {} // c:282

/// Port of `zerrmsg(FILE *file, const char *fmt, va_list ap)` from `Src/utils.c:289`.
///
/// C body emits the formatted message + (when locallevel > 0 or
/// SHINSTDIN unset) the line number prefix + "\n". The Rust port
/// is invoked indirectly through `zwarning` — direct callers pass
/// pre-formatted strings.
/// WARNING: param names don't match C — Rust=(msg, errno) vs C=(file, fmt, ap)
// Rust idiom replacement: pre-formatted `msg: &str` covers the C
// va_list expansion; the C `file`+`fmt`+`ap` triplet collapses
// because callers (zerr/zwarning) pre-format via Rust's `format!`.
pub fn zerrmsg(msg: &str, errno: Option<i32>) {
    // c:289
    // c:296 — `lineno` is the parser-advanced line counter. The
    // previous Rust port read `lineno_lock` (a Mutex<i32> in utils.rs)
    // that ONLY fusevm_bridge::set_lineno wrote to; the actual
    // parser uses lex::LEX_LINENO (thread_local Cell<u64>) updated
    // by every newline scan. Result: error messages emitted from
    // parse-time always printed line 0 (or last fusevm_bridge value)
    // rather than the actual line of the syntax error.
    //
    // Route through lex::lineno() so the parser-advanced counter
    // drives the error prefix.
    let lineno = lineno() as i32;
    let locallevel = LOCALLEVEL.load(Ordering::Relaxed);
    // c:301-308 — `if ((unset(SHINSTDIN) || locallevel) && lineno)
    //                 fprintf(file, "%d: ", lineno); else fputc(' ', file);`
    if (unset(SHINSTDIN) || locallevel != 0) && lineno != 0 {
        eprint!("{}: ", lineno);
    } else {
        eprint!(" ");
    }
    if let Some(e) = errno {
        eprintln!("{}: {}", msg, io::Error::from_raw_os_error(e));
    } else {
        eprintln!("{}", msg);
    }
}

/// Port of `void zsetupterm(void)` from Src/utils.c:390.
///
/// Reference-counts terminfo's `cur_term` setup. The C source
/// guards with `#ifdef HAVE_SETUPTERM`; without terminfo this is a
/// pure no-op. Rust's `term`-style state lives elsewhere (modules
/// like `zle/termcap` initialize on demand), so this is a no-op
/// counter for symbol-table parity.
pub fn zsetupterm() {
    // c:390
    static TERM_COUNT: std::sync::atomic::AtomicI32 = // c:402
        std::sync::atomic::AtomicI32::new(0);
    let tc = TERM_COUNT.load(Ordering::Relaxed);
    // c:395-396 — DPUTS(term_count < 0 || (term_count > 0 && !cur_term),
    //                   "inconsistent term_count and/or cur_term");
    // The Rust port has no `cur_term` analogue (terminfo state lives
    // in zle/termcap module), so the second condition reduces to
    // `term_count > 0 && true` if we treat cur_term as the count
    // itself. Simplified to the bare `term_count < 0` invariant.
    DPUTS!(tc < 0, "inconsistent term_count and/or cur_term"); // c:395-396
    TERM_COUNT.fetch_add(1, Ordering::Relaxed); // c:402
}

/// Port of `void zdeleteterm(void)` from Src/utils.c:409.
pub fn zdeleteterm() {
    // c:409
    static TERM_COUNT: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
    let tc = TERM_COUNT.load(Ordering::Relaxed);
    // c:412-413 — DPUTS(term_count < 1 || !cur_term,
    //                   "inconsistent term_count and/or cur_term");
    DPUTS!(tc < 1, "inconsistent term_count and/or cur_term"); // c:412-413
    if tc > 0 {
        TERM_COUNT.fetch_sub(1, Ordering::Relaxed); // c:414
    }
}

/// Port of `int putraw(int c)` from Src/utils.c:424. Writes a
/// single byte to stdout for the termcap library, returning 0.
pub fn putraw(c: char) -> i32 {
    // c:424
    print!("{}", c); // c:434
    0 // c:434
}

/// Port of `int putshout(int c)` from Src/utils.c:434. Writes a
/// single byte to `shout` (zsh's interactive stdout — falls back
/// to stdout in zshrs's static-link path), returning 0.
pub fn putshout(c: char) -> i32 {
    // c:434
    print!("{}", c); // c:434
    0 // c:437
}

/// Port of `nicechar_sel(int c, int quotable)` from `Src/utils.c:462`.
/// Renders one byte as its `^X` / `M-X` / `\\n` / `\\t` display form;
/// `quotable=true` emits `\\C-X` instead of `^X` so the result is
/// shell-quotable.
pub fn nicechar_sel(c: char, quotable: bool) -> String {
    // c:462
    // c:466 — `c &= 0xff;` mask to byte before any classification.
    let mut c = (c as u32) & 0xff;
    let mut out = String::new();
    // c:467 — `if (ZISPRINT(c)) goto done;`. Use the canonical
    // `ZISPRINT` predicate (port of ztype.h:89 — IPRINT typtab bit
    // AND != 0x7f). Previously used a custom `is_print` closure that
    // wrongly accepted 0xa0+ as printable — diverged from C ZISPRINT
    // for every high-bit byte under PRINTEIGHTBIT-off, breaking
    // `\M-X` escape generation.
    if !ZISPRINT(c as u8) {
        // c:467
        if c & 0x80 != 0 {
            // c:469
            if isset(PRINTEIGHTBIT) { // c:470
                 // c:471 — goto done (output raw); c unchanged.
            } else {
                out.push_str("\\M-"); // c:472-474
                c &= 0x7f; // c:475
                if ZISPRINT(c as u8) {
                    // c:476-477
                    // c:477 — goto done after writing \M- + ASCII char.
                    if let Some(ch) = char::from_u32(c) {
                        out.push(ch);
                    }
                    return out;
                }
            }
        }
        if c == 0x7f {
            // c:479
            out.push_str(if quotable { "\\C-" } else { "^" }); // c:481-486
            c = b'?' as u32;
        } else if c == b'\n' as u32 {
            // c:487
            out.push('\\');
            c = b'n' as u32;
        } else if c == b'\t' as u32 {
            // c:490
            out.push('\\');
            c = b't' as u32;
        } else if c < 0x20 {
            // c:493
            out.push_str(if quotable { "\\C-" } else { "^" }); // c:494-499
            c += 0x40;
        }
    }
    if let Some(ch) = char::from_u32(c) {
        // c:511
        out.push(ch);
    }
    out
}

/// Port of `nicechar(int c)` from Src/utils.c:520. C body:
///     `return nicechar_sel(c, 0);`
pub fn nicechar(c: char) -> String {
    // c:520
    nicechar_sel(c, false) // c:523
}

/// Port of `is_nicechar(int c)` from `Src/utils.c:531-539`.
/// ```c
/// c &= 0xff;
/// if (ZISPRINT(c)) return 0;
/// if (c & 0x80)    return !isset(PRINTEIGHTBIT);
/// return (c == 0x7f || c == '\n' || c == '\t' || c < 0x20);
/// ```
/// "Nice" means "needs escape-formatting when printed" — so
/// returns true for control chars + (under PRINTEIGHTBIT off)
/// high-bit bytes. The previous Rust port treated all `!c.is_ascii()`
/// as nice unconditionally — divergent for users running with
/// `setopt printeightbit` (very common for non-ASCII filenames).
pub fn is_nicechar(c: char) -> bool {
    let cu = (c as u32) & 0xff;
    // c:534 — `if (ZISPRINT(c)) return 0;` — printable ASCII is not nice.
    if ZISPRINT(cu as u8) {
        return false;
    }
    // c:536 — high-bit byte path.
    if (cu & 0x80) != 0 {
        return !isset(PRINTEIGHTBIT);
    }
    // c:538 — ASCII control chars (DEL/\n/\t/<0x20).
    cu == 0x7f || cu == b'\n' as u32 || cu == b'\t' as u32 || cu < 0x20
}

/// Initialize multibyte state (from utils.c mb_charinit) - no-op in Rust
/// Port of `mb_charinit` from `Src/utils.c:553`.
pub fn mb_charinit() {
    // Rust handles UTF-8 natively
}

/// Port of `wcs_nicechar_sel(wchar_t c, size_t *widthp, char **swidep,
/// int quotable)` from `Src/utils.c:593-705`. Four branches per C:
///   1. c < 0x80 and not printable: control-char escape (\n, \t, ^X, \C-X)
///      — same as `nicechar_sel`.
///   2. c >= 0x80 printable AND PRINTEIGHTBIT set: emit raw UTF-8 bytes.
///   3. c >= 0x80 fits in UTF-8: emit UTF-8 bytes (default-on for MULTIBYTE).
///   4. c >= 0x10000: `\U%.8x` 8-digit hex; c >= 0x100: `\u%.4x` 4-digit hex.
///
/// Param mapping (Rule S1, faithful to C):
/// - `widthp: Option<&mut usize>` ← C `size_t *widthp` (`None` ≡ `NULL`).
///   Set to the display column width of the produced sequence.
/// - `swidep: Option<&mut usize>` ← C `char **swidep` (`None` ≡ `NULL`).
///   Set to the byte index in the returned string where the post-wide
///   meta-prefixed bytes begin (i.e. boundary between display-width
///   characters and trailing Meta+X encoding bytes that don't add to
///   column position).
pub fn wcs_nicechar_sel(
    c: char,
    widthp: Option<&mut usize>,
    swidep: Option<&mut usize>,
    quotable: bool,
) -> String {
    // c:593
    let cv = c as u32;
    // c:616 — `if (!WC_ISPRINT(c) && (c < 0x80 || !isset(PRINTEIGHTBIT)))`.
    // The non-printable + (low-ASCII or PRINTEIGHTBIT-off) branch.
    let print_eightbit = isset(PRINTEIGHTBIT);
    let is_printable = u9_iswprint(c);
    let buf: String;
    if !is_printable && (cv < 0x80 || !print_eightbit) {
        if cv == 0x7f {
            // c:617-624 — DEL: `^?` / `\C-?`
            buf = if quotable {
                "\\C-?".to_string()
            } else {
                "^?".to_string()
            };
            // c:686 widthp = (s - buf) + wcw (wcw for '?' = 1).
            if let Some(wp) = widthp { *wp = buf.chars().count(); }
            if let Some(sp) = swidep { *sp = buf.len(); }
            return buf;
        } else if c == '\n' {
            // c:625-627 — `\n` literal.
            buf = "\\n".to_string();
            if let Some(wp) = widthp { *wp = 2; }
            if let Some(sp) = swidep { *sp = buf.len(); }
            return buf;
        } else if c == '\t' {
            // c:628-630 — `\t` literal.
            buf = "\\t".to_string();
            if let Some(wp) = widthp { *wp = 2; }
            if let Some(sp) = swidep { *sp = buf.len(); }
            return buf;
        } else if cv < 0x20 {
            // c:631-638 — ^X / \C-X for controls (excluding \n, \t).
            let cc = (cv + 0x40) as u8 as char;
            buf = if quotable {
                format!("\\C-{}", cc)
            } else {
                format!("^{}", cc)
            };
            if let Some(wp) = widthp { *wp = buf.chars().count(); }
            if let Some(sp) = swidep { *sp = buf.len(); }
            return buf;
        }
        // c:639-641 — c >= 0x80 non-printable falls through to ret=-1
        // path (hex escape).
    } else if cv < 0x80 {
        // c:644-704 — printable ASCII: emit raw char. wcw=1.
        buf = c.to_string();
        if let Some(wp) = widthp { *wp = 1; }
        if let Some(sp) = swidep { *sp = buf.len(); }
        return buf;
    }
    // c:644-678 — high-bit char: try UTF-8 encode first.
    if u9_iswprint(c) {
        // c:681-693 — printable wide char: emit raw UTF-8.
        buf = c.to_string();
        let wcw = zwcwidth(c) as usize;
        if let Some(wp) = widthp { *wp = wcw; }
        if let Some(sp) = swidep { *sp = buf.len(); }
        return buf;
    }
    // c:656-678 — non-printable wide: hex escape (or fall back to byte
    // nicechar for c < 0x100).
    if cv >= 0x10000 {
        // c:656-659 — `\U%.8x` (10 chars).
        buf = format!("\\U{:08x}", cv);
        if let Some(wp) = widthp { *wp = 10; }
        if let Some(sp) = swidep { *sp = buf.len(); }
        buf
    } else if cv >= 0x100 {
        // c:660-663 — `\u%.4x` (6 chars).
        buf = format!("\\u{:04x}", cv);
        if let Some(wp) = widthp { *wp = 6; }
        if let Some(sp) = swidep { *sp = buf.len(); }
        buf
    } else {
        // c:664-674 — fall back to byte nicechar_sel.
        buf = nicechar_sel(c, quotable);
        // c:670-671 — `*widthp = ztrlen(buf);` (display width respects
        // metafied chars). `ztrlen` counts visible cells.
        if let Some(wp) = widthp { *wp = ztrlen(&buf); }
        // c:672-673 — `*swidep = buf + strlen(buf);` (no trailing meta
        // since nicechar_sel ASCII output).
        if let Some(sp) = swidep { *sp = buf.len(); }
        buf
    }
}

/// Port of `wcs_nicechar(wchar_t c, size_t *widthp, char **swidep)` from `Src/utils.c:709`.
/// C body: `return wcs_nicechar_sel(c, widthp, swidep, 0);`
pub fn wcs_nicechar(
    c: char,
    widthp: Option<&mut usize>,
    swidep: Option<&mut usize>,
) -> String {
    // c:709
    wcs_nicechar_sel(c, widthp, swidep, false)                               // c:711
}

/// Port of `int is_wcs_nicechar(wchar_t c)` from Src/utils.c:720.
///
/// "Return 1 if wcs_nicechar() would reformat this character for
/// display." Mirrors the C condition: non-printable AND (low ASCII
/// OR PRINTEIGHTBIT unset) for control chars; for high bytes, true
/// when ≥0x100 or `is_nicechar` says so.
pub fn is_wcs_nicechar(c: char) -> bool {
    // c:720
    let cv = c as u32;
    let printable = !c.is_control() && cv >= 0x20;
    let print_eight = isset(PRINTEIGHTBIT); // c:722
    if !printable && (cv < 0x80 || !print_eight) {
        if cv == 0x7f || c == '\n' || c == '\t' || cv < 0x20 {
            // c:734
            return true;
        }
        if cv >= 0x80 {
            // c:734
            return cv >= 0x100 || is_nicechar(c); // c:734
        }
    }
    false // c:734
}

/// Get wide character width (from utils.c zwcwidth)
/// Port of `int zwcwidth(wint_t wc)` from `Src/utils.c:734`.
///
/// C body (c:734-745):
/// ```c
/// int wcw;
/// /* assume a single-byte character if not valid */
/// if (wc == WEOF || unset(MULTIBYTE))                  // c:738
///     return 1;
/// wcw = WCWIDTH(wc);                                   // c:740
/// /* if not printable, assume width 1 */
/// if (wcw < 0)                                         // c:742
///     return 1;
/// return wcw;                                          // c:744
/// ```
///
/// The previous Rust port skipped the `unset(MULTIBYTE)` early-
/// return (c:738). When the MULTIBYTE option is OFF (set via
/// `setopt nomultibyte` / `set +o multibyte`), C zsh treats every
/// codepoint as a single-byte char and returns width 1 unconditionally.
/// Without the option check, the Rust port would still report the
/// Unicode-width-table answer (2 for CJK, 0 for combining marks)
/// in single-byte mode — diverging from prompt/refresh layout that
/// relies on the option as the source of truth.
pub fn zwcwidth(wc: char) -> usize {
    // c:734
    // c:738 — `if (wc == WEOF || unset(MULTIBYTE)) return 1;`. WEOF
    // path is Rust-impossible (char is always a valid scalar). The
    // MULTIBYTE option gate maps to the canonical option register.
    if !isset(MULTIBYTE) {
        // c:738
        return 1; // c:739
    }
    // c:740-744 — WCWIDTH(wc); negative result → width 1.
    unicode_width::UnicodeWidthChar::width(wc).unwrap_or(1) // c:740-744
}

/// Port of `pathprog(char *prog, char **namep)` from `Src/utils.c:760-786`.
/// ```c
/// for (pp = path; *pp; pp++) {
///     sprintf(buf, "%s/%s", *pp, prog);
///     funmeta = unmeta(buf);
///     if (access(funmeta, F_OK) == 0 && stat(funmeta, &st) >= 0 &&
///         !S_ISDIR(st.st_mode)) {
///         return funmeta;
///     }
/// }
/// return NULL;
/// ```
/// C checks: (1) F_OK = exists, (2) stat succeeds, (3) NOT a directory.
/// NO executable-bit check. Used by autoload / `which` paths that
/// need to find any file in PATH, not just executables.
///
/// Previously the Rust port added a `mode & 0o111 != 0` executable
/// check — divergent, made every `pathprog` lookup miss non-executable
/// files (e.g. autoload-function plaintext scripts that don't have
/// +x set).
/// WARNING: param names don't match C — Rust=(prog) vs C=(prog, namep)
pub fn pathprog(prog: &str) -> Option<PathBuf> {
    // c:760
    // The early-return on `prog` containing '/' is a Rust-port
    // convenience NOT in C's pathprog. C unconditionally walks $PATH
    // and prefixes each entry. The convenience is harmless since
    // C's caller (`findcmd`) handles slashes separately before
    // calling pathprog.
    if prog.contains('/') {
        let p = PathBuf::from(prog);
        return if p.exists() { Some(p) } else { None };
    }
    if let Some(path_var) = getsparam("PATH") {
        for dir in path_var.split(':') {
            // c:773
            let full_path = PathBuf::from(dir).join(prog);
            // c:776 — `funmeta = unmeta(buf)`. The previous Rust port
            // passed the raw composed path to `fs::metadata`, missing
            // the unmeta step. Paths containing Meta-encoded bytes
            // (from PATH entries or prog name with metafy lead bytes)
            // would silently miss valid executables.
            let unmeta_path = unmeta(full_path.to_str().unwrap_or("")); // c:776 unmeta(buf)
                                                                                              // c:777-779 — `access(F_OK) == 0 && stat >= 0 && !S_ISDIR`.
                                                                                              // is_file() folds existence + stat + not-dir into one.
            if let Ok(meta) = fs::metadata(&unmeta_path) {
                if meta.is_file() {
                    // Return the unmeta'd path since that's what
                    // C does (funmeta is the returned value).
                    return Some(PathBuf::from(unmeta_path));
                }
            }
        }
    }
    None
}

/// Port of `findpwd(char *s)` from `Src/utils.c:792`.
///
/// ```c
/// char *findpwd(char *s)
/// {
///     char *t;
///     if (*s == '/')
///         return xsymlink(s, 0);
///     s = tricat((pwd[1]) ? pwd : "", "/", s);
///     t = xsymlink(s, 0);
///     zsfree(s);
///     return t;
/// }
/// ```
///
/// Resolve `s` to its canonical form. Absolute paths route through
/// `xsymlink` directly; relative paths get prefixed with the
/// current `pwd` first.
///
/// Signature note: C takes `s: char *`. The previous Rust port had
/// no parameter (returning the cwd) — completely wrong. New port
/// matches C: takes `&str`, returns `Option<String>` (xsymlink can
/// return NULL).
// get a symlink-free pathname for s relative to PWD                        // c:792
pub fn findpwd(s: &str) -> Option<String> {
    // c:792
    if s.starts_with('/') {
        // c:792
        return xsymlink(s); // c:797
    }
    // C: tricat((pwd[1]) ? pwd : "", "/", s) — uses the global
    // `pwd` (logical cwd; differs from realpath when chasing
    // symlinks is disabled). The Rust port reads `$PWD` since
    // shell-set `PWD` mirrors C's `pwd` global; falls back to
    // `getcwd()` when unset.
    let pwd = getsparam("PWD")
        .or_else(|| {
            std::env::current_dir()
                .ok()
                .map(|p| p.to_string_lossy().into_owned())
        })
        .unwrap_or_default(); // c:798
    let prefix: &str = if pwd.len() > 1 { &pwd } else { "" }; // c:798 pwd[1]
    let combined = format!("{}/{}", prefix, s); // c:798
    xsymlink(&combined) // c:799
}

/// Validate an inherited `$PWD` exactly like zsh's ispwd() at
/// src/zsh/Src/utils.c:809: PWD must be absolute, must stat to the
/// same dev+inode as ".", and must contain no `.` or `..` components.
/// When this returns false, callers should fall back to `getcwd()`.
pub(crate) fn ispwd(pwd: &str) -> bool {
    if !pwd.starts_with('/') {
        return false;
    }
    let pwd_meta = match fs::metadata(pwd) {
        Ok(m) => m,
        Err(_) => return false,
    };
    let dot_meta = match fs::metadata(".") {
        Ok(m) => m,
        Err(_) => return false,
    };
    if pwd_meta.dev() != dot_meta.dev() || pwd_meta.ino() != dot_meta.ino() {
        return false;
    }
    // Reject any component that is exactly `.` or `..` — the same loop
    // zsh runs after the dev/ino check.
    for comp in pwd.split('/') {
        if comp == "." || comp == ".." {
            return false;
        }
    }
    true
}

// ---------------------------------------------------------------------------
// Missing utility functions ported from utils.c
// ---------------------------------------------------------------------------

/// Split path into components (from utils.c slashsplit).
///
/// Port of `static char **slashsplit(char *s)` from `Src/utils.c:837`.
///
/// C body (c:837-863):
/// ```c
/// if (!*s) return zshcalloc(...);            // c:842 — empty input → empty
/// for (t = s, t0 = 0; *t; t++)               // c:845 — count slashes
///     if (*t == '/') t0++;
/// q = r = zalloc(sizeof(char*) * (t0 + 2));
/// while ((t = strchr(s, '/'))) {             // c:850
///     *q++ = ztrduppfx(s, t - s);            // c:851 — emit prefix
///     while (*t == '/') t++;                 // c:852-853 — collapse runs
///     if (!*t) { *q = NULL; return r; }      // c:854-857 — trailing `/` ends
///     s = t;
/// }
/// *q++ = ztrdup(s);                          // c:860 — final tail
/// ```
///
/// Three behaviors that the previous `split('/').filter(non_empty)`
/// Rust port got WRONG:
///   1. **Leading `/` keeps an empty segment** (c:851 with `t == s`).
///      C: `slashsplit("/usr")` → `["", "usr"]`; previous Rust dropped
///      the empty, returning `["usr"]`. Caller `xsymlinks` (c:879)
///      iterates the result building `xbuf` byte-by-byte — the
///      empty leading segment is how it knows to start from `/`.
///      Without it, absolute-path resolution silently became relative.
///   2. **Consecutive slashes collapse** (c:852-853 inner while-loop).
///      C: `"a//b"` → `["a", "b"]` (drop empty between). Filter-on-
///      empty Rust gets this right by coincidence.
///   3. **Trailing `/` drops** (c:854-857). C: `"a/b/"` → `["a", "b"]`.
///      Filter-on-empty Rust gets this right by coincidence.
///
/// Pin: the leading-empty-segment behavior IS the C contract, not
/// an oversight to filter out. Matches `xsymlinks` and any future
/// path-walker port that reads slashsplit output.
pub fn slashsplit(s: &str) -> Vec<String> {
    // c:837
    if s.is_empty() {
        // c:842
        return Vec::new();
    }
    let mut result = Vec::new();
    let mut rest = s;
    // c:850 — `while ((t = strchr(s, '/')))`.
    while let Some(pos) = rest.find('/') {
        result.push(rest[..pos].to_string()); // c:851 ztrduppfx
        rest = &rest[pos..];
        // c:852-853 — `while (*t == '/') t++;` collapse run.
        while let Some(rest_after) = rest.strip_prefix('/') {
            rest = rest_after;
        }
        // c:854-857 — if walked off end, return without emitting tail.
        if rest.is_empty() {
            return result;
        }
    }
    // c:860 — `*q++ = ztrdup(s);` emit final tail.
    result.push(rest.to_string());
    result
}

/// Port of `static int xsymlinks(char *s)` from `Src/utils.c:872`.
///
/// Expands `.` and `..` components AND follows ONE LEVEL of
/// symlinks (per C source comment at c:865-867: "expands .. or .
/// expressions and one level of symlinks"). Used by the `:A`/`:P`
/// modifier paths via `subst.rs:6896`.
///
/// The previous Rust port did `.`/`..` normalization ONLY, with a
/// stale doc-comment claiming "Does NOT follow symlinks (matches
/// the `physical = 0` mode in C)." That claim is FALSE: C
/// `xsymlinks` IS the symlink-following form (it calls `readlink(2)`
/// at c:908). The `physical = 0` no-symlink path is a different
/// fn (`xsymlink` at utils.c:971, without the trailing `s`). Result:
/// `:A` modifier output never resolved actual symlinks — `:A` on
/// `/tmp/link -> /usr` returned `/tmp/link` instead of `/usr`.
///
/// Rewrite: walk components; for each non-`.`/`..` component, try
/// `readlink` on the accumulated path. If it succeeds, the target
/// replaces (or prepends to) the accumulator; otherwise the
/// component appends as-is. C handles re-rooting (absolute symlink
/// target replaces buf) and component-level concat.
pub fn xsymlinks(s: &str) -> io::Result<String> {
    // c:872
    if s.is_empty() {
        return Ok(String::new());
    }

    let path = if !s.starts_with('/') {
        // c:879 — slashsplit
        let cwd = std::env::current_dir()?;
        format!("{}/{}", cwd.display(), s)
    } else {
        s.to_string()
    };

    let components: Vec<&str> = path.split('/').collect();
    // c:877 — `xbuflen = strlen(xbuf)`. Start with empty xbuf for
    // absolute paths (the leading "" from slashsplit handles that).
    let mut xbuf = String::new();
    for comp in components {
        match comp {
            "" | "." => continue, // c:881
            ".." => {
                // c:883
                if xbuf == "/" || xbuf.is_empty() {
                    // c:886-889
                    continue;
                }
                // c:891-895 — walk back one `/`-delimited component.
                if let Some(pos) = xbuf.rfind('/') {
                    xbuf.truncate(pos);
                }
            }
            c => {
                // c:905-907 — `memcpy xbuf2, xbuf` then append `/comp`.
                let candidate = format!("{}/{}", xbuf, c); // c:907
                                                           // c:908 — `readlink(unmeta(xbuf2), xbuf3, PATH_MAX)`.
                #[cfg(unix)]
                {
                    match fs::read_link(&candidate) {
                        // c:908
                        Ok(target) => {
                            // c:918-933 — successful readlink: target is
                            // either absolute (replaces xbuf wholesale) or
                            // relative (appends with `/`).
                            let t = target.to_string_lossy().into_owned();
                            if t.starts_with('/') {
                                // c:927
                                xbuf = t; // c:928
                            } else {
                                xbuf = format!("{}/{}", xbuf, t); // c:930-931
                            }
                            continue;
                        }
                        Err(_) => {
                            // c:909-916 — readlink failed (not a symlink),
                            // append the component verbatim.
                            xbuf = candidate; // c:910-912
                        }
                    }
                }
                #[cfg(not(unix))]
                {
                    xbuf = candidate;
                }
            }
        }
    }

    if xbuf.is_empty() {
        Ok(if path.starts_with('/') {
            "/".to_string()
        } else {
            ".".to_string()
        })
    } else {
        Ok(xbuf)
    }
}

/// Port of `xsymlink(char *s, int heap)` from `Src/utils.c:971`.
///
/// ```c
/// mod_export char *
/// xsymlink(char *s, int heap)
/// {
///     if (*s != '/')
///         return NULL;
///     *xbuf = '\0';
///     if (!chrealpath(&s, 'P', heap)) {
///         zwarn("path expansion failed, using root directory");
///         return heap ? dupstring("/") : ztrdup("/");
///     }
///     return s;
/// }
/// ```
///
/// Returns `Some(resolved)` on success, `None` if the path isn't
/// absolute (C: returns NULL). On resolve failure emits the same
/// "path expansion failed, using root directory" warning and
/// returns `Some("/")`.
///
/// C body (c:971-980):
/// ```c
/// char *xsymlink(char *s, int heap)
/// {
///     if (*s != '/') return NULL;
///     *xbuf = '\0';
///     if (!chrealpath(&s, 'P', heap)) {
///         zwarn("path expansion failed, using root directory");
///         return heap ? dupstring("/") : ztrdup("/");
///     }
///     return s;
/// }
/// ```
///
/// Previous Rust port was a FAKE — called `std::fs::canonicalize`
/// directly with the rationale "same semantics for symlink-
/// resolution". That bypassed the canonical `chrealpath` port
/// (hist.rs:2311) which handles the partial-prefix walk + xbuf
/// state that `fs::canonicalize` doesn't replicate. Re-port now
/// matches C line-by-line.
///
/// Rust signature drift: `heap` param dropped — Rust strings always
/// live on the heap; the C `heap` flag toggles between zhalloc
/// (heap arena) and ztrdup (process-wide) allocation which has no
/// distinction in Rust. Always defaults to `false` (ztrdup arm) at
/// the chrealpath call.
pub fn xsymlink(path: &str) -> Option<String> {
    // c:971
    // c:973 — `if (*s != '/') return NULL;`
    if !path.starts_with('/') {
        // c:973
        return None;
    }
    // c:974 — `*xbuf = '\0';`  Reset the xbuf cursor; no-op in Rust
    // (xbuf is an internal chrealpath buffer not exposed at this level).
    // c:975 — `if (!chrealpath(&s, 'P', heap))`
    match chrealpath(path, b'P', false) {
        // c:975
        Some(r) => Some(r), // c:979 return s
        None => {
            // c:976 failure arm
            // c:977 — `zwarn("path expansion failed, using root directory");`
            zwarn("path expansion failed, using root directory"); // c:977
                                                                  // c:978 — `return heap ? dupstring("/") : ztrdup("/");`
            Some("/".to_string()) // c:978
        }
    }
}

/// Port of `void print_if_link(char *s, int all)` from Src/utils.c:985.
///
/// "Print arrow + symlink target(s) iff `s` is an absolute path
/// pointing through symlinks." When `all` is set, follows the
/// chain (`xsymlinks` loop, c:992); otherwise emits just the final
/// realpath if it differs from the input. Always relative to
/// stdout. The Rust port writes via `print!` to mirror C's
/// `printf`/`zputs(stdout)` calls.
pub fn print_if_link(s: &str, all: bool) {
    // c:985
    if !s.starts_with('/') {
        // c:987
        return;
    }
    if all {
        // c:988
        let mut start = s.to_string();
        loop {
            // c:992
            match xsymlinks(&start) {
                Ok(target) if !target.is_empty() && target != start => {
                    print!(" -> "); // c:994
                    print!("{}", if target.is_empty() { "/" } else { &target }); // c:995
                    start = target; // c:998-999
                }
                _ => break, // c:1002
            }
        }
    } else {
        // c:1006
        // c:1007-1011 — HAVE_MEMCCPY arm: copy s into a PATH_MAX-1 buffer
        // and DPUTS1 if the input overflows. Rust's String has no fixed
        // PATH_MAX limit but the C parity-check still fires when a
        // pathological caller passes a path that wouldn't fit C's
        // s_at_entry[PATH_MAX+1] buffer.
        DPUTS1!(
            // c:1009
            s.len() >= libc::PATH_MAX as usize, // c:1008 memccpy returns NULL
            "path longer than PATH_MAX: {}",
            s // c:1009
        );
        let s_at_entry = s.to_string(); // c:1013
                                        // c:1015 — `if (chrealpath(&s, 'P', 0) && strcmp(s, s_at_entry))`
                                        // The previous Rust port called std::fs::canonicalize directly —
                                        // a fake that bypassed the canonical chrealpath port (hist.rs:2311).
        let mut resolved = s.to_string(); // c:1015 &s in/out
        if let Some(r) = chrealpath(&resolved, b'P', false) {
            // c:1015
            resolved = r;
            if resolved != s_at_entry {
                // c:1015 strcmp(s, s_at_entry)
                print!(" -> "); // c:1016
                print!("{}", if resolved.is_empty() { "/" } else { &resolved });
                // c:1017
            }
        }
    }
    let _ = io::stdout().flush();
}

/// Port of `void fprintdir(char *s, FILE *f)` from Src/utils.c:1031.
///
/// "print a directory" — abbreviates `s` via `finddir` (so a path
/// matching `$HOME` or a `nameddirtab` entry shows as `~name/...`)
/// and returns the rendering. The C source writes to a FILE*; Rust
/// returns the string for the caller to print.
pub fn fprintdir(s: &str) -> String {
    // c:1031
    match finddir(s) {
        // c:1031
        None => unmeta(s),          // c:1036
        Some(rendered) => rendered, // c:1038-1040
    }
}

/// Port of `char *substnamedir(char *s)` from Src/utils.c:1053.
///
/// C body (c:1053-1061):
/// ```c
/// Nameddir d = finddir(s);
/// if (!d)
///     return quotestring(s, QT_BACKSLASH);
/// return zhtricat("~", d->node.nam, quotestring(s + strlen(d->dir),
///                                                QT_BACKSLASH));
/// ```
///
/// The previous Rust port was a FAKE — it called `finddir(s)` (Rust
/// signature returns `Option<String>` of the already-formatted
/// `~name/rest`) and returned that string unchanged in the Some-arm,
/// missing the `quotestring(..., QT_BACKSLASH)` C applies to the
/// residue. C's `finddir` returns a `Nameddir` pointer; the
/// `~name` + quoted-residue join lives here in `substnamedir`.
///
/// This re-port duplicates the HOME-first + nameddirtab-scan logic
/// (finddir_scan at c:1106) so it can take the (name, dir_prefix)
/// split BEFORE pre-joining, then apply quotestring to the residue
/// per c:1059.
pub fn substnamedir(s: &str) -> String {
    // c:1053
    // C `finddir` at c:1127 checks $HOME first (longest implicit
    // named dir), then scans nameddirtab. Duplicate that ordering
    // here without the pre-format, so we can apply quotestring on
    // just the residue.
    let home = getsparam("HOME").unwrap_or_default(); // c:1133
    if !home.is_empty() && home.len() > 1 && s.starts_with(&home) {
        // c:1138-1141
        let rest = &s[home.len()..];
        if rest.is_empty() || rest.starts_with('/') {
            // C: zhtricat("~", "", quotestring(rest, QT_BACKSLASH))
            // — HOME is the implicit homenode (no name).
            return format!("~{}", quotestring(rest, QT_BACKSLASH)); // c:1059
        }
    }
    // c:1106 finddir_scan — longest-prefix walk over nameddirtab.
    if let Some((name, rest)) = finddir_scan(s) {
        // c:1106
        // c:1059 — `zhtricat("~", d->node.nam, quotestring(s + strlen(d->dir), QT_BACKSLASH))`
        return format!("~{}{}", name, quotestring(&rest, QT_BACKSLASH)); // c:1059
    }
    quotestring(s, QT_BACKSLASH) // c:1058
}

// ===========================================================
// Direct ports of utility entries from Src/utils.c.
// ===========================================================

/// Cached current-user lookup.
/// Port of `get_username()` from Src/utils.c:1075 — `getpwuid(3)`
/// against the current real uid, with the result cached so a
/// re-call after setuid sees the new identity. The C source uses
/// a `cached_uid`+`cached_username` pair guarded by a uid match;
/// the Rust port uses an `OnceLock` keyed on uid for the same
/// invalidate-on-uid-change behaviour.
pub fn get_username() -> String {
    static CACHE: Mutex<Option<(u32, String)>> = Mutex::new(None);

    let current_uid = unsafe { libc::getuid() };
    let mut guard = CACHE.lock().unwrap();
    if let Some((uid, name)) = &*guard {
        if *uid == current_uid {
            return name.clone();
        }
    }
    let name = unsafe {
        let pw = libc::getpwuid(current_uid);
        if pw.is_null() {
            String::new() // c:1088 — `ztrdup("")` fallback
        } else {
            // c:1086 — `cached_username = ztrdup_metafy(pswd->pw_name);`.
            // `ztrdup_metafy` calls `metafy((char *)s, -1, META_DUP)`
            // (utils.c:4929). The previous Rust port returned the raw
            // pw_name verbatim — fine for ASCII usernames, but high-bit
            // bytes (e.g. an `émile` username on some systems) would
            // surface to callers un-metafied. zsh's downstream pipeline
            // (param expansion, prompt rendering) assumes paramtab
            // entries are metafied; an un-metafied high-bit byte breaks
            // the Meta-escape contract.
            let raw = std::ffi::CStr::from_ptr((*pw).pw_name)
                .to_string_lossy()
                .into_owned();
            metafy(&raw) // c:1086 ztrdup_metafy
        }
    };
    *guard = Some((current_uid, name.clone()));
    name
}

/// Port of `finddir_scan(HashNode hn, UNUSED(int flags))` from Src/utils.c:1106 — ScanFunc the
/// C source registers with `scanhashtable(nameddirtab, …)` to pick
/// the longest-prefix entry. Reads the typed `nameddir` entries
/// from `nameddirtab()`.
/// WARNING: param names don't match C — Rust=(path) vs C=(hn, flags)
pub fn finddir_scan(path: &str) -> Option<(String, String)> {
    // c:1106
    let table = nameddirtab().lock().ok()?;
    let mut best: Option<(String, String, usize)> = None;
    for (name, nd) in table.iter() {
        if path.starts_with(nd.dir.as_str()) {
            let len = nd.dir.len();
            let rest = &path[len..];
            if (rest.is_empty() || rest.starts_with('/'))
                && best.as_ref().map_or(true, |b| len > b.2)
            {
                best = Some((name.clone(), rest.to_string(), len));
            }
        }
    }
    best.map(|(n, r, _)| (n, r))
}

/// Port of `Nameddir finddir(char *s)` from Src/utils.c:1127.
///
/// "See if a path has a named directory as its prefix." Compares
/// `s` against `$HOME` first (longest implicit named dir), then
/// scans the global `nameddirtab`, then falls back to
/// `subst_string_by_hook("zsh_directory_name", "d", s)`.
///
/// Rust signature returns `Option<String>` (the abbreviated path
/// `~name/rest`) instead of the C `Nameddir` pointer.
pub fn finddir(path: &str) -> Option<String> {
    // c:1127
    // c:1138 — `homenode.dir = home ? home : "";`. Reads the C global
    // `char *home` (params.c:91) DIRECTLY — not via paramtab. zshrs
    // ports the global as `params::home_lock()` accessed by
    // `homegetfn` (which ignores its &param arg per
    // `c:5109 UNUSED(Param pm)`). Pass a default param so the read
    // works even before paramtab["HOME"] has been hydrated (during
    // early init / unit-test environments).
    let _default_pm = crate::ported::zsh_h::param::default();
    let home = crate::ported::params::homegetfn(&_default_pm); // c:1138 home
    if !home.is_empty() && home.len() > 1 && path.starts_with(&home) {
        // c:1138-1141
        let rest = &path[home.len()..];
        if rest.is_empty() || rest.starts_with('/') {
            return Some(format!("~{}", rest));
        }
    }
    if let Some((name, rest)) = finddir_scan(path) {
        // c:1167
        return Some(format!("~{}{}", name, rest));
    }
    // c:1169 — zsh_directory_name hook — returns ["name", "len"]
    if let Some(reply) = subst_string_by_hook("zsh_directory_name", Some("d"), path) {
        if reply.len() >= 2 {
            // c:1170
            if let Ok(len) = reply[1].parse::<usize>() {
                if len <= path.len() {
                    let prefix = &path[..len];
                    let _ = prefix;
                    return Some(format!("~[{}]{}", reply[0], &path[len..]));
                }
            }
        }
    }
    None // c:1187
}

/// Port of `void adduserdir(char *s, char *t, int flags, int always)`
/// from Src/utils.c:1187.
///
/// Adds (or removes when `t` is empty / non-absolute) an entry in
/// the global `nameddirtab`. ND_USERNAME entries from `getpwnam`
/// don't override explicit assignments. AUTONAMEDIRS gating, the
/// trailing-slash trim, and PWD/OLDPWD ND_NOABBREV stamp are all
/// preserved. Routes through `crate::ported::hashnameddir`.
pub fn adduserdir(name: &str, dir: &str, flags: i32, always: bool) {
    // c:1187

    if !interact() {
        return;
    } // c:1193
    if let Ok(t) = nameddirtab().lock() {
        if (flags & ND_USERNAME) != 0 && t.contains_key(name) {
            // c:1199
            return;
        }
        if !always && !isset(AUTONAMEDIRS) && !t.contains_key(name) {
            // c:1207
            return;
        }
    }
    // c:1211 — `if (!t || *t != '/' || strlen(t) >= PATH_MAX)`. C
    // rejects paths >= PATH_MAX as too long (would overflow path-
    // expansion buffers downstream). PATH_MAX is platform-dependent
    // (4096 on Linux, 1024 on macOS); libc::PATH_MAX exposes it.
    if dir.is_empty() || !dir.starts_with('/') || dir.len() >= libc::PATH_MAX as usize
    // c:1211 strlen(t) >= PATH_MAX
    {
        let _ = removenameddirnode(name); // c:1214
        return;
    }
    let mut trimmed = dir.trim_end_matches('/').to_string(); // c:1224-1226
    if trimmed.is_empty() {
        trimmed = dir.to_string(); // c:1227-1233
    }
    let final_flags = if name == "PWD" || name == "OLDPWD" {
        // c:1237
        flags | ND_NOABBREV
    } else {
        flags
    };
    // c:1239 — `nd = (Nameddir) zshcalloc(sizeof *nd); nd->node.flags = …;
    //           nd->dir = ztrdup(t); addnode(nameddirtab, ztrdup(s), nd);`
    let nd = nameddir {
        node: hashnode {
            next: None,
            nam: name.to_string(),
            flags: final_flags,
        },
        dir: trimmed,
        diff: 0,
    };
    crate::ported::hashnameddir::addnameddirnode(name, nd);
}

/// Port of `char *getnameddir(char *name)` from Src/utils.c:1247.
///
/// Looks up `name` in `nameddirtab`; if absent, checks for a
/// scalar parameter whose value starts with `/` and registers it
/// via `adduserdir`; finally falls back to `getpwnam(name)` for
/// `~user`-style lookups when USE_GETPWNAM is enabled.
pub fn getnameddir(name: &str) -> Option<String> {
    // c:1247
    if let Ok(t) = nameddirtab().lock() {
        if let Some(nd) = t.get(name) {
            // c:1254
            return Some(nd.dir.clone());
        }
    }
    // c:1260 — `if ((s = getsparam(name)) && *s == '/')`. paramtab read.
    if let Some(s) = getsparam(name) {
        if s.starts_with('/') {
            adduserdir(name, &s, 0, true); // c:1264
            return Some(s);
        }
    }
    #[cfg(unix)]
    {
        // c:1268 — getpwnam fallback.
        let cn = CString::new(name).ok()?;
        let pw = unsafe { libc::getpwnam(cn.as_ptr()) };
        if !pw.is_null() {
            let raw_dir = unsafe {
                std::ffi::CStr::from_ptr((*pw).pw_dir)
                    .to_string_lossy()
                    .into_owned()
            };
            // c:1273-1274 — `isset(CHASELINKS) ? xsymlink(pw->pw_dir, 0)
            // : ztrdup(pw->pw_dir);`. Resolve symlinks when the option
            // is set. Previously omitted in the Rust port — silently
            // returned the raw passwd-db dir even when the user had
            // `setopt chaselinks`.
            let dir = if isset(CHASELINKS) {
                xsymlink(&raw_dir).unwrap_or(raw_dir)
            } else {
                raw_dir
            };
            // c:1276 — `adduserdir(name, dir, ND_USERNAME, 1);`
            // Cache the lookup so subsequent `~user` expansions hit the
            // nameddirtab fast-path at c:1254 instead of round-tripping
            // through getpwnam every time.
            adduserdir(name, &dir, ND_USERNAME, true); // c:1276
            return Some(dir);
        }
    }
    None
}

/// Compare directory paths (from utils.c dircmp)
/// Port of `dircmp(char *s, char *t)` from `Src/utils.c:1296`.
pub fn dircmp(s: &str, t: &str) -> bool {
    let s = s.trim_end_matches('/');
    let t = t.trim_end_matches('/');
    s == t
}

// Add a function to the list of pre-prompt functions.                     // c:1332
/// Register a callback to run before each prompt.
/// Port of `addprepromptfn(voidvoidfnptr_t func)` from Src/utils.c:1319.
pub fn addprepromptfn(func: fn()) {
    // c:1319
    PREPROMPT_FNS.lock().unwrap().push(func);
}

// Remove a function from the list of pre-prompt functions.                // c:1332
/// Remove a previously-registered pre-prompt callback.
/// Port of `delprepromptfn(voidvoidfnptr_t func)` from Src/utils.c:1332.
pub fn delprepromptfn(func: fn()) {
    // c:1332
    let mut list = PREPROMPT_FNS.lock().unwrap();
    if let Some(pos) = list.iter().position(|f| *f as usize == func as usize) {
        list.remove(pos);
    }
}

// Add a function to the list of timed functions.                           // c:1367
/// Port of `void addtimedfn(voidvoidfnptr_t func, time_t when)` from
/// `Src/utils.c:1371`. Faithful walk of the timedfns LinkList:
/// allocate a `Timedfn`, lazy-init the list when empty, otherwise scan
/// from `firstnode` and insert BEFORE the first node whose `when` is
/// greater than ours. The standard linklist API only inserts AFTER a
/// node, so the C loop carries `ln` as the previous node and inserts
/// before `next` once `when < next->when`. Note: zsh's `time_t` is
/// signed and historically negative-`when` was supported, so we keep
/// `i64`.
pub fn addtimedfn(func: fn(), when: i64) {
    // c:1371
    let mut list = TIMED_FNS.lock().unwrap(); // c:1365 timedfns
    let tfdat: (i64, fn()) = (when, func); // c:1373-1375 Timedfn tfdat
    if list.is_empty() {
        // c:1377 !timedfns
        list.push(tfdat); // c:1378-1379 znewlinklist + zaddlinknode
        return;
    }
    if list.is_empty() {
        // c:1394 !ln (firstnode of empty list)
        list.push(tfdat); // c:1395 zaddlinknode
        return; // c:1396
    }
    let mut idx: usize = 0; // c:1381 LinkNode ln = firstnode(timedfns)
    loop {
        // c:1398 for(;;)
        let next = idx + 1; // c:1400 LinkNode next = nextnode(ln)
        if next >= list.len() {
            // c:1401 !next
            list.push(tfdat); // c:1402 zaddlinknode
            return; // c:1403
        }
        let tfdat2_when = list[next].0; // c:1405 tfdat2 = getdata(next)
        if when < tfdat2_when {
            // c:1406 when < tfdat2->when
            list.insert(next, tfdat); // c:1407 zinsertlinknode(timedfns, ln, tfdat)
            return; // c:1408
        }
        idx = next; // c:1410 ln = next
    }
}

/// Remove a registered timed function (first occurrence only).
/// Port of `deltimedfn(voidvoidfnptr_t func)` from Src/utils.c:1430.
pub fn deltimedfn(func: fn()) {
    // c:1430
    let mut list = TIMED_FNS.lock().unwrap();
    if let Some(pos) = list.iter().position(|(_, f)| *f as usize == func as usize) {
        list.remove(pos);
    }
}

/// Invoke a hook function by name plus any `<name>_functions` array.
/// Port of `callhookfunc(char *name, LinkList lnklst, int arrayp, int *retval)` from Src/utils.c:1469. Returns 0 if at
/// least one hook ran, 1 otherwise — the C source uses the same
/// stat semantics so the prompt machinery can detect "did periodic
/// fire". Hook dispatch goes through the executor singleton (which
/// owns the function table); we look up `name` and then walk the
/// `<name>_functions` array exactly as the C source does at
/// Src/utils.c:1469.
/// Direct port of
/// `int callhookfunc(char *name, LinkList lnklst, int arrayp, int *retval)`
/// at `Src/utils.c:1469`. C signature mapping:
///   - `char *name`     → `name: &str`
///   - `LinkList lnklst` → `lnklst: Option<&[String]>` (Rust-shape
///     stand-in for the C-shape `LinkList` — both are an ordered list
///     of meta-fied strings; the LinkList port itself diverges)
///   - `int arrayp`     → `arrayp: i32`
///   - `int *retval`    → `retval: *mut i32` (out-param; NULL is fine
///     when the caller doesn't need the doshfunc return value)
///
/// Returns `stat` — 0 if at least one shfunc fired, 1 otherwise
/// (matches `c:1495 stat = 0` after every dispatch). When `retval` is
/// non-null, `*retval` is set to the most recent `doshfunc`-returned
/// status, mirroring the C body's `*retval = ret` semantics.
pub fn callhookfunc(name: &str, lnklst: Option<&[String]>, arrayp: i32, retval: *mut i32) -> i32 {
    let mut stat: i32 = 1; // c:1475
    let mut ret: i32 = 0; // c:1475

    // Build the args vec mirroring C's doshfunc input — argv[0] is the
    // function name, $1..$N are the carried args.
    let mk_args = |fname: &str| -> Vec<String> {
        let mut v: Vec<String> = vec![fname.to_string()];
        if let Some(extra) = lnklst {
            v.extend_from_slice(extra);
        }
        v
    };

    // c:1495 — `if ((shfunc = getshfunc(name))) { doshfunc(...); stat = 0; }`
    let shf_clone: Option<crate::ported::zsh_h::shfunc> = shfunctab_lock()
        .read()
        .ok()
        .and_then(|t| t.get(name).cloned());
    if let Some(mut shf) = shf_clone {
        let mut args = mk_args(name);
        crate::ported::exec::execshfunc(&mut shf, &mut args); // c:1503 doshfunc
        ret = crate::ported::builtin::LASTVAL.load(std::sync::atomic::Ordering::Relaxed);
        stat = 0; // c:1504
    }

    if arrayp != 0 {
        // c:1507-1525 — fire every `${name}_functions` hook in order.
        let arr_name = format!("{}_functions", name); // c:1511
        let arr = crate::ported::params::paramtab()
            .read()
            .ok()
            .and_then(|t| t.get(&arr_name).and_then(|p| p.u_arr.clone()))
            .unwrap_or_default(); // c:1512 getaparam
        for fn_name in arr {
            // c:1514
            let shf_clone: Option<crate::ported::zsh_h::shfunc> = shfunctab_lock()
                .read()
                .ok()
                .and_then(|t| t.get(&fn_name).cloned());
            if let Some(mut shf) = shf_clone {
                // c:1518
                let mut args = mk_args(&fn_name);
                crate::ported::exec::execshfunc(&mut shf, &mut args); // c:1519
                ret = crate::ported::builtin::LASTVAL.load(std::sync::atomic::Ordering::Relaxed);
                stat = 0; // c:1520
            }
        }
    }

    // c:1528 — `if (retval) *retval = ret;`
    if !retval.is_null() {
        unsafe {
            *retval = ret;
        }
    }
    let _ = ret;

    stat // c:1530
}

// do pre-prompt stuff                                                      // c:1530
/// Run pre-prompt machinery: precmd, periodic, prepromptfns.
/// Port of `preprompt()` from Src/utils.c:1530. Rust port skips
/// the `PROMPT_SP` heuristic + mailcheck (those need terminal +
/// MAIL state plumbing not yet present); fires the `precmd` hook
/// + `precmd_functions` array, the `periodic` hook on its
/// PERIOD-second cadence, and walks the prepromptfns registry.
pub fn preprompt() {
    // c:1532 `static time_t lastperiodic;` — periodic-hook last-fire timestamp.
    static LAST_PERIODIC: AtomicI64 = AtomicI64::new(0);
    // c:1447 `static time_t lastmailcheck;` — mailcheck last-fire timestamp.
    static LAST_MAILCHECK: AtomicI64 = AtomicI64::new(0);

    // c:1535-1536 — `zlong period = getiparam("PERIOD"); zlong mailcheck
    //                = getiparam("MAILCHECK");`
    let period = crate::ported::params::getiparam("PERIOD");
    let mailcheck = crate::ported::params::getiparam("MAILCHECK");

    // c:1538-1543 — `winch_unblock(); ... winch_block();` — let any
    // pending SIGWINCH fire so the prompt picks up the latest column
    // count, then re-block to prevent the resize-handler from
    // interrupting prompt rendering mid-paint.
    crate::ported::signals_h::winch_unblock();
    crate::ported::signals_h::winch_block();

    // c:1545-1567 — PROMPT_SP heuristic (move prompt to new line on
    // dangling output). Deferred: needs zterm_columns + hasxn + raw
    // shout writes + countprompt — the live-terminal substrate that
    // zshrs's non-interactive `-c` path doesn't use.

    // c:1569-1572 — `if (unset(NOTIFY)) scanjobs();` — sync job-status
    // print before prompt. C scanjobs walks jobtab[i] printing each
    // STAT_CHANGED entry; inlined here per the same pattern used in
    // exec.rs's execcmd_exec %job-resume AUTORESUME branch.
    if !crate::ported::zsh_h::isset(crate::ported::zsh_h::NOTIFY) {
        if let Some(jt) = crate::ported::jobs::JOBTAB.get() {
            let mut guard = jt.lock().unwrap();
            let long_list = crate::ported::zsh_h::isset(
                crate::ported::zsh_h::LONGLISTJOBS,
            );
            for i in 1..guard.len() {
                if (guard[i].stat & crate::ported::zsh_h::STAT_CHANGED) != 0 {
                    let s = crate::ported::jobs::printjob(
                        &guard[i], i, long_list, None, None,
                    );
                    if !s.is_empty() {
                        eprint!("{}", s);
                    }
                }
            }
        }
    }

    // c:1573-1574 — `if (errflag) return;` — bail if a previous error
    // already set the global flag.
    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
        return;
    }

    // c:1576-1580 — `callhookfunc("precmd", NULL, 1, NULL);` + errflag bail.
    callhookfunc("precmd", None, 1, std::ptr::null_mut());
    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
        return;
    }

    // c:1582-1589 — periodic-hook dispatch on PERIOD cadence.
    if period > 0 {
        let now = std::time::SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        if now > LAST_PERIODIC.load(Ordering::Relaxed) + period
            && callhookfunc("periodic", None, 1, std::ptr::null_mut()) == 0
        {
            LAST_PERIODIC.store(now, Ordering::Relaxed);
        }
    }
    // c:1588-1589 — `if (errflag) return;` post-periodic bail.
    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
        return;
    }

    // c:1591-1611 — mail check: `if (mailcheck && difftime(now,
    // lastmailcheck) > mailcheck)` walk MAILPATH (or scalar MAIL) via
    // checkmailpath. Faithful port — checkmailpath already exists at
    // utils.rs:1623, getaparam exists for the MAILPATH array.
    let currentmailcheck = std::time::SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);
    if mailcheck > 0
        && (currentmailcheck - LAST_MAILCHECK.load(Ordering::Relaxed)) > mailcheck
    {
        // c:1597 — `if (mailpath && *mailpath && **mailpath)`
        let mailpath = crate::ported::params::getaparam("MAILPATH");
        let has_mailpath = mailpath
            .as_ref()
            .map(|p| !p.is_empty() && p.first().map(|s| !s.is_empty()).unwrap_or(false))
            .unwrap_or(false);
        if has_mailpath {
            // c:1598 `checkmailpath(mailpath);`
            let _ = checkmailpath(mailpath.as_deref().unwrap()); // c:1598
        } else {
            // c:1600-1608 — `if ((mailfile = getsparam("MAIL")) && *mailfile)
            //                  { x[0]=mailfile; x[1]=NULL; checkmailpath(x); }`
            crate::ported::signals::queue_signals(); // c:1600
            if let Some(mailfile) = crate::ported::params::getsparam("MAIL") {
                if !mailfile.is_empty() {
                    let x = vec![mailfile]; // c:1604-1605
                    let _ = checkmailpath(&x); // c:1606
                }
            }
            crate::ported::signals::unqueue_signals(); // c:1608
        }
        LAST_MAILCHECK.store(currentmailcheck, Ordering::Relaxed); // c:1610
    }

    // c:1613-1618 — `if (prepromptfns) for (...) ppnode->func();`
    let snapshot: Vec<fn()> = PREPROMPT_FNS.lock().unwrap().clone();
    for f in snapshot {
        f();
    }
}

// the last time we checked mail                                            // c:1447
/// Check mail paths (from utils.c checkmailpath)
/// Rust idiom replacement: `str::find('?')` + `fs::metadata` covers
/// the C `strchr`+`stat` mtime-compare loop; the prompt-expansion +
/// printprompt callback runs in the caller, not in this fn.
pub fn checkmailpath(paths: &[String]) -> Vec<String> {
    let mut messages = Vec::new();
    for path in paths {
        // PATH?message format
        let (file, msg) = if let Some(pos) = path.find('?') {
            (&path[..pos], Some(&path[pos + 1..]))
        } else {
            (path.as_str(), None)
        };

        if let Ok(meta) = fs::metadata(file) {
            if let Ok(modified) = meta.modified() {
                if let Ok(elapsed) = modified.elapsed() {
                    if elapsed.as_secs() < 60 {
                        let default_msg = format!("You have new mail in {}", file);
                        messages.push(msg.unwrap_or(&default_msg).to_string());
                    }
                }
            }
        }
    }
    messages
}

/// Port of `printprompt4()` from `Src/utils.c:1718`.
///
/// Render the PS4 / PROMPT4 prefix and write it to stderr. zsh's
/// implementation reads `prompt4` global, suppresses XTRACE during
/// promptexpand (so subshells inside `%(?…)` don't recursively trace),
/// then fprintf's the expanded prefix to xtrerr. zshrs uses the same
/// suppress-XTRACE-around-expand pattern; ksh/sh emulation defaults
/// to `+ ` per Src/init.c:1192, zsh default to `+%N:%i> `.
///
/// The C source's caller (Src/exec.c::tracingcond etc.) follows this
/// with the per-line/per-arg fprintf — same shape mirrored at the two
/// zshrs call sites in fusevm_bridge.rs (BUILTIN_XTRACE_LINE / ARGS).
pub(crate) fn printprompt4() {
    // c:utils.c:1720 — `if (!isset(XTRACE)) return;`. C tests
    // `xtrerr` first then conditionally; the read-the-option early-
    // return path is equivalent for our purposes since we don't ship
    // the `xtrerr` separate-stream support.
    if !isset(XTRACE) {
        return;
    }
    // c:utils.c:1722-1724 — `if (prompt4) { ... s = dupstring(prompt4);`
    // C `prompt4` is a global initialized in init.c:1192 from the
    // emulation bits; PS4/PROMPT4 paramtab entries alias it via
    // IPDEF7R/IPDEF7 (params.c:381, 421). Read from paramtab to
    // honour user-set values, fall back to the same emulation
    // default C uses at init.c:1192.
    let posix = EMULATION(EMULATE_KSH | EMULATE_SH); // c:init.c:1192
    let prefix_template = getsparam("PS4")
        .or_else(|| getsparam("PROMPT4"))
        .unwrap_or_else(|| {
            if posix {
                "+ ".to_string() // c:init.c:1192
            } else {
                "+%N:%i> ".to_string() // c:init.c:1193
            }
        });
    // c:utils.c:1723,1726,1730 — `t = opts[XTRACE]; opts[XTRACE] = 0;
    //                              promptexpand(...);  opts[XTRACE] = t;`
    let saved = isset(XTRACE);
    opt_state_set(&opt_name(XTRACE), false);
    let prefix = crate::prompt::expand_prompt(&prefix_template);
    opt_state_set(&opt_name(XTRACE), saved);
    eprint!("{}", prefix);
}

/// Port of `freestr(void *a)` from `Src/utils.c:1739`.
///
/// C body:
/// ```c
/// void freestr(void *a) { zsfree(a); }
/// ```
/// The C function is registered as the `freenode` callback for
/// hashtables holding plain string values. The Rust port consumes
/// `a` by value; Rust's `Drop` runs the equivalent of `zsfree` when
/// the `String` is moved into this fn and falls out of scope at the
/// closing brace — the no-op body is the correct port. Param name
/// `a` matches C exactly per Rule E.
pub fn freestr(_a: String) {}

/// Port of `int gettempfile(const char *prefix, int use_heap, char **tempname)`
/// from Src/utils.c:2231. Creates a fresh tempfile with `O_RDWR|O_CREAT|O_EXCL`
/// and mode 0600 under umask 0177; returns `(fd, path)` on success.
///
/// C signature uses an out-param for the filename and returns the fd; Rust
/// returns both as a tuple. Matches the C control-flow: open with O_EXCL,
/// retry up to 16 times on EEXIST (the non-mkstemp branch, c:2255-2269).
pub fn gettempfile(prefix: Option<&str>) -> Option<(i32, String)> {
    // c:2231
    #[cfg(unix)]
    {
        queue_signals(); // c:2239
        let old_umask = unsafe { libc::umask(0o177) }; // c:2240
        let mut failures = 0; // c:2255
        let mut result: Option<(i32, String)> = None;
        loop {
            let fn_ = match gettempname(prefix, false) {
                // c:2260
                Some(n) => n,
                None => break,
            };
            let cn = match CString::new(fn_.clone()) {
                Ok(c) => c,
                Err(_) => break,
            };
            let fd = unsafe {
                libc::open(
                    cn.as_ptr(),
                    libc::O_RDWR | libc::O_CREAT | libc::O_EXCL, // c:2264
                    0o600 as libc::c_int,
                )
            };
            if fd >= 0 {
                result = Some((fd, fn_));
                break;
            }
            let err = io::Error::last_os_error().raw_os_error().unwrap_or(0);
            if err != libc::EEXIST {
                // c:2269
                break;
            }
            failures += 1;
            if failures >= 16 {
                // c:2269
                break;
            }
        }
        unsafe {
            libc::umask(old_umask);
        } // c:2273
        unqueue_signals(); // c:2274
        result
    }
    #[cfg(not(unix))]
    {
        let _ = prefix;
        None
    }
}

/// Port of `void gettyinfo(struct ttyinfo *ti)` from Src/utils.c:1746.
///
/// Reads the current termios from the global `SHTTY`. Returns
/// `None` when SHTTY is closed or the call fails (matching C's
/// silent return when SHTTY == -1).
/// C body (single statement): `fdgettyinfo(SHTTY, ti);`
/// Rust returns `Option<termios>` instead of taking an out-ptr;
/// the SHTTY=-1 case naturally returns Err from fdgettyinfo → None.
#[cfg(unix)]
pub fn gettyinfo() -> Option<libc::termios> {
    // c:1746
    fdgettyinfo(SHTTY.load(Ordering::Relaxed)).ok() // c:1748
}

/// Emit the `$PS4` xtrace prefix to stderr.
/// Read terminal mode from a file descriptor.
/// Port of `fdgettyinfo(int SHTTY, struct ttyinfo *ti)` from Src/utils.c:1753. C source uses
/// `tcgetattr(SHTTY, &ti->tio)`; we return the populated termios
/// or an io::Error on failure (caller equivalent to zsh's `zerr`).
#[cfg(unix)]
/// WARNING: param names don't match C — Rust=(fd) vs C=(SHTTY, ti)
pub fn fdgettyinfo(fd: i32) -> io::Result<libc::termios> {
    let mut tio: libc::termios = unsafe { std::mem::zeroed() };
    if unsafe { libc::tcgetattr(fd, &mut tio) } == -1 {
        Err(io::Error::last_os_error())
    } else {
        Ok(tio)
    }
}

#[cfg(not(unix))]
/// Port of `fdgettyinfo(int SHTTY, struct ttyinfo *ti)` from `Src/utils.c:1753`.
/// WARNING: param names don't match C — Rust=(_fd) vs C=(SHTTY, ti)
pub fn fdgettyinfo(_fd: i32) -> std::io::Result<()> {
    Err(std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        "no termios",
    ))
}

/// Port of `void settyinfo(struct ttyinfo *ti)` from Src/utils.c:1778.
///
/// Restores the termios state on the global `SHTTY` with EINTR
/// retry; no-op when SHTTY is closed.
/// C body (single statement): `fdsettyinfo(SHTTY, ti);`
/// Rust returns bool; SHTTY=-1 yields Err → false from fdsettyinfo.
#[cfg(unix)]
pub fn settyinfo(ti: &libc::termios) -> bool {
    // c:1778
    fdsettyinfo(SHTTY.load(Ordering::Relaxed), ti).is_ok() // c:1780
}

/// Apply terminal mode to a file descriptor, with EINTR retry.
/// Port of `fdsettyinfo(int SHTTY, struct ttyinfo *ti)` from Src/utils.c:1785. C source loops
/// `while (tcsetattr(SHTTY, TCSADRAIN, &ti->tio) == -1 && errno
/// == EINTR)` — same retry shape here.
#[cfg(unix)]
pub fn fdsettyinfo(SHTTY2: i32, ti: &libc::termios) -> io::Result<()> {
    loop {
        if unsafe { libc::tcsetattr(SHTTY2, libc::TCSADRAIN, ti) } != -1 {
            return Ok(());
        }
        let err = io::Error::last_os_error();
        if err.kind() != io::ErrorKind::Interrupted {
            return Err(err);
        }
    }
}

#[cfg(not(unix))]
/// Port of `fdsettyinfo(int SHTTY, struct ttyinfo *ti)` from `Src/utils.c:1785`.
pub fn fdsettyinfo(SHTTY: i32, ti: &()) -> std::io::Result<()> {
    Err(std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        "no termios",
    ))
}

// window size changed                                                     // c:1831
/// Port of `adjustlines(int signalled)` from Src/utils.c:1831 — TIOCGWINSZ
/// lookup that seeds `$LINES`. The C variant updates the global
/// `zterm_lines` and returns whether it changed; this Rust port
/// returns the row count directly. Falls back to `$LINES` env var,
/// then 24, mirroring the C source's `tclines > 0 ? tclines : 24`
/// fallback at line 1844.
/// WARNING: param names don't match C — Rust=() vs C=(signalled)
pub fn adjustlines() -> usize {
    // c:1831
    #[cfg(unix)]
    {
        unsafe {
            let mut ws: libc::winsize = std::mem::zeroed();
            if libc::ioctl(1, libc::TIOCGWINSZ, &mut ws) == 0 && ws.ws_row > 0 {
                return ws.ws_row as usize;
            }
        }
    }
    // c:1844 fallback — paramtab `$LINES`, not OS env.
    getsparam("LINES")
        .and_then(|s| s.parse().ok())
        .unwrap_or(24)
}

/// Port of `adjustcolumns(int signalled)` from Src/utils.c:1856 — TIOCGWINSZ
/// lookup that seeds `$COLUMNS`. The C variant updates the global
/// `zterm_columns` and returns whether it changed; this Rust port
/// returns the column count directly. Falls back to `$COLUMNS` env
/// var, then 80, mirroring the C source's `tccolumns > 0 ? tccolumns : 80`
/// fallback at line 1869.
/// WARNING: param names don't match C — Rust=() vs C=(signalled)
pub fn adjustcolumns() -> usize {
    // c:1856
    #[cfg(unix)]
    {
        unsafe {
            let mut ws: libc::winsize = std::mem::zeroed();
            if libc::ioctl(1, libc::TIOCGWINSZ, &mut ws) == 0 && ws.ws_col > 0 {
                return ws.ws_col as usize;
            }
        }
    }
    // c:1820 fallback — `if (zterm_columns <= 0) zterm_columns =
    //                    tccolumns > 0 ? tccolumns : 80`. C consults
    //                    `getsparam("COLUMNS")` (paramtab), not OS env.
    getsparam("COLUMNS")
        .and_then(|s| s.parse().ok())
        .unwrap_or(80)
}

// window size changed                                                      // c:1824
/// Port of `void adjustwinsize(int from)` from `Src/utils.c:1889`.
/// SIGWINCH handler + LINES/COLUMNS-update entry. Reads the tty's
/// current geometry via `TIOCGWINSZ` ioctl, updates the cached
/// `zterm_lines`/`zterm_columns`, and writes `$LINES` / `$COLUMNS`
/// when they're already set in the environment.
/// ```c
/// void
/// adjustwinsize(int from)
/// {
///     static int getwinsz = 1;
///     int ttyrows = shttyinfo.winsize.ws_row;
///     int ttycols = shttyinfo.winsize.ws_col;
///     int resetzle = 0;
///     if (getwinsz || from == 1) {
///         if (SHTTY == -1) return;
///         if (ioctl(SHTTY, TIOCGWINSZ, &shttyinfo.winsize) == 0) {
///             resetzle = (ttyrows != ... || ttycols != ...);
///             ...
///         } else {
///             shttyinfo.winsize.ws_row = zterm_lines;
///             shttyinfo.winsize.ws_col = zterm_columns;
///         }
///     }
///     switch (from) {
///     case 0: case 1:
///         getwinsz = 0;
///         if (adjustlines(from) && zgetenv("LINES")) setiparam("LINES", zterm_lines);
///         if (adjustcolumns(from) && zgetenv("COLUMNS")) setiparam("COLUMNS", zterm_columns);
///         getwinsz = 1;
///         break;
///     case 2: resetzle = adjustlines(0); break;
///     case 3: resetzle = adjustcolumns(0); break;
///     }
///     if (interact && resetzle) zleentry(ZLE_CMD_REFRESH);
/// }
/// ```
pub fn adjustwinsize(from: i32) -> (usize, usize) {
    // c:1889

    // c:1891 — `static int getwinsz = 1;`
    let getwinsz = ADJUSTWINSIZE_GETWINSZ.load(Ordering::SeqCst);

    let mut ttyrows: i32 = 0;
    let mut ttycols: i32 = 0;

    // c:1898-1917 — TIOCGWINSZ probe.
    if getwinsz != 0 || from == 1 {
        // c:1898
        let shtty = SHTTY.load(Ordering::Relaxed);
        if shtty == -1 {
            // c:1900
            return (adjustcolumns(), adjustlines()); // c:1901
        }
        #[cfg(unix)]
        unsafe {
            let mut ws: libc::winsize = std::mem::zeroed();
            if libc::ioctl(shtty, libc::TIOCGWINSZ, &mut ws as *mut _) == 0 {
                // c:1902
                ttyrows = ws.ws_row as i32; // c:1907
                ttycols = ws.ws_col as i32; // c:1908
            }
        }
    }

    let mut resetzle = 0i32;
    match from {
        // c:1921
        0 | 1 => {
            // c:1922-1923
            ADJUSTWINSIZE_GETWINSZ.store(0, Ordering::SeqCst); // c:1924
                                                               // c:1931-1932 — `if (adjustlines(from) && zgetenv("LINES")) setiparam(...)`
            let lines = adjustlines() as i32;
            if std::env::var_os("LINES").is_some() {
                setiparam("LINES", lines as i64); // c:1932
            }
            // c:1933-1934 — same for COLUMNS.
            let cols = adjustcolumns() as i32;
            if std::env::var_os("COLUMNS").is_some() {
                setiparam("COLUMNS", cols as i64); // c:1934
            }
            ADJUSTWINSIZE_GETWINSZ.store(1, Ordering::SeqCst); // c:1935
        }
        2 => {
            // c:1937
            resetzle = adjustlines() as i32; // c:1938
        }
        3 => {
            // c:1940
            resetzle = adjustcolumns() as i32; // c:1941
        }
        _ => {}
    }

    // c:1946-1958 — resetzle + zleentry(ZLE_CMD_REFRESH) when interact.
    if from >= 2 && resetzle != 0 {
        // ZLE refresh dispatch via zleentry(ZLE_CMD_REFRESH) lands here
        // once the C signal handler shape ports.
        let _ = ttyrows;
        let _ = ttycols;
    }

    (adjustcolumns(), adjustlines())
}

/// Port of `static int getwinsz` from `Src/utils.c:1891`. Local
/// reentry guard inside adjustwinsize — bumped to 0 around the
/// setiparam recursion so the recursive call short-circuits.
pub static ADJUSTWINSIZE_GETWINSZ: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(1); // c:1891

/// Port of `check_fd_table(int fd)` from `Src/utils.c:1968-1983`.
/// ```c
/// if (fd <= max_zsh_fd) return;
/// if (fd >= fdtable_size) {
///     int old_size = fdtable_size;
///     while (fd >= fdtable_size)
///         fdtable = zrealloc(fdtable, (fdtable_size *= 2) * sizeof(*fdtable));
///     memset(fdtable + old_size, 0, (fdtable_size - old_size) * sizeof(*fdtable));
/// }
/// max_zsh_fd = fd;
/// ```
/// C semantics: GROW the `fdtable` array so it can index `fd`, then
/// update `max_zsh_fd`. Returns void.
///
/// The Rust port keeps the same signature for name-parity but the
/// fdtable global isn't yet modeled — so this is a no-op shim. The
/// previous Rust impl was an `fcntl(F_GETFD)` validity check —
/// COMPLETELY DIFFERENT SEMANTICS from C (which doesn't validate
/// the fd at all, just grows the table). Fixed to no-op + bool
/// return for caller compatibility (no live callers).
pub fn check_fd_table(fd: i32) -> bool {
    // c:1969
    // c:1971-1972 — `if (fd <= max_zsh_fd) return;`
    let cur_max = MAX_ZSH_FD.load(Ordering::Relaxed); // c:1971
    if fd <= cur_max {
        // c:1971
        return true; // c:1972 (return; in C)
    }
    if fd < 0 {
        // defensive — fdtable index must be ≥0
        return false;
    }
    // c:1974-1981 — `if (fd >= fdtable_size) { while (fd >= fdtable_size)
    //                fdtable_size *= 2; zrealloc; memset(new_slots, 0); }`.
    // Rust Vec::resize handles the realloc + zero-fill in one call (the
    // expansion bumps capacity geometrically too).
    {
        let mut g = fdtable_lock().lock().unwrap();
        if (fd as usize) >= g.len() {
            g.resize((fd as usize) + 1, FDT_UNUSED); // c:1975-1979 grow
        }
    }
    // c:1982 — `max_zsh_fd = fd;`.
    MAX_ZSH_FD.store(fd, Ordering::Relaxed); // c:1982
    true
}

/// Port of `movefd(int fd)` from `Src/utils.c:1989-2012`.
/// ```c
/// if (fd != -1 && fd < 10) {
///     int fe = fcntl(fd, F_DUPFD, 10);
///     zclose(fd);          // unconditional close, even when fe == -1
///     fd = fe;
/// }
/// if (fd != -1) {
///     check_fd_table(fd);
///     fdtable[fd] = FDT_INTERNAL;
/// }
/// return fd;
/// ```
/// Two divergences fixed this iteration:
///   1. Old Rust closed the source fd ONLY on success; C closes
///      unconditionally per the c:1999-2004 comment "probably better
///      to avoid a leak."
///   2. Old Rust added `FD_CLOEXEC` after the dup; C does NOT —
///      CLOEXEC is added later by `addmodulefd`/`addlockfd` callers
///      that need it. movefd itself does not.
pub fn movefd(fd: i32) -> i32 {
    #[cfg(unix)]
    {
        let mut fd = fd;
        if fd != -1 && fd < 10 {
            // c:1992
            let fe = unsafe { libc::fcntl(fd, libc::F_DUPFD, 10) }; // c:1994
            unsafe { libc::close(fd) }; // c:2004 zclose(fd) — unconditional
            fd = fe; // c:2005
        }
        // c:2007-2010 — `if (fd != -1) { check_fd_table(fd); fdtable[fd]
        // = FDT_INTERNAL; }`. The fdtable global IS now modeled (port
        // of utils.c:63 — `Vec<u8>` behind `fdtable_lock`). Mark the
        // new fd as FDT_INTERNAL so the rest of the shell knows the
        // shell is using it (a later `addmodulefd` / `addlockfd`
        // upgrade may reclassify; raw external code shouldn't touch
        // it). The previous port skipped this so internal-fd tracking
        // (which `closeallelse` and forkexec rely on) silently
        // never saw zshrs-internal fds.
        if fd != -1 {
            // c:2007
            check_fd_table(fd); // c:2008
            fdtable_set(fd, FDT_INTERNAL); // c:2009
        }
        fd
    }
    #[cfg(not(unix))]
    {
        fd
    }
}

/// Port of `redup(int x, int y)` from `Src/utils.c:2021`.
///
/// C signature: `int redup(int x, int y)`.
/// "Move fd x to y. If x == -1, fd y is closed. Returns y for
/// success, -1 for failure." (c:2014-2016 docstring.)
///
/// Body mirrors c:2023-2068: when `x == -1`, close `y` (return `y`);
/// when `x == y`, no-op (return `y`); otherwise `dup2(x, y)` +
/// `close(x)`.
///
/// C body fdtable updates (c:2053-2063) that the previous Rust port
/// SKIPPED with a stale "fdtable global not yet ported" comment —
/// fdtable IS now ported and these updates are load-bearing:
///   * `fdtable[y] = fdtable[x]` — the new fd inherits the old fd's
///     ownership category (FDT_INTERNAL / FDT_MODULE / etc.).
///   * If the inherited type is `FDT_FLOCK` / `FDT_FLOCK_EXEC`, promote
///     to `FDT_INTERNAL` (the dup'd fd doesn't carry the flock).
///   * If `fdtable[x] == FDT_FLOCK`, decrement `fdtable_flocks` (the
///     original lock-holding fd is about to be closed).
///
/// Without these updates, redup'd fds had stale `FDT_UNUSED` ownership
/// and `closeallelse(FDT_EXTERNAL)` etc. couldn't classify them.
pub fn redup(x: i32, y: i32) -> i32 {
    // c:2021
    let mut ret = y; // c:2023
    #[cfg(unix)]
    {
        if x < 0 {
            // c:2047
            zclose(y); // c:2048
        } else if x != y {
            // c:2049
            // c:2053-2057 — successful dup2: copy fdtable + FLOCK promote.
            if unsafe { libc::dup2(x, y) } == -1 {
                // c:2050
                ret = -1; // c:2051
            } else {
                check_fd_table(y); // c:2053
                let kind_x = fdtable_get(x); // c:2054
                let kind_y = if kind_x == FDT_FLOCK || kind_x == FDT_FLOCK_EXEC {
                    FDT_INTERNAL // c:2055-2056
                } else {
                    kind_x // c:2054
                };
                fdtable_set(y, kind_y);
            }
            // c:2062-2063 — `if (fdtable[x] == FDT_FLOCK) fdtable_flocks--;`
            // The original lock-holding fd is about to be closed, so the
            // flock-count tracker must drop. Even on dup2 failure C still
            // closes x, so this runs in both arms.
            if fdtable_get(x) == FDT_FLOCK {
                // c:2062
                FDTABLE_FLOCKS.fetch_sub(1, Ordering::SeqCst); // c:2063
            }
            zclose(x); // c:2064
        }
    }
    #[cfg(not(unix))]
    {
        let _ = (x, y);
    }
    ret // c:2067
}

/// Port of `addmodulefd(int fd, int fdt)` from `Src/utils.c:2090-2097`.
/// ```c
/// if (fd >= 0) {
///     check_fd_table(fd);
///     fdtable[fd] = fdt;
/// }
/// ```
/// Two divergences fixed this iteration:
///   1. C accepts an `fdt` parameter (FDT_MODULE / FDT_INTERNAL /
///      FDT_EXTERNAL — see callers in Src/Modules/{random,socket,tcp,
///      db_gdbm}.c). Rust port hardcoded `FDT_MODULE` — silently
///      ignored caller intent. Now takes `fdt` per C.
///   2. C does NOT set `FD_CLOEXEC`. Rust port was adding it
///      unconditionally — divergent. CLOEXEC is the caller's
///      responsibility based on the fdt semantics.
pub fn addmodulefd(fd: i32, fdt: i32) {
    // c:2091
    // c:2093 — `if (fd >= 0)`.
    if fd >= 0 {
        // c:2094 — `check_fd_table(fd)` — grow fdtable to cover fd.
        check_fd_table(fd); // c:2094
                            // c:2095 — `fdtable[fd] = fdt`. Routes through the canonical
                            // helper which mirrors C's direct `fdtable[fd] = fdt` write.
        fdtable_set(fd, fdt); // c:2095
    }
}

/// Port of `addlockfd(int fd, int cloexec)` from `Src/utils.c:2111-2121`.
/// ```c
/// if (cloexec) {
///     if (fdtable[fd] != FDT_FLOCK)
///         fdtable_flocks++;
///     fdtable[fd] = FDT_FLOCK;
/// } else {
///     fdtable[fd] = FDT_FLOCK_EXEC;
/// }
/// ```
/// Critical divergence fixed: C updates `fdtable[fd]` to FDT_FLOCK
/// (cloexec=true) or FDT_FLOCK_EXEC (cloexec=false). Previous Rust
/// port did the OPPOSITE — called `fcntl(F_SETFD, FD_CLOEXEC)` when
/// `cloexec` was true. That's wrong on two counts:
///   1. C's "cloexec" parameter selects the FDT category, not a
///      libc fcntl flag.
///   2. FDT_FLOCK means "lock survives across exec via fd inheritance"
///      — the OPPOSITE of close-on-exec. The Rust port was adding
///      CLOEXEC for the very case where the fd should be inheritable.
pub fn addlockfd(fd: i32, cloexec: bool) {
    // c:2112
    if cloexec {
        // c:2114
        // c:2115-2117 — track flock count, set FDT_FLOCK.
        if fdtable_get(fd) != FDT_FLOCK {
            FDTABLE_FLOCKS.fetch_add(1, Ordering::SeqCst);
        }
        fdtable_set(fd, FDT_FLOCK);
    } else {
        // c:2118
        // c:2119 — FDT_FLOCK_EXEC means flock inherited by exec.
        fdtable_set(fd, FDT_FLOCK_EXEC);
    }
}

// FDTABLE_FLOCKS canonical declaration lives at utils.rs:5219
// (the exec.c global with same name). Reuse, don't redeclare.

/// Close the given fd, and clear it from fdtable.                          // c:2123
/// Port of `int zclose(int fd)` from `Src/utils.c:2127`.
pub fn zclose(fd: i32) -> i32 {
    // c:2127
    if fd >= 0 {
        // c:2129
        // c:2130-2133 — comment carry: "Careful: we allow closing of
        // arbitrary fd's, beyond max_zsh_fd. In that case we don't
        // try anything clever."
        let max_fd = MAX_ZSH_FD.load(Ordering::Relaxed); // c:2134
        if fd <= max_fd {
            // c:2134
            if fdtable_get(fd) == FDT_FLOCK {
                // c:2135
                FDTABLE_FLOCKS.fetch_sub(1, Ordering::Relaxed);
                // c:2136
            }
            fdtable_set(fd, FDT_UNUSED); // c:2137
                                                               // c:2138-2139 — shrink max_zsh_fd past trailing UNUSED slots.
            let mut m = MAX_ZSH_FD.load(Ordering::Relaxed);
            while m > 0 && fdtable_get(m) == FDT_UNUSED {
                m -= 1;
            }
            MAX_ZSH_FD.store(m, Ordering::Relaxed);
            // c:2140-2143 — coproc fd tracking.
            if fd
                == coprocin
                    .load(Ordering::Relaxed)
            {
                coprocin
                    .store(-1, Ordering::Relaxed);
            }
            if fd
                == coprocout
                    .load(Ordering::Relaxed)
            {
                coprocout
                    .store(-1, Ordering::Relaxed);
            }
        }
        #[cfg(unix)]
        unsafe {
            return libc::close(fd);
        } // c:2145
        #[cfg(not(unix))]
        return 0;
    }
    -1 // c:2147
}

/// Port of `int zcloselockfd(int fd)` from `Src/utils.c:2155-2164`.
/// ```c
/// if (fd > max_zsh_fd) return -1;
/// if (fdtable[fd] != FDT_FLOCK && fdtable[fd] != FDT_FLOCK_EXEC)
///     return -1;
/// zclose(fd);
/// return 0;
/// ```
/// "Close an fd returning 0 if used for locking; return -1 if it
/// isn't." Caller (`bin_zsystem_flock -u`) uses the -1 return to
/// distinguish "fd not in the flock table" from "successfully
/// released a lock."
///
/// Previously the Rust port skipped the FDT_FLOCK / FDT_FLOCK_EXEC
/// check entirely and always returned 0 — meaning `zsystem flock -u
/// <unlocked-fd>` reported success on any fd. Now that the
/// `addlockfd` port (this iteration) populates the fdtable with the
/// canonical FDT_FLOCK / FDT_FLOCK_EXEC slots, the check can fire
/// faithfully.
pub fn zcloselockfd(fd: i32) -> i32 {
    // c:2156
    let max_fd = MAX_ZSH_FD.load(Ordering::Relaxed);
    // c:2158 — `if (fd > max_zsh_fd) return -1;`.
    if fd > max_fd {
        return -1;
    }
    // c:2160-2161 — `if (fdtable[fd] != FDT_FLOCK && != FDT_FLOCK_EXEC)
    //                   return -1;`.
    let slot = fdtable_get(fd);
    if slot != FDT_FLOCK && slot != FDT_FLOCK_EXEC {
        return -1;
    }
    zclose(fd); // c:2162
    0 // c:2163
}

/// Port of `char *gettempname(const char *prefix, int use_heap)` from Src/utils.c:2178.
///
/// Returns a unique tempfile name templated like C `mktemp(3)` —
/// `{prefix}.XXXXXX`. Falls back to `getsparam("TMPPREFIX")` and
/// then `DEFAULT_TMPPREFIX` when `prefix` is None. Does NOT create
/// the file (matches C — only `gettempfile` creates).
pub fn gettempname(prefix: Option<&str>, _use_heap: bool) -> Option<String> {
    // c:2178
    let suffix = if prefix.is_some() {
        ".XXXXXX"
    } else {
        "XXXXXX"
    }; // c:2178
    queue_signals(); // c:2182
    let prefix_owned: String = match prefix {
        // c:2183
        Some(p) => p.to_string(),
        // c:2184 — `getsparam("TMPPREFIX")`. Read from paramtab (not OS
        //          env); fall back to compile-time default when unset.
        None => getsparam("TMPPREFIX")
            .unwrap_or_else(|| crate::ported::config_h::DEFAULT_TMPPREFIX.to_string()),
    };
    let template = format!("{}{}", prefix_owned, suffix); // c:2186-2188
                                                          // C uses mktemp(3) which mutates the X's into a unique name              // c:2192/2219
                                                          // without creating the file. Rust has no mktemp; emulate with
                                                          // pid+timestamp. Caller is responsible for O_EXCL open.
    let pid = std::process::id();
    let nanos = std::time::SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    let unique = format!("{:x}{:x}", pid, nanos & 0xffffff);
    let name = template.replace("XXXXXX", &unique);
    unqueue_signals(); // c:2221
    Some(name)
}

/// Check if metafied - port from zsh/Src/utils.c has_token()
// Check if a string contains a token                                       // c:2282
/// Port of `has_token(const char *s)` from `Src/utils.c:2282` — used by
/// `ecstrcode` (parse.rs:989) to flip the token-marker bit on the
/// encoded string offset. Token markers live in 0x83..=0x9f (Pound,
/// Stringg, Hat, Star, ..., Bnull, Nularg). Earlier impl checked
/// only 0x83 (Meta) which missed Dash/Equals/Inbrack/Inbrace/etc.,
/// so any string containing those (e.g. `dart-lang/dart` →
/// `dart\u{9b}lang/dart`) got encoded with the no-token bit set,
/// breaking byte parity with C's wordcode-emitter output.
/// Port of `int has_token(const char *s)` from `Src/utils.c:2280-2288`.
/// ```c
/// while (*s)
///     if (itok(*s++)) return 1;
/// return 0;
/// ```
/// Routes through the canonical `itok()` typtab-driven predicate.
/// Previous Rust port used hardcoded `0x83..=0x9f` which was:
///   - Wrong on the low end: includes `0x83` (Meta, NOT a token).
///   - Wrong on the high end: missing `0xa0..=0xa1` (part of the
///     Snull..Nularg range that `itok` legitimately covers).
/// The full canonical ITOK range per `Src/zsh.h:152-159` is
/// `0x84..=0xa1` (Pound..Nularg), with possible gaps in the middle
/// (Snull at 0x9d follows Bang at 0x9c).
pub fn has_token(s: &str) -> bool {
    // c:2282
    s.bytes().any(itok) // c:2285
}

// Delete a character in a string                                           // c:2294
/// Remove character from string (from utils.c chuck)
pub fn chuck(s: &mut String, pos: usize) {
    // c:2294
    if pos < s.len() {
        s.remove(pos);
    }
}

/// Convert character to lowercase
/// To-lowercase that respects locale.
/// Port of `tulower(int c)` from Src/utils.c.
pub fn tulower(c: char) -> char {
    // c:2302
    c.to_lowercase().next().unwrap_or(c)
}

/// Convert character to uppercase
/// To-uppercase that respects locale.
/// Port of `tuupper(int c)` from Src/utils.c.
pub fn tuupper(c: char) -> char {
    // c:2310
    c.to_uppercase().next().unwrap_or(c)
}

/// Port of `void ztrncpy(char *s, char *t, int len)` from `Src/utils.c:2320`.
///
/// C body (c:2320-2326):
/// ```c
/// while (len--)
///     *s++ = *t++;
/// *s = '\0';
/// ```
///
/// Copy `len` bytes from `t` into `s`, then NUL-terminate. C does no
/// bounds check; the caller must ensure `s` has `len + 1` bytes of
/// capacity. Rust port preserves the (s, t, len) parameter shape:
/// `s: &mut String` (output), `t: &str` (source), `len: usize` (copy
/// count). The C NUL terminator at c:2325 is implicit in Rust's
/// length-prefixed String. The Rust port clamps `len` to `t.len()` to
/// avoid the out-of-bounds read C's `*s++ = *t++` would perform when
/// `len > strlen(t)` (UB in C; explicitly bounded in Rust).
pub fn ztrncpy(s: &mut String, t: &str, len: usize) {
    // c:2320
    s.clear();                                                               // C overwrites from start of *s
    let take = len.min(t.len());                                             // c:2322 while (len--)
    s.push_str(&t[..take]);                                                  // c:2322 *s++ = *t++
    // c:2325 — `*s = '\0';` (implicit in Rust String length)
}

/// Port of `void strucpy(char **s, char *t)` from `Src/utils.c:2331-2337`.
/// ```c
/// char *u = *s;
/// while ((*u++ = *t++));
/// *s = u - 1;       // leave *s pointing at the NUL terminator
/// ```
/// Body: `strcpy(*s, t)` + advance `*s` to point at the new
/// NUL terminator. The "u" in the name is a C convention for the
/// local pointer-walker, NOT "upper-case" — the function does NOT
/// change case.
///
/// Rust API: param name `s` matches C exactly per Rule E (despite Rust
/// convention of `dest` for output buffers). The C pointer-advance
/// `*s = u - 1` translates to "the new end of s is at s.len()", which
/// is implicit in Rust's owning-String model.
pub fn strucpy(s: &mut String, t: &str) {
    // c:2331
    s.push_str(t);                                                           // c:2335 `*u++ = *t++` loop
    // c:2336 — `*s = u - 1;` — pointer-advance is implicit in Rust
    // (`s.len()` is the new end-of-string position).
}

/// Port of `void struncpy(char **s, char *t, int n)` from `Src/utils.c:2341-2350`.
/// ```c
/// char *u = *s;
/// while (n-- && (*u = *t++)) u++;
/// *s = u;
/// if (n > 0) *u = '\0';
/// ```
/// Body: copy up to `n` bytes from `t` to `*s`, NUL-terminate.
/// Note c:2348 — "just one null-byte will do, unlike strncpy(3)";
/// doesn't pad with NULs.
///
/// Param names match C exactly per Rule E.
pub fn struncpy(s: &mut String, t: &str, n: usize) {
    // c:2341
    // c:2345 — `while (n-- && (*u = *t++)) u++;` — copy up to n
    // bytes, stop at NUL (which in Rust &str is the end-of-string).
    let take = n.min(t.len());
    s.push_str(&t[..take]);
}

/// Array length - port from arrlen()
/// Port of `arrlen(char **s)` from `Src/utils.c:2357`.
pub fn arrlen<T>(s: &[T]) -> usize {
    // c:2357
    s.len()
}

// Return TRUE iff arrlen(s) >= lower_bound, but more efficiently.          // c:2382
/// Check if array length >= n (from utils.c arrlen_ge)
pub fn arrlen_ge<T>(arr: &[T], n: usize) -> bool {
    // c:2369
    arr.len() >= n
}

// Return TRUE iff arrlen(s) > lower_bound, but more efficiently.           // c:2400
/// Check if array length > n (from utils.c arrlen_gt)
pub fn arrlen_gt<T>(arr: &[T], n: usize) -> bool {
    // c:2382
    arr.len() > n
}

// Return TRUE iff arrlen(s) <= upper_bound, but more efficiently.          // c:2409
/// Check if array length <= n (from utils.c arrlen_le)
pub fn arrlen_le<T>(arr: &[T], n: usize) -> bool {
    // c:2391
    arr.len() <= n
}

// Return TRUE iff arrlen(s) < upper_bound, but more efficiently.           // c:2400
/// Check if array length < n (from utils.c arrlen_lt)
pub fn arrlen_lt<T>(arr: &[T], n: usize) -> bool {
    // c:2400
    arr.len() < n
}

/// Skip balanced parentheses (from utils.c skipparens)
// Skip over a balanced pair of parenthesis.                                // c:2409
pub fn skipparens(s: &str, open: char, close: char) -> usize {
    // c:2409
    let mut depth = 0;
    for (i, c) in s.char_indices() {
        if c == open {
            depth += 1;
        } else if c == close {
            depth -= 1;
            if depth == 0 {
                return i + c.len_utf8();
            }
        }
    }
    s.len()
}

/// zlong with base autodetection. Returns the parsed value AND a
/// `&str` slice pointing to the first unconsumed character —
/// matching C's `char **t` out-arg.
///
/// C signature: `zlong zstrtol(const char *s, char **t, int base)`.
/// `base == 0` triggers prefix-based detection (`0x`/`0X`→16,
/// `0b`/`0B`→2, leading `0`→8, otherwise 10). Otherwise `base`
/// must be in [2, 36]; values outside that range emit `zerr` and
/// return 0.
///
/// On overflow, mirrors C's "number truncated after N digits"
/// behaviour: emits a `zwarn` and returns the truncated value
/// (last in-range computation).
/// Port of `zstrtol(const char *s, char **t, int base)` from `Src/utils.c:2427`.
/// WARNING: param names don't match C — Rust=(s, base) vs C=(s, t, base)
pub fn zstrtol(s: &str, base: i32) -> (i64, &str) {
    // c:2427
    zstrtol_underscore(s, base, false) // c:2427
}

/// Port of `zstrtol_underscore(const char *s, char **t, int base, int underscore)` from `Src/utils.c:2438`.
///
/// C signature: `zlong zstrtol_underscore(const char *s, char **t,
///                                         int base, int underscore)`.
///
/// Convert string to zlong with optional `_` digit-separator support.
/// `underscore != 0` allows underscores to appear inside the digit
/// run (skipped during accumulation). Returns the parsed value AND
/// the unconsumed-tail slice (matching C's `*t = (char *)s` writeback
/// at line 2516).
///
/// `base == 0` triggers prefix-based detection (`0x`/`0X`→16,
/// `0b`/`0B`→2, leading `0`→8, otherwise 10). Bases outside [2, 36]
/// emit `zerr` and return `(0, original_s)`. Overflow truncates and
/// emits a `zwarn` per c:2510-2512.
pub fn zstrtol_underscore(s: &str, base: i32, underscore: bool) -> (i64, &str) {
    // c:2438
    let bytes = s.as_bytes();
    let mut i = 0usize;
    // c:2444-2445 — skip leading `inblank` whitespace.
    while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t') {
        i += 1;
    }
    // c:2447-2450 — handle sign.
    let neg = if i < bytes.len() && (bytes[i] == b'-') {
        // c:2447 IS_DASH
        i += 1;
        true
    } else if i < bytes.len() && bytes[i] == b'+' {
        // c:2449
        i += 1;
        false
    } else {
        false
    };

    // c:2452-2461 — base autodetect when base == 0.
    let mut base = base;
    if base == 0 {
        // c:2452
        if i >= bytes.len() || bytes[i] != b'0' {
            // c:2453
            base = 10; // c:2454
        } else {
            i += 1; // c:2455 ++s
            if i < bytes.len() && (bytes[i] == b'x' || bytes[i] == b'X') {
                // c:2455
                base = 16;
                i += 1;
            } else if i < bytes.len() && (bytes[i] == b'b' || bytes[i] == b'B') {
                // c:2457
                base = 2;
                i += 1;
            } else {
                // c:2459
                base = 8; // c:2460
            }
        }
    }
    let inp_idx = i; // c:2462 inp = s
    if base < 2 || base > 36 {
        // c:2463
        zerr(&format!(
            "invalid base (must be 2 to 36 inclusive): {}",
            base
        )); // c:2464
        return (0, s); // c:2465 return 0
    }

    let mut calc: u64 = 0; // c:2441
    let mut trunc_idx: Option<usize> = None; // c:2440 trunc = NULL
    if base <= 10 {
        // c:2466
        // c:2467-2479 — digit accumulator, low bases.
        let max_d = b'0' + base as u8;
        while i < bytes.len() {
            let c = bytes[i];
            if c >= b'0' && c < max_d {
                if trunc_idx.is_none() {
                    let newcalc = calc
                        .wrapping_mul(base as u64)
                        .wrapping_add((c - b'0') as u64);
                    if newcalc < calc {
                        // c:2473
                        trunc_idx = Some(i); // c:2474
                    } else {
                        calc = newcalc;
                    }
                }
                i += 1;
            } else if underscore && c == b'_' {
                // c:2467 underscore && *s == '_'
                i += 1;
            } else {
                break;
            }
        }
    } else {
        // c:2480
        // c:2481-2495 — digit accumulator, high bases (>10).
        while i < bytes.len() {
            let c = bytes[i];
            let digit = if c.is_ascii_digit() {
                // c:2481 idigit
                Some((c - b'0') as u32)
            } else if c >= b'a' && c < b'a' + base as u8 - 10 {
                // c:2482
                Some(((c & 0x1f) + 9) as u32) // c:2486 (*s & 0x1f) + 9
            } else if c >= b'A' && c < b'A' + base as u8 - 10 {
                // c:2483
                Some(((c & 0x1f) + 9) as u32)
            } else if underscore && c == b'_' {
                // c:2484
                i += 1;
                continue;
            } else {
                None
            };
            match digit {
                Some(d) => {
                    if trunc_idx.is_none() {
                        let newcalc = calc.wrapping_mul(base as u64).wrapping_add(d as u64);
                        if newcalc < calc {
                            // c:2489
                            trunc_idx = Some(i); // c:2490
                        } else {
                            calc = newcalc;
                        }
                    }
                    i += 1;
                }
                None => break,
            }
        }
    }

    // c:2504-2511 — special-case: signed-overflow into top bit.
    // The lowest negative number triggers the first test but is
    // representable correctly; check it explicitly.
    if trunc_idx.is_none() && (calc as i64) < 0 {
        // c:2504
        let top_bit_only = calc & !(1u64 << 63); // c:2506
        if !neg || top_bit_only != 0 {
            // c:2505
            trunc_idx = Some(i.saturating_sub(1)); // c:2508
            calc /= base as u64; // c:2509
        }
    }

    if let Some(t) = trunc_idx {
        // c:2512
        let digits = t.saturating_sub(inp_idx);
        let inp_str = &s[inp_idx..];
        zwarn(&format!(
            "number truncated after {} digits: {}",
            digits, inp_str
        )); // c:2513
    }

    let result = if neg {
        // c:2517
        -(calc as i64)
    } else {
        calc as i64
    };
    (result, &s[i..])
}

/// Parse unsigned integer with underscore support
/// Port from zsh/Src/utils.c zstrtoul_underscore() lines 2528-2575
/// Port of `zstrtoul_underscore(const char *s, zulong *retval)` from
/// `Src/utils.c:2527-2590`. C body:
/// ```c
/// if (*s == '+') s++;
/// if      (*s != '0')                    base = 10;
/// else if (*++s == 'x' || *s == 'X')     base = 16, s++;
/// else if (*s == 'b' || *s == 'B')       base = 2,  s++;
/// else                                   base = isset(OCTALZEROES) ? 8 : 10;
/// ```
/// The leading-zero case is option-gated on OCTALZEROES — without
/// the option (default), `0777` parses as decimal 777, NOT octal 511.
/// Previously the Rust port always treated leading-zero as octal —
/// divergent for the default shell setup.
pub fn zstrtoul_underscore(s: &str) -> Option<u64> {
    // c:2529
    let s = s.trim();
    let s = s.strip_prefix('+').unwrap_or(s); // c:2533-2534

    let (base, rest) = if s.starts_with("0x") || s.starts_with("0X") {
        // c:2538
        (16, &s[2..])
    } else if s.starts_with("0b") || s.starts_with("0B") {
        // c:2540
        (2, &s[2..])
    } else if s.starts_with('0')
        && s.len() > 1
        && isset(OCTALZEROES)
    // c:2543
    {
        (8, &s[1..])
    } else {
        (10, s)
    };

    let rest = rest.replace('_', "");
    u64::from_str_radix(&rest, base).ok()
}

/// Port of `int setblock_fd(int turnonblocking, int fd, long *modep)`
/// from `Src/utils.c:2579`. Toggles O_NONBLOCK on `fd`.
///
/// **Signature matches C exactly** (param order `turnonblocking, fd`):
/// - `turnonblocking=1` → clear O_NONBLOCK (enable blocking).
/// - `turnonblocking=0` → set O_NONBLOCK (disable blocking).
///
/// Returns:
/// - `(true, mode)` if the fd's state was changed.
/// - `(false, -1)` if the fd is a regular file (C skips regular
///   files entirely — only operates on pipes/sockets/ttys per
///   `c:2599 if (!fstat(fd, &st) && !S_ISREG(st.st_mode))`).
/// - `(false, mode)` if state was already correct.
///
/// Rust returns `(state_changed, modep_value)` as a tuple to mirror
/// C's `int` return + `long *modep` out-param.
pub fn setblock_fd(turnonblocking: bool, fd: i32) -> (bool, libc::c_long) {
    // c:2579
    #[cfg(unix)]
    unsafe {
        // c:2598-2600 — `if (!fstat(fd, &st) && !S_ISREG(st.st_mode))`.
        let mut st: libc::stat = std::mem::zeroed();
        if libc::fstat(fd, &mut st) != 0 {
            return (false, -1);
        }
        // c:2599 — skip regular files (no nonblock concept).
        let mode_bits = st.st_mode as u32;
        if (mode_bits & libc::S_IFMT as u32) == libc::S_IFREG as u32 {
            return (false, -1); // c:2614 *modep = -1; return 0
        }
        // c:2601 — `*modep = fcntl(fd, F_GETFL, 0);`
        let modep = libc::fcntl(fd, libc::F_GETFL, 0) as libc::c_long;
        if modep < 0 {
            return (false, -1); // c:2602 if (*modep != -1)
        }
        const NONBLOCK: libc::c_long = libc::O_NONBLOCK as libc::c_long;
        if !turnonblocking {
            // c:2603-2606 — want to KNOW if blocking was off; set it on.
            if (modep & NONBLOCK) != 0 {
                return (true, modep); // already nonblock — no-op, but "off"
            }
            if libc::fcntl(fd, libc::F_SETFL, modep | NONBLOCK) == 0 {
                return (true, modep); // c:2606
            }
        } else {
            // c:2607-2611 — want to clear NONBLOCK if currently set.
            if (modep & NONBLOCK) != 0 && libc::fcntl(fd, libc::F_SETFL, modep & !NONBLOCK) == 0 {
                return (true, modep); // c:2611 state changed
            }
        }
        (false, modep)
    }
    #[cfg(not(unix))]
    {
        let _ = (turnonblocking, fd);
        (false, -1)
    }
}

/// Port of `int setblock_stdin(void)` from `Src/utils.c:2620-2625`.
/// ```c
/// long mode;
/// return setblock_fd(1, 0, &mode);
/// ```
/// `turnonblocking=1, fd=0` — turn ON blocking on stdin (clear
/// O_NONBLOCK).
pub fn setblock_stdin() -> i32 {
    // c:2624 — `return setblock_fd(1, 0, &mode);`.
    let (changed, _mode) = setblock_fd(true, 0); // c:2624
    changed as i32
}

/// Read poll - check for pending input
/// Port from zsh/Src/utils.c read_poll() lines 2643-2730
/// Poll an fd with timeout, returning whether it's readable.
/// Port of the `poll(2)` wrapper Src/utils.c uses for
/// `read -t` timeout handling.
pub fn read_poll(fd: i32, timeout_us: i64) -> bool {
    // c:2645
    #[cfg(unix)]
    {
        let mut fds = [libc::pollfd {
            fd: fd as RawFd,
            events: libc::POLLIN,
            revents: 0,
        }];
        let timeout_ms = (timeout_us / 1000) as i32;
        let result = unsafe { libc::poll(fds.as_mut_ptr(), 1, timeout_ms) };
        result > 0 && (fds[0].revents & libc::POLLIN) != 0
    }
    #[cfg(not(unix))]
    {
        let _ = (fd, timeout_us);
        false
    }
}

/// Compute time difference in microseconds (from utils.c timespec_diff_us)
/// Port of `timespec_diff_us(const struct timespec *t1, const struct timespec *t2)` from `Src/utils.c:2752`.
/// Rust idiom replacement: `Instant::duration_since().as_micros()`
/// replaces the C tv_sec/tv_nsec arithmetic + sign-flip dance.
pub fn timespec_diff_us(t1: &std::time::Instant, t2: &std::time::Instant) -> i64 {
    if *t2 > *t1 {
        t2.duration_since(*t1).as_micros() as i64
    } else {
        -(t1.duration_since(*t2).as_micros() as i64)
    }
}

/// Port of `int zmonotime(time_t *tloc)` from Src/utils.c:2780.
///
/// "Like time(), but uses the monotonic clock." Reads CLOCK_MONOTONIC
/// and returns `tv_sec`; writes the same value through `tloc` if
/// non-NULL (Rust: `Some(&mut t)`).
pub fn zmonotime(tloc: Option<&mut i64>) -> i64 {
    // c:2780
    #[cfg(unix)]
    {
        // c:2782 — `struct timespec ts;`
        let mut ts = libc::timespec {                                        // c:2782
            tv_sec: 0,
            tv_nsec: 0,
        };
        // c:2783 — `zgettime_monotonic_if_available(&ts);`
        unsafe {
            libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);             // c:2783
        }
        // c:2784-2785 — `if (tloc) *tloc = ts.tv_sec;`
        if let Some(t) = tloc {                                              // c:2784
            *t = ts.tv_sec as i64;                                           // c:2785
        }
        ts.tv_sec as i64                                                     // c:2786 return ts.tv_sec
    }
    #[cfg(not(unix))]
    {
        let _ = tloc;
        0
    }
}

/// Check if string looks like a number
/// Check whether a string parses as a decimal integer.
/// Sleep for a given number of seconds (fractional)
/// Sleep for a fractional number of seconds.
/// Port of `int zsleep(long us)` from Src/utils.c:2797.
///
/// "Sleep for the given number of microseconds." Wraps
/// `nanosleep(2)` with EINTR retry, returning 1 on completion, 0
/// on permanent error.
pub fn zsleep(us: i64) -> i32 {
    // c:2797
    let mut sleeptime = libc::timespec {
        // c:2797-2803
        tv_sec: (us / 1_000_000) as libc::time_t,
        tv_nsec: ((us % 1_000_000) * 1000) as libc::c_long,
    };
    #[cfg(unix)]
    {
        loop {
            // c:2804
            let mut rem = libc::timespec {
                tv_sec: 0,
                tv_nsec: 0,
            };
            let ret = unsafe { libc::nanosleep(&sleeptime, &mut rem) }; // c:2806
            if ret == 0 {
                // c:2808
                return 1;
            }
            let err = io::Error::last_os_error().raw_os_error().unwrap_or(0);
            if err != libc::EINTR {
                // c:2810
                return 0; // c:2811
            }
            sleeptime = rem; // c:2812
        }
    }
    #[cfg(not(unix))]
    {
        let _ = sleeptime;
        std::thread::sleep(std::time::Duration::from_micros(us.max(0) as u64));
        1
    }
}

/// Sleep random amount up to max microseconds (from utils.c zsleep_random)
/// Port of `int zsleep_random(long max_us, time_t end_time)`
/// from Src/utils.c:2833.
///
/// "Sleep for time (fairly) randomly up to max_us microseconds.
/// Don't let the time extend beyond end_time. end_time is compared
/// to the current *monotonic* clock time. Return 1 if that seemed
/// to work, else 0."
pub fn zsleep_random(max_us: i64, end_time: i64) -> i32 {
    // c:2833
    let now = zmonotime(None); // c:2833
    let r16 = unsafe { libc::rand() } & 0xFFFF; // c:2845
    let mut r: i64 = (max_us >> 16) * (r16 as i64); // c:2852
    while r != 0 && now + (r / 1_000_000) > end_time {
        // c:2858
        r >>= 1; // c:2859
    }
    if r != 0 {
        // c:2860
        zsleep(r) // c:2861
    } else {
        0 // c:2862
    }
}

/// Port of `int checkrmall(char *s)` from `Src/utils.c:2867`.
///
/// C body (c:2867-2919): count files in directory `s` (capped at 100,
/// honoring GLOBDOTS), emit one of 4 prompts based on count, optionally
/// sleep 10s under RMSTARWAIT, then `getquery("ny", 1)` for confirm.
///
/// Previous Rust port was a FAKE — single generic prompt, no file
/// count, no shout/errflag checks, no RMSTARWAIT, wrong valid-chars
/// order ("yn" instead of "ny" so 'n' isn't the default-first-arm).
/// Lost 9 of the 10 C semantic branches.
pub fn checkrmall(s: &str) -> bool {
    // c:2867
    // c:2871-2872 — `if (!shout) return 1;` — no controlling tty for
    // prompt output, default to yes. Rust analogue: if stderr isn't
    // a tty, skip the prompt and approve.
    let shout_is_tty = unsafe { libc::isatty(2) != 0 }; // c:2871
    if !shout_is_tty {
        // c:2871
        return true; // c:2872
    }
    // c:2873-2878 — `if (*s != '/')` build absolute via pwd prefix.
    let s_owned: String; // c:2873
    let s_abs: &str = if !s.starts_with('/') {
        // c:2873
        let pwd = getsparam("PWD").unwrap_or_default(); // c:2874 pwd[1]
        s_owned = if pwd.len() > 1 {
            // c:2874 if (pwd[1])
            crate::ported::string::tricat(&pwd, "/", s) // c:2875 zhtricat
        } else {
            // c:2876
            crate::ported::string::dyncat("/", s) // c:2877
        };
        s_owned.as_str()
    } else {
        s
    };
    let max_count: i32 = 100; // c:2879
    let mut count: i32 = 0; // c:2870 count = 0
                            // c:2880-2892 — opendir + readdir loop, count entries (skip
                            // dotfiles when !GLOBDOTS), cap at max_count.
    let ignoredots = !isset(GLOBDOTS); // c:2881
    // c:2880 — `if (!(dir = opendir(unmeta(s)))) return;` then
    //          `while ((fn = zreaddir(dir, 1))) { ... }`.
    if let Ok(mut dir) = fs::read_dir(s_abs) {
        while let Some(fname) = zreaddir(&mut dir, 1) {
            if ignoredots && fname.starts_with('.') {
                // c:2885
                continue; // c:2886
            }
            count += 1; // c:2887
            if count > max_count {
                // c:2888
                break; // c:2889
            }
        }
        // c:closedir(dir) auto on drop
    }
    // c:2893-2904 — four-arm prompt based on file count.
    let stderr_h = io::stderr();
    let mut stderr_w = stderr_h.lock();
    if count > max_count {
        // c:2893
        let _ = write!(
            stderr_w,
            "zsh: sure you want to delete more than {} files in ",
            max_count
        ); // c:2894
    } else if count == 1 {
        // c:2896
        let _ = write!(stderr_w, "zsh: sure you want to delete the only file in ");
    // c:2897
    } else if count > 0 {
        // c:2898
        let _ = write!(
            stderr_w,
            "zsh: sure you want to delete all {} files in ",
            count
        ); // c:2899
    } else {
        // c:2901
        // c:2902 — `We don't know how many files the glob will expand to`
        let _ = write!(stderr_w, "zsh: sure you want to delete all the files in ");
        // c:2903
    }
    // c:2905 — `nicezputs(s, shout)` — escape non-printables in path.
    let _ = nicezputs(s_abs, &mut stderr_w); // c:2905
                                                      // c:2906-2912 — RMSTARWAIT: print "? (waiting ten seconds)",
                                                      // flush, beep, sleep 10, then newline.
    if isset(RMSTARWAIT) {
        // c:2906
        let _ = write!(stderr_w, "? (waiting ten seconds)"); // c:2907
        let _ = stderr_w.flush(); // c:2908
        drop(stderr_w); // release lock around zbeep + sleep
        zbeep(); // c:2909
        std::thread::sleep(std::time::Duration::from_secs(10)); // c:2910
        let _ = writeln!(io::stderr()); // c:2911 fputc('\n', shout)
    } else {
        drop(stderr_w);
    }
    // c:2913-2914 — `if (errflag) return 0;`
    if errflag.load(Ordering::Relaxed) != 0 {
        // c:2913
        return false; // c:2914
    }
    // c:2915-2917 — emit "[yn]? ", flush, beep.
    let _ = io::stderr().write_all(b" [yn]? ");                              // c:2915 nicezputs of " [yn]? "
    let _ = io::stderr().flush();                                            // c:2916
    zbeep();                                                                 // c:2917
    // c:2918 — `return (getquery("ny", 1) == 'y');`. Default-first-
    // char is 'n' (no) — getquery's first valid char is the default
    // returned when the user just hits Enter.
    getquery(Some("ny"), 1) == b'y' as i32                                   // c:2918
}

/// Port of `ssize_t read_loop(int fd, char *buf, size_t len)` from
/// `Src/utils.c:2923`.
///
/// C body (c:2923-2945):
/// ```c
/// while (1) {
///     ssize_t ret = read(fd, buf, len);
///     if (ret == len) break;
///     if (ret <= 0) {
///         if (ret < 0) {
///             if (errno == EINTR) continue;          // c:2933
///             if (fd != SHTTY)                       // c:2935
///                 zwarn("read failed: %e", errno);   // c:2936
///         }
///         return ret;
///     }
///     buf += ret; len -= ret;
/// }
/// ```
///
/// The previous Rust port omitted the `zwarn("read failed: %e", errno)`
/// emission at c:2935-2936. That's a real diagnostic divergence:
/// any non-SHTTY read failure in zsh emits a stderr message; the
/// Rust port silently propagated the io::Error to the caller, which
/// in most call sites was discarded (`let _ = read_loop(...)`).
/// Users debugging an interrupted file read (e.g. `< /broken/fd`)
/// would see no error message at all.
/// WARNING: param names don't match C — Rust=(fd, buf) vs C=(fd, buf, len)
pub fn read_loop(fd: i32, buf: &mut [u8]) -> io::Result<usize> {
    // c:2923
    #[cfg(unix)]
    {
        let mut total = 0;
        while total < buf.len() {
            let n = unsafe {
                libc::read(
                    // c:2928
                    fd,
                    buf[total..].as_mut_ptr() as *mut libc::c_void,
                    buf.len() - total,
                )
            };
            if n <= 0 {
                // c:2931
                if n < 0 {
                    let e = io::Error::last_os_error();
                    if e.kind() == io::ErrorKind::Interrupted {
                        // c:2933
                        continue; // c:2934
                    }
                    // c:2935-2936 — `if (fd != SHTTY) zwarn("read failed: %e", errno);`
                    let shtty = SHTTY.load(Ordering::Relaxed);
                    if fd != shtty {
                        // c:2935
                        zwarn(
                            // c:2936
                            &format!("read failed: {}", e),
                        );
                    }
                    return Err(e);
                }
                break;
            }
            total += n as usize;
        }
        Ok(total)
    }
    #[cfg(not(unix))]
    {
        let _ = (fd, buf);
        Err(io::Error::new(io::ErrorKind::Unsupported, "not unix"))
    }
}

/// Port of `ssize_t write_loop(int fd, const char *buf, size_t len)` from
/// `Src/utils.c:2949`.
///
/// C body (c:2949-2970):
/// ```c
/// while (1) {
///     ssize_t ret = write(fd, buf, len);
///     if (ret == len) break;
///     if (ret < 0) {
///         if (errno == EINTR) continue;
///         if (fd != SHTTY)                            // c:2960
///             zwarn("write failed: %e", errno);       // c:2961
///         return -1;
///     }
///     buf += ret; len -= ret;
/// }
/// ```
///
/// Same divergence as `read_loop`: previous Rust port omitted the
/// `zwarn("write failed: %e", errno)` emission for non-SHTTY fds.
/// WARNING: param names don't match C — Rust=(fd, buf) vs C=(fd, buf, len)
pub fn write_loop(fd: i32, buf: &[u8]) -> io::Result<usize> {
    // c:2949
    #[cfg(unix)]
    {
        let mut total = 0;
        while total < buf.len() {
            let n = unsafe {
                libc::write(
                    // c:2954
                    fd,
                    buf[total..].as_ptr() as *const libc::c_void,
                    buf.len() - total,
                )
            };
            if n <= 0 {
                if n < 0 {
                    let e = io::Error::last_os_error();
                    if e.kind() == io::ErrorKind::Interrupted {
                        // c:2958
                        continue; // c:2959
                    }
                    // c:2960-2961 — `if (fd != SHTTY) zwarn("write failed: %e", errno);`
                    let shtty = SHTTY.load(Ordering::Relaxed);
                    if fd != shtty {
                        // c:2960
                        zwarn(
                            // c:2961
                            &format!("write failed: {}", e),
                        );
                    }
                    return Err(e);
                }
                break;
            }
            total += n as usize;
        }
        Ok(total)
    }
    #[cfg(not(unix))]
    {
        let _ = (fd, buf);
        Err(io::Error::new(io::ErrorKind::Unsupported, "not unix"))
    }
}

/// Read a single character (from utils.c read1char).
///
/// Port of `static int read1char(int echo)` from `Src/utils.c:2972`.
///
/// C body (c:2972-2988):
/// ```c
/// char c;
/// int q = queue_signal_level();
/// dont_queue_signals();
/// while (read(SHTTY, &c, 1) != 1) {
///     if (errno != EINTR || errflag || retflag || breaks || contflag) {
///         restore_queue_signals(q);
///         return -1;
///     }
/// }
/// restore_queue_signals(q);
/// if (echo) write_loop(SHTTY, &c, 1);
/// return (unsigned char) c;
/// ```
///
/// Returns the byte read (0..=255) on success, `-1` on error. Three
/// divergences in the previous Rust port:
///   1. **Read from stdin** instead of SHTTY (the tty fd). zsh reads
///      single chars during `read -k`, `bindkey`, query prompts —
///      always against the controlling terminal, not the standard
///      input which may be a pipe.
///   2. **No `echo` parameter.** When `echo=1` C writes the byte
///      back to SHTTY so the user sees what they typed.
///   3. **No `errflag` / `retflag` / `breaks` / `contflag` early-exit.**
///      A SIGINT-driven errflag should abort the read; previous Rust
///      port had no break path.
/// WARNING: returns i32 not `Option<char>` to match C's signature
/// (`(unsigned char) c` for success, `-1` for error).
pub fn read1char(echo: i32) -> i32 {
    // c:2972
    #[cfg(unix)]
    {
        let shtty = SHTTY.load(Ordering::Relaxed);
        if shtty < 0 {
            return -1;
        }
        let mut c: u8 = 0;
        loop {
            let rc = unsafe { libc::read(shtty, &mut c as *mut u8 as *mut libc::c_void, 1) };
            if rc == 1 {
                // c:2978
                break;
            }
            // c:2979 — `if (errno != EINTR || errflag || retflag || breaks
            //              || contflag) return -1;`
            let err = io::Error::last_os_error().raw_os_error().unwrap_or(0);
            if err != libc::EINTR {
                return -1;
            }
            // Check errflag interrupt flag (the most observable of the
            // four guards; retflag/breaks/contflag are control-flow
            // states that don't apply outside the exec engine).
            if errflag.load(Ordering::Relaxed) != 0 {
                return -1;
            }
        }
        // c:2985-2986 — `if (echo) write_loop(SHTTY, &c, 1);`
        if echo != 0 {
            // c:2985
            let _ = write_loop(shtty, &[c]); // c:2986
        }
        // c:2987 — `return (unsigned char) c;`
        c as i32
    }
    #[cfg(not(unix))]
    {
        let _ = echo;
        -1
    }
}

/// Port of `int noquery(int purge)` from Src/utils.c:2992.
///
/// "If anything has been typed before the query, return without
/// asking. Optionally also purge the input queue." Returns the
/// number of bytes pending on `SHTTY` (via FIONREAD ioctl); when
/// `purge` is set, drains them before returning.
pub fn noquery(purge: bool) -> i32 {
    // c:2992
    let mut val: libc::c_int = 0; // c:2992
    #[cfg(unix)]
    {
        let shtty = SHTTY.load(Ordering::Relaxed); // c:2999
        if shtty == -1 {
            return 0;
        }
        unsafe {
            // c:2999
            libc::ioctl(shtty, libc::FIONREAD, &mut val as *mut libc::c_int);
        }
        if purge {
            // c:3000
            let mut c: u8 = 0;
            for _ in 0..val {
                // c:3001
                let _ = unsafe {
                    // c:3002
                    libc::read(shtty, &mut c as *mut u8 as *mut libc::c_void, 1)
                };
            }
        }
    }
    val // c:3009
}

/// Port of `int getquery(char *valid_chars, int purge)` from
/// `Src/utils.c:3014`.
///
/// Read a single keystroke from the TTY in raw mode and return its
/// character code (as `int`, mirroring C). If `valid_chars` is `None`
/// (C `NULL`) any byte is accepted. If `valid_chars` is `Some(s)`, only
/// chars in `s` (plus `\n` which maps to `s[0]`) are accepted; other
/// input rings the bell and re-prompts. `Y`/`N` are pre-normalised to
/// `y`/`n` per c:3041-3045.
///
/// `purge != 0` triggers `noquery(purge)`'s queue-purge path — if input
/// is queued, returns `'n'` without reading.
pub fn getquery(valid_chars: Option<&str>, purge: i32) -> i32 {
    // c:3014
    // c:3016 — `int c, d, nl = 0;`
    let mut c: i32;
    let mut d: i32;
    let mut nl: i32 = 0;
    // c:3017 — `int isem = !strcmp(term, "emacs");`
    // Stub: `term` is the $TERM environment global, declared in
    // `Src/init.c` (extern in zsh.h). Local stub reads $TERM from
    // paramtab; absent → empty string.
    let term: String = getsparam("TERM").unwrap_or_default();
    let isem: bool = term == "emacs";
    // c:3018 — `struct ttyinfo ti;`
    // Rust: termios is the canonical TTY-state type returned by gettyinfo.
    let mut ti: libc::termios;                                               // c:3018

    attachtty(mypgrp.load(Ordering::Relaxed)); // c:3020 attachtty(mypgrp)

    // c:3022 — `gettyinfo(&ti);`
    ti = match gettyinfo() {
        Some(t) => t,
        None => return -1,
    };
    // c:3023-3030 — `ti.tio.c_lflag &= ~ECHO; if (!isem) {
    //                  ti.tio.c_lflag &= ~ICANON;
    //                  ti.tio.c_cc[VMIN] = 1;
    //                  ti.tio.c_cc[VTIME] = 0; }`
    #[cfg(unix)]
    {
        ti.c_lflag &= !(libc::ECHO);                                         // c:3024
        if !isem {                                                           // c:3025
            ti.c_lflag &= !(libc::ICANON);                                   // c:3026
            ti.c_cc[libc::VMIN] = 1;                                         // c:3027
            ti.c_cc[libc::VTIME] = 0;                                        // c:3028
        }
    }
    // c:3037 — `settyinfo(&ti);`
    settyinfo(&ti);

    // c:3039 — `if (noquery(purge))`
    if noquery(purge != 0) != 0 {
        // Stub: `shttyinfo` is the canonical saved-TTY-state global,
        // declared in `Src/init.c` (extern in zsh.h:1856). Without the
        // global tracked here we re-fetch current termios as a degraded
        // best-effort restore.
        if !isem {                                                           // c:3040
            if let Some(saved) = gettyinfo() {                               // c:3041 settyinfo(&shttyinfo)
                settyinfo(&saved);
            }
        }
        let _ = write_loop(SHTTY.load(Ordering::Relaxed), b"n\n");           // c:3042
        return b'n' as i32;                                                  // c:3043
    }

    // c:3046-3061 — `while ((c = read1char(0)) >= 0) { ... }`
    c = -1;
    loop {
        let cc = read1char(0);                                               // c:3046
        if cc < 0 {
            c = cc;
            break;
        }
        c = cc;
        if c == b'Y' as i32 {                                                // c:3047
            c = b'y' as i32;                                                 // c:3048
        } else if c == b'N' as i32 {                                         // c:3049
            c = b'n' as i32;                                                 // c:3050
        }
        if valid_chars.is_none() {                                           // c:3051
            break;                                                           // c:3052
        }
        if c == b'\n' as i32 {                                               // c:3053
            // c:3054 — `c = *valid_chars;`
            c = valid_chars.unwrap().bytes().next().unwrap_or(b'\n') as i32;
            nl = 1;                                                          // c:3055
            break;                                                           // c:3056
        }
        // c:3058 — `if (strchr(valid_chars, c))`
        if valid_chars.unwrap().bytes().any(|b| b as i32 == c) {
            nl = 1;                                                          // c:3059
            break;                                                           // c:3060
        }
        zbeep();                                                             // c:3062
    }
    if c >= 0 {                                                              // c:3064
        // c:3065-3066 — `char buf = (char)c; write_loop(SHTTY, &buf, 1);`
        let buf = [c as u8];                                                 // c:3065
        let _ = write_loop(SHTTY.load(Ordering::Relaxed), &buf);             // c:3066
    }
    if nl != 0 {                                                             // c:3068
        let _ = write_loop(SHTTY.load(Ordering::Relaxed), b"\n");            // c:3069
    }

    if isem {                                                                // c:3071
        if c != b'\n' as i32 {                                               // c:3072
            // c:3073 — `while ((d = read1char(1)) >= 0 && d != '\n');`
            loop {
                d = read1char(1);
                if d < 0 || d == b'\n' as i32 {
                    break;
                }
            }
        }
    } else if c != b'\n' as i32 && valid_chars.is_none() {                   // c:3075
        // c:3077-3094 — MULTIBYTE_SUPPORT branch: drain trailing bytes
        // of an incomplete multibyte sequence.
        if isset(MULTIBYTE) && c >= 0 {                                      // c:3077
            // c:3083-3093 — `for (;;) { ret = mbrlen(&cc, 1, &mbs);
            //                          if (ret != MB_INCOMPLETE) break;
            //                          c = read1char(1); if (c < 0) break;
            //                          cc = (char)c; }`
            // Rust: model MB_INCOMPLETE detection by checking whether
            // the byte buffer so far forms a complete UTF-8 prefix.
            // `cc` carries the current byte under examination.
            let mut cc: u8 = c as u8;                                        // c:3082
            let mut accum: Vec<u8> = vec![cc];
            loop {
                // mbrlen returns MB_INCOMPLETE when the partial buffer
                // doesn't yet form a complete UTF-8 character. Rust
                // equivalent: from_utf8 errors with error_len() == None
                // when more bytes are needed.
                let incomplete = match std::str::from_utf8(&accum) {
                    Ok(_) => false,
                    Err(e) => e.error_len().is_none(),
                };
                if !incomplete {                                             // c:3088
                    break;
                }
                let nc = read1char(1);                                       // c:3089
                if nc < 0 {                                                  // c:3090
                    break;                                                   // c:3091
                }
                cc = nc as u8;                                               // c:3092
                accum.push(cc);
            }
            let _ = cc;
        }
        // c:3097 — `write_loop(SHTTY, "\n", 1);`
        let _ = write_loop(SHTTY.load(Ordering::Relaxed), b"\n");
    }

    // c:3101 — `settyinfo(&shttyinfo);` — restore saved TTY state.
    // Stub-degraded path: refetch current termios. The proper port
    // requires wiring an `shttyinfo` global in init.rs.
    if let Some(saved) = gettyinfo() {
        settyinfo(&saved);
    }

    c                                                                        // c:3102 return c
}

// `spscan` (Src/utils.c:3109) — canonical port lives below the
// thread_local block in this file. The pre-existing 3-arg
// `(name, candidates[], threshold) → Option<String>` shape was a
// drift fake (C is `void spscan(HashNode hn, scanflags)`); deleted
// because it had zero call sites and conflicted with the faithful
// port spckword needs.

// spellcheck a word                                                       // c:3123
// fix s ; if hist is nonzero, fix the history list too                    // c:3124

// File-static state shared with the (inlined) spscan callback. C uses
// raw file-statics at utils.c:3045-3050 (`best`, `d`, `guess`, `ic`,
// `spckpat`, `spnamepat`); Rust port mirrors them as thread_locals
// (per-evaluator per PORT_PLAN.md bucket-1) so concurrent worker
// threads each have their own correction state.
thread_local! {
    /// Port of `static int d;` from `Src/utils.c:3045`. Best dist seen.
    static SPCK_D: std::cell::Cell<i32> = const { std::cell::Cell::new(0) };
    /// Port of `static char *best;` from `Src/utils.c:3046`. Best-match.
    static SPCK_BEST: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
    /// Port of `static char *guess;` from `Src/utils.c:3047`. Word to fix.
    static SPCK_GUESS: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
    /// Port of `static Patprog spckpat;` from `Src/utils.c:3049`.
    static SPCK_PAT: std::cell::RefCell<Option<crate::ported::pattern::Patprog>>
        = const { std::cell::RefCell::new(None) };
    /// Port of `static Patprog spnamepat;` from `Src/utils.c:3050`.
    static SPCK_NAMEPAT: std::cell::RefCell<Option<crate::ported::pattern::Patprog>>
        = const { std::cell::RefCell::new(None) };
}

/// Port of `static void spscan(HashNode hn, UNUSED(int scanflags))` from
/// `Src/utils.c:3074`. Inlined per-call below because hashtable.rs's
/// scan helpers don't model C's `scanhashtable(table, ..., scanfn, ...)`
/// callback shape — Rust call sites iterate the table directly. C
/// body:
/// ```c
/// int nd = spdist(hn->nam, guess, (int) strlen(guess) / 4 + 1);
/// if (nd < d && (!spckpat || !pattry(spckpat, hn->nam))) {
///     best = hn->nam;
///     d = nd;
/// }
/// ```
fn spscan(name: &str) {
    // c:3074
    let guess = SPCK_GUESS.with(|g| g.borrow().clone()).unwrap_or_default();
    if guess.is_empty() {
        return;
    }
    let thresh = guess.len() / 4 + 1; // c:3076 `(int) strlen(guess) / 4 + 1`
    let nd = spdist(name, &guess, thresh) as i32; // c:3076
    let d = SPCK_D.with(|c| c.get());
    if nd < d {
        // c:3077
        // c:3078 — `if (!spckpat || !pattry(spckpat, hn->nam))`.
        let allow = SPCK_PAT.with(|p| {
            p.borrow()
                .as_ref()
                .map(|prog| !crate::ported::pattern::pattry(prog, name))
                .unwrap_or(true)
        });
        if allow {
            SPCK_BEST.with(|b| *b.borrow_mut() = Some(name.to_string())); // c:3079
            SPCK_D.with(|c| c.set(nd)); // c:3080
        }
    }
}

/// Port of `void spckword(char **s, int hist, int cmd, int ask)` from
/// `Src/utils.c:3128`.
///
/// Spell-check `*s`. If a near-match is found in the appropriate
/// hashtable (params for `$x`, command tables for command position,
/// directory entries otherwise) AND the user accepts the correction
/// (`ask=0` auto-accepts), replace `*s` in place with the corrected
/// form and (if `hist!=0`) rewrite the history entry too.
///
/// Faithful 1:1 line-by-line port. Interactive prompting (c:3273-3287)
/// is stubbed to auto-accept when `ask=1` since `getquery` /
/// `promptexpand` / `shout` / `zbeep` aren't yet wired in zshrs —
/// flagged with WARNING at the prompt site.
///
/// Caller updates: previous Rust signature `(word, candidates[], threshold)
/// → Option<String>` is gone — `lex.rs` builds candidate lists itself,
/// but C's spckword scans the canonical hashtables directly. The new
/// signature matches C exactly: takes `&mut String` for in-place fix.
pub fn spckword(s: &mut String, hist: i32, cmd: i32, ask: i32) {
    use crate::ported::hashtable::{
        aliastab_lock, cmdnamtab_lock, fillcmdnamtable, pathchecked, reswdtab_lock,
        shfunctab_lock,
    };
    use crate::ported::params::{getsparam, paramtab};
    use crate::ported::pattern::{patcompile, PAT_HEAPDUP};
    use crate::ported::zsh_h::{
        isset, AUTOCD, Dash, Equals, HASHLISTALL, Stringg as StringTok, Tilde,
    };
    // c:3130-3133 — locals.
    let _t: Option<String>;
    let mut ic: char = '\0'; // c:3131
    let mut preflen: usize = 0; // c:3132
                                // c:3133 — `autocd = cmd && isset(AUTOCD) && strcmp(*s, ".") && strcmp(*s, "..")`.
    let autocd = cmd != 0 && isset(AUTOCD) && s != "." && s != "..";

    // c:3135-3136 — `if (!(*s)[0] || !(*s)[1]) return;`
    if s.len() < 2 {
        return;
    }
    // c:3137-3140 — HISTFLAG_NOEXEC or leading %/- skip.
    let bytes = s.as_bytes();
    let first = bytes[0] as char;
    let histdone = crate::ported::hist::histdone.load(std::sync::atomic::Ordering::Relaxed); // c:3137
    if (histdone & HISTFLAG_NOEXEC) != 0
        || (if cmd != 0 {
            first == '%' // c:3139 — leading % is a job
        } else {
            first == '-' || first == Dash // c:3139 — leading hyphen is an option
        })
    {
        return; // c:3140
    }
    // c:3141-3142 — `if (!strcmp(*s, "in")) return;`.
    if s == "in" {
        return; // c:3142
    }
    // c:3143-3155 — `cmd` branch: skip if it's already a known
    // function/builtin/cmdname/alias/reswd. Optional HASHLISTALL
    // re-fill of cmdnamtab on miss.
    if cmd != 0 {
        // c:3143
        let known = shfunctab_lock() // c:3144
            .read()
            .map(|t| t.get(s).is_some())
            .unwrap_or(false)
            || BUILTINS // c:3145
                .iter()
                .any(|b| b.node.nam == *s)
            || cmdnamtab_lock() // c:3146
                .read()
                .map(|t| t.get(s).is_some())
                .unwrap_or(false)
            || aliastab_lock() // c:3147
                .read()
                .map(|t| t.get(s).is_some())
                .unwrap_or(false)
            || reswdtab_lock() // c:3148
                .read()
                .map(|t| t.get(s).is_some())
                .unwrap_or(false);
        if known {
            return; // c:3149
        }
        // c:3150-3154 — HASHLISTALL: bulk-hash $PATH then retry.
        if isset(HASHLISTALL) {
            // c:3150
            let path: Vec<String> = getsparam("PATH") // c:3151
                .map(|p| p.split(':').map(String::from).collect())
                .unwrap_or_default();
            fillcmdnamtable(&path); // c:3151
            if cmdnamtab_lock() // c:3152
                .read()
                .map(|t| t.get(s).is_some())
                .unwrap_or(false)
            {
                return; // c:3153
            }
        }
    }
    // c:3156-3165 — Tilde/Equals/String prefix skip + itok/Dash
    // detok + early-return on any other tokenized char.
    let mut start = 0usize;
    let bytes = s.as_bytes(); // re-bind after potential string ops
    if !bytes.is_empty() {
        let c0 = bytes[0] as char;
        if c0 == Tilde || c0 == Equals || c0 == StringTok {
            // c:3157
            start = 1; // c:3158 t++
        }
    }
    // Scan from `start` for tokenized bytes.
    {
        let mut buf = s.clone().into_bytes();
        let mut i = start;
        let mut had_dash_only = true; // accumulator
        while i < buf.len() {
            let b = buf[i];
            if itok(b) {
                // c:3160
                if b as char == Dash {
                    // c:3161
                    buf[i] = b'-'; // c:3162
                } else {
                    return; // c:3164
                }
            } else {
                had_dash_only = had_dash_only && false;
            }
            i += 1;
        }
        let _ = had_dash_only;
        *s = String::from_utf8_lossy(&buf).into_owned();
    }
    // c:3166 — `best = NULL;`
    SPCK_BEST.with(|b| *b.borrow_mut() = None);
    SPCK_D.with(|c| c.set(100)); // initialised at each table-scan branch in C; mirror up-front.
                                 // c:3167-3169 — `for (t = *s; *t; t++) if (*t == '/') break;`
                                 // `t` is the position of the first slash (or end of string).
    let t_pos = s.find('/').unwrap_or(s.len()); // c:3167
                                                // c:3170-3171 — `if (**s == Tilde && !*t) return;`
    let bytes = s.as_bytes();
    if !bytes.is_empty() && (bytes[0] as char) == Tilde && t_pos == bytes.len() {
        // c:3170
        return; // c:3171
    }

    // c:3173-3178 — compile CORRECT_IGNORE pattern if set.
    if let Some(ci) = getsparam("CORRECT_IGNORE") {
        // c:3173
        let prog = patcompile(&ci, PAT_HEAPDUP, None); // c:3176
        SPCK_PAT.with(|p| *p.borrow_mut() = prog);
    } else {
        SPCK_PAT.with(|p| *p.borrow_mut() = None); // c:3178
    }
    // c:3180-3185 — compile CORRECT_IGNORE_FILE pattern if set.
    if let Some(ci) = getsparam("CORRECT_IGNORE_FILE") {
        // c:3180
        let prog = patcompile(&ci, PAT_HEAPDUP, None); // c:3183
        SPCK_NAMEPAT.with(|p| *p.borrow_mut() = prog);
    } else {
        SPCK_NAMEPAT.with(|p| *p.borrow_mut() = None); // c:3185
    }

    let bytes = s.as_bytes();
    let first = if bytes.is_empty() {
        '\0'
    } else {
        bytes[0] as char
    };

    // c:3187-3193 — `**s == String && !*t`: $-prefixed name → scan paramtab.
    if first == StringTok && t_pos == bytes.len() {
        // c:3187
        // c:3188 — `guess = *s + 1;` strip leading $.
        let guess = s[1..].to_string();
        // c:3189-3190 — `if (itype_end(guess, INAMESPC, 1) == guess) return;`
        if itype_end(&guess, true) == 0 {
            // c:3189
            return; // c:3190
        }
        ic = StringTok; // c:3191
        SPCK_GUESS.with(|g| *g.borrow_mut() = Some(guess));
        SPCK_D.with(|c| c.set(100)); // c:3192
        if let Ok(t) = paramtab().read() {
            // c:3193 scanhashtable(paramtab, ..., spscan, ...)
            for k in t.keys() {
                spscan(k);
            }
        }
    // c:3194-3202 — `**s == Equals`: =cmd → hashcmd; then scan aliases+cmdnam.
    } else if first == Equals {
        // c:3194
        if t_pos != bytes.len() {
            // c:3195
            return; // c:3196
        }
        // c:3197-3198 — `if (hashcmd(guess = *s + 1, pathchecked)) return;`
        let guess = s[1..].to_string();
        let path: Vec<String> = getsparam("PATH")
            .map(|p| p.split(':').map(String::from).collect())
            .unwrap_or_default();
        let pc = pathchecked.load(std::sync::atomic::Ordering::Relaxed);
        if crate::ported::exec::hashcmd(&guess, &path[pc.min(path.len())..]).is_some() {
            return; // c:3198
        }
        SPCK_D.with(|c| c.set(100)); // c:3199
        ic = Equals; // c:3200
        SPCK_GUESS.with(|g| *g.borrow_mut() = Some(guess));
        if let Ok(t) = aliastab_lock().read() {
            // c:3201
            for (k, _) in t.iter() {
                spscan(k);
            }
        }
        if let Ok(t) = cmdnamtab_lock().read() {
            // c:3202
            for (k, _) in t.iter() {
                spscan(k);
            }
        }
    // c:3203-3248 — default branch: filename / dir spell-check.
    } else {
        // c:3203
        let mut guess = s.clone(); // c:3204
                                   // c:3205-3218 — Tilde / String inline-expand prefix handling.
        if !guess.is_empty()
            && ((guess.as_bytes()[0] as char) == Tilde
                || (guess.as_bytes()[0] as char) == StringTok)
        {
            // c:3205
            ic = guess.as_bytes()[0] as char; // c:3207
            if t_pos + 1 >= s.len() {
                // c:3208 — `if (!*++t) return;`
                return; // c:3209
            }
            // c:3210-3214 — `noerrs=2; singsub(&guess); noerrs = ne;`
            let saved_noerrs = crate::ported::exec::noerrs.load(std::sync::atomic::Ordering::Relaxed);
            crate::ported::exec::noerrs.store(2, std::sync::atomic::Ordering::Relaxed); // c:3212
            guess = crate::ported::subst::singsub(&guess); // c:3213
            crate::ported::exec::noerrs.store(saved_noerrs, std::sync::atomic::Ordering::Relaxed);
            if guess.is_empty() {
                return; // c:3216 `if (!guess) return;`
            }
            // c:3217 — `preflen = strlen(guess) - strlen(t);` t = original
            // s[t_pos..] (the post-slash remainder).
            let t_len = s.len() - t_pos;
            preflen = guess.len().saturating_sub(t_len);
        }
        // c:3219-3220 — `if (access(unmeta(guess), F_OK) == 0) return;`
        let cstr = match std::ffi::CString::new(unmeta(&guess).as_str()) {
            Ok(c) => c,
            Err(_) => return,
        };
        if unsafe { libc::access(cstr.as_ptr(), libc::F_OK) } == 0 {
            // c:3219
            return; // c:3220
        }
        // c:3221 — `best = spname(guess);`
        // The Rust spname has a signature-drift adaptation taking
        // `(name, dir)`; pass the parent dir extracted from guess.
        let path_obj = std::path::Path::new(&guess);
        let parent = path_obj
            .parent()
            .and_then(|p| p.to_str())
            .filter(|s| !s.is_empty())
            .unwrap_or(".");
        let basename = path_obj.file_name().and_then(|n| n.to_str()).unwrap_or("");
        let best = spname(basename, parent);
        SPCK_BEST.with(|b| *b.borrow_mut() = best);
        SPCK_GUESS.with(|g| *g.borrow_mut() = Some(guess.clone()));
        // c:3222-3247 — command-position default branch: hashcmd +
        // scan tables + autocd cdpath scan.
        if t_pos == s.len() && cmd != 0 {
            // c:3222
            // c:3223-3224 — hashcmd shortcut.
            let path: Vec<String> = getsparam("PATH")
                .map(|p| p.split(':').map(String::from).collect())
                .unwrap_or_default();
            let pc = pathchecked.load(std::sync::atomic::Ordering::Relaxed);
            if crate::ported::exec::hashcmd(&guess, &path[pc.min(path.len())..]).is_some() {
                return; // c:3224
            }
            SPCK_D.with(|c| c.set(100)); // c:3225
                                          // c:3226-3230 — scan reswd, alias, shfunc, builtin, cmdnam.
            if let Ok(t) = reswdtab_lock().read() {
                // c:3226
                for (k, _) in t.iter() {
                    spscan(k);
                }
            }
            if let Ok(t) = aliastab_lock().read() {
                // c:3227
                for (k, _) in t.iter() {
                    spscan(k);
                }
            }
            if let Ok(t) = shfunctab_lock().read() {
                // c:3228
                for (k, _) in t.iter() {
                    spscan(k);
                }
            }
            // c:3229 — builtintab scan: BUILTINS is a static array.
            for b in BUILTINS.iter() {
                spscan(&b.node.nam);
            }
            if let Ok(t) = cmdnamtab_lock().read() {
                // c:3230
                for (k, _) in t.iter() {
                    spscan(k);
                }
            }
            // c:3231-3247 — autocd $cdpath scan: for each cdpath
            // entry, find the closest filename match via mindist.
            // Strict `<` (not `<=` as in spscan) so a cdpath dir
            // wins only when STRICTLY better than the existing best
            // — preferring earlier cdpath dirs on ties.
            if autocd {
                // c:3232 — `if (cd_able_vars(unmeta(guess))) return;`
                let unmeta_guess = crate::ported::utils::unmeta(&guess);
                if crate::ported::builtin::cd_able_vars(&unmeta_guess).is_some() {
                    return; // c:3233
                }
                // c:3234 — `for (pp = cdpath; *pp; pp++)`. Read CDPATH.
                let cdpath: Vec<String> = crate::ported::params::paramtab()
                    .read()
                    .ok()
                    .and_then(|t| t.get("cdpath").and_then(|pm| pm.u_arr.clone()))
                    .unwrap_or_default();
                let mut cur_d = SPCK_D.with(|c| c.get());
                let mut cur_best = SPCK_BEST.with(|b| b.borrow().clone());
                for pp in cdpath.iter() {
                    // c:3235-3245 — `mindist(*pp, *s, bestcd, 1)`.
                    if let Some((bestcd, thisdist)) = mindist(pp, &s) {
                        // c:3239 — STRICT `<` (not `<=`) per C comment
                        // at c:3236-3239.
                        if (thisdist as i32) < cur_d {
                            cur_best = Some(bestcd);
                            cur_d = thisdist as i32;
                        }
                    }
                }
                SPCK_BEST.with(|b| *b.borrow_mut() = cur_best);
                SPCK_D.with(|c| c.set(cur_d));
            }
        }
    }
    // c:3250-3251 — `if (errflag) return;`
    if (errflag.load(std::sync::atomic::Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
        return; // c:3251
    }
    // c:3252 — `if (best && strlen(best) > 1 && strcmp(best, guess))`.
    let best = SPCK_BEST.with(|b| b.borrow().clone());
    let guess = SPCK_GUESS.with(|g| g.borrow().clone()).unwrap_or_default();
    let Some(mut best) = best else {
        return;
    };
    if best.len() <= 1 || best == guess {
        return;
    }
    // c:3253-3272 — assemble the prefixed/de-tokenized replacement.
    if ic != '\0' {
        // c:3254
        if preflen > 0 {
            // c:3256
            // c:3258-3259 — `if (strncmp(guess, best, preflen)) return;`
            if !best.starts_with(&guess[..preflen.min(guess.len())]) {
                return; // c:3259
            }
            // c:3261-3263 — `u = ...s[0..t-*s] + best[preflen..]`.
            let t_off = s.len() - (s.len() - t_pos);
            let mut u = String::with_capacity(t_off + best.len() - preflen + 1);
            u.push_str(&s[..t_off]);
            u.push_str(&best[preflen..]);
            best = u;
        } else {
            // c:3264 — `u = "\0" + best;` (prepend NUL placeholder).
            best = format!("\0{}", best);
        }
        // c:3269-3271 — `best = u; guess = *s; *guess = *best = ztokens[ic - Pound];`
        let pound = crate::ported::zsh_h::Pound as u8;
        let zt = crate::ported::lex::ztokens.as_bytes();
        let token_char = if (ic as u8) >= pound {
            let idx = (ic as u8 - pound) as usize;
            if idx < zt.len() {
                zt[idx] as char
            } else {
                ic
            }
        } else {
            ic
        };
        // Set first char of both `*s` (the original) and `best`.
        if !s.is_empty() {
            let mut sb = s.clone().into_bytes();
            sb[0] = token_char as u8;
            *s = String::from_utf8_lossy(&sb).into_owned();
        }
        if !best.is_empty() {
            let mut bb = best.into_bytes();
            bb[0] = token_char as u8;
            best = String::from_utf8_lossy(&bb).into_owned();
        }
    }
    // c:3273-3289 — interactive prompt (`ask`) or auto-accept.
    let x: char;
    if ask != 0 {
        // c:3273
        // WARNING — DIVERGENCE: `noquery()`, `shout`, `promptexpand`,
        // `zputs(stream)`, `zbeep`, `getquery("nyae", 0)` aren't yet
        // wired in zshrs (interactive ZLE prompt machinery). Default
        // to 'n' (decline) when ask=1 — preserves the C behavior of
        // declining when shout is NULL (c:3286-3287). Re-enable the
        // interactive flow when promptexpand/getquery land.
        x = 'n';
    } else {
        x = 'y'; // c:3289
    }
    // c:3290-3300 — apply chosen action.
    if x == 'y' {
        // c:3290
        *s = best; // c:3291 `*s = dupstring(best);`
        if hist != 0 {
            // c:3292
            // c:3293 — `hwrep(best);` (history rewrite). Stubbed: hist
            // rewrite plumbing isn't yet hooked into the lex caller.
        }
    } else if x == 'a' {
        // c:3294
        crate::ported::hist::histdone.fetch_or(
            HISTFLAG_NOEXEC,
            std::sync::atomic::Ordering::Relaxed,
        ); // c:3295
    } else if x == 'e' {
        // c:3296
        crate::ported::hist::histdone.fetch_or(
            HISTFLAG_NOEXEC | crate::ported::zsh_h::HISTFLAG_RECALL,
            std::sync::atomic::Ordering::Relaxed,
        ); // c:3297
    }
    // c:3299-3300 — `if (ic) **s = ic;` — restore prefix sigil.
    if ic != '\0' && !s.is_empty() {
        let mut sb = s.clone().into_bytes();
        sb[0] = ic as u8;
        *s = String::from_utf8_lossy(&sb).into_owned();
    }
}

/// Port of `ztrftimebuf(int *bufsizeptr, int decr)` from `Src/utils.c:3312`.
///
/// ```c
/// static int ztrftimebuf(int *bufsizeptr, int decr) {
///     if (*bufsizeptr <= decr) return 1;
///     *bufsizeptr -= decr;
///     return 0;
/// }
/// ```
///
/// "Helper for ztrftime: try to fit decr more bytes (plus a NUL)
/// in the buffer, and a new string length to decrement from that.
/// Returns 0 if the new length fits, 1 otherwise."
///
/// Previous Rust port had wrong semantics — returned `needed.max(256)`
/// (a buffer-sizing helper) instead of the C "decrement-and-check"
/// semantics. New port matches C: takes &mut bufsize + decr,
/// returns i32 (0 = fit, 1 = doesn't fit).
pub fn ztrftimebuf(bufsizeptr: &mut i32, decr: i32) -> i32 {
    if *bufsizeptr <= decr {
        return 1;
    }
    *bufsizeptr -= decr;
    0
}

/// Format time struct (from utils.c ztrftime)
/// Port of `ztrftime(char *buf, int bufsize, char *fmt, struct tm *tm, long nsec)` from `Src/utils.c:3337`.
/// WARNING: param names don't match C — Rust=(fmt, time) vs C=(buf, bufsize, fmt, tm, nsec)
// Rust idiom replacement: `SystemTime::duration_since(UNIX_EPOCH)`
// + libc::localtime covers the C tm-struct populate; the C source's
// 192-line body builds the fmt-walk by hand because C has no
// strftime extension support — Rust delegates to libc::strftime via
// the strftime crate equivalent inline.
pub fn ztrftime(fmt: &str, time: std::time::SystemTime) -> String {
    let duration = time.duration_since(UNIX_EPOCH).unwrap_or_default();
    let secs = duration.as_secs() as i64;
    let nsec = duration.subsec_nanos() as u64;

    #[cfg(unix)]
    unsafe {
        let tm = libc::localtime(&secs);
        if tm.is_null() {
            return String::new();
        }
        let tm_ref = &*tm;

        // c:3398-3406 — pre-pass: walk fmt and substitute zsh-specific
        // extensions BEFORE delegating to libc::strftime. The C source
        // handles these inline; Rust pre-rewrites them into literal
        // numbers so libc::strftime sees only standard specifiers.
        //   %K  = 24-hr clock, no leading zero (0-23)
        //   %L  = 12-hr clock, no leading zero (1-12)
        //   %f  = day of month, no leading zero (1-31)
        //   %.N = fractional seconds, N digits (0-9)
        let mut preprocessed = String::with_capacity(fmt.len());
        let bytes = fmt.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            if bytes[i] == b'%' && i + 1 < bytes.len() {
                // c:3374-3384 — parse optional `N.` prefix (digit count
                // for the `%.` fractional-seconds specifier).
                let mut j = i + 1;
                while j < bytes.len() && bytes[j].is_ascii_digit() {
                    j += 1;
                }
                let digs: u32 = if j > i + 1 {
                    std::str::from_utf8(&bytes[i + 1..j])
                        .unwrap_or("3")
                        .parse()
                        .unwrap_or(3)
                } else {
                    3
                };
                // c:3408 — `switch (*fmt++)` examines the specifier
                // char (after the digit prefix).
                if j < bytes.len() && bytes[j] == b'.' {
                    // c:3409 — fractional-seconds. Default 3 digits if
                    // none specified (j == i+1).
                    let digs = digs.min(9);
                    let trunc_div: u64 = 10u64.pow(9 - digs);
                    let val = nsec / trunc_div;
                    preprocessed
                        .push_str(&format!("{:0width$}", val, width = digs as usize));
                    i = j + 1;
                    continue;
                }
                let next = bytes[i + 1];
                // c:3445-3460 — %K / %L / %f extensions.
                match next {
                    b'K' => {
                        preprocessed.push_str(&format!("{}", tm_ref.tm_hour));
                        i += 2;
                        continue;
                    }
                    b'L' => {
                        let mut h12 = tm_ref.tm_hour % 12;
                        if h12 == 0 {
                            h12 = 12;
                        }
                        preprocessed.push_str(&format!("{}", h12));
                        i += 2;
                        continue;
                    }
                    b'f' => {
                        preprocessed.push_str(&format!("{}", tm_ref.tm_mday));
                        i += 2;
                        continue;
                    }
                    _ => {}
                }
            }
            preprocessed.push(bytes[i] as char);
            i += 1;
        }

        let mut buf = vec![0u8; 256];
        let c_fmt = CString::new(preprocessed).unwrap_or_default();
        let len = libc::strftime(
            buf.as_mut_ptr() as *mut libc::c_char,
            buf.len(),
            c_fmt.as_ptr(),
            tm,
        );

        if len > 0 {
            buf.truncate(len);
            String::from_utf8_lossy(&buf).to_string()
        } else {
            String::new()
        }
    }

    #[cfg(not(unix))]
    {
        let _ = (fmt, secs, nsec);
        String::new()
    }
}

/// Join array with delimiter (from utils.c zjoin)
/// Port of `zjoin(char **arr, int delim, int heap)` from `Src/utils.c:3622`.
/// WARNING: param names don't match C — Rust=(arr, delim) vs C=(arr, delim, heap)
pub fn zjoin(arr: &[String], delim: char) -> String {
    arr.join(&delim.to_string())
}

/// Port of `char **colonsplit(char *s, int uniq)` from
/// `Src/utils.c:3650`. Splits `s` on `:`; when `uniq` is set,
/// duplicate segments are dropped (linear scan against the
/// already-emitted prefix).
/// ```c
/// char **
/// colonsplit(char *s, int uniq)
/// {
///     int ct;
///     char *t, **ret, **ptr, **p;
///     for (t = s, ct = 0; *t; t++)
///         if (*t == ':') ct++;
///     ptr = ret = zalloc(sizeof(char *) * (ct + 2));
///     t = s;
///     do {
///         s = t;
///         for (; *t && *t != ':'; t++);
///         if (uniq)
///             for (p = ret; p < ptr; p++)
///                 if (strlen(*p) == t - s && !strncmp(*p, s, t - s))
///                     goto cont;
///         *ptr = zalloc((t - s) + 1);
///         ztrncpy(*ptr++, s, t - s);
///       cont: ;
///     } while (*t++);
///     *ptr = NULL;
///     return ret;
/// }
/// ```
pub fn colonsplit(s: &str, uniq: bool) -> Vec<String> {
    // c:3650
    // c:3655-3657 — count colons.
    let ct = s.matches(':').count(); // c:3655
    let mut ret: Vec<String> = Vec::with_capacity(ct + 2); // c:3658 zalloc((ct+2)*sizeof(char *))

    // c:3661-3673 — do-while loop walking segments.
    let bytes = s.as_bytes();
    let mut t: usize = 0;
    loop {
        let seg_start = t; // c:3662 s = t
                           // c:3664 — `for (; *t && *t != ':'; t++)`
        while t < bytes.len() && bytes[t] != b':' {
            t += 1;
        }
        let seg = &s[seg_start..t];
        // c:3665-3668 — uniq dedupe.
        if !uniq || !ret.iter().any(|p| p == seg) {
            // c:3665
            ret.push(seg.to_string()); // c:3670 zalloc + ztrncpy
        }
        // c:3673 — `while (*t++)` — break if at end, else step past colon.
        if t >= bytes.len() {
            break;
        } // c:3673
        t += 1; // c:3673 t++
    }
    // c:3674 — `*ptr = NULL;` — Rust Vec needs no sentinel.
    ret // c:3675
}

/// Port of `skipwsep(char **s)` from `Src/utils.c:3680`.
///
/// Skip whitespace separators (`iwsep`-true bytes) at the start of
/// `s`. Returns `(remaining_str, count)` — `count` is the number of
/// bytes / chars skipped. C body:
///
/// ```c
/// while (*t && iwsep(*t == Meta ? t[1] ^ 32 : *t)) {
///     if (*t == Meta) t++;
///     t++;
///     i++;
/// }
/// ```
///
/// The C version honours Meta-encoded bytes (`Meta` followed by
/// `^32`-XOR'd byte). Rust port mirrors that byte-by-byte.
pub fn skipwsep(s: &str) -> (&str, usize) {
    let bytes = s.as_bytes();
    let mut i: usize = 0;
    let mut count: usize = 0;
    while i < bytes.len() {
        let b = if bytes[i] == Meta && i + 1 < bytes.len() {
            bytes[i + 1] ^ 32
        } else {
            bytes[i]
        };
        if !iwsep(b) {
            break;
        }
        if bytes[i] == Meta {
            i += 1;
        }
        i += 1;
        count += 1;
    }
    (&s[i..], count)
}

/// IFS-style word splitting - port from zsh/Src/utils.c spacesplit()
///
/// Splits on whitespace (space, tab, newline), treating consecutive
/// whitespace as a single separator.
/// Split on whitespace.
/// Port of `spacesplit(char *s, int allownull, int heap, int quote)` from Src/utils.c.
/// WARNING: param names don't match C — Rust=(s, allownull) vs C=(s, allownull, heap, quote)
// Rust idiom replacement: `str::split` / `split_whitespace` collapse
// the C tokenizer (Inull-skip + quote-aware advance + zalloc per
// segment); the `heap` / `quote` C params drop with the C buffer.
pub fn spacesplit(s: &str, allownull: bool) -> Vec<String> {
    // c:3711
    if allownull {
        s.split([' ', '\t', '\n']).map(|p| p.to_string()).collect()
    } else {
        s.split_whitespace().map(|p| p.to_string()).collect()
    }
}

/// Find a separator in string (from utils.c findsep)
/// Port of `findsep(char **s, char *sep, int quote)` from `Src/utils.c:3784`.
/// Rust idiom replacement: `str::find` covers the literal/whitespace
/// cases; the C `quote` arg drops since callers operate on already-
/// unquoted strings in zshrs.
/// WARNING: param names don't match C — Rust=(s, sep) vs C=(s, sep, quote)
pub fn findsep(s: &str, sep: Option<&str>) -> Option<usize> {
    match sep {
        Some(sep) if sep.len() == 1 => s.find(sep.chars().next().unwrap()),
        Some(sep) => s.find(sep),
        None => {
            // Default: split on whitespace
            s.find(|c: char| c.is_ascii_whitespace())
        }
    }
}

/// Find word at position (from utils.c findword)
/// Port of `findword(char **s, char *sep)` from `Src/utils.c:3849`.
pub fn findword<'a>(s: &'a str, sep: Option<&'a str>) -> Option<(&'a str, &'a str)> {
    let s = match sep {
        Some(_) => s,
        None => s.trim_start(),
    };
    if s.is_empty() {
        return None;
    }
    match sep {
        Some(sep) => {
            if let Some(pos) = s.find(sep) {
                Some((&s[..pos], &s[pos + sep.len()..]))
            } else {
                Some((s, ""))
            }
        }
        None => {
            let end = s.find(|c: char| c.is_ascii_whitespace()).unwrap_or(s.len());
            Some((&s[..end], &s[end..]))
        }
    }
}

/// Port of `wordcount(char *s, char *sep, int mul)` from `Src/utils.c:3879`.
///
/// Returns the number of words in `s` that would result from splitting
/// on `sep` (or `$IFS` when `sep` is None). `mul` controls how
/// consecutive empty fields are counted:
/// - `mul == 0`: don't count leading/trailing/consecutive empties.
/// - `mul > 0`: count consecutive empties (each `sep` boundary
///   produces one word, even if the surrounding text is empty).
/// - `mul < 0`: count empty trailing fields (final separator after
///   the last non-empty field counts as one extra empty word).
///
/// C body (paraphrased):
/// ```c
/// if (sep) {
///     r = 1;
///     sl = strlen(sep);
///     for (; (c = findsep(&s, sep, 0)) >= 0; s += sl)
///         if ((c || mul) && (sl || *(s + sl)))
///             r++;
/// } else {
///     /* IFS-based: walk skipwsep / itype_end(s, ISEP, 1) */
/// }
/// ```
///
/// This port walks the metafied byte stream directly to mirror C's
/// pointer arithmetic. The `sep`-based branch is exact; the IFS
/// branch uses [`iwsep`] for whitespace-separator detection (C's
/// `ISEP` char class collapsed to whitespace, which is the common
/// case for default `$IFS` = `" \t\n"`).
pub fn wordcount(s: &str, sep: Option<&str>, mul: i32) -> i32 {
    // c:3879
    let bytes = s.as_bytes();
    if let Some(sep) = sep {
        // C: r = 1; sl = strlen(sep); for (; findsep(&s,sep,0) >= 0; s+=sl)
        //        if ((c || mul) && (sl || *(s+sl))) r++;
        let sep_bytes = sep.as_bytes();
        let sl = sep_bytes.len();
        let mut r: i32 = 1;
        let mut pos = 0;
        while pos <= bytes.len() {
            let rest = &bytes[pos..];
            let c_offset = match sep_bytes.is_empty() {
                true => Some(0usize),
                false => rest.windows(sl).position(|w| w == sep_bytes),
            };
            let Some(c) = c_offset else { break };
            // C `c` is the chars before the separator; `(sl || *(s+sl))`
            // means: if sl is zero (empty sep), only count when there's a
            // following char. Otherwise (sl > 0), the second clause is true
            // when sep is non-empty AND there are bytes after sep.
            let after_off = pos + c + sl;
            let following_nonempty = after_off < bytes.len();
            let cond_b = sl != 0 || following_nonempty;
            if (c != 0 || mul != 0) && cond_b {
                r += 1;
            }
            if sl == 0 {
                // Avoid infinite loop on empty sep — mirrors C's findsep
                // which advances by 1 byte when sep is empty.
                pos += 1;
            } else {
                pos += c + sl;
            }
        }
        r
    } else {
        // IFS branch (sep == NULL). C source uses itype_end(s, ISEP, 1)
        // to skip ISEP chars (default $IFS = " \t\n"). We use iwsep.
        let mut s_pos = 0usize;
        let t_orig = s_pos;
        let mut r: i32 = 0;
        // C: if (mul <= 0) skipwsep(&s);
        if mul <= 0 {
            while s_pos < bytes.len() && iwsep(bytes[s_pos]) {
                s_pos += 1;
            }
        }
        // C: if ((*s && itype_end(s,ISEP,1)!=s) || (mul<0 && t!=s)) r++;
        let has_word_now = s_pos < bytes.len() && !iwsep(bytes[s_pos]);
        if has_word_now || (mul < 0 && t_orig != s_pos) {
            r += 1;
        }
        // C: for (; *s; r++) { advance over word + maybe-skipwsep + findsep + maybe-skipwsep }
        while s_pos < bytes.len() {
            // Advance past the current word (non-ISEP chars).
            let word_start = s_pos;
            while s_pos < bytes.len() && !iwsep(bytes[s_pos]) {
                s_pos += 1;
            }
            if s_pos > word_start && mul <= 0 {
                while s_pos < bytes.len() && iwsep(bytes[s_pos]) {
                    s_pos += 1;
                }
            }
            // C: (void)findsep(&s, NULL, 0) — advance past one sep run.
            // Already handled above when mul<=0; for mul>0 we still need
            // to consume one separator byte to make progress.
            if s_pos < bytes.len() && iwsep(bytes[s_pos]) {
                s_pos += 1;
            }
            let t_after = s_pos;
            if mul <= 0 {
                while s_pos < bytes.len() && iwsep(bytes[s_pos]) {
                    s_pos += 1;
                }
            }
            if s_pos < bytes.len() {
                r += 1;
            } else {
                // C: if (mul < 0 && t != s) r++;
                if mul < 0 && t_after != s_pos {
                    r += 1;
                }
                break;
            }
        }
        r
    }
}

/// Join array with separator - port from zsh/Src/utils.c sepjoin() lines 3926-3958
///
/// If sep is None, uses first char of IFS (defaults to space).
/// Join an array with separator.
/// Port of `sepjoin(char **s, char *sep, int heap)` from Src/utils.c:3928.
/// WARNING: param names don't match C — Rust=(arr, sep) vs C=(s, sep, heap)
// Rust idiom replacement: `slice::join` covers the C `zalloc`+`strcpy`
// loop with running length pre-compute; the `heap` arg drops since
// String owns its own allocation.
pub fn sepjoin(arr: &[String], sep: Option<&str>) -> String {
    // c:3928
    // c:3934-3935 — if (!*s) return heap ? dupstring("") : ztrdup("");
    if arr.is_empty() {
        return String::new();
    }
    // c:3936-3946 — if sep is NULL, derive from ifs[0]; default " " when
    //               ifs[0] is space (common case). Use first MB char of
    //               IFS otherwise. Rust mirrors via paramtab IFS lookup.
    let ifs_storage: String;
    let sep_str: &str = match sep {
        Some(s) => s, // c:3936
        None => {
            let ifs = getsparam("IFS").unwrap_or_default();
            // c:3938 — if (ifs && *ifs != ' ') sep = first MB char of ifs;
            if !ifs.is_empty() && !ifs.starts_with(' ') {
                ifs_storage = ifs
                    .chars()
                    .next()
                    .map(|c| c.to_string())
                    .unwrap_or_default();
                &ifs_storage
            } else {
                // c:3942-3944 — else sep = " ";
                " "
            }
        }
    };
    // c:3947-3956 — pre-compute total length, alloc, copy elements
    //               interleaving sep. Rust slice::join collapses all that
    //               into the one canonical call.
    arr.join(sep_str)
}

/// Split string by separator - port from zsh/Src/utils.c sepsplit() lines 3961-3992
///
/// If sep is None, performs IFS-style word splitting (spacesplit).
/// Otherwise splits on the given separator string.
/// allownull: if true, allows empty strings in result
/// Split a string on `IFS` separators.
/// Port of `sepsplit(char *s, char *sep, int allownull, int heap)` from Src/utils.c:3962.
/// WARNING: param names don't match C — Rust=(s, sep, allownull) vs C=(s, sep, allownull, heap)
pub fn sepsplit(s: &str, sep: Option<&str>, allownull: bool) -> Vec<String> {
    // c:3962
    // Handle Nularg at start (zsh internal marker) - line 3968
    let s = if s.starts_with('\x00') && s.len() > 1 {
        &s[1..]
    } else {
        s
    };

    match sep {
        None => spacesplit(s, allownull),
        Some("") => {
            // Empty separator: split into characters
            if allownull {
                s.chars().map(|c| c.to_string()).collect()
            } else {
                s.chars()
                    .map(|c| c.to_string())
                    .filter(|c| !c.is_empty())
                    .collect()
            }
        }
        Some(sep) => {
            let parts: Vec<String> = s.split(sep).map(|p| p.to_string()).collect();
            if allownull {
                parts
            } else {
                parts.into_iter().filter(|p| !p.is_empty()).collect()
            }
        }
    }
}

/// Port of `getshfunc(char *nam)` from `Src/utils.c:3998`.
///
/// C body:
/// ```c
/// Shfunc getshfunc(char *nam) {
///     return (Shfunc) shfunctab->getnode(shfunctab, nam);
/// }
/// ```
///
/// Routes through the global `shfunctab` singleton in
/// hashtable.rs (hashtable::shfunctab_lock). Returns an owned
/// `shfunc` clone so callers can read `flags` (to detect
/// `PM_UNDEFINED` autoload stubs), `funcdef`, `filename`, and
/// `body` without holding the table lock. Owned clone vs C's
/// `*Shfunc` raw pointer trades one allocation for Rust
/// lifetime safety — `getshfunc` is a function-lookup site,
/// not per-statement, so the cost is irrelevant.
///
/// Returning `Option<String>` of just `body` (the old contract)
/// made every PM_UNDEFINED autoload stub invisible because
/// `body=None` collapsed via `and_then` to `None`, which callers
/// then read as "function doesn't exist."
pub fn getshfunc(nam: &str) -> Option<shfunc> {
    let tab = shfunctab_lock()
        .read()
        .expect("shfunctab poisoned");
    tab.get(nam).cloned()
}

/// Port of `char **subst_string_by_func(Shfunc func, char *arg1, char *orig)`
/// from Src/utils.c:4017.
///
/// Calls the named shell function with `[func, arg1?, orig]` as
/// positional args under `sfcontext = SFC_SUBST` and returns the
/// `$reply` array on success. Routes through `callhookfunc` (the
/// static-linked equivalent of `doshfunc`), then reads `$reply`
/// from the env-var fallback because the global `paramtab` is not
/// yet a singleton in the Rust port (params::getaparam takes a
/// `&ParamTable` arg).
pub fn subst_string_by_func(
    func_name: &str,
    arg1: Option<&str>,
    orig: &str,
) -> Option<Vec<String>> // c:4017
{
    let osc = SFCONTEXT.load(Ordering::Relaxed); // c:4019
    let osm = STOPMSG.load(Ordering::Relaxed);
    let old_incompfunc = INCOMPFUNC.load(Ordering::Relaxed);
    let mut args: Vec<String> = Vec::with_capacity(3); // c:4020-4026
    args.push(func_name.to_string()); // c:4023
    if let Some(a) = arg1 {
        // c:4024
        args.push(a.to_string()); // c:4025
    }
    args.push(orig.to_string()); // c:4026
    SFCONTEXT // c:4027
        .store(SFC_SUBST, Ordering::Relaxed);
    INCOMPFUNC.store(0, Ordering::Relaxed); // c:4028

    let rc = callhookfunc(func_name, Some(&args), 0, std::ptr::null_mut()); // c:4030
                                                                            // c:4033 — `ret = getaparam("reply")` against paramtab. `reply`
                                                                            // is a shell-local PM_ARRAY entry, never exported to env.
    let ret: Option<Vec<String>> = if rc != 0 {
        None // c:4031
    } else {
        getaparam("reply") // c:4033
    };

    SFCONTEXT.store(osc, Ordering::Relaxed); // c:4035
    STOPMSG.store(osm, Ordering::Relaxed); // c:4036
    INCOMPFUNC.store(old_incompfunc, Ordering::Relaxed); // c:4037
    ret // c:4038
}

/// Port of `char **subst_string_by_hook(char *name, char *arg1, char *orig)`
/// from Src/utils.c:4049.
///
/// Looks up `name` as a shell function and calls
/// `subst_string_by_func` on it. If that returns no result, walks
/// `${name}_hook` as an array of function names, trying each in
/// order until one yields a `$reply`.
pub fn subst_string_by_hook(name: &str, arg1: Option<&str>, orig: &str) -> Option<Vec<String>> // c:4049
{
    let mut ret: Option<Vec<String>> = None;
    if getshfunc(name).is_some() {
        // c:4054
        ret = subst_string_by_func(name, arg1, orig); // c:4055
    }
    if ret.is_none() {
        // c:4058
        let arrnam = format!("{}_hook", name); // c:4061-4063
                                               // c:4065 — `arr = getaparam(arrnam)`. The previous Rust port
                                               // read `env::var(arrnam)` and NUL-split — wrong: the hook
                                               // array is a shell-local PM_ARRAY in paramtab, not env.
        if let Some(arr) = getaparam(&arrnam) {
            // c:4065
            for f in arr.iter() {
                // c:4068
                if f.is_empty() {
                    continue;
                }
                if getshfunc(f).is_some() {
                    // c:4069
                    ret = subst_string_by_func(f, arg1, orig); // c:4070
                    if ret.is_some() {
                        // c:4071
                        break; // c:4072
                    }
                }
            }
        }
    }
    ret // c:4094
}

/// Make single-element array (from utils.c mkarray)
/// Port of `mkarray(char *s)` from `Src/utils.c:4083`.
pub fn mkarray(s: Option<&str>) -> Vec<String> {
    match s {
        Some(val) => vec![val.to_string()],
        None => Vec::new(),
    }
}

/// Make single-element array on heap (from utils.c hmkarray)
/// Port of `hmkarray(char *s)` from `Src/utils.c:4094`.
pub fn hmkarray(s: &str) -> Vec<String> {
    if s.is_empty() {
        Vec::new()
    } else {
        vec![s.to_string()]
    }
}

/// Port of `void zbeep(void)` from Src/utils.c:4105.
///
/// Honours `$ZBEEP` (a key-string sequence) when set and the BEEP
/// option when unset; emits the BEL char (\007) to SHTTY by
/// default. The Rust port writes via `write_loop` to mirror C's
/// raw-write semantics.
pub fn zbeep() {
    // c:4105
    queue_signals(); // c:4105
    if let Ok(zbeep) = std::env::var("ZBEEP") {
        // c:4109
        let (decoded, _) = getkeystring(&zbeep); // c:4111
        #[cfg(unix)]
        {
            let shtty = SHTTY.load(Ordering::Relaxed);
            if shtty != -1 {
                let _ = write_loop(shtty, decoded.as_bytes()); // c:4112
            } else {
                eprint!("{}", decoded);
            }
        }
        #[cfg(not(unix))]
        eprint!("{}", decoded);
    } else if isset(BEEP) {
        // c:4113
        #[cfg(unix)]
        {
            let shtty = SHTTY.load(Ordering::Relaxed);
            if shtty != -1 {
                let _ = write_loop(shtty, b"\x07"); // c:4114
            } else {
                eprint!("\x07");
            }
        }
        #[cfg(not(unix))]
        eprint!("\x07");
    }
    unqueue_signals(); // c:4115
}

/// Free array (no-op in Rust, provided for API compat)
/// Port of `freearray(char **s)` from `Src/utils.c:4120`.
pub fn freearray(s: Vec<String>) {
    // c:4124 — DPUTS(!s, "freearray() with zero argument")
    // Rust takes Vec<String> by value (never null), so the C !s
    // check maps to no condition that can fire in Rust. Document
    // the gap.
    let _ = &s; // c:4124 (no-op; Vec is never NULL in Rust)
                // Rust Drop handles this
}

/// Split on '=' returning (name, value) (from utils.c equalsplit)
/// Port of `equalsplit(char *s, char **t)` from `Src/utils.c:4133`.
/// WARNING: param names don't match C — Rust=(s) vs C=(s, t)
pub fn equalsplit(s: &str) -> Option<(String, String)> {
    let eq = s.find('=')?;
    Some((s[..eq].to_string(), s[eq + 1..].to_string()))
}

// initialize the ztypes table                                              // c:4151
/// Port of `inittyptab()` from `Src/utils.c:4155`. Initialise the
/// `typtab[256]` lookup table that backs the `idigit`/`ialnum`/etc.
/// predicates in `ztype_h`.
///
/// C body (c:4155-4250) does:
///   1. Zero the table.
///   2. Mark 0..=31 and 128..=159 + 127 as ICNTRL.
///   3. Mark '0'..='9' as IDIGIT|IALNUM|IWORD|IIDENT|IUSER.
///   4. Mark 'a'..='z' and 'A'..='Z' as IALPHA|IALNUM|IIDENT|IUSER|IWORD.
///   5. Mark '_' as IIDENT|IUSER.
///   6. Mark '-', '.', Dash as IUSER.
///   7. Mark ' '/'\t' as IBLANK|INBLANK; '\n' as INBLANK.
///   8. Mark '\0', Meta, Marker as IMETA.
///   9. Mark Pound..LAST_NORMAL_TOK as ITOK|IMETA.
///   10. Mark Snull..Nularg as ITOK|IMETA|INULL.
///   11. Walk $IFS adding ISEP and IWSEP for blanks.
///
/// This first-pass port covers steps 1-7. Steps 8-11 require Meta /
/// Marker / Pound / Snull / Nularg constants from zsh_h/zsh.h and
/// the `ifs` global; the remaining Meta/IFS marks are skipped until
/// those land. Idempotent — safe to call multiple times.
pub fn inittyptab() {
    // utils.c:4155

    // c:4160 — `if (!(typtab_flags & ZTF_INIT))` one-off init.
    {
        let mut flags = TYPTAB_FLAGS.lock().unwrap();
        if (*flags & ZTF_INIT) == 0 {
            *flags = ZTF_INIT;
        }
    }

    let mut t = TYPTAB.lock().unwrap();
    // c:4168 — `memset(typtab, 0, sizeof(typtab));`
    for slot in t.iter_mut() {
        *slot = 0;
    }
    // c:4169-4170 — control chars 0..32 and 128..160.
    for c in 0..32u32 {
        t[c as usize] = ICNTRL as u32;
        t[(c + 128) as usize] = ICNTRL as u32;
    }
    // c:4171 — `typtab[127] = ICNTRL;`
    t[127] = ICNTRL as u32;
    // c:4172-4173 — '0'..='9'.
    for c in (b'0' as usize)..=(b'9' as usize) {
        t[c] = (IDIGIT | IALNUM | IWORD | IIDENT | IUSER) as u32;
    }
    // c:4174-4175 — 'a'..='z' and matching 'A'..='Z'.
    for c in (b'a' as usize)..=(b'z' as usize) {
        let upper = c - (b'a' as usize) + (b'A' as usize);
        let bits = (IALPHA | IALNUM | IIDENT | IUSER | IWORD) as u32;
        t[c] = bits;
        t[upper] = bits;
    }
    // c:4190 — `typtab['_'] = IIDENT | IUSER;`
    t[b'_' as usize] = (IIDENT | IUSER) as u32;
    // c:4191 — `typtab['-'] = typtab['.'] = typtab[(unsigned char) Dash] = IUSER;`.
    t[b'-' as usize] = IUSER as u32;
    t[b'.' as usize] = IUSER as u32;
    // c:4191 — `Dash` token marker (0x9b per zsh.h:182, "Only in patterns").
    // Marking it IUSER lets pattern-side $-named-character paths
    // accept it as a user-name byte. Previously omitted in the port.
    t[crate::ported::zsh_h::Dash as usize] = IUSER as u32;
    // c:4192-4194 — blanks.
    t[b' ' as usize] |= (IBLANK | INBLANK) as u32;
    t[b'\t' as usize] |= (IBLANK | INBLANK) as u32;
    t[b'\n' as usize] |= INBLANK as u32;

    // c:4195 — `typtab['\0'] |= IMETA;`. Previously omitted in the
    // Rust port — '\0' had only ICNTRL (set by the 0..32 loop at
    // c:4169) and was missing IMETA. C `imeta('\0')` is true (the
    // NUL byte must be Meta-encoded as `Meta + ('\0' ^ 32)`); the
    // Rust `imeta()` typtab predicate returned false, so any code
    // routing through `imeta(c)` (e.g. `input::shingetline`)
    // failed to Meta-encode NUL bytes from stdin, corrupting the
    // SHIN buffer when piped binary data hit a `\0`.
    t[0] |= IMETA as u32; // c:4195
                          // c:4196-4197 — Meta + Marker marked IMETA.
    {
        t[Meta as usize] |= IMETA as u32;
        t[Marker as usize] |= IMETA as u32;
    }

    // c:4133-4134 — `for (t0 = Pound; t0 <= LAST_NORMAL_TOK; t0++)
    //                    typtab[t0] |= ITOK | IMETA;`
    // Marks all char-rewrite token markers (Pound, Stringg, Hat,
    // Star, ...). Without this, `itok(Stringg)` returns false and
    // `is_valid_assignment_target("$NAME")` wrongly accepts the
    // leading `$` as part of an identifier prefix → `$=cmd` lexes
    // as ENVSTRING instead of STRING.
    {
        let lo = Pound as usize;
        let hi = LAST_NORMAL_TOK as usize;
        for t0 in lo..=hi {
            t[t0] |= (ITOK | IMETA) as u32;
        }
    }

    // c:4135-4136 — `for (t0 = Snull; t0 <= Nularg; t0++)
    //                    typtab[t0] |= ITOK | IMETA | INULL;`
    {
        let lo = Snull as usize;
        let hi = Nularg as usize;
        for t0 in lo..=hi {
            t[t0] |= (ITOK | IMETA | INULL) as u32;
        }
    }

    // c:4202-4231 — IFS walk. Sets ISEP on every IFS char and IWSEP
    // on the blank (inblank) subset. Reads the current `ifs` global
    // (defaulting to DEFAULT_IFS), demetafies `Meta+X` pairs, and
    // skips a doubled blank (`s[1]==c`) so the IWSEP bit doesn't
    // mark "blank repeated → no-skip" IFS chars. Mirrors C exactly.
    {
        // c:4216 — `for (s = ifs ? ifs : CURRENT_DEFAULT_IFS; ...)`.
        // C: `paramtab->getnode("IFS")->gsu.s->getfn(pm)`. Rust:
        // look up pm in paramtab, dispatch via ifsgetfn(pm).
        let ifs = crate::ported::params::paramtab()
            .read()
            .ok()
            .and_then(|t| t.get("IFS").map(|pm| crate::ported::params::ifsgetfn(pm)))
            .unwrap_or_default();
        let src: String = if ifs.is_empty() {
            DEFAULT_IFS.to_string()
        } else {
            ifs
        };
        let bytes = src.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            // c:4217 — `int c = (unsigned char) (*s == Meta ? *++s ^ 32 : *s)`.
            let c = if bytes[i] == Meta && i + 1 < bytes.len() {
                i += 1;
                bytes[i] ^ 32
            } else {
                bytes[i]
            };
            // c:4218-4223 — MULTIBYTE non-ASCII skip. Bytes >= 0x80
            // (after demetafy) are not classified by typtab — they
            // reach wcsitype via WC_ZISTYPE instead.
            if c >= 0x80 {
                i += 1;
                continue;
            }
            let cu = c as usize;
            // c:4224-4229 — `if (inblank(c))` — for default-IFS
            // chars space/tab/newline, mark IWSEP unless the next
            // byte repeats the same char.
            let is_inblank = (t[cu] & (INBLANK as u32)) != 0;
            if is_inblank {
                if i + 1 < bytes.len() && bytes[i + 1] == c {
                    i += 1; // c:4226 — skip the dup
                } else {
                    t[cu] |= IWSEP as u32; // c:4228
                }
            }
            // c:4230 — `typtab[c] |= ISEP;`
            t[cu] |= ISEP as u32;
            i += 1;
        }
    }

    // c:4232-4252 — wordchars walk. ORs IWORD onto every byte in
    // `$WORDCHARS` (or DEFAULT_WORDCHARS when unset). Used by every
    // word-class lookup in pattern matching, `${var:#word}`, etc.
    // Drops to ASCII-only under MULTIBYTE_SUPPORT (the non-ASCII path
    // routes through wordchars_wide).
    {
        let wc = crate::ported::params::paramtab()
            .read()
            .ok()
            .and_then(|t| t.get("WORDCHARS").map(|pm| crate::ported::params::wordcharsgetfn(pm)))
            .unwrap_or_default();
        let src: String = if wc.is_empty() {
            DEFAULT_WORDCHARS.to_string()
        } else {
            wc
        };
        let bytes = src.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            // c:4238 — Meta+X demetafy.
            let c = if bytes[i] == Meta && i + 1 < bytes.len() {
                i += 1;
                bytes[i] ^ 32
            } else {
                bytes[i]
            };
            // c:4239-4249 — MULTIBYTE non-ASCII skip.
            if c < 0x80 {
                t[c as usize] |= IWORD as u32; // c:4251
            }
            i += 1;
        }
    }

    // c:4253-4254 — SPECCHARS walk. ORs ISPECIAL onto every member
    // of the hardcoded SPECCHARS string. Drives glob-special and
    // quote-special detection.
    {
        for &b in SPECCHARS.as_bytes() {
            t[b as usize] |= ISPECIAL as u32; // c:4254
        }
    }

    // c:4255-4256 — comma special only when ZTF_SP_COMMA was set
    // via `makecommaspecial(1)`. KSH_GLOB / extended-glob path.
    {
        let flags = *TYPTAB_FLAGS.lock().unwrap();
        if (flags & ZTF_SP_COMMA) != 0 {
            // c:4255
            t[b',' as usize] |= ISPECIAL as u32; // c:4256
        }
    }

    // c:4257-4261 — bangchar special when BANGHIST + interact +
    // bangchar != 0. Sets ZTF_BANGCHAR flag bit then marks the
    // bangchar byte ISPECIAL.
    {
        let bangchar2 = bangchar.load(Ordering::SeqCst) as usize;
        let flags = *TYPTAB_FLAGS.lock().unwrap();
        let interact_flag = (flags & ZTF_INTERACT) != 0;
        let banghist = isset(BANGHIST);
        if banghist && bangchar2 != 0 && bangchar2 < 256 && interact_flag {
            // c:4257
            *TYPTAB_FLAGS.lock().unwrap() |= ZTF_BANGCHAR; // c:4258
            t[bangchar2] |= ISPECIAL as u32; // c:4259
        } else {
            *TYPTAB_FLAGS.lock().unwrap() &= !ZTF_BANGCHAR; // c:4261
        }
    }

    // c:4262-4263 — PATCHARS walk. ORs IPATTERN onto every member.
    // Used by pattern compilation to detect glob metachars.
    {
        for &b in PATCHARS.as_bytes() {
            t[b as usize] |= IPATTERN as u32; // c:4263
        }
    }
}

/// Port of `void makecommaspecial(int yesno)` from Src/utils.c:4270.
///
/// Toggles `ZTF_SP_COMMA` and the `ISPECIAL` bit on `,` in the
/// global typtab — used by glob/extended-glob to flag `,`
/// (KSH_GLOB) as a metacharacter.
pub fn makecommaspecial(yesno: bool) {
    // c:4270
    let mut flags = TYPTAB_FLAGS.lock().unwrap();
    let mut tab = TYPTAB.lock().unwrap();
    if yesno {
        // c:4272
        *flags |= ZTF_SP_COMMA; // c:4273
        tab[b',' as usize] |= ISPECIAL as u32; // c:4274
    } else {
        *flags &= !ZTF_SP_COMMA; // c:4276
        tab[b',' as usize] &= !(ISPECIAL as u32); // c:4277
    }
}

/// Port of `void makebangspecial(int yesno)` from Src/utils.c:4283.
///
/// Toggles `ISPECIAL` on the current `bangchar`. When `yesno==0`
/// always clears; when nonzero, sets only if `ZTF_BANGCHAR` was
/// stored by `inittyptab` (i.e. BANGHIST is on).
pub fn makebangspecial(yesno: bool) {
    // c:4283
    let bc = bangchar.load(Ordering::SeqCst) as usize;
    if bc == 0 || bc >= 256 {
        return;
    }
    let flags = *TYPTAB_FLAGS.lock().unwrap();
    let mut tab = TYPTAB.lock().unwrap();
    if !yesno {
        // c:4289
        tab[bc] &= !(ISPECIAL as u32); // c:4290
    } else if (flags & ZTF_BANGCHAR) != 0 {
        // c:4291
        tab[bc] |= ISPECIAL as u32; // c:4292
    }
}

/// Port of `wcsiblank(wint_t wc)` from `Src/utils.c:4302`.
///
/// ```c
/// mod_export int wcsiblank(wint_t wc) {
///     if (iswspace(wc) && wc != L'\n')
///         return 1;
///     return 0;
/// }
/// ```
///
/// "wide-character version of the iblank() macro" — true for any
/// whitespace EXCEPT newline. The previous Rust port included
/// newline (since `c.is_whitespace()` returns true for '\n') —
/// wrong for callers that use this to find token boundaries.
pub fn wcsiblank(wc: char) -> bool {
    wc.is_whitespace() && wc != '\n'
}

/// Port of `int wcsitype(wchar_t c, int itype)` from Src/utils.c:4321.
///
/// "zistype macro extended to support wide characters. Works for
/// IIDENT, IWORD, IALNUM, ISEP."
///
/// The Rust port checks whether `c` falls in the typtab class
/// represented by `itype`. ASCII chars consult the global TYPTAB
/// (the same one C's `zistype()` macro indexes); non-ASCII chars
/// route through Unicode predicates that mirror the C `iswalnum`
/// fallback at line 4346.
pub fn wcsitype(c: char, itype: u32) -> bool {
    // c:4321
    if !isset(MULTIBYTE) {
        // c:4327
        if (c as u32) < 256 {
            let tab = TYPTAB.lock().unwrap();
            return (tab[c as usize] & itype) != 0;
        }
        return false;
    }
    if (c as u32) < 128 {
        // c:4343
        let tab = TYPTAB.lock().unwrap();
        return (tab[c as usize] & itype) != 0;
    }
    let cls = itype as u16;
    if cls == IIDENT {
        // c:4347
        if isset(POSIXIDENTIFIERS) {
            return false; // c:4348
        }
        return c.is_alphanumeric(); // c:4350
    }
    if cls == IWORD {
        // c:4352
        if c.is_alphanumeric() {
            return true;
        } // c:4353
          // C: IS_COMBINING(c) — no Rust crate-free combining-mark
          // predicate. zero-width chars (combining, zero-width-joiner,
          // etc.) are treated as word per c:4362.
        if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 {
            // c:4362
            return true;
        }
        // c:4364 — `wmemchr(wordchars_wide.chars, c, …)`. Reads from
        // the canonical `wordchars` global (writable by
        // `wordcharssetfn` at `Src/params.c:5143`). Previously routed
        // through `std::env::var("WORDCHARS")` which is the libc
        // process environment — never reflects runtime `WORDCHARS=:`
        // assignments inside the shell.
        let w = crate::ported::params::paramtab()
            .read()
            .ok()
            .and_then(|t| t.get("WORDCHARS").map(|pm| crate::ported::params::wordcharsgetfn(pm)))
            .unwrap_or_default();
        return w.chars().any(|x| x == c);
    }
    if cls == ISEP {
        // c:4366
        // c:4367 — same canonical-global pattern for IFS.
        let ifs = crate::ported::params::paramtab()
            .read()
            .ok()
            .and_then(|t| t.get("IFS").map(|pm| crate::ported::params::ifsgetfn(pm)))
            .unwrap_or_default();
        return ifs.chars().any(|x| x == c);
    }
    let _ = IALNUM;
    c.is_alphanumeric() // c:4370
}

/// Check if a character type at end of string (from utils.c itype_end)
/// Returns the position after the identifier characters
/// Port of `itype_end(const char *ptr, int itype, int once)` from `Src/utils.c:4395`.
/// WARNING: param names don't match C — Rust=(s, allow_digits_start) vs C=(ptr, itype, once)
// Rust idiom replacement: `chars().peekable()` + `is_alphanumeric`
// covers the C `itype` table-lookup loop; the `once`/`itype` args
// collapse into the boolean `allow_digits_start` since callers in
// zshrs only use IDENT/IFS classifications.
pub fn itype_end(s: &str, allow_digits_start: bool) -> usize {
    let mut chars = s.chars().peekable();
    let mut pos = 0;

    if let Some(&first) = chars.peek() {
        if !allow_digits_start && first.is_ascii_digit() {
            return 0;
        }
        if !first.is_alphanumeric() && first != '_' && first != '.' {
            return 0;
        }
    }

    for c in s.chars() {
        if c.is_alphanumeric() || c == '_' || c == '.' {
            pos += c.len_utf8();
        } else {
            break;
        }
    }
    pos
}

/// Duplicate array (from utils.c arrdup)
/// Port of `arrdup(char **s)` from `Src/utils.c:4493`.
pub fn arrdup(s: &[String]) -> Vec<String> {
    s.to_vec()
}

/// Duplicate array with max elements (from utils.c arrdup_max)
/// Port of `arrdup_max(char **s, unsigned max)` from `Src/utils.c:4508`.
pub fn arrdup_max(s: &[String], max: usize) -> Vec<String> {
    s.iter().take(max).cloned().collect()
}

/// Duplicate array with zsh allocation (from utils.c zarrdup)
/// Port of `zarrdup(char **s)` from `Src/utils.c:4532`.
pub fn zarrdup(s: &[String]) -> Vec<String> {
    s.to_vec()
}

/// Duplicate array of wide strings (from utils.c wcs_zarrdup) - same as zarrdup in Rust
/// Port of `wcs_zarrdup(wchar_t **s)` from `Src/utils.c:4547`.
pub fn wcs_zarrdup(s: &[String]) -> Vec<String> {
    s.to_vec()
}

/// Spelling correction: find closest match (from utils.c spname)
/// Port of `spname(char *oldname)` from `Src/utils.c:4562`.
/// WARNING: param names don't match C — Rust=(name, dir) vs C=(oldname)
pub fn spname(name: &str, dir: &str) -> Option<String> {
    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return None,
    };

    let mut best = None;
    let mut best_dist = 4; // threshold

    for entry in entries.flatten() {
        if let Some(entry_name) = entry.file_name().to_str() {
            let dist = spdist(name, entry_name, best_dist);
            if dist < best_dist {
                best_dist = dist;
                best = Some(entry_name.to_string());
            }
        }
    }
    best
}

/// Spelling correction with full path (from utils.c mindist)
/// Port of `mindist(char *dir, char *mindistguess, char *mindistbest, int wantdir)` from `Src/utils.c:4624`.
/// WARNING: param names don't match C — Rust=(dir, name) vs C=(dir, mindistguess, mindistbest, wantdir)
pub fn mindist(dir: &str, name: &str) -> Option<(String, usize)> {
    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return None,
    };

    let mut best = None;
    let mut best_dist = 4;

    for entry in entries.flatten() {
        if let Some(entry_name) = entry.file_name().to_str() {
            let dist = spdist(name, entry_name, best_dist);
            if dist < best_dist {
                best_dist = dist;
                best = Some(entry_name.to_string());
            }
        }
    }
    best.map(|name| (name, best_dist))
}

// spellcheck a word                                                        // c:3123
// fix s ; if hist is nonzero, fix the history list too                     // c:3124
/// Compute edit distance between two strings (for spelling correction)
/// Direct port of `int spdist(char *s, char *t, int thresh)` from
/// `Src/utils.c:4675-4750`. Drives the CORRECT-option typo prompt:
/// returns 0 for identical, 1 for case-only mistakes, 2 for one
/// transposition / missing letter / QWERTY-adjacent mistype, 200 for
/// anything farther.
///
/// **This is NOT a Levenshtein DP — that was the previous Rust impl
/// dressed as a port.** The C source uses a keyboard-adjacency model
/// (qwertykeymap / dvorakkeymap arrays + tulower equality) which is
/// the actual behaviour `setopt CORRECT` depends on. Restored
/// faithfully.
pub fn spdist(s: &str, t: &str, thresh: usize) -> usize {
    // c:4679-4690 — qwerty keymap (rows of 14 chars each: numeric row,
    // QWERTY top row, home row, bottom row, then shift versions).
    // Embedded `\n` / `\t` mark off-grid cells.
    const QWERTYKEYMAP: &str = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
\t1234567890-=\t\
\tqwertyuiop[]\t\
\tasdfghjkl;'\n\t\
\tzxcvbnm,./\t\t\t\
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
\t!@#$%^&*()_+\t\
\tQWERTYUIOP{}\t\
\tASDFGHJKL:\"\n\t\
\tZXCVBNM<>?\n\n\t\
\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    // c:4691-4702 — dvorak keymap, same shape.
    const DVORAKKEYMAP: &str = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
\t1234567890[]\t\
\t',.pyfgcrl/=\t\
\taoeuidhtns-\n\t\
\t;qjkxbmwvz\t\t\t\
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
\t!@#$%^&*(){}\t\
\t\"<>PYFGCRL?+\t\
\tAOEUIDHTNS_\n\t\
\t:QJKXBMWVZ\n\n\t\
\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

    // c:4703-4707 — `keymap = isset(DVORAK) ? dvorakkeymap : qwertykeymap;`
    let keymap = if isset(DVORAK) {
        DVORAKKEYMAP.as_bytes()
    } else {
        QWERTYKEYMAP.as_bytes()
    };

    let s_b = s.as_bytes();
    let t_b = t.as_bytes();

    // c:4709-4710 — `if (!strcmp(s, t)) return 0;`
    if s == t {
        return 0;
    }

    // c:4712-4714 — `for (p, q; *p && tulower(*p) == tulower(*q); p++, q++);
    //                  if (!*p && !*q) return 1;` — case-only mismatch.
    let mut p = 0usize;
    let mut q = 0usize;
    while p < s_b.len()
        && q < t_b.len()
        && tulower(s_b[p] as char) == tulower(t_b[q] as char)
    {
        p += 1;
        q += 1;
    }
    if p == s_b.len() && q == t_b.len() {
        return 1;
    }

    // c:4715-4716 — `if (!thresh) return 200;`
    if thresh == 0 {
        return 200;
    }

    // c:4717-4727 — first walk: detect transposition / missing letter at
    // first divergence.
    p = 0;
    q = 0;
    while p < s_b.len() && q < t_b.len() {
        if s_b[p] == t_b[q] {
            // c:4718-4719 — match: skip (don't count `aa` as transposed).
            p += 1;
            q += 1;
            continue;
        }
        // c:4720-4721 — `if (p[1] == q[0] && q[1] == p[0]) return
        //                  spdist(p+2, q+2, thresh-1) + 1;` transposition.
        if p + 1 < s_b.len()
            && q + 1 < t_b.len()
            && s_b[p + 1] == t_b[q]
            && t_b[q + 1] == s_b[p]
        {
            // SAFETY: bytes are ASCII for the keymap test below; from_utf8
            // is fine on a non-ASCII tail here because spdist takes &str.
            let s_tail = std::str::from_utf8(&s_b[p + 2..]).unwrap_or("");
            let t_tail = std::str::from_utf8(&t_b[q + 2..]).unwrap_or("");
            return spdist(s_tail, t_tail, thresh.saturating_sub(1)) + 1;
        }
        // c:4722-4723 — `if (p[1] == q[0]) return spdist(p+1, q, thresh-1) + 2;`
        if p + 1 < s_b.len() && s_b[p + 1] == t_b[q] {
            let s_tail = std::str::from_utf8(&s_b[p + 1..]).unwrap_or("");
            let t_tail = std::str::from_utf8(&t_b[q..]).unwrap_or("");
            return spdist(s_tail, t_tail, thresh.saturating_sub(1)) + 2;
        }
        // c:4724-4725 — `if (p[0] == q[1]) return spdist(p, q+1, thresh-1) + 2;`
        if q + 1 < t_b.len() && s_b[p] == t_b[q + 1] {
            let s_tail = std::str::from_utf8(&s_b[p..]).unwrap_or("");
            let t_tail = std::str::from_utf8(&t_b[q + 1..]).unwrap_or("");
            return spdist(s_tail, t_tail, thresh.saturating_sub(1)) + 2;
        }
        // c:4726-4727 — `if (*p != *q) break;`
        break;
    }
    // c:4728-4729 — `if ((!*p && strlen(q) == 1) || (!*q && strlen(p) == 1))
    //                  return 2;` — single trailing-char insertion.
    if (p == s_b.len() && (t_b.len() - q) == 1)
        || (q == t_b.len() && (s_b.len() - p) == 1)
    {
        return 2;
    }
    // c:4730-4748 — second walk: keyboard-adjacency mistype detection.
    p = 0;
    q = 0;
    while p < s_b.len() && q < t_b.len() {
        if p + 1 < s_b.len()
            && q + 1 < t_b.len()
            && s_b[p] != t_b[q]
            && s_b[p + 1] == t_b[q + 1]
        {
            // c:4737-4738 — `if (!(z = strchr(keymap, p[0])) || *z == '\n' ||
            //                       *z == '\t') return spdist(p+1, q+1,
            //                       thresh-1) + 1;`
            let pos = keymap.iter().position(|&b| b == s_b[p]);
            let z_ok = match pos {
                Some(i) => keymap[i] != b'\n' && keymap[i] != b'\t',
                None => false,
            };
            if !z_ok {
                let s_tail = std::str::from_utf8(&s_b[p + 1..]).unwrap_or("");
                let t_tail = std::str::from_utf8(&t_b[q + 1..]).unwrap_or("");
                return spdist(s_tail, t_tail, thresh.saturating_sub(1)) + 1;
            }
            // c:4739 — `t0 = z - keymap;`
            let t0 = pos.unwrap() as isize;
            // c:4740-4744 — eight adjacency offsets (-15,-14,-13,-1,+1,
            //                +13,+14,+15) → keyboard neighbours.
            let offsets: [isize; 8] = [-15, -14, -13, -1, 1, 13, 14, 15];
            let adjacent = offsets.iter().any(|&off| {
                let idx = t0 + off;
                if idx >= 0 && (idx as usize) < keymap.len() {
                    keymap[idx as usize] == t_b[q]
                } else {
                    false
                }
            });
            if adjacent {
                // c:4745 — `return spdist(p+1, q+1, thresh-1) + 2;`
                let s_tail = std::str::from_utf8(&s_b[p + 1..]).unwrap_or("");
                let t_tail = std::str::from_utf8(&t_b[q + 1..]).unwrap_or("");
                return spdist(s_tail, t_tail, thresh.saturating_sub(1)) + 2;
            }
            // c:4746 — `return 200;`
            return 200;
        } else if p < s_b.len() && q < t_b.len() && s_b[p] != t_b[q] {
            // c:4747-4748 — `else if (*p != *q) break;`
            break;
        }
        p += 1;
        q += 1;
    }
    // c:4749 — `return 200;`
    200
}

/// Set terminal to cbreak mode (from utils.c setcbreak)
#[cfg(unix)]
/// Port of `setcbreak` from `Src/utils.c:4756`.
pub fn setcbreak() -> bool {
    if let Some(mut ti) = gettyinfo() {
        ti.c_lflag &= !(libc::ICANON | libc::ECHO);
        ti.c_cc[libc::VMIN] = 1;
        ti.c_cc[libc::VTIME] = 0;
        settyinfo(&ti)
    } else {
        false
    }
}

#[cfg(not(unix))]
/// Port of `setcbreak` from `Src/utils.c:4756`.
pub fn setcbreak() -> bool {
    false
}

/// Port of `void attachtty(pid_t pgrp)` from Src/utils.c:4775.
/// Hands the controlling terminal to `pgrp`. Gated by `jobbing &&
/// interact`; falls back to `mypgrp` and disables MONITOR on permanent
/// failure (matching the C source's recursion + `opts[MONITOR]=0` path).
#[cfg(unix)]
pub fn attachtty(pgrp: i32) {
    // c:4775

    if !(jobbing() && interact()) {
        return; // c:4779
    }
    let shtty = SHTTY.load(Ordering::Relaxed); // c:4781
    if shtty == -1 {
        return;
    }
    let ep = ATTACHTTY_EP.load(Ordering::Relaxed);
    let rc = unsafe { libc::tcsetpgrp(shtty, pgrp) }; // c:4781
    if rc == -1 && ep == 0 {
        // c:4781
        let mypgrp_val = *crate::ported::jobs::MYPGRP // c:4792
            .get_or_init(|| Mutex::new(0))
            .lock()
            .unwrap();
        if pgrp != mypgrp_val && unsafe { libc::kill(-pgrp, 0) } == -1 {
            attachtty(mypgrp_val); // c:4793
        } else {
            let errno_val = io::Error::last_os_error().raw_os_error().unwrap_or(0);
            if errno_val != libc::ENOTTY {
                // c:4795
                zwarn(&format!(
                    "can't set tty pgrp: {}", // c:4797
                    io::Error::from_raw_os_error(errno_val)
                ));
                let _ = io::stderr().flush(); // c:4798
            }
            opt_state_set("monitor", false); // c:4815 opts[MONITOR]=0
            ATTACHTTY_EP.store(1, Ordering::Relaxed); // c:4815
        }
    } else if rc != -1 {
        // c:4815
        *crate::ported::jobs::LAST_ATTACHED_PGRP // c:4815
            .get_or_init(|| Mutex::new(0))
            .lock()
            .unwrap() = pgrp;
    }
}

/// Port of `pid_t gettygrp(void)` from Src/utils.c:4815.
#[cfg(unix)]
pub fn gettygrp() -> i32 {
    // c:4815
    let shtty = SHTTY.load(Ordering::Relaxed);
    if shtty == -1 {
        // c:4819
        return -1; // c:4820
    }
    unsafe { libc::tcgetpgrp(shtty) } // c:4823
}

/// Convert raw bytes (possibly containing NUL / 0x83-0x9b) to
/// zsh's metafied form: each `imeta(b)` byte becomes `Meta` (0x83)
/// followed by `b ^ 32`.
///
/// Port of `metafy(char *buf, int len, int heap)` from Src/utils.c:4856. The C source takes a
/// `heap` mode controlling whether the result is `zalloc`'d /
/// `zhalloc`'d / written into a static buffer / appended to the
/// existing buffer; in Rust we always return an owned `String`
/// since allocation strategy is uniform. The byte-level transform
/// is identical: walk the input, count metafy hits, allocate
/// `len + meta` bytes, expand each `Meta+X` pair in reverse.
/// Rust idiom replacement: forward byte-walk + Vec::push covers the
/// C two-pass (count + alloc + reverse-expand) approach; Vec grows
/// on demand so the pre-count is unnecessary in Rust.
/// WARNING: param names don't match C — Rust=(buf) vs C=(buf, len, heap)
pub fn metafy(buf: &str) -> String {
    // c:4856
    let bytes = buf.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    for &b in bytes {
        // C: `#define imeta(c) ((c) >= Meta)` from Src/zsh.h —
        // every byte >= 0x83 needs escaping. The previous
        // narrow-range check `(0x83..=0x9b)` was a bug: bytes
        // 0x9c..=0xff (e.g. UTF-8 continuation bytes, high-Latin
        // characters) escaped C's imeta() but not the Rust
        // version, which then fed un-escaped bytes downstream
        // and corrupted Meta-aware loops.
        if imeta_byte(b) {
            out.push(Meta);
            out.push(b ^ 32);
        } else {
            out.push(b);
        }
    }
    // metafied bytes are in [0..=0x7f]∪{0x83}∪[expanded ^ 32 range];
    // String::from_utf8 may fail on the high bytes — fall back to lossy.
    String::from_utf8(out.clone()).unwrap_or_else(|_| String::from_utf8_lossy(&out).into_owned())
}

/// Port of `ztrdup_metafy(const char *s)` from `Src/utils.c:4929`.
///
/// ```c
/// mod_export char *
/// ztrdup_metafy(const char *s)
/// {
///     if (!s) return NULL;
///     return metafy((char *)s, -1, META_DUP);
/// }
/// ```
pub fn ztrdup_metafy(s: &str) -> String {
    metafy(s)
}

/// Port of `unmetafy(char *s, int *len)` from `Src/utils.c:4954`.
///
/// Take a metafied byte buffer in `s` and convert it in place to
/// its literal representation. C signature:
///
/// ```c
/// char *unmetafy(char *s, int *len);
/// ```
///
/// The Rust port mutates `s` in place and returns the resulting
/// length (mirroring C's `*len` out-parameter). C control flow:
///
/// ```c
/// for (p = s; *p && *p != Meta; p++);     // skip prefix with no Meta
/// for (t = p; (*t = *p++);)                 // walk the rest
///     if (*t++ == Meta && *p)
///         t[-1] = *p++ ^ 32;                // un-escape: XOR with 32
/// ```
///
/// Same algorithm here, byte-indexed against the Vec rather than
/// pointer-walked.
/// WARNING: param names don't match C — Rust=(s) vs C=(s, len)
pub fn unmetafy(s: &mut Vec<u8>) -> usize {
    // c:4954
    // First loop: find the first `Meta` byte. Everything before it
    // stays as-is, so we don't need to copy.
    let mut p: usize = 0;
    while p < s.len() && s[p] != Meta{
        p += 1;
    }
    // Second loop: walk from `p` onward, copying each byte into the
    // `t` slot (which trails `p` by one position per Meta-escape
    // we collapse).
    let mut t: usize = p;
    while p < s.len() {
        let b = s[p];
        s[t] = b;
        p += 1;
        if b == Meta && p < s.len() {
            // C: t[-1] = *p++ ^ 32; — overwrite the just-written
            // Meta with the un-escaped byte.
            s[t] = s[p] ^ 32;
            p += 1;
        }
        t += 1;
    }
    s.truncate(t);
    t
}

/// Port of `int metalen(const char *s, int len)` from `Src/utils.c:4971-4983`.
/// ```c
/// int mlen = len;
/// while (len--) {
///     if (*s++ == Meta) { mlen++; s++; }
/// }
/// return mlen;
/// ```
/// Doc: "Return the character length of a metafied substring, given
/// the unmetafied substring length." So **input `len` is the
/// UNMETAFIED char count, output is the METAFIED byte count.**
///
/// Previously the Rust port had INVERTED semantics: looped while
/// `i < len` byte-walk, returned char count. That's the reverse
/// operation. Pin the correct C contract.
pub fn metalen(s: &str, len: usize) -> usize {
    // c:4972
    let bytes = s.as_bytes();
    let mut mlen = len; // c:4974
    let mut remaining = len;
    let mut i = 0;
    while remaining > 0 && i < bytes.len() {
        // c:4976 `while (len--)`
        if bytes[i] == Meta {
            // c:4977
            mlen += 1; // c:4978
            i += 2; // c:4979 s++ (already advanced past Meta)
        } else {
            i += 1;
        }
        remaining -= 1;
    }
    mlen
}

/// Port of `unmeta(const char *file_name)` from `Src/utils.c:4994`.
///
/// Convert a zsh internal (metafied) string to a system-call-safe
/// form (e.g. for passing to `open(2)`).
///
/// C body shape (c:4994-5010):
/// ```c
/// meta = 0;
/// for (t = file_name; *t; t++)
///     if (*t == Meta) { meta = 1; break; }
/// if (!meta) return (char *) file_name;        // no-copy fast path
/// for (t = file_name, p = fn; *t; p++)
///     if ((*p = *t++) == Meta && *t)
///         *p = *t++ ^ 32;
/// ```
// Rust idiom replacement: byte-scan fast path + `unmetafy` covers
// the C in-place decode + alloc-on-copy dance; String owns its own
// allocation so no `heap` flag needed.
pub fn unmeta(s: &str) -> String {
    // c:4994
    let bytes = s.as_bytes();
    // c:4995-4996 — Meta-byte scan; no-copy fast path.
    if !bytes.iter().any(|&b| b == Meta) {
        return s.to_string();
    }
    let mut buf = bytes.to_vec();
    let len = unmetafy(&mut buf); // c:4999-5001
    buf.truncate(len);
    String::from_utf8_lossy(&buf).into_owned()
}

/// Port of `convchar_t unmeta_one(const char *in, int *sz)` from `Src/utils.c:5056-5086`.
/// Non-MULTIBYTE branch (c:5077-5083):
/// ```c
/// if (in[0] == Meta) { *sz = 2; wc = (unsigned char)(in[1] ^ 32); }
/// else               { *sz = 1; wc = (unsigned char) in[0]; }
/// ```
/// Returns `(decoded_char, bytes_consumed)`. The c:5070 NULL/empty
/// guard returns `(0, 0)`. Previously used hardcoded `0x83` for the
/// Meta byte — now routes through the canonical `Meta` constant
/// (defined as `'\u{83}'` at zsh.h:144).
/// WARNING: param names don't match C — Rust=(s) vs C=(in, sz)
pub fn unmeta_one(s: &str) -> (char, usize) {
    // c:5058
    let bytes = s.as_bytes();
    // c:5070 — `if (!in || !*in) return 0;`
    if bytes.is_empty() {
        return ('\0', 0);
    }
    // c:5077 — `if (in[0] == Meta)`.
    if bytes[0] == Meta && bytes.len() > 1 {
        // c:5078-5079 — `*sz = 2; wc = (unsigned char)(in[1] ^ 32);`.
        ((bytes[1] ^ 32) as char, 2)
    } else {
        // c:5081-5082 — `*sz = 1; wc = (unsigned char) in[0];`.
        (bytes[0] as char, 1)
    }
}

/// Port of `ztrcmp(char const *s1, char const *s2)` from `Src/utils.c:5106`.
///
/// Byte-walking compare with lazy Meta resolution. C body skips
/// matching bytes wholesale, then on the first differing byte
/// un-meta-fies just that byte and compares. Faster than
/// `unmeta(s1).cmp(unmeta(s2))` because:
///   1. No allocation up front,
///   2. Only the first differing position pays the un-meta cost.
///
/// ```c
/// while(*s1 && *s1 == *s2) { s1++; s2++; }
/// if (!(c1 = *s1)) c1 = -1;
/// else if (c1 == Meta) c1 = *++s1 ^ 32;
/// if (!(c2 = *s2)) c2 = -1;
/// else if (c2 == Meta) c2 = *++s2 ^ 32;
/// return c1 - c2;
/// ```
pub fn ztrcmp(s1: &str, s2: &str) -> std::cmp::Ordering {
    // c:5106
    let b1 = s1.as_bytes();
    let b2 = s2.as_bytes();
    let mut i1 = 0;
    let mut i2 = 0;
    // Skip the matching prefix.
    while i1 < b1.len() && i2 < b2.len() && b1[i1] == b2[i2] {
        i1 += 1;
        i2 += 1;
    }
    // Resolve c1: -1 for end-of-string, else the next byte
    // (un-meta-fied if it's a Meta marker).
    let c1: i32 = if i1 >= b1.len() {
        -1
    } else if b1[i1] == Meta && i1 + 1 < b1.len() {
        (b1[i1 + 1] ^ 32) as i32
    } else {
        b1[i1] as i32
    };
    let c2: i32 = if i2 >= b2.len() {
        -1
    } else if b2[i2] == Meta && i2 + 1 < b2.len() {
        (b2[i2 + 1] ^ 32) as i32
    } else {
        b2[i2] as i32
    };
    c1.cmp(&c2)
}

// pastebuf() DELETED — was a misplaced fn. The real `pastebuf()`
// lives in `Src/Zle/zle_misc.c:558` (a ZLE clipboard helper that
// pastes a Cutbuffer into the line-edit buffer). It has nothing to
// do with metafication, despite this file's prior body which
// reimplemented half of `metafy`. The real metafy lives below at
// `pub fn metafy()` (port of utils.c:4856).

/// Unmetafied string length (from utils.c ztrlen lines 5135-5152)
pub fn ztrlen(s: &str) -> usize {
    // c:5136
    let mut len = 0;
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        len += 1;
        if chars[i] as u32 == Meta as u32 && i + 1 < chars.len() {
            i += 2;
        } else {
            i += 1;
        }
    }
    len
}

/// Port of `ztrlenend(char const *s, char const *eptr)` from `Src/utils.c:5162`.
///
/// ```c
/// for (l = 0; s < eptr; l++) {
///     if (*s++ == Meta) s++;  // skip past Meta-escaped pair
/// }
/// return l;
/// ```
///
/// Count the unmetafied character length from `s` up to `end`
/// bytes. Each Meta-escaped pair counts as 1 character.
/// Previous Rust port called `chars().count()` which counts UTF-8
/// codepoints, not byte-walked Meta-pairs — wrong semantics.
pub fn ztrlenend(s: &str, eptr: usize) -> usize {
    let bytes = s.as_bytes();
    let cap = eptr.min(bytes.len());
    let mut l = 0;
    let mut i = 0;
    while i < cap {
        if bytes[i] == Meta {
            // Meta sentinel + escaped byte = 1 visible char.
            i += 2;
        } else {
            i += 1;
        }
        l += 1;
    }
    l
}

/// Port of `int ztrsub(char const *t, char const *s)` from `Src/utils.c:5185-5203`.
/// ```c
/// int l = t - s;
/// while (s != t) {
///     if (*s++ == Meta) { s++; l--; }
/// }
/// return l;
/// ```
/// "Subtract two pointers in a metafied string." `s` is the start
/// pointer, `t` is the end pointer; both point into the SAME buffer.
/// Returns the count of unmetafied chars in `[s, t)` — same as
/// `ztrlen` of the substring but expressed via pointer arithmetic.
///
/// Rust API: takes the full buffer and a byte-offset pair
/// `[start..end)` since Rust can't replicate raw-pointer subtraction
/// across distinct `&str` arguments without UB risk. The semantics
/// stay faithful: count of unmetafied chars between two offsets.
/// Previous Rust port took two unrelated `&str` and computed
/// `ztrlen(&t[..t.len()-s.len()])` — a fundamentally different
/// operation that worked only when `s` was the suffix of `t`.
/// WARNING: param names don't match C — Rust=(buf, start, end) vs C=(t, s)
pub fn ztrsub(buf: &str, start: usize, end: usize) -> usize {
    // c:5187
    let bytes = buf.as_bytes();
    let end = end.min(bytes.len());
    let start = start.min(end);
    let mut l = (end - start) as isize; // c:5189
    let mut i = start;
    while i < end {
        if bytes[i] == Meta {
            // c:5192
            i += 2; // c:5198
            l -= 1; // c:5199
        } else {
            i += 1;
        }
    }
    l.max(0) as usize
}

/// Port of `char *zreaddir(DIR *dir, int ignoredots)` from
/// `Src/utils.c:5217`.
///
/// Port of `char *zreaddir(DIR *dir, int ignoredots)` from
/// `Src/utils.c:5217-5240`. Pulls the next entry from a DIR* the
/// caller owns; returns the entry's bare name (metafied String) or
/// `None` at EOF. The C source's `ignoredots` flag (c:5232) skips
/// `.` and `..` when set; callers (`spnamepat` at c:4648) that
/// want dot entries pass 0.
///
/// Caller pattern matches C exactly:
///   let mut dir = fs::read_dir(path)?;
///   while let Some(name) = zreaddir(&mut dir, 1) { ... }
///   // dir auto-closed on drop (= closedir)
pub fn zreaddir(dir: &mut fs::ReadDir, ignoredots: i32) -> Option<String> {
    // c:5217
    for entry in dir.by_ref() {
        // c:5221 readdir loop
        let Ok(e) = entry else { continue };
        let Ok(name) = e.file_name().into_string() else { continue };
        // c:5232 — `if (ignoredots && de->d_name[0] == '.' &&
        //              (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])))`.
        if ignoredots != 0 && (name == "." || name == "..") {
            continue; // c:5234
        }
        return Some(name); // c:5238 return de->d_name
    }
    None // c:5240 — fell off the end
}

/// Port of `int zputs(char const *s, FILE *stream)` from `Src/utils.c:5263-5282`.
///
/// ```c
/// while (*s) {
///     if (*s == Meta)     c = *++s ^ 32;
///     else if (itok(*s)) { s++; continue; }
///     else                c = *s;
///     s++;
///     if (fputc(c, stream) < 0) return EOF;
/// }
/// return 0;
/// ```
///
/// Writes `s` to `stream` with Meta+X pair decoding and ITOK-byte
/// skipping. Returns `0` on success, `-1` on write error (mirroring
/// C's `EOF` sentinel for the int return).
pub fn zputs(s: &str, stream: &mut dyn std::io::Write) -> i32 {
    // c:5265
    let bytes = s.as_bytes();                                                // c:5265 *s walk
    let mut i = 0;
    while i < bytes.len() {                                                  // c:5267 while (*s)
        let c: u8;                                                           // c:5268 char c
        if bytes[i] == Meta {                                                // c:5269 if (*s == Meta)
            // c:5270 — `c = *++s ^ 32;`
            if i + 1 < bytes.len() {
                c = bytes[i + 1] ^ 32;
                i += 1;                                                      // c:5270 ++s
            } else {
                i += 1;
                continue;
            }
        } else if itok(bytes[i]) {                                           // c:5271 else if (itok(*s))
            // c:5272 — `s++; continue;` (skip token byte)
            i += 1;
            continue;
        } else {
            c = bytes[i];                                                    // c:5274 else c = *s
        }
        i += 1;                                                              // c:5276 s++
        if stream.write_all(&[c]).is_err() {                                 // c:5277 fputc(c, stream)
            return -1;                                                       // c:5278 return EOF
        }
    }
    0                                                                        // c:5280 return 0
}

/// Port of `nicedup(char const *s, int heap)` from `Src/utils.c:5289`
/// (single-byte build) and `Src/utils.c:5530` (multibyte build).
///
/// C bodies:
/// ```c
/// /* Src/utils.c:5289 — !MULTIBYTE_SUPPORT */
/// char *retstr;
/// (void)sb_niceformat(s, NULL, &retstr, heap ? NICEFLAG_HEAP : 0);
/// return retstr;
///
/// /* Src/utils.c:5530 — MULTIBYTE_SUPPORT */
/// char *retstr;
/// (void)mb_niceformat(s, NULL, &retstr, heap ? NICEFLAG_HEAP : 0);
/// return retstr;
/// ```
///
/// zshrs targets MULTIBYTE_SUPPORT (matches every modern zsh
/// build) — route through `mb_niceformat`. Previous Rust port
/// went through `sb_niceformat`, which is the !MULTIBYTE path —
/// strips wide-char handling and corrupts non-ASCII via byte-mask
/// `nicechar(c & 0xff)`.
///
/// C signature faithful: `(s, heap) -> String`. `heap` selects between
/// arena (`NICEFLAG_HEAP`) and persistent (default `ztrdup`) allocation
/// in C; Rust has a single allocator so both paths produce the same
/// owned `String`, but the `heap` parameter is preserved per Rule S1.
pub fn nicedup(s: &str, heap: i32) -> String {
    // c:5530
    // c:5532 — `char *retstr;`
    let retstr: Option<String>;
    // c:5534 — `(void)mb_niceformat(s, NULL, &retstr, heap ? NICEFLAG_HEAP : 0);`
    let mut slot: Option<String> = None;
    let _ = mb_niceformat(
        s,
        None,
        Some(&mut slot),
        if heap != 0 { NICEFLAG_HEAP } else { 0 },
    );
    retstr = slot;
    // c:5536 — `return retstr;`
    retstr.unwrap_or_default()
}

/// Nice-format and duplicate string.
/// Port of `nicedupstring(char const *s)` from `Src/utils.c:5301`.
/// C body: `return nicedup(s, 1);` — heap-arena allocation form.
pub fn nicedupstring(s: &str) -> String {
    // c:5301
    nicedup(s, 1) // c:5303
}

/// Nicely format a string
/// Port of `nicezputs(char const *s, FILE *stream)` from `Src/utils.c`.
///
/// Under `MULTIBYTE_SUPPORT` (the daily-driver path), C defines
/// `nicezputs(str, outs)` as a macro at zsh.h:3274:
///   `(void)mb_niceformat((str), (outs), NULL, 0)`.
/// Without MULTIBYTE (c:5313): `sb_niceformat(s, stream, NULL, 0)`.
///
/// The previous Rust impl was `s.chars().map(nicechar).collect()` —
/// wrong on every front:
///   1. No unmetafy step (Meta-byte pairs surfaced as `\M-…` mangled).
///   2. `nicechar` is byte-based (`c & 0xff`), corrupting non-ASCII
///      multibyte codepoints into spurious `\M-X` escapes.
///   3. No `itok` ZSH-token-byte handling.
///
/// Route through `mb_niceformat` to match the C macro under
/// MULTIBYTE — which itself unmetafies and uses `wcs_nicechar` for
/// proper wide-char handling.
pub fn nicezputs(s: &str, stream: &mut dyn std::io::Write) -> i32 {
    // c:5313 (non-MULTIBYTE) / zsh.h:3274 (MULTIBYTE macro:
    //   `(void)mb_niceformat((str), (outs), NULL, 0)`).
    let _ = mb_niceformat(s, Some(stream), None, 0);
    0                                                                        // c:5316 return 0
}

/// Port of `niceztrlen(char const *s)` from `Src/utils.c:5324`.
///
/// Returns the length (in bytes) of the visible representation of
/// the metafied string `s`. C body walks each char via `nicechar`
/// and accumulates `strlen(nicechar(c))`; the Rust port mirrors
/// this via [`sb_niceformat`] which is the same render-then-measure
/// path.
pub fn niceztrlen(s: &str) -> usize {
    // c equivalent: l = sb_niceformat(s, NULL, NULL, 0); return l;
    sb_niceformat(s, None, None, 0)
}

/// Multibyte-aware nice-format of a string.
/// Port of `mb_niceformat(const char *s, FILE *stream, char **outstrp, int flags)` from Src/utils.c:5366. Walks the
/// (un-metafied) string char-by-char; for each control byte or
/// invalid sequence emits a `^X`/`\\xNN` representation, otherwise
/// passes the char through. The C source threads an
/// `mbstate_t` through `mbrtowc()` and falls back to single-byte
/// `\M-` notation on `MB_INVALID`; the Rust port uses Rust's
/// chars iterator which already produces valid scalar values, so
/// invalid-byte fallback collapses to the control-char branch.
/// WARNING: param names don't match C — Rust=(s) vs C=(s, stream, outstrp, flags)
// Rust idiom replacement: `chars()` + `wcs_nicechar` covers the C
// mbrtowc loop with `MB_INVALID` fallback (Rust UTF-8 guarantees
// valid scalars, so the invalid-byte arm collapses).
pub fn mb_niceformat(
    s: &str,
    mut stream: Option<&mut dyn std::io::Write>,
    outstrp: Option<&mut Option<String>>,
    flags: i32,
) -> usize {
    // c:5366
    let mut l: usize = 0;                                                    // c:5368 size_t l = 0
    let mut newl: usize;                                                     // c:5368 size_t newl
    // c:5369 — `int umlen, outalloc, outleft, eol = 0;`. outalloc/outleft
    // model C's buffer-growth math; Rust String auto-grows so the realloc
    // loop at c:5430-5440 collapses to push_str. umlen and eol carry.
    let mut umlen: usize;                                                    // c:5369
    let mut eol: bool = false;                                               // c:5369 int eol = 0
    // c:5370 — `wchar_t c;` (carried inside loop in Rust)
    // c:5371 — `char *ums, *ptr, *fmt, *outstr, *outptr;`
    let mut ums: Vec<u8>;                                                    // c:5371 char *ums
    let mut ptr: usize;                                                      // c:5371 char *ptr
    let mut fmt: String;                                                     // c:5371 char *fmt
    let mut outstr: Option<String>;                                          // c:5371 char *outstr
    // c:5372 — `mbstate_t mbs;` (Rust UTF-8 has no shift state; tracked
    // by the `valid_up_to` / `error_len` logic below).

    // c:5374-5380 — `if (outstrp) outptr = outstr = zalloc(5*strlen(s));`
    if outstrp.is_some() {                                                   // c:5374
        outstr = Some(String::with_capacity(5 * s.len()));                   // c:5376
    } else {                                                                 // c:5377
        outstr = None;                                                       // c:5379 outstr = NULL
    }

    // c:5382 — `ums = ztrdup(s);`
    // c:5383-5387 — comment-only block carried verbatim:
    /*
     * is this necessary at this point? niceztrlen does this
     * but it's used in lots of places.  however, one day this may
     * be, too.
     */                                                                      // c:5383-5387
    // c:5388 — `untokenize(ums);` — Rust port uses the char-based
    // `lex::untokenize` (only fires on codepoints in the 0x84..=0xa1
    // ITOK range, never on UTF-8 continuation bytes); the byte-level
    // shape of C's untokenize is unsafe on raw UTF-8 input because a
    // continuation byte like 0x97 (part of `字` = 0xE5 0xAD 0x97) sits
    // inside the ITOK range and would be misclassified as a token.
    // c:5389 — `ptr = unmetafy(ums, &umlen);` — `unmeta` is the safe
    // UTF-8 wrapper that runs unmetafy only when Meta bytes are
    // present and otherwise no-ops.
    let detok = untokenize(s);
    let unmeta_str = unmeta(&detok);
    ums = unmeta_str.into_bytes();
    umlen = ums.len();                                                       // c:5389 *umlen
    ptr = 0;                                                                 // c:5389 ptr starts at 0 in ums

    // c:5391 — `memset(&mbs, 0, sizeof mbs);` (Rust: stateless UTF-8)
    while umlen > 0 {                                                        // c:5392
        // c:5393 — `cnt = eol ? MB_INVALID : mbrtowc(&c, ptr, umlen, &mbs);`
        // Rust equivalent: try to decode one UTF-8 scalar from ums[ptr..],
        // honoring `eol` (force the invalid arm when set).
        let cnt: usize;
        let decoded_c: Option<char>;
        if eol {
            // c:5396 — MB_INVALID arm via `eol = 1`.
            decoded_c = None;
            cnt = 1;
        } else {
            let remaining = &ums[ptr..ptr + umlen];
            match std::str::from_utf8(remaining) {
                Ok(s_slice) => {
                    // Valid UTF-8 throughout remaining; consume one char.
                    if let Some(ch) = s_slice.chars().next() {
                        decoded_c = Some(ch);
                        cnt = ch.len_utf8();
                    } else {
                        // Empty? Shouldn't happen given umlen > 0, but
                        // mirror MB_INVALID.
                        decoded_c = None;
                        cnt = 1;
                    }
                }
                Err(e) => {
                    let valid_up_to = e.valid_up_to();
                    if valid_up_to > 0 {
                        // Decode the first valid char then continue.
                        let valid_slice = unsafe {
                            std::str::from_utf8_unchecked(&remaining[..valid_up_to])
                        };
                        let ch = valid_slice.chars().next().unwrap();
                        decoded_c = Some(ch);
                        cnt = ch.len_utf8();
                    } else if e.error_len().is_none() {
                        // Incomplete sequence at end of input — MB_INCOMPLETE.
                        // c:5394 — `case MB_INCOMPLETE: eol = 1; FALL THROUGH`
                        eol = true;
                        decoded_c = None;                                    // MB_INVALID
                        cnt = 1;
                    } else {
                        // Definite invalid byte — MB_INVALID.
                        decoded_c = None;
                        cnt = 1;
                    }
                }
            }
        }

        // c:5396-5403 / c:5404-5424 switch dispatch on cnt/decoded_c.
        let cnt_used: usize;
        match decoded_c {
            None => {
                // c:5397 — `case MB_INVALID:` (or fall-through from
                // MB_INCOMPLETE at c:5394).
                // c:5400 — `fmt = nicechar_sel(*ptr, flags & NICEFLAG_QUOTE);`
                fmt = nicechar_sel(ums[ptr] as char, (flags & NICEFLAG_QUOTE) != 0);
                newl = fmt.len();                                            // c:5401 newl = strlen(fmt)
                cnt_used = 1;                                                // c:5402 cnt = 1
                // c:5403 — `memset(&mbs, 0, sizeof mbs);` (Rust: stateless)
            }
            Some(c) if c == '\0' => {
                // c:5406 — `case 0:` — '\0' decodes to 0; consume 1 byte
                // and fall through to default.
                cnt_used = 1;                                                // c:5409
                // c:5411-5421 default arm (FALL THROUGH from case 0)
                if c == '\'' && (flags & NICEFLAG_QUOTE) != 0 {
                    // c:5413
                    fmt = "\\'".to_string();                                 // c:5414
                    newl = 2;                                                // c:5415
                } else if c == '\\' && (flags & NICEFLAG_QUOTE) != 0 {       // c:5417
                    fmt = "\\\\".to_string();                                // c:5418
                    newl = 2;                                                // c:5419
                } else {
                    // c:5422 — `fmt = wcs_nicechar_sel(c, &newl, NULL,
                    //                                 flags & NICEFLAG_QUOTE);`
                    let mut width: usize = 0;
                    fmt = wcs_nicechar_sel(
                        c,
                        Some(&mut width),
                        None,
                        (flags & NICEFLAG_QUOTE) != 0,
                    );
                    newl = width;
                }
            }
            Some(c) => {
                // c:5411 — `default:` arm (cnt > 0, valid char).
                cnt_used = cnt;
                if c == '\'' && (flags & NICEFLAG_QUOTE) != 0 {              // c:5413
                    fmt = "\\'".to_string();                                 // c:5414
                    newl = 2;                                                // c:5415
                } else if c == '\\' && (flags & NICEFLAG_QUOTE) != 0 {       // c:5417
                    fmt = "\\\\".to_string();                                // c:5418
                    newl = 2;                                                // c:5419
                } else {
                    // c:5422 — `fmt = wcs_nicechar_sel(c, &newl, NULL,
                    //                                 flags & NICEFLAG_QUOTE);`
                    let mut width: usize = 0;
                    fmt = wcs_nicechar_sel(
                        c,
                        Some(&mut width),
                        None,
                        (flags & NICEFLAG_QUOTE) != 0,
                    );
                    newl = width;
                }
            }
        }

        umlen -= cnt_used;                                                   // c:5427 umlen -= cnt
        ptr += cnt_used;                                                     // c:5428 ptr += cnt
        l += newl;                                                           // c:5429 l += newl

        if let Some(ref mut w) = stream {                                    // c:5431 if (stream)
            // c:5432 — `zputs(fmt, stream);`
            let _ = w.write_all(fmt.as_bytes());
        }
        if let Some(ref mut buf) = outstr {                                  // c:5433 if (outstr)
            // c:5434-5446 — append fmt to outstr, growing on demand. Rust
            // String auto-grows; the realloc loop collapses to push_str.
            buf.push_str(&fmt);                                              // c:5446 memcpy(outptr, fmt, outlen)
        }
        let _ = fmt;
    }

    // c:5451 — `free(ums);` (Rust drop at scope exit)
    drop(ums);
    if let Some(slot) = outstrp {                                            // c:5452 if (outstrp)
        // c:5453 — `*outptr = '\0';` (no-op for Rust String)
        // c:5455-5460 — NICEFLAG_NODUP / NICEFLAG_HEAP shaping. Rust has
        // a single allocator so all three paths produce identical owned
        // String contents; transfer ownership into caller's slot.
        *slot = outstr.take();
    }

    l                                                                        // c:5462 return l
}

/// Port of `is_mb_niceformat(const char *s)` from `Src/utils.c:5474`.
///
/// Predicate: would any character in `s` need representation by
/// `mb_niceformat` / `nicedup`? C body:
/// ```c
/// ums = ztrdup(s);
/// untokenize(ums);
/// ptr = unmetafy(ums, &umlen);
/// while (umlen > 0) {
///     cnt = mbrtowc(&c, ptr, umlen, &mbs);
///     switch (cnt) {
///     case MB_INCOMPLETE: case MB_INVALID:
///         if (is_nicechar(*ptr)) { ret = 1; break; }
///         cnt = 1;
///         memset(&mbs, 0, sizeof mbs);
///         break;
///     case 0: cnt = 1;  /* FALLTHROUGH */
///     default:
///         if (is_wcs_nicechar(c)) ret = 1;
///         break;
///     }
///     if (ret) break;
///     umlen -= cnt; ptr += cnt;
/// }
/// ```
///
/// Rust port: unmetafy in place via [`unmetafy`], then walk the
/// resulting bytes. For valid UTF-8 sequences, check
/// `is_wcs_nicechar(scalar)`; for invalid bytes, check
/// `is_nicechar(byte)`. Either path bailing positive returns true.
pub fn is_mb_niceformat(s: &str) -> i32 {
    // c:5474
    // c:5476 — `int umlen, ret = 0;`
    let umlen: usize;                                                        // c:5476
    let mut ret: i32 = 0;                                                    // c:5476
    // c:5477 — `char *ums, *ptr;` (eptr modelled by bytes.len())
    let mut ums: Vec<u8>;                                                    // c:5477
    let mut ptr: usize;                                                      // c:5477

    // c:5481-5483 — `ums = ztrdup(s); untokenize(ums); ptr =
    //                unmetafy(ums, &umlen);` — Rust uses char-based
    // `lex::untokenize` (never corrupts UTF-8 continuation bytes) and
    // `unmeta` (no-op on UTF-8 without Meta bytes; runs unmetafy when
    // they're present).
    let detok = untokenize(s);
    let unmeta_str = unmeta(&detok);
    ums = unmeta_str.into_bytes();
    umlen = ums.len();                                                       // c:5483 *umlen
    ptr = 0;                                                                 // c:5483 ptr starts at 0

    // c:5485 — `memset(&mbs, 0, sizeof mbs);` (Rust: stateless UTF-8)
    while ret == 0 && ptr < ums.len() {                                      // c:5486 while (umlen > 0)
        let remaining = &ums[ptr..];
        match std::str::from_utf8(remaining) {
            Ok(s_slice) => {
                // c:5503-5511 — `default: if (is_wcs_nicechar(c)) ret = 1;`
                for ch in s_slice.chars() {
                    if is_wcs_nicechar(ch) {                                 // c:5508
                        ret = 1;                                             // c:5509
                        break;
                    }
                }
                break;
            }
            Err(e) => {
                let valid_up_to = e.valid_up_to();
                if valid_up_to > 0 {
                    let valid = unsafe {
                        std::str::from_utf8_unchecked(&remaining[..valid_up_to])
                    };
                    for ch in valid.chars() {
                        if is_wcs_nicechar(ch) {                             // c:5508
                            ret = 1;                                         // c:5509
                            break;
                        }
                    }
                    if ret != 0 {
                        break;
                    }
                    ptr += valid_up_to;
                    continue;
                }
                // c:5493-5498 — `case MB_INVALID: if (is_nicechar(*ptr))
                //                ret = 1; break;`
                if is_nicechar(remaining[0] as char) {                       // c:5494
                    ret = 1;                                                 // c:5495
                    break;
                }
                ptr += 1;
            }
        }
    }

    drop(ums);                                                               // c:5519 free(ums)
    ret                                                                      // c:5523 return ret
}

/// Multibyte metachar length with conversion (from utils.c mb_metacharlenconv_r)
/// Port of `mb_metacharlenconv_r(const char *s, wint_t *wcp, mbstate_t *mbsp)` from `Src/utils.c:5548`.
/// WARNING: param names don't match C — Rust=(s, pos) vs C=(s, wcp, mbsp)
// Rust idiom replacement: Rust strings are UTF-8 with valid scalar
// values, so `chars().next()` covers the mbrtowc state machine; no
// mbstate_t threading required.
pub fn mb_metacharlenconv_r(s: &str, pos: usize) -> (usize, Option<char>) {
    if let Some(c) = s[pos..].chars().next() {
        (c.len_utf8(), Some(c))
    } else {
        (0, None)
    }
}

/// Multibyte-aware metafied-string char advance.
/// Port of `mb_metacharlenconv(const char *s, wint_t *wcp)` from Src/utils.c:5611. Returns
/// `(bytes_consumed, scalar_char)` for the next char in `s`. C
/// source dispatches to `mb_metacharlenconv_r()` for true
/// multibyte; we use Rust's UTF-8 char iterator which already
/// handles multi-byte correctly. ASCII fast-path: `Meta+X` is 2
/// bytes consumed → `(2, X^32)`; bare ASCII is `(1, c)`.
/// WARNING: param names don't match C — Rust=(s) vs C=(s, wcp)
pub fn mb_metacharlenconv(s: &str) -> (usize, Option<char>) {
    let bytes = s.as_bytes();
    if bytes.is_empty() {
        return (0, None);
    }
    if bytes[0] == 0x83 && bytes.len() >= 2 {
        // Meta+X pair → unescape.
        let raw = bytes[1] as u32 ^ 32;
        return (2, char::from_u32(raw));
    }
    if bytes[0] <= 0x7f {
        return (1, Some(bytes[0] as char));
    }
    // Multi-byte UTF-8 — let Rust decode.
    if let Some(c) = s.chars().next() {
        return (c.len_utf8(), Some(c));
    }
    (1, None)
}

/// Multibyte metastring length to end (from utils.c mb_metastrlenend)
/// Port of `mb_metastrlenend(char *ptr, int width, char *eptr)` from `Src/utils.c:5655`.
// Rust idiom replacement: `chars().count()` / unicode-width replaces
// the C mbrtowc loop + wcwidth fallback.
pub fn mb_metastrlenend(ptr: &str, width: bool, eptr: usize) -> usize {
    if width {
        ptr[..eptr.min(ptr.len())]
            .chars()
            .map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(1))
            .sum()
    } else {
        ptr[..eptr.min(ptr.len())].chars().count()
    }
}

/// Multibyte char length with conversion (from utils.c mb_charlenconv_r)
/// Port of `mb_charlenconv_r(const char *s, int slen, wint_t *wcp, mbstate_t *mbsp)` from `Src/utils.c:5747`.
/// WARNING: param names don't match C — Rust=(s, pos) vs C=(s, slen, wcp, mbsp)
pub fn mb_charlenconv_r(s: &str, pos: usize) -> (usize, Option<char>) {
    mb_metacharlenconv_r(s, pos)
}

/// Multibyte char length (from utils.c mb_charlenconv)
/// Port of `mb_charlenconv(const char *s, int slen, wint_t *wcp)` from `Src/utils.c:5793`.
/// WARNING: param names don't match C — Rust=(s, pos) vs C=(s, slen, wcp)
pub fn mb_charlenconv(s: &str, pos: usize) -> usize {
    s[pos..].chars().next().map(|c| c.len_utf8()).unwrap_or(0)
}

/// Port of `int metacharlenconv(const char *x, int *c)` from `Src/utils.c:5810-5826`.
/// ```c
/// if (*x == Meta) {
///     if (c) *c = x[1] ^ 32;
///     return 2;
/// }
/// if (c) *c = (char)*x;
/// return 1;
/// ```
/// Single-byte metafied char advance — Meta+X is 2 bytes (decode
/// via XOR 32), plain byte is 1 byte. Previously used hardcoded
/// `0x83`; now routes through the canonical `Meta` const for
/// maintainability parity.
/// WARNING: param names don't match C — Rust=(s) vs C=(x, c)
pub fn metacharlenconv(s: &str) -> (usize, Option<char>) {
    // c:5811
    let bytes = s.as_bytes();
    if bytes.is_empty() {
        return (0, None);
    }
    if bytes[0] == Meta && bytes.len() >= 2 {
        // c:5818
        let raw = bytes[1] as u32 ^ 32; // c:5820
        return (2, char::from_u32(raw)); // c:5821
    }
    (1, Some(bytes[0] as char)) // c:5823-5825
}

/// Plain (non-metafy) char advance.
/// Port of `charlenconv(const char *x, int len, int *c)` from Src/utils.c:5832 — the
/// non-MULTIBYTE_SUPPORT branch. Single-byte read with
/// `len`-bound check; matches the C source's `if (!len)` early
/// exit.
/// WARNING: param names don't match C — Rust=(s, len) vs C=(x, len, c)
pub fn charlenconv(s: &str, len: usize) -> (usize, Option<char>) {
    if len == 0 {
        return (0, None);
    }
    let bytes = s.as_bytes();
    if bytes.is_empty() {
        return (0, None);
    }
    (1, Some(bytes[0] as char))
}

/// Port of `size_t sb_niceformat(const char *s, FILE *stream, char **outstrp, int flags)` from `Src/utils.c:5849-5910`.
/// ```c
/// ums = ztrdup(s); untokenize(ums); ptr = unmetafy(ums, &umlen);
/// while (ptr < eptr) {
///     int c = (unsigned char) *ptr;
///     if      (c == '\''  && (flags & NICEFLAG_QUOTE)) fmt = "\\'";
///     else if (c == '\\'  && (flags & NICEFLAG_QUOTE)) fmt = "\\\\";
///     else                                              fmt = nicechar_sel(c, ...);
/// }
/// ```
/// Single-byte nice format. **Unmetafies the input first** (c:5872),
/// then calls `nicechar_sel` for EVERY byte (not just controls).
///
/// Param mapping (Rule S1, faithful to C):
/// - `s: &str`            ← C `const char *s`
/// - `stream: Option<&mut dyn Write>` ← C `FILE *stream` (`None` ≡ `NULL`)
/// - `outstrp: Option<&mut Option<String>>` ← C `char **outstrp`
///   (outer `None` ≡ `NULL`; inner `Option<String>` is the storage slot
///   the caller passes as `&local`, mirroring `char *retstr;
///   sb_niceformat(s, NULL, &retstr, ...);`).
/// - returns `usize` ← C `size_t l`.
pub fn sb_niceformat(
    s: &str,
    mut stream: Option<&mut dyn std::io::Write>,
    outstrp: Option<&mut Option<String>>,
    flags: i32,
) -> usize {
    // c:5851
    let mut l: usize = 0;                                                    // c:5853 size_t l = 0
    let mut newl: usize;                                                     // c:5853 size_t newl
    // c:5854 — `int umlen, outalloc, outleft;`. outalloc/outleft model
    // C's buffer-growth math; Rust String auto-grows so the realloc
    // loop at c:5897-5906 collapses to push_str. umlen carries through.
    let umlen: usize;                                                        // c:5854
    // c:5855 — `char *ums, *ptr, *eptr, *fmt, *outstr, *outptr;`
    let mut ums: Vec<u8>;                                                    // c:5855 char *ums
    let mut ptr: usize;                                                      // c:5855 char *ptr
    let eptr: usize;                                                         // c:5855 char *eptr
    let mut fmt: String;                                                     // c:5855 char *fmt
    let mut outstr: Option<String>;                                          // c:5855 char *outstr

    // c:5857-5863 — `if (outstrp) outptr = outstr = zalloc(2*strlen(s));`
    if outstrp.is_some() {                                                   // c:5857
        outstr = Some(String::with_capacity(2 * s.len()));                   // c:5859
    } else {                                                                 // c:5860
        outstr = None;                                                       // c:5862 outstr = NULL
    }

    // c:5865 — `ums = ztrdup(s);`
    ums = s.as_bytes().to_vec();
    // c:5866-5870 — comment-only block carried verbatim:
    /*
     * is this necessary at this point? niceztrlen does this
     * but it's used in lots of places.  however, one day this may
     * be, too.
     */                                                                      // c:5866-5870
    // c:5871 — `untokenize(ums);` inlined byte-level (lex::untokenize
    // is char-based; C does byte-walks). Mirrors `Src/exec.c:2077-2099`
    // exec.c untokenize().
    let ztokens_table = b"#$^*(())$=|{}[]`<>>?~`,-!'\"\\\\";                 // ZTOKENS — Src/lex.c:38
    let mut detok: Vec<u8> = Vec::with_capacity(ums.len());
    for &c in &ums {                                                         // exec.c:2082
        if (0x84u8..=0xa1u8).contains(&c) {                                  // exec.c:2083 itok(c)
            if c != 0xa1u8 {                                                 // exec.c:2086 c != Nularg
                let idx = (c - 0x84) as usize;
                if idx < ztokens_table.len() {
                    detok.push(ztokens_table[idx]);
                }
            }
        } else {
            detok.push(c);                                                   // exec.c:2094
        }
    }
    ums = detok;
    // c:5872 — `ptr = unmetafy(ums, &umlen);`
    umlen = unmetafy(&mut ums);
    ums.truncate(umlen);
    eptr = umlen;                                                            // c:5873 eptr = ptr + umlen
    ptr = 0;                                                                 // c:5872 ptr starts at 0 in ums

    while ptr < eptr {                                                       // c:5875
        let c: i32 = ums[ptr] as i32;                                        // c:5876 int c = (unsigned char) *ptr
        if c == b'\'' as i32 && (flags & NICEFLAG_QUOTE) != 0 {              // c:5877
            fmt = "\\'".to_string();                                         // c:5878
            newl = 2;                                                        // c:5879
        } else if c == b'\\' as i32 && (flags & NICEFLAG_QUOTE) != 0 {       // c:5881
            fmt = "\\\\".to_string();                                        // c:5882
            newl = 2;                                                        // c:5883
        } else {                                                             // c:5885
            fmt = nicechar_sel(c as u8 as char, (flags & NICEFLAG_QUOTE) != 0); // c:5886
            newl = 1;                                                        // c:5887
        }

        ptr += 1;                                                            // c:5890 ++ptr
        l += newl;                                                           // c:5891 l += newl

        if let Some(ref mut w) = stream {                                    // c:5893 if (stream)
            // c:5894 — `zputs(fmt, stream);`
            let _ = w.write_all(fmt.as_bytes());
        }
        if let Some(ref mut buf) = outstr {                                  // c:5895 if (outstr)
            // c:5896-5912 — append fmt to outstr, growing on demand. Rust
            // String auto-grows; the realloc loop at c:5897-5906 collapses
            // to push_str (memcpy + outptr/outleft bookkeeping unneeded).
            buf.push_str(&fmt);                                              // c:5907 memcpy(outptr, fmt, outlen)
        }
        // `fmt` is consumed at next iter assignment (C reuses pointer).
        let _ = fmt;
    }

    // c:5915 — `free(ums);` (Rust drop at scope exit)
    drop(ums);
    if let Some(slot) = outstrp {                                            // c:5916 if (outstrp)
        // c:5917 — `*outptr = '\0';` (no-op for String — push_str leaves
        // no embedded NUL, and Rust String is length-prefixed).
        // c:5919-5925 — NICEFLAG_NODUP / NICEFLAG_HEAP shaping: in C this
        // selects between ztrdup (perm) / dupstring (heap arena) / direct
        // ownership-transfer (NODUP). Rust has a single allocator so all
        // three paths produce identical owned String contents; transfer
        // ownership of `outstr` into the caller's slot.
        *slot = outstr.take();
    }

    l                                                                        // c:5928 return l
}

/// Port of `int is_sb_niceformat(const char *s)` from `Src/utils.c:5937-5959`.
///
/// Predicate: would `sb_niceformat` change the input? Walks each byte
/// after unmetafy, returns `1` if any byte is "nice" per `is_nicechar`,
/// else `0` (C `int` return faithful to Rule S1).
pub fn is_sb_niceformat(s: &str) -> i32 {
    // c:5937
    // c:5939 — `int umlen, ret = 0;`
    let umlen: usize;                                                        // c:5939
    let mut ret: i32 = 0;                                                    // c:5939
    // c:5940 — `char *ums, *ptr, *eptr;`
    let mut ums: Vec<u8>;                                                    // c:5940 char *ums
    let mut ptr: usize;                                                      // c:5940 char *ptr
    let eptr: usize;                                                         // c:5940 char *eptr

    ums = s.as_bytes().to_vec();                                             // c:5942 ums = ztrdup(s)
    // c:5943 — `untokenize(ums);` inlined byte-level (lex::untokenize is
    // char-based; C does byte-walks). Mirrors `Src/exec.c:2077-2099`.
    let ztokens_table = b"#$^*(())$=|{}[]`<>>?~`,-!'\"\\\\";                 // ZTOKENS — Src/lex.c:38
    let mut detok: Vec<u8> = Vec::with_capacity(ums.len());
    for &c in &ums {                                                         // exec.c:2082
        if (0x84u8..=0xa1u8).contains(&c) {                                  // exec.c:2083 itok(c)
            if c != 0xa1u8 {                                                 // exec.c:2086 c != Nularg
                let idx = (c - 0x84) as usize;
                if idx < ztokens_table.len() {
                    detok.push(ztokens_table[idx]);
                }
            }
        } else {
            detok.push(c);                                                   // exec.c:2094
        }
    }
    ums = detok;
    umlen = unmetafy(&mut ums);                                              // c:5944 ptr = unmetafy(ums, &umlen)
    ums.truncate(umlen);
    eptr = umlen;                                                            // c:5945 eptr = ptr + umlen
    ptr = 0;                                                                 // c:5944 ptr starts at 0 in ums

    while ptr < eptr {                                                       // c:5947
        if is_nicechar(ums[ptr] as char) {                                   // c:5948 is_nicechar(*ptr)
            ret = 1;                                                         // c:5949
            break;                                                           // c:5950
        }
        ptr += 1;                                                            // c:5952 ++ptr
    }

    drop(ums);                                                               // c:5955 free(ums)
    ret                                                                      // c:5957 return ret
}

/// Tab expansion — direct port of `zexpandtabs(const char *s, int len, int width, int startpos, FILE *fout, int all)` in zsh/Src/utils.c:5975.
/// Writes `s` into `out` with TAB characters expanded to spaces against
/// a tabstop of `width`. `startpos` carries the cumulative emitted
/// column from previous calls (used by `print -X` which preserves
/// alignment across args). When `all_tabs` is false, only leading TABs
/// (those at the start of a line) are expanded; embedded TABs are
/// emitted verbatim and `startpos` is advanced by one tabstop. When
/// `all_tabs` is true, every TAB expands. Returns the new `startpos`.
pub(crate) fn zexpandtabs(
    s: &str,
    width: i32,
    startpos: i32,
    all_tabs: bool,
    out: &mut String,
) -> i32 {
    let mut startpos = startpos;
    let mut at_start = true;
    for c in s.chars() {
        if c == '\t' {
            if all_tabs || at_start {
                if width <= 0 || startpos % width == 0 {
                    out.push(' ');
                    startpos += 1;
                }
                if width > 0 {
                    while startpos % width != 0 {
                        out.push(' ');
                        startpos += 1;
                    }
                }
            } else {
                let rem = startpos % width;
                startpos += width - rem;
                out.push('\t');
            }
            continue;
        } else if c == '\n' || c == '\r' {
            out.push(c);
            startpos = 0;
            at_start = true;
            continue;
        }
        at_start = false;
        out.push(c);
        startpos += unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as i32;
    }
    startpos
}

/// Port of `int hasspecial(char const *s)` from `Src/utils.c:6072-6082`.
/// ```c
/// while (*s) {
///     if (ispecial(*s == Meta ? *++s ^ 32 : *s)) return 1;
///     s++;
/// }
/// return 0;
/// ```
/// Predicate: does `s` contain any byte that needs shell-quoting?
/// Routes through the canonical typtab-driven `ztype_h::ispecial`
/// which respects the `ZTF_SP_COMMA` (set by `makecommaspecial`)
/// and `ZTF_BANGCHAR` (BANGHIST + interact) flags. Previously used
/// a hardcoded char list — diverged from C for `,` (KSH_GLOB
/// extended-glob) and for the dynamic bangchar (`!` by default but
/// user-rewritable via `$HISTCHARS`).
pub fn hasspecial(s: &str) -> bool {
    // c:6072
    let bytes = s.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        let c = if bytes[i] == Meta && i + 1 < bytes.len() {
            // c:6075 — `*s == Meta ? *++s ^ 32 : *s`.
            let v = bytes[i + 1] ^ 32;
            i += 2;
            v
        } else {
            let v = bytes[i];
            i += 1;
            v
        };
        if crate::ported::ztype_h::ispecial(c) {
            // c:6075
            return true;
        }
    }
    false
}

/// Port of `static char *addunprintable(char *v, const char *u, const char *uend)`
/// from `Src/utils.c:6082-6124`. Renders unprintable bytes using
/// **shell-compatible C-string escapes**:
///   - `\0` (NUL, with `\000` form if next byte is octal-digit)
///   - `\a` (0x07 BEL), `\b` (BS), `\f` (FF), `\n` (LF),
///     `\r` (CR), `\t` (TAB), `\v` (VT)
///   - `\nnn` 3-digit octal fallback for any other control byte
///
/// Previously the Rust port emitted ZLE-style caret notation (`^X`,
/// `\u{:04x}`) — completely different convention. C semantics target
/// `printf %q` / `$'...'` reuse where the output must round-trip
/// through `printf %b` to reconstruct the original byte. Caret
/// notation is what ZLE *displays* but not what `addunprintable`
/// emits.
/// WARNING: param names don't match C — Rust=(c) vs C=(v, u, uend)
pub fn addunprintable(c: char) -> String {
    // c:6082
    let b = c as u32 & 0xff;
    match b as u8 {
        // c:6097-6103 — `\0`. C peeks next byte and uses `\000` form
        // when followed by an octal digit. Rust port works on a single
        // char so emits just `\0`; callers needing the disambiguation
        // form can pass the lookahead byte separately.
        0x00 => "\\0".to_string(),
        0x07 => "\\a".to_string(), // c:6106
        0x08 => "\\b".to_string(), // c:6107
        0x0c => "\\f".to_string(), // c:6108
        0x0a => "\\n".to_string(), // c:6109
        0x0d => "\\r".to_string(), // c:6110
        0x09 => "\\t".to_string(), // c:6111
        0x0b => "\\v".to_string(), // c:6112
        // c:6114-6119 — `\nnn` 3-digit octal default.
        _ => format!("\\{:o}{:o}{:o}", (b >> 6) & 7, (b >> 3) & 7, b & 7),
    }
}

/// Quote a string according to the specified type
/// Port from zsh/Src/utils.c quotestring() (lines 6141-6452)
/// Quote a string per the requested bslashquote style.
/// Port of `quotestring(const char *s, int instring)` from Src/utils.c — used by `print
/// -%q`, `${(q)var}`, completion-output escaping, history
/// re-emission.
/// WARNING: param names don't match C — Rust=(s, quote_type) vs C=(s, instring)
pub fn quotestring(s: &str, quote_type: i32) -> String {
    // c:6141
    if s.is_empty() {
        return if quote_type == QT_NONE {
            String::new()
        } else if quote_type == QT_BACKSLASH || quote_type == QT_BACKSLASH_SHOWNULL {
            "''".to_string()
        } else if quote_type == QT_SINGLE || quote_type == QT_SINGLE_OPTIONAL {
            "''".to_string()
        } else if quote_type == QT_DOUBLE {
            "\"\"".to_string()
        } else if quote_type == QT_DOLLARS {
            "$''".to_string()
        } else {
            String::new()
        };
    }

    if quote_type == QT_NONE {
        s.to_string()
    } else if quote_type == QT_BACKSLASH_PATTERN {
        // Only bslashquote pattern characters (lines 6242-6247)
        let mut result = String::with_capacity(s.len() * 2);
        for c in s.chars() {
            if matches!(
                c,
                '*' | '?' | '[' | ']' | '<' | '>' | '(' | ')' | '|' | '#' | '^' | '~'
            ) {
                result.push('\\');
            }
            result.push(c);
        }
        result
    } else if quote_type == QT_BACKSLASH || quote_type == QT_BACKSLASH_SHOWNULL {
        // Backslash quoting (lines 6260-6416)
        let mut result = String::with_capacity(s.len() * 2);
        for c in s.chars() {
            if ispecial(c) {
                result.push('\\');
            }
            result.push(c);
        }
        result
    } else if quote_type == QT_SINGLE {
        // Single quote: 'string' (lines 6359-6382)
        let mut result = String::with_capacity(s.len() + 4);
        result.push('\'');
        for c in s.chars() {
            if c == '\'' {
                result.push_str("'\\''");
            } else if c == '\n' {
                result.push_str("'$'\\n''");
            } else {
                result.push(c);
            }
        }
        result.push('\'');
        result
    } else if quote_type == QT_SINGLE_OPTIONAL {
        // c:Src/utils.c:6314-6385 QT_SINGLE_OPTIONAL — minimum
        // quoting. Walks the string with two states:
        //   quotesub=1 — not currently inside a quote span. Bare
        //     apostrophes get `\'` (backslash form). Any OTHER
        //     special char triggers back-filling: insert `'` at
        //     `quotestart` (start of unquoted prefix), shifting
        //     subsequent chars right, then push char, transition
        //     to quotesub=2.
        //   quotesub=2 — currently inside a `'…'` span. Bare
        //     apostrophes break the span: push `'\\'`, transition
        //     back to quotesub=1 with quotestart=position-after.
        //     Other specials append in-place.
        // End: if quotesub=2, close with `'`.
        // For "hello world" this yields `'hello world'` (back-
        // filled at start). For "it's" it yields `it\'s` (no quote
        // span ever opens). The naive per-char approach without
        // back-filling produced `hello' 'world` — parity bug.
        let needs_quoting = s.chars().any(ispecial);
        if !needs_quoting {
            return s.to_string();
        }
        let mut result: Vec<char> = Vec::with_capacity(s.len() + 4);
        let mut quotestart: usize = 0; // index in `result` where the next `'` would go
        let mut quotesub: u8 = 1; // 1 = not quoting, 2 = inside `'…'`
        for c in s.chars() {
            if c == '\'' {
                if quotesub == 2 {
                    // close current quote span, then `\'`, then
                    // mark that we may need to reopen on next special
                    result.push('\'');
                    result.push('\\');
                    result.push('\'');
                    quotesub = 1;
                    quotestart = result.len();
                } else {
                    result.push('\\');
                    result.push('\'');
                    quotestart = result.len();
                }
            } else if ispecial(c) {
                if quotesub == 1 {
                    // Back-fill: insert `'` at quotestart, shifting
                    // everything after right by 1.
                    result.insert(quotestart, '\'');
                    quotesub = 2;
                }
                result.push(c);
            } else {
                result.push(c);
            }
        }
        if quotesub == 2 {
            result.push('\'');
        }
        result.into_iter().collect()
    } else if quote_type == QT_DOUBLE {
        // Double quote: "string" (lines 6272-6280, 6311-6312)
        let mut result = String::with_capacity(s.len() + 4);
        result.push('"');
        for c in s.chars() {
            if matches!(c, '$' | '`' | '"' | '\\') {
                result.push('\\');
            }
            result.push(c);
        }
        result.push('"');
        result
    } else if quote_type == QT_DOLLARS {
        // $'...' quoting with escape sequences (lines 6203-6241)
        let mut result = String::with_capacity(s.len() + 4);
        result.push_str("$'");
        for c in s.chars() {
            match c {
                '\\' | '\'' => {
                    result.push('\\');
                    result.push(c);
                }
                '\n' => result.push_str("\\n"),
                '\r' => result.push_str("\\r"),
                '\t' => result.push_str("\\t"),
                '\x1b' => result.push_str("\\e"),
                '\x07' => result.push_str("\\a"),
                '\x08' => result.push_str("\\b"),
                '\x0c' => result.push_str("\\f"),
                '\x0b' => result.push_str("\\v"),
                c if c.is_ascii_control() => {
                    result.push_str(&format!("\\{:03o}", c as u8));
                }
                c => result.push(c),
            }
        }
        result.push('\'');
        result
    } else if quote_type == QT_BACKTICK {
        // Backtick quoting (minimal - just escape backticks)
        s.replace('`', "\\`")
    } else {
        // Unknown quote_type — treat as no-op to match C's `default:` arm.
        s.to_string()
    }
}

// ===========================================================
// xtrace helpers moved from src/ported/vm_helper.
// printprompt4 is a direct port of utils.c:1718-1735; quotedzputs
// is its argument-formatter companion (zsh formats `set -x` lines
// via the same utils.c path).
// ===========================================================

/// Port of `quotedzputs(char const *s, FILE *stream)` from `Src/utils.c:6464`.
///
/// Quote a string for re-readable output (`set -x`, `typeset -p`,
/// `set` listing, etc.). zsh's algorithm under MULTIBYTE_SUPPORT
/// (c:6464-6543):
///   1. empty input → `''`                                     (c:6470-6475)
///   2. needs nice-format (controls / non-printables) → emit
///      `$'<mb_niceformat output>'`                            (c:6478-6492)
///   3. no SPECCHARS member → return string unchanged          (c:6511-6517)
///   4. otherwise wrap in `'…'` (Bourne) or RCQUOTES form,
///      with embedded `'` rewritten as `'\''` / `''`           (c:6533-6587)
///
/// Previous Rust port omitted step 2 (the `$'…'` branch).
/// Result: strings containing control bytes (`\n`, `\t`, escape
/// sequences) were single-quoted instead of `$'…'`-quoted,
/// breaking round-trip through `typeset -p` / `set` / `set -x`
/// because POSIX single-quotes are *strong* — embedded `\n` would
/// be re-fed as a literal newline rather than the C-escape.
///
/// The C signature is `char *quotedzputs(char const *s, FILE *stream)`
/// — when `stream` is non-NULL it writes there and returns NULL,
/// otherwise it returns the quoted string. Rust's variant covers
/// only the `stream==NULL` form (the `set -x` callers all want the
/// string back, not direct stdout writing). The `stream==Some` form
/// is fputs/fputc-direct in C; Rust callers that need writer-based
/// output should compose `print!`/`write!` with this fn's return.
/// WARNING: param names don't match C — Rust=(s) vs C=(s, stream)
pub(crate) fn quotedzputs(s: &str) -> String {
    // c:6464
    // c:6469-6475 — `if (!*s)` empty string emits `''` literal.
    if s.is_empty() {
        return "''".to_string(); // c:6472
    }
    // c:6477-6508 — `is_mb_niceformat(s)` / `is_sb_niceformat(s)` arm:
    // if the string contains nice-formatted chars (controls,
    // non-printables), wrap in `$'…'` using sb/mb_niceformat with
    // NICEFLAG_QUOTE so embedded `'`/`\` get backslash-escaped.
    if is_mb_niceformat(s) != 0 {
        // c:6478-6492 (MULTIBYTE_SUPPORT branch): use mb_niceformat
        //   with NICEFLAG_QUOTE so multi-byte chars round-trip through
        //   wcs_nicechar (raw UTF-8 for printable wides, `\u`/`\U` for
        //   large codepoints) AND `'`/`\\` get backslash-escaped.
        // Under !MULTIBYTE_SUPPORT (c:6494-6508) C would use
        //   sb_niceformat instead. Static-link path: MULTIBYTE is
        //   always available in Rust.
        // c:6485-6492 — `mb_niceformat(s, NULL, &substr,
        //                              NICEFLAG_QUOTE|NICEFLAG_NODUP);`
        let mut substr: Option<String> = None;
        let _ = mb_niceformat(
            s,
            None,
            Some(&mut substr),
            NICEFLAG_QUOTE | NICEFLAG_NODUP,
        );
        return format!("$'{}'", substr.unwrap_or_default()); // c:6488
    }
    // c:6511-6518 — `if (!hasspecial(s)) return dupstring(s);`.
    if !hasspecial(s) {
        // c:6511
        return s.to_string(); // c:6516
    }
    // c:6520-6529 — outstr buffer alloc (zhalloc). Rust uses growable
    // String; the C `l = strlen(s) + 2 + (per-' overhead)` size hint
    // is just allocation tuning and doesn't affect output.
    let mut out = String::with_capacity(s.len() + 2);
    let bytes = s.as_bytes();
    let csh_junkie = isset(CSHJUNKIEQUOTES); // c:6554 / c:6612

    if isset(RCQUOTES) {
        // c:6533 — RCQUOTES: wrap entire string in `'…'`; each
        // embedded `'` becomes `''` (the rc-style doubled quote).
        out.push('\''); // c:6539
        let mut i = 0;
        while i < bytes.len() {
            // c:6540-6547 — decode current byte through Meta/Dash.
            let c = if bytes[i] as char == Dash {
                // c:6541 — `if (*s == Dash) c = '-';`
                i += 1;
                '-'
            } else if bytes[i] == Meta && i + 1 < bytes.len() {
                // c:6543 — `else if (*s == Meta) c = *++s ^ 32;`
                let dec = bytes[i + 1] ^ 32;
                i += 2;
                dec as char
            } else {
                // c:6546 — `else c = *s;`
                let dec = bytes[i];
                i += 1;
                dec as char
            };
            if c == '\'' {
                // c:6548-6553 — `if (c == '\'') *ptr++ = '\'';` (the
                // rc-quote doubling).
                out.push('\''); // c:6553
            } else if c == '\n' && csh_junkie {
                // c:6554-6560 — `if (c == '\n' && isset(CSHJUNKIEQUOTES))
                //                 *ptr++ = '\\';`
                out.push('\\'); // c:6559
            }
            // c:6561-6570 — emit c (metafy on imeta).
            out.push(c); // c:6569 (non-stream branch always re-metafies;
                         // Rust String holds decoded chars directly)
        }
        out.push('\''); // c:6576
    } else {
        // c:6578-6637 — Bourne-style quoting, "avoiding empty quoted
        // strings". Tracks `inquote` so that `it's` becomes
        // `'it'\''s'` (no empty `''` runs).
        let mut inquote = false; // c:6466 (initialised at top of C fn)
        let mut i = 0;
        while i < bytes.len() {
            // c:6579-6586 — decode current byte.
            let c = if bytes[i] as char == Dash {
                i += 1;
                '-' // c:6581
            } else if bytes[i] == Meta && i + 1 < bytes.len() {
                let dec = bytes[i + 1] ^ 32; // c:6583
                i += 2;
                dec as char
            } else {
                let dec = bytes[i]; // c:6585
                i += 1;
                dec as char
            };
            if c == '\'' {
                // c:6587-6602 — `'` closes any open inquote then emits
                // `\'` outside the quotes.
                if inquote {
                    out.push('\''); // c:6593
                    inquote = false; // c:6594
                }
                out.push('\\'); // c:6600
                out.push('\''); // c:6601
            } else {
                // c:6603-6629 — other chars open a quote run if not
                // already open, optionally backslash-escape `\n` under
                // CSHJUNKIEQUOTES, then emit the byte.
                if !inquote {
                    out.push('\''); // c:6609
                    inquote = true; // c:6610
                }
                if c == '\n' && csh_junkie {
                    out.push('\\'); // c:6617
                }
                out.push(c); // c:6627 (imeta-encoding handled by Rust
                             // String storage in the non-stream form)
            }
        }
        if inquote {
            out.push('\''); // c:6636
        }
    }
    // c:6639-6640 — `if (!stream) *ptr++ = '\0';` — Rust String already
    // NUL-terminated implicitly; no-op.
    out // c:6642
}

/// Port of `char *dquotedztrdup(char const *s)` from `Src/utils.c:6648-6723`.
/// Two arms (selected by `isset(CSHJUNKIEQUOTES)` at c:6655):
///
/// **CSHJUNKIEQUOTES path** (c:6656-6686): the csh-junk-quote style
/// where only the non-special sections are wrapped in `"..."` and
/// special chars (`"`, `$`, `` ` ``) appear OUTSIDE the quotes with
/// backslash escape. `\n` inside the quotes gets an extra `\` so it
/// round-trips through history.
///
/// **Default path** (c:6687-6719): wraps the whole string in `"..."`.
/// `\` is doubled to `\\`. `"`, `$`, `` ` `` get backslash-escaped.
/// A trailing `\` gets an extra `\` appended (the `pending` quirk).
///
/// Previously the Rust port only implemented the default arm; the
/// CSHJUNKIEQUOTES path is now ported faithfully.
pub fn dquotedztrdup(s: &str) -> String {
    // c:6648
    let mut out = String::with_capacity(s.len() * 4 + 2);
    let bytes = s.as_bytes();
    // c:6655 — `if (isset(CSHJUNKIEQUOTES))`.
    if isset(CSHJUNKIEQUOTES) {
        let mut inquote = false;
        let mut i = 0;
        while i < bytes.len() {
            // c:6661-6662 — Meta byte decode.
            let c = if bytes[i] == Meta && i + 1 < bytes.len() {
                i += 2;
                (bytes[i - 1] ^ 32) as char
            } else {
                i += 1;
                bytes[i - 1] as char
            };
            match c {
                // c:6664-6673 — `"` / `$` / `` ` `` — close quote
                // (if open), then `\<c>`.
                '"' | '$' | '`' => {
                    if inquote {
                        out.push('"');
                        inquote = false;
                    }
                    out.push('\\');
                    out.push(c);
                }
                // c:6674-6682 — default arm: open quote if needed,
                // backslash-escape newline, emit char.
                _ => {
                    if !inquote {
                        out.push('"');
                        inquote = true;
                    }
                    if c == '\n' {
                        out.push('\\');
                    }
                    out.push(c);
                }
            }
        }
        // c:6685-6686 — close trailing quote.
        if inquote {
            out.push('"');
        }
    } else {
        // c:6687-6718 — default (non-CSH) arm.
        out.push('"');
        let mut pending = false;
        let mut i = 0;
        while i < bytes.len() {
            let c = if bytes[i] == Meta && i + 1 < bytes.len() {
                i += 2;
                (bytes[i - 1] ^ 32) as char
            } else {
                i += 1;
                bytes[i - 1] as char
            };
            match c {
                '\\' => {
                    if pending {
                        out.push('\\');
                    }
                    out.push('\\');
                    pending = true;
                }
                '"' | '$' | '`' => {
                    if pending {
                        out.push('\\');
                    }
                    out.push('\\');
                    out.push(c);
                    pending = false;
                }
                other => {
                    out.push(other);
                    pending = false;
                }
            }
        }
        if pending {
            out.push('\\');
        }
        out.push('"');
    }
    // c:6720 — `ret = metafy(buf, p - buf, META_DUP);` re-metafy result.
    metafy(&out)
}

/// Port of `dquotedzputs(char const *s, FILE *stream)` from
/// Src/utils.c:6729. C body (4 lines):
///     `char *d = dquotedztrdup(s);
///      int ret = zputs(d, stream);
///      zsfree(d);
///      return ret;`
/// Rust returns the quoted string directly (callers compose via
/// `format!` rather than streaming through a FILE*), so the zputs
/// call drops.
pub fn dquotedzputs(s: &str) -> String {
    // c:6729
    dquotedztrdup(s) // c:6731
}

/// Convert UCS-4 to UTF-8 (from utils.c ucs4toutf8)
/// Port of `ucs4toutf8(char *dest, unsigned int wval)` from `Src/utils.c:6743`.
///
/// C accepts 0..=0x7FFFFFFF (legacy UCS-4 / 6-byte UTF-8) — wider
/// than Unicode's 0..=0x10FFFF — to match `wctomb(3)` on Linux
/// with UTF-8 locale. The Rust `char::from_u32` path would reject
/// codepoints above 0x10FFFF (and surrogates), so this port mirrors
/// the C bit-pattern dispatch directly.
///
/// C body (c:6743-6779):
/// ```c
/// if (wval < 0x80) len = 1;            // ASCII
/// else if (wval < 0x800) len = 2;
/// else if (wval < 0x10000) len = 3;
/// else if (wval < 0x200000) len = 4;
/// else if (wval < 0x4000000) len = 5;
/// else if (wval < 0x80000000) len = 6;
/// else { zerr("character not in range"); return -1; }
///
/// switch (len) { /* falls through except to the last case */
/// case 6: dest[5] = (wval & 0x3f) | 0x80; wval >>= 6;
/// case 5: dest[4] = (wval & 0x3f) | 0x80; wval >>= 6;
/// case 4: dest[3] = (wval & 0x3f) | 0x80; wval >>= 6;
/// case 3: dest[2] = (wval & 0x3f) | 0x80; wval >>= 6;
/// case 2: dest[1] = (wval & 0x3f) | 0x80; wval >>= 6;
///     *dest = wval | ((0xfc << (6 - len)) & 0xfc);
///     break;
/// case 1: *dest = wval;
/// }
/// return len;
/// ```
///
/// Returns the encoded bytes (1..=6 long) on success, None on
/// out-of-range. C's `zerr("character not in range")` (c:6763) is
/// not emitted here — the return signals the error.
/// WARNING: param names don't match C — Rust=(wval) vs C=(dest, wval)
pub fn ucs4toutf8(wval: u32) -> Option<String> {
    // c:6743
    let len: usize = if wval < 0x80 {
        1
    }
    // c:6750
    else if wval < 0x800 {
        2
    }
    // c:6752
    else if wval < 0x10000 {
        3
    }
    // c:6754
    else if wval < 0x200000 {
        4
    }
    // c:6756
    else if wval < 0x4000000 {
        5
    }
    // c:6758
    else if wval < 0x80000000 {
        6
    }
    // c:6760
    else {
        // c:6762
        zerr("character not in range"); // c:6763
        return None; // c:6764
    };
    let mut buf = [0u8; 6];
    let mut w = wval;
    // c:6767-6776 — fall-through switch building trailing bytes first.
    match len {
        1 => {
            buf[0] = w as u8;
        } // c:6775
        n => {
            // Trailing (len-1) bytes: each is `(w & 0x3f) | 0x80`,
            // shifting w right 6 between them (c:6768-6772).
            for i in (1..n).rev() {
                buf[i] = ((w & 0x3f) as u8) | 0x80;
                w >>= 6;
            }
            // Leading byte: `w | ((0xfc << (6 - len)) & 0xfc)` (c:6773).
            buf[0] = (w as u8) | (((0xfcu32 << (6 - n)) & 0xfc) as u8);
        }
    }
    Some(String::from_utf8_lossy(&buf[..len]).into_owned())
}

/// Port of `ucs4tomb(unsigned int wval, char *buf)` from `Src/utils.c:6788`.
///
/// Encode a UCS-4 codepoint into the buffer `buf` using the current
/// locale's multibyte encoding. Returns the number of bytes written,
/// or -1 on conversion failure. C body uses `wctomb(3)` when
/// `__STDC_ISO_10646__` is defined (which it is on every modern
/// glibc / macOS libc), falls back to UTF-8 if the codeset is
/// `"UTF-8"`, and uses `iconv(3)` otherwise.
///
/// This Rust port mirrors the primary `wctomb` path via libc FFI;
/// the iconv fallback is unused on macOS/Linux modern builds.
/// On conversion failure, emits `zerr("character not in range")`
/// to match C source line 6794.
///
/// C body shape:
/// ```c
/// int count = wctomb(buf, (wchar_t)wval);
/// if (count == -1) zerr("character not in range");
/// return count;
/// ```
pub fn ucs4tomb(wval: u32, buf: &mut [u8]) -> i32 {
    // libc::wctomb requires at least MB_CUR_MAX bytes (typically 4
    // for UTF-8, 6 for some encodings). Use a stack buffer first,
    // then copy into the caller's buffer.
    // libc crate doesn't expose wctomb on all platforms; declare
    // the POSIX prototype directly. wchar_t is i32 on macOS/Linux
    // for our supported targets.
    extern "C" {
        fn wctomb(s: *mut libc::c_char, wc: libc::wchar_t) -> libc::c_int;
    }
    // libc::c_char is i8 on most targets but u8 on aarch64-linux. Use c_char
    // so the wctomb arg pointer type matches per-target without a cast.
    let mut local = [0 as libc::c_char; 16];
    let count = unsafe { wctomb(local.as_mut_ptr(), wval as libc::wchar_t) };
    if count < 0 {
        zerr("character not in range");
        return -1;
    }
    let n = count as usize;
    if n > buf.len() {
        zerr("character not in range");
        return -1;
    }
    for i in 0..n {
        buf[i] = local[i] as u8;
    }
    count
}

/// Parse getkeystring escape sequences (from utils.c getkeystring)
/// Handles \n \t \r \e \a \b \f \v \\ \' \" \xNN \uNNNN \UNNNNNNNN \0NNN
/// Port of `getkeystring(char *s, int *len, int how, int *misc)` from `Src/utils.c:6915`.
/// WARNING: param names don't match C — Rust=(s) vs C=(s, len, how, misc)
pub fn getkeystring(s: &str) -> (String, usize) {
    // c:6915
    let mut result = String::new();
    let mut chars = s.chars().peekable();
    let mut consumed = 0;

    while let Some(c) = chars.next() {
        consumed += c.len_utf8();
        if c != '\\' {
            result.push(c);
            continue;
        }
        match chars.next() {
            Some('n') => {
                result.push('\n');
                consumed += 1;
            }
            Some('t') => {
                result.push('\t');
                consumed += 1;
            }
            Some('r') => {
                result.push('\r');
                consumed += 1;
            }
            Some('e') | Some('E') => {
                result.push('\x1b');
                consumed += 1;
            }
            Some('a') => {
                result.push('\x07');
                consumed += 1;
            }
            Some('b') => {
                result.push('\x08');
                consumed += 1;
            }
            Some('f') => {
                result.push('\x0c');
                consumed += 1;
            }
            Some('v') => {
                result.push('\x0b');
                consumed += 1;
            }
            Some('\\') => {
                result.push('\\');
                consumed += 1;
            }
            Some('\'') => {
                result.push('\'');
                consumed += 1;
            }
            Some('"') => {
                result.push('"');
                consumed += 1;
            }
            Some('x') => {
                consumed += 1;
                let mut hex = String::new();
                for _ in 0..2 {
                    if let Some(&c) = chars.peek() {
                        if c.is_ascii_hexdigit() {
                            hex.push(chars.next().unwrap());
                            consumed += 1;
                        } else {
                            break;
                        }
                    }
                }
                if let Ok(val) = u8::from_str_radix(&hex, 16) {
                    result.push(val as char);
                }
            }
            Some('u') => {
                consumed += 1;
                let mut hex = String::new();
                for _ in 0..4 {
                    if let Some(&c) = chars.peek() {
                        if c.is_ascii_hexdigit() {
                            hex.push(chars.next().unwrap());
                            consumed += 1;
                        } else {
                            break;
                        }
                    }
                }
                if let Ok(val) = u32::from_str_radix(&hex, 16) {
                    if let Some(c) = char::from_u32(val) {
                        result.push(c);
                    }
                }
            }
            Some('U') => {
                consumed += 1;
                let mut hex = String::new();
                for _ in 0..8 {
                    if let Some(&c) = chars.peek() {
                        if c.is_ascii_hexdigit() {
                            hex.push(chars.next().unwrap());
                            consumed += 1;
                        } else {
                            break;
                        }
                    }
                }
                if let Ok(val) = u32::from_str_radix(&hex, 16) {
                    if let Some(c) = char::from_u32(val) {
                        result.push(c);
                    }
                }
            }
            Some(c @ '0'..='7') => {
                consumed += 1;
                let mut oct = String::new();
                oct.push(c);
                for _ in 0..2 {
                    if let Some(&c) = chars.peek() {
                        if ('0'..='7').contains(&c) {
                            oct.push(chars.next().unwrap());
                            consumed += 1;
                        } else {
                            break;
                        }
                    }
                }
                if let Ok(val) = u8::from_str_radix(&oct, 8) {
                    result.push(val as char);
                }
            }
            Some('c') => {
                consumed += 1;
                // \cX = control character
                if let Some(c) = chars.next() {
                    consumed += 1;
                    result.push((c as u8 & 0x1f) as char);
                }
            }
            Some(c) => {
                consumed += 1;
                result.push('\\');
                result.push(c);
            }
            None => {
                result.push('\\');
            }
        }
    }
    (result, consumed)
}

/// Check if s is a prefix of t (from utils.c strpfx)
// Return non-zero if s is a prefix of t.                                  // c:7345
pub fn strpfx(s: &str, t: &str) -> bool {
    t.starts_with(s)
}

/// Check if s is a suffix of t (from utils.c strsfx)
// Return non-zero if s is a suffix of t.                                  // c:7345
pub fn strsfx(s: &str, t: &str) -> bool {
    t.ends_with(s)
}

/// Go up n directories (from utils.c upchdir)
/// Port of `upchdir(int n)` from `Src/utils.c:7356`.
pub fn upchdir(n: usize) -> io::Result<()> {
    let mut path = String::new();
    for i in 0..n {
        if i > 0 {
            path.push('/');
        }
        path.push_str("..");
    }
    std::env::set_current_dir(&path)?;
    Ok(())
}

/// Port of `struct dirsav` from `Src/zsh.h:1159`.
///
/// ```c
/// struct dirsav {
///     int dirfd, level;
///     char *dirname;
///     dev_t dev;
///     ino_t ino;
/// };
/// ```
///
/// The previous Rust port omitted `dev` and `ino` which the
/// `restoredir` integrity check (utils.c:7592) reads. Adding them
/// so callers can verify the saved-and-restored cwd matches the
/// captured device + inode.
// `struct dirsav` lives in `dirsav` per Rule C
// (its C definition is `Src/zsh.h:1159`, not utils.c). The previous
// Rust port had a `pub struct DirSav` PascalCase duplicate of the
// canonical lowercase struct; deleted in favour of routing through
// `zsh_h::dirsav` directly.

/// Port of `init_dirsav(Dirsav d)` from `Src/utils.c:7381`. Initialize a
/// `dirsav` struct to its empty/default state. C body memset's the
/// fields to 0 (dirfd to -1).
///
/// C signature: `void init_dirsav(Dirsav d)` where
/// `Dirsav = struct dirsav *`. Rust port returns the initialised
/// struct since callers always pair-with a fresh allocation.
/// WARNING: param names don't match C — Rust=() vs C=(path)
/// C body (3 lines):
///   `d->ino = d->dev = 0; d->dirname = NULL; d->dirfd = d->level = -1;`
/// The C `dirname = NULL` becomes `dirname: None`; Rust port prefills
/// dirname with current_dir for legacy callers that immediately read
/// it (mirrors what `setpwd()` does in C right after `init_dirsav`).
pub fn init_dirsav() -> dirsav {
    // c:7381
    dirsav {
        dirfd: -1,
        level: 0,
        dev: 0,
        ino: 0, // c:7383-7385
        dirname: std::env::current_dir()
            .ok()
            .map(|p| p.to_string_lossy().to_string()),
    }
}

/// Change directory with safeguards (from utils.c lchdir)
/// Port of `lchdir(char const *path, struct dirsav *d, int hard)` from `Src/utils.c:7400`.
/// WARNING: param names don't match C — Rust=(path) vs C=(path, d, hard)
pub fn lchdir(path: &str) -> io::Result<()> {
    let resolved = if path.starts_with('/') {
        PathBuf::from(path)
    } else {
        let cwd = std::env::current_dir()?;
        cwd.join(path)
    };
    std::env::set_current_dir(&resolved)?;
    Ok(())
}

/// Port of `restoredir(struct dirsav *d)` from `Src/utils.c:7565`.
///
/// ```c
/// int restoredir(struct dirsav *d) {
///     if (d->dirname && *d->dirname == '/')
///         return chdir(d->dirname);
///     if (d->dirfd >= 0) {
///         if (!fchdir(d->dirfd)) {
///             if (!d->dirname) return 0;
///             else if (chdir(d->dirname)) {
///                 close(d->dirfd); d->dirfd = -1; err = -2;
///             }
///         } else {
///             close(d->dirfd); d->dirfd = err = -1;
///         }
///     } else if (d->level > 0)
///         err = upchdir(d->level);
///     else if (d->level < 0) err = -1;
///     // dev/ino integrity check ...
/// }
/// ```
///
/// Restore the cwd captured in `d`. Absolute `dirname` short-
/// circuits to `chdir`. Otherwise tries `fchdir(dirfd)` (when
/// supported) then falls through to `upchdir(level)` for the
/// nested-fn-exit case. Returns 0 on success, non-zero on failure
/// (matching C's int return).
///
/// Signature change: previous Rust port took `saved: &str` and
/// returned `bool` — different shape from C, missed the dirfd /
/// level / dev / ino fields entirely.
pub fn restoredir(d: &mut dirsav) -> i32 {
    // C: if (d->dirname && *d->dirname == '/') return chdir(d->dirname);
    if let Some(name) = d.dirname.as_ref() {
        if name.starts_with('/') {
            return match std::env::set_current_dir(name) {
                Ok(_) => 0,
                Err(_) => -1,
            };
        }
    }
    let mut err: i32 = 0;
    // C: HAVE_FCHDIR path — try fchdir(dirfd) first.
    #[cfg(unix)]
    if d.dirfd >= 0 {
        let rc = unsafe { libc::fchdir(d.dirfd) };
        if rc == 0 {
            if d.dirname.is_none() {
                return 0;
            }
            let name = d.dirname.as_ref().unwrap();
            if std::env::set_current_dir(name).is_err() {
                unsafe { libc::close(d.dirfd) };
                d.dirfd = -1;
                err = -2;
            }
        } else {
            unsafe { libc::close(d.dirfd) };
            d.dirfd = -1;
            err = -1;
        }
    } else if d.level > 0 {
        // C: err = upchdir(d->level);
        let _ = upchdir(d.level as usize);
    } else if d.level < 0 {
        err = -1;
    }
    // C: dev/ino integrity check after the chdir/fchdir.
    if (d.dev != 0 || d.ino != 0) && err == 0 {
        if let Ok(meta) = fs::metadata(".") {
            if meta.ino() != d.ino || meta.dev() != d.dev {
                err = -1;
            }
        } else {
            err = -1;
        }
    }
    err
}

/// Port of `privasserted()` from `Src/utils.c:7607`.
///
/// "Check whether the shell is running with privileges in effect.
/// This is the case if EITHER the euid is zero, OR (if the system
/// supports POSIX.1e (POSIX.6) capability sets) the process'
/// Effective or Inheritable capability sets are non-empty."
///
/// ```c
/// if (!geteuid()) return 1;
/// #ifdef HAVE_CAP_GET_PROC
///     cap_t caps = cap_get_proc();
///     if (caps) {
///         cap_flag_value_t val;
///         for (cap_value_t cap = 0;
///              !cap_get_flag(caps, cap, CAP_EFFECTIVE, &val); cap++)
///             if (val && cap != CAP_WAKE_ALARM) {
///                 cap_free(caps);
///                 return 1;
///             }
///     }
///     cap_free(caps);
/// #endif
///     return 0;
/// ```
///
/// The previous Rust port checked `getuid() != geteuid()` which is
/// the SUID-binary detection, not the "running with privileges"
/// check. The capability-set inspection requires libcap (gated
/// behind the `libcap` feature in `crate::ported::modules::cap`);
/// without it, only the euid==0 path is exercised — same as the
/// C `#else` arm when HAVE_CAP_GET_PROC isn't defined.
pub fn privasserted() -> bool {
    #[cfg(unix)]
    {
        if unsafe { libc::geteuid() } == 0 {
            return true;
        }
    }
    // POSIX.1e capabilities check (HAVE_CAP_GET_PROC) — only
    // active on Linux when zshrs is built with `--features
    // libcap`. The cap module's `cap_get_proc` returns Ok(text)
    // when the process has any capability set; we treat any
    // non-default-empty result as "privileges asserted".
    #[cfg(all(target_os = "linux", feature = "libcap"))]
    {
        // Pending: walk the capability set with cap_get_flag and
        // skip CAP_WAKE_ALARM as the C source does. The cap.rs
        // port doesn't yet expose the flag-iteration FFI; until
        // it does, conservative-true on any non-empty cap text.
        if let Ok(text) = crate::ported::modules::cap::cap_get_proc() {
            // Empty / default-empty cap text = no privileges.
            if !text.is_empty() && text != "=" {
                return true;
            }
        }
    }
    false
}

/// Port of `mode_to_octal(mode_t mode)` from `Src/utils.c:7634`.
///
/// Convert a `mode_t` into the equivalent canonical octal value
/// by testing each `S_I*` flag explicitly and OR-ing the matching
/// octal bit. This is NOT just `mode & 07777` — on systems where
/// the libc `S_IRUSR`/etc. constants don't match the canonical
/// values (e.g. when zsh runs against a non-POSIX libc),
/// `mode & 07777` returns the libc representation. The C version
/// translates to canonical bits explicitly so callers get a stable
/// portable result.
///
/// ```c
/// int mode_to_octal(mode_t mode)
/// {
///     int m = 0;
///     if (mode & S_ISUID) m |= 04000;
///     ... (12 bit-by-bit mappings)
///     return m;
/// }
/// ```
pub fn mode_to_octal(mode: u32) -> i32 {
    // c:7634
    #[cfg(not(unix))]
    {
        // No POSIX permission bits on non-Unix; fall back to canonical
        // octal-bit layout via the same mask values C uses on POSIX.
        let mut o: i32 = 0;
        if mode & 0o4000 != 0 {
            o |= 0o4000;
        } // c:7638-7639
        if mode & 0o2000 != 0 {
            o |= 0o2000;
        } // c:7640-7641
        if mode & 0o1000 != 0 {
            o |= 0o1000;
        } // c:7642-7643
        if mode & 0o0400 != 0 {
            o |= 0o0400;
        } // c:7644-7645
        if mode & 0o0200 != 0 {
            o |= 0o0200;
        } // c:7646-7647
        if mode & 0o0100 != 0 {
            o |= 0o0100;
        } // c:7648-7649
        if mode & 0o0040 != 0 {
            o |= 0o0040;
        } // c:7650-7651
        if mode & 0o0020 != 0 {
            o |= 0o0020;
        } // c:7652-7653
        if mode & 0o0010 != 0 {
            o |= 0o0010;
        } // c:7654-7655
        if mode & 0o0004 != 0 {
            o |= 0o0004;
        } // c:7656-7657
        if mode & 0o0002 != 0 {
            o |= 0o0002;
        } // c:7658-7659
        if mode & 0o0001 != 0 {
            o |= 0o0001;
        } // c:7660-7661
        return o; // c:7662
    }
    #[cfg(unix)]
    {
        // c:7636 — int m = 0;
        let mut m: i32 = 0;
        // c:7638-7661 — 12 bit-by-bit mappings from libc S_I* → canonical octal.
        if mode & S_ISUID as u32 != 0 {
            m |= 0o4000;
        } // c:7638-7639
        if mode & S_ISGID as u32 != 0 {
            m |= 0o2000;
        } // c:7640-7641
        if mode & S_ISVTX as u32 != 0 {
            m |= 0o1000;
        } // c:7642-7643
        if mode & S_IRUSR as u32 != 0 {
            m |= 0o0400;
        } // c:7644-7645
        if mode & S_IWUSR as u32 != 0 {
            m |= 0o0200;
        } // c:7646-7647
        if mode & S_IXUSR as u32 != 0 {
            m |= 0o0100;
        } // c:7648-7649
        if mode & S_IRGRP as u32 != 0 {
            m |= 0o0040;
        } // c:7650-7651
        if mode & S_IWGRP as u32 != 0 {
            m |= 0o0020;
        } // c:7652-7653
        if mode & S_IXGRP as u32 != 0 {
            m |= 0o0010;
        } // c:7654-7655
        if mode & S_IROTH as u32 != 0 {
            m |= 0o0004;
        } // c:7656-7657
        if mode & S_IWOTH as u32 != 0 {
            m |= 0o0002;
        } // c:7658-7659
        if mode & S_IXOTH as u32 != 0 {
            m |= 0o0001;
        } // c:7660-7661
        m // c:7662
    }
}

/// Port of `mailstat(char *path, struct stat *st)` from `Src/utils.c:7685`.
///
/// C signature: `int mailstat(char *path, struct stat *st)`.
/// Writes maildir aggregate stats into `*st` (or the native stat for
/// non-directory paths) and returns the underlying `stat(2)` return
/// (0 on success, -1 on error — matches C's `i = stat(path, st);
/// return i;`).
///
/// When `path` is a maildir directory (containing `cur/`, `tmp/`,
/// `new/` subdirs), walks `new/` + `cur/` and aggregates into `*st`:
/// nlink=1, S_IFDIR→S_IFREG, size=Σ message bytes,
/// blocks=Σ messages, atime=newest, mtime=newest. When the path is
/// a plain file, leaves the native `stat(2)` result in `*st`.
pub fn mailstat(path: &str, st: &mut libc::stat) -> i32 {
    // c:7685
    let c_path = match CString::new(path) {
        Ok(c) => c,
        Err(_) => return -1,
    };
    // C: if ((i = stat(path, st)) != 0 || !S_ISDIR(st->st_mode)) return i;
    let i = unsafe { libc::stat(c_path.as_ptr(), st as *mut _) }; // c:7693
    if i != 0 || (st.st_mode & libc::S_IFMT) != libc::S_IFDIR {
        // c:7693
        return i; // c:7693
    }
    // C 7700-7706: nlink=1, S_IFDIR → S_IFREG, zero size/blocks.
    st.st_nlink = 1; // c:7701
    st.st_mode &= !libc::S_IFDIR; // c:7702
    st.st_mode |= libc::S_IFREG; // c:7703
    st.st_size = 0; // c:7704
    st.st_blocks = 0; // c:7705
                      // C 7707-7712: stat(path/cur). If absent or not a dir, return 0
                      // with the partial out (just the IFREG-coerced root).
    let cur_path = match CString::new(format!("{}/cur", path)) {
        Ok(c) => c,
        Err(_) => return 0,
    };
    let mut sub: libc::stat = unsafe { std::mem::zeroed() };
    if unsafe { libc::stat(cur_path.as_ptr(), &mut sub) } != 0               // c:7708
        || (sub.st_mode & libc::S_IFMT) != libc::S_IFDIR
    // c:7708
    {
        return 0; // c:7710
    }
    st.st_atime = sub.st_atime; // c:7712
                                // C 7715-7722: stat(path/tmp).
    let tmp_path = match CString::new(format!("{}/tmp", path)) {
        Ok(c) => c,
        Err(_) => return 0,
    };
    if unsafe { libc::stat(tmp_path.as_ptr(), &mut sub) } != 0               // c:7716
        || (sub.st_mode & libc::S_IFMT) != libc::S_IFDIR
    // c:7716
    {
        return 0; // c:7718
    }
    st.st_mtime = sub.st_mtime; // c:7720
                                // C 7724-7730: stat(path/new). C overwrites mtime with new/'s mtime.
    let new_path = match CString::new(format!("{}/new", path)) {
        Ok(c) => c,
        Err(_) => return 0,
    };
    if unsafe { libc::stat(new_path.as_ptr(), &mut sub) } != 0               // c:7724
        || (sub.st_mode & libc::S_IFMT) != libc::S_IFDIR
    // c:7724
    {
        return 0; // c:7726
    }
    st.st_mtime = sub.st_mtime; // c:7728
                                // C 7749-7778: walk new/ and cur/, sum size + blocks, track newest
                                // atime / mtime.
    let mut atime: libc::time_t = 0; // c:7748
    let mut mtime: libc::time_t = 0; // c:7748
    for sub_name in ["new", "cur"] {
        let dir = format!("{}/{}", path, sub_name);
        let entries = match fs::read_dir(&dir) {
            Ok(e) => e,
            Err(_) => return 0,
        };
        for entry in entries.flatten() {
            let name = entry.file_name();
            let name_bytes = name.as_encoded_bytes();
            // C: if (fn->d_name[0] == '.') continue;
            if name_bytes.first() == Some(&b'.') {
                // c:7758
                continue;
            }
            let entry_path = match CString::new(entry.path().to_string_lossy().as_bytes()) {
                Ok(c) => c,
                Err(_) => continue,
            };
            let mut entry_st: libc::stat = unsafe { std::mem::zeroed() };
            if unsafe { libc::stat(entry_path.as_ptr(), &mut entry_st) } != 0 {
                // c:7762
                continue;
            }
            st.st_size += entry_st.st_size; // c:7766
            st.st_blocks += 1; // c:7767
                               // C: if (atime != mtime && atime > atime_max) atime_max = atime;
            if entry_st.st_atime != entry_st.st_mtime && entry_st.st_atime > atime {
                // c:7769
                atime = entry_st.st_atime;
            }
            if entry_st.st_mtime > mtime {
                // c:7771
                mtime = entry_st.st_mtime;
            }
        }
    }
    // C 7783-7784: if (atime) st_ret.st_atime = atime;
    if atime != 0 {
        // c:7783
        st.st_atime = atime;
    }
    if mtime != 0 {
        // c:7785
        st.st_mtime = mtime;
    }
    0 // c:7787
}

/// Script name for error messages
pub static mut SCRIPT_NAME: Option<String> = None;
/// Script filename
pub static mut SCRIPT_FILENAME: Option<String> = None;

/// Print a fatal error to stderr.
/// Port of `zerr(VA_ALIST1(const char *fmt))` from Src/utils.c:172. C source sets `errflag`
/// after emitting `<prefix>: <msg>\n` so the running script aborts
/// at the next safe point. The Rust port currently just prints —
// =====================================================================
// Module-static state for the zerr/zwarn/zerrnam/zwarnnam family —
// port of the file-statics in Src/init.c + Src/exec.c that
// `zwarning()` (utils.c:142) reads to build the error prefix.
// =====================================================================

// name of script being sourced                                             // c:33
/// Port of `char *scriptname` from `Src/init.c`. Set when `source`
/// is reading a script; cleared on return. Used by `zwarning()`
/// (utils.c:147) as the diagnostic prefix.
static SCRIPTNAME: std::sync::OnceLock<Mutex<Option<String>>> =
    std::sync::OnceLock::new();

/// Port of `char *argzero` from `Src/init.c`. The shell's argv[0].
/// Used by `zwarning()` (utils.c:147) as the fallback diagnostic
/// prefix when scriptname is unset.
static ARGZERO: std::sync::OnceLock<Mutex<Option<String>>> = std::sync::OnceLock::new();

/// Port of `char *scriptfilename` from `Src/init.c` (listed in
/// `zsh.export:356`). The FILE that the current code was PARSED
/// from. Distinct from `scriptname` (which doshfunc overrides on
/// function entry at c:5903). `scriptfilename` stays at the outer
/// file across function calls; PS4's `%x` reads it at
/// `Src/prompt.c:937`.
///
/// Set at init.c:479 (`-c` mode → `scriptname = scriptfilename
/// = "zsh"`), init.c:1367 (`source` enters the named file),
/// init.c:1558+1592+1667 (save/install/restore around `.` /
/// `source` bin_dot dispatch).
static SCRIPTFILENAME: std::sync::OnceLock<Mutex<Option<String>>> =
    std::sync::OnceLock::new();

/// Port of `char *posixzero` from `Src/params.c:76`. The original
/// argv[0] preserved unchanged by later mutations. Used by
/// `argzerogetfn` for `$0` under `isset(POSIXARGZERO)`.
static POSIXZERO: std::sync::OnceLock<Mutex<Option<String>>> =
    std::sync::OnceLock::new();

// error flag: bits from enum errflag_bits                                 // c:124
/// Port of `int errflag` from `Src/init.c`. Tracks whether an
/// error has been raised (`ERRFLAG_ERROR = 1`) or break/return
/// is in flight (`ERRFLAG_INT = 2`).
///
/// Direct global access: `errflag.load(Ordering::Relaxed)` reads
/// the current value, `errflag.fetch_or(ERRFLAG_ERROR, …)` matches
/// C's `errflag |= ERRFLAG_ERROR`, `errflag.store(0, …)` matches
/// C's `errflag = 0`.
#[allow(non_upper_case_globals)]
pub static errflag: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);

/// Port of `int noerrs` from `Src/init.c`. Counter — when `> 0`,
/// suppresses error printing. `noerrs >= 2` also suppresses the
/// `errflag` set inside `zerr`/`zerrnam`.
static NOERRS: std::sync::OnceLock<Mutex<i32>> = std::sync::OnceLock::new();

/// Port of `int locallevel` from `Src/init.c`. Function-call depth
/// (0 = top-level, 1+ = inside a fn). `zwarning()` checks this in
/// the script-prefix path (utils.c:150).
// `LOCALLEVEL` removed — see `locallevel_lock` removal comment
// below. Canonical static lives in `LOCALLEVEL`
// (port of params.c:54).

/// Port of `int lineno` from `Src/init.c`. Current line number;
/// `zerrmsg()` includes it in the diagnostic when locallevel > 0
/// or shinstdin is unset (utils.c:301).
static LINENO: std::sync::OnceLock<Mutex<i32>> = std::sync::OnceLock::new();

/// Port of the `isset(SHINSTDIN)` check from utils.c:150. C reads
/// the SHINSTDIN option directly; the Rust port caches it here so
/// callers don't pull in the whole option-table for every error.
static SHINSTDIN_OPT: std::sync::OnceLock<Mutex<bool>> = std::sync::OnceLock::new();

/// `ERRFLAG_ERROR` from `Src/zsh.h`. Set on `zerr`/`zerrnam` to
/// signal a fatal error has occurred.
pub const ERRFLAG_ERROR: i32 = 1;

/// Port of `mod_export int incompfunc` from `Src/utils.c:46`.
/// Set non-zero while a `comp*` builtin is dispatching from inside
/// a user-defined completion function — guards `comparguments` /
/// `compset` / `compadd` / `compdescribe` / `comptags` / `compvalues`
/// / `compfiles` / `compgroups` / `compquote` against being called
/// outside the `compfunc` shfunc context (each builtin checks
/// `INCOMPFUNC` early and emits "can only be called from completion
/// function" when zero).
pub static INCOMPFUNC: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0); // c:46

/// Port of `mod_export int resetneeded` from `Src/utils.c:1821`.
/// Set when the editor needs a redraw — incremented by widgets +
/// signal handlers (e.g. SIGWINCH); the next prompt loop tick clears
/// it after running zrefresh.
pub static RESETNEEDED: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0); // c:1821

/// Port of `mod_export int winchanged` from `Src/utils.c:1827`.
/// Set by the SIGWINCH handler — the next refresh re-queries the
/// terminal size and re-renders, then clears the flag.
pub static WINCHANGED: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0); // c:1827

/// Port of C's `GETKEY_*` flag enumeration from `Src/zsh.h:3143`.
/// Passed to `getkeystring_with` to alter escape interpretation:
///   - GETKEY_OCTAL_ESC: `\NNN` octal escapes are interpreted (1<<0)
///   - GETKEY_EMACS: unknown `\<char>` drops the backslash (1<<1)
///   - GETKEY_BACKSLASH_C: `\c` truncates the result (1<<3)
pub const GETKEY_OCTAL_ESC: u32 = 1 << 0; // c:zsh.h:3143
pub const GETKEY_EMACS: u32 = 1 << 1; // c:zsh.h:3150
pub const GETKEY_CTRL: u32 = 1 << 2; // c:zsh.h:3152
pub const GETKEY_BACKSLASH_C: u32 = 1 << 3; // c:zsh.h:3154

/// `GETKEYS_PRINT = GETKEY_OCTAL_ESC | GETKEY_BACKSLASH_C |
/// GETKEY_EMACS` per Src/zsh.h:3185 — the flag set `bin_print` uses
/// when interpreting escapes (with EMACS: unknown `\<c>` → `<c>`).
pub const GETKEYS_PRINT: u32 = GETKEY_OCTAL_ESC | GETKEY_EMACS | GETKEY_BACKSLASH_C; // c:zsh.h:3185

/// `GETKEYS_ECHO = GETKEY_BACKSLASH_C` per Src/zsh.h:3178 — the flag
/// set `echo` uses (and `print -e`). NOT GETKEY_EMACS: unknown
/// `\<c>` is preserved as literal `\<c>`. NOT GETKEY_OCTAL_ESC:
/// `\NNN` is NOT interpreted as octal. Only `\a \b \e \E \f \n \r
/// \t \v \\` (plus `\c` truncation) are handled.
///
/// Per builtin.c:4754-4760 — `bin_print` selects this for BIN_ECHO
/// or when `-e` is set; otherwise GETKEYS_PRINT.
pub const GETKEYS_ECHO: u32 = GETKEY_BACKSLASH_C; // c:zsh.h:3178

/// Static `int ep` from Src/utils.c:4775 — sticky flag suppressing the
/// `can't set tty pgrp` warning after the first failure.
static ATTACHTTY_EP: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0); // c:4775

// =====================================================================
// !!! WARNING: RUST-ONLY HELPERS — NO DIRECT C COUNTERPART !!!
// =====================================================================
//
// These three ported (`fdtable_lock`, `fdtable_get`, `fdtable_set`) DO
// NOT EXIST as functions in `Src/utils.c`. The C source declares
// `fdtable` as a bare `unsigned char *` global (Src/utils.c:~63) and
// every call site reads/writes it as a direct array index:
//
//     if (fdtable[fd] != FDT_UNUSED) ...
//     fdtable[fd] = FDT_EXTERNAL;
//
// Rust can't hand out raw mutable indexes through a `Mutex<Vec<u8>>`
// safely without a borrow scope, so the Rust port wraps the same
// slot access in three `pub fn`s. Each call site to these helpers
// corresponds 1:1 to a `fdtable[fd] = X` or `fdtable[fd] != X`
// statement in the C source — they are NOT new policy, they only
// adapt the storage shape from `unsigned char *` to a growable
// `Mutex<Vec<i32>>`.
//
// Also adapts `growfdtable` (Src/utils.c:1965) by lazily growing the
// Vec inside `fdtable_set` instead of a separate `growfdtable`
// call — the C source calls `growfdtable(fd)` immediately before
// every `fdtable[fd] = X` write anyway.
//
// !!! Do NOT use these for any state that the C source does not
// already store in `fdtable[]`. Adding a new "fd kind" here is a
// scope expansion, not a port. !!!
// =====================================================================

static FDTABLE: std::sync::OnceLock<Mutex<Vec<i32>>> = std::sync::OnceLock::new();

/// Port of `int max_zsh_fd` global from `Src/exec.c:201`.
/// "The highest fd we know zsh is using" — bumped by `fdtable_set`
/// callers, shrunk by `zclose` when trailing slots fall to UNUSED.
pub static MAX_ZSH_FD: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(-1);

/// Port of `int fdtable_flocks` global from `Src/exec.c:204`.
/// Count of `FDT_FLOCK`-tagged fds; consulted by `closem()` to
/// decide whether to skip the flock-fd sweep.
pub static FDTABLE_FLOCKS: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);

/// Setter for `scriptname`. Called from `bin_dot` / `source`
/// when entering a script.
pub fn set_scriptname(name: Option<String>) {
    *scriptname_lock().lock().unwrap() = name;
}

/// Read `scriptname` — direct mirror of the C file-scope
/// `char *scriptname;` at `Src/utils.c:36`. Exposed for the
/// prompt expander (`%N`), `bin_dot`, and ZLE source tracking.
pub fn scriptname_get() -> Option<String> {
    scriptname_lock().lock().unwrap().clone()
}

/// Setter for `scriptfilename` — direct mirror of the C
/// `scriptfilename` global at `Src/init.c` (zsh.export:356).
/// Called from `bin_dot` at init.c:1592, restored at c:1667.
pub fn set_scriptfilename(name: Option<String>) {
    *scriptfilename_lock().lock().unwrap() = name;
}

/// Read `scriptfilename` — direct mirror of the C global.
/// Used by PS4's `%x` (`Src/prompt.c:937`) and the function-call
/// frame at `Src/exec.c:1613` (`fstack.filename = scriptfilename`).
pub fn scriptfilename_get() -> Option<String> {
    scriptfilename_lock().lock().unwrap().clone()
}

/// Read `locallevel` — function nesting depth. Routes through the
/// canonical `LOCALLEVEL` static (port of
/// `Src/params.c:54`). The prior Rust port stored a duplicate
/// `Mutex<i32>` here that split state from params.rs; both copies
/// were never kept in sync.
pub fn locallevel() -> i32 {
    LOCALLEVEL.load(Ordering::Relaxed)
}

/// Bump `locallevel` (called by `startparamscope`).
pub fn inc_locallevel() {
    LOCALLEVEL.fetch_add(1, Ordering::Relaxed);
}

/// Decrement `locallevel` (called by `endparamscope`).
pub fn dec_locallevel() {
    LOCALLEVEL.fetch_sub(1, Ordering::Relaxed);
}

/// Setter for `argzero`. Called once at shell init from `parseargs`.
pub fn set_argzero(name: Option<String>) {
    // c:271 (init.c) — `argv0 = argzero = posixzero = *argv++;`. At
    // shell init both argzero and posixzero share the same source.
    // posixzero is preserved unchanged after later mutations (exec -a,
    // function frames) — argzero is what gets rewritten. If posixzero
    // hasn't been independently set yet, mirror argzero so the
    // POSIXARGZERO branch in `argzerogetfn` returns something useful.
    let mut posix_lock = posixzero_lock().lock().unwrap();
    if posix_lock.is_none() {
        *posix_lock = name.clone();
    }
    drop(posix_lock);
    *argzero_lock().lock().unwrap() = name;
}

/// Read `argzero`. Used by `argzerogetfn` for `$0`.
pub fn argzero() -> Option<String> {
    argzero_lock().lock().unwrap().clone()
}

/// Port of `char *posixzero` from `Src/params.c:76`. The original
/// `argv[0]` preserved from shell startup, unchanged by later `exec -a`
/// or function-call rewrites. C's `argzerogetfn` (params.c:4958)
/// returns this instead of `argzero` when `isset(POSIXARGZERO)`.
///
/// Set once at init via `set_posixzero` (called from the init path
/// in C at init.c:271/297/321). `set_argzero` mirrors the value here
/// on first call so the POSIXARGZERO branch has something to read
/// even if the init path hasn't run yet.
pub fn set_posixzero(name: Option<String>) {
    *posixzero_lock().lock().unwrap() = name;
}

/// Read `posixzero`. C: `posixzero` (params.c:76 global). Used by
/// `argzerogetfn` when `isset(POSIXARGZERO)`.
pub fn posixzero() -> Option<String> {
    posixzero_lock().lock().unwrap().clone()
}

/// Setter for `noerrs`. Increment to suppress error output;
/// decrement to restore.
pub fn set_noerrs(v: i32) {
    *noerrs_lock().lock().unwrap() = v;
}

/// Setter for `locallevel`. Called by function-call entry/exit.
pub fn set_locallevel(v: i32) {
    LOCALLEVEL.store(v, Ordering::Relaxed);
}

/// Setter for `lineno`. Called by the parser as it advances.
pub fn set_lineno(v: i32) {
    *lineno_lock().lock().unwrap() = v;
}

/// Setter for the cached SHINSTDIN flag. Called by the option
/// machinery whenever `setopt shinstdin` / `unsetopt shinstdin`
/// fires.
pub fn set_shinstdin(v: bool) {
    *shinstdin_lock().lock().unwrap() = v;
}

/// Check if string is a valid identifier
// `isident` DELETED — fake duplicate (cited `c:params.c:1288` from
// utils.rs, location mismatch). Canonical port is
// `isident` at `params.rs:2056`, which
// correctly handles namespace prefix (`ns.var`) per the real C
// body — the utils.rs copy dropped that arm. Callers now route
// through `isident` directly.

// QuoteType enum + impl deleted — the canonical quote-type values are
// the bare `QT_*: i32` constants at `zsh_h.rs:175` (port of anonymous
// `enum { QT_NONE, QT_BACKSLASH, … }` from `Src/zsh.h:253-298`).
// `quotestring()` takes `quote_type: i32` matching C's signature.

/// Map a `(q)` flag count to a `QT_*` value.
/// Port of the q-flag dispatch in `Src/subst.c` `paramsubst()` —
/// `(q)`=`QT_BACKSLASH`, `(qq)`=`QT_SINGLE`, `(qqq)`=`QT_DOUBLE`,
/// `(qqqq+)`=`QT_DOLLARS`.
pub fn qflag_quotetype(count: u32) -> i32 {
    match count {
        0 => QT_NONE,
        1 => QT_BACKSLASH,
        2 => QT_SINGLE,
        3 => QT_DOUBLE,
        _ => QT_DOLLARS,
    }
}

/// Port of `ispecial()` macro from `Src/ztype.h:59` — `zistype(X,
/// ISPECIAL)`. C populates the ISPECIAL typtab bit at
/// `Src/utils.c:4253-4262`:
///   - Every byte in `SPECCHARS` (`Src/zsh.h:228` =
///     `"#$^*()=|{}[]\`<>?~;&\\n\\t \\\\\\'\\""`)
///   - `,` when `typtab_flags & ZTF_SP_COMMA` (set by
///     `makecommaspecial(1)` per c:4271)
///   - `bangchar` (default `!`) when `BANGHIST` is set AND
///     `ZTF_INTERACT` is set (interactive shell)
///
/// The Rust port enumerates `SPECCHARS` directly because the
/// typtab is not lazily initialised — production `ispecial` calls
/// happen after `init_typtab` runs at shell startup, but
/// `quotestring` is unit-tested in isolation where the typtab is
/// all-zero. Match the C SPECCHARS set byte-for-byte; the option-
/// driven augments (`,` and bangchar) are still NOT wired through
/// (a known gap — see the typtab-routing TODO at the param level).
fn ispecial(c: char) -> bool {
    // c:59 (Src/ztype.h)
    // c:228 (Src/zsh.h) `SPECCHARS` — exact byte set from the C
    // string literal. `^` and `{`/`}` were already present; `!`
    // is INTENTIONALLY OMITTED here because C only ISPECIAL-tags
    // bangchar under BANGHIST + interactive. Including `!`
    // unconditionally diverges from C for non-interactive scripts
    // and unbang'd input.
    matches!(
        c,
        '#' | '$' | '^' | '*' | '(' | ')' | '='  // c:228 first half
            | '|' | '{' | '}' | '[' | ']' | '`'   // c:228 mid
            | '<' | '>' | '?' | '~' | ';' | '&'   // c:228 specials
            | '\n' | '\t' | ' ' | '\\' | '\'' | '"' // c:228 whitespace/backslash/quotes
    )
}

/// Convert integer to string with specified base — re-export of
/// the canonical port at `convbase_param`.
///
/// The previous Rust port at this file was a SECOND, divergent
/// implementation of `convbase`:
///   - Used LOWERCASE digit chars (`"0123456789abc..."`) for
///     bases 11..36, while C uses UPPERCASE (`(dig - 10) + 'A'`
///     at Src/params.c:5621).
///   - Skipped CBASES + OCTALZEROES prefix handling at c:5598-5604
///     (`0x`/`0` prefixes when option-gated).
///   - Returned wrong format for bases not handled by the explicit
///     match arms.
///
/// The canonical port lives at `params.rs::convbase_ptr` (c:5588
/// faithful body) + `params.rs::convbase` (c:5634 wrapper). Route
/// through there so utils.rs / params.rs agree byte-for-byte and
/// no divergent duplicate stays alive.
pub fn convbase(val: i64, base: u32) -> String {
    // c:5634 (Src/params.c)
    convbase_param(val, base)
}

// `checkglobqual` DELETED — fake bracket-depth scanner cited
// `c:glob.c:1158` from utils.rs. Canonical port is
// `crate::ported::glob::checkglobqual(str, sl, nobareglob, sp)` at
// `glob.rs:813`, which matches the real C signature
// `(char *str, int sl, int nobareglob, char **sp)`. The utils.rs
// fake reduced the signature to a single bool and discarded the
// `sp` glob-qualifier-start out-pointer that callers need. Zero
// callers used the utils.rs version.

// `dupstrpfx` DELETED — fake duplicate cited `c:string.c:161` from
// utils.rs (wrong location). Canonical port is
// `dupstrpfx` at `string.rs:161`. Zero
// callers used the utils.rs version.

/// Get username from UID (from utils.c getpwuid handling)
pub fn statuidprint(uid: u32) -> Option<String> {
    #[cfg(unix)]
    {
        let pwd = unsafe { libc::getpwuid(uid) };
        if pwd.is_null() {
            return None;
        }
        let name = unsafe { std::ffi::CStr::from_ptr((*pwd).pw_name) };
        name.to_str().ok().map(|s| s.to_string())
    }
    #[cfg(not(unix))]
    {
        let _ = uid;
        None
    }
}

// `ztrdup` / `dyncat` / `tricat` / `bicat` DELETED — these were
// fake duplicates of the canonical ports in `src/ported/string.rs`
// (`Src/string.c:62`, `:131`, `:98`, `:145`). The utils.rs copies
// admitted they were not in utils.c via `c:string.c:NNN`
// annotations; PORT.md Rule 1 disallows the duplicate location.
// Callers now route through `crate::ported::string::{ztrdup,
// dyncat, tricat, bicat}` directly.

// `iwsep` / `iwsep_byte` DELETED — these were fake hardcoded
// `c==' '||c=='\t'||c=='\n'` checks claiming to port `iwsep` from
// "Src/zsh.h", but the real macro lives at `Src/ztype.h:61`
// (`#define iwsep(X) zistype(X, IWSEP)`) and consults the `typtab[]`
// lookup — which mutates when IFS is reassigned. The canonical port
// is `iwsep(u8) -> bool` at `ztype_h.rs:133`.
// Internal utils.rs callers (skipwsep / spacesplit / findword) now
// route through it directly.

// `imeta(c: char)` DELETED — fake `char`-arg wrapper cited
// `c:ztype.h:60` from utils.rs (wrong location, wrong signature).
// Canonical port is `crate::ported::ztype_h::imeta(u8) -> bool` at
// `ztype_h.rs:130`. C `imeta(X)` takes a byte; wrapping with `char`
// invented an `> 0xff` early-out that C doesn't have. Migrated 1
// caller (`zle/computil.rs:6194`).

/// Get hostname
pub fn gethostname() -> String {
    #[cfg(unix)]
    {
        let mut buf = vec![0u8; 256];
        unsafe {
            if libc::gethostname(buf.as_mut_ptr() as *mut libc::c_char, buf.len()) == 0 {
                let len = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
                return String::from_utf8_lossy(&buf[..len]).to_string();
            }
        }
    }
    // gethostname(2) failure fallback. C consults `$HOST` /
    // `cached_hostname`. Read paramtab; fall back to "localhost".
    getsparam("HOST")
        .or_else(|| getsparam("HOSTNAME"))
        .unwrap_or_else(|| "localhost".to_string())
}

/// Get current working directory — re-export of the canonical
/// port at `compat::zgetcwd` (Src/compat.c:559).
// `zgetcwd` DELETED — fake `Option<String>` wrapper around the
// canonical `compat::zgetcwd() -> String` port (`Src/compat.c:559`).
// C `zgetcwd` never returns NULL (falls back to `"."` per the
// `dupstring(".")` arm), so the `Option` was caller-API churn, not
// a port. Callers route through `crate::ported::compat::zgetcwd()`
// directly.

// `zchdir` duplicate deleted — canonical port at compat.rs:253
// matches C's `int zchdir(char *dir)` signature (returns i32, not
// bool). Zero callers used the utils.rs bool variant.

// `realpath` / `mkdir` / `symlink` / `readlink` / `getenv` DELETED —
// five Rust-only convenience wrappers around std::fs / std::env /
// std::os::unix::fs with ZERO callers across the tree. Each was a
// thin std-lib re-export with no zsh C counterpart:
//   - realpath  → std::fs::canonicalize (libc realpath(3) — not in
//                 utils.c; the zsh source uses chrealpath at hist.c:1971)
//   - mkdir     → std::fs::create_dir (libc mkdir(2) — zsh's analogue
//                 is Modules/files.c::bin_mkdir, not a utils.c helper)
//   - symlink   → std::os::unix::fs::symlink (libc symlink(2))
//   - readlink  → std::fs::read_link (libc readlink(2))
//   - getenv    → std::env::var (libc getenv(3) — zsh's analogue is
//                 zgetenv in compat.c)
// PORT.md Rule 0/A: names must exist in upstream zsh C source as a
// `<name>(` function. None of these qualify; with zero callers they
// were dead code by definition. Callers needing the same semantics
// route through the canonical port (chrealpath / bin_mkdir / etc.)
// or std::fs directly.

/// Per-prompt callback registry.
/// Port of the static `prepromptfns` LinkList in Src/utils.c:1319.
/// Holds the bare-fn pointers `addprepromptfn`/`delprepromptfn`
/// register and `preprompt()` walks.
static PREPROMPT_FNS: Mutex<Vec<fn()>> = Mutex::new(Vec::new());

/// Set environment variable
pub fn setenv(name: &str, value: &str) {
    std::env::set_var(name, value);
}

/// Unset environment variable
pub fn unsetenv(name: &str) {
    std::env::remove_var(name);
}

/// Time-ordered timed-function registry.
/// Port of the `timedfns` LinkList Src/utils.c:1371 keeps for the
/// `sched` builtin. Sorted ascending by `when` (epoch seconds);
/// `addtimedfn` does an insertion sort (matches the C source's
/// `for (;;)` walk at lines 1394-1411).
pub static TIMED_FNS: Mutex<Vec<(i64, fn())>> = Mutex::new(Vec::new()); // c:1371 timedfns (mod_export in C)

/// Get current user ID
pub fn getuid() -> u32 {
    #[cfg(unix)]
    unsafe {
        libc::getuid()
    }
    #[cfg(not(unix))]
    0
}

/// Get effective user ID
pub fn geteuid() -> u32 {
    #[cfg(unix)]
    unsafe {
        libc::geteuid()
    }
    #[cfg(not(unix))]
    0
}

/// Get current group ID
pub fn getgid() -> u32 {
    #[cfg(unix)]
    unsafe {
        libc::getgid()
    }
    #[cfg(not(unix))]
    0
}

/// Get effective group ID
pub fn getegid() -> u32 {
    #[cfg(unix)]
    unsafe {
        libc::getegid()
    }
    #[cfg(not(unix))]
    0
}

/// Get process ID
pub fn getpid() -> i32 {
    std::process::id() as i32
}

/// Get parent process ID
pub fn getppid() -> i32 {
    #[cfg(unix)]
    unsafe {
        libc::getppid()
    }
    #[cfg(not(unix))]
    0
}

/// `getkeystring` with the C `how` parameter (Src/utils.c:6915).
/// The plain `getkeystring(s)` shim above defaults `how=0` for
/// non-EMACS callers (zbeep, dollar-quote — they keep unknown
/// `\<char>` as literal `\<char>`). Pass `GETKEYS_PRINT` to get
/// the print/echo behavior (drop backslash on unknown).
pub fn getkeystring_with(s: &str, how: u32) -> (String, usize) {
    // c:utils.c:6915
    let mut result = String::new();
    let mut chars = s.chars().peekable();
    let mut consumed = 0;
    while let Some(c) = chars.next() {
        consumed += c.len_utf8();
        if c != '\\' {
            result.push(c);
            continue;
        }
        match chars.next() {
            Some('n') => {
                result.push('\n');
                consumed += 1;
            }
            Some('t') => {
                result.push('\t');
                consumed += 1;
            }
            Some('r') => {
                result.push('\r');
                consumed += 1;
            }
            Some('e') | Some('E') => {
                result.push('\x1b');
                consumed += 1;
            }
            Some('a') => {
                result.push('\x07');
                consumed += 1;
            }
            Some('b') => {
                result.push('\x08');
                consumed += 1;
            }
            Some('f') => {
                result.push('\x0c');
                consumed += 1;
            }
            Some('v') => {
                result.push('\x0b');
                consumed += 1;
            }
            // c:utils.c:7140-7152 — `\\` (and `\'` under
            // GETKEY_DOLLAR_QUOTE) explicitly emit the trailing byte
            // bare. Outside DOLLAR_QUOTE, `\'` FALLTHROUGHs to default.
            // `\\` stays special because the default arm (c:7180-7184)
            // skips the leading-backslash emission iff `*s == '\\'`,
            // so `\\` reduces to `\` regardless of GETKEY_EMACS.
            //
            // Previous Rust port also had `Some('\'')` and `Some('"')`
            // arms that unconditionally dropped the backslash. That
            // contradicted C: under GETKEYS_ECHO (no GETKEY_EMACS),
            // `\'` must remain `\'` because echo doesn't strip
            // unknown escapes. Removed those arms — `\'` and `\"`
            // now flow through the default arm and honour GETKEY_EMACS.
            //
            // Root cause of `echo "${(qq)s}"` for `s="a'b"` emitting
            // `'a'''b'` instead of zsh's `'a'\''b'`.
            Some('\\') => {
                result.push('\\');
                consumed += 1;
            }
            Some('x') => {
                consumed += 1;
                let mut hex = String::new();
                for _ in 0..2 {
                    if let Some(&c) = chars.peek() {
                        if c.is_ascii_hexdigit() {
                            hex.push(chars.next().unwrap());
                            consumed += 1;
                        } else {
                            break;
                        }
                    }
                }
                if let Ok(val) = u8::from_str_radix(&hex, 16) {
                    result.push(val as char);
                }
            }
            // Octal escape: \NNN (1-3 octal digits). Gated on
            // GETKEY_OCTAL_ESC per c:utils.c:7156-7178.
            Some(d) if d.is_digit(8) && (how & GETKEY_OCTAL_ESC) != 0 => {
                consumed += 1;
                let mut oct = String::from(d);
                for _ in 0..2 {
                    if let Some(&c) = chars.peek() {
                        if c.is_digit(8) {
                            oct.push(chars.next().unwrap());
                            consumed += 1;
                        } else {
                            break;
                        }
                    }
                }
                if let Ok(val) = u8::from_str_radix(&oct, 8) {
                    result.push(val as char);
                }
            }
            // c:utils.c:7180-7184 — default arm. With GETKEY_EMACS
            // set, drop the backslash; otherwise keep `\<char>`.
            Some(c) => {
                consumed += 1;
                // c:utils.c:7045 — `\c` under GETKEY_BACKSLASH_C
                // means TRUNCATE: drop the rest of the input,
                // suppress the trailing newline. Used by `echo`
                // (and `print` without -r). Set TLS flag so the
                // caller can detect + suppress the newline.
                if c == 'c' && (how & GETKEY_BACKSLASH_C) != 0 {
                    GETKEY_TRUNCATED.with(|cell| cell.set(true));
                    break;
                }
                if (how & GETKEY_EMACS) == 0 {
                    result.push('\\');
                }
                result.push(c);
            }
            None => {
                result.push('\\');
            }
        }
    }
    (result, consumed)
}

thread_local! {
    /// Sticky flag set by `getkeystring_with` when a `\c` escape
    /// truncates the input under `GETKEY_BACKSLASH_C`. The caller
    /// (bin_print / bin_echo) reads + clears via `getkey_truncated_take()`
    /// to suppress the trailing newline and skip any subsequent args
    /// in the print pass.
    pub static GETKEY_TRUNCATED: std::cell::Cell<bool> =
        const { std::cell::Cell::new(false) };
}

/// Read + clear the `\c`-truncated flag set by the previous
/// `getkeystring_with` call. Returns true once after a truncation
/// fires, false otherwise.
pub fn getkey_truncated_take() -> bool {
    GETKEY_TRUNCATED.with(|c| {
        let v = c.get();
        c.set(false);
        v
    })
}

/// !!! RUST-ONLY HELPER — see WARNING block above. Equivalent to
/// the C expression `fdtable[fd]` (read). Returns `FDT_UNUSED` for
/// any fd that has not been explicitly set, matching the C source's
/// post-`growfdtable` zero-fill behaviour at Src/utils.c:1979.
pub fn fdtable_get(fd: i32) -> i32 {
    // c:utils.c:fdtable[fd]
    if fd < 0 {
        return FDT_UNUSED;
    }
    let g = fdtable_lock().lock().unwrap();
    g.get(fd as usize).copied().unwrap_or(FDT_UNUSED)
}

/// !!! RUST-ONLY HELPER — see WARNING block above. Equivalent to
/// the C statement `fdtable[fd] = kind;`. Inlines the `growfdtable`
/// call from Src/utils.c:1965 since C always invokes it immediately
/// before the assignment anyway. Also updates `MAX_ZSH_FD` to track
/// the highest assigned slot — C does this inside `check_fd_table`
/// at `Src/utils.c:1982` (`max_zsh_fd = fd;`). Previously the Rust
/// `fdtable_set` skipped this, leaving `max_zsh_fd = 0` after any
/// `addlockfd` / `addmodulefd` call, which broke `zcloselockfd`'s
/// `if (fd > max_zsh_fd)` guard.
pub fn fdtable_set(fd: i32, kind: i32) {
    // c:utils.c:fdtable[fd]
    if fd < 0 {
        return;
    }
    let mut g = fdtable_lock().lock().unwrap();
    if (fd as usize) >= g.len() {
        g.resize((fd as usize) + 1, FDT_UNUSED);
    }
    g[fd as usize] = kind;
    // c:1982 — `max_zsh_fd = fd;` (with `if (fd <= max_zsh_fd) return;`
    // guard at c:1971). Inline equivalent: bump only when this fd
    // exceeds the current max.
    let cur = MAX_ZSH_FD.load(Ordering::Relaxed);
    if fd > cur {
        MAX_ZSH_FD.store(fd, Ordering::Relaxed);
    }
}

/// Port of `imeta()` macro from `Src/ztype.h:60` — `zistype(X, IMETA)`.
///
/// IMETA is set in the typtab at `Src/utils.c:4195-4201` for:
///   - `'\0'` (c:4195)
///   - `Meta` (0x83) (c:4196)
///   - `Marker` (0xa2) (c:4197)
///   - `Pound..=LAST_NORMAL_TOK` = `0x84..=0x9c` (c:4198)
///   - `Snull..=Nularg` = `0x9d..=0xa1` (c:4200)
///
/// The CANONICAL set is `{0x00, 0x83..=0xa2}`. The previous Rust
/// port used `b >= 0x83` (every byte 0x83..=0xff) which was WRONG
/// for bytes `0xa3..=0xff`: C `imeta()` returns false for those,
/// so metafy passes them through unchanged. Rust's broader range
/// caused `metafy` to corrupt UTF-8 multibyte content (e.g.,
/// `é` = `0xc3 0xa9`: both bytes are NOT IMETA in C, but Rust
/// escaped both as `Meta + (byte ^ 32)`, mangling the encoding
/// and breaking every downstream consumer that expects the raw
/// UTF-8 bytes to round-trip).
///
/// Use the closed-range form rather than the typtab lookup so
/// `metafy` / `imeta_byte` work in test contexts where the typtab
/// isn't initialised, AND so the fast-path doesn't go through a
/// Mutex lock per byte (`metafy` is called per-character on every
/// shell input line).
#[inline]
pub fn imeta_byte(b: u8) -> bool {
    // c:4195-4201 — canonical IMETA range.
    b == 0 || (0x83..=0xa2).contains(&b)
}

// `pub fn convfloat` and `pub fn convfloat_underscore` re-export
// wrappers DELETED per PORT.md Rule C. C defines both in
// `Src/params.c:5690` and `:5765` — the canonical Rust home is
// `src/ported/params.rs` (`convfloat` at line 7356,
// `convfloat_underscore` matching). Caller convenience cost is
// `use crate::ported::params::convfloat;` instead of `utils::`;
// no Rust-side re-export needed.

// ===========================================================
// Methods moved verbatim from src/ported/vm_helper because their
// C counterpart's source file maps 1:1 to this Rust module.
// Phase: drift
// ===========================================================

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

// END moved-from-exec-rs

// ===========================================================
// Free ported moved verbatim from src/ported/vm_helper.
// ===========================================================
// BEGIN moved-from-exec-rs (free ported)
pub(crate) fn base64_decode(s: &str) -> Vec<u8> {
    let decode_char = |c: u8| -> Option<u8> {
        match c {
            b'A'..=b'Z' => Some(c - b'A'),
            b'a'..=b'z' => Some(c - b'a' + 26),
            b'0'..=b'9' => Some(c - b'0' + 52),
            b'+' => Some(62),
            b'/' => Some(63),
            _ => None,
        }
    };
    let bytes = s.as_bytes();
    let mut out = Vec::with_capacity(s.len() / 4 * 3);
    let mut i = 0;
    while i + 4 <= bytes.len() {
        let chunk = &bytes[i..i + 4];
        let pad = chunk.iter().filter(|&&c| c == b'=').count();
        let v0 = decode_char(chunk[0]).unwrap_or(0) as u32;
        let v1 = decode_char(chunk[1]).unwrap_or(0) as u32;
        let v2 = decode_char(chunk[2]).unwrap_or(0) as u32;
        let v3 = decode_char(chunk[3]).unwrap_or(0) as u32;
        let n = (v0 << 18) | (v1 << 12) | (v2 << 6) | v3;
        out.push(((n >> 16) & 0xff) as u8);
        if pad < 2 {
            out.push(((n >> 8) & 0xff) as u8);
        }
        if pad < 1 {
            out.push((n & 0xff) as u8);
        }
        i += 4;
    }
    out
}
// END moved-from-exec-rs (free ported)

// ===========================================================
// Utility helpers moved from src/ported/vm_helper.
// All correspond to Src/utils.c logic (path/string/bslashquote helpers).
// ===========================================================

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ─── RUST-ONLY ACCESSORS ───
//
// Singleton accessor ported for `OnceLock<Mutex<T>>` / `OnceLock<
// RwLock<T>>` globals declared above. C zsh uses direct global
// access; Rust needs these wrappers because `OnceLock::get_or_init`
// is the only way to lazily construct shared state. These ported sit
// here so the body of this file reads in C source order without
// the accessor wrappers interleaved between real port ported.
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ─── RUST-ONLY ACCESSORS ───
//
// Singleton accessor ported for `OnceLock<Mutex<T>>` / `OnceLock<
// RwLock<T>>` globals declared above. C zsh uses direct global
// access; Rust needs these wrappers because `OnceLock::get_or_init`
// is the only way to lazily construct shared state. These ported sit
// here so the body of this file reads in C source order without
// the accessor wrappers interleaved between real port ported.
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ─── RUST-ONLY ACCESSORS ───
//
// Singleton accessor ported for `OnceLock<Mutex<T>>` / `OnceLock<
// RwLock<T>>` globals declared above. C zsh uses direct global
// access; Rust needs these wrappers because `OnceLock::get_or_init`
// is the only way to lazily construct shared state. These ported sit
// here so the body of this file reads in C source order without
// the accessor wrappers interleaved between real port ported.
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ─── RUST-ONLY ACCESSORS ───
//
// Singleton accessor ported for `OnceLock<Mutex<T>>` / `OnceLock<
// RwLock<T>>` globals declared above. C zsh uses direct global
// access; Rust needs these wrappers because `OnceLock::get_or_init`
// is the only way to lazily construct shared state. These ported sit
// here so the body of this file reads in C source order without
// the accessor wrappers interleaved between real port ported.
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// WARNING: NOT IN UTILS.C — Rust-only OnceLock get-or-init
// helpers. C dereferences each global directly.
fn scriptname_lock() -> &'static Mutex<Option<String>> {
    SCRIPTNAME.get_or_init(|| Mutex::new(None))
}

// WARNING: NOT IN UTILS.C — see scriptname_lock above.
fn argzero_lock() -> &'static Mutex<Option<String>> {
    ARGZERO.get_or_init(|| Mutex::new(None))
}

// WARNING: NOT IN UTILS.C — see scriptname_lock above.
fn scriptfilename_lock() -> &'static Mutex<Option<String>> {
    SCRIPTFILENAME.get_or_init(|| Mutex::new(None))
}

// WARNING: NOT IN UTILS.C — Rust-only OnceLock accessor for `posixzero`.
// C's `posixzero` lives in params.c:76; the canonical storage lives
// here for OnceLock initialisation parity with `argzero`.
fn posixzero_lock() -> &'static Mutex<Option<String>> {
    POSIXZERO.get_or_init(|| Mutex::new(None))
}

// WARNING: NOT IN UTILS.C — see scriptname_lock above.
fn noerrs_lock() -> &'static Mutex<i32> {
    NOERRS.get_or_init(|| Mutex::new(0))
}

// `locallevel_lock` removed — duplicate of canonical
// `LOCALLEVEL` (port of params.c:54). All
// accessors here now route through the canonical AtomicI32.
// WARNING: NOT IN UTILS.C — see scriptname_lock above.
fn lineno_lock() -> &'static Mutex<i32> {
    LINENO.get_or_init(|| Mutex::new(0))
}

// WARNING: NOT IN UTILS.C — Rust-only cache for the SHINSTDIN
// option flag so error-emission doesn't pull in the option-table.
fn shinstdin_lock() -> &'static Mutex<bool> {
    SHINSTDIN_OPT.get_or_init(|| Mutex::new(false))
}

/// !!! RUST-ONLY HELPER — see WARNING block above. C source uses bare
/// `unsigned char *fdtable` global from Src/utils.c:~63.
fn fdtable_lock() -> &'static Mutex<Vec<i32>> {
    FDTABLE.get_or_init(|| Mutex::new(Vec::new()))
}

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

    /// c:4033 — `subst_string_by_func` returns `getaparam("reply")`
    /// after the hook function finishes. The previous Rust port read
    /// `env::var("reply")` and split on NUL — wrong because `reply`
    /// is a shell-local PM_ARRAY in paramtab, never exported. This
    /// pin sets `reply` via `setaparam` (the paramtab write path)
    /// and exercises `subst_string_by_hook` end-to-end isn't viable
    /// in unit tests without a function-name hook, so we exercise
    /// just the getaparam plumbing.
    #[test]
    fn getaparam_reads_reply_from_paramtab_not_env() {
        let _g = crate::test_util::global_state_lock();
        // Stash any prior `reply` value.
        let saved = getaparam("reply");

        // Write a known reply array via the canonical setaparam path.
        let payload = vec!["abbreviated".to_string(), "11".to_string()];
        let _ = setaparam("reply", payload.clone());

        // The reply array must be reachable via getaparam — not env.
        // (env::var would return Err because setaparam never exports
        //  an un-flagged array to env.)
        assert_eq!(
            getaparam("reply"),
            Some(payload),
            "getaparam(\"reply\") must return paramtab array"
        );

        // Restore.
        let _ = setaparam("reply", saved.unwrap_or_default());
    }

    /// c:1133-1134 — `finddir` reads the global `home` variable (the
    /// canonical `$HOME` storage, not `getenv("HOME")`). The global
    /// is updated by `homesetfn` (Src/params.c:5118) whenever the user
    /// assigns `HOME=...` inside the shell.
    /// Regression target: a previous Rust port read `env::var("HOME")`
    /// so an in-shell `HOME=...` assignment would not retarget
    /// `~`-abbreviation until the user re-exported HOME.
    #[test]
    fn finddir_uses_paramtab_home_not_env() {
        let _g = crate::test_util::global_state_lock();
        // C's `homesetfn` (params.c:5118) UNUSED(Param pm) — the
        // function ignores its param-pointer argument and writes the
        // canonical `char *home` global directly. zshrs's port mirrors
        // that: `params::homesetfn(_pm, x)` ignores _pm and updates
        // `home_lock()`. So we pass a stack-default param; the paramtab
        // wiring (PM_SPECIAL dispatch) is not on the test path.
        let mut pm = crate::ported::zsh_h::param::default();
        let saved = crate::ported::params::homegetfn(&pm);
        let sentinel = "/tmp/zshrs-finddir-pin".to_string();
        homesetfn(&mut pm, sentinel.clone());

        // `/tmp/zshrs-finddir-pin/x` must abbreviate to `~/x`.
        let abbrev = finddir(&format!("{}/x", sentinel));
        assert_eq!(
            abbrev.as_deref(),
            Some("~/x"),
            "finddir must consult canonical HOME (got {:?})",
            abbrev
        );

        // Restore.
        homesetfn(&mut pm, saved);
    }

    #[test]
    fn test_sepsplit() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(sepsplit("a:b:c", Some(":"), false), vec!["a", "b", "c"]);
        assert_eq!(sepsplit("a::b", Some(":"), false), vec!["a", "b"]);
        assert_eq!(sepsplit("a::b", Some(":"), true), vec!["a", "", "b"]);
    }

    #[test]
    fn test_unmetafy_no_meta_byte_passes_through() {
        let _g = crate::test_util::global_state_lock();
        // No Meta byte → buffer unchanged, length unchanged.
        let mut buf = b"hello".to_vec();
        let n = unmetafy(&mut buf);
        assert_eq!(n, 5);
        assert_eq!(&buf, b"hello");
    }

    #[test]
    fn test_unmetafy_collapses_meta_escapes() {
        let _g = crate::test_util::global_state_lock();
        // C: Meta byte (0x83) followed by `'a' ^ 32` (0x41 = 'A')
        // unmetafies to a single byte 'a' (0x61).
        // i.e. {0x83, 'a' ^ 32} → {'a'}.
        let mut buf = vec![0x83, b'a' ^ 32];
        let n = unmetafy(&mut buf);
        assert_eq!(n, 1);
        assert_eq!(buf, vec![b'a']);
    }

    #[test]
    fn test_unmetafy_mixed_prefix_then_meta() {
        let _g = crate::test_util::global_state_lock();
        // Plain prefix, then Meta-escaped 0xFF (0x83, 0xFF ^ 32 = 0xDF).
        let mut buf = vec![b'X', b'Y', 0x83, 0xFF ^ 32, b'Z'];
        let n = unmetafy(&mut buf);
        assert_eq!(n, 4);
        assert_eq!(buf, vec![b'X', b'Y', 0xFF, b'Z']);
    }

    #[test]
    fn test_unmetafy_returns_self_value() {
        let _g = crate::test_util::global_state_lock();
        // C returns `s` (the buffer) for chaining; Rust returns
        // the new length. Verify length matches a call that
        // collapses two Meta-escapes.
        let mut buf = vec![
            b'A',
            0x83,
            b'B' ^ 32, // → 'B'
            0x83,
            b'C' ^ 32, // → 'C'
            b'D',
        ];
        let n = unmetafy(&mut buf);
        assert_eq!(n, 4);
        assert_eq!(buf, b"ABCD".to_vec());
    }

    #[test]
    fn test_imeta_byte_threshold() {
        let _g = crate::test_util::global_state_lock();
        // Canonical IMETA per Src/utils.c:4195-4201:
        //   - 0x00 (c:4195)
        //   - 0x83..=0xa2 (Meta through Marker — c:4196-4200)
        //
        // The previous version of this test asserted `imeta_byte(0xFF) == true`
        // based on the WRONG `b >= Meta` predicate the Rust port
        // originally used. C `imeta()` reads the typtab; 0xa3..=0xff
        // have NO typtab assignment so they're NOT IMETA, and
        // `metafy` must NOT escape them.
        assert!(imeta_byte(0x00), "c:4195 — NUL is IMETA");
        assert!(!imeta_byte(0x82), "0x82 is NOT IMETA (below Meta)");
        assert!(imeta_byte(Meta), "c:4196 — Meta (0x83) is IMETA");
        assert!(imeta_byte(0xa2), "c:4197 — Marker (0xa2) is IMETA");
        // c:4198-4200 upper bound — Nularg is the last IMETA byte.
        assert!(imeta_byte(0xa1), "c:4200 — Nularg (0xa1) is IMETA");
        // 0xa3..=0xff are NOT IMETA — UTF-8 multi-byte content
        // must pass through `metafy` unchanged.
        assert!(!imeta_byte(0xa3), "0xa3 NOT IMETA (above Marker)");
        assert!(!imeta_byte(0xc3), "0xc3 NOT IMETA (UTF-8 'é' lead)");
        assert!(!imeta_byte(0xFF), "0xFF NOT IMETA");
    }

    #[test]
    fn test_meta_constant_value() {
        let _g = crate::test_util::global_state_lock();
        // Locked at 0x83 by Src/zsh.h. If this test fails, zsh
        // bumped the Meta sentinel and the encoding mapping needs
        // a full audit.
        assert_eq!(Meta, 0x83);
    }

    #[test]
    #[cfg(unix)]
    fn test_mode_to_octal_canonical_bits() {
        let _g = crate::test_util::global_state_lock();
        // rwx for owner = 0o700.
        let mode = (S_IRUSR | S_IWUSR | S_IXUSR) as u32;
        assert_eq!(mode_to_octal(mode), 0o700);
        // rwx all = 0o777.
        let all = (S_IRUSR | S_IWUSR | S_IXUSR) as u32 * (1 + 8 + 64);
        // Use libc constants individually for portability.
        let m = (S_IRUSR
            | S_IWUSR
            | S_IXUSR
            | S_IRGRP
            | S_IWGRP
            | S_IXGRP
            | S_IROTH
            | S_IWOTH
            | S_IXOTH) as u32;
        assert_eq!(mode_to_octal(m), 0o777);
        let _ = all;
    }

    #[test]
    #[cfg(unix)]
    fn test_mode_to_octal_setuid_setgid_sticky() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(mode_to_octal(S_ISUID as u32), 0o4000);
        assert_eq!(mode_to_octal(S_ISGID as u32), 0o2000);
        assert_eq!(mode_to_octal(S_ISVTX as u32), 0o1000);
        // All three: 0o7000.
        let all = (S_ISUID | S_ISGID | S_ISVTX) as u32;
        assert_eq!(mode_to_octal(all), 0o7000);
    }

    #[test]
    #[cfg(unix)]
    fn test_mailstat_plain_file_returns_native_stat() {
        let _g = crate::test_util::global_state_lock();
        // Plain file path → *st fields mirror native stat,
        // not the maildir aggregation.
        let mut st: libc::stat = unsafe { std::mem::zeroed() };
        let rc = mailstat("/etc/hosts", &mut st);
        if rc == 0 {
            assert_eq!(
                st.st_nlink as u64,
                fs::metadata("/etc/hosts").unwrap().nlink()
            );
        }
    }

    #[test]
    fn test_mailstat_nonexistent_returns_neg1() {
        let _g = crate::test_util::global_state_lock();
        let mut st: libc::stat = unsafe { std::mem::zeroed() };
        assert_eq!(mailstat("/nonexistent/path/does/not/exist", &mut st), -1);
    }

    #[test]
    fn test_mailstat_directory_without_maildir_subdirs() {
        let _g = crate::test_util::global_state_lock();
        // /tmp is a directory but not a maildir (no cur/tmp/new) —
        // returns the partial aggregate (top dir's atime/mtime,
        // size=0 since cur/ wasn't found before we'd start summing).
        let mut st: libc::stat = unsafe { std::mem::zeroed() };
        let rc = mailstat("/tmp", &mut st);
        assert_eq!(rc, 0);
        assert_eq!(st.st_nlink, 1);
        assert_eq!(st.st_size, 0);
        assert_eq!(st.st_blocks, 0);
        // S_IFDIR bit should be cleared, S_IFREG set.
        #[cfg(unix)]
        {
            assert_eq!(st.st_mode & libc::S_IFDIR, 0);
            assert_ne!(st.st_mode & libc::S_IFREG, 0);
        }
    }

    #[test]
    fn test_dupstrpfx_byte_counted() {
        let _g = crate::test_util::global_state_lock(); // c:161
                                                        // 5 bytes of ASCII = 5 chars, identical. Canonical
                                                        // port lives at `string.rs:161`; pin from utils.rs's test
                                                        // module via the qualified path.
        assert_eq!(dupstrpfx("hello", 3), "hel"); // c:161
        assert_eq!(dupstrpfx("hi", 10), "hi"); // c:161
        assert_eq!(dupstrpfx("anything", 0), ""); // c:161
    }

    #[test]
    fn test_metafy_passes_through_ascii() {
        let _g = crate::test_util::global_state_lock();
        // ASCII bytes (< 0x83) stay untouched.
        assert_eq!(metafy("hello"), "hello");
        assert_eq!(metafy(""), "");
    }

    #[test]
    fn test_metafy_imeta_predicate_matches_c_macro() {
        let _g = crate::test_util::global_state_lock();
        // Canonical C IMETA per Src/utils.c:4195-4201:
        //   - 0x00 (c:4195)
        //   - 0x83 (Meta, c:4196)
        //   - 0x84..=0x9c (Pound..LAST_NORMAL_TOK=Bang, c:4198)
        //   - 0x9d..=0xa1 (Snull..Nularg, c:4200)
        //   - 0xa2 (Marker, c:4197)
        //
        // The closed set is {0x00, 0x83..=0xa2}. Every other byte
        // (0x01..=0x82, 0xa3..=0xff) is NOT IMETA in C and so must
        // NOT be Meta-encoded by `metafy`. The previous Rust
        // hardcoded predicate `b >= 0x83` falsely marked
        // 0xa3..=0xff as IMETA, corrupting UTF-8 multibyte
        // content.

        // NUL is IMETA (c:4195).
        assert!(imeta_byte(0x00), "c:4195 — '\\0' IS imeta");

        // 0x01..=0x82 are NOT IMETA (no typtab assignment for them).
        for b in 0x01u8..=0x82 {
            assert!(!imeta_byte(b), "byte {:#x} should NOT be imeta", b);
        }

        // 0x83..=0xa2 ARE IMETA (the canonical full range).
        for b in 0x83u8..=0xa2 {
            assert!(imeta_byte(b), "c:4196-4200 — byte {:#x} IS imeta", b);
        }

        // 0xa3..=0xff are NOT IMETA. C `imeta()` returns false
        // for these so `metafy` must pass them through unchanged.
        // UTF-8 continuation bytes (0x80..=0xbf) and multi-byte
        // leads (0xc0..=0xff) live here and must round-trip.
        for b in 0xa3u8..=0xff {
            assert!(
                !imeta_byte(b),
                "byte {:#x} should NOT be imeta (no c:4195-4201 assignment)",
                b
            );
        }
    }

    /// Pin: `metafy` passes UTF-8 multibyte bytes through unchanged
    /// when they fall outside the canonical IMETA range. Previously
    /// the broader `b >= 0x83` predicate corrupted every UTF-8
    /// continuation byte and lead byte that wasn't a token marker.
    #[test]
    fn metafy_preserves_utf8_high_bytes_outside_imeta_range() {
        let _g = crate::test_util::global_state_lock();
        // 'é' = U+00E9 = UTF-8 0xc3 0xa9. Both bytes are >= 0x83 BUT
        // both are also > 0xa2, so they're NOT IMETA per the typtab.
        // C `metafy` passes them through unchanged.
        let input = std::str::from_utf8(&[0xC3, 0xA9]).unwrap();
        let out = metafy(input);
        // Should round-trip the two bytes exactly (no Meta escape).
        let out_bytes = out.as_bytes();
        assert_eq!(
            out_bytes,
            &[0xC3, 0xA9],
            "c:4196-4200 — UTF-8 bytes 0xc3/0xa9 outside IMETA range must pass through"
        );
    }

    #[test]
    fn test_ztrcmp_meta_aware() {
        let _g = crate::test_util::global_state_lock();
        // Two identical metafied strings → Equal.
        assert_eq!(ztrcmp("foo", "foo"), std::cmp::Ordering::Equal);
        // "foo" < "foz".
        assert_eq!(ztrcmp("foo", "foz"), std::cmp::Ordering::Less);
        // Prefix comparison: shorter < longer.
        assert_eq!(ztrcmp("foo", "foobar"), std::cmp::Ordering::Less);
        // Meta-encoded comparison: {0x83, 'a'^32} should compare as 'a'.
        let s_meta = unsafe { std::str::from_utf8_unchecked(&[0x83, b'a' ^ 32]) };
        let s_plain = "a";
        // Meta-encoded "a" should compare equal to plain "a".
        assert_eq!(ztrcmp(s_meta, s_plain), std::cmp::Ordering::Equal);
    }

    #[test]
    fn test_skipwsep_skips_runs() {
        let _g = crate::test_util::global_state_lock();
        // 3 spaces + 'x' → returns ("x", 3).
        let (rest, n) = skipwsep("   x");
        assert_eq!(rest, "x");
        assert_eq!(n, 3);
        // No leading whitespace → 0 skipped.
        let (rest, n) = skipwsep("foo");
        assert_eq!(rest, "foo");
        assert_eq!(n, 0);
        // Mix of space/tab/newline.
        let (rest, n) = skipwsep(" \t\nbar");
        assert_eq!(rest, "bar");
        assert_eq!(n, 3);
    }

    #[test]
    fn test_imeta_macro_threshold() {
        let _g = crate::test_util::global_state_lock(); // c:60
                                                        // `Src/ztype.h:60` `imeta(X) zistype(X, IMETA)` — typtab-driven
                                                        // predicate. Per `Src/utils.c:4195-4201`, IMETA is set on:
                                                        // NUL, Meta=0x83, Marker=0xa2, and the Pound..Nularg ITOK range
                                                        // (0x84..=0xa2). Routes through canonical
                                                        // `ztype_h::imeta(u8)`; init the typtab first since the
                                                        // canonical port reads through it.
        inittyptab(); // c:4148
        assert!(imeta(0x00), "c:4195 — NUL is IMETA"); // c:4195
        assert!(imeta(Meta), "c:4196 — Meta (0x83) is IMETA"); // c:4196
        assert!(imeta(0xa2), "c:4197 — Marker (0xa2) is IMETA"); // c:4197
        assert!(
            imeta(0x84),
            "c:4199-4201 — Pound (0x84) is IMETA via ITOK range"
        );
        assert!(
            imeta(0x9b),
            "c:4199-4201 — Dash sentinel within ITOK range is IMETA"
        );
        assert!(!imeta(0x82), "c:4170 — 0x82 is ICNTRL, not IMETA"); // c:4170
        assert!(!imeta(0xa3), "byte 0xa3 is past Marker — NOT IMETA");
        assert!(
            !imeta(0xff),
            "byte 0xff is NOT IMETA (the prior `>= Meta` over-report)"
        );
        assert!(!imeta(b' '), "space is not IMETA");
        assert!(!imeta(b'A'), "'A' is not IMETA");
    }

    #[test]
    fn test_unmeta_routes_through_unmetafy() {
        let _g = crate::test_util::global_state_lock();
        // unmeta wraps the in-place unmetafy via a byte-vector
        // copy; the no-Meta fast path returns the source as-is.
        assert_eq!(unmeta("plain"), "plain");
    }

    #[test]
    fn test_iwsep_includes_newline() {
        let _g = crate::test_util::global_state_lock(); // c:61
                                                        // The previous port omitted '\n' which broke wordcount on
                                                        // multi-line input. Routes through canonical
                                                        // `ztype_h::iwsep` (`Src/ztype.h:61`).
        assert!(iwsep(b'\n')); // c:61
        assert!(iwsep(b'\t')); // c:61
        assert!(iwsep(b' ')); // c:61
        assert!(!iwsep(b'a')); // c:61
    }

    #[test]
    fn test_mailstat_aggregates_maildir() {
        let _g = crate::test_util::global_state_lock();
        // Create a temp maildir layout with 2 messages in new/ and 1
        // in cur/, verify the aggregate.
        let tmp = std::env::temp_dir().join(format!("zshrs_mailstat_test_{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        fs::create_dir_all(tmp.join("cur")).unwrap();
        fs::create_dir_all(tmp.join("new")).unwrap();
        fs::create_dir_all(tmp.join("tmp")).unwrap();
        let mut f = fs::File::create(tmp.join("new").join("msg1")).unwrap();
        f.write_all(b"hello").unwrap();
        let mut f = fs::File::create(tmp.join("new").join("msg2")).unwrap();
        f.write_all(b"world!").unwrap();
        let mut f = fs::File::create(tmp.join("cur").join("msg3")).unwrap();
        f.write_all(b"third").unwrap();
        let mut st: libc::stat = unsafe { std::mem::zeroed() };
        let rc = mailstat(tmp.to_str().unwrap(), &mut st);
        assert_eq!(rc, 0, "maildir should stat");
        assert_eq!(st.st_blocks, 3, "3 messages total across new/ + cur/");
        assert_eq!(st.st_size, 5 + 6 + 5, "5+6+5 bytes total");
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_spacesplit() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(spacesplit("a b c", false), vec!["a", "b", "c"]);
        assert_eq!(spacesplit("a  b", false), vec!["a", "b"]);
    }

    #[test]
    fn test_sepjoin() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            sepjoin(&["a".into(), "b".into(), "c".into()], Some(":")),
            "a:b:c"
        );
        assert_eq!(sepjoin(&["a".into(), "b".into()], None), "a b");
    }

    #[test]
    fn test_isident() {
        let _g = crate::test_util::global_state_lock(); // c:1288
                                                        // Canonical port lives at `params.rs:2056` (`Src/params.c:1288`).
        assert!(isident("foo")); // c:1288
        assert!(isident("_bar")); // c:1288
        assert!(isident("baz123")); // c:1288
        assert!(!isident("123abc")); // c:1288
        assert!(!isident("foo-bar")); // c:1288
    }

    #[test]
    fn test_nicechar() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(nicechar('\n'), "\\n");
        assert_eq!(nicechar('\t'), "\\t");
        assert_eq!(nicechar('a'), "a");
    }

    #[test]
    fn test_quotedzputs_single_quote_wrap() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotedzputs("simple"), "simple");
        assert_eq!(quotedzputs("has space"), "'has space'");
        assert_eq!(quotedzputs("it's"), "'it'\\''s'");
    }

    #[test]
    fn test_quotestring_backslash() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            quotestring("hello", QT_BACKSLASH),
            "hello"
        );
        assert_eq!(
            quotestring("has space", QT_BACKSLASH),
            "has\\ space"
        );
        assert_eq!(
            quotestring("$var", QT_BACKSLASH),
            "\\$var"
        );
    }

    /// Pin: `ispecial(c)` matches the canonical SPECCHARS set at
    /// `Src/zsh.h:228` exactly: `"#$^*()=|{}[]\`<>?~;&\n\t \\'\""`.
    /// Previously the Rust local `ispecial` included `!` unconditionally
    /// which diverged from C — C only ISPECIAL-tags bangchar (default
    /// `!`) under BANGHIST + interactive (per `Src/utils.c:4257-4261`).
    ///
    /// This test exercises the path indirectly via `quotestring` with
    /// `QT_BACKSLASH` (which prepends `\` before every ispecial char).
    #[test]
    fn quotestring_backslash_only_specchars_no_bang_in_default() {
        let _g = crate::test_util::global_state_lock();
        // `!` is NOT in canonical SPECCHARS — should NOT be backslashed
        // in default non-interactive mode (matches C).
        assert_eq!(
            quotestring("a!b", QT_BACKSLASH),
            "a!b",
            "c:228 — `!` is not in SPECCHARS; only bangchar+BANGHIST adds it"
        );
        // `,` is NOT in canonical SPECCHARS until makecommaspecial(1).
        assert_eq!(
            quotestring("a,b", QT_BACKSLASH),
            "a,b",
            "c:228 — `,` not in SPECCHARS until makecommaspecial(1)"
        );
        // `^` IS in canonical SPECCHARS (per c:228 `"#$^..."`).
        assert_eq!(
            quotestring("a^b", QT_BACKSLASH),
            "a\\^b",
            "c:228 — `^` is in SPECCHARS"
        );
        // Open and close braces are in canonical SPECCHARS.
        assert_eq!(
            quotestring("a{b", QT_BACKSLASH),
            "a\\{b",
            "c:228 — open-brace is in SPECCHARS"
        );
        assert_eq!(
            quotestring("a}b", QT_BACKSLASH),
            "a\\}b",
            "c:228 — close-brace is in SPECCHARS"
        );
        // `#` is in canonical SPECCHARS (first char of c:228).
        assert_eq!(
            quotestring("a#b", QT_BACKSLASH),
            "a\\#b",
            "c:228 — `#` is the first char of SPECCHARS"
        );
        // `\\` (literal backslash) is in canonical SPECCHARS.
        assert_eq!(
            quotestring("a\\b", QT_BACKSLASH),
            "a\\\\b",
            "c:228 — `\\\\` in SPECCHARS"
        );
    }

    #[test]
    fn test_quotestring_single() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            quotestring("hello", QT_SINGLE),
            "'hello'"
        );
        assert_eq!(
            quotestring("it's", QT_SINGLE),
            "'it'\\''s'"
        );
    }

    #[test]
    fn test_quotestring_double() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            quotestring("hello", QT_DOUBLE),
            "\"hello\""
        );
        assert_eq!(
            quotestring("say \"hi\"", QT_DOUBLE),
            "\"say \\\"hi\\\"\""
        );
    }

    #[test]
    fn test_quotestring_dollars() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            quotestring("hello", QT_DOLLARS),
            "$'hello'"
        );
        assert_eq!(
            quotestring("line\nbreak", QT_DOLLARS),
            "$'line\\nbreak'"
        );
        assert_eq!(
            quotestring("tab\there", QT_DOLLARS),
            "$'tab\\there'"
        );
    }

    #[test]
    fn test_quotestring_pattern() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            quotestring("*.txt", QT_BACKSLASH_PATTERN),
            "\\*.txt"
        );
        assert_eq!(
            quotestring("file[1]", QT_BACKSLASH_PATTERN),
            "file\\[1\\]"
        );
    }

    #[test]
    fn test_quotetype_from_q_count() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(qflag_quotetype(1), QT_BACKSLASH);
        assert_eq!(qflag_quotetype(2), QT_SINGLE);
        assert_eq!(qflag_quotetype(3), QT_DOUBLE);
        assert_eq!(qflag_quotetype(4), QT_DOLLARS);
    }

    #[test]
    fn test_tulower_tuupper() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(tulower('A'), 'a');
        assert_eq!(tuupper('a'), 'A');
        assert_eq!(tulower('1'), '1');
    }

    #[test]
    fn test_wordcount_ifs_default() {
        let _g = crate::test_util::global_state_lock();
        // C: wordcount("a b c", NULL, 0) -> 3
        assert_eq!(wordcount("a b c", None, 0), 3);
        // Leading/trailing whitespace coalesced when mul <= 0.
        assert_eq!(wordcount("  a b  ", None, 0), 2);
        // Empty string with mul == 0 -> 0 words.
        assert_eq!(wordcount("", None, 0), 0);
        // Single word, no separators.
        assert_eq!(wordcount("foo", None, 0), 1);
    }

    #[test]
    fn test_wordcount_with_explicit_sep() {
        let _g = crate::test_util::global_state_lock();
        // C: wordcount("a:b:c", ":", 0) -> 3 (3 fields, 2 separators)
        assert_eq!(wordcount("a:b:c", Some(":"), 0), 3);
        // Empty fields counted when mul != 0.
        assert_eq!(wordcount("a::b", Some(":"), 1), 3);
        // Without mul, consecutive empties collapse: a, b => 2... but
        // C's "if ((c || mul) && (sl || *(s+sl)))" — second `:` has
        // c=0 and mul=0 so doesn't increment. Result: a, b => 2.
        assert_eq!(wordcount("a::b", Some(":"), 0), 2);
    }

    #[test]
    fn test_ucs4tomb_ascii() {
        let _g = crate::test_util::global_state_lock();
        let mut buf = [0u8; 8];
        // 'A' = 0x41, ASCII, single byte in any locale.
        let n = ucs4tomb('A' as u32, &mut buf);
        // wctomb may return 1 in C/POSIX locale; in UTF-8 locale also 1.
        assert_eq!(n, 1);
        assert_eq!(buf[0], b'A');
    }

    #[test]
    fn test_is_mb_niceformat_plain_ascii() {
        let _g = crate::test_util::global_state_lock();
        // Plain printable ASCII — nothing needs nicechar escaping.
        assert_eq!(is_mb_niceformat("hello world"), 0);
    }

    #[test]
    fn test_is_mb_niceformat_with_control_char() {
        let _g = crate::test_util::global_state_lock();
        // Tab is control (< 0x20) — needs nice escaping.
        assert_eq!(is_mb_niceformat("a\tb"), 1);
        // Bell character.
        assert_eq!(is_mb_niceformat("a\x07b"), 1);
    }

    /// c:4856/4954 — `metafy` + `unmetafy` MUST round-trip for ASCII.
    /// A regression in the round-trip corrupts every metafied buffer
    /// the lexer/param-subst pipeline produces.
    #[test]
    fn metafy_unmetafy_round_trips_for_ascii() {
        let _g = crate::test_util::global_state_lock();
        let s = "hello world";
        let m = metafy(s);
        let mut buf = m.into_bytes();
        unmetafy(&mut buf);
        assert_eq!(std::str::from_utf8(&buf).unwrap(), s);
    }

    /// `ztrlen` counts metafied characters (Meta-pairs as 1).
    /// Regression that double-counts Meta-pair bytes would break
    /// every fixed-width column path (printf %s).
    #[test]
    fn ztrlen_counts_ascii_one_per_byte() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(ztrlen(""), 0);
        assert_eq!(ztrlen("a"), 1);
        assert_eq!(ztrlen("hello"), 5);
    }

    /// `Src/utils.c:5141-5149` — Meta-byte pair counts as 1 char.
    /// `*s++ == Meta` advances 2 bytes per iteration but increments
    /// `l` only once. Pin: a string with one Meta+X pair counts as 1.
    #[test]
    fn ztrlen_counts_meta_pair_as_one() {
        let _g = crate::test_util::global_state_lock();
        // META (0x83) + 0x20 = unmetafies to one '\0' byte (or a NUL).
        let meta = char::from_u32(Meta as u32).unwrap();
        let s: String = [meta, '\x20'].iter().collect();
        assert_eq!(
            ztrlen(&s),
            1,
            "c:5141-5148 — Meta+X pair counts as ONE char, not two"
        );
        // Mixed: "a" + META + "x" + "b" = 3 unmetafied chars.
        let mixed: String = ['a', meta, '\x20', 'b'].iter().collect();
        assert_eq!(
            ztrlen(&mixed),
            3,
            "c:5141 — three unmetafied chars from 'a' + Meta+X + 'b'"
        );
    }

    /// `Src/utils.c:2579-2618` — `setblock_fd(turnonblocking, fd, modep)`.
    /// Signature pin: previously the Rust port had a 2-arg
    /// `(fd, blocking: bool) -> bool` signature, swapping the
    /// turnonblocking/fd argument order vs C AND collapsing the
    /// 3rd `*modep` out-param entirely. The fix restored canonical
    /// C order `(turnonblocking, fd)` with a `(bool, c_long)` tuple
    /// return mirroring `int return + long *modep`.
    ///
    /// Also pin the c:2599 regular-file short-circuit: C only
    /// operates on non-regular fds (pipes, sockets, ttys). A regular
    /// file returns `(false, -1)` immediately.
    #[cfg(unix)]
    #[test]
    fn setblock_fd_skips_regular_files_per_c_2599() {
        let _g = crate::test_util::global_state_lock();
        // Open a regular tempfile.
        let dir = tempfile::TempDir::new().unwrap();
        let f = fs::File::create(dir.path().join("regular")).unwrap();
        let fd = f.as_raw_fd();
        let (changed, mode) = setblock_fd(true, fd);
        assert!(
            !changed,
            "c:2599 — regular files short-circuit; setblock_fd must NOT report a change"
        );
        assert_eq!(mode, -1, "c:2614 — `*modep = -1` for regular files");
    }

    /// `Src/utils.c:2606-2611` — `setblock_fd(turnonblocking=true, fd)`
    /// clears O_NONBLOCK on a pipe (non-regular fd). Returns
    /// `(true, prior_flags)` if the state was changed.
    #[cfg(unix)]
    #[test]
    fn setblock_fd_clears_o_nonblock_on_pipe() {
        let _g = crate::test_util::global_state_lock();
        // Create a pipe — non-regular fd.
        let mut pipefd: [libc::c_int; 2] = [0; 2];
        let r = unsafe { libc::pipe(pipefd.as_mut_ptr()) };
        assert_eq!(r, 0, "pipe(2) must succeed");
        let read_fd = pipefd[0];
        let write_fd = pipefd[1];
        // Force NONBLOCK on the read end.
        let cur = unsafe { libc::fcntl(read_fd, libc::F_GETFL, 0) };
        unsafe {
            libc::fcntl(read_fd, libc::F_SETFL, cur | libc::O_NONBLOCK);
        }
        // Verify NONBLOCK is set.
        let now = unsafe { libc::fcntl(read_fd, libc::F_GETFL, 0) };
        assert_ne!(
            now & libc::O_NONBLOCK,
            0,
            "test setup: NONBLOCK should be set"
        );
        // Call setblock_fd to ENABLE blocking (clear NONBLOCK).
        let (changed, _mode) = setblock_fd(true, read_fd);
        assert!(
            changed,
            "c:2611 — turnonblocking=true on a NONBLOCK pipe must report state change"
        );
        // Verify NONBLOCK is now cleared.
        let after = unsafe { libc::fcntl(read_fd, libc::F_GETFL, 0) };
        assert_eq!(
            after & libc::O_NONBLOCK,
            0,
            "c:2611 — O_NONBLOCK must be cleared after turnonblocking=true"
        );
        // Cleanup.
        unsafe {
            libc::close(read_fd);
            libc::close(write_fd);
        }
    }

    /// `Src/utils.c:2620-2625` — `setblock_stdin()`. C body
    /// `setblock_fd(1, 0, &mode)` enables BLOCKING on fd 0 (stdin).
    /// Previously the Rust port called `setblock_fd(0, false)`
    /// which DISABLES blocking — exact opposite. Pin via real fd
    /// inspection.
    #[cfg(unix)]
    #[test]
    fn setblock_stdin_enables_blocking_on_fd_zero() {
        let _g = crate::test_util::global_state_lock();
        // Set stdin to NONBLOCKING first to verify the function
        // ACTUALLY switches it back to blocking. Skip if stdin is
        // not a normal fd (some CI configurations).
        let cur = unsafe { libc::fcntl(0, libc::F_GETFL, 0) };
        if cur < 0 {
            return;
        }
        // Force NONBLOCK first.
        unsafe {
            libc::fcntl(0, libc::F_SETFL, cur | libc::O_NONBLOCK);
        }
        let after_set_nb = unsafe { libc::fcntl(0, libc::F_GETFL, 0) };
        if after_set_nb & libc::O_NONBLOCK == 0 {
            // System rejected the change (regular file? CI tty?) — skip.
            unsafe {
                libc::fcntl(0, libc::F_SETFL, cur);
            }
            return;
        }
        // Call setblock_stdin — should CLEAR O_NONBLOCK.
        setblock_stdin();
        let after_setblock = unsafe { libc::fcntl(0, libc::F_GETFL, 0) };
        assert_eq!(
            after_setblock & libc::O_NONBLOCK,
            0,
            "c:2624 — setblock_stdin must CLEAR O_NONBLOCK (enable blocking)"
        );
        // Restore original flags.
        unsafe {
            libc::fcntl(0, libc::F_SETFL, cur);
        }
    }

    /// `Src/utils.c:2437-2519` — `zstrtol_underscore(s, base, false)`.
    /// Base-10 (explicit) parses canonical decimal.
    #[test]
    fn zstrtol_underscore_base_10_parses_decimal() {
        let _g = crate::test_util::global_state_lock();
        let (v, rest) = zstrtol_underscore("12345", 10, false);
        assert_eq!(v, 12345, "c:2471 — decimal accumulator");
        assert_eq!(rest, "", "rest is empty after full consumption");
        // With trailing non-digit.
        let (v, rest) = zstrtol_underscore("100abc", 10, false);
        assert_eq!(v, 100);
        assert_eq!(
            rest, "abc",
            "c:2467 — loop exits at first non-digit; rest carries on"
        );
    }

    /// c:2452-2461 — base==0 autodetect: `0x`→16, `0b`→2, leading
    /// `0`→8 (always, unlike `zstrtoul_underscore` which honors
    /// OCTALZEROES). `zstrtol_underscore` does NOT consult
    /// OCTALZEROES — pure prefix detection.
    #[test]
    fn zstrtol_underscore_base_zero_autodetects_prefix() {
        let _g = crate::test_util::global_state_lock();
        // Hex.
        assert_eq!(
            zstrtol_underscore("0xff", 0, false).0,
            255,
            "c:2455 — 0x → base 16"
        );
        assert_eq!(zstrtol_underscore("0XFF", 0, false).0, 255);
        // Binary.
        assert_eq!(
            zstrtol_underscore("0b1010", 0, false).0,
            10,
            "c:2457 — 0b → base 2"
        );
        // Leading 0 → octal (always, no OCTALZEROES gate).
        assert_eq!(
            zstrtol_underscore("0777", 0, false).0,
            511,
            "c:2460 — leading 0 → octal (no OCTALZEROES gate for zstrtol)"
        );
        // Decimal default.
        assert_eq!(zstrtol_underscore("12345", 0, false).0, 12345);
    }

    /// c:2447-2450 — leading `-` and `+` consumed; `-` triggers
    /// negation at the end (c:2497-2498).
    #[test]
    fn zstrtol_underscore_handles_sign_chars() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            zstrtol_underscore("-42", 10, false).0,
            -42,
            "c:2447 — leading `-` → negate"
        );
        assert_eq!(
            zstrtol_underscore("+42", 10, false).0,
            42,
            "c:2449 — leading `+` consumed, no negation"
        );
        // Mixed whitespace + sign.
        assert_eq!(
            zstrtol_underscore("  -100", 10, false).0,
            -100,
            "c:2444 — leading whitespace skipped, then sign"
        );
    }

    /// c:2466-2492 — base 16 (hex) accumulator: digits 0-9 + letters
    /// a-f / A-F via `idigit(*s)` || `'a' ≤ *s < 'a'+base-10`.
    /// Pin both upper- and lower-case.
    #[test]
    fn zstrtol_underscore_base_16_accepts_letters() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            zstrtol_underscore("ff", 16, false).0,
            255,
            "c:2485 — base-16 'ff' → 255"
        );
        assert_eq!(
            zstrtol_underscore("FF", 16, false).0,
            255,
            "c:2485 — base-16 'FF' → 255 (upper case via `*s & 0x1f`)"
        );
        assert_eq!(
            zstrtol_underscore("DEADbeef", 16, false).0,
            0xDEADBEEF,
            "c:2485 — mixed case 32-bit hex"
        );
    }

    /// c:2468/2482 — `underscore` flag enables digit-separator. With
    /// underscore=false, `_` is treated as end-of-digits. With
    /// underscore=true, `_` is skipped (c:2469-2470).
    #[test]
    fn zstrtol_underscore_underscore_flag() {
        let _g = crate::test_util::global_state_lock();
        // underscore=false: `_` terminates parse.
        let (v, rest) = zstrtol_underscore("1_000", 10, false);
        assert_eq!(v, 1, "c:2467 — underscore=false: `_` terminates");
        assert_eq!(rest, "_000");
        // underscore=true: `_` accepted and skipped.
        let (v, rest) = zstrtol_underscore("1_000_000", 10, true);
        assert_eq!(
            v, 1_000_000,
            "c:2469-2470 — underscore=true: `_` skipped during accumulation"
        );
        assert_eq!(rest, "", "fully consumed including `_`s");
    }

    /// `Src/utils.c:2750-2774` — `timespec_diff_us(t1, t2)` returns
    /// the signed microsecond delta `t2 - t1`. Pin both sign
    /// conventions: t2 after t1 → positive; t2 before t1 → negative.
    #[test]
    fn timespec_diff_us_sign_matches_c_t2_minus_t1() {
        let _g = crate::test_util::global_state_lock();
        let t1 = std::time::Instant::now();
        std::thread::sleep(std::time::Duration::from_millis(2));
        let t2 = std::time::Instant::now();
        // c:2759 — `diff_sec = t2 - t1` for the t2 > t1 path. Result
        // positive (microseconds elapsed).
        let d = timespec_diff_us(&t1, &t2);
        assert!(d > 0, "c:2759 — t2 after t1 → positive delta");
        assert!(d >= 1000, "expected >= 1ms (1000us); got {}us", d);
        // c:2754 — reversed (t1 > t2): result negative.
        let r = timespec_diff_us(&t2, &t1);
        assert!(r < 0, "c:2770 — t1 after t2 → negative delta");
        assert_eq!(d, -r, "swap of args negates the result");
    }

    /// c:2752 — `timespec_diff_us(t, t)` is 0 (identical Instants).
    #[test]
    fn timespec_diff_us_same_instant_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        let t = std::time::Instant::now();
        assert_eq!(
            timespec_diff_us(&t, &t),
            0,
            "c:2752 — identical Instants → 0"
        );
    }

    /// `Src/utils.c:6743-6779` — `ucs4toutf8(dest, wval)`. Encodes
    /// a Unicode codepoint to UTF-8. Pin canonical 1-, 2-, 3-, and
    /// 4-byte encodings.
    #[test]
    fn ucs4toutf8_encodes_canonical_lengths() {
        let _g = crate::test_util::global_state_lock();
        // c:6750 — 1 byte: ASCII range [0, 0x80).
        assert_eq!(
            ucs4toutf8(0x41),
            Some("A".to_string()),
            "c:6750 — 0x41 → 'A' (1 byte)"
        );
        // c:6752 — 2 bytes: [0x80, 0x800). 'é' = U+00E9.
        assert_eq!(
            ucs4toutf8(0xe9),
            Some("é".to_string()),
            "c:6752 — U+00E9 → 'é' (2 bytes)"
        );
        // c:6754 — 3 bytes: [0x800, 0x10000). '字' = U+5B57.
        assert_eq!(
            ucs4toutf8(0x5B57),
            Some("字".to_string()),
            "c:6754 — U+5B57 → '字' (3 bytes)"
        );
        // c:6756 — 4 bytes: [0x10000, 0x200000). '𝄞' = U+1D11E.
        assert_eq!(
            ucs4toutf8(0x1D11E),
            Some("𝄞".to_string()),
            "c:6756 — U+1D11E → '𝄞' (4 bytes)"
        );
    }

    /// `Src/utils.c:6762-6764` — values `>= 0x80000000` are
    /// out-of-range; C emits `zerr("character not in range")` and
    /// returns `-1`. Surrogates (U+D800..U+DFFF) and values in
    /// 0x110000..0x80000000 are *encoded as raw bytes* by C
    /// (matches `wctomb(3)` extended UCS-4 range).
    #[test]
    fn ucs4toutf8_rejects_invalid_codepoints() {
        let _g = crate::test_util::global_state_lock();
        // c:6762-6764 — value >= 0x80000000 → None.
        assert_eq!(
            ucs4toutf8(0xFFFF_FFFE),
            None,
            "c:6763 — values >= 0x80000000 → None"
        );
        assert_eq!(
            ucs4toutf8(0x8000_0000),
            None,
            "c:6760-6764 — exactly 0x80000000 is out of range"
        );
        // Surrogates encode as 3-byte sequences per C bit-pattern
        // (c:6754 → len=3). The C source has no Unicode-validity
        // gate; that's a wctomb-only concern in callers.
        assert!(
            ucs4toutf8(0xD800).is_some(),
            "c:6754 — surrogates encode as 3-byte UTF-8 in C (no Unicode validity check)"
        );
    }

    /// `Src/utils.c:6648-6723` — `dquotedztrdup(s)`. Default (non-
    /// CSHJUNKIEQUOTES) arm: wrap whole string in `"..."`. Backslash
    /// doubles, `"`/`$`/`` ` `` get escaped.
    #[test]
    fn dquotedztrdup_default_path_wraps_whole_string() {
        let _g = crate::test_util::global_state_lock();
        if isset(CSHJUNKIEQUOTES) {
            return; // Default-only test.
        }
        // c:6690 — `*p++ = '"';` then iterate.
        assert_eq!(
            dquotedztrdup("hello"),
            "\"hello\"",
            "c:6690+6711+6718 — wrap plain text in double quotes"
        );
        // c:6703-6708 — `$` gets `\$`.
        assert_eq!(
            dquotedztrdup("$var"),
            "\"\\$var\"",
            "c:6703-6708 — `$` → `\\$`"
        );
        // c:6703-6708 — `"` gets `\"`.
        assert_eq!(
            dquotedztrdup("a\"b"),
            "\"a\\\"b\"",
            "c:6703-6708 — `\"` → `\\\"`"
        );
    }

    /// c:6697-6701 — backslash handling with `pending` state.
    /// Single `\` followed by an ordinary char emits just `\` (no
    /// doubling). The "pending" extra `\` fires only when the next
    /// char is itself a backslash or one of the escaped specials
    /// (`"`/`$`/`` ` ``), OR at end-of-string before the closing `"`.
    #[test]
    fn dquotedztrdup_pending_backslash_only_doubles_when_needed() {
        let _g = crate::test_util::global_state_lock();
        if isset(CSHJUNKIEQUOTES) {
            return;
        }
        // Mid-string `\` before ordinary char: stays as single `\`.
        assert_eq!(
            dquotedztrdup("a\\b"),
            "\"a\\b\"",
            "c:6711+6712 — `\\b` mid-string: no extra (pending=0 after b)"
        );
        // Trailing `\` triggers pending quirk: c:6716-6717 emits
        // an extra `\` before the closing `"`.
        let r = dquotedztrdup("a\\");
        assert!(
            r.ends_with("\\\\\""),
            "c:6716-6717 — trailing `\\` → emit extra `\\` before closing `\"`: got {:?}",
            r
        );
    }

    /// `Src/utils.c:6464-6549` — `quotedzputs(s)`. Three branches:
    ///   - empty → `''`.
    ///   - has special chars (via `hasspecial`) → wrap in `'...'`
    ///     with `'\''` escape for embedded apostrophes.
    ///   - else → return unchanged.
    /// Pin all three.
    #[test]
    fn quotedzputs_empty_string_returns_double_quotes() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotedzputs(""), "''", "c:6470-6475 — empty input → ''");
    }

    /// c:6514-6543 — needs-quote path: wrap in single quotes,
    /// escape embedded `'` as `'\''`.
    #[test]
    fn quotedzputs_wraps_specials_in_single_quotes() {
        let _g = crate::test_util::global_state_lock();
        inittyptab();
        // Space is special → wrap.
        assert_eq!(quotedzputs("hello world"), "'hello world'");
        // `=` is special → wrap.
        assert_eq!(quotedzputs("foo=bar"), "'foo=bar'");
        // Embedded apostrophe — `'\''` escape.
        assert_eq!(
            quotedzputs("it's"),
            "'it'\\''s'",
            "c:6517-6519 — apostrophe → '\\''"
        );
    }

    /// c:6512 — no specials → return unchanged.
    #[test]
    fn quotedzputs_plain_alnum_returns_unchanged() {
        let _g = crate::test_util::global_state_lock();
        inittyptab();
        assert_eq!(
            quotedzputs("hello"),
            "hello",
            "c:6512 — no specials, no wrap"
        );
        assert_eq!(quotedzputs("abc123"), "abc123");
    }

    /// c:6578-6637 — Bourne path "avoids empty quoted strings" by
    /// tracking `inquote`. Input `'x` should not produce empty `''`
    /// at the front; `x'` should not produce trailing `''`.
    #[test]
    fn quotedzputs_avoids_empty_quoted_runs_on_boundaries() {
        let _g = crate::test_util::global_state_lock();
        inittyptab();
        // Leading `'`: c:6587 with inquote=0 skips the close, emits
        // `\'`. Then `x`: c:6604 opens a quote, emits `x`, end emits
        // closing `'`. Result: `\''x'` — the visible doubled `''` is
        // `\'` (escaped) + `'` (opening single-quote), NOT an empty
        // single-quoted string.
        assert_eq!(
            quotedzputs("'x"),
            "\\''x'",
            "c:6587-6610 — leading `'` produces `\\'` + opening `'`"
        );
        // Trailing `'`: open quote at `x`, see `'` with inquote=1 → close
        // (`'`), emit `\'`. End-of-string: inquote=0, no trailing `'`.
        // Result: `'x'\\'` — NO trailing `''`.
        assert_eq!(
            quotedzputs("x'"),
            "'x'\\'",
            "c:6631-6637 — trailing `'` doesn't generate empty `''`"
        );
    }

    /// c:6478-6492 — `is_mb_niceformat` arm of quotedzputs uses
    /// mb_niceformat with NICEFLAG_QUOTE so embedded `'` becomes
    /// `\'` inside the `$'...'` wrapper (preventing the quote from
    /// terminating the wrap). Previously sb_niceformat without
    /// NICEFLAG_QUOTE was used, which would have left `'` raw.
    #[test]
    fn quotedzputs_dollar_quote_escapes_apostrophe_inside_wrap() {
        let _g = crate::test_util::global_state_lock();
        inittyptab();
        // String with embedded `'` AND a control char so the
        // is_mb_niceformat arm is taken (controls trigger it).
        let r = quotedzputs("a\nb'c");
        // Expected: `$'a\nb\'c'` — the embedded `'` is `\'` inside
        // dollar-quotes, NOT terminating the wrap.
        assert!(
            r.starts_with("$'") && r.ends_with("'"),
            "c:6488 — must be wrapped in $'...'  got {:?}",
            r
        );
        assert!(
            r.contains("\\'"),
            "c:5413-5414 — embedded `'` must be `\\'` not raw `'`: got {:?}",
            r
        );
    }

    /// c:6533-6576 — RCQUOTES branch: wrap everything in `'…'`,
    /// double embedded `'` as `''`.
    #[test]
    fn quotedzputs_rcquotes_doubles_apostrophe() {
        let _g = crate::test_util::global_state_lock();
        inittyptab();
        let prev = crate::ported::options::opt_state_get("rcquotes").unwrap_or(false);
        crate::ported::options::opt_state_set("rcquotes", true);
        // RCQUOTES: `it's` → `'it''s'` (doubled `'`, NOT `'\''`).
        let got = quotedzputs("it's");
        crate::ported::options::opt_state_set("rcquotes", prev);
        assert_eq!(
            got, "'it''s'",
            "c:6548-6553 — RCQUOTES: `'` → `''` (doubled)"
        );
    }

    /// `Src/utils.c:2331-2337` — `strucpy(s, t)`. Appends `t` to
    /// `*s` and leaves `*s` pointing at the NUL terminator (Rust
    /// equivalent: append-in-place; `.len()` gives the new end).
    /// Pin: empty input → no-op; non-empty input → append.
    #[test]
    fn strucpy_appends_t_to_dest() {
        let _g = crate::test_util::global_state_lock();
        let mut dest = String::from("prefix-");
        strucpy(&mut dest, "suffix");
        assert_eq!(
            dest, "prefix-suffix",
            "c:2335 — strucpy appends t (NOT case-changes — c-name 'u' is pointer-walk, not upper)"
        );
        // Empty t — no change.
        let mut dest = String::from("alone");
        strucpy(&mut dest, "");
        assert_eq!(dest, "alone");
        // Empty dest + non-empty t.
        let mut dest = String::new();
        strucpy(&mut dest, "hello");
        assert_eq!(dest, "hello");
    }

    /// `Src/utils.c:2341-2350` — `struncpy(s, t, n)`. Appends up to
    /// `n` bytes of `t` to `*s`. Pin: n=0 no-op; n < len(t) clips;
    /// n >= len(t) appends all.
    #[test]
    fn struncpy_appends_up_to_n_bytes() {
        let _g = crate::test_util::global_state_lock();
        // n < len(t).
        let mut dest = String::from("X");
        struncpy(&mut dest, "abcdef", 3);
        assert_eq!(dest, "Xabc", "c:2345 — clipped to n=3 bytes of `abcdef`");
        // n >= len(t).
        let mut dest = String::from("Y");
        struncpy(&mut dest, "abc", 100);
        assert_eq!(dest, "Yabc", "c:2345 — full copy when n >= len");
        // n=0 → no append.
        let mut dest = String::from("Z");
        struncpy(&mut dest, "abc", 0);
        assert_eq!(dest, "Z", "c:2345 — n=0 stops the copy loop immediately");
    }

    /// `Src/utils.c:2280-2288` — `has_token(s)`. Returns true iff
    /// any byte in `s` triggers `itok()`. Pin: ASCII strings →
    /// false; strings containing token bytes (Pound 0x84, Bang
    /// 0x9c, Nularg 0xa1) → true.
    #[test]
    fn has_token_detects_typtab_token_bytes() {
        let _g = crate::test_util::global_state_lock();
        inittyptab();
        // c:2285 — pure ASCII has no token bytes.
        assert!(!has_token(""), "empty: no tokens");
        assert!(
            !has_token("hello world"),
            "c:2285 — ASCII text has no token bytes"
        );
        // Pound (0x84) — first token byte.
        let s: String = std::iter::once(0x84u8 as char).collect();
        assert!(
            has_token(&s),
            "c:2285 — Pound (0x84) is itok → has_token=true"
        );
        // Bang (0x9c) — last_normal_tok.
        let s: String = std::iter::once(0x9cu8 as char).collect();
        assert!(has_token(&s), "c:2285 — Bang (0x9c) is itok");
        // Nularg (0xa1) — upper bound.
        let s: String = std::iter::once(0xa1u8 as char).collect();
        assert!(has_token(&s), "c:2285 — Nularg (0xa1) is itok");
    }

    /// `Src/utils.c:2280` — Meta byte (0x83) is NOT a token. Pin
    /// the regression: previously the Rust port hardcoded `0x83`
    /// as a token byte. Now correctly excludes it.
    #[test]
    fn has_token_excludes_meta_byte() {
        let _g = crate::test_util::global_state_lock();
        inittyptab();
        // 0x83 is Meta, NOT itok. Should return false.
        let s: String = std::iter::once(0x83u8 as char).collect();
        assert!(
            !has_token(&s),
            "c:2285 — Meta (0x83) is NOT itok; previous hardcoded 0x83 list misfired"
        );
    }

    /// `Src/utils.c:6082-6124` — `addunprintable(c)`. Renders bytes
    /// using shell-compatible C-string escapes (`\a`/`\b`/`\f`/`\n`/
    /// `\r`/`\t`/`\v` for the named controls + `\nnn` octal for
    /// others + `\0` for NUL). Previously emitted ZLE caret form —
    /// wrong convention.
    #[test]
    fn addunprintable_named_control_escapes() {
        let _g = crate::test_util::global_state_lock();
        // c:6106-6112 — named-escape per byte.
        assert_eq!(addunprintable('\x07'), "\\a", "c:6106 — BEL → \\a");
        assert_eq!(addunprintable('\x08'), "\\b", "c:6107 — BS → \\b");
        assert_eq!(addunprintable('\x0c'), "\\f", "c:6108 — FF → \\f");
        assert_eq!(addunprintable('\n'), "\\n", "c:6109 — LF → \\n");
        assert_eq!(addunprintable('\r'), "\\r", "c:6110 — CR → \\r");
        assert_eq!(addunprintable('\t'), "\\t", "c:6111 — TAB → \\t");
        assert_eq!(addunprintable('\x0b'), "\\v", "c:6112 — VT → \\v");
    }

    /// c:6097-6103 — NUL renders as `\0`. (C peeks next byte for
    /// `\000` disambiguation; Rust port works one char at a time
    /// so emits the short `\0` form.)
    #[test]
    fn addunprintable_nul_renders_as_backslash_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(addunprintable('\0'), "\\0", "c:6097-6098 — NUL → \\0");
    }

    /// c:6114-6119 — `\nnn` 3-digit octal fallback for un-named
    /// control bytes. Pin the format: each digit is one octal digit
    /// (0-7), zero-padded to 3 positions.
    #[test]
    fn addunprintable_octal_fallback_for_unnamed_controls() {
        let _g = crate::test_util::global_state_lock();
        // 0x01 (SOH) = 001 octal.
        assert_eq!(addunprintable('\x01'), "\\001", "c:6116-6118 — SOH → \\001");
        // 0x1b (ESC) = 033 octal.
        assert_eq!(addunprintable('\x1b'), "\\033", "c:6116-6118 — ESC → \\033");
        // 0x7f (DEL) = 177 octal.
        assert_eq!(addunprintable('\x7f'), "\\177", "c:6116-6118 — DEL → \\177");
        // 0xff (high) = 377 octal.
        assert_eq!(
            addunprintable(char::from_u32(0xff).unwrap()),
            "\\377",
            "c:6116-6118 — 0xff → \\377"
        );
    }

    /// `Src/utils.c:6072-6082` — `hasspecial(s)`. Pin canonical
    /// special chars from SPECCHARS (`Src/zsh.h:228`): `#$^*()=|{}[]
    /// \`<>?~;&\\n\\t \\\\\\'\\"`.
    #[test]
    fn hasspecial_recognises_canonical_special_chars() {
        let _g = crate::test_util::global_state_lock();
        // typtab access is read-only here (no flag mutation); concurrent
        // inittyptab() rebuilds are idempotent for the default flag set.
        inittyptab();
        // c:6075 — every char in SPECCHARS triggers true.
        for c in "#$^*()=|{}[]<>?~;&".chars() {
            let s = c.to_string();
            assert!(
                hasspecial(&s),
                "c:6075 — '{}' is in SPECCHARS, must be special",
                c
            );
        }
        // Whitespace specials.
        assert!(hasspecial(" "), "space in SPECCHARS");
        assert!(hasspecial("\t"), "tab in SPECCHARS");
        assert!(hasspecial("\n"), "newline in SPECCHARS");
        // Non-special chars → false.
        assert!(
            !hasspecial("hello"),
            "c:6075 — plain alphanumerics are NOT special"
        );
        assert!(!hasspecial("ABC012"));
    }

    /// `Src/utils.c:6072-6075` — Meta-byte handling: `*s == Meta ?
    /// *++s ^ 32 : *s`. Pin Meta+X pair gets decoded before special-
    /// check. A Meta+'A' decodes to 'a' (not special), so a string
    /// containing only Meta+'A' returns false.
    #[test]
    fn hasspecial_decodes_meta_byte_before_check() {
        let _g = crate::test_util::global_state_lock();
        // Same as above — read-only after inittyptab.
        inittyptab();
        // Meta + 0x41 ('A') → 'a' (0x61) → not special.
        let bytes: Vec<u8> = vec![Meta, 0x41u8];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        assert!(
            !hasspecial(s),
            "c:6075 — Meta+0x41 decodes to 'a' which is NOT special"
        );
    }

    /// `Src/utils.c:5849-5910` — `sb_niceformat(s)`. Pin: ASCII
    /// printable passes through unchanged; controls escape.
    /// C-equivalent call shape: `char *out; sb_niceformat(s, NULL, &out, 0);`
    #[test]
    fn sb_niceformat_passes_printable_ascii() {
        let _g = crate::test_util::global_state_lock();
        let mut out: Option<String> = None;
        let l = sb_niceformat("hello", None, Some(&mut out), 0);
        assert_eq!(
            out.as_deref(), Some("hello"),
            "c:5886 — nicechar_sel passes printable through"
        );
        assert_eq!(l, 5, "c:5928 — return length equals output bytes");

        let mut out: Option<String> = None;
        sb_niceformat("", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some(""));

        let mut out: Option<String> = None;
        sb_niceformat("ABC012!?@", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("ABC012!?@"));
    }

    /// c:5886 — controls get `\n`/`\t`/`^X`/`^?` escapes via nicechar_sel.
    #[test]
    fn sb_niceformat_escapes_controls() {
        let _g = crate::test_util::global_state_lock();
        let mut out: Option<String> = None;
        sb_niceformat("a\nb", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("a\\nb"), "c:5886 — newline → \\n");

        let mut out: Option<String> = None;
        sb_niceformat("a\tb", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("a\\tb"));

        let mut out: Option<String> = None;
        sb_niceformat("\x01", None, Some(&mut out), 0);
        assert_eq!(
            out.as_deref(), Some("^A"),
            "c:5886 — control char → ^X form"
        );

        let mut out: Option<String> = None;
        sb_niceformat("\x7f", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("^?"));
    }

    /// `Src/utils.c:5872` — unmetafy step before formatting. Pin:
    /// metafied Meta+X pair gets unescaped first, then run through
    /// nicechar_sel.
    #[test]
    fn sb_niceformat_unmetafies_before_formatting() {
        let _g = crate::test_util::global_state_lock();
        // META + 0x41 ('A') → decodes to 'a' (0x61). 'a' is printable
        // → passes through unchanged.
        let bytes: Vec<u8> = vec![Meta, 0x41u8];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        let mut out: Option<String> = None;
        sb_niceformat(s, None, Some(&mut out), 0);
        assert_eq!(
            out.as_deref(), Some("a"),
            "c:5872 — unmetafy first: Meta+0x41 → 'a' → printable passthrough"
        );
        // META + 0x20 → decodes to NUL → "^@" form.
        let bytes: Vec<u8> = vec![Meta, 0x20u8];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        let mut out: Option<String> = None;
        sb_niceformat(s, None, Some(&mut out), 0);
        let r = out.unwrap_or_default();
        assert!(
            !r.is_empty(),
            "c:5872 — Meta+0x20 → NUL → must emit some escape, not empty"
        );
    }

    /// `Src/utils.c:5937-5959` — `is_sb_niceformat(s)`. Returns 1
    /// if sb_niceformat would change the input, else 0.
    #[test]
    fn is_sb_niceformat_true_for_strings_with_controls() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(is_sb_niceformat("\n"), 1, "newline is nice");
        assert_eq!(is_sb_niceformat("a\tb"), 1);
        assert_eq!(is_sb_niceformat("\x01"), 1);
        // Non-control ASCII → 0.
        assert_eq!(is_sb_niceformat("hello"), 0);
        assert_eq!(is_sb_niceformat(""), 0);
    }

    /// `Src/utils.c:5810-5826` — `metacharlenconv(x, c)`. Returns
    /// `(2, decoded)` for Meta+X pair, `(1, byte)` for plain byte.
    /// Pin both branches.
    #[test]
    fn metacharlenconv_plain_ascii_returns_one_byte() {
        let _g = crate::test_util::global_state_lock();
        // c:5823-5825 — plain byte.
        let (n, c) = metacharlenconv("a");
        assert_eq!((n, c), (1, Some('a')), "c:5823 — plain byte → (1, byte)");
        let (n, c) = metacharlenconv("X");
        assert_eq!((n, c), (1, Some('X')));
    }

    /// c:5818-5821 — Meta+X pair: `*c = x[1] ^ 32; return 2`.
    #[test]
    fn metacharlenconv_meta_pair_xor_decodes() {
        let _g = crate::test_util::global_state_lock();
        // Meta + 0x41 ('A') → 'A' ^ 32 = 'a'.
        let bytes: Vec<u8> = vec![Meta, 0x41u8];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        let (n, c) = metacharlenconv(s);
        assert_eq!(
            (n, c),
            (2, Some('a')),
            "c:5820 — Meta+0x41 → 'a' (XOR 32), consumed 2 bytes"
        );
        // Empty input.
        let (n, c) = metacharlenconv("");
        assert_eq!((n, c), (0, None));
    }

    /// `Src/utils.c:5832-5843` — `charlenconv(x, len, c)`. Returns
    /// `(0, NUL)` when len=0, `(1, byte)` otherwise.
    #[test]
    fn charlenconv_zero_len_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        // c:5834 — `if (!len) { *c = '\0'; return 0; }`.
        let (n, c) = charlenconv("abc", 0);
        assert_eq!((n, c), (0, None), "c:5834-5837 — len=0 returns 0");
    }

    /// c:5840-5842 — non-zero len: read one byte, return (1, byte).
    #[test]
    fn charlenconv_returns_first_byte_for_nonzero_len() {
        let _g = crate::test_util::global_state_lock();
        let (n, c) = charlenconv("abc", 3);
        assert_eq!((n, c), (1, Some('a')), "c:5841 — *c = *x; return 1");
        let (n, c) = charlenconv("xy", 2);
        assert_eq!((n, c), (1, Some('x')));
    }

    /// `Src/utils.c:5474-5524` — `is_mb_niceformat(s)`. Predicate:
    /// would mb_niceformat produce a different output? Pin canonical
    /// branches:
    ///   - Pure ASCII printable → false (no escape needed).
    ///   - Contains control char → true.
    ///   - Pure UTF-8 wide chars → false under default PRINTEIGHTBIT-off
    ///     since is_wcs_nicechar treats them as printable.
    #[test]
    fn is_mb_niceformat_false_for_pure_printable_ascii() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            is_mb_niceformat("hello"), 0,
            "c:5509 — printable ASCII needs no nice-format"
        );
        assert_eq!(
            is_mb_niceformat(""), 0,
            "c:5486 — empty string has no chars to flag"
        );
        assert_eq!(is_mb_niceformat("ABC012!?@"), 0);
    }

    /// c:5509 + `is_wcs_nicechar` — newline/tab/control chars trigger
    /// the predicate.
    #[test]
    fn is_mb_niceformat_true_for_strings_with_controls() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(is_mb_niceformat("a\nb"), 1, "c:5509 — newline is nice");
        assert_eq!(is_mb_niceformat("\t"), 1);
        assert_eq!(is_mb_niceformat("\x01"), 1, "c:5509 — control char is nice");
        assert_eq!(is_mb_niceformat("\x7f"), 1, "c:5509 — DEL is nice");
    }

    /// `Src/utils.c:5366-5460` — `mb_niceformat(s)`. Multibyte-aware
    /// nice-formatter. Calls `wcs_nicechar` per char. Test uses
    /// C-equivalent call shape: `char *out; mb_niceformat(s, NULL, &out, 0);`
    #[test]
    fn mb_niceformat_preserves_printable_wide_chars() {
        let _g = crate::test_util::global_state_lock();
        let mut out: Option<String> = None;
        mb_niceformat("hello", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("hello"), "c:5407 — printable ASCII passes through");

        let mut out: Option<String> = None;
        mb_niceformat("café", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("café"), "c:5407 — Latin-1 'é' must NOT byte-mask to \\M-X");

        let mut out: Option<String> = None;
        mb_niceformat("字", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("字"), "c:5407 — CJK printable passes through");

        let mut out: Option<String> = None;
        mb_niceformat("abcéxyz", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("abcéxyz"));
    }

    /// `Src/utils.c:5407` + `wcs_nicechar` — controls still escape.
    #[test]
    fn mb_niceformat_escapes_controls() {
        let _g = crate::test_util::global_state_lock();
        let mut out: Option<String> = None;
        mb_niceformat("\n", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("\\n"), "c:5407 → wcs_nicechar c:625 — newline escapes");

        let mut out: Option<String> = None;
        mb_niceformat("\t", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("\\t"), "c:5407 → wcs_nicechar c:628 — tab escapes");

        let mut out: Option<String> = None;
        mb_niceformat("a\nb", None, Some(&mut out), 0);
        assert_eq!(out.as_deref(), Some("a\\nb"));
    }

    /// `Src/utils.c:4971-4983` — `metalen(s, len)`. **Input `len` is
    /// the UNMETAFIED char count; output is the METAFIED byte count.**
    /// Pin a metafied string with one Meta+X pair: 3 unmetafied chars
    /// → 4 metafied bytes.
    #[test]
    fn metalen_returns_metafied_byte_count() {
        let _g = crate::test_util::global_state_lock();
        // Pure ASCII: 5 chars → 5 bytes (no Meta encountered).
        assert_eq!(
            metalen("hello", 5),
            5,
            "c:4972 — ASCII: metafied bytes == unmetafied chars"
        );
        // [a, Meta, X, b] = 4 metafied bytes representing 3 chars.
        let bytes: Vec<u8> = vec![b'a', Meta, 0x41, b'b'];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        assert_eq!(
            metalen(s, 3),
            4,
            "c:4978 — 3 unmetafied chars + 1 Meta = 4 metafied bytes"
        );
        // Two Meta pairs: [Meta, X, Meta, Y] = 4 bytes for 2 chars.
        let bytes: Vec<u8> = vec![Meta, 0x41, Meta, 0x42];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        assert_eq!(
            metalen(s, 2),
            4,
            "c:4978 — 2 unmetafied chars (both Meta+X) = 4 metafied bytes"
        );
    }

    /// c:4974 — `mlen = len;` is the initial value (no Meta found,
    /// returns the input length unchanged). Pin len=0 and pure-ASCII.
    #[test]
    fn metalen_returns_input_for_no_meta_chars() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(metalen("", 0), 0, "c:4974 — empty input returns 0");
        assert_eq!(
            metalen("abc", 3),
            3,
            "c:4974 — no Meta chars: output == input len"
        );
    }

    /// `Src/utils.c:5070` — `if (!in || !*in) return 0;`.
    /// Empty input returns `('\0', 0)`. Pin: empty &str → 0 bytes consumed.
    #[test]
    fn unmeta_one_empty_input_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        let (c, n) = unmeta_one("");
        assert_eq!(c, '\0', "c:5070 — empty input returns NUL char");
        assert_eq!(n, 0, "c:5070 — empty input consumes 0 bytes");
    }

    /// `Src/utils.c:5081-5082` — Non-Meta byte: `*sz = 1; wc = byte`.
    #[test]
    fn unmeta_one_plain_ascii_consumes_one_byte() {
        let _g = crate::test_util::global_state_lock();
        for c in "aA0!~".chars() {
            let s = c.to_string();
            let (got, n) = unmeta_one(&s);
            assert_eq!(got, c, "c:5082 — '{}' decodes to itself", c);
            assert_eq!(n, 1, "c:5081 — non-Meta byte consumes 1");
        }
    }

    /// `Src/utils.c:5077-5079` — Meta byte: `*sz = 2; wc = in[1] ^ 32`.
    /// Pin via constructed Meta byte sequence.
    #[test]
    fn unmeta_one_meta_pair_decodes_xor_32() {
        let _g = crate::test_util::global_state_lock();
        // META + 0x41 ('A') → 'A' ^ 32 = 0x61 ('a'). 2 bytes consumed.
        let bytes: Vec<u8> = vec![Meta, 0x41u8];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        let (got, n) = unmeta_one(s);
        assert_eq!(got, 'a', "c:5079 — Meta+0x41 decodes to 0x41^32 = 'a'");
        assert_eq!(n, 2, "c:5078 — Meta pair consumes 2 bytes");
        // META + 0x20 = 0x20^32 = 0x00 (NUL).
        let bytes: Vec<u8> = vec![Meta, 0x20u8];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        let (got, n) = unmeta_one(s);
        assert_eq!(
            got, '\0',
            "c:5079 — Meta+0x20 decodes to NUL (the canonical metafy-NUL pattern)"
        );
        assert_eq!(n, 2);
    }

    /// Edge case: bare Meta byte at end with no follower. C source
    /// would read past the buffer (UB); Rust port's `bytes.len() > 1`
    /// guard handles this by falling through to the non-Meta arm.
    #[test]
    fn unmeta_one_trailing_meta_byte_falls_through() {
        let _g = crate::test_util::global_state_lock();
        let bytes: Vec<u8> = vec![Meta];
        let s = unsafe { std::str::from_utf8_unchecked(&bytes) };
        let (_, n) = unmeta_one(s);
        // Defensive: Rust returns the Meta byte itself as char + 1.
        assert_eq!(n, 1, "trailing Meta — no panic, no read past buffer");
    }

    /// `Src/utils.c:5185-5203` — `ztrsub(t, s)`. Count of unmetafied
    /// chars between two pointers in the same metafied string.
    /// Rust port takes (buf, start, end) byte-offsets since raw
    /// pointer subtraction across distinct &str isn't safe.
    /// Pin pure-ASCII subrange (no Meta) → returns end-start.
    #[test]
    fn ztrsub_ascii_no_meta_returns_byte_distance() {
        let _g = crate::test_util::global_state_lock();
        // "hello world" — substring [0..5) → "hello" → 5 chars.
        assert_eq!(
            ztrsub("hello world", 0, 5),
            5,
            "c:5189 — no Meta: returns t-s byte distance"
        );
        // [6..11) → "world" → 5 chars.
        assert_eq!(ztrsub("hello world", 6, 11), 5);
        // Empty range.
        assert_eq!(ztrsub("hello", 2, 2), 0);
    }

    /// c:5191-5199 — each Meta+X pair decrements `l` by 1 (since the
    /// initial l = byte-distance counts both bytes but the pair is
    /// one logical char). Pin via constructed Meta string.
    #[test]
    fn ztrsub_subtracts_one_per_meta_pair() {
        let _g = crate::test_util::global_state_lock();
        // Build "a" + Meta + 0x20 + "b" = 4 bytes total.
        // Unmetafied: "a" + 1-char-from-meta + "b" = 3 chars.
        let meta_byte = Meta;
        let buf_bytes: Vec<u8> = vec![b'a', meta_byte, 0x20, b'b'];
        let buf = unsafe { std::str::from_utf8_unchecked(&buf_bytes) };
        // [0..4) byte distance = 4; Meta pair found → l-- → 3.
        assert_eq!(
            ztrsub(buf, 0, 4),
            3,
            "c:5192-5199 — Meta pair decrements distance by 1"
        );
    }

    /// c:5189 — `int l = t - s;` start of count. Pin edge case
    /// where start > end (caller bug) doesn't underflow or panic;
    /// Rust port clamps via `start.min(end)`.
    #[test]
    fn ztrsub_clamps_inverted_range_to_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            ztrsub("hello", 4, 2),
            0,
            "inverted range start>end → 0 (defensive; not in C contract)"
        );
        // Beyond-buffer end gets clamped.
        assert_eq!(
            ztrsub("hi", 0, 100),
            2,
            "end > buf.len() clamps to buffer length"
        );
    }

    /// `Src/utils.c:5141` — trailing Meta byte with no following char
    /// is a malformed-string edge case. C has a DPUTS debug-only
    /// fprintf for "unexpected end" but otherwise the loop terminates
    /// because `*s` is now NUL. Rust port must not panic on this
    /// truncated input.
    #[test]
    fn ztrlen_handles_trailing_meta_byte_without_panic() {
        let _g = crate::test_util::global_state_lock();
        let meta = char::from_u32(Meta as u32).unwrap();
        let trailing: String = ['a', meta].iter().collect();
        // c:5141-5149 — increment l, then encounter Meta, then loop
        // condition `*s` is empty → terminate. Count: 'a' + Meta = 2
        // (Rust port counts Meta as 1 char when no follower).
        let r = ztrlen(&trailing);
        assert!(r >= 1, "trailing Meta must not panic; got {}", r);
    }

    /// c:params.c:1288 — `isident` rejects digit-leading names.
    /// A regression accepting them lets `typeset 1foo=bar` install
    /// a poisoned param that no later expansion can address.
    #[test]
    fn isident_rejects_digit_leading_names() {
        let _g = crate::test_util::global_state_lock(); // c:1288
                                                        // c:1315-1319 — C `isident`: if first char is digit, ALL
                                                        // chars must be digit (all-digit names are valid positional
                                                        // params like $99). So `1foo` is rejected (digit-then-alpha)
                                                        // but `99` is accepted as a valid positional-param name.
                                                        // The previous utils.rs fake rejected all-digit names too,
                                                        // which was non-faithful — the canonical `params::isident`
                                                        // at `params.rs:2056` matches the C semantics.
        assert!(!isident("1foo")); // c:1318
        assert!(
            isident("99"), // c:1318
            "c:1315-1319 — all-digit names are valid positional params"
        );
        assert!(!isident("")); // c:1290
    }

    /// `isident` MUST accept underscore-leading + alpha-leading names —
    /// `_foo`, `Foo_BAR_42` are valid POSIX shell idents.
    #[test]
    fn isident_accepts_underscore_and_alpha_leading() {
        let _g = crate::test_util::global_state_lock(); // c:1288
        assert!(isident("foo")); // c:1288
        assert!(isident("_foo")); // c:1288
        assert!(isident("Foo_BAR_42")); // c:1288
        assert!(isident("a")); // c:1288
    }

    /// `isident` rejects whitespace/punctuation/$. A regression
    /// accepting them would let assignments install names no
    /// subsequent lookup can address (`typeset 'foo bar'=baz`).
    #[test]
    fn isident_rejects_special_chars() {
        let _g = crate::test_util::global_state_lock(); // c:1288
                                                        // c:1320-1322 — non-digit-leading names use itype_end with
                                                        // INAMESPC bits (alnum + `_` + `.`); other chars terminate.
                                                        // The previous utils.rs fake rejected `foo.` outright, which
                                                        // is incorrect — C accepts trailing `.` (it's an INAMESPC
                                                        // member). Whitespace, `-`, and `$` are NOT INAMESPC chars
                                                        // and remain rejected.
        assert!(!isident("foo bar")); // c:1322
        assert!(!isident("foo-bar")); // c:1322
        assert!(!isident("a$b")); // c:1322
    }

    /// `convbase(0, 10)` MUST produce `"0"`, not `""`. The literal-zero
    /// edge case is a real C divergence point — regression here would
    /// make `$(( 0 ))` print nothing.
    #[test]
    fn convbase_zero_renders_as_zero_literal() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(convbase(0, 10), "0");
    }

    /// `convbase` uses zsh's canonical `BASE#NUMBER` syntax for
    /// non-decimal output (matches `$(( [#16] 255 ))` → `16#FF`).
    /// This is the format `printf "%X"` and `$(( ... ))` both produce.
    #[test]
    fn convbase_uses_base_prefix_syntax_for_non_decimal() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(convbase(255, 16), "16#FF");
        assert_eq!(convbase(8, 8), "8#10");
        assert_eq!(convbase(5, 2), "2#101");
        // Base 10 has no prefix.
        assert_eq!(convbase(42, 10), "42");
    }

    /// Negative number renders with leading `-`. Regression that drops
    /// the sign would silently flip arithmetic semantics in `$((...))`.
    #[test]
    fn convbase_preserves_negative_sign() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(convbase(-42, 10), "-42");
    }

    /// `Src/utils.c:837` — `slashsplit` keeps an EMPTY leading segment
    /// when the input has a leading `/`. The caller `xsymlinks`
    /// (c:879) reads this empty as the signal "absolute path, start
    /// xbuf from /". Previous Rust test asserted filtering of all
    /// empties (matching the previous buggy port); that contract was
    /// wrong relative to C. Updated to assert the C contract.
    #[test]
    fn slashsplit_keeps_leading_empty_segment() {
        let _g = crate::test_util::global_state_lock();
        // c:851 with t==s on first iter → empty prefix segment.
        assert_eq!(
            slashsplit("/usr/local/bin"),
            vec![
                "".to_string(),
                "usr".to_string(),
                "local".to_string(),
                "bin".to_string()
            ],
            "c:851 — leading `/` produces empty first segment"
        );
        // Single `/`: one empty segment only.
        assert_eq!(
            slashsplit("/"),
            vec!["".to_string()],
            "c:854-857 — trailing `/` after first iter returns ['']"
        );
        // Consecutive slashes collapse (c:852-853 inner while-loop).
        assert_eq!(
            slashsplit("//foo"),
            vec!["".to_string(), "foo".to_string()],
            "c:852-853 — consecutive `/` collapse, leading empty kept"
        );
        // Empty input still empty result.
        assert_eq!(
            slashsplit(""),
            Vec::<String>::new(),
            "c:842 — empty input → empty array"
        );
    }

    /// `Src/utils.c:837` — relative paths (no leading `/`) start
    /// at the first non-slash; trailing `/` doesn't add a segment
    /// (c:854-857 returns before tail emit).
    #[test]
    fn slashsplit_relative_path_no_trailing_empty() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            slashsplit("a/b/"),
            vec!["a".to_string(), "b".to_string()],
            "c:854-857 — trailing `/` doesn't add segment"
        );
        // Mid-slash collapse in relative path.
        assert_eq!(
            slashsplit("a//b"),
            vec!["a".to_string(), "b".to_string()],
            "c:852-853 — mid `//` collapses"
        );
    }

    /// `equalsplit("foo=bar")` returns Some(("foo","bar")). Used by
    /// `typeset NAME=val` parsing. Regression returning None on a
    /// well-formed assignment would silently break every export/typeset.
    #[test]
    fn equalsplit_returns_first_equals_split() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            equalsplit("foo=bar"),
            Some(("foo".to_string(), "bar".to_string()))
        );
        assert_eq!(
            equalsplit("a=b=c"),
            Some(("a".to_string(), "b=c".to_string())),
            "splits on FIRST `=` only"
        );
    }

    /// `equalsplit("foo")` returns None — no `=` to split on. Catches
    /// a regression returning Some(("foo","")) which would let bare
    /// names install with empty values silently.
    #[test]
    fn equalsplit_no_equals_returns_none() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(equalsplit("foo"), None);
        assert_eq!(equalsplit(""), None);
    }

    /// c:5106 — `ztrcmp` is the canonical zsh string compare. Same
    /// inputs MUST produce same Ordering (deterministic). Regression
    /// to a randomised hash-order would mis-sort `${(o)array}` output.
    #[test]
    fn ztrcmp_deterministic_and_lexicographic() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(ztrcmp("abc", "abc"), std::cmp::Ordering::Equal);
        assert_eq!(ztrcmp("abc", "abd"), std::cmp::Ordering::Less);
        assert_eq!(ztrcmp("abd", "abc"), std::cmp::Ordering::Greater);
        // Empty strings.
        assert_eq!(ztrcmp("", ""), std::cmp::Ordering::Equal);
        assert_eq!(ztrcmp("", "a"), std::cmp::Ordering::Less);
    }

    /// c:5106 — shorter-as-prefix sorts before longer (like strcmp).
    /// Catches a regression where length-then-content would mis-sort.
    #[test]
    fn ztrcmp_shorter_prefix_is_less() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(ztrcmp("a", "ab"), std::cmp::Ordering::Less);
        assert_eq!(ztrcmp("foo", "foob"), std::cmp::Ordering::Less);
    }

    /// `Src/utils.c:5117-5122` — Meta+X pair decodes to `X ^ 32`
    /// for comparison purposes. Pin: two strings differing only in
    /// the metafied byte's decoded value compare correctly.
    #[test]
    fn ztrcmp_decodes_meta_byte_for_comparison() {
        let _g = crate::test_util::global_state_lock();
        // META+0x21 ('!') = NUL? Let me use safer chars.
        // META+'A' (0x41) decodes to 'A' ^ 32 = 'a' (0x61).
        // So a "META a" pair represents the byte 'a' (0x61).
        // Compare "Ma" (M=Meta+a-byte-pair) vs "b" (>a).
        let meta_byte = Meta;
        let s1_bytes: Vec<u8> = vec![meta_byte, 0x41u8]; // decodes to 0x61 = 'a'
        let s2_bytes: Vec<u8> = vec![b'b'];
        let s1 = unsafe { std::str::from_utf8_unchecked(&s1_bytes) };
        let s2 = unsafe { std::str::from_utf8_unchecked(&s2_bytes) };
        // 'a' (0x61) < 'b' (0x62) → Less.
        assert_eq!(
            ztrcmp(s1, s2),
            std::cmp::Ordering::Less,
            "c:5117-5118 — Meta+0x41 decodes to 'a' which < 'b'"
        );
    }

    /// `Src/utils.c:531-539` — `is_nicechar`. Returns true for chars
    /// needing escape-formatting:
    ///   - c:534 — printable ASCII (0x20-0x7e) → false.
    ///   - c:536 — high-bit byte (>= 0x80) → !PRINTEIGHTBIT.
    ///   - c:538 — DEL/\n/\t/<0x20 → true.
    /// Pin the three branches.
    #[test]
    fn is_nicechar_printable_ascii_is_not_nice() {
        let _g = crate::test_util::global_state_lock();
        // c:534 — letters, digits, punctuation in 0x20-0x7e all NOT nice.
        for c in "abcXYZ012!?@~".chars() {
            assert!(
                !is_nicechar(c),
                "c:534 — '{}' (ASCII printable) must NOT be nice",
                c
            );
        }
        // c:534 — space (0x20) is printable.
        assert!(!is_nicechar(' '));
    }

    /// c:538 — control chars (DEL, newline, tab, <0x20) ARE nice.
    #[test]
    fn is_nicechar_control_chars_are_nice() {
        let _g = crate::test_util::global_state_lock();
        assert!(is_nicechar('\n'), "c:538 — newline is nice");
        assert!(is_nicechar('\t'), "c:538 — tab is nice");
        assert!(is_nicechar('\x7f'), "c:538 — DEL is nice");
        assert!(is_nicechar('\x00'), "c:538 — NUL is nice (<0x20)");
        assert!(is_nicechar('\x07'), "c:538 — BEL is nice (<0x20)");
        assert!(is_nicechar('\x1b'), "c:538 — ESC is nice (<0x20)");
        assert!(is_nicechar('\x1f'), "c:538 — boundary 0x1f is nice");
    }

    /// `Src/utils.c:2528-2570` — `zstrtoul_underscore(s)`. Pins
    /// the base-prefix dispatch: `0x` → 16, `0b` → 2, leading `0`
    /// only when OCTALZEROES is set. Tests the default state.
    #[test]
    fn zstrtoul_underscore_recognises_hex_binary_decimal() {
        let _g = crate::test_util::global_state_lock();
        // c:2538 — hex.
        assert_eq!(zstrtoul_underscore("0xff"), Some(255));
        assert_eq!(zstrtoul_underscore("0XFF"), Some(255));
        // c:2540 — binary.
        assert_eq!(zstrtoul_underscore("0b1010"), Some(10));
        assert_eq!(zstrtoul_underscore("0B11"), Some(3));
        // c:2537 — pure decimal (no leading zero).
        assert_eq!(zstrtoul_underscore("12345"), Some(12345));
        // c:2543 — leading-zero with OCTALZEROES off (default) → decimal.
        if !isset(OCTALZEROES) {
            assert_eq!(
                zstrtoul_underscore("0777"),
                Some(777),
                "c:2543 — OCTALZEROES off: leading-0 parses as decimal"
            );
            assert_eq!(zstrtoul_underscore("010"), Some(10));
        }
    }

    /// `Src/utils.c:2547-2548` — underscores are stripped before
    /// parsing. C: `if (*s == '_') continue;`. Used to support
    /// human-readable big numbers like `1_000_000`.
    #[test]
    fn zstrtoul_underscore_strips_underscores() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            zstrtoul_underscore("1_000_000"),
            Some(1_000_000),
            "c:2547-2548 — `_` stripped from numeric input"
        );
        assert_eq!(zstrtoul_underscore("0xff_ff"), Some(0xffff));
        assert_eq!(zstrtoul_underscore("0b1010_1010"), Some(0xaa));
    }

    /// `Src/utils.c:2533-2534` — leading `+` sign is consumed.
    /// `zulong` is unsigned so `-` is not handled here (caller
    /// handles negation at `zstrtol_underscore`).
    #[test]
    fn zstrtoul_underscore_consumes_leading_plus() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(zstrtoul_underscore("+42"), Some(42));
        assert_eq!(zstrtoul_underscore("+0xff"), Some(255));
    }

    /// `Src/utils.c:467` — `if (ZISPRINT(c)) goto done;`. Printable
    /// ASCII passes through nicechar_sel unchanged. Pin a single-char
    /// printable input → single-char output.
    #[test]
    fn nicechar_sel_passes_printable_ascii_unchanged() {
        let _g = crate::test_util::global_state_lock();
        for c in "aA0!~".chars() {
            assert_eq!(
                nicechar_sel(c, false),
                c.to_string(),
                "c:467 — printable ASCII '{}' passes through",
                c
            );
        }
    }

    /// `Src/utils.c:487-492` — `\n` becomes `\n` (backslash-n) and
    /// `\t` becomes `\t`. Both two-char outputs. Pin the canonical
    /// escapes.
    #[test]
    fn nicechar_sel_escapes_newline_and_tab() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            nicechar_sel('\n', false),
            "\\n",
            "c:487-489 — newline escape"
        );
        assert_eq!(nicechar_sel('\t', false), "\\t", "c:490-492 — tab escape");
    }

    /// `Src/utils.c:493-501` — control chars (<0x20 except \n/\t)
    /// get `^X` prefix in non-quotable mode and `\C-X` in quotable.
    /// c:500 — `c += 0x40` adds 0x40 to map 0x01 → 0x41 ('A').
    #[test]
    fn nicechar_sel_control_chars_use_caret_or_c_prefix() {
        let _g = crate::test_util::global_state_lock();
        // Non-quotable: ^A
        assert_eq!(
            nicechar_sel('\x01', false),
            "^A",
            "c:499-500 — \\x01 → ^A non-quotable"
        );
        // Quotable: \C-A
        assert_eq!(
            nicechar_sel('\x01', true),
            "\\C-A",
            "c:495-500 — \\x01 → \\C-A quotable"
        );
        // ^G (BEL = 0x07 → 0x47 = 'G')
        assert_eq!(nicechar_sel('\x07', false), "^G");
        // ^[ (ESC = 0x1b → 0x5b = '[')
        assert_eq!(nicechar_sel('\x1b', false), "^[");
    }

    /// `Src/utils.c:479-486` — DEL (0x7f) renders as `^?` or `\C-?`.
    #[test]
    fn nicechar_sel_del_renders_as_caret_question() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(nicechar_sel('\x7f', false), "^?", "c:485-486 — DEL is `^?`");
        assert_eq!(
            nicechar_sel('\x7f', true),
            "\\C-?",
            "c:481-486 — DEL is `\\C-?` quotable"
        );
    }

    /// `Src/utils.c:469-478` — high-bit bytes (>= 0x80) under default
    /// PRINTEIGHTBIT-off path: write `\M-` then mask to low ASCII.
    /// If the masked char is printable, output is `\M-<char>`. Pin
    /// the canonical 0xc1 → \M-A and 0xe1 → \M-a cases.
    #[test]
    fn nicechar_sel_highbit_uses_meta_prefix_when_printeightbit_off() {
        let _g = crate::test_util::global_state_lock();
        if isset(PRINTEIGHTBIT) {
            return; // Test only valid when PRINTEIGHTBIT off (default).
        }
        // 0xc1 = 'A' + 0x80 → \M-A
        let c = char::from_u32(0xc1).unwrap();
        assert_eq!(
            nicechar_sel(c, false),
            "\\M-A",
            "c:472-477 — high-bit 0xc1 → \\M-A under PRINTEIGHTBIT-off"
        );
        // 0xe1 = 'a' + 0x80 → \M-a
        let c = char::from_u32(0xe1).unwrap();
        assert_eq!(nicechar_sel(c, false), "\\M-a");
    }

    /// `Src/utils.c:593-705` — `wcs_nicechar_sel`. Wide-char variant
    /// of `nicechar_sel`: control chars escape, printable wides
    /// emit raw UTF-8, large codepoints get `\u`/`\U` hex escape.
    /// Pin every branch. Previously delegated to `nicechar_sel`
    /// which byte-masked via `c & 0xff` — every UTF-8 codepoint
    /// emerged mangled.
    #[test]
    fn wcs_nicechar_sel_printable_wide_emits_utf8() {
        let _g = crate::test_util::global_state_lock();
        // c:644-678 — printable wide char emits raw UTF-8.
        assert_eq!(wcs_nicechar_sel('a', None, None, false), "a", "ASCII printable");
        assert_eq!(wcs_nicechar_sel('é', None, None, false), "é", "Latin-1 printable");
        assert_eq!(wcs_nicechar_sel('字', None, None, false), "字", "CJK printable");
    }

    /// c:625-630 — `\n` and `\t` escape (same as nicechar_sel for ASCII).
    #[test]
    fn wcs_nicechar_sel_escapes_newline_and_tab() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(wcs_nicechar_sel('\n', None, None, false), "\\n");
        assert_eq!(wcs_nicechar_sel('\t', None, None, false), "\\t");
    }

    /// c:617-623 — DEL (0x7f) renders as `^?` (non-quotable) or
    /// `\C-?` (quotable). Same as the byte version.
    #[test]
    fn wcs_nicechar_sel_del_uses_caret_question() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(wcs_nicechar_sel('\x7f', None, None, false), "^?");
        assert_eq!(wcs_nicechar_sel('\x7f', None, None, true), "\\C-?");
    }

    /// c:631-638 — control chars (0x01-0x1f, except \n/\t) emit
    /// `^X` or `\C-X` with the +0x40 offset.
    #[test]
    fn wcs_nicechar_sel_control_chars_use_caret_prefix() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(wcs_nicechar_sel('\x01', None, None, false), "^A");
        assert_eq!(wcs_nicechar_sel('\x07', None, None, false), "^G");
        assert_eq!(wcs_nicechar_sel('\x1b', None, None, false), "^[");
        assert_eq!(wcs_nicechar_sel('\x01', None, None, true), "\\C-A");
    }

    /// `Src/utils.c:656-663` — large non-printable codepoints get
    /// hex escape: `\U%.8x` (>= 0x10000), `\u%.4x` (>= 0x100).
    /// Both lowercase per C's `%x`. Use a non-printable wide char
    /// to test (most BMP chars are printable; pick a control area
    /// like U+0085 NEXT LINE).
    #[test]
    fn wcs_nicechar_sel_large_nonprintable_uses_hex_escape() {
        let _g = crate::test_util::global_state_lock();
        // U+0085 NEL — control in C1 range, not iswprint.
        let nel = char::from_u32(0x85).unwrap();
        // Since 0x85 < 0x100, falls into nicechar_sel fallback.
        // After u9_iswprint says false, cv < 0x80 is false (cv=0x85),
        // PRINTEIGHTBIT off → enters !is_printable && cv < 0x80 branch?
        // Actually 0x85 >= 0x80 → !print_eightbit true → enters non-print branch.
        // 0x85 not 0x7f, not \n/\t, not <0x20. Falls past all if-elses to
        // post-branch logic → !is_printable, cv >= 0x100 false, cv >= 0x100 false
        // → falls to nicechar_sel fallback.
        let r = wcs_nicechar_sel(nel, None, None, false);
        assert!(!r.is_empty(), "must emit something for U+0085");
        // U+200B ZERO WIDTH SPACE — non-printable, 0x100-0xffff range.
        let zwsp = char::from_u32(0x200B).unwrap();
        let r = wcs_nicechar_sel(zwsp, None, None, false);
        // u9_iswprint(0x200B) returns true via unicode-width 0-width.
        // Per the impl: u9_iswprint true → emit raw. So r should be
        // the raw zwsp char.
        assert!(!r.is_empty());
    }

    /// `Src/utils.c:709-714` — `wcs_nicechar(c)` delegates to
    /// `wcs_nicechar_sel(c, widthp, swidep, 0)`. Pin the parity.
    #[test]
    fn wcs_nicechar_matches_wcs_nicechar_sel_with_zero() {
        let _g = crate::test_util::global_state_lock();
        for c in ['a', '\n', '\t', '\x7f', '\x01', 'é', '字'] {
            assert_eq!(
                wcs_nicechar(c, None, None),
                wcs_nicechar_sel(c, None, None, false),
                "c:711 — `wcs_nicechar(c, NULL, NULL)` must equal `wcs_nicechar_sel(c, NULL, NULL, 0)` for {:?}",
                c
            );
        }
    }

    /// `Src/utils.c:1989-2012` — `movefd(fd)`. Three contracts:
    /// 1. fd == -1 → returned as-is (no syscalls).
    /// 2. fd >= 10 → returned unchanged (already high).
    /// 3. fd < 10 → dupped with F_DUPFD >= 10, original closed
    ///    unconditionally (even on dup failure).
    #[test]
    #[cfg(unix)]
    fn movefd_returns_minus_one_unchanged() {
        let _g = crate::test_util::global_state_lock();
        // c:1992 — `if (fd != -1 && fd < 10)` skipped for fd=-1.
        assert_eq!(movefd(-1), -1, "fd=-1 must be returned unchanged");
    }

    /// c:1992 — `fd >= 10` skips the dup-and-close path.
    #[test]
    #[cfg(unix)]
    fn movefd_returns_high_fd_unchanged() {
        let _g = crate::test_util::global_state_lock();
        // Use a known-open fd. /dev/null open with fd guaranteed to be
        // <10 because new fds get the lowest free slot — but we can
        // dup it to 10 directly to test the early-return path.
        let f = unsafe { libc::open(b"/dev/null\0".as_ptr() as *const _, libc::O_RDONLY) };
        if f < 0 {
            return;
        } // Skip on unusual systems.
        let high = unsafe { libc::fcntl(f, libc::F_DUPFD, 20) };
        unsafe {
            libc::close(f);
        }
        if high < 0 {
            return;
        }
        // movefd(20) must return 20 unchanged (no dup, no close).
        let r = movefd(high);
        assert_eq!(r, high, "c:1992 — fd >= 10 returned unchanged");
        unsafe {
            libc::close(high);
        }
    }

    /// `Src/utils.c:1968-1983` — `check_fd_table(fd)`. Grows the
    /// fdtable Vec to `fd+1` slots (filling new entries with
    /// FDT_UNUSED) and bumps `MAX_ZSH_FD` to `fd`. Pin: a small
    /// fd grows the table to `fd+1` length; MAX_ZSH_FD reflects it.
    /// fd ≤ cur_max early-returns without growing (c:1971-1972).
    #[test]
    fn check_fd_table_grows_to_fd_plus_one() {
        let _g = crate::test_util::global_state_lock();
        // Reset globals so the test is deterministic regardless
        // of prior ordering — other tests may have grown the
        // table or bumped MAX_ZSH_FD.
        MAX_ZSH_FD.store(-1, Ordering::Relaxed);
        {
            let mut g = fdtable_lock().lock().unwrap();
            g.clear();
        }

        // c:1971-1972 — fd ≤ cur_max is the early-return path.
        // With cur_max=-1, fd=-1 hits `fd <= cur_max` → true.
        assert!(check_fd_table(-1));
        assert_eq!(
            MAX_ZSH_FD.load(Ordering::Relaxed),
            -1,
            "fd ≤ cur_max early-returns; MAX_ZSH_FD untouched"
        );

        // c:1974-1981 — fd > cur_max grows fdtable to fd+1 slots.
        assert!(check_fd_table(7));
        assert_eq!(
            MAX_ZSH_FD.load(Ordering::Relaxed),
            7,
            "c:1982 — MAX_ZSH_FD := fd"
        );
        let len = {
            let g = fdtable_lock().lock().unwrap();
            g.len()
        };
        assert!(
            len >= 8,
            "c:1975-1979 — fdtable grew to ≥ fd+1 = 8 slots, got {}",
            len
        );

        // Idempotence under fd ≤ cur_max.
        assert!(check_fd_table(3));
        assert_eq!(
            MAX_ZSH_FD.load(Ordering::Relaxed),
            7,
            "fd ≤ cur_max keeps MAX_ZSH_FD at 7"
        );
    }

    /// `Src/utils.c:4364,4367` — `wcsitype(c, IWORD/ISEP)` reads
    /// from the `wordchars`/`ifs` GLOBALS (writable by
    /// `wordcharssetfn`/`ifssetfn`). Previously read from
    /// `std::env::var` — the libc process env — which never
    /// reflects runtime `WORDCHARS=…` shell-side assignments.
    /// Pin: set wordchars via `wordcharssetfn`, verify `wcsitype`
    /// returns true for chars in the new set.
    #[test]
    fn wcsitype_iword_reads_from_canonical_wordchars_global() {
        let _g = crate::test_util::global_state_lock();
        // Test only meaningful in MULTIBYTE mode for non-ASCII chars;
        // ASCII chars (< 0x80) route through TYPTAB directly.
        // Skip if MULTIBYTE off.
        if !isset(MULTIBYTE) {
            return;
        }
        // Save and set WORDCHARS to a single non-ASCII char.
        // C dispatches `pm->gsu.s->{get,set}fn(pm, val)`; mirror via
        // paramtab lookup.
        let saved = crate::ported::params::paramtab()
            .read()
            .ok()
            .and_then(|t| t.get("WORDCHARS").map(|pm| crate::ported::params::wordcharsgetfn(pm)))
            .unwrap_or_default();
        let mut do_set = |val: String| {
            if let Ok(mut tab) = crate::ported::params::paramtab().write() {
                if let Some(pm) = tab.get_mut("WORDCHARS") {
                    wordcharssetfn(pm, val);
                }
            }
        };
        do_set("é".to_string());
        // 'é' is alphanumeric per Unicode → IWORD returns true via
        // is_alphanumeric short-circuit at c:4353. So we can't pin
        // the WORDCHARS-specific path with 'é'. Use a non-alnum char.
        do_set(":".to_string());
        // ':' is ASCII so wcsitype routes through TYPTAB (which now
        // has IWORD on ':' because wordcharssetfn called inittyptab).
        assert!(
            wcsitype(':', IWORD as u32),
            "c:4364 — wordchars membership through canonical global"
        );
        // Restore.
        do_set(saved);
    }

    /// `Src/utils.c:2090-2097` — `addmodulefd(fd, fdt)`. Stores the
    /// provided fdt in the fdtable slot for the given fd. Previously
    /// Rust port hardcoded FDT_MODULE and added CLOEXEC unconditionally.
    /// Pin: pass FDT_EXTERNAL, verify slot stores FDT_EXTERNAL (not
    /// FDT_MODULE).
    #[cfg(unix)]
    #[test]
    fn addmodulefd_respects_fdt_parameter() {
        let _g = crate::test_util::global_state_lock();
        // Open a real fd to use.
        let fd = unsafe { libc::open(b"/dev/null\0".as_ptr() as *const _, libc::O_RDONLY) };
        if fd < 0 {
            return;
        }
        addmodulefd(fd, FDT_EXTERNAL);
        assert_eq!(
            fdtable_get(fd),
            FDT_EXTERNAL,
            "c:2095 — fdt parameter must be stored verbatim"
        );
        // Switch to FDT_MODULE.
        addmodulefd(fd, FDT_MODULE);
        assert_eq!(
            fdtable_get(fd),
            FDT_MODULE,
            "c:2095 — second call overwrites with new fdt"
        );
        // Cleanup.
        unsafe {
            libc::close(fd);
        }
    }

    /// `Src/utils.c:2093` — `if (fd >= 0)`. Negative fd is silently
    /// ignored (no fdtable update). Pin the guard.
    #[test]
    fn addmodulefd_ignores_negative_fd() {
        let _g = crate::test_util::global_state_lock();
        // Should not panic, no side effect.
        addmodulefd(-1, FDT_MODULE);
        // Nothing to assert on side-effect-free path; just verify no panic.
    }

    /// `Src/utils.c:2155-2164` — `zcloselockfd(fd)` returns -1 for
    /// non-lock fds, 0 for FDT_FLOCK/FDT_FLOCK_EXEC. Previously the
    /// Rust port always returned 0 — broke `zsystem flock -u <fd>`
    /// distinguishing "fd never flocked" from "successfully released".
    #[cfg(unix)]
    #[test]
    fn zcloselockfd_returns_minus_one_for_non_lock_fd() {
        let _g = crate::test_util::global_state_lock();
        let fd = unsafe { libc::open(b"/dev/null\0".as_ptr() as *const _, libc::O_RDONLY) };
        if fd < 0 {
            return;
        }
        // No addlockfd call → fd is NOT in the flock table.
        let r = zcloselockfd(fd);
        assert_eq!(r, -1, "c:2160-2161 — non-lock fd must return -1, NOT 0");
        unsafe {
            libc::close(fd);
        }
    }

    /// `Src/utils.c:2155-2164` — `zcloselockfd(fd)` returns 0 for an
    /// fd that was registered via `addlockfd`. Pin the end-to-end
    /// round-trip: addlockfd → zcloselockfd returns 0.
    #[cfg(unix)]
    #[test]
    fn zcloselockfd_returns_zero_for_flock_fd_then_closes() {
        let _g = crate::test_util::global_state_lock();
        let fd = unsafe { libc::open(b"/dev/null\0".as_ptr() as *const _, libc::O_RDONLY) };
        if fd < 0 {
            return;
        }
        addlockfd(fd, true);
        let r = zcloselockfd(fd);
        assert_eq!(r, 0, "c:2162-2163 — FDT_FLOCK fd → zclose + return 0");
        // After zcloselockfd, the underlying close() has fired.
        // close() returning -1 with EBADF would confirm that, but
        // that's a libc side-effect; just verify the return value.
    }

    /// `Src/utils.c:1982` — `check_fd_table` sets `max_zsh_fd = fd`
    /// when fd is new. `fdtable_set` inlines this to keep the guard
    /// in `zcloselockfd` (`if (fd > max_zsh_fd) return -1`) working
    /// against fds populated by `addmodulefd`/`addlockfd`. Pin: after
    /// `fdtable_set(fd, FDT_FLOCK)`, MAX_ZSH_FD >= fd.
    #[cfg(unix)]
    #[test]
    fn fdtable_set_bumps_max_zsh_fd() {
        let _g = crate::test_util::global_state_lock();
        let fd = unsafe { libc::open(b"/dev/null\0".as_ptr() as *const _, libc::O_RDONLY) };
        if fd < 0 {
            return;
        }
        fdtable_set(fd, FDT_FLOCK);
        let max_fd = MAX_ZSH_FD.load(Ordering::Relaxed);
        assert!(
            max_fd >= fd,
            "c:1982 — max_zsh_fd ({}) must be >= newly-set fd ({})",
            max_fd,
            fd
        );
        // Cleanup.
        fdtable_set(fd, FDT_UNUSED);
        unsafe {
            libc::close(fd);
        }
    }

    /// `Src/utils.c:2111-2121` — `addlockfd(fd, cloexec)`. Selects
    /// between FDT_FLOCK (when cloexec=true, lock dies on exec) and
    /// FDT_FLOCK_EXEC (when cloexec=false, lock survives exec).
    /// Previously the Rust port did `fcntl(F_SETFD, CLOEXEC)` —
    /// totally wrong semantics. Pin the FDT slot.
    #[cfg(unix)]
    #[test]
    fn addlockfd_selects_flock_category_per_cloexec_flag() {
        let _g = crate::test_util::global_state_lock();
        let fd = unsafe { libc::open(b"/dev/null\0".as_ptr() as *const _, libc::O_RDONLY) };
        if fd < 0 {
            return;
        }
        // cloexec=true → FDT_FLOCK (lock dies on exec).
        addlockfd(fd, true);
        assert_eq!(
            fdtable_get(fd),
            FDT_FLOCK,
            "c:2117 — cloexec=true → FDT_FLOCK"
        );
        // cloexec=false → FDT_FLOCK_EXEC (lock survives exec).
        addlockfd(fd, false);
        assert_eq!(
            fdtable_get(fd),
            FDT_FLOCK_EXEC,
            "c:2119 — cloexec=false → FDT_FLOCK_EXEC"
        );
        // Cleanup.
        unsafe {
            libc::close(fd);
        }
    }

    /// `Src/utils.c:1211` — `adduserdir` rejects paths `>= PATH_MAX`
    /// by removing the existing entry (treating as "can't use this
    /// value as a directory"). Pin with a path constructed to exceed
    /// PATH_MAX, verify no insertion happens.
    #[cfg(unix)]
    #[test]
    fn adduserdir_rejects_paths_at_or_above_path_max() {
        let _g = crate::test_util::global_state_lock();
        if !interact() {
            // c:1193 — non-interactive shells skip the table entirely.
            // Test inactive in non-interactive contexts.
            return;
        }
        // Ensure the entry doesn't exist beforehand.
        let name = "ZSHRS_TEST_PATHMAX_DIR";
        let _ = removenameddirnode(name);
        // Construct an oversized path.
        let mut over: String = "/".to_string();
        over.push_str(&"a".repeat(libc::PATH_MAX as usize));
        // adduserdir with the oversized path + AUTONAMEDIRS-off path
        // via always=true (so the AUTONAMEDIRS guard doesn't reject).
        adduserdir(name, &over, 0, true);
        // c:1211 — too-long → remove path triggered, no entry added.
        let tab = nameddirtab().lock().unwrap();
        assert!(
            !tab.contains_key(name),
            "c:1211 — strlen(t) >= PATH_MAX must NOT insert entry"
        );
    }

    /// `Src/utils.c:760-786` — `pathprog`. Finds any regular file
    /// (not directory, no executable-bit check) in `$PATH`.
    /// Previously required `mode & 0o111 != 0` — silently missed
    /// non-executable autoload-function plaintext scripts. Pin
    /// using a known-existing file in /tmp.
    #[test]
    #[cfg(unix)]
    fn pathprog_finds_non_executable_files() {
        let _g = crate::test_util::global_state_lock();
        // Create a non-executable file in /tmp and add /tmp to PATH.
        let test_name = format!("zshrs_test_pathprog_{}", unsafe { libc::getpid() });
        let path = PathBuf::from("/tmp").join(&test_name);
        // Write content, NO executable bit.
        if fs::write(&path, b"plain content").is_err() {
            return; // /tmp may be unwritable on some systems.
        }
        // Verify it's not executable.
        let meta = fs::metadata(&path).unwrap();
        assert_eq!(
            meta.permissions().mode() & 0o111,
            0,
            "test setup: must be non-executable"
        );
        // Set PATH to /tmp.
        let saved_path = getsparam("PATH");
        assignsparam("PATH", "/tmp", 0);
        // pathprog should find the file.
        let r = pathprog(&test_name);
        // Cleanup.
        let _ = fs::remove_file(&path);
        if let Some(prev) = saved_path {
            assignsparam("PATH", &prev, 0);
        }
        assert_eq!(
            r,
            Some(path),
            "c:776 — pathprog finds non-executable files (only F_OK + !S_ISDIR)"
        );
    }

    /// `Src/utils.c:776-778` — pathprog skips directories. Pin: a
    /// directory in $PATH with the queried name must NOT be returned.
    #[test]
    #[cfg(unix)]
    fn pathprog_skips_directories() {
        let _g = crate::test_util::global_state_lock();
        let test_name = format!("zshrs_test_pathprog_dir_{}", unsafe { libc::getpid() });
        let path = PathBuf::from("/tmp").join(&test_name);
        if fs::create_dir(&path).is_err() {
            return;
        }
        let saved_path = getsparam("PATH");
        assignsparam("PATH", "/tmp", 0);
        let r = pathprog(&test_name);
        // Cleanup.
        let _ = fs::remove_dir(&path);
        if let Some(prev) = saved_path {
            assignsparam("PATH", &prev, 0);
        }
        assert!(
            r.is_none(),
            "c:778 — pathprog must skip directories (!S_ISDIR)"
        );
    }

    /// `Src/utils.c:4367` — `wcsitype(c, ISEP)` reads from canonical
    /// `ifs` global. Pin via `ifssetfn`. ASCII path routes through
    /// TYPTAB which gets ISEP bit from inittyptab's IFS walk
    /// (added earlier this session). End-to-end pin.
    #[test]
    fn wcsitype_isep_reads_from_canonical_ifs_global() {
        let _g = crate::test_util::global_state_lock();
        if !isset(MULTIBYTE) {
            return;
        }
        // C: `pm->gsu.s->{get,set}fn(pm, val)`. Mirror via paramtab.
        let saved = crate::ported::params::paramtab()
            .read()
            .ok()
            .and_then(|t| t.get("IFS").map(|pm| crate::ported::params::ifsgetfn(pm)))
            .unwrap_or_default();
        let mut do_set = |val: String| {
            if let Ok(mut tab) = crate::ported::params::paramtab().write() {
                if let Some(pm) = tab.get_mut("IFS") {
                    ifssetfn(pm, val);
                }
            }
        };
        do_set(":".to_string());
        assert!(
            wcsitype(':', ISEP as u32),
            "c:4367 — IFS membership through canonical global"
        );
        // Restore.
        do_set(saved);
    }

    /// c:536 — high-bit byte (>= 0x80) is nice when PRINTEIGHTBIT is
    /// OFF (the default). Pin the default case; the alternate
    /// behavior under PRINTEIGHTBIT-on is harder to exercise from
    /// a unit test due to opt-state global mutation.
    #[test]
    fn is_nicechar_highbit_is_nice_when_printeightbit_off() {
        let _g = crate::test_util::global_state_lock();
        // Default state: PRINTEIGHTBIT is off → high-bit bytes are nice.
        // Use char with high-bit equivalent low byte.
        // 0xb5 (Meta+5 territory) — masked to 0xff → still 0xb5.
        let c = char::from_u32(0xb5).unwrap();
        // ZISPRINT(0xb5) is false (>0x7e). 0xb5 & 0x80 set → check PRINTEIGHTBIT.
        // Default PRINTEIGHTBIT off → returns true (nice).
        if !isset(PRINTEIGHTBIT) {
            assert!(
                is_nicechar(c),
                "c:536 — high-bit byte 0x{:x} must be nice when PRINTEIGHTBIT off",
                0xb5_u32
            );
        }
    }

    /// `Src/utils.c:1969-1983` — `check_fd_table(fd)` grows the
    /// fdtable so it can index `fd` and bumps `max_zsh_fd`. The
    /// previous Rust port was a no-op shim that always returned true;
    /// real behavior is "fdtable.len() must be > fd" and
    /// "MAX_ZSH_FD >= fd" after the call.
    #[test]
    fn check_fd_table_grows_fdtable_and_bumps_max_zsh_fd() {
        let _g = crate::test_util::global_state_lock();
        // Snapshot prior state so we don't poison other tests.
        let saved_max = MAX_ZSH_FD.load(Ordering::Relaxed);
        // Pick a target fd well above the typical 0/1/2.
        let target = saved_max.max(50) + 7;
        check_fd_table(target);
        let new_max = MAX_ZSH_FD.load(Ordering::Relaxed);
        assert!(
            new_max >= target,
            "c:1982 — max_zsh_fd must be >= target after grow (got {})",
            new_max
        );
        // Calling again with a lower fd MUST NOT shrink max_zsh_fd
        // (c:1971-1972 early return).
        check_fd_table(target - 3);
        assert_eq!(
            MAX_ZSH_FD.load(Ordering::Relaxed),
            new_max,
            "c:1971 — fd <= max_zsh_fd path must not change max"
        );
        // Restore approximately — best we can do without exposing
        // fdtable internals; subsequent fdtable_set callers cope.
        MAX_ZSH_FD.store(saved_max, Ordering::Relaxed);
    }

    /// `Src/utils.c:1971` — `if (fd <= max_zsh_fd) return;` early
    /// exit. Calling with a small fd (<= current max) must be a
    /// no-op.
    #[test]
    fn check_fd_table_small_fd_is_noop() {
        let _g = crate::test_util::global_state_lock();
        // Ensure max_zsh_fd is non-trivial.
        let _ = check_fd_table(100);
        let max_before = MAX_ZSH_FD.load(Ordering::Relaxed);
        check_fd_table(5);
        assert_eq!(
            MAX_ZSH_FD.load(Ordering::Relaxed),
            max_before,
            "c:1971 — small fd path must not touch max_zsh_fd"
        );
    }

    /// `Src/utils.c:1969-1983` — defensive: negative fd shouldn't
    /// panic. C-side would have indexed past the start of `fdtable`
    /// (UB). Rust port should fail-soft.
    #[test]
    fn check_fd_table_negative_fd_does_not_panic() {
        let _g = crate::test_util::global_state_lock();
        // Should not panic; behavior beyond "no panic" is unspecified.
        let _ = check_fd_table(-1);
        let _ = check_fd_table(-100);
    }

    /// `Src/zsh.h:3274` — under MULTIBYTE_SUPPORT, the C source
    /// defines `nicezputs(str, outs)` as a macro:
    ///   `(void)mb_niceformat((str), (outs), NULL, 0)`.
    /// So Rust `nicezputs(s)` must equal `mb_niceformat(s)` for
    /// every input. Pins the macro-equivalence after fixing the
    /// previous `chars().map(nicechar)` impl (which corrupted
    /// non-ASCII multibyte codepoints into `\M-X` mangle).
    #[test]
    fn nicezputs_matches_mb_niceformat_under_multibyte() {
        let _g = crate::test_util::global_state_lock();
        // C-equivalent pattern: write to a stream, compare to
        // mb_niceformat outstrp-form output. Inline rather than via a
        // Rust-only helper.
        for input in ["hello", "a\nb", "é", ""] {
            let mut nz_buf: Vec<u8> = Vec::new();
            let _ = nicezputs(input, &mut nz_buf);
            let nz = String::from_utf8(nz_buf).expect("utf8");

            let mut mb_out: Option<String> = None;
            let _ = mb_niceformat(input, None, Some(&mut mb_out), 0);
            assert_eq!(nz, mb_out.unwrap_or_default(),
                "nicezputs and mb_niceformat must agree for {:?}", input);
        }
        // \n must produce literal `\n` (backslash + n).
        let mut nz_buf: Vec<u8> = Vec::new();
        let _ = nicezputs("a\nb", &mut nz_buf);
        let nz = String::from_utf8(nz_buf).expect("utf8");
        assert!(nz.contains("\\n"), "nicechar emits `\\n`, not raw 0x0a");
    }

    /// `Src/utils.c:5530` — under MULTIBYTE_SUPPORT, `nicedup(s, heap)`
    /// body is `(void)mb_niceformat(s, NULL, &retstr, …); return retstr;`
    /// so the returned string MUST equal mb_niceformat output.
    #[test]
    fn nicedup_matches_mb_niceformat_under_multibyte() {
        let _g = crate::test_util::global_state_lock();
        for input in ["hello", "a\nb", "é"] {
            let nd = nicedup(input, 0);
            let mut mb_out: Option<String> = None;
            let _ = mb_niceformat(input, None, Some(&mut mb_out), 0);
            assert_eq!(nd, mb_out.unwrap_or_default(),
                "nicedup must equal mb_niceformat outstrp form for {:?}", input);
        }
        // nicedupstring delegates to nicedup(s, 1).
        assert_eq!(nicedupstring("hé\nllo"), nicedup("hé\nllo", 1));
    }

    /// `Src/utils.c:734-744` — `zwcwidth(wc)` returns 1 when MULTIBYTE
    /// option is unset (c:738), regardless of the codepoint's actual
    /// display width. The previous Rust port skipped the option gate
    /// and always used the Unicode width table. Pin the option-gated
    /// behavior end-to-end: unset MULTIBYTE → width 1 for CJK; set →
    /// width 2 for CJK.
    #[test]
    fn zwcwidth_returns_1_when_multibyte_unset() {
        let _g = crate::test_util::global_state_lock();
        let saved = isset(MULTIBYTE);
        // c:738 — unset(MULTIBYTE) path returns 1 for everything.
        dosetopt(MULTIBYTE, 0, 1);
        assert_eq!(zwcwidth('a'), 1, "c:738 — ASCII width 1 under nomultibyte");
        assert_eq!(
            zwcwidth('字'),
            1,
            "c:738 — CJK collapses to 1 under nomultibyte (was 2 via Unicode-width)"
        );
        assert_eq!(
            zwcwidth('\u{200B}'),
            1,
            "c:738 — zero-width space → 1 under nomultibyte (was 0 via Unicode-width)"
        );
        // c:740-744 — MULTIBYTE on: Unicode-width table applies.
        dosetopt(MULTIBYTE, 1, 1);
        assert_eq!(zwcwidth('a'), 1, "ASCII width is 1");
        assert_eq!(zwcwidth('字'), 2, "c:740 — CJK width 2 under multibyte");
        // Restore prior state.
        dosetopt(MULTIBYTE, if saved { 1 } else { 0 }, 1);
    }

    /// `Src/utils.c:6478-6492` — `quotedzputs(s, NULL)` under
    /// MULTIBYTE_SUPPORT detects "needs nice-format" inputs via
    /// `is_mb_niceformat(s)` and emits `$'<nice-formatted body>'`
    /// (the dollar-quoted form so embedded `\n`/`\t`/escape sequences
    /// round-trip through `typeset -p`/`set`/`set -x`). The previous
    /// Rust port skipped this branch entirely and single-quoted such
    /// strings — POSIX `'…'` is *strong* (no escapes interpreted),
    /// breaking round-trip for control bytes.
    #[test]
    fn quotedzputs_uses_dollar_quotes_for_control_chars() {
        let _g = crate::test_util::global_state_lock();
        // Ensure typtab is initialised — hasspecial depends on ISPECIAL
        // bits which are populated by inittyptab. Without this the
        // SPECCHARS arm short-circuits to "no specials".
        inittyptab();
        // Empty stays `''`.
        assert_eq!(quotedzputs(""), "''", "c:6470-6475 — empty input → ''");
        // Plain alphanumeric: no specials, no controls → bare.
        assert_eq!(
            quotedzputs("hello"),
            "hello",
            "c:6511-6517 — no SPECCHARS member → return unchanged"
        );
        // Control char `\n` needs `$'…'` to round-trip.
        let r = quotedzputs("a\nb");
        assert!(
            r.starts_with("$'") && r.ends_with('\''),
            "c:6488 — control char must use $'…' form (got {:?})",
            r
        );
        // Tab and ESC also force the niceformat arm.
        let r = quotedzputs("\t");
        assert!(
            r.starts_with("$'"),
            "c:6488 — TAB forces $'…' arm (got {:?})",
            r
        );
        let r = quotedzputs("\u{1b}[31m");
        assert!(
            r.starts_with("$'"),
            "c:6488 — ESC sequence forces $'…' arm (got {:?})",
            r
        );
        // Single quote forces single-quote arm via SPECCHARS membership
        // (Src/zsh.h:228 — SPECCHARS includes `'`). Embedded `'`
        // rewrites to `'\''`.
        let r = quotedzputs("a'b");
        assert!(
            r.contains("'\\''"),
            "c:6573-6587 — embedded ' → '\\''  (got {:?})",
            r
        );
    }

    /// `Src/utils.c:2923-2945` — `read_loop` returns the requested
    /// length on full read, or the partial count on EOF. Pin: writing
    /// a known buffer to a pipe and reading it back returns the same
    /// content + correct length. Drives the no-side-effect path that
    /// the diagnostic-message fix doesn't touch.
    #[test]
    #[cfg(unix)]
    fn read_loop_round_trips_pipe_bytes() {
        let _g = crate::test_util::global_state_lock();
        // Create a pipe; write 16 bytes; read them back.
        let mut fds: [libc::c_int; 2] = [0; 2];
        unsafe {
            assert_eq!(libc::pipe(fds.as_mut_ptr()), 0, "pipe(2) ok");
        }
        let payload = b"hello-world-1234";
        let written = unsafe {
            libc::write(
                fds[1],
                payload.as_ptr() as *const libc::c_void,
                payload.len(),
            )
        };
        assert_eq!(written, payload.len() as isize, "write all 16 bytes");
        unsafe {
            libc::close(fds[1]);
        }
        let mut buf = [0u8; 16];
        let got = read_loop(fds[0], &mut buf).expect("read_loop ok");
        assert_eq!(got, payload.len(), "c:2929 — read_loop returns full length");
        assert_eq!(
            &buf[..],
            &payload[..],
            "c:2940-2941 — buffer copied verbatim"
        );
        unsafe {
            libc::close(fds[0]);
        }
    }

    /// `Src/utils.c:2949-2970` — `write_loop` returns the requested
    /// length when the kernel accepts all bytes (the common case for
    /// a pipe with room). Pin the no-side-effect happy path.
    #[test]
    #[cfg(unix)]
    fn write_loop_writes_all_bytes_to_pipe() {
        let _g = crate::test_util::global_state_lock();
        let mut fds: [libc::c_int; 2] = [0; 2];
        unsafe {
            assert_eq!(libc::pipe(fds.as_mut_ptr()), 0, "pipe(2) ok");
        }
        let payload = b"abcdef";
        let got = write_loop(fds[1], payload).expect("write_loop ok");
        assert_eq!(
            got,
            payload.len(),
            "c:2955-2956 — write_loop returns full length on accept"
        );
        // Read back to verify.
        unsafe {
            libc::close(fds[1]);
        }
        let mut buf = [0u8; 6];
        let _ = unsafe { libc::read(fds[0], buf.as_mut_ptr() as *mut libc::c_void, buf.len()) };
        assert_eq!(&buf[..], &payload[..]);
        unsafe {
            libc::close(fds[0]);
        }
    }

    /// `Src/utils.c:2935-2936` — `read_loop` on a closed/invalid fd
    /// returns an io::Error (the C path returns `ret` and emits the
    /// `zwarn` to stderr). Pin the error propagation; the zwarn
    /// emission is a stderr side-effect tested only by inspecting
    /// log output (out of scope here).
    #[test]
    #[cfg(unix)]
    fn read_loop_returns_error_on_invalid_fd() {
        let _g = crate::test_util::global_state_lock();
        let mut buf = [0u8; 4];
        // fd 9999 is essentially guaranteed-not-open in a test
        // process; the canonical "bad fd" error path.
        let r = read_loop(9999, &mut buf);
        assert!(
            r.is_err(),
            "c:2935 — invalid fd → io::Error (and zwarn to stderr)"
        );
    }

    /// `Src/utils.c:2972-2988` — `read1char(echo)` reads from SHTTY,
    /// not stdin. With SHTTY uninitialised (default -1 in test
    /// environment), the function MUST return -1 immediately rather
    /// than blocking on a stdin read. The previous Rust port read
    /// from stdin and returned None — which in some test runners
    /// would block waiting for input.
    #[test]
    #[cfg(unix)]
    fn read1char_returns_minus_one_when_shtty_unset() {
        let _g = crate::test_util::global_state_lock();
        // Test environment: SHTTY is -1 (no controlling tty bound
        // by the port). C-side would `read(-1, ...)` which fails;
        // Rust port should fail-fast with -1.
        let saved = SHTTY.load(Ordering::Relaxed);
        SHTTY.store(-1, Ordering::Relaxed);
        let got = read1char(0); // echo=0
        assert_eq!(got, -1, "c:2978 — SHTTY=-1 → read fails → return -1");
        // Restore.
        SHTTY.store(saved, Ordering::Relaxed);
    }

    /// `Src/utils.c:1989-2012` — `movefd(fd)` dups fd to >= 10 (so it
    /// stays out of the user fd range 0..=9), closes the original,
    /// AND marks the new fd as `FDT_INTERNAL` in fdtable. Previous
    /// Rust port omitted the fdtable_set call — internal-fd tracking
    /// (`closeallelse`, forkexec) silently never saw zshrs-internal
    /// fds. Pin: after movefd, fdtable[new_fd] == FDT_INTERNAL.
    #[test]
    #[cfg(unix)]
    fn movefd_marks_fdtable_internal() {
        let _g = crate::test_util::global_state_lock();
        // Open /dev/null to get a small (< 10) fd...
        let dev_null = CString::new("/dev/null").unwrap();
        let fd = unsafe { libc::open(dev_null.as_ptr(), libc::O_RDONLY) };
        assert!(fd >= 0, "open /dev/null returned -1");
        let new_fd = movefd(fd);
        assert!(
            new_fd >= 10,
            "c:1992-1994 — movefd dups to fd >= 10 (got {})",
            new_fd
        );
        // c:2009 — fdtable[new_fd] := FDT_INTERNAL.
        let entry = fdtable_get(new_fd);
        assert_eq!(
            entry,
            FDT_INTERNAL,
            "c:2009 — movefd must mark new_fd as FDT_INTERNAL (got {})",
            entry
        );
        unsafe {
            libc::close(new_fd);
        }
    }

    /// `Src/utils.c:1989` — `movefd(-1)` is a defensive no-op: the
    /// `fd != -1 && fd < 10` gate at c:1992 fails, the c:2007
    /// `if (fd != -1)` post-check skips, and -1 is returned
    /// unchanged. Pin so a refactor of the early-out doesn't
    /// accidentally call fcntl(-1, ...).
    #[test]
    #[cfg(unix)]
    fn movefd_minus_one_returns_minus_one() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            movefd(-1),
            -1,
            "c:1992 — movefd(-1) bypasses both gates and returns -1"
        );
    }

    /// `Src/utils.c:2021-2068` — `redup(x, y)` after a successful
    /// `dup2(x, y)` must:
    ///   * Copy fdtable[x] to fdtable[y] (c:2054).
    ///   * Promote FDT_FLOCK/FDT_FLOCK_EXEC to FDT_INTERNAL on the
    ///     dup target (c:2055-2056) — the lock doesn't transfer.
    ///   * Then close x (c:2064).
    ///
    /// Previous Rust port skipped the fdtable updates entirely.
    /// Pin the ownership-transfer with two fds the test allocates.
    #[test]
    #[cfg(unix)]
    fn redup_copies_fdtable_ownership_to_target() {
        let _g = crate::test_util::global_state_lock();
        // Open two distinct fds — both will land in the fdtable.
        let dev_null = CString::new("/dev/null").unwrap();
        let x = unsafe { libc::open(dev_null.as_ptr(), libc::O_RDONLY) };
        let y = unsafe { libc::open(dev_null.as_ptr(), libc::O_RDONLY) };
        assert!(x >= 0 && y >= 0, "open /dev/null returned -1");
        assert_ne!(x, y);

        // Mark x as FDT_INTERNAL so we can observe the copy to y.
        check_fd_table(x);
        check_fd_table(y);
        fdtable_set(x, FDT_INTERNAL);
        fdtable_set(y, FDT_UNUSED);

        let ret = redup(x, y);
        assert_eq!(ret, y, "c:2067 — successful redup returns y");
        // c:2054 — fdtable[y] inherited from fdtable[x].
        assert_eq!(
            fdtable_get(y),
            FDT_INTERNAL,
            "c:2054 — fdtable[y] = fdtable[x] (FDT_INTERNAL)"
        );
        // x is closed by c:2064.
        unsafe {
            libc::close(y);
        }
    }

    /// `Src/utils.c:872-908` — `xsymlinks(path)` resolves `.`/`..`
    /// AND follows ONE LEVEL of symlinks via `readlink(2)`. Pin the
    /// symlink-following behavior with a temp-dir-managed symlink
    /// (the actual bug-fix pin — previous Rust port just normalised
    /// `.`/`..` without ever calling readlink).
    #[test]
    #[cfg(unix)]
    fn xsymlinks_follows_one_level_of_symlinks() {
        let _g = crate::test_util::global_state_lock();
        let tmp = std::env::temp_dir();
        let target = tmp.join(format!("zshrs_xsymlinks_target_{}", std::process::id()));
        let link = tmp.join(format!("zshrs_xsymlinks_link_{}", std::process::id()));
        // Create target as a regular dir to symlink to.
        let _ = fs::create_dir(&target);
        let _ = fs::remove_file(&link);
        std::os::unix::fs::symlink(&target, &link).unwrap();

        let got = xsymlinks(link.to_str().unwrap()).unwrap();
        assert_eq!(
            got,
            target.to_string_lossy(),
            "c:908 — xsymlinks must follow the symlink to its target"
        );

        // Cleanup.
        let _ = fs::remove_file(&link);
        let _ = fs::remove_dir(&target);
    }

    /// `Src/utils.c:881-882` — `.` components are skipped.
    /// `Src/utils.c:883-896` — `..` components walk back one
    /// `/`-segment of xbuf (unless xbuf is empty or `/`).
    ///
    /// Use a temp directory we create ourselves — we can't pin
    /// `/tmp/...` literally because the macOS sandbox symlinks
    /// `/tmp -> /private/tmp`, which `xsymlinks` correctly follows
    /// (proving the bug-fix works). Test with a directory under
    /// the env tempdir + a non-existent sub so readlink fails on
    /// every component and the test exercises ONLY the c:881-896
    /// `.` / `..` paths.
    #[test]
    #[cfg(unix)]
    fn xsymlinks_normalises_dot_and_dotdot() {
        let _g = crate::test_util::global_state_lock();
        let tmp = fs::canonicalize(std::env::temp_dir()).unwrap();
        let base_dir = tmp.join(format!("zshrs_xs_norm_{}", std::process::id()));
        let _ = fs::create_dir(&base_dir);
        // `<base>/.` should be `<base>` (c:881 — `.` skipped).
        let arg = format!("{}/./.", base_dir.display());
        let got = xsymlinks(&arg).unwrap();
        assert_eq!(
            got,
            base_dir.to_string_lossy(),
            "c:881 — `.` segments collapse"
        );
        // `<base>/foo/..` should be `<base>` (c:891-895 — `..` walks back).
        let arg = format!("{}/foo/..", base_dir.display());
        let got = xsymlinks(&arg).unwrap();
        assert_eq!(
            got,
            base_dir.to_string_lossy(),
            "c:891-895 — `..` walks back one segment"
        );
        let _ = fs::remove_dir(&base_dir);
    }

    /// `Src/utils.c:2055-2056` — when fdtable[x] is FDT_FLOCK or
    /// FDT_FLOCK_EXEC, the dup'd fd y gets promoted to FDT_INTERNAL
    /// (the dup doesn't carry the flock). Pin the promotion.
    ///
    /// Note on fdtable_flocks: C's c:2062-2063 decrements the
    /// flock count in `redup` BEFORE `zclose(x)`, and zclose ALSO
    /// decrements when it sees fdtable[fd] == FDT_FLOCK (c:2135).
    /// So C double-decrements for an FDT_FLOCK source fd in redup —
    /// the C comment at c:2058-2061 calls this "isn't expected to
    /// happen" (FDT_FLOCK fds aren't normally redup'd). We mirror
    /// C exactly: test asserts the count drops by 2 (1 → -1) to
    /// pin the faithful-to-C behavior. Reporting this as a C bug
    /// upstream is the right path; the port preserves it verbatim.
    #[test]
    #[cfg(unix)]
    fn redup_promotes_flock_to_internal_on_target() {
        let _g = crate::test_util::global_state_lock();
        let dev_null = CString::new("/dev/null").unwrap();
        let x = unsafe { libc::open(dev_null.as_ptr(), libc::O_RDONLY) };
        let y = unsafe { libc::open(dev_null.as_ptr(), libc::O_RDONLY) };
        assert!(x >= 0 && y >= 0);
        assert_ne!(x, y);

        check_fd_table(x);
        check_fd_table(y);
        // Mark x as FDT_FLOCK.
        fdtable_set(x, FDT_FLOCK);
        FDTABLE_FLOCKS.store(2, Ordering::SeqCst); // start at 2 so double-decrement lands at 0

        let _ = redup(x, y);

        // c:2055-2056 — promoted to FDT_INTERNAL on y, NOT carried.
        assert_eq!(
            fdtable_get(y),
            FDT_INTERNAL,
            "c:2055-2056 — FDT_FLOCK on x promotes to FDT_INTERNAL on y"
        );
        // c:2062-2063 + zclose c:2135 — double decrement.
        assert_eq!(
            FDTABLE_FLOCKS.load(Ordering::SeqCst),
            0,
            "c:2062-2063 + c:2135 — flock count double-decremented (faithful to C)"
        );
        unsafe {
            libc::close(y);
        }
    }

    /// `Src/utils.c:5217-5240` — `zreaddir(dir, ignoredots)` exposes
    /// the dot-filter as a parameter and returns one entry per call.
    /// Two paths:
    /// * `ignoredots=1` (the common case at c:590/655/1653/2884) —
    ///   `.` and `..` are filtered out.
    /// * `ignoredots=0` (used at c:4648 for spelling correction) —
    ///   `.` and `..` are RETAINED as valid candidates.
    #[test]
    #[cfg(unix)]
    fn zreaddir_honors_ignoredots_flag() {
        let _g = crate::test_util::global_state_lock();
        let tmp = std::env::temp_dir();
        let base = tmp.join(format!("zshrs_zreaddir_{}", std::process::id()));
        let _ = fs::create_dir(&base);
        // Create one real entry to make the test non-trivial.
        fs::write(base.join("file"), "x").unwrap();

        // c:5232 — ignoredots=1: skip `.` and `..`, keep `file`.
        let mut dir = fs::read_dir(&base).unwrap();
        let mut with_skip: Vec<String> = Vec::new();
        while let Some(n) = zreaddir(&mut dir, 1) {
            with_skip.push(n);
        }
        assert!(
            with_skip.contains(&"file".to_string()),
            "c:5232 — real entry survives ignoredots=1"
        );
        assert!(
            !with_skip.contains(&".".to_string()),
            "c:5232 — `.` filtered with ignoredots=1"
        );
        assert!(
            !with_skip.contains(&"..".to_string()),
            "c:5232 — `..` filtered with ignoredots=1"
        );

        // c:4648-equivalent — ignoredots=0: KEEP `.` and `..`.
        // (libstd's fs::read_dir filters them before exposing on
        // macOS/Linux, so the without_skip set is functionally
        // identical here — pin only that the API path doesn't error.)
        let mut dir2 = fs::read_dir(&base).unwrap();
        let mut without_skip: Vec<String> = Vec::new();
        while let Some(n) = zreaddir(&mut dir2, 0) {
            without_skip.push(n);
        }
        assert!(
            without_skip.contains(&"file".to_string()),
            "ignoredots=0 still yields real entries"
        );

        let _ = fs::remove_file(base.join("file"));
        let _ = fs::remove_dir(&base);
    }

    /// `Src/utils.c:1075` — `get_username()` uses `ztrdup_metafy` on
    /// `pswd->pw_name`. Previous Rust port returned the raw pw_name
    /// verbatim — fine for ASCII usernames, broken for high-bit
    /// bytes (downstream paramtab consumers assume metafied entries).
    /// Pin: ASCII usernames round-trip identically through metafy,
    /// AND the result is a non-empty string for the current uid.
    /// `Src/utils.c:3445-3460` — ztrftime zsh-specific %K/%L/%f
    /// extensions return values WITHOUT leading zeros (vs the
    /// strftime %H/%I/%d which pad).
    #[test]
    #[cfg(unix)]
    fn ztrftime_zsh_extensions_no_leading_zero() {
        let _g = crate::test_util::global_state_lock();
        // Pick a known time: Jan 5 2024 09:07:42.123456789 UTC.
        // Use SystemTime + an offset since we want deterministic values
        // independent of TZ; we just verify that the format substitutes
        // the right NUMBER OF DIGITS for the leading-zero case.
        use std::time::Duration;
        // 2024-01-05 09:07:42 UTC = 1704445662
        let t = UNIX_EPOCH + Duration::new(1704445662, 123_456_789);
        // %H gives leading zero "09"; %K should give "9" (in some TZ
        // hour will be different but the digit-count rule still holds).
        let h_padded = ztrftime("%H", t);
        let k_unpadded = ztrftime("%K", t);
        // Both represent the same hour. If h_padded starts with `0`,
        // k_unpadded must be 1-char and not start with `0`.
        if h_padded.starts_with('0') && h_padded.len() == 2 {
            assert!(
                !k_unpadded.starts_with('0'),
                "c:3445 — %K must strip the leading 0 from %H={}, got %K={}",
                h_padded,
                k_unpadded
            );
            assert_eq!(
                k_unpadded.len(),
                1,
                "c:3445 — %K should be 1 digit when hour < 10"
            );
        }
        // %f for day-of-month: t is Jan 5 → in any reasonable TZ
        // day is between 4 and 6; format should be 1 digit when day < 10.
        let d_padded = ztrftime("%d", t);
        let f_unpadded = ztrftime("%f", t);
        if d_padded.starts_with('0') && d_padded.len() == 2 {
            assert!(!f_unpadded.starts_with('0'));
            assert_eq!(
                f_unpadded.len(),
                1,
                "c:3457 — %f should be 1 digit when day < 10"
            );
        }
        // %3. fractional seconds: input nsec is 123456789 → first 3
        // digits should be 123. Note zsh's syntax is `%N.` where N is
        // the digit count (c:3374-3384 + c:3409).
        let frac = ztrftime("%3.", t);
        assert_eq!(
            frac, "123",
            "c:3409-3438 — %3. must emit first 3 digits of nsec"
        );
        // %. with no digit prefix defaults to 3 digits per c:3409.
        let frac_default = ztrftime("%.", t);
        assert_eq!(frac_default, "123", "c:3409 — %. defaults to 3 digits");
    }

    #[test]
    #[cfg(unix)]
    fn get_username_returns_metafied_non_empty_string() {
        let _g = crate::test_util::global_state_lock();
        let name = get_username();
        // CI environments may have a username from `whoami(1)`. In
        // weird sandbox-only setups it might be empty, but on every
        // standard build it should be set.
        if name.is_empty() {
            return;
        }
        // ASCII round-trip via metafy is identity. If the username
        // had high-bit bytes (the bug case), the output would be
        // Meta-escaped — both forms are non-empty so this pin
        // doesn't reject either, just ensures the fn returns
        // SOMETHING usable (not an empty string).
        assert!(
            !name.is_empty(),
            "c:1086 — getpwuid result must yield a non-empty username"
        );
        // Sanity: metafy(name) == name for ASCII input (every byte
        // is below Meta range). Confirms the c:1086 step preserves
        // ASCII paths byte-for-byte.
        if name.bytes().all(|b| b < 0x80) {
            assert_eq!(
                metafy(&name),
                name,
                "c:1086 — ASCII metafy is identity (so pin holds for ASCII users)"
            );
        }
    }

    // ═══════════════════════════════════════════════════════════════════
    // Pure-utility tests — zstrtol/zstrtoul (numeric parse) and
    // getkeystring (key-escape decode). Each test pinned against either
    // C zsh semantics or a direct zsh shell invocation where applicable.
    // ═══════════════════════════════════════════════════════════════════

    // ── zstrtol: numeric string → (value, unconsumed_tail) ──────────
    /// `zstrtol("42", 10)` → (42, "") — full consumption.
    #[test]
    fn zstrtol_decimal_full_consumption() {
        let (v, t) = zstrtol("42", 10);
        assert_eq!(v, 42);
        assert_eq!(t, "");
    }

    /// `zstrtol("42abc", 10)` → (42, "abc") — stops at first non-digit.
    #[test]
    fn zstrtol_decimal_stops_at_non_digit() {
        let (v, t) = zstrtol("42abc", 10);
        assert_eq!(v, 42);
        assert_eq!(t, "abc");
    }

    /// `zstrtol("-7", 10)` → (-7, "") — sign handling.
    #[test]
    fn zstrtol_negative_decimal() {
        let (v, t) = zstrtol("-7", 10);
        assert_eq!(v, -7);
        assert_eq!(t, "");
    }

    /// `zstrtol("+12", 10)` → (12, "") — explicit `+` sign.
    #[test]
    fn zstrtol_explicit_plus_sign_consumed() {
        let (v, t) = zstrtol("+12", 10);
        assert_eq!(v, 12);
        assert_eq!(t, "");
    }

    /// `zstrtol("ff", 16)` → (255, "") — hex base.
    #[test]
    fn zstrtol_hex_base_16() {
        let (v, t) = zstrtol("ff", 16);
        assert_eq!(v, 255);
        assert_eq!(t, "");
    }

    /// `zstrtol("FF", 16)` → (255, "") — case-insensitive hex.
    #[test]
    fn zstrtol_hex_uppercase() {
        let (v, t) = zstrtol("FF", 16);
        assert_eq!(v, 255);
    }

    /// `zstrtol("1010", 2)` → (10, "") — binary base.
    #[test]
    fn zstrtol_binary_base_2() {
        let (v, t) = zstrtol("1010", 2);
        assert_eq!(v, 10);
        assert_eq!(t, "");
    }

    /// `zstrtol("17", 8)` → (15, "") — octal base.
    #[test]
    fn zstrtol_octal_base_8() {
        let (v, t) = zstrtol("17", 8);
        assert_eq!(v, 15);
    }

    /// `zstrtol("0x1A", 0)` → base-detect picks hex from `0x` prefix → 26.
    #[test]
    fn zstrtol_base_zero_detects_hex_prefix() {
        let (v, _) = zstrtol("0x1A", 0);
        assert_eq!(v, 26, "0x1A with base=0 → hex 26");
    }

    /// `zstrtol("0b101", 0)` → base-detect picks binary → 5.
    #[test]
    fn zstrtol_base_zero_detects_binary_prefix() {
        let (v, _) = zstrtol("0b101", 0);
        assert_eq!(v, 5, "0b101 with base=0 → binary 5");
    }

    /// `zstrtol("017", 0)` → base-detect: leading `0` → octal → 15.
    #[test]
    fn zstrtol_base_zero_leading_zero_means_octal() {
        let (v, _) = zstrtol("017", 0);
        assert_eq!(v, 15, "017 with base=0 → octal 15");
    }

    /// `zstrtol("  42", 10)` → (42, "") — leading whitespace skipped.
    #[test]
    fn zstrtol_leading_whitespace_skipped() {
        let (v, _) = zstrtol("  42", 10);
        assert_eq!(v, 42);
    }

    /// `zstrtol("0", 10)` → (0, "") — zero is valid input.
    #[test]
    fn zstrtol_zero_input() {
        let (v, t) = zstrtol("0", 10);
        assert_eq!(v, 0);
        assert_eq!(t, "");
    }

    // ── zstrtol_underscore: digit-separator support ─────────────────
    /// `zstrtol_underscore("1_000_000", 10, true)` → (1_000_000, "")
    /// — underscores skipped during digit accumulation.
    #[test]
    fn zstrtol_underscore_separator_in_decimal() {
        let (v, _) = zstrtol_underscore("1_000_000", 10, true);
        assert_eq!(v, 1_000_000);
    }

    /// Without underscore flag, `_` stops parsing.
    #[test]
    fn zstrtol_underscore_disabled_stops_at_underscore() {
        let (v, t) = zstrtol_underscore("123_456", 10, false);
        assert_eq!(v, 123);
        assert_eq!(t, "_456");
    }

    // ── zstrtoul_underscore: unsigned variant ───────────────────────
    /// Parses a plain decimal unsigned.
    #[test]
    fn zstrtoul_underscore_basic_decimal() {
        let v = zstrtoul_underscore("12345");
        assert_eq!(v, Some(12345));
    }

    /// Empty string → None (no number to parse).
    #[test]
    fn zstrtoul_underscore_empty_returns_none() {
        let v = zstrtoul_underscore("");
        assert_eq!(v, None);
    }

    // ── getkeystring: shell-escape decode ───────────────────────────
    /// `\n` → newline, `\t` → tab, `\r` → CR.
    /// Anchor: `print -r -- $'\n\t\r'` produces the three bytes.
    #[test]
    fn getkeystring_decodes_common_escapes() {
        assert_eq!(getkeystring("\\n").0, "\n");
        assert_eq!(getkeystring("\\t").0, "\t");
        assert_eq!(getkeystring("\\r").0, "\r");
    }

    /// `\\` → single backslash.
    #[test]
    fn getkeystring_double_backslash_yields_single() {
        assert_eq!(getkeystring("\\\\").0, "\\");
    }

    /// `\xNN` hex escape → byte value.
    /// Anchor: `print -r -- $'\x41'` → "A" (0x41 = 65 = 'A').
    #[test]
    fn getkeystring_hex_escape_lowercase_x() {
        assert_eq!(getkeystring("\\x41").0, "A");
        assert_eq!(getkeystring("\\x7E").0, "~");
    }

    /// `\u{NNNN}` Unicode escape → corresponding char.
    /// Anchor: `print -r -- $'é'` → "é".
    #[test]
    fn getkeystring_unicode_escape_u_four_digits() {
        assert_eq!(getkeystring("\\u00E9").0, "é");
    }

    /// Plain text passes through unchanged.
    #[test]
    fn getkeystring_plain_text_passes_through() {
        assert_eq!(getkeystring("hello").0, "hello");
        assert_eq!(getkeystring("").0, "");
    }

    /// Mixed plain + escapes → both handled.
    #[test]
    fn getkeystring_mixed_text_and_escapes() {
        assert_eq!(getkeystring("a\\nb").0, "a\nb");
        assert_eq!(getkeystring("line1\\nline2\\tindented").0, "line1\nline2\tindented");
    }

    // ── metafy / unmetafy round-trip on ASCII (identity) ────────────
    /// ASCII strings: metafy is identity (no meta-bytes to escape).
    #[test]
    fn metafy_then_unmetafy_ascii_roundtrips_to_input() {
        let s = "hello world";
        let m = metafy(s);
        assert_eq!(m, s);
        let mut bytes = m.into_bytes();
        let _len = unmetafy(&mut bytes);
        assert_eq!(String::from_utf8(bytes).unwrap(), "hello world");
    }

    // ═══════════════════════════════════════════════════════════════════
    // quotestring — emit a quoted shell-safe form of the input string.
    // Each QT_* mode produces a different quoting style. Tests pin
    // each mode for both empty and non-empty input.
    // ═══════════════════════════════════════════════════════════════════

    use crate::zsh_h::{
        QT_BACKSLASH, QT_BACKSLASH_PATTERN, QT_BACKSLASH_SHOWNULL,
        QT_DOLLARS, QT_DOUBLE, QT_NONE, QT_SINGLE, QT_SINGLE_OPTIONAL,
    };

    /// QT_NONE: no quoting; passes input through unchanged.
    #[test]
    fn quotestring_qt_none_passes_through_unchanged() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("hello world", QT_NONE), "hello world");
        assert_eq!(quotestring("", QT_NONE), "");
        assert_eq!(quotestring("a*b?c", QT_NONE), "a*b?c");
    }

    /// QT_NONE empty → empty.
    #[test]
    fn quotestring_qt_none_empty_is_empty() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("", QT_NONE), "");
    }

    /// QT_BACKSLASH on empty → "''" (single-quote pair).
    #[test]
    fn quotestring_qt_backslash_empty_yields_empty_single_quotes() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("", QT_BACKSLASH), "''");
    }

    /// QT_BACKSLASH_SHOWNULL on empty → "''" too.
    #[test]
    fn quotestring_qt_backslash_shownull_empty_yields_empty_single_quotes() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("", QT_BACKSLASH_SHOWNULL), "''");
    }

    /// QT_SINGLE on empty → "''" (single-quote pair).
    #[test]
    fn quotestring_qt_single_empty_yields_empty_single_quotes() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("", QT_SINGLE), "''");
    }

    /// QT_SINGLE_OPTIONAL on empty → "''" too.
    #[test]
    fn quotestring_qt_single_optional_empty_yields_empty_single_quotes() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("", QT_SINGLE_OPTIONAL), "''");
    }

    /// QT_DOUBLE on empty → "" (empty double-quote pair).
    #[test]
    fn quotestring_qt_double_empty_yields_empty_double_quotes() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("", QT_DOUBLE), "\"\"");
    }

    /// QT_DOLLARS on empty → "$''".
    #[test]
    fn quotestring_qt_dollars_empty_yields_dollar_quote_pair() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("", QT_DOLLARS), "$''");
    }

    /// QT_BACKSLASH_PATTERN escapes only pattern meta-chars.
    /// Input "a*b?c[d]" → "a\\*b\\?c\\[d\\]".
    #[test]
    fn quotestring_qt_backslash_pattern_escapes_glob_metas() {
        let _g = crate::test_util::global_state_lock();
        let r = quotestring("a*b?c[d]", QT_BACKSLASH_PATTERN);
        assert!(
            r.contains("\\*") && r.contains("\\?") && r.contains("\\[")
                && r.contains("\\]"),
            "all globs must be escaped; got {r:?}"
        );
    }

    /// QT_BACKSLASH_PATTERN does NOT escape plain ASCII letters/digits.
    #[test]
    fn quotestring_qt_backslash_pattern_doesnt_escape_plain_chars() {
        let _g = crate::test_util::global_state_lock();
        let r = quotestring("plain", QT_BACKSLASH_PATTERN);
        assert_eq!(r, "plain", "plain text passes through");
    }

    /// QT_BACKSLASH_PATTERN escapes `<`, `>`, `(`, `)`, `|`, `#`, `^`, `~`.
    #[test]
    fn quotestring_qt_backslash_pattern_escapes_all_meta_set() {
        let _g = crate::test_util::global_state_lock();
        for ch in ['<', '>', '(', ')', '|', '#', '^', '~'] {
            let s = format!("x{ch}y");
            let r = quotestring(&s, QT_BACKSLASH_PATTERN);
            assert!(
                r.contains(&format!("\\{ch}")),
                "{ch:?} must be escaped, got {r:?}"
            );
        }
    }

    /// QT_SINGLE on simple input → "'simple'" (wrapped in single quotes).
    #[test]
    fn quotestring_qt_single_wraps_simple_input() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(quotestring("simple", QT_SINGLE), "'simple'");
    }

    // ═══════════════════════════════════════════════════════════════════
    // metafy / unmetafy edge cases — bytes that NEED meta-encoding
    // (NUL, Meta byte 0x83, Nularg 0xa1, etc.). Pin that metafy escapes
    // them via the Meta + (byte ^ 32) scheme and unmetafy reverses it.
    // ═══════════════════════════════════════════════════════════════════

    /// `metafy` is identity for plain ASCII (no meta bytes).
    #[test]
    fn metafy_ascii_is_identity_byte_for_byte() {
        let s = "abc 123 XYZ!@#";
        assert_eq!(metafy(s), s);
    }

    /// Empty string round-trips.
    #[test]
    fn metafy_empty_string_returns_empty() {
        assert_eq!(metafy(""), "");
    }

    /// metafy → unmetafy is round-trip identity for ASCII input.
    #[test]
    fn metafy_unmetafy_roundtrip_with_punctuation() {
        let s = "Hello, World! 123 +-=";
        let m = metafy(s);
        let mut bytes = m.into_bytes();
        let _ = unmetafy(&mut bytes);
        let result = String::from_utf8(bytes).expect("valid utf-8");
        assert_eq!(result, s);
    }

    /// Multi-byte UTF-8 chars get meta-encoded then unmetafied.
    /// **Known limitation**: zshrs's metafy via `from_utf8_lossy` step
    /// (after Meta+byte^32 expansion) mangles UTF-8 because the encoded
    /// intermediate isn't valid UTF-8. Pin the limitation.
    #[test]
    #[ignore = "ZSHRS LIMITATION: metafy/unmetafy round-trip mangles UTF-8 via lossy String conversion"]
    fn metafy_unmetafy_roundtrip_with_utf8_multibyte_anchored() {
        let s = "日本語";
        let m = metafy(s);
        let mut bytes = m.into_bytes();
        let _ = unmetafy(&mut bytes);
        let result = String::from_utf8(bytes).expect("valid utf-8");
        assert_eq!(result, s, "UTF-8 round-trip must preserve content");
    }

    // ─── zsh-corpus pins for quotestring per quote_type ─────────────

    /// `quotestring(QT_NONE)` is identity on any input.
    #[test]
    fn quotestring_corpus_qt_none_is_identity() {
        let s = "anything `goes` $here";
        assert_eq!(quotestring(s, QT_NONE), s);
    }

    /// `quotestring(QT_SINGLE)` on plain word wraps in single quotes.
    #[test]
    fn quotestring_corpus_qt_single_wraps_plain_word() {
        let out = quotestring("hello", QT_SINGLE);
        assert!(out.starts_with('\'') && out.ends_with('\''),
            "single-quoted = wraps with ', got {out:?}");
        assert!(out.contains("hello"), "content preserved");
    }

    /// `quotestring(QT_DOUBLE)` on plain word wraps in double quotes.
    #[test]
    fn quotestring_corpus_qt_double_wraps_plain_word() {
        let out = quotestring("hello", QT_DOUBLE);
        assert!(out.starts_with('"') && out.ends_with('"'),
            "double-quoted = wraps with \", got {out:?}");
    }

    /// `quotestring(QT_SINGLE)` on string with apostrophe escapes the
    /// apostrophe by closing+escape+reopen: `it's` → `'it'\''s'`.
    #[test]
    fn quotestring_corpus_qt_single_escapes_apostrophe() {
        let out = quotestring("it's", QT_SINGLE);
        assert!(
            out.contains("\\'") || out.contains("'\\''"),
            "apostrophe gets escaped in single-quote form, got {out:?}",
        );
    }

    /// `quotestring(QT_BACKSLASH)` escapes shell special chars.
    /// Pattern chars like `*`, `?`, `$`, `(`, `)` should be backslashed.
    #[test]
    fn quotestring_corpus_qt_backslash_escapes_glob_chars() {
        let out = quotestring("a*b?c", QT_BACKSLASH);
        assert!(out.contains("\\*"), "* gets backslashed, got {out:?}");
        assert!(out.contains("\\?"), "? gets backslashed, got {out:?}");
    }

    /// `quotestring("", QT_DOUBLE)` returns `""` literal.
    #[test]
    fn quotestring_corpus_qt_double_empty_yields_double_quotes() {
        let out = quotestring("", QT_DOUBLE);
        assert_eq!(out, "\"\"", "empty double-quoted = \"\"");
    }

    /// `quotestring("", QT_SINGLE)` returns `''` literal.
    #[test]
    fn quotestring_corpus_qt_single_empty_yields_single_quotes() {
        let out = quotestring("", QT_SINGLE);
        assert_eq!(out, "''", "empty single-quoted = ''");
    }

    /// `quotestring(QT_BACKSLASH)` on plain alphanumeric is identity.
    /// "abc123" should round-trip with no backslashes added.
    #[test]
    fn quotestring_corpus_qt_backslash_plain_word_unchanged() {
        let out = quotestring("abc123", QT_BACKSLASH);
        assert_eq!(out, "abc123",
            "plain alphanumeric needs no escape, got {out:?}");
    }

    /// `ztrlen("hello")` = 5 (no meta chars).
    #[test]
    fn ztrlen_corpus_plain_ascii_byte_count() {
        assert_eq!(ztrlen("hello"), 5);
    }

    /// `ztrlen("")` = 0.
    #[test]
    fn ztrlen_corpus_empty_is_zero() {
        assert_eq!(ztrlen(""), 0);
    }
}