aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/m4/src/builtin.c
blob: 302a86a4952026525f968d2dc848f5c84c55b68e (plain) (blame)
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
/* GNU m4 -- A simple macro processor

   Copyright (C) 1989-1994, 2000, 2004, 2006-2014, 2016 Free Software
   Foundation, Inc.

   This file is part of GNU M4.

   GNU M4 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 M4 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/>.
*/

/* Code for all builtin macros, initialization of symbol table, and
   expansion of user defined macros.  */

#include "m4.h"

#include "execute.h"
#include "memchr2.h"
#include "progname.h"
#include "regex.h"
#include "spawn-pipe.h"
#include "wait-process.h"

#define ARG(i) (argc > (i) ? TOKEN_DATA_TEXT (argv[i]) : "")

/* Initialization of builtin and predefined macros.  The table
   "builtin_tab" is both used for initialization, and by the "builtin"
   builtin.  */

#define DECLARE(name) \
  static void name (struct obstack *, int, token_data **)

DECLARE (m4___file__);
DECLARE (m4___line__);
DECLARE (m4___program__);
DECLARE (m4_builtin);
DECLARE (m4_changecom);
DECLARE (m4_changequote);
#ifdef ENABLE_CHANGEWORD
DECLARE (m4_changeword);
#endif
DECLARE (m4_debugmode);
DECLARE (m4_debugfile);
DECLARE (m4_decr);
DECLARE (m4_define);
DECLARE (m4_defn);
DECLARE (m4_divert);
DECLARE (m4_divnum);
DECLARE (m4_dnl);
DECLARE (m4_dumpdef);
DECLARE (m4_errprint);
DECLARE (m4_esyscmd);
DECLARE (m4_eval);
DECLARE (m4_format);
DECLARE (m4_ifdef);
DECLARE (m4_ifelse);
DECLARE (m4_include);
DECLARE (m4_incr);
DECLARE (m4_index);
DECLARE (m4_indir);
DECLARE (m4_len);
DECLARE (m4_m4exit);
DECLARE (m4_m4wrap);
DECLARE (m4_maketemp);
DECLARE (m4_mkstemp);
DECLARE (m4_patsubst);
DECLARE (m4_popdef);
DECLARE (m4_pushdef);
DECLARE (m4_regexp);
DECLARE (m4_shift);
DECLARE (m4_sinclude);
DECLARE (m4_substr);
DECLARE (m4_syscmd);
DECLARE (m4_sysval);
DECLARE (m4_traceoff);
DECLARE (m4_traceon);
DECLARE (m4_translit);
DECLARE (m4_undefine);
DECLARE (m4_undivert);

#undef DECLARE

static builtin const builtin_tab[] =
{

  /* name               GNUext  macros  blind   function */

  { "__file__",         true,   false,  false,  m4___file__ },
  { "__line__",         true,   false,  false,  m4___line__ },
  { "__program__",      true,   false,  false,  m4___program__ },
  { "builtin",          true,   true,   true,   m4_builtin },
  { "changecom",        false,  false,  false,  m4_changecom },
  { "changequote",      false,  false,  false,  m4_changequote },
#ifdef ENABLE_CHANGEWORD
  { "changeword",       true,   false,  true,   m4_changeword },
#endif
  { "debugmode",        true,   false,  false,  m4_debugmode },
  { "debugfile",        true,   false,  false,  m4_debugfile },
  { "decr",             false,  false,  true,   m4_decr },
  { "define",           false,  true,   true,   m4_define },
  { "defn",             false,  false,  true,   m4_defn },
  { "divert",           false,  false,  false,  m4_divert },
  { "divnum",           false,  false,  false,  m4_divnum },
  { "dnl",              false,  false,  false,  m4_dnl },
  { "dumpdef",          false,  false,  false,  m4_dumpdef },
  { "errprint",         false,  false,  true,   m4_errprint },
  { "esyscmd",          true,   false,  true,   m4_esyscmd },
  { "eval",             false,  false,  true,   m4_eval },
  { "format",           true,   false,  true,   m4_format },
  { "ifdef",            false,  false,  true,   m4_ifdef },
  { "ifelse",           false,  false,  true,   m4_ifelse },
  { "include",          false,  false,  true,   m4_include },
  { "incr",             false,  false,  true,   m4_incr },
  { "index",            false,  false,  true,   m4_index },
  { "indir",            true,   true,   true,   m4_indir },
  { "len",              false,  false,  true,   m4_len },
  { "m4exit",           false,  false,  false,  m4_m4exit },
  { "m4wrap",           false,  false,  true,   m4_m4wrap },
  { "maketemp",         false,  false,  true,   m4_maketemp },
  { "mkstemp",          false,  false,  true,   m4_mkstemp },
  { "patsubst",         true,   false,  true,   m4_patsubst },
  { "popdef",           false,  false,  true,   m4_popdef },
  { "pushdef",          false,  true,   true,   m4_pushdef },
  { "regexp",           true,   false,  true,   m4_regexp },
  { "shift",            false,  false,  true,   m4_shift },
  { "sinclude",         false,  false,  true,   m4_sinclude },
  { "substr",           false,  false,  true,   m4_substr },
  { "syscmd",           false,  false,  true,   m4_syscmd },
  { "sysval",           false,  false,  false,  m4_sysval },
  { "traceoff",         false,  false,  false,  m4_traceoff },
  { "traceon",          false,  false,  false,  m4_traceon },
  { "translit",         false,  false,  true,   m4_translit },
  { "undefine",         false,  false,  true,   m4_undefine },
  { "undivert",         false,  false,  false,  m4_undivert },

  { 0,                  false,  false,  false,  0 },

  /* placeholder is intentionally stuck after the table end delimiter,
     so that we can easily find it, while not treating it as a real
     builtin.  */
  { "placeholder",      true,   false,  false,  m4_placeholder },
};

static predefined const predefined_tab[] =
{
#if UNIX
  { "unix",     "__unix__",     "" },
#endif
#if W32_NATIVE
  { "windows",  "__windows__",  "" },
#endif
#if OS2
  { "os2",      "__os2__",      "" },
#endif
#if !UNIX && !W32_NATIVE && !OS2
# warning Platform macro not provided
#endif
  { NULL,       "__gnu__",      "" },

  { NULL,       NULL,           NULL },
};

/*----------------------------------------.
| Find the builtin, which lives on ADDR.  |
`----------------------------------------*/

const builtin * M4_GNUC_PURE
find_builtin_by_addr (builtin_func *func)
{
  const builtin *bp;

  for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
    if (bp->func == func)
      return bp;
  if (func == m4_placeholder)
    return bp + 1;
  return NULL;
}

/*----------------------------------------------------------.
| Find the builtin, which has NAME.  On failure, return the |
| placeholder builtin.                                      |
`----------------------------------------------------------*/

const builtin * M4_GNUC_PURE
find_builtin_by_name (const char *name)
{
  const builtin *bp;

  for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
    if (STREQ (bp->name, name))
      return bp;
  return bp + 1;
}

/*----------------------------------------------------------------.
| Install a builtin macro with name NAME, bound to the C function |
| given in BP.  MODE is SYMBOL_INSERT or SYMBOL_PUSHDEF.          |
`----------------------------------------------------------------*/

void
define_builtin (const char *name, const builtin *bp, symbol_lookup mode)
{
  symbol *sym;

  sym = lookup_symbol (name, mode);
  SYMBOL_TYPE (sym) = TOKEN_FUNC;
  SYMBOL_MACRO_ARGS (sym) = bp->groks_macro_args;
  SYMBOL_BLIND_NO_ARGS (sym) = bp->blind_if_no_args;
  SYMBOL_FUNC (sym) = bp->func;
}

/* Storage for the compiled regular expression of
   --warn-macro-sequence.  */
static struct re_pattern_buffer macro_sequence_buf;

/* Storage for the matches of --warn-macro-sequence.  */
static struct re_registers macro_sequence_regs;

/* True if --warn-macro-sequence is in effect.  */
static bool macro_sequence_inuse;

/*----------------------------------------.
| Clean up regular expression variables.  |
`----------------------------------------*/

