zshrs 0.13.41

The first JIT-compiled Unix shell — bytecode VM, Cranelift JIT, 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
//! One csh command line → zsh. No control-structure keyword at its head.
//!
//! The line is first scanned into simple commands joined by `;` `&` `&&`
//! `||` `|` `|&` (csh word rules: `(` and every operator character break a
//! word, quotes and backslashes are kept verbatim). Each simple command is
//! then rendered by [`render_cmd`], which special-cases the builtins whose
//! syntax or output differs from zsh and sends everything else word by word
//! through [`super::words::translate_word`].
//!
//! Conventions of the emitted zsh:
//!   * csh variables are always zsh arrays (`set x = a` → `x=(a)`) so that
//!     `$#x` / `$x[2]` mean the same on both sides;
//!   * csh aliases become zsh functions, registered in the `_csh_alias` /
//!     `_csh_alias_ls` associative arrays so `alias`, `unalias`, `which` and
//!     `where` can list them;
//!   * shell variables that tcsh mirrors into the environment or special
//!     parameters (`home` `user` `term` `cwd` `prompt` `prompt2` `prompt3`)
//!     are renamed to their zsh parameter on both read and write.
//!
//! # Errors that end a script
//!
//! tcsh stops a script on an error raised in the shell process itself and
//! only fails the command on an error raised in a forked child. Verified
//! against tcsh 6.21 (`/bin/tcsh -f script`):
//!
//! | error | builtin in the shell | external / pipeline stage |
//! |-------|----------------------|----------------------------|
//! | `x: Undefined variable.` | script ends, status 1 | script ends, status 1 |
//! | `f: File exists.` (noclobber), `f: No such file or directory.`, `f: Permission denied.`, `f: Is a directory.` on a redirect | script ends | message, status 1, script continues |
//! | `cmd: No match.` (every glob word empty) | script ends | message, status 1, script continues |
//! | `cmd: Command not found.` | n/a | message, status 1, script continues |
//! | `Unknown user: u.` for `~u` | script ends | message, status 1, script continues |
//! | `EVENT: Event not found.` for a `!` word | the whole line is dropped and the script ends | |
//!
//! "Script ends" is emitted as [`ABORT`]: `exit 1` in a script, only the
//! failed command in an interactive shell. The conditions are checked by
//! self-contained zsh text wrapped around the command (see [`guard_command`]).

use super::expr::translate_at;
use super::lex::split_words;
use super::words::{translate_word, translate_word_globbed};

const NULL_COMMAND: &str = "Invalid null command.";
const MISSING_NAME: &str = "Missing name for redirect.";
const AMBIGUOUS_OUT: &str = "Ambiguous output redirect.";
const AMBIGUOUS_IN: &str = "Ambiguous input redirect.";
const BADLY_PLACED: &str = "Badly placed ()'s.";

/// Ends the script after an error tcsh treats as fatal. An interactive
/// shell only abandons the failed command.
const ABORT: &str = "{ [[ -o interactive ]] || exit 1; false; }";

/// [`ABORT`] for the body of a zsh function.
const ABORT_RET: &str = "{ [[ -o interactive ]] || exit 1; return 1; }";

/// Print `msg` on stderr and end the script (see [`ABORT`]).
fn fatal(msg: &str) -> String {
    format!("{{ print -u2 -r -- {}; {ABORT}; }}", sq(msg))
}

/// The builtins of tcsh 6.21 (`builtins` output). They run inside the shell
/// process, so their errors end a script; every other command is forked.
const TCSH_BUILTINS: &[&str] = &[
    ":", "@", "alias", "alloc", "bg", "bindkey", "break", "breaksw", "builtins", "bye", "case",
    "cd", "chdir", "complete", "continue", "default", "dirs", "echo", "echotc", "else", "end",
    "endif", "endsw", "eval", "exec", "exit", "fg", "filetest", "foreach", "glob", "goto",
    "hashstat", "history", "hup", "if", "jobs", "kill", "limit", "login", "logout", "ls-F", "nice",
    "nohup", "notify", "onintr", "popd", "printenv", "pushd", "rehash", "repeat", "sched", "set",
    "setenv", "settc", "setty", "shift", "source", "stop", "suspend", "switch", "telltc",
    "termname", "time", "umask", "unalias", "uncomplete", "unhash", "unlimit", "unset",
    "unsetenv", "wait", "watchlog", "where", "which", "while",
];

/// Translate a command line that may hold lists (`;` `&&` `||`), pipes
/// (`|` `|&`), background `&`, redirections (`>&` `>!` `>>&` `>>!` `<<`),
/// parenthesised subshells, and the builtins whose syntax differs from zsh
/// (`set` `unset` `setenv` `unsetenv` `alias` `unalias` `shift` `exit`
/// `source` `@` `limit` `unlimit` `jobs` `kill` `wait` `nice` `hup` `glob`
/// `dirs` `pushd` `popd` `which` `where` …).
/// Words go through [`super::words::translate_word`].
///
/// Commands are wrapped in the checks that reproduce tcsh's fatal errors
/// (see the module docs), so a line is usually `if CHECKS; then CMD; else
/// FAIL; fi`. A few translations depend on what earlier lines of the same
/// script set (`set -r`, `set echo_style`); a script is translated in order.
///
/// An empty line yields an empty string. Errors carry tcsh's message text
/// (`Invalid null command.`, `Ambiguous output redirect.`, …).
pub fn translate_line(line: &str) -> Result<String, String> {
    if let Some(event) = history_event(line) {
        return Ok(fatal(&format!("{event}: Event not found.")));
    }
    translate_cmds(line)
}

/// [`translate_line`] without history-substitution checking (alias bodies
/// carry `!` text that is only a history reference at invocation time).
fn translate_cmds(line: &str) -> Result<String, String> {
    STATE.with(|st| st.borrow_mut().commands += 1);
    let cmds = parse(line)?;
    // each segment: its zsh text, its csh text (for the job notice), its separator
    let mut segments: Vec<(String, String, Sep)> = Vec::new();
    let mut pipes: Vec<String> = Vec::new();
    let mut csh_pipes: Vec<String> = Vec::new();
    let mut ops: Vec<Sep> = Vec::new();
    let mut pipeline: Vec<Simple> = Vec::new();
    let mut conns: Vec<Sep> = Vec::new();
    let mut prev: Option<Sep> = None;

    for (cmd, sep) in cmds {
        if cmd.is_empty() {
            // csh drops empty commands around `;` and `&` and a leading `&&`;
            // an empty operand of `|` `||` or a trailing `&&` is an error.
            let bad_after = matches!(sep, Sep::Pipe | Sep::PipeErr | Sep::Or);
            let bad_before = matches!(prev, Some(Sep::Pipe | Sep::PipeErr | Sep::And | Sep::Or));
            if !cmd.ins.is_empty() || !cmd.outs.is_empty() || bad_after || bad_before {
                return Err(NULL_COMMAND.to_string());
            }
            prev = Some(sep);
            continue;
        }
        pipeline.push(cmd);
        prev = Some(sep);
        if matches!(sep, Sep::Pipe | Sep::PipeErr) {
            conns.push(sep);
            continue;
        }
        pipes.push(render_pipeline(&pipeline, &conns, sep == Sep::Bg)?);
        csh_pipes.push(csh_pipeline_text(&pipeline, &conns));
        pipeline.clear();
        conns.clear();
        match sep {
            Sep::And | Sep::Or => ops.push(sep),
            _ => {
                let mut csh = csh_pipes[0].clone();
                for (op, p) in ops.iter().zip(&csh_pipes[1..]) {
                    csh.push_str(if *op == Sep::And { " && " } else { " || " });
                    csh.push_str(p);
                }
                segments.push((render_and_or(&pipes, &ops), csh, sep));
                pipes.clear();
                csh_pipes.clear();
                ops.clear();
            }
        }
    }

    Ok(group_background(&segments))
}

/// What tcsh prints on stdout after starting a background job, even in a
/// script: `[job] pid`. The csh text of the job is kept for `wait`'s report.
fn job_notice(text: &str) -> String {
    format!(
        "print -r -- \"[${{#jobstates}}] $!\"; _csh_jt[${{#jobstates}}]={};",
        sq(text)
    )
}

/// The csh spelling of one simple command, as a job report shows it.
fn csh_simple_text(s: &Simple) -> String {
    let mut out = match &s.sub {
        Some(inner) => format!("( {} )", inner.trim()),
        None => s.words.join(" "),
    };
    for r in s.ins.iter().chain(&s.outs) {
        out.push(' ');
        match r {
            Redir::In(t) => out.push_str(&format!("< {t}")),
            Redir::Here(d) => out.push_str(&format!("<< {d}")),
            Redir::Out { target, append, err, force } => {
                out.push_str(if *append { ">>" } else { ">" });
                if *err {
                    out.push('&');
                }
                if *force {
                    out.push('!');
                }
                out.push_str(&format!(" {target}"));
            }
        }
    }
    out
}

fn csh_pipeline_text(stages: &[Simple], conns: &[Sep]) -> String {
    let mut out = String::new();
    for (k, s) in stages.iter().enumerate() {
        if k > 0 {
            out.push_str(if conns[k - 1] == Sep::PipeErr { " |& " } else { " | " });
        }
        out.push_str(&csh_simple_text(s));
    }
    out
}

/// Join `;`-separated segments. In csh `&` ends a whole `;` list: `a; b &`
/// backgrounds both (job text `( a; b )`), unlike zsh where only `b` is
/// backgrounded, so a multi-item list before `&` becomes `( a; b ) &`.
fn group_background(segments: &[(String, String, Sep)]) -> String {
    let mut parts: Vec<String> = Vec::new();
    let mut group: Vec<&str> = Vec::new();
    let mut csh_group: Vec<&str> = Vec::new();
    for (text, csh, sep) in segments {
        group.push(text);
        csh_group.push(csh);
        match sep {
            Sep::Bg if group.len() > 1 => {
                parts.push(format!("( {} ) &", group.join("; ")));
                parts.push(job_notice(&format!("( {} )", csh_group.join("; "))));
                group.clear();
                csh_group.clear();
            }
            Sep::Bg => {
                parts.push(format!("{} &", group[0]));
                parts.push(job_notice(csh_group[0]));
                group.clear();
                csh_group.clear();
            }
            _ => {}
        }
    }
    if !group.is_empty() {
        parts.push(group.join("; "));
    }
    parts.join(" ")
}

// ---------------------------------------------------------------------------
// Scanning
// ---------------------------------------------------------------------------

/// What follows a simple command.
#[derive(Clone, Copy, PartialEq, Debug)]
enum Sep {
    Semi,
    And,
    Or,
    Pipe,
    PipeErr,
    Bg,
    End,
}

/// A redirection; for `Out` the flags spell the csh operator
/// (`>` `>>` with optional `&` and `!`).
#[derive(Clone, Debug)]
enum Redir {
    In(String),
    Here(String),
    Out { target: String, append: bool, err: bool, force: bool },
}

/// One simple command: a `( subshell )` or a word list, plus redirections.
#[derive(Default, Debug)]
struct Simple {
    sub: Option<String>,
    words: Vec<String>,
    ins: Vec<Redir>,
    outs: Vec<Redir>,
}

impl Simple {
    fn is_empty(&self) -> bool {
        self.sub.is_none() && self.words.is_empty()
    }

    fn at_command_start(&self) -> bool {
        self.is_empty() && self.ins.is_empty() && self.outs.is_empty()
    }
}

#[derive(Default)]
struct Parser {
    cmds: Vec<(Simple, Sep)>,
    cur: Simple,
    word: String,
    pending: Option<Redir>,
}

impl Parser {
    /// Hand the word being built to the current command (or to the
    /// redirection waiting for its target).
    fn flush_word(&mut self) -> Result<(), String> {
        if self.word.is_empty() {
            return Ok(());
        }
        let w = std::mem::take(&mut self.word);
        match self.pending.take() {
            Some(Redir::In(_)) => self.cur.ins.push(Redir::In(w)),
            Some(Redir::Here(_)) => self.cur.ins.push(Redir::Here(w)),
            Some(Redir::Out { append, err, force, .. }) => {
                self.cur.outs.push(Redir::Out { target: w, append, err, force })
            }
            None => {
                if self.cur.sub.is_some() {
                    return Err(BADLY_PLACED.to_string());
                }
                self.cur.words.push(w);
            }
        }
        Ok(())
    }

    fn start_redirect(&mut self, r: Redir) -> Result<(), String> {
        self.flush_word()?;
        if self.pending.is_some() {
            return Err(MISSING_NAME.to_string());
        }
        self.pending = Some(r);
        Ok(())
    }

    fn end_command(&mut self, sep: Sep) -> Result<(), String> {
        self.flush_word()?;
        if self.pending.is_some() {
            return Err(MISSING_NAME.to_string());
        }
        let cur = std::mem::take(&mut self.cur);
        self.cmds.push((cur, sep));
        Ok(())
    }
}

/// Index of the `)` matching the `(` at `start`, quotes and backslashes
/// honoured.
fn matching_paren(cs: &[char], start: usize) -> Result<usize, String> {
    let mut depth = 0usize;
    let mut quote: Option<char> = None;
    let mut i = start;
    while i < cs.len() {
        let c = cs[i];
        match quote {
            Some(q) => {
                if c == '\\' && q != '\'' {
                    i += 1;
                } else if c == q {
                    quote = None;
                }
            }
            None => match c {
                '\\' => i += 1,
                '\'' | '"' | '`' => quote = Some(c),
                '(' => depth += 1,
                ')' => {
                    depth -= 1;
                    if depth == 0 {
                        return Ok(i);
                    }
                }
                _ => {}
            },
        }
        i += 1;
    }
    Err("Too many ('s.".to_string())
}

fn parse(line: &str) -> Result<Vec<(Simple, Sep)>, String> {
    let cs: Vec<char> = line.chars().collect();
    let n = cs.len();
    let mut p = Parser::default();
    let mut i = 0;
    while i < n {
        let c = cs[i];
        let next = cs.get(i + 1).copied();
        match c {
            '\\' => {
                p.word.push(c);
                if let Some(d) = next {
                    p.word.push(d);
                }
                i += 2;
            }
            '\'' | '"' | '`' => {
                p.word.push(c);
                i += 1;
                loop {
                    let Some(&d) = cs.get(i) else {
                        return Err(format!("Unmatched '{c}'."));
                    };
                    p.word.push(d);
                    i += 1;
                    if d == c {
                        break;
                    }
                    // `\"` does not escape the quote inside "…" in csh
                    if d == '\\' && c == '"' && cs.get(i) == Some(&'"') {
                        continue;
                    }
                    if d == '\\' && c != '\'' {
                        // tcsh has no nested backquotes: an escaped one
                        // inside `…` leaves the first unmatched
                        if c == '`' && cs.get(i) == Some(&'`') {
                            return Err("Unmatched '`'.".to_string());
                        }
                        if let Some(&e) = cs.get(i) {
                            p.word.push(e);
                            i += 1;
                        }
                    }
                }
            }
            ':' if p.word.contains('$') => {
                p.word.push(c);
                i += 1;
                if let Some(end) = super::lex::subst_modifier_end(&cs, i) {
                    p.word.extend(&cs[i..=end]);
                    i = end + 1;
                }
            }
            ' ' | '\t' => {
                p.flush_word()?;
                i += 1;
            }
            ';' => {
                p.end_command(Sep::Semi)?;
                i += 1;
            }
            // `$var:&` — the `&` is the modifier character (and an unknown
            // one: tcsh stops with `Bad : modifier in $ '&'.`), not a
            // background operator.
            '&' if p.word.ends_with(':') && p.word.contains('$') => {
                p.word.push('&');
                i += 1;
            }
            '&' if next == Some('&') => {
                p.end_command(Sep::And)?;
                i += 2;
            }
            '&' => {
                p.end_command(Sep::Bg)?;
                i += 1;
            }
            '|' if next == Some('|') => {
                p.end_command(Sep::Or)?;
                i += 2;
            }
            '|' if next == Some('&') => {
                p.end_command(Sep::PipeErr)?;
                i += 2;
            }
            '|' => {
                p.end_command(Sep::Pipe)?;
                i += 1;
            }
            '<' if next == Some('<') => {
                p.start_redirect(Redir::Here(String::new()))?;
                i += 2;
            }
            '<' => {
                p.start_redirect(Redir::In(String::new()))?;
                i += 1;
            }
            '>' => {
                let mut j = i + 1;
                let mut take = |ch: char| {
                    let hit = cs.get(j) == Some(&ch);
                    if hit {
                        j += 1;
                    }
                    hit
                };
                let append = take('>');
                let err = take('&');
                let force = take('!');
                p.start_redirect(Redir::Out { target: String::new(), append, err, force })?;
                i = j;
            }
            '(' => {
                p.flush_word()?;
                let close = matching_paren(&cs, i)?;
                let inner: String = cs[i + 1..close].iter().collect();
                if p.pending.is_none() && p.cur.at_command_start() {
                    p.cur.sub = Some(inner);
                } else {
                    p.word = format!("({inner})");
                    p.flush_word()?;
                }
                i = close + 1;
            }
            ')' => return Err("Too many )'s.".to_string()),
            _ => {
                p.word.push(c);
                i += 1;
            }
        }
    }
    p.end_command(Sep::End)?;
    Ok(p.cmds)
}

