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
|
/* Builtin function expansion for GNU Make.
Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
2010 Free Software Foundation, Inc.
This file is part of GNU Make.
GNU Make 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.
GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. */
#include "make.h"
#include "filedef.h"
#include "variable.h"
#include "dep.h"
#include "job.h"
#include "commands.h"
#include "debug.h"
#ifdef _AMIGA
#include "amiga.h"
#endif
struct function_table_entry
{
const char *name;
unsigned char len;
unsigned char minimum_args;
unsigned char maximum_args;
char expand_args;
char *(*func_ptr) (char *output, char **argv, const char *fname);
};
static unsigned long
function_table_entry_hash_1 (const void *keyv)
{
const struct function_table_entry *key = keyv;
return_STRING_N_HASH_1 (key->name, key->len);
}
static unsigned long
function_table_entry_hash_2 (const void *keyv)
{
const struct function_table_entry *key = keyv;
return_STRING_N_HASH_2 (key->name, key->len);
}
static int
function_table_entry_hash_cmp (const void *xv, const void *yv)
{
const struct function_table_entry *x = xv;
const struct function_table_entry *y = yv;
int result = x->len - y->len;
if (result)
return result;
return_STRING_N_COMPARE (x->name, y->name, x->len);
}
static struct hash_table function_table;
/* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
each occurrence of SUBST with REPLACE. TEXT is null-terminated. SLEN is
the length of SUBST and RLEN is the length of REPLACE. If BY_WORD is
nonzero, substitutions are done only on matches which are complete
whitespace-delimited words. */
char *
subst_expand (char *o, const char *text, const char *subst, const char *replace,
unsigned int slen, unsigned int rlen, int by_word)
{
const char *t = text;
const char *p;
if (slen == 0 && !by_word)
{
/* The first occurrence of "" in any string is its end. */
o = variable_buffer_output (o, t, strlen (t));
if (rlen > 0)
o = variable_buffer_output (o, replace, rlen);
return o;
}
do
{
if (by_word && slen == 0)
/* When matching by words, the empty string should match
the end of each word, rather than the end of the whole text. */
p = end_of_token (next_token (t));
else
{
p = strstr (t, subst);
if (p == 0)
{
/* No more matches. Output everything left on the end. */
o = variable_buffer_output (o, t, strlen (t));
return o;
}
}
/* Output everything before this occurrence of the string to replace. */
if (p > t)
o = variable_buffer_output (o, t, p - t);
/* If we're substituting only by fully matched words,
or only at the ends of words, check that this case qualifies. */
if (by_word
&& ((p > text && !isblank ((unsigned char)p[-1]))
|| (p[slen] != '\0' && !isblank ((unsigned char)p[slen]))))
/* Struck out. Output the rest of the string that is
no longer to be replaced. */
o = variable_buffer_output (o, subst, slen);
else if (rlen > 0)
/* Output the replacement string. */
o = variable_buffer_output (o, replace, rlen);
/* Advance T past the string to be replaced. */
t = p + slen;
} while (*t != '\0');
return o;
}
/* Store into VARIABLE_BUFFER at O the result of scanning TEXT
and replacing strings matching PATTERN with REPLACE.
If PATTERN_PERCENT is not nil, PATTERN has already been
run through find_percent, and PATTERN_PERCENT is the result.
If REPLACE_PERCENT is not nil, REPLACE has already been
run through find_percent, and REPLACE_PERCENT is the result.
Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
character _AFTER_ the %, not to the % itself.
*/
char *
patsubst_expand_pat (char *o, const char *text,
const char *pattern, const char *replace,
const char *pattern_percent, const char *replace_percent)
{
unsigned int pattern_prepercent_len, pattern_postpercent_len;
unsigned int replace_prepercent_len, replace_postpercent_len;
const char *t;
unsigned int len;
int doneany = 0;
/* Record the length of REPLACE before and after the % so we don't have to
compute these lengths more than once. */
if (replace_percent)
{
replace_prepercent_len = replace_percent - replace - 1;
replace_postpercent_len = strlen (replace_percent);
}
else
{
replace_prepercent_len = strlen (replace);
replace_postpercent_len = 0;
}
if (!pattern_percent)
/* With no % in the pattern, this is just a simple substitution. */
return subst_expand (o, text, pattern, replace,
strlen (pattern), strlen (replace), 1);
/* Record the length of PATTERN before and after the %
so we don't have to compute it more than once. */
pattern_prepercent_len = pattern_percent - pattern - 1;
pattern_postpercent_len = strlen (pattern_percent);
while ((t = find_next_token (&text, &len)) != 0)
{
int fail = 0;
/* Is it big enough to match? */
if (len < pattern_prepercent_len + pattern_postpercent_len)
fail = 1;
/* Does the prefix match? */
if (!fail && pattern_prepercent_len > 0
&& (*t != *pattern
|| t[pattern_prepercent_len - 1] != pattern_percent[-2]
|| !strneq (t + 1, pattern + 1, pattern_prepercent_len - 1)))
fail = 1;
/* Does the suffix match? */
if (!fail && pattern_postpercent_len > 0
&& (t[len - 1] != pattern_percent[pattern_postpercent_len - 1]
|| t[len - pattern_postpercent_len] != *pattern_percent
|| !strneq (&t[len - pattern_postpercent_len],
pattern_percent, pattern_postpercent_len - 1)))
fail = 1;
if (fail)
/* It didn't match. Output the string. */
o = variable_buffer_output (o, t, len);
else
{
/* It matched. Output the replacement. */
/* Output the part of the replacement before the %. */
o = variable_buffer_output (o, replace, replace_prepercent_len);
if (replace_percent != 0)
{
/* Output the part of the matched string that
matched the % in the pattern. */
o = variable_buffer_output (o, t + pattern_prepercent_len,
len - (pattern_prepercent_len
+ pattern_postpercent_len));
/* Output the part of the replacement after the %. */
o = variable_buffer_output (o, replace_percent,
replace_postpercent_len);
}
}
/* Output a space, but not if the replacement is "". */
if (fail || replace_prepercent_len > 0
|| (replace_percent != 0 && len + replace_postpercent_len > 0))
{
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
}
if (doneany)
/* Kill the last space. */
--o;
return o;
}
/* Store into VARIABLE_BUFFER at O the result of scanning TEXT
and replacing strings matching PATTERN with REPLACE.
If PATTERN_PERCENT is not nil, PATTERN has already been
run through find_percent, and PATTERN_PERCENT is the result.
If REPLACE_PERCENT is not nil, REPLACE has already been
run through find_percent, and REPLACE_PERCENT is the result.
Note that we expect PATTERN_PERCENT and REPLACE_PERCENT to point to the
character _AFTER_ the %, not to the % itself.
*/
char *
patsubst_expand (char *o, const char *text, char *pattern, char *replace)
{
const char *pattern_percent = find_percent (pattern);
const char *replace_percent = find_percent (replace);
/* If there's a percent in the pattern or replacement skip it. */
if (replace_percent)
++replace_percent;
if (pattern_percent)
++pattern_percent;
return patsubst_expand_pat (o, text, pattern, replace,
pattern_percent, replace_percent);
}
/* Look up a function by name. */
static const struct function_table_entry *
lookup_function (const char *s)
{
const char *e = s;
while (*e && ( (*e >= 'a' && *e <= 'z') || *e == '-'))
e++;
if (*e == '\0' || isblank ((unsigned char) *e))
{
struct function_table_entry function_table_entry_key;
function_table_entry_key.name = s;
function_table_entry_key.len = e - s;
return hash_find_item (&function_table, &function_table_entry_key);
}
return 0;
}
/* Return 1 if PATTERN matches STR, 0 if not. */
int
pattern_matches (const char *pattern, const char *percent, const char *str)
{
unsigned int sfxlen, strlength;
if (percent == 0)
{
unsigned int len = strlen (pattern) + 1;
char *new_chars = alloca (len);
memcpy (new_chars, pattern, len);
percent = find_percent (new_chars);
if (percent == 0)
return streq (new_chars, str);
pattern = new_chars;
}
sfxlen = strlen (percent + 1);
strlength = strlen (str);
if (strlength < (percent - pattern) + sfxlen
|| !strneq (pattern, str, percent - pattern))
return 0;
return !strcmp (percent + 1, str + (strlength - sfxlen));
}
/* Find the next comma or ENDPAREN (counting nested STARTPAREN and
ENDPARENtheses), starting at PTR before END. Return a pointer to
next character.
If no next argument is found, return NULL.
*/
static char *
find_next_argument (char startparen, char endparen,
const char *ptr, const char *end)
{
int count = 0;
for (; ptr < end; ++ptr)
if (*ptr == startparen)
++count;
else if (*ptr == endparen)
{
--count;
if (count < 0)
return NULL;
}
else if (*ptr == ',' && !count)
return (char *)ptr;
/* We didn't find anything. */
return NULL;
}
/* Glob-expand LINE. The returned pointer is
only good until the next call to string_glob. */
static char *
string_glob (char *line)
{
static char *result = 0;
static unsigned int length;
struct nameseq *chain;
unsigned int idx;
chain = PARSE_FILE_SEQ (&line, struct nameseq, '\0', NULL,
/* We do not want parse_file_seq to strip `./'s.
That would break examples like:
$(patsubst ./%.c,obj/%.o,$(wildcard ./?*.c)). */
PARSEFS_NOSTRIP|PARSEFS_NOCACHE|PARSEFS_EXISTS);
if (result == 0)
{
length = 100;
result = xmalloc (100);
}
idx = 0;
while (chain != 0)
{
struct nameseq *next = chain->next;
unsigned int len = strlen (chain->name);
if (idx + len + 1 > length)
{
length += (len + 1) * 2;
result = xrealloc (result, length);
}
memcpy (&result[idx], chain->name, len);
idx += len;
result[idx++] = ' ';
/* Because we used PARSEFS_NOCACHE above, we have to free() NAME. */
free ((char *)chain->name);
free (chain);
chain = next;
}
/* Kill the last space and terminate the string. */
if (idx == 0)
result[0] = '\0';
else
result[idx - 1] = '\0';
return result;
}
/*
Builtin functions
*/
static char *
func_patsubst (char *o, char **argv, const char *funcname UNUSED)
{
o = patsubst_expand (o, argv[2], argv[0], argv[1]);
return o;
}
static char *
func_join (char *o, char **argv, const char *funcname UNUSED)
{
int doneany = 0;
/* Write each word of the first argument directly followed
by the corresponding word of the second argument.
If the two arguments have a different number of words,
the excess words are just output separated by blanks. */
const char *tp;
const char *pp;
const char *list1_iterator = argv[0];
const char *list2_iterator = argv[1];
do
{
unsigned int len1, len2;
tp = find_next_token (&list1_iterator, &len1);
if (tp != 0)
o = variable_buffer_output (o, tp, len1);
pp = find_next_token (&list2_iterator, &len2);
if (pp != 0)
o = variable_buffer_output (o, pp, len2);
if (tp != 0 || pp != 0)
{
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
}
while (tp != 0 || pp != 0);
if (doneany)
/* Kill the last blank. */
--o;
return o;
}
static char *
func_origin (char *o, char **argv, const char *funcname UNUSED)
{
/* Expand the argument. */
struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
if (v == 0)
o = variable_buffer_output (o, "undefined", 9);
else
switch (v->origin)
{
default:
case o_invalid:
abort ();
break;
case o_default:
o = variable_buffer_output (o, "default", 7);
break;
case o_env:
o = variable_buffer_output (o, "environment", 11);
break;
case o_file:
o = variable_buffer_output (o, "file", 4);
break;
case o_env_override:
o = variable_buffer_output (o, "environment override", 20);
break;
case o_command:
o = variable_buffer_output (o, "command line", 12);
break;
case o_override:
o = variable_buffer_output (o, "override", 8);
break;
case o_automatic:
o = variable_buffer_output (o, "automatic", 9);
break;
}
return o;
}
static char *
func_flavor (char *o, char **argv, const char *funcname UNUSED)
{
struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
if (v == 0)
o = variable_buffer_output (o, "undefined", 9);
else
if (v->recursive)
o = variable_buffer_output (o, "recursive", 9);
else
o = variable_buffer_output (o, "simple", 6);
return o;
}
#ifdef VMS
# define IS_PATHSEP(c) ((c) == ']')
#else
# ifdef HAVE_DOS_PATHS
# define IS_PATHSEP(c) ((c) == '/' || (c) == '\\')
# else
# define IS_PATHSEP(c) ((c) == '/')
# endif
#endif
static char *
func_notdir_suffix (char *o, char **argv, const char *funcname)
{
/* Expand the argument. */
const char *list_iterator = argv[0];
const char *p2;
int doneany =0;
unsigned int len=0;
int is_suffix = streq (funcname, "suffix");
int is_notdir = !is_suffix;
while ((p2 = find_next_token (&list_iterator, &len)) != 0)
{
const char *p = p2 + len;
while (p >= p2 && (!is_suffix || *p != '.'))
{
if (IS_PATHSEP (*p))
break;
--p;
}
if (p >= p2)
{
if (is_notdir)
++p;
else if (*p != '.')
continue;
o = variable_buffer_output (o, p, len - (p - p2));
}
#ifdef HAVE_DOS_PATHS
/* Handle the case of "d:foo/bar". */
else if (streq (funcname, "notdir") && p2[0] && p2[1] == ':')
{
p = p2 + 2;
o = variable_buffer_output (o, p, len - (p - p2));
}
#endif
else if (is_notdir)
o = variable_buffer_output (o, p2, len);
if (is_notdir || p >= p2)
{
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
}
if (doneany)
/* Kill last space. */
--o;
return o;
}
static char *
func_basename_dir (char *o, char **argv, const char *funcname)
{
/* Expand the argument. */
const char *p3 = argv[0];
const char *p2;
int doneany=0;
unsigned int len=0;
int is_basename= streq (funcname, "basename");
int is_dir= !is_basename;
while ((p2 = find_next_token (&p3, &len)) != 0)
{
const char *p = p2 + len;
while (p >= p2 && (!is_basename || *p != '.'))
{
if (IS_PATHSEP (*p))
break;
--p;
}
if (p >= p2 && (is_dir))
o = variable_buffer_output (o, p2, ++p - p2);
else if (p >= p2 && (*p == '.'))
o = variable_buffer_output (o, p2, p - p2);
#ifdef HAVE_DOS_PATHS
/* Handle the "d:foobar" case */
else if (p2[0] && p2[1] == ':' && is_dir)
o = variable_buffer_output (o, p2, 2);
#endif
else if (is_dir)
#ifdef VMS
o = variable_buffer_output (o, "[]", 2);
#else
#ifndef _AMIGA
o = variable_buffer_output (o, "./", 2);
#else
; /* Just a nop... */
#endif /* AMIGA */
#endif /* !VMS */
else
/* The entire name is the basename. */
o = variable_buffer_output (o, p2, len);
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
if (doneany)
/* Kill last space. */
--o;
return o;
}
static char *
func_addsuffix_addprefix (char *o, char **argv, const char *funcname)
{
int fixlen = strlen (argv[0]);
const char *list_iterator = argv[1];
int is_addprefix = streq (funcname, "addprefix");
int is_addsuffix = !is_addprefix;
int doneany = 0;
const char *p;
unsigned int len;
while ((p = find_next_token (&list_iterator, &len)) != 0)
{
if (is_addprefix)
o = variable_buffer_output (o, argv[0], fixlen);
o = variable_buffer_output (o, p, len);
if (is_addsuffix)
o = variable_buffer_output (o, argv[0], fixlen);
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
if (doneany)
/* Kill last space. */
--o;
return o;
}
static char *
func_subst (char *o, char **argv, const char *funcname UNUSED)
{
o = subst_expand (o, argv[2], argv[0], argv[1], strlen (argv[0]),
strlen (argv[1]), 0);
return o;
}
static char *
func_firstword (char *o, char **argv, const char *funcname UNUSED)
{
unsigned int i;
const char *words = argv[0]; /* Use a temp variable for find_next_token */
const char *p = find_next_token (&words, &i);
if (p != 0)
o = variable_buffer_output (o, p, i);
return o;
}
static char *
func_lastword (char *o, char **argv, const char *funcname UNUSED)
{
unsigned int i;
const char *words = argv[0]; /* Use a temp variable for find_next_token */
const char *p = NULL;
const char *t;
while ((t = find_next_token (&words, &i)))
p = t;
if (p != 0)
o = variable_buffer_output (o, p, i);
return o;
}
static char *
func_words (char *o, char **argv, const char *funcname UNUSED)
{
int i = 0;
const char *word_iterator = argv[0];
char buf[20];
while (find_next_token (&word_iterator, NULL) != 0)
++i;
sprintf (buf, "%d", i);
o = variable_buffer_output (o, buf, strlen (buf));
return o;
}
/* Set begpp to point to the first non-whitespace character of the string,
* and endpp to point to the last non-whitespace character of the string.
* If the string is empty or contains nothing but whitespace, endpp will be
* begpp-1.
*/
char *
strip_whitespace (const char **begpp, const char **endpp)
{
while (*begpp <= *endpp && isspace ((unsigned char)**begpp))
(*begpp) ++;
while (*endpp >= *begpp && isspace ((unsigned char)**endpp))
(*endpp) --;
return (char *)*begpp;
}
static void
check_numeric (const char *s, const char *msg)
{
const char *end = s + strlen (s) - 1;
const char *beg = s;
strip_whitespace (&s, &end);
for (; s <= end; ++s)
if (!ISDIGIT (*s)) /* ISDIGIT only evals its arg once: see make.h. */
break;
if (s <= end || end - beg < 0)
fatal (*expanding_var, "%s: '%s'", msg, beg);
}
static char *
func_word (char *o, char **argv, const char *funcname UNUSED)
{
const char *end_p;
const char *p;
int i;
/* Check the first argument. */
check_numeric (argv[0], _("non-numeric first argument to `word' function"));
i = atoi (argv[0]);
if (i == 0)
fatal (*expanding_var,
_("first argument to `word' function must be greater than 0"));
end_p = argv[1];
while ((p = find_next_token (&end_p, 0)) != 0)
if (--i == 0)
break;
if (i == 0)
o = variable_buffer_output (o, p, end_p - p);
return o;
}
static char *
func_wordlist (char *o, char **argv, const char *funcname UNUSED)
{
int start, count;
/* Check the arguments. */
check_numeric (argv[0],
_("non-numeric first argument to `wordlist' function"));
check_numeric (argv[1],
_("non-numeric second argument to `wordlist' function"));
start = atoi (argv[0]);
if (start < 1)
fatal (*expanding_var,
"invalid first argument to `wordlist' function: `%d'", start);
count = atoi (argv[1]) - start + 1;
if (count > 0)
{
const char *p;
const char *end_p = argv[2];
/* Find the beginning of the "start"th word. */
while (((p = find_next_token (&end_p, 0)) != 0) && --start)
;
if (p)
{
/* Find the end of the "count"th word from start. */
while (--count && (find_next_token (&end_p, 0) != 0))
;
/* Return the stuff in the middle. */
o = variable_buffer_output (o, p, end_p - p);
}
}
return o;
}
static char *
func_findstring (char *o, char **argv, const char *funcname UNUSED)
{
/* Find the first occurrence of the first string in the second. */
if (strstr (argv[1], argv[0]) != 0)
o = variable_buffer_output (o, argv[0], strlen (argv[0]));
return o;
}
static char *
func_foreach (char *o, char **argv, const char *funcname UNUSED)
{
/* expand only the first two. */
char *varname = expand_argument (argv[0], NULL);
char *list = expand_argument (argv[1], NULL);
const char *body = argv[2];
int doneany = 0;
const char *list_iterator = list;
const char *p;
unsigned int len;
struct variable *var;
push_new_variable_scope ();
var = define_variable (varname, strlen (varname), "", o_automatic, 0);
/* loop through LIST, put the value in VAR and expand BODY */
while ((p = find_next_token (&list_iterator, &len)) != 0)
{
char *result = 0;
free (var->value);
var->value = xstrndup (p, len);
result = allocated_variable_expand (body);
o = variable_buffer_output (o, result, strlen (result));
o = variable_buffer_output (o, " ", 1);
doneany = 1;
free (result);
}
if (doneany)
/* Kill the last space. */
--o;
pop_variable_scope ();
free (varname);
free (list);
return o;
}
struct a_word
{
struct a_word *next;
struct a_word *chain;
char *str;
int length;
int matched;
};
static unsigned long
a_word_hash_1 (const void *key)
{
return_STRING_HASH_1 (((struct a_word const *) key)->str);
}
static unsigned long
a_word_hash_2 (const void *key)
{
return_STRING_HASH_2 (((struct a_word const *) key)->str);
}
static int
a_word_hash_cmp (const void *x, const void *y)
{
int result = ((struct a_word const *) x)->length - ((struct a_word const *) y)->length;
if (result)
return result;
return_STRING_COMPARE (((struct a_word const *) x)->str,
((struct a_word const *) y)->str);
}
struct a_pattern
{
struct a_pattern *next;
char *str;
char *percent;
int length;
int save_c;
};
static char *
func_filter_filterout (char *o, char **argv, const char *funcname)
{
struct a_word *wordhead;
struct a_word **wordtail;
struct a_word *wp;
struct a_pattern *pathead;
struct a_pattern **pattail;
struct a_pattern *pp;
struct hash_table a_word_table;
int is_filter = streq (funcname, "filter");
const char *pat_iterator = argv[0];
const char *word_iterator = argv[1];
int literals = 0;
int words = 0;
int hashing = 0;
char *p;
unsigned int len;
/* Chop ARGV[0] up into patterns to match against the words. */
pattail = &pathead;
while ((p = find_next_token (&pat_iterator, &len)) != 0)
{
struct a_pattern *pat = alloca (sizeof (struct a_pattern));
*pattail = pat;
pattail = &pat->next;
if (*pat_iterator != '\0')
++pat_iterator;
pat->str = p;
pat->length = len;
pat->save_c = p[len];
p[len] = '\0';
pat->percent = find_percent (p);
if (pat->percent == 0)
literals++;
}
*pattail = 0;
/* Chop ARGV[1] up into words to match against the patterns. */
wordtail = &wordhead;
while ((p = find_next_token (&word_iterator, &len)) != 0)
{
struct a_word *word = alloca (sizeof (struct a_word));
*wordtail = word;
wordtail = &word->next;
if (*word_iterator != '\0')
++word_iterator;
p[len] = '\0';
word->str = p;
word->length = len;
word->matched = 0;
word->chain = 0;
words++;
}
*wordtail = 0;
/* Only use a hash table if arg list lengths justifies the cost. */
hashing = (literals >= 2 && (literals * words) >= 10);
if (hashing)
{
hash_init (&a_word_table, words, a_word_hash_1, a_word_hash_2,
a_word_hash_cmp);
for (wp = wordhead; wp != 0; wp = wp->next)
{
struct a_word *owp = hash_insert (&a_word_table, wp);
if (owp)
wp->chain = owp;
}
}
if (words)
{
int doneany = 0;
/* Run each pattern through the words, killing words. */
for (pp = pathead; pp != 0; pp = pp->next)
{
if (pp->percent)
for (wp = wordhead; wp != 0; wp = wp->next)
wp->matched |= pattern_matches (pp->str, pp->percent, wp->str);
else if (hashing)
{
struct a_word a_word_key;
a_word_key.str = pp->str;
a_word_key.length = pp->length;
wp = hash_find_item (&a_word_table, &a_word_key);
while (wp)
{
wp->matched |= 1;
wp = wp->chain;
}
}
else
for (wp = wordhead; wp != 0; wp = wp->next)
wp->matched |= (wp->length == pp->length
&& strneq (pp->str, wp->str, wp->length));
}
/* Output the words that matched (or didn't, for filter-out). */
for (wp = wordhead; wp != 0; wp = wp->next)
if (is_filter ? wp->matched : !wp->matched)
{
o = variable_buffer_output (o, wp->str, strlen (wp->str));
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
if (doneany)
/* Kill the last space. */
--o;
}
for (pp = pathead; pp != 0; pp = pp->next)
pp->str[pp->length] = pp->save_c;
if (hashing)
hash_free (&a_word_table, 0);
return o;
}
static char *
func_strip (char *o, char **argv, const char *funcname UNUSED)
{
const char *p = argv[0];
int doneany = 0;
while (*p != '\0')
{
int i=0;
const char *word_start;
while (isspace ((unsigned char)*p))
++p;
word_start = p;
for (i=0; *p != '\0' && !isspace ((unsigned char)*p); ++p, ++i)
{}
if (!i)
break;
o = variable_buffer_output (o, word_start, i);
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
if (doneany)
/* Kill the last space. */
--o;
return o;
}
/*
Print a warning or fatal message.
*/
static char *
func_error (char *o, char **argv, const char *funcname)
{
char **argvp;
char *msg, *p;
int len;
/* The arguments will be broken on commas. Rather than create yet
another special case where function arguments aren't broken up,
just create a format string that puts them back together. */
for (len=0, argvp=argv; *argvp != 0; ++argvp)
len += strlen (*argvp) + 2;
p = msg = alloca (len + 1);
for (argvp=argv; argvp[1] != 0; ++argvp)
{
strcpy (p, *argvp);
p += strlen (*argvp);
*(p++) = ',';
*(p++) = ' ';
}
strcpy (p, *argvp);
switch (*funcname) {
case 'e':
fatal (reading_file, "%s", msg);
case 'w':
error (reading_file, "%s", msg);
break;
case 'i':
printf ("%s\n", msg);
fflush(stdout);
break;
default:
fatal (*expanding_var, "Internal error: func_error: '%s'", funcname);
}
/* The warning function expands to the empty string. */
return o;
}
/*
chop argv[0] into words, and sort them.
*/
static char *
func_sort (char *o, char **argv, const char *funcname UNUSED)
{
const char *t;
char **words;
int wordi;
char *p;
unsigned int len;
int i;
/* Find the maximum number of words we'll have. */
t = argv[0];
wordi = 0;
while ((p = find_next_token (&t, NULL)) != 0)
{
++t;
++wordi;
}
words = xmalloc ((wordi == 0 ? 1 : wordi) * sizeof (char *));
/* Now assign pointers to each string in the array. */
t = argv[0];
wordi = 0;
while ((p = find_next_token (&t, &len)) != 0)
{
++t;
p[len] = '\0';
words[wordi++] = p;
}
if (wordi)
{
/* Now sort the list of words. */
qsort (words, wordi, sizeof (char *), alpha_compare);
/* Now write the sorted list, uniquified. */
for (i = 0; i < wordi; ++i)
{
len = strlen (words[i]);
if (i == wordi - 1 || strlen (words[i + 1]) != len
|| strcmp (words[i], words[i + 1]))
{
o = variable_buffer_output (o, words[i], len);
o = variable_buffer_output (o, " ", 1);
}
}
/* Kill the last space. */
--o;
}
free (words);
return o;
}
/*
$(if condition,true-part[,false-part])
CONDITION is false iff it evaluates to an empty string. White
space before and after condition are stripped before evaluation.
If CONDITION is true, then TRUE-PART is evaluated, otherwise FALSE-PART is
evaluated (if it exists). Because only one of the two PARTs is evaluated,
you can use $(if ...) to create side-effects (with $(shell ...), for
example).
*/
static char *
func_if (char *o, char **argv, const char *funcname UNUSED)
{
const char *begp = argv[0];
const char *endp = begp + strlen (argv[0]) - 1;
int result = 0;
/* Find the result of the condition: if we have a value, and it's not
empty, the condition is true. If we don't have a value, or it's the
empty string, then it's false. */
strip_whitespace (&begp, &endp);
if (begp <= endp)
{
char *expansion = expand_argument (begp, endp+1);
result = strlen (expansion);
free (expansion);
}
/* If the result is true (1) we want to eval the first argument, and if
it's false (0) we want to eval the second. If the argument doesn't
exist we do nothing, otherwise expand it and add to the buffer. */
argv += 1 + !result;
if (*argv)
{
char *expansion = expand_argument (*argv, NULL);
o = variable_buffer_output (o, expansion, strlen (expansion));
free (expansion);
}
return o;
}
/*
$(or condition1[,condition2[,condition3[...]]])
A CONDITION is false iff it evaluates to an empty string. White
space before and after CONDITION are stripped before evaluation.
CONDITION1 is evaluated. If it's true, then this is the result of
expansion. If it's false, CONDITION2 is evaluated, and so on. If none of
the conditions are true, the expansion is the empty string.
Once a CONDITION is true no further conditions are evaluated
(short-circuiting).
*/
static char *
func_or (char *o, char **argv, const char *funcname UNUSED)
{
for ( ; *argv ; ++argv)
{
const char *begp = *argv;
const char *endp = begp + strlen (*argv) - 1;
char *expansion;
int result = 0;
/* Find the result of the condition: if it's false keep going. */
strip_whitespace (&begp, &endp);
if (begp > endp)
continue;
expansion = expand_argument (begp, endp+1);
result = strlen (expansion);
/* If the result is false keep going. */
if (!result)
{
free (expansion);
continue;
}
/* It's true! Keep this result and return. */
o = variable_buffer_output (o, expansion, result);
free (expansion);
break;
}
return o;
}
/*
$(and condition1[,condition2[,condition3[...]]])
A CONDITION is false iff it evaluates to an empty string. White
space before and after CONDITION are stripped before evaluation.
CONDITION1 is evaluated. If it's false, then this is the result of
expansion. If it's true, CONDITION2 is evaluated, and so on. If all of
the conditions are true, the expansion is the result of the last condition.
Once a CONDITION is false no further conditions are evaluated
(short-circuiting).
*/
static char *
func_and (char *o, char **argv, const char *funcname UNUSED)
{
char *expansion;
int result;
while (1)
{
const char *begp = *argv;
const char *endp = begp + strlen (*argv) - 1;
/* An empty condition is always false. */
strip_whitespace (&begp, &endp);
if (begp > endp)
return o;
expansion = expand_argument (begp, endp+1);
result = strlen (expansion);
/* If the result is false, stop here: we're done. */
if (!result)
break;
/* Otherwise the result is true. If this is the last one, keep this
result and quit. Otherwise go on to the next one! */
if (*(++argv))
free (expansion);
else
{
o = variable_buffer_output (o, expansion, result);
break;
}
}
free (expansion);
return o;
}
static char *
func_wildcard (char *o, char **argv, const char *funcname UNUSED)
{
#ifdef _AMIGA
o = wildcard_expansion (argv[0], o);
#else
char *p = string_glob (argv[0]);
o = variable_buffer_output (o, p, strlen (p));
#endif
return o;
}
/*
$(eval <makefile string>)
Always resolves to the empty string.
Treat the arguments as a segment of makefile, and parse them.
*/
static char *
func_eval (char *o, char **argv, const char *funcname UNUSED)
{
char *buf;
unsigned int len;
/* Eval the buffer. Pop the current variable buffer setting so that the
eval'd code can use its own without conflicting. */
install_variable_buffer (&buf, &len);
eval_buffer (argv[0]);
restore_variable_buffer (buf, len);
return o;
}
static char *
func_value (char *o, char **argv, const char *funcname UNUSED)
{
/* Look up the variable. */
struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
/* Copy its value into the output buffer without expanding it. */
if (v)
o = variable_buffer_output (o, v->value, strlen(v->value));
return o;
}
/*
\r is replaced on UNIX as well. Is this desirable?
*/
static void
fold_newlines (char *buffer, unsigned int *length, int trim_newlines)
{
char *dst = buffer;
char *src = buffer;
char *last_nonnl = buffer - 1;
src[*length] = 0;
for (; *src != '\0'; ++src)
{
if (src[0] == '\r' && src[1] == '\n')
continue;
if (*src == '\n')
{
*dst++ = ' ';
}
else
{
last_nonnl = dst;
*dst++ = *src;
}
}
if (!trim_newlines && (last_nonnl < (dst - 2)))
last_nonnl = dst - 2;
*(++last_nonnl) = '\0';
*length = last_nonnl - buffer;
}
int shell_function_pid = 0, shell_function_completed;
#ifdef WINDOWS32
/*untested*/
#include <windows.h>
#include <io.h>
#include "sub_proc.h"
void
windows32_openpipe (int *pipedes, pid_t *pid_p, char **command_argv, char **envp)
{
SECURITY_ATTRIBUTES saAttr;
HANDLE hIn;
HANDLE hErr;
HANDLE hChildOutRd;
HANDLE hChildOutWr;
HANDLE hProcess;
saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
if (DuplicateHandle (GetCurrentProcess(),
GetStdHandle(STD_INPUT_HANDLE),
GetCurrentProcess(),
&hIn,
0,
TRUE,
DUPLICATE_SAME_ACCESS) == FALSE) {
fatal (NILF, _("windows32_openpipe(): DuplicateHandle(In) failed (e=%ld)\n"),
GetLastError());
}
if (DuplicateHandle(GetCurrentProcess(),
GetStdHandle(STD_ERROR_HANDLE),
GetCurrentProcess(),
&hErr,
0,
TRUE,
DUPLICATE_SAME_ACCESS) == FALSE) {
fatal (NILF, _("windows32_open_pipe(): DuplicateHandle(Err) failed (e=%ld)\n"),
GetLastError());
}
if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
fatal (NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError());
hProcess = process_init_fd(hIn, hChildOutWr, hErr);
if (!hProcess)
fatal (NILF, _("windows32_openpipe(): process_init_fd() failed\n"));
/* make sure that CreateProcess() has Path it needs */
sync_Path_environment();
/* `sync_Path_environment' may realloc `environ', so take note of
the new value. */
envp = environ;
if (!process_begin(hProcess, command_argv, envp, command_argv[0], NULL)) {
/* register process for wait */
process_register(hProcess);
/* set the pid for returning to caller */
*pid_p = (pid_t) hProcess;
/* set up to read data from child */
pipedes[0] = _open_osfhandle((intptr_t) hChildOutRd, O_RDONLY);
/* this will be closed almost right away */
pipedes[1] = _open_osfhandle((intptr_t) hChildOutWr, O_APPEND);
} else {
/* reap/cleanup the failed process */
process_cleanup(hProcess);
/* close handles which were duplicated, they weren't used */
CloseHandle(hIn);
CloseHandle(hErr);
/* close pipe handles, they won't be used */
CloseHandle(hChildOutRd);
CloseHandle(hChildOutWr);
/* set status for return */
pipedes[0] = pipedes[1] = -1;
*pid_p = (pid_t)-1;
}
}
#endif
#ifdef __MSDOS__
FILE *
msdos_openpipe (int* pipedes, int *pidp, char *text)
{
FILE *fpipe=0;
/* MSDOS can't fork, but it has `popen'. */
struct variable *sh = lookup_variable ("SHELL", 5);
int e;
extern int dos_command_running, dos_status;
/* Make sure not to bother processing an empty line. */
while (isblank ((unsigned char)*text))
++text;
if (*text == '\0')
return 0;
if (sh)
{
char buf[PATH_MAX + 7];
/* This makes sure $SHELL value is used by $(shell), even
though the target environment is not passed to it. */
sprintf (buf, "SHELL=%s", sh->value);
putenv (buf);
}
e = errno;
errno = 0;
dos_command_running = 1;
dos_status = 0;
/* If dos_status becomes non-zero, it means the child process
was interrupted by a signal, like SIGINT or SIGQUIT. See
fatal_error_signal in commands.c. */
fpipe = popen (text, "rt");
dos_command_running = 0;
if (!fpipe || dos_status)
{
pipedes[0] = -1;
*pidp = -1;
if (dos_status)
errno = EINTR;
else if (errno == 0)
errno = ENOMEM;
shell_function_completed = -1;
}
else
{
pipedes[0] = fileno (fpipe);
*pidp = 42; /* Yes, the Meaning of Life, the Universe, and Everything! */
errno = e;
shell_function_completed = 1;
}
return fpipe;
}
#endif
/*
Do shell spawning, with the naughty bits for different OSes.
*/
#ifdef VMS
/* VMS can't do $(shell ...) */
char *
func_shell_base (char *o, char **argv, int trim_newlines)
{
fprintf (stderr, "This platform does not support shell\n");
die (EXIT_FAILURE);
}
#define func_shell 0
#else
#ifndef _AMIGA
char *
func_shell_base (char *o, char **argv, int trim_newlines)
{
char *batch_filename = NULL;
#ifdef __MSDOS__
FILE *fpipe;
#endif
char **command_argv;
const char *error_prefix;
char **envp;
int pipedes[2];
pid_t pid;
#ifndef __MSDOS__
#ifdef WINDOWS32
/* Reset just_print_flag. This is needed on Windows when batch files
are used to run the commands, because we normally refrain from
creating batch files under -n. */
int j_p_f = just_print_flag;
just_print_flag = 0;
#endif
/* Construct the argument list. */
command_argv = construct_command_argv (argv[0], NULL, NULL, 0,
&batch_filename);
if (command_argv == 0)
{
#ifdef WINDOWS32
just_print_flag = j_p_f;
#endif
return o;
}
#endif
/* Using a target environment for `shell' loses in cases like:
export var = $(shell echo foobie)
because target_environment hits a loop trying to expand $(var)
to put it in the environment. This is even more confusing when
var was not explicitly exported, but just appeared in the
calling environment.
See Savannah bug #10593.
envp = target_environment (NILF);
*/
envp = environ;
/* For error messages. */
if (reading_file && reading_file->filenm)
{
char *p = alloca (strlen (reading_file->filenm)+11+4);
sprintf (p, "%s:%lu: ", reading_file->filenm, reading_file->lineno);
error_prefix = p;
}
else
error_prefix = "";
#if defined(__MSDOS__)
fpipe = msdos_openpipe (pipedes, &pid, argv[0]);
if (pipedes[0] < 0)
{
perror_with_name (error_prefix, "pipe");
return o;
}
#elif defined(WINDOWS32)
windows32_openpipe (pipedes, &pid, command_argv, envp);
/* Restore the value of just_print_flag. */
just_print_flag = j_p_f;
if (pipedes[0] < 0)
{
/* Open of the pipe failed, mark as failed execution. */
shell_function_completed = -1;
return o;
}
else
#else
if (pipe (pipedes) < 0)
{
perror_with_name (error_prefix, "pipe");
return o;
}
# ifdef __EMX__
/* close some handles that are unnecessary for the child process */
CLOSE_ON_EXEC(pipedes[1]);
CLOSE_ON_EXEC(pipedes[0]);
/* Never use fork()/exec() here! Use spawn() instead in exec_command() */
pid = child_execute_job (0, pipedes[1], command_argv, envp);
if (pid < 0)
perror_with_name (error_prefix, "spawn");
# else /* ! __EMX__ */
pid = fork ();
if (pid < 0)
perror_with_name (error_prefix, "fork");
else if (pid == 0)
child_execute_job (0, pipedes[1], command_argv, envp);
else
# endif
#endif
{
/* We are the parent. */
char *buffer;
unsigned int maxlen, i;
int cc;
/* Record the PID for reap_children. */
shell_function_pid = pid;
#ifndef __MSDOS__
shell_function_completed = 0;
/* Free the storage only the child needed. */
free (command_argv[0]);
free (command_argv);
/* Close the write side of the pipe. We test for -1, since
pipedes[1] is -1 on MS-Windows, and some versions of MS
libraries barf when `close' is called with -1. */
if (pipedes[1] >= 0)
close (pipedes[1]);
#endif
/* Set up and read from the pipe. */
maxlen = 200;
buffer = xmalloc (maxlen + 1);
/* Read from the pipe until it gets EOF. */
for (i = 0; ; i += cc)
{
if (i == maxlen)
{
maxlen += 512;
buffer = xrealloc (buffer, maxlen + 1);
}
EINTRLOOP (cc, read (pipedes[0], &buffer[i], maxlen - i));
if (cc <= 0)
break;
}
buffer[i] = '\0';
/* Close the read side of the pipe. */
#ifdef __MSDOS__
if (fpipe)
(void) pclose (fpipe);
#else
(void) close (pipedes[0]);
#endif
/* Loop until child_handler or reap_children() sets
shell_function_completed to the status of our child shell. */
while (shell_function_completed == 0)
reap_children (1, 0);
if (batch_filename) {
DB (DB_VERBOSE, (_("Cleaning up temporary batch file %s\n"),
batch_filename));
remove (batch_filename);
free (batch_filename);
}
shell_function_pid = 0;
/* The child_handler function will set shell_function_completed
to 1 when the child dies normally, or to -1 if it
dies with status 127, which is most likely an exec fail. */
if (shell_function_completed == -1)
{
/* This likely means that the execvp failed, so we should just
write the error message in the pipe from the child. */
fputs (buffer, stderr);
fflush (stderr);
}
else
{
/* The child finished normally. Replace all newlines in its output
with spaces, and put that in the variable output buffer. */
fold_newlines (buffer, &i, trim_newlines);
o = variable_buffer_output (o, buffer, i);
}
free (buffer);
}
return o;
}
#else /* _AMIGA */
/* Do the Amiga version of func_shell. */
char *
func_shell_base (char *o, char **argv, int trim_newlines)
{
/* Amiga can't fork nor spawn, but I can start a program with
redirection of my choice. However, this means that we
don't have an opportunity to reopen stdout to trap it. Thus,
we save our own stdout onto a new descriptor and dup a temp
file's descriptor onto our stdout temporarily. After we
spawn the shell program, we dup our own stdout back to the
stdout descriptor. The buffer reading is the same as above,
except that we're now reading from a file. */
#include <dos/dos.h>
#include <proto/dos.h>
BPTR child_stdout;
char tmp_output[FILENAME_MAX];
unsigned int maxlen = 200, i;
int cc;
char * buffer, * ptr;
char ** aptr;
int len = 0;
char* batch_filename = NULL;
/* Construct the argument list. */
command_argv = construct_command_argv (argv[0], NULL, NULL, 0,
&batch_filename);
if (command_argv == 0)
return o;
/* Note the mktemp() is a security hole, but this only runs on Amiga.
Ideally we would use main.c:open_tmpfile(), but this uses a special
Open(), not fopen(), and I'm not familiar enough with the code to mess
with it. */
strcpy (tmp_output, "t:MakeshXXXXXXXX");
mktemp (tmp_output);
child_stdout = Open (tmp_output, MODE_NEWFILE);
for (aptr=command_argv; *aptr; aptr++)
len += strlen (*aptr) + 1;
buffer = xmalloc (len + 1);
ptr = buffer;
for (aptr=command_argv; *aptr; aptr++)
{
strcpy (ptr, *aptr);
ptr += strlen (ptr) + 1;
*ptr ++ = ' ';
*ptr = 0;
}
ptr[-1] = '\n';
Execute (buffer, NULL, child_stdout);
free (buffer);
Close (child_stdout);
child_stdout = Open (tmp_output, MODE_OLDFILE);
buffer = xmalloc (maxlen);
i = 0;
do
{
if (i == maxlen)
{
maxlen += 512;
buffer = xrealloc (buffer, maxlen + 1);
}
cc = Read (child_stdout, &buffer[i], maxlen - i);
if (cc > 0)
i += cc;
} while (cc > 0);
Close (child_stdout);
fold_newlines (buffer, &i, trim_newlines);
o = variable_buffer_output (o, buffer, i);
free (buffer);
return o;
}
#endif /* _AMIGA */
char *
func_shell (char *o, char **argv, const char *funcname UNUSED)
{
return func_shell_base (o, argv, 1);
}
#endif /* !VMS */
#ifdef EXPERIMENTAL
/*
equality. Return is string-boolean, ie, the empty string is false.
*/
static char *
func_eq (char *o, char **argv, char *funcname)
{
int result = ! strcmp (argv[0], argv[1]);
o = variable_buffer_output (o, result ? "1" : "", result);
return o;
}
/*
string-boolean not operator.
*/
static char *
func_not (char *o, char **argv, char *funcname)
{
const char *s = argv[0];
int result = 0;
while (isspace ((unsigned char)*s))
s++;
result = ! (*s);
o = variable_buffer_output (o, result ? "1" : "", result);
return o;
}
#endif
#ifdef HAVE_DOS_PATHS
#define IS_ABSOLUTE(n) (n[0] && n[1] == ':')
#define ROOT_LEN 3
#else
#define IS_ABSOLUTE(n) (n[0] == '/')
#define ROOT_LEN 1
#endif
/* Return the absolute name of file NAME which does not contain any `.',
`..' components nor any repeated path separators ('/'). */
static char *
abspath (const char *name, char *apath)
{
char *dest;
const char *start, *end, *apath_limit;
unsigned long root_len = ROOT_LEN;
if (name[0] == '\0' || apath == NULL)
return NULL;
apath_limit = apath + GET_PATH_MAX;
if (!IS_ABSOLUTE(name))
{
/* It is unlikely we would make it until here but just to make sure. */
if (!starting_directory)
return NULL;
strcpy (apath, starting_directory);
#ifdef HAVE_DOS_PATHS
if (IS_PATHSEP(name[0]))
{
if (IS_PATHSEP(name[1]))
{
/* A UNC. Don't prepend a drive letter. */
apath[0] = name[0];
apath[1] = name[1];
root_len = 2;
}
/* We have /foo, an absolute file name except for the drive
letter. Assume the missing drive letter is the current
drive, which we can get if we remove from starting_directory
everything past the root directory. */
apath[root_len] = '\0';
}
#endif
dest = strchr (apath, '\0');
}
else
{
strncpy (apath, name, root_len);
apath[root_len] = '\0';
dest = apath + root_len;
/* Get past the root, since we already copied it. */
name += root_len;
#ifdef HAVE_DOS_PATHS
if (!IS_PATHSEP(apath[2]))
{
/* Convert d:foo into d:./foo and increase root_len. */
apath[2] = '.';
apath[3] = '/';
dest++;
root_len++;
/* strncpy above copied one character too many. */
name--;
}
else
apath[2] = '/'; /* make sure it's a forward slash */
#endif
}
for (start = end = name; *start != '\0'; start = end)
{
unsigned long len;
/* Skip sequence of multiple path-separators. */
while (IS_PATHSEP(*start))
++start;
/* Find end of path component. */
for (end = start; *end != '\0' && !IS_PATHSEP(*end); ++end)
;
len = end - start;
if (len == 0)
break;
else if (len == 1 && start[0] == '.')
/* nothing */;
else if (len == 2 && start[0] == '.' && start[1] == '.')
{
/* Back up to previous component, ignore if at root already. */
if (dest > apath + root_len)
for (--dest; !IS_PATHSEP(dest[-1]); --dest);
}
else
{
if (!IS_PATHSEP(dest[-1]))
*dest++ = '/';
if (dest + len >= apath_limit)
return NULL;
dest = memcpy (dest, start, len);
dest += len;
*dest = '\0';
}
}
/* Unless it is root strip trailing separator. */
if (dest > apath + root_len && IS_PATHSEP(dest[-1]))
--dest;
*dest = '\0';
return apath;
}
static char *
func_realpath (char *o, char **argv, const char *funcname UNUSED)
{
/* Expand the argument. */
const char *p = argv[0];
const char *path = 0;
int doneany = 0;
unsigned int len = 0;
#ifndef HAVE_REALPATH
struct stat st;
#endif
PATH_VAR (in);
PATH_VAR (out);
while ((path = find_next_token (&p, &len)) != 0)
{
if (len < GET_PATH_MAX)
{
strncpy (in, path, len);
in[len] = '\0';
if (
#ifdef HAVE_REALPATH
realpath (in, out)
#else
abspath (in, out) && stat (out, &st) == 0
#endif
)
{
o = variable_buffer_output (o, out, strlen (out));
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
}
}
/* Kill last space. */
if (doneany)
--o;
return o;
}
static char *
func_abspath (char *o, char **argv, const char *funcname UNUSED)
{
/* Expand the argument. */
const char *p = argv[0];
const char *path = 0;
int doneany = 0;
unsigned int len = 0;
PATH_VAR (in);
PATH_VAR (out);
while ((path = find_next_token (&p, &len)) != 0)
{
if (len < GET_PATH_MAX)
{
strncpy (in, path, len);
in[len] = '\0';
if (abspath (in, out))
{
o = variable_buffer_output (o, out, strlen (out));
o = variable_buffer_output (o, " ", 1);
doneany = 1;
}
}
}
/* Kill last space. */
if (doneany)
--o;
return o;
}
/* Lookup table for builtin functions.
This doesn't have to be sorted; we use a straight lookup. We might gain
some efficiency by moving most often used functions to the start of the
table.
If MAXIMUM_ARGS is 0, that means there is no maximum and all
comma-separated values are treated as arguments.
EXPAND_ARGS means that all arguments should be expanded before invocation.
Functions that do namespace tricks (foreach) don't automatically expand. */
static char *func_call (char *o, char **argv, const char *funcname);
static struct function_table_entry function_table_init[] =
{
/* Name/size */ /* MIN MAX EXP? Function */
{ STRING_SIZE_TUPLE("abspath"), 0, 1, 1, func_abspath},
{ STRING_SIZE_TUPLE("addprefix"), 2, 2, 1, func_addsuffix_addprefix},
{ STRING_SIZE_TUPLE("addsuffix"), 2, 2, 1, func_addsuffix_addprefix},
{ STRING_SIZE_TUPLE("basename"), 0, 1, 1, func_basename_dir},
{ STRING_SIZE_TUPLE("dir"), 0, 1, 1, func_basename_dir},
{ STRING_SIZE_TUPLE("notdir"), 0, 1, 1, func_notdir_suffix},
{ STRING_SIZE_TUPLE("subst"), 3, 3, 1, func_subst},
{ STRING_SIZE_TUPLE("suffix"), 0, 1, 1, func_notdir_suffix},
{ STRING_SIZE_TUPLE("filter"), 2, 2, 1, func_filter_filterout},
{ STRING_SIZE_TUPLE("filter-out"), 2, 2, 1, func_filter_filterout},
{ STRING_SIZE_TUPLE("findstring"), 2, 2, 1, func_findstring},
{ STRING_SIZE_TUPLE("firstword"), 0, 1, 1, func_firstword},
{ STRING_SIZE_TUPLE("flavor"), 0, 1, 1, func_flavor},
{ STRING_SIZE_TUPLE("join"), 2, 2, 1, func_join},
{ STRING_SIZE_TUPLE("lastword"), 0, 1, 1, func_lastword},
{ STRING_SIZE_TUPLE("patsubst"), 3, 3, 1, func_patsubst},
{ STRING_SIZE_TUPLE("realpath"), 0, 1, 1, func_realpath},
{ STRING_SIZE_TUPLE("shell"), 0, 1, 1, func_shell},
{ STRING_SIZE_TUPLE("sort"), 0, 1, 1, func_sort},
{ STRING_SIZE_TUPLE("strip"), 0, 1, 1, func_strip},
{ STRING_SIZE_TUPLE("wildcard"), 0, 1, 1, func_wildcard},
{ STRING_SIZE_TUPLE("word"), 2, 2, 1, func_word},
{ STRING_SIZE_TUPLE("wordlist"), 3, 3, 1, func_wordlist},
{ STRING_SIZE_TUPLE("words"), 0, 1, 1, func_words},
{ STRING_SIZE_TUPLE("origin"), 0, 1, 1, func_origin},
{ STRING_SIZE_TUPLE("foreach"), 3, 3, 0, func_foreach},
{ STRING_SIZE_TUPLE("call"), 1, 0, 1, func_call},
{ STRING_SIZE_TUPLE("info"), 0, 1, 1, func_error},
{ STRING_SIZE_TUPLE("error"), 0, 1, 1, func_error},
{ STRING_SIZE_TUPLE("warning"), 0, 1, 1, func_error},
{ STRING_SIZE_TUPLE("if"), 2, 3, 0, func_if},
{ STRING_SIZE_TUPLE("or"), 1, 0, 0, func_or},
{ STRING_SIZE_TUPLE("and"), 1, 0, 0, func_and},
{ STRING_SIZE_TUPLE("value"), 0, 1, 1, func_value},
{ STRING_SIZE_TUPLE("eval"), 0, 1, 1, func_eval},
#ifdef EXPERIMENTAL
{ STRING_SIZE_TUPLE("eq"), 2, 2, 1, func_eq},
{ STRING_SIZE_TUPLE("not"), 0, 1, 1, func_not},
#endif
};
#define FUNCTION_TABLE_ENTRIES (sizeof (function_table_init) / sizeof (struct function_table_entry))
/* These must come after the definition of function_table. */
static char *
expand_builtin_function (char *o, int argc, char **argv,
const struct function_table_entry *entry_p)
{
if (argc < (int)entry_p->minimum_args)
fatal (*expanding_var,
_("insufficient number of arguments (%d) to function `%s'"),
argc, entry_p->name);
/* I suppose technically some function could do something with no
arguments, but so far none do, so just test it for all functions here
rather than in each one. We can change it later if necessary. */
if (!argc)
return o;
if (!entry_p->func_ptr)
fatal (*expanding_var,
_("unimplemented on this platform: function `%s'"), entry_p->name);
return entry_p->func_ptr (o, argv, entry_p->name);
}
/* Check for a function invocation in *STRINGP. *STRINGP points at the
opening ( or { and is not null-terminated. If a function invocation
is found, expand it into the buffer at *OP, updating *OP, incrementing
*STRINGP past the reference and returning nonzero. If not, return zero. */
int
handle_function (char **op, const char **stringp)
{
const struct function_table_entry *entry_p;
char openparen = (*stringp)[0];
char closeparen = openparen == '(' ? ')' : '}';
const char *beg;
const char *end;
int count = 0;
char *abeg = NULL;
char **argv, **argvp;
int nargs;
beg = *stringp + 1;
entry_p = lookup_function (beg);
if (!entry_p)
return 0;
/* We found a builtin function. Find the beginning of its arguments (skip
whitespace after the name). */
beg = next_token (beg + entry_p->len);
/* Find the end of the function invocation, counting nested use of
whichever kind of parens we use. Since we're looking, count commas
to get a rough estimate of how many arguments we might have. The
count might be high, but it'll never be low. */
for (nargs=1, end=beg; *end != '\0'; ++end)
if (*end == ',')
++nargs;
else if (*end == openparen)
++count;
else if (*end == closeparen && --count < 0)
break;
if (count >= 0)
fatal (*expanding_var,
_("unterminated call to function `%s': missing `%c'"),
entry_p->name, closeparen);
*stringp = end;
/* Get some memory to store the arg pointers. */
argvp = argv = alloca (sizeof (char *) * (nargs + 2));
/* Chop the string into arguments, then a nul. As soon as we hit
MAXIMUM_ARGS (if it's >0) assume the rest of the string is part of the
last argument.
If we're expanding, store pointers to the expansion of each one. If
not, make a duplicate of the string and point into that, nul-terminating
each argument. */
if (entry_p->expand_args)
{
const char *p;
for (p=beg, nargs=0; p <= end; ++argvp)
{
const char *next;
++nargs;
if (nargs == entry_p->maximum_args
|| (! (next = find_next_argument (openparen, closeparen, p, end))))
next = end;
*argvp = expand_argument (p, next);
p = next + 1;
}
}
else
{
int len = end - beg;
char *p, *aend;
abeg = xmalloc (len+1);
memcpy (abeg, beg, len);
abeg[len] = '\0';
aend = abeg + len;
for (p=abeg, nargs=0; p <= aend; ++argvp)
{
char *next;
++nargs;
if (nargs == entry_p->maximum_args
|| (! (next = find_next_argument (openparen, closeparen, p, aend))))
next = aend;
*argvp = p;
*next = '\0';
p = next + 1;
}
}
*argvp = NULL;
/* Finally! Run the function... */
*op = expand_builtin_function (*op, nargs, argv, entry_p);
/* Free memory. */
if (entry_p->expand_args)
for (argvp=argv; *argvp != 0; ++argvp)
free (*argvp);
if (abeg)
free (abeg);
return 1;
}
/* User-defined functions. Expand the first argument as either a builtin
function or a make variable, in the context of the rest of the arguments
assigned to $1, $2, ... $N. $0 is the name of the function. */
static char *
func_call (char *o, char **argv, const char *funcname UNUSED)
{
static int max_args = 0;
char *fname;
char *cp;
char *body;
int flen;
int i;
int saved_args;
const struct function_table_entry *entry_p;
struct variable *v;
/* There is no way to define a variable with a space in the name, so strip
leading and trailing whitespace as a favor to the user. */
fname = argv[0];
while (*fname != '\0' && isspace ((unsigned char)*fname))
++fname;
cp = fname + strlen (fname) - 1;
while (cp > fname && isspace ((unsigned char)*cp))
--cp;
cp[1] = '\0';
/* Calling nothing is a no-op */
if (*fname == '\0')
return o;
/* Are we invoking a builtin function? */
entry_p = lookup_function (fname);
if (entry_p)
{
/* How many arguments do we have? */
for (i=0; argv[i+1]; ++i)
;
return expand_builtin_function (o, i, argv+1, entry_p);
}
/* Not a builtin, so the first argument is the name of a variable to be
expanded and interpreted as a function. Find it. */
flen = strlen (fname);
v = lookup_variable (fname, flen);
if (v == 0)
warn_undefined (fname, flen);
if (v == 0 || *v->value == '\0')
return o;
body = alloca (flen + 4);
body[0] = '$';
body[1] = '(';
memcpy (body + 2, fname, flen);
body[flen+2] = ')';
body[flen+3] = '\0';
/* Set up arguments $(1) .. $(N). $(0) is the function name. */
push_new_variable_scope ();
for (i=0; *argv; ++i, ++argv)
{
char num[11];
sprintf (num, "%d", i);
define_variable (num, strlen (num), *argv, o_automatic, 0);
}
/* If the number of arguments we have is < max_args, it means we're inside
a recursive invocation of $(call ...). Fill in the remaining arguments
in the new scope with the empty value, to hide them from this
invocation. */
for (; i < max_args; ++i)
{
char num[11];
sprintf (num, "%d", i);
define_variable (num, strlen (num), "", o_automatic, 0);
}
/* Expand the body in the context of the arguments, adding the result to
the variable buffer. */
v->exp_count = EXP_COUNT_MAX;
saved_args = max_args;
max_args = i;
o = variable_expand_string (o, body, flen+3);
max_args = saved_args;
v->exp_count = 0;
pop_variable_scope ();
return o + strlen (o);
}
void
hash_init_function_table (void)
{
hash_init (&function_table, FUNCTION_TABLE_ENTRIES * 2,
function_table_entry_hash_1, function_table_entry_hash_2,
function_table_entry_hash_cmp);
hash_load (&function_table, function_table_init,
FUNCTION_TABLE_ENTRIES, sizeof (struct function_table_entry));
}
|