File: variable.c

package info (click to toggle)
yash 2.60-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,152 kB
  • sloc: ansic: 34,578; makefile: 851; sh: 808; sed: 16
file content (3433 lines) | stat: -rw-r--r-- 110,940 bytes parent folder | download
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
/* Yash: yet another shell */
/* variable.c: deals with shell variables and parameters */
/* (C) 2007-2025 magicant */

/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


#include "common.h"
#include "variable.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#if HAVE_GETTEXT
# include <libintl.h>
#endif
#include <limits.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include "builtin.h"
#include "configm.h"
#include "exec.h"
#include "expand.h"
#include "hashtable.h"
#include "input.h"
#include "option.h"
#include "parser.h"
#include "path.h"
#include "plist.h"
#include "sig.h"
#include "strbuf.h"
#include "util.h"
#include "xfnmatch.h"
#include "yash.h"
#if YASH_ENABLE_LINEEDIT
# include "lineedit/complete.h"
# include "lineedit/lineedit.h"
# include "lineedit/terminfo.h"
#endif


static const wchar_t *const path_variables[PA_count] = {
    [PA_PATH]     = L VAR_PATH,
    [PA_CDPATH]   = L VAR_CDPATH,
    [PA_LOADPATH] = L VAR_YASH_LOADPATH,
};


/* variable environment (= set of variables) */
/* not to be confused with environment variables */
typedef struct environ_T {
    struct environ_T *parent;      /* parent environment */
    struct hashtable_T contents;   /* hashtable containing variables */
    bool is_temporary;             /* for temporary assignment? */
    char **paths[PA_count];
} environ_T;
/* `contents' is a hashtable from (wchar_t *) to (variable_T *).
 * A variable name may contain any characters except L'\0' and L'=', though
 * assignment syntax disallows other characters.
 * Variable names starting with L'=' are used for special purposes.
 * The positional parameter is treated as an array whose name is L"=".
 * Note that the number of positional parameters is offset by 1 against the
 * array index.
 * An environment whose `is_temporary' is true is used for temporary variables. 
 * The elements of `paths' are arrays of the pathnames contained in the
 * $PATH, $CDPATH and $YASH_LOADPATH variables. They are NULL if the
 * corresponding variables are not set. */
#define VAR_positional "="

/* flags for variable attributes */
typedef enum vartype_T {
    VF_SCALAR,
    VF_ARRAY,
    VF_EXPORT   = 1 << 2,
    VF_READONLY = 1 << 3,
    VF_NODELETE = 1 << 4,
} vartype_T;
#define VF_MASK ((1 << 2) - 1)
/* For any variable, the variable type is either VF_SCALAR or VF_ARRAY,
 * possibly OR'ed with other flags. */

/* type of variables */
typedef struct variable_T {
    vartype_T v_type;
    union {
        wchar_t *value;
        struct {
            void **vals;
            size_t valc;
        } array;
    } v_contents;
    void (*v_getter)(struct variable_T *var);
} variable_T;
#define v_value v_contents.value
#define v_vals  v_contents.array.vals
#define v_valc  v_contents.array.valc
/* `v_vals' is a NULL-terminated array of pointers to wide strings.
 * `v_valc' is, of course, the number of elements in `v_vals'.
 * `v_value', `v_vals' and the elements of `v_vals' are `free'able.
 * `v_value' is NULL if the variable is declared but not yet assigned.
 * `v_vals' is always non-NULL, but it may contain no elements.
 * `v_getter' is the setter function, which is reset to NULL on reassignment.*/

/* type of shell functions (defined later) */
typedef struct function_T function_T;


static void varvaluefree(variable_T *v)
    __attribute__((nonnull));
static void varfree(variable_T *v);
static void varkvfree(kvpair_T kv);
static void varkvfree_reexport(kvpair_T kv);

static void init_pwd(void);

static variable_T *search_variable(const wchar_t *name)
    __attribute__((pure,nonnull));
static variable_T *search_array_and_check_if_changeable(const wchar_t *name)
    __attribute__((pure,nonnull));
static void update_environment(const wchar_t *name)
    __attribute__((nonnull));
static void reset_locale(const wchar_t *name)
    __attribute__((nonnull));
static void reset_locale_category(const wchar_t *name, int category)
    __attribute__((nonnull));
static variable_T *new_global(const wchar_t *name)
    __attribute__((nonnull));
static variable_T *new_local(const wchar_t *name)
    __attribute__((nonnull));
static variable_T *new_temporary(const wchar_t *name)
    __attribute__((nonnull));
static variable_T *new_variable(const wchar_t *name, scope_T scope)
    __attribute__((nonnull));
static void xtrace_variable(const wchar_t *name, const wchar_t *value)
    __attribute__((nonnull));
static void xtrace_array(const wchar_t *name, void *const *values)
    __attribute__((nonnull));
static size_t make_array_of_all_variables(bool global, kvpair_T **resultp)
    __attribute__((nonnull));
static void get_all_variables_rec(
        hashtable_T *table, environ_T *env, bool global)
    __attribute__((nonnull));

static void lineno_getter(variable_T *var)
    __attribute__((nonnull));
static void random_getter(variable_T *var)
    __attribute__((nonnull));
static unsigned next_random(void);

static void variable_set(const wchar_t *name, variable_T *var)
    __attribute__((nonnull(1)));

static char **convert_path_array(void **ary)
    __attribute__((malloc,warn_unused_result));
static void add_to_list_no_dup(plist_T *list, char *s)
    __attribute__((nonnull(1)));
static void reset_path(path_T name, variable_T *var);

static void funcfree(function_T *f);
static void funckvfree(kvpair_T kv);

static void hash_all_commands_recursively(const command_T *c);
static void hash_all_commands_in_and_or(const and_or_T *ao);
static void hash_all_commands_in_if(const ifcommand_T *ic);
static void hash_all_commands_in_case(const caseitem_T *ci);
static void tryhash_word_as_command(const wordunit_T *w);


/* the current environment */
static environ_T *current_env;
/* the top-level environment (the farthest from the current) */
static environ_T *first_env;

/* whether $RANDOM is functioning as a random number */
static bool random_active;

/* hashtable from function names (wchar_t *) to functions (function_T *). */
static hashtable_T functions;


/* Frees the value of the specified variable (but not the variable itself). */
/* This function does not change the value of `*v'. */
void varvaluefree(variable_T *v)
{
    switch (v->v_type & VF_MASK) {
        case VF_SCALAR:
            free(v->v_value);
            break;
        case VF_ARRAY:
            plfree(v->v_vals, free);
            break;
    }
}

/* Frees the specified variable. */
void varfree(variable_T *v)
{
    if (v != NULL) {
        varvaluefree(v);
        free(v);
    }
}

/* Frees the specified key-value pair of a variable name and a variable. */
void varkvfree(kvpair_T kv)
{
    free(kv.key);
    varfree(kv.value);
}

/* Calls `variable_set' and `update_environment' for `kv.key' and
 * calls `varkvfree'. */
void varkvfree_reexport(kvpair_T kv)
{
    if (kv.key == NULL) {
        assert(kv.value == NULL);
        return;
    }

    variable_set(kv.key, NULL);
    if (((variable_T *) kv.value)->v_type & VF_EXPORT)
        update_environment(kv.key);
    varkvfree(kv);
}

/* Initializes the top-level environment. */
void init_environment(void)
{
    assert(first_env == NULL && current_env == NULL);
    first_env = current_env = xmalloc(sizeof *current_env);
    current_env->parent = NULL;
    current_env->is_temporary = false;
    ht_init(&current_env->contents, hashwcs, htwcscmp);
//    for (size_t i = 0; i < PA_count; i++)
//      current_env->paths[i] = NULL;

    ht_init(&functions, hashwcs, htwcscmp);

    /* add all the existing environment variables to the variable environment */
    for (char **e = environ; *e != NULL; e++) {
        wchar_t *we = malloc_mbstowcs(*e);
        if (we == NULL)
            continue;

        wchar_t *eqp = wcschr(we, L'=');
        variable_T *v = xmalloc(sizeof *v);
        v->v_type = VF_SCALAR | VF_EXPORT;
        v->v_value = (eqp != NULL) ? xwcsdup(&eqp[1]) : NULL;
        v->v_getter = NULL;
        if (eqp != NULL) {
            *eqp = L'\0';
            we = xreallocn(we, eqp - we + 1, sizeof *we);
        }
        varkvfree(ht_set(&current_env->contents, we, v));
    }

    /* initialize path according to $PATH etc. */
    for (size_t i = 0; i < PA_count; i++)
        current_env->paths[i] = decompose_paths(getvar(path_variables[i]));
}

/* Initializes the default variables.
 * This function must be called after the shell options have been set. */
void init_variables(void)
{
    /* set $IFS */
    set_variable(L VAR_IFS, xwcsdup(DEFAULT_IFS), SCOPE_GLOBAL, false);

    /* set $LINENO */
    {
        variable_T *v = new_variable(L VAR_LINENO, SCOPE_GLOBAL);
        assert(v != NULL);
        v->v_type = VF_SCALAR | (v->v_type & VF_EXPORT);
        v->v_value = NULL;
        v->v_getter = lineno_getter;
        // variable_set(VAR_LINENO, v);
        // if (v->v_type & VF_EXPORT)
        //     update_environment(L VAR_LINENO);
    }

    /* set $MAILCHECK */
    if (getvar(L VAR_MAILCHECK) == NULL)
        set_variable(L VAR_MAILCHECK, xwcsdup(L"600"), SCOPE_GLOBAL, false);

    /* set $PS1~4 */
    {
        const wchar_t *ps1 = (geteuid() != 0) ? L"$ " : L"# ";
        set_variable(L VAR_PS1, xwcsdup(ps1),   SCOPE_GLOBAL, false);
        set_variable(L VAR_PS2, xwcsdup(L"> "), SCOPE_GLOBAL, false);
        set_variable(L VAR_PS4, xwcsdup(L"+ "), SCOPE_GLOBAL, false);
    }

    /* set $PWD */
    init_pwd();

    /* export $OLDPWD */
    {
        variable_T *v = new_global(L VAR_OLDPWD);
        assert(v != NULL);
        v->v_type |= VF_EXPORT;
        variable_set(L VAR_OLDPWD, v);
    }

    /* set $PPID */
    set_variable(L VAR_PPID, malloc_wprintf(L"%jd", (intmax_t) getppid()),
            SCOPE_GLOBAL, false);

    /* set $OPTIND */
    set_variable(L VAR_OPTIND, xwcsdup(L"1"), SCOPE_GLOBAL, false);

    /* set $RANDOM */
    if (!posixly_correct) {
        variable_T *v = new_variable(L VAR_RANDOM, SCOPE_GLOBAL);
        assert(v != NULL);
        v->v_type = VF_SCALAR;
        v->v_value = NULL;
        v->v_getter = random_getter;
        random_active = true;
        srand((unsigned) time(NULL) ^ (unsigned) shell_pid << 17);
    } else {
        random_active = false;
    }

    /* set $YASH_LOADPATH */
    if (getvar(L VAR_YASH_LOADPATH) == NULL)
        set_variable(L VAR_YASH_LOADPATH, xwcsdup(L DEFAULT_LOADPATH),
                SCOPE_GLOBAL, false);

    /* set $YASH_VERSION */
    set_variable(L VAR_YASH_VERSION, xwcsdup(L PACKAGE_VERSION),
            SCOPE_GLOBAL, false);
}

/* Reset the value of $PWD if
 *  - $PWD is not set, or
 *  - the value of $PWD isn't an absolute path, or
 *  - the value of $PWD isn't the actual current directory, or
 *  - the value of $PWD isn't canonicalized. */
void init_pwd(void)
{
    const char *pwd = getenv(VAR_PWD);
    if (pwd == NULL || pwd[0] != '/' || !is_same_file(pwd, "."))
        goto set;
    const wchar_t *wpwd = getvar(L VAR_PWD);
    if (wpwd == NULL || !is_normalized_path(wpwd))
        goto set;
    return;

    char *newpwd;
    wchar_t *wnewpwd;
set:
    newpwd = xgetcwd();
    if (newpwd == NULL) {
        xerror(errno, Ngt("failed to set $PWD"));
        return;
    }
    wnewpwd = realloc_mbstowcs(newpwd);
    if (wnewpwd == NULL) {
        xerror(0, Ngt("failed to set $PWD"));
        return;
    }
    set_variable(L VAR_PWD, wnewpwd, SCOPE_GLOBAL, true);
}

/* Searches for a variable with the specified name.
 * Returns NULL if none was found. */
variable_T *search_variable(const wchar_t *name)
{
    for (environ_T *env = current_env; env != NULL; env = env->parent) {
        variable_T *var = ht_get(&env->contents, name).value;
        if (var != NULL)
            return var;
    }
    return NULL;
}

/* Searches for an array with the specified name and checks if it is not read-
 * only. If unsuccessful, prints an error message and returns NULL. */
variable_T *search_array_and_check_if_changeable(const wchar_t *name)
{
    variable_T *array = search_variable(name);
    if (array == NULL || (array->v_type & VF_MASK) != VF_ARRAY) {
        xerror(0, Ngt("no such array $%ls"), name);
        return NULL;
    } else if (array->v_type & VF_READONLY) {
        xerror(0, Ngt("$%ls is read-only"), name);
        return NULL;
    }
    return array;
}

/* Update the value in `environ' for the variable with the specified name.
 * `name' must not contain '='. */
void update_environment(const wchar_t *name)
{
    char *mname = malloc_wcstombs(name);
    if (mname == NULL)
        return;

    char *value = get_exported_value(name);
    if (value == NULL) {
        if (xunsetenv(mname) < 0)
            xerror(errno, Ngt("failed to unset environment variable $%s"),
                    mname);
    } else {
        if (setenv(mname, value, true) < 0)
            xerror(errno, Ngt("failed to set environment variable $%s"), mname);
    }

    free(mname);
    free(value);
}