// ---------------------------------------------------------------------------
// Pipelines, lists, redirections
// ---------------------------------------------------------------------------

/// Check redirections against pipeline position (csh rejects an output
/// redirect on a non-final stage and an input redirect on a non-first one)
/// and render the stages joined by `|` / `|&`.
/// `background` marks a pipeline that ends in `&`: its stages run in forked
/// children, where an expression error in `@` only fails that stage.
fn render_pipeline(stages: &[Simple], conns: &[Sep], background: bool) -> Result<String, String> {
    let last = stages.len() - 1;
    for (k, s) in stages.iter().enumerate() {
        if s.outs.len() > 1 || (!s.outs.is_empty() && k < last) {
            return Err(AMBIGUOUS_OUT.to_string());
        }
        if s.ins.len() > 1 || (!s.ins.is_empty() && k > 0) {
            return Err(AMBIGUOUS_IN.to_string());
        }
    }
    let mut out = String::new();
    for (k, s) in stages.iter().enumerate() {
        if k > 0 {
            out.push_str(if conns[k - 1] == Sep::PipeErr { " |& " } else { " | " });
        }
        let forked = stages.len() > 1 || background;
        match render_simple(s, stages.len() > 1) {
            Ok(text) => out.push_str(&text),
            // tcsh runs `@ i |= 8` as the pipe `@ i | = 8`: the error comes
            // from a child and the script goes on
            Err(msg) if forked && s.words.first().is_some_and(|w| w == "@") => {
                out.push_str(&format!("{{ print -u2 -r -- {}; false; }}", sq(&msg)));
            }
            Err(msg) => return Err(msg),
        }
    }
    Ok(out)
}

/// Join pipelines with `&&` / `||` using csh precedence: `&&` binds tighter
/// than `||` (`true || a && b` runs nothing), zsh is left-associative, so an
/// `&&` chain that follows a `||` is wrapped in `{ …; }`.
fn render_and_or(pipes: &[String], ops: &[Sep]) -> String {
    let mut operands: Vec<Vec<&str>> = vec![vec![pipes[0].as_str()]];
    for (op, p) in ops.iter().zip(&pipes[1..]) {
        if *op == Sep::Or {
            operands.push(vec![p.as_str()]);
        } else {
            operands.last_mut().expect("one operand").push(p.as_str());
        }
    }
    let rendered: Vec<String> = operands
        .iter()
        .enumerate()
        .map(|(i, chain)| {
            let joined = chain.join(" && ");
            if i > 0 && chain.len() > 1 {
                format!("{{ {joined}; }}")
            } else {
                joined
            }
        })
        .collect();
    rendered.join(" || ")
}

fn render_simple(s: &Simple, in_pipe: bool) -> Result<String, String> {
    let mut text = match &s.sub {
        Some(inner) => {
            if inner.trim().is_empty() {
                return Err(NULL_COMMAND.to_string());
            }
            format!("( {} )", translate_cmds(inner)?)
        }
        None => render_cmd(&s.words)?,
    };
    for r in s.ins.iter().chain(&s.outs) {
        text.push(' ');
        text.push_str(&render_redirect(r));
    }
    Ok(guard_command(s, text, in_pipe))
}

/// One redirection in zsh syntax. `>&` splits into `> f 2>&1`; `>!` is zsh's
/// `>|`. A quoted here-document delimiter is the terminator *with* its
/// quotes in csh (verified against tcsh), so it is re-quoted whole.
fn render_redirect(r: &Redir) -> String {
    match r {
        Redir::In(t) => format!("< {}", tw(t)),
        Redir::Here(d) => {
            if d.contains(['\'', '"', '\\']) {
                format!("<<{}", sq(d))
            } else {
                format!("<<{d}")
            }
        }
        Redir::Out { target, append, err, force } => {
            let op = match (append, force) {
                (false, false) => ">",
                (false, true) => ">|",
                (true, false) => ">>",
                (true, true) => ">>|",
            };
            let dup = if *err { " 2>&1" } else { "" };
            format!("{op} {}{dup}", tw(target))
        }
    }
}

// ---------------------------------------------------------------------------
// Error guards
// ---------------------------------------------------------------------------

/// Shell variables tcsh defines at startup (or whose zsh counterpart is
/// always set); a reference to one is never an `Undefined variable.` error.
const ALWAYS_DEFINED: &[&str] = &[
    "_", "addsuffix", "argv", "autologout", "command", "cwd", "dirstack", "echo_style", "edit",
    "euid", "gid", "group", "histchars", "history", "home", "host", "HOST", "hostname", "HOSTTYPE",
    "loginsh", "MACHTYPE", "OSTYPE", "owd", "path", "prompt", "prompt2", "prompt3", "savehist",
    "shell", "shlvl", "status", "term", "tcsh", "tty", "uid", "user", "VENDOR", "version",
    // environment tcsh exports itself at startup
    "GROUP", "HOME", "LOGNAME", "PATH", "PWD", "SHELL", "SHLVL", "TERM", "USER",
];

/// The event text of the first history reference (`!x`, `a!b`, `!!`, …) in
/// `line`, which tcsh rejects with `EVENT: Event not found.` because a
/// script has no history. `\!` is literal; `!` before a blank, `=`, `~`,
/// `(`, a closing bracket or an operator is literal too (verified against
/// tcsh). Single quotes do not protect a `!`.
fn history_event(line: &str) -> Option<String> {
    let cs: Vec<char> = line.chars().collect();
    let mut i = 0;
    while i < cs.len() {
        match cs[i] {
            '\\' => i += 1,
            '!' => {
                let rest = &cs[i + 1..];
                let Some(&next) = rest.first() else { break };
                match next {
                    ' ' | '\t' | '=' | '~' | '(' | ')' | '}' | ';' | '&' | '|' | '<' | '>' | '\'' | '"'
                    | '`' | '#' => {}
                    ':' | '*' | '^' | '$' | '%' | '!' => return Some("0".to_string()),
                    '-' if rest.get(1).is_some_and(|c| c.is_ascii_digit()) => return Some("0".to_string()),
                    '?' | '{' => {
                        let close = if next == '?' { '?' } else { '}' };
                        let text: String = rest[1..].iter().take_while(|&&c| c != close).collect();
                        return Some(text);
                    }
                    _ => {
                        let stop = [' ', '\t', ':', ';', '&', '|', '<', '>', '(', ')', '\'', '"', '`'];
                        let text: String = rest.iter().take_while(|c| !stop.contains(c)).collect();
                        return Some(text);
                    }
                }
            }
            _ => {}
        }
        i += 1;
    }
    None
}

/// Names of the variables a word dereferences (`$x`, `${x}`, `$#x`, `$x[1]`)
/// outside single quotes and backslash escapes; `$?x`, `$1`, `$$`, `$<` and
/// the always-defined variables are not listed.
pub(crate) fn variable_refs(word: &str) -> Vec<String> {
    let cs: Vec<char> = word.chars().collect();
    let mut names: Vec<String> = Vec::new();
    let mut quote: Option<char> = None;
    let mut i = 0;
    while i < cs.len() {
        let c = cs[i];
        i += 1;
        match (quote, c) {
            (Some(q), _) if c == q => quote = None,
            (None, '\'' | '"') => quote = Some(c),
            (Some('\''), _) => {}
            (_, '\\') => i += 1,
            (_, '$') => {
                let braced = cs.get(i) == Some(&'{');
                if braced {
                    i += 1;
                }
                if cs.get(i) == Some(&'#') {
                    i += 1;
                }
                let start = i;
                while i < cs.len() && (cs[i].is_ascii_alphanumeric() || cs[i] == '_') {
                    i += 1;
                }
                let name: String = cs[start..i].iter().collect();
                let ident = name.chars().next().is_some_and(|c| c.is_ascii_alphabetic() || c == '_');
                if ident && !ALWAYS_DEFINED.contains(&name.as_str()) && !names.contains(&name) {
                    names.push(name);
                }
            }
            _ => {}
        }
    }
    names
}

/// True when `word` holds a pattern tcsh expands: `*`, `?` or a `[…]` class
/// outside quotes, backslash escapes and `$…` references (`$x[2]` is a
/// subscript, not a class).
pub(crate) fn is_glob_word(word: &str) -> bool {
    let cs: Vec<char> = word.chars().collect();
    let mut quote: Option<char> = None;
    let mut i = 0;
    while i < cs.len() {
        let c = cs[i];
        i += 1;
        match (quote, c) {
            (Some(q), _) if c == q => quote = None,
            (Some(_), _) => {}
            (None, '\'' | '"' | '`') => quote = Some(c),
            (None, '\\') => i += 1,
            (None, '$') => {
                if cs.get(i) == Some(&'{') {
                    while i < cs.len() && cs[i] != '}' {
                        i += 1;
                    }
                    i += 1;
                } else if cs.get(i) == Some(&'*') {
                    i += 1; // `$*` is a variable, not a pattern
                } else {
                    while i < cs.len() && (cs[i].is_ascii_alphanumeric() || matches!(cs[i], '_' | '?' | '#')) {
                        i += 1;
                    }
                }
                if cs.get(i) == Some(&'[') {
                    while i < cs.len() && cs[i] != ']' {
                        i += 1;
                    }
                    i += 1;
                }
            }
            (None, '*' | '?') => return true,
            (None, '[') if cs[i..].iter().position(|&x| x == ']').is_some_and(|n| n > 0) => return true,
            _ => {}
        }
    }
    false
}

/// Probe text for an input redirection: prints tcsh's message and fails when
/// `target` cannot be read.
fn input_probe(target: &str) -> String {
    format!(
        "() {{ if [[ ! -e $1 ]]; then print -u2 -r -- \"$1: No such file or directory.\"; \
elif [[ ! -r $1 ]]; then print -u2 -r -- \"$1: Permission denied.\"; else return 0; fi; return 1; }} {target}"
    )
}

/// Probe text for an output redirection, including tcsh's `noclobber`
/// rules: `>` onto an existing file (not a character device) is
/// `File exists.`, `>>` onto a missing file is `No such file or directory.`;
/// `>!` and `>>!` ignore `noclobber`.
fn output_probe(target: &str, append: bool, force: bool) -> String {
    let mut conds: Vec<(&str, &str)> = Vec::new();
    if !force && !append {
        conds.push(("-o noclobber && -e $1 && ! -c $1", "File exists."));
    }
    if !force && append {
        conds.push(("-o noclobber && ! -e $1", "No such file or directory."));
    }
    conds.push(("-d $1", "Is a directory."));
    conds.push(("-e $1 && ! -w $1", "Permission denied."));
    conds.push(("! -e $1 && ! -d ${1:h}", "No such file or directory."));
    conds.push(("! -e $1 && ! -w ${1:h}", "Permission denied."));
    let chain: String = conds
        .iter()
        .enumerate()
        .map(|(k, (cond, msg))| {
            format!("{} [[ {cond} ]]; then _m={}; ", if k == 0 { "if" } else { "elif" }, sq(msg))
        })
        .collect();
    format!(
        "() {{ local _m; {chain}else return 0; fi; print -u2 -r -- \"$1: $_m\"; return 1; }} {target}"
    )
}

/// Guard for a command word containing `/`: tcsh reports
/// `p: Command not found.` for a missing path and `p: Permission denied.`
/// for a directory or a file without the execute bit (zsh says
/// `no such file or directory` with status 127).
fn path_command_probe(word: &str) -> String {
    format!(
        "() {{ if [[ ! -e $1 ]]; then print -u2 -r -- \"$1: Command not found.\"; \
elif [[ -d $1 || ! -x $1 ]]; then print -u2 -r -- \"$1: Permission denied.\"; else return 0; fi; return 1; }} {word}"
    )
}

/// `~name` with an unknown user is `Unknown user: name.`; zsh fails the
/// expansion of the word, which a silenced `:` observes.
fn tilde_probe(name: &str) -> String {
    format!("( : ~{name} ) 2>/dev/null || {{ print -u2 -r -- 'Unknown user: {name}.'; false; }}")
}

/// `${+x}` check for one variable reference.
fn defined_probe(name: &str) -> String {
    // braced so that several probes can be joined with `&&`
    format!("{{ (( ${{+{name}}} )) || {{ print -u2 -r -- '{name}: Undefined variable.'; false; }}; }}")
}

/// The user name of an unquoted `~name` word, if it is one.
fn tilde_user(word: &str) -> Option<&str> {
    let name = word.strip_prefix('~')?;
    let end = name.find('/').unwrap_or(name.len());
    let name = &name[..end];
    let ok = !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.'));
    ok.then_some(name)
}

/// Heads whose arguments are patterns or expressions, not filename words:
/// no `No match.` check applies.
fn skips_glob_check(head: &str) -> bool {
    matches!(head, "unset" | "unsetenv" | "unalias" | "set" | "setenv" | "@" | "eval" | "repeat" | "if")
}

/// Wrap `text` (one rendered simple command, redirections included) in the
/// checks tcsh performs before running it, so that the failures print tcsh's
/// message and end or continue the script exactly as tcsh does (module
/// docs). `in_pipe` marks a pipeline stage, which tcsh forks.
fn guard_command(s: &Simple, text: String, in_pipe: bool) -> String {
    let head = s.words.first().map(String::as_str);
    let in_shell = !in_pipe && head.is_some_and(|h| TCSH_BUILTINS.contains(&h));

    // Failures that end the script wherever the command runs.
    let mut always: Vec<String> = Vec::new();
    if head != Some("eval") {
        for w in &s.words {
            for name in variable_refs(w) {
                let probe = defined_probe(&name);
                if !always.contains(&probe) {
                    always.push(probe);
                }
            }
            for probe in super::words::subscript_probes(w) {
                if !always.contains(&probe) {
                    always.push(probe);
                }
            }
        }
    }

    // Failures that end the script only for a builtin.
    let mut local: Vec<String> = Vec::new();
    let skip_dev = |t: &str| t.starts_with("/dev/") || is_glob_word(t) || t.contains('`');
    for r in &s.ins {
        if let Redir::In(t) = r {
            if !skip_dev(t) {
                local.push(input_probe(&tw(t)));
            }
        }
    }
    for r in &s.outs {
        if let Redir::Out { target, append, force, .. } = r {
            if !skip_dev(target) {
                local.push(output_probe(&tw(target), *append, *force));
            }
        }
    }
    if let Some(h) = head {
        let plain = !h.contains(['$', '`', '*', '?', '[', '\'', '"', '\\']);
        if h.contains('/') && plain && !TCSH_BUILTINS.contains(&h) {
            local.push(path_command_probe(&tw(h)));
        }
        for w in &s.words[1..] {
            if let Some(name) = tilde_user(w) {
                local.push(tilde_probe(name));
            }
        }
        let globs: Vec<String> = s.words[1..].iter().filter(|w| is_glob_word(w)).map(|w| tw(w)).collect();
        if plain && !globs.is_empty() && !skips_glob_check(h) {
            local.push(format!(
                "{{ [[ ! -o cshnullglob ]] || () {{ setopt localoptions nullglob; local -a _g; _g=({}); (( $#_g )); }} \"$@\" || \
{{ print -u2 -r -- {}; false; }}; }}",
                globs.join(" "),
                sq(&format!("{h}: No match."))
            ));
        }
    }

    if local.is_empty() && always.is_empty() {
        return text;
    }
    // The checks overwrite `$?`; a command that reads it (`$status`) gets
    // the value from before them back.
    let keeps_status = text.contains("$?");
    let mut out = if keeps_status { format!("() {{ return $1; }} $_csh_s; {text}") } else { text };
    if !local.is_empty() {
        let fail = if in_shell { ABORT } else { "false" };
        out = format!("if {}; then {out}; else {fail}; fi", local.join(" && "));
    }
    if !always.is_empty() {
        out = format!("if {}; then {out}; else {ABORT}; fi", always.join(" && "));
    }
    if keeps_status {
        out = out.replacen("if ", "if _csh_s=$? && ", 1);
    }
    out
}

// ---------------------------------------------------------------------------
// Word helpers
// ---------------------------------------------------------------------------

/// Single-quote `s` for zsh.
fn sq(s: &str) -> String {
    format!("'{}'", s.replace('\'', "'\\''"))
}

/// csh variable whose value tcsh mirrors into a zsh special parameter.
fn mirrored_name(name: &str) -> Option<&'static str> {
    Some(match name {
        "cwd" => "PWD",
        "home" => "HOME",
        "user" => "USER",
        "term" => "TERM",
        "prompt" => "PROMPT",
        "prompt2" => "PROMPT2",
        "prompt3" => "PROMPT3",
        _ => return None,
    })
}

