OSDN Git Service

* Makefile.in (c-decl.o): Depends on defaults.h.
[pf3gnuchains/gcc-fork.git] / gcc / cpplib.c
1 /* CPP Library.
2    Copyright (C) 1986, 87, 89, 92-98, 1999 Free Software Foundation, Inc.
3    Contributed by Per Bothner, 1994-95.
4    Based on CCCP program by Paul Rubin, June 1986
5    Adapted to ANSI C, Richard Stallman, Jan 1987
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 This program 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, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "intl.h"
27
28 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
29 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
30
31 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
32 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
33 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
34 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
35 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
36    (Note that it is false while we're expanding macro *arguments*.) */
37 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
38
39 /* Forward declarations.  */
40
41 static char *my_strerror                PROTO ((int));
42 static void validate_else               PROTO ((cpp_reader *, char *));
43 static HOST_WIDEST_INT eval_if_expression       PROTO ((cpp_reader *));
44
45 static void conditional_skip            PROTO ((cpp_reader *, int,
46                                                 enum node_type, U_CHAR *));
47 static void skip_if_group               PROTO ((cpp_reader *));
48
49 static void parse_name                  PARAMS ((cpp_reader *, int));
50 static void parse_string                PARAMS ((cpp_reader *, int));
51 static int parse_assertion              PARAMS ((cpp_reader *));
52
53 /* External declarations.  */
54
55 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
56
57 /* `struct directive' defines one #-directive, including how to handle it.  */
58
59 struct directive {
60   int length;                   /* Length of name */
61   int (*func)                   /* Function to handle directive */
62     PARAMS ((cpp_reader *, struct directive *));
63   char *name;                   /* Name of directive */
64   enum node_type type;          /* Code which describes which directive.  */
65 };
66
67 /* These functions are declared to return int instead of void since they
68    are going to be placed in a table and some old compilers have trouble with
69    pointers to functions returning void.  */
70
71 static int do_define PARAMS ((cpp_reader *, struct directive *));
72 static int do_line PARAMS ((cpp_reader *, struct directive *));
73 static int do_include PARAMS ((cpp_reader *, struct directive *));
74 static int do_undef PARAMS ((cpp_reader *, struct directive *));
75 static int do_error PARAMS ((cpp_reader *, struct directive *));
76 static int do_pragma PARAMS ((cpp_reader *, struct directive *));
77 static int do_ident PARAMS ((cpp_reader *, struct directive *));
78 static int do_if PARAMS ((cpp_reader *, struct directive *));
79 static int do_xifdef PARAMS ((cpp_reader *, struct directive *));
80 static int do_else PARAMS ((cpp_reader *, struct directive *));
81 static int do_elif PARAMS ((cpp_reader *, struct directive *));
82 static int do_endif PARAMS ((cpp_reader *, struct directive *));
83 #ifdef SCCS_DIRECTIVE
84 static int do_sccs PARAMS ((cpp_reader *, struct directive *));
85 #endif
86 static int do_assert PARAMS ((cpp_reader *, struct directive *));
87 static int do_unassert PARAMS ((cpp_reader *, struct directive *));
88 static int do_warning PARAMS ((cpp_reader *, struct directive *));
89
90 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
91 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
92
93 /* Here is the actual list of #-directives, most-often-used first.
94    The initialize_builtins function assumes #define is the very first.  */
95
96 static struct directive directive_table[] = {
97   {  6, do_define,   "define",       T_DEFINE },
98   {  5, do_xifdef,   "ifdef",        T_IFDEF },
99   {  6, do_xifdef,   "ifndef",       T_IFNDEF },
100   {  7, do_include,  "include",      T_INCLUDE },
101   { 12, do_include,  "include_next", T_INCLUDE_NEXT },
102   {  6, do_include,  "import",       T_IMPORT },
103   {  5, do_endif,    "endif",        T_ENDIF },
104   {  4, do_else,     "else",         T_ELSE },
105   {  2, do_if,       "if",           T_IF },
106   {  4, do_elif,     "elif",         T_ELIF },
107   {  5, do_undef,    "undef",        T_UNDEF },
108   {  5, do_error,    "error",        T_ERROR },
109   {  7, do_warning,  "warning",      T_WARNING },
110   {  6, do_pragma,   "pragma",       T_PRAGMA },
111   {  4, do_line,     "line",         T_LINE },
112   {  5, do_ident,    "ident",        T_IDENT },
113 #ifdef SCCS_DIRECTIVE
114   {  4, do_sccs,     "sccs",         T_SCCS },
115 #endif
116   {  6, do_assert,   "assert",       T_ASSERT },
117   {  8, do_unassert, "unassert",     T_UNASSERT },
118   {  -1, 0, "", T_UNUSED }
119 };
120
121 /* Place into PFILE a quoted string representing the string SRC.
122    Caller must reserve enough space in pfile->token_buffer.  */
123
124 void
125 quote_string (pfile, src)
126      cpp_reader *pfile;
127      const char *src;
128 {
129   U_CHAR c;
130
131   CPP_PUTC_Q (pfile, '\"');
132   for (;;)
133     switch ((c = *src++))
134       {
135       default:
136         if (ISPRINT (c))
137           CPP_PUTC_Q (pfile, c);
138         else
139           {
140             sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
141             CPP_ADJUST_WRITTEN (pfile, 4);
142           }
143         break;
144
145       case '\"':
146       case '\\':
147         CPP_PUTC_Q (pfile, '\\');
148         CPP_PUTC_Q (pfile, c);
149         break;
150       
151       case '\0':
152         CPP_PUTC_Q (pfile, '\"');
153         CPP_NUL_TERMINATE_Q (pfile);
154         return;
155       }
156 }
157
158 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars.  */
159
160 void
161 cpp_grow_buffer (pfile, n)
162      cpp_reader *pfile;
163      long n;
164 {
165   long old_written = CPP_WRITTEN (pfile);
166   pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
167   pfile->token_buffer = (U_CHAR *)
168     xrealloc(pfile->token_buffer, pfile->token_buffer_size);
169   CPP_SET_WRITTEN (pfile, old_written);
170 }
171
172 /* Process the string STR as if it appeared as the body of a #define
173    If STR is just an identifier, define it with value 1.
174    If STR has anything after the identifier, then it should
175    be identifier=definition. */
176
177 void
178 cpp_define (pfile, str)
179      cpp_reader *pfile;
180      U_CHAR *str;
181 {
182   U_CHAR *buf, *p;
183   size_t count;
184
185   /* Copy the entire option so we can modify it.  */
186   count = strlen (str) + 3;
187   buf = (U_CHAR *) alloca (count);
188   memcpy (buf, str, count - 2);
189   /* Change the first "=" in the string to a space.  If there is none,
190      tack " 1" on the end. */
191   p = (U_CHAR *) strchr (buf, '=');
192   if (p)
193     {
194       *p = ' ';
195       count -= 2;
196     }
197   else
198       strcpy (&buf[count-3], " 1");
199   
200   if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
201     {
202       do_define (pfile, NULL);
203       cpp_pop_buffer (pfile);
204     }
205 }
206
207 /* Process the string STR as if it appeared as the body of a #assert. */
208 void
209 cpp_assert (pfile, str)
210      cpp_reader *pfile;
211      U_CHAR *str;
212 {
213   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
214     {
215       do_assert (pfile, NULL);
216       cpp_pop_buffer (pfile);
217     }
218 }
219
220
221 static enum cpp_token
222 null_underflow (pfile)
223      cpp_reader *pfile ATTRIBUTE_UNUSED;
224 {
225   return CPP_EOF;
226 }
227
228 static int
229 null_cleanup (pbuf, pfile)
230      cpp_buffer *pbuf ATTRIBUTE_UNUSED;
231      cpp_reader *pfile ATTRIBUTE_UNUSED;
232 {
233   return 0;
234 }
235
236 /* Skip a comment - C, C++, or Chill style.  M is the first character
237    of the comment marker.  If this really is a comment, skip to its
238    end and return ' '.  If we hit end-of-file before end-of-comment,
239    return EOF.  If this is not a comment, return M (which will be
240    '/' or '-').  */
241
242 static int
243 skip_comment (pfile, m)
244      cpp_reader *pfile;
245      int m;
246 {
247   if (m == '/' && PEEKC() == '*')
248     {
249       int c, prev_c = -1;
250       long line, col;
251       
252       FORWARD(1);
253       cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
254       for (;;)
255         {
256           c = GETC ();
257           if (c == EOF)
258             {
259               cpp_error_with_line (pfile, line, col, "unterminated comment");
260               return EOF;
261             }
262           else if (c == '\n' || c == '\r')
263             /* \r cannot be a macro escape marker here. */
264             CPP_BUMP_LINE (pfile);
265           else if (c == '/' && prev_c == '*')
266             return ' ';
267           else if (c == '*' && prev_c == '/'
268                    && CPP_OPTIONS (pfile)->warn_comments)
269             cpp_warning (pfile, "`/*' within comment");
270
271           prev_c = c;
272         }
273     }
274   else if ((m == '/' && PEEKC() == '/'
275             && CPP_OPTIONS (pfile)->cplusplus_comments)
276            || (m == '-' && PEEKC() == '-'
277                && CPP_OPTIONS (pfile)->chill))
278     {
279       FORWARD(1);
280       for (;;)
281         {
282           int c = GETC ();
283           if (c == EOF)
284             return ' '; /* Allow // to be terminated by EOF.  */
285               if (c == '\n')
286                 {
287                   /* Don't consider final '\n' to be part of comment.  */
288                   FORWARD(-1);
289                   return ' ';
290                 }
291               else if (c == '\r')
292                 /* \r cannot be a macro escape marker here. */
293                 CPP_BUMP_LINE (pfile);
294         }
295     }
296   else
297     return m;
298 }
299
300 /* Identical to skip_comment except that it copies the comment into the
301    token_buffer.  This is used if put_out_comments.  */
302 static int
303 copy_comment (pfile, m)
304      cpp_reader *pfile;
305      int m;
306 {
307   if (m == '/' && PEEKC() == '*')
308     {
309       int c, prev_c = -1;
310       long line, col;
311
312       CPP_PUTC (pfile, '/');
313       CPP_PUTC (pfile, '*');
314       FORWARD(1);
315       cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
316       for (;;)
317         {
318           c = GETC ();
319           if (c == EOF)
320             {
321               cpp_error_with_line (pfile, line, col, "unterminated comment");
322               /* We must pretend this was a legitimate comment, so that the
323                  output in token_buffer is not passed back tagged CPP_POP. */
324               return ' ';
325             }
326           else if (c == '\r')
327             {
328               /* \r cannot be a macro escape marker here. */
329               CPP_BUMP_LINE (pfile);
330               continue;
331             }
332
333           CPP_PUTC (pfile, c);
334           if (c == '\n')
335             {
336               pfile->lineno++;
337               CPP_BUMP_LINE (pfile);
338             }
339           else if (c == '/' && prev_c == '*')
340             return ' ';
341           else if (c == '*' && prev_c == '/'
342                    && CPP_OPTIONS (pfile)->warn_comments)
343             cpp_warning (pfile, "`/*' within comment");
344
345           prev_c = c;
346         }
347     }
348   else if ((m == '/' && PEEKC() == '/'
349             && CPP_OPTIONS (pfile)->cplusplus_comments)
350            || (m == '-' && PEEKC() == '-'
351                && CPP_OPTIONS (pfile)->chill))
352     {
353       CPP_PUTC (pfile, m);
354       CPP_PUTC (pfile, m);
355       FORWARD(1);
356       for (;;)
357         {
358           int c = GETC ();
359           if (c == EOF)
360             return ' '; /* Allow line comments to be terminated by EOF. */
361           else if (c == '\n')
362             {
363               /* Don't consider final '\n' to be part of comment.  */
364               FORWARD(-1);
365               return ' ';
366             }
367           else if (c == '\r')
368             /* \r cannot be a macro escape marker here. */
369             CPP_BUMP_LINE (pfile);
370
371           CPP_PUTC (pfile, c);
372         }
373     }
374   else
375     return m;
376 }
377
378
379 /* Skip whitespace \-newline and comments.  Does not macro-expand.  */
380
381 void
382 cpp_skip_hspace (pfile)
383      cpp_reader *pfile;
384 {
385   int c;
386   while (1)
387     {
388       c = GETC();
389       if (c == EOF)
390         return;
391       else if (is_hor_space[c])
392         {
393           if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
394             cpp_pedwarn (pfile, "%s in preprocessing directive",
395                          c == '\f' ? "formfeed" : "vertical tab");
396         }
397       else if (c == '\r')
398         {
399           /* \r is a backslash-newline marker if !has_escapes, and
400              a deletable-whitespace or no-reexpansion marker otherwise. */
401           if (CPP_BUFFER (pfile)->has_escapes)
402             {
403               if (PEEKC() == ' ')
404                 FORWARD(1);
405               else
406                 break;
407             }
408           else
409             CPP_BUFFER (pfile)->lineno++;
410         }
411       else if (c == '/' || c == '-')
412         {
413           c = skip_comment (pfile, c);
414           if (c == EOF)
415             return;
416           else if (c != ' ')
417             break;
418         }
419       else
420         break;
421     }
422   FORWARD(-1);
423 }
424
425 /* Read the rest of the current line.
426    The line is appended to PFILE's output buffer.  */
427
428 static void
429 copy_rest_of_line (pfile)
430      cpp_reader *pfile;
431 {
432   for (;;)
433     {
434       int c = GETC();
435       switch (c)
436         {
437         case '\n':
438           FORWARD(-1);
439         case EOF:
440           CPP_NUL_TERMINATE (pfile);
441           return;
442
443         case '\r':
444           if (CPP_BUFFER (pfile)->has_escapes)
445             break;
446           else
447             {
448               CPP_BUFFER (pfile)->lineno++;
449               continue;
450             }
451         case '\'':
452         case '\"':
453           parse_string (pfile, c);
454           continue;
455         case '/':
456           if (PEEKC() == '*' && CPP_TRADITIONAL (pfile))
457             {
458               CPP_PUTS (pfile, "/**/", 4);
459               skip_comment (pfile, c);
460               continue;
461             }
462           /* else fall through */
463         case '-':
464           c = skip_comment (pfile, c);
465           break;
466
467         case '\f':
468         case '\v':
469           if (CPP_PEDANTIC (pfile))
470             cpp_pedwarn (pfile, "%s in preprocessing directive",
471                          c == '\f' ? "formfeed" : "vertical tab");
472           break;
473
474         }
475       CPP_PUTC (pfile, c);
476     }
477 }
478
479 /* FIXME: It is almost definitely a performance win to make this do
480    the scan itself.  >75% of calls to copy_r_o_l are from here or
481    skip_if_group, which means the common case is to copy stuff into the
482    token_buffer only to discard it.  */
483 void
484 skip_rest_of_line (pfile)
485      cpp_reader *pfile;
486 {
487   long old = CPP_WRITTEN (pfile);
488   copy_rest_of_line (pfile);
489   CPP_SET_WRITTEN (pfile, old);
490 }
491
492 /* Handle a possible # directive.
493    '#' has already been read.  */
494
495 static int
496 handle_directive (pfile)
497      cpp_reader *pfile;
498 {
499   int c;
500   register struct directive *kt;
501   int ident_length;
502   U_CHAR *ident;
503   long old_written = CPP_WRITTEN (pfile);
504
505   cpp_skip_hspace (pfile);
506
507   c = PEEKC ();
508   if (c >= '0' && c <= '9')
509     {
510       /* Handle # followed by a line number.  */
511       if (CPP_PEDANTIC (pfile))
512         cpp_pedwarn (pfile, "`#' followed by integer");
513       do_line (pfile, NULL);
514       return 1;
515     }
516
517   /* Now find the directive name.  */
518   CPP_PUTC (pfile, '#');
519   parse_name (pfile, GETC());
520   ident = pfile->token_buffer + old_written + 1;
521   ident_length = CPP_PWRITTEN (pfile) - ident;
522   if (ident_length == 0)
523     {
524       /* A line of just `#' becomes blank.  */
525       if (PEEKC() == '\n')
526         return 1;
527       else
528         return 0;
529     }
530
531   /*
532    * Decode the keyword and call the appropriate expansion
533    * routine, after moving the input pointer up to the next line.
534    */
535   for (kt = directive_table; ; kt++)
536     {
537       if (kt->length <= 0)
538         return 0;
539       if (kt->length == ident_length
540           && !strncmp (kt->name, ident, ident_length)) 
541         break;
542     }
543
544   CPP_SET_WRITTEN (pfile, old_written);
545   (*kt->func) (pfile, kt);
546
547   return 1;
548 }
549
550 /* Pass a directive through to the output file.
551    BUF points to the contents of the directive, as a contiguous string.
552    LEN is the length of the string pointed to by BUF.
553    KEYWORD is the keyword-table entry for the directive.  */
554
555 static void
556 pass_thru_directive (buf, len, pfile, keyword)
557      U_CHAR *buf;
558      size_t len;
559      cpp_reader *pfile;
560      struct directive *keyword;
561 {
562   register unsigned keyword_length = keyword->length;
563
564   CPP_RESERVE (pfile, 1 + keyword_length + len);
565   CPP_PUTC_Q (pfile, '#');
566   CPP_PUTS_Q (pfile, keyword->name, keyword_length);
567   if (len != 0 && buf[0] != ' ')
568     CPP_PUTC_Q (pfile, ' ');
569   CPP_PUTS_Q (pfile, buf, len);
570 }
571
572 /* Check a purported macro name SYMNAME, and yield its length.
573    ASSERTION is nonzero if this is really for an assertion name.  */
574
575 int
576 check_macro_name (pfile, symname, assertion)
577      cpp_reader *pfile;
578      U_CHAR *symname;
579      int assertion;
580 {
581   U_CHAR *p;
582   int sym_length;
583
584   for (p = symname; is_idchar[*p]; p++)
585     ;
586   sym_length = p - symname;
587   if (sym_length == 0
588       || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
589     cpp_error (pfile,
590                assertion ? "invalid assertion name" : "invalid macro name");
591   else if (!is_idstart[*symname]
592            || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
593     U_CHAR *msg;                        /* what pain...  */
594     msg = (U_CHAR *) alloca (sym_length + 1);
595     bcopy (symname, msg, sym_length);
596     msg[sym_length] = 0;
597     cpp_error (pfile,
598                (assertion
599                 ? "invalid assertion name `%s'"
600                 : "invalid macro name `%s'"),
601                msg);
602   }
603   return sym_length;
604 }
605
606 /* Process a #define command.
607 KEYWORD is the keyword-table entry for #define,
608 or NULL for a "predefined" macro.  */
609
610 static int
611 do_define (pfile, keyword)
612      cpp_reader *pfile;
613      struct directive *keyword;
614 {
615   int hashcode;
616   MACRODEF mdef;
617   HASHNODE *hp;
618   long here;
619   U_CHAR *macro, *buf, *end;
620
621   here = CPP_WRITTEN (pfile);
622   copy_rest_of_line (pfile);
623
624   /* Copy out the line so we can pop the token buffer. */
625   buf = pfile->token_buffer + here;
626   end = CPP_PWRITTEN (pfile);
627   macro = (U_CHAR *) alloca (end - buf + 1);
628   bcopy (buf, macro, end - buf + 1);
629   end = macro + (end - buf);
630
631   CPP_SET_WRITTEN (pfile, here);
632
633   mdef = create_definition (macro, end, pfile, keyword == NULL);
634   if (mdef.defn == 0)
635     return 0;
636
637   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
638
639   if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
640     {
641       int ok = 0;
642       /* Redefining a precompiled key is ok.  */
643       if (hp->type == T_PCSTRING)
644         ok = 1;
645       /* Redefining a macro is ok if the definitions are the same.  */
646       else if (hp->type == T_MACRO)
647         ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
648       /* Redefining a constant is ok with -D.  */
649       else if (hp->type == T_CONST || hp->type == T_STDC)
650         ok = ! CPP_OPTIONS (pfile)->done_initializing;
651       /* Print the warning if it's not ok.  */
652       if (!ok)
653         {
654           cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
655           if (hp->type == T_MACRO)
656             cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
657                                             hp->value.defn->line,
658                         "this is the location of the previous definition");
659         }
660       /* Replace the old definition.  */
661       hp->type = T_MACRO;
662       hp->value.defn = mdef.defn;
663     }
664   else
665     cpp_install (pfile, mdef.symnam, mdef.symlen, T_MACRO,
666                  (char *) mdef.defn, hashcode);
667
668   if (keyword)
669     {
670       if (CPP_OPTIONS (pfile)->debug_output
671           || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
672         dump_definition (pfile, mdef);
673       else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
674         pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
675     }
676
677   return 0;
678 }
679
680
681 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
682    If BUFFER != NULL, then use the LENGTH characters in BUFFER
683    as the new input buffer.
684    Return the new buffer, or NULL on failure.  */
685
686 cpp_buffer *
687 cpp_push_buffer (pfile, buffer, length)
688      cpp_reader *pfile;
689      U_CHAR *buffer;
690      long length;
691 {
692   cpp_buffer *buf = CPP_BUFFER (pfile);
693   cpp_buffer *new;
694   if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
695     {
696       cpp_fatal (pfile, "macro or `#include' recursion too deep");
697       return NULL;
698     }
699
700   new = (cpp_buffer *) xcalloc (sizeof (cpp_buffer), 1);
701
702   new->if_stack = pfile->if_stack;
703   new->cleanup = null_cleanup;
704   new->underflow = null_underflow;
705   new->buf = new->cur = buffer;
706   new->alimit = new->rlimit = buffer + length;
707   new->prev = buf;
708   new->mark = -1;
709
710   CPP_BUFFER (pfile) = new;
711   return new;
712 }
713
714 cpp_buffer *
715 cpp_pop_buffer (pfile)
716      cpp_reader *pfile;
717 {
718   cpp_buffer *buf = CPP_BUFFER (pfile);
719   (*buf->cleanup) (buf, pfile);
720   CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
721   free (buf);
722   pfile->buffer_stack_depth--;
723   return CPP_BUFFER (pfile);
724 }
725
726 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
727    Pop the buffer when done.  */
728
729 void
730 cpp_scan_buffer (pfile)
731      cpp_reader *pfile;
732 {
733   cpp_buffer *buffer = CPP_BUFFER (pfile);
734   for (;;)
735     {
736       enum cpp_token token = cpp_get_token (pfile);
737       if (token == CPP_EOF) /* Should not happen ...  */
738         break;
739       if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
740         {
741           cpp_pop_buffer (pfile);
742           break;
743         }
744     }
745 }
746
747 /*
748  * Rescan a string (which may have escape marks) into pfile's buffer.
749  * Place the result in pfile->token_buffer.
750  *
751  * The input is copied before it is scanned, so it is safe to pass
752  * it something from the token_buffer that will get overwritten
753  * (because it follows CPP_WRITTEN).  This is used by do_include.
754  */
755
756 void
757 cpp_expand_to_buffer (pfile, buf, length)
758      cpp_reader *pfile;
759      U_CHAR *buf;
760      int length;
761 {
762   register cpp_buffer *ip;
763 #if 0
764   cpp_buffer obuf;
765 #endif
766   U_CHAR *buf1;
767 #if 0
768   int odepth = indepth;
769 #endif
770
771   if (length < 0)
772     {
773       cpp_fatal (pfile, "internal error: length < 0 in cpp_expand_to_buffer");
774       return;
775     }
776
777   /* Set up the input on the input stack.  */
778
779   buf1 = (U_CHAR *) alloca (length + 1);
780   memcpy (buf1, buf, length);
781   buf1[length] = 0;
782
783   ip = cpp_push_buffer (pfile, buf1, length);
784   if (ip == NULL)
785     return;
786   ip->has_escapes = 1;
787 #if 0
788   ip->lineno = obuf.lineno = 1;
789 #endif
790
791   /* Scan the input, create the output.  */
792   cpp_scan_buffer (pfile);
793
794   CPP_NUL_TERMINATE (pfile);
795 }
796
797 void
798 cpp_buf_line_and_col (pbuf, linep, colp)
799      register cpp_buffer *pbuf;
800      long *linep, *colp;
801 {
802   if (pbuf)
803     {
804       *linep = pbuf->lineno;
805       if (colp)
806         *colp = pbuf->cur - pbuf->line_base;
807     }
808   else
809     {
810       *linep = 0;
811       if (colp)
812         *colp = 0;
813     }
814 }
815
816 /* Return the cpp_buffer that corresponds to a file (not a macro).  */
817
818 cpp_buffer *
819 cpp_file_buffer (pfile)
820      cpp_reader *pfile;
821 {
822   cpp_buffer *ip = CPP_BUFFER (pfile);
823
824   for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
825     if (ip->fname != NULL)
826       return ip;
827   return NULL;
828 }
829
830 /*
831  * write out a #line command, for instance, after an #include file.
832  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
833  */
834
835 void
836 output_line_command (pfile, file_change)
837      cpp_reader *pfile;
838      enum file_change_code file_change;
839 {
840   long line;
841   cpp_buffer *ip = CPP_BUFFER (pfile);
842
843   if (ip->fname == NULL)
844     return;
845
846   if (CPP_OPTIONS (pfile)->no_line_commands
847       || CPP_OPTIONS (pfile)->no_output)
848     return;
849
850   cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
851
852   /* If the current file has not changed, we omit the #line if it would
853      appear to be a no-op, and we output a few newlines instead
854      if we want to increase the line number by a small amount.
855      We cannot do this if pfile->lineno is zero, because that means we
856      haven't output any line commands yet.  (The very first line command
857      output is a `same_file' command.)  */
858   if (file_change == same_file && pfile->lineno != 0)
859     {
860       if (line == pfile->lineno)
861         return;
862
863       /* If the inherited line number is a little too small,
864          output some newlines instead of a #line command.  */
865       if (line > pfile->lineno && line < pfile->lineno + 8)
866         {
867           CPP_RESERVE (pfile, 20);
868           while (line > pfile->lineno)
869             {
870               CPP_PUTC_Q (pfile, '\n');
871               pfile->lineno++;
872             }
873           return;
874         }
875     }
876
877   CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
878   CPP_PUTS_Q (pfile, "# ", 2);
879
880   sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
881   CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
882
883   quote_string (pfile, ip->nominal_fname); 
884   if (file_change != same_file)
885     {
886       CPP_PUTC_Q (pfile, ' ');
887       CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
888     }
889   /* Tell cc1 if following text comes from a system header file.  */
890   if (ip->system_header_p)
891     {
892       CPP_PUTC_Q (pfile, ' ');
893       CPP_PUTC_Q (pfile, '3');
894     }
895 #ifndef NO_IMPLICIT_EXTERN_C
896   /* Tell cc1plus if following text should be treated as C.  */
897   if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
898     {
899       CPP_PUTC_Q (pfile, ' ');
900       CPP_PUTC_Q (pfile, '4');
901     }
902 #endif
903   CPP_PUTC_Q (pfile, '\n');
904   pfile->lineno = line;
905 }
906
907
908 /* Like cpp_get_token, except that it does not read past end-of-line.
909    Also, horizontal space is skipped, and macros are popped.  */
910
911 static enum cpp_token
912 get_directive_token (pfile)
913      cpp_reader *pfile;
914 {
915   for (;;)
916     {
917       long old_written = CPP_WRITTEN (pfile);
918       enum cpp_token token;
919       cpp_skip_hspace (pfile);
920       if (PEEKC () == '\n')
921           return CPP_VSPACE;
922       token = cpp_get_token (pfile);
923       switch (token)
924       {
925       case CPP_POP:
926           if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
927               return token;
928           /* ... else fall though ...  */
929       case CPP_HSPACE:  case CPP_COMMENT:
930           CPP_SET_WRITTEN (pfile, old_written);
931           break;
932       default:
933           return token;
934       }
935     }
936 }
937 \f
938 /* Handle #include and #import.
939    This function expects to see "fname" or <fname> on the input.
940
941    The input is normally in part of the output_buffer following
942    CPP_WRITTEN, and will get overwritten by output_line_command.
943    I.e. in input file specification has been popped by handle_directive.
944    This is safe.  */
945
946 static int
947 do_include (pfile, keyword)
948      cpp_reader *pfile;
949      struct directive *keyword;
950 {
951   int importing = (keyword->type == T_IMPORT);
952   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
953   int angle_brackets = 0;       /* 0 for "...", 1 for <...> */
954   int before;  /* included before? */
955   long flen;
956   unsigned char *ftok;
957   cpp_buffer *fp;
958
959   enum cpp_token token;
960
961   /* Chain of dirs to search */
962   struct include_hash *ihash;
963   struct file_name_list *search_start;
964   
965   long old_written = CPP_WRITTEN (pfile);
966
967   int fd;
968
969   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
970     {
971       if (importing)
972         cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
973       if (skip_dirs)
974         cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
975     }
976
977   if (importing && CPP_OPTIONS (pfile)->warn_import
978       && !CPP_OPTIONS (pfile)->inhibit_warnings
979       && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
980     {
981       pfile->import_warning = 1;
982       cpp_warning (pfile,
983            "#import is obsolete, use an #ifndef wrapper in the header file");
984     }
985
986   pfile->parsing_include_directive++;
987   token = get_directive_token (pfile);
988   pfile->parsing_include_directive--;
989
990   if (token == CPP_STRING)
991     {
992       if (pfile->token_buffer[old_written] == '<')
993         angle_brackets = 1;
994     }
995 #ifdef VMS
996   else if (token == CPP_NAME)
997     {
998       /* Support '#include xyz' like VAX-C.  It is taken as
999          '#include <xyz.h>' and generates a warning.  */
1000       cpp_warning (pfile,
1001                "`#include filename' is obsolete, use `#include <filename.h>'");
1002       angle_brackets = 1;
1003
1004       /* Append the missing `.h' to the name. */
1005       CPP_PUTS (pfile, ".h", 2);
1006     }
1007 #endif
1008   else
1009     {
1010       cpp_error (pfile,
1011                  "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1012       CPP_SET_WRITTEN (pfile, old_written);
1013       skip_rest_of_line (pfile);
1014       return 0;
1015     }
1016
1017   flen = CPP_WRITTEN (pfile) - old_written;
1018   ftok = (unsigned char *) alloca (flen + 1);
1019   memcpy (ftok, pfile->token_buffer + old_written, flen);
1020   ftok[flen] = '\0';
1021
1022   if (get_directive_token (pfile) != CPP_VSPACE)
1023     {
1024       cpp_error (pfile, "junk at end of `#include'");
1025       skip_rest_of_line (pfile);
1026     }
1027
1028   CPP_SET_WRITTEN (pfile, old_written);
1029
1030   if (flen == 0)
1031     {
1032       cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1033       return 0;
1034     }
1035
1036   if (CPP_OPTIONS (pfile)->dump_includes)
1037     pass_thru_directive (ftok,
1038                          flen
1039 #ifdef VMS
1040           - ((token == CPP_NAME) ? 2 : 0)
1041 #endif
1042                          , pfile, keyword);
1043
1044 #ifdef VMS
1045   if (token == CPP_STRING)
1046 #endif
1047     {
1048       ftok++;
1049       flen -= 2;
1050       ftok[flen] = '\0';
1051     }
1052
1053   search_start = 0;
1054
1055   for (fp = CPP_BUFFER (pfile);
1056        fp != CPP_NULL_BUFFER (pfile);
1057        fp = CPP_PREV_BUFFER (fp))
1058     if (fp->fname != NULL)
1059       break;
1060
1061   if (fp == CPP_NULL_BUFFER (pfile))
1062     {
1063       cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
1064       return 0;
1065     }
1066   
1067   /* For #include_next, skip in the search path past the dir in which the
1068      containing file was found.  Treat files specified using an absolute path
1069      as if there are no more directories to search.  Treat the primary source
1070      file like any other included source, but generate a warning.  */
1071   if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1072     {
1073       if (fp->ihash->foundhere != ABSOLUTE_PATH)
1074         search_start = fp->ihash->foundhere->next;
1075     }
1076   else
1077     {
1078       if (skip_dirs)
1079         cpp_warning (pfile, "#include_next in primary source file");
1080       
1081       if (angle_brackets)
1082         search_start = CPP_OPTIONS (pfile)->bracket_include;
1083       else
1084         {
1085           if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1086             {
1087               if (fp)
1088                 search_start = fp->actual_dir;
1089             }
1090           else
1091             search_start = CPP_OPTIONS (pfile)->quote_include;
1092         }
1093     }
1094
1095   if (!search_start)
1096     {
1097       cpp_error (pfile, "No include path in which to find %s", ftok);
1098       return 0;
1099     }
1100
1101   fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1102
1103   if (fd == -2)
1104     return 0;
1105   
1106   if (fd == -1)
1107     {
1108       if (CPP_OPTIONS (pfile)->print_deps_missing_files
1109           && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1110                                        (pfile->system_include_depth > 0)))
1111         {
1112           if (!angle_brackets)
1113             deps_output (pfile, ftok, ' ');
1114           else
1115             {
1116               char *p;
1117               struct file_name_list *ptr;
1118               /* If requested as a system header, assume it belongs in
1119                  the first system header directory. */
1120               if (CPP_OPTIONS (pfile)->bracket_include)
1121                 ptr = CPP_OPTIONS (pfile)->bracket_include;
1122               else
1123                 ptr = CPP_OPTIONS (pfile)->quote_include;
1124
1125               p = (char *) alloca (strlen (ptr->name)
1126                                    + strlen (ftok) + 2);
1127               if (*ptr->name != '\0')
1128                 {
1129                   strcpy (p, ptr->name);
1130                   strcat (p, "/");
1131                 }
1132               strcat (p, ftok);
1133               deps_output (pfile, p, ' ');
1134             }
1135         }
1136       /* If -M was specified, and this header file won't be added to
1137          the dependency list, then don't count this as an error,
1138          because we can still produce correct output.  Otherwise, we
1139          can't produce correct output, because there may be
1140          dependencies we need inside the missing file, and we don't
1141          know what directory this missing file exists in. */
1142       else if (CPP_PRINT_DEPS (pfile)
1143                && (CPP_PRINT_DEPS (pfile)
1144                    <= (angle_brackets || (pfile->system_include_depth > 0))))
1145         cpp_warning (pfile, "No include path in which to find %s", ftok);
1146       else
1147         cpp_error_from_errno (pfile, ftok);
1148
1149       return 0;
1150     }
1151
1152   /* For -M, add the file to the dependencies on its first inclusion. */
1153   if (!before && (CPP_PRINT_DEPS (pfile)
1154                   > (angle_brackets || (pfile->system_include_depth > 0))))
1155     deps_output (pfile, ihash->name, ' ');
1156
1157   /* Handle -H option.  */
1158   if (CPP_OPTIONS(pfile)->print_include_names)
1159     {
1160       fp = CPP_BUFFER (pfile);
1161       while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1162         putc ('.', stderr);
1163       fprintf (stderr, " %s\n", ihash->name);
1164     }
1165
1166   /* Actually process the file */
1167
1168   if (importing)
1169     ihash->control_macro = "";
1170   
1171   if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1172     {
1173       close (fd);
1174       return 0;
1175     }
1176   
1177   if (angle_brackets)
1178     pfile->system_include_depth++;   /* Decremented in file_cleanup. */
1179
1180   if (finclude (pfile, fd, ihash))
1181     {
1182       output_line_command (pfile, enter_file);
1183       pfile->only_seen_white = 2;
1184     }
1185
1186   return 0;
1187 }
1188
1189 /* Interpret #line command.
1190    Note that the filename string (if any) is treated as if it were an
1191    include filename.  That means no escape handling.  */
1192
1193 static int
1194 do_line (pfile, keyword)
1195      cpp_reader *pfile;
1196      struct directive *keyword ATTRIBUTE_UNUSED;
1197 {
1198   cpp_buffer *ip = CPP_BUFFER (pfile);
1199   int new_lineno;
1200   long old_written = CPP_WRITTEN (pfile);
1201   enum file_change_code file_change = same_file;
1202   enum cpp_token token;
1203   char *x;
1204
1205   token = get_directive_token (pfile);
1206
1207   if (token != CPP_NUMBER)
1208     {
1209       cpp_error (pfile, "token after `#line' is not an integer");
1210       goto bad_line_directive;
1211     }
1212
1213   new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1214   if (x[0] != '\0')
1215     {
1216       cpp_error (pfile, "token after `#line' is not an integer");
1217       goto bad_line_directive;
1218     }      
1219   CPP_SET_WRITTEN (pfile, old_written);
1220
1221   if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1222     cpp_pedwarn (pfile, "line number out of range in `#line' command");
1223
1224   token = get_directive_token (pfile);
1225
1226   if (token == CPP_STRING)
1227     {
1228       U_CHAR *fname = pfile->token_buffer + old_written + 1;
1229       U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1230       long num_start = CPP_WRITTEN (pfile);
1231
1232       token = get_directive_token (pfile);
1233       if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1234         {
1235           U_CHAR *p = pfile->token_buffer + num_start;
1236           if (CPP_PEDANTIC (pfile))
1237             cpp_pedwarn (pfile, "garbage at end of `#line' command");
1238
1239           if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
1240             {
1241               cpp_error (pfile, "invalid format `#line' command");
1242               goto bad_line_directive;
1243             }
1244           if (*p == '1')
1245             file_change = enter_file;
1246           else if (*p == '2')
1247             file_change = leave_file;
1248           else if (*p == '3')
1249             ip->system_header_p = 1;
1250           else /* if (*p == '4') */
1251             ip->system_header_p = 2;
1252
1253           CPP_SET_WRITTEN (pfile, num_start);
1254           token = get_directive_token (pfile);
1255           p = pfile->token_buffer + num_start;
1256           if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4'))
1257             {
1258               ip->system_header_p = *p == '3' ? 1 : 2;
1259               token = get_directive_token (pfile);
1260             }
1261           if (token != CPP_VSPACE)
1262             {
1263               cpp_error (pfile, "invalid format `#line' command");
1264               goto bad_line_directive;
1265             }
1266         }
1267       
1268       *end_name = '\0';
1269       
1270       if (strcmp (fname, ip->nominal_fname))
1271         {
1272           char *newname, *oldname;
1273           if (!strcmp (fname, ip->fname))
1274             newname = ip->fname;
1275           else if (ip->last_nominal_fname
1276                    && !strcmp (fname, ip->last_nominal_fname))
1277             newname = ip->last_nominal_fname;
1278           else
1279             newname = xstrdup (fname);
1280
1281           oldname = ip->nominal_fname;
1282           ip->nominal_fname = newname;
1283
1284           if (ip->last_nominal_fname
1285               && ip->last_nominal_fname != oldname
1286               && ip->last_nominal_fname != newname
1287               && ip->last_nominal_fname != ip->fname)
1288             free (ip->last_nominal_fname);
1289
1290           if (newname == ip->fname)
1291             ip->last_nominal_fname = NULL;
1292           else
1293             ip->last_nominal_fname = oldname;
1294         } 
1295     }
1296   else if (token != CPP_VSPACE && token != CPP_EOF)
1297     {
1298       cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1299       goto bad_line_directive;
1300     }
1301
1302   /* The Newline at the end of this line remains to be processed.
1303      To put the next line at the specified line number,
1304      we must store a line number now that is one less.  */
1305   ip->lineno = new_lineno - 1;
1306   CPP_SET_WRITTEN (pfile, old_written);
1307   output_line_command (pfile, file_change);
1308   return 0;
1309
1310  bad_line_directive:
1311   skip_rest_of_line (pfile);
1312   CPP_SET_WRITTEN (pfile, old_written);
1313   return 0;
1314 }
1315
1316 /* Remove the definition of a symbol from the symbol table.
1317    According to the C standard, it is not an error to undef
1318    something that has no definitions. */
1319 static int
1320 do_undef (pfile, keyword)
1321      cpp_reader *pfile;
1322      struct directive *keyword;
1323 {
1324   int sym_length;
1325   HASHNODE *hp;
1326   U_CHAR *buf, *name, *limit;
1327   int c;
1328   long here = CPP_WRITTEN (pfile);
1329   enum cpp_token token;
1330
1331   cpp_skip_hspace (pfile);
1332   c = GETC();
1333   if (! is_idstart[c])
1334   {
1335       cpp_error (pfile, "token after #undef is not an identifier");
1336       skip_rest_of_line (pfile);
1337       return 1;
1338   }
1339
1340   parse_name (pfile, c);
1341   buf = pfile->token_buffer + here;
1342   limit = CPP_PWRITTEN(pfile);
1343
1344   /* Copy out the token so we can pop the token buffer. */
1345   name = (U_CHAR *) alloca (limit - buf + 1);
1346   bcopy(buf, name, limit - buf);
1347   name[limit - buf] = '\0';
1348
1349   token = get_directive_token (pfile);
1350   if (token != CPP_VSPACE && token != CPP_POP)
1351   {
1352       cpp_pedwarn (pfile, "junk on line after #undef");
1353       skip_rest_of_line (pfile);
1354   }
1355
1356   CPP_SET_WRITTEN (pfile, here);
1357
1358   sym_length = check_macro_name (pfile, buf, 0);
1359
1360   while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1361     {
1362       /* If we are generating additional info for debugging (with -g) we
1363          need to pass through all effective #undef commands.  */
1364       if (CPP_OPTIONS (pfile)->debug_output && keyword)
1365         pass_thru_directive (name, sym_length, pfile, keyword);
1366       if (hp->type != T_MACRO)
1367         cpp_warning (pfile, "undefining `%s'", hp->name);
1368       delete_macro (hp);
1369     }
1370
1371   return 0;
1372 }
1373
1374 /* Wrap do_undef for -U processing. */
1375 void
1376 cpp_undef (pfile, macro)
1377      cpp_reader *pfile;
1378      U_CHAR *macro;
1379 {
1380   if (cpp_push_buffer (pfile, macro, strlen (macro)))
1381     {
1382       do_undef (pfile, NULL);
1383       cpp_pop_buffer (pfile);
1384     }
1385 }
1386
1387 \f
1388 /*
1389  * Report an error detected by the program we are processing.
1390  * Use the text of the line in the error message.
1391  * (We use error because it prints the filename & line#.)
1392  */
1393
1394 static int
1395 do_error (pfile, keyword)
1396      cpp_reader *pfile;
1397      struct directive *keyword ATTRIBUTE_UNUSED;
1398 {
1399   long here = CPP_WRITTEN (pfile);
1400   U_CHAR *text;
1401   copy_rest_of_line (pfile);
1402   text = pfile->token_buffer + here;
1403   SKIP_WHITE_SPACE(text);
1404
1405   cpp_error (pfile, "#error %s", text);
1406   CPP_SET_WRITTEN (pfile, here);
1407   return 0;
1408 }
1409
1410 /*
1411  * Report a warning detected by the program we are processing.
1412  * Use the text of the line in the warning message, then continue.
1413  */
1414
1415 static int
1416 do_warning (pfile, keyword)
1417      cpp_reader *pfile;
1418      struct directive *keyword ATTRIBUTE_UNUSED;
1419 {
1420   U_CHAR *text;
1421   long here = CPP_WRITTEN(pfile);
1422   copy_rest_of_line (pfile);
1423   text = pfile->token_buffer + here;
1424   SKIP_WHITE_SPACE(text);
1425
1426   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1427     cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1428
1429   /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1430      if -pedantic-errors is given, #warning should cause an error.  */
1431   cpp_pedwarn (pfile, "#warning %s", text);
1432   CPP_SET_WRITTEN (pfile, here);
1433   return 0;
1434 }
1435
1436 /* Report program identification.
1437    This is not precisely what cccp does with #ident, however I believe
1438    it matches `closely enough' (behavior is identical as long as there
1439    are no macros on the #ident line, which is pathological in my opinion).  */
1440
1441 static int
1442 do_ident (pfile, keyword)
1443      cpp_reader *pfile;
1444      struct directive *keyword ATTRIBUTE_UNUSED;
1445 {
1446   /* Allow #ident in system headers, since that's not user's fault.  */
1447   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1448     cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1449
1450   CPP_PUTS (pfile, "#ident ", 7);
1451   cpp_skip_hspace (pfile);
1452   copy_rest_of_line (pfile);
1453
1454   return 0;
1455 }
1456
1457 /* Just check for some recognized pragmas that need validation here,
1458    and leave the text in the token buffer to be output. */
1459
1460 static int
1461 do_pragma (pfile, keyword)
1462      cpp_reader *pfile;
1463      struct directive *keyword ATTRIBUTE_UNUSED;
1464 {
1465   long here;
1466   U_CHAR *buf;
1467
1468   CPP_PUTS (pfile, "#pragma ", 8);
1469   cpp_skip_hspace (pfile);
1470   
1471   here = CPP_WRITTEN (pfile);
1472   copy_rest_of_line (pfile);
1473   buf = pfile->token_buffer + here;
1474   
1475   if (!strncmp (buf, "once", 4))
1476     {
1477       cpp_buffer *ip = NULL;
1478
1479       /* Allow #pragma once in system headers, since that's not the user's
1480          fault.  */
1481       if (!CPP_BUFFER (pfile)->system_header_p)
1482         cpp_warning (pfile, "`#pragma once' is obsolete");
1483       
1484       for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1485         {
1486           if (ip == CPP_NULL_BUFFER (pfile))
1487             return 0;
1488           if (ip->fname != NULL)
1489             break;
1490         }
1491
1492       if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1493         cpp_warning (pfile, "`#pragma once' outside include file");
1494       else
1495         ip->ihash->control_macro = "";  /* never repeat */
1496     }
1497   else if (!strncmp (buf, "implementation", 14))
1498     {
1499       /* Be quiet about `#pragma implementation' for a file only if it hasn't
1500          been included yet.  */
1501       struct include_hash *ptr;
1502       U_CHAR *p = buf + 14, *fname, *fcopy;
1503       SKIP_WHITE_SPACE (p);
1504       if (*p == '\n' || *p != '\"')
1505         return 0;
1506
1507       fname = p + 1;
1508       p = (U_CHAR *) index (fname, '\"');
1509
1510       fcopy = (U_CHAR *) alloca (p - fname + 1);
1511       bcopy (fname, fcopy, p - fname);
1512       fcopy[p-fname] = '\0';
1513
1514       ptr = include_hash (pfile, fcopy, 0);
1515       if (ptr)
1516         cpp_warning (pfile,
1517           "`#pragma implementation' for `%s' appears after file is included",
1518                      fcopy);
1519     }
1520
1521   return 0;
1522 }
1523
1524 #ifdef SCCS_DIRECTIVE
1525 /* Just ignore #sccs, on systems where we define it at all.  */
1526
1527 static int
1528 do_sccs (pfile, keyword)
1529      cpp_reader *pfile;
1530      struct directive *keyword ATTRIBUTE_UNUSED;
1531 {
1532   if (CPP_PEDANTIC (pfile))
1533     cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1534   skip_rest_of_line (pfile);
1535   return 0;
1536 }
1537 #endif
1538 \f
1539 /*
1540  * handle #if command by
1541  *   1) inserting special `defined' keyword into the hash table
1542  *      that gets turned into 0 or 1 by special_symbol (thus,
1543  *      if the luser has a symbol called `defined' already, it won't
1544  *      work inside the #if command)
1545  *   2) rescan the input into a temporary output buffer
1546  *   3) pass the output buffer to the yacc parser and collect a value
1547  *   4) clean up the mess left from steps 1 and 2.
1548  *   5) call conditional_skip to skip til the next #endif (etc.),
1549  *      or not, depending on the value from step 3.
1550  */
1551
1552 static int
1553 do_if (pfile, keyword)
1554      cpp_reader *pfile;
1555      struct directive *keyword ATTRIBUTE_UNUSED;
1556 {
1557   HOST_WIDEST_INT value = eval_if_expression (pfile);
1558   conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
1559   return 0;
1560 }
1561
1562 /*
1563  * handle a #elif directive by not changing  if_stack  either.
1564  * see the comment above do_else.
1565  */
1566
1567 static int
1568 do_elif (pfile, keyword)
1569      cpp_reader *pfile;
1570      struct directive *keyword ATTRIBUTE_UNUSED;
1571 {
1572   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1573     cpp_error (pfile, "`#elif' not within a conditional");
1574     return 0;
1575   } else {
1576     if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1577       cpp_error (pfile, "`#elif' after `#else'");
1578 #if 0
1579       fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1580 #endif
1581       if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
1582           && strcmp (pfile->if_stack->fname,
1583                      CPP_BUFFER (pfile)->nominal_fname) != 0)
1584         fprintf (stderr, ", file %s", pfile->if_stack->fname);
1585       fprintf (stderr, ")\n");
1586     }
1587     pfile->if_stack->type = T_ELIF;
1588   }
1589
1590   if (pfile->if_stack->if_succeeded)
1591     skip_if_group (pfile);
1592   else {
1593     HOST_WIDEST_INT value = eval_if_expression (pfile);
1594     if (value == 0)
1595       skip_if_group (pfile);
1596     else {
1597       ++pfile->if_stack->if_succeeded;  /* continue processing input */
1598       output_line_command (pfile, same_file);
1599     }
1600   }
1601   return 0;
1602 }
1603
1604 /*
1605  * evaluate a #if expression in BUF, of length LENGTH,
1606  * then parse the result as a C expression and return the value as an int.
1607  */
1608
1609 static HOST_WIDEST_INT
1610 eval_if_expression (pfile)
1611      cpp_reader *pfile;
1612 {
1613   HOST_WIDEST_INT value;
1614   long old_written = CPP_WRITTEN (pfile);
1615
1616   pfile->pcp_inside_if = 1;
1617   value = cpp_parse_expr (pfile);
1618   pfile->pcp_inside_if = 0;
1619
1620   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1621
1622   return value;
1623 }
1624
1625 /*
1626  * routine to handle ifdef/ifndef.  Try to look up the symbol,
1627  * then do or don't skip to the #endif/#else/#elif depending
1628  * on what directive is actually being processed.
1629  */
1630
1631 static int
1632 do_xifdef (pfile, keyword)
1633      cpp_reader *pfile;
1634      struct directive *keyword;
1635 {
1636   int skip;
1637   cpp_buffer *ip = CPP_BUFFER (pfile);
1638   U_CHAR *ident;
1639   int ident_length;
1640   enum cpp_token token;
1641   int start_of_file = 0;
1642   U_CHAR *control_macro = 0;
1643   int old_written = CPP_WRITTEN (pfile);
1644
1645   /* Detect a #ifndef at start of file (not counting comments).  */
1646   if (ip->fname != 0 && keyword->type == T_IFNDEF)
1647     start_of_file = pfile->only_seen_white == 2;
1648
1649   pfile->no_macro_expand++;
1650   token = get_directive_token (pfile);
1651   pfile->no_macro_expand--;
1652
1653   ident = pfile->token_buffer + old_written;
1654   ident_length = CPP_WRITTEN (pfile) - old_written;
1655   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1656
1657   if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1658     {
1659       skip = (keyword->type == T_IFDEF);
1660       if (! CPP_TRADITIONAL (pfile))
1661         cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1662     }
1663   else if (token == CPP_NAME)
1664     {
1665       HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1666       skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1667       if (start_of_file && !skip)
1668         {
1669           control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1670           bcopy (ident, control_macro, ident_length + 1);
1671         }
1672     }
1673   else
1674     {
1675       skip = (keyword->type == T_IFDEF);
1676       if (! CPP_TRADITIONAL (pfile))
1677         cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1678     }
1679
1680   if (!CPP_TRADITIONAL (pfile))
1681     { int c;
1682       cpp_skip_hspace (pfile);
1683       c = PEEKC ();
1684       if (c != EOF && c != '\n')
1685         cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1686     }
1687   skip_rest_of_line (pfile);
1688
1689 #if 0
1690     if (pcp_outfile) {
1691       /* Output a precondition for this macro.  */
1692       if (hp && hp->value.defn->predefined)
1693         fprintf (pcp_outfile, "#define %s\n", hp->name);
1694       else {
1695         U_CHAR *cp = buf;
1696         fprintf (pcp_outfile, "#undef ");
1697         while (is_idchar[*cp]) /* Ick! */
1698           fputc (*cp++, pcp_outfile);
1699         putc ('\n', pcp_outfile);
1700       }
1701 #endif
1702
1703   conditional_skip (pfile, skip, T_IF, control_macro);
1704   return 0;
1705 }
1706
1707 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1708    If this is a #ifndef starting at the beginning of a file,
1709    CONTROL_MACRO is the macro name tested by the #ifndef.
1710    Otherwise, CONTROL_MACRO is 0.  */
1711
1712 static void
1713 conditional_skip (pfile, skip, type, control_macro)
1714      cpp_reader *pfile;
1715      int skip;
1716      enum node_type type;
1717      U_CHAR *control_macro;
1718 {
1719   IF_STACK_FRAME *temp;
1720
1721   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1722   temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1723 #if 0
1724   temp->lineno = CPP_BUFFER (pfile)->lineno;
1725 #endif
1726   temp->next = pfile->if_stack;
1727   temp->control_macro = control_macro;
1728   pfile->if_stack = temp;
1729
1730   pfile->if_stack->type = type;
1731
1732   if (skip != 0) {
1733     skip_if_group (pfile);
1734     return;
1735   } else {
1736     ++pfile->if_stack->if_succeeded;
1737     output_line_command (pfile, same_file);
1738   }
1739 }
1740
1741 /* Subroutine of skip_if_group.  Examine one preprocessing directive and
1742    return 0 if skipping should continue, 1 if it should halt.  Also
1743    adjusts the if_stack as appropriate.
1744    The `#' has been read, but not the identifier. */
1745
1746 static int
1747 consider_directive_while_skipping (pfile, stack)
1748     cpp_reader *pfile;
1749     IF_STACK_FRAME *stack; 
1750 {
1751   long ident_len, ident;
1752   struct directive *kt;
1753   IF_STACK_FRAME *temp;
1754     
1755   cpp_skip_hspace (pfile);
1756
1757   ident = CPP_WRITTEN (pfile);
1758   parse_name (pfile, GETC());
1759   ident_len = CPP_WRITTEN (pfile) - ident;
1760
1761   CPP_SET_WRITTEN (pfile, ident);
1762
1763   for (kt = directive_table; kt->length >= 0; kt++)
1764     if (kt->length == ident_len
1765         && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1766       switch (kt->type)
1767         {
1768         case T_IF:
1769         case T_IFDEF:
1770         case T_IFNDEF:
1771             temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
1772             temp->next = pfile->if_stack;
1773             pfile->if_stack = temp;
1774             temp->fname = CPP_BUFFER(pfile)->nominal_fname;
1775             temp->type = kt->type;
1776             return 0;
1777
1778         case T_ELSE:
1779             if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
1780               validate_else (pfile, "#else");
1781             /* fall through */
1782         case T_ELIF:
1783             if (pfile->if_stack->type == T_ELSE)
1784               cpp_error (pfile, "`%s' after `#else'", kt->name);
1785             
1786             if (pfile->if_stack == stack)
1787               return 1;
1788             else
1789               {
1790                 pfile->if_stack->type = kt->type;
1791                 return 0;
1792               }
1793
1794             case T_ENDIF:
1795                 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
1796                   validate_else (pfile, "#endif");
1797
1798                 if (pfile->if_stack == stack)
1799                   return 1;
1800                     
1801                 temp = pfile->if_stack;
1802                 pfile->if_stack = temp->next;
1803                 free (temp);
1804                 return 0;
1805
1806             default:
1807                 return 0;
1808             }
1809
1810     /* Don't let erroneous code go by.  */
1811     if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
1812         cpp_pedwarn (pfile, "invalid preprocessor directive name");
1813     return 0;
1814 }
1815
1816 /* skip to #endif, #else, or #elif.  adjust line numbers, etc.
1817  * leaves input ptr at the sharp sign found.
1818  */
1819 static void
1820 skip_if_group (pfile)
1821     cpp_reader *pfile;
1822 {
1823   int c;
1824   IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
1825   U_CHAR *beg_of_line;
1826   long old_written;
1827
1828   if (CPP_OPTIONS (pfile)->output_conditionals)
1829     {
1830       CPP_PUTS (pfile, "#failed\n", 8);
1831       pfile->lineno++;
1832       output_line_command (pfile, same_file);
1833     }
1834
1835   old_written = CPP_WRITTEN (pfile);
1836   
1837   for (;;)
1838     {
1839       beg_of_line = CPP_BUFFER (pfile)->cur;
1840
1841       if (! CPP_TRADITIONAL (pfile))
1842         cpp_skip_hspace (pfile);
1843       c = GETC();
1844       if (c == '\n')
1845         {
1846           if (CPP_OPTIONS (pfile)->output_conditionals)
1847             CPP_PUTC (pfile, c);
1848           CPP_BUMP_LINE (pfile);
1849           continue;
1850         }
1851       else if (c == '#')
1852         {
1853           if (consider_directive_while_skipping (pfile, save_if_stack))
1854             break;
1855         }
1856       else if (c == EOF)
1857         return;  /* Caller will issue error. */
1858
1859       FORWARD(-1);
1860       if (CPP_OPTIONS (pfile)->output_conditionals)
1861         {
1862           CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
1863           copy_rest_of_line (pfile);
1864         }
1865       else
1866         {
1867           copy_rest_of_line (pfile);
1868           CPP_SET_WRITTEN (pfile, old_written);  /* discard it */
1869         }
1870
1871       c = GETC();
1872       if (c == EOF)
1873         return;  /* Caller will issue error. */
1874       else
1875         {
1876           /* \n */
1877           if (CPP_OPTIONS (pfile)->output_conditionals)
1878             {
1879               CPP_PUTC (pfile, c);
1880               pfile->lineno++;
1881             }
1882           CPP_BUMP_LINE (pfile);
1883         }
1884     }     
1885
1886   /* Back up to the beginning of this line.  Caller will process the
1887      directive. */
1888   CPP_BUFFER (pfile)->cur = beg_of_line;
1889   pfile->only_seen_white = 1;
1890   if (CPP_OPTIONS (pfile)->output_conditionals)
1891     {
1892       CPP_PUTS (pfile, "#endfailed\n", 11);
1893       pfile->lineno++;
1894     }
1895 }
1896
1897 /*
1898  * handle a #else directive.  Do this by just continuing processing
1899  * without changing  if_stack ;  this is so that the error message
1900  * for missing #endif's etc. will point to the original #if.  It
1901  * is possible that something different would be better.
1902  */
1903
1904 static int
1905 do_else (pfile, keyword)
1906      cpp_reader *pfile;
1907      struct directive *keyword ATTRIBUTE_UNUSED;
1908 {
1909   cpp_buffer *ip = CPP_BUFFER (pfile);
1910
1911   if (CPP_PEDANTIC (pfile))
1912     validate_else (pfile, "#else");
1913   skip_rest_of_line (pfile);
1914
1915   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1916     cpp_error (pfile, "`#else' not within a conditional");
1917     return 0;
1918   } else {
1919     /* #ifndef can't have its special treatment for containing the whole file
1920        if it has a #else clause.  */
1921     pfile->if_stack->control_macro = 0;
1922
1923     if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1924       cpp_error (pfile, "`#else' after `#else'");
1925       fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1926       if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
1927         fprintf (stderr, ", file %s", pfile->if_stack->fname);
1928       fprintf (stderr, ")\n");
1929     }
1930     pfile->if_stack->type = T_ELSE;
1931   }
1932
1933   if (pfile->if_stack->if_succeeded)
1934     skip_if_group (pfile);
1935   else {
1936     ++pfile->if_stack->if_succeeded;    /* continue processing input */
1937     output_line_command (pfile, same_file);
1938   }
1939   return 0;
1940 }
1941
1942 /*
1943  * unstack after #endif command
1944  */
1945
1946 static int
1947 do_endif (pfile, keyword)
1948      cpp_reader *pfile;
1949      struct directive *keyword ATTRIBUTE_UNUSED;
1950 {
1951   if (CPP_PEDANTIC (pfile))
1952     validate_else (pfile, "#endif");
1953   skip_rest_of_line (pfile);
1954
1955   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1956     cpp_error (pfile, "unbalanced `#endif'");
1957   else
1958     {
1959       IF_STACK_FRAME *temp = pfile->if_stack;
1960       pfile->if_stack = temp->next;
1961       if (temp->control_macro != 0)
1962         {
1963           /* This #endif matched a #ifndef at the start of the file.
1964              See if it is at the end of the file.  */
1965           int c;
1966
1967           parse_set_mark (pfile);
1968
1969           for (;;)
1970             {
1971               cpp_skip_hspace (pfile);
1972               c = GETC ();
1973               if (c != '\n')
1974                 break;
1975             }
1976           parse_goto_mark (pfile);
1977
1978           if (c == EOF)
1979             {
1980               /* This #endif ends a #ifndef
1981                  that contains all of the file (aside from whitespace).
1982                  Arrange not to include the file again
1983                  if the macro that was tested is defined. */
1984               struct cpp_buffer *ip;
1985               for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1986                 if (ip->fname != NULL)
1987                   break;
1988               ip->ihash->control_macro = (char *) temp->control_macro;
1989             }
1990         }
1991       free (temp);
1992       output_line_command (pfile, same_file);
1993     }
1994   return 0;
1995 }
1996
1997 /* When an #else or #endif is found while skipping failed conditional,
1998    if -pedantic was specified, this is called to warn about text after
1999    the command name.  P points to the first char after the command name.  */
2000
2001 static void
2002 validate_else (pfile, directive)
2003      cpp_reader *pfile;
2004      char *directive;
2005 {
2006   int c;
2007   cpp_skip_hspace (pfile);
2008   c = PEEKC ();
2009   if (c != EOF && c != '\n')
2010     cpp_pedwarn (pfile,
2011                  "text following `%s' violates ANSI standard", directive);
2012 }
2013
2014 /* Get the next token, and add it to the text in pfile->token_buffer.
2015    Return the kind of token we got.  */
2016   
2017 enum cpp_token
2018 cpp_get_token (pfile)
2019      cpp_reader *pfile;
2020 {
2021   register int c, c2, c3;
2022   enum cpp_token token;
2023   struct cpp_options *opts = CPP_OPTIONS (pfile);
2024
2025  get_next:
2026   c = GETC();
2027   if (c == EOF)
2028     {
2029     handle_eof:
2030       if (CPP_BUFFER (pfile)->manual_pop)
2031         /* If we've been reading from redirected input, the
2032            frontend will pop the buffer.  */
2033         return CPP_EOF;
2034       else if (CPP_BUFFER (pfile)->seen_eof)
2035         {
2036           if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2037             return CPP_EOF;
2038
2039           cpp_pop_buffer (pfile);
2040           goto get_next;
2041         }
2042       else
2043         {
2044           cpp_buffer *next_buf
2045             = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2046           CPP_BUFFER (pfile)->seen_eof = 1;
2047           if (CPP_BUFFER (pfile)->nominal_fname
2048               && next_buf != CPP_NULL_BUFFER (pfile))
2049             {
2050               /* We're about to return from an #include file.
2051                  Emit #line information now (as part of the CPP_POP) result.
2052                  But the #line refers to the file we will pop to.  */
2053               cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2054               CPP_BUFFER (pfile) = next_buf;
2055               pfile->input_stack_listing_current = 0;
2056               output_line_command (pfile, leave_file);
2057               CPP_BUFFER (pfile) = cur_buffer;
2058             }
2059           return CPP_POP;
2060         }
2061     }
2062   else
2063     {
2064       switch (c)
2065         {
2066         case '/':
2067           if (PEEKC () == '=')
2068             goto op2;
2069
2070         comment:
2071           if (opts->put_out_comments)
2072             c = copy_comment (pfile, c);
2073           else
2074             c = skip_comment (pfile, c);
2075           if (c == EOF)
2076             goto handle_eof;
2077           else if (c != ' ')
2078             goto randomchar;
2079           
2080           /* Comments are equivalent to spaces.
2081              For -traditional, a comment is equivalent to nothing.  */
2082           if (opts->traditional || opts->put_out_comments)
2083             return CPP_COMMENT;
2084           else
2085             {
2086               CPP_PUTC (pfile, c);
2087               return CPP_HSPACE;
2088             }
2089 #if 0
2090           if (opts->for_lint) {
2091             U_CHAR *argbp;
2092             int cmdlen, arglen;
2093             char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2094             
2095             if (lintcmd != NULL) {
2096               /* I believe it is always safe to emit this newline: */
2097               obp[-1] = '\n';
2098               bcopy ("#pragma lint ", (char *) obp, 13);
2099               obp += 13;
2100               bcopy (lintcmd, (char *) obp, cmdlen);
2101               obp += cmdlen;
2102
2103               if (arglen != 0) {
2104                 *(obp++) = ' ';
2105                 bcopy (argbp, (char *) obp, arglen);
2106                 obp += arglen;
2107               }
2108
2109               /* OK, now bring us back to the state we were in before we entered
2110                  this branch.  We need #line because the newline for the pragma
2111                  could mess things up.  */
2112               output_line_command (pfile, same_file);
2113               *(obp++) = ' ';   /* just in case, if comments are copied thru */
2114               *(obp++) = '/';
2115             }
2116           }
2117 #endif
2118
2119         case '#':
2120 #if 0
2121           /* If this is expanding a macro definition, don't recognize
2122              preprocessor directives.  */
2123           if (ip->macro != 0)
2124             goto randomchar;
2125           /* If this is expand_into_temp_buffer, recognize them
2126              only after an actual newline at this level,
2127              not at the beginning of the input level.  */
2128           if (ip->fname == 0 && beg_of_line == ip->buf)
2129             goto randomchar;
2130           if (ident_length)
2131             goto specialchar;
2132 #endif
2133
2134           if (!pfile->only_seen_white)
2135             goto randomchar;
2136           if (handle_directive (pfile))
2137             return CPP_DIRECTIVE;
2138           pfile->only_seen_white = 0;
2139           return CPP_OTHER;
2140
2141         case '\"':
2142         case '\'':
2143         string:
2144           parse_string (pfile, c);
2145           pfile->only_seen_white = 0;
2146           return c == '\'' ? CPP_CHAR : CPP_STRING;
2147
2148         case '$':
2149           if (!opts->dollars_in_ident)
2150             goto randomchar;
2151           goto letter;
2152
2153         case ':':
2154           if (opts->cplusplus && PEEKC () == ':')
2155             goto op2;
2156           goto randomchar;
2157
2158         case '&':
2159         case '+':
2160         case '|':
2161           c2 = PEEKC ();
2162           if (c2 == c || c2 == '=')
2163             goto op2;
2164           goto randomchar;
2165
2166         case '*':
2167         case '!':
2168         case '%':
2169         case '=':
2170         case '^':
2171           if (PEEKC () == '=')
2172             goto op2;
2173           goto randomchar;
2174
2175         case '-':
2176           c2 = PEEKC ();
2177           if (c2 == '-' && opts->chill)
2178             goto comment;  /* Chill style comment */
2179           if (c2 == '-' || c2 == '=')
2180             goto op2;
2181           if (c2 == '>')
2182             {
2183               if (opts->cplusplus && PEEKN (1) == '*')
2184                 {
2185                   /* In C++, there's a ->* operator.  */
2186                 op3:
2187                   token = CPP_OTHER;
2188                   pfile->only_seen_white = 0;
2189                   CPP_RESERVE (pfile, 4);
2190                   CPP_PUTC_Q (pfile, c);
2191                   CPP_PUTC_Q (pfile, GETC ());
2192                   CPP_PUTC_Q (pfile, GETC ());
2193                   CPP_NUL_TERMINATE_Q (pfile);
2194                   return token;
2195                 }
2196               goto op2;
2197             }
2198           goto randomchar;
2199
2200         case '<':
2201           if (pfile->parsing_include_directive)
2202             {
2203               for (;;)
2204                 {
2205                   CPP_PUTC (pfile, c);
2206                   if (c == '>')
2207                     break;
2208                   c = GETC ();
2209                   if (c == '\n' || c == EOF)
2210                     {
2211                       cpp_error (pfile,
2212                                  "missing '>' in `#include <FILENAME>'");
2213                       break;
2214                     }
2215                   else if (c == '\r')
2216                     {
2217                       if (!CPP_BUFFER (pfile)->has_escapes)
2218                         {
2219                           /* Backslash newline is replaced by nothing. */
2220                           CPP_ADJUST_WRITTEN (pfile, -1);
2221                           CPP_BUMP_LINE (pfile);
2222                         }
2223                       else
2224                         {
2225                           /* We might conceivably get \r- or \r<space> in
2226                              here.  Just delete 'em. */
2227                           int d = GETC();
2228                           if (d != '-' && d != ' ')
2229                             cpp_fatal (pfile,
2230                                   "internal error: unrecognized escape \\r%c",
2231                                        d);
2232                           CPP_ADJUST_WRITTEN (pfile, -1);
2233                         }                         
2234                     }
2235                 }
2236               return CPP_STRING;
2237             }
2238           /* else fall through */
2239         case '>':
2240           c2 = PEEKC ();
2241           if (c2 == '=')
2242             goto op2;
2243           /* GNU C++ supports MIN and MAX operators <? and >?.  */
2244           if (c2 != c && (!opts->cplusplus || c2 != '?'))
2245             goto randomchar;
2246           FORWARD(1);
2247           CPP_RESERVE (pfile, 4);
2248           CPP_PUTC (pfile, c);
2249           CPP_PUTC (pfile, c2);
2250           c3 = PEEKC ();
2251           if (c3 == '=')
2252             CPP_PUTC_Q (pfile, GETC ());
2253           CPP_NUL_TERMINATE_Q (pfile);
2254           pfile->only_seen_white = 0;
2255           return CPP_OTHER;
2256
2257         case '.':
2258           c2 = PEEKC ();
2259           if (ISDIGIT(c2))
2260             {
2261               CPP_RESERVE(pfile, 2);
2262               CPP_PUTC_Q (pfile, '.');
2263               c = GETC ();
2264               goto number;
2265             }
2266
2267           /* In C++ there's a .* operator.  */
2268           if (opts->cplusplus && c2 == '*')
2269             goto op2;
2270
2271           if (c2 == '.' && PEEKN(1) == '.')
2272             {
2273               CPP_RESERVE(pfile, 4);
2274               CPP_PUTC_Q (pfile, '.');
2275               CPP_PUTC_Q (pfile, '.');
2276               CPP_PUTC_Q (pfile, '.');
2277               FORWARD (2);
2278               CPP_NUL_TERMINATE_Q (pfile);
2279               pfile->only_seen_white = 0;
2280               return CPP_3DOTS;
2281             }
2282           goto randomchar;
2283
2284         op2:
2285           token = CPP_OTHER;
2286           pfile->only_seen_white = 0;
2287           CPP_RESERVE(pfile, 3);
2288           CPP_PUTC_Q (pfile, c);
2289           CPP_PUTC_Q (pfile, GETC ());
2290           CPP_NUL_TERMINATE_Q (pfile);
2291           return token;
2292
2293         case 'L':
2294           c2 = PEEKC ();
2295           if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2296             {
2297               CPP_PUTC (pfile, c);
2298               c = GETC ();
2299               goto string;
2300             }
2301           goto letter;
2302
2303         case '0': case '1': case '2': case '3': case '4':
2304         case '5': case '6': case '7': case '8': case '9':
2305         number:
2306           c2  = '.';
2307           for (;;)
2308             {
2309               CPP_RESERVE (pfile, 2);
2310               CPP_PUTC_Q (pfile, c);
2311               c = PEEKC ();
2312               if (c == EOF)
2313                 break;
2314               if (!is_idchar[c] && c != '.'
2315                   && ((c2 != 'e' && c2 != 'E'
2316                        && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2317                       || (c != '+' && c != '-')))
2318                 break;
2319               FORWARD(1);
2320               c2= c;
2321             }
2322           CPP_NUL_TERMINATE_Q (pfile);
2323           pfile->only_seen_white = 0;
2324           return CPP_NUMBER;
2325         case 'b': case 'c': case 'd': case 'h': case 'o':
2326         case 'B': case 'C': case 'D': case 'H': case 'O':
2327           if (opts->chill && PEEKC () == '\'')
2328             {
2329               pfile->only_seen_white = 0;
2330               CPP_RESERVE (pfile, 2);
2331               CPP_PUTC_Q (pfile, c);
2332               CPP_PUTC_Q (pfile, '\'');
2333               FORWARD(1);
2334               for (;;)
2335                 {
2336                   c = GETC();
2337                   if (c == EOF)
2338                     goto chill_number_eof;
2339                   if (!is_idchar[c])
2340                     break;
2341                   CPP_PUTC (pfile, c);
2342                 }
2343               if (c == '\'')
2344                 {
2345                   CPP_RESERVE (pfile, 2);
2346                   CPP_PUTC_Q (pfile, c);
2347                   CPP_NUL_TERMINATE_Q (pfile);
2348                   return CPP_STRING;
2349                 }
2350               else
2351                 {
2352                   FORWARD(-1);
2353                 chill_number_eof:
2354                   CPP_NUL_TERMINATE (pfile);
2355                   return CPP_NUMBER;
2356                 }
2357             }
2358           else
2359             goto letter;
2360         case '_':
2361         case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2362         case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2363         case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2364         case 'x': case 'y': case 'z':
2365         case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2366         case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2367         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2368         case 'Y': case 'Z':
2369         letter:
2370           {
2371             HASHNODE *hp;
2372             unsigned char *ident;
2373             int before_name_written = CPP_WRITTEN (pfile);
2374             int ident_len;
2375             parse_name (pfile, c);
2376             pfile->only_seen_white = 0;
2377             if (pfile->no_macro_expand)
2378               return CPP_NAME;
2379             ident = pfile->token_buffer + before_name_written;
2380             ident_len = CPP_PWRITTEN (pfile) - ident;
2381             hp = cpp_lookup (pfile, ident, ident_len, -1);
2382             if (!hp)
2383               return CPP_NAME;
2384             if (hp->type == T_DISABLED)
2385               {
2386                 if (pfile->output_escapes)
2387                   { /* Return "\r-IDENT", followed by '\0'.  */
2388                     int i;
2389                     CPP_RESERVE (pfile, 3);
2390                     ident = pfile->token_buffer + before_name_written;
2391                     CPP_ADJUST_WRITTEN (pfile, 2);
2392                     for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2393                     ident[0] = '\r';
2394                     ident[1] = '-';
2395                   }
2396                 return CPP_NAME;
2397               }
2398
2399             /* If macro wants an arglist, verify that a '(' follows.
2400                first skip all whitespace, copying it to the output
2401                after the macro name.  Then, if there is no '(',
2402                decide this is not a macro call and leave things that way.  */
2403             if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2404             {
2405               int is_macro_call, macbuf_whitespace = 0;
2406
2407               parse_set_mark (pfile);
2408               for (;;)
2409                 {
2410                   cpp_skip_hspace (pfile);
2411                   c = PEEKC ();
2412                   is_macro_call = c == '(';
2413                   if (c != EOF)
2414                     {
2415                       if (c != '\n')
2416                         break;
2417                       FORWARD (1);
2418                     }
2419                   else
2420                     {
2421                       if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2422                         {
2423                           if (CPP_BUFFER (pfile)->mark !=
2424                               (CPP_BUFFER (pfile)->cur
2425                                - CPP_BUFFER (pfile)->buf))
2426                              macbuf_whitespace = 1;
2427
2428                           /* The mark goes away automatically when
2429                              the buffer is popped. */
2430                           cpp_pop_buffer (pfile);
2431                           parse_set_mark (pfile);
2432                         }
2433                       else
2434                         break;
2435                     }
2436                 }
2437               if (!is_macro_call)
2438                 {
2439                   parse_goto_mark (pfile);
2440                   if (macbuf_whitespace)
2441                     CPP_PUTC (pfile, ' ');
2442                 }
2443               else
2444                 parse_clear_mark (pfile);
2445               if (!is_macro_call)
2446                 return CPP_NAME;
2447             }
2448             /* This is now known to be a macro call.
2449                Expand the macro, reading arguments as needed,
2450                and push the expansion on the input stack.  */
2451             macroexpand (pfile, hp);
2452             CPP_SET_WRITTEN (pfile, before_name_written);
2453           }
2454           goto get_next;
2455
2456         case ' ':  case '\t':  case '\v':
2457           for (;;)
2458             {
2459               CPP_PUTC (pfile, c);
2460               c = PEEKC ();
2461               if (c == EOF || !is_hor_space[c])
2462                 break;
2463               FORWARD(1);
2464             }
2465           return CPP_HSPACE;
2466
2467         case '\r':
2468           if (CPP_BUFFER (pfile)->has_escapes)
2469             {
2470               c = GETC ();
2471               if (c == '-')
2472                 {
2473                   if (pfile->output_escapes)
2474                     CPP_PUTS (pfile, "\r-", 2);
2475                   parse_name (pfile, GETC ());
2476                   return CPP_NAME;
2477                 }
2478               else if (c == ' ')
2479                 {
2480                   CPP_RESERVE (pfile, 2);
2481                   if (pfile->output_escapes)
2482                     CPP_PUTC_Q (pfile, '\r');
2483                   CPP_PUTC_Q (pfile, c);
2484                   return CPP_HSPACE;
2485                 }
2486               else
2487                 {
2488                   cpp_fatal (pfile,
2489                              "internal error: unrecognized escape \\r%c", c);
2490                   goto get_next;
2491                 }
2492             }
2493           else
2494             {
2495               /* Backslash newline is ignored. */
2496               CPP_BUMP_LINE (pfile);
2497               goto get_next;
2498             }
2499
2500         case '\n':
2501           CPP_PUTC (pfile, c);
2502           if (pfile->only_seen_white == 0)
2503             pfile->only_seen_white = 1;
2504           CPP_BUMP_LINE (pfile);
2505           if (! CPP_OPTIONS (pfile)->no_line_commands)
2506             {
2507               pfile->lineno++;
2508               if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2509                 output_line_command (pfile, same_file);
2510             }
2511           return CPP_VSPACE;
2512
2513         case '(': token = CPP_LPAREN;    goto char1;
2514         case ')': token = CPP_RPAREN;    goto char1;
2515         case '{': token = CPP_LBRACE;    goto char1;
2516         case '}': token = CPP_RBRACE;    goto char1;
2517         case ',': token = CPP_COMMA;     goto char1;
2518         case ';': token = CPP_SEMICOLON; goto char1;
2519
2520         randomchar:
2521         default:
2522           token = CPP_OTHER;
2523         char1:
2524           pfile->only_seen_white = 0;
2525           CPP_PUTC (pfile, c);
2526           return token;
2527         }
2528     }
2529 }
2530
2531 /* Like cpp_get_token, but skip spaces and comments.  */
2532
2533 enum cpp_token
2534 cpp_get_non_space_token (pfile)
2535      cpp_reader *pfile;
2536 {
2537   int old_written = CPP_WRITTEN (pfile);
2538   for (;;)
2539     {
2540       enum cpp_token token = cpp_get_token (pfile);
2541       if (token != CPP_COMMENT && token != CPP_POP
2542           && token != CPP_HSPACE && token != CPP_VSPACE)
2543         return token;
2544       CPP_SET_WRITTEN (pfile, old_written);
2545     }
2546 }
2547
2548 /* Parse an identifier starting with C.  */
2549
2550 static void
2551 parse_name (pfile, c)
2552      cpp_reader *pfile;
2553      int c;
2554 {
2555   for (;;)
2556   {
2557       if (! is_idchar[c])
2558       {
2559           FORWARD (-1);
2560           break;
2561       }
2562
2563       if (c == '$' && CPP_PEDANTIC (pfile))
2564         cpp_pedwarn (pfile, "`$' in identifier");
2565
2566       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
2567       CPP_PUTC_Q (pfile, c);
2568       c = GETC();
2569       if (c == EOF)
2570         break;
2571   }
2572   CPP_NUL_TERMINATE_Q (pfile);
2573   return;
2574 }
2575
2576 /* Parse a string starting with C.  A single quoted string is treated
2577    like a double -- some programs (e.g., troff) are perverse this way.
2578    (However, a single quoted string is not allowed to extend over
2579    multiple lines.)  */
2580 static void
2581 parse_string (pfile, c)
2582      cpp_reader *pfile;
2583      int c;
2584 {
2585   long start_line, start_column;
2586   
2587   cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2588
2589   CPP_PUTC (pfile, c);
2590   while (1)
2591     {
2592       int cc = GETC();
2593       if (cc == EOF)
2594         {
2595           if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2596             {
2597               /* try harder: this string crosses a macro expansion
2598                  boundary.  This can happen naturally if -traditional.
2599                  Otherwise, only -D can make a macro with an unmatched
2600                  quote.  */
2601               cpp_pop_buffer (pfile);
2602               continue;
2603             }
2604           if (!CPP_TRADITIONAL (pfile))
2605             {
2606               cpp_error_with_line (pfile, start_line, start_column,
2607                                  "unterminated string or character constant");
2608               if (pfile->multiline_string_line != start_line
2609                   && pfile->multiline_string_line != 0)
2610                 cpp_error_with_line (pfile,
2611                                      pfile->multiline_string_line, -1,
2612                                "possible real start of unterminated constant");
2613               pfile->multiline_string_line = 0;
2614             }
2615           break;
2616         }
2617       CPP_PUTC (pfile, cc);
2618       switch (cc)
2619         {
2620         case '\n':
2621           CPP_BUMP_LINE (pfile);
2622           pfile->lineno++;
2623           /* Traditionally, end of line ends a string constant with
2624              no error.  */
2625           if (CPP_TRADITIONAL (pfile))
2626             return;
2627           /* Character constants may not extend over multiple lines.  */
2628           if (c == '\'')
2629             {
2630               cpp_error_with_line (pfile, start_line, start_column,
2631                                    "unterminated character constant");
2632               return;
2633             }
2634           if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2635             {
2636               cpp_pedwarn_with_line (pfile, start_line, start_column,
2637                                      "string constant runs past end of line");
2638             }
2639           if (pfile->multiline_string_line == 0)
2640             pfile->multiline_string_line = start_line;
2641           break;
2642
2643         case '\r':
2644           CPP_ADJUST_WRITTEN (pfile, -1);
2645           if (CPP_BUFFER (pfile)->has_escapes)
2646             {
2647               cpp_fatal (pfile,
2648                          "internal error: \\r escape inside string constant");
2649               FORWARD(1);
2650             }
2651           else
2652             /* Backslash newline is replaced by nothing at all.  */
2653             CPP_BUMP_LINE (pfile);
2654           break;
2655
2656         case '\\':
2657           cc = GETC();
2658           if (cc != EOF)
2659             CPP_PUTC (pfile, cc);
2660           break;
2661
2662         case '\"':
2663         case '\'':
2664           if (cc == c)
2665             return;
2666           break;
2667         }
2668     }
2669 }
2670
2671 /* Read an assertion into the token buffer, converting to
2672    canonical form: `#predicate(a n swe r)'  The next non-whitespace
2673    character to read should be the first letter of the predicate.
2674    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2675    with answer (see callers for why). In case of 0, an error has been
2676    printed. */
2677 static int
2678 parse_assertion (pfile)
2679      cpp_reader *pfile;
2680 {
2681   int c, dropwhite;
2682   cpp_skip_hspace (pfile);
2683   c = PEEKC();
2684   if (! is_idstart[c])
2685     {
2686       cpp_error (pfile, "assertion predicate is not an identifier");
2687       return 0;
2688     }
2689   CPP_PUTC(pfile, '#');
2690   FORWARD(1);
2691   parse_name(pfile, c);
2692
2693   c = PEEKC();
2694   if (c != '(')
2695     {
2696       if (is_hor_space[c] || c == '\r')
2697         cpp_skip_hspace (pfile);
2698       c = PEEKC();
2699     }
2700   if (c != '(')
2701     return 1;
2702
2703   CPP_PUTC(pfile, '(');
2704   FORWARD(1);
2705   dropwhite = 1;
2706   while ((c = GETC()) != ')')
2707     {
2708       if (is_hor_space[c])
2709         {
2710           if (! dropwhite)
2711             {
2712               CPP_PUTC(pfile, ' ');
2713               dropwhite = 1;
2714             }
2715         }
2716       else if (c == '\n' || c == EOF)
2717         {
2718           if (c == '\n') FORWARD(-1);
2719           cpp_error (pfile, "un-terminated assertion answer");
2720           return 0;
2721         }
2722       else if (c == '\r')
2723         /* \r cannot be a macro escape here. */
2724         CPP_BUMP_LINE (pfile);
2725       else
2726         {
2727           CPP_PUTC (pfile, c);
2728           dropwhite = 0;
2729         }
2730     }
2731
2732   if (pfile->limit[-1] == ' ')
2733     pfile->limit[-1] = ')';
2734   else if (pfile->limit[-1] == '(')
2735     {
2736       cpp_error (pfile, "empty token sequence in assertion");
2737       return 0;
2738     }
2739   else
2740     CPP_PUTC (pfile, ')');
2741
2742   CPP_NUL_TERMINATE (pfile);
2743   return 2;
2744 }
2745
2746 static int
2747 do_assert (pfile, keyword)
2748      cpp_reader *pfile;
2749      struct directive *keyword ATTRIBUTE_UNUSED;
2750 {
2751   char *sym;
2752   int ret, c;
2753   HASHNODE *base, *this;
2754   int baselen, thislen;
2755
2756   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2757       && !CPP_BUFFER (pfile)->system_header_p)
2758     cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
2759
2760   cpp_skip_hspace (pfile);
2761   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
2762   ret = parse_assertion (pfile);
2763   if (ret == 0)
2764     goto error;
2765   else if (ret == 1)
2766     {
2767       cpp_error (pfile, "missing token-sequence in `#assert'");
2768       goto error;
2769     }
2770
2771   cpp_skip_hspace (pfile);
2772   c = PEEKC();
2773   if (c != EOF && c != '\n')
2774     {
2775       cpp_error (pfile, "junk at end of `#assert'");
2776       goto error;
2777     }
2778
2779   thislen = strlen (sym);
2780   baselen = index (sym, '(') - sym;
2781   this = cpp_lookup (pfile, sym, thislen, -1);
2782   if (this)
2783     {
2784       cpp_warning (pfile, "`%s' re-asserted", sym);
2785       goto error;
2786     }
2787
2788   base = cpp_lookup (pfile, sym, baselen, -1);
2789   if (! base)
2790     base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
2791   else if (base->type != T_ASSERT)
2792   {
2793     /* Token clash - but with what?! */
2794     cpp_fatal (pfile,
2795                "cpp internal error: base->type != T_ASSERT in do_assert");
2796     goto error;
2797   }
2798
2799   this = cpp_install (pfile, sym, thislen, T_ASSERT,
2800                       (char *)base->value.aschain, -1);
2801   base->value.aschain = this;
2802   
2803   pfile->limit = (unsigned char *) sym; /* Pop */
2804   return 0;
2805
2806  error:
2807   skip_rest_of_line (pfile);
2808   pfile->limit = (unsigned char *) sym; /* Pop */
2809   return 0;
2810 }
2811
2812 static int
2813 do_unassert (pfile, keyword)
2814      cpp_reader *pfile;
2815      struct directive *keyword ATTRIBUTE_UNUSED;
2816 {
2817   int c, ret;
2818   char *sym;
2819   long baselen, thislen;
2820   HASHNODE *base, *this, *next;
2821   
2822   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2823       && !CPP_BUFFER (pfile)->system_header_p)
2824     cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
2825
2826   cpp_skip_hspace (pfile);
2827
2828   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
2829   ret = parse_assertion (pfile);
2830   if (ret == 0)
2831     goto error;
2832   
2833   cpp_skip_hspace (pfile);
2834   c = PEEKC ();
2835   if (c != EOF && c != '\n')
2836       cpp_error (pfile, "junk at end of `#unassert'");
2837
2838   thislen = strlen (sym);
2839   if (ret == 1)
2840     {
2841       base = cpp_lookup (pfile, sym, thislen, -1);
2842       if (! base)
2843         goto error;  /* It isn't an error to #undef what isn't #defined,
2844                         so it isn't an error to #unassert what isn't
2845                         #asserted either. */
2846       
2847       for (this = base->value.aschain; this; this = next)
2848         {
2849           next = this->value.aschain;
2850           delete_macro (this);
2851         }
2852       delete_macro (base);
2853     }
2854   else
2855     {
2856       baselen = index (sym, '(') - sym;
2857       base = cpp_lookup (pfile, sym, baselen, -1);
2858       if (! base) goto error;
2859       this = cpp_lookup (pfile, sym, thislen, -1);
2860       if (! this) goto error;
2861
2862       next = base;
2863       while (next->value.aschain != this)
2864         next = next->value.aschain;
2865
2866       next->value.aschain = this->value.aschain;
2867       delete_macro (this);
2868
2869       if (base->value.aschain == NULL)
2870         delete_macro (base);  /* Last answer for this predicate deleted. */
2871     }
2872   
2873   pfile->limit = (unsigned char *) sym; /* Pop */
2874   return 0;
2875  error:
2876   skip_rest_of_line (pfile);
2877   pfile->limit = (unsigned char *) sym; /* Pop */
2878   return 0;
2879 }
2880
2881 /* Process STR as if it appeared as the body of an #unassert. */
2882 void
2883 cpp_unassert (pfile, str)
2884      cpp_reader *pfile;
2885      unsigned char *str;
2886 {
2887   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
2888     {
2889       do_assert (pfile, NULL);
2890       cpp_pop_buffer (pfile);
2891     }
2892 }  
2893
2894 int
2895 cpp_read_check_assertion (pfile)
2896      cpp_reader *pfile;
2897 {
2898   U_CHAR *name = CPP_PWRITTEN (pfile);
2899   int result;
2900   HASHNODE *hp;
2901   
2902   FORWARD (1);  /* Skip '#' */
2903   cpp_skip_hspace (pfile);
2904   if (! parse_assertion (pfile))
2905     result = 0;
2906   else
2907     {
2908       hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
2909       result = (hp != 0);
2910     }
2911
2912   pfile->limit = name;
2913   return result;
2914 }
2915
2916 /* Remember the current position of PFILE.  */
2917
2918 void
2919 parse_set_mark (pfile)
2920      cpp_reader *pfile;
2921 {
2922   cpp_buffer *ip = CPP_BUFFER (pfile);
2923   if (ip->mark != -1)
2924       cpp_fatal (pfile,
2925                  "cpp internal error: ip->mark != -1 in parse_set_mark");
2926
2927   ip->mark = ip->cur - ip->buf;
2928 }
2929
2930 /* Clear the current mark - we no longer need it.  */
2931
2932 void
2933 parse_clear_mark (pfile)
2934      cpp_reader *pfile;
2935 {
2936   cpp_buffer *ip = CPP_BUFFER (pfile);
2937   if (ip->mark == -1)
2938       cpp_fatal (pfile,
2939                  "cpp internal error: ip->mark == -1 in parse_clear_mark");
2940
2941   ip->mark = -1;
2942 }
2943
2944 /* Backup the current position of PFILE to that saved in its mark,
2945    and clear the mark.  */
2946
2947 void
2948 parse_goto_mark (pfile)
2949      cpp_reader *pfile;
2950 {
2951   cpp_buffer *ip = CPP_BUFFER (pfile);
2952   if (ip->mark == -1)
2953       cpp_fatal (pfile,
2954                  "cpp internal error: ip->mark == -1 in parse_goto_mark");
2955
2956   ip->cur = ip->buf + ip->mark;
2957   ip->mark = -1;
2958 }
2959
2960 void
2961 cpp_print_file_and_line (pfile)
2962      cpp_reader *pfile;
2963 {
2964   cpp_buffer *ip = cpp_file_buffer (pfile);
2965
2966   if (ip != NULL)
2967     {
2968       long line, col;
2969       cpp_buf_line_and_col (ip, &line, &col);
2970       cpp_file_line_for_message (pfile, ip->nominal_fname,
2971                                  line, pfile->show_column ? col : -1);
2972     }
2973 }
2974
2975 static void
2976 v_cpp_error (pfile, msgid, ap)
2977   cpp_reader *pfile;
2978   const char *msgid;
2979   va_list ap;
2980 {
2981   cpp_print_containing_files (pfile);
2982   cpp_print_file_and_line (pfile);
2983   v_cpp_message (pfile, 1, msgid, ap);
2984 }
2985
2986 void
2987 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
2988 {
2989 #ifndef ANSI_PROTOTYPES
2990   cpp_reader *pfile;
2991   const char *msgid;
2992 #endif
2993   va_list ap;
2994
2995   VA_START(ap, msgid);
2996   
2997 #ifndef ANSI_PROTOTYPES
2998   pfile = va_arg (ap, cpp_reader *);
2999   msgid = va_arg (ap, const char *);
3000 #endif
3001
3002   v_cpp_error (pfile, msgid, ap);
3003   va_end(ap);
3004 }
3005
3006 /* Print error message but don't count it.  */
3007
3008 static void
3009 v_cpp_warning (pfile, msgid, ap)
3010   cpp_reader *pfile;
3011   const char *msgid;
3012   va_list ap;
3013 {
3014   if (CPP_OPTIONS (pfile)->inhibit_warnings)
3015     return;
3016
3017   if (CPP_OPTIONS (pfile)->warnings_are_errors)
3018     pfile->errors++;
3019
3020   cpp_print_containing_files (pfile);
3021   cpp_print_file_and_line (pfile);
3022   v_cpp_message (pfile, 0, msgid, ap);
3023 }
3024
3025 void
3026 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3027 {
3028 #ifndef ANSI_PROTOTYPES
3029   cpp_reader *pfile;
3030   const char *msgid;
3031 #endif
3032   va_list ap;
3033   
3034   VA_START (ap, msgid);
3035   
3036 #ifndef ANSI_PROTOTYPES
3037   pfile = va_arg (ap, cpp_reader *);
3038   msgid = va_arg (ap, const char *);
3039 #endif
3040
3041   v_cpp_warning (pfile, msgid, ap);
3042   va_end(ap);
3043 }
3044
3045 /* Print an error message and maybe count it.  */
3046
3047 void
3048 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3049 {
3050 #ifndef ANSI_PROTOTYPES
3051   cpp_reader *pfile;
3052   const char *msgid;
3053 #endif
3054   va_list ap;
3055   
3056   VA_START (ap, msgid);
3057   
3058 #ifndef ANSI_PROTOTYPES
3059   pfile = va_arg (ap, cpp_reader *);
3060   msgid = va_arg (ap, const char *);
3061 #endif
3062
3063   if (CPP_OPTIONS (pfile)->pedantic_errors)
3064     v_cpp_error (pfile, msgid, ap);
3065   else
3066     v_cpp_warning (pfile, msgid, ap);
3067   va_end(ap);
3068 }
3069
3070 static void
3071 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3072   cpp_reader * pfile;
3073   int line;
3074   int column;
3075   const char * msgid;
3076   va_list ap;
3077 {
3078   cpp_buffer *ip = cpp_file_buffer (pfile);
3079
3080   cpp_print_containing_files (pfile);
3081
3082   if (ip != NULL)
3083     cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3084
3085   v_cpp_message (pfile, 1, msgid, ap);
3086 }
3087
3088 void
3089 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3090                              const char *msgid, ...))
3091 {
3092 #ifndef ANSI_PROTOTYPES
3093   cpp_reader *pfile;
3094   int line;
3095   int column;
3096   const char *msgid;
3097 #endif
3098   va_list ap;
3099   
3100   VA_START (ap, msgid);
3101   
3102 #ifndef ANSI_PROTOTYPES
3103   pfile = va_arg (ap, cpp_reader *);
3104   line = va_arg (ap, int);
3105   column = va_arg (ap, int);
3106   msgid = va_arg (ap, const char *);
3107 #endif
3108
3109   v_cpp_error_with_line(pfile, line, column, msgid, ap);
3110   va_end(ap);
3111 }
3112
3113 static void
3114 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3115   cpp_reader * pfile;
3116   int line;
3117   int column;
3118   const char *msgid;
3119   va_list ap;
3120 {
3121   cpp_buffer *ip;
3122
3123   if (CPP_OPTIONS (pfile)->inhibit_warnings)
3124     return;
3125
3126   if (CPP_OPTIONS (pfile)->warnings_are_errors)
3127     pfile->errors++;
3128
3129   cpp_print_containing_files (pfile);
3130
3131   ip = cpp_file_buffer (pfile);
3132
3133   if (ip != NULL)
3134     cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3135
3136   v_cpp_message (pfile, 0, msgid, ap);
3137 }  
3138
3139 void
3140 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3141                                const char *msgid, ...))
3142 {
3143 #ifndef ANSI_PROTOTYPES
3144   cpp_reader *pfile;
3145   int line;
3146   int column;
3147   const char *msgid;
3148 #endif
3149   va_list ap;
3150   
3151   VA_START (ap, msgid);
3152   
3153 #ifndef ANSI_PROTOTYPES
3154   pfile = va_arg (ap, cpp_reader *);
3155   line = va_arg (ap, int);
3156   column = va_arg (ap, int);
3157   msgid = va_arg (ap, const char *);
3158 #endif
3159
3160   v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3161   va_end(ap);
3162 }
3163
3164 void
3165 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3166                                const char *msgid, ...))
3167 {
3168 #ifndef ANSI_PROTOTYPES
3169   cpp_reader *pfile;
3170   int line;
3171   int column;
3172   const char *msgid;
3173 #endif
3174   va_list ap;
3175   
3176   VA_START (ap, msgid);
3177   
3178 #ifndef ANSI_PROTOTYPES
3179   pfile = va_arg (ap, cpp_reader *);
3180   line = va_arg (ap, int);
3181   column = va_arg (ap, int);
3182   msgid = va_arg (ap, const char *);
3183 #endif
3184
3185   if (CPP_OPTIONS (pfile)->pedantic_errors)
3186     v_cpp_error_with_line (pfile, column, line, msgid, ap);
3187   else
3188     v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3189   va_end(ap);
3190 }
3191
3192 /* Report a warning (or an error if pedantic_errors)
3193    giving specified file name and line number, not current.  */
3194
3195 void
3196 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line,
3197                                         const char *msgid, ...))
3198 {
3199 #ifndef ANSI_PROTOTYPES
3200   cpp_reader *pfile;
3201   char *file;
3202   int line;
3203   const char *msgid;
3204 #endif
3205   va_list ap;
3206   
3207   VA_START (ap, msgid);
3208
3209 #ifndef ANSI_PROTOTYPES
3210   pfile = va_arg (ap, cpp_reader *);
3211   file = va_arg (ap, char *);
3212   line = va_arg (ap, int);
3213   msgid = va_arg (ap, const char *);
3214 #endif
3215
3216   if (!CPP_OPTIONS (pfile)->pedantic_errors
3217       && CPP_OPTIONS (pfile)->inhibit_warnings)
3218     return;
3219   if (file != NULL)
3220     cpp_file_line_for_message (pfile, file, line, -1);
3221   v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3222   va_end(ap);
3223 }
3224
3225 /* my_strerror - return the descriptive text associated with an
3226    `errno' code.  */
3227
3228 static char *
3229 my_strerror (errnum)
3230      int errnum;
3231 {
3232   char *result;
3233
3234 #ifndef VMS
3235 #ifndef HAVE_STRERROR
3236   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3237 #else
3238   result = strerror (errnum);
3239 #endif
3240 #else   /* VMS */
3241   /* VAXCRTL's strerror() takes an optional second argument, which only
3242      matters when the first argument is EVMSERR.  However, it's simplest
3243      just to pass it unconditionally.  `vaxc$errno' is declared in
3244      <errno.h>, and maintained by the library in parallel with `errno'.
3245      We assume that caller's `errnum' either matches the last setting of
3246      `errno' by the library or else does not have the value `EVMSERR'.  */
3247
3248   result = strerror (errnum, vaxc$errno);
3249 #endif
3250
3251   if (!result)
3252     result = "errno = ?";
3253
3254   return result;
3255 }
3256
3257 /* Error including a message from `errno'.  */
3258
3259 void
3260 cpp_error_from_errno (pfile, name)
3261      cpp_reader *pfile;
3262      const char *name;
3263 {
3264   cpp_message_from_errno (pfile, 1, name);
3265 }
3266
3267 void
3268 cpp_message_from_errno (pfile, is_error, name)
3269      cpp_reader *pfile;
3270      int is_error;
3271      const char *name;
3272 {
3273   int e = errno;
3274   cpp_buffer *ip = cpp_file_buffer (pfile);
3275
3276   cpp_print_containing_files (pfile);
3277
3278   if (ip != NULL)
3279     cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3280
3281   cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3282 }
3283
3284 void
3285 cpp_perror_with_name (pfile, name)
3286      cpp_reader *pfile;
3287      const char *name;
3288 {
3289   cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3290 }
3291
3292 /* TODO:
3293  * No pre-compiled header file support.
3294  *
3295  * Possibly different enum token codes for each C/C++ token.
3296  *
3297  * Find and cleanup remaining uses of static variables,
3298  *
3299  * Support -dM flag (dump_all_macros).
3300  *
3301  * Support for_lint flag.
3302  */