summaryrefslogtreecommitdiff
path: root/m4/input.c
blob: 0dcb0ae1bec8de431fe22e29e008887d58627914 (plain)
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
/* GNU m4 -- A simple macro processor
   Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2006, 2007, 2008
   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/>.
*/

/* Handling of different input sources, and lexical analysis.  */

#include <config.h>

#include "m4private.h"

/* Define this to see runtime debug info.  Implied by DEBUG.  */
/*#define DEBUG_INPUT */

/* Maximum number of bytes where it is more efficient to inline the
   reference as a string than it is to track reference bookkeeping for
   those bytes.  */
#define INPUT_INLINE_THRESHOLD 16

/*
   Unread input can be either files that should be read (from the
   command line or by include/sinclude), strings which should be
   rescanned (normal macro expansion text), or quoted builtin
   definitions (as returned by the builtin "defn").  Unread input is
   organized in a stack, implemented with an obstack.  Each input
   source is described by a "struct m4_input_block".  The obstack is
   "input_stack".  The top of the input stack is "isp".

   Each input_block has an associated struct input_funcs, which is a
   vtable that defines polymorphic functions for peeking, reading,
   unget, cleanup, and printing in trace output.  All input is done
   through the function pointers of the input_funcs on the given
   input_block, and all characters are unsigned, to distinguish
   between stdio EOF and between special sentinel characters.  When a
   input_block is exhausted, its reader returns CHAR_RETRY which
   causes the input_block to be popped from the input_stack.

   The macro "m4wrap" places the text to be saved on another input
   stack, on the obstack "wrapup_stack", whose top is "wsp".  When EOF
   is seen on normal input (eg, when "current_input" is empty), input
   is switched over to "wrapup_stack", and the original
   "current_input" is freed.  A new stack is allocated for
   "wrapup_stack", which will accept any text produced by calls to
   "m4wrap" from within the wrapped text.  This process of shuffling
   "wrapup_stack" to "current_input" can continue indefinitely, even
   generating infinite loops (e.g. "define(`f',`m4wrap(`f')')f"),
   without memory leaks.  Adding wrapped data is done through
   m4_push_wrapup().

   Pushing new input on the input stack is done by m4_push_file(), the
   conceptual m4_push_string(), and m4_push_builtin() (for builtin
   definitions).  As an optimization, since most macro expansions
   result in strings, m4_push_string() is split in two parts,
   push_string_init(), which returns a pointer to the obstack for
   growing the output, and push_string_finish(), which returns a
   pointer to the finished input_block.  Thus, instead of creating a
   new input block for every character pushed, macro expansion need
   only add text to the top of the obstack.  However, it is not safe
   to alter the input stack while a string is being constructed.  This
   means the input engine is one of two states: consuming input, or
   collecting a macro's expansion.  The input_block *next is used to
   manage the coordination between the different push routines.

   Normally, input sources behave in LIFO order, resembling a stack.
   But thanks to the defn and m4wrap macros, when collecting the
   expansion of a macro, it is possible that we must intermix multiple
   input blocks in FIFO order.  Therefore, when collecting an
   expansion, a meta-input block is formed which will visit its
   children in FIFO order, without losing data when the obstack is
   cleared in LIFO order.

   The current file and line number are stored in the context, for use
   by the error handling functions in utility.c.  When collecting a
   macro's expansion, these variables can be temporarily inconsistent
   in order to provide better error message locations, but they must
   be restored before further parsing takes place.  Each input block
   maintains its own notion of the current file and line, so swapping
   between input blocks must update the context accordingly.  */

static	int	file_peek		(m4_input_block *);
static	int	file_read		(m4_input_block *, m4 *, bool, bool);
static	void	file_unget		(m4_input_block *, int);
static	bool	file_clean		(m4_input_block *, m4 *, bool);
static	void	file_print		(m4_input_block *, m4 *, m4_obstack *);
static	int	builtin_peek		(m4_input_block *);
static	int	builtin_read		(m4_input_block *, m4 *, bool, bool);
static	void	builtin_unget		(m4_input_block *, int);
static	void	builtin_print		(m4_input_block *, m4 *, m4_obstack *);
static	int	string_peek		(m4_input_block *);
static	int	string_read		(m4_input_block *, m4 *, bool, bool);
static	void	string_unget		(m4_input_block *, int);
static	void	string_print		(m4_input_block *, m4 *, m4_obstack *);
static	int	composite_peek		(m4_input_block *);
static	int	composite_read		(m4_input_block *, m4 *, bool, bool);
static	void	composite_unget		(m4_input_block *, int);
static	bool	composite_clean		(m4_input_block *, m4 *, bool);
static	void	composite_print		(m4_input_block *, m4 *, m4_obstack *);

static	void	init_builtin_token	(m4 *, m4_symbol_value *);
static	void	append_quote_token	(m4_obstack *, m4_symbol_value *);
static	bool	match_input		(m4 *, const char *, bool);
static	int	next_char		(m4 *, bool, bool);
static	int	peek_char		(m4 *);
static	bool	pop_input		(m4 *, bool);
static	void	unget_input		(int);
static	bool	consume_syntax		(m4 *, m4_obstack *, unsigned int);

#ifdef DEBUG_INPUT
static int m4_print_token (const char *, m4__token_type, m4_symbol_value *);
#endif

/* Vtable of callbacks for each input method.  */
struct input_funcs
{
  /* Peek at input, return an unsigned char, CHAR_BUILTIN if it is a
     builtin, or CHAR_RETRY if none available.  */
  int	(*peek_func)	(m4_input_block *);

  /* Read input, return an unsigned char, CHAR_BUILTIN if it is a
     builtin, or CHAR_RETRY if none available.  If ALLOW_QUOTE, then
     CHAR_QUOTE may be returned.  If SAFE, then do not alter the
     current file or line.  */
  int	(*read_func)	(m4_input_block *, m4 *, bool allow_quote, bool safe);

  /* Unread a single unsigned character or CHAR_BUILTIN, must be the
     same character previously read by read_func.  */
  void	(*unget_func)	(m4_input_block *, int);

  /* Optional function to perform cleanup at end of input.  If
     CLEANUP, it is safe to perform non-recoverable cleanup actions.
     Return true only if no cleanup remains to be done.  */
  bool	(*clean_func)	(m4_input_block *, m4 *, bool cleanup);

  /* Add a representation of the input block to the obstack, for use
     in trace expansion output.  */
  void	(*print_func)	(m4_input_block *, m4 *, m4_obstack *);
};

