zsh-workers
 help / color / mirror / code / Atom feed
35a0fb2db9a182b4d5fbb9a9cf555f5dc4d3a697 blob 196529 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
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
 
/*
 * builtin.c - builtin commands
 *
 * 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.
 *
 */

/* this is defined so we get the prototype for open_memstream */
#define _GNU_SOURCE 1

#include "zsh.mdh"
#include "builtin.pro"

#include <math.h>

/* Builtins in the main executable */

static struct builtin builtins[] =
{
    BIN_PREFIX("-", BINF_DASH),
    BIN_PREFIX("builtin", BINF_BUILTIN),
    BIN_PREFIX("command", BINF_COMMAND),
    BIN_PREFIX("exec", BINF_EXEC),
    BIN_PREFIX("noglob", BINF_NOGLOB),
    BUILTIN("[", BINF_HANDLES_OPTS, bin_test, 0, -1, BIN_BRACKET, NULL, NULL),
    BUILTIN(".", BINF_PSPECIAL, bin_dot, 1, -1, 0, NULL, NULL),
    BUILTIN(":", BINF_PSPECIAL, bin_true, 0, -1, 0, NULL, NULL),
    BUILTIN("alias", BINF_MAGICEQUALS | BINF_PLUSOPTS, bin_alias, 0, -1, 0, "Lgmrs", NULL),
    BUILTIN("autoload", BINF_PLUSOPTS, bin_functions, 0, -1, 0, "dmktrRTUwWXz", "u"),
    BUILTIN("bg", 0, bin_fg, 0, -1, BIN_BG, NULL, NULL),
    BUILTIN("break", BINF_PSPECIAL, bin_break, 0, 1, BIN_BREAK, NULL, NULL),
    BUILTIN("bye", 0, bin_break, 0, 1, BIN_EXIT, NULL, NULL),
    BUILTIN("cd", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 2, BIN_CD, "qsPL", NULL),
    BUILTIN("chdir", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 2, BIN_CD, "qsPL", NULL),
    BUILTIN("continue", BINF_PSPECIAL, bin_break, 0, 1, BIN_CONTINUE, NULL, NULL),
    BUILTIN("declare", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%klmp:%rtuxz", NULL),
    BUILTIN("dirs", 0, bin_dirs, 0, -1, 0, "clpv", NULL),
    BUILTIN("disable", 0, bin_enable, 0, -1, BIN_DISABLE, "afmprs", NULL),
    BUILTIN("disown", 0, bin_fg, 0, -1, BIN_DISOWN, NULL, NULL),
    BUILTIN("echo", BINF_SKIPINVALID, bin_print, 0, -1, BIN_ECHO, "neE", "-"),
    BUILTIN("emulate", 0, bin_emulate, 0, -1, 0, "lLR", NULL),
    BUILTIN("enable", 0, bin_enable, 0, -1, BIN_ENABLE, "afmprs", NULL),
    BUILTIN("eval", BINF_PSPECIAL, bin_eval, 0, -1, BIN_EVAL, NULL, NULL),
    BUILTIN("exit", BINF_PSPECIAL, bin_break, 0, 1, BIN_EXIT, NULL, NULL),
    BUILTIN("export", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, BIN_EXPORT, "E:%F:%HL:%R:%TUZ:%afhi:%lp:%rtu", "xg"),
    BUILTIN("false", 0, bin_false, 0, -1, 0, NULL, NULL),
    /*
     * We used to behave as if the argument to -e was optional.
     * But that's actually not useful, so it's more consistent to
     * cause an error.
     */
    BUILTIN("fc", 0, bin_fc, 0, -1, BIN_FC, "aAdDe:EfiIlLmnpPrRt:W", NULL),
    BUILTIN("fg", 0, bin_fg, 0, -1, BIN_FG, NULL, NULL),
    BUILTIN("float", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "E:%F:%HL:%R:%Z:%ghlp:%rtux", "E"),
    BUILTIN("functions", BINF_PLUSOPTS, bin_functions, 0, -1, 0, "ckmMstTuUWx:z", NULL),
    BUILTIN("getln", 0, bin_read, 0, -1, 0, "ecnAlE", "zr"),
    BUILTIN("getopts", 0, bin_getopts, 2, -1, 0, NULL, NULL),
    BUILTIN("hash", BINF_MAGICEQUALS, bin_hash, 0, -1, 0, "Ldfmrv", NULL),

#ifdef ZSH_HASH_DEBUG
    BUILTIN("hashinfo", 0, bin_hashinfo, 0, 0, 0, NULL, NULL),
#endif

    BUILTIN("history", 0, bin_fc, 0, -1, BIN_FC, "adDEfiLmnpPrt:", "l"),
    BUILTIN("integer", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "HL:%R:%Z:%ghi:%lp:%rtux", "i"),
    BUILTIN("jobs", 0, bin_fg, 0, -1, BIN_JOBS, "dlpZrs", NULL),
    BUILTIN("kill", BINF_HANDLES_OPTS, bin_kill, 0, -1, 0, NULL, NULL),
    BUILTIN("let", 0, bin_let, 1, -1, 0, NULL, NULL),
    BUILTIN("local", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%ahi:%lp:%rtux", NULL),
    BUILTIN("log", 0, bin_log, 0, 0, 0, NULL, NULL),
    BUILTIN("logout", 0, bin_break, 0, 1, BIN_LOGOUT, NULL, NULL),

#if defined(ZSH_MEM) & defined(ZSH_MEM_DEBUG)
    BUILTIN("mem", 0, bin_mem, 0, 0, 0, "v", NULL),
#endif

#if defined(ZSH_PAT_DEBUG)
    BUILTIN("patdebug", 0, bin_patdebug, 1, -1, 0, "p", NULL),
#endif

    BUILTIN("popd", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 1, BIN_POPD, "q", NULL),
    BUILTIN("print", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, "abcC:Df:ilmnNoOpPrRsSu:v:x:X:z-", NULL),
    BUILTIN("printf", BINF_SKIPINVALID | BINF_SKIPDASH, bin_print, 1, -1, BIN_PRINTF, "v:", NULL),
    BUILTIN("pushd", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 2, BIN_PUSHD, "qsPL", NULL),
    BUILTIN("pushln", 0, bin_print, 0, -1, BIN_PRINT, NULL, "-nz"),
    BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL),
    BUILTIN("r", 0, bin_fc, 0, -1, BIN_R, "IlLnr", NULL),
    BUILTIN("read", 0, bin_read, 0, -1, 0, "cd:ek:%lnpqrst:%zu:AE", NULL),
    BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, BIN_READONLY, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"),
    BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
    BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL),
    BUILTIN("set", BINF_PSPECIAL | BINF_HANDLES_OPTS, bin_set, 0, -1, 0, NULL, NULL),
    BUILTIN("setopt", 0, bin_setopt, 0, -1, BIN_SETOPT, NULL, NULL),
    BUILTIN("shift", BINF_PSPECIAL, bin_shift, 0, -1, 0, "p", NULL),
    BUILTIN("source", BINF_PSPECIAL, bin_dot, 1, -1, 0, NULL, NULL),
    BUILTIN("suspend", 0, bin_suspend, 0, 0, 0, "f", NULL),
    BUILTIN("test", BINF_HANDLES_OPTS, bin_test, 0, -1, BIN_TEST, NULL, NULL),
    BUILTIN("ttyctl", 0, bin_ttyctl, 0, 0, 0, "fu", NULL),
    BUILTIN("times", BINF_PSPECIAL, bin_times, 0, 0, 0, NULL, NULL),
    BUILTIN("trap", BINF_PSPECIAL | BINF_HANDLES_OPTS, bin_trap, 0, -1, 0, NULL, NULL),
    BUILTIN("true", 0, bin_true, 0, -1, 0, NULL, NULL),
    BUILTIN("type", 0, bin_whence, 0, -1, 0, "ampfsSw", "v"),
    BUILTIN("typeset", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%klp:%rtuxmz", NULL),
    BUILTIN("umask", 0, bin_umask, 0, 1, 0, "S", NULL),
    BUILTIN("unalias", 0, bin_unhash, 0, -1, BIN_UNALIAS, "ams", NULL),
    BUILTIN("unfunction", 0, bin_unhash, 1, -1, BIN_UNFUNCTION, "m", "f"),
    BUILTIN("unhash", 0, bin_unhash, 1, -1, BIN_UNHASH, "adfms", NULL),
    BUILTIN("unset", BINF_PSPECIAL, bin_unset, 1, -1, BIN_UNSET, "fmv", NULL),
    BUILTIN("unsetopt", 0, bin_setopt, 0, -1, BIN_UNSETOPT, NULL, NULL),
    BUILTIN("wait", 0, bin_fg, 0, -1, BIN_WAIT, NULL, NULL),
    BUILTIN("whence", 0, bin_whence, 0, -1, 0, "acmpvfsSwx:", NULL),
    BUILTIN("where", 0, bin_whence, 0, -1, 0, "pmsSwx:", "ca"),
    BUILTIN("which", 0, bin_whence, 0, -1, 0, "ampsSwx:", "c"),
    BUILTIN("zmodload", 0, bin_zmodload, 0, -1, 0, "AFRILP:abcfdilmpsue", NULL),
    BUILTIN("zcompile", 0, bin_zcompile, 0, -1, 0, "tUMRcmzka", NULL),
};

/****************************************/
/* Builtin Command Hash Table Functions */
/****************************************/

/* hash table containing builtin commands */

/**/
mod_export HashTable builtintab;

/**/
void
createbuiltintable(void)
{
    builtintab = newhashtable(85, "builtintab", NULL);

    builtintab->hash        = hasher;
    builtintab->emptytable  = NULL;
    builtintab->filltable   = NULL;
    builtintab->cmpnodes    = strcmp;
    builtintab->addnode     = addhashnode;
    builtintab->getnode     = gethashnode;
    builtintab->getnode2    = gethashnode2;
    builtintab->removenode  = removehashnode;
    builtintab->disablenode = disablehashnode;
    builtintab->enablenode  = enablehashnode;
    builtintab->freenode    = freebuiltinnode;
    builtintab->printnode   = printbuiltinnode;

    (void)addbuiltins("zsh", builtins, sizeof(builtins)/sizeof(*builtins));
}

/* Print a builtin */

/**/
static void
printbuiltinnode(HashNode hn, int printflags)
{
    Builtin bn = (Builtin) hn;

    if (printflags & PRINT_WHENCE_WORD) {
	printf("%s: builtin\n", bn->node.nam);
	return;
    }

    if (printflags & PRINT_WHENCE_CSH) {
	printf("%s: shell built-in command\n", bn->node.nam);
	return;
    }

    if (printflags & PRINT_WHENCE_VERBOSE) {
	printf("%s is a shell builtin\n", bn->node.nam);
	return;
    }

    /* default is name only */
    printf("%s\n", bn->node.nam);
}

/**/
static void
freebuiltinnode(HashNode hn)
{
    Builtin bn = (Builtin) hn;

    if(!(bn->node.flags & BINF_ADDED)) {
	zsfree(bn->node.nam);
	zsfree(bn->optstr);
	zfree(bn, sizeof(struct builtin));
    }
}

/**/
void
init_builtins(void)
{
    if (!EMULATION(EMULATE_ZSH)) {
	HashNode hn = reswdtab->getnode2(reswdtab, "repeat");
	if (hn)
	    reswdtab->disablenode(hn, 0);
    }
}

/* Make sure we have space for a new option and increment. */

#define OPT_ALLOC_CHUNK 16

/**/
static int
new_optarg(Options ops)
{
    /* Argument index must be a non-zero 6-bit number. */
    if (ops->argscount == 63)
	return 1;
    if (ops->argsalloc == ops->argscount) {
	char **newptr =
	    (char **)zhalloc((ops->argsalloc + OPT_ALLOC_CHUNK) *
			     sizeof(char *));
	if (ops->argsalloc)
	    memcpy(newptr, ops->args, ops->argsalloc * sizeof(char *));
	ops->args = newptr;
	ops->argsalloc += OPT_ALLOC_CHUNK;
    }
    ops->argscount++;
    return 0;
}


/* execute a builtin handler function after parsing the arguments */

/**/
int
execbuiltin(LinkList args, LinkList assigns, Builtin bn)
{
    char *pp, *name, *optstr;
    int flags, argc, execop, xtr = isset(XTRACE);
    struct options ops;

    /* initialise options structure */
    memset(ops.ind, 0, MAX_OPS*sizeof(unsigned char));
    ops.args = NULL;
    ops.argscount = ops.argsalloc = 0;

    /* initialize some local variables */
    name = (char *) ugetnode(args);

    if (!bn->handlerfunc) {
	DPUTS(1, "Missing builtin detected too late");
	deletebuiltin(bn->node.nam);
	return 1;
    }
    /* get some information about the command */
    flags = bn->node.flags;
    optstr = bn->optstr;

    /* Set up the argument list. */
    /* count the arguments */
    argc = countlinknodes(args);

    {
	/*
	 * Keep all arguments, including options, in an array.
	 * We don't actually need the option part of the argument
	 * after option processing, but it makes XTRACE output
	 * much simpler.
	 */
	VARARR(char *, argarr, argc + 1);
	char **argv;

	/*
	 * Get the actual arguments, into argv.  Remember argarr
	 * may be an array declaration, depending on the compiler.
	 */
	argv = argarr;
	while ((*argv++ = (char *)ugetnode(args)));
	argv = argarr;

	/* Sort out the options. */
	if (optstr) {
	    char *arg = *argv;
	    int sense; /* 1 for -x, 0 for +x */
	    /* while arguments look like options ... */
	    while (arg &&
		   /* Must begin with - or maybe + */
		   ((sense = (*arg == '-')) ||
		    ((flags & BINF_PLUSOPTS) && *arg == '+'))) {
		/* Digits aren't arguments unless the command says they are. */
		if (!(flags & BINF_KEEPNUM) && idigit(arg[1]))
		    break;
		/* For cd and friends, a single dash is not an option. */
		if ((flags & BINF_SKIPDASH) && !arg[1])
		    break;
		if ((flags & BINF_DASHDASHVALID) && !strcmp(arg, "--")) {
		    /*
		     * Need to skip this before checking whether this is
		     * really an option.
		     */
		    argv++;
		    break;
		}
		/*
		 * Unrecognised options to echo etc. are not really
		 * options.
		 *
		 * Note this flag is not smart enough to handle option
		 * arguments.  In fact, ideally it shouldn't be added
		 * to any new builtins, to preserve standard option
		 * handling as much as possible.
		*/
		if (flags & BINF_SKIPINVALID) {
		    char *p = arg;
		    while (*++p && strchr(optstr, (int) *p));
		    if (*p)
			break;
		}
		/* handle -- or - (ops.ind['-']), and +
		 * (ops.ind['-'] and ops.ind['+']) */
		if (arg[1] == '-')
		    arg++;
		if (!arg[1]) {
		    ops.ind['-'] = 1;
		    if (!sense)
			ops.ind['+'] = 1;
		}
		/* save options in ops, as long as they are in bn->optstr */
		while (*++arg) {
		    char *optptr;
		    if ((optptr = strchr(optstr, execop = (int)*arg))) {
			ops.ind[(int)*arg] = (sense) ? 1 : 2;
			if (optptr[1] == ':') {
			    char *argptr = NULL;
			    if (optptr[2] == ':') {
				if (arg[1])
				    argptr = arg+1;
				/* Optional argument in same word*/
			    } else if (optptr[2] == '%') {
				/* Optional numeric argument in same
				 * or next word. */
				if (arg[1] && idigit(arg[1]))
				    argptr = arg+1;
				else if (argv[1] && idigit(*argv[1]))
				    argptr = arg = *++argv;
			    } else {
				/* Mandatory argument */
				if (arg[1])
				    argptr = arg+1;
				else if ((arg = *++argv))
				    argptr = arg;
				else {
				    zwarnnam(name, "argument expected: -%c",
					     execop);
				    return 1;
				}
			    }
			    if (argptr) {
				if (new_optarg(&ops)) {
				    zwarnnam(name,
					     "too many option arguments");
				    return 1;
				}
				ops.ind[execop] |= ops.argscount << 2;
				ops.args[ops.argscount-1] = argptr;
				while (arg[1])
				    arg++;
			    }
			}
		    } else
			break;
		}
		/* The above loop may have exited on an invalid option.  (We  *
		 * assume that any option requiring metafication is invalid.) */
		if (*arg) {
		    if(*arg == Meta)
			*++arg ^= 32;
		    zwarnnam(name, "bad option: %c%c", "+-"[sense], *arg);
		    return 1;
		}
		arg = *++argv;
		/* for the "print" builtin, the options after -R are treated as
		   options to "echo" */
		if ((flags & BINF_PRINTOPTS) && ops.ind['R'] &&
		    !ops.ind['f']) {
		    optstr = "ne";
		    flags |= BINF_SKIPINVALID;
		}
		/* the option -- indicates the end of the options */
		if (ops.ind['-'])
		    break;
	    }
	} else if (!(flags & BINF_HANDLES_OPTS) && *argv &&
		   !strcmp(*argv, "--")) {
	    ops.ind['-'] = 1;
	    argv++;
	}

	/* handle built-in options, for overloaded handler functions */
	if ((pp = bn->defopts)) {
	    while (*pp) {
		/* only if not already set */
		if (!ops.ind[(int)*pp])
		    ops.ind[(int)*pp] = 1;
		pp++;
	    }
	}

	/* Fix the argument count by subtracting option arguments */
	argc -= argv - argarr;

	if (errflag) {
	    errflag &= ~ERRFLAG_ERROR;
	    return 1;
	}

	/* check that the argument count lies within the specified bounds */
	if (argc < bn->minargs || (argc > bn->maxargs && bn->maxargs != -1)) {
	    zwarnnam(name, (argc < bn->minargs)
		     ? "not enough arguments" : "too many arguments");
	    return 1;
	}

	/* display execution trace information, if required */
	if (xtr) {
	    /* Use full argument list including options for trace output */
	    char **fullargv = argarr;
	    printprompt4();
	    fprintf(xtrerr, "%s", name);
	    while (*fullargv) {
	        fputc(' ', xtrerr);
	        quotedzputs(*fullargv++, xtrerr);
	    }
	    if (assigns) {
		LinkNode node;
		for (node = firstnode(assigns); node; incnode(node)) {
		    Asgment asg = (Asgment)node;
		    fputc(' ', xtrerr);
		    quotedzputs(asg->name, xtrerr);
		    if (asg->flags & ASG_ARRAY) {
			fprintf(xtrerr, "=(");
			if (asg->value.array) {
			    if (asg->flags & ASG_KEY_VALUE) {
				LinkNode keynode, valnode;
				keynode = firstnode(asg->value.array);
				for (;;) {
				    if (!keynode)
					break;
				    valnode = nextnode(keynode);
				    if (!valnode)
					break;
				    fputc('[', xtrerr);
				    quotedzputs((char *)getdata(keynode),
						xtrerr);
				    fprintf(stderr, "]=");
				    quotedzputs((char *)getdata(valnode),
						xtrerr);
				    keynode = nextnode(valnode);
				}
			    } else {
				LinkNode arrnode;
				for (arrnode = firstnode(asg->value.array);
				     arrnode;
				     incnode(arrnode)) {
				    fputc(' ', xtrerr);
				    quotedzputs((char *)getdata(arrnode),
						xtrerr);
				}
			    }
			}
			fprintf(xtrerr, " )");
		    } else if (asg->value.scalar) {
			fputc('=', xtrerr);
			quotedzputs(asg->value.scalar, xtrerr);
		    }
		}
	    }
	    fputc('\n', xtrerr);
	    fflush(xtrerr);
	}
	/* call the handler function, and return its return value */
	if (flags & BINF_ASSIGN)
	{
	    /*
	     * Takes two sets of arguments.
	     */
	    HandlerFuncAssign assignfunc = (HandlerFuncAssign)bn->handlerfunc;
	    return (*(assignfunc)) (name, argv, assigns, &ops, bn->funcid);
	}
	else
	{
	    return (*(bn->handlerfunc)) (name, argv, &ops, bn->funcid);
	}
    }
}

/* Enable/disable an element in one of the internal hash tables.  *
 * With no arguments, it lists all the currently enabled/disabled *
 * elements in that particular hash table.                        */

/**/
int
bin_enable(char *name, char **argv, Options ops, int func)
{
    HashTable ht;
    HashNode hn;
    ScanFunc scanfunc;
    Patprog pprog;
    int flags1 = 0, flags2 = 0;
    int match = 0, returnval = 0;

    /* Find out which hash table we are working with. */
    if (OPT_ISSET(ops,'p')) {
	return pat_enables(name, argv, func == BIN_ENABLE);
    } else if (OPT_ISSET(ops,'f'))
	ht = shfunctab;
    else if (OPT_ISSET(ops,'r'))
	ht = reswdtab;
    else if (OPT_ISSET(ops,'s'))
	ht = sufaliastab;
    else if (OPT_ISSET(ops,'a'))
	ht = aliastab;
    else
	ht = builtintab;

    /* Do we want to enable or disable? */
    if (func == BIN_ENABLE) {
	flags2 = DISABLED;
	scanfunc = ht->enablenode;
    } else {
	flags1 = DISABLED;
	scanfunc = ht->disablenode;
    }

    /* Given no arguments, print the names of the enabled/disabled elements  *
     * in this hash table.  If func == BIN_ENABLE, then scanhashtable will   *
     * print nodes NOT containing the DISABLED flag, else scanhashtable will *
     * print nodes containing the DISABLED flag.                             */
    if (!*argv) {
	queue_signals();
	scanhashtable(ht, 1, flags1, flags2, ht->printnode, 0);
	unqueue_signals();
	return 0;
    }

    /* With -m option, treat arguments as glob patterns. */
    if (OPT_ISSET(ops,'m')) {
	for (; *argv; argv++) {
	    queue_signals();

	    /* parse pattern */
	    tokenize(*argv);
	    if ((pprog = patcompile(*argv, PAT_STATIC, 0)))
		match += scanmatchtable(ht, pprog, 0, 0, 0, scanfunc, 0);
	    else {
		untokenize(*argv);
		zwarnnam(name, "bad pattern : %s", *argv);
		returnval = 1;
	    }
	    unqueue_signals();
	}
	/* If we didn't match anything, we return 1. */
	if (!match)
	    returnval = 1;
	return returnval;
    }

    /* Take arguments literally -- do not glob */
    queue_signals();
    for (; *argv; argv++) {
	if ((hn = ht->getnode2(ht, *argv))) {
	    scanfunc(hn, 0);
	} else {
	    zwarnnam(name, "no such hash table element: %s", *argv);
	    returnval = 1;
	}
    }
    unqueue_signals();
    return returnval;
}

/* set: either set the shell options, or set the shell arguments, *
 * or declare an array, or show various things                    */

/**/
int
bin_set(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
{
    int action, optno, array = 0, hadopt = 0,
	hadplus = 0, hadend = 0, sort = 0;
    char **x, *arrayname = NULL;

    /* Obsolescent sh compatibility: set - is the same as set +xv *
     * and set - args is the same as set +xv -- args              */
    if (!EMULATION(EMULATE_ZSH) && *args && **args == '-' && !args[0][1]) {
	dosetopt(VERBOSE, 0, 0, opts);
	dosetopt(XTRACE, 0, 0, opts);
	if (!args[1])
	    return 0;
    }

    /* loop through command line options (begins with "-" or "+") */
    while (*args && (**args == '-' || **args == '+')) {
	action = (**args == '-');
	hadplus |= !action;
	if(!args[0][1])
	    *args = "--";
	while (*++*args) {
	    if(**args == Meta)
		*++*args ^= 32;
	    if(**args != '-' || action)
		hadopt = 1;
	    /* The pseudo-option `--' signifies the end of options. */
	    if (**args == '-') {
		hadend = 1;
		args++;
		goto doneoptions;
	    } else if (**args == 'o') {
		if (!*++*args)
		    args++;
		if (!*args) {
		    printoptionstates(hadplus);
		    inittyptab();
		    return 0;
		}
		if(!(optno = optlookup(*args)))
		    zerrnam(nam, "no such option: %s", *args);
		else if(dosetopt(optno, action, 0, opts))
		    zerrnam(nam, "can't change option: %s", *args);
		break;
	    } else if(**args == 'A') {
		if(!*++*args)
		    args++;
		array = action ? 1 : -1;
		arrayname = *args;
		if (!arrayname)
		    goto doneoptions;
		else if  (!isset(KSHARRAYS))
		{
		    args++;
		    goto doneoptions;
		}
		break;
	    } else if (**args == 's')
		sort = action ? 1 : -1;
	    else {
	    	if (!(optno = optlookupc(**args)))
		    zerrnam(nam, "bad option: -%c", **args);
		else if(dosetopt(optno, action, 0, opts))
		    zerrnam(nam, "can't change option: -%c", **args);
	    }
	}
	args++;
    }
    if (errflag)
	return 1;
 doneoptions:
    inittyptab();

    /* Show the parameters, possibly with values */
    queue_signals();
    if (!arrayname)
    {
	if (!hadopt && !*args)
	    scanhashtable(paramtab, 1, 0, 0, paramtab->printnode,
			  hadplus ? PRINT_NAMEONLY : 0);

	if (array) {
	    /* display arrays */
	    scanhashtable(paramtab, 1, PM_ARRAY, 0, paramtab->printnode,
			  hadplus ? PRINT_NAMEONLY : 0);
	}
	if (!*args && !hadend) {
	    unqueue_signals();
	    return 0;
	}
    }
    if (sort)
	strmetasort(args, sort < 0 ? SORTIT_BACKWARDS : 0, NULL);
    if (array) {
	/* create an array with the specified elements */
	char **a = NULL, **y;
	int len = arrlen(args);

	if (array < 0 && (a = getaparam(arrayname)) && arrlen_gt(a, len)) {
	    a += len;
	    len += arrlen(a);
	}
	for (x = y = zalloc((len + 1) * sizeof(char *)); len--;) {
	    if (!*args)
		args = a;
	    *y++ = ztrdup(*args++);
	}
	*y++ = NULL;
	setaparam(arrayname, x);
    } else {
	/* set shell arguments */
	freearray(pparams);
	pparams = zarrdup(args);
    }
    unqueue_signals();
    return 0;
}

/**** directory-handling builtins ****/

/**/
int doprintdir = 0;		/* set in exec.c (for autocd, cdpath, etc.) */

/* pwd: display the name of the current directory */

/**/
int
bin_pwd(UNUSED(char *name), UNUSED(char **argv), Options ops, UNUSED(int func))
{
    if (OPT_ISSET(ops,'r') || OPT_ISSET(ops,'P') ||
	(isset(CHASELINKS) && !OPT_ISSET(ops,'L')))
	printf("%s\n", zgetcwd());
    else {
	zputs(pwd, stdout);
	putchar('\n');
    }
    return 0;
}

/* the directory stack */

/**/
mod_export LinkList dirstack;

/* dirs: list the directory stack, or replace it with a provided list */

/**/
int
bin_dirs(UNUSED(char *name), char **argv, Options ops, UNUSED(int func))
{
    LinkList l;

    queue_signals();
    /* with -v, -p or no arguments display the directory stack */
    if (!(*argv || OPT_ISSET(ops,'c')) || OPT_ISSET(ops,'v') ||
	OPT_ISSET(ops,'p')) {
	LinkNode node;
	char *fmt;
	int pos = 1;

	/* with the -v option, display a numbered list, starting at zero */
	if (OPT_ISSET(ops,'v')) {
	    printf("0\t");
	    fmt = "\n%d\t";
	/* with the -p option, display entries one per line */
	} else if (OPT_ISSET(ops,'p'))
	    fmt = "\n";
	else
	    fmt = " ";
	if (OPT_ISSET(ops,'l'))
	    zputs(pwd, stdout);
	else
	    fprintdir(pwd, stdout);
	for (node = firstnode(dirstack); node; incnode(node)) {
	    printf(fmt, pos++);
	    if (OPT_ISSET(ops,'l'))
		zputs(getdata(node), stdout);
	    else
		fprintdir(getdata(node), stdout);

	}
	unqueue_signals();
	putchar('\n');
	return 0;
    }
    /* replace the stack with the specified directories */
    l = znewlinklist();
    while (*argv)
	zaddlinknode(l, ztrdup(*argv++));
    freelinklist(dirstack, freestr);
    dirstack = l;
    unqueue_signals();
    return 0;
}

/* cd, chdir, pushd, popd */

/**/
void
set_pwd_env(void)
{
    Param pm;

    /* update the PWD and OLDPWD shell parameters */

    pm = (Param) paramtab->getnode(paramtab, "PWD");
    if (pm && PM_TYPE(pm->node.flags) != PM_SCALAR) {
	pm->node.flags &= ~PM_READONLY;
	unsetparam_pm(pm, 0, 1);
    }

    pm = (Param) paramtab->getnode(paramtab, "OLDPWD");
    if (pm && PM_TYPE(pm->node.flags) != PM_SCALAR) {
	pm->node.flags &= ~PM_READONLY;
	unsetparam_pm(pm, 0, 1);
    }

    assignsparam("PWD", ztrdup(pwd), 0);
    assignsparam("OLDPWD", ztrdup(oldpwd), 0);

    pm = (Param) paramtab->getnode(paramtab, "PWD");
    if (!(pm->node.flags & PM_EXPORTED))
	addenv(pm, pwd);
    pm = (Param) paramtab->getnode(paramtab, "OLDPWD");
    if (!(pm->node.flags & PM_EXPORTED))
	addenv(pm, oldpwd);
}

/* set if we are resolving links to their true paths */
static int chasinglinks;

/* The main pwd changing function.  The real work is done by other     *
 * functions.  cd_get_dest() does the initial argument processing;     *
 * cd_do_chdir() actually changes directory, if possible; cd_new_pwd() *
 * does the ancillary processing associated with actually changing    *
 * directory.                                                          */

/**/
int
bin_cd(char *nam, char **argv, Options ops, int func)
{
    LinkNode dir;

    if (isset(RESTRICTED)) {
	zwarnnam(nam, "restricted");
	return 1;
    }
    doprintdir = (doprintdir == -1);

    chasinglinks = OPT_ISSET(ops,'P') ||
	(isset(CHASELINKS) && !OPT_ISSET(ops,'L'));
    queue_signals();
    zpushnode(dirstack, ztrdup(pwd));
    if (!(dir = cd_get_dest(nam, argv, OPT_ISSET(ops,'s'), func))) {
	zsfree(getlinknode(dirstack));
	unqueue_signals();
	return 1;
    }
    cd_new_pwd(func, dir, OPT_ISSET(ops, 'q'));

    unqueue_signals();
    return 0;
}

/* Get directory to chdir to */

/**/
static LinkNode
cd_get_dest(char *nam, char **argv, int hard, int func)
{
    LinkNode dir = NULL;
    LinkNode target;
    char *dest;

    if (!argv[0]) {
	if (func == BIN_POPD && !nextnode(firstnode(dirstack))) {
	    zwarnnam(nam, "directory stack empty");
	    return NULL;
	}
	if (func == BIN_PUSHD && unset(PUSHDTOHOME))
	    dir = nextnode(firstnode(dirstack));
	if (dir)
	    zinsertlinknode(dirstack, dir, getlinknode(dirstack));
	else if (func != BIN_POPD) {
	    if (!home) {
		zwarnnam(nam, "HOME not set");
		return NULL;
	    }
	    zpushnode(dirstack, ztrdup(home));
	}
    } else if (!argv[1]) {
	int dd;
	char *end;

	doprintdir++;
	if (!isset(POSIXCD) && argv[0][1] && (argv[0][0] == '+' || argv[0][0] == '-')
	    && strspn(argv[0]+1, "0123456789") == strlen(argv[0]+1)) {
	    dd = zstrtol(argv[0] + 1, &end, 10);
	    if (*end == '\0') {
		if ((argv[0][0] == '+') ^ isset(PUSHDMINUS))
		    for (dir = firstnode(dirstack); dir && dd; dd--, incnode(dir));
		else
		    for (dir = lastnode(dirstack); dir != (LinkNode) dirstack && dd;
			 dd--, dir = prevnode(dir));
		if (!dir || dir == (LinkNode) dirstack) {
		    zwarnnam(nam, "no such entry in dir stack");
		    return NULL;
		}
	    }
	}
	if (!dir)
	    zpushnode(dirstack, ztrdup(strcmp(argv[0], "-")
				       ? (doprintdir--, argv[0]) : oldpwd));
    } else {
	char *u, *d;
	int len1, len2, len3;

	if (!(u = strstr(pwd, argv[0]))) {
	    zwarnnam(nam, "string not in pwd: %s", argv[0]);
	    return NULL;
	}
	len1 = strlen(argv[0]);
	len2 = strlen(argv[1]);
	len3 = u - pwd;
	d = (char *)zalloc(len3 + len2 + strlen(u + len1) + 1);
	strncpy(d, pwd, len3);
	strcpy(d + len3, argv[1]);
	strcat(d, u + len1);
	zpushnode(dirstack, d);
	doprintdir++;
    }

    target = dir;
    if (func == BIN_POPD) {
	if (!dir) {
	    target = dir = firstnode(dirstack);
	} else if (dir != firstnode(dirstack)) {
	    return dir;
	}
	dir = nextnode(dir);
    }
    if (!dir) {
	dir = firstnode(dirstack);
    }
    if (!dir || !getdata(dir)) {
	DPUTS(1, "Directory not set, not detected early enough");
	return NULL;
    }
    if (!(dest = cd_do_chdir(nam, getdata(dir), hard))) {
	if (!target)
	    zsfree(getlinknode(dirstack));
	if (func == BIN_POPD)
	    zsfree(remnode(dirstack, dir));
	return NULL;
    }
    if (dest != (char *)getdata(dir)) {
	zsfree(getdata(dir));
	setdata(dir, dest);
    }
    return target ? target : dir;
}

/* Change to given directory, if possible.  This function works out  *
 * exactly how the directory should be interpreted, including cdpath *
 * and CDABLEVARS.  For each possible interpretation of the given    *
 * path, this calls cd_try_chdir(), which attempts to chdir to that  *
 * particular path.                                                  */

/**/
static char *
cd_do_chdir(char *cnam, char *dest, int hard)
{
    char **pp, *ret;
    int hasdot = 0, eno = ENOENT;
    /*
     * nocdpath indicates that cdpath should not be used.
     * This is the case iff dest is a relative path
     * whose first segment is . or .., but if the path is
     * absolute then cdpath won't be used anyway.
     */
    int nocdpath;
#ifdef __CYGWIN__
    /*
     * Normalize path under Cygwin to avoid messing with
     * DOS style names with drives in them
     */
    static char buf[PATH_MAX+1];
#ifdef HAVE_CYGWIN_CONV_PATH
    cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE, dest, buf,
		     PATH_MAX);
#else
#ifndef _SYS_CYGWIN_H
    void cygwin_conv_to_posix_path(const char *, char *);
#endif

    cygwin_conv_to_posix_path(dest, buf);
#endif
    dest = buf;
#endif
    nocdpath = dest[0] == '.' &&
	(dest[1] == '/' || !dest[1] || (dest[1] == '.' &&
					(dest[2] == '/' || !dest[2])));

    /*
     * If we have an absolute path, use it as-is only
     */
    if (*dest == '/') {
	if ((ret = cd_try_chdir(NULL, dest, hard)))
	    return ret;
	zwarnnam(cnam, "%e: %s", errno, dest);
	return NULL;
    }

    /*
     * If cdpath is being used, check it for ".".
     * Don't bother doing this if POSIXCD is set, we don't
     * need to know (though it doesn't actually matter).
     */
    if (!nocdpath && !isset(POSIXCD))
	for (pp = cdpath; *pp; pp++)
	    if (!(*pp)[0] || ((*pp)[0] == '.' && (*pp)[1] == '\0'))
		hasdot = 1;
    /*
     * If 
     * (- there is no . in cdpath
     *  - or cdpath is not being used)
     *  - and the POSIXCD option is not set
     * try the directory as-is (i.e. from .)
     */
    if (!hasdot && !isset(POSIXCD)) {
	if ((ret = cd_try_chdir(NULL, dest, hard)))
	    return ret;
	if (errno != ENOENT)
	    eno = errno;
    }
    /* if cdpath is being used, try given directory relative to each element in
       cdpath in turn */
    if (!nocdpath)
	for (pp = cdpath; *pp; pp++) {
	    if ((ret = cd_try_chdir(*pp, dest, hard))) {
		if (isset(POSIXCD)) {
		    /*
		     * For POSIX we need to print the directory
		     * any time CDPATH was used, except in the
		     * special case of an empty segment being
		     * treated as a ".".
		     */
		    if (**pp)
			doprintdir++;
		}  else {
		    if (strcmp(*pp, ".")) {
			doprintdir++;
		    }
		}
		return ret;
	    }
	    if (errno != ENOENT)
		eno = errno;
	}
    /*
     * POSIX requires us to check "." after CDPATH rather than before.
     */
    if (isset(POSIXCD)) {
	if ((ret = cd_try_chdir(NULL, dest, hard)))
	    return ret;
	if (errno != ENOENT)
	    eno = errno;
    }

    /* handle the CDABLEVARS option */
    if ((ret = cd_able_vars(dest))) {
	if ((ret = cd_try_chdir(NULL, ret,hard))) {
	    doprintdir++;
	    return ret;
	}
	if (errno != ENOENT)
	    eno = errno;
    }

    /* If we got here, it means that we couldn't chdir to any of the
       multitudinous possible paths allowed by zsh.  We've run out of options!
       Add more here! */
    zwarnnam(cnam, "%e: %s", eno, dest);
    return NULL;
}

/* If the CDABLEVARS option is set, return the new *
 * interpretation of the given path.               */

/**/
char *
cd_able_vars(char *s)
{
    char *rest, save;

    if (isset(CDABLEVARS)) {
	for (rest = s; *rest && *rest != '/'; rest++);
	save = *rest;
	*rest = 0;
	s = getnameddir(s);
	*rest = save;

	if (s && *rest)
	    s = dyncat(s, rest);

	return s;
    }
    return NULL;
}

/* Attempt to change to a single given directory.  The directory,    *
 * for the convenience of the calling function, may be provided in   *
 * two parts, which must be concatenated before attempting to chdir. *
 * Returns NULL if the chdir fails.  If the directory change is      *
 * possible, it is performed, and a pointer to the new full pathname *
 * is returned.                                                      */

/**/
static char *
cd_try_chdir(char *pfix, char *dest, int hard)
{
    char *buf;
    int dlen, dochaselinks = 0;

    /* handle directory prefix */
    if (pfix && *pfix) {
	if (*pfix == '/') {
#ifdef __CYGWIN__
/* NB: Don't turn "/"+"bin" into "//"+"bin" by mistake!  "//bin" may *
 * not be what user really wants (probably wants "/bin"), but        *
 * "//bin" could be valid too (see fixdir())!  This is primarily for *
 * handling CDPATH correctly.  Likewise for "//"+"bin" not becoming  *
 * "///bin" (aka "/bin").                                            */
	    int root = pfix[1] == '\0' || (pfix[1] == '/' && pfix[2] == '\0');
	    buf = tricat(pfix, ( root ? "" : "/" ), dest);
#else
	    buf = tricat(pfix, "/", dest);
#endif
	} else {
	    int pfl = strlen(pfix);
	    dlen = strlen(pwd);
	    if (dlen == 1 && *pwd == '/')
		dlen = 0;
	    buf = zalloc(dlen + pfl + strlen(dest) + 3);
	    if (dlen)
		strcpy(buf, pwd);
	    buf[dlen] = '/';
	    strcpy(buf + dlen + 1, pfix);
	    buf[dlen + 1 + pfl] = '/';
	    strcpy(buf + dlen + pfl + 2, dest);
	}
    } else if (*dest == '/')
	buf = ztrdup(dest);
    else {
	dlen = strlen(pwd);
	if (pwd[dlen-1] == '/')
	    --dlen;
	buf = zalloc(dlen + strlen(dest) + 2);
	strcpy(buf, pwd);
	buf[dlen] = '/';
	strcpy(buf + dlen + 1, dest);
    }

    /* Normalise path.  See the definition of fixdir() for what this means.
     * We do not do this if we are chasing links.
     */
    if (!chasinglinks)
	dochaselinks = fixdir(buf);
    else
	unmetafy(buf, &dlen);

    /* We try the full path first.  If that fails, try the
     * argument to cd relatively.  This is useful if the cwd
     * or a parent directory is renamed in the interim.
     */
    if (lchdir(buf, NULL, hard) &&
	(pfix || *dest == '/' || lchdir(unmeta(dest), NULL, hard))) {
	free(buf);
	return NULL;
    }
    /* the chdir succeeded, so decide if we should force links to be chased */
    if (dochaselinks)
	chasinglinks = 1;
    return metafy(buf, -1, META_NOALLOC);
}

/* do the extra processing associated with changing directory */

/**/
static void
cd_new_pwd(int func, LinkNode dir, int quiet)
{
    char *new_pwd, *s;
    struct stat st1, st2;
    int dirstacksize;

    if (func == BIN_PUSHD)
	rolllist(dirstack, dir);
    new_pwd = remnode(dirstack, dir);

    if (func == BIN_POPD && firstnode(dirstack)) {
	zsfree(new_pwd);
	new_pwd = getlinknode(dirstack);
    } else if (func == BIN_CD && unset(AUTOPUSHD))
	zsfree(getlinknode(dirstack));

    if (chasinglinks) {
	s = findpwd(new_pwd);
	if (s) {
	    zsfree(new_pwd);
	    new_pwd = s;
	}
    }
    if (isset(PUSHDIGNOREDUPS)) {
	LinkNode n;
	for (n = firstnode(dirstack); n; incnode(n)) {
	    if (!strcmp(new_pwd, getdata(n))) {
		zsfree(remnode(dirstack, n));
		break;
	    }
	}
    }

    if (stat(unmeta(new_pwd), &st1) < 0) {
	zsfree(new_pwd);
	new_pwd = NULL;
	new_pwd = metafy(zgetcwd(), -1, META_DUP);
    } else if (stat(".", &st2) < 0) {
	if (chdir(unmeta(new_pwd)) < 0)
	    zwarn("unable to chdir(%s): %e", new_pwd, errno);
    } else if (st1.st_ino != st2.st_ino || st1.st_dev != st2.st_dev) {
	if (chasinglinks) {
	    zsfree(new_pwd);
	    new_pwd = NULL;
	    new_pwd = metafy(zgetcwd(), -1, META_DUP);
	} else if (chdir(unmeta(new_pwd)) < 0)
	    zwarn("unable to chdir(%s): %e", new_pwd, errno);
    }

    /* shift around the pwd variables, to make oldpwd and pwd relate to the
       current (i.e. new) pwd */
    zsfree(oldpwd);
    oldpwd = pwd;
    setjobpwd();
    pwd = new_pwd;
    set_pwd_env();

    if (isset(INTERACTIVE) || isset(POSIXCD)) {
	if (func != BIN_CD && isset(INTERACTIVE)) {
            if (unset(PUSHDSILENT) && !quiet)
	        printdirstack();
	} else if (unset(CDSILENT) && doprintdir) {
	    fprintdir(pwd, stdout);
	    putchar('\n');
	}
    }

    /* execute the chpwd function */
    fflush(stdout);
    fflush(stderr);
    if (!quiet)
	callhookfunc("chpwd", NULL, 1, NULL);

    dirstacksize = getiparam("DIRSTACKSIZE");
    /* handle directory stack sizes out of range */
    if (dirstacksize > 0) {
	int remove = countlinknodes(dirstack) -
	    (dirstacksize < 2 ? 2 : dirstacksize);
	while (remove-- >= 0)
	    zsfree(remnode(dirstack, lastnode(dirstack)));
    }
}

/* Print the directory stack */

/**/
static void
printdirstack(void)
{
    LinkNode node;

    fprintdir(pwd, stdout);
    for (node = firstnode(dirstack); node; incnode(node)) {
	putchar(' ');
	fprintdir(getdata(node), stdout);
    }
    putchar('\n');
}

/* Normalise a path.  Segments consisting of ., and foo/.. *
 * combinations, are removed and the path is unmetafied.
 * Returns 1 if we found a ../ path which should force links to
 * be chased, 0 otherwise.
 */

/**/
int
fixdir(char *src)
{
    char *dest = src, *d0 = dest;
#ifdef __CYGWIN__
    char *s0 = src;
#endif
    /* This function is always called with n path containing at
     * least one slash, either because one was input by the user or
     * because the caller has prepended either pwd or a cdpath dir.
     * If asked to make a relative change and pwd is set to ".",
     * the current directory has been removed out from under us,
     * so force links to be chased.
     *
     * Ordinarily we can't get here with "../" as the first component
     * but handle the silly special case of ".." in cdpath.
     *
     * Order of comparisons here looks funny, but it short-circuits
     * most rapidly in the event of a false condition.  Set to 2
     * here so we still obey the (lack of) CHASEDOTS option after
     * the first "../" is preserved (test chasedots > 1 below).
     */
    int chasedots = (src[0] == '.' && pwd[0] == '.' && pwd[1] == '\0' &&
		     (src[1] == '/' || (src[1] == '.' && src[2] == '/'))) * 2;

/*** if have RFS superroot directory ***/
#ifdef HAVE_SUPERROOT
    /* allow /.. segments to remain */
    while (*src == '/' && src[1] == '.' && src[2] == '.' &&
	   (!src[3] || src[3] == '/')) {
	*dest++ = '/';
	*dest++ = '.';
	*dest++ = '.';
	src += 3;
    }
#endif

    for (;;) {
	/* compress multiple /es into single */
	if (*src == '/') {
#ifdef __CYGWIN__
	    /* allow leading // under cygwin, but /// still becomes / */
	    if (src == s0 && src[1] == '/' && src[2] != '/')
		*dest++ = *src++;
#endif
	    *dest++ = *src++;
	    while (*src == '/')
		src++;
	}
	/* if we are at the end of the input path, remove a trailing / (if it
	   exists), and return ct */
	if (!*src) {
	    while (dest > d0 + 1 && dest[-1] == '/')
		dest--;
	    *dest = '\0';
	    return chasedots;
	}
	if (src[0] == '.' && src[1] == '.' &&
	    (src[2] == '\0' || src[2] == '/')) {
	    if (isset(CHASEDOTS) || chasedots > 1) {
		chasedots = 1;
		/* and treat as normal path segment */
	    } else {
		if (dest > d0 + 1) {
		    /*
		     * remove a foo/.. combination:
		     * first check foo exists, else return.
		     */
		    struct stat st;
		    *dest = '\0';
		    if (stat(d0, &st) < 0 || !S_ISDIR(st.st_mode)) {
			char *ptrd, *ptrs;
			if (dest == src)
			    *dest = '.';
			for (ptrs = src, ptrd = dest; *ptrs; ptrs++, ptrd++)
			    *ptrd = (*ptrs == Meta) ? (*++ptrs ^ 32) : *ptrs;
			*ptrd = '\0';
			return 1;
		    }
		    for (dest--; dest > d0 + 1 && dest[-1] != '/'; dest--);
		    if (dest[-1] != '/')
			dest--;
		}
		src++;
		while (*++src == '/');
		continue;
	    }
	}
	if (src[0] == '.' && (src[1] == '/' || src[1] == '\0')) {
	    /* skip a . section */
	    while (*++src == '/');
	} else {
	    /* copy a normal segment into the output */
	    while (*src != '/' && *src != '\0')
		if ((*dest++ = *src++) == Meta)
		    dest[-1] = *src++ ^ 32;
	}
    }
    /* unreached */
}

/**/
mod_export void
printqt(char *str)
{
    /* Print str, but turn any single quote into '\'' or ''. */
    for (; *str; str++)
	if (*str == '\'')
	    printf(isset(RCQUOTES) ? "''" : "'\\''");
	else
	    putchar(*str);
}

/**/
mod_export void
printif(char *str, int c)
{
    /* If flag c has an argument, print that */
    if (str) {
	printf(" -%c ", c);
	quotedzputs(str, stdout);
    }
}

/**** history list functions ****/

/* fc, history, r */

/**/
int
bin_fc(char *nam, char **argv, Options ops, int func)
{
    zlong first = -1, last = -1;
    int retval;
    char *s;
    struct asgment *asgf = NULL, *asgl = NULL;
    Patprog pprog = NULL;

    /* fc is only permitted in interactive shells */
#ifdef FACIST_INTERACTIVE
    if (!interact) {
	zwarnnam(nam, "not interactive shell");
	return 1;
    }
#endif
    if (OPT_ISSET(ops,'p')) {
	char *hf = "";
	zlong hs = DEFAULT_HISTSIZE;
	zlong shs = 0;
	int level = OPT_ISSET(ops,'a') ? locallevel : -1;
	if (*argv) {
	    hf = *argv++;
	    if (*argv) {
		char *check;
		hs = zstrtol(*argv++, &check, 10);
		if (*check) {
		    zwarnnam("fc", "HISTSIZE must be an integer");
		    return 1;
		}
		if (*argv) {
		    shs = zstrtol(*argv++, &check, 10);
		    if (*check) {
			zwarnnam("fc", "SAVEHIST must be an integer");
			return 1;
		    }
		} else
		    shs = hs;
		if (*argv) {
		    zwarnnam("fc", "too many arguments");
		    return 1;
		}
	    } else {
		hs = histsiz;
		shs = savehistsiz;
	    }
	}
	if (!pushhiststack(hf, hs, shs, level))
	    return 1;
	if (*hf) {
	    struct stat st;
	    if (stat(hf, &st) >= 0 || errno != ENOENT)
		readhistfile(hf, 1, HFILE_USE_OPTIONS);
	}
	return 0;
    }
    if (OPT_ISSET(ops,'P')) {
	if (*argv) {
	    zwarnnam("fc", "too many arguments");
	    return 1;
	}
	return !saveandpophiststack(-1, HFILE_USE_OPTIONS);
    }
    /* with the -m option, the first argument is taken *
     * as a pattern that history lines have to match   */
    if (*argv && OPT_ISSET(ops,'m')) {
	tokenize(*argv);
	if (!(pprog = patcompile(*argv++, 0, NULL))) {
	    zwarnnam(nam, "invalid match pattern");
	    return 1;
	}
    }
    queue_signals();
    if (OPT_ISSET(ops,'R')) {
	/* read history from a file */
	readhistfile(*argv, 1, OPT_ISSET(ops,'I') ? HFILE_SKIPOLD : 0);
	unqueue_signals();
	return 0;
    }
    if (OPT_ISSET(ops,'W')) {
	/* write history to a file */
	savehistfile(*argv, 1, OPT_ISSET(ops,'I') ? HFILE_SKIPOLD : 0);
	unqueue_signals();
	return 0;
    }
    if (OPT_ISSET(ops,'A')) {
	/* append history to a file */
	savehistfile(*argv, 1, HFILE_APPEND |
		     (OPT_ISSET(ops,'I') ? HFILE_SKIPOLD : 0));
	unqueue_signals();
	return 0;
    }

    if (zleactive) {
	unqueue_signals();
	zwarnnam(nam, "no interactive history within ZLE");
	return 1;
    }

    /* put foo=bar type arguments into the substitution list */
    while (*argv && equalsplit(*argv, &s)) {
	Asgment a = (Asgment) zhalloc(sizeof *a);

	if (!**argv) {
	    zwarnnam(nam, "invalid replacement pattern: =%s", s);
	    return 1;
	}
	if (!asgf)
	    asgf = asgl = a;
	else {
	    asgl->node.next = &a->node;
	    asgl = a;
	}
	a->name = *argv;
	a->flags = 0;
	a->value.scalar = s;
	a->node.next = a->node.prev = NULL;
	argv++;
    }
    /* interpret and check first history line specifier */
    if (*argv) {
	first = fcgetcomm(*argv);
	if (first == -1) {
	    unqueue_signals();
	    return 1;
	}
	argv++;
    }
    /* interpret and check second history line specifier */
    if (*argv) {
	last = fcgetcomm(*argv);
	if (last == -1) {
	    unqueue_signals();
	    return 1;
	}
	argv++;
    }
    /* There is a maximum of two history specifiers.  At least, there *
     * will be as long as the history list is one-dimensional.        */
    if (*argv) {
	unqueue_signals();
	zwarnnam("fc", "too many arguments");
	return 1;
    }
    /* default values of first and last, and range checking */
    if (last == -1) {
	if (OPT_ISSET(ops,'l') && first < curhist) {
	    /*
	     * When listing base our calculations on curhist,
	     * to show anything added since the edited history line.
	     * Also, in that case curhist will have been modified
	     * past the current history line; then we want to
	     * show everything, because the user expects to
	     * see the result of "print -s".  Otherwise, we subtract
	     * -1 from the line, because the user doesn't usually expect
	     * to see the command line that caused history to be
	     * listed.
	     */
	    last = (curline.histnum == curhist) ? addhistnum(curhist,-1,0)
		: curhist;
	    if (last < firsthist())
		last = firsthist();
	}
	else
	    last = first;
    }
    if (first == -1) {
	/*
	 * When listing, we want to see everything that's been
	 * added to the history, including by print -s, so use
	 * curhist.
	 * When reexecuting, we want to restrict to the last edited
	 * command line to avoid giving the user a nasty turn
	 * if some helpful soul ran "print -s 'rm -rf /'".
	 */
	first = OPT_ISSET(ops,'l')? addhistnum(curhist,-16,0)
			: addhistnum(curline.histnum,-1,0);
	if (first < 1)
	    first = 1;
	if (last < first)
	    last = first;
    }
    if (OPT_ISSET(ops,'l')) {
	/* list the required part of the history */
	retval = fclist(stdout, ops, first, last, asgf, pprog, 0);
	unqueue_signals();
    }
    else {
	/* edit history file, and (if successful) use the result as a new command */
	int tempfd;
	FILE *out;
	char *fil;

	retval = 1;
	if ((tempfd = gettempfile(NULL, 1, &fil)) < 0
	 || ((out = fdopen(tempfd, "w")) == NULL)) {
	    unqueue_signals();
	    zwarnnam("fc", "can't open temp file: %e", errno);
	} else {
	    /*
	     * Nasty behaviour results if we use the current history
	     * line here.  Treat it as if it doesn't exist, unless
	     * that gives us an empty range.
	     */
	    if (last >= curhist) {
		last = curhist - 1;
		if (first > last) {
		    unqueue_signals();
		    zwarnnam("fc",
		      "current history line would recurse endlessly, aborted");
		    fclose(out);
		    unlink(fil);
		    return 1;
		}
	    }
	    ops->ind['n'] = 1;	/* No line numbers here. */
	    if (!fclist(out, ops, first, last, asgf, pprog, 1)) {
		char *editor;

		if (func == BIN_R)
		    editor = "-";
		else if (OPT_HASARG(ops, 'e'))
		    editor = OPT_ARG(ops, 'e');
		else
		    editor = getsparam("FCEDIT");
		if (!editor)
		    editor = getsparam("EDITOR");
		if (!editor)
		    editor = DEFAULT_FCEDIT;

		unqueue_signals();
		if (fcedit(editor, fil)) {
		    if (stuff(fil))
			zwarnnam("fc", "%e: %s", errno, fil);
		    else {
			loop(0,1);
			retval = lastval;
		    }
		}
	    } else
		unqueue_signals();
	}
	unlink(fil);
    }
    return retval;
}

/* History handling functions: these are called by ZLE, as well as  *
 * the actual builtins.  fcgetcomm() gets a history line, specified *
 * either by number or leading string.  fcsubs() performs a given   *
 * set of simple old=new substitutions on a given command line.     *
 * fclist() outputs a given range of history lines to a text file.  */

/* get the history event associated with s */

/**/
static zlong
fcgetcomm(char *s)
{
    zlong cmd;

    /* First try to match a history number.  Negative *
     * numbers indicate reversed numbering.           */
    if ((cmd = atoi(s)) != 0 || *s == '0') {
	if (cmd < 0)
	    cmd = addhistnum(curline.histnum,cmd,HIST_FOREIGN);
	if (cmd < 0)
	    cmd = 0;
	return cmd;
    }
    /* not a number, so search by string */
    cmd = hcomsearch(s);
    if (cmd == -1)
	zwarnnam("fc", "event not found: %s", s);
    return cmd;
}

/* Perform old=new substitutions.  Uses the asgment structure from zsh.h, *
 * which is essentially a linked list of string,replacement pairs.       */

/**/
static int
fcsubs(char **sp, struct asgment *sub)
{
    char *oldstr, *newstr, *oldpos, *newpos, *newmem, *s = *sp;
    int subbed = 0;

    /* loop through the linked list */
    while (sub) {
	oldstr = sub->name;
	newstr = sub->value.scalar;
	sub = (Asgment)sub->node.next;
	oldpos = s;
	/* loop over occurrences of oldstr in s, replacing them with newstr */
	while ((newpos = (char *)strstr(oldpos, oldstr))) {
	    newmem = (char *) zhalloc(1 + (newpos - s)
				      + strlen(newstr) + strlen(newpos + strlen(oldstr)));
	    ztrncpy(newmem, s, newpos - s);
	    strcat(newmem, newstr);
	    oldpos = newmem + strlen(newmem);
	    strcat(newmem, newpos + strlen(oldstr));
	    s = newmem;
	    subbed = 1;
	}
    }
    *sp = s;
    return subbed;
}

/* Print a series of history events to a file.  The file pointer is     *
 * given by f, and the required range of events by first and last.      *
 * subs is an optional list of foo=bar substitutions to perform on the  *
 * history lines before output.  com is an optional comp structure      *
 * that the history lines are required to match.  n, r, D and d are     *
 * options: n indicates that each line should be numbered.  r indicates *
 * that the lines should be output in reverse order (newest first).     *
 * D indicates that the real time taken by each command should be       *
 * output.  d indicates that the time of execution of each command      *
 * should be output; d>1 means that the date should be output too; d>3  *
 * means that mm/dd/yyyy form should be used for the dates, as opposed  *
 * to dd.mm.yyyy form; d>7 means that yyyy-mm-dd form should be used.   */

/**/
static int
fclist(FILE *f, Options ops, zlong first, zlong last,
       struct asgment *subs, Patprog pprog, int is_command)
{
    int fclistdone = 0, xflags = 0;
    zlong tmp;
    char *s, *tdfmt, *timebuf;
    Histent ent;

