zsh-workers
 help / color / mirror / code / Atom feed
bc07e8c39a6c939fe0748a69097839ecdf2b452f blob 174010 bytes (raw)

   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
 
/*
 * exec.c - command execution
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 1992-1997 Paul Falstad
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Paul Falstad or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Paul Falstad and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Paul Falstad and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Paul Falstad and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

#include "zsh.mdh"
#include "exec.pro"

/* Flags for last argument of addvars */

enum {
    /* Export the variable for "VAR=val cmd ..." */
    ADDVAR_EXPORT =   1 << 0,
    /* Apply restrictions for variable */
    ADDVAR_RESTRICT = 1 << 1,
    /* Variable list is being restored later */
    ADDVAR_RESTORE =  1 << 2
};

/* Structure in which to save values around shell function call */

struct funcsave {
    char opts[OPT_SIZE];
    char *argv0;
    int zoptind, lastval, optcind, numpipestats;
    int *pipestats;
    char *scriptname;
    int breaks, contflag, loops, emulation, noerrexit, oflags, restore_sticky;
    Emulation_options sticky;
    struct funcstack fstack;
};
typedef struct funcsave *Funcsave;

/*
 * Used to suppress ERREXIT and trapping of SIGZERR, SIGEXIT in the
 * evaluation of sub-commands of the command under evaluation. The
 * variable must be updated before the evaluation of the sub-commands
 * starts and restored to its previous state right after that
 * evaluation ends. The variable is read and acted upon in execlist.
 *
 * A good usage example can be found in execwhile in loop.c, which
 * evaluates while statements. The variable is updated to disable
 * ERREXIT just before evaluating the while's condition and restored
 * to its previous state right after the evaluation of the condition.
 *
 * Bits from noerrexit_bits.
 */

/**/
int noerrexit;

/*
 * Used to suppress ERREXIT and ERRRETURN for the command under
 * evaluation.  The variable must be enabled (set to 1) at the very
 * end of the evaluation of the command. It must come after the
 * evaluation of any sub-commands of the command under evaluation. The
 * variable is read and acted upon in execlist, which also takes care
 * of initialising and resetting it to 0.
 *
 * Unlike the variable noerrexit, whose state applies to the
 * evaluation of whole sub-commands (and their direct and indirect
 * sub-commands), the scope of the variable this_noerrexit is much
 * more localized. ERREXIT and ERRRETURN are triggered at the end of
 * the function execlist after the evaluation of some or all of the
 * list's sub-commands. The role of the variable this_noerrexit is to
 * give to the functions evaluating the list's sub-commands the
 * possibility to tell the calling execlist not to trigger ERREXIT and
 * ERRRETURN. In other words, the variable acts as an additional
 * return value between the called evaluation functions and the
 * calling execlist. For that reason the variable must always be set
 * as late as possible and in particular after any sub-command
 * evaluation. If the variable is set before the evaluation of a
 * sub-command, if may affect the wrong execlist, if the sub-command
 * evaluation involves another execlist call, and/or the variable may
 * get modified by the sub-command evaluation and thus wouldn't return
 * the desired value to the calling execlist.
 *
 * Good usage examples can be found in the exec functions in loop.c,
 * which evaluate compound commands. The variable is enabled right
 * before returning from the functions, after all the sub-commands of
 * the compound commands have already been evaluated.
 *
 * 0 or 1
 */

/**/
int this_noerrexit;

/*
 * noerrs = 1: suppress error messages
 * noerrs = 2: don't set errflag on parse error, either
 */

/**/
mod_export int noerrs;

/* do not save history on exec and exit */

/**/
int nohistsave;

/* error flag: bits from enum errflag_bits */

/**/
mod_export volatile int errflag;

/*
 * State of trap return value.  Value is from enum trap_state.
 */

/**/
int trap_state;

/*
 * Value associated with return from a trap.
 * This is only active if we are inside a trap, else its value
 * is irrelevant.  It is initialised to -1 for a function trap and
 * -2 for a non-function trap and if negative is decremented as
 * we go deeper into functions and incremented as we come back up.
 * The value is used to decide if an explicit "return" should cause
 * a return from the caller of the trap; it does this by setting
 * trap_return to a status (i.e. a non-negative value).
 *
 * In summary, trap_return is
 * - zero unless we are in a trap
 * - negative in a trap unless it has triggered.  Code uses this
 *   to detect an active trap.
 * - non-negative in a trap once it was triggered.  It should remain
 *   non-negative until restored after execution of the trap.
 */

/**/
int trap_return;

/* != 0 if this is a subshell */

/**/
int subsh;

/* != 0 if we have a return pending */

/**/
mod_export volatile int retflag;

/**/
long lastval2;

/* The table of file descriptors.  A table element is zero if the  *
 * corresponding fd is not used by the shell.  It is greater than  *
 * 1 if the fd is used by a <(...) or >(...) substitution and 1 if *
 * it is an internal file descriptor which must be closed before   *
 * executing an external command.  The first ten elements of the   *
 * table is not used.  A table element is set by movefd and cleard *
 * by zclose.                                                      */

/**/
mod_export unsigned char *fdtable;

/* The allocated size of fdtable */

/**/
int fdtable_size;

/* The highest fd that marked with nonzero in fdtable */

/**/
mod_export int max_zsh_fd;

/* input fd from the coprocess */

/**/
mod_export int coprocin;

/* output fd from the coprocess */

/**/
mod_export int coprocout;

/* count of file locks recorded in fdtable */

/**/
int fdtable_flocks;


/* != 0 if the line editor is active */

/**/
mod_export int zleactive;

/* pid of process undergoing 'process substitution' */

/**/
pid_t cmdoutpid;

/* pid of last process started by <(...),  >(...) */

/**/
mod_export pid_t procsubstpid;

/* exit status of process undergoing 'process substitution' */

/**/
int cmdoutval;

/*
 * This is set by an exiting $(...) substitution to indicate we need
 * to retain the status.  We initialize it to zero if we think we need
 * to reset the status for a command.
 */

/**/
int use_cmdoutval;

/* The context in which a shell function is called, see SFC_* in zsh.h. */

/**/
mod_export int sfcontext;

/* Stack to save some variables before executing a signal handler function */

/**/
struct execstack *exstack;

/* Stack with names of function calls, 'source' calls, and 'eval' calls
 * currently active. */

/**/
mod_export Funcstack funcstack;

#define execerr()				\
    do {					\
	if (!forked) {				\
	    redir_err = lastval = 1;		\
	    goto done;				\
	} else {				\
	    _exit(1);				\
	}					\
    } while (0)

static int doneps4;
static char *STTYval;
static char *blank_env[] = { NULL };

/* Execution functions. */

static int (*execfuncs[WC_COUNT-WC_CURSH]) (Estate, int) = {
    execcursh, exectime, NULL /* execfuncdef handled specially */,
    execfor, execselect,
    execwhile, execrepeat, execcase, execif, execcond,
    execarith, execautofn, exectry
};

/* structure for command builtin for when it is used with -v or -V */
static struct builtin commandbn =
    BUILTIN("command", 0, bin_whence, 0, -1, BIN_COMMAND, "pvV", NULL);

/* parse string into a list */

/**/
mod_export Eprog
parse_string(char *s, int reset_lineno)
{
    Eprog p;
    zlong oldlineno;

    zcontext_save();
    inpush(s, INP_LINENO, NULL);
    strinbeg(0);
    oldlineno = lineno;
    if (reset_lineno)
	lineno = 1;
    p = parse_list();
    lineno = oldlineno;
    if (tok == LEXERR && !lastval)
	lastval = 1;
    strinend();
    inpop();
    zcontext_restore();
    return p;
}

/**/
#ifdef HAVE_GETRLIMIT

/* the resource limits for the shell and its children */

/**/
mod_export struct rlimit current_limits[RLIM_NLIMITS], limits[RLIM_NLIMITS];

/**/
mod_export int
zsetlimit(int limnum, char *nam)
{
    if (limits[limnum].rlim_max != current_limits[limnum].rlim_max ||
	limits[limnum].rlim_cur != current_limits[limnum].rlim_cur) {
	if (setrlimit(limnum, limits + limnum)) {
	    if (nam)
		zwarnnam(nam, "setrlimit failed: %e", errno);
	    limits[limnum] = current_limits[limnum];
	    return -1;
	}
	current_limits[limnum] = limits[limnum];
    }
    return 0;
}

/**/
mod_export int
setlimits(char *nam)
{
    int limnum;
    int ret = 0;

    for (limnum = 0; limnum < RLIM_NLIMITS; limnum++)
	if (zsetlimit(limnum, nam))
	    ret++;
    return ret;
}

/**/
#endif /* HAVE_GETRLIMIT */

/* fork and set limits */

/**/
static pid_t
zfork(struct timeval *tv)
{
    pid_t pid;
    struct timezone dummy_tz;

    /*
     * Is anybody willing to explain this test?
     */
    if (thisjob != -1 && thisjob >= jobtabsize - 1 && !expandjobtab()) {
	zerr("job table full");
	return -1;
    }
    if (tv)
	gettimeofday(tv, &dummy_tz);
    /*
     * Queueing signals is necessary on Linux because fork()
     * manipulates mutexes, leading to deadlock in memory
     * allocation.  We don't expect fork() to be particularly
     * zippy anyway.
     */
    queue_signals();
    pid = fork();
    unqueue_signals();
    if (pid == -1) {
	zerr("fork failed: %e", errno);
	return -1;
    }
#ifdef HAVE_GETRLIMIT
    if (!pid)
	/* set resource limits for the child process */
	setlimits(NULL);
#endif
    return pid;
}

/*
 *   Allen Edeln gebiet ich Andacht,
 *   Hohen und Niedern von Heimdalls Geschlecht;
 *   Ich will list_pipe's Wirken kuenden
 *   Die aeltesten Sagen, der ich mich entsinne...
 *
 * In most shells, if you do something like:
 *
 *   cat foo | while read a; do grep $a bar; done
 *
 * the shell forks and executes the loop in the sub-shell thus created.
 * In zsh this traditionally executes the loop in the current shell, which
 * is nice to have if the loop does something to change the shell, like
 * setting parameters or calling builtins.
 * Putting the loop in a sub-shell makes life easy, because the shell only
 * has to put it into the job-structure and then treats it as a normal
 * process. Suspending and interrupting is no problem then.
 * Some years ago, zsh either couldn't suspend such things at all, or
 * it got really messed up when users tried to do it. As a solution, we
 * implemented the list_pipe-stuff, which has since then become a reason
 * for many nightmares.
 * Pipelines like the one above are executed by the functions in this file
 * which call each other (and sometimes recursively). The one above, for
 * example would lead to a function call stack roughly like:
 *
 *  execlist->execpline->execcmd->execwhile->execlist->execpline
 *
 * (when waiting for the grep, ignoring execpline2 for now). At this time,
 * zsh has built two job-table entries for it: one for the cat and one for
 * the grep. If the user hits ^Z at this point (and jobbing is used), the
 * shell is notified that the grep was suspended. The list_pipe flag is
 * used to tell the execpline where it was waiting that it was in a pipeline
 * with a shell construct at the end (which may also be a shell function or
 * several other things). When zsh sees the suspended grep, it forks to let
 * the sub-shell execute the rest of the while loop. The parent shell walks
 * up in the function call stack to the first execpline. There it has to find
 * out that it has just forked and then has to add information about the sub-
 * shell (its pid and the text for it) in the job entry of the cat. The pid
 * is passed down in the list_pipe_pid variable.
 * But there is a problem: the suspended grep is a child of the parent shell
 * and can't be adopted by the sub-shell. So the parent shell also has to
 * keep the information about this process (more precisely: this pipeline)
 * by keeping the job table entry it created for it. The fact that there
 * are two jobs which have to be treated together is remembered by setting
 * the STAT_SUPERJOB flag in the entry for the cat-job (which now also
 * contains a process-entry for the whole loop -- the sub-shell) and by
 * setting STAT_SUBJOB in the job of the grep-job. With that we can keep
 * sub-jobs from being displayed and we can handle an fg/bg on the super-
 * job correctly. When the super-job is continued, the shell also wakes up
 * the sub-job. But then, the grep will exit sometime. Now the parent shell
 * has to remember not to try to wake it up again (in case of another ^Z).
 * It also has to wake up the sub-shell (which suspended itself immediately
 * after creation), so that the rest of the loop is executed by it.
 * But there is more: when the sub-shell is created, the cat may already
 * have exited, so we can't put the sub-shell in the process group of it.
 * In this case, we put the sub-shell in the process group of the parent
 * shell and in any case, the sub-shell has to put all commands executed
 * by it into its own process group, because only this way the parent
 * shell can control them since it only knows the process group of the sub-
 * shell. Of course, this information is also important when putting a job
 * in the foreground, where we have to attach its process group to the
 * controlling tty.
 * All this is made more difficult because we have to handle return values
 * correctly. If the grep is signaled, its exit status has to be propagated
 * back to the parent shell which needs it to set the exit status of the
 * super-job. And of course, when the grep is signaled (including ^C), the
 * loop has to be stopped, etc.
 * The code for all this is distributed over three files (exec.c, jobs.c,
 * and signals.c) and none of them is a simple one. So, all in all, there
 * may still be bugs, but considering the complexity (with race conditions,
 * signal handling, and all that), this should probably be expected.
 */

/**/
int list_pipe = 0, simple_pline = 0;

static pid_t list_pipe_pid;
static struct timeval list_pipe_start;
static int nowait, pline_level = 0;
static int list_pipe_child = 0, list_pipe_job;
static char list_pipe_text[JOBTEXTSIZE];

/* execute a current shell command */

/**/
static int
execcursh(Estate state, int do_exec)
{
    Wordcode end = state->pc + WC_CURSH_SKIP(state->pc[-1]);

    /* Skip word only used for try/always */
    state->pc++;

    /*
     * The test thisjob != -1 was added because sometimes thisjob
     * can be invalid at this point.  The case in question was
     * in a precmd function after operations involving background
     * jobs.
     *
     * This is because sometimes we bypass job control to execute
     * very simple functions via execssimple().
     */
    if (!list_pipe && thisjob != -1 && thisjob != list_pipe_job &&
	!hasprocs(thisjob))
	deletejob(jobtab + thisjob, 0);
    cmdpush(CS_CURSH);
    execlist(state, 1, do_exec);
    cmdpop();

    state->pc = end;
    this_noerrexit = 1;

    return lastval;
}

/* execve after handling $_ and #! */

#define POUNDBANGLIMIT 128

/**/
static int
zexecve(char *pth, char **argv, char **newenvp)
{
    int eno;
    static char buf[PATH_MAX * 2+1];
    char **eep;

    unmetafy(pth, NULL);
    for (eep = argv; *eep; eep++)
	if (*eep != pth)
	    unmetafy(*eep, NULL);
    buf[0] = '_';
    buf[1] = '=';
    if (*pth == '/')
	strcpy(buf + 2, pth);
    else
	sprintf(buf + 2, "%s/%s", unmeta(pwd), pth);
    zputenv(buf);
#ifndef FD_CLOEXEC
    closedumps();
#endif

    if (newenvp == NULL)
	    newenvp = environ;
    winch_unblock();
    execve(pth, argv, newenvp);

    /* If the execve returns (which in general shouldn't happen),   *
     * then check for an errno equal to ENOEXEC.  This errno is set *
     * if the process file has the appropriate access permission,   *
     * but has an invalid magic number in its header.               */
    if ((eno = errno) == ENOEXEC || eno == ENOENT) {
	char execvebuf[POUNDBANGLIMIT + 1], *ptr, *ptr2, *argv0;
	int fd, ct, t0;

	if ((fd = open(pth, O_RDONLY|O_NOCTTY)) >= 0) {
	    argv0 = *argv;
	    *argv = pth;
	    memset(execvebuf, '\0', POUNDBANGLIMIT + 1);
	    ct = read(fd, execvebuf, POUNDBANGLIMIT);
	    close(fd);
	    if (ct >= 0) {
		if (ct >= 2 && execvebuf[0] == '#' && execvebuf[1] == '!') {
		    for (t0 = 0; t0 != ct; t0++)
			if (execvebuf[t0] == '\n')
			    break;
		    if (t0 == ct)
			zerr("%s: bad interpreter: %s: %e", pth,
			     metafy(execvebuf + 2, -1, META_STATIC), eno);
		    else {
			while (inblank(execvebuf[t0]))
			    execvebuf[t0--] = '\0';
			for (ptr = execvebuf + 2; *ptr && *ptr == ' '; ptr++);
			for (ptr2 = ptr; *ptr && *ptr != ' '; ptr++);
			if (eno == ENOENT) {
			    char *pprog;
			    if (*ptr)
				*ptr = '\0';
			    if (*ptr2 != '/' &&
				(pprog = pathprog(ptr2, NULL))) {
				if (ptr == execvebuf + t0 + 1) {
				    argv[-1] = ptr2;
				    winch_unblock();
				    execve(pprog, argv - 1, newenvp);
				} else {
				    argv[-2] = ptr2;
				    argv[-1] = ptr + 1;
				    winch_unblock();
				    execve(pprog, argv - 2, newenvp);
				}
			    }
			    zerr("%s: bad interpreter: %s: %e", pth,
				 metafy(ptr2, -1, META_STATIC), eno);
			} else if (*ptr) {
			    *ptr = '\0';
			    argv[-2] = ptr2;
			    argv[-1] = ptr + 1;
			    winch_unblock();
			    execve(ptr2, argv - 2, newenvp);
			} else {
			    argv[-1] = ptr2;
			    winch_unblock();
			    execve(ptr2, argv - 1, newenvp);
			}
		    }
		} else if (eno == ENOEXEC) {
                    /* Perform binary safety check on classic shell    *
                     * scripts (shebang wasn't introduced until UNIX   *
                     * Seventh Edition). POSIX says we shall allow     *
                     * execution of scripts with concatenated binary   *
                     * and suggests checking a line exists before the  *
                     * first NUL character with a lowercase letter or  *
                     * expansion. This is consistent with FreeBSD sh.  */
                    int isbinary, hasletter;
                    if (!(ptr2 = memchr(execvebuf, '\0', ct))) {
                        isbinary = 0;
                    } else {
                        isbinary = 1;
                        hasletter = 0;
                        for (ptr = execvebuf; ptr < ptr2; ptr++) {
			    if (islower((unsigned char) *ptr) || *ptr == '$' || *ptr == '`')
                                hasletter = 1;
                            if (hasletter && *ptr == '\n') {
                                isbinary = 0;
                                break;
                            }
                        }
                    }
		    if (!isbinary) {
		        char** args = argv;
			if (argv[0][0] == '-' || argv[0][0] == '+') {
			    /*
			     * guard against +foo or -bar script paths being
			     * taken as options. POSIX says the script path
			     * must be passed as an *operand*. "--" would also
			     * make sure the next argument is treated as an
			     * operand with POSIX compliant sh implementations
			     * but "-" is more portable (to the Bourne shell in
			     * particular) and shorter.
			     */
			    *--args = "-";
			}
			*--args = "sh";
			winch_unblock();
			execve("/bin/sh", args, newenvp);
		    }
		}
	    } else
		eno = errno;
	    *argv = argv0;
	} else
	    eno = errno;
    }
    /* restore the original arguments and path but do not bother with *
     * null characters as these cannot be passed to external commands *
     * anyway.  So the result is truncated at the first null char.    */
    pth = metafy(pth, -1, META_NOALLOC);
    for (eep = argv; *eep; eep++)
	if (*eep != pth)
	    (void) metafy(*eep, -1, META_NOALLOC);
    return eno;
}

#define MAXCMDLEN (PATH_MAX*4)

/* test whether we really want to believe the error number */

/**/
static int
isgooderr(int e, char *dir)
{
    /*
     * Maybe the directory was unreadable, or maybe it wasn't
     * even a directory.
     */
    return ((e != EACCES || !access(dir, X_OK)) &&
	    e != ENOENT && e != ENOTDIR);
}

/*
 * Attempt to handle command not found.
 * Return 0 if the condition was handled, non-zero otherwise.
 */

/**/
static int
commandnotfound(char *arg0, LinkList args)
{
    Shfunc shf = (Shfunc)
	shfunctab->getnode(shfunctab, "command_not_found_handler");

    if (!shf) {
	lastval = 127;
	return 1;
    }

    pushnode(args, arg0);
    lastval = doshfunc(shf, args, 1);
    return 0;
}

/*
 * Search the default path for cmd.
 * pbuf of length plen is the buffer to use.
 * Return NULL if not found.
 */

static char *
search_defpath(char *cmd, char *pbuf, int plen)
{
    char *ps = DEFAULT_PATH, *pe = NULL, *s;

    for (ps = DEFAULT_PATH; ps; ps = pe ? pe+1 : NULL) {
	pe = strchr(ps, ':');
	if (*ps == '/') {
	    s = pbuf;
	    if (pe) {
		if (pe - ps >= plen)
		    continue;
		struncpy(&s, ps, pe-ps);
	    } else {
		if (strlen(ps) >= plen)
		    continue;
		strucpy(&s, ps);
	    }
	    *s++ = '/';
	    if ((s - pbuf) + strlen(cmd) >= plen)
		continue;
	    strucpy(&s, cmd);
	    if (iscom(pbuf))
		return pbuf;
	}
    }
    return NULL;
}

/* execute an external command */

/**/
static void
execute(LinkList args, int flags, int defpath)
{
    Cmdnam cn;
    char buf[MAXCMDLEN+1], buf2[MAXCMDLEN+1];
    char *s, *z, *arg0;
    char **argv, **pp, **newenvp = NULL;
    int eno = 0, ee;

    arg0 = (char *) peekfirst(args);
    if (isset(RESTRICTED) && (strchr(arg0, '/') || defpath)) {
	zerr("%s: restricted", arg0);
	_exit(1);
    }

    /* If the parameter STTY is set in the command's environment, *
     * we first run the stty command with the value of this       *
     * parameter as it arguments. If the parameter is empty, we   *
     * do nothing, but this causes the terminal settings to be    *
     * restored later which can be useful.                        */
    if ((s = STTYval) && *s && isatty(0) && (GETPGRP() == getpid())) {
	char *t = tricat("stty", " ", s);

	STTYval = 0;	/* this prevents infinite recursion */
	zsfree(s);
	execstring(t, 1, 0, "stty");
	zsfree(t);
    } else if (s) {
	STTYval = 0;
	zsfree(s);
    }

    /* If ARGV0 is in the commands environment, we use *
     * that as argv[0] for this external command       */
    if (unset(RESTRICTED) && (z = zgetenv("ARGV0"))) {
	setdata(firstnode(args), (void *) ztrdup(z));
	/*
	 * Note we don't do anything with the parameter structure
	 * for ARGV0: that's OK since we're about to exec or exit
	 * on failure.
	 */
#ifdef USE_SET_UNSET_ENV
	unsetenv("ARGV0");
#else
	delenvvalue(z - 6);
#endif
    } else if (flags & BINF_DASH) {
    /* Else if the pre-command `-' was given, we add `-' *
     * to the front of argv[0] for this command.         */
	sprintf(buf2, "-%s", arg0);
	setdata(firstnode(args), (void *) ztrdup(buf2));
    }

    argv = makecline(args);
    if (flags & BINF_CLEARENV)
	newenvp = blank_env;

    /*
     * Note that we don't close fd's attached to process substitution
     * here, which should be visible to external processes.
     */
    closem(FDT_XTRACE, 0);
#ifndef FD_CLOEXEC
    if (SHTTY != -1) {
	close(SHTTY);
	SHTTY = -1;
    }
#endif
    child_unblock();
    if ((int) strlen(arg0) >= PATH_MAX) {
	zerr("command too long: %s", arg0);
	_exit(1);
    }
    for (s = arg0; *s; s++)
	if (*s == '/') {
	    int lerrno = zexecve(arg0, argv, newenvp);
	    if (arg0 == s || unset(PATHDIRS) ||
		(arg0[0] == '.' && (arg0 + 1 == s ||
				    (arg0[1] == '.' && arg0 + 2 == s)))) {
		zerr("%e: %s", lerrno, arg0);
		_exit((lerrno == EACCES || lerrno == ENOEXEC) ? 126 : 127);
	    }
	    break;
	}

    /* for command -p, search the default path */
    if (defpath) {
	char pbuf[PATH_MAX+1];
	char *dptr;

	if (!search_defpath(arg0, pbuf, PATH_MAX)) {
	    if (commandnotfound(arg0, args) == 0)
		_realexit();
	    zerr("command not found: %s", arg0);
	    _exit(127);
	}

	ee = zexecve(pbuf, argv, newenvp);

	if ((dptr = strrchr(pbuf, '/')))
	    *dptr = '\0';
	if (isgooderr(ee, *pbuf ? pbuf : "/"))
	    eno = ee;

    } else {

	if ((cn = (Cmdnam) cmdnamtab->getnode(cmdnamtab, arg0))) {
	    char nn[PATH_MAX+1], *dptr;

	    if (cn->node.flags & HASHED)
		strcpy(nn, cn->u.cmd);
	    else {
		for (pp = path; pp < cn->u.name; pp++)
		    if (!**pp || (**pp == '.' && (*pp)[1] == '\0')) {
			ee = zexecve(arg0, argv, newenvp);
			if (isgooderr(ee, *pp))
			    eno = ee;
		    } else if (**pp != '/') {
			z = buf;
			strucpy(&z, *pp);
			*z++ = '/';
			strcpy(z, arg0);
			ee = zexecve(buf, argv, newenvp);
			if (isgooderr(ee, *pp))
			    eno = ee;
		    }
		strcpy(nn, cn->u.name ? *(cn->u.name) : "");
		strcat(nn, "/");
		strcat(nn, cn->node.nam);
	    }
	    ee = zexecve(nn, argv, newenvp);

	    if ((dptr = strrchr(nn, '/')))
		*dptr = '\0';
	    if (isgooderr(ee, *nn ? nn : "/"))
		eno = ee;
	}
	for (pp = path; *pp; pp++)
	    if (!(*pp)[0] || ((*pp)[0] == '.' && !(*pp)[1])) {
		ee = zexecve(arg0, argv, newenvp);
		if (isgooderr(ee, *pp))
		    eno = ee;
	    } else {
		z = buf;
		strucpy(&z, *pp);
		*z++ = '/';
		strcpy(z, arg0);
		ee = zexecve(buf, argv, newenvp);
		if (isgooderr(ee, *pp))
		    eno = ee;
	    }
    }

    if (eno)
	zerr("%e: %s", eno, arg0);
    else if (commandnotfound(arg0, args) == 0)
	_realexit();
    else
	zerr("command not found: %s", arg0);
    /* This is bash behavior, but fails to restore interactive settings etc.
    lastval = ((eno == EACCES || eno == ENOEXEC) ? 126 : 127);
    return;
    */
    _exit((eno == EACCES || eno == ENOEXEC) ? 126 : 127);
}