/// Rewrite `$cwd` `${home}` `$?user` … to the zsh parameter, outside single
/// quotes.
fn map_special_vars(word: &str) -> String {
    let cs: Vec<char> = word.chars().collect();
    let mut out = String::with_capacity(word.len());
    let mut in_sq = false;
    let mut i = 0;
    while i < cs.len() {
        let c = cs[i];
        out.push(c);
        i += 1;
        match c {
            '\'' => in_sq = !in_sq,
            '\\' if !in_sq => {
                if let Some(&d) = cs.get(i) {
                    out.push(d);
                    i += 1;
                }
            }
            '$' if !in_sq => {
                let mut is_set = false;
                for pre in ['{', '?', '#'] {
                    if cs.get(i) == Some(&pre) {
                        is_set |= pre == '?';
                        out.push(pre);
                        i += 1;
                    }
                }
                let start = i;
                while i < cs.len() && (cs[i].is_ascii_alphanumeric() || cs[i] == '_') {
                    i += 1;
                }
                let name: String = cs[start..i].iter().collect();
                match mirrored_name(&name) {
                    // `$?prompt` asks whether the shell is interactive (a
                    // script has no prompt), which the word translator
                    // answers; PROMPT itself always has a value.
                    _ if is_set && name == "prompt" => out.push_str(&name),
                    Some(z) => out.push_str(z),
                    None => out.push_str(&name),
                }
            }
            _ => {}
        }
    }
    out
}

/// csh word → zsh word, with the mirrored variable names applied first and
/// an unclosed `[` made literal (csh keeps `echo [a` as `[a`; zsh calls it a
/// bad pattern).
fn tw(word: &str) -> String {
    // tcsh's `.*` also matches `.` and `..`; zsh's does not
    if let Some(dir) = word.strip_suffix(".*") {
        if dir.is_empty() || (dir.ends_with('/') && !dir.contains(['*', '?', '[', '$', '\\', '"', '\''])) {
            return format!("{dir}. {dir}.. {dir}.*");
        }
    }
    translate_word_globbed(&escape_unclosed_brackets(&map_special_vars(word)))
}

/// Backslash every unquoted `[` that has no `]` after it, outside `$x[…]`
/// subscripts.
fn escape_unclosed_brackets(word: &str) -> String {
    if !word.contains('[') {
        return word.to_string();
    }
    let cs: Vec<char> = word.chars().collect();
    let mut out = String::with_capacity(word.len() + 2);
    let mut quote: Option<char> = None;
    let mut after_var = false;
    let mut i = 0;
    while i < cs.len() {
        let c = cs[i];
        match (quote, c) {
            (Some(q), _) if c == q => quote = None,
            (Some(_), _) => {}
            (None, '\'' | '"' | '`') => quote = Some(c),
            (None, '\\') => {
                out.push(c);
                if let Some(&d) = cs.get(i + 1) {
                    out.push(d);
                }
                i += 2;
                after_var = false;
                continue;
            }
            (None, '[') if !after_var && !cs[i + 1..].contains(&']') => {
                out.push('\\');
            }
            _ => {}
        }
        after_var = quote.is_none() && (c == '}' || is_var_name_tail(&cs[..=i]));
        out.push(c);
        i += 1;
    }
    out
}

/// True when `prefix` ends in `$name` (a variable reference whose `[` starts
/// a subscript).
fn is_var_name_tail(prefix: &[char]) -> bool {
    let end = prefix.len();
    let start = prefix
        .iter()
        .rposition(|c| !(c.is_ascii_alphanumeric() || *c == '_'))
        .map_or(0, |p| p + 1);
    start < end && start > 0 && prefix[start - 1] == '$'
}

/// csh quote removal: `'…'` literal, `"…"` contents, `\c` → `c`.
fn dequote(word: &str) -> String {
    let mut out = String::new();
    let mut quote: Option<char> = None;
    let mut chars = word.chars();
    while let Some(c) = chars.next() {
        match (quote, c) {
            (None, '\'' | '"') => quote = Some(c),
            (Some(q), _) if c == q => quote = None,
            (Some('\''), _) => out.push(c),
            (_, '\\') => {
                if let Some(d) = chars.next() {
                    out.push(d);
                }
            }
            _ => out.push(c),
        }
    }
    out
}

/// True when `word` holds `*`, `?` or `[` outside quotes and escapes.
fn has_glob(word: &str) -> bool {
    scan_unquoted(word, |c| matches!(c, '*' | '?' | '['))
}

/// True when `word` would be expanded by the shell on first parse: an
/// unquoted-by-single-quote `$` or backquote.
fn has_expansion(word: &str) -> bool {
    let mut in_sq = false;
    let mut chars = word.chars();
    while let Some(c) = chars.next() {
        match c {
            '\'' => in_sq = !in_sq,
            '\\' if !in_sq => {
                chars.next();
            }
            '$' | '`' if !in_sq => return true,
            _ => {}
        }
    }
    false
}

/// Run `pred` over the characters of `word` that are outside any quotes and
/// not backslash-escaped; true when it matches one.
fn scan_unquoted(word: &str, pred: impl Fn(char) -> bool) -> bool {
    let mut quote: Option<char> = None;
    let mut chars = word.chars();
    while let Some(c) = chars.next() {
        match quote {
            Some(q) => {
                if c == q {
                    quote = None;
                }
            }
            None => match c {
                '\\' => {
                    chars.next();
                }
                '\'' | '"' | '`' => quote = Some(c),
                _ if pred(c) => return true,
                _ => {}
            },
        }
    }
    false
}

/// Join zsh statements into one command: a lone statement as is, several
/// wrapped in `{ …; }` so they bind as a unit to pipes and redirections.
fn seq(stmts: Vec<String>) -> String {
    match stmts.len() {
        1 => stmts.into_iter().next().expect("one statement"),
        _ => format!("{{ {}; }}", stmts.join("; ")),
    }
}

fn is_digits(s: &str) -> bool {
    !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit())
}

/// Strip one `( … )` group: the text inside, trimmed.
fn paren_inner(word: &str) -> Option<&str> {
    word.strip_prefix('(')?.strip_suffix(')').map(str::trim)
}

// ---------------------------------------------------------------------------
// Commands
// ---------------------------------------------------------------------------

/// Render one simple command (head word plus arguments, no redirections).
fn render_cmd(words: &[String]) -> Result<String, String> {
    let head = words[0].as_str();
    let args = &words[1..];
    let allows_parens = matches!(head, "set" | "@" | "exit");
    if !allows_parens && words.iter().any(|w| w.starts_with('(')) {
        return Err(BADLY_PLACED.to_string());
    }
    // a word with an alias is that alias's function, whatever builtin it names
    if !matches!(head, "alias" | "unalias") && STATE.with(|st| st.borrow().aliases.contains(head)) {
        return Ok(generic(head, args));
    }
    match head {
        "set" => cmd_set(args),
        "unset" => cmd_unset(args),
        "setenv" => cmd_setenv(args),
        "unsetenv" => cmd_unsetenv(args),
        "alias" => cmd_alias(args),
        "unalias" => cmd_unalias(args),
        "shift" => cmd_shift(args),
        "exit" => cmd_exit(args),
        "source" => cmd_source(args),
        "@" => {
            record_at_target(&args);
            translate_at(&args.join(" "))
        }
        "eval" => cmd_eval(args),
        "repeat" => cmd_repeat(args),
        "nohup" if args.is_empty() => Ok("trap '' HUP".to_string()),
        "which" | "where" if args.is_empty() => Err(format!("{head}: Too few arguments.")),
        "which" => Ok(cmd_which(args)),
        "where" => Ok(cmd_where(args)),
        "umask" => cmd_umask(args),
        "history" => Ok(cmd_history(args)),
        "dirs" => Ok(cmd_dirs(args)),
        "pushd" => Ok(cmd_pushd(args)),
        "popd" => Ok(cmd_popd(args)),
        "echo" => Ok(cmd_echo(args)),
        // programmable completion and terminal capabilities have no effect
        // in a script, but an argument-less `uncomplete`/`settc`/`filetest`
        // is tcsh's usage error
        "complete" => Ok(":".to_string()),
        "uncomplete" | "settc" | "filetest" if args.is_empty() => {
            Ok(fatal(&format!("{head}: Too few arguments.")))
        }
        "uncomplete" | "settc" => Ok(":".to_string()),
        "filetest" => Ok(cmd_filetest(args)),
        "hashstat" => Ok(cmd_hashstat()),
        "unhash" => {
            STATE.with(|st| st.borrow_mut().hashed = false);
            Ok(":".to_string())
        }
        "rehash" => {
            STATE.with(|st| st.borrow_mut().hashed = true);
            Ok(generic("rehash", args))
        }
        "chdir" | "cd" if args.len() > 1 => Ok(fatal(&format!("{head}: Too many arguments."))),
        "chdir" => Ok(generic("cd", args)),
        "jobs" => Ok(cmd_jobs(args)),
        "fg" | "bg" => Ok(cmd_fg_bg(head, args)),
        "kill" => Ok(cmd_kill(args)),
        "wait" => Ok(WAIT.to_string()),
        "time" => cmd_time(args),
        "nice" => cmd_nice(args),
        "hup" => cmd_hup(args),
        "glob" => Ok(generic("() { print -rn -- \"${(pj:\\0:)@}\"; }", args)),
        "limit" => cmd_limit(args),
        "unlimit" => cmd_unlimit(args),
        "termname" => Ok("print -r -- \"$TERM\"".to_string()),
        "stop" if args.is_empty() => Ok(fatal("stop: Too few arguments.")),
        "stop" => Ok(format!("{{ [[ -o monitor ]] && {}; true; }}", generic("kill -STOP", args))),
        "suspend" => Ok("{ [[ -o monitor ]] && suspend; true; }".to_string()),
        "notify" if args.is_empty() => {
            Ok(format!("if (( ${{#jobstates}} )); then :; else {}; fi", fatal("notify: No current job.")))
        }
        "notify" => Ok(":".to_string()),
        "builtins" => Ok(format!("print -rl -- {}", TCSH_BUILTINS.iter().map(|b| sq(b)).collect::<Vec<_>>().join(" "))),
        "watchlog" => Ok(generic("log", args)),
        "newgrp" => Ok(generic("exec newgrp", args)),
        "ls-F" => Ok(generic(LS_F, args)),
        "printenv" => cmd_printenv(args),
        "login" | "logout" => Ok(fatal("Not a login shell.")),
        "bye" => Ok("exit 0".to_string()),
        _ => Ok(generic(&tw(head), args)),
    }
}

fn generic(head: &str, args: &[String]) -> String {
    let mut parts = vec![head.to_string()];
    parts.extend(args.iter().map(|w| tw(w)));
    parts.join(" ")
}

// --- set / unset / setenv / unsetenv ---------------------------------------

/// csh option-style variables and the zsh option they drive
/// (`(option, inverted)`: `nonomatch` is the inverse of zsh `nomatch`).
fn option_var(name: &str) -> Option<(&'static str, bool)> {
    Some(match name {
        "noclobber" => ("noclobber", false),
        "ignoreeof" => ("ignoreeof", false),
        "noglob" => ("noglob", false),
        "notify" => ("notify", false),
        "nonomatch" => ("nomatch cshnullglob", true),
        _ => return None,
    })
}

/// Right-hand side of one `set` assignment.
enum Val {
    /// `set x` — a single empty word.
    Empty,
    Word(String),
    /// `(a b c)`, the text between the parentheses.
    List(String),
}

/// `set` / `@` with no arguments: the variables tcsh itself defines, then the
/// ones the script `set` (recorded in `_csh_vars`), sorted, one `name<TAB>value`
/// per line; a list shows in parentheses, a one-word value bare.
const SET_LISTING_HEAD: &str = "() { setopt localtraps; trap '' PIPE; local _st=$1 _n _v; shift; local -a _a; local -A _l; \
_l=(addsuffix '' anyerror '' argv \"(${(j: :)argv})\" cdtohome '' csubstnonl '' cwd \"$PWD\" \
echo_style bsd euid \"$EUID\" euser \"${USERNAME-}\" gid \"$GID\" \
group \"$(id -gn)\" history 100 home \"$HOME\" killring 30 owd \"${OLDPWD-}\" path \"(${(j: :)path})\" \
shell \"${SHELL-/bin/csh}\" shlvl \"${shlvl[1]-1}\" status \"$_st\" tcsh \"${tcsh-}\" term \"${TERM-}\" \
tty \"${(j: :)tty}\" uid \"$UID\" user \"${USER-}\" version \"${version-}\"); \
_l[dirstack]=\"$PWD ${(j: :)${(@)dirstack}}\"; _l[dirstack]=\"${_l[dirstack]% }\"; \
for _n in ";

const SET_LISTING_TAIL: &str = "; do (( ${+parameters[$_n]} )) || continue; \
if [[ ${parameters[$_n]} == array* ]]; then \
_a=(\"${(@P)_n}\"); if (( $#_a == 1 )); then _v=\"$_a[1]\"; else _v=\"(${(j: :)_a})\"; fi; \
else _v=\"${(P)_n}\"; fi; _l[$_n]=$_v; done; \
{ for _n in ${(ok)_l}; do print -r -- \"$_n\"$'\\t'\"${_l[$_n]}\"; done; } 2>/dev/null; return 0; } $? \"$@\"";

/// Tell the translator how the script it is about to translate begins (see
/// `ShellState::starts_with_filler`). Called once per whole-script
/// translation, before the first line.
pub fn note_script_start(src: &str) {
    let first = src.split('\n').next().unwrap_or("").trim();
    let filler = first.is_empty() || first.starts_with('#');
    STATE.with(|st| st.borrow_mut().starts_with_filler = filler);
}

/// `@ name = …`, `@ name++`, `@ name[i] += …`: the variable the statement
/// assigns joins the names the argument-less `set` / `@` listing shows, as a
/// `set` does. A bare `@` (the listing) and an expression that starts with a
/// non-name assigns nothing.
fn record_at_target(args: &[String]) {
    let text = args.join(" ");
    let name: String = text
        .trim_start()
        .chars()
        .enumerate()
        .take_while(|(i, c)| c.is_ascii_alphabetic() || *c == '_' || (*i > 0 && c.is_ascii_digit()))
        .map(|(_, c)| c)
        .collect();
    if !name.is_empty() {
        STATE.with(|st| st.borrow_mut().set_names.insert(name));
    }
}

/// The listing for `set` / `@` with no arguments: the fixed text with the names the
/// script has `set` so far (translation runs in execution order).
fn set_listing() -> String {
    let (names, commands): (Vec<String>, usize) = STATE.with(|st| {
        let st = st.borrow();
        (st.set_names.iter().cloned().collect(), st.commands)
    });
    // tcsh defines `_` (the previous command line; empty in a script) once a
    // command has run, so the first command of a script lists without it. The
    // listing is itself counted, hence 2.
    let filler = STATE.with(|st| st.borrow().starts_with_filler);
    let head = if commands >= 2 || filler {
        SET_LISTING_HEAD.replacen("_l=(", "_l=(_ '' ", 1)
    } else {
        SET_LISTING_HEAD.to_string()
    };
    let tail = SET_LISTING_TAIL;
    format!("{head}{}{tail}", names.join(" "))
}

/// `set` with csh's argument grammar (verified against tcsh): a sequence of
/// `name`, `name = word`, `name=word`, `name = (list)`, `name[i] = word`.
fn cmd_set(args: &[String]) -> Result<String, String> {
    let mut args = args;
    let mut readonly = false;
    while let Some(f) = args.first() {
        let is_flag = f.len() > 1 && f.starts_with('-') && f[1..].chars().all(|c| "rfl".contains(c));
        if !is_flag {
            break;
        }
        readonly |= f.contains('r');
        args = &args[1..];
    }
    if args.is_empty() {
        return Ok(set_listing());
    }

    let mut stmts = Vec::new();
    let mut i = 0;
    while i < args.len() {
        let w = args[i].as_str();
        i += 1;
        let (name, sub, rest) = split_set_name(w)?;
        if name == "path" {
            STATE.with(|st| st.borrow_mut().hashed = true);
        }
        let val = set_value(args, &mut i, rest);
        // `set status = n` sets the exit status the next `$status` reads
        if name == "status" && sub.is_none() {
            let n = match &val {
                Val::Word(w) => tw(w),
                _ => "0".to_string(),
            };
            stmts.push(format!("() {{ return {n}; }}"));
            continue;
        }
        // a nested group is where tcsh expects the next variable name
        if matches!(&val, Val::List(inner) if inner.contains('(')) {
            return Err("set: Variable name must begin with a letter.".to_string());
        }
        if name == "echo_style" && sub.is_none() {
            if let Val::Word(v) = &val {
                record_echo_style(&dequote(v));
            }
        }
        STATE.with(|st| st.borrow_mut().set_names.insert(name.to_string()));
        let assigned = seq(set_statements(name, sub, val, readonly)?);
        let known_readonly = STATE.with(|st| st.borrow().readonly.contains(name));
        if readonly {
            STATE.with(|st| st.borrow_mut().readonly.insert(name.to_string()));
        }
        stmts.push(if known_readonly { guard_readonly("set", name, assigned) } else { assigned });
    }
    Ok(seq(stmts))
}