/* Returns the value of variable `name' that should be exported.
 * If the variable is not exported or the variable value cannot be converted to
 * a multibyte string, NULL is returned. */
char *get_exported_value(const wchar_t *name)
{
    for (environ_T *env = current_env; env != NULL; env = env->parent) {
        const variable_T *var = ht_get(&env->contents, name).value;
        if (var != NULL && (var->v_type & VF_EXPORT)) {
            switch (var->v_type & VF_MASK) {
                case VF_SCALAR:
                    if (var->v_value == NULL)
                        continue;
                    return malloc_wcstombs(var->v_value);
                case VF_ARRAY:
                    return realloc_wcstombs(joinwcsarray(var->v_vals, L":"));
                default:
                    assert(false);
            }
        }
    }
    return NULL;
}

/* Resets the locate settings for the specified variable.
 * If `name' is not any of "LANG", "LC_ALL", etc., does nothing. */
void reset_locale(const wchar_t *name)
{
    if (wcscmp(name, L VAR_LANG) == 0) {
        goto reset_locale_all;
    } else if (wcsncmp(name, L"LC_", 3) == 0) {
        /* POSIX forbids resetting LC_CTYPE even if the value of the variable
         * is changed, but we do reset LC_CTYPE if the shell is interactive and
         * not in the POSIXly-correct mode. */
        if (wcscmp(&name[3], &(L VAR_LC_ALL)[3]) == 0) {
reset_locale_all:
            reset_locale_category(L VAR_LC_COLLATE, LC_COLLATE);
            if (!posixly_correct && is_interactive_now)
                reset_locale_category(L VAR_LC_CTYPE, LC_CTYPE);
            reset_locale_category(L VAR_LC_MESSAGES, LC_MESSAGES);
            reset_locale_category(L VAR_LC_MONETARY, LC_MONETARY);
            reset_locale_category(L VAR_LC_NUMERIC, LC_NUMERIC);
            reset_locale_category(L VAR_LC_TIME, LC_TIME);
        } else if (wcscmp(&name[3], &(L VAR_LC_COLLATE)[3]) == 0) {
            reset_locale_category(L VAR_LC_COLLATE, LC_COLLATE);
        } else if (wcscmp(&name[3], &(L VAR_LC_CTYPE)[3]) == 0) {
            if (!posixly_correct && is_interactive_now)
                reset_locale_category(L VAR_LC_CTYPE, LC_CTYPE);
        } else if (wcscmp(&name[3], &(L VAR_LC_MESSAGES)[3]) == 0) {
            reset_locale_category(L VAR_LC_MESSAGES, LC_MESSAGES);
        } else if (wcscmp(&name[3], &(L VAR_LC_MONETARY)[3]) == 0) {
            reset_locale_category(L VAR_LC_MONETARY, LC_MONETARY);
        } else if (wcscmp(&name[3], &(L VAR_LC_NUMERIC)[3]) == 0) {
            reset_locale_category(L VAR_LC_NUMERIC, LC_NUMERIC);
        } else if (wcscmp(&name[3], &(L VAR_LC_TIME)[3]) == 0) {
            reset_locale_category(L VAR_LC_TIME, LC_TIME);
        }
    }
}

/* Resets the locale of the specified category.
 * `name' must be one of the `LC_*' constants except LC_ALL. */
void reset_locale_category(const wchar_t *name, int category)
{
    const wchar_t *locale = getvar(L VAR_LC_ALL);
    if (locale == NULL) {
        locale = getvar(name);
        if (locale == NULL) {
            locale = getvar(L VAR_LANG);
            if (locale == NULL)
                locale = L"";
        }
    }
    char *wlocale = malloc_wcstombs(locale);
    if (wlocale != NULL) {
        setlocale(category, wlocale);
        free(wlocale);
    }
}

/* Creates a new scalar variable that has no value.
 * If the variable already exists, it is returned without change. So the return
 * value may be an array variable or it may be a scalar variable with a value.
 * Temporary variables with the `name' are cleared if any. */
variable_T *new_global(const wchar_t *name)
{
    variable_T *var;
    for (environ_T *env = current_env; env != NULL; env = env->parent) {
        var = ht_get(&env->contents, name).value;
        if (var != NULL) {
            if (env->is_temporary) {
                assert(!(var->v_type & VF_NODELETE));
                varkvfree_reexport(ht_remove(&env->contents, name));
                continue;
            }
            return var;
        }
    }
    var = xmalloc(sizeof *var);
    var->v_type = VF_SCALAR;
    var->v_value = NULL;
    var->v_getter = NULL;
    ht_set(&first_env->contents, xwcsdup(name), var);
    return var;
}

/* Creates a new scalar variable that has no value.
 * If the variable already exists, it is returned without change. So the return
 * value may be an array variable or it may be a scalar variable with a value.
 * Temporary variables with the `name' are cleared if any. */
variable_T *new_local(const wchar_t *name)
{
    environ_T *env = current_env;
    while (env->is_temporary) {
        varkvfree_reexport(ht_remove(&env->contents, name));
        env = env->parent;
    }
    variable_T *var = ht_get(&env->contents, name).value;
    if (var != NULL)
        return var;
    var = xmalloc(sizeof *var);
    var->v_type = VF_SCALAR;
    var->v_value = NULL;
    var->v_getter = NULL;
    ht_set(&env->contents, xwcsdup(name), var);
    return var;
}

/* Creates a new scalar variable that has no value.
 * If the variable already exists, it is returned without change. So the return
 * value may be an array variable or it may be a scalar variable with a value.
 * The current environment must be a temporary environment.
 * If there is a read-only non-temporary variable with the specified name, it is
 * returned (no new temporary variable is created). */
variable_T *new_temporary(const wchar_t *name)
{
    environ_T *env = current_env;
    assert(env->is_temporary);

    /* check if read-only */
    variable_T *var = search_variable(name);
    if (var != NULL && (var->v_type & VF_READONLY))
        return var;

    var = ht_get(&env->contents, name).value;
    if (var != NULL)
        return var;
    var = xmalloc(sizeof *var);
    var->v_type = VF_SCALAR;
    var->v_value = NULL;
    var->v_getter = NULL;
    ht_set(&env->contents, xwcsdup(name), var);
    return var;
}

/* Creates a new variable with the specified name if there is none.
 * If the variable already exists, it is cleared and returned.
 *
 * On error, an error message is printed to the standard error and NULL is
 * returned. Otherwise, the (new) variable is returned.
 * `v_type' is the only valid member of the returned variable and all the
 * members of the variable (including `v_type') must be initialized by the
 * caller. If `v_type' of the return value includes the VF_EXPORT flag, the
 * caller must call `update_environment'. */
variable_T *new_variable(const wchar_t *name, scope_T scope)
{
    variable_T *var;

    switch (scope) {
        case SCOPE_GLOBAL:  var = new_global(name);     break;
        case SCOPE_LOCAL:   var = new_local(name);      break;
        case SCOPE_TEMP:    var = new_temporary(name);  break;
        default:            assert(false);
    }
    if (var->v_type & VF_READONLY) {
        xerror(0, Ngt("$%ls is read-only"), name);
        return NULL;
    } else {
        varvaluefree(var);
        return var;
    }
}

/* Creates a scalar variable with the specified name and value.
 * `value' must be a `free'able string or NULL. The caller must not modify or
 * free `value' hereafter, whether or not this function is successful.
 * If `export' is true, the variable is exported (i.e., the VF_EXPORT flag is
 * set to the variable), but this function does not reset an existing VF_EXPORT
 * flag if `export' is false. The `shopt_allexport' option, if true, supersedes
 * `export' unless `name' begins with an '='.
 * Returns true iff successful. On error, an error message is printed to the
 * standard error. */
bool set_variable(
        const wchar_t *name, wchar_t *value, scope_T scope, bool export)
{
    if (shopt_allexport && name[0] != '=')
        export = true;

    variable_T *var = new_variable(name, scope);
    if (var == NULL) {
        free(value);
        return false;
    }

    var->v_type = VF_SCALAR
        | (var->v_type & (VF_EXPORT | VF_NODELETE))
        | (export ? VF_EXPORT : 0);
    var->v_value = value;
    var->v_getter = NULL;

    variable_set(name, var);
    if (var->v_type & VF_EXPORT)
        update_environment(name);
    return true;
}

/* Creates an array variable with the specified name and values.
 * `values' is a NULL-terminated array of pointers to wide strings. It is used
 * as the contents of the array variable hereafter, so you must not modify or
 * free the array or its elements whether or not this function succeeds.
 * `values' and its elements must be `free'able.
 * `count' is the number of elements in `values'. If `count' is zero, the
 * number is counted in this function.
 * If `export' is true, the variable is exported (i.e., the VF_EXPORT flag is
 * set to the variable), but this function does not reset an existing VF_EXPORT
 * flag if `export' is false. The `shopt_allexport' option, if true, supersedes
 * `export' unless `name' begins with an '='.
 * Returns the set array iff successful. On error, an error message is printed
 * to the standard error and NULL is returned. */
variable_T *set_array(const wchar_t *name, size_t count, void **values,
        scope_T scope, bool export)
{
    if (shopt_allexport && name[0] != '=')
        export = true;

    variable_T *var = new_variable(name, scope);
    if (var == NULL) {
        plfree(values, free);
        return NULL;
    }

    var->v_type = VF_ARRAY
        | (var->v_type & (VF_EXPORT | VF_NODELETE))
        | (export ? VF_EXPORT : 0);
    var->v_vals = values;
    var->v_valc = (count != 0) ? count : plcount(var->v_vals);
    var->v_getter = NULL;

    variable_set(name, var);
    if (var->v_type & VF_EXPORT)
        update_environment(name);
    return var;
}

/* Changes the value of the specified array element.
 * `name' must be the name of an existing array.
 * `index' is the index of the element (counted from zero).
 * `value' is the new value, which must be a `free'able string. Since `value' is
 * used as the contents of the array element, you must not modify or free
 * `value' after this function returned (whether successful or not).
 * Returns true iff successful. An error message is printed on failure. */
bool set_array_element(const wchar_t *name, size_t index, wchar_t *value)
{
    variable_T *array = search_array_and_check_if_changeable(name);
    if (array == NULL)
        goto fail;
    if (array->v_valc <= index)
        goto invalid_index;

    free(array->v_vals[index]);
    array->v_vals[index] = value;
    if (array->v_type & VF_EXPORT)
        update_environment(name);
    return true;

invalid_index:
    xerror(0,
            Ngt("index %zu is out of range "
                "(the actual size of array $%ls is %zu)"),
            index + 1, name, array->v_valc);
fail:
    free(value);
    return false;
}

/* Sets the positional parameters of the current environment.
 * The existent parameters are cleared.
 * `values' is an NULL-terminated array of pointers to wide strings.
 * `values[0]' will be the new $1, `values[1]' $2, and so on.
 * When a new non-temporary environment is created, this function must be called
 * at least once before the environment is used by the user. */
void set_positional_parameters(void *const *values)
{
    set_array(L VAR_positional, 0, pldup(values, copyaswcs),
            SCOPE_LOCAL, false);
}

/* Performs the specified assignments.
 * If `shopt_xtrace' is true, traces are printed to the standard error.
 * If `temp' is true, the variables are assigned in the current environment,
 * which must be a temporary environment. Otherwise, they are assigned globally.
 * If `export' is true, the variables are exported (i.e., the VF_EXPORT flag is
 * set to the variables), but this function does not reset any existing
 * VF_EXPORT flag if `export' is false. The `shopt_allexport' option, if true,
 * supersedes `export'.
 * Returns true iff successful. On error, already-assigned variables are not
 * restored to the previous values. */
bool do_assignments(const assign_T *assign, bool temp, bool export)
{
    if (temp)
        assert(current_env->is_temporary);

    scope_T scope = temp ? SCOPE_TEMP : SCOPE_GLOBAL;
    for (; assign != NULL; assign = assign->next) {
        switch (assign->a_type) {
            case A_SCALAR:;
                wchar_t *value =
                    expand_single(assign->a_scalar, TT_MULTI, Q_WORD, ES_NONE);
                if (value == NULL)
                    return false;
                if (shopt_xtrace)
                    xtrace_variable(assign->a_name, value);
                if (!set_variable(assign->a_name, value, scope, export))
                    return false;
                break;
            case A_ARRAY:;
                plist_T valuelist = expand_line(assign->a_array, false);
                if (valuelist.contents == NULL)
                    return false;
                if (shopt_xtrace)
                    xtrace_array(assign->a_name, valuelist.contents);
                size_t count = valuelist.length;
                void **values = pl_toary(&valuelist);
                if (!set_array(assign->a_name, count, values, scope, export))
                    return false;
                break;
        }
    }
    return true;
}

/* Pushes a trace of the specified variable assignment to the xtrace buffer. */
void xtrace_variable(const wchar_t *name, const wchar_t *value)
{
    xwcsbuf_T *buf = get_xtrace_buffer();
    wb_wccat(buf, L' ');
    wb_cat(buf, name);
    wb_wccat(buf, L'=');
    wb_quote_as_word(buf, value);
}

/* Pushes a trace of the specified array assignment to the xtrace buffer. */
void xtrace_array(const wchar_t *name, void *const *values)
{
    xwcsbuf_T *buf = get_xtrace_buffer();

    wb_wprintf(buf, L" %ls=(", name);
    if (*values != NULL) {
        for (;;) {
            wb_quote_as_word(buf, *values);
            values++;
            if (*values == NULL)
                break;
            wb_wccat(buf, L' ');
        }
    }
    wb_wccat(buf, L')');
}

/* Gets the value of the specified scalar variable.
 * Cannot be used for special parameters such as $$ and $@.
 * Returns the value of the variable, or NULL if not found.
 * The return value must not be modified or `free'ed by the caller and
 * is valid until the variable is re-assigned or unset. */