#define RET_IF_COM(X) { if (iscom(X)) return docopy ? dupstring(X) : arg0; }

/*
 * Get the full pathname of an external command.
 * If the second argument is zero, return the first argument if found;
 * if non-zero, return the path using heap memory.  (RET_IF_COM(X),
 * above).
 * If the third argument is non-zero, use the system default path
 * instead of the current path.
 */

/**/
mod_export char *
findcmd(char *arg0, int docopy, int default_path)
{
    char **pp;
    char *z, *s, buf[MAXCMDLEN];
    Cmdnam cn;

    if (default_path)
    {
	if (search_defpath(arg0, buf, MAXCMDLEN))
	    return docopy ? dupstring(buf) : arg0;
	return NULL;
    }
    cn = (Cmdnam) cmdnamtab->getnode(cmdnamtab, arg0);
    if (!cn && isset(HASHCMDS) && !isrelative(arg0))
	cn = hashcmd(arg0, path);
    if ((int) strlen(arg0) > PATH_MAX)
	return NULL;
    if ((s = strchr(arg0, '/'))) {
	RET_IF_COM(arg0);
	if (arg0 == s || unset(PATHDIRS) || !strncmp(arg0, "./", 2) ||
	    !strncmp(arg0, "../", 3)) {
	    return NULL;
	}
    }
    if (cn) {
	char nn[PATH_MAX+1];

	if (cn->node.flags & HASHED)
	    strcpy(nn, cn->u.cmd);
	else {
	    for (pp = path; pp < cn->u.name; pp++)
		if (**pp != '/') {
		    z = buf;
		    if (**pp) {
			strucpy(&z, *pp);
			*z++ = '/';
		    }
		    strcpy(z, arg0);
		    RET_IF_COM(buf);
		}
	    strcpy(nn, cn->u.name ? *(cn->u.name) : "");
	    strcat(nn, "/");
	    strcat(nn, cn->node.nam);
	}
	RET_IF_COM(nn);
    }
    for (pp = path; *pp; pp++) {
	z = buf;
	if (**pp) {
	    strucpy(&z, *pp);
	    *z++ = '/';
	}
	strcpy(z, arg0);
	RET_IF_COM(buf);
    }
    return NULL;
}

/*
 * Return TRUE if the given path denotes an executable regular file, or a
 * symlink to one.
 */

/**/
int
iscom(char *s)
{
    struct stat statbuf;
    char *us = unmeta(s);

    return (access(us, X_OK) == 0 && stat(us, &statbuf) >= 0 &&
	    S_ISREG(statbuf.st_mode));
}

/**/
int
isreallycom(Cmdnam cn)
{
    char fullnam[MAXCMDLEN];

    if (cn->node.flags & HASHED)
	strcpy(fullnam, cn->u.cmd);
    else if (!cn->u.name)
	return 0;
    else {
	strcpy(fullnam, *(cn->u.name));
	strcat(fullnam, "/");
	strcat(fullnam, cn->node.nam);
    }
    return iscom(fullnam);
}

/*
 * Return TRUE if the given path contains a dot or dot-dot component
 * and does not start with a slash.
 */

/**/
int
isrelative(char *s)
{
    if (*s != '/')
	return 1;
    for (; *s; s++)
	if (*s == '.' && s[-1] == '/' &&
	    (s[1] == '/' || s[1] == '\0' ||
	     (s[1] == '.' && (s[2] == '/' || s[2] == '\0'))))
	    return 1;
    return 0;
}

/**/
mod_export Cmdnam
hashcmd(char *arg0, char **pp)
{
    Cmdnam cn;
    char *s, buf[PATH_MAX+1];
    char **pq;

    if (*arg0 == '/')
        return NULL;
    for (; *pp; pp++)
	if (**pp == '/') {
	    s = buf;
	    struncpy(&s, *pp, PATH_MAX);
	    *s++ = '/';
	    if ((s - buf) + strlen(arg0) >= PATH_MAX)
		continue;
	    strcpy(s, arg0);
	    if (iscom(buf))
		break;
	}

    if (!*pp)
	return NULL;

    cn = (Cmdnam) zshcalloc(sizeof *cn);
    cn->node.flags = 0;
    cn->u.name = pp;
    cmdnamtab->addnode(cmdnamtab, ztrdup(arg0), cn);

    if (isset(HASHDIRS)) {
	for (pq = pathchecked; pq <= pp; pq++)
	    hashdir(pq);
	pathchecked = pp + 1;
    }

    return cn;
}

/* The value that 'locallevel' had when we forked. When we get back to this
 * level, the current process (which is a subshell) will terminate.
 */

/**/
int
forklevel;

/* Arguments to entersubsh() */
enum {
    /* Subshell is to be run asynchronously (else synchronously) */
    ESUB_ASYNC = 0x01,
    /*
     * Perform process group and tty handling and clear the
     * (real) job table, since it won't be any longer valid
     */
    ESUB_PGRP = 0x02,
    /* Don't unset traps */
    ESUB_KEEPTRAP = 0x04,
    /* This is only a fake entry to a subshell */
    ESUB_FAKE = 0x08,
    /* Release the process group if pid is the shell's process group */
    ESUB_REVERTPGRP = 0x10,
    /* Don't handle the MONITOR option even if previously set */
    ESUB_NOMONITOR = 0x20,
    /* This is a subshell where job control is allowed */
    ESUB_JOB_CONTROL = 0x40
};

/*
 * gleaderp may be NULL.  Otherwise, *gleaderp is set to point to the
 * group leader of the job of the new process if this is assigned.  Else
 * it is left alone: it is initialised to -1.
 */

/**/
static void
entersubsh(int flags, struct entersubsh_ret *retp)
{
    int i, sig, monitor, job_control_ok;

    if (!(flags & ESUB_KEEPTRAP))
	for (sig = 0; sig <= SIGCOUNT; sig++)
	    if (!(sigtrapped[sig] & ZSIG_FUNC) &&
		!(isset(POSIXTRAPS) && (sigtrapped[sig] & ZSIG_IGNORED)))
		unsettrap(sig);
    monitor = isset(MONITOR);
    job_control_ok = monitor && (flags & ESUB_JOB_CONTROL) && isset(POSIXJOBS);
    exit_val = 0; 		/* parent exit status is irrelevant */
    if (flags & ESUB_NOMONITOR)
	opts[MONITOR] = 0;
    if (!isset(MONITOR)) {
	if (flags & ESUB_ASYNC) {
	    settrap(SIGINT, NULL, 0);
	    settrap(SIGQUIT, NULL, 0);
	    if (isatty(0)) {
		close(0);
		if (open("/dev/null", O_RDWR | O_NOCTTY)) {
		    zerr("can't open /dev/null: %e", errno);
		    _exit(1);
		}
	    }
	}
    } else if (thisjob != -1 && (flags & ESUB_PGRP)) {
	if (jobtab[list_pipe_job].gleader && (list_pipe || list_pipe_child)) {
	    if (setpgrp(0L, jobtab[list_pipe_job].gleader) == -1 ||
		(killpg(jobtab[list_pipe_job].gleader, 0) == -1  &&
		 errno == ESRCH)) {
		jobtab[list_pipe_job].gleader =
		    jobtab[thisjob].gleader = (list_pipe_child ? mypgrp : getpid());
		setpgrp(0L, jobtab[list_pipe_job].gleader);
		if (!(flags & ESUB_ASYNC))
		    attachtty(jobtab[thisjob].gleader);
	    }
	    if (retp && !(flags & ESUB_ASYNC)) {
		retp->gleader = jobtab[list_pipe_job].gleader;
		retp->list_pipe_job = list_pipe_job;
	    }
	}
	else if (!jobtab[thisjob].gleader ||
		 setpgrp(0L, jobtab[thisjob].gleader) == -1) {
	    /*
	     * This is the standard point at which a newly started
	     * process gets put into the foreground by taking over
	     * the terminal.  Note that in normal circumstances we do
	     * this only from the process itself.  This only works if
	     * we are still ignoring SIGTTOU at this point; in this
	     * case ignoring the signal has the special effect that
	     * the operation is allowed to work (in addition to not
	     * causing the shell to be suspended).
	     */
	    jobtab[thisjob].gleader = getpid();
	    if (list_pipe_job != thisjob &&
		!jobtab[list_pipe_job].gleader)
		jobtab[list_pipe_job].gleader = jobtab[thisjob].gleader;
	    setpgrp(0L, jobtab[thisjob].gleader);
	    if (!(flags & ESUB_ASYNC)) {
		attachtty(jobtab[thisjob].gleader);
		if (retp) {
		    retp->gleader = jobtab[thisjob].gleader;
		    if (list_pipe_job != thisjob)
			retp->list_pipe_job = list_pipe_job;
		}
	    }
	}
    }
    if (!(flags & ESUB_FAKE))
	subsh = 1;
    /*
     * Increment the visible parameter ZSH_SUBSHELL even if this
     * is a fake subshell because we are exec'ing at the end.
     * Logically this should be equivalent to a real subshell so
     * we don't hang out the dirty washing.
     */
    zsh_subshell++;
    if ((flags & ESUB_REVERTPGRP) && getpid() == mypgrp)
	release_pgrp();
    shout = NULL;
    if (flags & ESUB_NOMONITOR) {
	/*
	 * Allowing any form of interactive signalling here is
	 * actively harmful as we are in a context where there is no
	 * control over the process.
	 */
	signal_ignore(SIGTTOU);
	signal_ignore(SIGTTIN);
	signal_ignore(SIGTSTP);
    } else if (!job_control_ok) {
	/*
	 * If this process is not going to be doing job control,
	 * we don't want to do special things with the corresponding
	 * signals.  If it is, we need to keep the special behaviour:
	 * see note about attachtty() above.
	 */
	signal_default(SIGTTOU);
	signal_default(SIGTTIN);
	signal_default(SIGTSTP);
    }
    if (interact) {
	signal_default(SIGTERM);
	if (!(sigtrapped[SIGINT] & ZSIG_IGNORED))
	    signal_default(SIGINT);
	if (!(sigtrapped[SIGPIPE]))
	    signal_default(SIGPIPE);
    }
    if (!(sigtrapped[SIGQUIT] & ZSIG_IGNORED))
	signal_default(SIGQUIT);
    /*
     * sigtrapped[sig] == ZSIG_IGNORED for signals that remain ignored,
     * but other trapped signals are temporarily blocked when intrap,
     * and must be unblocked before continuing into the subshell.  This
     * is orthogonal to what the default handler for the signal may be.
     *
     * Start loop at 1 because 0 is SIGEXIT
     */
    if (intrap)
	for (sig = 1; sig <= SIGCOUNT; sig++)
	    if (sigtrapped[sig] && sigtrapped[sig] != ZSIG_IGNORED)
		signal_unblock(signal_mask(sig));
    if (!job_control_ok)
	opts[MONITOR] = 0;
    opts[USEZLE] = 0;
    zleactive = 0;
    /*
     * If we've saved fd's for later restoring, we're never going
     * to restore them now, so just close them.
     */
    for (i = 10; i <= max_zsh_fd; i++) {
	if (fdtable[i] & FDT_SAVED_MASK)
	    zclose(i);
    }
    if (flags & ESUB_PGRP)
	clearjobtab(monitor);
    get_usage();
    forklevel = locallevel;
}

/* execute a string */

/**/
mod_export void
execstring(char *s, int dont_change_job, int exiting, char *context)
{
    Eprog prog;

    pushheap();
    if (isset(VERBOSE)) {
	zputs(s, stderr);
	fputc('\n', stderr);
	fflush(stderr);
    }
    if ((prog = parse_string(s, 0)))
	execode(prog, dont_change_job, exiting, context);
    popheap();
}

/**/
mod_export void
execode(Eprog p, int dont_change_job, int exiting, char *context)
{
    struct estate s;
    static int zsh_eval_context_len;
    int alen;

    if (!zsh_eval_context_len) {
	zsh_eval_context_len = 16;
	alen = 0;
	zsh_eval_context = (char **)zalloc(zsh_eval_context_len *
					   sizeof(*zsh_eval_context));
    } else {
	alen = arrlen(zsh_eval_context);
	if (zsh_eval_context_len == alen + 1) {
	    zsh_eval_context_len *= 2;
	    zsh_eval_context = zrealloc(zsh_eval_context,
					zsh_eval_context_len *
					sizeof(*zsh_eval_context));
	}
    }
    zsh_eval_context[alen] = context;
    zsh_eval_context[alen+1] = NULL;

    s.prog = p;
    s.pc = p->prog;
    s.strs = p->strs;
    useeprog(p);		/* Mark as in use */

    execlist(&s, dont_change_job, exiting);

    freeeprog(p);		/* Free if now unused */

    /*
     * zsh_eval_context may have been altered by a recursive
     * call, but that's OK since we're using the global value.
     */
    zsh_eval_context[alen] = NULL;
}

/* Execute a simplified command. This is used to execute things that
 * will run completely in the shell, so that we can by-pass all that
 * nasty job-handling and redirection stuff in execpline and execcmd. */

/**/
static int
execsimple(Estate state)
{
    wordcode code = *state->pc++;
    int lv, otj;

    if (errflag)
	return (lastval = 1);

    if (!isset(EXECOPT))
	return lastval = 0;

    /* In evaluated traps, don't modify the line number. */
    if (!IN_EVAL_TRAP() && !ineval && code)
	lineno = code - 1;

    code = wc_code(*state->pc++);

    /*
     * Because we're bypassing job control, ensure the called
     * code doesn't see the current job.
     */
    otj = thisjob;
    thisjob = -1;

    if (code == WC_ASSIGN) {
	cmdoutval = 0;
	addvars(state, state->pc - 1, 0);
	setunderscore("");
	if (isset(XTRACE)) {
	    fputc('\n', xtrerr);
	    fflush(xtrerr);
	}
	lv = (errflag ? errflag : cmdoutval);
    } else {
	int q = queue_signal_level();
	dont_queue_signals();
	if (errflag)
	    lv = errflag;
	else if (code == WC_FUNCDEF)
	    lv = execfuncdef(state, NULL);
	else
	    lv = (execfuncs[code - WC_CURSH])(state, 0);
	restore_queue_signals(q);
    }

    thisjob = otj;

    return lastval = lv;
}

/* Main routine for executing a list.                                *
 * exiting means that the (sub)shell we are in is a definite goner   *
 * after the current list is finished, so we may be able to exec the *
 * last command directly instead of forking.  If dont_change_job is  *
 * nonzero, then restore the current job number after executing the  *
 * list.                                                             */

/**/
void
execlist(Estate state, int dont_change_job, int exiting)
{
    static int donetrap;
    Wordcode next;
    wordcode code;
    int ret, cj, csp, ltype;
    int old_pline_level, old_list_pipe, old_list_pipe_job;
    char *old_list_pipe_text;
    zlong oldlineno;
    /*
     * ERREXIT only forces the shell to exit if the last command in a &&
     * or || fails.  This is the case even if an earlier command is a
     * shell function or other current shell structure, so we have to set
     * noerrexit here if the sublist is not of type END.
     */
    int oldnoerrexit = noerrexit;

    queue_signals();

    cj = thisjob;
    old_pline_level = pline_level;
    old_list_pipe = list_pipe;
    old_list_pipe_job = list_pipe_job;
    if (*list_pipe_text)
	old_list_pipe_text = ztrdup(list_pipe_text);
    else
	old_list_pipe_text = NULL;
    oldlineno = lineno;

    if (sourcelevel && unset(SHINSTDIN)) {
	pline_level = list_pipe = list_pipe_job = 0;
	*list_pipe_text = '\0';
    }

    /* Loop over all sets of comands separated by newline, *
     * semi-colon or ampersand (`sublists').               */
    code = *state->pc++;
    if (wc_code(code) != WC_LIST) {
	/* Empty list; this returns status zero. */
	lastval = 0;
    }
    while (wc_code(code) == WC_LIST && !breaks && !retflag && !errflag) {
	int donedebug;
	int this_donetrap = 0;
	this_noerrexit = 0;

	ltype = WC_LIST_TYPE(code);
	csp = cmdsp;

	if (!IN_EVAL_TRAP() && !ineval) {
	    /*
	     * Ensure we have a valid line number for debugging,
	     * unless we are in an evaluated trap in which case
	     * we retain the line number from the context.
	     * This was added for DEBUGBEFORECMD but I've made
	     * it unconditional to keep dependencies to a minimum.
	     *
	     * The line number is updated for individual pipelines.
	     * This isn't necessary for debug traps since they only
	     * run once per sublist.
	     */
	    wordcode code2 = *state->pc, lnp1 = 0;
	    if (ltype & Z_SIMPLE) {
		lnp1 = code2;
	    } else if (wc_code(code2) == WC_SUBLIST) {
		if (WC_SUBLIST_FLAGS(code2) == WC_SUBLIST_SIMPLE)
		    lnp1 = state->pc[1];
		else
		    lnp1 = WC_PIPE_LINENO(state->pc[1]);
	    }
	    if (lnp1)
		lineno = lnp1 - 1;
	}

	if (sigtrapped[SIGDEBUG] && isset(DEBUGBEFORECMD) && !intrap) {
	    Wordcode pc2 = state->pc;
	    int oerrexit_opt = opts[ERREXIT];
	    Param pm;
	    opts[ERREXIT] = 0;
	    noerrexit |= NOERREXIT_EXIT | NOERREXIT_RETURN;
	    if (ltype & Z_SIMPLE) /* skip the line number */
		pc2++;
	    pm = assignsparam("ZSH_DEBUG_CMD",
			      getpermtext(state->prog, pc2, 0),
			      0);

	    exiting = donetrap;
	    ret = lastval;
	    dotrap(SIGDEBUG);
	    if (!retflag)
		lastval = ret;
	    donetrap = exiting;
	    noerrexit = oldnoerrexit;
	    /*
	     * Only execute the trap once per sublist, even
	     * if the DEBUGBEFORECMD option changes.
	     */
	    donedebug = isset(ERREXIT) ? 2 : 1;
	    opts[ERREXIT] = oerrexit_opt;
	    if (pm)
		unsetparam_pm(pm, 0, 1);
	} else
	    donedebug = intrap ? 1 : 0;

	/* Reset donetrap:  this ensures that a trap is only *
	 * called once for each sublist that fails.          */
	donetrap = 0;
	if (ltype & Z_SIMPLE) {
	    next = state->pc + WC_LIST_SKIP(code);
	    if (donedebug != 2)
		execsimple(state);
	    state->pc = next;
	    goto sublist_done;
	}

	/* Loop through code followed by &&, ||, or end of sublist. */
	code = *state->pc++;
	if (donedebug == 2) {
	    /* Skip sublist. */
	    while (wc_code(code) == WC_SUBLIST) {
		state->pc = state->pc + WC_SUBLIST_SKIP(code);
		if (WC_SUBLIST_TYPE(code) == WC_SUBLIST_END)
		    break;
		code = *state->pc++;
	    }
	    donetrap = 1;
	    /* yucky but consistent... */
	    goto sublist_done;
	}
	while (wc_code(code) == WC_SUBLIST) {
	    this_noerrexit = 0;
	    int isandor = WC_SUBLIST_TYPE(code) != WC_SUBLIST_END;
	    int isnot = WC_SUBLIST_FLAGS(code) & WC_SUBLIST_NOT;
	    next = state->pc + WC_SUBLIST_SKIP(code);
	    /* suppress errexit for commands before && and || and after ! */
	    if (isandor || isnot)
		noerrexit |= NOERREXIT_EXIT | NOERREXIT_RETURN;
	    switch (WC_SUBLIST_TYPE(code)) {
	    case WC_SUBLIST_END:
		/* End of sublist; just execute, ignoring status. */
		if (WC_SUBLIST_FLAGS(code) & WC_SUBLIST_SIMPLE)
		    execsimple(state);
		else
		    execpline(state, code, ltype, (ltype & Z_END) && exiting);
		state->pc = next;
		/* suppress errexit for the command "! ..." */
		if (isnot)
		  this_noerrexit = 1;
		goto sublist_done;
		break;
	    case WC_SUBLIST_AND:
		/* If the return code is non-zero, we skip pipelines until *
		 * we find a sublist followed by ORNEXT.                   */
		if ((ret = ((WC_SUBLIST_FLAGS(code) & WC_SUBLIST_SIMPLE) ?
			    execsimple(state) :
			    execpline(state, code, Z_SYNC, 0))) || breaks) {
		    state->pc = next;
		    code = *state->pc++;
		    next = state->pc + WC_SUBLIST_SKIP(code);
		    while (wc_code(code) == WC_SUBLIST &&
			   WC_SUBLIST_TYPE(code) == WC_SUBLIST_AND) {
			state->pc = next;
			code = *state->pc++;
			next = state->pc + WC_SUBLIST_SKIP(code);
		    }
		    if (wc_code(code) != WC_SUBLIST) {
			/* We've skipped to the end of the list, not executing *
			 * the final pipeline, so don't perform error handling *
			 * for this sublist.                                   */
			this_donetrap = 1;
			goto sublist_done;
		    } else if (WC_SUBLIST_TYPE(code) == WC_SUBLIST_END) {
			this_donetrap = 1;
			/*
			 * Treat this in the same way as if we reached
			 * the end of the sublist normally.
			 */
			state->pc = next;
			goto sublist_done;
		    }
		}
		cmdpush(CS_CMDAND);
		break;
	    case WC_SUBLIST_OR:
		/* If the return code is zero, we skip pipelines until *
		 * we find a sublist followed by ANDNEXT.              */
		if (!(ret = ((WC_SUBLIST_FLAGS(code) & WC_SUBLIST_SIMPLE) ?
			     execsimple(state) :
			     execpline(state, code, Z_SYNC, 0))) || breaks) {
		    state->pc = next;
		    code = *state->pc++;
		    next = state->pc + WC_SUBLIST_SKIP(code);
		    while (wc_code(code) == WC_SUBLIST &&
			   WC_SUBLIST_TYPE(code) == WC_SUBLIST_OR) {
			state->pc = next;
			code = *state->pc++;
			next = state->pc + WC_SUBLIST_SKIP(code);
		    }
		    if (wc_code(code) != WC_SUBLIST) {
			/* We've skipped to the end of the list, not executing *
			 * the final pipeline, so don't perform error handling *
			 * for this sublist.                                   */
			this_donetrap = 1;
			goto sublist_done;
		    } else if (WC_SUBLIST_TYPE(code) == WC_SUBLIST_END) {
			this_donetrap = 1;
			/*
			 * Treat this in the same way as if we reached
			 * the end of the sublist normally.
			 */
			state->pc = next;
			goto sublist_done;
		    }
		}
		cmdpush(CS_CMDOR);
		break;
	    }
	    state->pc = next;
	    code = *state->pc++;
	    noerrexit = oldnoerrexit;
	}
	state->pc--;
sublist_done:

	noerrexit = oldnoerrexit;

	if (sigtrapped[SIGDEBUG] && !isset(DEBUGBEFORECMD) && !donedebug) {
	    /*
	     * Save and restore ERREXIT for consistency with
	     * DEBUGBEFORECMD, even though it's not used.
	     */
	    int oerrexit_opt = opts[ERREXIT];
	    opts[ERREXIT] = 0;
	    noerrexit |= NOERREXIT_EXIT | NOERREXIT_RETURN;
	    exiting = donetrap;
	    ret = lastval;
	    dotrap(SIGDEBUG);
	    if (!retflag)
		lastval = ret;
	    donetrap = exiting;
	    noerrexit = oldnoerrexit;
	    opts[ERREXIT] = oerrexit_opt;
	}

	cmdsp = csp;

	/* Check whether we are suppressing traps/errexit *
	 * (typically in init scripts) and if we haven't  *
	 * already performed them for this sublist.       */
	if (!this_noerrexit && !donetrap && !this_donetrap) {
	    if (sigtrapped[SIGZERR] && lastval &&
		!(noerrexit & NOERREXIT_EXIT)) {
		dotrap(SIGZERR);
		donetrap = 1;
	    }
	    if (lastval) {
		int errreturn = isset(ERRRETURN) &&
		    (isset(INTERACTIVE) || locallevel || sourcelevel) &&
		    !(noerrexit & NOERREXIT_RETURN);
		int errexit = (isset(ERREXIT) ||
			       (isset(ERRRETURN) && !errreturn)) &&
		    !(noerrexit & NOERREXIT_EXIT);
		if (errexit) {
		    errflag = 0;
		    if (sigtrapped[SIGEXIT])
			dotrap(SIGEXIT);
		    if (mypid != getpid())
			_realexit();
		    else
			realexit();
		}
		if (errreturn) {
		    retflag = 1;
		    breaks = loops;
		}
	    }
	}
	if (ltype & Z_END)
	    break;
	code = *state->pc++;
    }
    this_noerrexit = 0;
    pline_level = old_pline_level;
    list_pipe = old_list_pipe;
    list_pipe_job = old_list_pipe_job;
    if (old_list_pipe_text) {
	strcpy(list_pipe_text, old_list_pipe_text);
	zsfree(old_list_pipe_text);
    } else {
	*list_pipe_text = '\0';
    }
    lineno = oldlineno;
    if (dont_change_job)
	thisjob = cj;

    if (exiting && sigtrapped[SIGEXIT]) {
	int eflag = errflag;
	errflag = 0;	/* Clear the context for trap */
	dotrap(SIGEXIT);
	/* Make sure this doesn't get executed again. */
	sigtrapped[SIGEXIT] = 0;
	errflag = eflag;
    }

    unqueue_signals();
}

