Do not report -Wnested-extern errors for __FUNCTION__/__PRETTY_FUNCTION__.
[official-gcc.git] / gcc / cccp.c
blob02035e4bb1c9c2ca0a2bf9f685b977c8f5d057ed
1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 1987, 1989, 1992 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding! */
24 typedef unsigned char U_CHAR;
26 #ifdef EMACS
27 #define NO_SHORTNAMES
28 #include "../src/config.h"
29 #ifdef open
30 #undef open
31 #undef read
32 #undef write
33 #endif /* open */
34 #endif /* EMACS */
36 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
37 for the sake of machines with limited C compilers. */
38 #ifndef EMACS
39 #include "config.h"
40 #endif /* not EMACS */
42 #ifndef STANDARD_INCLUDE_DIR
43 #define STANDARD_INCLUDE_DIR "/usr/include"
44 #endif
46 #ifndef LOCAL_INCLUDE_DIR
47 #define LOCAL_INCLUDE_DIR "/usr/local/include"
48 #endif
50 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
51 #ifdef __STDC__
52 #define PTR_INT_TYPE ptrdiff_t
53 #else
54 #define PTR_INT_TYPE long
55 #endif
56 #endif /* 0 */
58 #include "pcp.h"
60 #ifndef STDC_VALUE
61 #define STDC_VALUE 1
62 #endif
64 /* By default, colon separates directories in a path. */
65 #ifndef PATH_SEPARATOR
66 #define PATH_SEPARATOR ':'
67 #endif
69 /* In case config.h defines these. */
70 #undef bcopy
71 #undef bzero
72 #undef bcmp
74 #include <sys/types.h>
75 #include <sys/stat.h>
76 #include <ctype.h>
77 #include <stdio.h>
78 #include <signal.h>
80 #ifndef VMS
81 #ifndef USG
82 #include <sys/time.h> /* for __DATE__ and __TIME__ */
83 #include <sys/resource.h>
84 #else
85 #include <time.h>
86 #include <fcntl.h>
87 #endif /* USG */
88 #endif /* not VMS */
90 /* This defines "errno" properly for VMS, and gives us EACCES. */
91 #include <errno.h>
93 /* VMS-specific definitions */
94 #ifdef VMS
95 #include <time.h>
96 #include <perror.h> /* This defines sys_errlist/sys_nerr properly */
97 #include <descrip.h>
98 #define O_RDONLY 0 /* Open arg for Read/Only */
99 #define O_WRONLY 1 /* Open arg for Write/Only */
100 #define read(fd,buf,size) VMS_read (fd,buf,size)
101 #define write(fd,buf,size) VMS_write (fd,buf,size)
102 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
103 #define fopen(fname,mode) VMS_fopen (fname,mode)
104 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
105 #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
106 static char * VMS_strncat ();
107 static int VMS_read ();
108 static int VMS_write ();
109 static int VMS_open ();
110 static FILE * VMS_fopen ();
111 static FILE * VMS_freopen ();
112 static void hack_vms_include_specification ();
113 typedef struct { unsigned :16, :16, :16; } vms_ino_t;
114 #define ino_t vms_ino_t
115 #define INCLUDE_LEN_FUDGE 10 /* leave room for VMS syntax conversion */
116 #ifdef __GNUC__
117 #define BSTRING /* VMS/GCC supplies the bstring routines */
118 #endif /* __GNUC__ */
119 #endif /* VMS */
121 extern char *index ();
122 extern char *rindex ();
124 #ifndef O_RDONLY
125 #define O_RDONLY 0
126 #endif
128 #undef MIN
129 #undef MAX
130 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
131 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
133 /* Find the largest host integer type and set its size and type. */
135 #ifndef HOST_BITS_PER_WIDE_INT
137 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
138 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
139 #define HOST_WIDE_INT long
140 #else
141 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
142 #define HOST_WIDE_INT int
143 #endif
145 #endif
147 #ifndef S_ISREG
148 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
149 #endif
151 #ifndef S_ISDIR
152 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
153 #endif
155 /* Define a generic NULL if one hasn't already been defined. */
157 #ifndef NULL
158 #define NULL 0
159 #endif
161 #ifndef GENERIC_PTR
162 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
163 #define GENERIC_PTR void *
164 #else
165 #define GENERIC_PTR char *
166 #endif
167 #endif
169 #ifndef NULL_PTR
170 #define NULL_PTR ((GENERIC_PTR)0)
171 #endif
173 #ifndef INCLUDE_LEN_FUDGE
174 #define INCLUDE_LEN_FUDGE 0
175 #endif
177 /* Forward declarations. */
179 char *xmalloc ();
180 void error ();
181 void warning ();
183 /* External declarations. */
185 extern char *getenv ();
186 extern FILE *fdopen ();
187 extern char *version_string;
188 extern struct tm *localtime ();
189 extern int sys_nerr;
190 extern char *sys_errlist[];
192 #ifndef errno
193 extern int errno;
194 #endif
196 /* Forward declarations. */
198 struct directive;
199 struct file_buf;
200 struct arglist;
201 struct argdata;
203 #if defined(USG) || defined(VMS)
204 #ifndef BSTRING
205 void bcopy ();
206 void bzero ();
207 int bcmp ();
208 #endif
209 #endif
211 /* These functions are declared to return int instead of void since they
212 are going to be placed in a table and some old compilers have trouble with
213 pointers to functions returning void. */
215 static int do_define ();
216 static int do_line ();
217 static int do_include ();
218 static int do_undef ();
219 static int do_error ();
220 static int do_pragma ();
221 static int do_ident ();
222 static int do_if ();
223 static int do_xifdef ();
224 static int do_else ();
225 static int do_elif ();
226 static int do_endif ();
227 static int do_sccs ();
228 static int do_once ();
229 static int do_assert ();
230 static int do_unassert ();
231 static int do_warning ();
233 static void add_import ();
234 static void append_include_chain ();
235 static void deps_output ();
236 static void make_undef ();
237 static void make_definition ();
238 static void make_assertion ();
239 static void path_include ();
240 static void initialize_builtins ();
241 static void initialize_char_syntax ();
242 static void dump_arg_n ();
243 static void dump_defn_1 ();
244 static void delete_macro ();
245 static void trigraph_pcp ();
246 static void rescan ();
247 static void finclude ();
248 static void validate_else ();
249 static int comp_def_part ();
250 static void error_from_errno ();
251 static void error_with_line ();
252 void pedwarn ();
253 void pedwarn_with_line ();
254 static void pedwarn_with_file_and_line ();
255 static void fatal ();
256 void fancy_abort ();
257 static void pfatal_with_name ();
258 static void perror_with_name ();
259 static void pipe_closed ();
260 static void print_containing_files ();
261 static int lookup_import ();
262 static int redundant_include_p ();
263 static is_system_include ();
264 static int check_preconditions ();
265 static void pcfinclude ();
266 static void pcstring_used ();
267 static void write_output ();
268 static int check_macro_name ();
269 static int compare_defs ();
270 static int compare_token_lists ();
271 static int eval_if_expression ();
272 static int discard_comments ();
273 static int change_newlines ();
274 static int line_for_error ();
275 static int hashf ();
276 static int file_size_and_mode ();
278 static struct arglist *read_token_list ();
279 static void free_token_list ();
281 static struct hashnode *install ();
282 struct hashnode *lookup ();
284 static struct assertion_hashnode *assertion_install ();
285 static struct assertion_hashnode *assertion_lookup ();
287 static char *xrealloc ();
288 static char *xcalloc ();
289 static char *savestring ();
291 static void delete_assertion ();
292 static void macroexpand ();
293 static void dump_all_macros ();
294 static void conditional_skip ();
295 static void skip_if_group ();
296 static void output_line_command ();
298 /* Last arg to output_line_command. */
299 enum file_change_code {same_file, enter_file, leave_file};
301 static int grow_outbuf ();
302 static int handle_directive ();
303 static void memory_full ();
305 static U_CHAR *macarg1 ();
306 static char *macarg ();
308 static U_CHAR *skip_to_end_of_comment ();
309 static U_CHAR *skip_quoted_string ();
310 static U_CHAR *skip_paren_group ();
312 static char *check_precompiled ();
313 /* static struct macrodef create_definition (); [moved below] */
314 static void dump_single_macro ();
316 #ifndef FAILURE_EXIT_CODE
317 #define FAILURE_EXIT_CODE 33 /* gnu cc command understands this */
318 #endif
320 #ifndef SUCCESS_EXIT_CODE
321 #define SUCCESS_EXIT_CODE 0 /* 0 means success on Unix. */
322 #endif
324 /* Name under which this program was invoked. */
326 static char *progname;
328 /* Nonzero means use extra default include directories for C++. */
330 static int cplusplus;
332 /* Nonzero means handle cplusplus style comments */
334 static int cplusplus_comments;
336 /* Nonzero means handle #import, for objective C. */
338 static int objc;
340 /* Nonzero means this is an assembly file, and allow
341 unknown directives, which could be comments. */
343 static int lang_asm;
345 /* Current maximum length of directory names in the search path
346 for include files. (Altered as we get more of them.) */
348 static int max_include_len;
350 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
352 static int lint = 0;
354 /* Nonzero means copy comments into the output file. */
356 static int put_out_comments = 0;
358 /* Nonzero means don't process the ANSI trigraph sequences. */
360 static int no_trigraphs = 0;
362 /* Nonzero means print the names of included files rather than
363 the preprocessed output. 1 means just the #include "...",
364 2 means #include <...> as well. */
366 static int print_deps = 0;
368 /* Nonzero means print names of header files (-H). */
370 static int print_include_names = 0;
372 /* Nonzero means don't output line number information. */
374 static int no_line_commands;
376 /* dump_only means inhibit output of the preprocessed text
377 and instead output the definitions of all user-defined
378 macros in a form suitable for use as input to cccp.
379 dump_names means pass #define and the macro name through to output.
380 dump_definitions means pass the whole definition (plus #define) through
383 static enum {dump_none, dump_only, dump_names, dump_definitions}
384 dump_macros = dump_none;
386 /* Nonzero means pass all #define and #undef directives which we actually
387 process through to the output stream. This feature is used primarily
388 to allow cc1 to record the #defines and #undefs for the sake of
389 debuggers which understand about preprocessor macros, but it may
390 also be useful with -E to figure out how symbols are defined, and
391 where they are defined. */
392 static int debug_output = 0;
394 /* Nonzero indicates special processing used by the pcp program. The
395 special effects of this mode are:
397 Inhibit all macro expansion, except those inside #if directives.
399 Process #define directives normally, and output their contents
400 to the output file.
402 Output preconditions to pcp_outfile indicating all the relevant
403 preconditions for use of this file in a later cpp run.
405 static FILE *pcp_outfile;
407 /* Nonzero means we are inside an IF during a -pcp run. In this mode
408 macro expansion is done, and preconditions are output for all macro
409 uses requiring them. */
410 static int pcp_inside_if;
412 /* Nonzero means never to include precompiled files.
413 This is 1 since there's no way now to make precompiled files,
414 so it's not worth testing for them. */
415 static int no_precomp = 1;
417 /* Nonzero means give all the error messages the ANSI standard requires. */
419 int pedantic;
421 /* Nonzero means try to make failure to fit ANSI C an error. */
423 static int pedantic_errors;
425 /* Nonzero means don't print warning messages. -w. */
427 static int inhibit_warnings = 0;
429 /* Nonzero means warn if slash-star appears in a comment. */
431 static int warn_comments;
433 /* Nonzero means warn if a macro argument is (or would be)
434 stringified with -traditional. */
436 static int warn_stringify;
438 /* Nonzero means warn if there are any trigraphs. */
440 static int warn_trigraphs;
442 /* Nonzero means warn if #import is used. */
444 static int warn_import = 1;
446 /* Nonzero means turn warnings into errors. */
448 static int warnings_are_errors;
450 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
452 int traditional;
454 /* Nonzero causes output not to be done,
455 but directives such as #define that have side effects
456 are still obeyed. */
458 static int no_output;
460 /* Nonzero means that we have finished processing the command line options.
461 This flag is used to decide whether or not to issue certain errors
462 and/or warnings. */
464 static int done_initializing = 0;
466 /* Line where a newline was first seen in a string constant. */
468 static int multiline_string_line = 0;
470 /* I/O buffer structure.
471 The `fname' field is nonzero for source files and #include files
472 and for the dummy text used for -D and -U.
473 It is zero for rescanning results of macro expansion
474 and for expanding macro arguments. */
475 #define INPUT_STACK_MAX 200
476 static struct file_buf {
477 char *fname;
478 /* Filename specified with #line command. */
479 char *nominal_fname;
480 /* Record where in the search path this file was found.
481 For #include_next. */
482 struct file_name_list *dir;
483 int lineno;
484 int length;
485 U_CHAR *buf;
486 U_CHAR *bufp;
487 /* Macro that this level is the expansion of.
488 Included so that we can reenable the macro
489 at the end of this level. */
490 struct hashnode *macro;
491 /* Value of if_stack at start of this file.
492 Used to prohibit unmatched #endif (etc) in an include file. */
493 struct if_stack *if_stack;
494 /* Object to be freed at end of input at this level. */
495 U_CHAR *free_ptr;
496 /* True if this is a header file included using <FILENAME>. */
497 char system_header_p;
498 } instack[INPUT_STACK_MAX];
500 static int last_error_tick; /* Incremented each time we print it. */
501 static int input_file_stack_tick; /* Incremented when the status changes. */
503 /* Current nesting level of input sources.
504 `instack[indepth]' is the level currently being read. */
505 static int indepth = -1;
506 #define CHECK_DEPTH(code) \
507 if (indepth >= (INPUT_STACK_MAX - 1)) \
509 error_with_line (line_for_error (instack[indepth].lineno), \
510 "macro or `#include' recursion too deep"); \
511 code; \
514 /* Current depth in #include directives that use <...>. */
515 static int system_include_depth = 0;
517 typedef struct file_buf FILE_BUF;
519 /* The output buffer. Its LENGTH field is the amount of room allocated
520 for the buffer, not the number of chars actually present. To get
521 that, subtract outbuf.buf from outbuf.bufp. */
523 #define OUTBUF_SIZE 10 /* initial size of output buffer */
524 static FILE_BUF outbuf;
526 /* Grow output buffer OBUF points at
527 so it can hold at least NEEDED more chars. */
529 #define check_expand(OBUF, NEEDED) \
530 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
531 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
533 struct file_name_list
535 struct file_name_list *next;
536 char *fname;
537 /* If the following is nonzero, it is a macro name.
538 Don't include the file again if that macro is defined. */
539 U_CHAR *control_macro;
542 /* #include "file" looks in source file dir, then stack. */
543 /* #include <file> just looks in the stack. */
544 /* -I directories are added to the end, then the defaults are added. */
545 static struct default_include { char *fname; int cplusplus; } include_defaults_array[]
546 #ifdef INCLUDE_DEFAULTS
547 = INCLUDE_DEFAULTS;
548 #else
550 /* Pick up GNU C++ specific include files. */
551 { GPLUSPLUS_INCLUDE_DIR, 1},
552 #ifdef CROSS_COMPILE
553 /* This is the dir for fixincludes. Put it just before
554 the files that we fix. */
555 { GCC_INCLUDE_DIR, 0},
556 /* For cross-compilation, this dir name is generated
557 automatically in Makefile.in. */
558 { CROSS_INCLUDE_DIR, 0 },
559 /* This is another place that the target system's headers might be. */
560 { TOOL_INCLUDE_DIR, 0},
561 #else /* not CROSS_COMPILE */
562 /* This should be /use/local/include and should come before
563 the fixincludes-fixed header files. */
564 { LOCAL_INCLUDE_DIR, 0},
565 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
566 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
567 { TOOL_INCLUDE_DIR, 0},
568 /* This is the dir for fixincludes. Put it just before
569 the files that we fix. */
570 { GCC_INCLUDE_DIR, 0},
571 /* Some systems have an extra dir of include files. */
572 #ifdef SYSTEM_INCLUDE_DIR
573 { SYSTEM_INCLUDE_DIR, 0},
574 #endif
575 { STANDARD_INCLUDE_DIR, 0},
576 #endif /* not CROSS_COMPILE */
577 { 0, 0}
579 #endif /* no INCLUDE_DEFAULTS */
581 /* The code looks at the defaults through this pointer, rather than through
582 the constant structure above. This pointer gets changed if an environment
583 variable specifies other defaults. */
584 static struct default_include *include_defaults = include_defaults_array;
586 static struct file_name_list *include = 0; /* First dir to search */
587 /* First dir to search for <file> */
588 /* This is the first element to use for #include <...>.
589 If it is 0, use the entire chain for such includes. */
590 static struct file_name_list *first_bracket_include = 0;
591 /* This is the first element in the chain that corresponds to
592 a directory of system header files. */
593 static struct file_name_list *first_system_include = 0;
594 static struct file_name_list *last_include = 0; /* Last in chain */
596 /* Chain of include directories to put at the end of the other chain. */
597 static struct file_name_list *after_include = 0;
598 static struct file_name_list *last_after_include = 0; /* Last in chain */
600 /* List of included files that contained #pragma once. */
601 static struct file_name_list *dont_repeat_files = 0;
603 /* List of other included files.
604 If ->control_macro if nonzero, the file had a #ifndef
605 around the entire contents, and ->control_macro gives the macro name. */
606 static struct file_name_list *all_include_files = 0;
608 /* Directory prefix that should replace `/usr' in the standard
609 include file directories. */
610 static char *include_prefix;
612 /* Global list of strings read in from precompiled files. This list
613 is kept in the order the strings are read in, with new strings being
614 added at the end through stringlist_tailp. We use this list to output
615 the strings at the end of the run.
617 static STRINGDEF *stringlist;
618 static STRINGDEF **stringlist_tailp = &stringlist;
621 /* Structure returned by create_definition */
622 typedef struct macrodef MACRODEF;
623 struct macrodef
625 struct definition *defn;
626 U_CHAR *symnam;
627 int symlen;
630 static struct macrodef create_definition ();
633 /* Structure allocated for every #define. For a simple replacement
634 such as
635 #define foo bar ,
636 nargs = -1, the `pattern' list is null, and the expansion is just
637 the replacement text. Nargs = 0 means a functionlike macro with no args,
638 e.g.,
639 #define getchar() getc (stdin) .
640 When there are args, the expansion is the replacement text with the
641 args squashed out, and the reflist is a list describing how to
642 build the output from the input: e.g., "3 chars, then the 1st arg,
643 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
644 The chars here come from the expansion. Whatever is left of the
645 expansion after the last arg-occurrence is copied after that arg.
646 Note that the reflist can be arbitrarily long---
647 its length depends on the number of times the arguments appear in
648 the replacement text, not how many args there are. Example:
649 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
650 pattern list
651 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
652 where (x, y) means (nchars, argno). */
654 typedef struct definition DEFINITION;
655 struct definition {
656 int nargs;
657 int length; /* length of expansion string */
658 int predefined; /* True if the macro was builtin or */
659 /* came from the command line */
660 U_CHAR *expansion;
661 int line; /* Line number of definition */
662 char *file; /* File of definition */
663 char rest_args; /* Nonzero if last arg. absorbs the rest */
664 struct reflist {
665 struct reflist *next;
666 char stringify; /* nonzero if this arg was preceded by a
667 # operator. */
668 char raw_before; /* Nonzero if a ## operator before arg. */
669 char raw_after; /* Nonzero if a ## operator after arg. */
670 char rest_args; /* Nonzero if this arg. absorbs the rest */
671 int nchars; /* Number of literal chars to copy before
672 this arg occurrence. */
673 int argno; /* Number of arg to substitute (origin-0) */
674 } *pattern;
675 union {
676 /* Names of macro args, concatenated in reverse order
677 with comma-space between them.
678 The only use of this is that we warn on redefinition
679 if this differs between the old and new definitions. */
680 U_CHAR *argnames;
681 } args;
684 /* different kinds of things that can appear in the value field
685 of a hash node. Actually, this may be useless now. */
686 union hashval {
687 int ival;
688 char *cpval;
689 DEFINITION *defn;
690 KEYDEF *keydef;
694 * special extension string that can be added to the last macro argument to
695 * allow it to absorb the "rest" of the arguments when expanded. Ex:
696 * #define wow(a, b...) process (b, a, b)
697 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
698 * { wow (one, two); } -> { process (two, one, two); }
699 * if this "rest_arg" is used with the concat token '##' and if it is not
700 * supplied then the token attached to with ## will not be outputted. Ex:
701 * #define wow (a, b...) process (b ## , a, ## b)
702 * { wow (1, 2); } -> { process (2, 1, 2); }
703 * { wow (one); } -> { process (one); {
705 static char rest_extension[] = "...";
706 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
708 /* The structure of a node in the hash table. The hash table
709 has entries for all tokens defined by #define commands (type T_MACRO),
710 plus some special tokens like __LINE__ (these each have their own
711 type, and the appropriate code is run when that type of node is seen.
712 It does not contain control words like "#define", which are recognized
713 by a separate piece of code. */
715 /* different flavors of hash nodes --- also used in keyword table */
716 enum node_type {
717 T_DEFINE = 1, /* the `#define' keyword */
718 T_INCLUDE, /* the `#include' keyword */
719 T_INCLUDE_NEXT, /* the `#include_next' keyword */
720 T_IMPORT, /* the `#import' keyword */
721 T_IFDEF, /* the `#ifdef' keyword */
722 T_IFNDEF, /* the `#ifndef' keyword */
723 T_IF, /* the `#if' keyword */
724 T_ELSE, /* `#else' */
725 T_PRAGMA, /* `#pragma' */
726 T_ELIF, /* `#elif' */
727 T_UNDEF, /* `#undef' */
728 T_LINE, /* `#line' */
729 T_ERROR, /* `#error' */
730 T_WARNING, /* `#warning' */
731 T_ENDIF, /* `#endif' */
732 T_SCCS, /* `#sccs', used on system V. */
733 T_IDENT, /* `#ident', used on system V. */
734 T_ASSERT, /* `#assert', taken from system V. */
735 T_UNASSERT, /* `#unassert', taken from system V. */
736 T_SPECLINE, /* special symbol `__LINE__' */
737 T_DATE, /* `__DATE__' */
738 T_FILE, /* `__FILE__' */
739 T_BASE_FILE, /* `__BASE_FILE__' */
740 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
741 T_VERSION, /* `__VERSION__' */
742 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
743 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
744 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
745 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
746 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
747 T_TIME, /* `__TIME__' */
748 T_CONST, /* Constant value, used by `__STDC__' */
749 T_MACRO, /* macro defined by `#define' */
750 T_DISABLED, /* macro temporarily turned off for rescan */
751 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
752 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
753 T_UNUSED /* Used for something not defined. */
756 struct hashnode {
757 struct hashnode *next; /* double links for easy deletion */
758 struct hashnode *prev;
759 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
760 chain is kept, in case the node is the head
761 of the chain and gets deleted. */
762 enum node_type type; /* type of special token */
763 int length; /* length of token, for quick comparison */
764 U_CHAR *name; /* the actual name */
765 union hashval value; /* pointer to expansion, or whatever */
768 typedef struct hashnode HASHNODE;
770 /* Some definitions for the hash table. The hash function MUST be
771 computed as shown in hashf () below. That is because the rescan
772 loop computes the hash value `on the fly' for most tokens,
773 in order to avoid the overhead of a lot of procedure calls to
774 the hashf () function. Hashf () only exists for the sake of
775 politeness, for use when speed isn't so important. */
777 #define HASHSIZE 1403
778 static HASHNODE *hashtab[HASHSIZE];
779 #define HASHSTEP(old, c) ((old << 2) + c)
780 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
782 /* Symbols to predefine. */
784 #ifdef CPP_PREDEFINES
785 static char *predefs = CPP_PREDEFINES;
786 #else
787 static char *predefs = "";
788 #endif
790 /* We let tm.h override the types used here, to handle trivial differences
791 such as the choice of unsigned int or long unsigned int for size_t.
792 When machines start needing nontrivial differences in the size type,
793 it would be best to do something here to figure out automatically
794 from other information what type to use. */
796 /* The string value for __size_type__. */
798 #ifndef SIZE_TYPE
799 #define SIZE_TYPE "long unsigned int"
800 #endif
802 /* The string value for __ptrdiff_type__. */
804 #ifndef PTRDIFF_TYPE
805 #define PTRDIFF_TYPE "long int"
806 #endif
808 /* The string value for __wchar_type__. */
810 #ifndef WCHAR_TYPE
811 #define WCHAR_TYPE "int"
812 #endif
814 /* The string value for __USER_LABEL_PREFIX__ */
816 #ifndef USER_LABEL_PREFIX
817 #define USER_LABEL_PREFIX ""
818 #endif
820 /* The string value for __REGISTER_PREFIX__ */
822 #ifndef REGISTER_PREFIX
823 #define REGISTER_PREFIX ""
824 #endif
826 /* In the definition of a #assert name, this structure forms
827 a list of the individual values asserted.
828 Each value is itself a list of "tokens".
829 These are strings that are compared by name. */
831 struct tokenlist_list {
832 struct tokenlist_list *next;
833 struct arglist *tokens;
836 struct assertion_hashnode {
837 struct assertion_hashnode *next; /* double links for easy deletion */
838 struct assertion_hashnode *prev;
839 /* also, a back pointer to this node's hash
840 chain is kept, in case the node is the head
841 of the chain and gets deleted. */
842 struct assertion_hashnode **bucket_hdr;
843 int length; /* length of token, for quick comparison */
844 U_CHAR *name; /* the actual name */
845 /* List of token-sequences. */
846 struct tokenlist_list *value;
849 typedef struct assertion_hashnode ASSERTION_HASHNODE;
851 /* Some definitions for the hash table. The hash function MUST be
852 computed as shown in hashf below. That is because the rescan
853 loop computes the hash value `on the fly' for most tokens,
854 in order to avoid the overhead of a lot of procedure calls to
855 the hashf function. hashf only exists for the sake of
856 politeness, for use when speed isn't so important. */
858 #define ASSERTION_HASHSIZE 37
859 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
861 /* Nonzero means inhibit macroexpansion of what seem to be
862 assertion tests, in rescan. For #if. */
863 static int assertions_flag;
865 /* `struct directive' defines one #-directive, including how to handle it. */
867 struct directive {
868 int length; /* Length of name */
869 int (*func)(); /* Function to handle directive */
870 char *name; /* Name of directive */
871 enum node_type type; /* Code which describes which directive. */
872 char angle_brackets; /* Nonzero => <...> is special. */
873 char traditional_comments; /* Nonzero: keep comments if -traditional. */
874 char pass_thru; /* Copy preprocessed directive to output file. */
877 /* Here is the actual list of #-directives, most-often-used first. */
879 static struct directive directive_table[] = {
880 { 6, do_define, "define", T_DEFINE, 0, 1},
881 { 2, do_if, "if", T_IF},
882 { 5, do_xifdef, "ifdef", T_IFDEF},
883 { 6, do_xifdef, "ifndef", T_IFNDEF},
884 { 5, do_endif, "endif", T_ENDIF},
885 { 4, do_else, "else", T_ELSE},
886 { 4, do_elif, "elif", T_ELIF},
887 { 4, do_line, "line", T_LINE},
888 { 7, do_include, "include", T_INCLUDE, 1},
889 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
890 { 6, do_include, "import", T_IMPORT, 1},
891 { 5, do_undef, "undef", T_UNDEF},
892 { 5, do_error, "error", T_ERROR},
893 { 7, do_warning, "warning", T_WARNING},
894 #ifdef SCCS_DIRECTIVE
895 { 4, do_sccs, "sccs", T_SCCS},
896 #endif
897 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
898 { 5, do_ident, "ident", T_IDENT, 0, 0, 1},
899 { 6, do_assert, "assert", T_ASSERT},
900 { 8, do_unassert, "unassert", T_UNASSERT},
901 { -1, 0, "", T_UNUSED},
904 /* When a directive handler is called,
905 this points to the # that started the directive. */
906 U_CHAR *directive_start;
908 /* table to tell if char can be part of a C identifier. */
909 U_CHAR is_idchar[256];
910 /* table to tell if char can be first char of a c identifier. */
911 U_CHAR is_idstart[256];
912 /* table to tell if c is horizontal space. */
913 U_CHAR is_hor_space[256];
914 /* table to tell if c is horizontal or vertical space. */
915 static U_CHAR is_space[256];
917 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
918 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
920 static int errors = 0; /* Error counter for exit code */
922 /* Name of output file, for error messages. */
923 static char *out_fname;
925 /* Zero means dollar signs are punctuation.
926 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
927 This must be 0 for correct processing of this ANSI C program:
928 #define foo(a) #a
929 #define lose(b) foo (b)
930 #define test$
931 lose (test) */
932 static int dollars_in_ident;
933 #ifndef DOLLARS_IN_IDENTIFIERS
934 #define DOLLARS_IN_IDENTIFIERS 1
935 #endif
937 static FILE_BUF expand_to_temp_buffer ();
939 static DEFINITION *collect_expansion ();
941 /* Stack of conditionals currently in progress
942 (including both successful and failing conditionals). */
944 struct if_stack {
945 struct if_stack *next; /* for chaining to the next stack frame */
946 char *fname; /* copied from input when frame is made */
947 int lineno; /* similarly */
948 int if_succeeded; /* true if a leg of this if-group
949 has been passed through rescan */
950 U_CHAR *control_macro; /* For #ifndef at start of file,
951 this is the macro name tested. */
952 enum node_type type; /* type of last directive seen in this group */
954 typedef struct if_stack IF_STACK_FRAME;
955 static IF_STACK_FRAME *if_stack = NULL;
957 /* Buffer of -M output. */
958 static char *deps_buffer;
960 /* Number of bytes allocated in above. */
961 static int deps_allocated_size;
963 /* Number of bytes used. */
964 static int deps_size;
966 /* Number of bytes since the last newline. */
967 static int deps_column;
969 /* Nonzero means -I- has been seen,
970 so don't look for #include "foo" the source-file directory. */
971 static int ignore_srcdir;
974 main (argc, argv)
975 int argc;
976 char **argv;
978 int st_mode;
979 long st_size;
980 char *in_fname;
981 char *p;
982 int f, i;
983 FILE_BUF *fp;
984 char **pend_files = (char **) xmalloc (argc * sizeof (char *));
985 char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
986 char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
987 char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
988 char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
990 /* Record the option used with each element of pend_assertions.
991 This is preparation for supporting more than one option for making
992 an assertion. */
993 char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
994 int inhibit_predefs = 0;
995 int no_standard_includes = 0;
996 int no_standard_cplusplus_includes = 0;
997 int missing_newline = 0;
999 /* Non-0 means don't output the preprocessed program. */
1000 int inhibit_output = 0;
1001 /* Non-0 means -v, so print the full set of include dirs. */
1002 int verbose = 0;
1004 /* File name which deps are being written to.
1005 This is 0 if deps are being written to stdout. */
1006 char *deps_file = 0;
1007 /* Fopen file mode to open deps_file with. */
1008 char *deps_mode = "a";
1009 /* Stream on which to print the dependency information. */
1010 FILE *deps_stream = 0;
1011 /* Target-name to write with the dependency information. */
1012 char *deps_target = 0;
1014 #ifdef RLIMIT_STACK
1015 /* Get rid of any avoidable limit on stack size. */
1017 struct rlimit rlim;
1019 /* Set the stack limit huge so that alloca (particularly stringtab
1020 * in dbxread.c) does not fail. */
1021 getrlimit (RLIMIT_STACK, &rlim);
1022 rlim.rlim_cur = rlim.rlim_max;
1023 setrlimit (RLIMIT_STACK, &rlim);
1025 #endif /* RLIMIT_STACK defined */
1027 signal (SIGPIPE, pipe_closed);
1029 p = argv[0] + strlen (argv[0]);
1030 while (p != argv[0] && p[-1] != '/') --p;
1031 progname = p;
1033 #ifdef VMS
1035 /* Remove directories from PROGNAME. */
1036 char *s;
1038 progname = savestring (argv[0]);
1040 if (!(s = rindex (progname, ']')))
1041 s = rindex (progname, ':');
1042 if (s)
1043 strcpy (progname, s+1);
1044 if (s = rindex (progname, '.'))
1045 *s = '\0';
1047 #endif
1049 in_fname = NULL;
1050 out_fname = NULL;
1052 /* Initialize is_idchar to allow $. */
1053 dollars_in_ident = 1;
1054 initialize_char_syntax ();
1055 dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
1057 no_line_commands = 0;
1058 no_trigraphs = 1;
1059 dump_macros = dump_none;
1060 no_output = 0;
1061 cplusplus = 0;
1062 cplusplus_comments = 0;
1064 bzero (pend_files, argc * sizeof (char *));
1065 bzero (pend_defs, argc * sizeof (char *));
1066 bzero (pend_undefs, argc * sizeof (char *));
1067 bzero (pend_assertions, argc * sizeof (char *));
1068 bzero (pend_includes, argc * sizeof (char *));
1070 /* Process switches and find input file name. */
1072 for (i = 1; i < argc; i++) {
1073 if (argv[i][0] != '-') {
1074 if (out_fname != NULL)
1075 fatal ("Usage: %s [switches] input output", argv[0]);
1076 else if (in_fname != NULL)
1077 out_fname = argv[i];
1078 else
1079 in_fname = argv[i];
1080 } else {
1081 switch (argv[i][1]) {
1083 case 'i':
1084 if (!strcmp (argv[i], "-include")) {
1085 if (i + 1 == argc)
1086 fatal ("Filename missing after `-include' option");
1087 else
1088 pend_includes[i] = argv[i+1], i++;
1090 if (!strcmp (argv[i], "-imacros")) {
1091 if (i + 1 == argc)
1092 fatal ("Filename missing after `-imacros' option");
1093 else
1094 pend_files[i] = argv[i+1], i++;
1096 if (!strcmp (argv[i], "-iprefix")) {
1097 if (i + 1 == argc)
1098 fatal ("Filename missing after `-iprefix' option");
1099 else
1100 include_prefix = argv[++i];
1102 /* Add directory to end of path for includes,
1103 with the default prefix at the front of its name. */
1104 if (!strcmp (argv[i], "-iwithprefix")) {
1105 struct file_name_list *dirtmp;
1106 char *prefix;
1108 if (include_prefix != 0)
1109 prefix = include_prefix;
1110 else {
1111 prefix = savestring (GCC_INCLUDE_DIR);
1112 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1113 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1114 prefix[strlen (prefix) - 7] = 0;
1117 dirtmp = (struct file_name_list *)
1118 xmalloc (sizeof (struct file_name_list));
1119 dirtmp->next = 0; /* New one goes on the end */
1120 dirtmp->control_macro = 0;
1121 if (i + 1 == argc)
1122 fatal ("Directory name missing after `-iwithprefix' option");
1124 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
1125 + strlen (prefix) + 1);
1126 strcpy (dirtmp->fname, prefix);
1127 strcat (dirtmp->fname, argv[++i]);
1129 if (after_include == 0)
1130 after_include = dirtmp;
1131 else
1132 last_after_include->next = dirtmp;
1133 last_after_include = dirtmp; /* Tail follows the last one */
1135 /* Add directory to main path for includes,
1136 with the default prefix at the front of its name. */
1137 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1138 struct file_name_list *dirtmp;
1139 char *prefix;
1141 if (include_prefix != 0)
1142 prefix = include_prefix;
1143 else {
1144 prefix = savestring (GCC_INCLUDE_DIR);
1145 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1146 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1147 prefix[strlen (prefix) - 7] = 0;
1150 dirtmp = (struct file_name_list *)
1151 xmalloc (sizeof (struct file_name_list));
1152 dirtmp->next = 0; /* New one goes on the end */
1153 dirtmp->control_macro = 0;
1154 if (i + 1 == argc)
1155 fatal ("Directory name missing after `-iwithprefixbefore' option");
1157 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
1158 + strlen (prefix) + 1);
1159 strcpy (dirtmp->fname, prefix);
1160 strcat (dirtmp->fname, argv[++i]);
1162 append_include_chain (dirtmp, dirtmp);
1164 /* Add directory to end of path for includes. */
1165 if (!strcmp (argv[i], "-idirafter")) {
1166 struct file_name_list *dirtmp;
1168 dirtmp = (struct file_name_list *)
1169 xmalloc (sizeof (struct file_name_list));
1170 dirtmp->next = 0; /* New one goes on the end */
1171 dirtmp->control_macro = 0;
1172 if (i + 1 == argc)
1173 fatal ("Directory name missing after `-idirafter' option");
1174 else
1175 dirtmp->fname = argv[++i];
1177 if (after_include == 0)
1178 after_include = dirtmp;
1179 else
1180 last_after_include->next = dirtmp;
1181 last_after_include = dirtmp; /* Tail follows the last one */
1183 break;
1185 case 'o':
1186 if (out_fname != NULL)
1187 fatal ("Output filename specified twice");
1188 if (i + 1 == argc)
1189 fatal ("Filename missing after -o option");
1190 out_fname = argv[++i];
1191 if (!strcmp (out_fname, "-"))
1192 out_fname = "";
1193 break;
1195 case 'p':
1196 if (!strcmp (argv[i], "-pedantic"))
1197 pedantic = 1;
1198 else if (!strcmp (argv[i], "-pedantic-errors")) {
1199 pedantic = 1;
1200 pedantic_errors = 1;
1201 } else if (!strcmp (argv[i], "-pcp")) {
1202 char *pcp_fname = argv[++i];
1203 pcp_outfile =
1204 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1205 ? fopen (pcp_fname, "w")
1206 : fdopen (dup (fileno (stdout)), "w"));
1207 if (pcp_outfile == 0)
1208 pfatal_with_name (pcp_fname);
1209 no_precomp = 1;
1211 break;
1213 case 't':
1214 if (!strcmp (argv[i], "-traditional")) {
1215 traditional = 1;
1216 if (dollars_in_ident > 0)
1217 dollars_in_ident = 1;
1218 } else if (!strcmp (argv[i], "-trigraphs")) {
1219 no_trigraphs = 0;
1221 break;
1223 case 'l':
1224 if (! strcmp (argv[i], "-lang-c"))
1225 cplusplus = 0, cplusplus_comments = 0, objc = 0;
1226 if (! strcmp (argv[i], "-lang-c++"))
1227 cplusplus = 1, cplusplus_comments = 1, objc = 0;
1228 if (! strcmp (argv[i], "-lang-objc"))
1229 objc = 1, cplusplus = 0, cplusplus_comments = 1;
1230 if (! strcmp (argv[i], "-lang-objc++"))
1231 objc = 1, cplusplus = 1, cplusplus_comments = 1;
1232 if (! strcmp (argv[i], "-lang-asm"))
1233 lang_asm = 1;
1234 if (! strcmp (argv[i], "-lint"))
1235 lint = 1;
1236 break;
1238 case '+':
1239 cplusplus = 1, cplusplus_comments = 1;
1240 break;
1242 case 'w':
1243 inhibit_warnings = 1;
1244 break;
1246 case 'W':
1247 if (!strcmp (argv[i], "-Wtrigraphs"))
1248 warn_trigraphs = 1;
1249 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1250 warn_trigraphs = 0;
1251 else if (!strcmp (argv[i], "-Wcomment"))
1252 warn_comments = 1;
1253 else if (!strcmp (argv[i], "-Wno-comment"))
1254 warn_comments = 0;
1255 else if (!strcmp (argv[i], "-Wcomments"))
1256 warn_comments = 1;
1257 else if (!strcmp (argv[i], "-Wno-comments"))
1258 warn_comments = 0;
1259 else if (!strcmp (argv[i], "-Wtraditional"))
1260 warn_stringify = 1;
1261 else if (!strcmp (argv[i], "-Wno-traditional"))
1262 warn_stringify = 0;
1263 else if (!strcmp (argv[i], "-Wimport"))
1264 warn_import = 1;
1265 else if (!strcmp (argv[i], "-Wno-import"))
1266 warn_import = 0;
1267 else if (!strcmp (argv[i], "-Werror"))
1268 warnings_are_errors = 1;
1269 else if (!strcmp (argv[i], "-Wno-error"))
1270 warnings_are_errors = 0;
1271 else if (!strcmp (argv[i], "-Wall"))
1273 warn_trigraphs = 1;
1274 warn_comments = 1;
1276 break;
1278 case 'M':
1279 if (!strcmp (argv[i], "-M"))
1280 print_deps = 2;
1281 else if (!strcmp (argv[i], "-MM"))
1282 print_deps = 1;
1283 else if (!strcmp (argv[i], "-MD"))
1284 print_deps = 2;
1285 else if (!strcmp (argv[i], "-MMD"))
1286 print_deps = 1;
1287 /* For -MD and -MMD options, write deps on file named by next arg. */
1288 if (!strcmp (argv[i], "-MD")
1289 || !strcmp (argv[i], "-MMD")) {
1290 i++;
1291 deps_file = argv[i];
1292 deps_mode = "w";
1293 } else {
1294 /* For -M and -MM, write deps on standard output
1295 and suppress the usual output. */
1296 deps_stream = stdout;
1297 inhibit_output = 1;
1299 break;
1301 case 'd':
1303 char *p = argv[i] + 2;
1304 char c;
1305 while (c = *p++) {
1306 /* Arg to -d specifies what parts of macros to dump */
1307 switch (c) {
1308 case 'M':
1309 dump_macros = dump_only;
1310 no_output = 1;
1311 break;
1312 case 'N':
1313 dump_macros = dump_names;
1314 break;
1315 case 'D':
1316 dump_macros = dump_definitions;
1317 break;
1321 break;
1323 case 'g':
1324 if (argv[i][2] == '3')
1325 debug_output = 1;
1326 break;
1328 case 'v':
1329 fprintf (stderr, "GNU CPP version %s", version_string);
1330 #ifdef TARGET_VERSION
1331 TARGET_VERSION;
1332 #endif
1333 fprintf (stderr, "\n");
1334 verbose = 1;
1335 break;
1337 case 'H':
1338 print_include_names = 1;
1339 break;
1341 case 'D':
1343 char *p, *p1;
1345 if (argv[i][2] != 0)
1346 p = argv[i] + 2;
1347 else if (i + 1 == argc)
1348 fatal ("Macro name missing after -D option");
1349 else
1350 p = argv[++i];
1352 pend_defs[i] = p;
1354 break;
1356 case 'A':
1358 char *p, *p1;
1360 if (argv[i][2] != 0)
1361 p = argv[i] + 2;
1362 else if (i + 1 == argc)
1363 fatal ("Assertion missing after -A option");
1364 else
1365 p = argv[++i];
1367 if (!strcmp (p, "-")) {
1368 /* -A- eliminates all predefined macros and assertions.
1369 Let's include also any that were specified earlier
1370 on the command line. That way we can get rid of any
1371 that were passed automatically in from GCC. */
1372 int j;
1373 inhibit_predefs = 1;
1374 for (j = 0; j < i; j++)
1375 pend_defs[j] = pend_assertions[j] = 0;
1376 } else {
1377 pend_assertions[i] = p;
1378 pend_assertion_options[i] = "-A";
1381 break;
1383 case 'U': /* JF #undef something */
1384 if (argv[i][2] != 0)
1385 pend_undefs[i] = argv[i] + 2;
1386 else if (i + 1 == argc)
1387 fatal ("Macro name missing after -U option");
1388 else
1389 pend_undefs[i] = argv[i+1], i++;
1390 break;
1392 case 'C':
1393 put_out_comments = 1;
1394 break;
1396 case 'E': /* -E comes from cc -E; ignore it. */
1397 break;
1399 case 'P':
1400 no_line_commands = 1;
1401 break;
1403 case '$': /* Don't include $ in identifiers. */
1404 dollars_in_ident = 0;
1405 break;
1407 case 'I': /* Add directory to path for includes. */
1409 struct file_name_list *dirtmp;
1411 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
1412 ignore_srcdir = 1;
1413 /* Don't use any preceding -I directories for #include <...>. */
1414 first_bracket_include = 0;
1416 else {
1417 dirtmp = (struct file_name_list *)
1418 xmalloc (sizeof (struct file_name_list));
1419 dirtmp->next = 0; /* New one goes on the end */
1420 dirtmp->control_macro = 0;
1421 if (argv[i][2] != 0)
1422 dirtmp->fname = argv[i] + 2;
1423 else if (i + 1 == argc)
1424 fatal ("Directory name missing after -I option");
1425 else
1426 dirtmp->fname = argv[++i];
1427 append_include_chain (dirtmp, dirtmp);
1430 break;
1432 case 'n':
1433 if (!strcmp (argv[i], "-nostdinc"))
1434 /* -nostdinc causes no default include directories.
1435 You must specify all include-file directories with -I. */
1436 no_standard_includes = 1;
1437 else if (!strcmp (argv[i], "-nostdinc++"))
1438 /* -nostdinc++ causes no default C++-specific include directories. */
1439 no_standard_cplusplus_includes = 1;
1440 else if (!strcmp (argv[i], "-noprecomp"))
1441 no_precomp = 1;
1442 break;
1444 case 'u':
1445 /* Sun compiler passes undocumented switch "-undef".
1446 Let's assume it means to inhibit the predefined symbols. */
1447 inhibit_predefs = 1;
1448 break;
1450 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1451 if (in_fname == NULL) {
1452 in_fname = "";
1453 break;
1454 } else if (out_fname == NULL) {
1455 out_fname = "";
1456 break;
1457 } /* else fall through into error */
1459 default:
1460 fatal ("Invalid option `%s'", argv[i]);
1465 /* Add dirs from CPATH after dirs from -I. */
1466 /* There seems to be confusion about what CPATH should do,
1467 so for the moment it is not documented. */
1468 /* Some people say that CPATH should replace the standard include dirs,
1469 but that seems pointless: it comes before them, so it overrides them
1470 anyway. */
1471 p = (char *) getenv ("CPATH");
1472 if (p != 0 && ! no_standard_includes)
1473 path_include (p);
1475 /* Now that dollars_in_ident is known, initialize is_idchar. */
1476 initialize_char_syntax ();
1478 /* Initialize output buffer */
1480 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1481 outbuf.bufp = outbuf.buf;
1482 outbuf.length = OUTBUF_SIZE;
1484 /* Do partial setup of input buffer for the sake of generating
1485 early #line directives (when -g is in effect). */
1487 fp = &instack[++indepth];
1488 if (in_fname == NULL)
1489 in_fname = "";
1490 fp->nominal_fname = fp->fname = in_fname;
1491 fp->lineno = 0;
1493 /* Install __LINE__, etc. Must follow initialize_char_syntax
1494 and option processing. */
1495 initialize_builtins (fp, &outbuf);
1497 /* Do standard #defines and assertions
1498 that identify system and machine type. */
1500 if (!inhibit_predefs) {
1501 char *p = (char *) alloca (strlen (predefs) + 1);
1502 strcpy (p, predefs);
1503 while (*p) {
1504 char *q;
1505 while (*p == ' ' || *p == '\t')
1506 p++;
1507 /* Handle -D options. */
1508 if (p[0] == '-' && p[1] == 'D') {
1509 q = &p[2];
1510 while (*p && *p != ' ' && *p != '\t')
1511 p++;
1512 if (*p != 0)
1513 *p++= 0;
1514 if (debug_output)
1515 output_line_command (fp, &outbuf, 0, same_file);
1516 make_definition (q, &outbuf);
1517 while (*p == ' ' || *p == '\t')
1518 p++;
1519 } else if (p[0] == '-' && p[1] == 'A') {
1520 /* Handle -A options (assertions). */
1521 char *assertion;
1522 char *past_name;
1523 char *value;
1524 char *past_value;
1525 char *termination;
1526 int save_char;
1528 assertion = &p[2];
1529 past_name = assertion;
1530 /* Locate end of name. */
1531 while (*past_name && *past_name != ' '
1532 && *past_name != '\t' && *past_name != '(')
1533 past_name++;
1534 /* Locate `(' at start of value. */
1535 value = past_name;
1536 while (*value && (*value == ' ' || *value == '\t'))
1537 value++;
1538 if (*value++ != '(')
1539 abort ();
1540 while (*value && (*value == ' ' || *value == '\t'))
1541 value++;
1542 past_value = value;
1543 /* Locate end of value. */
1544 while (*past_value && *past_value != ' '
1545 && *past_value != '\t' && *past_value != ')')
1546 past_value++;
1547 termination = past_value;
1548 while (*termination && (*termination == ' ' || *termination == '\t'))
1549 termination++;
1550 if (*termination++ != ')')
1551 abort ();
1552 if (*termination && *termination != ' ' && *termination != '\t')
1553 abort ();
1554 /* Temporarily null-terminate the value. */
1555 save_char = *termination;
1556 *termination = '\0';
1557 /* Install the assertion. */
1558 make_assertion ("-A", assertion);
1559 *termination = (char) save_char;
1560 p = termination;
1561 while (*p == ' ' || *p == '\t')
1562 p++;
1563 } else {
1564 abort ();
1569 /* Now handle the command line options. */
1571 /* Do -U's, -D's and -A's in the order they were seen. */
1572 for (i = 1; i < argc; i++) {
1573 if (pend_undefs[i]) {
1574 if (debug_output)
1575 output_line_command (fp, &outbuf, 0, same_file);
1576 make_undef (pend_undefs[i], &outbuf);
1578 if (pend_defs[i]) {
1579 if (debug_output)
1580 output_line_command (fp, &outbuf, 0, same_file);
1581 make_definition (pend_defs[i], &outbuf);
1583 if (pend_assertions[i])
1584 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1587 done_initializing = 1;
1589 { /* read the appropriate environment variable and if it exists
1590 replace include_defaults with the listed path. */
1591 char *epath = 0;
1592 switch ((objc << 1) + cplusplus)
1594 case 0:
1595 epath = getenv ("C_INCLUDE_PATH");
1596 break;
1597 case 1:
1598 epath = getenv ("CPLUS_INCLUDE_PATH");
1599 break;
1600 case 2:
1601 epath = getenv ("OBJC_INCLUDE_PATH");
1602 break;
1603 case 3:
1604 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
1605 break;
1607 /* If the environment var for this language is set,
1608 add to the default list of include directories. */
1609 if (epath) {
1610 char *nstore = (char *) alloca (strlen (epath) + 2);
1611 int num_dirs;
1612 char *startp, *endp;
1614 for (num_dirs = 1, startp = epath; *startp; startp++)
1615 if (*startp == PATH_SEPARATOR)
1616 num_dirs++;
1617 include_defaults
1618 = (struct default_include *) xmalloc ((num_dirs
1619 * sizeof (struct default_include))
1620 + sizeof (include_defaults_array));
1621 startp = endp = epath;
1622 num_dirs = 0;
1623 while (1) {
1624 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1625 if ((*endp == PATH_SEPARATOR
1626 #if 0 /* Obsolete, now that we use semicolons as the path separator. */
1627 #ifdef __MSDOS__
1628 && (endp-startp != 1 || !isalpha (*startp))
1629 #endif
1630 #endif
1632 || *endp == 0) {
1633 strncpy (nstore, startp, endp-startp);
1634 if (endp == startp)
1635 strcpy (nstore, ".");
1636 else
1637 nstore[endp-startp] = '\0';
1639 include_defaults[num_dirs].fname = savestring (nstore);
1640 include_defaults[num_dirs].cplusplus = cplusplus;
1641 num_dirs++;
1642 if (*endp == '\0')
1643 break;
1644 endp = startp = endp + 1;
1645 } else
1646 endp++;
1648 /* Put the usual defaults back in at the end. */
1649 bcopy (include_defaults_array, &include_defaults[num_dirs],
1650 sizeof (include_defaults_array));
1654 first_system_include = 0;
1655 /* Unless -fnostdinc,
1656 tack on the standard include file dirs to the specified list */
1657 if (!no_standard_includes) {
1658 struct default_include *p = include_defaults;
1659 char *specd_prefix = include_prefix;
1660 char *default_prefix = savestring (GCC_INCLUDE_DIR);
1661 int default_len = 0;
1662 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1663 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1664 default_len = strlen (default_prefix) - 7;
1665 default_prefix[default_len] = 0;
1667 /* Search "translated" versions of GNU directories.
1668 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1669 if (specd_prefix != 0 && default_len != 0)
1670 for (p = include_defaults; p->fname; p++) {
1671 /* Some standard dirs are only for C++. */
1672 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1673 /* Does this dir start with the prefix? */
1674 if (!strncmp (p->fname, default_prefix, default_len)) {
1675 /* Yes; change prefix and add to search list. */
1676 struct file_name_list *new
1677 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1678 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
1679 char *str = (char *) xmalloc (this_len + 1);
1680 strcpy (str, specd_prefix);
1681 strcat (str, p->fname + default_len);
1682 new->fname = str;
1683 new->control_macro = 0;
1684 append_include_chain (new, new);
1685 if (first_system_include == 0)
1686 first_system_include = new;
1690 /* Search ordinary names for GNU include directories. */
1691 for (p = include_defaults; p->fname; p++) {
1692 /* Some standard dirs are only for C++. */
1693 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1694 struct file_name_list *new
1695 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1696 new->control_macro = 0;
1697 new->fname = p->fname;
1698 append_include_chain (new, new);
1699 if (first_system_include == 0)
1700 first_system_include = new;
1705 /* Tack the after_include chain at the end of the include chain. */
1706 append_include_chain (after_include, last_after_include);
1707 if (first_system_include == 0)
1708 first_system_include = after_include;
1710 /* With -v, print the list of dirs to search. */
1711 if (verbose) {
1712 struct file_name_list *p;
1713 fprintf (stderr, "#include \"...\" search starts here:\n");
1714 for (p = include; p; p = p->next) {
1715 if (p == first_bracket_include)
1716 fprintf (stderr, "#include <...> search starts here:\n");
1717 fprintf (stderr, " %s\n", p->fname);
1719 fprintf (stderr, "End of search list.\n");
1722 /* Scan the -imacros files before the main input.
1723 Much like #including them, but with no_output set
1724 so that only their macro definitions matter. */
1726 no_output++;
1727 for (i = 1; i < argc; i++)
1728 if (pend_files[i]) {
1729 int fd = open (pend_files[i], O_RDONLY, 0666);
1730 if (fd < 0) {
1731 perror_with_name (pend_files[i]);
1732 return FAILURE_EXIT_CODE;
1734 finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
1736 no_output--;
1738 /* Copy the entire contents of the main input file into
1739 the stacked input buffer previously allocated for it. */
1741 /* JF check for stdin */
1742 if (in_fname == NULL || *in_fname == 0) {
1743 in_fname = "";
1744 f = 0;
1745 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
1746 goto perror;
1748 /* Either of two environment variables can specify output of deps.
1749 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1750 where OUTPUT_FILE is the file to write deps info to
1751 and DEPS_TARGET is the target to mention in the deps. */
1753 if (print_deps == 0
1754 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1755 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1756 char *spec = getenv ("DEPENDENCIES_OUTPUT");
1757 char *s;
1758 char *output_file;
1760 if (spec == 0) {
1761 spec = getenv ("SUNPRO_DEPENDENCIES");
1762 print_deps = 2;
1764 else
1765 print_deps = 1;
1767 s = spec;
1768 /* Find the space before the DEPS_TARGET, if there is one. */
1769 /* This should use index. (mrs) */
1770 while (*s != 0 && *s != ' ') s++;
1771 if (*s != 0) {
1772 deps_target = s + 1;
1773 output_file = (char *) xmalloc (s - spec + 1);
1774 bcopy (spec, output_file, s - spec);
1775 output_file[s - spec] = 0;
1777 else {
1778 deps_target = 0;
1779 output_file = spec;
1782 deps_file = output_file;
1783 deps_mode = "a";
1786 /* For -M, print the expected object file name
1787 as the target of this Make-rule. */
1788 if (print_deps) {
1789 deps_allocated_size = 200;
1790 deps_buffer = (char *) xmalloc (deps_allocated_size);
1791 deps_buffer[0] = 0;
1792 deps_size = 0;
1793 deps_column = 0;
1795 if (deps_target) {
1796 deps_output (deps_target, 0);
1797 deps_output (":", 0);
1798 } else if (*in_fname == 0)
1799 deps_output ("-: ", 0);
1800 else {
1801 int len;
1802 char *p = in_fname;
1803 char *p1 = p;
1804 /* Discard all directory prefixes from P. */
1805 while (*p1) {
1806 if (*p1 == '/')
1807 p = p1 + 1;
1808 p1++;
1810 /* Output P, but remove known suffixes. */
1811 len = strlen (p);
1812 if (p[len - 2] == '.' && p[len - 1] == 'c')
1813 deps_output (p, len - 2);
1814 else if (p[len - 2] == '.' && p[len - 1] == 'C')
1815 deps_output (p, len - 2);
1816 else if (p[len - 3] == '.'
1817 && p[len - 2] == 'c'
1818 && p[len - 1] == 'c')
1819 deps_output (p, len - 3);
1820 else if (p[len - 4] == '.'
1821 && p[len - 3] == 'c'
1822 && p[len - 2] == 'x'
1823 && p[len - 1] == 'x')
1824 deps_output (p, len - 4);
1825 else if (p[len - 2] == '.' && p[len - 1] == 's')
1826 deps_output (p, len - 2);
1827 else if (p[len - 2] == '.' && p[len - 1] == 'S')
1828 deps_output (p, len - 2);
1829 else if (p[len - 2] == '.' && p[len - 1] == 'm')
1830 deps_output (p, len - 2);
1831 else
1832 deps_output (p, 0);
1833 /* Supply our own suffix. */
1834 #ifndef VMS
1835 deps_output (".o : ", 0);
1836 #else
1837 deps_output (".obj : ", 0);
1838 #endif
1839 deps_output (in_fname, 0);
1840 deps_output (" ", 0);
1844 file_size_and_mode (f, &st_mode, &st_size);
1845 fp->nominal_fname = fp->fname = in_fname;
1846 fp->lineno = 1;
1847 fp->system_header_p = 0;
1848 /* JF all this is mine about reading pipes and ttys */
1849 if (! S_ISREG (st_mode)) {
1850 /* Read input from a file that is not a normal disk file.
1851 We cannot preallocate a buffer with the correct size,
1852 so we must read in the file a piece at the time and make it bigger. */
1853 int size;
1854 int bsize;
1855 int cnt;
1856 U_CHAR *bufp;
1858 bsize = 2000;
1859 size = 0;
1860 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
1861 bufp = fp->buf;
1862 for (;;) {
1863 cnt = read (f, bufp, bsize - size);
1864 if (cnt < 0) goto perror; /* error! */
1865 if (cnt == 0) break; /* End of file */
1866 size += cnt;
1867 bufp += cnt;
1868 if (bsize == size) { /* Buffer is full! */
1869 bsize *= 2;
1870 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
1871 bufp = fp->buf + size; /* May have moved */
1874 fp->length = size;
1875 } else {
1876 /* Read a file whose size we can determine in advance.
1877 For the sake of VMS, st_size is just an upper bound. */
1878 long i;
1879 fp->length = 0;
1880 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
1882 while (st_size > 0) {
1883 i = read (f, fp->buf + fp->length, st_size);
1884 if (i <= 0) {
1885 if (i == 0) break;
1886 goto perror;
1888 fp->length += i;
1889 st_size -= i;
1892 fp->bufp = fp->buf;
1893 fp->if_stack = if_stack;
1895 /* Make sure data ends with a newline. And put a null after it. */
1897 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
1898 /* Backslash-newline at end is not good enough. */
1899 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
1900 fp->buf[fp->length++] = '\n';
1901 missing_newline = 1;
1903 fp->buf[fp->length] = '\0';
1905 /* Unless inhibited, convert trigraphs in the input. */
1907 if (!no_trigraphs)
1908 trigraph_pcp (fp);
1910 /* Now that we know the input file is valid, open the output. */
1912 if (!out_fname || !strcmp (out_fname, ""))
1913 out_fname = "stdout";
1914 else if (! freopen (out_fname, "w", stdout))
1915 pfatal_with_name (out_fname);
1917 output_line_command (fp, &outbuf, 0, same_file);
1919 /* Scan the -include files before the main input. */
1921 for (i = 1; i < argc; i++)
1922 if (pend_includes[i]) {
1923 int fd = open (pend_includes[i], O_RDONLY, 0666);
1924 if (fd < 0) {
1925 perror_with_name (pend_includes[i]);
1926 return FAILURE_EXIT_CODE;
1928 finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
1931 /* Scan the input, processing macros and directives. */
1933 rescan (&outbuf, 0);
1935 if (missing_newline)
1936 fp->lineno--;
1938 if (pedantic && missing_newline)
1939 pedwarn ("file does not end in newline");
1941 /* Now we have processed the entire input
1942 Write whichever kind of output has been requested. */
1944 if (dump_macros == dump_only)
1945 dump_all_macros ();
1946 else if (! inhibit_output) {
1947 write_output ();
1950 if (print_deps) {
1951 /* Don't actually write the deps file if compilation has failed. */
1952 if (errors == 0) {
1953 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
1954 pfatal_with_name (deps_file);
1955 fputs (deps_buffer, deps_stream);
1956 putc ('\n', deps_stream);
1957 if (deps_file) {
1958 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1959 fatal ("I/O error on output");
1964 if (pcp_outfile && pcp_outfile != stdout
1965 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
1966 fatal ("I/O error on `-pcp' output");
1968 if (ferror (stdout) || fclose (stdout) != 0)
1969 fatal ("I/O error on output");
1971 if (errors)
1972 exit (FAILURE_EXIT_CODE);
1973 exit (SUCCESS_EXIT_CODE);
1975 perror:
1976 pfatal_with_name (in_fname);
1977 return 0;
1980 /* Given a colon-separated list of file names PATH,
1981 add all the names to the search path for include files. */
1983 static void
1984 path_include (path)
1985 char *path;
1987 char *p;
1989 p = path;
1991 if (*p)
1992 while (1) {
1993 char *q = p;
1994 char *name;
1995 struct file_name_list *dirtmp;
1997 /* Find the end of this name. */
1998 while (*q != 0 && *q != PATH_SEPARATOR) q++;
1999 if (p == q) {
2000 /* An empty name in the path stands for the current directory. */
2001 name = (char *) xmalloc (2);
2002 name[0] = '.';
2003 name[1] = 0;
2004 } else {
2005 /* Otherwise use the directory that is named. */
2006 name = (char *) xmalloc (q - p + 1);
2007 bcopy (p, name, q - p);
2008 name[q - p] = 0;
2011 dirtmp = (struct file_name_list *)
2012 xmalloc (sizeof (struct file_name_list));
2013 dirtmp->next = 0; /* New one goes on the end */
2014 dirtmp->control_macro = 0;
2015 dirtmp->fname = name;
2016 append_include_chain (dirtmp, dirtmp);
2018 /* Advance past this name. */
2019 p = q;
2020 if (*p == 0)
2021 break;
2022 /* Skip the colon. */
2023 p++;
2027 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2028 before main CCCP processing. Name `pcp' is also in honor of the
2029 drugs the trigraph designers must have been on.
2031 Using an extra pass through the buffer takes a little extra time,
2032 but is infinitely less hairy than trying to handle trigraphs inside
2033 strings, etc. everywhere, and also makes sure that trigraphs are
2034 only translated in the top level of processing. */
2036 static void
2037 trigraph_pcp (buf)
2038 FILE_BUF *buf;
2040 register U_CHAR c, *fptr, *bptr, *sptr;
2041 int len;
2043 fptr = bptr = sptr = buf->buf;
2044 while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
2045 if (*++sptr != '?')
2046 continue;
2047 switch (*++sptr) {
2048 case '=':
2049 c = '#';
2050 break;
2051 case '(':
2052 c = '[';
2053 break;
2054 case '/':
2055 c = '\\';
2056 break;
2057 case ')':
2058 c = ']';
2059 break;
2060 case '\'':
2061 c = '^';
2062 break;
2063 case '<':
2064 c = '{';
2065 break;
2066 case '!':
2067 c = '|';
2068 break;
2069 case '>':
2070 c = '}';
2071 break;
2072 case '-':
2073 c = '~';
2074 break;
2075 case '?':
2076 sptr--;
2077 continue;
2078 default:
2079 continue;
2081 len = sptr - fptr - 2;
2082 if (bptr != fptr && len > 0)
2083 bcopy (fptr, bptr, len); /* BSD doc says bcopy () works right
2084 for overlapping strings. In ANSI
2085 C, this will be memmove (). */
2086 bptr += len;
2087 *bptr++ = c;
2088 fptr = ++sptr;
2090 len = buf->length - (fptr - buf->buf);
2091 if (bptr != fptr && len > 0)
2092 bcopy (fptr, bptr, len);
2093 buf->length -= fptr - bptr;
2094 buf->buf[buf->length] = '\0';
2095 if (warn_trigraphs && fptr != bptr)
2096 warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
2099 /* Move all backslash-newline pairs out of embarrassing places.
2100 Exchange all such pairs following BP
2101 with any potentially-embarrassing characters that follow them.
2102 Potentially-embarrassing characters are / and *
2103 (because a backslash-newline inside a comment delimiter
2104 would cause it not to be recognized). */
2106 static void
2107 newline_fix (bp)
2108 U_CHAR *bp;
2110 register U_CHAR *p = bp;
2111 register int count = 0;
2113 /* First count the backslash-newline pairs here. */
2115 while (1) {
2116 if (p[0] == '\\') {
2117 if (p[1] == '\n')
2118 p += 2, count++;
2119 else if (p[1] == '\r' && p[2] == '\n')
2120 p += 3, count++;
2121 else
2122 break;
2123 } else
2124 break;
2127 /* What follows the backslash-newlines is not embarrassing. */
2129 if (count == 0 || (*p != '/' && *p != '*'))
2130 return;
2132 /* Copy all potentially embarrassing characters
2133 that follow the backslash-newline pairs
2134 down to where the pairs originally started. */
2136 while (*p == '*' || *p == '/')
2137 *bp++ = *p++;
2139 /* Now write the same number of pairs after the embarrassing chars. */
2140 while (count-- > 0) {
2141 *bp++ = '\\';
2142 *bp++ = '\n';
2146 /* Like newline_fix but for use within a directive-name.
2147 Move any backslash-newlines up past any following symbol constituents. */
2149 static void
2150 name_newline_fix (bp)
2151 U_CHAR *bp;
2153 register U_CHAR *p = bp;
2154 register int count = 0;
2156 /* First count the backslash-newline pairs here. */
2157 while (1) {
2158 if (p[0] == '\\') {
2159 if (p[1] == '\n')
2160 p += 2, count++;
2161 else if (p[1] == '\r' && p[2] == '\n')
2162 p += 3, count++;
2163 else
2164 break;
2165 } else
2166 break;
2169 /* What follows the backslash-newlines is not embarrassing. */
2171 if (count == 0 || !is_idchar[*p])
2172 return;
2174 /* Copy all potentially embarrassing characters
2175 that follow the backslash-newline pairs
2176 down to where the pairs originally started. */
2178 while (is_idchar[*p])
2179 *bp++ = *p++;
2181 /* Now write the same number of pairs after the embarrassing chars. */
2182 while (count-- > 0) {
2183 *bp++ = '\\';
2184 *bp++ = '\n';
2188 /* Look for lint commands in comments.
2190 When we come in here, ibp points into a comment. Limit is as one expects.
2191 scan within the comment -- it should start, after lwsp, with a lint command.
2192 If so that command is returned as a (constant) string.
2194 Upon return, any arg will be pointed to with argstart and will be
2195 arglen long. Note that we don't parse that arg since it will just
2196 be printed out again.
2199 static char *
2200 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2201 register U_CHAR *ibp;
2202 register U_CHAR *limit;
2203 U_CHAR **argstart; /* point to command arg */
2204 int *arglen, *cmdlen; /* how long they are */
2206 long linsize;
2207 register U_CHAR *numptr; /* temp for arg parsing */
2209 *arglen = 0;
2211 SKIP_WHITE_SPACE (ibp);
2213 if (ibp >= limit) return NULL;
2215 linsize = limit - ibp;
2217 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2218 if ((linsize >= 10) && !strncmp (ibp, "NOTREACHED", 10)) {
2219 *cmdlen = 10;
2220 return "NOTREACHED";
2222 if ((linsize >= 8) && !strncmp (ibp, "ARGSUSED", 8)) {
2223 *cmdlen = 8;
2224 return "ARGSUSED";
2226 if ((linsize >= 11) && !strncmp (ibp, "LINTLIBRARY", 11)) {
2227 *cmdlen = 11;
2228 return "LINTLIBRARY";
2230 if ((linsize >= 7) && !strncmp (ibp, "VARARGS", 7)) {
2231 *cmdlen = 7;
2232 ibp += 7; linsize -= 7;
2233 if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
2235 /* OK, read a number */
2236 for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
2237 numptr++);
2238 *arglen = numptr - *argstart;
2239 return "VARARGS";
2241 return NULL;
2245 * The main loop of the program.
2247 * Read characters from the input stack, transferring them to the
2248 * output buffer OP.
2250 * Macros are expanded and push levels on the input stack.
2251 * At the end of such a level it is popped off and we keep reading.
2252 * At the end of any other kind of level, we return.
2253 * #-directives are handled, except within macros.
2255 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2256 * and insert them when appropriate. This is set while scanning macro
2257 * arguments before substitution. It is zero when scanning for final output.
2258 * There are three types of Newline markers:
2259 * * Newline - follows a macro name that was not expanded
2260 * because it appeared inside an expansion of the same macro.
2261 * This marker prevents future expansion of that identifier.
2262 * When the input is rescanned into the final output, these are deleted.
2263 * These are also deleted by ## concatenation.
2264 * * Newline Space (or Newline and any other whitespace character)
2265 * stands for a place that tokens must be separated or whitespace
2266 * is otherwise desirable, but where the ANSI standard specifies there
2267 * is no whitespace. This marker turns into a Space (or whichever other
2268 * whitespace char appears in the marker) in the final output,
2269 * but it turns into nothing in an argument that is stringified with #.
2270 * Such stringified arguments are the only place where the ANSI standard
2271 * specifies with precision that whitespace may not appear.
2273 * During this function, IP->bufp is kept cached in IBP for speed of access.
2274 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2275 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2276 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2277 * explicitly, and before RECACHE, since RECACHE uses OBP.
2280 static void
2281 rescan (op, output_marks)
2282 FILE_BUF *op;
2283 int output_marks;
2285 /* Character being scanned in main loop. */
2286 register U_CHAR c;
2288 /* Length of pending accumulated identifier. */
2289 register int ident_length = 0;
2291 /* Hash code of pending accumulated identifier. */
2292 register int hash = 0;
2294 /* Current input level (&instack[indepth]). */
2295 FILE_BUF *ip;
2297 /* Pointer for scanning input. */
2298 register U_CHAR *ibp;
2300 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2301 register U_CHAR *limit;
2303 /* Pointer for storing output. */
2304 register U_CHAR *obp;
2306 /* REDO_CHAR is nonzero if we are processing an identifier
2307 after backing up over the terminating character.
2308 Sometimes we process an identifier without backing up over
2309 the terminating character, if the terminating character
2310 is not special. Backing up is done so that the terminating character
2311 will be dispatched on again once the identifier is dealt with. */
2312 int redo_char = 0;
2314 /* 1 if within an identifier inside of which a concatenation
2315 marker (Newline -) has been seen. */
2316 int concatenated = 0;
2318 /* While scanning a comment or a string constant,
2319 this records the line it started on, for error messages. */
2320 int start_line;
2322 /* Record position of last `real' newline. */
2323 U_CHAR *beg_of_line;
2325 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2327 #define POPMACRO \
2328 do { ip->macro->type = T_MACRO; \
2329 if (ip->free_ptr) free (ip->free_ptr); \
2330 --indepth; } while (0)
2332 /* Reload `rescan's local variables that describe the current
2333 level of the input stack. */
2335 #define RECACHE \
2336 do { ip = &instack[indepth]; \
2337 ibp = ip->bufp; \
2338 limit = ip->buf + ip->length; \
2339 op->bufp = obp; \
2340 check_expand (op, limit - ibp); \
2341 beg_of_line = 0; \
2342 obp = op->bufp; } while (0)
2344 if (no_output && instack[indepth].fname != 0)
2345 skip_if_group (&instack[indepth], 1);
2347 obp = op->bufp;
2348 RECACHE;
2350 beg_of_line = ibp;
2352 /* Our caller must always put a null after the end of
2353 the input at each input stack level. */
2354 if (*limit != 0)
2355 abort ();
2357 while (1) {
2358 c = *ibp++;
2359 *obp++ = c;
2361 switch (c) {
2362 case '\\':
2363 if (ibp >= limit)
2364 break;
2365 if (*ibp == '\n') {
2366 /* Always merge lines ending with backslash-newline,
2367 even in middle of identifier. */
2368 ++ibp;
2369 ++ip->lineno;
2370 --obp; /* remove backslash from obuf */
2371 break;
2373 /* Otherwise, backslash suppresses specialness of following char,
2374 so copy it here to prevent the switch from seeing it.
2375 But first get any pending identifier processed. */
2376 if (ident_length > 0)
2377 goto specialchar;
2378 *obp++ = *ibp++;
2379 break;
2381 case '#':
2382 if (assertions_flag) {
2383 /* Copy #foo (bar lose) without macro expansion. */
2384 SKIP_WHITE_SPACE (ibp);
2385 while (is_idchar[*ibp])
2386 *obp++ = *ibp++;
2387 SKIP_WHITE_SPACE (ibp);
2388 if (*ibp == '(') {
2389 ip->bufp = ibp;
2390 skip_paren_group (ip);
2391 bcopy (ibp, obp, ip->bufp - ibp);
2392 obp += ip->bufp - ibp;
2393 ibp = ip->bufp;
2397 /* If this is expanding a macro definition, don't recognize
2398 preprocessor directives. */
2399 if (ip->macro != 0)
2400 goto randomchar;
2401 /* If this is expand_into_temp_buffer, recognize them
2402 only after an actual newline at this level,
2403 not at the beginning of the input level. */
2404 if (ip->fname == 0 && beg_of_line == ip->buf)
2405 goto randomchar;
2406 if (ident_length)
2407 goto specialchar;
2410 /* # keyword: a # must be first nonblank char on the line */
2411 if (beg_of_line == 0)
2412 goto randomchar;
2414 U_CHAR *bp;
2416 /* Scan from start of line, skipping whitespace, comments
2417 and backslash-newlines, and see if we reach this #.
2418 If not, this # is not special. */
2419 bp = beg_of_line;
2420 /* If -traditional, require # to be at beginning of line. */
2421 if (!traditional)
2422 while (1) {
2423 if (is_hor_space[*bp])
2424 bp++;
2425 else if (*bp == '\\' && bp[1] == '\n')
2426 bp += 2;
2427 else if (*bp == '/' && bp[1] == '*') {
2428 bp += 2;
2429 while (!(*bp == '*' && bp[1] == '/'))
2430 bp++;
2431 bp += 2;
2433 else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
2434 bp += 2;
2435 while (*bp++ != '\n') ;
2437 else break;
2439 if (bp + 1 != ibp)
2440 goto randomchar;
2443 /* This # can start a directive. */
2445 --obp; /* Don't copy the '#' */
2447 ip->bufp = ibp;
2448 op->bufp = obp;
2449 if (! handle_directive (ip, op)) {
2450 #ifdef USE_C_ALLOCA
2451 alloca (0);
2452 #endif
2453 /* Not a known directive: treat it as ordinary text.
2454 IP, OP, IBP, etc. have not been changed. */
2455 if (no_output && instack[indepth].fname) {
2456 /* If not generating expanded output,
2457 what we do with ordinary text is skip it.
2458 Discard everything until next # directive. */
2459 skip_if_group (&instack[indepth], 1);
2460 RECACHE;
2461 beg_of_line = ibp;
2462 break;
2464 ++obp; /* Copy the '#' after all */
2465 goto randomchar;
2467 #ifdef USE_C_ALLOCA
2468 alloca (0);
2469 #endif
2470 /* A # directive has been successfully processed. */
2471 /* If not generating expanded output, ignore everything until
2472 next # directive. */
2473 if (no_output && instack[indepth].fname)
2474 skip_if_group (&instack[indepth], 1);
2475 obp = op->bufp;
2476 RECACHE;
2477 beg_of_line = ibp;
2478 break;
2480 case '\"': /* skip quoted string */
2481 case '\'':
2482 /* A single quoted string is treated like a double -- some
2483 programs (e.g., troff) are perverse this way */
2485 if (ident_length)
2486 goto specialchar;
2488 start_line = ip->lineno;
2490 /* Skip ahead to a matching quote. */
2492 while (1) {
2493 if (ibp >= limit) {
2494 if (ip->macro != 0) {
2495 /* try harder: this string crosses a macro expansion boundary.
2496 This can happen naturally if -traditional.
2497 Otherwise, only -D can make a macro with an unmatched quote. */
2498 POPMACRO;
2499 RECACHE;
2500 continue;
2502 if (!traditional) {
2503 error_with_line (line_for_error (start_line),
2504 "unterminated string or character constant");
2505 error_with_line (multiline_string_line,
2506 "possible real start of unterminated constant");
2507 multiline_string_line = 0;
2509 break;
2511 *obp++ = *ibp;
2512 switch (*ibp++) {
2513 case '\n':
2514 ++ip->lineno;
2515 ++op->lineno;
2516 /* Traditionally, end of line ends a string constant with no error.
2517 So exit the loop and record the new line. */
2518 if (traditional) {
2519 beg_of_line = ibp;
2520 goto while2end;
2522 if (c == '\'') {
2523 error_with_line (line_for_error (start_line),
2524 "unterminated character constant");
2525 goto while2end;
2527 if (pedantic && multiline_string_line == 0) {
2528 pedwarn_with_line (line_for_error (start_line),
2529 "string constant runs past end of line");
2531 if (multiline_string_line == 0)
2532 multiline_string_line = ip->lineno - 1;
2533 break;
2535 case '\\':
2536 if (ibp >= limit)
2537 break;
2538 if (*ibp == '\n') {
2539 /* Backslash newline is replaced by nothing at all,
2540 but keep the line counts correct. */
2541 --obp;
2542 ++ibp;
2543 ++ip->lineno;
2544 } else {
2545 /* ANSI stupidly requires that in \\ the second \
2546 is *not* prevented from combining with a newline. */
2547 while (*ibp == '\\' && ibp[1] == '\n') {
2548 ibp += 2;
2549 ++ip->lineno;
2551 *obp++ = *ibp++;
2553 break;
2555 case '\"':
2556 case '\'':
2557 if (ibp[-1] == c)
2558 goto while2end;
2559 break;
2562 while2end:
2563 break;
2565 case '/':
2566 if (*ibp == '\\' && ibp[1] == '\n')
2567 newline_fix (ibp);
2569 if (*ibp != '*'
2570 && !(cplusplus_comments && *ibp == '/'))
2571 goto randomchar;
2572 if (ip->macro != 0)
2573 goto randomchar;
2574 if (ident_length)
2575 goto specialchar;
2577 if (*ibp == '/') {
2578 /* C++ style comment... */
2579 start_line = ip->lineno;
2581 --ibp; /* Back over the slash */
2582 --obp;
2584 /* Comments are equivalent to spaces. */
2585 if (! put_out_comments)
2586 *obp++ = ' ';
2587 else {
2588 /* must fake up a comment here */
2589 *obp++ = '/';
2590 *obp++ = '/';
2593 U_CHAR *before_bp = ibp+2;
2595 while (ibp < limit) {
2596 if (*ibp++ == '\n') {
2597 ibp--;
2598 if (put_out_comments) {
2599 bcopy (before_bp, obp, ibp - before_bp);
2600 obp += ibp - before_bp;
2602 break;
2605 break;
2609 /* Ordinary C comment. Skip it, optionally copying it to output. */
2611 start_line = ip->lineno;
2613 ++ibp; /* Skip the star. */
2615 /* If this cpp is for lint, we peek inside the comments: */
2616 if (lint) {
2617 U_CHAR *argbp;
2618 int cmdlen, arglen;
2619 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2621 if (lintcmd != NULL) {
2622 /* I believe it is always safe to emit this newline: */
2623 obp[-1] = '\n';
2624 bcopy ("#pragma lint ", obp, 13);
2625 obp += 13;
2626 bcopy (lintcmd, obp, cmdlen);
2627 obp += cmdlen;
2629 if (arglen != 0) {
2630 *(obp++) = ' ';
2631 bcopy (argbp, obp, arglen);
2632 obp += arglen;
2635 /* OK, now bring us back to the state we were in before we entered
2636 this branch. We need #line b/c the newline for the pragma
2637 could fuck things up. */
2638 output_line_command (ip, op, 0, same_file);
2639 *(obp++) = ' '; /* just in case, if comments are copied thru */
2640 *(obp++) = '/';
2644 /* Comments are equivalent to spaces.
2645 Note that we already output the slash; we might not want it.
2646 For -traditional, a comment is equivalent to nothing. */
2647 if (! put_out_comments) {
2648 if (traditional)
2649 obp--;
2650 else
2651 obp[-1] = ' ';
2653 else
2654 *obp++ = '*';
2657 U_CHAR *before_bp = ibp;
2659 while (ibp < limit) {
2660 switch (*ibp++) {
2661 case '/':
2662 if (warn_comments && ibp < limit && *ibp == '*')
2663 warning ("`/*' within comment");
2664 break;
2665 case '*':
2666 if (*ibp == '\\' && ibp[1] == '\n')
2667 newline_fix (ibp);
2668 if (ibp >= limit || *ibp == '/')
2669 goto comment_end;
2670 break;
2671 case '\n':
2672 ++ip->lineno;
2673 /* Copy the newline into the output buffer, in order to
2674 avoid the pain of a #line every time a multiline comment
2675 is seen. */
2676 if (!put_out_comments)
2677 *obp++ = '\n';
2678 ++op->lineno;
2681 comment_end:
2683 if (ibp >= limit)
2684 error_with_line (line_for_error (start_line),
2685 "unterminated comment");
2686 else {
2687 ibp++;
2688 if (put_out_comments) {
2689 bcopy (before_bp, obp, ibp - before_bp);
2690 obp += ibp - before_bp;
2694 break;
2696 case '$':
2697 if (!dollars_in_ident)
2698 goto randomchar;
2699 goto letter;
2701 case '0': case '1': case '2': case '3': case '4':
2702 case '5': case '6': case '7': case '8': case '9':
2703 /* If digit is not part of identifier, it starts a number,
2704 which means that following letters are not an identifier.
2705 "0x5" does not refer to an identifier "x5".
2706 So copy all alphanumerics that follow without accumulating
2707 as an identifier. Periods also, for sake of "3.e7". */
2709 if (ident_length == 0) {
2710 while (ibp < limit) {
2711 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
2712 ++ip->lineno;
2713 ibp += 2;
2715 c = *ibp++;
2716 /* ".." terminates a preprocessing number. This is useless for C
2717 code but useful for preprocessing other things. */
2718 if (!isalnum (c) && (c != '.' || *ibp == '.') && c != '_') {
2719 --ibp;
2720 break;
2722 *obp++ = c;
2723 /* A sign can be part of a preprocessing number
2724 if it follows an e. */
2725 if (c == 'e' || c == 'E') {
2726 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
2727 ++ip->lineno;
2728 ibp += 2;
2730 if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
2731 *obp++ = *ibp++;
2732 /* But traditional C does not let the token go past the sign. */
2733 if (traditional)
2734 break;
2738 break;
2740 /* fall through */
2742 case '_':
2743 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2744 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2745 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2746 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2747 case 'y': case 'z':
2748 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2749 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
2750 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2751 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2752 case 'Y': case 'Z':
2753 letter:
2754 ident_length++;
2755 /* Compute step of hash function, to avoid a proc call on every token */
2756 hash = HASHSTEP (hash, c);
2757 break;
2759 case '\n':
2760 if (ip->fname == 0 && *ibp == '-') {
2761 /* Newline - inhibits expansion of preceding token.
2762 If expanding a macro arg, we keep the newline -.
2763 In final output, it is deleted.
2764 We recognize Newline - in macro bodies and macro args. */
2765 if (! concatenated) {
2766 ident_length = 0;
2767 hash = 0;
2769 ibp++;
2770 if (!output_marks) {
2771 obp--;
2772 } else {
2773 /* If expanding a macro arg, keep the newline -. */
2774 *obp++ = '-';
2776 break;
2779 /* If reprocessing a macro expansion, newline is a special marker. */
2780 else if (ip->macro != 0) {
2781 /* Newline White is a "funny space" to separate tokens that are
2782 supposed to be separate but without space between.
2783 Here White means any whitespace character.
2784 Newline - marks a recursive macro use that is not
2785 supposed to be expandable. */
2787 if (is_space[*ibp]) {
2788 /* Newline Space does not prevent expansion of preceding token
2789 so expand the preceding token and then come back. */
2790 if (ident_length > 0)
2791 goto specialchar;
2793 /* If generating final output, newline space makes a space. */
2794 if (!output_marks) {
2795 obp[-1] = *ibp++;
2796 /* And Newline Newline makes a newline, so count it. */
2797 if (obp[-1] == '\n')
2798 op->lineno++;
2799 } else {
2800 /* If expanding a macro arg, keep the newline space.
2801 If the arg gets stringified, newline space makes nothing. */
2802 *obp++ = *ibp++;
2804 } else abort (); /* Newline followed by something random? */
2805 break;
2808 /* If there is a pending identifier, handle it and come back here. */
2809 if (ident_length > 0)
2810 goto specialchar;
2812 beg_of_line = ibp;
2814 /* Update the line counts and output a #line if necessary. */
2815 ++ip->lineno;
2816 ++op->lineno;
2817 if (ip->lineno != op->lineno) {
2818 op->bufp = obp;
2819 output_line_command (ip, op, 1, same_file);
2820 check_expand (op, ip->length - (ip->bufp - ip->buf));
2821 obp = op->bufp;
2823 break;
2825 /* Come here either after (1) a null character that is part of the input
2826 or (2) at the end of the input, because there is a null there. */
2827 case 0:
2828 if (ibp <= limit)
2829 /* Our input really contains a null character. */
2830 goto randomchar;
2832 /* At end of a macro-expansion level, pop it and read next level. */
2833 if (ip->macro != 0) {
2834 obp--;
2835 ibp--;
2836 /* If traditional, and we have an identifier that ends here,
2837 process it now, so we get the right error for recursion. */
2838 if (traditional && ident_length
2839 && ! is_idchar[*instack[indepth - 1].bufp]) {
2840 redo_char = 1;
2841 goto randomchar;
2843 POPMACRO;
2844 RECACHE;
2845 break;
2848 /* If we don't have a pending identifier,
2849 return at end of input. */
2850 if (ident_length == 0) {
2851 obp--;
2852 ibp--;
2853 op->bufp = obp;
2854 ip->bufp = ibp;
2855 goto ending;
2858 /* If we do have a pending identifier, just consider this null
2859 a special character and arrange to dispatch on it again.
2860 The second time, IDENT_LENGTH will be zero so we will return. */
2862 /* Fall through */
2864 specialchar:
2866 /* Handle the case of a character such as /, ', " or null
2867 seen following an identifier. Back over it so that
2868 after the identifier is processed the special char
2869 will be dispatched on again. */
2871 ibp--;
2872 obp--;
2873 redo_char = 1;
2875 default:
2877 randomchar:
2879 if (ident_length > 0) {
2880 register HASHNODE *hp;
2882 /* We have just seen an identifier end. If it's a macro, expand it.
2884 IDENT_LENGTH is the length of the identifier
2885 and HASH is its hash code.
2887 The identifier has already been copied to the output,
2888 so if it is a macro we must remove it.
2890 If REDO_CHAR is 0, the char that terminated the identifier
2891 has been skipped in the output and the input.
2892 OBP-IDENT_LENGTH-1 points to the identifier.
2893 If the identifier is a macro, we must back over the terminator.
2895 If REDO_CHAR is 1, the terminating char has already been
2896 backed over. OBP-IDENT_LENGTH points to the identifier. */
2898 if (!pcp_outfile || pcp_inside_if) {
2899 startagain:
2900 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
2901 hp = hp->next) {
2903 if (hp->length == ident_length) {
2904 int obufp_before_macroname;
2905 int op_lineno_before_macroname;
2906 register int i = ident_length;
2907 register U_CHAR *p = hp->name;
2908 register U_CHAR *q = obp - i;
2909 int disabled;
2911 if (! redo_char)
2912 q--;
2914 do { /* All this to avoid a strncmp () */
2915 if (*p++ != *q++)
2916 goto hashcollision;
2917 } while (--i);
2919 /* We found a use of a macro name.
2920 see if the context shows it is a macro call. */
2922 /* Back up over terminating character if not already done. */
2923 if (! redo_char) {
2924 ibp--;
2925 obp--;
2928 /* Save this as a displacement from the beginning of the output
2929 buffer. We can not save this as a position in the output
2930 buffer, because it may get realloc'ed by RECACHE. */
2931 obufp_before_macroname = (obp - op->buf) - ident_length;
2932 op_lineno_before_macroname = op->lineno;
2934 if (hp->type == T_PCSTRING) {
2935 pcstring_used (hp); /* Mark the definition of this key
2936 as needed, ensuring that it
2937 will be output. */
2938 break; /* Exit loop, since the key cannot have a
2939 definition any longer. */
2942 /* Record whether the macro is disabled. */
2943 disabled = hp->type == T_DISABLED;
2945 /* This looks like a macro ref, but if the macro was disabled,
2946 just copy its name and put in a marker if requested. */
2948 if (disabled) {
2949 #if 0
2950 /* This error check caught useful cases such as
2951 #define foo(x,y) bar (x (y,0), y)
2952 foo (foo, baz) */
2953 if (traditional)
2954 error ("recursive use of macro `%s'", hp->name);
2955 #endif
2957 if (output_marks) {
2958 check_expand (op, limit - ibp + 2);
2959 *obp++ = '\n';
2960 *obp++ = '-';
2962 break;
2965 /* If macro wants an arglist, verify that a '(' follows.
2966 first skip all whitespace, copying it to the output
2967 after the macro name. Then, if there is no '(',
2968 decide this is not a macro call and leave things that way. */
2969 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
2970 && hp->value.defn->nargs >= 0)
2972 U_CHAR *old_ibp = ibp;
2973 U_CHAR *old_obp = obp;
2974 int old_iln = ip->lineno;
2975 int old_oln = op->lineno;
2977 while (1) {
2978 /* Scan forward over whitespace, copying it to the output. */
2979 if (ibp == limit && ip->macro != 0) {
2980 POPMACRO;
2981 RECACHE;
2982 old_ibp = ibp;
2983 old_obp = obp;
2984 old_iln = ip->lineno;
2985 old_oln = op->lineno;
2987 /* A comment: copy it unchanged or discard it. */
2988 else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
2989 if (put_out_comments) {
2990 *obp++ = '/';
2991 *obp++ = '*';
2992 } else if (! traditional) {
2993 *obp++ = ' ';
2995 ibp += 2;
2996 while (ibp + 1 != limit
2997 && !(ibp[0] == '*' && ibp[1] == '/')) {
2998 /* We need not worry about newline-marks,
2999 since they are never found in comments. */
3000 if (*ibp == '\n') {
3001 /* Newline in a file. Count it. */
3002 ++ip->lineno;
3003 ++op->lineno;
3005 if (put_out_comments)
3006 *obp++ = *ibp++;
3007 else
3008 ibp++;
3010 ibp += 2;
3011 if (put_out_comments) {
3012 *obp++ = '*';
3013 *obp++ = '/';
3016 else if (is_space[*ibp]) {
3017 *obp++ = *ibp++;
3018 if (ibp[-1] == '\n') {
3019 if (ip->macro == 0) {
3020 /* Newline in a file. Count it. */
3021 ++ip->lineno;
3022 ++op->lineno;
3023 } else if (!output_marks) {
3024 /* A newline mark, and we don't want marks
3025 in the output. If it is newline-hyphen,
3026 discard it entirely. Otherwise, it is
3027 newline-whitechar, so keep the whitechar. */
3028 obp--;
3029 if (*ibp == '-')
3030 ibp++;
3031 else {
3032 if (*ibp == '\n')
3033 ++op->lineno;
3034 *obp++ = *ibp++;
3036 } else {
3037 /* A newline mark; copy both chars to the output. */
3038 *obp++ = *ibp++;
3042 else break;
3044 if (*ibp != '(') {
3045 /* It isn't a macro call.
3046 Put back the space that we just skipped. */
3047 ibp = old_ibp;
3048 obp = old_obp;
3049 ip->lineno = old_iln;
3050 op->lineno = old_oln;
3051 /* Exit the for loop. */
3052 break;
3056 /* This is now known to be a macro call.
3057 Discard the macro name from the output,
3058 along with any following whitespace just copied. */
3059 obp = op->buf + obufp_before_macroname;
3060 op->lineno = op_lineno_before_macroname;
3062 /* Prevent accidental token-pasting with a character
3063 before the macro call. */
3064 if (!traditional && obp != op->buf
3065 && (obp[-1] == '-' || obp[1] == '+' || obp[1] == '&'
3066 || obp[-1] == '|' || obp[1] == '<' || obp[1] == '>')) {
3067 /* If we are expanding a macro arg, make a newline marker
3068 to separate the tokens. If we are making real output,
3069 a plain space will do. */
3070 if (output_marks)
3071 *obp++ = '\n';
3072 *obp++ = ' ';
3075 /* Expand the macro, reading arguments as needed,
3076 and push the expansion on the input stack. */
3077 ip->bufp = ibp;
3078 op->bufp = obp;
3079 macroexpand (hp, op);
3081 /* Reexamine input stack, since macroexpand has pushed
3082 a new level on it. */
3083 obp = op->bufp;
3084 RECACHE;
3085 break;
3087 hashcollision:
3089 } /* End hash-table-search loop */
3091 ident_length = hash = 0; /* Stop collecting identifier */
3092 redo_char = 0;
3093 concatenated = 0;
3094 } /* End if (ident_length > 0) */
3095 } /* End switch */
3096 } /* End per-char loop */
3098 /* Come here to return -- but first give an error message
3099 if there was an unterminated successful conditional. */
3100 ending:
3101 if (if_stack != ip->if_stack) {
3102 char *str;
3103 switch (if_stack->type) {
3104 case T_IF:
3105 str = "if";
3106 break;
3107 case T_IFDEF:
3108 str = "ifdef";
3109 break;
3110 case T_IFNDEF:
3111 str = "ifndef";
3112 break;
3113 case T_ELSE:
3114 str = "else";
3115 break;
3116 case T_ELIF:
3117 str = "elif";
3118 break;
3120 error_with_line (line_for_error (if_stack->lineno),
3121 "unterminated `#%s' conditional", str);
3123 if_stack = ip->if_stack;
3127 * Rescan a string into a temporary buffer and return the result
3128 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3130 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3131 * and insert such markers when appropriate. See `rescan' for details.
3132 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3133 * before substitution; it is 0 for other uses.
3135 static FILE_BUF
3136 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3137 U_CHAR *buf, *limit;
3138 int output_marks, assertions;
3140 register FILE_BUF *ip;
3141 FILE_BUF obuf;
3142 int length = limit - buf;
3143 U_CHAR *buf1;
3144 int odepth = indepth;
3145 int save_assertions_flag = assertions_flag;
3147 assertions_flag = assertions;
3149 if (length < 0)
3150 abort ();
3152 /* Set up the input on the input stack. */
3154 buf1 = (U_CHAR *) alloca (length + 1);
3156 register U_CHAR *p1 = buf;
3157 register U_CHAR *p2 = buf1;
3159 while (p1 != limit)
3160 *p2++ = *p1++;
3162 buf1[length] = 0;
3164 /* Set up to receive the output. */
3166 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3167 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3168 obuf.fname = 0;
3169 obuf.macro = 0;
3170 obuf.free_ptr = 0;
3172 CHECK_DEPTH ({return obuf;});
3174 ++indepth;
3176 ip = &instack[indepth];
3177 ip->fname = 0;
3178 ip->nominal_fname = 0;
3179 ip->system_header_p = 0;
3180 ip->macro = 0;
3181 ip->free_ptr = 0;
3182 ip->length = length;
3183 ip->buf = ip->bufp = buf1;
3184 ip->if_stack = if_stack;
3186 ip->lineno = obuf.lineno = 1;
3188 /* Scan the input, create the output. */
3189 rescan (&obuf, output_marks);
3191 /* Pop input stack to original state. */
3192 --indepth;
3194 if (indepth != odepth)
3195 abort ();
3197 /* Record the output. */
3198 obuf.length = obuf.bufp - obuf.buf;
3200 assertions_flag = save_assertions_flag;
3201 return obuf;
3205 * Process a # directive. Expects IP->bufp to point after the '#', as in
3206 * `#define foo bar'. Passes to the command handler
3207 * (do_define, do_include, etc.): the addresses of the 1st and
3208 * last chars of the command (starting immediately after the #
3209 * keyword), plus op and the keyword table pointer. If the command
3210 * contains comments it is copied into a temporary buffer sans comments
3211 * and the temporary buffer is passed to the command handler instead.
3212 * Likewise for backslash-newlines.
3214 * Returns nonzero if this was a known # directive.
3215 * Otherwise, returns zero, without advancing the input pointer.
3218 static int
3219 handle_directive (ip, op)
3220 FILE_BUF *ip, *op;
3222 register U_CHAR *bp, *cp;
3223 register struct directive *kt;
3224 register int ident_length;
3225 U_CHAR *resume_p;
3227 /* Nonzero means we must copy the entire command
3228 to get rid of comments or backslash-newlines. */
3229 int copy_command = 0;
3231 U_CHAR *ident, *after_ident;
3233 bp = ip->bufp;
3235 /* Record where the directive started. do_xifdef needs this. */
3236 directive_start = bp - 1;
3238 /* Skip whitespace and \-newline. */
3239 while (1) {
3240 if (is_hor_space[*bp]) {
3241 if ((*bp == '\f' || *bp == '\v') && pedantic)
3242 pedwarn ("%s in preprocessing directive",
3243 *bp == '\f' ? "formfeed" : "vertical tab");
3244 bp++;
3245 } else if (*bp == '/' && bp[1] == '*') {
3246 ip->bufp = bp;
3247 skip_to_end_of_comment (ip, &ip->lineno, 0);
3248 bp = ip->bufp;
3249 } else if (*bp == '\\' && bp[1] == '\n') {
3250 bp += 2; ip->lineno++;
3251 } else break;
3254 /* Now find end of directive name.
3255 If we encounter a backslash-newline, exchange it with any following
3256 symbol-constituents so that we end up with a contiguous name. */
3258 cp = bp;
3259 while (1) {
3260 if (is_idchar[*cp])
3261 cp++;
3262 else {
3263 if (*cp == '\\' && cp[1] == '\n')
3264 name_newline_fix (cp);
3265 if (is_idchar[*cp])
3266 cp++;
3267 else break;
3270 ident_length = cp - bp;
3271 ident = bp;
3272 after_ident = cp;
3274 /* A line of just `#' becomes blank. */
3276 if (ident_length == 0 && *after_ident == '\n') {
3277 ip->bufp = after_ident;
3278 return 1;
3281 if (ident_length == 0 || !is_idstart[*ident]) {
3282 U_CHAR *p = ident;
3283 while (is_idchar[*p]) {
3284 if (*p < '0' || *p > '9')
3285 break;
3286 p++;
3288 /* Handle # followed by a line number. */
3289 if (p != ident && !is_idchar[*p]) {
3290 static struct directive line_directive_table[] = {
3291 { 4, do_line, "line", T_LINE},
3293 if (pedantic)
3294 pedwarn ("`#' followed by integer");
3295 after_ident = ident;
3296 kt = line_directive_table;
3297 goto old_linenum;
3300 /* Avoid error for `###' and similar cases unless -pedantic. */
3301 if (p == ident) {
3302 while (*p == '#' || is_hor_space[*p]) p++;
3303 if (*p == '\n') {
3304 if (pedantic && !lang_asm)
3305 warning ("invalid preprocessor directive");
3306 return 0;
3310 if (!lang_asm)
3311 error ("invalid preprocessor directive name");
3313 return 0;
3317 * Decode the keyword and call the appropriate expansion
3318 * routine, after moving the input pointer up to the next line.
3320 for (kt = directive_table; kt->length > 0; kt++) {
3321 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
3322 register U_CHAR *buf;
3323 register U_CHAR *limit;
3324 int unterminated;
3325 int junk;
3326 int *already_output;
3328 /* Nonzero means do not delete comments within the directive.
3329 #define needs this when -traditional. */
3330 int keep_comments;
3332 old_linenum:
3334 limit = ip->buf + ip->length;
3335 unterminated = 0;
3336 already_output = 0;
3337 keep_comments = traditional && kt->traditional_comments;
3338 /* #import is defined only in Objective C, or when on the NeXT. */
3339 if (kt->type == T_IMPORT && !(objc || lookup ("__NeXT__", -1, -1)))
3340 break;
3342 /* Find the end of this command (first newline not backslashed
3343 and not in a string or comment).
3344 Set COPY_COMMAND if the command must be copied
3345 (it contains a backslash-newline or a comment). */
3347 buf = bp = after_ident;
3348 while (bp < limit) {
3349 register U_CHAR c = *bp++;
3350 switch (c) {
3351 case '\\':
3352 if (bp < limit) {
3353 if (*bp == '\n') {
3354 ip->lineno++;
3355 copy_command = 1;
3357 bp++;
3359 break;
3361 case '\'':
3362 case '\"':
3363 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command, &unterminated);
3364 /* Don't bother calling the directive if we already got an error
3365 message due to unterminated string. Skip everything and pretend
3366 we called the directive. */
3367 if (unterminated) {
3368 if (traditional) {
3369 /* Traditional preprocessing permits unterminated strings. */
3370 ip->bufp = bp;
3371 goto endloop1;
3373 ip->bufp = bp;
3374 return 1;
3376 break;
3378 /* <...> is special for #include. */
3379 case '<':
3380 if (!kt->angle_brackets)
3381 break;
3382 while (*bp && *bp != '>') bp++;
3383 break;
3385 case '/':
3386 if (*bp == '\\' && bp[1] == '\n')
3387 newline_fix (bp);
3388 if (*bp == '*'
3389 || (cplusplus_comments && *bp == '/')) {
3390 U_CHAR *obp = bp - 1;
3391 ip->bufp = bp + 1;
3392 skip_to_end_of_comment (ip, &ip->lineno, 0);
3393 bp = ip->bufp;
3394 /* No need to copy the command because of a comment at the end;
3395 just don't include the comment in the directive. */
3396 if (bp == limit || *bp == '\n') {
3397 bp = obp;
3398 goto endloop1;
3400 /* Don't remove the comments if -traditional. */
3401 if (! keep_comments)
3402 copy_command++;
3404 break;
3406 case '\f':
3407 case '\v':
3408 if (pedantic)
3409 pedwarn ("%s in preprocessing directive",
3410 c == '\f' ? "formfeed" : "vertical tab");
3411 break;
3413 case '\n':
3414 --bp; /* Point to the newline */
3415 ip->bufp = bp;
3416 goto endloop1;
3419 ip->bufp = bp;
3421 endloop1:
3422 resume_p = ip->bufp;
3423 /* BP is the end of the directive.
3424 RESUME_P is the next interesting data after the directive.
3425 A comment may come between. */
3427 /* If a directive should be copied through, and -E was given,
3428 pass it through before removing comments. */
3429 if (!no_output && kt->pass_thru && put_out_comments) {
3430 int len;
3432 /* Output directive name. */
3433 check_expand (op, kt->length + 2);
3434 /* Make sure # is at the start of a line */
3435 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
3436 op->lineno++;
3437 *op->bufp++ = '\n';
3439 *op->bufp++ = '#';
3440 bcopy (kt->name, op->bufp, kt->length);
3441 op->bufp += kt->length;
3443 /* Output arguments. */
3444 len = (bp - buf);
3445 check_expand (op, len);
3446 bcopy (buf, op->bufp, len);
3447 op->bufp += len;
3448 /* Take account of any (escaped) newlines just output. */
3449 while (--len >= 0)
3450 if (buf[len] == '\n')
3451 op->lineno++;
3453 already_output = &junk;
3454 } /* Don't we need a newline or #line? */
3456 if (copy_command) {
3457 register U_CHAR *xp = buf;
3458 /* Need to copy entire command into temp buffer before dispatching */
3460 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
3461 some slop */
3462 buf = cp;
3464 /* Copy to the new buffer, deleting comments
3465 and backslash-newlines (and whitespace surrounding the latter). */
3467 while (xp < bp) {
3468 register U_CHAR c = *xp++;
3469 *cp++ = c;
3471 switch (c) {
3472 case '\n':
3473 abort (); /* A bare newline should never part of the line. */
3474 break;
3476 /* <...> is special for #include. */
3477 case '<':
3478 if (!kt->angle_brackets)
3479 break;
3480 while (xp < bp && c != '>') {
3481 c = *xp++;
3482 if (c == '\\' && xp < bp && *xp == '\n')
3483 xp++;
3484 else
3485 *cp++ = c;
3487 break;
3489 case '\\':
3490 if (*xp == '\n') {
3491 xp++;
3492 cp--;
3493 if (cp != buf && is_space[cp[-1]]) {
3494 while (cp != buf && is_space[cp[-1]]) cp--;
3495 cp++;
3496 SKIP_WHITE_SPACE (xp);
3497 } else if (is_space[*xp]) {
3498 *cp++ = *xp++;
3499 SKIP_WHITE_SPACE (xp);
3501 } else {
3502 *cp++ = *xp++;
3504 break;
3506 case '\'':
3507 case '\"':
3509 register U_CHAR *bp1
3510 = skip_quoted_string (xp - 1, bp, ip->lineno,
3511 NULL_PTR, NULL_PTR, NULL_PTR);
3512 while (xp != bp1)
3513 if (*xp == '\\') {
3514 if (*++xp != '\n')
3515 *cp++ = '\\';
3516 else
3517 xp++;
3518 } else
3519 *cp++ = *xp++;
3521 break;
3523 case '/':
3524 if (*xp == '*'
3525 || (cplusplus_comments && *xp == '/')) {
3526 ip->bufp = xp + 1;
3527 /* If we already copied the command through,
3528 already_output != 0 prevents outputting comment now. */
3529 skip_to_end_of_comment (ip, already_output, 0);
3530 if (keep_comments)
3531 while (xp != ip->bufp)
3532 *cp++ = *xp++;
3533 /* Delete or replace the slash. */
3534 else if (traditional)
3535 cp--;
3536 else
3537 cp[-1] = ' ';
3538 xp = ip->bufp;
3543 /* Null-terminate the copy. */
3545 *cp = 0;
3546 } else
3547 cp = bp;
3549 ip->bufp = resume_p;
3551 /* Some directives should be written out for cc1 to process,
3552 just as if they were not defined. And sometimes we're copying
3553 definitions through. */
3555 if (!no_output && already_output == 0
3556 && (kt->pass_thru
3557 || (kt->type == T_DEFINE
3558 && (dump_macros == dump_names
3559 || dump_macros == dump_definitions)))) {
3560 int len;
3562 /* Output directive name. */
3563 check_expand (op, kt->length + 1);
3564 *op->bufp++ = '#';
3565 bcopy (kt->name, op->bufp, kt->length);
3566 op->bufp += kt->length;
3568 if (kt->pass_thru || dump_macros == dump_definitions) {
3569 /* Output arguments. */
3570 len = (cp - buf);
3571 check_expand (op, len);
3572 bcopy (buf, op->bufp, len);
3573 op->bufp += len;
3574 } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
3575 U_CHAR *xp = buf;
3576 U_CHAR *yp;
3577 SKIP_WHITE_SPACE (xp);
3578 yp = xp;
3579 while (is_idchar[*xp]) xp++;
3580 len = (xp - yp);
3581 check_expand (op, len + 1);
3582 *op->bufp++ = ' ';
3583 bcopy (yp, op->bufp, len);
3584 op->bufp += len;
3586 } /* Don't we need a newline or #line? */
3588 /* Call the appropriate command handler. buf now points to
3589 either the appropriate place in the input buffer, or to
3590 the temp buffer if it was necessary to make one. cp
3591 points to the first char after the contents of the (possibly
3592 copied) command, in either case. */
3593 (*kt->func) (buf, cp, op, kt);
3594 check_expand (op, ip->length - (ip->bufp - ip->buf));
3596 return 1;
3600 /* It is deliberate that we don't warn about undefined directives.
3601 That is the responsibility of cc1. */
3602 return 0;
3605 static struct tm *
3606 timestamp ()
3608 static struct tm *timebuf;
3609 if (!timebuf) {
3610 time_t t = time (0);
3611 timebuf = localtime (&t);
3613 return timebuf;
3616 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3617 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3621 * expand things like __FILE__. Place the expansion into the output
3622 * buffer *without* rescanning.
3625 static void
3626 special_symbol (hp, op)
3627 HASHNODE *hp;
3628 FILE_BUF *op;
3630 char *buf;
3631 int i, len;
3632 int true_indepth;
3633 FILE_BUF *ip = NULL;
3634 struct tm *timebuf;
3636 int paren = 0; /* For special `defined' keyword */
3638 if (pcp_outfile && pcp_inside_if
3639 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
3640 error ("Predefined macro `%s' used inside `#if' during precompilation",
3641 hp->name);
3643 for (i = indepth; i >= 0; i--)
3644 if (instack[i].fname != NULL) {
3645 ip = &instack[i];
3646 break;
3648 if (ip == NULL) {
3649 error ("cccp error: not in any file?!");
3650 return; /* the show must go on */
3653 switch (hp->type) {
3654 case T_FILE:
3655 case T_BASE_FILE:
3657 char *string;
3658 if (hp->type == T_FILE)
3659 string = ip->nominal_fname;
3660 else
3661 string = instack[0].nominal_fname;
3663 if (string)
3665 buf = (char *) alloca (3 + strlen (string));
3666 sprintf (buf, "\"%s\"", string);
3668 else
3669 buf = "\"\"";
3671 break;
3674 case T_INCLUDE_LEVEL:
3675 true_indepth = 0;
3676 for (i = indepth; i >= 0; i--)
3677 if (instack[i].fname != NULL)
3678 true_indepth++;
3680 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
3681 sprintf (buf, "%d", true_indepth - 1);
3682 break;
3684 case T_VERSION:
3685 buf = (char *) alloca (3 + strlen (version_string));
3686 sprintf (buf, "\"%s\"", version_string);
3687 break;
3689 case T_SIZE_TYPE:
3690 buf = (char *) alloca (3 + strlen (SIZE_TYPE));
3691 sprintf (buf, "%s", SIZE_TYPE);
3692 break;
3694 case T_PTRDIFF_TYPE:
3695 buf = (char *) alloca (3 + strlen (PTRDIFF_TYPE));
3696 sprintf (buf, "%s", PTRDIFF_TYPE);
3697 break;
3699 case T_WCHAR_TYPE:
3700 buf = (char *) alloca (3 + strlen (WCHAR_TYPE));
3701 sprintf (buf, "%s", WCHAR_TYPE);
3702 break;
3704 case T_USER_LABEL_PREFIX_TYPE:
3705 buf = (char *) alloca (3 + strlen (USER_LABEL_PREFIX));
3706 sprintf (buf, "%s", USER_LABEL_PREFIX);
3707 break;
3709 case T_REGISTER_PREFIX_TYPE:
3710 buf = (char *) alloca (3 + strlen (REGISTER_PREFIX));
3711 sprintf (buf, "%s", REGISTER_PREFIX);
3712 break;
3714 case T_CONST:
3715 buf = (char *) alloca (4 * sizeof (int));
3716 sprintf (buf, "%d", hp->value.ival);
3717 if (pcp_inside_if && pcp_outfile)
3718 /* Output a precondition for this macro use */
3719 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
3720 break;
3722 case T_SPECLINE:
3723 buf = (char *) alloca (10);
3724 sprintf (buf, "%d", ip->lineno);
3725 break;
3727 case T_DATE:
3728 case T_TIME:
3729 buf = (char *) alloca (20);
3730 timebuf = timestamp ();
3731 if (hp->type == T_DATE)
3732 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3733 timebuf->tm_mday, timebuf->tm_year + 1900);
3734 else
3735 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3736 timebuf->tm_sec);
3737 break;
3739 case T_SPEC_DEFINED:
3740 buf = " 0 "; /* Assume symbol is not defined */
3741 ip = &instack[indepth];
3742 SKIP_WHITE_SPACE (ip->bufp);
3743 if (*ip->bufp == '(') {
3744 paren++;
3745 ip->bufp++; /* Skip over the paren */
3746 SKIP_WHITE_SPACE (ip->bufp);
3749 if (!is_idstart[*ip->bufp])
3750 goto oops;
3751 if (hp = lookup (ip->bufp, -1, -1)) {
3752 if (pcp_outfile && pcp_inside_if
3753 && hp->value.defn->predefined)
3754 /* Output a precondition for this macro use. */
3755 fprintf (pcp_outfile, "#define %s\n", hp->name);
3756 buf = " 1 ";
3758 else
3759 if (pcp_outfile && pcp_inside_if) {
3760 /* Output a precondition for this macro use */
3761 U_CHAR *cp = ip->bufp;
3762 fprintf (pcp_outfile, "#undef ");
3763 while (is_idchar[*cp]) /* Ick! */
3764 fputc (*cp++, pcp_outfile);
3765 putc ('\n', pcp_outfile);
3767 while (is_idchar[*ip->bufp])
3768 ++ip->bufp;
3769 SKIP_WHITE_SPACE (ip->bufp);
3770 if (paren) {
3771 if (*ip->bufp != ')')
3772 goto oops;
3773 ++ip->bufp;
3775 break;
3777 oops:
3779 error ("`defined' without an identifier");
3780 break;
3782 default:
3783 error ("cccp error: invalid special hash type"); /* time for gdb */
3784 abort ();
3786 len = strlen (buf);
3787 check_expand (op, len);
3788 bcopy (buf, op->bufp, len);
3789 op->bufp += len;
3791 return;
3795 /* Routines to handle #directives */
3797 /* Handle #include and #import.
3798 This function expects to see "fname" or <fname> on the input. */
3800 static int
3801 do_include (buf, limit, op, keyword)
3802 U_CHAR *buf, *limit;
3803 FILE_BUF *op;
3804 struct directive *keyword;
3806 int importing = (keyword->type == T_IMPORT);
3807 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3808 static int import_warning = 0;
3809 char *fname; /* Dynamically allocated fname buffer */
3810 char *pcftry;
3811 char *pcfname;
3812 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3814 struct file_name_list *search_start = include; /* Chain of dirs to search */
3815 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3816 struct file_name_list *searchptr = 0;
3817 int flen;
3819 int f; /* file number */
3821 int retried = 0; /* Have already tried macro
3822 expanding the include line*/
3823 FILE_BUF trybuf; /* It got expanded into here */
3824 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3825 int pcf = -1;
3826 char *pcfbuf;
3827 int pcfbuflimit;
3828 int pcfnum;
3829 f= -1; /* JF we iz paranoid! */
3831 if (importing && warn_import && !inhibit_warnings
3832 && !instack[indepth].system_header_p && !import_warning) {
3833 import_warning = 1;
3834 warning ("using `#import' is not recommended");
3835 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3836 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3837 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3838 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3839 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3840 fprintf (stderr, " ... <real contents of file> ...\n");
3841 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3842 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3843 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3844 fprintf (stderr, "when it is equipped with such a conditional.\n");
3847 get_filename:
3849 fbeg = buf;
3850 SKIP_WHITE_SPACE (fbeg);
3851 /* Discard trailing whitespace so we can easily see
3852 if we have parsed all the significant chars we were given. */
3853 while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
3855 switch (*fbeg++) {
3856 case '\"':
3858 FILE_BUF *fp;
3859 /* Copy the operand text, concatenating the strings. */
3861 U_CHAR *fin = fbeg;
3862 fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
3863 fend = fbeg;
3864 while (fin != limit) {
3865 while (fin != limit && *fin != '\"')
3866 *fend++ = *fin++;
3867 fin++;
3868 if (fin == limit)
3869 break;
3870 /* If not at the end, there had better be another string. */
3871 /* Skip just horiz space, and don't go past limit. */
3872 while (fin != limit && is_hor_space[*fin]) fin++;
3873 if (fin != limit && *fin == '\"')
3874 fin++;
3875 else
3876 goto fail;
3879 *fend = 0;
3881 /* We have "filename". Figure out directory this source
3882 file is coming from and put it on the front of the list. */
3884 /* If -I- was specified, don't search current dir, only spec'd ones. */
3885 if (ignore_srcdir) break;
3887 for (fp = &instack[indepth]; fp >= instack; fp--)
3889 int n;
3890 char *ep,*nam;
3892 if ((nam = fp->nominal_fname) != NULL) {
3893 /* Found a named file. Figure out dir of the file,
3894 and put it in front of the search list. */
3895 dsp[0].next = search_start;
3896 search_start = dsp;
3897 #ifndef VMS
3898 ep = rindex (nam, '/');
3899 #else /* VMS */
3900 ep = rindex (nam, ']');
3901 if (ep == NULL) ep = rindex (nam, '>');
3902 if (ep == NULL) ep = rindex (nam, ':');
3903 if (ep != NULL) ep++;
3904 #endif /* VMS */
3905 if (ep != NULL) {
3906 n = ep - nam;
3907 dsp[0].fname = (char *) alloca (n + 1);
3908 strncpy (dsp[0].fname, nam, n);
3909 dsp[0].fname[n] = '\0';
3910 if (n + INCLUDE_LEN_FUDGE > max_include_len)
3911 max_include_len = n + INCLUDE_LEN_FUDGE;
3912 } else {
3913 dsp[0].fname = 0; /* Current directory */
3915 break;
3918 break;
3921 case '<':
3922 fend = fbeg;
3923 while (fend != limit && *fend != '>') fend++;
3924 if (*fend == '>' && fend + 1 == limit) {
3925 angle_brackets = 1;
3926 /* If -I-, start with the first -I dir after the -I-. */
3927 if (first_bracket_include)
3928 search_start = first_bracket_include;
3929 break;
3931 goto fail;
3933 default:
3934 fail:
3935 if (retried) {
3936 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3937 return 0;
3938 } else {
3939 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
3940 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
3941 bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
3942 limit = buf + (trybuf.bufp - trybuf.buf);
3943 free (trybuf.buf);
3944 retried++;
3945 goto get_filename;
3949 /* For #include_next, skip in the search path
3950 past the dir in which the containing file was found. */
3951 if (skip_dirs) {
3952 FILE_BUF *fp;
3953 for (fp = &instack[indepth]; fp >= instack; fp--)
3954 if (fp->fname != NULL) {
3955 /* fp->dir is null if the containing file was specified
3956 with an absolute file name. In that case, don't skip anything. */
3957 if (fp->dir)
3958 search_start = fp->dir->next;
3959 break;
3963 flen = fend - fbeg;
3965 if (flen == 0)
3967 error ("empty file name in `#%s'", keyword->name);
3968 return 0;
3971 /* Allocate this permanently, because it gets stored in the definitions
3972 of macros. */
3973 fname = (char *) xmalloc (max_include_len + flen + 2);
3974 /* + 2 above for slash and terminating null. */
3976 /* If specified file name is absolute, just open it. */
3978 if (*fbeg == '/') {
3979 strncpy (fname, fbeg, flen);
3980 fname[flen] = 0;
3981 if (redundant_include_p (fname))
3982 return 0;
3983 if (importing)
3984 f = lookup_import (fname);
3985 else
3986 f = open (fname, O_RDONLY, 0666);
3987 if (f == -2)
3988 return 0; /* Already included this file */
3989 } else {
3990 /* Search directory path, trying to open the file.
3991 Copy each filename tried into FNAME. */
3993 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3994 if (searchptr->fname) {
3995 /* The empty string in a search path is ignored.
3996 This makes it possible to turn off entirely
3997 a standard piece of the list. */
3998 if (searchptr->fname[0] == 0)
3999 continue;
4000 strcpy (fname, searchptr->fname);
4001 strcat (fname, "/");
4002 fname[strlen (fname) + flen] = 0;
4003 } else {
4004 fname[0] = 0;
4006 strncat (fname, fbeg, flen);
4007 #ifdef VMS
4008 /* Change this 1/2 Unix 1/2 VMS file specification into a
4009 full VMS file specification */
4010 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4011 /* Fix up the filename */
4012 hack_vms_include_specification (fname);
4013 } else {
4014 /* This is a normal VMS filespec, so use it unchanged. */
4015 strncpy (fname, fbeg, flen);
4016 fname[flen] = 0;
4018 #endif /* VMS */
4019 if (importing)
4020 f = lookup_import (fname);
4021 else
4022 f = open (fname, O_RDONLY, 0666);
4023 if (f == -2)
4024 return 0; /* Already included this file */
4025 #ifdef EACCES
4026 else if (f == -1 && errno == EACCES)
4027 warning ("Header file %s exists, but is not readable", fname);
4028 #endif
4029 if (redundant_include_p (fname)) {
4030 close (f);
4031 return 0;
4033 if (f >= 0)
4034 break;
4038 if (f < 0) {
4039 /* A file that was not found. */
4041 strncpy (fname, fbeg, flen);
4042 fname[flen] = 0;
4043 /* If -M was specified, and this header file won't be added to the
4044 dependency list, then don't count this as an error, because we can
4045 still produce correct output. Otherwise, we can't produce correct
4046 output, because there may be dependencies we need inside the missing
4047 file, and we don't know what directory this missing file exists in. */
4048 if (print_deps
4049 && (print_deps <= (angle_brackets || (system_include_depth > 0))))
4050 warning ("No include path in which to find %s", fname);
4051 else if (search_start)
4052 error_from_errno (fname);
4053 else
4054 error ("No include path in which to find %s", fname);
4055 } else {
4056 struct stat stat_f;
4058 /* Check to see if this include file is a once-only include file.
4059 If so, give up. */
4061 struct file_name_list* ptr;
4063 for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
4064 if (!strcmp (ptr->fname, fname)) {
4065 close (f);
4066 return 0; /* This file was once'd. */
4070 for (ptr = all_include_files; ptr; ptr = ptr->next) {
4071 if (!strcmp (ptr->fname, fname))
4072 break; /* This file was included before. */
4075 if (ptr == 0) {
4076 /* This is the first time for this file. */
4077 /* Add it to list of files included. */
4079 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
4080 ptr->control_macro = 0;
4081 ptr->next = all_include_files;
4082 all_include_files = ptr;
4083 ptr->fname = savestring (fname);
4085 /* For -M, add this file to the dependencies. */
4086 if (print_deps > (angle_brackets || (system_include_depth > 0))) {
4087 deps_output ("", 0);
4088 deps_output (fname, 0);
4089 deps_output (" ", 0);
4093 /* Handle -H option. */
4094 if (print_include_names)
4095 fprintf (stderr, "%s\n", fname);
4097 if (angle_brackets)
4098 system_include_depth++;
4100 /* Actually process the file. */
4101 add_import (f, fname); /* Record file on "seen" list for #import. */
4103 pcftry = (char *) alloca (strlen (fname) + 30);
4104 pcfbuf = 0;
4105 pcfnum = 0;
4107 fstat (f, &stat_f);
4109 if (!no_precomp)
4110 do {
4111 sprintf (pcftry, "%s%d", fname, pcfnum++);
4113 pcf = open (pcftry, O_RDONLY, 0666);
4114 if (pcf != -1)
4116 struct stat s;
4118 fstat (pcf, &s);
4119 if (bcmp (&stat_f.st_ino, &s.st_ino, sizeof (s.st_ino))
4120 || stat_f.st_dev != s.st_dev)
4122 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
4123 /* Don't need it any more. */
4124 close (pcf);
4126 else
4128 /* Don't need it at all. */
4129 close (pcf);
4130 break;
4133 } while (pcf != -1 && !pcfbuf);
4135 /* Actually process the file */
4136 if (pcfbuf) {
4137 pcfname = xmalloc (strlen (pcftry) + 1);
4138 strcpy (pcfname, pcftry);
4139 pcfinclude (pcfbuf, pcfbuflimit, fname, op);
4141 else
4142 finclude (f, fname, op, is_system_include (fname), searchptr);
4144 if (angle_brackets)
4145 system_include_depth--;
4147 return 0;
4150 /* Return nonzero if there is no need to include file NAME
4151 because it has already been included and it contains a conditional
4152 to make a repeated include do nothing. */
4154 static int
4155 redundant_include_p (name)
4156 char *name;
4158 struct file_name_list *l = all_include_files;
4159 for (; l; l = l->next)
4160 if (! strcmp (name, l->fname)
4161 && l->control_macro
4162 && lookup (l->control_macro, -1, -1))
4163 return 1;
4164 return 0;
4167 /* Return nonzero if the given FILENAME is an absolute pathname which
4168 designates a file within one of the known "system" include file
4169 directories. We assume here that if the given FILENAME looks like
4170 it is the name of a file which resides either directly in a "system"
4171 include file directory, or within any subdirectory thereof, then the
4172 given file must be a "system" include file. This function tells us
4173 if we should suppress pedantic errors/warnings for the given FILENAME. */
4175 static int
4176 is_system_include (filename)
4177 register char *filename;
4179 struct file_name_list *searchptr;
4181 for (searchptr = first_system_include; searchptr;
4182 searchptr = searchptr->next)
4183 if (searchptr->fname) {
4184 register char *sys_dir = searchptr->fname;
4185 register unsigned length = strlen (sys_dir);
4187 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
4188 return 1;
4190 return 0;
4193 /* Process the contents of include file FNAME, already open on descriptor F,
4194 with output to OP.
4195 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
4196 "system" include directories (as decided by the `is_system_include'
4197 function above).
4198 DIRPTR is the link in the dir path through which this file was found,
4199 or 0 if the file name was absolute. */
4201 static void
4202 finclude (f, fname, op, system_header_p, dirptr)
4203 int f;
4204 char *fname;
4205 FILE_BUF *op;
4206 int system_header_p;
4207 struct file_name_list *dirptr;
4209 int st_mode;
4210 long st_size;
4211 long i;
4212 FILE_BUF *fp; /* For input stack frame */
4213 int missing_newline = 0;
4215 CHECK_DEPTH (return;);
4217 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
4219 perror_with_name (fname);
4220 close (f);
4221 return;
4224 fp = &instack[indepth + 1];
4225 bzero (fp, sizeof (FILE_BUF));
4226 fp->nominal_fname = fp->fname = fname;
4227 fp->length = 0;
4228 fp->lineno = 1;
4229 fp->if_stack = if_stack;
4230 fp->system_header_p = system_header_p;
4231 fp->dir = dirptr;
4233 if (S_ISREG (st_mode)) {
4234 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
4235 fp->bufp = fp->buf;
4237 /* Read the file contents, knowing that st_size is an upper bound
4238 on the number of bytes we can read. */
4239 while (st_size > 0) {
4240 i = read (f, fp->buf + fp->length, st_size);
4241 if (i <= 0) {
4242 if (i == 0) break;
4243 goto nope;
4245 fp->length += i;
4246 st_size -= i;
4249 else if (S_ISDIR (st_mode)) {
4250 error ("directory `%s' specified in #include", fname);
4251 close (f);
4252 return;
4253 } else {
4254 /* Cannot count its file size before reading.
4255 First read the entire file into heap and
4256 copy them into buffer on stack. */
4258 U_CHAR *bufp;
4259 U_CHAR *basep;
4260 int bsize = 2000;
4262 st_size = 0;
4263 basep = (U_CHAR *) xmalloc (bsize + 2);
4264 fp->buf = basep; /* So it will get freed, on error. */
4265 bufp = basep;
4267 for (;;) {
4268 i = read (f, bufp, bsize - st_size);
4269 if (i < 0)
4270 goto nope; /* error! */
4271 if (i == 0)
4272 break; /* End of file */
4273 st_size += i;
4274 bufp += i;
4275 if (bsize == st_size) { /* Buffer is full! */
4276 bsize *= 2;
4277 basep = (U_CHAR *) xrealloc (basep, bsize + 2);
4278 fp->buf = basep;
4279 bufp = basep + st_size; /* May have moved */
4282 fp->bufp = fp->buf;
4283 fp->length = st_size;
4286 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
4287 /* Backslash-newline at end is not good enough. */
4288 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
4289 fp->buf[fp->length++] = '\n';
4290 missing_newline = 1;
4292 fp->buf[fp->length] = '\0';
4294 /* Close descriptor now, so nesting does not use lots of descriptors. */
4295 close (f);
4297 /* Must do this before calling trigraph_pcp, so that the correct file name
4298 will be printed in warning messages. */
4300 indepth++;
4301 input_file_stack_tick++;
4303 if (!no_trigraphs)
4304 trigraph_pcp (fp);
4306 output_line_command (fp, op, 0, enter_file);
4307 rescan (op, 0);
4309 if (missing_newline)
4310 fp->lineno--;
4312 if (pedantic && missing_newline)
4313 pedwarn ("file does not end in newline");
4315 indepth--;
4316 input_file_stack_tick++;
4317 output_line_command (&instack[indepth], op, 0, leave_file);
4318 free (fp->buf);
4319 return;
4321 nope:
4323 perror_with_name (fname);
4324 close (f);
4325 free (fp->buf);
4328 /* Record that inclusion of the file named FILE
4329 should be controlled by the macro named MACRO_NAME.
4330 This means that trying to include the file again
4331 will do something if that macro is defined. */
4333 static void
4334 record_control_macro (file, macro_name)
4335 char *file;
4336 U_CHAR *macro_name;
4338 struct file_name_list *new;
4340 for (new = all_include_files; new; new = new->next) {
4341 if (!strcmp (new->fname, file)) {
4342 new->control_macro = macro_name;
4343 return;
4347 /* If the file is not in all_include_files, something's wrong. */
4348 abort ();
4351 /* Maintain and search list of included files, for #import. */
4353 #define IMPORT_HASH_SIZE 31
4355 struct import_file {
4356 char *name;
4357 ino_t inode;
4358 dev_t dev;
4359 struct import_file *next;
4362 /* Hash table of files already included with #include or #import. */
4364 static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
4366 /* Hash a file name for import_hash_table. */
4368 static int
4369 import_hash (f)
4370 char *f;
4372 int val = 0;
4374 while (*f) val += *f++;
4375 return (val%IMPORT_HASH_SIZE);
4378 /* Search for file FILENAME in import_hash_table.
4379 Return -2 if found, either a matching name or a matching inode.
4380 Otherwise, open the file and return a file descriptor if successful
4381 or -1 if unsuccessful. */
4383 static int
4384 lookup_import (filename)
4385 char *filename;
4387 struct import_file *i;
4388 int h;
4389 int hashval;
4390 struct stat sb;
4391 int fd;
4393 hashval = import_hash (filename);
4395 /* Attempt to find file in list of already included files */
4396 i = import_hash_table[hashval];
4398 while (i) {
4399 if (!strcmp (filename, i->name))
4400 return -2; /* return found */
4401 i = i->next;
4403 /* Open it and try a match on inode/dev */
4404 fd = open (filename, O_RDONLY, 0666);
4405 if (fd < 0)
4406 return fd;
4407 fstat (fd, &sb);
4408 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
4409 i = import_hash_table[h];
4410 while (i) {
4411 /* Compare the inode and the device.
4412 Supposedly on some systems the inode is not a scalar. */
4413 if (!bcmp (&i->inode, &sb.st_ino, sizeof (sb.st_ino))
4414 && i->dev == sb.st_dev) {
4415 close (fd);
4416 return -2; /* return found */
4418 i = i->next;
4421 return fd; /* Not found, return open file */
4424 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
4426 static void
4427 add_import (fd, fname)
4428 int fd;
4429 char *fname;
4431 struct import_file *i;
4432 int hashval;
4433 struct stat sb;
4435 hashval = import_hash (fname);
4436 fstat (fd, &sb);
4437 i = (struct import_file *)xmalloc (sizeof (struct import_file));
4438 i->name = (char *)xmalloc (strlen (fname)+1);
4439 strcpy (i->name, fname);
4440 bcopy (&sb.st_ino, &i->inode, sizeof (sb.st_ino));
4441 i->dev = sb.st_dev;
4442 i->next = import_hash_table[hashval];
4443 import_hash_table[hashval] = i;
4446 /* Load the specified precompiled header into core, and verify its
4447 preconditions. PCF indicates the file descriptor to read, which must
4448 be a regular file. FNAME indicates the file name of the original
4449 header. *LIMIT will be set to an address one past the end of the file.
4450 If the preconditions of the file are not satisfied, the buffer is
4451 freed and we return 0. If the preconditions are satisfied, return
4452 the address of the buffer following the preconditions. The buffer, in
4453 this case, should never be freed because various pieces of it will
4454 be referred to until all precompiled strings are output at the end of
4455 the run.
4457 static char *
4458 check_precompiled (pcf, fname, limit)
4459 int pcf;
4460 char *fname;
4461 char **limit;
4463 int st_mode;
4464 long st_size;
4465 int length = 0;
4466 char *buf;
4467 char *dollar_loc;
4468 int i;
4469 char *cp;
4471 if (pcp_outfile)
4472 return 0;
4474 if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
4475 return 0;
4477 if (S_ISREG (st_mode))
4479 buf = xmalloc (st_size + 2);
4480 while (st_size > 0)
4482 i = read (pcf, buf + length, st_size);
4483 if (i < 0)
4484 goto nope;
4485 if (i == 0)
4486 break;
4487 length += i;
4488 st_size -= i;
4491 else
4492 abort ();
4494 if (length > 0 && buf[length-1] != '\n')
4495 buf[length++] = '\n';
4496 buf[length] = '\0';
4498 *limit = buf + length;
4500 /* File is in core. Check the preconditions. */
4501 if (!check_preconditions (buf))
4502 goto nope;
4503 for (cp = buf; *cp; cp++)
4505 #ifdef DEBUG_PCP
4506 fprintf (stderr, "Using preinclude %s\n", fname);
4507 #endif
4508 return cp + 1;
4510 nope:
4511 #ifdef DEBUG_PCP
4512 fprintf (stderr, "Cannot use preinclude %s\n", fname);
4513 #endif
4514 free (buf);
4515 return 0;
4518 /* PREC (null terminated) points to the preconditions of a
4519 precompiled header. These are a series of #define and #undef
4520 lines which must match the current contents of the hash
4521 table. */
4522 static int
4523 check_preconditions (prec)
4524 char *prec;
4526 MACRODEF mdef;
4527 char *lineend;
4529 while (*prec) {
4530 lineend = (char *) index (prec, '\n');
4532 if (*prec++ != '#') {
4533 error ("Bad format encountered while reading precompiled file");
4534 return 0;
4536 if (!strncmp (prec, "define", 6)) {
4537 HASHNODE *hp;
4539 prec += 6;
4540 mdef = create_definition (prec, lineend, NULL_PTR);
4542 if (mdef.defn == 0)
4543 abort ();
4545 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
4546 || (hp->type != T_MACRO && hp->type != T_CONST)
4547 || (hp->type == T_MACRO
4548 && !compare_defs (mdef.defn, hp->value.defn)
4549 && (mdef.defn->length != 2
4550 || mdef.defn->expansion[0] != '\n'
4551 || mdef.defn->expansion[1] != ' ')))
4552 return 0;
4553 } else if (!strncmp (prec, "undef", 5)) {
4554 char *name;
4555 int len;
4557 prec += 5;
4558 while (is_hor_space[(U_CHAR) *prec])
4559 prec++;
4560 name = prec;
4561 while (is_idchar[(U_CHAR) *prec])
4562 prec++;
4563 len = prec - name;
4565 if (lookup (name, len, -1))
4566 return 0;
4567 } else {
4568 error ("Bad format encountered while reading precompiled file");
4569 return 0;
4571 prec = lineend + 1;
4573 /* They all passed successfully */
4574 return 1;
4577 /* Process the main body of a precompiled file. BUF points to the
4578 string section of the file, following the preconditions. LIMIT is one
4579 character past the end. NAME is the name of the file being read
4580 in. OP is the main output buffer */
4581 static void
4582 pcfinclude (buf, limit, name, op)
4583 U_CHAR *buf, *limit, *name;
4584 FILE_BUF *op;
4586 FILE_BUF tmpbuf;
4587 int nstrings;
4588 U_CHAR *cp = buf;
4590 /* First in the file comes 4 bytes indicating the number of strings, */
4591 /* in network byte order. (MSB first). */
4592 nstrings = *cp++;
4593 nstrings = (nstrings << 8) | *cp++;
4594 nstrings = (nstrings << 8) | *cp++;
4595 nstrings = (nstrings << 8) | *cp++;
4597 /* Looping over each string... */
4598 while (nstrings--) {
4599 U_CHAR *string_start;
4600 U_CHAR *endofthiskey;
4601 STRINGDEF *str;
4602 int nkeys;
4604 /* Each string starts with a STRINGDEF structure (str), followed */
4605 /* by the text of the string (string_start) */
4607 /* First skip to a longword boundary */
4608 /* ??? Why a 4-byte boundary? On all machines? */
4609 /* NOTE: This works correctly even if HOST_WIDE_INT
4610 is narrower than a pointer.
4611 Do not try risky measures here to get another type to use!
4612 Do not include gstddef.h or stddef.h--either one will fail! */
4613 if ((HOST_WIDE_INT) cp & 3)
4614 cp += 4 - ((HOST_WIDE_INT) cp & 3);
4616 /* Now get the string. */
4617 str = (STRINGDEF *) cp;
4618 string_start = cp += sizeof (STRINGDEF);
4620 for (; *cp; cp++) /* skip the string */
4623 /* We need to macro expand the string here to ensure that the
4624 proper definition environment is in place. If it were only
4625 expanded when we find out it is needed, macros necessary for
4626 its proper expansion might have had their definitions changed. */
4627 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
4628 /* Lineno is already set in the precompiled file */
4629 str->contents = tmpbuf.buf;
4630 str->len = tmpbuf.length;
4631 str->writeflag = 0;
4632 str->filename = name;
4633 str->output_mark = outbuf.bufp - outbuf.buf;
4635 str->chain = 0;
4636 *stringlist_tailp = str;
4637 stringlist_tailp = &str->chain;
4639 /* Next comes a fourbyte number indicating the number of keys */
4640 /* for this string. */
4641 nkeys = *cp++;
4642 nkeys = (nkeys << 8) | *cp++;
4643 nkeys = (nkeys << 8) | *cp++;
4644 nkeys = (nkeys << 8) | *cp++;
4646 /* If this number is -1, then the string is mandatory. */
4647 if (nkeys == -1)
4648 str->writeflag = 1;
4649 else
4650 /* Otherwise, for each key, */
4651 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
4652 KEYDEF *kp = (KEYDEF *) cp;
4653 HASHNODE *hp;
4655 /* It starts with a KEYDEF structure */
4656 cp += sizeof (KEYDEF);
4658 /* Find the end of the key. At the end of this for loop we
4659 advance CP to the start of the next key using this variable. */
4660 endofthiskey = cp + strlen (cp);
4661 kp->str = str;
4663 /* Expand the key, and enter it into the hash table. */
4664 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
4665 tmpbuf.bufp = tmpbuf.buf;
4667 while (is_hor_space[*tmpbuf.bufp])
4668 tmpbuf.bufp++;
4669 if (!is_idstart[*tmpbuf.bufp]
4670 || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
4671 str->writeflag = 1;
4672 continue;
4675 hp = lookup (tmpbuf.bufp, -1, -1);
4676 if (hp == NULL) {
4677 kp->chain = 0;
4678 install (tmpbuf.bufp, -1, T_PCSTRING, 0, (char *) kp, -1);
4680 else if (hp->type == T_PCSTRING) {
4681 kp->chain = hp->value.keydef;
4682 hp->value.keydef = kp;
4684 else
4685 str->writeflag = 1;
4688 /* This output_line_command serves to switch us back to the current
4689 input file in case some of these strings get output (which will
4690 result in line commands for the header file being output). */
4691 output_line_command (&instack[indepth], op, 0, enter_file);
4694 /* Called from rescan when it hits a key for strings. Mark them all */
4695 /* used and clean up. */
4696 static void
4697 pcstring_used (hp)
4698 HASHNODE *hp;
4700 KEYDEF *kp, *tmp;
4702 for (kp = hp->value.keydef; kp; kp = kp->chain)
4703 kp->str->writeflag = 1;
4704 delete_macro (hp);
4707 /* Write the output, interspersing precompiled strings in their */
4708 /* appropriate places. */
4709 static void
4710 write_output ()
4712 STRINGDEF *next_string;
4713 U_CHAR *cur_buf_loc;
4714 int line_command_len = 80;
4715 char *line_command = xmalloc (line_command_len);
4716 int len;
4718 /* In each run through the loop, either cur_buf_loc == */
4719 /* next_string_loc, in which case we print a series of strings, or */
4720 /* it is less than next_string_loc, in which case we write some of */
4721 /* the buffer. */
4722 cur_buf_loc = outbuf.buf;
4723 next_string = stringlist;
4725 while (cur_buf_loc < outbuf.bufp || next_string) {
4726 if (next_string
4727 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
4728 if (next_string->writeflag) {
4729 len = strlen (next_string->filename);
4730 if (len > line_command_len)
4731 line_command = xrealloc (line_command,
4732 line_command_len *= 2);
4733 sprintf (line_command, "\n# %d \"%s\"\n",
4734 next_string->lineno, next_string->filename);
4735 if (write (fileno (stdout), line_command, strlen (line_command)) < 0)
4736 pfatal_with_name (out_fname);
4737 if (write (fileno (stdout), next_string->contents, next_string->len) < 0)
4738 pfatal_with_name (out_fname);
4740 next_string = next_string->chain;
4742 else {
4743 len = (next_string
4744 ? (next_string->output_mark
4745 - (cur_buf_loc - outbuf.buf))
4746 : outbuf.bufp - cur_buf_loc);
4748 if (write (fileno (stdout), cur_buf_loc, len) < len)
4749 pfatal_with_name (out_fname);
4750 cur_buf_loc += len;
4753 free (line_command);
4756 /* Pass a directive through to the output file.
4757 BUF points to the contents of the directive, as a contiguous string.
4758 LIMIT points to the first character past the end of the directive.
4759 KEYWORD is the keyword-table entry for the directive. */
4761 static void
4762 pass_thru_directive (buf, limit, op, keyword)
4763 U_CHAR *buf, *limit;
4764 FILE_BUF *op;
4765 struct directive *keyword;
4767 register unsigned keyword_length = keyword->length;
4769 check_expand (op, 1 + keyword_length + (limit - buf));
4770 *op->bufp++ = '#';
4771 bcopy (keyword->name, op->bufp, keyword_length);
4772 op->bufp += keyword_length;
4773 if (limit != buf && buf[0] != ' ')
4774 *op->bufp++ = ' ';
4775 bcopy (buf, op->bufp, limit - buf);
4776 op->bufp += (limit - buf);
4777 #if 0
4778 *op->bufp++ = '\n';
4779 /* Count the line we have just made in the output,
4780 to get in sync properly. */
4781 op->lineno++;
4782 #endif
4785 /* The arglist structure is built by do_define to tell
4786 collect_definition where the argument names begin. That
4787 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
4788 would contain pointers to the strings x, y, and z.
4789 Collect_definition would then build a DEFINITION node,
4790 with reflist nodes pointing to the places x, y, and z had
4791 appeared. So the arglist is just convenience data passed
4792 between these two routines. It is not kept around after
4793 the current #define has been processed and entered into the
4794 hash table. */
4796 struct arglist {
4797 struct arglist *next;
4798 U_CHAR *name;
4799 int length;
4800 int argno;
4801 char rest_args;
4804 /* Create a DEFINITION node from a #define directive. Arguments are
4805 as for do_define. */
4806 static MACRODEF
4807 create_definition (buf, limit, op)
4808 U_CHAR *buf, *limit;
4809 FILE_BUF *op;
4811 U_CHAR *bp; /* temp ptr into input buffer */
4812 U_CHAR *symname; /* remember where symbol name starts */
4813 int sym_length; /* and how long it is */
4814 int line = instack[indepth].lineno;
4815 char *file = instack[indepth].nominal_fname;
4816 int rest_args = 0;
4818 DEFINITION *defn;
4819 int arglengths = 0; /* Accumulate lengths of arg names
4820 plus number of args. */
4821 MACRODEF mdef;
4823 bp = buf;
4825 while (is_hor_space[*bp])
4826 bp++;
4828 symname = bp; /* remember where it starts */
4829 sym_length = check_macro_name (bp, "macro");
4830 bp += sym_length;
4832 /* Lossage will occur if identifiers or control keywords are broken
4833 across lines using backslash. This is not the right place to take
4834 care of that. */
4836 if (*bp == '(') {
4837 struct arglist *arg_ptrs = NULL;
4838 int argno = 0;
4840 bp++; /* skip '(' */
4841 SKIP_WHITE_SPACE (bp);
4843 /* Loop over macro argument names. */
4844 while (*bp != ')') {
4845 struct arglist *temp;
4847 temp = (struct arglist *) alloca (sizeof (struct arglist));
4848 temp->name = bp;
4849 temp->next = arg_ptrs;
4850 temp->argno = argno++;
4851 temp->rest_args = 0;
4852 arg_ptrs = temp;
4854 if (rest_args)
4855 pedwarn ("another parameter follows `%s'",
4856 rest_extension);
4858 if (!is_idstart[*bp])
4859 pedwarn ("invalid character in macro parameter name");
4861 /* Find the end of the arg name. */
4862 while (is_idchar[*bp]) {
4863 bp++;
4864 /* do we have a "special" rest-args extension here? */
4865 if (limit - bp > REST_EXTENSION_LENGTH &&
4866 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
4867 rest_args = 1;
4868 temp->rest_args = 1;
4869 break;
4872 temp->length = bp - temp->name;
4873 if (rest_args == 1)
4874 bp += REST_EXTENSION_LENGTH;
4875 arglengths += temp->length + 2;
4876 SKIP_WHITE_SPACE (bp);
4877 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
4878 error ("badly punctuated parameter list in `#define'");
4879 goto nope;
4881 if (*bp == ',') {
4882 bp++;
4883 SKIP_WHITE_SPACE (bp);
4885 if (bp >= limit) {
4886 error ("unterminated parameter list in `#define'");
4887 goto nope;
4890 struct arglist *otemp;
4892 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
4893 if (temp->length == otemp->length &&
4894 strncmp (temp->name, otemp->name, temp->length) == 0) {
4895 U_CHAR *name;
4897 name = (U_CHAR *) alloca (temp->length + 1);
4898 (void) strncpy (name, temp->name, temp->length);
4899 name[temp->length] = '\0';
4900 error ("duplicate argument name `%s' in `#define'", name);
4901 goto nope;
4906 ++bp; /* skip paren */
4907 /* Skip exactly one space or tab if any. */
4908 if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
4909 /* now everything from bp before limit is the definition. */
4910 defn = collect_expansion (bp, limit, argno, arg_ptrs);
4911 defn->rest_args = rest_args;
4913 /* Now set defn->args.argnames to the result of concatenating
4914 the argument names in reverse order
4915 with comma-space between them. */
4916 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
4918 struct arglist *temp;
4919 int i = 0;
4920 for (temp = arg_ptrs; temp; temp = temp->next) {
4921 bcopy (temp->name, &defn->args.argnames[i], temp->length);
4922 i += temp->length;
4923 if (temp->next != 0) {
4924 defn->args.argnames[i++] = ',';
4925 defn->args.argnames[i++] = ' ';
4928 defn->args.argnames[i] = 0;
4930 } else {
4931 /* simple expansion or empty definition; gobble it */
4932 if (is_hor_space[*bp])
4933 ++bp; /* skip exactly one blank/tab char */
4934 /* now everything from bp before limit is the definition. */
4935 defn = collect_expansion (bp, limit, -1, NULL_PTR);
4936 defn->args.argnames = (U_CHAR *) "";
4939 defn->line = line;
4940 defn->file = file;
4942 /* OP is null if this is a predefinition */
4943 defn->predefined = !op;
4944 mdef.defn = defn;
4945 mdef.symnam = symname;
4946 mdef.symlen = sym_length;
4948 return mdef;
4950 nope:
4951 mdef.defn = 0;
4952 return mdef;
4955 /* Process a #define command.
4956 BUF points to the contents of the #define command, as a contiguous string.
4957 LIMIT points to the first character past the end of the definition.
4958 KEYWORD is the keyword-table entry for #define. */
4960 static int
4961 do_define (buf, limit, op, keyword)
4962 U_CHAR *buf, *limit;
4963 FILE_BUF *op;
4964 struct directive *keyword;
4966 int hashcode;
4967 MACRODEF mdef;
4969 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
4970 if (pcp_outfile && op)
4971 pass_thru_directive (buf, limit, op, keyword);
4973 mdef = create_definition (buf, limit, op);
4974 if (mdef.defn == 0)
4975 goto nope;
4977 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
4980 HASHNODE *hp;
4981 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
4982 int ok = 0;
4983 /* Redefining a precompiled key is ok. */
4984 if (hp->type == T_PCSTRING)
4985 ok = 1;
4986 /* Redefining a macro is ok if the definitions are the same. */
4987 else if (hp->type == T_MACRO)
4988 ok = ! compare_defs (mdef.defn, hp->value.defn);
4989 /* Redefining a constant is ok with -D. */
4990 else if (hp->type == T_CONST)
4991 ok = ! done_initializing;
4992 /* Print the warning if it's not ok. */
4993 if (!ok) {
4994 U_CHAR *msg; /* what pain... */
4996 /* If we are passing through #define and #undef directives, do
4997 that for this re-definition now. */
4998 if (debug_output && op)
4999 pass_thru_directive (buf, limit, op, keyword);
5001 msg = (U_CHAR *) alloca (mdef.symlen + 22);
5002 *msg = '`';
5003 bcopy (mdef.symnam, msg + 1, mdef.symlen);
5004 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
5005 pedwarn (msg);
5006 if (hp->type == T_MACRO)
5007 pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
5008 "this is the location of the previous definition");
5010 /* Replace the old definition. */
5011 hp->type = T_MACRO;
5012 hp->value.defn = mdef.defn;
5013 } else {
5014 /* If we are passing through #define and #undef directives, do
5015 that for this new definition now. */
5016 if (debug_output && op)
5017 pass_thru_directive (buf, limit, op, keyword);
5018 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
5019 (char *) mdef.defn, hashcode);
5023 return 0;
5025 nope:
5027 return 1;
5030 /* Check a purported macro name SYMNAME, and yield its length.
5031 USAGE is the kind of name this is intended for. */
5033 static int
5034 check_macro_name (symname, usage)
5035 U_CHAR *symname;
5036 char *usage;
5038 U_CHAR *p;
5039 int sym_length;
5041 for (p = symname; is_idchar[*p]; p++)
5043 sym_length = p - symname;
5044 if (sym_length == 0)
5045 error ("invalid %s name", usage);
5046 else if (!is_idstart[*symname]) {
5047 U_CHAR *msg; /* what pain... */
5048 msg = (U_CHAR *) alloca (sym_length + 1);
5049 bcopy (symname, msg, sym_length);
5050 msg[sym_length] = 0;
5051 error ("invalid %s name `%s'", usage, msg);
5052 } else {
5053 if (! strncmp (symname, "defined", 7) && sym_length == 7)
5054 error ("invalid %s name `defined'", usage);
5056 return sym_length;
5060 * return zero if two DEFINITIONs are isomorphic
5062 static int
5063 compare_defs (d1, d2)
5064 DEFINITION *d1, *d2;
5066 register struct reflist *a1, *a2;
5067 register U_CHAR *p1 = d1->expansion;
5068 register U_CHAR *p2 = d2->expansion;
5069 int first = 1;
5071 if (d1->nargs != d2->nargs)
5072 return 1;
5073 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
5074 return 1;
5075 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
5076 a1 = a1->next, a2 = a2->next) {
5077 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
5078 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
5079 || a1->argno != a2->argno
5080 || a1->stringify != a2->stringify
5081 || a1->raw_before != a2->raw_before
5082 || a1->raw_after != a2->raw_after)
5083 return 1;
5084 first = 0;
5085 p1 += a1->nchars;
5086 p2 += a2->nchars;
5088 if (a1 != a2)
5089 return 1;
5090 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
5091 p2, d2->length - (p2 - d2->expansion), 1))
5092 return 1;
5093 return 0;
5096 /* Return 1 if two parts of two macro definitions are effectively different.
5097 One of the parts starts at BEG1 and has LEN1 chars;
5098 the other has LEN2 chars at BEG2.
5099 Any sequence of whitespace matches any other sequence of whitespace.
5100 FIRST means these parts are the first of a macro definition;
5101 so ignore leading whitespace entirely.
5102 LAST means these parts are the last of a macro definition;
5103 so ignore trailing whitespace entirely. */
5105 static int
5106 comp_def_part (first, beg1, len1, beg2, len2, last)
5107 int first;
5108 U_CHAR *beg1, *beg2;
5109 int len1, len2;
5110 int last;
5112 register U_CHAR *end1 = beg1 + len1;
5113 register U_CHAR *end2 = beg2 + len2;
5114 if (first) {
5115 while (beg1 != end1 && is_space[*beg1]) beg1++;
5116 while (beg2 != end2 && is_space[*beg2]) beg2++;
5118 if (last) {
5119 while (beg1 != end1 && is_space[end1[-1]]) end1--;
5120 while (beg2 != end2 && is_space[end2[-1]]) end2--;
5122 while (beg1 != end1 && beg2 != end2) {
5123 if (is_space[*beg1] && is_space[*beg2]) {
5124 while (beg1 != end1 && is_space[*beg1]) beg1++;
5125 while (beg2 != end2 && is_space[*beg2]) beg2++;
5126 } else if (*beg1 == *beg2) {
5127 beg1++; beg2++;
5128 } else break;
5130 return (beg1 != end1) || (beg2 != end2);
5133 /* Read a replacement list for a macro with parameters.
5134 Build the DEFINITION structure.
5135 Reads characters of text starting at BUF until END.
5136 ARGLIST specifies the formal parameters to look for
5137 in the text of the definition; NARGS is the number of args
5138 in that list, or -1 for a macro name that wants no argument list.
5139 MACRONAME is the macro name itself (so we can avoid recursive expansion)
5140 and NAMELEN is its length in characters.
5142 Note that comments and backslash-newlines have already been deleted
5143 from the argument. */
5145 /* Leading and trailing Space, Tab, etc. are converted to markers
5146 Newline Space, Newline Tab, etc.
5147 Newline Space makes a space in the final output
5148 but is discarded if stringified. (Newline Tab is similar but
5149 makes a Tab instead.)
5151 If there is no trailing whitespace, a Newline Space is added at the end
5152 to prevent concatenation that would be contrary to the standard. */
5154 static DEFINITION *
5155 collect_expansion (buf, end, nargs, arglist)
5156 U_CHAR *buf, *end;
5157 int nargs;
5158 struct arglist *arglist;
5160 DEFINITION *defn;
5161 register U_CHAR *p, *limit, *lastp, *exp_p;
5162 struct reflist *endpat = NULL;
5163 /* Pointer to first nonspace after last ## seen. */
5164 U_CHAR *concat = 0;
5165 /* Pointer to first nonspace after last single-# seen. */
5166 U_CHAR *stringify = 0;
5167 int maxsize;
5168 int expected_delimiter = '\0';
5170 /* Scan thru the replacement list, ignoring comments and quoted
5171 strings, picking up on the macro calls. It does a linear search
5172 thru the arg list on every potential symbol. Profiling might say
5173 that something smarter should happen. */
5175 if (end < buf)
5176 abort ();
5178 /* Find the beginning of the trailing whitespace. */
5179 /* Find end of leading whitespace. */
5180 limit = end;
5181 p = buf;
5182 while (p < limit && is_space[limit[-1]]) limit--;
5183 while (p < limit && is_space[*p]) p++;
5185 /* Allocate space for the text in the macro definition.
5186 Leading and trailing whitespace chars need 2 bytes each.
5187 Each other input char may or may not need 1 byte,
5188 so this is an upper bound.
5189 The extra 2 are for invented trailing newline-marker and final null. */
5190 maxsize = (sizeof (DEFINITION)
5191 + 2 * (end - limit) + 2 * (p - buf)
5192 + (limit - p) + 3);
5193 defn = (DEFINITION *) xcalloc (1, maxsize);
5195 defn->nargs = nargs;
5196 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
5197 lastp = exp_p;
5199 p = buf;
5201 /* Convert leading whitespace to Newline-markers. */
5202 while (p < limit && is_space[*p]) {
5203 *exp_p++ = '\n';
5204 *exp_p++ = *p++;
5207 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
5208 error ("`##' at start of macro definition");
5209 p += 2;
5212 /* Process the main body of the definition. */
5213 while (p < limit) {
5214 int skipped_arg = 0;
5215 register U_CHAR c = *p++;
5217 *exp_p++ = c;
5219 if (!traditional) {
5220 switch (c) {
5221 case '\'':
5222 case '\"':
5223 if (expected_delimiter != '\0') {
5224 if (c == expected_delimiter)
5225 expected_delimiter = '\0';
5226 } else
5227 expected_delimiter = c;
5228 break;
5230 /* Special hack: if a \# is written in the #define
5231 include a # in the definition. This is useless for C code
5232 but useful for preprocessing other things. */
5234 case '\\':
5235 /* \# quotes a # even outside of strings. */
5236 if (p < limit && *p == '#' && !expected_delimiter) {
5237 exp_p--;
5238 *exp_p++ = *p++;
5239 } else if (p < limit && expected_delimiter) {
5240 /* In a string, backslash goes through
5241 and makes next char ordinary. */
5242 *exp_p++ = *p++;
5244 break;
5246 case '#':
5247 /* # is ordinary inside a string. */
5248 if (expected_delimiter)
5249 break;
5250 if (p < limit && *p == '#') {
5251 /* ##: concatenate preceding and following tokens. */
5252 /* Take out the first #, discard preceding whitespace. */
5253 exp_p--;
5254 while (exp_p > lastp && is_hor_space[exp_p[-1]])
5255 --exp_p;
5256 /* Skip the second #. */
5257 p++;
5258 /* Discard following whitespace. */
5259 SKIP_WHITE_SPACE (p);
5260 concat = p;
5261 if (p == limit)
5262 error ("`##' at end of macro definition");
5263 } else if (nargs >= 0) {
5264 /* Single #: stringify following argument ref.
5265 Don't leave the # in the expansion. */
5266 exp_p--;
5267 SKIP_WHITE_SPACE (p);
5268 if (p == limit || ! is_idstart[*p])
5269 error ("`#' operator is not followed by a macro argument name");
5270 else
5271 stringify = p;
5273 break;
5275 } else {
5276 /* In -traditional mode, recognize arguments inside strings and
5277 and character constants, and ignore special properties of #.
5278 Arguments inside strings are considered "stringified", but no
5279 extra quote marks are supplied. */
5280 switch (c) {
5281 case '\'':
5282 case '\"':
5283 if (expected_delimiter != '\0') {
5284 if (c == expected_delimiter)
5285 expected_delimiter = '\0';
5286 } else
5287 expected_delimiter = c;
5288 break;
5290 case '\\':
5291 /* Backslash quotes delimiters and itself, but not macro args. */
5292 if (expected_delimiter != 0 && p < limit
5293 && (*p == expected_delimiter || *p == '\\')) {
5294 *exp_p++ = *p++;
5295 continue;
5297 break;
5299 case '/':
5300 if (expected_delimiter != '\0') /* No comments inside strings. */
5301 break;
5302 if (*p == '*') {
5303 /* If we find a comment that wasn't removed by handle_directive,
5304 this must be -traditional. So replace the comment with
5305 nothing at all. */
5306 exp_p--;
5307 p += 1;
5308 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
5309 p++;
5310 #if 0
5311 /* Mark this as a concatenation-point, as if it had been ##. */
5312 concat = p;
5313 #endif
5315 break;
5319 /* Handle the start of a symbol. */
5320 if (is_idchar[c] && nargs > 0) {
5321 U_CHAR *id_beg = p - 1;
5322 int id_len;
5324 --exp_p;
5325 while (p != limit && is_idchar[*p]) p++;
5326 id_len = p - id_beg;
5328 if (is_idstart[c]) {
5329 register struct arglist *arg;
5331 for (arg = arglist; arg != NULL; arg = arg->next) {
5332 struct reflist *tpat;
5334 if (arg->name[0] == c
5335 && arg->length == id_len
5336 && strncmp (arg->name, id_beg, id_len) == 0) {
5337 if (expected_delimiter && warn_stringify) {
5338 if (traditional) {
5339 warning ("macro argument `%.*s' is stringified.",
5340 id_len, arg->name);
5341 } else {
5342 warning ("macro arg `%.*s' would be stringified with -traditional.",
5343 id_len, arg->name);
5346 /* If ANSI, don't actually substitute inside a string. */
5347 if (!traditional && expected_delimiter)
5348 break;
5349 /* make a pat node for this arg and append it to the end of
5350 the pat list */
5351 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
5352 tpat->next = NULL;
5353 tpat->raw_before = concat == id_beg;
5354 tpat->raw_after = 0;
5355 tpat->rest_args = arg->rest_args;
5356 tpat->stringify = (traditional ? expected_delimiter != '\0'
5357 : stringify == id_beg);
5359 if (endpat == NULL)
5360 defn->pattern = tpat;
5361 else
5362 endpat->next = tpat;
5363 endpat = tpat;
5365 tpat->argno = arg->argno;
5366 tpat->nchars = exp_p - lastp;
5368 register U_CHAR *p1 = p;
5369 SKIP_WHITE_SPACE (p1);
5370 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
5371 tpat->raw_after = 1;
5373 lastp = exp_p; /* place to start copying from next time */
5374 skipped_arg = 1;
5375 break;
5380 /* If this was not a macro arg, copy it into the expansion. */
5381 if (! skipped_arg) {
5382 register U_CHAR *lim1 = p;
5383 p = id_beg;
5384 while (p != lim1)
5385 *exp_p++ = *p++;
5386 if (stringify == id_beg)
5387 error ("`#' operator should be followed by a macro argument name");
5392 if (limit < end) {
5393 /* Convert trailing whitespace to Newline-markers. */
5394 while (limit < end && is_space[*limit]) {
5395 *exp_p++ = '\n';
5396 *exp_p++ = *limit++;
5398 } else if (!traditional && expected_delimiter == 0) {
5399 /* There is no trailing whitespace, so invent some in ANSI mode.
5400 But not if "inside a string" (which in ANSI mode
5401 happens only for -D option). */
5402 *exp_p++ = '\n';
5403 *exp_p++ = ' ';
5406 *exp_p = '\0';
5408 defn->length = exp_p - defn->expansion;
5410 /* Crash now if we overrun the allocated size. */
5411 if (defn->length + 1 > maxsize)
5412 abort ();
5414 #if 0
5415 /* This isn't worth the time it takes. */
5416 /* give back excess storage */
5417 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
5418 #endif
5420 return defn;
5423 static int
5424 do_assert (buf, limit, op, keyword)
5425 U_CHAR *buf, *limit;
5426 FILE_BUF *op;
5427 struct directive *keyword;
5429 U_CHAR *bp; /* temp ptr into input buffer */
5430 U_CHAR *symname; /* remember where symbol name starts */
5431 int sym_length; /* and how long it is */
5432 struct arglist *tokens = NULL;
5434 if (pedantic && done_initializing && !instack[indepth].system_header_p)
5435 pedwarn ("ANSI C does not allow `#assert'");
5437 bp = buf;
5439 while (is_hor_space[*bp])
5440 bp++;
5442 symname = bp; /* remember where it starts */
5443 sym_length = check_macro_name (bp, "assertion");
5444 bp += sym_length;
5445 /* #define doesn't do this, but we should. */
5446 SKIP_WHITE_SPACE (bp);
5448 /* Lossage will occur if identifiers or control tokens are broken
5449 across lines using backslash. This is not the right place to take
5450 care of that. */
5452 if (*bp != '(') {
5453 error ("missing token-sequence in `#assert'");
5454 return 1;
5458 int error_flag = 0;
5460 bp++; /* skip '(' */
5461 SKIP_WHITE_SPACE (bp);
5463 tokens = read_token_list (&bp, limit, &error_flag);
5464 if (error_flag)
5465 return 1;
5466 if (tokens == 0) {
5467 error ("empty token-sequence in `#assert'");
5468 return 1;
5471 ++bp; /* skip paren */
5472 SKIP_WHITE_SPACE (bp);
5475 /* If this name isn't already an assertion name, make it one.
5476 Error if it was already in use in some other way. */
5479 ASSERTION_HASHNODE *hp;
5480 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
5481 struct tokenlist_list *value
5482 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
5484 hp = assertion_lookup (symname, sym_length, hashcode);
5485 if (hp == NULL) {
5486 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
5487 error ("`defined' redefined as assertion");
5488 hp = assertion_install (symname, sym_length, hashcode);
5491 /* Add the spec'd token-sequence to the list of such. */
5492 value->tokens = tokens;
5493 value->next = hp->value;
5494 hp->value = value;
5497 return 0;
5500 static int
5501 do_unassert (buf, limit, op, keyword)
5502 U_CHAR *buf, *limit;
5503 FILE_BUF *op;
5504 struct directive *keyword;
5506 U_CHAR *bp; /* temp ptr into input buffer */
5507 U_CHAR *symname; /* remember where symbol name starts */
5508 int sym_length; /* and how long it is */
5510 struct arglist *tokens = NULL;
5511 int tokens_specified = 0;
5513 if (pedantic && done_initializing && !instack[indepth].system_header_p)
5514 pedwarn ("ANSI C does not allow `#unassert'");
5516 bp = buf;
5518 while (is_hor_space[*bp])
5519 bp++;
5521 symname = bp; /* remember where it starts */
5522 sym_length = check_macro_name (bp, "assertion");
5523 bp += sym_length;
5524 /* #define doesn't do this, but we should. */
5525 SKIP_WHITE_SPACE (bp);
5527 /* Lossage will occur if identifiers or control tokens are broken
5528 across lines using backslash. This is not the right place to take
5529 care of that. */
5531 if (*bp == '(') {
5532 int error_flag = 0;
5534 bp++; /* skip '(' */
5535 SKIP_WHITE_SPACE (bp);
5537 tokens = read_token_list (&bp, limit, &error_flag);
5538 if (error_flag)
5539 return 1;
5540 if (tokens == 0) {
5541 error ("empty token list in `#unassert'");
5542 return 1;
5545 tokens_specified = 1;
5547 ++bp; /* skip paren */
5548 SKIP_WHITE_SPACE (bp);
5552 ASSERTION_HASHNODE *hp;
5553 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
5554 struct tokenlist_list *tail, *prev;
5556 hp = assertion_lookup (symname, sym_length, hashcode);
5557 if (hp == NULL)
5558 return 1;
5560 /* If no token list was specified, then eliminate this assertion
5561 entirely. */
5562 if (! tokens_specified) {
5563 struct tokenlist_list *next;
5564 for (tail = hp->value; tail; tail = next) {
5565 next = tail->next;
5566 free_token_list (tail->tokens);
5567 free (tail);
5569 delete_assertion (hp);
5570 } else {
5571 /* If a list of tokens was given, then delete any matching list. */
5573 tail = hp->value;
5574 prev = 0;
5575 while (tail) {
5576 struct tokenlist_list *next = tail->next;
5577 if (compare_token_lists (tail->tokens, tokens)) {
5578 if (prev)
5579 prev->next = next;
5580 else
5581 hp->value = tail->next;
5582 free_token_list (tail->tokens);
5583 free (tail);
5584 } else {
5585 prev = tail;
5587 tail = next;
5592 return 0;
5595 /* Test whether there is an assertion named NAME
5596 and optionally whether it has an asserted token list TOKENS.
5597 NAME is not null terminated; its length is SYM_LENGTH.
5598 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
5601 check_assertion (name, sym_length, tokens_specified, tokens)
5602 U_CHAR *name;
5603 int sym_length;
5604 int tokens_specified;
5605 struct arglist *tokens;
5607 ASSERTION_HASHNODE *hp;
5608 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
5610 if (pedantic && !instack[indepth].system_header_p)
5611 pedwarn ("ANSI C does not allow testing assertions");
5613 hp = assertion_lookup (name, sym_length, hashcode);
5614 if (hp == NULL)
5615 /* It is not an assertion; just return false. */
5616 return 0;
5618 /* If no token list was specified, then value is 1. */
5619 if (! tokens_specified)
5620 return 1;
5623 struct tokenlist_list *tail;
5625 tail = hp->value;
5627 /* If a list of tokens was given,
5628 then succeed if the assertion records a matching list. */
5630 while (tail) {
5631 if (compare_token_lists (tail->tokens, tokens))
5632 return 1;
5633 tail = tail->next;
5636 /* Fail if the assertion has no matching list. */
5637 return 0;
5641 /* Compare two lists of tokens for equality including order of tokens. */
5643 static int
5644 compare_token_lists (l1, l2)
5645 struct arglist *l1, *l2;
5647 while (l1 && l2) {
5648 if (l1->length != l2->length)
5649 return 0;
5650 if (strncmp (l1->name, l2->name, l1->length))
5651 return 0;
5652 l1 = l1->next;
5653 l2 = l2->next;
5656 /* Succeed if both lists end at the same time. */
5657 return l1 == l2;
5660 /* Read a space-separated list of tokens ending in a close parenthesis.
5661 Return a list of strings, in the order they were written.
5662 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
5663 Parse the text starting at *BPP, and update *BPP.
5664 Don't parse beyond LIMIT. */
5666 static struct arglist *
5667 read_token_list (bpp, limit, error_flag)
5668 U_CHAR **bpp;
5669 U_CHAR *limit;
5670 int *error_flag;
5672 struct arglist *token_ptrs = 0;
5673 U_CHAR *bp = *bpp;
5674 int depth = 1;
5676 *error_flag = 0;
5678 /* Loop over the assertion value tokens. */
5679 while (depth > 0) {
5680 struct arglist *temp;
5681 int eofp = 0;
5682 U_CHAR *beg = bp;
5684 /* Find the end of the token. */
5685 if (*bp == '(') {
5686 bp++;
5687 depth++;
5688 } else if (*bp == ')') {
5689 depth--;
5690 if (depth == 0)
5691 break;
5692 bp++;
5693 } else if (*bp == '"' || *bp == '\'')
5694 bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
5695 else
5696 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
5697 && *bp != '"' && *bp != '\'' && bp != limit)
5698 bp++;
5700 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
5701 temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
5702 bcopy (beg, temp->name, bp - beg);
5703 temp->name[bp - beg] = 0;
5704 temp->next = token_ptrs;
5705 token_ptrs = temp;
5706 temp->length = bp - beg;
5708 SKIP_WHITE_SPACE (bp);
5710 if (bp >= limit) {
5711 error ("unterminated token sequence in `#assert' or `#unassert'");
5712 *error_flag = -1;
5713 return 0;
5716 *bpp = bp;
5718 /* We accumulated the names in reverse order.
5719 Now reverse them to get the proper order. */
5721 register struct arglist *prev = 0, *this, *next;
5722 for (this = token_ptrs; this; this = next) {
5723 next = this->next;
5724 this->next = prev;
5725 prev = this;
5727 return prev;
5731 static void
5732 free_token_list (tokens)
5733 struct arglist *tokens;
5735 while (tokens) {
5736 struct arglist *next = tokens->next;
5737 free (tokens->name);
5738 free (tokens);
5739 tokens = next;
5744 * Install a name in the assertion hash table.
5746 * If LEN is >= 0, it is the length of the name.
5747 * Otherwise, compute the length by scanning the entire name.
5749 * If HASH is >= 0, it is the precomputed hash code.
5750 * Otherwise, compute the hash code.
5752 static ASSERTION_HASHNODE *
5753 assertion_install (name, len, hash)
5754 U_CHAR *name;
5755 int len;
5756 int hash;
5758 register ASSERTION_HASHNODE *hp;
5759 register int i, bucket;
5760 register U_CHAR *p, *q;
5762 i = sizeof (ASSERTION_HASHNODE) + len + 1;
5763 hp = (ASSERTION_HASHNODE *) xmalloc (i);
5764 bucket = hash;
5765 hp->bucket_hdr = &assertion_hashtab[bucket];
5766 hp->next = assertion_hashtab[bucket];
5767 assertion_hashtab[bucket] = hp;
5768 hp->prev = NULL;
5769 if (hp->next != NULL)
5770 hp->next->prev = hp;
5771 hp->length = len;
5772 hp->value = 0;
5773 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
5774 p = hp->name;
5775 q = name;
5776 for (i = 0; i < len; i++)
5777 *p++ = *q++;
5778 hp->name[len] = 0;
5779 return hp;
5783 * find the most recent hash node for name name (ending with first
5784 * non-identifier char) installed by install
5786 * If LEN is >= 0, it is the length of the name.
5787 * Otherwise, compute the length by scanning the entire name.
5789 * If HASH is >= 0, it is the precomputed hash code.
5790 * Otherwise, compute the hash code.
5792 static ASSERTION_HASHNODE *
5793 assertion_lookup (name, len, hash)
5794 U_CHAR *name;
5795 int len;
5796 int hash;
5798 register U_CHAR *bp;
5799 register ASSERTION_HASHNODE *bucket;
5801 bucket = assertion_hashtab[hash];
5802 while (bucket) {
5803 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
5804 return bucket;
5805 bucket = bucket->next;
5807 return NULL;
5810 static void
5811 delete_assertion (hp)
5812 ASSERTION_HASHNODE *hp;
5815 if (hp->prev != NULL)
5816 hp->prev->next = hp->next;
5817 if (hp->next != NULL)
5818 hp->next->prev = hp->prev;
5820 /* make sure that the bucket chain header that
5821 the deleted guy was on points to the right thing afterwards. */
5822 if (hp == *hp->bucket_hdr)
5823 *hp->bucket_hdr = hp->next;
5825 free (hp);
5829 * interpret #line command. Remembers previously seen fnames
5830 * in its very own hash table.
5832 #define FNAME_HASHSIZE 37
5834 static int
5835 do_line (buf, limit, op, keyword)
5836 U_CHAR *buf, *limit;
5837 FILE_BUF *op;
5838 struct directive *keyword;
5840 register U_CHAR *bp;
5841 FILE_BUF *ip = &instack[indepth];
5842 FILE_BUF tem;
5843 int new_lineno;
5844 enum file_change_code file_change = same_file;
5846 /* Expand any macros. */
5847 tem = expand_to_temp_buffer (buf, limit, 0, 0);
5849 /* Point to macroexpanded line, which is null-terminated now. */
5850 bp = tem.buf;
5851 SKIP_WHITE_SPACE (bp);
5853 if (!isdigit (*bp)) {
5854 error ("invalid format `#line' command");
5855 return 0;
5858 /* The Newline at the end of this line remains to be processed.
5859 To put the next line at the specified line number,
5860 we must store a line number now that is one less. */
5861 new_lineno = atoi (bp) - 1;
5863 /* NEW_LINENO is one less than the actual line number here. */
5864 if (pedantic && new_lineno < 0)
5865 pedwarn ("line number out of range in `#line' command");
5867 /* skip over the line number. */
5868 while (isdigit (*bp))
5869 bp++;
5871 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
5872 if (*bp && !is_space[*bp]) {
5873 error ("invalid format `#line' command");
5874 return;
5876 #endif
5878 SKIP_WHITE_SPACE (bp);
5880 if (*bp == '\"') {
5881 static HASHNODE *fname_table[FNAME_HASHSIZE];
5882 HASHNODE *hp, **hash_bucket;
5883 U_CHAR *fname;
5884 int fname_length;
5886 fname = ++bp;
5888 while (*bp && *bp != '\"')
5889 bp++;
5890 if (*bp != '\"') {
5891 error ("invalid format `#line' command");
5892 return 0;
5895 fname_length = bp - fname;
5897 bp++;
5898 SKIP_WHITE_SPACE (bp);
5899 if (*bp) {
5900 if (pedantic)
5901 pedwarn ("garbage at end of `#line' command");
5902 if (*bp == '1')
5903 file_change = enter_file;
5904 else if (*bp == '2')
5905 file_change = leave_file;
5906 else if (*bp == '3')
5907 ip->system_header_p = 1;
5908 else {
5909 error ("invalid format `#line' command");
5910 return 0;
5913 bp++;
5914 SKIP_WHITE_SPACE (bp);
5915 if (*bp == '3') {
5916 ip->system_header_p = 1;
5917 bp++;
5918 SKIP_WHITE_SPACE (bp);
5920 if (*bp) {
5921 error ("invalid format `#line' command");
5922 return 0;
5926 hash_bucket =
5927 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
5928 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5929 if (hp->length == fname_length &&
5930 strncmp (hp->value.cpval, fname, fname_length) == 0) {
5931 ip->nominal_fname = hp->value.cpval;
5932 break;
5934 if (hp == 0) {
5935 /* Didn't find it; cons up a new one. */
5936 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
5937 hp->next = *hash_bucket;
5938 *hash_bucket = hp;
5940 hp->length = fname_length;
5941 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
5942 bcopy (fname, hp->value.cpval, fname_length);
5944 } else if (*bp) {
5945 error ("invalid format `#line' command");
5946 return 0;
5949 ip->lineno = new_lineno;
5950 output_line_command (ip, op, 0, file_change);
5951 check_expand (op, ip->length - (ip->bufp - ip->buf));
5952 return 0;
5956 * remove the definition of a symbol from the symbol table.
5957 * according to un*x /lib/cpp, it is not an error to undef
5958 * something that has no definitions, so it isn't one here either.
5961 static int
5962 do_undef (buf, limit, op, keyword)
5963 U_CHAR *buf, *limit;
5964 FILE_BUF *op;
5965 struct directive *keyword;
5967 int sym_length;
5968 HASHNODE *hp;
5969 U_CHAR *orig_buf = buf;
5971 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
5972 if (pcp_outfile && op)
5973 pass_thru_directive (buf, limit, op, keyword);
5975 SKIP_WHITE_SPACE (buf);
5976 sym_length = check_macro_name (buf, "macro");
5978 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
5979 /* If we are generating additional info for debugging (with -g) we
5980 need to pass through all effective #undef commands. */
5981 if (debug_output && op)
5982 pass_thru_directive (orig_buf, limit, op, keyword);
5983 if (hp->type != T_MACRO)
5984 warning ("undefining `%s'", hp->name);
5985 delete_macro (hp);
5988 if (pedantic) {
5989 buf += sym_length;
5990 SKIP_WHITE_SPACE (buf);
5991 if (buf != limit)
5992 pedwarn ("garbage after `#undef' directive");
5994 return 0;
5998 * Report a fatal error detected by the program we are processing.
5999 * Use the text of the line in the error message, then terminate.
6000 * (We use error because it prints the filename & line#.)
6003 static int
6004 do_error (buf, limit, op, keyword)
6005 U_CHAR *buf, *limit;
6006 FILE_BUF *op;
6007 struct directive *keyword;
6009 int length = limit - buf;
6010 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
6011 bcopy (buf, copy, length);
6012 copy[length] = 0;
6013 SKIP_WHITE_SPACE (copy);
6014 error ("#error %s", copy);
6015 exit (FAILURE_EXIT_CODE);
6016 /* NOTREACHED */
6017 return 0;
6021 * Report a warning detected by the program we are processing.
6022 * Use the text of the line in the warning message, then continue.
6023 * (We use error because it prints the filename & line#.)
6026 static int
6027 do_warning (buf, limit, op, keyword)
6028 U_CHAR *buf, *limit;
6029 FILE_BUF *op;
6030 struct directive *keyword;
6032 int length = limit - buf;
6033 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
6034 bcopy (buf, copy, length);
6035 copy[length] = 0;
6036 SKIP_WHITE_SPACE (copy);
6037 warning ("#warning %s", copy);
6038 return 0;
6041 /* Remember the name of the current file being read from so that we can
6042 avoid ever including it again. */
6044 static int
6045 do_once ()
6047 int i;
6048 FILE_BUF *ip = NULL;
6050 for (i = indepth; i >= 0; i--)
6051 if (instack[i].fname != NULL) {
6052 ip = &instack[i];
6053 break;
6056 if (ip != NULL) {
6057 struct file_name_list *new;
6059 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
6060 new->next = dont_repeat_files;
6061 dont_repeat_files = new;
6062 new->fname = savestring (ip->fname);
6063 new->control_macro = 0;
6065 return 0;
6068 /* #ident has already been copied to the output file, so just ignore it. */
6070 static int
6071 do_ident (buf, limit)
6072 U_CHAR *buf, *limit;
6074 /* Allow #ident in system headers, since that's not user's fault. */
6075 if (pedantic && !instack[indepth].system_header_p)
6076 pedwarn ("ANSI C does not allow `#ident'");
6077 return 0;
6080 /* #pragma and its argument line have already been copied to the output file.
6081 Just check for some recognized pragmas that need validation here. */
6083 static int
6084 do_pragma (buf, limit)
6085 U_CHAR *buf, *limit;
6087 while (*buf == ' ' || *buf == '\t')
6088 buf++;
6089 if (!strncmp (buf, "once", 4)) {
6090 /* Allow #pragma once in system headers, since that's not the user's
6091 fault. */
6092 if (!instack[indepth].system_header_p)
6093 warning ("`#pragma once' is obsolete");
6094 do_once ();
6097 if (!strncmp (buf, "implementation", 14)) {
6098 /* Be quiet about `#pragma implementation' for a file only if it hasn't
6099 been included yet. */
6100 struct file_name_list *ptr;
6101 U_CHAR *p = buf + 14, *fname, *inc_fname;
6102 SKIP_WHITE_SPACE (p);
6103 if (*p == '\n' || *p != '\"')
6104 return 0;
6106 fname = p + 1;
6107 if (p = (U_CHAR *) index (fname, '\"'))
6108 *p = '\0';
6110 for (ptr = all_include_files; ptr; ptr = ptr->next) {
6111 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
6112 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
6113 if (inc_fname && !strcmp (inc_fname, fname))
6114 warning ("`#pragma implementation' for `%s' appears after file is included",
6115 fname);
6119 return 0;
6122 #if 0
6123 /* This was a fun hack, but #pragma seems to start to be useful.
6124 By failing to recognize it, we pass it through unchanged to cc1. */
6127 * the behavior of the #pragma directive is implementation defined.
6128 * this implementation defines it as follows.
6131 static int
6132 do_pragma ()
6134 close (0);
6135 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
6136 goto nope;
6137 close (1);
6138 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
6139 goto nope;
6140 execl ("/usr/games/hack", "#pragma", 0);
6141 execl ("/usr/games/rogue", "#pragma", 0);
6142 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
6143 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
6144 nope:
6145 fatal ("You are in a maze of twisty compiler features, all different");
6147 #endif
6149 /* Just ignore #sccs, on systems where we define it at all. */
6151 static int
6152 do_sccs ()
6154 if (pedantic)
6155 pedwarn ("ANSI C does not allow `#sccs'");
6156 return 0;
6160 * handle #if command by
6161 * 1) inserting special `defined' keyword into the hash table
6162 * that gets turned into 0 or 1 by special_symbol (thus,
6163 * if the luser has a symbol called `defined' already, it won't
6164 * work inside the #if command)
6165 * 2) rescan the input into a temporary output buffer
6166 * 3) pass the output buffer to the yacc parser and collect a value
6167 * 4) clean up the mess left from steps 1 and 2.
6168 * 5) call conditional_skip to skip til the next #endif (etc.),
6169 * or not, depending on the value from step 3.
6172 static int
6173 do_if (buf, limit, op, keyword)
6174 U_CHAR *buf, *limit;
6175 FILE_BUF *op;
6176 struct directive *keyword;
6178 int value;
6179 FILE_BUF *ip = &instack[indepth];
6181 value = eval_if_expression (buf, limit - buf);
6182 conditional_skip (ip, value == 0, T_IF, NULL_PTR);
6183 return 0;
6187 * handle a #elif directive by not changing if_stack either.
6188 * see the comment above do_else.
6191 static int
6192 do_elif (buf, limit, op, keyword)
6193 U_CHAR *buf, *limit;
6194 FILE_BUF *op;
6195 struct directive *keyword;
6197 int value;
6198 FILE_BUF *ip = &instack[indepth];
6200 if (if_stack == instack[indepth].if_stack) {
6201 error ("`#elif' not within a conditional");
6202 return 0;
6203 } else {
6204 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
6205 error ("`#elif' after `#else'");
6206 fprintf (stderr, " (matches line %d", if_stack->lineno);
6207 if (if_stack->fname != NULL && ip->fname != NULL &&
6208 strcmp (if_stack->fname, ip->nominal_fname) != 0)
6209 fprintf (stderr, ", file %s", if_stack->fname);
6210 fprintf (stderr, ")\n");
6212 if_stack->type = T_ELIF;
6215 if (if_stack->if_succeeded)
6216 skip_if_group (ip, 0);
6217 else {
6218 value = eval_if_expression (buf, limit - buf);
6219 if (value == 0)
6220 skip_if_group (ip, 0);
6221 else {
6222 ++if_stack->if_succeeded; /* continue processing input */
6223 output_line_command (ip, op, 1, same_file);
6226 return 0;
6230 * evaluate a #if expression in BUF, of length LENGTH,
6231 * then parse the result as a C expression and return the value as an int.
6233 static int
6234 eval_if_expression (buf, length)
6235 U_CHAR *buf;
6236 int length;
6238 FILE_BUF temp_obuf;
6239 HASHNODE *save_defined;
6240 int value;
6242 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
6243 pcp_inside_if = 1;
6244 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
6245 pcp_inside_if = 0;
6246 delete_macro (save_defined); /* clean up special symbol */
6248 value = parse_c_expression (temp_obuf.buf);
6250 free (temp_obuf.buf);
6252 return value;
6256 * routine to handle ifdef/ifndef. Try to look up the symbol,
6257 * then do or don't skip to the #endif/#else/#elif depending
6258 * on what directive is actually being processed.
6261 static int
6262 do_xifdef (buf, limit, op, keyword)
6263 U_CHAR *buf, *limit;
6264 FILE_BUF *op;
6265 struct directive *keyword;
6267 int skip;
6268 FILE_BUF *ip = &instack[indepth];
6269 U_CHAR *end;
6270 int start_of_file = 0;
6271 U_CHAR *control_macro = 0;
6273 /* Detect a #ifndef at start of file (not counting comments). */
6274 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
6275 U_CHAR *p = ip->buf;
6276 while (p != directive_start) {
6277 U_CHAR c = *p++;
6278 if (is_space[c])
6280 else if (c == '/' && p != ip->bufp && *p == '*') {
6281 /* Skip this comment. */
6282 int junk;
6283 U_CHAR *save_bufp = ip->bufp;
6284 ip->bufp = p + 1;
6285 p = skip_to_end_of_comment (ip, &junk, 1);
6286 ip->bufp = save_bufp;
6287 } else {
6288 goto fail;
6291 /* If we get here, this conditional is the beginning of the file. */
6292 start_of_file = 1;
6293 fail: ;
6296 /* Discard leading and trailing whitespace. */
6297 SKIP_WHITE_SPACE (buf);
6298 while (limit != buf && is_hor_space[limit[-1]]) limit--;
6300 /* Find the end of the identifier at the beginning. */
6301 for (end = buf; is_idchar[*end]; end++);
6303 if (end == buf) {
6304 skip = (keyword->type == T_IFDEF);
6305 if (! traditional)
6306 pedwarn (end == limit ? "`#%s' with no argument"
6307 : "`#%s' argument starts with punctuation",
6308 keyword->name);
6309 } else {
6310 HASHNODE *hp;
6312 if (pedantic && buf[0] >= '0' && buf[0] <= '9')
6313 pedwarn ("`#%s' argument starts with a digit", keyword->name);
6314 else if (end != limit && !traditional)
6315 pedwarn ("garbage at end of `#%s' argument", keyword->name);
6317 hp = lookup (buf, end-buf, -1);
6319 if (pcp_outfile) {
6320 /* Output a precondition for this macro. */
6321 if (hp && hp->value.defn->predefined)
6322 fprintf (pcp_outfile, "#define %s\n", hp->name);
6323 else {
6324 U_CHAR *cp = buf;
6325 fprintf (pcp_outfile, "#undef ");
6326 while (is_idchar[*cp]) /* Ick! */
6327 fputc (*cp++, pcp_outfile);
6328 putc ('\n', pcp_outfile);
6332 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
6333 if (start_of_file && !skip) {
6334 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
6335 bcopy (buf, control_macro, end - buf);
6336 control_macro[end - buf] = 0;
6340 conditional_skip (ip, skip, T_IF, control_macro);
6341 return 0;
6344 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
6345 If this is a #ifndef starting at the beginning of a file,
6346 CONTROL_MACRO is the macro name tested by the #ifndef.
6347 Otherwise, CONTROL_MACRO is 0. */
6349 static void
6350 conditional_skip (ip, skip, type, control_macro)
6351 FILE_BUF *ip;
6352 int skip;
6353 enum node_type type;
6354 U_CHAR *control_macro;
6356 IF_STACK_FRAME *temp;
6358 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
6359 temp->fname = ip->nominal_fname;
6360 temp->lineno = ip->lineno;
6361 temp->next = if_stack;
6362 temp->control_macro = control_macro;
6363 if_stack = temp;
6365 if_stack->type = type;
6367 if (skip != 0) {
6368 skip_if_group (ip, 0);
6369 return;
6370 } else {
6371 ++if_stack->if_succeeded;
6372 output_line_command (ip, &outbuf, 1, same_file);
6377 * skip to #endif, #else, or #elif. adjust line numbers, etc.
6378 * leaves input ptr at the sharp sign found.
6379 * If ANY is nonzero, return at next directive of any sort.
6381 static void
6382 skip_if_group (ip, any)
6383 FILE_BUF *ip;
6384 int any;
6386 register U_CHAR *bp = ip->bufp, *cp;
6387 register U_CHAR *endb = ip->buf + ip->length;
6388 struct directive *kt;
6389 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
6390 U_CHAR *beg_of_line = bp;
6391 register int ident_length;
6392 U_CHAR *ident, *after_ident;
6394 while (bp < endb) {
6395 switch (*bp++) {
6396 case '/': /* possible comment */
6397 if (*bp == '\\' && bp[1] == '\n')
6398 newline_fix (bp);
6399 if (*bp == '*'
6400 || (cplusplus_comments && *bp == '/')) {
6401 ip->bufp = ++bp;
6402 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
6404 break;
6405 case '\"':
6406 case '\'':
6407 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
6408 NULL_PTR, NULL_PTR);
6409 break;
6410 case '\\':
6411 /* Char after backslash loses its special meaning. */
6412 if (bp < endb) {
6413 if (*bp == '\n')
6414 ++ip->lineno; /* But do update the line-count. */
6415 bp++;
6417 break;
6418 case '\n':
6419 ++ip->lineno;
6420 beg_of_line = bp;
6421 break;
6422 case '#':
6423 ip->bufp = bp - 1;
6425 /* # keyword: a # must be first nonblank char on the line */
6426 if (beg_of_line == 0)
6427 break;
6428 /* Scan from start of line, skipping whitespace, comments
6429 and backslash-newlines, and see if we reach this #.
6430 If not, this # is not special. */
6431 bp = beg_of_line;
6432 while (1) {
6433 if (is_hor_space[*bp])
6434 bp++;
6435 else if (*bp == '\\' && bp[1] == '\n')
6436 bp += 2;
6437 else if (*bp == '/' && bp[1] == '*') {
6438 bp += 2;
6439 while (!(*bp == '*' && bp[1] == '/'))
6440 bp++;
6441 bp += 2;
6442 } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
6443 bp += 2;
6444 while (*bp++ != '\n') ;
6446 else break;
6448 if (bp != ip->bufp) {
6449 bp = ip->bufp + 1; /* Reset bp to after the #. */
6450 break;
6453 bp = ip->bufp + 1; /* Point after the '#' */
6455 /* Skip whitespace and \-newline. */
6456 while (1) {
6457 if (is_hor_space[*bp])
6458 bp++;
6459 else if (*bp == '\\' && bp[1] == '\n')
6460 bp += 2;
6461 else if (*bp == '/' && bp[1] == '*') {
6462 bp += 2;
6463 while (!(*bp == '*' && bp[1] == '/')) {
6464 if (*bp == '\n')
6465 ip->lineno++;
6466 bp++;
6468 bp += 2;
6469 } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
6470 bp += 2;
6471 while (*bp++ != '\n') ;
6473 else break;
6476 cp = bp;
6478 /* Now find end of directive name.
6479 If we encounter a backslash-newline, exchange it with any following
6480 symbol-constituents so that we end up with a contiguous name. */
6482 while (1) {
6483 if (is_idchar[*bp])
6484 bp++;
6485 else {
6486 if (*bp == '\\' && bp[1] == '\n')
6487 name_newline_fix (bp);
6488 if (is_idchar[*bp])
6489 bp++;
6490 else break;
6493 ident_length = bp - cp;
6494 ident = cp;
6495 after_ident = bp;
6497 /* A line of just `#' becomes blank. */
6499 if (ident_length == 0 && *after_ident == '\n') {
6500 continue;
6503 if (ident_length == 0 || !is_idstart[*ident]) {
6504 U_CHAR *p = ident;
6505 while (is_idchar[*p]) {
6506 if (*p < '0' || *p > '9')
6507 break;
6508 p++;
6510 /* Handle # followed by a line number. */
6511 if (p != ident && !is_idchar[*p]) {
6512 if (pedantic)
6513 pedwarn ("`#' followed by integer");
6514 continue;
6517 /* Avoid error for `###' and similar cases unless -pedantic. */
6518 if (p == ident) {
6519 while (*p == '#' || is_hor_space[*p]) p++;
6520 if (*p == '\n') {
6521 if (pedantic && !lang_asm)
6522 pedwarn ("invalid preprocessor directive");
6523 continue;
6527 if (!lang_asm && pedantic)
6528 pedwarn ("invalid preprocessor directive name");
6529 continue;
6532 for (kt = directive_table; kt->length >= 0; kt++) {
6533 IF_STACK_FRAME *temp;
6534 if (ident_length == kt->length
6535 && strncmp (cp, kt->name, kt->length) == 0) {
6536 /* If we are asked to return on next directive, do so now. */
6537 if (any)
6538 return;
6540 switch (kt->type) {
6541 case T_IF:
6542 case T_IFDEF:
6543 case T_IFNDEF:
6544 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
6545 temp->next = if_stack;
6546 if_stack = temp;
6547 temp->lineno = ip->lineno;
6548 temp->fname = ip->nominal_fname;
6549 temp->type = kt->type;
6550 break;
6551 case T_ELSE:
6552 case T_ENDIF:
6553 if (pedantic && if_stack != save_if_stack)
6554 validate_else (bp);
6555 case T_ELIF:
6556 if (if_stack == instack[indepth].if_stack) {
6557 error ("`#%s' not within a conditional", kt->name);
6558 break;
6560 else if (if_stack == save_if_stack)
6561 return; /* found what we came for */
6563 if (kt->type != T_ENDIF) {
6564 if (if_stack->type == T_ELSE)
6565 error ("`#else' or `#elif' after `#else'");
6566 if_stack->type = kt->type;
6567 break;
6570 temp = if_stack;
6571 if_stack = if_stack->next;
6572 free (temp);
6573 break;
6575 break;
6578 /* Don't let erroneous code go by. */
6579 if (kt->length < 0 && !lang_asm && pedantic)
6580 pedwarn ("invalid preprocessor directive name");
6583 ip->bufp = bp;
6584 /* after this returns, rescan will exit because ip->bufp
6585 now points to the end of the buffer.
6586 rescan is responsible for the error message also. */
6590 * handle a #else directive. Do this by just continuing processing
6591 * without changing if_stack ; this is so that the error message
6592 * for missing #endif's etc. will point to the original #if. It
6593 * is possible that something different would be better.
6596 static int
6597 do_else (buf, limit, op, keyword)
6598 U_CHAR *buf, *limit;
6599 FILE_BUF *op;
6600 struct directive *keyword;
6602 FILE_BUF *ip = &instack[indepth];
6604 if (pedantic) {
6605 SKIP_WHITE_SPACE (buf);
6606 if (buf != limit)
6607 pedwarn ("text following `#else' violates ANSI standard");
6610 if (if_stack == instack[indepth].if_stack) {
6611 error ("`#else' not within a conditional");
6612 return 0;
6613 } else {
6614 /* #ifndef can't have its special treatment for containing the whole file
6615 if it has a #else clause. */
6616 if_stack->control_macro = 0;
6618 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
6619 error ("`#else' after `#else'");
6620 fprintf (stderr, " (matches line %d", if_stack->lineno);
6621 if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
6622 fprintf (stderr, ", file %s", if_stack->fname);
6623 fprintf (stderr, ")\n");
6625 if_stack->type = T_ELSE;
6628 if (if_stack->if_succeeded)
6629 skip_if_group (ip, 0);
6630 else {
6631 ++if_stack->if_succeeded; /* continue processing input */
6632 output_line_command (ip, op, 1, same_file);
6634 return 0;
6638 * unstack after #endif command
6641 static int
6642 do_endif (buf, limit, op, keyword)
6643 U_CHAR *buf, *limit;
6644 FILE_BUF *op;
6645 struct directive *keyword;
6647 if (pedantic) {
6648 SKIP_WHITE_SPACE (buf);
6649 if (buf != limit)
6650 pedwarn ("text following `#endif' violates ANSI standard");
6653 if (if_stack == instack[indepth].if_stack)
6654 error ("unbalanced `#endif'");
6655 else {
6656 IF_STACK_FRAME *temp = if_stack;
6657 if_stack = if_stack->next;
6658 if (temp->control_macro != 0) {
6659 /* This #endif matched a #ifndef at the start of the file.
6660 See if it is at the end of the file. */
6661 FILE_BUF *ip = &instack[indepth];
6662 U_CHAR *p = ip->bufp;
6663 U_CHAR *ep = ip->buf + ip->length;
6665 while (p != ep) {
6666 U_CHAR c = *p++;
6667 switch (c) {
6668 case ' ':
6669 case '\t':
6670 case '\n':
6671 break;
6672 case '/':
6673 if (p != ep && *p == '*') {
6674 /* Skip this comment. */
6675 int junk;
6676 U_CHAR *save_bufp = ip->bufp;
6677 ip->bufp = p + 1;
6678 p = skip_to_end_of_comment (ip, &junk, 1);
6679 ip->bufp = save_bufp;
6681 break;
6682 default:
6683 goto fail;
6686 /* If we get here, this #endif ends a #ifndef
6687 that contains all of the file (aside from whitespace).
6688 Arrange not to include the file again
6689 if the macro that was tested is defined. */
6690 if (indepth != 0)
6691 record_control_macro (ip->fname, temp->control_macro);
6692 fail: ;
6694 free (temp);
6695 output_line_command (&instack[indepth], op, 1, same_file);
6697 return 0;
6700 /* When an #else or #endif is found while skipping failed conditional,
6701 if -pedantic was specified, this is called to warn about text after
6702 the command name. P points to the first char after the command name. */
6704 static void
6705 validate_else (p)
6706 register U_CHAR *p;
6708 /* Advance P over whitespace and comments. */
6709 while (1) {
6710 if (*p == '\\' && p[1] == '\n')
6711 p += 2;
6712 if (is_hor_space[*p])
6713 p++;
6714 else if (*p == '/') {
6715 if (p[1] == '\\' && p[2] == '\n')
6716 newline_fix (p + 1);
6717 if (p[1] == '*') {
6718 p += 2;
6719 /* Don't bother warning about unterminated comments
6720 since that will happen later. Just be sure to exit. */
6721 while (*p) {
6722 if (p[1] == '\\' && p[2] == '\n')
6723 newline_fix (p + 1);
6724 if (*p == '*' && p[1] == '/') {
6725 p += 2;
6726 break;
6728 p++;
6731 else if (cplusplus_comments && p[1] == '/') {
6732 p += 2;
6733 while (*p && *p++ != '\n') ;
6735 } else break;
6737 if (*p && *p != '\n')
6738 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
6741 /* Skip a comment, assuming the input ptr immediately follows the
6742 initial slash-star. Bump *LINE_COUNTER for each newline.
6743 (The canonical line counter is &ip->lineno.)
6744 Don't use this routine (or the next one) if bumping the line
6745 counter is not sufficient to deal with newlines in the string.
6747 If NOWARN is nonzero, don't warn about slash-star inside a comment.
6748 This feature is useful when processing a comment that is going to be
6749 processed or was processed at another point in the preprocessor,
6750 to avoid a duplicate warning. */
6751 static U_CHAR *
6752 skip_to_end_of_comment (ip, line_counter, nowarn)
6753 register FILE_BUF *ip;
6754 int *line_counter; /* place to remember newlines, or NULL */
6755 int nowarn;
6757 register U_CHAR *limit = ip->buf + ip->length;
6758 register U_CHAR *bp = ip->bufp;
6759 FILE_BUF *op = &outbuf; /* JF */
6760 int output = put_out_comments && !line_counter;
6762 /* JF this line_counter stuff is a crock to make sure the
6763 comment is only put out once, no matter how many times
6764 the comment is skipped. It almost works */
6765 if (output) {
6766 *op->bufp++ = '/';
6767 *op->bufp++ = '*';
6769 if (cplusplus_comments && bp[-1] == '/') {
6770 if (output) {
6771 while (bp < limit)
6772 if ((*op->bufp++ = *bp++) == '\n') {
6773 bp--;
6774 break;
6776 op->bufp[-1] = '*';
6777 *op->bufp++ = '/';
6778 *op->bufp++ = '\n';
6779 } else {
6780 while (bp < limit) {
6781 if (*bp++ == '\n') {
6782 bp--;
6783 break;
6787 ip->bufp = bp;
6788 return bp;
6790 while (bp < limit) {
6791 if (output)
6792 *op->bufp++ = *bp;
6793 switch (*bp++) {
6794 case '/':
6795 if (warn_comments && !nowarn && bp < limit && *bp == '*')
6796 warning ("`/*' within comment");
6797 break;
6798 case '\n':
6799 if (line_counter != NULL)
6800 ++*line_counter;
6801 if (output)
6802 ++op->lineno;
6803 break;
6804 case '*':
6805 if (*bp == '\\' && bp[1] == '\n')
6806 newline_fix (bp);
6807 if (*bp == '/') {
6808 if (output)
6809 *op->bufp++ = '/';
6810 ip->bufp = ++bp;
6811 return bp;
6813 break;
6816 ip->bufp = bp;
6817 return bp;
6821 * Skip over a quoted string. BP points to the opening quote.
6822 * Returns a pointer after the closing quote. Don't go past LIMIT.
6823 * START_LINE is the line number of the starting point (but it need
6824 * not be valid if the starting point is inside a macro expansion).
6826 * The input stack state is not changed.
6828 * If COUNT_NEWLINES is nonzero, it points to an int to increment
6829 * for each newline passed.
6831 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
6832 * if we pass a backslash-newline.
6834 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
6836 static U_CHAR *
6837 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
6838 register U_CHAR *bp;
6839 register U_CHAR *limit;
6840 int start_line;
6841 int *count_newlines;
6842 int *backslash_newlines_p;
6843 int *eofp;
6845 register U_CHAR c, match;
6847 match = *bp++;
6848 while (1) {
6849 if (bp >= limit) {
6850 error_with_line (line_for_error (start_line),
6851 "unterminated string or character constant");
6852 error_with_line (multiline_string_line,
6853 "possible real start of unterminated constant");
6854 multiline_string_line = 0;
6855 if (eofp)
6856 *eofp = 1;
6857 break;
6859 c = *bp++;
6860 if (c == '\\') {
6861 while (*bp == '\\' && bp[1] == '\n') {
6862 if (backslash_newlines_p)
6863 *backslash_newlines_p = 1;
6864 if (count_newlines)
6865 ++*count_newlines;
6866 bp += 2;
6868 if (*bp == '\n' && count_newlines) {
6869 if (backslash_newlines_p)
6870 *backslash_newlines_p = 1;
6871 ++*count_newlines;
6873 bp++;
6874 } else if (c == '\n') {
6875 if (traditional) {
6876 /* Unterminated strings and character constants are 'legal'. */
6877 bp--; /* Don't consume the newline. */
6878 if (eofp)
6879 *eofp = 1;
6880 break;
6882 if (pedantic || match == '\'') {
6883 error_with_line (line_for_error (start_line),
6884 "unterminated string or character constant");
6885 bp--;
6886 if (eofp)
6887 *eofp = 1;
6888 break;
6890 /* If not traditional, then allow newlines inside strings. */
6891 if (count_newlines)
6892 ++*count_newlines;
6893 if (multiline_string_line == 0)
6894 multiline_string_line = start_line;
6895 } else if (c == match)
6896 break;
6898 return bp;
6901 /* Skip across a group of balanced parens, starting from IP->bufp.
6902 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
6904 This does not handle newlines, because it's used for the arg of #if,
6905 where there aren't any newlines. Also, backslash-newline can't appear. */
6907 static U_CHAR *
6908 skip_paren_group (ip)
6909 register FILE_BUF *ip;
6911 U_CHAR *limit = ip->buf + ip->length;
6912 U_CHAR *p = ip->bufp;
6913 int depth = 0;
6914 int lines_dummy = 0;
6916 while (p != limit) {
6917 int c = *p++;
6918 switch (c) {
6919 case '(':
6920 depth++;
6921 break;
6923 case ')':
6924 depth--;
6925 if (depth == 0)
6926 return ip->bufp = p;
6927 break;
6929 case '/':
6930 if (*p == '*') {
6931 ip->bufp = p;
6932 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
6933 p = ip->bufp;
6936 case '"':
6937 case '\'':
6939 int eofp = 0;
6940 p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
6941 if (eofp)
6942 return ip->bufp = p;
6944 break;
6948 ip->bufp = p;
6949 return p;
6953 * write out a #line command, for instance, after an #include file.
6954 * If CONDITIONAL is nonzero, we can omit the #line if it would
6955 * appear to be a no-op, and we can output a few newlines instead
6956 * if we want to increase the line number by a small amount.
6957 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
6960 static void
6961 output_line_command (ip, op, conditional, file_change)
6962 FILE_BUF *ip, *op;
6963 int conditional;
6964 enum file_change_code file_change;
6966 int len;
6967 char *line_cmd_buf;
6969 if (no_line_commands
6970 || ip->fname == NULL
6971 || no_output) {
6972 op->lineno = ip->lineno;
6973 return;
6976 if (conditional) {
6977 if (ip->lineno == op->lineno)
6978 return;
6980 /* If the inherited line number is a little too small,
6981 output some newlines instead of a #line command. */
6982 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
6983 check_expand (op, 10);
6984 while (ip->lineno > op->lineno) {
6985 *op->bufp++ = '\n';
6986 op->lineno++;
6988 return;
6992 /* Don't output a line number of 0 if we can help it. */
6993 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
6994 && *ip->bufp == '\n') {
6995 ip->lineno++;
6996 ip->bufp++;
6999 line_cmd_buf = (char *) alloca (strlen (ip->nominal_fname) + 100);
7000 #ifdef OUTPUT_LINE_COMMANDS
7001 sprintf (line_cmd_buf, "#line %d \"%s\"", ip->lineno, ip->nominal_fname);
7002 #else
7003 sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->nominal_fname);
7004 #endif
7005 if (file_change != same_file)
7006 strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
7007 /* Tell cc1 if following text comes from a system header file. */
7008 if (ip->system_header_p)
7009 strcat (line_cmd_buf, " 3");
7010 len = strlen (line_cmd_buf);
7011 line_cmd_buf[len++] = '\n';
7012 check_expand (op, len + 1);
7013 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7014 *op->bufp++ = '\n';
7015 bcopy (line_cmd_buf, op->bufp, len);
7016 op->bufp += len;
7017 op->lineno = ip->lineno;
7020 /* This structure represents one parsed argument in a macro call.
7021 `raw' points to the argument text as written (`raw_length' is its length).
7022 `expanded' points to the argument's macro-expansion
7023 (its length is `expand_length').
7024 `stringified_length' is the length the argument would have
7025 if stringified.
7026 `use_count' is the number of times this macro arg is substituted
7027 into the macro. If the actual use count exceeds 10,
7028 the value stored is 10.
7029 `free1' and `free2', if nonzero, point to blocks to be freed
7030 when the macro argument data is no longer needed. */
7032 struct argdata {
7033 U_CHAR *raw, *expanded;
7034 int raw_length, expand_length;
7035 int stringified_length;
7036 U_CHAR *free1, *free2;
7037 char newlines;
7038 char comments;
7039 char use_count;
7042 /* Expand a macro call.
7043 HP points to the symbol that is the macro being called.
7044 Put the result of expansion onto the input stack
7045 so that subsequent input by our caller will use it.
7047 If macro wants arguments, caller has already verified that
7048 an argument list follows; arguments come from the input stack. */
7050 static void
7051 macroexpand (hp, op)
7052 HASHNODE *hp;
7053 FILE_BUF *op;
7055 int nargs;
7056 DEFINITION *defn = hp->value.defn;
7057 register U_CHAR *xbuf;
7058 int xbuf_len;
7059 int start_line = instack[indepth].lineno;
7060 int rest_args, rest_zero;
7062 CHECK_DEPTH (return;);
7064 /* it might not actually be a macro. */
7065 if (hp->type != T_MACRO) {
7066 special_symbol (hp, op);
7067 return;
7070 /* This macro is being used inside a #if, which means it must be */
7071 /* recorded as a precondition. */
7072 if (pcp_inside_if && pcp_outfile && defn->predefined)
7073 dump_single_macro (hp, pcp_outfile);
7075 nargs = defn->nargs;
7077 if (nargs >= 0) {
7078 register int i;
7079 struct argdata *args;
7080 char *parse_error = 0;
7082 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
7084 for (i = 0; i < nargs; i++) {
7085 args[i].raw = (U_CHAR *) "";
7086 args[i].expanded = 0;
7087 args[i].raw_length = args[i].expand_length
7088 = args[i].stringified_length = 0;
7089 args[i].free1 = args[i].free2 = 0;
7090 args[i].use_count = 0;
7093 /* Parse all the macro args that are supplied. I counts them.
7094 The first NARGS args are stored in ARGS.
7095 The rest are discarded.
7096 If rest_args is set then we assume macarg absorbed the rest of the args.
7098 i = 0;
7099 rest_args = 0;
7100 do {
7101 /* Discard the open-parenthesis or comma before the next arg. */
7102 ++instack[indepth].bufp;
7103 if (rest_args)
7104 continue;
7105 if (i < nargs || (nargs == 0 && i == 0)) {
7106 /* if we are working on last arg which absorbs rest of args... */
7107 if (i == nargs - 1 && defn->rest_args)
7108 rest_args = 1;
7109 parse_error = macarg (&args[i], rest_args);
7111 else
7112 parse_error = macarg (NULL_PTR, 0);
7113 if (parse_error) {
7114 error_with_line (line_for_error (start_line), parse_error);
7115 break;
7117 i++;
7118 } while (*instack[indepth].bufp != ')');
7120 /* If we got one arg but it was just whitespace, call that 0 args. */
7121 if (i == 1) {
7122 register U_CHAR *bp = args[0].raw;
7123 register U_CHAR *lim = bp + args[0].raw_length;
7124 /* cpp.texi says for foo ( ) we provide one argument.
7125 However, if foo wants just 0 arguments, treat this as 0. */
7126 if (nargs == 0)
7127 while (bp != lim && is_space[*bp]) bp++;
7128 if (bp == lim)
7129 i = 0;
7132 /* Don't output an error message if we have already output one for
7133 a parse error above. */
7134 rest_zero = 0;
7135 if (nargs == 0 && i > 0) {
7136 if (! parse_error)
7137 error ("arguments given to macro `%s'", hp->name);
7138 } else if (i < nargs) {
7139 /* traditional C allows foo() if foo wants one argument. */
7140 if (nargs == 1 && i == 0 && traditional)
7142 /* the rest args token is allowed to absorb 0 tokens */
7143 else if (i == nargs - 1 && defn->rest_args)
7144 rest_zero = 1;
7145 else if (parse_error)
7147 else if (i == 0)
7148 error ("macro `%s' used without args", hp->name);
7149 else if (i == 1)
7150 error ("macro `%s' used with just one arg", hp->name);
7151 else
7152 error ("macro `%s' used with only %d args", hp->name, i);
7153 } else if (i > nargs) {
7154 if (! parse_error)
7155 error ("macro `%s' used with too many (%d) args", hp->name, i);
7158 /* Swallow the closeparen. */
7159 ++instack[indepth].bufp;
7161 /* If macro wants zero args, we parsed the arglist for checking only.
7162 Read directly from the macro definition. */
7163 if (nargs == 0) {
7164 xbuf = defn->expansion;
7165 xbuf_len = defn->length;
7166 } else {
7167 register U_CHAR *exp = defn->expansion;
7168 register int offset; /* offset in expansion,
7169 copied a piece at a time */
7170 register int totlen; /* total amount of exp buffer filled so far */
7172 register struct reflist *ap, *last_ap;
7174 /* Macro really takes args. Compute the expansion of this call. */
7176 /* Compute length in characters of the macro's expansion.
7177 Also count number of times each arg is used. */
7178 xbuf_len = defn->length;
7179 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
7180 if (ap->stringify)
7181 xbuf_len += args[ap->argno].stringified_length;
7182 else if (ap->raw_before || ap->raw_after || traditional)
7183 /* Add 4 for two newline-space markers to prevent
7184 token concatenation. */
7185 xbuf_len += args[ap->argno].raw_length + 4;
7186 else {
7187 /* We have an ordinary (expanded) occurrence of the arg.
7188 So compute its expansion, if we have not already. */
7189 if (args[ap->argno].expanded == 0) {
7190 FILE_BUF obuf;
7191 obuf = expand_to_temp_buffer (args[ap->argno].raw,
7192 args[ap->argno].raw + args[ap->argno].raw_length,
7193 1, 0);
7195 args[ap->argno].expanded = obuf.buf;
7196 args[ap->argno].expand_length = obuf.length;
7197 args[ap->argno].free2 = obuf.buf;
7200 /* Add 4 for two newline-space markers to prevent
7201 token concatenation. */
7202 xbuf_len += args[ap->argno].expand_length + 4;
7204 if (args[ap->argno].use_count < 10)
7205 args[ap->argno].use_count++;
7208 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
7210 /* Generate in XBUF the complete expansion
7211 with arguments substituted in.
7212 TOTLEN is the total size generated so far.
7213 OFFSET is the index in the definition
7214 of where we are copying from. */
7215 offset = totlen = 0;
7216 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
7217 last_ap = ap, ap = ap->next) {
7218 register struct argdata *arg = &args[ap->argno];
7219 int count_before = totlen;
7221 /* Add chars to XBUF. */
7222 for (i = 0; i < ap->nchars; i++, offset++)
7223 xbuf[totlen++] = exp[offset];
7225 /* If followed by an empty rest arg with concatenation,
7226 delete the last run of nonwhite chars. */
7227 if (rest_zero && totlen > count_before
7228 && ((ap->rest_args && ap->raw_before)
7229 || (last_ap != NULL && last_ap->rest_args
7230 && last_ap->raw_after))) {
7231 /* Delete final whitespace. */
7232 while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
7233 totlen--;
7236 /* Delete the nonwhites before them. */
7237 while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
7238 totlen--;
7242 if (ap->stringify != 0) {
7243 int arglen = arg->raw_length;
7244 int escaped = 0;
7245 int in_string = 0;
7246 int c;
7247 i = 0;
7248 while (i < arglen
7249 && (c = arg->raw[i], is_space[c]))
7250 i++;
7251 while (i < arglen
7252 && (c = arg->raw[arglen - 1], is_space[c]))
7253 arglen--;
7254 if (!traditional)
7255 xbuf[totlen++] = '\"'; /* insert beginning quote */
7256 for (; i < arglen; i++) {
7257 c = arg->raw[i];
7259 /* Special markers Newline Space
7260 generate nothing for a stringified argument. */
7261 if (c == '\n' && arg->raw[i+1] != '\n') {
7262 i++;
7263 continue;
7266 /* Internal sequences of whitespace are replaced by one space
7267 except within an string or char token. */
7268 if (! in_string
7269 && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
7270 while (1) {
7271 /* Note that Newline Space does occur within whitespace
7272 sequences; consider it part of the sequence. */
7273 if (c == '\n' && is_space[arg->raw[i+1]])
7274 i += 2;
7275 else if (c != '\n' && is_space[c])
7276 i++;
7277 else break;
7278 c = arg->raw[i];
7280 i--;
7281 c = ' ';
7284 if (escaped)
7285 escaped = 0;
7286 else {
7287 if (c == '\\')
7288 escaped = 1;
7289 if (in_string) {
7290 if (c == in_string)
7291 in_string = 0;
7292 } else if (c == '\"' || c == '\'')
7293 in_string = c;
7296 /* Escape these chars */
7297 if (c == '\"' || (in_string && c == '\\'))
7298 xbuf[totlen++] = '\\';
7299 if (isprint (c))
7300 xbuf[totlen++] = c;
7301 else {
7302 sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
7303 totlen += 4;
7306 if (!traditional)
7307 xbuf[totlen++] = '\"'; /* insert ending quote */
7308 } else if (ap->raw_before || ap->raw_after || traditional) {
7309 U_CHAR *p1 = arg->raw;
7310 U_CHAR *l1 = p1 + arg->raw_length;
7311 if (ap->raw_before) {
7312 while (p1 != l1 && is_space[*p1]) p1++;
7313 while (p1 != l1 && is_idchar[*p1])
7314 xbuf[totlen++] = *p1++;
7315 /* Delete any no-reexpansion marker that follows
7316 an identifier at the beginning of the argument
7317 if the argument is concatenated with what precedes it. */
7318 if (p1[0] == '\n' && p1[1] == '-')
7319 p1 += 2;
7320 } else if (!traditional) {
7321 /* Ordinary expanded use of the argument.
7322 Put in newline-space markers to prevent token pasting. */
7323 xbuf[totlen++] = '\n';
7324 xbuf[totlen++] = ' ';
7326 if (ap->raw_after) {
7327 /* Arg is concatenated after: delete trailing whitespace,
7328 whitespace markers, and no-reexpansion markers. */
7329 while (p1 != l1) {
7330 if (is_space[l1[-1]]) l1--;
7331 else if (l1[-1] == '-') {
7332 U_CHAR *p2 = l1 - 1;
7333 /* If a `-' is preceded by an odd number of newlines then it
7334 and the last newline are a no-reexpansion marker. */
7335 while (p2 != p1 && p2[-1] == '\n') p2--;
7336 if ((l1 - 1 - p2) & 1) {
7337 l1 -= 2;
7339 else break;
7341 else break;
7345 bcopy (p1, xbuf + totlen, l1 - p1);
7346 totlen += l1 - p1;
7347 if (!traditional && !ap->raw_after) {
7348 /* Ordinary expanded use of the argument.
7349 Put in newline-space markers to prevent token pasting. */
7350 xbuf[totlen++] = '\n';
7351 xbuf[totlen++] = ' ';
7353 } else {
7354 /* Ordinary expanded use of the argument.
7355 Put in newline-space markers to prevent token pasting. */
7356 if (!traditional) {
7357 xbuf[totlen++] = '\n';
7358 xbuf[totlen++] = ' ';
7360 bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
7361 totlen += arg->expand_length;
7362 if (!traditional) {
7363 xbuf[totlen++] = '\n';
7364 xbuf[totlen++] = ' ';
7366 /* If a macro argument with newlines is used multiple times,
7367 then only expand the newlines once. This avoids creating output
7368 lines which don't correspond to any input line, which confuses
7369 gdb and gcov. */
7370 if (arg->use_count > 1 && arg->newlines > 0) {
7371 /* Don't bother doing change_newlines for subsequent
7372 uses of arg. */
7373 arg->use_count = 1;
7374 arg->expand_length
7375 = change_newlines (arg->expanded, arg->expand_length);
7379 if (totlen > xbuf_len)
7380 abort ();
7383 /* if there is anything left of the definition
7384 after handling the arg list, copy that in too. */
7386 for (i = offset; i < defn->length; i++) {
7387 /* if we've reached the end of the macro */
7388 if (exp[i] == ')')
7389 rest_zero = 0;
7390 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
7391 && last_ap->raw_after))
7392 xbuf[totlen++] = exp[i];
7395 xbuf[totlen] = 0;
7396 xbuf_len = totlen;
7398 for (i = 0; i < nargs; i++) {
7399 if (args[i].free1 != 0)
7400 free (args[i].free1);
7401 if (args[i].free2 != 0)
7402 free (args[i].free2);
7405 } else {
7406 xbuf = defn->expansion;
7407 xbuf_len = defn->length;
7410 /* Now put the expansion on the input stack
7411 so our caller will commence reading from it. */
7413 register FILE_BUF *ip2;
7415 ip2 = &instack[++indepth];
7417 ip2->fname = 0;
7418 ip2->nominal_fname = 0;
7419 ip2->lineno = 0;
7420 ip2->buf = xbuf;
7421 ip2->length = xbuf_len;
7422 ip2->bufp = xbuf;
7423 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
7424 ip2->macro = hp;
7425 ip2->if_stack = if_stack;
7426 ip2->system_header_p = 0;
7428 /* Recursive macro use sometimes works traditionally.
7429 #define foo(x,y) bar (x (y,0), y)
7430 foo (foo, baz) */
7432 if (!traditional)
7433 hp->type = T_DISABLED;
7438 * Parse a macro argument and store the info on it into *ARGPTR.
7439 * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
7440 * Return nonzero to indicate a syntax error.
7443 static char *
7444 macarg (argptr, rest_args)
7445 register struct argdata *argptr;
7446 int rest_args;
7448 FILE_BUF *ip = &instack[indepth];
7449 int paren = 0;
7450 int newlines = 0;
7451 int comments = 0;
7453 /* Try to parse as much of the argument as exists at this
7454 input stack level. */
7455 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
7456 &paren, &newlines, &comments, rest_args);
7458 /* If we find the end of the argument at this level,
7459 set up *ARGPTR to point at it in the input stack. */
7460 if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
7461 && bp != ip->buf + ip->length) {
7462 if (argptr != 0) {
7463 argptr->raw = ip->bufp;
7464 argptr->raw_length = bp - ip->bufp;
7465 argptr->newlines = newlines;
7467 ip->bufp = bp;
7468 } else {
7469 /* This input stack level ends before the macro argument does.
7470 We must pop levels and keep parsing.
7471 Therefore, we must allocate a temporary buffer and copy
7472 the macro argument into it. */
7473 int bufsize = bp - ip->bufp;
7474 int extra = newlines;
7475 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
7476 int final_start = 0;
7478 bcopy (ip->bufp, buffer, bufsize);
7479 ip->bufp = bp;
7480 ip->lineno += newlines;
7482 while (bp == ip->buf + ip->length) {
7483 if (instack[indepth].macro == 0) {
7484 free (buffer);
7485 return "unterminated macro call";
7487 ip->macro->type = T_MACRO;
7488 if (ip->free_ptr)
7489 free (ip->free_ptr);
7490 ip = &instack[--indepth];
7491 newlines = 0;
7492 comments = 0;
7493 bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
7494 &newlines, &comments, rest_args);
7495 final_start = bufsize;
7496 bufsize += bp - ip->bufp;
7497 extra += newlines;
7498 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
7499 bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
7500 ip->bufp = bp;
7501 ip->lineno += newlines;
7504 /* Now, if arg is actually wanted, record its raw form,
7505 discarding comments and duplicating newlines in whatever
7506 part of it did not come from a macro expansion.
7507 EXTRA space has been preallocated for duplicating the newlines.
7508 FINAL_START is the index of the start of that part. */
7509 if (argptr != 0) {
7510 argptr->raw = buffer;
7511 argptr->raw_length = bufsize;
7512 argptr->free1 = buffer;
7513 argptr->newlines = newlines;
7514 argptr->comments = comments;
7515 if ((newlines || comments) && ip->fname != 0)
7516 argptr->raw_length
7517 = final_start +
7518 discard_comments (argptr->raw + final_start,
7519 argptr->raw_length - final_start,
7520 newlines);
7521 argptr->raw[argptr->raw_length] = 0;
7522 if (argptr->raw_length > bufsize + extra)
7523 abort ();
7527 /* If we are not discarding this argument,
7528 macroexpand it and compute its length as stringified.
7529 All this info goes into *ARGPTR. */
7531 if (argptr != 0) {
7532 register U_CHAR *buf, *lim;
7533 register int totlen;
7535 buf = argptr->raw;
7536 lim = buf + argptr->raw_length;
7538 while (buf != lim && is_space[*buf])
7539 buf++;
7540 while (buf != lim && is_space[lim[-1]])
7541 lim--;
7542 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
7543 while (buf != lim) {
7544 register U_CHAR c = *buf++;
7545 totlen++;
7546 /* Internal sequences of whitespace are replaced by one space
7547 in most cases, but not always. So count all the whitespace
7548 in case we need to keep it all. */
7549 #if 0
7550 if (is_space[c])
7551 SKIP_ALL_WHITE_SPACE (buf);
7552 else
7553 #endif
7554 if (c == '\"' || c == '\\') /* escape these chars */
7555 totlen++;
7556 else if (!isprint (c))
7557 totlen += 3;
7559 argptr->stringified_length = totlen;
7561 return 0;
7564 /* Scan text from START (inclusive) up to LIMIT (exclusive),
7565 counting parens in *DEPTHPTR,
7566 and return if reach LIMIT
7567 or before a `)' that would make *DEPTHPTR negative
7568 or before a comma when *DEPTHPTR is zero.
7569 Single and double quotes are matched and termination
7570 is inhibited within them. Comments also inhibit it.
7571 Value returned is pointer to stopping place.
7573 Increment *NEWLINES each time a newline is passed.
7574 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
7575 Set *COMMENTS to 1 if a comment is seen. */
7577 static U_CHAR *
7578 macarg1 (start, limit, depthptr, newlines, comments, rest_args)
7579 U_CHAR *start;
7580 register U_CHAR *limit;
7581 int *depthptr, *newlines, *comments;
7582 int rest_args;
7584 register U_CHAR *bp = start;
7586 while (bp < limit) {
7587 switch (*bp) {
7588 case '(':
7589 (*depthptr)++;
7590 break;
7591 case ')':
7592 if (--(*depthptr) < 0)
7593 return bp;
7594 break;
7595 case '\\':
7596 /* Traditionally, backslash makes following char not special. */
7597 if (bp + 1 < limit && traditional)
7599 bp++;
7600 /* But count source lines anyway. */
7601 if (*bp == '\n')
7602 ++*newlines;
7604 break;
7605 case '\n':
7606 ++*newlines;
7607 break;
7608 case '/':
7609 if (bp[1] == '\\' && bp[2] == '\n')
7610 newline_fix (bp + 1);
7611 if (cplusplus_comments && bp[1] == '/') {
7612 *comments = 1;
7613 bp += 2;
7614 while (bp < limit && *bp++ != '\n') ;
7615 ++*newlines;
7616 break;
7618 if (bp[1] != '*' || bp + 1 >= limit)
7619 break;
7620 *comments = 1;
7621 bp += 2;
7622 while (bp + 1 < limit) {
7623 if (bp[0] == '*'
7624 && bp[1] == '\\' && bp[2] == '\n')
7625 newline_fix (bp + 1);
7626 if (bp[0] == '*' && bp[1] == '/')
7627 break;
7628 if (*bp == '\n') ++*newlines;
7629 bp++;
7631 break;
7632 case '\'':
7633 case '\"':
7635 int quotec;
7636 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
7637 if (*bp == '\\') {
7638 bp++;
7639 if (*bp == '\n')
7640 ++*newlines;
7641 while (*bp == '\\' && bp[1] == '\n') {
7642 bp += 2;
7644 } else if (*bp == '\n') {
7645 ++*newlines;
7646 if (quotec == '\'')
7647 break;
7651 break;
7652 case ',':
7653 /* if we've returned to lowest level and we aren't absorbing all args */
7654 if ((*depthptr) == 0 && rest_args == 0)
7655 return bp;
7656 break;
7658 bp++;
7661 return bp;
7664 /* Discard comments and duplicate newlines
7665 in the string of length LENGTH at START,
7666 except inside of string constants.
7667 The string is copied into itself with its beginning staying fixed.
7669 NEWLINES is the number of newlines that must be duplicated.
7670 We assume that that much extra space is available past the end
7671 of the string. */
7673 static int
7674 discard_comments (start, length, newlines)
7675 U_CHAR *start;
7676 int length;
7677 int newlines;
7679 register U_CHAR *ibp;
7680 register U_CHAR *obp;
7681 register U_CHAR *limit;
7682 register int c;
7684 /* If we have newlines to duplicate, copy everything
7685 that many characters up. Then, in the second part,
7686 we will have room to insert the newlines
7687 while copying down.
7688 NEWLINES may actually be too large, because it counts
7689 newlines in string constants, and we don't duplicate those.
7690 But that does no harm. */
7691 if (newlines > 0) {
7692 ibp = start + length;
7693 obp = ibp + newlines;
7694 limit = start;
7695 while (limit != ibp)
7696 *--obp = *--ibp;
7699 ibp = start + newlines;
7700 limit = start + length + newlines;
7701 obp = start;
7703 while (ibp < limit) {
7704 *obp++ = c = *ibp++;
7705 switch (c) {
7706 case '\n':
7707 /* Duplicate the newline. */
7708 *obp++ = '\n';
7709 break;
7711 case '\\':
7712 if (*ibp == '\n') {
7713 obp--;
7714 ibp++;
7716 break;
7718 case '/':
7719 if (*ibp == '\\' && ibp[1] == '\n')
7720 newline_fix (ibp);
7721 /* Delete any comment. */
7722 if (cplusplus_comments && ibp[0] == '/') {
7723 obp--;
7724 ibp++;
7725 while (ibp < limit && *ibp++ != '\n') ;
7726 break;
7728 if (ibp[0] != '*' || ibp + 1 >= limit)
7729 break;
7730 obp--;
7731 ibp++;
7732 while (ibp + 1 < limit) {
7733 if (ibp[0] == '*'
7734 && ibp[1] == '\\' && ibp[2] == '\n')
7735 newline_fix (ibp + 1);
7736 if (ibp[0] == '*' && ibp[1] == '/')
7737 break;
7738 ibp++;
7740 ibp += 2;
7741 break;
7743 case '\'':
7744 case '\"':
7745 /* Notice and skip strings, so that we don't
7746 think that comments start inside them,
7747 and so we don't duplicate newlines in them. */
7749 int quotec = c;
7750 while (ibp < limit) {
7751 *obp++ = c = *ibp++;
7752 if (c == quotec)
7753 break;
7754 if (c == '\n' && quotec == '\'')
7755 break;
7756 if (c == '\\' && ibp < limit) {
7757 while (*ibp == '\\' && ibp[1] == '\n')
7758 ibp += 2;
7759 *obp++ = *ibp++;
7763 break;
7767 return obp - start;
7770 /* Turn newlines to spaces in the string of length LENGTH at START,
7771 except inside of string constants.
7772 The string is copied into itself with its beginning staying fixed. */
7774 static int
7775 change_newlines (start, length)
7776 U_CHAR *start;
7777 int length;
7779 register U_CHAR *ibp;
7780 register U_CHAR *obp;
7781 register U_CHAR *limit;
7782 register int c;
7784 ibp = start;
7785 limit = start + length;
7786 obp = start;
7788 while (ibp < limit) {
7789 *obp++ = c = *ibp++;
7790 switch (c) {
7791 case '\n':
7792 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
7793 string. Skip past the newline and its duplicate.
7794 Put a space in the output. */
7795 if (*ibp == '\n')
7797 ibp++;
7798 obp--;
7799 *obp++ = ' ';
7801 break;
7803 case '\'':
7804 case '\"':
7805 /* Notice and skip strings, so that we don't delete newlines in them. */
7807 int quotec = c;
7808 while (ibp < limit) {
7809 *obp++ = c = *ibp++;
7810 if (c == quotec)
7811 break;
7812 if (c == '\n' && quotec == '\'')
7813 break;
7816 break;
7820 return obp - start;
7824 * error - print error message and increment count of errors.
7827 void
7828 error (msg, arg1, arg2, arg3)
7829 char *msg;
7830 char *arg1, *arg2, *arg3;
7832 int i;
7833 FILE_BUF *ip = NULL;
7835 print_containing_files ();
7837 for (i = indepth; i >= 0; i--)
7838 if (instack[i].fname != NULL) {
7839 ip = &instack[i];
7840 break;
7843 if (ip != NULL)
7844 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
7845 fprintf (stderr, msg, arg1, arg2, arg3);
7846 fprintf (stderr, "\n");
7847 errors++;
7850 /* Error including a message from `errno'. */
7852 static void
7853 error_from_errno (name)
7854 char *name;
7856 int i;
7857 FILE_BUF *ip = NULL;
7859 print_containing_files ();
7861 for (i = indepth; i >= 0; i--)
7862 if (instack[i].fname != NULL) {
7863 ip = &instack[i];
7864 break;
7867 if (ip != NULL)
7868 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
7870 if (errno < sys_nerr)
7871 fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
7872 else
7873 fprintf (stderr, "%s: undocumented I/O error\n", name);
7875 errors++;
7878 /* Print error message but don't count it. */
7880 void
7881 warning (msg, arg1, arg2, arg3)
7882 char *msg;
7883 char *arg1, *arg2, *arg3;
7885 int i;
7886 FILE_BUF *ip = NULL;
7888 if (inhibit_warnings)
7889 return;
7891 if (warnings_are_errors)
7892 errors++;
7894 print_containing_files ();
7896 for (i = indepth; i >= 0; i--)
7897 if (instack[i].fname != NULL) {
7898 ip = &instack[i];
7899 break;
7902 if (ip != NULL)
7903 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
7904 fprintf (stderr, "warning: ");
7905 fprintf (stderr, msg, arg1, arg2, arg3);
7906 fprintf (stderr, "\n");
7909 static void
7910 error_with_line (line, msg, arg1, arg2, arg3)
7911 int line;
7912 char *msg;
7913 char *arg1, *arg2, *arg3;
7915 int i;
7916 FILE_BUF *ip = NULL;
7918 print_containing_files ();
7920 for (i = indepth; i >= 0; i--)
7921 if (instack[i].fname != NULL) {
7922 ip = &instack[i];
7923 break;
7926 if (ip != NULL)
7927 fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
7928 fprintf (stderr, msg, arg1, arg2, arg3);
7929 fprintf (stderr, "\n");
7930 errors++;
7933 static void
7934 warning_with_line (line, msg, arg1, arg2, arg3)
7935 int line;
7936 char *msg;
7937 char *arg1, *arg2, *arg3;
7939 int i;
7940 FILE_BUF *ip = NULL;
7942 if (inhibit_warnings)
7943 return;
7945 if (warnings_are_errors)
7946 errors++;
7948 print_containing_files ();
7950 for (i = indepth; i >= 0; i--)
7951 if (instack[i].fname != NULL) {
7952 ip = &instack[i];
7953 break;
7956 if (ip != NULL)
7957 fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
7958 fprintf (stderr, "warning: ");
7959 fprintf (stderr, msg, arg1, arg2, arg3);
7960 fprintf (stderr, "\n");
7963 /* print an error message and maybe count it. */
7965 void
7966 pedwarn (msg, arg1, arg2, arg3)
7967 char *msg;
7968 char *arg1, *arg2, *arg3;
7970 if (pedantic_errors)
7971 error (msg, arg1, arg2, arg3);
7972 else
7973 warning (msg, arg1, arg2, arg3);
7976 void
7977 pedwarn_with_line (line, msg, arg1, arg2, arg3)
7978 int line;
7979 char *msg;
7980 char *arg1, *arg2, *arg3;
7982 if (pedantic_errors)
7983 error_with_line (line, msg, arg1, arg2, arg3);
7984 else
7985 warning_with_line (line, msg, arg1, arg2, arg3);
7988 /* Report a warning (or an error if pedantic_errors)
7989 giving specified file name and line number, not current. */
7991 static void
7992 pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
7993 char *file;
7994 int line;
7995 char *msg;
7996 char *arg1, *arg2, *arg3;
7998 int i;
7999 if (!pedantic_errors && inhibit_warnings)
8000 return;
8001 if (file != NULL)
8002 fprintf (stderr, "%s:%d: ", file, line);
8003 if (pedantic_errors)
8004 errors++;
8005 if (!pedantic_errors)
8006 fprintf (stderr, "warning: ");
8007 fprintf (stderr, msg, arg1, arg2, arg3);
8008 fprintf (stderr, "\n");
8011 /* Print the file names and line numbers of the #include
8012 commands which led to the current file. */
8014 static void
8015 print_containing_files ()
8017 FILE_BUF *ip = NULL;
8018 int i;
8019 int first = 1;
8021 /* If stack of files hasn't changed since we last printed
8022 this info, don't repeat it. */
8023 if (last_error_tick == input_file_stack_tick)
8024 return;
8026 for (i = indepth; i >= 0; i--)
8027 if (instack[i].fname != NULL) {
8028 ip = &instack[i];
8029 break;
8032 /* Give up if we don't find a source file. */
8033 if (ip == NULL)
8034 return;
8036 /* Find the other, outer source files. */
8037 for (i--; i >= 0; i--)
8038 if (instack[i].fname != NULL) {
8039 ip = &instack[i];
8040 if (first) {
8041 first = 0;
8042 fprintf (stderr, "In file included");
8043 } else {
8044 fprintf (stderr, ",\n ");
8047 fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
8049 if (! first)
8050 fprintf (stderr, ":\n");
8052 /* Record we have printed the status as of this time. */
8053 last_error_tick = input_file_stack_tick;
8056 /* Return the line at which an error occurred.
8057 The error is not necessarily associated with the current spot
8058 in the input stack, so LINE says where. LINE will have been
8059 copied from ip->lineno for the current input level.
8060 If the current level is for a file, we return LINE.
8061 But if the current level is not for a file, LINE is meaningless.
8062 In that case, we return the lineno of the innermost file. */
8064 static int
8065 line_for_error (line)
8066 int line;
8068 int i;
8069 int line1 = line;
8071 for (i = indepth; i >= 0; ) {
8072 if (instack[i].fname != 0)
8073 return line1;
8074 i--;
8075 if (i < 0)
8076 return 0;
8077 line1 = instack[i].lineno;
8079 abort ();
8080 /*NOTREACHED*/
8081 return 0;
8085 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
8087 * As things stand, nothing is ever placed in the output buffer to be
8088 * removed again except when it's KNOWN to be part of an identifier,
8089 * so flushing and moving down everything left, instead of expanding,
8090 * should work ok.
8093 /* You might think void was cleaner for the return type,
8094 but that would get type mismatch in check_expand in strict ANSI. */
8095 static int
8096 grow_outbuf (obuf, needed)
8097 register FILE_BUF *obuf;
8098 register int needed;
8100 register U_CHAR *p;
8101 int minsize;
8103 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
8104 return 0;
8106 /* Make it at least twice as big as it is now. */
8107 obuf->length *= 2;
8108 /* Make it have at least 150% of the free space we will need. */
8109 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
8110 if (minsize > obuf->length)
8111 obuf->length = minsize;
8113 if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
8114 memory_full ();
8116 obuf->bufp = p + (obuf->bufp - obuf->buf);
8117 obuf->buf = p;
8119 return 0;
8122 /* Symbol table for macro names and special symbols */
8125 * install a name in the main hash table, even if it is already there.
8126 * name stops with first non alphanumeric, except leading '#'.
8127 * caller must check against redefinition if that is desired.
8128 * delete_macro () removes things installed by install () in fifo order.
8129 * this is important because of the `defined' special symbol used
8130 * in #if, and also if pushdef/popdef directives are ever implemented.
8132 * If LEN is >= 0, it is the length of the name.
8133 * Otherwise, compute the length by scanning the entire name.
8135 * If HASH is >= 0, it is the precomputed hash code.
8136 * Otherwise, compute the hash code.
8138 static HASHNODE *
8139 install (name, len, type, ivalue, value, hash)
8140 U_CHAR *name;
8141 int len;
8142 enum node_type type;
8143 int ivalue;
8144 char *value;
8145 int hash;
8147 register HASHNODE *hp;
8148 register int i, bucket;
8149 register U_CHAR *p, *q;
8151 if (len < 0) {
8152 p = name;
8153 while (is_idchar[*p])
8154 p++;
8155 len = p - name;
8158 if (hash < 0)
8159 hash = hashf (name, len, HASHSIZE);
8161 i = sizeof (HASHNODE) + len + 1;
8162 hp = (HASHNODE *) xmalloc (i);
8163 bucket = hash;
8164 hp->bucket_hdr = &hashtab[bucket];
8165 hp->next = hashtab[bucket];
8166 hashtab[bucket] = hp;
8167 hp->prev = NULL;
8168 if (hp->next != NULL)
8169 hp->next->prev = hp;
8170 hp->type = type;
8171 hp->length = len;
8172 if (hp->type == T_CONST)
8173 hp->value.ival = ivalue;
8174 else
8175 hp->value.cpval = value;
8176 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
8177 p = hp->name;
8178 q = name;
8179 for (i = 0; i < len; i++)
8180 *p++ = *q++;
8181 hp->name[len] = 0;
8182 return hp;
8186 * find the most recent hash node for name name (ending with first
8187 * non-identifier char) installed by install
8189 * If LEN is >= 0, it is the length of the name.
8190 * Otherwise, compute the length by scanning the entire name.
8192 * If HASH is >= 0, it is the precomputed hash code.
8193 * Otherwise, compute the hash code.
8195 HASHNODE *
8196 lookup (name, len, hash)
8197 U_CHAR *name;
8198 int len;
8199 int hash;
8201 register U_CHAR *bp;
8202 register HASHNODE *bucket;
8204 if (len < 0) {
8205 for (bp = name; is_idchar[*bp]; bp++) ;
8206 len = bp - name;
8209 if (hash < 0)
8210 hash = hashf (name, len, HASHSIZE);
8212 bucket = hashtab[hash];
8213 while (bucket) {
8214 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
8215 return bucket;
8216 bucket = bucket->next;
8218 return NULL;
8222 * Delete a hash node. Some weirdness to free junk from macros.
8223 * More such weirdness will have to be added if you define more hash
8224 * types that need it.
8227 /* Note that the DEFINITION of a macro is removed from the hash table
8228 but its storage is not freed. This would be a storage leak
8229 except that it is not reasonable to keep undefining and redefining
8230 large numbers of macros many times.
8231 In any case, this is necessary, because a macro can be #undef'd
8232 in the middle of reading the arguments to a call to it.
8233 If #undef freed the DEFINITION, that would crash. */
8235 static void
8236 delete_macro (hp)
8237 HASHNODE *hp;
8240 if (hp->prev != NULL)
8241 hp->prev->next = hp->next;
8242 if (hp->next != NULL)
8243 hp->next->prev = hp->prev;
8245 /* make sure that the bucket chain header that
8246 the deleted guy was on points to the right thing afterwards. */
8247 if (hp == *hp->bucket_hdr)
8248 *hp->bucket_hdr = hp->next;
8250 #if 0
8251 if (hp->type == T_MACRO) {
8252 DEFINITION *d = hp->value.defn;
8253 struct reflist *ap, *nextap;
8255 for (ap = d->pattern; ap != NULL; ap = nextap) {
8256 nextap = ap->next;
8257 free (ap);
8259 free (d);
8261 #endif
8262 free (hp);
8266 * return hash function on name. must be compatible with the one
8267 * computed a step at a time, elsewhere
8269 static int
8270 hashf (name, len, hashsize)
8271 register U_CHAR *name;
8272 register int len;
8273 int hashsize;
8275 register int r = 0;
8277 while (len--)
8278 r = HASHSTEP (r, *name++);
8280 return MAKE_POS (r) % hashsize;
8284 /* Dump the definition of a single macro HP to OF. */
8285 static void
8286 dump_single_macro (hp, of)
8287 register HASHNODE *hp;
8288 FILE *of;
8290 register DEFINITION *defn = hp->value.defn;
8291 struct reflist *ap;
8292 int offset;
8293 int concat;
8296 /* Print the definition of the macro HP. */
8298 fprintf (of, "#define %s", hp->name);
8300 if (defn->nargs >= 0) {
8301 int i;
8303 fprintf (of, "(");
8304 for (i = 0; i < defn->nargs; i++) {
8305 dump_arg_n (defn, i, of);
8306 if (i + 1 < defn->nargs)
8307 fprintf (of, ", ");
8309 fprintf (of, ")");
8312 fprintf (of, " ");
8314 offset = 0;
8315 concat = 0;
8316 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
8317 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
8318 if (ap->nchars != 0)
8319 concat = 0;
8320 offset += ap->nchars;
8321 if (ap->stringify)
8322 fprintf (of, " #");
8323 if (ap->raw_before && !concat)
8324 fprintf (of, " ## ");
8325 concat = 0;
8326 dump_arg_n (defn, ap->argno, of);
8327 if (ap->raw_after) {
8328 fprintf (of, " ## ");
8329 concat = 1;
8332 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
8333 fprintf (of, "\n");
8336 /* Dump all macro definitions as #defines to stdout. */
8338 static void
8339 dump_all_macros ()
8341 int bucket;
8343 for (bucket = 0; bucket < HASHSIZE; bucket++) {
8344 register HASHNODE *hp;
8346 for (hp = hashtab[bucket]; hp; hp= hp->next) {
8347 if (hp->type == T_MACRO)
8348 dump_single_macro (hp, stdout);
8353 /* Output to OF a substring of a macro definition.
8354 BASE is the beginning of the definition.
8355 Output characters START thru LENGTH.
8356 Discard newlines outside of strings, thus
8357 converting funny-space markers to ordinary spaces. */
8359 static void
8360 dump_defn_1 (base, start, length, of)
8361 U_CHAR *base;
8362 int start;
8363 int length;
8364 FILE *of;
8366 U_CHAR *p = base + start;
8367 U_CHAR *limit = base + start + length;
8369 while (p < limit) {
8370 if (*p != '\n')
8371 putc (*p, of);
8372 else if (*p == '\"' || *p =='\'') {
8373 U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
8374 NULL_PTR, NULL_PTR);
8375 fwrite (p, p1 - p, 1, of);
8376 p = p1 - 1;
8378 p++;
8382 /* Print the name of argument number ARGNUM of macro definition DEFN
8383 to OF.
8384 Recall that DEFN->args.argnames contains all the arg names
8385 concatenated in reverse order with comma-space in between. */
8387 static void
8388 dump_arg_n (defn, argnum, of)
8389 DEFINITION *defn;
8390 int argnum;
8391 FILE *of;
8393 register U_CHAR *p = defn->args.argnames;
8394 while (argnum + 1 < defn->nargs) {
8395 p = (U_CHAR *) index (p, ' ') + 1;
8396 argnum++;
8399 while (*p && *p != ',') {
8400 putc (*p, of);
8401 p++;
8405 /* Initialize syntactic classifications of characters. */
8407 static void
8408 initialize_char_syntax ()
8410 register int i;
8413 * Set up is_idchar and is_idstart tables. These should be
8414 * faster than saying (is_alpha (c) || c == '_'), etc.
8415 * Set up these things before calling any routines tthat
8416 * refer to them.
8418 for (i = 'a'; i <= 'z'; i++) {
8419 is_idchar[i - 'a' + 'A'] = 1;
8420 is_idchar[i] = 1;
8421 is_idstart[i - 'a' + 'A'] = 1;
8422 is_idstart[i] = 1;
8424 for (i = '0'; i <= '9'; i++)
8425 is_idchar[i] = 1;
8426 is_idchar['_'] = 1;
8427 is_idstart['_'] = 1;
8428 is_idchar['$'] = dollars_in_ident;
8429 is_idstart['$'] = dollars_in_ident;
8431 /* horizontal space table */
8432 is_hor_space[' '] = 1;
8433 is_hor_space['\t'] = 1;
8434 is_hor_space['\v'] = 1;
8435 is_hor_space['\f'] = 1;
8436 is_hor_space['\r'] = 1;
8438 is_space[' '] = 1;
8439 is_space['\t'] = 1;
8440 is_space['\v'] = 1;
8441 is_space['\f'] = 1;
8442 is_space['\n'] = 1;
8443 is_space['\r'] = 1;
8446 /* Initialize the built-in macros. */
8448 static void
8449 initialize_builtins (inp, outp)
8450 FILE_BUF *inp;
8451 FILE_BUF *outp;
8453 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
8454 install ("__DATE__", -1, T_DATE, 0, 0, -1);
8455 install ("__FILE__", -1, T_FILE, 0, 0, -1);
8456 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
8457 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
8458 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
8459 #ifndef NO_BUILTIN_SIZE_TYPE
8460 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
8461 #endif
8462 #ifndef NO_BUILTIN_PTRDIFF_TYPE
8463 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
8464 #endif
8465 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
8466 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
8467 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
8468 install ("__TIME__", -1, T_TIME, 0, 0, -1);
8469 if (!traditional)
8470 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
8471 if (objc)
8472 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
8473 /* This is supplied using a -D by the compiler driver
8474 so that it is present only when truly compiling with GNU C. */
8475 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
8477 if (debug_output)
8479 char directive[2048];
8480 register struct directive *dp = &directive_table[0];
8481 struct tm *timebuf = timestamp ();
8483 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
8484 instack[0].nominal_fname);
8485 output_line_command (inp, outp, 0, same_file);
8486 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8488 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
8489 output_line_command (inp, outp, 0, same_file);
8490 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8492 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
8493 output_line_command (inp, outp, 0, same_file);
8494 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8496 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
8497 output_line_command (inp, outp, 0, same_file);
8498 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8500 sprintf (directive, " __WCHAR_TYPE__ %s\n", WCHAR_TYPE);
8501 output_line_command (inp, outp, 0, same_file);
8502 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8504 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
8505 monthnames[timebuf->tm_mon],
8506 timebuf->tm_mday, timebuf->tm_year + 1900);
8507 output_line_command (inp, outp, 0, same_file);
8508 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8510 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
8511 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
8512 output_line_command (inp, outp, 0, same_file);
8513 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8515 if (!traditional)
8517 sprintf (directive, " __STDC__ 1");
8518 output_line_command (inp, outp, 0, same_file);
8519 pass_thru_directive (directive, &directive[strlen (directive)],
8520 outp, dp);
8522 if (objc)
8524 sprintf (directive, " __OBJC__ 1");
8525 output_line_command (inp, outp, 0, same_file);
8526 pass_thru_directive (directive, &directive[strlen (directive)],
8527 outp, dp);
8533 * process a given definition string, for initialization
8534 * If STR is just an identifier, define it with value 1.
8535 * If STR has anything after the identifier, then it should
8536 * be identifier=definition.
8539 static void
8540 make_definition (str, op)
8541 U_CHAR *str;
8542 FILE_BUF *op;
8544 FILE_BUF *ip;
8545 struct directive *kt;
8546 U_CHAR *buf, *p;
8548 buf = str;
8549 p = str;
8550 if (!is_idstart[*p]) {
8551 error ("malformed option `-D %s'", str);
8552 return;
8554 while (is_idchar[*++p])
8556 if (*p == 0) {
8557 buf = (U_CHAR *) alloca (p - buf + 4);
8558 strcpy ((char *)buf, str);
8559 strcat ((char *)buf, " 1");
8560 } else if (*p != '=') {
8561 error ("malformed option `-D %s'", str);
8562 return;
8563 } else {
8564 U_CHAR *q;
8565 /* Copy the entire option so we can modify it. */
8566 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
8567 strncpy (buf, str, p - str);
8568 /* Change the = to a space. */
8569 buf[p - str] = ' ';
8570 /* Scan for any backslash-newline and remove it. */
8571 p++;
8572 q = &buf[p - str];
8573 while (*p) {
8574 if (*p == '\\' && p[1] == '\n')
8575 p += 2;
8576 /* Change newline chars into newline-markers. */
8577 else if (*p == '\n')
8579 *q++ = '\n';
8580 *q++ = '\n';
8581 p++;
8583 else
8584 *q++ = *p++;
8586 *q = 0;
8589 ip = &instack[++indepth];
8590 ip->nominal_fname = ip->fname = "*Initialization*";
8592 ip->buf = ip->bufp = buf;
8593 ip->length = strlen (buf);
8594 ip->lineno = 1;
8595 ip->macro = 0;
8596 ip->free_ptr = 0;
8597 ip->if_stack = if_stack;
8598 ip->system_header_p = 0;
8600 for (kt = directive_table; kt->type != T_DEFINE; kt++)
8603 /* Pass NULL instead of OP, since this is a "predefined" macro. */
8604 do_define (buf, buf + strlen (buf), NULL, kt);
8605 --indepth;
8608 /* JF, this does the work for the -U option */
8610 static void
8611 make_undef (str, op)
8612 U_CHAR *str;
8613 FILE_BUF *op;
8615 FILE_BUF *ip;
8616 struct directive *kt;
8618 ip = &instack[++indepth];
8619 ip->nominal_fname = ip->fname = "*undef*";
8621 ip->buf = ip->bufp = str;
8622 ip->length = strlen (str);
8623 ip->lineno = 1;
8624 ip->macro = 0;
8625 ip->free_ptr = 0;
8626 ip->if_stack = if_stack;
8627 ip->system_header_p = 0;
8629 for (kt = directive_table; kt->type != T_UNDEF; kt++)
8632 do_undef (str, str + strlen (str), op, kt);
8633 --indepth;
8636 /* Process the string STR as if it appeared as the body of a #assert.
8637 OPTION is the option name for which STR was the argument. */
8639 static void
8640 make_assertion (option, str)
8641 char *option;
8642 U_CHAR *str;
8644 FILE_BUF *ip;
8645 struct directive *kt;
8646 U_CHAR *buf, *p, *q;
8648 /* Copy the entire option so we can modify it. */
8649 buf = (U_CHAR *) alloca (strlen (str) + 1);
8650 strcpy ((char *) buf, str);
8651 /* Scan for any backslash-newline and remove it. */
8652 p = q = buf;
8653 while (*p) {
8654 if (*p == '\\' && p[1] == '\n')
8655 p += 2;
8656 else
8657 *q++ = *p++;
8659 *q = 0;
8661 p = buf;
8662 if (!is_idstart[*p]) {
8663 error ("malformed option `%s %s'", option, str);
8664 return;
8666 while (is_idchar[*++p])
8668 while (*p == ' ' || *p == '\t') p++;
8669 if (! (*p == 0 || *p == '(')) {
8670 error ("malformed option `%s %s'", option, str);
8671 return;
8674 ip = &instack[++indepth];
8675 ip->nominal_fname = ip->fname = "*Initialization*";
8677 ip->buf = ip->bufp = buf;
8678 ip->length = strlen (buf);
8679 ip->lineno = 1;
8680 ip->macro = 0;
8681 ip->free_ptr = 0;
8682 ip->if_stack = if_stack;
8683 ip->system_header_p = 0;
8685 for (kt = directive_table; kt->type != T_ASSERT; kt++)
8688 /* pass NULL as output ptr to do_define since we KNOW it never
8689 does any output.... */
8690 do_assert (buf, buf + strlen (buf) , NULL_PTR, kt);
8691 --indepth;
8694 /* Append a chain of `struct file_name_list's
8695 to the end of the main include chain.
8696 FIRST is the beginning of the chain to append, and LAST is the end. */
8698 static void
8699 append_include_chain (first, last)
8700 struct file_name_list *first, *last;
8702 struct file_name_list *dir;
8704 if (!first || !last)
8705 return;
8707 if (include == 0)
8708 include = first;
8709 else
8710 last_include->next = first;
8712 if (first_bracket_include == 0)
8713 first_bracket_include = first;
8715 for (dir = first; ; dir = dir->next) {
8716 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
8717 if (len > max_include_len)
8718 max_include_len = len;
8719 if (dir == last)
8720 break;
8723 last->next = NULL;
8724 last_include = last;
8727 /* Add output to `deps_buffer' for the -M switch.
8728 STRING points to the text to be output.
8729 SIZE is the number of bytes, or 0 meaning output until a null.
8730 Outputting the empty string breaks the line if it is long enough. */
8732 static void
8733 deps_output (string, size)
8734 char *string;
8735 unsigned size;
8737 if (size == 0)
8738 size = strlen (string);
8740 #ifndef MAX_OUTPUT_COLUMNS
8741 #define MAX_OUTPUT_COLUMNS 75
8742 #endif
8743 if (size == 0 && deps_column != 0
8744 && size + deps_column > MAX_OUTPUT_COLUMNS) {
8745 deps_output ("\\\n ", 0);
8746 deps_column = 0;
8749 if (deps_size + size + 1 > deps_allocated_size) {
8750 deps_allocated_size = deps_size + size + 50;
8751 deps_allocated_size *= 2;
8752 deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
8754 bcopy (string, &deps_buffer[deps_size], size);
8755 deps_size += size;
8756 deps_column += size;
8757 deps_buffer[deps_size] = 0;
8760 #if defined(USG) || defined(VMS)
8761 #ifndef BSTRING
8763 void
8764 bzero (b, length)
8765 register char *b;
8766 register unsigned length;
8768 while (length-- > 0)
8769 *b++ = 0;
8772 void
8773 bcopy (b1, b2, length)
8774 register char *b1;
8775 register char *b2;
8776 register unsigned length;
8778 while (length-- > 0)
8779 *b2++ = *b1++;
8783 bcmp (b1, b2, length) /* This could be a macro! */
8784 register char *b1;
8785 register char *b2;
8786 register unsigned length;
8788 while (length-- > 0)
8789 if (*b1++ != *b2++)
8790 return 1;
8792 return 0;
8794 #endif /* not BSTRING */
8795 #endif /* USG or VMS */
8798 static void
8799 fatal (str, arg)
8800 char *str, *arg;
8802 fprintf (stderr, "%s: ", progname);
8803 fprintf (stderr, str, arg);
8804 fprintf (stderr, "\n");
8805 exit (FAILURE_EXIT_CODE);
8808 /* More 'friendly' abort that prints the line and file.
8809 config.h can #define abort fancy_abort if you like that sort of thing. */
8811 void
8812 fancy_abort ()
8814 fatal ("Internal gcc abort.");
8817 static void
8818 perror_with_name (name)
8819 char *name;
8821 fprintf (stderr, "%s: ", progname);
8822 if (errno < sys_nerr)
8823 fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
8824 else
8825 fprintf (stderr, "%s: undocumented I/O error\n", name);
8826 errors++;
8829 static void
8830 pfatal_with_name (name)
8831 char *name;
8833 perror_with_name (name);
8834 #ifdef VMS
8835 exit (vaxc$errno);
8836 #else
8837 exit (FAILURE_EXIT_CODE);
8838 #endif
8841 /* Handler for SIGPIPE. */
8843 static void
8844 pipe_closed (signo)
8845 /* If this is missing, some compilers complain. */
8846 int signo;
8848 fatal ("output pipe has been closed");
8851 static void
8852 memory_full ()
8854 fatal ("Memory exhausted.");
8858 char *
8859 xmalloc (size)
8860 unsigned size;
8862 register char *ptr = (char *) malloc (size);
8863 if (ptr != 0) return (ptr);
8864 memory_full ();
8865 /*NOTREACHED*/
8866 return 0;
8869 static char *
8870 xrealloc (old, size)
8871 char *old;
8872 unsigned size;
8874 register char *ptr = (char *) realloc (old, size);
8875 if (ptr != 0) return (ptr);
8876 memory_full ();
8877 /*NOTREACHED*/
8878 return 0;
8881 static char *
8882 xcalloc (number, size)
8883 unsigned number, size;
8885 register unsigned total = number * size;
8886 register char *ptr = (char *) malloc (total);
8887 if (ptr != 0) {
8888 if (total > 100)
8889 bzero (ptr, total);
8890 else {
8891 /* It's not too long, so loop, zeroing by longs.
8892 It must be safe because malloc values are always well aligned. */
8893 register long *zp = (long *) ptr;
8894 register long *zl = (long *) (ptr + total - 4);
8895 register int i = total - 4;
8896 while (zp < zl)
8897 *zp++ = 0;
8898 if (i < 0)
8899 i = 0;
8900 while (i < total)
8901 ptr[i++] = 0;
8903 return ptr;
8905 memory_full ();
8906 /*NOTREACHED*/
8907 return 0;
8910 static char *
8911 savestring (input)
8912 char *input;
8914 unsigned size = strlen (input);
8915 char *output = xmalloc (size + 1);
8916 strcpy (output, input);
8917 return output;
8920 /* Get the file-mode and data size of the file open on FD
8921 and store them in *MODE_POINTER and *SIZE_POINTER. */
8923 static int
8924 file_size_and_mode (fd, mode_pointer, size_pointer)
8925 int fd;
8926 int *mode_pointer;
8927 long int *size_pointer;
8929 struct stat sbuf;
8931 if (fstat (fd, &sbuf) < 0) return (-1);
8932 if (mode_pointer) *mode_pointer = sbuf.st_mode;
8933 if (size_pointer) *size_pointer = sbuf.st_size;
8934 return 0;
8937 #ifdef VMS
8939 /* Under VMS we need to fix up the "include" specification
8940 filename so that everything following the 1st slash is
8941 changed into its correct VMS file specification. */
8943 static void
8944 hack_vms_include_specification (fname)
8945 char *fname;
8947 register char *cp, *cp1, *cp2;
8948 int f, check_filename_before_returning, no_prefix_seen;
8949 char Local[512];
8951 check_filename_before_returning = 0;
8952 no_prefix_seen = 0;
8954 /* Ignore leading "./"s */
8955 while (fname[0] == '.' && fname[1] == '/') {
8956 strcpy (fname, fname+2);
8957 no_prefix_seen = 1; /* mark this for later */
8959 /* Look for the boundary between the VMS and UNIX filespecs */
8960 cp = rindex (fname, ']'); /* Look for end of dirspec. */
8961 if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto */
8962 if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
8963 if (cp) {
8964 cp++;
8965 } else {
8966 cp = index (fname, '/'); /* Look for the "/" */
8969 cp2 = Local; /* initialize */
8971 /* We are trying to do a number of things here. First of all, we are
8972 trying to hammer the filenames into a standard format, such that later
8973 processing can handle them.
8975 If the file name contains something like [dir.], then it recognizes this
8976 as a root, and strips the ".]". Later processing will add whatever is
8977 needed to get things working properly.
8979 If no device is specified, then the first directory name is taken to be
8980 a device name (or a rooted logical). */
8982 /* See if we found that 1st slash */
8983 if (cp == 0) return; /* Nothing to do!!! */
8984 if (*cp != '/') return; /* Nothing to do!!! */
8985 /* Point to the UNIX filename part (which needs to be fixed!) */
8986 cp1 = cp+1;
8987 /* If the directory spec is not rooted, we can just copy
8988 the UNIX filename part and we are done */
8989 if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
8990 if (cp[-2] != '.') {
8992 * The VMS part ends in a `]', and the preceding character is not a `.'.
8993 * We strip the `]', and then splice the two parts of the name in the
8994 * usual way. Given the default locations for include files in cccp.c,
8995 * we will only use this code if the user specifies alternate locations
8996 * with the /include (-I) switch on the command line. */
8997 cp -= 1; /* Strip "]" */
8998 cp1--; /* backspace */
8999 } else {
9001 * The VMS part has a ".]" at the end, and this will not do. Later
9002 * processing will add a second directory spec, and this would be a syntax
9003 * error. Thus we strip the ".]", and thus merge the directory specs.
9004 * We also backspace cp1, so that it points to a '/'. This inhibits the
9005 * generation of the 000000 root directory spec (which does not belong here
9006 * in this case).
9008 cp -= 2; /* Strip ".]" */
9009 cp1--; }; /* backspace */
9010 } else {
9012 /* We drop in here if there is no VMS style directory specification yet.
9013 * If there is no device specification either, we make the first dir a
9014 * device and try that. If we do not do this, then we will be essentially
9015 * searching the users default directory (as if they did a #include "asdf.h").
9017 * Then all we need to do is to push a '[' into the output string. Later
9018 * processing will fill this in, and close the bracket.
9020 if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec. take first dir */
9021 *cp2++ = '['; /* Open the directory specification */
9024 /* at this point we assume that we have the device spec, and (at least
9025 the opening "[" for a directory specification. We may have directories
9026 specified already */
9028 /* If there are no other slashes then the filename will be
9029 in the "root" directory. Otherwise, we need to add
9030 directory specifications. */
9031 if (index (cp1, '/') == 0) {
9032 /* Just add "000000]" as the directory string */
9033 strcpy (cp2, "000000]");
9034 cp2 += strlen (cp2);
9035 check_filename_before_returning = 1; /* we might need to fool with this later */
9036 } else {
9037 /* As long as there are still subdirectories to add, do them. */
9038 while (index (cp1, '/') != 0) {
9039 /* If this token is "." we can ignore it */
9040 if ((cp1[0] == '.') && (cp1[1] == '/')) {
9041 cp1 += 2;
9042 continue;
9044 /* Add a subdirectory spec. Do not duplicate "." */
9045 if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
9046 *cp2++ = '.';
9047 /* If this is ".." then the spec becomes "-" */
9048 if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
9049 /* Add "-" and skip the ".." */
9050 *cp2++ = '-';
9051 cp1 += 3;
9052 continue;
9054 /* Copy the subdirectory */
9055 while (*cp1 != '/') *cp2++= *cp1++;
9056 cp1++; /* Skip the "/" */
9058 /* Close the directory specification */
9059 if (cp2[-1] == '.') /* no trailing periods */
9060 cp2--;
9061 *cp2++ = ']';
9063 /* Now add the filename */
9064 while (*cp1) *cp2++ = *cp1++;
9065 *cp2 = 0;
9066 /* Now append it to the original VMS spec. */
9067 strcpy (cp, Local);
9069 /* If we put a [000000] in the filename, try to open it first. If this fails,
9070 remove the [000000], and return that name. This provides flexibility
9071 to the user in that they can use both rooted and non-rooted logical names
9072 to point to the location of the file. */
9074 if (check_filename_before_returning && no_prefix_seen) {
9075 f = open (fname, O_RDONLY, 0666);
9076 if (f >= 0) {
9077 /* The file name is OK as it is, so return it as is. */
9078 close (f);
9079 return;
9081 /* The filename did not work. Try to remove the [000000] from the name,
9082 and return it. */
9083 cp = index (fname, '[');
9084 cp2 = index (fname, ']') + 1;
9085 strcpy (cp, cp2); /* this gets rid of it */
9087 return;
9089 #endif /* VMS */
9091 #ifdef VMS
9093 /* These are the read/write replacement routines for
9094 VAX-11 "C". They make read/write behave enough
9095 like their UNIX counterparts that CCCP will work */
9097 static int
9098 read (fd, buf, size)
9099 int fd;
9100 char *buf;
9101 int size;
9103 #undef read /* Get back the REAL read routine */
9104 register int i;
9105 register int total = 0;
9107 /* Read until the buffer is exhausted */
9108 while (size > 0) {
9109 /* Limit each read to 32KB */
9110 i = (size > (32*1024)) ? (32*1024) : size;
9111 i = read (fd, buf, i);
9112 if (i <= 0) {
9113 if (i == 0) return (total);
9114 return (i);
9116 /* Account for this read */
9117 total += i;
9118 buf += i;
9119 size -= i;
9121 return (total);
9124 static int
9125 write (fd, buf, size)
9126 int fd;
9127 char *buf;
9128 int size;
9130 #undef write /* Get back the REAL write routine */
9131 int i;
9132 int j;
9134 /* Limit individual writes to 32Kb */
9135 i = size;
9136 while (i > 0) {
9137 j = (i > (32*1024)) ? (32*1024) : i;
9138 if (write (fd, buf, j) < 0) return (-1);
9139 /* Account for the data written */
9140 buf += j;
9141 i -= j;
9143 return (size);
9146 /* The following wrapper functions supply additional arguments to the VMS
9147 I/O routines to optimize performance with file handling. The arguments
9148 are:
9149 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
9150 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
9151 "fop=tef"- Truncate unused portions of file when closing file.
9152 "shr=nil"- Disallow file sharing while file is open.
9155 static FILE *
9156 freopen (fname, type, oldfile)
9157 char *fname;
9158 char *type;
9159 FILE *oldfile;
9161 #undef freopen /* Get back the REAL fopen routine */
9162 if (strcmp (type, "w") == 0)
9163 return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
9164 return freopen (fname, type, oldfile, "mbc=16");
9167 static FILE *
9168 fopen (fname, type)
9169 char *fname;
9170 char *type;
9172 #undef fopen /* Get back the REAL fopen routine */
9173 if (strcmp (type, "w") == 0)
9174 return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
9175 return fopen (fname, type, "mbc=16");
9178 static int
9179 open (fname, flags, prot)
9180 char *fname;
9181 int flags;
9182 int prot;
9184 #undef open /* Get back the REAL open routine */
9185 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
9188 /* Avoid run-time library bug, where copying M out of N+M characters with
9189 N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
9190 gcc-cpp exercises this particular bug. */
9192 static char *
9193 strncat (dst, src, cnt)
9194 char *dst;
9195 const char *src;
9196 unsigned cnt;
9198 register char *d = dst, *s = (char *) src;
9199 register int n = cnt; /* convert to _signed_ type */
9201 while (*d) d++; /* advance to end */
9202 while (--n >= 0)
9203 if (!(*d++ = *s++)) break;
9204 if (n < 0) *d = '\0';
9205 return dst;
9207 #endif /* VMS */