/// Split `name[sub]rest` and validate the name as tcsh does.
fn split_set_name(w: &str) -> Result<(&str, Option<&str>, &str), String> {
    let starts_ok = w.chars().next().is_some_and(|c| c.is_ascii_alphabetic() || c == '_');
    if !starts_ok {
        return Err("set: Variable name must begin with a letter.".to_string());
    }
    let end = w
        .find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
        .unwrap_or(w.len());
    let (name, mut rest) = w.split_at(end);
    let mut sub = None;
    if let Some(body) = rest.strip_prefix('[') {
        let close = body.find(']').ok_or("set: Subscript error.")?;
        sub = Some(&body[..close]);
        rest = &body[close + 1..];
    }
    if !rest.is_empty() && !rest.starts_with('=') {
        return Err("set: Variable name must contain alphanumeric characters.".to_string());
    }
    Ok((name, sub, rest))
}

/// Value of one assignment, given the text after the name (`rest`) and the
/// words still to come. tcsh accepts `x`, `x = w`, `x=w`, `x = (l)`, `x=(l)`
/// and `x= (l)`; with `x=` or a trailing `x =` and no group the value is the
/// empty word, and the next word starts a new assignment.
fn set_value(args: &[String], i: &mut usize, rest: &str) -> Val {
    let group = |i: &mut usize| match args.get(*i) {
        Some(next) if next.starts_with('(') => {
            *i += 1;
            paren_inner(next).map(|l| Val::List(l.to_string()))
        }
        _ => None,
    };
    if let Some(glued) = rest.strip_prefix('=') {
        if !glued.is_empty() {
            return Val::Word(glued.to_string());
        }
        return group(i).unwrap_or(Val::Empty);
    }
    match args.get(*i).map(String::as_str) {
        Some("=") => {
            *i += 1;
            match args.get(*i) {
                Some(w) if w.starts_with('(') => group(i).unwrap_or(Val::Empty),
                Some(w) => {
                    *i += 1;
                    Val::Word(w.clone())
                }
                None => Val::Empty,
            }
        }
        Some(next) if next.starts_with("=(") => {
            *i += 1;
            paren_inner(&next[1..]).map_or(Val::Empty, |l| Val::List(l.to_string()))
        }
        _ => Val::Empty,
    }
}

fn set_statements(name: &str, sub: Option<&str>, val: Val, readonly: bool) -> Result<Vec<String>, String> {
    let elems: Vec<String> = match &val {
        Val::Empty => vec!["''".to_string()],
        Val::Word(w) => vec![tw(w)],
        Val::List(inner) => split_words(inner).iter().map(|w| tw(w)).collect(),
    };
    if let Some(s) = sub {
        // a subscript is a number, or a `$` reference that expands to one
        if !s.contains('$') && !s.chars().all(|c| c.is_ascii_digit() || c == '-' || c == '*') {
            return Err("set: Subscript error.".to_string());
        }
        if s.contains('-') && s.chars().all(|c| c.is_ascii_digit() || c == '-') {
            return Err("set: Subscript error.".to_string());
        }
        if matches!(val, Val::List(_)) {
            return Err("set: Syntax Error.".to_string());
        }
        // tcsh aborts on an unset variable or an index past the end.
        let idx = tw(s);
        return Ok(vec![format!(
            "{{ if (( ! ${{+{name}}} )); then print -u2 -r -- '{name}: Undefined variable.'; return 1; \
elif (( {idx} > ${{#{name}}} )); then print -u2 -r -- 'set: Subscript out of range.'; return 1; fi; \
{name}[{idx}]={}; }}",
            elems[0]
        )]);
    }
    let mut out = Vec::new();
    if let Some((opt, inverted)) = option_var(name) {
        out.push(format!("{} {opt}", if inverted { "unsetopt" } else { "setopt" }));
    }
    if let Some(z) = mirrored_name(name) {
        let first = elems.first().cloned().unwrap_or_else(|| "''".to_string());
        let export = if matches!(z, "HOME" | "USER" | "TERM") { "export " } else { "" };
        out.push(format!("{export}{z}={first}"));
        return Ok(out);
    }
    if name == "history" || name == "savehist" {
        let z = if name == "history" { "HISTSIZE" } else { "SAVEHIST" };
        let first = elems.first().cloned().unwrap_or_else(|| "''".to_string());
        out.push(format!("{z}={first}"));
        if name == "history" {
            return Ok(out);
        }
    }
    let ro = if readonly { "readonly " } else { "" };
    let list = if matches!(&val, Val::List(inner) if inner.trim().is_empty()) {
        String::new()
    } else {
        elems.join(" ")
    };
    out.push(format!("{ro}{name}=({list})"));
    Ok(out)
}

/// `if` that refuses to change a variable declared `set -r`: tcsh reports
/// `CMD: $NAME is read-only.` and ends the script.
fn guard_readonly(cmd: &str, name: &str, action: String) -> String {
    format!(
        "if [[ ${{parameters[{name}]-}} == *readonly* ]]; then {}; else {action}; fi",
        fatal(&format!("{cmd}: ${name} is read-only."))
    )
}

/// Remember a `set echo_style = …` for the `echo` lines that follow.
fn record_echo_style(value: &str) {
    let style = match value {
        "bsd" => EchoStyle::Bsd,
        "sysv" => EchoStyle::Sysv,
        "both" => EchoStyle::Both,
        "none" => EchoStyle::None,
        _ => return,
    };
    STATE.with(|st| st.borrow_mut().echo_style = style);
}

/// Unset every parameter whose name matches one of the shell patterns and
/// whose export flag equals `exported`. csh keeps shell variables and the
/// environment apart (`unset` touches the first, `unsetenv` the second);
/// zsh has one namespace, so the export flag stands in for the split.
fn unset_matching(pats: &[String], exported: bool) -> String {
    format!(
        "() {{ local _n _p; for _p; do for _n in ${{(k)parameters[(I)$_p]}}; do \
[[ ${{parameters[$_n]}} == *export* ]] {} unset -- $_n; done; done; }} {}",
        if exported { "&&" } else { "||" },
        pats.iter().map(|p| sq(p)).collect::<Vec<_>>().join(" ")
    )
}

fn cmd_unset(args: &[String]) -> Result<String, String> {
    if args.is_empty() {
        return Err("unset: Too few arguments.".to_string());
    }
    let mut stmts = Vec::new();
    let mut pats = Vec::new();
    for w in args {
        let name = dequote(w);
        if has_glob(w) {
            pats.push(name);
            continue;
        }
        if let Some((opt, inverted)) = option_var(&name) {
            stmts.push(format!("{} {opt}", if inverted { "setopt" } else { "unsetopt" }));
        }
        if name == "echo_style" {
            record_echo_style("bsd");
        }
        let known_readonly = STATE.with(|st| st.borrow().readonly.contains(&name));
        match mirrored_name(&name) {
            Some(z) => stmts.push(format!("unset {z}")),
            None if known_readonly => {
                let plain = format!("unset -- {name}");
                stmts.push(guard_readonly("unset", &name, plain));
            }
            None => pats.push(name),
        }
    }
    if !pats.is_empty() {
        stmts.push(unset_matching(&pats, false));
    }
    Ok(seq(stmts))
}

fn check_env_name(name: &str, cmd: &str) -> Result<(), String> {
    let first_ok = name.chars().next().is_some_and(|c| c.is_ascii_alphabetic() || c == '_');
    if !first_ok {
        return Err(format!("{cmd}: Variable name must begin with a letter."));
    }
    if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
        return Err(format!("{cmd}: Variable name must contain alphanumeric characters."));
    }
    Ok(())
}

/// `hashstat`: tcsh reports the hash table geometry once a command hash
/// exists, and nothing before.
fn cmd_hashstat() -> String {
    if STATE.with(|st| st.borrow().hashed) {
        "print -r -- '512 hash buckets of 8 bits each'".to_string()
    } else {
        ":".to_string()
    }
}

fn cmd_setenv(args: &[String]) -> Result<String, String> {
    if args.first().is_some_and(|n| n == "PATH") {
        STATE.with(|st| st.borrow_mut().hashed = true);
    }
    match args {
        [] => Ok("printenv".to_string()),
        [name] => {
            check_env_name(name, "setenv")?;
            Ok(format!("export {name}=''"))
        }
        [name, value] => {
            check_env_name(name, "setenv")?;
            if has_glob(value) {
                // csh expands an unquoted pattern and joins the matches with spaces.
                Ok(format!("() {{ export {name}=\"$*\"; }} {}", tw(value)))
            } else {
                Ok(format!("export {name}={}", tw(value)))
            }
        }
        _ => Err("setenv: Too many arguments.".to_string()),
    }
}

fn cmd_unsetenv(args: &[String]) -> Result<String, String> {
    if args.is_empty() {
        return Err("unsetenv: Too few arguments.".to_string());
    }
    let pats: Vec<String> = args.iter().map(|w| dequote(w)).collect();
    Ok(unset_matching(&pats, true))
}

// --- alias / unalias --------------------------------------------------------

/// Names that are zsh builtins and so need `builtin` (not `command`) when an
/// alias of the same name refers to itself.
const SELF_ALIAS_BUILTINS: &[&str] = &[
    "cd", "pwd", "pushd", "popd", "dirs", "kill", "jobs", "fg", "bg", "wait", "umask", "limit",
    "unlimit", "rehash", "source", "exec", "eval", "test", "time",
];

const ALIAS_LISTING: &str = "() { local _k; for _k in ${(ko)_csh_alias_ls}; do \
print -r -- \"$_k\"$'\\t'\"${_csh_alias_ls[$_k]}\"; done; }";

fn cmd_alias(args: &[String]) -> Result<String, String> {
    let Some(name_raw) = args.first() else {
        return Ok(ALIAS_LISTING.to_string());
    };
    let name = dequote(name_raw);
    if args.len() == 1 {
        return Ok(format!(
            "() {{ (( ${{+_csh_alias[$1]}} )) && print -r -- \"${{_csh_alias[$1]}}\"; true; }} {}",
            sq(&name)
        ));
    }
    if name == "alias" || name == "unalias" {
        return Err(format!("{name}: Too dangerous to alias that."));
    }

    let body_words = &args[1..];
    let body = body_words.iter().map(|w| dequote_alias_word(w)).collect::<Vec<_>>().join(" ");
    let listing = if body_words.len() > 1 { format!("({body})") } else { body.clone() };

    // the body is translated as if the name were not an alias (an alias
    // of `echo` that runs `echo` means the builtin)
    STATE.with(|st| st.borrow_mut().aliases.remove(&name));
    let (mut csh_body, reps, needed) = alias_refs(&body);
    if name != "echo" && csh_body.split_whitespace().next() == Some(name.as_str()) {
        let kw = if SELF_ALIAS_BUILTINS.contains(&name.as_str()) { "builtin" } else { "command" };
        csh_body = format!("{kw} {csh_body}");
    }
    if csh_body.trim().is_empty() {
        csh_body = ":".to_string();
    }
    // (a body ending in a redirect operator takes its target from the call)
    let subshell = parse(&csh_body)
        .or_else(|_| parse(&format!("{csh_body} {ARGS_MARK}")))?
        .last()
        .is_some_and(|(c, _)| c.sub.is_some());
    // Without a history reference tcsh appends the call's arguments to the
    // body; the mark travels through translation as one more word so that
    // it lands inside whatever the last command was wrapped into.
    let append_args = reps.is_empty() && !subshell;
    // a body that opens with a control keyword is a whole statement
    let control = matches!(
        csh_body.split_whitespace().next(),
        Some("if" | "while" | "foreach" | "switch")
    );
    let source = if append_args { format!("{csh_body} {ARGS_MARK}") } else { csh_body };
    let translated = if control {
        super::translate(&source)?.trim_end().to_string()
    } else {
        translate_cmds(&source)?
    };
    let mut zbody = restore_refs(&translated, &reps).replace(ARGS_MARK, "\"$@\"");
    if subshell {
        // arguments cannot follow a `( … )` group
        zbody = format!(
            "if (( $# )); then print -u2 -r -- {}; {ABORT_RET}; else {zbody}; fi",
            sq(BADLY_PLACED)
        );
    }
    if needed > 0 {
        zbody = format!(
            "if (( $# < {needed} )); then print -u2 -r -- 'Bad ! arg selector.'; {ABORT_RET}; fi; {zbody}"
        );
    }

    let safe = name
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '+' | '-'));
    if !safe {
        return Ok(format!("alias -- {}={}", sq(&name), sq(&body)));
    }
    STATE.with(|st| st.borrow_mut().aliases.insert(name.clone()));
    Ok(format!(
        "{{ typeset -gA _csh_alias _csh_alias_ls; {name}() {{ {zbody}; }}; \
_csh_alias[{name}]={}; _csh_alias_ls[{name}]={}; {ALIAS_LOOP_CHECK} {}; }}",
        sq(&body),
        sq(&listing),
        sq(&name)
    ))
}

/// Run after each alias definition. tcsh follows the first word of an alias
/// through other aliases; a chain that comes back to an alias already seen
/// (other than an alias naming itself, which refers to the real command)
/// is `Alias loop.` when any member is invoked, and ends the script.
/// Members of a loop are redefined to print that error.
const ALIAS_LOOP_CHECK: &str = "() { local _n=$1 _m; local -a _c _w; while (( ${+_csh_alias[$_n]} )); do \
_c+=($_n); _m=\"${${_csh_alias[$_n]}%% *}\"; [[ $_m == $_n ]] && return; \
if (( ${_c[(Ie)$_m]} )); then for _m in $_c; do functions[$_m]='print -u2 -r -- \"Alias loop.\"; \
{ [[ -o interactive ]] || exit 1; return 1; }'; done; return; fi; _n=$_m; done; }";

/// Quote removal for one alias word. tcsh stores the alias value as plain
/// text (quotes and backslashes removed) and lexes it again when the alias
/// is used, so `\|` is a pipe, `\*` a glob and `\!` (the way to keep a
/// history reference for invocation time) a plain `!`.
fn dequote_alias_word(w: &str) -> String {
    dequote(w).replace("\\!", "!")
}

/// Which words of the alias invocation a `!` reference selects.
enum Sel {
    All,
    Arg(usize),
    Last,
    Range(usize, usize),
    From(usize),
    ToPenultimate(usize),
}

#[derive(Clone, Copy)]
enum Ctx {
    Bare,
    Double,
    Single,
}

/// Parse the text after a `!`: `*` `^` `$` or `:sel` with an optional
/// `:h :t :r :e` modifier tail. Returns the selector, modifiers and the
/// number of characters consumed.
fn parse_ref(rest: &[char]) -> Option<(Sel, String, usize)> {
    let digits = |from: usize| -> (usize, usize) {
        let mut j = from;
        while rest.get(j).is_some_and(|c| c.is_ascii_digit()) {
            j += 1;
        }
        (rest[from..j].iter().collect::<String>().parse().unwrap_or(0), j)
    };
    let (sel, mut used) = match *rest.first()? {
        '*' => (Sel::All, 1),
        '^' => (Sel::Arg(1), 1),
        '$' => (Sel::Last, 1),
        ':' => match *rest.get(1)? {
            '*' => (Sel::All, 2),
            '^' => (Sel::Arg(1), 2),
            '$' => (Sel::Last, 2),
            c if c.is_ascii_digit() => {
                let (n, j) = digits(1);
                match rest.get(j) {
                    Some('-') if rest.get(j + 1).is_some_and(|c| c.is_ascii_digit()) => {
                        let (m, k) = digits(j + 1);
                        (Sel::Range(n, m), k)
                    }
                    Some('-') => (Sel::ToPenultimate(n), j + 1),
                    Some('*') => (Sel::From(n), j + 1),
                    _ => (Sel::Arg(n), j),
                }
            }
            _ => return None,
        },
        _ => return None,
    };
    let mut mods = String::new();
    if matches!(sel, Sel::Arg(_) | Sel::Last) {
        while rest.get(used) == Some(&':') && rest.get(used + 1).is_some_and(|c| "htreul".contains(*c)) {
            mods.push(rest[used + 1]);
            used += 2;
        }
    }
    Some((sel, mods, used))
}