const wchar_t *getvar(const wchar_t *name)
{
    variable_T *var = search_variable(name);
    if (var != NULL && (var->v_type & VF_MASK) == VF_SCALAR) {
        if (var->v_getter) {
            var->v_getter(var);
            if ((var->v_type & VF_MASK) != VF_SCALAR)
                return NULL;
        }
        return var->v_value;
    }
    return NULL;
}

/* Returns the value(s) of the specified variable/array as an array.
 * The return value's type is `struct get_variable_T'. It has three members:
 * `type', `count' and `values'.
 * `type' is the type of the result:
 *    GV_NOTFOUND:     no such variable/array
 *    GV_SCALAR:       a normal scalar variable
 *    GV_ARRAY:        an array of zero or more values
 *    GV_ARRAY_CONCAT: an array whose values should be concatenated by caller
 * `values' is an array containing the value(s) of the variable/array.
 * A scalar value (GV_SCALAR) is returned as a NULL-terminated array containing
 * exactly one wide string. An array (GV_ARRAY*) is returned as a NULL-
 * terminated array of pointers to wide strings. If no such variable is found
 * (GV_NOTFOUND), `values' is NULL. The caller must free the `values' array and
 * its element strings iff `freevalues' is true. If `freevalues' is false, the
 * caller must not modify the array or its elements.
 * `count' is the number of elements in `values'. */
struct get_variable_T get_variable(const wchar_t *name)
{
    struct get_variable_T result;
    wchar_t *value;
    variable_T *var;

    if (name[0] == L'\0') {
        goto not_found;
    } else if (name[1] == L'\0') {
        /* `name' is one-character long: check if it's a special parameter */
        switch (name[0]) {
            case L'*':
                result.type = GV_ARRAY_CONCAT;
                goto positional_parameters;
            case L'@':
                result.type = GV_ARRAY;
positional_parameters:
                var = search_variable(L VAR_positional);
                assert(var != NULL && (var->v_type & VF_MASK) == VF_ARRAY);
                result.count = var->v_valc;
                result.values = var->v_vals;
                result.freevalues = false;
                return result;
            case L'#':
                var = search_variable(L VAR_positional);
                assert(var != NULL && (var->v_type & VF_MASK) == VF_ARRAY);
                value = malloc_wprintf(L"%zu", var->v_valc);
                goto return_single;
            case L'?':
                value = malloc_wprintf(L"%d", laststatus);
                goto return_single;
            case L'-':
                value = get_hyphen_parameter();
                goto return_single;
            case L'$':
                value = malloc_wprintf(L"%jd", (intmax_t) shell_pid);
                goto return_single;
            case L'!':
                value = malloc_wprintf(L"%jd", (intmax_t) lastasyncpid);
                goto return_single;
            case L'0':
                value = xwcsdup(command_name);
                goto return_single;
        }
    }

    if (iswdigit(name[0])) {
        /* `name' starts with a digit: a positional parameter */
        wchar_t *nameend;
        errno = 0;
        uintmax_t v = wcstoumax(name, &nameend, 10);
        if (errno != 0 || *nameend != L'\0')
            goto not_found;  /* not a number or overflow */
        var = search_variable(L VAR_positional);
        assert(var != NULL && (var->v_type & VF_MASK) == VF_ARRAY);
        if (v == 0 || var->v_valc < v)
            goto not_found;  /* index out of bounds */
        value = xwcsdup(var->v_vals[v - 1]);
        goto return_single;
    }

    /* now it should be a normal variable */
    var = search_variable(name);
    if (var != NULL) {
        if (var->v_getter)
            var->v_getter(var);
        switch (var->v_type & VF_MASK) {
            case VF_SCALAR:
                value = var->v_value ? xwcsdup(var->v_value) : NULL;
                goto return_single;
            case VF_ARRAY:
                result.type = GV_ARRAY;
                result.count = var->v_valc;
                result.values = var->v_vals;
                result.freevalues = false;
                return result;
        }
    }
    goto not_found;

return_single:  /* return a scalar as a one-element array */
    if (value != NULL) {
        result.type = GV_SCALAR;
        result.count = 1;
        result.values = xmallocn(2, sizeof *result.values);
        result.values[0] = value;
        result.values[1] = NULL;
        result.freevalues = true;
        return result;
    }

not_found:
    return (struct get_variable_T) { .type = GV_NOTFOUND };
}

/* If `gv->freevalues' is false, substitutes `gv->values' with a newly-malloced
 * copy of it and turns `gv->freevalues' to true. */
void save_get_variable_values(struct get_variable_T *gv)
{
    if (!gv->freevalues) {
        gv->values = plndup(gv->values, gv->count, copyaswcs);
        gv->freevalues = true;
    }
}

/* Makes a new array that contains all the variables in the current environment.
 * The elements of the array are key-value pairs of names (const wchar_t *) and
 * values (const variable_T *).
 * If `global' is true, variables in all the ancestor environments are also
 * included (except the ones hidden by local variables).
 * The resultant array is assigned to `*resultp' and the number of the key-value
 * pairs is returned. The array contents must not be modified or freed. */
size_t make_array_of_all_variables(bool global, kvpair_T **resultp)
{
    if (current_env->parent == NULL || (!global && current_env->is_temporary)) {
        *resultp = ht_tokvarray(&current_env->contents);
        return current_env->contents.count;
    } else {
        hashtable_T variables;
        size_t count;

        ht_init(&variables, hashwcs, htwcscmp);
        get_all_variables_rec(&variables, current_env, global);
        *resultp = ht_tokvarray(&variables);
        count = variables.count;
        ht_destroy(&variables);
        return count;
    }
}

/* Gathers all variables in the specified environment and adds them to the
 * specified hashtable.
 * If `global' is true, variables in all the ancestor environments are also
 * included (except the ones hidden by local variables).
 * Keys and values added to the hashtable must not be modified or freed by the
 * caller. */
void get_all_variables_rec(hashtable_T *table, environ_T *env, bool global)
{
    if (env->parent != NULL && (global || env->is_temporary))
        get_all_variables_rec(table, env->parent, global);

    size_t i = 0;
    kvpair_T kv;

    while ((kv = ht_next(&env->contents, &i)).key != NULL)
        ht_set(table, kv.key, kv.value);
}

/* Creates a new variable environment.
 * `temp' specifies whether the new environment is for temporary assignments.
 * The current environment will be the parent of the new environment. */
/* Don't forget to call `set_positional_parameters'! */
void open_new_environment(bool temp)
{
    environ_T *newenv = xmalloc(sizeof *newenv);

    newenv->parent = current_env;
    newenv->is_temporary = temp;
    ht_init(&newenv->contents, hashwcs, htwcscmp);
    for (size_t i = 0; i < PA_count; i++)
        newenv->paths[i] = NULL;
    current_env = newenv;
}

/* Destroys the current variable environment.
 * The parent of the current becomes the new current. */
void close_current_environment(void)
{
    environ_T *oldenv = current_env;

    assert(oldenv != first_env);
    current_env = oldenv->parent;
    ht_clear(&oldenv->contents, varkvfree_reexport);
    ht_destroy(&oldenv->contents);
    for (size_t i = 0; i < PA_count; i++)
        plfree((void **) oldenv->paths[i], free);
    free(oldenv);
}


/********** Getters **********/

/* line number of the currently executing command */
static unsigned long current_lineno;

/* Sets `current_lineno' and re-exports $LINENO if it is exported. */
void update_lineno(unsigned long lineno)
{
    current_lineno = lineno;

    variable_T *var = search_variable(L VAR_LINENO);
    if (var != NULL && var->v_getter == lineno_getter &&
            (var->v_type & VF_EXPORT)) {
        // Need to update the environment so the correct value is exported to
        // external commands
        lineno_getter(var);
    }
}

/* getter for $LINENO */
void lineno_getter(variable_T *var)
{
    assert((var->v_type & VF_MASK) == VF_SCALAR);
    free(var->v_value);
    var->v_value = malloc_wprintf(L"%lu", current_lineno);
    // variable_set(VAR_LINENO, var);
    if (var->v_type & VF_EXPORT)
        update_environment(L VAR_LINENO);
}

/* getter for $RANDOM */
void random_getter(variable_T *var)
{
    assert((var->v_type & VF_MASK) == VF_SCALAR);
    free(var->v_value);
    var->v_value = malloc_wprintf(L"%u", next_random());
    // variable_set(VAR_RANDOM, var);
    if (var->v_type & VF_EXPORT)
        update_environment(L VAR_RANDOM);
}

/* Returns a random number between 0 and 32767 using `rand'. */
unsigned next_random(void)
{
#if (RAND_MAX & (RAND_MAX + 1u)) == 0u // RAND_MAX + 1 is a power of 2
    unsigned v = (unsigned) rand();
    return (v ^ (v >> 15)) & ((1u << 15) - 1u);
#else
    unsigned v;
    do
        v = (unsigned) rand();
    while (v > RAND_MAX - (RAND_MAX + 1u) % (1u << 15));
    return v & ((1u << 15) - 1u);
#endif
}


/********** Setter **********/

/* General callback function that is called after an assignment.
 * `var' is NULL when the variable is unset. */
void variable_set(const wchar_t *name, variable_T *var)
{
    switch (name[0]) {
    case L'C':
        if (wcscmp(name, L VAR_CDPATH) == 0)
            reset_path(PA_CDPATH, var);
#if YASH_ENABLE_LINEEDIT
        else if (wcscmp(name, L VAR_COLUMNS) == 0)
            le_need_term_update = true;
#endif
        break;
    case L'L':
        if (wcscmp(name, L VAR_LANG) == 0 || wcsncmp(name, L"LC_", 3) == 0)
            reset_locale(name);
#if YASH_ENABLE_LINEEDIT
        else if (wcscmp(name, L VAR_LINES) == 0)
            le_need_term_update = true;
#endif
        break;
    case L'P':
        if (wcscmp(name, L VAR_PATH) == 0) {
            clear_cmdhash();
            reset_path(PA_PATH, var);
        }
        break;
    case L'R':
        if (random_active && wcscmp(name, L VAR_RANDOM) == 0) {
            random_active = false;
            if (var != NULL
                    && (var->v_type & VF_MASK) == VF_SCALAR
                    && var->v_value != NULL) {
                unsigned long seed;
                if (xwcstoul(var->v_value, 0, &seed)) {
                    srand((unsigned) seed);
                    var->v_getter = random_getter;
                    random_active = true;
                }
            }
        }
        break;
#if YASH_ENABLE_LINEEDIT
    case L'T':
        if (wcscmp(name, L VAR_TERM) == 0)
            le_need_term_update = true;
        break;
#endif /* YASH_ENABLE_LINEEDIT */
    case L'Y':
        if (wcscmp(name, L VAR_YASH_LOADPATH) == 0)
            reset_path(PA_LOADPATH, var);
        break;
    }
}


/********** Path Array Manipulation **********/

/* Splits the specified string at colons.
 * If `paths' is non-NULL, a newly-malloced NULL-terminated array of pointers to
 * newly-malloced multibyte strings is returned.
 * If `paths' is NULL, NULL is returned. */
char **decompose_paths(const wchar_t *paths)
{
    if (paths == NULL)
        return NULL;

    plist_T list;
    pl_init(&list);

    const wchar_t *colon;
    while ((colon = wcschr(paths, L':')) != NULL) {
        add_to_list_no_dup(&list, malloc_wcsntombs(paths, colon - paths));
        paths = &colon[1];
    }
    add_to_list_no_dup(&list, malloc_wcstombs(paths));

    return (char **) pl_toary(&list);
}

/* Converts an array of wide strings into an newly-malloced array of multibyte
 * strings.
 * If `paths' is NULL, NULL is returned. */
char **convert_path_array(void **ary)
{
    if (ary == NULL)
        return NULL;

    plist_T list;
    pl_init(&list);

    while (*ary != NULL) {
        add_to_list_no_dup(&list, malloc_wcstombs(*ary));
        ary++;
    }

    return (char **) pl_toary(&list);
}

/* If `s' is non-NULL and not contained in `list', adds `s' to list.
 * Otherwise, frees `s'. */
void add_to_list_no_dup(plist_T *list, char *s)
{
    if (s != NULL) {
        for (size_t i = 0; i < list->length; i++) {
            if (strcmp(s, list->contents[i]) == 0) {
                free(s);
                return;
            }
        }
        pl_add(list, s);
    }
}

/* Reconstructs the path array of the specified variable in the environment.
 * `var' may be NULL. */
void reset_path(path_T name, variable_T *var)
{
    for (environ_T *env = current_env; env != NULL; env = env->parent) {
        plfree((void **) env->paths[name], free);

        variable_T *v = ht_get(&env->contents, path_variables[name]).value;
        if (v != NULL) {
            switch (v->v_type & VF_MASK) {
                case VF_SCALAR:
                    env->paths[name] = decompose_paths(v->v_value);
                    break;
                case VF_ARRAY:
                    env->paths[name] = convert_path_array(v->v_vals);
                    break;
            }
            if (v == var)
                break;
        } else {
            env->paths[name] = NULL;
        }
    }
}

/* Returns the path array of the specified variable.
 * The return value is NULL if the variable is not set.
 * The caller must not make any change to the returned array. */
char *const *get_path_array(path_T name)
{
    for (environ_T *env = current_env; env != NULL; env = env->parent)
        if (env->paths[name] != NULL)
            return env->paths[name];
    return NULL;
}


/********** Shell Functions **********/

/* type of functions */
struct function_T {
    vartype_T  f_type;  /* only VF_READONLY and VF_NODELETE are valid */
    command_T *f_body;  /* body of function */
};

/* Frees the specified function. */
void funcfree(function_T *f)
{
    if (f != NULL) {
        comsfree(f->f_body);
        free(f);
    }
}

/* Frees the specified key-value pair of a function name and a function. */
void funckvfree(kvpair_T kv)
{
    free(kv.key);
    funcfree(kv.value);
}