static void
free_pattern_buffer (struct re_pattern_buffer *buf, struct re_registers *regs)
{
  regfree (buf);
  free (regs->start);
  free (regs->end);
}

/*-----------------------------------------------------------------.
| Set the regular expression of --warn-macro-sequence that will be |
| checked during define and pushdef.  Exit on failure.             |
`-----------------------------------------------------------------*/
void
set_macro_sequence (const char *regexp)
{
  const char *msg;

  if (! regexp)
    regexp = DEFAULT_MACRO_SEQUENCE;
  else if (regexp[0] == '\0')
    {
      macro_sequence_inuse = false;
      return;
    }

  msg = re_compile_pattern (regexp, strlen (regexp), &macro_sequence_buf);
  if (msg != NULL)
    {
      M4ERROR ((EXIT_FAILURE, 0,
                "--warn-macro-sequence: bad regular expression `%s': %s",
                regexp, msg));
    }
  re_set_registers (&macro_sequence_buf, &macro_sequence_regs,
                    macro_sequence_regs.num_regs,
                    macro_sequence_regs.start, macro_sequence_regs.end);
  macro_sequence_inuse = true;
}

/*-----------------------------------------------------------.
| Free dynamic memory utilized by the macro sequence regular |
| expression during the define builtin.                      |
`-----------------------------------------------------------*/
void
free_macro_sequence (void)
{
  free_pattern_buffer (&macro_sequence_buf, &macro_sequence_regs);
}

/*-----------------------------------------------------------------.
| Define a predefined or user-defined macro, with name NAME, and   |
| expansion TEXT.  MODE destinguishes between the "define" and the |
| "pushdef" case.  It is also used from main.                      |
`-----------------------------------------------------------------*/

void
define_user_macro (const char *name, const char *text, symbol_lookup mode)
{
  symbol *s;
  char *defn = xstrdup (text ? text : "");

  s = lookup_symbol (name, mode);
  if (SYMBOL_TYPE (s) == TOKEN_TEXT)
    free (SYMBOL_TEXT (s));

  SYMBOL_TYPE (s) = TOKEN_TEXT;
  SYMBOL_TEXT (s) = defn;

  /* Implement --warn-macro-sequence.  */
  if (macro_sequence_inuse && text)
    {
      regoff_t offset = 0;
      size_t len = strlen (defn);

      while ((offset = re_search (&macro_sequence_buf, defn, len, offset,
                                  len - offset, &macro_sequence_regs)) >= 0)
        {
          /* Skip empty matches.  */
          if (macro_sequence_regs.start[0] == macro_sequence_regs.end[0])
            offset++;
          else
            {
              char tmp;
              offset = macro_sequence_regs.end[0];
              tmp = defn[offset];
              defn[offset] = '\0';
              M4ERROR ((warning_status, 0,
                        "Warning: definition of `%s' contains sequence `%s'",
                        name, defn + macro_sequence_regs.start[0]));
              defn[offset] = tmp;
            }
        }
      if (offset == -2)
        M4ERROR ((warning_status, 0,
                  "error checking --warn-macro-sequence for macro `%s'",
                  name));
    }
}

/*-----------------------------------------------.
| Initialize all builtin and predefined macros.  |
`-----------------------------------------------*/

void
builtin_init (void)
{
  const builtin *bp;
  const predefined *pp;
  char *string;

  for (bp = &builtin_tab[0]; bp->name != NULL; bp++)
    if (!no_gnu_extensions || !bp->gnu_extension)
      {
        if (prefix_all_builtins)
          {
            string = (char *) xmalloc (strlen (bp->name) + 4);
            strcpy (string, "m4_");
            strcat (string, bp->name);
            define_builtin (string, bp, SYMBOL_INSERT);
            free (string);
          }
        else
          define_builtin (bp->name, bp, SYMBOL_INSERT);
      }

  for (pp = &predefined_tab[0]; pp->func != NULL; pp++)
    if (no_gnu_extensions)
      {
        if (pp->unix_name != NULL)
          define_user_macro (pp->unix_name, pp->func, SYMBOL_INSERT);
      }
    else
      {
        if (pp->gnu_name != NULL)
          define_user_macro (pp->gnu_name, pp->func, SYMBOL_INSERT);
      }
}

/*-------------------------------------------------------------------.
| Give friendly warnings if a builtin macro is passed an             |
| inappropriate number of arguments.  NAME is the macro name for     |
| messages, ARGC is actual number of arguments, MIN is the minimum   |
| number of acceptable arguments, negative if not applicable, MAX is |
| the maximum number, negative if not applicable.                    |
`-------------------------------------------------------------------*/

static bool
bad_argc (token_data *name, int argc, int min, int max)
{
  bool isbad = false;

  if (min > 0 && argc < min)
    {
      if (!suppress_warnings)
        M4ERROR ((warning_status, 0,
                  "Warning: too few arguments to builtin `%s'",
                  TOKEN_DATA_TEXT (name)));
      isbad = true;
    }
  else if (max > 0 && argc > max && !suppress_warnings)
    M4ERROR ((warning_status, 0,
              "Warning: excess arguments to builtin `%s' ignored",
              TOKEN_DATA_TEXT (name)));

  return isbad;
}

/*-----------------------------------------------------------------.
| The function numeric_arg () converts ARG to an int pointed to by |
| VALUEP.  If the conversion fails, print error message for macro  |
| MACRO.  Return true iff conversion succeeds.                     |
`-----------------------------------------------------------------*/

static bool
numeric_arg (token_data *macro, const char *arg, int *valuep)
{
  char *endp;

  if (*arg == '\0')
    {
      *valuep = 0;
      M4ERROR ((warning_status, 0,
                "empty string treated as 0 in builtin `%s'",
                TOKEN_DATA_TEXT (macro)));
    }
  else
    {
      errno = 0;
      *valuep = strtol (arg, &endp, 10);
      if (*endp != '\0')
        {
          M4ERROR ((warning_status, 0,
                    "non-numeric argument to builtin `%s'",
                    TOKEN_DATA_TEXT (macro)));
          return false;
        }
      if (isspace (to_uchar (*arg)))
        M4ERROR ((warning_status, 0,
                  "leading whitespace ignored in builtin `%s'",
                  TOKEN_DATA_TEXT (macro)));
      else if (errno == ERANGE)
        M4ERROR ((warning_status, 0,
                  "numeric overflow detected in builtin `%s'",
                  TOKEN_DATA_TEXT (macro)));
    }
  return true;
}

/*------------------------------------------------------.
| The function ntoa () converts VALUE to a signed ASCII |
| representation in radix RADIX.                        |
`------------------------------------------------------*/

/* Digits for number to ASCII conversions.  */
static char const digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";

const char *
ntoa (int32_t value, int radix)
{
  bool negative;
  uint32_t uvalue;
  static char str[256];
  char *s = &str[sizeof str];

  *--s = '\0';

  if (value < 0)
    {
      negative = true;
      uvalue = -(uint32_t) value;
    }
  else
    {
      negative = false;
      uvalue = (uint32_t) value;
    }

  do
    {
      *--s = digits[uvalue % radix];
      uvalue /= radix;
    }
  while (uvalue > 0);

  if (negative)
    *--s = '-';
  return s;
}

/*---------------------------------------------------------------.
| Format an int VAL, and stuff it into an obstack OBS.  Used for |
| macros expanding to numbers.                                   |
`---------------------------------------------------------------*/

static void
shipout_int (struct obstack *obs, int val)
{
  const char *s;

  s = ntoa ((int32_t) val, 10);
  obstack_grow (obs, s, strlen (s));
}

/*-------------------------------------------------------------------.
| Print ARGC arguments from the table ARGV to obstack OBS, separated |
| by SEP, and quoted by the current quotes if QUOTED is true.        |
`-------------------------------------------------------------------*/

static void
dump_args (struct obstack *obs, int argc, token_data **argv,
           const char *sep, bool quoted)
{
  int i;
  size_t len = strlen (sep);

  for (i = 1; i < argc; i++)
    {
      if (i > 1)
        obstack_grow (obs, sep, len);
      if (quoted)
        obstack_grow (obs, lquote.string, lquote.length);
      obstack_grow (obs, TOKEN_DATA_TEXT (argv[i]),
                    strlen (TOKEN_DATA_TEXT (argv[i])));
      if (quoted)
        obstack_grow (obs, rquote.string, rquote.length);
    }
}