/* Execute a pipeline.                                                *
 * last1 is a flag that this command is the last command in a shell   *
 * that is about to exit, so we can exec instead of forking.  It gets *
 * passed all the way down to execcmd() which actually makes the      *
 * decision.  A 0 is always passed if the command is not the last in  *
 * the pipeline.  This function assumes that the sublist is not NULL. *
 * If last1 is zero but the command is at the end of a pipeline, we   *
 * pass 2 down to execcmd().                                          *
 */

/**/
static int
execpline(Estate state, wordcode slcode, int how, int last1)
{
    int ipipe[2], opipe[2];
    int pj, newjob;
    int old_simple_pline = simple_pline;
    int slflags = WC_SUBLIST_FLAGS(slcode);
    wordcode code = *state->pc++;
    static int lastwj, lpforked;

    if (wc_code(code) != WC_PIPE && !(how & Z_TIMED))
	return lastval = (slflags & WC_SUBLIST_NOT) != 0;
    else if (slflags & WC_SUBLIST_NOT)
	last1 = 0;

    /* If trap handlers are allowed to run here, they may start another
     * external job in the middle of us starting this one, which can
     * result in jobs being reaped before their job table entries have
     * been initialized, which in turn leads to waiting forever for
     * jobs that no longer exist.  So don't do that.
     */
    queue_signals();

    pj = thisjob;
    ipipe[0] = ipipe[1] = opipe[0] = opipe[1] = 0;
    child_block();

    /*
     * Get free entry in job table and initialize it.  This is currently
     * the only call to initjob() (apart from a minor exception in
     * clearjobtab()), so this is also the only place where we can
     * expand the job table under us.
     */
    if ((thisjob = newjob = initjob()) == -1) {
	child_unblock();
	unqueue_signals();
	return 1;
    }
    if (how & Z_TIMED)
	jobtab[thisjob].stat |= STAT_TIMED;

    if (slflags & WC_SUBLIST_COPROC) {
	how = Z_ASYNC;
	if (coprocin >= 0) {
	    zclose(coprocin);
	    zclose(coprocout);
	}
	if (mpipe(ipipe) < 0) {
	    coprocin = coprocout = -1;
	    slflags &= ~WC_SUBLIST_COPROC;
	} else if (mpipe(opipe) < 0) {
	    close(ipipe[0]);
	    close(ipipe[1]);
	    coprocin = coprocout = -1;
	    slflags &= ~WC_SUBLIST_COPROC;
	} else {
	    coprocin = ipipe[0];
	    coprocout = opipe[1];
	    fdtable[coprocin] = fdtable[coprocout] = FDT_UNUSED;
	}
    }
    /* This used to set list_pipe_pid=0 unconditionally, but in things
     * like `ls|if true; then sleep 20; cat; fi' where the sleep was
     * stopped, the top-level execpline() didn't get the pid for the
     * sub-shell because it was overwritten. */
    if (!pline_level++) {
        list_pipe_pid = 0;
	nowait = 0;
	simple_pline = (WC_PIPE_TYPE(code) == WC_PIPE_END);
	list_pipe_job = newjob;
    }
    lastwj = lpforked = 0;
    execpline2(state, code, how, opipe[0], ipipe[1], last1);
    pline_level--;
    if (how & Z_ASYNC) {
	clearoldjobtab();
	lastwj = newjob;

        if (thisjob == list_pipe_job)
            list_pipe_job = 0;
	jobtab[thisjob].stat |= STAT_NOSTTY;
	if (slflags & WC_SUBLIST_COPROC) {
	    zclose(ipipe[1]);
	    zclose(opipe[0]);
	}
	if (how & Z_DISOWN) {
	    pipecleanfilelist(jobtab[thisjob].filelist, 0);
	    deletejob(jobtab + thisjob, 1);
	    thisjob = -1;
	}
	else
	    spawnjob();
	child_unblock();
	unqueue_signals();
	/* Executing background code resets shell status */
	return lastval = 0;
    } else {
	if (newjob != lastwj) {
	    Job jn = jobtab + newjob;
	    int updated;

	    if (newjob == list_pipe_job && list_pipe_child)
		_exit(0);

	    lastwj = thisjob = newjob;

	    if (list_pipe || (pline_level && !(how & Z_TIMED) &&
			      !(jn->stat & STAT_NOSTTY)))
		jn->stat |= STAT_NOPRINT;

	    if (nowait) {
		if(!pline_level) {
		    int jobsub;
		    struct process *pn, *qn;

		    curjob = newjob;
		    DPUTS(!list_pipe_pid, "invalid list_pipe_pid");
		    addproc(list_pipe_pid, list_pipe_text, 0,
			    &list_pipe_start, -1, -1);

		    /* If the super-job contains only the sub-shell, the
		       sub-shell is the group leader. */
		    if (!jn->procs->next || lpforked == 2) {
			jn->gleader = list_pipe_pid;
			jn->stat |= STAT_SUBLEADER;
			/*
			 * Pick up any subjob that's still lying around
			 * as it's now our responsibility.
			 * If we find it we're a SUPERJOB.
			 */
			for (jobsub = 1; jobsub <= maxjob; jobsub++) {
			    Job jnsub = jobtab + jobsub;
			    if (jnsub->stat & STAT_SUBJOB_ORPHANED) {
				jn->other = jobsub;
				jn->stat |= STAT_SUPERJOB;
				jnsub->stat &= ~STAT_SUBJOB_ORPHANED;
				jnsub->other = list_pipe_pid;
			    }
			}
		    }
		    for (pn = jobtab[jn->other].procs; pn; pn = pn->next)
			if (WIFSTOPPED(pn->status))
			    break;

		    if (pn) {
			for (qn = jn->procs; qn->next; qn = qn->next);
			qn->status = pn->status;
		    }

		    jn->stat &= ~(STAT_DONE | STAT_NOPRINT);
		    jn->stat |= STAT_STOPPED | STAT_CHANGED | STAT_LOCKED |
			STAT_INUSE;
		    printjob(jn, !!isset(LONGLISTJOBS), 1);
		}
		else if (newjob != list_pipe_job)
		    deletejob(jn, 0);
		else
		    lastwj = -1;
	    }

	    errbrk_saved = 0;
	    for (; !nowait;) {
		if (list_pipe_child) {
		    jn->stat |= STAT_NOPRINT;
		    makerunning(jn);
		}
		if (!(jn->stat & STAT_LOCKED)) {
		    updated = hasprocs(thisjob);
		    waitjobs();		/* deals with signal queue */
		    child_block();
		} else
		    updated = 0;
		if (!updated &&
		    list_pipe_job && hasprocs(list_pipe_job) &&
		    !(jobtab[list_pipe_job].stat & STAT_STOPPED)) {
		    int q = queue_signal_level();
		    child_unblock();
		    child_block();
		    dont_queue_signals();
		    restore_queue_signals(q);
		}
		if (list_pipe_child &&
		    jn->stat & STAT_DONE &&
		    lastval2 & 0200)
		    killpg(mypgrp, lastval2 & ~0200);
		if (!list_pipe_child && !lpforked && !subsh && jobbing &&
		    (list_pipe || last1 || pline_level) &&
		    ((jn->stat & STAT_STOPPED) ||
		     (list_pipe_job && pline_level &&
		      (jobtab[list_pipe_job].stat & STAT_STOPPED)))) {
		    pid_t pid = 0;
		    int synch[2];
		    struct timeval bgtime;

		    /*
		     * A pipeline with the shell handling the right
		     * hand side was stopped.  We'll fork to allow
		     * it to continue.
		     */
		    if (pipe(synch) < 0 || (pid = zfork(&bgtime)) == -1) {
			/* Failure */
			if (pid < 0) {
			    close(synch[0]);
			    close(synch[1]);
			} else
			    zerr("pipe failed: %e", errno);
			zleentry(ZLE_CMD_TRASH);
			fprintf(stderr, "zsh: job can't be suspended\n");
			fflush(stderr);
			makerunning(jn);
			killjb(jn, SIGCONT);
			thisjob = newjob;
		    }
		    else if (pid) {
			/*
			 * Parent: job control is here.  If the job
			 * started for the RHS of the pipeline is still
			 * around, then its a SUBJOB and the job for
			 * earlier parts of the pipeeline is its SUPERJOB.
			 * The newly forked shell isn't recorded as a
			 * separate job here, just as list_pipe_pid.
			 * If the superjob exits (it may already have
			 * done so, see child branch below), we'll use
			 * list_pipe_pid to form the basis of a
			 * replacement job --- see SUBLEADER code above.
			 */
			char dummy;

			lpforked =
			    (killpg(jobtab[list_pipe_job].gleader, 0) == -1 ? 2 : 1);
			list_pipe_pid = pid;
			list_pipe_start = bgtime;
			nowait = 1;
			errflag |= ERRFLAG_ERROR;
			breaks = loops;
			close(synch[1]);
			read_loop(synch[0], &dummy, 1);
			close(synch[0]);
			/* If this job has finished, we leave it as a
			 * normal (non-super-) job. */
			if (!(jn->stat & STAT_DONE)) {
			    jobtab[list_pipe_job].other = newjob;
			    jobtab[list_pipe_job].stat |= STAT_SUPERJOB;
			    jn->stat |= STAT_SUBJOB | STAT_NOPRINT;
			    jn->other = list_pipe_pid;	/* see zsh.h */
			    if (hasprocs(list_pipe_job))
				jn->gleader = jobtab[list_pipe_job].gleader;
			}
			if ((list_pipe || last1) && hasprocs(list_pipe_job))
			    killpg(jobtab[list_pipe_job].gleader, SIGSTOP);
			break;
		    }
		    else {
			close(synch[0]);
			entersubsh(ESUB_ASYNC, NULL);
			/*
			 * At this point, we used to attach this process
			 * to the process group of list_pipe_job (the
			 * new superjob) any time that was still available.
			 * That caused problems in at least two
			 * cases because this forked shell was then
			 * suspended with the right hand side of the
			 * pipeline, and the SIGSTOP below suspended
			 * it a second time when it was continued.
			 *
			 * It's therefore not clear entirely why you'd ever
			 * do anything other than the following, but no
			 * doubt we'll find out...
			 */
			setpgrp(0L, mypgrp = getpid());
			close(synch[1]);
			kill(getpid(), SIGSTOP);
			list_pipe = 0;
			list_pipe_child = 1;
			opts[INTERACTIVE] = 0;
			if (errbrk_saved) {
			    /*
			     * Keep any user interrupt bit in errflag.
			     */
			    errflag = prev_errflag | (errflag & ERRFLAG_INT);
			    breaks = prev_breaks;
			}
			break;
		    }
		}
		else if (subsh && jn->stat & STAT_STOPPED) {
		    if (thisjob == newjob)
			makerunning(jn);
		    else
			thisjob = newjob;
		}
		else
		    break;
	    }
	    child_unblock();
	    unqueue_signals();

	    if (list_pipe && (lastval & 0200) && pj >= 0 &&
		(!(jn->stat & STAT_INUSE) || (jn->stat & STAT_DONE))) {
		deletejob(jn, 0);
		jn = jobtab + pj;
		if (jn->gleader)
		    killjb(jn, lastval & ~0200);
	    }
	    if (list_pipe_child ||
		((jn->stat & STAT_DONE) &&
		 (list_pipe || (pline_level && !(jn->stat & STAT_SUBJOB)))))
		deletejob(jn, 0);
	    thisjob = pj;
	}
	else
	    unqueue_signals();
	if ((slflags & WC_SUBLIST_NOT) && !errflag && !retflag)
	    lastval = !lastval;
    }
    if (!pline_level)
	simple_pline = old_simple_pline;
    return lastval;
}

/* execute pipeline.  This function assumes the `pline' is not NULL. */

/**/
static void
execpline2(Estate state, wordcode pcode,
	   int how, int input, int output, int last1)
{
    struct execcmd_params eparams;

    if (breaks || retflag)
	return;

    /* In evaluated traps, don't modify the line number. */
    if (!IN_EVAL_TRAP() && !ineval && WC_PIPE_LINENO(pcode))
	lineno = WC_PIPE_LINENO(pcode) - 1;

    if (pline_level == 1) {
	if ((how & Z_ASYNC) || !sfcontext)
	    strcpy(list_pipe_text,
		   getjobtext(state->prog,
			      state->pc + (WC_PIPE_TYPE(pcode) == WC_PIPE_END ?
					   0 : 1)));
	else
	    list_pipe_text[0] = '\0';
    }
    if (WC_PIPE_TYPE(pcode) == WC_PIPE_END) {
	execcmd_analyse(state, &eparams);
	execcmd_exec(state, &eparams, input, output, how, last1 ? 1 : 2, -1);
    } else {
	int pipes[2];
	int old_list_pipe = list_pipe;
	Wordcode next = state->pc + (*state->pc);

	++state->pc;
	execcmd_analyse(state, &eparams);

	if (mpipe(pipes) < 0) {
	    /* FIXME */
	}

	addfilelist(NULL, pipes[0]);
	execcmd_exec(state, &eparams, input, pipes[1], how, 0, pipes[0]);
	zclose(pipes[1]);
	state->pc = next;

	/* if another execpline() is invoked because the command is *
	 * a list it must know that we're already in a pipeline     */
	cmdpush(CS_PIPE);
	list_pipe = 1;
	execpline2(state, *state->pc++, how, pipes[0], output, last1);
	list_pipe = old_list_pipe;
	cmdpop();
    }
}

/* make the argv array */

/**/
static char **
makecline(LinkList list)
{
    LinkNode node;
    char **argv, **ptr;

    /* A bigger argv is necessary for executing scripts */
    ptr = argv = 2 + (char **) hcalloc((countlinknodes(list) + 4) *
				       sizeof(char *));

    if (isset(XTRACE)) {
	if (!doneps4)
	    printprompt4();

	for (node = firstnode(list); node; incnode(node)) {
	    *ptr++ = (char *)getdata(node);
	    quotedzputs(getdata(node), xtrerr);
	    if (nextnode(node))
		fputc(' ', xtrerr);
	}
	fputc('\n', xtrerr);
	fflush(xtrerr);
    } else {
	for (node = firstnode(list); node; incnode(node))
	    *ptr++ = (char *)getdata(node);
    }
    *ptr = NULL;
    return (argv);
}

/**/
mod_export void
untokenize(char *s)
{
    if (*s) {
	int c;

	while ((c = *s++))
	    if (itok(c)) {
		char *p = s - 1;

		if (c != Nularg)
		    *p++ = ztokens[c - Pound];

		while ((c = *s++)) {
		    if (itok(c)) {
			if (c != Nularg)
			    *p++ = ztokens[c - Pound];
		    } else
			*p++ = c;
		}
		*p = '\0';
		break;
	    }
    }
}


/*
 * Given a tokenized string, output it to standard output in
 * such a way that it's clear which tokens are active.
 * Hence Star becomes an unquoted "*", while a "*" becomes "\*".
 *
 * The code here is a kind of amalgamation of the tests in
 * zshtokenize() and untokenize() with some outputting.
 */

/**/
void
quote_tokenized_output(char *str, FILE *file)
{
    char *s = str;

    for (; *s; s++) {
	switch (*s) {
	case Meta:
	    putc(*++s ^ 32, file);
	    continue;

	case Nularg:
	    /* Do nothing.  I think. */
	    continue;

	case '\\':
	case '<':
	case '>':
	case '(':
	case '|':
	case ')':
	case '^':
	case '#':
	case '~':
	case '[':
	case ']':
	case '*':
	case '?':
	case '$':
	case ' ':
	    putc('\\', file);
	    break;

	case '\t':
	    fputs("$'\\t'", file);
	    continue;

	case '\n':
	    fputs("$'\\n'", file);
	    continue;

	case '\r':
	    fputs("$'\\r'", file);
	    continue;

	case '=':
	    if (s == str)
		putc('\\', file);
	    break;

	default:
	    if (itok(*s)) {
		putc(ztokens[*s - Pound], file);
		continue;
	    }
	    break;
	}

	putc(*s, file);
    }
}

/* Check that we can use a parameter for allocating a file descriptor. */

static int
checkclobberparam(struct redir *f)
{
    struct value vbuf;
    Value v;
    char *s = f->varid;
    int fd;

    if (!s)
	return 1;

    if (!(v = getvalue(&vbuf, &s, 0)))
	return 1;

    if (v->pm->node.flags & PM_READONLY) {
	zwarn("can't allocate file descriptor to readonly parameter %s",
	      f->varid);
	/* don't flag a system error for this */
	errno = 0;
	return 0;
    }

    /*
     * We can't clobber the value in the parameter if it's
     * already an opened file descriptor --- that means it's a decimal
     * integer corresponding to an opened file descriptor,
     * not merely an expression that evaluates to a file descriptor.
     */
    if (!isset(CLOBBER) && (s = getstrvalue(v)) &&
	(fd = (int)zstrtol(s, &s, 10)) >= 0 && !*s &&
	fd <= max_zsh_fd && fdtable[fd] == FDT_EXTERNAL) {
	zwarn("can't clobber parameter %s containing file descriptor %d",
	     f->varid, fd);
	/* don't flag a system error for this */
	errno = 0;
	return 0;
    }
    return 1;
}

/* Open a file for writing redirection */

/**/
static int
clobber_open(struct redir *f)
{
    struct stat buf;
    int fd, oerrno;
    char *ufname = unmeta(f->name);

    /* If clobbering, just open. */
    if (isset(CLOBBER) || IS_CLOBBER_REDIR(f->type))
	return open(ufname,
		O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0666);

    /* If not clobbering, attempt to create file exclusively. */
    if ((fd = open(ufname,
		   O_WRONLY | O_CREAT | O_EXCL | O_NOCTTY, 0666)) >= 0)
	return fd;

    /* If that fails, we are still allowed to open non-regular files. *
     * Try opening, and if it's a regular file then close it again    *
     * because we weren't supposed to open it.                        */
    oerrno = errno;
    if ((fd = open(ufname, O_WRONLY | O_NOCTTY)) != -1) {
	if(!fstat(fd, &buf)) {
	    if (!S_ISREG(buf.st_mode))
		return fd;
	    /*
	     * If CLOBBER_EMPTY is in effect and the file is empty,
	     * we are allowed to re-use it.
	     *
	     * Note: there is an intrinsic race here because another
	     * process can write to this file at any time.  The only fix
	     * would be file locking, which we wish to avoid in basic
	     * file operations at this level.  This would not be
	     * fixed. just additionally complicated, by re-opening the
	     * file and truncating.
	     */
	    if (isset(CLOBBEREMPTY) && buf.st_size == 0)
		return fd;
	}
	close(fd);
    }

    errno = oerrno;
    return -1;
}

/* size of buffer for tee and cat processes */
#define TCBUFSIZE 4092

/* close an multio (success) */

/**/
static void
closemn(struct multio **mfds, int fd, int type)
{
    if (fd >= 0 && mfds[fd] && mfds[fd]->ct >= 2) {
	struct multio *mn = mfds[fd];
	char buf[TCBUFSIZE];
	int len, i;
	pid_t pid;
	struct timeval bgtime;

	/*
	 * We need to block SIGCHLD in case the process
	 * we are spawning terminates before the job table
	 * is set up to handle it.
	 */
	child_block();
	if ((pid = zfork(&bgtime))) {
	    for (i = 0; i < mn->ct; i++)
		zclose(mn->fds[i]);
	    zclose(mn->pipe);
	    if (pid == -1) {
		mfds[fd] = NULL;
		child_unblock();
		return;
	    }
	    mn->ct = 1;
	    mn->fds[0] = fd;
	    addproc(pid, NULL, 1, &bgtime, -1, -1);
	    child_unblock();
	    return;
	}
	/* pid == 0 */
	opts[INTERACTIVE] = 0;
	dont_queue_signals();
	child_unblock();
	closeallelse(mn);
	if (mn->rflag) {
	    /* tee process */
	    while ((len = read(mn->pipe, buf, TCBUFSIZE)) != 0) {
		if (len < 0) {
		    if (errno == EINTR)
			continue;
		    else
			break;
		}
		for (i = 0; i < mn->ct; i++)
		    if (write_loop(mn->fds[i], buf, len) < 0)
			break;
	    }
	} else {
	    /* cat process */
	    for (i = 0; i < mn->ct; i++)
		while ((len = read(mn->fds[i], buf, TCBUFSIZE)) != 0) {
		    if (len < 0) {
			if (errno == EINTR && !isatty(mn->fds[i]))
			    continue;
			else
			    break;
		    }
		    if (write_loop(mn->pipe, buf, len) < 0)
			break;
		}
	}
	_exit(0);
    } else if (fd >= 0 && type == REDIR_CLOSE)
	mfds[fd] = NULL;
}

/* close all the mnodes (failure) */

/**/
static void
closemnodes(struct multio **mfds)
{
    int i, j;

    for (i = 0; i < 10; i++)
	if (mfds[i]) {
	    for (j = 0; j < mfds[i]->ct; j++)
		zclose(mfds[i]->fds[j]);
	    mfds[i] = NULL;
	}
}

/**/
static void
closeallelse(struct multio *mn)
{
    int i, j;
    long openmax;

    openmax = fdtable_size;

    for (i = 0; i < openmax; i++)
	if (mn->pipe != i) {
	    for (j = 0; j < mn->ct; j++)
		if (mn->fds[j] == i)
		    break;
	    if (j == mn->ct)
		zclose(i);
	}
}

/*
 * A multio is a list of fds associated with a certain fd.
 * Thus if you do "foo >bar >ble", the multio for fd 1 will have
 * two fds, the result of open("bar",...), and the result of
 * open("ble",....).
 */

/*
 * Add a fd to an multio.  fd1 must be < 10, and may be in any state.
 * fd2 must be open, and is `consumed' by this function.  Note that
 * fd1 == fd2 is possible, and indicates that fd1 was really closed.
 * We effectively do `fd2 = movefd(fd2)' at the beginning of this
 * function, but in most cases we can avoid an extra dup by delaying
 * the movefd: we only >need< to move it if we're actually doing a
 * multiple redirection.
 *
 * If varid is not NULL, we open an fd above 10 and set the parameter
 * named varid to that value.  fd1 is not used.
 */

/**/
static void
addfd(int forked, int *save, struct multio **mfds, int fd1, int fd2, int rflag,
      char *varid)
{
    int pipes[2];

    if (varid) {
	/* fd will be over 10, don't touch mfds */
	fd1 = movefd(fd2);
	if (fd1 == -1) {
	    zerr("cannot move fd %d: %e", fd2, errno);
	    return;
	} else {
	    fdtable[fd1] = FDT_EXTERNAL;
	    setiparam(varid, (zlong)fd1);
	    /*
	     * If setting the parameter failed, close the fd else
	     * it will leak.
	     */
	    if (errflag)
		zclose(fd1);
	}
    } else if (!mfds[fd1] || unset(MULTIOS)) {
	if(!mfds[fd1]) {		/* starting a new multio */
	    mfds[fd1] = (struct multio *) zhalloc(sizeof(struct multio));
	    if (!forked && save[fd1] == -2) {
		if (fd1 == fd2)
		    save[fd1] = -1;
		else {
		    int fdN = movefd(fd1);
		    /*
		     * fd1 may already be closed here, so
		     * ignore bad file descriptor error
		     */
		    if (fdN < 0) {
			if (errno != EBADF) {
			    zerr("cannot duplicate fd %d: %e", fd1, errno);
			    mfds[fd1] = NULL;
			    closemnodes(mfds);
			    return;
			}
		    } else {
			DPUTS(fdtable[fdN] != FDT_INTERNAL,
			      "Saved file descriptor not marked as internal");
			fdtable[fdN] |= FDT_SAVED_MASK;
		    }
		    save[fd1] = fdN;
		}
	    }
	}
	if (!varid)
	    redup(fd2, fd1);
	mfds[fd1]->ct = 1;
	mfds[fd1]->fds[0] = fd1;
	mfds[fd1]->rflag = rflag;
    } else {
	if (mfds[fd1]->rflag != rflag) {
	    zerr("file mode mismatch on fd %d", fd1);
	    closemnodes(mfds);
	    return;
	}
	if (mfds[fd1]->ct == 1) {	/* split the stream */
	    int fdN = movefd(fd1);
	    if (fdN < 0) {
		zerr("multio failed for fd %d: %e", fd1, errno);
		closemnodes(mfds);
		return;
	    }
	    mfds[fd1]->fds[0] = fdN;
	    fdN = movefd(fd2);
	    if (fdN < 0) {
		zerr("multio failed for fd %d: %e", fd2, errno);
		closemnodes(mfds);
		return;
	    }
	    mfds[fd1]->fds[1] = fdN;
	    if (mpipe(pipes) < 0) {
		zerr("multio failed for fd %d: %e", fd2, errno);
		closemnodes(mfds);
		return;
	    }
	    mfds[fd1]->pipe = pipes[1 - rflag];
	    redup(pipes[rflag], fd1);
	    mfds[fd1]->ct = 2;
	} else {		/* add another fd to an already split stream */
	    int fdN;
	    if(!(mfds[fd1]->ct % MULTIOUNIT)) {
		int new = sizeof(struct multio) + sizeof(int) * mfds[fd1]->ct;
		int old = new - sizeof(int) * MULTIOUNIT;
		mfds[fd1] = hrealloc((char *)mfds[fd1], old, new);
	    }
	    if ((fdN = movefd(fd2)) < 0) {
		zerr("multio failed for fd %d: %e", fd2, errno);
		closemnodes(mfds);
		return;
	    }
	    mfds[fd1]->fds[mfds[fd1]->ct++] = fdN;
	}
    }
}