    /* reverse range if required */
    if (OPT_ISSET(ops,'r')) {
	tmp = last;
	last = first;
	first = tmp;
    }
    if (is_command && first > last) {
	zwarnnam("fc", "history events can't be executed backwards, aborted");
	if (f != stdout)
	    fclose(f);
	return 1;
    }

    ent = gethistent(first, first < last? GETHIST_DOWNWARD : GETHIST_UPWARD);
    if (!ent || (first < last? ent->histnum > last : ent->histnum < last)) {
	if (first == last) {
	    char buf[DIGBUFSIZE];
	    convbase(buf, first, 10);
	    zwarnnam("fc", "no such event: %s", buf);
	} else
	    zwarnnam("fc", "no events in that range");
	if (f != stdout)
	    fclose(f);
	return 1;
    }

    if (OPT_ISSET(ops,'d') || OPT_ISSET(ops,'f') ||
	OPT_ISSET(ops,'E') || OPT_ISSET(ops,'i') ||
	OPT_ISSET(ops,'t')) {
	if (OPT_ISSET(ops,'t')) {
	    tdfmt = OPT_ARG(ops,'t');
	} else if (OPT_ISSET(ops,'i')) {
	    tdfmt = "%Y-%m-%d %H:%M";
	} else if (OPT_ISSET(ops,'E')) {
	    tdfmt = "%f.%-m.%Y %H:%M";
	} else if (OPT_ISSET(ops,'f')) {
	    tdfmt = "%-m/%f/%Y %H:%M";
	} else {
	    tdfmt = "%H:%M";
	}
	timebuf = zhalloc(256);
    } else {
	tdfmt = timebuf = NULL;
    }