/* The rest of this file is code for builtins and expansion of user
   defined macros.  All the functions for builtins have a prototype as:

        void m4_MACRONAME (struct obstack *obs, int argc, char *argv[]);

   The function are expected to leave their expansion on the obstack OBS,
   as an unfinished object.  ARGV is a table of ARGC pointers to the
   individual arguments to the macro.  Please note that in general
   argv[argc] != NULL.  */

/* The first section are macros for definining, undefining, examining,
   changing, ... other macros.  */

/*-------------------------------------------------------------------.
| The function define_macro is common for the builtins "define",     |
| "undefine", "pushdef" and "popdef".  ARGC and ARGV is as for the   |
| caller, and MODE argument determines how the macro name is entered |
| into the symbol table.                                             |
`-------------------------------------------------------------------*/

static void
define_macro (int argc, token_data **argv, symbol_lookup mode)
{
  const builtin *bp;

  if (bad_argc (argv[0], argc, 2, 3))
    return;

  if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
    {
      M4ERROR ((warning_status, 0,
                "Warning: %s: invalid macro name ignored", ARG (0)));
      return;
    }

  if (argc == 2)
    {
      define_user_macro (ARG (1), "", mode);
      return;
    }

  switch (TOKEN_DATA_TYPE (argv[2]))
    {
    case TOKEN_TEXT:
      define_user_macro (ARG (1), ARG (2), mode);
      break;

    case TOKEN_FUNC:
      bp = find_builtin_by_addr (TOKEN_DATA_FUNC (argv[2]));
      if (bp == NULL)
        return;
      else
        define_builtin (ARG (1), bp, mode);
      break;

    case TOKEN_VOID:
    default:
      M4ERROR ((warning_status, 0,
                "INTERNAL ERROR: bad token data type in define_macro ()"));
      abort ();
    }
}

static void
m4_define (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  define_macro (argc, argv, SYMBOL_INSERT);
}

static void
m4_undefine (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  int i;
  if (bad_argc (argv[0], argc, 2, -1))
    return;
  for (i = 1; i < argc; i++)
    lookup_symbol (ARG (i), SYMBOL_DELETE);
}

static void
m4_pushdef (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  define_macro (argc, argv,  SYMBOL_PUSHDEF);
}

static void
m4_popdef (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  int i;
  if (bad_argc (argv[0], argc, 2, -1))
    return;
  for (i = 1; i < argc; i++)
    lookup_symbol (ARG (i), SYMBOL_POPDEF);
}

/*---------------------.
| Conditionals of m4.  |
`---------------------*/

static void
m4_ifdef (struct obstack *obs, int argc, token_data **argv)
{
  symbol *s;
  const char *result;

  if (bad_argc (argv[0], argc, 3, 4))
    return;
  s = lookup_symbol (ARG (1), SYMBOL_LOOKUP);

  if (s != NULL && SYMBOL_TYPE (s) != TOKEN_VOID)
    result = ARG (2);
  else if (argc >= 4)
    result = ARG (3);
  else
    result = NULL;

  if (result != NULL)
    obstack_grow (obs, result, strlen (result));
}

static void
m4_ifelse (struct obstack *obs, int argc, token_data **argv)
{
  const char *result;
  token_data *me = argv[0];

  if (argc == 2)
    return;

  if (bad_argc (me, argc, 4, -1))
    return;
  else
    /* Diagnose excess arguments if 5, 8, 11, etc., actual arguments.  */
    bad_argc (me, (argc + 2) % 3, -1, 1);

  argv++;
  argc--;

  result = NULL;
  while (result == NULL)

    if (STREQ (ARG (0), ARG (1)))
      result = ARG (2);

    else
      switch (argc)
        {
        case 3:
          return;

        case 4:
        case 5:
          result = ARG (3);
          break;

        default:
          argc -= 3;
          argv += 3;
        }

  obstack_grow (obs, result, strlen (result));
}

/*-------------------------------------------------------------------.
| The function dump_symbol () is for use by "dumpdef".  It builds up |
| a table of all defined, un-shadowed, symbols.                      |
`-------------------------------------------------------------------*/

/* The structure dump_symbol_data is used to pass the information needed
   from call to call to dump_symbol.  */

struct dump_symbol_data
{
  struct obstack *obs;          /* obstack for table */
  symbol **base;                /* base of table */
  int size;                     /* size of table */
};

static void
dump_symbol (symbol *sym, void *arg)
{
  struct dump_symbol_data *data = (struct dump_symbol_data *) arg;
  if (!SYMBOL_SHADOWED (sym) && SYMBOL_TYPE (sym) != TOKEN_VOID)
    {
      obstack_blank (data->obs, sizeof (symbol *));
      data->base = (symbol **) obstack_base (data->obs);
      data->base[data->size++] = sym;
    }
}

/*------------------------------------------------------------------------.
| qsort comparison routine, for sorting the table made in m4_dumpdef ().  |
`------------------------------------------------------------------------*/

static int
dumpdef_cmp (const void *s1, const void *s2)
{
  return strcmp (SYMBOL_NAME (* (symbol *const *) s1),
                 SYMBOL_NAME (* (symbol *const *) s2));
}

/*-------------------------------------------------------------.
| Implementation of "dumpdef" itself.  It builds up a table of |
| pointers to symbols, sorts it and prints the sorted table.   |
`-------------------------------------------------------------*/

static void
m4_dumpdef (struct obstack *obs, int argc, token_data **argv)
{
  symbol *s;
  int i;
  struct dump_symbol_data data;
  const builtin *bp;

  data.obs = obs;
  data.base = (symbol **) obstack_base (obs);
  data.size = 0;

  if (argc == 1)
    {
      hack_all_symbols (dump_symbol, &data);
    }
  else
    {
      for (i = 1; i < argc; i++)
        {
          s = lookup_symbol (TOKEN_DATA_TEXT (argv[i]), SYMBOL_LOOKUP);
          if (s != NULL && SYMBOL_TYPE (s) != TOKEN_VOID)
            dump_symbol (s, &data);
          else
            M4ERROR ((warning_status, 0,
                      "undefined macro `%s'", TOKEN_DATA_TEXT (argv[i])));
        }
    }

  /* Make table of symbols invisible to expand_macro ().  */

  obstack_finish (obs);

  qsort (data.base, data.size, sizeof (symbol *), dumpdef_cmp);

  for (; data.size > 0; --data.size, data.base++)
    {
      DEBUG_PRINT1 ("%s:\t", SYMBOL_NAME (data.base[0]));

      switch (SYMBOL_TYPE (data.base[0]))
        {
        case TOKEN_TEXT:
          if (debug_level & DEBUG_TRACE_QUOTE)
            DEBUG_PRINT3 ("%s%s%s\n",
                          lquote.string, SYMBOL_TEXT (data.base[0]), rquote.string);
          else
            DEBUG_PRINT1 ("%s\n", SYMBOL_TEXT (data.base[0]));
          break;

        case TOKEN_FUNC:
          bp = find_builtin_by_addr (SYMBOL_FUNC (data.base[0]));
          if (bp == NULL)
            {
              M4ERROR ((warning_status, 0, "\
INTERNAL ERROR: builtin not found in builtin table"));
              abort ();
            }
          DEBUG_PRINT1 ("<%s>\n", bp->name);
          break;

        case TOKEN_VOID:
        default:
          M4ERROR ((warning_status, 0,
                    "INTERNAL ERROR: bad token data type in m4_dumpdef ()"));
          abort ();
          break;
        }
    }
}

/*-----------------------------------------------------------------.
| The builtin "builtin" allows calls to builtin macros, even if    |
| their definition has been overridden or shadowed.  It is thus    |
| possible to redefine builtins, and still access their original   |
| definition.  This macro is not available in compatibility mode.  |
`-----------------------------------------------------------------*/