/**/
static void
addvars(Estate state, Wordcode pc, int addflags)
{
    LinkList vl;
    int xtr, isstr, htok = 0;
    char **arr, **ptr, *name;
    int flags;

    Wordcode opc = state->pc;
    wordcode ac;
    local_list1(svl);

    /*
     * Warn when creating a global without using typeset -g in a
     * function.  Don't do this if there is a list of variables marked
     * to be restored after the command, since then the assignment
     * is implicitly scoped.
     */
    flags = !(addflags & ADDVAR_RESTORE) ? ASSPM_WARN : 0;
    xtr = isset(XTRACE);
    if (xtr) {
	printprompt4();
	doneps4 = 1;
    }
    state->pc = pc;
    while (wc_code(ac = *state->pc++) == WC_ASSIGN) {
	int myflags = flags;
	name = ecgetstr(state, EC_DUPTOK, &htok);
	if (htok)
	    untokenize(name);
	if (WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC)
	    myflags |= ASSPM_AUGMENT;
	if (xtr)
	    fprintf(xtrerr,
		WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC ? "%s+=" : "%s=", name);
	if ((isstr = (WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR))) {
	    init_list1(svl, ecgetstr(state, EC_DUPTOK, &htok));
	    vl = &svl;
	} else {
	    vl = ecgetlist(state, WC_ASSIGN_NUM(ac), EC_DUPTOK, &htok);
	    if (errflag) {
		state->pc = opc;
		return;
	    }
	}

	if (vl && htok) {
	    int prefork_ret = 0;
	    prefork(vl, (isstr ? (PREFORK_SINGLE|PREFORK_ASSIGN) :
			 PREFORK_ASSIGN), &prefork_ret);
	    if (errflag) {
		state->pc = opc;
		return;
	    }
	    if (prefork_ret & PREFORK_KEY_VALUE)
		myflags |= ASSPM_KEY_VALUE;
	    if (!isstr || (isset(GLOBASSIGN) && isstr &&
			   haswilds((char *)getdata(firstnode(vl))))) {
		globlist(vl, prefork_ret);
		/* Unset the parameter to force it to be recreated
		 * as either scalar or array depending on how many
		 * matches were found for the glob.
		 */
		if (isset(GLOBASSIGN) && isstr)
			unsetparam(name);
		if (errflag) {
		    state->pc = opc;
		    return;
		}
	    }
	}
	if (isstr && (empty(vl) || !nextnode(firstnode(vl)))) {
	    Param pm;
	    char *val;
	    int allexp;

	    if (empty(vl))
		val = ztrdup("");
	    else {
		untokenize(peekfirst(vl));
		val = ztrdup(ugetnode(vl));
	    }
	    if (xtr) {
		quotedzputs(val, xtrerr);
		fputc(' ', xtrerr);
	    }
	    if ((addflags & ADDVAR_EXPORT) && !strchr(name, '[')) {
		if ((addflags & ADDVAR_RESTRICT) && isset(RESTRICTED) &&
		    (pm = (Param) paramtab->removenode(paramtab, name)) &&
		    (pm->node.flags & PM_RESTRICTED)) {
		    zerr("%s: restricted", pm->node.nam);
		    zsfree(val);
		    state->pc = opc;
		    return;
		}
		if (strcmp(name, "STTY") == 0) {
		    zsfree(STTYval);
		    STTYval = ztrdup(val);
		}
		allexp = opts[ALLEXPORT];
		opts[ALLEXPORT] = 1;
		if (isset(KSHARRAYS))
		    unsetparam(name);
	    	pm = assignsparam(name, val, myflags);
		opts[ALLEXPORT] = allexp;
	    } else
	    	pm = assignsparam(name, val, myflags);
	    if (!pm) {
		lastval = 1;
		/*
		 * This is cheating but some exec functions propagate
		 * assignment status only from command substitution
		 *
		 * zerr("%s: assignment failed", name);
		 */
		if (!cmdoutval)
		    cmdoutval = 1;
	    }
	    if (errflag) {
		state->pc = opc;
		return;
	    }
	    continue;
	}
	if (vl) {
	    ptr = arr = (char **) zalloc(sizeof(char *) *
					 (countlinknodes(vl) + 1));

	    while (nonempty(vl))
		*ptr++ = ztrdup((char *) ugetnode(vl));
	} else
	    ptr = arr = (char **) zalloc(sizeof(char *));

	*ptr = NULL;
	if (xtr) {
	    fprintf(xtrerr, "( ");
	    for (ptr = arr; *ptr; ptr++) {
		quotedzputs(*ptr, xtrerr);
		fputc(' ', xtrerr);
	    }
	    fprintf(xtrerr, ") ");
	}
	if (!assignaparam(name, arr, myflags)) {
	    lastval = 1;
	    /*
	     * See above RE "cheating"
	     *
	     * zerr("%s: array assignment failed", name);
	     */
	    if (!cmdoutval)
		cmdoutval = 1;
	}
	if (errflag) {
	    state->pc = opc;
	    return;
	}
    }
    state->pc = opc;
}

/**/
void
setunderscore(char *str)
{
    queue_signals();
    if (str && *str) {
	size_t l = strlen(str) + 1, nl = (l + 31) & ~31;

	if (nl > underscorelen || (underscorelen - nl) > 64) {
	    zfree(zunderscore, underscorelen);
	    zunderscore = (char *) zalloc(underscorelen = nl);
	}
	strcpy(zunderscore, str);
	underscoreused = l;
    } else {
	if (underscorelen > 128) {
	    zfree(zunderscore, underscorelen);
	    zunderscore = (char *) zalloc(underscorelen = 32);
	}
	*zunderscore = '\0';
	underscoreused = 1;
    }
    unqueue_signals();
}

/* These describe the type of expansions that need to be done on the words
 * used in the thing we are about to execute. They are set in execcmd() and
 * used in execsubst() which might be called from one of the functions
 * called from execcmd() (like execfor() and so on). */

static int esprefork, esglob = 1;

/**/
void
execsubst(LinkList strs)
{
    if (strs) {
	prefork(strs, esprefork, NULL);
	if (esglob && !errflag) {
	    LinkList ostrs = strs;
	    globlist(strs, 0);
	    strs = ostrs;
	}
    }
}

/*
 * Check if a builtin requires an autoload and if so
 * deal with it.  This may return NULL.
 */

/**/
static HashNode
resolvebuiltin(const char *cmdarg, HashNode hn)
{
    if (!((Builtin) hn)->handlerfunc) {
	char *modname = dupstring(((Builtin) hn)->optstr);
	/*
	 * Ensure the module is loaded and the
	 * feature corresponding to the builtin
	 * is enabled.
	 */
	(void)ensurefeature(modname, "b:",
			    (hn->flags & BINF_AUTOALL) ? NULL :
			    hn->nam);
	hn = builtintab->getnode(builtintab, cmdarg);
	if (!hn) {
	    lastval = 1;
	    zerr("autoloading module %s failed to define builtin: %s",
		 modname, cmdarg);
	    return NULL;
	}
    }
    return hn;
}

/*
 * We are about to execute a command at the lowest level of the
 * hierarchy.  Analyse the parameters from the wordcode.
 */

/**/
static void
execcmd_analyse(Estate state, Execcmd_params eparams)
{
    wordcode code;
    int i;

    eparams->beg = state->pc;
    eparams->redir =
	(wc_code(*state->pc) == WC_REDIR ? ecgetredirs(state) : NULL);
    if (wc_code(*state->pc) == WC_ASSIGN) {
	cmdoutval = 0;
	eparams->varspc = state->pc;
	while (wc_code((code = *state->pc)) == WC_ASSIGN)
	    state->pc += (WC_ASSIGN_TYPE(code) == WC_ASSIGN_SCALAR ?
			  3 : WC_ASSIGN_NUM(code) + 2);
    } else
	eparams->varspc = NULL;

    code = *state->pc++;

    eparams->type = wc_code(code);
    eparams->postassigns = 0;

    /* It would be nice if we could use EC_DUPTOK instead of EC_DUP here.
     * But for that we would need to check/change all builtins so that
     * they don't modify their argument strings. */
    switch (eparams->type) {
    case WC_SIMPLE:
	eparams->args = ecgetlist(state, WC_SIMPLE_ARGC(code), EC_DUP,
				  &eparams->htok);
	eparams->assignspc = NULL;
	break;

    case WC_TYPESET:
	eparams->args = ecgetlist(state, WC_TYPESET_ARGC(code), EC_DUP,
				  &eparams->htok);
	eparams->postassigns = *state->pc++;
	eparams->assignspc = state->pc;
	for (i = 0; i < eparams->postassigns; i++) {
	    code = *state->pc;
	    DPUTS(wc_code(code) != WC_ASSIGN,
		  "BUG: miscounted typeset assignments");
	    state->pc += (WC_ASSIGN_TYPE(code) == WC_ASSIGN_SCALAR ?
			  3 : WC_ASSIGN_NUM(code) + 2);
	}
	break;

    default:
	eparams->args = NULL;
	eparams->assignspc = NULL;
	eparams->htok = 0;
	break;
    }
}

/*
 * Transfer the first node of args to preargs, performing
 * prefork expansion on the way if necessary.
 */
static void execcmd_getargs(LinkList preargs, LinkList args, int expand)
{
    if (!firstnode(args)) {
	return;
    } else if (expand) {
	local_list0(svl);
	init_list0(svl);
	/* not init_list1, as we need real nodes */
	addlinknode(&svl, uremnode(args, firstnode(args)));
	/* Analysing commands, so vanilla options to prefork */
	prefork(&svl, 0, NULL);
	joinlists(preargs, &svl);
    } else {
        addlinknode(preargs, uremnode(args, firstnode(args)));
    }
}

/**/
static int
execcmd_fork(Estate state, int how, int type, Wordcode varspc,
	     LinkList *filelistp, char *text, int oautocont,
	     int close_if_forked)
{
    pid_t pid;
    int synch[2], flags;
    struct entersubsh_ret esret;
    struct timeval bgtime;

    child_block();
    esret.gleader = -1;
    esret.list_pipe_job = -1;

    if (pipe(synch) < 0) {
	zerr("pipe failed: %e", errno);
	return -1;
    } else if ((pid = zfork(&bgtime)) == -1) {
	close(synch[0]);
	close(synch[1]);
	lastval = 1;
	errflag |= ERRFLAG_ERROR;
	return -1;
    }
    if (pid) {
	close(synch[1]);
	read_loop(synch[0], (char *)&esret, sizeof(esret));
	close(synch[0]);
	if (how & Z_ASYNC) {
	    lastpid = (zlong) pid;
	} else if (!jobtab[thisjob].stty_in_env && varspc) {
	    /* search for STTY=... */
	    Wordcode p = varspc;
	    wordcode ac;

	    while (wc_code(ac = *p) == WC_ASSIGN) {
		if (!strcmp(ecrawstr(state->prog, p + 1, NULL), "STTY")) {
		    jobtab[thisjob].stty_in_env = 1;
		    break;
		}
		p += (WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR ?
		      3 : WC_ASSIGN_NUM(ac) + 2);
	    }
	}
	addproc(pid, text, 0, &bgtime, esret.gleader, esret.list_pipe_job);
	if (oautocont >= 0)
	    opts[AUTOCONTINUE] = oautocont;
	pipecleanfilelist(jobtab[thisjob].filelist, 1);
	return pid;
    }

    /* pid == 0 */
    close(synch[0]);
    flags = ((how & Z_ASYNC) ? ESUB_ASYNC : 0) | ESUB_PGRP;
    if ((type != WC_SUBSH) && !(how & Z_ASYNC))
	flags |= ESUB_KEEPTRAP;
    if (type == WC_SUBSH && !(how & Z_ASYNC))
	flags |= ESUB_JOB_CONTROL;
    *filelistp = jobtab[thisjob].filelist;
    entersubsh(flags, &esret);
    if (write_loop(synch[1], (const void *) &esret, sizeof(esret)) != sizeof(esret)) {
	zerr("Failed to send entersubsh_ret report: %e", errno);
	return -1;
    }
    close(synch[1]);
    zclose(close_if_forked);

    if (sigtrapped[SIGINT] & ZSIG_IGNORED)
	holdintr();
    /*
     * EXIT traps shouldn't be called even if we forked to run
     * shell code as this isn't the main shell.
     */
    sigtrapped[SIGEXIT] = 0;
#ifdef HAVE_NICE
    /* Check if we should run background jobs at a lower priority. */
    if ((how & Z_ASYNC) && isset(BGNICE)) {
	errno = 0;
	if (nice(5) == -1 && errno)
	    zwarn("nice(5) failed: %e", errno);
    }
#endif /* HAVE_NICE */

    return 0;
}

/*
 * Execute a command at the lowest level of the hierarchy.
 */