/* A block of input to be scanned.  */
struct m4_input_block
{
  m4_input_block *prev;		/* Previous input_block on the input stack.  */
  struct input_funcs *funcs;	/* Virtual functions of this input_block.  */
  const char *file;		/* File where this input is from.  */
  int line;			/* Line where this input is from.  */

  union
    {
      struct
	{
	  char *str;		/* String value.  */
	  size_t len;		/* Remaining length.  */
	}
      u_s;	/* See string_funcs.  */
      struct
	{
	  FILE *fp;			/* Input file handle.  */
	  bool_bitfield end : 1;	/* True iff peek returned EOF.  */
	  bool_bitfield close : 1;	/* True to close file on pop.  */
	  bool_bitfield line_start : 1;	/* Saved start_of_input_line state.  */
	}
      u_f;	/* See file_funcs.  */
      struct
	{
	  const m4_builtin *builtin;	/* Pointer to builtin's function.  */
	  m4_module *module;		/* Originating module.  */
	  bool_bitfield read : 1;	/* True iff block has been read.  */
	  int flags : 24;		/* Flags tied to the builtin. */
	  m4_hash *arg_signature;	/* Argument signature for builtin.  */
	}
      u_b;	/* See builtin_funcs.  */
      struct
	{
	  m4_symbol_chain *chain;	/* Current link in chain.  */
	  m4_symbol_chain *end;		/* Last link in chain.  */
	}
      u_c;	/* See composite_funcs.  */
    }
  u;
};


/* Obstack for storing individual tokens.  */
static m4_obstack token_stack;

/* Obstack for storing input file names.  */
static m4_obstack file_names;

/* Wrapup input stack.

   FIXME - m4wrap should be FIFO, which implies a queue, not a stack.
   While fixing this, m4wrap should also remember what the current
   file and line are for each chunk of wrapped text.  */
static m4_obstack *wrapup_stack;

/* Current stack, from input or wrapup.  */
static m4_obstack *current_input;

/* Bottom of token_stack, for obstack_free.  */
static void *token_bottom;

/* Pointer to top of current_input.  */
static m4_input_block *isp;

/* Pointer to top of wrapup_stack.  */
static m4_input_block *wsp;

/* Aux. for handling split m4_push_string ().  */
static m4_input_block *next;

/* Flag for next_char () to increment current_line.  */
static bool start_of_input_line;

/* Flag for next_char () to recognize change in input block.  */
static bool input_change;

/* Vtable for handling input from files.  */
static struct input_funcs file_funcs = {
  file_peek, file_read, file_unget, file_clean, file_print
};

/* Vtable for handling input from builtin functions.  */
static struct input_funcs builtin_funcs = {
  builtin_peek, builtin_read, builtin_unget, NULL, builtin_print
};

/* Vtable for handling input from strings.  */
static struct input_funcs string_funcs = {
  string_peek, string_read, string_unget, NULL, string_print
};

/* Vtable for handling input from composite chains.  */
static struct input_funcs composite_funcs = {
  composite_peek, composite_read, composite_unget, composite_clean,
  composite_print
};


/* Input files, from command line or [s]include.  */
static int
file_peek (m4_input_block *me)
{
  int ch;

  ch = me->u.u_f.end ? EOF : getc (me->u.u_f.fp);
  if (ch == EOF)
    {
      me->u.u_f.end = true;
      return CHAR_RETRY;
    }

  ungetc (ch, me->u.u_f.fp);
  return ch;
}

static int
file_read (m4_input_block *me, m4 *context, bool allow_quote M4_GNUC_UNUSED,
	   bool safe M4_GNUC_UNUSED)
{
  int ch;

  if (start_of_input_line)
    {
      start_of_input_line = false;
      m4_set_current_line (context, ++me->line);
    }

  /* If stdin is a terminal, calling getc after peek_char already
     called it would make the user have to hit ^D twice to quit.  */
  ch = me->u.u_f.end ? EOF : getc (me->u.u_f.fp);
  if (ch == EOF)
    {
      me->u.u_f.end = true;
      return CHAR_RETRY;
    }

  if (ch == '\n')
    start_of_input_line = true;
  return ch;
}

static void
file_unget (m4_input_block *me, int ch)
{
  assert (ch < CHAR_EOF);
  if (ungetc (ch, me->u.u_f.fp) < 0)
    {
      assert (!"INTERNAL ERROR: failed ungetc!");
      abort (); /* ungetc should not be called without a previous read.  */
    }
  me->u.u_f.end = false;
  if (ch == '\n')
    start_of_input_line = false;
}

static bool
file_clean (m4_input_block *me, m4 *context, bool cleanup)
{
  if (!cleanup)
    return false;
  if (me->prev)
    m4_debug_message (context, M4_DEBUG_TRACE_INPUT,
		      _("input reverted to %s, line %d"),
		      me->prev->file, me->prev->line);
  else
    m4_debug_message (context, M4_DEBUG_TRACE_INPUT, _("input exhausted"));

  if (ferror (me->u.u_f.fp))
    {
      m4_error (context, 0, 0, NULL, _("error reading file `%s'"), me->file);
      if (me->u.u_f.close)
	fclose (me->u.u_f.fp);
    }
  else if (me->u.u_f.close && fclose (me->u.u_f.fp) == EOF)
    m4_error (context, 0, errno, NULL, _("error reading file `%s'"), me->file);
  start_of_input_line = me->u.u_f.line_start;
  m4_set_output_line (context, -1);
  return true;
}

static void
file_print (m4_input_block *me, m4 *context, m4_obstack *obs)
{
  const char *text = me->file;
  obstack_grow (obs, "<file: ", strlen ("<file: "));
  obstack_grow (obs, text, strlen (text));
  obstack_1grow (obs, '>');
}

/* m4_push_file () pushes an input file FP with name TITLE on the
  input stack, saving the current file name and line number.  If next
  is non-NULL, this push invalidates a call to m4_push_string_init (),
  whose storage is consequently released.  If CLOSE, then close FP at
  end of file.

  file_read () manages line numbers for error messages, so they do not
  get wrong due to lookahead.  The token consisting of a newline
  alone is taken as belonging to the line it ends, and the current
  line number is not incremented until the next character is read.  */