static void
m4_builtin (struct obstack *obs, int argc, token_data **argv)
{
  const builtin *bp;
  const char *name;

  if (bad_argc (argv[0], argc, 2, -1))
    return;
  if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
    {
      M4ERROR ((warning_status, 0,
                "Warning: %s: invalid macro name ignored", ARG (0)));
      return;
    }

  name = ARG (1);
  bp = find_builtin_by_name (name);
  if (bp->func == m4_placeholder)
    M4ERROR ((warning_status, 0,
              "undefined builtin `%s'", name));
  else
    {
      int i;
      if (! bp->groks_macro_args)
        for (i = 2; i < argc; i++)
          if (TOKEN_DATA_TYPE (argv[i]) != TOKEN_TEXT)
            {
              TOKEN_DATA_TYPE (argv[i]) = TOKEN_TEXT;
              TOKEN_DATA_TEXT (argv[i]) = (char *) "";
            }
      bp->func (obs, argc - 1, argv + 1);
    }
}

/*-------------------------------------------------------------------.
| The builtin "indir" allows indirect calls to macros, even if their |
| name is not a proper macro name.  It is thus possible to define    |
| macros with ill-formed names for internal use in larger macro      |
| packages.  This macro is not available in compatibility mode.      |
`-------------------------------------------------------------------*/

static void
m4_indir (struct obstack *obs, int argc, token_data **argv)
{
  symbol *s;
  const char *name;

  if (bad_argc (argv[0], argc, 2, -1))
    return;
  if (TOKEN_DATA_TYPE (argv[1]) != TOKEN_TEXT)
    {
      M4ERROR ((warning_status, 0,
                "Warning: %s: invalid macro name ignored", ARG (0)));
      return;
    }

  name = ARG (1);
  s = lookup_symbol (name, SYMBOL_LOOKUP);
  if (s == NULL || SYMBOL_TYPE (s) == TOKEN_VOID)
    M4ERROR ((warning_status, 0,
              "undefined macro `%s'", name));
  else
    {
      int i;
      if (! SYMBOL_MACRO_ARGS (s))
        for (i = 2; i < argc; i++)
          if (TOKEN_DATA_TYPE (argv[i]) != TOKEN_TEXT)
            {
              TOKEN_DATA_TYPE (argv[i]) = TOKEN_TEXT;
              TOKEN_DATA_TEXT (argv[i]) = (char *) "";
            }
      call_macro (s, argc - 1, argv + 1, obs);
    }
}

/*------------------------------------------------------------------.
| The macro "defn" returns the quoted definition of the macro named |
| by the first argument.  If the macro is builtin, it will push a   |
| special macro-definition token on the input stack.                |
`------------------------------------------------------------------*/

static void
m4_defn (struct obstack *obs, int argc, token_data **argv)
{
  symbol *s;
  builtin_func *b;
  unsigned int i;

  if (bad_argc (argv[0], argc, 2, -1))
    return;

  assert (0 < argc);
  for (i = 1; i < (unsigned) argc; i++)
    {
      const char *arg = ARG((int) i);
      s = lookup_symbol (arg, SYMBOL_LOOKUP);
      if (s == NULL)
        continue;

      switch (SYMBOL_TYPE (s))
        {
        case TOKEN_TEXT:
          obstack_grow (obs, lquote.string, lquote.length);
          obstack_grow (obs, SYMBOL_TEXT (s), strlen (SYMBOL_TEXT (s)));
          obstack_grow (obs, rquote.string, rquote.length);
          break;

        case TOKEN_FUNC:
          b = SYMBOL_FUNC (s);
          if (b == m4_placeholder)
            M4ERROR ((warning_status, 0, "\
builtin `%s' requested by frozen file is not supported", arg));
          else if (argc != 2)
            M4ERROR ((warning_status, 0,
                      "Warning: cannot concatenate builtin `%s'",
                      arg));
          else
            push_macro (b);
          break;

        case TOKEN_VOID:
          /* Nothing to do for traced but undefined macro.  */
          break;

        default:
          M4ERROR ((warning_status, 0,
                    "INTERNAL ERROR: bad symbol type in m4_defn ()"));
          abort ();
        }
    }
}

/*--------------------------------------------------------------.
| This section contains macros to handle the builtins "syscmd", |
| "esyscmd" and "sysval".  "esyscmd" is GNU specific.           |
`--------------------------------------------------------------*/

/* Exit code from last "syscmd" command.  */
static int sysval;

static void
m4_syscmd (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  const char *cmd = ARG (1);
  int status;
  int sig_status;
  const char *prog_args[4] = { "sh", "-c" };
  if (bad_argc (argv[0], argc, 2, 2) || !*cmd)
    {
      /* The empty command is successful.  */
      sysval = 0;
      return;
    }

  debug_flush_files ();
#if W32_NATIVE
  if (strstr (SYSCMD_SHELL, "cmd"))
    {
      prog_args[0] = "cmd";
      prog_args[1] = "/c";
    }
#endif
  prog_args[2] = cmd;
  errno = 0;
  status = execute (ARG (0), SYSCMD_SHELL, (char **) prog_args, false,
                    false, false, false, true, false, &sig_status);
  if (sig_status)
    {
      assert (status == 127);
      sysval = sig_status << 8;
    }
  else
    {
      if (status == 127 && errno)
        M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
      sysval = status;
    }
}

static void
m4_esyscmd (struct obstack *obs, int argc, token_data **argv)
{
  const char *cmd = ARG (1);
  const char *prog_args[4] = { "sh", "-c" };
  pid_t child;
  int fd;
  FILE *pin;
  int status;
  int sig_status;

  if (bad_argc (argv[0], argc, 2, 2) || !*cmd)
    {
      /* The empty command is successful.  */
      sysval = 0;
      return;
    }

  debug_flush_files ();
#if W32_NATIVE
  if (strstr (SYSCMD_SHELL, "cmd"))
    {
      prog_args[0] = "cmd";
      prog_args[1] = "/c";
    }
#endif
  prog_args[2] = cmd;
  errno = 0;
  child = create_pipe_in (ARG (0), SYSCMD_SHELL, (char **) prog_args,
                          NULL, false, true, false, &fd);
  if (child == -1)
    {
      M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
      sysval = 127;
      return;
    }
#if OS2
  /* On OS/2 kLIBC, fdopen() creates a stream in a mode of a file descriptor.
     So include "t" to open a stream in a text mode explicitly on OS/2. */
  pin = fdopen (fd, "rt");
#else
  pin = fdopen (fd, "r");
#endif
  if (pin == NULL)
    {
      M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
      sysval = 127;
      close (fd);
      return;
    }
  while (1)
    {
      size_t avail = obstack_room (obs);
      size_t len;
      if (!avail)
        {
          int ch = getc (pin);
          if (ch == EOF)
            break;
          obstack_1grow (obs, ch);
          continue;
        }
      len = fread (obstack_next_free (obs), 1, avail, pin);
      if (len <= 0)
        break;
      obstack_blank_fast (obs, len);
    }
  if (ferror (pin) || fclose (pin))
    M4ERROR ((EXIT_FAILURE, errno, "cannot read pipe"));
  errno = 0;
  status = wait_subprocess (child, ARG (0), false, true, true, false,
                            &sig_status);
  if (sig_status)
    {
      assert (status == 127);
      sysval = sig_status << 8;
    }
  else
    {
      if (status == 127 && errno)
        M4ERROR ((warning_status, errno, "cannot run command `%s'", cmd));
      sysval = status;
    }
}

static void
m4_sysval (struct obstack *obs, int argc M4_GNUC_UNUSED,
           token_data **argv M4_GNUC_UNUSED)
{
  shipout_int (obs, sysval);
}

/*------------------------------------------------------------------.
| This section contains the top level code for the "eval" builtin.  |
| The actual work is done in the function evaluate (), which lives  |
| in eval.c.                                                        |
`------------------------------------------------------------------*/