    /* xflags exclude events */
    if (OPT_ISSET(ops,'L')) {
	xflags |= HIST_FOREIGN;
    }
    if (OPT_ISSET(ops,'I')) {
	xflags |= HIST_READ;
    }

    for (;;) {
	if (ent->node.flags & xflags)
	    s = NULL;
	else
	    s = dupstring(ent->node.nam);
	/* this if does the pattern matching, if required */
	if (s && (!pprog || pattry(pprog, s))) {
	    /* perform substitution */
	    fclistdone |= (subs ? fcsubs(&s, subs) : 1);

	    /* do numbering */
	    if (!OPT_ISSET(ops,'n')) {
		char buf[DIGBUFSIZE];
		convbase(buf, ent->histnum, 10);
		fprintf(f, "%5s%c ", buf,
			ent->node.flags & HIST_FOREIGN ? '*' : ' ');
	    }
	    /* output actual time (and possibly date) of execution of the
	       command, if required */
	    if (tdfmt != NULL) {
		struct tm *ltm;
		int len;
		ltm = localtime(&ent->stim);
		if ((len = ztrftime(timebuf, 256, tdfmt, ltm, 0L)) >= 0) {
		    fwrite(timebuf, 1, len, f);
		    fprintf(f, "  ");
		}
	    }
	    /* display the time taken by the command, if required */
	    if (OPT_ISSET(ops,'D')) {
		long diff;
		diff = (ent->ftim) ? ent->ftim - ent->stim : 0;
		fprintf(f, "%ld:%02ld  ", diff / 60, diff % 60);
	    }

	    /* output the command */
	    if (f == stdout) {
		nicezputs(s, f);
		putc('\n', f);
	    } else {
		int len;
		unmetafy(s, &len);
		fwrite(s, 1, len, f);
		putc('\n', f);
	    }
	}
	/* move on to the next history line, or quit the loop */
	if (first < last) {
	    if (!(ent = down_histent(ent)) || ent->histnum > last)
		break;
	}
	else {
	    if (!(ent = up_histent(ent)) || ent->histnum < last)
		break;
	}
    }

    /* final processing */
    if (f != stdout)
	fclose(f);
    if (!fclistdone) {
	if (subs)
	    zwarnnam("fc", "no substitutions performed");
	else if (xflags || pprog)
	    zwarnnam("fc", "no matching events found");
	return 1;
    }
    return 0;
}

/* edit a history file */

/**/
static int
fcedit(char *ename, char *fn)
{
    char *s;

    if (!strcmp(ename, "-"))
	return 1;

    s = tricat(ename, " ", fn);
    execstring(s, 1, 0, "fc");
    zsfree(s);

    return !lastval;
}

/**** parameter builtins ****/

/* Separate an argument into name=value parts, returning them in an     *
 * asgment structure.  Because the asgment structure used is global,    *
 * only one of these can be active at a time.  The string s gets placed *
 * in this global structure, so it needs to be in permanent memory.     */

/**/
static Asgment
getasg(char ***argvp, LinkList assigns)
{
    char *s = **argvp;
    static struct asgment asg;

    /* sanity check for valid argument */
    if (!s) {
	if (assigns) {
	    Asgment asgp = (Asgment)firstnode(assigns);
	    if (!asgp)
		return NULL;
	    (void)uremnode(assigns, &asgp->node);
	    return asgp;
	}
	return NULL;
    }

    /* check if name is empty */
    if (*s == '=') {
	zerr("bad assignment");
	return NULL;
    }
    asg.name = s;
    asg.flags = 0;

    /* search for `=' */
    for (; *s && *s != '='; s++);

    /* found `=', so return with a value */
    if (*s) {
	*s = '\0';
	asg.value.scalar = s + 1;
    } else {
	/* didn't find `=', so we only have a name */
	asg.value.scalar = NULL;
    }
    (*argvp)++;
    return &asg;
}

/* for new special parameters */
enum {
    NS_NONE,
    NS_NORMAL,
    NS_SECONDS
};

static const struct gsu_scalar tiedarr_gsu =
{ tiedarrgetfn, tiedarrsetfn, tiedarrunsetfn };

/* Install a base if we are turning on a numeric option with an argument */

static int
typeset_setbase(const char *name, Param pm, Options ops, int on, int always)
{
    char *arg = NULL;

    if ((on & PM_INTEGER) && OPT_HASARG(ops,'i'))
	arg = OPT_ARG(ops,'i');
    else if ((on & PM_EFLOAT) && OPT_HASARG(ops,'E'))
	arg = OPT_ARG(ops,'E');
    else if ((on & PM_FFLOAT) && OPT_HASARG(ops,'F'))
	arg = OPT_ARG(ops,'F');

    if (arg) {
	char *eptr;
	int base = (int)zstrtol(arg, &eptr, 10);
	if (*eptr) {
	    if (on & PM_INTEGER)
		zwarnnam(name, "bad base value: %s", arg);
	    else
		zwarnnam(name, "bad precision value: %s", arg);
	    return 1;
	}
	if ((on & PM_INTEGER) && (base < 2 || base > 36)) {
	    zwarnnam(name, "invalid base (must be 2 to 36 inclusive): %d",
		     base);
	    return 1;
	}
	pm->base = base;
    } else if (always)
	pm->base = 0;

    return 0;
}

/* Install a width if we are turning on a padding option with an argument */

static int
typeset_setwidth(const char * name, Param pm, Options ops, int on, int always)
{
    char *arg = NULL;

    if ((on & PM_LEFT) && OPT_HASARG(ops,'L'))
	arg = OPT_ARG(ops,'L');
    else if ((on & PM_RIGHT_B) && OPT_HASARG(ops,'R'))
	arg = OPT_ARG(ops,'R');
    else if ((on & PM_RIGHT_Z) && OPT_HASARG(ops,'Z'))
	arg = OPT_ARG(ops,'Z');

    if (arg) {
	char *eptr;
	pm->width = (int)zstrtol(arg, &eptr, 10);
	if (*eptr) {
	    zwarnnam(name, "bad width value: %s", arg);
	    return 1;
	}
    } else if (always)
	pm->width = 0;

    return 0;
}

/* function to set a single parameter */

/**/
static Param
typeset_single(char *cname, char *pname, Param pm, UNUSED(int func),
	       int on, int off, int roff, Asgment asg, Param altpm,
	       Options ops, int joinchar)
{
    int usepm, tc, keeplocal = 0, newspecial = NS_NONE, readonly, dont_set = 0;
    char *subscript;

    /*
     * Do we use the existing pm?  Note that this isn't the end of the
     * story, because if we try and create a new pm at the same
     * locallevel as an unset one we use the pm struct anyway: that's
     * handled in createparam().  Here we just avoid using it for the
     * present tests if it's unset.
     *
     * POSIXBUILTINS horror: we need to retain the 'readonly' or 'export'
     * flags of an unset parameter.
     */
    usepm = pm && (!(pm->node.flags & PM_UNSET) ||
		   (isset(POSIXBUILTINS) &&
		    (pm->node.flags & (PM_READONLY|PM_EXPORTED))));

    /*
     * We need to compare types with an existing pm if special,
     * even if that's unset
     */
    if (!usepm && pm && (pm->node.flags & PM_SPECIAL))
	usepm = 2;	/* indicate that we preserve the PM_UNSET flag */

    /*
     * Don't use an existing param if
     *   - the local level has changed, and
     *   - we are really locallizing the parameter
     */
    if (usepm && locallevel != pm->level && (on & PM_LOCAL)) {
	/*
	 * If the original parameter was special and we're creating
	 * a new one, we need to keep it special.
	 *
	 * The -h (hide) flag prevents an existing special being made
	 * local.  It can be applied either to the special or in the
	 * typeset/local statement for the local variable.
	 */
	if ((pm->node.flags & PM_SPECIAL)
	    && !(on & PM_HIDE) && !(pm->node.flags & PM_HIDE & ~off))
	    newspecial = NS_NORMAL;
	usepm = 0;
    }

    /* attempting a type conversion, or making a tied colonarray? */
    tc = 0;
    if (ASG_ARRAYP(asg) && PM_TYPE(on) == PM_SCALAR &&
	!(usepm && (PM_TYPE(pm->node.flags) & (PM_ARRAY|PM_HASHED))))
	on |= PM_ARRAY;
    if (usepm && ASG_ARRAYP(asg) && newspecial == NS_NONE &&
	PM_TYPE(pm->node.flags) != PM_ARRAY &&
	PM_TYPE(pm->node.flags) != PM_HASHED) {
	if (on & (PM_EFLOAT|PM_FFLOAT|PM_INTEGER)) {
	    zerrnam(cname, "%s: can't assign array value to non-array", pname);
	    return NULL;
	}
	if (pm->node.flags & PM_SPECIAL) {
	    zerrnam(cname, "%s: can't assign array value to non-array special", pname);
	    return NULL;
	}
	tc = 1;
	usepm = 0;
    }
    else if (usepm || newspecial != NS_NONE) {
	int chflags = ((off & pm->node.flags) | (on & ~pm->node.flags)) &
	    (PM_INTEGER|PM_EFLOAT|PM_FFLOAT|PM_HASHED|
	     PM_ARRAY|PM_TIED|PM_AUTOLOAD);
	/* keep the parameter if just switching between floating types */
	if ((tc = chflags && chflags != (PM_EFLOAT|PM_FFLOAT)))
	    usepm = 0;
    }

    /*
     * Extra checks if converting the type of a parameter, or if
     * trying to remove readonlyness.  It's dangerous doing either
     * with a special or a parameter which isn't loaded yet (which
     * may be special when it is loaded; we can't tell yet).
     */
    if ((readonly =
	 ((usepm || newspecial != NS_NONE) &&
	  (off & pm->node.flags & PM_READONLY))) ||
	tc) {
	if (pm->node.flags & PM_SPECIAL) {
	    int err = 1;
	    if (!readonly && !strcmp(pname, "SECONDS"))
	    {
		/*
		 * We allow SECONDS to change type between integer
		 * and floating point.  If we are creating a new
		 * local copy we check the type here and allow
		 * a new special to be created with that type.
		 * We then need to make sure the correct type
		 * for the special is restored at the end of the scope.
		 * If we are changing the type of an existing
		 * parameter, we do the whole thing here.
		 */
		if (newspecial != NS_NONE)
		{
		    /*
		     * The first test allows `typeset' to copy the
		     * existing type.  This is the usual behaviour
		     * for making special parameters local.
		     */
		    if (PM_TYPE(on) == 0 || PM_TYPE(on) == PM_INTEGER ||
			PM_TYPE(on) == PM_FFLOAT || PM_TYPE(on) == PM_EFLOAT)
		    {
			newspecial = NS_SECONDS;
			err = 0;	/* and continue */
			tc = 0;	/* but don't do a normal conversion */
		    }
		} else if (!setsecondstype(pm, on, off)) {
		    if (asg->value.scalar &&
			!(pm = assignsparam(
			      pname, ztrdup(asg->value.scalar), 0)))
			return NULL;
		    usepm = 1;
		    err = 0;
		}
	    }
	    if (err)
	    {
		zerrnam(cname, "%s: can't change type of a special parameter",
			pname);
		return NULL;
	    }
	} else if (pm->node.flags & PM_AUTOLOAD) {
	    zerrnam(cname, "%s: can't change type of autoloaded parameter",
		    pname);
	    return NULL;
	}
    }
    else if (newspecial != NS_NONE && strcmp(pname, "SECONDS") == 0)
	newspecial = NS_SECONDS;

    if (isset(POSIXBUILTINS)) {
	/*
	 * Stricter rules about retaining readonly attribute in this case.
	 */
	if ((on & (PM_READONLY|PM_EXPORTED)) &&
	    (!usepm || (pm->node.flags & PM_UNSET)) &&
	    !ASG_VALUEP(asg))
	    on |= PM_UNSET;
	else if (usepm && (pm->node.flags & PM_READONLY) &&
		 !(on & PM_READONLY) && func != BIN_EXPORT) {
	    zerr("read-only variable: %s", pm->node.nam);
	    return NULL;
	}
	/* This is handled by createparam():
	if (usepm && (pm->node.flags & PM_EXPORTED) && !(off & PM_EXPORTED))
	    on |= PM_EXPORTED;
	*/
    }

    /*
     * A parameter will be local if
     * 1. we are re-using an existing local parameter
     *    or
     * 2. we are not using an existing parameter, but
     *   i. there is already a parameter, which will be hidden
     *     or
     *   ii. we are creating a new local parameter
     */
    if (usepm) {
	if ((asg->flags & ASG_ARRAY) ?
	    !(PM_TYPE(pm->node.flags) & (PM_ARRAY|PM_HASHED)) :
	    (asg->value.scalar && (PM_TYPE(pm->node.flags &
					   (PM_ARRAY|PM_HASHED))))) {
	    zerrnam(cname, "%s: inconsistent type for assignment", pname);
	    return NULL;
	}
	on &= ~PM_LOCAL;
	if (!on && !roff && !ASG_VALUEP(asg)) {
	    if (OPT_ISSET(ops,'p'))
		paramtab->printnode(&pm->node, PRINT_TYPESET);
	    else if (!OPT_ISSET(ops,'g') &&
		     (unset(TYPESETSILENT) || OPT_ISSET(ops,'m')))
		paramtab->printnode(&pm->node, PRINT_INCLUDEVALUE);
	    return pm;
	}
	if ((pm->node.flags & PM_RESTRICTED) && isset(RESTRICTED)) {
	    zerrnam(cname, "%s: restricted", pname);
	    return pm;
	}
	if ((on & PM_UNIQUE) && !(pm->node.flags & PM_READONLY & ~off)) {
	    Param apm;
	    char **x;
	    if (PM_TYPE(pm->node.flags) == PM_ARRAY) {
		x = (*pm->gsu.a->getfn)(pm);
		uniqarray(x);
		if (pm->node.flags & PM_SPECIAL) {
		    if (zheapptr(x))
			x = zarrdup(x);
		    (*pm->gsu.a->setfn)(pm, x);
		} else if (pm->ename && x)
		    arrfixenv(pm->ename, x);
	    } else if (PM_TYPE(pm->node.flags) == PM_SCALAR && pm->ename &&
		       (apm =
			(Param) paramtab->getnode(paramtab, pm->ename))) {
		x = (*apm->gsu.a->getfn)(apm);
		uniqarray(x);
		if (x)
		    arrfixenv(pm->node.nam, x);
	    }
	}
	if (usepm == 2)		/* do not change the PM_UNSET flag */
	    pm->node.flags = (pm->node.flags | (on & ~PM_READONLY)) & ~off;
	else {
	    /*
	     * Keep unset if using readonly in POSIX mode.
	     */
	    if (!(on & PM_READONLY) || !isset(POSIXBUILTINS))
		off |= PM_UNSET;
	    pm->node.flags = (pm->node.flags |
			      (on & ~PM_READONLY)) & ~off;
	}
	if (on & (PM_LEFT | PM_RIGHT_B | PM_RIGHT_Z)) {
	    if (typeset_setwidth(cname, pm, ops, on, 0))
		return NULL;
	}
	if (on & (PM_INTEGER | PM_EFLOAT | PM_FFLOAT)) {
	    if (typeset_setbase(cname, pm, ops, on, 0))
		return NULL;
	}
	if (!(pm->node.flags & (PM_ARRAY|PM_HASHED))) {
	    if (pm->node.flags & PM_EXPORTED) {
		if (!(pm->node.flags & PM_UNSET) && !pm->env && !ASG_VALUEP(asg))
		    addenv(pm, getsparam(pname));
	    } else if (pm->env && !(pm->node.flags & PM_HASHELEM))
		delenv(pm);
	    DPUTS(ASG_ARRAYP(asg), "BUG: typeset got array value where scalar expected");
	    if (altpm && !(pm->node.flags & PM_SPECIAL)) {
		struct tieddata* tdp = (struct tieddata *) pm->u.data;
		if (tdp) {
		    if (tdp->joinchar != joinchar && !asg->value.scalar) {
			/*
			 * Reassign the scalar to itself to do the splitting with
			 * the new joinchar
			 */
			tdp->joinchar = joinchar;
			if (!(pm = assignsparam(pname, ztrdup(getsparam(pname)), 0)))
			    return NULL;
		    }
		}
		else
		    DPUTS(!tdp, "BUG: no join character to update");
	    }
	    if (asg->value.scalar &&
		!(pm = assignsparam(pname, ztrdup(asg->value.scalar), 0)))
		return NULL;
	} else if (asg->flags & ASG_ARRAY) {
	    int flags = (asg->flags & ASG_KEY_VALUE) ? ASSPM_KEY_VALUE : 0;
	    if (!(pm = assignaparam(pname, asg->value.array ?
				 zlinklist2array(asg->value.array, 1) :
				 mkarray(NULL), flags)))
		return NULL;
	}
	if (errflag)
	    return NULL;
	pm->node.flags |= (on & PM_READONLY);
	if (OPT_ISSET(ops,'p'))
	    paramtab->printnode(&pm->node, PRINT_TYPESET);
	return pm;
    }

    if ((asg->flags & ASG_ARRAY) ?
	!(on & (PM_ARRAY|PM_HASHED)) :
	(asg->value.scalar && (on & (PM_ARRAY|PM_HASHED)))) {
	zerrnam(cname, "%s: inconsistent type for assignment", pname);
	return NULL;
    }

    /*
     * We're here either because we're creating a new parameter,
     * or we're adding a parameter at a different local level,
     * or we're converting the type of a parameter.  In the
     * last case only, we need to delete the old parameter.
     */
    if (tc) {
	/* Maintain existing readonly/exported status... */
	on |= ~off & (PM_READONLY|PM_EXPORTED) & pm->node.flags;
	/* ...but turn off existing readonly so we can delete it */
	pm->node.flags &= ~PM_READONLY;
	/*
	 * If we're just changing the type, we should keep the
	 * variable at the current level of localness.
	 */
	keeplocal = pm->level;
	/*
	 * Try to carry over a value, but not when changing from,
	 * to, or between non-scalar types.
	 *
	 * (We can do better now, but it does have user-visible
	 * implications.)
	 */
	if (!ASG_VALUEP(asg) && !((pm->node.flags|on) & (PM_ARRAY|PM_HASHED))) {
	    asg->value.scalar = dupstring(getsparam(pname));
	    asg->flags = 0;
	}
	/* pname may point to pm->nam which is about to disappear */
	pname = dupstring(pname);
	unsetparam_pm(pm, 0, 1);
    }

    if (newspecial != NS_NONE) {
	Param tpm, pm2;
	if ((pm->node.flags & PM_RESTRICTED) && isset(RESTRICTED)) {
	    zerrnam(cname, "%s: restricted", pname);
	    return pm;
	}
	if (pm->node.flags & PM_SINGLE) {
	    zerrnam(cname, "%s: can only have a single instance", pname);
	    return pm;
	}

	on |= pm->node.flags & PM_TIED;

	/*
	 * For specials, we keep the same struct but zero everything.
	 * Maybe it would be easier to create a new struct but copy
	 * the get/set methods.
	 */
	tpm = (Param) zshcalloc(sizeof *tpm);

	tpm->node.nam = pm->node.nam;
	if (pm->ename &&
	    (pm2 = (Param) paramtab->getnode(paramtab, pm->ename)) &&
	    pm2->level == locallevel) {
	    /* This is getting silly, but anyway:  if one of a path/PATH
	     * pair has already been made local at the current level, we
	     * have to make sure that the other one does not have its value
	     * saved:  since that comes from an internal variable it will
	     * already reflect the local value, so restoring it on exit
	     * would be wrong.
	     *
	     * This problem is also why we make sure we have a copy
	     * of the environment entry in tpm->env, rather than relying
	     * on the restored value to provide it.
	     */
	    tpm->node.flags = pm->node.flags | PM_NORESTORE;
	} else {
	    copyparam(tpm, pm, 1);
	}
	tpm->old = pm->old;
	tpm->level = pm->level;
	tpm->base = pm->base;
	tpm->width = pm->width;
	if (pm->env)
	    delenv(pm);
	tpm->env = NULL;

	pm->old = tpm;
	/*
	 * The remaining on/off flags should be harmless to use,
	 * because we've checked for unpleasant surprises above.
	 */
	pm->node.flags = (PM_TYPE(pm->node.flags) | on | PM_SPECIAL) & ~off;
	/*
	 * Readonlyness of special parameters must be preserved.
	 */
	pm->node.flags |= tpm->node.flags & PM_READONLY;
	if (newspecial == NS_SECONDS) {
	    /* We save off the raw internal value of the SECONDS var */
	    tpm->u.dval = getrawseconds();
	    setsecondstype(pm, on, off);
	}

	/*
	 * Final tweak: if we've turned on one of the flags with
	 * numbers, we should use the appropriate integer.
	 */
	if (on & (PM_LEFT|PM_RIGHT_B|PM_RIGHT_Z)) {
	    if (typeset_setwidth(cname, pm, ops, on, 1))
		return NULL;
	}
	if (on & (PM_INTEGER|PM_EFLOAT|PM_FFLOAT)) {
	    if (typeset_setbase(cname, pm, ops, on, 1))
		return NULL;
	}
    } else if ((subscript = strchr(pname, '['))) {
	if (on & PM_READONLY) {
	    zerrnam(cname,
		    "%s: can't create readonly array elements", pname);
	    return NULL;
	} else if ((on & PM_LOCAL) && locallevel) {
	    *subscript = 0;
	    pm = (Param) (paramtab == realparamtab ?
			  /* getnode2() to avoid autoloading */
			  paramtab->getnode2(paramtab, pname) :
			  paramtab->getnode(paramtab, pname));
	    *subscript = '[';
	    if (!pm || pm->level != locallevel) {
		zerrnam(cname,
			"%s: can't create local array elements", pname);
		return NULL;
	    }
	}
	if (PM_TYPE(on) == PM_SCALAR && !ASG_ARRAYP(asg)) {
	    /*
	     * This will either complain about bad identifiers, or will set
	     * a hash element or array slice.  This once worked by accident,
	     * creating a stray parameter along the way via createparam(),
	     * now called below in the isident() branch.
	     */
	    if (!(pm = assignsparam(
		      pname,
		      ztrdup(asg->value.scalar ? asg->value.scalar : ""), 0)))
		return NULL;
	    dont_set = 1;
	    asg->flags = 0;
	    keeplocal = 0;
	    on = pm->node.flags;
	} else if (PM_TYPE(on) == PM_ARRAY && ASG_ARRAYP(asg)) {
	    int flags = (asg->flags & ASG_KEY_VALUE) ? ASSPM_KEY_VALUE : 0;
	    if (!(pm = assignaparam(pname, asg->value.array ?
				    zlinklist2array(asg->value.array, 1) :
				    mkarray(NULL), flags)))
		return NULL;
	    dont_set = 1;
	    keeplocal = 0;
	    on = pm->node.flags;
	} else {
	    zerrnam(cname,
		    "%s: inconsistent array element or slice assignment", pname);
	    return NULL;
	}
    }
    /*
     * As we can hide existing parameters, we allow a name if
     * it's not a normal identifier but is one of the special
     * set found in the parameter table.  The second test is
     * because we can set individual positional parameters;
     * however "0" is not a positional parameter and is OK.
     *
     * It would be neater to extend isident() and be clearer
     * about where we allow various parameter types.  It's
     * not entirely clear to me isident() should reject
     * specially named parameters given that it accepts digits.
     */
    else if ((isident(pname) || paramtab->getnode(paramtab, pname))
	     && (!idigit(*pname) || !strcmp(pname, "0"))) {
	/*
	 * Create a new node for a parameter with the flags in `on' minus the
	 * readonly flag
	 */
	pm = createparam(pname, on & ~PM_READONLY);
	if (!pm) {
	    if (on & (PM_LEFT | PM_RIGHT_B | PM_RIGHT_Z |
		      PM_INTEGER | PM_EFLOAT | PM_FFLOAT))
		zerrnam(cname, "can't change variable attribute: %s", pname);
	    return NULL;
	}
	if (on & (PM_LEFT | PM_RIGHT_B | PM_RIGHT_Z)) {
	    if (typeset_setwidth(cname, pm, ops, on, 0)) {
		unsetparam_pm(pm, 0, 1);
		return NULL;
	    }
	}
	if (on & (PM_INTEGER | PM_EFLOAT | PM_FFLOAT)) {
	    if (typeset_setbase(cname, pm, ops, on, 0)) {
		unsetparam_pm(pm, 0, 1);
		return NULL;
	    }
	}
    } else {
	if (idigit(*pname))
	    zerrnam(cname, "not an identifier: %s", pname);
	else
	    zerrnam(cname, "not valid in this context: %s", pname);
	return NULL;
    }

    if (altpm && PM_TYPE(pm->node.flags) == PM_SCALAR && !(pm->node.flags & PM_SPECIAL)) {
	/*
	 * It seems safer to set this here than in createparam(),
	 * to make sure we only ever use the colonarr functions
	 * when u.data is correctly set.
	 */
	struct tieddata *tdp = (struct tieddata *)
	    zalloc(sizeof(struct tieddata));
	if (!tdp) {
	    unsetparam_pm(pm, 0, 1);
	    return NULL;
	}
	tdp->joinchar = joinchar;
	tdp->arrptr = &altpm->u.arr;

	pm->gsu.s = &tiedarr_gsu;
	pm->u.data = tdp;
    }

    if (keeplocal)
	pm->level = keeplocal;
    else if (on & PM_LOCAL)
	pm->level = locallevel;
    if (ASG_VALUEP(asg) && !dont_set) {
	Param ipm = pm;
	if (pm->node.flags & (PM_ARRAY|PM_HASHED)) {
	    char **arrayval;
	    int flags = (asg->flags & ASG_KEY_VALUE) ? ASSPM_KEY_VALUE : 0;
	    if (!ASG_ARRAYP(asg)) {
		/*
		 * Attempt to assign a scalar value to an array.
		 * This can happen if the array is special.
		 * We'll be lenient and guess what the user meant.
		 * This is how normal assignment works.
		 */
		if (*asg->value.scalar) {
		    /* Array with one value */
		    arrayval = mkarray(ztrdup(asg->value.scalar));
		} else {
		    /* Empty array */
		    arrayval = mkarray(NULL);
		}
	    } else if (asg->value.array)
		arrayval = zlinklist2array(asg->value.array, 1);
	    else
		arrayval = mkarray(NULL);
	    if (!(pm=assignaparam(pname, arrayval, flags)))
		return NULL;
	} else {
	    DPUTS(ASG_ARRAYP(asg), "BUG: inconsistent array value for scalar");
	    if (!(pm = assignsparam(pname, ztrdup(asg->value.scalar), 0)))
		return NULL;
	}
	if (pm != ipm) {
	    DPUTS(ipm->node.flags != pm->node.flags,
		  "BUG: parameter recreated with wrong flags");
	    unsetparam_pm(ipm, 0, 1);
	}
    } else if (newspecial != NS_NONE &&
	       !(pm->old->node.flags & (PM_NORESTORE|PM_READONLY))) {
	/*
	 * We need to use the special setting function to re-initialise
	 * the special parameter to empty.
	 */
	switch (PM_TYPE(pm->node.flags)) {
	case PM_SCALAR:
	    pm->gsu.s->setfn(pm, ztrdup(""));
	    break;
	case PM_INTEGER:
	    /*
	     * Restricted integers are dangerous to initialize to 0,
	     * so don't do that.
	     */
	    if (!(pm->old->node.flags & PM_RESTRICTED))
		pm->gsu.i->setfn(pm, 0);
	    break;
	case PM_EFLOAT:
	case PM_FFLOAT:
	    pm->gsu.f->setfn(pm, 0.0);
	    break;
	case PM_ARRAY:
	    pm->gsu.a->setfn(pm, mkarray(NULL));
	    break;
	case PM_HASHED:
	    pm->gsu.h->setfn(pm, newparamtable(17, pm->node.nam));
	    break;
	}
    }
    pm->node.flags |= (on & PM_READONLY);
    DPUTS(OPT_ISSET(ops,'p'), "BUG: -p not handled");

    return pm;
}

/*
 * declare, export, float, integer, local, readonly, typeset
 *
 * Note the difference in interface from most builtins, covered by the
 * BINF_ASSIGN builtin flag.  This is only made use of by builtins
 * called by reserved word, which only covers declare, local, readonly
 * and typeset.  Otherwise assigns is NULL.
 */

/**/
mod_export int
bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func)
{
    Param pm;
    Asgment asg;
    Patprog pprog;
    char *optstr = TYPESET_OPTSTR;
    int on = 0, off = 0, roff, bit = PM_ARRAY;
    int i;
    int returnval = 0, printflags = 0;
    int hasargs;

    /* hash -f is really the builtin `functions' */
    if (OPT_ISSET(ops,'f'))
	return bin_functions(name, argv, ops, func);

    /* POSIX handles "readonly" specially */
    if (func == BIN_READONLY && isset(POSIXBUILTINS) && !OPT_PLUS(ops, 'g'))
	ops->ind['g'] = 1;

    /* Translate the options into PM_* flags.   *
     * Unfortunately, this depends on the order *
     * these flags are defined in zsh.h         */
    for (; *optstr; optstr++, bit <<= 1)
    {
	int optval = STOUC(*optstr);
	if (OPT_MINUS(ops,optval))
	    on |= bit;
	else if (OPT_PLUS(ops,optval))
	    off |= bit;
    }
    roff = off;

    /* Sanity checks on the options.  Remove conflicting options. */
    if (on & PM_FFLOAT) {
	off |= PM_UPPER | PM_ARRAY | PM_HASHED | PM_INTEGER | PM_EFLOAT;
	/* Allow `float -F' to work even though float sets -E by default */
	on &= ~PM_EFLOAT;
    }
    if (on & PM_EFLOAT)
	off |= PM_UPPER | PM_ARRAY | PM_HASHED | PM_INTEGER | PM_FFLOAT;
    if (on & PM_INTEGER)
	off |= PM_UPPER | PM_ARRAY | PM_HASHED | PM_EFLOAT | PM_FFLOAT;
    /*
     * Allowing -Z with -L is a feature: left justify, suppressing
     * leading zeroes.
     */
    if (on & (PM_LEFT|PM_RIGHT_Z))
	off |= PM_RIGHT_B;
    if (on & PM_RIGHT_B)
	off |= PM_LEFT | PM_RIGHT_Z;
    if (on & PM_UPPER)
	off |= PM_LOWER;
    if (on & PM_LOWER)
	off |= PM_UPPER;
    if (on & PM_HASHED)
	off |= PM_ARRAY;
    if (on & PM_TIED)
	off |= PM_INTEGER | PM_EFLOAT | PM_FFLOAT | PM_ARRAY | PM_HASHED;

    on &= ~off;

    queue_signals();

    /* Given no arguments, list whatever the options specify. */
    if (OPT_ISSET(ops,'p')) {

	if (isset(POSIXBUILTINS) && SHELL_EMULATION() != EMULATE_KSH) {
	  if (func == BIN_EXPORT)
	    printflags |= PRINT_POSIX_EXPORT;
	  else if (func == BIN_READONLY)
	    printflags |= PRINT_POSIX_READONLY;
	  else
	    printflags |= PRINT_TYPESET;
	} else
	    printflags |= PRINT_TYPESET;

	if (OPT_HASARG(ops,'p')) {
	    char *eptr;
	    int pflag = (int)zstrtol(OPT_ARG(ops,'p'), &eptr, 10);
	    if (pflag == 1 && !*eptr)
		printflags |= PRINT_LINE;
	    else if (pflag || *eptr) {
		zwarnnam(name, "bad argument to -p: %s", OPT_ARG(ops,'p'));
		unqueue_signals();
		return 1;
	    }
	    /* -p0 treated as -p for consistency */
	}
    }
    hasargs = *argv != NULL || (assigns && firstnode(assigns));
    if (!hasargs) {
	int exclude = 0;
	if (!OPT_ISSET(ops,'p')) {
	    if (!(on|roff))
		printflags |= PRINT_TYPE;
	    if (roff || OPT_ISSET(ops,'+'))
		printflags |= PRINT_NAMEONLY;
	} else if (printflags & (PRINT_POSIX_EXPORT|PRINT_POSIX_READONLY)) {
	    /*
	     * For POSIX export/readonly, exclude non-scalars unless
	     * explicitly requested.
	     */
	    exclude = (PM_ARRAY|PM_HASHED) & ~(on|roff);
	}
	scanhashtable(paramtab, 1, on|roff, exclude, paramtab->printnode, printflags);
	unqueue_signals();
	return 0;
    }

    if (!(OPT_ISSET(ops,'g') || OPT_ISSET(ops,'x') || OPT_ISSET(ops,'m')) ||
	OPT_PLUS(ops,'g') || *name == 'l' ||
	(!isset(GLOBALEXPORT) && !OPT_ISSET(ops,'g')))
	on |= PM_LOCAL;

    if ((on & PM_TIED) && !OPT_ISSET(ops, 'p')) {
	Param apm;
	struct asgment asg0, asg2;
	char *oldval = NULL, *joinstr;
	int joinchar, nargs;
	int already_tied = 0;

	if (OPT_ISSET(ops,'m')) {
	    zwarnnam(name, "incompatible options for -T");
	    unqueue_signals();
	    return 1;
	}
	on &= ~off;
	nargs = arrlen(argv) + (assigns ? countlinknodes(assigns) : 0);
	if (nargs < 2) {
	    zwarnnam(name, "-T requires names of scalar and array");
	    unqueue_signals();
	    return 1;
	}
	if (nargs > 3) {
	    zwarnnam(name, "too many arguments for -T");
	    unqueue_signals();
	    return 1;
	}

	if (!(asg = getasg(&argv, assigns))) {
	    unqueue_signals();
	    return 1;
	}
	asg0 = *asg;
	if (ASG_ARRAYP(&asg0)) {
	    unqueue_signals();
	    zwarnnam(name, "first argument of tie must be scalar: %s",
		     asg0.name);
	    return 1;
	}

	if (!(asg = getasg(&argv, assigns))) {
	    unqueue_signals();
	    return 1;
	}
	if (!ASG_ARRAYP(asg) && asg->value.scalar) {
	    unqueue_signals();
	    zwarnnam(name, "second argument of tie must be array: %s",
		     asg->name);
	    return 1;
	}

	if (!strcmp(asg0.name, asg->name)) {
	    unqueue_signals();
	    zerrnam(name, "can't tie a variable to itself: %s", asg0.name);
	    return 1;
	}
	if (strchr(asg0.name, '[') || strchr(asg->name, '[')) {
	    unqueue_signals();
	    zerrnam(name, "can't tie array elements: %s", asg0.name);
	    return 1;
	}
	if (ASG_VALUEP(asg) && ASG_VALUEP(&asg0)) {
	    unqueue_signals();
	    zerrnam(name, "only one tied parameter can have value: %s", asg0.name);
	    return 1;
	}

	/*
	 * Third argument, if given, is character used to join
	 * the elements of the array in the scalar.
	 */
	if (*argv)
	    joinstr = *argv;
	else if (assigns && firstnode(assigns)) {
	    Asgment nextasg = (Asgment)firstnode(assigns);
	    if (ASG_ARRAYP(nextasg) || ASG_VALUEP(nextasg)) {
		zwarnnam(name, "third argument of tie must be join character");
		unqueue_signals();
		return 1;
	    }
	    joinstr = nextasg->name;
	} else
	    joinstr = NULL;
	if (!joinstr)
	    joinchar = ':';
	else if (!*joinstr)
	    joinchar = 0;
	else if (*joinstr == Meta)
	    joinchar = joinstr[1] ^ 32;
	else
	    joinchar = *joinstr;

	pm = (Param) paramtab->getnode(paramtab, asg0.name);
	apm = (Param) paramtab->getnode(paramtab, asg->name);

	if (pm && (pm->node.flags & (PM_SPECIAL|PM_TIED)) == (PM_SPECIAL|PM_TIED)) {
	    /*
	     * Only allow typeset -T on special tied parameters if the tied
	     * parameter and join char are the same
	     */
	    if (strcmp(pm->ename, asg->name) || !(apm->node.flags & PM_SPECIAL)) {
		zwarnnam(name, "%s special parameter can only be tied to special parameter %s", asg0.name, pm->ename);
		unqueue_signals();
		return 1;
	    }
	    if (joinchar != ':') {
		zwarnnam(name, "cannot change the join character of special tied parameters");
		unqueue_signals();
		return 1;
	    }
	    already_tied = 1;
	} else if (apm && (apm->node.flags & (PM_SPECIAL|PM_TIED)) == (PM_SPECIAL|PM_TIED)) {
	    /*
	     * For the array variable, this covers attempts to tie the
	     * array to a different scalar or to the scalar after it has
	     * been made non-special
	     */
	    zwarnnam(name, "%s special parameter can only be tied to special parameter %s", asg->name, apm->ename);
	    unqueue_signals();
	    return 1;
	} else if (pm) {
	    if (!(pm->node.flags & PM_UNSET)
		&& (locallevel == pm->level || !(on & PM_LOCAL))) {
		if (pm->node.flags & PM_TIED) {
		    if (PM_TYPE(pm->node.flags) != PM_SCALAR) {
			zwarnnam(name, "already tied as non-scalar: %s", asg0.name);
			unqueue_signals();
			return 1;
		    } else if (!strcmp(asg->name, pm->ename)) {
			already_tied = 1;
		    } else {
			zwarnnam(name, "can't tie already tied scalar: %s",
				 asg0.name);
			unqueue_signals();
			return 1;
		    }
		} else {
		    /*
		     * Variable already exists in the current scope but is not tied.
		     * We're preserving its value and export attribute but no other
		     * attributes upon converting to "tied".
		     */
		    if (!asg0.value.scalar && !asg->value.array &&
			!(PM_TYPE(pm->node.flags) & (PM_ARRAY|PM_HASHED)))
			oldval = ztrdup(getsparam(asg0.name));
		    on |= (pm->node.flags & ~roff) & PM_EXPORTED;
		}
	    }
	}
	if (already_tied) {
	    int ret;
	    /*
	     * If already tied, we still need to call typeset_single on
	     * both the array and colonarray, if only to update the attributes
	     * of both, and of course to set the new value if one is provided
	     * for either of them.
	     */
	    ret = !(typeset_single(name, asg0.name, pm,
				   func, on, off, roff, &asg0, apm,
				   ops, joinchar) &&
		    typeset_single(name, asg->name, apm,
				   func, (on | PM_ARRAY) & ~PM_EXPORTED,
				   off & ~PM_ARRAY, roff, asg, NULL, ops, 0)
		   );
	    unqueue_signals();
	    return ret;
	}
	/*
	 * Create the tied array; this is normal except that
	 * it has the PM_TIED flag set.  Do it first because
	 * we need the address.
	 *
	 * Don't attempt to set it yet, it's too early
	 * to be exported properly.
	 */
	asg2.name = asg->name;
	asg2.flags = 0;
	asg2.value.array = (LinkList)0;
	if (!(apm=typeset_single(name, asg->name,
				 (Param)paramtab->getnode(paramtab,
							  asg->name),
				 func, (on | PM_ARRAY) & ~PM_EXPORTED,
				 off, roff, &asg2, NULL, ops, 0))) {
	    if (oldval)
		zsfree(oldval);
	    unqueue_signals();
	    return 1;
	}
	/*
	 * Create the tied colonarray.  We make it as a normal scalar
	 * and fix up the oddities later.
	 */
	if (!(pm=typeset_single(name, asg0.name, pm,
				func, on, off, roff, &asg0, apm,
				ops, joinchar))) {
	    if (oldval)
		zsfree(oldval);
	    unsetparam_pm(apm, 1, 1);
	    unqueue_signals();
	    return 1;
	}

	/*
	 * pm->ename is only deleted when the struct is, so
	 * we need to free it here if it already exists.
	 */
	if (pm->ename)
	    zsfree(pm->ename);
	pm->ename = ztrdup(asg->name);
	if (apm->ename)
	    zsfree(apm->ename);
	apm->ename = ztrdup(asg0.name);
	if (asg->value.array) {
	    int flags = (asg->flags & ASG_KEY_VALUE) ? ASSPM_KEY_VALUE : 0;
	    assignaparam(asg->name, zlinklist2array(asg->value.array, 1), flags);
	} else if (oldval)
	    assignsparam(asg0.name, oldval, 0);
	unqueue_signals();

	return 0;
    }
    if (off & PM_TIED) {
	unqueue_signals();
	zerrnam(name, "use unset to remove tied variables");
	return 1;
    }

    /* With the -m option, treat arguments as glob patterns */
    if (OPT_ISSET(ops,'m')) {
	if (!OPT_ISSET(ops,'p')) {
	    if (!(on|roff))
		printflags |= PRINT_TYPE;
	    if (!on)
		printflags |= PRINT_NAMEONLY;
	}

	while ((asg = getasg(&argv, assigns))) {
	    LinkList pmlist = newlinklist();
	    LinkNode pmnode;

	    tokenize(asg->name);   /* expand argument */
	    if (!(pprog = patcompile(asg->name, 0, NULL))) {
		untokenize(asg->name);
		zwarnnam(name, "bad pattern : %s", asg->name);
		returnval = 1;
		continue;
	    }
	    if (OPT_PLUS(ops,'m') && !ASG_VALUEP(asg)) {
		scanmatchtable(paramtab, pprog, 1, on|roff, 0,
			       paramtab->printnode, printflags);
		continue;
	    }
	    /*
	     * Search through the parameter table and change all parameters
	     * matching the glob pattern to have these flags and/or value.
	     * Bad news:  if the parameter gets altered, e.g. by
	     * a type conversion, then paramtab can be shifted around,
	     * so we need to store the parameters to alter on a separate
	     * list for later use.
	     */
	    for (i = 0; i < paramtab->hsize; i++) {
		for (pm = (Param) paramtab->nodes[i]; pm;
		     pm = (Param) pm->node.next) {
		    if (((pm->node.flags & PM_RESTRICTED) && isset(RESTRICTED)) ||
			(pm->node.flags & PM_UNSET))
			continue;
		    if (pattry(pprog, pm->node.nam))
			addlinknode(pmlist, pm);
		}
	    }
	    for (pmnode = firstnode(pmlist); pmnode; incnode(pmnode)) {
		pm = (Param) getdata(pmnode);
		if (!typeset_single(name, pm->node.nam, pm, func, on, off, roff,
				    asg, NULL, ops, 0))
		    returnval = 1;
	    }
	}
	unqueue_signals();
	return returnval;
    }

    /* Take arguments literally.  Don't glob */
    while ((asg = getasg(&argv, assigns))) {
	HashNode hn = (paramtab == realparamtab ?
		       /* getnode2() to avoid autoloading */
		       paramtab->getnode2(paramtab, asg->name) :
		       paramtab->getnode(paramtab, asg->name));
	if (OPT_ISSET(ops,'p')) {
	    if (hn)
		paramtab->printnode(hn, printflags);
	    else {
		zwarnnam(name, "no such variable: %s", asg->name);
		returnval = 1;
	    }
	    continue;
	}
	if (!typeset_single(name, asg->name, (Param)hn,
			    func, on, off, roff, asg, NULL,
			    ops, 0))
	    returnval = 1;
    }
    unqueue_signals();
    return returnval;
}