void
m4_push_file (m4 *context, FILE *fp, const char *title, bool close_file)
{
  m4_input_block *i;

  if (next != NULL)
    {
      obstack_free (current_input, next);
      next = NULL;
    }

  m4_debug_message (context, M4_DEBUG_TRACE_INPUT,
		    _("input read from %s"), title);

  i = (m4_input_block *) obstack_alloc (current_input, sizeof *i);
  i->funcs = &file_funcs;
  /* Save title on a separate obstack, so that wrapped text can refer
     to it even after the file is popped.  */
  i->file = obstack_copy0 (&file_names, title, strlen (title));
  i->line = 1;

  i->u.u_f.fp = fp;
  i->u.u_f.end = false;
  i->u.u_f.close = close_file;
  i->u.u_f.line_start = start_of_input_line;

  m4_set_output_line (context, -1);

  i->prev = isp;
  isp = i;
  input_change = true;
}


/* Handle a builtin macro token.  */
static int
builtin_peek (m4_input_block *me)
{
  if (me->u.u_b.read)
    return CHAR_RETRY;

  return CHAR_BUILTIN;
}

static int
builtin_read (m4_input_block *me, m4 *context M4_GNUC_UNUSED,
	      bool allow_quote M4_GNUC_UNUSED, bool safe M4_GNUC_UNUSED)
{
  if (me->u.u_b.read)
    return CHAR_RETRY;

  me->u.u_b.read = true;
  return CHAR_BUILTIN;
}

static void
builtin_unget (m4_input_block *me, int ch)
{
  assert (ch == CHAR_BUILTIN && me->u.u_b.read);
  me->u.u_b.read = false;
}

static void
builtin_print (m4_input_block *me, m4 *context, m4_obstack *obs)
{
  const m4_builtin *bp = me->u.u_b.builtin;
  const char *text = bp->name;

  obstack_1grow (obs, '<');
  obstack_grow (obs, text, strlen (text));
  obstack_1grow (obs, '>');
  if (m4_is_debug_bit (context, M4_DEBUG_TRACE_MODULE))
    {
      text = m4_get_module_name (me->u.u_b.module);
      obstack_1grow (obs, '{');
      obstack_grow (obs, text, strlen (text));
      obstack_1grow (obs, '}');
    }
}

/* m4_push_builtin () pushes TOKEN, which contains a builtin's
   definition, on the input stack.  If next is non-NULL, this push
   invalidates a call to m4_push_string_init (), whose storage is
   consequently released.  */
void
m4_push_builtin (m4 *context, m4_symbol_value *token)
{
  m4_input_block *i;

  /* Make sure we were passed a builtin function type token.  */
  assert (m4_is_symbol_value_func (token));

  if (next != NULL)
    {
      obstack_free (current_input, next);
      next = NULL;
    }

  i = (m4_input_block *) obstack_alloc (current_input, sizeof *i);
  i->funcs = &builtin_funcs;
  i->file = m4_get_current_file (context);
  i->line = m4_get_current_line (context);

  i->u.u_b.builtin	= m4_get_symbol_value_builtin (token);
  i->u.u_b.module	= VALUE_MODULE (token);
  i->u.u_b.arg_signature = VALUE_ARG_SIGNATURE (token);
  i->u.u_b.flags	= VALUE_FLAGS (token);
  /* Check for bitfield truncation.  */
  assert (i->u.u_b.flags == VALUE_FLAGS (token)
	  && i->u.u_b.builtin->min_args == VALUE_MIN_ARGS (token)
	  && i->u.u_b.builtin->max_args == VALUE_MAX_ARGS (token));
  i->u.u_b.read		= false;

  i->prev = isp;
  isp = i;
  input_change = true;
}


/* Handle string expansion text.  */
static int
string_peek (m4_input_block *me)
{
  return me->u.u_s.len ? to_uchar (*me->u.u_s.str) : CHAR_RETRY;
}

static int
string_read (m4_input_block *me, m4 *context M4_GNUC_UNUSED,
	     bool allow_quote M4_GNUC_UNUSED, bool safe M4_GNUC_UNUSED)
{
  if (!me->u.u_s.len)
    return CHAR_RETRY;
  me->u.u_s.len--;
  return to_uchar (*me->u.u_s.str++);
}

static void
string_unget (m4_input_block *me, int ch)
{
  assert (ch < CHAR_EOF && to_uchar (me->u.u_s.str[-1]) == ch);
  me->u.u_s.str--;
  me->u.u_s.len++;
}

static void
string_print (m4_input_block *me, m4 *context, m4_obstack *obs)
{
  bool quote = m4_is_debug_bit (context, M4_DEBUG_TRACE_QUOTE);
  size_t arg_length = m4_get_max_debug_arg_length_opt (context);

  m4_shipout_string_trunc (context, obs, me->u.u_s.str, me->u.u_s.len,
			   quote, &arg_length);
}

/* First half of m4_push_string ().  The pointer next points to the
   new input_block.  Return the obstack that will collect the
   expansion text.  */
m4_obstack *
m4_push_string_init (m4 *context)
{
  /* Free any memory occupied by completely parsed input.  */
  assert (!next);
  while (isp && pop_input (context, false));

  /* Reserve the next location on the obstack.  */
  next = (m4_input_block *) obstack_alloc (current_input, sizeof *next);
  next->funcs = &string_funcs;
  next->file = m4_get_current_file (context);
  next->line = m4_get_current_line (context);

  return current_input;
}

/* If VALUE contains text, then convert the current string into a
   chain if it is not one already, and add the contents of VALUE as a
   new link in the chain.  LEVEL describes the current expansion
   level, or SIZE_MAX if the contents of VALUE reside entirely on the
   current_input stack and VALUE lives in temporary storage.  Allows
   gathering input from multiple locations, rather than copying
   everything consecutively onto the input stack.  Must be called
   between push_string_init and push_string_finish.  Return true only
   if LEVEL is less than SIZE_MAX and a reference was created to
   VALUE, in which case, the lifetime of the contents of VALUE must
   last as long as the input engine can parse references from it.  */
bool
m4__push_symbol (m4 *context, m4_symbol_value *value, size_t level)
{
  m4_symbol_chain *chain;
  bool result = false;

  assert (next);
  /* TODO - also accept TOKEN_COMP chains.  */
  assert (m4_is_symbol_value_text (value));

  /* Speed consideration - for short enough symbols, the speed and
     memory overhead of parsing another INPUT_CHAIN link outweighs the
     time to inline the symbol text.  */
  if (m4_get_symbol_value_len (value) <= INPUT_INLINE_THRESHOLD)
    {
      obstack_grow (current_input, m4_get_symbol_value_text (value),
		    m4_get_symbol_value_len (value));
      return false;
    }

  if (next->funcs == &string_funcs)
    {
      next->funcs = &composite_funcs;
      next->u.u_c.chain = next->u.u_c.end = NULL;
    }
  m4__make_text_link (current_input, &next->u.u_c.chain, &next->u.u_c.end);
  chain = (m4_symbol_chain *) obstack_alloc (current_input, sizeof *chain);
  if (next->u.u_c.end)
    next->u.u_c.end->next = chain;
  else
    next->u.u_c.chain = chain;
  next->u.u_c.end = chain;
  chain->next = NULL;
  chain->quote_age = m4_get_symbol_value_quote_age (value);
  chain->str = m4_get_symbol_value_text (value);
  chain->len = m4_get_symbol_value_len (value);
  chain->level = level;
  chain->argv = NULL;
  chain->index = 0;
  chain->flatten = false;
  if (level < SIZE_MAX)
    {
      m4__adjust_refcount (context, level, true);
      result = true;
    }
  return result;
}

