]> gcc.gnu.org Git - gcc.git/blob - gcc/cccp.c
*** empty log message ***
[gcc.git] / gcc / cccp.c
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
5
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.
10
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.
15
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.
19
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! */
23 \f
24 typedef unsigned char U_CHAR;
25
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 */
35
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 */
41
42 #ifndef STANDARD_INCLUDE_DIR
43 #define STANDARD_INCLUDE_DIR "/usr/include"
44 #endif
45
46 #ifndef LOCAL_INCLUDE_DIR
47 #define LOCAL_INCLUDE_DIR "/usr/local/include"
48 #endif
49
50 #include "pcp.h"
51
52 #ifndef STDC_VALUE
53 #define STDC_VALUE 1
54 #endif
55
56 /* In case config.h defines these. */
57 #undef bcopy
58 #undef bzero
59 #undef bcmp
60
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <ctype.h>
64 #include <stdio.h>
65 #include <signal.h>
66
67 #ifndef VMS
68 #ifndef USG
69 #include <sys/time.h> /* for __DATE__ and __TIME__ */
70 #include <sys/resource.h>
71 #else
72 #define index strchr
73 #define rindex strrchr
74 #include <time.h>
75 #include <fcntl.h>
76 #endif /* USG */
77 #endif /* not VMS */
78
79 extern char *index ();
80 extern char *rindex ();
81
82 /* VMS-specific definitions */
83 #ifdef VMS
84 #include <time.h>
85 #include <errno.h> /* This defines "errno" properly */
86 #include <perror.h> /* This defines sys_errlist/sys_nerr properly */
87 #include <descrip.h>
88 #define O_RDONLY 0 /* Open arg for Read/Only */
89 #define O_WRONLY 1 /* Open arg for Write/Only */
90 #define read(fd,buf,size) VMS_read(fd,buf,size)
91 #define write(fd,buf,size) VMS_write(fd,buf,size)
92 #define open(fname,mode,prot) VMS_open(fname,mode,prot)
93 #define fopen(fname,mode) VMS_fopen(fname,mode)
94 #define freopen(fname,mode,ofile) VMS_freopen(fname,mode,ofile)
95 static int VMS_read ();
96 static int VMS_write ();
97 static int VMS_open ();
98 static FILE * VMS_fopen ();
99 static FILE * VMS_freopen ();
100 static void hack_vms_include_specification ();
101 typedef struct { unsigned :16, :16, :16; } vms_ino_t;
102 #define ino_t vms_ino_t
103 #ifdef __GNUC__
104 #define BSTRING /* VMS/GCC supplies the bstring routines */
105 #endif /* __GNUC__ */
106 #endif /* VMS */
107
108 #ifndef O_RDONLY
109 #define O_RDONLY 0
110 #endif
111
112 #undef MIN
113 #undef MAX
114 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
115 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
116
117 #ifndef S_ISREG
118 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
119 #endif
120
121 /* Exported declarations. */
122
123 char *xmalloc ();
124 void error ();
125 void warning ();
126
127 /* External declarations. */
128
129 extern char *getenv ();
130 extern FILE *fdopen ();
131 extern char *version_string;
132 extern struct tm *localtime ();
133 extern int sys_nerr;
134 extern char *sys_errlist[];
135
136 #ifndef errno
137 extern int errno;
138 #endif
139
140 /* Forward declarations. */
141
142 struct directive;
143 struct file_buf;
144 struct arglist;
145 struct argdata;
146
147 #if defined(USG) || defined(VMS)
148 #ifndef BSTRING
149 void bcopy ();
150 void bzero ();
151 int bcmp ();
152 #endif
153 #endif
154
155 /* These functions are declared to return int instead of void since they
156 are going to be placed in a table and some old compilers have trouble with
157 pointers to functions returning void. */
158
159 static int do_define ();
160 static int do_line ();
161 static int do_include ();
162 static int do_undef ();
163 static int do_error ();
164 static int do_pragma ();
165 static int do_ident ();
166 static int do_if ();
167 static int do_xifdef ();
168 static int do_else ();
169 static int do_elif ();
170 static int do_endif ();
171 static int do_sccs ();
172 static int do_once ();
173 static int do_assert ();
174 static int do_unassert ();
175 static int do_warning ();
176
177 static void add_import ();
178 static void deps_output ();
179 static void make_undef ();
180 static void make_definition ();
181 static void make_assertion ();
182 static void path_include ();
183 static void initialize_builtins ();
184 static void initialize_char_syntax ();
185 static void dump_arg_n ();
186 static void dump_defn_1 ();
187 static void delete_macro ();
188 static void trigraph_pcp ();
189 static void rescan ();
190 static void finclude ();
191 static void validate_else ();
192 static int comp_def_part ();
193 static void error_from_errno ();
194 static void error_with_line ();
195 void pedwarn ();
196 static void pedwarn_with_file_and_line ();
197 static void fatal ();
198 void fancy_abort ();
199 static void pfatal_with_name ();
200 static void perror_with_name ();
201 static void print_containing_files ();
202 static int lookup_import ();
203 static int lookup_include ();
204 static int check_preconditions ();
205 static void pcfinclude ();
206 static void pcstring_used ();
207 static void write_output ();
208 static int check_macro_name ();
209 static int compare_defs ();
210 static int compare_token_lists ();
211 static int eval_if_expression ();
212 static int discard_comments ();
213 static int delete_newlines ();
214 static int line_for_error ();
215 static int hashf ();
216 static int file_size_and_mode ();
217
218 static struct arglist *read_token_list ();
219 static void free_token_list ();
220
221 static struct hashnode *install ();
222 struct hashnode *lookup ();
223
224 static struct assertion_hashnode *assertion_install ();
225 static struct assertion_hashnode *assertion_lookup ();
226
227 static char *xrealloc ();
228 static char *xcalloc ();
229 static char *savestring ();
230
231 static void delete_assertion ();
232 static void macroexpand ();
233 static void dump_all_macros ();
234 static void conditional_skip ();
235 static void skip_if_group ();
236 static void output_line_command ();
237
238 /* Last arg to output_line_command. */
239 enum file_change_code {same_file, enter_file, leave_file};
240
241 static int grow_outbuf ();
242 static int handle_directive ();
243 static void memory_full ();
244
245 static U_CHAR *macarg1 ();
246 static char *macarg ();
247
248 static U_CHAR *skip_to_end_of_comment ();
249 static U_CHAR *skip_quoted_string ();
250 static U_CHAR *skip_paren_group ();
251
252 static char *check_precompiled ();
253 static struct macrodef create_definition ();
254 static void dump_single_macro ();
255 \f
256 #ifndef FAILURE_EXIT_CODE
257 #define FAILURE_EXIT_CODE 33 /* gnu cc command understands this */
258 #endif
259
260 #ifndef SUCCESS_EXIT_CODE
261 #define SUCCESS_EXIT_CODE 0 /* 0 means success on Unix. */
262 #endif
263
264 /* Name under which this program was invoked. */
265
266 static char *progname;
267
268 /* Nonzero means handle C++ comment syntax and use
269 extra default include directories for C++. */
270
271 static int cplusplus;
272
273 /* Nonzero means handle #import, for objective C. */
274
275 static int objc;
276
277 /* Nonzero means this is an assembly file, and allow
278 unknown directives, which could be comments. */
279
280 static int lang_asm;
281
282 /* Current maximum length of directory names in the search path
283 for include files. (Altered as we get more of them.) */
284
285 static int max_include_len;
286
287 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
288
289 static int lint = 0;
290
291 /* Nonzero means copy comments into the output file. */
292
293 static int put_out_comments = 0;
294
295 /* Nonzero means don't process the ANSI trigraph sequences. */
296
297 static int no_trigraphs = 0;
298
299 /* Nonzero means print the names of included files rather than
300 the preprocessed output. 1 means just the #include "...",
301 2 means #include <...> as well. */
302
303 static int print_deps = 0;
304
305 /* Nonzero means print names of header files (-H). */
306
307 static int print_include_names = 0;
308
309 /* Nonzero means don't output line number information. */
310
311 static int no_line_commands;
312
313 /* dump_only means inhibit output of the preprocessed text
314 and instead output the definitions of all user-defined
315 macros in a form suitable for use as input to cccp.
316 dump_names means pass #define and the macro name through to output.
317 dump_definitions means pass the whole definition (plus #define) through
318 */
319
320 static enum {dump_none, dump_only, dump_names, dump_definitions}
321 dump_macros = dump_none;
322
323 /* Nonzero means pass all #define and #undef directives which we actually
324 process through to the output stream. This feature is used primarily
325 to allow cc1 to record the #defines and #undefs for the sake of
326 debuggers which understand about preprocessor macros, but it may
327 also be useful with -E to figure out how symbols are defined, and
328 where they are defined. */
329 static int debug_output = 0;
330
331 /* Holds local startup time. */
332 static struct tm *timebuf = NULL;
333
334 /* Nonzero indicates special processing used by the pcp program. The
335 special effects of this mode are:
336
337 Inhibit all macro expansion, except those inside #if directives.
338
339 Process #define directives normally, and output their contents
340 to the output file.
341
342 Output preconditions to pcp_outfile indicating all the relevant
343 preconditions for use of this file in a later cpp run.
344 */
345 static FILE *pcp_outfile;
346
347 /* Nonzero means we are inside an IF during a -pcp run. In this mode
348 macro expansion is done, and preconditions are output for all macro
349 uses requiring them. */
350 static int pcp_inside_if;
351
352 /* Nonzero means never to include precompiled files. */
353 static int no_precomp;
354
355 /* Nonzero means give all the error messages the ANSI standard requires. */
356
357 int pedantic;
358
359 /* Nonzero means try to make failure to fit ANSI C an error. */
360
361 static int pedantic_errors;
362
363 /* Nonzero means don't print warning messages. -w. */
364
365 static int inhibit_warnings = 0;
366
367 /* Nonzero means warn if slash-star appears in a comment. */
368
369 static int warn_comments;
370
371 /* Nonzero means warn if a macro argument is (or would be)
372 stringified with -traditional. */
373
374 static int warn_stringify;
375
376 /* Nonzero means warn if there are any trigraphs. */
377
378 static int warn_trigraphs;
379
380 /* Nonzero means warn if #import is used. */
381
382 static int warn_import = 1;
383
384 /* Nonzero means turn warnings into errors. */
385
386 static int warnings_are_errors;
387
388 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
389
390 int traditional;
391
392 /* Nonzero causes output not to be done,
393 but directives such as #define that have side effects
394 are still obeyed. */
395
396 static int no_output;
397
398 /* Nonzero means that we have finished processing the command line options.
399 This flag is used to decide whether or not to issue certain errors
400 and/or warnings. */
401
402 static int done_initializing = 0;
403 \f
404 /* I/O buffer structure.
405 The `fname' field is nonzero for source files and #include files
406 and for the dummy text used for -D and -U.
407 It is zero for rescanning results of macro expansion
408 and for expanding macro arguments. */
409 #define INPUT_STACK_MAX 200
410 static struct file_buf {
411 char *fname;
412 /* Filename specified with #line command. */
413 char *nominal_fname;
414 /* Record where in the search path this file was found.
415 For #include_next. */
416 struct file_name_list *dir;
417 int lineno;
418 int length;
419 U_CHAR *buf;
420 U_CHAR *bufp;
421 /* Macro that this level is the expansion of.
422 Included so that we can reenable the macro
423 at the end of this level. */
424 struct hashnode *macro;
425 /* Value of if_stack at start of this file.
426 Used to prohibit unmatched #endif (etc) in an include file. */
427 struct if_stack *if_stack;
428 /* Object to be freed at end of input at this level. */
429 U_CHAR *free_ptr;
430 /* True if this is a header file included using <FILENAME>. */
431 char system_header_p;
432 } instack[INPUT_STACK_MAX];
433
434 static int last_error_tick; /* Incremented each time we print it. */
435 static int input_file_stack_tick; /* Incremented when the status changes. */
436
437 /* Current nesting level of input sources.
438 `instack[indepth]' is the level currently being read. */
439 static int indepth = -1;
440 #define CHECK_DEPTH(code) \
441 if (indepth >= (INPUT_STACK_MAX - 1)) \
442 { \
443 error_with_line (line_for_error (instack[indepth].lineno), \
444 "macro or `#include' recursion too deep"); \
445 code; \
446 }
447
448 /* Current depth in #include directives that use <...>. */
449 static int system_include_depth = 0;
450
451 typedef struct file_buf FILE_BUF;
452
453 /* The output buffer. Its LENGTH field is the amount of room allocated
454 for the buffer, not the number of chars actually present. To get
455 that, subtract outbuf.buf from outbuf.bufp. */
456
457 #define OUTBUF_SIZE 10 /* initial size of output buffer */
458 static FILE_BUF outbuf;
459
460 /* Grow output buffer OBUF points at
461 so it can hold at least NEEDED more chars. */
462
463 #define check_expand(OBUF, NEEDED) \
464 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
465 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
466
467 struct file_name_list
468 {
469 struct file_name_list *next;
470 char *fname;
471 /* If the following is nonzero, it is a macro name.
472 Don't include the file again if that macro is defined. */
473 U_CHAR *control_macro;
474 };
475
476 /* #include "file" looks in source file dir, then stack. */
477 /* #include <file> just looks in the stack. */
478 /* -I directories are added to the end, then the defaults are added. */
479 static struct default_include { char *fname; int cplusplus; } include_defaults_array[]
480 #ifdef INCLUDE_DEFAULTS
481 = INCLUDE_DEFAULTS;
482 #else
483 = {
484 /* Pick up GNU C++ specific include files. */
485 { GPLUSPLUS_INCLUDE_DIR, 1},
486 { GCC_INCLUDE_DIR, 0},
487 #ifdef CROSS_COMPILE
488 /* For cross-compilation, this dir name is generated
489 automatically in Makefile.in. */
490 { CROSS_INCLUDE_DIR, 0 },
491 #else /* not CROSS_COMPILE */
492 { LOCAL_INCLUDE_DIR, 0},
493 /* Some systems have an extra dir of include files. */
494 #ifdef SYSTEM_INCLUDE_DIR
495 { SYSTEM_INCLUDE_DIR, 0},
496 #endif
497 { STANDARD_INCLUDE_DIR, 0},
498 #endif /* not CROSS_COMPILE */
499 { 0, 0}
500 };
501 #endif /* no INCLUDE_DEFAULTS */
502
503 /* The code looks at the defaults through this pointer, rather than through
504 the constant structure above. This pointer gets changed if an environment
505 variable specifies other defaults. */
506 static struct default_include *include_defaults = include_defaults_array;
507
508 static struct file_name_list *include = 0; /* First dir to search */
509 /* First dir to search for <file> */
510 static struct file_name_list *first_bracket_include = 0;
511 static struct file_name_list *last_include = 0; /* Last in chain */
512
513 /* Chain of include directories to put at the end of the other chain. */
514 static struct file_name_list *after_include = 0;
515 static struct file_name_list *last_after_include = 0; /* Last in chain */
516
517 /* List of included files that contained #pragma once. */
518 static struct file_name_list *dont_repeat_files = 0;
519
520 /* List of other included files.
521 If ->control_macro if nonzero, the file had a #ifndef
522 around the entire contents, and ->control_macro gives the macro name. */
523 static struct file_name_list *all_include_files = 0;
524
525 /* Global list of strings read in from precompiled files. This list
526 is kept in the order the strings are read in, with new strings being
527 added at the end through stringlist_tailp. We use this list to output
528 the strings at the end of the run.
529 */
530 static STRINGDEF *stringlist;
531 static STRINGDEF **stringlist_tailp = &stringlist;
532
533
534 /* Structure returned by create_definition */
535 typedef struct macrodef MACRODEF;
536 struct macrodef
537 {
538 struct definition *defn;
539 U_CHAR *symnam;
540 int symlen;
541 };
542
543 \f
544 /* Structure allocated for every #define. For a simple replacement
545 such as
546 #define foo bar ,
547 nargs = -1, the `pattern' list is null, and the expansion is just
548 the replacement text. Nargs = 0 means a functionlike macro with no args,
549 e.g.,
550 #define getchar() getc (stdin) .
551 When there are args, the expansion is the replacement text with the
552 args squashed out, and the reflist is a list describing how to
553 build the output from the input: e.g., "3 chars, then the 1st arg,
554 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
555 The chars here come from the expansion. Whatever is left of the
556 expansion after the last arg-occurrence is copied after that arg.
557 Note that the reflist can be arbitrarily long---
558 its length depends on the number of times the arguments appear in
559 the replacement text, not how many args there are. Example:
560 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
561 pattern list
562 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
563 where (x, y) means (nchars, argno). */
564
565 typedef struct definition DEFINITION;
566 struct definition {
567 int nargs;
568 int length; /* length of expansion string */
569 int predefined; /* True if the macro was builtin or */
570 /* came from the command line */
571 U_CHAR *expansion;
572 int line; /* Line number of definition */
573 char *file; /* File of definition */
574 struct reflist {
575 struct reflist *next;
576 char stringify; /* nonzero if this arg was preceded by a
577 # operator. */
578 char raw_before; /* Nonzero if a ## operator before arg. */
579 char raw_after; /* Nonzero if a ## operator after arg. */
580 int nchars; /* Number of literal chars to copy before
581 this arg occurrence. */
582 int argno; /* Number of arg to substitute (origin-0) */
583 } *pattern;
584 union {
585 /* Names of macro args, concatenated in reverse order
586 with comma-space between them.
587 The only use of this is that we warn on redefinition
588 if this differs between the old and new definitions. */
589 U_CHAR *argnames;
590 } args;
591 };
592
593 /* different kinds of things that can appear in the value field
594 of a hash node. Actually, this may be useless now. */
595 union hashval {
596 int ival;
597 char *cpval;
598 DEFINITION *defn;
599 KEYDEF *keydef;
600 };
601
602
603 /* The structure of a node in the hash table. The hash table
604 has entries for all tokens defined by #define commands (type T_MACRO),
605 plus some special tokens like __LINE__ (these each have their own
606 type, and the appropriate code is run when that type of node is seen.
607 It does not contain control words like "#define", which are recognized
608 by a separate piece of code. */
609
610 /* different flavors of hash nodes --- also used in keyword table */
611 enum node_type {
612 T_DEFINE = 1, /* the `#define' keyword */
613 T_INCLUDE, /* the `#include' keyword */
614 T_INCLUDE_NEXT, /* the `#include_next' keyword */
615 T_IMPORT, /* the `#import' keyword */
616 T_IFDEF, /* the `#ifdef' keyword */
617 T_IFNDEF, /* the `#ifndef' keyword */
618 T_IF, /* the `#if' keyword */
619 T_ELSE, /* `#else' */
620 T_PRAGMA, /* `#pragma' */
621 T_ELIF, /* `#elif' */
622 T_UNDEF, /* `#undef' */
623 T_LINE, /* `#line' */
624 T_ERROR, /* `#error' */
625 T_WARNING, /* `#warning' */
626 T_ENDIF, /* `#endif' */
627 T_SCCS, /* `#sccs', used on system V. */
628 T_IDENT, /* `#ident', used on system V. */
629 T_ASSERT, /* `#assert', taken from system V. */
630 T_UNASSERT, /* `#unassert', taken from system V. */
631 T_SPECLINE, /* special symbol `__LINE__' */
632 T_DATE, /* `__DATE__' */
633 T_FILE, /* `__FILE__' */
634 T_BASE_FILE, /* `__BASE_FILE__' */
635 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
636 T_VERSION, /* `__VERSION__' */
637 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
638 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
639 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
640 T_TIME, /* `__TIME__' */
641 T_CONST, /* Constant value, used by `__STDC__' */
642 T_MACRO, /* macro defined by `#define' */
643 T_DISABLED, /* macro temporarily turned off for rescan */
644 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
645 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
646 T_UNUSED /* Used for something not defined. */
647 };
648
649 struct hashnode {
650 struct hashnode *next; /* double links for easy deletion */
651 struct hashnode *prev;
652 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
653 chain is kept, in case the node is the head
654 of the chain and gets deleted. */
655 enum node_type type; /* type of special token */
656 int length; /* length of token, for quick comparison */
657 U_CHAR *name; /* the actual name */
658 union hashval value; /* pointer to expansion, or whatever */
659 };
660
661 typedef struct hashnode HASHNODE;
662
663 /* Some definitions for the hash table. The hash function MUST be
664 computed as shown in hashf () below. That is because the rescan
665 loop computes the hash value `on the fly' for most tokens,
666 in order to avoid the overhead of a lot of procedure calls to
667 the hashf () function. Hashf () only exists for the sake of
668 politeness, for use when speed isn't so important. */
669
670 #define HASHSIZE 1403
671 static HASHNODE *hashtab[HASHSIZE];
672 #define HASHSTEP(old, c) ((old << 2) + c)
673 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
674
675 /* Symbols to predefine. */
676
677 #ifdef CPP_PREDEFINES
678 static char *predefs = CPP_PREDEFINES;
679 #else
680 static char *predefs = "";
681 #endif
682 \f
683 /* We let tm.h override the types used here, to handle trivial differences
684 such as the choice of unsigned int or long unsigned int for size_t.
685 When machines start needing nontrivial differences in the size type,
686 it would be best to do something here to figure out automatically
687 from other information what type to use. */
688
689 /* The string value for __size_type__. */
690
691 #ifndef SIZE_TYPE
692 #define SIZE_TYPE "long unsigned int"
693 #endif
694
695 /* The string value for __ptrdiff_type__. */
696
697 #ifndef PTRDIFF_TYPE
698 #define PTRDIFF_TYPE "long int"
699 #endif
700
701 /* The string value for __wchar_type__. */
702
703 #ifndef WCHAR_TYPE
704 #define WCHAR_TYPE "int"
705 #endif
706 \f
707 /* In the definition of a #assert name, this structure forms
708 a list of the individual values asserted.
709 Each value is itself a list of "tokens".
710 These are strings that are compared by name. */
711
712 struct tokenlist_list {
713 struct tokenlist_list *next;
714 struct arglist *tokens;
715 };
716
717 struct assertion_hashnode {
718 struct assertion_hashnode *next; /* double links for easy deletion */
719 struct assertion_hashnode *prev;
720 /* also, a back pointer to this node's hash
721 chain is kept, in case the node is the head
722 of the chain and gets deleted. */
723 struct assertion_hashnode **bucket_hdr;
724 int length; /* length of token, for quick comparison */
725 U_CHAR *name; /* the actual name */
726 /* List of token-sequences. */
727 struct tokenlist_list *value;
728 };
729
730 typedef struct assertion_hashnode ASSERTION_HASHNODE;
731
732 /* Some definitions for the hash table. The hash function MUST be
733 computed as shown in hashf () below. That is because the rescan
734 loop computes the hash value `on the fly' for most tokens,
735 in order to avoid the overhead of a lot of procedure calls to
736 the hashf () function. Hashf () only exists for the sake of
737 politeness, for use when speed isn't so important. */
738
739 #define ASSERTION_HASHSIZE 37
740 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
741
742 /* Nonzero means inhibit macroexpansion of what seem to be
743 assertion tests, in rescan. For #if. */
744 static int assertions_flag;
745 \f
746 /* `struct directive' defines one #-directive, including how to handle it. */
747
748 struct directive {
749 int length; /* Length of name */
750 int (*func)(); /* Function to handle directive */
751 char *name; /* Name of directive */
752 enum node_type type; /* Code which describes which directive. */
753 char angle_brackets; /* Nonzero => <...> is special. */
754 char traditional_comments; /* Nonzero: keep comments if -traditional. */
755 char pass_thru; /* Copy preprocessed directive to output file. */
756 };
757
758 /* Here is the actual list of #-directives, most-often-used first. */
759
760 static struct directive directive_table[] = {
761 { 6, do_define, "define", T_DEFINE, 0, 1},
762 { 2, do_if, "if", T_IF},
763 { 5, do_xifdef, "ifdef", T_IFDEF},
764 { 6, do_xifdef, "ifndef", T_IFNDEF},
765 { 5, do_endif, "endif", T_ENDIF},
766 { 4, do_else, "else", T_ELSE},
767 { 4, do_elif, "elif", T_ELIF},
768 { 4, do_line, "line", T_LINE},
769 { 7, do_include, "include", T_INCLUDE, 1},
770 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
771 { 6, do_include, "import", T_IMPORT, 1},
772 { 5, do_undef, "undef", T_UNDEF},
773 { 5, do_error, "error", T_ERROR},
774 { 7, do_warning, "warning", T_WARNING},
775 #ifdef SCCS_DIRECTIVE
776 { 4, do_sccs, "sccs", T_SCCS},
777 #endif
778 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
779 { 5, do_ident, "ident", T_IDENT, 0, 0, 1},
780 { 6, do_assert, "assert", T_ASSERT},
781 { 8, do_unassert, "unassert", T_UNASSERT},
782 { -1, 0, "", T_UNUSED},
783 };
784
785 /* When a directive handler is called,
786 this points to the # that started the directive. */
787 U_CHAR *directive_start;
788
789 /* table to tell if char can be part of a C identifier. */
790 U_CHAR is_idchar[256];
791 /* table to tell if char can be first char of a c identifier. */
792 U_CHAR is_idstart[256];
793 /* table to tell if c is horizontal space. */
794 U_CHAR is_hor_space[256];
795 /* table to tell if c is horizontal or vertical space. */
796 static U_CHAR is_space[256];
797
798 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
799 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
800
801 static int errors = 0; /* Error counter for exit code */
802
803 /* Zero means dollar signs are punctuation.
804 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
805 This must be 0 for correct processing of this ANSI C program:
806 #define foo(a) #a
807 #define lose(b) foo(b)
808 #define test$
809 lose(test) */
810 static int dollars_in_ident;
811 #ifndef DOLLARS_IN_IDENTIFIERS
812 #define DOLLARS_IN_IDENTIFIERS 1
813 #endif
814
815 static FILE_BUF expand_to_temp_buffer ();
816
817 static DEFINITION *collect_expansion ();
818
819 /* Stack of conditionals currently in progress
820 (including both successful and failing conditionals). */
821
822 struct if_stack {
823 struct if_stack *next; /* for chaining to the next stack frame */
824 char *fname; /* copied from input when frame is made */
825 int lineno; /* similarly */
826 int if_succeeded; /* true if a leg of this if-group
827 has been passed through rescan */
828 U_CHAR *control_macro; /* For #ifndef at start of file,
829 this is the macro name tested. */
830 enum node_type type; /* type of last directive seen in this group */
831 };
832 typedef struct if_stack IF_STACK_FRAME;
833 static IF_STACK_FRAME *if_stack = NULL;
834
835 /* Buffer of -M output. */
836 static char *deps_buffer;
837
838 /* Number of bytes allocated in above. */
839 static int deps_allocated_size;
840
841 /* Number of bytes used. */
842 static int deps_size;
843
844 /* Number of bytes since the last newline. */
845 static int deps_column;
846
847 /* File name which deps are being written to.
848 This is 0 if deps are being written to stdout. */
849 static char *deps_file = 0;
850
851 /* Nonzero means -I- has been seen,
852 so don't look for #include "foo" the source-file directory. */
853 static int ignore_srcdir;
854 \f
855 /* Handler for SIGPIPE. */
856
857 static void
858 pipe_closed (signo)
859 /* If this is missing, some compilers complain. */
860 int signo;
861 {
862 fatal ("output pipe has been closed");
863 }
864
865 int
866 main (argc, argv)
867 int argc;
868 char **argv;
869 {
870 int st_mode;
871 long st_size;
872 char *in_fname, *out_fname;
873 char *p;
874 int f, i;
875 FILE_BUF *fp;
876 char **pend_files = (char **) xmalloc (argc * sizeof (char *));
877 char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
878 char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
879 char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
880 char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
881
882 /* Record the option used with each element of pend_assertions.
883 This is preparation for supporting more than one option for making
884 an assertion. */
885 char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
886 int inhibit_predefs = 0;
887 int no_standard_includes = 0;
888 int no_standard_cplusplus_includes = 0;
889 int missing_newline = 0;
890
891 /* Non-0 means don't output the preprocessed program. */
892 int inhibit_output = 0;
893
894 /* Stream on which to print the dependency information. */
895 FILE *deps_stream = 0;
896 /* Target-name to write with the dependency information. */
897 char *deps_target = 0;
898
899 #ifdef RLIMIT_STACK
900 /* Get rid of any avoidable limit on stack size. */
901 {
902 struct rlimit rlim;
903
904 /* Set the stack limit huge so that alloca (particularly stringtab
905 * in dbxread.c) does not fail. */
906 getrlimit (RLIMIT_STACK, &rlim);
907 rlim.rlim_cur = rlim.rlim_max;
908 setrlimit (RLIMIT_STACK, &rlim);
909 }
910 #endif /* RLIMIT_STACK defined */
911
912 progname = argv[0];
913 #ifdef VMS
914 {
915 /* Remove directories from PROGNAME. */
916 char *s;
917
918 progname = savestring (argv[0]);
919
920 if (!(s = rindex (progname, ']')))
921 s = rindex (progname, ':');
922 if (s)
923 strcpy (progname, s+1);
924 if (s = rindex (progname, '.'))
925 *s = '\0';
926 }
927 #endif
928
929 in_fname = NULL;
930 out_fname = NULL;
931
932 /* Initialize is_idchar to allow $. */
933 dollars_in_ident = 1;
934 initialize_char_syntax ();
935 dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
936
937 no_line_commands = 0;
938 no_trigraphs = 1;
939 dump_macros = dump_none;
940 no_output = 0;
941 cplusplus = 0;
942
943 #ifdef SIGPIPE
944 signal (SIGPIPE, pipe_closed);
945 #endif
946
947 for (i = 0; include_defaults[i].fname; i++)
948 max_include_len = MAX (max_include_len,
949 strlen (include_defaults[i].fname));
950 /* Leave room for making file name longer when converting to VMS syntax. */
951 #ifdef VMS
952 max_include_len += 10;
953 #endif
954
955 bzero (pend_files, argc * sizeof (char *));
956 bzero (pend_defs, argc * sizeof (char *));
957 bzero (pend_undefs, argc * sizeof (char *));
958 bzero (pend_assertions, argc * sizeof (char *));
959 bzero (pend_includes, argc * sizeof (char *));
960
961 /* Process switches and find input file name. */
962
963 for (i = 1; i < argc; i++) {
964 if (argv[i][0] != '-') {
965 if (out_fname != NULL)
966 fatal ("Usage: %s [switches] input output", argv[0]);
967 else if (in_fname != NULL)
968 out_fname = argv[i];
969 else
970 in_fname = argv[i];
971 } else {
972 switch (argv[i][1]) {
973
974 case 'i':
975 if (!strcmp (argv[i], "-include")) {
976 if (i + 1 == argc)
977 fatal ("Filename missing after -include option");
978 else
979 pend_includes[i] = argv[i+1], i++;
980 }
981 if (!strcmp (argv[i], "-imacros")) {
982 if (i + 1 == argc)
983 fatal ("Filename missing after -imacros option");
984 else
985 pend_files[i] = argv[i+1], i++;
986 }
987 /* Add directory to end of path for includes. */
988 if (!strcmp (argv[i], "-idirafter")) {
989 struct file_name_list *dirtmp;
990
991 dirtmp = (struct file_name_list *)
992 xmalloc (sizeof (struct file_name_list));
993 dirtmp->next = 0; /* New one goes on the end */
994 dirtmp->control_macro = 0;
995 if (after_include == 0)
996 after_include = dirtmp;
997 else
998 last_after_include->next = dirtmp;
999 last_after_include = dirtmp; /* Tail follows the last one */
1000
1001 if (i + 1 == argc)
1002 fatal ("Directory name missing after -idirafter option");
1003 else
1004 dirtmp->fname = argv[++i];
1005
1006 if (strlen (dirtmp->fname) > max_include_len)
1007 max_include_len = strlen (dirtmp->fname);
1008 }
1009 break;
1010
1011 case 'o':
1012 if (out_fname != NULL)
1013 fatal ("Output filename specified twice");
1014 if (i + 1 == argc)
1015 fatal ("Filename missing after -o option");
1016 out_fname = argv[++i];
1017 if (!strcmp (out_fname, "-"))
1018 out_fname = "";
1019 break;
1020
1021 case 'p':
1022 if (!strcmp (argv[i], "-pedantic"))
1023 pedantic = 1;
1024 else if (!strcmp (argv[i], "-pedantic-errors")) {
1025 pedantic = 1;
1026 pedantic_errors = 1;
1027 } else if (!strcmp (argv[i], "-pcp")) {
1028 char *pcp_fname = argv[++i];
1029 pcp_outfile =
1030 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1031 ? fopen (pcp_fname, "w")
1032 : fdopen (dup (fileno (stdout)), "w"));
1033 if (pcp_outfile == 0)
1034 pfatal_with_name (pcp_fname);
1035 no_precomp = 1;
1036 }
1037 break;
1038
1039 case 't':
1040 if (!strcmp (argv[i], "-traditional")) {
1041 traditional = 1;
1042 if (dollars_in_ident > 0)
1043 dollars_in_ident = 1;
1044 } else if (!strcmp (argv[i], "-trigraphs")) {
1045 no_trigraphs = 0;
1046 }
1047 break;
1048
1049 case 'l':
1050 if (! strcmp (argv[i], "-lang-c"))
1051 cplusplus = 0, objc = 0;
1052 if (! strcmp (argv[i], "-lang-c++"))
1053 cplusplus = 1, objc = 0;
1054 if (! strcmp (argv[i], "-lang-objc"))
1055 objc = 1, cplusplus = 0;
1056 if (! strcmp (argv[i], "-lang-objc++"))
1057 objc = 1, cplusplus = 1;
1058 if (! strcmp (argv[i], "-lang-asm"))
1059 lang_asm = 1;
1060 if (! strcmp (argv[i], "-lint"))
1061 lint = 1;
1062 break;
1063
1064 case '+':
1065 cplusplus = 1;
1066 break;
1067
1068 case 'w':
1069 inhibit_warnings = 1;
1070 break;
1071
1072 case 'W':
1073 if (!strcmp (argv[i], "-Wtrigraphs"))
1074 warn_trigraphs = 1;
1075 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1076 warn_trigraphs = 0;
1077 else if (!strcmp (argv[i], "-Wcomment"))
1078 warn_comments = 1;
1079 else if (!strcmp (argv[i], "-Wno-comment"))
1080 warn_comments = 0;
1081 else if (!strcmp (argv[i], "-Wcomments"))
1082 warn_comments = 1;
1083 else if (!strcmp (argv[i], "-Wno-comments"))
1084 warn_comments = 0;
1085 else if (!strcmp (argv[i], "-Wtraditional"))
1086 warn_stringify = 1;
1087 else if (!strcmp (argv[i], "-Wno-traditional"))
1088 warn_stringify = 0;
1089 else if (!strcmp (argv[i], "-Wimport"))
1090 warn_import = 1;
1091 else if (!strcmp (argv[i], "-Wno-import"))
1092 warn_import = 0;
1093 else if (!strcmp (argv[i], "-Werror"))
1094 warnings_are_errors = 1;
1095 else if (!strcmp (argv[i], "-Wno-error"))
1096 warnings_are_errors = 0;
1097 else if (!strcmp (argv[i], "-Wall"))
1098 {
1099 warn_trigraphs = 1;
1100 warn_comments = 1;
1101 }
1102 break;
1103
1104 case 'M':
1105 if (!strcmp (argv[i], "-M"))
1106 print_deps = 2;
1107 else if (!strcmp (argv[i], "-MM"))
1108 print_deps = 1;
1109 else if (!strcmp (argv[i], "-MD"))
1110 print_deps = 2;
1111 else if (!strcmp (argv[i], "-MMD"))
1112 print_deps = 1;
1113 /* For -MD and -MMD options, write deps on file named by next arg. */
1114 if (!strcmp (argv[i], "-MD")
1115 || !strcmp (argv[i], "-MMD")) {
1116 i++;
1117 deps_file = argv[i];
1118 deps_stream = fopen (argv[i], "a");
1119 if (deps_stream == 0)
1120 pfatal_with_name (argv[i]);
1121 } else {
1122 /* For -M and -MM, write deps on standard output
1123 and suppress the usual output. */
1124 deps_stream = stdout;
1125 inhibit_output = 1;
1126 }
1127 break;
1128
1129 case 'd':
1130 {
1131 char *p = argv[i] + 2;
1132 char c;
1133 while (c = *p++) {
1134 /* Arg to -d specifies what parts of macros to dump */
1135 switch (c) {
1136 case 'M':
1137 dump_macros = dump_only;
1138 no_output = 1;
1139 break;
1140 case 'N':
1141 dump_macros = dump_names;
1142 break;
1143 case 'D':
1144 dump_macros = dump_definitions;
1145 break;
1146 }
1147 }
1148 }
1149 break;
1150
1151 case 'g':
1152 if (argv[i][2] == '3')
1153 debug_output = 1;
1154 break;
1155
1156 case 'v':
1157 fprintf (stderr, "GNU CPP version %s", version_string);
1158 #ifdef TARGET_VERSION
1159 TARGET_VERSION;
1160 #endif
1161 fprintf (stderr, "\n");
1162 break;
1163
1164 case 'H':
1165 print_include_names = 1;
1166 break;
1167
1168 case 'D':
1169 {
1170 char *p, *p1;
1171
1172 if (argv[i][2] != 0)
1173 p = argv[i] + 2;
1174 else if (i + 1 == argc)
1175 fatal ("Macro name missing after -D option");
1176 else
1177 p = argv[++i];
1178
1179 pend_defs[i] = p;
1180 }
1181 break;
1182
1183 case 'A':
1184 {
1185 char *p, *p1;
1186
1187 if (argv[i][2] != 0)
1188 p = argv[i] + 2;
1189 else if (i + 1 == argc)
1190 fatal ("Assertion missing after -A option");
1191 else
1192 p = argv[++i];
1193
1194 if (!strcmp (p, "-")) {
1195 /* -A- eliminates all predefined macros and assertions.
1196 Let's include also any that were specified earlier
1197 on the command line. That way we can get rid of any
1198 that were passed automatically in from GCC. */
1199 int j;
1200 inhibit_predefs = 1;
1201 for (j = 0; j < i; j++)
1202 pend_defs[j] = pend_assertions[j] = 0;
1203 } else {
1204 pend_assertions[i] = p;
1205 pend_assertion_options[i] = "-A";
1206 }
1207 }
1208 break;
1209
1210 case 'U': /* JF #undef something */
1211 if (argv[i][2] != 0)
1212 pend_undefs[i] = argv[i] + 2;
1213 else if (i + 1 == argc)
1214 fatal ("Macro name missing after -U option");
1215 else
1216 pend_undefs[i] = argv[i+1], i++;
1217 break;
1218
1219 case 'C':
1220 put_out_comments = 1;
1221 break;
1222
1223 case 'E': /* -E comes from cc -E; ignore it. */
1224 break;
1225
1226 case 'P':
1227 no_line_commands = 1;
1228 break;
1229
1230 case '$': /* Don't include $ in identifiers. */
1231 dollars_in_ident = 0;
1232 break;
1233
1234 case 'I': /* Add directory to path for includes. */
1235 {
1236 struct file_name_list *dirtmp;
1237
1238 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
1239 ignore_srcdir = 1;
1240 else {
1241 dirtmp = (struct file_name_list *)
1242 xmalloc (sizeof (struct file_name_list));
1243 dirtmp->next = 0; /* New one goes on the end */
1244 dirtmp->control_macro = 0;
1245 if (include == 0)
1246 include = dirtmp;
1247 else
1248 last_include->next = dirtmp;
1249 last_include = dirtmp; /* Tail follows the last one */
1250 if (argv[i][2] != 0)
1251 dirtmp->fname = argv[i] + 2;
1252 else if (i + 1 == argc)
1253 fatal ("Directory name missing after -I option");
1254 else
1255 dirtmp->fname = argv[++i];
1256 if (strlen (dirtmp->fname) > max_include_len)
1257 max_include_len = strlen (dirtmp->fname);
1258 if (ignore_srcdir && first_bracket_include == 0)
1259 first_bracket_include = dirtmp;
1260 }
1261 }
1262 break;
1263
1264 case 'n':
1265 if (!strcmp (argv[i], "-nostdinc"))
1266 /* -nostdinc causes no default include directories.
1267 You must specify all include-file directories with -I. */
1268 no_standard_includes = 1;
1269 else if (!strcmp (argv[i], "-nostdinc++"))
1270 /* -nostdinc++ causes no default C++-specific include directories. */
1271 no_standard_cplusplus_includes = 1;
1272 else if (!strcmp (argv[i], "-noprecomp"))
1273 no_precomp = 1;
1274 break;
1275
1276 case 'u':
1277 /* Sun compiler passes undocumented switch "-undef".
1278 Let's assume it means to inhibit the predefined symbols. */
1279 inhibit_predefs = 1;
1280 break;
1281
1282 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1283 if (in_fname == NULL) {
1284 in_fname = "";
1285 break;
1286 } else if (out_fname == NULL) {
1287 out_fname = "";
1288 break;
1289 } /* else fall through into error */
1290
1291 default:
1292 fatal ("Invalid option `%s'", argv[i]);
1293 }
1294 }
1295 }
1296
1297 /* Add dirs from CPATH after dirs from -I. */
1298 /* There seems to be confusion about what CPATH should do,
1299 so for the moment it is not documented. */
1300 /* Some people say that CPATH should replace the standard include dirs,
1301 but that seems pointless: it comes before them, so it overrides them
1302 anyway. */
1303 p = (char *) getenv ("CPATH");
1304 if (p != 0 && ! no_standard_includes)
1305 path_include (p);
1306
1307 /* Now that dollars_in_ident is known, initialize is_idchar. */
1308 initialize_char_syntax ();
1309
1310 /* Initialize output buffer */
1311
1312 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1313 outbuf.bufp = outbuf.buf;
1314 outbuf.length = OUTBUF_SIZE;
1315
1316 /* Do partial setup of input buffer for the sake of generating
1317 early #line directives (when -g is in effect). */
1318
1319 fp = &instack[++indepth];
1320 if (in_fname == NULL)
1321 in_fname = "";
1322 fp->nominal_fname = fp->fname = in_fname;
1323 fp->lineno = 0;
1324
1325 /* Install __LINE__, etc. Must follow initialize_char_syntax
1326 and option processing. */
1327 initialize_builtins (fp, &outbuf);
1328
1329 /* Do standard #defines and assertions
1330 that identify system and machine type. */
1331
1332 if (!inhibit_predefs) {
1333 char *p = (char *) alloca (strlen (predefs) + 1);
1334 strcpy (p, predefs);
1335 while (*p) {
1336 char *q;
1337 while (*p == ' ' || *p == '\t')
1338 p++;
1339 /* Handle -D options. */
1340 if (p[0] == '-' && p[1] == 'D') {
1341 q = &p[2];
1342 while (*p && *p != ' ' && *p != '\t')
1343 p++;
1344 if (*p != 0)
1345 *p++= 0;
1346 if (debug_output)
1347 output_line_command (fp, &outbuf, 0, same_file);
1348 make_definition (q, &outbuf);
1349 while (*p == ' ' || *p == '\t')
1350 p++;
1351 } else if (p[0] == '-' && p[1] == 'A') {
1352 /* Handle -A options (assertions). */
1353 char *assertion;
1354 char *past_name;
1355 char *value;
1356 char *past_value;
1357 char *termination;
1358 int save_char;
1359
1360 assertion = &p[2];
1361 past_name = assertion;
1362 /* Locate end of name. */
1363 while (*past_name && *past_name != ' '
1364 && *past_name != '\t' && *past_name != '(')
1365 past_name++;
1366 /* Locate `(' at start of value. */
1367 value = past_name;
1368 while (*value && (*value == ' ' || *value == '\t'))
1369 value++;
1370 if (*value++ != '(')
1371 abort ();
1372 while (*value && (*value == ' ' || *value == '\t'))
1373 value++;
1374 past_value = value;
1375 /* Locate end of value. */
1376 while (*past_value && *past_value != ' '
1377 && *past_value != '\t' && *past_value != ')')
1378 past_value++;
1379 termination = past_value;
1380 while (*termination && (*termination == ' ' || *termination == '\t'))
1381 termination++;
1382 if (*termination++ != ')')
1383 abort ();
1384 if (*termination && *termination != ' ' && *termination != '\t')
1385 abort ();
1386 /* Temporarily null-terminate the value. */
1387 save_char = *termination;
1388 *termination = '\0';
1389 /* Install the assertion. */
1390 make_assertion ("-A", assertion);
1391 *termination = (char) save_char;
1392 p = termination;
1393 while (*p == ' ' || *p == '\t')
1394 p++;
1395 } else {
1396 abort ();
1397 }
1398 }
1399 }
1400
1401 /* Now handle the command line options. */
1402
1403 /* Do assertions specified with -A. */
1404 for (i = 1; i < argc; i++)
1405 if (pend_assertions[i])
1406 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1407
1408 /* Do defines specified with -D. */
1409 for (i = 1; i < argc; i++)
1410 if (pend_defs[i]) {
1411 if (debug_output)
1412 output_line_command (fp, &outbuf, 0, same_file);
1413 make_definition (pend_defs[i], &outbuf);
1414 }
1415
1416 /* Do undefines specified with -U. */
1417 for (i = 1; i < argc; i++)
1418 if (pend_undefs[i]) {
1419 if (debug_output)
1420 output_line_command (fp, &outbuf, 0, same_file);
1421 make_undef (pend_undefs[i], &outbuf);
1422 }
1423
1424 done_initializing = 1;
1425
1426 { /* read the appropriate environment variable and if it exists
1427 replace include_defaults with the listed path. */
1428 char *epath = 0;
1429 switch ((objc << 1) + cplusplus)
1430 {
1431 case 0:
1432 epath = getenv ("C_INCLUDE_PATH");
1433 break;
1434 case 1:
1435 epath = getenv ("CPLUS_INCLUDE_PATH");
1436 break;
1437 case 2:
1438 epath = getenv ("OBJC_INCLUDE_PATH");
1439 break;
1440 case 3:
1441 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
1442 break;
1443 }
1444 /* If the environment var for this language is set,
1445 add to the default list of include directories. */
1446 if (epath) {
1447 char *nstore = (char *) alloca (strlen (epath) + 2);
1448 int num_dirs;
1449 char *startp, *endp;
1450
1451 for (num_dirs = 1, startp = epath; *startp; startp++)
1452 if (*startp == ':')
1453 num_dirs++;
1454 include_defaults
1455 = (struct default_include *) xmalloc ((num_dirs
1456 * sizeof (struct default_include))
1457 + sizeof (include_defaults_array));
1458 startp = endp = epath;
1459 num_dirs = 0;
1460 while (1) {
1461 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1462 if ((*endp == ':'
1463 #ifdef __MSDOS__
1464 && (endp-startp != 1 || !isalpha (*startp)))
1465 #endif
1466 )
1467 || (*endp == 0)) {
1468 strncpy (nstore, startp, endp-startp);
1469 if (endp == startp)
1470 strcpy (nstore, ".");
1471 else
1472 nstore[endp-startp] = '\0';
1473
1474 max_include_len = MAX (max_include_len, endp-startp+2);
1475 include_defaults[num_dirs].fname = savestring (nstore);
1476 include_defaults[num_dirs].cplusplus = cplusplus;
1477 num_dirs++;
1478 if (*endp == '\0')
1479 break;
1480 endp = startp = endp + 1;
1481 } else
1482 endp++;
1483 }
1484 /* Put the usual defaults back in at the end. */
1485 bcopy (include_defaults_array, &include_defaults[num_dirs],
1486 sizeof (include_defaults_array));
1487 }
1488 }
1489
1490 /* Unless -fnostdinc,
1491 tack on the standard include file dirs to the specified list */
1492 if (!no_standard_includes) {
1493 struct default_include *p = include_defaults;
1494 char *specd_prefix = getenv ("GCC_EXEC_PREFIX");
1495 char *default_prefix = savestring (GCC_INCLUDE_DIR);
1496 int default_len = 0;
1497 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1498 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1499 default_len = strlen (default_prefix) - 7;
1500 default_prefix[default_len] = 0;
1501 }
1502 /* Search "translated" versions of GNU directories.
1503 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1504 if (specd_prefix != 0 && default_len != 0)
1505 for (p = include_defaults; p->fname; p++) {
1506 /* Some standard dirs are only for C++. */
1507 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1508 /* Does this dir start with the prefix? */
1509 if (!strncmp (p->fname, default_prefix, default_len)) {
1510 /* Yes; change prefix and add to search list. */
1511 struct file_name_list *new
1512 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1513 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
1514 char *str = (char *) xmalloc (this_len + 1);
1515 strcpy (str, specd_prefix);
1516 strcat (str, p->fname + default_len);
1517 new->fname = str;
1518 new->control_macro = 0;
1519
1520 /* Add elt to tail of list. */
1521 if (include == 0)
1522 include = new;
1523 else
1524 last_include->next = new;
1525 /* Make sure list for #include <...> also has the standard dirs. */
1526 if (ignore_srcdir && first_bracket_include == 0)
1527 first_bracket_include = new;
1528 /* Record new tail. */
1529 last_include = new;
1530 /* Update max_include_len if necessary. */
1531 if (this_len > max_include_len)
1532 max_include_len = this_len;
1533 }
1534 }
1535 }
1536 /* Search ordinary names for GNU include directories. */
1537 for (p = include_defaults; p->fname; p++) {
1538 /* Some standard dirs are only for C++. */
1539 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1540 struct file_name_list *new
1541 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1542 new->control_macro = 0;
1543 /* Add elt to tail of list. */
1544 if (include == 0)
1545 include = new;
1546 else
1547 last_include->next = new;
1548 /* Make sure list for #include <...> also has the standard dirs. */
1549 if (ignore_srcdir && first_bracket_include == 0)
1550 first_bracket_include = new;
1551 /* Record new tail. */
1552 last_include = new;
1553 new->fname = p->fname;
1554 }
1555 }
1556 }
1557
1558 /* Tack the after_include chain at the end of the include chain. */
1559 if (last_include)
1560 last_include->next = after_include;
1561 else
1562 include = after_include;
1563 if (ignore_srcdir && first_bracket_include == 0)
1564 first_bracket_include = after_include;
1565
1566 /* Terminate the after_include chain. */
1567 if (last_after_include)
1568 last_after_include->next = 0;
1569
1570 /* Scan the -imacros files before the main input.
1571 Much like #including them, but with no_output set
1572 so that only their macro definitions matter. */
1573
1574 no_output++;
1575 for (i = 1; i < argc; i++)
1576 if (pend_files[i]) {
1577 int fd = open (pend_files[i], O_RDONLY, 0666);
1578 if (fd < 0) {
1579 perror_with_name (pend_files[i]);
1580 return FAILURE_EXIT_CODE;
1581 }
1582 finclude (fd, pend_files[i], &outbuf, 0, 0);
1583 }
1584 no_output--;
1585
1586 /* Copy the entire contents of the main input file into
1587 the stacked input buffer previously allocated for it. */
1588
1589 /* JF check for stdin */
1590 if (in_fname == NULL || *in_fname == 0) {
1591 in_fname = "";
1592 f = 0;
1593 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
1594 goto perror;
1595
1596 /* Either of two environment variables can specify output of deps.
1597 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1598 where OUTPUT_FILE is the file to write deps info to
1599 and DEPS_TARGET is the target to mention in the deps. */
1600
1601 if (print_deps == 0
1602 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1603 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1604 char *spec = getenv ("DEPENDENCIES_OUTPUT");
1605 char *s;
1606 char *output_file;
1607
1608 if (spec == 0) {
1609 spec = getenv ("SUNPRO_DEPENDENCIES");
1610 print_deps = 2;
1611 }
1612 else
1613 print_deps = 1;
1614
1615 s = spec;
1616 /* Find the space before the DEPS_TARGET, if there is one. */
1617 /* Don't use `index'; that causes trouble on USG. */
1618 while (*s != 0 && *s != ' ') s++;
1619 if (*s != 0) {
1620 deps_target = s + 1;
1621 output_file = (char *) xmalloc (s - spec + 1);
1622 bcopy (spec, output_file, s - spec);
1623 output_file[s - spec] = 0;
1624 }
1625 else {
1626 deps_target = 0;
1627 output_file = spec;
1628 }
1629
1630 deps_file = output_file;
1631 deps_stream = fopen (output_file, "a");
1632 if (deps_stream == 0)
1633 pfatal_with_name (output_file);
1634 }
1635
1636 /* For -M, print the expected object file name
1637 as the target of this Make-rule. */
1638 if (print_deps) {
1639 deps_allocated_size = 200;
1640 deps_buffer = (char *) xmalloc (deps_allocated_size);
1641 deps_buffer[0] = 0;
1642 deps_size = 0;
1643 deps_column = 0;
1644
1645 if (deps_target) {
1646 deps_output (deps_target, 0);
1647 deps_output (":", 0);
1648 } else if (*in_fname == 0)
1649 deps_output ("-: ", 0);
1650 else {
1651 int len;
1652 char *p = in_fname;
1653 char *p1 = p;
1654 /* Discard all directory prefixes from P. */
1655 while (*p1) {
1656 if (*p1 == '/')
1657 p = p1 + 1;
1658 p1++;
1659 }
1660 /* Output P, but remove known suffixes. */
1661 len = strlen (p);
1662 if (p[len - 2] == '.' && p[len - 1] == 'c')
1663 deps_output (p, len - 2);
1664 else if (p[len - 2] == '.' && p[len - 1] == 'C')
1665 deps_output (p, len - 2);
1666 else if (p[len - 3] == '.'
1667 && p[len - 2] == 'c'
1668 && p[len - 1] == 'c')
1669 deps_output (p, len - 3);
1670 else if (p[len - 2] == '.' && p[len - 1] == 's')
1671 deps_output (p, len - 2);
1672 else if (p[len - 2] == '.' && p[len - 1] == 'S')
1673 deps_output (p, len - 2);
1674 else if (p[len - 2] == '.' && p[len - 1] == 'm')
1675 deps_output (p, len - 2);
1676 else
1677 deps_output (p, 0);
1678 /* Supply our own suffix. */
1679 deps_output (".o : ", 0);
1680 deps_output (in_fname, 0);
1681 deps_output (" ", 0);
1682 }
1683 }
1684
1685 file_size_and_mode (f, &st_mode, &st_size);
1686 fp->nominal_fname = fp->fname = in_fname;
1687 fp->lineno = 1;
1688 fp->system_header_p = 0;
1689 /* JF all this is mine about reading pipes and ttys */
1690 if (! S_ISREG (st_mode)) {
1691 /* Read input from a file that is not a normal disk file.
1692 We cannot preallocate a buffer with the correct size,
1693 so we must read in the file a piece at the time and make it bigger. */
1694 int size;
1695 int bsize;
1696 int cnt;
1697 U_CHAR *bufp;
1698
1699 bsize = 2000;
1700 size = 0;
1701 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
1702 bufp = fp->buf;
1703 for (;;) {
1704 cnt = read (f, bufp, bsize - size);
1705 if (cnt < 0) goto perror; /* error! */
1706 if (cnt == 0) break; /* End of file */
1707 size += cnt;
1708 bufp += cnt;
1709 if (bsize == size) { /* Buffer is full! */
1710 bsize *= 2;
1711 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
1712 bufp = fp->buf + size; /* May have moved */
1713 }
1714 }
1715 fp->length = size;
1716 } else {
1717 /* Read a file whose size we can determine in advance.
1718 For the sake of VMS, st_size is just an upper bound. */
1719 long i;
1720 fp->length = 0;
1721 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
1722
1723 while (st_size > 0) {
1724 i = read (f, fp->buf + fp->length, st_size);
1725 if (i <= 0) {
1726 if (i == 0) break;
1727 goto perror;
1728 }
1729 fp->length += i;
1730 st_size -= i;
1731 }
1732 }
1733 fp->bufp = fp->buf;
1734 fp->if_stack = if_stack;
1735
1736 /* Make sure data ends with a newline. And put a null after it. */
1737
1738 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
1739 /* Backslash-newline at end is not good enough. */
1740 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
1741 fp->buf[fp->length++] = '\n';
1742 missing_newline = 1;
1743 }
1744 fp->buf[fp->length] = '\0';
1745
1746 /* Unless inhibited, convert trigraphs in the input. */
1747
1748 if (!no_trigraphs)
1749 trigraph_pcp (fp);
1750
1751 /* Now that we know the input file is valid, open the output. */
1752
1753 if (!out_fname || !strcmp (out_fname, ""))
1754 out_fname = "stdout";
1755 else if (! freopen (out_fname, "w", stdout))
1756 pfatal_with_name (out_fname);
1757
1758 output_line_command (fp, &outbuf, 0, same_file);
1759
1760 /* Scan the -include files before the main input. */
1761
1762 for (i = 1; i < argc; i++)
1763 if (pend_includes[i]) {
1764 int fd = open (pend_includes[i], O_RDONLY, 0666);
1765 if (fd < 0) {
1766 perror_with_name (pend_includes[i]);
1767 return FAILURE_EXIT_CODE;
1768 }
1769 finclude (fd, pend_includes[i], &outbuf, 0, 0);
1770 }
1771
1772 /* Scan the input, processing macros and directives. */
1773
1774 rescan (&outbuf, 0);
1775
1776 if (pedantic && missing_newline)
1777 pedwarn ("file does not end in newline");
1778
1779 /* Now we have processed the entire input
1780 Write whichever kind of output has been requested. */
1781
1782 if (dump_macros == dump_only)
1783 dump_all_macros ();
1784 else if (! inhibit_output) {
1785 write_output ();
1786 }
1787
1788 if (print_deps) {
1789 /* Don't actually write the deps file if compilation has failed.
1790 Delete it instead. */
1791 if (errors > 0 && deps_file != 0)
1792 unlink (deps_file);
1793 else {
1794 fputs (deps_buffer, deps_stream);
1795 putc ('\n', deps_stream);
1796 if (deps_stream != stdout) {
1797 fclose (deps_stream);
1798 if (ferror (deps_stream))
1799 fatal ("I/O error on output");
1800 }
1801 }
1802 }
1803
1804 if (ferror (stdout))
1805 fatal ("I/O error on output");
1806
1807 if (errors)
1808 exit (FAILURE_EXIT_CODE);
1809 exit (SUCCESS_EXIT_CODE);
1810
1811 perror:
1812 pfatal_with_name (in_fname);
1813 return 0;
1814 }
1815 \f
1816 /* Given a colon-separated list of file names PATH,
1817 add all the names to the search path for include files. */
1818
1819 static void
1820 path_include (path)
1821 char *path;
1822 {
1823 char *p;
1824
1825 p = path;
1826
1827 if (*p)
1828 while (1) {
1829 char *q = p;
1830 char *name;
1831 struct file_name_list *dirtmp;
1832
1833 /* Find the end of this name. */
1834 #ifdef __MSDOS__
1835 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1836 while (*q != 0 && (*q != ':' || (q - p == 1 && isalpha (*p)))) q++;
1837 #else
1838 while (*q != 0 && *q != ':') q++;
1839 #endif
1840 if (p == q) {
1841 /* An empty name in the path stands for the current directory. */
1842 name = (char *) xmalloc (2);
1843 name[0] = '.';
1844 name[1] = 0;
1845 } else {
1846 /* Otherwise use the directory that is named. */
1847 name = (char *) xmalloc (q - p + 1);
1848 bcopy (p, name, q - p);
1849 name[q - p] = 0;
1850 }
1851
1852 dirtmp = (struct file_name_list *)
1853 xmalloc (sizeof (struct file_name_list));
1854 dirtmp->next = 0; /* New one goes on the end */
1855 dirtmp->control_macro = 0;
1856 if (include == 0)
1857 include = dirtmp;
1858 else
1859 last_include->next = dirtmp;
1860 last_include = dirtmp; /* Tail follows the last one */
1861 dirtmp->fname = name;
1862 if (strlen (dirtmp->fname) > max_include_len)
1863 max_include_len = strlen (dirtmp->fname);
1864 if (ignore_srcdir && first_bracket_include == 0)
1865 first_bracket_include = dirtmp;
1866
1867 /* Advance past this name. */
1868 p = q;
1869 if (*p == 0)
1870 break;
1871 /* Skip the colon. */
1872 p++;
1873 }
1874 }
1875 \f
1876 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
1877 before main CCCP processing. Name `pcp' is also in honor of the
1878 drugs the trigraph designers must have been on.
1879
1880 Using an extra pass through the buffer takes a little extra time,
1881 but is infinitely less hairy than trying to handle trigraphs inside
1882 strings, etc. everywhere, and also makes sure that trigraphs are
1883 only translated in the top level of processing. */
1884
1885 static void
1886 trigraph_pcp (buf)
1887 FILE_BUF *buf;
1888 {
1889 register U_CHAR c, *fptr, *bptr, *sptr;
1890 int len;
1891
1892 fptr = bptr = sptr = buf->buf;
1893 while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
1894 if (*++sptr != '?')
1895 continue;
1896 switch (*++sptr) {
1897 case '=':
1898 c = '#';
1899 break;
1900 case '(':
1901 c = '[';
1902 break;
1903 case '/':
1904 c = '\\';
1905 break;
1906 case ')':
1907 c = ']';
1908 break;
1909 case '\'':
1910 c = '^';
1911 break;
1912 case '<':
1913 c = '{';
1914 break;
1915 case '!':
1916 c = '|';
1917 break;
1918 case '>':
1919 c = '}';
1920 break;
1921 case '-':
1922 c = '~';
1923 break;
1924 case '?':
1925 sptr--;
1926 continue;
1927 default:
1928 continue;
1929 }
1930 len = sptr - fptr - 2;
1931 if (bptr != fptr && len > 0)
1932 bcopy (fptr, bptr, len); /* BSD doc says bcopy () works right
1933 for overlapping strings. In ANSI
1934 C, this will be memmove (). */
1935 bptr += len;
1936 *bptr++ = c;
1937 fptr = ++sptr;
1938 }
1939 len = buf->length - (fptr - buf->buf);
1940 if (bptr != fptr && len > 0)
1941 bcopy (fptr, bptr, len);
1942 buf->length -= fptr - bptr;
1943 buf->buf[buf->length] = '\0';
1944 if (warn_trigraphs && fptr != bptr)
1945 warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
1946 }
1947 \f
1948 /* Move all backslash-newline pairs out of embarrassing places.
1949 Exchange all such pairs following BP
1950 with any potentially-embarrassing characters that follow them.
1951 Potentially-embarrassing characters are / and *
1952 (because a backslash-newline inside a comment delimiter
1953 would cause it not to be recognized). */
1954
1955 static void
1956 newline_fix (bp)
1957 U_CHAR *bp;
1958 {
1959 register U_CHAR *p = bp;
1960 register int count = 0;
1961
1962 /* First count the backslash-newline pairs here. */
1963
1964 while (1) {
1965 if (p[0] == '\\') {
1966 if (p[1] == '\n')
1967 p += 2, count++;
1968 else if (p[1] == '\r' && p[2] == '\n')
1969 p += 3, count++;
1970 else
1971 break;
1972 } else
1973 break;
1974 }
1975
1976 /* What follows the backslash-newlines is not embarrassing. */
1977
1978 if (count == 0 || (*p != '/' && *p != '*'))
1979 return;
1980
1981 /* Copy all potentially embarrassing characters
1982 that follow the backslash-newline pairs
1983 down to where the pairs originally started. */
1984
1985 while (*p == '*' || *p == '/')
1986 *bp++ = *p++;
1987
1988 /* Now write the same number of pairs after the embarrassing chars. */
1989 while (count-- > 0) {
1990 *bp++ = '\\';
1991 *bp++ = '\n';
1992 }
1993 }
1994
1995 /* Like newline_fix but for use within a directive-name.
1996 Move any backslash-newlines up past any following symbol constituents. */
1997
1998 static void
1999 name_newline_fix (bp)
2000 U_CHAR *bp;
2001 {
2002 register U_CHAR *p = bp;
2003 register int count = 0;
2004
2005 /* First count the backslash-newline pairs here. */
2006 while (1) {
2007 if (p[0] == '\\') {
2008 if (p[1] == '\n')
2009 p += 2, count++;
2010 else if (p[1] == '\r' && p[2] == '\n')
2011 p += 3, count++;
2012 else
2013 break;
2014 } else
2015 break;
2016 }
2017
2018 /* What follows the backslash-newlines is not embarrassing. */
2019
2020 if (count == 0 || !is_idchar[*p])
2021 return;
2022
2023 /* Copy all potentially embarrassing characters
2024 that follow the backslash-newline pairs
2025 down to where the pairs originally started. */
2026
2027 while (is_idchar[*p])
2028 *bp++ = *p++;
2029
2030 /* Now write the same number of pairs after the embarrassing chars. */
2031 while (count-- > 0) {
2032 *bp++ = '\\';
2033 *bp++ = '\n';
2034 }
2035 }
2036 \f
2037 /* Look for lint commands in comments.
2038
2039 When we come in here, ibp points into a comment. Limit is as one expects.
2040 scan within the comment -- it should start, after lwsp, with a lint command.
2041 If so that command is returned as a (constant) string.
2042
2043 Upon return, any arg will be pointed to with argstart and will be
2044 arglen long. Note that we don't parse that arg since it will just
2045 be printed out again.
2046 */
2047
2048 static char *
2049 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2050 register U_CHAR *ibp;
2051 register U_CHAR *limit;
2052 U_CHAR **argstart; /* point to command arg */
2053 int *arglen, *cmdlen; /* how long they are */
2054 {
2055 long linsize;
2056 register U_CHAR *numptr; /* temp for arg parsing */
2057
2058 *arglen = 0;
2059
2060 SKIP_WHITE_SPACE (ibp);
2061
2062 if (ibp >= limit) return NULL;
2063
2064 linsize = limit - ibp;
2065
2066 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2067 if ((linsize >= 10) && !strncmp (ibp, "NOTREACHED", 10)) {
2068 *cmdlen = 10;
2069 return "NOTREACHED";
2070 }
2071 if ((linsize >= 8) && !strncmp (ibp, "ARGSUSED", 8)) {
2072 *cmdlen = 8;
2073 return "ARGSUSED";
2074 }
2075 if ((linsize >= 11) && !strncmp (ibp, "LINTLIBRARY", 8)) {
2076 *cmdlen = 8;
2077 return "LINTLIBRARY";
2078 }
2079 if ((linsize >= 7) && !strncmp (ibp, "VARARGS", 7)) {
2080 *cmdlen = 7;
2081 ibp += 7; linsize -= 7;
2082 if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
2083
2084 /* OK, read a number */
2085 for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
2086 numptr++);
2087 *arglen = numptr - *argstart;
2088 return "VARARGS";
2089 }
2090 return NULL;
2091 }
2092 \f
2093 /*
2094 * The main loop of the program.
2095 *
2096 * Read characters from the input stack, transferring them to the
2097 * output buffer OP.
2098 *
2099 * Macros are expanded and push levels on the input stack.
2100 * At the end of such a level it is popped off and we keep reading.
2101 * At the end of any other kind of level, we return.
2102 * #-directives are handled, except within macros.
2103 *
2104 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2105 * and insert them when appropriate. This is set while scanning macro
2106 * arguments before substitution. It is zero when scanning for final output.
2107 * There are three types of Newline markers:
2108 * * Newline - follows a macro name that was not expanded
2109 * because it appeared inside an expansion of the same macro.
2110 * This marker prevents future expansion of that identifier.
2111 * When the input is rescanned into the final output, these are deleted.
2112 * These are also deleted by ## concatenation.
2113 * * Newline Space (or Newline and any other whitespace character)
2114 * stands for a place that tokens must be separated or whitespace
2115 * is otherwise desirable, but where the ANSI standard specifies there
2116 * is no whitespace. This marker turns into a Space (or whichever other
2117 * whitespace char appears in the marker) in the final output,
2118 * but it turns into nothing in an argument that is stringified with #.
2119 * Such stringified arguments are the only place where the ANSI standard
2120 * specifies with precision that whitespace may not appear.
2121 *
2122 * During this function, IP->bufp is kept cached in IBP for speed of access.
2123 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2124 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2125 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2126 * explicitly, and before RECACHE, since RECACHE uses OBP.
2127 */
2128
2129 static void
2130 rescan (op, output_marks)
2131 FILE_BUF *op;
2132 int output_marks;
2133 {
2134 /* Character being scanned in main loop. */
2135 register U_CHAR c;
2136
2137 /* Length of pending accumulated identifier. */
2138 register int ident_length = 0;
2139
2140 /* Hash code of pending accumulated identifier. */
2141 register int hash = 0;
2142
2143 /* Current input level (&instack[indepth]). */
2144 FILE_BUF *ip;
2145
2146 /* Pointer for scanning input. */
2147 register U_CHAR *ibp;
2148
2149 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2150 register U_CHAR *limit;
2151
2152 /* Pointer for storing output. */
2153 register U_CHAR *obp;
2154
2155 /* REDO_CHAR is nonzero if we are processing an identifier
2156 after backing up over the terminating character.
2157 Sometimes we process an identifier without backing up over
2158 the terminating character, if the terminating character
2159 is not special. Backing up is done so that the terminating character
2160 will be dispatched on again once the identifier is dealt with. */
2161 int redo_char = 0;
2162
2163 /* 1 if within an identifier inside of which a concatenation
2164 marker (Newline -) has been seen. */
2165 int concatenated = 0;
2166
2167 /* While scanning a comment or a string constant,
2168 this records the line it started on, for error messages. */
2169 int start_line;
2170
2171 /* Line where a newline was first seen in a string constant. */
2172 int multiline_string_line = 0;
2173
2174 /* Record position of last `real' newline. */
2175 U_CHAR *beg_of_line;
2176
2177 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2178
2179 #define POPMACRO \
2180 do { ip->macro->type = T_MACRO; \
2181 if (ip->free_ptr) free (ip->free_ptr); \
2182 --indepth; } while (0)
2183
2184 /* Reload `rescan's local variables that describe the current
2185 level of the input stack. */
2186
2187 #define RECACHE \
2188 do { ip = &instack[indepth]; \
2189 ibp = ip->bufp; \
2190 limit = ip->buf + ip->length; \
2191 op->bufp = obp; \
2192 check_expand (op, limit - ibp); \
2193 beg_of_line = 0; \
2194 obp = op->bufp; } while (0)
2195
2196 if (no_output && instack[indepth].fname != 0)
2197 skip_if_group (&instack[indepth], 1);
2198
2199 obp = op->bufp;
2200 RECACHE;
2201 beg_of_line = ibp;
2202
2203 /* Our caller must always put a null after the end of
2204 the input at each input stack level. */
2205 if (*limit != 0)
2206 abort ();
2207
2208 while (1) {
2209 c = *ibp++;
2210 *obp++ = c;
2211
2212 switch (c) {
2213 case '\\':
2214 if (ibp >= limit)
2215 break;
2216 if (*ibp == '\n') {
2217 /* Always merge lines ending with backslash-newline,
2218 even in middle of identifier. */
2219 ++ibp;
2220 ++ip->lineno;
2221 --obp; /* remove backslash from obuf */
2222 break;
2223 }
2224 /* Otherwise, backslash suppresses specialness of following char,
2225 so copy it here to prevent the switch from seeing it.
2226 But first get any pending identifier processed. */
2227 if (ident_length > 0)
2228 goto specialchar;
2229 *obp++ = *ibp++;
2230 break;
2231
2232 case '#':
2233 if (assertions_flag) {
2234 /* Copy #foo (bar lose) without macro expansion. */
2235 SKIP_WHITE_SPACE (ibp);
2236 while (is_idchar[*ibp])
2237 *obp++ = *ibp++;
2238 SKIP_WHITE_SPACE (ibp);
2239 if (*ibp == '(') {
2240 ip->bufp = ibp;
2241 skip_paren_group (ip);
2242 bcopy (ibp, obp, ip->bufp - ibp);
2243 obp += ip->bufp - ibp;
2244 ibp = ip->bufp;
2245 }
2246 }
2247
2248 /* If this is expanding a macro definition, don't recognize
2249 preprocessor directives. */
2250 if (ip->macro != 0)
2251 goto randomchar;
2252 if (ident_length)
2253 goto specialchar;
2254
2255 /* # keyword: a # must be first nonblank char on the line */
2256 if (beg_of_line == 0)
2257 goto randomchar;
2258 {
2259 U_CHAR *bp;
2260
2261 /* Scan from start of line, skipping whitespace, comments
2262 and backslash-newlines, and see if we reach this #.
2263 If not, this # is not special. */
2264 bp = beg_of_line;
2265 while (1) {
2266 if (is_hor_space[*bp])
2267 bp++;
2268 else if (*bp == '\\' && bp[1] == '\n')
2269 bp += 2;
2270 else if (*bp == '/' && bp[1] == '*') {
2271 bp += 2;
2272 while (!(*bp == '*' && bp[1] == '/'))
2273 bp++;
2274 bp += 2;
2275 }
2276 else if ((cplusplus || objc) && *bp == '/' && bp[1] == '/') {
2277 bp += 2;
2278 while (*bp++ != '\n') ;
2279 }
2280 else break;
2281 }
2282 if (bp + 1 != ibp)
2283 goto randomchar;
2284 }
2285
2286 /* This # can start a directive. */
2287
2288 --obp; /* Don't copy the '#' */
2289
2290 ip->bufp = ibp;
2291 op->bufp = obp;
2292 if (! handle_directive (ip, op)) {
2293 #ifdef USE_C_ALLOCA
2294 alloca (0);
2295 #endif
2296 /* Not a known directive: treat it as ordinary text.
2297 IP, OP, IBP, etc. have not been changed. */
2298 if (no_output && instack[indepth].fname) {
2299 /* If not generating expanded output,
2300 what we do with ordinary text is skip it.
2301 Discard everything until next # directive. */
2302 skip_if_group (&instack[indepth], 1);
2303 RECACHE;
2304 beg_of_line = ibp;
2305 break;
2306 }
2307 ++obp; /* Copy the '#' after all */
2308 goto randomchar;
2309 }
2310 #ifdef USE_C_ALLOCA
2311 alloca (0);
2312 #endif
2313 /* A # directive has been successfully processed. */
2314 /* If not generating expanded output, ignore everything until
2315 next # directive. */
2316 if (no_output && instack[indepth].fname)
2317 skip_if_group (&instack[indepth], 1);
2318 obp = op->bufp;
2319 RECACHE;
2320 beg_of_line = ibp;
2321 break;
2322
2323 case '\"': /* skip quoted string */
2324 case '\'':
2325 /* A single quoted string is treated like a double -- some
2326 programs (e.g., troff) are perverse this way */
2327
2328 if (ident_length)
2329 goto specialchar;
2330
2331 start_line = ip->lineno;
2332
2333 /* Skip ahead to a matching quote. */
2334
2335 while (1) {
2336 if (ibp >= limit) {
2337 if (traditional) {
2338 if (ip->macro != 0) {
2339 /* try harder: this string crosses a macro expansion boundary */
2340 POPMACRO;
2341 RECACHE;
2342 continue;
2343 }
2344 } else {
2345 error_with_line (line_for_error (start_line),
2346 "unterminated string or character constant");
2347 error_with_line (multiline_string_line,
2348 "possible real start of unterminated constant");
2349 multiline_string_line = 0;
2350 }
2351 break;
2352 }
2353 *obp++ = *ibp;
2354 switch (*ibp++) {
2355 case '\n':
2356 ++ip->lineno;
2357 ++op->lineno;
2358 /* Traditionally, end of line ends a string constant with no error.
2359 So exit the loop and record the new line. */
2360 if (traditional) {
2361 beg_of_line = ibp;
2362 goto while2end;
2363 }
2364 if (pedantic || c == '\'') {
2365 error_with_line (line_for_error (start_line),
2366 "unterminated string or character constant");
2367 goto while2end;
2368 }
2369 if (multiline_string_line == 0)
2370 multiline_string_line = ip->lineno - 1;
2371 break;
2372
2373 case '\\':
2374 if (ibp >= limit)
2375 break;
2376 if (*ibp == '\n') {
2377 /* Backslash newline is replaced by nothing at all,
2378 but keep the line counts correct. */
2379 --obp;
2380 ++ibp;
2381 ++ip->lineno;
2382 } else {
2383 /* ANSI stupidly requires that in \\ the second \
2384 is *not* prevented from combining with a newline. */
2385 while (*ibp == '\\' && ibp[1] == '\n') {
2386 ibp += 2;
2387 ++ip->lineno;
2388 }
2389 *obp++ = *ibp++;
2390 }
2391 break;
2392
2393 case '\"':
2394 case '\'':
2395 if (ibp[-1] == c)
2396 goto while2end;
2397 break;
2398 }
2399 }
2400 while2end:
2401 break;
2402
2403 case '/':
2404 if (*ibp == '\\' && ibp[1] == '\n')
2405 newline_fix (ibp);
2406
2407 if (*ibp != '*'
2408 && !((cplusplus || objc) && *ibp == '/'))
2409 goto randomchar;
2410 if (ip->macro != 0)
2411 goto randomchar;
2412 if (ident_length)
2413 goto specialchar;
2414
2415 if (*ibp == '/') {
2416 /* C++ style comment... */
2417 start_line = ip->lineno;
2418
2419 --ibp; /* Back over the slash */
2420 --obp;
2421
2422 /* Comments are equivalent to spaces. */
2423 if (! put_out_comments)
2424 *obp++ = ' ';
2425 else {
2426 /* must fake up a comment here */
2427 *obp++ = '/';
2428 *obp++ = '/';
2429 }
2430 {
2431 U_CHAR *before_bp = ibp+2;
2432
2433 while (ibp < limit) {
2434 if (*ibp++ == '\n') {
2435 ibp--;
2436 if (put_out_comments) {
2437 bcopy (before_bp, obp, ibp - before_bp);
2438 obp += ibp - before_bp;
2439 }
2440 break;
2441 }
2442 }
2443 break;
2444 }
2445 }
2446
2447 /* Ordinary C comment. Skip it, optionally copying it to output. */
2448
2449 start_line = ip->lineno;
2450
2451 ++ibp; /* Skip the star. */
2452
2453 /* If this cpp is for lint, we peek inside the comments: */
2454 if (lint) {
2455 U_CHAR *argbp;
2456 int cmdlen, arglen;
2457 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2458
2459 if (lintcmd != NULL) {
2460 /* I believe it is always safe to emit this newline: */
2461 obp[-1] = '\n';
2462 bcopy ("#pragma lint ", obp, 13);
2463 obp += 13;
2464 bcopy (lintcmd, obp, cmdlen);
2465 obp += cmdlen;
2466
2467 if (arglen != 0) {
2468 *(obp++) = ' ';
2469 bcopy (argbp, obp, arglen);
2470 obp += arglen;
2471 }
2472
2473 /* OK, now bring us back to the state we were in before we entered
2474 this branch. We need #line b/c the newline for the pragma
2475 could fuck things up. */
2476 output_line_command (ip, op, 0, same_file);
2477 *(obp++) = ' '; /* just in case, if comments are copied thru */
2478 *(obp++) = '/';
2479 }
2480 }
2481
2482 /* Comments are equivalent to spaces.
2483 Note that we already output the slash; we might not want it.
2484 For -traditional, a comment is equivalent to nothing. */
2485 if (! put_out_comments) {
2486 if (traditional)
2487 obp--;
2488 else
2489 obp[-1] = ' ';
2490 }
2491 else
2492 *obp++ = '*';
2493
2494 {
2495 U_CHAR *before_bp = ibp;
2496
2497 while (ibp < limit) {
2498 switch (*ibp++) {
2499 case '/':
2500 if (warn_comments && ibp < limit && *ibp == '*')
2501 warning("`/*' within comment");
2502 break;
2503 case '*':
2504 if (*ibp == '\\' && ibp[1] == '\n')
2505 newline_fix (ibp);
2506 if (ibp >= limit || *ibp == '/')
2507 goto comment_end;
2508 break;
2509 case '\n':
2510 ++ip->lineno;
2511 /* Copy the newline into the output buffer, in order to
2512 avoid the pain of a #line every time a multiline comment
2513 is seen. */
2514 if (!put_out_comments)
2515 *obp++ = '\n';
2516 ++op->lineno;
2517 }
2518 }
2519 comment_end:
2520
2521 if (ibp >= limit)
2522 error_with_line (line_for_error (start_line),
2523 "unterminated comment");
2524 else {
2525 ibp++;
2526 if (put_out_comments) {
2527 bcopy (before_bp, obp, ibp - before_bp);
2528 obp += ibp - before_bp;
2529 }
2530 }
2531 }
2532 break;
2533
2534 case '$':
2535 if (!dollars_in_ident)
2536 goto randomchar;
2537 goto letter;
2538
2539 case '0': case '1': case '2': case '3': case '4':
2540 case '5': case '6': case '7': case '8': case '9':
2541 /* If digit is not part of identifier, it starts a number,
2542 which means that following letters are not an identifier.
2543 "0x5" does not refer to an identifier "x5".
2544 So copy all alphanumerics that follow without accumulating
2545 as an identifier. Periods also, for sake of "3.e7". */
2546
2547 if (ident_length == 0) {
2548 while (ibp < limit) {
2549 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
2550 ++ip->lineno;
2551 ibp += 2;
2552 }
2553 c = *ibp++;
2554 /* ".." terminates a preprocessing number. This is useless for C
2555 code but useful for preprocessing other things. */
2556 if (!isalnum (c) && (c != '.' || *ibp == '.') && c != '_') {
2557 --ibp;
2558 break;
2559 }
2560 *obp++ = c;
2561 /* A sign can be part of a preprocessing number
2562 if it follows an e. */
2563 if (c == 'e' || c == 'E') {
2564 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
2565 ++ip->lineno;
2566 ibp += 2;
2567 }
2568 if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
2569 *obp++ = *ibp++;
2570 /* But traditional C does not let the token go past the sign. */
2571 if (traditional)
2572 break;
2573 }
2574 }
2575 }
2576 break;
2577 }
2578 /* fall through */
2579
2580 case '_':
2581 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2582 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2583 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2584 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2585 case 'y': case 'z':
2586 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2587 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
2588 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2589 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2590 case 'Y': case 'Z':
2591 letter:
2592 ident_length++;
2593 /* Compute step of hash function, to avoid a proc call on every token */
2594 hash = HASHSTEP (hash, c);
2595 break;
2596
2597 case '\n':
2598 /* If reprocessing a macro expansion, newline is a special marker. */
2599 if (ip->macro != 0) {
2600 /* Newline White is a "funny space" to separate tokens that are
2601 supposed to be separate but without space between.
2602 Here White means any whitespace character.
2603 Newline - marks a recursive macro use that is not
2604 supposed to be expandable. */
2605
2606 if (*ibp == '-') {
2607 /* Newline - inhibits expansion of preceding token.
2608 If expanding a macro arg, we keep the newline -.
2609 In final output, it is deleted. */
2610 if (! concatenated) {
2611 ident_length = 0;
2612 hash = 0;
2613 }
2614 ibp++;
2615 if (!output_marks) {
2616 obp--;
2617 } else {
2618 /* If expanding a macro arg, keep the newline -. */
2619 *obp++ = '-';
2620 }
2621 } else if (is_space[*ibp]) {
2622 /* Newline Space does not prevent expansion of preceding token
2623 so expand the preceding token and then come back. */
2624 if (ident_length > 0)
2625 goto specialchar;
2626
2627 /* If generating final output, newline space makes a space. */
2628 if (!output_marks) {
2629 obp[-1] = *ibp++;
2630 /* And Newline Newline makes a newline, so count it. */
2631 if (obp[-1] == '\n')
2632 op->lineno++;
2633 } else {
2634 /* If expanding a macro arg, keep the newline space.
2635 If the arg gets stringified, newline space makes nothing. */
2636 *obp++ = *ibp++;
2637 }
2638 } else abort (); /* Newline followed by something random? */
2639 break;
2640 }
2641
2642 /* If there is a pending identifier, handle it and come back here. */
2643 if (ident_length > 0)
2644 goto specialchar;
2645
2646 beg_of_line = ibp;
2647
2648 /* Update the line counts and output a #line if necessary. */
2649 ++ip->lineno;
2650 ++op->lineno;
2651 if (ip->lineno != op->lineno) {
2652 op->bufp = obp;
2653 output_line_command (ip, op, 1, same_file);
2654 check_expand (op, ip->length - (ip->bufp - ip->buf));
2655 obp = op->bufp;
2656 }
2657 break;
2658
2659 /* Come here either after (1) a null character that is part of the input
2660 or (2) at the end of the input, because there is a null there. */
2661 case 0:
2662 if (ibp <= limit)
2663 /* Our input really contains a null character. */
2664 goto randomchar;
2665
2666 /* At end of a macro-expansion level, pop it and read next level. */
2667 if (ip->macro != 0) {
2668 obp--;
2669 ibp--;
2670 /* If traditional, and we have an identifier that ends here,
2671 process it now, so we get the right error for recursion. */
2672 if (traditional && ident_length
2673 && ! is_idchar[*instack[indepth - 1].bufp]) {
2674 redo_char = 1;
2675 goto randomchar;
2676 }
2677 POPMACRO;
2678 RECACHE;
2679 break;
2680 }
2681
2682 /* If we don't have a pending identifier,
2683 return at end of input. */
2684 if (ident_length == 0) {
2685 obp--;
2686 ibp--;
2687 op->bufp = obp;
2688 ip->bufp = ibp;
2689 goto ending;
2690 }
2691
2692 /* If we do have a pending identifier, just consider this null
2693 a special character and arrange to dispatch on it again.
2694 The second time, IDENT_LENGTH will be zero so we will return. */
2695
2696 /* Fall through */
2697
2698 specialchar:
2699
2700 /* Handle the case of a character such as /, ', " or null
2701 seen following an identifier. Back over it so that
2702 after the identifier is processed the special char
2703 will be dispatched on again. */
2704
2705 ibp--;
2706 obp--;
2707 redo_char = 1;
2708
2709 default:
2710
2711 randomchar:
2712
2713 if (ident_length > 0) {
2714 register HASHNODE *hp;
2715
2716 /* We have just seen an identifier end. If it's a macro, expand it.
2717
2718 IDENT_LENGTH is the length of the identifier
2719 and HASH is its hash code.
2720
2721 The identifier has already been copied to the output,
2722 so if it is a macro we must remove it.
2723
2724 If REDO_CHAR is 0, the char that terminated the identifier
2725 has been skipped in the output and the input.
2726 OBP-IDENT_LENGTH-1 points to the identifier.
2727 If the identifier is a macro, we must back over the terminator.
2728
2729 If REDO_CHAR is 1, the terminating char has already been
2730 backed over. OBP-IDENT_LENGTH points to the identifier. */
2731
2732 if (!pcp_outfile || pcp_inside_if) {
2733 startagain:
2734 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
2735 hp = hp->next) {
2736
2737 if (hp->length == ident_length) {
2738 int obufp_before_macroname;
2739 int op_lineno_before_macroname;
2740 register int i = ident_length;
2741 register U_CHAR *p = hp->name;
2742 register U_CHAR *q = obp - i;
2743 int disabled;
2744
2745 if (! redo_char)
2746 q--;
2747
2748 do { /* All this to avoid a strncmp () */
2749 if (*p++ != *q++)
2750 goto hashcollision;
2751 } while (--i);
2752
2753 /* We found a use of a macro name.
2754 see if the context shows it is a macro call. */
2755
2756 /* Back up over terminating character if not already done. */
2757 if (! redo_char) {
2758 ibp--;
2759 obp--;
2760 }
2761
2762 /* Save this as a displacement from the beginning of the output
2763 buffer. We can not save this as a position in the output
2764 buffer, because it may get realloc'ed by RECACHE. */
2765 obufp_before_macroname = (obp - op->buf) - ident_length;
2766 op_lineno_before_macroname = op->lineno;
2767
2768 if (hp->type == T_PCSTRING) {
2769 pcstring_used (hp); /* Mark the definition of this key
2770 as needed, ensuring that it
2771 will be output. */
2772 break; /* Exit loop, since the key cannot have a
2773 definition any longer. */
2774 }
2775
2776 /* Record whether the macro is disabled. */
2777 disabled = hp->type == T_DISABLED;
2778
2779 /* This looks like a macro ref, but if the macro was disabled,
2780 just copy its name and put in a marker if requested. */
2781
2782 if (disabled) {
2783 #if 0
2784 /* This error check caught useful cases such as
2785 #define foo(x,y) bar(x(y,0), y)
2786 foo(foo, baz) */
2787 if (traditional)
2788 error ("recursive use of macro `%s'", hp->name);
2789 #endif
2790
2791 if (output_marks) {
2792 check_expand (op, limit - ibp + 2);
2793 *obp++ = '\n';
2794 *obp++ = '-';
2795 }
2796 break;
2797 }
2798
2799 /* If macro wants an arglist, verify that a '(' follows.
2800 first skip all whitespace, copying it to the output
2801 after the macro name. Then, if there is no '(',
2802 decide this is not a macro call and leave things that way. */
2803 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
2804 && hp->value.defn->nargs >= 0)
2805 {
2806 U_CHAR *old_ibp = ibp;
2807 U_CHAR *old_obp = obp;
2808 int old_iln = ip->lineno;
2809 int old_oln = op->lineno;
2810
2811 while (1) {
2812 /* Scan forward over whitespace, copying it to the output. */
2813 if (ibp == limit && ip->macro != 0) {
2814 POPMACRO;
2815 RECACHE;
2816 old_ibp = ibp;
2817 old_obp = obp;
2818 old_iln = ip->lineno;
2819 old_oln = op->lineno;
2820 }
2821 /* A comment: copy it unchanged or discard it. */
2822 else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
2823 if (put_out_comments) {
2824 *obp++ = '/';
2825 *obp++ = '*';
2826 } else if (! traditional) {
2827 *obp++ = ' ';
2828 }
2829 ibp += 2;
2830 while (ibp + 1 != limit
2831 && !(ibp[0] == '*' && ibp[1] == '/')) {
2832 /* We need not worry about newline-marks,
2833 since they are never found in comments. */
2834 if (*ibp == '\n') {
2835 /* Newline in a file. Count it. */
2836 ++ip->lineno;
2837 ++op->lineno;
2838 }
2839 if (put_out_comments)
2840 *obp++ = *ibp++;
2841 else
2842 ibp++;
2843 }
2844 ibp += 2;
2845 if (put_out_comments) {
2846 *obp++ = '*';
2847 *obp++ = '/';
2848 }
2849 }
2850 else if (is_space[*ibp]) {
2851 *obp++ = *ibp++;
2852 if (ibp[-1] == '\n') {
2853 if (ip->macro == 0) {
2854 /* Newline in a file. Count it. */
2855 ++ip->lineno;
2856 ++op->lineno;
2857 } else if (!output_marks) {
2858 /* A newline mark, and we don't want marks
2859 in the output. If it is newline-hyphen,
2860 discard it entirely. Otherwise, it is
2861 newline-whitechar, so keep the whitechar. */
2862 obp--;
2863 if (*ibp == '-')
2864 ibp++;
2865 else {
2866 if (*ibp == '\n')
2867 ++op->lineno;
2868 *obp++ = *ibp++;
2869 }
2870 } else {
2871 /* A newline mark; copy both chars to the output. */
2872 *obp++ = *ibp++;
2873 }
2874 }
2875 }
2876 else break;
2877 }
2878 if (*ibp != '(') {
2879 /* It isn't a macro call.
2880 Put back the space that we just skipped. */
2881 ibp = old_ibp;
2882 obp = old_obp;
2883 ip->lineno = old_iln;
2884 op->lineno = old_oln;
2885 /* Exit the for loop. */
2886 break;
2887 }
2888 }
2889
2890 /* This is now known to be a macro call.
2891 Discard the macro name from the output,
2892 along with any following whitespace just copied. */
2893 obp = op->buf + obufp_before_macroname;
2894 op->lineno = op_lineno_before_macroname;
2895
2896 /* Expand the macro, reading arguments as needed,
2897 and push the expansion on the input stack. */
2898 ip->bufp = ibp;
2899 op->bufp = obp;
2900 macroexpand (hp, op);
2901
2902 /* Reexamine input stack, since macroexpand has pushed
2903 a new level on it. */
2904 obp = op->bufp;
2905 RECACHE;
2906 break;
2907 }
2908 hashcollision:
2909 ;
2910 } /* End hash-table-search loop */
2911 }
2912 ident_length = hash = 0; /* Stop collecting identifier */
2913 redo_char = 0;
2914 concatenated = 0;
2915 } /* End if (ident_length > 0) */
2916 } /* End switch */
2917 } /* End per-char loop */
2918
2919 /* Come here to return -- but first give an error message
2920 if there was an unterminated successful conditional. */
2921 ending:
2922 if (if_stack != ip->if_stack) {
2923 char *str;
2924 switch (if_stack->type) {
2925 case T_IF:
2926 str = "if";
2927 break;
2928 case T_IFDEF:
2929 str = "ifdef";
2930 break;
2931 case T_IFNDEF:
2932 str = "ifndef";
2933 break;
2934 case T_ELSE:
2935 str = "else";
2936 break;
2937 case T_ELIF:
2938 str = "elif";
2939 break;
2940 }
2941 error_with_line (line_for_error (if_stack->lineno),
2942 "unterminated `#%s' conditional", str);
2943 }
2944 if_stack = ip->if_stack;
2945 }
2946 \f
2947 /*
2948 * Rescan a string into a temporary buffer and return the result
2949 * as a FILE_BUF. Note this function returns a struct, not a pointer.
2950 *
2951 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
2952 * and insert such markers when appropriate. See `rescan' for details.
2953 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
2954 * before substitution; it is 0 for other uses.
2955 */
2956 static FILE_BUF
2957 expand_to_temp_buffer (buf, limit, output_marks, assertions)
2958 U_CHAR *buf, *limit;
2959 int output_marks, assertions;
2960 {
2961 register FILE_BUF *ip;
2962 FILE_BUF obuf;
2963 int length = limit - buf;
2964 U_CHAR *buf1;
2965 int odepth = indepth;
2966 int save_assertions_flag = assertions_flag;
2967
2968 assertions_flag = assertions;
2969
2970 if (length < 0)
2971 abort ();
2972
2973 /* Set up the input on the input stack. */
2974
2975 buf1 = (U_CHAR *) alloca (length + 1);
2976 {
2977 register U_CHAR *p1 = buf;
2978 register U_CHAR *p2 = buf1;
2979
2980 while (p1 != limit)
2981 *p2++ = *p1++;
2982 }
2983 buf1[length] = 0;
2984
2985 /* Set up to receive the output. */
2986
2987 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
2988 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
2989 obuf.fname = 0;
2990 obuf.macro = 0;
2991 obuf.free_ptr = 0;
2992
2993 CHECK_DEPTH ({return obuf;});
2994
2995 ++indepth;
2996
2997 ip = &instack[indepth];
2998 ip->fname = 0;
2999 ip->nominal_fname = 0;
3000 ip->system_header_p = 0;
3001 ip->macro = 0;
3002 ip->free_ptr = 0;
3003 ip->length = length;
3004 ip->buf = ip->bufp = buf1;
3005 ip->if_stack = if_stack;
3006
3007 ip->lineno = obuf.lineno = 1;
3008
3009 /* Scan the input, create the output. */
3010 rescan (&obuf, output_marks);
3011
3012 /* Pop input stack to original state. */
3013 --indepth;
3014
3015 if (indepth != odepth)
3016 abort ();
3017
3018 /* Record the output. */
3019 obuf.length = obuf.bufp - obuf.buf;
3020
3021 assertions_flag = save_assertions_flag;
3022 return obuf;
3023 }
3024 \f
3025 /*
3026 * Process a # directive. Expects IP->bufp to point after the '#', as in
3027 * `#define foo bar'. Passes to the command handler
3028 * (do_define, do_include, etc.): the addresses of the 1st and
3029 * last chars of the command (starting immediately after the #
3030 * keyword), plus op and the keyword table pointer. If the command
3031 * contains comments it is copied into a temporary buffer sans comments
3032 * and the temporary buffer is passed to the command handler instead.
3033 * Likewise for backslash-newlines.
3034 *
3035 * Returns nonzero if this was a known # directive.
3036 * Otherwise, returns zero, without advancing the input pointer.
3037 */
3038
3039 static int
3040 handle_directive (ip, op)
3041 FILE_BUF *ip, *op;
3042 {
3043 register U_CHAR *bp, *cp;
3044 register struct directive *kt;
3045 register int ident_length;
3046 U_CHAR *resume_p;
3047
3048 /* Nonzero means we must copy the entire command
3049 to get rid of comments or backslash-newlines. */
3050 int copy_command = 0;
3051
3052 U_CHAR *ident, *after_ident;
3053
3054 bp = ip->bufp;
3055
3056 /* Record where the directive started. do_xifdef needs this. */
3057 directive_start = bp - 1;
3058
3059 /* Skip whitespace and \-newline. */
3060 while (1) {
3061 if (is_hor_space[*bp]) {
3062 if ((*bp == '\f' || *bp == '\v') && pedantic)
3063 pedwarn ("%s in preprocessing directive",
3064 *bp == '\f' ? "formfeed" : "vertical tab");
3065 bp++;
3066 } else if (*bp == '/' && bp[1] == '*') {
3067 ip->bufp = bp;
3068 skip_to_end_of_comment (ip, &ip->lineno, 0);
3069 bp = ip->bufp;
3070 } else if (*bp == '\\' && bp[1] == '\n') {
3071 bp += 2; ip->lineno++;
3072 } else break;
3073 }
3074
3075 /* Now find end of directive name.
3076 If we encounter a backslash-newline, exchange it with any following
3077 symbol-constituents so that we end up with a contiguous name. */
3078
3079 cp = bp;
3080 while (1) {
3081 if (is_idchar[*cp])
3082 cp++;
3083 else {
3084 if (*cp == '\\' && cp[1] == '\n')
3085 name_newline_fix (cp);
3086 if (is_idchar[*cp])
3087 cp++;
3088 else break;
3089 }
3090 }
3091 ident_length = cp - bp;
3092 ident = bp;
3093 after_ident = cp;
3094
3095 /* A line of just `#' becomes blank. */
3096
3097 if (ident_length == 0 && *after_ident == '\n') {
3098 ip->bufp = after_ident;
3099 return 1;
3100 }
3101
3102 if (ident_length == 0 || !is_idstart[*ident]) {
3103 U_CHAR *p = ident;
3104 while (is_idchar[*p]) {
3105 if (*p < '0' || *p > '9')
3106 break;
3107 p++;
3108 }
3109 /* Handle # followed by a line number. */
3110 if (p != ident && !is_idchar[*p]) {
3111 static struct directive line_directive_table[] = {
3112 { 4, do_line, "line", T_LINE},
3113 };
3114 if (pedantic)
3115 pedwarn ("`#' followed by integer");
3116 after_ident = ident;
3117 kt = line_directive_table;
3118 goto old_linenum;
3119 }
3120
3121 /* Avoid error for `###' and similar cases unless -pedantic. */
3122 if (p == ident) {
3123 while (*p == '#' || is_hor_space[*p]) p++;
3124 if (*p == '\n') {
3125 if (pedantic && !lang_asm)
3126 warning ("invalid preprocessor directive");
3127 return 0;
3128 }
3129 }
3130
3131 if (!lang_asm)
3132 error ("invalid preprocessor directive name");
3133
3134 return 0;
3135 }
3136
3137 /*
3138 * Decode the keyword and call the appropriate expansion
3139 * routine, after moving the input pointer up to the next line.
3140 */
3141 for (kt = directive_table; kt->length > 0; kt++) {
3142 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
3143 register U_CHAR *buf;
3144 register U_CHAR *limit;
3145 int unterminated;
3146 int junk;
3147 int *already_output = 0;
3148
3149 /* Nonzero means do not delete comments within the directive.
3150 #define needs this when -traditional. */
3151 int keep_comments;
3152
3153 old_linenum:
3154
3155 limit = ip->buf + ip->length;
3156 unterminated = 0;
3157 keep_comments = traditional && kt->traditional_comments;
3158 /* #import is defined only in Objective C, or when on the NeXT. */
3159 if (kt->type == T_IMPORT && !(objc || lookup ("__NeXT__", -1, -1)))
3160 break;
3161
3162 /* Find the end of this command (first newline not backslashed
3163 and not in a string or comment).
3164 Set COPY_COMMAND if the command must be copied
3165 (it contains a backslash-newline or a comment). */
3166
3167 buf = bp = after_ident;
3168 while (bp < limit) {
3169 register U_CHAR c = *bp++;
3170 switch (c) {
3171 case '\\':
3172 if (bp < limit) {
3173 if (*bp == '\n') {
3174 ip->lineno++;
3175 copy_command = 1;
3176 }
3177 bp++;
3178 }
3179 break;
3180
3181 case '\'':
3182 case '\"':
3183 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command, &unterminated);
3184 /* Don't bother calling the directive if we already got an error
3185 message due to unterminated string. Skip everything and pretend
3186 we called the directive. */
3187 if (unterminated) {
3188 if (traditional) {
3189 /* Traditional preprocessing permits unterminated strings. */
3190 ip->bufp = bp;
3191 goto endloop1;
3192 }
3193 ip->bufp = bp;
3194 return 1;
3195 }
3196 break;
3197
3198 /* <...> is special for #include. */
3199 case '<':
3200 if (!kt->angle_brackets)
3201 break;
3202 while (*bp && *bp != '>') bp++;
3203 break;
3204
3205 case '/':
3206 if (*bp == '\\' && bp[1] == '\n')
3207 newline_fix (bp);
3208 if (*bp == '*'
3209 || ((cplusplus || objc) && *bp == '/')) {
3210 U_CHAR *obp = bp - 1;
3211 ip->bufp = bp + 1;
3212 skip_to_end_of_comment (ip, &ip->lineno, 0);
3213 bp = ip->bufp;
3214 /* No need to copy the command because of a comment at the end;
3215 just don't include the comment in the directive. */
3216 if (bp == limit || *bp == '\n') {
3217 bp = obp;
3218 goto endloop1;
3219 }
3220 /* Don't remove the comments if -traditional. */
3221 if (! keep_comments)
3222 copy_command++;
3223 }
3224 break;
3225
3226 case '\f':
3227 case '\v':
3228 if (pedantic)
3229 pedwarn ("%s in preprocessing directive",
3230 c == '\f' ? "formfeed" : "vertical tab");
3231 break;
3232
3233 case '\n':
3234 --bp; /* Point to the newline */
3235 ip->bufp = bp;
3236 goto endloop1;
3237 }
3238 }
3239 ip->bufp = bp;
3240
3241 endloop1:
3242 resume_p = ip->bufp;
3243 /* BP is the end of the directive.
3244 RESUME_P is the next interesting data after the directive.
3245 A comment may come between. */
3246
3247 /* If a directive should be copied through, and -E was given,
3248 pass it through before removing comments. */
3249 if (!no_output && kt->pass_thru && put_out_comments) {
3250 int len;
3251
3252 /* Output directive name. */
3253 check_expand (op, kt->length + 2);
3254 /* Make sure # is at the start of a line */
3255 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
3256 op->lineno++;
3257 *op->bufp++ = '\n';
3258 }
3259 *op->bufp++ = '#';
3260 bcopy (kt->name, op->bufp, kt->length);
3261 op->bufp += kt->length;
3262
3263 /* Output arguments. */
3264 len = (bp - buf);
3265 check_expand (op, len);
3266 bcopy (buf, op->bufp, len);
3267 op->bufp += len;
3268 /* Take account of any (escaped) newlines just output. */
3269 while (--len >= 0)
3270 if (buf[len] == '\n')
3271 op->lineno++;
3272
3273 already_output = &junk;
3274 } /* Don't we need a newline or #line? */
3275
3276 if (copy_command) {
3277 register U_CHAR *xp = buf;
3278 /* Need to copy entire command into temp buffer before dispatching */
3279
3280 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
3281 some slop */
3282 buf = cp;
3283
3284 /* Copy to the new buffer, deleting comments
3285 and backslash-newlines (and whitespace surrounding the latter). */
3286
3287 while (xp < bp) {
3288 register U_CHAR c = *xp++;
3289 *cp++ = c;
3290
3291 switch (c) {
3292 case '\n':
3293 abort (); /* A bare newline should never part of the line. */
3294 break;
3295
3296 /* <...> is special for #include. */
3297 case '<':
3298 if (!kt->angle_brackets)
3299 break;
3300 while (xp < bp && c != '>') {
3301 c = *xp++;
3302 if (c == '\\' && xp < bp && *xp == '\n')
3303 xp++;
3304 else
3305 *cp++ = c;
3306 }
3307 break;
3308
3309 case '\\':
3310 if (*xp == '\n') {
3311 xp++;
3312 cp--;
3313 if (cp != buf && is_space[cp[-1]]) {
3314 while (cp != buf && is_space[cp[-1]]) cp--;
3315 cp++;
3316 SKIP_WHITE_SPACE (xp);
3317 } else if (is_space[*xp]) {
3318 *cp++ = *xp++;
3319 SKIP_WHITE_SPACE (xp);
3320 }
3321 } else {
3322 *cp++ = *xp++;
3323 }
3324 break;
3325
3326 case '\'':
3327 case '\"':
3328 {
3329 register U_CHAR *bp1
3330 = skip_quoted_string (xp - 1, bp, ip->lineno, 0, 0, 0);
3331 while (xp != bp1)
3332 if (*xp == '\\') {
3333 if (*++xp != '\n')
3334 *cp++ = '\\';
3335 else
3336 xp++;
3337 } else
3338 *cp++ = *xp++;
3339 }
3340 break;
3341
3342 case '/':
3343 if (*xp == '*'
3344 || ((cplusplus || objc) && *xp == '/')) {
3345 ip->bufp = xp + 1;
3346 /* If we already copied the command through,
3347 already_output != 0 prevents outputting comment now. */
3348 skip_to_end_of_comment (ip, already_output, 0);
3349 if (keep_comments)
3350 while (xp != ip->bufp)
3351 *cp++ = *xp++;
3352 /* Delete or replace the slash. */
3353 else if (traditional)
3354 cp--;
3355 else
3356 cp[-1] = ' ';
3357 xp = ip->bufp;
3358 }
3359 }
3360 }
3361
3362 /* Null-terminate the copy. */
3363
3364 *cp = 0;
3365 } else
3366 cp = bp;
3367
3368 ip->bufp = resume_p;
3369
3370 /* Some directives should be written out for cc1 to process,
3371 just as if they were not defined. And sometimes we're copying
3372 definitions through. */
3373
3374 if (!no_output && already_output == 0
3375 && (kt->pass_thru
3376 || (kt->type == T_DEFINE
3377 && (dump_macros == dump_names
3378 || dump_macros == dump_definitions)))) {
3379 int len;
3380
3381 /* Output directive name. */
3382 check_expand (op, kt->length + 1);
3383 *op->bufp++ = '#';
3384 bcopy (kt->name, op->bufp, kt->length);
3385 op->bufp += kt->length;
3386
3387 if (kt->pass_thru || dump_macros == dump_definitions) {
3388 /* Output arguments. */
3389 len = (cp - buf);
3390 check_expand (op, len);
3391 bcopy (buf, op->bufp, len);
3392 op->bufp += len;
3393 }
3394 } /* Don't we need a newline or #line? */
3395
3396 /* Call the appropriate command handler. buf now points to
3397 either the appropriate place in the input buffer, or to
3398 the temp buffer if it was necessary to make one. cp
3399 points to the first char after the contents of the (possibly
3400 copied) command, in either case. */
3401 (*kt->func) (buf, cp, op, kt);
3402 check_expand (op, ip->length - (ip->bufp - ip->buf));
3403
3404 return 1;
3405 }
3406 }
3407
3408 /* It is deliberate that we don't warn about undefined directives.
3409 That is the responsibility of cc1. */
3410 return 0;
3411 }
3412 \f
3413 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3414 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3415 };
3416
3417 /*
3418 * expand things like __FILE__. Place the expansion into the output
3419 * buffer *without* rescanning.
3420 */
3421
3422 static void
3423 special_symbol (hp, op)
3424 HASHNODE *hp;
3425 FILE_BUF *op;
3426 {
3427 char *buf;
3428 int i, len;
3429 int true_indepth;
3430 FILE_BUF *ip = NULL;
3431
3432 int paren = 0; /* For special `defined' keyword */
3433
3434 if (pcp_outfile && pcp_inside_if
3435 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
3436 error ("Predefined macro `%s' used inside `#if' during precompilation",
3437 hp->name);
3438
3439 for (i = indepth; i >= 0; i--)
3440 if (instack[i].fname != NULL) {
3441 ip = &instack[i];
3442 break;
3443 }
3444 if (ip == NULL) {
3445 error ("cccp error: not in any file?!");
3446 return; /* the show must go on */
3447 }
3448
3449 switch (hp->type) {
3450 case T_FILE:
3451 case T_BASE_FILE:
3452 {
3453 char *string;
3454 if (hp->type == T_FILE)
3455 string = ip->nominal_fname;
3456 else
3457 string = instack[0].nominal_fname;
3458
3459 if (string)
3460 {
3461 buf = (char *) alloca (3 + strlen (string));
3462 sprintf (buf, "\"%s\"", string);
3463 }
3464 else
3465 buf = "\"\"";
3466
3467 break;
3468 }
3469
3470 case T_INCLUDE_LEVEL:
3471 true_indepth = 0;
3472 for (i = indepth; i >= 0; i--)
3473 if (instack[i].fname != NULL)
3474 true_indepth++;
3475
3476 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
3477 sprintf (buf, "%d", true_indepth - 1);
3478 break;
3479
3480 case T_VERSION:
3481 buf = (char *) alloca (3 + strlen (version_string));
3482 sprintf (buf, "\"%s\"", version_string);
3483 break;
3484
3485 case T_SIZE_TYPE:
3486 buf = (char *) alloca (3 + strlen (SIZE_TYPE));
3487 sprintf (buf, "%s", SIZE_TYPE);
3488 break;
3489
3490 case T_PTRDIFF_TYPE:
3491 buf = (char *) alloca (3 + strlen (PTRDIFF_TYPE));
3492 sprintf (buf, "%s", PTRDIFF_TYPE);
3493 break;
3494
3495 case T_WCHAR_TYPE:
3496 buf = (char *) alloca (3 + strlen (WCHAR_TYPE));
3497 sprintf (buf, "%s", WCHAR_TYPE);
3498 break;
3499
3500 case T_CONST:
3501 buf = (char *) alloca (4 * sizeof (int));
3502 sprintf (buf, "%d", hp->value.ival);
3503 if (pcp_inside_if && pcp_outfile)
3504 /* Output a precondition for this macro use */
3505 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
3506 break;
3507
3508 case T_SPECLINE:
3509 buf = (char *) alloca (10);
3510 sprintf (buf, "%d", ip->lineno);
3511 break;
3512
3513 case T_DATE:
3514 case T_TIME:
3515 buf = (char *) alloca (20);
3516 if (hp->type == T_DATE)
3517 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3518 timebuf->tm_mday, timebuf->tm_year + 1900);
3519 else
3520 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3521 timebuf->tm_sec);
3522 break;
3523
3524 case T_SPEC_DEFINED:
3525 buf = " 0 "; /* Assume symbol is not defined */
3526 ip = &instack[indepth];
3527 SKIP_WHITE_SPACE (ip->bufp);
3528 if (*ip->bufp == '(') {
3529 paren++;
3530 ip->bufp++; /* Skip over the paren */
3531 SKIP_WHITE_SPACE (ip->bufp);
3532 }
3533
3534 if (!is_idstart[*ip->bufp])
3535 goto oops;
3536 if (hp = lookup (ip->bufp, -1, -1)) {
3537 if (pcp_outfile && pcp_inside_if
3538 && hp->value.defn->predefined)
3539 /* Output a precondition for this macro use. */
3540 fprintf (pcp_outfile, "#define %s\n", hp->name);
3541 buf = " 1 ";
3542 }
3543 else
3544 if (pcp_outfile && pcp_inside_if) {
3545 /* Output a precondition for this macro use */
3546 U_CHAR *cp = ip->bufp;
3547 fprintf (pcp_outfile, "#undef ");
3548 while (is_idchar[*cp]) /* Ick! */
3549 fputc (*cp++, pcp_outfile);
3550 putc ('\n', pcp_outfile);
3551 }
3552 while (is_idchar[*ip->bufp])
3553 ++ip->bufp;
3554 SKIP_WHITE_SPACE (ip->bufp);
3555 if (paren) {
3556 if (*ip->bufp != ')')
3557 goto oops;
3558 ++ip->bufp;
3559 }
3560 break;
3561
3562 oops:
3563
3564 error ("`defined' without an identifier");
3565 break;
3566
3567 default:
3568 error ("cccp error: invalid special hash type"); /* time for gdb */
3569 abort ();
3570 }
3571 len = strlen (buf);
3572 check_expand (op, len);
3573 bcopy (buf, op->bufp, len);
3574 op->bufp += len;
3575
3576 return;
3577 }
3578
3579 \f
3580 /* Routines to handle #directives */
3581
3582 /* Handle #include and #import.
3583 This function expects to see "fname" or <fname> on the input. */
3584
3585 static int
3586 do_include (buf, limit, op, keyword)
3587 U_CHAR *buf, *limit;
3588 FILE_BUF *op;
3589 struct directive *keyword;
3590 {
3591 int importing = (keyword->type == T_IMPORT);
3592 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3593 static int import_warning = 0;
3594 char *fname; /* Dynamically allocated fname buffer */
3595 char *pcftry;
3596 char *pcfname;
3597 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3598
3599 struct file_name_list *search_start = include; /* Chain of dirs to search */
3600 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3601 struct file_name_list *searchptr;
3602 int flen;
3603
3604 int f; /* file number */
3605
3606 int retried = 0; /* Have already tried macro
3607 expanding the include line*/
3608 FILE_BUF trybuf; /* It got expanded into here */
3609 int system_header_p = 0; /* 0 for "...", 1 for <...> */
3610 int pcf = -1;
3611 char *pcfbuf;
3612 int pcfbuflimit;
3613 int pcfnum;
3614 f= -1; /* JF we iz paranoid! */
3615
3616 if (importing && warn_import
3617 && !instack[indepth].system_header_p && !import_warning) {
3618 import_warning = 1;
3619 warning ("using `#import' is not recommended");
3620 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3621 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3622 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3623 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3624 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3625 fprintf (stderr, " ... <real contents of file> ...\n");
3626 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3627 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3628 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3629 fprintf (stderr, "when it is equipped with such a conditional.\n");
3630 }
3631
3632 get_filename:
3633
3634 fbeg = buf;
3635 SKIP_WHITE_SPACE (fbeg);
3636 /* Discard trailing whitespace so we can easily see
3637 if we have parsed all the significant chars we were given. */
3638 while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
3639
3640 switch (*fbeg++) {
3641 case '\"':
3642 fend = fbeg;
3643 while (fend != limit && *fend != '\"')
3644 fend++;
3645 if (*fend == '\"' && fend + 1 == limit) {
3646 FILE_BUF *fp;
3647
3648 /* We have "filename". Figure out directory this source
3649 file is coming from and put it on the front of the list. */
3650
3651 /* If -I- was specified, don't search current dir, only spec'd ones. */
3652 if (ignore_srcdir) break;
3653
3654 for (fp = &instack[indepth]; fp >= instack; fp--)
3655 {
3656 int n;
3657 char *ep,*nam;
3658
3659 if ((nam = fp->nominal_fname) != NULL) {
3660 /* Found a named file. Figure out dir of the file,
3661 and put it in front of the search list. */
3662 dsp[0].next = search_start;
3663 search_start = dsp;
3664 #ifndef VMS
3665 ep = rindex (nam, '/');
3666 #else /* VMS */
3667 ep = rindex (nam, ']');
3668 if (ep == NULL) ep = rindex (nam, '>');
3669 if (ep == NULL) ep = rindex (nam, ':');
3670 if (ep != NULL) ep++;
3671 #endif /* VMS */
3672 if (ep != NULL) {
3673 n = ep - nam;
3674 dsp[0].fname = (char *) alloca (n + 1);
3675 strncpy (dsp[0].fname, nam, n);
3676 dsp[0].fname[n] = '\0';
3677 if (n > max_include_len) max_include_len = n;
3678 } else {
3679 dsp[0].fname = 0; /* Current directory */
3680 }
3681 break;
3682 }
3683 }
3684 break;
3685 }
3686 goto fail;
3687
3688 case '<':
3689 fend = fbeg;
3690 while (fend != limit && *fend != '>') fend++;
3691 if (*fend == '>' && fend + 1 == limit) {
3692 system_header_p = 1;
3693 /* If -I-, start with the first -I dir after the -I-. */
3694 if (first_bracket_include)
3695 search_start = first_bracket_include;
3696 break;
3697 }
3698 goto fail;
3699
3700 default:
3701 fail:
3702 if (retried) {
3703 if (importing)
3704 error ("`#import' expects \"fname\" or <fname>");
3705 else
3706 error ("`#include' expects \"fname\" or <fname>");
3707 return 0;
3708 } else {
3709 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
3710 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
3711 bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
3712 limit = buf + (trybuf.bufp - trybuf.buf);
3713 free (trybuf.buf);
3714 retried++;
3715 goto get_filename;
3716 }
3717 }
3718
3719 /* For #include_next, skip in the search path
3720 past the dir in which the containing file was found. */
3721 if (skip_dirs) {
3722 FILE_BUF *fp;
3723 for (fp = &instack[indepth]; fp >= instack; fp--)
3724 if (fp->fname != NULL) {
3725 /* fp->dir is null if the containing file was specified
3726 with an absolute file name. In that case, don't skip anything. */
3727 if (fp->dir)
3728 search_start = fp->dir->next;
3729 break;
3730 }
3731 }
3732
3733 flen = fend - fbeg;
3734 /* Allocate this permanently, because it gets stored in the definitions
3735 of macros. */
3736 fname = (char *) xmalloc (max_include_len + flen + 2);
3737 /* + 2 above for slash and terminating null. */
3738
3739 /* If specified file name is absolute, just open it. */
3740
3741 if (*fbeg == '/') {
3742 strncpy (fname, fbeg, flen);
3743 fname[flen] = 0;
3744 if (lookup_include (fname))
3745 return 0;
3746 if (importing)
3747 f = lookup_import (fname);
3748 else
3749 f = open (fname, O_RDONLY, 0666);
3750 if (f == -2)
3751 return 0; /* Already included this file */
3752 } else {
3753 /* Search directory path, trying to open the file.
3754 Copy each filename tried into FNAME. */
3755
3756 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3757 if (searchptr->fname) {
3758 /* The empty string in a search path is ignored.
3759 This makes it possible to turn off entirely
3760 a standard piece of the list. */
3761 if (searchptr->fname[0] == 0)
3762 continue;
3763 strcpy (fname, searchptr->fname);
3764 strcat (fname, "/");
3765 fname[strlen (fname) + flen] = 0;
3766 } else {
3767 fname[0] = 0;
3768 }
3769 strncat (fname, fbeg, flen);
3770 #ifdef VMS
3771 /* Change this 1/2 Unix 1/2 VMS file specification into a
3772 full VMS file specification */
3773 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3774 /* Fix up the filename */
3775 hack_vms_include_specification (fname);
3776 } else {
3777 /* This is a normal VMS filespec, so use it unchanged. */
3778 strncpy (fname, fbeg, flen);
3779 fname[flen] = 0;
3780 }
3781 #endif /* VMS */
3782 if (importing)
3783 f = lookup_import (fname);
3784 else
3785 f = open (fname, O_RDONLY, 0666);
3786 if (f == -2)
3787 return 0; /* Already included this file */
3788 if (lookup_include (fname)) {
3789 close (f);
3790 return 0;
3791 }
3792 if (f >= 0)
3793 break;
3794 }
3795 }
3796
3797 if (f < 0) {
3798 /* A file that was not found. */
3799
3800 strncpy (fname, fbeg, flen);
3801 fname[flen] = 0;
3802 error_from_errno (fname);
3803
3804 /* For -M, add this file to the dependencies. */
3805 if (print_deps > (system_header_p || (system_include_depth > 0))) {
3806 /* Break the line before this. */
3807 deps_output ("", 0);
3808
3809 /* If it was requested as a system header file,
3810 then assume it belongs in the first place to look for such. */
3811 if (system_header_p) {
3812 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3813 if (searchptr->fname) {
3814 if (searchptr->fname[0] == 0)
3815 continue;
3816 deps_output (searchptr->fname, 0);
3817 deps_output ("/", 0);
3818 break;
3819 }
3820 }
3821 }
3822 /* Otherwise, omit the directory, as if the file existed
3823 in the directory with the source. */
3824 deps_output (fbeg, flen);
3825 deps_output (" ", 0);
3826 }
3827 } else {
3828 struct stat stat_f;
3829
3830 /* Check to see if this include file is a once-only include file.
3831 If so, give up. */
3832
3833 struct file_name_list* ptr;
3834
3835 for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
3836 if (!strcmp (ptr->fname, fname)) {
3837 close (f);
3838 return 0; /* This file was once'd. */
3839 }
3840 }
3841
3842 for (ptr = all_include_files; ptr; ptr = ptr->next) {
3843 if (!strcmp (ptr->fname, fname))
3844 break; /* This file was included before. */
3845 }
3846
3847 if (ptr == 0) {
3848 /* This is the first time for this file. */
3849 /* Add it to list of files included. */
3850
3851 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3852 ptr->control_macro = 0;
3853 ptr->next = all_include_files;
3854 all_include_files = ptr;
3855 ptr->fname = savestring (fname);
3856
3857 /* For -M, add this file to the dependencies. */
3858 if (print_deps > (system_header_p || (system_include_depth > 0))) {
3859 deps_output ("", 0);
3860 deps_output (fname, 0);
3861 deps_output (" ", 0);
3862 }
3863 }
3864
3865 /* Handle -H option. */
3866 if (print_include_names)
3867 fprintf (stderr, "%s\n", fname);
3868
3869 if (system_header_p)
3870 system_include_depth++;
3871
3872 /* Actually process the file. */
3873 add_import (f, fname); /* Record file on "seen" list for #import. */
3874
3875 pcftry = (char *) alloca (strlen (fname) + 30);
3876 pcfbuf = 0;
3877 pcfnum = 0;
3878
3879 fstat (f, &stat_f);
3880
3881 if (!no_precomp)
3882 do {
3883 sprintf (pcftry, "%s%d", fname, pcfnum++);
3884
3885 pcf = open (pcftry, O_RDONLY, 0666);
3886 if (pcf != -1)
3887 {
3888 struct stat s;
3889
3890 fstat (pcf, &s);
3891 if (bcmp (&stat_f.st_ino, &s.st_ino, sizeof (s.st_ino))
3892 || stat_f.st_dev != s.st_dev)
3893 {
3894 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3895 /* Don't need it any more. */
3896 close (pcf);
3897 }
3898 else
3899 {
3900 /* Don't need it at all. */
3901 close (pcf);
3902 break;
3903 }
3904 }
3905 } while (pcf != -1 && !pcfbuf);
3906
3907 /* Actually process the file */
3908 if (pcfbuf) {
3909 pcfname = xmalloc (strlen (pcftry) + 1);
3910 strcpy (pcfname, pcftry);
3911 pcfinclude (pcfbuf, pcfbuflimit, fname, op);
3912 }
3913 else
3914 finclude (f, fname, op, system_header_p, searchptr);
3915
3916 if (system_header_p)
3917 system_include_depth--;
3918 }
3919 return 0;
3920 }
3921
3922 /* Return nonzero if there is no need to include file NAME
3923 because it has already been included and it contains a conditional
3924 to make a repeated include do nothing. */
3925
3926 static int
3927 lookup_include (name)
3928 char *name;
3929 {
3930 struct file_name_list *l = all_include_files;
3931 for (; l; l = l->next)
3932 if (! strcmp (name, l->fname)
3933 && l->control_macro
3934 && lookup (l->control_macro, -1, -1))
3935 return 1;
3936 return 0;
3937 }
3938
3939 /* Process the contents of include file FNAME, already open on descriptor F,
3940 with output to OP.
3941 SYSTEM_HEADER_P is 1 if this file was specified using <...>.
3942 DIRPTR is the link in the dir path through which this file was found,
3943 or 0 if the file name was absolute. */
3944
3945 static void
3946 finclude (f, fname, op, system_header_p, dirptr)
3947 int f;
3948 char *fname;
3949 FILE_BUF *op;
3950 int system_header_p;
3951 struct file_name_list *dirptr;
3952 {
3953 int st_mode;
3954 long st_size;
3955 long i;
3956 FILE_BUF *fp; /* For input stack frame */
3957 int missing_newline = 0;
3958
3959 CHECK_DEPTH (return;);
3960
3961 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
3962 goto nope; /* Impossible? */
3963
3964 fp = &instack[indepth + 1];
3965 bzero (fp, sizeof (FILE_BUF));
3966 fp->nominal_fname = fp->fname = fname;
3967 fp->length = 0;
3968 fp->lineno = 1;
3969 fp->if_stack = if_stack;
3970 fp->system_header_p = system_header_p;
3971 fp->dir = dirptr;
3972
3973 if (S_ISREG (st_mode)) {
3974 fp->buf = (U_CHAR *) alloca (st_size + 2);
3975 fp->bufp = fp->buf;
3976
3977 /* Read the file contents, knowing that st_size is an upper bound
3978 on the number of bytes we can read. */
3979 while (st_size > 0) {
3980 i = read (f, fp->buf + fp->length, st_size);
3981 if (i <= 0) {
3982 if (i == 0) break;
3983 goto nope;
3984 }
3985 fp->length += i;
3986 st_size -= i;
3987 }
3988 }
3989 else {
3990 /* Cannot count its file size before reading.
3991 First read the entire file into heap and
3992 copy them into buffer on stack. */
3993
3994 U_CHAR *bufp;
3995 U_CHAR *basep;
3996 int bsize = 2000;
3997
3998 st_size = 0;
3999 basep = (U_CHAR *) xmalloc (bsize + 2);
4000 bufp = basep;
4001
4002 for (;;) {
4003 i = read (f, bufp, bsize - st_size);
4004 if (i < 0)
4005 goto nope; /* error! */
4006 if (i == 0)
4007 break; /* End of file */
4008 st_size += i;
4009 bufp += i;
4010 if (bsize == st_size) { /* Buffer is full! */
4011 bsize *= 2;
4012 basep = (U_CHAR *) xrealloc (basep, bsize + 2);
4013 bufp = basep + st_size; /* May have moved */
4014 }
4015 }
4016 fp->buf = (U_CHAR *) alloca (st_size + 2);
4017 fp->bufp = fp->buf;
4018 bcopy (basep, fp->buf, st_size);
4019 fp->length = st_size;
4020 free (basep);
4021 }
4022
4023 /* Close descriptor now, so nesting does not use lots of descriptors. */
4024 close (f);
4025
4026 if (!no_trigraphs)
4027 trigraph_pcp (fp);
4028
4029 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
4030 /* Backslash-newline at end is not good enough. */
4031 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
4032 fp->buf[fp->length++] = '\n';
4033 missing_newline = 1;
4034 }
4035 fp->buf[fp->length] = '\0';
4036
4037 indepth++;
4038 input_file_stack_tick++;
4039
4040 output_line_command (fp, op, 0, enter_file);
4041 rescan (op, 0);
4042
4043 if (pedantic && missing_newline)
4044 pedwarn ("file does not end in newline");
4045
4046 indepth--;
4047 input_file_stack_tick++;
4048 output_line_command (&instack[indepth], op, 0, leave_file);
4049 return;
4050
4051 nope:
4052
4053 perror_with_name (fname);
4054 close (f);
4055 }
4056
4057 /* Record that inclusion of the file named FILE
4058 should be controlled by the macro named MACRO_NAME.
4059 This means that trying to include the file again
4060 will do something if that macro is defined. */
4061
4062 static void
4063 record_control_macro (file, macro_name)
4064 char *file;
4065 U_CHAR *macro_name;
4066 {
4067 struct file_name_list *new;
4068
4069 for (new = all_include_files; new; new = new->next) {
4070 if (!strcmp (new->fname, file)) {
4071 new->control_macro = macro_name;
4072 return;
4073 }
4074 }
4075
4076 /* If the file is not in all_include_files, something's wrong. */
4077 abort ();
4078 }
4079 \f
4080 /* Maintain and search list of included files, for #import. */
4081
4082 #define IMPORT_HASH_SIZE 31
4083
4084 struct import_file {
4085 char *name;
4086 ino_t inode;
4087 dev_t dev;
4088 struct import_file *next;
4089 };
4090
4091 /* Hash table of files already included with #include or #import. */
4092
4093 static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
4094
4095 /* Hash a file name for import_hash_table. */
4096
4097 static int
4098 import_hash (f)
4099 char *f;
4100 {
4101 int val = 0;
4102
4103 while (*f) val += *f++;
4104 return (val%IMPORT_HASH_SIZE);
4105 }
4106
4107 /* Search for file FILENAME in import_hash_table.
4108 Return -2 if found, either a matching name or a matching inode.
4109 Otherwise, open the file and return a file descriptor if successful
4110 or -1 if unsuccessful. */
4111
4112 static int
4113 lookup_import (filename)
4114 char *filename;
4115 {
4116 struct import_file *i;
4117 int h;
4118 int hashval;
4119 struct stat sb;
4120 int fd;
4121
4122 hashval = import_hash (filename);
4123
4124 /* Attempt to find file in list of already included files */
4125 i = import_hash_table[hashval];
4126
4127 while (i) {
4128 if (!strcmp (filename, i->name))
4129 return -2; /* return found */
4130 i = i->next;
4131 }
4132 /* Open it and try a match on inode/dev */
4133 fd = open (filename, O_RDONLY, 0666);
4134 if (fd < 0)
4135 return fd;
4136 fstat (fd, &sb);
4137 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
4138 i = import_hash_table[h];
4139 while (i) {
4140 /* Compare the inode and the device.
4141 Supposedly on some systems the inode is not a scalar. */
4142 if (!bcmp (&i->inode, &sb.st_ino, sizeof (sb.st_ino))
4143 && i->dev == sb.st_dev) {
4144 close (fd);
4145 return -2; /* return found */
4146 }
4147 i = i->next;
4148 }
4149 }
4150 return fd; /* Not found, return open file */
4151 }
4152
4153 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
4154
4155 static void
4156 add_import (fd, fname)
4157 int fd;
4158 char *fname;
4159 {
4160 struct import_file *i;
4161 int hashval;
4162 struct stat sb;
4163
4164 hashval = import_hash (fname);
4165 fstat (fd, &sb);
4166 i = (struct import_file *)xmalloc (sizeof (struct import_file));
4167 i->name = (char *)xmalloc (strlen (fname)+1);
4168 strcpy (i->name, fname);
4169 bcopy (&sb.st_ino, &i->inode, sizeof (sb.st_ino));
4170 i->dev = sb.st_dev;
4171 i->next = import_hash_table[hashval];
4172 import_hash_table[hashval] = i;
4173 }
4174 \f
4175 /* Load the specified precompiled header into core, and verify its
4176 preconditions. PCF indicates the file descriptor to read, which must
4177 be a regular file. FNAME indicates the file name of the original
4178 header. *LIMIT will be set to an address one past the end of the file.
4179 If the preconditions of the file are not satisfied, the buffer is
4180 freed and we return 0. If the preconditions are satisfied, return
4181 the address of the buffer following the preconditions. The buffer, in
4182 this case, should never be freed because various pieces of it will
4183 be referred to until all precompiled strings are output at the end of
4184 the run.
4185 */
4186 static char *
4187 check_precompiled (pcf, fname, limit)
4188 int pcf;
4189 char *fname;
4190 char **limit;
4191 {
4192 int st_mode;
4193 long st_size;
4194 int length = 0;
4195 char *buf;
4196 char *dollar_loc;
4197 int i;
4198 char *cp;
4199
4200 if (pcp_outfile)
4201 return 0;
4202
4203 if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
4204 return 0;
4205
4206 if (S_ISREG (st_mode))
4207 {
4208 buf = xmalloc (st_size + 2);
4209 while (st_size > 0)
4210 {
4211 i = read (pcf, buf + length, st_size);
4212 if (i < 0)
4213 goto nope;
4214 if (i == 0)
4215 break;
4216 length += i;
4217 st_size -= i;
4218 }
4219 }
4220 else
4221 abort ();
4222
4223 if (length > 0 && buf[length-1] != '\n')
4224 buf[length++] = '\n';
4225 buf[length] = '\0';
4226
4227 *limit = buf + length;
4228
4229 /* File is in core. Check the preconditions. */
4230 if (!check_preconditions (buf))
4231 goto nope;
4232 for (cp = buf; *cp; cp++)
4233 ;
4234 #ifdef DEBUG_PCP
4235 fprintf (stderr, "Using preinclude %s\n", fname);
4236 #endif
4237 return cp + 1;
4238
4239 nope:
4240 #ifdef DEBUG_PCP
4241 fprintf (stderr, "Cannot use preinclude %s\n", fname);
4242 #endif
4243 free (buf);
4244 return 0;
4245 }
4246
4247 /* PREC (null terminated) points to the preconditions of a
4248 precompiled header. These are a series of #define and #undef
4249 lines which must match the current contents of the hash
4250 table. */
4251 static int
4252 check_preconditions (prec)
4253 char *prec;
4254 {
4255 MACRODEF mdef;
4256 char *lineend;
4257
4258 while (*prec) {
4259 lineend = (char *) index (prec, '\n');
4260
4261 if (*prec++ != '#') {
4262 error ("Bad format encountered while reading precompiled file");
4263 return 0;
4264 }
4265 if (!strncmp (prec, "define", 6)) {
4266 HASHNODE *hp;
4267
4268 prec += 6;
4269 mdef = create_definition (prec, lineend, 0);
4270
4271 if (mdef.defn == 0)
4272 abort();
4273
4274 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
4275 || (hp->type != T_MACRO && hp->type != T_CONST)
4276 || (hp->type == T_MACRO
4277 && !compare_defs (mdef.defn, hp->value.defn)
4278 && (mdef.defn->length != 2
4279 || mdef.defn->expansion[0] != '\n'
4280 || mdef.defn->expansion[1] != ' ')))
4281 return 0;
4282 } else if (!strncmp (prec, "undef", 5)) {
4283 char *name;
4284 int len;
4285
4286 prec += 5;
4287 while (is_hor_space[*prec])
4288 prec++;
4289 name = prec;
4290 while (is_idchar[*prec])
4291 prec++;
4292 len = prec - name;
4293
4294 if (lookup (name, len, -1))
4295 return 0;
4296 } else {
4297 error ("Bad format encountered while reading precompiled file");
4298 return 0;
4299 }
4300 prec = lineend + 1;
4301 }
4302 /* They all passed successfully */
4303 return 1;
4304 }
4305
4306 /* Process the main body of a precompiled file. BUF points to the
4307 string section of the file, following the preconditions. LIMIT is one
4308 character past the end. NAME is the name of the file being read
4309 in. OP is the main output buffer */
4310 static void
4311 pcfinclude (buf, limit, name, op)
4312 U_CHAR *buf, *limit, *name;
4313 FILE_BUF *op;
4314 {
4315 FILE_BUF tmpbuf;
4316 int nstrings;
4317 U_CHAR *cp = buf;
4318
4319 /* First in the file comes 4 bytes indicating the number of strings, */
4320 /* in network byte order. (MSB first). */
4321 nstrings = *cp++;
4322 nstrings = (nstrings << 8) | *cp++;
4323 nstrings = (nstrings << 8) | *cp++;
4324 nstrings = (nstrings << 8) | *cp++;
4325
4326 /* Looping over each string... */
4327 while (nstrings--) {
4328 U_CHAR *string_start;
4329 U_CHAR *endofthiskey;
4330 STRINGDEF *str;
4331 int nkeys;
4332
4333 /* Each string starts with a STRINGDEF structure (str), followed */
4334 /* by the text of the string (string_start) */
4335
4336 /* First skip to a longword boundary */
4337 if ((int)cp & 3)
4338 cp += 4 - ((int)cp & 3);
4339
4340 /* Now get the string. */
4341 str = (STRINGDEF *) cp;
4342 string_start = cp += sizeof (STRINGDEF);
4343
4344 for (; *cp; cp++) /* skip the string */
4345 ;
4346
4347 /* We need to macro expand the string here to ensure that the
4348 proper definition environment is in place. If it were only
4349 expanded when we find out it is needed, macros necessary for
4350 its proper expansion might have had their definitions changed. */
4351 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
4352 /* Lineno is already set in the precompiled file */
4353 str->contents = tmpbuf.buf;
4354 str->len = tmpbuf.length;
4355 str->writeflag = 0;
4356 str->filename = name;
4357 str->output_mark = outbuf.bufp - outbuf.buf;
4358
4359 str->chain = 0;
4360 *stringlist_tailp = str;
4361 stringlist_tailp = &str->chain;
4362
4363 /* Next comes a fourbyte number indicating the number of keys */
4364 /* for this string. */
4365 nkeys = *cp++;
4366 nkeys = (nkeys << 8) | *cp++;
4367 nkeys = (nkeys << 8) | *cp++;
4368 nkeys = (nkeys << 8) | *cp++;
4369
4370 /* If this number is -1, then the string is mandatory. */
4371 if (nkeys == -1)
4372 str->writeflag = 1;
4373 else
4374 /* Otherwise, for each key, */
4375 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
4376 KEYDEF *kp = (KEYDEF *) cp;
4377 HASHNODE *hp;
4378
4379 /* It starts with a KEYDEF structure */
4380 cp += sizeof (KEYDEF);
4381
4382 /* Find the end of the key. At the end of this for loop we
4383 advance CP to the start of the next key using this variable. */
4384 endofthiskey = cp + strlen (cp);
4385 kp->str = str;
4386
4387 /* Expand the key, and enter it into the hash table. */
4388 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
4389 tmpbuf.bufp = tmpbuf.buf;
4390
4391 while (is_hor_space[*tmpbuf.bufp])
4392 tmpbuf.bufp++;
4393 if (!is_idstart[*tmpbuf.bufp]
4394 || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
4395 str->writeflag = 1;
4396 continue;
4397 }
4398
4399 hp = lookup (tmpbuf.bufp, -1, -1);
4400 if (hp == NULL) {
4401 kp->chain = 0;
4402 install (tmpbuf.bufp, -1, T_PCSTRING, (int) kp, -1);
4403 }
4404 else if (hp->type == T_PCSTRING) {
4405 kp->chain = hp->value.keydef;
4406 hp->value.keydef = kp;
4407 }
4408 else
4409 str->writeflag = 1;
4410 }
4411 }
4412 /* This output_line_command serves to switch us back to the current
4413 input file in case some of these strings get output (which will
4414 result in line commands for the header file being output). */
4415 output_line_command (&instack[indepth], op, 0, enter_file);
4416 }
4417
4418 /* Called from rescan when it hits a key for strings. Mark them all */
4419 /* used and clean up. */
4420 static void
4421 pcstring_used (hp)
4422 HASHNODE *hp;
4423 {
4424 KEYDEF *kp, *tmp;
4425
4426 for (kp = hp->value.keydef; kp; kp = kp->chain)
4427 kp->str->writeflag = 1;
4428 delete_macro (hp);
4429 }
4430
4431 /* Write the output, interspersing precompiled strings in their */
4432 /* appropriate places. */
4433 static void
4434 write_output ()
4435 {
4436 STRINGDEF *next_string;
4437 U_CHAR *cur_buf_loc;
4438 int line_command_len = 80;
4439 char *line_command = xmalloc (line_command_len);
4440 int len;
4441
4442 /* In each run through the loop, either cur_buf_loc == */
4443 /* next_string_loc, in which case we print a series of strings, or */
4444 /* it is less than next_string_loc, in which case we write some of */
4445 /* the buffer. */
4446 cur_buf_loc = outbuf.buf;
4447 next_string = stringlist;
4448
4449 while (cur_buf_loc < outbuf.bufp || next_string) {
4450 if (next_string
4451 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
4452 if (next_string->writeflag) {
4453 len = strlen (next_string->filename);
4454 if (len > line_command_len)
4455 line_command = xrealloc (line_command,
4456 line_command_len *= 2);
4457 sprintf (line_command, "\n# %d \"%s\"\n",
4458 next_string->lineno, next_string->filename);
4459 write (fileno (stdout), line_command,
4460 strlen (line_command));
4461 write (fileno (stdout),
4462 next_string->contents, next_string->len);
4463 }
4464 next_string = next_string->chain;
4465 }
4466 else {
4467 len = (next_string
4468 ? (next_string->output_mark
4469 - (cur_buf_loc - outbuf.buf))
4470 : outbuf.bufp - cur_buf_loc);
4471
4472 write (fileno (stdout), cur_buf_loc, len);
4473 cur_buf_loc += len;
4474 }
4475 }
4476 }
4477
4478 /* Pass a directive through to the output file.
4479 BUF points to the contents of the directive, as a contiguous string.
4480 LIMIT points to the first character past the end of the directive.
4481 KEYWORD is the keyword-table entry for the directive. */
4482
4483 static void
4484 pass_thru_directive (buf, limit, op, keyword)
4485 U_CHAR *buf, *limit;
4486 FILE_BUF *op;
4487 struct directive *keyword;
4488 {
4489 register unsigned keyword_length = keyword->length;
4490
4491 check_expand (op, 1 + keyword_length + (limit - buf));
4492 *op->bufp++ = '#';
4493 bcopy (keyword->name, op->bufp, keyword_length);
4494 op->bufp += keyword_length;
4495 if (limit != buf && buf[0] != ' ')
4496 *op->bufp++ = ' ';
4497 bcopy (buf, op->bufp, limit - buf);
4498 op->bufp += (limit - buf);
4499 *op->bufp++ = '\n';
4500 }
4501 \f
4502 /* The arglist structure is built by do_define to tell
4503 collect_definition where the argument names begin. That
4504 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
4505 would contain pointers to the strings x, y, and z.
4506 Collect_definition would then build a DEFINITION node,
4507 with reflist nodes pointing to the places x, y, and z had
4508 appeared. So the arglist is just convenience data passed
4509 between these two routines. It is not kept around after
4510 the current #define has been processed and entered into the
4511 hash table. */
4512
4513 struct arglist {
4514 struct arglist *next;
4515 U_CHAR *name;
4516 int length;
4517 int argno;
4518 };
4519
4520 /* Create a DEFINITION node from a #define directive. Arguments are
4521 as for do_define. */
4522 static MACRODEF
4523 create_definition (buf, limit, op)
4524 U_CHAR *buf, *limit;
4525 FILE_BUF *op;
4526 {
4527 U_CHAR *bp; /* temp ptr into input buffer */
4528 U_CHAR *symname; /* remember where symbol name starts */
4529 int sym_length; /* and how long it is */
4530 int line = instack[indepth].lineno;
4531 char *file = instack[indepth].nominal_fname;
4532
4533 DEFINITION *defn;
4534 int arglengths = 0; /* Accumulate lengths of arg names
4535 plus number of args. */
4536 MACRODEF mdef;
4537
4538 bp = buf;
4539
4540 while (is_hor_space[*bp])
4541 bp++;
4542
4543 symname = bp; /* remember where it starts */
4544 sym_length = check_macro_name (bp, "macro");
4545 bp += sym_length;
4546
4547 /* Lossage will occur if identifiers or control keywords are broken
4548 across lines using backslash. This is not the right place to take
4549 care of that. */
4550
4551 if (*bp == '(') {
4552 struct arglist *arg_ptrs = NULL;
4553 int argno = 0;
4554
4555 bp++; /* skip '(' */
4556 SKIP_WHITE_SPACE (bp);
4557
4558 /* Loop over macro argument names. */
4559 while (*bp != ')') {
4560 struct arglist *temp;
4561
4562 temp = (struct arglist *) alloca (sizeof (struct arglist));
4563 temp->name = bp;
4564 temp->next = arg_ptrs;
4565 temp->argno = argno++;
4566 arg_ptrs = temp;
4567
4568 if (!is_idstart[*bp])
4569 pedwarn ("parameter name starts with a digit in `#define'");
4570
4571 /* Find the end of the arg name. */
4572 while (is_idchar[*bp]) {
4573 bp++;
4574 }
4575 temp->length = bp - temp->name;
4576 arglengths += temp->length + 2;
4577 SKIP_WHITE_SPACE (bp);
4578 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
4579 error ("badly punctuated parameter list in `#define'");
4580 goto nope;
4581 }
4582 if (*bp == ',') {
4583 bp++;
4584 SKIP_WHITE_SPACE (bp);
4585 }
4586 if (bp >= limit) {
4587 error ("unterminated parameter list in `#define'");
4588 goto nope;
4589 }
4590 {
4591 struct arglist *otemp;
4592
4593 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
4594 if (temp->length == otemp->length &&
4595 strncmp(temp->name, otemp->name, temp->length) == 0) {
4596 U_CHAR *name;
4597
4598 name = (U_CHAR *) alloca(temp->length + 1);
4599 (void) strncpy(name, temp->name, temp->length);
4600 name[temp->length] = '\0';
4601 error ("duplicate argument name `%s' in `#define'", name);
4602 goto nope;
4603 }
4604 }
4605 }
4606
4607 ++bp; /* skip paren */
4608 /* Skip exactly one space or tab if any. */
4609 if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
4610 /* now everything from bp before limit is the definition. */
4611 defn = collect_expansion (bp, limit, argno, arg_ptrs);
4612
4613 /* Now set defn->args.argnames to the result of concatenating
4614 the argument names in reverse order
4615 with comma-space between them. */
4616 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
4617 {
4618 struct arglist *temp;
4619 int i = 0;
4620 for (temp = arg_ptrs; temp; temp = temp->next) {
4621 bcopy (temp->name, &defn->args.argnames[i], temp->length);
4622 i += temp->length;
4623 if (temp->next != 0) {
4624 defn->args.argnames[i++] = ',';
4625 defn->args.argnames[i++] = ' ';
4626 }
4627 }
4628 defn->args.argnames[i] = 0;
4629 }
4630 } else {
4631 /* simple expansion or empty definition; gobble it */
4632 if (is_hor_space[*bp])
4633 ++bp; /* skip exactly one blank/tab char */
4634 /* now everything from bp before limit is the definition. */
4635 defn = collect_expansion (bp, limit, -1, 0);
4636 defn->args.argnames = (U_CHAR *) "";
4637 }
4638
4639 defn->line = line;
4640 defn->file = file;
4641
4642 /* OP is null if this is a predefinition */
4643 defn->predefined = !op;
4644 mdef.defn = defn;
4645 mdef.symnam = symname;
4646 mdef.symlen = sym_length;
4647
4648 return mdef;
4649
4650 nope:
4651 mdef.defn = 0;
4652 return mdef;
4653 }
4654
4655 /* Process a #define command.
4656 BUF points to the contents of the #define command, as a contiguous string.
4657 LIMIT points to the first character past the end of the definition.
4658 KEYWORD is the keyword-table entry for #define. */
4659
4660 static int
4661 do_define (buf, limit, op, keyword)
4662 U_CHAR *buf, *limit;
4663 FILE_BUF *op;
4664 struct directive *keyword;
4665 {
4666 int hashcode;
4667 MACRODEF mdef;
4668
4669 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
4670 if (pcp_outfile && op)
4671 pass_thru_directive (buf, limit, op, keyword);
4672
4673 mdef = create_definition (buf, limit, op);
4674 if (mdef.defn == 0)
4675 goto nope;
4676
4677 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
4678
4679 {
4680 HASHNODE *hp;
4681 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
4682 int ok = 0;
4683 /* Redefining a precompiled key is ok. */
4684 if (hp->type == T_PCSTRING)
4685 ok = 1;
4686 /* Redefining a macro is ok if the definitions are the same. */
4687 else if (hp->type == T_MACRO)
4688 ok = ! compare_defs (mdef.defn, hp->value.defn);
4689 /* Redefining a constant is ok with -D. */
4690 else if (hp->type == T_CONST)
4691 ok = ! done_initializing;
4692 /* Print the warning if it's not ok. */
4693 if (!ok) {
4694 U_CHAR *msg; /* what pain... */
4695
4696 /* If we are passing through #define and #undef directives, do
4697 that for this re-definition now. */
4698 if (debug_output && op)
4699 pass_thru_directive (buf, limit, op, keyword);
4700
4701 msg = (U_CHAR *) alloca (mdef.symlen + 22);
4702 *msg = '`';
4703 bcopy (mdef.symnam, msg + 1, mdef.symlen);
4704 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
4705 pedwarn (msg);
4706 if (hp->type == T_MACRO)
4707 pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
4708 "this is the location of the previous definition");
4709 }
4710 /* Replace the old definition. */
4711 hp->type = T_MACRO;
4712 hp->value.defn = mdef.defn;
4713 } else {
4714 /* If we are passing through #define and #undef directives, do
4715 that for this new definition now. */
4716 if (debug_output && op)
4717 pass_thru_directive (buf, limit, op, keyword);
4718 install (mdef.symnam, mdef.symlen, T_MACRO, mdef.defn, hashcode);
4719 }
4720 }
4721
4722 return 0;
4723
4724 nope:
4725
4726 return 1;
4727 }
4728 \f
4729 /* Check a purported macro name SYMNAME, and yield its length.
4730 USAGE is the kind of name this is intended for. */
4731
4732 static int
4733 check_macro_name (symname, usage)
4734 U_CHAR *symname;
4735 char *usage;
4736 {
4737 U_CHAR *p;
4738 int sym_length;
4739
4740 for (p = symname; is_idchar[*p]; p++)
4741 ;
4742 sym_length = p - symname;
4743 if (sym_length == 0)
4744 error ("invalid %s name", usage);
4745 else if (!is_idstart[*symname]) {
4746 U_CHAR *msg; /* what pain... */
4747 msg = (U_CHAR *) alloca (sym_length + 1);
4748 bcopy (symname, msg, sym_length);
4749 msg[sym_length] = 0;
4750 error ("invalid %s name `%s'", usage, msg);
4751 } else {
4752 if (! strncmp (symname, "defined", 7) && sym_length == 7)
4753 error ("invalid %s name `defined'", usage);
4754 }
4755 return sym_length;
4756 }
4757
4758 /*
4759 * return zero if two DEFINITIONs are isomorphic
4760 */
4761 static int
4762 compare_defs (d1, d2)
4763 DEFINITION *d1, *d2;
4764 {
4765 register struct reflist *a1, *a2;
4766 register U_CHAR *p1 = d1->expansion;
4767 register U_CHAR *p2 = d2->expansion;
4768 int first = 1;
4769
4770 if (d1->nargs != d2->nargs)
4771 return 1;
4772 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
4773 return 1;
4774 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
4775 a1 = a1->next, a2 = a2->next) {
4776 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
4777 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
4778 || a1->argno != a2->argno
4779 || a1->stringify != a2->stringify
4780 || a1->raw_before != a2->raw_before
4781 || a1->raw_after != a2->raw_after)
4782 return 1;
4783 first = 0;
4784 p1 += a1->nchars;
4785 p2 += a2->nchars;
4786 }
4787 if (a1 != a2)
4788 return 1;
4789 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
4790 p2, d2->length - (p2 - d2->expansion), 1))
4791 return 1;
4792 return 0;
4793 }
4794
4795 /* Return 1 if two parts of two macro definitions are effectively different.
4796 One of the parts starts at BEG1 and has LEN1 chars;
4797 the other has LEN2 chars at BEG2.
4798 Any sequence of whitespace matches any other sequence of whitespace.
4799 FIRST means these parts are the first of a macro definition;
4800 so ignore leading whitespace entirely.
4801 LAST means these parts are the last of a macro definition;
4802 so ignore trailing whitespace entirely. */
4803
4804 static int
4805 comp_def_part (first, beg1, len1, beg2, len2, last)
4806 int first;
4807 U_CHAR *beg1, *beg2;
4808 int len1, len2;
4809 int last;
4810 {
4811 register U_CHAR *end1 = beg1 + len1;
4812 register U_CHAR *end2 = beg2 + len2;
4813 if (first) {
4814 while (beg1 != end1 && is_space[*beg1]) beg1++;
4815 while (beg2 != end2 && is_space[*beg2]) beg2++;
4816 }
4817 if (last) {
4818 while (beg1 != end1 && is_space[end1[-1]]) end1--;
4819 while (beg2 != end2 && is_space[end2[-1]]) end2--;
4820 }
4821 while (beg1 != end1 && beg2 != end2) {
4822 if (is_space[*beg1] && is_space[*beg2]) {
4823 while (beg1 != end1 && is_space[*beg1]) beg1++;
4824 while (beg2 != end2 && is_space[*beg2]) beg2++;
4825 } else if (*beg1 == *beg2) {
4826 beg1++; beg2++;
4827 } else break;
4828 }
4829 return (beg1 != end1) || (beg2 != end2);
4830 }
4831 \f
4832 /* Read a replacement list for a macro with parameters.
4833 Build the DEFINITION structure.
4834 Reads characters of text starting at BUF until END.
4835 ARGLIST specifies the formal parameters to look for
4836 in the text of the definition; NARGS is the number of args
4837 in that list, or -1 for a macro name that wants no argument list.
4838 MACRONAME is the macro name itself (so we can avoid recursive expansion)
4839 and NAMELEN is its length in characters.
4840
4841 Note that comments and backslash-newlines have already been deleted
4842 from the argument. */
4843
4844 /* Leading and trailing Space, Tab, etc. are converted to markers
4845 Newline Space, Newline Tab, etc.
4846 Newline Space makes a space in the final output
4847 but is discarded if stringified. (Newline Tab is similar but
4848 makes a Tab instead.)
4849
4850 If there is no trailing whitespace, a Newline Space is added at the end
4851 to prevent concatenation that would be contrary to the standard. */
4852
4853 static DEFINITION *
4854 collect_expansion (buf, end, nargs, arglist)
4855 U_CHAR *buf, *end;
4856 int nargs;
4857 struct arglist *arglist;
4858 {
4859 DEFINITION *defn;
4860 register U_CHAR *p, *limit, *lastp, *exp_p;
4861 struct reflist *endpat = NULL;
4862 /* Pointer to first nonspace after last ## seen. */
4863 U_CHAR *concat = 0;
4864 /* Pointer to first nonspace after last single-# seen. */
4865 U_CHAR *stringify = 0;
4866 int maxsize;
4867 int expected_delimiter = '\0';
4868
4869 /* Scan thru the replacement list, ignoring comments and quoted
4870 strings, picking up on the macro calls. It does a linear search
4871 thru the arg list on every potential symbol. Profiling might say
4872 that something smarter should happen. */
4873
4874 if (end < buf)
4875 abort ();
4876
4877 /* Find the beginning of the trailing whitespace. */
4878 /* Find end of leading whitespace. */
4879 limit = end;
4880 p = buf;
4881 while (p < limit && is_space[limit[-1]]) limit--;
4882 while (p < limit && is_space[*p]) p++;
4883
4884 /* Allocate space for the text in the macro definition.
4885 Leading and trailing whitespace chars need 2 bytes each.
4886 Each other input char may or may not need 1 byte,
4887 so this is an upper bound.
4888 The extra 2 are for invented trailing newline-marker and final null. */
4889 maxsize = (sizeof (DEFINITION)
4890 + 2 * (end - limit) + 2 * (p - buf)
4891 + (limit - p) + 3);
4892 defn = (DEFINITION *) xcalloc (1, maxsize);
4893
4894 defn->nargs = nargs;
4895 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
4896 lastp = exp_p;
4897
4898 p = buf;
4899
4900 /* Convert leading whitespace to Newline-markers. */
4901 while (p < limit && is_space[*p]) {
4902 *exp_p++ = '\n';
4903 *exp_p++ = *p++;
4904 }
4905
4906 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
4907 error ("`##' at start of macro definition");
4908 p += 2;
4909 }
4910
4911 /* Process the main body of the definition. */
4912 while (p < limit) {
4913 int skipped_arg = 0;
4914 register U_CHAR c = *p++;
4915
4916 *exp_p++ = c;
4917
4918 if (!traditional) {
4919 switch (c) {
4920 case '\'':
4921 case '\"':
4922 if (expected_delimiter != '\0') {
4923 if (c == expected_delimiter)
4924 expected_delimiter = '\0';
4925 } else
4926 expected_delimiter = c;
4927 break;
4928
4929 /* Special hack: if a \# is written in the #define
4930 include a # in the definition. This is useless for C code
4931 but useful for preprocessing other things. */
4932
4933 case '\\':
4934 /* \# quotes a # even outside of strings. */
4935 if (p < limit && *p == '#' && !expected_delimiter) {
4936 exp_p--;
4937 *exp_p++ = *p++;
4938 } else if (p < limit && expected_delimiter) {
4939 /* In a string, backslash goes through
4940 and makes next char ordinary. */
4941 *exp_p++ = *p++;
4942 }
4943 break;
4944
4945 case '#':
4946 /* # is ordinary inside a string. */
4947 if (expected_delimiter)
4948 break;
4949 if (p < limit && *p == '#') {
4950 /* ##: concatenate preceding and following tokens. */
4951 /* Take out the first #, discard preceding whitespace. */
4952 exp_p--;
4953 while (exp_p > lastp && is_hor_space[exp_p[-1]])
4954 --exp_p;
4955 /* Skip the second #. */
4956 p++;
4957 /* Discard following whitespace. */
4958 SKIP_WHITE_SPACE (p);
4959 concat = p;
4960 if (p == limit)
4961 error ("`##' at end of macro definition");
4962 } else {
4963 /* Single #: stringify following argument ref.
4964 Don't leave the # in the expansion. */
4965 exp_p--;
4966 SKIP_WHITE_SPACE (p);
4967 if (p == limit || ! is_idstart[*p] || nargs <= 0)
4968 error ("`#' operator is not followed by a macro argument name");
4969 else
4970 stringify = p;
4971 }
4972 break;
4973 }
4974 } else {
4975 /* In -traditional mode, recognize arguments inside strings and
4976 and character constants, and ignore special properties of #.
4977 Arguments inside strings are considered "stringified", but no
4978 extra quote marks are supplied. */
4979 switch (c) {
4980 case '\'':
4981 case '\"':
4982 if (expected_delimiter != '\0') {
4983 if (c == expected_delimiter)
4984 expected_delimiter = '\0';
4985 } else
4986 expected_delimiter = c;
4987 break;
4988
4989 case '\\':
4990 /* Backslash quotes delimiters and itself, but not macro args. */
4991 if (expected_delimiter != 0 && p < limit
4992 && (*p == expected_delimiter || *p == '\\')) {
4993 *exp_p++ = *p++;
4994 continue;
4995 }
4996 break;
4997
4998 case '/':
4999 if (expected_delimiter != '\0') /* No comments inside strings. */
5000 break;
5001 if (*p == '*') {
5002 /* If we find a comment that wasn't removed by handle_directive,
5003 this must be -traditional. So replace the comment with
5004 nothing at all. */
5005 exp_p--;
5006 p += 1;
5007 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
5008 p++;
5009 #if 0
5010 /* Mark this as a concatenation-point, as if it had been ##. */
5011 concat = p;
5012 #endif
5013 }
5014 break;
5015 }
5016 }
5017
5018 /* Handle the start of a symbol. */
5019 if (is_idchar[c] && nargs > 0) {
5020 U_CHAR *id_beg = p - 1;
5021 int id_len;
5022
5023 --exp_p;
5024 while (p != limit && is_idchar[*p]) p++;
5025 id_len = p - id_beg;
5026
5027 if (is_idstart[c]) {
5028 register struct arglist *arg;
5029
5030 for (arg = arglist; arg != NULL; arg = arg->next) {
5031 struct reflist *tpat;
5032
5033 if (arg->name[0] == c
5034 && arg->length == id_len
5035 && strncmp (arg->name, id_beg, id_len) == 0) {
5036 if (expected_delimiter && warn_stringify) {
5037 if (traditional) {
5038 warning ("macro argument `%.*s' is stringified.",
5039 id_len, arg->name);
5040 } else {
5041 warning ("macro arg `%.*s' would be stringified with -traditional.",
5042 id_len, arg->name);
5043 }
5044 }
5045 /* If ANSI, don't actually substitute inside a string. */
5046 if (!traditional && expected_delimiter)
5047 break;
5048 /* make a pat node for this arg and append it to the end of
5049 the pat list */
5050 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
5051 tpat->next = NULL;
5052 tpat->raw_before = concat == id_beg;
5053 tpat->raw_after = 0;
5054 tpat->stringify = (traditional ? expected_delimiter != '\0'
5055 : stringify == id_beg);
5056
5057 if (endpat == NULL)
5058 defn->pattern = tpat;
5059 else
5060 endpat->next = tpat;
5061 endpat = tpat;
5062
5063 tpat->argno = arg->argno;
5064 tpat->nchars = exp_p - lastp;
5065 {
5066 register U_CHAR *p1 = p;
5067 SKIP_WHITE_SPACE (p1);
5068 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
5069 tpat->raw_after = 1;
5070 }
5071 lastp = exp_p; /* place to start copying from next time */
5072 skipped_arg = 1;
5073 break;
5074 }
5075 }
5076 }
5077
5078 /* If this was not a macro arg, copy it into the expansion. */
5079 if (! skipped_arg) {
5080 register U_CHAR *lim1 = p;
5081 p = id_beg;
5082 while (p != lim1)
5083 *exp_p++ = *p++;
5084 if (stringify == id_beg)
5085 error ("`#' operator should be followed by a macro argument name");
5086 }
5087 }
5088 }
5089
5090 if (limit < end) {
5091 /* Convert trailing whitespace to Newline-markers. */
5092 while (limit < end && is_space[*limit]) {
5093 *exp_p++ = '\n';
5094 *exp_p++ = *limit++;
5095 }
5096 } else if (!traditional) {
5097 /* There is no trailing whitespace, so invent some. */
5098 *exp_p++ = '\n';
5099 *exp_p++ = ' ';
5100 }
5101
5102 *exp_p = '\0';
5103
5104 defn->length = exp_p - defn->expansion;
5105
5106 /* Crash now if we overrun the allocated size. */
5107 if (defn->length + 1 > maxsize)
5108 abort ();
5109
5110 #if 0
5111 /* This isn't worth the time it takes. */
5112 /* give back excess storage */
5113 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
5114 #endif
5115
5116 return defn;
5117 }
5118 \f
5119 static int
5120 do_assert (buf, limit, op, keyword)
5121 U_CHAR *buf, *limit;
5122 FILE_BUF *op;
5123 struct directive *keyword;
5124 {
5125 U_CHAR *bp; /* temp ptr into input buffer */
5126 U_CHAR *symname; /* remember where symbol name starts */
5127 int sym_length; /* and how long it is */
5128 struct arglist *tokens = NULL;
5129
5130 if (pedantic && done_initializing && !instack[indepth].system_header_p)
5131 pedwarn ("ANSI C does not allow `#assert'");
5132
5133 bp = buf;
5134
5135 while (is_hor_space[*bp])
5136 bp++;
5137
5138 symname = bp; /* remember where it starts */
5139 sym_length = check_macro_name (bp, "assertion");
5140 bp += sym_length;
5141 /* #define doesn't do this, but we should. */
5142 SKIP_WHITE_SPACE (bp);
5143
5144 /* Lossage will occur if identifiers or control tokens are broken
5145 across lines using backslash. This is not the right place to take
5146 care of that. */
5147
5148 if (*bp != '(') {
5149 error ("missing token-sequence in `#assert'");
5150 return 1;
5151 }
5152
5153 {
5154 int error_flag = 0;
5155
5156 bp++; /* skip '(' */
5157 SKIP_WHITE_SPACE (bp);
5158
5159 tokens = read_token_list (&bp, limit, &error_flag);
5160 if (error_flag)
5161 return 1;
5162 if (tokens == 0) {
5163 error ("empty token-sequence in `#assert'");
5164 return 1;
5165 }
5166
5167 ++bp; /* skip paren */
5168 SKIP_WHITE_SPACE (bp);
5169 }
5170
5171 /* If this name isn't already an assertion name, make it one.
5172 Error if it was already in use in some other way. */
5173
5174 {
5175 ASSERTION_HASHNODE *hp;
5176 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
5177 struct tokenlist_list *value
5178 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
5179
5180 hp = assertion_lookup (symname, sym_length, hashcode);
5181 if (hp == NULL) {
5182 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
5183 error ("`defined' redefined as assertion");
5184 hp = assertion_install (symname, sym_length, hashcode);
5185 }
5186
5187 /* Add the spec'd token-sequence to the list of such. */
5188 value->tokens = tokens;
5189 value->next = hp->value;
5190 hp->value = value;
5191 }
5192
5193 return 0;
5194 }
5195 \f
5196 static int
5197 do_unassert (buf, limit, op, keyword)
5198 U_CHAR *buf, *limit;
5199 FILE_BUF *op;
5200 struct directive *keyword;
5201 {
5202 U_CHAR *bp; /* temp ptr into input buffer */
5203 U_CHAR *symname; /* remember where symbol name starts */
5204 int sym_length; /* and how long it is */
5205
5206 struct arglist *tokens = NULL;
5207 int tokens_specified = 0;
5208
5209 if (pedantic && done_initializing && !instack[indepth].system_header_p)
5210 pedwarn ("ANSI C does not allow `#unassert'");
5211
5212 bp = buf;
5213
5214 while (is_hor_space[*bp])
5215 bp++;
5216
5217 symname = bp; /* remember where it starts */
5218 sym_length = check_macro_name (bp, "assertion");
5219 bp += sym_length;
5220 /* #define doesn't do this, but we should. */
5221 SKIP_WHITE_SPACE (bp);
5222
5223 /* Lossage will occur if identifiers or control tokens are broken
5224 across lines using backslash. This is not the right place to take
5225 care of that. */
5226
5227 if (*bp == '(') {
5228 int error_flag = 0;
5229
5230 bp++; /* skip '(' */
5231 SKIP_WHITE_SPACE (bp);
5232
5233 tokens = read_token_list (&bp, limit, &error_flag);
5234 if (error_flag)
5235 return 1;
5236 if (tokens == 0) {
5237 error ("empty token list in `#unassert'");
5238 return 1;
5239 }
5240
5241 tokens_specified = 1;
5242
5243 ++bp; /* skip paren */
5244 SKIP_WHITE_SPACE (bp);
5245 }
5246
5247 {
5248 ASSERTION_HASHNODE *hp;
5249 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
5250 struct tokenlist_list *tail, *prev;
5251
5252 hp = assertion_lookup (symname, sym_length, hashcode);
5253 if (hp == NULL)
5254 return 1;
5255
5256 /* If no token list was specified, then eliminate this assertion
5257 entirely. */
5258 if (! tokens_specified) {
5259 struct tokenlist_list *next;
5260 for (tail = hp->value; tail; tail = next) {
5261 next = tail->next;
5262 free_token_list (tail->tokens);
5263 free (tail);
5264 }
5265 delete_assertion (hp);
5266 } else {
5267 /* If a list of tokens was given, then delete any matching list. */
5268
5269 tail = hp->value;
5270 prev = 0;
5271 while (tail) {
5272 struct tokenlist_list *next = tail->next;
5273 if (compare_token_lists (tail->tokens, tokens)) {
5274 if (prev)
5275 prev->next = next;
5276 else
5277 hp->value = tail->next;
5278 free_token_list (tail->tokens);
5279 free (tail);
5280 } else {
5281 prev = tail;
5282 }
5283 tail = next;
5284 }
5285 }
5286 }
5287
5288 return 0;
5289 }
5290 \f
5291 /* Test whether there is an assertion named NAME
5292 and optionally whether it has an asserted token list TOKENS.
5293 NAME is not null terminated; its length is SYM_LENGTH.
5294 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
5295
5296 int
5297 check_assertion (name, sym_length, tokens_specified, tokens)
5298 U_CHAR *name;
5299 int sym_length;
5300 int tokens_specified;
5301 struct arglist *tokens;
5302 {
5303 ASSERTION_HASHNODE *hp;
5304 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
5305
5306 if (pedantic && !instack[indepth].system_header_p)
5307 pedwarn ("ANSI C does not allow testing assertions");
5308
5309 hp = assertion_lookup (name, sym_length, hashcode);
5310 if (hp == NULL)
5311 /* It is not an assertion; just return false. */
5312 return 0;
5313
5314 /* If no token list was specified, then value is 1. */
5315 if (! tokens_specified)
5316 return 1;
5317
5318 {
5319 struct tokenlist_list *tail;
5320
5321 tail = hp->value;
5322
5323 /* If a list of tokens was given,
5324 then succeed if the assertion records a matching list. */
5325
5326 while (tail) {
5327 if (compare_token_lists (tail->tokens, tokens))
5328 return 1;
5329 tail = tail->next;
5330 }
5331
5332 /* Fail if the assertion has no matching list. */
5333 return 0;
5334 }
5335 }
5336
5337 /* Compare two lists of tokens for equality including order of tokens. */
5338
5339 static int
5340 compare_token_lists (l1, l2)
5341 struct arglist *l1, *l2;
5342 {
5343 while (l1 && l2) {
5344 if (l1->length != l2->length)
5345 return 0;
5346 if (strncmp (l1->name, l2->name, l1->length))
5347 return 0;
5348 l1 = l1->next;
5349 l2 = l2->next;
5350 }
5351
5352 /* Succeed if both lists end at the same time. */
5353 return l1 == l2;
5354 }
5355 \f
5356 /* Read a space-separated list of tokens ending in a close parenthesis.
5357 Return a list of strings, in the order they were written.
5358 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
5359 Parse the text starting at *BPP, and update *BPP.
5360 Don't parse beyond LIMIT. */
5361
5362 static struct arglist *
5363 read_token_list (bpp, limit, error_flag)
5364 U_CHAR **bpp;
5365 U_CHAR *limit;
5366 int *error_flag;
5367 {
5368 struct arglist *token_ptrs = 0;
5369 U_CHAR *bp = *bpp;
5370 int depth = 1;
5371
5372 *error_flag = 0;
5373
5374 /* Loop over the assertion value tokens. */
5375 while (depth > 0) {
5376 struct arglist *temp;
5377 int eofp = 0;
5378 U_CHAR *beg = bp;
5379
5380 /* Find the end of the token. */
5381 if (*bp == '(') {
5382 bp++;
5383 depth++;
5384 } else if (*bp == ')') {
5385 depth--;
5386 if (depth == 0)
5387 break;
5388 bp++;
5389 } else if (*bp == '"' || *bp == '\'')
5390 bp = skip_quoted_string (bp, limit, 0, 0, 0, &eofp);
5391 else
5392 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
5393 && *bp != '"' && *bp != '\'' && bp != limit)
5394 bp++;
5395
5396 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
5397 temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
5398 bcopy (beg, temp->name, bp - beg);
5399 temp->name[bp - beg] = 0;
5400 temp->next = token_ptrs;
5401 token_ptrs = temp;
5402 temp->length = bp - beg;
5403
5404 SKIP_WHITE_SPACE (bp);
5405
5406 if (bp >= limit) {
5407 error ("unterminated token sequence in `#assert' or `#unassert'");
5408 *error_flag = -1;
5409 return 0;
5410 }
5411 }
5412 *bpp = bp;
5413
5414 /* We accumulated the names in reverse order.
5415 Now reverse them to get the proper order. */
5416 {
5417 register struct arglist *prev = 0, *this, *next;
5418 for (this = token_ptrs; this; this = next) {
5419 next = this->next;
5420 this->next = prev;
5421 prev = this;
5422 }
5423 return prev;
5424 }
5425 }
5426
5427 static void
5428 free_token_list (tokens)
5429 struct arglist *tokens;
5430 {
5431 while (tokens) {
5432 struct arglist *next = tokens->next;
5433 free (tokens->name);
5434 free (tokens);
5435 tokens = next;
5436 }
5437 }
5438 \f
5439 /*
5440 * Install a name in the assertion hash table.
5441 *
5442 * If LEN is >= 0, it is the length of the name.
5443 * Otherwise, compute the length by scanning the entire name.
5444 *
5445 * If HASH is >= 0, it is the precomputed hash code.
5446 * Otherwise, compute the hash code.
5447 */
5448 static ASSERTION_HASHNODE *
5449 assertion_install (name, len, hash)
5450 U_CHAR *name;
5451 int len;
5452 int hash;
5453 {
5454 register ASSERTION_HASHNODE *hp;
5455 register int i, bucket;
5456 register U_CHAR *p, *q;
5457
5458 i = sizeof (ASSERTION_HASHNODE) + len + 1;
5459 hp = (ASSERTION_HASHNODE *) xmalloc (i);
5460 bucket = hash;
5461 hp->bucket_hdr = &assertion_hashtab[bucket];
5462 hp->next = assertion_hashtab[bucket];
5463 assertion_hashtab[bucket] = hp;
5464 hp->prev = NULL;
5465 if (hp->next != NULL)
5466 hp->next->prev = hp;
5467 hp->length = len;
5468 hp->value = 0;
5469 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
5470 p = hp->name;
5471 q = name;
5472 for (i = 0; i < len; i++)
5473 *p++ = *q++;
5474 hp->name[len] = 0;
5475 return hp;
5476 }
5477
5478 /*
5479 * find the most recent hash node for name name (ending with first
5480 * non-identifier char) installed by install
5481 *
5482 * If LEN is >= 0, it is the length of the name.
5483 * Otherwise, compute the length by scanning the entire name.
5484 *
5485 * If HASH is >= 0, it is the precomputed hash code.
5486 * Otherwise, compute the hash code.
5487 */
5488 static ASSERTION_HASHNODE *
5489 assertion_lookup (name, len, hash)
5490 U_CHAR *name;
5491 int len;
5492 int hash;
5493 {
5494 register U_CHAR *bp;
5495 register ASSERTION_HASHNODE *bucket;
5496
5497 bucket = assertion_hashtab[hash];
5498 while (bucket) {
5499 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
5500 return bucket;
5501 bucket = bucket->next;
5502 }
5503 return NULL;
5504 }
5505
5506 static void
5507 delete_assertion (hp)
5508 ASSERTION_HASHNODE *hp;
5509 {
5510
5511 if (hp->prev != NULL)
5512 hp->prev->next = hp->next;
5513 if (hp->next != NULL)
5514 hp->next->prev = hp->prev;
5515
5516 /* make sure that the bucket chain header that
5517 the deleted guy was on points to the right thing afterwards. */
5518 if (hp == *hp->bucket_hdr)
5519 *hp->bucket_hdr = hp->next;
5520
5521 free (hp);
5522 }
5523 \f
5524 /*
5525 * interpret #line command. Remembers previously seen fnames
5526 * in its very own hash table.
5527 */
5528 #define FNAME_HASHSIZE 37
5529
5530 static int
5531 do_line (buf, limit, op, keyword)
5532 U_CHAR *buf, *limit;
5533 FILE_BUF *op;
5534 struct directive *keyword;
5535 {
5536 register U_CHAR *bp;
5537 FILE_BUF *ip = &instack[indepth];
5538 FILE_BUF tem;
5539 int new_lineno;
5540 enum file_change_code file_change = same_file;
5541
5542 /* Expand any macros. */
5543 tem = expand_to_temp_buffer (buf, limit, 0, 0);
5544
5545 /* Point to macroexpanded line, which is null-terminated now. */
5546 bp = tem.buf;
5547 SKIP_WHITE_SPACE (bp);
5548
5549 if (!isdigit (*bp)) {
5550 error ("invalid format `#line' command");
5551 return 0;
5552 }
5553
5554 /* The Newline at the end of this line remains to be processed.
5555 To put the next line at the specified line number,
5556 we must store a line number now that is one less. */
5557 new_lineno = atoi (bp) - 1;
5558
5559 /* skip over the line number. */
5560 while (isdigit (*bp))
5561 bp++;
5562
5563 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
5564 if (*bp && !is_space[*bp]) {
5565 error ("invalid format `#line' command");
5566 return;
5567 }
5568 #endif
5569
5570 SKIP_WHITE_SPACE (bp);
5571
5572 if (*bp == '\"') {
5573 static HASHNODE *fname_table[FNAME_HASHSIZE];
5574 HASHNODE *hp, **hash_bucket;
5575 U_CHAR *fname;
5576 int fname_length;
5577
5578 fname = ++bp;
5579
5580 while (*bp && *bp != '\"')
5581 bp++;
5582 if (*bp != '\"') {
5583 error ("invalid format `#line' command");
5584 return 0;
5585 }
5586
5587 fname_length = bp - fname;
5588
5589 bp++;
5590 SKIP_WHITE_SPACE (bp);
5591 if (*bp) {
5592 if (*bp == '1')
5593 file_change = enter_file;
5594 else if (*bp == '2')
5595 file_change = leave_file;
5596 else if (*bp == '3')
5597 ip->system_header_p = 1;
5598 else {
5599 error ("invalid format `#line' command");
5600 return 0;
5601 }
5602
5603 bp++;
5604 SKIP_WHITE_SPACE (bp);
5605 if (*bp == '3') {
5606 ip->system_header_p = 1;
5607 bp++;
5608 SKIP_WHITE_SPACE (bp);
5609 }
5610 if (*bp) {
5611 error ("invalid format `#line' command");
5612 return 0;
5613 }
5614 }
5615
5616 hash_bucket =
5617 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
5618 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5619 if (hp->length == fname_length &&
5620 strncmp (hp->value.cpval, fname, fname_length) == 0) {
5621 ip->nominal_fname = hp->value.cpval;
5622 break;
5623 }
5624 if (hp == 0) {
5625 /* Didn't find it; cons up a new one. */
5626 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
5627 hp->next = *hash_bucket;
5628 *hash_bucket = hp;
5629
5630 hp->length = fname_length;
5631 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
5632 bcopy (fname, hp->value.cpval, fname_length);
5633 }
5634 } else if (*bp) {
5635 error ("invalid format `#line' command");
5636 return 0;
5637 }
5638
5639 ip->lineno = new_lineno;
5640 output_line_command (ip, op, 0, file_change);
5641 check_expand (op, ip->length - (ip->bufp - ip->buf));
5642 return 0;
5643 }
5644
5645 /*
5646 * remove the definition of a symbol from the symbol table.
5647 * according to un*x /lib/cpp, it is not an error to undef
5648 * something that has no definitions, so it isn't one here either.
5649 */
5650
5651 static int
5652 do_undef (buf, limit, op, keyword)
5653 U_CHAR *buf, *limit;
5654 FILE_BUF *op;
5655 struct directive *keyword;
5656 {
5657 int sym_length;
5658 HASHNODE *hp;
5659 U_CHAR *orig_buf = buf;
5660
5661 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
5662 if (pcp_outfile && op)
5663 pass_thru_directive (buf, limit, op, keyword);
5664
5665 SKIP_WHITE_SPACE (buf);
5666 sym_length = check_macro_name (buf, "macro");
5667
5668 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
5669 /* If we are generating additional info for debugging (with -g) we
5670 need to pass through all effective #undef commands. */
5671 if (debug_output && op)
5672 pass_thru_directive (orig_buf, limit, op, keyword);
5673 if (hp->type != T_MACRO)
5674 warning ("undefining `%s'", hp->name);
5675 delete_macro (hp);
5676 }
5677
5678 if (pedantic) {
5679 buf += sym_length;
5680 SKIP_WHITE_SPACE (buf);
5681 if (buf != limit)
5682 pedwarn ("garbage after `#undef' directive");
5683 }
5684 return 0;
5685 }
5686 \f
5687 /*
5688 * Report a fatal error detected by the program we are processing.
5689 * Use the text of the line in the error message, then terminate.
5690 * (We use error() because it prints the filename & line#.)
5691 */
5692
5693 static int
5694 do_error (buf, limit, op, keyword)
5695 U_CHAR *buf, *limit;
5696 FILE_BUF *op;
5697 struct directive *keyword;
5698 {
5699 int length = limit - buf;
5700 char *copy = (char *) xmalloc (length + 1);
5701 bcopy (buf, copy, length);
5702 copy[length] = 0;
5703 SKIP_WHITE_SPACE (copy);
5704 error ("#error %s", copy);
5705 exit (FAILURE_EXIT_CODE);
5706 /* NOTREACHED */
5707 return 0;
5708 }
5709
5710 /*
5711 * Report a warning detected by the program we are processing.
5712 * Use the text of the line in the warning message, then continue.
5713 * (We use error() because it prints the filename & line#.)
5714 */
5715
5716 static int
5717 do_warning (buf, limit, op, keyword)
5718 U_CHAR *buf, *limit;
5719 FILE_BUF *op;
5720 struct directive *keyword;
5721 {
5722 int length = limit - buf;
5723 char *copy = (char *) xmalloc (length + 1);
5724 bcopy (buf, copy, length);
5725 copy[length] = 0;
5726 SKIP_WHITE_SPACE (copy);
5727 error ("#warning %s", copy);
5728 return 0;
5729 }
5730
5731 /* Remember the name of the current file being read from so that we can
5732 avoid ever including it again. */
5733
5734 static int
5735 do_once ()
5736 {
5737 int i;
5738 FILE_BUF *ip = NULL;
5739
5740 for (i = indepth; i >= 0; i--)
5741 if (instack[i].fname != NULL) {
5742 ip = &instack[i];
5743 break;
5744 }
5745
5746 if (ip != NULL) {
5747 struct file_name_list *new;
5748
5749 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5750 new->next = dont_repeat_files;
5751 dont_repeat_files = new;
5752 new->fname = savestring (ip->fname);
5753 new->control_macro = 0;
5754 }
5755 return 0;
5756 }
5757
5758 /* #ident has already been copied to the output file, so just ignore it. */
5759
5760 static int
5761 do_ident (buf, limit)
5762 U_CHAR *buf, *limit;
5763 {
5764 /* Allow #ident in system headers, since that's not user's fault. */
5765 if (pedantic && !instack[indepth].system_header_p)
5766 pedwarn ("ANSI C does not allow `#ident'");
5767 return 0;
5768 }
5769
5770 /* #pragma and its argument line have already been copied to the output file.
5771 Here just check for recognized pragmas. */
5772
5773 static int
5774 do_pragma (buf, limit)
5775 U_CHAR *buf, *limit;
5776 {
5777 while (*buf == ' ' || *buf == '\t')
5778 buf++;
5779 if (!strncmp (buf, "once", 4)) {
5780 warning ("`#pragma once' is obsolete");
5781 do_once ();
5782 }
5783 return 0;
5784 }
5785
5786 #if 0
5787 /* This was a fun hack, but #pragma seems to start to be useful.
5788 By failing to recognize it, we pass it through unchanged to cc1. */
5789
5790 /*
5791 * the behavior of the #pragma directive is implementation defined.
5792 * this implementation defines it as follows.
5793 */
5794
5795 static int
5796 do_pragma ()
5797 {
5798 close (0);
5799 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
5800 goto nope;
5801 close (1);
5802 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
5803 goto nope;
5804 execl ("/usr/games/hack", "#pragma", 0);
5805 execl ("/usr/games/rogue", "#pragma", 0);
5806 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
5807 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
5808 nope:
5809 fatal ("You are in a maze of twisty compiler features, all different");
5810 }
5811 #endif
5812
5813 /* Just ignore #sccs, on systems where we define it at all. */
5814
5815 static int
5816 do_sccs ()
5817 {
5818 if (pedantic)
5819 pedwarn ("ANSI C does not allow `#sccs'");
5820 return 0;
5821 }
5822 \f
5823 /*
5824 * handle #if command by
5825 * 1) inserting special `defined' keyword into the hash table
5826 * that gets turned into 0 or 1 by special_symbol (thus,
5827 * if the luser has a symbol called `defined' already, it won't
5828 * work inside the #if command)
5829 * 2) rescan the input into a temporary output buffer
5830 * 3) pass the output buffer to the yacc parser and collect a value
5831 * 4) clean up the mess left from steps 1 and 2.
5832 * 5) call conditional_skip to skip til the next #endif (etc.),
5833 * or not, depending on the value from step 3.
5834 */
5835
5836 static int
5837 do_if (buf, limit, op, keyword)
5838 U_CHAR *buf, *limit;
5839 FILE_BUF *op;
5840 struct directive *keyword;
5841 {
5842 int value;
5843 FILE_BUF *ip = &instack[indepth];
5844
5845 value = eval_if_expression (buf, limit - buf);
5846 conditional_skip (ip, value == 0, T_IF, 0);
5847 return 0;
5848 }
5849
5850 /*
5851 * handle a #elif directive by not changing if_stack either.
5852 * see the comment above do_else.
5853 */
5854
5855 static int
5856 do_elif (buf, limit, op, keyword)
5857 U_CHAR *buf, *limit;
5858 FILE_BUF *op;
5859 struct directive *keyword;
5860 {
5861 int value;
5862 FILE_BUF *ip = &instack[indepth];
5863
5864 if (if_stack == instack[indepth].if_stack) {
5865 error ("`#elif' not within a conditional");
5866 return 0;
5867 } else {
5868 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
5869 error ("`#elif' after `#else'");
5870 fprintf (stderr, " (matches line %d", if_stack->lineno);
5871 if (if_stack->fname != NULL && ip->fname != NULL &&
5872 strcmp (if_stack->fname, ip->nominal_fname) != 0)
5873 fprintf (stderr, ", file %s", if_stack->fname);
5874 fprintf (stderr, ")\n");
5875 }
5876 if_stack->type = T_ELIF;
5877 }
5878
5879 if (if_stack->if_succeeded)
5880 skip_if_group (ip, 0);
5881 else {
5882 value = eval_if_expression (buf, limit - buf);
5883 if (value == 0)
5884 skip_if_group (ip, 0);
5885 else {
5886 ++if_stack->if_succeeded; /* continue processing input */
5887 output_line_command (ip, op, 1, same_file);
5888 }
5889 }
5890 return 0;
5891 }
5892
5893 /*
5894 * evaluate a #if expression in BUF, of length LENGTH,
5895 * then parse the result as a C expression and return the value as an int.
5896 */
5897 static int
5898 eval_if_expression (buf, length)
5899 U_CHAR *buf;
5900 int length;
5901 {
5902 FILE_BUF temp_obuf;
5903 HASHNODE *save_defined;
5904 int value;
5905
5906 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, -1);
5907 pcp_inside_if = 1;
5908 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
5909 pcp_inside_if = 0;
5910 delete_macro (save_defined); /* clean up special symbol */
5911
5912 value = parse_c_expression (temp_obuf.buf);
5913
5914 free (temp_obuf.buf);
5915
5916 return value;
5917 }
5918
5919 /*
5920 * routine to handle ifdef/ifndef. Try to look up the symbol,
5921 * then do or don't skip to the #endif/#else/#elif depending
5922 * on what directive is actually being processed.
5923 */
5924
5925 static int
5926 do_xifdef (buf, limit, op, keyword)
5927 U_CHAR *buf, *limit;
5928 FILE_BUF *op;
5929 struct directive *keyword;
5930 {
5931 int skip;
5932 FILE_BUF *ip = &instack[indepth];
5933 U_CHAR *end;
5934 int start_of_file = 0;
5935 U_CHAR *control_macro = 0;
5936
5937 /* Detect a #ifndef at start of file (not counting comments). */
5938 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
5939 U_CHAR *p = ip->buf;
5940 while (p != directive_start) {
5941 char c = *p++;
5942 switch (c) {
5943 case ' ':
5944 case '\t':
5945 case '\n':
5946 break;
5947 case '/':
5948 if (p != ip->bufp && *p == '*') {
5949 /* Skip this comment. */
5950 int junk;
5951 U_CHAR *save_bufp = ip->bufp;
5952 ip->bufp = p + 1;
5953 p = skip_to_end_of_comment (ip, &junk, 1);
5954 ip->bufp = save_bufp;
5955 }
5956 break;
5957 default:
5958 goto fail;
5959 }
5960 }
5961 /* If we get here, this conditional is the beginning of the file. */
5962 start_of_file = 1;
5963 fail: ;
5964 }
5965
5966 /* Discard leading and trailing whitespace. */
5967 SKIP_WHITE_SPACE (buf);
5968 while (limit != buf && is_hor_space[limit[-1]]) limit--;
5969
5970 /* Find the end of the identifier at the beginning. */
5971 for (end = buf; is_idchar[*end]; end++);
5972
5973 if (end == buf) {
5974 skip = (keyword->type == T_IFDEF);
5975 if (! traditional)
5976 pedwarn (end == limit ? "`#%s' with no argument"
5977 : "`#%s' argument starts with punctuation",
5978 keyword->name);
5979 } else {
5980 HASHNODE *hp;
5981
5982 if (pedantic && buf[0] >= '0' && buf[0] <= '9')
5983 pedwarn ("`#%s' argument starts with a digit", keyword->name);
5984 else if (end != limit && !traditional)
5985 pedwarn ("garbage at end of `#%s' argument", keyword->name);
5986
5987 hp = lookup (buf, end-buf, -1);
5988
5989 if (pcp_outfile) {
5990 /* Output a precondition for this macro. */
5991 if (hp && hp->value.defn->predefined)
5992 fprintf(pcp_outfile, "#define %s\n", hp->name);
5993 else {
5994 U_CHAR *cp = buf;
5995 fprintf(pcp_outfile, "#undef ");
5996 while (is_idchar[*cp]) /* Ick! */
5997 fputc (*cp++, pcp_outfile);
5998 putc ('\n', pcp_outfile);
5999 }
6000 }
6001
6002 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
6003 if (start_of_file && !skip) {
6004 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
6005 bcopy (buf, control_macro, end - buf);
6006 control_macro[end - buf] = 0;
6007 }
6008 }
6009
6010 conditional_skip (ip, skip, T_IF, control_macro);
6011 return 0;
6012 }
6013
6014 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
6015 If this is a #ifndef starting at the beginning of a file,
6016 CONTROL_MACRO is the macro name tested by the #ifndef.
6017 Otherwise, CONTROL_MACRO is 0. */
6018
6019 static void
6020 conditional_skip (ip, skip, type, control_macro)
6021 FILE_BUF *ip;
6022 int skip;
6023 enum node_type type;
6024 U_CHAR *control_macro;
6025 {
6026 IF_STACK_FRAME *temp;
6027
6028 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
6029 temp->fname = ip->nominal_fname;
6030 temp->lineno = ip->lineno;
6031 temp->next = if_stack;
6032 temp->control_macro = control_macro;
6033 if_stack = temp;
6034
6035 if_stack->type = type;
6036
6037 if (skip != 0) {
6038 skip_if_group (ip, 0);
6039 return;
6040 } else {
6041 ++if_stack->if_succeeded;
6042 output_line_command (ip, &outbuf, 1, same_file);
6043 }
6044 }
6045
6046 /*
6047 * skip to #endif, #else, or #elif. adjust line numbers, etc.
6048 * leaves input ptr at the sharp sign found.
6049 * If ANY is nonzero, return at next directive of any sort.
6050 */
6051 static void
6052 skip_if_group (ip, any)
6053 FILE_BUF *ip;
6054 int any;
6055 {
6056 register U_CHAR *bp = ip->bufp, *cp;
6057 register U_CHAR *endb = ip->buf + ip->length;
6058 struct directive *kt;
6059 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
6060 U_CHAR *beg_of_line = bp;
6061 register int ident_length;
6062 U_CHAR *ident, *after_ident;
6063
6064 while (bp < endb) {
6065 switch (*bp++) {
6066 case '/': /* possible comment */
6067 if (*bp == '\\' && bp[1] == '\n')
6068 newline_fix (bp);
6069 if (*bp == '*'
6070 || ((cplusplus || objc) && *bp == '/')) {
6071 ip->bufp = ++bp;
6072 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
6073 }
6074 break;
6075 case '\"':
6076 case '\'':
6077 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
6078 break;
6079 case '\\':
6080 /* Char after backslash loses its special meaning. */
6081 if (bp < endb) {
6082 if (*bp == '\n')
6083 ++ip->lineno; /* But do update the line-count. */
6084 bp++;
6085 }
6086 break;
6087 case '\n':
6088 ++ip->lineno;
6089 beg_of_line = bp;
6090 break;
6091 case '#':
6092 ip->bufp = bp - 1;
6093
6094 /* # keyword: a # must be first nonblank char on the line */
6095 if (beg_of_line == 0)
6096 break;
6097 /* Scan from start of line, skipping whitespace, comments
6098 and backslash-newlines, and see if we reach this #.
6099 If not, this # is not special. */
6100 bp = beg_of_line;
6101 while (1) {
6102 if (is_hor_space[*bp])
6103 bp++;
6104 else if (*bp == '\\' && bp[1] == '\n')
6105 bp += 2;
6106 else if (*bp == '/' && bp[1] == '*') {
6107 bp += 2;
6108 while (!(*bp == '*' && bp[1] == '/'))
6109 bp++;
6110 bp += 2;
6111 } else if ((cplusplus || objc) && *bp == '/' && bp[1] == '/') {
6112 bp += 2;
6113 while (*bp++ != '\n') ;
6114 }
6115 else break;
6116 }
6117 if (bp != ip->bufp) {
6118 bp = ip->bufp + 1; /* Reset bp to after the #. */
6119 break;
6120 }
6121
6122 bp = ip->bufp + 1; /* Point after the '#' */
6123
6124 /* Skip whitespace and \-newline. */
6125 while (1) {
6126 if (is_hor_space[*bp])
6127 bp++;
6128 else if (*bp == '\\' && bp[1] == '\n')
6129 bp += 2;
6130 else if (*bp == '/' && bp[1] == '*') {
6131 bp += 2;
6132 while (!(*bp == '*' && bp[1] == '/')) {
6133 if (*bp == '\n')
6134 ip->lineno++;
6135 bp++;
6136 }
6137 bp += 2;
6138 } else if ((cplusplus || objc) && *bp == '/' && bp[1] == '/') {
6139 bp += 2;
6140 while (*bp++ != '\n') ;
6141 }
6142 else break;
6143 }
6144
6145 cp = bp;
6146
6147 /* Now find end of directive name.
6148 If we encounter a backslash-newline, exchange it with any following
6149 symbol-constituents so that we end up with a contiguous name. */
6150
6151 while (1) {
6152 if (is_idchar[*bp])
6153 bp++;
6154 else {
6155 if (*bp == '\\' && bp[1] == '\n')
6156 name_newline_fix (bp);
6157 if (is_idchar[*bp])
6158 bp++;
6159 else break;
6160 }
6161 }
6162 ident_length = bp - cp;
6163 ident = cp;
6164 after_ident = bp;
6165
6166 /* A line of just `#' becomes blank. */
6167
6168 if (ident_length == 0 && *after_ident == '\n') {
6169 continue;
6170 }
6171
6172 if (ident_length == 0 || !is_idstart[*ident]) {
6173 U_CHAR *p = ident;
6174 while (is_idchar[*p]) {
6175 if (*p < '0' || *p > '9')
6176 break;
6177 p++;
6178 }
6179 /* Handle # followed by a line number. */
6180 if (p != ident && !is_idchar[*p]) {
6181 if (pedantic)
6182 pedwarn ("`#' followed by integer");
6183 continue;
6184 }
6185
6186 /* Avoid error for `###' and similar cases unless -pedantic. */
6187 if (p == ident) {
6188 while (*p == '#' || is_hor_space[*p]) p++;
6189 if (*p == '\n') {
6190 if (pedantic && !lang_asm)
6191 pedwarn ("invalid preprocessor directive");
6192 continue;
6193 }
6194 }
6195
6196 if (!lang_asm && pedantic)
6197 pedwarn ("invalid preprocessor directive name");
6198 continue;
6199 }
6200
6201 for (kt = directive_table; kt->length >= 0; kt++) {
6202 IF_STACK_FRAME *temp;
6203 if (ident_length == kt->length
6204 && strncmp (cp, kt->name, kt->length) == 0) {
6205 /* If we are asked to return on next directive, do so now. */
6206 if (any)
6207 return;
6208
6209 switch (kt->type) {
6210 case T_IF:
6211 case T_IFDEF:
6212 case T_IFNDEF:
6213 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
6214 temp->next = if_stack;
6215 if_stack = temp;
6216 temp->lineno = ip->lineno;
6217 temp->fname = ip->nominal_fname;
6218 temp->type = kt->type;
6219 break;
6220 case T_ELSE:
6221 case T_ENDIF:
6222 if (pedantic && if_stack != save_if_stack)
6223 validate_else (bp);
6224 case T_ELIF:
6225 if (if_stack == instack[indepth].if_stack) {
6226 error ("`#%s' not within a conditional", kt->name);
6227 break;
6228 }
6229 else if (if_stack == save_if_stack)
6230 return; /* found what we came for */
6231
6232 if (kt->type != T_ENDIF) {
6233 if (if_stack->type == T_ELSE)
6234 error ("`#else' or `#elif' after `#else'");
6235 if_stack->type = kt->type;
6236 break;
6237 }
6238
6239 temp = if_stack;
6240 if_stack = if_stack->next;
6241 free (temp);
6242 break;
6243 }
6244 break;
6245 }
6246 }
6247 /* Don't let erroneous code go by. */
6248 if (kt->length < 0 && !lang_asm && pedantic)
6249 pedwarn ("invalid preprocessor directive name");
6250 }
6251 }
6252 ip->bufp = bp;
6253 /* after this returns, rescan will exit because ip->bufp
6254 now points to the end of the buffer.
6255 rescan is responsible for the error message also. */
6256 }
6257
6258 /*
6259 * handle a #else directive. Do this by just continuing processing
6260 * without changing if_stack ; this is so that the error message
6261 * for missing #endif's etc. will point to the original #if. It
6262 * is possible that something different would be better.
6263 */
6264
6265 static int
6266 do_else (buf, limit, op, keyword)
6267 U_CHAR *buf, *limit;
6268 FILE_BUF *op;
6269 struct directive *keyword;
6270 {
6271 FILE_BUF *ip = &instack[indepth];
6272
6273 if (pedantic) {
6274 SKIP_WHITE_SPACE (buf);
6275 if (buf != limit)
6276 pedwarn ("text following `#else' violates ANSI standard");
6277 }
6278
6279 if (if_stack == instack[indepth].if_stack) {
6280 error ("`#else' not within a conditional");
6281 return 0;
6282 } else {
6283 /* #ifndef can't have its special treatment for containing the whole file
6284 if it has a #else clause. */
6285 if_stack->control_macro = 0;
6286
6287 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
6288 error ("`#else' after `#else'");
6289 fprintf (stderr, " (matches line %d", if_stack->lineno);
6290 if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
6291 fprintf (stderr, ", file %s", if_stack->fname);
6292 fprintf (stderr, ")\n");
6293 }
6294 if_stack->type = T_ELSE;
6295 }
6296
6297 if (if_stack->if_succeeded)
6298 skip_if_group (ip, 0);
6299 else {
6300 ++if_stack->if_succeeded; /* continue processing input */
6301 output_line_command (ip, op, 1, same_file);
6302 }
6303 return 0;
6304 }
6305
6306 /*
6307 * unstack after #endif command
6308 */
6309
6310 static int
6311 do_endif (buf, limit, op, keyword)
6312 U_CHAR *buf, *limit;
6313 FILE_BUF *op;
6314 struct directive *keyword;
6315 {
6316 if (pedantic) {
6317 SKIP_WHITE_SPACE (buf);
6318 if (buf != limit)
6319 pedwarn ("text following `#endif' violates ANSI standard");
6320 }
6321
6322 if (if_stack == instack[indepth].if_stack)
6323 error ("unbalanced `#endif'");
6324 else {
6325 IF_STACK_FRAME *temp = if_stack;
6326 if_stack = if_stack->next;
6327 if (temp->control_macro != 0) {
6328 /* This #endif matched a #ifndef at the start of the file.
6329 See if it is at the end of the file. */
6330 FILE_BUF *ip = &instack[indepth];
6331 U_CHAR *p = ip->bufp;
6332 U_CHAR *ep = ip->buf + ip->length;
6333
6334 while (p != ep) {
6335 U_CHAR c = *p++;
6336 switch (c) {
6337 case ' ':
6338 case '\t':
6339 case '\n':
6340 break;
6341 case '/':
6342 if (p != ep && *p == '*') {
6343 /* Skip this comment. */
6344 int junk;
6345 U_CHAR *save_bufp = ip->bufp;
6346 ip->bufp = p + 1;
6347 p = skip_to_end_of_comment (ip, &junk, 1);
6348 ip->bufp = save_bufp;
6349 }
6350 break;
6351 default:
6352 goto fail;
6353 }
6354 }
6355 /* If we get here, this #endif ends a #ifndef
6356 that contains all of the file (aside from whitespace).
6357 Arrange not to include the file again
6358 if the macro that was tested is defined. */
6359 if (indepth != 0)
6360 record_control_macro (ip->fname, temp->control_macro);
6361 fail: ;
6362 }
6363 free (temp);
6364 output_line_command (&instack[indepth], op, 1, same_file);
6365 }
6366 return 0;
6367 }
6368
6369 /* When an #else or #endif is found while skipping failed conditional,
6370 if -pedantic was specified, this is called to warn about text after
6371 the command name. P points to the first char after the command name. */
6372
6373 static void
6374 validate_else (p)
6375 register U_CHAR *p;
6376 {
6377 /* Advance P over whitespace and comments. */
6378 while (1) {
6379 if (*p == '\\' && p[1] == '\n')
6380 p += 2;
6381 if (is_hor_space[*p])
6382 p++;
6383 else if (*p == '/') {
6384 if (p[1] == '\\' && p[2] == '\n')
6385 newline_fix (p + 1);
6386 if (p[1] == '*') {
6387 p += 2;
6388 /* Don't bother warning about unterminated comments
6389 since that will happen later. Just be sure to exit. */
6390 while (*p) {
6391 if (p[1] == '\\' && p[2] == '\n')
6392 newline_fix (p + 1);
6393 if (*p == '*' && p[1] == '/') {
6394 p += 2;
6395 break;
6396 }
6397 p++;
6398 }
6399 }
6400 else if ((cplusplus || objc) && p[1] == '/') {
6401 p += 2;
6402 while (*p && *p++ != '\n') ;
6403 }
6404 } else break;
6405 }
6406 if (*p && *p != '\n')
6407 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
6408 }
6409 \f
6410 /* Skip a comment, assuming the input ptr immediately follows the
6411 initial slash-star. Bump *LINE_COUNTER for each newline.
6412 (The canonical line counter is &ip->lineno.)
6413 Don't use this routine (or the next one) if bumping the line
6414 counter is not sufficient to deal with newlines in the string.
6415
6416 If NOWARN is nonzero, don't warn about slash-star inside a comment.
6417 This feature is useful when processing a comment that is going to be
6418 processed or was processed at another point in the preprocessor,
6419 to avoid a duplicate warning. */
6420 static U_CHAR *
6421 skip_to_end_of_comment (ip, line_counter, nowarn)
6422 register FILE_BUF *ip;
6423 int *line_counter; /* place to remember newlines, or NULL */
6424 int nowarn;
6425 {
6426 register U_CHAR *limit = ip->buf + ip->length;
6427 register U_CHAR *bp = ip->bufp;
6428 FILE_BUF *op = &outbuf; /* JF */
6429 int output = put_out_comments && !line_counter;
6430
6431 /* JF this line_counter stuff is a crock to make sure the
6432 comment is only put out once, no matter how many times
6433 the comment is skipped. It almost works */
6434 if (output) {
6435 *op->bufp++ = '/';
6436 *op->bufp++ = '*';
6437 }
6438 if ((cplusplus || objc) && bp[-1] == '/') {
6439 if (output) {
6440 while (bp < limit)
6441 if ((*op->bufp++ = *bp++) == '\n') {
6442 bp--;
6443 break;
6444 }
6445 op->bufp[-1] = '*';
6446 *op->bufp++ = '/';
6447 *op->bufp++ = '\n';
6448 } else {
6449 while (bp < limit) {
6450 if (*bp++ == '\n') {
6451 bp--;
6452 break;
6453 }
6454 }
6455 }
6456 ip->bufp = bp;
6457 return bp;
6458 }
6459 while (bp < limit) {
6460 if (output)
6461 *op->bufp++ = *bp;
6462 switch (*bp++) {
6463 case '/':
6464 if (warn_comments && !nowarn && bp < limit && *bp == '*')
6465 warning ("`/*' within comment");
6466 break;
6467 case '\n':
6468 if (line_counter != NULL)
6469 ++*line_counter;
6470 if (output)
6471 ++op->lineno;
6472 break;
6473 case '*':
6474 if (*bp == '\\' && bp[1] == '\n')
6475 newline_fix (bp);
6476 if (*bp == '/') {
6477 if (output)
6478 *op->bufp++ = '/';
6479 ip->bufp = ++bp;
6480 return bp;
6481 }
6482 break;
6483 }
6484 }
6485 ip->bufp = bp;
6486 return bp;
6487 }
6488
6489 /*
6490 * Skip over a quoted string. BP points to the opening quote.
6491 * Returns a pointer after the closing quote. Don't go past LIMIT.
6492 * START_LINE is the line number of the starting point (but it need
6493 * not be valid if the starting point is inside a macro expansion).
6494 *
6495 * The input stack state is not changed.
6496 *
6497 * If COUNT_NEWLINES is nonzero, it points to an int to increment
6498 * for each newline passed.
6499 *
6500 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
6501 * if we pass a backslash-newline.
6502 *
6503 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
6504 */
6505 static U_CHAR *
6506 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
6507 register U_CHAR *bp;
6508 register U_CHAR *limit;
6509 int start_line;
6510 int *count_newlines;
6511 int *backslash_newlines_p;
6512 int *eofp;
6513 {
6514 register U_CHAR c, match;
6515
6516 match = *bp++;
6517 while (1) {
6518 if (bp >= limit) {
6519 error_with_line (line_for_error (start_line),
6520 "unterminated string or character constant");
6521 if (eofp)
6522 *eofp = 1;
6523 break;
6524 }
6525 c = *bp++;
6526 if (c == '\\') {
6527 while (*bp == '\\' && bp[1] == '\n') {
6528 if (backslash_newlines_p)
6529 *backslash_newlines_p = 1;
6530 if (count_newlines)
6531 ++*count_newlines;
6532 bp += 2;
6533 }
6534 if (*bp == '\n' && count_newlines) {
6535 if (backslash_newlines_p)
6536 *backslash_newlines_p = 1;
6537 ++*count_newlines;
6538 }
6539 bp++;
6540 } else if (c == '\n') {
6541 if (traditional) {
6542 /* Unterminated strings and character constants are 'legal'. */
6543 bp--; /* Don't consume the newline. */
6544 if (eofp)
6545 *eofp = 1;
6546 break;
6547 }
6548 if (match == '\'') {
6549 error_with_line (line_for_error (start_line),
6550 "unterminated character constant");
6551 bp--;
6552 if (eofp)
6553 *eofp = 1;
6554 break;
6555 }
6556 if (traditional) { /* Unterminated strings are 'legal'. */
6557 if (eofp)
6558 *eofp = 1;
6559 break;
6560 }
6561 /* If not traditional, then allow newlines inside strings. */
6562 if (count_newlines)
6563 ++*count_newlines;
6564 } else if (c == match)
6565 break;
6566 }
6567 return bp;
6568 }
6569
6570 /* Skip across a group of balanced parens, starting from IP->bufp.
6571 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
6572
6573 This does not handle newlines, because it's used for the arg of #if,
6574 where there aren't any newlines. Also, backslash-newline can't appear. */
6575
6576 static U_CHAR *
6577 skip_paren_group (ip)
6578 register FILE_BUF *ip;
6579 {
6580 U_CHAR *limit = ip->buf + ip->length;
6581 U_CHAR *p = ip->bufp;
6582 int depth = 0;
6583 int lines_dummy = 0;
6584
6585 while (p != limit) {
6586 int c = *p++;
6587 switch (c) {
6588 case '(':
6589 depth++;
6590 break;
6591
6592 case ')':
6593 depth--;
6594 if (depth == 0)
6595 return ip->bufp = p;
6596 break;
6597
6598 case '/':
6599 if (*p == '*') {
6600 ip->bufp = p;
6601 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
6602 p = ip->bufp;
6603 }
6604
6605 case '"':
6606 case '\'':
6607 {
6608 int eofp = 0;
6609 p = skip_quoted_string (p - 1, limit, 0, 0, 0, &eofp);
6610 if (eofp)
6611 return ip->bufp = p;
6612 }
6613 break;
6614 }
6615 }
6616
6617 ip->bufp = p;
6618 return p;
6619 }
6620 \f
6621 /*
6622 * write out a #line command, for instance, after an #include file.
6623 * If CONDITIONAL is nonzero, we can omit the #line if it would
6624 * appear to be a no-op, and we can output a few newlines instead
6625 * if we want to increase the line number by a small amount.
6626 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
6627 */
6628
6629 static void
6630 output_line_command (ip, op, conditional, file_change)
6631 FILE_BUF *ip, *op;
6632 int conditional;
6633 enum file_change_code file_change;
6634 {
6635 int len;
6636 char line_cmd_buf[500];
6637
6638 if (no_line_commands
6639 || ip->fname == NULL
6640 || no_output) {
6641 op->lineno = ip->lineno;
6642 return;
6643 }
6644
6645 if (conditional) {
6646 if (ip->lineno == op->lineno)
6647 return;
6648
6649 /* If the inherited line number is a little too small,
6650 output some newlines instead of a #line command. */
6651 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
6652 check_expand (op, 10);
6653 while (ip->lineno > op->lineno) {
6654 *op->bufp++ = '\n';
6655 op->lineno++;
6656 }
6657 return;
6658 }
6659 }
6660
6661 /* Don't output a line number of 0 if we can help it. */
6662 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
6663 && *ip->bufp == '\n') {
6664 ip->lineno++;
6665 ip->bufp++;
6666 }
6667
6668 #ifdef OUTPUT_LINE_COMMANDS
6669 sprintf (line_cmd_buf, "#line %d \"%s\"", ip->lineno, ip->nominal_fname);
6670 #else
6671 sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->nominal_fname);
6672 #endif
6673 if (file_change != same_file)
6674 strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
6675 /* Tell cc1 if following text comes from a system header file. */
6676 if (ip->system_header_p)
6677 strcat (line_cmd_buf, " 3");
6678 len = strlen (line_cmd_buf);
6679 line_cmd_buf[len++] = '\n';
6680 check_expand (op, len + 1);
6681 if (op->bufp > op->buf && op->bufp[-1] != '\n')
6682 *op->bufp++ = '\n';
6683 bcopy (line_cmd_buf, op->bufp, len);
6684 op->bufp += len;
6685 op->lineno = ip->lineno;
6686 }
6687 \f
6688 /* This structure represents one parsed argument in a macro call.
6689 `raw' points to the argument text as written (`raw_length' is its length).
6690 `expanded' points to the argument's macro-expansion
6691 (its length is `expand_length').
6692 `stringified_length' is the length the argument would have
6693 if stringified.
6694 `use_count' is the number of times this macro arg is substituted
6695 into the macro. If the actual use count exceeds 10,
6696 the value stored is 10.
6697 `free1' and `free2', if nonzero, point to blocks to be freed
6698 when the macro argument data is no longer needed. */
6699
6700 struct argdata {
6701 U_CHAR *raw, *expanded;
6702 int raw_length, expand_length;
6703 int stringified_length;
6704 U_CHAR *free1, *free2;
6705 char newlines;
6706 char comments;
6707 char use_count;
6708 };
6709
6710 /* Expand a macro call.
6711 HP points to the symbol that is the macro being called.
6712 Put the result of expansion onto the input stack
6713 so that subsequent input by our caller will use it.
6714
6715 If macro wants arguments, caller has already verified that
6716 an argument list follows; arguments come from the input stack. */
6717
6718 static void
6719 macroexpand (hp, op)
6720 HASHNODE *hp;
6721 FILE_BUF *op;
6722 {
6723 int nargs;
6724 DEFINITION *defn = hp->value.defn;
6725 register U_CHAR *xbuf;
6726 int xbuf_len;
6727 int start_line = instack[indepth].lineno;
6728
6729 CHECK_DEPTH (return;);
6730
6731 /* it might not actually be a macro. */
6732 if (hp->type != T_MACRO) {
6733 special_symbol (hp, op);
6734 return;
6735 }
6736
6737 /* This macro is being used inside a #if, which means it must be */
6738 /* recorded as a precondition. */
6739 if (pcp_inside_if && pcp_outfile && defn->predefined)
6740 dump_single_macro (hp, pcp_outfile);
6741
6742 nargs = defn->nargs;
6743
6744 if (nargs >= 0) {
6745 register int i;
6746 struct argdata *args;
6747 char *parse_error = 0;
6748
6749 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
6750
6751 for (i = 0; i < nargs; i++) {
6752 args[i].raw = args[i].expanded = (U_CHAR *) "";
6753 args[i].raw_length = args[i].expand_length
6754 = args[i].stringified_length = 0;
6755 args[i].free1 = args[i].free2 = 0;
6756 args[i].use_count = 0;
6757 }
6758
6759 /* Parse all the macro args that are supplied. I counts them.
6760 The first NARGS args are stored in ARGS.
6761 The rest are discarded. */
6762 i = 0;
6763 do {
6764 /* Discard the open-parenthesis or comma before the next arg. */
6765 ++instack[indepth].bufp;
6766 parse_error
6767 = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
6768 if (parse_error) {
6769 error_with_line (line_for_error (start_line), parse_error);
6770 break;
6771 }
6772 i++;
6773 } while (*instack[indepth].bufp != ')');
6774
6775 /* If we got one arg but it was just whitespace, call that 0 args. */
6776 if (i == 1) {
6777 register U_CHAR *bp = args[0].raw;
6778 register U_CHAR *lim = bp + args[0].raw_length;
6779 while (bp != lim && is_space[*bp]) bp++;
6780 if (bp == lim)
6781 i = 0;
6782 }
6783
6784 if (nargs == 0 && i > 0)
6785 error ("arguments given to macro `%s'", hp->name);
6786 else if (i < nargs) {
6787 /* traditional C allows foo() if foo wants one argument. */
6788 if (nargs == 1 && i == 0 && traditional)
6789 ;
6790 else if (i == 0)
6791 error ("macro `%s' used without args", hp->name);
6792 else if (i == 1)
6793 error ("macro `%s' used with just one arg", hp->name);
6794 else
6795 error ("macro `%s' used with only %d args", hp->name, i);
6796 } else if (i > nargs)
6797 error ("macro `%s' used with too many (%d) args", hp->name, i);
6798
6799 /* Swallow the closeparen. */
6800 ++instack[indepth].bufp;
6801
6802 /* If macro wants zero args, we parsed the arglist for checking only.
6803 Read directly from the macro definition. */
6804 if (nargs == 0) {
6805 xbuf = defn->expansion;
6806 xbuf_len = defn->length;
6807 } else {
6808 register U_CHAR *exp = defn->expansion;
6809 register int offset; /* offset in expansion,
6810 copied a piece at a time */
6811 register int totlen; /* total amount of exp buffer filled so far */
6812
6813 register struct reflist *ap;
6814
6815 /* Macro really takes args. Compute the expansion of this call. */
6816
6817 /* Compute length in characters of the macro's expansion.
6818 Also count number of times each arg is used. */
6819 xbuf_len = defn->length;
6820 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
6821 if (ap->stringify)
6822 xbuf_len += args[ap->argno].stringified_length;
6823 else if (ap->raw_before || ap->raw_after || traditional)
6824 xbuf_len += args[ap->argno].raw_length;
6825 else
6826 xbuf_len += args[ap->argno].expand_length;
6827
6828 if (args[ap->argno].use_count < 10)
6829 args[ap->argno].use_count++;
6830 }
6831
6832 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
6833
6834 /* Generate in XBUF the complete expansion
6835 with arguments substituted in.
6836 TOTLEN is the total size generated so far.
6837 OFFSET is the index in the definition
6838 of where we are copying from. */
6839 offset = totlen = 0;
6840 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
6841 register struct argdata *arg = &args[ap->argno];
6842
6843 for (i = 0; i < ap->nchars; i++)
6844 xbuf[totlen++] = exp[offset++];
6845
6846 if (ap->stringify != 0) {
6847 int arglen = arg->raw_length;
6848 int escaped = 0;
6849 int in_string = 0;
6850 int c;
6851 i = 0;
6852 while (i < arglen
6853 && (c = arg->raw[i], is_space[c]))
6854 i++;
6855 while (i < arglen
6856 && (c = arg->raw[arglen - 1], is_space[c]))
6857 arglen--;
6858 if (!traditional)
6859 xbuf[totlen++] = '\"'; /* insert beginning quote */
6860 for (; i < arglen; i++) {
6861 c = arg->raw[i];
6862
6863 /* Special markers Newline Space
6864 generate nothing for a stringified argument. */
6865 if (c == '\n' && arg->raw[i+1] != '\n') {
6866 i++;
6867 continue;
6868 }
6869
6870 /* Internal sequences of whitespace are replaced by one space
6871 except within an string or char token. */
6872 if (! in_string
6873 && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
6874 while (1) {
6875 /* Note that Newline Space does occur within whitespace
6876 sequences; consider it part of the sequence. */
6877 if (c == '\n' && is_space[arg->raw[i+1]])
6878 i += 2;
6879 else if (c != '\n' && is_space[c])
6880 i++;
6881 else break;
6882 c = arg->raw[i];
6883 }
6884 i--;
6885 c = ' ';
6886 }
6887
6888 if (escaped)
6889 escaped = 0;
6890 else {
6891 if (c == '\\')
6892 escaped = 1;
6893 if (in_string) {
6894 if (c == in_string)
6895 in_string = 0;
6896 } else if (c == '\"' || c == '\'')
6897 in_string = c;
6898 }
6899
6900 /* Escape these chars */
6901 if (c == '\"' || (in_string && c == '\\'))
6902 xbuf[totlen++] = '\\';
6903 if (isprint (c))
6904 xbuf[totlen++] = c;
6905 else {
6906 sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
6907 totlen += 4;
6908 }
6909 }
6910 if (!traditional)
6911 xbuf[totlen++] = '\"'; /* insert ending quote */
6912 } else if (ap->raw_before || ap->raw_after || traditional) {
6913 U_CHAR *p1 = arg->raw;
6914 U_CHAR *l1 = p1 + arg->raw_length;
6915 if (ap->raw_before) {
6916 while (p1 != l1 && is_space[*p1]) p1++;
6917 while (p1 != l1 && is_idchar[*p1])
6918 xbuf[totlen++] = *p1++;
6919 /* Delete any no-reexpansion marker that follows
6920 an identifier at the beginning of the argument
6921 if the argument is concatenated with what precedes it. */
6922 if (p1[0] == '\n' && p1[1] == '-')
6923 p1 += 2;
6924 }
6925 if (ap->raw_after) {
6926 /* Arg is concatenated after: delete trailing whitespace,
6927 whitespace markers, and no-reexpansion markers. */
6928 while (p1 != l1) {
6929 if (is_space[l1[-1]]) l1--;
6930 else if (l1[-1] == '-') {
6931 U_CHAR *p2 = l1 - 1;
6932 /* If a `-' is preceded by an odd number of newlines then it
6933 and the last newline are a no-reexpansion marker. */
6934 while (p2 != p1 && p2[-1] == '\n') p2--;
6935 if ((l1 - 1 - p2) & 1) {
6936 l1 -= 2;
6937 }
6938 else break;
6939 }
6940 else break;
6941 }
6942 }
6943 bcopy (p1, xbuf + totlen, l1 - p1);
6944 totlen += l1 - p1;
6945 } else {
6946 bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
6947 totlen += arg->expand_length;
6948 /* If a macro argument with newlines is used multiple times,
6949 then only expand the newlines once. This avoids creating output
6950 lines which don't correspond to any input line, which confuses
6951 gdb and gcov. */
6952 if (arg->use_count > 1 && arg->newlines > 0) {
6953 /* Don't bother doing delete_newlines for subsequent
6954 uses of arg. */
6955 arg->use_count = 1;
6956 arg->expand_length
6957 = delete_newlines (arg->expanded, arg->expand_length);
6958 }
6959 }
6960
6961 if (totlen > xbuf_len)
6962 abort ();
6963 }
6964
6965 /* if there is anything left of the definition
6966 after handling the arg list, copy that in too. */
6967
6968 for (i = offset; i < defn->length; i++)
6969 xbuf[totlen++] = exp[i];
6970
6971 xbuf[totlen] = 0;
6972 xbuf_len = totlen;
6973
6974 for (i = 0; i < nargs; i++) {
6975 if (args[i].free1 != 0)
6976 free (args[i].free1);
6977 if (args[i].free2 != 0)
6978 free (args[i].free2);
6979 }
6980 }
6981 } else {
6982 xbuf = defn->expansion;
6983 xbuf_len = defn->length;
6984 }
6985
6986 /* Now put the expansion on the input stack
6987 so our caller will commence reading from it. */
6988 {
6989 register FILE_BUF *ip2;
6990
6991 ip2 = &instack[++indepth];
6992
6993 ip2->fname = 0;
6994 ip2->nominal_fname = 0;
6995 ip2->lineno = 0;
6996 ip2->buf = xbuf;
6997 ip2->length = xbuf_len;
6998 ip2->bufp = xbuf;
6999 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
7000 ip2->macro = hp;
7001 ip2->if_stack = if_stack;
7002 ip2->system_header_p = 0;
7003
7004 /* Recursive macro use sometimes works traditionally.
7005 #define foo(x,y) bar(x(y,0), y)
7006 foo(foo, baz) */
7007
7008 if (!traditional)
7009 hp->type = T_DISABLED;
7010 }
7011 }
7012 \f
7013 /*
7014 * Parse a macro argument and store the info on it into *ARGPTR.
7015 * Return nonzero to indicate a syntax error.
7016 */
7017
7018 static char *
7019 macarg (argptr)
7020 register struct argdata *argptr;
7021 {
7022 FILE_BUF *ip = &instack[indepth];
7023 int paren = 0;
7024 int newlines = 0;
7025 int comments = 0;
7026
7027 /* Try to parse as much of the argument as exists at this
7028 input stack level. */
7029 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
7030 &paren, &newlines, &comments);
7031
7032 /* If we find the end of the argument at this level,
7033 set up *ARGPTR to point at it in the input stack. */
7034 if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
7035 && bp != ip->buf + ip->length) {
7036 if (argptr != 0) {
7037 argptr->raw = ip->bufp;
7038 argptr->raw_length = bp - ip->bufp;
7039 argptr->newlines = newlines;
7040 }
7041 ip->bufp = bp;
7042 } else {
7043 /* This input stack level ends before the macro argument does.
7044 We must pop levels and keep parsing.
7045 Therefore, we must allocate a temporary buffer and copy
7046 the macro argument into it. */
7047 int bufsize = bp - ip->bufp;
7048 int extra = newlines;
7049 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
7050 int final_start = 0;
7051
7052 bcopy (ip->bufp, buffer, bufsize);
7053 ip->bufp = bp;
7054 ip->lineno += newlines;
7055
7056 while (bp == ip->buf + ip->length) {
7057 if (instack[indepth].macro == 0) {
7058 free (buffer);
7059 return "unterminated macro call";
7060 }
7061 ip->macro->type = T_MACRO;
7062 if (ip->free_ptr)
7063 free (ip->free_ptr);
7064 ip = &instack[--indepth];
7065 newlines = 0;
7066 comments = 0;
7067 bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
7068 &newlines, &comments);
7069 final_start = bufsize;
7070 bufsize += bp - ip->bufp;
7071 extra += newlines;
7072 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
7073 bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
7074 ip->bufp = bp;
7075 ip->lineno += newlines;
7076 }
7077
7078 /* Now, if arg is actually wanted, record its raw form,
7079 discarding comments and duplicating newlines in whatever
7080 part of it did not come from a macro expansion.
7081 EXTRA space has been preallocated for duplicating the newlines.
7082 FINAL_START is the index of the start of that part. */
7083 if (argptr != 0) {
7084 argptr->raw = buffer;
7085 argptr->raw_length = bufsize;
7086 argptr->free1 = buffer;
7087 argptr->newlines = newlines;
7088 argptr->comments = comments;
7089 if ((newlines || comments) && ip->fname != 0)
7090 argptr->raw_length
7091 = final_start +
7092 discard_comments (argptr->raw + final_start,
7093 argptr->raw_length - final_start,
7094 newlines);
7095 argptr->raw[argptr->raw_length] = 0;
7096 if (argptr->raw_length > bufsize + extra)
7097 abort ();
7098 }
7099 }
7100
7101 /* If we are not discarding this argument,
7102 macroexpand it and compute its length as stringified.
7103 All this info goes into *ARGPTR. */
7104
7105 if (argptr != 0) {
7106 FILE_BUF obuf;
7107 register U_CHAR *buf, *lim;
7108 register int totlen;
7109
7110 obuf = expand_to_temp_buffer (argptr->raw,
7111 argptr->raw + argptr->raw_length,
7112 1, 0);
7113
7114 argptr->expanded = obuf.buf;
7115 argptr->expand_length = obuf.length;
7116 argptr->free2 = obuf.buf;
7117
7118 buf = argptr->raw;
7119 lim = buf + argptr->raw_length;
7120
7121 while (buf != lim && is_space[*buf])
7122 buf++;
7123 while (buf != lim && is_space[lim[-1]])
7124 lim--;
7125 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
7126 while (buf != lim) {
7127 register U_CHAR c = *buf++;
7128 totlen++;
7129 /* Internal sequences of whitespace are replaced by one space
7130 in most cases, but not always. So count all the whitespace
7131 in case we need to keep it all. */
7132 #if 0
7133 if (is_space[c])
7134 SKIP_ALL_WHITE_SPACE (buf);
7135 else
7136 #endif
7137 if (c == '\"' || c == '\\') /* escape these chars */
7138 totlen++;
7139 else if (!isprint (c))
7140 totlen += 3;
7141 }
7142 argptr->stringified_length = totlen;
7143 }
7144 return 0;
7145 }
7146 \f
7147 /* Scan text from START (inclusive) up to LIMIT (exclusive),
7148 counting parens in *DEPTHPTR,
7149 and return if reach LIMIT
7150 or before a `)' that would make *DEPTHPTR negative
7151 or before a comma when *DEPTHPTR is zero.
7152 Single and double quotes are matched and termination
7153 is inhibited within them. Comments also inhibit it.
7154 Value returned is pointer to stopping place.
7155
7156 Increment *NEWLINES each time a newline is passed.
7157 Set *COMMENTS to 1 if a comment is seen. */
7158
7159 static U_CHAR *
7160 macarg1 (start, limit, depthptr, newlines, comments)
7161 U_CHAR *start;
7162 register U_CHAR *limit;
7163 int *depthptr, *newlines, *comments;
7164 {
7165 register U_CHAR *bp = start;
7166
7167 while (bp < limit) {
7168 switch (*bp) {
7169 case '(':
7170 (*depthptr)++;
7171 break;
7172 case ')':
7173 if (--(*depthptr) < 0)
7174 return bp;
7175 break;
7176 case '\\':
7177 /* Traditionally, backslash makes following char not special. */
7178 if (bp + 1 < limit && traditional)
7179 {
7180 bp++;
7181 /* But count source lines anyway. */
7182 if (*bp == '\n')
7183 ++*newlines;
7184 }
7185 break;
7186 case '\n':
7187 ++*newlines;
7188 break;
7189 case '/':
7190 if (bp[1] == '\\' && bp[2] == '\n')
7191 newline_fix (bp + 1);
7192 if ((cplusplus || objc) && bp[1] == '/') {
7193 *comments = 1;
7194 bp += 2;
7195 while (bp < limit && *bp++ != '\n') ;
7196 ++*newlines;
7197 break;
7198 }
7199 if (bp[1] != '*' || bp + 1 >= limit)
7200 break;
7201 *comments = 1;
7202 bp += 2;
7203 while (bp + 1 < limit) {
7204 if (bp[0] == '*'
7205 && bp[1] == '\\' && bp[2] == '\n')
7206 newline_fix (bp + 1);
7207 if (bp[0] == '*' && bp[1] == '/')
7208 break;
7209 if (*bp == '\n') ++*newlines;
7210 bp++;
7211 }
7212 break;
7213 case '\'':
7214 case '\"':
7215 {
7216 int quotec;
7217 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
7218 if (*bp == '\\') {
7219 bp++;
7220 if (*bp == '\n')
7221 ++*newlines;
7222 while (*bp == '\\' && bp[1] == '\n') {
7223 bp += 2;
7224 }
7225 } else if (*bp == '\n') {
7226 ++*newlines;
7227 if (quotec == '\'')
7228 break;
7229 }
7230 }
7231 }
7232 break;
7233 case ',':
7234 if ((*depthptr) == 0)
7235 return bp;
7236 break;
7237 }
7238 bp++;
7239 }
7240
7241 return bp;
7242 }
7243 \f
7244 /* Discard comments and duplicate newlines
7245 in the string of length LENGTH at START,
7246 except inside of string constants.
7247 The string is copied into itself with its beginning staying fixed.
7248
7249 NEWLINES is the number of newlines that must be duplicated.
7250 We assume that that much extra space is available past the end
7251 of the string. */
7252
7253 static int
7254 discard_comments (start, length, newlines)
7255 U_CHAR *start;
7256 int length;
7257 int newlines;
7258 {
7259 register U_CHAR *ibp;
7260 register U_CHAR *obp;
7261 register U_CHAR *limit;
7262 register int c;
7263
7264 /* If we have newlines to duplicate, copy everything
7265 that many characters up. Then, in the second part,
7266 we will have room to insert the newlines
7267 while copying down.
7268 NEWLINES may actually be too large, because it counts
7269 newlines in string constants, and we don't duplicate those.
7270 But that does no harm. */
7271 if (newlines > 0) {
7272 ibp = start + length;
7273 obp = ibp + newlines;
7274 limit = start;
7275 while (limit != ibp)
7276 *--obp = *--ibp;
7277 }
7278
7279 ibp = start + newlines;
7280 limit = start + length + newlines;
7281 obp = start;
7282
7283 while (ibp < limit) {
7284 *obp++ = c = *ibp++;
7285 switch (c) {
7286 case '\n':
7287 /* Duplicate the newline. */
7288 *obp++ = '\n';
7289 break;
7290
7291 case '\\':
7292 if (*ibp == '\n') {
7293 obp--;
7294 ibp++;
7295 }
7296 break;
7297
7298 case '/':
7299 if (*ibp == '\\' && ibp[1] == '\n')
7300 newline_fix (ibp);
7301 /* Delete any comment. */
7302 if ((cplusplus || objc) && ibp[0] == '/') {
7303 obp--;
7304 ibp++;
7305 while (ibp < limit && *ibp++ != '\n') ;
7306 break;
7307 }
7308 if (ibp[0] != '*' || ibp + 1 >= limit)
7309 break;
7310 obp--;
7311 ibp++;
7312 while (ibp + 1 < limit) {
7313 if (ibp[0] == '*'
7314 && ibp[1] == '\\' && ibp[2] == '\n')
7315 newline_fix (ibp + 1);
7316 if (ibp[0] == '*' && ibp[1] == '/')
7317 break;
7318 ibp++;
7319 }
7320 ibp += 2;
7321 break;
7322
7323 case '\'':
7324 case '\"':
7325 /* Notice and skip strings, so that we don't
7326 think that comments start inside them,
7327 and so we don't duplicate newlines in them. */
7328 {
7329 int quotec = c;
7330 while (ibp < limit) {
7331 *obp++ = c = *ibp++;
7332 if (c == quotec)
7333 break;
7334 if (c == '\n' && quotec == '\'')
7335 break;
7336 if (c == '\\' && ibp < limit) {
7337 while (*ibp == '\\' && ibp[1] == '\n')
7338 ibp += 2;
7339 *obp++ = *ibp++;
7340 }
7341 }
7342 }
7343 break;
7344 }
7345 }
7346
7347 return obp - start;
7348 }
7349 \f
7350 /* Delete newlines in the string of length LENGTH at START, except inside
7351 of string constants. The string is copied into itself with its beginning
7352 staying fixed. */
7353
7354 static int
7355 delete_newlines (start, length)
7356 U_CHAR *start;
7357 int length;
7358 {
7359 register U_CHAR *ibp;
7360 register U_CHAR *obp;
7361 register U_CHAR *limit;
7362 register int c;
7363
7364 ibp = start;
7365 limit = start + length;
7366 obp = start;
7367
7368 while (ibp < limit) {
7369 *obp++ = c = *ibp++;
7370 switch (c) {
7371 case '\n':
7372 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
7373 output. Skip past the newline and its duplicate. */
7374 if (*ibp == '\n')
7375 {
7376 ibp++;
7377 obp--;
7378 }
7379 break;
7380
7381 case '\'':
7382 case '\"':
7383 /* Notice and skip strings, so that we don't delete newlines in them. */
7384 {
7385 int quotec = c;
7386 while (ibp < limit) {
7387 *obp++ = c = *ibp++;
7388 if (c == quotec)
7389 break;
7390 if (c == '\n' && quotec == '\'')
7391 break;
7392 }
7393 }
7394 break;
7395 }
7396 }
7397
7398 return obp - start;
7399 }
7400 \f
7401 /*
7402 * error - print error message and increment count of errors.
7403 */
7404
7405 void
7406 error (msg, arg1, arg2, arg3)
7407 char *msg;
7408 {
7409 int i;
7410 FILE_BUF *ip = NULL;
7411
7412 print_containing_files ();
7413
7414 for (i = indepth; i >= 0; i--)
7415 if (instack[i].fname != NULL) {
7416 ip = &instack[i];
7417 break;
7418 }
7419
7420 if (ip != NULL)
7421 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
7422 fprintf (stderr, msg, arg1, arg2, arg3);
7423 fprintf (stderr, "\n");
7424 errors++;
7425 }
7426
7427 /* Error including a message from `errno'. */
7428
7429 static void
7430 error_from_errno (name)
7431 char *name;
7432 {
7433 int i;
7434 FILE_BUF *ip = NULL;
7435
7436 print_containing_files ();
7437
7438 for (i = indepth; i >= 0; i--)
7439 if (instack[i].fname != NULL) {
7440 ip = &instack[i];
7441 break;
7442 }
7443
7444 if (ip != NULL)
7445 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
7446
7447 if (errno < sys_nerr)
7448 fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
7449 else
7450 fprintf (stderr, "%s: undocumented I/O error\n", name);
7451
7452 errors++;
7453 }
7454
7455 /* Print error message but don't count it. */
7456
7457 void
7458 warning (msg, arg1, arg2, arg3)
7459 char *msg;
7460 {
7461 int i;
7462 FILE_BUF *ip = NULL;
7463
7464 if (inhibit_warnings)
7465 return;
7466
7467 if (warnings_are_errors)
7468 errors++;
7469
7470 print_containing_files ();
7471
7472 for (i = indepth; i >= 0; i--)
7473 if (instack[i].fname != NULL) {
7474 ip = &instack[i];
7475 break;
7476 }
7477
7478 if (ip != NULL)
7479 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
7480 fprintf (stderr, "warning: ");
7481 fprintf (stderr, msg, arg1, arg2, arg3);
7482 fprintf (stderr, "\n");
7483 }
7484
7485 static void
7486 error_with_line (line, msg, arg1, arg2, arg3)
7487 int line;
7488 char *msg;
7489 {
7490 int i;
7491 FILE_BUF *ip = NULL;
7492
7493 print_containing_files ();
7494
7495 for (i = indepth; i >= 0; i--)
7496 if (instack[i].fname != NULL) {
7497 ip = &instack[i];
7498 break;
7499 }
7500
7501 if (ip != NULL)
7502 fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
7503 fprintf (stderr, msg, arg1, arg2, arg3);
7504 fprintf (stderr, "\n");
7505 errors++;
7506 }
7507
7508 /* print an error message and maybe count it. */
7509
7510 void
7511 pedwarn (msg, arg1, arg2, arg3)
7512 char *msg;
7513 {
7514 if (pedantic_errors)
7515 error (msg, arg1, arg2, arg3);
7516 else
7517 warning (msg, arg1, arg2, arg3);
7518 }
7519
7520 /* Report a warning (or an error if pedantic_errors)
7521 giving specified file name and line number, not current. */
7522
7523 static void
7524 pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
7525 char *file;
7526 int line;
7527 char *msg;
7528 {
7529 int i;
7530 if (!pedantic_errors && inhibit_warnings)
7531 return;
7532 if (file != NULL)
7533 fprintf (stderr, "%s:%d: ", file, line);
7534 if (pedantic_errors || warnings_are_errors)
7535 errors++;
7536 if (!pedantic_errors)
7537 fprintf (stderr, "warning: ");
7538 fprintf (stderr, msg, arg1, arg2, arg3);
7539 fprintf (stderr, "\n");
7540 }
7541 \f
7542 /* Print the file names and line numbers of the #include
7543 commands which led to the current file. */
7544
7545 static void
7546 print_containing_files ()
7547 {
7548 FILE_BUF *ip = NULL;
7549 int i;
7550 int first = 1;
7551
7552 /* If stack of files hasn't changed since we last printed
7553 this info, don't repeat it. */
7554 if (last_error_tick == input_file_stack_tick)
7555 return;
7556
7557 for (i = indepth; i >= 0; i--)
7558 if (instack[i].fname != NULL) {
7559 ip = &instack[i];
7560 break;
7561 }
7562
7563 /* Give up if we don't find a source file. */
7564 if (ip == NULL)
7565 return;
7566
7567 /* Find the other, outer source files. */
7568 for (i--; i >= 0; i--)
7569 if (instack[i].fname != NULL) {
7570 ip = &instack[i];
7571 if (first) {
7572 first = 0;
7573 fprintf (stderr, "In file included");
7574 } else {
7575 fprintf (stderr, ",");
7576 }
7577
7578 fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
7579 }
7580 if (! first)
7581 fprintf (stderr, ":\n");
7582
7583 /* Record we have printed the status as of this time. */
7584 last_error_tick = input_file_stack_tick;
7585 }
7586 \f
7587 /* Return the line at which an error occurred.
7588 The error is not necessarily associated with the current spot
7589 in the input stack, so LINE says where. LINE will have been
7590 copied from ip->lineno for the current input level.
7591 If the current level is for a file, we return LINE.
7592 But if the current level is not for a file, LINE is meaningless.
7593 In that case, we return the lineno of the innermost file. */
7594
7595 static int
7596 line_for_error (line)
7597 int line;
7598 {
7599 int i;
7600 int line1 = line;
7601
7602 for (i = indepth; i >= 0; ) {
7603 if (instack[i].fname != 0)
7604 return line1;
7605 i--;
7606 if (i < 0)
7607 return 0;
7608 line1 = instack[i].lineno;
7609 }
7610 abort ();
7611 /*NOTREACHED*/
7612 return 0;
7613 }
7614
7615 /*
7616 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
7617 *
7618 * As things stand, nothing is ever placed in the output buffer to be
7619 * removed again except when it's KNOWN to be part of an identifier,
7620 * so flushing and moving down everything left, instead of expanding,
7621 * should work ok.
7622 */
7623
7624 /* You might think void was cleaner for the return type,
7625 but that would get type mismatch in check_expand in strict ANSI. */
7626 static int
7627 grow_outbuf (obuf, needed)
7628 register FILE_BUF *obuf;
7629 register int needed;
7630 {
7631 register U_CHAR *p;
7632 int minsize;
7633
7634 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
7635 return 0;
7636
7637 /* Make it at least twice as big as it is now. */
7638 obuf->length *= 2;
7639 /* Make it have at least 150% of the free space we will need. */
7640 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
7641 if (minsize > obuf->length)
7642 obuf->length = minsize;
7643
7644 if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
7645 memory_full ();
7646
7647 obuf->bufp = p + (obuf->bufp - obuf->buf);
7648 obuf->buf = p;
7649
7650 return 0;
7651 }
7652 \f
7653 /* Symbol table for macro names and special symbols */
7654
7655 /*
7656 * install a name in the main hash table, even if it is already there.
7657 * name stops with first non alphanumeric, except leading '#'.
7658 * caller must check against redefinition if that is desired.
7659 * delete_macro () removes things installed by install () in fifo order.
7660 * this is important because of the `defined' special symbol used
7661 * in #if, and also if pushdef/popdef directives are ever implemented.
7662 *
7663 * If LEN is >= 0, it is the length of the name.
7664 * Otherwise, compute the length by scanning the entire name.
7665 *
7666 * If HASH is >= 0, it is the precomputed hash code.
7667 * Otherwise, compute the hash code.
7668 */
7669 static HASHNODE *
7670 install (name, len, type, value, hash)
7671 U_CHAR *name;
7672 int len;
7673 enum node_type type;
7674 int value;
7675 int hash;
7676 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
7677 {
7678 register HASHNODE *hp;
7679 register int i, bucket;
7680 register U_CHAR *p, *q;
7681
7682 if (len < 0) {
7683 p = name;
7684 while (is_idchar[*p])
7685 p++;
7686 len = p - name;
7687 }
7688
7689 if (hash < 0)
7690 hash = hashf (name, len, HASHSIZE);
7691
7692 i = sizeof (HASHNODE) + len + 1;
7693 hp = (HASHNODE *) xmalloc (i);
7694 bucket = hash;
7695 hp->bucket_hdr = &hashtab[bucket];
7696 hp->next = hashtab[bucket];
7697 hashtab[bucket] = hp;
7698 hp->prev = NULL;
7699 if (hp->next != NULL)
7700 hp->next->prev = hp;
7701 hp->type = type;
7702 hp->length = len;
7703 hp->value.ival = value;
7704 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
7705 p = hp->name;
7706 q = name;
7707 for (i = 0; i < len; i++)
7708 *p++ = *q++;
7709 hp->name[len] = 0;
7710 return hp;
7711 }
7712
7713 /*
7714 * find the most recent hash node for name name (ending with first
7715 * non-identifier char) installed by install
7716 *
7717 * If LEN is >= 0, it is the length of the name.
7718 * Otherwise, compute the length by scanning the entire name.
7719 *
7720 * If HASH is >= 0, it is the precomputed hash code.
7721 * Otherwise, compute the hash code.
7722 */
7723 HASHNODE *
7724 lookup (name, len, hash)
7725 U_CHAR *name;
7726 int len;
7727 int hash;
7728 {
7729 register U_CHAR *bp;
7730 register HASHNODE *bucket;
7731
7732 if (len < 0) {
7733 for (bp = name; is_idchar[*bp]; bp++) ;
7734 len = bp - name;
7735 }
7736
7737 if (hash < 0)
7738 hash = hashf (name, len, HASHSIZE);
7739
7740 bucket = hashtab[hash];
7741 while (bucket) {
7742 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
7743 return bucket;
7744 bucket = bucket->next;
7745 }
7746 return NULL;
7747 }
7748
7749 /*
7750 * Delete a hash node. Some weirdness to free junk from macros.
7751 * More such weirdness will have to be added if you define more hash
7752 * types that need it.
7753 */
7754
7755 /* Note that the DEFINITION of a macro is removed from the hash table
7756 but its storage is not freed. This would be a storage leak
7757 except that it is not reasonable to keep undefining and redefining
7758 large numbers of macros many times.
7759 In any case, this is necessary, because a macro can be #undef'd
7760 in the middle of reading the arguments to a call to it.
7761 If #undef freed the DEFINITION, that would crash. */
7762
7763 static void
7764 delete_macro (hp)
7765 HASHNODE *hp;
7766 {
7767
7768 if (hp->prev != NULL)
7769 hp->prev->next = hp->next;
7770 if (hp->next != NULL)
7771 hp->next->prev = hp->prev;
7772
7773 /* make sure that the bucket chain header that
7774 the deleted guy was on points to the right thing afterwards. */
7775 if (hp == *hp->bucket_hdr)
7776 *hp->bucket_hdr = hp->next;
7777
7778 #if 0
7779 if (hp->type == T_MACRO) {
7780 DEFINITION *d = hp->value.defn;
7781 struct reflist *ap, *nextap;
7782
7783 for (ap = d->pattern; ap != NULL; ap = nextap) {
7784 nextap = ap->next;
7785 free (ap);
7786 }
7787 free (d);
7788 }
7789 #endif
7790 free (hp);
7791 }
7792
7793 /*
7794 * return hash function on name. must be compatible with the one
7795 * computed a step at a time, elsewhere
7796 */
7797 static int
7798 hashf (name, len, hashsize)
7799 register U_CHAR *name;
7800 register int len;
7801 int hashsize;
7802 {
7803 register int r = 0;
7804
7805 while (len--)
7806 r = HASHSTEP (r, *name++);
7807
7808 return MAKE_POS (r) % hashsize;
7809 }
7810 \f
7811
7812 /* Dump the definition of a single macro HP to OF. */
7813 static void
7814 dump_single_macro (hp, of)
7815 register HASHNODE *hp;
7816 FILE *of;
7817 {
7818 register DEFINITION *defn = hp->value.defn;
7819 struct reflist *ap;
7820 int offset;
7821 int concat;
7822
7823
7824 /* Print the definition of the macro HP. */
7825
7826 fprintf (of, "#define %s", hp->name);
7827
7828 if (defn->nargs >= 0) {
7829 int i;
7830
7831 fprintf (of, "(");
7832 for (i = 0; i < defn->nargs; i++) {
7833 dump_arg_n (defn, i, of);
7834 if (i + 1 < defn->nargs)
7835 fprintf (of, ", ");
7836 }
7837 fprintf (of, ")");
7838 }
7839
7840 fprintf (of, " ");
7841
7842 offset = 0;
7843 concat = 0;
7844 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
7845 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
7846 if (ap->nchars != 0)
7847 concat = 0;
7848 offset += ap->nchars;
7849 if (ap->stringify)
7850 fprintf (of, " #");
7851 if (ap->raw_before && !concat)
7852 fprintf (of, " ## ");
7853 concat = 0;
7854 dump_arg_n (defn, ap->argno, of);
7855 if (ap->raw_after) {
7856 fprintf (of, " ## ");
7857 concat = 1;
7858 }
7859 }
7860 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
7861 fprintf (of, "\n");
7862 }
7863
7864 /* Dump all macro definitions as #defines to stdout. */
7865
7866 static void
7867 dump_all_macros ()
7868 {
7869 int bucket;
7870
7871 for (bucket = 0; bucket < HASHSIZE; bucket++) {
7872 register HASHNODE *hp;
7873
7874 for (hp = hashtab[bucket]; hp; hp= hp->next) {
7875 if (hp->type == T_MACRO)
7876 dump_single_macro (hp, stdout);
7877 }
7878 }
7879 }
7880
7881 /* Output to OF a substring of a macro definition.
7882 BASE is the beginning of the definition.
7883 Output characters START thru LENGTH.
7884 Discard newlines outside of strings, thus
7885 converting funny-space markers to ordinary spaces. */
7886
7887 static void
7888 dump_defn_1 (base, start, length, of)
7889 U_CHAR *base;
7890 int start;
7891 int length;
7892 FILE *of;
7893 {
7894 U_CHAR *p = base + start;
7895 U_CHAR *limit = base + start + length;
7896
7897 while (p < limit) {
7898 if (*p != '\n')
7899 putc (*p, of);
7900 else if (*p == '\"' || *p =='\'') {
7901 U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
7902 fwrite (p, p1 - p, 1, of);
7903 p = p1 - 1;
7904 }
7905 p++;
7906 }
7907 }
7908
7909 /* Print the name of argument number ARGNUM of macro definition DEFN
7910 to OF.
7911 Recall that DEFN->args.argnames contains all the arg names
7912 concatenated in reverse order with comma-space in between. */
7913
7914 static void
7915 dump_arg_n (defn, argnum, of)
7916 DEFINITION *defn;
7917 int argnum;
7918 FILE *of;
7919 {
7920 register U_CHAR *p = defn->args.argnames;
7921 while (argnum + 1 < defn->nargs) {
7922 p = (U_CHAR *) index (p, ' ') + 1;
7923 argnum++;
7924 }
7925
7926 while (*p && *p != ',') {
7927 putc (*p, of);
7928 p++;
7929 }
7930 }
7931 \f
7932 /* Initialize syntactic classifications of characters. */
7933
7934 static void
7935 initialize_char_syntax ()
7936 {
7937 register int i;
7938
7939 /*
7940 * Set up is_idchar and is_idstart tables. These should be
7941 * faster than saying (is_alpha (c) || c == '_'), etc.
7942 * Set up these things before calling any routines tthat
7943 * refer to them.
7944 */
7945 for (i = 'a'; i <= 'z'; i++) {
7946 is_idchar[i - 'a' + 'A'] = 1;
7947 is_idchar[i] = 1;
7948 is_idstart[i - 'a' + 'A'] = 1;
7949 is_idstart[i] = 1;
7950 }
7951 for (i = '0'; i <= '9'; i++)
7952 is_idchar[i] = 1;
7953 is_idchar['_'] = 1;
7954 is_idstart['_'] = 1;
7955 is_idchar['$'] = dollars_in_ident;
7956 is_idstart['$'] = dollars_in_ident;
7957
7958 /* horizontal space table */
7959 is_hor_space[' '] = 1;
7960 is_hor_space['\t'] = 1;
7961 is_hor_space['\v'] = 1;
7962 is_hor_space['\f'] = 1;
7963 is_hor_space['\r'] = 1;
7964
7965 is_space[' '] = 1;
7966 is_space['\t'] = 1;
7967 is_space['\v'] = 1;
7968 is_space['\f'] = 1;
7969 is_space['\n'] = 1;
7970 is_space['\r'] = 1;
7971 }
7972
7973 /* Initialize the built-in macros. */
7974
7975 static void
7976 initialize_builtins (inp, outp)
7977 FILE_BUF *inp;
7978 FILE_BUF *outp;
7979 {
7980 time_t t;
7981
7982 t = time (0);
7983 timebuf = localtime (&t);
7984
7985 install ("__LINE__", -1, T_SPECLINE, 0, -1);
7986 install ("__DATE__", -1, T_DATE, 0, -1);
7987 install ("__FILE__", -1, T_FILE, 0, -1);
7988 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, -1);
7989 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, -1);
7990 install ("__VERSION__", -1, T_VERSION, 0, -1);
7991 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, -1);
7992 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, -1);
7993 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, -1);
7994 install ("__TIME__", -1, T_TIME, 0, -1);
7995 if (!traditional)
7996 install ("__STDC__", -1, T_CONST, STDC_VALUE, -1);
7997 if (objc)
7998 install ("__OBJC__", -1, T_CONST, 1, -1);
7999 /* This is supplied using a -D by the compiler driver
8000 so that it is present only when truly compiling with GNU C. */
8001 /* install ("__GNUC__", -1, T_CONST, 2, -1); */
8002
8003 if (debug_output)
8004 {
8005 char directive[2048];
8006 register struct directive *dp = &directive_table[0];
8007
8008 sprintf (directive, " __BASE_FILE__ \"%s\"",
8009 instack[0].nominal_fname);
8010 output_line_command (inp, outp, 0, same_file);
8011 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8012
8013 sprintf (directive, " __VERSION__ \"%s\"", version_string);
8014 output_line_command (inp, outp, 0, same_file);
8015 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8016
8017 sprintf (directive, " __SIZE_TYPE__ %s", SIZE_TYPE);
8018 output_line_command (inp, outp, 0, same_file);
8019 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8020
8021 sprintf (directive, " __PTRDIFF_TYPE__ %s", PTRDIFF_TYPE);
8022 output_line_command (inp, outp, 0, same_file);
8023 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8024
8025 sprintf (directive, " __WCHAR_TYPE__ %s", WCHAR_TYPE);
8026 output_line_command (inp, outp, 0, same_file);
8027 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8028
8029 sprintf (directive, " __WCHAR_TYPE__ %s", WCHAR_TYPE);
8030 output_line_command (inp, outp, 0, same_file);
8031 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8032
8033 sprintf (directive, " __DATE__ \"%s %2d %4d\"",
8034 monthnames[timebuf->tm_mon],
8035 timebuf->tm_mday, timebuf->tm_year + 1900);
8036 output_line_command (inp, outp, 0, same_file);
8037 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8038
8039 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"",
8040 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
8041 output_line_command (inp, outp, 0, same_file);
8042 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
8043
8044 if (!traditional)
8045 {
8046 sprintf (directive, " __STDC__ 1");
8047 output_line_command (inp, outp, 0, same_file);
8048 pass_thru_directive (directive, &directive[strlen (directive)],
8049 outp, dp);
8050 }
8051 if (objc)
8052 {
8053 sprintf (directive, " __OBJC__ 1");
8054 output_line_command (inp, outp, 0, same_file);
8055 pass_thru_directive (directive, &directive[strlen (directive)],
8056 outp, dp);
8057 }
8058 }
8059 }
8060 \f
8061 /*
8062 * process a given definition string, for initialization
8063 * If STR is just an identifier, define it with value 1.
8064 * If STR has anything after the identifier, then it should
8065 * be identifier=definition.
8066 */
8067
8068 static void
8069 make_definition (str, op)
8070 U_CHAR *str;
8071 FILE_BUF *op;
8072 {
8073 FILE_BUF *ip;
8074 struct directive *kt;
8075 U_CHAR *buf, *p;
8076
8077 buf = str;
8078 p = str;
8079 if (!is_idstart[*p]) {
8080 error ("malformed option `-D %s'", str);
8081 return;
8082 }
8083 while (is_idchar[*++p])
8084 ;
8085 if (*p == 0) {
8086 buf = (U_CHAR *) alloca (p - buf + 4);
8087 strcpy ((char *)buf, str);
8088 strcat ((char *)buf, " 1");
8089 } else if (*p != '=') {
8090 error ("malformed option `-D %s'", str);
8091 return;
8092 } else {
8093 U_CHAR *q;
8094 /* Copy the entire option so we can modify it. */
8095 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
8096 strncpy (buf, str, p - str);
8097 /* Change the = to a space. */
8098 buf[p - str] = ' ';
8099 /* Scan for any backslash-newline and remove it. */
8100 p++;
8101 q = &buf[p - str];
8102 while (*p) {
8103 if (*p == '\\' && p[1] == '\n')
8104 p += 2;
8105 /* Change newline chars into newline-markers. */
8106 else if (*p == '\n')
8107 {
8108 *q++ = '\n';
8109 *q++ = '\n';
8110 p++;
8111 }
8112 else
8113 *q++ = *p++;
8114 }
8115 *q = 0;
8116 }
8117
8118 ip = &instack[++indepth];
8119 ip->nominal_fname = ip->fname = "*Initialization*";
8120
8121 ip->buf = ip->bufp = buf;
8122 ip->length = strlen (buf);
8123 ip->lineno = 1;
8124 ip->macro = 0;
8125 ip->free_ptr = 0;
8126 ip->if_stack = if_stack;
8127 ip->system_header_p = 0;
8128
8129 for (kt = directive_table; kt->type != T_DEFINE; kt++)
8130 ;
8131
8132 do_define (buf, buf + strlen (buf) , op, kt);
8133 --indepth;
8134 }
8135
8136 /* JF, this does the work for the -U option */
8137
8138 static void
8139 make_undef (str, op)
8140 U_CHAR *str;
8141 FILE_BUF *op;
8142 {
8143 FILE_BUF *ip;
8144 struct directive *kt;
8145
8146 ip = &instack[++indepth];
8147 ip->nominal_fname = ip->fname = "*undef*";
8148
8149 ip->buf = ip->bufp = str;
8150 ip->length = strlen (str);
8151 ip->lineno = 1;
8152 ip->macro = 0;
8153 ip->free_ptr = 0;
8154 ip->if_stack = if_stack;
8155 ip->system_header_p = 0;
8156
8157 for (kt = directive_table; kt->type != T_UNDEF; kt++)
8158 ;
8159
8160 do_undef (str, str + strlen (str), op, kt);
8161 --indepth;
8162 }
8163 \f
8164 /* Process the string STR as if it appeared as the body of a #assert.
8165 OPTION is the option name for which STR was the argument. */
8166
8167 static void
8168 make_assertion (option, str)
8169 char *option;
8170 U_CHAR *str;
8171 {
8172 FILE_BUF *ip;
8173 struct directive *kt;
8174 U_CHAR *buf, *p, *q;
8175
8176 /* Copy the entire option so we can modify it. */
8177 buf = (U_CHAR *) alloca (strlen (str) + 1);
8178 strcpy ((char *) buf, str);
8179 /* Scan for any backslash-newline and remove it. */
8180 p = q = buf;
8181 while (*p) {
8182 if (*p == '\\' && p[1] == '\n')
8183 p += 2;
8184 else
8185 *q++ = *p++;
8186 }
8187 *q = 0;
8188
8189 p = buf;
8190 if (!is_idstart[*p]) {
8191 error ("malformed option `%s %s'", option, str);
8192 return;
8193 }
8194 while (is_idchar[*++p])
8195 ;
8196 while (*p == ' ' || *p == '\t') p++;
8197 if (! (*p == 0 || *p == '(')) {
8198 error ("malformed option `%s %s'", option, str);
8199 return;
8200 }
8201
8202 ip = &instack[++indepth];
8203 ip->nominal_fname = ip->fname = "*Initialization*";
8204
8205 ip->buf = ip->bufp = buf;
8206 ip->length = strlen (buf);
8207 ip->lineno = 1;
8208 ip->macro = 0;
8209 ip->free_ptr = 0;
8210 ip->if_stack = if_stack;
8211 ip->system_header_p = 0;
8212
8213 for (kt = directive_table; kt->type != T_ASSERT; kt++)
8214 ;
8215
8216 /* pass NULL as output ptr to do_define since we KNOW it never
8217 does any output.... */
8218 do_assert (buf, buf + strlen (buf) , NULL, kt);
8219 --indepth;
8220 }
8221 \f
8222 /* Add output to `deps_buffer' for the -M switch.
8223 STRING points to the text to be output.
8224 SIZE is the number of bytes, or 0 meaning output until a null.
8225 Outputting the empty string breaks the line if it is long enough. */
8226
8227 static void
8228 deps_output (string, size)
8229 char *string;
8230 unsigned size;
8231 {
8232 if (size == 0)
8233 size = strlen (string);
8234
8235 #ifndef MAX_OUTPUT_COLUMNS
8236 #define MAX_OUTPUT_COLUMNS 75
8237 #endif
8238 if (size == 0 && deps_column != 0
8239 && size + deps_column > MAX_OUTPUT_COLUMNS) {
8240 deps_output ("\\\n ", 0);
8241 deps_column = 0;
8242 }
8243
8244 if (deps_size + size + 1 > deps_allocated_size) {
8245 deps_allocated_size = deps_size + size + 50;
8246 deps_allocated_size *= 2;
8247 deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
8248 }
8249 bcopy (string, &deps_buffer[deps_size], size);
8250 deps_size += size;
8251 deps_column += size;
8252 deps_buffer[deps_size] = 0;
8253 }
8254 \f
8255 #if defined(USG) || defined(VMS)
8256 #ifndef BSTRING
8257
8258 void
8259 bzero (b, length)
8260 register char *b;
8261 register unsigned length;
8262 {
8263 #ifdef VMS
8264 short zero = 0;
8265 long max_str = 65535;
8266
8267 while (length > max_str) {
8268 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
8269 length -= max_str;
8270 b += max_str;
8271 }
8272 (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
8273 #else
8274 while (length-- > 0)
8275 *b++ = 0;
8276 #endif /* not VMS */
8277 }
8278
8279 void
8280 bcopy (b1, b2, length)
8281 register char *b1;
8282 register char *b2;
8283 register unsigned length;
8284 {
8285 #ifdef VMS
8286 long max_str = 65535;
8287
8288 while (length > max_str) {
8289 (void) LIB$MOVC3 (&max_str, b1, b2);
8290 length -= max_str;
8291 b1 += max_str;
8292 b2 += max_str;
8293 }
8294 (void) LIB$MOVC3 (&length, b1, b2);
8295 #else
8296 while (length-- > 0)
8297 *b2++ = *b1++;
8298 #endif /* not VMS */
8299 }
8300
8301 int
8302 bcmp (b1, b2, length) /* This could be a macro! */
8303 register char *b1;
8304 register char *b2;
8305 register unsigned length;
8306 {
8307 #ifdef VMS
8308 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
8309 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
8310
8311 return STR$COMPARE (&src1, &src2);
8312 #else
8313 while (length-- > 0)
8314 if (*b1++ != *b2++)
8315 return 1;
8316
8317 return 0;
8318 #endif /* not VMS */
8319 }
8320 #endif /* not BSTRING */
8321 #endif /* USG or VMS */
8322
8323 \f
8324 static void
8325 fatal (str, arg)
8326 char *str, *arg;
8327 {
8328 if (deps_file)
8329 unlink (deps_file);
8330 fprintf (stderr, "%s: ", progname);
8331 fprintf (stderr, str, arg);
8332 fprintf (stderr, "\n");
8333 exit (FAILURE_EXIT_CODE);
8334 }
8335
8336 /* More 'friendly' abort that prints the line and file.
8337 config.h can #define abort fancy_abort if you like that sort of thing. */
8338
8339 void
8340 fancy_abort ()
8341 {
8342 fatal ("Internal gcc abort.");
8343 }
8344
8345 static void
8346 perror_with_name (name)
8347 char *name;
8348 {
8349 fprintf (stderr, "%s: ", progname);
8350 if (errno < sys_nerr)
8351 fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
8352 else
8353 fprintf (stderr, "%s: undocumented I/O error\n", name);
8354 errors++;
8355 }
8356
8357 static void
8358 pfatal_with_name (name)
8359 char *name;
8360 {
8361 perror_with_name (name);
8362 #ifdef VMS
8363 exit (vaxc$errno);
8364 #else
8365 exit (FAILURE_EXIT_CODE);
8366 #endif
8367 }
8368
8369 \f
8370 static void
8371 memory_full ()
8372 {
8373 fatal ("Memory exhausted.");
8374 }
8375
8376
8377 char *
8378 xmalloc (size)
8379 unsigned size;
8380 {
8381 register char *ptr = (char *) malloc (size);
8382 if (ptr != 0) return (ptr);
8383 memory_full ();
8384 /*NOTREACHED*/
8385 return 0;
8386 }
8387
8388 static char *
8389 xrealloc (old, size)
8390 char *old;
8391 unsigned size;
8392 {
8393 register char *ptr = (char *) realloc (old, size);
8394 if (ptr != 0) return (ptr);
8395 memory_full ();
8396 /*NOTREACHED*/
8397 return 0;
8398 }
8399
8400 static char *
8401 xcalloc (number, size)
8402 unsigned number, size;
8403 {
8404 register unsigned total = number * size;
8405 register char *ptr = (char *) malloc (total);
8406 if (ptr != 0) {
8407 if (total > 100)
8408 bzero (ptr, total);
8409 else {
8410 /* It's not too long, so loop, zeroing by longs.
8411 It must be safe because malloc values are always well aligned. */
8412 register long *zp = (long *) ptr;
8413 register long *zl = (long *) (ptr + total - 4);
8414 register int i = total - 4;
8415 while (zp < zl)
8416 *zp++ = 0;
8417 if (i < 0)
8418 i = 0;
8419 while (i < total)
8420 ptr[i++] = 0;
8421 }
8422 return ptr;
8423 }
8424 memory_full ();
8425 /*NOTREACHED*/
8426 return 0;
8427 }
8428
8429 static char *
8430 savestring (input)
8431 char *input;
8432 {
8433 unsigned size = strlen (input);
8434 char *output = xmalloc (size + 1);
8435 strcpy (output, input);
8436 return output;
8437 }
8438 \f
8439 /* Get the file-mode and data size of the file open on FD
8440 and store them in *MODE_POINTER and *SIZE_POINTER. */
8441
8442 static int
8443 file_size_and_mode (fd, mode_pointer, size_pointer)
8444 int fd;
8445 int *mode_pointer;
8446 long int *size_pointer;
8447 {
8448 struct stat sbuf;
8449
8450 if (fstat (fd, &sbuf) < 0) return (-1);
8451 if (mode_pointer) *mode_pointer = sbuf.st_mode;
8452 if (size_pointer) *size_pointer = sbuf.st_size;
8453 return 0;
8454 }
8455 \f
8456 #ifdef VMS
8457
8458 /* Under VMS we need to fix up the "include" specification
8459 filename so that everything following the 1st slash is
8460 changed into its correct VMS file specification. */
8461
8462 static void
8463 hack_vms_include_specification (fname)
8464 char *fname;
8465 {
8466 register char *cp, *cp1, *cp2;
8467 int f, check_filename_before_returning, no_prefix_seen;
8468 char Local[512];
8469
8470 check_filename_before_returning = 0;
8471 no_prefix_seen = 0;
8472
8473 /* Ignore leading "./"s */
8474 while (fname[0] == '.' && fname[1] == '/') {
8475 strcpy (fname, fname+2);
8476 no_prefix_seen = 1; /* mark this for later */
8477 }
8478 /* Look for the boundary between the VMS and UNIX filespecs */
8479 cp = rindex (fname, ']'); /* Look for end of dirspec. */
8480 if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto */
8481 if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
8482 if (cp) {
8483 cp++;
8484 } else {
8485 cp = index (fname, '/'); /* Look for the "/" */
8486 }
8487
8488 cp2 = Local; /* initialize */
8489
8490 /* We are trying to do a number of things here. First of all, we are
8491 trying to hammer the filenames into a standard format, such that later
8492 processing can handle them.
8493
8494 If the file name contains something like [dir.], then it recognizes this
8495 as a root, and strips the ".]". Later processing will add whatever is
8496 needed to get things working properly.
8497
8498 If no device is specified, then the first directory name is taken to be
8499 a device name (or a rooted logical). */
8500
8501 /* See if we found that 1st slash */
8502 if (cp == 0) return; /* Nothing to do!!! */
8503 if (*cp != '/') return; /* Nothing to do!!! */
8504 /* Point to the UNIX filename part (which needs to be fixed!) */
8505 cp1 = cp+1;
8506 /* If the directory spec is not rooted, we can just copy
8507 the UNIX filename part and we are done */
8508 if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
8509 if (cp[-2] != '.') {
8510 /*
8511 * The VMS part ends in a `]', and the preceding character is not a `.'.
8512 * We strip the `]', and then splice the two parts of the name in the
8513 * usual way. Given the default locations for include files in cccp.c,
8514 * we will only use this code if the user specifies alternate locations
8515 * with the /include (-I) switch on the command line. */
8516 cp -= 1; /* Strip "]" */
8517 cp1--; /* backspace */
8518 } else {
8519 /*
8520 * The VMS part has a ".]" at the end, and this will not do. Later
8521 * processing will add a second directory spec, and this would be a syntax
8522 * error. Thus we strip the ".]", and thus merge the directory specs.
8523 * We also backspace cp1, so that it points to a '/'. This inhibits the
8524 * generation of the 000000 root directory spec (which does not belong here
8525 * in this case).
8526 */
8527 cp -= 2; /* Strip ".]" */
8528 cp1--; }; /* backspace */
8529 } else {
8530
8531 /* We drop in here if there is no VMS style directory specification yet.
8532 * If there is no device specification either, we make the first dir a
8533 * device and try that. If we do not do this, then we will be essentially
8534 * searching the users default directory (as if they did a #include "asdf.h").
8535 *
8536 * Then all we need to do is to push a '[' into the output string. Later
8537 * processing will fill this in, and close the bracket.
8538 */
8539 if(cp[-1] != ':') *cp2++ = ':'; /* dev not in spec. take first dir */
8540 *cp2++ = '['; /* Open the directory specification */
8541 }
8542
8543 /* at this point we assume that we have the device spec, and (at least
8544 the opening "[" for a directory specification. We may have directories
8545 specified already */
8546
8547 /* If there are no other slashes then the filename will be
8548 in the "root" directory. Otherwise, we need to add
8549 directory specifications. */
8550 if (index (cp1, '/') == 0) {
8551 /* Just add "000000]" as the directory string */
8552 strcpy (cp2, "000000]");
8553 cp2 += strlen (cp2);
8554 check_filename_before_returning = 1; /* we might need to fool with this later */
8555 } else {
8556 /* As long as there are still subdirectories to add, do them. */
8557 while (index (cp1, '/') != 0) {
8558 /* If this token is "." we can ignore it */
8559 if ((cp1[0] == '.') && (cp1[1] == '/')) {
8560 cp1 += 2;
8561 continue;
8562 }
8563 /* Add a subdirectory spec. Do not duplicate "." */
8564 if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
8565 *cp2++ = '.';
8566 /* If this is ".." then the spec becomes "-" */
8567 if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
8568 /* Add "-" and skip the ".." */
8569 *cp2++ = '-';
8570 cp1 += 3;
8571 continue;
8572 }
8573 /* Copy the subdirectory */
8574 while (*cp1 != '/') *cp2++= *cp1++;
8575 cp1++; /* Skip the "/" */
8576 }
8577 /* Close the directory specification */
8578 if(cp2[-1] == '.') /* no trailing periods */
8579 cp2--;
8580 *cp2++ = ']';
8581 }
8582 /* Now add the filename */
8583 while (*cp1) *cp2++ = *cp1++;
8584 *cp2 = 0;
8585 /* Now append it to the original VMS spec. */
8586 strcpy (cp, Local);
8587
8588 /* If we put a [000000] in the filename, try to open it first. If this fails,
8589 remove the [000000], and return that name. This provides flexibility
8590 to the user in that they can use both rooted and non-rooted logical names
8591 to point to the location of the file. */
8592
8593 if (check_filename_before_returning && no_prefix_seen) {
8594 f = open (fname, O_RDONLY, 0666);
8595 if (f >= 0) {
8596 /* The file name is OK as it is, so return it as is. */
8597 close (f);
8598 return;
8599 }
8600 /* The filename did not work. Try to remove the [000000] from the name,
8601 and return it. */
8602 cp = index (fname, '[');
8603 cp2 = index (fname, ']') + 1;
8604 strcpy (cp, cp2); /* this gets rid of it */
8605 }
8606 return;
8607 }
8608 #endif /* VMS */
8609 \f
8610 #ifdef VMS
8611
8612 /* These are the read/write replacement routines for
8613 VAX-11 "C". They make read/write behave enough
8614 like their UNIX counterparts that CCCP will work */
8615
8616 static int
8617 read (fd, buf, size)
8618 int fd;
8619 char *buf;
8620 int size;
8621 {
8622 #undef read /* Get back the REAL read routine */
8623 register int i;
8624 register int total = 0;
8625
8626 /* Read until the buffer is exhausted */
8627 while (size > 0) {
8628 /* Limit each read to 32KB */
8629 i = (size > (32*1024)) ? (32*1024) : size;
8630 i = read (fd, buf, i);
8631 if (i <= 0) {
8632 if (i == 0) return (total);
8633 return(i);
8634 }
8635 /* Account for this read */
8636 total += i;
8637 buf += i;
8638 size -= i;
8639 }
8640 return (total);
8641 }
8642
8643 static int
8644 write (fd, buf, size)
8645 int fd;
8646 char *buf;
8647 int size;
8648 {
8649 #undef write /* Get back the REAL write routine */
8650 int i;
8651 int j;
8652
8653 /* Limit individual writes to 32Kb */
8654 i = size;
8655 while (i > 0) {
8656 j = (i > (32*1024)) ? (32*1024) : i;
8657 if (write (fd, buf, j) < 0) return (-1);
8658 /* Account for the data written */
8659 buf += j;
8660 i -= j;
8661 }
8662 return (size);
8663 }
8664
8665 /* The following wrapper functions supply additional arguments to the VMS
8666 I/O routines to optimize performance with file handling. The arguments
8667 are:
8668 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
8669 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
8670 "fop=tef"- Truncate unused portions of file when closing file.
8671 "shr=nil"- Disallow file sharing while file is open.
8672 */
8673
8674 static FILE *
8675 freopen (fname, type, oldfile)
8676 char *fname;
8677 char *type;
8678 FILE *oldfile;
8679 {
8680 #undef freopen /* Get back the REAL fopen routine */
8681 if (strcmp (type, "w") == 0)
8682 return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
8683 return freopen (fname, type, oldfile, "mbc=16");
8684 }
8685
8686 static FILE *
8687 fopen (fname, type)
8688 char *fname;
8689 char *type;
8690 {
8691 #undef fopen /* Get back the REAL fopen routine */
8692 if (strcmp (type, "w") == 0)
8693 return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
8694 return fopen (fname, type, "mbc=16");
8695 }
8696
8697 static int
8698 open (fname, flags, prot)
8699 char *fname;
8700 int flags;
8701 int prot;
8702 {
8703 #undef open /* Get back the REAL open routine */
8704 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
8705 }
8706
8707 #endif /* VMS */
This page took 0.457671 seconds and 5 git commands to generate.