/* Helper for bin_functions() when run as "autoload -X" */

/**/
int
eval_autoload(Shfunc shf, char *name, Options ops, int func)
{
    if (!(shf->node.flags & PM_UNDEFINED))
	return 1;

    if (shf->funcdef) {
	freeeprog(shf->funcdef);
	shf->funcdef = &dummy_eprog;
    }
    if (OPT_MINUS(ops,'X')) {
	char *fargv[3];
	fargv[0] = quotestring(name, QT_SINGLE_OPTIONAL);
	fargv[1] = "\"$@\"";
	fargv[2] = 0;
	shf->funcdef = mkautofn(shf);
	return bin_eval(name, fargv, ops, func);
    }

    return !loadautofn(shf, (OPT_ISSET(ops,'k') ? 2 :
			     (OPT_ISSET(ops,'z') ? 0 : 1)), 1,
		       OPT_ISSET(ops,'d'));
}

/* Helper for bin_functions() for -X and -r options */

/**/
static int
check_autoload(Shfunc shf, char *name, Options ops, int func)
{
    if (OPT_ISSET(ops,'X'))
    {
	return eval_autoload(shf, name, ops, func);
    }
    if ((OPT_ISSET(ops,'r') || OPT_ISSET(ops,'R')) &&
	(shf->node.flags & PM_UNDEFINED))
    {
	char *dir_path;
	if (shf->filename && (shf->node.flags & PM_LOADDIR)) {
	    char *spec_path[2];
	    spec_path[0] = shf->filename;
	    spec_path[1] = NULL;
	    if (getfpfunc(shf->node.nam, NULL, &dir_path, spec_path, 1)) {
		/* shf->filename is already correct. */
		return 0;
	    }
	    if (!OPT_ISSET(ops,'d')) {
		if (OPT_ISSET(ops,'R')) {
		    zerr("%s: function definition file not found",
			 shf->node.nam);
		    return 1;
		}
		return 0;
	    }
	}
	if (getfpfunc(shf->node.nam, NULL, &dir_path, NULL, 1)) {
	    dircache_set(&shf->filename, NULL);
	    if (*dir_path != '/') {
		dir_path = zhtricat(metafy(zgetcwd(), -1, META_HEAPDUP),
				    "/", dir_path);
		dir_path = xsymlink(dir_path, 1);
	    }
	    dircache_set(&shf->filename, dir_path);
	    shf->node.flags |= PM_LOADDIR;
	    return 0;
	}
	if (OPT_ISSET(ops,'R')) {
	    zerr("%s: function definition file not found",
		 shf->node.nam);
	    return 1;
	}
	/* with -r, we don't flag an error, just let it be found later. */
    }
    return 0;
}

/* List a user-defined math function. */
static void
listusermathfunc(MathFunc p)
{
    int showargs;

    if (p->module)
	showargs = 3;
    else if (p->maxargs != (p->minargs ? p->minargs : -1))
	showargs = 2;
    else if (p->minargs)
	showargs = 1;
    else
	showargs = 0;

    printf("functions -M%s %s", (p->flags & MFF_STR) ? "s" : "", p->name);
    if (showargs) {
	printf(" %d", p->minargs);
	showargs--;
    }
    if (showargs) {
	printf(" %d", p->maxargs);
	showargs--;
    }
    if (showargs) {
	/*
	 * function names are not required to consist of ident characters
	 */
	putchar(' ');
	quotedzputs(p->module, stdout);
	showargs--;
    }
    putchar('\n');
}


static void
add_autoload_function(Shfunc shf, char *funcname)
{
    char *nam;
    if (*funcname == '/' && funcname[1] &&
	(nam = strrchr(funcname, '/')) && nam[1] &&
	(shf->node.flags & PM_UNDEFINED)) {
	char *dir;
	nam = strrchr(funcname, '/');
	if (nam == funcname) {
	    dir = "/";
	} else {
	    *nam++ = '\0';
	    dir = funcname;
	}
	dircache_set(&shf->filename, NULL);
	dircache_set(&shf->filename, dir);
	shf->node.flags |= PM_LOADDIR;
	shf->node.flags |= PM_ABSPATH_USED;
	shfunctab->addnode(shfunctab, ztrdup(nam), shf);
    } else {
        Shfunc shf2;
        Funcstack fs;
        const char *calling_f = NULL;
        char buf[PATH_MAX+1];

        /* Find calling function */
        for (fs = funcstack; fs; fs = fs->prev) {
            if (fs->tp == FS_FUNC && fs->name && (!shf->node.nam || 0 != strcmp(fs->name,shf->node.nam))) {
                calling_f = fs->name;
                break;
            }
        }

        /* Get its directory */
        if (calling_f) {
            /* Should contain load directory, and be loaded via absolute path */
            if ((shf2 = (Shfunc) shfunctab->getnode2(shfunctab, calling_f))
                    && (shf2->node.flags & PM_LOADDIR) && (shf2->node.flags & PM_ABSPATH_USED)
                    && shf2->filename)
            {
                if (strlen(shf2->filename) + strlen(funcname) + 1 < PATH_MAX)
                {
                    sprintf(buf, "%s/%s", shf2->filename, funcname);
                    /* Set containing directory if the function file
                     * exists (do normal FPATH processing otherwise) */
                    if (!access(buf, R_OK)) {
                        dircache_set(&shf->filename, NULL);
                        dircache_set(&shf->filename, shf2->filename);
                        shf->node.flags |= PM_LOADDIR;
                        shf->node.flags |= PM_ABSPATH_USED;
                    }
                }
            }
        }

	shfunctab->addnode(shfunctab, ztrdup(funcname), shf);
    }
}

/* Display or change the attributes of shell functions.   *
 * If called as autoload, it will define a new autoloaded *
 * (undefined) shell function.                            */

/**/
int
bin_functions(char *name, char **argv, Options ops, int func)
{
    Patprog pprog;
    Shfunc shf;
    int i, returnval = 0;
    int on = 0, off = 0, pflags = 0, roff, expand = 0;

    /* Do we have any flags defined? */
    if (OPT_PLUS(ops,'u'))
	off |= PM_UNDEFINED;
    else if (OPT_MINUS(ops,'u') || OPT_ISSET(ops,'X'))
	on |= PM_UNDEFINED;
    if (OPT_MINUS(ops,'U'))
	on |= PM_UNALIASED|PM_UNDEFINED;
    else if (OPT_PLUS(ops,'U'))
	off |= PM_UNALIASED;
    if (OPT_MINUS(ops,'t'))
	on |= PM_TAGGED;
    else if (OPT_PLUS(ops,'t'))
	off |= PM_TAGGED;
    if (OPT_MINUS(ops,'T'))
	on |= PM_TAGGED_LOCAL;
    else if (OPT_PLUS(ops,'T'))
	off |= PM_TAGGED_LOCAL;
    if (OPT_MINUS(ops,'W'))
	on |= PM_WARNNESTED;
    else if (OPT_PLUS(ops,'W'))
	off |= PM_WARNNESTED;
    roff = off;
    if (OPT_MINUS(ops,'z')) {
	on |= PM_ZSHSTORED;
	off |= PM_KSHSTORED;
    } else if (OPT_PLUS(ops,'z')) {
	off |= PM_ZSHSTORED;
	roff |= PM_ZSHSTORED;
    }
    if (OPT_MINUS(ops,'k')) {
	on |= PM_KSHSTORED;
	off |= PM_ZSHSTORED;
    } else if (OPT_PLUS(ops,'k')) {
	off |= PM_KSHSTORED;
	roff |= PM_KSHSTORED;
    }
    if (OPT_MINUS(ops,'d')) {
	on |= PM_CUR_FPATH;
	off |= PM_CUR_FPATH;
    } else if (OPT_PLUS(ops,'d')) {
	off |= PM_CUR_FPATH;
	roff |= PM_CUR_FPATH;
    }

    if ((off & PM_UNDEFINED) || (OPT_ISSET(ops,'k') && OPT_ISSET(ops,'z')) ||
	(OPT_ISSET(ops,'x') && !OPT_HASARG(ops,'x')) ||
	(OPT_MINUS(ops,'X') && (OPT_ISSET(ops,'m') || !scriptname)) ||
	(OPT_ISSET(ops,'c') && (OPT_ISSET(ops,'x') || OPT_ISSET(ops,'X') ||
				OPT_ISSET(ops,'m')))) {
	zwarnnam(name, "invalid option(s)");
	return 1;
    }

    if (OPT_ISSET(ops,'c')) {
	Shfunc newsh;
	if (!*argv || !argv[1] || argv[2]) {
	    zwarnnam(name, "-c: requires two arguments");
	    return 1;
	}
	shf = (Shfunc) shfunctab->getnode(shfunctab, *argv);
	if (!shf) {
	    zwarnnam(name, "no such function: %s", *argv);
	    return 1;
	}
	if (shf->node.flags & PM_UNDEFINED) {
	    if (shf->funcdef) {
		freeeprog(shf->funcdef);
		shf->funcdef = &dummy_eprog;
	    }
	    shf = loadautofn(shf, 1, 0, 0);
	    if (!shf)
		return 1;
	}
	newsh = zalloc(sizeof(*newsh));
	memcpy(newsh, shf, sizeof(*newsh));
	if (newsh->node.flags & PM_LOADDIR) {
	    /* Expand original location of autoloaded file */
	    newsh->node.flags &= ~PM_LOADDIR;
	    newsh->filename = tricat(shf->filename, "/", shf->node.nam);
	} else
	    newsh->filename = ztrdup(shf->filename);
	newsh->funcdef->nref++;
	if (newsh->redir)
	    newsh->redir->nref++;
	if (shf->sticky)
	    newsh->sticky = sticky_emulation_dup(sticky, 0);
	shfunctab->addnode(shfunctab, ztrdup(argv[1]), &newsh->node);
	return 0;
    }

    if (OPT_ISSET(ops,'x')) {
	char *eptr;
	expand = (int)zstrtol(OPT_ARG(ops,'x'), &eptr, 10);
	if (*eptr) {
	    zwarnnam(name, "number expected after -x");
	    return 1;
	}
	if (expand == 0)	/* no indentation at all */
	    expand = -1;
    }

    if (OPT_PLUS(ops,'f') || roff || OPT_ISSET(ops,'+'))
	pflags |= PRINT_NAMEONLY;

    if (OPT_MINUS(ops,'M') || OPT_PLUS(ops,'M')) {
	MathFunc p, q, prev;
	/*
	 * Add/remove/list function as mathematical.
	 */
	if (on || off || pflags || OPT_ISSET(ops,'X') || OPT_ISSET(ops,'u')
	    || OPT_ISSET(ops,'U') || OPT_ISSET(ops,'w')) {
	    zwarnnam(name, "invalid option(s)");
	    return 1;
	}
	if (!*argv) {
	    /* List functions. */
	    queue_signals();
	    for (p = mathfuncs; p; p = p->next)
		if (p->flags & MFF_USERFUNC)
		    listusermathfunc(p);
	    unqueue_signals();
	} else if (OPT_ISSET(ops,'m')) {
	    /* List matching functions. */
	    for (; *argv; argv++) {
		queue_signals();
		tokenize(*argv);
		if ((pprog = patcompile(*argv, PAT_STATIC, 0))) {
		    for (p = mathfuncs, q = NULL; p; q = p) {
			MathFunc next;
			do {
			    next = NULL;
			    if ((p->flags & MFF_USERFUNC) &&
				pattry(pprog, p->name)) {
				if (OPT_PLUS(ops,'M')) {
				    next = p->next;
				    removemathfunc(q, p);
				    p = next;
				} else
				    listusermathfunc(p);
			    }
			    /* if we deleted one, retry with the new p */
			} while (next);
			if (p)
			    p = p->next;
		    }
		} else {
		    untokenize(*argv);
		    zwarnnam(name, "bad pattern : %s", *argv);
		    returnval = 1;
		}
		unqueue_signals();
	    }
	} else if (OPT_PLUS(ops,'M')) {
	    /* Delete functions. -m is allowed but is handled above. */
	    for (; *argv; argv++) {
		queue_signals();
		for (p = mathfuncs, q = NULL; p; q = p, p = p->next) {
		    if (!strcmp(p->name, *argv)) {
			if (!(p->flags & MFF_USERFUNC)) {
			    zwarnnam(name, "+M %s: is a library function",
				     *argv);
			    returnval = 1;
			    break;
			}
			removemathfunc(q, p);
			break;
		    }
		}
		unqueue_signals();
	    }
	} else {
	    /* Add a function */
	    int minargs, maxargs;
	    char *funcname = *argv++;
	    char *modname = NULL;
	    char *ptr;

	    if (OPT_ISSET(ops,'s')) {
		minargs = maxargs = 1;
	    } else {
		minargs = 0;
		maxargs = -1;
	    }

	    ptr = itype_end(funcname, IIDENT, 0);
	    if (idigit(*funcname) || funcname == ptr || *ptr) {
		zwarnnam(name, "-M %s: bad math function name", funcname);
		return 1;
	    }

	    if (*argv) {
		minargs = (int)zstrtol(*argv, &ptr, 0);
		if (minargs < 0 || *ptr) {
		    zwarnnam(name, "-M: invalid min number of arguments: %s",
			     *argv);
		    return 1;
		}
		if (OPT_ISSET(ops,'s') && minargs != 1) {
		    zwarnnam(name, "-Ms: must take a single string argument");
		    return 1;
		}
		maxargs = minargs;
		argv++;
	    }
	    if (*argv) {
		maxargs = (int)zstrtol(*argv, &ptr, 0);
		if (maxargs < -1 ||
		    (maxargs != -1 && maxargs < minargs) ||
		    *ptr) {
		    zwarnnam(name,
			     "-M: invalid max number of arguments: %s",
			     *argv);
		    return 1;
		}
		if (OPT_ISSET(ops,'s') && maxargs != 1) {
		    zwarnnam(name, "-Ms: must take a single string argument");
		    return 1;
		}
		argv++;
	    }
	    if (*argv)
		modname = *argv++;
	    if (*argv) {
		zwarnnam(name, "-M: too many arguments");
		return 1;
	    }

	    p = (MathFunc)zshcalloc(sizeof(struct mathfunc));
	    p->name = ztrdup(funcname);
	    p->flags = MFF_USERFUNC;
	    if (OPT_ISSET(ops,'s'))
		p->flags |= MFF_STR;
	    p->module = modname ? ztrdup(modname) : NULL;
	    p->minargs = minargs;
	    p->maxargs = maxargs;

	    queue_signals();
	    for (q = mathfuncs, prev = NULL; q; prev = q, q = q->next) {
		if (!strcmp(q->name, funcname)) {
		    removemathfunc(prev, q);
		    break;
		}
	    }

	    p->next = mathfuncs;
	    mathfuncs = p;
	    unqueue_signals();
	}

	return returnval;
    }

    if (OPT_MINUS(ops,'X')) {
	Funcstack fs;
	char *funcname = NULL;
	int ret;
	if (*argv && argv[1]) {
	    zwarnnam(name, "-X: too many arguments");
	    return 1;
	}
	queue_signals();
	for (fs = funcstack; fs; fs = fs->prev) {
	    if (fs->tp == FS_FUNC) {
		/*
		 * dupstring here is paranoia but unlikely to be
		 * problematic
		 */
		funcname = dupstring(fs->name);
		break;
	    }
	}
	if (!funcname)
	{
	    zerrnam(name, "bad autoload");
	    ret = 1;
	} else {
	    if ((shf = (Shfunc) shfunctab->getnode(shfunctab, funcname))) {
		DPUTS(!shf->funcdef,
		      "BUG: Calling autoload from empty function");
	    } else {
		shf = (Shfunc) zshcalloc(sizeof *shf);
		shfunctab->addnode(shfunctab, ztrdup(funcname), shf);
	    }
	    if (*argv) {
		dircache_set(&shf->filename, NULL);
		dircache_set(&shf->filename, *argv);
		on |= PM_LOADDIR;
	    }
	    shf->node.flags = on;
	    ret = eval_autoload(shf, funcname, ops, func);
	}
	unqueue_signals();
	return ret;
    } else if (!*argv) {
	/* If no arguments given, we will print functions.  If flags *
	 * are given, we will print only functions containing these  *
	 * flags, else we'll print them all.                         */
	int ret = 0;

	queue_signals();
	if (OPT_ISSET(ops,'U') && !OPT_ISSET(ops,'u'))
		on &= ~PM_UNDEFINED;
	    scanshfunc(1, on|off, DISABLED, shfunctab->printnode,
		       pflags, expand);
	unqueue_signals();
	return ret;
    }

    /* With the -m option, treat arguments as glob patterns */
    if (OPT_ISSET(ops,'m')) {
	on &= ~PM_UNDEFINED;
	for (; *argv; argv++) {
	    queue_signals();
	    /* expand argument */
	    tokenize(*argv);
	    if ((pprog = patcompile(*argv, PAT_STATIC, 0))) {
		/* with no options, just print all functions matching the glob pattern */
		if (!(on|off) && !OPT_ISSET(ops,'X')) {
		    scanmatchshfunc(pprog, 1, 0, DISABLED,
				   shfunctab->printnode, pflags, expand);
		} else {
		    /* apply the options to all functions matching the glob pattern */
		    for (i = 0; i < shfunctab->hsize; i++) {
			for (shf = (Shfunc) shfunctab->nodes[i]; shf;
			     shf = (Shfunc) shf->node.next)
			    if (pattry(pprog, shf->node.nam) &&
				!(shf->node.flags & DISABLED)) {
				shf->node.flags = (shf->node.flags |
					      (on & ~PM_UNDEFINED)) & ~off;
				if (check_autoload(shf, shf->node.nam,
						   ops, func)) {
				    returnval = 1;
				}
			    }
		    }
		}
	    } else {
		untokenize(*argv);
		zwarnnam(name, "bad pattern : %s", *argv);
		returnval = 1;
	    }
	    unqueue_signals();
	}
	return returnval;
    }

    /* Take the arguments literally -- do not glob */
    queue_signals();
    for (; *argv; argv++) {
	if (OPT_ISSET(ops,'w'))
	    returnval = dump_autoload(name, *argv, on, ops, func);
	else if ((shf = (Shfunc) shfunctab->getnode(shfunctab, *argv))) {
	    /* if any flag was given */
	    if (on|off) {
		/* turn on/off the given flags */
		shf->node.flags = (shf->node.flags | (on & ~PM_UNDEFINED)) & ~off;
		if (check_autoload(shf, shf->node.nam, ops, func))
		    returnval = 1;
	    } else
		/* no flags, so just print */
		printshfuncexpand(&shf->node, pflags, expand);
	} else if (on & PM_UNDEFINED) {
	    int signum = -1, ok = 1;

	    if (!strncmp(*argv, "TRAP", 4) &&
		(signum = getsignum(*argv + 4)) != -1) {
		/*
		 * Because of the possibility of alternative names,
		 * we must remove the trap explicitly.
		 */
		removetrapnode(signum);
	    }

	    if (**argv == '/') {
		char *base = strrchr(*argv, '/') + 1;
		if (*base &&
		    (shf = (Shfunc) shfunctab->getnode(shfunctab, base))) {
		    char *dir;
		    /* turn on/off the given flags */
		    shf->node.flags =
			(shf->node.flags | (on & ~PM_UNDEFINED)) & ~off;
		    if (shf->node.flags & PM_UNDEFINED) {
			/* update path if not yet loaded */
			if (base == *argv + 1)
			    dir = "/";
			else {
			    dir = *argv;
			    base[-1] = '\0';
			}
			dircache_set(&shf->filename, NULL);
			dircache_set(&shf->filename, dir);
		    }
		    if (check_autoload(shf, shf->node.nam, ops, func))
			returnval = 1;
		    continue;
		}
	    }

	    /* Add a new undefined (autoloaded) function to the *
	     * hash table with the corresponding flags set.     */
	    shf = (Shfunc) zshcalloc(sizeof *shf);
	    shf->node.flags = on;
	    shf->funcdef = mkautofn(shf);
	    shfunc_set_sticky(shf);
	    add_autoload_function(shf, *argv);

	    if (signum != -1) {
		if (settrap(signum, NULL, ZSIG_FUNC)) {
		    shfunctab->removenode(shfunctab, *argv);
		    shfunctab->freenode(&shf->node);
		    returnval = 1;
		    ok = 0;
		}
	    }

	    if (ok && check_autoload(shf, shf->node.nam, ops, func))
		returnval = 1;
	} else
	    returnval = 1;
    }
    unqueue_signals();
    return returnval;
}