/* Last half of m4_push_string ().  If next is now NULL, a call to
   m4_push_file () or m4_push_builtin () has pushed a different input
   block to the top of the stack.  If the new object is void, we do
   not push it.  The function m4_push_string_finish () returns the
   opaque finished object, whether that is still a string or has been
   replaced by a file or builtin; this object can then be used in
   m4_input_print () during tracing.  This pointer is only for
   temporary use, since reading the next token might release the
   memory used for the object.  */
m4_input_block *
m4_push_string_finish (void)
{
  m4_input_block *ret = NULL;
  size_t len = obstack_object_size (current_input);

  if (next == NULL)
    {
      assert (!len);
      return isp;
    }

  if (len || next->funcs == &composite_funcs)
    {
      if (next->funcs == &string_funcs)
	{
	  next->u.u_s.str = (char *) obstack_finish (current_input);
	  next->u.u_s.len = len;
	}
      else
	m4__make_text_link (current_input, &next->u.u_c.chain,
			    &next->u.u_c.end);
      next->prev = isp;
      ret = isp = next;
      input_change = true;
    }
  else
    obstack_free (current_input, next);
  next = NULL;
  return ret;
}


/* A composite block contains multiple sub-blocks which are processed
   in FIFO order, even though the obstack allocates memory in LIFO
   order.  */
static int
composite_peek (m4_input_block *me)
{
  m4_symbol_chain *chain = me->u.u_c.chain;
  while (chain)
    {
      if (chain->str)
	{
	  if (chain->len)
	    return to_uchar (chain->str[0]);
	}
      else
	{
	  /* TODO - peek into argv.  */
	  assert (!"implemented yet");
	  abort ();
	}
      chain = chain->next;
    }
  return CHAR_RETRY;
}

static int
composite_read (m4_input_block *me, m4 *context, bool allow_quote, bool safe)
{
  m4_symbol_chain *chain = me->u.u_c.chain;
  while (chain)
    {
      if (allow_quote && chain->quote_age == m4__quote_age (M4SYNTAX))
	return CHAR_QUOTE;
      if (chain->str)
	{
	  if (chain->len)
	    {
	      /* Partial consumption invalidates quote age.  */
	      chain->quote_age = 0;
	      chain->len--;
	      return to_uchar (*chain->str++);
	    }
	}
      else
	{
	  /* TODO - peek into argv.  */
	  assert (!"implemented yet");
	  abort ();
	}
      if (chain->level < SIZE_MAX)
	m4__adjust_refcount (context, chain->level, false);
      me->u.u_c.chain = chain = chain->next;
    }
  return CHAR_RETRY;
}

static void
composite_unget (m4_input_block *me, int ch)
{
  m4_symbol_chain *chain = me->u.u_c.chain;
  if (chain->str)
    {
      assert (ch < CHAR_EOF && to_uchar (chain->str[-1]) == ch);
      chain->str--;
      chain->len++;
    }
  else
    {
      /* TODO support argv ref.  */
      assert (!"implemented yet");
      abort ();
    }
}

static bool
composite_clean (m4_input_block *me, m4 *context, bool cleanup)
{
  m4_symbol_chain *chain = me->u.u_c.chain;
  assert (!chain || !cleanup);
  while (chain)
    {
      if (chain->str)
	assert (!chain->len);
      else
	{
	  /* TODO - peek into argv.  */
	  assert (!"implemented yet");
	  abort ();
	}
      if (chain->level < SIZE_MAX)
	m4__adjust_refcount (context, chain->level, false);
      me->u.u_c.chain = chain = chain->next;
    }
  return true;
}

static void
composite_print (m4_input_block *me, m4 *context, m4_obstack *obs)
{
  bool quote = m4_is_debug_bit (context, M4_DEBUG_TRACE_QUOTE);
  size_t maxlen = m4_get_max_debug_arg_length_opt (context);
  m4_symbol_chain *chain = me->u.u_c.chain;
  const char *lquote = m4_get_syntax_lquote (M4SYNTAX);
  const char *rquote = m4_get_syntax_rquote (M4SYNTAX);

  if (quote)
    m4_shipout_string (context, obs, lquote, SIZE_MAX, false);
  while (chain)
    {
      /* TODO support argv refs as well.  */
      assert (chain->str);
      if (m4_shipout_string_trunc (context, obs, chain->str, chain->len, false,
				   &maxlen))
	break;
      chain = chain->next;
    }
  if (quote)
    m4_shipout_string (context, obs, rquote, SIZE_MAX, false);
}

/* Given an obstack OBS, capture any unfinished text as a link in the
   chain that starts at *START and ends at *END.  START may be NULL if
   *END is non-NULL.  */
void
m4__make_text_link (m4_obstack *obs, m4_symbol_chain **start,
		    m4_symbol_chain **end)
{
  m4_symbol_chain *chain;
  size_t len = obstack_object_size (obs);

  assert (end && (start || *end));
  if (len)
    {
      char *str = (char *) obstack_finish (obs);
      chain = (m4_symbol_chain *) obstack_alloc (obs, sizeof *chain);
      if (*end)
	(*end)->next = chain;
      else
	*start = chain;
      *end = chain;
      chain->next = NULL;
      chain->quote_age = 0;
      chain->str = str;
      chain->len = len;
      chain->level = SIZE_MAX;
      chain->argv = NULL;
      chain->index = 0;
      chain->flatten = false;
    }
}



/* When tracing, print a summary of the contents of the input block
   created by push_string_init/push_string_finish to OBS.  */
void
m4_input_print (m4 *context, m4_obstack *obs, m4_input_block *input)
{
  assert (context && obs);
  if (input == NULL)
    {
      bool quote = m4_is_debug_bit (context, M4_DEBUG_TRACE_QUOTE);
      if (quote)
	{
	  const char *lquote = m4_get_syntax_lquote (M4SYNTAX);
	  const char *rquote = m4_get_syntax_rquote (M4SYNTAX);
	  obstack_grow (obs, lquote, strlen (lquote));
	  obstack_grow (obs, rquote, strlen (rquote));
	}
    }
  else
    {
      assert (input->funcs->print_func);
      input->funcs->print_func (input, context, obs);
    }
}