/* Defines function `name' as command `body'.
 * It is an error to re-define a read-only function.
 * Returns true iff successful. */
bool define_function(const wchar_t *name, command_T *body)
{
    function_T *f = ht_get(&functions, name).value;
    if (f != NULL && (f->f_type & VF_READONLY)) {
        xerror(0, Ngt("function `%ls' cannot be redefined "
                    "because it is read-only"), name);
        return false;
    }

    f = xmalloc(sizeof *f);
    f->f_type = 0;
    f->f_body = comsdup(body);
    if (shopt_hashondef)
        hash_all_commands_recursively(body);
    funckvfree(ht_set(&functions, xwcsdup(name), f));
    return true;
}

/* Gets the body of the function with the specified name.
 * Returns NULL if there is no such a function. */
command_T *get_function(const wchar_t *name)
{
    function_T *f = ht_get(&functions, name).value;
    if (f != NULL)
        return f->f_body;
    else
        return NULL;
}


/* Registers all the commands in the argument to the command hashtable. */
void hash_all_commands_recursively(const command_T *c)
{
    for (; c != NULL; c = c->next) {
        switch (c->c_type) {
            case CT_SIMPLE:
                if (c->c_words)
                    tryhash_word_as_command(c->c_words[0]);
                break;
            case CT_GROUP:
            case CT_SUBSHELL:
                hash_all_commands_in_and_or(c->c_subcmds);
                break;
            case CT_IF:
                hash_all_commands_in_if(c->c_ifcmds);
                break;
            case CT_FOR:
                hash_all_commands_in_and_or(c->c_forcmds);
                break;
            case CT_WHILE:
                hash_all_commands_in_and_or(c->c_whlcond);
                hash_all_commands_in_and_or(c->c_whlcmds);
                break;
            case CT_CASE:
                hash_all_commands_in_case(c->c_casitems);
                break;
#if YASH_ENABLE_DOUBLE_BRACKET
            case CT_BRACKET:
#endif
            case CT_FUNCDEF:
                break;
        }
    }
}

void hash_all_commands_in_and_or(const and_or_T *ao)
{
    for (; ao != NULL; ao = ao->next) {
        for (pipeline_T *p = ao->ao_pipelines; p != NULL; p = p->next) {
            hash_all_commands_recursively(p->pl_commands);
        }
    }
}

void hash_all_commands_in_if(const ifcommand_T *ic)
{
    for (; ic != NULL; ic = ic->next) {
        hash_all_commands_in_and_or(ic->ic_condition);
        hash_all_commands_in_and_or(ic->ic_commands);
    }
}

void hash_all_commands_in_case(const caseitem_T *ci)
{
    for (; ci != NULL; ci = ci->next) {
        hash_all_commands_in_and_or(ci->ci_commands);
    }
}

void tryhash_word_as_command(const wordunit_T *w)
{
    if (w != NULL && w->next == NULL && w->wu_type == WT_STRING) {
        wchar_t *cmdname = unquote(w->wu_string);
        if (cmdname != NULL && wcschr(cmdname, L'/') == NULL) {
            char *mbsname = malloc_wcstombs(cmdname);
            get_command_path(mbsname, false);
            free(mbsname);
        }
        free(cmdname);
    }
}

#if YASH_ENABLE_LINEEDIT

/* Generates completion candidates for variable names matching the pattern. */
/* The prototype of this function is declared in "lineedit/complete.h". */
void generate_variable_candidates(const le_compopt_T *compopt)
{
    if (!(compopt->type & CGT_VARIABLE))
        return;

    le_compdebug("adding variable name candidates");
    if (!le_compile_cpatterns(compopt))
        return;

    size_t i = 0;
    kvpair_T kv;
    while ((kv = ht_next(&first_env->contents, &i)).key != NULL) {
        const wchar_t *name = kv.key;
        const variable_T *var = kv.value;
        switch (var->v_type & VF_MASK) {
            case VF_SCALAR:
                if (!(compopt->type & CGT_SCALAR))
                    continue;
                break;
            case VF_ARRAY:
                if (!(compopt->type & CGT_ARRAY))
                    continue;
                break;
        }
        if (name[0] != L'=' && le_wmatch_comppatterns(compopt, name))
            le_new_candidate(CT_VAR, xwcsdup(name), NULL, compopt);
    }
}

/* Generates completion candidates for function names matching the pattern. */
/* The prototype of this function is declared in "lineedit/complete.h". */
void generate_function_candidates(const le_compopt_T *compopt)
{
    if (!(compopt->type & CGT_FUNCTION))
        return;

    le_compdebug("adding function name candidates");
    if (!le_compile_cpatterns(compopt))
        return;

    size_t i = 0;
    const wchar_t *name;
    while ((name = ht_next(&functions, &i).key) != NULL)
        if (le_wmatch_comppatterns(compopt, name))
            le_new_candidate(CT_COMMAND, xwcsdup(name), NULL, compopt);
}

#endif /* YASH_ENABLE_LINEEDIT */


/********** Directory Stack **********/

#if YASH_ENABLE_DIRSTACK

/* Parses the specified string as the index of a directory stack entry.
 * The index string must start with the plus or minus sign.
 * If the corresponding entry is found, the entry is assigned to `*entryp' and
 * the index value is assigned to `*indexp'. If the index is out of range, it is
 * an error. If the string is not a valid integer, `indexstr' is assigned to
 * `*entryp' and SIZE_MAX is assigned to `*indexp'.
 * If the index denotes $PWD, the number of the entries in $DIRSTACK is assigned
 * to `*indexp'.
 * Returns true if successful (`*entryp' and `*indexp' are assigned).
 * Returns false on error, in which case an error message is printed iff
 * `printerror' is true. */
bool parse_dirstack_index(
        const wchar_t *restrict indexstr, size_t *restrict indexp,
        const wchar_t **restrict entryp, bool printerror)
{
    long num;

    if (indexstr[0] != L'-' && indexstr[0] != L'+')
        goto not_index;
    if (!xwcstol(indexstr, 10, &num))
        goto not_index;

    variable_T *var = search_variable(L VAR_DIRSTACK);
    if (var == NULL || ((var->v_type & VF_MASK) != VF_ARRAY)) {
        if (num == 0)
            goto return_pwd;
        if (printerror)
            xerror(0, Ngt("the directory stack is empty"));
        return false;
    }
#if LONG_MAX > SIZE_MAX
    if (num > SIZE_MAX || num < -(long) SIZE_MAX)
        goto out_of_range;
#endif
    if (indexstr[0] == L'+' && num >= 0) {
        if (num == 0) {
            num = var->v_valc;
            goto return_pwd;
        }
        if ((size_t) num > var->v_valc)
            goto out_of_range;
        *indexp = var->v_valc - (size_t) num;
        *entryp = var->v_vals[*indexp];
        return true;
    } else if (indexstr[0] == L'-' && num <= 0) {
        if (num == LONG_MIN)
            goto out_of_range;
        num = -num;
        assert(num >= 0);
        if ((size_t) num > var->v_valc)
            goto out_of_range;
        if ((size_t) num == var->v_valc)
            goto return_pwd;
        *indexp = (size_t) num;
        *entryp = var->v_vals[*indexp];
        return true;
    } else {
        goto not_index;
    }

    const wchar_t *pwd;
return_pwd:
    pwd = getvar(L VAR_PWD);
    if (pwd == NULL) {
        if (printerror)
            xerror(0, Ngt("$PWD is not set"));
        return false;
    }
    *entryp = pwd;
    *indexp = (size_t) num;
    return true;
not_index:
    *entryp = indexstr;
    *indexp = SIZE_MAX;
    return true;
out_of_range:
    if (printerror)
        xerror(0, Ngt("index %ls is out of range"), indexstr);
    return false;
}

#endif /* YASH_ENABLE_DIRSTACK */

#if YASH_ENABLE_LINEEDIT

/* Generates candidates to complete a directory stack index. */
/* The prototype of this function is declared in "lineedit/complete.h". */
void generate_dirstack_candidates(const le_compopt_T *compopt)
{
    if (!(compopt->type & CGT_DIRSTACK))
        return;

    le_compdebug("adding directory stack index candidates");

#if YASH_ENABLE_DIRSTACK
    if (!le_compile_cpatterns(compopt))
        return;

    variable_T *dirstack = search_variable(L VAR_DIRSTACK);
    size_t totalcount = 1;
    wchar_t *index;
    if (dirstack != NULL && (dirstack->v_type & VF_MASK) == VF_ARRAY) {
        size_t count = dirstack->v_valc;
        for (size_t i = 0; i < count; i++) {
            switch (compopt->src[0]) {
                case L'+':
                    index = malloc_wprintf(L"+%zu", count - i);
                    break;
                case L'-':
                    index = malloc_wprintf(L"-%zu", i);
                    break;
                default:
                    return;
            }
            if (le_wmatch_comppatterns(compopt, index))
                le_new_candidate(
                        CT_WORD, index, xwcsdup(dirstack->v_vals[i]), compopt);
            else
                free(index);
        }
        totalcount += count;
    }

    switch (compopt->src[0]) {
        case L'+':
            index = malloc_wprintf(L"+%zu", (size_t) 0);
            break;
        case L'-':
            index = malloc_wprintf(L"-%zu", totalcount - 1);
            break;
        default:
            return;
    }
    if (le_wmatch_comppatterns(compopt, index)) {
        const wchar_t *pwd = getvar(L VAR_PWD);
        wchar_t *duppwd = (pwd != NULL) ? xwcsdup(pwd) : NULL;
        le_new_candidate(CT_WORD, index, duppwd, compopt);
    } else {
        free(index);
    }
#else /* YASH_ENABLE_DIRSTACK */
    le_compdebug("  directory stack is disabled");
#endif
}

#endif /* YASH_ENABLE_LINEEDIT */


/********** Built-ins **********/

struct reading_option_T;

static void print_variable(
        const wchar_t *name, const variable_T *var,
        const wchar_t *argv0, bool readonly, bool export)
    __attribute__((nonnull));
static void print_scalar(const wchar_t *name, bool namequote,
        const variable_T *var, const wchar_t *argv0)
    __attribute__((nonnull));
static void print_array(
        const wchar_t *name, const variable_T *var, const wchar_t *argv0)
    __attribute__((nonnull));
static void print_function(
        const wchar_t *name, const function_T *func,
        const wchar_t *argv0, bool readonly)
    __attribute__((nonnull));
static char *vartype_option_string(vartype_T type)
    __attribute__((malloc,warn_unused_result));
static bool can_make_readonly(const wchar_t *name)
    __attribute__((nonnull,pure));
#if YASH_ENABLE_ARRAY
static int array_dump_all(const wchar_t *argv0);
static void array_remove_elements(
        const wchar_t *name, variable_T *array, size_t count,
        void *const *indexwcss)
    __attribute__((nonnull));
static int compare_long(const void *lp1, const void *lp2)
    __attribute__((nonnull,pure));
static void array_insert_elements(
        const wchar_t *name, variable_T *array, size_t count,
        void *const *values)
    __attribute__((nonnull));
static void array_set_element(const wchar_t *name, variable_T *array,
        const wchar_t *indexword, const wchar_t *value)
    __attribute__((nonnull));
#endif /* YASH_ENABLE_ARRAY */
static bool unset_function(const wchar_t *name)
    __attribute__((nonnull));
static bool unset_variable(const wchar_t *name)
    __attribute__((nonnull));
static bool check_options(const wchar_t *options)
    __attribute__((nonnull,pure));
static bool set_optind(unsigned long optind, unsigned long optsubind);
static inline bool set_optarg(const wchar_t *value);
static bool set_variable_single_char(const wchar_t *varname, wchar_t value)
    __attribute__((nonnull));
static bool read_with_prompt(
        xwcsbuf_T *buf, xstrbuf_T *cc, const struct reading_option_T *ro)
    __attribute__((nonnull));
static struct promptset_T promptset_for_read(
        bool firstline, const struct reading_option_T *ro)
    __attribute__((nonnull,warn_unused_result));
static xwcsbuf_T read_one_line_with_prompt(
        struct promptset_T prompt, wchar_t delimiter, bool lineedit)
    __attribute__((warn_unused_result));
static xwcsbuf_T read_one_line(wchar_t delimiter)
    __attribute__((warn_unused_result));
static bool unescape_line(
        const xwcsbuf_T *line, xwcsbuf_T *buf, xstrbuf_T *cc, wchar_t delimiter)
    __attribute__((nonnull));
static void assign_array(const wchar_t *name, const plist_T *ranges, size_t i)
    __attribute__((nonnull));

/* Options for the "typeset" built-in. */
const struct xgetopt_T typeset_options[] = {
    { L'f', L"functions", OPTARG_NONE, false, NULL, },
    { L'g', L"global",    OPTARG_NONE, false, NULL, },
    { L'p', L"print",     OPTARG_NONE, true,  NULL, },
    { L'r', L"readonly",  OPTARG_NONE, false, NULL, },
    { L'x', L"export",    OPTARG_NONE, false, NULL, },
    { L'X', L"unexport",  OPTARG_NONE, false, NULL, },
#if YASH_ENABLE_HELP
    { L'-', L"help",      OPTARG_NONE, false, NULL, },
#endif
    { L'\0', NULL, 0, false, NULL, },
};
/* Note: `local_options' is defined as part of `typeset_options'. */

/* The "typeset" built-in, which accepts the following options:
 *  -f: affect functions rather than variables
 *  -g: global
 *  -p: print variables
 *  -r: make variables readonly
 *  -x: export variables
 *  -X: cancel exportation of variables
 * Equivalent built-ins:
 *  export:   typeset -gx
 *  local:    typeset
 *  readonly: typeset -gr
 * The "set" built-in without any arguments is redirected to this built-in. */
