add packaging
[platform/upstream/m4.git] / src / input.c
1 /* GNU m4 -- A simple macro processor
2
3    Copyright (C) 1989-1994, 2004-2013 Free Software Foundation, Inc.
4
5    This file is part of GNU M4.
6
7    GNU M4 is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    GNU M4 is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* Handling of different input sources, and lexical analysis.  */
22
23 #include "m4.h"
24
25 #include "memchr2.h"
26
27 /* Unread input can be either files, that should be read (eg. included
28    files), strings, which should be rescanned (eg. macro expansion text),
29    or quoted macro definitions (as returned by the builtin "defn").
30    Unread input are organised in a stack, implemented with an obstack.
31    Each input source is described by a "struct input_block".  The obstack
32    is "current_input".  The top of the input stack is "isp".
33
34    The macro "m4wrap" places the text to be saved on another input
35    stack, on the obstack "wrapup_stack", whose top is "wsp".  When EOF
36    is seen on normal input (eg, when "current_input" is empty), input is
37    switched over to "wrapup_stack", and the original "current_input" is
38    freed.  A new stack is allocated for "wrapup_stack", which will
39    accept any text produced by calls to "m4wrap" from within the
40    wrapped text.  This process of shuffling "wrapup_stack" to
41    "current_input" can continue indefinitely, even generating infinite
42    loops (e.g. "define(`f',`m4wrap(`f')')f"), without memory leaks.
43
44    Pushing new input on the input stack is done by push_file (),
45    push_string (), push_wrapup () (for wrapup text), and push_macro ()
46    (for macro definitions).  Because macro expansion needs direct access
47    to the current input obstack (for optimisation), push_string () are
48    split in two functions, push_string_init (), which returns a pointer
49    to the current input stack, and push_string_finish (), which return a
50    pointer to the final text.  The input_block *next is used to manage
51    the coordination between the different push routines.
52
53    The current file and line number are stored in two global
54    variables, for use by the error handling functions in m4.c.  Macro
55    expansion wants to report the line where a macro name was detected,
56    rather than where it finished collecting arguments.  This also
57    applies to text resulting from macro expansions.  So each input
58    block maintains its own notion of the current file and line, and
59    swapping between input blocks updates the global variables
60    accordingly.  */
61
62 #ifdef ENABLE_CHANGEWORD
63 #include "regex.h"
64 #endif
65
66 enum input_type
67 {
68   INPUT_STRING,         /* String resulting from macro expansion.  */
69   INPUT_FILE,           /* File from command line or include.  */
70   INPUT_MACRO           /* Builtin resulting from defn.  */
71 };
72
73 typedef enum input_type input_type;
74
75 struct input_block
76 {
77   struct input_block *prev;     /* previous input_block on the input stack */
78   input_type type;              /* see enum values */
79   const char *file;             /* file where this input is from */
80   int line;                     /* line where this input is from */
81   union
82     {
83       struct
84         {
85           char *string;         /* remaining string value */
86           char *end;            /* terminating NUL of string */
87         }
88         u_s;    /* INPUT_STRING */
89       struct
90         {
91           FILE *fp;                  /* input file handle */
92           bool_bitfield end : 1;     /* true if peek has seen EOF */
93           bool_bitfield close : 1;   /* true if we should close file on pop */
94           bool_bitfield advance : 1; /* track previous start_of_input_line */
95         }
96         u_f;    /* INPUT_FILE */
97       builtin_func *func;       /* pointer to macro's function */
98     }
99   u;
100 };
101
102 typedef struct input_block input_block;
103
104 \f
105 /* Current input file name.  */
106 const char *current_file;
107
108 /* Current input line number.  */
109 int current_line;
110
111 /* Obstack for storing individual tokens.  */
112 static struct obstack token_stack;
113
114 /* Obstack for storing file names.  */
115 static struct obstack file_names;
116
117 /* Wrapup input stack.  */
118 static struct obstack *wrapup_stack;
119
120 /* Current stack, from input or wrapup.  */
121 static struct obstack *current_input;
122
123 /* Bottom of token_stack, for obstack_free.  */
124 static void *token_bottom;
125
126 /* Pointer to top of current_input.  */
127 static input_block *isp;
128
129 /* Pointer to top of wrapup_stack.  */
130 static input_block *wsp;
131
132 /* Aux. for handling split push_string ().  */
133 static input_block *next;
134
135 /* Flag for next_char () to increment current_line.  */
136 static bool start_of_input_line;
137
138 /* Flag for next_char () to recognize change in input block.  */
139 static bool input_change;
140
141 #define CHAR_EOF        256     /* character return on EOF */
142 #define CHAR_MACRO      257     /* character return for MACRO token */
143
144 /* Quote chars.  */
145 STRING rquote;
146 STRING lquote;
147
148 /* Comment chars.  */
149 STRING bcomm;
150 STRING ecomm;
151
152 #ifdef ENABLE_CHANGEWORD
153
154 # define DEFAULT_WORD_REGEXP "[_a-zA-Z][_a-zA-Z0-9]*"
155
156 static struct re_pattern_buffer word_regexp;
157 static int default_word_regexp;
158 static struct re_registers regs;
159
160 #else /* ! ENABLE_CHANGEWORD */
161 # define default_word_regexp 1
162 #endif /* ! ENABLE_CHANGEWORD */
163
164 #ifdef DEBUG_INPUT
165 static const char *token_type_string (token_type);
166 #endif
167 \f
168
169 /*-------------------------------------------------------------------.
170 | push_file () pushes an input file on the input stack, saving the   |
171 | current file name and line number.  If next is non-NULL, this push |
172 | invalidates a call to push_string_init (), whose storage is        |
173 | consequently released.  If CLOSE_WHEN_DONE, then close FP after    |
174 | EOF is detected.                                                   |
175 `-------------------------------------------------------------------*/
176
177 void
178 push_file (FILE *fp, const char *title, bool close_when_done)
179 {
180   input_block *i;
181
182   if (next != NULL)
183     {
184       obstack_free (current_input, next);
185       next = NULL;
186     }
187
188   if (debug_level & DEBUG_TRACE_INPUT)
189     DEBUG_MESSAGE1 ("input read from %s", title);
190
191   i = (input_block *) obstack_alloc (current_input,
192                                      sizeof (struct input_block));
193   i->type = INPUT_FILE;
194   i->file = (char *) obstack_copy0 (&file_names, title, strlen (title));
195   i->line = 1;
196   input_change = true;
197
198   i->u.u_f.fp = fp;
199   i->u.u_f.end = false;
200   i->u.u_f.close = close_when_done;
201   i->u.u_f.advance = start_of_input_line;
202   output_current_line = -1;
203
204   i->prev = isp;
205   isp = i;
206 }
207
208 /*---------------------------------------------------------------.
209 | push_macro () pushes a builtin macro's definition on the input |
210 | stack.  If next is non-NULL, this push invalidates a call to   |
211 | push_string_init (), whose storage is consequently released.   |
212 `---------------------------------------------------------------*/
213
214 void
215 push_macro (builtin_func *func)
216 {
217   input_block *i;
218
219   if (next != NULL)
220     {
221       obstack_free (current_input, next);
222       next = NULL;
223     }
224
225   i = (input_block *) obstack_alloc (current_input,
226                                      sizeof (struct input_block));
227   i->type = INPUT_MACRO;
228   i->file = current_file;
229   i->line = current_line;
230   input_change = true;
231
232   i->u.func = func;
233   i->prev = isp;
234   isp = i;
235 }
236
237 /*------------------------------------------------------------------.
238 | First half of push_string ().  The pointer next points to the new |
239 | input_block.                                                      |
240 `------------------------------------------------------------------*/
241
242 struct obstack *
243 push_string_init (void)
244 {
245   if (next != NULL)
246     {
247       M4ERROR ((warning_status, 0,
248                 "INTERNAL ERROR: recursive push_string!"));
249       abort ();
250     }
251
252   next = (input_block *) obstack_alloc (current_input,
253                                         sizeof (struct input_block));
254   next->type = INPUT_STRING;
255   next->file = current_file;
256   next->line = current_line;
257
258   return current_input;
259 }
260
261 /*-------------------------------------------------------------------.
262 | Last half of push_string ().  If next is now NULL, a call to       |
263 | push_file () has invalidated the previous call to push_string_init |
264 | (), so we just give up.  If the new object is void, we do not push |
265 | it.  The function push_string_finish () returns a pointer to the   |
266 | finished object.  This pointer is only for temporary use, since    |
267 | reading the next token might release the memory used for the       |
268 | object.                                                            |
269 `-------------------------------------------------------------------*/
270
271 const char *
272 push_string_finish (void)
273 {
274   const char *ret = NULL;
275
276   if (next == NULL)
277     return NULL;
278
279   if (obstack_object_size (current_input) > 0)
280     {
281       size_t len = obstack_object_size (current_input);
282       obstack_1grow (current_input, '\0');
283       next->u.u_s.string = (char *) obstack_finish (current_input);
284       next->u.u_s.end = next->u.u_s.string + len;
285       next->prev = isp;
286       isp = next;
287       ret = isp->u.u_s.string; /* for immediate use only */
288       input_change = true;
289     }
290   else
291     obstack_free (current_input, next); /* people might leave garbage on it. */
292   next = NULL;
293   return ret;
294 }
295
296 /*------------------------------------------------------------------.
297 | The function push_wrapup () pushes a string on the wrapup stack.  |
298 | When the normal input stack gets empty, the wrapup stack will     |
299 | become the input stack, and push_string () and push_file () will  |
300 | operate on wrapup_stack.  Push_wrapup should be done as           |
301 | push_string (), but this will suffice, as long as arguments to    |
302 | m4_m4wrap () are moderate in size.                                |
303 `------------------------------------------------------------------*/
304
305 void
306 push_wrapup (const char *s)
307 {
308   size_t len = strlen (s);
309   input_block *i;
310   i = (input_block *) obstack_alloc (wrapup_stack,
311                                      sizeof (struct input_block));
312   i->prev = wsp;
313   i->type = INPUT_STRING;
314   i->file = current_file;
315   i->line = current_line;
316   i->u.u_s.string = (char *) obstack_copy0 (wrapup_stack, s, len);
317   i->u.u_s.end = i->u.u_s.string + len;
318   wsp = i;
319 }
320 \f
321
322 /*-------------------------------------------------------------------.
323 | The function pop_input () pops one level of input sources.  If the |
324 | popped input_block is a file, current_file and current_line are    |
325 | reset to the saved values before the memory for the input_block is |
326 | released.                                                          |
327 `-------------------------------------------------------------------*/
328
329 static void
330 pop_input (void)
331 {
332   input_block *tmp = isp->prev;
333
334   switch (isp->type)
335     {
336     case INPUT_STRING:
337     case INPUT_MACRO:
338       break;
339
340     case INPUT_FILE:
341       if (debug_level & DEBUG_TRACE_INPUT)
342         {
343           if (tmp)
344             DEBUG_MESSAGE2 ("input reverted to %s, line %d",
345                             tmp->file, tmp->line);
346           else
347             DEBUG_MESSAGE ("input exhausted");
348         }
349
350       if (ferror (isp->u.u_f.fp))
351         {
352           M4ERROR ((warning_status, 0, "read error"));
353           if (isp->u.u_f.close)
354             fclose (isp->u.u_f.fp);
355           retcode = EXIT_FAILURE;
356         }
357       else if (isp->u.u_f.close && fclose (isp->u.u_f.fp) == EOF)
358         {
359           M4ERROR ((warning_status, errno, "error reading file"));
360           retcode = EXIT_FAILURE;
361         }
362       start_of_input_line = isp->u.u_f.advance;
363       output_current_line = -1;
364       break;
365
366     default:
367       M4ERROR ((warning_status, 0,
368                 "INTERNAL ERROR: input stack botch in pop_input ()"));
369       abort ();
370     }
371   obstack_free (current_input, isp);
372   next = NULL; /* might be set in push_string_init () */
373
374   isp = tmp;
375   input_change = true;
376 }
377
378 /*-------------------------------------------------------------------.
379 | To switch input over to the wrapup stack, main calls pop_wrapup    |
380 | ().  Since wrapup text can install new wrapup text, pop_wrapup ()  |
381 | returns false when there is no wrapup text on the stack, and true  |
382 | otherwise.                                                         |
383 `-------------------------------------------------------------------*/
384
385 bool
386 pop_wrapup (void)
387 {
388   next = NULL;
389   obstack_free (current_input, NULL);
390   free (current_input);
391
392   if (wsp == NULL)
393     {
394       /* End of the program.  Free all memory even though we are about
395          to exit, since it makes leak detection easier.  */
396       obstack_free (&token_stack, NULL);
397       obstack_free (&file_names, NULL);
398       obstack_free (wrapup_stack, NULL);
399       free (wrapup_stack);
400 #ifdef ENABLE_CHANGEWORD
401       regfree (&word_regexp);
402 #endif /* ENABLE_CHANGEWORD */
403       return false;
404     }
405
406   current_input = wrapup_stack;
407   wrapup_stack = (struct obstack *) xmalloc (sizeof (struct obstack));
408   obstack_init (wrapup_stack);
409
410   isp = wsp;
411   wsp = NULL;
412   input_change = true;
413
414   return true;
415 }
416
417 /*-------------------------------------------------------------------.
418 | When a MACRO token is seen, next_token () uses init_macro_token () |
419 | to retrieve the value of the function pointer.                     |
420 `-------------------------------------------------------------------*/
421
422 static void
423 init_macro_token (token_data *td)
424 {
425   if (isp->type != INPUT_MACRO)
426     {
427       M4ERROR ((warning_status, 0,
428                 "INTERNAL ERROR: bad call to init_macro_token ()"));
429       abort ();
430     }
431
432   TOKEN_DATA_TYPE (td) = TOKEN_FUNC;
433   TOKEN_DATA_FUNC (td) = isp->u.func;
434 }
435 \f
436
437 /*-----------------------------------------------------------------.
438 | Low level input is done a character at a time.  The function     |
439 | peek_input () is used to look at the next character in the input |
440 | stream.  At any given time, it reads from the input_block on the |
441 | top of the current input stack.                                  |
442 `-----------------------------------------------------------------*/
443
444 static int
445 peek_input (void)
446 {
447   int ch;
448   input_block *block = isp;
449
450   while (1)
451     {
452       if (block == NULL)
453         return CHAR_EOF;
454
455       switch (block->type)
456         {
457         case INPUT_STRING:
458           ch = to_uchar (block->u.u_s.string[0]);
459           if (ch != '\0')
460             return ch;
461           break;
462
463         case INPUT_FILE:
464           ch = getc (block->u.u_f.fp);
465           if (ch != EOF)
466             {
467               ungetc (ch, block->u.u_f.fp);
468               return ch;
469             }
470           block->u.u_f.end = true;
471           break;
472
473         case INPUT_MACRO:
474           return CHAR_MACRO;
475
476         default:
477           M4ERROR ((warning_status, 0,
478                     "INTERNAL ERROR: input stack botch in peek_input ()"));
479           abort ();
480         }
481       block = block->prev;
482     }
483 }
484
485 /*-------------------------------------------------------------------.
486 | The function next_char () is used to read and advance the input to |
487 | the next character.  It also manages line numbers for error        |
488 | messages, so they do not get wrong, due to lookahead.  The token   |
489 | consisting of a newline alone is taken as belonging to the line it |
490 | ends, and the current line number is not incremented until the     |
491 | next character is read.  99.9% of all calls will read from a       |
492 | string, so factor that out into a macro for speed.                 |
493 `-------------------------------------------------------------------*/
494
495 #define next_char() \
496   (isp && isp->type == INPUT_STRING && isp->u.u_s.string[0]     \
497    && !input_change                                             \
498    ? to_uchar (*isp->u.u_s.string++)                            \
499    : next_char_1 ())
500
501 static int
502 next_char_1 (void)
503 {
504   int ch;
505
506   while (1)
507     {
508       if (isp == NULL)
509         {
510           current_file = "";
511           current_line = 0;
512           return CHAR_EOF;
513         }
514
515       if (input_change)
516         {
517           current_file = isp->file;
518           current_line = isp->line;
519           input_change = false;
520         }
521
522       switch (isp->type)
523         {
524         case INPUT_STRING:
525           ch = to_uchar (*isp->u.u_s.string++);
526           if (ch != '\0')
527             return ch;
528           break;
529
530         case INPUT_FILE:
531           if (start_of_input_line)
532             {
533               start_of_input_line = false;
534               current_line = ++isp->line;
535             }
536
537           /* If stdin is a terminal, calling getc after peek_input
538              already called it would make the user have to hit ^D
539              twice to quit.  */
540           ch = isp->u.u_f.end ? EOF : getc (isp->u.u_f.fp);
541           if (ch != EOF)
542             {
543               if (ch == '\n')
544                 start_of_input_line = true;
545               return ch;
546             }
547           break;
548
549         case INPUT_MACRO:
550           pop_input (); /* INPUT_MACRO input sources has only one token */
551           return CHAR_MACRO;
552
553         default:
554           M4ERROR ((warning_status, 0,
555                     "INTERNAL ERROR: input stack botch in next_char ()"));
556           abort ();
557         }
558
559       /* End of input source --- pop one level.  */
560       pop_input ();
561     }
562 }
563
564 /*-------------------------------------------------------------------.
565 | skip_line () simply discards all immediately following characters, |
566 | upto the first newline.  It is only used from m4_dnl ().           |
567 `-------------------------------------------------------------------*/
568
569 void
570 skip_line (void)
571 {
572   int ch;
573   const char *file = current_file;
574   int line = current_line;
575
576   while ((ch = next_char ()) != CHAR_EOF && ch != '\n')
577     ;
578   if (ch == CHAR_EOF)
579     /* current_file changed to "" if we see CHAR_EOF, use the
580        previous value we stored earlier.  */
581     M4ERROR_AT_LINE ((warning_status, 0, file, line,
582                       "Warning: end of file treated as newline"));
583   /* On the rare occasion that dnl crosses include file boundaries
584      (either the input file did not end in a newline, or changeword
585      was used), calling next_char can update current_file and
586      current_line, and that update will be undone as we return to
587      expand_macro.  This informs next_char to fix things again.  */
588   if (file != current_file || line != current_line)
589     input_change = true;
590 }
591 \f
592
593 /*------------------------------------------------------------------.
594 | This function is for matching a string against a prefix of the    |
595 | input stream.  If the string matches the input and consume is     |
596 | true, the input is discarded; otherwise any characters read are   |
597 | pushed back again.  The function is used only when multicharacter |
598 | quotes or comment delimiters are used.                            |
599 `------------------------------------------------------------------*/
600
601 static bool
602 match_input (const char *s, bool consume)
603 {
604   int n;                        /* number of characters matched */
605   int ch;                       /* input character */
606   const char *t;
607   bool result = false;
608
609   ch = peek_input ();
610   if (ch != to_uchar (*s))
611     return false;                       /* fail */
612
613   if (s[1] == '\0')
614     {
615       if (consume)
616         next_char ();
617       return true;                      /* short match */
618     }
619
620   next_char ();
621   for (n = 1, t = s++; peek_input () == to_uchar (*s++); )
622     {
623       next_char ();
624       n++;
625       if (*s == '\0')           /* long match */
626         {
627           if (consume)
628             return true;
629           result = true;
630           break;
631         }
632     }
633
634   /* Failed or shouldn't consume, push back input.  */
635   {
636     struct obstack *h = push_string_init ();
637
638     /* `obstack_grow' may be macro evaluating its arg 1 several times. */
639     obstack_grow (h, t, n);
640   }
641   push_string_finish ();
642   return result;
643 }
644
645 /*--------------------------------------------------------------------.
646 | The macro MATCH() is used to match a string S against the input.    |
647 | The first character is handled inline, for speed.  Hopefully, this  |
648 | will not hurt efficiency too much when single character quotes and  |
649 | comment delimiters are used.  If CONSUME, then CH is the result of  |
650 | next_char, and a successful match will discard the matched string.  |
651 | Otherwise, CH is the result of peek_char, and the input stream is   |
652 | effectively unchanged.                                              |
653 `--------------------------------------------------------------------*/
654
655 #define MATCH(ch, s, consume)                                           \
656   (to_uchar ((s)[0]) == (ch)                                            \
657    && (ch) != '\0'                                                      \
658    && ((s)[1] == '\0' || (match_input ((s) + (consume), consume))))
659 \f
660
661 /*--------------------------------------------------------.
662 | Initialize input stacks, and quote/comment characters.  |
663 `--------------------------------------------------------*/
664
665 void
666 input_init (void)
667 {
668   current_file = "";
669   current_line = 0;
670
671   current_input = (struct obstack *) xmalloc (sizeof (struct obstack));
672   obstack_init (current_input);
673   wrapup_stack = (struct obstack *) xmalloc (sizeof (struct obstack));
674   obstack_init (wrapup_stack);
675
676   obstack_init (&file_names);
677
678   /* Allocate an object in the current chunk, so that obstack_free
679      will always work even if the first token parsed spills to a new
680      chunk.  */
681   obstack_init (&token_stack);
682   obstack_alloc (&token_stack, 1);
683   token_bottom = obstack_base (&token_stack);
684
685   isp = NULL;
686   wsp = NULL;
687   next = NULL;
688
689   start_of_input_line = false;
690
691   lquote.string = xstrdup (DEF_LQUOTE);
692   lquote.length = strlen (lquote.string);
693   rquote.string = xstrdup (DEF_RQUOTE);
694   rquote.length = strlen (rquote.string);
695   bcomm.string = xstrdup (DEF_BCOMM);
696   bcomm.length = strlen (bcomm.string);
697   ecomm.string = xstrdup (DEF_ECOMM);
698   ecomm.length = strlen (ecomm.string);
699
700 #ifdef ENABLE_CHANGEWORD
701   set_word_regexp (user_word_regexp);
702 #endif
703 }
704 \f
705
706 /*------------------------------------------------------------------.
707 | Functions for setting quotes and comment delimiters.  Used by     |
708 | m4_changecom () and m4_changequote ().  Pass NULL if the argument |
709 | was not present, to distinguish from an explicit empty string.    |
710 `------------------------------------------------------------------*/
711
712 void
713 set_quotes (const char *lq, const char *rq)
714 {
715   free (lquote.string);
716   free (rquote.string);
717
718   /* POSIX states that with 0 arguments, the default quotes are used.
719      POSIX XCU ERN 112 states that behavior is implementation-defined
720      if there was only one argument, or if there is an empty string in
721      either position when there are two arguments.  We allow an empty
722      left quote to disable quoting, but a non-empty left quote will
723      always create a non-empty right quote.  See the texinfo for what
724      some other implementations do.  */
725   if (!lq)
726     {
727       lq = DEF_LQUOTE;
728       rq = DEF_RQUOTE;
729     }
730   else if (!rq || (*lq && !*rq))
731     rq = DEF_RQUOTE;
732
733   lquote.string = xstrdup (lq);
734   lquote.length = strlen (lquote.string);
735   rquote.string = xstrdup (rq);
736   rquote.length = strlen (rquote.string);
737 }
738
739 void
740 set_comment (const char *bc, const char *ec)
741 {
742   free (bcomm.string);
743   free (ecomm.string);
744
745   /* POSIX requires no arguments to disable comments.  It requires
746      empty arguments to be used as-is, but this is counter to
747      traditional behavior, because a non-null begin and null end makes
748      it impossible to end a comment.  An aardvark has been filed:
749      http://www.opengroup.org/austin/mailarchives/ag-review/msg02168.html
750      This implementation assumes the aardvark will be approved.  See
751      the texinfo for what some other implementations do.  */
752   if (!bc)
753     bc = ec = "";
754   else if (!ec || (*bc && !*ec))
755     ec = DEF_ECOMM;
756
757   bcomm.string = xstrdup (bc);
758   bcomm.length = strlen (bcomm.string);
759   ecomm.string = xstrdup (ec);
760   ecomm.length = strlen (ecomm.string);
761 }
762
763 #ifdef ENABLE_CHANGEWORD
764
765 void
766 set_word_regexp (const char *regexp)
767 {
768   const char *msg;
769   struct re_pattern_buffer new_word_regexp;
770
771   if (!*regexp || STREQ (regexp, DEFAULT_WORD_REGEXP))
772     {
773       default_word_regexp = true;
774       return;
775     }
776
777   /* Dry run to see whether the new expression is compilable.  */
778   init_pattern_buffer (&new_word_regexp, NULL);
779   msg = re_compile_pattern (regexp, strlen (regexp), &new_word_regexp);
780   regfree (&new_word_regexp);
781
782   if (msg != NULL)
783     {
784       M4ERROR ((warning_status, 0,
785                 "bad regular expression `%s': %s", regexp, msg));
786       return;
787     }
788
789   /* If compilation worked, retry using the word_regexp struct.  We
790      can't rely on struct assigns working, so redo the compilation.
791      The fastmap can be reused between compilations, and will be freed
792      by the final regfree.  */
793   if (!word_regexp.fastmap)
794     word_regexp.fastmap = xcharalloc (UCHAR_MAX + 1);
795   msg = re_compile_pattern (regexp, strlen (regexp), &word_regexp);
796   assert (!msg);
797   re_set_registers (&word_regexp, &regs, regs.num_regs, regs.start, regs.end);
798   if (re_compile_fastmap (&word_regexp))
799     assert (false);
800
801   default_word_regexp = false;
802 }
803
804 #endif /* ENABLE_CHANGEWORD */
805 \f
806
807 /*--------------------------------------------------------------------.
808 | Parse and return a single token from the input stream.  A token     |
809 | can either be TOKEN_EOF, if the input_stack is empty; it can be     |
810 | TOKEN_STRING for a quoted string; TOKEN_WORD for something that is  |
811 | a potential macro name; and TOKEN_SIMPLE for any single character   |
812 | that is not a part of any of the previous types.  If LINE is not    |
813 | NULL, set *LINE to the line where the token starts.                 |
814 |                                                                     |
815 | Next_token () return the token type, and passes back a pointer to   |
816 | the token data through TD.  The token text is collected on the      |
817 | obstack token_stack, which never contains more than one token text  |
818 | at a time.  The storage pointed to by the fields in TD is           |
819 | therefore subject to change the next time next_token () is called.  |
820 `--------------------------------------------------------------------*/
821
822 token_type
823 next_token (token_data *td, int *line)
824 {
825   int ch;
826   int quote_level;
827   token_type type;
828 #ifdef ENABLE_CHANGEWORD
829   int startpos;
830   char *orig_text = NULL;
831 #endif
832   const char *file;
833   int dummy;
834
835   obstack_free (&token_stack, token_bottom);
836   if (!line)
837     line = &dummy;
838
839  /* Can't consume character until after CHAR_MACRO is handled.  */
840   ch = peek_input ();
841   if (ch == CHAR_EOF)
842     {
843 #ifdef DEBUG_INPUT
844       xfprintf (stderr, "next_token -> EOF\n");
845 #endif
846       next_char ();
847       return TOKEN_EOF;
848     }
849   if (ch == CHAR_MACRO)
850     {
851       init_macro_token (td);
852       next_char ();
853 #ifdef DEBUG_INPUT
854       xfprintf (stderr, "next_token -> MACDEF (%s)\n",
855                 find_builtin_by_addr (TOKEN_DATA_FUNC (td))->name);
856 #endif
857       return TOKEN_MACDEF;
858     }
859
860   next_char (); /* Consume character we already peeked at.  */
861   file = current_file;
862   *line = current_line;
863   if (MATCH (ch, bcomm.string, true))
864     {
865       obstack_grow (&token_stack, bcomm.string, bcomm.length);
866       while ((ch = next_char ()) != CHAR_EOF
867              && !MATCH (ch, ecomm.string, true))
868         obstack_1grow (&token_stack, ch);
869       if (ch != CHAR_EOF)
870         obstack_grow (&token_stack, ecomm.string, ecomm.length);
871       else
872         /* current_file changed to "" if we see CHAR_EOF, use the
873            previous value we stored earlier.  */
874         M4ERROR_AT_LINE ((EXIT_FAILURE, 0, file, *line,
875                           "ERROR: end of file in comment"));
876
877       type = TOKEN_STRING;
878     }
879   else if (default_word_regexp && (isalpha (ch) || ch == '_'))
880     {
881       obstack_1grow (&token_stack, ch);
882       while ((ch = peek_input ()) != CHAR_EOF && (isalnum (ch) || ch == '_'))
883         {
884           obstack_1grow (&token_stack, ch);
885           next_char ();
886         }
887       type = TOKEN_WORD;
888     }
889
890 #ifdef ENABLE_CHANGEWORD
891
892   else if (!default_word_regexp && word_regexp.fastmap[ch])
893     {
894       obstack_1grow (&token_stack, ch);
895       while (1)
896         {
897           ch = peek_input ();
898           if (ch == CHAR_EOF)
899             break;
900           obstack_1grow (&token_stack, ch);
901           startpos = re_search (&word_regexp,
902                                 (char *) obstack_base (&token_stack),
903                                 obstack_object_size (&token_stack), 0, 0,
904                                 &regs);
905           if (startpos ||
906               regs.end [0] != (regoff_t) obstack_object_size (&token_stack))
907             {
908               *(((char *) obstack_base (&token_stack)
909                  + obstack_object_size (&token_stack)) - 1) = '\0';
910               break;
911             }
912           next_char ();
913         }
914
915       obstack_1grow (&token_stack, '\0');
916       orig_text = (char *) obstack_finish (&token_stack);
917
918       if (regs.start[1] != -1)
919         obstack_grow (&token_stack,orig_text + regs.start[1],
920                       regs.end[1] - regs.start[1]);
921       else
922         obstack_grow (&token_stack, orig_text,regs.end[0]);
923
924       type = TOKEN_WORD;
925     }
926
927 #endif /* ENABLE_CHANGEWORD */
928
929   else if (!MATCH (ch, lquote.string, true))
930     {
931       switch (ch)
932         {
933         case '(':
934           type = TOKEN_OPEN;
935           break;
936         case ',':
937           type = TOKEN_COMMA;
938           break;
939         case ')':
940           type = TOKEN_CLOSE;
941           break;
942         default:
943           type = TOKEN_SIMPLE;
944           break;
945         }
946       obstack_1grow (&token_stack, ch);
947     }
948   else
949     {
950       bool fast = lquote.length == 1 && rquote.length == 1;
951       quote_level = 1;
952       while (1)
953         {
954           /* Try scanning a buffer first.  */
955           const char *buffer = (isp && isp->type == INPUT_STRING
956                                 ? isp->u.u_s.string : NULL);
957           if (buffer && *buffer)
958             {
959               size_t len = isp->u.u_s.end - buffer;
960               const char *p = buffer;
961               do
962                 {
963                   p = (char *) memchr2 (p, *lquote.string, *rquote.string,
964                                         buffer + len - p);
965                 }
966               while (p && fast && (*p++ == *rquote.string
967                                    ? --quote_level : ++quote_level));
968               if (p)
969                 {
970                   if (fast)
971                     {
972                       assert (!quote_level);
973                       obstack_grow (&token_stack, buffer, p - buffer - 1);
974                       isp->u.u_s.string += p - buffer;
975                       break;
976                     }
977                   obstack_grow (&token_stack, buffer, p - buffer);
978                   ch = to_uchar (*p);
979                   isp->u.u_s.string += p - buffer + 1;
980                 }
981               else
982                 {
983                   obstack_grow (&token_stack, buffer, len);
984                   isp->u.u_s.string += len;
985                   continue;
986                 }
987             }
988           /* Fall back to a byte.  */
989           else
990             ch = next_char ();
991           if (ch == CHAR_EOF)
992             /* current_file changed to "" if we see CHAR_EOF, use
993                the previous value we stored earlier.  */
994             M4ERROR_AT_LINE ((EXIT_FAILURE, 0, file, *line,
995                               "ERROR: end of file in string"));
996
997           if (MATCH (ch, rquote.string, true))
998             {
999               if (--quote_level == 0)
1000                 break;
1001               obstack_grow (&token_stack, rquote.string, rquote.length);
1002             }
1003           else if (MATCH (ch, lquote.string, true))
1004             {
1005               quote_level++;
1006               obstack_grow (&token_stack, lquote.string, lquote.length);
1007             }
1008           else
1009             obstack_1grow (&token_stack, ch);
1010         }
1011       type = TOKEN_STRING;
1012     }
1013
1014   obstack_1grow (&token_stack, '\0');
1015
1016   TOKEN_DATA_TYPE (td) = TOKEN_TEXT;
1017   TOKEN_DATA_TEXT (td) = (char *) obstack_finish (&token_stack);
1018 #ifdef ENABLE_CHANGEWORD
1019   if (orig_text == NULL)
1020     orig_text = TOKEN_DATA_TEXT (td);
1021   TOKEN_DATA_ORIG_TEXT (td) = orig_text;
1022 #endif
1023 #ifdef DEBUG_INPUT
1024   xfprintf (stderr, "next_token -> %s (%s)\n",
1025             token_type_string (type), TOKEN_DATA_TEXT (td));
1026 #endif
1027   return type;
1028 }
1029
1030 /*-----------------------------------------------.
1031 | Peek at the next token from the input stream.  |
1032 `-----------------------------------------------*/
1033
1034 token_type
1035 peek_token (void)
1036 {
1037   token_type result;
1038   int ch = peek_input ();
1039
1040   if (ch == CHAR_EOF)
1041     {
1042       result = TOKEN_EOF;
1043     }
1044   else if (ch == CHAR_MACRO)
1045     {
1046       result = TOKEN_MACDEF;
1047     }
1048   else if (MATCH (ch, bcomm.string, false))
1049     {
1050       result = TOKEN_STRING;
1051     }
1052   else if ((default_word_regexp && (isalpha (ch) || ch == '_'))
1053 #ifdef ENABLE_CHANGEWORD
1054            || (! default_word_regexp && word_regexp.fastmap[ch])
1055 #endif /* ENABLE_CHANGEWORD */
1056            )
1057     {
1058       result = TOKEN_WORD;
1059     }
1060   else if (MATCH (ch, lquote.string, false))
1061     {
1062       result = TOKEN_STRING;
1063     }
1064   else
1065     switch (ch)
1066       {
1067       case '(':
1068         result = TOKEN_OPEN;
1069         break;
1070       case ',':
1071         result = TOKEN_COMMA;
1072         break;
1073       case ')':
1074         result = TOKEN_CLOSE;
1075         break;
1076       default:
1077         result = TOKEN_SIMPLE;
1078       }
1079
1080 #ifdef DEBUG_INPUT
1081   xfprintf (stderr, "peek_token -> %s\n", token_type_string (result));
1082 #endif /* DEBUG_INPUT */
1083   return result;
1084 }
1085 \f
1086
1087 #ifdef DEBUG_INPUT
1088
1089 static const char *
1090 token_type_string (token_type t)
1091 {
1092  switch (t)
1093     { /* TOKSW */
1094     case TOKEN_EOF:
1095       return "EOF";
1096     case TOKEN_STRING:
1097       return "STRING";
1098     case TOKEN_WORD:
1099       return "WORD";
1100     case TOKEN_OPEN:
1101       return "OPEN";
1102     case TOKEN_COMMA:
1103       return "COMMA";
1104     case TOKEN_CLOSE:
1105       return "CLOSE";
1106     case TOKEN_SIMPLE:
1107       return "SIMPLE";
1108     case TOKEN_MACDEF:
1109       return "MACDEF";
1110     default:
1111       abort ();
1112     }
1113  }
1114
1115 static void
1116 print_token (const char *s, token_type t, token_data *td)
1117 {
1118   xfprintf (stderr, "%s: ", s);
1119   switch (t)
1120     { /* TOKSW */
1121     case TOKEN_OPEN:
1122     case TOKEN_COMMA:
1123     case TOKEN_CLOSE:
1124     case TOKEN_SIMPLE:
1125       xfprintf (stderr, "char:");
1126       break;
1127
1128     case TOKEN_WORD:
1129       xfprintf (stderr, "word:");
1130       break;
1131
1132     case TOKEN_STRING:
1133       xfprintf (stderr, "string:");
1134       break;
1135
1136     case TOKEN_MACDEF:
1137       xfprintf (stderr, "macro: %p\n", TOKEN_DATA_FUNC (td));
1138       break;
1139
1140     case TOKEN_EOF:
1141       xfprintf (stderr, "eof\n");
1142       break;
1143     }
1144   xfprintf (stderr, "\t\"%s\"\n", TOKEN_DATA_TEXT (td));
1145 }
1146
1147 static void M4_GNUC_UNUSED
1148 lex_debug (void)
1149 {
1150   token_type t;
1151   token_data td;
1152
1153   while ((t = next_token (&td, NULL)) != TOKEN_EOF)
1154     print_token ("lex", t, &td);
1155 }
1156 #endif /* DEBUG_INPUT */