fn ref_expansion(sel: &Sel, mods: &str, ctx: Ctx) -> String {
    let single = |x: String| -> String {
        let mut e = format!("${{{x}}}");
        for m in mods.chars() {
            e = match m {
                // csh `:u` / `:l` change only the first character
                'u' => format!("${{(U)${{{e}[1]}}}}${{{e}[2,-1]}}"),
                'l' => format!("${{(L)${{{e}[1]}}}}${{{e}[2,-1]}}"),
                _ => format!("${{{e}:{m}}}"),
            };
        }
        match ctx {
            Ctx::Bare => format!("\"{e}\""),
            Ctx::Double => e,
            Ctx::Single => format!("'\"{e}\"'"),
        }
    };
    let many = |x: String| -> String {
        match ctx {
            Ctx::Bare => format!("\"${{{x}}}\""),
            Ctx::Double => format!("${{(j: :){x}}}"),
            Ctx::Single => format!("'\"${{(j: :){x}}}\"'"),
        }
    };
    match sel {
        Sel::All => many("@".to_string()),
        Sel::Arg(n) => single(n.to_string()),
        Sel::Last => single("@[-1]".to_string()),
        Sel::Range(n, m) => many(format!("@[{n},{m}]")),
        Sel::From(n) => many(format!("@[{n},-1]")),
        Sel::ToPenultimate(n) => many(format!("@[{n},-2]")),
    }
}

/// Stands for the alias call's arguments while the body is translated.
const ARGS_MARK: char = '\u{E002}';
const REF_OPEN: char = '\u{E000}';
const REF_CLOSE: char = '\u{E001}';

/// Fewest alias arguments for which tcsh accepts the selector; fewer is
/// `Bad ! arg selector.` (verified: `:N` needs N, `:N-M` needs M, `:N-` needs
/// N+1, `*` `:N*` `$` never fail).
fn min_args(sel: &Sel) -> usize {
    match *sel {
        Sel::All | Sel::Last | Sel::From(_) => 0,
        Sel::Arg(n) => n,
        Sel::Range(_, m) => m,
        Sel::ToPenultimate(n) => n + 1,
    }
}

/// Replace every `!` argument reference in an alias body with a sentinel
/// (so the line translator never sees it) and collect the zsh text each
/// sentinel stands for, chosen by the quote context it appeared in.
fn alias_refs(body: &str) -> (String, Vec<String>, usize) {
    let cs: Vec<char> = body.chars().collect();
    let mut out = String::new();
    let mut reps = Vec::new();
    let mut needed = 0usize;
    let (mut in_sq, mut in_dq) = (false, false);
    let mut i = 0;
    while i < cs.len() {
        let c = cs[i];
        match c {
            '\\' if !in_sq => {
                out.push(c);
                if let Some(&d) = cs.get(i + 1) {
                    out.push(d);
                }
                i += 2;
                continue;
            }
            '\'' if !in_dq => in_sq = !in_sq,
            '"' if !in_sq => in_dq = !in_dq,
            '!' => {
                if let Some((sel, mods, used)) = parse_ref(&cs[i + 1..]) {
                    let ctx = if in_sq {
                        Ctx::Single
                    } else if in_dq {
                        Ctx::Double
                    } else {
                        Ctx::Bare
                    };
                    needed = needed.max(min_args(&sel));
                    out.push(REF_OPEN);
                    out.push_str(&reps.len().to_string());
                    out.push(REF_CLOSE);
                    reps.push(ref_expansion(&sel, &mods, ctx));
                    i += 1 + used;
                    continue;
                }
            }
            _ => {}
        }
        out.push(c);
        i += 1;
    }
    (out, reps, needed)
}

/// Inverse of the sentinel substitution done by [`alias_refs`].
fn restore_refs(text: &str, reps: &[String]) -> String {
    let mut out = String::new();
    let mut chars = text.chars();
    while let Some(c) = chars.next() {
        if c != REF_OPEN {
            out.push(c);
            continue;
        }
        let idx: String = chars.by_ref().take_while(|&d| d != REF_CLOSE).collect();
        if let Some(r) = idx.parse::<usize>().ok().and_then(|n| reps.get(n)) {
            out.push_str(r);
        }
    }
    out
}

fn cmd_unalias(args: &[String]) -> Result<String, String> {
    if args.is_empty() {
        return Err("unalias: Too few arguments.".to_string());
    }
    STATE.with(|st| {
        let mut st = st.borrow_mut();
        for w in args {
            let pat = dequote(w);
            if pat.contains(['*', '?', '[']) {
                st.aliases.clear();
            } else {
                st.aliases.remove(&pat);
            }
        }
    });
    let pats: Vec<String> = args.iter().map(|w| sq(&dequote(w))).collect();
    Ok(format!(
        "() {{ local _k _p; for _p; do for _k in ${{(k)_csh_alias[(I)$_p]}}; do unset -f -- $_k; \
unset \"_csh_alias[$_k]\" \"_csh_alias_ls[$_k]\"; done; done; }} {}",
        pats.join(" ")
    ))
}

// --- shift / exit / source / eval / repeat -----------------------------------

fn cmd_shift(args: &[String]) -> Result<String, String> {
    const NO_MORE: &str =
        "print -u2 -r -- 'shift: No more words.'; { [[ -o interactive ]] || exit 1; false; }";
    match args {
        [] => Ok(format!("{{ if (( $# )); then shift; else {NO_MORE}; fi; }}")),
        [v] => {
            let name = dequote(v);
            let name = mirrored_name(&name).unwrap_or(&name);
            if name == "argv" {
                return cmd_shift(&[]);
            }
            Ok(format!(
                "{{ if (( ! ${{+{name}}} )); then print -u2 -r -- '{name}: Undefined variable.'; {ABORT}; \
elif (( ! ${{#{name}}} )); then {NO_MORE}; else {name}=(\"${{(@){name}[2,-1]}}\"); fi; }}"
            ))
        }
        _ => Err("shift: Too many arguments.".to_string()),
    }
}

/// `exit` takes a number, optionally parenthesised, or a `$var`; it does not
/// evaluate expressions (`exit (1+2)` is "Badly formed number." in tcsh).
/// Without an argument tcsh exits 0 (verified: it ignores `$status`).
fn cmd_exit(args: &[String]) -> Result<String, String> {
    let text = cmd_exit_text(args)?;
    // in a sourced file `exit` only leaves that file
    Ok(match text.strip_prefix("exit") {
        Some(rest) if super::source_mode() => format!("return{rest}"),
        _ => text,
    })
}

fn cmd_exit_text(args: &[String]) -> Result<String, String> {
    if args.len() > 1 {
        return Err("exit: Expression Syntax.".to_string());
    }
    let Some(raw) = args.first() else {
        return Ok("exit 0".to_string());
    };
    let inner = paren_inner(raw).unwrap_or(raw.as_str());
    let value = dequote(inner);
    if value.is_empty() {
        return Ok("exit 0".to_string());
    }
    // `exit (expr)`: the status is the value of the expression.
    if paren_inner(raw).is_some() && value.contains(char::is_whitespace) {
        return super::expr::translate_exit(inner);
    }
    if value.contains('$') {
        return Ok(format!("exit {}", tw(inner)));
    }
    let digits = value.strip_prefix('-').unwrap_or(&value);
    if is_digits(digits) {
        return Ok(format!("exit {value}"));
    }
    if value.starts_with(|c: char| c.is_ascii_digit() || c == '-') {
        return Err("exit: Badly formed number.".to_string());
    }
    Err("exit: Expression Syntax.".to_string())
}

/// `source [-h] file [args]`. `-h` only loads history (no-op). The sourced
/// file is csh text and the arguments become its `argv`: both are handled by
/// the `source` function the driver preamble defines, so the call is
/// emitted unchanged and only a missing file operand is an error here.
fn cmd_source(args: &[String]) -> Result<String, String> {
    match args.first().map(String::as_str) {
        None => Err("source: Too few arguments.".to_string()),
        Some("-h") => Ok(":".to_string()),
        Some(_) => Ok(generic("source", args)),
    }
}

/// `eval` re-parses its arguments as csh. When no argument expands on the
/// first parse the resulting text is known now and is translated inline;
/// otherwise the zsh `eval` is emitted and the value is taken as-is.
fn cmd_eval(args: &[String]) -> Result<String, String> {
    if args.is_empty() {
        return Ok("eval".to_string());
    }
    if args.iter().any(|w| has_expansion(w)) {
        // The text is only known at run time and must reach `eval` as one
        // blank-joined string: an unquoted value would be split and globbed
        // first (a lone `|` would vanish), so each expanding word is passed
        // as if double-quoted.
        let words: Vec<String> = args
            .iter()
            .map(|w| {
                let plain = !w.contains(['"', '\'', '`', '\\']);
                if plain && has_expansion(w) {
                    tw(&format!("\"{w}\""))
                } else {
                    tw(w)
                }
            })
            .collect();
        return Ok(format!("eval {}", words.join(" ")));
    }
    let text = args.iter().map(|w| dequote(w)).collect::<Vec<_>>().join(" ");
    // The text is a whole csh program (`if`/`foreach` blocks included), so it
    // goes through the script translator, not the one-line command layer.
    let inner = super::translate(&text)?;
    let inner = inner.trim_end();
    if inner.contains('\n') {
        Ok(format!("{{ {inner}\n}}"))
    } else {
        Ok(format!("{{ {inner}; }}"))
    }
}

/// `repeat N cmd`: `cmd` is one simple command; a redirection on the line is
/// applied once around the whole loop (`repeat 2 echo a > f` leaves two
/// lines in `f`).
fn cmd_repeat(args: &[String]) -> Result<String, String> {
    if args.len() < 2 {
        return Err("repeat: Too few arguments.".to_string());
    }
    let count = dequote(&args[0]);
    if !count.contains('$') && !is_digits(count.strip_prefix('-').unwrap_or(&count)) {
        return Err("repeat: Badly formed number.".to_string());
    }
    let body = render_cmd(&args[1..])?;
    // An arithmetic `for`, not `repeat`: under the drop-in's sh-style
    // emulation `repeat` is not a reserved word (c:builtin.c:214), so the
    // zsh loop would be a parse error when read line by line. A nested
    // repeat gets its own counter.
    let var = format!("_csh_r{}", body.matches("_csh_r").count());
    Ok(format!(
        "for (( {var} = {}; {var} > 0; {var}-- )); do {body}; done",
        tw(&args[0])
    ))
}

// --- which / where / umask / history / echo / directory stack ----------------

/// `which` in tcsh wording: builtins (and the zsh functions that stand in for them
/// in `--csh` mode: `cd`, `pushd`, `popd`) as `NAME: shell built-in command.`,
/// aliases as `NAME: <tab> aliased to VALUE`, misses as
/// `NAME: Command not found.` with status 1.
fn cmd_which(args: &[String]) -> String {
    let body = "() { local _c _r=0; for _c; do if (( ${+_csh_alias[$_c]} )); then \
print -r -- \"$_c: \"$'\\t'\" aliased to ${_csh_alias[$_c]}\"; else \
case \"$(whence -w -- $_c)\" in \
*\": builtin\"|*\": reserved\"|*\": function\") print -r -- \"$_c: shell built-in command.\";; \
*\": none\") print -r -- \"$_c: Command not found.\"; _r=1;; \
*) whence -p -- $_c;; esac; fi; done; return $_r; }";
    generic(body, args)
}

/// `where`: every alias / builtin / `$PATH` match, status 1 when none.
fn cmd_where(args: &[String]) -> String {
    let body = "() { local _c _p _r=1; for _c; do if (( ${+_csh_alias[$_c]} )); then \
print -r -- \"$_c is aliased to ${_csh_alias[$_c]}\"; _r=0; fi; \
case \"$(whence -w -- $_c)\" in \
*\": builtin\"|*\": reserved\") print -r -- \"$_c is a shell built-in\"; _r=0;; \
*\": function\") (( ${+_csh_alias[$_c]} )) || { print -r -- \"$_c is a shell built-in\"; _r=0; };; esac; \
for _p in ${(f)\"$(whence -pa -- $_c)\"}; do print -r -- $_p; _r=0; done; done; return $_r; }";
    generic(body, args)
}

/// `umask` prints the mask without leading zeros (zsh prints `022`).
fn cmd_umask(args: &[String]) -> Result<String, String> {
    match args {
        [] => Ok("printf '%o\\n' $((8#$(umask)))".to_string()),
        [m] => {
            let v = dequote(m);
            let in_range = u32::from_str_radix(&v, 8).is_ok_and(|m| m <= 0o777);
            if !v.contains('$') && !(is_digits(&v) && in_range) {
                return Err("umask: Improper mask.".to_string());
            }
            Ok(format!("umask {}", tw(m)))
        }
        _ => Err("umask: Too many arguments.".to_string()),
    }
}

/// `history [-h] [-r] [-c] [N]` onto `fc`.
fn cmd_history(args: &[String]) -> String {
    let mut flags = String::new();
    let mut count = None;
    for a in args {
        match a.as_str() {
            "-c" => return "fc -p".to_string(),
            "-h" => flags.push('n'),
            "-r" => flags.push('r'),
            n if is_digits(n) => count = Some(n.to_string()),
            _ => return generic("history", args),
        }
    }
    let range = count.map_or("1".to_string(), |n| format!("-{n}"));
    // an empty history lists nothing (zsh's `fc` would say "no such event")
    format!("{{ (( HISTCMD > 1 )) && fc -l{flags} {range}; true; }}")
}

/// The directory stack line tcsh prints: entries separated by spaces with a
/// trailing space.
const STACK_LINE: &str = "print -r -- \"$(dirs) \"";

/// `dirs`: `-n` and `-p` print the same single line as bare `dirs`, `-l`
/// expands `~`, `-S` / `-L` (save/load) are not supported and ignored, an
/// unknown flag is tcsh's usage error.
fn cmd_dirs(args: &[String]) -> String {
    let words: Vec<&str> = args.iter().map(String::as_str).collect();
    match words.as_slice() {
        [] | ["-n"] | ["-p"] => STACK_LINE.to_string(),
        ["-l"] => "print -r -- \"$(dirs -l) \"".to_string(),
        ["-S"] | ["-L"] => ":".to_string(),
        [flag, ..] if flag.starts_with('-') && !flag[1..].chars().all(|c| "plvnSLc".contains(c)) => {
            fatal("Usage: dirs [-plvnSLc].")
        }
        _ => generic("dirs", args),
    }
}

/// The `+N` operand of `pushd` / `popd`, when `args` is exactly that.
fn stack_index(args: &[String]) -> Option<&str> {
    match args {
        [a] => a.strip_prefix('+').filter(|n| is_digits(n)),
        _ => None,
    }
}

/// `pushd`: csh always prints the stack afterwards; bare `pushd` with an
/// empty stack is "No other directory.", `pushd +N` past the end is
/// "Directory stack not that deep.".
fn cmd_pushd(args: &[String]) -> String {
    if args.is_empty() {
        return format!(
            "{{ if (( $#dirstack )); then pushd -q && {STACK_LINE}; else \
print -u2 -r -- 'pushd: No other directory.'; false; fi; }}"
        );
    }
    if let Some(n) = stack_index(args) {
        return format!(
            "if (( {n} > $#dirstack )); then {}; else {{ pushd -q +{n} && {STACK_LINE}; }}; fi",
            fatal("pushd: Directory stack not that deep.")
        );
    }
    format!("{{ {} && {STACK_LINE}; }}", generic("pushd -q", args))
}

/// `popd`: prints the stack afterwards; `popd +N` past the end is
/// "Directory stack not that deep.".
fn cmd_popd(args: &[String]) -> String {
    if let Some(n) = stack_index(args) {
        return format!(
            "if (( {n} > $#dirstack )); then {}; else {{ popd -q +{n} && {STACK_LINE}; }}; fi",
            fatal("popd: Directory stack not that deep.")
        );
    }
    format!("{{ {} && {STACK_LINE}; }}", generic("popd -q", args))
}

/// How the tcsh `echo_style` variable shapes `echo`.
#[derive(Clone, Copy, PartialEq, Default)]
enum EchoStyle {
    /// `bsd` (default): `-n` suppresses the newline, no escapes.
    #[default]
    Bsd,
    /// `sysv`: backslash escapes, `-n` is an ordinary word.
    Sysv,
    /// `both`: `-n` and backslash escapes.
    Both,
    /// `none`: neither.
    None,
}

/// Translation-time state of the shell variables whose value changes how
/// later lines are translated. A script is translated in execution order,
/// so a `set` seen earlier decides the lines after it.
#[derive(Default)]
struct ShellState {
    echo_style: EchoStyle,
    /// Names declared `set -r`: later `set` / `unset` of them is an error.
    readonly: std::collections::BTreeSet<String>,
    /// The command hash exists (`rehash`, `set path`, `setenv PATH`): only
    /// then does `hashstat` have anything to report.
    hashed: bool,
    /// Variables the script has `set`, for the argument-less `set` listing.
    set_names: std::collections::BTreeSet<String>,
    /// Command lines translated so far, for the `_` variable of the listing.
    commands: usize,
    /// The script's first physical line is blank or a comment: tcsh has read
    /// a line before the first command, so `_` already exists there.
    starts_with_filler: bool,
    /// Names currently aliased: a call to one goes to its function, even
    /// when the word is also a builtin this translator renders inline.
    aliases: std::collections::BTreeSet<String>,
}

thread_local! {
    static STATE: std::cell::RefCell<ShellState> = std::cell::RefCell::new(ShellState::default());
}