int typeset_builtin(int argc, void **argv)
{
    bool function = false, global = false, print = false;
    bool readonly = false, export = false, unexport = false;

    const struct xgetopt_T *options =
        (ARGV(0)[0] == L'l' /*local*/) ? local_options : typeset_options;
    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, options, 0)) != NULL) {
        switch (opt->shortopt) {
            case L'f':  function = true;  break;
            case L'g':  global   = true;  break;
            case L'p':  print    = true;  break;
            case L'r':  readonly = true;  break;
            case L'x':  export   = true;  break;
            case L'X':  unexport = true;  break;
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return special_builtin_error(Exit_ERROR);
        }
    }

    switch (ARGV(0)[0]) {
        case L'e':
            assert(wcscmp(ARGV(0), L"export") == 0);
            global = true;
            if (!unexport)
                export = true;
            break;
        case L'l':
            assert(wcscmp(ARGV(0), L"local") == 0);
            break;
        case L'r':
            assert(wcscmp(ARGV(0), L"readonly") == 0);
            global = readonly = true;
            break;
        case L's':
            assert(wcscmp(ARGV(0), L"set") == 0);
            global = true;
            break;
        case L't':
            assert(wcscmp(ARGV(0), L"typeset") == 0);
            break;
        default:
            assert(false);
    }

    if (function && global && ARGV(0)[0] == L't' /*typeset*/)
        return special_builtin_error(
                mutually_exclusive_option_error(L'f', L'g'));
    if (function && export)
        return special_builtin_error(
                mutually_exclusive_option_error(L'f', L'x'));
    if (function && unexport)
        return special_builtin_error(
                mutually_exclusive_option_error(L'f', L'X'));
    if (export && unexport)
        return special_builtin_error(
                mutually_exclusive_option_error(L'x', L'X'));

    if (xoptind == argc) {
        kvpair_T *kvs;
        size_t count;

        if (!function) {
            /* print all variables */
            count = make_array_of_all_variables(global, &kvs);
            qsort(kvs, count, sizeof *kvs, keywcscoll);
            for (size_t i = 0; yash_error_message_count == 0 && i < count; i++)
                print_variable(
                        kvs[i].key, kvs[i].value, ARGV(0), readonly, export);
        } else {
            /* print all functions */
            kvs = ht_tokvarray(&functions);
            count = functions.count;
            qsort(kvs, count, sizeof *kvs, keywcscoll);
            for (size_t i = 0; yash_error_message_count == 0 && i < count; i++)
                print_function(kvs[i].key, kvs[i].value, ARGV(0), readonly);
        }
        free(kvs);
    } else {
        do {
            wchar_t *arg = ARGV(xoptind);
            if (!function) {
                wchar_t *wequal = wcschr(arg, L'=');
                if (wequal != NULL)
                    *wequal = L'\0';
                if (wequal != NULL || !print) {
                    /* create/assign variable */
                    variable_T *var = global ? new_global(arg) : new_local(arg);
                    vartype_T saveexport = var->v_type & VF_EXPORT;
                    if (wequal != NULL) {
                        if (var->v_type & VF_READONLY) {
                            xerror(0, Ngt("$%ls is read-only"), arg);
                        } else {
                            varvaluefree(var);
                            var->v_type = VF_SCALAR | (var->v_type & ~VF_MASK);
                            var->v_value = xwcsdup(&wequal[1]);
                            var->v_getter = NULL;
                        }
                    }
                    if (readonly) {
                        if (can_make_readonly(arg))
                            var->v_type |= VF_READONLY | VF_NODELETE;
                        else
                            xerror(0, Ngt("$%ls cannot be made read-only "
                                        "in the POSIXly-correct mode"), arg);
                    }
                    if (export)
                        var->v_type |= VF_EXPORT;
                    else if (unexport)
                        var->v_type &= ~VF_EXPORT;
                    variable_set(arg, var);
                    if (saveexport != (var->v_type & VF_EXPORT)
                            || (wequal != NULL && (var->v_type & VF_EXPORT)))
                        update_environment(arg);
                } else {
                    /* print the variable */
                    variable_T *var = search_variable(arg);
                    if (var != NULL) {
                        print_variable(arg, var, ARGV(0), readonly, export);
                    } else {
                        xerror(0, Ngt("no such variable $%ls"), arg);
                    }
                }
            } else {
                /* treat function */
                function_T *f = ht_get(&functions, arg).value;
                if (f != NULL) {
                    if (print) {
                        if (!readonly || (f->f_type & VF_READONLY))
                            print_function(arg, f, ARGV(0), readonly);
                    } else {
                        if (readonly)
                            f->f_type |= VF_READONLY | VF_NODELETE;
                    }
                } else {
                    xerror(0, Ngt("no such function `%ls'"), arg);
                }
            }
        } while (++xoptind < argc);
    }

    return (yash_error_message_count == 0) ?
            Exit_SUCCESS : special_builtin_error(Exit_FAILURE);
}

/* Prints the specified variable to the standard output.
 * This function does not print special variables whose name begins with an '='.
 * If `readonly' or `export' is true, the variable is printed only if it is
 * read-only or exported, respectively. The `name' is quoted if `is_name(name)'
 * is not true.
 * An error message is printed to the standard error on error. */
void print_variable(
        const wchar_t *name, const variable_T *var,
        const wchar_t *argv0, bool readonly, bool export)
{
    wchar_t *qname = NULL;

    if (name[0] == L'=')
        return;
    if (readonly && !(var->v_type & VF_READONLY))
        return;
    if (export && !(var->v_type & VF_EXPORT))
        return;

    if (!is_name(name))
        name = qname = quote_as_word(name);

    switch (var->v_type & VF_MASK) {
        case VF_SCALAR:
            print_scalar(name, qname != NULL, var, argv0);
            break;
        case VF_ARRAY:
            print_array(name, var, argv0);
            break;
    }

    free(qname);
}

/* Prints the specified scalar variable to the standard output.
 * `namequote' must equal `is_name(name)'. If `argv0' is "set" and `namequote'
 * is true, the variable is not printed since it cannot be assigned in the
 * normal assignment syntax.
 * An error message is printed to the standard error on error. */
void print_scalar(const wchar_t *name, bool namequote,
        const variable_T *var, const wchar_t *argv0)
{
    wchar_t *quotedvalue =
        (var->v_value != NULL) ? quote_as_word(var->v_value) : NULL;
    const char *separator = (name[0] == L'-') ? "-- " : "";
    const char *format;
    char *opts;

    switch (argv0[0]) {
        case L's':
            assert(wcscmp(argv0, L"set") == 0);
            if (!namequote && quotedvalue != NULL)
                xprintf("%ls=%ls\n", name, quotedvalue);
            break;
        case L'e':
        case L'r':
            assert(wcscmp(argv0, L"export") == 0
                    || wcscmp(argv0, L"readonly") == 0);
            format = (quotedvalue != NULL) ? "%ls %s%ls=%ls\n" : "%ls %s%ls\n";
            xprintf(format, argv0, separator, name, quotedvalue);
            break;
        case L'l':
            assert(wcscmp(argv0, L"local") == 0);
            goto typeset;
        case L't':
            assert(wcscmp(argv0, L"typeset") == 0);
typeset:
            format = (quotedvalue != NULL) ? "%ls%s %s%ls=%ls\n" : "%ls%s %s%ls\n";
            opts = vartype_option_string(var->v_type);
            xprintf(format, argv0, opts, separator, name, quotedvalue);
            free(opts);
            break;
        default:
            assert(false);
    }
    free(quotedvalue);
}

/* Prints the specified array variable to the standard output.
 * An error message is printed to the standard error on error. */
void print_array(
        const wchar_t *name, const variable_T *var, const wchar_t *argv0)
{
    if (!xprintf("%ls=(", name))
        return;
    if (var->v_valc > 0) {
        for (size_t i = 0; ; ) {
            wchar_t *qvalue = quote_as_word(var->v_vals[i]);
            bool ok = xprintf("%ls", qvalue);
            free(qvalue);
            if (!ok)
                return;
            i++;
            if (i >= var->v_valc)
                break;
            if (!xprintf(" "))
                return;
        }
    }
    if (!xprintf(")\n"))
        return;

    const char *separator = (name[0] == L'-') ? "-- " : "";

    switch (argv0[0]) {
        case L'a':
            assert(wcscmp(argv0, L"array") == 0);
            break;
        case L's':
            assert(wcscmp(argv0, L"set") == 0);
            break;
        case L'e':
        case L'r':
            assert(wcscmp(argv0, L"export") == 0
                    || wcscmp(argv0, L"readonly") == 0);
            xprintf("%ls %s%ls\n", argv0, separator, name);
            break;
        case L'l':
            assert(wcscmp(argv0, L"local") == 0);
            goto typeset;
        case L't':
            assert(wcscmp(argv0, L"typeset") == 0);
typeset:;
            char *opts = vartype_option_string(var->v_type);
            xprintf("%ls%s %s%ls\n", argv0, opts, separator, name);
            free(opts);
            break;
        default:
            assert(false);
    }
}

/* Prints the specified function to the standard output.
 * If `readonly' is true, the function is printed only if it is read-only.
 * An error message is printed to the standard error if failed to print to the
 * standard output. */
void print_function(
        const wchar_t *name, const function_T *func,
        const wchar_t *argv0, bool readonly)
{
    if (readonly && !(func->f_type & VF_READONLY))
        return;

    wchar_t *qname = NULL;
    if (!is_name(name))
        name = qname = quote_as_word(name);

    wchar_t *value = command_to_wcs(func->f_body, true);
    const char *format = (qname == NULL) ? "%ls()\n%ls" : "function %ls()\n%ls";
    bool ok = xprintf(format, name, value);
    free(value);
    if (!ok)
        goto end;

    const char *separator = (name[0] == L'-') ? "-- " : "";
    switch (argv0[0]) {
        case L'r':
            assert(wcscmp(argv0, L"readonly") == 0);
            if (func->f_type & VF_READONLY)
                xprintf("%ls -f %s%ls\n", argv0, separator, name);
            break;
        case L't':
            assert(wcscmp(argv0, L"typeset") == 0);
            if (func->f_type & VF_READONLY)
                xprintf("%ls -fr %s%ls\n", argv0, separator, name);
            break;
        default:
            assert(false);
    }

end:
    free(qname);
}

/* Returns a newly malloced string that specifies options for the typeset
 * built-in that can be used to restore the given variable type. */
char *vartype_option_string(vartype_T type)
{
    xstrbuf_T opts;
    sb_initwithmax(&opts, 4);
    if (type & VF_EXPORT)
        sb_ccat(&opts, 'x');
    if (type & VF_READONLY)
        sb_ccat(&opts, 'r');
    if (opts.length > 0)
        sb_insert(&opts, 0, " -");
    return sb_tostr(&opts);
}

/* Determines whether the specified variable can be made read-only.
 * Returns true if it can be made read-only. */
bool can_make_readonly(const wchar_t *name)
{
    if (!posixly_correct)
        return true;
    return wcscmp(name, L VAR_LINENO) != 0 &&
        wcscmp(name, L VAR_OPTARG) != 0 &&
        wcscmp(name, L VAR_OPTIND) != 0 &&
        wcscmp(name, L VAR_PWD) != 0 &&
        wcscmp(name, L VAR_OLDPWD) != 0;
}

#if YASH_ENABLE_HELP
const char typeset_help[] = Ngt(
"set or print variables"
);
const char typeset_syntax[] = Ngt(
"\ttypeset [-fgprxX] [name[=value]...]\n"
);
const char export_help[] = Ngt(
"export variables as environment variables"
);
const char export_syntax[] = Ngt(
"\texport [-prX] [name[=value]...]\n"
);
const char local_help[] = Ngt(
"set or print local variables"
);
const char local_syntax[] = Ngt(
"\tlocal [-prxX] [name[=value]...]\n"
);
const char readonly_help[] = Ngt(
"make variables read-only"
);
const char readonly_syntax[] = Ngt(
"\treadonly [-fpxX] [name[=value]...]\n"
);
#endif

#if YASH_ENABLE_ARRAY

/* Options for the "array" built-in. */
const struct xgetopt_T array_options[] = {
    { L'd', L"delete", OPTARG_NONE, true,  NULL, },
    { L'i', L"insert", OPTARG_NONE, true,  NULL, },
    { L's', L"set",    OPTARG_NONE, true,  NULL, },
#if YASH_ENABLE_HELP
    { L'-', L"help",   OPTARG_NONE, false, NULL, },
#endif
    { L'\0', NULL, 0, false, NULL, },
};

/* The "array" built-in, which accepts the following options:
 *  -d: delete an array element
 *  -i: insert an array element
 *  -s: set an array element value */
int array_builtin(int argc, void **argv)
{
    enum {
        NONE   = 0,
        DELETE = 1 << 0,
        INSERT = 1 << 1,
        SET    = 1 << 2,
    } options = NONE;

    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, array_options, XGETOPT_DIGIT)) != NULL) {
        switch (opt->shortopt) {
            case L'd':  options |= DELETE;  break;
            case L'i':  options |= INSERT;  break;
            case L's':  options |= SET;     break;
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return Exit_ERROR;
        }
    }

    /* error checks */
    if (options != NONE && (options & (options - 1)) != 0) {
        xerror(0, Ngt("more than one option cannot be used at once"));
        return Exit_ERROR;
    }
    size_t min, max;
    switch (options) {
        case NONE:    min = 0;  max = SIZE_MAX;  break;
        case DELETE:  min = 1;  max = SIZE_MAX;  break;
        case INSERT:  min = 2;  max = SIZE_MAX;  break;
        case SET:     min = 3;  max = 3;         break;
        default:      assert(false);
    }
    if (!validate_operand_count(argc - xoptind, min, max))
        return Exit_ERROR;

    if (xoptind == argc)
        return array_dump_all(ARGV(0));

    const wchar_t *name = ARGV(xoptind++);
    if (wcschr(name, L'=') != NULL) {
        xerror(0, Ngt("`%ls' is not a valid array name"), name);
        return Exit_FAILURE;
    }

    if (options == NONE) {
        set_array(name, argc - xoptind, pldup(&argv[xoptind], copyaswcs),
                SCOPE_GLOBAL, false);
    } else {
        variable_T *array = search_array_and_check_if_changeable(name);
        if (array == NULL)
            return Exit_FAILURE;
        switch (options) {
            case DELETE:
                array_remove_elements(
                        name, array, argc - xoptind, &argv[xoptind]);
                break;
            case INSERT:
                array_insert_elements(
                        name, array, argc - xoptind, &argv[xoptind]);
                break;
            case SET:
                array_set_element(
                        name, array, ARGV(xoptind), ARGV(xoptind + 1));
                break;
            default:
                assert(false);
        }
    }
    return (yash_error_message_count == 0) ? Exit_SUCCESS : Exit_FAILURE;
}