/**/
static void
execcmd_exec(Estate state, Execcmd_params eparams,
	     int input, int output, int how, int last1, int close_if_forked)
{
    HashNode hn = NULL;
    LinkList filelist = NULL;
    LinkNode node;
    Redir fn;
    struct multio *mfds[10];
    char *text;
    int save[10];
    int fil, dfil, is_cursh, do_exec = 0, redir_err = 0, i;
    int nullexec = 0, magic_assign = 0, forked = 0, old_lastval;
    int is_shfunc = 0, is_builtin = 0, is_exec = 0, use_defpath = 0;
    /* Various flags to the command. */
    int cflags = 0, orig_cflags = 0, checked = 0, oautocont = -1;
    FILE *oxtrerr = xtrerr, *newxtrerr = NULL;
    /*
     * Retrieve parameters for quick reference (they are unique
     * to us so we can modify the structure if we want).
     */
    LinkList args = eparams->args;
    LinkList redir = eparams->redir;
    Wordcode varspc = eparams->varspc;
    int type = eparams->type;
    /*
     * preargs comes from expanding the head of the args list
     * in order to check for prefix commands.
     */
    LinkList preargs;

    /*
     * for the "time" keyword
     */
    child_times_t shti, chti;
    struct timeval then;
    if (how & Z_TIMED)
	shelltime(&shti, &chti, &then, 0);

    doneps4 = 0;

    /*
     * If assignment but no command get the status from variable
     * assignment.
     */
    old_lastval = lastval;
    if (!args && varspc)
	lastval = errflag ? errflag : cmdoutval;
    /*
     * If there are arguments, we should reset the status for the
     * command before execution---unless we are using the result of a
     * command substitution, which will be indicated by setting
     * use_cmdoutval to 1.  We haven't kicked those off yet, so
     * there's no race.
     */
    use_cmdoutval = !args;

    for (i = 0; i < 10; i++) {
	save[i] = -2;
	mfds[i] = NULL;
    }

    /* If the command begins with `%', then assume it is a *
     * reference to a job in the job table.                */
    if ((type == WC_SIMPLE || type == WC_TYPESET) && args && nonempty(args) &&
	*(char *)peekfirst(args) == '%') {
        if (how & Z_DISOWN) {
	    oautocont = opts[AUTOCONTINUE];
            opts[AUTOCONTINUE] = 1;
	}
	pushnode(args, dupstring((how & Z_DISOWN)
				 ? "disown" : (how & Z_ASYNC) ? "bg" : "fg"));
	how = Z_SYNC;
    }

    /* If AUTORESUME is set, the command is SIMPLE, and doesn't have *
     * any redirections, then check if it matches as a prefix of a   *
     * job currently in the job table.  If it does, then we treat it *
     * as a command to resume this job.                              */
    if (isset(AUTORESUME) && type == WC_SIMPLE && (how & Z_SYNC) &&
	args && nonempty(args) && (!redir || empty(redir)) && !input &&
	!nextnode(firstnode(args))) {
	if (unset(NOTIFY))
	    scanjobs();
	if (findjobnam(peekfirst(args)) != -1)
	    pushnode(args, dupstring("fg"));
    }

    if ((how & Z_ASYNC) || output ||
	(last1 == 2 && input && EMULATION(EMULATE_SH))) {
	/*
	 * If running in the background, not the last command in a
	 * pipeline, or the last command in a multi-stage pipeline
	 * in sh mode, we don't need any of the rest of this function
	 * to affect the state in the main shell, so fork immediately.
	 *
	 * In other cases we may need to process the command line
	 * a bit further before we make the decision.
	 */
	text = getjobtext(state->prog, eparams->beg);
	switch (execcmd_fork(state, how, type, varspc, &filelist,
			     text, oautocont, close_if_forked)) {
	case -1:
	    goto fatal;
	case 0:
	    break;
	default:
	    return;
	}
	last1 = forked = 1;
    } else
	text = NULL;

    /* Check if it's a builtin needing automatic MAGIC_EQUALS_SUBST      *
     * handling.  Things like typeset need this.  We can't detect the    *
     * command if it contains some tokens (e.g. x=ex; ${x}port), so this *
     * only works in simple cases.  has_token() is called to make sure   *
     * this really is a simple case.                                     */
    if ((type == WC_SIMPLE || type == WC_TYPESET) && args) {
	/*
	 * preargs contains args that have been expanded by prefork.
	 * Running execcmd_getargs() causes any argument available
	 * in args to be exanded where necessary and transferred to
	 * preargs.  We call execcmd_getargs() every time we need to
	 * analyse an argument not available in preargs, though there is
	 * no guarantee a further argument will be available.
	 */
	preargs = newlinklist();
	execcmd_getargs(preargs, args, eparams->htok);
	while (nonempty(preargs)) {
	    char *cmdarg = (char *) peekfirst(preargs);
	    checked = !has_token(cmdarg);
	    if (!checked)
		break;
	    if (type == WC_TYPESET &&
		(hn = builtintab->getnode2(builtintab, cmdarg))) {
		/*
		 * If reserved word for typeset command found (and so
		 * enabled), use regardless of whether builtin is
		 * enabled as we share the implementation.
		 *
		 * Reserved words take precedence over shell functions.
		 */
		checked = 1;
	    } else if (isset(POSIXBUILTINS) && (cflags & BINF_EXEC)) {
		/*
		 * POSIX doesn't allow "exec" to operate on builtins
		 * or shell functions.
		 */
		break;
	    } else {
		if (!(cflags & (BINF_BUILTIN | BINF_COMMAND)) &&
		    (hn = shfunctab->getnode(shfunctab, cmdarg))) {
		    is_shfunc = 1;
		    break;
		}
		if (!(hn = builtintab->getnode(builtintab, cmdarg))) {
		    checked = !(cflags & BINF_BUILTIN);
		    break;
		}
	    }
	    orig_cflags |= cflags;
	    cflags &= ~BINF_BUILTIN & ~BINF_COMMAND;
	    cflags |= hn->flags;
	    if (!(hn->flags & BINF_PREFIX)) {
		is_builtin = 1;

		/* autoload the builtin if necessary */
		if (!(hn = resolvebuiltin(cmdarg, hn))) {
		    if (forked)
			_realexit();
		    if (how & Z_TIMED)
			shelltime(&shti, &chti, &then, 1);
		    return;
		}
		if (type != WC_TYPESET)
		    magic_assign = (hn->flags & BINF_MAGICEQUALS);
		break;
	    }
	    checked = 0;
	    /*
	     * We usually don't need the argument containing the
	     * precommand modifier itself.  Exception: when "command"
	     * will implemented by a call to "whence", in which case
	     * we'll simply re-insert the argument.
	     */
	    uremnode(preargs, firstnode(preargs));
	    if (!firstnode(preargs)) {
		execcmd_getargs(preargs, args, eparams->htok);
		if (!firstnode(preargs))
		    break;
	    }
	    if ((cflags & BINF_COMMAND)) {
		/*
		 * Check for options to "command".
		 * If just -p, this is handled here: use the default
		 * path to execute.
		 * If -v or -V, possibly with -p, dispatch to bin_whence
		 * but with flag to indicate special handling of -p.
		 * Otherwise, just leave marked as BINF_COMMAND
		 * modifier with no additional action.
		 */
		LinkNode argnode, oldnode, pnode = NULL;
		char *argdata, *cmdopt;
		int has_p = 0, has_vV = 0, has_other = 0;
		argnode = firstnode(preargs);
		argdata = (char *) getdata(argnode);
		while (IS_DASH(*argdata)) {
		    /* Just to be definite, stop on single "-", too, */
		    if (!argdata[1] ||
			(IS_DASH(argdata[1]) && !argdata[2]))
			break;
		    for (cmdopt = argdata+1; *cmdopt; cmdopt++) {
			switch (*cmdopt) {
			case 'p':
			    /*
			     * If we've got this multiple times (command
			     * -p -p) we'll treat the second -p as a
			     * command because we only remove one below.
			     * Don't think that's a big issue, and it's
			     * also traditional behaviour.
			     */
			    has_p = 1;
			    pnode = argnode;
			    break;
			case 'v':
			case 'V':
			    has_vV = 1;
			    break;
			default:
			    has_other = 1;
			    break;
			}
		    }
		    if (has_other) {
			/* Don't know how to handle this, so don't */
			has_p = has_vV = 0;
			break;
		    }

		    oldnode = argnode;
		    argnode = nextnode(argnode);
		    if (!argnode) {
			execcmd_getargs(preargs, args, eparams->htok);
			if (!(argnode = nextnode(oldnode)))
			    break;
		    }
		    argdata = (char *) getdata(argnode);
		}
		if (has_vV) {
		    /*
		     * Leave everything alone, dispatch to whence.
		     * We need to put the name back in the list.
		     */
		    pushnode(preargs, "command");
		    hn = &commandbn.node;
		    is_builtin = 1;
		    break;
		} else if (has_p) {
		    /* Use default path */
		    use_defpath = 1;
		    /*
		     * We don't need this node as we're not treating
		     * "command" as a builtin this time.
		     */
		    if (pnode)
			uremnode(preargs, pnode);
		}
		/*
		 * Else just any trailing
		 * end-of-options marker.  This can only occur
		 * if we just had -p or something including more
		 * than just -p, -v and -V, in which case we behave
		 * as if this is command [non-option-stuff].  This
		 * isn't a good place for standard option handling.
		 */
		if (IS_DASH(argdata[0]) && IS_DASH(argdata[1]) && !argdata[2])
		     uremnode(preargs, argnode);
	    } else if (cflags & BINF_EXEC) {
		/*
		 * Check for compatibility options to exec builtin.
		 * It would be nice to do these more generically,
		 * but currently we don't have a mechanism for
		 * precommand modifiers.
		 */
		LinkNode argnode = firstnode(preargs), oldnode;
		char *argdata = (char *) getdata(argnode);
		char *cmdopt, *exec_argv0 = NULL;
		/*
		 * Careful here: we want to make sure a final dash
		 * is passed through in order that it still behaves
		 * as a precommand modifier (zsh equivalent of -l).
		 * It has to be last, but I think that's OK since
		 * people aren't likely to mix the option style
		 * with the zsh style.
		 */
		while (argdata && IS_DASH(*argdata) && strlen(argdata) >= 2) {
		    oldnode = argnode;
		    argnode = nextnode(oldnode);
		    if (!argnode) {
			execcmd_getargs(preargs, args, eparams->htok);
			argnode = nextnode(oldnode);
		    }
		    if (!argnode) {
			zerr("exec requires a command to execute");
			lastval = 1;
			errflag |= ERRFLAG_ERROR;
			goto done;
		    }
		    uremnode(preargs, oldnode);
		    if (IS_DASH(argdata[0]) && IS_DASH(argdata[1]) && !argdata[2])
			break;
		    for (cmdopt = &argdata[1]; *cmdopt; ++cmdopt) {
			switch (*cmdopt) {
			case 'a':
			    /* argument is ARGV0 string */
			    if (cmdopt[1]) {
				exec_argv0 = cmdopt+1;
				/* position on last non-NULL character */
				cmdopt += strlen(cmdopt+1);
			    } else {
				if (!argnode) {
				    zerr("exec requires a command to execute");
				    lastval = 1;
				    errflag |= ERRFLAG_ERROR;
				    goto done;
				}
				if (!nextnode(argnode))
				    execcmd_getargs(preargs, args,
						    eparams->htok);
				if (!nextnode(argnode)) {
				    zerr("exec flag -a requires a parameter");
				    lastval = 1;
				    errflag |= ERRFLAG_ERROR;
				    goto done;
				}
				exec_argv0 = (char *) getdata(argnode);
				oldnode = argnode;
				argnode = nextnode(argnode);
				uremnode(args, oldnode);
			    }
			    break;
			case 'c':
			    cflags |= BINF_CLEARENV;
			    break;
			case 'l':
			    cflags |= BINF_DASH;
			    break;
			default:
			    zerr("unknown exec flag -%c", *cmdopt);
			    lastval = 1;
			    errflag |= ERRFLAG_ERROR;
			    if (forked)
				_realexit();
			    if (how & Z_TIMED)
				shelltime(&shti, &chti, &then, 1);
			    return;
			}
		    }
		    if (!argnode)
			break;
		    argdata = (char *) getdata(argnode);
		}
		if (exec_argv0) {
		    char *str, *s;
		    exec_argv0 = dupstring(exec_argv0);
		    remnulargs(exec_argv0);
		    untokenize(exec_argv0);
		    size_t sz = strlen(exec_argv0);
		    str = s = zalloc(5 + 1 + sz + 1);
		    strcpy(s, "ARGV0=");
		    s+=6;
		    strcpy(s, exec_argv0);
		    zputenv(str);
		}
	    }
	    hn = NULL;
	    if ((cflags & BINF_COMMAND) && unset(POSIXBUILTINS))
		break;
	    if (!nonempty(preargs))
		execcmd_getargs(preargs, args, eparams->htok);
	}
    } else
	preargs = NULL;

    /* Do prefork substitutions.
     *
     * Decide if we need "magic" handling of ~'s etc. in
     * assignment-like arguments.
     * - If magic_assign is set, we are using a builtin of the
     *   tyepset family, but did not recognise this as a keyword,
     *   so need guess-o-matic behaviour.
     * - Otherwise, if we did recognise the keyword, we never need
     *   guess-o-matic behaviour as the argument was properly parsed
     *   as such.
     * - Otherwise, use the behaviour specified by the MAGIC_EQUAL_SUBST
     *   option.
     */
    esprefork = (magic_assign ||
		 (isset(MAGICEQUALSUBST) && type != WC_TYPESET)) ?
		 PREFORK_TYPESET : 0;

    if (args) {
	if (eparams->htok)
	    prefork(args, esprefork, NULL);
	if (preargs)
	    args = joinlists(preargs, args);
    }

    if (type == WC_SIMPLE || type == WC_TYPESET) {
	int unglobbed = 0;

	for (;;) {
	    char *cmdarg;

	    if (!(cflags & BINF_NOGLOB))
		while (!checked && !errflag && args && nonempty(args) &&
		       has_token((char *) peekfirst(args)))
		    zglob(args, firstnode(args), 0);
	    else if (!unglobbed) {
		for (node = firstnode(args); node; incnode(node))
		    untokenize((char *) getdata(node));
		unglobbed = 1;
	    }

	    /* Current shell should not fork unless the *
	     * exec occurs at the end of a pipeline.    */
	    if ((cflags & BINF_EXEC) && last1)
		do_exec = 1;

	    /* Empty command */
	    if (!args || empty(args)) {
		if (redir && nonempty(redir)) {
		    if (do_exec) {
			/* Was this "exec < foobar"? */
			nullexec = 1;
			break;
		    } else if (varspc) {
			nullexec = 2;
			break;
		    } else if (!nullcmd || !*nullcmd || opts[CSHNULLCMD] ||
			       (cflags & BINF_PREFIX)) {
			zerr("redirection with no command");
			lastval = 1;
			errflag |= ERRFLAG_ERROR;
			if (forked)
			    _realexit();
			if (how & Z_TIMED)
			    shelltime(&shti, &chti, &then, 1);
			return;
		    } else if (!nullcmd || !*nullcmd || opts[SHNULLCMD]) {
			if (!args)
			    args = newlinklist();
			addlinknode(args, dupstring(":"));
		    } else if (readnullcmd && *readnullcmd &&
			       ((Redir) peekfirst(redir))->type == REDIR_READ &&
			       !nextnode(firstnode(redir))) {
			if (!args)
			    args = newlinklist();
			addlinknode(args, dupstring(readnullcmd));
		    } else {
			if (!args)
			    args = newlinklist();
			addlinknode(args, dupstring(nullcmd));
		    }
		} else if ((cflags & BINF_PREFIX) && (cflags & BINF_COMMAND)) {
		    lastval = 0;
		    if (forked)
			_realexit();
		    if (how & Z_TIMED)
			shelltime(&shti, &chti, &then, 1);
		    return;
		} else {
		    /*
		     * No arguments.  Reset the status if there were
		     * arguments before and no command substitution
		     * has provided a status.
		     */
		    if (badcshglob == 1) {
			zerr("no match");
			lastval = 1;
			if (forked)
			    _realexit();
			if (how & Z_TIMED)
			    shelltime(&shti, &chti, &then, 1);
			return;
		    }
		    cmdoutval = use_cmdoutval ? lastval : 0;
		    if (varspc) {
			/* Make sure $? is still correct for assignment */
			lastval = old_lastval;
			addvars(state, varspc, 0);
		    }
		    if (errflag)
			lastval = 1;
		    else
			lastval = cmdoutval;
		    if (isset(XTRACE)) {
			fputc('\n', xtrerr);
			fflush(xtrerr);
		    }
		    if (forked)
			_realexit();
		    if (how & Z_TIMED)
			shelltime(&shti, &chti, &then, 1);
		    return;
		}
	    } else if (isset(RESTRICTED) && (cflags & BINF_EXEC) && do_exec) {
		zerrnam("exec", "%s: restricted",
			(char *) getdata(firstnode(args)));
		lastval = 1;
		if (forked)
		    _realexit();
		if (how & Z_TIMED)
		    shelltime(&shti, &chti, &then, 1);
		return;
	    }

	    /*
	     * Quit looking for a command if:
	     * - there was an error; or
	     * - we checked the simple cases needing MAGIC_EQUAL_SUBST; or
	     * - we know we already found a builtin (because either:
	     *   - we loaded a builtin from a module, or
	     *   - we have determined there are options which would
	     *     require us to use the "command" builtin); or
	     * - we aren't using POSIX and so BINF_COMMAND indicates a zsh
	     *   precommand modifier is being used in place of the
	     *   builtin
	     * - we are using POSIX and this is an EXEC, so we can't
	     *   execute a builtin or function.
	     */
	    if (errflag || checked || is_builtin ||
		(isset(POSIXBUILTINS) ?
		 (cflags & BINF_EXEC) : (cflags & BINF_COMMAND)))
		break;

	    cmdarg = (char *) peekfirst(args);
	    if (!(cflags & (BINF_BUILTIN | BINF_COMMAND)) &&
		(hn = shfunctab->getnode(shfunctab, cmdarg))) {
		is_shfunc = 1;
		break;
	    }
	    if (!(hn = builtintab->getnode(builtintab, cmdarg))) {
		if (cflags & BINF_BUILTIN) {
		    zwarn("no such builtin: %s", cmdarg);
		    lastval = 1;
		    if (oautocont >= 0)
			opts[AUTOCONTINUE] = oautocont;
		    if (forked)
			_realexit();
		    if (how & Z_TIMED)
			shelltime(&shti, &chti, &then, 1);
		    return;
		}
		break;
	    }
	    if (!(hn->flags & BINF_PREFIX)) {
		is_builtin = 1;

		/* autoload the builtin if necessary */
		if (!(hn = resolvebuiltin(cmdarg, hn))) {
		    if (forked)
			_realexit();
		    if (how & Z_TIMED)
			shelltime(&shti, &chti, &then, 1);
		    return;
		}
		break;
	    }
	    cflags &= ~BINF_BUILTIN & ~BINF_COMMAND;
	    cflags |= hn->flags;
	    uremnode(args, firstnode(args));
	    hn = NULL;
	}
    }

    if (errflag) {
	if (!lastval)
	    lastval = 1;
	if (oautocont >= 0)
	    opts[AUTOCONTINUE] = oautocont;
	if (forked)
	    _realexit();
	if (how & Z_TIMED)
	    shelltime(&shti, &chti, &then, 1);
	return;
    }

    /* Get the text associated with this command. */
    if (!text &&
	(!sfcontext && (jobbing || (how & Z_TIMED))))
	text = getjobtext(state->prog, eparams->beg);

    /*
     * Set up special parameter $_
     * For execfuncdef we may need to take account of an
     * anonymous function with arguments.
     */
    if (type != WC_FUNCDEF)
	setunderscore((args && nonempty(args)) ?
		      ((char *) getdata(lastnode(args))) : "");

    /* Warn about "rm *" */
    if (type == WC_SIMPLE && interact && unset(RMSTARSILENT) &&
	isset(SHINSTDIN) && args && nonempty(args) &&
	nextnode(firstnode(args)) && !strcmp(peekfirst(args), "rm")) {
	LinkNode node, next;

	for (node = nextnode(firstnode(args)); node && !errflag; node = next) {
	    char *s = (char *) getdata(node);
	    int l = strlen(s);

	    next = nextnode(node);
	    if (s[0] == Star && !s[1]) {
		if (!checkrmall(pwd)) {
		    errflag |= ERRFLAG_ERROR;
		    break;
		}
	    } else if (l >= 2 && s[l - 2] == '/' && s[l - 1] == Star) {
		char t = s[l - 2];
		int rmall;

		s[l - 2] = 0;
		rmall = checkrmall(l == 2 ? "/" : s);
		s[l - 2] = t;

		if (!rmall) {
		    errflag |= ERRFLAG_ERROR;
		    break;
		}
	    }
	}
    }

    if (type == WC_FUNCDEF) {
	/*
	 * The first word of a function definition is a list of
	 * names.  If this is empty, we're doing an anonymous function:
	 * in that case redirections are handled normally.
	 * If not, it's a function definition: then we don't do
	 * redirections here but pass in the list of redirections to
	 * be stored for recall with the function.
	 */
	if (*state->pc != 0) {
	    /* Nonymous, don't do redirections here */
	    redir = NULL;
	}
    } else if (is_shfunc || type == WC_AUTOFN) {
	Shfunc shf;
	if (is_shfunc)
	    shf = (Shfunc)hn;
	else {
	    shf = loadautofn(state->prog->shf, 1, 0, 0);
	    if (shf)
		state->prog->shf = shf;
	    else {
		/*
		 * This doesn't set errflag, so just return now.
		 */
		lastval = 1;
		if (oautocont >= 0)
		    opts[AUTOCONTINUE] = oautocont;
		if (forked)
		    _realexit();
		if (how & Z_TIMED)
		    shelltime(&shti, &chti, &then, 1);
		return;
	    }
	}
	/*
	 * A function definition may have a list of additional
	 * redirections to apply, so retrieve it.
	 */
	if (shf->redir) {
	    struct estate s;
	    LinkList redir2;

	    s.prog = shf->redir;
	    s.pc = shf->redir->prog;
	    s.strs = shf->redir->strs;
	    redir2 = ecgetredirs(&s);
	    if (!redir)
		redir = redir2;
	    else {
		while (nonempty(redir2))
		    addlinknode(redir, ugetnode(redir2));
	    }
	}
    }

    if (errflag) {
	lastval = 1;
	if (oautocont >= 0)
	    opts[AUTOCONTINUE] = oautocont;
	if (forked)
	    _realexit();
	if (how & Z_TIMED)
	    shelltime(&shti, &chti, &then, 1);
	return;
    }

    if ((type == WC_SIMPLE || type == WC_TYPESET) && !nullexec) {
	char *s;
	char trycd = (isset(AUTOCD) && isset(SHINSTDIN) &&
		      (!redir || empty(redir)) && args && !empty(args) &&
		      !nextnode(firstnode(args)) && *(char *)peekfirst(args));

	DPUTS((!args || empty(args)), "BUG: empty(args) in exec.c");
	if (!hn) {
	    /* Resolve external commands */
	    char *cmdarg = (char *) peekfirst(args);
	    char **checkpath = pathchecked;
	    int dohashcmd = isset(HASHCMDS);

	    hn = cmdnamtab->getnode(cmdnamtab, cmdarg);
	    if (hn && trycd && !isreallycom((Cmdnam)hn)) {
		if (!(((Cmdnam)hn)->node.flags & HASHED)) {
		    checkpath = path;
		    dohashcmd = 1;
		}
		cmdnamtab->removenode(cmdnamtab, cmdarg);
		cmdnamtab->freenode(hn);
		hn = NULL;
	    }
	    if (!hn && dohashcmd && strcmp(cmdarg, "..")) {
		for (s = cmdarg; *s && *s != '/'; s++);
		if (!*s)
		    hn = (HashNode) hashcmd(cmdarg, checkpath);
	    }
	}

	/* If no command found yet, see if it  *
	 * is a directory we should AUTOCD to. */
	if (!hn && trycd && (s = cancd(peekfirst(args)))) {
	    peekfirst(args) = (void *) s;
	    pushnode(args, dupstring("--"));
	    pushnode(args, dupstring("cd"));
	    if ((hn = builtintab->getnode(builtintab, "cd")))
		is_builtin = 1;
	}
    }

    /* This is nonzero if the command is a current shell procedure? */
    is_cursh = (is_builtin || is_shfunc || nullexec || type >= WC_CURSH);

    /**************************************************************************
     * Do we need to fork?  We need to fork if:                               *
     * 1) The command is supposed to run in the background.  This             *
     *    case is now handled above (forked = 1 here). (or)                   *
     * 2) There is no `exec' flag, and either:                                *
     *    a) This is a builtin or shell function with output piped somewhere. *
     *    b) This is an external command and we can't do a `fake exec'.       *
     *                                                                        *
     * A `fake exec' is possible if we have all the following conditions:     *
     * 1) last1 flag is 1.  This indicates that the current shell will not    *
     *    be needed after the current command.  This is typically the case    *
     *    when the command is the last stage in a subshell, or is the         *
     *    last command after the option `-c'.                                 *
     * 2) We don't have any traps set.                                        *
     * 3) We don't have any files to delete.                                  *
     *                                                                        *
     * The condition above for a `fake exec' will also work for a current     *
     * shell command such as a builtin, but doesn't really buy us anything    *
     * (doesn't save us a process), since it is already running in the        *
     * current shell.                                                         *
     **************************************************************************/

    if (!forked) {
	if (!do_exec &&
	    (((is_builtin || is_shfunc) && output) ||
	     (!is_cursh && (last1 != 1 || nsigtrapped || havefiles() ||
			    fdtable_flocks)))) {
	    switch (execcmd_fork(state, how, type, varspc, &filelist,
				 text, oautocont, close_if_forked)) {
	    case -1:
		goto fatal;
	    case 0:
		break;
	    default:
		return;
	    }
	    forked = 1;
	} else if (is_cursh) {
	    /* This is a current shell procedure that didn't need to fork.    *
	     * This includes current shell procedures that are being exec'ed, *
	     * as well as null execs.                                         */
	    jobtab[thisjob].stat |= STAT_CURSH;
	    if (!jobtab[thisjob].procs)
		jobtab[thisjob].stat |= STAT_NOPRINT;
	    if (is_builtin)
		jobtab[thisjob].stat |= STAT_BUILTIN;
	} else {
	    /* This is an exec (real or fake) for an external command.    *
	     * Note that any form of exec means that the subshell is fake *
	     * (but we may be in a subshell already).                     */
	    is_exec = 1;
	    /*
	     * If we are in a subshell environment anyway, say we're forked,
	     * even if we're actually not forked because we know the
	     * subshell is exiting.  This ensures SHLVL reflects the current
	     * shell, and also optimises out any save/restore we'd need to
	     * do if we were returning to the main shell.
	     */
	    if (type == WC_SUBSH)
		forked = 1;
	}
    }

    if ((esglob = !(cflags & BINF_NOGLOB)) && args && eparams->htok) {
	LinkList oargs = args;
	globlist(args, 0);
	args = oargs;
    }
    if (errflag) {
	lastval = 1;
	goto err;
    }

    /* Make a copy of stderr for xtrace output before redirecting */
    fflush(xtrerr);
    if (isset(XTRACE) && xtrerr == stderr &&
	(type < WC_SUBSH || type == WC_TIMED)) {
	if ((newxtrerr = fdopen(movefd(dup(fileno(stderr))), "w"))) {
	    xtrerr = newxtrerr;
	    fdtable[fileno(xtrerr)] = FDT_XTRACE;
	}
    }

    /* Add pipeline input/output to mnodes */
    if (input)
	addfd(forked, save, mfds, 0, input, 0, NULL);
    if (output)
	addfd(forked, save, mfds, 1, output, 1, NULL);

    /* Do process substitutions */
    if (redir)
	spawnpipes(redir, nullexec);

    /* Do io redirections */
    while (redir && nonempty(redir)) {
	fn = (Redir) ugetnode(redir);

	DPUTS(fn->type == REDIR_HEREDOC || fn->type == REDIR_HEREDOCDASH,
	      "BUG: unexpanded here document");
	if (fn->type == REDIR_INPIPE) {
	    if (!checkclobberparam(fn) || fn->fd2 == -1) {
		if (fn->fd2 != -1)
		    zclose(fn->fd2);
		closemnodes(mfds);
		fixfds(save);
		execerr();
	    }
	    addfd(forked, save, mfds, fn->fd1, fn->fd2, 0, fn->varid);
	} else if (fn->type == REDIR_OUTPIPE) {
	    if (!checkclobberparam(fn) || fn->fd2 == -1) {
		if (fn->fd2 != -1)
		    zclose(fn->fd2);
		closemnodes(mfds);
		fixfds(save);
		execerr();
	    }
	    addfd(forked, save, mfds, fn->fd1, fn->fd2, 1, fn->varid);
	} else {
	    int closed;
	    if (fn->type != REDIR_HERESTR && xpandredir(fn, redir))
		continue;
	    if (errflag) {
		closemnodes(mfds);
		fixfds(save);
		execerr();
	    }
	    if (isset(RESTRICTED) && IS_WRITE_FILE(fn->type)) {
		zwarn("writing redirection not allowed in restricted mode");
		execerr();
	    }
	    if (unset(EXECOPT))
		continue;
	    switch(fn->type) {
	    case REDIR_HERESTR:
		if (!checkclobberparam(fn))
		    fil = -1;
		else
		    fil = getherestr(fn);
		if (fil == -1) {
		    if (errno && errno != EINTR)
			zwarn("can't create temp file for here document: %e",
			      errno);
		    closemnodes(mfds);
		    fixfds(save);
		    execerr();
		}
		addfd(forked, save, mfds, fn->fd1, fil, 0, fn->varid);
		break;
	    case REDIR_READ:
	    case REDIR_READWRITE:
		if (!checkclobberparam(fn))
		    fil = -1;
		else if (fn->type == REDIR_READ)
		    fil = open(unmeta(fn->name), O_RDONLY | O_NOCTTY);
		else
		    fil = open(unmeta(fn->name),
			       O_RDWR | O_CREAT | O_NOCTTY, 0666);
		if (fil == -1) {
		    closemnodes(mfds);
		    fixfds(save);
		    if (errno != EINTR)
			zwarn("%e: %s", errno, fn->name);
		    execerr();
		}
		addfd(forked, save, mfds, fn->fd1, fil, 0, fn->varid);
		/* If this is 'exec < file', read from stdin, *
		 * not terminal, unless `file' is a terminal. */
		if (nullexec == 1 && fn->fd1 == 0 && !fn->varid &&
		    isset(SHINSTDIN) && interact && !zleactive)
		    init_io(NULL);
		break;
	    case REDIR_CLOSE:
		if (fn->varid) {
		    char *s = fn->varid, *t;
		    struct value vbuf;
		    Value v;
		    int bad = 0;

		    if (!(v = getvalue(&vbuf, &s, 0))) {
			bad = 1;
		    } else if (v->pm->node.flags & PM_READONLY) {
			bad = 2;
		    } else {
			s = getstrvalue(v);
			if (errflag)
			    bad = 1;
			else {
			    fn->fd1 = zstrtol(s, &t, 0);
			    if (s == t)
				bad = 1;
			    else if (*t) {
				/* Check for base#number format */
				if (*t == '#' && *s != '0')
				    fn->fd1 = zstrtol(s = t+1, &t, fn->fd1);
				if (s == t || *t)
				    bad = 1;
			    }
			    if (!bad && fn->fd1 <= max_zsh_fd) {
				if (fn->fd1 >= 10 &&
				    (fdtable[fn->fd1] & FDT_TYPE_MASK) ==
				    FDT_INTERNAL)
				    bad = 3;
			    }
			}
		    }
		    if (bad) {
			const char *bad_msg[] = {
			    "parameter %s does not contain a file descriptor",
			    "can't close file descriptor from readonly parameter %s",
			    "file descriptor %d used by shell, not closed"
			};
			if (bad > 2)
			    zwarn(bad_msg[bad-1], fn->fd1);
			else
			    zwarn(bad_msg[bad-1], fn->varid);
			execerr();
		    }
		}
		/*
		 * Note we may attempt to close an fd beyond max_zsh_fd:
		 * OK as long as we never look in fdtable for it.
 		 */
		closed = 0;
		if (!forked && fn->fd1 < 10 && save[fn->fd1] == -2) {
		    save[fn->fd1] = movefd(fn->fd1);
		    if (save[fn->fd1] >= 0) {
			/*
			 * The original fd is now closed, we don't need
			 * to do it below.
			 */
			closed = 1;
		    }
		}
		if (fn->fd1 < 10)
		    closemn(mfds, fn->fd1, REDIR_CLOSE);
		/*
		 * Only report failures to close file descriptors
		 * if they're under user control as we don't know
		 * what the previous status of others was.
		 */
		if (!closed && zclose(fn->fd1) < 0 && fn->varid) {
		    zwarn("failed to close file descriptor %d: %e",
			  fn->fd1, errno);
		}
		break;
	    case REDIR_MERGEIN:
	    case REDIR_MERGEOUT:
		if (fn->fd2 < 10)
		    closemn(mfds, fn->fd2, fn->type);
		if (!checkclobberparam(fn))
		    fil = -1;
		else if (fn->fd2 > 9 &&
			 /*
			  * If the requested fd is > max_zsh_fd,
			  * the shell doesn't know about it.
			  * Just assume the user knows what they're
			  * doing.
			  */
			 (fn->fd2 <= max_zsh_fd &&
			  ((fdtable[fn->fd2] != FDT_UNUSED &&
			    fdtable[fn->fd2] != FDT_EXTERNAL) ||
			   fn->fd2 == coprocin ||
			   fn->fd2 == coprocout))) {
		    fil = -1;
		    errno = EBADF;
		} else {
		    int fd = fn->fd2;
		    if(fd == -2)
			fd = (fn->type == REDIR_MERGEOUT) ? coprocout : coprocin;
		    fil = movefd(dup(fd));
		}
		if (fil == -1) {
		    char fdstr[DIGBUFSIZE];

		    closemnodes(mfds);
		    fixfds(save);
		    if (fn->fd2 != -2)
		    	sprintf(fdstr, "%d", fn->fd2);
		    if (errno)
			zwarn("%s: %e", fn->fd2 == -2 ? "coprocess" : fdstr,
			      errno);
		    execerr();
		}
		addfd(forked, save, mfds, fn->fd1, fil,
		      fn->type == REDIR_MERGEOUT, fn->varid);
		break;
	    default:
		if (!checkclobberparam(fn))
		    fil = -1;
		else if (IS_APPEND_REDIR(fn->type))
		    fil = open(unmeta(fn->name),
			       ((unset(CLOBBER) && unset(APPENDCREATE)) &&
				!IS_CLOBBER_REDIR(fn->type)) ?
			       O_WRONLY | O_APPEND | O_NOCTTY :
			       O_WRONLY | O_APPEND | O_CREAT | O_NOCTTY, 0666);
		else
		    fil = clobber_open(fn);
		if(fil != -1 && IS_ERROR_REDIR(fn->type))
		    dfil = movefd(dup(fil));
		else
		    dfil = 0;
		if (fil == -1 || dfil == -1) {
		    if(fil != -1)
			close(fil);
		    closemnodes(mfds);
		    fixfds(save);
		    if (errno && errno != EINTR)
			zwarn("%e: %s", errno, fn->name);
		    execerr();
		}
		addfd(forked, save, mfds, fn->fd1, fil, 1, fn->varid);
		if(IS_ERROR_REDIR(fn->type))
		    addfd(forked, save, mfds, 2, dfil, 1, NULL);
		break;
	    }
	    /* May be error in addfd due to setting parameter. */
	    if (errflag) {
		closemnodes(mfds);
		fixfds(save);
		execerr();
	    }
	}
    }

    /* We are done with redirection.  close the mnodes, *
     * spawning tee/cat processes as necessary.         */
    for (i = 0; i < 10; i++)
	if (mfds[i] && mfds[i]->ct >= 2)
	    closemn(mfds, i, REDIR_CLOSE);

    if (nullexec) {
	/*
	 * If nullexec is 2, we have variables to add with the redirections
	 * in place.  If nullexec is 1, we may have variables but they
	 * need the standard restore logic.
	 */
	if (varspc) {
	    LinkList restorelist = 0, removelist = 0;
	    if (!isset(POSIXBUILTINS) && nullexec != 2)
		save_params(state, varspc, &restorelist, &removelist);
	    addvars(state, varspc, 0);
	    if (restorelist)
		restore_params(restorelist, removelist);
	}
	lastval = errflag ? errflag : cmdoutval;
	if (nullexec == 1) {
	    /*
	     * If nullexec is 1 we specifically *don't* restore the original
	     * fd's before returning.
	     */
	    for (i = 0; i < 10; i++)
		if (save[i] != -2)
		    zclose(save[i]);
	    /*
	     * We're done with this job, no need to wait for it.
	     */
	    jobtab[thisjob].stat |= STAT_DONE;
	    goto done;
	}
	if (isset(XTRACE)) {
	    fputc('\n', xtrerr);
	    fflush(xtrerr);
	}
    } else if (isset(EXECOPT) && !errflag) {
	int q = queue_signal_level();
	/*
	 * We delay the entersubsh() to here when we are exec'ing
	 * the current shell (including a fake exec to run a builtin then
	 * exit) in case there is an error return.
	 */
	if (is_exec) {
	    int flags = ((how & Z_ASYNC) ? ESUB_ASYNC : 0) |
		ESUB_PGRP | ESUB_FAKE;
	    if (type != WC_SUBSH)
		flags |= ESUB_KEEPTRAP;
	    if ((do_exec || (type >= WC_CURSH && last1 == 1))
		&& !forked)
		flags |= ESUB_REVERTPGRP;
	    entersubsh(flags, NULL);
	}
	if (type == WC_FUNCDEF) {
	    Eprog redir_prog;
	    if (!redir && wc_code(*eparams->beg) == WC_REDIR)  {
		/*
		 * We're not using a redirection from the currently
		 * parsed environment, which is what we'd do for an
		 * anonymous function, but there are redirections we
		 * should store with the new function.
		 */
		struct estate s;

		s.prog = state->prog;
		s.pc = eparams->beg;
		s.strs = state->prog->strs;

		/*
		 * The copy uses the wordcode parsing area, so save and
		 * restore state.
		 */
		zcontext_save();
		redir_prog = eccopyredirs(&s);
		zcontext_restore();
	    } else
		redir_prog = NULL;

	    dont_queue_signals();
	    lastval = execfuncdef(state, redir_prog);
	    restore_queue_signals(q);
	}
	else if (type >= WC_CURSH) {
	    if (last1 == 1)
		do_exec = 1;
	    dont_queue_signals();
	    if (type == WC_AUTOFN) {
		/*
		 * We pre-loaded this to get any redirs.
		 * So we execute a simplified function here.
		 */
		lastval =  execautofn_basic(state, do_exec);
	    } else
		lastval = (execfuncs[type - WC_CURSH])(state, do_exec);
	    restore_queue_signals(q);
	} else if (is_builtin || is_shfunc) {
	    LinkList restorelist = 0, removelist = 0;
	    int do_save = 0;
	    /* builtin or shell function */

	    if (!forked) {
		if (isset(POSIXBUILTINS)) {
		    /*
		     * If it's a function or special builtin --- save
		     * if it's got "command" in front.
		     * If it's a normal command --- save.
		     */
		    if (is_shfunc || (hn->flags & (BINF_PSPECIAL|BINF_ASSIGN)))
			do_save = (orig_cflags & BINF_COMMAND);
		    else
			do_save = 1;
		} else {
		    /*
		     * Save if it's got "command" in front or it's
		     * not a magic-equals assignment.
		     */
		    if ((cflags & (BINF_COMMAND|BINF_ASSIGN)) || !magic_assign)
			do_save = 1;
		}
		if (do_save && varspc)
		    save_params(state, varspc, &restorelist, &removelist);
	    }
	    if (varspc) {
		/* Export this if the command is a shell function,
		 * but not if it's a builtin.
		 */
		int flags = 0;
		if (is_shfunc)
		    flags |= ADDVAR_EXPORT;
		if (restorelist)
		    flags |= ADDVAR_RESTORE;

		addvars(state, varspc, flags);
		if (errflag) {
		    if (restorelist)
			restore_params(restorelist, removelist);
		    lastval = 1;
		    fixfds(save);
		    goto done;
		}
	    }

	    if (is_shfunc) {
		/* It's a shell function */
		execshfunc((Shfunc) hn, args);
		pipecleanfilelist(filelist, 0);
	    } else {
		/* It's a builtin */
		LinkList assigns = (LinkList)0;
		int postassigns = eparams->postassigns;
		if (forked)
		    closem(FDT_INTERNAL, 0);
		if (postassigns) {
		    Wordcode opc = state->pc;
		    state->pc = eparams->assignspc;
		    assigns = newlinklist();
		    while (postassigns--) {
			int htok;
			wordcode ac = *state->pc++;
			char *name = ecgetstr(state, EC_DUPTOK, &htok);
			Asgment asg;
			local_list1(svl);

			DPUTS(wc_code(ac) != WC_ASSIGN,
			      "BUG: bad assignment list for typeset");
			if (htok) {
			    init_list1(svl, name);
			    if (WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR &&
				WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC) {
				char *data;
				/*
				 * Special case: this is a name only, so
				 * it's not required to be a single
				 * expansion.  Furthermore, for
				 * consistency with the builtin
				 * interface, it may expand into
				 * scalar assignments:
				 *  ass=(one=two three=four)
				 *  typeset a=b $ass
				 */
				/* Unused dummy value for name */
				(void)ecgetstr(state, EC_DUPTOK, &htok);
				prefork(&svl, PREFORK_TYPESET, NULL);
				if (errflag) {
				    state->pc = opc;
				    break;
				}
				globlist(&svl, 0);
				if (errflag) {
				    state->pc = opc;
				    break;
				}
				while ((data = ugetnode(&svl))) {
				    char *ptr;
				    asg = (Asgment)zhalloc(sizeof(struct asgment));
				    asg->flags = 0;
				    if ((ptr = strchr(data, '='))) {
					*ptr++ = '\0';
					asg->name = data;
					asg->value.scalar = ptr;
				    } else {
					asg->name = data;
					asg->value.scalar = NULL;
				    }
				    uaddlinknode(assigns, &asg->node);
				}
				continue;
			    }
			    prefork(&svl, PREFORK_SINGLE, NULL);
			    name = empty(&svl) ? "" :
				(char *)getdata(firstnode(&svl));
			}
			untokenize(name);
			asg = (Asgment)zhalloc(sizeof(struct asgment));
			asg->name = name;
			if (WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR) {
			    char *val = ecgetstr(state, EC_DUPTOK, &htok);
			    asg->flags = 0;
			    if (WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC) {
				/* Fake assignment, no value */
				asg->value.scalar = NULL;
			    } else {
				if (htok) {
				    init_list1(svl, val);
				    prefork(&svl,
					    PREFORK_SINGLE|PREFORK_ASSIGN,
					    NULL);
				    if (errflag) {
					state->pc = opc;
					break;
				    }
				    /*
				     * No globassign for typeset
				     * arguments, thank you
				     */
				    val = empty(&svl) ? "" :
					(char *)getdata(firstnode(&svl));
				}
				untokenize(val);
				asg->value.scalar = val;
			    }
			} else {
			    asg->flags = ASG_ARRAY;
			    asg->value.array =
				ecgetlist(state, WC_ASSIGN_NUM(ac),
					  EC_DUPTOK, &htok);
			    if (asg->value.array)
			    {
				if (!errflag) {
				    int prefork_ret = 0;
				    prefork(asg->value.array, PREFORK_ASSIGN,
					    &prefork_ret);
				    if (errflag) {
					state->pc = opc;
					break;
				    }
				    if (prefork_ret & PREFORK_KEY_VALUE)
					asg->flags |= ASG_KEY_VALUE;
				    globlist(asg->value.array, prefork_ret);
				}
				if (errflag) {
				    state->pc = opc;
				    break;
				}
			    }
			}

			uaddlinknode(assigns, &asg->node);
		    }
		    state->pc = opc;
		}
		dont_queue_signals();
		if (!errflag) {
		    int ret = execbuiltin(args, assigns, (Builtin) hn);
		    /*
		     * In case of interruption assume builtin status
		     * is less useful than what interrupt set.
		     */
		    if (!(errflag & ERRFLAG_INT))
			lastval = ret;
		}
		if (do_save & BINF_COMMAND)
		    errflag &= ~ERRFLAG_ERROR;
		restore_queue_signals(q);
		fflush(stdout);
		if (save[1] == -2) {
		    if (ferror(stdout)) {
			zwarn("write error: %e", errno);
			clearerr(stdout);
		    }
		} else
		    clearerr(stdout);
	    }
	    if (isset(PRINTEXITVALUE) && isset(SHINSTDIN) &&
		lastval && !subsh) {
#if defined(ZLONG_IS_LONG_LONG) && defined(PRINTF_HAS_LLD)
		fprintf(stderr, "zsh: exit %lld\n", lastval);
#else
		fprintf(stderr, "zsh: exit %ld\n", (long)lastval);
#endif
		fflush(stderr);
	    }

	    if (do_exec) {
		if (subsh)
		    _realexit();

		/* If we are exec'ing a command, and we are not in a subshell, *
		 * then check if we should save the history file.              */
		if (isset(RCS) && interact && !nohistsave)
		    savehistfile(NULL, 1, HFILE_USE_OPTIONS);
		realexit();
	    }
	    if (restorelist)
		restore_params(restorelist, removelist);

	} else {
	    if (!subsh) {
	        /* for either implicit or explicit "exec", decrease $SHLVL
		 * as we're now done as a shell */
		if (!forked)
		    setiparam("SHLVL", --shlvl);

		/* If we are exec'ing a command, and we are not *
		 * in a subshell, then save the history file.   */
		if (do_exec && isset(RCS) && interact && !nohistsave)
		    savehistfile(NULL, 1, HFILE_USE_OPTIONS);
	    }
	    if (type == WC_SIMPLE || type == WC_TYPESET) {
		if (varspc) {
		    int addflags = ADDVAR_EXPORT|ADDVAR_RESTRICT;
		    if (forked)
			addflags |= ADDVAR_RESTORE;
		    addvars(state, varspc, addflags);
		    if (errflag)
			_exit(1);
		}
		closem(FDT_INTERNAL, 0);
		if (coprocin != -1) {
		    zclose(coprocin);
		    coprocin = -1;
		}
		if (coprocout != -1) {
		    zclose(coprocout);
		    coprocout = -1;
		}
#ifdef HAVE_GETRLIMIT
		if (!forked)
		    setlimits(NULL);
#endif
		if (how & Z_ASYNC) {
		    zsfree(STTYval);
		    STTYval = 0;
		}
		execute(args, cflags, use_defpath);
	    } else {		/* ( ... ) */
		DPUTS(varspc,
		      "BUG: assignment before complex command");
		list_pipe = 0;
		pipecleanfilelist(filelist, 0);
		/* If we're forked (and we should be), no need to return */
		DPUTS(last1 != 1 && !forked, "BUG: not exiting?");
		DPUTS(type != WC_SUBSH, "Not sure what we're doing.");
		/* Skip word only used for try/always blocks */
		state->pc++;
		execlist(state, 0, 1);
	    }
	}
    }

  err:
    if (forked) {
	/*
	 * So what's going on here then?  Well, I'm glad you asked.
	 *
	 * If we create multios for use in a subshell we do
	 * this after forking, in this function above.  That
	 * means that the current (sub)process is responsible
	 * for clearing them up.  However, the processes won't
	 * go away until we have closed the fd's talking to them.
	 * Since we're about to exit the shell there's nothing
	 * to stop us closing all fd's (including the ones 0 to 9
	 * that we usually leave alone).
	 *
	 * Then we wait for any processes.  When we forked,
	 * we cleared the jobtable and started a new job just for
	 * any oddments like this, so if there aren't any we won't
	 * need to wait.  The result of not waiting is that
	 * the multios haven't flushed the fd's properly, leading
	 * to obscure missing data.
	 *
	 * It would probably be cleaner to ensure that the
	 * parent shell handled multios, but that requires
	 * some architectural changes which are likely to be
	 * hairy.
	 */
	for (i = 0; i < 10; i++)
	    if (fdtable[i] != FDT_UNUSED)
		close(i);
	closem(FDT_UNUSED, 1);
	if (thisjob != -1)
	    waitjobs();
	_realexit();
    }
    fixfds(save);

 done:
    if (isset(POSIXBUILTINS) &&
	(cflags & (BINF_PSPECIAL|BINF_EXEC)) &&
	!(orig_cflags & BINF_COMMAND)) {
	/*
	 * For POSIX-compatible behaviour with special
	 * builtins (including exec which we don't usually
	 * classify as a builtin) we treat all errors as fatal.
	 * The "command" builtin is not special so resets this behaviour.
	 */
	forked |= zsh_subshell;
    fatal:
	if (redir_err || errflag) {
	    if (!isset(INTERACTIVE)) {
		if (forked)
		    _exit(1);
		else
		    exit(1);
	    }
	    errflag |= ERRFLAG_ERROR;
	}
    }
    if ((is_cursh || do_exec) && (how & Z_TIMED))
	shelltime(&shti, &chti, &then, 1);
    if (newxtrerr) {
	int eno = errno;
	fil = fileno(newxtrerr);
	fclose(newxtrerr);
	xtrerr = oxtrerr;
	/* Call zclose() to clean up internal tables, ignore EBADF */
	zclose(fil);
	errno = eno;
    }

    zsfree(STTYval);
    STTYval = 0;
    if (oautocont >= 0)
	opts[AUTOCONTINUE] = oautocont;
}