/* The function m4_push_wrapup () pushes a string on the wrapup stack.
   When the normal input stack gets empty, the wrapup stack will become
   the input stack, and m4_push_string () and m4_push_file () will
   operate on wrapup_stack.  M4_push_wrapup should be done as
   m4_push_string (), but this will suffice, as long as arguments to
   m4_m4wrap () are moderate in size.

   FIXME - we should allow pushing builtins as well as text.  */
void
m4_push_wrapup (m4 *context, const char *s)
{
  m4_input_block *i;

  i = (m4_input_block *) obstack_alloc (wrapup_stack, sizeof *i);
  i->prev = wsp;

  i->funcs = &string_funcs;
  i->file = m4_get_current_file (context);
  i->line = m4_get_current_line (context);

  i->u.u_s.len = strlen (s);
  i->u.u_s.str = obstack_copy (wrapup_stack, s, i->u.u_s.len);

  wsp = i;
}


/* The function pop_input () pops one level of input sources.  If
   CLEANUP, the current_file and current_line are restored as needed.
   The return value is false if cleanup is still required, or if the
   current input source is not at the end.  */
static bool
pop_input (m4 *context, bool cleanup)
{
  m4_input_block *tmp = isp->prev;

  assert (isp);
  if (isp->funcs->peek_func (isp) != CHAR_RETRY
      || (isp->funcs->clean_func
	  && !isp->funcs->clean_func (isp, context, cleanup)))
    return false;

  if (tmp != NULL)
    {
      obstack_free (current_input, isp);
      next = NULL;	/* might be set in m4_push_string_init () */
    }

  isp = tmp;
  input_change = true;
  return true;
}

/* To switch input over to the wrapup stack, main () calls pop_wrapup.
   Since wrapup text can install new wrapup text, pop_wrapup ()
   returns true if there is more wrapped text to parse.  */
bool
m4_pop_wrapup (m4 *context)
{
  static size_t level = 0;

  next = NULL;
  obstack_free (current_input, NULL);
  free (current_input);

  if (wsp == NULL)
    {
      obstack_free (wrapup_stack, NULL);
      m4_set_current_file (context, NULL);
      m4_set_current_line (context, 0);
      m4_debug_message (context, M4_DEBUG_TRACE_INPUT,
		       _("input from m4wrap exhausted"));
      current_input = NULL;
      DELETE (wrapup_stack);
      return false;
    }

  m4_debug_message (context, M4_DEBUG_TRACE_INPUT,
		    _("input from m4wrap recursion level %lu"),
		    (unsigned long int) ++level);

  current_input = wrapup_stack;
  wrapup_stack = (m4_obstack *) xmalloc (sizeof *wrapup_stack);
  obstack_init (wrapup_stack);

  isp = wsp;
  wsp = NULL;
  input_change = true;

  return true;
}

/* When a BUILTIN token is seen, m4__next_token () uses init_builtin_token
   to retrieve the value of the function pointer.  */
static void
init_builtin_token (m4 *context, m4_symbol_value *token)
{
  m4_input_block *block = isp;
  assert (block->funcs->read_func == builtin_read && !block->u.u_b.read);

  m4_set_symbol_value_builtin (token, block->u.u_b.builtin);
  VALUE_MODULE (token)		= block->u.u_b.module;
  VALUE_FLAGS (token)		= block->u.u_b.flags;
  VALUE_ARG_SIGNATURE (token)	= block->u.u_b.arg_signature;
  VALUE_MIN_ARGS (token)	= block->u.u_b.builtin->min_args;
  VALUE_MAX_ARGS (token)	= block->u.u_b.builtin->max_args;
}

/* When a QUOTE token is seen, convert VALUE to a composite (if it is
   not one already), consisting of any unfinished text on OBS, as well
   as the quoted token from the top of the input stack.  Use OBS for
   any additional allocations needed to store the token chain.  */
static void
append_quote_token (m4_obstack *obs, m4_symbol_value *value)
{
  m4_symbol_chain *src_chain = isp->u.u_c.chain;
  m4_symbol_chain *chain;
  assert (isp->funcs == &composite_funcs && obs);

  if (value->type == M4_SYMBOL_VOID)
    {
      value->type = M4_SYMBOL_COMP;
      value->u.u_c.chain = value->u.u_c.end = NULL;
    }
  assert (value->type == M4_SYMBOL_COMP);
  m4__make_text_link (obs, &value->u.u_c.chain, &value->u.u_c.end);
  chain = (m4_symbol_chain *) obstack_copy (obs, src_chain, sizeof *chain);
  if (value->u.u_c.end)
    value->u.u_c.end->next = chain;
  else
    value->u.u_c.chain = chain;
  value->u.u_c.end = chain;
  value->u.u_c.end->next = NULL;
  isp->u.u_c.chain = src_chain->next;
}


/* Low level input is done a character at a time.  The function
   next_char () is used to read and advance the input to the next
   character.  If ALLOW_QUOTE, and the current input matches the
   current quote age, return CHAR_QUOTE and leave consumption of data
   for append_quote_token.  If RETRY, then avoid returning CHAR_RETRY
   by popping input.  */
static int
next_char (m4 *context, bool allow_quote, bool retry)
{
  int ch;

  while (1)
    {
      if (isp == NULL)
	{
	  m4_set_current_file (context, NULL);
	  m4_set_current_line (context, 0);
	  return CHAR_EOF;
	}

      if (input_change)
	{
	  m4_set_current_file (context, isp->file);
	  m4_set_current_line (context, isp->line);
	}

      assert (isp->funcs->read_func);
      while (((ch = isp->funcs->read_func (isp, context, allow_quote, !retry))
	      != CHAR_RETRY)
	     || !retry)
	{
	  /* if (!IS_IGNORE (ch)) */
	  return ch;
	}

      /* End of input source --- pop one level.  */
      pop_input (context, true);
    }
}

/* The function peek_char () is used to look at the next character in
   the input stream.  At any given time, it reads from the input_block
   on the top of the current input stack.  */
static int
peek_char (m4 *context)
{
  int ch;
  m4_input_block *block = isp;

  while (1)
    {
      if (block == NULL)
	return CHAR_EOF;

      assert (block->funcs->peek_func);
      if ((ch = block->funcs->peek_func (block)) != CHAR_RETRY)
	{
/*	  if (IS_IGNORE (ch)) */
/*	    return next_char (context, false, true); */
	  return ch;
	}

      block = block->prev;
    }
}