/// True when `word` is an expansion that could yield `-n` as a whole: it
/// starts with `$` or a backquote, or is `-` followed by one.
fn may_expand_to_dash_n(word: &str) -> bool {
    let w = word.strip_prefix('-').unwrap_or(word);
    w.starts_with(['$', '`'])
}

/// tcsh `echo` under the current `echo_style` (default `bsd`: only a leading
/// `-n` is an option and backslashes are never interpreted, unlike zsh
/// `echo`, so `print` is used). When the first word comes from an expansion
/// its value is only known at run time and `-n` is tested there, as tcsh
/// does after expansion.
fn cmd_echo(args: &[String]) -> String {
    let (raw, dash_n) = match STATE.with(|s| s.borrow().echo_style) {
        EchoStyle::Bsd => (true, true),
        EchoStyle::Sysv => (false, false),
        EchoStyle::Both => (false, true),
        EchoStyle::None => (true, false),
    };
    let print = |n: bool| -> String {
        match (raw, n) {
            (true, true) => "print -rn --",
            (true, false) => "print -r --",
            (false, true) => "print -n --",
            (false, false) => "print --",
        }
        .to_string()
    };
    match args.first() {
        Some(a) if dash_n && dequote(a) == "-n" => generic(&print(true), &args[1..]),
        Some(a) if dash_n && may_expand_to_dash_n(a) && map_special_vars(a) == *a => {
            // The words go through an array assignment, not `() { … } args`:
            // the arguments of an anonymous call are not filename-generated,
            // so an unquoted `$pat` holding `a*` would stay unexpanded.
            format!(
                "{{ _csh_ea=({}); if [[ ${{_csh_ea[1]-}} == -n ]]; then {} \"${{(@)_csh_ea[2,-1]}}\"; else {} \"${{(@)_csh_ea}}\"; fi; }}",
                args.iter().map(|w| tw(w)).collect::<Vec<_>>().join(" "),
                print(true),
                print(false)
            )
        }
        _ => generic(&print(false), args),
    }
}

/// `ls-F`: `ls -F`, with tcsh's error for a missing operand (the script
/// ends). Not reproduced: its per-directory headers and column layout.
const LS_F: &str = "() { local _a; for _a; do [[ $_a == -* || -e $_a || -L $_a ]] || \
{ print -u2 -r -- \"$_a: No such file or directory.\"; { [[ -o interactive ]] || exit 1; return 1; }; }; done; \
command ls -F \"$@\"; }";

/// `printenv [NAME]`: tcsh's builtin prints the environment or the value of
/// one exported variable, status 1 when it is not in the environment.
fn cmd_printenv(args: &[String]) -> Result<String, String> {
    match args {
        [] => Ok("env".to_string()),
        [_] => Ok(generic(
            "() { if [[ ${parameters[$1]-} == *export* ]]; then print -r -- ${(P)1}; else return 1; fi; }",
            args,
        )),
        _ => Err("printenv: Too many arguments.".to_string()),
    }
}

// --- job control, signals, time ----------------------------------------------

/// tcsh's job line: `[N]  M STATE-padded-to-29 TEXT` with `M` `+` for the
/// oldest job, `-` for the next and blank otherwise; `jobs -l` puts the pid
/// before the state.
fn cmd_jobs(args: &[String]) -> String {
    let long = args.iter().any(|a| a == "-l");
    format!(
        "() {{ local _n _s _p _k; local -a _f; local _i=0; for _n in ${{(onk)jobstates}}; do \
_f=(${{(s.:.)jobstates[$_n]}}); _s=$_f[1]; _p=${{_f[3]%%=*}}; \
case $_s in running) _s=Running;; suspended*) _s=Suspended;; done) _s=Done;; esac; \
_k=' '; (( _i == 0 )) && _k='+'; (( _i == 1 )) && _k='-'; _i=$(( _i + 1 )); \
printf '[%d]  %s %s%-29s %s\\n' $_n \"$_k\" \"{}\" \"$_s\" \"$jobtexts[$_n]\"; done; }}",
        if long { "${_p} " } else { "" }
    )
}

/// `wait`: block for every background job and report each on stderr the way
/// tcsh does (`Done`, `Exit N`, or the terminating signal).
const WAIT: &str = "() { local _n _s _f _t _k _c=${#jobstates}; local _i=0; for _n in ${(On)${(k)jobstates}}; do \
_t=${_csh_jt[$_n]:-${jobtexts[$_n]}}; wait %$_n 2>/dev/null; _s=$?; (( _s == 127 )) && _s=0; \
case $_s in 0) _f=Done;; 129) _f=Hangup;; 130) _f=Interrupt;; 137) _f=Killed;; 143) _f=Terminated;; \
*) _f=\"Exit $_s\";; esac; _k='+'; (( _i == 0 && _c % 2 == 1 )) && _k=' '; _i=$(( _i + 1 )); \
printf '[%d]  %s %-29s %s\\n' $_n \"$_k\" \"$_f\" \"$_t\" >&2; done; true; }";

/// `fg` / `bg`: a script has no job control (`No job control in this
/// shell.`, script ends); an interactive shell passes them through.
fn cmd_fg_bg(head: &str, args: &[String]) -> String {
    format!(
        "if [[ -o monitor ]]; then {}; else {}; fi",
        generic(head, args),
        fatal("No job control in this shell.")
    )
}

/// `kill -l`: tcsh prints an empty line, then each signal name followed by a
/// space on its own line.
const KILL_LIST: &str = "() { local _s; print -r -- ''; for _s in ${(@)signals[2,-3]}; do print -r -- \"$_s \"; done; }";

/// `kill [-sig] id…`: an unknown signal is `SIG: Unknown signal; kill -l
/// lists signals.` and a failing target `ID: No such process` (or
/// `Operation not permitted`; tcsh prints these two on stdout), both ending
/// the script.
const KILL_FN: &str = "() { local _a _s; if [[ $1 == -<-> || $1 == -[A-Za-z]* ]]; then _s=${${1#-}#SIG}; \
[[ $_s == <-> ]] || (( ${signals[(Ie)${_s:u}]} )) || { print -u2 -r -- \"$_s: Unknown signal; kill -l lists signals.\"; \
{ [[ -o interactive ]] || exit 1; return 1; }; }; fi; builtin kill \"$@\" 2>/dev/null && return 0; \
for _a in ${@:#-*}; do builtin kill -0 -- $_a 2>/dev/null && continue; \
if ps -p $_a >/dev/null 2>&1; then print -r -- \"$_a: Operation not permitted\"; \
else print -r -- \"$_a: No such process\"; fi; { [[ -o interactive ]] || exit 1; return 1; }; done; return 1; }";

fn cmd_kill(args: &[String]) -> String {
    match args.first().map(|a| dequote(a)) {
        None => fatal("kill: Too few arguments."),
        Some(a) if a == "-l" => KILL_LIST.to_string(),
        Some(_) => generic(KILL_FN, args),
    }
}


/// `filetest -op file…`: a `1` or `0` per file on one line, as the matching\n/// `if (-op f)` would answer.
fn cmd_filetest(args: &[String]) -> String {
    let op = dequote(&args[0]);
    let letter = op.strip_prefix('-').and_then(|s| s.chars().next());
    match letter {
        Some(l) if "edfrwxzslbcpSugko".contains(l) && args.len() > 1 => format!(
            "() {{ local _f; local -a _o; for _f; do [[ -{l} $_f ]] && _o+=1 || _o+=0; done; print -r -- \"${{(j: :)_o}}\"; }} {}",
            args[1..].iter().map(|w| tw(w)).collect::<Vec<_>>().join(" ")
        ),
        _ => fatal("filetest: Too few arguments."),
    }
}

fn cmd_time(args: &[String]) -> Result<String, String> {
    if args.is_empty() {
        return Ok("_csh_time_self".to_string());
    }
    // zsh's `time` writes its report to the shell's stderr and cannot be
    // pointed elsewhere, so the command's own stderr is parked on a spare
    // descriptor and the report is collected from the shell's fd 2, then
    // reprinted in tcsh's format on stdout.
    Ok(format!(
        "{{ local _ctf=$(mktemp) _cfd _crc; exec {{_cfd}}>&2; \
TIMEFMT='%*U %*S %*E %P %F %W'; \
{{ time {{ ( {} ); }} 2>&$_cfd; }} 2>$_ctf; _crc=$?; \
_csh_time_fmt <$_ctf; exec {{_cfd}}>&-; rm -f $_ctf; (exit $_crc); }}",
        render_cmd(args)?
    ))
}

/// `nice [+N|-N] cmd`: the increment is added to the shell's (default 4).
/// The external `nice -n` takes it; a builtin runs as is, since tcsh
/// applies priority to forked children only.
fn cmd_nice(args: &[String]) -> Result<String, String> {
    let Some(first) = args.first() else {
        return Ok(":".to_string());
    };
    let spec = dequote(first);
    let digits = spec.strip_prefix(['+', '-']).unwrap_or("");
    let (inc, rest) = if is_digits(digits) {
        (spec.trim_start_matches('+').to_string(), &args[1..])
    } else {
        ("4".to_string(), args)
    };
    if rest.is_empty() {
        return Ok(":".to_string());
    }
    let inner = render_cmd(rest)?;
    if TCSH_BUILTINS.contains(&rest[0].as_str()) {
        Ok(inner)
    } else {
        Ok(format!("command nice -n {inc} {inner}"))
    }
}

/// `hup cmd`: run `cmd` with SIGHUP ignored; bare `hup` ignores it in the
/// shell itself.
fn cmd_hup(args: &[String]) -> Result<String, String> {
    if args.is_empty() {
        return Ok("trap '' HUP".to_string());
    }
    Ok(format!("( trap '' HUP; {} )", render_cmd(args)?))
}

// --- limit / unlimit --------------------------------------------------------

/// tcsh resource name, zsh `ulimit` flag and the unit the flag works in
/// (`s` seconds, `b` 512-byte blocks, `k` kilobytes, `n` a count).
const RESOURCES: &[(&str, char, char)] = &[
    ("cputime", 't', 's'),
    ("filesize", 'f', 'b'),
    ("datasize", 'd', 'k'),
    ("stacksize", 's', 'k'),
    ("coredumpsize", 'c', 'b'),
    ("memoryuse", 'm', 'k'),
    ("descriptors", 'n', 'n'),
    ("memorylocked", 'l', 'k'),
    ("maxproc", 'u', 'n'),
];

/// zsh text that prints one resource as tcsh does: `unlimited`, `M:SS` /
/// `H:MM:SS`, `N kbytes`, or a bare count with a trailing blank. `scope` is
/// `-S` (soft) or `-H` (hard).
fn limit_line(name: &str, flag: char, unit: char, scope: &str) -> String {
    let shown = match unit {
        's' => "(( _v >= 3600 )) && _v=\"$(( _v / 3600 )):${(l:2::0:)$(( _v % 3600 / 60 ))}:${(l:2::0:)$(( _v % 60 ))}\" \
|| _v=\"$(( _v / 60 )):${(l:2::0:)$(( _v % 60 ))}\"",
        'b' => "_v=\"$(( _v / 2 )) kbytes\"",
        'k' => "_v=\"$_v kbytes\"",
        _ => "_v=\"$_v \"",
    };
    format!(
        "_v=$(ulimit {scope} -{flag} 2>/dev/null) || _v=unlimited; [[ $_v == unlimited ]] || {{ {shown}; }}; printf '%-12s %s\\n' {name} \"$_v\""
    )
}

/// Resolve a (possibly abbreviated) tcsh resource name; the error is tcsh's
/// message for an unknown or ambiguous prefix.
fn find_resource(name: &str, cmd: &str) -> Result<(&'static str, char, char), String> {
    if let Some(exact) = RESOURCES.iter().find(|(n, _, _)| *n == name) {
        return Ok(*exact);
    }
    let hits: Vec<_> = RESOURCES
        .iter()
        .filter(|(n, _, _)| !name.is_empty() && n.starts_with(name))
        .collect();
    match hits.as_slice() {
        [one] => Ok(**one),
        [] => Err(format!("{cmd}: No such limit.")),
        _ => Err(format!("{cmd}: Ambiguous.")),
    }
}

/// Size or time operand of `limit` in the resource's unit, or `None` when it
/// is not a literal tcsh accepts. Sizes take `k` `m` `g` (kbytes by default),
/// times `s` `m` `h` or `M:SS`.
fn limit_value(text: &str, unit: char) -> Option<String> {
    if text == "unlimited" {
        return Some("unlimited".to_string());
    }
    let (num, suffix) = match text.find(|c: char| !c.is_ascii_digit() && c != ':') {
        Some(i) => text.split_at(i),
        None => (text, ""),
    };
    if num.is_empty() {
        return None;
    }
    let n = |s: &str| s.parse::<u64>().ok();
    match unit {
        's' => {
            if let Some((m, s)) = num.split_once(':') {
                return Some((n(m)? * 60 + n(s)?).to_string());
            }
            let mult = match suffix {
                "" | "s" => 1,
                "m" => 60,
                "h" => 3600,
                _ => return None,
            };
            Some((n(num)? * mult).to_string())
        }
        'b' | 'k' => {
            let kb = match suffix {
                "" | "k" => n(num)?,
                "m" => n(num)? * 1024,
                "g" => n(num)? * 1024 * 1024,
                _ => return None,
            };
            Some(kb.to_string())
        }
        _ if suffix.is_empty() => Some(num.to_string()),
        _ => None,
    }
}

/// `limit [-h] [resource [value]]` onto `ulimit`.
fn cmd_limit(args: &[String]) -> Result<String, String> {
    let hard = args.first().is_some_and(|a| a == "-h");
    let args = if hard { &args[1..] } else { args };
    let scope = if hard { "-H" } else { "-S" };
    match args {
        [] => {
            let lines: Vec<String> =
                RESOURCES.iter().map(|(n, f, u)| limit_line(n, *f, *u, scope)).collect();
            Ok(format!("() {{ local _v; {}; }}", lines.join("; ")))
        }
        [name] => Ok(match find_resource(&dequote(name), "limit") {
            Ok((n, f, u)) => format!("() {{ local _v; {}; }}", limit_line(n, f, u, scope)),
            Err(e) => fatal(&e),
        }),
        [name, value] => {
            let (n, f, u) = match find_resource(&dequote(name), "limit") {
                Ok(r) => r,
                Err(e) => return Ok(fatal(&e)),
            };
            let raw = dequote(value);
            let Some(v) = limit_value(&raw, u) else {
                return Ok(fatal("limit: Improper or unknown scale factor."));
            };
            // zsh's own `limit` reads the same time and size operands
            // (`1:30`, `2h`, `4m`); it has no `memoryuse`.
            let h = if hard { "-h " } else { "" };
            if n == "memoryuse" {
                Ok(format!("ulimit {scope} -{f} {v}"))
            } else {
                Ok(format!("limit {h}{n} {raw}"))
            }
        }
        _ => Err("limit: Too many arguments.".to_string()),
    }
}