/* Arrange to have variables restored. */

/**/
static void
save_params(Estate state, Wordcode pc, LinkList *restore_p, LinkList *remove_p)
{
    Param pm;
    char *s;
    wordcode ac;

    *restore_p = newlinklist();
    *remove_p = newlinklist();

    while (wc_code(ac = *pc) == WC_ASSIGN) {
	s = ecrawstr(state->prog, pc + 1, NULL);
	if ((pm = (Param) paramtab->getnode(paramtab, s))) {
	    Param tpm = NULL;
	    if (pm->env)
		delenv(pm);
	    if (!(pm->node.flags & PM_SPECIAL)) {
		/*
		 * We used to remove ordinary parameters from the
		 * table, but that meant "HELLO=$HELLO shellfunc"
		 * failed because the expansion of $HELLO hasn't
		 * been done at this point.  Instead, copy the
		 * parameter:  in this case, we'll insert the
		 * copied parameter straight back into the parameter
		 * table so we want to be sure everything is
		 * properly set up and in permanent memory.
		 */
		tpm = (Param) zshcalloc(sizeof *tpm);
		tpm->node.nam = ztrdup(pm->node.nam);
		copyparam(tpm, pm, 0);
	    } else if (!(pm->node.flags & PM_READONLY) &&
		       (unset(RESTRICTED) || !(pm->node.flags & PM_RESTRICTED))) {
		/*
		 * In this case we're just saving parts of
		 * the parameter in a temporary, so use heap allocation
		 * and don't bother copying every detail.
		 */
		tpm = (Param) hcalloc(sizeof *tpm);
		tpm->node.nam = pm->node.nam;
		copyparam(tpm, pm, 1);
	    }
	    addlinknode(*remove_p, dupstring(s));
	    if (tpm)
		addlinknode(*restore_p, tpm);
	} else
	    addlinknode(*remove_p, dupstring(s));

	pc += (WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR ?
	       3 : WC_ASSIGN_NUM(ac) + 2);
    }
}

/* Restore saved parameters after executing a shfunc or builtin */

/**/
static void
restore_params(LinkList restorelist, LinkList removelist)
{
    Param pm;
    char *s;

    /* remove temporary parameters */
    while ((s = (char *) ugetnode(removelist))) {
	if ((pm = (Param) paramtab->getnode(paramtab, s)) &&
	    !(pm->node.flags & PM_SPECIAL)) {
	    pm->node.flags &= ~PM_READONLY;
	    unsetparam_pm(pm, 0, 0);
	}
    }

    if (restorelist) {
	/* restore saved parameters */
	while ((pm = (Param) ugetnode(restorelist))) {
	    if (pm->node.flags & PM_SPECIAL) {
		Param tpm = (Param) paramtab->getnode(paramtab, pm->node.nam);

		DPUTS(!tpm || PM_TYPE(pm->node.flags) != PM_TYPE(tpm->node.flags) ||
		      !(pm->node.flags & PM_SPECIAL),
		      "BUG: in restoring special parameters");
		if (!pm->env && tpm->env)
		    delenv(tpm);
		tpm->node.flags = pm->node.flags;
		switch (PM_TYPE(pm->node.flags)) {
		case PM_SCALAR:
		    tpm->gsu.s->setfn(tpm, pm->u.str);
		    break;
		case PM_INTEGER:
		    tpm->gsu.i->setfn(tpm, pm->u.val);
		    break;
		case PM_EFLOAT:
		case PM_FFLOAT:
		    tpm->gsu.f->setfn(tpm, pm->u.dval);
		    break;
		case PM_ARRAY:
		    tpm->gsu.a->setfn(tpm, pm->u.arr);
		    break;
		case PM_HASHED:
		    tpm->gsu.h->setfn(tpm, pm->u.hash);
		    break;
		}
		pm = tpm;
	    } else {
		paramtab->addnode(paramtab, pm->node.nam, pm);
	    }
	    if ((pm->node.flags & PM_EXPORTED) && ((s = getsparam(pm->node.nam))))
		addenv(pm, s);
	}
    }
}

/* restore fds after redirecting a builtin */

/**/
static void
fixfds(int *save)
{
    int old_errno = errno;
    int i;

    for (i = 0; i != 10; i++)
	if (save[i] != -2)
	    redup(save[i], i);
    errno = old_errno;
}

/*
 * Close internal shell fds.
 *
 * Close any that are marked as used if "how" is FDT_UNUSED, else
 * close any with the value "how".
 *
 * If "all" is zero, we'll skip cases where we need the file
 * descriptor to be visible externally.
 */

/**/
mod_export void
closem(int how, int all)
{
    int i;

    for (i = 10; i <= max_zsh_fd; i++)
	if (fdtable[i] != FDT_UNUSED &&
	    /*
	     * Process substitution needs to be visible to user;
	     * fd's are explicitly cleaned up by filelist handling.
	     * External FDs are managed directly by the user.
	     */
	    (all || (fdtable[i] != FDT_PROC_SUBST &&
		     fdtable[i] != FDT_EXTERNAL)) &&
	    (how == FDT_UNUSED || (fdtable[i] & FDT_TYPE_MASK) == how)) {
	    if (i == SHTTY)
		SHTTY = -1;
	    zclose(i);
	}
}

/* convert here document into a here string */

/**/
char *
gethere(char **strp, int typ)
{
    char *buf;
    int bsiz, qt = 0, strip = 0;
    char *s, *t, *bptr, c;
    char *str = *strp;

    for (s = str; *s; s++)
	if (inull(*s)) {
	    qt = 1;
	    break;
	}
    str = quotesubst(str);
    untokenize(str);
    if (typ == REDIR_HEREDOCDASH) {
	strip = 1;
	while (*str == '\t')
	    str++;
    }
    *strp = str;
    bptr = buf = zalloc(bsiz = 256);
    for (;;) {
	t = bptr;

	while ((c = hgetc()) == '\t' && strip)
	    ;
	for (;;) {
	    if (bptr >= buf + bsiz - 2) {
		ptrdiff_t toff = t - buf;
		ptrdiff_t bptroff = bptr - buf;
		char *newbuf = realloc(buf, 2 * bsiz);
		if (!newbuf) {
		    /* out of memory */
		    zfree(buf, bsiz);
		    return NULL;
		}
		buf = newbuf;
		t = buf + toff;
		bptr = buf + bptroff;
		bsiz *= 2;
	    }
	    if (lexstop || c == '\n')
		break;
	    if (!qt && c == '\\') {
		*bptr++ = c;
		c = hgetc();
		if (c == '\n') {
		    bptr--;
		    c = hgetc();
		    continue;
		}
	    }
	    *bptr++ = c;
	    c = hgetc();
	}
	*bptr = '\0';
	if (!strcmp(t, str))
	    break;
	if (lexstop) {
	    t = bptr;
	    break;
	}
	*bptr++ = '\n';
    }
    *t = '\0';
    s = buf;
    buf = dupstring(buf);
    zfree(s, bsiz);
    if (!qt) {
	int ef = errflag;

	parsestr(&buf);

	if (!(errflag & ERRFLAG_ERROR)) {
	    /* Retain any user interrupt error */
	    errflag = ef | (errflag & ERRFLAG_INT);
	}
    }
    return buf;
}

/* open here string fd */

/**/
static int
getherestr(struct redir *fn)
{
    char *s, *t;
    int fd, len;

    t = fn->name;
    singsub(&t);
    untokenize(t);
    unmetafy(t, &len);
    /*
     * For real here-strings we append a newline, as if the
     * string given was a complete command line.
     *
     * For here-strings from here documents, we use the original
     * text exactly.
     */
    if (!(fn->flags & REDIRF_FROM_HEREDOC))
	t[len++] = '\n';
    if ((fd = gettempfile(NULL, 1, &s)) < 0)
	return -1;
    write_loop(fd, t, len);
    close(fd);
    fd = open(s, O_RDONLY | O_NOCTTY);
    unlink(s);
    return fd;
}

/*
 * Test if some wordcode starts with a simple redirection of type
 * redir_type.  If it does, return the name of the file, copied onto
 * the heap.  If it doesn't, return NULL.
 */

static char *
simple_redir_name(Eprog prog, int redir_type)
{
    Wordcode pc;

    pc = prog->prog;
    if (prog != &dummy_eprog &&
	wc_code(pc[0]) == WC_LIST && (WC_LIST_TYPE(pc[0]) & Z_END) &&
	wc_code(pc[1]) == WC_SUBLIST && !WC_SUBLIST_FLAGS(pc[1]) &&
	WC_SUBLIST_TYPE(pc[1]) == WC_SUBLIST_END &&
	wc_code(pc[2]) == WC_PIPE && WC_PIPE_TYPE(pc[2]) == WC_PIPE_END &&
	wc_code(pc[3]) == WC_REDIR && WC_REDIR_TYPE(pc[3]) == redir_type &&
	!WC_REDIR_VARID(pc[3]) &&
	!pc[4] &&
	wc_code(pc[6]) == WC_SIMPLE && !WC_SIMPLE_ARGC(pc[6])) {
	return dupstring(ecrawstr(prog, pc + 5, NULL));
    }

    return NULL;
}

/* $(...) */

/**/
LinkList
getoutput(char *cmd, int qt)
{
    Eprog prog;
    int pipes[2];
    pid_t pid;
    char *s;

    int onc = nocomments;
    nocomments = (interact && !sourcelevel && unset(INTERACTIVECOMMENTS));
    prog = parse_string(cmd, 0);
    nocomments = onc;

    if (!prog)
	return NULL;

    if (!isset(EXECOPT))
	return newlinklist();

    if ((s = simple_redir_name(prog, REDIR_READ))) {
	/* $(< word) */
	int stream;
	LinkList retval;
	int readerror;

	singsub(&s);
	if (errflag)
	    return NULL;
	untokenize(s);
	if ((stream = open(unmeta(s), O_RDONLY | O_NOCTTY)) == -1) {
	    zwarn("%e: %s", errno, s);
	    lastval = cmdoutval = 1;
	    return newlinklist();
	}
	retval = readoutput(stream, qt, &readerror);
	if (readerror) {
	  zwarn("error when reading %s: %e", s, readerror);
	  lastval = cmdoutval = 1;
	}
	return retval;
    }
    if (mpipe(pipes) < 0) {
	errflag |= ERRFLAG_ERROR;
	cmdoutpid = 0;
	return NULL;
    }
    child_block();
    cmdoutval = 0;
    if ((cmdoutpid = pid = zfork(NULL)) == -1) {
	/* fork error */
	zclose(pipes[0]);
	zclose(pipes[1]);
	errflag |= ERRFLAG_ERROR;
	cmdoutpid = 0;
	child_unblock();
	return NULL;
    } else if (pid) {
	LinkList retval;

	zclose(pipes[1]);
	retval = readoutput(pipes[0], qt, NULL);
	fdtable[pipes[0]] = FDT_UNUSED;
	waitforpid(pid, 0);		/* unblocks */
	lastval = cmdoutval;
	return retval;
    }
    /* pid == 0 */
    child_unblock();
    zclose(pipes[0]);
    redup(pipes[1], 1);
    entersubsh(ESUB_PGRP|ESUB_NOMONITOR, NULL);
    cmdpush(CS_CMDSUBST);
    execode(prog, 0, 1, "cmdsubst");
    cmdpop();
    close(1);
    _realexit();
    zerr("exit returned in child!!");
    kill(getpid(), SIGKILL);
    return NULL;
}

/* read output of command substitution
 *
 * The file descriptor "in" is closed by the function.
 *
 * "qt" indicates if the substitution was in double quotes.
 *
 * "readerror", if not NULL, is used to return any error that
 * occurred during the read.
 */

/**/
mod_export LinkList
readoutput(int in, int qt, int *readerror)
{
    LinkList ret;
    char *buf, *bufptr, *ptr, inbuf[64];
    int bsiz, c, cnt = 0, readret;
    int q = queue_signal_level();

    ret = newlinklist();
    ptr = buf = (char *) hcalloc(bsiz = 64);
    /*
     * We need to be sensitive to SIGCHLD else we can be
     * stuck forever with important processes unreaped.
     * The case that triggered this was where the exiting
     * process is group leader of the foreground process and we need
     * to reclaim the terminal else ^C doesn't work.
     */
    dont_queue_signals();
    child_unblock();
    for (;;) {
	readret = read(in, inbuf, 64);
	if (readret <= 0) {
	    if (readret < 0 && errno == EINTR)
		continue;
	    else
		break;
	}
	for (bufptr = inbuf; bufptr < inbuf + readret; bufptr++) {
	    c = *bufptr;
	    if (imeta(c)) {
		*ptr++ = Meta;
		c ^= 32;
		cnt++;
	    }
	    if (++cnt >= bsiz) {
		char *pp;
		queue_signals();
		pp = (char *) hcalloc(bsiz *= 2);
		dont_queue_signals();

		memcpy(pp, buf, cnt - 1);
		ptr = (buf = pp) + cnt - 1;
	    }
	    *ptr++ = c;
	}
    }
    child_block();
    restore_queue_signals(q);
    if (readerror)
	*readerror = readret < 0 ? errno : 0;
    close(in);
    while (cnt && ptr[-1] == '\n')
	ptr--, cnt--;
    *ptr = '\0';
    if (qt) {
	if (!cnt) {
	    *ptr++ = Nularg;
	    *ptr = '\0';
	}
	addlinknode(ret, buf);
    } else {
	char **words = spacesplit(buf, 0, 1, 0);

	while (*words) {
	    if (isset(GLOBSUBST))
		shtokenize(*words);
	    addlinknode(ret, *words++);
	}
    }
    return ret;
}

