shellfirm 0.4.0

`shellfirm` will intercept any risky patterns (default or defined by you) and prompt you a small challenge for double verification, kinda like a captcha for your terminal.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
use std::fmt::Write as _;
use std::fs;
use std::io::{BufRead, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

use clap::builder::PossibleValuesParser;
use clap::{Arg, ArgAction, ArgMatches, Command};
use console::style;
use regex::Regex;
use shellfirm::config::{ascii_fallback, glyph};
use shellfirm::error::Result;
use shellfirm::Config;

const MARKER: &str = "# Added by shellfirm init";
/// Closes the block for shells whose hook is embedded in the rc file, so the
/// whole block can be found (and upgraded / removed) even after the hook text
/// changes between shellfirm versions.
const END_MARKER: &str = "# End of shellfirm init";
/// First line of an rc file that `shellfirm init` created. `--uninstall`
/// deletes such a file again when nothing else is left in it.
const CREATED_NOTE: &str =
    "# Created by `shellfirm init`; `shellfirm init --uninstall` deletes this file when it is otherwise empty.";
/// Follows [`CREATED_NOTE`] once per directory `shellfirm init` created for
/// the rc file (deepest first), so `--uninstall` removes them again when
/// they are empty.
const CREATED_DIR_NOTE: &str = "# shellfirm init also created the directory ";
/// Follows the directory notes once per (empty) file `shellfirm init`
/// created next to the rc file (nushell's `env.nu`), so `--uninstall`
/// removes it again with the rc file when it is still empty.
const CREATED_FILE_NOTE: &str = "# shellfirm init also created the file ";

/// A file the shell needs next to its rc file, created empty when it is
/// missing: nushell asks whether to create `env.nu` at its first start and
/// takes the first typed line as the answer.
fn companion_file(shell: Shell, rc_path: &Path) -> Option<PathBuf> {
    matches!(shell, Shell::Nushell).then(|| rc_path.with_file_name("env.nu"))
}

/// Creates the companion file of `rc_path` when it is missing. Returns it
/// when it was created.
fn create_companion(shell: Shell, rc_path: &Path) -> Option<PathBuf> {
    let file = companion_file(shell, rc_path)?;
    if file.exists() {
        return None;
    }
    fs::write(&file, "").ok().map(|()| file)
}

/// Version of the shell hook protocol. Every hook sets it (for example
/// `__shellfirm_hook_version=9` in bash) so `shellfirm doctor` can tell a
/// shell that still runs an older hook.
pub const HOOK_VERSION: &str = "14";

/// Every name `init` accepts (canonical names and binary names).
const SHELL_NAMES: [&str; 12] = [
    "bash",
    "zsh",
    "fish",
    "nushell",
    "nu",
    "powershell",
    "pwsh",
    "elvish",
    "xonsh",
    "oils",
    "osh",
    "ysh",
];

// ---------------------------------------------------------------------------
// Shell enum
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::enum_variant_names)]
pub enum Shell {
    Bash,
    Zsh,
    Fish,
    Nushell,
    PowerShell,
    Elvish,
    Xonsh,
    Oils,
}

/// How the current shell was found.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShellSource {
    /// The process that started shellfirm is a known shell.
    ParentProcess,
    /// `$SHELL` (the login shell; may differ from the shell in use).
    ShellEnv,
}

impl ShellSource {
    pub const fn describe(self) -> &'static str {
        match self {
            Self::ParentProcess => "parent process",
            Self::ShellEnv => "$SHELL",
        }
    }
}

/// Where rc files live. Abstracted so path resolution is unit-testable.
struct RcEnv {
    home: Option<PathBuf>,
    /// `dirs::config_dir()` (platform config dir).
    platform_config: Option<PathBuf>,
    zdotdir: Option<PathBuf>,
    xdg_config_home: Option<PathBuf>,
}

impl RcEnv {
    fn from_process() -> Self {
        let abs_env = |name: &str| {
            std::env::var_os(name)
                .map(PathBuf::from)
                .filter(|p| p.is_absolute())
        };
        Self {
            home: dirs::home_dir(),
            platform_config: dirs::config_dir(),
            zdotdir: abs_env("ZDOTDIR"),
            xdg_config_home: abs_env("XDG_CONFIG_HOME"),
        }
    }

    /// `$XDG_CONFIG_HOME`, falling back to `~/.config` (what fish, elvish,
    /// Oils and PowerShell on Unix use on every platform).
    fn xdg_config(&self) -> Option<PathBuf> {
        self.xdg_config_home
            .clone()
            .or_else(|| self.home.as_ref().map(|h| h.join(".config")))
    }
}

/// Name of the process that started shellfirm (`zsh`, `-bash`, ...).
fn parent_process_name() -> Option<String> {
    #[cfg(unix)]
    {
        let ppid = std::os::unix::process::parent_id();
        if let Ok(comm) = fs::read_to_string(format!("/proc/{ppid}/comm")) {
            return Some(comm.trim().to_string());
        }
        let out = std::process::Command::new("ps")
            .args(["-o", "comm=", "-p", &ppid.to_string()])
            .stdin(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .output()
            .ok()?;
        let name = String::from_utf8_lossy(&out.stdout).trim().to_string();
        (!name.is_empty()).then_some(name)
    }
    #[cfg(not(unix))]
    {
        None
    }
}

impl Shell {
    pub const ALL: [Self; 8] = [
        Self::Bash,
        Self::Zsh,
        Self::Fish,
        Self::Nushell,
        Self::PowerShell,
        Self::Elvish,
        Self::Xonsh,
        Self::Oils,
    ];

    pub const fn name(self) -> &'static str {
        match self {
            Self::Bash => "bash",
            Self::Zsh => "zsh",
            Self::Fish => "fish",
            Self::Nushell => "nushell",
            Self::PowerShell => "powershell",
            Self::Elvish => "elvish",
            Self::Xonsh => "xonsh",
            Self::Oils => "oils",
        }
    }

    /// The shell in use: the parent process when it is a known shell (so a
    /// zsh started from a bash login shell is reported as zsh), else `$SHELL`.
    pub fn detect_current() -> Option<(Self, ShellSource)> {
        if let Some(shell) = parent_process_name().and_then(|n| {
            let n = n.trim_start_matches('-');
            Self::from_binary_path(n)
        }) {
            return Some((shell, ShellSource::ParentProcess));
        }
        let shell_path = std::env::var("SHELL").ok()?;
        Self::from_binary_path(&shell_path).map(|s| (s, ShellSource::ShellEnv))
    }

    fn current() -> Option<Self> {
        Self::detect_current().map(|(s, _)| s)
    }

    fn from_binary_path(path: &str) -> Option<Self> {
        let binary = Path::new(path).file_name()?.to_str()?;
        let binary = binary.strip_suffix(".exe").unwrap_or(binary);
        Self::ALL
            .iter()
            .copied()
            .find(|s| s.binaries().contains(&binary))
    }

    /// Accepts the canonical name and the binary names (`nu`, `pwsh`, `osh`, ...).
    pub fn from_name(name: &str) -> Option<Self> {
        let name = name.trim().to_ascii_lowercase();
        Self::ALL
            .iter()
            .copied()
            .find(|s| s.name() == name || s.binaries().contains(&name.as_str()))
    }

    pub const fn binaries(self) -> &'static [&'static str] {
        match self {
            Self::Bash => &["bash"],
            Self::Zsh => &["zsh"],
            Self::Fish => &["fish"],
            Self::Nushell => &["nu"],
            Self::PowerShell => &["pwsh", "powershell"],
            Self::Elvish => &["elvish"],
            Self::Xonsh => &["xonsh"],
            Self::Oils => &["osh", "ysh"],
        }
    }

    pub fn rc_file_path(self) -> Option<PathBuf> {
        self.rc_file_path_in(&RcEnv::from_process())
    }

    fn rc_file_path_in(self, env: &RcEnv) -> Option<PathBuf> {
        let home = env.home.as_ref();
        match self {
            Self::Zsh => env
                .zdotdir
                .clone()
                .or_else(|| home.cloned())
                .map(|d| d.join(".zshrc")),
            Self::Bash => home.map(|h| h.join(".bashrc")),
            Self::Fish => env.xdg_config().map(|c| c.join("fish/config.fish")),
            // nushell honours XDG_CONFIG_HOME, otherwise the platform dir.
            Self::Nushell => env
                .xdg_config_home
                .clone()
                .or_else(|| env.platform_config.clone())
                .map(|c| c.join("nushell/config.nu")),
            Self::PowerShell => {
                if cfg!(windows) {
                    dirs::document_dir()
                        .map(|d| d.join("PowerShell/Microsoft.PowerShell_profile.ps1"))
                } else {
                    env.xdg_config()
                        .map(|c| c.join("powershell/Microsoft.PowerShell_profile.ps1"))
                }
            }
            Self::Elvish => env.xdg_config().map(|c| c.join("elvish/rc.elv")),
            Self::Xonsh => home.map(|h| h.join(".xonshrc")),
            Self::Oils => env.xdg_config().map(|c| c.join("oils/oshrc")),
        }
    }

    /// Shells that can `eval` the output of `shellfirm init <shell>` at
    /// startup get a one-liner; the others get the hook embedded in the rc file.
    pub const fn uses_eval(self) -> bool {
        matches!(self, Self::Bash | Self::Zsh | Self::Fish | Self::Oils)
    }

    fn rc_snippet(self) -> String {
        match self {
            Self::Zsh => r#"eval "$(shellfirm init zsh)""#.to_string(),
            Self::Bash => r#"eval "$(shellfirm init bash)""#.to_string(),
            Self::Fish => "shellfirm init fish | source".to_string(),
            Self::Oils => r#"eval "$(shellfirm init oils)""#.to_string(),
            Self::Nushell | Self::PowerShell | Self::Elvish | Self::Xonsh => {
                self.hook().to_string()
            }
        }
    }

    /// The exact block written to the rc file.
    fn rc_block(self) -> String {
        let snippet = self.rc_snippet();
        if self.uses_eval() {
            format!("\n{MARKER}\n{snippet}\n")
        } else {
            format!("\n{MARKER}\n{snippet}\n{END_MARKER}\n")
        }
    }

    pub const fn hook(self) -> &'static str {
        match self {
            Self::Bash => BASH_HOOK,
            Self::Zsh => ZSH_HOOK,
            Self::Fish => FISH_HOOK,
            Self::Nushell => NUSHELL_HOOK,
            Self::PowerShell => POWERSHELL_HOOK,
            Self::Elvish => ELVISH_HOOK,
            Self::Xonsh => XONSH_HOOK,
            Self::Oils => OILS_HOOK,
        }
    }

    /// Hook text written by earlier shellfirm versions, so `--uninstall` and
    /// upgrades can still find and remove it: embedded blocks, and hooks
    /// pasted by hand (`shellfirm init bash >> ~/.bashrc` in 0.3.x).
    const fn legacy_hooks(self) -> &'static [&'static str] {
        match self {
            Self::Nushell => &[legacy::NUSHELL_HOOK],
            Self::PowerShell => &[legacy::POWERSHELL_HOOK],
            Self::Elvish => &[legacy::ELVISH_HOOK],
            Self::Xonsh => &[legacy::XONSH_HOOK],
            Self::Bash => &[legacy::BASH_HOOK],
            Self::Zsh => &[legacy::ZSH_HOOK],
            Self::Fish => &[legacy::FISH_HOOK],
            Self::Oils => &[legacy::OILS_HOOK],
        }
    }

    /// A line that only an old (0.3.x) hook contains.
    const fn legacy_signatures(self) -> &'static [&'static str] {
        match self {
            Self::Bash | Self::Oils => &["trap '_shellfirm_hook' DEBUG", "_shellfirm_hook()"],
            Self::Zsh => &[
                "zle -N accept-line shellfirm-pre-command",
                "shellfirm-pre-command()",
            ],
            Self::Fish => &["function _shellfirm_check", "bind \\r _shellfirm_check"],
            Self::Nushell | Self::PowerShell | Self::Elvish | Self::Xonsh => &[],
        }
    }

    /// Command that reloads the shell so the hook becomes active.
    pub const fn activate_hint(self) -> &'static str {
        match self {
            Self::Bash => "exec bash",
            Self::Zsh => "exec zsh",
            Self::Fish => "exec fish",
            Self::Oils => "exec osh",
            Self::Nushell => "exec nu",
            Self::Elvish => "exec elvish",
            Self::Xonsh => "exec xonsh",
            Self::PowerShell => ". $PROFILE",
        }
    }

    /// Known protection gaps that the user must be told about at install time.
    pub const fn limitation(self) -> Option<&'static str> {
        match self {
            Self::Nushell => Some(
                "nushell cannot cancel a command from a hook. shellfirm can only stop external \
                 programs (by clearing PATH for the line); for lines that use nushell built-ins \
                 (rm, mv, cp, save, ...), custom commands or programs run by path it only warns, \
                 and they still run. Aliases are expanded and the source of small custom \
                 commands is checked with the call.",
            ),
            Self::Elvish => Some(
                "elvish functions (`fn`) are not expanded: calling one is checked as typed, not \
                 by what its body runs. Commands typed directly are checked.",
            ),
            Self::Xonsh => Some(
                "xonsh aliases (string, list and callable) are expanded; `![...]`, `$[...]`, \
                 `!(...)`, `$(...)`, `$VAR` as the command, literal `@([...])` lists and the \
                 strings given to os.system / subprocess / evalx are checked (one that is only \
                 known when it runs is challenged); other Python code that starts programs \
                 (computed lists) is not.",
            ),
            Self::Oils => Some(
                "the Oils hook uses a DEBUG trap and reads the command from history; commands \
                 hidden from history (HISTCONTROL/HISTIGNORE) are not checked.",
            ),
            _ => None,
        }
    }
}

impl std::fmt::Display for Shell {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.pad(self.name())
    }
}

pub fn command() -> Command {
    Command::new("init")
        .about("Set up shell integration")
        .long_about(
            "Install the shellfirm hook for your shell.\n\n\
             Without arguments, installs the hook for the shell you are using (asks first), \
             and offers the other shells found on the system. --all installs for every \
             shell found. Specify a shell name to install for that shell only.\n\
             Use --dry-run to preview changes (install or uninstall) without writing anything.\n\n\
             When stdout is not a terminal (e.g. eval \"$(shellfirm init zsh)\"), prints the \
             hook to stdout instead of installing, unless --yes is given (for rollout \
             scripts: `shellfirm init zsh --yes` installs without a terminal). Without a \
             shell name the shell is taken from the parent process, then $SHELL.",
        )
        .arg(
            Arg::new("shell")
                .help(
                    "Install for a specific shell only: bash, zsh, fish, nushell (nu), \
                     powershell (pwsh), elvish, xonsh, oils (osh, ysh). If omitted, installs \
                     for the current shell.",
                )
                .value_parser(PossibleValuesParser::new(SHELL_NAMES))
                .ignore_case(true)
                .required(false),
        )
        .arg(
            Arg::new("all")
                .long("all")
                .help("Install (or with --uninstall, remove) for every shell found on the system")
                .conflicts_with("shell")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("dry-run")
                .long("dry-run")
                .help("Show what would be done without making any changes")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("uninstall")
                .long("uninstall")
                .help("Remove shellfirm hooks from shell rc files")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("yes")
                .short('y')
                .long("yes")
                .help(
                    "Do not ask: modify rc files without confirmation and skip the setup \
                     questions (a settings file without a protection level gets the recommended \
                     Balanced level, min_severity: Medium). Also installs when stdout is not a \
                     terminal.",
                )
                .action(ArgAction::SetTrue),
        )
}

// Returns `Result` like the other subcommands' `run` (see `shellfirm.rs`).
#[allow(clippy::unnecessary_wraps)]
pub fn run(matches: &ArgMatches) -> Result<shellfirm::CmdExit> {
    let dry_run = matches.get_flag("dry-run");
    let uninstall = matches.get_flag("uninstall");
    let assume_yes = matches.get_flag("yes");
    let all = matches.get_flag("all");
    let explicit_shell = matches.get_one::<String>("shell").map(String::as_str);

    let shell = match explicit_shell.map(|name| validate_shell_arg(Some(name))) {
        Some(Ok(s)) => Some(s),
        Some(Err(exit)) => return Ok(exit),
        None => None,
    };

    // --- Uninstall mode ---
    if uninstall {
        return Ok(shell.map_or_else(
            || run_uninstall_all(dry_run),
            |shell| uninstall_hook(shell, dry_run),
        ));
    }

    // --- Print mode: `eval "$(shellfirm init zsh)"` ---
    if !dry_run && !assume_yes && !std::io::stdout().is_terminal() {
        let Some(shell) = shell.or_else(Shell::current) else {
            return Ok(shellfirm::CmdExit {
                code: exitcode::USAGE,
                message: Some(
                    "Could not detect the shell (parent process or $SHELL). Use: shellfirm init <shell>"
                        .to_string(),
                ),
            });
        };
        let hook = shell.hook();
        if hook.ends_with('\n') {
            print!("{hook}");
        } else {
            println!("{hook}");
        }
        return Ok(ok_exit(None));
    }

    // --- Install mode ---
    Ok(match shell {
        Some(shell) if dry_run => {
            eprintln!(
                "{}",
                ascii_fallback(&format!(
                    "\n{}\n",
                    style("shellfirm — dry run (no changes will be made)").bold()
                ))
            );
            preview_shell(shell);
            ok_exit(Some(
                "\nNo changes made. Run without --dry-run to apply.".into(),
            ))
        }
        Some(shell) => install_hook(shell, assume_yes),
        None if dry_run => run_dry_run_all(all),
        None => run_install_all(assume_yes, all),
    })
}

const fn ok_exit(message: Option<String>) -> shellfirm::CmdExit {
    shellfirm::CmdExit {
        code: exitcode::OK,
        message,
    }
}

/// Ask a yes/no question on stderr and read the answer from stdin. EOF or
/// an unreadable answer counts as "no". Plain text, no terminal escape codes.
fn ask_yes_no(question: &str, default: bool) -> bool {
    let hint = if default { "[Y/n]" } else { "[y/N]" };
    eprint!("{question} {hint} ");
    let _ = std::io::stderr().flush();
    let mut answer = String::new();
    let read = std::io::stdin().lock().read_line(&mut answer);
    match read {
        Ok(0) | Err(_) => {
            eprintln!();
            false
        }
        Ok(_) => match answer.trim().to_ascii_lowercase().as_str() {
            "" => default,
            "y" | "yes" => true,
            _ => false,
        },
    }
}

fn is_interactive() -> bool {
    std::io::stdin().is_terminal() && std::io::stderr().is_terminal()
}

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

fn validate_shell_arg(shell: Option<&str>) -> std::result::Result<Shell, shellfirm::CmdExit> {
    shell.map_or_else(
        || {
            Err(shellfirm::CmdExit {
                code: exitcode::USAGE,
                message: Some(
                    "Could not detect shell. Please specify: shellfirm init <shell>".to_string(),
                ),
            })
        },
        |name| {
            Shell::from_name(name).ok_or_else(|| shellfirm::CmdExit {
                code: exitcode::USAGE,
                message: Some(format!(
                    "Unsupported shell: {name}. Supported: bash, zsh, fish, nushell (nu), \
                     powershell (pwsh), elvish, xonsh, oils (osh)"
                )),
            })
        },
    )
}

// ---------------------------------------------------------------------------
// rc file planning (pure, unit-tested)
// ---------------------------------------------------------------------------

#[derive(Debug, PartialEq, Eq)]
enum InstallPlan {
    /// Nothing to do.
    AlreadyInstalled,
    /// Append this block to the file (creating it if needed).
    Append(String),
    /// An older hook was found; replace the whole file content.
    Upgrade(String),
}

/// Decide what installing `shell`'s hook into a file with `existing` content does.
fn plan_install(shell: Shell, existing: Option<&str>) -> InstallPlan {
    let block = shell.rc_block();
    let Some(content) = existing else {
        return InstallPlan::Append(block);
    };
    if content.contains(block.trim_start_matches('\n')) {
        return InstallPlan::AlreadyInstalled;
    }
    let (without, changed) = remove_shellfirm_block(content, shell);
    if changed {
        let mut new_content = without;
        // An eval-style loader the user wrote by hand still loads the new hook.
        if shell.uses_eval() && has_manual_hook(&new_content, shell) {
            return InstallPlan::Upgrade(new_content);
        }
        if !new_content.is_empty() && !new_content.ends_with('\n') {
            new_content.push('\n');
        }
        new_content.push_str(&block);
        return InstallPlan::Upgrade(new_content);
    }
    if has_manual_hook(content, shell) {
        return InstallPlan::AlreadyInstalled;
    }
    InstallPlan::Append(block)
}

/// True when the rc file already loads shellfirm: our marker, or a
/// hand-written (non-comment) line such as
/// `command -v shellfirm && eval "$(shellfirm init zsh)"`, or a hook pasted
/// by hand (`shellfirm init nu >> config.nu`).
fn has_manual_hook(content: &str, shell: Shell) -> bool {
    if content.contains(MARKER) {
        return true;
    }
    let header = shell.hook().lines().next().unwrap_or_default();
    if !header.is_empty() && content.lines().any(|l| l.trim_end() == header) {
        return true;
    }
    content.lines().any(|line| {
        let line = line.trim_start();
        if line.starts_with('#') {
            return false;
        }
        let Some(idx) = line.find("shellfirm init") else {
            return false;
        };
        let rest = line[idx + "shellfirm init".len()..].trim_start();
        let word: String = rest
            .chars()
            .take_while(char::is_ascii_alphanumeric)
            .collect();
        Shell::from_name(&word) == Some(shell)
    })
}

/// 1-based line number of a hook from shellfirm 0.3.x that is still in the
/// file (one that could not be removed automatically).
fn legacy_hook_line(content: &str, shell: Shell) -> Option<usize> {
    let sigs = shell.legacy_signatures();
    content
        .lines()
        .position(|l| {
            let t = l.trim_start();
            !t.starts_with('#') && sigs.iter().any(|s| t.contains(s))
        })
        .map(|i| i + 1)
}

fn loader_line_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| {
        Regex::new(
            r#"^\s*(?:command -v shellfirm\s*(?:>\s*/dev/null)?\s*(?:2>&1)?\s*&&\s*)?(?:eval\s+"\$\(\s*(?:\S*/)?shellfirm init (\w+)\s*\)"|(?:\S*/)?shellfirm init (\w+)\s*\|\s*source)\s*;?\s*$"#,
        )
        .expect("valid regex")
    })
}

/// True when `line` is a stand-alone line that only loads the hook for
/// `shell` (`eval "$(shellfirm init bash)"`, `shellfirm init fish | source`).
fn is_loader_line(line: &str, shell: Shell) -> bool {
    loader_line_re().captures(line).is_some_and(|c| {
        c.get(1)
            .or_else(|| c.get(2))
            .and_then(|m| Shell::from_name(m.as_str()))
            == Some(shell)
    })
}

/// How many times the file loads the current hook (more than one means
/// double hooks). A 0.3.x hook is reported on its own
/// ([`legacy_hook_line`]), not counted here.
fn hook_load_count(content: &str, shell: Shell) -> usize {
    if shell.uses_eval() {
        // The marked block of an eval shell is itself a loader line.
        content.lines().filter(|l| is_loader_line(l, shell)).count()
    } else {
        content.matches(END_MARKER).count()
    }
}

#[cfg(test)]
fn is_already_installed(rc_path: &Path, shell: Shell) -> bool {
    fs::read_to_string(rc_path)
        .ok()
        .is_some_and(|c| plan_install(shell, Some(&c)) == InstallPlan::AlreadyInstalled)
}

/// Remove every shellfirm block from rc file content.
fn remove_shellfirm_block(content: &str, shell: Shell) -> (String, bool) {
    let mut result = content.to_string();
    let mut changed = false;

    // Marker .. end-marker blocks (embedded hooks from this version on).
    while let Some(end_idx) = result.find(END_MARKER) {
        // The closest MARKER before the end marker opens this block.
        let Some(start) = result[..end_idx].rfind(MARKER) else {
            break;
        };
        let mut end = end_idx + END_MARKER.len();
        if result[end..].starts_with('\n') {
            end += 1;
        }
        // The block was written with a leading newline (see `rc_block`).
        let begin = if result[..start].ends_with('\n') {
            start - 1
        } else {
            start
        };
        result.replace_range(begin..end, "");
        changed = true;
    }

    // One-liner blocks and legacy embedded hooks (marker + text, no end marker).
    let mut texts = vec![shell.rc_snippet()];
    texts.extend(shell.legacy_hooks().iter().map(|s| (*s).to_string()));
    for text in &texts {
        for block in [
            format!("\n{MARKER}\n{text}\n"),
            format!("{MARKER}\n{text}\n"),
        ] {
            if result.contains(block.as_str()) {
                result = result.replace(block.as_str(), "");
                changed = true;
            }
        }
    }

    // Old hooks pasted by hand, without a marker.
    for text in shell.legacy_hooks() {
        for pasted in [format!("{text}\n"), (*text).to_string()] {
            if result.contains(pasted.as_str()) {
                result = result.replace(pasted.as_str(), "");
                changed = true;
            }
        }
    }

    (result, changed)
}

/// Remove stand-alone loader lines (`eval "$(shellfirm init bash)"`) that
/// were written by hand. Lines inside a construct (`if ...; then` on the
/// line before) are kept so the file stays valid.
fn remove_loader_lines(content: &str, shell: Shell) -> (String, Vec<usize>) {
    let mut out = String::with_capacity(content.len());
    let mut removed = Vec::new();
    let mut prev = "";
    for (i, line) in content.split_inclusive('\n').enumerate() {
        let bare = line.trim_end_matches(['\n', '\r']);
        let top_level = !bare.starts_with([' ', '\t'])
            && !["then", "do", "else", "{", "&&", "||", "\\", "|"]
                .iter()
                .any(|t| prev.ends_with(t));
        if top_level && is_loader_line(bare, shell) {
            removed.push(i + 1);
        } else {
            out.push_str(line);
        }
        let t = bare.trim();
        if !t.is_empty() && !t.starts_with('#') {
            prev = t;
        }
    }
    (out, removed)
}

fn friendly_io_error(action: &str, path: &Path, e: &std::io::Error) -> String {
    let hint = if e.kind() == std::io::ErrorKind::PermissionDenied {
        " (permission denied: is the file read-only?)"
    } else {
        ""
    };
    format!("could not {action} {}: {e}{hint}", path.display())
}

// ---------------------------------------------------------------------------
// rc file report (used by `shellfirm doctor`)
// ---------------------------------------------------------------------------

/// What the rc file of a shell says about the hook.
#[derive(Debug, Clone, Default, serde_derive::Serialize)]
pub struct RcReport {
    pub path: Option<PathBuf>,
    pub exists: bool,
    /// The file loads the hook (marker block, loader line or pasted hook).
    pub installed: bool,
    /// How many times the hook is loaded (> 1: double hooks).
    pub load_count: usize,
    /// Line of a shellfirm 0.3.x hook that is still in the file.
    pub legacy_hook_line: Option<usize>,
    /// The rc file does not load the hook, but this other startup file does
    /// (fish `conf.d`, a login file).
    pub loaded_from: Option<PathBuf>,
}

/// Other startup files the shell reads, where a hand-written loader line
/// may live instead of the rc file.
fn other_startup_files(shell: Shell, env: &RcEnv) -> Vec<PathBuf> {
    let home = env.home.clone();
    let mut files = Vec::new();
    match shell {
        Shell::Fish => {
            if let Some(dir) = env.xdg_config().map(|c| c.join("fish/conf.d")) {
                if let Ok(entries) = fs::read_dir(&dir) {
                    let mut found: Vec<PathBuf> = entries
                        .filter_map(std::result::Result::ok)
                        .map(|e| e.path())
                        .filter(|p| p.extension().is_some_and(|e| e == "fish"))
                        .collect();
                    found.sort();
                    files.extend(found);
                }
            }
        }
        Shell::Bash => {
            if let Some(h) = home {
                for f in [".bash_profile", ".bash_login", ".profile", ".bash_aliases"] {
                    files.push(h.join(f));
                }
            }
        }
        Shell::Zsh => {
            if let Some(d) = env.zdotdir.clone().or(home) {
                for f in [".zshenv", ".zprofile", ".zlogin"] {
                    files.push(d.join(f));
                }
            }
        }
        _ => {}
    }
    files
}

pub fn rc_report(shell: Shell) -> RcReport {
    rc_report_in(shell, &RcEnv::from_process())
}

fn rc_report_in(shell: Shell, env: &RcEnv) -> RcReport {
    let path = shell.rc_file_path_in(env);
    let content = path.as_ref().and_then(|p| fs::read_to_string(p).ok());
    let installed = content
        .as_deref()
        .is_some_and(|c| has_manual_hook(c, shell));
    let loaded_from = if installed {
        None
    } else {
        other_startup_files(shell, env)
            .into_iter()
            .find(|f| fs::read_to_string(f).is_ok_and(|c| has_manual_hook(&c, shell)))
    };
    RcReport {
        exists: content.is_some(),
        installed,
        load_count: content.as_deref().map_or(0, |c| hook_load_count(c, shell)),
        legacy_hook_line: content.as_deref().and_then(|c| legacy_hook_line(c, shell)),
        loaded_from,
        path,
    }
}

// ---------------------------------------------------------------------------
// Install
// ---------------------------------------------------------------------------

enum InstallOutcome {
    Installed(String),
    Upgraded(String),
    AlreadyInstalled(String),
    Failed(String),
}

impl InstallOutcome {
    const fn changed(&self) -> bool {
        matches!(self, Self::Installed(_) | Self::Upgraded(_))
    }
}

fn install_hook_quiet(shell: Shell) -> InstallOutcome {
    let Some(rc_path) = shell.rc_file_path() else {
        return InstallOutcome::Failed(format!("could not determine rc file for {shell}"));
    };
    let display = rc_path.display().to_string();

    let existing = if rc_path.exists() {
        match fs::read_to_string(&rc_path) {
            Ok(c) => Some(c),
            Err(e) => return InstallOutcome::Failed(friendly_io_error("read", &rc_path, &e)),
        }
    } else {
        None
    };

    match plan_install(shell, existing.as_deref()) {
        InstallPlan::AlreadyInstalled => InstallOutcome::AlreadyInstalled(display),
        InstallPlan::Upgrade(new_content) => match fs::write(&rc_path, new_content) {
            Ok(()) => InstallOutcome::Upgraded(display),
            Err(e) => InstallOutcome::Failed(friendly_io_error("write", &rc_path, &e)),
        },
        InstallPlan::Append(block) => {
            // Directories that do not exist yet (deepest first).
            let mut new_dirs = Vec::new();
            let mut dir = rc_path.parent();
            while let Some(d) = dir.filter(|d| !d.as_os_str().is_empty() && !d.exists()) {
                new_dirs.push(d.to_path_buf());
                dir = d.parent();
            }
            if let Some(parent) = rc_path.parent() {
                if let Err(e) = fs::create_dir_all(parent) {
                    return InstallOutcome::Failed(friendly_io_error("create", parent, &e));
                }
            }
            let companion = create_companion(shell, &rc_path);
            let block = match existing.as_deref() {
                // A brand-new file: note that shellfirm created it (and its
                // directories and companion file), so `--uninstall` can
                // delete them again.
                None => {
                    let mut notes = new_dirs.iter().fold(String::new(), |mut acc, d| {
                        let _ = writeln!(acc, "{CREATED_DIR_NOTE}{}", d.display());
                        acc
                    });
                    if let Some(f) = &companion {
                        let _ = writeln!(notes, "{CREATED_FILE_NOTE}{}", f.display());
                    }
                    format!("{CREATED_NOTE}\n{notes}{}", block.trim_start_matches('\n'))
                }
                Some("") => block.trim_start_matches('\n').to_string(),
                Some(_) => block,
            };
            let result = fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(&rc_path)
                .and_then(|mut f| f.write_all(block.as_bytes()));
            match result {
                Ok(()) => InstallOutcome::Installed(display),
                Err(e) => InstallOutcome::Failed(friendly_io_error("write", &rc_path, &e)),
            }
        }
    }
}

/// A note about a 0.3.x hook that is still in the rc file after installing.
fn legacy_note(shell: Shell) -> Option<String> {
    let path = shell.rc_file_path()?;
    let content = fs::read_to_string(&path).ok()?;
    let line = legacy_hook_line(&content, shell)?;
    Some(format!(
        "{} {} line {line} still has a hook from an older shellfirm (0.3.x); remove it by hand \
         to avoid double prompts (the new hook disables it at startup where it can).",
        style("note:").yellow().bold(),
        path.display()
    ))
}

fn install_hook(shell: Shell, assume_yes: bool) -> shellfirm::CmdExit {
    let Some(rc_path) = shell.rc_file_path() else {
        return shellfirm::CmdExit {
            code: exitcode::USAGE,
            message: Some(format!(
                "Could not determine rc file for {shell}. Add the following to your shell config manually:\n\n{}",
                shell.hook()
            )),
        };
    };
    let rc_display = rc_path.display().to_string();

    // `shellfirm init zsh` asks the same question as `shellfirm init`.
    if is_interactive() && !assume_yes {
        eprintln!(
            "{}",
            ascii_fallback(&format!(
                "\n{}\n",
                style(format!("shellfirm — install the hook for {shell}")).bold()
            ))
        );
        if matches!(
            preview_shell(shell),
            Preview::WillAdd | Preview::WillUpgrade
        ) {
            eprintln!();
            if !ask_yes_no("Modify this file?", true) {
                return ok_exit(Some("No changes made.".into()));
            }
        }
    }

    // The same layout as `shellfirm init` (one line per shell, a count,
    // then how to activate it), whichever shell and however it is run.
    let (installed, already, errors, mut notes) = install_each(&[shell]);
    if already > 0 {
        if let Some(n) = legacy_note(shell) {
            notes.push(format!("  {n}"));
        }
    }
    let extra = if errors > 0 {
        format!(
            "\n\nAdd this line to your {shell} config ({rc_display}) manually instead:\n\n{}",
            shell.rc_snippet()
        )
    } else {
        String::new()
    };
    summary_exit(&installed, already, errors, &notes, assume_yes, &extra)
}

/// Install for each shell, printing one line per shell. Returns
/// (newly installed, already set up, errors, notes).
fn install_each(shells: &[Shell]) -> (Vec<Shell>, u32, u32, Vec<String>) {
    let mut installed = Vec::new();
    let mut already = 0u32;
    let mut errors = 0u32;
    let mut notes = Vec::new();
    for shell in shells {
        let outcome = install_hook_quiet(*shell);
        let changed = outcome.changed();
        match outcome {
            InstallOutcome::Installed(path) | InstallOutcome::Upgraded(path) => {
                eprintln!(
                    "{}",
                    ascii_fallback(&format!(
                        "  {} {:<12} → {}",
                        style("✓").green().bold(),
                        shell,
                        style(&path).cyan()
                    ))
                );
                installed.push(*shell);
                if let Some(l) = shell.limitation() {
                    notes.push(format!("  {} {shell}: {l}", style("note:").yellow().bold()));
                }
            }
            InstallOutcome::AlreadyInstalled(path) => {
                eprintln!(
                    "{}",
                    ascii_fallback(&format!(
                        "  {} {:<12} → {} (already set up)",
                        style("✓").dim(),
                        shell,
                        style(&path).dim()
                    ))
                );
                already += 1;
            }
            InstallOutcome::Failed(msg) => {
                eprintln!(
                    "{}",
                    ascii_fallback(&format!(
                        "  {} {:<12} → {}",
                        style("✗").red().bold(),
                        shell,
                        style(&msg).red()
                    ))
                );
                errors += 1;
            }
        }
        if changed {
            if let Some(n) = legacy_note(*shell) {
                notes.push(format!("  {n}"));
            }
        }
    }
    (installed, already, errors, notes)
}

/// Shells whose rc file would change.
fn pending_shells(shells: &[Shell]) -> Vec<Shell> {
    shells
        .iter()
        .copied()
        .filter(|s| {
            s.rc_file_path().is_some_and(|p| {
                let existing = fs::read_to_string(p).ok();
                plan_install(*s, existing.as_deref()) != InstallPlan::AlreadyInstalled
            })
        })
        .collect()
}

fn summary_exit(
    installed: &[Shell],
    already: u32,
    errors: u32,
    notes: &[String],
    assume_yes: bool,
    extra: &str,
) -> shellfirm::CmdExit {
    eprintln!();
    for note in notes {
        eprintln!("{note}");
    }
    if !notes.is_empty() {
        eprintln!();
    }
    let new = installed.len();
    let total_protected = new + already as usize;
    let shells = if total_protected == 1 {
        "shell"
    } else {
        "shells"
    };
    let counts = if errors > 0 {
        let errs = if errors == 1 { "error" } else { "errors" };
        format!("{total_protected} {shells} protected ({new} new, {already} already set up, {errors} {errs}).")
    } else {
        format!("{total_protected} {shells} protected ({new} new, {already} already set up).")
    };

    if !assume_yes && !installed.is_empty() {
        if let Err(e) = run_interactive_setup() {
            eprintln!("  {}: {e}", style("warning").yellow());
        }
    }
    // Questions skipped (`--yes`, no terminal, Esc): the recommended level,
    // not the catch-everything default.
    if !installed.is_empty() {
        match apply_recommended_level() {
            Ok(true) => eprintln!(
                "  Protection level: Balanced (Recommended: medium-risk and above). Change it with \
                 `shellfirm config severity`."
            ),
            Ok(false) => {}
            Err(e) => eprintln!("  {}: {e}", style("warning").yellow()),
        }
    }
    let hints = if installed.is_empty() {
        String::new()
    } else {
        format_activate_hints(installed.to_vec())
    };
    shellfirm::CmdExit {
        code: if errors > 0 {
            exitcode::CANTCREAT
        } else {
            exitcode::OK
        },
        message: Some(format!("{counts}{extra}{hints}")),
    }
}

fn run_install_all(assume_yes: bool, all: bool) -> shellfirm::CmdExit {
    let detected = detect_installed_shells();
    let current = if all { None } else { Shell::detect_current() };
    let Some((current, source)) = current else {
        if !all {
            eprintln!(
                "\n{}",
                style("Could not tell which shell you are using; offering every shell found.")
                    .yellow()
            );
        }
        return install_many(&detected, assume_yes);
    };

    eprintln!(
        "{}",
        ascii_fallback(&format!(
            "\n{}\n",
            style(format!(
                "shellfirm — install the hook for {current} (your shell, from the {})",
                source.describe()
            ))
            .bold()
        ))
    );
    let preview = preview_shell(current);
    eprintln!();
    let interactive = is_interactive();

    let mut installed = Vec::new();
    let mut already = 0u32;
    let mut errors = 0u32;
    let mut notes = Vec::new();
    match preview {
        Preview::AlreadyInstalled => already += 1,
        Preview::Unknown => errors += 1,
        Preview::WillAdd | Preview::WillUpgrade => {
            if interactive && !assume_yes && !ask_yes_no("Modify this file?", true) {
                return ok_exit(Some("No changes made.".into()));
            }
            let (i, a, e, n) = install_each(&[current]);
            installed.extend(i);
            already += a;
            errors += e;
            notes.extend(n);
        }
    }

    // Other shells found on the system: only with an explicit yes.
    let others: Vec<Shell> = detected.iter().copied().filter(|s| *s != current).collect();
    let others = pending_shells(&others);
    let mut extra = String::new();
    if !others.is_empty() {
        let names = others
            .iter()
            .map(|s| s.name())
            .collect::<Vec<_>>()
            .join(", ");
        if interactive && !assume_yes {
            eprintln!();
            if ask_yes_no(&format!("Also add the hook for: {names}?"), false) {
                let (i, a, e, n) = install_each(&others);
                installed.extend(i);
                already += a;
                errors += e;
                notes.extend(n);
            }
        } else {
            extra = format!(
                "\nOther shells found ({names}) were not changed: run `shellfirm init <shell>` \
                 or `shellfirm init --all`."
            );
        }
    }
    summary_exit(&installed, already, errors, &notes, assume_yes, &extra)
}

fn install_many(detected: &[Shell], assume_yes: bool) -> shellfirm::CmdExit {
    if detected.is_empty() {
        return ok_exit(Some("No supported shells detected on this system.".into()));
    }

    eprintln!(
        "{}",
        ascii_fallback(&format!(
            "\n{}\n",
            style("shellfirm — install hooks for detected shells").bold()
        ))
    );
    let mut pending = 0;
    for shell in detected {
        if !matches!(
            preview_shell(*shell),
            Preview::AlreadyInstalled | Preview::Unknown
        ) {
            pending += 1;
        }
    }
    print_not_installed(detected);
    eprintln!();

    if pending == 0 {
        return ok_exit(Some("All detected shells are already set up.".into()));
    }

    if is_interactive() && !assume_yes {
        if !ask_yes_no(&format!("Modify the {pending} file(s) listed above?"), true) {
            return ok_exit(Some("No changes made.".into()));
        }
        eprintln!();
    }

    let (installed, already, errors, notes) = install_each(detected);
    summary_exit(&installed, already, errors, &notes, assume_yes, "")
}

/// Format the "restart your shell" hints, with the current shell first and highlighted.
fn format_activate_hints(mut shells: Vec<Shell>) -> String {
    let current = Shell::current();
    if let Some(cur) = current {
        if let Some(pos) = shells.iter().position(|s| *s == cur) {
            shells.remove(pos);
            shells.insert(0, cur);
        }
    }
    let hints: Vec<String> = shells
        .iter()
        .map(|shell| {
            if current == Some(*shell) {
                format!(
                    "  {} {} {}",
                    style(glyph("â–¸", ">")).magenta().bold(),
                    style(shell.activate_hint()).magenta().bold(),
                    style(format!("({shell}, your shell)")).magenta()
                )
            } else if shell.activate_hint().contains(shell.name()) {
                // `exec zsh (zsh)` would say it twice.
                format!("    {}", style(shell.activate_hint()).cyan())
            } else {
                format!(
                    "    {} {}",
                    style(shell.activate_hint()).cyan(),
                    style(format!("({shell})")).dim()
                )
            }
        })
        .collect();

    format!(
        "\n  Restart your shell to activate protection:\n\n{}\n",
        hints.join("\n")
    )
}

fn print_not_installed(detected: &[Shell]) {
    for shell in Shell::ALL {
        if !detected.contains(&shell) {
            eprintln!(
                "{}",
                ascii_fallback(&format!(
                    "  {} {:<12}   {}",
                    style("—").dim(),
                    shell,
                    style("(not installed on system)").dim()
                ))
            );
        }
    }
}

// ---------------------------------------------------------------------------
// --dry-run
// ---------------------------------------------------------------------------

fn run_dry_run_all(all: bool) -> shellfirm::CmdExit {
    let detected = detect_installed_shells();
    eprintln!(
        "{}",
        ascii_fallback(&format!(
            "\n{}\n",
            style("shellfirm — dry run (no changes will be made)").bold()
        ))
    );
    let current = if all { None } else { Shell::current() };
    if let Some(current) = current {
        preview_shell(current);
        let others: Vec<Shell> = detected.iter().copied().filter(|s| *s != current).collect();
        let others = pending_shells(&others);
        if !others.is_empty() {
            let names = others
                .iter()
                .map(|s| s.name())
                .collect::<Vec<_>>()
                .join(", ");
            eprintln!(
                "\n  Other shells found ({names}) are only changed after you agree (or with --all)."
            );
        }
    } else {
        for shell in &detected {
            preview_shell(*shell);
        }
        print_not_installed(&detected);
    }
    ok_exit(Some(
        "\nNo changes made. Run without --dry-run to apply.".into(),
    ))
}

enum Preview {
    AlreadyInstalled,
    WillAdd,
    WillUpgrade,
    Unknown,
}

fn preview_shell(shell: Shell) -> Preview {
    let Some(path) = shell.rc_file_path() else {
        eprintln!(
            "  {} {:<12}   {}",
            style("?").red(),
            shell,
            style("could not determine rc file").red()
        );
        return Preview::Unknown;
    };
    let existing = fs::read_to_string(&path).ok();
    let display = path.display().to_string();
    match plan_install(shell, existing.as_deref()) {
        InstallPlan::AlreadyInstalled => {
            eprintln!(
                "{}",
                ascii_fallback(&format!(
                    "  {} {:<12} → {} {}",
                    style("✓").dim(),
                    shell,
                    style(&display).dim(),
                    style("(already installed)").yellow()
                ))
            );
            Preview::AlreadyInstalled
        }
        InstallPlan::Upgrade(_) => {
            eprintln!(
                "{}",
                ascii_fallback(&format!(
                    "  {} {:<12} → {}",
                    style("→").green().bold(),
                    shell,
                    style(format!("will update the hook in {display}")).green()
                ))
            );
            Preview::WillUpgrade
        }
        InstallPlan::Append(_) => {
            let verb = if existing.is_some() {
                "will add hook to"
            } else {
                "will create"
            };
            eprintln!(
                "{}",
                ascii_fallback(&format!(
                    "  {} {:<12} → {}",
                    style("→").green().bold(),
                    shell,
                    style(format!("{verb} {display}")).green()
                ))
            );
            if shell.uses_eval() {
                eprintln!(
                    "  {:<14}   {}",
                    "",
                    style(format!("+ {}", shell.rc_snippet())).dim()
                );
            } else {
                eprintln!(
                    "  {:<14}   {}",
                    "",
                    style(format!(
                        "+ embedded hook ({} lines)",
                        shell.hook().lines().count()
                    ))
                    .dim()
                );
            }
            Preview::WillAdd
        }
    }
}

// ---------------------------------------------------------------------------
// Interactive first-run setup
// ---------------------------------------------------------------------------

/// Prompt the user to choose a challenge type and protection level.
///
/// Skipped when:
/// - stdin or stderr is not a terminal (piped / non-interactive)
/// - the settings file already chooses a challenge or a protection level
///   (a file that only holds, say, an ignore list, written by `shellfirm
///   config ignore` before `init`, still gets the two questions; the
///   answers are added to it)
fn run_interactive_setup() -> Result<()> {
    if !is_interactive() {
        return Ok(());
    }

    let config = Config::new(None)?;
    if config.setting_file_path.exists() && settings_choose_protection(&config) {
        return Ok(());
    }

    super::config::run_interactive_setup(&config)
}

/// When the settings file does not choose a protection level (no
/// `min_severity`, e.g. `init --yes` or skipped setup questions), set the
/// recommended Balanced level (`min_severity: Medium`). Returns `true` when
/// it was set. A file that cannot be read is left alone.
fn apply_recommended_level() -> Result<bool> {
    let config = Config::new(None)?;
    let Ok(mut root) = config.read_config_as_value() else {
        return Ok(false);
    };
    if !root.is_mapping() {
        return Ok(false);
    }
    if root
        .as_mapping()
        .is_some_and(|m| m.contains_key(serde_yaml::Value::from("min_severity")))
    {
        return Ok(false);
    }
    shellfirm::value_set(
        &mut root,
        "min_severity",
        serde_yaml::to_value(Some(shellfirm::checks::Severity::Medium))?,
    )?;
    config.save_config_from_value(&root)?;
    Ok(true)
}

/// `true` when the settings file sets `challenge` or `min_severity` (or
/// cannot be read: then it is left alone).
fn settings_choose_protection(config: &Config) -> bool {
    config.read_config_as_value().map_or(true, |root| {
        root.as_mapping().is_some_and(|m| {
            ["challenge", "min_severity"]
                .iter()
                .any(|k| m.contains_key(serde_yaml::Value::from(*k)))
        })
    })
}

// ---------------------------------------------------------------------------
// Uninstall
// ---------------------------------------------------------------------------

enum UninstallOutcome {
    Removed(String),
    /// The hook was removed and the file, which `shellfirm init` had
    /// created, was left empty and deleted.
    Deleted(String),
    WouldRemove(String),
    NotInstalled,
    Failed(String),
}

/// Result of removing the hook from rc file content.
#[derive(Debug, PartialEq, Eq)]
struct Removal {
    content: String,
    changed: bool,
    /// The file was created by `shellfirm init` and nothing else is left.
    delete_file: bool,
    /// Directories `shellfirm init` created for the file (deepest first);
    /// removed with the file when they are empty.
    created_dirs: Vec<PathBuf>,
    /// Empty files `shellfirm init` created next to it (nushell `env.nu`);
    /// removed with the file when they are still empty.
    created_files: Vec<PathBuf>,
}

fn plan_uninstall(content: &str, shell: Shell) -> Removal {
    let (without_blocks, mut changed) = remove_shellfirm_block(content, shell);
    let (mut out, loader_lines) = remove_loader_lines(&without_blocks, shell);
    changed |= !loader_lines.is_empty();
    let mut delete_file = false;
    let mut created_dirs = Vec::new();
    let mut created_files = Vec::new();
    if changed {
        if let Some(rest) = out.strip_prefix(CREATED_NOTE) {
            let mut rest = rest.strip_prefix('\n').unwrap_or(rest);
            while let Some(after) = rest.strip_prefix(CREATED_DIR_NOTE) {
                let (dir, tail) = after.split_once('\n').unwrap_or((after, ""));
                created_dirs.push(PathBuf::from(dir));
                rest = tail;
            }
            while let Some(after) = rest.strip_prefix(CREATED_FILE_NOTE) {
                let (file, tail) = after.split_once('\n').unwrap_or((after, ""));
                created_files.push(PathBuf::from(file));
                rest = tail;
            }
            if rest.trim().is_empty() {
                delete_file = true;
            }
            out = rest.to_string();
        }
    }
    Removal {
        content: out,
        changed,
        delete_file,
        created_dirs,
        created_files,
    }
}

/// Remove the files `shellfirm init` created next to `rc_path` (only there,
/// and only while they are still empty).
fn remove_created_files(rc_path: &Path, files: &[PathBuf]) {
    for file in files {
        if file.parent() == rc_path.parent()
            && file != rc_path
            && fs::metadata(file).is_ok_and(|m| m.is_file() && m.len() == 0)
        {
            let _ = fs::remove_file(file);
        }
    }
}

/// Remove the (empty) directories `shellfirm init` created for `rc_path`.
/// Only ancestors of the rc file are touched, and never a non-empty one.
fn remove_created_dirs(rc_path: &Path, dirs: &[PathBuf]) {
    for dir in dirs {
        if rc_path.starts_with(dir) && dir != rc_path {
            // Fails (and is ignored) when the directory is not empty.
            let _ = fs::remove_dir(dir);
        }
    }
}

fn uninstall_hook_quiet(shell: Shell, dry_run: bool) -> UninstallOutcome {
    let Some(rc_path) = shell.rc_file_path() else {
        return UninstallOutcome::NotInstalled;
    };
    if !rc_path.exists() {
        return UninstallOutcome::NotInstalled;
    }

    let display = rc_path.display().to_string();
    let content = match fs::read_to_string(&rc_path) {
        Ok(c) => c,
        Err(e) => return UninstallOutcome::Failed(friendly_io_error("read", &rc_path, &e)),
    };

    let removal = plan_uninstall(&content, shell);
    if !removal.changed {
        return UninstallOutcome::NotInstalled;
    }
    if dry_run {
        return UninstallOutcome::WouldRemove(display);
    }

    if removal.delete_file {
        return match fs::remove_file(&rc_path) {
            Ok(()) => {
                remove_created_files(&rc_path, &removal.created_files);
                remove_created_dirs(&rc_path, &removal.created_dirs);
                UninstallOutcome::Deleted(display)
            }
            Err(e) => UninstallOutcome::Failed(friendly_io_error("delete", &rc_path, &e)),
        };
    }
    match fs::write(&rc_path, removal.content) {
        Ok(()) => UninstallOutcome::Removed(display),
        Err(e) => UninstallOutcome::Failed(friendly_io_error("write", &rc_path, &e)),
    }
}

/// Print one shell's line of `init --uninstall --all`. Returns
/// `Some(true)` for a hook removed (or that would be), `Some(false)` for an
/// error, `None` when the shell had no hook.
fn print_uninstall_outcome(shell: Shell, outcome: UninstallOutcome) -> Option<bool> {
    let (mark, path, what) = match outcome {
        UninstallOutcome::Removed(path) => (style("✓").green().bold(), path, "(hook removed)"),
        UninstallOutcome::Deleted(path) => (
            style("✓").green().bold(),
            path,
            "(hook removed; the file shellfirm created was empty and is deleted)",
        ),
        UninstallOutcome::WouldRemove(path) => {
            (style("→").yellow().bold(), path, "(would remove hook)")
        }
        UninstallOutcome::NotInstalled => return None,
        UninstallOutcome::Failed(msg) => {
            eprintln!(
                "{}",
                ascii_fallback(&format!(
                    "  {} {:<12} → {}",
                    style("✗").red().bold(),
                    shell,
                    style(&msg).red()
                ))
            );
            return Some(false);
        }
    };
    eprintln!(
        "{}",
        ascii_fallback(&format!(
            "  {mark} {shell:<12} → {} {what}",
            style(&path).cyan()
        ))
    );
    Some(true)
}

fn run_uninstall_all(dry_run: bool) -> shellfirm::CmdExit {
    let title = if dry_run {
        "shellfirm — dry run: hooks that would be removed (no changes will be made)"
    } else {
        "shellfirm — removing the shellfirm hooks"
    };
    eprintln!("\n{}\n", style(ascii_fallback(title)).bold());

    let mut removed = 0u32;
    let mut errors = 0u32;
    let mut leftovers = Vec::new();
    // Files shellfirm created, with the directories created for them
    // (`~/.config` can be shared by several of them).
    let mut created = Vec::new();
    for shell in Shell::ALL {
        if !dry_run {
            if let Some(path) = shell.rc_file_path() {
                if let Ok(content) = fs::read_to_string(&path) {
                    let plan = plan_uninstall(&content, shell);
                    if plan.delete_file && !plan.created_dirs.is_empty() {
                        created.push((path, plan.created_dirs));
                    }
                }
            }
        }
        match print_uninstall_outcome(shell, uninstall_hook_quiet(shell, dry_run)) {
            Some(true) => removed += 1,
            Some(false) => errors += 1,
            None => {}
        }
        if let Some(note) = manual_leftover(shell) {
            leftovers.push(note);
        }
    }
    if !dry_run && removed > 0 {
        remove_recommended_settings();
    }
    for (path, dirs) in &created {
        if !path.exists() {
            remove_created_dirs(path, dirs);
        }
    }
    eprintln!();
    for note in &leftovers {
        eprintln!("  {} {note}", style("note:").yellow().bold());
    }

    if removed == 0 && errors == 0 {
        if leftovers.is_empty() {
            return ok_exit(Some("No shellfirm hooks found in any shell.".into()));
        }
        return ok_exit(Some(
            "No hooks written by `shellfirm init` were found; remove the lines above by hand."
                .into(),
        ));
    }
    let summary = if dry_run {
        format!(
            "{removed} hook(s) would be removed. No changes made. Run without --dry-run to apply."
        )
    } else if errors > 0 {
        format!("Removed hooks from {removed} shell(s) ({errors} error(s)).\nRestart your shells to deactivate.")
    } else {
        format!("Removed hooks from {removed} shell(s).\nRestart your shells to deactivate.")
    };
    shellfirm::CmdExit {
        code: if errors > 0 {
            exitcode::CANTCREAT
        } else {
            exitcode::OK
        },
        message: Some(summary),
    }
}

/// A line that still loads shellfirm after uninstalling (written by hand
/// inside a construct, or an old hook), so the user can remove it. Looks at
/// the file as uninstalling leaves it, so `--dry-run` reports the same
/// leftovers as a real run.
fn manual_leftover(shell: Shell) -> Option<String> {
    let path = shell.rc_file_path()?;
    let raw = fs::read_to_string(&path).ok()?;
    let removal = plan_uninstall(&raw, shell);
    if removal.delete_file {
        return None;
    }
    let content = if removal.changed {
        removal.content
    } else {
        raw
    };
    if let Some(line) = legacy_hook_line(&content, shell) {
        return Some(format!(
            "{} line {line}: a hook from an older shellfirm is still there; remove it by hand.",
            path.display()
        ));
    }
    let line = content.lines().position(|l| {
        let t = l.trim_start();
        !t.starts_with('#') && has_manual_hook(t, shell) && t.contains("shellfirm")
    })?;
    Some(format!(
        "{} line {}: still loads shellfirm (`{}`); remove it by hand.",
        path.display(),
        line + 1,
        content.lines().nth(line).unwrap_or_default().trim()
    ))
}

/// The settings file `init` wrote when the setup questions were skipped
/// (only the recommended level, see [`apply_recommended_level`]): removed
/// with the last hook, with its directory when nothing else is in it.
fn remove_recommended_settings() {
    let Ok(config) = Config::new(None) else {
        return;
    };
    let path = &config.setting_file_path;
    if fs::read_to_string(path).is_ok_and(|c| c.trim() == RECOMMENDED_SETTINGS.trim()) {
        let _ = fs::remove_file(path);
        if let Some(dir) = path.parent() {
            let _ = fs::remove_dir(dir);
        }
    }
}

/// What [`apply_recommended_level`] writes into a new settings file.
const RECOMMENDED_SETTINGS: &str = "min_severity: Medium\n";

fn uninstall_hook(shell: Shell, dry_run: bool) -> shellfirm::CmdExit {
    let Some(rc_path) = shell.rc_file_path() else {
        return ok_exit(Some(ascii_fallback(&format!(
            "No rc file found for {shell} — nothing to remove."
        ))));
    };
    let display = rc_path.display().to_string();
    let leftover = || {
        manual_leftover(shell)
            .map(|n| format!("\n{} {n}", style("note:").yellow().bold()))
            .unwrap_or_default()
    };
    match uninstall_hook_quiet(shell, dry_run) {
        UninstallOutcome::NotInstalled => {
            if rc_path.exists() {
                if let Some(n) = manual_leftover(shell) {
                    return ok_exit(Some(format!(
                        "No hook written by `shellfirm init` was found, but {n}"
                    )));
                }
                ok_exit(Some(format!("No shellfirm hook found in {display}")))
            } else {
                ok_exit(Some(ascii_fallback(&format!("{display} does not exist — nothing to remove."))))
            }
        }
        UninstallOutcome::WouldRemove(path) => ok_exit(Some(format!(
            "Would remove the shellfirm hook from {path}\nNo changes made. Run without --dry-run to apply."
        ))),
        UninstallOutcome::Removed(path) => {
            // The last hook: also the settings `init --yes` wrote.
            if Shell::ALL.iter().all(|s| !rc_report(*s).installed) {
                remove_recommended_settings();
            }
            ok_exit(Some(format!(
            "{} hook removed from {}\nRestart your shell to deactivate.{}",
            style("shellfirm").green().bold(),
            style(path).cyan(),
            leftover(),
        )))
        }
        UninstallOutcome::Deleted(path) => ok_exit(Some(format!(
            "{} hook removed; {} was created by `shellfirm init` and is now empty, so it was deleted.\nRestart your shell to deactivate.",
            style("shellfirm").green().bold(),
            style(path).cyan(),
        ))),
        UninstallOutcome::Failed(msg) => shellfirm::CmdExit {
            code: exitcode::CANTCREAT,
            message: Some(msg),
        },
    }
}

// ---------------------------------------------------------------------------
// Shell detection
// ---------------------------------------------------------------------------

pub fn detect_installed_shells() -> Vec<Shell> {
    let path = std::env::var_os("PATH").unwrap_or_default();
    let dirs: Vec<PathBuf> = std::env::split_paths(&path).collect();
    Shell::ALL
        .iter()
        .copied()
        .filter(|shell| {
            shell.binaries().iter().any(|bin| {
                dirs.iter().any(|d| {
                    let p = d.join(bin);
                    p.is_file() || (cfg!(windows) && d.join(format!("{bin}.exe")).is_file())
                })
            })
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Hook implementations
// ---------------------------------------------------------------------------
/// bash: Enter / Ctrl-J (and Ctrl-O) run a readline macro: first
/// `__shellfirm_accept` (via `bind -x`, which sees the exact text in
/// `READLINE_LINE`), then `accept-line`. On deny the second key is rebound
/// to a no-op so the line stays in the buffer. Aliases and small functions
/// are expanded in every command of the line, and history references line
/// by line. `fc` (also used by Ctrl-X Ctrl-E / vi `v`) checks the edited
/// text and re-runs (`fc -s`, also resolved on Enter however it is
/// spelled); atuin's `enter_accept` is wrapped. One shellfirm process per
/// Enter. No history or DEBUG trap involved (`extdebug` only inside a
/// subshell that looks up where a large function was defined). Shells
/// without readline (or bash < 4) fall back to a DEBUG trap.
const BASH_HOOK: &str = r#"# shellfirm hook for bash
if [[ $- == *i* && -z ${__shellfirm_hook_loaded:-} ]]; then
__shellfirm_hook_loaded=1
__shellfirm_hook_version=14
# Lets `shellfirm doctor` tell whether the shell it runs in loaded the hook
# (SHELLFIRM_HOOK_LOAD changes at every load, also after `exec bash`).
builtin export SHELLFIRM_HOOK_PID=$$ SHELLFIRM_HOOK_LOAD=$$.$RANDOM$RANDOM
__shellfirm_pending=""
__shellfirm_pending_x=""
__shellfirm_cont=""
__shellfirm_warned=""
__shellfirm_bypass_warned=""
__shellfirm_seen=""
__shellfirm_histcmd=""
__shellfirm_expanded=""
__shellfirm_line=""
__shellfirm_fc_ok=""
__shellfirm_block_msg=""
__shellfirm_notes=""
__shellfirm_rows=0
__shellfirm_traps=""
# Longer lines cannot be passed as one argument (Linux MAX_ARG_STRLEN is
# 128 KiB); they are checked through stdin instead and never prompt.
__shellfirm_max_arg=100000

__shellfirm_warn_missing() {
    if [[ -z $__shellfirm_warned ]]; then
        __shellfirm_warned=1
        builtin printf '%s\n' "shellfirm: binary not found in PATH; commands are NOT being checked" >&2
    fi
}

# shellfirm is on PATH but did not start (exit 127: a missing interpreter or
# library). The command runs unchecked, so say so every time.
__shellfirm_warn_broken() {
    builtin printf '%s\n' "shellfirm: warning: $(builtin type -P shellfirm) is installed but could not run (exit $1); this command was NOT checked. Run \`shellfirm doctor\`." >&2
}

# Byte length of $1 in __sf_len.
__shellfirm_len() { local LC_ALL=C; __sf_len=${#1}; }

# shellfirm prints settings, policy and custom-check problems on every call.
# Look once per session (and again when SHELLFIRM_CONFIG_DIR changes), show
# them once, and from then on keep them away from safe lines (they would
# otherwise be mixed into the line editor).
__shellfirm_probe_notes() {
    [[ -n $__shellfirm_notes && $__shellfirm_notes_key == "${SHELLFIRM_CONFIG_DIR-}" ]] && return
    __shellfirm_notes_key=${SHELLFIRM_CONFIG_DIR-}
    local __sf_w
    __sf_w=$(builtin command shellfirm pre-command --test --command=: 2>&1 >/dev/null)
    # Not runnable: the check itself reports it.
    (( $? >= 126 )) && return
    [[ -n $__sf_w ]] && builtin printf '%s\n' "$__sf_w" >&2
    # A SHELLFIRM_CONFIG_DIR that is not a directory: said once (above);
    # the checks then use the default directory, as shellfirm itself does,
    # without saying it again on every line.
    if [[ -n $__sf_w ]] && __shellfirm_stale_dir; then
        __sf_w=$(SHELLFIRM_CONFIG_DIR= builtin command shellfirm pre-command --test --command=: 2>&1 >/dev/null)
    fi
    if [[ -n $__sf_w ]]; then
        __shellfirm_notes=1
    else
        __shellfirm_notes=0
    fi
}
__shellfirm_stale_dir() { [[ -n ${SHELLFIRM_CONFIG_DIR//[[:space:]]/} && ! -d ${SHELLFIRM_CONFIG_DIR} ]]; }

# bash erases the edited line before running a `bind -x` function: put it
# back on the screen (it stays above a challenge). __shellfirm_rows is the
# number of screen rows it takes, so a safe line can be erased again.
# $1 = 1: a continuation line (PS2).
__shellfirm_show_line() {
    __shellfirm_rows=0 __shellfirm_line_rows=0
    # A dumb terminal (Emacs M-x shell) cannot move the cursor: plain text
    # only, so the line is not reprinted (the banner names it).
    [[ -t 2 && ${TERM-} != dumb ]] || return
    local __sf_p __sf_l __sf_w __sf_n=0 __sf_cols=${COLUMNS:-80} __sf_i
    if [[ ${1:-} == 1 ]]; then
        __sf_p=$PS2
    else
        __sf_p=$PS1
    fi
    # Expanding a prompt with $(...) would run its commands again.
    if [[ $__sf_p == *'$('* || $__sf_p == *'`'* ]]; then
        __sf_p=""
    else
        __sf_p=${__sf_p@P}
    fi
    __sf_p=${__sf_p//[$'\001\002']/}
    __sf_p=${__sf_p##*$'\n'}
    local __sf_t=$__sf_p${READLINE_LINE//[$'\001'-$'\010\013'-$'\037\177']/?}
    # A line that ends in a newline (pasted with it): readline leaves the
    # cursor on the row of its last text, not on a row of its own.
    __sf_t=${__sf_t%$'\n'}
    builtin printf '%s\n' "$__sf_t" >&2
    # Screen width of what was printed (colours and other escapes take none).
    local __sf_re=$'\e(\\[[0-9;?]*[ -/]*[@-~]|\\][^\a\e]*(\a|\e\\\\)|.)'
    for (( __sf_i = 0; __sf_i < 64; __sf_i++ )); do
        [[ $__sf_t =~ $__sf_re ]] || break
        __sf_t=${__sf_t/"${BASH_REMATCH[0]}"/}
    done
    (( __sf_cols > 0 )) || __sf_cols=80
    while IFS= builtin read -r __sf_l; do
        __shellfirm_width "$__sf_l"
        (( __sf_n += __sf_w > 0 ? (__sf_w + __sf_cols - 1) / __sf_cols : 1 ))
    done <<< "$__sf_t"
    __shellfirm_rows=$__sf_n __shellfirm_line_rows=$__sf_n
}
__shellfirm_line_rows=0
# Screen columns of $1 in __sf_w: double-width (CJK, emoji) characters take
# two, combining marks and zero-width characters none.
__shellfirm_width() {
    __sf_w=${#1}
    [[ $1 == *[!\ -~]* ]] || return 0
    local __sf_c __sf_i __sf_o __sf_s=$1
    (( ${#__sf_s} > 4000 )) && return 0
    __sf_w=0
    for (( __sf_i = 0; __sf_i < ${#__sf_s}; __sf_i++ )); do
        __sf_c=${__sf_s:__sf_i:1}
        if [[ $__sf_c == [\ -~] ]]; then
            (( __sf_w++ ))
            continue
        fi
        builtin printf -v __sf_o '%d' "'$__sf_c" 2>/dev/null || __sf_o=0
        if (( (__sf_o >= 0x300 && __sf_o <= 0x36F) || (__sf_o >= 0x200B && __sf_o <= 0x200F) \
            || (__sf_o >= 0xFE00 && __sf_o <= 0xFE0F) || __sf_o == 0x2060 || __sf_o == 0xFEFF )); then
            continue
        elif (( (__sf_o >= 0x1100 && __sf_o <= 0x115F) || (__sf_o >= 0x2E80 && __sf_o <= 0x303E) \
            || (__sf_o >= 0x3041 && __sf_o <= 0x33FF) || (__sf_o >= 0x3400 && __sf_o <= 0x4DBF) \
            || (__sf_o >= 0x4E00 && __sf_o <= 0x9FFF) || (__sf_o >= 0xA000 && __sf_o <= 0xA4CF) \
            || (__sf_o >= 0xAC00 && __sf_o <= 0xD7A3) || (__sf_o >= 0xF900 && __sf_o <= 0xFAFF) \
            || (__sf_o >= 0xFE30 && __sf_o <= 0xFE4F) || (__sf_o >= 0xFF00 && __sf_o <= 0xFF60) \
            || (__sf_o >= 0xFFE0 && __sf_o <= 0xFFE6) || (__sf_o >= 0x1F300 && __sf_o <= 0x1F64F) \
            || (__sf_o >= 0x1F900 && __sf_o <= 0x1F9FF) || (__sf_o >= 0x20000 && __sf_o <= 0x3FFFD) )); then
            (( __sf_w += 2 ))
        else
            (( __sf_w++ ))
        fi
    done
}
# Printed by bash before each command it runs (PS0, bash 4.4+): after a
# passed challenge, bash redraws the prompt and the line below the approved
# line; the first PS0 of that line erases this copy. Later commands of the
# same line (a pasted block, `a` newline `b`) get nothing: the subscript
# counts the expansions, so their output is never erased.
__shellfirm_ps0=()
__shellfirm_ps0n=0
__shellfirm_sync_ps0() {
    __shellfirm_ps0=() __shellfirm_ps0n=0
    local __sf_p='${__shellfirm_ps0[__shellfirm_ps0n++]-}'
    # Older hooks (a re-sourced rc file) put `${__shellfirm_ps0}` there.
    [[ ${PS0-} == *'${__shellfirm_ps0}'* ]] && PS0=${PS0//'${__shellfirm_ps0}'/}
    if builtin shopt -q promptvars; then
        [[ ${PS0-} == *"$__sf_p"* ]] || PS0=$__sf_p${PS0-}
    elif [[ ${PS0-} == *"$__sf_p"* ]]; then
        PS0=${PS0//"$__sf_p"/}
    fi
}

# A safe line: erase the reprinted line again (readline redraws it in the
# same place). Only called when pre-command showed nothing: after a
# challenge it exits 3 (SHELLFIRM_SIGNAL_PROMPTED), and the line, the
# challenge and the approved line stay on screen.
__shellfirm_unshow() {
    (( __shellfirm_rows > 0 )) || return
    builtin printf '\e[%dA\r\e[J' "$__shellfirm_rows" >&2
    __shellfirm_rows=0
}

# Reads the input waiting on the terminal into __shellfirm_rest (Enter as a
# newline; control characters and bracketed-paste marks left out).
__shellfirm_rest=""
__shellfirm_drain() {
    local __sf_c __sf_i
    __shellfirm_rest=""
    for (( __sf_i = 0; __sf_i < 64; __sf_i++ )); do
        builtin read -t 0 2>/dev/null || break
        __sf_c=""
        IFS= builtin read -r -s -d '' -t 0.02 __sf_c 2>/dev/null
        [[ -z $__sf_c ]] && break
        __shellfirm_rest+=$__sf_c
        (( ${#__shellfirm_rest} > 65536 )) && break
    done
    __shellfirm_rest=${__shellfirm_rest//$'\e[200~'/}
    __shellfirm_rest=${__shellfirm_rest//$'\e[201~'/}
    __shellfirm_rest=${__shellfirm_rest//$'\r'/$'\n'}
    __shellfirm_rest=${__shellfirm_rest//[$'\001'-$'\010\013'-$'\037\177']/}
    while [[ $__shellfirm_rest == *$'\n' ]]; do
        __shellfirm_rest=${__shellfirm_rest%$'\n'}
    done
    [[ -n ${__shellfirm_rest//[[:space:]]/} ]] || __shellfirm_rest=""
}
# "1 line" / "N lines" (in __sf_rn), "was" / "were" (in __sf_rv) and "it" /
# "them" (in __sf_rp) for __shellfirm_rest.
__shellfirm_rest_count() {
    local __sf_l __sf_k=0
    while IFS= builtin read -r __sf_l; do
        [[ -n ${__sf_l//[[:space:]]/} ]] && (( __sf_k++ ))
    done <<< "$__shellfirm_rest"
    (( __sf_k )) || __sf_k=1
    if (( __sf_k == 1 )); then
        __sf_rn="1 line" __sf_rv=was __sf_rp=it
    else
        __sf_rn="$__sf_k lines" __sf_rv=were __sf_rp=them
    fi
}

# Returns 0 to allow, 1 to block. A missing binary fails open; anything else
# that goes wrong (exit 126, a line too long to pass) fails closed.
# $2 = 1: called from the line editor (one shellfirm process per line: the
# line is reprinted first and erased again when nothing was shown);
# $3 = 1: the line is a continuation line.
__shellfirm_check() {
    if [[ -n ${BASH_CMDS[shellfirm]:-} && ! -x ${BASH_CMDS[shellfirm]} ]]; then
        builtin hash -d shellfirm 2>/dev/null
    fi
    if ! builtin type -P shellfirm >/dev/null 2>&1; then
        __shellfirm_warn_missing
        return 0
    fi
    local __sf_len __sf_rc __sf_out __sf_shown=""
    # Without interactive_comments `#` starts no comment: `rm -rf a # b`
    # removes b too.
    builtin shopt -q interactive_comments || local -x SHELLFIRM_NO_COMMENTS=1
    __shellfirm_rows=0
    __shellfirm_len "$1"
    (( __sf_len > __shellfirm_max_arg )) && __shellfirm_line=""
    if (( __sf_len > __shellfirm_max_arg )); then
        builtin printf '%s' "$1" | builtin command shellfirm check --stdin --exit-code >/dev/null 2>&1
        __sf_rc=$?
        (( __sf_rc == 0 )) && return 0
        if (( __sf_rc == 127 )); then
            __shellfirm_warn_broken 127
            return 0
        fi
        if (( __sf_rc == 10 || __sf_rc == 11 )); then
            builtin printf '\n%s\n' "shellfirm: blocked: this line is too long to confirm interactively ($__sf_len bytes) and it matches a risky pattern. Run it from a script file instead." >&2
        else
            builtin printf '\n%s\n' "shellfirm: blocked: this line ($__sf_len bytes) could not be checked (exit $__sf_rc). Run it from a script file instead." >&2
        fi
        return 1
    fi
    if [[ ${2:-} == 1 ]]; then
        __shellfirm_probe_notes
        # The stale SHELLFIRM_CONFIG_DIR was reported: not again (see above).
        if [[ -n $__shellfirm_notes && $__shellfirm_notes_key == "${SHELLFIRM_CONFIG_DIR-}" ]] && __shellfirm_stale_dir; then
            local -x SHELLFIRM_CONFIG_DIR=
        fi
        if [[ $__shellfirm_notes == 1 ]]; then
            # Dry run first, so warnings and the line only show for risky lines.
            __sf_out=$(builtin command shellfirm pre-command --test --command="$1" 2>/dev/null)
            __sf_rc=$?
            case $__sf_rc in
                0) [[ $__sf_out == *"skipped (below"* ]] || return 0 ;;
                65|77) __shellfirm_show_line "${3:-}"; __shellfirm_rows=0 ;;
            esac
        else
            __shellfirm_show_line "${3:-}"
            __sf_shown=1
        fi
        # Input typed or pasted after this line (not as one bracketed
        # paste) that bash has not read yet: a challenge discards it (it
        # could answer the challenge). Unless the line is plainly safe, it is
        # read first, so it can be named, and kept when the line is not run.
        if builtin read -t 0 2>/dev/null; then
            if [[ $__shellfirm_notes != 1 ]]; then
                builtin command shellfirm pre-command --test --command="$1" >/dev/null 2>&1
                __sf_rc=$?
            fi
            # 65: a challenge, 77: denied.
            (( __sf_rc == 65 || __sf_rc == 77 )) && __shellfirm_drain
        fi
    fi
    # The banner and the audit log show the line as typed, not the function
    # bodies checked with it.
    local -a __sf_la=()
    [[ -n $__shellfirm_line && $__shellfirm_line != "$1" ]] && __sf_la=(--line="$__shellfirm_line")
    __shellfirm_line=""
    SHELLFIRM_SIGNAL_PROMPTED=1 builtin command shellfirm pre-command --command="$1" "${__sf_la[@]}"
    __sf_rc=$?
    # The line runs: what was read ahead of it does not.
    if [[ -n $__shellfirm_rest ]] && (( __sf_rc == 0 || __sf_rc == 3 || __sf_rc == 127 )); then
        local __sf_rn __sf_rv __sf_rp
        __shellfirm_rest_count
        builtin printf '%s\n' "shellfirm: $__sf_rn typed or pasted after this command $__sf_rv not run (a challenge discards waiting input); run $__sf_rp again." >&2
        __shellfirm_rest=""
        __sf_shown=""
    fi
    case $__sf_rc in
        0) [[ -n $__sf_shown ]] && __shellfirm_unshow; return 0 ;;
        3)
            __shellfirm_rows=0
            [[ ${2:-} == 1 ]] && (( __shellfirm_line_rows > 0 )) && __shellfirm_ps0=($'\e['"$__shellfirm_line_rows"$'A\r\e[J') __shellfirm_ps0n=0
            return 0 ;;
        127) [[ -n $__sf_shown ]] && __shellfirm_unshow; __shellfirm_warn_broken 127; return 0 ;;
        126) builtin printf '%s\n' "shellfirm: could not run shellfirm (exit 126); command not run" >&2; return 1 ;;
        *) return 1 ;;
    esac
}

# A body (`declare -f` text without its header) as one line, which is how
# the check reads a definition. A here-document cannot be joined: that body
# stays as it is (its commands are then read as written, `$1` unknown).
# Sets __sf_ol.
__shellfirm_one_line() {
    local __sf_l __sf_o=""
    __sf_ol=$1
    [[ ${1//<<</} == *'<<'* ]] && return
    while IFS= builtin read -r __sf_l; do
        __sf_l=${__sf_l#"${__sf_l%%[![:space:]]*}"}
        __sf_l=${__sf_l%"${__sf_l##*[![:space:]]}"}
        [[ -z $__sf_l ]] && continue
        case $__sf_o in
            ''|*'{'|*'('|*';'|*'|'|*'&'|then|*[[:space:]]then|do|*[[:space:]]do|else|*[[:space:]]else|*[[:space:]]in) __sf_o+=" $__sf_l" ;;
            *) __sf_o+="; $__sf_l" ;;
        esac
    done <<< "$1"
    __sf_ol=${__sf_o# }
}

# What separates the words that can name a function: blanks, control
# characters and ASCII punctuation other than `_ : . @ + -`. Any other
# character (`nettoyé`, `日本`) can be part of a function name.
__shellfirm_nonname='[]!#"$%&'\''()*,/;<=>?[\\^`{|}~[:space:][:cntrl:]]'
# The same as an extended regular expression for one function name.
__shellfirm_namere='[^][:space:][:cntrl:]!#"$%&'\''()*,/;<=>?\^`{|}~0-9-][^][:space:][:cntrl:]!#"$%&'\''()*,/;<=>?\^`{|}~]*'

# The functions named in $1 (and those their bodies name, and so on), each
# once, are sent ahead of the line as definitions (`name() { body; }`): the
# check binds every call's arguments itself (`shift`, `"$@"`, any depth),
# so what it reads is what runs. A body that rewrites its arguments in a way
# the check does not follow (`set --`, `getopts`, `shift` in a loop) is
# also added as written, where its `$1` counts as unknown. Past the budget
# (64 functions, 64 KB, 16 KB for one body) the rest is not read: a marker
# says so (shell:function_depth_limit), so nothing is skipped silently. A
# function over 16 KB that was loaded from a file (nvm, sdkman, conda) is
# treated like a program on PATH: its arguments are checked, its body is not
# read (and gets no marker); one typed or pasted into the session gets it.
# Appends to the caller's __sf_defs, __sf_fx, __sf_fnames / __sf_fbodies
# and __sf_fseen (names already looked at).
__shellfirm_fn_defs() {
    local IFS=$' \t\n' __sf_todo=$1 __sf_w __sf_all __sf_b __sf_hdr __sf_ol __sf_i __sf_n __sf_src
    local -a __sf_ws __sf_new
    while [[ -n $__sf_todo ]]; do
        __sf_ws=(${__sf_todo//$__shellfirm_nonname/ })
        __sf_todo="" __sf_new=()
        for __sf_w in "${__sf_ws[@]}"; do
            [[ $__sf_w == __shellfirm_* || $__sf_w == [0-9.+-]* || " $__sf_fseen " == *" $__sf_w "* ]] && continue
            builtin declare -F -- "$__sf_w" >/dev/null 2>&1 || continue
            __sf_fseen+=" $__sf_w"
            __sf_new+=("$__sf_w")
        done
        __sf_n=${#__sf_new[@]}
        (( __sf_n )) || return 0
        __sf_all=$'\n'$(builtin declare -f -- "${__sf_new[@]}" 2>/dev/null)
        for (( __sf_i = 0; __sf_i < __sf_n; __sf_i++ )); do
            __sf_w=${__sf_new[__sf_i]}
            __sf_hdr=$'\n'"$__sf_w () "$'\n'
            [[ $__sf_all == *"$__sf_hdr"* ]] || continue
            __sf_all=${__sf_all#*"$__sf_hdr"}
            if (( __sf_i + 1 < __sf_n )); then
                __sf_b=${__sf_all%%$'\n'"${__sf_new[__sf_i+1]} () "$'\n'*}
            else
                __sf_b=$__sf_all
            fi
            # shellfirm's own functions (and those that wrap a plugin).
            [[ $__sf_b == *__shellfirm_* ]] && continue
            if (( ${#__sf_b} > 16000 )); then
                __sf_src=$(builtin shopt -s extdebug; builtin declare -F -- "$__sf_w" 2>/dev/null)
                __sf_src=${__sf_src#"$__sf_w "} __sf_src=${__sf_src#* }
                __shellfirm_from_file "$__sf_w" "$__sf_src" || __sf_fx+=$'\n'"shellfirm-function-depth-limit $__sf_w # a function too large for the hook to read ahead"
                continue
            fi
            if (( ${#__sf_fnames[@]} >= 64 || __sf_fbytes + ${#__sf_b} > 64000 )); then
                __sf_fx+=$'\n'"shellfirm-function-depth-limit $__sf_w # calls more functions than the hook reads ahead"
                return 0
            fi
            (( __sf_fbytes += ${#__sf_b} ))
            __sf_fnames+=("$__sf_w") __sf_fbodies+=("$__sf_b")
            # `$K delete ...` in the body, with K set in this session.
            if [[ $__sf_b =~ (^|[\;\&\|\({]|$'\n')[[:space:]]*\$\{?[A-Za-z_] ]]; then
                local __sf_ax __sf_nd __sf_defnames="" __sf_segmark=""
                local -a __sf_fcseg __sf_calls=()
                __shellfirm_segments "$__sf_b"
            fi
            __shellfirm_one_line "$__sf_b"
            __sf_defs+=$'\n'"$__sf_w() $__sf_ol"
            if [[ $__sf_b =~ (^|[[:space:]\;\&\|\({])(set[[:space:]]+(--|[^-+[:space:]])|getopts[[:space:]]|OPTIND) ]] \
                || [[ $__sf_b == *shift* && $__sf_b =~ (^|[[:space:]\;\&\|\({])(while|until|for|select)[[:space:]] ]]; then
                __sf_fx+=$'\n'$__sf_b
            fi
            __sf_todo+=" $__sf_b"
        done
    done
}

# Returns 0 when the function $1 (bash names its source $2) was loaded from
# a file: an absolute path to a regular file, a relative one that named a
# regular file when it was sourced (see __shellfirm_note_relsrc), or
# `source <(cat FILE)` (see __shellfirm_note_catsrc). A function typed or
# pasted at the prompt has the source `main` (and so has one from `eval
# "$(cat FILE)"`), whatever files the current directory holds; process
# substitution is /dev/fd/N.
__shellfirm_from_file() {
    case $2 in
        /dev/fd/*|/proc/*)
            [[ -n $__shellfirm_catfns && " $__shellfirm_catfns " == *" $1:"* ]] || return 1
            local __sf_k
            __sf_k=$(builtin declare -f -- "$1" 2>/dev/null | builtin command cksum 2>/dev/null)
            [[ " $__shellfirm_catfns " == *" $1:${__sf_k%% *} "* ]] ;;
        /*) [[ -f $2 ]] ;;
        main|environment|'') return 1 ;;
        *) [[ " $__shellfirm_relfns " == *" $1|$2 "* ]] ;;
    esac
}
# After a line that sources a file (and at the first prompt, after the rc
# files): the functions whose source is a relative path (`source ./nvm.sh`,
# `. nvm.sh`) that names a regular file from the current directory (or the
# one the line left, `cd d; . ./f; cd -`) are remembered as loaded from that
# file (bash keeps the path as written, which no longer resolves after a
# `cd`).
__shellfirm_relsrc=""
__shellfirm_relfns=""
__shellfirm_note_relsrc() {
    local __sf_l __sf_n __sf_s __sf_all
    __shellfirm_relsrc=""
    __sf_all=$(builtin shopt -s extdebug; builtin declare -F $(builtin compgen -A function) 2>/dev/null)
    while IFS= builtin read -r __sf_l; do
        __sf_n=${__sf_l%% *} __sf_s=${__sf_l#* } __sf_s=${__sf_s#* }
        case $__sf_s in main|environment|''|/*) continue ;; esac
        [[ $__sf_n == __shellfirm_* || " $__shellfirm_relfns " == *" $__sf_n|$__sf_s "* ]] && continue
        [[ -f $__sf_s || ( -n ${OLDPWD-} && -f $OLDPWD/$__sf_s ) ]] || continue
        (( ${#__shellfirm_relfns} < 64000 )) && __shellfirm_relfns+=" $__sf_n|$__sf_s"
    done <<< "$__sf_all"
}
# After a line that was exactly `source <(cat FILE)` (or `.`), FILE a
# regular file: the large functions it defined (source /dev/fd/N) count as
# loaded from FILE. Each is remembered with a checksum of its definition,
# so the same name defined again from another process substitution is not.
__shellfirm_catsrc=""
__shellfirm_catfns=""
__shellfirm_note_catsrc() {
    local __sf_f=$__shellfirm_catsrc __sf_l __sf_n __sf_s __sf_k __sf_all
    __shellfirm_catsrc=""
    [[ -f $__sf_f ]] || return
    __sf_all=$(builtin shopt -s extdebug; builtin declare -F $(builtin compgen -A function) 2>/dev/null)
    while IFS= builtin read -r __sf_l; do
        __sf_n=${__sf_l%% *} __sf_s=${__sf_l#* } __sf_s=${__sf_s#* }
        [[ $__sf_s == /dev/fd/* || $__sf_s == /proc/* ]] || continue
        [[ $__sf_n == __shellfirm_* ]] && continue
        __sf_k=$(builtin declare -f -- "$__sf_n" 2>/dev/null | builtin command cksum 2>/dev/null)
        (( ${#__shellfirm_catfns} < 64000 )) && __shellfirm_catfns+=" $__sf_n:${__sf_k%% *}"
    done <<< "$__sf_all"
}

# `fc` / `eval` in the body of a function the line calls (`rr() { command
# fc -s "$@"; }`) are resolved like typed ones, with the call's arguments in
# place. Appends to the caller's __sf_bfcseg / __sf_bnd.
__shellfirm_body_fc() {
    local __sf_k __sf_j __sf_i __sf_b __sf_args
    local -a __sf_av
    for (( __sf_k = 0; __sf_k < ${#__sf_calls[@]}; __sf_k += 2 )); do
        __sf_b="" __sf_args=${__sf_calls[__sf_k+1]}
        for (( __sf_j = 0; __sf_j < ${#__sf_fnames[@]}; __sf_j++ )); do
            [[ ${__sf_fnames[__sf_j]} == "${__sf_calls[__sf_k]}" ]] && { __sf_b=${__sf_fbodies[__sf_j]}; break; }
        done
        [[ $__sf_b == *fc* || $__sf_b == *eval* ]] || continue
        builtin read -r -a __sf_av <<< "$__sf_args"
        for __sf_i in '"$@"' '"${@}"' '"$*"' '"${*}"' '$@' '${@}' '$*' '${*}'; do
            __sf_b=${__sf_b//"$__sf_i"/$__sf_args}
        done
        for __sf_i in 9 8 7 6 5 4 3 2 1; do
            __sf_b=${__sf_b//"\"\${$__sf_i}\""/${__sf_av[__sf_i-1]}}
            __sf_b=${__sf_b//"\"\$$__sf_i\""/${__sf_av[__sf_i-1]}}
            __sf_b=${__sf_b//"\${$__sf_i}"/${__sf_av[__sf_i-1]}}
            __sf_b=${__sf_b//"\$$__sf_i"/${__sf_av[__sf_i-1]}}
        done
        local __sf_ax __sf_nd __sf_defnames="" __sf_segmark=""
        local -a __sf_fcseg __sf_calls=()
        __shellfirm_segments "$__sf_b"
        __sf_bfcseg+=("${__sf_fcseg[@]}")
        [[ $__sf_nd == *fc* ]] && __sf_bnd+=$'\n'$__sf_nd
    done
}

# Functions that run by themselves (PROMPT_COMMAND, traps, `bind -x` keys,
# PS0..PS4, bash-preexec's arrays) and the functions they name, however
# deep: a line that (re)defines one of them is checked as if it called it.
# Sets the caller's __sf_reach (names).
__shellfirm_hook_reach() {
    local __sf_roots="${PROMPT_COMMAND[*]} ${precmd_functions[*]} ${preexec_functions[*]} ${PS0-} ${PS1-} ${PS2-} ${PS4-}"
    __sf_roots+=" $(builtin trap -p 2>/dev/null) ${__shellfirm_traps-} $(builtin bind -X 2>/dev/null)"
    local __sf_fseen="" __sf_defs="" __sf_fx="" __sf_fbytes=0
    local -a __sf_fnames=() __sf_fbodies=()
    __shellfirm_fn_defs "$__sf_roots"
    __sf_reach=$__sf_fseen
}

# Splits $1 into the caller's __sf_lead (blanks), __sf_w (first word, quotes
# kept: `x='a b'` is one word) and __sf_after (the rest).
__shellfirm_word() {
    local __sf_wre="^([[:space:]]*)(([^[:space:]'\"\\\\]|\\\\.|'[^']*'|\"([^\"\\\\]|\\\\.)*\")*)"
    [[ $1 =~ $__sf_wre ]]
    __sf_lead=${BASH_REMATCH[1]} __sf_w=${BASH_REMATCH[2]}
    __sf_after=${1:${#BASH_REMATCH[0]}}
    # An unterminated quote: the rest of the text is that word.
    if [[ -z $__sf_w && -n $__sf_after && $__sf_after != [[:space:]]* ]]; then
        __sf_w=$__sf_after __sf_after=""
    fi
}

# The words of $1 with quotes and backslashes removed, in __sf_tw. Returns 1
# when they are not certain: an expansion (`$`, a backtick) or an open quote.
__shellfirm_words() {
    __sf_tw=()
    local __sf_s=$1 __sf_c __sf_q="" __sf_t="" __sf_have="" __sf_i __sf_n=${#1}
    (( __sf_n <= 4000 )) || return 1
    for (( __sf_i = 0; __sf_i < __sf_n; __sf_i++ )); do
        __sf_c=${__sf_s:__sf_i:1}
        if [[ $__sf_q == "'" ]]; then
            if [[ $__sf_c == "'" ]]; then __sf_q=""; else __sf_t+=$__sf_c; fi
            continue
        fi
        if [[ $__sf_q == '"' ]]; then
            case $__sf_c in
                '"') __sf_q="" ;;
                '\')
                    __sf_c=${__sf_s:__sf_i+1:1}
                    case $__sf_c in
                        '$'|'`'|'"'|'\') __sf_t+=$__sf_c; (( __sf_i++ )) ;;
                        $'\n') (( __sf_i++ )) ;;
                        *) __sf_t+='\' ;;
                    esac ;;
                '$'|'`') return 1 ;;
                *) __sf_t+=$__sf_c ;;
            esac
            continue
        fi
        case $__sf_c in
            [[:space:]])
                [[ -n $__sf_have ]] && __sf_tw+=("$__sf_t")
                __sf_t="" __sf_have="" ;;
            "'"|'"') __sf_q=$__sf_c __sf_have=1 ;;
            '\')
                (( __sf_i++ ))
                __sf_c=${__sf_s:__sf_i:1}
                [[ $__sf_c == $'\n' ]] || { __sf_t+=$__sf_c; __sf_have=1; } ;;
            '$'|'`') return 1 ;;
            *) __sf_t+=$__sf_c __sf_have=1 ;;
        esac
    done
    [[ -z $__sf_q ]] || return 1
    [[ -n $__sf_have ]] && __sf_tw+=("$__sf_t")
    return 0
}

# `fc` arguments (in "$@", quotes removed): re-run (`-s`, `-e -`) or edit.
# Sets the caller's __sf_rerun, __sf_list, __sf_edit_kw, __sf_old (the
# commands a re-run runs, after old=new).
__shellfirm_fc_parse() {
    local __sf_a __sf_next="" __sf_pat="" __sf_rep="" __sf_sel=-1 __sf_set=""
    __sf_rerun="" __sf_list="" __sf_old=""
    for __sf_a in "$@"; do
        if [[ -n $__sf_next ]]; then
            __sf_next=""
            [[ $__sf_a == - ]] && __sf_rerun=1
            continue
        fi
        case $__sf_a in
            -e) __sf_next=1 ;;
            -[0-9]*) __sf_sel=$__sf_a ;;
            -*l*) __sf_list=1 ;;
            -*s*) __sf_rerun=1 ;;
            -*) ;;
            *=*)
                if [[ -z $__sf_set ]]; then
                    __sf_set=1; __sf_pat=${__sf_a%%=*}; __sf_rep=${__sf_a#*=}
                else
                    __sf_sel=$__sf_a
                fi ;;
            *) __sf_sel=$__sf_a ;;
        esac
    done
    [[ -n $__sf_list || -z $__sf_rerun ]] && return
    # In a subshell fc skips the newest entry (taken to be the fc command
    # itself); push a dummy entry so it skips that instead.
    __sf_old=$(builtin enable fc 2>/dev/null; builtin history -s :; builtin fc -ln "$__sf_sel" "$__sf_sel" 2>/dev/null)
    __sf_old=${__sf_old#"${__sf_old%%[![:space:]]*}"}
    [[ -n $__sf_old && -n $__sf_pat ]] && __sf_old=${__sf_old//"$__sf_pat"/$__sf_rep}
}

# One command (with `fc` or `eval` in it), word by word after removing
# quotes: `command fc -s 'a=b' x`, `builtin fc -s a\=b x`, and the text
# `eval` runs (expanded and checked like a typed line). Appends to the
# caller's __sf_fx; sets __shellfirm_block_msg for an unsafe `fc`.
__shellfirm_fc_eval() {
    local __sf_seg=$1 __sf_i=0 __sf_kw="" __sf_w __sf_rerun __sf_list __sf_old
    local -a __sf_tw
    # $2 = 1: a command in the body of a function the line calls.
    if ! __shellfirm_words "$__sf_seg"; then
        # Not certain what `command fc` / `builtin fc` would re-run or edit
        # (they skip the checked `fc`): refuse.
        if [[ $__sf_seg =~ (command|builtin)[[:space:]\'\"\\]+[\'\"\\]?f[\'\"\\]?c([[:space:]\'\"]|$) ]]; then
            __shellfirm_block_msg="shellfirm: cannot tell what this \`${BASH_REMATCH[1]} fc\` runs (quotes or expansions in its arguments): it skips the check. Use \`fc\` instead. Command not run."
        fi
        return
    fi
    while (( __sf_i < ${#__sf_tw[@]} )); do
        __sf_w=${__sf_tw[__sf_i]}
        case $__sf_w in
            time|-p|'!'|then|do|else|elif|if|while|until|coproc|[A-Za-z_]*=*) ;;
            [0-9]*[\<\>]*|[\<\>]*|'&>'*)
                # A redirection; a bare operator takes the next word.
                [[ $__sf_w =~ ^[0-9]*(\<|\>|\>\>|\<\<\<|\<\<|\>\||\<\>|\&\>|\&\>\>|\>\&|\<\&)$ ]] && (( __sf_i++ )) ;;
            command|builtin)
                [[ -n $__sf_kw ]] && break
                __sf_kw=$__sf_w ;;
            -*) [[ $__sf_kw == command ]] || break ;;
            *) break ;;
        esac
        (( __sf_i++ ))
    done
    __sf_w=${__sf_tw[__sf_i]:-}
    if [[ $__sf_w == fc ]]; then
        # A typed `command fc` / `builtin fc` read for certain: it may use
        # the builtin while this line runs.
        [[ -n $__sf_kw && -z ${2:-} ]] && __shellfirm_fc_typed=1
        __shellfirm_fc_parse "${__sf_tw[@]:__sf_i+1}"
        [[ -n $__sf_list ]] && return
        if [[ -z $__sf_rerun ]]; then
            [[ -n $__sf_kw ]] && __shellfirm_block_msg="shellfirm: \`$__sf_kw fc\` runs the edited commands without a check; use \`fc\` instead. Command not run."
            return
        fi
        if [[ -n $__sf_old && $'\n'$__shellfirm_fc_ok$'\n' != *$'\n'"$__sf_old"$'\n'* ]]; then
            __shellfirm_fc_ok+=$'\n'$__sf_old
            # The whole line is the re-run: check (and show) just that.
            if [[ ${__sf_cmd//[[:space:]]/} == "${__sf_seg//[[:space:]]/}" ]]; then
                __sf_cmd=$__sf_old
            else
                __sf_fx+=$'\n'$__sf_old
            fi
        fi
    elif [[ $__sf_w == eval && -z $__sf_kw ]] && (( ${#__sf_tw[@]} > __sf_i + 1 && __shellfirm_eval_depth < 3 )); then
        local __sf_saved=$__shellfirm_expanded
        (( __shellfirm_eval_depth++ ))
        __shellfirm_expand "${__sf_tw[*]:__sf_i+1}"
        (( __shellfirm_eval_depth-- ))
        [[ -n $__shellfirm_expanded ]] && __sf_fx+=$'\n'$__shellfirm_expanded
        __shellfirm_expanded=$__sf_saved
    fi
}
__shellfirm_eval_depth=0

# One command of a line (no control operators in it): expand the alias at
# its command position (after reserved words such as `time` / `!` and
# variable assignments) and collect the body of a small function it calls.
# Sets __sf_sx; appends to the caller's __sf_fx.
# The value of alias $1 in the caller's __sf_v; 1 when $1 is not an alias.
# bash 3.2 (macOS /bin/bash) has no BASH_ALIASES: the names are the ones
# __shellfirm_segments read (__sf_alnames) and the value comes from
# `alias NAME`, which prints it quoted for reuse as shell input.
__shellfirm_alias() {
    [[ -n $1 ]] || return 1
    if (( BASH_VERSINFO[0] >= 4 )); then
        [[ -n ${BASH_ALIASES[$1]+set} ]] || return 1
        __sf_v=${BASH_ALIASES[$1]}
        return 0
    fi
    [[ ${__sf_alnames-} == *" $1 "* ]] || return 1
    local __sf_d
    __sf_d=$(builtin alias -- "$1" 2>/dev/null) || return 1
    builtin eval "__sf_v=${__sf_d#*=}"
}
__shellfirm_segment() {
    local __sf_s=$1 __sf_pre="" __sf_w __sf_v __sf_i __sf_lead __sf_after="" __sf_trail="" __sf_tail=""
    __sf_sx=$1
    for (( __sf_i = 0; __sf_i <= 64; __sf_i++ )); do
        if (( __sf_i == 64 )); then
            # More leading words (`A=1 B=2 ... cmd`) than are read ahead.
            __sf_segmark=1
            break
        fi
        __shellfirm_word "$__sf_s"
        case $__sf_w in
            '') return ;;
            time|-p|'!'|then|do|else|elif|if|while|until|coproc|eval|[A-Za-z_]*=*)
                __sf_pre+=$__sf_lead$__sf_w
                __sf_s=$__sf_after ;;
            [0-9]*[\<\>]*|[\<\>]*|'&>'*)
                # A leading redirection (`>/dev/null kd ...`); a bare
                # operator takes the next word with it.
                __sf_pre+=$__sf_lead$__sf_w
                __sf_s=$__sf_after
                if [[ $__sf_w =~ ^[0-9]*(\<|\>|\>\>|\<\<\<|\<\<|\>\||\<\>|\&\>|\&\>\>|\>\&|\<\&)$ ]]; then
                    __shellfirm_word "$__sf_s"
                    __sf_pre+=$__sf_lead$__sf_w
                    __sf_s=$__sf_after
                fi ;;
            *) break ;;
        esac
    done
    if [[ -n ${2:-} ]]; then
        for (( __sf_i = 0; __sf_i < 5; __sf_i++ )); do
            __shellfirm_word "$__sf_s"
            __shellfirm_alias "$__sf_w" || break
            __sf_trail=$__sf_v __sf_tail=$__sf_after
            __sf_s=$__sf_lead$__sf_v$__sf_after
            # `alias ls='ls -l'`: bash does not expand an alias inside itself.
            __sf_v=${__sf_v#"${__sf_v%%[![:space:]]*}"}
            [[ ${__sf_v%%[[:space:]]*} == "$__sf_w" ]] && break
        done
        # An alias ending in a space also expands the next word (`sudo `).
        if [[ $__sf_trail == *[[:space:]] ]]; then
            __shellfirm_word "$__sf_tail"
            if __shellfirm_alias "$__sf_w"; then
                __sf_s=${__sf_s%"$__sf_tail"}$__sf_v$__sf_after
            fi
        fi
    fi
    __shellfirm_word "$__sf_s"
    # A command named by a variable set on an earlier line (`$K delete ...`,
    # `${K}`, `$f` naming a function): its value in this session. (One set
    # on the same line is read by the check itself.)
    if (( ! __sf_indef && ${__sf_vdepth:-0} < 2 )) && [[ $__sf_w =~ ^\$([A-Za-z_][A-Za-z0-9_]*|\{[A-Za-z_][A-Za-z0-9_]*\})$ ]]; then
        local __sf_vn=${BASH_REMATCH[1]#\{}
        __sf_vn=${__sf_vn%\}}
        if [[ $__sf_vn != __sf_* && -n ${!__sf_vn:+set} ]]; then
            local __sf_vv=${!__sf_vn} __sf_sxs=$__sf_sx __sf_vdepth=$(( ${__sf_vdepth:-0} + 1 ))
            if (( ${#__sf_vv} <= 4000 )) && [[ $__sf_vv != *__shellfirm_* ]]; then
                __shellfirm_segment "$__sf_vv$__sf_after" "${2:-}"
                __sf_fx+=$'\n'$__sf_sx
                __sf_sx=$__sf_sxs
                __shellfirm_word "$__sf_s"
            fi
        fi
    fi
    # A call of a function: its definition is sent with the line (see
    # __shellfirm_fn_defs); the arguments are kept for `fc` in its body.
    if (( ! __sf_indef && ${#__sf_calls[@]} < 16 )) && [[ -n $__sf_w && $__sf_w != __shellfirm_* ]] && builtin declare -F -- "$__sf_w" >/dev/null 2>&1; then
        [[ $__sf_after =~ ^[[:space:]]*(.*)$ ]]
        __sf_calls+=("$__sf_w" "${BASH_REMATCH[1]}")
        # After assignments / redirections (`A=1 f x`): the bare call too,
        # which the check binds to the definition.
        [[ -n $__sf_pre ]] && __sf_fx+=$'\n'"$__sf_w ${BASH_REMATCH[1]}"
    fi
    __sf_sx=$__sf_pre$__sf_s
}

# Expand every command of the line (after ; & | && || ( { and newlines).
# Sets __sf_ax; appends to the caller's __sf_calls (function calls),
# __sf_defnames (functions the line defines) and __sf_segmark (aliases past
# the read-ahead limit). $2 = 1: a huge line, only the first commands are
# expanded.
__shellfirm_segments() {
    local __sf_in=$1 __sf_seg __sf_op __sf_n=0 __sf_cap=512 __sf_al="" __sf_sx __sf_b
    local -i __sf_indef=0 __sf_defnext=0
    [[ ${2:-} == 1 ]] && __sf_cap=16
    local __sf_alnames=""
    if (( BASH_VERSINFO[0] >= 4 )); then
        builtin shopt -q expand_aliases && (( ${#BASH_ALIASES[@]} )) && __sf_al=1
    elif builtin shopt -q expand_aliases; then
        # bash 3.2: no BASH_ALIASES (see __shellfirm_alias).
        __sf_alnames=$(builtin compgen -a 2>/dev/null)
        [[ -n $__sf_alnames ]] && __sf_al=1
        __sf_alnames=" ${__sf_alnames//$'\n'/ } "
    fi
    __sf_ax=""
    # __sf_nd: the line without the bodies of the functions it defines.
    __sf_nd=""
    __sf_fcseg=()
    local __sf_re='^([^;&|(){}`'$'\n'']*)([;&|(){}`'$'\n'']*)'
    local __sf_defre='^[[:space:]]*(function[[:space:]]+[^[:space:]]+|'$__shellfirm_namere')[[:space:]]*$'
    while [[ -n $__sf_in ]]; do
        if (( ++__sf_n > __sf_cap )); then
            __sf_ax+=$__sf_in
            # Aliases further on are not expanded: say so (functions are
            # found anywhere on the line).
            [[ -n $__sf_al ]] && __sf_segmark=1
            break
        fi
        [[ $__sf_in =~ $__sf_re ]]
        __sf_seg=${BASH_REMATCH[1]} __sf_op=${BASH_REMATCH[2]}
        __sf_in=${__sf_in:${#BASH_REMATCH[0]}}
        __shellfirm_segment "$__sf_seg" "$__sf_al"
        __sf_ax+=$__sf_sx$__sf_op
        # A definition runs nothing: its `fc` / `eval` are looked at when
        # the function is called.
        if (( ! __sf_indef && ! __sf_defnext )); then
            __sf_nd+=$__sf_sx
            [[ $__sf_sx == *fc* || $__sf_sx == *eval* ]] && __sf_fcseg+=("$__sf_sx")
        fi
        __sf_nd+=$__sf_op
        # `name() {` / `function name {`: the body is a definition.
        if (( ! __sf_indef && ! __sf_defnext )) && [[ $__sf_seg =~ $__sf_defre ]] \
            && [[ $__sf_op == '('* || ( $__sf_seg == *function* && $__sf_op == '{'* ) || ( $__sf_seg == *function* && -z $__sf_op ) ]]; then
            __sf_defnext=1
            __sf_b=${BASH_REMATCH[1]#function}
            __sf_defnames+=" ${__sf_b//[[:space:]]/}"
        fi
        if (( __sf_defnext || __sf_indef )); then
            __sf_b=${__sf_op//[!\{]/}
            (( __sf_indef += ${#__sf_b} ))
            (( ${#__sf_b} )) && __sf_defnext=0
            __sf_b=${__sf_op//[!\}]/}
            (( __sf_indef -= ${#__sf_b} ))
            (( __sf_indef < 0 )) && __sf_indef=0
        fi
    done
}

# `fc -s [old=new] [cmd]` / `fc -e - ...` re-run a history entry: resolve
# what will run, wherever it appears on the line and however it is spelled
# (`command fc`, `builtin fc`, `\fc`, `eval fc ...`). `command fc` and
# `builtin fc` with an editor skip the checked `fc` wrapper: refused.
# Edits the caller's __sf_cmd / __sf_fx.
__shellfirm_resolve_fc() {
    local __sf_s=$1 __sf_i __sf_kw __sf_args __sf_whole __sf_old __sf_rerun __sf_list
    local -a __sf_fa
    local __sf_re='(^|[[:space:];&|(){}`"\])((command|builtin)[[:space:]]+|\\)?fc(([[:space:]][^;&|()`'$'\n'']*)?)([;&|()`'$'\n'']|$)'
    local __sf_one='^[[:space:]]*((command|builtin)[[:space:]]+|\\)?fc([[:space:]][^;&|()`'$'\n'']*)?$'
    for (( __sf_i = 0; __sf_i < 8; __sf_i++ )); do
        [[ $__sf_s =~ $__sf_re ]] || break
        __sf_kw=${BASH_REMATCH[3]} __sf_args=${BASH_REMATCH[4]}
        __sf_s=${__sf_s#*"${BASH_REMATCH[0]}"}
        # Quoted or escaped arguments are resolved word by word (__shellfirm_fc_eval).
        [[ $__sf_args == *[\'\"\\]* ]] && continue
        __sf_whole=""
        [[ $__sf_i == 0 && -z ${2:-} && $1 =~ $__sf_one ]] && __sf_whole=1
        __sf_fa=()
        builtin read -r -a __sf_fa <<< "$__sf_args"
        __shellfirm_fc_parse "${__sf_fa[@]}"
        [[ -n $__sf_list ]] && continue
        if [[ -z $__sf_rerun ]]; then
            [[ -n $__sf_kw ]] && __shellfirm_block_msg="shellfirm: \`$__sf_kw fc\` runs the edited commands without a check; use \`fc\` instead. Command not run."
            continue
        fi
        [[ -z $__sf_old ]] && continue
        __shellfirm_fc_ok+=$'\n'$__sf_old
        if [[ -n $__sf_whole ]]; then
            __sf_cmd=$__sf_old
        else
            __sf_fx+=$'\n'$__sf_old
        fi
    done
}

# The commands an assignment to PROMPT_COMMAND adds ($1 = `[n]` or "", $2 =
# `=` or `+=`, $3 = the value as written), in __sf_v: the new value is
# worked out in a subshell, and the value it had before is left out.
# Returns 1 (the value is then read as text) when the value could run
# something by itself (a command substitution, arithmetic, `${x@P}`) or
# cannot be worked out.
__shellfirm_pc_added() {
    local __sf_t=$3 __sf_ov __sf_nv __sf_re='\[[0-9@*]+\]' __sf_are='\$\{[^}]*:[^-+=?]'
    __sf_v=""
    [[ -z $1 || $1 =~ ^\[[0-9]+\]$ ]] || return 1
    (( BASH_VERSINFO[0] > 4 || ( BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4 ) )) || return 1
    # A PROMPT_COMMAND with the integer or nameref attribute evaluates what
    # is assigned.
    [[ ${PROMPT_COMMAND@a} == *[in]* ]] && return 1
    # Plain subscripts (`${PROMPT_COMMAND[@]}`, `${a[1]}`) only.
    while [[ $__sf_t =~ $__sf_re ]]; do
        __sf_t=${__sf_t/"${BASH_REMATCH[0]}"/}
    done
    [[ $__sf_t == *[\`\[]* || $__sf_t == *'$('* || $__sf_t == *[\<\>]\(* || $__sf_t == *@P* || $__sf_t == *'${!'* ]] && return 1
    [[ $__sf_t =~ $__sf_are ]] && return 1
    [[ $3 == \(* && $3 == *[\;\&\|\<\>$'\n']* ]] && return 1
    local IFS=$'\n'
    __sf_ov="${PROMPT_COMMAND[*]}"
    __sf_nv=$(builtin eval "PROMPT_COMMAND$1$2$3" 2>/dev/null || builtin exit 1; builtin printf '%s' "${PROMPT_COMMAND[*]}") || return 1
    [[ -n $__sf_ov && $__sf_nv == *"$__sf_ov"* ]] && __sf_nv=${__sf_nv/"$__sf_ov"/}
    __sf_v=${__sf_nv#"${__sf_nv%%[![:space:];&|]*}"}
    return 0
}

# Code the line installs to run by itself later is checked now, like a
# PROMPT_COMMAND or a trap: the command of a `bind -x` key binding, and the
# body of a command_not_found_handle defined on the line (as a `{ ... }`
# group). Sets the caller's __sf_auto.
__shellfirm_autorun() {
    local __sf_s=$1 __sf_v __sf_i
    local __sf_re=$'(^|[;&|({[:space:]])bind[[:space:]]+([^;&|]*[[:space:]])?-[A-Za-z]*x[[:space:]]*(\'([^\']*)\'|"(([^"\\\\]|\\\\.)*)")'
    local __sf_kre=$'^[[:space:]]*("([^"\\\\]|\\\\.)*"|[^:[:space:]]+)[[:space:]]*:[[:space:]]*(.*)$'
    for (( __sf_i = 0; __sf_i < 8; __sf_i++ )); do
        [[ $__sf_s =~ $__sf_re ]] || break
        __sf_s=${__sf_s#*"${BASH_REMATCH[0]}"}
        if [[ -n ${BASH_REMATCH[4]} ]]; then
            __sf_v=${BASH_REMATCH[4]}
        else
            __sf_v=${BASH_REMATCH[5]//\\\"/\"}
            __sf_v=${__sf_v//\\\\/\\}
        fi
        [[ $__sf_v =~ $__sf_kre ]] || continue
        __sf_v=${BASH_REMATCH[3]}
        [[ $__sf_v == \"*\" ]] && __sf_v=${__sf_v:1:${#__sf_v}-2}
        [[ -n ${__sf_v//[[:space:]]/} ]] && __sf_auto+=$'\n'$__sf_v
    done
    # PROMPT_COMMAND (string or array, `=f` naming a function, one element
    # `[1]=`, `+=`) and the command substitutions of PS0..PS4 run at every
    # prompt; a trap's command runs on its signal (`trap f DEBUG`).
    local __sf_wd=$'((\'[^\']*\'|"([^"\\\\]|\\\\.)*"|\\$\\{([^{}]|\\{[^{}]*\\})*\\}|\\\\.|[^][:space:];&|()<>\'"\\\\])+)'
    local __sf_pre=$'(^|[;&|({[:space:]])(PROMPT_COMMAND|PS[0-4])(\\[[^]]*\\])?(\\+?=)(\\(([^)]*)\\)|'"$__sf_wd)"
    local __sf_ere=$'\'[^\']*\'|"([^"\\\\]|\\\\.)*"|[^[:space:]]+'
    local __sf_sre=$'\\$\\{PROMPT_COMMAND(\\[[^]]*\\])?(:?[-+=])?(([^{}]|\\{[^{}]*\\})*)\\}'
    local __sf_q1=$'^\'([^\']*)\'$' __sf_q2='^"(([^"\\]|\\.)*)"$'
    local __sf_n __sf_ix __sf_op __sf_val __sf_arr __sf_w __sf_e __sf_j
    __sf_s=$1
    for (( __sf_i = 0; __sf_i < 8; __sf_i++ )); do
        [[ $__sf_s =~ $__sf_pre ]] || break
        __sf_s=${__sf_s#*"${BASH_REMATCH[0]}"}
        __sf_n=${BASH_REMATCH[2]} __sf_ix=${BASH_REMATCH[3]} __sf_op=${BASH_REMATCH[4]}
        __sf_val=${BASH_REMATCH[5]} __sf_arr=${BASH_REMATCH[6]} __sf_w=${BASH_REMATCH[7]}
        # What the assignment adds to PROMPT_COMMAND, however it is spelled
        # (`"${PROMPT_COMMAND:+$PROMPT_COMMAND; }x"`, `[1]=x`, `+=(x)`).
        if [[ $__sf_n == PROMPT_COMMAND ]] && __shellfirm_pc_added "$__sf_ix" "$__sf_op" "$__sf_val"; then
            [[ -n ${__sf_v//[[:space:]]/} ]] && __sf_auto+=$'\n'$__sf_v
            continue
        fi
        if [[ $__sf_val == \(* ]]; then
            # An array: each element is a command.
            __sf_v=""
            while [[ $__sf_arr =~ $__sf_ere ]]; do
                __sf_arr=${__sf_arr#*"${BASH_REMATCH[0]}"}
                __sf_e=${BASH_REMATCH[0]}
                [[ $__sf_e == [\'\"]* ]] && __sf_e=${__sf_e:1:${#__sf_e}-2}
                __sf_v+=$'\n'$__sf_e
            done
        elif [[ $__sf_w =~ $__sf_q1 ]]; then
            __sf_v=${BASH_REMATCH[1]}
        elif [[ $__sf_w =~ $__sf_q2 ]]; then
            __sf_v=${BASH_REMATCH[1]//\\\"/\"}
            __sf_v=${__sf_v//\\\$/\$}
            __sf_v=${__sf_v//\\\`/\`}
        else
            __sf_v=${__sf_w//[\'\"]/}
        fi
        # A prompt string only runs its command substitutions.
        [[ $__sf_n == PS* && $__sf_v != *'$('* && $__sf_v != *'`'* ]] && continue
        if [[ $__sf_n == PROMPT_COMMAND ]]; then
            # Read as text: the earlier value is not part of what it adds.
            for (( __sf_j = 0; __sf_j < 4; __sf_j++ )); do
                [[ $__sf_v =~ $__sf_sre ]] || break
                __sf_v=${__sf_v/"${BASH_REMATCH[0]}"/${BASH_REMATCH[3]}}
            done
            __sf_v=${__sf_v//'$PROMPT_COMMAND'/}
            __sf_v=${__sf_v#"${__sf_v%%[![:space:];&|]*}"}
        fi
        [[ -n ${__sf_v//[[:space:]]/} ]] && __sf_auto+=$'\n'$__sf_v
    done
    local __sf_tre=$'(^|[;&|({[:space:]])trap[[:space:]]+(--[[:space:]]+)?(\'([^\']*)\'|"(([^"\\\\]|\\\\.)*)"|('$__shellfirm_namere'))[[:space:]]+[A-Za-z0-9]'
    __sf_s=$1
    for (( __sf_i = 0; __sf_i < 8; __sf_i++ )); do
        [[ $__sf_s =~ $__sf_tre ]] || break
        __sf_s=${__sf_s#*"${BASH_REMATCH[0]}"}
        __sf_v=${BASH_REMATCH[4]}${BASH_REMATCH[5]}${BASH_REMATCH[7]}
        [[ -n ${__sf_v//[[:space:]]/} && $__sf_v != - ]] && __sf_auto+=$'\n'$__sf_v
    done
    local __sf_dre='(^|[;&|({[:space:]])(function[[:space:]]+)?command_not_found_handle[[:space:]]*(\([[:space:]]*\))?[[:space:]]*\{'
    if [[ $1 =~ $__sf_dre ]]; then
        __sf_auto+=$'\n'${1/"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]}{"}
    fi
    # `${K} delete ...` (the braces would split the command): the session
    # value of K in its place, like `$K` (see __shellfirm_segment).
    local __sf_vre='(^|[;&|(]|'$'\n'')[[:space:]]*\$\{([A-Za-z_][A-Za-z0-9_]*)\}([^;&|'$'\n'']*)'
    __sf_s=$1
    for (( __sf_i = 0; __sf_i < 8; __sf_i++ )); do
        [[ $__sf_s =~ $__sf_vre ]] || break
        __sf_s=${__sf_s#*"${BASH_REMATCH[0]}"}
        __sf_v=${BASH_REMATCH[2]}
        [[ $__sf_v != __sf_* && -n ${!__sf_v:+set} ]] || continue
        __sf_v=${!__sf_v}
        (( ${#__sf_v} <= 4000 )) && [[ $__sf_v != *__shellfirm_* ]] && __sf_auto+=$'\n'$__sf_v${BASH_REMATCH[3]}
    done
}

# Join continuation lines and expand aliases (in every command of the line)
# and `fc -s`, and add the definitions of the functions the line calls, so
# the check sees what bash will actually run (history references are
# expanded by the caller). Sets __shellfirm_expanded ("" = nothing to
# check). $2 = 1: a huge line (the work is capped).
__shellfirm_expand() {
    # Byte-wise matching is enough here and much faster.
    local LC_ALL=C
    local __sf_cmd=${1//$'\\\n'/} __sf_ax __sf_nd __sf_bnd="" __sf_fx="" __sf_seg __sf_fseen="" __sf_shown=""
    local __sf_defs="" __sf_defnames="" __sf_segmark="" __sf_fbytes=0 __sf_n
    local -a __sf_fcseg __sf_bfcseg=() __sf_calls=() __sf_fnames=() __sf_fbodies=()
    if [[ $__sf_cmd == *bind* || $__sf_cmd == *command_not_found_handle* || $__sf_cmd == *'${'[A-Za-z_]* \
        || $__sf_cmd == *PROMPT_COMMAND* || $__sf_cmd == *PS[0-4]* || $__sf_cmd == *trap* ]]; then
        local __sf_auto=""
        __shellfirm_autorun "$__sf_cmd"
        [[ -n $__sf_auto ]] && __sf_shown=$__sf_cmd
        __sf_cmd+=$__sf_auto
    fi
    __shellfirm_segments "$__sf_cmd" "${2:-}"
    __sf_cmd=$__sf_ax
    __shellfirm_expanded=""
    # Only adds text to history: nothing runs.
    if [[ $__sf_cmd =~ ^[[:space:]]*(builtin[[:space:]]+)?history[[:space:]]+-s[[:space:]] && $__sf_cmd != *[\;\|\&\`\$\(\)\<\>]* && $__sf_cmd != *$'\n'* ]]; then
        return
    fi
    [[ -n $__sf_segmark ]] && __sf_fx+=$'\n'"shellfirm-function-depth-limit line # more commands than the hook reads ahead"
    # A function that runs by itself (a hook, a trap, a key binding),
    # redefined on this line: checked as if it was called.
    if [[ -n $__sf_defnames ]]; then
        local __sf_reach=""
        __shellfirm_hook_reach
        for __sf_n in $__sf_defnames; do
            [[ " $__sf_reach " == *" $__sf_n "* ]] && __sf_fx+=$'\n'$__sf_n
        done
    fi
    __shellfirm_fn_defs "$__sf_cmd$__sf_fx"
    (( ${#__sf_calls[@]} )) && __shellfirm_body_fc
    local __sf_fx0=$__sf_fx __sf_cmd0=$__sf_cmd
    [[ $__sf_nd == *fc* ]] && __shellfirm_resolve_fc "$__sf_nd"
    [[ -n $__sf_bnd ]] && __shellfirm_resolve_fc "$__sf_bnd" 1
    for __sf_seg in "${__sf_fcseg[@]:0:8}"; do
        __shellfirm_fc_eval "$__sf_seg"
    done
    for __sf_seg in "${__sf_bfcseg[@]:0:8}"; do
        __shellfirm_fc_eval "$__sf_seg" 1
    done
    # Re-runs and `eval` text added just now can call functions too.
    if [[ $__sf_fx != "$__sf_fx0" || $__sf_cmd != "$__sf_cmd0" ]]; then
        __shellfirm_fn_defs "$__sf_cmd ${__sf_fx#"$__sf_fx0"}"
    fi
    __shellfirm_expanded=${__sf_defs#$'\n'}${__sf_defs:+$'\n'}$__sf_cmd$__sf_fx
    __shellfirm_line=${__sf_shown:-$__sf_cmd}
}

# `fc` and Ctrl-X Ctrl-E / vi `v` (edit-and-execute-command, which runs
# `fc`): the edited text is checked before bash runs it. A blocked edit is
# discarded (fc runs nothing when the editor fails).
__shellfirm_fcedit() {
    local __sf_f=${!#}
    builtin eval "$__shellfirm_fc_editor"' "$__sf_f"' || return
    local __sf_c
    __sf_c=$(<"$__sf_f")
    [[ -z ${__sf_c//[[:space:]]/} ]] && return 0
    __shellfirm_expand "$__sf_c"
    [[ -z $__shellfirm_expanded ]] && return 0
    __shellfirm_check "$__shellfirm_expanded" && return 0
    : > "$__sf_f"
    return 1
}
# `fc -s` / `fc -e -` (re-run) are checked here too, whatever the line looks
# like (`\fc`, `eval fc ...`, `x=1 fc ...`); lines checked on Enter are not
# asked about twice.
if ! builtin declare -F fc >/dev/null; then
fc() {
    local __sf_a __sf_ed="" __sf_next="" __sf_edit=1 __sf_rerun="" __sf_list=""
    local -a __sf_args=()
    for __sf_a in "$@"; do
        if [[ -n $__sf_next ]]; then
            __sf_ed=$__sf_a; __sf_next=""
            [[ $__sf_a == - ]] && { __sf_edit=""; __sf_rerun=1; }
            continue
        fi
        case $__sf_a in
            -e) __sf_next=1; continue ;;
            -[0-9]*) ;;
            -*l*) __sf_edit=""; __sf_list=1 ;;
            -*s*) __sf_edit=""; __sf_rerun=1 ;;
        esac
        __sf_args+=("$__sf_a")
    done
    if [[ -n $__sf_rerun && -z $__sf_list ]]; then
        local __sf_pat="" __sf_rep="" __sf_sel=-1 __sf_old __sf_set=""
        for __sf_a in "${__sf_args[@]}"; do
            [[ $__sf_a == -* && $__sf_a != -[0-9]* ]] && continue
            if [[ -z $__sf_set && $__sf_a == *=* ]]; then
                __sf_set=1; __sf_pat=${__sf_a%%=*}; __sf_rep=${__sf_a#*=}
            else
                __sf_sel=$__sf_a
            fi
        done
        __sf_old=$(builtin enable fc 2>/dev/null; builtin fc -ln "$__sf_sel" "$__sf_sel" 2>/dev/null)
        __sf_old=${__sf_old#"${__sf_old%%[![:space:]]*}"}
        [[ -n $__sf_pat ]] && __sf_old=${__sf_old//"$__sf_pat"/$__sf_rep}
        if [[ -n ${__sf_old//[[:space:]]/} && $'\n'$__shellfirm_fc_ok$'\n' != *$'\n'"$__sf_old"$'\n'* ]]; then
            __shellfirm_expand "$__sf_old"
            if [[ -n $__shellfirm_expanded ]] && ! __shellfirm_check "$__shellfirm_expanded"; then
                return 1
            fi
        fi
    fi
    if [[ -z $__sf_edit ]]; then
        __shellfirm_bfc "$@"
        return
    fi
    local __shellfirm_fc_editor=${__sf_ed:-${FCEDIT:-${EDITOR:-vi}}}
    __shellfirm_bfc -e __shellfirm_fcedit "${__sf_args[@]}"
}
# The fc builtin, which shellfirm keeps turned off (see below).
__shellfirm_bfc() {
    builtin enable fc 2>/dev/null
    builtin fc "$@"
    local __sf_r=$?
    [[ -n $__shellfirm_fc_off ]] && builtin enable -n fc 2>/dev/null
    return $__sf_r
}
__shellfirm_fc_off=""
fi

# atuin's enter_accept runs the chosen command with `eval` inside its own
# `bind -x` widget; check it first (atuin is usually loaded after shellfirm).
__shellfirm_wrap_plugins() {
    if [[ -z ${__shellfirm_atuin_wrapped:-} ]] && builtin declare -F __atuin_accept_line >/dev/null; then
        __shellfirm_atuin_wrapped=1
        local __sf_f
        __sf_f=$(builtin declare -f __atuin_accept_line)
        builtin eval "__shellfirm_orig_atuin_accept_line ${__sf_f#__atuin_accept_line}"
        __atuin_accept_line() {
            builtin export -n READLINE_LINE READLINE_POINT READLINE_MARK 2>/dev/null
            __shellfirm_seen=1
            __shellfirm_fc_ok=""
            __shellfirm_expand "$1"
            if [[ -n $__shellfirm_expanded ]] && ! __shellfirm_check "$__shellfirm_expanded"; then
                return 1
            fi
            __shellfirm_orig_atuin_accept_line "$@"
        }
    fi
}

# Removes a DEBUG-trap hook pasted from shellfirm <= 0.3.10, which would
# prompt a second time and can leave the shell unusable after a cancel.
__shellfirm_disable_legacy() {
    if [[ $(builtin trap -p DEBUG) == *_shellfirm_hook* ]]; then
        builtin trap - DEBUG
        builtin shopt -u extdebug
        if [[ $(builtin declare -p PROMPT_COMMAND 2>/dev/null) != "declare -a"* ]]; then
            PROMPT_COMMAND=${PROMPT_COMMAND//_shellfirm_prompt;/}
            PROMPT_COMMAND=${PROMPT_COMMAND//_shellfirm_prompt/}
        fi
        builtin printf '%s\n' "shellfirm: disabled an old shellfirm hook (DEBUG trap) found in your shell startup files; remove it (run \`shellfirm init bash\` to see where)." >&2
    fi
}

__shellfirm_precmd() {
    __shellfirm_sync_ps0
    __shellfirm_pending=""
    __shellfirm_pending_x=""
    __shellfirm_cont=""
    # The fc builtin stays off (a checked line may have turned it on).
    [[ -n $__shellfirm_fc_off ]] && builtin enable -n fc 2>/dev/null
    __shellfirm_wrap_plugins
    # Enter is looked at by __shellfirm_postcmd, after the other
    # PROMPT_COMMAND entries (a plugin may rebind Enter in one); here only
    # when that is not in place yet.
    if ! __shellfirm_install_postcmd; then
        __shellfirm_postcmd
    fi
}

# Last before the prompt: Enter (or Ctrl-J) rebound since the last look is
# bound to the check again, so the next command is checked.
__shellfirm_postcmd() {
    if [[ -z $__shellfirm_legacy_checked ]]; then
        __shellfirm_legacy_checked=1
        __shellfirm_disable_legacy
        __shellfirm_check_bindings
        __shellfirm_relsrc=1
    elif [[ -n $__shellfirm_histcmd && $HISTCMD != "$__shellfirm_histcmd" && -z $__shellfirm_seen ]]; then
        # A command ran (new history entry) without passing through the
        # Enter binding: something rebound Enter. Say so every time.
        builtin printf '%s\n' "shellfirm: warning: the last command did not pass through shellfirm (Enter was rebound) and was NOT checked; shellfirm bound Enter to the check again." >&2
        __shellfirm_unchecked_seen=1
        __shellfirm_rw_every=1
        __shellfirm_check_bindings
    elif [[ -n $__shellfirm_rebind_maybe || -n $__shellfirm_rw_every ]]; then
        # The line could have rebound Enter (`bind`, `source`), or something
        # rebinds it again and again (a plugin's PROMPT_COMMAND): look now,
        # before the next command runs.
        __shellfirm_check_bindings
    fi
    __shellfirm_rebind_maybe=""
    __shellfirm_seen=""
    __shellfirm_histcmd=$HISTCMD
    [[ -n $__shellfirm_catsrc ]] && __shellfirm_note_catsrc
    [[ -n $__shellfirm_relsrc ]] && __shellfirm_note_relsrc
}
__shellfirm_legacy_checked=""
__shellfirm_rebind_maybe=""
__shellfirm_rw_every=""
# Returns 1 when __shellfirm_postcmd was not the last PROMPT_COMMAND entry
# (it is then made the last one, from the next prompt on).
__shellfirm_install_postcmd() { return 1; }

if [[ ( -o emacs || -o vi ) && ${BASH_VERSINFO[0]:-0} -ge 4 ]]; then

# Prints the keys whose binding no longer goes through shellfirm.
__shellfirm_unbound_keys() {
    local __sf_km __sf_s __sf_k __sf_l __sf_out=""
    for __sf_km in emacs vi-insert vi-command; do
        __sf_s=$'\n'$(builtin bind -m "$__sf_km" -s 2>/dev/null)$'\n'
        for __sf_k in '\C-m' '\C-j'; do
            # The key's own line: the check, then accept-line or the wrapped
            # binding; or a macro that ends in the check (see
            # __shellfirm_fuse_macro).
            __sf_l=${__sf_s#*$'\n'"\"$__sf_k\": \""}
            if [[ $__sf_l == "$__sf_s" ]]; then
                __sf_out+=" $__sf_km:$__sf_k"
                continue
            fi
            __sf_l=${__sf_l%%$'\n'*}
            [[ $__sf_l == '\e[9900~\e[990'[156]'~"' || $__sf_l == *'\e[9900~\e[9901~"' ]] || __sf_out+=" $__sf_km:$__sf_k"
        done
    done
    builtin printf '%s' "$__sf_out"
}
# A macro on Enter that ends with a key bound to accept-line (`"\C-m":
# "\e[7777~\C-x\C-a"`, the first key rewriting the line): what it ends with
# is replaced by the check and accept-line, so the line it wrote is the
# line checked. Sets __sf_spec to the new macro; returns 1 when the macro
# does not end that way.
__shellfirm_fuse_macro() {
    local __sf_m=${2#\"} __sf_l __sf_key
    __sf_m=${__sf_m%\"}
    while IFS= builtin read -r __sf_l; do
        [[ $__sf_l == \"*\":\ accept-line ]] || continue
        __sf_key=${__sf_l#\"} __sf_key=${__sf_key%\": accept-line}
        [[ -z $__sf_key || $__sf_key == '\e[990'* || $__sf_key == '\C-m' || $__sf_key == '\C-j' ]] && continue
        if [[ $__sf_m == *"$__sf_key" && $__sf_m != "$__sf_key" ]]; then
            __sf_spec="\"${__sf_m%"$__sf_key"}\\e[9900~\\e[9901~\""
            return 0
        fi
    done < <(builtin bind -m "$1" -p 2>/dev/null)
    return 1
}
# Enter (or Ctrl-J) rebound after shellfirm loaded (a plugin, `bind
# '"\C-m": ...'`): bind it to the check again, and run that binding after
# the check (a readline function, a macro or a `bind -x` command).
# __shellfirm_rw holds those bindings ("keymap seq kind spec" lines).
__shellfirm_rw=""
__shellfirm_rw_seen=""
__shellfirm_check_bindings() {
    local __sf_bad __sf_e __sf_km __sf_k __sf_seq __sf_l __sf_kind __sf_spec __sf_q __sf_new="" __sf_fail=""
    __sf_bad=$(__shellfirm_unbound_keys)
    [[ -z $__sf_bad ]] && return
    for __sf_e in $__sf_bad; do
        __sf_km=${__sf_e%%:*} __sf_k=${__sf_e#*:}
        if [[ $__sf_k == '\C-m' ]]; then __sf_seq='\e[9905~'; else __sf_seq='\e[9906~'; fi
        __sf_q="\"$__sf_k\": "
        __sf_kind="" __sf_spec=""
        __sf_l=$(builtin bind -m "$__sf_km" -p 2>/dev/null | builtin command grep -F -m1 -- "$__sf_q")
        if [[ -n $__sf_l ]]; then
            __sf_kind=fn __sf_spec=${__sf_l#"$__sf_q"}
        else
            __sf_l=$(builtin bind -m "$__sf_km" -s 2>/dev/null | builtin command grep -F -m1 -- "$__sf_q")
            if [[ -n $__sf_l ]]; then
                __sf_kind=macro __sf_spec=${__sf_l#"$__sf_q"}
            else
                __sf_l=$(builtin bind -m "$__sf_km" -X 2>/dev/null | builtin command grep -F -m1 -- "$__sf_q")
                [[ -n $__sf_l ]] && __sf_kind=x __sf_spec=${__sf_l#"$__sf_q"}
            fi
        fi
        # Unbound, or bound to shellfirm's own sequences: plain accept-line.
        if [[ -z $__sf_kind || $__sf_spec == *9900~* || $__sf_spec == *__shellfirm_* ]]; then
            __sf_kind=fn __sf_spec=accept-line
        fi
        if [[ $__sf_kind == macro ]] && __shellfirm_fuse_macro "$__sf_km" "$__sf_spec"; then
            builtin bind -m "$__sf_km" "\"$__sf_k\": $__sf_spec" 2>/dev/null || { __sf_fail+=" $__sf_e"; continue; }
            __sf_new+=$'\n'"$__sf_km $__sf_seq fn redraw-current-line"
            continue
        fi
        __shellfirm_rw_bind "$__sf_km" "$__sf_seq" "$__sf_kind" "$__sf_spec" || { __sf_fail+=" $__sf_e"; continue; }
        builtin bind -m "$__sf_km" "\"$__sf_k\": \"\\e[9900~$__sf_seq\"" 2>/dev/null || { __sf_fail+=" $__sf_e"; continue; }
        __sf_new+=$'\n'"$__sf_km $__sf_seq $__sf_kind $__sf_spec"
    done
    if [[ -n $__sf_new ]]; then
        # Replace older entries for the same keymap and key.
        local __sf_old __sf_keep=""
        while IFS= builtin read -r __sf_old; do
            [[ -z $__sf_old || $__sf_new == *$'\n'"${__sf_old%%~ *}~ "* ]] && continue
            __sf_keep+=$'\n'$__sf_old
        done <<< "$__shellfirm_rw"
        __shellfirm_rw=$__sf_keep$__sf_new
        for __sf_e in $__sf_bad; do
            [[ " $__shellfirm_rw_seen " == *" $__sf_e "* ]] || __shellfirm_rw_seen+=" $__sf_e"
        done
        # Something rebinds Enter: look before every prompt from now on.
        __shellfirm_rw_every=1
        if [[ -z $__shellfirm_rebound_warned && -z ${__shellfirm_loading:-} ]]; then
            __shellfirm_rebound_warned=1
            builtin printf '%s\n' "shellfirm: Enter was rebound after shellfirm loaded (${__sf_bad# }); shellfirm now checks the line first, then runs that binding." >&2
        fi
    fi
    if [[ -n $__sf_fail && -z $__shellfirm_bypass_warned ]]; then
        __shellfirm_bypass_warned=1
        builtin printf '%s\n' "shellfirm: warning: Enter was rebound after shellfirm loaded (${__sf_fail# }) and shellfirm could not wrap it; commands are NOT being checked. Run \`shellfirm doctor\`." >&2
    fi
}
__shellfirm_rebound_warned=""
# __shellfirm_rw_bind <keymap> <seq> <kind> <spec>: bind seq to the binding.
__shellfirm_rw_bind() {
    case $3 in
        x) builtin bind -m "$1" -x "\"$2\": $4" 2>/dev/null ;;
        *) builtin bind -m "$1" "\"$2\": $4" 2>/dev/null ;;
    esac
}
# $1 = 1: run the re-wrapped bindings after the check; else skip them.
__shellfirm_rw_arm() {
    local __sf_l __sf_km __sf_seq __sf_kind __sf_spec
    while IFS= builtin read -r __sf_l; do
        [[ -z $__sf_l ]] && continue
        __sf_km=${__sf_l%% *} __sf_l=${__sf_l#* }
        __sf_seq=${__sf_l%% *} __sf_l=${__sf_l#* }
        __sf_kind=${__sf_l%% *} __sf_spec=${__sf_l#* }
        if [[ $1 == 1 ]]; then
            __shellfirm_rw_bind "$__sf_km" "$__sf_seq" "$__sf_kind" "$__sf_spec"
        else
            builtin bind -m "$__sf_km" "\"$__sf_seq\": redraw-current-line" 2>/dev/null
        fi
    done <<< "$__shellfirm_rw"
}

# Returns 0 when bash will ask for more input (PS2) instead of running $1.
__shellfirm_incomplete() {
    local __sf_re=$'[][\\\\\'"(){}<|&`]|(^|[[:space:];&|])(if|case|for|select|while|until|function|coproc)([[:space:]]|$)'
    [[ $1 =~ $__sf_re ]] || return 1
    local __sf_err
    __sf_err=$("${BASH:-bash}" --norc --noprofile -O extglob -n -c "$1" 2>&1 >/dev/null)
    case $__sf_err in
        *"unexpected end of file"*|*"unexpected EOF"*|*"here-document at line"*) return 0 ;;
    esac
    local __sf_bs=${1##*[!\\]}
    if [[ -z $__sf_err ]] && (( ${#__sf_bs} % 2 )); then
        # Trailing backslash: a line continuation unless it sits in a comment.
        "${BASH:-bash}" --norc --noprofile -O extglob -n -c "$1"$'\nfi' >/dev/null 2>&1 && return 0
        # After an operator (`a && \`): without the backslash the line is
        # still open (a backslash in a comment would leave it complete).
        __sf_err=$("${BASH:-bash}" --norc --noprofile -O extglob -n -c "${1%\\}" 2>&1 >/dev/null)
        [[ $__sf_err == *"unexpected end of file"* || $__sf_err == *"unexpected EOF"* ]] && return 0
    fi
    return 1
}

# The same for text after history expansion, with aliases expanded too.
__shellfirm_incomplete_expanded() {
    __shellfirm_incomplete "$1" || return 1
    local __sf_ax __sf_nd __sf_defnames="" __sf_segmark="" __sf_fx=""
    local -a __sf_fcseg __sf_calls=()
    __shellfirm_segments "$1"
    [[ $__sf_ax == "$1" ]] && return 0
    __shellfirm_incomplete "$__sf_ax"
}

# Keep __shellfirm_precmd (which resets the multi-line buffer) running before
# every primary prompt, even if PROMPT_COMMAND is reassigned later. Chains
# with bash-preexec and with string or array PROMPT_COMMAND.
# Returns 1 if it had to (re)install it.
__shellfirm_install_precmd() {
    if [[ -n ${bash_preexec_imported:-}${__bp_imported:-} ]]; then
        [[ " ${precmd_functions[*]} " == *" __shellfirm_precmd "* ]] && return 0
        precmd_functions=(__shellfirm_precmd "${precmd_functions[@]}")
        return 1
    fi
    [[ "${PROMPT_COMMAND[*]}" == *__shellfirm_precmd* ]] && return 0
    if [[ $(builtin declare -p PROMPT_COMMAND 2>/dev/null) == "declare -a"* ]]; then
        PROMPT_COMMAND=(__shellfirm_precmd "${PROMPT_COMMAND[@]}")
    else
        PROMPT_COMMAND="__shellfirm_precmd${PROMPT_COMMAND:+$'\n'$PROMPT_COMMAND}"
    fi
    return 1
}
__shellfirm_install_postcmd() {
    local __sf_e
    local -a __sf_keep=()
    if [[ -n ${bash_preexec_imported:-}${__bp_imported:-} ]]; then
        # bash-preexec runs precmd_functions from its first PROMPT_COMMAND
        # entry, and keeps the entries added later (a plugin that rebinds
        # Enter at every prompt) after it: shellfirm's look goes last in
        # PROMPT_COMMAND too, just before bash-preexec's own last entry.
        local __sf_r=0
        if [[ ${precmd_functions[${#precmd_functions[@]}-1]-} != __shellfirm_postcmd ]]; then
            for __sf_e in "${precmd_functions[@]}"; do
                [[ $__sf_e == __shellfirm_postcmd ]] || __sf_keep+=("$__sf_e")
            done
            precmd_functions=("${__sf_keep[@]}" __shellfirm_postcmd)
            __sf_r=1
        fi
        __shellfirm_pc_last || __sf_r=1
        return $__sf_r
    fi
    if [[ $(builtin declare -p PROMPT_COMMAND 2>/dev/null) == "declare -a"* ]]; then
        [[ ${PROMPT_COMMAND[${#PROMPT_COMMAND[@]}-1]} == __shellfirm_postcmd ]] && return 0
        for __sf_e in "${PROMPT_COMMAND[@]}"; do
            [[ $__sf_e == __shellfirm_postcmd ]] || __sf_keep+=("$__sf_e")
        done
        PROMPT_COMMAND=("${__sf_keep[@]}" __shellfirm_postcmd)
        return 1
    fi
    [[ $PROMPT_COMMAND == *$'\n'__shellfirm_postcmd ]] && return 0
    __sf_e=${PROMPT_COMMAND//$'\n'__shellfirm_postcmd/}
    PROMPT_COMMAND=$__sf_e$'\n'__shellfirm_postcmd
    return 1
}

# Under bash-preexec: `__shellfirm_postcmd` as the last PROMPT_COMMAND entry
# before `__bp_interactive_mode` (array) or its last line (string). Returns 1
# when it had to move it.
__shellfirm_pc_last() {
    local __sf_e __sf_s
    local -a __sf_keep=()
    if [[ $(builtin declare -p PROMPT_COMMAND 2>/dev/null) == "declare -a"* ]]; then
        local __sf_n=${#PROMPT_COMMAND[@]} __sf_bp=""
        if [[ ${PROMPT_COMMAND[__sf_n-1]//[[:space:]]/} == __bp_interactive_mode ]]; then
            (( __sf_n >= 2 )) && [[ ${PROMPT_COMMAND[__sf_n-2]} == __shellfirm_postcmd ]] && return 0
        else
            [[ ${PROMPT_COMMAND[__sf_n-1]-} == __shellfirm_postcmd ]] && return 0
        fi
        for __sf_e in "${PROMPT_COMMAND[@]}"; do
            if [[ ${__sf_e//[[:space:]]/} == __bp_interactive_mode ]]; then
                __sf_bp=$__sf_e
            elif [[ $__sf_e != __shellfirm_postcmd ]]; then
                __sf_keep+=("$__sf_e")
            fi
        done
        __sf_keep+=(__shellfirm_postcmd)
        [[ -n $__sf_bp ]] && __sf_keep+=("$__sf_bp")
        PROMPT_COMMAND=("${__sf_keep[@]}")
        return 1
    fi
    __sf_s=${PROMPT_COMMAND-}
    [[ $__sf_s == *$'\n__shellfirm_postcmd\n__bp_interactive_mode' || $__sf_s == *$'\n__shellfirm_postcmd' ]] && return 0
    __sf_s=${__sf_s//$'\n'__shellfirm_postcmd/}
    if [[ $__sf_s =~ ^(.*[^[:space:]\;])[[:space:]\;]*__bp_interactive_mode[[:space:]\;]*$ ]]; then
        __sf_s=${BASH_REMATCH[1]}$'\n__shellfirm_postcmd\n__bp_interactive_mode'
    else
        __sf_s+=$'\n__shellfirm_postcmd'
    fi
    PROMPT_COMMAND=$__sf_s
    return 1
}

__shellfirm_accept() {
    # bash exports READLINE_LINE to programs started here: a huge line would
    # make every exec fail with E2BIG.
    builtin export -n READLINE_LINE READLINE_POINT READLINE_MARK 2>/dev/null
    # The DEBUG / ERR / RETURN traps (see the binding below).
    __shellfirm_traps=${1-}
    builtin bind '"\e[9901~": accept-line'
    builtin bind '"\e[9903~": operate-and-get-next' 2>/dev/null
    [[ -n $__shellfirm_rw ]] && __shellfirm_rw_arm 1
    # starship-style DEBUG-trap timers started at this key press (this
    # `bind -x` command); re-arm them so they time the command, not the check.
    [[ -n ${STARSHIP_PREEXEC_READY+set} ]] && STARSHIP_PREEXEC_READY=true
    __shellfirm_seen=1
    __shellfirm_install_precmd || { __shellfirm_pending=""; __shellfirm_pending_x=""; __shellfirm_cont=""; }
    __shellfirm_fc_ok=""
    __shellfirm_block_msg=""
    __shellfirm_rest=""
    local __sf_line=$READLINE_LINE __sf_full __sf_x __sf_cont=$__shellfirm_cont
    # Only input bash is still collecting (an open quote or block, PS2) is
    # joined with this line. A line bash rejected (history expansion failed:
    # "event not found") ran nothing and is not kept.
    if [[ -n $__sf_cont ]]; then
        __sf_full=$__shellfirm_pending$'\n'$__sf_line
    else
        __sf_full=$__sf_line
        __shellfirm_pending_x=""
    fi
    __shellfirm_cont=""
    __shellfirm_pending=$__sf_full
    # The line can rebind Enter: look at the next prompt.
    [[ $__sf_full == *bind* || $__sf_full == *source* || $__sf_full == *.[[:space:]]* ]] && __shellfirm_rebind_maybe=1
    __shellfirm_catsrc=""
    local __sf_srcre='(^|[;&|({[:space:]])(source|\.)[[:space:]]+[^/<[:space:]]'
    [[ $__sf_full =~ $__sf_srcre ]] && __shellfirm_relsrc=1
    if [[ $__sf_full == *'<(cat'* ]]; then
        local __sf_cre='^[[:space:]]*(source|\.)[[:space:]]+<\([[:space:]]*cat[[:space:]]+([^][:space:];&|()<>`$'"'"'"\\*?[{}]+)[[:space:]]*\)[[:space:]]*$'
        if [[ $__sf_full =~ $__sf_cre ]]; then
            __shellfirm_catsrc=${BASH_REMATCH[2]}
            [[ $__shellfirm_catsrc == '~/'* ]] && __shellfirm_catsrc=$HOME/${__shellfirm_catsrc#'~/'}
        fi
    fi
    __shellfirm_fc_typed=""
    # History references (!!, !$, ^a^b) are expanded line by line, as bash
    # does. `history -p` drops the newest entry unless one was just pushed;
    # push and delete a dummy entry (in the subshell only) to avoid that.
    __sf_x=$__sf_line
    # The history characters can be changed (`histchars='@^#'`).
    local __sf_hc=${histchars-!^#}
    local __sf_h1=${__sf_hc:0:1} __sf_h2=${__sf_hc:1:1}
    if [[ $- == *H* && -n $__sf_h1 && ( $__sf_line == *"$__sf_h1"* || ( -n $__sf_h2 && $__sf_line == "$__sf_h2"* ) ) ]]; then
        __sf_x=$(builtin history -s :; builtin history -d -1; builtin history -p -- "$__sf_line" 2>/dev/null) || __sf_x=$__sf_line
    fi
    [[ -n $__sf_cont ]] && __sf_x=$__shellfirm_pending_x$'\n'$__sf_x
    __shellfirm_pending_x=$__sf_x
    [[ -z ${__sf_full//[[:space:]]/} ]] && return
    local __sf_len
    __shellfirm_len "$__sf_full"
    if (( __sf_len > __shellfirm_max_arg )); then
        # Huge input: no (slow) completeness probe; expansions are capped.
        __shellfirm_expand "$__sf_x" 1
        __shellfirm_check "${__shellfirm_expanded:-$__sf_x}" && return
    else
        # Incomplete input (open quote/block, trailing \): bash shows PS2 and
        # the whole construct is checked once it is complete. bash decides
        # that after history and alias expansion (`!$` or an alias can be
        # the `fi`), so the expanded text is probed.
        if __shellfirm_incomplete_expanded "$__sf_x"; then
            __shellfirm_cont=1
            return
        fi
        __shellfirm_expand "$__sf_x"
        if [[ -n $__shellfirm_block_msg ]]; then
            builtin printf '%s\n' "$__shellfirm_block_msg" >&2
        else
            # A typed `command fc` / `builtin fc` was read and checked: the
            # builtin is on while this line runs (off at the next prompt).
            [[ -n $__shellfirm_fc_typed && -n $__shellfirm_fc_off ]] && builtin enable fc 2>/dev/null
            [[ -z $__shellfirm_expanded ]] && return
            __shellfirm_check "$__shellfirm_expanded" 1 "$__sf_cont" && return
        fi
    fi
    # Blocked: skip accept-line. A single line stays in the buffer for
    # editing; a multi-line construct is discarded with SIGINT (a user INT
    # trap would swallow it, so lift it around the signal).
    builtin bind '"\e[9901~": redraw-current-line'
    builtin bind '"\e[9903~": redraw-current-line'
    [[ -n $__shellfirm_rw ]] && __shellfirm_rw_arm 0
    [[ -n $__shellfirm_fc_off ]] && builtin enable -n fc 2>/dev/null
    __shellfirm_pending=""
    __shellfirm_pending_x=""
    local __sf_rn __sf_rv __sf_rp
    [[ -n $__shellfirm_rest ]] && __shellfirm_rest_count
    if [[ $__sf_full == "$__sf_line" ]]; then
        # The cursor at the end (a binding that rewrote the line before the
        # check leaves it where the old line ended), so Ctrl-U clears it all
        # (also every line of a pasted block: readline edits it as one).
        if [[ -n $__shellfirm_rest ]]; then
            # The lines read ahead of the challenge stay with it.
            READLINE_LINE+=$'\n'$__shellfirm_rest
            builtin printf '%s\n' "shellfirm: the line and the $__sf_rn typed or pasted after it are kept for editing (Ctrl-U clears them all)." >&2
        elif [[ $READLINE_LINE == *$'\n'* ]]; then
            builtin printf '%s\n' "shellfirm: the lines are kept for editing (Ctrl-U clears them all)." >&2
        else
            builtin printf '%s\n' "shellfirm: the line is kept for editing (Ctrl-U clears it)." >&2
        fi
        READLINE_POINT=${#READLINE_LINE}
    else
        [[ -n $__shellfirm_rest ]] && builtin printf '%s\n' "shellfirm: the $__sf_rn typed or pasted after it $__sf_rv discarded too." >&2
        local __sf_int
        __sf_int=$(builtin trap -p INT)
        builtin trap - INT
        builtin kill -INT $$
        [[ -n $__sf_int ]] && builtin eval "$__sf_int"
    fi
    __shellfirm_rest=""
}

__sf_wrap=""
for __sf_km in emacs vi-insert vi-command; do
    # Passing "$_" keeps $_ (last argument of the previous command) intact.
    # A function does not see the DEBUG, ERR and RETURN traps (`trap -p`
    # prints nothing for them there): they are read here, at the top level.
    builtin bind -m "$__sf_km" -x '"\e[9900~": __shellfirm_accept "$(builtin trap -p DEBUG ERR RETURN 2>/dev/null)" "$_"'
    builtin bind -m "$__sf_km" '"\e[9901~": accept-line'
    # Enter bound before shellfirm loaded to something other than
    # accept-line (a macro or a command that rewrites the line): kept, and
    # wrapped below like a binding made later.
    __sf_l=$'\n'$(builtin bind -m "$__sf_km" -s 2>/dev/null; builtin bind -m "$__sf_km" -X 2>/dev/null)
    for __sf_k in '\C-m' '\C-j'; do
        if [[ $__sf_l == *$'\n'"\"$__sf_k\": "* ]]; then
            __sf_wrap=1
        else
            builtin bind -m "$__sf_km" "\"$__sf_k\": \"\\e[9900~\\e[9901~\""
        fi
    done
done
if [[ -n $__sf_wrap ]]; then
    __shellfirm_loading=1
    __shellfirm_check_bindings
    __shellfirm_loading=""
fi
unset __sf_wrap __sf_l __sf_k
# Ctrl-O (operate-and-get-next) runs the line too.
if [[ $(builtin bind -m emacs -q operate-and-get-next 2>/dev/null) == *'"\C-o"'* ]]; then
    builtin bind -m emacs '"\e[9903~": operate-and-get-next'
    builtin bind -m emacs '"\C-o": "\e[9900~\e[9903~"'
fi
unset __sf_km
__shellfirm_install_precmd
__shellfirm_install_postcmd
__shellfirm_disable_legacy
# `bind` that changes what Enter (or Ctrl-J) does: Enter is bound to the
# check again at once. A plugin that rebinds Enter from a PROMPT_COMMAND
# entry that runs after shellfirm's (appended, or loaded later) would
# otherwise leave the first command of a new shell unchecked: the look
# before each prompt runs before that entry.
if ! builtin declare -F bind >/dev/null && [[ -z ${BASH_ALIASES[bind]+set} ]]; then
bind() {
    builtin bind "$@"
    local __sf_r=$? __sf_a __sf_l __sf_need=""
    for __sf_a in "$@"; do
        __sf_l=${__sf_a,,}
        case $__sf_l in
            -*[fru]*) [[ $__sf_l == -[a-z]* ]] && __sf_need=1 ;;
            *c-m*|*c-j*|*'\r'*|*'\n'*|*'\015'*|*'\012'*|*'\x0d'*|*'\x0a'*|*ret*|*lfd*|*newline*|*$'\r'*|*$'\n'*) __sf_need=1 ;;
        esac
    done
    [[ -n $__sf_need && -z ${__shellfirm_loading:-} ]] && __shellfirm_check_bindings
    return $__sf_r
}
fi
# `command fc` / `builtin fc` skip the checked `fc` above, also where the
# line checked on Enter does not show them (in a function, a variable,
# PROMPT_COMMAND or a trap). With the builtin turned off they cannot: bash
# hands `command fc ...` to command_not_found_handle, which runs the checked
# `fc` (in a subshell, as bash runs that handler), and `builtin fc` fails.
if builtin declare -F __shellfirm_bfc >/dev/null && builtin enable -n fc 2>/dev/null; then
    __shellfirm_fc_off=1
    if builtin declare -F command_not_found_handle >/dev/null; then
        __sf_f=$(builtin declare -f command_not_found_handle)
        builtin eval "__shellfirm_orig_cnfh ${__sf_f#command_not_found_handle}"
        unset __sf_f
    fi
    command_not_found_handle() {
        if [[ $1 == fc ]]; then
            builtin shift
            \fc "$@"
            return
        fi
        if builtin declare -F __shellfirm_orig_cnfh >/dev/null; then
            __shellfirm_orig_cnfh "$@"
            return
        fi
        builtin printf 'bash: %s: command not found\n' "$1" >&2
        return 127
    }
fi

else
__shellfirm_check_bindings() { :; }
# Fallback without readline: DEBUG trap (reads the command from history).
if [[ -n $(builtin trap -p DEBUG) ]]; then
    builtin printf '%s\n' "shellfirm: a DEBUG trap is already set; not installing the fallback hook (line editing is off)" >&2
else
__shellfirm_ready=""
__shellfirm_blocked=""
__shellfirm_histnum="__sf_none__"
__shellfirm_dbg_precmd() { __shellfirm_ready=1; __shellfirm_blocked=""; }
__shellfirm_dbg() {
    # extdebug implies functrace: never interfere with our own functions.
    [[ $BASH_COMMAND == __shellfirm_* || ${FUNCNAME[1]:-} == __shellfirm_* ]] && return 0
    if [[ -z $__shellfirm_ready ]]; then
        [[ -n $__shellfirm_blocked ]] && return 1
        return 0
    fi
    [[ -n ${COMP_LINE:-} ]] && return 0
    local __sf_h __sf_n __sf_cmd
    __sf_h=$(HISTTIMEFORMAT='' builtin history 1)
    __sf_n=${__sf_h#"${__sf_h%%[![:space:]]*}"}
    __sf_n=${__sf_n%%[[:space:]]*}
    [[ $__sf_n == "$__shellfirm_histnum" ]] && return 0
    __shellfirm_ready=""
    __shellfirm_histnum=$__sf_n
    __sf_cmd=${__sf_h#*[0-9][[:space:]]}
    __sf_cmd=${__sf_cmd#"${__sf_cmd%%[![:space:]]*}"}
    [[ -z $__sf_cmd ]] && return 0
    # bash 3.2 (macOS /bin/bash) always runs here (no readline mode): its
    # aliases and functions are expanded as Enter does in readline mode,
    # so `alias kd='kubectl delete'; kd ns prod` is checked as what runs.
    if (( BASH_VERSINFO[0] < 4 )); then
        __shellfirm_expand "$__sf_cmd"
        __sf_cmd=${__shellfirm_expanded:-$__sf_cmd}
    fi
    if ! __shellfirm_check "$__sf_cmd"; then
        __shellfirm_blocked=1
        return 1
    fi
    return 0
}
PROMPT_COMMAND="__shellfirm_dbg_precmd${PROMPT_COMMAND:+$'\n'$PROMPT_COMMAND}"
builtin shopt -s extdebug
builtin trap '__shellfirm_dbg' DEBUG
fi
fi

# `shellfirm doctor` runs this in a fresh interactive shell; run it in this
# shell to check the live state: `__shellfirm_doctor_probe | shellfirm doctor --stdin`
__shellfirm_doctor_probe() {
    builtin printf 'shell=bash\nshell_version=%s\nhook_version=%s\npid=%s\n' "$BASH_VERSION" "$__shellfirm_hook_version" "$$"
    if [[ ( -o emacs || -o vi ) && ${BASH_VERSINFO[0]:-0} -ge 4 ]]; then
        local __sf_bad __sf_first="" __sf_e
        if [[ -z $__shellfirm_legacy_checked ]]; then
            # A new shell (`shellfirm doctor`): what its first prompt does,
            # PROMPT_COMMAND included (a plugin can rebind Enter there, after
            # shellfirm's look; the first command would then not be checked).
            {
                if [[ $(builtin declare -p PROMPT_COMMAND 2>/dev/null) == "declare -a"* ]]; then
                    for __sf_e in "${PROMPT_COMMAND[@]}"; do builtin eval "$__sf_e"; done
                else
                    builtin eval "${PROMPT_COMMAND-}"
                fi
            } >/dev/null 2>&1
            __sf_first=$(__shellfirm_unbound_keys)
        fi
        # What the next prompt does: Enter rebound after shellfirm loaded is
        # wrapped again.
        __shellfirm_check_bindings 2>/dev/null
        __sf_bad=$(__shellfirm_unbound_keys)
        if [[ -n $__sf_first ]]; then
            builtin printf 'mode=readline\nenter=%s (rebound at the first prompt by a PROMPT_COMMAND entry that runs after shellfirm looks: the first command of a new shell is not checked)\n' "${__sf_first# }"
        else
            builtin printf 'mode=readline\nenter=%s\n' "${__sf_bad:-ok}"
        fi
        if [[ $(builtin bind -m emacs -q operate-and-get-next 2>/dev/null) == *'"\C-o"'* ]]; then
            builtin printf 'ctrl_o=unchecked\n'
        else
            builtin printf 'ctrl_o=ok\n'
        fi
    else
        builtin printf 'mode=debug-trap\nenter=n/a\n'
    fi
    [[ "${PROMPT_COMMAND[*]} ${precmd_functions[*]}" == *__shellfirm_precmd* || "${PROMPT_COMMAND[*]}" == *__shellfirm_dbg_precmd* ]] \
        && builtin printf 'precmd=ok\n' || builtin printf 'precmd=missing\n'
    [[ $(builtin type -t fc) == function && $(builtin declare -f fc) == *__shellfirm_fcedit* ]] \
        && builtin printf 'fc=ok\n' || builtin printf 'fc=unchecked\n'
    if [[ $(builtin enable -n -p 2>/dev/null) == *"enable -n fc"* ]]; then
        if [[ $(builtin declare -f command_not_found_handle 2>/dev/null) == *'\fc "$@"'* ]]; then
            builtin printf 'fc_builtin=off\n'
        else
            builtin printf 'fc_builtin=off-no-handler\n'
        fi
    else
        builtin printf 'fc_builtin=on\n'
    fi
    [[ -n $__shellfirm_rw_seen ]] && builtin printf 'rewrapped=%s\n' "${__shellfirm_rw_seen# }"
    [[ $(builtin trap -p DEBUG) == *_shellfirm_hook* ]] && builtin printf 'legacy_hook=active\n'
    [[ -n $__shellfirm_bypass_warned || -n ${__shellfirm_unchecked_seen:-} ]] && builtin printf 'bypass_seen=yes\n'
    __shellfirm_wrap_plugins
    local __sf_pl=""
    builtin declare -F __atuin_accept_line >/dev/null && __sf_pl+=" atuin:${__shellfirm_atuin_wrapped:+checked}"
    [[ -n ${bash_preexec_imported:-}${__bp_imported:-} ]] && __sf_pl+=" bash-preexec"
    builtin declare -F __fzf_select__ >/dev/null && __sf_pl+=" fzf"
    [[ -n ${BLE_VERSION:-} ]] && __sf_pl+=" ble.sh:unsupported"
    builtin declare -F starship_precmd >/dev/null && __sf_pl+=" starship"
    builtin printf 'plugins=%s\n' "${__sf_pl# }"
}
fi
"#;

/// zsh: wraps the accept-line family of ZLE widgets (one wrapper per widget,
/// chaining any existing user widget, installed once even when the rc file
/// is sourced again), routes Ctrl-R + Enter through it, and checks
/// `$PREBUFFER$BUFFER` once the command is complete, with aliases (also
/// global and suffix aliases), `r` / `fc` re-runs and small functions
/// expanded in every command of the line. `fc` / `r` typed later run
/// through a checking wrapper (an alias set at the first prompt).
const ZSH_HOOK: &str = r#"# shellfirm hook for zsh
if [[ -o interactive ]]; then
typeset -g _shellfirm_hook_version=14
# Lets `shellfirm doctor` tell whether the shell it runs in loaded the hook
# (SHELLFIRM_HOOK_LOAD changes at every load, also after `exec zsh`).
export SHELLFIRM_HOOK_PID=$$ SHELLFIRM_HOOK_LOAD=$$.$RANDOM$RANDOM
zmodload zsh/zleparameter zsh/parameter 2>/dev/null
typeset -gA _shellfirm_orig_widget
typeset -g _shellfirm_warned="" _shellfirm_seen="" _shellfirm_bypass_warned="" _shellfirm_expanded=""
typeset -g _shellfirm_notes="" _shellfirm_block_msg="" _shellfirm_trashed=""
typeset -ga _shellfirm_fc_ok
# Longer lines cannot be passed as one argument (Linux MAX_ARG_STRLEN is
# 128 KiB); they are checked through stdin instead and never prompt.
typeset -gi _shellfirm_max_arg=100000

# A message on a line of its own, also while a line is being edited (zle
# then redraws the prompt below it by itself).
_shellfirm_say() {
    if zle; then
        zle -I
        _shellfirm_trashed=1
    fi
    print -ru2 -- "$@"
}

_shellfirm_warn_missing() {
    [[ -n $_shellfirm_warned ]] && return 0
    _shellfirm_warned=1
    _shellfirm_say "shellfirm: binary not found in PATH; commands are NOT being checked"
}

# The command hash can point at a binary that was removed or moved.
_shellfirm_have_binary() {
    (( $+commands[shellfirm] )) && [[ -x ${commands[shellfirm]} ]] && return 0
    rehash
    (( $+commands[shellfirm] )) && [[ -x ${commands[shellfirm]} ]]
}

# shellfirm prints settings, policy and custom-check problems on every call.
# Look once per session (and again when SHELLFIRM_CONFIG_DIR changes), show
# them once on a line of their own, and from then on keep them away from
# safe lines (they would be glued to the line being edited).
_shellfirm_probe_notes() {
    [[ -n $_shellfirm_notes && $_shellfirm_notes_key == "${SHELLFIRM_CONFIG_DIR-}" ]] && return
    typeset -g _shellfirm_notes_key=${SHELLFIRM_CONFIG_DIR-}
    local _sf_w
    _sf_w=$(command shellfirm pre-command --test --command=: 2>&1 >/dev/null)
    # Not runnable: the check itself reports it.
    (( $? >= 126 )) && return
    [[ -n $_sf_w ]] && _shellfirm_say "$_sf_w"
    # A SHELLFIRM_CONFIG_DIR that is not a directory: said once (above);
    # the checks then use the default directory, as shellfirm itself does,
    # without saying it again on every line.
    if [[ -n $_sf_w ]] && _shellfirm_stale_dir; then
        _sf_w=$(SHELLFIRM_CONFIG_DIR= command shellfirm pre-command --test --command=: 2>&1 >/dev/null)
    fi
    if [[ -n $_sf_w ]]; then
        _shellfirm_notes=1
    else
        _shellfirm_notes=0
    fi
}
_shellfirm_stale_dir() { [[ -n ${SHELLFIRM_CONFIG_DIR//[[:space:]]/} && ! -d ${SHELLFIRM_CONFIG_DIR} ]] }

# Reads the input waiting on the terminal into _shellfirm_rest (Enter as a
# newline; control characters and bracketed-paste marks left out).
typeset -g _shellfirm_rest="" _shellfirm_rest_note=""
_shellfirm_drain() {
    setopt localoptions extendedglob
    local _sf_c _sf_r=""
    local -i _sf_n=0
    while (( PENDING > 0 && _sf_n++ < 65536 )); do
        read -k 1 _sf_c || break
        _sf_r+=$_sf_c
    done
    _sf_r=${_sf_r//$'\e[200~'/}
    _sf_r=${_sf_r//$'\e[201~'/}
    _sf_r=${_sf_r//$'\r'/$'\n'}
    _sf_r=${_sf_r//[$'\001'-$'\010\013'-$'\037\177']/}
    _sf_r=${_sf_r%%$'\n'##}
    [[ -n ${_sf_r//[[:space:]]/} ]] && _shellfirm_rest=$_sf_r
}
# "1 line" / "N lines" for _shellfirm_rest, in _sf_rn ("was" / "were" in
# _sf_rv, "it is" / "they are" in _sf_rp).
_shellfirm_rest_count() {
    setopt localoptions extendedglob
    local -a _sf_ls=("${(@f)_shellfirm_rest}")
    _sf_ls=(${_sf_ls:#[[:space:]]#})
    local -i _sf_k=${#_sf_ls}
    (( _sf_k )) || _sf_k=1
    if (( _sf_k == 1 )); then
        _sf_rn="1 line" _sf_rv=was _sf_rp="it is"
    else
        _sf_rn="$_sf_k lines" _sf_rv=were _sf_rp="they are"
    fi
}
# The line runs: what was read ahead of it goes back to the editor, for the
# next prompt (it is not run, and is checked when Enter runs it).
_shellfirm_give_back() {
    [[ -n $_shellfirm_rest ]] || return 0
    setopt localoptions extendedglob
    local _sf_rn _sf_rv _sf_rp
    _shellfirm_rest_count
    print -z -- "$_shellfirm_rest"
    _shellfirm_rest=""
    _shellfirm_rest_note="shellfirm: $_sf_rn typed or pasted after this command $_sf_rv not run (a challenge discards waiting input); $_sf_rp back in the editor."
}

# Returns 0 to allow, 1 to block. A missing binary fails open; anything else
# that goes wrong (exit 126, a line too long to pass) fails closed.
_shellfirm_run() {
    if ! _shellfirm_have_binary; then
        _shellfirm_warn_missing
        return 0
    fi
    # Keep ZLE (and the buffer) alive when ^C cancels the challenge.
    setopt localoptions localtraps
    trap ':' INT
    # Without interactivecomments (zsh's default) `#` starts no comment:
    # `rm -rf a # b` removes b too.
    [[ -o interactivecomments ]] || local -x SHELLFIRM_NO_COMMENTS=1
    local -i _sf_len _sf_rc
    local _sf_out
    () { setopt localoptions nomultibyte; _sf_len=${#1} } "$1"
    if (( _sf_len > _shellfirm_max_arg )); then
        _shellfirm_line=""
        print -rn -- "$1" | command shellfirm check --stdin --exit-code >/dev/null 2>&1
        _sf_rc=$?
        (( _sf_rc == 0 )) && return 0
        if (( _sf_rc == 127 )); then
            _shellfirm_say "shellfirm: warning: ${commands[shellfirm]} is installed but could not run (exit 127); this command was NOT checked. Run \`shellfirm doctor\`."
            return 0
        fi
        if (( _sf_rc == 10 || _sf_rc == 11 )); then
            _shellfirm_say "shellfirm: blocked: this line is too long to confirm interactively ($_sf_len bytes) and it matches a risky pattern. Run it from a script file instead."
        else
            _shellfirm_say "shellfirm: blocked: this line ($_sf_len bytes) could not be checked (exit $_sf_rc). Run it from a script file instead."
        fi
        return 1
    fi
    # In the line editor the "approved, running" line is printed from
    # preexec, once zle (and a transient prompt) has redrawn the line.
    local -a _sf_env=()
    if zle; then
        _shellfirm_probe_notes
        if [[ -n $_shellfirm_notes && $_shellfirm_notes_key == "${SHELLFIRM_CONFIG_DIR-}" ]] && _shellfirm_stale_dir; then
            local -x SHELLFIRM_CONFIG_DIR=
        fi
        _sf_env=(SHELLFIRM_DEFER_APPROVED=1)
        if [[ $_shellfirm_notes == 1 ]]; then
            # Dry run first (quietly): the warnings only show for risky lines.
            _sf_out=$(command shellfirm pre-command --test --command="$1" 2>/dev/null)
            _sf_rc=$?
            (( _sf_rc == 0 )) && [[ $_sf_out != *"skipped (below"* ]] && return 0
            zle -I
            _shellfirm_trashed=1
        fi
        # Input typed or pasted after this line (not as one bracketed
        # paste) that zle has not read yet: a challenge discards it (it
        # could answer the challenge). Unless the line is plainly safe, it is
        # read first, and given back to the editor (_shellfirm_give_back).
        if (( PENDING > 0 )); then
            if [[ $_shellfirm_notes != 1 ]]; then
                command shellfirm pre-command --test --command="$1" >/dev/null 2>&1
                _sf_rc=$?
            fi
            # 65: a challenge, 77: denied.
            (( _sf_rc == 65 || _sf_rc == 77 )) && _shellfirm_drain
        fi
    fi
    local -a _sf_la=()
    local _sf_shown=${_shellfirm_line:-$1}
    [[ -n $_shellfirm_line && $_shellfirm_line != "$1" ]] && _sf_la=(--line="$_shellfirm_line")
    _shellfirm_line=""
    if (( ${#_sf_env} )); then
        # Exit 3 after any passed challenge (also with approved_notice off):
        # the screen below the line is then put right (_shellfirm_below).
        SHELLFIRM_DEFER_APPROVED=1 SHELLFIRM_SIGNAL_PROMPTED=1 command shellfirm pre-command --direct-tty --command="$1" "${_sf_la[@]}"
    else
        command shellfirm pre-command --direct-tty --command="$1" "${_sf_la[@]}"
    fi
    _sf_rc=$?
    (( ${#_sf_env} && _sf_rc != 0 && _sf_rc != 127 )) && _shellfirm_below $(( _sf_rc == 3 ))
    case $_sf_rc in
        0) return 0 ;;
        3) _shellfirm_approved=$_sf_shown; return 0 ;;
        127) _shellfirm_say "shellfirm: warning: ${commands[shellfirm]} is installed but could not run (exit 127); this command was NOT checked. Run \`shellfirm doctor\`."; return 0 ;;
        126) _shellfirm_say "shellfirm: could not run shellfirm (exit 126); command not run"; return 1 ;;
        *) return 1 ;;
    esac
}

# The banner and the challenge were printed below the line zle drew, and
# zle does not know: drawing the line again (a paste highlight going away, a
# transient prompt, the kept line after a cancel) it would move up over the
# challenge and the typed answer. Move down as many rows as zle moves up, so
# it draws below the answer instead. $1 = 1 (approved): the answer's next
# row is saved, and preexec erases from there what zle draws again (as bash
# does), so the approved line comes right after the answer.
typeset -g _shellfirm_restore=""
_shellfirm_below() {
    [[ -t 2 && -n ${TERM-} && $TERM != dumb ]] || return 0
    setopt localoptions extendedglob
    local _sf_p=${(%)PS1} _sf_l
    _sf_p=${_sf_p//$'\e'\[[0-9;?]#[ -\/]#[@-~]/}
    _sf_p=${_sf_p//$'\e'\][^$'\a\e']#($'\a'|$'\e'\\)/}
    local -a _sf_pl=("${(@f)_sf_p}")
    local -i _sf_cols=${COLUMNS:-80} _sf_k _sf_n=0 _sf_w
    (( _sf_cols > 0 )) || _sf_cols=80
    # Rows above the cursor: the prompt's first lines, then its last line
    # with the text before the cursor (one more row per line than it
    # strictly needs, which only leaves a blank row).
    (( _sf_k = ${#_sf_pl} - 1 ))
    _sf_p=${_sf_pl[-1]}$LBUFFER
    for _sf_l in "${(@f)_sf_p}"; do
        _sf_w=${(m)#_sf_l}
        (( _sf_k += _sf_w / _sf_cols + 1 ))
    done
    (( _sf_k-- ))
    _sf_p=${_sf_pl[-1]}$BUFFER
    for _sf_l in "${(@f)_sf_p}"; do
        _sf_w=${(m)#_sf_l}
        (( _sf_n += _sf_w / _sf_cols + 1 ))
    done
    (( _sf_n += ${#_sf_pl} + 2 ))
    local _sf_s=""
    if [[ $1 == 1 ]]; then
        # Make room first (nothing scrolls after the row is saved), come
        # back, save the row, then go down.
        _sf_s=${(pl:_sf_n::\n:)}$'\e['$_sf_n$'A\e7'
        _shellfirm_restore=1
    fi
    (( _sf_k > 0 )) && _sf_s+=${(pl:_sf_k::\n:)}
    print -rnu2 -- "$_sf_s"
}

# Returns 0 when $1 is a complete command list that zsh would run now.
# Parsing it as a function body never executes anything.
_shellfirm_complete() {
    { functions[_shellfirm_probe]=$1 } 2>/dev/null
    (( $+functions[_shellfirm_probe] )) || return 1
    unfunction _shellfirm_probe
    local _sf_bs=${1##*[^\\]}
    if (( ${#_sf_bs} % 2 )); then
        # Trailing backslash: a line continuation unless quoted/commented.
        { functions[_shellfirm_probe]=$1$'\nfi' } 2>/dev/null
        if (( $+functions[_shellfirm_probe] )); then
            unfunction _shellfirm_probe
            return 1
        fi
        # After an operator (`a && \`) the probe above is a syntax error;
        # zsh's own word splitting tells a continuation from a comment.
        local -a _sf_ws
        if [[ -o interactivecomments ]]; then
            _sf_ws=("${(@Z+c+)1}")
        else
            _sf_ws=("${(@z)1}")
        fi
        _sf_bs=${_sf_ws[-1]##*[^\\]}
        [[ ${_sf_ws[-1]} != \#* ]] && (( ${#_sf_bs} % 2 )) && return 1
    fi
    return 0
}

# Parse the arguments of `fc` / `r` ($1 = fc or r). Sets the caller's
# _sf_how (rerun: `r`, `fc -e -`; edit: an editor runs; other: list, read,
# write or push history), _sf_ed, _sf_subs (old=new), _sf_sel (first [last])
# and _sf_rest (the arguments without `-e <editor>`).
_shellfirm_fc_args() {
    local _sf_a _sf_next=""
    _sf_how=edit _sf_ed=""
    _sf_subs=() _sf_sel=() _sf_rest=()
    [[ $1 == r ]] && _sf_how=rerun
    shift
    for _sf_a in "$@"; do
        if [[ -n $_sf_next ]]; then
            if [[ $_sf_next == e ]]; then
                _sf_ed=$_sf_a
                [[ $_sf_a == - && $_sf_how != other ]] && _sf_how=rerun
            else
                _sf_rest+=("$_sf_a")
            fi
            _sf_next=""
            continue
        fi
        case $_sf_a in
            -e) _sf_next=e; continue ;;
            -[mt]) _sf_next=x ;;
            -[0-9]*) _sf_sel+=("$_sf_a") ;;
            --) ;;
            -*[lLRWAPp]*) _sf_how=other ;;
            -*s*) [[ $_sf_how == other ]] || _sf_how=rerun ;;
            -*) ;;
            *=*) if (( ${#_sf_sel} )); then _sf_sel+=("$_sf_a"); else _sf_subs+=("$_sf_a"); fi ;;
            *) _sf_sel+=("$_sf_a") ;;
        esac
        _sf_rest+=("$_sf_a")
    done
}

# The commands a re-run (`r`, `fc -e -`) will run, in _sf_fc_out.
# _sf_fc_err: why zsh will refuse the re-run (no such event, an old=new
# that matches nothing), in zsh's words.
_shellfirm_fc_text() {
    setopt localoptions extendedglob
    local _sf_s _sf_first=${_sf_sel[1]:--1} _sf_subbed=""
    local _sf_last=${_sf_sel[2]:-$_sf_first}
    _sf_fc_err=""
    _sf_fc_out=$(builtin fc -ln "$_sf_first" "$_sf_last" 2>&1) || {
        # zsh prefixes the message with this function's name.
        _sf_fc_err=${${_sf_fc_out##*fc:}##<->:}
        _sf_fc_err=${_sf_fc_err##[[:space:]]#}
        _sf_fc_out=""
        return
    }
    _sf_fc_out=${_sf_fc_out##[[:space:]]#}
    for _sf_s in $_sf_subs; do
        [[ $_sf_fc_out == *"${_sf_s%%=*}"* ]] && _sf_subbed=1
        _sf_fc_out=${_sf_fc_out//"${_sf_s%%=*}"/${_sf_s#*=}}
    done
    (( ${#_sf_subs} )) && [[ -z $_sf_subbed ]] && _sf_fc_err="no substitutions performed"
}

# `fc` / `r` typed on the command line (an alias set up at the first prompt,
# so functions in rc files that use `fc -p -a` are not affected): re-runs
# and edited commands are checked before zsh runs them.
_shellfirm_fc() {
    local _sf_name=$1 _sf_how _sf_ed _sf_fc_out _sf_fc_err
    local -a _sf_subs _sf_sel _sf_rest
    shift
    _shellfirm_fc_args "$_sf_name" "$@"
    case $_sf_how in
        rerun)
            _shellfirm_fc_text
            # zsh's own message, without this wrapper's name in front of it.
            if [[ -n $_sf_fc_err ]]; then
                print -ru2 -- "fc: $_sf_fc_err"
                return 1
            fi
            if [[ -n ${_sf_fc_out//[[:space:]]/} ]] && (( ! ${_shellfirm_fc_ok[(Ie)$_sf_fc_out]} )); then
                _shellfirm_expand "$_sf_fc_out"
                if [[ -n $_shellfirm_expanded ]] && ! _shellfirm_run "$_shellfirm_expanded"; then
                    return 1
                fi
            fi
            # A command the re-run cannot find is reported in zsh's words,
            # not with this wrapper's name in front.
            if (( $+functions[command_not_found_handler] )); then
                builtin $_sf_name "$@"
            else
                {
                    command_not_found_handler() { print -ru2 -- "zsh: command not found: $1"; return 127 }
                    builtin $_sf_name "$@"
                } always {
                    unfunction command_not_found_handler 2>/dev/null
                }
            fi ;;
        edit)
            local _shellfirm_fc_editor=${_sf_ed:-${FCEDIT:-${EDITOR:-vi}}}
            builtin fc -e _shellfirm_fcedit "${_sf_rest[@]}" ;;
        *) builtin fc "$@" ;;
    esac
}

# The editor `fc` runs: the real editor, then a check of the edited text. A
# blocked edit is emptied, and zsh runs nothing when the editor fails.
_shellfirm_fcedit() {
    local _sf_f=$1 _sf_c
    eval "$_shellfirm_fc_editor"' "$_sf_f"' || return
    _sf_c=$(<$_sf_f)
    [[ -z ${_sf_c//[[:space:]]/} ]] && return 0
    _shellfirm_expand "$_sf_c"
    [[ -z $_shellfirm_expanded ]] && return 0
    _shellfirm_run "$_shellfirm_expanded" && return 0
    : >| $_sf_f
    return 1
}

typeset -gi _shellfirm_depth=0
# What separates the words that can name a function: blanks, control
# characters and ASCII punctuation other than `_ : . @ + -`. Any other
# character (`nettoyé`, `日本`) can be part of a function name.
typeset -g _shellfirm_nonname='[]!#"$%&'\''()*,/;<=>?[\\^`{|}~[:space:][:cntrl:]]'
# Variables assigned on the line being checked; functions expanded for it.
typeset -gA _shellfirm_assigned
typeset -ga _shellfirm_fn_seen
typeset -g _shellfirm_vv="" _shellfirm_fb="" _shellfirm_fb_big="" _shellfirm_line="" _shellfirm_ol=""
typeset -g _shellfirm_defs="" _shellfirm_defs_extra="" _shellfirm_expanded_extra=""
typeset -gi _shellfirm_defs_n=0 _shellfirm_defs_bytes=0
typeset -ga _shellfirm_reach

# The value of variable $1 (in _shellfirm_vv): from an assignment earlier on
# the line being checked, else from this session.
_shellfirm_var_value() {
    _shellfirm_vv=""
    if (( ${+_shellfirm_assigned[$1]} )); then
        _shellfirm_vv=${_shellfirm_assigned[$1]}
    elif (( ${+parameters[$1]} )); then
        _shellfirm_vv="${(P)1}"
    fi
}

# The body of the function $1 when it is worth checking with a call (in
# _shellfirm_fb): a small function that is not shellfirm's. A function that
# is autoloaded but not loaded yet (`autoload -Uz nuke`) is read from its
# file (zsh's own functions, under .../share/zsh/, are left out).
_shellfirm_fn_body() {
    _shellfirm_fb=""
    (( $+functions[$1] )) || return 1
    local _sf_b=${functions[$1]} _sf_d _sf_f=""
    if [[ $_sf_b == builtin\ autoload\ -X* ]]; then
        _sf_f=${functions_source[$1]-}
        if [[ -z $_sf_f ]]; then
            for _sf_d in $fpath; do
                [[ -f $_sf_d/$1 && -r $_sf_d/$1 ]] && { _sf_f=$_sf_d/$1; break; }
            done
        fi
        [[ -n $_sf_f && $_sf_f != */share/zsh/* && -f $_sf_f && -r $_sf_f ]] || return 1
        _sf_b=$(<$_sf_f) 2>/dev/null || return 1
    fi
    _shellfirm_fb_big=""
    (( ${#_sf_b} > 0 )) || return 1
    [[ $_sf_b == *_shellfirm_* ]] && return 1
    if (( ${#_sf_b} > 16000 )); then
        # Loaded from a file: like a program on PATH (see _shellfirm_fn_defs).
        [[ -n $_sf_f || -f ${functions_source[$1]-} ]] || _shellfirm_from_file $1 || _shellfirm_fb_big=1
        return 1
    fi
    _shellfirm_fb=$_sf_b
}

# Functions loaded from a file in a way their source does not show: a
# relative path (`source ./nvm.sh`: zsh keeps it as written, which no longer
# resolves after a `cd`) and `source <(cat FILE)` (/proc/self/fd/N). Noted
# at the prompt after such a line (and at the first prompt, after the rc
# files); name -> "rel:<source>" or "cat:<body>" (the same name defined
# again from somewhere else does not count).
typeset -gA _shellfirm_srcfns
typeset -g _shellfirm_srcnote="" _shellfirm_catsrc=""
_shellfirm_from_file() {
    local _sf_v=${_shellfirm_srcfns[$1]-} _sf_s=${functions_source[$1]-}
    case $_sf_v in
        (rel:*) [[ -n $_sf_s && ${_sf_v#rel:} == "$_sf_s" ]] ;;
        (cat:*) [[ $_sf_s == (/proc/*|/dev/fd/*) && ${_sf_v#cat:} == "${functions[$1]}" ]] ;;
        (*) return 1 ;;
    esac
}
# The line $1 sources a file: look at the next prompt.
_shellfirm_src_line() {
    setopt localoptions extendedglob nobashrematch norematchpcre
    [[ $1 == (|*[\;\&\|\(\{[:space:]])(source|.)[[:space:]]* ]] || return 0
    _shellfirm_srcnote=1
    local _sf_re='^[[:space:]]*(source|\.)[[:space:]]+<\([[:space:]]*cat[[:space:]]+([^][:space:];&|()<>`$'"'"'"\\*?[{}]+)[[:space:]]*\)[[:space:]]*$'
    if [[ $1 =~ $_sf_re ]]; then
        _shellfirm_catsrc=${match[2]}
        [[ $_shellfirm_catsrc == '~/'* ]] && _shellfirm_catsrc=$HOME/${_shellfirm_catsrc#'~/'}
    fi
}
_shellfirm_note_src() {
    local _sf_n _sf_s _sf_f=$_shellfirm_catsrc
    _shellfirm_srcnote="" _shellfirm_catsrc=""
    (( ${+functions_source} )) || return 0
    [[ -n $_sf_f && -f $_sf_f ]] || _sf_f=""
    for _sf_n _sf_s in "${(@kv)functions_source}"; do
        [[ $_sf_n == _shellfirm* || -z $_sf_s ]] && continue
        case $_sf_s in
            (/proc/*|/dev/fd/*)
                [[ -n $_sf_f ]] && (( ${#functions[$_sf_n]} > 16000 && ${#_shellfirm_srcfns} < 256 )) \
                    && _shellfirm_srcfns[$_sf_n]="cat:${functions[$_sf_n]}" ;;
            (/*) ;;
            (*)
                [[ ${_shellfirm_srcfns[$_sf_n]-} == "rel:$_sf_s" ]] && continue
                [[ -f $_sf_s || ( -n ${OLDPWD-} && -f $OLDPWD/$_sf_s ) ]] && (( ${#_shellfirm_srcfns} < 4096 )) \
                    && _shellfirm_srcfns[$_sf_n]="rel:$_sf_s" ;;
        esac
    done
}

# A function body as one line, which is how the check reads a definition.
# A here-document cannot be joined: that body stays as it is (its commands
# are then read as written, `$1` unknown). Sets _shellfirm_ol.
_shellfirm_one_line() {
    setopt localoptions extendedglob
    _shellfirm_ol=$1
    [[ ${1//<<</} == *'<<'* ]] && return
    local _sf_l _sf_o=""
    for _sf_l in "${(@f)1}"; do
        _sf_l=${${_sf_l##[[:space:]]#}%%[[:space:]]#}
        [[ -z $_sf_l ]] && continue
        case $_sf_o in
            (''|*'{'|*'('|*';'|*'|'|*'&'|then|*[[:space:]]then|do|*[[:space:]]do|else|*[[:space:]]else|*[[:space:]]in) _sf_o+=" $_sf_l" ;;
            (*) _sf_o+="; $_sf_l" ;;
        esac
    done
    _shellfirm_ol=${_sf_o# }
}

# The functions named in $1 (and those their bodies name, and so on), each
# once, are sent ahead of the line as definitions (`name() { body; }`): the
# check binds every call's arguments itself (`shift`, `"$@"`, any depth),
# so what it reads is what runs. A body that rewrites its arguments in a way
# the check does not follow (`set --`, `argv=`, `getopts`, `shift` in a
# loop) is also added as written, where its `$1` counts as unknown. Past the
# budget (64 functions, 64 KB, 16 KB for one body) the rest is not read: a
# marker says so (shell:function_depth_limit), so nothing is skipped
# silently. A function over 16 KB loaded from a file (nvm, sdkman, conda) is
# treated like a program on PATH: its arguments are checked, its body is not
# read (no marker); one typed or pasted into the session gets the marker.
# Appends to _shellfirm_defs and _shellfirm_defs_extra.
_shellfirm_fn_defs() {
    setopt localoptions extendedglob
    local IFS=$' \t\n' _sf_todo=$1 _sf_w _sf_fb _sf_saved
    local -a _sf_ws
    while [[ -n $_sf_todo ]]; do
        _sf_ws=(${=_sf_todo//${~_shellfirm_nonname}/ })
        _sf_todo=""
        for _sf_w in $_sf_ws; do
            [[ $_sf_w == (_shellfirm*|[0-9.+-]*) ]] && continue
            (( $+functions[$_sf_w] )) || continue
            (( ${_shellfirm_fn_seen[(Ie)$_sf_w]} )) && continue
            _shellfirm_fn_seen+=($_sf_w)
            if ! _shellfirm_fn_body $_sf_w; then
                [[ -n $_shellfirm_fb_big ]] && _shellfirm_defs_extra+=$'\n'"shellfirm-function-depth-limit $_sf_w # a function too large for the hook to read ahead"
                continue
            fi
            _sf_fb=$_shellfirm_fb
            if (( ++_shellfirm_defs_n > 64 || _shellfirm_defs_bytes + ${#_sf_fb} > 64000 )); then
                _shellfirm_defs_extra+=$'\n'"shellfirm-function-depth-limit $_sf_w # calls more functions than the hook reads ahead"
                return
            fi
            (( _shellfirm_defs_bytes += ${#_sf_fb} ))
            _shellfirm_one_line "$_sf_fb"
            _shellfirm_defs+=$'\n'"$_sf_w() { $_shellfirm_ol; }"
            if [[ $_sf_fb =~ '(^|[[:space:];&|({])(set[[:space:]]+(--|-A[[:space:]]+argv|[^-+[:space:]])|getopts[[:space:]]|OPTIND|argv(\[[^]]*\])?\+?=)' ]] \
                || [[ $_sf_fb == *shift* && $_sf_fb =~ '(^|[[:space:];&|({])(while|until|for|foreach|select|repeat)[[:space:]]' ]]; then
                _shellfirm_defs_extra+=$'\n'"{ $_sf_fb }"
            fi
            # What the body runs that the check cannot see by itself
            # (`$K delete ...` with K from this session, `r`, aliases
            # resolved at definition), not the body itself.
            _sf_saved=$_shellfirm_expanded
            (( _shellfirm_depth++ ))
            _shellfirm_expand "$_sf_fb"
            (( _shellfirm_depth-- ))
            _shellfirm_defs_extra+=$_shellfirm_expanded_extra
            _shellfirm_expanded=$_sf_saved
            _sf_todo+=" $_sf_fb"
        done
    done
}

# Functions that run by themselves (the *_functions hook arrays, zle
# widgets, traps, zsh's hook function names) and the functions they name,
# however deep: in _shellfirm_reach (computed once per check).
_shellfirm_hook_reach() {
    setopt localoptions extendedglob
    local _sf_roots="$precmd_functions $preexec_functions $chpwd_functions $periodic_functions $zshexit_functions $zshaddhistory_functions $zsh_directory_name_functions"
    local _sf_v
    for _sf_v in ${(v)widgets}; do
        [[ $_sf_v == user:* ]] && _sf_roots+=" ${_sf_v#user:}"
    done
    _sf_roots+=" ${(k)functions[(I)(precmd|preexec|chpwd|periodic|zshexit|zshaddhistory|TRAP*)]}"
    _sf_roots+=" $(trap 2>/dev/null)"
    _shellfirm_reach=()
    local -a _sf_ws _sf_todo
    local _sf_w _sf_t=$_sf_roots
    local -i _sf_n=0
    while [[ -n $_sf_t ]] && (( _sf_n < 256 )); do
        _sf_ws=(${=_sf_t//${~_shellfirm_nonname}/ })
        _sf_t=""
        for _sf_w in $_sf_ws; do
            [[ $_sf_w == (_shellfirm*|azhw:*|[0-9.+-]*) ]] && continue
            (( $+functions[$_sf_w] )) || continue
            (( ${_shellfirm_reach[(Ie)$_sf_w]} )) && continue
            _shellfirm_reach+=($_sf_w)
            (( ++_sf_n < 256 )) || break
            _sf_t+=" ${functions[$_sf_w]}"
        done
    done
}

# The commands a mistyped command name $1 is one edit away from (what
# CORRECT offers: a swapped, missing, extra or wrong character).
_shellfirm_spell_candidates() {
    local _sf_w=$1 _sf_c _sf_x
    local -i _sf_i _sf_n=${#1}
    local -aU _sf_out
    (( _sf_n >= 2 && _sf_n <= 32 )) || return 0
    local -a _sf_al=({a..z} {0..9} - _ .)
    for (( _sf_i = 1; _sf_i <= _sf_n; _sf_i++ )); do
        _sf_x=${_sf_w[1,_sf_i-1]}${_sf_w[_sf_i+1,-1]}
        (( $+commands[$_sf_x] )) && _sf_out+=($_sf_x)
        if (( _sf_i < _sf_n )); then
            _sf_x=${_sf_w[1,_sf_i-1]}${_sf_w[_sf_i+1]}${_sf_w[_sf_i]}${_sf_w[_sf_i+2,-1]}
            (( $+commands[$_sf_x] )) && _sf_out+=($_sf_x)
        fi
        for _sf_c in $_sf_al; do
            _sf_x=${_sf_w[1,_sf_i-1]}$_sf_c${_sf_w[_sf_i+1,-1]}
            (( $+commands[$_sf_x] )) && _sf_out+=($_sf_x)
            _sf_x=${_sf_w[1,_sf_i-1]}$_sf_c${_sf_w[_sf_i,-1]}
            (( $+commands[$_sf_x] )) && _sf_out+=($_sf_x)
        done
    done
    for _sf_c in $_sf_al; do
        (( $+commands[$_sf_w$_sf_c] )) && _sf_out+=($_sf_w$_sf_c)
    done
    print -r -- ${_sf_out[1,8]}
}

# Expand what zsh expands before it runs a line, in every command of it
# (after ; && || | ( { ! time and assignments): aliases, global and suffix
# aliases, `r` / `fc -e -` re-runs (also `builtin fc`) and small functions.
# Sets _shellfirm_expanded ("" = nothing to check).
_shellfirm_expand() {
    setopt localoptions extendedglob
    local _sf_cmd=${1//$'\\\n'/} _sf_w _sf_v _sf_name _sf_kw="" _sf_extra="" _sf_head _sf_tail _sf_nh
    local _sf_how _sf_ed _sf_fc_out _sf_fc_err _sf_body _sf_p _sf_saved _sf_dn
    local -a _sf_words _sf_fargs _sf_subs _sf_sel _sf_rest _sf_av _sf_subst _sf_hookw _sf_hookb
    local -aU _sf_auto
    local -i _sf_i _sf_k _sf_j _sf_cmdpos=1 _sf_changed=0 _sf_nf=0 _sf_nfc=0 _sf_whole=0
    local -i _sf_indef=0 _sf_defnext=0 _sf_skip=0 _sf_hook=0 _sf_pre=0 _sf_reach=0
    _shellfirm_expanded="" _shellfirm_expanded_extra=""
    if (( ! _shellfirm_depth )); then
        _shellfirm_assigned=()
        _shellfirm_fn_seen=()
        _shellfirm_defs="" _shellfirm_defs_extra="" _shellfirm_defs_n=0 _shellfirm_defs_bytes=0
    fi
    _sf_words=(${(z)_sf_cmd})
    # Functions that run by themselves once this line has run (zsh hook
    # functions, `zle -N` widgets, `add-zsh-hook`, the *_functions arrays):
    # their bodies are checked now, like a PROMPT_COMMAND or a trap.
    if [[ $_sf_cmd == *(zle|add-zsh-hook|_functions|functions\[|PROMPT|PS[1-4]|RPS[12]|trap)* ]]; then
        for (( _sf_k = 1; _sf_k <= ${#_sf_words} && _sf_k <= 4000; _sf_k++ )); do
            case ${_sf_words[_sf_k]} in
                zle)
                    if [[ ${_sf_words[_sf_k+1]-} == -N ]]; then
                        _sf_v=${_sf_words[_sf_k+3]-}
                        [[ -z $_sf_v || $_sf_v == [\;\&\|\)\}]* ]] && _sf_v=${_sf_words[_sf_k+2]-}
                        _sf_auto+=(${(Q)_sf_v})
                    fi ;;
                add-zsh-hook)
                    _sf_j=$(( _sf_k + 1 ))
                    while [[ ${_sf_words[_sf_j]-} == -[^d]* ]]; do (( _sf_j++ )); done
                    [[ ${_sf_words[_sf_j]-} != -d* && -n ${_sf_words[_sf_j+1]-} ]] && _sf_auto+=(${(Q)_sf_words[_sf_j+1]}) ;;
                trap)
                    # `trap f SIG` / `trap 'cmd' SIG`: runs on the signal.
                    _sf_v=${(Q)_sf_words[_sf_k+1]-}
                    if [[ -n $_sf_v && $_sf_v != -* && -n ${_sf_words[_sf_k+2]-} ]]; then
                        if (( $+functions[$_sf_v] )); then _sf_auto+=($_sf_v); else _sf_hookb+=("$_sf_v"); fi
                    fi ;;
                functions\[(precmd|preexec|chpwd|periodic|zshexit|zshaddhistory|zsh_directory_name|command_not_found_handler|TRAP[A-Z0-9]##)\]=*)
                    # A hook function defined through the functions array.
                    _sf_hookb+=("${(Q)_sf_words[_sf_k]#*=}") ;;
                (PROMPT|RPROMPT|PS[1-4]|RPS[12]|PROMPT[234]|RPROMPT2)=*)
                    # A prompt runs its command substitutions (prompt_subst).
                    _sf_v=${(Q)_sf_words[_sf_k]#*=}
                    [[ $_sf_v == *('$('|'`')* ]] && _sf_hookb+=("$_sf_v") ;;
                (precmd|preexec|chpwd|periodic|zshexit|zshaddhistory|zsh_directory_name)_functions(\[*\]|)(|+)=\(*)
                    for (( _sf_j = _sf_k + 1; _sf_j <= ${#_sf_words}; _sf_j++ )); do
                        [[ ${_sf_words[_sf_j]} == ')' ]] && break
                        _sf_auto+=(${(Q)_sf_words[_sf_j]})
                    done ;;
                (precmd|preexec|chpwd|periodic|zshexit|zshaddhistory|zsh_directory_name)_functions(\[*\]|)(|+)=?*)
                    # One element (`precmd_functions[1]=f`, `+=f`).
                    _sf_auto+=(${(Q)_sf_words[_sf_k]#*=}) ;;
            esac
        done
    fi
    for (( _sf_i = 1; _sf_i <= ${#_sf_words} && _sf_i <= 4000; _sf_i++ )); do
        _sf_w=${_sf_words[_sf_i]}
        if [[ -o aliases && -n ${galiases[$_sf_w]-} ]]; then
            _sf_words[_sf_i]=${galiases[$_sf_w]}
            _sf_w=${_sf_words[_sf_i]}
            _sf_changed=1
        fi
        # Command substitutions (`$( )`, backticks, `<( )`) run commands of
        # their own: expanded like a line of their own.
        if [[ $_sf_w == *('$('|'`'|'<('|'>(')* && $_sf_w != \'* ]] && (( ${#_sf_subst} < 8 )); then
            _sf_subst+=("$_sf_w")
        fi
        # A function definition (`f() { ... }`, `function f { ... }`) runs
        # nothing: its body is checked when it is called.
        if (( _sf_defnext || _sf_indef )); then
            case $_sf_w in
                '{') (( _sf_indef++ )); _sf_defnext=0 ;;
                '}') (( _sf_indef > 0 )) && (( _sf_indef-- )) ;;
            esac
            # The body of a function that runs by itself, as a `{ ... }` group.
            if (( _sf_hook && ( _sf_indef > 0 || ! _sf_defnext ) )); then
                _sf_hookw+=("$_sf_w")
                if (( ! _sf_indef )); then
                    _sf_hookb+=("${(j: :)_sf_hookw}")
                    _sf_hook=0 _sf_hookw=()
                fi
            fi
        fi
        if (( _sf_cmdpos && ! _sf_indef && ! _sf_defnext )); then
            if [[ $_sf_w == function && -z $_sf_kw ]] || [[ ${_sf_words[_sf_i+1]-} == '()' && $_sf_w != [\;\&\|\(\)\{\}]* ]]; then
                _sf_defnext=1
                if [[ $_sf_w == function ]]; then _sf_dn=${(Q)_sf_words[_sf_i+1]-}; else _sf_dn=${(Q)_sf_w}; fi
                # A function that runs by itself, also one registered
                # earlier (`add-zsh-hook precmd g`, `zle -N w`) and the
                # functions those call: its new body is checked now.
                (( _sf_reach || _shellfirm_depth )) || { _sf_reach=1; _shellfirm_hook_reach; }
                if [[ $_sf_dn == (precmd|preexec|chpwd|periodic|zshexit|zshaddhistory|zsh_directory_name|command_not_found_handler|TRAP[A-Z0-9]##) ]] || (( ${_sf_auto[(Ie)$_sf_dn]} )) \
                    || (( ! _shellfirm_depth && ${_shellfirm_reach[(Ie)$_sf_dn]} )); then
                    _sf_hook=1 _sf_hookw=()
                    _sf_auto=(${_sf_auto:#$_sf_dn})
                fi
            fi
        fi
        case $_sf_w in
            ';'|'&&'|'||'|'|'|'|&'|'&'|'&!'|'&|'|'('|')'|'{'|'}'|';;'|';&'|';|'|'!'|then|do|else|elif|if|while|until|time|nocorrect|noglob|coproc|'()')
                _sf_cmdpos=1 _sf_kw="" _sf_skip=0 _sf_pre=0
                continue ;;
        esac
        (( _sf_cmdpos )) || continue
        if (( _sf_skip )); then
            _sf_skip=0
            continue
        fi
        # Variable assignments before the command name (remembered: a later
        # `$name` in command position runs that value).
        if [[ -z $_sf_kw && $_sf_w == [[:alpha:]_][[:alnum:]_]#=* ]]; then
            [[ $_sf_w == *=\(* ]] || _shellfirm_assigned[${_sf_w%%=*}]=${(Q)_sf_w#*=}
            _sf_pre=1
            continue
        fi
        # A redirection before the command (`>/dev/null kd ...`).
        if [[ $_sf_w == [0-9]#(\<|\>|\>\>|\<\<\<|\>\||\>\!|\<\>|\&\>|\&\>\>|\>\&|\<\&)* ]]; then
            _sf_pre=1
            [[ $_sf_w == [0-9]#(\<|\>|\>\>|\<\<\<|\>\||\>\!|\<\>|\&\>|\&\>\>|\>\&|\<\&) ]] && _sf_skip=1
            continue
        fi
        case $_sf_w in
            builtin|command|exec|-)
                [[ -z $_sf_kw ]] && { _sf_kw=$_sf_w; continue; } ;;
            repeat)
                # `repeat N cmd`: the count, then the command.
                [[ -z $_sf_kw ]] && { _sf_skip=1; continue; } ;;
        esac
        _sf_cmdpos=0
        (( _sf_indef || _sf_defnext )) && { _sf_kw=""; continue; }
        _sf_name=${(Q)_sf_w}
        if [[ -z $_sf_kw && -o aliases && -n ${aliases[$_sf_w]-} && ${aliases[$_sf_w]} != _shellfirm_fc\ * ]]; then
            _sf_head=$_sf_w _sf_tail=""
            for (( _sf_k = 0; _sf_k < 5; _sf_k++ )); do
                _sf_v=${aliases[$_sf_head]-}
                [[ -n $_sf_v ]] || break
                _sf_v=${_sf_v##[[:space:]]#}
                _sf_nh=${_sf_v%%[[:space:]]*}
                _sf_tail=${_sf_v#"$_sf_nh"}$_sf_tail
                # `alias ls='ls -G'`: zsh does not expand an alias inside itself.
                [[ $_sf_nh == $_sf_head ]] && break
                _sf_head=$_sf_nh
            done
            _sf_words[_sf_i]=$_sf_head$_sf_tail
            _sf_name=$_sf_head
            _sf_changed=1
            # An alias ending in a space also expands the next word.
            [[ $_sf_tail == *[[:space:]] ]] && _sf_cmdpos=1
        elif [[ -z $_sf_kw && -o aliases && $_sf_w == *.[^./]## && -n ${saliases[${_sf_w:e}]-} ]]; then
            # Suffix alias: `alias -s d='rm -rf'` makes `build.d` run `rm -rf build.d`.
            _sf_words[_sf_i]="${saliases[${_sf_w:e}]} $_sf_w"
            _sf_name=${${saliases[${_sf_w:e}]}%%[[:space:]]*}
            _sf_changed=1
        fi
        # The arguments of this command (up to the next control operator).
        _sf_fargs=()
        for (( _sf_k = _sf_i + 1; _sf_k <= ${#_sf_words} && _sf_k <= _sf_i + 64; _sf_k++ )); do
            case ${_sf_words[_sf_k]} in
                ';'|'&&'|'||'|'|'|'|&'|'&'|'&!'|'&|'|')'|'}'|';;') break ;;
            esac
            _sf_fargs+=("${(Q)_sf_words[_sf_k]}")
        done
        # A command named by a variable (`$K delete ...`, `${K}`, `${(P)n}`):
        # its value from earlier on this line, else from this session.
        if [[ -z $_sf_kw && $_sf_w == \$([[:alpha:]_][[:alnum:]_]#|\{(\(P\)|)[[:alpha:]_][[:alnum:]_]#\}) ]] && (( _shellfirm_depth < 3 )); then
            _sf_v=${${${_sf_w#\$}#\{}%\}}
            _shellfirm_var_value "${_sf_v#'(P)'}"
            if [[ $_sf_v == '(P)'* ]]; then
                _sf_v=$_shellfirm_vv
                _shellfirm_vv=""
                [[ $_sf_v == [[:alpha:]_][[:alnum:]_]# ]] && _shellfirm_var_value "$_sf_v"
            fi
            _sf_p=$_shellfirm_vv
            if [[ -n ${_sf_p//[[:space:]]/} && $_sf_p != *_shellfirm* ]] && (( ${#_sf_p} <= 4000 )); then
                _sf_saved=$_shellfirm_expanded
                (( _shellfirm_depth++ ))
                _shellfirm_expand "$_sf_p${_sf_fargs:+ ${(j: :)${(@q-)_sf_fargs}}}"
                (( _shellfirm_depth-- ))
                [[ -n $_shellfirm_expanded ]] && _sf_extra+=$'\n'$_shellfirm_expanded
                _shellfirm_expanded=$_sf_saved
            fi
        fi
        # CORRECT / CORRECT_ALL: zsh offers to correct a mistyped command
        # name (`kubeclt` -> `kubectl`) after this check. Check the
        # commands it could be corrected to as well.
        if [[ ( -o correct || -o correctall ) && -z $_sf_kw && $_sf_name != */* && -n $_sf_name ]] \
            && (( ! $+commands[$_sf_name] && ! $+aliases[$_sf_name] && ! $+functions[$_sf_name] && ! $+builtins[$_sf_name] && ! $+reswords[$_sf_name] )); then
            for _sf_p in $(_shellfirm_spell_candidates "$_sf_name"); do
                _sf_extra+=$'\n'"$_sf_p ${(j: :)${(@qq)_sf_fargs}}"
            done
        fi
        if [[ ( $_sf_name == fc || ( $_sf_name == r && ( -z $_sf_kw || $_sf_kw == builtin ) ) ) ]] && (( _sf_nfc < 8 )) \
            && (( ! $+functions[$_sf_name] || ${+aliases[$_sf_name]} )) || [[ $_sf_kw == builtin && $_sf_name == (fc|r) ]]; then
            (( _sf_nfc++ ))
            _shellfirm_fc_args "$_sf_name" "${_sf_fargs[@]}"
            if [[ $_sf_how == rerun ]]; then
                _shellfirm_fc_text
                if [[ -n $_sf_fc_out ]]; then
                    _shellfirm_fc_ok+=("$_sf_fc_out")
                    # The whole line is the re-run: check (and show) just that.
                    if (( _sf_i <= 2 && _sf_k > ${#_sf_words} && ! _sf_changed )); then
                        _sf_whole=1
                        _sf_cmd=$_sf_fc_out
                    else
                        _sf_extra+=$'\n'$_sf_fc_out
                    fi
                fi
            elif [[ $_sf_how == edit && ( -n $_sf_kw || $_sf_w == \\* ) ]]; then
                _shellfirm_block_msg="shellfirm: \`${_sf_kw:+$_sf_kw }$_sf_w\` with an editor runs the edited commands without a check; use \`fc\` instead. Command not run."
            fi
        elif [[ -z $_sf_kw && $_sf_name != _shellfirm* ]] && (( $+functions[$_sf_name] )); then
            # A call of a function: its definition is sent with the line
            # (_shellfirm_fn_defs). After assignments or a redirection
            # (`A=1 f x`) the bare call too, which the check binds to it.
            (( _sf_pre )) && _sf_extra+=$'\n'"$_sf_name ${(j: :)${(@q-)_sf_fargs}}"
        elif [[ $_sf_name == eval && -z $_sf_kw ]] && (( ${#_sf_fargs} && _shellfirm_depth < 3 )); then
            # The text `eval` runs (`eval 'builtin fc -e - a=b x'`), expanded
            # and checked like a typed line.
            _sf_saved=$_shellfirm_expanded
            (( _shellfirm_depth++ ))
            _shellfirm_expand "${(j: :)_sf_fargs}"
            (( _shellfirm_depth-- ))
            [[ -n $_shellfirm_expanded ]] && _sf_extra+=$'\n'$_shellfirm_expanded
            _shellfirm_expanded=$_sf_saved
        fi
        _sf_kw=""
    done
    # Command substitutions: the commands inside them.
    if (( ${#_sf_subst} && _shellfirm_depth < 3 )); then
        _sf_saved=$_shellfirm_expanded
        (( _shellfirm_depth++ ))
        for _sf_w in $_sf_subst; do
            _sf_v=${_sf_w#*(\$\(|\`|\<\(|\>\()}
            _sf_v=${_sf_v%(\)|\`)*}
            [[ -n ${_sf_v//[[:space:]]/} ]] || continue
            _shellfirm_expand "$_sf_v"
            [[ -n $_shellfirm_expanded && $_shellfirm_expanded != $_sf_v ]] && _sf_extra+=$'\n'$_shellfirm_expanded
        done
        (( _shellfirm_depth-- ))
        _shellfirm_expanded=$_sf_saved
    fi
    # Functions that run by themselves: the bodies defined on this line, and
    # those of functions this line registers that are already defined.
    for _sf_v in ${_sf_auto:#_shellfirm*}; do
        _shellfirm_fn_body "$_sf_v" && _sf_hookb+=("{ $_shellfirm_fb }")
    done
    if (( ${#_sf_hookb} && _shellfirm_depth < 3 )); then
        _sf_saved=$_shellfirm_expanded
        (( _shellfirm_depth++ ))
        for _sf_v in ${_sf_hookb[1,8]}; do
            _shellfirm_expand "$_sf_v"
            _sf_extra+=$'\n'${_shellfirm_expanded:-$_sf_v}
        done
        (( _shellfirm_depth-- ))
        _shellfirm_expanded=$_sf_saved
    fi
    (( _sf_changed && ! _sf_whole )) && _sf_cmd=${(j: :)_sf_words}
    # Only adds text to history: nothing runs.
    if [[ $_sf_cmd == [[:space:]]#(builtin[[:space:]]##|)print[[:space:]]##-[sS][[:space:]]* && $_sf_cmd != *[\;\|\&\`\$\(\)\<\>$'\n']* ]]; then
        return
    fi
    _shellfirm_expanded_extra=$_sf_extra
    if (( ! _shellfirm_depth )); then
        # The definitions of the functions the line (and what it adds)
        # names, ahead of it.
        _shellfirm_fn_defs "$_sf_cmd$_sf_extra"
        _shellfirm_expanded=${_shellfirm_defs#$'\n'}${_shellfirm_defs:+$'\n'}$_sf_cmd$_sf_extra$_shellfirm_defs_extra
        # What the banner and the audit log show: the line as typed
        # (aliases and re-runs resolved), not the definitions.
        _shellfirm_line=$_sf_cmd
    else
        _shellfirm_expanded=$_sf_cmd$_sf_extra
    fi
}

# Blocked: a single line stays in the buffer; a multi-line construct is
# pulled back into one editable buffer. (After `zle -I` zle redraws by
# itself; a reset-prompt would overwrite the message above it.)
_shellfirm_keep_line() {
    # The same words as in bash (the challenge already said "command not
    # run"). A multi-line construct comes back as one buffer: Ctrl-U would
    # only clear its last line.
    local _sf_msg="shellfirm: the line is kept for editing (Ctrl-U clears it)." _sf_rn _sf_rv _sf_rp
    # Ctrl-U (kill-whole-line) clears only one line of a multi-line buffer.
    [[ -n $PREBUFFER || $BUFFER == *$'\n'* ]] && _sf_msg="shellfirm: the lines are kept for editing (Ctrl-C clears them all; Ctrl-U clears one line)."
    if [[ -n $_shellfirm_rest ]]; then
        # The lines read ahead of the challenge stay with it.
        _shellfirm_rest_count
        BUFFER+=$'\n'$_shellfirm_rest
        CURSOR=${#BUFFER}
        _shellfirm_rest=""
        _sf_msg="shellfirm: the line and the $_sf_rn typed or pasted after it are kept for editing (Ctrl-C clears them all; Ctrl-U clears one line)."
    fi
    [[ -n ${1-} ]] && _sf_msg=""
    # The display was given up (`zle -I`) before the check printed: zle
    # draws the prompt again below whatever is printed now. A `zle -M`
    # message would share the prompt's row and overwrite it.
    # A multi-line construct is redrawn from its first line, which would
    # take a `zle -M` message away: print it above instead.
    if [[ ( -n $_shellfirm_trashed || -n $PREBUFFER ) && -n $_sf_msg ]]; then
        [[ -z $_shellfirm_trashed ]] && { zle -I; _shellfirm_trashed=1; }
        print -ru2 -- "$_sf_msg"
        _sf_msg=""
    fi
    if [[ -n $PREBUFFER ]]; then
        zle push-line-or-edit
    elif [[ -z $_shellfirm_trashed ]]; then
        zle reset-prompt
    fi
    # Shown below the line (a multi-line prompt redraw can hide the
    # message printed above it); it goes away at the next key.
    [[ -n $_sf_msg ]] && zle -M "$_sf_msg"
}

# $1 = name of the wrapped widget (never $WIDGET: that is the widget bound
# to the key, e.g. atuin-search when atuin calls `zle accept-line`).
_shellfirm_widget() {
    local _sf_name=$1; shift
    local _sf_orig=${_shellfirm_orig_widget[$_sf_name]:-.$_sf_name}
    _shellfirm_seen=1
    # Re-entered through a plugin chain: already checked, finish the chain.
    if (( ${_shellfirm_in_widget:-0} )); then
        if [[ $_sf_name == accept-* ]]; then
            zle ".$_sf_name" -- "$@"
        else
            zle "$_sf_orig" -- "$@"
        fi
        return
    fi
    local -i _shellfirm_in_widget=1
    _shellfirm_approved="" _shellfirm_restore="" _shellfirm_rest="" _shellfirm_rest_note=""
    # In a recursive edit that a transient prompt runs from zle-line-init
    # (romkatv / p10k style: it draws the line again once the edit ends),
    # the line is checked at zle-line-finish instead, after that redraw: the
    # banner then goes below the collapsed line and nothing draws over it.
    # A blocked line comes back at the next prompt; in vi mode Ctrl-U would
    # not clear it there, so vi mode keeps the check here.
    if (( ${ZLE_RECURSIVE:-0} > 0 )) && [[ $_sf_name == accept-line && -z $PREBUFFER ]] \
        && [[ $(bindkey -lL main 2>/dev/null) != *viins* ]] && _shellfirm_add_gate; then
        zle "$_sf_orig" -- "$@"
        return
    fi
    # Here-document body lines are data; the line that opened the
    # here-document was checked when it was accepted.
    if [[ -n $PREBUFFER && ${(%):-%_} == *heredoc* ]]; then
        zle "$_sf_orig" -- "$@"
        return
    fi
    setopt localoptions extendedglob
    _shellfirm_fc_ok=()
    _shellfirm_block_msg=""
    _shellfirm_trashed=""
    # History references are expanded first: zsh decides whether the input
    # is complete after expanding them (`!$` can be the closing `fi`). The
    # history characters can be changed (`histchars='@^#'`).
    local _sf_h1=${histchars[1]-} _sf_h2=${histchars[2]-}
    if [[ -o banghist && -n $_sf_h1 && ( $BUFFER == *"$_sf_h1"* || ( -n $_sf_h2 && $BUFFER == "$_sf_h2"* ) ) ]]; then
        zle expand-history 2>/dev/null
    fi
    local _sf_full=$PREBUFFER$BUFFER
    if [[ -n ${_sf_full//[[:space:]]/} ]] && _shellfirm_complete "$_sf_full"; then
        _shellfirm_expand "$_sf_full"
        if [[ -n $_shellfirm_block_msg ]]; then
            _shellfirm_say "$_shellfirm_block_msg"
            _shellfirm_keep_line said
            return 1
        fi
        if [[ -n $_shellfirm_expanded ]] && ! _shellfirm_run "$_shellfirm_expanded"; then
            _shellfirm_keep_line
            return 1
        fi
        _shellfirm_give_back
        _shellfirm_src_line "$_sf_full"
    fi
    # Checked: the line-finish gate lets exactly this text through.
    _shellfirm_allowed=$PREBUFFER$BUFFER
    zle "$_sf_orig" -- "$@"
}

# zle-line-finish: the last moment before zsh runs the line, whatever Enter
# is bound to. A line that did not pass through the wrapped widgets (Enter
# bound later, by a plugin loaded after the first prompt, to a widget that
# calls `.accept-line`), or that the widget shellfirm ran after its check
# changed (`BUFFER=...; zle .accept-line`), is checked here. Blocked: the
# line is emptied, so nothing runs, and put back for editing.
_shellfirm_line_finish() {
    local _sf_full=$PREBUFFER$BUFFER
    local _sf_ok=$_shellfirm_allowed
    _shellfirm_allowed=""
    [[ -n ${_sf_full//[[:space:]]/} && $_sf_full != "$_sf_ok" ]] || return 0
    [[ -n $PREBUFFER && ${(%):-%_} == *heredoc* ]] && return 0
    _shellfirm_complete "$_sf_full" || return 0
    setopt localoptions extendedglob
    local _sf_h1=${histchars[1]-} _sf_h2=${histchars[2]-}
    if [[ -o banghist && -n $_sf_h1 && ( $BUFFER == *"$_sf_h1"* || ( -n $_sf_h2 && $BUFFER == "$_sf_h2"* ) ) ]]; then
        zle expand-history 2>/dev/null
        _sf_full=$PREBUFFER$BUFFER
    fi
    _shellfirm_gated=1
    _shellfirm_fc_ok=()
    _shellfirm_block_msg=""
    _shellfirm_trashed=""
    _shellfirm_rest="" _shellfirm_rest_note=""
    _shellfirm_expand "$_sf_full"
    if [[ -n $_shellfirm_block_msg ]]; then
        _shellfirm_say "$_shellfirm_block_msg"
    elif [[ -z $_shellfirm_expanded ]] || _shellfirm_run "$_shellfirm_expanded"; then
        _shellfirm_give_back
        _shellfirm_src_line "$_sf_full"
        return 0
    fi
    local _sf_rn _sf_rv _sf_rp
    if [[ -n $_shellfirm_rest ]]; then
        _shellfirm_rest_count
        print -rz -- "$_sf_full"$'\n'"$_shellfirm_rest"
        _shellfirm_rest=""
        print -ru2 -- "shellfirm: the line and the $_sf_rn typed or pasted after it are kept for editing (Ctrl-C clears them all; Ctrl-U clears one line)."
    elif [[ -n $PREBUFFER || $_sf_full == *$'\n'* ]]; then
        print -rz -- "$_sf_full"
        print -ru2 -- "shellfirm: the lines are kept for editing (Ctrl-C clears them all; Ctrl-U clears one line)."
    else
        print -rz -- "$_sf_full"
        print -ru2 -- "shellfirm: the line is kept for editing (Ctrl-U clears it)."
    fi
    BUFFER=""
    # A multi-line construct: abandon it (the whole text is back in the editor).
    [[ -n $PREBUFFER ]] && zle send-break
    return 0
}

# zle-line-init: the zle-line-finish check is in place for this line.
_shellfirm_line_init() { _shellfirm_add_gate }

# Ctrl-R (incremental search) + Enter: accept the search, then run the
# (checked) accept-line instead of zsh's built-in one.
_shellfirm_isearch_accept() {
    zle accept-search
    zle accept-line -- "$@"
}

# A command is about to run that did not pass through the wrapped widgets
# (Enter bound to a widget that calls `zle .accept-line`): the next prompt
# wraps what Enter runs again. Without the line-finish check, warn once.
_shellfirm_preexec() {
    # After a passed challenge: "approved, running ..." now, below the line
    # as zle (or a transient prompt) left it.
    if [[ -n $_shellfirm_restore ]]; then
        _shellfirm_restore=""
        print -rnu2 -- $'\e8\e[J'
    fi
    if [[ -n $_shellfirm_approved ]]; then
        if [[ $_shellfirm_notes == 0 ]]; then
            # Nothing to keep quiet: stderr stays the terminal, so the line
            # is dim when the terminal has colour.
            command shellfirm pre-command --approved-notice --command="$_shellfirm_approved"
        elif [[ -t 2 && -z ${NO_COLOR-} && ${CLICOLOR-} != 0 && ${TERM-} == (xterm|screen|tmux|rxvt|*color*|kitty|alacritty|foot|wezterm|vte|linux|iterm|ghostty)* && $TERM != *mono* ]]; then
            CLICOLOR_FORCE=1 command shellfirm pre-command --approved-notice --command="$_shellfirm_approved" 2>/dev/null
        else
            command shellfirm pre-command --approved-notice --command="$_shellfirm_approved" 2>/dev/null
        fi
        _shellfirm_approved=""
    fi
    if [[ -n $_shellfirm_rest_note ]]; then
        print -ru2 -- "$_shellfirm_rest_note"
        _shellfirm_rest_note=""
    fi
    if [[ -z $_shellfirm_seen ]]; then
        _shellfirm_rewrap_sig=""
        if [[ -z $_shellfirm_gated && -z $_shellfirm_bypass_warned ]]; then
            _shellfirm_bypass_warned=1
            print -ru2 -- "shellfirm: warning: this command did not pass through shellfirm (Enter is bound to \`$(bindkey -M main '^M' 2>/dev/null)\`); commands are NOT being checked. Run \`shellfirm doctor\`."
        fi
    fi
    _shellfirm_seen=""
    _shellfirm_gated=""
}

# At the first prompt (after the rc files ran): `fc` and `r` typed on the
# command line go through _shellfirm_fc, unless they are already an alias or
# a function.
_shellfirm_first_prompt() {
    add-zsh-hook -d precmd _shellfirm_first_prompt
    _shellfirm_srcnote=1
    (( $+aliases[fc] || $+functions[fc] )) || alias fc='_shellfirm_fc fc'
    (( $+aliases[r] || $+functions[r] )) || alias r='_shellfirm_fc r'
}

# How the widget $1 (bound to Enter) runs a command: `chained` (it calls the
# checked accept-line), `bypass` (it calls zsh's built-in .accept-line) or
# `unknown`. Looks one call deep into the functions it uses.
_shellfirm_enter_kind() {
    setopt localoptions extendedglob
    local _sf_f=${widgets[$1]-} _sf_b _sf_w _sf_d
    [[ $_sf_f == user:* ]] || { print -r -- unknown; return; }
    _sf_f=${_sf_f#user:}
    local -a _sf_todo=($_sf_f)
    for (( _sf_d = 0; _sf_d < 2 && ${#_sf_todo}; _sf_d++ )); do
        local -a _sf_next=()
        for _sf_f in $_sf_todo; do
            _sf_b=${functions[$_sf_f]-}
            if [[ $_sf_b == *zle[[:space:]]##(-w[[:space:]]##|)[\"\']#.accept-* ]]; then
                print -r -- bypass; return
            fi
            if [[ $_sf_b == *zle[[:space:]]##(-w[[:space:]]##|)[\"\']#(accept-line|accept-and-hold|accept-line-and-down-history|accept-and-infer-next-history)* ]]; then
                print -r -- chained; return
            fi
            for _sf_w in ${(z)_sf_b}; do
                [[ $_sf_w != $_sf_f && $_sf_w != _shellfirm* ]] || continue
                if (( $+functions[$_sf_w] )); then
                    _sf_next+=($_sf_w)
                elif [[ ${widgets[$_sf_w]-} == user:* ]]; then
                    _sf_next+=(${widgets[$_sf_w]#user:})
                fi
            done
        done
        _sf_todo=(${(u)_sf_next})
    done
    print -r -- unknown
}

# `shellfirm doctor` runs this in a fresh interactive shell; run it in this
# shell to check the live state: `__shellfirm_doctor_probe | shellfirm doctor --stdin`
# (the same name in every shell; `_shellfirm_doctor_probe` is the old one).
__shellfirm_doctor_probe() {
    # What the next prompt would do anyway (a probe run with -c has none).
    _shellfirm_rewrap
    print -r -- "shell=zsh"
    print -r -- "shell_version=$ZSH_VERSION"
    print -r -- "hook_version=$_shellfirm_hook_version"
    print -r -- "pid=$$"
    local _sf_w _sf_ok _sf_bad=""
    for _sf_w in accept-line accept-and-hold accept-line-and-down-history accept-and-infer-next-history; do
        _sf_ok=""
        [[ ${widgets[$_sf_w]-} == user:_shellfirm_widget_$_sf_w ]] && _sf_ok=1
        [[ -z $_sf_ok && " ${(v)widgets} " == *" user:_shellfirm_widget_$_sf_w "* ]] && _sf_ok=1
        [[ -z $_sf_ok ]] && _sf_bad+=" $_sf_w=${widgets[$_sf_w]-}"
    done
    print -r -- "widgets=${${_sf_bad# }:-ok}"
    local _sf_k=${${(z)$(bindkey -M main '^M' 2>/dev/null)}[2]}
    [[ -n $_shellfirm_rewrapped ]] && print -r -- "rewrapped=${_shellfirm_rewrapped# }"
    [[ -o correct || -o correctall ]] && print -r -- "correct=checked"
    if [[ ${widgets[$_sf_k]-} == user:_shellfirm_widget_* ]]; then
        print -r -- "enter=ok"
    else
        case $_sf_k in
            accept-line|accept-and-hold|accept-line-and-down-history|accept-and-infer-next-history) print -r -- "enter=ok" ;;
            .accept-*) print -r -- "enter=bypass:$_sf_k" ;;
            *) print -r -- "enter=$(_shellfirm_enter_kind $_sf_k):$_sf_k" ;;
        esac
    fi
    _sf_k=${${(z)$(bindkey -M isearch '^M' 2>/dev/null)}[2]}
    [[ $_sf_k == _shellfirm_isearch_accept ]] && print -r -- "isearch=ok" || print -r -- "isearch=unchecked:$_sf_k"
    (( ${preexec_functions[(I)_shellfirm_preexec]} )) && print -r -- "precmd=ok" || print -r -- "precmd=missing"
    if [[ ${aliases[fc]-} == '_shellfirm_fc fc' ]] || (( ${precmd_functions[(I)_shellfirm_first_prompt]} )); then
        print -r -- "fc=ok"
    else
        print -r -- "fc=unchecked"
    fi
    # The zle-line-finish check (registered again when a plugin replaced it).
    _shellfirm_add_gate && print -r -- "line_finish=ok" || print -r -- "line_finish=missing"
    [[ -n $_shellfirm_bypass_warned ]] && print -r -- "bypass_seen=yes"
    [[ ${widgets[accept-line]-} == user:shellfirm-pre-command ]] && print -r -- "legacy_hook=active"
    local _sf_pl=""
    (( $+widgets[atuin-search] )) && _sf_pl+=" atuin"
    (( $+widgets[fzf-cd-widget] || $+widgets[fzf-file-widget] )) && _sf_pl+=" fzf"
    (( $+functions[_zsh_autosuggest_start] )) && _sf_pl+=" zsh-autosuggestions"
    (( $+functions[_zsh_highlight] )) && _sf_pl+=" zsh-syntax-highlighting"
    (( $+functions[zvm_init] )) && _sf_pl+=" zsh-vi-mode"
    (( $+functions[abbr] )) && _sf_pl+=" zsh-abbr"
    (( $+functions[prompt_powerlevel9k_setup] || $+functions[_p9k_precmd] )) && _sf_pl+=" powerlevel10k"
    (( $+functions[starship_precmd] || $+functions[prompt_starship_precmd] )) && _sf_pl+=" starship"
    print -r -- "plugins=${_sf_pl# }"
}
_shellfirm_doctor_probe() { __shellfirm_doctor_probe "$@" }

# Wrap the widget $1 so that it checks the line first, then runs what the
# widget did before.
_shellfirm_wrap_widget() {
    local _sf_w=$1
    functions[_shellfirm_widget_$_sf_w]="_shellfirm_widget $_sf_w \"\$@\""
    case ${widgets[$_sf_w]-} in
        user:_shellfirm_widget_*) ;;
        user:shellfirm-pre-command)
            # Hook pasted from shellfirm <= 0.3.10: replace it.
            print -ru2 -- "shellfirm: replaced an old shellfirm hook found in your shell startup files; remove it (run \`shellfirm init zsh\` to see where)."
            _shellfirm_orig_widget[$_sf_w]=.$_sf_w
            zle -N "$_sf_w" "_shellfirm_widget_$_sf_w" ;;
        user:*|completion:*)
            # A copy of the current widget, under a name of its own each time
            # (a plugin may have replaced an earlier copy).
            local -i _sf_n=1
            while (( $+widgets[_shellfirm_orig${_sf_n}_$_sf_w] )); do (( _sf_n++ )); done
            zle -A "$_sf_w" "_shellfirm_orig${_sf_n}_$_sf_w"
            _shellfirm_orig_widget[$_sf_w]=_shellfirm_orig${_sf_n}_$_sf_w
            zle -N "$_sf_w" "_shellfirm_widget_$_sf_w" ;;
        *)
            _shellfirm_orig_widget[$_sf_w]=.$_sf_w
            zle -N "$_sf_w" "_shellfirm_widget_$_sf_w" ;;
    esac
}

# Before every prompt: a plugin loaded after shellfirm may have replaced an
# accept-line widget (`zle -N accept-line auto-ls`) or bound Enter to a
# widget that calls zsh's built-in `.accept-line`. Wrap those again, before
# the first command runs. A widget chain that still reaches shellfirm's
# wrapper (zsh-autosuggestions keeps a copy of it) is left alone.
_shellfirm_rewrap() {
    [[ -n $_shellfirm_srcnote ]] && _shellfirm_note_src
    _shellfirm_add_gate
    # Nothing changed since the last prompt (no widget was added or
    # replaced, no command bypassed the wrappers): nothing to do, and no
    # `bindkey` subshell per prompt.
    local _sf_sig="${#widgets}:${widgets[accept-line]-}:${widgets[accept-and-hold]-}"
    [[ $_sf_sig == "$_shellfirm_rewrap_sig" ]] && return
    _shellfirm_rewrap_sig=$_sf_sig
    local _sf_w _sf_k _sf_vals=" ${(v)widgets} "
    for _sf_w in accept-line accept-and-hold accept-line-and-down-history accept-and-infer-next-history; do
        [[ $_sf_vals == *" user:_shellfirm_widget_$_sf_w "* ]] && continue
        _shellfirm_wrap_widget $_sf_w
        _shellfirm_rewrapped+=" $_sf_w"
    done
    for _sf_k in '^M' '^J'; do
        _sf_w=${${(z)$(bindkey -M main "$_sf_k" 2>/dev/null)}[2]}
        [[ -n $_sf_w && $_sf_w != (accept-line|accept-and-hold|accept-line-and-down-history|accept-and-infer-next-history|.*) ]] || continue
        [[ ${widgets[$_sf_w]-} == user:_shellfirm_widget_* ]] && continue
        [[ $(_shellfirm_enter_kind $_sf_w) == bypass ]] || continue
        _shellfirm_wrap_widget $_sf_w
        _shellfirm_rewrapped+=" $_sf_w"
    done
}
typeset -g _shellfirm_rewrapped="" _shellfirm_rewrap_sig=""
typeset -g _shellfirm_allowed="" _shellfirm_gated="" _shellfirm_approved=""

# Registers the zle-line-finish check (zsh 5.3+) as the last line-finish
# hook: after one added later (a plugin that rewrites $BUFFER there), and
# again when a plugin replaced the zle-line-finish widget with its own
# (`zle -N zle-line-finish f`), which is kept and runs first. Returns 1
# when it is not in place.
_shellfirm_add_gate() {
    setopt localoptions extendedglob
    local -a _sf_h
    local _sf_e _sf_cur=${widgets[zle-line-finish]-}
    local -i _sf_max=0 _sf_ours=-1 _sf_n=1
    zstyle -g _sf_h zle-line-finish widgets
    for _sf_e in $_sf_h; do
        if [[ $_sf_e == <->:_shellfirm_line_finish ]]; then
            _sf_ours=${_sf_e%%:*}
        elif [[ $_sf_e == <->:* ]] && (( ${_sf_e%%:*} > _sf_max )); then
            _sf_max=${_sf_e%%:*}
        fi
    done
    [[ $_sf_cur == user:azhw:zle-line-finish ]] && (( _sf_ours > _sf_max )) && return 0
    (( $+functions[add-zle-hook-widget] )) || autoload -Uz add-zle-hook-widget 2>/dev/null || return 1
    # The dispatcher (azhw:*) is defined the first time add-zle-hook-widget runs.
    (( $+functions[azhw:zle-line-finish] )) || add-zle-hook-widget -L >/dev/null 2>&1
    (( $+functions[azhw:zle-line-finish] )) || return 1
    _sf_h=(${_sf_h:#<->:_shellfirm_line_finish})
    if [[ -n $_sf_cur && $_sf_cur != user:azhw:zle-line-finish ]]; then
        # A plugin's own zle-line-finish: a copy of it stays a hook. A
        # plugin that sets the same widget again at every prompt keeps the
        # one copy it has (it must not run once more per prompt).
        local _sf_have=""
        for _sf_e in ${(M)_sf_h:#<->:_shellfirm_prev_lf*}; do
            [[ ${widgets[${_sf_e#*:}]-} == "$_sf_cur" ]] && { _sf_have=1; break; }
        done
        if [[ -z $_sf_have ]]; then
            while (( $+widgets[_shellfirm_prev_lf$_sf_n] )); do (( _sf_n++ )); done
            zle -A zle-line-finish _shellfirm_prev_lf$_sf_n 2>/dev/null && _sf_h+=("$(( ++_sf_max )):_shellfirm_prev_lf$_sf_n")
        fi
    fi
    (( $+widgets[_shellfirm_line_finish] )) || zle -N _shellfirm_line_finish
    _sf_h+=("$(( _sf_max + 1 )):_shellfirm_line_finish")
    zstyle zle-line-finish widgets "${_sf_h[@]}"
    zle -N zle-line-finish azhw:zle-line-finish
    [[ ${widgets[zle-line-finish]-} == user:azhw:zle-line-finish ]]
}

# Wrap once: re-sourcing ~/.zshrc must not wrap our own wrappers again
# (with plugins such as zsh-autosuggestions that would loop forever).
if (( ! ${+_shellfirm_zsh_loaded} )); then
typeset -g _shellfirm_zsh_loaded=1
() {
    local _sf_w
    for _sf_w in accept-line accept-and-hold accept-line-and-down-history accept-and-infer-next-history; do
        _shellfirm_wrap_widget $_sf_w
    done
    zle -N _shellfirm_isearch_accept
    local _sf_k
    for _sf_k in '^M' '^J'; do
        [[ $(bindkey -M isearch "$_sf_k" 2>/dev/null) == *undefined-key ]] && bindkey -M isearch "$_sf_k" _shellfirm_isearch_accept
    done
    if autoload -Uz add-zsh-hook 2>/dev/null; then
        add-zsh-hook preexec _shellfirm_preexec
        add-zsh-hook precmd _shellfirm_first_prompt
        add-zsh-hook precmd _shellfirm_rewrap
    fi
    _shellfirm_add_gate
    # Again when the editor starts, after every precmd hook: a plugin whose
    # precmd (run after shellfirm's) replaces zle-line-finish cannot take
    # the check away for that line.
    if (( $+functions[add-zle-hook-widget] )) || autoload -Uz add-zle-hook-widget 2>/dev/null; then
        zle -N _shellfirm_line_init
        add-zle-hook-widget line-init _shellfirm_line_init 2>/dev/null
    fi
}
fi
fi
"#;

/// fish: binds Enter / Ctrl-J (default and vi insert/normal modes), checks
/// the whole command line (Alt-Enter multi-line buffers, the abbreviation
/// fish is about to expand, small alias functions anywhere on the line),
/// routes `commandline -f execute` from plugins through the same check, and
/// runs an Enter binding to a fish function that was there before
/// (transient prompts) after the check.
const FISH_HOOK: &str = r#"# shellfirm hook for fish
if status is-interactive
    set -g __shellfirm_hook_version 14
    # Lets `shellfirm doctor` tell whether the shell it runs in loaded the hook
    # (SHELLFIRM_HOOK_LOAD changes at every load, also after `exec fish`).
    set -gx SHELLFIRM_HOOK_PID $fish_pid
    set -gx SHELLFIRM_HOOK_LOAD $fish_pid.(random)(random)
    set -g __shellfirm_warned 0
    set -g __shellfirm_seen 0
    set -g __shellfirm_bypass_warned 0

    function __shellfirm_warn_missing
        if test $__shellfirm_warned = 0
            set -g __shellfirm_warned 1
            echo >&2
            echo "shellfirm: binary not found in PATH; commands are NOT being checked" >&2
        end
    end

    # The expansion of the abbreviation under the cursor, which fish expands
    # when the line is executed (after this check reads the buffer).
    function __shellfirm_abbr_expansion
        set -l tok (builtin commandline -t)
        test -n "$tok"; or return 1
        set -l first (builtin commandline -po)[1]
        for line in (abbr --show)
            set -l parts (string split -m1 -- ' -- ' $line)
            set -q parts[2]; or continue
            set -l opts
            set -l toks
            printf '%s\n' $parts[1] | read -lat opts
            printf '%s\n' $parts[2] | read -lat toks
            set -l regex
            set -l func
            set -l position command
            set -l i 1
            while test $i -lt (count $opts)
                set -l next (math $i + 1)
                switch $opts[$i]
                    case --regex -r
                        set regex $opts[$next]
                    case --function -f
                        set func $opts[$next]
                    case --position -p
                        set position $opts[$next]
                end
                set i $next
            end
            if set -q regex[1]
                string match -qr -- $regex $tok; or continue
            else
                test "$tok" = "$toks[1]"; or continue
            end
            if test "$position" != anywhere; and test "$tok" != "$first"
                continue
            end
            if set -q func[1]
                $func $tok | string collect
            else
                set -e toks[1]
                string join ' ' -- $toks
            end
            return 0
        end
        return 1
    end

    # The source of the function $argv[1] when its body is worth checking
    # with a call: a small function that is not one of fish's own (those in
    # $__fish_data_dir/functions, unless redefined) and not shellfirm's.
    # Which functions are fish's own is remembered for the session (until a
    # line could define functions, or a file for one shows up in a function
    # directory that comes before fish's own).
    function __shellfirm_function_source
        set -l w $argv[1]
        string match -q '__shellfirm*' -- $w; and return 1
        if test "$__shellfirm_fn_path" != "$fish_function_path"
            set -g __shellfirm_fn_path $fish_function_path
            set -g __shellfirm_fn_shipped
        end
        if set -l i (contains -i -- $w $__shellfirm_fn_shipped)
            set -l own 1
            for d in $fish_function_path
                test "$d" = "$__fish_data_dir/functions"; and break
                if test -e "$d/$w.fish"
                    set own 0
                    break
                end
            end
            test $own = 1; and return 1
            set -e __shellfirm_fn_shipped[$i]
        end
        functions -q -- $w; or return 1
        set -l file (functions --details -- $w 2>/dev/null)
        if set -q __fish_data_dir[1]; and string match -q -- "$__fish_data_dir/functions/*" $file
            set -a __shellfirm_fn_shipped $w
            return 1
        end
        set -l body (functions --no-details -- $w 2>/dev/null | string collect)
        string match -q '*__shellfirm*' -- $body; and return 1
        if test (string length -- "$body") -gt 16000
            # Loaded from a file (a plugin): like a program on PATH, its
            # arguments are checked and its body is not read. Typed or
            # pasted into the session: not read, said so
            # (shell:function_depth_limit), never skipped silently.
            test -f "$file"; and return 1
            printf '%s' "shellfirm-function-depth-limit $w # a function too large for the hook to read ahead"
            return 0
        end
        printf '%s' $body
    end

    # Bodies of small functions (fish aliases) called anywhere in $argv[1],
    # with `$argv` replaced by the arguments of the call. The text is split
    # into commands with fish's own tokenizer, so redirections (`>&2`,
    # `2>&1`) stay what they are, and an argument is put in quoted when it
    # has characters that would change the body's meaning. Functions already
    # in __shellfirm_fn_seen are not repeated. $argv[2] = probe: print only
    # the names of the functions that would be expanded.
    function __shellfirm_function_bodies
        set -l toks
        printf '%s' $argv[1] | read -lzat toks
        set -l seg
        set -l subs
        set -l n 0
        set -l skip 0
        for t in $toks ';'
            if test $skip = 1
                set skip 0
                continue
            end
            if not contains -- $t ';' '|' '&&' '||' '&' \n
                # A redirection operator takes the next word (its target).
                if string match -qr -- '^([0-9]*[<>]+[&?|]?|&>>?)$' $t
                    set skip 1
                    continue
                end
                string match -qr -- '^[0-9]*[<>]|^&>' $t; and continue
                # Command substitutions run commands of their own.
                if string match -q -- '*(*' $t
                    set -a subs (string match -ra -- '\(([^()]*)\)' $t | string match -rv -- '^\(')
                end
                set -a seg $t
                continue
            end
            set -l words $seg
            set seg
            while set -q words[1]; and contains -- $words[1] and or not '!' begin time if while else eval
                set -e words[1]
            end
            set -q words[1]; or continue
            # `command x` / `builtin x` / `exec x` do not call a function.
            contains -- $words[1] command builtin exec; and continue
            contains -- $words[1] $__shellfirm_fn_seen; and continue
            functions -q -- $words[1]; or continue
            set -l body (__shellfirm_function_source $words[1]); or continue
            set -ga __shellfirm_fn_seen $words[1]
            if test "$argv[2]" = probe
                echo $words[1]
                continue
            end
            set -e words[1]
            set -l q
            for a in $words
                if string match -qr -- '[^A-Za-z0-9_./~:=@%+,-]' $a
                    set -a q "'"(string replace -a -- "'" "\\'" (string replace -a -- '\\' '\\\\' $a))"'"
                else
                    set -a q $a
                end
            end
            # A body that changes its arguments (`set -e argv[1]`, `set
            # argv ...`, argparse) keeps `$argv` as written: unknown.
            if string match -qr -- '(^|[\s;|&(])(set\s+(-\S+\s+)*argv\b|argparse\s)' $body
                printf '%s\n' $body
                continue
            end
            set body (string replace -a -- '$argv[' \x1f $body | string collect)
            for i in (seq (count $q))
                test $i -le 9; or break
                set body (string replace -a -- \x1f"$i]" $q[$i] $body | string collect)
            end
            set body (string replace -a -- '$argv' "$q" $body | string collect)
            string replace -a -- \x1f '$argv[' $body
        end
        for s in $subs[1..4]
            __shellfirm_function_bodies $s $argv[2]
        end
    end

    # A command named by a variable (`$c delete ...`, `eval $x`), with the
    # value the variable has in this session or from a `set` earlier on the
    # line: prints those commands with the value in place of the variable.
    function __shellfirm_var_commands
        string match -q -- '*$*' $argv[1]; or return
        test (string length -- "$argv[1]") -le 4000; or return
        set -l toks
        printf '%s' $argv[1] | read -lzat toks
        set -l names
        set -l values
        set -l seg
        set -l n 0
        for t in $toks ';'
            if not contains -- $t ';' '|' '&&' '||' '&' \n
                set -a seg $t
                continue
            end
            while set -q seg[1]; and contains -- $seg[1] and or not begin command builtin exec time eval
                set -e seg[1]
            end
            if test "$seg[1]" = set
                # `set [options] NAME VALUE...` with literal values.
                set -e seg[1]
                while set -q seg[1]; and string match -q -- '-*' $seg[1]
                    set -e seg[1]
                end
                if set -q seg[2]; and string match -qr -- '^[A-Za-z_][A-Za-z0-9_]*$' $seg[1]; and not string match -qr -- '[$(]' "$seg[2..-1]"
                    set -a names $seg[1]
                    set -a values (string join ' ' -- $seg[2..-1])
                end
            else if set -q seg[1]; and string match -qr -- '^\$[A-Za-z_][A-Za-z0-9_]*$' $seg[1]
                set -l name (string sub -s 2 -- $seg[1])
                set -l value
                if set -l i (contains -i -- $name $names)
                    set value $values[$i]
                else if set -q $name
                    set value (string join ' ' -- $$name)
                end
                if test -n "$value"
                    string join ' ' -- $value $seg[2..-1]
                    set n (math $n + 1)
                    test $n -ge 8; and return
                end
            end
            set seg
        end
    end

    # `bind KEY 'command'` installs code that runs when the key is pressed:
    # print those commands (not fish's input functions) so they are checked
    # when the binding is made, like a function that runs by itself.
    function __shellfirm_bind_commands
        string match -qr -- '(^|[;|&\n(]|\s)bind\s' $argv[1]; or return
        test (string length -- "$argv[1]") -le 4000; or return
        set -l toks
        printf '%s' $argv[1] | read -lzat toks
        set -l seg
        set -l fnames (bind --function-names)
        for t in $toks ';'
            if not contains -- $t ';' '|' '&&' '||' '&' \n
                set -a seg $t
                continue
            end
            while set -q seg[1]; and contains -- $seg[1] and or not begin command builtin
                set -e seg[1]
            end
            if test "$seg[1]" = bind
                set -e seg[1]
                set -l run 1
                while set -q seg[1]; and string match -q -- '-*' $seg[1]
                    switch $seg[1]
                        case -M --mode -m --sets-mode
                            set -e seg[1]
                        case -e --erase -a --all -L --list-modes -f --function-names -K --key-names -h --help
                            set run 0
                    end
                    set -e seg[1]
                end
                if test $run = 1; and set -q seg[2]
                    for c in $seg[2..-1]
                        contains -- $c $fnames; or echo $c
                    end
                end
            end
            set seg
        end
    end

    # Defining a function, abbreviation or alias runs nothing: those
    # statements are left out of the check (calling them is checked).
    # Prints the rest. Anything it cannot read for certain (an open quote,
    # a command substitution in a header) is printed unchanged.
    function __shellfirm_strip_defs
        set -l s $argv[1]
        if test (string length -- "$s") -gt 4000; or not string match -qr '(^|[;\n&|({]|\s)(function|abbr|alias)\s' -- $s
            printf '%s' $s
            return
        end
        set -l q ''
        set -l esc 0
        set -l par 0
        set -l spec 0
        set -l comment 0
        set -l stmt ''
        set -l stmts
        set -l specs
        for c in (string replace -a \n \x1e -- $s | string split '')
            if test $comment = 1
                test "$c" = \x1e; or continue
                set comment 0
            end
            if test $esc = 1
                set esc 0
                set stmt "$stmt$c"
                continue
            end
            if test -n "$q"
                test "$c" = '\\'; and set esc 1
                test "$c" = "$q"; and set q ''
                set stmt "$stmt$c"
                continue
            end
            switch $c
                case "'" '"'
                    set q $c
                case '\\'
                    set esc 1
                case '('
                    set par (math $par + 1)
                    set spec 1
                case ')'
                    set par (math $par - 1)
                case '|' '&' '<' '>' '$'
                    test $par = 0; and set spec 1
                case '#'
                    if test $par = 0; and string match -qr '(^|\s)$' -- $stmt
                        set comment 1
                        continue
                    end
                case ';' \x1e
                    if test $par = 0
                        set -a stmts $stmt
                        set -a specs $spec
                        set stmt ''
                        set spec 0
                        continue
                    end
            end
            set stmt "$stmt$c"
        end
        if test -n "$q"; or test $esc = 1; or test $par != 0
            printf '%s' $s
            return
        end
        set -a stmts $stmt
        set -a specs $spec
        # Functions that run by themselves (event handlers, the prompt, key
        # bindings) and those they call: redefining one is checked.
        set -l reach (__shellfirm_hook_reach)
        set -l depth 0
        set -l fnlevel -1
        set -l kept
        set -l i 0
        for st in $stmts
            set i (math $i + 1)
            set -l words (string trim -- $st | string split -n ' ')
            while set -q words[1]; and contains -- $words[1] and or not time
                set -e words[1]
            end
            set -l w $words[1]
            if test $fnlevel -ge 0
                switch "$w"
                    case function if while for begin switch
                        set depth (math $depth + 1)
                    case end
                        if test (count $words) -gt 1
                            printf '%s' $s
                            return
                        end
                        set depth (math $depth - 1)
                        test $depth -eq $fnlevel; and set fnlevel -1
                end
                continue
            end
            switch "$w"
                case function
                    if test $specs[$i] != 0
                        printf '%s' $s
                        return
                    end
                    # An event handler (`--on-event fish_prompt`, ...) and
                    # the functions fish calls by name (the prompt, the
                    # title, command-not-found) run by themselves: they are
                    # checked when they are defined.
                    if string match -qr -- '^(--on-(event|variable|signal|process-exit|job-exit)(=.*)?|-[a-zA-Z]*[evspj])$' $words[3..-1]; \
                        or contains -- "$words[2]" fish_prompt fish_right_prompt fish_mode_prompt fish_title fish_greeting fish_command_not_found $reach
                        set depth (math $depth + 1)
                        set -a kept (string replace -a \x1e \n -- $st)
                        continue
                    end
                    set fnlevel $depth
                    set depth (math $depth + 1)
                    continue
                case abbr alias
                    test $specs[$i] = 0; and continue
                case if while for begin switch
                    set depth (math $depth + 1)
                case end
                    set depth (math $depth - 1)
            end
            set -a kept (string replace -a \x1e \n -- $st)
        end
        if test $fnlevel -ge 0
            printf '%s' $s
            return
        end
        string join \n -- $kept
    end

    # The functions that run by themselves (event handlers, the prompt and
    # title, key bindings) and the functions they name, however deep.
    function __shellfirm_hook_reach
        set -l fns (functions -n)
        set -l todo (functions --handlers 2>/dev/null | string match -rv '^Event ' | string match -ra '[A-Za-z0-9_:.@+-]+') \
            fish_prompt fish_right_prompt fish_mode_prompt fish_title fish_greeting fish_command_not_found \
            (builtin bind 2>/dev/null | string replace -r -- '^bind( -M \S+| -m \S+| --preset| --user)* \S+ ' '' | string match -ra '[A-Za-z0-9_:.@+-]+')
        set -l seen
        while set -q todo[1]; and test (count $seen) -lt 128
            set -l next
            for w in $todo
                contains -- $w $fns; or continue
                contains -- $w $seen $next; and continue
                string match -q -- '__fish*' $w; and continue
                string match -q -- '__shellfirm*' $w; and continue
                set -a next $w
            end
            set todo
            for w in $next
                set -a seen $w
                # fish's own functions (from its data directory) are not read.
                set -l file (functions --details -- $w 2>/dev/null)
                string match -q -- "$__fish_data_dir/*" $file; and continue
                set -a todo (functions --no-details -- $w 2>/dev/null | string match -ra '[A-Za-z0-9_:.@+-]+')
            end
        end
        printf '%s\n' $seen
    end

    # After the check: run the Enter binding that was there before shellfirm
    # (a transient-prompt plugin, ...), else execute.
    function __shellfirm_run_enter
        set -l var __shellfirm_enter_$fish_bind_mode
        if set -q $var
            set -g __shellfirm_allowed (builtin commandline | string collect)
            for c in $$var
                if contains -- $c (builtin bind --function-names)
                    builtin commandline -f $c
                else if functions -q -- $c; and functions --no-details -- $c | string match -qr 'builtin\s+commandline'
                    # It runs the line with `builtin commandline -f execute`,
                    # which skips the check: a copy of it that goes through
                    # the checked `commandline` (a line it rewrote is checked).
                    functions --no-details -- $c | string replace -r -- '^function\s+\S+' "function __shellfirm_via_$c" \
                        | string replace -ar -- 'builtin(\s+)commandline\b' 'commandline' | source
                    eval __shellfirm_via_$c
                else
                    eval $c
                end
            end
            return
        end
        builtin commandline -f execute
    end

    # shellfirm prints settings problems (a stale SHELLFIRM_CONFIG_DIR, an
    # invalid setting) on every call. Shown once per session (and again when
    # SHELLFIRM_CONFIG_DIR changes) on a line of their own, not glued to the
    # line being edited; after that only above a challenge.
    function __shellfirm_probe_notes
        set -l key "$SHELLFIRM_CONFIG_DIR"
        set -q __shellfirm_notes; and test "$__shellfirm_notes_key" = "$key"; and return
        set -g __shellfirm_notes_key $key
        set -l w (command shellfirm pre-command --test --command=: 2>&1 >/dev/null | string collect)
        if test -n "$w"
            echo >&2
            printf '%s\n' $w >&2
        end
        # A SHELLFIRM_CONFIG_DIR that is not a directory: the checks use the
        # default directory (as shellfirm itself does) without saying it again.
        if test -n "$w"; and __shellfirm_stale_dir
            set w (env SHELLFIRM_CONFIG_DIR= shellfirm pre-command --test --command=: 2>&1 >/dev/null | string collect)
        end
        if test -n "$w"
            set -g __shellfirm_notes 1
        else
            set -g __shellfirm_notes 0
        end
    end
    # shellfirm, with the default configuration directory once a stale
    # SHELLFIRM_CONFIG_DIR was reported.
    function __shellfirm_pc
        if test "$__shellfirm_notes_key" = "$SHELLFIRM_CONFIG_DIR"; and __shellfirm_stale_dir
            env SHELLFIRM_CONFIG_DIR= shellfirm $argv
        else
            command shellfirm $argv
        end
    end
    function __shellfirm_stale_dir
        set -q SHELLFIRM_CONFIG_DIR; or return 1
        set -l t (string trim -- "$SHELLFIRM_CONFIG_DIR")
        test -n "$t"; and not test -d "$SHELLFIRM_CONFIG_DIR"
    end

    function __shellfirm_check
        set -g __shellfirm_seen 1
        set -e __shellfirm_allowed
        set -e __shellfirm_pending_change
        set -l cmd (builtin commandline | string collect)
        if test -z (string trim -- "$cmd" | string collect)
            __shellfirm_run_enter
            return
        end
        # This line can define functions: which are fish's own is looked up again.
        if string match -qr -- '(^|[^\w-])(function|funcsave|funced|functions|alias|source|\.)(\s|$)' $cmd
            set -g __shellfirm_fn_shipped
        end
        # Incomplete input (open block or quote): let fish add a new line.
        builtin commandline --is-valid
        if test $status -eq 2
            builtin commandline -f execute
            return
        end
        if not command -q shellfirm
            __shellfirm_warn_missing
            __shellfirm_run_enter
            return
        end
        set -l check $cmd
        set -l exp (__shellfirm_abbr_expansion | string collect)
        if test -n "$exp"
            set -l tok (builtin commandline -t)
            set -l pos (builtin commandline -C)
            set -l before (string sub -l $pos -- $cmd)
            if string match -q -- "*$tok" $before
                set -l start (math (string length -- $before) - (string length -- $tok))
                set -l pre ""
                test $start -gt 0; and set pre (string sub -l $start -- $cmd | string collect)
                set -l post (string sub -s (math $pos + 1) -- $cmd | string collect)
                set check "$pre$exp$post"
            else
                set check "$cmd"\n"$exp"
            end
        end
        set check (__shellfirm_strip_defs "$check" | string collect)
        if test -z (string trim -- "$check" | string collect)
            __shellfirm_run_enter
            return
        end
        # What the banner and the audit log show (not the function bodies).
        set -l shown $check
        set -l vc (__shellfirm_var_commands "$check" | string collect)
        test -n "$vc"; and set check "$check"\n"$vc"
        set -l bc (__shellfirm_bind_commands "$check" | string collect)
        test -n "$bc"; and set check "$check"\n"$bc"
        # Functions and aliases, and those their bodies call (`alias nk2
        # rmr`, `function w1; w2 $argv; end`), each once, however deep,
        # within a budget (64 functions, 64 KB). Past it the rest is not
        # read: a marker says so (shell:function_depth_limit).
        set -g __shellfirm_fn_seen
        set -l body (__shellfirm_function_bodies "$check" | string collect)
        set -l bodies
        set -l bytes 0
        while test -n "$body"
            set check "$check"\n"$body"
            set -a bodies $body
            set bytes (math $bytes + (string length -- "$body"))
            if test (count $__shellfirm_fn_seen) -gt 64; or test $bytes -gt 64000
                set -l deeper (__shellfirm_function_bodies "$body" probe)
                set -q deeper[1]; and set check "$check"\n"shellfirm-function-depth-limit $deeper[1] # calls more functions than the hook reads ahead"
                break
            end
            set body (__shellfirm_function_bodies "$body" | string collect)
        end
        # `eval $x` / `$c ...` inside those bodies: the session value.
        if set -q bodies[1]
            set -l bvc (__shellfirm_var_commands (string join \n -- $bodies | string replace -a \n ';' | string collect) | string collect)
            test -n "$bvc"; and set check "$check"\n"$bvc"
        end
        if test (string length -- "$check") -gt 30000
            # Too long to pass as one argument: checked through stdin, no prompt.
            printf '%s' $check | command shellfirm check --stdin --exit-code >/dev/null 2>&1
            set -l rc $status
            if test $rc -eq 0
                __shellfirm_run_enter
                return
            end
            echo >&2
            echo "shellfirm: blocked: this line is too long to confirm interactively and it matches a risky pattern (or could not be checked, exit $rc). Run it from a script file instead." >&2
            builtin commandline -f repaint
            return
        end
        set -l line
        test "$shown" != "$check"; and set line --line="$shown"
        __shellfirm_probe_notes
        if test "$__shellfirm_notes" = 1
            # Dry run first (quietly): the notes only show for risky lines.
            set -l out (__shellfirm_pc pre-command --test --command="$check" 2>/dev/null)
            set -l rc $status
            if test $rc -eq 0; and not string match -q '*skipped (below*' -- $out
                __shellfirm_run_enter
                return
            end
            echo >&2
        end
        # Exit 3: allowed after a passed challenge; the approved line is
        # printed by __shellfirm_approved_notice once fish has moved below
        # the line. (Set here for this call only, so a value exported by
        # the user changes nothing.)
        begin
            set -lx SHELLFIRM_SIGNAL_PROMPTED 1
            set -lx SHELLFIRM_DEFER_APPROVED 1
            __shellfirm_pc pre-command --command="$check" $line
        end
        switch $status
            case 0
                __shellfirm_run_enter
            case 3
                set -g __shellfirm_approved "$shown"
                set -e __shellfirm_approved_mark
                if isatty stderr; and test -n "$TERM"; and test "$TERM" != dumb
                    # fish redraws the line before it runs, starting as
                    # many rows up as the line takes (a long or wide line
                    # wraps). Keep room below the answer (no scrolling
                    # later), mark the row after it for the approved line,
                    # and move down so the redraw starts on that row, not
                    # over the answered challenge.
                    set -l n (__shellfirm_line_rows)
                    for i in (seq (math $n + 1))
                        echo >&2
                    end
                    printf '\e[%dA\e7' (math $n + 1) >&2
                    set -g __shellfirm_approved_mark 1
                    if test $n -gt 1
                        for i in (seq (math $n - 1))
                            echo >&2
                        end
                    end
                end
                __shellfirm_run_enter
            case 127
                echo >&2
                echo "shellfirm: warning: "(command -s shellfirm)" is installed but could not run (exit 127); this command was NOT checked. Run `shellfirm doctor`." >&2
                __shellfirm_run_enter
            case '*'
                # Blocked: keep the buffer so it can be edited. fish redraws
                # a multi-line buffer from where it starts: make room so the
                # messages above it stay visible.
                echo "shellfirm: the line is kept for editing (Ctrl-U clears it)." >&2
                set -l n (count (builtin commandline))
                if test $n -gt 1
                    for i in (seq $n)
                        echo >&2
                    end
                end
                builtin commandline -f repaint
        end
    end

    # The terminal rows the prompt and the buffer take (display columns:
    # CJK characters and emoji count two).
    function __shellfirm_line_rows
        set -l pw (string length --visible -- (fish_prompt 2>/dev/null)[-1] 2>/dev/null)
        test -n "$pw"; or set pw 0
        set -l cols $COLUMNS
        test -n "$cols"; and test "$cols" -gt 0 2>/dev/null; or set cols 80
        set -l rows 0
        for l in (builtin commandline)
            set -l w (math $pw + (string length --visible -- "$l"))
            set rows (math $rows + "max(1, ceil($w / $cols))")
        end
        test $rows -gt 0; or set rows 1
        echo $rows
    end

    # After a passed challenge: "approved, running ..." on the row after
    # the answer (in place of the line fish redrew there), right before the
    # command runs.
    function __shellfirm_approved_notice --on-event fish_preexec
        set -q __shellfirm_approved[1]; or return
        set -l a $__shellfirm_approved
        set -e __shellfirm_approved
        if set -q __shellfirm_approved_mark[1]
            set -e __shellfirm_approved_mark
            printf '\e8\r\e[J' >&2
        end
        if test "$__shellfirm_notes" = 0
            command shellfirm pre-command --approved-notice --command="$a"
        else
            command shellfirm pre-command --approved-notice --command="$a" 2>/dev/null
        end
    end

    # Plugins that run the line themselves with `commandline -f execute`
    # (atuin enter_accept, transient-prompt Enter bindings, ...) go through
    # the same check.
    if not functions -q commandline; or functions commandline | string match -q '*__shellfirm_check*'
        function commandline --description 'shellfirm: check the line before `commandline -f execute`'
            if contains -- execute $argv; and begin; contains -- -f $argv; or contains -- --function $argv; end
                set -l rest
                set -l changes 0
                # An earlier call in the same binding queued a function that
                # changes the line (`commandline -f accept-autosuggestion;
                # commandline -f execute`): it has not run yet.
                if set -q __shellfirm_pending_change; and test "$__shellfirm_pending_change" = (builtin commandline | string collect)
                    set changes 1
                end
                set -e __shellfirm_pending_change
                for a in $argv
                    switch $a
                        case execute
                        case -f --function repaint force-repaint repaint-mode expand-abbr suppress-autosuggestion beginning-of-line beginning-of-buffer backward-char cancel
                            set -a rest $a
                        case '*'
                            set -a rest $a
                            set changes 1
                    end
                end
                if test $changes = 1
                    # The other functions (accept-autosuggestion, history
                    # search, ...) only run after this binding returns and can
                    # change the line, so it cannot be checked now: run them
                    # and leave running the line to Enter (which is checked).
                    test (count $rest) -gt 1; and builtin commandline $rest
                    echo >&2
                    echo "shellfirm: press Enter to run the line (it is checked first)." >&2
                    builtin commandline -f repaint
                    return
                end
                if test (count $rest) -gt 1
                    builtin commandline $rest
                end
                # Already checked (shellfirm ran the previous Enter binding).
                if set -q __shellfirm_allowed; and test "$__shellfirm_allowed" = (builtin commandline | string collect)
                    set -e __shellfirm_allowed
                    builtin commandline -f execute
                    return
                end
                __shellfirm_check
                return
            end
            if begin; contains -- -f $argv; or contains -- --function $argv; end
                for a in $argv
                    switch $a
                        case -f --function repaint force-repaint repaint-mode expand-abbr suppress-autosuggestion beginning-of-line beginning-of-buffer end-of-line end-of-buffer backward-char forward-char cancel
                        case '*'
                            set -g __shellfirm_pending_change (builtin commandline | string collect)
                    end
                end
            end
            builtin commandline $argv
        end
    end

    # A command ran without passing through shellfirm (Enter rebound to
    # `execute` directly, ...): warn once.
    function __shellfirm_preexec --on-event fish_preexec
        set -e __shellfirm_allowed
        set -e __shellfirm_pending_change
        if test $__shellfirm_seen = 0; and test $__shellfirm_bypass_warned = 0
            set -g __shellfirm_bypass_warned 1
            set -l b (bind \r | string join '; ')
            echo "shellfirm: warning: this command did not pass through shellfirm (Enter was rebound: $b); commands are NOT being checked. Run `shellfirm doctor`." >&2
        end
        set -g __shellfirm_seen 0
    end

    # What key $argv[2] runs in bind mode $argv[1]: the user binding, else
    # the preset one (`bind` prints both), without the `bind ... KEY` part.
    function __shellfirm_binding
        set -l lines (builtin bind -M $argv[1] $argv[2] 2>/dev/null)
        set -l user (string match -v -- 'bind --preset*' $lines)
        set -q user[1]; or set user $lines
        string replace -r -- '^bind( -M \S+| -m \S+| --preset| --user)* \S+ ' '' $user
    end

    # Enter / Ctrl-J run the check. An Enter binding to a fish function that
    # was there before (transient prompts, ...) runs after the check.
    function __shellfirm_bind
        for mode in default insert
            set -l cur (__shellfirm_binding $mode \r)
            if set -q cur[1]; and not string match -q '*__shellfirm_check*' -- $cur
                set -l first (string split ' ' -- $cur[1])[1]
                if functions -q -- $first
                    eval set -g __shellfirm_enter_$mode $cur[1]
                end
            end
            builtin bind -M $mode \r __shellfirm_check 2>/dev/null
            builtin bind -M $mode \n __shellfirm_check 2>/dev/null
        end
    end
    __shellfirm_bind
    # `bind` that rebinds Enter / Ctrl-J (`bind \r execute`, typed or from a
    # plugin, also one that does it from a fish_prompt handler at every
    # prompt, loaded before or after shellfirm) does not take the check
    # away: the new binding runs after the check, as one that was there
    # before shellfirm loaded does.
    if not functions -q bind; or functions bind | string match -q '*__shellfirm_enter_*'
        function bind --description 'shellfirm: Enter stays checked when it is rebound'
            set -l mode default
            set -l a $argv
            set -l plain 1
            while set -q a[1]
                switch $a[1]
                    case -M --mode
                        set -e a[1]
                        set mode $a[1]
                    case --user
                    case --
                        set -e a[1]
                        break
                    case '-*'
                        # --preset, -e, -k, -m, -s, listing...: as it is.
                        set plain 0
                    case '*'
                        break
                end
                set -e a[1]
            end
            if test $plain = 1; and set -q a[2]; and contains -- $mode default insert; \
                and contains -- $a[1] \r \n enter ctrl-m ctrl-j; and not contains -- __shellfirm_check $a[2..-1]
                if contains -- $a[1] \r enter ctrl-m
                    set -g __shellfirm_enter_$mode $a[2..-1]
                end
                builtin bind -M $mode \r __shellfirm_check 2>/dev/null
                builtin bind -M $mode \n __shellfirm_check 2>/dev/null
                return 0
            end
            builtin bind $argv
        end
    end
    # Re-apply after fish_vi_key_bindings / fish_default_key_bindings.
    function __shellfirm_rebind --on-variable fish_key_bindings
        __shellfirm_bind
    end

    # `shellfirm doctor` runs this in a fresh interactive shell; run it in
    # this shell to check the live state: `__shellfirm_doctor_probe | shellfirm doctor --stdin`
    function __shellfirm_doctor_probe
        echo shell=fish
        echo shell_version=$version
        echo hook_version=$__shellfirm_hook_version
        echo pid=$fish_pid
        set -l bad
        set -l chained
        for mode in default insert
            set -l b (__shellfirm_binding $mode \r)
            # fish 4 names the key `enter`.
            set -q b[1]; or set b (__shellfirm_binding $mode enter)
            if set -q b[1]; and not string match -q '*__shellfirm_check*' -- $b
                # A function (a transient prompt plugin loaded after
                # shellfirm) that runs the line with the wrapped
                # `commandline -f execute` is still checked.
                set -l f (string split ' ' -- $b[1])[1]
                set -l body (functions --no-details -- $f 2>/dev/null | string collect)
                if test (count $b) = 1; and functions -q -- $f; and functions commandline | string match -q '*__shellfirm_check*'; \
                    and string match -qr 'commandline\s+(-f|--function)\s+(\S+\s+)*execute' -- $body; \
                    and not string match -qr 'builtin\s+commandline\s+(-f|--function)\s+(\S+\s+)*execute' -- $body
                    contains -- $f $chained; or set -a chained $f
                else
                    set -a bad "$mode:\\r="(string join ',' -- $b)
                end
            end
            set b (__shellfirm_binding $mode \n)
            if set -q b[1]; and not string match -q '*__shellfirm_check*' -- $b
                set -a bad "$mode:\\n="(string join ',' -- $b)
            end
        end
        if set -q bad[1]
            echo "enter="(string join ' ' -- $bad)
        else if set -q chained[1]
            echo "enter=chained:"(string join ',' -- $chained)
        else
            echo enter=ok
        end
        set -q __shellfirm_enter_default; and echo "enter_chain="(string join ' ' -- $__shellfirm_enter_default)
        if functions -q commandline; and functions commandline | string match -q '*__shellfirm_check*'
            echo execute_wrapper=ok
        else
            echo execute_wrapper=missing
        end
        test $__shellfirm_bypass_warned = 1; and echo bypass_seen=yes
        functions -q _shellfirm_check; and echo legacy_hook=active
        set -l pl
        functions -q _atuin_search; and set -a pl atuin
        functions -q _fzf_search_directory; and set -a pl fzf.fish
        functions -q _tide_prompt; or set -q tide_prompt_add_newline_before; and set -a pl tide
        if functions -q transient_execute
            if contains -- transient_execute $__shellfirm_enter_default $__shellfirm_enter_insert
                set -a pl transient:chained
            else
                set -a pl transient:wrapped
            end
        end
        set -l pls (string join ' ' -- $pl)
        echo "plugins=$pls"
    end
end
"#;

const NUSHELL_HOOK: &str = r#"# shellfirm hook for nushell
# nushell cannot cancel a command from a hook. A risky line made of external
# programs is challenged, and on deny PATH is cleared for that line so they
# cannot start. Built-ins (rm, mv, cp, save, ...), custom commands and
# programs run by path cannot be stopped: for those shellfirm only warns.
# SHELLFIRM_HOOK_PID lets `shellfirm doctor` tell whether this shell loaded the hook.
$env.SHELLFIRM_HOOK_PID = ($nu.pid | into string)
$env.config = ($env.config? | default {} | upsert hooks {|c| $c.hooks? | default {} })
$env.config.hooks = ($env.config.hooks | upsert pre_execution {|h| $h.pre_execution? | default [] })
$env.config.hooks = ($env.config.hooks | upsert pre_prompt {|h| $h.pre_prompt? | default [] })
$env.config.hooks.pre_execution = ($env.config.hooks.pre_execution | append {||
    let cmd = (commandline)
    if ($cmd | str trim | is-empty) { return }
    if (which shellfirm | is-empty) {
        if ($env.__SHELLFIRM_WARNED? | is-empty) {
            $env.__SHELLFIRM_WARNED = true
            print -e "shellfirm: binary not found in PATH; commands are NOT being checked"
        }
        return
    }
    if ($cmd | str length) > 30000 {
        # Too long to pass as one argument: checked through stdin, no prompt.
        let big = (try { $cmd | ^shellfirm check --stdin --exit-code | complete | get exit_code } catch { 1 })
        if $big == 0 { return }
        $env.__SHELLFIRM_SAVED_PATH = $env.PATH
        $env.PATH = []
        print -e "shellfirm: WARNING: this line is too long to confirm and it is risky (or could not be checked). External programs on it will not start; nushell built-ins like rm may still run."
        return
    }
    # Defining a custom command or an alias runs nothing (calling it is checked).
    let ast = (try { ast --json $cmd | get block | from json | get pipelines } catch { [] })
    if ($cmd | str trim) =~ '^(export\s+)?(def|alias)\s' and ($ast | length) == 1 and ($ast.0.elements | length) == 1 { return }
    # Aliases are replaced by what they stand for, and the source of small
    # custom commands is checked with the call, and so are the custom
    # commands and aliases that source calls, each once, however deep,
    # within a budget (32 commands, 32 KB). Past it the rest is not read: a
    # marker says so (shell:function_depth_limit).
    mut check = $cmd
    mut extra = []
    mut queue = [$cmd]
    mut seen = []
    mut level = 0
    mut nfn = 0
    mut bytes = 0
    mut over = false
    while ($queue | is-not-empty) and not $over {
        $level += 1
        mut next = []
        for text in $queue {
            if $over { break }
            let words = ($text | split row -r '[;|\n{}()]|&&|\|\|' | each {|s| $s | str trim | split row -r '\s+' | first } | where {|w| $w != "" } | uniq)
            for w in ($words | first 16) {
                if $w in $seen { continue }
                # `which` is cheap; the scope tables are only read for an
                # alias or a custom command.
                let t = (try { which $w | get type? | append "" | first } catch { "" })
                if $t not-in [alias custom] { continue }
                $seen = ($seen | append $w)
                let al = if $t == alias { try { scope aliases | where name == $w } catch { [] } } else { [] }
                if ($al | is-not-empty) {
                    if $level == 1 {
                        let re = ('(^|[;|\n{(]\s*|&&\s*|\|\|\s*)' + ($w | str replace -ra '([.^$*+?()\[\]{}|\\])' '\$1') + '(\s|$)')
                        $check = ($check | str replace -ra $re ('${1}' + ($al.0.expansion | str replace -a '$' '$$') + '${2}'))
                    } else {
                        $extra = ($extra | append $al.0.expansion)
                    }
                    $next = ($next | append $al.0.expansion)
                } else if $t == custom {
                    let src = (try { view source $w } catch { "" })
                    if ($src | str length) <= 2000 {
                        $nfn += 1
                        $bytes += ($src | str length)
                        if $nfn > 32 or $bytes > 32000 {
                            $extra = ($extra | append $"shellfirm-function-depth-limit ($w) # calls more functions than the hook reads ahead")
                            $over = true
                            break
                        }
                        $extra = ($extra | append $src)
                        $next = ($next | append $src)
                    }
                }
            }
        }
        $queue = $next
    }
    # `let c = "kubectl"; ^$c ...`: the program named by a variable set to a
    # literal on the same line.
    mut named = $check
    for l in ($cmd | parse -r r#'(?:^|[;\n|])\s*(?:let|mut)\s+(?P<n>[A-Za-z_][A-Za-z0-9_-]*)\s*=\s*["']?(?P<v>[^"'\s;|()]+)["']?\s*(?:$|[;\n|])'#) {
        $named = ($named | str replace -a ('^$' + $l.n) ('^' + $l.v))
    }
    # `^$env.K ...` / `$env.K ...`: the program named by an environment
    # variable, set on this line or earlier in the session.
    if ($named | str contains '$env.') {
        mut envs = {}
        for l in ($cmd | parse -r r#'\$env\.(?P<n>[A-Za-z_][A-Za-z0-9_]*)\s*=\s*["']?(?P<v>[^"'\s;|()]+)["']?'#) {
            $envs = ($envs | upsert $l.n $l.v)
        }
        for l in ($named | parse -r r#'(?:^|[;\n|({]\s*)\^?\$env\.(?P<n>[A-Za-z_][A-Za-z0-9_]*)'# | uniq) {
            let v = if ($l.n in $envs) { $envs | get $l.n } else { $env | get -i $l.n }
            if ($v | describe) == "string" and ($v | str trim | is-not-empty) {
                $named = ($named | str replace -ra ('\^?\$env\.' + $l.n + '(?![A-Za-z0-9_])') ('^' + $v))
            }
        }
    }
    if $named != $check { $extra = ($extra | append $named) }
    let full = ([$check] | append $extra | str join "\n")
    # Dry run first (no prompt, and no external context probes such as the
    # blast radius: the real check below runs them once). A match skipped
    # for its severity can still be raised by context: check it for real.
    let dry = (try { do { ^shellfirm pre-command --test --no-context $"--command=($full)" } | complete } catch { {exit_code: 1, stdout: ""} })
    if $dry.exit_code == 0 and not ($dry.stdout | str contains "skipped (below") { return }
    # Can shellfirm actually stop this line? Only external programs found
    # through PATH can be stopped (by clearing PATH).
    let heads = ($check | split row -r '[;|\n{}()]|&&|\|\|' | each {|s| $s | str trim | split row -r '\s+' | first } | where {|w| $w != "" })
    let unstoppable = ($check =~ '\$env\.PATH|with-env|load-env|overlay' or ($heads | any {|w|
        if ($w | str starts-with "^") {
            ($w | str contains "/")
        } else if ($w | str contains "/") {
            true
        } else {
            let t = (which $w | get type? | append "external" | first)
            $w in [rm mv cp save] or $t in [custom alias]
        }
    }))
    if $unstoppable {
        let ids = ($dry.stdout | lines | where {|l| $l starts-with "  - " } | each {|l| $l | str substring 4.. | split row " " | first } | str join ", ")
        $env.__SHELLFIRM_SAVED_PATH = $env.PATH
        $env.PATH = []
        print -e $"shellfirm: WARNING: this line is risky \(($ids)\), but shellfirm cannot stop nushell built-ins like rm, custom commands or programs run by path: they may still run. External programs on this line will not start."
        return
    }
    # The banner and challenge use the terminal; `complete` waits for the exit code.
    let rc = (^shellfirm pre-command --direct-tty $"--command=($full)" e> /dev/tty | complete | get exit_code)
    # 3: allowed after a passed challenge (SHELLFIRM_SIGNAL_PROMPTED=1 set
    # in the environment by the user).
    if $rc in [0 3] { return }
    if $rc == 127 {
        print -e "shellfirm: warning: shellfirm is installed but could not run (exit 127); this command was NOT checked. Run `shellfirm doctor`."
        return
    }
    $env.__SHELLFIRM_SAVED_PATH = $env.PATH
    $env.PATH = []
    print -e "shellfirm: external programs on this line will not start (nushell may report them as not found)."
})
$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append {||
    if ($env.__SHELLFIRM_SAVED_PATH? != null) {
        $env.PATH = $env.__SHELLFIRM_SAVED_PATH
        hide-env __SHELLFIRM_SAVED_PATH
        $env.LAST_EXIT_CODE = 1
    }
})"#;

const POWERSHELL_HOOK: &str = r#"# shellfirm hook for PowerShell
$global:__shellfirm_hook_version = 14
# Lets `shellfirm doctor` tell whether the shell it runs in loaded the hook.
$env:SHELLFIRM_HOOK_PID = "$PID"
Set-PSReadLineKeyHandler -Key Enter -ScriptBlock {
    $line = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

    if ([string]::IsNullOrWhiteSpace($line) -or -not (Get-Command shellfirm -CommandType Application -ErrorAction SilentlyContinue)) {
        [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
        return
    }

    Write-Host ""
    & shellfirm pre-command --direct-tty "--command=$line"
    # 3: allowed after a passed challenge (SHELLFIRM_SIGNAL_PROMPTED=1 in
    # the environment).
    if ($LASTEXITCODE -eq 0 -or $LASTEXITCODE -eq 3) {
        [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
    } else {
        [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
    }
}"#;

const ELVISH_HOOK: &str = r#"# shellfirm hook for elvish
use re
use str
# Lets `shellfirm doctor` tell whether the shell it runs in loaded the hook.
set-env SHELLFIRM_HOOK_PID (to-string $pid)
var shellfirm-warned = $false
var shellfirm-rebound-warned = $false
var shellfirm-orig-enter = $edit:insert:binding[Enter]
# The Enter binding that was there when shellfirm loaded. When the binding
# shellfirm runs after its check calls back into shellfirm (a plugin loaded
# after shellfirm that chains to "the previous Enter", which is shellfirm's),
# this one runs instead, so one Enter press is checked once and never loops.
var shellfirm-base-enter = $shellfirm-orig-enter
var shellfirm-in-enter = $false
var shellfirm-checked = ''
# True when elvish would wait for more input: the code has a parse error
# right at its end (an open block, list or string). Other errors (a
# variable defined in this session, a real syntax error) are not "incomplete".
fn shellfirm-incomplete {|cmd|
    if (not (has-external elvish)) {
        put $false
        return
    }
    var out = (try { e:elvish -norc -compileonly -json -c $cmd 2>/dev/null } catch { nop } | slurp)
    var errs = $nil
    try { set errs = (echo $out | from-json) } catch { }
    if (not-eq (kind-of $errs) list) {
        put $false
        return
    }
    var len = (str:to-utf8-bytes $cmd | count)
    for err $errs {
        if (and (has-key $err start) (== $err[start] $len)) {
            put $true
            return
        }
    }
    put $false
}
# The code in the option defaults of a lambda signature (`fn a {|&x=(code)|
# ... }`, `{|&x=(code)| ...}`): elvish runs it when the closure is made, so
# at definition time. One command per line ('' = none).
fn shellfirm-sig-code {|cmd|
    var out = ''
    if (str:contains $cmd '|') {
        for m [(re:find '\{\s*\|([^|]*)\|' $cmd)] {
            for g [(re:find '\(([^()]*)\)' $m[groups][1][text])] {
                set out = $out"\n"$g[groups][1][text]
            }
        }
    }
    put $out
}
# True when the line only defines a function (`fn name { ... }`): that runs
# nothing, and calling it is what gets checked. Option defaults with code
# in them run now: not "only".
fn shellfirm-only-fn {|cmd|
    var t = (str:trim-space $cmd)
    if (not (re:match '^fn\s+[^\s{]+\s*\{' $t)) {
        put $false
        return
    }
    if (not-eq (shellfirm-sig-code $t) '') {
        put $false
        return
    }
    var depth = (num 0)
    var q = ''
    var esc = $false
    var comment = $false
    var closed = $false
    for c [(str:split '' $t)] {
        if $closed {
            if (not (re:match '^\s$' $c)) {
                put $false
                return
            }
            continue
        }
        if $comment {
            if (eq $c "\n") { set comment = $false }
            continue
        }
        if $esc {
            set esc = $false
            continue
        }
        if (eq $q '"') {
            if (eq $c '\') { set esc = $true } elif (eq $c '"') { set q = '' }
            continue
        }
        if (eq $q "'") {
            if (eq $c "'") { set q = '' }
            continue
        }
        if (or (eq $c '"') (eq $c "'")) {
            set q = $c
        } elif (eq $c '#') {
            set comment = $true
        } elif (has-value ['{' '[' '('] $c) {
            set depth = (+ $depth 1)
        } elif (has-value ['}' ']' ')'] $c) {
            set depth = (- $depth 1)
            if (== $depth 0) { set closed = $true }
        }
    }
    put (and $closed (eq $q ''))
}
# The last line shellfirm checked (a chained binding that rewrote the line
# and called back into shellfirm had the new line checked already; when it
# was not let through, the binding did not accept it).
var shellfirm-last-checked = ''
var shellfirm-saved-paths = $nil
# The binding that ran after the check changed the line it accepts (an
# abbreviation-style plugin loaded before shellfirm: `set
# edit:current-command = ...` then the built-in Enter). elvish runs that new
# line once the binding returns and it cannot be taken back, so it is
# checked now; if it is not confirmed, external programs cannot start for
# that line (PATH is emptied until the next prompt).
fn shellfirm-recheck {|cmd|
    var now = $edit:current-command
    if (or (eq $now $cmd) (eq $now $shellfirm-last-checked) (eq (str:trim-space $now) '') (not (has-external shellfirm))) {
        return
    }
    var incomplete = $false
    if (str:contains-any $now '{[("''|^') {
        try { set incomplete = (shellfirm-incomplete $now) } catch { }
    }
    if $incomplete {
        return
    }
    set shellfirm-last-checked = $now
    var ok = $false
    try {
        set ok = ?(e:shellfirm pre-command --direct-tty --command=$now </dev/tty >/dev/tty 2>/dev/tty)
    } catch {
        set ok = $false
    }
    if $ok {
        return
    }
    set shellfirm-saved-paths = $paths
    set paths = []
    echo "shellfirm: the Enter binding changed the line after the check and it was not confirmed: external programs on it will not start (elvish built-ins still run)." >/dev/tty
}
# The check, for the line $line ($cmd: with what it calls). Exit 3 is
# "allowed after a passed challenge": with $defer the approved line is not
# printed yet (after-readline prints it below the line). Set for this call
# only, so a SHELLFIRM_SIGNAL_PROMPTED the user exported changes nothing.
var shellfirm-approved = ''
fn shellfirm-env {|name value|
    if (eq $value $nil) { unset-env $name } else { set-env $name $value }
}
fn shellfirm-check {|cmd line defer|
    var saved = [&]
    for n [SHELLFIRM_SIGNAL_PROMPTED SHELLFIRM_DEFER_APPROVED] {
        set saved[$n] = (if (has-env $n) { get-env $n } else { put $nil })
    }
    set-env SHELLFIRM_SIGNAL_PROMPTED 1
    if $defer { set-env SHELLFIRM_DEFER_APPROVED 1 } else { unset-env SHELLFIRM_DEFER_APPROVED }
    var r = $ok
    try {
        set r = ?(e:shellfirm pre-command --direct-tty --command=$cmd --line=$line </dev/tty >/dev/tty 2>/dev/tty)
    } finally {
        for n [SHELLFIRM_SIGNAL_PROMPTED SHELLFIRM_DEFER_APPROVED] { shellfirm-env $n $saved[$n] }
    }
    if (eq $r $ok) {
        put $true
        return
    }
    var st = -1
    try { set st = $r[reason][exit-status] } catch { }
    if (eq (to-string $st) 3) {
        if $defer { set shellfirm-approved = $line }
        put $true
        return
    }
    put $false
}
# Runs the binding after the check, marking that a check is in progress.
fn shellfirm-next {|next cmd|
    var was-in = $shellfirm-in-enter
    var was-checked = $shellfirm-checked
    set shellfirm-in-enter = $true
    set shellfirm-checked = $cmd
    try { $next } finally {
        set shellfirm-in-enter = $was-in
        set shellfirm-checked = $was-checked
    }
    if (not $was-in) {
        shellfirm-recheck $cmd
    }
}
# A fn (not a bare lambda): `return` only works inside fn.
fn shellfirm-enter {
    var cmd = $edit:current-command
    var next = $shellfirm-orig-enter
    if (not $shellfirm-in-enter) {
        set shellfirm-last-checked = ''
    }
    if $shellfirm-in-enter {
        # Called back from the binding shellfirm runs after its check: run
        # the binding from before shellfirm loaded. The line is checked
        # again only when that binding changed it.
        set next = $shellfirm-base-enter
        if (eq $cmd $shellfirm-checked) {
            $next
            return
        }
    }
    if (eq (str:trim-space $cmd) '') {
        shellfirm-next $next $cmd
        return
    }
    if (not (has-external shellfirm)) {
        if (not $shellfirm-warned) {
            set shellfirm-warned = $true
            echo "shellfirm: binary not found in PATH; commands are NOT being checked" >/dev/tty
        }
        shellfirm-next $next $cmd
        return
    }
    # Code that does not parse yet is never run: let Enter insert the
    # newline, and check the block once it is complete.
    var incomplete = $false
    if (str:contains-any $cmd '{[("''|^') {
        try { set incomplete = (shellfirm-incomplete $cmd) } catch { }
    }
    if $incomplete {
        shellfirm-next $next $cmd
        return
    }
    var only-fn = $false
    try { set only-fn = (shellfirm-only-fn $cmd) } catch { }
    if $only-fn {
        shellfirm-next $next $cmd
        return
    }
    set shellfirm-last-checked = $cmd
    var ok = $false
    try {
        if (> (count $cmd) 30000) {
            # Too long to pass as one argument: checked through stdin, no prompt.
            set ok = ?(print $cmd | e:shellfirm check --stdin --exit-code >/dev/null 2>/dev/null)
            if (not $ok) {
                echo "shellfirm: blocked: this line is too long to confirm interactively and it matches a risky pattern (or could not be checked). Run it from a script file instead." >/dev/tty
            }
        } else {
            set ok = (shellfirm-check $cmd(shellfirm-sig-code $cmd) $cmd $true)
        }
    } catch err {
        echo "shellfirm: could not check this line ("(to-string $err[reason])"); command not run" >/dev/tty
        set ok = $false
    }
    if $ok {
        shellfirm-next $next $cmd
    } else {
        edit:notify 'shellfirm: the line is kept for editing (Ctrl-U clears it).'
        edit:redraw &full=$true
    }
}
# Load once: evaluating this file again (reloading the rc) must not wrap
# shellfirm's own Enter binding, which would check every line twice. The
# marker holds this shell's pid, so a child elvish installs its own.
fn shellfirm-marker {|b| put $pid':'(repr $b) }
if (not-eq (shellfirm-marker $edit:insert:binding[Enter]) (if (has-env __SHELLFIRM_ELV_ENTER) { get-env __SHELLFIRM_ELV_ENTER } else { put '' })) {
    var shellfirm-binding = { shellfirm-enter }
    set edit:insert:binding[Enter] = $shellfirm-binding
    set-env __SHELLFIRM_ELV_ENTER (shellfirm-marker $shellfirm-binding)
    # Enter rebound after shellfirm loaded (a plugin, `set
    # edit:insert:binding[Enter] = ...`): before the next prompt, check
    # the line first again, then run that binding. This hook keeps itself
    # the last one, so a plugin that rebinds Enter at every prompt from a
    # hook added later is wrapped too.
    var shellfirm-before = $nil
    set shellfirm-before = {
        # PATH emptied for a line shellfirm did not let through: back now.
        if (not-eq $shellfirm-saved-paths $nil) {
            set paths = $shellfirm-saved-paths
            set shellfirm-saved-paths = $nil
        }
        set shellfirm-last-checked = ''
        var hooks = $edit:before-readline
        if (and (> (count $hooks) 0) (not-eq $hooks[-1] $shellfirm-before)) {
            set edit:before-readline = [(each {|h| if (not-eq $h $shellfirm-before) { put $h } } $hooks) $shellfirm-before]
        }
        var cur = $edit:insert:binding[Enter]
        if (not-eq (repr $cur) (repr $shellfirm-binding)) {
            set shellfirm-orig-enter = $cur
            set edit:insert:binding[Enter] = $shellfirm-binding
            if (not $shellfirm-rebound-warned) {
                set shellfirm-rebound-warned = $true
                echo "shellfirm: Enter was rebound after shellfirm loaded; shellfirm now checks the line first, then runs that binding." >/dev/tty
            }
        }
    }
    set edit:before-readline = [$@edit:before-readline $shellfirm-before]
    # The last moment before elvish runs the line: one that did not pass
    # through shellfirm's Enter (rebound by a hook that ran after
    # shellfirm's at this prompt) is checked here. Not confirmed: external
    # programs on it cannot start (PATH is emptied until the next prompt).
    # (No `return` here: it only works inside fn.)
    set edit:after-readline = [$@edit:after-readline {|line|
        # After a passed challenge: "approved, running ..." now, on the row
        # elvish left blank below the answer.
        if (not-eq $shellfirm-approved '') {
            var a = $shellfirm-approved
            set shellfirm-approved = ''
            if (and (not-eq $E:TERM dumb) (not-eq $E:TERM '')) {
                try { print "\e[A" >/dev/tty } catch { }
            }
            try { e:shellfirm pre-command --approved-notice --command=$a </dev/tty >/dev/tty 2>/dev/null } catch { }
        }
        var skip = (or (eq $line $shellfirm-last-checked) (eq (str:trim-space $line) '') (not (has-external shellfirm)))
        if (not $skip) {
            try { set skip = (shellfirm-only-fn $line) } catch { }
        }
        if (not $skip) {
            set shellfirm-last-checked = $line
            var ok = $false
            try {
                set ok = (shellfirm-check $line(shellfirm-sig-code $line) $line $false)
            } catch {
                set ok = $false
            }
            if (not $ok) {
                set shellfirm-saved-paths = $paths
                set paths = []
                echo "shellfirm: this line did not pass through shellfirm's Enter (it was rebound) and was not confirmed: external programs on it will not start (elvish built-ins still run)." >/dev/tty
            }
        }
    }]
}"#;

const XONSH_HOOK: &str = r#"# shellfirm hook for xonsh
import ast as _sf_ast
import builtins as _sf_builtins
import inspect as _sf_inspect
import os as _sf_os
import re as _sf_re
import shlex as _sf_shlex
import shutil as _sf_shutil
import subprocess as _sf_subprocess
import sys as _sf_sys

_sf_state = {"warned": False, "pending": None}
# Lets `shellfirm doctor` tell whether the shell it runs in loaded the hook.
__xonsh__.env["SHELLFIRM_HOOK_PID"] = str(_sf_os.getpid())


def _sf_keep_for_editing(cmd):
    """Put a blocked line back into the next prompt so it can be edited."""
    _sf_state["pending"] = cmd
    shell = getattr(__xonsh__.shell, "shell", None)
    prompter = getattr(shell, "prompter", None)
    if prompter is None:
        # readline backend: a queued line without a newline is put into the
        # next prompt for editing.
        queue = getattr(shell, "cmdqueue", None)
        if queue is not None:
            _sf_state["pending"] = None
            queue.append(cmd)
        return
    if getattr(prompter, "_sf_wrapped", False):
        return
    orig_prompt = prompter.prompt

    def _sf_prompt(*args, **kwargs):
        pending = _sf_state["pending"]
        if pending:
            _sf_state["pending"] = None
            kwargs["default"] = pending
        return orig_prompt(*args, **kwargs)

    prompter.prompt = _sf_prompt
    prompter._sf_wrapped = True


def _sf_code_words(code, out, depth=0):
    """The string constants of a function: the commands a callable alias
    runs (`git push --force ...` compiles to a list of its words)."""
    for c in code.co_consts:
        if isinstance(c, str):
            out.append(c)
        elif isinstance(c, (tuple, frozenset)):
            # `['kubectl', 'delete'] + a` keeps its words in a tuple.
            out.extend(w for w in c if isinstance(w, str))
        elif hasattr(c, "co_consts") and depth < 3:
            _sf_code_words(c, out, depth + 1)


def _sf_expand(line):
    """The line with aliases replaced by what they run, plus the commands
    of callable aliases and literal `@([...])` / `subprocess.run([...])`."""
    extra = []
    try:
        aliases = __xonsh__.aliases
    except Exception:
        aliases = {}
    parts = _sf_re.split(r"(\|\||&&|[;|\n]|\band\b|\bor\b)", line)
    out = []
    for seg in parts[:64]:
        # An alias can name another alias (`nk2` -> `rmr` -> `rm -rf`):
        # follow the chain, each alias once, at most 10 deep.
        seen = set()
        while len(seen) < 10:
            m = _sf_re.match(r"^(\s*)([^\s()]+)(.*)$", seg, _sf_re.S)
            name = m.group(2) if m else None
            if not name or name in seen or name not in aliases:
                break
            seen.add(name)
            try:
                value = aliases[name]
            except Exception:
                break
            if isinstance(value, (list, tuple)):
                seg = m.group(1) + " ".join(_sf_shlex.quote(str(w)) for w in value) + m.group(3)
            elif hasattr(value, "src") and isinstance(getattr(value, "src"), str):
                seg = m.group(1) + value.src + m.group(3)
            elif callable(value):
                try:
                    src = _sf_inspect.getsource(value)
                except Exception:
                    words = []
                    code = getattr(value, "__code__", None)
                    if code is not None:
                        _sf_code_words(code, words)
                    src = " ".join(words)
                if len(src) <= 2000:
                    extra.append(src)
                break
            else:
                break
        out.append(seg)
    text = "".join(out) + "".join(parts[64:])
    for m in _sf_re.finditer(r"(?:@\(|subprocess\.(?:run|call|check_call|check_output|Popen)\()\s*(\[[^\]]*\])", "\n".join([line] + extra)):
        try:
            words = _sf_ast.literal_eval(m.group(1))
            extra.append(" ".join(_sf_shlex.quote(str(w)) for w in words))
        except Exception:
            pass
    # `@($K) delete ...` / `@(cmd)`: the command named by an environment or
    # Python variable, set on this line or earlier in the session.
    if "@(" in line:
        env_set = dict(_sf_re.findall(r"\$([A-Za-z_]\w*)\s*=\s*['\"]([^'\"]*)['\"]", line))
        py_set = {}
        for name, value in _sf_re.findall(r"(?:^|[;\n])\s*([A-Za-z_]\w*)\s*=\s*(\[[^\]]*\]|'[^']*'|\"[^\"]*\")", line):
            try:
                py_set[name] = _sf_ast.literal_eval(value)
            except Exception:
                pass
        for m in _sf_re.finditer(r"@\(\s*(\$?)([A-Za-z_]\w*)\s*\)([^\n;|]*)", line):
            name = m.group(2)
            value = None
            try:
                if m.group(1):
                    value = env_set[name] if name in env_set else __xonsh__.env.get(name)
                elif name in py_set:
                    value = py_set[name]
                else:
                    value = (getattr(__xonsh__, "ctx", None) or {}).get(name)
            except Exception:
                value = None
            if isinstance(value, str):
                words = [value]
            elif isinstance(value, (list, tuple)) and all(isinstance(w, (str, int, float)) for w in value):
                words = [str(w) for w in value]
            else:
                continue
            if words and len(" ".join(words)) <= 4000:
                extra.append(" ".join(_sf_shlex.quote(w) for w in words) + m.group(3))
    # xonsh's own subprocess forms (`![...]`, `$[...]`, `!(...)`, `$(...)`),
    # shell strings handed to Python (`os.system('...')`, `subprocess.run(
    # '...', shell=True)`, `evalx('...')`) and `$K ...` (an environment
    # variable as the command): the commands they run.
    try:
        extra.extend(_sf_hidden_commands("\n".join([line] + extra), line))
    except Exception:
        pass
    return "\n".join([text] + extra)


def _sf_env_value(name, line):
    """The value of $name: set on this line (`$K = 'kubectl'`), else in the session."""
    m = _sf_re.search(r"\$" + _sf_re.escape(name) + r"\s*=\s*(['\"])([^'\"]*)\1", line)
    if m:
        return m.group(2)
    try:
        v = __xonsh__.env.get(name)
    except Exception:
        return None
    if isinstance(v, (list, tuple)) and all(isinstance(w, str) for w in v):
        return " ".join(v)
    return v if isinstance(v, str) else None


_SF_OPENERS = {"![": "]", "$[": "]", "!(": ")", "$(": ")"}
_SF_SHELL_CALL = _sf_re.compile(
    r"\b(os\.system|os\.popen|evalx|execx|subprocess\.(?:run|call|check_call|check_output|Popen|getoutput|getstatusoutput))\s*\("
)
_SF_STRING = _sf_re.compile(
    r"\s*([rRbBuU]?)('{3}[\s\S]*?'{3}|\"{3}[\s\S]*?\"{3}|'(?:[^'\\]|\\.)*'|\"(?:[^\"\\]|\\.)*\")"
)


def _sf_closing(text, start, close):
    """Index of the bracket that closes the one before `start` (quotes and
    nested brackets skipped), or -1."""
    pairs = {"(": ")", "[": "]", "{": "}"}
    stack = [close]
    i, n, q = start, len(text), None
    while i < n:
        c = text[i]
        if q:
            if c == "\\":
                i += 2
                continue
            if c == q:
                q = None
        elif c in "'\"":
            q = c
        elif c in pairs:
            stack.append(pairs[c])
        elif c in ")]}":
            if c != stack[-1]:
                return -1
            stack.pop()
            if not stack:
                return i
        i += 1
    return -1


def _sf_literal_or_name(args):
    """The first argument of a call: a string literal, or a Python variable
    holding a string or a list of words; None when it is not known."""
    lit = _SF_STRING.match(args)
    if lit:
        try:
            value = _sf_ast.literal_eval(lit.group(1) + lit.group(2))
        except Exception:
            return None
        if isinstance(value, bytes):
            value = value.decode("utf-8", "replace")
        return value if isinstance(value, str) else None
    name = _sf_re.match(r"\s*([A-Za-z_]\w*)\s*[,)]", args + ")")
    if not name:
        return None
    try:
        v = (getattr(__xonsh__, "ctx", None) or {}).get(name.group(1))
    except Exception:
        return None
    if isinstance(v, str):
        return v
    if isinstance(v, (list, tuple)) and all(isinstance(w, str) for w in v):
        return " ".join(_sf_shlex.quote(w) for w in v)
    return None


def _sf_hidden_commands(src, line):
    out = []
    todo = [src]
    rounds = 0
    while todo and rounds < 32:
        text = todo.pop()
        rounds += 1
        i = 0
        while i < len(text) - 1:
            op = text[i:i + 2]
            if op in _SF_OPENERS and not (i > 0 and text[i - 1] == "\\"):
                end = _sf_closing(text, i + 2, _SF_OPENERS[op])
                if end < 0:
                    break
                inner = text[i + 2:end]
                if inner.strip():
                    out.append(inner)
                    todo.append(inner)
                i = end + 1
                continue
            i += 1
        for m in _SF_SHELL_CALL.finditer(text):
            func = m.group(1)
            end = _sf_closing(text, m.end(), ")")
            args = text[m.end():end] if end >= 0 else text[m.end():]
            if _sf_re.match(r"\s*[\[(]", args):
                continue  # a literal list of words: read above
            shell_text = not func.startswith("subprocess.") or func.endswith("output") \
                or _sf_re.search(r"\bshell\s*=\s*True\b", args)
            value = _sf_literal_or_name(args)
            if value is not None:
                if value.strip():
                    out.append(value)
                    todo.append(value)
            elif shell_text:
                # A shell string that is only known when it runs.
                out.append("shellfirm-unknown-shell-payload " + func)
        # `$K delete ...`: the program named by an environment variable.
        for m in _sf_re.finditer(r"(?:^|[;\n|&(]|\band\b|\bor\b)[ \t]*\$([A-Za-z_]\w*)(?![\w\[(])(?![ \t]*=)([^\n;|&)]*)", text):
            v = _sf_env_value(m.group(1), line)
            if v and v.strip() and len(v) <= 4000:
                out.append(v + m.group(2))
    return out


def _sf_only_alias_def(line):
    """True when the line only defines an alias (`aliases['k'] = 'rm -rf'`,
    a list of words, or a lambda): that runs nothing, and calling the alias
    is what gets checked. A value that runs something now is not skipped."""
    try:
        tree = _sf_ast.parse(line)
    except Exception:
        return False
    if len(tree.body) != 1:
        return False
    node = tree.body[0]
    if isinstance(node, _sf_ast.Assign):
        targets = node.targets
    elif isinstance(node, (_sf_ast.AnnAssign, _sf_ast.AugAssign)):
        targets = [node.target]
    else:
        return False
    for t in targets:
        if not (isinstance(t, _sf_ast.Subscript) and isinstance(t.value, _sf_ast.Name) and t.value.id == "aliases"):
            return False
        if not isinstance(t.slice, _sf_ast.Constant):
            return False
    v = node.value
    if v is None:
        return False
    if isinstance(v, _sf_ast.Lambda):
        # Default values are evaluated when the lambda is made.
        return all(d is None or isinstance(d, _sf_ast.Constant) for d in v.args.defaults + v.args.kw_defaults)
    try:
        _sf_ast.literal_eval(v)
        return True
    except Exception:
        return False


def _sf_check_line(cmd):
    if not cmd or not cmd.strip():
        return cmd
    if _sf_shutil.which("shellfirm") is None:
        if not _sf_state["warned"]:
            _sf_state["warned"] = True
            print("shellfirm: binary not found in PATH; commands are NOT being checked")
        return cmd
    line = cmd.rstrip("\n")
    # Defining a function runs nothing (calling it, or the alias that
    # names it, is what gets checked).
    rows = line.split("\n")
    if _sf_re.match(r"def\s+\w+\s*\(", rows[0]) and all(not r.strip() or r[:1].isspace() for r in rows[1:]):
        return cmd
    if "aliases" in line and _sf_only_alias_def(line):
        return cmd
    try:
        check = _sf_expand(line)
    except Exception:
        check = line
    try:
        if len(check.encode("utf-8", "surrogateescape")) > 100000:
            # Too long to pass as one argument: checked through stdin, no prompt.
            rc = _sf_subprocess.run(
                ["shellfirm", "check", "--stdin", "--exit-code"],
                input=check.encode("utf-8", "surrogateescape"),
                stdout=_sf_subprocess.DEVNULL,
                stderr=_sf_subprocess.DEVNULL,
            ).returncode
            if rc != 0:
                print("shellfirm: blocked: this line is too long to confirm interactively and it matches a risky pattern (or could not be checked). Run it from a script file instead.")
        else:
            # The banner and the audit log show the line as typed, not
            # the expansion checked with it.
            argv = ["shellfirm", "pre-command", "--direct-tty", "--command=" + check]
            if check != line:
                argv.append("--line=" + line)
            rc = _sf_subprocess.call(argv)
    except FileNotFoundError as e:
        # Found on PATH but it does not start (missing interpreter/library).
        print("shellfirm: warning: shellfirm is installed but could not run ({}); this command was NOT checked. Run `shellfirm doctor`.".format(e))
        return cmd
    except OSError as e:
        # Fail closed: the binary exists but could not be run.
        print("shellfirm: could not run shellfirm ({}); command not run".format(e))
        rc = 1
    if rc == 127:
        print("shellfirm: warning: shellfirm is installed but could not run (exit 127); this command was NOT checked. Run `shellfirm doctor`.")
    # 3: allowed after a passed challenge (SHELLFIRM_SIGNAL_PROMPTED=1 in
    # the environment).
    if rc in (0, 3, 127):
        return cmd
    print("shellfirm: the line is kept for editing.")
    _sf_keep_for_editing(line)
    return ""


# Load once: sourcing this file again (reloading the rc) only updates the
# functions above; the handler is registered a single time.
if not getattr(_sf_builtins, "_shellfirm_xonsh_loaded", False):
    _sf_builtins._shellfirm_xonsh_loaded = True

    @events.on_transform_command
    def _shellfirm_transform(cmd, **kwargs):
        # prompt_toolkit's Enter asks whether the line compiles
        # (`can_compile` runs the transform too): nothing runs there, and
        # the line is checked once, when it is run.
        f = _sf_sys._getframe(1)
        for _ in range(6):
            if f is None:
                break
            if f.f_code.co_name == "can_compile":
                return cmd
            f = f.f_back
        return _sf_check_line(cmd)"#;

/// Oils (OSH/YSH): DEBUG trap + history (no `bind -x` in Oils).
const OILS_HOOK: &str = r#"# shellfirm hook for Oils (OSH/YSH)
__shellfirm_hook_version=14
# Lets `shellfirm doctor` tell whether the shell it runs in loaded the hook.
export SHELLFIRM_HOOK_PID=$$
__shellfirm_ready=""
__shellfirm_histnum="__sf_none__"
__shellfirm_blocked=""

__shellfirm_prompt() {
    __shellfirm_ready="1"
    __shellfirm_blocked=""
}

__shellfirm_hook() {
    [[ "$BASH_COMMAND" == __shellfirm_* || "${FUNCNAME[1]:-}" == __shellfirm_* ]] && return 0
    if [[ -z "$__shellfirm_ready" ]]; then
        [[ -n "$__shellfirm_blocked" ]] && return 1
        return 0
    fi
    [[ -n "${COMP_LINE:-}" ]] && return 0
    command -v shellfirm >/dev/null 2>&1 || return 0

    local histnum
    histnum=$(HISTTIMEFORMAT='' builtin history 1 | awk '{print $1}')
    [[ "$histnum" == "$__shellfirm_histnum" ]] && return 0

    __shellfirm_ready=""
    __shellfirm_histnum="$histnum"

    local full_cmd
    full_cmd=$(HISTTIMEFORMAT='' builtin history 1 | sed 's/^[ ]*[0-9]*[ ]*//')
    [[ -z "$full_cmd" ]] && return 0

    shellfirm pre-command --command="$full_cmd"
    case $? in
        0|3|127) return 0 ;;
    esac
    __shellfirm_blocked="1"
    return 1
}

PROMPT_COMMAND="__shellfirm_prompt${PROMPT_COMMAND:+$'\n'$PROMPT_COMMAND}"
shopt -s extdebug
trap '__shellfirm_hook' DEBUG
"#;

/// Hooks written by shellfirm <= 0.3.10, kept verbatim so they can be found
/// and removed (`--uninstall`) or upgraded.
mod legacy {
    pub const NUSHELL_HOOK: &str = r#"# shellfirm hook for nushell
$env.config.hooks.pre_execution = (
    $env.config.hooks.pre_execution | append {||
        let cmd = (commandline)
        if ($cmd | str trim | is-empty) {
            return
        }
        if ($cmd | str contains "shellfirm") {
            return
        }
        let result = (do { shellfirm pre-command -c $cmd } | complete)
        if $result.exit_code != 0 {
            commandline edit ""
        }
    }
)"#;

    pub const POWERSHELL_HOOK: &str = r#"# shellfirm hook for PowerShell
if (Get-Command shellfirm -ErrorAction SilentlyContinue) {
    Set-PSReadLineKeyHandler -Key Enter -ScriptBlock {
        $line = $null
        $cursor = $null
        [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

        if ([string]::IsNullOrWhiteSpace($line)) {
            [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
            return
        }

        if ($line -match 'shellfirm') {
            [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
            return
        }

        Write-Host ""
        shellfirm pre-command -c $line 2>$null
        if ($LASTEXITCODE -eq 0) {
            [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
        } else {
            [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
        }
    }
} else {
    Write-Warning "shellfirm binary not found. Install: https://github.com/kaplanelad/shellfirm#installation"
}"#;

    pub const ELVISH_HOOK: &str = r#"# shellfirm hook for elvish
if (not ?(which shellfirm &>/dev/null)) {
    echo "shellfirm binary not found. Install: https://github.com/kaplanelad/shellfirm#installation"
} else {
    set edit:insert:binding[Enter] = {
        var cmd = (edit:current-command)
        if (eq $cmd "") {
            edit:smart-enter
            return
        }
        if (str:contains $cmd "shellfirm") {
            edit:smart-enter
            return
        }
        try {
            echo ""
            shellfirm pre-command -c $cmd 2>/dev/null
            edit:smart-enter
        } catch {
            edit:redraw &full=$true
        }
    }
}"#;

    pub const XONSH_HOOK: &str = r#"# shellfirm hook for xonsh
import subprocess
import shutil

if shutil.which("shellfirm") is None:
    print("shellfirm binary not found. Install: https://github.com/kaplanelad/shellfirm#installation")
else:
    @events.on_precommand
    def _shellfirm_precommand(cmd, **kwargs):
        if not cmd or not cmd.strip():
            return
        if "shellfirm" in cmd:
            return
        result = subprocess.run(
            ["shellfirm", "pre-command", "-c", cmd],
            capture_output=True,
        )
        if result.returncode != 0:
            raise PermissionError("Command blocked by shellfirm")"#;

    /// Printed by `shellfirm init` 0.3.10 (pasted by hand into rc files).
    pub const BASH_HOOK: &str = r#"# shellfirm hook for bash — intercepts risky commands via DEBUG trap.
# Fires once per command line using PROMPT_COMMAND flag + history number.
# Without functrace, the DEBUG trap only fires for function CALLS (not
# internal commands), so fzf/keybinding internals are never affected.
__shellfirm_ready=""
__shellfirm_histnum="__sf_none__"
__shellfirm_blocked=""

_shellfirm_prompt() {
    __shellfirm_ready="1"
    __shellfirm_blocked=""
}

_shellfirm_hook() {
    # Fast exit for sub-commands after the first check (no subshells)
    if [[ -z "$__shellfirm_ready" ]]; then
        [[ -n "$__shellfirm_blocked" ]] && return 1
        return 0
    fi
    [[ -n "${COMP_LINE:-}" ]] && return 0
    [[ "$BASH_COMMAND" == *"shellfirm"* ]] && return 0
    [[ "$BASH_COMMAND" == "_shellfirm_"* ]] && return 0
    command -v shellfirm &>/dev/null || return 0

    # Check history number to distinguish real commands from keybinding
    # functions (fzf, etc.). Keybinding functions don't create new history
    # entries, so the number stays the same — we skip without consuming
    # the ready flag, so the actual command will be checked later.
    local histnum
    histnum=$(HISTTIMEFORMAT='' builtin history 1 | awk '{print $1}')
    [[ "$histnum" == "$__shellfirm_histnum" ]] && return 0

    # New command line — consume the flag and check
    __shellfirm_ready=""
    __shellfirm_histnum="$histnum"

    local full_cmd
    full_cmd=$(HISTTIMEFORMAT='' builtin history 1 | sed 's/^[ ]*[0-9]*[ ]*//')
    [[ -z "$full_cmd" ]] && return 0

    local __sf_prev_int
    __sf_prev_int=$(trap -p INT)
    trap ':' INT
    shellfirm pre-command -c "$full_cmd"
    local __sf_rc=$?
    if [[ -n "$__sf_prev_int" ]]; then
        eval "$__sf_prev_int"
    else
        trap - INT
    fi
    if [[ $__sf_rc -ne 0 ]]; then
        __shellfirm_blocked="1"
        return 1
    fi
    return 0
}

PROMPT_COMMAND="_shellfirm_prompt${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
shopt -s extdebug
trap '_shellfirm_hook' DEBUG"#;

    /// Printed by `shellfirm init` 0.3.10 (pasted by hand into rc files).
    pub const ZSH_HOOK: &str = r#"# shellfirm hook for zsh — intercepts Enter via the accept-line widget
shellfirm-pre-command() {
    if [[ -z "${BUFFER}" || "${BUFFER}" == *"shellfirm"* ]]; then
        zle .accept-line
        return
    fi
    shellfirm pre-command --direct-tty -c "${BUFFER}"
    if [[ $? -eq 0 ]]; then
        zle .accept-line
    else
        zle reset-prompt
    fi
}
zle -N accept-line shellfirm-pre-command"#;

    /// Printed by `shellfirm init` 0.3.10 (pasted by hand into rc files).
    pub const FISH_HOOK: &str = r#"# shellfirm hook for fish — intercepts Enter via key binding
function _shellfirm_check
    set -l cmd (commandline)
    if test -z "$cmd"; or string match -q '*shellfirm*' -- $cmd
        commandline -f execute
        return
    end
    stty sane
    shellfirm pre-command -c "$cmd"
    if test $status -eq 0
        commandline -f execute
    else
        commandline -f repaint
    end
end
bind \r _shellfirm_check
# Also bind in vi insert mode if active
bind -M insert \r _shellfirm_check 2>/dev/null"#;

    /// Printed by `shellfirm init` 0.3.10 (pasted by hand into rc files).
    pub const OILS_HOOK: &str = r#"# shellfirm hook for Oils (OSH/YSH) — same approach as the bash hook.
__shellfirm_ready=""
__shellfirm_histnum="__sf_none__"
__shellfirm_blocked=""

_shellfirm_prompt() {
    __shellfirm_ready="1"
    __shellfirm_blocked=""
}

_shellfirm_hook() {
    if [[ -z "$__shellfirm_ready" ]]; then
        [[ -n "$__shellfirm_blocked" ]] && return 1
        return 0
    fi
    [[ -n "${COMP_LINE:-}" ]] && return 0
    [[ "$BASH_COMMAND" == *"shellfirm"* ]] && return 0
    [[ "$BASH_COMMAND" == "_shellfirm_"* ]] && return 0
    command -v shellfirm &>/dev/null || return 0

    local histnum
    histnum=$(HISTTIMEFORMAT='' builtin history 1 | awk '{print $1}')
    [[ "$histnum" == "$__shellfirm_histnum" ]] && return 0

    __shellfirm_ready=""
    __shellfirm_histnum="$histnum"

    local full_cmd
    full_cmd=$(HISTTIMEFORMAT='' builtin history 1 | sed 's/^[ ]*[0-9]*[ ]*//')
    [[ -z "$full_cmd" ]] && return 0

    shellfirm pre-command -c "$full_cmd"
    if [[ $? -ne 0 ]]; then
        __shellfirm_blocked="1"
        return 1
    fi
    return 0
}

PROMPT_COMMAND="_shellfirm_prompt${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
shopt -s extdebug
trap '_shellfirm_hook' DEBUG"#;
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn env_with(home: &str) -> RcEnv {
        RcEnv {
            home: Some(PathBuf::from(home)),
            platform_config: Some(PathBuf::from(format!("{home}/.platform-config"))),
            zdotdir: None,
            xdg_config_home: None,
        }
    }

    #[test]
    fn all_hooks_are_non_empty() {
        for shell in Shell::ALL {
            assert!(
                !shell.hook().is_empty(),
                "hook for {shell} should not be empty"
            );
        }
    }

    #[test]
    fn rc_paths_resolve_for_known_shells() {
        for shell in Shell::ALL {
            let _ = shell.rc_file_path();
        }
    }

    #[test]
    fn rc_paths_default_locations() {
        let env = env_with("/h");
        let p = |s: Shell| s.rc_file_path_in(&env).unwrap();
        assert_eq!(p(Shell::Bash), PathBuf::from("/h/.bashrc"));
        assert_eq!(p(Shell::Zsh), PathBuf::from("/h/.zshrc"));
        assert_eq!(p(Shell::Fish), PathBuf::from("/h/.config/fish/config.fish"));
        assert_eq!(p(Shell::Elvish), PathBuf::from("/h/.config/elvish/rc.elv"));
        assert_eq!(p(Shell::Oils), PathBuf::from("/h/.config/oils/oshrc"));
        assert_eq!(p(Shell::Xonsh), PathBuf::from("/h/.xonshrc"));
        assert_eq!(
            p(Shell::Nushell),
            PathBuf::from("/h/.platform-config/nushell/config.nu")
        );
    }

    #[test]
    fn rc_paths_respect_zdotdir_and_xdg() {
        let mut env = env_with("/h");
        env.zdotdir = Some(PathBuf::from("/h/.config/zsh"));
        env.xdg_config_home = Some(PathBuf::from("/xdg"));
        let p = |s: Shell| s.rc_file_path_in(&env).unwrap();
        assert_eq!(p(Shell::Zsh), PathBuf::from("/h/.config/zsh/.zshrc"));
        assert_eq!(p(Shell::Fish), PathBuf::from("/xdg/fish/config.fish"));
        assert_eq!(p(Shell::Nushell), PathBuf::from("/xdg/nushell/config.nu"));
        assert_eq!(p(Shell::Elvish), PathBuf::from("/xdg/elvish/rc.elv"));
        assert_eq!(p(Shell::Bash), PathBuf::from("/h/.bashrc"));
    }

    #[test]
    fn rc_snippet_returns_eval_for_eval_shells() {
        for shell in [Shell::Zsh, Shell::Bash, Shell::Fish, Shell::Oils] {
            assert!(shell.uses_eval());
            assert!(
                shell.rc_snippet().contains("shellfirm init"),
                "{shell} snippet should contain eval one-liner"
            );
        }
    }

    #[test]
    fn rc_snippet_returns_full_hook_for_other_shells() {
        for shell in [
            Shell::Nushell,
            Shell::PowerShell,
            Shell::Elvish,
            Shell::Xonsh,
        ] {
            assert!(!shell.uses_eval());
            let block = shell.rc_block();
            assert!(block.contains(shell.hook()));
            assert!(block.trim_end().ends_with(END_MARKER));
        }
    }

    #[test]
    fn hooks_do_not_skip_commands_containing_shellfirm() {
        for shell in Shell::ALL {
            let hook = shell.hook();
            for pattern in [
                "*shellfirm*",
                "*\"shellfirm\"*",
                "str contains \"shellfirm\"",
                "-match 'shellfirm'",
                "\"shellfirm\" in cmd",
                "str:contains $cmd \"shellfirm\"",
            ] {
                assert!(
                    !hook.contains(pattern),
                    "{shell} hook must not skip commands containing 'shellfirm' ({pattern})"
                );
            }
        }
    }

    #[test]
    fn hooks_pass_command_with_equals_form() {
        // `-c "$cmd"` breaks for commands that start with `-`; the
        // `--command=<value>` form is always parsed as a value.
        for shell in Shell::ALL {
            let hook = shell.hook();
            assert!(
                hook.contains("--command="),
                "{shell} hook must use --command="
            );
            assert!(
                !hook.contains("pre-command -c "),
                "{shell} hook must not use -c"
            );
        }
    }

    #[test]
    fn hooks_fail_open_when_binary_missing() {
        assert!(Shell::Bash.hook().contains(
            "if ! builtin type -P shellfirm >/dev/null 2>&1; then\n        __shellfirm_warn_missing\n        return 0"
        ));
        assert!(Shell::Zsh
            .hook()
            .contains("if ! _shellfirm_have_binary; then"));
        assert!(Shell::Fish.hook().contains("if not command -q shellfirm"));
        assert!(Shell::Nushell.hook().contains("which shellfirm | is-empty"));
        assert!(Shell::Elvish.hook().contains("has-external shellfirm"));
        assert!(Shell::Xonsh.hook().contains("which(\"shellfirm\") is None"));
    }

    #[test]
    fn bash_hook_does_not_depend_on_history_in_readline_mode() {
        let hook = Shell::Bash.hook();
        let readline_part = hook.split("# Fallback without readline").next().unwrap();
        assert!(readline_part.contains("READLINE_LINE"));
        assert!(!readline_part.contains("history 1"));
        // `extdebug` is never turned on in the shell itself (only inside a
        // command substitution, to look up where a function was defined).
        assert!(readline_part
            .lines()
            .filter(|l| l.contains("shopt -s extdebug"))
            .all(|l| l.contains("$(builtin shopt -s extdebug;")));
        assert!(!readline_part.contains("trap '__shellfirm"));
    }

    #[test]
    fn zsh_hook_uses_direct_tty_prompt() {
        assert!(
            Shell::Zsh
                .hook()
                .contains("shellfirm pre-command --direct-tty"),
            "zsh hook must force DirectTtyPrompter to avoid crossterm hangs in zle"
        );
    }

    #[test]
    fn zsh_hook_chains_existing_widgets() {
        let hook = Shell::Zsh.hook();
        assert!(hook.contains("zle -A \"$_sf_w\" \"_shellfirm_orig${_sf_n}_$_sf_w\""));
        assert!(hook.contains("accept-and-hold"));
        assert!(hook.contains("$PREBUFFER$BUFFER"));
        // A plugin that replaces accept-line after init is wrapped again at
        // the prompt, before the first command runs.
        assert!(hook.contains("add-zsh-hook precmd _shellfirm_rewrap"));
    }

    #[test]
    fn hooks_resolve_rerun_and_eval_spellings() {
        let bash = Shell::Bash.hook();
        // Quoted `old=new` and single-quoted `eval` payloads are read word
        // by word after removing quotes.
        assert!(bash.contains("__shellfirm_words"));
        assert!(bash.contains("__shellfirm_fc_eval"));
        // The completeness probe sees history- and alias-expanded text.
        assert!(bash.contains("__shellfirm_incomplete_expanded \"$__sf_x\""));
        assert!(bash.contains("${histchars-!^#}"));
        let zsh = Shell::Zsh.hook();
        assert!(zsh.contains("_shellfirm_spell_candidates"));
        assert!(zsh.contains("$_sf_kw == builtin"));
        assert!(zsh.contains("${histchars[1]-}"));
    }

    #[test]
    fn hooks_load_once_and_skip_definitions() {
        assert!(Shell::Elvish.hook().contains("__SHELLFIRM_ELV_ENTER"));
        assert!(Shell::Elvish.hook().contains("shellfirm-only-fn"));
        assert!(Shell::Xonsh.hook().contains("_shellfirm_xonsh_loaded"));
        assert!(Shell::Xonsh.hook().contains("__xonsh__.aliases"));
        assert!(Shell::Fish.hook().contains("__shellfirm_strip_defs"));
        assert!(Shell::Fish.hook().contains("__shellfirm_pending_change"));
        assert!(Shell::Nushell.hook().contains("scope aliases"));
        assert!(Shell::Elvish.limitation().is_some());
        assert!(Shell::Xonsh.limitation().is_some());
    }

    #[test]
    fn eval_shells_hook_contains_shellfirm_pre_command() {
        for shell in [Shell::Zsh, Shell::Bash, Shell::Fish, Shell::Oils] {
            assert!(
                shell.hook().contains("shellfirm pre-command"),
                "{shell} hook must contain 'shellfirm pre-command' for interception"
            );
        }
    }

    #[test]
    fn validate_shell_arg_accepts_known_and_aliases() {
        for shell in Shell::ALL {
            assert!(validate_shell_arg(Some(shell.name())).is_ok());
            for bin in shell.binaries() {
                assert_eq!(validate_shell_arg(Some(bin)).ok(), Some(shell));
            }
        }
        assert_eq!(Shell::from_name("nu"), Some(Shell::Nushell));
        assert_eq!(Shell::from_name("pwsh"), Some(Shell::PowerShell));
        assert_eq!(Shell::from_name("ZSH"), Some(Shell::Zsh));
    }

    #[test]
    fn validate_shell_arg_rejects_unknown() {
        assert!(validate_shell_arg(Some("csh")).is_err());
        assert!(validate_shell_arg(None).is_err());
    }

    #[test]
    fn from_binary_path_detects_shell() {
        assert_eq!(Shell::from_binary_path("/bin/zsh"), Some(Shell::Zsh));
        assert_eq!(
            Shell::from_binary_path("/usr/local/bin/nu"),
            Some(Shell::Nushell)
        );
        assert_eq!(Shell::from_binary_path("/bin/tcsh"), None);
    }

    #[test]
    fn detect_installed_shells_does_not_panic() {
        let _ = detect_installed_shells();
    }

    #[test]
    fn plan_install_new_and_existing_files() {
        assert_eq!(
            plan_install(Shell::Zsh, None),
            InstallPlan::Append(Shell::Zsh.rc_block())
        );
        assert_eq!(
            plan_install(Shell::Zsh, Some("export A=1\n")),
            InstallPlan::Append(Shell::Zsh.rc_block())
        );
        let installed = format!("export A=1\n{}", Shell::Zsh.rc_block());
        assert_eq!(
            plan_install(Shell::Zsh, Some(&installed)),
            InstallPlan::AlreadyInstalled
        );
    }

    #[test]
    fn plan_install_detects_manual_guarded_line() {
        let content = "command -v shellfirm >/dev/null && eval \"$(shellfirm init zsh)\"\n";
        assert_eq!(
            plan_install(Shell::Zsh, Some(content)),
            InstallPlan::AlreadyInstalled
        );
        // ...but a line for another shell does not count.
        assert!(matches!(
            plan_install(Shell::Bash, Some(content)),
            InstallPlan::Append(_)
        ));
    }

    #[test]
    fn plan_install_detects_hand_pasted_hook() {
        let content = format!(
            "$env.config.show_banner = false\n{}\n",
            Shell::Nushell.hook()
        );
        assert_eq!(
            plan_install(Shell::Nushell, Some(&content)),
            InstallPlan::AlreadyInstalled
        );
    }

    #[test]
    fn plan_install_ignores_comment_mentioning_shellfirm_init() {
        let content = "# TODO: try `shellfirm init zsh` some day\n";
        assert!(matches!(
            plan_install(Shell::Zsh, Some(content)),
            InstallPlan::Append(_)
        ));
    }

    #[test]
    fn plan_install_upgrades_legacy_embedded_hook() {
        let legacy = format!("# mine\n\n{MARKER}\n{}\n", legacy::NUSHELL_HOOK);
        let InstallPlan::Upgrade(new_content) = plan_install(Shell::Nushell, Some(&legacy)) else {
            panic!("expected upgrade");
        };
        assert!(new_content.starts_with("# mine\n"));
        assert!(!new_content.contains("str contains \"shellfirm\""));
        assert!(new_content.contains(Shell::Nushell.hook()));
        assert_eq!(
            plan_install(Shell::Nushell, Some(&new_content)),
            InstallPlan::AlreadyInstalled
        );
    }

    #[test]
    fn plan_install_upgrades_older_marked_embedded_hook() {
        let old = format!("a\n\n{MARKER}\n# old hook text\n{END_MARKER}\nb\n");
        let InstallPlan::Upgrade(new_content) = plan_install(Shell::Xonsh, Some(&old)) else {
            panic!("expected upgrade");
        };
        assert!(new_content.contains("a\n"));
        assert!(new_content.contains("b\n"));
        assert!(!new_content.contains("# old hook text"));
        assert_eq!(new_content.matches(MARKER).count(), 1);
    }

    #[test]
    fn install_to_temp_file_and_detect() {
        let dir = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let rc = dir.root.join(".zshrc");
        fs::write(&rc, format!("# existing config\n{}", Shell::Zsh.rc_block())).unwrap();
        assert!(is_already_installed(&rc, Shell::Zsh));
        fs::write(&rc, "# existing config\n").unwrap();
        assert!(!is_already_installed(&rc, Shell::Zsh));
        assert!(!is_already_installed(&dir.root.join("missing"), Shell::Zsh));
    }

    #[test]
    fn uninstall_removes_block_with_current_marker() {
        let snippet = Shell::Zsh.rc_snippet();
        let content = format!("# my config\nPATH=/usr/bin\n\n{MARKER}\n{snippet}\n");
        let (result, changed) = remove_shellfirm_block(&content, Shell::Zsh);
        assert!(changed);
        assert_eq!(result, "# my config\nPATH=/usr/bin\n");
    }

    #[test]
    fn uninstall_round_trip_is_byte_identical() {
        for shell in Shell::ALL {
            for original in ["", "a=1\n", "a=1", "a=1\n\n# end\n"] {
                let InstallPlan::Append(block) = plan_install(shell, Some(original)) else {
                    panic!("{shell}: expected append");
                };
                let installed = format!("{original}{block}");
                let (result, changed) = remove_shellfirm_block(&installed, shell);
                assert!(changed, "{shell}: block not found");
                assert_eq!(result, original, "{shell}: uninstall must restore the file");
            }
        }
    }

    #[test]
    fn uninstall_removes_embedded_and_legacy_hooks() {
        for shell in [
            Shell::Nushell,
            Shell::PowerShell,
            Shell::Elvish,
            Shell::Xonsh,
        ] {
            let current = format!("# existing stuff\n{}", shell.rc_block());
            let (result, changed) = remove_shellfirm_block(&current, shell);
            assert!(changed, "{shell}");
            assert_eq!(result, "# existing stuff\n", "{shell}");

            let legacy = format!(
                "# existing stuff\n\n{MARKER}\n{}\n",
                shell.legacy_hooks()[0]
            );
            let (result, changed) = remove_shellfirm_block(&legacy, shell);
            assert!(changed, "{shell} legacy");
            assert_eq!(result, "# existing stuff\n", "{shell} legacy");
        }
    }

    #[test]
    fn uninstall_noop_when_not_installed() {
        let content = "# my config\nPATH=/usr/bin\n";
        let (result, changed) = remove_shellfirm_block(content, Shell::Zsh);
        assert!(!changed);
        assert_eq!(result, content);
    }

    #[test]
    fn uninstall_preserves_rest_of_file() {
        let snippet = Shell::Fish.rc_snippet();
        let content =
            format!("# before\nexport FOO=bar\n\n{MARKER}\n{snippet}\n\n# after\nexport BAZ=qux\n");
        let (result, changed) = remove_shellfirm_block(&content, Shell::Fish);
        assert!(changed);
        assert_eq!(
            result,
            "# before\nexport FOO=bar\n\n# after\nexport BAZ=qux\n"
        );
    }

    #[test]
    fn friendly_error_mentions_read_only() {
        let e = std::io::Error::from(std::io::ErrorKind::PermissionDenied);
        let msg = friendly_io_error("write", Path::new("/x/.zshrc"), &e);
        assert!(msg.contains("/x/.zshrc") && msg.contains("read-only"));
    }

    #[test]
    fn activate_hints() {
        assert_eq!(Shell::Bash.activate_hint(), "exec bash");
        assert_eq!(Shell::Zsh.activate_hint(), "exec zsh");
        assert_eq!(Shell::Fish.activate_hint(), "exec fish");
        assert_eq!(Shell::Oils.activate_hint(), "exec osh");
        assert_eq!(Shell::Nushell.activate_hint(), "exec nu");
        for shell in Shell::ALL {
            assert!(!shell.activate_hint().is_empty());
        }
    }

    #[test]
    fn limitations_documented_for_partial_shells() {
        assert!(Shell::Nushell.limitation().is_some());
        assert!(Shell::Bash.limitation().is_none());
        assert!(Shell::Zsh.limitation().is_none());
    }

    #[test]
    fn from_name_round_trip() {
        for shell in Shell::ALL {
            assert_eq!(Shell::from_name(shell.name()), Some(shell));
        }
    }

    #[test]
    fn display_matches_name() {
        for shell in Shell::ALL {
            assert_eq!(shell.to_string(), shell.name());
        }
    }

    #[test]
    fn hook_snapshots() {
        for shell in Shell::ALL {
            insta::assert_snapshot!(format!("hook_{}", shell.name()), shell.hook());
        }
    }

    /// Run `shell -n` style syntax checks when the shell is installed.
    /// The hook is read from stdin: a Windows path (`C:\Users\…`) is
    /// not a file name every `bash` there understands (WSL's `bash.exe`).
    fn syntax_check(bin: &str, args: &[&str], hook: &str) {
        use std::io::Write as _;
        // A shell that cannot run a trivial command (Windows' WSL launcher
        // without a distribution) cannot judge the hook either.
        let usable = std::process::Command::new(bin)
            .args(["-c", "echo ok"])
            .stdin(std::process::Stdio::null())
            .output()
            .is_ok_and(|o| o.status.success() && String::from_utf8_lossy(&o.stdout).trim() == "ok");
        if !usable {
            eprintln!("{bin} not installed or not usable; skipping syntax check");
            return;
        }
        let mut child = std::process::Command::new(bin)
            .args(args)
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .unwrap();
        child
            .stdin
            .take()
            .unwrap()
            .write_all(hook.as_bytes())
            .unwrap();
        let out = child.wait_with_output().unwrap();
        assert!(
            out.status.success(),
            "{bin} rejected the hook: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }

    #[test]
    fn bash_hook_is_valid_bash() {
        syntax_check("bash", &["-n"], Shell::Bash.hook());
    }

    #[test]
    fn zsh_hook_is_valid_zsh() {
        syntax_check("zsh", &["-n"], Shell::Zsh.hook());
    }

    #[test]
    fn fish_hook_is_valid_fish() {
        syntax_check("fish", &["-n"], Shell::Fish.hook());
    }

    /// Exercise the bash helper functions in a real (interactive, no tty)
    /// bash: completeness detection, alias expansion and the fail-open path.
    #[test]
    fn bash_hook_helpers_behave() {
        let script = format!(
            r#"{hook}
__sf_t() {{ if __shellfirm_incomplete "$1"; then echo "INC:$1"; else echo "COMP:$1"; fi; }}
__sf_t 'echo hi'
__sf_t 'for f in x; do'
__sf_t $'for f in x; do\necho $f\ndone'
__sf_t 'echo "abc'
__sf_t 'cat <<EOF'
__sf_t 'git push \'
__sf_t 'echo a # \'
__sf_t 'echo a )'
alias kd='kubectl delete'
__shellfirm_expand 'kd ns prod'; echo "EXP:$__shellfirm_expanded"
__shellfirm_expand $'git push \\\n-f origin'; echo "EXP:$__shellfirm_expanded"
PATH=/nonexistent __shellfirm_check 'rm -rf /' && echo FAILOPEN
"#,
            hook = Shell::Bash.hook()
        );
        let Ok(out) = std::process::Command::new("bash")
            .args(["--norc", "--noprofile", "-i", "-c", &script])
            .stdin(std::process::Stdio::null())
            .output()
        else {
            eprintln!("bash not installed; skipping");
            return;
        };
        let stdout = String::from_utf8_lossy(&out.stdout);
        // Completeness detection is part of the readline mode (bash 4+).
        let readline = bash_version().is_some_and(|v| v.0 >= 4);
        let completeness = [
            "COMP:echo hi",
            "INC:for f in x; do",
            "COMP:for f in x; do\necho $f\ndone",
            "INC:echo \"abc",
            "INC:cat <<EOF",
            "INC:git push \\",
            "COMP:echo a # \\",
            "COMP:echo a )",
        ];
        let always = [
            "EXP:kubectl delete ns prod",
            "EXP:git push -f origin",
            "FAILOPEN",
        ];
        let readline_only: &[&str] = if readline { &completeness } else { &[] };
        for expected in readline_only.iter().chain(always.iter()) {
            assert!(
                stdout.contains(expected),
                "missing {expected:?} in output:\n{stdout}\nstderr:\n{}",
                String::from_utf8_lossy(&out.stderr)
            );
        }
    }

    #[test]
    fn every_hook_reports_the_hook_version() {
        for shell in [
            Shell::Bash,
            Shell::Zsh,
            Shell::Fish,
            Shell::Oils,
            Shell::PowerShell,
        ] {
            assert!(
                shell.hook().contains(&format!(
                    "hook_version{}",
                    match shell {
                        Shell::Fish => format!(" {HOOK_VERSION}"),
                        Shell::PowerShell => format!(" = {HOOK_VERSION}"),
                        _ => format!("={HOOK_VERSION}"),
                    }
                )),
                "{shell} hook must set the hook version {HOOK_VERSION}"
            );
        }
    }

    #[test]
    fn hooks_never_treat_exit_126_as_missing_binary() {
        for shell in Shell::ALL {
            let hook = shell.hook();
            assert!(!hook.contains("126|127"), "{shell}: 126 must fail closed");
            assert!(!hook.contains("0|126|127"), "{shell}: 126 must fail closed");
            assert!(!hook.contains("[126 127]"), "{shell}: 126 must fail closed");
            assert!(
                !hook.contains("(0, 126, 127)"),
                "{shell}: 126 must fail closed"
            );
        }
    }

    #[test]
    fn hooks_check_long_lines_through_stdin() {
        for shell in [
            Shell::Bash,
            Shell::Zsh,
            Shell::Fish,
            Shell::Nushell,
            Shell::Elvish,
            Shell::Xonsh,
        ] {
            let hook = shell.hook();
            assert!(
                hook.contains("check --stdin --exit-code")
                    || hook.contains(r#""check", "--stdin", "--exit-code""#),
                "{shell}: long lines must be checked through stdin"
            );
        }
    }

    #[test]
    fn zsh_hook_does_not_use_widget_variable_for_chaining() {
        let hook = Shell::Zsh.hook();
        assert!(
            !hook.contains("[$WIDGET]"),
            "zsh: $WIDGET is the key widget"
        );
        assert!(hook.contains("functions[_shellfirm_widget_$_sf_w]"));
        assert!(
            hook.contains("_shellfirm_zsh_loaded"),
            "re-sourcing must not re-wrap"
        );
        assert!(hook.contains("bindkey -M isearch"));
    }

    #[test]
    fn nushell_hook_never_claims_builtins_are_stopped() {
        let hook = Shell::Nushell.hook();
        assert!(hook.contains("cannot stop nushell built-ins like rm"));
        assert!(hook.contains("pre-command --test"));
    }

    #[test]
    fn fish_hook_expands_abbreviations_and_wraps_execute() {
        let hook = Shell::Fish.hook();
        assert!(hook.contains("abbr --show"));
        assert!(hook.contains("function commandline"));
        assert!(hook.contains("builtin commandline -f execute"));
    }

    #[test]
    fn plan_install_replaces_hand_pasted_legacy_hook() {
        for shell in [Shell::Bash, Shell::Zsh, Shell::Fish] {
            let content = format!("export A=1\n{}\nexport B=2\n", shell.legacy_hooks()[0]);
            let InstallPlan::Upgrade(new_content) = plan_install(shell, Some(&content)) else {
                panic!("{shell}: expected the old hook to be replaced");
            };
            assert!(legacy_hook_line(&new_content, shell).is_none(), "{shell}");
            assert!(new_content.contains("export A=1\n"), "{shell}");
            assert!(new_content.contains("export B=2\n"), "{shell}");
            assert!(new_content.contains(&shell.rc_snippet()), "{shell}");
            assert_eq!(
                plan_install(shell, Some(&new_content)),
                InstallPlan::AlreadyInstalled
            );
        }
    }

    #[test]
    fn legacy_hook_line_finds_edited_old_hooks() {
        let content = "a=1\n_shellfirm_hook() { :; }\ntrap '_shellfirm_hook' DEBUG\n";
        assert_eq!(legacy_hook_line(content, Shell::Bash), Some(2));
        assert_eq!(
            legacy_hook_line("# trap '_shellfirm_hook' DEBUG\n", Shell::Bash),
            None
        );
        assert_eq!(
            legacy_hook_line("zle -N accept-line shellfirm-pre-command\n", Shell::Zsh),
            Some(1)
        );
        assert_eq!(legacy_hook_line(Shell::Bash.hook(), Shell::Bash), None);
        assert_eq!(legacy_hook_line(Shell::Zsh.hook(), Shell::Zsh), None);
        assert_eq!(legacy_hook_line(Shell::Fish.hook(), Shell::Fish), None);
    }

    #[test]
    fn loader_lines_are_recognised() {
        for (line, shell) in [
            (r#"eval "$(shellfirm init bash)""#, Shell::Bash),
            (r#"eval "$(shellfirm init zsh)";"#, Shell::Zsh),
            (
                r#"eval "$( /usr/local/bin/shellfirm init zsh )""#,
                Shell::Zsh,
            ),
            (
                r#"command -v shellfirm >/dev/null 2>&1 && eval "$(shellfirm init bash)""#,
                Shell::Bash,
            ),
            ("shellfirm init fish | source", Shell::Fish),
        ] {
            assert!(is_loader_line(line, shell), "{line}");
        }
        assert!(!is_loader_line(
            r#"eval "$(shellfirm init zsh)""#,
            Shell::Bash
        ));
        assert!(!is_loader_line(
            r#"# eval "$(shellfirm init bash)""#,
            Shell::Bash
        ));
        assert!(!is_loader_line(
            r#"[ -x x ] && eval "$(shellfirm init bash)" || true"#,
            Shell::Bash
        ));
    }

    #[test]
    fn uninstall_removes_hand_written_eval_line() {
        let content = "a=1\neval \"$(shellfirm init bash)\"\nb=2\n";
        let r = plan_uninstall(content, Shell::Bash);
        assert!(r.changed);
        assert_eq!(r.content, "a=1\nb=2\n");
        assert!(!r.delete_file);
    }

    #[test]
    fn uninstall_keeps_loader_lines_inside_a_construct() {
        let content = "if true; then\neval \"$(shellfirm init bash)\"\nfi\n";
        let r = plan_uninstall(content, Shell::Bash);
        assert!(
            !r.changed,
            "removing it would leave `then` with an empty body"
        );
        let content = "if status is-interactive\n    shellfirm init fish | source\nend\n";
        assert!(!plan_uninstall(content, Shell::Fish).changed);
    }

    #[test]
    fn uninstall_deletes_file_that_init_created() {
        for shell in Shell::ALL {
            let created = format!(
                "{CREATED_NOTE}\n{}",
                shell.rc_block().trim_start_matches('\n')
            );
            let r = plan_uninstall(&created, shell);
            assert!(r.changed && r.delete_file, "{shell}");
            // Something else was added later: keep the file, drop the note.
            let r = plan_uninstall(&format!("{created}alias x=y\n"), shell);
            assert!(r.changed && !r.delete_file, "{shell}");
            assert_eq!(r.content, "alias x=y\n", "{shell}");
        }
        // A file the user created is never deleted, even if it ends up empty.
        let r = plan_uninstall(Shell::Zsh.rc_block().trim_start_matches('\n'), Shell::Zsh);
        assert!(r.changed && !r.delete_file);
        assert_eq!(r.content, "");
    }

    #[test]
    fn created_file_is_already_installed() {
        for shell in Shell::ALL {
            let created = format!(
                "{CREATED_NOTE}\n{}",
                shell.rc_block().trim_start_matches('\n')
            );
            assert_eq!(
                plan_install(shell, Some(&created)),
                InstallPlan::AlreadyInstalled,
                "{shell}"
            );
        }
    }

    #[test]
    fn hook_load_count_detects_double_hooks() {
        let one = format!("a=1\n{}", Shell::Bash.rc_block());
        assert_eq!(hook_load_count(&one, Shell::Bash), 1);
        let two = format!("{one}eval \"$(shellfirm init bash)\"\n");
        assert_eq!(hook_load_count(&two, Shell::Bash), 2);
        // An old hook is reported on its own (legacy_hook_line), not as a
        // second load.
        let legacy = format!("{one}{}\n", legacy::BASH_HOOK);
        assert_eq!(hook_load_count(&legacy, Shell::Bash), 1);
        assert!(legacy_hook_line(&legacy, Shell::Bash).is_some());
    }

    #[test]
    fn init_accepts_every_shell_name_for_completion() {
        let cmd = command();
        let arg = cmd
            .get_arguments()
            .find(|a| a.get_id() == "shell")
            .expect("shell arg");
        let values: Vec<String> = arg
            .get_possible_values()
            .iter()
            .map(|v| v.get_name().to_string())
            .collect();
        for shell in Shell::ALL {
            assert!(values.contains(&shell.name().to_string()), "{shell}");
            for bin in shell.binaries() {
                assert!(values.contains(&(*bin).to_string()), "{bin}");
            }
        }
        let m = command()
            .try_get_matches_from(["init", "ZSH"])
            .expect("case-insensitive");
        assert_eq!(
            m.get_one::<String>("shell").map(String::as_str),
            Some("ZSH")
        );
        assert!(command().try_get_matches_from(["init", "csh"]).is_err());
    }

    /// Exercise the bash expansion helpers: aliases (also `alias g=git`),
    /// functions, `history -s` and `fc -s`.
    #[test]
    fn bash_hook_expansion_behaves() {
        let script = format!(
            r#"{hook}
alias g=git
alias ls='ls -l'
alias s='sudo '
alias k=kubectl
__shellfirm_expand 'g reset --hard'; echo "EXP1:$__shellfirm_expanded"
__shellfirm_expand 'ls -a'; echo "EXP2:$__shellfirm_expanded"
__shellfirm_expand 's k delete ns x'; echo "EXP3:$__shellfirm_expanded"
nuke() {{ rm -rf "$@"; }}
__shellfirm_expand 'nuke build src'; echo "EXP4:${{__shellfirm_expanded//$'\n'/ }}"
del1() {{ kubectl delete ns "$1"; }}
__shellfirm_expand 'del1 prod'; echo "EXP5:${{__shellfirm_expanded//$'\n'/ }}"
__shellfirm_expand 'history -s kubectl delete ns x'; echo "EXP6:[$__shellfirm_expanded]"
__shellfirm_expand 'history -s x; rm -rf y'; echo "EXP7:[$__shellfirm_expanded]"
builtin history -s 'kubectl delete ns st1'
__shellfirm_expand 'fc -s st1=t5 kubectl'; echo "EXP8:$__shellfirm_expanded"
"#,
            hook = Shell::Bash.hook()
        );
        let Ok(out) = std::process::Command::new("bash")
            .args(["--norc", "--noprofile", "-i", "-c", &script])
            .stdin(std::process::Stdio::null())
            .output()
        else {
            eprintln!("bash not installed; skipping");
            return;
        };
        let stdout = String::from_utf8_lossy(&out.stdout);
        for expected in [
            "EXP1:git reset --hard",
            "EXP2:ls -l -a",
            "EXP3:sudo kubectl delete ns x",
            "EXP4:nuke() { rm -rf \"$@\"; } nuke build src",
            "EXP5:del1() { kubectl delete ns \"$1\"; } del1 prod",
            "EXP6:[]",
            "EXP7:[history -s x; rm -rf y]",
            "EXP8:kubectl delete ns t5",
        ] {
            assert!(
                stdout.contains(expected),
                "missing {expected:?} in output:\n{stdout}\nstderr:\n{}",
                String::from_utf8_lossy(&out.stderr)
            );
        }
    }

    /// The zsh expansion helper: aliases (also `alias g=git`), global
    /// aliases, functions and `print -s`.
    #[test]
    fn zsh_hook_expansion_behaves() {
        let script = format!(
            r#"{hook}
alias g=git
alias ls='ls -G'
alias -g F=--force
_shellfirm_expand 'g reset --hard'; print -r -- "EXP1:$_shellfirm_expanded"
_shellfirm_expand 'ls -a'; print -r -- "EXP2:$_shellfirm_expanded"
_shellfirm_expand 'git push F origin main'; print -r -- "EXP3:$_shellfirm_expanded"
nuke() {{ rm -rf "$@" }}
_shellfirm_expand 'nuke build src'; print -r -- "EXP4:${{_shellfirm_expanded//$'\n'/ }}"
_shellfirm_expand 'print -s kubectl delete ns x'; print -r -- "EXP5:[$_shellfirm_expanded]"
_shellfirm_expand '  g push'; print -r -- "EXP6:$_shellfirm_expanded"
"#,
            hook = Shell::Zsh.hook()
        );
        let Ok(out) = std::process::Command::new("zsh")
            .args(["-f", "-i", "-c", &script])
            .stdin(std::process::Stdio::null())
            .output()
        else {
            eprintln!("zsh not installed; skipping");
            return;
        };
        let stdout = String::from_utf8_lossy(&out.stdout);
        for expected in [
            "EXP1:git reset --hard",
            "EXP2:ls -G -a",
            "EXP3:git push --force origin main",
            "EXP4:nuke() { rm -rf \"$@\"; } nuke build src",
            "EXP5:[]",
            "EXP6:git push",
        ] {
            assert!(
                stdout.contains(expected),
                "missing {expected:?} in output:\n{stdout}\nstderr:\n{}",
                String::from_utf8_lossy(&out.stderr)
            );
        }
    }

    /// `bash`'s (major, minor) version; None when bash is not installed.
    /// Before bash 4 (macOS /bin/bash is 3.2) the hook has no readline
    /// mode (`bind -x`): it uses the DEBUG-trap fallback, so the readline
    /// helpers (`__shellfirm_accept`, `__shellfirm_incomplete`) are not
    /// defined.
    fn bash_version() -> Option<(u32, u32)> {
        let out = std::process::Command::new("bash")
            .args(["-c", "echo ${BASH_VERSINFO[0]} ${BASH_VERSINFO[1]}"])
            .output()
            .ok()?;
        let text = String::from_utf8_lossy(&out.stdout);
        let mut it = text.split_whitespace().map(|n| n.parse().unwrap_or(0));
        Some((it.next()?, it.next()?))
    }

    /// Runs `script` in an interactive bash with the hook loaded; None when
    /// bash is not installed.
    fn run_bash(script: &str) -> Option<(String, String)> {
        let script = format!("{}\n{script}", Shell::Bash.hook());
        let out = std::process::Command::new("bash")
            .args(["--norc", "--noprofile", "-i", "-c", &script])
            .stdin(std::process::Stdio::null())
            .output()
            .ok()?;
        Some((
            String::from_utf8_lossy(&out.stdout).into_owned(),
            String::from_utf8_lossy(&out.stderr).into_owned(),
        ))
    }

    fn assert_has(out: &(String, String), expected: &[&str]) {
        for e in expected {
            assert!(
                out.0.contains(e),
                "missing {e:?} in output:\n{}\nstderr:\n{}",
                out.0,
                out.1
            );
        }
    }

    /// Aliases and functions are expanded in every command of a line, not
    /// only at its start.
    #[test]
    fn bash_hook_expands_every_command_of_a_line() {
        let Some(out) = run_bash(
            r#"
alias kd='kubectl delete'
nuke() { rm -rf "$@"; }
for c in 'true && kd ns b' 'time kd ns c' '(kd ns d)' '{ kd ns e; }' 'x=1 kd ns f' \
         'echo | kd ns g' '! kd ns h' 'if true; then kd ns j; fi' 'echo hi; nuke build'; do
  __shellfirm_expand "$c"; echo "EXP:${__shellfirm_expanded//$'\n'/ }"
done
big="kd ns bigal # $(printf '%0120000d' 0)"
__shellfirm_expand "$big" 1; echo "BIG:${__shellfirm_expanded:0:23}"
"#,
        ) else {
            return;
        };
        assert_has(
            &out,
            &[
                "EXP:true && kubectl delete ns b",
                "EXP:time kubectl delete ns c",
                "EXP:(kubectl delete ns d)",
                "EXP:{ kubectl delete ns e; }",
                "EXP:x=1 kubectl delete ns f",
                "EXP:echo | kubectl delete ns g",
                "EXP:! kubectl delete ns h",
                "EXP:if true; then kubectl delete ns j; fi",
                "nuke() { rm -rf \"$@\"; } echo hi; nuke build",
                "BIG:kubectl delete ns bigal",
            ],
        );
    }

    /// `fc -s` re-runs are resolved wherever they appear and however `fc`
    /// is spelled; `command fc` / `builtin fc` with an editor are refused.
    #[test]
    fn bash_hook_resolves_fc_reruns_anywhere() {
        let Some(out) = run_bash(
            r#"
builtin history -s 'kubectl delete ns st1'
for c in 'command fc -s st1=a kubectl' '\fc -s st1=b kubectl' 'fc -s st1=c kubectl && true' \
         'eval fc -s st1=d kubectl' 'x=1 fc -s st1=e kubectl' 'builtin fc -e - st1=f kubectl'; do
  __shellfirm_block_msg=""; __shellfirm_expand "$c"; echo "EXP:${__shellfirm_expanded//$'\n'/ | }"
done
__shellfirm_block_msg=""; __shellfirm_expand 'command fc'; echo "BLOCK:$__shellfirm_block_msg"
__shellfirm_block_msg=""; __shellfirm_expand 'fc -l'; echo "LIST:[$__shellfirm_block_msg]"
"#,
        ) else {
            return;
        };
        assert_has(
            &out,
            &[
                "EXP:kubectl delete ns a",
                "EXP:kubectl delete ns b",
                "kubectl delete ns c",
                "kubectl delete ns d",
                "kubectl delete ns e",
                "EXP:kubectl delete ns f",
                "BLOCK:shellfirm: `command fc`",
                "LIST:[]",
            ],
        );
    }

    /// A line that bash rejected (history expansion failed) is not kept:
    /// the next line is checked on its own, with `!` references expanded.
    #[test]
    fn bash_hook_drops_lines_bash_rejected() {
        // `__shellfirm_accept` is the readline mode's Enter (bash 4+).
        if bash_version().is_some_and(|v| v.0 < 4) {
            eprintln!("bash older than 4: no readline mode; skipping");
            return;
        }
        let Some(out) = run_bash(
            r#"
set -H
builtin history -s 'kubectl delete ns hx7'
__shellfirm_check() { echo "CHECK:${1//$'\n'/ | }"; return 0; }
bind() { :; }
READLINE_LINE='echo "hello!world"'; __shellfirm_accept
READLINE_LINE='!kubectl'; __shellfirm_accept
READLINE_LINE='for i in 1; do'; __shellfirm_accept
READLINE_LINE='kubectl delete ns x; done'; __shellfirm_accept
"#,
        ) else {
            return;
        };
        assert_has(
            &out,
            &[
                "CHECK:kubectl delete ns hx7",
                "CHECK:for i in 1; do | kubectl delete ns x; done",
            ],
        );
        assert!(!out.0.contains("hello!world | !kubectl"), "{}", out.0);
        assert!(!out.1.contains("history expansion failed"), "{}", out.1);
    }

    /// zsh: aliases in every command, suffix aliases, `r` / `fc -e -`
    /// re-runs anywhere on the line and `builtin fc` with an editor.
    #[test]
    fn zsh_hook_expands_every_command_of_a_line() {
        let script = format!(
            r#"{hook}
alias kd='kubectl delete'
alias -s d='rm -rf'
print -s 'kubectl delete ns z1'
# In `zsh -c` the newest history entry stands for the line being run, and
# `r` / `fc` skip it (as they skip the typed line in an interactive shell).
print -s ': the line being run'
for c in 'true && kd ns b' 'time kd ns c' '(kd ns d)' '{{ kd ns e }}' 'x=1 kd ns f' 'build.d' \
         'r z1=z2 kubectl; true' 'true; r z1=z3 kubectl' 'builtin fc -e - z1=z4 kubectl'; do
  _shellfirm_block_msg=""; _shellfirm_expand "$c"; print -r -- "EXP:${{_shellfirm_expanded//$'\n'/ | }}"
done
_shellfirm_block_msg=""; _shellfirm_expand 'builtin fc'; print -r -- "BLOCK:$_shellfirm_block_msg"
"#,
            hook = Shell::Zsh.hook()
        );
        let Ok(out) = std::process::Command::new("zsh")
            .args(["-f", "-i", "-c", &script])
            .stdin(std::process::Stdio::null())
            .output()
        else {
            eprintln!("zsh not installed; skipping");
            return;
        };
        let out = (
            String::from_utf8_lossy(&out.stdout).into_owned(),
            String::from_utf8_lossy(&out.stderr).into_owned(),
        );
        assert_has(
            &out,
            &[
                "EXP:true && kubectl delete ns b",
                "EXP:time kubectl delete ns c",
                "EXP:( kubectl delete ns d )",
                "EXP:{ kubectl delete ns e }",
                "EXP:x=1 kubectl delete ns f",
                "EXP:rm -rf build.d",
                "kubectl delete ns z2",
                "kubectl delete ns z3",
                "EXP:kubectl delete ns z4",
                "BLOCK:shellfirm: `builtin fc`",
            ],
        );
    }

    #[test]
    fn every_hook_exports_its_pid_for_doctor() {
        for shell in Shell::ALL {
            assert!(
                shell.hook().contains("SHELLFIRM_HOOK_PID"),
                "{shell}: doctor needs SHELLFIRM_HOOK_PID"
            );
        }
    }

    #[test]
    fn elvish_hook_never_throws_from_the_parse_probe() {
        let hook = Shell::Elvish.hook();
        assert!(hook.contains("-compileonly -json"));
        assert!(hook.contains("try { e:elvish"));
        assert!(!hook.contains("var parsed = (e:elvish"));
    }

    #[test]
    fn bash_hook_runs_one_process_per_enter() {
        let hook = Shell::Bash.hook();
        // The dry run is only used when shellfirm prints warnings; the
        // banner already shows the command.
        assert!(hook.contains("if [[ $__shellfirm_notes == 1 ]]; then"));
        // pre-command says when it prompted (exit 3): no timing guess.
        assert!(hook.contains("SHELLFIRM_SIGNAL_PROMPTED=1 builtin command shellfirm pre-command"));
        assert!(!hook.contains("EPOCHREALTIME"));
        assert!(!hook.contains("shellfirm: checking:"));
    }

    #[test]
    fn uninstall_dry_run_notes_match_a_real_uninstall() {
        let content = format!("a=1\n{}", Shell::Bash.rc_block());
        let r = plan_uninstall(&content, Shell::Bash);
        assert!(r.changed);
        // What a dry run reports as left over is computed on this content.
        assert!(!has_manual_hook(&r.content, Shell::Bash));
    }

    #[test]
    fn uninstall_removes_directories_init_created() {
        let dir = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let created = dir.root.join("cfg/nushell");
        let rc = created.join("config.nu");
        let content = format!(
            "{CREATED_NOTE}\n{CREATED_DIR_NOTE}{}\n{CREATED_DIR_NOTE}{}\n{}",
            created.display(),
            dir.root.join("cfg").display(),
            Shell::Nushell.rc_block().trim_start_matches('\n')
        );
        let r = plan_uninstall(&content, Shell::Nushell);
        assert!(r.delete_file);
        assert_eq!(r.created_dirs, vec![created.clone(), dir.root.join("cfg")]);
        fs::create_dir_all(&created).unwrap();
        fs::write(dir.root.join("keep"), "").unwrap();
        remove_created_dirs(&rc, &r.created_dirs);
        assert!(!dir.root.join("cfg").exists());
        // Never anything that is not an ancestor of the rc file, nor a
        // directory that is not empty.
        fs::create_dir_all(dir.root.join("other")).unwrap();
        fs::create_dir_all(dir.root.join("full/x")).unwrap();
        remove_created_dirs(
            &dir.root.join("full/x/rc"),
            &[dir.root.join("other"), dir.root.join("full")],
        );
        assert!(dir.root.join("other").exists() && dir.root.join("full").exists());
    }

    #[test]
    fn rc_report_finds_the_hook_in_fish_conf_d() {
        let dir = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let env = RcEnv {
            home: Some(dir.root.clone()),
            platform_config: None,
            zdotdir: None,
            xdg_config_home: Some(dir.root.join("cfg")),
        };
        fs::create_dir_all(dir.root.join("cfg/fish/conf.d")).unwrap();
        fs::write(dir.root.join("cfg/fish/config.fish"), "set -g x 1\n").unwrap();
        fs::write(
            dir.root.join("cfg/fish/conf.d/shellfirm.fish"),
            "shellfirm init fish | source\n",
        )
        .unwrap();
        let r = rc_report_in(Shell::Fish, &env);
        assert!(!r.installed);
        assert_eq!(
            r.loaded_from,
            Some(dir.root.join("cfg/fish/conf.d/shellfirm.fish"))
        );
    }

    /// Lines of `out` (stdout) that start with `prefix`.
    fn lines_with<'a>(out: &'a (String, String), prefix: &str) -> Vec<&'a str> {
        out.0.lines().filter(|l| l.starts_with(prefix)).collect()
    }

    /// bash: code a line installs to run by itself (`bind -x`, a
    /// command_not_found_handle) is checked when it is defined; a command
    /// named by a session variable is resolved; a chain of functions deeper
    /// than the expansion limit is still read (as written), a recursive one
    /// ends, and only a call graph past the budget is flagged.
    #[test]
    fn bash_hook_checks_autorun_code_session_variables_and_deep_chains() {
        let Some(out) = run_bash(
            r#"
export K=kubectl
nk() { kubectl delete ns fv; }
f=nk
a1() { kubectl delete ns d1; }; a2() { a1; }; a3() { a2; }; a4() { a3; }; a6() { a4; }
e1() { echo hi; }; e2() { e1; }; e3() { e2; }; e4() { e3; }; e5() { e4; }
c0() { echo leaf; }
for i in $(seq 1 80); do eval "c$i() { c$((i-1)); }"; done
pc() { kubectl delete ns pcf; }
rec() { rec; }
for c in "bind -x '\"\\C-t\": kubectl delete ns bx'" 'command_not_found_handle() { kubectl delete ns cn; }' \
         '$K delete ns s1' '${K} delete ns s2' 'echo a; ${K} delete ns s3' '$f'; do
  __shellfirm_expand "$c"; echo "EXP:${__shellfirm_expanded//$'\n'/ | }"
done
for c in "PROMPT_COMMAND='kubectl delete ns p1'" 'PROMPT_COMMAND=pc' "PROMPT_COMMAND+=('kubectl delete ns p2')" \
         "PS1='\$(kubectl delete ns p3) \$ '" 'trap pc USR1'; do
  __shellfirm_expand "$c"; echo "AUTO:${__shellfirm_expanded//$'\n'/ | }"
done
for c in a3 a4 a6 e5 c80 rec; do
  __shellfirm_expand "$c"; echo "DEPTH-$c:${__shellfirm_expanded//$'\n'/ | }"
done
"#,
        ) else {
            return;
        };
        assert_has(
            &out,
            &[
                "| kubectl delete ns bx",
                "| { kubectl delete ns cn; }",
                "kubectl delete ns s1",
                "kubectl delete ns s2",
                "kubectl delete ns s3",
                "kubectl delete ns fv",
            ],
        );
        let auto = lines_with(&out, "AUTO:").join("\n");
        for want in [
            "kubectl delete ns p1",
            "kubectl delete ns pcf",
            "kubectl delete ns p2",
            "kubectl delete ns p3",
        ] {
            assert!(auto.contains(want), "{want}: {auto}");
        }
        assert_eq!(auto.matches("kubectl delete ns pcf").count(), 2, "{auto}");
        let depth = |name: &str| lines_with(&out, &format!("DEPTH-{name}:")).join("\n");
        for name in ["a3", "a4", "a6"] {
            assert!(
                depth(name).contains("kubectl delete ns d1"),
                "{name}: {}",
                depth(name)
            );
        }
        for name in ["a3", "a4", "a6", "e5", "rec"] {
            assert!(
                !depth(name).contains("shellfirm-"),
                "{name}: {}",
                depth(name)
            );
        }
        assert!(
            depth("c80").contains("shellfirm-function-depth-limit"),
            "{}",
            depth("c80")
        );
    }

    /// bash: functions with non-ASCII names are followed; what an
    /// assignment adds to PROMPT_COMMAND is checked however it is spelled
    /// (and a default that is not used is not); a function a DEBUG trap runs
    /// is a hook root; a big function sourced by a relative path counts as
    /// loaded from a file, one typed at the prompt (`main`) does not.
    #[test]
    fn bash_hook_reads_unicode_names_prompt_command_idioms_and_trap_roots() {
        let Some(out) = run_bash(
            r#"
nettoyé() { kubectl delete ns na1; }
日本() { kubectl delete ns na2; }
for c in 'nettoyé' '日本' 'x=1; nettoyé'; do
  __shellfirm_expand "$c"; echo "NA:${__shellfirm_expanded//$'\n'/ | }"
done
for c in 'PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }kubectl delete ns q1"' \
         "PROMPT_COMMAND=\${PROMPT_COMMAND:+\$PROMPT_COMMAND;}'kubectl delete ns q2'" \
         "PROMPT_COMMAND[1]='kubectl delete ns q3'" \
         'PROMPT_COMMAND="$(true);kubectl delete ns q4"'; do
  __shellfirm_expand "$c"; echo "PC:${__shellfirm_expanded//$'\n'/ | }"
done
__shellfirm_expand 'PROMPT_COMMAND="${PROMPT_COMMAND:-kubectl delete ns q9}"'; echo "PCDEF:${__shellfirm_expanded//$'\n'/ | }"
g() { true; }
__shellfirm_traps="trap -- 'g' DEBUG"
__shellfirm_expand 'g() { kubectl delete ns t1; }'; echo "TRAP:${__shellfirm_expanded//$'\n'/ | }"
__shellfirm_relfns=" bigf|./nvm.sh"
__shellfirm_from_file bigf ./nvm.sh && echo REL:yes
__shellfirm_from_file bigf main || echo MAIN:no
__shellfirm_from_file other ./nvm.sh || echo OTHER:no
"#,
        ) else {
            return;
        };
        assert_has(
            &out,
            &[
                "kubectl delete ns na1",
                "kubectl delete ns na2",
                "REL:yes",
                "MAIN:no",
                "OTHER:no",
            ],
        );
        let na = lines_with(&out, "NA:");
        assert_eq!(na.len(), 3, "{na:?}");
        assert!(
            na.iter().all(|l| l.contains("kubectl delete ns na")),
            "{na:?}"
        );
        let pc = lines_with(&out, "PC:").join("\n");
        // The line itself, and what it adds after it.
        for want in ["q1", "q2", "q3", "q4"] {
            assert_eq!(
                pc.matches(&format!("kubectl delete ns {want}")).count(),
                2,
                "{want}: {pc}"
            );
        }
        let pcdef = lines_with(&out, "PCDEF:").join("\n");
        // Before bash 4.4 the hook cannot evaluate what the assignment adds
        // (`__shellfirm_pc_added`) and reads it as text: an unused default
        // is checked too (fail safe).
        if bash_version().is_some_and(|v| v >= (4, 4)) {
            assert!(!pcdef.contains("| kubectl delete ns q9"), "{pcdef}");
        }
        let trap = lines_with(&out, "TRAP:").join("\n");
        assert!(trap.contains("| g"), "{trap}");
    }

    /// bash: the functions a line calls go with it as one-line definitions
    /// (the check binds their arguments, `shift` included); a body that
    /// rewrites its arguments is also sent as written; every cap ends in
    /// the depth-limit marker; redefining a function a hook runs is checked.
    #[test]
    fn bash_hook_sends_function_definitions() {
        let Some(out) = run_bash(
            r#"
sh1() { shift; rm -rf "$1"; }
sh3() { set -- "$2"; rm -rf "$1"; }
h9() { kubectl delete ns wd1; }
g() { true; }; inner() { true; }; outer() { inner; }
PROMPT_COMMAND="outer;g"
eval "big() { $(for i in $(seq 1 900); do printf 'echo %s;' "line-number-$i"; done) }"
for c in 'sh1 nothere src' 'sh3 nothere src' 'A=1 B=2 C=3 D=4 E=5 F=6 G=7 H=8 I=9 h9' \
         'g() { kubectl delete ns rd1; }' 'inner() { kubectl delete ns rd2; }' 'plain() { kubectl delete ns no; }' 'big'; do
  __shellfirm_expand "$c"; echo "DEF-${c%% *}:${__shellfirm_expanded//$'\n'/ | }"
done
"#,
        ) else {
            return;
        };
        let get = |name: &str| lines_with(&out, &format!("DEF-{name}:")).join("\n");
        assert!(
            get("sh1").contains(r#"sh1() { shift; rm -rf "$1"; } | sh1 nothere src"#),
            "{}",
            get("sh1")
        );
        assert!(get("sh3").contains("| { "), "raw body: {}", get("sh3"));
        assert!(get("A=1").contains("| h9 "), "{}", get("A=1"));
        assert!(get("g()").ends_with("| g"), "{}", get("g()"));
        assert!(get("inner()").ends_with("| inner"), "{}", get("inner()"));
        assert!(!get("plain()").contains("| plain"), "{}", get("plain()"));
        assert!(
            get("big").contains("shellfirm-function-depth-limit big"),
            "{}",
            get("big")
        );
    }

    /// bash: the binding repair runs after the other PROMPT_COMMAND entries
    /// (a plugin can rebind Enter in one of them).
    #[test]
    fn bash_hook_repairs_enter_last_in_prompt_command() {
        let hook = Shell::Bash.hook();
        assert!(hook.contains("__shellfirm_postcmd() {"));
        assert!(hook.contains("PROMPT_COMMAND=$__sf_e$'\\n'__shellfirm_postcmd"));
        assert!(hook.contains("precmd_functions=(\"${__sf_keep[@]}\" __shellfirm_postcmd)"));
        // Every bypass is reported, not only the first.
        assert!(hook.contains("the last command did not pass through shellfirm"));
    }

    /// zsh: hook functions, widgets and `*_functions` entries defined or
    /// registered on a line are checked; `$K` / `${(P)n}` from the session;
    /// deep chains fail closed; a zle-line-finish check backs up Enter.
    #[test]
    fn zsh_hook_checks_autorun_code_session_variables_and_deep_chains() {
        let hook = Shell::Zsh.hook();
        assert!(hook.contains("_sf_h+=(\"$(( _sf_max + 1 )):_shellfirm_line_finish\")"));
        assert!(hook.contains("__shellfirm_doctor_probe() {"));
        assert!(hook.contains("_shellfirm_doctor_probe() { __shellfirm_doctor_probe \"$@\" }"));
        let script = format!(
            r#"{hook}
K=kubectl; n=K
a1() {{ kubectl delete ns d1 }}; a2() {{ a1 }}; a3() {{ a2 }}; a4() {{ a3 }}; a6() {{ a4 }}
e1() {{ echo hi }}; e2() {{ e1 }}; e3() {{ e2 }}; e4() {{ e3 }}; e5() {{ e4 }}
c0() {{ echo leaf }}
for i in {{1..80}}; do functions[c$i]="c$((i-1))"; done
pc() {{ kubectl delete ns pcf }}
zd=${{TMPDIR:-/tmp}}/sfz$$; mkdir -p $zd; print -r -- 'kubectl delete ns al' > $zd/nukez; fpath=($zd $fpath); autoload -Uz nukez
for c in 'autoload -Uz add-zsh-hook; add-zsh-hook precmd pc' "functions[precmd]='kubectl delete ns fp'" \
         "PROMPT='\$(kubectl delete ns pr)%# '" 'nukez'; do
  _shellfirm_block_msg=""; _shellfirm_expand "$c"; print -r -- "AUTO:${{_shellfirm_expanded//$'\n'/ | }}"
done
for c in a3 a4 a6 e5 c80; do _shellfirm_expand $c; print -r -- "DEPTH-$c:${{_shellfirm_expanded//$'\n'/ | }}"; done
rm -rf $zd
for c in 'precmd() {{ kubectl delete ns pc; }}' 'w() {{ kubectl delete ns w1; }}; zle -N w' \
         'g() {{ kubectl delete ns pe; }}; preexec_functions+=(g)' 'TRAPDEBUG() {{ kubectl delete ns td; }}' \
         '$K delete ns s1' '${{(P)n}} delete ns s2' 'x="kubectl delete ns ev"; eval $x' 'plain() {{ kubectl delete ns no; }}'; do
  _shellfirm_block_msg=""; _shellfirm_expand "$c"; print -r -- "EXP:${{_shellfirm_expanded//$'\n'/ | }}"
done
"#
        );
        let Ok(out) = std::process::Command::new("zsh")
            .args(["-f", "-i", "-c", &script])
            .stdin(std::process::Stdio::null())
            .output()
        else {
            eprintln!("zsh not installed; skipping");
            return;
        };
        let out = (
            String::from_utf8_lossy(&out.stdout).into_owned(),
            String::from_utf8_lossy(&out.stderr).into_owned(),
        );
        assert_has(
            &out,
            &[
                "| { kubectl delete ns pc ; }",
                "| { kubectl delete ns w1 ; }",
                "| { kubectl delete ns pe ; }",
                "| { kubectl delete ns td ; }",
                "kubectl delete ns s1",
                "kubectl delete ns s2",
                "kubectl delete ns ev",
            ],
        );
        let exp = lines_with(&out, "EXP:plain()").join("\n");
        assert!(
            !exp.contains("| {"),
            "a plain function definition runs nothing: {exp}"
        );
        let auto = lines_with(&out, "AUTO:").join("\n");
        for want in [
            "kubectl delete ns pcf",
            "kubectl delete ns fp",
            "kubectl delete ns pr",
            "kubectl delete ns al",
        ] {
            assert!(auto.contains(want), "{want}: {auto}");
        }
        let depth = |name: &str| lines_with(&out, &format!("DEPTH-{name}:")).join("\n");
        for name in ["a3", "a4", "a6"] {
            assert!(
                depth(name).contains("kubectl delete ns d1"),
                "{name}: {}",
                depth(name)
            );
        }
        for name in ["a3", "a4", "a6", "e5"] {
            assert!(
                !depth(name).contains("shellfirm-"),
                "{name}: {}",
                depth(name)
            );
        }
        assert!(
            depth("c80").contains("shellfirm-function-depth-limit"),
            "{}",
            depth("c80")
        );
    }

    /// fish: fish's own functions (`cd`, `prevd`, `ls`, ...) are not
    /// expanded, redirections stay intact, arguments cannot add operators,
    /// event handlers and `bind` commands are checked, deep chains fail
    /// closed.
    #[test]
    fn fish_hook_function_expansion() {
        let hook = Shell::Fish.hook();
        assert!(hook.contains("\"$__fish_data_dir/functions/*\""));
        let script = format!(
            r#"{hook}
function nuke; kubectl delete ns $argv; end
function redir; echo hi >&2; echo $argv[1] 2>&1; end
function a1; kubectl delete ns $argv; end; function a2; a1 $argv; end; function a3; a2 $argv; end; function a4; a3 $argv; end
for c in 'cd /tmp' 'prevd' 'ls' 'nuke q1' 'redir "a;b"' 'echo (nuke q2)' 'command nuke q3'
    set -g __shellfirm_fn_seen
    set -l r (__shellfirm_function_bodies $c | string join ' | ')
    echo "FB:$c=>$r"
end
set -g __shellfirm_fn_seen; set r (__shellfirm_function_bodies 'a1 x' probe); echo "PROBE:$r"
set r (__shellfirm_bind_commands "bind \ct 'kubectl delete ns bd'; bind \ce end-of-line" | string join ' | '); echo "BIND:$r"
set r (__shellfirm_strip_defs 'function hk --on-event fish_prompt; kubectl delete ns ev; end' | string join ' | '); echo "DEFS:$r"
set r (__shellfirm_strip_defs 'function hk; kubectl delete ns ev; end' | string join ' | '); echo "PLAIN:$r"
set r (__shellfirm_strip_defs 'function fish_prompt; kubectl delete ns fp; end' | string join ' | '); echo "PROMPTFN:$r"
"#
        );
        let Ok(out) = std::process::Command::new("fish")
            .args(["--no-config", "-i", "-c", &script])
            .stdin(std::process::Stdio::null())
            .output()
        else {
            eprintln!("fish not installed; skipping");
            return;
        };
        let out = (
            String::from_utf8_lossy(&out.stdout).into_owned(),
            String::from_utf8_lossy(&out.stderr).into_owned(),
        );
        assert_has(
            &out,
            &[
                "FB:cd /tmp=>\n",
                "FB:prevd=>\n",
                "FB:ls=>\n",
                "kubectl delete ns q1",
                "echo hi >&2; echo 'a;b' 2>&1;",
                "kubectl delete ns q2",
                "FB:command nuke q3=>\n",
                "PROBE:a1",
                "BIND:kubectl delete ns bd\n",
                "DEFS:function hk --on-event fish_prompt",
                "PLAIN:\n",
                "PROMPTFN:function fish_prompt",
            ],
        );
    }

    #[test]
    fn nushell_install_notes_and_uninstall_removes_the_created_env_nu() {
        let dir = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let rc = dir.root.join("config.nu");
        assert_eq!(
            companion_file(Shell::Nushell, &rc),
            Some(dir.root.join("env.nu"))
        );
        assert_eq!(companion_file(Shell::Bash, &rc), None);
        assert_eq!(
            create_companion(Shell::Nushell, &rc),
            Some(dir.root.join("env.nu"))
        );
        // Already there: not created (and so not noted) again.
        assert_eq!(create_companion(Shell::Nushell, &rc), None);
        let content = format!(
            "{CREATED_NOTE}\n{CREATED_FILE_NOTE}{}\n{}",
            dir.root.join("env.nu").display(),
            Shell::Nushell.rc_block().trim_start_matches('\n')
        );
        let r = plan_uninstall(&content, Shell::Nushell);
        assert!(r.delete_file);
        assert_eq!(r.created_files, vec![dir.root.join("env.nu")]);
        // Not empty any more: kept.
        fs::write(dir.root.join("env.nu"), "$env.X = 1").unwrap();
        remove_created_files(&rc, &r.created_files);
        assert!(dir.root.join("env.nu").exists());
        fs::write(dir.root.join("env.nu"), "").unwrap();
        remove_created_files(&rc, &r.created_files);
        assert!(!dir.root.join("env.nu").exists());
        // Never a file that is not next to the rc file.
        fs::create_dir_all(dir.root.join("other")).unwrap();
        fs::write(dir.root.join("other/env.nu"), "").unwrap();
        remove_created_files(&rc, &[dir.root.join("other/env.nu")]);
        assert!(dir.root.join("other/env.nu").exists());
    }

    /// nushell: a program named by `$env.NAME` (set earlier or on the
    /// line) is resolved; xonsh: `@($K)` / `@(cmd)` from the session.
    #[test]
    fn nushell_and_xonsh_hooks_resolve_session_variables() {
        let nu = Shell::Nushell.hook();
        assert!(nu.contains(r"\^?\$env\.(?P<n>[A-Za-z_][A-Za-z0-9_]*)"));
        assert!(nu.contains("$env | get -i $l.n"));
        let xonsh = Shell::Xonsh.hook();
        assert!(xonsh.contains("__xonsh__.env.get(name)"));
        assert!(xonsh.contains(r#"(getattr(__xonsh__, "ctx", None) or {}).get(name)"#));
    }

    /// elvish: a binding that rewrites the line after the check (a plugin
    /// loaded before shellfirm) has the new line checked before it runs.
    #[test]
    fn elvish_hook_rechecks_a_rewritten_line() {
        let hook = Shell::Elvish.hook();
        assert!(hook.contains("fn shellfirm-recheck {|cmd|"));
        assert!(hook.contains("shellfirm-recheck $cmd"));
        assert!(hook.contains("set paths = $shellfirm-saved-paths"));
    }
}