static void
m4_eval (struct obstack *obs, int argc, token_data **argv)
{
  int32_t value = 0;
  int radix = 10;
  int min = 1;
  const char *s;

  if (bad_argc (argv[0], argc, 2, 4))
    return;

  if (*ARG (2) && !numeric_arg (argv[0], ARG (2), &radix))
    return;

  if (radix < 1 || radix > (int) strlen (digits))
    {
      M4ERROR ((warning_status, 0,
                "radix %d in builtin `%s' out of range",
                radix, ARG (0)));
      return;
    }

  if (argc >= 4 && !numeric_arg (argv[0], ARG (3), &min))
    return;
  if (min < 0)
    {
      M4ERROR ((warning_status, 0,
                "negative width to builtin `%s'", ARG (0)));
      return;
    }

  if (!*ARG (1))
    M4ERROR ((warning_status, 0,
              "empty string treated as 0 in builtin `%s'", ARG (0)));
  else if (evaluate (ARG (1), &value))
    return;

  if (radix == 1)
    {
      if (value < 0)
        {
          obstack_1grow (obs, '-');
          value = -value;
        }
      /* This assumes 2's-complement for correctly handling INT_MIN.  */
      while (min-- - value > 0)
        obstack_1grow (obs, '0');
      while (value-- != 0)
        obstack_1grow (obs, '1');
      obstack_1grow (obs, '\0');
      return;
    }

  s = ntoa (value, radix);

  if (*s == '-')
    {
      obstack_1grow (obs, '-');
      s++;
    }
  for (min -= strlen (s); --min >= 0;)
    obstack_1grow (obs, '0');

  obstack_grow (obs, s, strlen (s));
}

static void
m4_incr (struct obstack *obs, int argc, token_data **argv)
{
  int value;

  if (bad_argc (argv[0], argc, 2, 2))
    return;

  if (!numeric_arg (argv[0], ARG (1), &value))
    return;

  shipout_int (obs, value + 1);
}

static void
m4_decr (struct obstack *obs, int argc, token_data **argv)
{
  int value;

  if (bad_argc (argv[0], argc, 2, 2))
    return;

  if (!numeric_arg (argv[0], ARG (1), &value))
    return;

  shipout_int (obs, value - 1);
}

/* This section contains the macros "divert", "undivert" and "divnum" for
   handling diversion.  The utility functions used lives in output.c.  */

/*-----------------------------------------------------------------.
| Divert further output to the diversion given by ARGV[1].  Out of |
| range means discard further output.                              |
`-----------------------------------------------------------------*/

static void
m4_divert (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  int i = 0;

  if (bad_argc (argv[0], argc, 1, 2))
    return;

  if (argc >= 2 && !numeric_arg (argv[0], ARG (1), &i))
    return;

  make_diversion (i);
}

/*-----------------------------------------------------.
| Expand to the current diversion number, -1 if none.  |
`-----------------------------------------------------*/

static void
m4_divnum (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 1, 1))
    return;
  shipout_int (obs, current_diversion);
}

/*------------------------------------------------------------------.
| Bring back the diversion given by the argument list.  If none is  |
| specified, bring back all diversions.  GNU specific is the option |
| of undiverting named files, by passing a non-numeric argument to  |
| undivert ().                                                      |
`------------------------------------------------------------------*/

static void
m4_undivert (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  int i, file;
  FILE *fp;
  char *endp;

  if (argc == 1)
    undivert_all ();
  else
    for (i = 1; i < argc; i++)
      {
        file = strtol (ARG (i), &endp, 10);
        if (*endp == '\0' && !isspace (to_uchar (*ARG (i))))
          insert_diversion (file);
        else if (no_gnu_extensions)
          M4ERROR ((warning_status, 0,
                    "non-numeric argument to builtin `%s'", ARG (0)));
        else
          {
            fp = m4_path_search (ARG (i), NULL);
            if (fp != NULL)
              {
                insert_file (fp);
                if (fclose (fp) == EOF)
                  M4ERROR ((warning_status, errno,
                            "error undiverting `%s'", ARG (i)));
              }
            else
              M4ERROR ((warning_status, errno,
                        "cannot undivert `%s'", ARG (i)));
          }
      }
}

/* This section contains various macros, which does not fall into any
   specific group.  These are "dnl", "shift", "changequote", "changecom"
   and "changeword".  */

/*-----------------------------------------------------------.
| Delete all subsequent whitespace from input.  The function |
| skip_line () lives in input.c.                             |
`-----------------------------------------------------------*/

static void
m4_dnl (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 1, 1))
    return;

  skip_line ();
}

/*--------------------------------------------------------------------.
| Shift all arguments one to the left, discarding the first           |
| argument.  Each output argument is quoted with the current quotes.  |
`--------------------------------------------------------------------*/

static void
m4_shift (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 2, -1))
    return;
  dump_args (obs, argc - 1, argv + 1, ",", true);
}

/*--------------------------------------------------------------------------.
| Change the current quotes.  The function set_quotes () lives in input.c.  |
`--------------------------------------------------------------------------*/

static void
m4_changequote (struct obstack *obs M4_GNUC_UNUSED, int argc,
                token_data **argv)
{
  if (bad_argc (argv[0], argc, 1, 3))
    return;

  /* Explicit NULL distinguishes between empty and missing argument.  */
  set_quotes ((argc >= 2) ? TOKEN_DATA_TEXT (argv[1]) : NULL,
             (argc >= 3) ? TOKEN_DATA_TEXT (argv[2]) : NULL);
}

/*-----------------------------------------------------------------.
| Change the current comment delimiters.  The function set_comment |
| () lives in input.c.                                             |
`-----------------------------------------------------------------*/

static void
m4_changecom (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 1, 3))
    return;

  /* Explicit NULL distinguishes between empty and missing argument.  */
  set_comment ((argc >= 2) ? TOKEN_DATA_TEXT (argv[1]) : NULL,
               (argc >= 3) ? TOKEN_DATA_TEXT (argv[2]) : NULL);
}

#ifdef ENABLE_CHANGEWORD

/*---------------------------------------------------------------.
| Change the regular expression used for breaking the input into |
| words.  The function set_word_regexp () lives in input.c.      |
`---------------------------------------------------------------*/

static void
m4_changeword (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 2, 2))
    return;

  set_word_regexp (TOKEN_DATA_TEXT (argv[1]));
}

#endif /* ENABLE_CHANGEWORD */

/* This section contains macros for inclusion of other files -- "include"
   and "sinclude".  This differs from bringing back diversions, in that
   the input is scanned before being copied to the output.  */

/*---------------------------------------------------------------.
| Generic include function.  Include the file given by the first |
| argument, if it exists.  Complain about inaccessible files iff |
| SILENT is false.                                               |
`---------------------------------------------------------------*/

static void
include (int argc, token_data **argv, bool silent)
{
  FILE *fp;
  char *name;

  if (bad_argc (argv[0], argc, 2, 2))
    return;

  fp = m4_path_search (ARG (1), &name);
  if (fp == NULL)
    {
      if (!silent)
        {
          M4ERROR ((warning_status, errno, "cannot open `%s'", ARG (1)));
          retcode = EXIT_FAILURE;
        }
      return;
    }

  push_file (fp, name, true);
  free (name);
}

/*------------------------------------------------.
| Include a file, complaining in case of errors.  |
`------------------------------------------------*/

static void
m4_include (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  include (argc, argv, false);
}

/*----------------------------------.
| Include a file, ignoring errors.  |
`----------------------------------*/

static void
m4_sinclude (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  include (argc, argv, true);
}

/* More miscellaneous builtins -- "maketemp", "errprint", "__file__",
   "__line__", and "__program__".  The last three are GNU specific.  */

/*------------------------------------------------------------------.
| Use the first argument as at template for a temporary file name.  |
`------------------------------------------------------------------*/

/* Add trailing 'X' to PATTERN of length LEN as necessary, then
   securely create the file, and place the quoted new file name on
   OBS.  Report errors on behalf of ME.  */