/**/
Eprog
mkautofn(Shfunc shf)
{
    Eprog p;

    p = (Eprog) zalloc(sizeof(*p));
    p->len = 5 * sizeof(wordcode);
    p->prog = (Wordcode) zalloc(p->len);
    p->strs = NULL;
    p->shf = shf;
    p->npats = 0;
    p->nref = 1; /* allocated from permanent storage */
    p->pats = (Patprog *) p->prog;
    p->flags = EF_REAL;
    p->dump = NULL;

    p->prog[0] = WCB_LIST((Z_SYNC | Z_END), 0);
    p->prog[1] = WCB_SUBLIST(WC_SUBLIST_END, 0, 3);
    p->prog[2] = WCB_PIPE(WC_PIPE_END, 0);
    p->prog[3] = WCB_AUTOFN();
    p->prog[4] = WCB_END();

    return p;
}

/* unset: unset parameters */

/**/
int
bin_unset(char *name, char **argv, Options ops, int func)
{
    Param pm, next;
    Patprog pprog;
    char *s;
    int match = 0, returnval = 0;
    int i;

    /* unset -f is the same as unfunction */
    if (OPT_ISSET(ops,'f'))
	return bin_unhash(name, argv, ops, func);

    /* with -m option, treat arguments as glob patterns */
    if (OPT_ISSET(ops,'m')) {
	while ((s = *argv++)) {
	    queue_signals();
	    /* expand */
	    tokenize(s);
	    if ((pprog = patcompile(s, PAT_STATIC, NULL))) {
		/* Go through the parameter table, and unset any matches */
		for (i = 0; i < paramtab->hsize; i++) {
		    for (pm = (Param) paramtab->nodes[i]; pm; pm = next) {
			/* record pointer to next, since we may free this one */
			next = (Param) pm->node.next;
			if ((!(pm->node.flags & PM_RESTRICTED) ||
			     unset(RESTRICTED)) &&
			    pattry(pprog, pm->node.nam)) {
			    unsetparam_pm(pm, 0, 1);
			    match++;
			}
		    }
		}
	    } else {
		untokenize(s);
		zwarnnam(name, "bad pattern : %s", s);
		returnval = 1;
	    }
	    unqueue_signals();
	}
	/* If we didn't match anything, we return 1. */
	if (!match)
	    returnval = 1;
	return returnval;
    }

    /* do not glob -- unset the given parameter */
    queue_signals();
    while ((s = *argv++)) {
	char *ss = strchr(s, '['), *subscript = 0;
	if (ss) {
	    char *sse;
	    *ss = 0;
	    if ((sse = parse_subscript(ss+1, 1, ']'))) {
		*sse = 0;
		subscript = dupstring(ss+1);
		*sse = ']';
		remnulargs(subscript);
		untokenize(subscript);
	    }
	}
	if ((ss && !subscript) || !isident(s)) {
	    if (ss)
		*ss = '[';
	    zerrnam(name, "%s: invalid parameter name", s);
	    returnval = 1;
	    continue;
	}
	pm = (Param) (paramtab == realparamtab ?
		      /* getnode2() to avoid autoloading */
		      paramtab->getnode2(paramtab, s) :
		      paramtab->getnode(paramtab, s));
	/*
	 * Unsetting an unset variable is not an error.
	 * This appears to be reasonably standard behaviour.
	 */
	if (!pm)
	    continue;
	else if ((pm->node.flags & PM_RESTRICTED) && isset(RESTRICTED)) {
	    zerrnam(name, "%s: restricted", pm->node.nam);
	    returnval = 1;
	} else if (ss) {
	    if (PM_TYPE(pm->node.flags) == PM_HASHED) {
		HashTable tht = paramtab;
		if ((paramtab = pm->gsu.h->getfn(pm)))
		    unsetparam(subscript);
		paramtab = tht;
	    } else if (PM_TYPE(pm->node.flags) == PM_SCALAR ||
		       PM_TYPE(pm->node.flags) == PM_ARRAY) {
		struct value vbuf;
		vbuf.isarr = (PM_TYPE(pm->node.flags) == PM_ARRAY ?
			      SCANPM_ARRONLY : 0);
		vbuf.pm = pm;
		vbuf.flags = 0;
		vbuf.start = 0;
		vbuf.end = -1;
		vbuf.arr = 0;
		*ss = '[';
		if (getindex(&ss, &vbuf, SCANPM_ASSIGNING) == 0 &&
		    vbuf.pm && !(vbuf.pm->node.flags & PM_UNSET)) {
		    if (PM_TYPE(pm->node.flags) == PM_SCALAR) {
			setstrvalue(&vbuf, ztrdup(""));
		    } else {
			/* start is after the element for reverse index */
			int start = vbuf.start - !!(vbuf.flags & VALFLAG_INV);
			if (arrlen_gt(vbuf.pm->u.arr, start)) {
			    char *arr[2];
			    arr[0] = "";
			    arr[1] = 0;
			    setarrvalue(&vbuf, zarrdup(arr));
			}
		    }
		}
		returnval = errflag;
		errflag &= ~ERRFLAG_ERROR;
	    } else {
		zerrnam(name, "%s: invalid element for unset", s);
		returnval = 1;
	    }
	} else {
	    if (unsetparam_pm(pm, 0, 1))
		returnval = 1;
	}
	if (ss)
	    *ss = '[';
    }
    unqueue_signals();
    return returnval;
}

/* type, whence, which, command */

static LinkList matchednodes;

static void
fetchcmdnamnode(HashNode hn, UNUSED(int printflags))
{
    Cmdnam cn = (Cmdnam) hn;
    addlinknode(matchednodes, cn->node.nam);
}

/**/
int
bin_whence(char *nam, char **argv, Options ops, int func)
{
    HashNode hn;
    Patprog pprog;
    int returnval = 0;
    int printflags = 0;
    int aliasflags;
    int csh, all, v, wd;
    int informed = 0;
    int expand = 0;
    char *cnam, **allmatched = 0;

    /* Check some option information */
    csh = OPT_ISSET(ops,'c');
    v   = OPT_ISSET(ops,'v');
    all = OPT_ISSET(ops,'a');
    wd  = OPT_ISSET(ops,'w');

    if (OPT_ISSET(ops,'x')) {
	char *eptr;
	expand = (int)zstrtol(OPT_ARG(ops,'x'), &eptr, 10);
	if (*eptr) {
	    zwarnnam(nam, "number expected after -x");
	    return 1;
	}
	if (expand == 0)	/* no indentation at all */
	    expand = -1;
    }

    if (OPT_ISSET(ops,'w'))
	printflags |= PRINT_WHENCE_WORD;
    else if (OPT_ISSET(ops,'c'))
	printflags |= PRINT_WHENCE_CSH;
    else if (OPT_ISSET(ops,'v'))
	printflags |= PRINT_WHENCE_VERBOSE;
    else
	printflags |= PRINT_WHENCE_SIMPLE;
    if (OPT_ISSET(ops,'f'))
	printflags |= PRINT_WHENCE_FUNCDEF;

    if (func == BIN_COMMAND)
	if (OPT_ISSET(ops,'V')) {
	    printflags = aliasflags = PRINT_WHENCE_VERBOSE;
	    v = 1;
	} else {
	    aliasflags = PRINT_LIST;
	    printflags = PRINT_WHENCE_SIMPLE;
	    v = 0;
	}
    else
	aliasflags = printflags;

    /* With -m option -- treat arguments as a glob patterns */
    if (OPT_ISSET(ops,'m')) {
	cmdnamtab->filltable(cmdnamtab);
	if (all) {
	    pushheap();
	    matchednodes = newlinklist();
	}
	queue_signals();
	for (; *argv; argv++) {
	    /* parse the pattern */
	    tokenize(*argv);
	    if (!(pprog = patcompile(*argv, PAT_STATIC, NULL))) {
		untokenize(*argv);
		zwarnnam(nam, "bad pattern : %s", *argv);
		returnval = 1;
		continue;
	    }
	    if (!OPT_ISSET(ops,'p')) {
		/* -p option is for path search only.    *
		 * We're not using it, so search for ... */

		/* aliases ... */
		informed +=
		scanmatchtable(aliastab, pprog, 1, 0, DISABLED,
			       aliastab->printnode, printflags);

		/* and reserved words ... */
		informed +=
		scanmatchtable(reswdtab, pprog, 1, 0, DISABLED,
			       reswdtab->printnode, printflags);

		/* and shell functions... */
		informed +=
		scanmatchshfunc(pprog, 1, 0, DISABLED,
			       shfunctab->printnode, printflags, expand);

		/* and builtins. */
		informed +=
		scanmatchtable(builtintab, pprog, 1, 0, DISABLED,
			       builtintab->printnode, printflags);
	    }
	    /* Done search for `internal' commands, if the -p option *
	     * was not used.  Now search the path.                   */
	    informed +=
	    scanmatchtable(cmdnamtab, pprog, 1, 0, 0,
			   (all ? fetchcmdnamnode : cmdnamtab->printnode),
			   printflags);
	    run_queued_signals();
	}
	unqueue_signals();
	if (all) {
	    allmatched = argv = zlinklist2array(matchednodes, 1);
	    matchednodes = NULL;
	    popheap();
	} else
	    return returnval || !informed;
    }

    /* Take arguments literally -- do not glob */
    queue_signals();
    for (; *argv; argv++) {
	if (!OPT_ISSET(ops,'p') && !allmatched) {
	    char *suf;

	    /* Look for alias */
	    if ((hn = aliastab->getnode(aliastab, *argv))) {
		aliastab->printnode(hn, aliasflags);
		informed = 1;
		if (!all)
		    continue;
	    }
	    /* Look for suffix alias */
	    if ((suf = strrchr(*argv, '.')) && suf[1] &&
		suf > *argv && suf[-1] != Meta &&
		(hn = sufaliastab->getnode(sufaliastab, suf+1))) {
		sufaliastab->printnode(hn, printflags);
		informed = 1;
		if (!all)
		    continue;
	    }
	    /* Look for reserved word */
	    if ((hn = reswdtab->getnode(reswdtab, *argv))) {
		reswdtab->printnode(hn, printflags);
		informed = 1;
		if (!all)
		    continue;
	    }
	    /* Look for shell function */
	    if ((hn = shfunctab->getnode(shfunctab, *argv))) {
		printshfuncexpand(hn, printflags, expand);
		informed = 1;
		if (!all)
		    continue;
	    }
	    /* Look for builtin command */
	    if ((hn = builtintab->getnode(builtintab, *argv))) {
		builtintab->printnode(hn, printflags);
		informed = 1;
		if (!all)
		    continue;
	    }
	    /* Look for commands that have been added to the *
	     * cmdnamtab with the builtin `hash foo=bar'.    */
	    if ((hn = cmdnamtab->getnode(cmdnamtab, *argv)) && (hn->flags & HASHED)) {
		cmdnamtab->printnode(hn, printflags);
		informed = 1;
		if (!all)
		    continue;
	    }
	}

	/* Option -a is to search the entire path, *
	 * rather than just looking for one match. */
	if (all && **argv != '/') {
	    char **pp, *buf;

	    pushheap();
	    for (pp = path; *pp; pp++) {
		if (**pp) {
		    buf = zhtricat(*pp, "/", *argv);
		} else buf = dupstring(*argv);

		if (iscom(buf)) {
		    if (wd) {
			printf("%s: command\n", *argv);
		    } else {
			if (v && !csh) {
			    zputs(*argv, stdout), fputs(" is ", stdout);
			    quotedzputs(buf, stdout);
			} else
			    zputs(buf, stdout);
			if (OPT_ISSET(ops,'s') || OPT_ISSET(ops, 'S'))
			    print_if_link(buf, OPT_ISSET(ops, 'S'));
			fputc('\n', stdout);
		    }
		    informed = 1;
		}
	    }
	    if (!informed && (wd || v || csh)) {
		/* this is information and not an error so, as in csh, use stdout */
		zputs(*argv, stdout);
		puts(wd ? ": none" : " not found");
		returnval = 1;
	    }
	    popheap();
	} else if (func == BIN_COMMAND && OPT_ISSET(ops,'p') &&
		   (hn = builtintab->getnode(builtintab, *argv))) {
	    /*
	     * Special case for "command -p[vV]" which needs to
	     * show a builtin in preference to an external command.
	     */
	    builtintab->printnode(hn, printflags);
	    informed = 1;
	} else if ((cnam = findcmd(*argv, 1,
				   func == BIN_COMMAND &&
				   OPT_ISSET(ops,'p')))) {
	    /* Found external command. */
	    if (wd) {
		printf("%s: command\n", *argv);
	    } else {
		if (v && !csh) {
		    zputs(*argv, stdout), fputs(" is ", stdout);
		    quotedzputs(cnam, stdout);
		} else
		    zputs(cnam, stdout);
		if (OPT_ISSET(ops,'s') || OPT_ISSET(ops,'S'))
		    print_if_link(cnam, OPT_ISSET(ops,'S'));
		fputc('\n', stdout);
	    }
	    informed = 1;
	} else {
	    /* Not found at all. That's not an error as such so this goes to stdout */
	    if (v || csh || wd)
		zputs(*argv, stdout), puts(wd ? ": none" : " not found");
	    returnval = 1;
	}
    }
    if (allmatched)
	freearray(allmatched);

    unqueue_signals();
    return returnval || !informed;
}

/**** command & named directory hash table builtins ****/

/*****************************************************************
 * hash -- explicitly hash a command.                            *
 * 1) Given no arguments, list the hash table.                   *
 * 2) The -m option prints out commands in the hash table that   *
 *    match a given glob pattern.                                *
 * 3) The -f option causes the entire path to be added to the    *
 *    hash table (cannot be combined with any arguments).        *
 * 4) The -r option causes the entire hash table to be discarded *
 *    (cannot be combined with any arguments).                   *
 * 5) Given argument of the form foo=bar, add element to command *
 *    hash table, so that when `foo' is entered, then `bar' is   *
 *    executed.                                                  *
 * 6) Given arguments not of the previous form, add it to the    *
 *    command hash table as if it were being executed.           *
 * 7) The -d option causes analogous things to be done using     *
 *    the named directory hash table.                            *
 *****************************************************************/

/**/
int
bin_hash(char *name, char **argv, Options ops, UNUSED(int func))
{
    HashTable ht;
    Patprog pprog;
    Asgment asg;
    int returnval = 0;
    int printflags = 0;

    if (OPT_ISSET(ops,'d'))
	ht = nameddirtab;
    else
	ht = cmdnamtab;

    if (OPT_ISSET(ops,'r') || OPT_ISSET(ops,'f')) {
	/* -f and -r can't be used with any arguments */
	if (*argv) {
	    zwarnnam("hash", "too many arguments");
	    return 1;
	}

	/* empty the hash table */
	if (OPT_ISSET(ops,'r'))
	    ht->emptytable(ht);

	/* fill the hash table in a standard way */
	if (OPT_ISSET(ops,'f'))
	    ht->filltable(ht);

	return 0;
    }

    if (OPT_ISSET(ops,'L')) printflags |= PRINT_LIST;

    /* Given no arguments, display current hash table. */
    if (!*argv) {
	queue_signals();
	scanhashtable(ht, 1, 0, 0, ht->printnode, printflags);
	unqueue_signals();
	return 0;
    }

    queue_signals();
    while (*argv) {
	void *hn;
	if (OPT_ISSET(ops,'m')) {
	    /* with the -m option, treat the argument as a glob pattern */
	    tokenize(*argv);  /* expand */
	    if ((pprog = patcompile(*argv, PAT_STATIC, NULL))) {
		/* display matching hash table elements */
		scanmatchtable(ht, pprog, 1, 0, 0, ht->printnode, printflags);
	    } else {
		untokenize(*argv);
		zwarnnam(name, "bad pattern : %s", *argv);
		returnval = 1;
	    }
	    argv++;
            continue;
	}
        if (!(asg = getasg(&argv, NULL))) {
	    zwarnnam(name, "bad assignment");
	    returnval = 1;
	    break;
        } else if (ASG_VALUEP(asg)) {
	    if(isset(RESTRICTED)) {
		zwarnnam(name, "restricted: %s", asg->value.scalar);
		returnval = 1;
	    } else {
		/* The argument is of the form foo=bar, *
		 * so define an entry for the table.    */
		if(OPT_ISSET(ops,'d')) {
		    /* shouldn't return NULL if asg->name is not NULL */
		    if (*itype_end(asg->name, IUSER, 0)) {
			zwarnnam(name,
				 "invalid character in directory name: %s",
				 asg->name);
			returnval = 1;
			continue;
		    } else {
			Nameddir nd = hn = zshcalloc(sizeof *nd);
			nd->node.flags = 0;
			nd->dir = ztrdup(asg->value.scalar);
		    }
		} else {
		    Cmdnam cn = hn = zshcalloc(sizeof *cn);
		    cn->node.flags = HASHED;
		    cn->u.cmd = ztrdup(asg->value.scalar);
		}
		ht->addnode(ht, ztrdup(asg->name), hn);
		if(OPT_ISSET(ops,'v'))
		    ht->printnode(hn, 0);
	    }
	} else if (!(hn = ht->getnode2(ht, asg->name))) {
	    /* With no `=value' part to the argument, *
	     * work out what it ought to be.          */
	    if(OPT_ISSET(ops,'d')) {
		if(!getnameddir(asg->name)) {
		    zwarnnam(name, "no such directory name: %s", asg->name);
		    returnval = 1;
		}
	    } else {
		if (!hashcmd(asg->name, path)) {
		    zwarnnam(name, "no such command: %s", asg->name);
		    returnval = 1;
		}
	    }
	    if(OPT_ISSET(ops,'v') && (hn = ht->getnode2(ht, asg->name)))
		ht->printnode(hn, 0);
	} else if(OPT_ISSET(ops,'v'))
	    ht->printnode(hn, 0);
    }
    unqueue_signals();
    return returnval;
}

/* unhash: remove specified elements from a hash table */

/**/
int
bin_unhash(char *name, char **argv, Options ops, int func)
{
    HashTable ht;
    HashNode hn, nhn;
    Patprog pprog;
    int match = 0, returnval = 0, all = 0;
    int i;

    /* Check which hash table we are working with. */
    if (func == BIN_UNALIAS) {
	if (OPT_ISSET(ops,'s'))
	    ht = sufaliastab;	/* suffix aliases */
	else
	    ht = aliastab;	/* aliases           */
	if (OPT_ISSET(ops, 'a')) {
	    if (*argv) {
		zwarnnam(name, "-a: too many arguments");
		return 1;
	    }
	    all = 1;
	} else if (!*argv) {
	    zwarnnam(name, "not enough arguments");
	    return 1;
	}
    } else if (OPT_ISSET(ops,'d'))
	ht = nameddirtab;	/* named directories */
    else if (OPT_ISSET(ops,'f'))
	ht = shfunctab;		/* shell functions   */
    else if (OPT_ISSET(ops,'s'))
	ht = sufaliastab;	/* suffix aliases, must precede aliases */
    else if (func == BIN_UNHASH && (OPT_ISSET(ops,'a')))
	ht = aliastab;		/* aliases           */
    else
	ht = cmdnamtab;		/* external commands */

    if (all) {
	queue_signals();
	for (i = 0; i < ht->hsize; i++) {
	    for (hn = ht->nodes[i]; hn; hn = nhn) {
		/* record pointer to next, since we may free this one */
		nhn = hn->next;
		ht->freenode(ht->removenode(ht, hn->nam));
	    }
	}
	unqueue_signals();
	return 0;
    }

    /* With -m option, treat arguments as glob patterns. *
     * "unhash -m '*'" is legal, but not recommended.    */
    if (OPT_ISSET(ops,'m')) {
	for (; *argv; argv++) {
	    queue_signals();
	    /* expand argument */
	    tokenize(*argv);
	    if ((pprog = patcompile(*argv, PAT_STATIC, NULL))) {
		/* remove all nodes matching glob pattern */
		for (i = 0; i < ht->hsize; i++) {
		    for (hn = ht->nodes[i]; hn; hn = nhn) {
			/* record pointer to next, since we may free this one */
			nhn = hn->next;
			if (pattry(pprog, hn->nam)) {
			    ht->freenode(ht->removenode(ht, hn->nam));
			    match++;
			}
		    }
		}
	    } else {
		untokenize(*argv);
		zwarnnam(name, "bad pattern : %s", *argv);
		returnval = 1;
	    }
	    unqueue_signals();
	}
	/* If we didn't match anything, we return 1. */
	if (!match)
	    returnval = 1;
	return returnval;
    }

    /* Take arguments literally -- do not glob */
    queue_signals();
    for (; *argv; argv++) {
	if ((hn = ht->removenode(ht, *argv))) {
	    ht->freenode(hn);
	} else if (func == BIN_UNSET && isset(POSIXBUILTINS)) {
	    /* POSIX: unset: "Unsetting a variable or function that was *
	     * not previously set shall not be considered an error."    */
	    returnval = 0;
	} else {
	    zwarnnam(name, "no such hash table element: %s", *argv);
	    returnval = 1;
	}
    }
    unqueue_signals();
    return returnval;
}

/**** alias builtins ****/

/* alias: display or create aliases. */

/**/
int
bin_alias(char *name, char **argv, Options ops, UNUSED(int func))
{
    Alias a;
    Patprog pprog;
    Asgment asg;
    int returnval = 0;
    int flags1 = 0, flags2 = DISABLED;
    int printflags = 0;
    int type_opts;
    HashTable ht = aliastab;

    /* Did we specify the type of alias? */
    type_opts = OPT_ISSET(ops, 'r') + OPT_ISSET(ops, 'g') +
	OPT_ISSET(ops, 's');
    if (type_opts) {
	if (type_opts > 1) {
	    zwarnnam(name, "illegal combination of options");
	    return 1;
	}
	if (OPT_ISSET(ops,'g'))
	    flags1 |= ALIAS_GLOBAL;
	else
	    flags2 |= ALIAS_GLOBAL;
	if (OPT_ISSET(ops, 's')) {
	    /*
	     * Although we keep suffix aliases in a different table,
	     * it is useful to be able to distinguish Alias structures
	     * without reference to the table, so we have a separate
	     * flag, too.
	     */
	    flags1 |= ALIAS_SUFFIX;
	    ht = sufaliastab;
	} else
	    flags2 |= ALIAS_SUFFIX;
    }

    if (OPT_ISSET(ops,'L'))
	printflags |= PRINT_LIST;
    else if (OPT_PLUS(ops,'g') || OPT_PLUS(ops,'r') || OPT_PLUS(ops,'s') ||
	     OPT_PLUS(ops,'m') || OPT_ISSET(ops,'+'))
	printflags |= PRINT_NAMEONLY;

    /* In the absence of arguments, list all aliases.  If a command *
     * line flag is specified, list only those of that type.        */
    if (!*argv) {
	queue_signals();
	scanhashtable(ht, 1, flags1, flags2, ht->printnode, printflags);
	unqueue_signals();
	return 0;
    }

    /* With the -m option, treat the arguments as *
     * glob patterns of aliases to display.       */
    if (OPT_ISSET(ops,'m')) {
	for (; *argv; argv++) {
	    queue_signals();
	    tokenize(*argv);  /* expand argument */
	    if ((pprog = patcompile(*argv, PAT_STATIC, NULL))) {
		/* display the matching aliases */
		scanmatchtable(ht, pprog, 1, flags1, flags2,
			       ht->printnode, printflags);
	    } else {
		untokenize(*argv);
		zwarnnam(name, "bad pattern : %s", *argv);
		returnval = 1;
	    }
	    unqueue_signals();
	}
	return returnval;
    }

    /* Take arguments literally.  Don't glob */
    queue_signals();
    while ((asg = getasg(&argv, NULL))) {
	if (asg->value.scalar && !OPT_ISSET(ops,'L')) {
	    /* The argument is of the form foo=bar and we are not *
	     * forcing a listing with -L, so define an alias      */
	    ht->addnode(ht, ztrdup(asg->name),
			createaliasnode(ztrdup(asg->value.scalar), flags1));
	} else if ((a = (Alias) ht->getnode(ht, asg->name))) {
	    /* display alias if appropriate */
	    if (!type_opts || ht == sufaliastab ||
		(OPT_ISSET(ops,'r') &&
		 !(a->node.flags & (ALIAS_GLOBAL|ALIAS_SUFFIX))) ||
		(OPT_ISSET(ops,'g') && (a->node.flags & ALIAS_GLOBAL)))
		ht->printnode(&a->node, printflags);
	} else
	    returnval = 1;
    }
    unqueue_signals();
    return returnval;
}


/**** miscellaneous builtins ****/

/* true, : (colon) */

/**/
int
bin_true(UNUSED(char *name), UNUSED(char **argv), UNUSED(Options ops), UNUSED(int func))
{
    return 0;
}

/* false builtin */

/**/
int
bin_false(UNUSED(char *name), UNUSED(char **argv), UNUSED(Options ops), UNUSED(int func))
{
    return 1;
}

/* the zle buffer stack */

/**/
mod_export LinkList bufstack;

/* echo, print, printf, pushln */

#define print_val(VAL) \
    if (prec >= 0) \
	count += fprintf(fout, spec, width, prec, VAL); \
    else \
	count += fprintf(fout, spec, width, VAL);

/*
 * Because of the use of getkeystring() to interpret the arguments,
 * the elements of args spend a large part of the function unmetafied
 * with the lengths in len.  This may have seemed a good idea once.
 * As we are stuck with this for now, we need to be very careful
 * deciding what state args is in.
 */

