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