static void
mkstemp_helper (struct obstack *obs, const char *me, const char *pattern,
                size_t len)
{
  int fd;
  size_t i;
  char *name;

  /* Guarantee that there are six trailing 'X' characters, even if the
     user forgot to supply them.  Output must be quoted if
     successful.  */
  obstack_grow (obs, lquote.string, lquote.length);
  obstack_grow (obs, pattern, len);
  for (i = 0; len > 0 && i < 6; i++)
    if (pattern[len - i - 1] != 'X')
      break;
  obstack_grow0 (obs, "XXXXXX", 6 - i);
  name = (char *) obstack_base (obs) + lquote.length;

  errno = 0;
  fd = mkstemp (name);
  if (fd < 0)
    {
      M4ERROR ((0, errno, "%s: cannot create tempfile `%s'", me, pattern));
      obstack_free (obs, obstack_finish (obs));
    }
  else
    {
      close (fd);
      /* Remove NUL, then finish quote.  */
      obstack_blank_fast (obs, -1);
      obstack_grow (obs, rquote.string, rquote.length);
    }
}

static void
m4_maketemp (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 2, 2))
    return;
  if (no_gnu_extensions)
    {
      /* POSIX states "any trailing 'X' characters [are] replaced with
         the current process ID as a string", without referencing the
         file system.  Horribly insecure, but we have to do it when we
         are in traditional mode.

         For reference, Solaris m4 does:
           maketemp() -> `'
           maketemp(X) -> `X'
           maketemp(XX) -> `Xn', where n is last digit of pid
           maketemp(XXXXXXXX) -> `X00nnnnn', where nnnnn is 16-bit pid
      */
      const char *str = ARG (1);
      int len = strlen (str);
      int i;
      int len2;

      M4ERROR ((warning_status, 0, "recommend using mkstemp instead"));
      for (i = len; i > 1; i--)
        if (str[i - 1] != 'X')
          break;
      obstack_grow (obs, str, i);
      str = ntoa ((int32_t) getpid (), 10);
      len2 = strlen (str);
      if (len2 > len - i)
        obstack_grow0 (obs, str + len2 - (len - i), len - i);
      else
        {
          while (i++ < len - len2)
            obstack_1grow (obs, '0');
          obstack_grow0 (obs, str, len2);
        }
    }
  else
    mkstemp_helper (obs, ARG (0), ARG (1), strlen (ARG (1)));
}

static void
m4_mkstemp (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 2, 2))
    return;
  mkstemp_helper (obs, ARG (0), ARG (1), strlen (ARG (1)));
}

/*----------------------------------------.
| Print all arguments on standard error.  |
`----------------------------------------*/

static void
m4_errprint (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 2, -1))
    return;
  dump_args (obs, argc, argv, " ", false);
  obstack_1grow (obs, '\0');
  debug_flush_files ();
  xfprintf (stderr, "%s", (char *) obstack_finish (obs));
  fflush (stderr);
}

static void
m4___file__ (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 1, 1))
    return;
  obstack_grow (obs, lquote.string, lquote.length);
  obstack_grow (obs, current_file, strlen (current_file));
  obstack_grow (obs, rquote.string, rquote.length);
}

static void
m4___line__ (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 1, 1))
    return;
  shipout_int (obs, current_line);
}

static void
m4___program__ (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 1, 1))
    return;
  obstack_grow (obs, lquote.string, lquote.length);
  obstack_grow (obs, program_name, strlen (program_name));
  obstack_grow (obs, rquote.string, rquote.length);
}

/* This section contains various macros for exiting, saving input until
   EOF is seen, and tracing macro calls.  That is: "m4exit", "m4wrap",
   "traceon" and "traceoff".  */

/*----------------------------------------------------------.
| Exit immediately, with exit status specified by the first |
| argument, or 0 if no arguments are present.               |
`----------------------------------------------------------*/

static void M4_GNUC_NORETURN
m4_m4exit (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  int exit_code = EXIT_SUCCESS;

  /* Warn on bad arguments, but still exit.  */
  bad_argc (argv[0], argc, 1, 2);
  if (argc >= 2 && !numeric_arg (argv[0], ARG (1), &exit_code))
    exit_code = EXIT_FAILURE;
  if (exit_code < 0 || exit_code > 255)
    {
      M4ERROR ((warning_status, 0,
                "exit status out of range: `%d'", exit_code));
      exit_code = EXIT_FAILURE;
    }
  /* Change debug stream back to stderr, to force flushing debug stream and
     detect any errors it might have encountered.  */
  debug_set_output (NULL);
  debug_flush_files ();
  if (exit_code == EXIT_SUCCESS && retcode != EXIT_SUCCESS)
    exit_code = retcode;
  /* Propagate non-zero status to atexit handlers.  */
  if (exit_code != EXIT_SUCCESS)
    exit_failure = exit_code;
  exit (exit_code);
}

/*------------------------------------------------------------------.
| Save the argument text until EOF has been seen, allowing for user |
| specified cleanup action.  GNU version saves all arguments, the   |
| standard version only the first.                                  |
`------------------------------------------------------------------*/

static void
m4_m4wrap (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 2, -1))
    return;
  if (no_gnu_extensions)
    obstack_grow (obs, ARG (1), strlen (ARG (1)));
  else
    dump_args (obs, argc, argv, " ", false);
  obstack_1grow (obs, '\0');
  push_wrapup ((char *) obstack_finish (obs));
}

/* Enable tracing of all specified macros, or all, if none is specified.
   Tracing is disabled by default, when a macro is defined.  This can be
   overridden by the "t" debug flag.  */

/*------------------------------------------------------------------.
| Set_trace () is used by "traceon" and "traceoff" to enable and    |
| disable tracing of a macro.  It disables tracing if DATA is NULL, |
| otherwise it enables tracing.                                     |
`------------------------------------------------------------------*/

static void
set_trace (symbol *sym, void *data)
{
  SYMBOL_TRACED (sym) = data != NULL;
  /* Remove placeholder from table if macro is undefined and untraced.  */
  if (SYMBOL_TYPE (sym) == TOKEN_VOID && data == NULL)
    lookup_symbol (SYMBOL_NAME (sym), SYMBOL_POPDEF);
}

static void
m4_traceon (struct obstack *obs, int argc, token_data **argv)
{
  symbol *s;
  int i;

  if (argc == 1)
    hack_all_symbols (set_trace, obs);
  else
    for (i = 1; i < argc; i++)
      {
        s = lookup_symbol (ARG (i), SYMBOL_LOOKUP);
        if (!s)
          s = lookup_symbol (ARG (i), SYMBOL_INSERT);
        set_trace (s, obs);
      }
}

/*------------------------------------------------------------------------.
| Disable tracing of all specified macros, or all, if none is specified.  |
`------------------------------------------------------------------------*/

static void
m4_traceoff (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  symbol *s;
  int i;

  if (argc == 1)
    hack_all_symbols (set_trace, NULL);
  else
    for (i = 1; i < argc; i++)
      {
        s = lookup_symbol (TOKEN_DATA_TEXT (argv[i]), SYMBOL_LOOKUP);
        if (s != NULL)
          set_trace (s, NULL);
      }
}

/*------------------------------------------------------------------.
| On-the-fly control of the format of the tracing output.  It takes |
| one argument, which is a character string like given to the -d    |
| option, or none in which case the debug_level is zeroed.          |
`------------------------------------------------------------------*/

static void
m4_debugmode (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  int new_debug_level;
  int change_flag;

  if (bad_argc (argv[0], argc, 1, 2))
    return;

  if (argc == 1)
    debug_level = 0;
  else
    {
      if (ARG (1)[0] == '+' || ARG (1)[0] == '-')
        {
          change_flag = ARG (1)[0];
          new_debug_level = debug_decode (ARG (1) + 1);
        }
      else
        {
          change_flag = 0;
          new_debug_level = debug_decode (ARG (1));
        }

      if (new_debug_level < 0)
        M4ERROR ((warning_status, 0,
                  "Debugmode: bad debug flags: `%s'", ARG (1)));
      else
        {
          switch (change_flag)
            {
            case 0:
              debug_level = new_debug_level;
              break;

            case '+':
              debug_level |= new_debug_level;
              break;

            case '-':
              debug_level &= ~new_debug_level;
              break;

            default:
              M4ERROR ((warning_status, 0,
                        "INTERNAL ERROR: bad flag in m4_debugmode ()"));
              abort ();
            }
        }
    }
}