#if LONG_MAX < SIZE_MAX
# define LONG_LT_SIZE(longvalue,sizevalue) \
    ((size_t) (longvalue) < (sizevalue))
# define LONG_EQ_SIZE(longvalue,sizevalue) \
    ((size_t) (longvalue) == (sizevalue))
#else
# define LONG_LT_SIZE(longvalue,sizevalue) \
    ((longvalue) < (long) (sizevalue))
# define LONG_EQ_SIZE(longvalue,sizevalue) \
    ((longvalue) == (long) (sizevalue))
#endif

/* Prints all existing arrays.
 * Returns an exit status to be returned by the array built-in. */
int array_dump_all(const wchar_t *argv0)
{
    kvpair_T *kvs;
    size_t count = make_array_of_all_variables(true, &kvs);
    qsort(kvs, count, sizeof *kvs, keywcscoll);
    for (size_t i = 0; yash_error_message_count == 0 && i < count; i++) {
        variable_T *var = kvs[i].value;
        if ((var->v_type & VF_MASK) == VF_ARRAY)
            print_variable(kvs[i].key, var, argv0, false, false);
    }
    free(kvs);
    return (yash_error_message_count == 0) ? Exit_SUCCESS : Exit_FAILURE;
}

/* Removes elements from `array'.
 * `indexwcss' is an NULL-terminated array of pointers to wide strings,
 * which are parsed as indices of elements to be removed.
 * `count' is the number of elements in `indexwcss'.
 * An error message is printed to the standard error on error. */
void array_remove_elements(
        const wchar_t *name, variable_T *array, size_t count,
        void *const *indexwcss)
{
    size_t extended_count = count;
    if (extended_count == 0)
        extended_count = 1; // A variable-length array must not be empty.
    long indices[extended_count];

    assert((array->v_type & VF_MASK) == VF_ARRAY);

    /* convert all the strings into long integers */
    for (size_t i = 0; i < count; i++) {
        const wchar_t *indexwcs = indexwcss[i];
        if (!xwcstol(indexwcs, 10, &indices[i])) {
            xerror(errno, Ngt("`%ls' is not a valid integer"), indexwcs);
            return;
        }

        if (indices[i] >= 0) {
            indices[i] -= 1;
        } else {
#if LONG_MAX < SIZE_MAX
            if (!LONG_LT_SIZE(LONG_MAX, array->v_valc))
#endif
                indices[i] += array->v_valc;
        }
    }

    /* sort all the indices */
    qsort(indices, count, sizeof *indices, compare_long);

    /* skip negative indices, which are out of range */
    size_t i = 0;
    while (i < count && indices[i] < 0)
        i++;

    /* remove the elements */
    size_t oldcount = array->v_valc, newcount = 0;
    for (size_t scanindex = 0; scanindex < array->v_valc; scanindex++) {
        while (i < count && LONG_LT_SIZE(indices[i], scanindex))
            i++;
        if (i < count && LONG_EQ_SIZE(indices[i], scanindex)) {
            /* remove the element */
            free(array->v_vals[scanindex]);
        } else {
            /* keep the element */
            array->v_vals[newcount] = array->v_vals[scanindex];
            newcount++;
        }
    }
    array->v_valc = newcount;
    array->v_vals[newcount] = NULL;

    if (newcount < oldcount) {
        variable_set(name, array);
        if (array->v_type & VF_EXPORT)
            update_environment(name);
    }
}

int compare_long(const void *lp1, const void *lp2)
{
    long l1 = *(const long *) lp1, l2 = *(const long *) lp2;
    return l1 == l2 ? 0 : l1 < l2 ? -1 : 1;
}

/* Inserts the specified elements into the specified array.
 * `values' is an NULL-terminated array of pointers to wide strings.
 * The first string in `values' is parsed as the integer index that specifies
 * where to insert the elements. The other strings are inserted to the array.
 * `count' is the number of strings in `values' including the first index
 * string.
 * An error message is printed to the standard error on error. */
void array_insert_elements(
        const wchar_t *name, variable_T *array, size_t count,
        void *const *values)
{
    long index;

    assert((array->v_type & VF_MASK) == VF_ARRAY);
    assert(count > 0);
    assert(values[0] != NULL);
    {
        const wchar_t *indexword = *values;
        if (!xwcstol(indexword, 10, &index)) {
            xerror(errno, Ngt("`%ls' is not a valid integer"), indexword);
            return;
        }
    }
    count--, values++;
    assert(plcount(values) == count);

    if (index < 0) {
        index += array->v_valc + 1;
        if (index < 0)
            index = 0;
    }

    size_t uindex;
    if (LONG_LT_SIZE(index, array->v_valc))
        uindex = (size_t) index;
    else
        uindex = array->v_valc;

    plist_T list;
    pl_initwith(&list, array->v_vals, array->v_valc);
    pl_insert(&list, uindex, values);
    for (size_t i = 0; i < count; i++)
        list.contents[uindex + i] = xwcsdup(list.contents[uindex + i]);
    array->v_valc = list.length;
    array->v_vals = pl_toary(&list);

    if (count > 0) {
        variable_set(name, array);
        if (array->v_type & VF_EXPORT)
            update_environment(name);
    }
}

/* Sets the value of the specified element of the array.
 * `name' is the name of the array variable.
 * `indexword' is parsed as the integer index of the element.
 * An error message is printed to the standard error on error. */
void array_set_element(const wchar_t *name, variable_T *array,
        const wchar_t *indexword, const wchar_t *value)
{
    assert((array->v_type & VF_MASK) == VF_ARRAY);

    long index;
    if (!xwcstol(indexword, 10, &index)) {
        xerror(errno, Ngt("`%ls' is not a valid integer"), indexword);
        return;
    }

    size_t uindex;
    if (index < 0) {
        index += array->v_valc;
        if (index < 0)
            goto invalid_index;
        assert(LONG_LT_SIZE(index, array->v_valc));
        uindex = (size_t) index;
    } else if (index > 0) {
        if (!LONG_LT_SIZE(index - 1, array->v_valc))
            goto invalid_index;
        uindex = (size_t) index - 1;
    } else {
        goto invalid_index;
    }
    assert(uindex < array->v_valc);
    free(array->v_vals[uindex]);
    array->v_vals[uindex] = xwcsdup(value);

    variable_set(name, array);
    if (array->v_type & VF_EXPORT)
        update_environment(name);
    return;

invalid_index:
    xerror(0, Ngt("index %ls is out of range "
                "(the actual size of array $%ls is %zu)"),
            indexword, name, array->v_valc);
}

#if YASH_ENABLE_HELP
const char array_help[] = Ngt(
"manipulate an array"
);
const char array_syntax[] = Ngt(
"\tarray                  # print arrays\n"
"\tarray name [value...]  # set array values\n"
"\tarray -d name [index...]\n"
"\tarray -i name index [value...]\n"
"\tarray -s name index value\n"
);
#endif

#endif /* YASH_ENABLE_ARRAY */

/* Options for the "unset" built-in. */
const struct xgetopt_T unset_options[] = {
    { L'f', L"functions", OPTARG_NONE, true,  NULL, },
    { L'v', L"variables", OPTARG_NONE, true,  NULL, },
#if YASH_ENABLE_HELP
    { L'-', L"help",      OPTARG_NONE, false, NULL, },
#endif
    { L'\0', NULL, 0, false, NULL, },
};

/* The "unset" built-in, which accepts the following options:
 *  -f: deletes functions
 *  -v: deletes variables (default) */
int unset_builtin(int argc, void **argv)
{
    bool function = false;

    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, unset_options, 0)) != NULL) {
        switch (opt->shortopt) {
            case L'f':  function = true;   break;
            case L'v':  function = false;  break;
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return special_builtin_error(Exit_ERROR);
        }
    }

    if (posixly_correct && xoptind == argc)
        return insufficient_operands_error(1);

    for (; xoptind < argc; xoptind++) {
        const wchar_t *name = ARGV(xoptind);
        if (function) {
            unset_function(name);
        } else {
            if (wcschr(name, L'='))
                continue;
            unset_variable(name);
        }
    }

    return (yash_error_message_count == 0) ?
            Exit_SUCCESS : special_builtin_error(Exit_FAILURE);
}

/* Unsets the specified function.
 * On error, an error message is printed to the standard error and TRUE is
 * returned. */
bool unset_function(const wchar_t *name)
{
    kvpair_T kv = ht_remove(&functions, name);
    function_T *f = kv.value;
    if (f != NULL) {
        if (!(f->f_type & VF_NODELETE)) {
            funckvfree(kv);
        } else {
            xerror(0, Ngt("function `%ls' is read-only"), name);
            ht_set(&functions, kv.key, kv.value);
            return true;
        }
    }
    return false;
}

/* Unsets the specified variable.
 * On error, an error message is printed to the standard error and TRUE is
 * returned. */
bool unset_variable(const wchar_t *name)
{
    for (environ_T *env = current_env; env != NULL; env = env->parent) {
        kvpair_T kv = ht_remove(&env->contents, name);
        variable_T *var = kv.value;
        if (var != NULL) {
            if (!(var->v_type & VF_NODELETE)) {
                bool exported = var->v_type & VF_EXPORT;
                varkvfree(kv);
                variable_set(name, NULL);
                if (exported)
                    update_environment(name);
                return false;
            } else {
                xerror(0, Ngt("$%ls is read-only"), name);
                ht_set(&env->contents, kv.key, kv.value);
                return true;
            }
        }
    }
    return false;
}

#if YASH_ENABLE_HELP
const char unset_help[] = Ngt(
"remove variables or functions"
);
const char unset_syntax[] = Ngt(
"\tunset [-fv] [name...]\n"
);
#endif

/* Options for the "shift" built-in. */
const struct xgetopt_T shift_options[] = {
    { L'A', L"array", OPTARG_REQUIRED, false, NULL, },
#if YASH_ENABLE_HELP
    { L'-', L"help",  OPTARG_NONE,     false, NULL, },
#endif
    { L'\0', NULL, 0, false, NULL, },
};

/* The "shift" built-in */
int shift_builtin(int argc, void **argv)
{
    const wchar_t *arrayname = NULL;

    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, shift_options, XGETOPT_DIGIT)) != NULL) {
        switch (opt->shortopt) {
            case L'A':
                arrayname = xoptarg;
                break;
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return special_builtin_error(Exit_ERROR);
        }
    }

    if (!validate_operand_count(argc - xoptind, 0, 1))
        return special_builtin_error(Exit_ERROR);

    long count;
    if (xoptind < argc) {
        if (!xwcstol(ARGV(xoptind), 10, &count)) {
            xerror(errno, Ngt("`%ls' is not a valid integer"), ARGV(xoptind));
            return special_builtin_error(Exit_ERROR);
        } else if (posixly_correct && count < 0) {
            xerror(0, Ngt("%ls: the operand value must not be negative"),
                    ARGV(xoptind));
            return special_builtin_error(Exit_ERROR);
        }
    } else {
        count = 1;
    }

    variable_T *var;
    if (arrayname == NULL) {
        var = search_variable(L VAR_positional);
        assert(var != NULL && (var->v_type & VF_MASK) == VF_ARRAY);
    } else {
        var = search_variable(arrayname);
        if (wcschr(arrayname, L'=') != NULL ||
                var == NULL || (var->v_type & VF_MASK) != VF_ARRAY) {
            xerror(0, Ngt("$%ls is not an array"), arrayname);
            return Exit_FAILURE;
        }
    }

    unsigned long abscount =
        (count >= 0) ? (unsigned long) count : -(unsigned long) count;

    if (
#if ULONG_MAX > SIZE_MAX
            abscount > (unsigned long) var->v_valc
#else
            (size_t) abscount > var->v_valc
#endif
            ) {
        const char *message;
        if (arrayname == NULL) {
            message = ngt("%ld: cannot shift so many "
                    "(there is only one positional parameter)",
                    "%ld: cannot shift so many "
                    "(there are only %zu positional parameters)",
                    var->v_valc);
        } else {
            message = ngt("%ld: cannot shift so many "
                    "(there is only one array element)",
                    "%ld: cannot shift so many "
                    "(there are only %zu array elements)",
                    var->v_valc);
        }
        xerror(0, message, count, var->v_valc);
        return Exit_FAILURE;
    }

    size_t from = (count >= 0) ? 0 : (var->v_valc - (size_t) abscount);
    plist_T list;
    pl_initwith(&list, var->v_vals, var->v_valc);
    for (size_t i = 0; i < (size_t) abscount; i++)
        free(list.contents[from + i]);
    pl_remove(&list, from, (size_t) abscount);
    var->v_valc = list.length;
    var->v_vals = pl_toary(&list);

    return Exit_SUCCESS;
}

#if YASH_ENABLE_HELP
const char shift_help[] = Ngt(
"remove some positional parameters or array elements"
);
const char shift_syntax[] = Ngt(
"\tshift [-A array_name] [count]\n"
);
#endif