/**/
static Eprog
parsecmd(char *cmd, char **eptr)
{
    char *str;
    Eprog prog;

    for (str = cmd + 2; *str && *str != Outpar; str++);
    if (!*str || cmd[1] != Inpar) {
	/*
	 * This can happen if the expression is being parsed
	 * inside another construct, e.g. as a value within ${..:..} etc.
	 * So print a proper error message instead of the not very
	 * useful but traditional "oops".
 	 */
	char *errstr = dupstrpfx(cmd, 2);
	untokenize(errstr);
	zerr("unterminated `%s...)'", errstr);
	return NULL;
    }
    *str = '\0';
    if (eptr)
	*eptr = str+1;
    if (!(prog = parse_string(cmd + 2, 0))) {
	zerr("parse error in process substitution");
	return NULL;
    }
    return prog;
}

/* =(...) */

/**/
char *
getoutputfile(char *cmd, char **eptr)
{
    pid_t pid;
    char *nam;
    Eprog prog;
    int fd;
    char *s;

    if (thisjob == -1){
	zerr("process substitution %s cannot be used here", cmd);
	return NULL;
    }
    if (!(prog = parsecmd(cmd, eptr)))
	return NULL;
    if (!(nam = gettempname(NULL, 1)))
	return NULL;

    if ((s = simple_redir_name(prog, REDIR_HERESTR))) {
	/*
	 * =(<<<stuff).  Optimise a la $(<file).  It's
	 * effectively the reverse, converting a string into a file name
	 * rather than vice versa.
	 */
	singsub(&s);
	if (errflag)
	    s = NULL;
	else {
	    untokenize(s);
	    s = dyncat(s, "\n");
	}
    }

    if (!s)             /* Unclear why we need to do this before open() */
	child_block();  /* but it has been so for a long time: leave it */

    if ((fd = open(nam, O_WRONLY | O_CREAT | O_EXCL | O_NOCTTY, 0600)) < 0) {
	zerr("process substitution failed: %e", errno);
	if (!s)
	    child_unblock();
	return NULL;
    } else {
	char *suffix = getsparam("TMPSUFFIX");
	if (suffix && *suffix && !strstr(suffix, "/")) {
	    suffix = dyncat(nam, unmeta(suffix));
	    if (link(nam, suffix) == 0) {
		addfilelist(nam, 0);
		nam = suffix;
	    }
	}
    }
    addfilelist(nam, 0);

    if (s) {
	/* optimised here-string */
	int len;
	unmetafy(s, &len);
	write_loop(fd, s, len);
	close(fd);
	return nam;
    }

    if ((cmdoutpid = pid = zfork(NULL)) == -1) {
	/* fork error */
	close(fd);
	child_unblock();
	return nam;
    } else if (pid) {
	close(fd);
	waitforpid(pid, 0);
	cmdoutval = 0;
	return nam;
    }

    /* pid == 0 */
    closem(FDT_UNUSED, 0);
    redup(fd, 1);
    entersubsh(ESUB_PGRP|ESUB_NOMONITOR, NULL);
    cmdpush(CS_CMDSUBST);
    execode(prog, 0, 1, "equalsubst");
    cmdpop();
    close(1);
    _realexit();
    zerr("exit returned in child!!");
    kill(getpid(), SIGKILL);
    return NULL;
}

#if !defined(PATH_DEV_FD) && defined(HAVE_FIFOS)
/* get a temporary named pipe */