/*-------------------------------------------------------------------------.
| Specify the destination of the debugging output.  With one argument, the |
| argument is taken as a file name, with no arguments, revert to stderr.   |
`-------------------------------------------------------------------------*/

static void
m4_debugfile (struct obstack *obs M4_GNUC_UNUSED, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 1, 2))
    return;

  if (argc == 1)
    debug_set_output (NULL);
  else if (!debug_set_output (ARG (1)))
    M4ERROR ((warning_status, errno,
              "cannot set debug file `%s'", ARG (1)));
}

/* This section contains text processing macros: "len", "index",
   "substr", "translit", "format", "regexp" and "patsubst".  The last
   three are GNU specific.  */

/*---------------------------------------------.
| Expand to the length of the first argument.  |
`---------------------------------------------*/

static void
m4_len (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 2, 2))
    return;
  shipout_int (obs, strlen (ARG (1)));
}

/*-------------------------------------------------------------------.
| The macro expands to the first index of the second argument in the |
| first argument.                                                    |
`-------------------------------------------------------------------*/

static void
m4_index (struct obstack *obs, int argc, token_data **argv)
{
  const char *haystack;
  const char *result;
  int retval;

  if (bad_argc (argv[0], argc, 3, 3))
    {
      /* builtin(`index') is blank, but index(`abc') is 0.  */
      if (argc == 2)
        shipout_int (obs, 0);
      return;
    }

  haystack = ARG (1);
  result = strstr (haystack, ARG (2));
  retval = result ? result - haystack : -1;

  shipout_int (obs, retval);
}

/*-----------------------------------------------------------------.
| The macro "substr" extracts substrings from the first argument,  |
| starting from the index given by the second argument, extending  |
| for a length given by the third argument.  If the third argument |
| is missing, the substring extends to the end of the first        |
| argument.                                                        |
`-----------------------------------------------------------------*/

static void
m4_substr (struct obstack *obs, int argc, token_data **argv)
{
  int start = 0;
  int length, avail;

  if (bad_argc (argv[0], argc, 3, 4))
    {
      /* builtin(`substr') is blank, but substr(`abc') is abc.  */
      if (argc == 2)
        obstack_grow (obs, ARG (1), strlen (ARG (1)));
      return;
    }

  length = avail = strlen (ARG (1));
  if (!numeric_arg (argv[0], ARG (2), &start))
    return;

  if (argc >= 4 && !numeric_arg (argv[0], ARG (3), &length))
    return;

  if (start < 0 || length <= 0 || start >= avail)
    return;

  if (start + length > avail)
    length = avail - start;
  obstack_grow (obs, ARG (1) + start, length);
}

/*------------------------------------------------------------------.
| For "translit", ranges are allowed in the second and third        |
| argument.  They are expanded in the following function, and the   |
| expanded strings, without any ranges left, are used to translate  |
| the characters of the first argument.  A single - (dash) can be   |
| included in the strings by being the first or the last character  |
| in the string.  If the first character in a range is after the    |
| first in the character set, the range is made backwards, thus 9-0 |
| is the string 9876543210.                                         |
`------------------------------------------------------------------*/

static const char *
expand_ranges (const char *s, struct obstack *obs)
{
  unsigned char from;
  unsigned char to;

  for (from = '\0'; *s != '\0'; from = to_uchar (*s++))
    {
      if (*s == '-' && from != '\0')
        {
          to = to_uchar (*++s);
          if (to == '\0')
            {
              /* trailing dash */
              obstack_1grow (obs, '-');
              break;
            }
          else if (from <= to)
            {
              while (from++ < to)
                obstack_1grow (obs, from);
            }
          else
            {
              while (--from >= to)
                obstack_1grow (obs, from);
            }
        }
      else
        obstack_1grow (obs, *s);
    }
  obstack_1grow (obs, '\0');
  return (char *) obstack_finish (obs);
}

/*-----------------------------------------------------------------.
| The macro "translit" translates all characters in the first      |
| argument, which are present in the second argument, into the     |
| corresponding character from the third argument.  If the third   |
| argument is shorter than the second, the extra characters in the |
| second argument are deleted from the first.                      |
`-----------------------------------------------------------------*/

static void
m4_translit (struct obstack *obs, int argc, token_data **argv)
{
  const char *data = ARG (1);
  const char *from = ARG (2);
  const char *to;
  char map[UCHAR_MAX + 1];
  char found[UCHAR_MAX + 1];
  unsigned char ch;

  if (bad_argc (argv[0], argc, 3, 4) || !*data || !*from)
    {
      /* builtin(`translit') is blank, but translit(`abc') is abc.  */
      if (2 <= argc)
        obstack_grow (obs, data, strlen (data));
      return;
    }

  to = ARG (3);
  if (strchr (to, '-') != NULL)
    {
      to = expand_ranges (to, obs);
      assert (to && *to);
    }

  /* If there are only one or two bytes to replace, it is faster to
     use memchr2.  Using expand_ranges does nothing unless there are
     at least three bytes.  */
  if (!from[1] || !from[2])
    {
      const char *p;
      size_t len = strlen (data);
      while ((p = (char *) memchr2 (data, from[0], from[1], len)))
        {
          obstack_grow (obs, data, p - data);
          len -= p - data;
          if (!len)
            return;
          data = p + 1;
          len--;
          if (*p == from[0] && to[0])
            obstack_1grow (obs, to[0]);
          else if (*p == from[1] && to[0] && to[1])
            obstack_1grow (obs, to[1]);
        }
      obstack_grow (obs, data, len);
      return;
    }

  if (strchr (from, '-') != NULL)
    {
      from = expand_ranges (from, obs);
      assert (from && *from);
    }

  /* Calling strchr(from) for each character in data is quadratic,
     since both strings can be arbitrarily long.  Instead, create a
     from-to mapping in one pass of from, then use that map in one
     pass of data, for linear behavior.  Traditional behavior is that
     only the first instance of a character in from is consulted,
     hence the found map.  */
  memset (map, 0, sizeof map);
  memset (found, 0, sizeof found);
  for ( ; (ch = *from) != '\0'; from++)
    {
      if (! found[ch])
        {
          found[ch] = 1;
          map[ch] = *to;
        }
      if (*to != '\0')
        to++;
    }

  for (data = ARG (1); (ch = *data) != '\0'; data++)
    {
      if (! found[ch])
        obstack_1grow (obs, ch);
      else if (map[ch])
        obstack_1grow (obs, map[ch]);
    }
}

/*-------------------------------------------------------------------.
| Frontend for printf like formatting.  The function format () lives |
| in the file format.c.                                              |
`-------------------------------------------------------------------*/

static void
m4_format (struct obstack *obs, int argc, token_data **argv)
{
  if (bad_argc (argv[0], argc, 2, -1))
    return;
  expand_format (obs, argc - 1, argv + 1);
}

/*------------------------------------------------------------------.
| Function to perform substitution by regular expressions.  Used by |
| the builtins regexp and patsubst.  The changed text is placed on  |
| the obstack.  The substitution is REPL, with \& substituted by    |
| this part of VICTIM matched by the last whole regular expression, |
| taken from REGS[0], and \N substituted by the text matched by the |
| Nth parenthesized sub-expression, taken from REGS[N].             |
`------------------------------------------------------------------*/

static int substitute_warned = 0;

static void
substitute (struct obstack *obs, const char *victim, const char *repl,
            struct re_registers *regs)
{
  int ch;
  __re_size_t ind;
  while (1)
    {
      const char *backslash = strchr (repl, '\\');
      if (!backslash)
        {
          obstack_grow (obs, repl, strlen (repl));
          return;
        }
      obstack_grow (obs, repl, backslash - repl);
      repl = backslash;
      ch = *++repl;
      switch (ch)
        {
        case '0':
          if (!substitute_warned)
            {
              M4ERROR ((warning_status, 0, "\
Warning: \\0 will disappear, use \\& instead in replacements"));
              substitute_warned = 1;
            }
          /* Fall through.  */

        case '&':
          obstack_grow (obs, victim + regs->start[0],
                        regs->end[0] - regs->start[0]);
          repl++;
          break;

        case '1': case '2': case '3': case '4': case '5': case '6':
        case '7': case '8': case '9':
          ind = ch -= '0';
          if (regs->num_regs - 1 <= ind)
            M4ERROR ((warning_status, 0,
                      "Warning: sub-expression %d not present", ch));
          else if (regs->end[ch] > 0)
            obstack_grow (obs, victim + regs->start[ch],
                          regs->end[ch] - regs->start[ch]);
          repl++;
          break;

        case '\0':
          M4ERROR ((warning_status, 0,
                    "Warning: trailing \\ ignored in replacement"));
          return;

        default:
          obstack_1grow (obs, ch);
          repl++;
          break;
        }
    }
}