/* The function unget_input () puts back a character on the input
   stack, using an existing input_block if possible.  This is not safe
   to call except immediately after next_char(context, allow, false).  */
static void
unget_input (int ch)
{
  assert (isp != NULL && isp->funcs->unget_func != NULL);
  isp->funcs->unget_func (isp, ch);
}

/* skip_line () simply discards all immediately following characters,
   up to the first newline.  It is only used from m4_dnl ().  NAME is
   the spelling of argv[0], for use in any warning message.  */
void
m4_skip_line (m4 *context, const char *name)
{
  int ch;
  const char *file = m4_get_current_file (context);
  int line = m4_get_current_line (context);

  while ((ch = next_char (context, false, true)) != CHAR_EOF && ch != '\n')
    ;
  if (ch == CHAR_EOF)
    /* current_file changed; use the previous value we cached.  */
    m4_warn_at_line (context, 0, file, line, name,
		     _("end of file treated as newline"));
  /* On the rare occasion that dnl crosses include file boundaries
     (either the input file did not end in a newline, or changesyntax
     was used), calling next_char can update current_file and
     current_line, and that update will be undone as we return to
     expand_macro.  This tells next_char () to restore the location.  */
  if (file != m4_get_current_file (context)
      || line != m4_get_current_line (context))
    input_change = true;
}


/* This function is for matching a string against a prefix of the
   input stream.  If the string S matches the input and CONSUME is
   true, the input is discarded; otherwise any characters read are
   pushed back again.  The function is used only when multicharacter
   quotes or comment delimiters are used.

   All strings herein should be unsigned.  Otherwise sign-extension
   of individual chars might break quotes with 8-bit chars in it.

   FIXME - when matching multiquotes that cross file boundaries, we do
   not properly restore the current input file and line when we
   restore unconsumed characters.  */
static bool
match_input (m4 *context, const char *s, bool consume)
{
  int n;			/* number of characters matched */
  int ch;			/* input character */
  const char *t;
  m4_obstack *st;
  bool result = false;

  ch = peek_char (context);
  if (ch != to_uchar (*s))
    return false;			/* fail */

  if (s[1] == '\0')
    {
      if (consume)
	next_char (context, false, true);
      return true;			/* short match */
    }

  next_char (context, false, true);
  for (n = 1, t = s++; (ch = peek_char (context)) == to_uchar (*s++); )
    {
      next_char (context, false, true);
      n++;
      if (*s == '\0')		/* long match */
	{
	  if (consume)
	    return true;
	  result = true;
	  break;
	}
    }

  /* Failed or shouldn't consume, push back input.  */
  st = m4_push_string_init (context);
  obstack_grow (st, t, n);
  m4_push_string_finish ();
  return result;
}

/* The macro MATCH() is used to match an unsigned char string S
  against the input.  The first character is handled inline, for
  speed.  Hopefully, this will not hurt efficiency too much when
  single character quotes and comment delimiters are used.  If
  CONSUME, then CH is the result of next_char, and a successful match
  will discard the matched string.  Otherwise, CH is the result of
  peek_char, and the input stream is effectively unchanged.  */
#define MATCH(C, ch, s, consume)					\
  (to_uchar ((s)[0]) == (ch)						\
   && (ch) != '\0'							\
   && ((s)[1] == '\0' || (match_input (C, (s) + (consume), consume))))

/* While the current input character has the given SYNTAX, append it
   to OBS.  Take care not to pop input source unless the next source
   would continue the chain.  Return true if the chain ended with
   CHAR_EOF.  */
static bool
consume_syntax (m4 *context, m4_obstack *obs, unsigned int syntax)
{
  int ch;
  bool allow_quote = m4__safe_quotes (M4SYNTAX);
  assert (syntax);
  while (1)
    {
      /* It is safe to call next_char without first checking
	 peek_char, except at input source boundaries, which we detect
	 by CHAR_RETRY.  We exploit the fact that CHAR_EOF,
	 CHAR_BUILTIN, and CHAR_QUOTE do not satisfy any syntax
	 categories.  */
      while ((ch = next_char (context, allow_quote, false)) != CHAR_RETRY
	     && m4_has_syntax (M4SYNTAX, ch, syntax))
	{
	  assert (ch < CHAR_EOF);
	  obstack_1grow (obs, ch);
	}
      if (ch == CHAR_RETRY || ch == CHAR_QUOTE)
	{
	  ch = peek_char (context);
	  if (m4_has_syntax (M4SYNTAX, ch, syntax))
	    {
	      assert (ch < CHAR_EOF);
	      obstack_1grow (obs, ch);
	      next_char (context, false, true);
	      continue;
	    }
	  return ch == CHAR_EOF;
	}
      unget_input (ch);
      return false;
    }
}


/* Inititialize input stacks.  */
void
m4_input_init (m4 *context)
{
  obstack_init (&file_names);
  m4_set_current_file (context, NULL);
  m4_set_current_line (context, 0);

  current_input = (m4_obstack *) xmalloc (sizeof *current_input);
  obstack_init (current_input);
  wrapup_stack = (m4_obstack *) xmalloc (sizeof *wrapup_stack);
  obstack_init (wrapup_stack);

  /* Allocate an object in the current chunk, so that obstack_free
     will always work even if the first token parsed spills to a new
     chunk.  */
  obstack_init (&token_stack);
  token_bottom = obstack_finish (&token_stack);

  isp = NULL;
  wsp = NULL;
  next = NULL;

  start_of_input_line = false;
}

/* Free memory used by the input engine.  */
void
m4_input_exit (void)
{
  assert (current_input == NULL);
  assert (wrapup_stack == NULL);
  obstack_free (&file_names, NULL);
  obstack_free (&token_stack, NULL);
}


/* Parse and return a single token from the input stream, constructed
   into TOKEN.  See m4__token_type for the valid return types, along
   with a description of what TOKEN will contain.  If LINE is not
   NULL, set *LINE to the line number where the token starts.  If OBS,
   expand safe tokens (strings and comments) directly into OBS rather
   than in a temporary staging area.  Report errors (unterminated
   comments or strings) on behalf of CALLER, if non-NULL.

   If OBS is NULL or the token expansion is unknown, the token text is
   collected on the obstack token_stack, which never contains more
   than one token text at a time.  The storage pointed to by the
   fields in TOKEN is therefore subject to change the next time
   m4__next_token () is called.  */
