1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
|
/* subst.c -- The part of the shell that does parameter, command, arithmetic,
and globbing substitutions. */
/* ``Have a little faith, there's magic in the night. You ain't a
beauty, but, hey, you're alright.'' */
/* Copyright (C) 1987-2018 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash 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 3 of the License, or
(at your option) any later version.
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "bashtypes.h"
#include <stdio.h>
#include "chartypes.h"
#if defined (HAVE_PWD_H)
# include <pwd.h>
#endif
#include <signal.h>
#include <errno.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#define NEED_FPURGE_DECL
#include "bashansi.h"
#include "posixstat.h"
#include "bashintl.h"
#include "shell.h"
#include "parser.h"
#include "flags.h"
#include "jobs.h"
#include "execute_cmd.h"
#include "filecntl.h"
#include "trap.h"
#include "pathexp.h"
#include "mailcheck.h"
#include "shmbutil.h"
#if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
# include <mbstr.h> /* mbschr */
#endif
#include "typemax.h"
#include "builtins/getopt.h"
#include "builtins/common.h"
#include "builtins/builtext.h"
#include <tilde/tilde.h>
#include <glob/strmatch.h>
#if !defined (errno)
extern int errno;
#endif /* !errno */
/* The size that strings change by. */
#define DEFAULT_INITIAL_ARRAY_SIZE 112
#define DEFAULT_ARRAY_SIZE 128
/* Variable types. */
#define VT_VARIABLE 0
#define VT_POSPARMS 1
#define VT_ARRAYVAR 2
#define VT_ARRAYMEMBER 3
#define VT_ASSOCVAR 4
#define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
/* Flags for quoted_strchr */
#define ST_BACKSL 0x01
#define ST_CTLESC 0x02
#define ST_SQUOTE 0x04 /* unused yet */
#define ST_DQUOTE 0x08 /* unused yet */
/* These defs make it easier to use the editor. */
#define LBRACE '{'
#define RBRACE '}'
#define LPAREN '('
#define RPAREN ')'
#define LBRACK '['
#define RBRACK ']'
#if defined (HANDLE_MULTIBYTE)
#define WLPAREN L'('
#define WRPAREN L')'
#endif
#define DOLLAR_AT_STAR(c) ((c) == '@' || (c) == '*')
#define STR_DOLLAR_AT_STAR(s) (DOLLAR_AT_STAR ((s)[0]) && (s)[1] == '\0')
/* Evaluates to 1 if C is one of the shell's special parameters whose length
can be taken, but is also one of the special expansion characters. */
#define VALID_SPECIAL_LENGTH_PARAM(c) \
((c) == '-' || (c) == '?' || (c) == '#' || (c) == '@')
/* Evaluates to 1 if C is one of the shell's special parameters for which an
indirect variable reference may be made. */
#define VALID_INDIR_PARAM(c) \
((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*')
/* Evaluates to 1 if C is one of the OP characters that follows the parameter
in ${parameter[:]OPword}. */
#define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
/* Evaluates to 1 if this is one of the shell's special variables. */
#define SPECIAL_VAR(name, wi) \
(*name && ((DIGIT (*name) && all_digits (name)) || \
(name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
(wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1]))))
/* This can be used by all of the *_extract_* functions that have a similar
structure. It can't just be wrapped in a do...while(0) loop because of
the embedded `break'. The dangling else accommodates a trailing semicolon;
we could also put in a do ; while (0) */
#define CHECK_STRING_OVERRUN(oind, ind, len, ch) \
if (ind >= len) \
{ \
oind = len; \
ch = 0; \
break; \
} \
else \
/* An expansion function that takes a string and a quoted flag and returns
a WORD_LIST *. Used as the type of the third argument to
expand_string_if_necessary(). */
typedef WORD_LIST *EXPFUNC __P((char *, int));
/* Process ID of the last command executed within command substitution. */
pid_t last_command_subst_pid = NO_PID;
pid_t current_command_subst_pid = NO_PID;
/* Variables used to keep track of the characters in IFS. */
SHELL_VAR *ifs_var;
char *ifs_value;
unsigned char ifs_cmap[UCHAR_MAX + 1];
int ifs_is_set, ifs_is_null;
#if defined (HANDLE_MULTIBYTE)
unsigned char ifs_firstc[MB_LEN_MAX];
size_t ifs_firstc_len;
#else
unsigned char ifs_firstc;
#endif
/* If non-zero, command substitution inherits the value of errexit option */
int inherit_errexit = 0;
/* Sentinel to tell when we are performing variable assignments preceding a
command name and putting them into the environment. Used to make sure
we use the temporary environment when looking up variable values. */
int assigning_in_environment;
/* Used to hold a list of variable assignments preceding a command. Global
so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
SIGCHLD trap and so it can be saved and restored by the trap handlers. */
WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
/* Tell the expansion functions to not longjmp back to top_level on fatal
errors. Enabled when doing completion and prompt string expansion. */
int no_longjmp_on_fatal_error = 0;
/* Non-zero means to allow unmatched globbed filenames to expand to
a null file. */
int allow_null_glob_expansion;
/* Non-zero means to throw an error when globbing fails to match anything. */
int fail_glob_expansion;
/* Extern functions and variables from different files. */
extern struct fd_bitmap *current_fds_to_close;
extern int wordexp_only;
#if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION)
extern PROCESS *last_procsub_child;
#endif
#if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
extern wchar_t *wcsdup __P((const wchar_t *));
#endif
#if 0
/* Variables to keep track of which words in an expanded word list (the
output of expand_word_list_internal) are the result of globbing
expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
(CURRENTLY UNUSED). */
char *glob_argv_flags;
static int glob_argv_flags_size;
#endif
static WORD_LIST *cached_quoted_dollar_at = 0;
/* Distinguished error values to return from expansion functions */
static WORD_LIST expand_word_error, expand_word_fatal;
static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
static char expand_param_error, expand_param_fatal, expand_param_unset;
static char extract_string_error, extract_string_fatal;
/* Set by expand_word_unsplit and several of the expand_string_XXX functions;
used to inhibit splitting and re-joining $* on $IFS, primarily when doing
assignment statements. The idea is that if we're in a context where this
is set, we're not going to be performing word splitting, so we use the same
rules to expand $* as we would if it appeared within double quotes. */
static int expand_no_split_dollar_star = 0;
/* A WORD_LIST of words to be expanded by expand_word_list_internal,
without any leading variable assignments. */
static WORD_LIST *garglist = (WORD_LIST *)NULL;
static char *quoted_substring __P((char *, int, int));
static int quoted_strlen __P((char *));
static char *quoted_strchr __P((char *, int, int));
static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
static WORD_LIST *expand_string_internal __P((char *, int));
static WORD_LIST *expand_string_leave_quoted __P((char *, int));
static WORD_LIST *expand_string_for_rhs __P((char *, int, int, int, int *, int *));
static WORD_LIST *expand_string_for_pat __P((char *, int, int *, int *));
static char *quote_escapes_internal __P((const char *, int));
static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
static WORD_LIST *list_dequote_escapes __P((WORD_LIST *));
static char *make_quoted_char __P((int));
static WORD_LIST *quote_list __P((WORD_LIST *));
static int unquoted_substring __P((char *, char *));
static int unquoted_member __P((int, char *));
#if defined (ARRAY_VARS)
static SHELL_VAR *do_compound_assignment __P((char *, char *, int));
#endif
static int do_assignment_internal __P((const WORD_DESC *, int));
static char *string_extract_verbatim __P((char *, size_t, int *, char *, int));
static char *string_extract __P((char *, int *, char *, int));
static char *string_extract_double_quoted __P((char *, int *, int));
static inline char *string_extract_single_quoted __P((char *, int *));
static inline int skip_single_quoted __P((const char *, size_t, int, int));
static int skip_double_quoted __P((char *, size_t, int, int));
static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
static char *extract_dollar_brace_string __P((char *, int *, int, int));
static int skip_matched_pair __P((const char *, int, int, int, int));
static char *pos_params __P((char *, int, int, int));
static unsigned char *mb_getcharlens __P((char *, int));
static char *remove_upattern __P((char *, char *, int));
#if defined (HANDLE_MULTIBYTE)
static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int));
#endif
static char *remove_pattern __P((char *, char *, int));
static int match_upattern __P((char *, char *, int, char **, char **));
#if defined (HANDLE_MULTIBYTE)
static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
#endif
static int match_pattern __P((char *, char *, int, char **, char **));
static int getpatspec __P((int, char *));
static char *getpattern __P((char *, int, int));
static char *variable_remove_pattern __P((char *, char *, int, int));
static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int));
static char *parameter_list_remove_pattern __P((int, char *, int, int));
#ifdef ARRAY_VARS
static char *array_remove_pattern __P((SHELL_VAR *, char *, int, char *, int));
#endif
static char *parameter_brace_remove_pattern __P((char *, char *, int, char *, int, int, int));
static char *string_var_assignment __P((SHELL_VAR *, char *));
#if defined (ARRAY_VARS)
static char *array_var_assignment __P((SHELL_VAR *, int, int));
#endif
static char *pos_params_assignment __P((WORD_LIST *, int, int));
static char *string_transform __P((int, SHELL_VAR *, char *));
static char *list_transform __P((int, SHELL_VAR *, WORD_LIST *, int, int));
static char *parameter_list_transform __P((int, int, int));
#if defined ARRAY_VARS
static char *array_transform __P((int, SHELL_VAR *, char *, int));
#endif
static char *parameter_brace_transform __P((char *, char *, int, char *, int, int, int, int));
static char *process_substitute __P((char *, int));
static char *read_comsub __P((int, int, int, int *));
#ifdef ARRAY_VARS
static arrayind_t array_length_reference __P((char *));
#endif
static int valid_brace_expansion_word __P((char *, int));
static int chk_atstar __P((char *, int, int *, int *));
static int chk_arithsub __P((const char *, int));
static WORD_DESC *parameter_brace_expand_word __P((char *, int, int, int, arrayind_t *));
static char *parameter_brace_find_indir __P((char *, int, int, int));
static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *));
static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int, int *, int *));
static void parameter_brace_expand_error __P((char *, char *, int));
static int valid_length_expression __P((char *));
static intmax_t parameter_brace_expand_length __P((char *));
static char *skiparith __P((char *, int));
static int verify_substring_values __P((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *));
static int get_var_and_type __P((char *, char *, arrayind_t, int, int, SHELL_VAR **, char **));
static char *mb_substring __P((char *, int, int));
static char *parameter_brace_substring __P((char *, char *, int, char *, int, int, int));
static int shouldexp_replacement __P((char *));
static char *pos_params_pat_subst __P((char *, char *, char *, int));
static char *parameter_brace_patsub __P((char *, char *, int, char *, int, int, int));
static char *pos_params_casemod __P((char *, char *, int, int));
static char *parameter_brace_casemod __P((char *, char *, int, int, char *, int, int, int));
static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int, int *, int *));
static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
static WORD_LIST *word_list_split __P((WORD_LIST *));
static void exp_jump_to_top_level __P((int));
static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
#ifdef BRACE_EXPANSION
static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
#endif
#if defined (ARRAY_VARS)
static int make_internal_declare __P((char *, char *, char *));
#endif
static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
/* **************************************************************** */
/* */
/* Utility Functions */
/* */
/* **************************************************************** */
#if defined (DEBUG)
void
dump_word_flags (flags)
int flags;
{
int f;
f = flags;
fprintf (stderr, "%d -> ", f);
if (f & W_ARRAYIND)
{
f &= ~W_ARRAYIND;
fprintf (stderr, "W_ARRAYIND%s", f ? "|" : "");
}
if (f & W_ASSIGNASSOC)
{
f &= ~W_ASSIGNASSOC;
fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : "");
}
if (f & W_ASSIGNARRAY)
{
f &= ~W_ASSIGNARRAY;
fprintf (stderr, "W_ASSIGNARRAY%s", f ? "|" : "");
}
if (f & W_SAWQUOTEDNULL)
{
f &= ~W_SAWQUOTEDNULL;
fprintf (stderr, "W_SAWQUOTEDNULL%s", f ? "|" : "");
}
if (f & W_NOPROCSUB)
{
f &= ~W_NOPROCSUB;
fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : "");
}
if (f & W_DQUOTE)
{
f &= ~W_DQUOTE;
fprintf (stderr, "W_DQUOTE%s", f ? "|" : "");
}
if (f & W_HASQUOTEDNULL)
{
f &= ~W_HASQUOTEDNULL;
fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : "");
}
if (f & W_ASSIGNARG)
{
f &= ~W_ASSIGNARG;
fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : "");
}
if (f & W_ASSNBLTIN)
{
f &= ~W_ASSNBLTIN;
fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : "");
}
if (f & W_ASSNGLOBAL)
{
f &= ~W_ASSNGLOBAL;
fprintf (stderr, "W_ASSNGLOBAL%s", f ? "|" : "");
}
if (f & W_COMPASSIGN)
{
f &= ~W_COMPASSIGN;
fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : "");
}
if (f & W_NOEXPAND)
{
f &= ~W_NOEXPAND;
fprintf (stderr, "W_NOEXPAND%s", f ? "|" : "");
}
if (f & W_ITILDE)
{
f &= ~W_ITILDE;
fprintf (stderr, "W_ITILDE%s", f ? "|" : "");
}
if (f & W_NOTILDE)
{
f &= ~W_NOTILDE;
fprintf (stderr, "W_NOTILDE%s", f ? "|" : "");
}
if (f & W_ASSIGNRHS)
{
f &= ~W_ASSIGNRHS;
fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : "");
}
if (f & W_NOASSNTILDE)
{
f &= ~W_NOASSNTILDE;
fprintf (stderr, "W_NOASSNTILDE%s", f ? "|" : "");
}
if (f & W_NOCOMSUB)
{
f &= ~W_NOCOMSUB;
fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : "");
}
if (f & W_DOLLARSTAR)
{
f &= ~W_DOLLARSTAR;
fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : "");
}
if (f & W_DOLLARAT)
{
f &= ~W_DOLLARAT;
fprintf (stderr, "W_DOLLARAT%s", f ? "|" : "");
}
if (f & W_TILDEEXP)
{
f &= ~W_TILDEEXP;
fprintf (stderr, "W_TILDEEXP%s", f ? "|" : "");
}
if (f & W_NOSPLIT2)
{
f &= ~W_NOSPLIT2;
fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : "");
}
if (f & W_NOSPLIT)
{
f &= ~W_NOSPLIT;
fprintf (stderr, "W_NOSPLIT%s", f ? "|" : "");
}
if (f & W_NOBRACE)
{
f &= ~W_NOBRACE;
fprintf (stderr, "W_NOBRACE%s", f ? "|" : "");
}
if (f & W_NOGLOB)
{
f &= ~W_NOGLOB;
fprintf (stderr, "W_NOGLOB%s", f ? "|" : "");
}
if (f & W_SPLITSPACE)
{
f &= ~W_SPLITSPACE;
fprintf (stderr, "W_SPLITSPACE%s", f ? "|" : "");
}
if (f & W_ASSIGNMENT)
{
f &= ~W_ASSIGNMENT;
fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : "");
}
if (f & W_QUOTED)
{
f &= ~W_QUOTED;
fprintf (stderr, "W_QUOTED%s", f ? "|" : "");
}
if (f & W_HASDOLLAR)
{
f &= ~W_HASDOLLAR;
fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : "");
}
if (f & W_COMPLETE)
{
f &= ~W_COMPLETE;
fprintf (stderr, "W_COMPLETE%s", f ? "|" : "");
}
if (f & W_CHKLOCAL)
{
f &= ~W_CHKLOCAL;
fprintf (stderr, "W_CHKLOCAL%s", f ? "|" : "");
}
fprintf (stderr, "\n");
fflush (stderr);
}
#endif
#ifdef INCLUDE_UNUSED
static char *
quoted_substring (string, start, end)
char *string;
int start, end;
{
register int len, l;
register char *result, *s, *r;
len = end - start;
/* Move to string[start], skipping quoted characters. */
for (s = string, l = 0; *s && l < start; )
{
if (*s == CTLESC)
{
s++;
continue;
}
l++;
if (*s == 0)
break;
}
r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
/* Copy LEN characters, including quote characters. */
s = string + l;
for (l = 0; l < len; s++)
{
if (*s == CTLESC)
*r++ = *s++;
*r++ = *s;
l++;
if (*s == 0)
break;
}
*r = '\0';
return result;
}
#endif
#ifdef INCLUDE_UNUSED
/* Return the length of S, skipping over quoted characters */
static int
quoted_strlen (s)
char *s;
{
register char *p;
int i;
i = 0;
for (p = s; *p; p++)
{
if (*p == CTLESC)
{
p++;
if (*p == 0)
return (i + 1);
}
i++;
}
return i;
}
#endif
#ifdef INCLUDE_UNUSED
/* Find the first occurrence of character C in string S, obeying shell
quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
escaped with CTLESC are skipped. */
static char *
quoted_strchr (s, c, flags)
char *s;
int c, flags;
{
register char *p;
for (p = s; *p; p++)
{
if (((flags & ST_BACKSL) && *p == '\\')
|| ((flags & ST_CTLESC) && *p == CTLESC))
{
p++;
if (*p == '\0')
return ((char *)NULL);
continue;
}
else if (*p == c)
return p;
}
return ((char *)NULL);
}
/* Return 1 if CHARACTER appears in an unquoted portion of
STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
static int
unquoted_member (character, string)
int character;
char *string;
{
size_t slen;
int sindex, c;
DECLARE_MBSTATE;
slen = strlen (string);
sindex = 0;
while (c = string[sindex])
{
if (c == character)
return (1);
switch (c)
{
default:
ADVANCE_CHAR (string, slen, sindex);
break;
case '\\':
sindex++;
if (string[sindex])
ADVANCE_CHAR (string, slen, sindex);
break;
case '\'':
sindex = skip_single_quoted (string, slen, ++sindex, 0);
break;
case '"':
sindex = skip_double_quoted (string, slen, ++sindex, 0);
break;
}
}
return (0);
}
/* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
static int
unquoted_substring (substr, string)
char *substr, *string;
{
size_t slen;
int sindex, c, sublen;
DECLARE_MBSTATE;
if (substr == 0 || *substr == '\0')
return (0);
slen = strlen (string);
sublen = strlen (substr);
for (sindex = 0; c = string[sindex]; )
{
if (STREQN (string + sindex, substr, sublen))
return (1);
switch (c)
{
case '\\':
sindex++;
if (string[sindex])
ADVANCE_CHAR (string, slen, sindex);
break;
case '\'':
sindex = skip_single_quoted (string, slen, ++sindex, 0);
break;
case '"':
sindex = skip_double_quoted (string, slen, ++sindex, 0);
break;
default:
ADVANCE_CHAR (string, slen, sindex);
break;
}
}
return (0);
}
#endif
/* Most of the substitutions must be done in parallel. In order
to avoid using tons of unclear goto's, I have some functions
for manipulating malloc'ed strings. They all take INDX, a
pointer to an integer which is the offset into the string
where manipulation is taking place. They also take SIZE, a
pointer to an integer which is the current length of the
character array for this string. */
/* Append SOURCE to TARGET at INDEX. SIZE is the current amount
of space allocated to TARGET. SOURCE can be NULL, in which
case nothing happens. Gets rid of SOURCE by freeing it.
Returns TARGET in case the location has changed. */
INLINE char *
sub_append_string (source, target, indx, size)
char *source, *target;
int *indx;
size_t *size;
{
if (source)
{
int n;
size_t srclen;
srclen = STRLEN (source);
if (srclen >= (int)(*size - *indx))
{
n = srclen + *indx;
n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
target = (char *)xrealloc (target, (*size = n));
}
FASTCOPY (source, target + *indx, srclen);
*indx += srclen;
target[*indx] = '\0';
free (source);
}
return (target);
}
#if 0
/* UNUSED */
/* Append the textual representation of NUMBER to TARGET.
INDX and SIZE are as in SUB_APPEND_STRING. */
char *
sub_append_number (number, target, indx, size)
intmax_t number;
char *target;
int *indx;
size_t *size;
{
char *temp;
temp = itos (number);
return (sub_append_string (temp, target, indx, size));
}
#endif
/* Extract a substring from STRING, starting at SINDEX and ending with
one of the characters in CHARLIST. Don't make the ending character
part of the string. Leave SINDEX pointing at the ending character.
Understand about backslashes in the string. If (flags & SX_VARNAME)
is non-zero, and array variables have been compiled into the shell,
everything between a `[' and a corresponding `]' is skipped over.
If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
contain a closing character from CHARLIST. */
static char *
string_extract (string, sindex, charlist, flags)
char *string;
int *sindex;
char *charlist;
int flags;
{
register int c, i;
int found;
size_t slen;
char *temp;
DECLARE_MBSTATE;
slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
i = *sindex;
found = 0;
while (c = string[i])
{
if (c == '\\')
{
if (string[i + 1])
i++;
else
break;
}
#if defined (ARRAY_VARS)
else if ((flags & SX_VARNAME) && c == LBRACK)
{
int ni;
/* If this is an array subscript, skip over it and continue. */
ni = skipsubscript (string, i, 0);
if (string[ni] == RBRACK)
i = ni;
}
#endif
else if (MEMBER (c, charlist))
{
found = 1;
break;
}
ADVANCE_CHAR (string, slen, i);
}
/* If we had to have a matching delimiter and didn't find one, return an
error and let the caller deal with it. */
if ((flags & SX_REQMATCH) && found == 0)
{
*sindex = i;
return (&extract_string_error);
}
temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
*sindex = i;
return (temp);
}
/* Extract the contents of STRING as if it is enclosed in double quotes.
SINDEX, when passed in, is the offset of the character immediately
following the opening double quote; on exit, SINDEX is left pointing after
the closing double quote. If STRIPDQ is non-zero, unquoted double
quotes are stripped and the string is terminated by a null byte.
Backslashes between the embedded double quotes are processed. If STRIPDQ
is zero, an unquoted `"' terminates the string. */
static char *
string_extract_double_quoted (string, sindex, flags)
char *string;
int *sindex, flags;
{
size_t slen;
char *send;
int j, i, t;
unsigned char c;
char *temp, *ret; /* The new string we return. */
int pass_next, backquote, si; /* State variables for the machine. */
int dquote;
int stripdq;
DECLARE_MBSTATE;
slen = strlen (string + *sindex) + *sindex;
send = string + slen;
stripdq = (flags & SX_STRIPDQ);
pass_next = backquote = dquote = 0;
temp = (char *)xmalloc (1 + slen - *sindex);
j = 0;
i = *sindex;
while (c = string[i])
{
/* Process a character that was quoted by a backslash. */
if (pass_next)
{
/* XXX - take another look at this in light of Interp 221 */
/* Posix.2 sez:
``The backslash shall retain its special meaning as an escape
character only when followed by one of the characters:
$ ` " \ <newline>''.
If STRIPDQ is zero, we handle the double quotes here and let
expand_word_internal handle the rest. If STRIPDQ is non-zero,
we have already been through one round of backslash stripping,
and want to strip these backslashes only if DQUOTE is non-zero,
indicating that we are inside an embedded double-quoted string. */
/* If we are in an embedded quoted string, then don't strip
backslashes before characters for which the backslash
retains its special meaning, but remove backslashes in
front of other characters. If we are not in an
embedded quoted string, don't strip backslashes at all.
This mess is necessary because the string was already
surrounded by double quotes (and sh has some really weird
quoting rules).
The returned string will be run through expansion as if
it were double-quoted. */
if ((stripdq == 0 && c != '"') ||
(stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
temp[j++] = '\\';
pass_next = 0;
add_one_character:
COPY_CHAR_I (temp, j, string, send, i);
continue;
}
/* A backslash protects the next character. The code just above
handles preserving the backslash in front of any character but
a double quote. */
if (c == '\\')
{
pass_next++;
i++;
continue;
}
/* Inside backquotes, ``the portion of the quoted string from the
initial backquote and the characters up to the next backquote
that is not preceded by a backslash, having escape characters
removed, defines that command''. */
if (backquote)
{
if (c == '`')
backquote = 0;
temp[j++] = c; /* COPY_CHAR_I? */
i++;
continue;
}
if (c == '`')
{
temp[j++] = c;
backquote++;
i++;
continue;
}
/* Pass everything between `$(' and the matching `)' or a quoted
${ ... } pair through according to the Posix.2 specification. */
if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
{
int free_ret = 1;
si = i + 2;
if (string[i + 1] == LPAREN)
ret = extract_command_subst (string, &si, (flags & SX_COMPLETE));
else
ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0);
temp[j++] = '$';
temp[j++] = string[i + 1];
/* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
is set. */
if (ret == 0 && no_longjmp_on_fatal_error)
{
free_ret = 0;
ret = string + i + 2;
}
/* XXX - CHECK_STRING_OVERRUN here? */
for (t = 0; ret[t]; t++, j++)
temp[j] = ret[t];
temp[j] = string[si];
if (string[si])
{
j++;
i = si + 1;
}
else
i = si;
if (free_ret)
free (ret);
continue;
}
/* Add any character but a double quote to the quoted string we're
accumulating. */
if (c != '"')
goto add_one_character;
/* c == '"' */
if (stripdq)
{
dquote ^= 1;
i++;
continue;
}
break;
}
temp[j] = '\0';
/* Point to after the closing quote. */
if (c)
i++;
*sindex = i;
return (temp);
}
/* This should really be another option to string_extract_double_quoted. */
static int
skip_double_quoted (string, slen, sind, flags)
char *string;
size_t slen;
int sind;
int flags;
{
int c, i;
char *ret;
int pass_next, backquote, si;
DECLARE_MBSTATE;
pass_next = backquote = 0;
i = sind;
while (c = string[i])
{
if (pass_next)
{
pass_next = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '\\')
{
pass_next++;
i++;
continue;
}
else if (backquote)
{
if (c == '`')
backquote = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '`')
{
backquote++;
i++;
continue;
}
else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
{
si = i + 2;
if (string[i + 1] == LPAREN)
ret = extract_command_subst (string, &si, SX_NOALLOC|(flags&SX_COMPLETE));
else
ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, SX_NOALLOC);
/* These can consume the entire string if they are unterminated */
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
else if (c != '"')
{
ADVANCE_CHAR (string, slen, i);
continue;
}
else
break;
}
if (c)
i++;
return (i);
}
/* Extract the contents of STRING as if it is enclosed in single quotes.
SINDEX, when passed in, is the offset of the character immediately
following the opening single quote; on exit, SINDEX is left pointing after
the closing single quote. */
static inline char *
string_extract_single_quoted (string, sindex)
char *string;
int *sindex;
{
register int i;
size_t slen;
char *t;
DECLARE_MBSTATE;
/* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
i = *sindex;
while (string[i] && string[i] != '\'')
ADVANCE_CHAR (string, slen, i);
t = substring (string, *sindex, i);
if (string[i])
i++;
*sindex = i;
return (t);
}
/* Skip over a single-quoted string. We overload the SX_COMPLETE flag to mean
that we are splitting out words for completion and have encountered a $'...'
string, which allows backslash-escaped single quotes. */
static inline int
skip_single_quoted (string, slen, sind, flags)
const char *string;
size_t slen;
int sind;
int flags;
{
register int c;
DECLARE_MBSTATE;
c = sind;
while (string[c] && string[c] != '\'')
{
if ((flags & SX_COMPLETE) && string[c] == '\\' && string[c+1] == '\'' && string[c+2])
ADVANCE_CHAR (string, slen, c);
ADVANCE_CHAR (string, slen, c);
}
if (string[c])
c++;
return c;
}
/* Just like string_extract, but doesn't hack backslashes or any of
that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
static char *
string_extract_verbatim (string, slen, sindex, charlist, flags)
char *string;
size_t slen;
int *sindex;
char *charlist;
int flags;
{
register int i;
#if defined (HANDLE_MULTIBYTE)
wchar_t *wcharlist;
#endif
int c;
char *temp;
DECLARE_MBSTATE;
if ((flags & SX_NOCTLESC) && charlist[0] == '\'' && charlist[1] == '\0')
{
temp = string_extract_single_quoted (string, sindex);
--*sindex; /* leave *sindex at separator character */
return temp;
}
i = *sindex;
#if defined (HANDLE_MULTIBYTE)
wcharlist = 0;
#endif
while (c = string[i])
{
#if defined (HANDLE_MULTIBYTE)
size_t mblength;
#endif
if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
{
i += 2;
CHECK_STRING_OVERRUN (i, i, slen, c);
continue;
}
/* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
through, to protect the CTLNULs from later calls to
remove_quoted_nulls. */
else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL)
{
i += 2;
CHECK_STRING_OVERRUN (i, i, slen, c);
continue;
}
#if defined (HANDLE_MULTIBYTE)
if (locale_utf8locale && slen > i && UTF8_SINGLEBYTE (string[i]))
mblength = (string[i] != 0) ? 1 : 0;
else
mblength = MBLEN (string + i, slen - i);
if (mblength > 1)
{
wchar_t wc;
mblength = mbtowc (&wc, string + i, slen - i);
if (MB_INVALIDCH (mblength))
{
if (MEMBER (c, charlist))
break;
}
else
{
if (wcharlist == 0)
{
size_t len;
len = mbstowcs (wcharlist, charlist, 0);
if (len == -1)
len = 0;
wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1));
mbstowcs (wcharlist, charlist, len + 1);
}
if (wcschr (wcharlist, wc))
break;
}
}
else
#endif
if (MEMBER (c, charlist))
break;
ADVANCE_CHAR (string, slen, i);
}
#if defined (HANDLE_MULTIBYTE)
FREE (wcharlist);
#endif
temp = substring (string, *sindex, i);
*sindex = i;
return (temp);
}
/* Extract the $( construct in STRING, and return a new string.
Start extracting at (SINDEX) as if we had just seen "$(".
Make (SINDEX) get the position of the matching ")". )
XFLAGS is additional flags to pass to other extraction functions. */
char *
extract_command_subst (string, sindex, xflags)
char *string;
int *sindex;
int xflags;
{
char *ret;
if (string[*sindex] == LPAREN || (xflags & SX_COMPLETE))
return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/
else
{
xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
ret = xparse_dolparen (string, string+*sindex, sindex, xflags);
return ret;
}
}
/* Extract the $[ construct in STRING, and return a new string. (])
Start extracting at (SINDEX) as if we had just seen "$[".
Make (SINDEX) get the position of the matching "]". */
char *
extract_arithmetic_subst (string, sindex)
char *string;
int *sindex;
{
return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
}
#if defined (PROCESS_SUBSTITUTION)
/* Extract the <( or >( construct in STRING, and return a new string.
Start extracting at (SINDEX) as if we had just seen "<(".
Make (SINDEX) get the position of the matching ")". */ /*))*/
char *
extract_process_subst (string, starter, sindex, xflags)
char *string;
char *starter;
int *sindex;
int xflags;
{
#if 0
/* XXX - check xflags&SX_COMPLETE here? */
return (extract_delimited_string (string, sindex, starter, "(", ")", SX_COMMAND));
#else
xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
return (xparse_dolparen (string, string+*sindex, sindex, xflags));
#endif
}
#endif /* PROCESS_SUBSTITUTION */
#if defined (ARRAY_VARS)
/* This can be fooled by unquoted right parens in the passed string. If
each caller verifies that the last character in STRING is a right paren,
we don't even need to call extract_delimited_string. */
char *
extract_array_assignment_list (string, sindex)
char *string;
int *sindex;
{
int slen;
char *ret;
slen = strlen (string);
if (string[slen - 1] == RPAREN)
{
ret = substring (string, *sindex, slen - 1);
*sindex = slen - 1;
return ret;
}
return 0;
}
#endif
/* Extract and create a new string from the contents of STRING, a
character string delimited with OPENER and CLOSER. SINDEX is
the address of an int describing the current offset in STRING;
it should point to just after the first OPENER found. On exit,
SINDEX gets the position of the last character of the matching CLOSER.
If OPENER is more than a single character, ALT_OPENER, if non-null,
contains a character string that can also match CLOSER and thus
needs to be skipped. */
static char *
extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
char *string;
int *sindex;
char *opener, *alt_opener, *closer;
int flags;
{
int i, c, si;
size_t slen;
char *t, *result;
int pass_character, nesting_level, in_comment;
int len_closer, len_opener, len_alt_opener;
DECLARE_MBSTATE;
slen = strlen (string + *sindex) + *sindex;
len_opener = STRLEN (opener);
len_alt_opener = STRLEN (alt_opener);
len_closer = STRLEN (closer);
pass_character = in_comment = 0;
nesting_level = 1;
i = *sindex;
while (nesting_level)
{
c = string[i];
/* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond
the end of the string, catch it and cut the loop. */
if (i > slen)
{
i = slen;
c = string[i = slen];
break;
}
if (c == 0)
break;
if (in_comment)
{
if (c == '\n')
in_comment = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
if (pass_character) /* previous char was backslash */
{
pass_character = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
/* Not exactly right yet; should handle shell metacharacters and
multibyte characters, too. See COMMENT_BEGIN define in parse.y */
if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1])))
{
in_comment = 1;
ADVANCE_CHAR (string, slen, i);
continue;
}
if (c == CTLESC || c == '\\')
{
pass_character++;
i++;
continue;
}
/* Process a nested command substitution, but only if we're parsing an
arithmetic substitution. */
if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN)
{
si = i + 2;
t = extract_command_subst (string, &si, flags|SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* Process a nested OPENER. */
if (STREQN (string + i, opener, len_opener))
{
si = i + len_opener;
t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* Process a nested ALT_OPENER */
if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
{
si = i + len_alt_opener;
t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* If the current substring terminates the delimited string, decrement
the nesting level. */
if (STREQN (string + i, closer, len_closer))
{
i += len_closer - 1; /* move to last byte of the closer */
nesting_level--;
if (nesting_level == 0)
break;
}
/* Pass old-style command substitution through verbatim. */
if (c == '`')
{
si = i + 1;
t = string_extract (string, &si, "`", flags|SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* Pass single-quoted and double-quoted strings through verbatim. */
if (c == '\'' || c == '"')
{
si = i + 1;
i = (c == '\'') ? skip_single_quoted (string, slen, si, 0)
: skip_double_quoted (string, slen, si, 0);
continue;
}
/* move past this character, which was not special. */
ADVANCE_CHAR (string, slen, i);
}
if (c == 0 && nesting_level)
{
if (no_longjmp_on_fatal_error == 0)
{
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
exp_jump_to_top_level (DISCARD);
}
else
{
*sindex = i;
return (char *)NULL;
}
}
si = i - *sindex - len_closer + 1;
if (flags & SX_NOALLOC)
result = (char *)NULL;
else
{
result = (char *)xmalloc (1 + si);
strncpy (result, string + *sindex, si);
result[si] = '\0';
}
*sindex = i;
return (result);
}
/* Extract a parameter expansion expression within ${ and } from STRING.
Obey the Posix.2 rules for finding the ending `}': count braces while
skipping over enclosed quoted strings and command substitutions.
SINDEX is the address of an int describing the current offset in STRING;
it should point to just after the first `{' found. On exit, SINDEX
gets the position of the matching `}'. QUOTED is non-zero if this
occurs inside double quotes. */
/* XXX -- this is very similar to extract_delimited_string -- XXX */
static char *
extract_dollar_brace_string (string, sindex, quoted, flags)
char *string;
int *sindex, quoted, flags;
{
register int i, c;
size_t slen;
int pass_character, nesting_level, si, dolbrace_state;
char *result, *t;
DECLARE_MBSTATE;
pass_character = 0;
nesting_level = 1;
slen = strlen (string + *sindex) + *sindex;
/* The handling of dolbrace_state needs to agree with the code in parse.y:
parse_matched_pair(). The different initial value is to handle the
case where this function is called to parse the word in
${param op word} (SX_WORD). */
dolbrace_state = (flags & SX_WORD) ? DOLBRACE_WORD : DOLBRACE_PARAM;
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && (flags & SX_POSIXEXP))
dolbrace_state = DOLBRACE_QUOTE;
i = *sindex;
while (c = string[i])
{
if (pass_character)
{
pass_character = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
/* CTLESCs and backslashes quote the next character. */
if (c == CTLESC || c == '\\')
{
pass_character++;
i++;
continue;
}
if (string[i] == '$' && string[i+1] == LBRACE)
{
nesting_level++;
i += 2;
continue;
}
if (c == RBRACE)
{
nesting_level--;
if (nesting_level == 0)
break;
i++;
continue;
}
/* Pass the contents of old-style command substitutions through
verbatim. */
if (c == '`')
{
si = i + 1;
t = string_extract (string, &si, "`", flags|SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* Pass the contents of new-style command substitutions and
arithmetic substitutions through verbatim. */
if (string[i] == '$' && string[i+1] == LPAREN)
{
si = i + 2;
t = extract_command_subst (string, &si, flags|SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
#if defined (PROCESS_SUBSTITUTION)
/* Technically this should only work at the start of a word */
if ((string[i] == '<' || string[i] == '>') && string[i+1] == LPAREN)
{
si = i + 2;
t = extract_process_subst (string, (string[i] == '<' ? "<(" : ">)"), &si, flags|SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
#endif
/* Pass the contents of double-quoted strings through verbatim. */
if (c == '"')
{
si = i + 1;
i = skip_double_quoted (string, slen, si, 0);
/* skip_XXX_quoted leaves index one past close quote */
continue;
}
if (c == '\'')
{
/*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/
if (posixly_correct && shell_compatibility_level > 42 && dolbrace_state != DOLBRACE_QUOTE && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
ADVANCE_CHAR (string, slen, i);
else
{
si = i + 1;
i = skip_single_quoted (string, slen, si, 0);
}
continue;
}
#if defined (ARRAY_VARS)
if (c == LBRACK && dolbrace_state == DOLBRACE_PARAM)
{
si = skipsubscript (string, i, 0);
CHECK_STRING_OVERRUN (i, si, slen, c);
if (string[si] == RBRACK)
c = string[i = si];
}
#endif
/* move past this character, which was not special. */
ADVANCE_CHAR (string, slen, i);
/* This logic must agree with parse.y:parse_matched_pair, since they
share the same defines. */
if (dolbrace_state == DOLBRACE_PARAM && c == '%' && (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE;
else if (dolbrace_state == DOLBRACE_PARAM && c == '#' && (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE;
else if (dolbrace_state == DOLBRACE_PARAM && c == '/' && (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
else if (dolbrace_state == DOLBRACE_PARAM && c == '^' && (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE;
else if (dolbrace_state == DOLBRACE_PARAM && c == ',' && (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE;
/* This is intended to handle all of the [:]op expansions and the substring/
length/pattern removal/pattern substitution expansions. */
else if (dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", c) != 0)
dolbrace_state = DOLBRACE_OP;
else if (dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", c) == 0)
dolbrace_state = DOLBRACE_WORD;
}
if (c == 0 && nesting_level)
{
if (no_longjmp_on_fatal_error == 0)
{ /* { */
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
exp_jump_to_top_level (DISCARD);
}
else
{
*sindex = i;
return ((char *)NULL);
}
}
result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
*sindex = i;
return (result);
}
/* Remove backslashes which are quoting backquotes from STRING. Modifies
STRING, and returns a pointer to it. */
char *
de_backslash (string)
char *string;
{
register size_t slen;
register int i, j, prev_i;
DECLARE_MBSTATE;
slen = strlen (string);
i = j = 0;
/* Loop copying string[i] to string[j], i >= j. */
while (i < slen)
{
if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
string[i + 1] == '$'))
i++;
prev_i = i;
ADVANCE_CHAR (string, slen, i);
if (j < prev_i)
do string[j++] = string[prev_i++]; while (prev_i < i);
else
j = i;
}
string[j] = '\0';
return (string);
}
#if 0
/*UNUSED*/
/* Replace instances of \! in a string with !. */
void
unquote_bang (string)
char *string;
{
register int i, j;
register char *temp;
temp = (char *)xmalloc (1 + strlen (string));
for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
{
if (string[i] == '\\' && string[i + 1] == '!')
{
temp[j] = '!';
i++;
}
}
strcpy (string, temp);
free (temp);
}
#endif
#define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0)
/* This function assumes s[i] == open; returns with s[ret] == close; used to
parse array subscripts. FLAGS & 1 means to not attempt to skip over
matched pairs of quotes or backquotes, or skip word expansions; it is
intended to be used after expansion has been performed and during final
assignment parsing (see arrayfunc.c:assign_compound_array_list()) or
during execution by a builtin which has already undergone word expansion. */
static int
skip_matched_pair (string, start, open, close, flags)
const char *string;
int start, open, close, flags;
{
int i, pass_next, backq, si, c, count, oldjmp;
size_t slen;
char *temp, *ss;
DECLARE_MBSTATE;
slen = strlen (string + start) + start;
oldjmp = no_longjmp_on_fatal_error;
no_longjmp_on_fatal_error = 1;
i = start + 1; /* skip over leading bracket */
count = 1;
pass_next = backq = 0;
ss = (char *)string;
while (c = string[i])
{
if (pass_next)
{
pass_next = 0;
if (c == 0)
CQ_RETURN(i);
ADVANCE_CHAR (string, slen, i);
continue;
}
else if ((flags & 1) == 0 && c == '\\')
{
pass_next = 1;
i++;
continue;
}
else if (backq)
{
if (c == '`')
backq = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if ((flags & 1) == 0 && c == '`')
{
backq = 1;
i++;
continue;
}
else if ((flags & 1) == 0 && c == open)
{
count++;
i++;
continue;
}
else if (c == close)
{
count--;
if (count == 0)
break;
i++;
continue;
}
else if ((flags & 1) == 0 && (c == '\'' || c == '"'))
{
i = (c == '\'') ? skip_single_quoted (ss, slen, ++i, 0)
: skip_double_quoted (ss, slen, ++i, 0);
/* no increment, the skip functions increment past the closing quote. */
}
else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
{
si = i + 2;
if (string[si] == '\0')
CQ_RETURN(si);
/* XXX - extract_command_subst here? */
if (string[i+1] == LPAREN)
temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
else
temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si;
if (string[i] == '\0') /* don't increment i past EOS in loop */
break;
i++;
continue;
}
else
ADVANCE_CHAR (string, slen, i);
}
CQ_RETURN(i);
}
#if defined (ARRAY_VARS)
/* Flags has 1 as a reserved value, since skip_matched_pair uses it for
skipping over quoted strings and taking the first instance of the
closing character. */
int
skipsubscript (string, start, flags)
const char *string;
int start, flags;
{
return (skip_matched_pair (string, start, '[', ']', flags));
}
#endif
/* Skip characters in STRING until we find a character in DELIMS, and return
the index of that character. START is the index into string at which we
begin. This is similar in spirit to strpbrk, but it returns an index into
STRING and takes a starting index. This little piece of code knows quite
a lot of shell syntax. It's very similar to skip_double_quoted and other
functions of that ilk. */
int
skip_to_delim (string, start, delims, flags)
char *string;
int start;
char *delims;
int flags;
{
int i, pass_next, backq, dquote, si, c, oldjmp;
int invert, skipquote, skipcmd, noprocsub, completeflag;
int arithexp, skipcol;
size_t slen;
char *temp, open[3];
DECLARE_MBSTATE;
slen = strlen (string + start) + start;
oldjmp = no_longjmp_on_fatal_error;
if (flags & SD_NOJMP)
no_longjmp_on_fatal_error = 1;
invert = (flags & SD_INVERT);
skipcmd = (flags & SD_NOSKIPCMD) == 0;
noprocsub = (flags & SD_NOPROCSUB);
completeflag = (flags & SD_COMPLETE) ? SX_COMPLETE : 0;
arithexp = (flags & SD_ARITHEXP);
skipcol = 0;
i = start;
pass_next = backq = dquote = 0;
while (c = string[i])
{
/* If this is non-zero, we should not let quote characters be delimiters
and the current character is a single or double quote. We should not
test whether or not it's a delimiter until after we skip single- or
double-quoted strings. */
skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"'));
if (pass_next)
{
pass_next = 0;
if (c == 0)
CQ_RETURN(i);
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '\\')
{
pass_next = 1;
i++;
continue;
}
else if (backq)
{
if (c == '`')
backq = 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '`')
{
backq = 1;
i++;
continue;
}
else if (arithexp && skipcol && c == ':')
{
skipcol--;
i++;
continue;
}
else if (arithexp && c == '?')
{
skipcol++;
i++;
continue;
}
else if (skipquote == 0 && invert == 0 && member (c, delims))
break;
/* the usual case is to use skip_xxx_quoted, but we don't skip over double
quoted strings when looking for the history expansion character as a
delimiter. */
/* special case for programmable completion which takes place before
parser converts backslash-escaped single quotes between $'...' to
`regular' single-quoted strings. */
else if (completeflag && i > 0 && string[i-1] == '$' && c == '\'')
i = skip_single_quoted (string, slen, ++i, SX_COMPLETE);
else if (c == '\'')
i = skip_single_quoted (string, slen, ++i, 0);
else if (c == '"')
i = skip_double_quoted (string, slen, ++i, completeflag);
else if (c == LPAREN && arithexp)
{
si = i + 1;
if (string[si] == '\0')
CQ_RETURN(si);
temp = extract_delimited_string (string, &si, "(", "(", ")", SX_NOALLOC); /* ) */
i = si;
if (string[i] == '\0') /* don't increment i past EOS in loop */
break;
i++;
continue;
}
else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE))
{
si = i + 2;
if (string[si] == '\0')
CQ_RETURN(si);
if (string[i+1] == LPAREN)
temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
else
temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC);
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si;
if (string[i] == '\0') /* don't increment i past EOS in loop */
break;
i++;
continue;
}
#if defined (PROCESS_SUBSTITUTION)
else if (skipcmd && noprocsub == 0 && (c == '<' || c == '>') && string[i+1] == LPAREN)
{
si = i + 2;
if (string[si] == '\0')
CQ_RETURN(si);
temp = extract_delimited_string (string, &si, (c == '<') ? "<(" : ">(", "(", ")", SX_COMMAND|SX_NOALLOC); /* )) */
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si;
if (string[i] == '\0')
break;
i++;
continue;
}
#endif /* PROCESS_SUBSTITUTION */
#if defined (EXTENDED_GLOB)
else if ((flags & SD_EXTGLOB) && extended_glob && string[i+1] == LPAREN && member (c, "?*+!@"))
{
si = i + 2;
if (string[si] == '\0')
CQ_RETURN(si);
open[0] = c;
open[1] = LPAREN;
open[2] = '\0';
temp = extract_delimited_string (string, &si, open, "(", ")", SX_NOALLOC); /* ) */
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si;
if (string[i] == '\0') /* don't increment i past EOS in loop */
break;
i++;
continue;
}
#endif
else if ((flags & SD_GLOB) && c == LBRACK)
{
si = i + 1;
if (string[si] == '\0')
CQ_RETURN(si);
temp = extract_delimited_string (string, &si, "[", "[", "]", SX_NOALLOC); /* ] */
i = si;
if (string[i] == '\0') /* don't increment i past EOS in loop */
break;
i++;
continue;
}
else if ((skipquote || invert) && (member (c, delims) == 0))
break;
else
ADVANCE_CHAR (string, slen, i);
}
CQ_RETURN(i);
}
#if defined (BANG_HISTORY)
/* Skip to the history expansion character (delims[0]), paying attention to
quoted strings and command and process substitution. This is a stripped-
down version of skip_to_delims. The essential difference is that this
resets the quoting state when starting a command substitution */
int
skip_to_histexp (string, start, delims, flags)
char *string;
int start;
char *delims;
int flags;
{
int i, pass_next, backq, dquote, c, oldjmp;
int histexp_comsub, histexp_backq, old_dquote;
size_t slen;
DECLARE_MBSTATE;
slen = strlen (string + start) + start;
oldjmp = no_longjmp_on_fatal_error;
if (flags & SD_NOJMP)
no_longjmp_on_fatal_error = 1;
histexp_comsub = histexp_backq = old_dquote = 0;
i = start;
pass_next = backq = dquote = 0;
while (c = string[i])
{
if (pass_next)
{
pass_next = 0;
if (c == 0)
CQ_RETURN(i);
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '\\')
{
pass_next = 1;
i++;
continue;
}
else if (backq && c == '`')
{
backq = 0;
histexp_backq--;
dquote = old_dquote;
i++;
continue;
}
else if (c == '`')
{
backq = 1;
histexp_backq++;
old_dquote = dquote; /* simple - one level for now */
dquote = 0;
i++;
continue;
}
/* When in double quotes, act as if the double quote is a member of
history_no_expand_chars, like the history library does */
else if (dquote && c == delims[0] && string[i+1] == '"')
{
i++;
continue;
}
else if (c == delims[0])
break;
/* the usual case is to use skip_xxx_quoted, but we don't skip over double
quoted strings when looking for the history expansion character as a
delimiter. */
else if (dquote && c == '\'')
{
i++;
continue;
}
else if (c == '\'')
i = skip_single_quoted (string, slen, ++i, 0);
/* The posixly_correct test makes posix-mode shells allow double quotes
to quote the history expansion character */
else if (posixly_correct == 0 && c == '"')
{
dquote = 1 - dquote;
i++;
continue;
}
else if (c == '"')
i = skip_double_quoted (string, slen, ++i, 0);
#if defined (PROCESS_SUBSTITUTION)
else if ((c == '$' || c == '<' || c == '>') && string[i+1] == LPAREN && string[i+2] != LPAREN)
#else
else if (c == '$' && string[i+1] == LPAREN && string[i+2] != LPAREN)
#endif
{
if (string[i+2] == '\0')
CQ_RETURN(i+2);
i += 2;
histexp_comsub++;
old_dquote = dquote;
dquote = 0;
}
else if (histexp_comsub && c == RPAREN)
{
histexp_comsub--;
dquote = old_dquote;
i++;
continue;
}
else if (backq) /* placeholder */
{
ADVANCE_CHAR (string, slen, i);
continue;
}
else
ADVANCE_CHAR (string, slen, i);
}
CQ_RETURN(i);
}
#endif /* BANG_HISTORY */
#if defined (READLINE)
/* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
an unclosed quoted string), or if the character at EINDEX is quoted
by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
single and double-quoted string parsing functions should not return an
error if there are unclosed quotes or braces. The characters that this
recognizes need to be the same as the contents of
rl_completer_quote_characters. */
int
char_is_quoted (string, eindex)
char *string;
int eindex;
{
int i, pass_next, c, oldjmp;
size_t slen;
DECLARE_MBSTATE;
slen = strlen (string);
oldjmp = no_longjmp_on_fatal_error;
no_longjmp_on_fatal_error = 1;
i = pass_next = 0;
while (i <= eindex)
{
c = string[i];
if (pass_next)
{
pass_next = 0;
if (i >= eindex) /* XXX was if (i >= eindex - 1) */
CQ_RETURN(1);
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '\\')
{
pass_next = 1;
i++;
continue;
}
else if (c == '$' && string[i+1] == '\'' && string[i+2])
{
i += 2;
i = skip_single_quoted (string, slen, i, SX_COMPLETE);
if (i > eindex)
CQ_RETURN (i);
}
else if (c == '\'' || c == '"')
{
i = (c == '\'') ? skip_single_quoted (string, slen, ++i, 0)
: skip_double_quoted (string, slen, ++i, SX_COMPLETE);
if (i > eindex)
CQ_RETURN(1);
/* no increment, the skip_xxx functions go one past end */
}
else
ADVANCE_CHAR (string, slen, i);
}
CQ_RETURN(0);
}
int
unclosed_pair (string, eindex, openstr)
char *string;
int eindex;
char *openstr;
{
int i, pass_next, openc, olen;
size_t slen;
DECLARE_MBSTATE;
slen = strlen (string);
olen = strlen (openstr);
i = pass_next = openc = 0;
while (i <= eindex)
{
if (pass_next)
{
pass_next = 0;
if (i >= eindex) /* XXX was if (i >= eindex - 1) */
return 0;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (string[i] == '\\')
{
pass_next = 1;
i++;
continue;
}
else if (STREQN (string + i, openstr, olen))
{
openc = 1 - openc;
i += olen;
}
/* XXX - may want to handle $'...' specially here */
else if (string[i] == '\'' || string[i] == '"')
{
i = (string[i] == '\'') ? skip_single_quoted (string, slen, i, 0)
: skip_double_quoted (string, slen, i, SX_COMPLETE);
if (i > eindex)
return 0;
}
else
ADVANCE_CHAR (string, slen, i);
}
return (openc);
}
/* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
individual words. If DELIMS is NULL, the current value of $IFS is used
to split the string, and the function follows the shell field splitting
rules. SENTINEL is an index to look for. NWP, if non-NULL,
gets the number of words in the returned list. CWP, if non-NULL, gets
the index of the word containing SENTINEL. Non-whitespace chars in
DELIMS delimit separate fields. This is used by programmable completion. */
WORD_LIST *
split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp)
char *string;
int slen;
char *delims;
int sentinel, flags;
int *nwp, *cwp;
{
int ts, te, i, nw, cw, ifs_split, dflags;
char *token, *d, *d2;
WORD_LIST *ret, *tl;
if (string == 0 || *string == '\0')
{
if (nwp)
*nwp = 0;
if (cwp)
*cwp = 0;
return ((WORD_LIST *)NULL);
}
d = (delims == 0) ? ifs_value : delims;
ifs_split = delims == 0;
/* Make d2 the non-whitespace characters in delims */
d2 = 0;
if (delims)
{
size_t slength;
#if defined (HANDLE_MULTIBYTE)
size_t mblength = 1;
#endif
DECLARE_MBSTATE;
slength = strlen (delims);
d2 = (char *)xmalloc (slength + 1);
i = ts = 0;
while (delims[i])
{
#if defined (HANDLE_MULTIBYTE)
mbstate_t state_bak;
state_bak = state;
mblength = MBRLEN (delims + i, slength, &state);
if (MB_INVALIDCH (mblength))
state = state_bak;
else if (mblength > 1)
{
memcpy (d2 + ts, delims + i, mblength);
ts += mblength;
i += mblength;
slength -= mblength;
continue;
}
#endif
if (whitespace (delims[i]) == 0)
d2[ts++] = delims[i];
i++;
slength--;
}
d2[ts] = '\0';
}
ret = (WORD_LIST *)NULL;
/* Remove sequences of whitespace characters at the start of the string, as
long as those characters are delimiters. */
for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
;
if (string[i] == '\0')
{
FREE (d2);
return (ret);
}
ts = i;
nw = 0;
cw = -1;
dflags = flags|SD_NOJMP;
while (1)
{
te = skip_to_delim (string, ts, d, dflags);
/* If we have a non-whitespace delimiter character, use it to make a
separate field. This is just about what $IFS splitting does and
is closer to the behavior of the shell parser. */
if (ts == te && d2 && member (string[ts], d2))
{
te = ts + 1;
/* If we're using IFS splitting, the non-whitespace delimiter char
and any additional IFS whitespace delimits a field. */
if (ifs_split)
while (member (string[te], d) && spctabnl (string[te]) && ((flags&SD_NOQUOTEDELIM) == 0 || (string[te] != '\'' && string[te] != '"')))
te++;
else
while (member (string[te], d2) && ((flags&SD_NOQUOTEDELIM) == 0 || (string[te] != '\'' && string[te] != '"')))
te++;
}
token = substring (string, ts, te);
ret = add_string_to_list (token, ret);
free (token);
nw++;
if (sentinel >= ts && sentinel <= te)
cw = nw;
/* If the cursor is at whitespace just before word start, set the
sentinel word to the current word. */
if (cwp && cw == -1 && sentinel == ts-1)
cw = nw;
/* If the cursor is at whitespace between two words, make a new, empty
word, add it before (well, after, since the list is in reverse order)
the word we just added, and set the current word to that one. */
if (cwp && cw == -1 && sentinel < ts)
{
tl = make_word_list (make_word (""), ret->next);
ret->next = tl;
cw = nw;
nw++;
}
if (string[te] == 0)
break;
i = te;
/* XXX - honor SD_NOQUOTEDELIM here */
while (member (string[i], d) && (ifs_split || spctabnl(string[i])) && ((flags&SD_NOQUOTEDELIM) == 0 || (string[te] != '\'' && string[te] != '"')))
i++;
if (string[i])
ts = i;
else
break;
}
/* Special case for SENTINEL at the end of STRING. If we haven't found
the word containing SENTINEL yet, and the index we're looking for is at
the end of STRING (or past the end of the previously-found token,
possible if the end of the line is composed solely of IFS whitespace)
add an additional null argument and set the current word pointer to that. */
if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te))
{
if (whitespace (string[sentinel - 1]))
{
token = "";
ret = add_string_to_list (token, ret);
nw++;
}
cw = nw;
}
if (nwp)
*nwp = nw;
if (cwp)
*cwp = cw;
FREE (d2);
return (REVERSE_LIST (ret, WORD_LIST *));
}
#endif /* READLINE */
#if 0
/* UNUSED */
/* Extract the name of the variable to bind to from the assignment string. */
char *
assignment_name (string)
char *string;
{
int offset;
char *temp;
offset = assignment (string, 0);
if (offset == 0)
return (char *)NULL;
temp = substring (string, 0, offset);
return (temp);
}
#endif
/* **************************************************************** */
/* */
/* Functions to convert strings to WORD_LISTs and vice versa */
/* */
/* **************************************************************** */
/* Return a single string of all the words in LIST. SEP is the separator
to put between individual elements of LIST in the output string. */
char *
string_list_internal (list, sep)
WORD_LIST *list;
char *sep;
{
register WORD_LIST *t;
char *result, *r;
size_t word_len, sep_len, result_size;
if (list == 0)
return ((char *)NULL);
/* Short-circuit quickly if we don't need to separate anything. */
if (list->next == 0)
return (savestring (list->word->word));
/* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
sep_len = STRLEN (sep);
result_size = 0;
for (t = list; t; t = t->next)
{
if (t != list)
result_size += sep_len;
result_size += strlen (t->word->word);
}
r = result = (char *)xmalloc (result_size + 1);
for (t = list; t; t = t->next)
{
if (t != list && sep_len)
{
if (sep_len > 1)
{
FASTCOPY (sep, r, sep_len);
r += sep_len;
}
else
*r++ = sep[0];
}
word_len = strlen (t->word->word);
FASTCOPY (t->word->word, r, word_len);
r += word_len;
}
*r = '\0';
return (result);
}
/* Return a single string of all the words present in LIST, separating
each word with a space. */
char *
string_list (list)
WORD_LIST *list;
{
return (string_list_internal (list, " "));
}
/* An external interface that can be used by the rest of the shell to
obtain a string containing the first character in $IFS. Handles all
the multibyte complications. If LENP is non-null, it is set to the
length of the returned string. */
char *
ifs_firstchar (lenp)
int *lenp;
{
char *ret;
int len;
ret = xmalloc (MB_LEN_MAX + 1);
#if defined (HANDLE_MULTIBYTE)
if (ifs_firstc_len == 1)
{
ret[0] = ifs_firstc[0];
ret[1] = '\0';
len = ret[0] ? 1 : 0;
}
else
{
memcpy (ret, ifs_firstc, ifs_firstc_len);
ret[len = ifs_firstc_len] = '\0';
}
#else
ret[0] = ifs_firstc;
ret[1] = '\0';
len = ret[0] ? 0 : 1;
#endif
if (lenp)
*lenp = len;
return ret;
}
/* Return a single string of all the words present in LIST, obeying the
quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
expansion [of $*] appears within a double quoted string, it expands
to a single field with the value of each parameter separated by the
first character of the IFS variable, or by a <space> if IFS is unset." */
/* Posix interpretation 888 changes this when IFS is null by specifying
that when unquoted, this expands to separate arguments */
char *
string_list_dollar_star (list, quoted, flags)
WORD_LIST *list;
int quoted, flags;
{
char *ret;
#if defined (HANDLE_MULTIBYTE)
# if defined (__GNUC__)
char sep[MB_CUR_MAX + 1];
# else
char *sep = 0;
# endif
#else
char sep[2];
#endif
#if defined (HANDLE_MULTIBYTE)
# if !defined (__GNUC__)
sep = (char *)xmalloc (MB_CUR_MAX + 1);
# endif /* !__GNUC__ */
if (ifs_firstc_len == 1)
{
sep[0] = ifs_firstc[0];
sep[1] = '\0';
}
else
{
memcpy (sep, ifs_firstc, ifs_firstc_len);
sep[ifs_firstc_len] = '\0';
}
#else
sep[0] = ifs_firstc;
sep[1] = '\0';
#endif
ret = string_list_internal (list, sep);
#if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
free (sep);
#endif
return ret;
}
/* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
is non-zero, the $@ appears within double quotes, and we should quote
the list before converting it into a string. If IFS is unset, and the
word is not quoted, we just need to quote CTLESC and CTLNUL characters
in the words in the list, because the default value of $IFS is
<space><tab><newline>, IFS characters in the words in the list should
also be split. If IFS is null, and the word is not quoted, we need
to quote the words in the list to preserve the positional parameters
exactly.
Valid values for the FLAGS argument are the PF_ flags in command.h,
the only one we care about is PF_ASSIGNRHS. $@ is supposed to expand
to the positional parameters separated by spaces no matter what IFS is
set to if in a context where word splitting is not performed. The only
one that we didn't handle before is assignment statement arguments to
declaration builtins like `declare'. */
char *
string_list_dollar_at (list, quoted, flags)
WORD_LIST *list;
int quoted;
int flags;
{
char *ifs, *ret;
#if defined (HANDLE_MULTIBYTE)
# if defined (__GNUC__)
char sep[MB_CUR_MAX + 1];
# else
char *sep = 0;
# endif /* !__GNUC__ */
#else
char sep[2];
#endif
WORD_LIST *tlist;
/* XXX this could just be ifs = ifs_value; */
ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
#if defined (HANDLE_MULTIBYTE)
# if !defined (__GNUC__)
sep = (char *)xmalloc (MB_CUR_MAX + 1);
# endif /* !__GNUC__ */
/* XXX - testing PF_ASSIGNRHS to make sure positional parameters are
separated with a space even when word splitting will not occur. */
if (flags & PF_ASSIGNRHS)
{
sep[0] = ' ';
sep[1] = '\0';
}
else if (ifs && *ifs)
{
if (ifs_firstc_len == 1)
{
sep[0] = ifs_firstc[0];
sep[1] = '\0';
}
else
{
memcpy (sep, ifs_firstc, ifs_firstc_len);
sep[ifs_firstc_len] = '\0';
}
}
else
{
sep[0] = ' ';
sep[1] = '\0';
}
#else /* !HANDLE_MULTIBYTE */
/* XXX - PF_ASSIGNRHS means no word splitting, so we want positional
parameters separated by a space. */
sep[0] = ((flags & PF_ASSIGNRHS) || ifs == 0 || *ifs == 0) ? ' ' : *ifs;
sep[1] = '\0';
#endif /* !HANDLE_MULTIBYTE */
/* XXX -- why call quote_list if ifs == 0? we can get away without doing
it now that quote_escapes quotes spaces */
tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
? quote_list (list)
: list_quote_escapes (list);
ret = string_list_internal (tlist, sep);
#if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
free (sep);
#endif
return ret;
}
/* Turn the positional parameters into a string, understanding quoting and
the various subtleties of using the first character of $IFS as the
separator. Calls string_list_dollar_at, string_list_dollar_star, and
string_list as appropriate. */
char *
string_list_pos_params (pchar, list, quoted)
int pchar;
WORD_LIST *list;
int quoted;
{
char *ret;
WORD_LIST *tlist;
if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES))
{
tlist = quote_list (list);
word_list_remove_quoted_nulls (tlist);
ret = string_list_dollar_star (tlist, 0, 0);
}
else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT))
{
tlist = quote_list (list);
word_list_remove_quoted_nulls (tlist);
ret = string_list (tlist);
}
else if (pchar == '*' && quoted == 0 && ifs_is_null) /* XXX */
ret = expand_no_split_dollar_star ? string_list_dollar_star (list, quoted, 0) : string_list_dollar_at (list, quoted, 0); /* Posix interp 888 */
else if (pchar == '*')
{
/* Even when unquoted, string_list_dollar_star does the right thing
making sure that the first character of $IFS is used as the
separator. */
ret = string_list_dollar_star (list, quoted, 0);
}
else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
/* We use string_list_dollar_at, but only if the string is quoted, since
that quotes the escapes if it's not, which we don't want. We could
use string_list (the old code did), but that doesn't do the right
thing if the first character of $IFS is not a space. We use
string_list_dollar_star if the string is unquoted so we make sure that
the elements of $@ are separated by the first character of $IFS for
later splitting. */
ret = string_list_dollar_at (list, quoted, 0);
else if (pchar == '@' && quoted == 0 && ifs_is_null) /* XXX */
ret = string_list_dollar_at (list, quoted, 0); /* Posix interp 888 */
else if (pchar == '@')
ret = string_list_dollar_star (list, quoted, 0);
else
ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list);
return ret;
}
/* Return the list of words present in STRING. Separate the string into
words at any of the characters found in SEPARATORS. If QUOTED is
non-zero then word in the list will have its quoted flag set, otherwise
the quoted flag is left as make_word () deemed fit.
This obeys the P1003.2 word splitting semantics. If `separators' is
exactly <space><tab><newline>, then the splitting algorithm is that of
the Bourne shell, which treats any sequence of characters from `separators'
as a delimiter. If IFS is unset, which results in `separators' being set
to "", no splitting occurs. If separators has some other value, the
following rules are applied (`IFS white space' means zero or more
occurrences of <space>, <tab>, or <newline>, as long as those characters
are in `separators'):
1) IFS white space is ignored at the start and the end of the
string.
2) Each occurrence of a character in `separators' that is not
IFS white space, along with any adjacent occurrences of
IFS white space delimits a field.
3) Any nonzero-length sequence of IFS white space delimits a field.
*/
/* BEWARE! list_string strips null arguments. Don't call it twice and
expect to have "" preserved! */
/* This performs word splitting and quoted null character removal on
STRING. */
#define issep(c) \
(((separators)[0]) ? ((separators)[1] ? isifs(c) \
: (c) == (separators)[0]) \
: 0)
/* member of the space character class in the current locale */
#define ifs_whitespace(c) ISSPACE(c)
/* "adjacent IFS white space" */
#define ifs_whitesep(c) ((sh_style_split || separators == 0) ? spctabnl (c) \
: ifs_whitespace (c))
WORD_LIST *
list_string (string, separators, quoted)
register char *string, *separators;
int quoted;
{
WORD_LIST *result;
WORD_DESC *t;
char *current_word, *s;
int sindex, sh_style_split, whitesep, xflags;
size_t slen;
if (!string || !*string)
return ((WORD_LIST *)NULL);
sh_style_split = separators && separators[0] == ' ' &&
separators[1] == '\t' &&
separators[2] == '\n' &&
separators[3] == '\0';
for (xflags = 0, s = ifs_value; s && *s; s++)
{
if (*s == CTLESC) xflags |= SX_NOCTLESC;
else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
}
slen = 0;
/* Remove sequences of whitespace at the beginning of STRING, as
long as those characters appear in IFS. Do not do this if
STRING is quoted or if there are no separator characters. We use the
Posix definition of whitespace as a member of the space character
class in the current locale. */
if (!quoted || !separators || !*separators)
{
for (s = string; *s && issep (*s) && ifs_whitespace (*s); s++);
if (!*s)
return ((WORD_LIST *)NULL);
string = s;
}
/* OK, now STRING points to a word that does not begin with white space.
The splitting algorithm is:
extract a word, stopping at a separator
skip sequences of whitespace characters as long as they are separators
This obeys the field splitting rules in Posix.2. */
slen = STRLEN (string);
for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
{
/* Don't need string length in ADVANCE_CHAR unless multibyte chars are
possible, but need it in string_extract_verbatim for bounds checking */
current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags);
if (current_word == 0)
break;
/* If we have a quoted empty string, add a quoted null argument. We
want to preserve the quoted null character iff this is a quoted
empty string; otherwise the quoted null characters are removed
below. */
if (QUOTED_NULL (current_word))
{
t = alloc_word_desc ();
t->word = make_quoted_char ('\0');
t->flags |= W_QUOTED|W_HASQUOTEDNULL;
result = make_word_list (t, result);
}
else if (current_word[0] != '\0')
{
/* If we have something, then add it regardless. However,
perform quoted null character removal on the current word. */
remove_quoted_nulls (current_word);
result = add_string_to_list (current_word, result);
result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */
if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
result->word->flags |= W_QUOTED;
}
/* If we're not doing sequences of separators in the traditional
Bourne shell style, then add a quoted null argument. */
else if (!sh_style_split && !ifs_whitespace (string[sindex]))
{
t = alloc_word_desc ();
t->word = make_quoted_char ('\0');
t->flags |= W_QUOTED|W_HASQUOTEDNULL;
result = make_word_list (t, result);
}
free (current_word);
/* Note whether or not the separator is IFS whitespace, used later. */
whitesep = string[sindex] && ifs_whitesep (string[sindex]);
/* Move past the current separator character. */
if (string[sindex])
{
DECLARE_MBSTATE;
ADVANCE_CHAR (string, slen, sindex);
}
/* Now skip sequences of whitespace characters if they are
in the list of separators. */
while (string[sindex] && ifs_whitesep (string[sindex]) && issep (string[sindex]))
sindex++;
/* If the first separator was IFS whitespace and the current character
is a non-whitespace IFS character, it should be part of the current
field delimiter, not a separate delimiter that would result in an
empty field. Look at POSIX.2, 3.6.5, (3)(b). */
if (string[sindex] && whitesep && issep (string[sindex]) && !ifs_whitesep (string[sindex]))
{
sindex++;
/* An IFS character that is not IFS white space, along with any
adjacent IFS white space, shall delimit a field. (SUSv3) */
while (string[sindex] && ifs_whitesep (string[sindex]) && isifs (string[sindex]))
sindex++;
}
}
return (REVERSE_LIST (result, WORD_LIST *));
}
/* Parse a single word from STRING, using SEPARATORS to separate fields.
ENDPTR is set to the first character after the word. This is used by
the `read' builtin.
This is never called with SEPARATORS != $IFS, and takes advantage of that.
XXX - this function is very similar to list_string; they should be
combined - XXX */
/* character is in $IFS */
#define islocalsep(c) (local_cmap[(unsigned char)(c)] != 0)
char *
get_word_from_string (stringp, separators, endptr)
char **stringp, *separators, **endptr;
{
register char *s;
char *current_word;
int sindex, sh_style_split, whitesep, xflags;
unsigned char local_cmap[UCHAR_MAX+1]; /* really only need single-byte chars here */
size_t slen;
if (!stringp || !*stringp || !**stringp)
return ((char *)NULL);
sh_style_split = separators && separators[0] == ' ' &&
separators[1] == '\t' &&
separators[2] == '\n' &&
separators[3] == '\0';
memset (local_cmap, '\0', sizeof (local_cmap));
for (xflags = 0, s = separators; s && *s; s++)
{
if (*s == CTLESC) xflags |= SX_NOCTLESC;
if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
local_cmap[(unsigned char)*s] = 1; /* local charmap of separators */
}
s = *stringp;
slen = 0;
/* Remove sequences of whitespace at the beginning of STRING, as
long as those characters appear in SEPARATORS. This happens if
SEPARATORS == $' \t\n' or if IFS is unset. */
if (sh_style_split || separators == 0)
for (; *s && spctabnl (*s) && islocalsep (*s); s++);
else
for (; *s && ifs_whitespace (*s) && islocalsep (*s); s++);
/* If the string is nothing but whitespace, update it and return. */
if (!*s)
{
*stringp = s;
if (endptr)
*endptr = s;
return ((char *)NULL);
}
/* OK, S points to a word that does not begin with white space.
Now extract a word, stopping at a separator, save a pointer to
the first character after the word, then skip sequences of spc,
tab, or nl as long as they are separators.
This obeys the field splitting rules in Posix.2. */
sindex = 0;
/* Don't need string length in ADVANCE_CHAR unless multibyte chars are
possible, but need it in string_extract_verbatim for bounds checking */
slen = STRLEN (s);
current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags);
/* Set ENDPTR to the first character after the end of the word. */
if (endptr)
*endptr = s + sindex;
/* Note whether or not the separator is IFS whitespace, used later. */
whitesep = s[sindex] && ifs_whitesep (s[sindex]);
/* Move past the current separator character. */
if (s[sindex])
{
DECLARE_MBSTATE;
ADVANCE_CHAR (s, slen, sindex);
}
/* Now skip sequences of space, tab, or newline characters if they are
in the list of separators. */
while (s[sindex] && spctabnl (s[sindex]) && islocalsep (s[sindex]))
sindex++;
/* If the first separator was IFS whitespace and the current character is
a non-whitespace IFS character, it should be part of the current field
delimiter, not a separate delimiter that would result in an empty field.
Look at POSIX.2, 3.6.5, (3)(b). */
if (s[sindex] && whitesep && islocalsep (s[sindex]) && !ifs_whitesep (s[sindex]))
{
sindex++;
/* An IFS character that is not IFS white space, along with any adjacent
IFS white space, shall delimit a field. */
while (s[sindex] && ifs_whitesep (s[sindex]) && islocalsep(s[sindex]))
sindex++;
}
/* Update STRING to point to the next field. */
*stringp = s + sindex;
return (current_word);
}
/* Remove IFS white space at the end of STRING. Start at the end
of the string and walk backwards until the beginning of the string
or we find a character that's not IFS white space and not CTLESC.
Only let CTLESC escape a white space character if SAW_ESCAPE is
non-zero. */
char *
strip_trailing_ifs_whitespace (string, separators, saw_escape)
char *string, *separators;
int saw_escape;
{
char *s;
s = string + STRLEN (string) - 1;
while (s > string && ((spctabnl (*s) && isifs (*s)) ||
(saw_escape && *s == CTLESC && spctabnl (s[1]))))
s--;
*++s = '\0';
return string;
}
#if 0
/* UNUSED */
/* Split STRING into words at whitespace. Obeys shell-style quoting with
backslashes, single and double quotes. */
WORD_LIST *
list_string_with_quotes (string)
char *string;
{
WORD_LIST *list;
char *token, *s;
size_t s_len;
int c, i, tokstart, len;
for (s = string; s && *s && spctabnl (*s); s++)
;
if (s == 0 || *s == 0)
return ((WORD_LIST *)NULL);
s_len = strlen (s);
tokstart = i = 0;
list = (WORD_LIST *)NULL;
while (1)
{
c = s[i];
if (c == '\\')
{
i++;
if (s[i])
i++;
}
else if (c == '\'')
i = skip_single_quoted (s, s_len, ++i, 0);
else if (c == '"')
i = skip_double_quoted (s, s_len, ++i, 0);
else if (c == 0 || spctabnl (c))
{
/* We have found the end of a token. Make a word out of it and
add it to the word list. */
token = substring (s, tokstart, i);
list = add_string_to_list (token, list);
free (token);
while (spctabnl (s[i]))
i++;
if (s[i])
tokstart = i;
else
break;
}
else
i++; /* normal character */
}
return (REVERSE_LIST (list, WORD_LIST *));
}
#endif
/********************************************************/
/* */
/* Functions to perform assignment statements */
/* */
/********************************************************/
#if defined (ARRAY_VARS)
static SHELL_VAR *
do_compound_assignment (name, value, flags)
char *name, *value;
int flags;
{
SHELL_VAR *v;
int mklocal, mkassoc, mkglobal, chklocal;
WORD_LIST *list;
char *newname; /* used for local nameref references */
mklocal = flags & ASS_MKLOCAL;
mkassoc = flags & ASS_MKASSOC;
mkglobal = flags & ASS_MKGLOBAL;
chklocal = flags & ASS_CHKLOCAL;
if (mklocal && variable_context)
{
v = find_variable (name); /* follows namerefs */
newname = (v == 0) ? nameref_transform_name (name, flags) : v->name;
if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
{
if (readonly_p (v))
err_readonly (name);
return (v); /* XXX */
}
list = expand_compound_array_assignment (v, value, flags);
if (mkassoc)
v = make_local_assoc_variable (newname, 0);
else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context)
v = make_local_array_variable (newname, 0);
if (v)
assign_compound_array_list (v, list, flags);
if (list)
dispose_words (list);
}
/* In a function but forcing assignment in global context. CHKLOCAL means to
check for an existing local variable first. */
else if (mkglobal && variable_context)
{
v = chklocal ? find_variable (name) : 0;
if (v && (local_p (v) == 0 || v->context != variable_context))
v = 0;
if (v == 0)
v = find_global_variable (name);
if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
{
if (readonly_p (v))
err_readonly (name);
return (v); /* XXX */
}
/* sanity check */
newname = (v == 0) ? nameref_transform_name (name, flags) : name;
list = expand_compound_array_assignment (v, value, flags);
if (v == 0 && mkassoc)
v = make_new_assoc_variable (newname);
else if (v && mkassoc && assoc_p (v) == 0)
v = convert_var_to_assoc (v);
else if (v == 0)
v = make_new_array_variable (newname);
else if (v && mkassoc == 0 && array_p (v) == 0)
v = convert_var_to_array (v);
if (v)
assign_compound_array_list (v, list, flags);
if (list)
dispose_words (list);
}
else
{
v = assign_array_from_string (name, value, flags);
if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
{
if (readonly_p (v))
err_readonly (name);
return (v); /* XXX */
}
}
return (v);
}
#endif
/* Given STRING, an assignment string, get the value of the right side
of the `=', and bind it to the left side. If EXPAND is true, then
perform parameter expansion, command substitution, and arithmetic
expansion on the right-hand side. Perform tilde expansion in any
case. Do not perform word splitting on the result of expansion. */
static int
do_assignment_internal (word, expand)
const WORD_DESC *word;
int expand;
{
int offset, appendop, assign_list, aflags, retval;
char *name, *value, *temp;
SHELL_VAR *entry;
#if defined (ARRAY_VARS)
char *t;
int ni;
#endif
const char *string;
if (word == 0 || word->word == 0)
return 0;
appendop = assign_list = aflags = 0;
string = word->word;
offset = assignment (string, 0);
name = savestring (string);
value = (char *)NULL;
if (name[offset] == '=')
{
if (name[offset - 1] == '+')
{
appendop = 1;
name[offset - 1] = '\0';
}
name[offset] = 0; /* might need this set later */
temp = name + offset + 1;
#if defined (ARRAY_VARS)
if (expand && (word->flags & W_COMPASSIGN))
{
assign_list = ni = 1;
value = extract_array_assignment_list (temp, &ni);
}
else
#endif
if (expand && temp[0])
value = expand_string_if_necessary (temp, 0, expand_string_assignment);
else
value = savestring (temp);
}
if (value == 0)
{
value = (char *)xmalloc (1);
value[0] = '\0';
}
if (echo_command_at_execute)
{
if (appendop)
name[offset - 1] = '+';
xtrace_print_assignment (name, value, assign_list, 1);
if (appendop)
name[offset - 1] = '\0';
}
#define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
if (appendop)
aflags |= ASS_APPEND;
#if defined (ARRAY_VARS)
if (t = mbschr (name, LBRACK))
{
if (assign_list)
{
report_error (_("%s: cannot assign list to array member"), name);
ASSIGN_RETURN (0);
}
entry = assign_array_element (name, value, aflags);
if (entry == 0)
ASSIGN_RETURN (0);
}
else if (assign_list)
{
if ((word->flags & W_ASSIGNARG) && (word->flags & W_CHKLOCAL))
aflags |= ASS_CHKLOCAL;
if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL) == 0)
aflags |= ASS_MKLOCAL;
if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL))
aflags |= ASS_MKGLOBAL;
if (word->flags & W_ASSIGNASSOC)
aflags |= ASS_MKASSOC;
entry = do_compound_assignment (name, value, aflags);
}
else
#endif /* ARRAY_VARS */
entry = bind_variable (name, value, aflags);
stupidly_hack_special_variables (name);
/* Return 1 if the assignment seems to have been performed correctly. */
if (entry == 0 || readonly_p (entry))
retval = 0; /* assignment failure */
else if (noassign_p (entry))
{
last_command_exit_value = EXECUTION_FAILURE;
retval = 1; /* error status, but not assignment failure */
}
else
retval = 1;
if (entry && retval != 0 && noassign_p (entry) == 0)
VUNSETATTR (entry, att_invisible);
ASSIGN_RETURN (retval);
}
/* Perform the assignment statement in STRING, and expand the
right side by doing tilde, command and parameter expansion. */
int
do_assignment (string)
char *string;
{
WORD_DESC td;
td.flags = W_ASSIGNMENT;
td.word = string;
return do_assignment_internal (&td, 1);
}
int
do_word_assignment (word, flags)
WORD_DESC *word;
int flags;
{
return do_assignment_internal (word, 1);
}
/* Given STRING, an assignment string, get the value of the right side
of the `=', and bind it to the left side. Do not perform any word
expansions on the right hand side. */
int
do_assignment_no_expand (string)
char *string;
{
WORD_DESC td;
td.flags = W_ASSIGNMENT;
td.word = string;
return (do_assignment_internal (&td, 0));
}
/***************************************************
* *
* Functions to manage the positional parameters *
* *
***************************************************/
/* Return the word list that corresponds to `$*'. */
WORD_LIST *
list_rest_of_args ()
{
register WORD_LIST *list, *args;
int i;
/* Break out of the loop as soon as one of the dollar variables is null. */
for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
list = make_word_list (make_bare_word (dollar_vars[i]), list);
for (args = rest_of_args; args; args = args->next)
list = make_word_list (make_bare_word (args->word->word), list);
return (REVERSE_LIST (list, WORD_LIST *));
}
int
number_of_args ()
{
register WORD_LIST *list;
int n;
for (n = 0; n < 9 && dollar_vars[n+1]; n++)
;
for (list = rest_of_args; list; list = list->next)
n++;
return n;
}
/* Return the value of a positional parameter. This handles values > 10. */
char *
get_dollar_var_value (ind)
intmax_t ind;
{
char *temp;
WORD_LIST *p;
if (ind < 10)
temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
else /* We want something like ${11} */
{
ind -= 10;
for (p = rest_of_args; p && ind--; p = p->next)
;
temp = p ? savestring (p->word->word) : (char *)NULL;
}
return (temp);
}
/* Make a single large string out of the dollar digit variables,
and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
case of "$*" with respect to IFS. */
char *
string_rest_of_args (dollar_star)
int dollar_star;
{
register WORD_LIST *list;
char *string;
list = list_rest_of_args ();
string = dollar_star ? string_list_dollar_star (list, 0, 0) : string_list (list);
dispose_words (list);
return (string);
}
/* Return a string containing the positional parameters from START to
END, inclusive. If STRING[0] == '*', we obey the rules for $*,
which only makes a difference if QUOTED is non-zero. If QUOTED includes
Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
no quoting chars are added. */
static char *
pos_params (string, start, end, quoted)
char *string;
int start, end, quoted;
{
WORD_LIST *save, *params, *h, *t;
char *ret;
int i;
/* see if we can short-circuit. if start == end, we want 0 parameters. */
if (start == end)
return ((char *)NULL);
save = params = list_rest_of_args ();
if (save == 0 && start > 0)
return ((char *)NULL);
if (start == 0) /* handle ${@:0[:x]} specially */
{
t = make_word_list (make_word (dollar_vars[0]), params);
save = params = t;
}
for (i = start ? 1 : 0; params && i < start; i++)
params = params->next;
if (params == 0)
{
dispose_words (save);
return ((char *)NULL);
}
for (h = t = params; params && i < end; i++)
{
t = params;
params = params->next;
}
t->next = (WORD_LIST *)NULL;
ret = string_list_pos_params (string[0], h, quoted);
if (t != params)
t->next = params;
dispose_words (save);
return (ret);
}
/******************************************************************/
/* */
/* Functions to expand strings to strings or WORD_LISTs */
/* */
/******************************************************************/
#if defined (PROCESS_SUBSTITUTION)
#define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
#else
#define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
#endif
/* If there are any characters in STRING that require full expansion,
then call FUNC to expand STRING; otherwise just perform quote
removal if necessary. This returns a new string. */
static char *
expand_string_if_necessary (string, quoted, func)
char *string;
int quoted;
EXPFUNC *func;
{
WORD_LIST *list;
size_t slen;
int i, saw_quote;
char *ret;
DECLARE_MBSTATE;
/* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
i = saw_quote = 0;
while (string[i])
{
if (EXP_CHAR (string[i]))
break;
else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
saw_quote = 1;
ADVANCE_CHAR (string, slen, i);
}
if (string[i])
{
list = (*func) (string, quoted);
if (list)
{
ret = string_list (list);
dispose_words (list);
}
else
ret = (char *)NULL;
}
else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
ret = string_quote_removal (string, quoted);
else
ret = savestring (string);
return ret;
}
static inline char *
expand_string_to_string_internal (string, quoted, func)
char *string;
int quoted;
EXPFUNC *func;
{
WORD_LIST *list;
char *ret;
if (string == 0 || *string == '\0')
return ((char *)NULL);
list = (*func) (string, quoted);
if (list)
{
ret = string_list (list);
dispose_words (list);
}
else
ret = (char *)NULL;
return (ret);
}
char *
expand_string_to_string (string, quoted)
char *string;
int quoted;
{
return (expand_string_to_string_internal (string, quoted, expand_string));
}
char *
expand_string_unsplit_to_string (string, quoted)
char *string;
int quoted;
{
return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
}
char *
expand_assignment_string_to_string (string, quoted)
char *string;
int quoted;
{
return (expand_string_to_string_internal (string, quoted, expand_string_assignment));
}
char *
expand_arith_string (string, quoted)
char *string;
int quoted;
{
WORD_DESC td;
WORD_LIST *list, *tlist;
size_t slen;
int i, saw_quote;
char *ret;
DECLARE_MBSTATE;
/* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
i = saw_quote = 0;
while (string[i])
{
if (EXP_CHAR (string[i]))
break;
else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
saw_quote = 1;
ADVANCE_CHAR (string, slen, i);
}
if (string[i])
{
/* This is expanded version of expand_string_internal as it's called by
expand_string_leave_quoted */
td.flags = W_NOPROCSUB|W_NOTILDE; /* don't want process substitution or tilde expansion */
td.word = savestring (string);
list = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
/* This takes care of the calls from expand_string_leave_quoted and
expand_string */
if (list)
{
tlist = word_list_split (list);
dispose_words (list);
list = tlist;
if (list)
dequote_list (list);
}
/* This comes from expand_string_if_necessary */
if (list)
{
ret = string_list (list);
dispose_words (list);
}
else
ret = (char *)NULL;
FREE (td.word);
}
else if (saw_quote && (quoted & Q_ARITH))
ret = string_quote_removal (string, quoted);
else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
ret = string_quote_removal (string, quoted);
else
ret = savestring (string);
return ret;
}
#if defined (COND_COMMAND)
/* Just remove backslashes in STRING. Returns a new string. */
char *
remove_backslashes (string)
char *string;
{
char *r, *ret, *s;
r = ret = (char *)xmalloc (strlen (string) + 1);
for (s = string; s && *s; )
{
if (*s == '\\')
s++;
if (*s == 0)
break;
*r++ = *s++;
}
*r = '\0';
return ret;
}
/* This needs better error handling. */
/* Expand W for use as an argument to a unary or binary operator in a
[[...]] expression. If SPECIAL is 1, this is the rhs argument
to the != or == operator, and should be treated as a pattern. In
this case, we quote the string specially for the globbing code. If
SPECIAL is 2, this is an rhs argument for the =~ operator, and should
be quoted appropriately for regcomp/regexec. The caller is responsible
for removing the backslashes if the unquoted word is needed later. */
char *
cond_expand_word (w, special)
WORD_DESC *w;
int special;
{
char *r, *p;
WORD_LIST *l;
int qflags;
if (w->word == 0 || w->word[0] == '\0')
return ((char *)NULL);
expand_no_split_dollar_star = 1;
w->flags |= W_NOSPLIT2;
l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
expand_no_split_dollar_star = 0;
if (l)
{
if (special == 0) /* LHS */
{
dequote_list (l);
r = string_list (l);
}
else
{
/* Need to figure out whether or not we should call dequote_escapes
or a new dequote_ctlnul function here, and under what
circumstances. */
qflags = QGLOB_CVTNULL|QGLOB_CTLESC;
if (special == 2)
qflags |= QGLOB_REGEXP;
word_list_remove_quoted_nulls (l);
p = string_list (l);
r = quote_string_for_globbing (p, qflags);
free (p);
}
dispose_words (l);
}
else
r = (char *)NULL;
return r;
}
#endif
/* Call expand_word_internal to expand W and handle error returns.
A convenience function for functions that don't want to handle
any errors or free any memory before aborting. */
static WORD_LIST *
call_expand_word_internal (w, q, i, c, e)
WORD_DESC *w;
int q, i, *c, *e;
{
WORD_LIST *result;
result = expand_word_internal (w, q, i, c, e);
if (result == &expand_word_error || result == &expand_word_fatal)
{
/* By convention, each time this error is returned, w->word has
already been freed (it sometimes may not be in the fatal case,
but that doesn't result in a memory leak because we're going
to exit in most cases). */
w->word = (char *)NULL;
last_command_exit_value = EXECUTION_FAILURE;
exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
/* NOTREACHED */
return (NULL);
}
else
return (result);
}
/* Perform parameter expansion, command substitution, and arithmetic
expansion on STRING, as if it were a word. Leave the result quoted.
Since this does not perform word splitting, it leaves quoted nulls
in the result. */
static WORD_LIST *
expand_string_internal (string, quoted)
char *string;
int quoted;
{
WORD_DESC td;
WORD_LIST *tresult;
if (string == 0 || *string == 0)
return ((WORD_LIST *)NULL);
td.flags = 0;
td.word = savestring (string);
tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
FREE (td.word);
return (tresult);
}
/* Expand STRING by performing parameter expansion, command substitution,
and arithmetic expansion. Dequote the resulting WORD_LIST before
returning it, but do not perform word splitting. The call to
remove_quoted_nulls () is in here because word splitting normally
takes care of quote removal. */
WORD_LIST *
expand_string_unsplit (string, quoted)
char *string;
int quoted;
{
WORD_LIST *value;
if (string == 0 || *string == '\0')
return ((WORD_LIST *)NULL);
expand_no_split_dollar_star = 1;
value = expand_string_internal (string, quoted);
expand_no_split_dollar_star = 0;
if (value)
{
if (value->word)
{
remove_quoted_nulls (value->word->word);
value->word->flags &= ~W_HASQUOTEDNULL;
}
dequote_list (value);
}
return (value);
}
/* Expand the rhs of an assignment statement */
WORD_LIST *
expand_string_assignment (string, quoted)
char *string;
int quoted;
{
WORD_DESC td;
WORD_LIST *value;
if (string == 0 || *string == '\0')
return ((WORD_LIST *)NULL);
expand_no_split_dollar_star = 1;
#if 0
/* Other shells (ksh93) do it this way, which affects how $@ is expanded
in constructs like bar=${@#0} (preserves the spaces resulting from the
expansion of $@ in a context where you don't do word splitting); Posix
interp 888 makes the expansion of $@ in contexts where word splitting
is not performed unspecified. */
td.flags = W_ASSIGNRHS|W_NOSPLIT2; /* Posix interp 888 */
#else
td.flags = W_ASSIGNRHS;
#endif
td.word = savestring (string);
value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
FREE (td.word);
expand_no_split_dollar_star = 0;
if (value)
{
if (value->word)
{
remove_quoted_nulls (value->word->word);
value->word->flags &= ~W_HASQUOTEDNULL;
}
dequote_list (value);
}
return (value);
}
/* Expand one of the PS? prompt strings. This is a sort of combination of
expand_string_unsplit and expand_string_internal, but returns the
passed string when an error occurs. Might want to trap other calls
to jump_to_top_level here so we don't endlessly loop. */
WORD_LIST *
expand_prompt_string (string, quoted, wflags)
char *string;
int quoted;
int wflags;
{
WORD_LIST *value;
WORD_DESC td;
if (string == 0 || *string == 0)
return ((WORD_LIST *)NULL);
td.flags = wflags;
td.word = savestring (string);
no_longjmp_on_fatal_error = 1;
value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
no_longjmp_on_fatal_error = 0;
if (value == &expand_word_error || value == &expand_word_fatal)
{
value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
return value;
}
FREE (td.word);
if (value)
{
if (value->word)
{
remove_quoted_nulls (value->word->word);
value->word->flags &= ~W_HASQUOTEDNULL;
}
dequote_list (value);
}
return (value);
}
/* Expand STRING just as if you were expanding a word, but do not dequote
the resultant WORD_LIST. This is called only from within this file,
and is used to correctly preserve quoted characters when expanding
things like ${1+"$@"}. This does parameter expansion, command
substitution, arithmetic expansion, and word splitting. */
static WORD_LIST *
expand_string_leave_quoted (string, quoted)
char *string;
int quoted;
{
WORD_LIST *tlist;
WORD_LIST *tresult;
if (string == 0 || *string == '\0')
return ((WORD_LIST *)NULL);
tlist = expand_string_internal (string, quoted);
if (tlist)
{
tresult = word_list_split (tlist);
dispose_words (tlist);
return (tresult);
}
return ((WORD_LIST *)NULL);
}
/* This does not perform word splitting or dequote the WORD_LIST
it returns. */
static WORD_LIST *
expand_string_for_rhs (string, quoted, op, pflags, dollar_at_p, expanded_p)
char *string;
int quoted, op, pflags;
int *dollar_at_p, *expanded_p;
{
WORD_DESC td;
WORD_LIST *tresult;
int old_nosplit;
if (string == 0 || *string == '\0')
return (WORD_LIST *)NULL;
/* We want field splitting to be determined by what is going to be done with
the entire ${parameterOPword} expansion, so we don't want to split the RHS
we expand here. However, the expansion of $* is determined by whether we
are going to eventually perform word splitting, so we want to set this
depending on whether or not are are going to be splitting: if the expansion
is quoted, if the OP is `=', or if IFS is set to the empty string, we
are not going to be splitting, so we set expand_no_split_dollar_star to
We pass through PF_ASSIGNRHS as W_ASSIGNRHS if this is on the RHS of an
assignment statement. */
/* The updated treatment of $* is the result of Posix interp 888 */
/* This was further clarified on the austin-group list in March, 2017 and
in Posix bug 1129 */
old_nosplit = expand_no_split_dollar_star;
expand_no_split_dollar_star = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || op == '=' || ifs_is_null == 0; /* XXX - was 1 */
td.flags = W_NOSPLIT2; /* no splitting, remove "" and '' */
if (pflags & PF_ASSIGNRHS) /* pass through */
td.flags |= W_ASSIGNRHS;
if (op == '=')
#if 0
td.flags |= W_ASSIGNRHS; /* expand b in ${a=b} like assignment */
#else
td.flags |= W_ASSIGNRHS|W_NOASSNTILDE; /* expand b in ${a=b} like assignment */
#endif
td.word = string;
tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p);
expand_no_split_dollar_star = old_nosplit;
return (tresult);
}
/* This does not perform word splitting or dequote the WORD_LIST
it returns and it treats $* as if it were quoted. */
static WORD_LIST *
expand_string_for_pat (string, quoted, dollar_at_p, expanded_p)
char *string;
int quoted, *dollar_at_p, *expanded_p;
{
WORD_DESC td;
WORD_LIST *tresult;
int oexp;
if (string == 0 || *string == '\0')
return (WORD_LIST *)NULL;
oexp = expand_no_split_dollar_star;
expand_no_split_dollar_star = 1;
td.flags = W_NOSPLIT2; /* no splitting, remove "" and '' */
td.word = string;
tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p);
expand_no_split_dollar_star = oexp;
return (tresult);
}
/* Expand STRING just as if you were expanding a word. This also returns
a list of words. Note that filename globbing is *NOT* done for word
or string expansion, just when the shell is expanding a command. This
does parameter expansion, command substitution, arithmetic expansion,
and word splitting. Dequote the resultant WORD_LIST before returning. */
WORD_LIST *
expand_string (string, quoted)
char *string;
int quoted;
{
WORD_LIST *result;
if (string == 0 || *string == '\0')
return ((WORD_LIST *)NULL);
result = expand_string_leave_quoted (string, quoted);
return (result ? dequote_list (result) : result);
}
/*******************************************
* *
* Functions to expand WORD_DESCs *
* *
*******************************************/
/* Expand WORD, performing word splitting on the result. This does
parameter expansion, command substitution, arithmetic expansion,
word splitting, and quote removal. */
WORD_LIST *
expand_word (word, quoted)
WORD_DESC *word;
int quoted;
{
WORD_LIST *result, *tresult;
tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
result = word_list_split (tresult);
dispose_words (tresult);
return (result ? dequote_list (result) : result);
}
/* Expand WORD, but do not perform word splitting on the result. This
does parameter expansion, command substitution, arithmetic expansion,
and quote removal. */
WORD_LIST *
expand_word_unsplit (word, quoted)
WORD_DESC *word;
int quoted;
{
WORD_LIST *result;
result = expand_word_leave_quoted (word, quoted);
return (result ? dequote_list (result) : result);
}
/* Perform shell expansions on WORD, but do not perform word splitting or
quote removal on the result. Virtually identical to expand_word_unsplit;
could be combined if implementations don't diverge. */
WORD_LIST *
expand_word_leave_quoted (word, quoted)
WORD_DESC *word;
int quoted;
{
WORD_LIST *result;
expand_no_split_dollar_star = 1;
if (ifs_is_null)
word->flags |= W_NOSPLIT;
word->flags |= W_NOSPLIT2;
result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
expand_no_split_dollar_star = 0;
return result;
}
/***************************************************
* *
* Functions to handle quoting chars *
* *
***************************************************/
/* Conventions:
A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
The parser passes CTLNUL as CTLESC CTLNUL. */
/* Quote escape characters in string s, but no other characters. This is
used to protect CTLESC and CTLNUL in variable values from the rest of
the word expansion process after the variable is expanded (word splitting
and filename generation). If IFS is null, we quote spaces as well, just
in case we split on spaces later (in the case of unquoted $@, we will
eventually attempt to split the entire word on spaces). Corresponding
code exists in dequote_escapes. Even if we don't end up splitting on
spaces, quoting spaces is not a problem. This should never be called on
a string that is quoted with single or double quotes or part of a here
document (effectively double-quoted).
FLAGS says whether or not we are going to split the result. If we are not,
and there is a CTLESC or CTLNUL in IFS, we need to quote CTLESC and CTLNUL,
respectively, to prevent them from being removed as part of dequoting. */
static char *
quote_escapes_internal (string, flags)
const char *string;
int flags;
{
const char *s, *send;
char *t, *result;
size_t slen;
int quote_spaces, skip_ctlesc, skip_ctlnul, nosplit;
DECLARE_MBSTATE;
slen = strlen (string);
send = string + slen;
quote_spaces = (ifs_value && *ifs_value == 0);
nosplit = (flags & PF_NOSPLIT2);
for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
{
skip_ctlesc |= (nosplit == 0 && *s == CTLESC);
skip_ctlnul |= (nosplit == 0 && *s == CTLNUL);
}
t = result = (char *)xmalloc ((slen * 2) + 1);
s = string;
while (*s)
{
if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' '))
*t++ = CTLESC;
COPY_CHAR_P (t, s, send);
}
*t = '\0';
return (result);
}
char *
quote_escapes (string)
const char *string;
{
return (quote_escapes_internal (string, 0));
}
char *
quote_rhs (string)
const char *string;
{
return (quote_escapes_internal (string, PF_NOSPLIT2));
}
static WORD_LIST *
list_quote_escapes (list)
WORD_LIST *list;
{
register WORD_LIST *w;
char *t;
for (w = list; w; w = w->next)
{
t = w->word->word;
w->word->word = quote_escapes (t);
free (t);
}
return list;
}
/* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
This is necessary to make unquoted CTLESC and CTLNUL characters in the
data stream pass through properly.
We need to remove doubled CTLESC characters inside quoted strings before
quoting the entire string, so we do not double the number of CTLESC
characters.
Also used by parts of the pattern substitution code. */
char *
dequote_escapes (string)
const char *string;
{
const char *s, *send;
char *t, *result;
size_t slen;
int quote_spaces;
DECLARE_MBSTATE;
if (string == 0)
return (char *)0;
slen = strlen (string);
send = string + slen;
t = result = (char *)xmalloc (slen + 1);
if (strchr (string, CTLESC) == 0)
return (strcpy (result, string));
quote_spaces = (ifs_value && *ifs_value == 0);
s = string;
while (*s)
{
if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
{
s++;
if (*s == '\0')
break;
}
COPY_CHAR_P (t, s, send);
}
*t = '\0';
return result;
}
#if defined (INCLUDE_UNUSED)
static WORD_LIST *
list_dequote_escapes (list)
WORD_LIST *list;
{
register WORD_LIST *w;
char *t;
for (w = list; w; w = w->next)
{
t = w->word->word;
w->word->word = dequote_escapes (t);
free (t);
}
return list;
}
#endif
/* Return a new string with the quoted representation of character C.
This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
set in any resultant WORD_DESC where this value is the word. */
static char *
make_quoted_char (c)
int c;
{
char *temp;
temp = (char *)xmalloc (3);
if (c == 0)
{
temp[0] = CTLNUL;
temp[1] = '\0';
}
else
{
temp[0] = CTLESC;
temp[1] = c;
temp[2] = '\0';
}
return (temp);
}
/* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
this value is the word. */
char *
quote_string (string)
char *string;
{
register char *t;
size_t slen;
char *result, *send;
if (*string == 0)
{
result = (char *)xmalloc (2);
result[0] = CTLNUL;
result[1] = '\0';
}
else
{
DECLARE_MBSTATE;
slen = strlen (string);
send = string + slen;
result = (char *)xmalloc ((slen * 2) + 1);
for (t = result; string < send; )
{
*t++ = CTLESC;
COPY_CHAR_P (t, string, send);
}
*t = '\0';
}
return (result);
}
/* De-quote quoted characters in STRING. */
char *
dequote_string (string)
char *string;
{
register char *s, *t;
size_t slen;
char *result, *send;
DECLARE_MBSTATE;
#if defined (DEBUG)
if (string[0] == CTLESC && string[1] == 0)
internal_inform ("dequote_string: string with bare CTLESC");
#endif
slen = STRLEN (string);
t = result = (char *)xmalloc (slen + 1);
if (QUOTED_NULL (string))
{
result[0] = '\0';
return (result);
}
/* A string consisting of only a single CTLESC should pass through unchanged */
if (string[0] == CTLESC && string[1] == 0)
{
result[0] = CTLESC;
result[1] = '\0';
return (result);
}
/* If no character in the string can be quoted, don't bother examining
each character. Just return a copy of the string passed to us. */
if (strchr (string, CTLESC) == NULL)
return (strcpy (result, string));
send = string + slen;
s = string;
while (*s)
{
if (*s == CTLESC)
{
s++;
if (*s == '\0')
break;
}
COPY_CHAR_P (t, s, send);
}
*t = '\0';
return (result);
}
/* Quote the entire WORD_LIST list. */
static WORD_LIST *
quote_list (list)
WORD_LIST *list;
{
register WORD_LIST *w;
char *t;
for (w = list; w; w = w->next)
{
t = w->word->word;
w->word->word = quote_string (t);
if (*t == 0)
w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */
w->word->flags |= W_QUOTED;
free (t);
}
return list;
}
WORD_DESC *
dequote_word (word)
WORD_DESC *word;
{
register char *s;
s = dequote_string (word->word);
if (QUOTED_NULL (word->word))
word->flags &= ~W_HASQUOTEDNULL;
free (word->word);
word->word = s;
return word;
}
/* De-quote quoted characters in each word in LIST. */
WORD_LIST *
dequote_list (list)
WORD_LIST *list;
{
register char *s;
register WORD_LIST *tlist;
for (tlist = list; tlist; tlist = tlist->next)
{
s = dequote_string (tlist->word->word);
if (QUOTED_NULL (tlist->word->word))
tlist->word->flags &= ~W_HASQUOTEDNULL;
free (tlist->word->word);
tlist->word->word = s;
}
return list;
}
/* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
string. */
char *
remove_quoted_escapes (string)
char *string;
{
char *t;
if (string)
{
t = dequote_escapes (string);
strcpy (string, t);
free (t);
}
return (string);
}
/* Remove quoted $IFS characters from STRING. Quoted IFS characters are
added to protect them from word splitting, but we need to remove them
if no word splitting takes place. This returns newly-allocated memory,
so callers can use it to replace savestring(). */
char *
remove_quoted_ifs (string)
char *string;
{
register size_t slen;
register int i, j;
char *ret, *send;
DECLARE_MBSTATE;
slen = strlen (string);
send = string + slen;
i = j = 0;
ret = (char *)xmalloc (slen + 1);
while (i < slen)
{
if (string[i] == CTLESC)
{
i++;
if (string[i] == 0 || isifs (string[i]) == 0)
ret[j++] = CTLESC;
if (i == slen)
break;
}
COPY_CHAR_I (ret, j, string, send, i);
}
ret[j] = '\0';
return (ret);
}
char *
remove_quoted_nulls (string)
char *string;
{
register size_t slen;
register int i, j, prev_i;
DECLARE_MBSTATE;
if (strchr (string, CTLNUL) == 0) /* XXX */
return string; /* XXX */
slen = strlen (string);
i = j = 0;
while (i < slen)
{
if (string[i] == CTLESC)
{
/* Old code had j++, but we cannot assume that i == j at this
point -- what if a CTLNUL has already been removed from the
string? We don't want to drop the CTLESC or recopy characters
that we've already copied down. */
i++;
string[j++] = CTLESC;
if (i == slen)
break;
}
else if (string[i] == CTLNUL)
{
i++;
continue;
}
prev_i = i;
ADVANCE_CHAR (string, slen, i); /* COPY_CHAR_I? */
if (j < prev_i)
{
do string[j++] = string[prev_i++]; while (prev_i < i);
}
else
j = i;
}
string[j] = '\0';
return (string);
}
/* Perform quoted null character removal on each element of LIST.
This modifies LIST. */
void
word_list_remove_quoted_nulls (list)
WORD_LIST *list;
{
register WORD_LIST *t;
for (t = list; t; t = t->next)
{
remove_quoted_nulls (t->word->word);
t->word->flags &= ~W_HASQUOTEDNULL;
}
}
/* **************************************************************** */
/* */
/* Functions for Matching and Removing Patterns */
/* */
/* **************************************************************** */
#if defined (HANDLE_MULTIBYTE)
# ifdef INCLUDE_UNUSED
static unsigned char *
mb_getcharlens (string, len)
char *string;
int len;
{
int i, offset, last;
unsigned char *ret;
char *p;
DECLARE_MBSTATE;
i = offset = 0;
last = 0;
ret = (unsigned char *)xmalloc (len);
memset (ret, 0, len);
while (string[last])
{
ADVANCE_CHAR (string, len, offset);
ret[last] = offset - last;
last = offset;
}
return ret;
}
# endif
#endif
/* Remove the portion of PARAM matched by PATTERN according to OP, where OP
can have one of 4 values:
RP_LONG_LEFT remove longest matching portion at start of PARAM
RP_SHORT_LEFT remove shortest matching portion at start of PARAM
RP_LONG_RIGHT remove longest matching portion at end of PARAM
RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
*/
#define RP_LONG_LEFT 1
#define RP_SHORT_LEFT 2
#define RP_LONG_RIGHT 3
#define RP_SHORT_RIGHT 4
/* Returns its first argument if nothing matched; new memory otherwise */
static char *
remove_upattern (param, pattern, op)
char *param, *pattern;
int op;
{
register size_t len;
register char *end;
register char *p, *ret, c;
len = STRLEN (param);
end = param + len;
switch (op)
{
case RP_LONG_LEFT: /* remove longest match at start */
for (p = end; p >= param; p--)
{
c = *p; *p = '\0';
if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
*p = c;
return (savestring (p));
}
*p = c;
}
break;
case RP_SHORT_LEFT: /* remove shortest match at start */
for (p = param; p <= end; p++)
{
c = *p; *p = '\0';
if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
*p = c;
return (savestring (p));
}
*p = c;
}
break;
case RP_LONG_RIGHT: /* remove longest match at end */
for (p = param; p <= end; p++)
{
if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
c = *p; *p = '\0';
ret = savestring (param);
*p = c;
return (ret);
}
}
break;
case RP_SHORT_RIGHT: /* remove shortest match at end */
for (p = end; p >= param; p--)
{
if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
c = *p; *p = '\0';
ret = savestring (param);
*p = c;
return (ret);
}
}
break;
}
return (param); /* no match, return original string */
}
#if defined (HANDLE_MULTIBYTE)
/* Returns its first argument if nothing matched; new memory otherwise */
static wchar_t *
remove_wpattern (wparam, wstrlen, wpattern, op)
wchar_t *wparam;
size_t wstrlen;
wchar_t *wpattern;
int op;
{
wchar_t wc, *ret;
int n;
switch (op)
{
case RP_LONG_LEFT: /* remove longest match at start */
for (n = wstrlen; n >= 0; n--)
{
wc = wparam[n]; wparam[n] = L'\0';
if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
wparam[n] = wc;
return (wcsdup (wparam + n));
}
wparam[n] = wc;
}
break;
case RP_SHORT_LEFT: /* remove shortest match at start */
for (n = 0; n <= wstrlen; n++)
{
wc = wparam[n]; wparam[n] = L'\0';
if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
wparam[n] = wc;
return (wcsdup (wparam + n));
}
wparam[n] = wc;
}
break;
case RP_LONG_RIGHT: /* remove longest match at end */
for (n = 0; n <= wstrlen; n++)
{
if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
wc = wparam[n]; wparam[n] = L'\0';
ret = wcsdup (wparam);
wparam[n] = wc;
return (ret);
}
}
break;
case RP_SHORT_RIGHT: /* remove shortest match at end */
for (n = wstrlen; n >= 0; n--)
{
if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
{
wc = wparam[n]; wparam[n] = L'\0';
ret = wcsdup (wparam);
wparam[n] = wc;
return (ret);
}
}
break;
}
return (wparam); /* no match, return original string */
}
#endif /* HANDLE_MULTIBYTE */
static char *
remove_pattern (param, pattern, op)
char *param, *pattern;
int op;
{
char *xret;
if (param == NULL)
return (param);
if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */
return (savestring (param));
#if defined (HANDLE_MULTIBYTE)
if (MB_CUR_MAX > 1)
{
wchar_t *ret, *oret;
size_t n;
wchar_t *wparam, *wpattern;
mbstate_t ps;
/* XXX - could optimize here by checking param and pattern for multibyte
chars with mbsmbchar and calling remove_upattern. */
n = xdupmbstowcs (&wpattern, NULL, pattern);
if (n == (size_t)-1)
{
xret = remove_upattern (param, pattern, op);
return ((xret == param) ? savestring (param) : xret);
}
n = xdupmbstowcs (&wparam, NULL, param);
if (n == (size_t)-1)
{
free (wpattern);
xret = remove_upattern (param, pattern, op);
return ((xret == param) ? savestring (param) : xret);
}
oret = ret = remove_wpattern (wparam, n, wpattern, op);
/* Don't bother to convert wparam back to multibyte string if nothing
matched; just return copy of original string */
if (ret == wparam)
{
free (wparam);
free (wpattern);
return (savestring (param));
}
free (wparam);
free (wpattern);
n = strlen (param);
xret = (char *)xmalloc (n + 1);
memset (&ps, '\0', sizeof (mbstate_t));
n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
xret[n] = '\0'; /* just to make sure */
free (oret);
return xret;
}
else
#endif
{
xret = remove_upattern (param, pattern, op);
return ((xret == param) ? savestring (param) : xret);
}
}
/* Match PAT anywhere in STRING and return the match boundaries.
This returns 1 in case of a successful match, 0 otherwise. SP
and EP are pointers into the string where the match begins and
ends, respectively. MTYPE controls what kind of match is attempted.
MATCH_BEG and MATCH_END anchor the match at the beginning and end
of the string, respectively. The longest match is returned. */
static int
match_upattern (string, pat, mtype, sp, ep)
char *string, *pat;
int mtype;
char **sp, **ep;
{
int c, mlen;
size_t len;
register char *p, *p1, *npat;
char *end;
/* If the pattern doesn't match anywhere in the string, go ahead and
short-circuit right away. A minor optimization, saves a bunch of
unnecessary calls to strmatch (up to N calls for a string of N
characters) if the match is unsuccessful. To preserve the semantics
of the substring matches below, we make sure that the pattern has
`*' as first and last character, making a new pattern if necessary. */
/* XXX - check this later if I ever implement `**' with special meaning,
since this will potentially result in `**' at the beginning or end */
len = STRLEN (pat);
if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*')
{
int unescaped_backslash;
char *pp;
p = npat = (char *)xmalloc (len + 3);
p1 = pat;
if ((mtype != MATCH_BEG) && (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob)))
*p++ = '*';
while (*p1)
*p++ = *p1++;
#if 1
/* Need to also handle a pattern that ends with an unescaped backslash.
For right now, we ignore it because the pattern matching code will
fail the match anyway */
/* If the pattern ends with a `*' we leave it alone if it's preceded by
an even number of backslashes, but if it's escaped by a backslash
we need to add another `*'. */
if ((mtype != MATCH_END) && (p1[-1] == '*' && (unescaped_backslash = p1[-2] == '\\')))
{
pp = p1 - 3;
while (pp >= pat && *pp-- == '\\')
unescaped_backslash = 1 - unescaped_backslash;
if (unescaped_backslash)
*p++ = '*';
}
else if (mtype != MATCH_END && p1[-1] != '*')
*p++ = '*';
#else
if (p1[-1] != '*' || p1[-2] == '\\')
*p++ = '*';
#endif
*p = '\0';
}
else
npat = pat;
c = strmatch (npat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
if (npat != pat)
free (npat);
if (c == FNM_NOMATCH)
return (0);
len = STRLEN (string);
end = string + len;
mlen = umatchlen (pat, len);
switch (mtype)
{
case MATCH_ANY:
for (p = string; p <= end; p++)
{
if (match_pattern_char (pat, p, FNMATCH_IGNCASE))
{
p1 = (mlen == -1) ? end : p + mlen;
/* p1 - p = length of portion of string to be considered
p = current position in string
mlen = number of characters consumed by match (-1 for entire string)
end = end of string
we want to break immediately if the potential match len
is greater than the number of characters remaining in the
string
*/
if (p1 > end)
break;
for ( ; p1 >= p; p1--)
{
c = *p1; *p1 = '\0';
if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
{
*p1 = c;
*sp = p;
*ep = p1;
return 1;
}
*p1 = c;
#if 1
/* If MLEN != -1, we have a fixed length pattern. */
if (mlen != -1)
break;
#endif
}
}
}
return (0);
case MATCH_BEG:
if (match_pattern_char (pat, string, FNMATCH_IGNCASE) == 0)
return (0);
for (p = (mlen == -1) ? end : string + mlen; p >= string; p--)
{
c = *p; *p = '\0';
if (strmatch (pat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
{
*p = c;
*sp = string;
*ep = p;
return 1;
}
*p = c;
/* If MLEN != -1, we have a fixed length pattern. */
if (mlen != -1)
break;
}
return (0);
case MATCH_END:
for (p = end - ((mlen == -1) ? len : mlen); p <= end; p++)
{
if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
{
*sp = p;
*ep = end;
return 1;
}
/* If MLEN != -1, we have a fixed length pattern. */
if (mlen != -1)
break;
}
return (0);
}
return (0);
}
#if defined (HANDLE_MULTIBYTE)
#define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c))
/* Match WPAT anywhere in WSTRING and return the match boundaries.
This returns 1 in case of a successful match, 0 otherwise. Wide
character version. */
static int
match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
wchar_t *wstring;
char **indices;
size_t wstrlen;
wchar_t *wpat;
int mtype;
char **sp, **ep;
{
wchar_t wc, *wp, *nwpat, *wp1;
size_t len;
int mlen;
int n, n1, n2, simple;
simple = (wpat[0] != L'\\' && wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'[');
#if defined (EXTENDED_GLOB)
if (extended_glob)
simple &= (wpat[1] != L'(' || (wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'+' && wpat[0] != L'!' && wpat[0] != L'@')); /*)*/
#endif
/* If the pattern doesn't match anywhere in the string, go ahead and
short-circuit right away. A minor optimization, saves a bunch of
unnecessary calls to strmatch (up to N calls for a string of N
characters) if the match is unsuccessful. To preserve the semantics
of the substring matches below, we make sure that the pattern has
`*' as first and last character, making a new pattern if necessary. */
len = wcslen (wpat);
if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*')
{
int unescaped_backslash;
wchar_t *wpp;
wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
wp1 = wpat;
if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob))
*wp++ = L'*';
while (*wp1 != L'\0')
*wp++ = *wp1++;
#if 1
/* See comments above in match_upattern. */
if (wp1[-1] == L'*' && (unescaped_backslash = wp1[-2] == L'\\'))
{
wpp = wp1 - 3;
while (wpp >= wpat && *wpp-- == L'\\')
unescaped_backslash = 1 - unescaped_backslash;
if (unescaped_backslash)
*wp++ = L'*';
}
else if (wp1[-1] != L'*')
*wp++ = L'*';
#else
if (wp1[-1] != L'*' || wp1[-2] == L'\\')
*wp++ = L'*';
#endif
*wp = '\0';
}
else
nwpat = wpat;
len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
if (nwpat != wpat)
free (nwpat);
if (len == FNM_NOMATCH)
return (0);
mlen = wmatchlen (wpat, wstrlen);
/* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
switch (mtype)
{
case MATCH_ANY:
for (n = 0; n <= wstrlen; n++)
{
n2 = simple ? (WFOLD(*wpat) == WFOLD(wstring[n])) : match_pattern_wchar (wpat, wstring + n, FNMATCH_IGNCASE);
if (n2)
{
n1 = (mlen == -1) ? wstrlen : n + mlen;
if (n1 > wstrlen)
break;
for ( ; n1 >= n; n1--)
{
wc = wstring[n1]; wstring[n1] = L'\0';
if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
{
wstring[n1] = wc;
*sp = indices[n];
*ep = indices[n1];
return 1;
}
wstring[n1] = wc;
/* If MLEN != -1, we have a fixed length pattern. */
if (mlen != -1)
break;
}
}
}
return (0);
case MATCH_BEG:
if (match_pattern_wchar (wpat, wstring, FNMATCH_IGNCASE) == 0)
return (0);
for (n = (mlen == -1) ? wstrlen : mlen; n >= 0; n--)
{
wc = wstring[n]; wstring[n] = L'\0';
if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
{
wstring[n] = wc;
*sp = indices[0];
*ep = indices[n];
return 1;
}
wstring[n] = wc;
/* If MLEN != -1, we have a fixed length pattern. */
if (mlen != -1)
break;
}
return (0);
case MATCH_END:
for (n = wstrlen - ((mlen == -1) ? wstrlen : mlen); n <= wstrlen; n++)
{
if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
{
*sp = indices[n];
*ep = indices[wstrlen];
return 1;
}
/* If MLEN != -1, we have a fixed length pattern. */
if (mlen != -1)
break;
}
return (0);
}
return (0);
}
#undef WFOLD
#endif /* HANDLE_MULTIBYTE */
static int
match_pattern (string, pat, mtype, sp, ep)
char *string, *pat;
int mtype;
char **sp, **ep;
{
#if defined (HANDLE_MULTIBYTE)
int ret;
size_t n;
wchar_t *wstring, *wpat;
char **indices;
#endif
if (string == 0 || pat == 0 || *pat == 0)
return (0);
#if defined (HANDLE_MULTIBYTE)
if (MB_CUR_MAX > 1)
{
if (mbsmbchar (string) == 0 && mbsmbchar (pat) == 0)
return (match_upattern (string, pat, mtype, sp, ep));
n = xdupmbstowcs (&wpat, NULL, pat);
if (n == (size_t)-1)
return (match_upattern (string, pat, mtype, sp, ep));
n = xdupmbstowcs (&wstring, &indices, string);
if (n == (size_t)-1)
{
free (wpat);
return (match_upattern (string, pat, mtype, sp, ep));
}
ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
free (wpat);
free (wstring);
free (indices);
return (ret);
}
else
#endif
return (match_upattern (string, pat, mtype, sp, ep));
}
static int
getpatspec (c, value)
int c;
char *value;
{
if (c == '#')
return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
else /* c == '%' */
return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
}
/* Posix.2 says that the WORD should be run through tilde expansion,
parameter expansion, command substitution and arithmetic expansion.
This leaves the result quoted, so quote_string_for_globbing () has
to be called to fix it up for strmatch (). If QUOTED is non-zero,
it means that the entire expression was enclosed in double quotes.
This means that quoting characters in the pattern do not make any
special pattern characters quoted. For example, the `*' in the
following retains its special meaning: "${foo#'*'}". */
static char *
getpattern (value, quoted, expandpat)
char *value;
int quoted, expandpat;
{
char *pat, *tword;
WORD_LIST *l;
#if 0
int i;
#endif
/* There is a problem here: how to handle single or double quotes in the
pattern string when the whole expression is between double quotes?
POSIX.2 says that enclosing double quotes do not cause the pattern to
be quoted, but does that leave us a problem with @ and array[@] and their
expansions inside a pattern? */
#if 0
if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
{
i = 0;
pat = string_extract_double_quoted (tword, &i, SX_STRIPDQ);
free (tword);
tword = pat;
}
#endif
/* expand_string_for_pat () leaves WORD quoted and does not perform
word splitting. */
l = *value ? expand_string_for_pat (value,
(quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
(int *)NULL, (int *)NULL)
: (WORD_LIST *)0;
pat = string_list (l);
dispose_words (l);
if (pat)
{
tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
free (pat);
pat = tword;
}
return (pat);
}
#if 0
/* Handle removing a pattern from a string as a result of ${name%[%]value}
or ${name#[#]value}. */
static char *
variable_remove_pattern (value, pattern, patspec, quoted)
char *value, *pattern;
int patspec, quoted;
{
char *tword;
tword = remove_pattern (value, pattern, patspec);
return (tword);
}
#endif
static char *
list_remove_pattern (list, pattern, patspec, itype, quoted)
WORD_LIST *list;
char *pattern;
int patspec, itype, quoted;
{
WORD_LIST *new, *l;
WORD_DESC *w;
char *tword;
for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
{
tword = remove_pattern (l->word->word, pattern, patspec);
w = alloc_word_desc ();
w->word = tword ? tword : savestring ("");
new = make_word_list (w, new);
}
l = REVERSE_LIST (new, WORD_LIST *);
tword = string_list_pos_params (itype, l, quoted);
dispose_words (l);
return (tword);
}
static char *
parameter_list_remove_pattern (itype, pattern, patspec, quoted)
int itype;
char *pattern;
int patspec, quoted;
{
char *ret;
WORD_LIST *list;
list = list_rest_of_args ();
if (list == 0)
return ((char *)NULL);
ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
dispose_words (list);
return (ret);
}
#if defined (ARRAY_VARS)
static char *
array_remove_pattern (var, pattern, patspec, varname, quoted)
SHELL_VAR *var;
char *pattern;
int patspec;
char *varname; /* so we can figure out how it's indexed */
int quoted;
{
ARRAY *a;
HASH_TABLE *h;
int itype;
char *ret;
WORD_LIST *list;
SHELL_VAR *v;
/* compute itype from varname here */
v = array_variable_part (varname, 0, &ret, 0);
/* XXX */
if (v && invisible_p (v))
return ((char *)NULL);
itype = ret[0];
a = (v && array_p (v)) ? array_cell (v) : 0;
h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
if (list == 0)
return ((char *)NULL);
ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
dispose_words (list);
return ret;
}
#endif /* ARRAY_VARS */
static char *
parameter_brace_remove_pattern (varname, value, ind, patstr, rtype, quoted, flags)
char *varname, *value;
int ind;
char *patstr;
int rtype, quoted, flags;
{
int vtype, patspec, starsub;
char *temp1, *val, *pattern, *oname;
SHELL_VAR *v;
if (value == 0)
return ((char *)NULL);
oname = this_command_name;
this_command_name = varname;
vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
if (vtype == -1)
{
this_command_name = oname;
return ((char *)NULL);
}
starsub = vtype & VT_STARSUB;
vtype &= ~VT_STARSUB;
patspec = getpatspec (rtype, patstr);
if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
patstr++;
/* Need to pass getpattern newly-allocated memory in case of expansion --
the expansion code will free the passed string on an error. */
temp1 = savestring (patstr);
pattern = getpattern (temp1, quoted, 1);
free (temp1);
temp1 = (char *)NULL; /* shut up gcc */
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
temp1 = remove_pattern (val, pattern, patspec);
if (vtype == VT_VARIABLE)
FREE (val);
if (temp1)
{
val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
? quote_string (temp1)
: quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
temp1 = array_remove_pattern (v, pattern, patspec, varname, quoted);
if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
{
val = quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
#endif
case VT_POSPARMS:
temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
if (temp1 && quoted == 0 && ifs_is_null)
{
/* Posix interp 888 */
}
else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
{
val = quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
}
this_command_name = oname;
FREE (pattern);
return temp1;
}
#if defined (PROCESS_SUBSTITUTION)
/*****************************************************************/
/* */
/* Hacking Process Substitution */
/* */
/*****************************************************************/
#if !defined (HAVE_DEV_FD)
/* Named pipes must be removed explicitly with `unlink'. This keeps a list
of FIFOs the shell has open. unlink_fifo_list will walk the list and
unlink all of them. add_fifo_list adds the name of an open FIFO to the
list. NFIFO is a count of the number of FIFOs in the list. */
#define FIFO_INCR 20
/* PROC value of -1 means the process has been reaped and the FIFO needs to
be removed. PROC value of 0 means the slot is unused. */
struct temp_fifo {
char *file;
pid_t proc;
};
static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
static int nfifo;
static int fifo_list_size;
void
clear_fifo_list ()
{
}
char *
copy_fifo_list (sizep)
int *sizep;
{
if (sizep)
*sizep = 0;
return (char *)NULL;
}
static void
add_fifo_list (pathname)
char *pathname;
{
int osize, i;
if (nfifo >= fifo_list_size - 1)
{
osize = fifo_list_size;
fifo_list_size += FIFO_INCR;
fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
fifo_list_size * sizeof (struct temp_fifo));
for (i = osize; i < fifo_list_size; i++)
{
fifo_list[i].file = (char *)NULL;
fifo_list[i].proc = 0; /* unused */
}
}
fifo_list[nfifo].file = savestring (pathname);
nfifo++;
}
void
unlink_fifo (i)
int i;
{
if ((fifo_list[i].proc == (pid_t)-1) || (fifo_list[i].proc > 0 && (kill(fifo_list[i].proc, 0) == -1)))
{
unlink (fifo_list[i].file);
free (fifo_list[i].file);
fifo_list[i].file = (char *)NULL;
fifo_list[i].proc = 0;
}
}
void
unlink_fifo_list ()
{
int saved, i, j;
if (nfifo == 0)
return;
for (i = saved = 0; i < nfifo; i++)
{
if ((fifo_list[i].proc == (pid_t)-1) || (fifo_list[i].proc > 0 && (kill(fifo_list[i].proc, 0) == -1)))
{
unlink (fifo_list[i].file);
free (fifo_list[i].file);
fifo_list[i].file = (char *)NULL;
fifo_list[i].proc = 0;
}
else
saved++;
}
/* If we didn't remove some of the FIFOs, compact the list. */
if (saved)
{
for (i = j = 0; i < nfifo; i++)
if (fifo_list[i].file)
{
fifo_list[j].file = fifo_list[i].file;
fifo_list[j].proc = fifo_list[i].proc;
j++;
}
nfifo = j;
}
else
nfifo = 0;
}
/* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
from some point in the past, and close all open FIFOs in fifo_list
that are not marked as active in LIST. If LIST is NULL, close
everything in fifo_list. LSIZE is the number of elements in LIST, in
case it's larger than fifo_list_size (size of fifo_list). */
void
close_new_fifos (list, lsize)
char *list;
int lsize;
{
int i;
if (list == 0)
{
unlink_fifo_list ();
return;
}
for (i = 0; i < lsize; i++)
if (list[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1)
unlink_fifo (i);
for (i = lsize; i < fifo_list_size; i++)
unlink_fifo (i);
}
int
find_procsub_child (pid)
pid_t pid;
{
int i;
for (i = 0; i < nfifo; i++)
if (fifo_list[i].proc == pid)
return i;
return -1;
}
void
set_procsub_status (ind, pid, status)
int ind;
pid_t pid;
int status;
{
if (ind >= 0 && ind < nfifo)
fifo_list[ind].proc = (pid_t)-1; /* sentinel */
}
/* If we've marked the process for this procsub as dead, close the
associated file descriptor and delete the FIFO. */
void
reap_procsubs ()
{
int i;
for (i = 0; i < nfifo; i++)
if (fifo_list[i].proc == (pid_t)-1) /* reaped */
unlink_fifo (i);
}
void
wait_procsubs ()
{
int i, r;
for (i = 0; i < nfifo; i++)
{
if (fifo_list[i].proc != (pid_t)-1 && fifo_list[i].proc > 0)
{
r = wait_for (fifo_list[i].proc);
fifo_list[i].proc = (pid_t)-1;
}
}
}
int
fifos_pending ()
{
return nfifo;
}
int
num_fifos ()
{
return nfifo;
}
static char *
make_named_pipe ()
{
char *tname;
tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR);
if (mkfifo (tname, 0600) < 0)
{
free (tname);
return ((char *)NULL);
}
add_fifo_list (tname);
return (tname);
}
#else /* HAVE_DEV_FD */
/* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
has open to children. NFDS is a count of the number of bits currently
set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
of open files. */
/* dev_fd_list[I] value of -1 means the process has been reaped and file
descriptor I needs to be closed. Value of 0 means the slot is unused. */
static pid_t *dev_fd_list = (pid_t *)NULL;
static int nfds;
static int totfds; /* The highest possible number of open files. */
void
clear_fifo (i)
int i;
{
if (dev_fd_list[i])
{
dev_fd_list[i] = 0;
nfds--;
}
}
void
clear_fifo_list ()
{
register int i;
if (nfds == 0)
return;
for (i = 0; nfds && i < totfds; i++)
clear_fifo (i);
nfds = 0;
}
char *
copy_fifo_list (sizep)
int *sizep;
{
char *ret;
if (nfds == 0 || totfds == 0)
{
if (sizep)
*sizep = 0;
return (char *)NULL;
}
if (sizep)
*sizep = totfds;
ret = (char *)xmalloc (totfds * sizeof (pid_t));
return (memcpy (ret, dev_fd_list, totfds * sizeof (pid_t)));
}
static void
add_fifo_list (fd)
int fd;
{
if (dev_fd_list == 0 || fd >= totfds)
{
int ofds;
ofds = totfds;
totfds = getdtablesize ();
if (totfds < 0 || totfds > 256)
totfds = 256;
if (fd >= totfds)
totfds = fd + 2;
dev_fd_list = (pid_t *)xrealloc (dev_fd_list, totfds * sizeof (dev_fd_list[0]));
/* XXX - might need a loop for this */
memset (dev_fd_list + ofds, '\0', (totfds - ofds) * sizeof (pid_t));
}
dev_fd_list[fd] = 1; /* marker; updated later */
nfds++;
}
int
fifos_pending ()
{
return 0; /* used for cleanup; not needed with /dev/fd */
}
int
num_fifos ()
{
return nfds;
}
void
unlink_fifo (fd)
int fd;
{
if (dev_fd_list[fd])
{
close (fd);
dev_fd_list[fd] = 0;
nfds--;
}
}
void
unlink_fifo_list ()
{
register int i;
if (nfds == 0)
return;
for (i = totfds-1; nfds && i >= 0; i--)
unlink_fifo (i);
nfds = 0;
}
/* Take LIST, which is a snapshot copy of dev_fd_list from some point in
the past, and close all open fds in dev_fd_list that are not marked
as open in LIST. If LIST is NULL, close everything in dev_fd_list.
LSIZE is the number of elements in LIST, in case it's larger than
totfds (size of dev_fd_list). */
void
close_new_fifos (list, lsize)
char *list;
int lsize;
{
int i;
if (list == 0)
{
unlink_fifo_list ();
return;
}
for (i = 0; i < lsize; i++)
if (list[i] == 0 && i < totfds && dev_fd_list[i])
unlink_fifo (i);
for (i = lsize; i < totfds; i++)
unlink_fifo (i);
}
int
find_procsub_child (pid)
pid_t pid;
{
int i;
if (nfds == 0)
return -1;
for (i = 0; i < totfds; i++)
if (dev_fd_list[i] == pid)
return i;
return -1;
}
void
set_procsub_status (ind, pid, status)
int ind;
pid_t pid;
int status;
{
if (ind >= 0 && ind < totfds)
dev_fd_list[ind] = (pid_t)-1; /* sentinel */
}
/* If we've marked the process for this procsub as dead, close the
associated file descriptor. */
void
reap_procsubs ()
{
int i;
for (i = 0; nfds > 0 && i < totfds; i++)
if (dev_fd_list[i] == (pid_t)-1)
unlink_fifo (i);
}
void
wait_procsubs ()
{
int i, r;
for (i = 0; nfds > 0 && i < totfds; i++)
{
if (dev_fd_list[i] != (pid_t)-1 && dev_fd_list[i] > 0)
{
r = wait_for (dev_fd_list[i]);
dev_fd_list[i] = (pid_t)-1;
}
}
}
#if defined (NOTDEF)
print_dev_fd_list ()
{
register int i;
fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
fflush (stderr);
for (i = 0; i < totfds; i++)
{
if (dev_fd_list[i])
fprintf (stderr, " %d", i);
}
fprintf (stderr, "\n");
}
#endif /* NOTDEF */
static char *
make_dev_fd_filename (fd)
int fd;
{
char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8);
strcpy (ret, DEV_FD_PREFIX);
p = inttostr (fd, intbuf, sizeof (intbuf));
strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
add_fifo_list (fd);
return (ret);
}
#endif /* HAVE_DEV_FD */
/* Return a filename that will open a connection to the process defined by
executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
a filename in /dev/fd corresponding to a descriptor that is one of the
ends of the pipe. If not defined, we use named pipes on systems that have
them. Systems without /dev/fd and named pipes are out of luck.
OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
use the read end of the pipe and dup that file descriptor to fd 0 in
the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
writing or use the write end of the pipe in the child, and dup that
file descriptor to fd 1 in the child. The parent does the opposite. */
static char *
process_substitute (string, open_for_read_in_child)
char *string;
int open_for_read_in_child;
{
char *pathname;
int fd, result, rc, function_value;
pid_t old_pid, pid;
#if defined (HAVE_DEV_FD)
int parent_pipe_fd, child_pipe_fd;
int fildes[2];
#endif /* HAVE_DEV_FD */
#if defined (JOB_CONTROL)
pid_t old_pipeline_pgrp;
#endif
if (!string || !*string || wordexp_only)
return ((char *)NULL);
#if !defined (HAVE_DEV_FD)
pathname = make_named_pipe ();
#else /* HAVE_DEV_FD */
if (pipe (fildes) < 0)
{
sys_error ("%s", _("cannot make pipe for process substitution"));
return ((char *)NULL);
}
/* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
the pipe in the parent, otherwise the read end. */
parent_pipe_fd = fildes[open_for_read_in_child];
child_pipe_fd = fildes[1 - open_for_read_in_child];
/* Move the parent end of the pipe to some high file descriptor, to
avoid clashes with FDs used by the script. */
parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
pathname = make_dev_fd_filename (parent_pipe_fd);
#endif /* HAVE_DEV_FD */
if (pathname == 0)
{
sys_error ("%s", _("cannot make pipe for process substitution"));
return ((char *)NULL);
}
old_pid = last_made_pid;
#if defined (JOB_CONTROL)
old_pipeline_pgrp = pipeline_pgrp;
if (pipeline_pgrp == 0 || (subshell_environment & (SUBSHELL_PIPE|SUBSHELL_FORK|SUBSHELL_ASYNC)) == 0)
pipeline_pgrp = shell_pgrp;
save_pipeline (1);
#endif /* JOB_CONTROL */
pid = make_child ((char *)NULL, 1);
if (pid == 0)
{
reset_terminating_signals (); /* XXX */
free_pushed_string_input ();
/* Cancel traps, in trap.c. */
restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */
QUIT; /* catch any interrupts we got post-fork */
setup_async_signals ();
subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB;
/* We don't inherit the verbose option for command substitutions now, so
let's try it for process substitutions. */
change_flag ('v', FLAG_OFF);
/* if we're expanding a redirection, we shouldn't have access to the
temporary environment, but commands in the subshell should have
access to their own temporary environment. */
if (expanding_redir)
flush_temporary_env ();
}
#if defined (JOB_CONTROL)
set_sigchld_handler ();
stop_making_children ();
/* XXX - should we only do this in the parent? (as in command subst) */
pipeline_pgrp = old_pipeline_pgrp;
#else
stop_making_children ();
#endif /* JOB_CONTROL */
if (pid < 0)
{
sys_error ("%s", _("cannot make child for process substitution"));
free (pathname);
#if defined (HAVE_DEV_FD)
close (parent_pipe_fd);
close (child_pipe_fd);
#endif /* HAVE_DEV_FD */
#if defined (JOB_CONTROL)
restore_pipeline (1);
#endif
return ((char *)NULL);
}
if (pid > 0)
{
#if defined (JOB_CONTROL)
if (last_procsub_child)
discard_last_procsub_child ();
last_procsub_child = restore_pipeline (0);
#endif
#if defined (HAVE_DEV_FD)
dev_fd_list[parent_pipe_fd] = pid;
#else
fifo_list[nfifo-1].proc = pid;
#endif
last_made_pid = old_pid;
#if defined (JOB_CONTROL) && defined (PGRP_PIPE)
close_pgrp_pipe ();
#endif /* JOB_CONTROL && PGRP_PIPE */
#if defined (HAVE_DEV_FD)
close (child_pipe_fd);
#endif /* HAVE_DEV_FD */
return (pathname);
}
set_sigint_handler ();
#if defined (JOB_CONTROL)
/* make sure we don't have any job control */
set_job_control (0);
/* The idea is that we want all the jobs we start from an async process
substitution to be in the same process group, but not the same pgrp
as our parent shell, since we don't want to affect our parent shell's
jobs if we get a SIGHUP and end up calling hangup_all_jobs, for example.
If pipeline_pgrp != shell_pgrp, we assume that there is a job control
shell somewhere in our parent process chain (since make_child initializes
pipeline_pgrp to shell_pgrp if job_control == 0). What we do in this
case is to set pipeline_pgrp to our PID, so all jobs started by this
process have that same pgrp and we are basically the process group leader.
This should not have negative effects on child processes surviving
after we exit, since we wait for the children we create, but that is
something to watch for. */
if (pipeline_pgrp != shell_pgrp)
pipeline_pgrp = getpid ();
#endif /* JOB_CONTROL */
#if !defined (HAVE_DEV_FD)
/* Open the named pipe in the child. */
fd = open (pathname, open_for_read_in_child ? O_RDONLY : O_WRONLY);
if (fd < 0)
{
/* Two separate strings for ease of translation. */
if (open_for_read_in_child)
sys_error (_("cannot open named pipe %s for reading"), pathname);
else
sys_error (_("cannot open named pipe %s for writing"), pathname);
exit (127);
}
if (open_for_read_in_child)
{
if (sh_unset_nodelay_mode (fd) < 0)
{
sys_error (_("cannot reset nodelay mode for fd %d"), fd);
exit (127);
}
}
#else /* HAVE_DEV_FD */
fd = child_pipe_fd;
#endif /* HAVE_DEV_FD */
/* Discard buffered stdio output before replacing the underlying file
descriptor. */
if (open_for_read_in_child == 0)
fpurge (stdout);
if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
{
sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
open_for_read_in_child ? 0 : 1);
exit (127);
}
if (fd != (open_for_read_in_child ? 0 : 1))
close (fd);
/* Need to close any files that this process has open to pipes inherited
from its parent. */
if (current_fds_to_close)
{
close_fd_bitmap (current_fds_to_close);
current_fds_to_close = (struct fd_bitmap *)NULL;
}
#if defined (HAVE_DEV_FD)
/* Make sure we close the parent's end of the pipe and clear the slot
in the fd list so it is not closed later, if reallocated by, for
instance, pipe(2). */
close (parent_pipe_fd);
dev_fd_list[parent_pipe_fd] = 0;
#endif /* HAVE_DEV_FD */
/* subshells shouldn't have this flag, which controls using the temporary
environment for variable lookups. We have already flushed the temporary
environment above in the case we're expanding a redirection, so processes
executed by this command need to be able to set it independently of their
parent. */
expanding_redir = 0;
remove_quoted_escapes (string);
/* Give process substitution a place to jump back to on failure,
so we don't go back up to main (). */
result = setjmp_nosigs (top_level);
/* If we're running a process substitution inside a shell function,
trap `return' so we don't return from the function in the subshell
and go off to never-never land. */
if (result == 0 && return_catch_flag)
function_value = setjmp_nosigs (return_catch);
else
function_value = 0;
if (result == ERREXIT)
rc = last_command_exit_value;
else if (result == EXITPROG)
rc = last_command_exit_value;
else if (result)
rc = EXECUTION_FAILURE;
else if (function_value)
rc = return_catch_value;
else
{
subshell_level++;
rc = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
/* leave subshell level intact for any exit trap */
}
#if !defined (HAVE_DEV_FD)
/* Make sure we close the named pipe in the child before we exit. */
close (open_for_read_in_child ? 0 : 1);
#endif /* !HAVE_DEV_FD */
last_command_exit_value = rc;
rc = run_exit_trap ();
exit (rc);
/*NOTREACHED*/
}
#endif /* PROCESS_SUBSTITUTION */
/***********************************/
/* */
/* Command Substitution */
/* */
/***********************************/
static char *
read_comsub (fd, quoted, flags, rflag)
int fd, quoted, flags;
int *rflag;
{
char *istring, buf[128], *bufp;
int istring_index, c, tflag, skip_ctlesc, skip_ctlnul;
size_t istring_size;
ssize_t bufn;
int nullbyte;
istring = (char *)NULL;
istring_index = istring_size = bufn = tflag = 0;
skip_ctlesc = ifs_cmap[CTLESC];
skip_ctlnul = ifs_cmap[CTLNUL];
nullbyte = 0;
/* Read the output of the command through the pipe. This may need to be
changed to understand multibyte characters in the future. */
while (1)
{
if (fd < 0)
break;
if (--bufn <= 0)
{
bufn = zread (fd, buf, sizeof (buf));
if (bufn <= 0)
break;
bufp = buf;
}
c = *bufp++;
if (c == 0)
{
#if 1
if (nullbyte == 0)
{
internal_warning ("%s", _("command substitution: ignored null byte in input"));
nullbyte = 1;
}
#endif
continue;
}
/* Add the character to ISTRING, possibly after resizing it. */
RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
/* This is essentially quote_string inline */
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */)
istring[istring_index++] = CTLESC;
else if ((flags & PF_ASSIGNRHS) && skip_ctlesc && c == CTLESC)
istring[istring_index++] = CTLESC;
/* Escape CTLESC and CTLNUL in the output to protect those characters
from the rest of the word expansions (word splitting and globbing.)
This is essentially quote_escapes inline. */
else if (skip_ctlesc == 0 && c == CTLESC)
istring[istring_index++] = CTLESC;
else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0)))
istring[istring_index++] = CTLESC;
istring[istring_index++] = c;
#if 0
#if defined (__CYGWIN__)
if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
{
istring_index--;
istring[istring_index - 1] = '\n';
}
#endif
#endif
}
if (istring)
istring[istring_index] = '\0';
/* If we read no output, just return now and save ourselves some
trouble. */
if (istring_index == 0)
{
FREE (istring);
if (rflag)
*rflag = tflag;
return (char *)NULL;
}
/* Strip trailing newlines from the output of the command. */
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
{
while (istring_index > 0)
{
if (istring[istring_index - 1] == '\n')
{
--istring_index;
/* If the newline was quoted, remove the quoting char. */
if (istring[istring_index - 1] == CTLESC)
--istring_index;
}
else
break;
}
istring[istring_index] = '\0';
}
else
strip_trailing (istring, istring_index - 1, 1);
if (rflag)
*rflag = tflag;
return istring;
}
/* Perform command substitution on STRING. This returns a WORD_DESC * with the
contained string possibly quoted. */
WORD_DESC *
command_substitute (string, quoted, flags)
char *string;
int quoted;
int flags;
{
pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
char *istring, *s;
int result, fildes[2], function_value, pflags, rc, tflag;
WORD_DESC *ret;
istring = (char *)NULL;
/* Don't fork () if there is no need to. In the case of no command to
run, just return NULL. */
#if 1
for (s = string; s && *s && (shellblank (*s) || *s == '\n'); s++)
;
if (s == 0 || *s == 0)
return ((WORD_DESC *)NULL);
#else
if (!string || !*string || (string[0] == '\n' && !string[1]))
return ((WORD_DESC *)NULL);
#endif
if (wordexp_only && read_but_dont_execute)
{
last_command_exit_value = EX_WEXPCOMSUB;
jump_to_top_level (EXITPROG);
}
/* We're making the assumption here that the command substitution will
eventually run a command from the file system. Since we'll run
maybe_make_export_env in this subshell before executing that command,
the parent shell and any other shells it starts will have to remake
the environment. If we make it before we fork, other shells won't
have to. Don't bother if we have any temporary variable assignments,
though, because the export environment will be remade after this
command completes anyway, but do it if all the words to be expanded
are variable assignments. */
if (subst_assign_varlist == 0 || garglist == 0)
maybe_make_export_env (); /* XXX */
/* Flags to pass to parse_and_execute() */
pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0;
old_pid = last_made_pid;
/* Pipe the output of executing STRING into the current shell. */
if (pipe (fildes) < 0)
{
sys_error ("%s", _("cannot make pipe for command substitution"));
goto error_exit;
}
#if defined (JOB_CONTROL)
old_pipeline_pgrp = pipeline_pgrp;
/* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
if ((subshell_environment & SUBSHELL_PIPE) == 0)
pipeline_pgrp = shell_pgrp;
cleanup_the_pipeline ();
#endif /* JOB_CONTROL */
old_async_pid = last_asynchronous_pid;
pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
last_asynchronous_pid = old_async_pid;
if (pid == 0)
{
/* Reset the signal handlers in the child, but don't free the
trap strings. Set a flag noting that we have to free the
trap strings if we run trap to change a signal disposition. */
reset_signal_handlers ();
if (ISINTERRUPT)
{
kill (getpid (), SIGINT);
CLRINTERRUPT; /* if we're ignoring SIGINT somehow */
}
QUIT; /* catch any interrupts we got post-fork */
subshell_environment |= SUBSHELL_RESETTRAP;
}
#if defined (JOB_CONTROL)
/* XXX DO THIS ONLY IN PARENT ? XXX */
set_sigchld_handler ();
stop_making_children ();
if (pid != 0)
pipeline_pgrp = old_pipeline_pgrp;
#else
stop_making_children ();
#endif /* JOB_CONTROL */
if (pid < 0)
{
sys_error (_("cannot make child for command substitution"));
error_exit:
last_made_pid = old_pid;
FREE (istring);
close (fildes[0]);
close (fildes[1]);
return ((WORD_DESC *)NULL);
}
if (pid == 0)
{
/* The currently executing shell is not interactive. */
interactive = 0;
set_sigint_handler (); /* XXX */
free_pushed_string_input ();
/* Discard buffered stdio output before replacing the underlying file
descriptor. */
fpurge (stdout);
if (dup2 (fildes[1], 1) < 0)
{
sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1"));
exit (EXECUTION_FAILURE);
}
/* If standard output is closed in the parent shell
(such as after `exec >&-'), file descriptor 1 will be
the lowest available file descriptor, and end up in
fildes[0]. This can happen for stdin and stderr as well,
but stdout is more important -- it will cause no output
to be generated from this command. */
if ((fildes[1] != fileno (stdin)) &&
(fildes[1] != fileno (stdout)) &&
(fildes[1] != fileno (stderr)))
close (fildes[1]);
if ((fildes[0] != fileno (stdin)) &&
(fildes[0] != fileno (stdout)) &&
(fildes[0] != fileno (stderr)))
close (fildes[0]);
#ifdef __CYGWIN__
/* Let stdio know the fd may have changed from text to binary mode, and
make sure to preserve stdout line buffering. */
freopen (NULL, "w", stdout);
sh_setlinebuf (stdout);
#endif /* __CYGWIN__ */
/* This is a subshell environment. */
subshell_environment |= SUBSHELL_COMSUB;
/* Many shells do not appear to inherit the -v option for command
substitutions. */
change_flag ('v', FLAG_OFF);
/* When inherit_errexit option is not enabled, command substitution does
not inherit the -e flag. It is enabled when Posix mode is enabled */
if (inherit_errexit == 0)
{
builtin_ignoring_errexit = 0;
change_flag ('e', FLAG_OFF);
}
set_shellopts ();
/* If we are expanding a redirection, we can dispose of any temporary
environment we received, since redirections are not supposed to have
access to the temporary environment. We will have to see whether this
affects temporary environments supplied to `eval', but the temporary
environment gets copied to builtin_env at some point. */
if (expanding_redir)
{
flush_temporary_env ();
expanding_redir = 0;
}
remove_quoted_escapes (string);
startup_state = 2; /* see if we can avoid a fork */
parse_and_execute_level = 0;
/* Give command substitution a place to jump back to on failure,
so we don't go back up to main (). */
result = setjmp_nosigs (top_level);
/* If we're running a command substitution inside a shell function,
trap `return' so we don't return from the function in the subshell
and go off to never-never land. */
if (result == 0 && return_catch_flag)
function_value = setjmp_nosigs (return_catch);
else
function_value = 0;
if (result == ERREXIT)
rc = last_command_exit_value;
else if (result == EXITPROG)
rc = last_command_exit_value;
else if (result)
rc = EXECUTION_FAILURE;
else if (function_value)
rc = return_catch_value;
else
{
subshell_level++;
rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
/* leave subshell level intact for any exit trap */
}
last_command_exit_value = rc;
rc = run_exit_trap ();
#if defined (PROCESS_SUBSTITUTION)
unlink_fifo_list ();
#endif
exit (rc);
}
else
{
#if defined (JOB_CONTROL) && defined (PGRP_PIPE)
close_pgrp_pipe ();
#endif /* JOB_CONTROL && PGRP_PIPE */
close (fildes[1]);
tflag = 0;
istring = read_comsub (fildes[0], quoted, flags, &tflag);
close (fildes[0]);
current_command_subst_pid = pid;
last_command_exit_value = wait_for (pid);
last_command_subst_pid = pid;
last_made_pid = old_pid;
#if defined (JOB_CONTROL)
/* If last_command_exit_value > 128, then the substituted command
was terminated by a signal. If that signal was SIGINT, then send
SIGINT to ourselves. This will break out of loops, for instance. */
if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
kill (getpid (), SIGINT);
/* wait_for gives the terminal back to shell_pgrp. If some other
process group should have it, give it away to that group here.
pipeline_pgrp is non-zero only while we are constructing a
pipeline, so what we are concerned about is whether or not that
pipeline was started in the background. A pipeline started in
the background should never get the tty back here. We duplicate
the conditions that wait_for tests to make sure we only give
the terminal back to pipeline_pgrp under the conditions that wait_for
gave it to shell_pgrp. If wait_for doesn't mess with the terminal
pgrp, we should not either. */
if (interactive && pipeline_pgrp != (pid_t)0 && running_in_background == 0 &&
(subshell_environment & (SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0)
give_terminal_to (pipeline_pgrp, 0);
#endif /* JOB_CONTROL */
ret = alloc_word_desc ();
ret->word = istring;
ret->flags = tflag;
return ret;
}
}
/********************************************************
* *
* Utility functions for parameter expansion *
* *
********************************************************/
#if defined (ARRAY_VARS)
static arrayind_t
array_length_reference (s)
char *s;
{
int len;
arrayind_t ind;
char *akey;
char *t, c;
ARRAY *array;
HASH_TABLE *h;
SHELL_VAR *var;
var = array_variable_part (s, 0, &t, &len);
/* If unbound variables should generate an error, report one and return
failure. */
if ((var == 0 || invisible_p (var) || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error)
{
c = *--t;
*t = '\0';
last_command_exit_value = EXECUTION_FAILURE;
err_unboundvar (s);
*t = c;
return (-1);
}
else if (var == 0 || invisible_p (var))
return 0;
/* We support a couple of expansions for variables that are not arrays.
We'll return the length of the value for v[0], and 1 for v[@] or
v[*]. Return 0 for everything else. */
array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
h = assoc_p (var) ? assoc_cell (var) : (HASH_TABLE *)NULL;
if (ALL_ELEMENT_SUB (t[0]) && t[1] == RBRACK)
{
if (assoc_p (var))
return (h ? assoc_num_elements (h) : 0);
else if (array_p (var))
return (array ? array_num_elements (array) : 0);
else
return (var_isset (var) ? 1 : 0);
}
if (assoc_p (var))
{
t[len - 1] = '\0';
akey = expand_assignment_string_to_string (t, 0); /* [ */
t[len - 1] = RBRACK;
if (akey == 0 || *akey == 0)
{
err_badarraysub (t);
FREE (akey);
return (-1);
}
t = assoc_reference (assoc_cell (var), akey);
free (akey);
}
else
{
ind = array_expand_index (var, t, len, 0);
/* negative subscripts to indexed arrays count back from end */
if (var && array_p (var) && ind < 0)
ind = array_max_index (array_cell (var)) + 1 + ind;
if (ind < 0)
{
err_badarraysub (t);
return (-1);
}
if (array_p (var))
t = array_reference (array, ind);
else
t = (ind == 0) ? value_cell (var) : (char *)NULL;
}
len = MB_STRLEN (t);
return (len);
}
#endif /* ARRAY_VARS */
static int
valid_brace_expansion_word (name, var_is_special)
char *name;
int var_is_special;
{
if (DIGIT (*name) && all_digits (name))
return 1;
else if (var_is_special)
return 1;
#if defined (ARRAY_VARS)
else if (valid_array_reference (name, 0))
return 1;
#endif /* ARRAY_VARS */
else if (legal_identifier (name))
return 1;
else
return 0;
}
static int
chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at)
char *name;
int quoted;
int *quoted_dollar_atp, *contains_dollar_at;
{
char *temp1;
if (name == 0)
{
if (quoted_dollar_atp)
*quoted_dollar_atp = 0;
if (contains_dollar_at)
*contains_dollar_at = 0;
return 0;
}
/* check for $@ and $* */
if (name[0] == '@' && name[1] == 0)
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
return 1;
}
else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
{
if (contains_dollar_at)
*contains_dollar_at = 1;
return 1;
}
/* Now check for ${array[@]} and ${array[*]} */
#if defined (ARRAY_VARS)
else if (valid_array_reference (name, 0))
{
temp1 = mbschr (name, LBRACK);
if (temp1 && temp1[1] == '@' && temp1[2] == RBRACK)
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
return 1;
}
/* ${array[*]}, when unquoted, should be treated like ${array[@]},
which should result in separate words even when IFS is unset. */
if (temp1 && temp1[1] == '*' && temp1[2] == RBRACK && quoted == 0)
{
if (contains_dollar_at)
*contains_dollar_at = 1;
return 1;
}
}
#endif
return 0;
}
/* Parameter expand NAME, and return a new string which is the expansion,
or NULL if there was no expansion. NAME is as given in ${NAMEcWORD}.
VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
NAME was found inside of a double-quoted expression. */
static WORD_DESC *
parameter_brace_expand_word (name, var_is_special, quoted, pflags, indp)
char *name;
int var_is_special, quoted, pflags;
arrayind_t *indp;
{
WORD_DESC *ret;
char *temp, *tt;
intmax_t arg_index;
SHELL_VAR *var;
int atype, rflags;
arrayind_t ind;
ret = 0;
temp = 0;
rflags = 0;
if (indp)
*indp = INTMAX_MIN;
/* Handle multiple digit arguments, as in ${11}. */
if (legal_number (name, &arg_index))
{
tt = get_dollar_var_value (arg_index);
if (tt)
temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
? quote_string (tt)
: quote_escapes (tt);
else
temp = (char *)NULL;
FREE (tt);
}
else if (var_is_special) /* ${@} */
{
int sindex;
tt = (char *)xmalloc (2 + strlen (name));
tt[sindex = 0] = '$';
strcpy (tt + 1, name);
ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
(int *)NULL, (int *)NULL, pflags);
free (tt);
}
#if defined (ARRAY_VARS)
else if (valid_array_reference (name, 0))
{
expand_arrayref:
var = array_variable_part (name, 0, &tt, (int *)0);
/* These are the cases where word splitting will not be performed */
if (pflags & PF_ASSIGNRHS)
{
if (ALL_ELEMENT_SUB (tt[0]) && tt[1] == RBRACK)
{
/* Only treat as double quoted if array variable */
if (var && (array_p (var) || assoc_p (var)))
temp = array_value (name, quoted|Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind);
else
temp = array_value (name, quoted, 0, &atype, &ind);
}
else
temp = array_value (name, quoted, 0, &atype, &ind);
}
/* Posix interp 888 */
else if (pflags & PF_NOSPLIT2)
{
/* Special cases, then general case, for each of A[@], A[*], A[n] */
#if defined (HANDLE_MULTIBYTE)
if (tt[0] == '@' && tt[1] == RBRACK && var && quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc[0] != ' ')
#else
if (tt[0] == '@' && tt[1] == RBRACK && var && quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc != ' ')
#endif
temp = array_value (name, Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind);
else if (tt[0] == '@' && tt[1] == RBRACK)
temp = array_value (name, quoted, 0, &atype, &ind);
else if (tt[0] == '*' && tt[1] == RBRACK && expand_no_split_dollar_star && ifs_is_null)
temp = array_value (name, Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT, 0, &atype, &ind);
else if (tt[0] == '*' && tt[1] == RBRACK)
temp = array_value (name, quoted, 0, &atype, &ind);
else
temp = array_value (name, quoted, 0, &atype, &ind);
}
else if (tt[0] == '*' && tt[1] == RBRACK && expand_no_split_dollar_star && ifs_is_null)
temp = array_value (name, Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT, 0, &atype, &ind);
else
temp = array_value (name, quoted, 0, &atype, &ind);
if (atype == 0 && temp)
{
temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
? quote_string (temp)
: quote_escapes (temp);
rflags |= W_ARRAYIND;
if (indp)
*indp = ind;
}
else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
rflags |= W_HASQUOTEDNULL;
}
#endif
else if (var = find_variable (name))
{
if (var_isset (var) && invisible_p (var) == 0)
{
#if defined (ARRAY_VARS)
if (assoc_p (var))
temp = assoc_reference (assoc_cell (var), "0");
else if (array_p (var))
temp = array_reference (array_cell (var), 0);
else
temp = value_cell (var);
#else
temp = value_cell (var);
#endif
if (temp)
temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
? quote_string (temp)
: ((pflags & PF_ASSIGNRHS) ? quote_rhs (temp)
: quote_escapes (temp));
}
else
temp = (char *)NULL;
}
else if (var = find_variable_last_nameref (name, 0))
{
temp = nameref_cell (var);
#if defined (ARRAY_VARS)
/* Handle expanding nameref whose value is x[n] */
if (temp && *temp && valid_array_reference (temp, 0))
{
name = temp;
goto expand_arrayref;
}
else
#endif
/* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */
if (temp && *temp && legal_identifier (temp) == 0)
{
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("%s: invalid variable name for name reference"), temp);
temp = &expand_param_error;
}
else
temp = (char *)NULL;
}
else
temp = (char *)NULL;
if (ret == 0)
{
ret = alloc_word_desc ();
ret->word = temp;
ret->flags |= rflags;
}
return ret;
}
static char *
parameter_brace_find_indir (name, var_is_special, quoted, find_nameref)
char *name;
int var_is_special, quoted, find_nameref;
{
char *temp, *t;
WORD_DESC *w;
SHELL_VAR *v;
int pflags, oldex;
if (find_nameref && var_is_special == 0 && (v = find_variable_last_nameref (name, 0)) &&
nameref_p (v) && (t = nameref_cell (v)) && *t)
return (savestring (t));
/* If var_is_special == 0, and name is not an array reference, this does
more expansion than necessary. It should really look up the variable's
value and not try to expand it. */
pflags = PF_IGNUNBOUND;
/* Note that we're not going to be doing word splitting here */
if (var_is_special)
{
pflags |= PF_ASSIGNRHS; /* suppresses word splitting */
oldex = expand_no_split_dollar_star;
expand_no_split_dollar_star = 1;
}
w = parameter_brace_expand_word (name, var_is_special, quoted, pflags, 0);
if (var_is_special)
expand_no_split_dollar_star = oldex;
t = w->word;
/* Have to dequote here if necessary */
if (t)
{
temp = ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || var_is_special)
? dequote_string (t)
: dequote_escapes (t);
free (t);
t = temp;
}
dispose_word_desc (w);
return t;
}
/* Expand an indirect reference to a variable: ${!NAME} expands to the
value of the variable whose name is the value of NAME. */
static WORD_DESC *
parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at)
char *name;
int var_is_special, quoted;
int *quoted_dollar_atp, *contains_dollar_at;
{
char *t;
WORD_DESC *w;
SHELL_VAR *v;
/* See if it's a nameref first, behave in ksh93-compatible fashion.
There is at least one incompatibility: given ${!foo[0]} where foo=bar,
bash performs an indirect lookup on foo[0] and expands the result;
ksh93 expands bar[0]. We could do that here -- there are enough usable
primitives to do that -- but do not at this point. */
if (var_is_special == 0 && (v = find_variable_last_nameref (name, 0)))
{
if (nameref_p (v) && (t = nameref_cell (v)) && *t)
{
w = alloc_word_desc ();
w->word = savestring (t);
w->flags = 0;
return w;
}
}
/* An indirect reference to a positional parameter or a special parameter
is ok. Indirect references to array references, as explained above, are
ok (currently). Only references to unset variables are errors at this
point. */
if (legal_identifier (name) && v == 0)
{
report_error (_("%s: invalid indirect expansion"), name);
w = alloc_word_desc ();
w->word = &expand_param_error;
w->flags = 0;
return (w);
}
t = parameter_brace_find_indir (name, var_is_special, quoted, 0);
chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
#if defined (ARRAY_VARS)
/* Array references to unset variables are also an error */
if (t == 0 && valid_array_reference (name, 0))
{
v = array_variable_part (name, 0, (char **)0, (int *)0);
if (v == 0)
{
report_error (_("%s: invalid indirect expansion"), name);
w = alloc_word_desc ();
w->word = &expand_param_error;
w->flags = 0;
return (w);
}
else
return (WORD_DESC *)NULL;
}
#endif
if (t == 0)
return (WORD_DESC *)NULL;
if (valid_brace_expansion_word (t, SPECIAL_VAR (t, 0)) == 0)
{
report_error (_("%s: invalid variable name"), t);
free (t);
w = alloc_word_desc ();
w->word = &expand_param_error;
w->flags = 0;
return (w);
}
w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0, 0);
free (t);
return w;
}
/* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
depending on the value of C, the separating character. C can be one of
"-", "+", or "=". QUOTED is true if the entire brace expression occurs
between double quotes. */
static WORD_DESC *
parameter_brace_expand_rhs (name, value, op, quoted, pflags, qdollaratp, hasdollarat)
char *name, *value;
int op, quoted, pflags, *qdollaratp, *hasdollarat;
{
WORD_DESC *w;
WORD_LIST *l;
char *t, *t1, *temp, *vname;
int l_hasdollat, sindex;
SHELL_VAR *v;
/*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/
/* If the entire expression is between double quotes, we want to treat
the value as a double-quoted string, with the exception that we strip
embedded unescaped double quotes (for sh backwards compatibility). */
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
{
sindex = 0;
temp = string_extract_double_quoted (value, &sindex, SX_STRIPDQ);
}
else
temp = value;
w = alloc_word_desc ();
l_hasdollat = 0;
l = *temp ? expand_string_for_rhs (temp, quoted, op, pflags, &l_hasdollat, (int *)NULL)
: (WORD_LIST *)0;
if (hasdollarat)
*hasdollarat = l_hasdollat || (l && l->next);
if (temp != value)
free (temp);
if (l)
{
/* If l->next is not null, we know that TEMP contained "$@", since that
is the only expansion that creates more than one word. */
if (qdollaratp && ((l_hasdollat && quoted) || l->next))
{
/*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/
*qdollaratp = 1;
}
/* The expansion of TEMP returned something. We need to treat things
slightly differently if L_HASDOLLAT is non-zero. If we have "$@",
the individual words have already been quoted. We need to turn them
into a string with the words separated by the first character of
$IFS without any additional quoting, so string_list_dollar_at won't
do the right thing. If IFS is null, we want "$@" to split into
separate arguments, not be concatenated, so we use string_list_internal
and mark the word to be split on spaces later. We use
string_list_dollar_star for "$@" otherwise. */
if (l->next && ifs_is_null)
{
temp = string_list_internal (l, " ");
w->flags |= W_SPLITSPACE;
}
else if (l_hasdollat || l->next)
temp = string_list_dollar_star (l, quoted, 0);
else
{
temp = string_list (l);
if (temp && (QUOTED_NULL (temp) == 0) && (l->word->flags & W_SAWQUOTEDNULL))
w->flags |= W_SAWQUOTEDNULL; /* XXX */
}
/* If we have a quoted null result (QUOTED_NULL(temp)) and the word is
a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the
flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the
expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
(which is more paranoia than anything else), we need to return the
quoted null string and set the flags to indicate it. */
if (l->next == 0 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp) && QUOTED_NULL (l->word->word) && (l->word->flags & W_HASQUOTEDNULL))
{
w->flags |= W_HASQUOTEDNULL;
/*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/
/* If we return a quoted null with L_HASDOLLARAT, we either have a
construct like "${@-$@}" or "${@-${@-$@}}" with no positional
parameters or a quoted expansion of "$@" with $1 == ''. In either
case, we don't want to enable special handling of $@. */
if (qdollaratp && l_hasdollat)
*qdollaratp = 0;
}
dispose_words (l);
}
else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && l_hasdollat)
{
/* Posix interp 221 changed the rules on this. The idea is that
something like "$xxx$@" should expand the same as "${foo-$xxx$@}"
when foo and xxx are unset. The problem is that it's not in any
way backwards compatible and few other shells do it. We're eventually
going to try and split the difference (heh) a little bit here. */
/* l_hasdollat == 1 means we saw a quoted dollar at. */
/* The brace expansion occurred between double quotes and there was
a $@ in TEMP. It does not matter if the $@ is quoted, as long as
it does not expand to anything. In this case, we want to return
a quoted empty string. Posix interp 888 */
temp = make_quoted_char ('\0');
w->flags |= W_HASQUOTEDNULL;
/*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/
}
else
temp = (char *)NULL;
if (op == '-' || op == '+')
{
w->word = temp;
return w;
}
/* op == '=' */
t1 = temp ? dequote_string (temp) : savestring ("");
free (temp);
/* bash-4.4/5.0 */
vname = name;
if (*name == '!' &&
(legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) || VALID_INDIR_PARAM (name[1])))
{
vname = parameter_brace_find_indir (name + 1, SPECIAL_VAR (name, 1), quoted, 1);
if (vname == 0 || *vname == 0)
{
report_error (_("%s: invalid indirect expansion"), name);
free (vname);
free (t1);
dispose_word (w);
return &expand_wdesc_error;
}
if (legal_identifier (vname) == 0)
{
report_error (_("%s: invalid variable name"), vname);
free (vname);
free (t1);
dispose_word (w);
return &expand_wdesc_error;
}
}
#if defined (ARRAY_VARS)
if (valid_array_reference (vname, 0))
v = assign_array_element (vname, t1, 0);
else
#endif /* ARRAY_VARS */
v = bind_variable (vname, t1, 0);
if (v == 0 || readonly_p (v) || noassign_p (v)) /* expansion error */
{
if ((v == 0 || readonly_p (v)) && interactive_shell == 0 && posixly_correct)
{
last_command_exit_value = EXECUTION_FAILURE;
exp_jump_to_top_level (FORCE_EOF);
}
else
{
if (vname != name)
free (vname);
last_command_exit_value = EX_BADUSAGE;
exp_jump_to_top_level (DISCARD);
}
}
stupidly_hack_special_variables (vname);
if (vname != name)
free (vname);
/* From Posix group discussion Feb-March 2010. Issue 7 0000221 */
/* If we are double-quoted or if we are not going to be performing word
splitting, we want to quote the value we return appropriately, like
the other expansions this function handles. */
w->word = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (t1) : quote_escapes (t1);
free (t1);
return w;
}
/* Deal with the right hand side of a ${name:?value} expansion in the case
that NAME is null or not set. If VALUE is non-null it is expanded and
used as the error message to print, otherwise a standard message is
printed. */
static void
parameter_brace_expand_error (name, value, check_null)
char *name, *value;
int check_null;
{
WORD_LIST *l;
char *temp;
last_command_exit_value = EXECUTION_FAILURE; /* ensure it's non-zero */
if (value && *value)
{
l = expand_string (value, 0);
temp = string_list (l);
report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
FREE (temp);
dispose_words (l);
}
else if (check_null == 0)
report_error (_("%s: parameter not set"), name);
else
report_error (_("%s: parameter null or not set"), name);
/* Free the data we have allocated during this expansion, since we
are about to longjmp out. */
free (name);
FREE (value);
}
/* Return 1 if NAME is something for which parameter_brace_expand_length is
OK to do. */
static int
valid_length_expression (name)
char *name;
{
return (name[1] == '\0' || /* ${#} */
((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */
(DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
#if defined (ARRAY_VARS)
valid_array_reference (name + 1, 0) || /* ${#a[7]} */
#endif
legal_identifier (name + 1)); /* ${#PS1} */
}
/* Handle the parameter brace expansion that requires us to return the
length of a parameter. */
static intmax_t
parameter_brace_expand_length (name)
char *name;
{
char *t, *newname;
intmax_t number, arg_index;
WORD_LIST *list;
#if defined (ARRAY_VARS)
SHELL_VAR *var;
#endif
if (name[1] == '\0') /* ${#} */
number = number_of_args ();
else if (DOLLAR_AT_STAR (name[1]) && name[2] == '\0') /* ${#@}, ${#*} */
number = number_of_args ();
else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
{
/* Take the lengths of some of the shell's special parameters. */
switch (name[1])
{
case '-':
t = which_set_flags ();
break;
case '?':
t = itos (last_command_exit_value);
break;
case '$':
t = itos (dollar_dollar_pid);
break;
case '!':
if (last_asynchronous_pid == NO_PID)
t = (char *)NULL; /* XXX - error if set -u set? */
else
t = itos (last_asynchronous_pid);
break;
case '#':
t = itos (number_of_args ());
break;
}
number = STRLEN (t);
FREE (t);
}
#if defined (ARRAY_VARS)
else if (valid_array_reference (name + 1, 0))
number = array_length_reference (name + 1);
#endif /* ARRAY_VARS */
else
{
number = 0;
if (legal_number (name + 1, &arg_index)) /* ${#1} */
{
t = get_dollar_var_value (arg_index);
if (t == 0 && unbound_vars_is_error)
return INTMAX_MIN;
number = MB_STRLEN (t);
FREE (t);
}
#if defined (ARRAY_VARS)
else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var)))
{
if (assoc_p (var))
t = assoc_reference (assoc_cell (var), "0");
else
t = array_reference (array_cell (var), 0);
if (t == 0 && unbound_vars_is_error)
return INTMAX_MIN;
number = MB_STRLEN (t);
}
#endif
else /* ${#PS1} */
{
newname = savestring (name);
newname[0] = '$';
list = expand_string (newname, Q_DOUBLE_QUOTES);
t = list ? string_list (list) : (char *)NULL;
free (newname);
if (list)
dispose_words (list);
number = t ? MB_STRLEN (t) : 0;
FREE (t);
}
}
return (number);
}
/* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
so we do some ad-hoc parsing of an arithmetic expression to find
the first DELIM, instead of using strchr(3). Two rules:
1. If the substring contains a `(', read until closing `)'.
2. If the substring contains a `?', read past one `:' for each `?'.
The SD_ARITHEXP flag to skip_to_delim takes care of doing this.
*/
static char *
skiparith (substr, delim)
char *substr;
int delim;
{
int i;
char delims[2];
delims[0] = delim;
delims[1] = '\0';
i = skip_to_delim (substr, 0, delims, SD_ARITHEXP);
return (substr + i);
}
/* Verify and limit the start and end of the desired substring. If
VTYPE == 0, a regular shell variable is being used; if it is 1,
then the positional parameters are being used; if it is 2, then
VALUE is really a pointer to an array variable that should be used.
Return value is 1 if both values were OK, 0 if there was a problem
with an invalid expression, or -1 if the values were out of range. */
static int
verify_substring_values (v, value, substr, vtype, e1p, e2p)
SHELL_VAR *v;
char *value, *substr;
int vtype;
intmax_t *e1p, *e2p;
{
char *t, *temp1, *temp2;
arrayind_t len;
int expok;
#if defined (ARRAY_VARS)
ARRAY *a;
HASH_TABLE *h;
#endif
/* duplicate behavior of strchr(3) */
t = skiparith (substr, ':');
if (*t && *t == ':')
*t = '\0';
else
t = (char *)0;
temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES);
*e1p = evalexp (temp1, 0, &expok); /* XXX - EXP_EXPANDED? */
free (temp1);
if (expok == 0)
return (0);
len = -1; /* paranoia */
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
len = MB_STRLEN (value);
break;
case VT_POSPARMS:
len = number_of_args () + 1;
if (*e1p == 0)
len++; /* add one arg if counting from $0 */
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
/* For arrays, the first value deals with array indices. Negative
offsets count from one past the array's maximum index. Associative
arrays treat the number of elements as the maximum index. */
if (assoc_p (v))
{
h = assoc_cell (v);
len = assoc_num_elements (h) + (*e1p < 0);
}
else
{
a = (ARRAY *)value;
len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */
}
break;
#endif
}
if (len == -1) /* paranoia */
return -1;
if (*e1p < 0) /* negative offsets count from end */
*e1p += len;
if (*e1p > len || *e1p < 0)
return (-1);
#if defined (ARRAY_VARS)
/* For arrays, the second offset deals with the number of elements. */
if (vtype == VT_ARRAYVAR)
len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a);
#endif
if (t)
{
t++;
temp2 = savestring (t);
temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
free (temp2);
t[-1] = ':';
*e2p = evalexp (temp1, 0, &expok); /* XXX - EXP_EXPANDED? */
free (temp1);
if (expok == 0)
return (0);
/* Should we allow positional parameter length < 0 to count backwards
from end of positional parameters? */
#if 1
if ((vtype == VT_ARRAYVAR || vtype == VT_POSPARMS) && *e2p < 0)
#else
/* XXX - TAG: bash-5.1 */
if (vtype == VT_ARRAYVAR && *e2p < 0)
#endif
{
internal_error (_("%s: substring expression < 0"), t);
return (0);
}
#if defined (ARRAY_VARS)
/* In order to deal with sparse arrays, push the intelligence about how
to deal with the number of elements desired down to the array-
specific functions. */
if (vtype != VT_ARRAYVAR)
#endif
{
if (*e2p < 0)
{
*e2p += len;
if (*e2p < 0 || *e2p < *e1p)
{
internal_error (_("%s: substring expression < 0"), t);
return (0);
}
}
else
*e2p += *e1p; /* want E2 chars starting at E1 */
if (*e2p > len)
*e2p = len;
}
}
else
*e2p = len;
return (1);
}
/* Return the type of variable specified by VARNAME (simple variable,
positional param, or array variable). Also return the value specified
by VARNAME (value of a variable or a reference to an array element).
QUOTED is the standard description of quoting state, using Q_* defines.
FLAGS is currently a set of flags to pass to array_value. If IND is
non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
passed to array_value so the array index is not computed again.
If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
characters in the value are quoted with CTLESC and takes appropriate
steps. For convenience, *VALP is set to the dequoted VALUE. */
static int
get_var_and_type (varname, value, ind, quoted, flags, varp, valp)
char *varname, *value;
arrayind_t ind;
int quoted, flags;
SHELL_VAR **varp;
char **valp;
{
int vtype, want_indir;
char *temp, *vname;
SHELL_VAR *v;
arrayind_t lind;
want_indir = *varname == '!' &&
(legal_variable_starter ((unsigned char)varname[1]) || DIGIT (varname[1])
|| VALID_INDIR_PARAM (varname[1]));
if (want_indir)
vname = parameter_brace_find_indir (varname+1, SPECIAL_VAR (varname, 1), quoted, 1);
/* XXX - what if vname == 0 || *vname == 0 ? */
else
vname = varname;
if (vname == 0)
{
vtype = VT_VARIABLE;
*varp = (SHELL_VAR *)NULL;
*valp = (char *)NULL;
return (vtype);
}
/* This sets vtype to VT_VARIABLE or VT_POSPARMS */
vtype = STR_DOLLAR_AT_STAR (vname);
if (vtype == VT_POSPARMS && vname[0] == '*')
vtype |= VT_STARSUB;
*varp = (SHELL_VAR *)NULL;
#if defined (ARRAY_VARS)
if (valid_array_reference (vname, 0))
{
v = array_variable_part (vname, 0, &temp, (int *)0);
/* If we want to signal array_value to use an already-computed index,
set LIND to that index */
lind = (ind != INTMAX_MIN && (flags & AV_USEIND)) ? ind : 0;
if (v && invisible_p (v))
{
vtype = VT_ARRAYMEMBER;
*varp = (SHELL_VAR *)NULL;
*valp = (char *)NULL;
}
if (v && (array_p (v) || assoc_p (v)))
{
if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == RBRACK)
{
/* Callers have to differentiate between indexed and associative */
vtype = VT_ARRAYVAR;
if (temp[0] == '*')
vtype |= VT_STARSUB;
*valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v);
}
else
{
vtype = VT_ARRAYMEMBER;
*valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
}
*varp = v;
}
else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == RBRACK))
{
vtype = VT_VARIABLE;
*varp = v;
if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
*valp = value ? dequote_string (value) : savestring ("");
else
*valp = value ? dequote_escapes (value) : (char *)NULL;
}
else
{
vtype = VT_ARRAYMEMBER;
*varp = v;
*valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
}
}
else if ((v = find_variable (vname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v)))
{
vtype = VT_ARRAYMEMBER;
*varp = v;
*valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0);
}
else
#endif
{
if (value && vtype == VT_VARIABLE)
{
*varp = find_variable (vname);
if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
*valp = dequote_string (value);
else
*valp = dequote_escapes (value);
}
else
*valp = value;
}
if (want_indir)
free (vname);
return vtype;
}
/***********************************************************/
/* */
/* Functions to perform transformations on variable values */
/* */
/***********************************************************/
static char *
string_var_assignment (v, s)
SHELL_VAR *v;
char *s;
{
char flags[MAX_ATTRIBUTES], *ret, *val;
int i;
val = sh_quote_reusable (s, 0);
i = var_attribute_string (v, 0, flags);
ret = (char *)xmalloc (i + strlen (val) + strlen (v->name) + 16 + MAX_ATTRIBUTES);
if (i > 0)
sprintf (ret, "declare -%s %s=%s", flags, v->name, val);
else
sprintf (ret, "%s=%s", v->name, val);
free (val);
return ret;
}
#if defined (ARRAY_VARS)
static char *
array_var_assignment (v, itype, quoted)
SHELL_VAR *v;
int itype, quoted;
{
char *ret, *val, flags[MAX_ATTRIBUTES];
int i;
if (v == 0)
return (char *)NULL;
val = array_p (v) ? array_to_assign (array_cell (v), 0)
: assoc_to_assign (assoc_cell (v), 0);
if (val == 0)
{
val = (char *)xmalloc (3);
val[0] = LPAREN;
val[1] = RPAREN;
val[2] = 0;
}
else
{
ret = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (val) : quote_escapes (val);
free (val);
val = ret;
}
i = var_attribute_string (v, 0, flags);
ret = (char *)xmalloc (i + strlen (val) + strlen (v->name) + 16);
sprintf (ret, "declare -%s %s=%s", flags, v->name, val);
free (val);
return ret;
}
#endif
static char *
pos_params_assignment (list, itype, quoted)
WORD_LIST *list;
int itype;
int quoted;
{
char *temp, *ret;
/* first, we transform the list to quote each word. */
temp = list_transform ('Q', (SHELL_VAR *)0, list, itype, quoted);
ret = (char *)xmalloc (strlen (temp) + 8);
strcpy (ret, "set -- ");
strcpy (ret + 7, temp);
free (temp);
return ret;
}
static char *
string_transform (xc, v, s)
int xc;
SHELL_VAR *v;
char *s;
{
char *ret, flags[MAX_ATTRIBUTES], *t;
int i;
if (((xc == 'A' || xc == 'a') && v == 0) || (xc != 'a' && s == 0))
return (char *)NULL;
switch (xc)
{
/* Transformations that interrogate the variable */
case 'a':
i = var_attribute_string (v, 0, flags);
ret = (i > 0) ? savestring (flags) : (char *)NULL;
break;
case 'A':
ret = string_var_assignment (v, s);
break;
/* Transformations that modify the variable's value */
case 'E':
t = ansiexpand (s, 0, strlen (s), (int *)0);
ret = dequote_escapes (t);
free (t);
break;
case 'P':
ret = decode_prompt_string (s);
break;
case 'Q':
ret = sh_quote_reusable (s, 0);
break;
default:
ret = (char *)NULL;
break;
}
return ret;
}
static char *
list_transform (xc, v, list, itype, quoted)
int xc;
SHELL_VAR *v;
WORD_LIST *list;
int itype, quoted;
{
WORD_LIST *new, *l;
WORD_DESC *w;
char *tword;
int qflags;
for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
{
tword = string_transform (xc, v, l->word->word);
w = alloc_word_desc ();
w->word = tword ? tword : savestring (""); /* XXX */
new = make_word_list (w, new);
}
l = REVERSE_LIST (new, WORD_LIST *);
qflags = quoted;
/* If we are expanding in a context where word splitting will not be
performed, treat as quoted. This changes how $* will be expanded. */
if (itype == '*' && expand_no_split_dollar_star && ifs_is_null)
qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */
tword = string_list_pos_params (itype, l, qflags);
dispose_words (l);
return (tword);
}
static char *
parameter_list_transform (xc, itype, quoted)
int xc;
int itype;
int quoted;
{
char *ret;
WORD_LIST *list;
list = list_rest_of_args ();
if (list == 0)
return ((char *)NULL);
if (xc == 'A')
ret = pos_params_assignment (list, itype, quoted);
else
ret = list_transform (xc, (SHELL_VAR *)0, list, itype, quoted);
dispose_words (list);
return (ret);
}
#if defined (ARRAY_VARS)
static char *
array_transform (xc, var, varname, quoted)
int xc;
SHELL_VAR *var;
char *varname; /* so we can figure out how it's indexed */
int quoted;
{
ARRAY *a;
HASH_TABLE *h;
int itype;
char *ret;
WORD_LIST *list;
SHELL_VAR *v;
/* compute itype from varname here */
v = array_variable_part (varname, 0, &ret, 0);
/* XXX */
if (v && invisible_p (v))
return ((char *)NULL);
itype = ret[0];
if (xc == 'A')
return (array_var_assignment (v, itype, quoted));
a = (v && array_p (v)) ? array_cell (v) : 0;
h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
if (list == 0)
return ((char *)NULL);
ret = list_transform (xc, v, list, itype, quoted);
dispose_words (list);
return ret;
}
#endif /* ARRAY_VARS */
static char *
parameter_brace_transform (varname, value, ind, xform, rtype, quoted, pflags, flags)
char *varname, *value;
int ind;
char *xform;
int rtype, quoted, pflags, flags;
{
int vtype, xc;
char *temp1, *val, *oname;
SHELL_VAR *v;
xc = xform[0];
if (value == 0 && xc != 'A' && xc != 'a')
return ((char *)NULL);
oname = this_command_name;
this_command_name = varname;
vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
if (vtype == -1)
{
this_command_name = oname;
return ((char *)NULL);
}
/* check for valid values of xc */
switch (xc)
{
case 'a': /* expand to a string with just attributes */
case 'A': /* expand as an assignment statement with attributes */
case 'E': /* expand like $'...' */
case 'P': /* expand like prompt string */
case 'Q': /* quote reusably */
break;
default:
this_command_name = oname;
return &expand_param_error;
}
/* If we are asked to display the attributes of an unset variable, V will
be NULL after the call to get_var_and_type. Double-check here. */
if (xc == 'a' && vtype == VT_VARIABLE && varname && v == 0)
v = find_variable (varname);
temp1 = (char *)NULL; /* shut up gcc */
switch (vtype & ~VT_STARSUB)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
temp1 = string_transform (xc, v, val);
if (vtype == VT_VARIABLE)
FREE (val);
if (temp1)
{
val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
? quote_string (temp1)
: quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
temp1 = array_transform (xc, v, varname, quoted);
if (temp1 && quoted == 0 && ifs_is_null)
{
/* Posix interp 888 */
}
else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
{
val = quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
#endif
case VT_POSPARMS:
temp1 = parameter_list_transform (xc, varname[0], quoted);
if (temp1 && quoted == 0 && ifs_is_null)
{
/* Posix interp 888 */
}
else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
{
val = quote_escapes (temp1);
free (temp1);
temp1 = val;
}
break;
}
this_command_name = oname;
return temp1;
}
/******************************************************/
/* */
/* Functions to extract substrings of variable values */
/* */
/******************************************************/
#if defined (HANDLE_MULTIBYTE)
/* Character-oriented rather than strictly byte-oriented substrings. S and
E, rather being strict indices into STRING, indicate character (possibly
multibyte character) positions that require calculation.
Used by the ${param:offset[:length]} expansion. */
static char *
mb_substring (string, s, e)
char *string;
int s, e;
{
char *tt;
int start, stop, i;
size_t slen;
DECLARE_MBSTATE;
start = 0;
/* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
i = s;
while (string[start] && i--)
ADVANCE_CHAR (string, slen, start);
stop = start;
i = e - s;
while (string[stop] && i--)
ADVANCE_CHAR (string, slen, stop);
tt = substring (string, start, stop);
return tt;
}
#endif
/* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
is `@', use the positional parameters; otherwise, use the value of
VARNAME. If VARNAME is an array variable, use the array elements. */
static char *
parameter_brace_substring (varname, value, ind, substr, quoted, pflags, flags)
char *varname, *value;
int ind;
char *substr;
int quoted, pflags, flags;
{
intmax_t e1, e2;
int vtype, r, starsub;
char *temp, *val, *tt, *oname;
SHELL_VAR *v;
if (value == 0 && ((varname[0] != '@' && varname[0] != '*') || varname[1]))
return ((char *)NULL);
oname = this_command_name;
this_command_name = varname;
vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
if (vtype == -1)
{
this_command_name = oname;
return ((char *)NULL);
}
starsub = vtype & VT_STARSUB;
vtype &= ~VT_STARSUB;
r = verify_substring_values (v, val, substr, vtype, &e1, &e2);
this_command_name = oname;
if (r <= 0)
{
if (vtype == VT_VARIABLE)
FREE (val);
return ((r == 0) ? &expand_param_error : (char *)NULL);
}
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
#if defined (HANDLE_MULTIBYTE)
if (MB_CUR_MAX > 1)
tt = mb_substring (val, e1, e2);
else
#endif
tt = substring (val, e1, e2);
if (vtype == VT_VARIABLE)
FREE (val);
if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
temp = quote_string (tt);
else
temp = tt ? quote_escapes (tt) : (char *)NULL;
FREE (tt);
break;
case VT_POSPARMS:
case VT_ARRAYVAR:
if (vtype == VT_POSPARMS)
tt = pos_params (varname, e1, e2, quoted);
#if defined (ARRAY_VARS)
/* assoc_subrange and array_subrange both call string_list_pos_params,
so we can treat this case just like VT_POSPARAMS. */
else if (assoc_p (v))
/* we convert to list and take first e2 elements starting at e1th
element -- officially undefined for now */
tt = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted);
else
/* We want E2 to be the number of elements desired (arrays can be
sparse, so verify_substring_values just returns the numbers
specified and we rely on array_subrange to understand how to
deal with them). */
tt = array_subrange (array_cell (v), e1, e2, starsub, quoted);
#endif
/* We want to leave this alone in every case where pos_params/
string_list_pos_params quotes the list members */
if (tt && quoted == 0 && ifs_is_null)
{
temp = tt; /* Posix interp 888 */
}
else if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
{
temp = tt ? quote_escapes (tt) : (char *)NULL;
FREE (tt);
}
else
temp = tt;
break;
default:
temp = (char *)NULL;
}
return temp;
}
/****************************************************************/
/* */
/* Functions to perform pattern substitution on variable values */
/* */
/****************************************************************/
#ifdef INCLUDE_UNUSED
static int
shouldexp_replacement (s)
char *s;
{
register char *p;
for (p = s; p && *p; p++)
{
if (*p == '\\')
p++;
else if (*p == '&')
return 1;
}
return 0;
}
#endif
char *
pat_subst (string, pat, rep, mflags)
char *string, *pat, *rep;
int mflags;
{
char *ret, *s, *e, *str, *rstr, *mstr;
int rptr, mtype, rxpand, mlen;
size_t rsize, l, replen, rslen;
if (string == 0)
return (savestring (""));
mtype = mflags & MATCH_TYPEMASK;
#if 0 /* bash-4.2 ? */
rxpand = (rep && *rep) ? shouldexp_replacement (rep) : 0;
#else
rxpand = 0;
#endif
/* Special cases:
* 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
* with REP and return the result.
* 2. A null pattern with mtype == MATCH_END means to append REP to
* STRING and return the result.
* 3. A null STRING with a matching pattern means to append REP to
* STRING and return the result.
* These don't understand or process `&' in the replacement string.
*/
if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
{
replen = STRLEN (rep);
l = STRLEN (string);
ret = (char *)xmalloc (replen + l + 2);
if (replen == 0)
strcpy (ret, string);
else if (mtype == MATCH_BEG)
{
strcpy (ret, rep);
strcpy (ret + replen, string);
}
else
{
strcpy (ret, string);
strcpy (ret + l, rep);
}
return (ret);
}
else if (*string == 0 && (match_pattern (string, pat, mtype, &s, &e) != 0))
{
replen = STRLEN (rep);
ret = (char *)xmalloc (replen + 1);
if (replen == 0)
ret[0] = '\0';
else
strcpy (ret, rep);
return (ret);
}
ret = (char *)xmalloc (rsize = 64);
ret[0] = '\0';
for (replen = STRLEN (rep), rptr = 0, str = string; *str;)
{
if (match_pattern (str, pat, mtype, &s, &e) == 0)
break;
l = s - str;
if (rep && rxpand)
{
int x;
mlen = e - s;
mstr = xmalloc (mlen + 1);
for (x = 0; x < mlen; x++)
mstr[x] = s[x];
mstr[mlen] = '\0';
rstr = strcreplace (rep, '&', mstr, 0);
free (mstr);
rslen = strlen (rstr);
}
else
{
rstr = rep;
rslen = replen;
}
RESIZE_MALLOCED_BUFFER (ret, rptr, (l + rslen), rsize, 64);
/* OK, now copy the leading unmatched portion of the string (from
str to s) to ret starting at rptr (the current offset). Then copy
the replacement string at ret + rptr + (s - str). Increment
rptr (if necessary) and str and go on. */
if (l)
{
strncpy (ret + rptr, str, l);
rptr += l;
}
if (replen)
{
strncpy (ret + rptr, rstr, rslen);
rptr += rslen;
}
str = e; /* e == end of match */
if (rstr != rep)
free (rstr);
if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
break;
if (s == e)
{
/* On a zero-length match, make sure we copy one character, since
we increment one character to avoid infinite recursion. */
RESIZE_MALLOCED_BUFFER (ret, rptr, 1, rsize, 64);
ret[rptr++] = *str++;
e++; /* avoid infinite recursion on zero-length match */
}
}
/* Now copy the unmatched portion of the input string */
if (str && *str)
{
RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
strcpy (ret + rptr, str);
}
else
ret[rptr] = '\0';
return ret;
}
/* Do pattern match and replacement on the positional parameters. */
static char *
pos_params_pat_subst (string, pat, rep, mflags)
char *string, *pat, *rep;
int mflags;
{
WORD_LIST *save, *params;
WORD_DESC *w;
char *ret;
int pchar, qflags;
save = params = list_rest_of_args ();
if (save == 0)
return ((char *)NULL);
for ( ; params; params = params->next)
{
ret = pat_subst (params->word->word, pat, rep, mflags);
w = alloc_word_desc ();
w->word = ret ? ret : savestring ("");
dispose_word (params->word);
params->word = w;
}
pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
/* If we are expanding in a context where word splitting will not be
performed, treat as quoted. This changes how $* will be expanded. */
if (pchar == '*' && (mflags & MATCH_ASSIGNRHS) && expand_no_split_dollar_star && ifs_is_null)
qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */
ret = string_list_pos_params (pchar, save, qflags);
dispose_words (save);
return (ret);
}
/* Perform pattern substitution on VALUE, which is the expansion of
VARNAME. PATSUB is an expression supplying the pattern to match
and the string to substitute. QUOTED is a flags word containing
the type of quoting currently in effect. */
static char *
parameter_brace_patsub (varname, value, ind, patsub, quoted, pflags, flags)
char *varname, *value;
int ind;
char *patsub;
int quoted, pflags, flags;
{
int vtype, mflags, starsub, delim;
char *val, *temp, *pat, *rep, *p, *lpatsub, *tt, *oname;
SHELL_VAR *v;
if (value == 0)
return ((char *)NULL);
oname = this_command_name;
this_command_name = varname; /* error messages */
vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
if (vtype == -1)
{
this_command_name = oname;
return ((char *)NULL);
}
starsub = vtype & VT_STARSUB;
vtype &= ~VT_STARSUB;
mflags = 0;
/* PATSUB is never NULL when this is called. */
if (*patsub == '/')
{
mflags |= MATCH_GLOBREP;
patsub++;
}
/* Malloc this because expand_string_if_necessary or one of the expansion
functions in its call chain may free it on a substitution error. */
lpatsub = savestring (patsub);
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
mflags |= MATCH_QUOTED;
if (starsub)
mflags |= MATCH_STARSUB;
if (pflags & PF_ASSIGNRHS)
mflags |= MATCH_ASSIGNRHS;
/* If the pattern starts with a `/', make sure we skip over it when looking
for the replacement delimiter. */
delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0);
if (lpatsub[delim] == '/')
{
lpatsub[delim] = 0;
rep = lpatsub + delim + 1;
}
else
rep = (char *)NULL;
if (rep && *rep == '\0')
rep = (char *)NULL;
/* Perform the same expansions on the pattern as performed by the
pattern removal expansions. */
pat = getpattern (lpatsub, quoted, 1);
if (rep)
{
/* We want to perform quote removal on the expanded replacement even if
the entire expansion is double-quoted because the parser and string
extraction functions treated quotes in the replacement string as
special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */
if (shell_compatibility_level > 42)
rep = expand_string_if_necessary (rep, quoted & ~(Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT), expand_string_unsplit);
/* This is the bash-4.2 code. */
else if ((mflags & MATCH_QUOTED) == 0)
rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
else
rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
}
/* ksh93 doesn't allow the match specifier to be a part of the expanded
pattern. This is an extension. Make sure we don't anchor the pattern
at the beginning or end of the string if we're doing global replacement,
though. */
p = pat;
if (mflags & MATCH_GLOBREP)
mflags |= MATCH_ANY;
else if (pat && pat[0] == '#')
{
mflags |= MATCH_BEG;
p++;
}
else if (pat && pat[0] == '%')
{
mflags |= MATCH_END;
p++;
}
else
mflags |= MATCH_ANY;
/* OK, we now want to substitute REP for PAT in VAL. If
flags & MATCH_GLOBREP is non-zero, the substitution is done
everywhere, otherwise only the first occurrence of PAT is
replaced. The pattern matching code doesn't understand
CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
values passed in (VT_VARIABLE) so the pattern substitution
code works right. We need to requote special chars after
we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
other cases if QUOTED == 0, since the posparams and arrays
indexed by * or @ do special things when QUOTED != 0. */
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
temp = pat_subst (val, p, rep, mflags);
if (vtype == VT_VARIABLE)
FREE (val);
if (temp)
{
tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
free (temp);
temp = tt;
}
break;
case VT_POSPARMS:
/* This does the right thing for the case where we are not performing
word splitting. MATCH_STARSUB restricts it to ${* /foo/bar}, and
pos_params_pat_subst/string_list_pos_params will do the right thing
in turn for the case where ifs_is_null. Posix interp 888 */
if ((pflags & PF_NOSPLIT2) && (mflags & MATCH_STARSUB))
mflags |= MATCH_ASSIGNRHS;
temp = pos_params_pat_subst (val, p, rep, mflags);
if (temp && quoted == 0 && ifs_is_null)
{
/* Posix interp 888 */
}
else if (temp && (mflags & MATCH_QUOTED) == 0)
{
tt = quote_escapes (temp);
free (temp);
temp = tt;
}
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
/* If we are expanding in a context where word splitting will not be
performed, treat as quoted. This changes how ${A[*]} will be
expanded to make it identical to $*. */
if ((mflags & MATCH_STARSUB) && (mflags & MATCH_ASSIGNRHS) && ifs_is_null)
mflags |= MATCH_QUOTED; /* Posix interp 888 */
/* these eventually call string_list_pos_params */
if (assoc_p (v))
temp = assoc_patsub (assoc_cell (v), p, rep, mflags);
else
temp = array_patsub (array_cell (v), p, rep, mflags);
if (temp && quoted == 0 && ifs_is_null)
{
/* Posix interp 888 */
}
else if (temp && (mflags & MATCH_QUOTED) == 0)
{
tt = quote_escapes (temp);
free (temp);
temp = tt;
}
break;
#endif
}
FREE (pat);
FREE (rep);
free (lpatsub);
this_command_name = oname;
return temp;
}
/****************************************************************/
/* */
/* Functions to perform case modification on variable values */
/* */
/****************************************************************/
/* Do case modification on the positional parameters. */
static char *
pos_params_modcase (string, pat, modop, mflags)
char *string, *pat;
int modop;
int mflags;
{
WORD_LIST *save, *params;
WORD_DESC *w;
char *ret;
int pchar, qflags;
save = params = list_rest_of_args ();
if (save == 0)
return ((char *)NULL);
for ( ; params; params = params->next)
{
ret = sh_modcase (params->word->word, pat, modop);
w = alloc_word_desc ();
w->word = ret ? ret : savestring ("");
dispose_word (params->word);
params->word = w;
}
pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
/* If we are expanding in a context where word splitting will not be
performed, treat as quoted. This changes how $* will be expanded. */
if (pchar == '*' && (mflags & MATCH_ASSIGNRHS) && ifs_is_null)
qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */
ret = string_list_pos_params (pchar, save, qflags);
dispose_words (save);
return (ret);
}
/* Perform case modification on VALUE, which is the expansion of
VARNAME. MODSPEC is an expression supplying the type of modification
to perform. QUOTED is a flags word containing the type of quoting
currently in effect. */
static char *
parameter_brace_casemod (varname, value, ind, modspec, patspec, quoted, pflags, flags)
char *varname, *value;
int ind, modspec;
char *patspec;
int quoted, pflags, flags;
{
int vtype, starsub, modop, mflags, x;
char *val, *temp, *pat, *p, *lpat, *tt, *oname;
SHELL_VAR *v;
if (value == 0)
return ((char *)NULL);
oname = this_command_name;
this_command_name = varname;
vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
if (vtype == -1)
{
this_command_name = oname;
return ((char *)NULL);
}
starsub = vtype & VT_STARSUB;
vtype &= ~VT_STARSUB;
modop = 0;
mflags = 0;
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
mflags |= MATCH_QUOTED;
if (starsub)
mflags |= MATCH_STARSUB;
if (pflags & PF_ASSIGNRHS)
mflags |= MATCH_ASSIGNRHS;
p = patspec;
if (modspec == '^')
{
x = p && p[0] == modspec;
modop = x ? CASE_UPPER : CASE_UPFIRST;
p += x;
}
else if (modspec == ',')
{
x = p && p[0] == modspec;
modop = x ? CASE_LOWER : CASE_LOWFIRST;
p += x;
}
else if (modspec == '~')
{
x = p && p[0] == modspec;
modop = x ? CASE_TOGGLEALL : CASE_TOGGLE;
p += x;
}
lpat = p ? savestring (p) : 0;
/* Perform the same expansions on the pattern as performed by the
pattern removal expansions. */
pat = lpat ? getpattern (lpat, quoted, 1) : 0;
/* OK, now we do the case modification. */
switch (vtype)
{
case VT_VARIABLE:
case VT_ARRAYMEMBER:
temp = sh_modcase (val, pat, modop);
if (vtype == VT_VARIABLE)
FREE (val);
if (temp)
{
tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
free (temp);
temp = tt;
}
break;
case VT_POSPARMS:
temp = pos_params_modcase (val, pat, modop, mflags);
if (temp && quoted == 0 && ifs_is_null)
{
/* Posix interp 888 */
}
else if (temp && (mflags & MATCH_QUOTED) == 0)
{
tt = quote_escapes (temp);
free (temp);
temp = tt;
}
break;
#if defined (ARRAY_VARS)
case VT_ARRAYVAR:
/* If we are expanding in a context where word splitting will not be
performed, treat as quoted. This changes how ${A[*]} will be
expanded to make it identical to $*. */
if ((mflags & MATCH_STARSUB) && (mflags & MATCH_ASSIGNRHS) && ifs_is_null)
mflags |= MATCH_QUOTED; /* Posix interp 888 */
temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags)
: array_modcase (array_cell (v), pat, modop, mflags);
if (temp && quoted == 0 && ifs_is_null)
{
/* Posix interp 888 */
}
else if (temp && (mflags & MATCH_QUOTED) == 0)
{
tt = quote_escapes (temp);
free (temp);
temp = tt;
}
break;
#endif
}
FREE (pat);
free (lpat);
this_command_name = oname;
return temp;
}
/* Check for unbalanced parens in S, which is the contents of $(( ... )). If
any occur, this must be a nested command substitution, so return 0.
Otherwise, return 1. A valid arithmetic expression must always have a
( before a matching ), so any cases where there are more right parens
means that this must not be an arithmetic expression, though the parser
will not accept it without a balanced total number of parens. */
static int
chk_arithsub (s, len)
const char *s;
int len;
{
int i, count;
DECLARE_MBSTATE;
i = count = 0;
while (i < len)
{
if (s[i] == LPAREN)
count++;
else if (s[i] == RPAREN)
{
count--;
if (count < 0)
return 0;
}
switch (s[i])
{
default:
ADVANCE_CHAR (s, len, i);
break;
case '\\':
i++;
if (s[i])
ADVANCE_CHAR (s, len, i);
break;
case '\'':
i = skip_single_quoted (s, len, ++i, 0);
break;
case '"':
i = skip_double_quoted ((char *)s, len, ++i, 0);
break;
}
}
return (count == 0);
}
/****************************************************************/
/* */
/* Functions to perform parameter expansion on a string */
/* */
/****************************************************************/
/* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
static WORD_DESC *
parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
char *string;
int *indexp, quoted, pflags, *quoted_dollar_atp, *contains_dollar_at;
{
int check_nullness, var_is_set, var_is_null, var_is_special;
int want_substring, want_indir, want_patsub, want_casemod;
char *name, *value, *temp, *temp1;
WORD_DESC *tdesc, *ret;
int t_index, sindex, c, tflag, modspec, all_element_arrayref;
intmax_t number;
arrayind_t ind;
temp = temp1 = value = (char *)NULL;
var_is_set = var_is_null = var_is_special = check_nullness = 0;
want_substring = want_indir = want_patsub = want_casemod = 0;
all_element_arrayref = 0;
sindex = *indexp;
t_index = ++sindex;
/* ${#var} doesn't have any of the other parameter expansions on it. */
if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */
name = string_extract (string, &t_index, "}", SX_VARNAME);
else
#if defined (CASEMOD_EXPANSIONS)
/* To enable case-toggling expansions using the `~' operator character
change the 1 to 0. */
# if defined (CASEMOD_CAPCASE)
name = string_extract (string, &t_index, "#%^,~:-=?+/@}", SX_VARNAME);
# else
name = string_extract (string, &t_index, "#%^,:-=?+/@}", SX_VARNAME);
# endif /* CASEMOD_CAPCASE */
#else
name = string_extract (string, &t_index, "#%:-=?+/@}", SX_VARNAME);
#endif /* CASEMOD_EXPANSIONS */
/* Handle ${@[stuff]} now that @ is a word expansion operator. Not exactly
the cleanest code ever. */
if (*name == 0 && sindex == t_index && string[sindex] == '@')
{
name = (char *)xrealloc (name, 2);
name[0] = '@';
name[1] = '\0';
t_index++;
}
else if (*name == '!' && t_index > sindex && string[t_index] == '@' && string[t_index+1] == RBRACE)
{
name = (char *)xrealloc (name, t_index - sindex + 2);
name[t_index - sindex] = '@';
name[t_index - sindex + 1] = '\0';
t_index++;
}
ret = 0;
tflag = 0;
ind = INTMAX_MIN;
/* If the name really consists of a special variable, then make sure
that we have the entire name. We don't allow indirect references
to special variables except `#', `?', `@' and `*'. This clause is
designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more
general. */
if ((sindex == t_index && VALID_SPECIAL_LENGTH_PARAM (string[t_index])) ||
(sindex == t_index && string[sindex] == '#' && VALID_SPECIAL_LENGTH_PARAM (string[sindex + 1])) ||
(sindex == t_index - 1 && string[sindex] == '!' && VALID_INDIR_PARAM (string[t_index])))
{
t_index++;
temp1 = string_extract (string, &t_index, "#%:-=?+/@}", 0);
name = (char *)xrealloc (name, 3 + (strlen (temp1)));
*name = string[sindex];
if (string[sindex] == '!')
{
/* indirect reference of $#, $?, $@, or $* */
name[1] = string[sindex + 1];
strcpy (name + 2, temp1);
}
else
strcpy (name + 1, temp1);
free (temp1);
}
sindex = t_index;
/* Find out what character ended the variable name. Then
do the appropriate thing. */
if (c = string[sindex])
sindex++;
/* If c is followed by one of the valid parameter expansion
characters, move past it as normal. If not, assume that
a substring specification is being given, and do not move
past it. */
if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
{
check_nullness++;
if (c = string[sindex])
sindex++;
}
else if (c == ':' && string[sindex] != RBRACE)
want_substring = 1;
else if (c == '/' /* && string[sindex] != RBRACE */) /* XXX */
want_patsub = 1;
#if defined (CASEMOD_EXPANSIONS)
else if (c == '^' || c == ',' || c == '~')
{
modspec = c;
want_casemod = 1;
}
#endif
/* Catch the valid and invalid brace expressions that made it through the
tests above. */
/* ${#-} is a valid expansion and means to take the length of $-.
Similarly for ${#?} and ${##}... */
if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
{
name = (char *)xrealloc (name, 3);
name[1] = c;
name[2] = '\0';
c = string[sindex++];
}
/* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
member (c, "%:=+/") && string[sindex] == RBRACE)
{
temp = (char *)NULL;
goto bad_substitution; /* XXX - substitution error */
}
/* Indirect expansion begins with a `!'. A valid indirect expansion is
either a variable name, one of the positional parameters or a special
variable that expands to one of the positional parameters. */
want_indir = *name == '!' &&
(legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
|| VALID_INDIR_PARAM (name[1]));
/* Determine the value of this variable whose name is NAME. */
/* Check for special variables, directly referenced. */
if (SPECIAL_VAR (name, want_indir))
var_is_special++;
/* Check for special expansion things, like the length of a parameter */
if (*name == '#' && name[1])
{
/* If we are not pointing at the character just after the
closing brace, then we haven't gotten all of the name.
Since it begins with a special character, this is a bad
substitution. Also check NAME for validity before trying
to go on. */
if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
{
temp = (char *)NULL;
goto bad_substitution; /* substitution error */
}
number = parameter_brace_expand_length (name);
if (number == INTMAX_MIN && unbound_vars_is_error)
{
last_command_exit_value = EXECUTION_FAILURE;
err_unboundvar (name+1);
free (name);
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
free (name);
*indexp = sindex;
if (number < 0)
return (&expand_wdesc_error);
else
{
ret = alloc_word_desc ();
ret->word = itos (number);
return ret;
}
}
/* ${@} is identical to $@. */
if (name[0] == '@' && name[1] == '\0')
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
tflag |= W_DOLLARAT;
}
/* Process ${!PREFIX*} expansion. */
if (want_indir && string[sindex - 1] == RBRACE &&
(string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
legal_variable_starter ((unsigned char) name[1]))
{
char **x;
WORD_LIST *xlist;
temp1 = savestring (name + 1);
number = strlen (temp1);
temp1[number - 1] = '\0';
x = all_variables_matching_prefix (temp1);
xlist = strvec_to_word_list (x, 0, 0);
if (string[sindex - 2] == '*')
temp = string_list_dollar_star (xlist, quoted, 0);
else
{
temp = string_list_dollar_at (xlist, quoted, 0);
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
tflag |= W_DOLLARAT;
}
free (x);
dispose_words (xlist);
free (temp1);
*indexp = sindex;
free (name);
ret = alloc_word_desc ();
ret->word = temp;
ret->flags = tflag; /* XXX */
return ret;
}
#if defined (ARRAY_VARS)
/* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */
if (want_indir && string[sindex - 1] == RBRACE &&
string[sindex - 2] == RBRACK && valid_array_reference (name+1, 0))
{
char *x, *x1;
temp1 = savestring (name + 1);
x = array_variable_name (temp1, 0, &x1, (int *)0);
FREE (x);
if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == RBRACK)
{
temp = array_keys (temp1, quoted); /* handles assoc vars too */
if (x1[0] == '@')
{
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
tflag |= W_DOLLARAT;
}
free (name);
free (temp1);
*indexp = sindex;
ret = alloc_word_desc ();
ret->word = temp;
ret->flags = tflag; /* XXX */
return ret;
}
free (temp1);
}
#endif /* ARRAY_VARS */
/* Make sure that NAME is valid before trying to go on. */
if (valid_brace_expansion_word (want_indir ? name + 1 : name,
var_is_special) == 0)
{
temp = (char *)NULL;
goto bad_substitution; /* substitution error */
}
if (want_indir)
{
tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
{
temp = (char *)NULL;
goto bad_substitution;
}
/* Turn off the W_ARRAYIND flag because there is no way for this function
to return the index we're supposed to be using. */
if (tdesc && tdesc->flags)
tdesc->flags &= ~W_ARRAYIND;
}
else
tdesc = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND|(pflags&(PF_NOSPLIT2|PF_ASSIGNRHS)), &ind);
if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
{
tflag = 0;
tdesc = 0;
}
if (tdesc)
{
temp = tdesc->word;
tflag = tdesc->flags;
dispose_word_desc (tdesc);
}
else
temp = (char *)0;
if (temp == &expand_param_error || temp == &expand_param_fatal)
{
FREE (name);
FREE (value);
return (temp == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
}
#if defined (ARRAY_VARS)
if (valid_array_reference (name, 0))
{
int qflags;
char *t;
qflags = quoted;
/* If in a context where word splitting will not take place, treat as
if double-quoted. Has effects with $* and ${array[*]} */
if (pflags & PF_ASSIGNRHS)
qflags |= Q_DOUBLE_QUOTES;
/* We duplicate a little code here */
t = mbschr (name, LBRACK);
if (t && ALL_ELEMENT_SUB (t[1]) && t[2] == RBRACK)
{
all_element_arrayref = 1;
if (expand_no_split_dollar_star && t[1] == '*') /* XXX */
qflags |= Q_DOUBLE_QUOTES;
}
chk_atstar (name, qflags, quoted_dollar_atp, contains_dollar_at);
}
#endif
var_is_set = temp != (char *)0;
var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
/* XXX - this may not need to be restricted to special variables */
if (check_nullness)
var_is_null |= var_is_set && var_is_special && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp);
#if defined (ARRAY_VARS)
if (check_nullness)
var_is_null |= var_is_set &&
(quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) &&
QUOTED_NULL (temp) &&
valid_array_reference (name, 0) &&
chk_atstar (name, 0, (int *)0, (int *)0);
#endif
/* Get the rest of the stuff inside the braces. */
if (c && c != RBRACE)
{
/* Extract the contents of the ${ ... } expansion
according to the Posix.2 rules. */
value = extract_dollar_brace_string (string, &sindex, quoted, (c == '%' || c == '#' || c =='/' || c == '^' || c == ',' || c ==':') ? SX_POSIXEXP|SX_WORD : SX_WORD);
if (string[sindex] == RBRACE)
sindex++;
else
goto bad_substitution; /* substitution error */
}
else
value = (char *)NULL;
*indexp = sindex;
/* All the cases where an expansion can possibly generate an unbound
variable error. */
if (want_substring || want_patsub || want_casemod || c == '#' || c == '%' || c == RBRACE)
{
if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]) && all_element_arrayref == 0)
{
last_command_exit_value = EXECUTION_FAILURE;
err_unboundvar (name);
FREE (value);
FREE (temp);
free (name);
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
}
/* If this is a substring spec, process it and add the result. */
if (want_substring)
{
temp1 = parameter_brace_substring (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
FREE (value);
FREE (temp);
if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
{
FREE (name);
return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
}
ret = alloc_word_desc ();
ret->word = temp1;
/* We test quoted_dollar_atp because we want variants with double-quoted
"$@" to take a different code path. In fact, we make sure at the end
of expand_word_internal that we're only looking at these flags if
quoted_dollar_at == 0. */
if (temp1 &&
(quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
/* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
FREE (name);
return ret;
}
else if (want_patsub)
{
temp1 = parameter_brace_patsub (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
FREE (value);
FREE (temp);
if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
{
FREE (name);
return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
}
ret = alloc_word_desc ();
ret->word = temp1;
if (temp1 &&
(quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
/* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
FREE (name);
return ret;
}
#if defined (CASEMOD_EXPANSIONS)
else if (want_casemod)
{
temp1 = parameter_brace_casemod (name, temp, ind, modspec, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
FREE (value);
FREE (temp);
if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
{
FREE (name);
return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
}
ret = alloc_word_desc ();
ret->word = temp1;
if (temp1 &&
(quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
/* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
FREE (name);
return ret;
}
#endif
/* Do the right thing based on which character ended the variable name. */
switch (c)
{
default:
case '\0':
bad_substitution:
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("%s: bad substitution"), string ? string : "??");
FREE (value);
FREE (temp);
free (name);
if (shell_compatibility_level <= 43)
return &expand_wdesc_error;
else
return ((posixly_correct && interactive_shell == 0) ? &expand_wdesc_fatal : &expand_wdesc_error);
case RBRACE:
break;
case '@':
temp1 = parameter_brace_transform (name, temp, ind, value, c, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
free (temp);
free (value);
if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
{
free (name);
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("%s: bad substitution"), string ? string : "??");
return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
}
ret = alloc_word_desc ();
ret->word = temp1;
if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
/* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
free (name);
return ret;
case '#': /* ${param#[#]pattern} */
case '%': /* ${param%[%]pattern} */
if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
{
FREE (value);
break;
}
temp1 = parameter_brace_remove_pattern (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
free (temp);
free (value);
ret = alloc_word_desc ();
ret->word = temp1;
if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
/* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
free (name);
return ret;
case '-':
case '=':
case '?':
case '+':
if (var_is_set && var_is_null == 0)
{
/* If the operator is `+', we don't want the value of the named
variable for anything, just the value of the right hand side. */
if (c == '+')
{
/* XXX -- if we're double-quoted and the named variable is "$@",
we want to turn off any special handling of "$@" --
we're not using it, so whatever is on the rhs applies. */
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 0;
if (contains_dollar_at)
*contains_dollar_at = 0;
FREE (temp);
if (value)
{
/* From Posix discussion on austin-group list. Issue 221
requires that backslashes escaping `}' inside
double-quoted ${...} be removed. */
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
quoted |= Q_DOLBRACE;
ret = parameter_brace_expand_rhs (name, value, c,
quoted,
pflags,
quoted_dollar_atp,
contains_dollar_at);
/* XXX - fix up later, esp. noting presence of
W_HASQUOTEDNULL in ret->flags */
free (value);
}
else
temp = (char *)NULL;
}
else
{
FREE (value);
}
/* Otherwise do nothing; just use the value in TEMP. */
}
else /* VAR not set or VAR is NULL. */
{
FREE (temp);
temp = (char *)NULL;
if (c == '=' && var_is_special)
{
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("$%s: cannot assign in this way"), name);
free (name);
free (value);
return &expand_wdesc_error;
}
else if (c == '?')
{
parameter_brace_expand_error (name, value, check_nullness);
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
else if (c != '+')
{
/* XXX -- if we're double-quoted and the named variable is "$@",
we want to turn off any special handling of "$@" --
we're not using it, so whatever is on the rhs applies. */
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
*quoted_dollar_atp = 0;
if (contains_dollar_at)
*contains_dollar_at = 0;
/* From Posix discussion on austin-group list. Issue 221 requires
that backslashes escaping `}' inside double-quoted ${...} be
removed. */
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
quoted |= Q_DOLBRACE;
ret = parameter_brace_expand_rhs (name, value, c, quoted, pflags,
quoted_dollar_atp,
contains_dollar_at);
/* XXX - fix up later, esp. noting presence of
W_HASQUOTEDNULL in tdesc->flags */
}
free (value);
}
break;
}
free (name);
if (ret == 0)
{
ret = alloc_word_desc ();
ret->flags = tflag;
ret->word = temp;
}
return (ret);
}
/* Expand a single ${xxx} expansion. The braces are optional. When
the braces are used, parameter_brace_expand() does the work,
possibly calling param_expand recursively. */
static WORD_DESC *
param_expand (string, sindex, quoted, expanded_something,
contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
pflags)
char *string;
int *sindex, quoted, *expanded_something, *contains_dollar_at;
int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
{
char *temp, *temp1, uerror[3], *savecmd;
int zindex, t_index, expok;
unsigned char c;
intmax_t number;
SHELL_VAR *var;
WORD_LIST *list;
WORD_DESC *tdesc, *ret;
int tflag;
/*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/
zindex = *sindex;
c = string[++zindex];
temp = (char *)NULL;
ret = tdesc = (WORD_DESC *)NULL;
tflag = 0;
/* Do simple cases first. Switch on what follows '$'. */
switch (c)
{
/* $0 .. $9? */
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
temp1 = dollar_vars[TODIGIT (c)];
/* This doesn't get called when (pflags&PF_IGNUNBOUND) != 0 */
if (unbound_vars_is_error && temp1 == (char *)NULL)
{
uerror[0] = '$';
uerror[1] = c;
uerror[2] = '\0';
last_command_exit_value = EXECUTION_FAILURE;
err_unboundvar (uerror);
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
if (temp1)
temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
? quote_string (temp1)
: quote_escapes (temp1);
else
temp = (char *)NULL;
break;
/* $$ -- pid of the invoking shell. */
case '$':
temp = itos (dollar_dollar_pid);
break;
/* $# -- number of positional parameters. */
case '#':
temp = itos (number_of_args ());
break;
/* $? -- return value of the last synchronous command. */
case '?':
temp = itos (last_command_exit_value);
break;
/* $- -- flags supplied to the shell on invocation or by `set'. */
case '-':
temp = which_set_flags ();
break;
/* $! -- Pid of the last asynchronous command. */
case '!':
/* If no asynchronous pids have been created, expand to nothing.
If `set -u' has been executed, and no async processes have
been created, this is an expansion error. */
if (last_asynchronous_pid == NO_PID)
{
if (expanded_something)
*expanded_something = 0;
temp = (char *)NULL;
if (unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
{
uerror[0] = '$';
uerror[1] = c;
uerror[2] = '\0';
last_command_exit_value = EXECUTION_FAILURE;
err_unboundvar (uerror);
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
}
else
temp = itos (last_asynchronous_pid);
break;
/* The only difference between this and $@ is when the arg is quoted. */
case '*': /* `$*' */
list = list_rest_of_args ();
#if 0
/* According to austin-group posix proposal by Geoff Clare in
<20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
"The shell shall write a message to standard error and
immediately exit when it tries to expand an unset parameter
other than the '@' and '*' special parameters."
*/
if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
{
uerror[0] = '$';
uerror[1] = '*';
uerror[2] = '\0';
last_command_exit_value = EXECUTION_FAILURE;
err_unboundvar (uerror);
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
#endif
/* If there are no command-line arguments, this should just
disappear if there are other characters in the expansion,
even if it's quoted. */
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
temp = (char *)NULL;
else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
{
/* If we have "$*" we want to make a string of the positional
parameters, separated by the first character of $IFS, and
quote the whole string, including the separators. If IFS
is unset, the parameters are separated by ' '; if $IFS is
null, the parameters are concatenated. */
temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list, quoted, 0) : string_list (list);
if (temp)
{
temp1 = (quoted & Q_DOUBLE_QUOTES) ? quote_string (temp) : temp;
if (*temp == 0)
tflag |= W_HASQUOTEDNULL;
if (temp != temp1)
free (temp);
temp = temp1;
}
}
else
{
/* We check whether or not we're eventually going to split $* here,
for example when IFS is empty and we are processing the rhs of
an assignment statement. In that case, we don't separate the
arguments at all. Otherwise, if the $* is not quoted it is
identical to $@ */
if (expand_no_split_dollar_star && quoted == 0 && ifs_is_set == 0 && (pflags & PF_ASSIGNRHS))
{
/* Posix interp 888: RHS of assignment, IFS unset */
temp = string_list_dollar_at (list, Q_DOUBLE_QUOTES, pflags);
tflag |= W_SPLITSPACE;
}
else if (expand_no_split_dollar_star && quoted == 0 && ifs_is_null && (pflags & PF_ASSIGNRHS))
{
/* Posix interp 888: RHS of assignment, IFS set to '' */
temp1 = string_list_dollar_star (list, quoted, pflags);
temp = temp1 ? quote_escapes (temp1) : temp1;
FREE (temp1);
}
else if (expand_no_split_dollar_star && quoted == 0 && ifs_is_set && ifs_is_null == 0 && (pflags & PF_ASSIGNRHS))
{
/* Posix interp 888: RHS of assignment, IFS set to non-null value */
temp1 = string_list_dollar_star (list, quoted, pflags);
temp = temp1 ? quote_string (temp1) : temp1;
FREE (temp1);
}
/* XXX - should we check ifs_is_set here as well? */
# if defined (HANDLE_MULTIBYTE)
else if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
# else
else if (expand_no_split_dollar_star && ifs_firstc == 0)
# endif
/* Posix interp 888: not RHS, no splitting, IFS set to '' */
temp = string_list_dollar_star (list, quoted, 0);
else
{
temp = string_list_dollar_at (list, quoted, 0);
/* Set W_SPLITSPACE to make sure the individual positional
parameters are split into separate arguments */
#if 0
if (quoted == 0 && (ifs_is_set == 0 || ifs_is_null))
#else /* change with bash-5.0 */
if (quoted == 0 && ifs_is_null)
#endif
tflag |= W_SPLITSPACE;
/* If we're not quoted but we still don't want word splitting, make
we quote the IFS characters to protect them from splitting (e.g.,
when $@ is in the string as well). */
else if (temp && quoted == 0 && ifs_is_set && (pflags & PF_ASSIGNRHS))
{
temp1 = quote_string (temp);
free (temp);
temp = temp1;
}
}
if (expand_no_split_dollar_star == 0 && contains_dollar_at)
*contains_dollar_at = 1;
}
dispose_words (list);
break;
/* When we have "$@" what we want is "$1" "$2" "$3" ... This
means that we have to turn quoting off after we split into
the individually quoted arguments so that the final split
on the first character of $IFS is still done. */
case '@': /* `$@' */
list = list_rest_of_args ();
#if 0
/* According to austin-group posix proposal by Geoff Clare in
<20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
"The shell shall write a message to standard error and
immediately exit when it tries to expand an unset parameter
other than the '@' and '*' special parameters."
*/
if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
{
uerror[0] = '$';
uerror[1] = '@';
uerror[2] = '\0';
last_command_exit_value = EXECUTION_FAILURE;
err_unboundvar (uerror);
return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
}
#endif
/* We want to flag the fact that we saw this. We can't turn
off quoting entirely, because other characters in the
string might need it (consider "\"$@\""), but we need some
way to signal that the final split on the first character
of $IFS should be done, even though QUOTED is 1. */
/* XXX - should this test include Q_PATQUOTE? */
if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
*quoted_dollar_at_p = 1;
if (contains_dollar_at)
*contains_dollar_at = 1;
/* We want to separate the positional parameters with the first
character of $IFS in case $IFS is something other than a space.
We also want to make sure that splitting is done no matter what --
according to POSIX.2, this expands to a list of the positional
parameters no matter what IFS is set to. */
/* XXX - what to do when in a context where word splitting is not
performed? Even when IFS is not the default, posix seems to imply
that we behave like unquoted $* ? See below for how we use
PF_NOSPLIT2 here. */
/* These are the cases where word splitting will not be performed. */
if (pflags & PF_ASSIGNRHS)
temp = string_list_dollar_at (list, (quoted|Q_DOUBLE_QUOTES), pflags);
/* This needs to match what expand_word_internal does with non-quoted $@
does with separating with spaces. Passing Q_DOUBLE_QUOTES means that
the characters in LIST will be quoted, and PF_ASSIGNRHS ensures that
they will separated by spaces. After doing this, we need the special
handling for PF_NOSPLIT2 in expand_word_internal to remove the CTLESC
quotes. */
else if (pflags & PF_NOSPLIT2)
{
#if defined (HANDLE_MULTIBYTE)
if (quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc[0] != ' ')
#else
if (quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc != ' ')
#endif
/* Posix interp 888 */
temp = string_list_dollar_at (list, Q_DOUBLE_QUOTES, pflags);
else
temp = string_list_dollar_at (list, quoted, pflags);
}
else
temp = string_list_dollar_at (list, quoted, pflags);
tflag |= W_DOLLARAT;
dispose_words (list);
break;
case LBRACE:
tdesc = parameter_brace_expand (string, &zindex, quoted, pflags,
quoted_dollar_at_p,
contains_dollar_at);
if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
return (tdesc);
temp = tdesc ? tdesc->word : (char *)0;
/* XXX */
/* Quoted nulls should be removed if there is anything else
in the string. */
/* Note that we saw the quoted null so we can add one back at
the end of this function if there are no other characters
in the string, discard TEMP, and go on. The exception to
this is when we have "${@}" and $1 is '', since $@ needs
special handling. */
if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
{
if (had_quoted_null_p)
*had_quoted_null_p = 1;
if (*quoted_dollar_at_p == 0)
{
free (temp);
tdesc->word = temp = (char *)NULL;
}
}
ret = tdesc;
goto return0;
/* Do command or arithmetic substitution. */
case LPAREN:
/* We have to extract the contents of this paren substitution. */
t_index = zindex + 1;
/* XXX - might want to check for string[t_index+2] == LPAREN and parse
as arithmetic substitution immediately. */
temp = extract_command_subst (string, &t_index, (pflags&PF_COMPLETE) ? SX_COMPLETE : 0);
zindex = t_index;
/* For Posix.2-style `$(( ))' arithmetic substitution,
extract the expression and pass it to the evaluator. */
if (temp && *temp == LPAREN)
{
char *temp2;
temp1 = temp + 1;
temp2 = savestring (temp1);
t_index = strlen (temp2) - 1;
if (temp2[t_index] != RPAREN)
{
free (temp2);
goto comsub;
}
/* Cut off ending `)' */
temp2[t_index] = '\0';
if (chk_arithsub (temp2, t_index) == 0)
{
free (temp2);
#if 0
internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
#endif
goto comsub;
}
/* Expand variables found inside the expression. */
temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES|Q_ARITH);
free (temp2);
arithsub:
/* No error messages. */
savecmd = this_command_name;
this_command_name = (char *)NULL;
number = evalexp (temp1, EXP_EXPANDED, &expok);
this_command_name = savecmd;
free (temp);
free (temp1);
if (expok == 0)
{
if (interactive_shell == 0 && posixly_correct)
{
last_command_exit_value = EXECUTION_FAILURE;
return (&expand_wdesc_fatal);
}
else
return (&expand_wdesc_error);
}
temp = itos (number);
break;
}
comsub:
if (pflags & PF_NOCOMSUB)
/* we need zindex+1 because string[zindex] == RPAREN */
temp1 = substring (string, *sindex, zindex+1);
else
{
tdesc = command_substitute (temp, quoted, pflags&PF_ASSIGNRHS);
temp1 = tdesc ? tdesc->word : (char *)NULL;
if (tdesc)
dispose_word_desc (tdesc);
}
FREE (temp);
temp = temp1;
break;
/* Do POSIX.2d9-style arithmetic substitution. This will probably go
away in a future bash release. */
case '[': /*]*/
/* Extract the contents of this arithmetic substitution. */
t_index = zindex + 1;
temp = extract_arithmetic_subst (string, &t_index);
zindex = t_index;
if (temp == 0)
{
temp = savestring (string);
if (expanded_something)
*expanded_something = 0;
goto return0;
}
/* Do initial variable expansion. */
temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES|Q_ARITH);
goto arithsub;
default:
/* Find the variable in VARIABLE_LIST. */
temp = (char *)NULL;
for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
;
temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
/* If this isn't a variable name, then just output the `$'. */
if (temp1 == 0 || *temp1 == '\0')
{
FREE (temp1);
temp = (char *)xmalloc (2);
temp[0] = '$';
temp[1] = '\0';
if (expanded_something)
*expanded_something = 0;
goto return0;
}
/* If the variable exists, return its value cell. */
var = find_variable (temp1);
if (var && invisible_p (var) == 0 && var_isset (var))
{
#if defined (ARRAY_VARS)
if (assoc_p (var) || array_p (var))
{
temp = array_p (var) ? array_reference (array_cell (var), 0)
: assoc_reference (assoc_cell (var), "0");
if (temp)
temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
? quote_string (temp)
: quote_escapes (temp);
else if (unbound_vars_is_error)
goto unbound_variable;
}
else
#endif
{
temp = value_cell (var);
temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
? quote_string (temp)
: ((pflags & PF_ASSIGNRHS) ? quote_rhs (temp)
: quote_escapes (temp));
}
free (temp1);
goto return0;
}
else if (var && (invisible_p (var) || var_isset (var) == 0))
temp = (char *)NULL;
else if ((var = find_variable_last_nameref (temp1, 0)) && var_isset (var) && invisible_p (var) == 0)
{
temp = nameref_cell (var);
#if defined (ARRAY_VARS)
if (temp && *temp && valid_array_reference (temp, 0))
{
tdesc = parameter_brace_expand_word (temp, SPECIAL_VAR (temp, 0), quoted, pflags, (arrayind_t *)NULL);
if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
return (tdesc);
ret = tdesc;
goto return0;
}
else
#endif
/* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */
if (temp && *temp && legal_identifier (temp) == 0)
{
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("%s: invalid variable name for name reference"), temp);
return (&expand_wdesc_error); /* XXX */
}
else
temp = (char *)NULL;
}
temp = (char *)NULL;
unbound_variable:
if (unbound_vars_is_error)
{
last_command_exit_value = EXECUTION_FAILURE;
err_unboundvar (temp1);
}
else
{
free (temp1);
goto return0;
}
free (temp1);
last_command_exit_value = EXECUTION_FAILURE;
return ((unbound_vars_is_error && interactive_shell == 0)
? &expand_wdesc_fatal
: &expand_wdesc_error);
}
if (string[zindex])
zindex++;
return0:
*sindex = zindex;
if (ret == 0)
{
ret = alloc_word_desc ();
ret->flags = tflag; /* XXX */
ret->word = temp;
}
return ret;
}
void
invalidate_cached_quoted_dollar_at ()
{
dispose_words (cached_quoted_dollar_at);
cached_quoted_dollar_at = 0;
}
/* Make a word list which is the result of parameter and variable
expansion, command substitution, arithmetic substitution, and
quote removal of WORD. Return a pointer to a WORD_LIST which is
the result of the expansion. If WORD contains a null word, the
word list returned is also null.
QUOTED contains flag values defined in shell.h.
ISEXP is used to tell expand_word_internal that the word should be
treated as the result of an expansion. This has implications for
how IFS characters in the word are treated.
CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
they point to an integer value which receives information about expansion.
CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
else zero.
This only does word splitting in the case of $@ expansion. In that
case, we split on ' '. */
/* Values for the local variable quoted_state. */
#define UNQUOTED 0
#define PARTIALLY_QUOTED 1
#define WHOLLY_QUOTED 2
static WORD_LIST *
expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
WORD_DESC *word;
int quoted, isexp;
int *contains_dollar_at;
int *expanded_something;
{
WORD_LIST *list;
WORD_DESC *tword;
/* The intermediate string that we build while expanding. */
char *istring;
/* The current size of the above object. */
size_t istring_size;
/* Index into ISTRING. */
int istring_index;
/* Temporary string storage. */
char *temp, *temp1;
/* The text of WORD. */
register char *string;
/* The size of STRING. */
size_t string_size;
/* The index into STRING. */
int sindex;
/* This gets 1 if we see a $@ while quoted. */
int quoted_dollar_at;
/* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
whether WORD contains no quoting characters, a partially quoted
string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
int quoted_state;
/* State flags */
int had_quoted_null;
int has_quoted_ifs; /* did we add a quoted $IFS character here? */
int has_dollar_at, temp_has_dollar_at;
int split_on_spaces;
int local_expanded;
int tflag;
int pflags; /* flags passed to param_expand */
int mb_cur_max;
int assignoff; /* If assignment, offset of `=' */
register unsigned char c; /* Current character. */
int t_index; /* For calls to string_extract_xxx. */
char twochars[2];
DECLARE_MBSTATE;
/* OK, let's see if we can optimize a common idiom: "$@" */
if (STREQ (word->word, "\"$@\"") &&
(word->flags == (W_HASDOLLAR|W_QUOTED)) &&
dollar_vars[1]) /* XXX - check IFS here as well? */
{
if (contains_dollar_at)
*contains_dollar_at = 1;
if (expanded_something)
*expanded_something = 1;
if (cached_quoted_dollar_at)
return (copy_word_list (cached_quoted_dollar_at));
list = list_rest_of_args ();
list = quote_list (list);
cached_quoted_dollar_at = copy_word_list (list);
return (list);
}
istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
istring[istring_index = 0] = '\0';
quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
has_quoted_ifs = 0;
split_on_spaces = 0;
quoted_state = UNQUOTED;
string = word->word;
if (string == 0)
goto finished_with_string;
mb_cur_max = MB_CUR_MAX;
/* Don't need the string length for the SADD... and COPY_ macros unless
multibyte characters are possible, but do need it for bounds checking. */
string_size = (mb_cur_max > 1) ? strlen (string) : 1;
if (contains_dollar_at)
*contains_dollar_at = 0;
assignoff = -1;
/* Begin the expansion. */
for (sindex = 0; ;)
{
c = string[sindex];
/* Case on top-level character. */
switch (c)
{
case '\0':
goto finished_with_string;
case CTLESC:
sindex++;
#if HANDLE_MULTIBYTE
if (mb_cur_max > 1 && string[sindex])
{
SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
}
else
#endif
{
temp = (char *)xmalloc (3);
temp[0] = CTLESC;
temp[1] = c = string[sindex];
temp[2] = '\0';
}
dollar_add_string:
if (string[sindex])
sindex++;
add_string:
if (temp)
{
istring = sub_append_string (temp, istring, &istring_index, &istring_size);
temp = (char *)0;
}
break;
#if defined (PROCESS_SUBSTITUTION)
/* Process substitution. */
case '<':
case '>':
{
/* XXX - technically this should only be expanded at the start
of a word */
if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct)
{
sindex--; /* add_character: label increments sindex */
goto add_character;
}
else
t_index = sindex + 1; /* skip past both '<' and LPAREN */
temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index, 0); /*))*/
sindex = t_index;
/* If the process substitution specification is `<()', we want to
open the pipe for writing in the child and produce output; if
it is `>()', we want to open the pipe for reading in the child
and consume input. */
temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
FREE (temp1);
goto dollar_add_string;
}
#endif /* PROCESS_SUBSTITUTION */
case '=':
/* Posix.2 section 3.6.1 says that tildes following `=' in words
which are not assignment statements are not expanded. If the
shell isn't in posix mode, though, we perform tilde expansion
on `likely candidate' unquoted assignment statements (flags
include W_ASSIGNMENT but not W_QUOTED). A likely candidate
contains an unquoted :~ or =~. Something to think about: we
now have a flag that says to perform tilde expansion on arguments
to `assignment builtins' like declare and export that look like
assignment statements. We now do tilde expansion on such words
even in POSIX mode. */
if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
{
if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
goto add_ifs_character;
else
goto add_character;
}
/* If we're not in posix mode or forcing assignment-statement tilde
expansion, note where the `=' appears in the word and prepare to
do tilde expansion following the first `='. */
if ((word->flags & W_ASSIGNMENT) &&
(posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
assignoff == -1 && sindex > 0)
assignoff = sindex;
if (sindex == assignoff && string[sindex+1] == '~') /* XXX */
word->flags |= W_ITILDE;
#if 0
else if ((word->flags & W_ASSIGNMENT) &&
(posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
string[sindex+1] == '~')
word->flags |= W_ITILDE;
#endif
if (word->flags & W_ASSIGNARG)
word->flags |= W_ASSIGNRHS; /* affects $@ */
if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
{
has_quoted_ifs++;
goto add_ifs_character;
}
else
goto add_character;
case ':':
if (word->flags & (W_NOTILDE|W_NOASSNTILDE))
{
if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
goto add_ifs_character;
else
goto add_character;
}
if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
string[sindex+1] == '~')
word->flags |= W_ITILDE;
if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
goto add_ifs_character;
else
goto add_character;
case '~':
/* If the word isn't supposed to be tilde expanded, or we're not
at the start of a word or after an unquoted : or = in an
assignment statement, we don't do tilde expansion. We don't
do tilde expansion if quoted or in an arithmetic context. */
if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
(sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
(quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
{
word->flags &= ~W_ITILDE;
if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
goto add_ifs_character;
else
goto add_character;
}
if (word->flags & W_ASSIGNRHS)
tflag = 2;
else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
tflag = 1;
else
tflag = 0;
temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
word->flags &= ~W_ITILDE;
if (temp && *temp && t_index > 0)
{
temp1 = bash_tilde_expand (temp, tflag);
if (temp1 && *temp1 == '~' && STREQ (temp, temp1))
{
FREE (temp);
FREE (temp1);
goto add_character; /* tilde expansion failed */
}
free (temp);
temp = temp1;
sindex += t_index;
goto add_quoted_string; /* XXX was add_string */
}
else
{
FREE (temp);
goto add_character;
}
case '$':
if (expanded_something)
*expanded_something = 1;
local_expanded = 1;
temp_has_dollar_at = 0;
pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0;
if (word->flags & W_NOSPLIT2)
pflags |= PF_NOSPLIT2;
if (word->flags & W_ASSIGNRHS)
pflags |= PF_ASSIGNRHS;
if (word->flags & W_COMPLETE)
pflags |= PF_COMPLETE;
tword = param_expand (string, &sindex, quoted, expanded_something,
&temp_has_dollar_at, "ed_dollar_at,
&had_quoted_null, pflags);
has_dollar_at += temp_has_dollar_at;
split_on_spaces += (tword->flags & W_SPLITSPACE);
if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
{
free (string);
free (istring);
return ((tword == &expand_wdesc_error) ? &expand_word_error
: &expand_word_fatal);
}
if (contains_dollar_at && has_dollar_at)
*contains_dollar_at = 1;
if (tword && (tword->flags & W_HASQUOTEDNULL))
had_quoted_null = 1; /* note for later */
if (tword && (tword->flags & W_SAWQUOTEDNULL))
had_quoted_null = 1; /* XXX */
temp = tword ? tword->word : (char *)NULL;
dispose_word_desc (tword);
/* Kill quoted nulls; we will add them back at the end of
expand_word_internal if nothing else in the string */
if (had_quoted_null && temp && QUOTED_NULL (temp))
{
FREE (temp);
temp = (char *)NULL;
}
goto add_string;
break;
case '`': /* Backquoted command substitution. */
{
t_index = sindex++;
temp = string_extract (string, &sindex, "`", SX_REQMATCH);
/* The test of sindex against t_index is to allow bare instances of
` to pass through, for backwards compatibility. */
if (temp == &extract_string_error || temp == &extract_string_fatal)
{
if (sindex - 1 == t_index)
{
sindex = t_index;
goto add_character;
}
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index);
free (string);
free (istring);
return ((temp == &extract_string_error) ? &expand_word_error
: &expand_word_fatal);
}
if (expanded_something)
*expanded_something = 1;
local_expanded = 1;
if (word->flags & W_NOCOMSUB)
/* sindex + 1 because string[sindex] == '`' */
temp1 = substring (string, t_index, sindex + 1);
else
{
de_backslash (temp);
tword = command_substitute (temp, quoted, 0);
temp1 = tword ? tword->word : (char *)NULL;
if (tword)
dispose_word_desc (tword);
}
FREE (temp);
temp = temp1;
goto dollar_add_string;
}
case '\\':
if (string[sindex + 1] == '\n')
{
sindex += 2;
continue;
}
c = string[++sindex];
if (quoted & Q_HERE_DOCUMENT)
tflag = CBSHDOC;
else if (quoted & Q_DOUBLE_QUOTES)
tflag = CBSDQUOTE;
else
tflag = 0;
/* From Posix discussion on austin-group list: Backslash escaping
a } in ${...} is removed. Issue 0000221 */
if ((quoted & Q_DOLBRACE) && c == RBRACE)
{
SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
}
/* This is the fix for " $@\ " */
else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0) && isexp == 0 && isifs (c))
{
RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
DEFAULT_ARRAY_SIZE);
istring[istring_index++] = CTLESC;
istring[istring_index++] = '\\';
istring[istring_index] = '\0';
SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
}
else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && c == 0)
{
RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
DEFAULT_ARRAY_SIZE);
istring[istring_index++] = CTLESC;
istring[istring_index++] = '\\';
istring[istring_index] = '\0';
break;
}
else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
{
SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
}
else if (c == 0)
{
c = CTLNUL;
sindex--; /* add_character: label increments sindex */
goto add_character;
}
else
{
SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
}
sindex++;
add_twochars:
/* BEFORE jumping here, we need to increment sindex if appropriate */
RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
DEFAULT_ARRAY_SIZE);
istring[istring_index++] = twochars[0];
istring[istring_index++] = twochars[1];
istring[istring_index] = '\0';
break;
case '"':
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0))
goto add_character;
t_index = ++sindex;
temp = string_extract_double_quoted (string, &sindex, (word->flags & W_COMPLETE) ? SX_COMPLETE : 0);
/* If the quotes surrounded the entire string, then the
whole word was quoted. */
quoted_state = (t_index == 1 && string[sindex] == '\0')
? WHOLLY_QUOTED
: PARTIALLY_QUOTED;
if (temp && *temp)
{
tword = alloc_word_desc ();
tword->word = temp;
if (word->flags & W_ASSIGNARG)
tword->flags |= word->flags & (W_ASSIGNARG|W_ASSIGNRHS); /* affects $@ */
if (word->flags & W_COMPLETE)
tword->flags |= W_COMPLETE; /* for command substitutions */
if (word->flags & W_NOCOMSUB)
tword->flags |= W_NOCOMSUB;
if (word->flags & W_NOPROCSUB)
tword->flags |= W_NOPROCSUB;
temp = (char *)NULL;
temp_has_dollar_at = 0; /* does this quoted (sub)string include $@? */
/* Need to get W_HASQUOTEDNULL flag through this function. */
list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &temp_has_dollar_at, (int *)NULL);
has_dollar_at += temp_has_dollar_at;
if (list == &expand_word_error || list == &expand_word_fatal)
{
free (istring);
free (string);
/* expand_word_internal has already freed temp_word->word
for us because of the way it prints error messages. */
tword->word = (char *)NULL;
dispose_word (tword);
return list;
}
dispose_word (tword);
/* "$@" (a double-quoted dollar-at) expands into nothing,
not even a NULL word, when there are no positional
parameters. Posix interp 888 says that other parts of the
word that expand to quoted nulls result in quoted nulls, so
we can't just throw the entire word away if we have "$@"
anywhere in it. We use had_quoted_null to keep track */
if (list == 0 && temp_has_dollar_at) /* XXX - was has_dollar_at */
{
quoted_dollar_at++;
break;
}
/* If this list comes back with a quoted null from expansion,
we have either "$x" or "$@" with $1 == ''. In either case,
we need to make sure we add a quoted null argument and
disable the special handling that "$@" gets. */
if (list && list->word && list->next == 0 && (list->word->flags & W_HASQUOTEDNULL))
{
/* If we already saw a quoted null, we don't need to add
another one */
if (had_quoted_null && temp_has_dollar_at)
{
quoted_dollar_at++;
break;
}
had_quoted_null = 1; /* XXX */
}
/* If we get "$@", we know we have expanded something, so we
need to remember it for the final split on $IFS. This is
a special case; it's the only case where a quoted string
can expand into more than one word. It's going to come back
from the above call to expand_word_internal as a list with
a single word, in which all characters are quoted and
separated by blanks. What we want to do is to turn it back
into a list for the next piece of code. */
if (list)
dequote_list (list);
if (temp_has_dollar_at) /* XXX - was has_dollar_at */
{
quoted_dollar_at++;
if (contains_dollar_at)
*contains_dollar_at = 1;
if (expanded_something)
*expanded_something = 1;
local_expanded = 1;
}
}
else
{
/* What we have is "". This is a minor optimization. */
FREE (temp);
list = (WORD_LIST *)NULL;
had_quoted_null = 1; /* note for later */
}
/* The code above *might* return a list (consider the case of "$@",
where it returns "$1", "$2", etc.). We can't throw away the
rest of the list, and we have to make sure each word gets added
as quoted. We test on tresult->next: if it is non-NULL, we
quote the whole list, save it to a string with string_list, and
add that string. We don't need to quote the results of this
(and it would be wrong, since that would quote the separators
as well), so we go directly to add_string. */
if (list)
{
if (list->next)
{
/* Testing quoted_dollar_at makes sure that "$@" is
split correctly when $IFS does not contain a space. */
temp = quoted_dollar_at
? string_list_dollar_at (list, Q_DOUBLE_QUOTES, 0)
: string_list (quote_list (list));
dispose_words (list);
goto add_string;
}
else
{
temp = savestring (list->word->word);
tflag = list->word->flags;
dispose_words (list);
/* If the string is not a quoted null string, we want
to remove any embedded unquoted CTLNUL characters.
We do not want to turn quoted null strings back into
the empty string, though. We do this because we
want to remove any quoted nulls from expansions that
contain other characters. For example, if we have
x"$*"y or "x$*y" and there are no positional parameters,
the $* should expand into nothing. */
/* We use the W_HASQUOTEDNULL flag to differentiate the
cases: a quoted null character as above and when
CTLNUL is contained in the (non-null) expansion
of some variable. We use the had_quoted_null flag to
pass the value through this function to its caller. */
if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
remove_quoted_nulls (temp); /* XXX */
}
}
else
temp = (char *)NULL;
if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
had_quoted_null = 1; /* note for later */
/* We do not want to add quoted nulls to strings that are only
partially quoted; we can throw them away. The exception to
this is when we are going to be performing word splitting,
since we have to preserve a null argument if the next character
will cause word splitting. */
if (temp == 0 && quoted_state == PARTIALLY_QUOTED && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
continue;
add_quoted_string:
if (temp)
{
temp1 = temp;
temp = quote_string (temp);
free (temp1);
goto add_string;
}
else
{
/* Add NULL arg. */
c = CTLNUL;
sindex--; /* add_character: label increments sindex */
had_quoted_null = 1; /* note for later */
goto add_character;
}
/* break; */
case '\'':
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
goto add_character;
t_index = ++sindex;
temp = string_extract_single_quoted (string, &sindex);
/* If the entire STRING was surrounded by single quotes,
then the string is wholly quoted. */
quoted_state = (t_index == 1 && string[sindex] == '\0')
? WHOLLY_QUOTED
: PARTIALLY_QUOTED;
/* If all we had was '', it is a null expansion. */
if (*temp == '\0')
{
free (temp);
temp = (char *)NULL;
}
else
remove_quoted_escapes (temp); /* ??? */
if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
had_quoted_null = 1; /* note for later */
/* We do not want to add quoted nulls to strings that are only
partially quoted; such nulls are discarded. See above for the
exception, which is when the string is going to be split.
Posix interp 888 */
if (temp == 0 && (quoted_state == PARTIALLY_QUOTED) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
continue;
/* If we have a quoted null expansion, add a quoted NULL to istring. */
if (temp == 0)
{
c = CTLNUL;
sindex--; /* add_character: label increments sindex */
goto add_character;
}
else
goto add_quoted_string;
/* break; */
default:
/* This is the fix for " $@ " */
add_ifs_character:
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0))
{
if ((quoted&(Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)
has_quoted_ifs++;
if (string[sindex]) /* from old goto dollar_add_string */
sindex++;
if (c == 0)
{
c = CTLNUL;
goto add_character;
}
else
{
#if HANDLE_MULTIBYTE
/* XXX - should make sure that c is actually multibyte,
otherwise we can use the twochars branch */
if (mb_cur_max > 1)
sindex--;
if (mb_cur_max > 1)
{
SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
}
else
#endif
{
twochars[0] = CTLESC;
twochars[1] = c;
goto add_twochars;
}
}
}
SADD_MBCHAR (temp, string, sindex, string_size);
add_character:
RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
DEFAULT_ARRAY_SIZE);
istring[istring_index++] = c;
istring[istring_index] = '\0';
/* Next character. */
sindex++;
}
}
finished_with_string:
/* OK, we're ready to return. If we have a quoted string, and
quoted_dollar_at is not set, we do no splitting at all; otherwise
we split on ' '. The routines that call this will handle what to
do if nothing has been expanded. */
/* Partially and wholly quoted strings which expand to the empty
string are retained as an empty arguments. Unquoted strings
which expand to the empty string are discarded. The single
exception is the case of expanding "$@" when there are no
positional parameters. In that case, we discard the expansion. */
/* Because of how the code that handles "" and '' in partially
quoted strings works, we need to make ISTRING into a QUOTED_NULL
if we saw quoting characters, but the expansion was empty.
"" and '' are tossed away before we get to this point when
processing partially quoted strings. This makes "" and $xxx""
equivalent when xxx is unset. We also look to see whether we
saw a quoted null from a ${} expansion and add one back if we
need to. */
/* If we expand to nothing and there were no single or double quotes
in the word, we throw it away. Otherwise, we return a NULL word.
The single exception is for $@ surrounded by double quotes when
there are no positional parameters. In that case, we also throw
the word away. */
if (*istring == '\0')
{
if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
{
istring[0] = CTLNUL;
istring[1] = '\0';
tword = alloc_word_desc ();
tword->word = istring;
istring = 0; /* avoid later free() */
tword->flags |= W_HASQUOTEDNULL; /* XXX */
list = make_word_list (tword, (WORD_LIST *)NULL);
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
tword->flags |= W_QUOTED;
}
/* According to sh, ksh, and Posix.2, if a word expands into nothing
and a double-quoted "$@" appears anywhere in it, then the entire
word is removed. */
/* XXX - exception appears to be that quoted null strings result in
null arguments */
else if (quoted_state == UNQUOTED || quoted_dollar_at)
list = (WORD_LIST *)NULL;
else
list = (WORD_LIST *)NULL;
}
else if (word->flags & W_NOSPLIT)
{
tword = alloc_word_desc ();
tword->word = istring;
if (had_quoted_null && QUOTED_NULL (istring))
tword->flags |= W_HASQUOTEDNULL;
istring = 0; /* avoid later free() */
if (word->flags & W_ASSIGNMENT)
tword->flags |= W_ASSIGNMENT; /* XXX */
if (word->flags & W_COMPASSIGN)
tword->flags |= W_COMPASSIGN; /* XXX */
if (word->flags & W_NOGLOB)
tword->flags |= W_NOGLOB; /* XXX */
if (word->flags & W_NOBRACE)
tword->flags |= W_NOBRACE; /* XXX */
if (word->flags & W_NOEXPAND)
tword->flags |= W_NOEXPAND; /* XXX */
if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
tword->flags |= W_QUOTED;
list = make_word_list (tword, (WORD_LIST *)NULL);
}
else
{
char *ifs_chars;
ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
/* If we have $@, we need to split the results no matter what. If
IFS is unset or NULL, string_list_dollar_at has separated the
positional parameters with a space, so we split on space (we have
set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
string_list_dollar_at has separated the positional parameters
with the first character of $IFS, so we split on $IFS. If
SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either
unset or null, and we want to make sure that we split on spaces
regardless of what else has happened to IFS since the expansion,
or we expanded "$@" with IFS null and we need to split the positional
parameters into separate words. */
if (split_on_spaces)
{
/* If IFS is not set, and the word is not quoted, we want to split
the individual words on $' \t\n'. We rely on previous steps to
quote the portions of the word that should not be split */
if (ifs_is_set == 0)
list = list_string (istring, " \t\n", 1); /* XXX quoted == 1? */
else
list = list_string (istring, " ", 1); /* XXX quoted == 1? */
}
/* If we have $@ (has_dollar_at != 0) and we are in a context where we
don't want to split the result (W_NOSPLIT2), and we are not quoted,
we have already separated the arguments with the first character of
$IFS. In this case, we want to return a list with a single word
with the separator possibly replaced with a space (it's what other
shells seem to do).
quoted_dollar_at is internal to this function and is set if we are
passed an argument that is unquoted (quoted == 0) but we encounter a
double-quoted $@ while expanding it. */
else if (has_dollar_at && quoted_dollar_at == 0 && ifs_chars && quoted == 0 && (word->flags & W_NOSPLIT2))
{
tword = alloc_word_desc ();
/* Only split and rejoin if we have to */
if (*ifs_chars && *ifs_chars != ' ')
{
/* list_string dequotes CTLESCs in the string it's passed, so we
need it to get the space separation right if space isn't the
first character in IFS (but is present) and to remove the
quoting we added back in param_expand(). */
list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
/* This isn't exactly right in the case where we're expanding
the RHS of an expansion like ${var-$@} where IFS=: (for
example). The W_NOSPLIT2 means we do the separation with :;
the list_string removes the quotes and breaks the string into
a list, and the string_list rejoins it on spaces. When we
return, we expect to be able to split the results, but the
space separation means the right split doesn't happen. */
tword->word = string_list (list);
}
else
tword->word = istring;
if (had_quoted_null && QUOTED_NULL (istring))
tword->flags |= W_HASQUOTEDNULL; /* XXX */
if (tword->word != istring)
free (istring);
istring = 0; /* avoid later free() */
goto set_word_flags;
}
else if (has_dollar_at && ifs_chars)
list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
else
{
tword = alloc_word_desc ();
if (expanded_something && *expanded_something == 0 && has_quoted_ifs)
tword->word = remove_quoted_ifs (istring);
else
tword->word = istring;
if (had_quoted_null && QUOTED_NULL (istring))
tword->flags |= W_HASQUOTEDNULL; /* XXX */
else if (had_quoted_null)
tword->flags |= W_SAWQUOTEDNULL; /* XXX */
if (tword->word != istring)
free (istring);
istring = 0; /* avoid later free() */
set_word_flags:
if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
tword->flags |= W_QUOTED;
if (word->flags & W_ASSIGNMENT)
tword->flags |= W_ASSIGNMENT;
if (word->flags & W_COMPASSIGN)
tword->flags |= W_COMPASSIGN;
if (word->flags & W_NOGLOB)
tword->flags |= W_NOGLOB;
if (word->flags & W_NOBRACE)
tword->flags |= W_NOBRACE;
if (word->flags & W_NOEXPAND)
tword->flags |= W_NOEXPAND;
list = make_word_list (tword, (WORD_LIST *)NULL);
}
}
free (istring);
return (list);
}
/* **************************************************************** */
/* */
/* Functions for Quote Removal */
/* */
/* **************************************************************** */
/* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
backslash quoting rules for within double quotes or a here document. */
char *
string_quote_removal (string, quoted)
char *string;
int quoted;
{
size_t slen;
char *r, *result_string, *temp, *send;
int sindex, tindex, dquote;
unsigned char c;
DECLARE_MBSTATE;
/* The result can be no longer than the original string. */
slen = strlen (string);
send = string + slen;
r = result_string = (char *)xmalloc (slen + 1);
for (dquote = sindex = 0; c = string[sindex];)
{
switch (c)
{
case '\\':
c = string[++sindex];
if (c == 0)
{
*r++ = '\\';
break;
}
if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
*r++ = '\\';
/* FALLTHROUGH */
default:
SCOPY_CHAR_M (r, string, send, sindex);
break;
case '\'':
if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
{
*r++ = c;
sindex++;
break;
}
tindex = sindex + 1;
temp = string_extract_single_quoted (string, &tindex);
if (temp)
{
strcpy (r, temp);
r += strlen (r);
free (temp);
}
sindex = tindex;
break;
case '"':
dquote = 1 - dquote;
sindex++;
break;
}
}
*r = '\0';
return (result_string);
}
#if 0
/* UNUSED */
/* Perform quote removal on word WORD. This allocates and returns a new
WORD_DESC *. */
WORD_DESC *
word_quote_removal (word, quoted)
WORD_DESC *word;
int quoted;
{
WORD_DESC *w;
char *t;
t = string_quote_removal (word->word, quoted);
w = alloc_word_desc ();
w->word = t ? t : savestring ("");
return (w);
}
/* Perform quote removal on all words in LIST. If QUOTED is non-zero,
the members of the list are treated as if they are surrounded by
double quotes. Return a new list, or NULL if LIST is NULL. */
WORD_LIST *
word_list_quote_removal (list, quoted)
WORD_LIST *list;
int quoted;
{
WORD_LIST *result, *t, *tresult, *e;
for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
{
tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
#if 0
result = (WORD_LIST *) list_append (result, tresult);
#else
if (result == 0)
result = e = tresult;
else
{
e->next = tresult;
while (e->next)
e = e->next;
}
#endif
}
return (result);
}
#endif
/*******************************************
* *
* Functions to perform word splitting *
* *
*******************************************/
void
setifs (v)
SHELL_VAR *v;
{
char *t;
unsigned char uc;
ifs_var = v;
ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
ifs_is_set = ifs_var != 0;
ifs_is_null = ifs_is_set && (*ifs_value == 0);
/* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
handle multibyte chars in IFS */
memset (ifs_cmap, '\0', sizeof (ifs_cmap));
for (t = ifs_value ; t && *t; t++)
{
uc = *t;
ifs_cmap[uc] = 1;
}
#if defined (HANDLE_MULTIBYTE)
if (ifs_value == 0)
{
ifs_firstc[0] = '\0'; /* XXX - ? */
ifs_firstc_len = 1;
}
else
{
if (locale_utf8locale && UTF8_SINGLEBYTE (*ifs_value))
ifs_firstc_len = (*ifs_value != 0) ? 1 : 0;
else
{
size_t ifs_len;
ifs_len = strnlen (ifs_value, MB_CUR_MAX);
ifs_firstc_len = MBLEN (ifs_value, ifs_len);
}
if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
{
ifs_firstc[0] = ifs_value[0];
ifs_firstc[1] = '\0';
ifs_firstc_len = 1;
}
else
memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
}
#else
ifs_firstc = ifs_value ? *ifs_value : 0;
#endif
}
char *
getifs ()
{
return ifs_value;
}
/* This splits a single word into a WORD LIST on $IFS, but only if the word
is not quoted. list_string () performs quote removal for us, even if we
don't do any splitting. */
WORD_LIST *
word_split (w, ifs_chars)
WORD_DESC *w;
char *ifs_chars;
{
WORD_LIST *result;
if (w)
{
char *xifs;
xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
result = list_string (w->word, xifs, w->flags & W_QUOTED);
}
else
result = (WORD_LIST *)NULL;
return (result);
}
/* Perform word splitting on LIST and return the RESULT. It is possible
to return (WORD_LIST *)NULL. */
static WORD_LIST *
word_list_split (list)
WORD_LIST *list;
{
WORD_LIST *result, *t, *tresult, *e;
WORD_DESC *w;
for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
{
tresult = word_split (t->word, ifs_value);
/* POSIX 2.6: "If the complete expansion appropriate for a word results
in an empty field, that empty field shall be deleted from the list
of fields that form the completely expanded command, unless the
original word contained single-quote or double-quote characters."
This is where we handle these words that contain quoted null strings
and other characters that expand to nothing after word splitting. */
if (tresult == 0 && t->word && (t->word->flags & W_SAWQUOTEDNULL)) /* XXX */
{
w = alloc_word_desc ();
w->word = (char *)xmalloc (1);
w->word[0] = '\0';
tresult = make_word_list (w, (WORD_LIST *)NULL);
}
if (result == 0)
result = e = tresult;
else
{
e->next = tresult;
while (e->next)
e = e->next;
}
}
return (result);
}
/**************************************************
* *
* Functions to expand an entire WORD_LIST *
* *
**************************************************/
/* Do any word-expansion-specific cleanup and jump to top_level */
static void
exp_jump_to_top_level (v)
int v;
{
set_pipestatus_from_exit (last_command_exit_value);
/* Cleanup code goes here. */
expand_no_split_dollar_star = 0; /* XXX */
if (expanding_redir)
undo_partial_redirects ();
expanding_redir = 0;
assigning_in_environment = 0;
if (parse_and_execute_level == 0)
top_level_cleanup (); /* from sig.c */
jump_to_top_level (v);
}
/* Put NLIST (which is a WORD_LIST * of only one element) at the front of
ELIST, and set ELIST to the new list. */
#define PREPEND_LIST(nlist, elist) \
do { nlist->next = elist; elist = nlist; } while (0)
/* Separate out any initial variable assignments from TLIST. If set -k has
been executed, remove all assignment statements from TLIST. Initial
variable assignments and other environment assignments are placed
on SUBST_ASSIGN_VARLIST. */
static WORD_LIST *
separate_out_assignments (tlist)
WORD_LIST *tlist;
{
register WORD_LIST *vp, *lp;
if (tlist == 0)
return ((WORD_LIST *)NULL);
if (subst_assign_varlist)
dispose_words (subst_assign_varlist); /* Clean up after previous error */
subst_assign_varlist = (WORD_LIST *)NULL;
vp = lp = tlist;
/* Separate out variable assignments at the start of the command.
Loop invariant: vp->next == lp
Loop postcondition:
lp = list of words left after assignment statements skipped
tlist = original list of words
*/
while (lp && (lp->word->flags & W_ASSIGNMENT))
{
vp = lp;
lp = lp->next;
}
/* If lp != tlist, we have some initial assignment statements.
We make SUBST_ASSIGN_VARLIST point to the list of assignment
words and TLIST point to the remaining words. */
if (lp != tlist)
{
subst_assign_varlist = tlist;
/* ASSERT(vp->next == lp); */
vp->next = (WORD_LIST *)NULL; /* terminate variable list */
tlist = lp; /* remainder of word list */
}
/* vp == end of variable list */
/* tlist == remainder of original word list without variable assignments */
if (!tlist)
/* All the words in tlist were assignment statements */
return ((WORD_LIST *)NULL);
/* ASSERT(tlist != NULL); */
/* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
/* If the -k option is in effect, we need to go through the remaining
words, separate out the assignment words, and place them on
SUBST_ASSIGN_VARLIST. */
if (place_keywords_in_env)
{
WORD_LIST *tp; /* tp == running pointer into tlist */
tp = tlist;
lp = tlist->next;
/* Loop Invariant: tp->next == lp */
/* Loop postcondition: tlist == word list without assignment statements */
while (lp)
{
if (lp->word->flags & W_ASSIGNMENT)
{
/* Found an assignment statement, add this word to end of
subst_assign_varlist (vp). */
if (!subst_assign_varlist)
subst_assign_varlist = vp = lp;
else
{
vp->next = lp;
vp = lp;
}
/* Remove the word pointed to by LP from TLIST. */
tp->next = lp->next;
/* ASSERT(vp == lp); */
lp->next = (WORD_LIST *)NULL;
lp = tp->next;
}
else
{
tp = lp;
lp = lp->next;
}
}
}
return (tlist);
}
#define WEXP_VARASSIGN 0x001
#define WEXP_BRACEEXP 0x002
#define WEXP_TILDEEXP 0x004
#define WEXP_PARAMEXP 0x008
#define WEXP_PATHEXP 0x010
/* All of the expansions, including variable assignments at the start of
the list. */
#define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
/* All of the expansions except variable assignments at the start of
the list. */
#define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
/* All of the `shell expansions': brace expansion, tilde expansion, parameter
expansion, command substitution, arithmetic expansion, word splitting, and
quote removal. */
#define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
/* Take the list of words in LIST and do the various substitutions. Return
a new list of words which is the expanded list, and without things like
variable assignments. */
WORD_LIST *
expand_words (list)
WORD_LIST *list;
{
return (expand_word_list_internal (list, WEXP_ALL));
}
/* Same as expand_words (), but doesn't hack variable or environment
variables. */
WORD_LIST *
expand_words_no_vars (list)
WORD_LIST *list;
{
return (expand_word_list_internal (list, WEXP_NOVARS));
}
WORD_LIST *
expand_words_shellexp (list)
WORD_LIST *list;
{
return (expand_word_list_internal (list, WEXP_SHELLEXP));
}
static WORD_LIST *
glob_expand_word_list (tlist, eflags)
WORD_LIST *tlist;
int eflags;
{
char **glob_array, *temp_string;
register int glob_index;
WORD_LIST *glob_list, *output_list, *disposables, *next;
WORD_DESC *tword;
output_list = disposables = (WORD_LIST *)NULL;
glob_array = (char **)NULL;
while (tlist)
{
/* For each word, either globbing is attempted or the word is
added to orig_list. If globbing succeeds, the results are
added to orig_list and the word (tlist) is added to the list
of disposable words. If globbing fails and failed glob
expansions are left unchanged (the shell default), the
original word is added to orig_list. If globbing fails and
failed glob expansions are removed, the original word is
added to the list of disposable words. orig_list ends up
in reverse order and requires a call to REVERSE_LIST to
be set right. After all words are examined, the disposable
words are freed. */
next = tlist->next;
/* If the word isn't an assignment and contains an unquoted
pattern matching character, then glob it. */
if ((tlist->word->flags & W_NOGLOB) == 0 &&
unquoted_glob_pattern_p (tlist->word->word))
{
glob_array = shell_glob_filename (tlist->word->word);
/* Handle error cases.
I don't think we should report errors like "No such file
or directory". However, I would like to report errors
like "Read failed". */
if (glob_array == 0 || GLOB_FAILED (glob_array))
{
glob_array = (char **)xmalloc (sizeof (char *));
glob_array[0] = (char *)NULL;
}
/* Dequote the current word in case we have to use it. */
if (glob_array[0] == NULL)
{
temp_string = dequote_string (tlist->word->word);
free (tlist->word->word);
tlist->word->word = temp_string;
}
/* Make the array into a word list. */
glob_list = (WORD_LIST *)NULL;
for (glob_index = 0; glob_array[glob_index]; glob_index++)
{
tword = make_bare_word (glob_array[glob_index]);
glob_list = make_word_list (tword, glob_list);
}
if (glob_list)
{
output_list = (WORD_LIST *)list_append (glob_list, output_list);
PREPEND_LIST (tlist, disposables);
}
else if (fail_glob_expansion != 0)
{
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("no match: %s"), tlist->word->word);
exp_jump_to_top_level (DISCARD);
}
else if (allow_null_glob_expansion == 0)
{
/* Failed glob expressions are left unchanged. */
PREPEND_LIST (tlist, output_list);
}
else
{
/* Failed glob expressions are removed. */
PREPEND_LIST (tlist, disposables);
}
}
else
{
/* Dequote the string. */
temp_string = dequote_string (tlist->word->word);
free (tlist->word->word);
tlist->word->word = temp_string;
PREPEND_LIST (tlist, output_list);
}
strvec_dispose (glob_array);
glob_array = (char **)NULL;
tlist = next;
}
if (disposables)
dispose_words (disposables);
if (output_list)
output_list = REVERSE_LIST (output_list, WORD_LIST *);
return (output_list);
}
#if defined (BRACE_EXPANSION)
static WORD_LIST *
brace_expand_word_list (tlist, eflags)
WORD_LIST *tlist;
int eflags;
{
register char **expansions;
char *temp_string;
WORD_LIST *disposables, *output_list, *next;
WORD_DESC *w;
int eindex;
for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
{
next = tlist->next;
if (tlist->word->flags & W_NOBRACE)
{
/*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
PREPEND_LIST (tlist, output_list);
continue;
}
if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
{
/*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
PREPEND_LIST (tlist, output_list);
continue;
}
/* Only do brace expansion if the word has a brace character. If
not, just add the word list element to BRACES and continue. In
the common case, at least when running shell scripts, this will
degenerate to a bunch of calls to `mbschr', and then what is
basically a reversal of TLIST into BRACES, which is corrected
by a call to REVERSE_LIST () on BRACES when the end of TLIST
is reached. */
if (mbschr (tlist->word->word, LBRACE))
{
expansions = brace_expand (tlist->word->word);
for (eindex = 0; temp_string = expansions[eindex]; eindex++)
{
w = alloc_word_desc ();
w->word = temp_string;
/* If brace expansion didn't change the word, preserve
the flags. We may want to preserve the flags
unconditionally someday -- XXX */
if (STREQ (temp_string, tlist->word->word))
w->flags = tlist->word->flags;
else
w = make_word_flags (w, temp_string);
output_list = make_word_list (w, output_list);
}
free (expansions);
/* Add TLIST to the list of words to be freed after brace
expansion has been performed. */
PREPEND_LIST (tlist, disposables);
}
else
PREPEND_LIST (tlist, output_list);
}
if (disposables)
dispose_words (disposables);
if (output_list)
output_list = REVERSE_LIST (output_list, WORD_LIST *);
return (output_list);
}
#endif
#if defined (ARRAY_VARS)
/* Take WORD, a compound associative array assignment, and internally run
'declare -A w', where W is the variable name portion of WORD. */
static int
make_internal_declare (word, option, cmd)
char *word;
char *option;
char *cmd;
{
int t, r;
WORD_LIST *wl;
WORD_DESC *w;
w = make_word (word);
t = assignment (w->word, 0);
if (w->word[t] == '=')
{
w->word[t] = '\0';
if (w->word[t - 1] == '+') /* cut off any append op */
w->word[t - 1] = '\0';
}
wl = make_word_list (w, (WORD_LIST *)NULL);
wl = make_word_list (make_word (option), wl);
r = declare_builtin (wl);
dispose_words (wl);
return r;
}
#endif
static WORD_LIST *
shell_expand_word_list (tlist, eflags)
WORD_LIST *tlist;
int eflags;
{
WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list, *wcmd;
int expanded_something, has_dollar_at;
/* We do tilde expansion all the time. This is what 1003.2 says. */
new_list = (WORD_LIST *)NULL;
for (wcmd = tlist; wcmd; wcmd = wcmd->next)
if (wcmd->word->flags & W_ASSNBLTIN)
break;
for (orig_list = tlist; tlist; tlist = next)
{
next = tlist->next;
#if defined (ARRAY_VARS)
/* If this is a compound array assignment to a builtin that accepts
such assignments (e.g., `declare'), take the assignment and perform
it separately, handling the semantics of declarations inside shell
functions. This avoids the double-evaluation of such arguments,
because `declare' does some evaluation of compound assignments on
its own. */
if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
{
int t;
char opts[16];
int opti;
opti = 0;
if (tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL|W_CHKLOCAL|W_ASSIGNARRAY))
opts[opti++] = '-';
if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL)) == (W_ASSIGNASSOC|W_ASSNGLOBAL))
{
opts[opti++] = 'g';
opts[opti++] = 'A';
}
else if (tlist->word->flags & W_ASSIGNASSOC)
opts[opti++] = 'A';
else if ((tlist->word->flags & (W_ASSIGNARRAY|W_ASSNGLOBAL)) == (W_ASSIGNARRAY|W_ASSNGLOBAL))
{
opts[opti++] = 'g';
opts[opti++] = 'a';
}
else if (tlist->word->flags & W_ASSIGNARRAY)
opts[opti++] = 'a';
else if (tlist->word->flags & W_ASSNGLOBAL)
opts[opti++] = 'g';
if (tlist->word->flags & W_CHKLOCAL)
opts[opti++] = 'G';
/* If we have special handling note the integer attribute and others
that transform the value upon assignment. What we do is take all
of the option arguments and scan through them looking for options
that cause such transformations, and add them to the `opts' array. */
/* if (opti > 0) */
{
char omap[128];
int oind;
WORD_LIST *l;
memset (omap, '\0', sizeof (omap));
for (l = orig_list->next; l != tlist; l = l->next)
{
if (l->word->word[0] != '-')
break; /* non-option argument */
if (l->word->word[0] == '-' && l->word->word[1] == '-' && l->word->word[2] == 0)
break; /* -- signals end of options */
for (oind = 1; l->word->word[oind]; oind++)
switch (l->word->word[oind])
{
case 'i':
case 'l':
case 'u':
case 'c':
omap[l->word->word[oind]] = 1;
if (opti == 0)
opts[opti++] = '-';
break;
default:
break;
}
}
for (oind = 0; oind < sizeof (omap); oind++)
if (omap[oind])
opts[opti++] = oind;
}
opts[opti] = '\0';
if (opti > 0)
{
t = make_internal_declare (tlist->word->word, opts, wcmd ? wcmd->word->word : (char *)0);
if (t != EXECUTION_SUCCESS)
{
last_command_exit_value = t;
exp_jump_to_top_level (DISCARD);
}
}
t = do_word_assignment (tlist->word, 0);
if (t == 0)
{
last_command_exit_value = EXECUTION_FAILURE;
exp_jump_to_top_level (DISCARD);
}
/* Now transform the word as ksh93 appears to do and go on */
t = assignment (tlist->word->word, 0);
tlist->word->word[t] = '\0';
if (tlist->word->word[t - 1] == '+')
tlist->word->word[t - 1] = '\0'; /* cut off append op */
tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC|W_ASSIGNARRAY);
}
#endif
expanded_something = 0;
expanded = expand_word_internal
(tlist->word, 0, 0, &has_dollar_at, &expanded_something);
if (expanded == &expand_word_error || expanded == &expand_word_fatal)
{
/* By convention, each time this error is returned,
tlist->word->word has already been freed. */
tlist->word->word = (char *)NULL;
/* Dispose our copy of the original list. */
dispose_words (orig_list);
/* Dispose the new list we're building. */
dispose_words (new_list);
last_command_exit_value = EXECUTION_FAILURE;
if (expanded == &expand_word_error)
exp_jump_to_top_level (DISCARD);
else
exp_jump_to_top_level (FORCE_EOF);
}
/* Don't split words marked W_NOSPLIT. */
if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
{
temp_list = word_list_split (expanded);
dispose_words (expanded);
}
else
{
/* If no parameter expansion, command substitution, process
substitution, or arithmetic substitution took place, then
do not do word splitting. We still have to remove quoted
null characters from the result. */
word_list_remove_quoted_nulls (expanded);
temp_list = expanded;
}
expanded = REVERSE_LIST (temp_list, WORD_LIST *);
new_list = (WORD_LIST *)list_append (expanded, new_list);
}
if (orig_list)
dispose_words (orig_list);
if (new_list)
new_list = REVERSE_LIST (new_list, WORD_LIST *);
return (new_list);
}
/* The workhorse for expand_words () and expand_words_no_vars ().
First arg is LIST, a WORD_LIST of words.
Second arg EFLAGS is a flags word controlling which expansions are
performed.
This does all of the substitutions: brace expansion, tilde expansion,
parameter expansion, command substitution, arithmetic expansion,
process substitution, word splitting, and pathname expansion, according
to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
set, or for which no expansion is done, do not undergo word splitting.
Words with the W_NOGLOB bit set do not undergo pathname expansion; words
with W_NOBRACE set do not undergo brace expansion (see
brace_expand_word_list above). */
static WORD_LIST *
expand_word_list_internal (list, eflags)
WORD_LIST *list;
int eflags;
{
WORD_LIST *new_list, *temp_list;
int tint;
char *savecmd;
tempenv_assign_error = 0;
if (list == 0)
return ((WORD_LIST *)NULL);
garglist = new_list = copy_word_list (list);
if (eflags & WEXP_VARASSIGN)
{
garglist = new_list = separate_out_assignments (new_list);
if (new_list == 0)
{
if (subst_assign_varlist)
{
/* All the words were variable assignments, so they are placed
into the shell's environment. */
for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
{
savecmd = this_command_name;
this_command_name = (char *)NULL; /* no arithmetic errors */
tint = do_word_assignment (temp_list->word, 0);
this_command_name = savecmd;
/* Variable assignment errors in non-interactive shells
running in Posix.2 mode cause the shell to exit, unless
they are being run by the `command' builtin. */
if (tint == 0)
{
last_command_exit_value = EXECUTION_FAILURE;
if (interactive_shell == 0 && posixly_correct && executing_command_builtin == 0)
exp_jump_to_top_level (FORCE_EOF);
else
exp_jump_to_top_level (DISCARD);
}
}
dispose_words (subst_assign_varlist);
subst_assign_varlist = (WORD_LIST *)NULL;
}
return ((WORD_LIST *)NULL);
}
}
/* Begin expanding the words that remain. The expansions take place on
things that aren't really variable assignments. */
#if defined (BRACE_EXPANSION)
/* Do brace expansion on this word if there are any brace characters
in the string. */
if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
new_list = brace_expand_word_list (new_list, eflags);
#endif /* BRACE_EXPANSION */
/* Perform the `normal' shell expansions: tilde expansion, parameter and
variable substitution, command substitution, arithmetic expansion,
and word splitting. */
new_list = shell_expand_word_list (new_list, eflags);
/* Okay, we're almost done. Now let's just do some filename
globbing. */
if (new_list)
{
if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
/* Glob expand the word list unless globbing has been disabled. */
new_list = glob_expand_word_list (new_list, eflags);
else
/* Dequote the words, because we're not performing globbing. */
new_list = dequote_list (new_list);
}
if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
{
sh_wassign_func_t *assign_func;
int is_special_builtin, is_builtin_or_func;
/* If the remainder of the words expand to nothing, Posix.2 requires
that the variable and environment assignments affect the shell's
environment. */
assign_func = new_list ? assign_in_env : do_word_assignment;
tempenv_assign_error = 0;
is_builtin_or_func = (new_list && new_list->word && (find_shell_builtin (new_list->word->word) || find_function (new_list->word->word)));
/* Posix says that special builtins exit if a variable assignment error
occurs in an assignment preceding it. */
is_special_builtin = (posixly_correct && new_list && new_list->word && find_special_builtin (new_list->word->word));
for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
{
savecmd = this_command_name;
this_command_name = (char *)NULL;
assigning_in_environment = (assign_func == assign_in_env);
tint = (*assign_func) (temp_list->word, is_builtin_or_func);
assigning_in_environment = 0;
this_command_name = savecmd;
/* Variable assignment errors in non-interactive shells running
in Posix.2 mode cause the shell to exit. */
if (tint == 0)
{
if (assign_func == do_word_assignment)
{
last_command_exit_value = EXECUTION_FAILURE;
if (interactive_shell == 0 && posixly_correct)
exp_jump_to_top_level (FORCE_EOF);
else
exp_jump_to_top_level (DISCARD);
}
else if (interactive_shell == 0 && is_special_builtin)
{
last_command_exit_value = EXECUTION_FAILURE;
exp_jump_to_top_level (FORCE_EOF);
}
else
tempenv_assign_error++;
}
}
dispose_words (subst_assign_varlist);
subst_assign_varlist = (WORD_LIST *)NULL;
}
return (new_list);
}
|