/*------------------------------------------.
| Initialize regular expression variables.  |
`------------------------------------------*/

void
init_pattern_buffer (struct re_pattern_buffer *buf, struct re_registers *regs)
{
  buf->translate = NULL;
  buf->fastmap = NULL;
  buf->buffer = NULL;
  buf->allocated = 0;
  if (regs)
    {
      regs->start = NULL;
      regs->end = NULL;
    }
}

/*------------------------------------------------------------------.
| Regular expression version of index.  Given two arguments, expand |
| to the index of the first match of the second argument (a regexp) |
| in the first.  Expand to -1 if here is no match.  Given a third   |
| argument, it changes the expansion to this argument.              |
`------------------------------------------------------------------*/

static void
m4_regexp (struct obstack *obs, int argc, token_data **argv)
{
  const char *victim;           /* first argument */
  const char *regexp;           /* regular expression */
  const char *repl;             /* replacement string */

  struct re_pattern_buffer buf; /* compiled regular expression */
  struct re_registers regs;     /* for subexpression matches */
  const char *msg;              /* error message from re_compile_pattern */
  int startpos;                 /* start position of match */
  int length;                   /* length of first argument */

  if (bad_argc (argv[0], argc, 3, 4))
    {
      /* builtin(`regexp') is blank, but regexp(`abc') is 0.  */
      if (argc == 2)
        shipout_int (obs, 0);
      return;
    }

  victim = TOKEN_DATA_TEXT (argv[1]);
  regexp = TOKEN_DATA_TEXT (argv[2]);

  init_pattern_buffer (&buf, &regs);
  msg = re_compile_pattern (regexp, strlen (regexp), &buf);

  if (msg != NULL)
    {
      M4ERROR ((warning_status, 0,
                "bad regular expression: `%s': %s", regexp, msg));
      free_pattern_buffer (&buf, &regs);
      return;
    }

  length = strlen (victim);
  /* Avoid overhead of allocating regs if we won't use it.  */
  startpos = re_search (&buf, victim, length, 0, length,
                        argc == 3 ? NULL : &regs);

  if (startpos == -2)
    M4ERROR ((warning_status, 0,
               "error matching regular expression `%s'", regexp));
  else if (argc == 3)
    shipout_int (obs, startpos);
  else if (startpos >= 0)
    {
      repl = TOKEN_DATA_TEXT (argv[3]);
      substitute (obs, victim, repl, &regs);
    }

  free_pattern_buffer (&buf, &regs);
}

/*--------------------------------------------------------------------------.
| Substitute all matches of a regexp occuring in a string.  Each match of   |
| the second argument (a regexp) in the first argument is changed to the    |
| third argument, with \& substituted by the matched text, and \N           |
| substituted by the text matched by the Nth parenthesized sub-expression.  |
`--------------------------------------------------------------------------*/

static void
m4_patsubst (struct obstack *obs, int argc, token_data **argv)
{
  const char *victim;           /* first argument */
  const char *regexp;           /* regular expression */

  struct re_pattern_buffer buf; /* compiled regular expression */
  struct re_registers regs;     /* for subexpression matches */
  const char *msg;              /* error message from re_compile_pattern */
  int matchpos;                 /* start position of match */
  int offset;                   /* current match offset */
  int length;                   /* length of first argument */

  if (bad_argc (argv[0], argc, 3, 4))
    {
      /* builtin(`patsubst') is blank, but patsubst(`abc') is abc.  */
      if (argc == 2)
        obstack_grow (obs, ARG (1), strlen (ARG (1)));
      return;
    }

  regexp = TOKEN_DATA_TEXT (argv[2]);

  init_pattern_buffer (&buf, &regs);
  msg = re_compile_pattern (regexp, strlen (regexp), &buf);

  if (msg != NULL)
    {
      M4ERROR ((warning_status, 0,
                "bad regular expression `%s': %s", regexp, msg));
      free (buf.buffer);
      return;
    }

  victim = TOKEN_DATA_TEXT (argv[1]);
  length = strlen (victim);

  offset = 0;
  while (offset <= length)
    {
      matchpos = re_search (&buf, victim, length,
                            offset, length - offset, &regs);
      if (matchpos < 0)
        {

          /* Match failed -- either error or there is no match in the
             rest of the string, in which case the rest of the string is
             copied verbatim.  */

          if (matchpos == -2)
            M4ERROR ((warning_status, 0,
                      "error matching regular expression `%s'", regexp));
          else if (offset < length)
            obstack_grow (obs, victim + offset, length - offset);
          break;
        }

      /* Copy the part of the string that was skipped by re_search ().  */

      if (matchpos > offset)
        obstack_grow (obs, victim + offset, matchpos - offset);

      /* Handle the part of the string that was covered by the match.  */

      substitute (obs, victim, ARG (3), &regs);

      /* Update the offset to the end of the match.  If the regexp
         matched a null string, advance offset one more, to avoid
         infinite loops.  */

      offset = regs.end[0];
      if (regs.start[0] == regs.end[0])
        obstack_1grow (obs, victim[offset++]);
    }
  obstack_1grow (obs, '\0');

  free_pattern_buffer (&buf, &regs);
}

/* Finally, a placeholder builtin.  This builtin is not installed by
   default, but when reading back frozen files, this is associated
   with any builtin we don't recognize (for example, if the frozen
   file was created with a changeword capable m4, but is then loaded
   by a different m4 that does not support changeword).  This way, we
   can keep 'm4 -R' quiet in the common case that the user did not
   know or care about the builtin when the frozen file was created,
   while still flagging it as a potential error if an attempt is made
   to actually use the builtin.  */

/*--------------------------------------------------------------------.
| Issue a warning that this macro is a placeholder for an unsupported |
| builtin that was requested while reloading a frozen file.           |
`--------------------------------------------------------------------*/

void
m4_placeholder (struct obstack *obs M4_GNUC_UNUSED, int argc,
                token_data **argv)
{
  M4ERROR ((warning_status, 0, "\
builtin `%s' requested by frozen file is not supported", ARG (0)));
}

/*-------------------------------------------------------------------.
| This function handles all expansion of user defined and predefined |
| macros.  It is called with an obstack OBS, where the macros        |
| expansion will be placed, as an unfinished object.  SYM points to  |
| the macro definition, giving the expansion text.  ARGC and ARGV    |
| are the arguments, as usual.                                       |
`-------------------------------------------------------------------*/

void
expand_user_macro (struct obstack *obs, symbol *sym,
                   int argc, token_data **argv)
{
  const char *text = SYMBOL_TEXT (sym);
  int i;
  while (1)
    {
      const char *dollar = strchr (text, '$');
      if (!dollar)
        {
          obstack_grow (obs, text, strlen (text));
          return;
        }
      obstack_grow (obs, text, dollar - text);
      text = dollar;
      switch (*++text)
        {
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
          if (no_gnu_extensions)
            {
              i = *text++ - '0';
            }
          else
            {
              for (i = 0; isdigit (to_uchar (*text)); text++)
                i = i*10 + (*text - '0');
            }
          if (i < argc)
            obstack_grow (obs, TOKEN_DATA_TEXT (argv[i]),
                          strlen (TOKEN_DATA_TEXT (argv[i])));
          break;

        case '#': /* number of arguments */
          shipout_int (obs, argc - 1);
          text++;
          break;

        case '*': /* all arguments */
        case '@': /* ... same, but quoted */
          dump_args (obs, argc, argv, ",", *text == '@');
          text++;
          break;

        default:
          obstack_1grow (obs, '$');
          break;
        }
    }
}