m4__token_type
m4__next_token (m4 *context, m4_symbol_value *token, int *line,
		m4_obstack *obs, const char *caller)
{
  int ch;
  int quote_level;
  m4__token_type type;
  const char *file;
  int dummy;
  size_t len;
  /* The obstack where token data is stored.  Generally token_stack,
     for tokens where argument collection might not use the literal
     token.  But for comments and strings, we can output directly into
     the argument collection obstack OBS, if provided.  */
  m4_obstack *obs_safe = &token_stack;

  assert (next == NULL);
  if (!line)
    line = &dummy;
  memset (token, '\0', sizeof *token);
  do {
    obstack_free (&token_stack, token_bottom);

    /* Must consume an input character, but not until CHAR_BUILTIN is
       handled.  */
    ch = peek_char (context);
    if (ch == CHAR_EOF)			/* EOF */
      {
#ifdef DEBUG_INPUT
	xfprintf (stderr, "next_token -> EOF\n");
#endif
	next_char (context, false, true);
	return M4_TOKEN_EOF;
      }

    if (ch == CHAR_BUILTIN)		/* BUILTIN TOKEN */
      {
	init_builtin_token (context, token);
	next_char (context, false, true);
#ifdef DEBUG_INPUT
	m4_print_token ("next_token", M4_TOKEN_MACDEF, token);
#endif
	return M4_TOKEN_MACDEF;
      }

    /* Consume character we already peeked at.  */
    next_char (context, false, true);
    file = m4_get_current_file (context);
    *line = m4_get_current_line (context);

    if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_ESCAPE))
      {					/* ESCAPED WORD */
	obstack_1grow (&token_stack, ch);
	if ((ch = next_char (context, false, true)) < CHAR_EOF)
	  {
	    obstack_1grow (&token_stack, ch);
	    if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_ALPHA))
	      consume_syntax (context, &token_stack,
			      M4_SYNTAX_ALPHA | M4_SYNTAX_NUM);
	    type = M4_TOKEN_WORD;
	  }
	else
	  type = M4_TOKEN_SIMPLE;	/* escape before eof */
      }
    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_ALPHA))
      {
	type = (m4_is_syntax_macro_escaped (M4SYNTAX)
		? M4_TOKEN_STRING : M4_TOKEN_WORD);
	if (type == M4_TOKEN_STRING && obs)
	  obs_safe = obs;
	obstack_1grow (obs_safe, ch);
	consume_syntax (context, obs_safe, M4_SYNTAX_ALPHA | M4_SYNTAX_NUM);
      }
    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_LQUOTE))
      {					/* QUOTED STRING, SINGLE QUOTES */
	if (obs)
	  obs_safe = obs;
	quote_level = 1;
	while (1)
	  {
	    ch = next_char (context, obs && m4__quote_age (M4SYNTAX), true);
	    if (ch == CHAR_EOF)
	      m4_error_at_line (context, EXIT_FAILURE, 0, file, *line, caller,
				_("end of file in string"));
	    if (ch == CHAR_QUOTE)
	      append_quote_token (obs, token);
	    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_RQUOTE))
	      {
		if (--quote_level == 0)
		  break;
		obstack_1grow (obs_safe, ch);
	      }
	    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_LQUOTE))
	      {
		quote_level++;
		obstack_1grow (obs_safe, ch);
	      }
	    else
	      obstack_1grow (obs_safe, ch);
	  }
	type = M4_TOKEN_STRING;
      }
    else if (!m4_is_syntax_single_quotes (M4SYNTAX)
	     && MATCH (context, ch, context->syntax->lquote.string, true))
      {					/* QUOTED STRING, LONGER QUOTES */
	if (obs)
	  obs_safe = obs;
	quote_level = 1;
	assert (!m4__quote_age (M4SYNTAX));
	while (1)
	  {
	    ch = next_char (context, false, true);
	    if (ch == CHAR_EOF)
	      m4_error_at_line (context, EXIT_FAILURE, 0, file, *line, caller,
				_("end of file in string"));
	    if (MATCH (context, ch, context->syntax->rquote.string, true))
	      {
		if (--quote_level == 0)
		  break;
		obstack_grow (obs_safe, context->syntax->rquote.string,
			      context->syntax->rquote.length);
	      }
	    else if (MATCH (context, ch, context->syntax->lquote.string, true))
	      {
		quote_level++;
		obstack_grow (obs_safe, context->syntax->lquote.string,
			      context->syntax->lquote.length);
	      }
	    else
	      obstack_1grow (obs_safe, ch);
	  }
	type = M4_TOKEN_STRING;
      }
    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_BCOMM))
      {					/* COMMENT, SHORT DELIM */
	if (obs && !m4_get_discard_comments_opt (context))
	  obs_safe = obs;
	obstack_1grow (obs_safe, ch);
	while ((ch = next_char (context, false, true)) < CHAR_EOF
	       && !m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_ECOMM))
	  obstack_1grow (obs_safe, ch);
	if (ch != CHAR_EOF)
	  {
	    assert (ch < CHAR_EOF);
	    obstack_1grow (obs_safe, ch);
	  }
	else
	  m4_error_at_line (context, EXIT_FAILURE, 0, file, *line, caller,
			    _("end of file in comment"));
	type = (m4_get_discard_comments_opt (context)
		? M4_TOKEN_NONE : M4_TOKEN_STRING);
      }
    else if (!m4_is_syntax_single_comments (M4SYNTAX)
	     && MATCH (context, ch, context->syntax->bcomm.string, true))
      {					/* COMMENT, LONGER DELIM */
	if (obs && !m4_get_discard_comments_opt (context))
	  obs_safe = obs;
	obstack_grow (obs_safe, context->syntax->bcomm.string,
		      context->syntax->bcomm.length);
	while ((ch = next_char (context, false, true)) < CHAR_EOF
	       && !MATCH (context, ch, context->syntax->ecomm.string, true))
	  obstack_1grow (obs_safe, ch);
	if (ch != CHAR_EOF)
	  {
	    assert (ch < CHAR_EOF);
	    obstack_grow (obs_safe, context->syntax->ecomm.string,
			  context->syntax->ecomm.length);
	  }
	else
	  m4_error_at_line (context, EXIT_FAILURE, 0, file, *line, caller,
			    _("end of file in comment"));
	type = (m4_get_discard_comments_opt (context)
		? M4_TOKEN_NONE : M4_TOKEN_STRING);
      }
    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_ACTIVE))
      {					/* ACTIVE CHARACTER */
	obstack_1grow (&token_stack, ch);
	type = M4_TOKEN_WORD;
      }
    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_OPEN))
      {					/* OPEN PARENTHESIS */
	obstack_1grow (&token_stack, ch);
	type = M4_TOKEN_OPEN;
      }
    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_COMMA))
      {					/* COMMA */
	obstack_1grow (&token_stack, ch);
	type = M4_TOKEN_COMMA;
      }
    else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_CLOSE))
      {					/* CLOSE PARENTHESIS */
	obstack_1grow (&token_stack, ch);
	type = M4_TOKEN_CLOSE;
      }
    else if (m4_is_syntax_single_quotes (M4SYNTAX)
	     && m4_is_syntax_single_comments (M4SYNTAX))
      {			/* EVERYTHING ELSE (SHORT QUOTES AND COMMENTS) */
	assert (ch < CHAR_EOF);
	obstack_1grow (&token_stack, ch);

	if (m4_has_syntax (M4SYNTAX, ch,
			   (M4_SYNTAX_OTHER | M4_SYNTAX_NUM | M4_SYNTAX_DOLLAR
			    | M4_SYNTAX_LBRACE | M4_SYNTAX_RBRACE)))
	  {
	    if (obs)
	      {
		obs_safe = obs;
		obstack_1grow (obs, ch);
	      }
	    consume_syntax (context, obs_safe,
			    (M4_SYNTAX_OTHER | M4_SYNTAX_NUM
			     | M4_SYNTAX_DOLLAR | M4_SYNTAX_LBRACE
			     | M4_SYNTAX_RBRACE));
	    type = M4_TOKEN_STRING;
	  }
	else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_SPACE))
	  {
	    /* Coalescing newlines when interactive or when synclines
	       are enabled is wrong.  */
	    if (!m4_get_interactive_opt (context)
		&& !m4_get_syncoutput_opt (context))
	      consume_syntax (context, &token_stack, M4_SYNTAX_SPACE);
	    type = M4_TOKEN_SPACE;
	  }
	else
	  type = M4_TOKEN_SIMPLE;
      }
    else		/* EVERYTHING ELSE (LONG QUOTES OR COMMENTS) */
      {
	assert (ch < CHAR_EOF);
	obstack_1grow (&token_stack, ch);

	if (m4_has_syntax (M4SYNTAX, ch,
			   (M4_SYNTAX_OTHER | M4_SYNTAX_NUM | M4_SYNTAX_DOLLAR
			    | M4_SYNTAX_LBRACE | M4_SYNTAX_RBRACE)))
	  {
	    if (obs)
	      {
		obs_safe = obs;
		obstack_1grow (obs, ch);
	      }
	    type = M4_TOKEN_STRING;
	  }
	else if (m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_SPACE))
	  type = M4_TOKEN_SPACE;
	else
	  type = M4_TOKEN_SIMPLE;
      }
  } while (type == M4_TOKEN_NONE);

  if (token->type == M4_SYMBOL_VOID)
    {
      if (obs_safe != obs)
	{
	  len = obstack_object_size (&token_stack);
	  obstack_1grow (&token_stack, '\0');

	  m4_set_symbol_value_text (token, obstack_finish (&token_stack), len,
				    m4__quote_age (M4SYNTAX));
	}
      else
	assert (type == M4_TOKEN_STRING);
    }
  else
    assert (token->type == M4_SYMBOL_COMP && type == M4_TOKEN_STRING);
  VALUE_MAX_ARGS (token) = -1;

