Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / bin / epp / cpplib.c
1 /* CPP Library.
2  * Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3  * Written by Per Bothner, 1994-95.
4  * Based on CCCP program by by Paul Rubin, June 1986
5  * Adapted to ANSI C, Richard Stallman, Jan 1987
6  * Copyright (C) 2003-2011 Kim Woelders
7  * 
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2, or (at your option) any
11  * later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21  * 
22  * In other words, you are welcome to use, share and improve this program.
23  * You are forbidden to forbid anyone else to use, share and improve
24  * what you give them.   Help stamp out software-hoarding!  */
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #ifdef HAVE_ALLOCA_H
31 # include <alloca.h>
32 #elif defined __GNUC__
33 # define alloca __builtin_alloca
34 #elif defined _AIX
35 # define alloca __alloca
36 #elif defined _MSC_VER
37 # include <malloc.h>
38 # define alloca _alloca
39 #else
40 # include <stddef.h>
41 void *alloca (size_t);
42 #endif
43
44 #ifdef __EMX__
45 #include <strings.h>
46 #endif
47
48 #ifndef STANDARD_INCLUDE_DIR
49 #define STANDARD_INCLUDE_DIR "/usr/include"
50 #endif
51
52 #ifndef LOCAL_INCLUDE_DIR
53 #define LOCAL_INCLUDE_DIR "/usr/local/include"
54 #endif
55
56 #include "cpplib.h"
57 #include "cpphash.h"
58
59 /*
60  * On Windows, if the file is not opened in binary mode,
61  * read does not return the correct size, because of
62  * CR / LF translation.
63  */
64 #ifndef O_BINARY
65 # define O_BINARY 0
66 #endif
67
68 const char         *version_string = "0.0.0";
69
70 #ifndef STDC_VALUE
71 #define STDC_VALUE 1
72 #endif
73
74 /* By default, colon separates directories in a path.  */
75 #ifndef PATH_SEPARATOR
76 #define PATH_SEPARATOR ':'
77 #endif
78
79 #include <ctype.h>
80 #include <stdio.h>
81 #include <string.h>
82 #include <signal.h>
83 #include <stdlib.h>
84 #include <unistd.h>
85 #include <sys/types.h>
86 #include <sys/stat.h>
87 #include <fcntl.h>
88 #include <stdlib.h>
89
90 #ifndef VMS
91 #ifndef USG
92 #include <time.h>
93 #include <sys/time.h>           /* for __DATE__ and __TIME__ */
94 #ifdef HAVE_SYS_RESOURCE_H
95 # include <sys/resource.h>
96 #endif
97 #else
98 #include <sys/param.h>          /* CYGNUS LOCAL: shebs -noquiet */
99 #include <sys/times.h>
100 #include <time.h>
101 #include <fcntl.h>
102 #endif /* USG */
103 #endif /* not VMS */
104
105 /* This defines "errno" properly for VMS, and gives us EACCES. */
106 #include <errno.h>
107
108 #ifndef O_RDONLY
109 #define O_RDONLY 0
110 #endif
111
112 #undef MIN
113 #undef MAX
114 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
115 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
116
117 #ifndef S_ISREG
118 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
119 #endif
120
121 #ifndef S_ISDIR
122 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
123 #endif
124
125 /* Define a generic NULL if one hasn't already been defined.  */
126
127 #ifndef GENERIC_PTR
128 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
129 #define GENERIC_PTR void *
130 #else
131 #define GENERIC_PTR char *
132 #endif
133 #endif
134
135 #ifndef INCLUDE_LEN_FUDGE
136 #define INCLUDE_LEN_FUDGE 0
137 #endif
138
139 #define USE_FILE_NAME_MAPS 0
140
141 /* Symbols to predefine.  */
142
143 #ifdef CPP_PREDEFINES
144 static const char  *predefs = CPP_PREDEFINES;
145
146 #else
147 static const char  *predefs = "";
148
149 #endif
150
151 /* We let tm.h override the types used here, to handle trivial differences
152  * such as the choice of unsigned int or long unsigned int for size_t.
153  * When machines start needing nontrivial differences in the size type,
154  * it would be best to do something here to figure out automatically
155  * from other information what type to use.  */
156
157 /* The string value for __SIZE_TYPE__.  */
158
159 #ifndef SIZE_TYPE
160 #define SIZE_TYPE "long unsigned int"
161 #endif
162
163 /* The string value for __PTRDIFF_TYPE__.  */
164
165 #ifndef PTRDIFF_TYPE
166 #define PTRDIFF_TYPE "long int"
167 #endif
168
169 /* The string value for __WCHAR_TYPE__.  */
170
171 #ifndef WCHAR_TYPE
172 #define WCHAR_TYPE "int"
173 #endif
174 #define CPP_WCHAR_TYPE(PFILE) \
175         (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
176
177 /* The string value for __USER_LABEL_PREFIX__ */
178
179 #ifndef USER_LABEL_PREFIX
180 #define USER_LABEL_PREFIX ""
181 #endif
182
183 /* The string value for __REGISTER_PREFIX__ */
184
185 #ifndef REGISTER_PREFIX
186 #define REGISTER_PREFIX ""
187 #endif
188
189 struct directive {
190    int                 length;
191    int                 (*func) (cpp_reader * pfile, struct directive * keyword,
192                                 unsigned char *buf, unsigned char *limit);
193    const char         *name;
194    enum node_type      type;
195    char                command_reads_line;
196    char                traditional_comments;
197    char                pass_thru;
198 };
199
200 /* In the definition of a #assert name, this structure forms
201  * a list of the individual values asserted.
202  * Each value is itself a list of "tokens".
203  * These are strings that are compared by name.  */
204
205 struct tokenlist_list {
206    struct tokenlist_list *next;
207    struct arglist     *tokens;
208 };
209
210 struct assertion_hashnode {
211    struct assertion_hashnode *next;     /* double links for easy deletion */
212    struct assertion_hashnode *prev;
213    /* also, a back pointer to this node's hash
214     * chain is kept, in case the node is the head
215     * of the chain and gets deleted. */
216    struct assertion_hashnode **bucket_hdr;
217    int                 length;  /* length of token, for quick comparison */
218    char               *name;    /* the actual name */
219    /* List of token-sequences.  */
220    struct tokenlist_list *value;
221 };
222
223 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[(unsigned char)(*p)]) p++; } while (0)
224 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[(unsigned char)(*p)]) p++; } while (0)
225
226 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
227 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
228 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
229 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
230 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
231  * (Note that it is false while we're expanding marco *arguments*.) */
232 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
233
234 /* Move all backslash-newline pairs out of embarrassing places.
235  * Exchange all such pairs following BP
236  * with any potentially-embarrassing characters that follow them.
237  * Potentially-embarrassing characters are / and *
238  * (because a backslash-newline inside a comment delimiter
239  * would cause it not to be recognized).  */
240
241 #define NEWLINE_FIX \
242   do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
243
244 /* Same, but assume we've already read the potential '\\' into C. */
245 #define NEWLINE_FIX1(C) do { \
246     while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
247   } while(0)
248
249 /* Name under which this program was invoked.  */
250
251 char               *progname;
252
253 struct cpp_pending {
254    struct cpp_pending *next;
255    const char         *cmd;
256    const char         *arg;
257 };
258
259 /* Structure returned by create_definition */
260 typedef struct {
261    struct definition  *defn;
262    char               *symnam;
263    int                 symlen;
264 } MACRODEF;
265
266 /* Forward declarations.  */
267 typedef struct file_name_list file_name_list;
268
269 static void         add_import(cpp_reader * pfile, int fd, char *fname);
270 static int          finclude(cpp_reader * pfile, int f, const char *fname,
271                              int system_header_p, file_name_list * dirptr);
272 static void         validate_else(cpp_reader * pfile, const char *directive);
273 static int          comp_def_part(int first, unsigned char *beg1, int len1,
274                                   unsigned char *beg2, int len2, int last);
275 static int          lookup_import(cpp_reader * pfile, char *filename,
276                                   file_name_list * searchptr);
277 static int          redundant_include_p(cpp_reader * pfile, char *name);
278
279 static int          is_system_include(cpp_reader * pfile, char *filename);
280
281 static int          open_include_file(cpp_reader * pfile, char *filename,
282                                       file_name_list * searchptr);
283 static int          check_macro_name(cpp_reader * pfile, unsigned char *symname,
284                                      const char *usage);
285
286 static int          compare_token_lists(struct arglist *l1, struct arglist *l2);
287 static HOST_WIDE_INT eval_if_expression(cpp_reader * pfile, unsigned char *buf,
288                                         int length);
289
290 static int          file_size_and_mode(int fd, int *mode_pointer,
291                                        long int *size_pointer);
292 static struct arglist *read_token_list(cpp_reader * pfile, int *error_flag);
293 static void         free_token_list(struct arglist *tokens);
294 static int          safe_read(int desc, char *ptr, int len);
295 static void         push_macro_expansion(cpp_reader * pfile,
296                                          unsigned char *x,
297                                          int xbuf_len, HASHNODE * hp);
298
299 static struct cpp_pending *nreverse_pending(struct cpp_pending *list);
300 static char        *savestring(const char *input);
301
302 static void         conditional_skip(cpp_reader * pfile, int skip,
303                                      enum node_type type,
304                                      unsigned char *control_macro);
305 static void         skip_if_group(cpp_reader * pfile, int any);
306
307 static void         cpp_error_with_line(cpp_reader * pfile, int line,
308                                         int column, const char *msg);
309 static void         cpp_pedwarn_with_line(cpp_reader * pfile, int line,
310                                           int column, const char *msg);
311 static void         cpp_pedwarn_with_file_and_line(cpp_reader * pfile,
312                                                    const char *file, int line,
313                                                    const char *msg,
314                                                    const char *arg1,
315                                                    const char *arg2,
316                                                    const char *arg3);
317 static void         cpp_error_from_errno(cpp_reader * pfile, const char *name);
318
319 static cpp_buffer  *cpp_push_buffer(cpp_reader * pfile, unsigned char *buffer,
320                                     long length);
321 static cpp_buffer  *cpp_pop_buffer(cpp_reader * pfile);
322
323 /* Last arg to output_line_command.  */
324 enum file_change_code {
325    same_file, enter_file, leave_file
326 };
327
328 /* These functions are declared to return int instead of void since they
329  * are going to be placed in a table and some old compilers have trouble with
330  * pointers to functions returning void.  */
331
332 static int          do_define(cpp_reader * pfile, struct directive *keyword,
333                               unsigned char *buf, unsigned char *limit);
334
335 static int          do_line(cpp_reader * pfile, struct directive *keyword,
336                             unsigned char *unused1, unsigned char *unused2);
337
338 static int          do_include(cpp_reader * pfile, struct directive *keyword,
339                                unsigned char *unused1, unsigned char *unused2);
340
341 static int          do_undef(cpp_reader * pfile, struct directive *keyword,
342                              unsigned char *buf, unsigned char *limit);
343
344 static int          do_error(cpp_reader * pfile, struct directive *keyword,
345                              unsigned char *buf, unsigned char *limit);
346
347 static int          do_pragma(cpp_reader * pfile, struct directive *keyword,
348                               unsigned char *buf, unsigned char *limit);
349
350 static int          do_ident(cpp_reader * pfile, struct directive *keyword,
351                              unsigned char *buf, unsigned char *limit);
352
353 static int          do_if(cpp_reader * pfile, struct directive *keyword,
354                           unsigned char *buf, unsigned char *limit);
355
356 static int          do_xifdef(cpp_reader * pfile, struct directive *keyword,
357                               unsigned char *buf, unsigned char *limit);
358
359 static int          do_else(cpp_reader * pfile, struct directive *keyword,
360                             unsigned char *buf, unsigned char *limit);
361
362 static int          do_elif(cpp_reader * pfile, struct directive *keyword,
363                             unsigned char *buf, unsigned char *limit);
364
365 static int          do_endif(cpp_reader * pfile, struct directive *keyword,
366                              unsigned char *buf, unsigned char *limit);
367
368 static int          do_assert(cpp_reader * pfile, struct directive *keyword,
369                               unsigned char *buf, unsigned char *limit);
370
371 static int          do_unassert(cpp_reader * pfile, struct directive *keyword,
372                                 unsigned char *buf, unsigned char *limit);
373
374 static int          do_warning(cpp_reader * pfile, struct directive *keyword,
375                                unsigned char *buf, unsigned char *limit);
376
377 struct arglist     *reverse_token_list(struct arglist *tokens);
378
379 static int          parse_name(cpp_reader * pfile, int c);
380
381 static void         parse_set_mark(struct parse_marker *pmark,
382                                    cpp_reader * pfile);
383 static void         parse_clear_mark(struct parse_marker *pmark);
384 static void         parse_goto_mark(struct parse_marker *pmark,
385                                     cpp_reader * pfile);
386 static void         parse_move_mark(struct parse_marker *pmark,
387                                     cpp_reader * pfile);
388
389 struct file_name_list {
390    file_name_list     *next;
391    char               *fname;
392    /* If the following is nonzero, it is a macro name.
393     * Don't include the file again if that macro is defined.  */
394    unsigned char      *control_macro;
395    /* If the following is nonzero, it is a C-language system include
396     * directory.  */
397    int                 c_system_include_path;
398    /* Mapping of file names for this directory.  */
399    struct file_name_map *name_map;
400    /* Non-zero if name_map is valid.  */
401    int                 got_name_map;
402 };
403
404 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
405  * via the same directory as the file that #included it. */
406 #define SELF_DIR_DUMMY ((file_name_list*)(~0))
407
408 /* #include "file" looks in source file dir, then stack. */
409 /* #include <file> just looks in the stack. */
410 /* -I directories are added to the end, then the defaults are added. */
411 /* The */
412 static struct default_include {
413    const char         *fname;   /* The name of the directory.  */
414    int                 cplusplus;       /* Only look here if we're compiling C++.  */
415    int                 cxx_aware;       /* Includes in this directory don't need to
416                                          * be wrapped in extern "C" when compiling
417                                          * C++.  */
418 } include_defaults_array[]
419 #ifdef INCLUDE_DEFAULTS
420    = INCLUDE_DEFAULTS;
421
422 #else
423    =
424 {
425    /* Pick up GNU C++ specific include files.  */
426    {
427    GPLUSPLUS_INCLUDE_DIR, 1, 1}
428    ,
429 #ifdef CROSS_COMPILE
430       /* This is the dir for fixincludes.  Put it just before
431        * the files that we fix.  */
432    {
433    GCC_INCLUDE_DIR, 0, 0}
434    ,
435       /* For cross-compilation, this dir name is generated
436        * automatically in Makefile.in.  */
437    {
438    CROSS_INCLUDE_DIR, 0, 0}
439    ,
440       /* This is another place that the target system's headers might be.  */
441    {
442    TOOL_INCLUDE_DIR, 0, 1}
443    ,
444 #else /* not CROSS_COMPILE */
445       /* This should be /usr/local/include and should come before
446        * the fixincludes-fixed header files.  */
447    {
448    LOCAL_INCLUDE_DIR, 0, 1}
449    ,
450       /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
451        * Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
452    {
453    TOOL_INCLUDE_DIR, 0, 1}
454    ,
455       /* This is the dir for fixincludes.  Put it just before
456        * the files that we fix.  */
457    {
458    GCC_INCLUDE_DIR, 0, 0}
459    ,
460       /* Some systems have an extra dir of include files.  */
461 #ifdef SYSTEM_INCLUDE_DIR
462    {
463    SYSTEM_INCLUDE_DIR, 0, 0}
464    ,
465 #endif
466    {
467    STANDARD_INCLUDE_DIR, 0, 0}
468    ,
469 #endif /* not CROSS_COMPILE */
470    {
471    0, 0, 0}
472 };
473
474 #endif /* no INCLUDE_DEFAULTS */
475
476 /* Here is the actual list of #-directives, most-often-used first.
477  * The initialize_builtins function assumes #define is the very first.  */
478
479 static struct directive directive_table[] = {
480    {6, do_define, "define", T_DEFINE, 0, 1, 0},
481    {5, do_xifdef, "ifdef", T_IFDEF, 1, 0, 0},
482    {6, do_xifdef, "ifndef", T_IFNDEF, 1, 0, 0},
483    {7, do_include, "include", T_INCLUDE, 1, 0, 0},
484    {12, do_include, "include_next", T_INCLUDE_NEXT, 1, 0, 0},
485    {6, do_include, "import", T_IMPORT, 1, 0, 0},
486    {5, do_endif, "endif", T_ENDIF, 1, 0, 0},
487    {4, do_else, "else", T_ELSE, 1, 0, 0},
488    {2, do_if, "if", T_IF, 1, 0, 0},
489    {4, do_elif, "elif", T_ELIF, 1, 0, 0},
490    {5, do_undef, "undef", T_UNDEF, 0, 0, 0},
491    {5, do_error, "error", T_ERROR, 0, 0, 0},
492    {7, do_warning, "warning", T_WARNING, 0, 0, 0},
493    {6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
494    {4, do_line, "line", T_LINE, 1, 0, 0},
495    {5, do_ident, "ident", T_IDENT, 1, 0, 1},
496 #ifdef SCCS_DIRECTIVE
497    {4, do_sccs, "sccs", T_SCCS, 0, 0, 0},
498 #endif
499    {6, do_assert, "assert", T_ASSERT, 1, 0, 0},
500    {8, do_unassert, "unassert", T_UNASSERT, 1, 0, 0},
501    {-1, 0, "", T_UNUSED, 0, 0, 0},
502 };
503
504 /* table to tell if char can be part of a C identifier. */
505 unsigned char       is_idchar[256];
506
507 /* table to tell if char can be first char of a c identifier. */
508 unsigned char       is_idstart[256];
509
510 /* table to tell if c is horizontal space.  */
511 unsigned char       is_hor_space[256];
512
513 /* table to tell if c is horizontal or vertical space.  */
514 static unsigned char is_space[256];
515
516 /* Initialize syntactic classifications of characters.  */
517
518 static void
519 initialize_char_syntax(struct cpp_options *opts)
520 {
521    int                 i;
522
523    /*
524     * Set up is_idchar and is_idstart tables.  These should be
525     * faster than saying (is_alpha (c) || c == '_'), etc.
526     * Set up these things before calling any routines tthat
527     * refer to them.
528     */
529    for (i = 'a'; i <= 'z'; i++)
530      {
531         is_idchar[i - 'a' + 'A'] = 1;
532         is_idchar[i] = 1;
533         is_idstart[i - 'a' + 'A'] = 1;
534         is_idstart[i] = 1;
535      }
536    for (i = '0'; i <= '9'; i++)
537       is_idchar[i] = 1;
538    is_idchar[(unsigned char)'_'] = 1;
539    is_idstart[(unsigned char)'_'] = 1;
540    is_idchar[(unsigned char)'$'] = opts->dollars_in_ident;
541    is_idstart[(unsigned char)'$'] = opts->dollars_in_ident;
542
543    /* horizontal space table */
544    is_hor_space[(unsigned char)' '] = 1;
545    is_hor_space[(unsigned char)'\t'] = 1;
546    is_hor_space[(unsigned char)'\v'] = 1;
547    is_hor_space[(unsigned char)'\f'] = 1;
548    is_hor_space[(unsigned char)'\r'] = 1;
549
550    is_space[(unsigned char)' '] = 1;
551    is_space[(unsigned char)'\t'] = 1;
552    is_space[(unsigned char)'\v'] = 1;
553    is_space[(unsigned char)'\f'] = 1;
554    is_space[(unsigned char)'\n'] = 1;
555    is_space[(unsigned char)'\r'] = 1;
556 }
557
558 /* Place into PFILE a quoted string representing the string SRC.
559  * Caller must reserve enough space in pfile->token_buffer. */
560 static void
561 quote_string(cpp_reader * pfile, const char *src)
562 {
563    unsigned char       c;
564
565    CPP_PUTC_Q(pfile, '\"');
566    for (;;)
567       switch ((c = *src++))
568         {
569         default:
570            if (isprint(c))
571               CPP_PUTC_Q(pfile, c);
572            else
573              {
574                 sprintf((char *)CPP_PWRITTEN(pfile), "\\%03o", c);
575                 CPP_ADJUST_WRITTEN(pfile, 4);
576              }
577            break;
578
579         case '\"':
580         case '\\':
581            CPP_PUTC_Q(pfile, '\\');
582            CPP_PUTC_Q(pfile, c);
583            break;
584
585         case '\0':
586            CPP_PUTC_Q(pfile, '\"');
587            CPP_NUL_TERMINATE_Q(pfile);
588            return;
589         }
590 }
591
592 /* Make sure PFILE->token_buffer will hold at least N more chars. */
593
594 void
595 cpp_grow_buffer(cpp_reader * pfile, long n)
596 {
597    long                old_written = CPP_WRITTEN(pfile);
598
599    pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
600    pfile->token_buffer =
601       (unsigned char *)xrealloc(pfile->token_buffer, pfile->token_buffer_size);
602    CPP_SET_WRITTEN(pfile, old_written);
603 }
604
605 /*
606  * process a given definition string, for initialization
607  * If STR is just an identifier, define it with value 1.
608  * If STR has anything after the identifier, then it should
609  * be identifier=definition.
610  */
611
612 void
613 cpp_define(cpp_reader * pfile, unsigned char *str)
614 {
615    unsigned char      *buf, *p;
616
617    buf = str;
618    p = str;
619    if (!is_idstart[*p])
620      {
621         cpp_error(pfile, "malformed option `-D %s'", str);
622         return;
623      }
624    while (is_idchar[*++p])
625       ;
626    if (*p == 0)
627      {
628         buf = (unsigned char *)alloca(p - buf + 4);
629         strcpy((char *)buf, (const char *)str);
630         strcat((char *)buf, " 1");
631      }
632    else if (*p != '=')
633      {
634         cpp_error(pfile, "malformed option `-D %s'", str);
635         return;
636      }
637    else
638      {
639         unsigned char      *q;
640
641         /* Copy the entire option so we can modify it.  */
642         buf = (unsigned char *)alloca(2 * strlen((char *)str) + 1);
643         strncpy((char *)buf, (const char *)str, p - str);
644         /* Change the = to a space.  */
645         buf[p - str] = ' ';
646         /* Scan for any backslash-newline and remove it.  */
647         p++;
648         q = &buf[p - str];
649         while (*p)
650           {
651              if (*p == '\\' && p[1] == '\n')
652                 p += 2;
653              else
654                 *q++ = *p++;
655           }
656         *q = 0;
657      }
658
659    do_define(pfile, NULL, buf, buf + strlen((char *)buf));
660 }
661
662 /* Process the string STR as if it appeared as the body of a #assert.
663  * OPTION is the option name for which STR was the argument.  */
664
665 static void
666 make_assertion(cpp_reader * pfile, const char *option, const char *str)
667 {
668    unsigned char      *buf, *p, *q;
669
670    /* Copy the entire option so we can modify it.  */
671    buf = (unsigned char *)alloca(strlen((char *)str) + 1);
672    strcpy((char *)buf, (const char *)str);
673    /* Scan for any backslash-newline and remove it.  */
674    p = q = buf;
675    while (*p)
676      {
677         *q++ = *p++;
678      }
679    *q = 0;
680
681    p = buf;
682    if (!is_idstart[*p])
683      {
684         cpp_error(pfile, "malformed option `%s %s'", option, str);
685         return;
686      }
687    while (is_idchar[*++p])
688       ;
689    while (*p == ' ' || *p == '\t')
690       p++;
691    if (!(*p == 0 || *p == '('))
692      {
693         cpp_error(pfile, "malformed option `%s %s'", option, str);
694         return;
695      }
696    cpp_push_buffer(pfile, buf, strlen((char *)buf));
697    do_assert(pfile, NULL, NULL, NULL);
698    cpp_pop_buffer(pfile);
699 }
700
701 /* Append a chain of `file_name_list's
702  * to the end of the main include chain.
703  * FIRST is the beginning of the chain to append, and LAST is the end.  */
704
705 static void
706 append_include_chain(cpp_reader * pfile, file_name_list * first,
707                      file_name_list * last)
708 {
709    struct cpp_options *opts = CPP_OPTIONS(pfile);
710    file_name_list     *dir;
711
712    if (!first || !last)
713       return;
714
715    if (!opts->include)
716       opts->include = first;
717    else
718       opts->last_include->next = first;
719
720    if (!opts->first_bracket_include)
721       opts->first_bracket_include = first;
722
723    for (dir = first;; dir = dir->next)
724      {
725         int                 len = strlen(dir->fname) + INCLUDE_LEN_FUDGE;
726
727         if (len > pfile->max_include_len)
728            pfile->max_include_len = len;
729         if (dir == last)
730            break;
731      }
732
733    last->next = NULL;
734    opts->last_include = last;
735 }
736
737 /* Add output to `deps_buffer' for the -M switch.
738  * STRING points to the text to be output.
739  * SPACER is ':' for targets, ' ' for dependencies, zero for text
740  * to be inserted literally.  */
741
742 static void
743 deps_output(cpp_reader * pfile, const char *string, int spacer)
744 {
745    int                 size = strlen(string);
746
747    if (size == 0)
748       return;
749
750 #ifndef MAX_OUTPUT_COLUMNS
751 #define MAX_OUTPUT_COLUMNS 72
752 #endif
753    if (spacer
754        && pfile->deps_column > 0
755        && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
756      {
757         deps_output(pfile, " \\\n  ", 0);
758         pfile->deps_column = 0;
759      }
760    if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
761      {
762         pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
763         pfile->deps_buffer = (char *)xrealloc(pfile->deps_buffer,
764                                               pfile->deps_allocated_size);
765      }
766    if (spacer == ' ' && pfile->deps_column > 0)
767       pfile->deps_buffer[pfile->deps_size++] = ' ';
768    memcpy(&pfile->deps_buffer[pfile->deps_size], string, size);
769    pfile->deps_size += size;
770    pfile->deps_column += size;
771    if (spacer == ':')
772       pfile->deps_buffer[pfile->deps_size++] = ':';
773    pfile->deps_buffer[pfile->deps_size] = 0;
774 }
775
776 /* Given a colon-separated list of file names PATH,
777  * add all the names to the search path for include files.  */
778
779 static void
780 path_include(cpp_reader * pfile, char *path)
781 {
782    char               *p;
783
784    p = path;
785
786    if (*p)
787       while (1)
788         {
789            char               *q = p;
790            char               *name;
791            file_name_list     *dirtmp;
792
793            /* Find the end of this name.  */
794            while (*q != 0 && *q != PATH_SEPARATOR)
795               q++;
796            if (p == q)
797              {
798                 /* An empty name in the path stands for the current directory.  */
799                 name = (char *)xmalloc(2);
800                 name[0] = '.';
801                 name[1] = 0;
802              }
803            else
804              {
805                 /* Otherwise use the directory that is named.  */
806                 name = (char *)xmalloc(q - p + 1);
807                 memcpy(name, p, q - p);
808                 name[q - p] = 0;
809              }
810
811            dirtmp = (file_name_list *) xmalloc(sizeof(file_name_list));
812
813            dirtmp->next = 0;    /* New one goes on the end */
814            dirtmp->control_macro = 0;
815            dirtmp->c_system_include_path = 0;
816            dirtmp->fname = name;
817            dirtmp->got_name_map = 0;
818            append_include_chain(pfile, dirtmp, dirtmp);
819
820            /* Advance past this name.  */
821            p = q;
822            if (*p == 0)
823               break;
824            /* Skip the colon.  */
825            p++;
826         }
827 }
828
829 void
830 init_parse_options(struct cpp_options *opts)
831 {
832    memset((char *)opts, 0, sizeof *opts);
833    opts->in_fname = NULL;
834    opts->out_fname = NULL;
835
836    /* Initialize is_idchar to allow $.  */
837    opts->dollars_in_ident = 1;
838    initialize_char_syntax(opts);
839    opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
840
841    opts->no_line_commands = 0;
842    opts->no_trigraphs = 1;
843    opts->put_out_comments = 0;
844    opts->print_include_names = 0;
845    opts->dump_macros = dump_none;
846    opts->no_output = 0;
847    opts->cplusplus = 0;
848    opts->cplusplus_comments = 1;
849
850    opts->verbose = 0;
851    opts->objc = 0;
852    opts->lang_asm = 0;
853    opts->for_lint = 0;
854    opts->chill = 0;
855    opts->pedantic_errors = 0;
856    opts->inhibit_warnings = 0;
857    opts->warn_comments = 0;
858    opts->warn_import = 1;
859    opts->warnings_are_errors = 0;
860 }
861
862 static enum cpp_token
863 null_underflow(cpp_reader * pfile __UNUSED__)
864 {
865    return CPP_EOF;
866 }
867
868 static int
869 null_cleanup(cpp_buffer * pbuf __UNUSED__, cpp_reader * pfile __UNUSED__)
870 {
871    return 0;
872 }
873
874 static int
875 macro_cleanup(cpp_buffer * pbuf, cpp_reader * pfile __UNUSED__)
876 {
877    HASHNODE           *macro = (HASHNODE *) pbuf->data;
878
879    if (macro->type == T_DISABLED)
880       macro->type = T_MACRO;
881    if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
882       free(pbuf->buf);
883    return 0;
884 }
885
886 static int
887 file_cleanup(cpp_buffer * pbuf, cpp_reader * pfile __UNUSED__)
888 {
889    if (pbuf->buf)
890      {
891         free(pbuf->buf);
892         pbuf->buf = 0;
893      }
894    return 0;
895 }
896
897 /* Assuming we have read '/'.
898  * If this is the start of a comment (followed by '*' or '/'),
899  * skip to the end of the comment, and return ' '.
900  * Return EOF if we reached the end of file before the end of the comment.
901  * If not the start of a comment, return '/'. */
902
903 static int
904 skip_comment(cpp_reader * pfile, long *linep)
905 {
906    int                 c = 0;
907
908    while (PEEKC() == '\\' && PEEKN(1) == '\n')
909      {
910         if (linep)
911            (*linep)++;
912         FORWARD(2);
913      }
914    if (PEEKC() == '*')
915      {
916         FORWARD(1);
917         for (;;)
918           {
919              int                 prev_c = c;
920
921              c = GETC();
922              if (c == EOF)
923                 return EOF;
924              while (c == '\\' && PEEKC() == '\n')
925                {
926                   if (linep)
927                      (*linep)++;
928                   FORWARD(1), c = GETC();
929                }
930              if (prev_c == '*' && c == '/')
931                 return ' ';
932              if (c == '\n' && linep)
933                 (*linep)++;
934           }
935      }
936    else if (PEEKC() == '/' && CPP_OPTIONS(pfile)->cplusplus_comments)
937      {
938         FORWARD(1);
939         for (;;)
940           {
941              c = GETC();
942              if (c == EOF)
943                 return ' ';     /* Allow // to be terminated by EOF. */
944              while (c == '\\' && PEEKC() == '\n')
945                {
946                   FORWARD(1);
947                   c = GETC();
948                   if (linep)
949                      (*linep)++;
950                }
951              if (c == '\n')
952                {
953                   /* Don't consider final '\n' to be part of comment. */
954                   FORWARD(-1);
955                   return ' ';
956                }
957           }
958      }
959    else
960       return '/';
961 }
962
963 /* Skip whitespace \-newline and comments.  Does not macro-expand.  */
964 void
965 cpp_skip_hspace(cpp_reader * pfile)
966 {
967    while (1)
968      {
969         int                 c = PEEKC();
970
971         if (c == EOF)
972            return;              /* FIXME */
973         if (is_hor_space[c])
974           {
975              if ((c == '\f' || c == '\v') && CPP_PEDANTIC(pfile))
976                 cpp_pedwarn(pfile, "%s in preprocessing directive",
977                             c == '\f' ? "formfeed" : "vertical tab");
978              FORWARD(1);
979           }
980         else if (c == '/')
981           {
982              FORWARD(1);
983              c = skip_comment(pfile, NULL);
984              if (c == '/')
985                 FORWARD(-1);
986              if (c == EOF || c == '/')
987                 return;
988           }
989         else if (c == '\\' && PEEKN(1) == '\n')
990           {
991              FORWARD(2);
992           }
993         else if (c == '@' && CPP_BUFFER(pfile)->has_escapes
994                  && is_hor_space[PEEKN(1)])
995           {
996              FORWARD(1);
997           }
998         else
999            return;
1000      }
1001 }
1002
1003 /* Read the rest of the current line.
1004  * The line is appended to PFILE's output buffer. */
1005
1006 static void
1007 copy_rest_of_line(cpp_reader * pfile)
1008 {
1009    struct cpp_options *opts = CPP_OPTIONS(pfile);
1010
1011    for (;;)
1012      {
1013         int                 c = GETC();
1014         int                 nextc;
1015
1016         switch (c)
1017           {
1018           case EOF:
1019              goto end_directive;
1020           case '\\':
1021              if (PEEKC() == '\n')
1022                {
1023                   FORWARD(1);
1024                   continue;
1025                }
1026           case '\'':
1027           case '\"':
1028              goto scan_directive_token;
1029              break;
1030           case '/':
1031              nextc = PEEKC();
1032              if (nextc == '*' || (opts->cplusplus_comments && nextc == '*'))
1033                 goto scan_directive_token;
1034              break;
1035           case '\f':
1036           case '\v':
1037              if (CPP_PEDANTIC(pfile))
1038                 cpp_pedwarn(pfile, "%s in preprocessing directive",
1039                             c == '\f' ? "formfeed" : "vertical tab");
1040              break;
1041
1042           case '\n':
1043              FORWARD(-1);
1044              goto end_directive;
1045            scan_directive_token:
1046              FORWARD(-1);
1047              cpp_get_token(pfile);
1048              continue;
1049           }
1050         CPP_PUTC(pfile, c);
1051      }
1052  end_directive:;
1053    CPP_NUL_TERMINATE(pfile);
1054 }
1055
1056 void
1057 skip_rest_of_line(cpp_reader * pfile)
1058 {
1059    long                old = CPP_WRITTEN(pfile);
1060
1061    copy_rest_of_line(pfile);
1062    CPP_SET_WRITTEN(pfile, old);
1063 }
1064
1065 /* Handle a possible # directive.
1066  * '#' has already been read.  */
1067
1068 static int
1069 handle_directive(cpp_reader * pfile)
1070 {
1071    int                 c;
1072    struct directive   *kt;
1073    int                 ident_length;
1074    long                after_ident = 0;
1075    unsigned char      *ident, *line_end;
1076    long                old_written = CPP_WRITTEN(pfile);
1077
1078    cpp_skip_hspace(pfile);
1079
1080    c = PEEKC();
1081    if (c >= '0' && c <= '9')
1082      {
1083         /* Handle # followed by a line number.  */
1084         if (CPP_PEDANTIC(pfile))
1085            cpp_pedwarn(pfile, "`#' followed by integer");
1086         do_line(pfile, NULL, NULL, NULL);
1087         goto done_a_directive;
1088      }
1089    /* Now find the directive name. */
1090    CPP_PUTC(pfile, '#');
1091    parse_name(pfile, GETC());
1092    ident = pfile->token_buffer + old_written + 1;
1093    ident_length = CPP_PWRITTEN(pfile) - ident;
1094    if (ident_length == 0 && PEEKC() == '\n')
1095      {
1096         /* A line of just `#' becomes blank.  */
1097         goto done_a_directive;
1098      }
1099    /*
1100     * Decode the keyword and call the appropriate expansion
1101     * routine, after moving the input pointer up to the next line.
1102     */
1103    for (kt = directive_table;; kt++)
1104      {
1105         if (kt->length <= 0)
1106            goto not_a_directive;
1107         if (kt->length == ident_length
1108             && !strncmp(kt->name, (const char *)ident, ident_length))
1109            break;
1110      }
1111
1112    if (!kt->command_reads_line)
1113      {
1114         /* Nonzero means do not delete comments within the directive.
1115          * #define needs this when -traditional.  */
1116         int                 comments = 0;
1117         int                 save_put_out_comments =
1118            CPP_OPTIONS(pfile)->put_out_comments;
1119
1120         CPP_OPTIONS(pfile)->put_out_comments = comments;
1121         after_ident = CPP_WRITTEN(pfile);
1122         copy_rest_of_line(pfile);
1123         CPP_OPTIONS(pfile)->put_out_comments = save_put_out_comments;
1124      }
1125    /* For #pragma and #define, we may want to pass through the directive.
1126     * Other directives may create output, but we don't want the directive
1127     * itself out, so we pop it now.  For example #include may write a #line
1128     * command (see comment in do_include), and conditionals may emit
1129     * #failed ... #endfailed stuff.  But note that popping the buffer
1130     * means the parameters to kt->func may point after pfile->limit
1131     * so these parameters are invalid as soon as something gets appended
1132     * to the token_buffer.  */
1133
1134    line_end = CPP_PWRITTEN(pfile);
1135    if (!kt->pass_thru && kt->type != T_DEFINE)
1136       CPP_SET_WRITTEN(pfile, old_written);
1137
1138    (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1139    if (kt->pass_thru
1140        || (kt->type == T_DEFINE
1141            && CPP_OPTIONS(pfile)->dump_macros == dump_definitions))
1142      {
1143         /* Just leave the entire #define in the output stack. */
1144      }
1145    else if (kt->type == T_DEFINE
1146             && CPP_OPTIONS(pfile)->dump_macros == dump_names)
1147      {
1148         unsigned char      *p = pfile->token_buffer + old_written + 7;  /* Skip "#define". */
1149
1150         SKIP_WHITE_SPACE(p);
1151         while (is_idchar[*p])
1152            p++;
1153         pfile->limit = p;
1154         CPP_PUTC(pfile, '\n');
1155      }
1156    else if (kt->type == T_DEFINE)
1157       CPP_SET_WRITTEN(pfile, old_written);
1158  done_a_directive:
1159    return 1;
1160
1161  not_a_directive:
1162    return 0;
1163 }
1164
1165 /* Pass a directive through to the output file.
1166  * BUF points to the contents of the directive, as a contiguous string.
1167  * LIMIT points to the first character past the end of the directive.
1168  * KEYWORD is the keyword-table entry for the directive.  */
1169
1170 static void
1171 pass_thru_directive(char *buf, char *limit, cpp_reader * pfile,
1172                     struct directive *keyword)
1173 {
1174    unsigned            keyword_length = keyword->length;
1175
1176    CPP_RESERVE(pfile, 1 + keyword_length + (limit - buf));
1177    CPP_PUTC_Q(pfile, '#');
1178    CPP_PUTS_Q(pfile, keyword->name, keyword_length);
1179    if (limit != buf && buf[0] != ' ')
1180       CPP_PUTC_Q(pfile, ' ');
1181    CPP_PUTS_Q(pfile, buf, limit - buf);
1182 }
1183
1184 /* The arglist structure is built by do_define to tell
1185  * collect_definition where the argument names begin.  That
1186  * is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1187  * would contain pointers to the strings x, y, and z.
1188  * Collect_definition would then build a DEFINITION node,
1189  * with reflist nodes pointing to the places x, y, and z had
1190  * appeared.  So the arglist is just convenience data passed
1191  * between these two routines.  It is not kept around after
1192  * the current #define has been processed and entered into the
1193  * hash table. */
1194
1195 struct arglist {
1196    struct arglist     *next;
1197    char               *name;
1198    int                 length;
1199    int                 argno;
1200    char                rest_args;
1201 };
1202
1203 /* Read a replacement list for a macro with parameters.
1204  * Build the DEFINITION structure.
1205  * Reads characters of text starting at BUF until END.
1206  * ARGLIST specifies the formal parameters to look for
1207  * in the text of the definition; NARGS is the number of args
1208  * in that list, or -1 for a macro name that wants no argument list.
1209  * MACRONAME is the macro name itself (so we can avoid recursive expansion)
1210  * and NAMELEN is its length in characters.
1211  * 
1212  * Note that comments, backslash-newlines, and leading white space
1213  * have already been deleted from the argument.  */
1214
1215 static DEFINITION  *
1216 collect_expansion(cpp_reader * pfile, unsigned char *buf, unsigned char *limit,
1217                   int nargs, struct arglist *arglist)
1218 {
1219    DEFINITION         *defn;
1220    unsigned char      *p, *lastp, *exp_p;
1221    reflist            *endpat = NULL;
1222
1223    /* Pointer to first nonspace after last ## seen.  */
1224    unsigned char      *concat = 0;
1225
1226    /* Pointer to first nonspace after last single-# seen.  */
1227    unsigned char      *stringify = 0;
1228    int                 maxsize;
1229    int                 expected_delimiter = '\0';
1230
1231    /* Scan thru the replacement list, ignoring comments and quoted
1232     * strings, picking up on the macro calls.  It does a linear search
1233     * thru the arg list on every potential symbol.  Profiling might say
1234     * that something smarter should happen. */
1235
1236    if (limit < buf)
1237       abort();
1238
1239    /* Find the beginning of the trailing whitespace.  */
1240    p = buf;
1241    while (p < limit && is_space[limit[-1]])
1242       limit--;
1243
1244    /* Allocate space for the text in the macro definition.
1245     * Leading and trailing whitespace chars need 2 bytes each.
1246     * Each other input char may or may not need 1 byte,
1247     * so this is an upper bound.  The extra 5 are for invented
1248     * leading and trailing newline-marker and final null.  */
1249    maxsize = (sizeof(DEFINITION) + (limit - p) + 5);
1250    /* Occurrences of '@' get doubled, so allocate extra space for them. */
1251    while (p < limit)
1252       if (*p++ == '@')
1253          maxsize++;
1254    defn = (DEFINITION *) xcalloc(1, maxsize);
1255
1256    defn->nargs = nargs;
1257    exp_p = defn->expansion = (unsigned char *)defn + sizeof(DEFINITION);
1258    lastp = exp_p;
1259
1260    p = buf;
1261
1262    /* Add one initial space escape-marker to prevent accidental
1263     * token-pasting (often removed by macroexpand). */
1264    *exp_p++ = '@';
1265    *exp_p++ = ' ';
1266
1267    if (limit - p >= 2 && p[0] == '#' && p[1] == '#')
1268      {
1269         cpp_error(pfile, "`##' at start of macro definition");
1270         p += 2;
1271      }
1272    /* Process the main body of the definition.  */
1273    while (p < limit)
1274      {
1275         int                 skipped_arg = 0;
1276         unsigned char       c = *p++;
1277
1278         *exp_p++ = c;
1279
1280         switch (c)
1281           {
1282            case '\'':
1283            case '\"':
1284              if (expected_delimiter != '\0')
1285                {
1286                   if (c == expected_delimiter)
1287                      expected_delimiter = '\0';
1288                }
1289              else
1290                 expected_delimiter = c;
1291              break;
1292              
1293            case '\\':
1294              if (p < limit && expected_delimiter)
1295                {
1296                   /* In a string, backslash goes through
1297                    * and makes next char ordinary.  */
1298                   *exp_p++ = *p++;
1299                }
1300              break;
1301              
1302            case '@':
1303              /* An '@' in a string or character constant stands for itself,
1304               * and does not need to be escaped. */
1305              if (!expected_delimiter)
1306                 *exp_p++ = c;
1307              break;
1308              
1309            case '#':
1310              /* # is ordinary inside a string.  */
1311              if (expected_delimiter)
1312                 break;
1313              if (p < limit && *p == '#')
1314                {
1315                   /* ##: concatenate preceding and following tokens.  */
1316                   /* Take out the first #, discard preceding whitespace.  */
1317                   exp_p--;
1318                   while (exp_p > lastp && is_hor_space[exp_p[-1]])
1319                      --exp_p;
1320                   /* Skip the second #.  */
1321                   p++;
1322                   /* Discard following whitespace.  */
1323                   SKIP_WHITE_SPACE(p);
1324                   concat = p;
1325                   if (p == limit)
1326                      cpp_error(pfile, "`##' at end of macro definition");
1327                }
1328              else if (nargs >= 0)
1329                {
1330                   /* Single #: stringify following argument ref.
1331                    * Don't leave the # in the expansion.  */
1332                   exp_p--;
1333                   SKIP_WHITE_SPACE(p);
1334                   if (p == limit || !is_idstart[*p])
1335                      cpp_error(pfile,
1336                                "`#' operator is not followed by a macro argument name");
1337                   else
1338                      stringify = p;
1339                }
1340              break;
1341           }
1342
1343         /* Handle the start of a symbol.  */
1344         if (is_idchar[c] && nargs > 0)
1345           {
1346              unsigned char      *id_beg = p - 1;
1347              int                 id_len;
1348
1349              --exp_p;
1350              while (p != limit && is_idchar[*p])
1351                 p++;
1352              id_len = p - id_beg;
1353
1354              if (is_idstart[c])
1355                {
1356                   struct arglist     *arg;
1357
1358                   for (arg = arglist; arg; arg = arg->next)
1359                     {
1360                        reflist            *tpat;
1361
1362                        if (arg->name[0] == c
1363                            && arg->length == id_len
1364                            && strncmp((const char *)arg->name,
1365                                       (const char *)id_beg, id_len) == 0)
1366                          {
1367                             if (expected_delimiter
1368                                 && CPP_OPTIONS(pfile)->warn_stringify)
1369                               {
1370                                  cpp_warning(pfile,
1371                                              "macro arg `%.*s' would be stringified with -traditional.",
1372                                              id_len, arg->name);
1373                               }
1374                             /* If ANSI, don't actually substitute inside a string.  */
1375                             if (expected_delimiter)
1376                                break;
1377                             /* make a pat node for this arg and append it to the end of
1378                              * the pat list */
1379                             tpat = (reflist *) xmalloc(sizeof(reflist));
1380
1381                             tpat->next = NULL;
1382                             tpat->raw_before = concat == id_beg;
1383                             tpat->raw_after = 0;
1384                             tpat->rest_args = arg->rest_args;
1385                             tpat->stringify = (stringify == id_beg);
1386
1387                             if (!endpat)
1388                                defn->pattern = tpat;
1389                             else
1390                                endpat->next = tpat;
1391                             endpat = tpat;
1392
1393                             tpat->argno = arg->argno;
1394                             tpat->nchars = exp_p - lastp;
1395                             {
1396                                unsigned char      *p1 = p;
1397
1398                                SKIP_WHITE_SPACE(p1);
1399                                if (p1 + 2 <= limit && p1[0] == '#'
1400                                    && p1[1] == '#')
1401                                   tpat->raw_after = 1;
1402                             }
1403                             lastp = exp_p;      /* place to start copying from next time */
1404                             skipped_arg = 1;
1405                             break;
1406                          }
1407                     }
1408                }
1409              /* If this was not a macro arg, copy it into the expansion.  */
1410              if (!skipped_arg)
1411                {
1412                   unsigned char      *lim1 = p;
1413
1414                   p = id_beg;
1415                   while (p != lim1)
1416                      *exp_p++ = *p++;
1417                   if (stringify == id_beg)
1418                      cpp_error(pfile,
1419                                "`#' operator should be followed by a macro argument name");
1420                }
1421           }
1422      }
1423
1424    if (expected_delimiter == 0)
1425      {
1426         /* If ANSI, put in a "@ " marker to prevent token pasting.
1427          * But not if "inside a string" (which in ANSI mode
1428          * happens only for -D option).  */
1429         *exp_p++ = '@';
1430         *exp_p++ = ' ';
1431      }
1432    *exp_p = '\0';
1433
1434    defn->length = exp_p - defn->expansion;
1435
1436    /* Crash now if we overrun the allocated size.  */
1437    if (defn->length + 1 > maxsize)
1438       abort();
1439
1440    return defn;
1441 }
1442
1443 /*
1444  * special extension string that can be added to the last macro argument to 
1445  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
1446  *              #define wow(a, b...)            process (b, a, b)
1447  *              { wow (1, 2, 3); }      ->      { process (2, 3, 1, 2, 3); }
1448  *              { wow (one, two); }     ->      { process (two, one, two); }
1449  * if this "rest_arg" is used with the concat token '##' and if it is not
1450  * supplied then the token attached to with ## will not be outputted.  Ex:
1451  *              #define wow (a, b...)           process (b ## , a, ## b)
1452  *              { wow (1, 2); }         ->      { process (2, 1, 2); }
1453  *              { wow (one); }          ->      { process (one); {
1454  */
1455 static char         rest_extension[] = "...";
1456
1457 #define REST_EXTENSION_LENGTH   (sizeof (rest_extension) - 1)
1458
1459 /* Create a DEFINITION node from a #define directive.  Arguments are 
1460  * as for do_define. */
1461 static void
1462 create_definition(MACRODEF * mdef, unsigned char *buf, unsigned char *limit,
1463                   cpp_reader * pfile, int predefinition)
1464 {
1465    unsigned char      *bp;      /* temp ptr into input buffer */
1466    unsigned char      *symname; /* remember where symbol name starts */
1467    int                 sym_length;      /* and how long it is */
1468    int                 rest_args = 0;
1469    long                line, col;
1470    const char         *file =
1471       CPP_BUFFER(pfile) ? CPP_BUFFER(pfile)->nominal_fname : "";
1472    DEFINITION         *defn;
1473    int                 arglengths = 0;  /* Accumulate lengths of arg names
1474                                          * plus number of args.  */
1475
1476    cpp_buf_line_and_col(CPP_BUFFER(pfile), &line, &col);
1477
1478    bp = buf;
1479
1480    while (is_hor_space[*bp])
1481       bp++;
1482
1483    symname = bp;                /* remember where it starts */
1484
1485    sym_length = check_macro_name(pfile, bp, "macro");
1486    bp += sym_length;
1487
1488    /* Lossage will occur if identifiers or control keywords are broken
1489     * across lines using backslash.  This is not the right place to take
1490     * care of that. */
1491
1492    if (*bp == '(')
1493      {
1494         struct arglist     *arg_ptrs = NULL;
1495         int                 argno = 0;
1496
1497         bp++;                   /* skip '(' */
1498         SKIP_WHITE_SPACE(bp);
1499
1500         /* Loop over macro argument names.  */
1501         while (*bp != ')')
1502           {
1503              struct arglist     *temp;
1504
1505              temp = (struct arglist *)alloca(sizeof(struct arglist));
1506
1507              temp->name = (char *)bp;
1508              temp->next = arg_ptrs;
1509              temp->argno = argno++;
1510              temp->rest_args = 0;
1511              arg_ptrs = temp;
1512
1513              if (rest_args)
1514                 cpp_pedwarn(pfile, "another parameter follows `%s'",
1515                             rest_extension);
1516
1517              if (!is_idstart[*bp])
1518                 cpp_pedwarn(pfile, "invalid character in macro parameter name");
1519
1520              /* Find the end of the arg name.  */
1521              while (is_idchar[*bp])
1522                {
1523                   bp++;
1524                   /* do we have a "special" rest-args extension here? */
1525                   if ((unsigned)(limit - bp) > REST_EXTENSION_LENGTH &&
1526                       strncmp(rest_extension, (const char *)bp,
1527                               REST_EXTENSION_LENGTH) == 0)
1528                     {
1529                        rest_args = 1;
1530                        temp->rest_args = 1;
1531                        break;
1532                     }
1533                }
1534              temp->length = (char *)bp - temp->name;
1535              if (rest_args == 1)
1536                 bp += REST_EXTENSION_LENGTH;
1537              arglengths += temp->length + 2;
1538              SKIP_WHITE_SPACE(bp);
1539              if (temp->length == 0 || (*bp != ',' && *bp != ')'))
1540                {
1541                   cpp_error(pfile,
1542                             "badly punctuated parameter list in `#define'");
1543                   goto nope;
1544                }
1545              if (*bp == ',')
1546                {
1547                   bp++;
1548                   SKIP_WHITE_SPACE(bp);
1549                }
1550              if (bp >= limit)
1551                {
1552                   cpp_error(pfile, "unterminated parameter list in `#define'");
1553                   goto nope;
1554                }
1555              {
1556                 struct arglist     *otemp;
1557
1558                 for (otemp = temp->next; otemp; otemp = otemp->next)
1559                    if (temp->length == otemp->length &&
1560                        strncmp((const char *)temp->name,
1561                                (const char *)otemp->name, temp->length) == 0)
1562                      {
1563                         char               *name;
1564
1565                         name = (char *)alloca(temp->length + 1);
1566                         strncpy(name, (const char *)temp->name, temp->length);
1567                         name[temp->length] = '\0';
1568                         cpp_error(pfile,
1569                                   "duplicate argument name `%s' in `#define'",
1570                                   name);
1571                         goto nope;
1572                      }
1573              }
1574           }
1575
1576         ++bp;                   /* skip paren */
1577         SKIP_WHITE_SPACE(bp);
1578         /* now everything from bp before limit is the definition. */
1579         defn = collect_expansion(pfile, bp, limit, argno, arg_ptrs);
1580         defn->rest_args = rest_args;
1581
1582         /* Now set defn->args.argnames to the result of concatenating
1583          * the argument names in reverse order
1584          * with comma-space between them.  */
1585         defn->args.argnames = (unsigned char *)xmalloc(arglengths + 1);
1586         {
1587            struct arglist     *temp;
1588            int                 i = 0;
1589
1590            for (temp = arg_ptrs; temp; temp = temp->next)
1591              {
1592                 memcpy(&defn->args.argnames[i], temp->name, temp->length);
1593                 i += temp->length;
1594                 if (temp->next)
1595                   {
1596                      defn->args.argnames[i++] = ',';
1597                      defn->args.argnames[i++] = ' ';
1598                   }
1599              }
1600            defn->args.argnames[i] = 0;
1601         }
1602      }
1603    else
1604      {
1605         /* Simple expansion or empty definition.  */
1606
1607         if (bp < limit)
1608           {
1609              if (is_hor_space[*bp])
1610                {
1611                   bp++;
1612                   SKIP_WHITE_SPACE(bp);
1613                }
1614              else
1615                {
1616                   switch (*bp)
1617                     {
1618                     case '!':
1619                     case '"':
1620                     case '#':
1621                     case '%':
1622                     case '&':
1623                     case '\'':
1624                     case ')':
1625                     case '*':
1626                     case '+':
1627                     case ',':
1628                     case '-':
1629                     case '.':
1630                     case '/':
1631                     case ':':
1632                     case ';':
1633                     case '<':
1634                     case '=':
1635                     case '>':
1636                     case '?':
1637                     case '[':
1638                     case '\\':
1639                     case ']':
1640                     case '^':
1641                     case '{':
1642                     case '|':
1643                     case '}':
1644                     case '~':
1645                        cpp_warning(pfile,
1646                                    "missing white space after `#define %.*s'",
1647                                    sym_length, symname);
1648                        break;
1649
1650                     default:
1651                        cpp_pedwarn(pfile,
1652                                    "missing white space after `#define %.*s'",
1653                                    sym_length, symname);
1654                        break;
1655                     }
1656                }
1657           }
1658         /* now everything from bp before limit is the definition. */
1659         defn = collect_expansion(pfile, bp, limit, -1, NULL);
1660         defn->args.argnames = (unsigned char *)"";
1661      }
1662
1663    defn->line = line;
1664    defn->file = file;
1665
1666    /* OP is null if this is a predefinition */
1667    defn->predefined = predefinition;
1668    mdef->defn = defn;
1669    mdef->symnam = (char *)symname;
1670    mdef->symlen = sym_length;
1671
1672    return;
1673
1674  nope:
1675    mdef->defn = 0;
1676 }
1677
1678 /* Check a purported macro name SYMNAME, and yield its length.
1679  * USAGE is the kind of name this is intended for.  */
1680
1681 static int
1682 check_macro_name(cpp_reader * pfile, unsigned char *symname, const char *usage)
1683 {
1684    unsigned char      *p;
1685    int                 sym_length;
1686
1687    for (p = symname; is_idchar[*p]; p++)
1688       ;
1689    sym_length = p - symname;
1690    if (sym_length == 0)
1691      {
1692         cpp_error(pfile, "invalid %s name", usage);
1693      }
1694    else if (!is_idstart[*symname])
1695      {
1696         unsigned char      *msg;        /* what pain... */
1697
1698         msg = (unsigned char *)alloca(sym_length + 1);
1699         memcpy(msg, symname, sym_length);
1700         msg[sym_length] = 0;
1701         cpp_error(pfile, "invalid %s name `%s'", usage, msg);
1702      }
1703    else
1704      {
1705         if (!strncmp((const char *)symname, "defined", 7) && sym_length == 7)
1706            cpp_error(pfile, "invalid %s name `defined'", usage);
1707      }
1708    return sym_length;
1709 }
1710
1711 /*
1712  * return zero if two DEFINITIONs are isomorphic
1713  */
1714 static int
1715 compare_defs(DEFINITION * d1, DEFINITION * d2)
1716 {
1717    reflist            *a1, *a2;
1718    unsigned char      *p1 = d1->expansion;
1719    unsigned char      *p2 = d2->expansion;
1720    int                 first = 1;
1721
1722    if (d1->nargs != d2->nargs)
1723       return 1;
1724    if (strcmp((char *)d1->args.argnames, (char *)d2->args.argnames))
1725       return 1;
1726    for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1727         a1 = a1->next, a2 = a2->next)
1728      {
1729         if (!
1730             ((a1->nchars == a2->nchars
1731               && !strncmp((const char *)p1, (const char *)p2, a1->nchars))
1732              || !comp_def_part(first, p1, a1->nchars, p2, a2->nchars, 0))
1733             || a1->argno != a2->argno || a1->stringify != a2->stringify
1734             || a1->raw_before != a2->raw_before
1735             || a1->raw_after != a2->raw_after)
1736            return 1;
1737         first = 0;
1738         p1 += a1->nchars;
1739         p2 += a2->nchars;
1740      }
1741    if (a1 != a2)
1742       return 1;
1743    if (comp_def_part(first, p1, d1->length - (p1 - d1->expansion),
1744                      p2, d2->length - (p2 - d2->expansion), 1))
1745       return 1;
1746    return 0;
1747 }
1748
1749 /* Return 1 if two parts of two macro definitions are effectively different.
1750  * One of the parts starts at BEG1 and has LEN1 chars;
1751  * the other has LEN2 chars at BEG2.
1752  * Any sequence of whitespace matches any other sequence of whitespace.
1753  * FIRST means these parts are the first of a macro definition;
1754  * so ignore leading whitespace entirely.
1755  * LAST means these parts are the last of a macro definition;
1756  * so ignore trailing whitespace entirely.  */
1757
1758 static int
1759 comp_def_part(int first, unsigned char *beg1, int len1,
1760               unsigned char *beg2, int len2, int last)
1761 {
1762    unsigned char      *end1 = beg1 + len1;
1763    unsigned char      *end2 = beg2 + len2;
1764
1765    if (first)
1766      {
1767         while (beg1 != end1 && is_space[*beg1])
1768            beg1++;
1769         while (beg2 != end2 && is_space[*beg2])
1770            beg2++;
1771      }
1772    if (last)
1773      {
1774         while (beg1 != end1 && is_space[end1[-1]])
1775            end1--;
1776         while (beg2 != end2 && is_space[end2[-1]])
1777            end2--;
1778      }
1779    while (beg1 != end1 && beg2 != end2)
1780      {
1781         if (is_space[*beg1] && is_space[*beg2])
1782           {
1783              while (beg1 != end1 && is_space[*beg1])
1784                 beg1++;
1785              while (beg2 != end2 && is_space[*beg2])
1786                 beg2++;
1787           }
1788         else if (*beg1 == *beg2)
1789           {
1790              beg1++;
1791              beg2++;
1792           }
1793         else
1794            break;
1795      }
1796    return (beg1 != end1) || (beg2 != end2);
1797 }
1798
1799 /* Process a #define command.
1800  * BUF points to the contents of the #define command, as a contiguous string.
1801  * LIMIT points to the first character past the end of the definition.
1802  * KEYWORD is the keyword-table entry for #define,
1803  * or NULL for a "predefined" macro.  */
1804
1805 static int
1806 do_define(cpp_reader * pfile, struct directive *keyword,
1807           unsigned char *buf, unsigned char *limit)
1808 {
1809    int                 hashcode;
1810    MACRODEF            mdef;
1811    HASHNODE           *hp;
1812
1813    create_definition(&mdef, buf, limit, pfile, !keyword);
1814    if (!mdef.defn)
1815       return 1;
1816
1817    hashcode = hashf(mdef.symnam, mdef.symlen, HASHSIZE);
1818
1819    if ((hp = cpp_lookup(mdef.symnam, mdef.symlen, hashcode)))
1820      {
1821         int                 ok = 0;
1822
1823         /* Redefining a precompiled key is ok.  */
1824         if (hp->type == T_PCSTRING)
1825            ok = 1;
1826         /* Redefining a macro is ok if the definitions are the same.  */
1827         else if (hp->type == T_MACRO)
1828            ok = !compare_defs(mdef.defn, hp->value.defn);
1829         /* Redefining a constant is ok with -D.  */
1830         else if (hp->type == T_CONST)
1831            ok = !CPP_OPTIONS(pfile)->done_initializing;
1832         /* Print the warning if it's not ok.  */
1833         if (!ok)
1834           {
1835              char               *msg;   /* what pain... */
1836
1837              /* If we are passing through #define and #undef directives, do
1838               * that for this re-definition now.  */
1839              if (CPP_OPTIONS(pfile)->debug_output && keyword)
1840                 pass_thru_directive((char *)buf, (char *)limit, pfile, keyword);
1841
1842              msg = (char *)alloca(mdef.symlen + 22);
1843              *msg = '`';
1844              memcpy(msg + 1, mdef.symnam, mdef.symlen);
1845              strcpy((msg + mdef.symlen + 1), "' redefined");
1846              cpp_pedwarn(pfile, msg);
1847              if (hp->type == T_MACRO)
1848                 cpp_pedwarn_with_file_and_line(pfile, hp->value.defn->file,
1849                                                hp->value.defn->line,
1850                                                "this is the location of the previous definition",
1851                                                NULL, NULL, NULL);
1852           }
1853         /* Replace the old definition.  */
1854         hp->type = T_MACRO;
1855         hp->value.defn = mdef.defn;
1856      }
1857    else
1858      {
1859         /* If we are passing through #define and #undef directives, do
1860          * that for this new definition now.  */
1861         if (CPP_OPTIONS(pfile)->debug_output && keyword)
1862            pass_thru_directive((char *)buf, (char *)limit, pfile, keyword);
1863         install(mdef.symnam, mdef.symlen, T_MACRO, 0,
1864                 (char *)mdef.defn, hashcode);
1865      }
1866
1867    return 0;
1868 }
1869
1870 /* This structure represents one parsed argument in a macro call.
1871  * `raw' points to the argument text as written (`raw_length' is its length).
1872  * `expanded' points to the argument's macro-expansion
1873  * (its length is `expand_length').
1874  * `stringified_length' is the length the argument would have
1875  * if stringified.
1876  * `use_count' is the number of times this macro arg is substituted
1877  * into the macro.  If the actual use count exceeds 10, 
1878  * the value stored is 10. */
1879
1880 /* raw and expanded are relative to ARG_BASE */
1881 #define ARG_BASE ((pfile)->token_buffer)
1882
1883 struct argdata {
1884    /* Strings relative to pfile->token_buffer */
1885    long                raw, expanded, stringified;
1886    int                 raw_length, expand_length;
1887    int                 stringified_length;
1888    char                newlines;
1889    char                use_count;
1890 };
1891
1892 cpp_buffer         *
1893 cpp_push_buffer(cpp_reader * pfile, unsigned char *buffer, long length)
1894 {
1895 #ifdef STATIC_BUFFERS
1896    cpp_buffer         *buf = CPP_BUFFER(pfile);
1897
1898    if (buf == pfile->buffer_stack)
1899       cpp_fatal("macro or `#include' recursion too deep");
1900    buf--;
1901    memset((char *)buf, 0, sizeof(cpp_buffer));
1902    CPP_BUFFER(pfile) = buf;
1903 #else
1904    cpp_buffer         *buf = (cpp_buffer *) xmalloc(sizeof(cpp_buffer));
1905
1906    memset((char *)buf, 0, sizeof(cpp_buffer));
1907    CPP_PREV_BUFFER(buf) = CPP_BUFFER(pfile);
1908    CPP_BUFFER(pfile) = buf;
1909 #endif
1910    buf->if_stack = pfile->if_stack;
1911    buf->cleanup = null_cleanup;
1912    buf->underflow = null_underflow;
1913    buf->buf = buf->cur = buffer;
1914    buf->alimit = buf->rlimit = buffer + length;
1915
1916    return buf;
1917 }
1918
1919 static cpp_buffer  *
1920 cpp_pop_buffer(cpp_reader * pfile)
1921 {
1922    cpp_buffer         *buf = CPP_BUFFER(pfile);
1923
1924 #ifdef STATIC_BUFFERS
1925    (*buf->cleanup) (buf, pfile);
1926    return ++CPP_BUFFER(pfile);
1927 #else
1928    cpp_buffer         *next_buf = CPP_PREV_BUFFER(buf);
1929
1930    (*buf->cleanup) (buf, pfile);
1931    CPP_BUFFER(pfile) = next_buf;
1932    free(buf);
1933    return next_buf;
1934 #endif
1935 }
1936
1937 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1938  * Pop the buffer when done. */
1939
1940 static void
1941 cpp_scan_buffer(cpp_reader * pfile)
1942 {
1943    cpp_buffer         *buffer = CPP_BUFFER(pfile);
1944
1945    for (;;)
1946      {
1947         enum cpp_token      token = cpp_get_token(pfile);
1948
1949         if (token == CPP_EOF)   /* Should not happen ... */
1950            break;
1951         if (token == CPP_POP && CPP_BUFFER(pfile) == buffer)
1952           {
1953              cpp_pop_buffer(pfile);
1954              break;
1955           }
1956      }
1957 }
1958
1959 /*
1960  * Rescan a string (which may have escape marks) into pfile's buffer.
1961  * Place the result in pfile->token_buffer.
1962  *
1963  * The input is copied before it is scanned, so it is safe to pass
1964  * it something from the token_buffer that will get overwritten
1965  * (because it follows CPP_WRITTEN).  This is used by do_include.
1966  */
1967
1968 static void
1969 cpp_expand_to_buffer(cpp_reader * pfile, unsigned char *buf, int length)
1970 {
1971    cpp_buffer         *ip;
1972    unsigned char      *limit = buf + length;
1973    unsigned char      *buf1;
1974
1975    if (length < 0)
1976       abort();
1977
1978    /* Set up the input on the input stack.  */
1979
1980    buf1 = (unsigned char *)alloca(length + 1);
1981    {
1982       unsigned char      *p1 = buf;
1983       unsigned char      *p2 = buf1;
1984       int                 in_string = 0;
1985       
1986 #if 0 /* old behavior */ 
1987       while (p1 != limit) *p2++ = *p1++;
1988 #else /* new one - handle \ escapes if not in string */
1989       while (p1 != limit)
1990         {
1991            if (!in_string)
1992              {
1993                 if (*p1 == '"') in_string = 1;
1994                 if (*p1 == '\\')
1995                   {
1996                      p1++;
1997                      if (p1 != limit) *p2++ = *p1++;
1998                   }
1999                 else
2000                    *p2++ = *p1++;
2001              }
2002            else
2003              {
2004                 if ((*p1 == '"') && (p1 > buf) && (p1[-1] != '\\'))
2005                    in_string = 0;
2006                 *p2++ = *p1++;
2007              }
2008         }
2009 #endif
2010       *p2 = 0;
2011       length = p2 - buf1;
2012    }
2013
2014    ip = cpp_push_buffer(pfile, buf1, length);
2015    ip->has_escapes = 1;
2016
2017    /* Scan the input, create the output.  */
2018    cpp_scan_buffer(pfile);
2019
2020    CPP_NUL_TERMINATE(pfile);
2021 }
2022
2023 static void
2024 adjust_position(unsigned char *buf, unsigned char *limit, long *linep,
2025                 long *colp)
2026 {
2027    while (buf < limit)
2028      {
2029         unsigned char       ch = *buf++;
2030
2031         if (ch == '\n')
2032            (*linep)++, (*colp) = 1;
2033         else
2034            (*colp)++;
2035      }
2036 }
2037
2038 /* Move line_base forward, updating lineno and colno. */
2039
2040 static void
2041 update_position(cpp_buffer * pbuf)
2042 {
2043    unsigned char      *old_pos = pbuf->buf + pbuf->line_base;
2044    unsigned char      *new_pos = pbuf->cur;
2045    struct parse_marker *mark;
2046
2047    for (mark = pbuf->marks; mark; mark = mark->next)
2048      {
2049         if (pbuf->buf + mark->position < new_pos)
2050            new_pos = pbuf->buf + mark->position;
2051      }
2052    pbuf->line_base += new_pos - old_pos;
2053    adjust_position(old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2054 }
2055
2056 void
2057 cpp_buf_line_and_col(cpp_buffer * pbuf, long *linep, long *colp)
2058 {
2059    long                dummy;
2060
2061    if (!colp)
2062       colp = &dummy;
2063    if (pbuf)
2064      {
2065         *linep = pbuf->lineno;
2066         *colp = pbuf->colno;
2067         adjust_position(pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2068      }
2069    else
2070      {
2071         *linep = 0;
2072         *colp = 0;
2073      }
2074 }
2075
2076 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2077
2078 cpp_buffer         *
2079 cpp_file_buffer(cpp_reader * pfile)
2080 {
2081    cpp_buffer         *ip = CPP_BUFFER(pfile);
2082
2083    for (; ip != CPP_NULL_BUFFER(pfile); ip = CPP_PREV_BUFFER(ip))
2084       if (ip->fname)
2085          return ip;
2086    return NULL;
2087 }
2088
2089 static long
2090 count_newlines(unsigned char *buf, unsigned char *limit)
2091 {
2092    long                count = 0;
2093
2094    while (buf < limit)
2095      {
2096         unsigned char       ch = *buf++;
2097
2098         if (ch == '\n')
2099            count++;
2100      }
2101    return count;
2102 }
2103
2104 /*
2105  * write out a #line command, for instance, after an #include file.
2106  * If CONDITIONAL is nonzero, we can omit the #line if it would
2107  * appear to be a no-op, and we can output a few newlines instead
2108  * if we want to increase the line number by a small amount.
2109  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2110  */
2111
2112 static void
2113 output_line_command(cpp_reader * pfile, int conditional,
2114                     enum file_change_code file_change)
2115 {
2116    long                line, col;
2117    cpp_buffer         *ip = CPP_BUFFER(pfile);
2118
2119    if (CPP_OPTIONS(pfile)->no_line_commands
2120        || !ip->fname || CPP_OPTIONS(pfile)->no_output)
2121      {
2122         return;
2123      }
2124    update_position(ip);
2125    line = CPP_BUFFER(pfile)->lineno;
2126    col = CPP_BUFFER(pfile)->colno;
2127    adjust_position(CPP_LINE_BASE(ip), ip->cur, &line, &col);
2128
2129    if (conditional)
2130      {
2131         if (line == pfile->lineno)
2132            return;
2133
2134         /* If the inherited line number is a little too small,
2135          * output some newlines instead of a #line command.  */
2136         if (line > pfile->lineno && line < pfile->lineno + 8)
2137           {
2138              CPP_RESERVE(pfile, 20);
2139              while (line > pfile->lineno)
2140                {
2141                   CPP_PUTC_Q(pfile, '\n');
2142                   pfile->lineno++;
2143                }
2144              return;
2145           }
2146      }
2147
2148    CPP_RESERVE(pfile, 4 * strlen(ip->nominal_fname) + 50);
2149    {
2150       static char         sharp_line[] = "#line ";
2151
2152       CPP_PUTS_Q(pfile, sharp_line, sizeof(sharp_line) - 1);
2153    }
2154
2155    sprintf((char *)CPP_PWRITTEN(pfile), "%d ", (int)line);
2156    CPP_ADJUST_WRITTEN(pfile, strlen((char *)CPP_PWRITTEN(pfile)));
2157
2158    quote_string(pfile, ip->nominal_fname);
2159    if (file_change != same_file)
2160      {
2161         CPP_PUTC_Q(pfile, ' ');
2162         CPP_PUTC_Q(pfile, file_change == enter_file ? '1' : '2');
2163      }
2164    /* Tell cc1 if following text comes from a system header file.  */
2165    if (ip->system_header_p)
2166      {
2167         CPP_PUTC_Q(pfile, ' ');
2168         CPP_PUTC_Q(pfile, '3');
2169      }
2170 #ifndef NO_IMPLICIT_EXTERN_C
2171    /* Tell cc1plus if following text should be treated as C.  */
2172    if (ip->system_header_p == 2 && CPP_OPTIONS(pfile)->cplusplus)
2173      {
2174         CPP_PUTC_Q(pfile, ' ');
2175         CPP_PUTC_Q(pfile, '4');
2176      }
2177 #endif
2178    CPP_PUTC_Q(pfile, '\n');
2179    pfile->lineno = line;
2180 }
2181
2182 /*
2183  * Parse a macro argument and append the info on PFILE's token_buffer.
2184  * REST_ARGS means to absorb the rest of the args.
2185  * Return nonzero to indicate a syntax error.
2186  */
2187
2188 static enum cpp_token
2189 macarg(cpp_reader * pfile, int rest_args)
2190 {
2191    int                 paren = 0;
2192    enum cpp_token      token;
2193    char                save_put_out_comments =
2194       CPP_OPTIONS(pfile)->put_out_comments;
2195
2196    CPP_OPTIONS(pfile)->put_out_comments = 0;
2197
2198    token = CPP_OTHER;
2199    /* Try to parse as much of the argument as exists at this
2200     * input stack level.  */
2201    pfile->no_macro_expand++;
2202    for (;;)
2203      {
2204         token = cpp_get_token(pfile);
2205         switch (token)
2206           {
2207           case CPP_EOF:
2208              goto done;
2209           case CPP_POP:
2210              /* If we've hit end of file, it's an error (reported by caller).
2211               * Ditto if it's the end of cpp_expand_to_buffer text.
2212               * If we've hit end of macro, just continue.  */
2213              if (!CPP_IS_MACRO_BUFFER(CPP_BUFFER(pfile)))
2214                 goto done;
2215              break;
2216           case CPP_LPAREN:
2217              paren++;
2218              break;
2219           case CPP_RPAREN:
2220              if (--paren < 0)
2221                 goto found;
2222              break;
2223           case CPP_COMMA:
2224              /* if we've returned to lowest level and
2225               * we aren't absorbing all args */
2226              if (paren == 0 && rest_args == 0)
2227                 goto found;
2228              break;
2229            found:
2230              /* Remove ',' or ')' from argument buffer. */
2231              CPP_ADJUST_WRITTEN(pfile, -1);
2232              goto done;
2233           default:;
2234           }
2235      }
2236
2237  done:
2238    CPP_OPTIONS(pfile)->put_out_comments = save_put_out_comments;
2239    pfile->no_macro_expand--;
2240
2241    return token;
2242 }
2243
2244 /* Turn newlines to spaces in the string of length LENGTH at START,
2245  * except inside of string constants.
2246  * The string is copied into itself with its beginning staying fixed.  */
2247
2248 static int
2249 change_newlines(unsigned char *start, int length)
2250 {
2251    unsigned char      *ibp;
2252    unsigned char      *obp;
2253    unsigned char      *limit;
2254    int                 c;
2255
2256    ibp = start;
2257    limit = start + length;
2258    obp = start;
2259
2260    while (ibp < limit)
2261      {
2262         *obp++ = c = *ibp++;
2263         switch (c)
2264           {
2265
2266           case '\'':
2267           case '\"':
2268              /* Notice and skip strings, so that we don't delete newlines in them.  */
2269              {
2270                 int                 quotec = c;
2271
2272                 while (ibp < limit)
2273                   {
2274                      *obp++ = c = *ibp++;
2275                      if (c == quotec)
2276                         break;
2277                      if (c == '\n' && quotec == '\'')
2278                         break;
2279                   }
2280              }
2281              break;
2282           }
2283      }
2284
2285    return obp - start;
2286 }
2287
2288 static struct tm   *
2289 timestamp(cpp_reader * pfile)
2290 {
2291    if (!pfile->timebuf)
2292      {
2293         time_t              t = time((time_t *) 0);
2294
2295         pfile->timebuf = localtime(&t);
2296      }
2297    return pfile->timebuf;
2298 }
2299
2300 static const char  *monthnames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2301    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2302 };
2303
2304 /*
2305  * expand things like __FILE__.  Place the expansion into the output
2306  * buffer *without* rescanning.
2307  */
2308
2309 static void
2310 special_symbol(HASHNODE * hp, cpp_reader * pfile)
2311 {
2312    const char         *buf;
2313    char               *bufx;
2314    int                 len;
2315    int                 true_indepth;
2316    cpp_buffer         *ip = NULL;
2317    struct tm          *timebuf;
2318
2319    int                 paren = 0;       /* For special `defined' keyword */
2320
2321    for (ip = CPP_BUFFER(pfile);; ip = CPP_PREV_BUFFER(ip))
2322      {
2323         if (!ip)
2324           {
2325              cpp_error(pfile, "cccp error: not in any file?!");
2326              return;            /* the show must go on */
2327           }
2328         if (ip->fname)
2329            break;
2330      }
2331
2332    switch (hp->type)
2333      {
2334      case T_FILE:
2335      case T_BASE_FILE:
2336         {
2337            const char         *string;
2338
2339            if (hp->type == T_BASE_FILE)
2340              {
2341                 while (CPP_PREV_BUFFER(ip))
2342                    ip = CPP_PREV_BUFFER(ip);
2343              }
2344            string = ip->nominal_fname;
2345
2346            if (!string)
2347               string = "";
2348            CPP_RESERVE(pfile, 3 + 4 * strlen(string));
2349            quote_string(pfile, string);
2350            return;
2351         }
2352
2353      case T_INCLUDE_LEVEL:
2354         true_indepth = 0;
2355         for (ip = CPP_BUFFER(pfile); ip; ip = CPP_PREV_BUFFER(ip))
2356            if (ip->fname)
2357               true_indepth++;
2358
2359         bufx = (char *)alloca(8);       /* Eight bytes ought to be more than enough */
2360         sprintf(bufx, "%d", true_indepth - 1);
2361         buf = bufx;
2362         break;
2363
2364      case T_VERSION:
2365         bufx = (char *)alloca(3 + strlen(version_string));
2366         sprintf(bufx, "\"%s\"", version_string);
2367         buf = bufx;
2368         break;
2369
2370 #ifndef NO_BUILTIN_SIZE_TYPE
2371      case T_SIZE_TYPE:
2372         buf = SIZE_TYPE;
2373         break;
2374 #endif
2375
2376 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2377      case T_PTRDIFF_TYPE:
2378         buf = PTRDIFF_TYPE;
2379         break;
2380 #endif
2381
2382      case T_WCHAR_TYPE:
2383         buf = CPP_WCHAR_TYPE(pfile);
2384         break;
2385
2386      case T_USER_LABEL_PREFIX_TYPE:
2387         buf = USER_LABEL_PREFIX;
2388         break;
2389
2390      case T_REGISTER_PREFIX_TYPE:
2391         buf = REGISTER_PREFIX;
2392         break;
2393
2394      case T_CONST:
2395         bufx = (char *)alloca(4 * sizeof(int));
2396         sprintf(bufx, "%d", hp->value.ival);
2397         buf = bufx;
2398         break;
2399
2400      case T_SPECLINE:
2401         {
2402            long                line = ip->lineno;
2403            long                col = ip->colno;
2404
2405            adjust_position(CPP_LINE_BASE(ip), ip->cur, &line, &col);
2406
2407            bufx = (char *)alloca(10);
2408            sprintf(bufx, "%d", (int)line);
2409            buf = bufx;
2410         }
2411         break;
2412
2413      case T_DATE:
2414      case T_TIME:
2415         bufx = (char *)alloca(20);
2416         timebuf = timestamp(pfile);
2417         if (hp->type == T_DATE)
2418            sprintf(bufx, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2419                    timebuf->tm_mday, timebuf->tm_year + 1900);
2420         else
2421            sprintf(bufx, "\"%02d:%02d:%02d\"", timebuf->tm_hour,
2422                    timebuf->tm_min, timebuf->tm_sec);
2423         buf = bufx;
2424         break;
2425
2426      case T_SPEC_DEFINED:
2427         buf = " 0 ";            /* Assume symbol is not defined */
2428         ip = CPP_BUFFER(pfile);
2429         SKIP_WHITE_SPACE(ip->cur);
2430         if (*ip->cur == '(')
2431           {
2432              paren++;
2433              ip->cur++;         /* Skip over the paren */
2434              SKIP_WHITE_SPACE(ip->cur);
2435           }
2436         if (!is_idstart[*ip->cur])
2437            goto oops;
2438         if ((hp = cpp_lookup((const char *)ip->cur, -1, -1)))
2439           {
2440              buf = " 1 ";
2441           }
2442         while (is_idchar[*ip->cur])
2443            ++ip->cur;
2444         SKIP_WHITE_SPACE(ip->cur);
2445         if (paren)
2446           {
2447              if (*ip->cur != ')')
2448                 goto oops;
2449              ++ip->cur;
2450           }
2451         break;
2452
2453       oops:
2454
2455         cpp_error(pfile, "`defined' without an identifier");
2456         break;
2457
2458      default:
2459         cpp_error(pfile, "cccp error: invalid special hash type");      /* time for gdb */
2460         abort();
2461      }
2462    len = strlen(buf);
2463    CPP_RESERVE(pfile, len + 1);
2464    CPP_PUTS_Q(pfile, buf, len);
2465    CPP_NUL_TERMINATE_Q(pfile);
2466
2467    return;
2468 }
2469
2470 /* Initialize the built-in macros.  */
2471
2472 static void
2473 initialize_builtins(cpp_reader * pfile)
2474 {
2475    install("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2476    install("__DATE__", -1, T_DATE, 0, 0, -1);
2477    install("__FILE__", -1, T_FILE, 0, 0, -1);
2478    install("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2479    install("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2480    install("__VERSION__", -1, T_VERSION, 0, 0, -1);
2481 #ifndef NO_BUILTIN_SIZE_TYPE
2482    install("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2483 #endif
2484 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2485    install("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2486 #endif
2487    install("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2488    install("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2489    install("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2490    install("__TIME__", -1, T_TIME, 0, 0, -1);
2491    install("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2492    if (CPP_OPTIONS(pfile)->objc)
2493       install("__OBJC__", -1, T_CONST, 1, 0, -1);
2494 /*  This is supplied using a -D by the compiler driver
2495  * so that it is present only when truly compiling with GNU C.  */
2496 /*  install ("__GNUC__", -1, T_CONST, 2, 0, -1);  */
2497
2498    if (CPP_OPTIONS(pfile)->debug_output)
2499      {
2500         char                directive[2048];
2501         struct directive   *dp = &directive_table[0];
2502         struct tm          *timebuf = timestamp(pfile);
2503         cpp_buffer         *pbuffer = CPP_BUFFER(pfile);
2504
2505         while (CPP_PREV_BUFFER(pbuffer))
2506            pbuffer = CPP_PREV_BUFFER(pbuffer);
2507         sprintf(directive, " __BASE_FILE__ \"%s\"\n", pbuffer->nominal_fname);
2508         output_line_command(pfile, 0, same_file);
2509         pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2510                             dp);
2511
2512         sprintf(directive, " __VERSION__ \"%s\"\n", version_string);
2513         output_line_command(pfile, 0, same_file);
2514         pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2515                             dp);
2516
2517 #ifndef NO_BUILTIN_SIZE_TYPE
2518         sprintf(directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2519         output_line_command(pfile, 0, same_file);
2520         pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2521                             dp);
2522 #endif
2523
2524 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2525         sprintf(directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2526         output_line_command(pfile, 0, same_file);
2527         pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2528                             dp);
2529 #endif
2530
2531         sprintf(directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE(pfile));
2532         output_line_command(pfile, 0, same_file);
2533         pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2534                             dp);
2535
2536         sprintf(directive, " __DATE__ \"%s %2d %4d\"\n",
2537                 monthnames[timebuf->tm_mon],
2538                 timebuf->tm_mday, timebuf->tm_year + 1900);
2539         output_line_command(pfile, 0, same_file);
2540         pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2541                             dp);
2542
2543         sprintf(directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2544                 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2545         output_line_command(pfile, 0, same_file);
2546         pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2547                             dp);
2548
2549         sprintf(directive, " __STDC__ 1");
2550         output_line_command(pfile, 0, same_file);
2551         pass_thru_directive(directive, &directive[strlen(directive)],
2552                             pfile, dp);
2553      }
2554 }
2555
2556 /* Return 1 iff a token ending in C1 followed directly by a token C2
2557  * could cause mis-tokenization. */
2558
2559 static int
2560 unsafe_chars(int c1, int c2)
2561 {
2562 //   printf("unsafe %c %c ...", c1, c2);
2563    switch (c1)
2564      {
2565      case '+':
2566      case '-':
2567      case '.':
2568 //        printf(" no0\n");
2569         return 0;
2570      case '0':
2571      case '1':
2572      case '2':
2573      case '3':
2574      case '4':
2575      case '5':
2576      case '6':
2577      case '7':
2578      case '8':
2579      case '9':
2580      case 'e':
2581      case 'E':
2582         if (c2 == '-' || c2 == '+')
2583           {
2584 //             printf(" yes2\n");
2585              return 1;          /* could extend a pre-processing number */
2586           }
2587         goto letter;
2588      case 'L':
2589         if (c2 == '\'' || c2 == '\"')
2590           {
2591 //             printf(" yes3\n");
2592              return 1;          /* Could turn into L"xxx" or L'xxx'. */
2593           }
2594         goto letter;
2595       letter:
2596      case '_':
2597      case 'a':
2598      case 'b':
2599      case 'c':
2600      case 'd':
2601      case 'f':
2602      case 'g':
2603      case 'h':
2604      case 'i':
2605      case 'j':
2606      case 'k':
2607      case 'l':
2608      case 'm':
2609      case 'n':
2610      case 'o':
2611      case 'p':
2612      case 'q':
2613      case 'r':
2614      case 's':
2615      case 't':
2616      case 'u':
2617      case 'v':
2618      case 'w':
2619      case 'x':
2620      case 'y':
2621      case 'z':
2622      case 'A':
2623      case 'B':
2624      case 'C':
2625      case 'D':
2626      case 'F':
2627      case 'G':
2628      case 'H':
2629      case 'I':
2630      case 'J':
2631      case 'K':
2632      case 'M':
2633      case 'N':
2634      case 'O':
2635      case 'P':
2636      case 'Q':
2637      case 'R':
2638      case 'S':
2639      case 'T':
2640      case 'U':
2641      case 'V':
2642      case 'W':
2643      case 'X':
2644      case 'Y':
2645      case 'Z':
2646         /* We're in the middle of either a name or a pre-processing number. */
2647         if (is_idchar[c2] || c2 == '.')
2648           {
2649 //             printf(" yes4 %i %i\n", is_idchar[c2], c2 == '.');
2650              return 1;
2651           }
2652         else
2653           {
2654 //             printf(" no5\n");
2655              return 0;
2656           }
2657      case '<':
2658      case '>':
2659      case '!':
2660      case '%':
2661      case '#':
2662      case ':':
2663      case '^':
2664      case '&':
2665      case '|':
2666      case '*':
2667      case '/':
2668      case '=':
2669         if (c2 == c1 || c2 == '=')
2670           {
2671 //             printf(" yes6\n");
2672              return 1;
2673           }
2674         else
2675           {
2676 //             printf(" no7\n");
2677              return 0;
2678           }
2679      }
2680 //   printf(" no8\n");
2681    return 0;
2682 }
2683
2684 /* Expand a macro call.
2685  * HP points to the symbol that is the macro being called.
2686  * Put the result of expansion onto the input stack
2687  * so that subsequent input by our caller will use it.
2688  * 
2689  * If macro wants arguments, caller has already verified that
2690  * an argument list follows; arguments come from the input stack.  */
2691
2692 static void
2693 macroexpand(cpp_reader * pfile, HASHNODE * hp)
2694 {
2695    int                 nargs;
2696    DEFINITION         *defn = hp->value.defn;
2697    unsigned char      *xbuf;
2698    long                start_line, start_column;
2699    int                 xbuf_len;
2700    struct argdata     *args;
2701    long                old_written = CPP_WRITTEN(pfile);
2702
2703    int                 rest_args, rest_zero;
2704    int                 i;
2705
2706    pfile->output_escapes++;
2707    cpp_buf_line_and_col(cpp_file_buffer(pfile), &start_line, &start_column);
2708
2709    rest_zero = 0;
2710    args = NULL;
2711    nargs = defn->nargs;
2712
2713    if (nargs >= 0)
2714      {
2715         enum cpp_token      token;
2716
2717         token = CPP_OTHER;
2718
2719         args = (struct argdata *)alloca((nargs + 1) * sizeof(struct argdata));
2720
2721         for (i = 0; i < nargs; i++)
2722           {
2723              args[i].raw = args[i].expanded = 0;
2724              args[i].raw_length = 0;
2725              args[i].expand_length = args[i].stringified_length = -1;
2726              args[i].use_count = 0;
2727           }
2728
2729         /* Parse all the macro args that are supplied.  I counts them.
2730          * The first NARGS args are stored in ARGS.
2731          * The rest are discarded.  If rest_args is set then we assume
2732          * macarg absorbed the rest of the args. */
2733         i = 0;
2734         rest_args = 0;
2735         rest_args = 0;
2736         FORWARD(1);             /* Discard the open-parenthesis before the first arg.  */
2737         do
2738           {
2739              if (rest_args)
2740                 continue;
2741              if (i < nargs || (nargs == 0 && i == 0))
2742                {
2743                   unsigned char *bp;
2744                   
2745                   /* if we are working on last arg which absorbs rest of args... */
2746                   if (i == nargs - 1 && defn->rest_args)
2747                      rest_args = 1;
2748                   args[i].raw = CPP_WRITTEN(pfile);
2749                   token = macarg(pfile, rest_args);
2750                   args[i].raw_length = CPP_WRITTEN(pfile) - args[i].raw;
2751                   args[i].newlines = 0; /* FIXME */
2752                   bp = ARG_BASE + args[i].raw;
2753                   while (is_space[(unsigned char)(*bp)]) { bp++; }
2754                   args[i].raw_length -= bp - (ARG_BASE + args[i].raw);
2755                   args[i].raw = bp - ARG_BASE;
2756                   if (args[i].raw_length > 0)
2757                     {
2758                        bp = ARG_BASE + args[i].raw + args[i].raw_length - 1;
2759                        while (is_space[(unsigned char)(*bp)])
2760                          {
2761                             bp--; 
2762                             args[i].raw_length--;
2763                             if (args[i].raw_length < 1) break;
2764                          }
2765                     }
2766                }
2767              else
2768                 token = macarg(pfile, 0);
2769              if (token == CPP_EOF || token == CPP_POP)
2770                {
2771                   cpp_error_with_line(pfile, start_line, start_column,
2772                                       "unterminated macro call");
2773                   return;
2774                }
2775              i++;
2776           }
2777         while (token == CPP_COMMA);
2778
2779         /* If we got one arg but it was just whitespace, call that 0 args.  */
2780         if (i == 1)
2781           {
2782              unsigned char      *bp = ARG_BASE + args[0].raw;
2783              unsigned char      *lim = bp + args[0].raw_length;
2784
2785              /* cpp.texi says for foo ( ) we provide one argument.
2786               * However, if foo wants just 0 arguments, treat this as 0.  */
2787              if (nargs == 0)
2788                 while (bp != lim && is_space[*bp])
2789                    bp++;
2790              if (bp == lim)
2791                 i = 0;
2792           }
2793         /* Don't output an error message if we have already output one for
2794          * a parse error above.  */
2795         if (nargs == 0 && i > 0)
2796           {
2797              cpp_error(pfile, "arguments given to macro `%s'", hp->name);
2798           }
2799         else if (i < nargs)
2800           {
2801              if (i == nargs - 1 && defn->rest_args)
2802                 rest_zero = 1;
2803              else if (i == 0)
2804                 cpp_error(pfile, "macro `%s' used without args", hp->name);
2805              else if (i == 1)
2806                 cpp_error(pfile, "macro `%s' used with just one arg", hp->name);
2807              else
2808                 cpp_error(pfile, "macro `%s' used with only %d args",
2809                           hp->name, i);
2810           }
2811         else if (i > nargs)
2812           {
2813              cpp_error(pfile,
2814                        "macro `%s' used with too many (%d) args", hp->name, i);
2815           }
2816      }
2817    /* If macro wants zero args, we parsed the arglist for checking only.
2818     * Read directly from the macro definition.  */
2819    if (nargs <= 0)
2820      {
2821         xbuf = defn->expansion;
2822         xbuf_len = defn->length;
2823      }
2824    else
2825      {
2826         unsigned char      *exp = defn->expansion;
2827         int                 offset;     /* offset in expansion,
2828                                          * copied a piece at a time */
2829         int                 totlen;     /* total amount of exp buffer filled so far */
2830         reflist            *ap, *last_ap;
2831
2832         /* Macro really takes args.  Compute the expansion of this call.  */
2833
2834         /* Compute length in characters of the macro's expansion.
2835          * Also count number of times each arg is used.  */
2836         xbuf_len = defn->length;
2837         for (ap = defn->pattern; ap; ap = ap->next)
2838           {
2839              if (ap->stringify)
2840                {
2841                   struct argdata     *arg = &args[ap->argno];
2842
2843                   /* Stringify it it hasn't already been */
2844                   if (arg->stringified_length < 0)
2845                     {
2846                        int                 arglen = arg->raw_length;
2847                        int                 escaped = 0;
2848                        int                 in_string = 0;
2849                        int                 c;
2850
2851                        /* Initially need_space is -1.  Otherwise, 1 means the
2852                         * previous character was a space, but we suppressed it;
2853                         * 0 means the previous character was a non-space. */
2854                        int                 need_space = -1;
2855
2856                        i = 0;
2857                        arg->stringified = CPP_WRITTEN(pfile);
2858                        CPP_PUTC(pfile, '\"');   /* insert beginning quote */
2859                        for (; i < arglen; i++)
2860                          {
2861                             c = (ARG_BASE + arg->raw)[i];
2862
2863                             if (!in_string)
2864                               {
2865                                  /* Internal sequences of whitespace are replaced by
2866                                   * one space except within an string or char token. */
2867                                  if (is_space[c])
2868                                    {
2869                                       if (CPP_WRITTEN(pfile) > arg->stringified
2870                                           && (CPP_PWRITTEN(pfile))[-1] == '@')
2871                                         {
2872                                            /* "@ " escape markers are removed */
2873                                            CPP_ADJUST_WRITTEN(pfile, -1);
2874                                            continue;
2875                                         }
2876                                       if (need_space == 0)
2877                                          need_space = 1;
2878                                       continue;
2879                                    }
2880                                  else if (need_space > 0)
2881                                     CPP_PUTC(pfile, ' ');
2882                                  need_space = 0;
2883                               }
2884                             if (escaped)
2885                                escaped = 0;
2886                             else
2887                               {
2888                                  if (c == '\\')
2889                                     escaped = 1;
2890                                  if (in_string)
2891                                    {
2892                                       if (c == in_string)
2893                                          in_string = 0;
2894                                    }
2895                                  else if (c == '\"' || c == '\'')
2896                                     in_string = c;
2897                               }
2898
2899                             /* Escape these chars */
2900                             if (c == '\"' || (in_string && c == '\\'))
2901                                CPP_PUTC(pfile, '\\');
2902                             if (isprint(c))
2903                                CPP_PUTC(pfile, c);
2904                             else
2905                               {
2906                                  CPP_RESERVE(pfile, 4);
2907                                  sprintf((char *)CPP_PWRITTEN(pfile), "\\%03o",
2908                                          (unsigned int)c);
2909                                  CPP_ADJUST_WRITTEN(pfile, 4);
2910                               }
2911                          }
2912                        CPP_PUTC(pfile, '\"');   /* insert ending quote */
2913                        arg->stringified_length
2914                           = CPP_WRITTEN(pfile) - arg->stringified;
2915                     }
2916                   xbuf_len += args[ap->argno].stringified_length;
2917                }
2918              else if (ap->raw_before || ap->raw_after)
2919                {
2920                   /* Add 4 for two newline-space markers to prevent
2921                    * token concatenation.  */
2922                   xbuf_len += args[ap->argno].raw_length + 4;
2923                }
2924              else
2925                {
2926                   /* We have an ordinary (expanded) occurrence of the arg.
2927                    * So compute its expansion, if we have not already.  */
2928                   if (args[ap->argno].expand_length < 0)
2929                     {
2930                        args[ap->argno].expanded = CPP_WRITTEN(pfile);
2931                        cpp_expand_to_buffer(pfile,
2932                                             ARG_BASE + args[ap->argno].raw,
2933                                             args[ap->argno].raw_length);
2934
2935                        args[ap->argno].expand_length
2936                           = CPP_WRITTEN(pfile) - args[ap->argno].expanded;
2937                     }
2938                   /* Add 4 for two newline-space markers to prevent
2939                    * token concatenation.  */
2940                   xbuf_len += args[ap->argno].expand_length + 4;
2941                }
2942              if (args[ap->argno].use_count < 10)
2943                 args[ap->argno].use_count++;
2944           }
2945
2946         xbuf = (unsigned char *)xmalloc(xbuf_len + 1);
2947
2948         /* Generate in XBUF the complete expansion
2949          * with arguments substituted in.
2950          * TOTLEN is the total size generated so far.
2951          * OFFSET is the index in the definition
2952          * of where we are copying from.  */
2953         offset = totlen = 0;
2954         for (last_ap = NULL, ap = defn->pattern; ap;
2955              last_ap = ap, ap = ap->next)
2956           {
2957              struct argdata     *arg = &args[ap->argno];
2958              int                 count_before = totlen;
2959
2960              /* Add chars to XBUF.  */
2961              for (i = 0; i < ap->nchars; i++, offset++)
2962                 xbuf[totlen++] = exp[offset];
2963
2964              /* If followed by an empty rest arg with concatenation,
2965               * delete the last run of nonwhite chars.  */
2966              if (rest_zero && totlen > count_before
2967                  && ((ap->rest_args && ap->raw_before)
2968                      || (last_ap && last_ap->rest_args && last_ap->raw_after)))
2969                {
2970                   /* Delete final whitespace.  */
2971                   while (totlen > count_before && is_space[xbuf[totlen - 1]])
2972                      totlen--;
2973
2974                   /* Delete the nonwhites before them.  */
2975                   while (totlen > count_before && !is_space[xbuf[totlen - 1]])
2976                      totlen--;
2977                }
2978              if (ap->stringify != 0)
2979                {
2980                   memcpy(xbuf + totlen, ARG_BASE + arg->stringified,
2981                          arg->stringified_length);
2982                   totlen += arg->stringified_length;
2983                }
2984              else if (ap->raw_before || ap->raw_after)
2985                {
2986                   unsigned char      *p1 = ARG_BASE + arg->raw;
2987                   unsigned char      *l1 = p1 + arg->raw_length;
2988
2989                   if (ap->raw_before)
2990                     {
2991                        while (p1 != l1 && is_space[*p1])
2992                           p1++;
2993                        while (p1 != l1 && is_idchar[*p1])
2994                           xbuf[totlen++] = *p1++;
2995                     }
2996                   if (ap->raw_after)
2997                     {
2998                        /* Arg is concatenated after: delete trailing whitespace,
2999                         * whitespace markers, and no-reexpansion markers.  */
3000                        while (p1 != l1)
3001                          {
3002                             if (is_space[l1[-1]])
3003                                l1--;
3004                             else if (l1[-1] == '-')
3005                               {
3006                                  unsigned char      *p2 = l1 - 1;
3007
3008                                  /* If a `-' is preceded by an odd number of newlines then it
3009                                   * and the last newline are a no-reexpansion marker.  */
3010                                  while (p2 != p1 && p2[-1] == '\n')
3011                                     p2--;
3012                                  if ((l1 - 1 - p2) & 1)
3013                                    {
3014                                       l1 -= 2;
3015                                    }
3016                                  else
3017                                     break;
3018                               }
3019                             else
3020                                break;
3021                          }
3022                     }
3023                   memcpy(xbuf + totlen, p1, l1 - p1);
3024                   totlen += l1 - p1;
3025                }
3026              else
3027                {
3028                   unsigned char      *expanded = ARG_BASE + arg->expanded;
3029
3030                   if (!ap->raw_before && totlen > 0 && arg->expand_length
3031                       && unsafe_chars(xbuf[totlen - 1], expanded[0]))
3032                     {
3033                        xbuf[totlen++] = '@';
3034                        xbuf[totlen++] = ' ';
3035                     }
3036                   memcpy(xbuf + totlen, expanded, arg->expand_length);
3037                   totlen += arg->expand_length;
3038
3039                   if (!ap->raw_after && totlen > 0 && offset < defn->length
3040                       && unsafe_chars(xbuf[totlen - 1], exp[offset]))
3041                     {
3042                        xbuf[totlen++] = '@';
3043                        xbuf[totlen++] = ' ';
3044                     }
3045                   /* If a macro argument with newlines is used multiple times,
3046                    * then only expand the newlines once.  This avoids creating
3047                    * output lines which don't correspond to any input line,
3048                    * which confuses gdb and gcov.  */
3049                   if (arg->use_count > 1 && arg->newlines > 0)
3050                     {
3051                        /* Don't bother doing change_newlines for subsequent
3052                         * uses of arg.  */
3053                        arg->use_count = 1;
3054                        arg->expand_length
3055                           = change_newlines(expanded, arg->expand_length);
3056                     }
3057                }
3058
3059              if (totlen > xbuf_len)
3060                 abort();
3061           }
3062         /* if there is anything left of the definition
3063          * after handling the arg list, copy that in too. */
3064
3065         for (i = offset; i < defn->length; i++)
3066           {
3067              /* if we've reached the end of the macro */
3068              if (exp[i] == ')')
3069                 rest_zero = 0;
3070              if (!(rest_zero && last_ap && last_ap->rest_args
3071                    && last_ap->raw_after))
3072                 xbuf[totlen++] = exp[i];
3073           }
3074
3075         xbuf[totlen] = 0;
3076         xbuf_len = totlen;
3077      }
3078
3079    pfile->output_escapes--;
3080
3081    /* Now put the expansion on the input stack
3082     * so our caller will commence reading from it.  */
3083    push_macro_expansion(pfile, xbuf, xbuf_len, hp);
3084    CPP_BUFFER(pfile)->has_escapes = 1;
3085
3086    /* Pop the space we've used in the token_buffer for argument expansion. */
3087    CPP_SET_WRITTEN(pfile, old_written);
3088
3089    /* Recursive macro use sometimes works traditionally.
3090     * #define foo(x,y) bar (x (y,0), y)
3091     * foo (foo, baz)  */
3092
3093    hp->type = T_DISABLED;
3094 }
3095
3096 static void
3097 push_macro_expansion(cpp_reader * pfile, unsigned char *xbuf, int xbuf_len,
3098                      HASHNODE * hp)
3099 {
3100    cpp_buffer         *mbuf = cpp_push_buffer(pfile, xbuf, xbuf_len);
3101
3102    mbuf->cleanup = macro_cleanup;
3103    mbuf->data = hp;
3104
3105    /* The first chars of the expansion should be a "@ " added by
3106     * collect_expansion.  This is to prevent accidental token-pasting
3107     * between the text preceding the macro invocation, and the macro
3108     * expansion text.
3109     * 
3110     * We would like to avoid adding unneeded spaces (for the sake of
3111     * tools that use cpp, such as imake).  In some common cases we can
3112     * tell that it is safe to omit the space.
3113     * 
3114     * The character before the macro invocation cannot have been an
3115     * idchar (or else it would have been pasted with the idchars of
3116     * the macro name).  Therefore, if the first non-space character
3117     * of the expansion is an idchar, we do not need the extra space
3118     * to prevent token pasting.
3119     * 
3120     * Also, we don't need the extra space if the first char is '(',
3121     * or some other (less common) characters.  */
3122
3123    if (xbuf[0] == '@' && xbuf[1] == ' '
3124        && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3125            || xbuf[2] == '\"'))
3126       mbuf->cur += 2;
3127 }
3128
3129 /* Like cpp_get_token, except that it does not read past end-of-line.
3130  * Also, horizontal space is skipped, and macros are popped.  */
3131
3132 static enum cpp_token
3133 get_directive_token(cpp_reader * pfile)
3134 {
3135    for (;;)
3136      {
3137         long                old_written = CPP_WRITTEN(pfile);
3138         enum cpp_token      token;
3139
3140         cpp_skip_hspace(pfile);
3141         if (PEEKC() == '\n')
3142            return CPP_VSPACE;
3143         token = cpp_get_token(pfile);
3144         switch (token)
3145           {
3146           case CPP_POP:
3147              if (!CPP_IS_MACRO_BUFFER(CPP_BUFFER(pfile)))
3148                 return token;
3149              /* ... else fall though ... */
3150           case CPP_HSPACE:
3151           case CPP_COMMENT:
3152              CPP_SET_WRITTEN(pfile, old_written);
3153              break;
3154           default:
3155              return token;
3156           }
3157      }
3158 }
3159
3160 /* Handle #include and #import.
3161  * This function expects to see "fname" or <fname> on the input.
3162  * 
3163  * The input is normally in part of the output_buffer following
3164  * CPP_WRITTEN, and will get overwritten by output_line_command.
3165  * I.e. in input file specification has been popped by handle_directive.
3166  * This is safe. */
3167
3168 static int
3169 do_include(cpp_reader * pfile, struct directive *keyword,
3170            unsigned char *unused1 __UNUSED__, unsigned char *unused2 __UNUSED__)
3171 {
3172    int                 importing = (keyword->type == T_IMPORT);
3173    int                 skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3174    char               *fname;   /* Dynamically allocated fname buffer */
3175    unsigned char      *fbeg, *fend;     /* Beginning and end of fname */
3176    enum cpp_token      token;
3177
3178    /* Chain of dirs to search */
3179    file_name_list     *search_start = CPP_OPTIONS(pfile)->include;
3180    file_name_list      dsp[1];  /* First in chain, if #include "..." */
3181    file_name_list     *searchptr = 0;
3182    long                old_written = CPP_WRITTEN(pfile);
3183    int                 flen;
3184    int                 f;       /* file number */
3185    int                 angle_brackets = 0;      /* 0 for "...", 1 for <...> */
3186
3187    f = -1;                      /* JF we iz paranoid! */
3188
3189    if (importing && CPP_OPTIONS(pfile)->warn_import
3190        && !CPP_OPTIONS(pfile)->inhibit_warnings
3191        && !CPP_BUFFER(pfile)->system_header_p && !pfile->import_warning)
3192      {
3193         pfile->import_warning = 1;
3194         cpp_warning(pfile, "using `#import' is not recommended");
3195         fprintf(stderr,
3196                 "The fact that a certain header file need not be processed more than once\n");
3197         fprintf(stderr,
3198                 "should be indicated in the header file, not where it is used.\n");
3199         fprintf(stderr,
3200                 "The best way to do this is with a conditional of this form:\n\n");
3201         fprintf(stderr, "  #ifndef _FOO_H_INCLUDED\n");
3202         fprintf(stderr, "  #define _FOO_H_INCLUDED\n");
3203         fprintf(stderr, "  ... <real contents of file> ...\n");
3204         fprintf(stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
3205         fprintf(stderr, "Then users can use `#include' any number of times.\n");
3206         fprintf(stderr,
3207                 "GNU C automatically avoids processing the file more than once\n");
3208         fprintf(stderr, "when it is equipped with such a conditional.\n");
3209      }
3210    pfile->parsing_include_directive++;
3211    token = get_directive_token(pfile);
3212    pfile->parsing_include_directive--;
3213
3214    if (token == CPP_STRING)
3215      {
3216         /* FIXME - check no trailing garbage */
3217         fbeg = pfile->token_buffer + old_written + 1;
3218         fend = CPP_PWRITTEN(pfile) - 1;
3219         if (fbeg[-1] == '<')
3220           {
3221              angle_brackets = 1;
3222              /* If -I-, start with the first -I dir after the -I-.  */
3223              if (CPP_OPTIONS(pfile)->first_bracket_include)
3224                 search_start = CPP_OPTIONS(pfile)->first_bracket_include;
3225           }
3226         /* If -I- was specified, don't search current dir, only spec'd ones. */
3227         else if (!CPP_OPTIONS(pfile)->ignore_srcdir)
3228           {
3229              cpp_buffer         *fp;
3230
3231              /* We have "filename".  Figure out directory this source
3232               * file is coming from and put it on the front of the list. */
3233
3234              for (fp = CPP_BUFFER(pfile); fp; fp = CPP_PREV_BUFFER(fp))
3235                {
3236                   int                 n;
3237                   const char         *ep, *nam;
3238
3239                   if ((nam = fp->nominal_fname))
3240                     {
3241                        /* Found a named file.  Figure out dir of the file,
3242                         * and put it in front of the search list.  */
3243                        dsp[0].next = search_start;
3244                        search_start = dsp;
3245 #ifndef VMS
3246                        ep = strrchr(nam, '/');
3247 #else /* VMS */
3248                        ep = strrchr(nam, ']');
3249                        if (!ep)
3250                           ep = strrchr(nam, '>');
3251                        if (!ep)
3252                           ep = strrchr(nam, ':');
3253                        if (ep)
3254                           ep++;
3255 #endif /* VMS */
3256                        if (ep)
3257                          {
3258                             n = ep - nam;
3259                             dsp[0].fname = (char *)alloca(n + 1);
3260                             strncpy(dsp[0].fname, nam, n);
3261                             dsp[0].fname[n] = '\0';
3262                             if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3263                                pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3264                          }
3265                        else
3266                          {
3267                             dsp[0].fname = 0;   /* Current directory */
3268                          }
3269                        dsp[0].got_name_map = 0;
3270                        break;
3271                     }
3272                }
3273           }
3274      }
3275    else
3276      {
3277         cpp_error(pfile,
3278                   "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3279         CPP_SET_WRITTEN(pfile, old_written);
3280         skip_rest_of_line(pfile);
3281         return 0;
3282      }
3283
3284    *fend = 0;
3285
3286    token = get_directive_token(pfile);
3287    if (token != CPP_VSPACE)
3288      {
3289         cpp_error(pfile, "junk at end of `#include'");
3290         while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3291            token = get_directive_token(pfile);
3292      }
3293    /* For #include_next, skip in the search path
3294     * past the dir in which the containing file was found.  */
3295    if (skip_dirs)
3296      {
3297         cpp_buffer         *fp;
3298
3299         for (fp = CPP_BUFFER(pfile); fp; fp = CPP_PREV_BUFFER(fp))
3300            if (fp->fname)
3301              {
3302                 /* fp->dir is null if the containing file was specified with
3303                  * an absolute file name.  In that case, don't skip anything.  */
3304                 if (fp->dir == SELF_DIR_DUMMY)
3305                    search_start = CPP_OPTIONS(pfile)->include;
3306                 else if (fp->dir)
3307                    search_start = fp->dir->next;
3308                 break;
3309              }
3310      }
3311    CPP_SET_WRITTEN(pfile, old_written);
3312
3313    flen = fend - fbeg;
3314
3315    if (flen == 0)
3316      {
3317         cpp_error(pfile, "empty file name in `#%s'", keyword->name);
3318         return 0;
3319      }
3320    /* Allocate this permanently, because it gets stored in the definitions
3321     * of macros.  */
3322    fname = (char *)xmalloc(pfile->max_include_len + flen + 4);
3323    /* + 2 above for slash and terminating null.  */
3324    /* + 2 added for '.h' on VMS (to support '#include filename') */
3325
3326    /* If specified file name is absolute, just open it.  */
3327
3328 #ifndef __EMX__
3329    if (*fbeg == '/')
3330 #else
3331    if (_fnisabs(fbeg))
3332 #endif
3333      {
3334         strncpy(fname, (const char *)fbeg, flen);
3335         fname[flen] = 0;
3336         if (redundant_include_p(pfile, fname))
3337            return 0;
3338         if (importing)
3339            f = lookup_import(pfile, fname, NULL);
3340         else
3341            f = open_include_file(pfile, fname, NULL);
3342         if (f == -2)
3343            return 0;            /* Already included this file */
3344      }
3345    else
3346      {
3347         /* Search directory path, trying to open the file.
3348          * Copy each filename tried into FNAME.  */
3349
3350         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
3351           {
3352              if (searchptr->fname)
3353                {
3354                   /* The empty string in a search path is ignored.
3355                    * This makes it possible to turn off entirely
3356                    * a standard piece of the list.  */
3357                   if (searchptr->fname[0] == 0)
3358                      continue;
3359                   strcpy(fname, searchptr->fname);
3360                   strcat(fname, "/");
3361                   fname[strlen(fname) + flen] = 0;
3362                }
3363              else
3364                {
3365                   fname[0] = 0;
3366                }
3367              strncat(fname, (const char *)fbeg, flen);
3368 #ifdef VMS
3369              /* Change this 1/2 Unix 1/2 VMS file specification into a
3370               * full VMS file specification */
3371              if (searchptr->fname && (searchptr->fname[0] != 0))
3372                {
3373                   /* Fix up the filename */
3374                   hack_vms_include_specification(fname);
3375                }
3376              else
3377                {
3378                   /* This is a normal VMS filespec, so use it unchanged.  */
3379                   strncpy(fname, fbeg, flen);
3380                   fname[flen] = 0;
3381                   /* if it's '#include filename', add the missing .h */
3382                   if (!strchr(fname, '.'))
3383                     {
3384                        strcat(fname, ".h");
3385                     }
3386                }
3387 #endif /* VMS */
3388              /* ??? There are currently 3 separate mechanisms for avoiding processing
3389               * of redundant include files: #import, #pragma once, and
3390               * redundant_include_p.  It would be nice if they were unified.  */
3391              if (redundant_include_p(pfile, fname))
3392                 return 0;
3393              if (importing)
3394                 f = lookup_import(pfile, fname, searchptr);
3395              else
3396                 f = open_include_file(pfile, fname, searchptr);
3397              if (f == -2)
3398                 return 0;       /* Already included this file */
3399 #ifdef EACCES
3400              else if (f == -1 && errno == EACCES)
3401                 cpp_warning(pfile, "Header file %s exists, but is not readable",
3402                             fname);
3403 #endif
3404              if (f >= 0)
3405                 break;
3406           }
3407      }
3408
3409    if (f < 0)
3410      {
3411         /* A file that was not found.  */
3412         strncpy(fname, (const char *)fbeg, flen);
3413         fname[flen] = 0;
3414         /* If generating dependencies and -MG was specified, we assume missing
3415          * files are leaf files, living in the same directory as the source file
3416          * or other similar place; these missing files may be generated from
3417          * other files and may not exist yet (eg: y.tab.h).  */
3418
3419         if (CPP_OPTIONS(pfile)->print_deps_missing_files
3420             && CPP_PRINT_DEPS(pfile)
3421             > (angle_brackets || (pfile->system_include_depth > 0)))
3422           {
3423              /* If it was requested as a system header file,
3424               * then assume it belongs in the first place to look for such.  */
3425              if (angle_brackets)
3426                {
3427                   for (searchptr = search_start; searchptr;
3428                        searchptr = searchptr->next)
3429                     {
3430                        if (searchptr->fname)
3431                          {
3432                             char               *p;
3433
3434                             if (searchptr->fname[0] == 0)
3435                                continue;
3436                             p = (char *)alloca(strlen(searchptr->fname)
3437                                                + strlen(fname) + 2);
3438                             strcpy(p, searchptr->fname);
3439                             strcat(p, "/");
3440                             strcat(p, fname);
3441                             deps_output(pfile, p, ' ');
3442                             break;
3443                          }
3444                     }
3445                }
3446              else
3447                {
3448                   /* Otherwise, omit the directory, as if the file existed
3449                    * in the directory with the source.  */
3450                   deps_output(pfile, fname, ' ');
3451                }
3452           }
3453         /* If -M was specified, and this header file won't be added to the
3454          * dependency list, then don't count this as an error, because we can
3455          * still produce correct output.  Otherwise, we can't produce correct
3456          * output, because there may be dependencies we need inside the missing
3457          * file, and we don't know what directory this missing file exists in. */
3458         else if (CPP_PRINT_DEPS(pfile)
3459                  && (CPP_PRINT_DEPS(pfile)
3460                      <= (angle_brackets || (pfile->system_include_depth > 0))))
3461            cpp_warning(pfile, "No include path in which to find %s", fname);
3462         else if (search_start)
3463            cpp_error_from_errno(pfile, fname);
3464         else
3465            cpp_error(pfile, "No include path in which to find %s", fname);
3466      }
3467    else
3468      {
3469         /* Check to see if this include file is a once-only include file.
3470          * If so, give up.  */
3471
3472         file_name_list     *ptr;
3473
3474         for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next)
3475           {
3476              if (!strcmp(ptr->fname, fname))
3477                {
3478                   close(f);
3479                   return 0;     /* This file was once'd. */
3480                }
3481           }
3482
3483         for (ptr = pfile->all_include_files; ptr; ptr = ptr->next)
3484           {
3485              if (!strcmp(ptr->fname, fname))
3486                 break;          /* This file was included before. */
3487           }
3488
3489         if (!ptr)
3490           {
3491              /* This is the first time for this file.  */
3492              /* Add it to list of files included.  */
3493
3494              ptr = (file_name_list *) xmalloc(sizeof(file_name_list));
3495
3496              ptr->control_macro = 0;
3497              ptr->c_system_include_path = 0;
3498              ptr->next = pfile->all_include_files;
3499              pfile->all_include_files = ptr;
3500              ptr->fname = savestring(fname);
3501              ptr->got_name_map = 0;
3502
3503              /* For -M, add this file to the dependencies.  */
3504              if (CPP_PRINT_DEPS(pfile)
3505                  > (angle_brackets || (pfile->system_include_depth > 0)))
3506                 deps_output(pfile, fname, ' ');
3507           }
3508         /* Handle -H option.  */
3509         if (CPP_OPTIONS(pfile)->print_include_names)
3510           {
3511              cpp_buffer         *buf = CPP_BUFFER(pfile);
3512
3513              while ((buf = CPP_PREV_BUFFER(buf)))
3514                 putc('.', stderr);
3515              fprintf(stderr, "%s\n", fname);
3516           }
3517         if (angle_brackets)
3518            pfile->system_include_depth++;
3519
3520         /* Actually process the file.  */
3521
3522         /* Record file on "seen" list for #import. */
3523         add_import(pfile, f, fname);
3524
3525         /* Actually process the file */
3526         cpp_push_buffer(pfile, NULL, 0);
3527         if (finclude(pfile, f, fname, is_system_include(pfile, fname),
3528                      searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3529           {
3530              output_line_command(pfile, 0, enter_file);
3531              pfile->only_seen_white = 2;
3532           }
3533         if (angle_brackets)
3534            pfile->system_include_depth--;
3535      }
3536    return 0;
3537 }
3538
3539 /* Return nonzero if there is no need to include file NAME
3540  * because it has already been included and it contains a conditional
3541  * to make a repeated include do nothing.  */
3542
3543 static int
3544 redundant_include_p(cpp_reader * pfile, char *name)
3545 {
3546    file_name_list     *l = pfile->all_include_files;
3547
3548    for (; l; l = l->next)
3549       if (!strcmp(name, l->fname)
3550           && l->control_macro
3551           && cpp_lookup((const char *)l->control_macro, -1, -1))
3552          return 1;
3553    return 0;
3554 }
3555
3556 /* Return nonzero if the given FILENAME is an absolute pathname which
3557  * designates a file within one of the known "system" include file
3558  * directories.  We assume here that if the given FILENAME looks like
3559  * it is the name of a file which resides either directly in a "system"
3560  * include file directory, or within any subdirectory thereof, then the
3561  * given file must be a "system" include file.  This function tells us
3562  * if we should suppress pedantic errors/warnings for the given FILENAME.
3563  * 
3564  * The value is 2 if the file is a C-language system header file
3565  * for which C++ should (on most systems) assume `extern "C"'.  */
3566
3567 static int
3568 is_system_include(cpp_reader * pfile, char *filename)
3569 {
3570    file_name_list     *searchptr;
3571
3572    for (searchptr = CPP_OPTIONS(pfile)->first_system_include; searchptr;
3573         searchptr = searchptr->next)
3574       if (searchptr->fname)
3575         {
3576            char               *sys_dir = searchptr->fname;
3577            unsigned            length = strlen(sys_dir);
3578
3579            if (!strncmp(sys_dir, filename, length) && filename[length] == '/')
3580              {
3581                 if (searchptr->c_system_include_path)
3582                    return 2;
3583                 else
3584                    return 1;
3585              }
3586         }
3587    return 0;
3588 }
3589
3590 /*
3591  * Install a name in the assertion hash table.
3592  *
3593  * If LEN is >= 0, it is the length of the name.
3594  * Otherwise, compute the length by scanning the entire name.
3595  *
3596  * If HASH is >= 0, it is the precomputed hash code.
3597  * Otherwise, compute the hash code.
3598  */
3599 static ASSERTION_HASHNODE *
3600 assertion_install(cpp_reader * pfile, const char *name, int len, int hash)
3601 {
3602    ASSERTION_HASHNODE *hp;
3603    int                 i, bucket;
3604
3605    i = sizeof(ASSERTION_HASHNODE) + len + 1;
3606    hp = (ASSERTION_HASHNODE *) xmalloc(i);
3607    bucket = hash;
3608    hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3609    hp->next = pfile->assertion_hashtab[bucket];
3610    pfile->assertion_hashtab[bucket] = hp;
3611    hp->prev = NULL;
3612    if (hp->next)
3613       hp->next->prev = hp;
3614    hp->length = len;
3615    hp->value = 0;
3616    hp->name = ((char *)hp) + sizeof(ASSERTION_HASHNODE);
3617    memcpy(hp->name, name, len);
3618    hp->name[len] = 0;
3619    return hp;
3620 }
3621 /*
3622  * find the most recent hash node for name name (ending with first
3623  * non-identifier char) installed by install
3624  *
3625  * If LEN is >= 0, it is the length of the name.
3626  * Otherwise, compute the length by scanning the entire name.
3627  *
3628  * If HASH is >= 0, it is the precomputed hash code.
3629  * Otherwise, compute the hash code.
3630  */
3631
3632 static ASSERTION_HASHNODE *
3633 assertion_lookup(cpp_reader * pfile, const char *name, int len, int hash)
3634 {
3635    ASSERTION_HASHNODE *bucket;
3636
3637    bucket = pfile->assertion_hashtab[hash];
3638    while (bucket)
3639      {
3640         if (bucket->length == len && strncmp(bucket->name, name, len) == 0)
3641            return bucket;
3642         bucket = bucket->next;
3643      }
3644    return NULL;
3645 }
3646
3647 static void
3648 delete_assertion(ASSERTION_HASHNODE * hp)
3649 {
3650    struct tokenlist_list *tail;
3651
3652    if (hp->prev)
3653       hp->prev->next = hp->next;
3654    if (hp->next)
3655       hp->next->prev = hp->prev;
3656
3657    for (tail = hp->value; tail;)
3658      {
3659         struct tokenlist_list *next = tail->next;
3660
3661         free_token_list(tail->tokens);
3662         free(tail);
3663         tail = next;
3664      }
3665
3666    /* make sure that the bucket chain header that
3667     * the deleted guy was on points to the right thing afterwards. */
3668    if (hp == *hp->bucket_hdr)
3669       *hp->bucket_hdr = hp->next;
3670
3671    free(hp);
3672 }
3673
3674 /* Convert a character string literal into a nul-terminated string.
3675  * The input string is [IN ... LIMIT).
3676  * The result is placed in RESULT.  RESULT can be the same as IN.
3677  * The value returned in the end of the string written to RESULT,
3678  * or NULL on error.  */
3679
3680 static char        *
3681 convert_string(cpp_reader * pfile, char *result, char *in, char *limit,
3682                int handle_escapes)
3683 {
3684    unsigned char       c;
3685
3686    c = *in++;
3687    if (c != '\"')
3688       return NULL;
3689    while (in < limit)
3690      {
3691         c = *in++;
3692
3693         switch (c)
3694           {
3695           case '\0':
3696              return NULL;
3697           case '\"':
3698              limit = in;
3699              break;
3700           case '\\':
3701              if (handle_escapes)
3702                {
3703                   char               *bpc = in;
3704                   int                 i =
3705                      (unsigned char)cpp_parse_escape(pfile, &bpc);
3706
3707                   in = bpc;
3708                   if (i >= 0)
3709                      *result++ = (unsigned char)c;
3710                   break;
3711                }
3712              /* else fall through */
3713           default:
3714              *result++ = c;
3715           }
3716      }
3717    *result = 0;
3718    return result;
3719 }
3720
3721 /*
3722  * interpret #line command.  Remembers previously seen fnames
3723  * in its very own hash table.
3724  */
3725 #define FNAME_HASHSIZE 37
3726
3727 static int
3728 do_line(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3729         unsigned char *unused1 __UNUSED__, unsigned char *unused2 __UNUSED__)
3730 {
3731    cpp_buffer         *ip = CPP_BUFFER(pfile);
3732    int                 new_lineno;
3733    long                old_written = CPP_WRITTEN(pfile);
3734    enum file_change_code file_change = same_file;
3735    enum cpp_token      token;
3736
3737    token = get_directive_token(pfile);
3738
3739    if (token != CPP_NUMBER || !isdigit(pfile->token_buffer[old_written]))
3740      {
3741         cpp_error(pfile, "invalid format `#line' command");
3742         goto bad_line_directive;
3743      }
3744    /* The Newline at the end of this line remains to be processed.
3745     * To put the next line at the specified line number,
3746     * we must store a line number now that is one less.  */
3747    new_lineno = atoi((char *)(pfile->token_buffer + old_written)) - 1;
3748    CPP_SET_WRITTEN(pfile, old_written);
3749
3750    /* NEW_LINENO is one less than the actual line number here.  */
3751    if (CPP_PEDANTIC(pfile) && new_lineno < 0)
3752       cpp_pedwarn(pfile, "line number out of range in `#line' command");
3753
3754    token = get_directive_token(pfile);
3755
3756    if (token == CPP_STRING)
3757      {
3758         char               *fname = (char *)pfile->token_buffer + old_written;
3759         char               *end_name;
3760         static HASHNODE    *fname_table[FNAME_HASHSIZE];
3761         HASHNODE           *hp, **hash_bucket;
3762         unsigned char      *p;
3763         long                num_start;
3764         int                 fname_length;
3765
3766         /* Turn the file name, which is a character string literal,
3767          * into a null-terminated string.  Do this in place.  */
3768         end_name =
3769            convert_string(pfile, fname, fname, (char *)CPP_PWRITTEN(pfile), 1);
3770         if (!end_name)
3771           {
3772              cpp_error(pfile, "invalid format `#line' command");
3773              goto bad_line_directive;
3774           }
3775         fname_length = end_name - fname;
3776
3777         num_start = CPP_WRITTEN(pfile);
3778         token = get_directive_token(pfile);
3779         if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3780           {
3781              p = pfile->token_buffer + num_start;
3782              if (CPP_PEDANTIC(pfile))
3783                 cpp_pedwarn(pfile, "garbage at end of `#line' command");
3784
3785              if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3786                {
3787                   cpp_error(pfile, "invalid format `#line' command");
3788                   goto bad_line_directive;
3789                }
3790              if (*p == '1')
3791                 file_change = enter_file;
3792              else if (*p == 2)
3793                 file_change = leave_file;
3794              else if (*p == 3)
3795                 ip->system_header_p = 1;
3796              else               /* if (*p == 4) */
3797                 ip->system_header_p = 2;
3798
3799              CPP_SET_WRITTEN(pfile, num_start);
3800              token = get_directive_token(pfile);
3801              p = pfile->token_buffer + num_start;
3802              if (token == CPP_NUMBER && p[1] == '\0'
3803                  && (*p == '3' || *p == '4'))
3804                {
3805                   ip->system_header_p = *p == 3 ? 1 : 2;
3806                   token = get_directive_token(pfile);
3807                }
3808              if (token != CPP_VSPACE)
3809                {
3810                   cpp_error(pfile, "invalid format `#line' command");
3811                   goto bad_line_directive;
3812                }
3813           }
3814         hash_bucket = &fname_table[hashf(fname, fname_length, FNAME_HASHSIZE)];
3815         for (hp = *hash_bucket; hp; hp = hp->next)
3816            if (hp->length == fname_length &&
3817                strncmp(hp->value.cpval, fname, fname_length) == 0)
3818              {
3819                 ip->nominal_fname = hp->value.cpval;
3820                 break;
3821              }
3822         if (!hp)
3823           {
3824              /* Didn't find it; cons up a new one.  */
3825              hp = (HASHNODE *) xcalloc(1, sizeof(HASHNODE) + fname_length + 1);
3826              hp->next = *hash_bucket;
3827              *hash_bucket = hp;
3828
3829              hp->length = fname_length;
3830              ip->nominal_fname = hp->value.cpval =
3831                 ((char *)hp) + sizeof(HASHNODE);
3832              memcpy(hp->value.cpval, fname, fname_length);
3833           }
3834      }
3835    else if (token != CPP_VSPACE && token != CPP_EOF)
3836      {
3837         cpp_error(pfile, "invalid format `#line' command");
3838         goto bad_line_directive;
3839      }
3840    ip->lineno = new_lineno;
3841  bad_line_directive:
3842    skip_rest_of_line(pfile);
3843    CPP_SET_WRITTEN(pfile, old_written);
3844    output_line_command(pfile, 0, file_change);
3845    return 0;
3846 }
3847
3848 /*
3849  * remove the definition of a symbol from the symbol table.
3850  * according to un*x /lib/cpp, it is not an error to undef
3851  * something that has no definitions, so it isn't one here either.
3852  */
3853
3854 static int
3855 do_undef(cpp_reader * pfile, struct directive *keyword, unsigned char *buf,
3856          unsigned char *limit)
3857 {
3858    int                 sym_length;
3859    HASHNODE           *hp;
3860    unsigned char      *orig_buf = buf;
3861
3862    SKIP_WHITE_SPACE(buf);
3863    sym_length = check_macro_name(pfile, buf, "macro");
3864
3865    while ((hp = cpp_lookup((const char *)buf, sym_length, -1)))
3866      {
3867         /* If we are generating additional info for debugging (with -g) we
3868          * need to pass through all effective #undef commands.  */
3869         if (CPP_OPTIONS(pfile)->debug_output && keyword)
3870            pass_thru_directive((char *)orig_buf, (char *)limit, pfile, keyword);
3871         if (hp->type != T_MACRO)
3872            cpp_warning(pfile, "undefining `%s'", hp->name);
3873         delete_macro(hp);
3874      }
3875
3876    if (CPP_PEDANTIC(pfile))
3877      {
3878         buf += sym_length;
3879         SKIP_WHITE_SPACE(buf);
3880         if (buf != limit)
3881            cpp_pedwarn(pfile, "garbage after `#undef' directive");
3882      }
3883    return 0;
3884 }
3885
3886 /*
3887  * Report an error detected by the program we are processing.
3888  * Use the text of the line in the error message.
3889  * (We use error because it prints the filename & line#.)
3890  */
3891
3892 static int
3893 do_error(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3894          unsigned char *buf, unsigned char *limit)
3895 {
3896    int                 length = limit - buf;
3897    unsigned char      *copy = (unsigned char *)xmalloc(length + 1);
3898
3899    memcpy(copy, buf, length);
3900    copy[length] = 0;
3901    SKIP_WHITE_SPACE(copy);
3902    cpp_error(pfile, "#error %s", copy);
3903    return 0;
3904 }
3905
3906 /*
3907  * Report a warning detected by the program we are processing.
3908  * Use the text of the line in the warning message, then continue.
3909  * (We use error because it prints the filename & line#.)
3910  */
3911
3912 static int
3913 do_warning(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3914            unsigned char *buf, unsigned char *limit)
3915 {
3916    int                 length = limit - buf;
3917    unsigned char      *copy = (unsigned char *)xmalloc(length + 1);
3918
3919    memcpy(copy, buf, length);
3920    copy[length] = 0;
3921    SKIP_WHITE_SPACE(copy);
3922    cpp_warning(pfile, "#warning %s", copy);
3923    return 0;
3924 }
3925
3926 /* Remember the name of the current file being read from so that we can
3927  * avoid ever including it again.  */
3928
3929 static int
3930 do_once(cpp_reader * pfile)
3931 {
3932    cpp_buffer         *ip = NULL;
3933    file_name_list     *new_;
3934
3935    for (ip = CPP_BUFFER(pfile);; ip = CPP_PREV_BUFFER(ip))
3936      {
3937         if (!ip)
3938            return 0;
3939         if (ip->fname)
3940            break;
3941      }
3942
3943    new_ = (file_name_list *) xmalloc(sizeof(file_name_list));
3944
3945    new_->next = pfile->dont_repeat_files;
3946    pfile->dont_repeat_files = new_;
3947    new_->fname = savestring(ip->fname);
3948    new_->control_macro = 0;
3949    new_->got_name_map = 0;
3950    new_->c_system_include_path = 0;
3951
3952    return 0;
3953 }
3954
3955 /* #ident has already been copied to the output file, so just ignore it.  */
3956
3957 static int
3958 do_ident(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3959          unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
3960 {
3961 /*  long old_written = CPP_WRITTEN (pfile); */
3962
3963    /* Allow #ident in system headers, since that's not user's fault.  */
3964    if (CPP_PEDANTIC(pfile) && !CPP_BUFFER(pfile)->system_header_p)
3965       cpp_pedwarn(pfile, "ANSI C does not allow `#ident'");
3966
3967    /* Leave rest of line to be read by later calls to cpp_get_token. */
3968
3969    return 0;
3970 }
3971
3972 /* #pragma and its argument line have already been copied to the output file.
3973  * Just check for some recognized pragmas that need validation here.  */
3974
3975 static int
3976 do_pragma(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3977           unsigned char *buf, unsigned char *limit __UNUSED__)
3978 {
3979    while (*buf == ' ' || *buf == '\t')
3980       buf++;
3981
3982    if (!strncmp((const char *)buf, "once", 4))
3983      {
3984         /* Allow #pragma once in system headers, since that's not the user's
3985          * fault.  */
3986         if (!CPP_BUFFER(pfile)->system_header_p)
3987            cpp_warning(pfile, "`#pragma once' is obsolete");
3988         do_once(pfile);
3989      }
3990    if (!strncmp((const char *)buf, "implementation", 14))
3991      {
3992         /* Be quiet about `#pragma implementation' for a file only if it hasn't
3993          * been included yet.  */
3994         file_name_list     *ptr;
3995         char               *p = (char *)buf + 14, *fname, *inc_fname;
3996         int                 fname_len;
3997
3998         SKIP_WHITE_SPACE(p);
3999         if (*p == '\n' || *p != '\"')
4000            return 0;
4001
4002         fname = p + 1;
4003         p = strchr(fname, '\"');
4004         fname_len = (p) ? (int)(p - fname) : (int)strlen(fname);
4005
4006         for (ptr = pfile->all_include_files; ptr; ptr = ptr->next)
4007           {
4008              inc_fname = strrchr(ptr->fname, '/');
4009              inc_fname = inc_fname ? inc_fname + 1 : (char *)ptr->fname;
4010              if (inc_fname && !strncmp(inc_fname, fname, fname_len))
4011                 cpp_warning(pfile,
4012                             "`#pragma implementation' for `%s' appears after file is included",
4013                             fname);
4014           }
4015      }
4016    return 0;
4017 }
4018
4019 /* Just ignore #sccs, on systems where we define it at all.  */
4020
4021 /*
4022  * handle #if command by
4023  *   1) inserting special `defined' keyword into the hash table
4024  *      that gets turned into 0 or 1 by special_symbol (thus,
4025  *      if the luser has a symbol called `defined' already, it won't
4026  *      work inside the #if command)
4027  *   2) rescan the input into a temporary output buffer
4028  *   3) pass the output buffer to the yacc parser and collect a value
4029  *   4) clean up the mess left from steps 1 and 2.
4030  *   5) call conditional_skip to skip til the next #endif (etc.),
4031  *      or not, depending on the value from step 3.
4032  */
4033
4034 static int
4035 do_if(cpp_reader * pfile, struct directive *keyword __UNUSED__,
4036       unsigned char *buf, unsigned char *limit)
4037 {
4038    HOST_WIDE_INT       value = eval_if_expression(pfile, buf, limit - buf);
4039
4040    conditional_skip(pfile, value == 0, T_IF, NULL);
4041    return 0;
4042 }
4043
4044 /*
4045  * handle a #elif directive by not changing  if_stack  either.
4046  * see the comment above do_else.
4047  */
4048
4049 static int
4050 do_elif(cpp_reader * pfile, struct directive *keyword __UNUSED__,
4051         unsigned char *buf, unsigned char *limit)
4052 {
4053    if (pfile->if_stack == CPP_BUFFER(pfile)->if_stack)
4054      {
4055         cpp_error(pfile, "`#elif' not within a conditional");
4056         return 0;
4057      }
4058    else
4059      {
4060         if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
4061           {
4062              cpp_error(pfile, "`#elif' after `#else'");
4063              if (pfile->if_stack->fname
4064                  && CPP_BUFFER(pfile)->fname
4065                  && strcmp(pfile->if_stack->fname,
4066                            CPP_BUFFER(pfile)->nominal_fname) != 0)
4067                 fprintf(stderr, ", file %s", pfile->if_stack->fname);
4068              fprintf(stderr, ")\n");
4069           }
4070         pfile->if_stack->type = T_ELIF;
4071      }
4072
4073    if (pfile->if_stack->if_succeeded)
4074       skip_if_group(pfile, 0);
4075    else
4076      {
4077         HOST_WIDE_INT       value = eval_if_expression(pfile, buf, limit - buf);
4078
4079         if (value == 0)
4080            skip_if_group(pfile, 0);
4081         else
4082           {
4083              ++pfile->if_stack->if_succeeded;   /* continue processing input */
4084              output_line_command(pfile, 1, same_file);
4085           }
4086      }
4087    return 0;
4088 }
4089
4090 /*
4091  * evaluate a #if expression in BUF, of length LENGTH,
4092  * then parse the result as a C expression and return the value as an int.
4093  */
4094 static              HOST_WIDE_INT
4095 eval_if_expression(cpp_reader * pfile, unsigned char *buf __UNUSED__,
4096                    int length __UNUSED__)
4097 {
4098    HASHNODE           *save_defined;
4099    HOST_WIDE_INT       value;
4100    long                old_written = CPP_WRITTEN(pfile);
4101
4102    save_defined = install("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4103    pfile->pcp_inside_if = 1;
4104
4105    value = cpp_parse_expr(pfile);
4106    pfile->pcp_inside_if = 0;
4107    delete_macro(save_defined);  /* clean up special symbol */
4108
4109    CPP_SET_WRITTEN(pfile, old_written); /* Pop */
4110
4111    return value;
4112 }
4113
4114 /*
4115  * routine to handle ifdef/ifndef.  Try to look up the symbol,
4116  * then do or don't skip to the #endif/#else/#elif depending
4117  * on what directive is actually being processed.
4118  */
4119
4120 static int
4121 do_xifdef(cpp_reader * pfile, struct directive *keyword,
4122           unsigned char *unused1 __UNUSED__, unsigned char *unused2 __UNUSED__)
4123 {
4124    int                 skip;
4125    cpp_buffer         *ip = CPP_BUFFER(pfile);
4126    char               *ident;
4127    int                 ident_length;
4128    enum cpp_token      token;
4129    int                 start_of_file = 0;
4130    unsigned char      *control_macro = 0;
4131    int                 old_written = CPP_WRITTEN(pfile);
4132    int                 c;
4133
4134    /* Detect a #ifndef at start of file (not counting comments).  */
4135    if (ip->fname != 0 && keyword->type == T_IFNDEF)
4136       start_of_file = pfile->only_seen_white == 2;
4137
4138    pfile->no_macro_expand++;
4139    token = get_directive_token(pfile);
4140    pfile->no_macro_expand--;
4141
4142    ident = (char *)pfile->token_buffer + old_written;
4143    ident_length = CPP_WRITTEN(pfile) - old_written;
4144    CPP_SET_WRITTEN(pfile, old_written); /* Pop */
4145
4146    if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4147      {
4148         skip = (keyword->type == T_IFDEF);
4149         cpp_pedwarn(pfile, "`#%s' with no argument", keyword->name);
4150      }
4151    else if (token == CPP_NAME)
4152      {
4153         HASHNODE           *hp = cpp_lookup(ident, ident_length, -1);
4154
4155         skip = (!hp) ^ (keyword->type == T_IFNDEF);
4156         if (start_of_file && !skip)
4157           {
4158              control_macro = (unsigned char *)xmalloc(ident_length + 1);
4159              memcpy(control_macro, ident, ident_length + 1);
4160           }
4161      }
4162    else
4163      {
4164         skip = (keyword->type == T_IFDEF);
4165         cpp_error(pfile, "`#%s' with invalid argument", keyword->name);
4166      }
4167
4168    cpp_skip_hspace(pfile);
4169    c = PEEKC();
4170    if (c != EOF && c != '\n')
4171       cpp_pedwarn(pfile, "garbage at end of `#%s' argument",
4172                   keyword->name);
4173    skip_rest_of_line(pfile);
4174
4175    conditional_skip(pfile, skip, T_IF, control_macro);
4176    return 0;
4177 }
4178
4179 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4180  * If this is a #ifndef starting at the beginning of a file,
4181  * CONTROL_MACRO is the macro name tested by the #ifndef.
4182  * Otherwise, CONTROL_MACRO is 0.  */
4183
4184 static void
4185 conditional_skip(cpp_reader * pfile, int skip, enum node_type type,
4186                  unsigned char *control_macro)
4187 {
4188    IF_STACK_FRAME     *temp;
4189
4190    temp = (IF_STACK_FRAME *) xcalloc(1, sizeof(IF_STACK_FRAME));
4191    temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4192    temp->next = pfile->if_stack;
4193    temp->control_macro = control_macro;
4194    pfile->if_stack = temp;
4195
4196    pfile->if_stack->type = type;
4197
4198    if (skip != 0)
4199      {
4200         skip_if_group(pfile, 0);
4201         return;
4202      }
4203    else
4204      {
4205         ++pfile->if_stack->if_succeeded;
4206         output_line_command(pfile, 1, same_file);
4207      }
4208 }
4209
4210 /*
4211  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
4212  * leaves input ptr at the sharp sign found.
4213  * If ANY is nonzero, return at next directive of any sort.
4214  */
4215
4216 static void
4217 skip_if_group(cpp_reader * pfile, int any)
4218 {
4219    int                 c;
4220    struct directive   *kt;
4221    IF_STACK_FRAME     *save_if_stack = pfile->if_stack; /* don't pop past here */
4222
4223    int                 ident_length;
4224    char               *ident;
4225    struct parse_marker line_start_mark;
4226
4227    parse_set_mark(&line_start_mark, pfile);
4228
4229    if (CPP_OPTIONS(pfile)->output_conditionals)
4230      {
4231         static char         failed[] = "#failed\n";
4232
4233         CPP_PUTS(pfile, failed, sizeof(failed) - 1);
4234         pfile->lineno++;
4235         output_line_command(pfile, 1, same_file);
4236      }
4237  beg_of_line:
4238    if (CPP_OPTIONS(pfile)->output_conditionals)
4239      {
4240         cpp_buffer         *pbuf = CPP_BUFFER(pfile);
4241         unsigned char      *start_line = pbuf->buf + line_start_mark.position;
4242
4243         CPP_PUTS(pfile, start_line, pbuf->cur - start_line);
4244      }
4245    parse_move_mark(&line_start_mark, pfile);
4246    cpp_skip_hspace(pfile);
4247    c = GETC();
4248    if (c == '#')
4249      {
4250         int                 old_written = CPP_WRITTEN(pfile);
4251
4252         cpp_skip_hspace(pfile);
4253
4254         parse_name(pfile, GETC());
4255         ident_length = CPP_WRITTEN(pfile) - old_written;
4256         ident = (char *)pfile->token_buffer + old_written;
4257         pfile->limit = (unsigned char *)ident;
4258
4259         for (kt = directive_table; kt->length >= 0; kt++)
4260           {
4261              IF_STACK_FRAME     *temp;
4262
4263              if (ident_length == kt->length
4264                  && strncmp(ident, kt->name, kt->length) == 0)
4265                {
4266                   /* If we are asked to return on next directive, do so now.  */
4267                   if (any)
4268                      goto done;
4269
4270                   switch (kt->type)
4271                     {
4272                     case T_IF:
4273                     case T_IFDEF:
4274                     case T_IFNDEF:
4275                        temp
4276                           =
4277                           (IF_STACK_FRAME *) xcalloc(1, sizeof(IF_STACK_FRAME));
4278                        temp->next = pfile->if_stack;
4279                        pfile->if_stack = temp;
4280                        temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4281                        temp->type = kt->type;
4282                        break;
4283                     case T_ELSE:
4284                     case T_ENDIF:
4285                        if (CPP_PEDANTIC(pfile)
4286                            && pfile->if_stack != save_if_stack)
4287                           validate_else(pfile,
4288                                         kt->type ==
4289                                         T_ELSE ? "#else" : "#endif");
4290                     case T_ELIF:
4291                        if (pfile->if_stack == CPP_BUFFER(pfile)->if_stack)
4292                          {
4293                             cpp_error(pfile,
4294                                       "`#%s' not within a conditional",
4295                                       kt->name);
4296                             break;
4297                          }
4298                        else if (pfile->if_stack == save_if_stack)
4299                           goto done;    /* found what we came for */
4300
4301                        if (kt->type != T_ENDIF)
4302                          {
4303                             if (pfile->if_stack->type == T_ELSE)
4304                                cpp_error(pfile,
4305                                          "`#else' or `#elif' after `#else'");
4306                             pfile->if_stack->type = kt->type;
4307                             break;
4308                          }
4309                        temp = pfile->if_stack;
4310                        pfile->if_stack = temp->next;
4311                        free(temp);
4312                        break;
4313                     default:;
4314                     }
4315                   break;
4316                }
4317              /* Don't let erroneous code go by.  */
4318              if (kt->length < 0 && !CPP_OPTIONS(pfile)->lang_asm
4319                  && CPP_PEDANTIC(pfile))
4320                 cpp_pedwarn(pfile, "invalid preprocessor directive name");
4321           }
4322         c = GETC();
4323      }
4324    /* We're in the middle of a line.  Skip the rest of it. */
4325    for (;;)
4326      {
4327         switch (c)
4328           {
4329              long                old;
4330
4331           case EOF:
4332              goto done;
4333           case '/':             /* possible comment */
4334              c = skip_comment(pfile, NULL);
4335              if (c == EOF)
4336                 goto done;
4337              break;
4338           case '\"':
4339           case '\'':
4340              FORWARD(-1);
4341              old = CPP_WRITTEN(pfile);
4342              cpp_get_token(pfile);
4343              CPP_SET_WRITTEN(pfile, old);
4344              break;
4345           case '\\':
4346              /* Char after backslash loses its special meaning.  */
4347              if (PEEKC() == '\n')
4348                 FORWARD(1);
4349              break;
4350           case '\n':
4351              goto beg_of_line;
4352              break;
4353           }
4354         c = GETC();
4355      }
4356  done:
4357    if (CPP_OPTIONS(pfile)->output_conditionals)
4358      {
4359         static char         end_failed[] = "#endfailed\n";
4360
4361         CPP_PUTS(pfile, end_failed, sizeof(end_failed) - 1);
4362         pfile->lineno++;
4363      }
4364    pfile->only_seen_white = 1;
4365    parse_goto_mark(&line_start_mark, pfile);
4366    parse_clear_mark(&line_start_mark);
4367 }
4368
4369 /*
4370  * handle a #else directive.  Do this by just continuing processing
4371  * without changing  if_stack ;  this is so that the error message
4372  * for missing #endif's etc. will point to the original #if.  It
4373  * is possible that something different would be better.
4374  */
4375
4376 static int
4377 do_else(cpp_reader * pfile, struct directive *keyword __UNUSED__,
4378         unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
4379 {
4380    cpp_buffer         *ip = CPP_BUFFER(pfile);
4381
4382    if (CPP_PEDANTIC(pfile))
4383       validate_else(pfile, "#else");
4384    skip_rest_of_line(pfile);
4385
4386    if (pfile->if_stack == CPP_BUFFER(pfile)->if_stack)
4387      {
4388         cpp_error(pfile, "`#else' not within a conditional");
4389         return 0;
4390      }
4391    else
4392      {
4393         /* #ifndef can't have its special treatment for containing the whole file
4394          * if it has a #else clause.  */
4395         pfile->if_stack->control_macro = 0;
4396
4397         if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
4398           {
4399              cpp_error(pfile, "`#else' after `#else'");
4400              fprintf(stderr, " (matches line %d", pfile->if_stack->lineno);
4401              if (strcmp(pfile->if_stack->fname, ip->nominal_fname) != 0)
4402                 fprintf(stderr, ", file %s", pfile->if_stack->fname);
4403              fprintf(stderr, ")\n");
4404           }
4405         pfile->if_stack->type = T_ELSE;
4406      }
4407
4408    if (pfile->if_stack->if_succeeded)
4409       skip_if_group(pfile, 0);
4410    else
4411      {
4412         ++pfile->if_stack->if_succeeded;        /* continue processing input */
4413         output_line_command(pfile, 1, same_file);
4414      }
4415    return 0;
4416 }
4417
4418 /*
4419  * unstack after #endif command
4420  */
4421
4422 static int
4423 do_endif(cpp_reader * pfile, struct directive *keyword __UNUSED__,
4424          unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
4425 {
4426    if (CPP_PEDANTIC(pfile))
4427       validate_else(pfile, "#endif");
4428    skip_rest_of_line(pfile);
4429
4430    if (pfile->if_stack == CPP_BUFFER(pfile)->if_stack)
4431      {
4432         cpp_error(pfile, "unbalanced `#endif'");
4433      }
4434    else
4435      {
4436         IF_STACK_FRAME     *temp = pfile->if_stack;
4437
4438         pfile->if_stack = temp->next;
4439         if (temp->control_macro)
4440           {
4441              /* This #endif matched a #ifndef at the start of the file.
4442               * See if it is at the end of the file.  */
4443              struct parse_marker start_mark;
4444              int                 c;
4445
4446              parse_set_mark(&start_mark, pfile);
4447
4448              for (;;)
4449                {
4450                   cpp_skip_hspace(pfile);
4451                   c = GETC();
4452                   if (c != '\n')
4453                      break;
4454                }
4455              parse_goto_mark(&start_mark, pfile);
4456              parse_clear_mark(&start_mark);
4457
4458              if (c == EOF)
4459                {
4460                   /* If we get here, this #endif ends a #ifndef
4461                    * that contains all of the file (aside from whitespace).
4462                    * Arrange not to include the file again
4463                    * if the macro that was tested is defined.
4464                    * 
4465                    * Do not do this for the top-level file in a -include or any
4466                    * file in a -imacros.  */
4467                   {
4468                      file_name_list     *ifile = pfile->all_include_files;
4469
4470                      for (; ifile; ifile = ifile->next)
4471                        {
4472                           if (!strcmp(ifile->fname, CPP_BUFFER(pfile)->fname))
4473                             {
4474                                ifile->control_macro = temp->control_macro;
4475                                break;
4476                             }
4477                        }
4478                   }
4479                }
4480           }
4481         free(temp);
4482         output_line_command(pfile, 1, same_file);
4483      }
4484    return 0;
4485 }
4486
4487 /* When an #else or #endif is found while skipping failed conditional,
4488  * if -pedantic was specified, this is called to warn about text after
4489  * the command name.  P points to the first char after the command name.  */
4490
4491 static void
4492 validate_else(cpp_reader * pfile, const char *directive)
4493 {
4494    int                 c;
4495
4496    cpp_skip_hspace(pfile);
4497    c = PEEKC();
4498    if (c != EOF && c != '\n')
4499       cpp_pedwarn(pfile,
4500                   "text following `%s' violates ANSI standard", directive);
4501 }
4502
4503 /* Get the next token, and add it to the text in pfile->token_buffer.
4504  * Return the kind of token we got. */
4505
4506 enum cpp_token
4507 cpp_get_token(cpp_reader * pfile)
4508 {
4509    int                 c, c2, c3;
4510    long                old_written = 0;
4511    long                start_line = 0, start_column = 0;
4512    enum cpp_token      token;
4513    struct cpp_options *opts = CPP_OPTIONS(pfile);
4514
4515    CPP_BUFFER(pfile)->prev = CPP_BUFFER(pfile)->cur;
4516  get_next:
4517    c = GETC();
4518    if (c == EOF)
4519      {
4520       handle_eof:
4521         if (CPP_BUFFER(pfile)->seen_eof)
4522           {
4523              if (cpp_pop_buffer(pfile) != CPP_NULL_BUFFER(pfile))
4524                 goto get_next;
4525              else
4526                 return CPP_EOF;
4527           }
4528         else
4529           {
4530              cpp_buffer         *next_buf = CPP_PREV_BUFFER(CPP_BUFFER(pfile));
4531
4532              CPP_BUFFER(pfile)->seen_eof = 1;
4533              if (CPP_BUFFER(pfile)->nominal_fname && next_buf)
4534                {
4535                   /* We're about to return from an #include file.
4536                    * Emit #line information now (as part of the CPP_POP) result.
4537                    * But the #line refers to the file we will pop to. */
4538                   cpp_buffer         *cur_buffer = CPP_BUFFER(pfile);
4539
4540                   CPP_BUFFER(pfile) = next_buf;
4541                   pfile->input_stack_listing_current = 0;
4542                   output_line_command(pfile, 0, leave_file);
4543                   CPP_BUFFER(pfile) = cur_buffer;
4544                }
4545              return CPP_POP;
4546           }
4547      }
4548    else
4549      {
4550         switch (c)
4551           {
4552              long                newlines;
4553              struct parse_marker start_mark;
4554
4555           case '/':
4556              if (PEEKC() == '=')
4557                 goto op2;
4558              if (opts->put_out_comments)
4559                 parse_set_mark(&start_mark, pfile);
4560              newlines = 0;
4561              cpp_buf_line_and_col(cpp_file_buffer(pfile),
4562                                   &start_line, &start_column);
4563              c = skip_comment(pfile, &newlines);
4564              if (opts->put_out_comments && (c == '/' || c == EOF))
4565                 parse_clear_mark(&start_mark);
4566              if (c == '/')
4567                 goto randomchar;
4568              if (c == EOF)
4569                {
4570                   cpp_error_with_line(pfile, start_line, start_column,
4571                                       "unterminated comment");
4572                   goto handle_eof;
4573                }
4574              c = '/';           /* Initial letter of comment. */
4575            return_comment:
4576              /* Comments are equivalent to spaces.
4577               * For -traditional, a comment is equivalent to nothing.  */
4578              if (opts->put_out_comments)
4579                {
4580                   cpp_buffer         *pbuf = CPP_BUFFER(pfile);
4581                   unsigned char      *start = pbuf->buf + start_mark.position;
4582                   int                 len = pbuf->cur - start;
4583
4584                   CPP_RESERVE(pfile, 1 + len);
4585                   CPP_PUTC_Q(pfile, c);
4586                   CPP_PUTS_Q(pfile, start, len);
4587                   pfile->lineno += newlines;
4588                   parse_clear_mark(&start_mark);
4589                   return CPP_COMMENT;
4590                }
4591              else if (newlines > 0)
4592                {
4593                  output_line_command(pfile, 0, same_file);
4594                  CPP_RESERVE(pfile, 1);
4595                  CPP_PUTC_Q(pfile, ' ');
4596                  return CPP_VSPACE;
4597                }
4598              else
4599                {
4600                   CPP_RESERVE(pfile, 1);
4601                   CPP_PUTC_Q(pfile, ' ');
4602                   return CPP_HSPACE;
4603                }
4604
4605           case '#':
4606              if (!pfile->only_seen_white)
4607                 goto randomchar;
4608              if (handle_directive(pfile))
4609                 return CPP_DIRECTIVE;
4610              pfile->only_seen_white = 0;
4611              return CPP_OTHER;
4612
4613           case '\"':
4614           case '\'':
4615              /* A single quoted string is treated like a double -- some
4616               * programs (e.g., troff) are perverse this way */
4617              cpp_buf_line_and_col(cpp_file_buffer(pfile),
4618                                   &start_line, &start_column);
4619              old_written = CPP_WRITTEN(pfile);
4620            string:
4621              CPP_PUTC(pfile, c);
4622              while (1)
4623                {
4624                   int                 cc = GETC();
4625
4626                   if (cc == EOF)
4627                     {
4628                        if (CPP_IS_MACRO_BUFFER(CPP_BUFFER(pfile)))
4629                          {
4630                             /* try harder: this string crosses a macro expansion
4631                              * boundary.  This can happen naturally if -traditional.
4632                              * Otherwise, only -D can make a macro with an unmatched
4633                              * quote.  */
4634                             cpp_buffer         *next_buf
4635                                = CPP_PREV_BUFFER(CPP_BUFFER(pfile));
4636
4637                             (*CPP_BUFFER(pfile)->cleanup)
4638                                (CPP_BUFFER(pfile), pfile);
4639                             CPP_BUFFER(pfile) = next_buf;
4640                             continue;
4641                          }
4642                        cpp_error_with_line(pfile, start_line, start_column,
4643                                            "unterminated string or character constant");
4644                        if (pfile->multiline_string_line != start_line
4645                            && pfile->multiline_string_line != 0)
4646                           cpp_error_with_line(pfile,
4647                                               pfile->multiline_string_line,
4648                                               -1,
4649                                               "possible real start of unterminated constant");
4650                        pfile->multiline_string_line = 0;
4651                        break;
4652                     }
4653                   CPP_PUTC(pfile, cc);
4654                   switch (cc)
4655                     {
4656                     case '\n':
4657                        /* Traditionally, end of line ends a string constant with
4658                         * no error.  So exit the loop and record the new line.  */
4659                        if (c == '\'')
4660                          {
4661                             cpp_error_with_line(pfile, start_line, start_column,
4662                                                 "unterminated character constant");
4663                             goto while2end;
4664                          }
4665                        if (CPP_PEDANTIC(pfile)
4666                            && pfile->multiline_string_line == 0)
4667                          {
4668                             cpp_pedwarn_with_line(pfile, start_line,
4669                                                   start_column,
4670                                                   "string constant runs past end of line");
4671                          }
4672                        if (pfile->multiline_string_line == 0)
4673                           pfile->multiline_string_line = start_line;
4674                        break;
4675
4676                     case '\\':
4677                        cc = GETC();
4678                        if (cc == '\n')
4679                          {
4680                             /* Backslash newline is replaced by nothing at all. */
4681                             CPP_ADJUST_WRITTEN(pfile, -1);
4682                             pfile->lineno++;
4683                          }
4684                        else
4685                          {
4686                             /* ANSI stupidly requires that in \\ the second \
4687                              * is *not* prevented from combining with a newline.  */
4688                             NEWLINE_FIX1(cc);
4689                             if (cc != EOF)
4690                                CPP_PUTC(pfile, cc);
4691                          }
4692                        break;
4693
4694                     case '\"':
4695                     case '\'':
4696                        if (cc == c)
4697                           goto while2end;
4698                        break;
4699                     }
4700                }
4701            while2end:
4702              pfile->lineno += count_newlines(pfile->token_buffer + old_written,
4703                                              CPP_PWRITTEN(pfile));
4704              pfile->only_seen_white = 0;
4705              return c == '\'' ? CPP_CHAR : CPP_STRING;
4706
4707           case '$':
4708              if (!opts->dollars_in_ident)
4709                 goto randomchar;
4710              goto letter;
4711
4712           case ':':
4713              if (opts->cplusplus && PEEKC() == ':')
4714                 goto op2;
4715              goto randomchar;
4716
4717           case '&':
4718           case '+':
4719           case '|':
4720              NEWLINE_FIX;
4721              c2 = PEEKC();
4722              if (c2 == c || c2 == '=')
4723                 goto op2;
4724              goto randomchar;
4725
4726           case '*':
4727           case '!':
4728           case '%':
4729           case '=':
4730           case '^':
4731              NEWLINE_FIX;
4732              if (PEEKC() == '=')
4733                 goto op2;
4734              goto randomchar;
4735
4736           case '-':
4737              NEWLINE_FIX;
4738              c2 = PEEKC();
4739              if (c2 == '-' && opts->chill)
4740                {
4741                   /* Chill style comment */
4742                   if (opts->put_out_comments)
4743                      parse_set_mark(&start_mark, pfile);
4744                   FORWARD(1);   /* Skip second '-'. */
4745                   for (;;)
4746                     {
4747                        c = GETC();
4748                        if (c == EOF)
4749                           break;
4750                        if (c == '\n')
4751                          {
4752                             /* Don't consider final '\n' to be part of comment. */
4753                             FORWARD(-1);
4754                             break;
4755                          }
4756                     }
4757                   c = '-';
4758                   goto return_comment;
4759                }
4760              if (c2 == '-' || c2 == '=' || c2 == '>')
4761                 goto op2;
4762              goto randomchar;
4763
4764           case '<':
4765              if (pfile->parsing_include_directive)
4766                {
4767                   for (;;)
4768                     {
4769                        CPP_PUTC(pfile, c);
4770                        if (c == '>')
4771                           break;
4772                        c = GETC();
4773                        NEWLINE_FIX1(c);
4774                        if (c == '\n' || c == EOF)
4775                          {
4776                             cpp_error(pfile,
4777                                       "missing '>' in `#include <FILENAME>'");
4778                             break;
4779                          }
4780                     }
4781                   return CPP_STRING;
4782                }
4783              /* else fall through */
4784           case '>':
4785              NEWLINE_FIX;
4786              c2 = PEEKC();
4787              if (c2 == '=')
4788                 goto op2;
4789              if (c2 != c)
4790                 goto randomchar;
4791              FORWARD(1);
4792              CPP_RESERVE(pfile, 4);
4793              CPP_PUTC(pfile, c);
4794              CPP_PUTC(pfile, c2);
4795              NEWLINE_FIX;
4796              c3 = PEEKC();
4797              if (c3 == '=')
4798                 CPP_PUTC_Q(pfile, GETC());
4799              CPP_NUL_TERMINATE_Q(pfile);
4800              pfile->only_seen_white = 0;
4801              return CPP_OTHER;
4802
4803           case '@':
4804              if (CPP_BUFFER(pfile)->has_escapes)
4805                {
4806                   c = GETC();
4807                   // fix macro expansions starting with - losing the -
4808                   if (c == '-')
4809                     {
4810                        CPP_PUTS(pfile, "-", 1);
4811                        return CPP_OTHER;
4812                     }
4813                   // fix macro expansions starting with - losing the +
4814                   else if (c == '+')
4815                     {
4816                        CPP_PUTS(pfile, "+", 1);
4817                        return CPP_OTHER;
4818                     }
4819                   // fix macro expansions starting with - losing the .
4820                   else if (c == '.')
4821                     {
4822                        CPP_PUTS(pfile, ".", 1);
4823                        return CPP_OTHER;
4824                     }
4825                   else if (is_space[c])
4826                     {
4827                        CPP_RESERVE(pfile, 1);
4828                        if (pfile->output_escapes)
4829                           CPP_PUTC_Q(pfile, '@');
4830                        return CPP_HSPACE;
4831                     }
4832                }
4833              if (pfile->output_escapes)
4834                {
4835                   CPP_PUTS(pfile, "@@", 2);
4836                   return CPP_OTHER;
4837                }
4838              goto randomchar;
4839
4840           case '.':
4841              NEWLINE_FIX;
4842              c2 = PEEKC();
4843              if (isdigit(c2))
4844                {
4845                   CPP_RESERVE(pfile, 2);
4846                   CPP_PUTC_Q(pfile, '.');
4847                   c = GETC();
4848                   goto number;
4849                }
4850              /* FIXME - misses the case "..\\\n." */
4851              if (c2 == '.' && PEEKN(1) == '.')
4852                {
4853                   CPP_RESERVE(pfile, 4);
4854                   CPP_PUTC_Q(pfile, '.');
4855                   CPP_PUTC_Q(pfile, '.');
4856                   CPP_PUTC_Q(pfile, '.');
4857                   FORWARD(2);
4858                   CPP_NUL_TERMINATE_Q(pfile);
4859                   pfile->only_seen_white = 0;
4860                   return CPP_3DOTS;
4861                }
4862              goto randomchar;
4863
4864            op2:
4865              token = CPP_OTHER;
4866              pfile->only_seen_white = 0;
4867            op2any:
4868              CPP_RESERVE(pfile, 3);
4869              CPP_PUTC_Q(pfile, c);
4870              CPP_PUTC_Q(pfile, GETC());
4871              CPP_NUL_TERMINATE_Q(pfile);
4872              return token;
4873
4874           case 'L':
4875              NEWLINE_FIX;
4876              c2 = PEEKC();
4877              if ((c2 == '\'' || c2 == '\"'))
4878                {
4879                   CPP_PUTC(pfile, c);
4880                   c = GETC();
4881                   goto string;
4882                }
4883              goto letter;
4884
4885           case '0':
4886           case '1':
4887           case '2':
4888           case '3':
4889           case '4':
4890           case '5':
4891           case '6':
4892           case '7':
4893           case '8':
4894           case '9':
4895            number:
4896              c2 = '.';
4897              for (;;)
4898                {
4899                   CPP_RESERVE(pfile, 2);
4900                   CPP_PUTC_Q(pfile, c);
4901                   NEWLINE_FIX;
4902                   c = PEEKC();
4903                   if (c == EOF)
4904                      break;
4905                   if (!is_idchar[c] && c != '.'
4906                       && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
4907                      break;
4908                   FORWARD(1);
4909                   c2 = c;
4910                }
4911              CPP_NUL_TERMINATE_Q(pfile);
4912              pfile->only_seen_white = 0;
4913              return CPP_NUMBER;
4914           case 'b':
4915           case 'c':
4916           case 'd':
4917           case 'h':
4918           case 'o':
4919           case 'B':
4920           case 'C':
4921           case 'D':
4922           case 'H':
4923           case 'O':
4924              if (opts->chill && PEEKC() == '\'')
4925                {
4926                   pfile->only_seen_white = 0;
4927                   CPP_RESERVE(pfile, 2);
4928                   CPP_PUTC_Q(pfile, c);
4929                   CPP_PUTC_Q(pfile, '\'');
4930                   FORWARD(1);
4931                   for (;;)
4932                     {
4933                        c = GETC();
4934                        if (c == EOF)
4935                           goto chill_number_eof;
4936                        if (!is_idchar[c])
4937                          {
4938                             if (c == '\\' && PEEKC() == '\n')
4939                               {
4940                                  FORWARD(2);
4941                                  continue;
4942                               }
4943                             break;
4944                          }
4945                        CPP_PUTC(pfile, c);
4946                     }
4947                   if (c == '\'')
4948                     {
4949                        CPP_RESERVE(pfile, 2);
4950                        CPP_PUTC_Q(pfile, c);
4951                        CPP_NUL_TERMINATE_Q(pfile);
4952                        return CPP_STRING;
4953                     }
4954                   else
4955                     {
4956                        FORWARD(-1);
4957                      chill_number_eof:
4958                        CPP_NUL_TERMINATE(pfile);
4959                        return CPP_NUMBER;
4960                     }
4961                }
4962              else
4963                 goto letter;
4964           case '_':
4965           case 'a':
4966           case 'e':
4967           case 'f':
4968           case 'g':
4969           case 'i':
4970           case 'j':
4971           case 'k':
4972           case 'l':
4973           case 'm':
4974           case 'n':
4975           case 'p':
4976           case 'q':
4977           case 'r':
4978           case 's':
4979           case 't':
4980           case 'u':
4981           case 'v':
4982           case 'w':
4983           case 'x':
4984           case 'y':
4985           case 'z':
4986           case 'A':
4987           case 'E':
4988           case 'F':
4989           case 'G':
4990           case 'I':
4991           case 'J':
4992           case 'K':
4993           case 'M':
4994           case 'N':
4995           case 'P':
4996           case 'Q':
4997           case 'R':
4998           case 'S':
4999           case 'T':
5000           case 'U':
5001           case 'V':
5002           case 'W':
5003           case 'X':
5004           case 'Y':
5005           case 'Z':
5006            letter:
5007              {
5008                 HASHNODE           *hp;
5009                 char               *ident;
5010                 int                 before_name_written = CPP_WRITTEN(pfile);
5011                 int                 ident_len;
5012
5013                 parse_name(pfile, c);
5014                 pfile->only_seen_white = 0;
5015                 if (pfile->no_macro_expand)
5016                    return CPP_NAME;
5017                 ident = (char *)pfile->token_buffer + before_name_written;
5018                 ident_len = CPP_PWRITTEN(pfile) - (unsigned char *)ident;
5019                 hp = cpp_lookup(ident, ident_len, -1);
5020                 if (!hp)
5021                    return CPP_NAME;
5022                 if (hp->type == T_DISABLED)
5023                   {
5024                      if (pfile->output_escapes)
5025                        {        /* Return "@-IDENT", followed by '\0'. */
5026                           int                 i;
5027
5028                           CPP_RESERVE(pfile, 3);
5029                           ident =
5030                              (char *)pfile->token_buffer + before_name_written;
5031                           CPP_ADJUST_WRITTEN(pfile, 2);
5032                           for (i = ident_len; i >= 0; i--)
5033                              ident[i + 2] = ident[i];
5034                           ident[0] = '@';
5035                           ident[1] = '-';
5036                        }
5037                      return CPP_NAME;
5038                   }
5039                 /* If macro wants an arglist, verify that a '(' follows.
5040                  * first skip all whitespace, copying it to the output
5041                  * after the macro name.  Then, if there is no '(',
5042                  * decide this is not a macro call and leave things that way.  */
5043                 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5044                   {
5045                      struct parse_marker macro_mark;
5046                      int                 is_macro_call;
5047
5048                      while (CPP_IS_MACRO_BUFFER(CPP_BUFFER(pfile)))
5049                        {
5050                           cpp_buffer         *next_buf;
5051
5052                           cpp_skip_hspace(pfile);
5053                           if (PEEKC() != EOF)
5054                              break;
5055                           next_buf = CPP_PREV_BUFFER(CPP_BUFFER(pfile));
5056                           (*CPP_BUFFER(pfile)->cleanup) (CPP_BUFFER(pfile),
5057                                                          pfile);
5058                           CPP_BUFFER(pfile) = next_buf;
5059                        }
5060                      parse_set_mark(&macro_mark, pfile);
5061                      for (;;)
5062                        {
5063                           cpp_skip_hspace(pfile);
5064                           c = PEEKC();
5065                           is_macro_call = c == '(';
5066                           if (c != '\n')
5067                              break;
5068                           FORWARD(1);
5069                        }
5070                      if (!is_macro_call)
5071                         parse_goto_mark(&macro_mark, pfile);
5072                      parse_clear_mark(&macro_mark);
5073                      if (!is_macro_call)
5074                         return CPP_NAME;
5075                   }
5076                 /* This is now known to be a macro call. */
5077
5078                 /* it might not actually be a macro.  */
5079                 if (hp->type != T_MACRO)
5080                   {
5081                      int                 xbuf_len;
5082                      unsigned char      *xbuf;
5083
5084                      CPP_SET_WRITTEN(pfile, before_name_written);
5085                      special_symbol(hp, pfile);
5086                      xbuf_len = CPP_WRITTEN(pfile) - before_name_written;
5087                      xbuf = (unsigned char *)xmalloc(xbuf_len + 1);
5088                      CPP_SET_WRITTEN(pfile, before_name_written);
5089                      memcpy(xbuf, CPP_PWRITTEN(pfile), xbuf_len + 1);
5090                      push_macro_expansion(pfile, xbuf, xbuf_len, hp);
5091                   }
5092                 else
5093                   {
5094                      /* Expand the macro, reading arguments as needed,
5095                       * and push the expansion on the input stack.  */
5096                      macroexpand(pfile, hp);
5097                      CPP_SET_WRITTEN(pfile, before_name_written);
5098                   }
5099
5100                 /* An extra "@ " is added to the end of a macro expansion
5101                  * to prevent accidental token pasting.  We prefer to avoid
5102                  * unneeded extra spaces (for the sake of cpp-using tools like
5103                  * imake).  Here we remove the space if it is safe to do so. */
5104                 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5105                     && pfile->buffer->rlimit[-2] == '@'
5106                     && pfile->buffer->rlimit[-1] == ' ')
5107                   {
5108                      int                 c1 = pfile->buffer->rlimit[-3];
5109
5110                      c2 = CPP_BUF_PEEK(CPP_PREV_BUFFER(CPP_BUFFER(pfile)));
5111
5112                      if (c2 == EOF || !unsafe_chars(c1, c2))
5113                         pfile->buffer->rlimit -= 2;
5114                   }
5115              }
5116              goto get_next;
5117
5118           case ' ':
5119           case '\t':
5120           case '\v':
5121           case '\r':
5122              for (;;)
5123                {
5124                   CPP_PUTC(pfile, c);
5125                   c = PEEKC();
5126                   if (c == EOF || !is_hor_space[c])
5127                      break;
5128                   FORWARD(1);
5129                }
5130              return CPP_HSPACE;
5131
5132           case '\\':
5133              c2 = PEEKC();
5134              if (c2 != '\n')
5135                 goto randomchar;
5136              token = CPP_HSPACE;
5137              goto op2any;
5138
5139           case '\n':
5140              CPP_PUTC(pfile, c);
5141              if (pfile->only_seen_white == 0)
5142                 pfile->only_seen_white = 1;
5143              pfile->lineno++;
5144              output_line_command(pfile, 1, same_file);
5145              return CPP_VSPACE;
5146
5147           case '(':
5148              token = CPP_LPAREN;
5149              goto char1;
5150           case ')':
5151              token = CPP_RPAREN;
5152              goto char1;
5153           case '{':
5154              token = CPP_LBRACE;
5155              goto char1;
5156           case '}':
5157              token = CPP_RBRACE;
5158              goto char1;
5159           case ',':
5160              token = CPP_COMMA;
5161              goto char1;
5162           case ';':
5163              token = CPP_SEMICOLON;
5164              goto char1;
5165
5166            randomchar:
5167           default:
5168              token = CPP_OTHER;
5169            char1:
5170              pfile->only_seen_white = 0;
5171              CPP_PUTC(pfile, c);
5172              return token;
5173           }
5174      }
5175 }
5176
5177 #if 0                           /* Unused */
5178 /* Like cpp_get_token, but skip spaces and comments. */
5179 enum cpp_token
5180 cpp_get_non_space_token(cpp_reader * pfile)
5181 {
5182    int                 old_written = CPP_WRITTEN(pfile);
5183
5184    for (;;)
5185      {
5186         enum cpp_token      token = cpp_get_token(pfile);
5187
5188         if (token != CPP_COMMENT && token != CPP_POP
5189             && token != CPP_HSPACE && token != CPP_VSPACE)
5190            return token;
5191         CPP_SET_WRITTEN(pfile, old_written);
5192      }
5193 }
5194 #endif
5195
5196 /* Parse an identifier starting with C. */
5197
5198 int
5199 parse_name(cpp_reader * pfile, int c)
5200 {
5201    for (;;)
5202      {
5203         if (!is_idchar[c])
5204           {
5205              if (c == '\\' && PEEKC() == '\n')
5206                {
5207                   FORWARD(2);
5208                   continue;
5209                }
5210              FORWARD(-1);
5211              break;
5212           }
5213         CPP_RESERVE(pfile, 2);  /* One more for final NUL. */
5214         CPP_PUTC_Q(pfile, c);
5215         c = GETC();
5216         if (c == EOF)
5217            break;
5218      }
5219    CPP_NUL_TERMINATE_Q(pfile);
5220    return 1;
5221 }
5222
5223 /* Maintain and search list of included files, for #import.  */
5224
5225 /* Hash a file name for import_hash_table.  */
5226
5227 static int
5228 import_hash(char *f)
5229 {
5230    int                 val = 0;
5231
5232    while (*f)
5233       val += *f++;
5234    return (val % IMPORT_HASH_SIZE);
5235 }
5236
5237 /* Search for file FILENAME in import_hash_table.
5238  * Return -2 if found, either a matching name or a matching inode.
5239  * Otherwise, open the file and return a file descriptor if successful
5240  * or -1 if unsuccessful.  */
5241
5242 static int
5243 lookup_import(cpp_reader * pfile, char *filename, file_name_list * searchptr)
5244 {
5245    struct import_file *i;
5246    int                 h;
5247    int                 hashval;
5248    struct stat         sb;
5249    int                 fd;
5250
5251    hashval = import_hash(filename);
5252
5253    /* Attempt to find file in list of already included files */
5254    i = pfile->import_hash_table[hashval];
5255
5256    while (i)
5257      {
5258         if (!strcmp(filename, i->name))
5259            return -2;           /* return found */
5260         i = i->next;
5261      }
5262    /* Open it and try a match on inode/dev */
5263    fd = open_include_file(pfile, filename, searchptr);
5264    if (fd < 0)
5265       return fd;
5266    fstat(fd, &sb);
5267    for (h = 0; h < IMPORT_HASH_SIZE; h++)
5268      {
5269         i = pfile->import_hash_table[h];
5270         while (i)
5271           {
5272              /* Compare the inode and the device.
5273               * Supposedly on some systems the inode is not a scalar.  */
5274              if (!memcmp
5275                  ((char *)&i->inode, (char *)&sb.st_ino, sizeof(sb.st_ino))
5276                  && i->dev == sb.st_dev)
5277                {
5278                   close(fd);
5279                   return -2;    /* return found */
5280                }
5281              i = i->next;
5282           }
5283      }
5284    return fd;                   /* Not found, return open file */
5285 }
5286
5287 /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
5288
5289 static void
5290 add_import(cpp_reader * pfile, int fd, char *fname)
5291 {
5292    struct import_file *i;
5293    int                 hashval;
5294    struct stat         sb;
5295
5296    hashval = import_hash(fname);
5297    fstat(fd, &sb);
5298    i = (struct import_file *)xmalloc(sizeof(struct import_file));
5299
5300    i->name = (char *)xmalloc(strlen(fname) + 1);
5301    strcpy(i->name, fname);
5302    memcpy((char *)&i->inode, (char *)&sb.st_ino, sizeof(sb.st_ino));
5303    i->dev = sb.st_dev;
5304    i->next = pfile->import_hash_table[hashval];
5305    pfile->import_hash_table[hashval] = i;
5306 }
5307
5308 /* The file_name_map structure holds a mapping of file names for a
5309  * particular directory.  This mapping is read from the file named
5310  * FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
5311  * map filenames on a file system with severe filename restrictions,
5312  * such as DOS.  The format of the file name map file is just a series
5313  * of lines with two tokens on each line.  The first token is the name
5314  * to map, and the second token is the actual name to use.  */
5315
5316 struct file_name_map {
5317    struct file_name_map *map_next;
5318    char               *map_from;
5319    char               *map_to;
5320 };
5321
5322 #if USE_FILE_NAME_MAPS
5323
5324 #define FILE_NAME_MAP_FILE "header.gcc"
5325
5326 /* Read a space delimited string of unlimited length from a stdio
5327  * file.  */
5328
5329 static char        *
5330 read_filename_string(int ch, FILE * f)
5331 {
5332    char               *alloc, *set;
5333    int                 len;
5334
5335    len = 20;
5336    set = alloc = (char *)xmalloc(len + 1);
5337    if (!is_space[ch])
5338      {
5339         *set++ = ch;
5340         while ((ch = getc(f)) != EOF && !is_space[ch])
5341           {
5342              if (set - alloc == len)
5343                {
5344                   len *= 2;
5345                   alloc = (char *)xrealloc(alloc, len + 1);
5346                   set = alloc + len / 2;
5347                }
5348              *set++ = ch;
5349           }
5350      }
5351    *set = '\0';
5352    ungetc(ch, f);
5353    return alloc;
5354 }
5355
5356 /* This structure holds a linked list of file name maps, one per directory. */
5357 struct file_name_map_list {
5358    struct file_name_map_list *map_list_next;
5359    char               *map_list_name;
5360    struct file_name_map *map_list_map;
5361 };
5362
5363 /* Read the file name map file for DIRNAME.  */
5364
5365 static struct file_name_map *
5366 read_name_map(cpp_reader * pfile, const char *dirname)
5367 {
5368    struct file_name_map_list *map_list_ptr;
5369    char               *name;
5370    FILE               *f;
5371
5372    for (map_list_ptr = CPP_OPTIONS(pfile)->map_list; map_list_ptr;
5373         map_list_ptr = map_list_ptr->map_list_next)
5374       if (!strcmp(map_list_ptr->map_list_name, dirname))
5375          return map_list_ptr->map_list_map;
5376
5377    map_list_ptr =
5378       ((struct file_name_map_list *)xmalloc(sizeof(struct file_name_map_list)));
5379
5380    map_list_ptr->map_list_name = savestring(dirname);
5381    map_list_ptr->map_list_map = NULL;
5382
5383    name = (char *)alloca(strlen(dirname) + strlen(FILE_NAME_MAP_FILE) + 2);
5384    strcpy(name, dirname);
5385    if (*dirname)
5386       strcat(name, "/");
5387    strcat(name, FILE_NAME_MAP_FILE);
5388 #ifndef __EMX__
5389    f = fopen(name, "rb");
5390 #else
5391    f = fopen(name, "rtb");
5392 #endif
5393    if (!f)
5394       map_list_ptr->map_list_map = NULL;
5395    else
5396      {
5397         int                 ch;
5398         int                 dirlen = strlen(dirname);
5399
5400         while ((ch = getc(f)) != EOF)
5401           {
5402              char               *from, *to;
5403              struct file_name_map *ptr;
5404
5405              if (is_space[ch])
5406                 continue;
5407              from = read_filename_string(ch, f);
5408              while ((ch = getc(f)) != EOF && is_hor_space[ch]);
5409              to = read_filename_string(ch, f);
5410
5411              ptr =
5412                 ((struct file_name_map *)xmalloc(sizeof(struct file_name_map)));
5413
5414              ptr->map_from = from;
5415
5416              /* Make the real filename absolute.  */
5417              if (*to == '/')
5418                 ptr->map_to = to;
5419              else
5420                {
5421                   ptr->map_to = (char *)xmalloc(dirlen + strlen(to) + 2);
5422                   strcpy(ptr->map_to, dirname);
5423                   ptr->map_to[dirlen] = '/';
5424                   strcpy(ptr->map_to + dirlen + 1, to);
5425                   free(to);
5426                }
5427
5428              ptr->map_next = map_list_ptr->map_list_map;
5429              map_list_ptr->map_list_map = ptr;
5430
5431              while ((ch = getc(f)) != '\n')
5432                 if (ch == EOF)
5433                    break;
5434           }
5435         fclose(f);
5436      }
5437
5438    map_list_ptr->map_list_next = CPP_OPTIONS(pfile)->map_list;
5439    CPP_OPTIONS(pfile)->map_list = map_list_ptr;
5440
5441    return map_list_ptr->map_list_map;
5442 }
5443
5444 /* Try to open include file FILENAME.  SEARCHPTR is the directory
5445  * being tried from the include file search path.  This function maps
5446  * filenames on file systems based on information read by
5447  * read_name_map.  */
5448
5449 static int
5450 open_include_file(cpp_reader * pfile, char *filename,
5451                   file_name_list * searchptr)
5452 {
5453    struct file_name_map *map;
5454    const char         *from;
5455    const char         *p, *dir;
5456
5457    if (searchptr && !searchptr->got_name_map)
5458      {
5459         searchptr->name_map = read_name_map(pfile,
5460                                             searchptr->fname
5461                                             ? searchptr->fname : ".");
5462         searchptr->got_name_map = 1;
5463      }
5464    /* First check the mapping for the directory we are using.  */
5465    if (searchptr && searchptr->name_map)
5466      {
5467         from = filename;
5468         if (searchptr->fname)
5469            from += strlen(searchptr->fname) + 1;
5470         for (map = searchptr->name_map; map; map = map->map_next)
5471           {
5472              if (!strcmp(map->map_from, from))
5473                {
5474                   /* Found a match.  */
5475                   return open(map->map_to, O_RDONLY | O_BINARY, 0666);
5476                }
5477           }
5478      }
5479    /* Try to find a mapping file for the particular directory we are
5480     * looking in.  Thus #include <sys/types.h> will look up sys/types.h
5481     * in /usr/include/header.gcc and look up types.h in
5482     * /usr/include/sys/header.gcc.  */
5483    p = strrchr(filename, '/');
5484    if (!p)
5485       p = filename;
5486    if (searchptr
5487        && searchptr->fname
5488        && strlen(searchptr->fname) == (unsigned)(p - filename)
5489        && !strncmp(searchptr->fname, filename, p - filename))
5490      {
5491         /* FILENAME is in SEARCHPTR, which we've already checked.  */
5492         using_file(filename);
5493         return open(filename, O_RDONLY | O_BINARY, 0666);
5494      }
5495    if (p == filename)
5496      {
5497         dir = ".";
5498         from = filename;
5499      }
5500    else
5501      {
5502         char               *s;
5503
5504         s = (char *)alloca(p - filename + 1);
5505         memcpy(s, filename, p - filename);
5506         s[p - filename] = '\0';
5507         from = p + 1;
5508         dir = s;
5509      }
5510    for (map = read_name_map(pfile, dir); map; map = map->map_next)
5511       if (!strcmp(map->map_from, from))
5512         {
5513            using_file(map->map_to);
5514            return open(map->map_to, O_RDONLY | O_BINARY, 0666);
5515         }
5516
5517    using_file(filename);
5518    return open(filename, O_RDONLY | O_BINARY, 0666);
5519 }
5520
5521 #else
5522
5523 static int
5524 open_include_file(cpp_reader * pfile __UNUSED__, char *filename,
5525                   file_name_list * searchptr __UNUSED__)
5526 {
5527    using_file(filename);
5528    return open(filename, O_RDONLY | O_BINARY, 0666);
5529 }
5530
5531 #endif /* USE_FILE_NAME_MAPS */
5532
5533 static int
5534 dos2unix(cpp_buffer *fp, int length)
5535 {
5536    unsigned char *tbuf;
5537    int nlen = 0, i;
5538    
5539    tbuf = xmalloc(length + 4);
5540    if (!tbuf) return length;
5541    for (i = 0; i < length; i++)
5542      {
5543         if ((fp->buf[i] == '\r') &&
5544             (fp->buf[i + 1] == '\n'))
5545           {
5546              // skip \r in \r\n
5547              continue;
5548           }
5549         tbuf[nlen] = fp->buf[i];
5550         nlen++;
5551      }
5552    tbuf[nlen] = 0;
5553    
5554    free(fp->buf);
5555    fp->buf = tbuf;
5556    return nlen;
5557 }
5558
5559 /* Process the contents of include file FNAME, already open on descriptor F,
5560  * with output to OP.
5561  * SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5562  * "system" include directories (as decided by the `is_system_include'
5563  * function above).
5564  * DIRPTR is the link in the dir path through which this file was found,
5565  * or 0 if the file name was absolute or via the current directory.
5566  * Return 1 on success, 0 on failure.
5567  * 
5568  * The caller is responsible for the cpp_push_buffer.  */
5569
5570 static int
5571 finclude(cpp_reader * pfile, int f, const char *fname, int system_header_p,
5572          file_name_list * dirptr)
5573 {
5574    int                 st_mode;
5575    long                st_size;
5576    long                i;
5577    int                 length;
5578    cpp_buffer         *fp;      /* For input stack frame */
5579
5580    if (file_size_and_mode(f, &st_mode, &st_size) < 0)
5581      {
5582         cpp_perror_with_name(pfile, fname);
5583         close(f);
5584         cpp_pop_buffer(pfile);
5585         return 0;
5586      }
5587    fp = CPP_BUFFER(pfile);
5588    fp->nominal_fname = fp->fname = fname;
5589    fp->dir = dirptr;
5590    fp->system_header_p = system_header_p;
5591    fp->lineno = 1;
5592    fp->colno = 1;
5593    fp->cleanup = file_cleanup;
5594
5595    if (S_ISREG(st_mode))
5596      {
5597         fp->buf = (unsigned char *)xmalloc(st_size + 2);
5598         /* Read the file contents, knowing that st_size is an upper bound
5599          * on the number of bytes we can read.  */
5600         length = safe_read(f, (char *)fp->buf, st_size);
5601         length = dos2unix(fp, length);
5602         
5603         fp->alimit = fp->buf + st_size + 2;
5604         fp->cur = fp->buf;
5605         fp->rlimit = fp->buf + length;
5606         if (length < 0)
5607            goto nope;
5608      }
5609    else if (S_ISDIR(st_mode))
5610      {
5611         cpp_error(pfile, "directory `%s' specified in #include", fname);
5612         close(f);
5613         return 0;
5614      }
5615    else
5616      {
5617         /* Cannot count its file size before reading.
5618          * First read the entire file into heap and
5619          * copy them into buffer on stack. */
5620
5621         int                 bsize = 2000;
5622
5623         st_size = 0;
5624         fp->buf = (unsigned char *)xmalloc(bsize + 2);
5625
5626         for (;;)
5627           {
5628              i = safe_read(f, (char *)(fp->buf + st_size), bsize - st_size);
5629              if (i < 0)
5630                 goto nope;      /* error! */
5631              st_size += i;
5632              if (st_size != bsize)
5633                 break;          /* End of file */
5634              bsize *= 2;
5635              fp->buf = (unsigned char *)xrealloc(fp->buf, bsize + 2);
5636           }
5637         length = st_size;
5638         length = dos2unix(fp, length);
5639    }
5640
5641    if ((length > 0 && fp->buf[length - 1] != '\n')
5642        /* Backslash-newline at end is not good enough.  */
5643        || (length > 1 && fp->buf[length - 2] == '\\'))
5644      {
5645         fp->buf[length++] = '\n';
5646      }
5647    fp->buf[length] = '\0';
5648      
5649    fp->rlimit = fp->buf + length;
5650
5651    /* Close descriptor now, so nesting does not use lots of descriptors.  */
5652    close(f);
5653
5654    /* Must do this before calling trigraph_pcp, so that the correct file name
5655     * will be printed in warning messages.  */
5656
5657    pfile->input_stack_listing_current = 0;
5658
5659    return 1;
5660
5661  nope:
5662
5663    cpp_perror_with_name(pfile, fname);
5664    close(f);
5665    free(fp->buf);
5666    return 1;
5667 }
5668
5669 int
5670 push_parse_file(cpp_reader * pfile, const char *fname)
5671 {
5672    struct cpp_options *opts = CPP_OPTIONS(pfile);
5673    struct cpp_pending *pend;
5674    char               *p;
5675    int                 f;
5676    cpp_buffer         *fp;
5677
5678    /* The code looks at the defaults through this pointer, rather than through
5679     * the constant structure above.  This pointer gets changed if an environment
5680     * variable specifies other defaults.  */
5681    struct default_include *include_defaults = include_defaults_array;
5682
5683    /* Add dirs from CPATH after dirs from -I.  */
5684    /* There seems to be confusion about what CPATH should do,
5685     * so for the moment it is not documented.  */
5686    /* Some people say that CPATH should replace the standard include dirs,
5687     * but that seems pointless: it comes before them, so it overrides them
5688     * anyway.  */
5689    p = (char *)getenv("CPATH");
5690    if (p && !opts->no_standard_includes)
5691       path_include(pfile, p);
5692
5693    /* Now that dollars_in_ident is known, initialize is_idchar.  */
5694    initialize_char_syntax(opts);
5695
5696    /* Do partial setup of input buffer for the sake of generating
5697     * early #line directives (when -g is in effect).  */
5698    fp = cpp_push_buffer(pfile, NULL, 0);
5699    if (!opts->in_fname)
5700       opts->in_fname = "";
5701    fp->nominal_fname = fp->fname = opts->in_fname;
5702    fp->lineno = 0;
5703
5704    /* Install __LINE__, etc.  Must follow initialize_char_syntax
5705     * and option processing.  */
5706    initialize_builtins(pfile);
5707
5708    /* Do standard #defines and assertions
5709     * that identify system and machine type.  */
5710
5711    if (!opts->inhibit_predefs)
5712      {
5713         p = (char *)alloca(strlen(predefs) + 1);
5714
5715         strcpy(p, predefs);
5716         while (*p)
5717           {
5718              char               *q;
5719
5720              while (*p == ' ' || *p == '\t')
5721                 p++;
5722              /* Handle -D options.  */
5723              if (p[0] == '-' && p[1] == 'D')
5724                {
5725                   q = &p[2];
5726                   while (*p && *p != ' ' && *p != '\t')
5727                      p++;
5728                   if (*p != 0)
5729                      *p++ = 0;
5730                   if (opts->debug_output)
5731                      output_line_command(pfile, 0, same_file);
5732                   cpp_define(pfile, (unsigned char *)q);
5733                   while (*p == ' ' || *p == '\t')
5734                      p++;
5735                }
5736              else if (p[0] == '-' && p[1] == 'A')
5737                {
5738                   /* Handle -A options (assertions).  */
5739                   char               *assertion;
5740                   char               *past_name;
5741                   char               *value;
5742                   char               *past_value;
5743                   char               *termination;
5744                   int                 save_char;
5745
5746                   assertion = &p[2];
5747                   past_name = assertion;
5748                   /* Locate end of name.  */
5749                   while (*past_name && *past_name != ' '
5750                          && *past_name != '\t' && *past_name != '(')
5751                      past_name++;
5752                   /* Locate `(' at start of value.  */
5753                   value = past_name;
5754                   while (*value && (*value == ' ' || *value == '\t'))
5755                      value++;
5756                   if (*value++ != '(')
5757                      abort();
5758                   while (*value && (*value == ' ' || *value == '\t'))
5759                      value++;
5760                   past_value = value;
5761                   /* Locate end of value.  */
5762                   while (*past_value && *past_value != ' '
5763                          && *past_value != '\t' && *past_value != ')')
5764                      past_value++;
5765                   termination = past_value;
5766                   while (*termination
5767                          && (*termination == ' ' || *termination == '\t'))
5768                      termination++;
5769                   if (*termination++ != ')')
5770                      abort();
5771                   if (*termination && *termination != ' '
5772                       && *termination != '\t')
5773                      abort();
5774                   /* Temporarily null-terminate the value.  */
5775                   save_char = *termination;
5776                   *termination = '\0';
5777                   /* Install the assertion.  */
5778                   make_assertion(pfile, "-A", assertion);
5779                   *termination = (char)save_char;
5780                   p = termination;
5781                   while (*p == ' ' || *p == '\t')
5782                      p++;
5783                }
5784              else
5785                {
5786                   abort();
5787                }
5788           }
5789      }
5790    /* Now handle the command line options.  */
5791
5792    /* Do -U's, -D's and -A's in the order they were seen.  */
5793    /* First reverse the list. */
5794    opts->pending = nreverse_pending(opts->pending);
5795
5796    for (pend = opts->pending; pend; pend = pend->next)
5797      {
5798         if (pend->cmd && pend->cmd[0] == '-')
5799           {
5800              switch (pend->cmd[1])
5801                {
5802                case 'U':
5803                   if (opts->debug_output)
5804                      output_line_command(pfile, 0, same_file);
5805                   do_undef(pfile, NULL, (unsigned char *)pend->arg,
5806                            (unsigned char *)pend->arg + strlen(pend->arg));
5807                   break;
5808                case 'D':
5809                   if (opts->debug_output)
5810                      output_line_command(pfile, 0, same_file);
5811                   cpp_define(pfile, (unsigned char *)pend->arg);
5812                   break;
5813                case 'A':
5814                   make_assertion(pfile, "-A", pend->arg);
5815                   break;
5816                }
5817           }
5818      }
5819
5820    opts->done_initializing = 1;
5821
5822    {                            /* read the appropriate environment variable and if it exists
5823                                  * replace include_defaults with the listed path. */
5824       char               *epath = 0;
5825
5826       switch ((opts->objc << 1) + opts->cplusplus)
5827         {
5828         case 0:
5829            epath = getenv("C_INCLUDE_PATH");
5830            break;
5831         case 1:
5832            epath = getenv("CPLUS_INCLUDE_PATH");
5833            break;
5834         case 2:
5835            epath = getenv("OBJC_INCLUDE_PATH");
5836            break;
5837         case 3:
5838            epath = getenv("OBJCPLUS_INCLUDE_PATH");
5839            break;
5840         }
5841       /* If the environment var for this language is set,
5842        * add to the default list of include directories.  */
5843       if (epath)
5844         {
5845            char               *nstore = (char *)alloca(strlen(epath) + 2);
5846            int                 num_dirs;
5847            char               *startp, *endp;
5848
5849            for (num_dirs = 1, startp = epath; *startp; startp++)
5850               if (*startp == PATH_SEPARATOR)
5851                  num_dirs++;
5852            include_defaults
5853               = (struct default_include *)xmalloc((num_dirs
5854                                                    *
5855                                                    sizeof(struct
5856                                                           default_include)) +
5857                                                   sizeof
5858                                                   (include_defaults_array));
5859
5860            startp = endp = epath;
5861            num_dirs = 0;
5862            while (1)
5863              {
5864                 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5865                 if ((*endp == PATH_SEPARATOR) || *endp == 0)
5866                   {
5867                      strncpy(nstore, startp, endp - startp);
5868                      if (endp == startp)
5869                         strcpy(nstore, ".");
5870                      else
5871                         nstore[endp - startp] = '\0';
5872
5873                      include_defaults[num_dirs].fname = savestring(nstore);
5874                      include_defaults[num_dirs].cplusplus = opts->cplusplus;
5875                      include_defaults[num_dirs].cxx_aware = 1;
5876                      num_dirs++;
5877                      if (*endp == '\0')
5878                         break;
5879                      endp = startp = endp + 1;
5880                   }
5881                 else
5882                    endp++;
5883              }
5884            /* Put the usual defaults back in at the end.  */
5885            memcpy((char *)&include_defaults[num_dirs],
5886                   (char *)include_defaults_array,
5887                   sizeof(include_defaults_array));
5888         }
5889    }
5890
5891    append_include_chain(pfile, opts->before_system, opts->last_before_system);
5892    opts->first_system_include = opts->before_system;
5893
5894    /* Unless -fnostdinc,
5895     * tack on the standard include file dirs to the specified list */
5896    if (!opts->no_standard_includes)
5897      {
5898         struct default_include *di = include_defaults;
5899         char               *specd_prefix = opts->include_prefix;
5900         char               *default_prefix = savestring(GCC_INCLUDE_DIR);
5901         int                 default_len = 0;
5902
5903         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
5904         if (!strcmp(default_prefix + strlen(default_prefix) - 8, "/include"))
5905           {
5906              default_len = strlen(default_prefix) - 7;
5907              default_prefix[default_len] = 0;
5908           }
5909         /* Search "translated" versions of GNU directories.
5910          * These have /usr/local/lib/gcc... replaced by specd_prefix.  */
5911         if (specd_prefix && default_len != 0)
5912            for (di = include_defaults; di->fname; di++)
5913              {
5914                 /* Some standard dirs are only for C++.  */
5915                 if (!di->cplusplus
5916                     || (opts->cplusplus
5917                         && !opts->no_standard_cplusplus_includes))
5918                   {
5919                      /* Does this dir start with the prefix?  */
5920                      if (!strncmp(di->fname, default_prefix, default_len))
5921                        {
5922                           /* Yes; change prefix and add to search list.  */
5923                           file_name_list     *new_
5924                              =
5925                              (file_name_list *) xmalloc(sizeof(file_name_list));
5926                           int                 this_len =
5927                              strlen(specd_prefix) + strlen(di->fname) -
5928                              default_len;
5929                           char               *str =
5930                              (char *)xmalloc(this_len + 1);
5931
5932                           strcpy(str, specd_prefix);
5933                           strcat(str, di->fname + default_len);
5934                           new_->fname = str;
5935                           new_->control_macro = 0;
5936                           new_->c_system_include_path = !di->cxx_aware;
5937                           new_->got_name_map = 0;
5938                           append_include_chain(pfile, new_, new_);
5939                           if (!opts->first_system_include)
5940                              opts->first_system_include = new_;
5941                        }
5942                   }
5943              }
5944         /* Search ordinary names for GNU include directories.  */
5945         for (di = include_defaults; di->fname; di++)
5946           {
5947              /* Some standard dirs are only for C++.  */
5948              if (!di->cplusplus
5949                  || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
5950                {
5951                   file_name_list     *new_
5952                      = (file_name_list *) xmalloc(sizeof(file_name_list));
5953
5954                   new_->control_macro = 0;
5955                   new_->c_system_include_path = !di->cxx_aware;
5956                   new_->fname = (char *)di->fname;
5957                   new_->got_name_map = 0;
5958                   append_include_chain(pfile, new_, new_);
5959                   if (!opts->first_system_include)
5960                      opts->first_system_include = new_;
5961                }
5962           }
5963      }
5964    /* Tack the after_include chain at the end of the include chain.  */
5965    append_include_chain(pfile, opts->after_include, opts->last_after_include);
5966    if (!opts->first_system_include)
5967       opts->first_system_include = opts->after_include;
5968
5969    /* With -v, print the list of dirs to search.  */
5970    if (opts->verbose)
5971      {
5972         file_name_list     *fl;
5973
5974         fprintf(stderr, "#include \"...\" search starts here:\n");
5975         for (fl = opts->include; fl; fl = fl->next)
5976           {
5977              if (fl == opts->first_bracket_include)
5978                 fprintf(stderr, "#include <...> search starts here:\n");
5979              fprintf(stderr, " %s\n", fl->fname);
5980           }
5981         fprintf(stderr, "End of search list.\n");
5982      }
5983    /* Scan the -imacros files before the main input.
5984     * Much like #including them, but with no_output set
5985     * so that only their macro definitions matter.  */
5986
5987    opts->no_output++;
5988    pfile->no_record_file++;
5989    for (pend = opts->pending; pend; pend = pend->next)
5990      {
5991         if (pend->cmd && strcmp(pend->cmd, "-imacros") == 0)
5992           {
5993              int                 fd = open(pend->arg, O_RDONLY | O_BINARY, 0666);
5994
5995              if (fd < 0)
5996                {
5997                   cpp_perror_with_name(pfile, pend->arg);
5998                   return FATAL_EXIT_CODE;
5999                }
6000              cpp_push_buffer(pfile, NULL, 0);
6001              finclude(pfile, fd, pend->arg, 0, NULL);
6002              cpp_scan_buffer(pfile);
6003           }
6004      }
6005    opts->no_output--;
6006    pfile->no_record_file--;
6007
6008    /* Copy the entire contents of the main input file into
6009     * the stacked input buffer previously allocated for it.  */
6010    if (!fname || *fname == 0)
6011      {
6012         fname = "";
6013         f = 0;
6014      }
6015    else if ((f = open(fname, O_RDONLY | O_BINARY, 0666)) < 0)
6016       cpp_pfatal_with_name(pfile, fname);
6017
6018    /* -MG doesn't select the form of output and must be specified with one of
6019     * -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
6020     * inhibit compilation.  */
6021    if (opts->print_deps_missing_files
6022        && (opts->print_deps == 0 || !opts->no_output))
6023       cpp_fatal("-MG must be specified with one of -M or -MM");
6024
6025    /* Either of two environment variables can specify output of deps.
6026     * Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6027     * where OUTPUT_FILE is the file to write deps info to
6028     * and DEPS_TARGET is the target to mention in the deps.  */
6029
6030    if (opts->print_deps == 0
6031        && (getenv("SUNPRO_DEPENDENCIES") != 0
6032            || getenv("DEPENDENCIES_OUTPUT") != 0))
6033      {
6034         char               *spec = getenv("DEPENDENCIES_OUTPUT");
6035         char               *s;
6036         char               *output_file;
6037
6038         if (!spec)
6039           {
6040              spec = getenv("SUNPRO_DEPENDENCIES");
6041              opts->print_deps = 2;
6042           }
6043         else
6044            opts->print_deps = 1;
6045
6046         s = spec;
6047         /* Find the space before the DEPS_TARGET, if there is one.  */
6048         /* This should use index.  (mrs) */
6049         while (*s != 0 && *s != ' ')
6050            s++;
6051         if (*s != 0)
6052           {
6053              opts->deps_target = s + 1;
6054              output_file = (char *)xmalloc(s - spec + 1);
6055              memcpy(output_file, spec, s - spec);
6056              output_file[s - spec] = 0;
6057           }
6058         else
6059           {
6060              opts->deps_target = 0;
6061              output_file = spec;
6062           }
6063
6064         opts->deps_file = output_file;
6065         opts->print_deps_append = 1;
6066      }
6067    /* For -M, print the expected object file name
6068     * as the target of this Make-rule.  */
6069    if (opts->print_deps)
6070      {
6071         pfile->deps_allocated_size = 200;
6072         pfile->deps_buffer = (char *)xmalloc(pfile->deps_allocated_size);
6073         pfile->deps_buffer[0] = 0;
6074         pfile->deps_size = 0;
6075         pfile->deps_column = 0;
6076
6077         if (opts->deps_target)
6078            deps_output(pfile, opts->deps_target, ':');
6079         else if (*opts->in_fname == 0)
6080            deps_output(pfile, "-", ':');
6081         else
6082           {
6083              char               *q;
6084              int                 len;
6085
6086              /* Discard all directory prefixes from filename.  */
6087              if ((q = (char *)strrchr(opts->in_fname, '/'))
6088 #ifdef DIR_SEPARATOR
6089                  && (q = strrchr(opts->in_fname, DIR_SEPARATOR))
6090 #endif
6091                 )
6092                 ++q;
6093              else
6094                 q = (char *)opts->in_fname;
6095
6096              /* Copy remainder to mungable area.  */
6097              p = (char *)alloca(strlen(q) + 8);
6098              strcpy(p, q);
6099
6100              /* Output P, but remove known suffixes.  */
6101              len = strlen(p);
6102              q = p + len;
6103              if (len >= 2 && p[len - 2] == '.' && strchr("cCsSm", p[len - 1]))
6104                 q = p + (len - 2);
6105              else if (len >= 3
6106                       && p[len - 3] == '.'
6107                       && p[len - 2] == 'c' && p[len - 1] == 'c')
6108                 q = p + (len - 3);
6109              else if (len >= 4
6110                       && p[len - 4] == '.'
6111                       && p[len - 3] == 'c'
6112                       && p[len - 2] == 'x' && p[len - 1] == 'x')
6113                 q = p + (len - 4);
6114              else if (len >= 4
6115                       && p[len - 4] == '.'
6116                       && p[len - 3] == 'c'
6117                       && p[len - 2] == 'p' && p[len - 1] == 'p')
6118                 q = p + (len - 4);
6119
6120              /* Supply our own suffix.  */
6121              strcpy(q, ".o");
6122
6123              deps_output(pfile, p, ':');
6124              deps_output(pfile, opts->in_fname, ' ');
6125           }
6126      }
6127
6128    /* Scan the -include files before the main input.
6129     * We push these in reverse order, so that the first one is handled first.  */
6130
6131    pfile->no_record_file++;
6132    opts->pending = nreverse_pending(opts->pending);
6133    for (pend = opts->pending; pend; pend = pend->next)
6134      {
6135         if (pend->cmd && strcmp(pend->cmd, "-include") == 0)
6136           {
6137              int                 fd = open(pend->arg, O_RDONLY | O_BINARY, 0666);
6138
6139              if (fd < 0)
6140                {
6141                   cpp_perror_with_name(pfile, pend->arg);
6142                   return FATAL_EXIT_CODE;
6143                }
6144              cpp_push_buffer(pfile, NULL, 0);
6145              finclude(pfile, fd, pend->arg, 0, NULL);
6146           }
6147      }
6148    pfile->no_record_file--;
6149
6150    /* Free the pending list. */
6151    for (pend = opts->pending; pend;)
6152      {
6153         struct cpp_pending *next = pend->next;
6154
6155         free(pend);
6156         pend = next;
6157      }
6158    opts->pending = NULL;
6159
6160    if (finclude(pfile, f, fname, 0, NULL))
6161       output_line_command(pfile, 0, same_file);
6162    return SUCCESS_EXIT_CODE;
6163 }
6164
6165 void
6166 init_parse_file(cpp_reader * pfile)
6167 {
6168    memset((char *)pfile, 0, sizeof(cpp_reader));
6169    pfile->get_token = cpp_get_token;
6170
6171    pfile->token_buffer_size = 200;
6172    pfile->token_buffer = (unsigned char *)xmalloc(pfile->token_buffer_size);
6173    CPP_SET_WRITTEN(pfile, 0);
6174
6175    pfile->system_include_depth = 0;
6176    pfile->dont_repeat_files = 0;
6177    pfile->all_include_files = 0;
6178    pfile->max_include_len = 0;
6179    pfile->timebuf = NULL;
6180    pfile->only_seen_white = 1;
6181    pfile->buffer = CPP_NULL_BUFFER(pfile);
6182 }
6183
6184 static struct cpp_pending *
6185 nreverse_pending(struct cpp_pending *list)
6186 {
6187    struct cpp_pending *prev = 0, *next, *pend;
6188
6189    for (pend = list; pend; pend = next)
6190      {
6191         next = pend->next;
6192         pend->next = prev;
6193         prev = pend;
6194      }
6195    return prev;
6196 }
6197
6198 static void
6199 push_pending(cpp_reader * pfile, const char *cmd, const char *arg)
6200 {
6201    struct cpp_pending *pend
6202       = (struct cpp_pending *)xmalloc(sizeof(struct cpp_pending));
6203
6204    pend->cmd = cmd;
6205    pend->arg = arg;
6206    pend->next = CPP_OPTIONS(pfile)->pending;
6207    CPP_OPTIONS(pfile)->pending = pend;
6208 }
6209
6210 /* Handle command-line options in (argc, argv).
6211  * Can be called multiple times, to handle multiple sets of options.
6212  * Returns if an unrecognized option is seen.
6213  * Returns number of handled arguments.  */
6214
6215 int
6216 cpp_handle_options(cpp_reader * pfile, int argc, char **argv)
6217 {
6218    int                 i;
6219    struct cpp_options *opts = CPP_OPTIONS(pfile);
6220
6221    for (i = 0; i < argc; i++)
6222      {
6223         if (argv[i][0] != '-')
6224           {
6225              if (opts->out_fname)
6226                 cpp_fatal("Usage: %s [switches] input output", argv[0]);
6227              else if (opts->in_fname)
6228                 opts->out_fname = argv[i];
6229              else
6230                 opts->in_fname = argv[i];
6231           }
6232         else
6233           {
6234              switch (argv[i][1])
6235                {
6236
6237                case 'i':
6238                   if (!strcmp(argv[i], "-include")
6239                       || !strcmp(argv[i], "-imacros"))
6240                     {
6241                        if (i + 1 == argc)
6242                           cpp_fatal("Filename missing after `%s' option",
6243                                     argv[i]);
6244                        else
6245                           push_pending(pfile, argv[i], argv[i + 1]), i++;
6246                     }
6247                   if (!strcmp(argv[i], "-iprefix"))
6248                     {
6249                        if (i + 1 == argc)
6250                           cpp_fatal("Filename missing after `-iprefix' option");
6251                        else
6252                           opts->include_prefix = argv[++i];
6253                     }
6254                   if (!strcmp(argv[i], "-ifoutput"))
6255                     {
6256                        opts->output_conditionals = 1;
6257                     }
6258                   if (!strcmp(argv[i], "-isystem"))
6259                     {
6260                        file_name_list     *dirtmp;
6261
6262                        if (i + 1 == argc)
6263                           cpp_fatal("Filename missing after `-isystem' option");
6264
6265                        dirtmp =
6266                           (file_name_list *) xmalloc(sizeof(file_name_list));
6267
6268                        dirtmp->next = 0;
6269                        dirtmp->control_macro = 0;
6270                        dirtmp->c_system_include_path = 1;
6271                        dirtmp->fname = (char *)xmalloc(strlen(argv[i + 1]) + 1);
6272                        strcpy(dirtmp->fname, argv[++i]);
6273                        dirtmp->got_name_map = 0;
6274
6275                        if (!opts->before_system)
6276                           opts->before_system = dirtmp;
6277                        else
6278                           opts->last_before_system->next = dirtmp;
6279                        opts->last_before_system = dirtmp;       /* Tail follows the last one */
6280                     }
6281                   /* Add directory to end of path for includes,
6282                    * with the default prefix at the front of its name.  */
6283                   if (!strcmp(argv[i], "-iwithprefix"))
6284                     {
6285                        file_name_list     *dirtmp;
6286                        char               *prefix;
6287
6288                        if (opts->include_prefix)
6289                           prefix = opts->include_prefix;
6290                        else
6291                          {
6292                             prefix = savestring(GCC_INCLUDE_DIR);
6293                             /* Remove the `include' from /usr/local/lib/gcc.../include.  */
6294                             if (!strcmp
6295                                 (prefix + strlen(prefix) - 8, "/include"))
6296                                prefix[strlen(prefix) - 7] = 0;
6297                          }
6298
6299                        dirtmp =
6300                           (file_name_list *) xmalloc(sizeof(file_name_list));
6301
6302                        dirtmp->next = 0;        /* New one goes on the end */
6303                        dirtmp->control_macro = 0;
6304                        dirtmp->c_system_include_path = 0;
6305                        if (i + 1 == argc)
6306                           cpp_fatal
6307                              ("Directory name missing after `-iwithprefix' option");
6308
6309                        dirtmp->fname = (char *)xmalloc(strlen(argv[i + 1])
6310                                                        + strlen(prefix) + 1);
6311                        strcpy(dirtmp->fname, prefix);
6312                        strcat(dirtmp->fname, argv[++i]);
6313                        dirtmp->got_name_map = 0;
6314
6315                        if (!opts->after_include)
6316                           opts->after_include = dirtmp;
6317                        else
6318                           opts->last_after_include->next = dirtmp;
6319                        opts->last_after_include = dirtmp;       /* Tail follows the last one */
6320                     }
6321                   /* Add directory to main path for includes,
6322                    * with the default prefix at the front of its name.  */
6323                   if (!strcmp(argv[i], "-iwithprefixbefore"))
6324                     {
6325                        file_name_list     *dirtmp;
6326                        char               *prefix;
6327
6328                        if (opts->include_prefix)
6329                           prefix = opts->include_prefix;
6330                        else
6331                          {
6332                             prefix = savestring(GCC_INCLUDE_DIR);
6333                             /* Remove the `include' from /usr/local/lib/gcc.../include.  */
6334                             if (!strcmp
6335                                 (prefix + strlen(prefix) - 8, "/include"))
6336                                prefix[strlen(prefix) - 7] = 0;
6337                          }
6338
6339                        dirtmp =
6340                           (file_name_list *) xmalloc(sizeof(file_name_list));
6341
6342                        dirtmp->next = 0;        /* New one goes on the end */
6343                        dirtmp->control_macro = 0;
6344                        dirtmp->c_system_include_path = 0;
6345                        if (i + 1 == argc)
6346                           cpp_fatal
6347                              ("Directory name missing after `-iwithprefixbefore' option");
6348
6349                        dirtmp->fname = (char *)xmalloc(strlen(argv[i + 1])
6350                                                        + strlen(prefix) + 1);
6351                        strcpy(dirtmp->fname, prefix);
6352                        strcat(dirtmp->fname, argv[++i]);
6353                        dirtmp->got_name_map = 0;
6354
6355                        append_include_chain(pfile, dirtmp, dirtmp);
6356                     }
6357                   /* Add directory to end of path for includes.  */
6358                   if (!strcmp(argv[i], "-idirafter"))
6359                     {
6360                        file_name_list     *dirtmp;
6361
6362                        dirtmp =
6363                           (file_name_list *) xmalloc(sizeof(file_name_list));
6364
6365                        dirtmp->next = 0;        /* New one goes on the end */
6366                        dirtmp->control_macro = 0;
6367                        dirtmp->c_system_include_path = 0;
6368                        if (i + 1 == argc)
6369                           cpp_fatal
6370                              ("Directory name missing after `-idirafter' option");
6371                        else
6372                           dirtmp->fname = argv[++i];
6373                        dirtmp->got_name_map = 0;
6374
6375                        if (!opts->after_include)
6376                           opts->after_include = dirtmp;
6377                        else
6378                           opts->last_after_include->next = dirtmp;
6379                        opts->last_after_include = dirtmp;       /* Tail follows the last one */
6380                     }
6381                   break;
6382
6383                case 'o':
6384                   if (opts->out_fname)
6385                      cpp_fatal("Output filename specified twice");
6386                   if (i + 1 == argc)
6387                      cpp_fatal("Filename missing after -o option");
6388                   opts->out_fname = argv[++i];
6389                   if (!strcmp(opts->out_fname, "-"))
6390                      opts->out_fname = "";
6391                   break;
6392
6393                case 'p':
6394                   if (!strcmp(argv[i], "-pedantic"))
6395                      CPP_PEDANTIC(pfile) = 1;
6396                   else if (!strcmp(argv[i], "-pedantic-errors"))
6397                     {
6398                        CPP_PEDANTIC(pfile) = 1;
6399                        opts->pedantic_errors = 1;
6400                     }
6401                   break;
6402
6403                case 't':
6404                   if (!strcmp(argv[i], "-trigraphs"))
6405                     {
6406                        if (!opts->chill)
6407                           opts->no_trigraphs = 0;
6408                     }
6409                   break;
6410
6411                case 'l':
6412                   if (!strcmp(argv[i], "-lang-c"))
6413                      opts->cplusplus = 0, opts->cplusplus_comments =
6414                         0, opts->objc = 0;
6415                   if (!strcmp(argv[i], "-lang-c++"))
6416                      opts->cplusplus = 1, opts->cplusplus_comments =
6417                         1, opts->objc = 0;
6418                   if (!strcmp(argv[i], "-lang-c-c++-comments"))
6419                      opts->cplusplus = 0, opts->cplusplus_comments =
6420                         1, opts->objc = 0;
6421                   if (!strcmp(argv[i], "-lang-objc"))
6422                      opts->objc = 1, opts->cplusplus =
6423                         0, opts->cplusplus_comments = 1;
6424                   if (!strcmp(argv[i], "-lang-objc++"))
6425                      opts->objc = 1, opts->cplusplus =
6426                         1, opts->cplusplus_comments = 1;
6427                   if (!strcmp(argv[i], "-lang-asm"))
6428                      opts->lang_asm = 1;
6429                   if (!strcmp(argv[i], "-lint"))
6430                      opts->for_lint = 1;
6431                   if (!strcmp(argv[i], "-lang-chill"))
6432                      opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6433                         opts->traditional = 1, opts->no_trigraphs = 1;
6434                   break;
6435
6436                case '+':
6437                   opts->cplusplus = 1, opts->cplusplus_comments = 1;
6438                   break;
6439
6440                case 'w':
6441                   opts->inhibit_warnings = 1;
6442                   break;
6443
6444                case 'W':
6445                   if (!strcmp(argv[i], "-Wtrigraphs"))
6446                      opts->warn_trigraphs = 1;
6447                   else if (!strcmp(argv[i], "-Wno-trigraphs"))
6448                      opts->warn_trigraphs = 0;
6449                   else if (!strcmp(argv[i], "-Wcomment"))
6450                      opts->warn_comments = 1;
6451                   else if (!strcmp(argv[i], "-Wno-comment"))
6452                      opts->warn_comments = 0;
6453                   else if (!strcmp(argv[i], "-Wcomments"))
6454                      opts->warn_comments = 1;
6455                   else if (!strcmp(argv[i], "-Wno-comments"))
6456                      opts->warn_comments = 0;
6457                   else if (!strcmp(argv[i], "-Wtraditional"))
6458                      opts->warn_stringify = 1;
6459                   else if (!strcmp(argv[i], "-Wno-traditional"))
6460                      opts->warn_stringify = 0;
6461                   else if (!strcmp(argv[i], "-Wimport"))
6462                      opts->warn_import = 1;
6463                   else if (!strcmp(argv[i], "-Wno-import"))
6464                      opts->warn_import = 0;
6465                   else if (!strcmp(argv[i], "-Werror"))
6466                      opts->warnings_are_errors = 1;
6467                   else if (!strcmp(argv[i], "-Wno-error"))
6468                      opts->warnings_are_errors = 0;
6469                   else if (!strcmp(argv[i], "-Wall"))
6470                     {
6471                        opts->warn_trigraphs = 1;
6472                        opts->warn_comments = 1;
6473                     }
6474                   break;
6475
6476                case 'M':
6477                   /* The style of the choices here is a bit mixed.
6478                    * The chosen scheme is a hybrid of keeping all options in one string
6479                    * and specifying each option in a separate argument:
6480                    * -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
6481                    * -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6482                    * -M[M][G][D file].  This is awkward to handle in specs, and is not
6483                    * as extensible.  */
6484                   /* ??? -MG must be specified in addition to one of -M or -MM.
6485                    * This can be relaxed in the future without breaking anything.
6486                    * The converse isn't true.  */
6487
6488                   /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
6489                   if (!strcmp(argv[i], "-MG"))
6490                     {
6491                        opts->print_deps_missing_files = 1;
6492                        break;
6493                     }
6494                   if (!strcmp(argv[i], "-M"))
6495                      opts->print_deps = 2;
6496                   else if (!strcmp(argv[i], "-MM"))
6497                      opts->print_deps = 1;
6498                   else if (!strcmp(argv[i], "-MD"))
6499                      opts->print_deps = 2;
6500                   else if (!strcmp(argv[i], "-MMD"))
6501                      opts->print_deps = 1;
6502                   /* For -MD and -MMD options, write deps on file named by next arg.  */
6503                   if (!strcmp(argv[i], "-MD") || !strcmp(argv[i], "-MMD"))
6504                     {
6505                        if (i + 1 == argc)
6506                           cpp_fatal("Filename missing after %s option",
6507                                     argv[i]);
6508                        opts->deps_file = argv[++i];
6509                     }
6510                   else
6511                     {
6512                        /* For -M and -MM, write deps on standard output
6513                         * and suppress the usual output.  */
6514                        opts->no_output = 1;
6515                     }
6516                   break;
6517
6518                case 'd':
6519                   {
6520                      char               *p = argv[i] + 2;
6521                      char                c;
6522
6523                      while ((c = *p++) != 0)
6524                        {
6525                           /* Arg to -d specifies what parts of macros to dump */
6526                           switch (c)
6527                             {
6528                             case 'M':
6529                                opts->dump_macros = dump_only;
6530                                opts->no_output = 1;
6531                                break;
6532                             case 'N':
6533                                opts->dump_macros = dump_names;
6534                                break;
6535                             case 'D':
6536                                opts->dump_macros = dump_definitions;
6537                                break;
6538                             }
6539                        }
6540                   }
6541                   break;
6542
6543                case 'g':
6544                   if (argv[i][2] == '3')
6545                      opts->debug_output = 1;
6546                   break;
6547
6548                case 'v':
6549                   fprintf(stderr, "GNU CPP version %s", version_string);
6550 #ifdef TARGET_VERSION
6551                   TARGET_VERSION;
6552 #endif
6553                   fprintf(stderr, "\n");
6554                   opts->verbose = 1;
6555                   break;
6556
6557                case 'H':
6558                   opts->print_include_names = 1;
6559                   break;
6560
6561                case 'D':
6562                   if (argv[i][2] != 0)
6563                      push_pending(pfile, "-D", argv[i] + 2);
6564                   else if (i + 1 == argc)
6565                      cpp_fatal("Macro name missing after -D option");
6566                   else
6567                      i++, push_pending(pfile, "-D", argv[i]);
6568                   break;
6569
6570                case 'a':
6571                   {
6572                      if (!strcmp(argv[i], "-a"))
6573                        {
6574                           if (i + 1 == argc)
6575                             cpp_fatal("Filename missing after `-a` option");
6576                           else if (strcmp(argv[++i], "/dev/null"))
6577                             {
6578                                opts->watchfile = argv[i];
6579                             }
6580                        }
6581                      break;
6582                   }
6583
6584                case 'A':
6585                   {
6586                      char               *p = NULL;
6587
6588                      if (argv[i][2] != 0)
6589                         p = argv[i] + 2;
6590                      else if (i + 1 == argc)
6591                         cpp_fatal("Assertion missing after -A option");
6592                      else
6593                         p = argv[++i];
6594
6595                      if (!strcmp(p, "-"))
6596                        {
6597                           struct cpp_pending **ptr;
6598
6599                           /* -A- eliminates all predefined macros and assertions.
6600                            * Let's include also any that were specified earlier
6601                            * on the command line.  That way we can get rid of any
6602                            * that were passed automatically in from GCC.  */
6603
6604                           opts->inhibit_predefs = 1;
6605                           for (ptr = &opts->pending; *ptr;)
6606                             {
6607                                struct cpp_pending *pend = *ptr;
6608
6609                                if (pend->cmd && pend->cmd[0] == '-'
6610                                    && (pend->cmd[1] == 'D'
6611                                        || pend->cmd[1] == 'A'))
6612                                  {
6613                                     *ptr = pend->next;
6614                                     free(pend);
6615                                  }
6616                                else
6617                                   ptr = &pend->next;
6618                             }
6619                        }
6620                      else
6621                        {
6622                           push_pending(pfile, "-A", p);
6623                        }
6624                   }
6625                   break;
6626
6627                case 'U':        /* JF #undef something */
6628                   if (argv[i][2] != 0)
6629                      push_pending(pfile, "-U", argv[i] + 2);
6630                   else if (i + 1 == argc)
6631                      cpp_fatal("Macro name missing after -U option");
6632                   else
6633                      push_pending(pfile, "-U", argv[i + 1]), i++;
6634                   break;
6635
6636                case 'C':
6637                   opts->put_out_comments = 1;
6638                   break;
6639
6640                case 'E':        /* -E comes from cc -E; ignore it.  */
6641                   break;
6642
6643                case 'P':
6644                   opts->no_line_commands = 1;
6645                   break;
6646
6647                case '$':        /* Don't include $ in identifiers.  */
6648                   opts->dollars_in_ident = 0;
6649                   break;
6650
6651                case 'I':        /* Add directory to path for includes.  */
6652                   {
6653                      file_name_list     *dirtmp;
6654
6655                      if (!CPP_OPTIONS(pfile)->ignore_srcdir
6656                          && !strcmp(argv[i] + 2, "-"))
6657                        {
6658                           CPP_OPTIONS(pfile)->ignore_srcdir = 1;
6659                           /* Don't use any preceding -I directories for #include <...>.  */
6660                           CPP_OPTIONS(pfile)->first_bracket_include = 0;
6661                        }
6662                      else
6663                        {
6664                           dirtmp =
6665                              (file_name_list *) xmalloc(sizeof(file_name_list));
6666
6667                           dirtmp->next = 0;     /* New one goes on the end */
6668                           dirtmp->control_macro = 0;
6669                           dirtmp->c_system_include_path = 0;
6670                           if (argv[i][2] != 0)
6671                              dirtmp->fname = argv[i] + 2;
6672                           else if (i + 1 == argc)
6673                              cpp_fatal
6674                                 ("Directory name missing after -I option");
6675                           else
6676                              dirtmp->fname = argv[++i];
6677                           dirtmp->got_name_map = 0;
6678                           append_include_chain(pfile, dirtmp, dirtmp);
6679                        }
6680                   }
6681                   break;
6682
6683                case 'n':
6684                   if (!strcmp(argv[i], "-nostdinc"))
6685                      /* -nostdinc causes no default include directories.
6686                       * You must specify all include-file directories with -I.  */
6687                      opts->no_standard_includes = 1;
6688                   else if (!strcmp(argv[i], "-nostdinc++"))
6689                      /* -nostdinc++ causes no default C++-specific include directories. */
6690                      opts->no_standard_cplusplus_includes = 1;
6691                   break;
6692
6693                case 'u':
6694                   /* Sun compiler passes undocumented switch "-undef".
6695                    * Let's assume it means to inhibit the predefined symbols.  */
6696                   opts->inhibit_predefs = 1;
6697                   break;
6698
6699                case '\0':       /* JF handle '-' as file name meaning stdin or stdout */
6700                   if (!opts->in_fname)
6701                     {
6702                        opts->in_fname = "";
6703                        break;
6704                     }
6705                   else if (!opts->out_fname)
6706                     {
6707                        opts->out_fname = "";
6708                        break;
6709                     }           /* else fall through into error */
6710                default:
6711                   return i;
6712                }
6713           }
6714      }
6715    return i;
6716 }
6717
6718 void
6719 cpp_finish(cpp_reader * pfile)
6720 {
6721    struct cpp_options *opts = CPP_OPTIONS(pfile);
6722
6723    if (opts->print_deps)
6724      {
6725         /* Stream on which to print the dependency information.  */
6726         FILE               *deps_stream;
6727
6728         /* Don't actually write the deps file if compilation has failed.  */
6729         if (pfile->errors == 0)
6730           {
6731              const char         *deps_mode =
6732                 opts->print_deps_append ? "ab" : "wb";
6733
6734              if (!opts->deps_file)
6735                 deps_stream = stdout;
6736              else if (!(deps_stream = fopen(opts->deps_file, deps_mode)))
6737                 cpp_pfatal_with_name(pfile, opts->deps_file);
6738              fputs(pfile->deps_buffer, deps_stream);
6739              putc('\n', deps_stream);
6740              if (opts->deps_file)
6741                {
6742                   if (ferror(deps_stream) || fclose(deps_stream) != 0)
6743                      cpp_fatal("I/O error on output");
6744                }
6745           }
6746      }
6747 }
6748
6749 static int
6750 do_assert(cpp_reader * pfile, struct directive *keyword __UNUSED__,
6751           unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
6752 {
6753    long                symstart;        /* remember where symbol name starts */
6754    int                 c;
6755    int                 sym_length;      /* and how long it is */
6756    struct arglist     *tokens = NULL;
6757
6758    if (CPP_PEDANTIC(pfile) && CPP_OPTIONS(pfile)->done_initializing
6759        && !CPP_BUFFER(pfile)->system_header_p)
6760       cpp_pedwarn(pfile, "ANSI C does not allow `#assert'");
6761
6762    cpp_skip_hspace(pfile);
6763    symstart = CPP_WRITTEN(pfile);       /* remember where it starts */
6764    parse_name(pfile, GETC());
6765    sym_length = check_macro_name(pfile, pfile->token_buffer + symstart,
6766                                  "assertion");
6767
6768    cpp_skip_hspace(pfile);
6769    if (PEEKC() != '(')
6770      {
6771         cpp_error(pfile, "missing token-sequence in `#assert'");
6772         goto error;
6773      }
6774    {
6775       int                 error_flag = 0;
6776
6777       tokens = read_token_list(pfile, &error_flag);
6778       if (error_flag)
6779          goto error;
6780       if (!tokens)
6781         {
6782            cpp_error(pfile, "empty token-sequence in `#assert'");
6783            goto error;
6784         }
6785       cpp_skip_hspace(pfile);
6786       c = PEEKC();
6787       if (c != EOF && c != '\n')
6788          cpp_pedwarn(pfile, "junk at end of `#assert'");
6789       skip_rest_of_line(pfile);
6790    }
6791
6792    /* If this name isn't already an assertion name, make it one.
6793     * Error if it was already in use in some other way.  */
6794
6795    {
6796       ASSERTION_HASHNODE *hp;
6797       const char         *symname = (char *)pfile->token_buffer + symstart;
6798       int                 hashcode =
6799          hashf(symname, sym_length, ASSERTION_HASHSIZE);
6800       struct tokenlist_list *value =
6801          (struct tokenlist_list *)xmalloc(sizeof(struct tokenlist_list));
6802
6803       hp = assertion_lookup(pfile, symname, sym_length, hashcode);
6804       if (!hp)
6805         {
6806            if (sym_length == 7 && !strncmp(symname, "defined", sym_length))
6807               cpp_error(pfile, "`defined' redefined as assertion");
6808            hp = assertion_install(pfile, symname, sym_length, hashcode);
6809         }
6810       /* Add the spec'd token-sequence to the list of such.  */
6811       value->tokens = tokens;
6812       value->next = hp->value;
6813       hp->value = value;
6814    }
6815    CPP_SET_WRITTEN(pfile, symstart);    /* Pop */
6816    return 0;
6817  error:
6818    CPP_SET_WRITTEN(pfile, symstart);    /* Pop */
6819    skip_rest_of_line(pfile);
6820    return 1;
6821 }
6822
6823 static int
6824 do_unassert(cpp_reader * pfile, struct directive *keyword __UNUSED__,
6825             unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
6826 {
6827    long                symstart;        /* remember where symbol name starts */
6828    int                 sym_length;      /* and how long it is */
6829    int                 c;
6830
6831    struct arglist     *tokens = NULL;
6832    int                 tokens_specified = 0;
6833
6834    if (CPP_PEDANTIC(pfile) && CPP_OPTIONS(pfile)->done_initializing
6835        && !CPP_BUFFER(pfile)->system_header_p)
6836       cpp_pedwarn(pfile, "ANSI C does not allow `#unassert'");
6837
6838    cpp_skip_hspace(pfile);
6839
6840    symstart = CPP_WRITTEN(pfile);       /* remember where it starts */
6841    parse_name(pfile, GETC());
6842    sym_length = check_macro_name(pfile, pfile->token_buffer + symstart,
6843                                  "assertion");
6844
6845    cpp_skip_hspace(pfile);
6846    if (PEEKC() == '(')
6847      {
6848         int                 error_flag = 0;
6849
6850         tokens = read_token_list(pfile, &error_flag);
6851         if (error_flag)
6852            goto error;
6853         if (!tokens)
6854           {
6855              cpp_error(pfile, "empty token list in `#unassert'");
6856              goto error;
6857           }
6858         tokens_specified = 1;
6859      }
6860    cpp_skip_hspace(pfile);
6861    c = PEEKC();
6862    if (c != EOF && c != '\n')
6863       cpp_error(pfile, "junk at end of `#unassert'");
6864    skip_rest_of_line(pfile);
6865
6866    {
6867       ASSERTION_HASHNODE *hp;
6868       const char         *symname = (char *)pfile->token_buffer + symstart;
6869       int                 hashcode =
6870          hashf(symname, sym_length, ASSERTION_HASHSIZE);
6871       struct tokenlist_list *tail, *prev;
6872
6873       hp = assertion_lookup(pfile, symname, sym_length, hashcode);
6874       if (!hp)
6875          return 1;
6876
6877       /* If no token list was specified, then eliminate this assertion
6878        * entirely.  */
6879       if (!tokens_specified)
6880          delete_assertion(hp);
6881       else
6882         {
6883            /* If a list of tokens was given, then delete any matching list.  */
6884
6885            tail = hp->value;
6886            prev = 0;
6887            while (tail)
6888              {
6889                 struct tokenlist_list *next = tail->next;
6890
6891                 if (compare_token_lists(tail->tokens, tokens))
6892                   {
6893                      if (prev)
6894                         prev->next = next;
6895                      else
6896                         hp->value = tail->next;
6897                      free_token_list(tail->tokens);
6898                      free(tail);
6899                   }
6900                 else
6901                   {
6902                      prev = tail;
6903                   }
6904                 tail = next;
6905              }
6906         }
6907    }
6908
6909    CPP_SET_WRITTEN(pfile, symstart);    /* Pop */
6910    return 0;
6911  error:
6912    CPP_SET_WRITTEN(pfile, symstart);    /* Pop */
6913    skip_rest_of_line(pfile);
6914    return 1;
6915 }
6916
6917 /* Test whether there is an assertion named NAME
6918  * and optionally whether it has an asserted token list TOKENS.
6919  * NAME is not null terminated; its length is SYM_LENGTH.
6920  * If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
6921
6922 static int
6923 check_assertion(cpp_reader * pfile, const char *name, int sym_length,
6924                 int tokens_specified, struct arglist *tokens)
6925 {
6926    ASSERTION_HASHNODE *hp;
6927    int                 hashcode = hashf(name, sym_length, ASSERTION_HASHSIZE);
6928
6929    if (CPP_PEDANTIC(pfile) && !CPP_BUFFER(pfile)->system_header_p)
6930       cpp_pedwarn(pfile, "ANSI C does not allow testing assertions");
6931
6932    hp = assertion_lookup(pfile, name, sym_length, hashcode);
6933    if (!hp)
6934       /* It is not an assertion; just return false.  */
6935       return 0;
6936
6937    /* If no token list was specified, then value is 1.  */
6938    if (!tokens_specified)
6939       return 1;
6940
6941    {
6942       struct tokenlist_list *tail;
6943
6944       tail = hp->value;
6945
6946       /* If a list of tokens was given,
6947        * then succeed if the assertion records a matching list.  */
6948
6949       while (tail)
6950         {
6951            if (compare_token_lists(tail->tokens, tokens))
6952               return 1;
6953            tail = tail->next;
6954         }
6955
6956       /* Fail if the assertion has no matching list.  */
6957       return 0;
6958    }
6959 }
6960
6961 /* Compare two lists of tokens for equality including order of tokens.  */
6962
6963 static int
6964 compare_token_lists(struct arglist *l1, struct arglist *l2)
6965 {
6966    while (l1 && l2)
6967      {
6968         if (l1->length != l2->length)
6969            return 0;
6970         if (strncmp(l1->name, l2->name, l1->length))
6971            return 0;
6972         l1 = l1->next;
6973         l2 = l2->next;
6974      }
6975
6976    /* Succeed if both lists end at the same time.  */
6977    return l1 == l2;
6978 }
6979
6980 struct arglist     *
6981 reverse_token_list(struct arglist *tokens)
6982 {
6983    struct arglist     *prev = 0, *cur, *next;
6984
6985    for (cur = tokens; cur; cur = next)
6986      {
6987         next = cur->next;
6988         cur->next = prev;
6989         prev = cur;
6990      }
6991    return prev;
6992 }
6993
6994 /* Read a space-separated list of tokens ending in a close parenthesis.
6995  * Return a list of strings, in the order they were written.
6996  * (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
6997
6998 static struct arglist *
6999 read_token_list(cpp_reader * pfile, int *error_flag)
7000 {
7001    struct arglist     *token_ptrs = 0;
7002    int                 depth = 1;
7003    int                 length;
7004
7005    *error_flag = 0;
7006    FORWARD(1);                  /* Skip '(' */
7007
7008    /* Loop over the assertion value tokens.  */
7009    while (depth > 0)
7010      {
7011         struct arglist     *temp;
7012         long                name_written = CPP_WRITTEN(pfile);
7013         int                 c;
7014
7015         cpp_skip_hspace(pfile);
7016
7017         c = GETC();
7018
7019         /* Find the end of the token.  */
7020         if (c == '(')
7021           {
7022              CPP_PUTC(pfile, c);
7023              depth++;
7024           }
7025         else if (c == ')')
7026           {
7027              depth--;
7028              if (depth == 0)
7029                 break;
7030              CPP_PUTC(pfile, c);
7031           }
7032         else if (c == '"' || c == '\'')
7033           {
7034              FORWARD(-1);
7035              cpp_get_token(pfile);
7036           }
7037         else if (c == '\n')
7038            break;
7039         else
7040           {
7041              while (c != EOF && !is_space[c] && c != '(' && c != ')'
7042                     && c != '"' && c != '\'')
7043                {
7044                   CPP_PUTC(pfile, c);
7045                   c = GETC();
7046                }
7047              if (c != EOF)
7048                 FORWARD(-1);
7049           }
7050
7051         length = CPP_WRITTEN(pfile) - name_written;
7052         temp = (struct arglist *)xmalloc(sizeof(struct arglist) + length + 1);
7053
7054         temp->name = (char *)(temp + 1);
7055         memcpy(temp->name, (char *)(pfile->token_buffer + name_written),
7056                length);
7057         temp->name[length] = 0;
7058         temp->next = token_ptrs;
7059         token_ptrs = temp;
7060         temp->length = length;
7061
7062         CPP_ADJUST_WRITTEN(pfile, -length);     /* pop */
7063
7064         if (c == EOF || c == '\n')
7065           {                     /* FIXME */
7066              cpp_error(pfile,
7067                        "unterminated token sequence following  `#' operator");
7068              return 0;
7069           }
7070      }
7071
7072    /* We accumulated the names in reverse order.
7073     * Now reverse them to get the proper order.  */
7074    return reverse_token_list(token_ptrs);
7075 }
7076
7077 static void
7078 free_token_list(struct arglist *tokens)
7079 {
7080    while (tokens)
7081      {
7082         struct arglist     *next = tokens->next;
7083
7084         free(tokens->name);
7085         free(tokens);
7086         tokens = next;
7087      }
7088 }
7089
7090 /* Get the file-mode and data size of the file open on FD
7091  * and store them in *MODE_POINTER and *SIZE_POINTER.  */
7092
7093 static int
7094 file_size_and_mode(int fd, int *mode_pointer, long int *size_pointer)
7095 {
7096    struct stat         sbuf;
7097
7098    if (fstat(fd, &sbuf) < 0)
7099       return (-1);
7100    if (mode_pointer)
7101       *mode_pointer = sbuf.st_mode;
7102    if (size_pointer)
7103       *size_pointer = sbuf.st_size;
7104    return 0;
7105 }
7106
7107 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7108  * retrying if necessary.  Return a negative value if an error occurs,
7109  * otherwise return the actual number of bytes read,
7110  * which must be LEN unless end-of-file was reached.  */
7111
7112 static int
7113 safe_read(int desc, char *ptr, int len)
7114 {
7115    int                 left = len;
7116
7117    while (left > 0)
7118      {
7119         int                 nchars = read(desc, ptr, left);
7120
7121         if (nchars < 0)
7122           {
7123 #ifdef EINTR
7124              if (errno == EINTR)
7125                 continue;
7126 #endif
7127              return nchars;
7128           }
7129         if (nchars == 0)
7130            break;
7131         ptr += nchars;
7132         left -= nchars;
7133      }
7134    return len - left;
7135 }
7136
7137 static char        *
7138 savestring(const char *input)
7139 {
7140    unsigned            size = strlen(input);
7141    char               *output = (char *)xmalloc(size + 1);
7142
7143    strcpy(output, input);
7144    return output;
7145 }
7146
7147 /* Initialize PMARK to remember the current position of PFILE. */
7148 void
7149 parse_set_mark(struct parse_marker *pmark, cpp_reader * pfile)
7150 {
7151    cpp_buffer         *pbuf = CPP_BUFFER(pfile);
7152
7153    pmark->next = pbuf->marks;
7154    pbuf->marks = pmark;
7155    pmark->buf = pbuf;
7156    pmark->position = pbuf->cur - pbuf->buf;
7157 }
7158
7159 /* Cleanup PMARK - we no longer need it. */
7160 static void
7161 parse_clear_mark(struct parse_marker *pmark)
7162 {
7163    struct parse_marker **pp = &pmark->buf->marks;
7164
7165    for (;; pp = &(*pp)->next)
7166      {
7167         if (!*pp)
7168            cpp_fatal("internal error", "in parse_set_mark");
7169         if (*pp == pmark)
7170            break;
7171      }
7172    *pp = pmark->next;
7173 }
7174
7175 /* Backup the current position of PFILE to that saved in PMARK. */
7176
7177 static void
7178 parse_goto_mark(struct parse_marker *pmark, cpp_reader * pfile)
7179 {
7180    cpp_buffer         *pbuf = CPP_BUFFER(pfile);
7181
7182    if (pbuf != pmark->buf)
7183       cpp_fatal("internal error %s", "parse_goto_mark");
7184    pbuf->cur = pbuf->buf + pmark->position;
7185 }
7186
7187 /* Reset PMARK to point to the current position of PFILE.  (Same
7188  * as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7189
7190 static void
7191 parse_move_mark(struct parse_marker *pmark, cpp_reader * pfile)
7192 {
7193    cpp_buffer         *pbuf = CPP_BUFFER(pfile);
7194
7195    if (pbuf != pmark->buf)
7196       cpp_fatal("internal error %s", "parse_move_mark");
7197    pmark->position = pbuf->cur - pbuf->buf;
7198 }
7199
7200 int
7201 cpp_read_check_assertion(cpp_reader * pfile)
7202 {
7203    int                 name_start = CPP_WRITTEN(pfile);
7204    int                 name_length, name_written;
7205    int                 result;
7206
7207    FORWARD(1);                  /* Skip '#' */
7208    cpp_skip_hspace(pfile);
7209    parse_name(pfile, GETC());
7210    name_written = CPP_WRITTEN(pfile);
7211    name_length = name_written - name_start;
7212    cpp_skip_hspace(pfile);
7213    if (CPP_BUF_PEEK(CPP_BUFFER(pfile)) == '(')
7214      {
7215         int                 error_flag;
7216         struct arglist     *token_ptrs = read_token_list(pfile, &error_flag);
7217
7218         result = check_assertion(pfile,
7219                                  (char *)pfile->token_buffer + name_start,
7220                                  name_length, 1, token_ptrs);
7221      }
7222    else
7223       result = check_assertion(pfile,
7224                                (char *)pfile->token_buffer + name_start,
7225                                name_length, 0, NULL);
7226    CPP_ADJUST_WRITTEN(pfile, -name_length);     /* pop */
7227    return result;
7228 }
7229
7230 static void
7231 cpp_print_file_and_line(cpp_reader * pfile)
7232 {
7233    cpp_buffer         *ip = cpp_file_buffer(pfile);
7234
7235    if (ip)
7236      {
7237         long                line, col;
7238
7239         cpp_buf_line_and_col(ip, &line, &col);
7240         cpp_file_line_for_message(pfile, ip->nominal_fname,
7241                                   line, pfile->show_column ? col : -1);
7242      }
7243 }
7244
7245 static void
7246 cpp_error_v(cpp_reader * pfile, const char *msg, va_list args)
7247 {
7248    cpp_print_containing_files(pfile);
7249    cpp_print_file_and_line(pfile);
7250    cpp_message_v(pfile, 1, msg, args);
7251 }
7252
7253 void
7254 cpp_error(cpp_reader * pfile, const char *msg, ...)
7255 {
7256    va_list             args;
7257
7258    va_start(args, msg);
7259
7260    cpp_error_v(pfile, msg, args);
7261
7262    va_end(args);
7263 }
7264
7265 /* Print error message but don't count it.  */
7266
7267 static void
7268 cpp_warning_v(cpp_reader * pfile, const char *msg, va_list args)
7269 {
7270    if (CPP_OPTIONS(pfile)->inhibit_warnings)
7271       return;
7272
7273    if (CPP_OPTIONS(pfile)->warnings_are_errors)
7274       pfile->errors++;
7275
7276    cpp_print_containing_files(pfile);
7277    cpp_print_file_and_line(pfile);
7278    cpp_message_v(pfile, 0, msg, args);
7279 }
7280
7281 void
7282 cpp_warning(cpp_reader * pfile, const char *msg, ...)
7283 {
7284    va_list             args;
7285
7286    va_start(args, msg);
7287
7288    cpp_warning_v(pfile, msg, args);
7289
7290    va_end(args);
7291 }
7292
7293 /* Print an error message and maybe count it.  */
7294
7295 void
7296 cpp_pedwarn(cpp_reader * pfile, const char *msg, ...)
7297 {
7298    va_list             args;
7299
7300    va_start(args, msg);
7301
7302    if (CPP_OPTIONS(pfile)->pedantic_errors)
7303       cpp_error_v(pfile, msg, args);
7304    else
7305       cpp_warning_v(pfile, msg, args);
7306
7307    va_end(args);
7308 }
7309
7310 static void
7311 cpp_error_with_line(cpp_reader * pfile, int line, int column, const char *msg)
7312 {
7313    cpp_buffer         *ip = cpp_file_buffer(pfile);
7314
7315    cpp_print_containing_files(pfile);
7316
7317    if (ip)
7318       cpp_file_line_for_message(pfile, ip->nominal_fname, line, column);
7319
7320    cpp_message(pfile, 1, msg, NULL, NULL, NULL);
7321 }
7322
7323 static void
7324 cpp_warning_with_line(cpp_reader * pfile, int line, int column, const char *msg)
7325 {
7326    cpp_buffer         *ip;
7327
7328    if (CPP_OPTIONS(pfile)->inhibit_warnings)
7329       return;
7330
7331    if (CPP_OPTIONS(pfile)->warnings_are_errors)
7332       pfile->errors++;
7333
7334    cpp_print_containing_files(pfile);
7335
7336    ip = cpp_file_buffer(pfile);
7337
7338    if (ip)
7339       cpp_file_line_for_message(pfile, ip->nominal_fname, line, column);
7340
7341    cpp_message(pfile, 0, msg, NULL, NULL, NULL);
7342 }
7343
7344 static void
7345 cpp_pedwarn_with_line(cpp_reader * pfile, int line, int column, const char *msg)
7346 {
7347    if (CPP_OPTIONS(pfile)->pedantic_errors)
7348       cpp_error_with_line(pfile, column, line, msg);
7349    else
7350       cpp_warning_with_line(pfile, line, column, msg);
7351 }
7352
7353 /* Report a warning (or an error if pedantic_errors)
7354  * giving specified file name and line number, not current.  */
7355
7356 void
7357 cpp_pedwarn_with_file_and_line(cpp_reader * pfile,
7358                                const char *file, int line,
7359                                const char *msg, const char *arg1,
7360                                const char *arg2, const char *arg3)
7361 {
7362    if (!CPP_OPTIONS(pfile)->pedantic_errors
7363        && CPP_OPTIONS(pfile)->inhibit_warnings)
7364       return;
7365    if (file)
7366       cpp_file_line_for_message(pfile, file, line, -1);
7367    cpp_message(pfile, CPP_OPTIONS(pfile)->pedantic_errors,
7368                msg, arg1, arg2, arg3);
7369 }
7370
7371 /* This defines "errno" properly for VMS, and gives us EACCES. */
7372 #include <errno.h>
7373 #ifndef errno
7374 extern int          errno;
7375
7376 #endif
7377
7378 #ifndef HAVE_STRERROR
7379 extern int          sys_nerr;
7380
7381 #if defined(bsd4_4)
7382 extern const char  *const sys_errlist[];
7383
7384 #else
7385 extern char        *sys_errlist[];
7386
7387 #endif
7388 #endif /* HAVE_STRERROR */
7389
7390 /*
7391  * my_strerror - return the descriptive text associated with an `errno' code.
7392  */
7393
7394 static const char  *
7395 my_strerror(int errnum)
7396 {
7397    const char         *result;
7398
7399 #ifndef HAVE_STRERROR
7400    result = ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7401 #else
7402    result = strerror(errnum);
7403 #endif
7404
7405    if (!result)
7406       result = "undocumented I/O error";
7407
7408    return result;
7409 }
7410
7411 /* Error including a message from `errno'.  */
7412
7413 static void
7414 cpp_error_from_errno(cpp_reader * pfile, const char *name)
7415 {
7416    cpp_buffer         *ip = cpp_file_buffer(pfile);
7417
7418    cpp_print_containing_files(pfile);
7419
7420    if (ip)
7421       cpp_file_line_for_message(pfile, ip->nominal_fname, ip->lineno, -1);
7422
7423    cpp_message(pfile, 1, "%s: %s", name, my_strerror(errno), NULL);
7424 }
7425
7426 void
7427 cpp_perror_with_name(cpp_reader * pfile, const char *name)
7428 {
7429    cpp_message(pfile, 1, "%s: %s: %s", progname, name, my_strerror(errno));
7430 }
7431
7432 /* TODO:
7433  * No pre-compiled header file support.
7434  *
7435  * Possibly different enum token codes for each C/C++ token.
7436  *
7437  * Should clean up remaining directives to that do_XXX functions
7438  *   only take two arguments and all have command_reads_line.
7439  *
7440  * Find and cleanup remaining uses of static variables,
7441  *
7442  * Support for trigraphs.
7443  *
7444  * Support -dM flag (dump_all_macros).
7445  *
7446  * Support for_lint flag.
7447  */
7448
7449 extern cpp_options         options;
7450
7451 void
7452 using_file(const char *filename)
7453 {
7454    FILE *f;
7455
7456    f = fopen(options.watchfile, "a");
7457    if (!f) return ;
7458    fputs(filename, f);
7459    fputc('\n', f);
7460    fclose(f);
7461 }