/* The "getopts" built-in */
int getopts_builtin(int argc, void **argv)
{
    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, help_option, XGETOPT_POSIX)) != NULL) {
        switch (opt->shortopt) {
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return Exit_ERROR;
        }
    }

    if (!validate_operand_count(argc - xoptind, 2, SIZE_MAX))
        return Exit_ERROR;

    const wchar_t *options = ARGV(xoptind++);
    const wchar_t *varname = ARGV(xoptind++);
    void *const *args;
    unsigned long optind, optsubind;
    const wchar_t *arg, *optp;
    wchar_t optchar;

    if (wcschr(varname, L'=')) {
        xerror(0, Ngt("`%ls' is not a valid variable name"), varname);
        return Exit_ERROR;
    } else if (!check_options(options)) {
        xerror(0, Ngt("`%ls' is not a valid option specification"), options);
        return Exit_ERROR;
    }

    /* Parse $OPTIND */
    {
        const wchar_t *varoptind = getvar(L VAR_OPTIND);
        wchar_t *endp;
        if (varoptind == NULL || varoptind[0] == L'\0')
            goto optind_invalid;
        errno = 0;
        optind = wcstoul(varoptind, &endp, 10);
        if (errno != 0 || varoptind == endp)
            goto optind_invalid;
        optind -= 1;

        if (*endp == L':') {
            endp++;
            if (!xwcstoul(endp, 10, &optsubind) || optsubind == 0)
                goto optind_invalid;
        } else {
            optsubind = 1;
        }
    }

    if (xoptind < argc) {
        if (optind >= (unsigned long) (argc - xoptind))
            goto no_more_options;
        args = &argv[xoptind];
    } else {
        variable_T *var = search_variable(L VAR_positional);
        assert(var != NULL && (var->v_type & VF_MASK) == VF_ARRAY);
        if (optind >= var->v_valc)
            goto no_more_options;
        args = var->v_vals;
    }

#define TRY(exp)  do { if (!(exp)) return Exit_ERROR; } while (0)
parse_arg:
    arg = args[optind];
    if (arg == NULL || arg[0] != L'-' || arg[1] == L'\0') {
        goto no_more_options;
    } else if (arg[1] == L'-' && arg[2] == L'\0') {  /* arg == "--" */
        optind++;
        goto no_more_options;
    } else if (xwcsnlen(arg, optsubind + 1) <= optsubind) {
        optind++, optsubind = 1;
        goto parse_arg;
    }

    optchar = arg[optsubind++];
    assert(optchar != L'\0');
    if (optchar == L':' || (optp = wcschr(options, optchar)) == NULL) {
        /* invalid option */
        TRY(set_variable_single_char(varname, L'?'));
        if (options[0] == L':') {
            TRY(set_variable_single_char(L VAR_OPTARG, optchar));
        } else {
            fprintf(stderr, gt("%ls: `-%lc' is not a valid option\n"),
                    command_name, (wint_t) optchar);
            TRY(!unset_variable(L VAR_OPTARG));
        }
    } else {
        /* valid option */
        if (optp[1] != L':') {
            /* option without an argument */
            TRY(!unset_variable(L VAR_OPTARG));
        } else {
            /* option with an argument */
            const wchar_t *optarg = &arg[optsubind];
            optsubind = 1;
            optind++;
            if (optarg[0] == L'\0') {
                optarg = args[optind++];
                if (optarg == NULL) {
                    /* argument is missing */
                    if (options[0] == L':') {
                        TRY(set_variable_single_char(varname, L':'));
                        TRY(set_variable_single_char(L VAR_OPTARG, optchar));
                    } else {
                        fprintf(stderr,
                            gt("%ls: the -%lc option's argument is missing\n"),
                            command_name, (wint_t) optchar);
                        TRY(set_variable_single_char(varname, L'?'));
                        TRY(!unset_variable(L VAR_OPTARG));
                    }
                    goto finish;
                }
            }
            TRY(set_optarg(optarg));
        }
        TRY(set_variable_single_char(varname, optchar));
    }
finish:
    TRY(set_optind(optind, optsubind));
    return Exit_SUCCESS;
#undef TRY

no_more_options:
    set_optind(optind, 0);
    set_variable_single_char(varname, L'?');
    unset_variable(L VAR_OPTARG);
    return Exit_FAILURE;
optind_invalid:
    xerror(0, Ngt("$OPTIND has an invalid value"));
    return Exit_ERROR;
}

/* Checks if the `options' is valid. Returns true iff OK. */
bool check_options(const wchar_t *options)
{
    if (options[0] == L':')
        options++;
    for (;;) switch (*options) {
        case L'\0':
            return true;
        case L':':
            return false;
        case L'?':
            if (posixly_correct)
                return false;
            /* falls thru! */
        default:
            if (posixly_correct && !iswalnum(*options))
                return false;
            options++;
            if (*options == L':')
                options++;
            continue;
    }
}

/* Sets $OPTIND to `optind' plus 1, followed by `optsubind' (if > 1). */
bool set_optind(unsigned long optind, unsigned long optsubind)
{
    wchar_t *value = malloc_wprintf(
            optsubind > 1 ? L"%lu:%lu" : L"%lu",
            optind + 1, optsubind);
    return set_variable(L VAR_OPTIND, value, SCOPE_GLOBAL, false);
}

/* Sets $OPTARG to `value'. */
bool set_optarg(const wchar_t *value)
{
    return set_variable(L VAR_OPTARG, xwcsdup(value), SCOPE_GLOBAL, false);
}

/* Sets the specified variable to the single character `value'. */
bool set_variable_single_char(const wchar_t *varname, wchar_t value)
{
    wchar_t *v = xmallocn(2, sizeof *v);
    v[0] = value;
    v[1] = L'\0';
    return set_variable(varname, v, SCOPE_GLOBAL, false);
}

#if YASH_ENABLE_HELP
const char getopts_help[] = Ngt(
"parse command options"
);
const char getopts_syntax[] = Ngt(
"\tgetopts options variable [argument...]\n"
);
#endif

/* Options for the "read" built-in. */
const struct xgetopt_T read_options[] = {
    { L'A', L"array",        OPTARG_NONE,     false, NULL, },
    { L'd', L"delimiter",    OPTARG_REQUIRED, true,  NULL, },
    { L'e', L"line-editing", OPTARG_NONE,     false, NULL, },
    { L'P', L"ps1",          OPTARG_NONE,     false, NULL, },
    { L'p', L"prompt",       OPTARG_REQUIRED, false, NULL, },
    { L'r', L"raw-mode",     OPTARG_NONE,     true,  NULL, },
#if YASH_ENABLE_HELP
    { L'-', L"help",         OPTARG_NONE,     false, NULL, },
#endif
    { L'\0', NULL, 0, false, NULL, },
};

struct reading_option_T {
    bool array, lineedit, ps1, raw;
    const wchar_t *delimiter, *prompt;
};

/* The "read" built-in, which accepts the following options:
 *  -A: assign values to array
 *  -d: specify delimiter
 *  -e: use line-editing
 *  -P: use $PS1
 *  -p: specify prompt
 *  -r: don't treat backslashes specially
 */
int read_builtin(int argc, void **argv)
{
    struct reading_option_T ro = {
        .array = false,
        .lineedit = false,
        .ps1 = false,
        .raw = false,
        .delimiter = L"\n",
        .prompt = NULL,
    };

    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, read_options, 0)) != NULL) {
        switch (opt->shortopt) {
            case L'A':  ro.array     = true;     break;
            case L'd':  ro.delimiter = xoptarg;  break;
            case L'e':  ro.lineedit  = true;     break;
            case L'P':  ro.ps1       = true;     break;
            case L'p':  ro.prompt    = xoptarg;  break;
            case L'r':  ro.raw       = true;     break;
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return 4;
        }
    }

    if (ro.ps1 && ro.prompt != NULL)
        return mutually_exclusive_option_error(L'P', L'p'), 4;
    if (xoptind == argc)
        return insufficient_operands_error(1), 4;
    if (wcslen(ro.delimiter) > 1) {
        xerror(0, Ngt("multi-character delimiter is not supported"));
        return 4;
    }

    /* check if the identifiers are valid */
    for (int i = xoptind; i < argc; i++) {
        if (wcschr(ARGV(i), L'=') != NULL) {
            xerror(0, Ngt("`%ls' is not a valid variable name"), ARGV(i));
            return 4;
        }
    }

    xwcsbuf_T buf;
    xstrbuf_T cc;

    wb_init(&buf);
    sb_init(&cc);
    if (!read_with_prompt(&buf, &cc, &ro)) {
        sb_destroy(&cc);
        wb_destroy(&buf);
        return 3;
    }
    assert(buf.length == cc.length);

    /* remove trailing delimiter */
    bool delimited;
    if (buf.length > 0 && buf.contents[buf.length - 1] == ro.delimiter[0] &&
            !(cc.contents[cc.length - 1] & CC_QUOTED)) {
        wb_truncate(&buf, buf.length - 1);
        sb_truncate(&cc, cc.length - 1);
        delimited = true;
    } else {
        delimited = false;
    }

    /* split fields */
    const wchar_t *tail;
    plist_T list;
    pl_init(&list);
    {
        const wchar_t *ifs = getvar(L VAR_IFS);
        if (ifs == NULL)
            ifs = DEFAULT_IFS;

        tail = extract_fields(buf.contents, cc.contents, ifs, &list);
        assert(list.length % 2 == 0);
    }

    /* Add missing empty fields */
    size_t count = (size_t) argc - xoptind;
    for (size_t i = list.length / 2; i < count; i++)
        pl_add(pl_add(&list, buf.contents), buf.contents);
    assert(list.length % 2 == 0);
    assert(list.length > 0);

    /* assign variables except last */
    const wchar_t *name;
    for (size_t i = 0; i < count - 1; i++) {
        const wchar_t *start = list.contents[2 * i];
        const wchar_t *end = list.contents[2 * i + 1];
        wchar_t *field = xwcsndup(start, end - start);
        name = ARGV(xoptind + i);
        set_variable(name, field, SCOPE_GLOBAL, false);
    }

    /* assign last variable */
    name = ARGV(xoptind + count - 1);
    if (ro.array) {
        assign_array(name, &list, 2 * (count - 1));
    } else {
        size_t i = count - 1;
        const wchar_t *start = list.contents[2 * i];
        const wchar_t *end = list.contents[2 * i + 1];
        if (2 * count < list.length)
            end = tail;
        wchar_t *field = xwcsndup(start, end - start);
        set_variable(name, field, SCOPE_GLOBAL, false);
    }

    pl_destroy(&list);
    sb_destroy(&cc);
    wb_destroy(&buf);
    return yash_error_message_count != 0 ? 2 : !delimited ? 1 : 0;
}

/* Reads one line from the standard input. The result is appended to `buf' and
 * `cc'. `buf' will contain no escapes or other special characters. `cc' is the
 * charcategory_T string for `buf'. It indicates whether `buf' can be split at
 * the corresponding character when passed to `extract_fields'.
 * If `ro->raw' is true, exactly one line is read and backslashes are not
 * treated as escapes. Otherwise, line continuations cause this function to read
 * more and backslash escapes are recognized.
 * Returns false on error while reading. */
bool read_with_prompt(
        xwcsbuf_T *buf, xstrbuf_T *cc, const struct reading_option_T *ro)
{
    wchar_t delimiter = ro->delimiter[0];

    bool firstline = true;
    bool completed = false;
    bool use_prompt = is_interactive_now && isatty(STDIN_FILENO);

    while (!completed) {
        xwcsbuf_T line;
        if (use_prompt) {
            struct promptset_T prompt = promptset_for_read(firstline, ro);
            line = read_one_line_with_prompt(prompt, delimiter, ro->lineedit);
            free_prompt(prompt);
        } else {
            line = read_one_line(delimiter);
        }
        if (line.contents == NULL)
            return false;

        if (ro->raw) {
            wb_cat(buf, line.contents);
            sb_ccat_repeat(cc, CC_SOFT_EXPANSION, line.length);
            completed = true;
        } else {
            completed =
                line.length == 0 || unescape_line(&line, buf, cc, delimiter);
        }
        wb_destroy(&line);

        firstline = false;
    }
    return true;
}

/* Returns a prompt set for reading in the "read" built-in. */
struct promptset_T promptset_for_read(
        bool firstline, const struct reading_option_T *ro)
{
    if (!firstline)
        return get_prompt(2);
    if (ro->ps1)
        return get_prompt(1);

    struct promptset_T ps;
    ps.main = escape(ro->prompt != NULL ? ro->prompt : L"", L"\\");
    ps.right = xwcsdup(L"");
    ps.styler = xwcsdup(L"");
    ps.predict = xwcsdup(L"");
    return ps;
}

/* Reads one line from the standard input with the specified prompt.
 * If `lineedit' is true, use line-editing if possible.
 * The result is returned as a wide string buffer. The buffer contents are NULL
 * if an error occurs. */
xwcsbuf_T read_one_line_with_prompt(
        struct promptset_T prompt, wchar_t delimiter, bool lineedit)
{
    xwcsbuf_T buf;

    if (lineedit) {
#if YASH_ENABLE_LINEEDIT
        if (delimiter == L'\n' && shopt_lineedit != SHOPT_NOLINEEDIT) {
            wchar_t *line;
            switch (le_readline(prompt, false, &line)) {
                case INPUT_OK:
                    wb_initwith(&buf, line);
                    return buf;
                case INPUT_EOF:
                    wb_init(&buf);
                    return buf;
                case INPUT_INTERRUPTED:
                    set_interrupted();
                    buf.contents = NULL;
                    return buf;
                case INPUT_ERROR:
                    break;
            }
        }
#endif // YASH_ENABLE_LINEEDIT
    }

    print_prompt(prompt.main);
    print_prompt(prompt.styler);

    buf = read_one_line(delimiter);

    print_prompt(PROMPT_RESET);

    return buf;
}

/* Reads one line from the standard input without printing any prompt or using
 * line-editing.
 * The result is returned as a newly-malloced wide string. The result is null
 * iff an error occurs. */