/// `unlimit [-h] [resource…]`: raise each (all, when none is named) to
/// `unlimited`; failures to do so are silent.
fn cmd_unlimit(args: &[String]) -> Result<String, String> {
    let hard = args.first().is_some_and(|a| a == "-h");
    let args = if hard { &args[1..] } else { args };
    let scope = if hard { "-H" } else { "-S" };
    let mut flags: Vec<char> = Vec::new();
    if args.is_empty() {
        flags.extend(RESOURCES.iter().map(|(_, f, _)| *f));
    }
    for a in args {
        match find_resource(&dequote(a), "unlimit") {
            Ok((_, f, _)) => flags.push(f),
            Err(e) => return Ok(fatal(&e)),
        }
    }
    let mut stmts: Vec<String> =
        flags.iter().map(|f| format!("ulimit {scope} -{f} unlimited 2>/dev/null")).collect();
    stmts.push("true".to_string());
    Ok(format!("{{ {}; }}", stmts.join("; ")))
}

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

    fn tr(line: &str) -> String {
        translate_line(line).unwrap_or_else(|e| panic!("{line}: {e}"))
    }

    fn err(line: &str) -> String {
        translate_line(line).expect_err(line)
    }

    /// True when `line` translates to `cmd` wrapped by the redirection
    /// probes (`if PROBE; then cmd; else FAIL; fi`).
    fn guarded(line: &str, cmd: &str) -> bool {
        let z = tr(line);
        z.contains("if () {")
            && (z.ends_with(&format!("then {cmd}; else false; fi"))
                || z.ends_with(&format!("then {cmd}; else {ABORT}; fi")))
    }

    #[test]
    fn and_binds_tighter_than_or() {
        // tcsh: `true || echo a && echo b` prints nothing.
        assert_eq!(
            tr("true || echo a && echo b"),
            "true || { print -r -- a && print -r -- b; }"
        );
        assert_eq!(tr("false && echo a || echo b"), "false && print -r -- a || print -r -- b");
    }

    #[test]
    fn leading_and_is_ignored_and_empty_commands_are_dropped() {
        assert_eq!(tr("&& echo a"), "print -r -- a");
        assert_eq!(tr("echo a ;; echo b"), "print -r -- a; print -r -- b");
        assert_eq!(tr("echo a;"), "print -r -- a");
        assert_eq!(tr("echo a & ; echo b"), "print -r -- a & print -r -- \"[${#jobstates}] $!\"; print -r -- b");
    }

    #[test]
    fn null_command_errors() {
        for l in ["| cat", "echo a |", "echo a &&", "|| echo a", "echo a && && echo b", "> /tmp/f", "()", "( )", "< f"] {
            assert_eq!(err(l), "Invalid null command.", "{l}");
        }
    }

    #[test]
    fn redirect_errors() {
        assert_eq!(err("echo a >"), "Missing name for redirect.");
        assert_eq!(err("echo a >&"), "Missing name for redirect.");
        assert_eq!(err("echo a <"), "Missing name for redirect.");
        assert_eq!(err("echo a > b > c"), "Ambiguous output redirect.");
        assert_eq!(err("echo a > f | cat"), "Ambiguous output redirect.");
        assert_eq!(err("echo a >& f | cat"), "Ambiguous output redirect.");
        assert_eq!(err("cat < f < f"), "Ambiguous input redirect.");
        assert_eq!(err("cat | cat < f"), "Ambiguous input redirect.");
        // a redirect to a file is probed first; the command itself is unchanged
        assert!(guarded("echo a | cat > f", "cat > f"));
        assert!(tr("cat < f | cat").ends_with("then cat < f; else false; fi | cat"));
    }

    #[test]
    fn paren_errors() {
        assert_eq!(err("echo (a b)"), "Badly placed ()'s.");
        assert_eq!(err("echo a(b)"), "Badly placed ()'s.");
        assert_eq!(err("(echo a) b"), "Badly placed ()'s.");
        assert_eq!(err("(echo a"), "Too many ('s.");
        assert_eq!(err("echo a)"), "Too many )'s.");
        assert_eq!(err("setenv A (b)"), "Badly placed ()'s.");
        assert_eq!(err("echo \"a"), "Unmatched '\"'.");
        assert_eq!(err("echo 'a"), "Unmatched '''.");
    }

    #[test]
    fn redirect_operators() {
        assert!(guarded("ls >! f", "ls >| f"));
        assert!(guarded("ls >> f", "ls >> f"));
        assert!(guarded("ls >>! f", "ls >>| f"));
        assert!(guarded("ls >& f", "ls > f 2>&1"));
        assert!(guarded("ls >>& f", "ls >> f 2>&1"));
        assert!(guarded("ls >&! f", "ls >| f 2>&1"));
        assert_eq!(tr("ls |& cat"), "ls |& cat");
        // redirect glued to words, and an fd-looking word stays a word
        assert!(guarded("echo a>f", "print -r -- a > f"));
        assert!(guarded("ls /x 2> f", "ls /x 2 > f"));
        // redirect target may precede the arguments
        assert!(guarded("echo > f a b", "print -r -- a b > f"));
    }

    #[test]
    fn heredoc_delimiter_keeps_csh_terminator_text() {
        assert_eq!(tr("cat <<EOF"), "cat <<EOF");
        assert_eq!(tr("cat << EOF"), "cat <<EOF");
        assert_eq!(tr("cat <<'EOF'"), "cat <<''\\''EOF'\\'''");
        assert!(guarded("cat <<EOF > f", "cat <<EOF > f"));
    }

    #[test]
    fn subshells_and_background() {
        // csh `&` ends a whole `;` list: `a; b &` backgrounds both.
        assert_eq!(tr("echo a; echo b &"), "( print -r -- a; print -r -- b ) & print -r -- \"[${#jobstates}] $!\";");
        assert_eq!(tr("echo a; echo b & echo c; echo d"), "( print -r -- a; print -r -- b ) & print -r -- \"[${#jobstates}] $!\"; print -r -- c; print -r -- d");
        assert_eq!(tr("echo a && echo b &"), "print -r -- a && print -r -- b & print -r -- \"[${#jobstates}] $!\";");
        assert_eq!(tr("(cd /usr; pwd)"), "( cd /usr; pwd )");
        assert_eq!(tr("(echo a; echo b) &"), "( print -r -- a; print -r -- b ) & print -r -- \"[${#jobstates}] $!\";");
        assert!(guarded("(echo a) > f", "( print -r -- a ) > f"));
        assert_eq!(tr("(echo a) | (cat)"), "( print -r -- a ) | ( cat )");
    }

    #[test]
    fn set_forms() {
        assert_eq!(tr("set"), set_listing());
        assert_eq!(tr("set x"), "x=('')");
        assert_eq!(tr("set x = word"), "x=(word)");
        assert_eq!(tr("set x=val"), "x=(val)");
        assert_eq!(tr("set x = "), "x=('')");
        assert_eq!(tr("set x = (a b c)"), "x=(a b c)");
        assert_eq!(tr("set x=(a b c)"), "x=(a b c)");
        assert_eq!(tr("set x= (a b)"), "x=(a b)");
        assert_eq!(tr("set x = ()"), "x=()");
        assert!(tr("set x[2] = w").ends_with("x[2]=w; }"));
        assert!(tr("set x[2]=w").contains("'set: Subscript out of range.'"));
        assert_eq!(tr("set x=a=b"), "x=(a=b)");
        assert_eq!(tr("set x = \"a b\""), "x=(\"a b\")");
    }

    #[test]
    fn set_multiple_assignments_follow_tcsh_parse() {
        // `set x = a b` sets x=a and declares b empty.
        assert_eq!(tr("set x = a b"), "{ x=(a); b=(''); }");
        assert_eq!(tr("set a=1 b=2"), "{ a=(1); b=(2); }");
        assert_eq!(tr("set a = 1 b = 2"), "{ a=(1); b=(2); }");
        assert_eq!(tr("set x= val"), "{ x=(''); val=(''); }");
        assert_eq!(tr("set x = (a b) y = (c d)"), "{ x=(a b); y=(c d); }");
    }

    #[test]
    fn set_readonly_flag_and_empty_line() {
        assert_eq!(tr("set -r x = 1"), "readonly x=(1)");
        assert_eq!(tr(""), "");
        assert_eq!(tr("  ;  "), "");
    }

    #[test]
    fn set_errors() {
        assert_eq!(err("set 1x = 2"), "set: Variable name must begin with a letter.");
        assert_eq!(err("set x =val"), "set: Variable name must begin with a letter.");
        assert_eq!(err("set = x"), "set: Variable name must begin with a letter.");
        assert_eq!(err("set x-y=1"), "set: Variable name must contain alphanumeric characters.");
        assert_eq!(err("set x[1-2] = (p q)"), "set: Subscript error.");
        assert_eq!(err("set x[2] = (p q)"), "set: Syntax Error.");
        assert_eq!(err("set x = (a b"), "Too many ('s.");
    }

    #[test]
    fn set_mirrors_special_variables() {
        // path is tied to PATH in zsh itself.
        assert_eq!(tr("set path = (/bin /usr/bin)"), "path=(/bin /usr/bin)");
        assert_eq!(tr("set home = /tmp/h"), "export HOME=/tmp/h");
        assert_eq!(tr("set user=bob"), "export USER=bob");
        assert_eq!(tr("set prompt = \"> \""), "PROMPT=\"> \"");
        assert_eq!(tr("set prompt2 = x"), "PROMPT2=x");
        assert_eq!(tr("set history = 50"), "HISTSIZE=50");
        assert_eq!(tr("set noclobber"), "{ setopt noclobber; noclobber=(''); }");
        assert_eq!(tr("set nonomatch"), "{ unsetopt nomatch cshnullglob; nonomatch=(''); }");
        assert!(tr("unset noclobber").starts_with("{ unsetopt noclobber; "));
        // names are rewritten before the word layer sees them; single quotes protect
        let w = |s: &str| translate_word(s);
        assert_eq!(
            tr("echo $cwd ${home} $?user '$cwd'"),
            format!("print -r -- {} {} {} '$cwd'", w("$PWD"), w("${HOME}"), w("$?USER"))
        );
    }

    #[test]
    fn unset_plain_and_glob() {
        // shell variables only: exported (environment) parameters are skipped
        let g = tr("unset a b*");
        assert!(g.ends_with("} 'a' 'b*'") && g.contains("== *export* ]] || unset"), "{g}");
        assert_eq!(tr("unset home"), "unset HOME");
    }

    #[test]
    fn setenv_forms() {
        assert_eq!(tr("setenv"), "printenv");
        assert_eq!(tr("setenv FOO"), "export FOO=''");
        assert_eq!(tr("setenv FOO bar"), "export FOO=bar");
        assert_eq!(tr("setenv FOO \"a b\""), "export FOO=\"a b\"");
        assert_eq!(tr("setenv PATH /usr/bin:/bin"), "export PATH=/usr/bin:/bin");
        assert_eq!(err("setenv FOO bar baz"), "setenv: Too many arguments.");
        assert_eq!(err("setenv FOO=bar"), "setenv: Variable name must contain alphanumeric characters.");
        assert_eq!(err("setenv 1X a"), "setenv: Variable name must begin with a letter.");
        assert_eq!(err("unsetenv"), "unsetenv: Too few arguments.");
        assert_eq!(err("unset"), "unset: Too few arguments.");
        // environment only: shell variables of the same name are kept
        let g = tr("unsetenv A B*");
        assert!(g.ends_with("} 'A' 'B*'") && g.contains("== *export* ]] && unset"), "{g}");
    }

    #[test]
    fn alias_listing_query_and_definition() {
        assert_eq!(tr("alias"), ALIAS_LISTING);
        assert!(tr("alias nope").ends_with("} 'nope'"));
        let d = tr("alias ll \"ls -l\"");
        assert!(d.contains("ll() { ls -l \"$@\"; }"), "{d}");
        assert!(d.contains("_csh_alias[ll]='ls -l'"), "{d}");
        assert!(d.contains("_csh_alias_ls[ll]='ls -l'"), "{d}");
        // several words: listing shows the word list in parentheses
        assert!(tr("alias ll ls -l").contains("_csh_alias_ls[ll]='(ls -l)'"));
        assert_eq!(err("alias alias x"), "alias: Too dangerous to alias that.");
        assert_eq!(err("alias unalias x"), "unalias: Too dangerous to alias that.");
    }

    #[test]
    fn alias_history_references_become_positional_parameters() {
        let d = tr("alias g \"echo \\!*\"");
        assert!(d.contains("g() { print -r -- \"${@}\"; }"), "{d}");
        let d = tr("alias f 'echo \\!^ \\!:2 \\!$ \\!:2-3 \\!:2- \\!:2* \\!:0'");
        assert!(
            d.contains("print -r -- \"${1}\" \"${2}\" \"${@[-1]}\" \"${@[2,3]}\" \"${@[2,-2]}\" \"${@[2,-1]}\" \"${0}\";"),
            "{d}"
        );
        let d = tr("alias b 'echo \\!:1:t'");
        assert!(d.contains("\"${${1}:t}\""), "{d}");
        let d = tr("alias u 'echo \\!:1:u'");
        assert!(d.contains("\"${(U)${${1}[1]}}${${1}[2,-1]}\""), "{d}");
        // inside double quotes the reference is spliced as text
        let d = tr("alias q 'echo \"\\!*\"'");
        assert!(d.contains("\"${(j: :)@}\""), "{d}");
        // a body with a pipe or list keeps both halves
        let d = tr("alias p \"echo x | cat\"");
        assert!(d.contains("print -r -- x | cat \"$@\""), "{d}");
    }

    #[test]
    fn alias_self_reference_uses_command_or_builtin() {
        assert!(tr("alias ls ls -F").contains("ls() { command ls -F \"$@\"; }"));
        let d = tr("alias cd 'cd \\!*; echo in'");
        assert!(d.contains("cd() { builtin cd \"${@}\"; print -r -- in; }"), "{d}");
    }

    #[test]
    fn unalias_requires_a_name() {
        assert_eq!(err("unalias"), "unalias: Too few arguments.");
        assert!(tr("unalias a*").ends_with("'a*'"));
    }

    #[test]
    fn shift_forms() {
        assert!(tr("shift").contains("if (( $# )); then shift; else"));
        assert!(tr("shift argv").contains("if (( $# )); then shift; else"));
        assert!(tr("shift x").contains("x=(\"${(@)x[2,-1]}\")"));
        assert!(tr("shift x").contains("'x: Undefined variable.'"));
        assert_eq!(err("shift a b"), "shift: Too many arguments.");
    }

    #[test]
    fn exit_forms() {
        assert_eq!(tr("exit"), "exit 0");
        assert_eq!(tr("exit 3"), "exit 3");
        assert_eq!(tr("exit (3)"), "exit 3");
        assert_eq!(tr("exit(3)"), "exit 3");
        assert_eq!(tr("exit ( 3 )"), "exit 3");
        assert_eq!(tr("exit \"3\""), "exit 3");
        assert_eq!(tr("exit -1"), "exit -1");
        assert_eq!(tr("exit ()"), "exit 0");
        assert_eq!(err("exit (1+2)"), "exit: Badly formed number.");
        assert_eq!(err("exit 1a"), "exit: Badly formed number.");
        assert_eq!(err("exit a"), "exit: Expression Syntax.");
        assert_eq!(err("exit 1 2"), "exit: Expression Syntax.");
    }

    #[test]
    fn source_forms() {
        assert_eq!(tr("source f.csh"), "source f.csh");
        assert_eq!(tr("source f.csh a b"), "source f.csh a b");
        assert_eq!(tr("source -h f"), ":");
        assert_eq!(err("source"), "source: Too few arguments.");
    }

    #[test]
    fn at_delegates_to_expression_layer() {
        assert_eq!(tr("@ n = 1 + 2"), translate_at("n = 1 + 2").unwrap());
        // operators outside parentheses split the line, as in csh
        assert_eq!(err("@ x = 1 |"), "Invalid null command.");
    }

    #[test]
    fn eval_translates_static_text_as_csh() {
        assert_eq!(tr("eval echo hi"), "{ print -r -- hi; }");
        assert_eq!(tr("eval 'set x = (1 2)'"), "{ x=(1 2); }");
        assert_eq!(tr("eval \"echo a;echo b\""), "{ print -r -- a; print -r -- b; }");
        // a first-parse expansion means the text is only known at run time
        assert_eq!(tr("eval $c"), format!("eval {}", translate_word("\"$c\"")));
    }

    #[test]
    fn repeat_forms() {
        assert_eq!(
            tr("repeat 3 echo hi"),
            "for (( _csh_r0 = 3; _csh_r0 > 0; _csh_r0-- )); do print -r -- hi; done"
        );
        // redirect applies around the whole loop, pipe/list bind to the loop
        assert!(guarded(
            "repeat 2 echo a > f",
            "for (( _csh_r0 = 2; _csh_r0 > 0; _csh_r0-- )); do print -r -- a; done > f"
        ));
        assert_eq!(
            tr("repeat 2 echo a | wc -l"),
            "for (( _csh_r0 = 2; _csh_r0 > 0; _csh_r0-- )); do print -r -- a; done | wc -l"
        );
        assert_eq!(err("repeat"), "repeat: Too few arguments.");
        assert_eq!(err("repeat 3"), "repeat: Too few arguments.");
        assert_eq!(err("repeat a echo"), "repeat: Badly formed number.");
        assert_eq!(err("repeat 2 (echo a)"), "Badly placed ()'s.");
    }

    #[test]
    fn echo_is_bsd_style() {
        assert_eq!(tr("echo a b"), "print -r -- a b");
        assert_eq!(tr("echo -n a"), "print -rn -- a");
        assert_eq!(tr("echo \"-n\" a"), "print -rn -- a");
        assert_eq!(tr("echo -e a"), "print -r -- -e a");
        assert_eq!(tr("echo"), "print -r --");
    }

    #[test]
    fn umask_forms() {
        assert_eq!(tr("umask"), "printf '%o\\n' $((8#$(umask)))");
        assert_eq!(tr("umask 022"), "umask 022");
        assert_eq!(err("umask 99"), "umask: Improper mask.");
        assert_eq!(err("umask a"), "umask: Improper mask.");
        assert_eq!(err("umask 022 1"), "umask: Too many arguments.");
        assert_eq!(err("umask 7777"), "umask: Improper mask.");
        assert_eq!(err("umask 1000"), "umask: Improper mask.");
        assert_eq!(tr("umask 0777"), "umask 0777");
    }

    #[test]
    fn directory_stack() {
        assert_eq!(tr("dirs"), "print -r -- \"$(dirs) \"");
        assert_eq!(tr("dirs -v"), "dirs -v");
        assert_eq!(tr("pushd /usr"), "{ pushd -q /usr && print -r -- \"$(dirs) \"; }");
        assert_eq!(tr("popd"), "{ popd -q && print -r -- \"$(dirs) \"; }");
        assert!(tr("pushd").contains("'pushd: No other directory.'"));
    }

    #[test]
    fn misc_builtins() {
        assert_eq!(err("which"), "which: Too few arguments.");
        assert_eq!(err("where"), "where: Too few arguments.");
        assert_eq!(tr("hashstat"), ":");
        assert_eq!(tr("nohup"), "trap '' HUP");
        assert_eq!(tr("nohup sleep 1"), "nohup sleep 1");
        assert_eq!(tr("limit cputime 10"), "limit cputime 10");
        assert_eq!(tr("history 3"), "{ (( HISTCMD > 1 )) && fc -l -3; true; }");
        assert_eq!(tr("history -h"), "{ (( HISTCMD > 1 )) && fc -ln 1; true; }");
        assert_eq!(tr("chdir /usr"), "cd /usr");
        assert!(tr("which ls").ends_with("} ls"));
        assert!(tr("where ls nope").ends_with("} ls nope"));
    }
    // --- behaviour under zsh, expectations taken from `/bin/tcsh -f` ---------

    /// Translate each line and run the result under `/bin/zsh -f` with the
    /// driver's `cshnullglob`, in an empty scratch directory. `None` when no
    /// zsh is installed.
    fn run(name: &str, lines: &[&str]) -> Option<(String, String, i32)> {
        let zsh = ["/bin/zsh", "/usr/bin/zsh", "/usr/local/bin/zsh", "/opt/homebrew/bin/zsh"]
            .into_iter()
            .find(|p| std::path::Path::new(p).exists())?;
        let dir = std::env::temp_dir().join(format!("csh_cmds_{}_{name}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).ok()?;
        let mut code = String::from("setopt cshnullglob extendedglob\n");
        for l in lines {
            code.push_str(&tr(l));
            code.push('\n');
        }
        let o = std::process::Command::new(zsh)
            .args(["-f", "-c", &code])
            .current_dir(&dir)
            .stdin(std::process::Stdio::null())
            .output()
            .ok()?;
        let _ = std::fs::remove_dir_all(&dir);
        Some((
            String::from_utf8_lossy(&o.stdout).into_owned(),
            String::from_utf8_lossy(&o.stderr).into_owned(),
            o.status.code().unwrap_or(-1),
        ))
    }

    /// Assert stdout, stderr and exit status of the translated lines.
    fn expect(name: &str, lines: &[&str], out: &str, err: &str, rc: i32) {
        if let Some((o, e, r)) = run(name, lines) {
            assert_eq!((o.as_str(), e.as_str(), r), (out, err, rc), "{lines:?}");
        }
    }

    #[test]
    fn failed_redirect_of_a_builtin_ends_the_script() {
        expect(
            "redir_builtin",
            &["echo ok", "echo a > /nonexistent_zz/f", "echo not-reached"],
            "ok\n",
            "/nonexistent_zz/f: No such file or directory.\n",
            1,
        );
    }

    #[test]
    fn failed_redirect_of_an_external_command_continues() {
        expect(
            "redir_external",
            &["ls > /nonexistent_zz/f", "echo st=$status", "echo b > /nonexistent_zz/f || echo alt"],
            "st=1\n",
            "/nonexistent_zz/f: No such file or directory.\n/nonexistent_zz/f: No such file or directory.\n",
            1,
        );
    }

    #[test]
    fn noclobber_refuses_to_overwrite_and_a_forced_redirect_does_not() {
        expect(
            "noclobber",
            &["set noclobber", "echo a > f1", "echo b >! f1", "cat f1", "echo c > f1", "echo not-reached"],
            "b\n",
            "f1: File exists.\n",
            1,
        );
        // `>>` onto a missing file is refused under noclobber
        expect(
            "noclobber_append",
            &["set noclobber", "echo c >> nofile_zz", "echo not-reached"],
            "",
            "nofile_zz: No such file or directory.\n",
            1,
        );
    }

    #[test]
    fn missing_input_file_ends_a_builtin_but_not_an_external_command() {
        expect(
            "input_redirect",
            &["cat < nofile_zz", "echo st=$status", "echo a < nofile_zz", "echo not-reached"],
            "st=1\n",
            "nofile_zz: No such file or directory.\nnofile_zz: No such file or directory.\n",
            1,
        );
    }

    #[test]
    fn no_match_is_fatal_for_a_builtin_and_status_one_for_an_external_command() {
        expect(
            "no_match",
            &["ls *.nomatch_zz", "echo st=$status", "echo a *.nomatch_zz", "echo not-reached"],
            "st=1\n",
            "ls: No match.\necho: No match.\n",
            1,
        );
        // one matching pattern is enough
        expect("one_match", &["touch f1.c", "echo *.zz *.c"], "f1.c\n", "", 0);
    }

    #[test]
    fn undefined_variable_ends_the_script_even_for_an_external_command() {
        expect(
            "undefined",
            &["echo ok", "ls $undef_zz", "echo not-reached"],
            "ok\n",
            "undef_zz: Undefined variable.\n",
            1,
        );
        expect("defined", &["set x = a", "echo $x $#x $?undef_zz $status"], "a 1 0 0\n", "", 0);
    }

    #[test]
    fn history_reference_drops_the_whole_line_and_ends_the_script() {
        expect(
            "history",
            &["echo ok", "echo a; echo b!c", "echo not-reached"],
            "ok\n",
            "c: Event not found.\n",
            1,
        );
        expect("history_bang_bang", &["echo !!"], "", "0: Event not found.\n", 1);
        // an escaped or blank-followed `!` is literal
        expect("history_literal", &["echo a\\!b \\!x ! x a!=b"], "a!b !x ! x a!=b\n", "", 0);
    }

    #[test]
    fn history_event_text_follows_tcsh() {
        assert_eq!(history_event("echo a!b"), Some("b".to_string()));
        assert_eq!(history_event("echo !5"), Some("5".to_string()));
        assert_eq!(history_event("echo !-1"), Some("0".to_string()));
        assert_eq!(history_event("echo !?foo?"), Some("foo".to_string()));
        assert_eq!(history_event("echo !foo:2"), Some("foo".to_string()));
        assert_eq!(history_event("echo 'a!b'"), Some("b".to_string()));
        assert_eq!(history_event("echo a!~b !(x) a!"), None);
        assert_eq!(history_event("echo a;b !; !& !| !> !<"), None);
    }

    #[test]
    fn status_survives_the_guards() {
        expect(
            "status",
            &["sh -c \"exit 3\" >& /dev/null", "echo st=$status", "sh -c \"exit 4\"", "echo $status > f1", "cat f1"],
            "st=3\n4\n",
            "",
            0,
        );
    }

    #[test]
    fn command_path_errors() {
        expect(
            "path_command",
            &["./nosuch_zz", "echo st=$status", "/tmp", "echo st=$status"],
            "st=1\nst=1\n",
            "./nosuch_zz: Command not found.\n/tmp: Permission denied.\n",
            0,
        );
    }

    #[test]
    fn unknown_user_follows_the_builtin_rule() {
        expect(
            "tilde",
            &["ls ~nosuchuser_zz", "echo st=$status", "echo ~nosuchuser_zz", "echo not-reached"],
            "st=1\n",
            "Unknown user: nosuchuser_zz.\nUnknown user: nosuchuser_zz.\n",
            1,
        );
    }

    #[test]
    fn glob_words_ignore_subscripts_and_unclosed_brackets() {
        assert!(is_glob_word("*.c") && is_glob_word("a?") && is_glob_word("[a-z]x"));
        assert!(!is_glob_word("$x[2]") && !is_glob_word("${x}[1]") && !is_glob_word("[a"));
        assert!(!is_glob_word("'*'") && !is_glob_word("\"a?\"") && !is_glob_word("\\*"));
        assert!(!is_glob_word("$?x") && !is_glob_word("$#x"));
        expect("bracket_literal", &["echo [a"], "[a\n", "", 0);
        expect("class_without_match", &["echo [a x[1]"], "", "echo: No match.\n", 1);
    }

    #[test]
    fn variable_refs_skip_specials_and_single_quotes() {
        assert_eq!(variable_refs("$a ${b} $#c $d[1] \"$e\" '$f' \\$g $?h $1 $$ $status $argv"),
            vec!["a", "b", "c", "d", "e"]);
    }

    #[test]
    fn alias_value_is_relexed_text() {
        // `\|` is stored as `|` and runs as a pipe when the alias is used
        let d = tr("alias a3 echo hi \\| tr h H");
        assert!(d.contains("a3() { print -r -- hi | tr h H \"$@\"; }"), "{d}");
        assert!(d.contains("_csh_alias_ls[a3]='(echo hi | tr h H)'"), "{d}");
        expect("alias_pipe", &["alias a3 echo hi \\| tr h H", "a3"], "Hi\n", "", 0);
    }

    #[test]
    fn alias_loops_end_the_script_when_invoked() {
        expect(
            "alias_loop",
            &["alias x1 y1", "alias y1 x1", "echo before", "x1", "echo not-reached"],
            "before\n",
            "Alias loop.\n",
            1,
        );
        // an alias naming itself runs the real command, a chain does not loop
        expect(
            "alias_chain",
            &["alias a1 b1", "alias b1 c1", "alias c1 echo end", "a1 hi", "alias ls ls -F", "alias ls"],
            "end hi\nls -F\n",
            "",
            0,
        );
    }

    #[test]
    fn alias_argument_selectors_need_enough_arguments() {
        expect(
            "alias_selector",
            &["alias a1 \"echo \\!:1\"", "a1 x", "a1", "echo not-reached"],
            "x\n",
            "Bad ! arg selector.\n",
            1,
        );
        assert_eq!(min_args(&Sel::Arg(2)), 2);
        assert_eq!(min_args(&Sel::Range(1, 3)), 3);
        assert_eq!(min_args(&Sel::ToPenultimate(2)), 3);
        assert_eq!(min_args(&Sel::From(5)), 0);
        assert_eq!(min_args(&Sel::All), 0);
    }

    #[test]
    fn alias_body_that_is_a_subshell_takes_no_arguments() {
        expect(
            "alias_subshell",
            &["alias r \"(echo a; echo b)\"", "r", "r x", "echo not-reached"],
            "a\nb\n",
            "Badly placed ()'s.\n",
            1,
        );
        assert_eq!(err("alias q (p)"), "Badly placed ()'s.");
    }

    #[test]
    fn alias_with_unquoted_history_glob_is_no_match() {
        // `\!*` is a pattern for tcsh: no file named `!…` means `alias: No match.`
        expect("alias_glob", &["alias h echo \\!:1 end \\!*", "echo not-reached"], "", "alias: No match.\n", 1);
    }

    #[test]
    fn alias_with_semicolon_ends_at_the_semicolon() {
        expect(
            "alias_semicolon",
            &["alias a1 echo one; echo two", "alias a1", "a1 x"],
            "two\necho one\none x\n",
            "",
            0,
        );
    }

    #[test]
    fn readonly_variables_reject_set_and_unset() {
        expect(
            "readonly",
            &["set -r rr = 1", "echo $rr", "set rr = 2", "echo not-reached"],
            "1\n",
            "set: $rr is read-only.\n",
            1,
        );
        expect(
            "readonly_unset",
            &["set -r rr = 1", "unset rr", "echo not-reached"],
            "",
            "unset: $rr is read-only.\n",
            1,
        );
    }

    #[test]
    fn echo_reads_dash_n_after_expansion_and_honours_echo_style() {
        expect("echo_expanded_n", &["set o = -n", "echo $o x", "echo y"], "xy\n", "", 0);
        expect("echo_empty_first", &["set e = ''", "echo $e -n y"], "y", "", 0);
        expect(
            "echo_style_none",
            &["set echo_style = none", "echo -n a", "echo \"\"", "echo \"x\\ty\""],
            "-n a\n\nx\\ty\n",
            "",
            0,
        );
        expect(
            "echo_style_both",
            &["set echo_style = both", "echo \"a\\tb\"", "echo -n c", "echo", "unset echo_style", "echo \"a\\tb\""],
            "a\tb\nc\na\\tb\n",
            "",
            0,
        );
    }

    #[test]
    fn kill_errors() {
        expect(
            "kill_signal",
            &["kill -FOO 99999999", "echo not-reached"],
            "",
            "FOO: Unknown signal; kill -l lists signals.\n",
            1,
        );
        // a missing process is reported on stdout, without a period
        expect("kill_pid", &["kill 99999999", "echo not-reached"], "99999999: No such process\n", "", 1);
        expect("kill_none", &["kill", "echo not-reached"], "", "kill: Too few arguments.\n", 1);
    }

    #[test]
    fn job_control_builtins_in_a_script() {
        expect("fg", &["fg", "echo not-reached"], "", "No job control in this shell.\n", 1);
        expect("bg", &["bg", "echo not-reached"], "", "No job control in this shell.\n", 1);
        expect("stop", &["stop", "echo not-reached"], "", "stop: Too few arguments.\n", 1);
        expect("notify", &["notify", "echo not-reached"], "", "notify: No current job.\n", 1);
        expect("suspend", &["suspend", "echo ok"], "ok\n", "", 0);
    }

    #[test]
    fn login_builtins_in_a_non_login_shell() {
        expect("logout", &["logout", "echo not-reached"], "", "Not a login shell.\n", 1);
        expect("login", &["login", "echo not-reached"], "", "Not a login shell.\n", 1);
        expect("bye", &["echo ok", "bye", "echo not-reached"], "ok\n", "", 0);
    }

    #[test]
    fn glob_builtin_separates_with_nul_and_no_newline() {
        expect("glob", &["touch ga gb", "glob g?", "echo", "glob nomatch_zz*", "echo not-reached"],
            "ga\0gb\n", "glob: No match.\n", 1);
    }

    #[test]
    fn dirs_flags() {
        expect("dirs_usage", &["dirs -x", "echo not-reached"], "", "Usage: dirs [-plvnSLc].\n", 1);
        assert_eq!(tr("dirs -S"), ":");
        assert_eq!(tr("dirs -n"), "print -r -- \"$(dirs) \"");
        assert!(tr("popd +2").contains("popd: Directory stack not that deep."));
        assert!(tr("pushd +2").contains("pushd: Directory stack not that deep."));
    }

    #[test]
    fn cd_with_two_operands_is_an_error() {
        expect("cd_args", &["cd /usr /tmp", "echo not-reached"], "", "cd: Too many arguments.\n", 1);
    }

    #[test]
    fn printenv_prints_exported_values_only() {
        expect(
            "printenv",
            &["setenv ZZ_A 1", "printenv ZZ_A", "printenv ZZ_NOPE", "echo st=$status"],
            "1\nst=1\n",
            "",
            0,
        );
    }

    #[test]
    fn limit_forms() {
        // listing: `name`, padding to 12, a blank, the value; counts keep a
        // trailing blank, sizes are `N kbytes`, times `M:SS`
        let z = tr("limit");
        assert!(z.contains("printf '%-12s %s\\n' cputime"), "{z}");
        assert!(z.contains("printf '%-12s %s\\n' descriptors"), "{z}");
        assert_eq!(tr("limit cputime 10"), "limit cputime 10");
        assert_eq!(tr("limit -h stacksize 4m"), "limit -h stacksize 4m");
        assert_eq!(tr("limit nosuch 1"), fatal("limit: No such limit."));
        assert_eq!(tr("limit c"), fatal("limit: Ambiguous."));
        assert_eq!(tr("limit cputime 1x"), fatal("limit: Improper or unknown scale factor."));
        assert_eq!(tr("unlimit nosuch"), fatal("unlimit: No such limit."));
        assert_eq!(limit_value("1:30", 's').as_deref(), Some("90"));
        assert_eq!(limit_value("2h", 's').as_deref(), Some("7200"));
        assert_eq!(limit_value("4m", 'k').as_deref(), Some("4096"));
        assert_eq!(limit_value("1x", 'k'), None);
    }

    #[test]
    fn nice_and_hup() {
        assert_eq!(tr("nice"), ":");
        assert_eq!(tr("nice ls"), "command nice -n 4 ls");
        assert_eq!(tr("nice +7 ls -l"), "command nice -n 7 ls -l");
        assert_eq!(tr("nice -3 ls"), "command nice -n -3 ls");
        assert_eq!(tr("nice echo hi"), "print -r -- hi");
        assert_eq!(tr("hup"), "trap '' HUP");
        assert_eq!(tr("hup sleep 1"), "( trap '' HUP; sleep 1 )");
    }

    #[test]
    fn builtins_lists_the_tcsh_builtins() {
        let z = tr("builtins");
        assert!(z.starts_with("print -rl -- ':' '@' 'alias'"), "{z}");
        assert!(TCSH_BUILTINS.windows(2).all(|w| w[0] <= w[1]), "sorted as tcsh prints it");
    }

    #[test]
    fn source_passes_every_operand_through() {
        assert_eq!(tr("source ~/.cshrc.local a b"), format!("source {} a b", tw("~/.cshrc.local")));
        assert_eq!(err("source"), "source: Too few arguments.");
    }

}