Fix a bug in 2.95.x prototypes that prevents us from building the world with
[dragonfly.git] / contrib / 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 = 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 = 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 = 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 = 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)->seen_eof)
2031         {
2032           if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2033             return CPP_EOF;
2034
2035           cpp_pop_buffer (pfile);
2036           goto get_next;
2037         }
2038       else
2039         {
2040           cpp_buffer *next_buf
2041             = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2042           CPP_BUFFER (pfile)->seen_eof = 1;
2043           if (CPP_BUFFER (pfile)->nominal_fname
2044               && next_buf != CPP_NULL_BUFFER (pfile))
2045             {
2046               /* We're about to return from an #include file.
2047                  Emit #line information now (as part of the CPP_POP) result.
2048                  But the #line refers to the file we will pop to.  */
2049               cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2050               CPP_BUFFER (pfile) = next_buf;
2051               pfile->input_stack_listing_current = 0;
2052               output_line_command (pfile, leave_file);
2053               CPP_BUFFER (pfile) = cur_buffer;
2054             }
2055           return CPP_POP;
2056         }
2057     }
2058   else
2059     {
2060       switch (c)
2061         {
2062         case '/':
2063           if (PEEKC () == '=')
2064             goto op2;
2065
2066         comment:
2067           if (opts->put_out_comments)
2068             c = copy_comment (pfile, c);
2069           else
2070             c = skip_comment (pfile, c);
2071           if (c == EOF)
2072             goto handle_eof;
2073           else if (c != ' ')
2074             goto randomchar;
2075           
2076           /* Comments are equivalent to spaces.
2077              For -traditional, a comment is equivalent to nothing.  */
2078           if (opts->traditional || opts->put_out_comments)
2079             return CPP_COMMENT;
2080           else
2081             {
2082               CPP_PUTC (pfile, c);
2083               return CPP_HSPACE;
2084             }
2085 #if 0
2086           if (opts->for_lint) {
2087             U_CHAR *argbp;
2088             int cmdlen, arglen;
2089             char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2090             
2091             if (lintcmd != NULL) {
2092               /* I believe it is always safe to emit this newline: */
2093               obp[-1] = '\n';
2094               bcopy ("#pragma lint ", (char *) obp, 13);
2095               obp += 13;
2096               bcopy (lintcmd, (char *) obp, cmdlen);
2097               obp += cmdlen;
2098
2099               if (arglen != 0) {
2100                 *(obp++) = ' ';
2101                 bcopy (argbp, (char *) obp, arglen);
2102                 obp += arglen;
2103               }
2104
2105               /* OK, now bring us back to the state we were in before we entered
2106                  this branch.  We need #line because the newline for the pragma
2107                  could mess things up.  */
2108               output_line_command (pfile, same_file);
2109               *(obp++) = ' ';   /* just in case, if comments are copied thru */
2110               *(obp++) = '/';
2111             }
2112           }
2113 #endif
2114
2115         case '#':
2116 #if 0
2117           /* If this is expanding a macro definition, don't recognize
2118              preprocessor directives.  */
2119           if (ip->macro != 0)
2120             goto randomchar;
2121           /* If this is expand_into_temp_buffer, recognize them
2122              only after an actual newline at this level,
2123              not at the beginning of the input level.  */
2124           if (ip->fname == 0 && beg_of_line == ip->buf)
2125             goto randomchar;
2126           if (ident_length)
2127             goto specialchar;
2128 #endif
2129
2130           if (!pfile->only_seen_white)
2131             goto randomchar;
2132           if (handle_directive (pfile))
2133             return CPP_DIRECTIVE;
2134           pfile->only_seen_white = 0;
2135           return CPP_OTHER;
2136
2137         case '\"':
2138         case '\'':
2139         string:
2140           parse_string (pfile, c);
2141           pfile->only_seen_white = 0;
2142           return c == '\'' ? CPP_CHAR : CPP_STRING;
2143
2144         case '$':
2145           if (!opts->dollars_in_ident)
2146             goto randomchar;
2147           goto letter;
2148
2149         case ':':
2150           if (opts->cplusplus && PEEKC () == ':')
2151             goto op2;
2152           goto randomchar;
2153
2154         case '&':
2155         case '+':
2156         case '|':
2157           c2 = PEEKC ();
2158           if (c2 == c || c2 == '=')
2159             goto op2;
2160           goto randomchar;
2161
2162         case '*':
2163         case '!':
2164         case '%':
2165         case '=':
2166         case '^':
2167           if (PEEKC () == '=')
2168             goto op2;
2169           goto randomchar;
2170
2171         case '-':
2172           c2 = PEEKC ();
2173           if (c2 == '-' && opts->chill)
2174             goto comment;  /* Chill style comment */
2175           if (c2 == '-' || c2 == '=' || c2 == '>')
2176             goto op2;
2177           goto randomchar;
2178
2179         case '<':
2180           if (pfile->parsing_include_directive)
2181             {
2182               for (;;)
2183                 {
2184                   CPP_PUTC (pfile, c);
2185                   if (c == '>')
2186                     break;
2187                   c = GETC ();
2188                   if (c == '\n' || c == EOF)
2189                     {
2190                       cpp_error (pfile,
2191                                  "missing '>' in `#include <FILENAME>'");
2192                       break;
2193                     }
2194                   else if (c == '\r')
2195                     {
2196                       if (!CPP_BUFFER (pfile)->has_escapes)
2197                         {
2198                           /* Backslash newline is replaced by nothing. */
2199                           CPP_ADJUST_WRITTEN (pfile, -1);
2200                           CPP_BUMP_LINE (pfile);
2201                         }
2202                       else
2203                         {
2204                           /* We might conceivably get \r- or \r<space> in
2205                              here.  Just delete 'em. */
2206                           int d = GETC();
2207                           if (d != '-' && d != ' ')
2208                             cpp_fatal (pfile,
2209                                   "internal error: unrecognized escape \\r%c",
2210                                        d);
2211                           CPP_ADJUST_WRITTEN (pfile, -1);
2212                         }                         
2213                     }
2214                 }
2215               return CPP_STRING;
2216             }
2217           /* else fall through */
2218         case '>':
2219           c2 = PEEKC ();
2220           if (c2 == '=')
2221             goto op2;
2222           if (c2 != c)
2223             goto randomchar;
2224           FORWARD(1);
2225           CPP_RESERVE (pfile, 4);
2226           CPP_PUTC (pfile, c);
2227           CPP_PUTC (pfile, c2);
2228           c3 = PEEKC ();
2229           if (c3 == '=')
2230             CPP_PUTC_Q (pfile, GETC ());
2231           CPP_NUL_TERMINATE_Q (pfile);
2232           pfile->only_seen_white = 0;
2233           return CPP_OTHER;
2234
2235         case '.':
2236           c2 = PEEKC ();
2237           if (ISDIGIT(c2))
2238             {
2239               CPP_RESERVE(pfile, 2);
2240               CPP_PUTC_Q (pfile, '.');
2241               c = GETC ();
2242               goto number;
2243             }
2244           if (c2 == '.' && PEEKN(1) == '.')
2245             {
2246               CPP_RESERVE(pfile, 4);
2247               CPP_PUTC_Q (pfile, '.');
2248               CPP_PUTC_Q (pfile, '.');
2249               CPP_PUTC_Q (pfile, '.');
2250               FORWARD (2);
2251               CPP_NUL_TERMINATE_Q (pfile);
2252               pfile->only_seen_white = 0;
2253               return CPP_3DOTS;
2254             }
2255           goto randomchar;
2256
2257         op2:
2258           token = CPP_OTHER;
2259           pfile->only_seen_white = 0;
2260           CPP_RESERVE(pfile, 3);
2261           CPP_PUTC_Q (pfile, c);
2262           CPP_PUTC_Q (pfile, GETC ());
2263           CPP_NUL_TERMINATE_Q (pfile);
2264           return token;
2265
2266         case 'L':
2267           c2 = PEEKC ();
2268           if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2269             {
2270               CPP_PUTC (pfile, c);
2271               c = GETC ();
2272               goto string;
2273             }
2274           goto letter;
2275
2276         case '0': case '1': case '2': case '3': case '4':
2277         case '5': case '6': case '7': case '8': case '9':
2278         number:
2279           c2  = '.';
2280           for (;;)
2281             {
2282               CPP_RESERVE (pfile, 2);
2283               CPP_PUTC_Q (pfile, c);
2284               c = PEEKC ();
2285               if (c == EOF)
2286                 break;
2287               if (!is_idchar[c] && c != '.'
2288                   && ((c2 != 'e' && c2 != 'E'
2289                        && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2290                       || (c != '+' && c != '-')))
2291                 break;
2292               FORWARD(1);
2293               c2= c;
2294             }
2295           CPP_NUL_TERMINATE_Q (pfile);
2296           pfile->only_seen_white = 0;
2297           return CPP_NUMBER;
2298         case 'b': case 'c': case 'd': case 'h': case 'o':
2299         case 'B': case 'C': case 'D': case 'H': case 'O':
2300           if (opts->chill && PEEKC () == '\'')
2301             {
2302               pfile->only_seen_white = 0;
2303               CPP_RESERVE (pfile, 2);
2304               CPP_PUTC_Q (pfile, c);
2305               CPP_PUTC_Q (pfile, '\'');
2306               FORWARD(1);
2307               for (;;)
2308                 {
2309                   c = GETC();
2310                   if (c == EOF)
2311                     goto chill_number_eof;
2312                   if (!is_idchar[c])
2313                     break;
2314                   CPP_PUTC (pfile, c);
2315                 }
2316               if (c == '\'')
2317                 {
2318                   CPP_RESERVE (pfile, 2);
2319                   CPP_PUTC_Q (pfile, c);
2320                   CPP_NUL_TERMINATE_Q (pfile);
2321                   return CPP_STRING;
2322                 }
2323               else
2324                 {
2325                   FORWARD(-1);
2326                 chill_number_eof:
2327                   CPP_NUL_TERMINATE (pfile);
2328                   return CPP_NUMBER;
2329                 }
2330             }
2331           else
2332             goto letter;
2333         case '_':
2334         case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2335         case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2336         case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2337         case 'x': case 'y': case 'z':
2338         case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2339         case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2340         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2341         case 'Y': case 'Z':
2342         letter:
2343           {
2344             HASHNODE *hp;
2345             unsigned char *ident;
2346             int before_name_written = CPP_WRITTEN (pfile);
2347             int ident_len;
2348             parse_name (pfile, c);
2349             pfile->only_seen_white = 0;
2350             if (pfile->no_macro_expand)
2351               return CPP_NAME;
2352             ident = pfile->token_buffer + before_name_written;
2353             ident_len = CPP_PWRITTEN (pfile) - ident;
2354             hp = cpp_lookup (pfile, ident, ident_len, -1);
2355             if (!hp)
2356               return CPP_NAME;
2357             if (hp->type == T_DISABLED)
2358               {
2359                 if (pfile->output_escapes)
2360                   { /* Return "\r-IDENT", followed by '\0'.  */
2361                     int i;
2362                     CPP_RESERVE (pfile, 3);
2363                     ident = pfile->token_buffer + before_name_written;
2364                     CPP_ADJUST_WRITTEN (pfile, 2);
2365                     for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2366                     ident[0] = '\r';
2367                     ident[1] = '-';
2368                   }
2369                 return CPP_NAME;
2370               }
2371
2372             /* If macro wants an arglist, verify that a '(' follows.
2373                first skip all whitespace, copying it to the output
2374                after the macro name.  Then, if there is no '(',
2375                decide this is not a macro call and leave things that way.  */
2376             if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2377             {
2378               int is_macro_call, macbuf_whitespace = 0;
2379
2380               parse_set_mark (pfile);
2381               for (;;)
2382                 {
2383                   cpp_skip_hspace (pfile);
2384                   c = PEEKC ();
2385                   is_macro_call = c == '(';
2386                   if (c != EOF)
2387                     {
2388                       if (c != '\n')
2389                         break;
2390                       FORWARD (1);
2391                     }
2392                   else
2393                     {
2394                       if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2395                         {
2396                           if (CPP_BUFFER (pfile)->mark !=
2397                               (CPP_BUFFER (pfile)->cur
2398                                - CPP_BUFFER (pfile)->buf))
2399                              macbuf_whitespace = 1;
2400
2401                           /* The mark goes away automatically when
2402                              the buffer is popped. */
2403                           cpp_pop_buffer (pfile);
2404                           parse_set_mark (pfile);
2405                         }
2406                       else
2407                         break;
2408                     }
2409                 }
2410               if (!is_macro_call)
2411                 {
2412                   parse_goto_mark (pfile);
2413                   if (macbuf_whitespace)
2414                     CPP_PUTC (pfile, ' ');
2415                 }
2416               else
2417                 parse_clear_mark (pfile);
2418               if (!is_macro_call)
2419                 return CPP_NAME;
2420             }
2421             /* This is now known to be a macro call.
2422                Expand the macro, reading arguments as needed,
2423                and push the expansion on the input stack.  */
2424             macroexpand (pfile, hp);
2425             CPP_SET_WRITTEN (pfile, before_name_written);
2426           }
2427           goto get_next;
2428
2429         case ' ':  case '\t':  case '\v':
2430           for (;;)
2431             {
2432               CPP_PUTC (pfile, c);
2433               c = PEEKC ();
2434               if (c == EOF || !is_hor_space[c])
2435                 break;
2436               FORWARD(1);
2437             }
2438           return CPP_HSPACE;
2439
2440         case '\r':
2441           if (CPP_BUFFER (pfile)->has_escapes)
2442             {
2443               c = GETC ();
2444               if (c == '-')
2445                 {
2446                   if (pfile->output_escapes)
2447                     CPP_PUTS (pfile, "\r-", 2);
2448                   parse_name (pfile, GETC ());
2449                   return CPP_NAME;
2450                 }
2451               else if (c == ' ')
2452                 {
2453                   CPP_RESERVE (pfile, 2);
2454                   if (pfile->output_escapes)
2455                     CPP_PUTC_Q (pfile, '\r');
2456                   CPP_PUTC_Q (pfile, c);
2457                   return CPP_HSPACE;
2458                 }
2459               else
2460                 {
2461                   cpp_fatal (pfile,
2462                              "internal error: unrecognized escape \\r%c", c);
2463                   goto get_next;
2464                 }
2465             }
2466           else
2467             {
2468               /* Backslash newline is ignored. */
2469               CPP_BUMP_LINE (pfile);
2470               goto get_next;
2471             }
2472
2473         case '\n':
2474           CPP_PUTC (pfile, c);
2475           if (pfile->only_seen_white == 0)
2476             pfile->only_seen_white = 1;
2477           CPP_BUMP_LINE (pfile);
2478           if (! CPP_OPTIONS (pfile)->no_line_commands)
2479             {
2480               pfile->lineno++;
2481               if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2482                 output_line_command (pfile, same_file);
2483             }
2484           return CPP_VSPACE;
2485
2486         case '(': token = CPP_LPAREN;    goto char1;
2487         case ')': token = CPP_RPAREN;    goto char1;
2488         case '{': token = CPP_LBRACE;    goto char1;
2489         case '}': token = CPP_RBRACE;    goto char1;
2490         case ',': token = CPP_COMMA;     goto char1;
2491         case ';': token = CPP_SEMICOLON; goto char1;
2492
2493         randomchar:
2494         default:
2495           token = CPP_OTHER;
2496         char1:
2497           pfile->only_seen_white = 0;
2498           CPP_PUTC (pfile, c);
2499           return token;
2500         }
2501     }
2502 }
2503
2504 /* Like cpp_get_token, but skip spaces and comments.  */
2505
2506 enum cpp_token
2507 cpp_get_non_space_token (pfile)
2508      cpp_reader *pfile;
2509 {
2510   int old_written = CPP_WRITTEN (pfile);
2511   for (;;)
2512     {
2513       enum cpp_token token = cpp_get_token (pfile);
2514       if (token != CPP_COMMENT && token != CPP_POP
2515           && token != CPP_HSPACE && token != CPP_VSPACE)
2516         return token;
2517       CPP_SET_WRITTEN (pfile, old_written);
2518     }
2519 }
2520
2521 /* Parse an identifier starting with C.  */
2522
2523 static void
2524 parse_name (pfile, c)
2525      cpp_reader *pfile;
2526      int c;
2527 {
2528   for (;;)
2529   {
2530       if (! is_idchar[c])
2531       {
2532           FORWARD (-1);
2533           break;
2534       }
2535
2536       if (c == '$' && CPP_PEDANTIC (pfile))
2537         cpp_pedwarn (pfile, "`$' in identifier");
2538
2539       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
2540       CPP_PUTC_Q (pfile, c);
2541       c = GETC();
2542       if (c == EOF)
2543         break;
2544   }
2545   CPP_NUL_TERMINATE_Q (pfile);
2546   return;
2547 }
2548
2549 /* Parse a string starting with C.  A single quoted string is treated
2550    like a double -- some programs (e.g., troff) are perverse this way.
2551    (However, a single quoted string is not allowed to extend over
2552    multiple lines.  */
2553 static void
2554 parse_string (pfile, c)
2555      cpp_reader *pfile;
2556      int c;
2557 {
2558   long start_line, start_column;
2559   
2560   cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2561
2562   CPP_PUTC (pfile, c);
2563   while (1)
2564     {
2565       int cc = GETC();
2566       if (cc == EOF)
2567         {
2568           if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2569             {
2570               /* try harder: this string crosses a macro expansion
2571                  boundary.  This can happen naturally if -traditional.
2572                  Otherwise, only -D can make a macro with an unmatched
2573                  quote.  */
2574               cpp_pop_buffer (pfile);
2575               continue;
2576             }
2577           if (!CPP_TRADITIONAL (pfile))
2578             {
2579               cpp_error_with_line (pfile, start_line, start_column,
2580                                  "unterminated string or character constant");
2581               if (pfile->multiline_string_line != start_line
2582                   && pfile->multiline_string_line != 0)
2583                 cpp_error_with_line (pfile,
2584                                      pfile->multiline_string_line, -1,
2585                                "possible real start of unterminated constant");
2586               pfile->multiline_string_line = 0;
2587             }
2588           break;
2589         }
2590       CPP_PUTC (pfile, cc);
2591       switch (cc)
2592         {
2593         case '\n':
2594           CPP_BUMP_LINE (pfile);
2595           pfile->lineno++;
2596           /* Traditionally, end of line ends a string constant with
2597              no error.  */
2598           if (CPP_TRADITIONAL (pfile))
2599             return;
2600           /* Character constants may not extend over multiple lines.  */
2601           if (c == '\'')
2602             {
2603               cpp_error_with_line (pfile, start_line, start_column,
2604                                    "unterminated character constant");
2605               return;
2606             }
2607           if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2608             {
2609               cpp_pedwarn_with_line (pfile, start_line, start_column,
2610                                      "string constant runs past end of line");
2611             }
2612           if (pfile->multiline_string_line == 0)
2613             pfile->multiline_string_line = start_line;
2614           break;
2615
2616         case '\r':
2617           CPP_ADJUST_WRITTEN (pfile, -1);
2618           if (CPP_BUFFER (pfile)->has_escapes)
2619             {
2620               cpp_fatal (pfile,
2621                          "internal error: \\r escape inside string constant");
2622               FORWARD(1);
2623             }
2624           else
2625             /* Backslash newline is replaced by nothing at all.  */
2626             CPP_BUMP_LINE (pfile);
2627           break;
2628
2629         case '\\':
2630           cc = GETC();
2631           if (cc != EOF)
2632             CPP_PUTC (pfile, cc);
2633           break;
2634
2635         case '\"':
2636         case '\'':
2637           if (cc == c)
2638             return;
2639           break;
2640         }
2641     }
2642 }
2643
2644 /* Read an assertion into the token buffer, converting to
2645    canonical form: `#predicate(a n swe r)'  The next non-whitespace
2646    character to read should be the first letter of the predicate.
2647    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2648    with answer (see callers for why). In case of 0, an error has been
2649    printed. */
2650 static int
2651 parse_assertion (pfile)
2652      cpp_reader *pfile;
2653 {
2654   int c, dropwhite;
2655   cpp_skip_hspace (pfile);
2656   c = PEEKC();
2657   if (! is_idstart[c])
2658     {
2659       cpp_error (pfile, "assertion predicate is not an identifier");
2660       return 0;
2661     }
2662   CPP_PUTC(pfile, '#');
2663   FORWARD(1);
2664   parse_name(pfile, c);
2665
2666   c = PEEKC();
2667   if (c != '(')
2668     {
2669       if (is_hor_space[c] || c == '\r')
2670         cpp_skip_hspace (pfile);
2671       c = PEEKC();
2672     }
2673   if (c != '(')
2674     return 1;
2675
2676   CPP_PUTC(pfile, '(');
2677   FORWARD(1);
2678   dropwhite = 1;
2679   while ((c = GETC()) != ')')
2680     {
2681       if (is_hor_space[c])
2682         {
2683           if (! dropwhite)
2684             {
2685               CPP_PUTC(pfile, ' ');
2686               dropwhite = 1;
2687             }
2688         }
2689       else if (c == '\n' || c == EOF)
2690         {
2691           if (c == '\n') FORWARD(-1);
2692           cpp_error (pfile, "un-terminated assertion answer");
2693           return 0;
2694         }
2695       else if (c == '\r')
2696         /* \r cannot be a macro escape here. */
2697         CPP_BUMP_LINE (pfile);
2698       else
2699         {
2700           CPP_PUTC (pfile, c);
2701           dropwhite = 0;
2702         }
2703     }
2704
2705   if (pfile->limit[-1] == ' ')
2706     pfile->limit[-1] = ')';
2707   else if (pfile->limit[-1] == '(')
2708     {
2709       cpp_error (pfile, "empty token sequence in assertion");
2710       return 0;
2711     }
2712   else
2713     CPP_PUTC (pfile, ')');
2714
2715   CPP_NUL_TERMINATE (pfile);
2716   return 2;
2717 }
2718
2719 static int
2720 do_assert (pfile, keyword)
2721      cpp_reader *pfile;
2722      struct directive *keyword ATTRIBUTE_UNUSED;
2723 {
2724   char *sym;
2725   int ret, c;
2726   HASHNODE *base, *this;
2727   int baselen, thislen;
2728
2729   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2730       && !CPP_BUFFER (pfile)->system_header_p)
2731     cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
2732
2733   cpp_skip_hspace (pfile);
2734   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
2735   ret = parse_assertion (pfile);
2736   if (ret == 0)
2737     goto error;
2738   else if (ret == 1)
2739     {
2740       cpp_error (pfile, "missing token-sequence in `#assert'");
2741       goto error;
2742     }
2743
2744   cpp_skip_hspace (pfile);
2745   c = PEEKC();
2746   if (c != EOF && c != '\n')
2747     {
2748       cpp_error (pfile, "junk at end of `#assert'");
2749       goto error;
2750     }
2751
2752   thislen = strlen (sym);
2753   baselen = index (sym, '(') - sym;
2754   this = cpp_lookup (pfile, sym, thislen, -1);
2755   if (this)
2756     {
2757       cpp_warning (pfile, "`%s' re-asserted", sym);
2758       goto error;
2759     }
2760
2761   base = cpp_lookup (pfile, sym, baselen, -1);
2762   if (! base)
2763     base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
2764   else if (base->type != T_ASSERT)
2765   {
2766     /* Token clash - but with what?! */
2767     cpp_fatal (pfile,
2768                "cpp internal error: base->type != T_ASSERT in do_assert");
2769     goto error;
2770   }
2771
2772   this = cpp_install (pfile, sym, thislen, T_ASSERT,
2773                       (char *)base->value.aschain, -1);
2774   base->value.aschain = this;
2775   
2776   pfile->limit = (unsigned char *) sym; /* Pop */
2777   return 0;
2778
2779  error:
2780   skip_rest_of_line (pfile);
2781   pfile->limit = (unsigned char *) sym; /* Pop */
2782   return 0;
2783 }
2784
2785 static int
2786 do_unassert (pfile, keyword)
2787      cpp_reader *pfile;
2788      struct directive *keyword ATTRIBUTE_UNUSED;
2789 {
2790   int c, ret;
2791   char *sym;
2792   long baselen, thislen;
2793   HASHNODE *base, *this, *next;
2794   
2795   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2796       && !CPP_BUFFER (pfile)->system_header_p)
2797     cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
2798
2799   cpp_skip_hspace (pfile);
2800
2801   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
2802   ret = parse_assertion (pfile);
2803   if (ret == 0)
2804     goto error;
2805   
2806   cpp_skip_hspace (pfile);
2807   c = PEEKC ();
2808   if (c != EOF && c != '\n')
2809       cpp_error (pfile, "junk at end of `#unassert'");
2810
2811   thislen = strlen (sym);
2812   if (ret == 1)
2813     {
2814       base = cpp_lookup (pfile, sym, thislen, -1);
2815       if (! base)
2816         goto error;  /* It isn't an error to #undef what isn't #defined,
2817                         so it isn't an error to #unassert what isn't
2818                         #asserted either. */
2819       
2820       for (this = base->value.aschain; this; this = next)
2821         {
2822           next = this->value.aschain;
2823           delete_macro (this);
2824         }
2825       delete_macro (base);
2826     }
2827   else
2828     {
2829       baselen = index (sym, '(') - sym;
2830       base = cpp_lookup (pfile, sym, baselen, -1);
2831       if (! base) goto error;
2832       this = cpp_lookup (pfile, sym, thislen, -1);
2833       if (! this) goto error;
2834
2835       next = base;
2836       while (next->value.aschain != this)
2837         next = next->value.aschain;
2838
2839       next->value.aschain = this->value.aschain;
2840       delete_macro (this);
2841
2842       if (base->value.aschain == NULL)
2843         delete_macro (base);  /* Last answer for this predicate deleted. */
2844     }
2845   
2846   pfile->limit = (unsigned char *) sym; /* Pop */
2847   return 0;
2848  error:
2849   skip_rest_of_line (pfile);
2850   pfile->limit = (unsigned char *) sym; /* Pop */
2851   return 0;
2852 }
2853
2854 /* Process STR as if it appeared as the body of an #unassert. */
2855 void
2856 cpp_unassert (pfile, str)
2857      cpp_reader *pfile;
2858      unsigned char *str;
2859 {
2860   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
2861     {
2862       do_assert (pfile, NULL);
2863       cpp_pop_buffer (pfile);
2864     }
2865 }  
2866
2867 int
2868 cpp_read_check_assertion (pfile)
2869      cpp_reader *pfile;
2870 {
2871   U_CHAR *name = CPP_PWRITTEN (pfile);
2872   int result;
2873   HASHNODE *hp;
2874   
2875   FORWARD (1);  /* Skip '#' */
2876   cpp_skip_hspace (pfile);
2877   if (! parse_assertion (pfile))
2878     result = 0;
2879   else
2880     {
2881       hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
2882       result = (hp != 0);
2883     }
2884
2885   pfile->limit = name;
2886   return result;
2887 }
2888
2889 /* Remember the current position of PFILE.  */
2890
2891 void
2892 parse_set_mark (pfile)
2893      cpp_reader *pfile;
2894 {
2895   cpp_buffer *ip = CPP_BUFFER (pfile);
2896   if (ip->mark != -1)
2897       cpp_fatal (pfile,
2898                  "cpp internal error: ip->mark != -1 in parse_set_mark");
2899
2900   ip->mark = ip->cur - ip->buf;
2901 }
2902
2903 /* Clear the current mark - we no longer need it.  */
2904
2905 void
2906 parse_clear_mark (pfile)
2907      cpp_reader *pfile;
2908 {
2909   cpp_buffer *ip = CPP_BUFFER (pfile);
2910   if (ip->mark == -1)
2911       cpp_fatal (pfile,
2912                  "cpp internal error: ip->mark == -1 in parse_clear_mark");
2913
2914   ip->mark = -1;
2915 }
2916
2917 /* Backup the current position of PFILE to that saved in its mark,
2918    and clear the mark.  */
2919
2920 void
2921 parse_goto_mark (pfile)
2922      cpp_reader *pfile;
2923 {
2924   cpp_buffer *ip = CPP_BUFFER (pfile);
2925   if (ip->mark == -1)
2926       cpp_fatal (pfile,
2927                  "cpp internal error: ip->mark == -1 in parse_goto_mark");
2928
2929   ip->cur = ip->buf + ip->mark;
2930   ip->mark = -1;
2931 }
2932
2933 void
2934 cpp_print_file_and_line (pfile)
2935      cpp_reader *pfile;
2936 {
2937   cpp_buffer *ip = cpp_file_buffer (pfile);
2938
2939   if (ip != NULL)
2940     {
2941       long line, col;
2942       cpp_buf_line_and_col (ip, &line, &col);
2943       cpp_file_line_for_message (pfile, ip->nominal_fname,
2944                                  line, pfile->show_column ? col : -1);
2945     }
2946 }
2947
2948 static void
2949 v_cpp_error (pfile, msgid, ap)
2950   cpp_reader *pfile;
2951   const char *msgid;
2952   va_list ap;
2953 {
2954   cpp_print_containing_files (pfile);
2955   cpp_print_file_and_line (pfile);
2956   v_cpp_message (pfile, 1, msgid, ap);
2957 }
2958
2959 void
2960 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
2961 {
2962 #ifndef ANSI_PROTOTYPES
2963   cpp_reader *pfile;
2964   const char *msgid;
2965 #endif
2966   va_list ap;
2967
2968   VA_START(ap, msgid);
2969   
2970 #ifndef ANSI_PROTOTYPES
2971   pfile = va_arg (ap, cpp_reader *);
2972   msgid = va_arg (ap, const char *);
2973 #endif
2974
2975   v_cpp_error (pfile, msgid, ap);
2976   va_end(ap);
2977 }
2978
2979 /* Print error message but don't count it.  */
2980
2981 static void
2982 v_cpp_warning (pfile, msgid, ap)
2983   cpp_reader *pfile;
2984   const char *msgid;
2985   va_list ap;
2986 {
2987   if (CPP_OPTIONS (pfile)->inhibit_warnings)
2988     return;
2989
2990   if (CPP_OPTIONS (pfile)->warnings_are_errors)
2991     pfile->errors++;
2992
2993   cpp_print_containing_files (pfile);
2994   cpp_print_file_and_line (pfile);
2995   v_cpp_message (pfile, 0, msgid, ap);
2996 }
2997
2998 void
2999 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3000 {
3001 #ifndef ANSI_PROTOTYPES
3002   cpp_reader *pfile;
3003   const char *msgid;
3004 #endif
3005   va_list ap;
3006   
3007   VA_START (ap, msgid);
3008   
3009 #ifndef ANSI_PROTOTYPES
3010   pfile = va_arg (ap, cpp_reader *);
3011   msgid = va_arg (ap, const char *);
3012 #endif
3013
3014   v_cpp_warning (pfile, msgid, ap);
3015   va_end(ap);
3016 }
3017
3018 /* Print an error message and maybe count it.  */
3019
3020 void
3021 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3022 {
3023 #ifndef ANSI_PROTOTYPES
3024   cpp_reader *pfile;
3025   const char *msgid;
3026 #endif
3027   va_list ap;
3028   
3029   VA_START (ap, msgid);
3030   
3031 #ifndef ANSI_PROTOTYPES
3032   pfile = va_arg (ap, cpp_reader *);
3033   msgid = va_arg (ap, const char *);
3034 #endif
3035
3036   if (CPP_OPTIONS (pfile)->pedantic_errors)
3037     v_cpp_error (pfile, msgid, ap);
3038   else
3039     v_cpp_warning (pfile, msgid, ap);
3040   va_end(ap);
3041 }
3042
3043 static void
3044 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3045   cpp_reader * pfile;
3046   int line;
3047   int column;
3048   const char * msgid;
3049   va_list ap;
3050 {
3051   cpp_buffer *ip = cpp_file_buffer (pfile);
3052
3053   cpp_print_containing_files (pfile);
3054
3055   if (ip != NULL)
3056     cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3057
3058   v_cpp_message (pfile, 1, msgid, ap);
3059 }
3060
3061 void
3062 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3063                              const char *msgid, ...))
3064 {
3065 #ifndef ANSI_PROTOTYPES
3066   cpp_reader *pfile;
3067   int line;
3068   int column;
3069   const char *msgid;
3070 #endif
3071   va_list ap;
3072   
3073   VA_START (ap, msgid);
3074   
3075 #ifndef ANSI_PROTOTYPES
3076   pfile = va_arg (ap, cpp_reader *);
3077   line = va_arg (ap, int);
3078   column = va_arg (ap, int);
3079   msgid = va_arg (ap, const char *);
3080 #endif
3081
3082   v_cpp_error_with_line(pfile, line, column, msgid, ap);
3083   va_end(ap);
3084 }
3085
3086 static void
3087 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3088   cpp_reader * pfile;
3089   int line;
3090   int column;
3091   const char *msgid;
3092   va_list ap;
3093 {
3094   cpp_buffer *ip;
3095
3096   if (CPP_OPTIONS (pfile)->inhibit_warnings)
3097     return;
3098
3099   if (CPP_OPTIONS (pfile)->warnings_are_errors)
3100     pfile->errors++;
3101
3102   cpp_print_containing_files (pfile);
3103
3104   ip = cpp_file_buffer (pfile);
3105
3106   if (ip != NULL)
3107     cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3108
3109   v_cpp_message (pfile, 0, msgid, ap);
3110 }  
3111
3112 void
3113 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3114                                const char *msgid, ...))
3115 {
3116 #ifndef ANSI_PROTOTYPES
3117   cpp_reader *pfile;
3118   int line;
3119   int column;
3120   const char *msgid;
3121 #endif
3122   va_list ap;
3123   
3124   VA_START (ap, msgid);
3125   
3126 #ifndef ANSI_PROTOTYPES
3127   pfile = va_arg (ap, cpp_reader *);
3128   line = va_arg (ap, int);
3129   column = va_arg (ap, int);
3130   msgid = va_arg (ap, const char *);
3131 #endif
3132
3133   v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3134   va_end(ap);
3135 }
3136
3137 void
3138 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3139                                const char *msgid, ...))
3140 {
3141 #ifndef ANSI_PROTOTYPES
3142   cpp_reader *pfile;
3143   int line;
3144   int column;
3145   const char *msgid;
3146 #endif
3147   va_list ap;
3148   
3149   VA_START (ap, msgid);
3150   
3151 #ifndef ANSI_PROTOTYPES
3152   pfile = va_arg (ap, cpp_reader *);
3153   line = va_arg (ap, int);
3154   column = va_arg (ap, int);
3155   msgid = va_arg (ap, const char *);
3156 #endif
3157
3158   if (CPP_OPTIONS (pfile)->pedantic_errors)
3159     v_cpp_error_with_line (pfile, column, line, msgid, ap);
3160   else
3161     v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3162   va_end(ap);
3163 }
3164
3165 /* Report a warning (or an error if pedantic_errors)
3166    giving specified file name and line number, not current.  */
3167
3168 void
3169 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line,
3170                                         const char *msgid, ...))
3171 {
3172 #ifndef ANSI_PROTOTYPES
3173   cpp_reader *pfile;
3174   char *file;
3175   int line;
3176   const char *msgid;
3177 #endif
3178   va_list ap;
3179   
3180   VA_START (ap, msgid);
3181
3182 #ifndef ANSI_PROTOTYPES
3183   pfile = va_arg (ap, cpp_reader *);
3184   file = va_arg (ap, char *);
3185   line = va_arg (ap, int);
3186   msgid = va_arg (ap, const char *);
3187 #endif
3188
3189   if (!CPP_OPTIONS (pfile)->pedantic_errors
3190       && CPP_OPTIONS (pfile)->inhibit_warnings)
3191     return;
3192   if (file != NULL)
3193     cpp_file_line_for_message (pfile, file, line, -1);
3194   v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3195   va_end(ap);
3196 }
3197
3198 /* my_strerror - return the descriptive text associated with an
3199    `errno' code.  */
3200
3201 static char *
3202 my_strerror (errnum)
3203      int errnum;
3204 {
3205   char *result;
3206
3207 #ifndef VMS
3208 #ifndef HAVE_STRERROR
3209   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3210 #else
3211   result = strerror (errnum);
3212 #endif
3213 #else   /* VMS */
3214   /* VAXCRTL's strerror() takes an optional second argument, which only
3215      matters when the first argument is EVMSERR.  However, it's simplest
3216      just to pass it unconditionally.  `vaxc$errno' is declared in
3217      <errno.h>, and maintained by the library in parallel with `errno'.
3218      We assume that caller's `errnum' either matches the last setting of
3219      `errno' by the library or else does not have the value `EVMSERR'.  */
3220
3221   result = strerror (errnum, vaxc$errno);
3222 #endif
3223
3224   if (!result)
3225     result = "errno = ?";
3226
3227   return result;
3228 }
3229
3230 /* Error including a message from `errno'.  */
3231
3232 void
3233 cpp_error_from_errno (pfile, name)
3234      cpp_reader *pfile;
3235      const char *name;
3236 {
3237   cpp_message_from_errno (pfile, 1, name);
3238 }
3239
3240 void
3241 cpp_message_from_errno (pfile, is_error, name)
3242      cpp_reader *pfile;
3243      int is_error;
3244      const char *name;
3245 {
3246   int e = errno;
3247   cpp_buffer *ip = cpp_file_buffer (pfile);
3248
3249   cpp_print_containing_files (pfile);
3250
3251   if (ip != NULL)
3252     cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3253
3254   cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3255 }
3256
3257 void
3258 cpp_perror_with_name (pfile, name)
3259      cpp_reader *pfile;
3260      const char *name;
3261 {
3262   cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3263 }
3264
3265 /* TODO:
3266  * No pre-compiled header file support.
3267  *
3268  * Possibly different enum token codes for each C/C++ token.
3269  *
3270  * Find and cleanup remaining uses of static variables,
3271  *
3272  * Support -dM flag (dump_all_macros).
3273  *
3274  * Support for_lint flag.
3275  */