xwcsbuf_T read_one_line(wchar_t delimiter)
{
    xwcsbuf_T buf;
    wb_init(&buf);

    inputresult_T result =
        read_input_delimited(&buf, stdin_input_file_info, false, delimiter);

    if (result == INPUT_ERROR) {
        wb_destroy(&buf);
        buf.contents = NULL;
    }
    return buf;
}

/* Parses a string that may contain backslash escapes.
 * Unescaped `line' is appended to `buf' with a corresponding charcategory_T
 * string appended to `cc'.
 * The result is false iff the read built-in should read the next line, that is,
 * the line does not end with an unescaped delimiter.
 * Line continuations are skipped and not appended to `buf' or `cc'. */
bool unescape_line(
        const xwcsbuf_T *line, xwcsbuf_T *buf, xstrbuf_T *cc, wchar_t delimiter)
{
    for (size_t i = 0; i < line->length; i++) {
        if (delimiter != L'\\' && line->contents[i] == L'\\') {
            i++;
            if (i == line->length)
                break;
            if (line->contents[i] == L'\n')
                continue;
            wb_wccat(buf, line->contents[i]);
            sb_ccat(cc, CC_SOFT_EXPANSION | CC_QUOTED);
        } else {
            wb_wccat(buf, line->contents[i]);
            sb_ccat(cc, CC_SOFT_EXPANSION);
            if (line->contents[i] == delimiter)
                return true;
        }
    }
    return false;
}

/* Assigns a result of field-splitting contained in `ranges' to an array named
 * `name'. Fields are assigned starting from index `i' in `ranges'. */
void assign_array(const wchar_t *name, const plist_T *ranges, size_t i)
{
    assert((ranges->length - i) % 2 == 0);

    plist_T fields;
    pl_init(&fields);
    while (i < ranges->length) {
        const wchar_t *start = ranges->contents[i++];
        const wchar_t *end = ranges->contents[i++];
        wchar_t *field = xwcsndup(start, end - start);
        pl_add(&fields, field);
    }

    if (fields.length == 1 && ((wchar_t *) fields.contents[0])[0] == L'\0') {
        free(fields.contents[0]);
        pl_remove(&fields, 0, 1);
    }

    size_t count = fields.length;
    void **values = pl_toary(&fields);
    set_array(name, count, values, SCOPE_GLOBAL, false);
}

#if YASH_ENABLE_HELP
const char read_help[] = Ngt(
"read a line from the standard input"
);
const char read_syntax[] = Ngt(
"\tread [-Aer] [-d delimiter] [-P|-p prompt] variable...\n"
);
#endif

/* options for the "pushd" built-in */
const struct xgetopt_T pushd_options[] = {
#if YASH_ENABLE_DIRSTACK
    { L'D', L"remove-duplicates", OPTARG_NONE,     true,  NULL, },
#endif
    { L'd', L"default-directory", OPTARG_REQUIRED, false, NULL, },
    { L'e', L"ensure-pwd",        OPTARG_NONE,     true,  NULL, },
    { L'L', L"logical",           OPTARG_NONE,     true,  NULL, },
    { L'P', L"physical",          OPTARG_NONE,     true,  NULL, },
#if YASH_ENABLE_HELP
    { L'-', L"help",              OPTARG_NONE,     false, NULL, },
#endif
    { L'\0', NULL, 0, false, NULL, },
};
/* Note: `cd_options' and `pwd_options' are defined as part of `pushd_options'.
 */

#if YASH_ENABLE_DIRSTACK

static variable_T *get_dirstack(void);
static void push_dirstack(variable_T *var, wchar_t *value)
    __attribute__((nonnull));
static void remove_dirstack_entry_at(variable_T *var, size_t index)
    __attribute__((nonnull));
static void remove_dirstack_dups(variable_T *var)
    __attribute__((nonnull));
static bool print_dirstack_entry(
        bool verbose, size_t plusindex, size_t minusindex, const wchar_t *dir)
    __attribute__((nonnull));

/* The "pushd" built-in.
 *  -L: don't resolve symbolic links (default)
 *  -P: resolve symbolic links
 *  -e: fail if new $PWD value cannot be determined
 *  --default-directory=<dir>: go to <dir> when the operand is missing
 *  --remove-duplicates: remove duplicate entries in the directory stack.
 * -L and -P are mutually exclusive: the one specified last is used. */
int pushd_builtin(int argc __attribute__((unused)), void **argv)
{
    bool logical = true, remove_dups = false, ensure_pwd = false;
    const wchar_t *newpwd = L"+1";

    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, pushd_options, XGETOPT_DIGIT)) != NULL) {
        switch (opt->shortopt) {
            case L'L':  logical = true;       break;
            case L'P':  logical = false;      break;
            case L'd':  newpwd = xoptarg;     break;
            case L'D':  remove_dups = true;   break;
            case L'e':  ensure_pwd = true;    break;
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return 5;
        }
    }

    if (logical && ensure_pwd) {
        xerror(0, Ngt("the -e option requires the -P option"));
        return 5;
    }
    if (!validate_operand_count(argc - xoptind, 0, 1))
        return 5;

    const wchar_t *origpwd = getvar(L VAR_PWD);
    if (origpwd == NULL) {
        xerror(0, Ngt("$PWD is not set"));
        return 4;
    }

    bool useoldpwd = false;
    if (xoptind < argc) {
        newpwd = ARGV(xoptind);
        if (wcscmp(newpwd, L"-") == 0)
            useoldpwd = true;
    }

    size_t stackindex;
    if (useoldpwd) {
        newpwd = getvar(L VAR_OLDPWD);
        stackindex = SIZE_MAX;
        if (newpwd == NULL || newpwd[0] == L'\0') {
            xerror(0, Ngt("$OLDPWD is not set"));
            return 4;
        }
    } else {
        if (!parse_dirstack_index(newpwd, &stackindex, &newpwd, true))
            return 4;
    }
    assert(newpwd != NULL);

    wchar_t *saveorigpwd = xwcsdup(origpwd);
    int result = change_directory(newpwd, useoldpwd, logical, ensure_pwd);
#ifndef NDEBUG
    newpwd = NULL;  /* newpwd cannot be used anymore. */
#endif
    if (result > 1) {
        free(saveorigpwd);
        return result;
    }

    variable_T *var = get_dirstack();
    if (var == NULL) {
        free(saveorigpwd);
        return 1;
    }

    push_dirstack(var, saveorigpwd);
    if (stackindex != SIZE_MAX)
        remove_dirstack_entry_at(var, stackindex);
    if (remove_dups)
        remove_dirstack_dups(var);
    if (var->v_type & VF_EXPORT)
        update_environment(L VAR_DIRSTACK);
    return result;
}

/* Returns the directory stack.
 * If the stack is not yet created, it is initialized as an empty stack and
 * returned.
 * Fails if a non-array variable or a read-only array already exists, in which
 * case NULL is returned. */
variable_T *get_dirstack(void)
{
    variable_T *var = search_variable(L VAR_DIRSTACK);
    if (var != NULL) {
        if ((var->v_type & VF_MASK) != VF_ARRAY) {
            xerror(0, Ngt("$%ls is not an array"), L VAR_DIRSTACK);
            return NULL;
        } else if (var->v_type & VF_READONLY) {
            xerror(0, Ngt("$%ls is read-only"), L VAR_DIRSTACK);
            return NULL;
        }
        return var;
    }

    // void **ary = xmallocn(1, sizeof *ary);
    void **ary = xmalloc(sizeof *ary);
    ary[0] = NULL;
    return set_array(L VAR_DIRSTACK, 0, ary, SCOPE_GLOBAL, false);
}

/* Adds the specified value to the directory stack ($DIRSTACK).
 * `var' must be the return value of a `get_dirstack' call.
 * `value' is used as an element of the $DIRSTACK array, so the caller must not
 * modify or free `value' after calling this function. */
void push_dirstack(variable_T *var, wchar_t *value)
{
    size_t index = var->v_valc++;
    var->v_vals = xrealloce(var->v_vals, index, 2, sizeof *var->v_vals);
    var->v_vals[index] = value;
    var->v_vals[index + 1] = NULL;
}

/* Removes the directory stack entry specified by `index'.
 * `var' must be the directory stack array and `index' must be less than
 * `var->v_valc'. */
void remove_dirstack_entry_at(variable_T *var, size_t index)
{
    assert(index < var->v_valc);
    free(var->v_vals[index]);
    memmove(&var->v_vals[index], &var->v_vals[index + 1],
            (var->v_valc - index) * sizeof *var->v_vals);
    var->v_valc--;
}

/* Removes directory stack entries that are the same as the current working
 * directory ($PWD).
 * `var' must be the return value of a `get_dirstack' call. */
void remove_dirstack_dups(variable_T *var)
{
    const wchar_t *pwd = getvar(L VAR_PWD);
    if (pwd == NULL)
        return;

    for (size_t index = var->v_valc; index-- > 0; )
        if (wcscmp(pwd, var->v_vals[index]) == 0)
            remove_dirstack_entry_at(var, index);
}

#if YASH_ENABLE_HELP
const char pushd_help[] = Ngt(
"push a directory into the directory stack"
);
const char pushd_syntax[] = Ngt(
"\tpushd [-L|-P [-e]] [directory]\n"
);
#endif

/* The "popd" built-in. */
int popd_builtin(int argc, void **argv)
{
    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, help_option, XGETOPT_DIGIT)) != NULL) {
        switch (opt->shortopt) {
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return 5;
        }
    }

    const wchar_t *arg;
    switch (argc - xoptind) {
        case 0:   arg = L"+0";          break;
        case 1:   arg = ARGV(xoptind);  break;
        default:  too_many_operands_error(1);  return 5;
    }

    variable_T *var = get_dirstack();
    if (var == NULL)
        return 4;
    if (var->v_valc == 0) {
        xerror(0, Ngt("the directory stack is empty"));
        return 4;
    }

    size_t stackindex;
    const wchar_t *dummy;
    if (!parse_dirstack_index(arg, &stackindex, &dummy, true))
        return 4;
    if (stackindex == SIZE_MAX) {
        xerror(0, Ngt("`%ls' is not a valid index"), arg);
        return 5;
    }

    if (stackindex < var->v_valc) {
        remove_dirstack_entry_at(var, stackindex);
        if (var->v_type & VF_EXPORT)
            update_environment(L VAR_DIRSTACK);
        return 0;
    }

    int result;
    wchar_t *newpwd;

    assert(var->v_valc > 0);
    var->v_valc--;
    newpwd = var->v_vals[var->v_valc];
    var->v_vals[var->v_valc] = NULL;
    result = change_directory(newpwd, true, true, false);
    free(newpwd);
    if (var->v_type & VF_EXPORT)
        update_environment(L VAR_DIRSTACK);
    return result;
}

#if YASH_ENABLE_HELP
const char popd_help[] = Ngt(
"pop a directory from the directory stack"
);
const char popd_syntax[] = Ngt(
"\tpopd [index]\n"
);
#endif

/* Options for the "dirs" built-in. */
const struct xgetopt_T dirs_options[] = {
    { L'c', L"clear",   OPTARG_NONE, true,  NULL, },
    { L'v', L"verbose", OPTARG_NONE, true,  NULL, },
#if YASH_ENABLE_HELP
    { L'-', L"help",    OPTARG_NONE, false, NULL, },
#endif
    { L'\0', NULL, 0, false, NULL, },
};

/* The "dirs" built-in, which accepts the following options:
 *  -c: clear the stack
 *  -v: verbose */
int dirs_builtin(int argc, void **argv)
{
    bool clear = false, verbose = false;

    const struct xgetopt_T *opt;
    xoptind = 0;
    while ((opt = xgetopt(argv, dirs_options, XGETOPT_DIGIT)) != NULL) {
        switch (opt->shortopt) {
            case L'c':  clear   = true;  break;
            case L'v':  verbose = true;  break;
#if YASH_ENABLE_HELP
            case L'-':
                return print_builtin_help(ARGV(0));
#endif
            default:
                return Exit_ERROR;
        }
    }

    if (clear)
        return unset_variable(L VAR_DIRSTACK) ? Exit_FAILURE : Exit_SUCCESS;

    variable_T *var = search_variable(L VAR_DIRSTACK);
    bool dirvalid = (var != NULL && (var->v_type & VF_MASK) == VF_ARRAY);
    size_t size = dirvalid ? var->v_valc : 0;
    const wchar_t *dir;
    if (xoptind < argc) {
        /* print the specified only */
        do {
            size_t index;
            if (!parse_dirstack_index(ARGV(xoptind), &index, &dir, true))
                continue;
            if (index == SIZE_MAX) {
                xerror(0, Ngt("`%ls' is not a valid index"), ARGV(xoptind));
                continue;
            }

            if (!print_dirstack_entry(verbose, size - index, index, dir))
                break;
        } while (++xoptind < argc);
    } else {
        /* print all */
        dir = getvar(L VAR_PWD);
        if (dir == NULL) {
            xerror(0, Ngt("$PWD is not set"));
        } else {
            print_dirstack_entry(verbose, 0, size, dir);
        }

        if (dirvalid && yash_error_message_count == 0) {
            for (size_t i = var->v_valc; i-- > 0; ) {
                if (!print_dirstack_entry(verbose, size - i, i, var->v_vals[i]))
                    break;
            }
        }
    }

    return (yash_error_message_count == 0) ? Exit_SUCCESS : Exit_FAILURE;
}

bool print_dirstack_entry(
        bool verbose, size_t plusindex, size_t minusindex, const wchar_t *dir)
{
    if (verbose)
        return xprintf("+%zu\t-%zu\t%ls\n", plusindex, minusindex, dir);
    else
        return xprintf("%ls\n", dir);
}

#if YASH_ENABLE_HELP
const char dirs_help[] = Ngt(
"print the directory stack"
);
const char dirs_syntax[] = Ngt(
"\tdirs [-cv] [index...]\n"
);
#endif

#endif /* YASH_ENABLE_DIRSTACK */


/* vim: set ts=8 sts=4 sw=4 et tw=80: */