/**/
int
bin_print(char *name, char **args, Options ops, int func)
{
    int flen, width, prec, type, argc, n, narg, curlen = 0;
    int nnl = 0, fmttrunc = 0, ret = 0, maxarg = 0, nc = 0;
    int flags[6], *len, visarr = 0;
    char *start, *endptr, *c, *d, *flag, *buf = NULL, spec[14], *fmt = NULL;
    char **first, **argp, *curarg, *flagch = "'0+- #", save = '\0', nullstr = '\0';
    size_t rcount = 0, count = 0;
    size_t *cursplit = 0, *splits = 0;
    FILE *fout = stdout;
#ifdef HAVE_OPEN_MEMSTREAM
    size_t mcount;
#define ASSIGN_MSTREAM(BUF,FOUT) \
    do { \
        if ((FOUT = open_memstream(&BUF, &mcount)) == NULL) { \
            zwarnnam(name, "open_memstream failed"); \
            return 1; \
        } \
    } while (0)
    /*
     * Some implementations of open_memstream() have a bug such that,
     * if fflush() is followed by fclose(), another NUL byte is written
     * to the buffer at the wrong position.  Therefore we must fclose()
     * before reading.
     */
#define READ_MSTREAM(BUF,FOUT) \
    ((fclose(FOUT) == 0) ? mcount : (size_t)-1)
#define CLOSE_MSTREAM(FOUT) 0

#else /* simulate HAVE_OPEN_MEMSTREAM */

#define ASSIGN_MSTREAM(BUF,FOUT) \
    do { \
        int tempfd; \
        char *tmpf; \
        if ((tempfd = gettempfile(NULL, 1, &tmpf)) < 0) { \
            zwarnnam(name, "can't open temp file: %e", errno); \
            return 1; \
        } \
        unlink(tmpf); \
        if ((FOUT = fdopen(tempfd, "w+")) == NULL) { \
            close(tempfd); \
            zwarnnam(name, "can't open temp file: %e", errno); \
            return 1; \
        } \
    } while (0)
#define READ_MSTREAM(BUF,FOUT) \
    ((((count = ftell(FOUT)), (BUF = (char *)zalloc(count + 1))) && \
      ((fseek(FOUT, 0L, SEEK_SET) == 0) && !(BUF[count] = '\0')) && \
      (fread(BUF, 1, count, FOUT) == count)) ? count : (size_t)-1)
#define CLOSE_MSTREAM(FOUT) fclose(FOUT)

#endif

#define IS_MSTREAM(FOUT) \
    (FOUT != stdout && \
     (OPT_ISSET(ops,'z') || OPT_ISSET(ops,'s') || OPT_ISSET(ops,'v')))

    /* Testing EBADF special-cases >&- redirections */
#define CLOSE_CLEANLY(FOUT) \
    (IS_MSTREAM(FOUT) ? CLOSE_MSTREAM(FOUT) == 0 : \
     ((FOUT == stdout) ? (fflush(FOUT) == 0 || errno == EBADF) : \
      (fclose(FOUT) == 0)))	/* implies error for -u on a closed fd */

    Histent ent;
    mnumber mnumval;
    double doubleval;
    int intval;
    zlong zlongval;
    zulong zulongval;
    char *stringval;

    /* Error check option combinations and option arguments */

    if (OPT_ISSET(ops, 'z') +
	OPT_ISSET(ops, 's') + OPT_ISSET(ops, 'S') +
	OPT_ISSET(ops, 'v') > 1) {
	zwarnnam(name, "only one of -s, -S, -v, or -z allowed");
	return 1;
    }
    if ((OPT_ISSET(ops, 'z') | OPT_ISSET(ops, 's') | OPT_ISSET(ops, 'S')) +
	(OPT_ISSET(ops, 'c') | OPT_ISSET(ops, 'C')) > 1) {
	zwarnnam(name, "-c or -C not allowed with -s, -S, or -z");
	return 1;
    }
    if ((OPT_ISSET(ops, 'z') | OPT_ISSET(ops, 'v') |
         OPT_ISSET(ops, 's') | OPT_ISSET(ops, 'S')) +
	(OPT_ISSET(ops, 'p') | OPT_ISSET(ops, 'u')) > 1) {
	zwarnnam(name, "-p or -u not allowed with -s, -S, -v, or -z");
	return 1;
    }
    /*
    if (OPT_ISSET(ops, 'f') &&
	(OPT_ISSET(ops, 'S') || OPT_ISSET(ops, 'c') || OPT_ISSET(ops, 'C'))) {
	zwarnnam(name, "-f not allowed with -c, -C, or -S");
	return 1;
    }
    */

    /* -C -- number of columns */
    if (!fmt && OPT_ISSET(ops,'C')) {
	char *eptr, *argptr = OPT_ARG(ops,'C');
	nc = (int)zstrtol(argptr, &eptr, 10);
	if (*eptr) {
	    zwarnnam(name, "number expected after -%c: %s", 'C', argptr);
	    return 1;
	}
	if (nc <= 0) {
	    zwarnnam(name, "invalid number of columns: %s", argptr);
	    return 1;
	}
    }

    if (func == BIN_PRINTF) {
        if (!strcmp(*args, "--") && !*++args) {
            zwarnnam(name, "not enough arguments");
	    return 1;
        }
  	fmt = *args++;
    } else if (func == BIN_ECHO && isset(BSDECHO))
	ops->ind['E'] = 1;
    else if (OPT_HASARG(ops,'f'))
	fmt = OPT_ARG(ops,'f');
    if (fmt)
	fmt = getkeystring(fmt, &flen, OPT_ISSET(ops,'b') ? GETKEYS_BINDKEY :
			   GETKEYS_PRINTF_FMT, &fmttrunc);

    first = args;

    /* -m option -- treat the first argument as a pattern and remove
     * arguments not matching */
    if (OPT_ISSET(ops,'m')) {
	Patprog pprog;
	char **t, **p;

	if (!*args) {
	    zwarnnam(name, "no pattern specified");
	    return 1;
	}
	queue_signals();
	tokenize(*args);
	if (!(pprog = patcompile(*args, PAT_STATIC, NULL))) {
	    untokenize(*args);
	    zwarnnam(name, "bad pattern: %s", *args);
	    unqueue_signals();
	    return 1;
	}
	for (t = p = ++args; *p; p++)
	    if (pattry(pprog, *p))
		*t++ = *p;
	*t = NULL;
	first = args;
	unqueue_signals();
	if (fmt && !*args) return 0;
    }
    /* compute lengths, and interpret according to -P, -D, -e, etc. */
    argc = arrlen(args);
    len = (int *) hcalloc(argc * sizeof(int));
    for (n = 0; n < argc; n++) {
	/* first \ sequences */
	if (fmt ||
	    (!OPT_ISSET(ops,'e') &&
	     (OPT_ISSET(ops,'R') || OPT_ISSET(ops,'r') || OPT_ISSET(ops,'E'))))
	    unmetafy(args[n], &len[n]);
	else {
	    int escape_how;
	    if (OPT_ISSET(ops,'b'))
		escape_how = GETKEYS_BINDKEY;
	    else if (func != BIN_ECHO && !OPT_ISSET(ops,'e'))
		escape_how = GETKEYS_PRINT;
	    else
		escape_how = GETKEYS_ECHO;
	    args[n] = getkeystring(args[n], &len[n], escape_how, &nnl);
	    if (nnl) {
		/* If there was a \c escape, make this the last arg. */
		argc = n + 1;
		args[argc] = NULL;
	    }
	}
	/* -P option -- interpret as a prompt sequence */
	if (OPT_ISSET(ops,'P')) {
	    /*
	     * promptexpand uses permanent storage: to avoid
	     * messy memory management, stick it on the heap
	     * instead.
	     */
	    char *str = unmetafy(
		promptexpand(metafy(args[n], len[n], META_NOALLOC),
			     0, NULL, NULL, NULL),
		&len[n]);
	    args[n] = dupstrpfx(str, len[n]);
	    free(str);
	}
	/* -D option -- interpret as a directory, and use ~ */
	if (OPT_ISSET(ops,'D')) {
	    Nameddir d;

	    queue_signals();
	    /* TODO: finddir takes a metafied file */
	    d = finddir(args[n]);
	    if (d) {
		int dirlen = strlen(d->dir);
		char *arg = zhalloc(len[n] - dirlen + strlen(d->node.nam) + 2);
		sprintf(arg, "~%s%s", d->node.nam, args[n] + dirlen);
		args[n] = arg;
		len[n] = strlen(args[n]);
	    }
	    unqueue_signals();
	}
    }

    /* -o and -O -- sort the arguments */
    if (OPT_ISSET(ops,'o') || OPT_ISSET(ops,'O')) {
	int flags;

	if (fmt && !*args)
	    return 0;
	flags = OPT_ISSET(ops,'i') ? SORTIT_IGNORING_CASE : 0;
	if (OPT_ISSET(ops,'O'))
	    flags |= SORTIT_BACKWARDS;
	strmetasort(args, flags, len);
    }

    /* -u and -p -- output to other than standard output */
    if ((OPT_HASARG(ops,'u') || OPT_ISSET(ops,'p')) &&
	/* rule out conflicting options -- historical precedence */
	((!fmt && (OPT_ISSET(ops,'c') || OPT_ISSET(ops,'C'))) ||
	 !(OPT_ISSET(ops, 'z') || OPT_ISSET(ops, 'v') ||
	   OPT_ISSET(ops, 's') || OPT_ISSET(ops, 'S')))) {
	int fdarg, fd;

	if (OPT_ISSET(ops, 'p')) {
	    fdarg = coprocout;
	    if (fdarg < 0) {
		zwarnnam(name, "-p: no coprocess");
		return 1;
	    }
	} else {
	    char *argptr = OPT_ARG(ops,'u'), *eptr;
	    /* Handle undocumented feature that -up worked */
	    if (!strcmp(argptr, "p")) {
		fdarg = coprocout;
		if (fdarg < 0) {
		    zwarnnam(name, "-p: no coprocess");
		    return 1;
		}
	    } else {
		fdarg = (int)zstrtol(argptr, &eptr, 10);
		if (*eptr) {
		    zwarnnam(name, "number expected after -u: %s", argptr);
		    return 1;
		}
	    }
	}

	if ((fd = dup(fdarg)) < 0) {
	    zwarnnam(name, "bad file number: %d", fdarg);
	    return 1;
	}
	if ((fout = fdopen(fd, "w")) == 0) {
	    close(fd);
	    zwarnnam(name, "bad mode on fd %d", fd);
	    return 1;
	}
    }

    if (OPT_ISSET(ops, 'v') ||
	(fmt && (OPT_ISSET(ops,'z') || OPT_ISSET(ops,'s'))))
	ASSIGN_MSTREAM(buf,fout);

    /* -c -- output in columns */
    if (!fmt && (OPT_ISSET(ops,'c') || OPT_ISSET(ops,'C'))) {
	int l, nr, sc, n, t, i;
#ifdef MULTIBYTE_SUPPORT
	int *widths;

	if (isset(MULTIBYTE)) {
	    int *wptr;

	    /*
	     * We need the character widths to align output in
	     * columns.
	     */
	    wptr = widths = (int *) zhalloc(argc * sizeof(int));
	    for (i = 0; i < argc && args[i]; i++, wptr++) {
		int l = len[i], width = 0;
		char *aptr = args[i];
		mbstate_t mbs;

		memset(&mbs, 0, sizeof(mbstate_t));
		while (l > 0) {
		    wchar_t wc;
		    size_t cnt;
		    int wcw;

		    /*
		     * Prevent misaligned columns due to escape sequences by
		     * skipping over them. Octals \033 and \233 are the
		     * possible escape characters recognized by ANSI.
		     *
		     * It ought to be possible to do this in the case
		     * of prompt expansion by propagating the information
		     * about escape sequences (currently we strip this
		     * out).
		     */
		    if (*aptr == '\033' || *aptr == '\233') {
			for (aptr++, l--;
			     l && !isalpha(STOUC(*aptr));
			     aptr++, l--)
			    ;
			aptr++;
			l--;
			continue;
		    }

		    cnt = mbrtowc(&wc, aptr, l, &mbs);

		    if (cnt == MB_INCOMPLETE || cnt == MB_INVALID)
		    {
			/* treat as ordinary string */
			width += l;
			break;
		    }
		    wcw = WCWIDTH(wc);
		    /* treat unprintable as 0 */
		    if (wcw > 0)
			width += wcw;
		    /* skip over NUL normally */
		    if (cnt == 0)
			cnt = 1;
		    aptr += cnt;
		    l -= cnt;
		}
		widths[i] = width;
	    }
	}
	else
	    widths = len;
#else
	int *widths = len;
#endif

	if (OPT_ISSET(ops,'C')) {
	    /*
	     * n: number of elements
	     * nc: number of columns (above)
	     * nr: number of rows
	     */
	    n = arrlen(args);
	    nr = (n + nc - 1) / nc;

	    /*
	     * i: loop counter
	     * l: maximum length seen
	     *
	     * Ignore lengths in last column since they don't affect
	     * the separation.
	     */
	    for (i = l = 0; i < argc; i++) {
		if (OPT_ISSET(ops, 'a')) {
		    if ((i % nc) == nc - 1)
			continue;
		} else {
		    if (i >= nr * (nc - 1))
			break;
		}
		if (l < widths[i])
		    l = widths[i];
	    }
	    sc = l + 2;
	}
	else
	{
	    /*
	     * n: loop counter
	     * l: maximum length seen
	     */
	    for (n = l = 0; n < argc; n++)
		if (l < widths[n])
		    l = widths[n];

	    /*
	     * sc: column width
	     * nc: number of columns (at least one)
	     */
	    sc = l + 2;
	    nc = (zterm_columns + 1) / sc;
	    if (!nc)
		nc = 1;
	    nr = (n + nc - 1) / nc;
	}

	if (OPT_ISSET(ops,'a'))	/* print across, i.e. columns first */
	    n = 0;
	for (i = 0; i < nr; i++) {
	    if (OPT_ISSET(ops,'a'))
	    {
		int ic;
		for (ic = 0; ic < nc && n < argc; ic++, n++)
		{
		    fwrite(args[n], len[n], 1, fout);
		    l = widths[n];
		    if (n < argc && ic < nc - 1)
			for (; l < sc; l++)
			    fputc(' ', fout);
		}
	    }
	    else
	    {
		n = i;
		do {
		    fwrite(args[n], len[n], 1, fout);
		    l = widths[n];
		    for (t = nr; t && n < argc; t--, n++);
		    if (n < argc)
			for (; l < sc; l++)
			    fputc(' ', fout);
		} while (n < argc);
	    }
	    fputc(OPT_ISSET(ops,'N') ? '\0' : '\n', fout);
	}
	if (IS_MSTREAM(fout) && (rcount = READ_MSTREAM(buf,fout)) == -1)
	    ret = 1;
	if (!CLOSE_CLEANLY(fout) || ret) {
            zwarnnam(name, "write error: %e", errno);
            ret = 1;
	}
	if (buf) {
	    /* assert: we must be doing -v at this point */
	    queue_signals();
	    if (ret)
		free(buf);
	    else
		setsparam(OPT_ARG(ops, 'v'),
			  metafy(buf, rcount, META_REALLOC));
	    unqueue_signals();
	}
	return ret;
    }

    /* normal output */
    if (!fmt) {
	if (OPT_ISSET(ops, 'z') ||
	    OPT_ISSET(ops, 's') || OPT_ISSET(ops, 'S')) {
	    /*
	     * We don't want the arguments unmetafied after all.
	     */
	    for (n = 0; n < argc; n++)
		metafy(args[n], len[n], META_NOALLOC);
	}

	/* -z option -- push the arguments onto the editing buffer stack */
	if (OPT_ISSET(ops,'z')) {
	    queue_signals();
	    zpushnode(bufstack, sepjoin(args, NULL, 0));
	    unqueue_signals();
	    return 0;
	}
	/* -s option -- add the arguments to the history list */
	if (OPT_ISSET(ops,'s') || OPT_ISSET(ops,'S')) {
	    int nwords = 0, nlen, iwords;
	    char **pargs = args;

	    queue_signals();
	    while (*pargs++)
		nwords++;
	    if (nwords) {
		if (OPT_ISSET(ops,'S')) {
		    int wordsize;
		    short *words;
		    if (nwords > 1) {
			zwarnnam(name, "option -S takes a single argument");
			unqueue_signals();
			return 1;
		    }
		    words = NULL;
		    wordsize = 0;
		    histsplitwords(*args, &words, &wordsize, &nwords, 1);
		    ent = prepnexthistent();
		    ent->words = (short *)zalloc(nwords*sizeof(short));
		    memcpy(ent->words, words, nwords*sizeof(short));
		    free(words);
		    ent->nwords = nwords/2;
		} else {
		    ent = prepnexthistent();
		    ent->words = (short *)zalloc(nwords*2*sizeof(short));
		    ent->nwords = nwords;
		    nlen = iwords = 0;
		    for (pargs = args; *pargs; pargs++) {
			ent->words[iwords++] = nlen;
			nlen += strlen(*pargs);
			ent->words[iwords++] = nlen;
			nlen++;
		    }
		}
	    } else {
		ent = prepnexthistent();
		ent->words = (short *)NULL;
	    }
	    ent->node.nam = zjoin(args, ' ', 0);
	    ent->stim = ent->ftim = time(NULL);
	    ent->node.flags = 0;
	    addhistnode(histtab, ent->node.nam, ent);
	    unqueue_signals();
	    return 0;
	}

	if (OPT_HASARG(ops, 'x') || OPT_HASARG(ops, 'X')) {
	    char *eptr;
	    int expand, startpos = 0;
	    int all = OPT_HASARG(ops, 'X');
	    char *xarg = all ? OPT_ARG(ops, 'X') : OPT_ARG(ops, 'x');

	    expand = (int)zstrtol(xarg, &eptr, 10);
	    if (*eptr || expand <= 0) {
		zwarnnam(name, "positive integer expected after -%c: %s", 'x',
			 xarg);
		return 1;
	    }
	    for (; *args; args++, len++) {
		startpos = zexpandtabs(*args, *len, expand, startpos, fout,
				       all);
		if (args[1]) {
		    if (OPT_ISSET(ops, 'l')) {
			fputc('\n', fout);
			startpos = 0;
		    } else if (OPT_ISSET(ops,'N')) {
			fputc('\0', fout);
		    } else {
			fputc(' ', fout);
			startpos++;
		    }
		}
	    }
	} else {
	    for (; *args; args++, len++) {
		fwrite(*args, *len, 1, fout);
		if (args[1])
		    fputc(OPT_ISSET(ops,'l') ? '\n' :
			  OPT_ISSET(ops,'N') ? '\0' : ' ', fout);
	    }
	}
	if (!(OPT_ISSET(ops,'n') || nnl ||
	    (OPT_ISSET(ops, 'v') && !OPT_ISSET(ops, 'l'))))
	    fputc(OPT_ISSET(ops,'N') ? '\0' : '\n', fout);
	if (IS_MSTREAM(fout) && (rcount = READ_MSTREAM(buf,fout)) == -1)
	    ret = 1;
	if (!CLOSE_CLEANLY(fout) || ret) {
            zwarnnam(name, "write error: %e", errno);
            ret = 1;
	}
	if (buf) {
	    /* assert: we must be doing -v at this point */
	    queue_signals();
	    if (ret)
		free(buf);
	    else
		setsparam(OPT_ARG(ops, 'v'),
			  metafy(buf, rcount, META_REALLOC));
	    unqueue_signals();
	}
	return ret;
    }

    /*
     * All the remaining code in this function is for printf-style
     * output (printf itself, or print -f).  We still have to handle
     * special cases of printing to a ZLE buffer or the history, however.
     */

    if (OPT_ISSET(ops,'v')) {
	struct value vbuf;
	char* s = OPT_ARG(ops,'v');
	Value v = getvalue(&vbuf, &s, 0);
	visarr = v && PM_TYPE(v->pm->node.flags) == PM_ARRAY;
    }
    /* printf style output */
    *spec = '%';
    argp = args;
    do {
    	rcount = count;
	if (argp > args && visarr) { /* reusing format string */
	    if (!splits)
		cursplit = splits = (size_t *)zhalloc(sizeof(size_t) *
			(arrlen(args) / (argp - args) + 1));
	    *cursplit++ = count;
	}
    	if (maxarg) {
	    first += maxarg;
	    argc -= maxarg;
    	    maxarg = 0;
	}
	for (c = fmt; c-fmt < flen; c++) {
	    if (*c != '%') {
		putc(*c, fout);
		++count;
		continue;
	    }

	    start = c++;
	    if (*c == '%') {
		putc('%', fout);
		++count;
		continue;
	    }

	    type = prec = -1;
	    width = 0;
	    curarg = NULL;
	    d = spec + 1;

	    if (*c >= '1' && *c <= '9') {
	    	narg = strtoul(c, &endptr, 0);
		if (*endptr == '$') {
		    c = endptr + 1;
		    if (narg <= 0 || narg > argc) {
		    	zwarnnam(name, "%d: argument specifier out of range",
				 narg);
			if (fout != stdout)
			    fclose(fout);
#ifdef HAVE_OPEN_MEMSTREAM
			if (buf)
			    free(buf);
#endif
			return 1;
		    } else {
		    	if (narg > maxarg) maxarg = narg;
		    	curarg = *(first + narg - 1);
			curlen = len[first - args + narg - 1];
		    }
		}
	    }

	    /* copy only one of each flag as spec has finite size */
	    memset(flags, 0, sizeof(flags));
	    while (*c && (flag = strchr(flagch, *c))) {
	    	if (!flags[flag - flagch]) {
	    	    flags[flag - flagch] = 1;
		    *d++ = *c;
		}
	    	c++;
	    }

	    if (idigit(*c)) {
		width = strtoul(c, &endptr, 0);
		c = endptr;
	    } else if (*c == '*') {
		if (idigit(*++c)) {
		    narg = strtoul(c, &endptr, 0);
		    if (*endptr == '$') {
		    	c = endptr + 1;
			if (narg > argc || narg <= 0) {
		    	    zwarnnam(name,
				     "%d: argument specifier out of range",
				     narg);
			    if (fout != stdout)
				fclose(fout);
#ifdef HAVE_OPEN_MEMSTREAM
			    if (buf)
				free(buf);
#endif
			    return 1;
			} else {
		    	    if (narg > maxarg) maxarg = narg;
		    	    argp = first + narg - 1;
			}
		    }
		}
		if (*argp) {
		    width = (int)mathevali(*argp++);
		    if (errflag) {
			errflag &= ~ERRFLAG_ERROR;
			ret = 1;
		    }
		}
	    }
	    *d++ = '*';

	    if (*c == '.') {
		if (*++c == '*') {
		    if (idigit(*++c)) {
			narg = strtoul(c, &endptr, 0);
			if (*endptr == '$') {
			    c = endptr + 1;
			    if (narg > argc || narg <= 0) {
		    		zwarnnam(name,
					 "%d: argument specifier out of range",
					 narg);
				if (fout != stdout)
				    fclose(fout);
#ifdef HAVE_OPEN_MEMSTREAM
				if (buf)
				    free(buf);
#endif
				return 1;
			    } else {
		    		if (narg > maxarg) maxarg = narg;
		    		argp = first + narg - 1;
			    }
			}
		    }

		    if (*argp) {
			prec = (int)mathevali(*argp++);
			if (errflag) {
			    errflag &= ~ERRFLAG_ERROR;
			    ret = 1;
			}
		    }
		} else if (idigit(*c)) {
		    prec = strtoul(c, &endptr, 0);
		    c = endptr;
		} else
		    prec = 0;
		if (prec >= 0) *d++ = '.', *d++ = '*';
	    }

	    /* ignore any size modifier */
	    if (*c == 'l' || *c == 'L' || *c == 'h') c++;

	    if (!curarg && *argp) {
		curarg = *argp;
		curlen = len[argp++ - args];
	    }
	    d[1] = '\0';
	    switch (*d = *c) {
	    case 'c':
		if (curarg)
		    intval = *curarg;
		else
		    intval = 0;
		print_val(intval);
		break;
	    case 's':
	    case 'b':
		if (curarg) {
		    char *b, *ptr;
		    int lbytes, lchars, lleft;
#ifdef MULTIBYTE_SUPPORT
		    mbstate_t mbs;
#endif

		    if (*c == 'b') {
			b = getkeystring(metafy(curarg, curlen, META_USEHEAP),
					 &lbytes,
					 OPT_ISSET(ops,'b') ? GETKEYS_BINDKEY :
					 GETKEYS_PRINTF_ARG, &nnl);
		    } else {
			b = curarg;
			lbytes = curlen;
		    }
		    /*
		     * Handle width/precision here and use fwrite so that
		     * nul characters can be output.
		     *
		     * First, examine width of string given that it
		     * may contain multibyte characters.  The output
		     * widths are for characters, so we need to count
		     * (in lchars).  However, if we need to truncate
		     * the string we need the width in bytes (in lbytes).
		     */
		    ptr = b;
#ifdef MULTIBYTE_SUPPORT
		    memset(&mbs, 0, sizeof(mbs));
#endif

		    for (lchars = 0, lleft = lbytes; lleft > 0; lchars++) {
			int chars;

			if (lchars == prec) {
			    /* Truncate at this point. */
			    lbytes = ptr - b;
			    break;
			}
#ifdef MULTIBYTE_SUPPORT
			if (isset(MULTIBYTE)) {
			    chars = mbrlen(ptr, lleft, &mbs);
			    if (chars < 0) {
				/*
				 * Invalid/incomplete character at this
				 * point.  Assume all the rest are a
				 * single byte.  That's about the best we
				 * can do.
				 */
				lchars += lleft;
				lbytes = (ptr - b) + lleft;
				break;
			    } else if (chars == 0) {
				/* NUL, handle as real character */
				chars = 1;
			    }
			}
			else	/* use the non-multibyte code below */
#endif
			    chars = 1; /* compiler can optimise this...*/
			lleft -= chars;
			ptr += chars;
		    }
		    if (width > 0 && flags[3]) width = -width;
		    if (width > 0 && lchars < width)
		    	count += fprintf(fout, "%*c", width - lchars, ' ');
		    count += fwrite(b, 1, lbytes, fout);
		    if (width < 0 && lchars < -width)
		    	count += fprintf(fout, "%*c", -width - lchars, ' ');
		    if (nnl) {
			/* If the %b arg had a \c escape, truncate the fmt. */
			flen = c - fmt + 1;
			fmttrunc = 1;
		    }
		} else if (width)
		    count += fprintf(fout, "%*c", width, ' ');
		break;
	    case 'q':
		stringval = curarg ?
		    quotestring(metafy(curarg, curlen, META_USEHEAP),
				QT_BACKSLASH_SHOWNULL) : &nullstr;
		*d = 's';
		print_val(unmetafy(stringval, &curlen));
		break;
	    case 'd':
	    case 'i':
		type=1;
		break;
	    case 'e':
	    case 'E':
	    case 'f':
	    case 'g':
	    case 'G':
		type=2;
		break;
	    case 'o':
	    case 'u':
	    case 'x':
	    case 'X':
		type=3;
		break;
	    case 'n':
		if (curarg) setiparam(curarg, count - rcount);
		break;
	    default:
	        if (*c) {
		    save = c[1];
	            c[1] = '\0';
		}
		zwarnnam(name, "%s: invalid directive", start);
		if (*c) c[1] = save;
		/* Why do we care about a clean close here? */
		if (!CLOSE_CLEANLY(fout))
		    zwarnnam(name, "write error: %e", errno);
#ifdef HAVE_OPEN_MEMSTREAM
		if (buf)
		    free(buf);
#endif
		return 1;
	    }

	    if (type > 0) {
		if (curarg && (*curarg == '\'' || *curarg == '"' )) {
		    convchar_t cc;
#ifdef MULTIBYTE_SUPPORT
		    if (isset(MULTIBYTE)) {
			mb_charinit();
			(void)mb_metacharlenconv(metafy(curarg+1, curlen-1,
							META_USEHEAP), &cc);
		    }
		    else
			cc = WEOF;
		    if (cc == WEOF)
			cc = (curlen > 1) ? STOUC(curarg[1]) : 0;
#else
		    cc = (curlen > 1) ? STOUC(curarg[1]) : 0;
#endif
		    if (type == 2) {
			doubleval = cc;
			print_val(doubleval);
		    } else {
			intval = cc;
			print_val(intval);
		    }
		} else {
		    switch (type) {
		    case 1:
#ifdef ZSH_64_BIT_TYPE
 		    	*d++ = 'l';
#endif
		    	*d++ = 'l', *d++ = *c, *d = '\0';
			zlongval = (curarg) ? mathevali(curarg) : 0;
			if (errflag) {
			    zlongval = 0;
			    errflag &= ~ERRFLAG_ERROR;
			    ret = 1;
			}
			print_val(zlongval)
			    break;
		    case 2:
			if (curarg) {
			    char *eptr;
			    /*
			     * First attempt to parse as a floating
			     * point constant.  If we go through
			     * a math evaluation, we can lose
			     * mostly unimportant information
			     * that people in standards organizations
			     * worry about.
			     */
			    doubleval = strtod(curarg, &eptr);
			    /*
			     * If it didn't parse as a constant,
			     * parse it as an expression.
			     */
			    if (*eptr != '\0') {
				mnumval = matheval(curarg);
				doubleval = (mnumval.type & MN_FLOAT) ?
				    mnumval.u.d : (double)mnumval.u.l;
			    }
			} else doubleval = 0;
			if (errflag) {
			    doubleval = 0;
			    errflag &= ~ERRFLAG_ERROR;
			    ret = 1;
			}
			/* force consistent form for Inf/NaN output */
			if (isnan(doubleval))
			    count += fputs("nan", fout);
			else if (isinf(doubleval))
			    count += fputs((doubleval < 0.0) ? "-inf" : "inf", fout);
		        else
			    print_val(doubleval)
			break;
		    case 3:
#ifdef ZSH_64_BIT_UTYPE
 		    	*d++ = 'l';
#endif
		    	*d++ = 'l', *d++ = *c, *d = '\0';
			if (!curarg)
			    zulongval = (zulong)0;
			else if (!zstrtoul_underscore(curarg, &zulongval))
			    zulongval = mathevali(curarg);
			if (errflag) {
			    zulongval = 0;
			    errflag &= ~ERRFLAG_ERROR;
			    ret = 1;
			}
			print_val(zulongval)
		    }
		}
	    }
	    if (maxarg && (argp - first > maxarg))
	    	maxarg = argp - first;
	}

    	if (maxarg) argp = first + maxarg;
	/* if there are remaining args, reuse format string */
    } while (*argp && argp != first && !fmttrunc && !OPT_ISSET(ops,'r'));

    if (IS_MSTREAM(fout)) {
	queue_signals();
	if ((rcount = READ_MSTREAM(buf,fout)) == -1) {
	    zwarnnam(name, "i/o error: %e", errno);
	    if (buf)
		free(buf);
	} else {
	    if (visarr && splits) {
		char **arrayval = zshcalloc((cursplit - splits + 2) * sizeof(char *));
		for (;cursplit >= splits; cursplit--) {
		    int start = cursplit == splits ? 0 : cursplit[-1];
		    arrayval[cursplit - splits] =
			    metafy(buf + start, count - start, META_DUP);
		    count = start;
		}
		setaparam(OPT_ARG(ops, 'v'), arrayval);
		free(buf);
	    } else {
		stringval = metafy(buf, rcount, META_REALLOC);
		if (OPT_ISSET(ops,'z')) {
		    zpushnode(bufstack, stringval);
		} else if (OPT_ISSET(ops,'v')) {
		    setsparam(OPT_ARG(ops, 'v'), stringval);
		} else {
		    ent = prepnexthistent();
		    ent->node.nam = stringval;
		    ent->stim = ent->ftim = time(NULL);
		    ent->node.flags = 0;
		    ent->words = (short *)NULL;
		    addhistnode(histtab, ent->node.nam, ent);
		}
	    }
	}
	unqueue_signals();
    }

    if (!CLOSE_CLEANLY(fout))
    {
	zwarnnam(name, "write error: %e", errno);
	ret = 1;
    }
    return ret;
}

