OSDN Git Service

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