#ifdef DEBUG_INPUT
  m4_print_token ("next_token", type, token);
#endif

  return type;
}

/* Peek at the next token in the input stream to see if it is an open
   parenthesis.  It is possible that what is peeked at may change as a
   result of changequote (or friends).  This honors multi-character
   comments and quotes, just as next_token does.  */
bool
m4__next_token_is_open (m4 *context)
{
  int ch = peek_char (context);

  if (ch == CHAR_EOF || ch == CHAR_BUILTIN
      || m4_has_syntax (M4SYNTAX, ch, (M4_SYNTAX_BCOMM | M4_SYNTAX_ESCAPE
				       | M4_SYNTAX_ALPHA | M4_SYNTAX_LQUOTE
				       | M4_SYNTAX_ACTIVE))
      || (!m4_is_syntax_single_comments (M4SYNTAX)
	  && MATCH (context, ch, context->syntax->bcomm.string, false))
      || (!m4_is_syntax_single_quotes (M4SYNTAX)
	  && MATCH (context, ch, context->syntax->lquote.string, false)))
    return false;
  return m4_has_syntax (M4SYNTAX, ch, M4_SYNTAX_OPEN);
}


#ifdef DEBUG_INPUT

int
m4_print_token (const char *s, m4__token_type type, m4_symbol_value *token)
{
  m4_obstack obs;
  size_t len;

  obstack_init (&obs);
  if (!s)
    s = "m4input";
  obstack_grow (&obs, s, strlen (s));
  obstack_1grow (&obs, ':');
  obstack_1grow (&obs, ' ');
  switch (type)
    {				/* TOKSW */
    case M4_TOKEN_EOF:
      obstack_grow (&obs, "eof", strlen ("eof"));
      token = NULL;
      break;
    case M4_TOKEN_NONE:
      obstack_grow (&obs, "none", strlen ("none"));
      token = NULL;
      break;
    case M4_TOKEN_STRING:
      obstack_grow (&obs, "string\t", strlen ("string\t"));
      break;
    case M4_TOKEN_SPACE:
      obstack_grow (&obs, "space\t", strlen ("space\t"));
      break;
    case M4_TOKEN_WORD:
      obstack_grow (&obs, "word\t", strlen ("word\t"));
      break;
    case M4_TOKEN_OPEN:
      obstack_grow (&obs, "open\t", strlen ("open\t"));
      break;
    case M4_TOKEN_COMMA:
      obstack_grow (&obs, "comma\t", strlen ("comma\t"));
      break;
    case M4_TOKEN_CLOSE:
      obstack_grow (&obs, "close\t", strlen ("close\t"));
      break;
    case M4_TOKEN_SIMPLE:
      obstack_grow (&obs, "simple\t", strlen ("simple\t"));
      break;
    case M4_TOKEN_MACDEF:
      obstack_grow (&obs, "builtin\t", strlen ("builtin\t"));
      break;
    default:
      abort ();
    }
  if (token)
    m4_symbol_value_print (token, &obs, true, "\"", "\"", SIZE_MAX, NULL);
  obstack_1grow (&obs, '\n');
  len = obstack_object_size (&obs);
  fwrite (obstack_finish (&obs), 1, len, stderr);
  obstack_free (&obs, NULL);
  return 0;
}
#endif /* DEBUG_INPUT */