/* shift builtin */

/**/
int
bin_shift(char *name, char **argv, Options ops, UNUSED(int func))
{
    int num = 1, l, ret = 0;
    char **s;

    /* optional argument can be either numeric or an array */
    queue_signals();
    if (*argv && !getaparam(*argv)) {
        num = mathevali(*argv++);
	if (errflag) {
	    unqueue_signals();
	    return 1;
	}
    }

    if (num < 0) {
	unqueue_signals();
        zwarnnam(name, "argument to shift must be non-negative");
        return 1;
    }

    if (*argv) {
        for (; *argv; argv++)
            if ((s = getaparam(*argv))) {
                if (arrlen_lt(s, num)) {
		    zwarnnam(name, "shift count must be <= $#");
		    ret++;
		    continue;
		}
		if (OPT_ISSET(ops,'p')) {
		    char **s2, **src, **dst;
		    int count;
		    l = arrlen(s);
		    src = s;
		    dst = s2 = (char **)zalloc((l - num + 1) * sizeof(char *));
		    for (count = l - num; count; count--)
			*dst++ = ztrdup(*src++);
		    *dst = NULL;
		    s = s2;
		} else {
		    s = zarrdup(s + num);
		}
                setaparam(*argv, s);
            }
    } else {
        if (num > (l = arrlen(pparams))) {
	    zwarnnam(name, "shift count must be <= $#");
	    ret = 1;
	} else {
	    s = zalloc((l - num + 1) * sizeof(char *));
	    if (OPT_ISSET(ops,'p')) {
		memcpy(s, pparams, (l - num) * sizeof(char *));
		s[l-num] = NULL;
		while (num--)
		    zsfree(pparams[l-1-num]);
	    } else {
		memcpy(s, pparams + num, (l - num + 1) * sizeof(char *));
		while (num--)
		    zsfree(pparams[num]);
	    }
	    zfree(pparams, (l + 1) * sizeof(char *));
	    pparams = s;
	}
    }
    unqueue_signals();
    return ret;
}

/*
 * Position of getopts option within OPTIND argument with multiple options.
 */

/**/
int optcind;

/* getopts: automagical option handling for shell scripts */

/**/
int
bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int func))
{
    int lenstr, lenoptstr, quiet, lenoptbuf;
    char *optstr = unmetafy(*argv++, &lenoptstr), *var = *argv++;
    char **args = (*argv) ? argv : pparams;
    char *str, optbuf[2] = " ", *p, opch;

    /* zoptind keeps count of the current argument number.  The *
     * user can set it to zero to start a new option parse.     */
    if (zoptind < 1) {
	/* first call */
	zoptind = 1;
	optcind = 0;
    }
    if (arrlen_lt(args, zoptind))
	/* no more options */
	return 1;

    /* leading ':' in optstr means don't print an error message */
    quiet = *optstr == ':';
    optstr += quiet;
    lenoptstr -= quiet;

    /* find place in relevant argument */
    str = unmetafy(dupstring(args[zoptind - 1]), &lenstr);
    if (!lenstr)		/* Definitely not an option. */
	return 1;
    if(optcind >= lenstr) {
	optcind = 0;
	if(!args[zoptind++])
	    return 1;
	str = unmetafy(dupstring(args[zoptind - 1]), &lenstr);
    }
    if(!optcind) {
	if(lenstr < 2 || (*str != '-' && *str != '+'))
	    return 1;
	if(lenstr == 2 && str[0] == '-' && str[1] == '-') {
	    zoptind++;
	    return 1;
	}
	optcind++;
    }
    opch = str[optcind++];
    if(str[0] == '+') {
	optbuf[0] = '+';
	lenoptbuf = 2;
    } else
	lenoptbuf = 1;
    optbuf[lenoptbuf - 1] = opch;

    /* check for legality */
    if(opch == ':' || !(p = memchr(optstr, opch, lenoptstr))) {
	p = "?";
	zsfree(zoptarg);
	setsparam(var, ztrdup(p));
	if(quiet) {
	    zoptarg = metafy(optbuf, lenoptbuf, META_DUP);
	} else {
	    zwarn("bad option: %c%c",
		  "?-+"[lenoptbuf], opch);
	    zoptarg=ztrdup("");
	}
	return 0;
    }

    /* check for required argument */
    if(p[1] == ':') {
	if(optcind == lenstr) {
	    if(!args[zoptind]) {
		zsfree(zoptarg);
		if(quiet) {
		    setsparam(var, ztrdup(":"));
		    zoptarg = metafy(optbuf, lenoptbuf, META_DUP);
		} else {
		    setsparam(var, ztrdup("?"));
		    zoptarg = ztrdup("");
		    zwarn("argument expected after %c%c option",
			  "?-+"[lenoptbuf], opch);
		}
		return 0;
	    }
	    p = ztrdup(args[zoptind++]);
	} else
	    p = metafy(str+optcind, lenstr-optcind, META_DUP);
	/*
	 * Careful:  I've just changed the following two lines from
	 *   optcind = ztrlen(args[zoptind - 1]);
	 * and it's a rigorous theorem that every change in getopts breaks
	 * something.  See zsh-workers/9095 for the bug fixed here.
	 *   PWS 2000/05/02
	 */
	optcind = 0;
	zoptind++;
	zsfree(zoptarg);
	zoptarg = p;
    } else {
	zsfree(zoptarg);
	zoptarg = ztrdup("");
    }

    setsparam(var, metafy(optbuf, lenoptbuf, META_DUP));
    return 0;
}

/* Boolean flag that we should exit the shell as soon as all functions return.
 *
 * Set by the 'exit' builtin.
 */

/**/
mod_export int
exit_pending;

/* Shell level at which we exit if exit_pending */
/**/
mod_export int
exit_level;

/* break, bye, continue, exit, logout, return -- most of these take   *
 * one numeric argument, and the other (logout) is related to return. *
 * (return is treated as a logout when in a login shell.)             */

/**/
int
bin_break(char *name, char **argv, UNUSED(Options ops), int func)
{
    int num = lastval, nump = 0, implicit;

    /* handle one optional numeric argument */
    implicit = !*argv;
    if (*argv) {
	num = mathevali(*argv++);
	nump = 1;
    }

    if (nump > 0 && (func == BIN_CONTINUE || func == BIN_BREAK) && num <= 0) {
	zerrnam(name, "argument is not positive: %d", num);
	return 1;
    }

    switch (func) {
    case BIN_CONTINUE:
	if (!loops) {   /* continue is only permitted in loops */
	    zerrnam(name, "not in while, until, select, or repeat loop");
	    return 1;
	}
	contflag = 1; /* FALLTHROUGH */
    case BIN_BREAK:
	if (!loops) {   /* break is only permitted in loops */
	    zerrnam(name, "not in while, until, select, or repeat loop");
	    return 1;
	}
	breaks = nump ? minimum(num,loops) : 1;
	break;
    case BIN_RETURN:
	if ((isset(INTERACTIVE) && isset(SHINSTDIN))
	    || locallevel || sourcelevel) {
	    retflag = 1;
	    breaks = loops;
	    lastval = num;
	    if (trap_state == TRAP_STATE_PRIMED && trap_return == -2
		/*
		 * With POSIX, "return" on its own in a trap doesn't
		 * update $? --- we keep the status from before the
		 * trap.
		 */
		&& !(isset(POSIXTRAPS) && implicit)) {
		trap_state = TRAP_STATE_FORCE_RETURN;
		trap_return = lastval;
	    }
	    return lastval;
	}
	zexit(num, ZEXIT_NORMAL);	/* else treat return as logout/exit */
	break;
    case BIN_LOGOUT:
	if (unset(LOGINSHELL)) {
	    zerrnam(name, "not login shell");
	    return 1;
	}
	/*FALLTHROUGH*/
    case BIN_EXIT:
	if (locallevel > forklevel && shell_exiting != -1) {
	    /*
	     * We don't exit directly from functions to allow tidying
	     * up, in particular EXIT traps.  We still need to perform
	     * the usual interactive tests to see if we can exit at
	     * all, however.
	     *
	     * If we are forked, we exit the shell at the function depth
	     * at which we became a subshell, hence the comparison.
	     *
	     * If we are already exiting... give this all up as
	     * a bad job.
	     */
	    if (stopmsg || (zexit(0, ZEXIT_DEFERRED), !stopmsg)) {
		retflag = 1;
		breaks = loops;
		exit_pending = 1;
		exit_level = locallevel;
		exit_val = num;
	    }
	} else
	    zexit(num, ZEXIT_NORMAL);
	break;
    }
    return 0;
}

/* we have printed a 'you have stopped (running) jobs.' message */

/**/
mod_export int stopmsg;

/* check to see if user has jobs running/stopped */

/**/
static void
checkjobs(void)
{
    int i;

    for (i = 1; i <= maxjob; i++)
	if (i != thisjob && (jobtab[i].stat & STAT_LOCKED) &&
	    !(jobtab[i].stat & STAT_NOPRINT) &&
	    (isset(CHECKRUNNINGJOBS) || jobtab[i].stat & STAT_STOPPED))
	    break;
    if (i <= maxjob) {
	if (jobtab[i].stat & STAT_STOPPED) {

#ifdef USE_SUSPENDED
	    zerr("you have suspended jobs.");
#else
	    zerr("you have stopped jobs.");
#endif

	} else
	    zerr("you have running jobs.");
	stopmsg = 1;
    }
}

/*
 * -1 if the shell is already committed to exit.
 * positive if zexit() was already called.
 */

/**/
int shell_exiting;

/*
 * Exit status if explicitly set by an exit command.
 * This is complicated by the fact the exit command may be within
 * a function whose state we need to unwind (exit_pending set
 * and the exit will happen up the stack), or we may need to execute
 * additional code such as a trap after we are committed to exiting
 * (shell_exiting and the exit will happen down the stack).
 *
 * It's lucky this is all so obvious there is no possibility of any
 * bugs.  (C.f. the entire rest of the shell.)
 */
/**/
int exit_val;

/*
 * Actually exit the shell, working out the status locally.
 * This is exit_val if "exit" has explicitly been called in the shell,
 * else lastval.
 */

/**/
void
realexit(void)
{
    exit((shell_exiting || exit_pending) ? exit_val : lastval);
}

/* As realexit(), but call _exit instead */

/**/
void
_realexit(void)
{
    _exit((shell_exiting || exit_pending) ? exit_val : lastval);
}

/* exit the shell.  val is the return value of the shell.  *
 * from_where is
 *   ZEXIT_SIGNAL   if zexit is called because of a signal
 *   ZEXIT_DEFERRED if we can't actually exit yet (e.g., functions need
 *                  terminating) but should perform the usual interactive
 *                  tests.
 */

/**/
mod_export void
zexit(int val, enum zexit_t from_where)
{
    /*
     * Don't do anything recursively:  see below.
     * Do, however, update exit status --- there's no nesting,
     * a later value always overrides an earlier.
     */
    exit_val = val;
    if (shell_exiting == -1)
	return;

    if (isset(MONITOR) && !stopmsg && from_where != ZEXIT_SIGNAL) {
	scanjobs();    /* check if jobs need printing           */
	if (isset(CHECKJOBS))
	    checkjobs();   /* check if any jobs are running/stopped */
	if (stopmsg) {
	    stopmsg = 2;
	    return;
	}
    }
    /* Positive shell_exiting means we have been here before */
    if (from_where == ZEXIT_DEFERRED ||
	(shell_exiting++ && from_where != ZEXIT_NORMAL))
	return;

    /*
     * We're now committed to exiting.  Set shell_exiting to -1 to
     * indicate we shouldn't do any recursive processing.
     */
    shell_exiting = -1;
    /*
     * We want to do all remaining processing regardless of preceding
     * errors, even user interrupts.
     */
    errflag = 0;

    if (isset(MONITOR)) {
	/* send SIGHUP to any jobs left running  */
	killrunjobs(from_where == ZEXIT_SIGNAL);
    }
    if (isset(RCS) && interact) {
	if (!nohistsave) {
	    int writeflags = HFILE_USE_OPTIONS;
	    if (from_where == ZEXIT_SIGNAL)
		writeflags |= HFILE_NO_REWRITE;
	    saveandpophiststack(1, writeflags);
	    savehistfile(NULL, 1, writeflags);
	}
	if (islogin && !subsh) {
	    sourcehome(".zlogout");
#ifdef GLOBAL_ZLOGOUT
	    if (isset(RCS) && isset(GLOBALRCS))
		source(GLOBAL_ZLOGOUT);
#endif
	}
    }
    lastval = exit_val;
    /*
     * Now we are committed to exiting any previous state
     * is irrelevant.  Ensure trap can run.
     */
    errflag = intrap = 0;
    if (sigtrapped[SIGEXIT])
	dotrap(SIGEXIT);
    callhookfunc("zshexit", NULL, 1, NULL);
    runhookdef(EXITHOOK, NULL);
    if (opts[MONITOR] && interact && (SHTTY != -1)) {
       release_pgrp();
    }
    if (mypid != getpid())
	_exit(exit_val);
    else
	exit(exit_val);
}

/* . (dot), source */

/**/
int
bin_dot(char *name, char **argv, UNUSED(Options ops), UNUSED(int func))
{
    char **old, *old0 = NULL;
    int diddot = 0, dotdot = 0;
    char *s, **t, *enam, *arg0, *buf;
    struct stat st;
    enum source_return ret;

    if (!*argv)
	return 0;
    old = pparams;
    /* get arguments for the script */
    if (argv[1])
	pparams = zarrdup(argv + 1);

    enam = arg0 = ztrdup(*argv);
    if (isset(FUNCTIONARGZERO)) {
	old0 = argzero;
	argzero = ztrdup(arg0);
    }
    s = unmeta(enam);
    errno = ENOENT;
    ret = SOURCE_NOT_FOUND;
    /* for source only, check in current directory first */
    if (*name != '.' && access(s, F_OK) == 0
	&& stat(s, &st) >= 0 && !S_ISDIR(st.st_mode)) {
	diddot = 1;
	ret = source(enam);
    }
    if (ret == SOURCE_NOT_FOUND) {
	/* use a path with / in it */
	for (s = arg0; *s; s++)
	    if (*s == '/') {
		if (*arg0 == '.') {
		    if (arg0 + 1 == s)
			++diddot;
		    else if (arg0[1] == '.' && arg0 + 2 == s)
			++dotdot;
		}
		ret = source(arg0);
		break;
	    }
	if (!*s || (ret == SOURCE_NOT_FOUND &&
		    isset(PATHDIRS) && diddot < 2 && dotdot == 0)) {
	    pushheap();
	    /* search path for script */
	    for (t = path; *t; t++) {
		if (!(*t)[0] || ((*t)[0] == '.' && !(*t)[1])) {
		    if (diddot)
			continue;
		    diddot = 1;
		    buf = dupstring(arg0);
		} else
		    buf = zhtricat(*t, "/", arg0);

		s = unmeta(buf);
		if (access(s, F_OK) == 0 && stat(s, &st) >= 0
		    && !S_ISDIR(st.st_mode)) {
		    ret = source(enam = buf);
		    break;
		}
	    }
	    popheap();
	}
    }
    /* clean up and return */
    if (argv[1]) {
	freearray(pparams);
	pparams = old;
    }
    if (ret == SOURCE_NOT_FOUND) {
	if (isset(POSIXBUILTINS)) {
	    /* hard error in POSIX (we'll exit later) */
	    zerrnam(name, "%e: %s", errno, enam);
	} else {
	    zwarnnam(name, "%e: %s", errno, enam);
	}
    }
    zsfree(arg0);
    if (old0) {
	zsfree(argzero);
	argzero = old0;
    }
    return ret == SOURCE_OK ? lastval : 128 - ret;
}

/*
 * common for bin_emulate and bin_eval
 */

static int
eval(char **argv)
{
    Eprog prog;
    char *oscriptname = scriptname;
    int oineval = ineval, fpushed;
    struct funcstack fstack;

    /*
     * If EVALLINENO is not set, we use the line number of the
     * environment and must flag this up to exec.c.  Otherwise,
     * we use a special script name to indicate the special line number.
     */
    ineval = !isset(EVALLINENO);
    if (!ineval) {
	scriptname = "(eval)";
	fstack.prev = funcstack;
	fstack.name = scriptname;
	fstack.caller = funcstack ? funcstack->name : dupstring(argzero);
	fstack.lineno = lineno;
	fstack.tp = FS_EVAL;

	/*
	 * To get file line numbers, we need to know if parent is
	 * the original script/shell or a sourced file, in which
	 * case we use the line number raw, or a function or eval,
	 * in which case we need to deduce where that came from.
	 *
	 * This replicates the logic for working out the information
	 * for $funcfiletrace---eval is similar to an inlined function
	 * call from a tracing perspective.
	 */
	if (!funcstack || funcstack->tp == FS_SOURCE) {
	    fstack.flineno = fstack.lineno;
	    fstack.filename = fstack.caller;
	} else {
	    fstack.flineno = funcstack->flineno + lineno;
	    /*
	     * Line numbers in eval start from 1, not zero,
	     * so offset by one to get line in file.
	     */
	    if (funcstack->tp == FS_EVAL)
		fstack.flineno--;
	    fstack.filename = funcstack->filename;
	    if (!fstack.filename)
		fstack.filename = "";
	}
	funcstack = &fstack;

	fpushed = 1;
    } else
	fpushed = 0;

    prog = parse_string(zjoin(argv, ' ', 1), 1);
    if (prog) {
	if (wc_code(*prog->prog) != WC_LIST) {
	    /* No code to execute */
	    lastval = 0;
	} else {
	    execode(prog, 1, 0, "eval");

	    if (errflag && !lastval)
		lastval = errflag;
	}
    } else {
	lastval = 1;
    }

    if (fpushed)
	funcstack = funcstack->prev;

    errflag &= ~ERRFLAG_ERROR;
    scriptname = oscriptname;
    ineval = oineval;

    return lastval;
}

/* emulate: set emulation mode and optionally evaluate shell code */

/**/
int
bin_emulate(char *nam, char **argv, Options ops, UNUSED(int func))
{
    int opt_L = OPT_ISSET(ops, 'L');
    int opt_R = OPT_ISSET(ops, 'R');
    int opt_l = OPT_ISSET(ops, 'l');
    int saveemulation, savehackchar;
    int ret = 1, new_emulation;
    unsigned int savepatterns;
    char saveopts[OPT_SIZE], new_opts[OPT_SIZE];
    char *cmd = 0;
    const char *shname = *argv;
    LinkList optlist;
    LinkNode optnode;
    Emulation_options save_sticky;
    OptIndex *on_ptr, *off_ptr;

    /* without arguments just print current emulation */
    if (!shname) {
	if (opt_L || opt_R) {
	    zwarnnam(nam, "not enough arguments");
	    return 1;
	}

	switch(SHELL_EMULATION()) {
	case EMULATE_CSH:
	    shname = "csh";
	    break;

	case EMULATE_KSH:
	    shname = "ksh";
	    break;

	case EMULATE_SH:
	    shname = "sh";
	    break;

	default:
	    shname = "zsh";
	    break;
	}

	printf("%s\n", shname);
	return 0;
    }

    /* with single argument set current emulation */
    if (!argv[1]) {
	char *cmdopts;
	if (opt_l) {
	    cmdopts = (char *)zhalloc(OPT_SIZE);
	    memcpy(cmdopts, opts, OPT_SIZE);
	} else
	    cmdopts = opts;
	emulate(shname, opt_R, &emulation, cmdopts);
	if (opt_L)
	    cmdopts[LOCALOPTIONS] = cmdopts[LOCALTRAPS] =
		cmdopts[LOCALPATTERNS] = 1;
	if (opt_l) {
	    list_emulate_options(cmdopts, opt_R);
	    return 0;
	}
	clearpatterndisables();
	return 0;
    }

    if (opt_l) {
	zwarnnam(nam, "too many arguments for -l");
	return 1;
    }

    argv++;
    memcpy(saveopts, opts, sizeof(opts));
    memcpy(new_opts, opts, sizeof(opts));
    savehackchar = keyboardhackchar;
    emulate(shname, opt_R, &new_emulation, new_opts);
    optlist = newlinklist();
    if (parseopts(nam, &argv, new_opts, &cmd, optlist, 0, NULL)) {
	ret = 1;
	goto restore;
    }

    /* parseopts() has consumed anything that looks like an option */
    if (*argv) {
	zwarnnam(nam, "unknown argument %s", *argv);
	goto restore;
    }

    savepatterns = savepatterndisables();
    /*
     * All emulations start with an empty set of pattern disables,
     * hence no special "sticky" behaviour is required.
     */
    clearpatterndisables();

    saveemulation = emulation;
    emulation = new_emulation;
    memcpy(opts, new_opts, sizeof(opts));
    /* If "-c command" is given, evaluate command using specified
     * emulation mode.
     */
    if (cmd) {
	if (opt_L) {
	    zwarnnam(nam, "option -L incompatible with -c");
	    goto restore2;
	}
	*--argv = cmd;	/* on stack, never free()d, see execbuiltin() */
    } else {
	if (opt_L)
	    opts[LOCALOPTIONS] = opts[LOCALTRAPS] = opts[LOCALPATTERNS] = 1;
	return 0;
    }

    save_sticky = sticky;
    sticky = hcalloc(sizeof(*sticky));
    sticky->emulation = emulation;
    for (optnode = firstnode(optlist); optnode; incnode(optnode)) {
	/* Data is index into new_opts */
	char *optptr = (char *)getdata(optnode);
	if (*optptr)
	    sticky->n_on_opts++;
	else
	    sticky->n_off_opts++;
    }
    if (sticky->n_on_opts)
	on_ptr = sticky->on_opts =
	    zhalloc(sticky->n_on_opts * sizeof(*sticky->on_opts));
    else
	on_ptr = NULL;
    if (sticky->n_off_opts)
	off_ptr = sticky->off_opts = zhalloc(sticky->n_off_opts *
					     sizeof(*sticky->off_opts));
    else
	off_ptr = NULL;
    for (optnode = firstnode(optlist); optnode; incnode(optnode)) {
	/* Data is index into new_opts */
	char *optptr = (char *)getdata(optnode);
	int optno = optptr - new_opts;
	if (*optptr)
	    *on_ptr++ = optno;
	else
	    *off_ptr++ = optno;
    }
    ret = eval(argv);
    sticky = save_sticky;
restore2:
    emulation = saveemulation;
    memcpy(opts, saveopts, sizeof(opts));
    restorepatterndisables(savepatterns);
restore:
    keyboardhackchar = savehackchar;
    inittyptab();	/* restore banghist */
    return ret;
}

/* eval: simple evaluation */

/**/
mod_export int ineval;

/**/
int
bin_eval(UNUSED(char *nam), char **argv, UNUSED(Options ops), UNUSED(int func))
{
    return eval(argv);
}

static char *zbuf;
static int readfd;

/* Read a character from readfd, or from the buffer zbuf.  Return EOF on end of
file/buffer. */

/* read: get a line of input, or (for compctl functions) return some *
 * useful data about the state of the editing line.  The -E and -e   *
 * options mean that the result should be sent to stdout.  -e means, *
 * in addition, that the result should not actually be assigned to   *
 * the specified parameters.                                         */