static char *
namedpipe(void)
{
    char *tnam = gettempname(NULL, 1);

    if (!tnam) {
	zerr("failed to create named pipe: %e", errno);
	return NULL;
    }
# ifdef HAVE_MKFIFO
    if (mkfifo(tnam, 0600) < 0){
# else
    if (mknod(tnam, 0010600, 0) < 0){
# endif
	zerr("failed to create named pipe: %s, %e", tnam, errno);
	return NULL;
    }
    return tnam;
}
#endif /* ! PATH_DEV_FD && HAVE_FIFOS */

/* <(...) or >(...) */

/**/
char *
getproc(char *cmd, char **eptr)
{
#if !defined(HAVE_FIFOS) && !defined(PATH_DEV_FD)
    zerr("doesn't look like your system supports FIFOs.");
    return NULL;
#else
    Eprog prog;
    int out = *cmd == Inang;
    char *pnam;
    pid_t pid;
    struct timeval bgtime;

#ifndef PATH_DEV_FD
    int fd;
    if (thisjob == -1) {
	zerr("process substitution %s cannot be used here", cmd);
	return NULL;
    }
    if (!(pnam = namedpipe()))
	return NULL;
    if (!(prog = parsecmd(cmd, eptr)))
	return NULL;
    addfilelist(pnam, 0);

    if ((pid = zfork(&bgtime))) {
	if (pid == -1)
	    return NULL;
	if (!out)
	    addproc(pid, NULL, 1, &bgtime, -1, -1);
	procsubstpid = pid;
	return pnam;
    }
    closem(FDT_UNUSED, 0);
    fd = open(pnam, out ? O_WRONLY | O_NOCTTY : O_RDONLY | O_NOCTTY);
    if (fd == -1) {
	zerr("can't open %s: %e", pnam, errno);
	_exit(1);
    }
    entersubsh(ESUB_ASYNC|ESUB_PGRP, NULL);
    redup(fd, out);
#else /* PATH_DEV_FD */
    int pipes[2], fd;

    if (thisjob == -1) {
	zerr("process substitution %s cannot be used here", cmd);
	return NULL;
    }
    pnam = zhalloc(strlen(PATH_DEV_FD) + 1 + DIGBUFSIZE);
    if (!(prog = parsecmd(cmd, eptr)))
	return NULL;
    if (mpipe(pipes) < 0)
	return NULL;
    if ((pid = zfork(&bgtime))) {
	sprintf(pnam, "%s/%d", PATH_DEV_FD, pipes[!out]);
	zclose(pipes[out]);
	if (pid == -1)
	{
	    zclose(pipes[!out]);
	    return NULL;
	}
	fd = pipes[!out];
	fdtable[fd] = FDT_PROC_SUBST;
	addfilelist(NULL, fd);
	if (!out)
	{
	    addproc(pid, NULL, 1, &bgtime, -1, -1);
	}
	procsubstpid = pid;
	return pnam;
    }
    entersubsh(ESUB_ASYNC|ESUB_PGRP, NULL);
    redup(pipes[out], out);
    closem(FDT_UNUSED, 0);   /* this closes pipes[!out] as well */
#endif /* PATH_DEV_FD */

    cmdpush(CS_CMDSUBST);
    execode(prog, 0, 1, out ? "outsubst" : "insubst");
    cmdpop();
    zclose(out);
    _realexit();
    return NULL;
#endif   /* HAVE_FIFOS and PATH_DEV_FD not defined */
}

/*
 * > >(...) or < <(...) (does not use named pipes)
 *
 * If the second argument is 1, this is part of
 * an "exec < <(...)" or "exec > >(...)" and we shouldn't
 * wait for the job to finish before continuing.
 */

/**/
static int
getpipe(char *cmd, int nullexec)
{
    Eprog prog;
    int pipes[2], out = *cmd == Inang;
    pid_t pid;
    struct timeval bgtime;
    char *ends;

    if (!(prog = parsecmd(cmd, &ends)))
	return -1;
    if (*ends) {
	zerr("invalid syntax for process substitution in redirection");
	return -1;
    }
    if (mpipe(pipes) < 0)
	return -1;
    if ((pid = zfork(&bgtime))) {
	zclose(pipes[out]);
	if (pid == -1) {
	    zclose(pipes[!out]);
	    return -1;
	}
	if (!nullexec)
	    addproc(pid, NULL, 1, &bgtime, -1, -1);
	procsubstpid = pid;
	return pipes[!out];
    }
    entersubsh(ESUB_ASYNC|ESUB_PGRP|ESUB_NOMONITOR, NULL);
    redup(pipes[out], out);
    closem(FDT_UNUSED, 0);	/* this closes pipes[!out] as well */
    cmdpush(CS_CMDSUBST);
    execode(prog, 0, 1, out ? "outsubst" : "insubst");
    cmdpop();
    _realexit();
    return 0;
}

/* open pipes with fds >= 10 */

/**/
static int
mpipe(int *pp)
{
    if (pipe(pp) < 0) {
	zerr("pipe failed: %e", errno);
	return -1;
    }
    pp[0] = movefd(pp[0]);
    pp[1] = movefd(pp[1]);
    return 0;
}

/*
 * Do process substitution with redirection
 *
 * If the second argument is 1, this is part of
 * an "exec < <(...)" or "exec > >(...)" and we shouldn't
 * wait for the job to finish before continuing.
 * Likewise, we shouldn't wait if we are opening the file
 * descriptor using the {fd}>>(...) notation since it stays
 * valid for subsequent commands.
 */

/**/
static void
spawnpipes(LinkList l, int nullexec)
{
    LinkNode n;
    Redir f;
    char *str;

    n = firstnode(l);
    for (; n; incnode(n)) {
	f = (Redir) getdata(n);
	if (f->type == REDIR_OUTPIPE || f->type == REDIR_INPIPE) {
	    str = f->name;
	    f->fd2 = getpipe(str, nullexec || f->varid);
	}
    }
}

/* evaluate a [[ ... ]] */

/**/
static int
execcond(Estate state, UNUSED(int do_exec))
{
    int stat;

    state->pc--;
    if (isset(XTRACE)) {
	printprompt4();
	fprintf(xtrerr, "[[");
	tracingcond++;
    }
    cmdpush(CS_COND);
    stat = evalcond(state, NULL);
    /*
     * 2 indicates a syntax error.  For compatibility, turn this
     * into a shell error.
     */
    if (stat == 2)
	errflag |= ERRFLAG_ERROR;
    cmdpop();
    if (isset(XTRACE)) {
	fprintf(xtrerr, " ]]\n");
	fflush(xtrerr);
	tracingcond--;
    }
    return stat;
}

/* evaluate a ((...)) arithmetic command */

/**/
static int
execarith(Estate state, UNUSED(int do_exec))
{
    char *e;
    mnumber val = zero_mnumber;
    int htok = 0;

    if (isset(XTRACE)) {
	printprompt4();
	fprintf(xtrerr, "((");
    }
    cmdpush(CS_MATH);
    e = ecgetstr(state, EC_DUPTOK, &htok);
    if (htok)
	singsub(&e);
    if (isset(XTRACE))
	fprintf(xtrerr, " %s", e);

    val = matheval(e);

    cmdpop();

    if (isset(XTRACE)) {
	fprintf(xtrerr, " ))\n");
	fflush(xtrerr);
    }
    if (errflag) {
	errflag &= ~ERRFLAG_ERROR;
	return 2;
    }
    /* should test for fabs(val.u.d) < epsilon? */
    return (val.type == MN_INTEGER) ? val.u.l == 0 : val.u.d == 0.0;
}

/* perform time ... command */

/**/
static int
exectime(Estate state, UNUSED(int do_exec))
{
    int jb;

    jb = thisjob;
    if (WC_TIMED_TYPE(state->pc[-1]) == WC_TIMED_EMPTY) {
	shelltime(NULL,NULL,NULL,0);
	return 0;
    }
    execpline(state, *state->pc++, Z_TIMED|Z_SYNC, 0);
    thisjob = jb;
    return lastval;
}

/* The string displayed in lieu of the name of an anonymous function (in PS4,
 * zprof output, etc)
 */
static const char *const ANONYMOUS_FUNCTION_NAME = "(anon)";

/* 
 * Take a function name argument and return true iff it is equal to the string
 * used for the names of anonymous functions, "(anon)".
 *
 * Note that it's possible to define a named function literally called "(anon)"
 * (though I doubt anyone would ever do that).
 */
/**/
int is_anonymous_function_name(const char *name)
{
    return !strcmp(name, ANONYMOUS_FUNCTION_NAME);
}

/* Define a shell function */

/**/
static int
execfuncdef(Estate state, Eprog redir_prog)
{
    Shfunc shf;
    char *s = NULL;
    int signum, nprg, sbeg, nstrs, npats, do_tracing, len, plen, i, htok = 0, ret = 0;
    int anon_func = 0;
    Wordcode beg = state->pc, end;
    Eprog prog;
    Patprog *pp;
    LinkList names;
    int tracing_flags;

    end = beg + WC_FUNCDEF_SKIP(state->pc[-1]);
    names = ecgetlist(state, *state->pc++, EC_DUPTOK, &htok);
    sbeg = *state->pc++;
    nstrs = *state->pc++;
    npats = *state->pc++;
    do_tracing = *state->pc++;

    nprg = (end - state->pc);
    plen = nprg * sizeof(wordcode);
    len = plen + (npats * sizeof(Patprog)) + nstrs;
    tracing_flags = do_tracing ? PM_TAGGED_LOCAL : 0;

    if (htok && names) {
	execsubst(names);
	if (errflag) {
	    state->pc = end;
	    return 1;
	}
    }

    DPUTS(!names && redir_prog,
	  "Passing redirection to anon function definition.");
    while (!names || (s = (char *) ugetnode(names))) {
	if (!names) {
	    prog = (Eprog) zhalloc(sizeof(*prog));
	    prog->nref = -1; /* on the heap */
	} else {
	    prog = (Eprog) zalloc(sizeof(*prog));
	    prog->nref = 1; /* allocated from permanent storage */
	}
	prog->npats = npats;
	prog->len = len;
	if (state->prog->dump || !names) {
	    if (!names) {
		prog->flags = EF_HEAP;
		prog->dump = NULL;
		prog->pats = pp = (Patprog *) zhalloc(npats * sizeof(Patprog));
	    } else {
		prog->flags = EF_MAP;
		incrdumpcount(state->prog->dump);
		prog->dump = state->prog->dump;
		prog->pats = pp = (Patprog *) zalloc(npats * sizeof(Patprog));
	    }
	    prog->prog = state->pc;
	    prog->strs = state->strs + sbeg;
	} else {
	    prog->flags = EF_REAL;
	    prog->pats = pp = (Patprog *) zalloc(len);
	    prog->prog = (Wordcode) (prog->pats + npats);
	    prog->strs = (char *) (prog->prog + nprg);
	    prog->dump = NULL;
	    memcpy(prog->prog, state->pc, plen);
	    memcpy(prog->strs, state->strs + sbeg, nstrs);
	}
	for (i = npats; i--; pp++)
	    *pp = dummy_patprog1;
	prog->shf = NULL;

	shf = (Shfunc) zalloc(sizeof(*shf));
	shf->funcdef = prog;
	shf->node.flags = tracing_flags;
	/* No dircache here, not a directory */
	shf->filename = ztrdup(scriptfilename);
	shf->lineno =
	    (funcstack && (funcstack->tp == FS_FUNC ||
			   funcstack->tp == FS_EVAL)) ?
	    funcstack->flineno + lineno :
	    lineno;
	/*
	 * redir_prog is permanently allocated --- but if
	 * this function has multiple names we need an additional
	 * one. Original redir_prog used with the last name
	 * because earlier functions are freed in case of duplicate
	 * names.
	 */
	if (names && nonempty(names) && redir_prog)
	    shf->redir = dupeprog(redir_prog, 0);
	else {
	    shf->redir = redir_prog;
	    redir_prog = 0;
	}
	shfunc_set_sticky(shf);

	if (!names) {
	    /*
	     * Anonymous function, execute immediately.
	     * Function name is "(anon)".
	     */
	    LinkList args;

	    anon_func = 1;
	    shf->node.flags |= PM_ANONYMOUS;

	    state->pc = end;
	    end += *state->pc++;
	    args = ecgetlist(state, *state->pc++, EC_DUPTOK, &htok);

	    if (htok && args) {
		execsubst(args);
		if (errflag) {
		    freeeprog(shf->funcdef);
		    if (shf->redir) /* shouldn't be */
			freeeprog(shf->redir);
		    dircache_set(&shf->filename, NULL);
		    zfree(shf, sizeof(*shf));
		    state->pc = end;
		    return 1;
		}
	    }

	    setunderscore((args && nonempty(args)) ?
			  ((char *) getdata(lastnode(args))) : "");

	    if (!args)
		args = newlinklist();
	    shf->node.nam = (char *) ANONYMOUS_FUNCTION_NAME;
	    pushnode(args, shf->node.nam);

	    execshfunc(shf, args);
	    ret = lastval;

	    if (isset(PRINTEXITVALUE) && isset(SHINSTDIN) &&
		lastval) {
#if defined(ZLONG_IS_LONG_LONG) && defined(PRINTF_HAS_LLD)
		fprintf(stderr, "zsh: exit %lld\n", lastval);
#else
		fprintf(stderr, "zsh: exit %ld\n", (long)lastval);
#endif
		fflush(stderr);
	    }

	    freeeprog(shf->funcdef);
	    if (shf->redir) /* shouldn't be */
		freeeprog(shf->redir);
	    dircache_set(&shf->filename, NULL);
	    zfree(shf, sizeof(*shf));
	    break;
	} else {
	    /* is this shell function a signal trap? */
	    if (!strncmp(s, "TRAP", 4) &&
		(signum = getsigidx(s + 4)) != -1) {
		if (settrap(signum, NULL, ZSIG_FUNC)) {
		    freeeprog(shf->funcdef);
		    dircache_set(&shf->filename, NULL);
		    zfree(shf, sizeof(*shf));
		    state->pc = end;
		    return 1;
		}

		/*
		 * Remove the old node explicitly in case it has
		 * an alternative name
		 */
		removetrapnode(signum);
	    }
	    /* Is this function traced and redefining itself? */
	    if (funcstack && funcstack->tp == FS_FUNC &&
		    !strcmp(s, funcstack->name)) {
		Shfunc old = ((Shfunc)shfunctab->getnode(shfunctab, s));
		if (old)
		    shf->node.flags |= old->node.flags & (PM_TAGGED|PM_TAGGED_LOCAL);
	    }
	    shfunctab->addnode(shfunctab, ztrdup(s), shf);
	}
    }
    if (!anon_func)
	setunderscore("");
    if (redir_prog) {
	/* For completeness, shouldn't happen */
	freeeprog(redir_prog);
    }
    state->pc = end;
    return ret;
}

/* Duplicate a sticky emulation */

/**/

mod_export Emulation_options
sticky_emulation_dup(Emulation_options src, int useheap)
{
    Emulation_options newsticky = useheap ?
	hcalloc(sizeof(*src)) : zshcalloc(sizeof(*src));
    newsticky->emulation = src->emulation;
    if (src->n_on_opts) {
	size_t sz = src->n_on_opts * sizeof(*src->on_opts);
	newsticky->n_on_opts = src->n_on_opts;
	newsticky->on_opts = useheap ? zhalloc(sz) : zalloc(sz);
	memcpy(newsticky->on_opts, src->on_opts, sz);
    }
    if (src->n_off_opts) {
	size_t sz = src->n_off_opts * sizeof(*src->off_opts);
	newsticky->n_off_opts = src->n_off_opts;
	newsticky->off_opts = useheap ? zhalloc(sz) : zalloc(sz);
	memcpy(newsticky->off_opts, src->off_opts, sz);
    }

    return newsticky;
}

/* Set the sticky emulation attributes for a shell function */

/**/

mod_export void
shfunc_set_sticky(Shfunc shf)
{
    if (sticky)
	shf->sticky = sticky_emulation_dup(sticky, 0);
    else
	shf->sticky = NULL;
}


/* Main entry point to execute a shell function. */

/**/
static void
execshfunc(Shfunc shf, LinkList args)
{
    LinkList last_file_list = NULL;
    unsigned char *ocs;
    int ocsp, osfc;

    if (errflag)
	return;

    /* thisjob may be invalid if we're called via execsimple: see execcursh */
    if (!list_pipe && thisjob != -1 && thisjob != list_pipe_job &&
	!hasprocs(thisjob)) {
	/* Without this deletejob the process table *
	 * would be filled by a recursive function. */
	last_file_list = jobtab[thisjob].filelist;
	jobtab[thisjob].filelist = NULL;
	deletejob(jobtab + thisjob, 0);
    }

    if (isset(XTRACE)) {
	LinkNode lptr;
	printprompt4();
	if (args)
	    for (lptr = firstnode(args); lptr; incnode(lptr)) {
		if (lptr != firstnode(args))
		    fputc(' ', xtrerr);
		quotedzputs((char *)getdata(lptr), xtrerr);
	    }
	fputc('\n', xtrerr);
	fflush(xtrerr);
    }
    queue_signals();
    ocs = cmdstack;
    ocsp = cmdsp;
    cmdstack = (unsigned char *) zalloc(CMDSTACKSZ);
    cmdsp = 0;
    if ((osfc = sfcontext) == SFC_NONE)
	sfcontext = SFC_DIRECT;
    xtrerr = stderr;

    doshfunc(shf, args, 0);

    sfcontext = osfc;
    free(cmdstack);
    cmdstack = ocs;
    cmdsp = ocsp;

    if (!list_pipe)
	deletefilelist(last_file_list, 0);
    unqueue_signals();
}

/*
 * Function to execute the special type of command that represents an
 * autoloaded shell function.  The command structure tells us which
 * function it is.  This function is actually called as part of the
 * execution of the autoloaded function itself, so when the function
 * has been autoloaded, its list is just run with no frills.
 *
 * There are two cases because if we are doing all-singing, all-dancing
 * non-simple code we load the shell function early in execcmd() (the
 * action also present in the non-basic version) to check if
 * there are redirections that need to be handled at that point.
 * Then we call execautofn_basic() to do the rest.
 */

/**/
static int
execautofn_basic(Estate state, UNUSED(int do_exec))
{
    Shfunc shf;
    char *oldscriptname, *oldscriptfilename;

    shf = state->prog->shf;

    /*
     * Probably we didn't know the filename where this function was
     * defined yet.
     */
    if (funcstack && !funcstack->filename)
	funcstack->filename = getshfuncfile(shf);

    oldscriptname = scriptname;
    oldscriptfilename = scriptfilename;
    scriptname = dupstring(shf->node.nam);
    scriptfilename = getshfuncfile(shf);
    execode(shf->funcdef, 1, 0, "loadautofunc");
    scriptname = oldscriptname;
    scriptfilename = oldscriptfilename;

    return lastval;
}

/**/
static int
execautofn(Estate state, UNUSED(int do_exec))
{
    Shfunc shf;

    if (!(shf = loadautofn(state->prog->shf, 1, 0, 0)))
	return 1;

    state->prog->shf = shf;
    return execautofn_basic(state, 0);
}

/*
 * Helper function to install the source file name of a shell function
 * just autoloaded.
 *
 * We attempt to do this efficiently as the typical case is the
 * directory part is a well-known directory, which is cached, and
 * the non-directory part is the same as the node name.
 */

/**/
static void
loadautofnsetfile(Shfunc shf, char *fdir)
{
    /*
     * If shf->filename is already the load directory ---
     * keep it as we can still use it to get the load file.
     * This makes autoload with an absolute path particularly efficient.
     */
    if (!(shf->node.flags & PM_LOADDIR) ||
	strcmp(shf->filename, fdir) != 0) {
	/* Old directory name not useful... */
	dircache_set(&shf->filename, NULL);
	if (fdir) {
	    /* ...can still cache directory */
	    shf->node.flags |= PM_LOADDIR;
	    dircache_set(&shf->filename, fdir);
	} else {
	    /* ...no separate directory part to cache, for some reason. */
	    shf->node.flags &= ~PM_LOADDIR;
	    shf->filename = ztrdup(shf->node.nam);
	}
    }
}

/**/
Shfunc
loadautofn(Shfunc shf, int fksh, int autol, int current_fpath)
{
    int noalias = noaliases, ksh = 1;
    Eprog prog;
    char *fdir;			/* Directory path where func found */

    pushheap();

    noaliases = (shf->node.flags & PM_UNALIASED);
    if (shf->filename && shf->filename[0] == '/' &&
	(shf->node.flags & PM_LOADDIR))
    {
	char *spec_path[2];
	spec_path[0] = dupstring(shf->filename);
	spec_path[1] = NULL;
	prog = getfpfunc(shf->node.nam, &ksh, &fdir, spec_path, 0);
	if (prog == &dummy_eprog &&
	    (current_fpath || (shf->node.flags & PM_CUR_FPATH)))
	    prog = getfpfunc(shf->node.nam, &ksh, &fdir, NULL, 0);
    }
    else
	prog = getfpfunc(shf->node.nam, &ksh, &fdir, NULL, 0);
    noaliases = noalias;

    if (ksh == 1) {
	ksh = fksh;
	if (ksh == 1)
	    ksh = (shf->node.flags & PM_KSHSTORED) ? 2 :
		  (shf->node.flags & PM_ZSHSTORED) ? 0 : 1;
    }

    if (prog == &dummy_eprog) {
	/* We're not actually in the function; decrement locallevel */
	locallevel--;
	zwarn("%s: function definition file not found", shf->node.nam);
	locallevel++;
	popheap();
	return NULL;
    }
    if (!prog) {
	popheap();
	return NULL;
    }
    if (ksh == 2 || (ksh == 1 && isset(KSHAUTOLOAD))) {
	if (autol) {
	    prog->flags |= EF_RUN;

	    freeeprog(shf->funcdef);
	    if (prog->flags & EF_MAP)
		shf->funcdef = prog;
	    else
		shf->funcdef = dupeprog(prog, 0);
	    shf->node.flags &= ~PM_UNDEFINED;
	    loadautofnsetfile(shf, fdir);
	} else {
	    VARARR(char, n, strlen(shf->node.nam) + 1);
	    strcpy(n, shf->node.nam);
	    execode(prog, 1, 0, "evalautofunc");
	    shf = (Shfunc) shfunctab->getnode(shfunctab, n);
	    if (!shf || (shf->node.flags & PM_UNDEFINED)) {
		/* We're not actually in the function; decrement locallevel */
		locallevel--;
		zwarn("%s: function not defined by file", n);
		locallevel++;
		popheap();
		return NULL;
	    }
	}
    } else {
	freeeprog(shf->funcdef);
	if (prog->flags & EF_MAP)
	    shf->funcdef = stripkshdef(prog, shf->node.nam);
	else
	    shf->funcdef = dupeprog(stripkshdef(prog, shf->node.nam), 0);
	shf->node.flags &= ~PM_UNDEFINED;
	loadautofnsetfile(shf, fdir);
    }
    popheap();

    return shf;
}

/*
 * Check if a sticky emulation differs from the current one.
 */

/**/

int sticky_emulation_differs(Emulation_options sticky2)
{
    /* If no new sticky emulation, not a different emulation */
    if (!sticky2)
	return 0;
    /* If no current sticky emulation, different */
    if (!sticky)
	return 1;
    /* If basic emulation different, different */
    if (sticky->emulation != sticky2->emulation)
	return 1;
    /* If differing numbers of options, different */
    if (sticky->n_on_opts != sticky2->n_on_opts ||
	sticky->n_off_opts != sticky2->n_off_opts)
	return 1;
    /*
     * We need to compare option arrays, if non-null.
     * We made parseopts() create the list of options in option
     * order to make this easy.
     */
    /* If different options turned on, different */
    if (sticky->n_on_opts &&
	memcmp(sticky->on_opts, sticky2->on_opts,
	       sticky->n_on_opts * sizeof(*sticky->on_opts)) != 0)
	return 1;
    /* If different options turned on, different */
    if (sticky->n_off_opts &&
	memcmp(sticky->off_opts, sticky2->off_opts,
	       sticky->n_off_opts * sizeof(*sticky->off_opts)) != 0)
	return 1;
    return 0;
}

/*
 * execute a shell function
 *
 * name is the name of the function
 *
 * prog is the code to execute
 *
 * doshargs, if set, are parameters to pass to the function,
 * in which the first element is the function name (even if
 * FUNCTIONARGZERO is set as this is handled inside this function).
 *
 * If noreturnval is nonzero, then reset the current return
 * value (lastval) to its value before the shell function
 * was executed.  However, in any case return the status value
 * from the function (i.e. if noreturnval is not set, this
 * will be the same as lastval).
 */

/**/
mod_export int
doshfunc(Shfunc shfunc, LinkList doshargs, int noreturnval)
{
    char **pptab, **x;
    int ret;
    char *name = shfunc->node.nam;
    int flags = shfunc->node.flags;
    char *fname = dupstring(name);
    Eprog prog, marked_prog;
    static int oflags;
    static int funcdepth;
    Heap funcheap;

    queue_signals();	/* Lots of memory and global state changes coming */
    /*
     * In case this is a special function such as a trap, mark it
     * as in use right now, so it doesn't get freed early.  The
     * worst that can happen is this hangs around in memory a little
     * longer than strictly needed.
     *
     * Classic example of this happening is running TRAPEXIT directly.
     *
     * Because the shell function's contents may change, we'll ensure
     * we use a consistent structure for use / free.
     */
    marked_prog = shfunc->funcdef;
    useeprog(marked_prog);

    NEWHEAPS(funcheap) {
	/*
	 * Save data in heap rather than on stack to keep recursive
	 * function cost down --- use of heap memory should be efficient
	 * at this point.  Saving is not actually massive.
	 */
	Funcsave funcsave = zhalloc(sizeof(struct funcsave));
	funcsave->scriptname = scriptname;
	funcsave->argv0 = NULL;
	funcsave->breaks = breaks;
	funcsave->contflag = contflag;
	funcsave->loops = loops;
	funcsave->lastval = lastval;
	funcsave->pipestats = NULL;
	funcsave->numpipestats = numpipestats;
	funcsave->noerrexit = noerrexit;
	if (trap_state == TRAP_STATE_PRIMED)
	    trap_return--;
	/*
	 * Suppression of ERR_RETURN is turned off in function scope.
	 */
	noerrexit &= ~NOERREXIT_RETURN;
	if (noreturnval) {
	    /*
	     * Easiest to use the heap here since we're bracketed
	     * immediately by a pushheap/popheap pair.
	     */
	    size_t bytes = sizeof(int)*numpipestats;
	    funcsave->pipestats = (int *)zhalloc(bytes);
	    memcpy(funcsave->pipestats, pipestats, bytes);
	}

	if (!strcmp(fname, "TRAPEXIT")) {
	    /*
	     * If we are executing TRAPEXIT directly, starttrapscope()
	     * will pull the rug out from under us to ensure the
	     * exit trap isn't run inside the function.  We just need
	     * the information locally here, so copy it on the heap.
	     *
	     * The funcdef is separately handled by reference counting.
	     */
	    Shfunc shcopy = (Shfunc)zhalloc(sizeof(struct shfunc));
	    memcpy(shcopy, shfunc, sizeof(struct shfunc));
	    shcopy->node.nam = dupstring(shfunc->node.nam);
	    shfunc = shcopy;
	    name = shfunc->node.nam;
	}

	starttrapscope();
	startpatternscope();

	pptab = pparams;
	if (!(flags & PM_UNDEFINED))
	    scriptname = dupstring(name);
	funcsave->zoptind = zoptind;
	funcsave->optcind = optcind;
	if (!isset(POSIXBUILTINS)) {
	    zoptind = 1;
	    optcind = 0;
	}

	/* We need to save the current options even if LOCALOPTIONS is *
	 * not currently set.  That's because if it gets set in the    *
	 * function we need to restore the original options on exit.   */
	memcpy(funcsave->opts, opts, sizeof(opts));
	funcsave->emulation = emulation;
	funcsave->sticky = sticky;

	if (sticky_emulation_differs(shfunc->sticky)) {
	    /*
	     * Function is marked for sticky emulation.
	     * Enable it now.
	     *
	     * We deliberately do not do this if the sticky emulation
	     * in effect is the same as that requested.  This enables
	     * option setting naturally within emulation environments.
	     * Note that a difference in EMULATE_FULLY (emulate with
	     * or without -R) counts as a different environment.
	     *
	     * This propagates the sticky emulation to subfunctions.
	     */
	    sticky = sticky_emulation_dup(shfunc->sticky, 1);
	    emulation = sticky->emulation;
	    funcsave->restore_sticky = 1;
	    installemulation(emulation, opts);
	    if (sticky->n_on_opts) {
		OptIndex *onptr;
		for (onptr = sticky->on_opts;
		     onptr < sticky->on_opts + sticky->n_on_opts;
		     onptr++)
		    opts[*onptr] = 1;
	    }
	    if (sticky->n_off_opts) {
		OptIndex *offptr;
		for (offptr = sticky->off_opts;
		     offptr < sticky->off_opts + sticky->n_off_opts;
		     offptr++)
		    opts[*offptr] = 0;
	    }
	    /* All emulations start with pattern disables clear */
	    clearpatterndisables();
	} else
	    funcsave->restore_sticky = 0;

	if (flags & (PM_TAGGED|PM_TAGGED_LOCAL))
	    opts[XTRACE] = 1;
	else if (oflags & PM_TAGGED_LOCAL) {
	    if (shfunc->node.nam == ANONYMOUS_FUNCTION_NAME /* pointer comparison */)
		flags |= PM_TAGGED_LOCAL;
	    else
		opts[XTRACE] = 0;
	}
	if (flags & PM_WARNNESTED)
	    opts[WARNNESTEDVAR] = 1;
	else if (oflags & PM_WARNNESTED) {
	    if (shfunc->node.nam == ANONYMOUS_FUNCTION_NAME)
		flags |= PM_WARNNESTED;
	    else
		opts[WARNNESTEDVAR] = 0;
	}
	funcsave->oflags = oflags;
	/*
	 * oflags is static, because we compare it on the next recursive
	 * call.  Hence also we maintain a saved version for restoring
	 * the previous value of oflags after the call.
	 */
	oflags = flags;
	opts[PRINTEXITVALUE] = 0;
	if (doshargs) {
	    LinkNode node;

	    node = firstnode(doshargs);
	    pparams = x = (char **) zshcalloc(((sizeof *x) *
					       (1 + countlinknodes(doshargs))));
	    if (isset(FUNCTIONARGZERO)) {
		funcsave->argv0 = argzero;
		argzero = ztrdup(getdata(node));
	    }
	    /* first node contains name regardless of option */
	    node = node->next;
	    for (; node; node = node->next, x++)
		*x = ztrdup(getdata(node));
	} else {
	    pparams = (char **) zshcalloc(sizeof *pparams);
	    if (isset(FUNCTIONARGZERO)) {
		funcsave->argv0 = argzero;
		argzero = ztrdup(argzero);
	    }
	}
	++funcdepth;
	if (zsh_funcnest >= 0 && funcdepth > zsh_funcnest) {
	    zerr("maximum nested function level reached; increase FUNCNEST?");
	    lastval = 1;
	    goto undoshfunc;
	}
	funcsave->fstack.name = dupstring(name);
	/*
	 * The caller is whatever is immediately before on the stack,
	 * unless we're at the top, in which case it's the script
	 * or interactive shell name.
	 */
	funcsave->fstack.caller = funcstack ? funcstack->name :
	    dupstring(funcsave->argv0 ? funcsave->argv0 : argzero);
	funcsave->fstack.lineno = lineno;
	funcsave->fstack.prev = funcstack;
	funcsave->fstack.tp = FS_FUNC;
	funcstack = &funcsave->fstack;

	funcsave->fstack.flineno = shfunc->lineno;
	funcsave->fstack.filename = getshfuncfile(shfunc);

	prog = shfunc->funcdef;
	DPUTS1(!prog->nref, "function definition %s has zero reference count",
	       (fname && *fname) ? fname : "<anon>");
	if (prog->flags & EF_RUN) {
	    Shfunc shf;

	    prog->flags &= ~EF_RUN;

	    runshfunc(prog, NULL, funcsave->fstack.name);

	    if (!(shf = (Shfunc) shfunctab->getnode(shfunctab,
						    (name = fname)))) {
		zwarn("%s: function not defined by file", name);
		if (noreturnval)
		    errflag |= ERRFLAG_ERROR;
		else
		    lastval = 1;
		goto doneshfunc;
	    }
	    prog = shf->funcdef;
	}
	runshfunc(prog, wrappers, funcsave->fstack.name);
    doneshfunc:
	funcstack = funcsave->fstack.prev;
    undoshfunc:
	--funcdepth;
	if (retflag) {
	    /*
	     * This function is forced to return.
	     */
	    retflag = 0;
	    breaks = funcsave->breaks;
	}
	freearray(pparams);
	if (funcsave->argv0) {
	    zsfree(argzero);
	    argzero = funcsave->argv0;
	}
	pparams = pptab;
	if (!isset(POSIXBUILTINS)) {
	    zoptind = funcsave->zoptind;
	    optcind = funcsave->optcind;
	}
	scriptname = funcsave->scriptname;
	oflags = funcsave->oflags;

	endpatternscope();	/* before restoring old LOCALPATTERNS */

	if (funcsave->restore_sticky) {
	    /*
	     * If we switched to an emulation environment just for
	     * this function, we interpret the option and emulation
	     * switch as being a firewall between environments.
	     */
	    memcpy(opts, funcsave->opts, sizeof(opts));
	    emulation = funcsave->emulation;
	    sticky = funcsave->sticky;
	} else if (isset(LOCALOPTIONS)) {
	    /* we need to call inittyptab() if these options change */
	    int init_typtab =
#ifdef MULTIBYTE_SUPPORT
			funcsave->opts[MULTIBYTE] != opts[MULTIBYTE] ||
#endif
			funcsave->opts[BANGHIST] != opts[BANGHIST] ||
			funcsave->opts[SHINSTDIN] != opts[SHINSTDIN];
	    /* take care of SUNKEYBOARDHACK but not of EMACS/VI */
	    if (funcsave->opts[SUNKEYBOARDHACK] != opts[SUNKEYBOARDHACK])
		keyboardhackchar = funcsave->opts[SUNKEYBOARDHACK] ? '`' : '\0';
	    /* restore all shell options except PRIVILEGED and RESTRICTED */
	    funcsave->opts[PRIVILEGED] = opts[PRIVILEGED];
	    funcsave->opts[RESTRICTED] = opts[RESTRICTED];
	    memcpy(opts, funcsave->opts, sizeof(opts));
	    emulation = funcsave->emulation;
	    if (init_typtab)
		inittyptab();
	} else {
	    /* just restore a couple. */
	    opts[XTRACE] = funcsave->opts[XTRACE];
	    opts[PRINTEXITVALUE] = funcsave->opts[PRINTEXITVALUE];
	    opts[LOCALOPTIONS] = funcsave->opts[LOCALOPTIONS];
	    opts[LOCALLOOPS] = funcsave->opts[LOCALLOOPS];
	    opts[WARNNESTEDVAR] = funcsave->opts[WARNNESTEDVAR];
	}

	if (opts[LOCALLOOPS]) {
	    if (contflag)
		zwarn("`continue' active at end of function scope");
	    if (breaks)
		zwarn("`break' active at end of function scope");
	    breaks = funcsave->breaks;
	    contflag = funcsave->contflag;
	    loops = funcsave->loops;
	}

	endtrapscope();

	if (trap_state == TRAP_STATE_PRIMED)
	    trap_return++;
	ret = lastval;
	noerrexit = funcsave->noerrexit;
	if (noreturnval) {
	    lastval = funcsave->lastval;
	    numpipestats = funcsave->numpipestats;
	    memcpy(pipestats, funcsave->pipestats, sizeof(int)*numpipestats);
	}
    } OLDHEAPS;

    freeeprog(marked_prog);
    unqueue_signals();

    /*
     * Exit with a tidy up.
     * Only leave if we're at the end of the appropriate function ---
     * not a nested function.  As we usually skip the function body,
     * the only likely case where we need that second test is
     * when we have an "always" block.  The endparamscope() has
     * already happened, hence the "+1" here.
     *
     * If we are in an exit trap, finish it first... we wouldn't set
     * exit_pending if we were already in one.
     */
    if (exit_pending && exit_level >= locallevel+1 && !in_exit_trap) {
	if (locallevel > forklevel) {
	    /* Still functions to return: force them to do so. */
	    retflag = 1;
	    breaks = loops;
	} else {
	    /*
	     * All functions finished: time to exit the shell.
	     * We already did the `stopmsg' test when the
	     * exit command was handled.
	     */
	    stopmsg = 1;
	    zexit(exit_val, ZEXIT_NORMAL);
	}
    }

    return ret;
}

/* This finally executes a shell function and any function wrappers     *
 * defined by modules. This works by calling the wrapper function which *
 * in turn has to call back this function with the arguments it gets.   */

/**/
mod_export void
runshfunc(Eprog prog, FuncWrap wrap, char *name)
{
    int cont, ouu;
    char *ou;

    queue_signals();

    ou = zalloc(ouu = underscoreused);
    if (ou)
	memcpy(ou, zunderscore, underscoreused);

    while (wrap) {
	wrap->module->wrapper++;
	cont = wrap->handler(prog, wrap->next, name);
	wrap->module->wrapper--;

	if (!wrap->module->wrapper &&
	    (wrap->module->node.flags & MOD_UNLOAD))
	    unload_module(wrap->module);

	if (!cont) {
	    if (ou)
		zfree(ou, ouu);
	    unqueue_signals();
	    return;
	}
	wrap = wrap->next;
    }
    startparamscope();
    execode(prog, 1, 0, "shfunc");	/* handles signal unqueueing */
    if (ou) {
	setunderscore(ou);
	zfree(ou, ouu);
    }
    endparamscope();

    unqueue_signals();
}

/*
 * Search fpath for an undefined function.  Finds the file, and returns the
 * list of its contents.
 *
 * If test is 0, load the function.
 *
 * If test_only is 1, don't load function, just test for it:
 * Non-null return means function was found
 *
 * *fdir points to path at which found (as passed in, not duplicated)
 */

/**/
Eprog
getfpfunc(char *s, int *ksh, char **fdir, char **alt_path, int test_only)
{
    char **pp, buf[PATH_MAX+1];
    off_t len;
    off_t rlen;
    char *d;
    Eprog r;
    int fd;

    pp = alt_path ? alt_path : fpath;
    for (; *pp; pp++) {
	if (strlen(*pp) + strlen(s) + 1 >= PATH_MAX)
	    continue;
	if (**pp)
	    sprintf(buf, "%s/%s", *pp, s);
	else
	    strcpy(buf, s);
	if ((r = try_dump_file(*pp, s, buf, ksh, test_only))) {
	    if (fdir)
		*fdir = *pp;
	    return r;
	}
	unmetafy(buf, NULL);
	if (!access(buf, R_OK) && (fd = open(buf, O_RDONLY | O_NOCTTY)) != -1) {
	    struct stat st;
	    if (!fstat(fd, &st) && S_ISREG(st.st_mode) &&
		(len = lseek(fd, 0, 2)) != -1) {
		if (test_only) {
		    close(fd);
		    if (fdir)
			*fdir = *pp;
		    return &dummy_eprog;
		}
		d = (char *) zalloc(len + 1);
		lseek(fd, 0, 0);
		if ((rlen = read(fd, d, len)) >= 0) {
		    char *oldscriptname = scriptname;

		    close(fd);
		    d[rlen] = '\0';
		    d = metafy(d, rlen, META_REALLOC);

		    scriptname = dupstring(s);
		    r = parse_string(d, 1);
		    scriptname = oldscriptname;

		    if (fdir)
			*fdir = *pp;

		    zfree(d, len + 1);

		    return r;
		} else
		    close(fd);

		zfree(d, len + 1);
	    } else
		close(fd);
	}
    }
    return test_only ? NULL : &dummy_eprog;
}

/* Handle the most common type of ksh-style autoloading, when doing a      *
 * zsh-style autoload.  Given the list read from an autoload file, and the *
 * name of the function being defined, check to see if the file consists   *
 * entirely of a single definition for that function.  If so, use the      *
 * contents of that definition.  Otherwise, use the entire file.           */

/**/
Eprog
stripkshdef(Eprog prog, char *name)
{
    Wordcode pc;
    wordcode code;
    char *ptr1, *ptr2;

    if (!prog)
	return NULL;
    pc = prog->prog;
    code = *pc++;
    if (wc_code(code) != WC_LIST ||
	(WC_LIST_TYPE(code) & (Z_SYNC|Z_END|Z_SIMPLE)) != (Z_SYNC|Z_END|Z_SIMPLE))
	return prog;
    pc++;
    code = *pc++;
    if (wc_code(code) != WC_FUNCDEF ||	*pc != 1)
	return prog;

    /*
     * See if name of function requested (name) is same as
     * name of function in word code.  name may still have "-"
     * tokenised.  The word code shouldn't, as function names should be
     * untokenised, but reports say it sometimes does.
     */
    ptr1 = name;
    ptr2 = ecrawstr(prog, pc + 1, NULL);
    while (*ptr1 && *ptr2) {
	if (*ptr1 != *ptr2 && *ptr1 != Dash && *ptr1 != '-' &&
	    *ptr2 != Dash && *ptr2 != '-')
	    break;
	ptr1++;
	ptr2++;
    }
    if (*ptr1 || *ptr2)
	return prog;

    {
	Eprog ret;
	Wordcode end = pc + WC_FUNCDEF_SKIP(code);
	int sbeg = pc[2], nstrs = pc[3], nprg, npats = pc[4], plen, len, i;
	Patprog *pp;

	pc += 6;

	nprg = end - pc;
	plen = nprg * sizeof(wordcode);
	len = plen + (npats * sizeof(Patprog)) + nstrs;

	if (prog->flags & EF_MAP) {
	    ret = prog;
	    free(prog->pats);
	    ret->pats = pp = (Patprog *) zalloc(npats * sizeof(Patprog));
	    ret->prog = pc;
	    ret->strs = prog->strs + sbeg;
	} else {
	    ret = (Eprog) zhalloc(sizeof(*ret));
	    ret->flags = EF_HEAP;
	    ret->pats = pp = (Patprog *) zhalloc(len);
	    ret->prog = (Wordcode) (ret->pats + npats);
	    ret->strs = (char *) (ret->prog + nprg);
	    memcpy(ret->prog, pc, plen);
	    memcpy(ret->strs, prog->strs + sbeg, nstrs);
	    ret->dump = NULL;
	}
	ret->len = len;
	ret->npats = npats;
	for (i = npats; i--; pp++)
	    *pp = dummy_patprog1;
	ret->shf = NULL;

	return ret;
    }
}

/* check to see if AUTOCD applies here */

/**/
static char *
cancd(char *s)
{
    int nocdpath = s[0] == '.' &&
    (s[1] == '/' || !s[1] || (s[1] == '.' && (s[2] == '/' || !s[1])));
    char *t;

    if (*s != '/') {
	char sbuf[PATH_MAX+1], **cp;

	if (cancd2(s))
	    return s;
	if (access(unmeta(s), X_OK) == 0)
	    return NULL;
	if (!nocdpath)
	    for (cp = cdpath; *cp; cp++) {
		if (strlen(*cp) + strlen(s) + 1 >= PATH_MAX)
		    continue;
		if (**cp)
		    sprintf(sbuf, "%s/%s", *cp, s);
		else
		    strcpy(sbuf, s);
		if (cancd2(sbuf)) {
		    doprintdir = -1;
		    return dupstring(sbuf);
		}
	    }
	if ((t = cd_able_vars(s))) {
	    if (cancd2(t)) {
		doprintdir = -1;
		return t;
	    }
	}
	return NULL;
    }
    return cancd2(s) ? s : NULL;
}

/**/
static int
cancd2(char *s)
{
    struct stat buf;
    char *us, *us2 = NULL;
    int ret;

    /*
     * If CHASEDOTS and CHASELINKS are not set, we want to rationalize the
     * path by removing foo/.. combinations in the logical rather than
     * the physical path.  If either is set, we test the physical path.
     */
    if (!isset(CHASEDOTS) && !isset(CHASELINKS)) {
	if (*s != '/')
	    us = tricat(pwd[1] ? pwd : "", "/", s);
	else
	    us = ztrdup(s);
	fixdir(us2 = us);
    } else
	us = unmeta(s);
    ret = !(access(us, X_OK) || stat(us, &buf) || !S_ISDIR(buf.st_mode));
    if (us2)
	free(us2);
    return ret;
}

/**/
void
execsave(void)
{
    struct execstack *es;

    es = (struct execstack *) zalloc(sizeof(struct execstack));
    es->list_pipe_pid = list_pipe_pid;
    es->nowait = nowait;
    es->pline_level = pline_level;
    es->list_pipe_child = list_pipe_child;
    es->list_pipe_job = list_pipe_job;
    strcpy(es->list_pipe_text, list_pipe_text);
    es->lastval = lastval;
    es->noeval = noeval;
    es->badcshglob = badcshglob;
    es->cmdoutpid = cmdoutpid;
    es->cmdoutval = cmdoutval;
    es->use_cmdoutval = use_cmdoutval;
    es->procsubstpid = procsubstpid;
    es->trap_return = trap_return;
    es->trap_state = trap_state;
    es->trapisfunc = trapisfunc;
    es->traplocallevel = traplocallevel;
    es->noerrs = noerrs;
    es->this_noerrexit = this_noerrexit;
    es->underscore = ztrdup(zunderscore);
    es->next = exstack;
    exstack = es;
    noerrs = cmdoutpid = 0;
}

/**/
void
execrestore(void)
{
    struct execstack *en = exstack;

    DPUTS(!exstack, "BUG: execrestore() without execsave()");

    queue_signals();
    exstack = exstack->next;

    list_pipe_pid = en->list_pipe_pid;
    nowait = en->nowait;
    pline_level = en->pline_level;
    list_pipe_child = en->list_pipe_child;
    list_pipe_job = en->list_pipe_job;
    strcpy(list_pipe_text, en->list_pipe_text);
    lastval = en->lastval;
    noeval = en->noeval;
    badcshglob = en->badcshglob;
    cmdoutpid = en->cmdoutpid;
    cmdoutval = en->cmdoutval;
    use_cmdoutval = en->use_cmdoutval;
    procsubstpid = en->procsubstpid;
    trap_return = en->trap_return;
    trap_state = en->trap_state;
    trapisfunc = en->trapisfunc;
    traplocallevel = en->traplocallevel;
    noerrs = en->noerrs;
    this_noerrexit = en->this_noerrexit;
    setunderscore(en->underscore);
    zsfree(en->underscore);
    free(en);

    unqueue_signals();
}
debug log:

solving bc07e8c39 ...
found bc07e8c39 in https://git.vuxu.org/mirror/zsh/

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).