/**/
int
bin_read(char *name, char **args, Options ops, UNUSED(int func))
{
    char *reply, *readpmpt;
    int bsiz, c = 0, gotnl = 0, al = 0, first, nchars = 1, bslash, keys = 0;
    int haso = 0;	/* true if /dev/tty has been opened specially */
    int isem = !strcmp(term, "emacs"), izle = zleactive;
    char *buf, *bptr, *firstarg, *zbuforig;
    LinkList readll = newlinklist();
    FILE *oshout = NULL;
    int readchar = -1, val, resettty = 0;
    struct ttyinfo saveti;
    char d;
    long izle_timeout = 0;
#ifdef MULTIBYTE_SUPPORT
    wchar_t delim = L'\n', wc;
    mbstate_t mbs;
    char *laststart;
    size_t ret;
#else
    char delim = '\n';
#endif

    if (OPT_HASARG(ops,c='k')) {
	char *eptr, *optarg = OPT_ARG(ops,c);
	nchars = (int)zstrtol(optarg, &eptr, 10);
	if (*eptr) {
	    zwarnnam(name, "number expected after -%c: %s", c, optarg);
	    return 1;
	}
    }
    /* This `*args++ : *args' looks a bit weird, but it works around a bug
     * in gcc-2.8.1 under DU 4.0. */
    firstarg = (*args && **args == '?' ? *args++ : *args);
    reply = *args ? *args++ : OPT_ISSET(ops,'A') ? "reply" : "REPLY";

    if (OPT_ISSET(ops,'A') && *args) {
	zwarnnam(name, "only one array argument allowed");
	return 1;
    }

    /* handle compctl case */
    if(OPT_ISSET(ops,'l') || OPT_ISSET(ops,'c'))
	return compctlreadptr(name, args, ops, reply);

    if ((OPT_ISSET(ops,'k') || OPT_ISSET(ops,'q')) &&
	!OPT_ISSET(ops,'u') && !OPT_ISSET(ops,'p')) {
	if (!zleactive) {
	    if (SHTTY == -1) {
		/* need to open /dev/tty specially */
		if ((SHTTY = open("/dev/tty", O_RDWR|O_NOCTTY)) != -1) {
		    haso = 1;
		    oshout = shout;
		    init_shout();
		}
	    } else if (!shout) {
		/* We need an output FILE* on the tty */
		init_shout();
	    }
	    /* We should have a SHTTY opened by now. */
	    if (SHTTY == -1) {
		/* Unfortunately, we didn't. */
		fprintf(stderr, "not interactive and can't open terminal\n");
		fflush(stderr);
		return 1;
	    }
	    if (unset(INTERACTIVE))
		gettyinfo(&shttyinfo);
	    /* attach to the tty */
	    attachtty(mypgrp);
	    if (!isem)
		setcbreak();
	    readfd = SHTTY;
	}
	keys = 1;
    } else if (OPT_HASARG(ops,'u') && !OPT_ISSET(ops,'p')) {
	/* -u means take input from the specified file descriptor. */
	char *eptr, *argptr = OPT_ARG(ops,'u');
	/* The old code handled -up, but that was never documented. Still...*/
	if (!strcmp(argptr, "p")) {
	    readfd = coprocin;
	    if (readfd < 0) {
		zwarnnam(name, "-p: no coprocess");
		return 1;
	    }
	} else {
	    readfd = (int)zstrtol(argptr, &eptr, 10);
	    if (*eptr) {
		zwarnnam(name, "number expected after -%c: %s", 'u', argptr);
		return 1;
	    }
	}
#if 0
	/* This code is left as a warning to future generations --- pws. */
	for (readfd = 9; readfd && !OPT_ISSET(ops,readfd + '0'); --readfd);
#endif
	izle = 0;
    } else if (OPT_ISSET(ops,'p')) {
	readfd = coprocin;
	if (readfd < 0) {
	    zwarnnam(name, "-p: no coprocess");
	    return 1;
	}
	izle = 0;
    } else
	readfd = izle = 0;

    if (OPT_ISSET(ops,'s') && SHTTY != -1) {
	struct ttyinfo ti;
	gettyinfo(&ti);
	saveti = ti;
	resettty = 1;
#ifdef HAS_TIO
	ti.tio.c_lflag &= ~ECHO;
#else
	ti.sgttyb.sg_flags &= ~ECHO;
#endif
	settyinfo(&ti);
    }

    /* handle prompt */
    if (firstarg) {
	for (readpmpt = firstarg;
	     *readpmpt && *readpmpt != '?'; readpmpt++);
	if (*readpmpt++) {
	    if (keys || isatty(0)) {
		zputs(readpmpt, (shout ? shout : stderr));
		fflush(shout ? shout : stderr);
	    }
	    readpmpt[-1] = '\0';
	}
    }

    if (OPT_ISSET(ops,'d')) {
	char *delimstr = OPT_ARG(ops,'d');
#ifdef MULTIBYTE_SUPPORT
	wint_t wi;

	if (isset(MULTIBYTE)) {
	    mb_charinit();
	    (void)mb_metacharlenconv(delimstr, &wi);
	}
	else
	    wi = WEOF;
	if (wi != WEOF)
	    delim = (wchar_t)wi;
	else
	    delim = (wchar_t)((delimstr[0] == Meta) ?
			      delimstr[1] ^ 32 : delimstr[0]);
#else
        delim = (delimstr[0] == Meta) ? delimstr[1] ^ 32 : delimstr[0];
#endif
	if (SHTTY != -1) {
	    struct ttyinfo ti;
	    gettyinfo(&ti);
	    if (! resettty) {
	      saveti = ti;
	      resettty = 1;
	    }
#ifdef HAS_TIO
	    ti.tio.c_lflag &= ~ICANON;
	    ti.tio.c_cc[VMIN] = 1;
	    ti.tio.c_cc[VTIME] = 0;
#else
	    ti.sgttyb.sg_flags |= CBREAK;
#endif
	    settyinfo(&ti);
	}
    }
    if (OPT_ISSET(ops,'t')) {
	zlong timeout = 0;
	if (OPT_HASARG(ops,'t')) {
	    mnumber mn = zero_mnumber;
	    mn = matheval(OPT_ARG(ops,'t'));
	    if (errflag)
		return 1;
	    if (mn.type == MN_FLOAT) {
		mn.u.d *= 1e6;
		timeout = (zlong)mn.u.d;
	    } else {
		timeout = (zlong)mn.u.l * (zlong)1000000;
	    }
	}
	if (izle) {
	    /*
	     * Timeout is in 100ths of a second rather than us.
	     * See calc_timeout() in zle_main for format of this.
	     */
	    timeout = -(timeout/(zlong)10000 + 1L);
	    izle_timeout = (long)timeout;
#ifdef LONG_MAX
	    /* saturate if range exceeded */
	    if ((zlong)izle_timeout != timeout)
		izle_timeout = LONG_MAX;
#endif
	} else {
	    if (readfd == -1 ||
		!read_poll(readfd, &readchar, keys && !zleactive,
			   timeout)) {
		if (keys && !zleactive && !isem)
		    settyinfo(&shttyinfo);
		else if (resettty && SHTTY != -1)
		    settyinfo(&saveti);
		if (haso) {
		    fclose(shout);
		    shout = oshout;
		    SHTTY = -1;
		}
		return OPT_ISSET(ops,'q') ? 2 : 1;
	    }
	}
    }

#ifdef MULTIBYTE_SUPPORT
    memset(&mbs, 0, sizeof(mbs));
#endif

    /*
     * option -k means read only a given number of characters (default 1)
     * option -q means get one character, and interpret it as a Y or N
     */
    if (OPT_ISSET(ops,'k') || OPT_ISSET(ops,'q')) {
	int eof = 0;
	/* allocate buffer space for result */
#ifdef MULTIBYTE_SUPPORT
	bptr = buf = (char *)zalloc(nchars*MB_CUR_MAX+1);
#else
	bptr = buf = (char *)zalloc(nchars+1);
#endif

	do {
	    if (izle) {
		zleentry(ZLE_CMD_GET_KEY, izle_timeout, NULL, &val);
		if (val < 0) {
		    eof = 1;
		    break;
		}
		*bptr = (char) val;
#ifdef MULTIBYTE_SUPPORT
		if (isset(MULTIBYTE)) {
		    ret = mbrlen(bptr++, 1, &mbs);
		    if (ret == MB_INVALID)
			memset(&mbs, 0, sizeof(mbs));
		    /* treat invalid as single character */
		    if (ret != MB_INCOMPLETE)
			nchars--;
		    continue;
		} else {
		    bptr++;
		    nchars--;
		}
#else
		bptr++;
		nchars--;
#endif
	    } else {
		/* If read returns 0, is end of file */
		if (readchar >= 0) {
		    *bptr = readchar;
		    val = 1;
		    readchar = -1;
		} else {
		    while ((val = read(readfd, bptr, nchars)) < 0) {
			if (errno != EINTR ||
			    errflag || retflag || breaks || contflag)
			    break;
		    }
		    if (val <= 0) {
			eof = 1;
			break;
		    }
		}

#ifdef MULTIBYTE_SUPPORT
		if (isset(MULTIBYTE)) {
		    while (val > 0) {
			ret = mbrlen(bptr, val, &mbs);
			if (ret == MB_INCOMPLETE) {
			    bptr += val;
			    break;
			} else {
			    if (ret == MB_INVALID) {
				memset(&mbs, 0, sizeof(mbs));
				/* treat as single byte */
				ret = 1;
			    }
			    else if (ret == 0) /* handle null as normal char */
				ret = 1;
			    else if (ret > (size_t)val) {
				/* Some mbrlen()s return the full char len */
				ret = val;
			    }
			    nchars--;
			    val -= ret;
			    bptr += ret;
			}
		    }
		    continue;
		}
#endif
		/* decrement number of characters read from number required */
		nchars -= val;

		/* increment pointer past read characters */
		bptr += val;
	    }
	} while (nchars > 0);

	if (!izle && !OPT_ISSET(ops,'u') && !OPT_ISSET(ops,'p')) {
	    /* dispose of result appropriately, etc. */
	    if (isem)
		while (val > 0 && read(SHTTY, &d, 1) == 1 && d != '\n');
	    else {
		settyinfo(&shttyinfo);
		resettty = 0;
	    }
	    if (haso) {
		fclose(shout);	/* close(SHTTY) */
		shout = oshout;
		SHTTY = -1;
	    }
	}

	if (OPT_ISSET(ops,'q'))
	{
	    /*
	     * Keep eof as status but status is now whether we read
	     * 'y' or 'Y'.  If we timed out, status is 2.
	     */
	    if (eof)
		eof = 2;
	    else
		eof = (bptr - buf != 1 || (buf[0] != 'y' && buf[0] != 'Y'));
	    buf[0] = eof ? 'n' : 'y';
	    bptr = buf + 1;
	}
	if (OPT_ISSET(ops,'e') || OPT_ISSET(ops,'E'))
	    fwrite(buf, bptr - buf, 1, stdout);
	if (!OPT_ISSET(ops,'e'))
	    setsparam(reply, metafy(buf, bptr - buf, META_REALLOC));
	else
	    zfree(buf, bptr - buf + 1);
	if (resettty && SHTTY != -1)
	    settyinfo(&saveti);
	return eof;
    }

    /* All possible special types of input have been exhausted.  Take one line,
       and assign words to the parameters until they run out.  Leftover words go
       onto the last parameter.  If an array is specified, all the words become
       separate elements of the array. */

    zbuforig = zbuf = (!OPT_ISSET(ops,'z')) ? NULL :
	(nonempty(bufstack)) ? (char *) getlinknode(bufstack) : ztrdup("");
    first = 1;
    bslash = 0;
    while (*args || (OPT_ISSET(ops,'A') && !gotnl)) {
	sigset_t s = child_unblock();
	buf = bptr = (char *)zalloc(bsiz = 64);
#ifdef MULTIBYTE_SUPPORT
	laststart = buf;
	ret = MB_INCOMPLETE;
#endif
	/* get input, a character at a time */
	while (!gotnl) {
	    c = zread(izle, &readchar, izle_timeout);
	    /* \ at the end of a line indicates a continuation *
	     * line, except in raw mode (-r option)            */
#ifdef MULTIBYTE_SUPPORT
	    if (c == EOF) {
		/* not waiting to be completed any more */
		ret = 0;
		break;
	    }
	    *bptr = (char)c;
	    if (isset(MULTIBYTE)) {
		ret = mbrtowc(&wc, bptr, 1, &mbs);
		if (!ret)	/* NULL */
		    ret = 1;
	    } else {
		ret = 1;
		wc = (wchar_t)c;
	    }
	    if (ret != MB_INCOMPLETE) {
		if (ret == MB_INVALID) {
		    memset(&mbs, 0, sizeof(mbs));
		    /* Treat this as a single character */
		    wc = (wchar_t)c;
		    laststart = bptr;
		}
		if (bslash && wc == delim) {
		    bslash = 0;
		    continue;
		}
		if (wc == delim)
		    break;
		/*
		 * `first' is non-zero if any separator we encounter is a
		 * non-whitespace separator, which means that anything
		 * (even an empty string) between, before or after separators
		 * is significant.  If it is zero, we have a whitespace
		 * separator, which shouldn't cause extra empty strings to
		 * be emitted.  Hence the test for (*buf || first) when
		 * we assign the result of reading a word.
		 */
		if (!bslash && wcsitype(wc, ISEP)) {
		    if (bptr != buf ||
			(!(c < 128 && iwsep(c)) && first)) {
			first |= !(c < 128 && iwsep(c));
			break;
		    }
		    first |= !(c < 128 && iwsep(c));
		    continue;
		}
		bslash = (wc == L'\\' && !bslash && !OPT_ISSET(ops,'r'));
		if (bslash)
		    continue;
		first = 0;
	    }
	    if (imeta(STOUC(*bptr))) {
		bptr[1] = bptr[0] ^ 32;
		bptr[0] = Meta;
		bptr += 2;
	    }
	    else
		bptr++;
	    if (ret != MB_INCOMPLETE)
		laststart = bptr;
#else
	    if (c == EOF)
		break;
	    if (bslash && c == delim) {
		bslash = 0;
		continue;
	    }
	    if (c == delim)
		break;
	    /*
	     * `first' is non-zero if any separator we encounter is a
	     * non-whitespace separator, which means that anything
	     * (even an empty string) between, before or after separators
	     * is significant.  If it is zero, we have a whitespace
	     * separator, which shouldn't cause extra empty strings to
	     * be emitted.  Hence the test for (*buf || first) when
	     * we assign the result of reading a word.
	     */
	    if (!bslash && isep(c)) {
		if (bptr != buf || (!iwsep(c) && first)) {
		    first |= !iwsep(c);
		    break;
		}
		first |= !iwsep(c);
		continue;
	    }
	    bslash = c == '\\' && !bslash && !OPT_ISSET(ops,'r');
	    if (bslash)
		continue;
	    first = 0;
	    if (imeta(c)) {
		*bptr++ = Meta;
		*bptr++ = c ^ 32;
	    } else
		*bptr++ = c;
#endif
	    /* increase the buffer size, if necessary */
	    if (bptr >= buf + bsiz - 1) {
		int blen = bptr - buf;
#ifdef MULTIBYTE_SUPPORT
		int llen = laststart - buf;
#endif

		buf = realloc(buf, bsiz *= 2);
		bptr = buf + blen;
#ifdef MULTIBYTE_SUPPORT
		laststart = buf + llen;
#endif
	    }
	}
	signal_setmask(s);
#ifdef MULTIBYTE_SUPPORT
	if (c == EOF) {
	    gotnl = 1;
	    *bptr = '\0';	/* see below */
	} else if (ret == MB_INCOMPLETE) {
	    /*
	     * We can only get here if there is an EOF in the
	     * middle of a character... safest to keep the debris,
	     * I suppose.
	     */
	    *bptr = '\0';
	} else {
	    if (wc == delim)
		gotnl = 1;
	    *laststart = '\0';
	}
#else
	if (c == delim || c == EOF)
	    gotnl = 1;
	*bptr = '\0';
#endif
	/* dispose of word appropriately */
	if (OPT_ISSET(ops,'e') ||
	    /*
	     * When we're doing an array assignment, we'll
	     * handle echoing at that point.  In all other
	     * cases (including -A with no assignment)
	     * we'll do it here.
	     */
	    (OPT_ISSET(ops,'E') && !OPT_ISSET(ops,'A'))) {
	    zputs(buf, stdout);
	    putchar('\n');
	}
	if (!OPT_ISSET(ops,'e') && (*buf || first || gotnl)) {
	    if (OPT_ISSET(ops,'A')) {
		addlinknode(readll, buf);
		al++;
	    } else
		setsparam(reply, buf);
	} else
	    free(buf);
	if (!OPT_ISSET(ops,'A'))
	    reply = *args++;
    }
    /* handle EOF */
    if (c == EOF) {
	if (readfd == coprocin) {
	    close(coprocin);
	    close(coprocout);
	    coprocin = coprocout = -1;
	}
    }
    /* final assignment (and display) of array parameter */
    if (OPT_ISSET(ops,'A')) {
	char **pp, **p = NULL;
	LinkNode n;

	p = (OPT_ISSET(ops,'e') ? (char **)NULL
	     : (char **)zalloc((al + 1) * sizeof(char *)));

	for (pp = p, n = firstnode(readll); n; incnode(n)) {
	    if (OPT_ISSET(ops,'E')) {
		zputs((char *) getdata(n), stdout);
		putchar('\n');
	    }
	    if (p)
		*pp++ = (char *)getdata(n);
	    else
		zsfree(getdata(n));
	}
	if (p) {
	    *pp++ = NULL;
	    setaparam(reply, p);
	}
	if (resettty && SHTTY != -1)
	    settyinfo(&saveti);
	return c == EOF;
    }
    buf = bptr = (char *)zalloc(bsiz = 64);
#ifdef MULTIBYTE_SUPPORT
    laststart = buf;
    ret = MB_INCOMPLETE;
#endif
    /* any remaining part of the line goes into one parameter */
    bslash = 0;
    if (!gotnl) {
	sigset_t s = child_unblock();
	for (;;) {
	    c = zread(izle, &readchar, izle_timeout);
#ifdef MULTIBYTE_SUPPORT
	    if (c == EOF) {
		/* not waiting to be completed any more */
		ret = 0;
		break;
	    }
	    *bptr = (char)c;
	    if (isset(MULTIBYTE)) {
		ret = mbrtowc(&wc, bptr, 1, &mbs);
		if (!ret)	/* NULL */
		    ret = 1;
	    } else {
		ret = 1;
		wc = (wchar_t)c;
	    }
	    if (ret != MB_INCOMPLETE) {
		if (ret == MB_INVALID) {
		    memset(&mbs, 0, sizeof(mbs));
		    /* Treat this as a single character */
		    wc = (wchar_t)c;
		    laststart = bptr;
		}
		/*
		 * \ at the end of a line introduces a continuation line,
		 * except in raw mode (-r option)
		 */
		if (bslash && wc == delim) {
		    bslash = 0;
		    continue;
		}
		if (wc == delim && !zbuf)
		    break;
		if (!bslash && bptr == buf && wcsitype(wc, ISEP)) {
		    if (c < 128 && iwsep(c))
			continue;
		    else if (!first) {
			first = 1;
			continue;
		    }
		}
		bslash = (wc == L'\\' && !bslash && !OPT_ISSET(ops,'r'));
		if (bslash)
		    continue;
	    }
	    if (imeta(STOUC(*bptr))) {
		bptr[1] = bptr[0] ^ 32;
		bptr[0] = Meta;
		bptr += 2;
	    }
	    else
		bptr++;
	    if (ret != MB_INCOMPLETE)
		laststart = bptr;
#else
	    /* \ at the end of a line introduces a continuation line, except in
	       raw mode (-r option) */
	    if (bslash && c == delim) {
		bslash = 0;
		continue;
	    }
	    if (c == EOF || (c == delim && !zbuf))
		break;
	    if (!bslash && isep(c) && bptr == buf) {
		if (iwsep(c))
		    continue;
		else if (!first) {
		    first = 1;
		    continue;
		}
	    }
	    bslash = c == '\\' && !bslash && !OPT_ISSET(ops,'r');
	    if (bslash)
		continue;
	    if (imeta(c)) {
		*bptr++ = Meta;
		*bptr++ = c ^ 32;
	    } else
		*bptr++ = c;
#endif
	    /* increase the buffer size, if necessary */
	    if (bptr >= buf + bsiz - 1) {
		int blen = bptr - buf;
#ifdef MULTIBYTE_SUPPORT
		int llen = laststart - buf;
#endif

		buf = realloc(buf, bsiz *= 2);
		bptr = buf + blen;
#ifdef MULTIBYTE_SUPPORT
		laststart = buf + llen;
#endif
	    }
	}
	signal_setmask(s);
    }
#ifdef MULTIBYTE_SUPPORT
    if (ret != MB_INCOMPLETE)
	bptr = laststart;
#endif
    /*
     * Strip trailing IFS whitespace.
     * iwsep can only be certain single-byte ASCII bytes, but we
     * must check the byte isn't metafied.
     */
    while (bptr > buf) {
	if (bptr > buf + 1 && bptr[-2] == Meta) {
	    /* non-ASCII, can't be IWSEP */
	    break;
	} else if (iwsep(bptr[-1]))
	    bptr--;
	else
	    break;
    }
    *bptr = '\0';
    if (resettty && SHTTY != -1)
	settyinfo(&saveti);
    /* final assignment of reply, etc. */
    if (OPT_ISSET(ops,'e') || OPT_ISSET(ops,'E')) {
	zputs(buf, stdout);
	putchar('\n');
    }
    if (!OPT_ISSET(ops,'e'))
	setsparam(reply, buf);
    else
	zsfree(buf);
    if (zbuforig) {
	char first = *zbuforig;

	zsfree(zbuforig);
	if (!first)
	    return 1;
    } else if (c == EOF) {
	if (readfd == coprocin) {
	    close(coprocin);
	    close(coprocout);
	    coprocin = coprocout = -1;
	}
	return 1;
    }
    /*
     * The following is to ensure a failure to set the parameter
     * causes a non-zero status return.  There are arguments for
     * turning a non-zero status into errflag more widely.
     */
    return errflag;
}

/**/
static int
zread(int izle, int *readchar, long izle_timeout)
{
    char cc, retry = 0;
    int ret;

    if (izle) {
	int c;
	zleentry(ZLE_CMD_GET_KEY, izle_timeout, NULL, &c);

	return (c < 0 ? EOF : c);
    }
    /* use zbuf if possible */
    if (zbuf) {
	/* If zbuf points to anything, it points to the next character in the
	   buffer.  This may be a null byte to indicate EOF.  If reading from the
	   buffer, move on the buffer pointer. */
	if (*zbuf == Meta)
	    return zbuf++, STOUC(*zbuf++ ^ 32);
	else
	    return (*zbuf) ? STOUC(*zbuf++) : EOF;
    }
    if (*readchar >= 0) {
	cc = *readchar;
	*readchar = -1;
	return STOUC(cc);
    }
    for (;;) {
	/* read a character from readfd */
	ret = read(readfd, &cc, 1);
	switch (ret) {
	case 1:
	    /* return the character read */
	    return STOUC(cc);
	case -1:
#if defined(EAGAIN) || defined(EWOULDBLOCK)
	    if (!retry && readfd == 0 && (
# ifdef EAGAIN
		errno == EAGAIN
#  ifdef EWOULDBLOCK
		||
#  endif /* EWOULDBLOCK */
# endif /* EAGAIN */
# ifdef EWOULDBLOCK
		errno == EWOULDBLOCK
# endif /* EWOULDBLOCK */
		) && setblock_stdin()) {
		retry = 1;
		continue;
	    } else
#endif /* EAGAIN || EWOULDBLOCK */
		if (errno == EINTR && !(errflag || retflag || breaks || contflag))
		    continue;
	    break;
	}
	return EOF;
    }
}

/* holds arguments for testlex() */
/**/
char **testargs, **curtestarg;

/* test, [: the old-style general purpose logical expression builtin */

/**/
void
testlex(void)
{
    if (tok == LEXERR)
	return;

    tokstr = *(curtestarg = testargs);
    if (!*testargs) {
	/* if tok is already zero, reading past the end:  error */
	tok = tok ? NULLTOK : LEXERR;
	return;
    } else if (!strcmp(*testargs, "-o"))
	tok = DBAR;
    else if (!strcmp(*testargs, "-a"))
	tok = DAMPER;
    else if (!strcmp(*testargs, "!"))
	tok = BANG;
    else if (!strcmp(*testargs, "("))
	tok = INPAR;
    else if (!strcmp(*testargs, ")"))
	tok = OUTPAR;
    else
	tok = STRING;
    testargs++;
}

/**/
int
bin_test(char *name, char **argv, UNUSED(Options ops), int func)
{
    char **s;
    Eprog prog;
    struct estate state;
    int nargs, sense = 0, ret;

    /* if "test" was invoked as "[", it needs a matching "]" *
     * which is subsequently ignored                         */
    if (func == BIN_BRACKET) {
	for (s = argv; *s; s++);
	if (s == argv || strcmp(s[-1], "]")) {
	    zwarnnam(name, "']' expected");
	    return 2;
	}
	s[-1] = NULL;
    }
    /* an empty argument list evaluates to false (1) */
    if (!*argv)
	return 1;

    /*
     * Implement some XSI extensions to POSIX here.
     * See
     * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
     */
    nargs = arrlen(argv);
    if (nargs == 3 || nargs == 4)
    {
	/*
	 * As parentheses are an extension, we need to be careful ---
	 * if this is a three-argument expression that could
	 * be a binary operator, prefer that.
	 */
	if (!strcmp(argv[0], "(") && !strcmp(argv[nargs-1],")") &&
	    (nargs != 3 || !is_cond_binary_op(argv[1]))) {
	    argv[nargs-1] = NULL;
	    argv++;
	}
	if (nargs == 4 && !strcmp("!", argv[0])) {
	    sense = 1;
	    argv++;
	}
    }

    zcontext_save();
    testargs = argv;
    tok = NULLTOK;
    condlex = testlex;
    testlex();
    prog = parse_cond();
    condlex = zshlex;

    if (errflag) {
	errflag &= ~ERRFLAG_ERROR;
	zcontext_restore();
	return 2;
    }

    if (!prog || tok == LEXERR) {
	zwarnnam(name, tokstr ? "parse error" : "argument expected");
	zcontext_restore();
	return 2;
    }
    zcontext_restore();

    if (*curtestarg) {
	zwarnnam(name, "too many arguments");
	return 2;
    }

    /* syntax is OK, so evaluate */

    state.prog = prog;
    state.pc = prog->prog;
    state.strs = prog->strs;

    ret = evalcond(&state, name);
    if (ret < 2 && sense)
	ret = ! ret;

    return ret;
}

/* display a time, provided in units of 1/60s, as minutes and seconds */
#define pttime(X) printf("%ldm%ld.%02lds",((long) (X))/(60 * clktck),\
			 ((long) (X))/clktck%clktck,\
			 ((long) (X))*100/clktck%100)

/* times: display, in a two-line format, the times provided by times(3) */

/**/
int
bin_times(UNUSED(char *name), UNUSED(char **argv), UNUSED(Options ops), UNUSED(int func))
{
    struct tms buf;
    long clktck = get_clktck();

    /* get time accounting information */
    if (times(&buf) == -1)
	return 1;
    pttime(buf.tms_utime);	/* user time */
    putchar(' ');
    pttime(buf.tms_stime);	/* system time */
    putchar('\n');
    pttime(buf.tms_cutime);	/* user time, children */
    putchar(' ');
    pttime(buf.tms_cstime);	/* system time, children */
    putchar('\n');
    return 0;
}

/* trap: set/unset signal traps */

/**/
int
bin_trap(char *name, char **argv, UNUSED(Options ops), UNUSED(int func))
{
    Eprog prog;
    char *arg, *s;
    int sig;

    if (*argv && !strcmp(*argv, "--"))
	argv++;

    /* If given no arguments, list all currently-set traps */
    if (!*argv) {
	queue_signals();
	for (sig = 0; sig < VSIGCOUNT; sig++) {
	    if (sigtrapped[sig] & ZSIG_FUNC) {
		HashNode hn;

		if ((hn = gettrapnode(sig, 0)))
		    shfunctab->printnode(hn, 0);
		DPUTS(!hn, "BUG: I did not find any trap functions!");
	    } else if (sigtrapped[sig]) {
		const char *name = getsigname(sig);
		if (!siglists[sig])
		    printf("trap -- '' %s\n", name);
		else {
		    s = getpermtext(siglists[sig], NULL, 0);
		    printf("trap -- ");
		    quotedzputs(s, stdout);
		    printf(" %s\n", name);
		    zsfree(s);
		}
	    }
	}
	unqueue_signals();
	return 0;
    }

    /* If we have a signal number, unset the specified *
     * signals.  With only -, remove all traps.        */
    if ((getsignum(*argv) != -1) || (!strcmp(*argv, "-") && argv++)) {
	if (!*argv) {
	    for (sig = 0; sig < VSIGCOUNT; sig++)
		unsettrap(sig);
	} else {
	    for (; *argv; argv++) {
		sig = getsignum(*argv);
		if (sig == -1) {
		    zwarnnam(name, "undefined signal: %s", *argv);
		    break;
		}
		unsettrap(sig);
	    }
	}
	return *argv != NULL;
    }

    /* Sort out the command to execute on trap */
    arg = *argv++;
    if (!*arg)
	prog = &dummy_eprog;
    else if (!(prog = parse_string(arg, 1))) {
	zwarnnam(name, "couldn't parse trap command");
	return 1;
    }

    /* set traps */
    for (; *argv; argv++) {
	Eprog t;
	int flags;

	sig = getsignum(*argv);
	if (sig == -1) {
	    zwarnnam(name, "undefined signal: %s", *argv);
	    break;
	}
	if (idigit(**argv) ||
	    !strcmp(sigs[sig], *argv) ||
	    (!strncmp("SIG", *argv, 3) && !strcmp(sigs[sig], *argv+3))) {
	    /* The signal was specified by number or by canonical name (with
	     * or without SIG prefix).
	     */
	    flags = 0;
	}
	else {
	    /*
	     * Record that the signal is used under an assumed name.
	     * If we ever have more than one alias per signal this
	     * will need improving.
	     */
	    flags = ZSIG_ALIAS;
	}
	t = dupeprog(prog, 0);
	if (settrap(sig, t, flags))
	    freeeprog(t);
    }
    return *argv != NULL;
}

/**/
int
bin_ttyctl(UNUSED(char *name), UNUSED(char **argv), Options ops, UNUSED(int func))
{
    if (OPT_ISSET(ops,'f'))
	ttyfrozen = 1;
    else if (OPT_ISSET(ops,'u'))
	ttyfrozen = 0;
    else
	printf("tty is %sfrozen\n", ttyfrozen ? "" : "not ");
    return 0;
}

/* let -- mathematical evaluation */

/**/
int
bin_let(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int func))
{
    mnumber val = zero_mnumber;

    while (*argv)
	val = matheval(*argv++);
    /* Errors in math evaluation in let are non-fatal. */
    errflag &= ~ERRFLAG_ERROR;
    /* should test for fabs(val.u.d) < epsilon? */
    return (val.type == MN_INTEGER) ? val.u.l == 0 : val.u.d == 0.0;
}

/* umask command.  umask may be specified as octal digits, or in the  *
 * symbolic form that chmod(1) uses.  Well, a subset of it.  Remember *
 * that only the bottom nine bits of umask are used, so there's no    *
 * point allowing the set{u,g}id and sticky bits to be specified.     */

/**/
int
bin_umask(char *nam, char **args, Options ops, UNUSED(int func))
{
    mode_t um;
    char *s = *args;

    /* Get the current umask. */
    queue_signals();
    um = umask(0777);
    umask(um);
    unqueue_signals();

    /* No arguments means to display the current setting. */
    if (!s) {
	if (OPT_ISSET(ops,'S')) {
	    char *who = "ugo";

	    while (*who) {
		char *what = "rwx";
		printf("%c=", *who++);
		while (*what) {
		    if (!(um & 0400))
			putchar(*what);
		    um <<= 1;
		    what++;
		}
		putchar(*who ? ',' : '\n');
	    }
	} else {
	    if (um & 0700)
		putchar('0');
	    printf("%03o\n", (unsigned)um);
	}
	return 0;
    }

    if (idigit(*s)) {
	/* Simple digital umask. */
	um = zstrtol(s, &s, 8);
	if (*s) {
	    zwarnnam(nam, "bad umask");
	    return 1;
	}
    } else {
	/* Symbolic notation -- slightly complicated. */
	int whomask, umaskop, mask;

	/* More than one symbolic argument may be used at once, each separated
	   by commas. */
	for (;;) {
	    /* First part of the argument -- who does this apply to?
	       u=owner, g=group, o=other. */
	    whomask = 0;
	    while (*s == 'u' || *s == 'g' || *s == 'o' || *s == 'a')
		if (*s == 'u')
		    s++, whomask |= 0700;
		else if (*s == 'g')
		    s++, whomask |= 0070;
		else if (*s == 'o')
		    s++, whomask |= 0007;
		else if (*s == 'a')
		    s++, whomask |= 0777;
	    /* Default whomask is everyone. */
	    if (!whomask)
		whomask = 0777;
	    /* Operation may be +, - or =. */
	    umaskop = (int)*s;
	    if (!(umaskop == '+' || umaskop == '-' || umaskop == '=')) {
		if (umaskop)
		    zwarnnam(nam, "bad symbolic mode operator: %c", umaskop);
		else
		    zwarnnam(nam, "bad umask");
		return 1;
	    }
	    /* Permissions mask -- r=read, w=write, x=execute. */
	    mask = 0;
	    while (*++s && *s != ',')
		if (*s == 'r')
		    mask |= 0444 & whomask;
		else if (*s == 'w')
		    mask |= 0222 & whomask;
		else if (*s == 'x')
		    mask |= 0111 & whomask;
		else {
		    zwarnnam(nam, "bad symbolic mode permission: %c", *s);
		    return 1;
		}
	    /* Apply parsed argument to um. */
	    if (umaskop == '+')
		um &= ~mask;
	    else if (umaskop == '-')
		um |= mask;
	    else		/* umaskop == '=' */
		um = (um | (whomask)) & ~mask;
	    if (*s == ',')
		s++;
	    else
		break;
	}
	if (*s) {
	    zwarnnam(nam, "bad character in symbolic mode: %c", *s);
	    return 1;
	}
    }

    /* Finally, set the new umask. */
    umask(um);
    return 0;
}

/* Generic builtin for facilities not available on this OS */

/**/
mod_export int
bin_notavail(char *nam, UNUSED(char **argv), UNUSED(Options ops), UNUSED(int func))
{
    zwarnnam(nam, "not available on this system");
    return 1;
}
debug log:

solving 35a0fb2db ...
found 35a0fb2db 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).