Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / cccp.c
blobae33aa4a1c6b589ada4f330d92c7cb6f7ae69204
1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-97, 1998 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "gansidecl.h"
24 #include <signal.h>
26 #ifdef HAVE_SYS_RESOURCE_H
27 # include <sys/resource.h>
28 #endif
30 #ifdef MULTIBYTE_CHARS
31 #include "mbchar.h"
32 #include <locale.h>
33 #endif /* MULTIBYTE_CHARS */
35 typedef unsigned char U_CHAR;
37 #include "pcp.h"
38 #include "intl.h"
40 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
41 # define __attribute__(x)
42 #endif
44 #ifndef STANDARD_INCLUDE_DIR
45 # define STANDARD_INCLUDE_DIR "/usr/include"
46 #endif
48 /* By default, colon separates directories in a path. */
49 #ifndef PATH_SEPARATOR
50 # define PATH_SEPARATOR ':'
51 #endif
53 /* By default, the suffix for object files is ".o". */
54 #ifdef OBJECT_SUFFIX
55 # define HAVE_OBJECT_SUFFIX
56 #else
57 # define OBJECT_SUFFIX ".o"
58 #endif
60 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
61 # define PRINTF_ALIST(msg) char *msg, ...
62 # define PRINTF_DCL(msg)
63 # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (__printf__, m, n)))
64 #else
65 # define PRINTF_ALIST(msg) msg, va_alist
66 # define PRINTF_DCL(msg) char *msg; va_dcl
67 # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (__printf__, m, n)))
68 # define vfprintf(file, msg, args) \
69 { \
70 char *a0 = va_arg(args, char *); \
71 char *a1 = va_arg(args, char *); \
72 char *a2 = va_arg(args, char *); \
73 char *a3 = va_arg(args, char *); \
74 fprintf (file, msg, a0, a1, a2, a3); \
76 #endif
78 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
79 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
80 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
81 #define PRINTF_PROTO_4(ARGS) PRINTF_PROTO(ARGS, 4, 5)
83 /* VMS-specific definitions */
84 #ifdef VMS
85 #include <descrip.h>
86 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
87 #define fopen(fname,mode) VMS_fopen (fname,mode)
88 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
89 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
90 #define fwrite(ptr,size,nitems,stream) VMS_fwrite (ptr,size,nitems,stream)
91 static int VMS_fstat (), VMS_stat ();
92 static int VMS_open ();
93 static FILE *VMS_fopen ();
94 static FILE *VMS_freopen ();
95 static size_t VMS_fwrite ();
96 static void hack_vms_include_specification ();
97 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
98 #define INO_T_HASH(a) 0
99 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
100 #endif /* VMS */
102 /* Windows does not natively support inodes, and neither does MSDOS. */
103 #if (defined (_WIN32) && ! defined (CYGWIN32)) || defined (__MSDOS__)
104 #define INO_T_EQ(a, b) 0
105 #endif
107 #ifndef O_RDONLY
108 #define O_RDONLY 0
109 #endif
111 #undef MIN
112 #undef MAX
113 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
114 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
116 /* Find the largest host integer type and set its size and type.
117 Watch out: on some crazy hosts `long' is shorter than `int'. */
119 #ifndef HOST_WIDE_INT
120 # if HAVE_INTTYPES_H
121 # include <inttypes.h>
122 # define HOST_WIDE_INT intmax_t
123 # else
124 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
125 # define HOST_WIDE_INT int
126 # else
127 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
128 # define HOST_WIDE_INT long
129 # else
130 # define HOST_WIDE_INT long long
131 # endif
132 # endif
133 # endif
134 #endif
136 #ifndef S_ISREG
137 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
138 #endif
140 #ifndef S_ISDIR
141 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
142 #endif
144 #ifndef INO_T_EQ
145 #define INO_T_EQ(a, b) ((a) == (b))
146 #endif
148 #ifndef INO_T_HASH
149 #define INO_T_HASH(a) (a)
150 #endif
152 #ifndef INCLUDE_LEN_FUDGE
153 #define INCLUDE_LEN_FUDGE 0
154 #endif
156 /* External declarations. */
158 extern char *version_string;
159 extern char *update_path PROTO((char *, char *));
160 #ifndef VMS
161 #ifndef HAVE_STRERROR
162 extern int sys_nerr;
163 extern char *sys_errlist[];
164 #else /* HAVE_STRERROR */
165 #ifdef NEED_DECLARATION_STRERROR
166 char *strerror ();
167 #endif
168 #endif
169 #else /* VMS */
170 char *strerror (int,...);
171 #endif
172 HOST_WIDE_INT parse_escape PROTO((char **, HOST_WIDE_INT));
173 HOST_WIDE_INT parse_c_expression PROTO((char *, int));
175 #ifndef errno
176 extern int errno;
177 #endif
179 /* Name under which this program was invoked. */
181 static char *progname;
183 /* Nonzero means use extra default include directories for C++. */
185 static int cplusplus;
187 /* Nonzero means handle cplusplus style comments */
189 static int cplusplus_comments;
191 /* Nonzero means handle #import, for objective C. */
193 static int objc;
195 /* Nonzero means this is an assembly file, and allow
196 unknown directives, which could be comments. */
198 static int lang_asm;
200 /* Current maximum length of directory names in the search path
201 for include files. (Altered as we get more of them.) */
203 static int max_include_len;
205 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
207 static int for_lint = 0;
209 /* Nonzero means copy comments into the output file. */
211 static int put_out_comments = 0;
213 /* Nonzero means don't process the ANSI trigraph sequences. */
215 static int no_trigraphs = 0;
217 /* Nonzero means print the names of included files rather than
218 the preprocessed output. 1 means just the #include "...",
219 2 means #include <...> as well. */
221 static int print_deps = 0;
223 /* Nonzero if missing .h files in -M output are assumed to be generated
224 files and not errors. */
226 static int print_deps_missing_files = 0;
228 /* Nonzero means print names of header files (-H). */
230 static int print_include_names = 0;
232 /* Nonzero means don't output line number information. */
234 static int no_line_directives;
236 /* Nonzero means output the text in failing conditionals,
237 inside #failed ... #endfailed. */
239 static int output_conditionals;
241 /* dump_only means inhibit output of the preprocessed text
242 and instead output the definitions of all user-defined
243 macros in a form suitable for use as input to cccp.
244 dump_names means pass #define and the macro name through to output.
245 dump_definitions means pass the whole definition (plus #define) through
248 static enum {dump_none, dump_only, dump_names, dump_definitions}
249 dump_macros = dump_none;
251 /* Nonzero means pass all #define and #undef directives which we actually
252 process through to the output stream. This feature is used primarily
253 to allow cc1 to record the #defines and #undefs for the sake of
254 debuggers which understand about preprocessor macros, but it may
255 also be useful with -E to figure out how symbols are defined, and
256 where they are defined. */
257 static int debug_output = 0;
259 /* Nonzero means pass #include lines through to the output,
260 even if they are ifdefed out. */
261 static int dump_includes;
263 /* Nonzero indicates special processing used by the pcp program. The
264 special effects of this mode are:
266 Inhibit all macro expansion, except those inside #if directives.
268 Process #define directives normally, and output their contents
269 to the output file.
271 Output preconditions to pcp_outfile indicating all the relevant
272 preconditions for use of this file in a later cpp run.
274 static FILE *pcp_outfile;
276 /* Nonzero means we are inside an IF during a -pcp run. In this mode
277 macro expansion is done, and preconditions are output for all macro
278 uses requiring them. */
279 static int pcp_inside_if;
281 /* Nonzero means never to include precompiled files.
282 This is 1 since there's no way now to make precompiled files,
283 so it's not worth testing for them. */
284 static int no_precomp = 1;
286 /* Nonzero means give all the error messages the ANSI standard requires. */
288 int pedantic;
290 /* Nonzero means try to make failure to fit ANSI C an error. */
292 static int pedantic_errors;
294 /* Nonzero means don't print warning messages. -w. */
296 static int inhibit_warnings = 0;
298 /* Nonzero means warn if slash-star appears in a slash-star comment,
299 or if newline-backslash appears in a slash-slash comment. */
301 static int warn_comments;
303 /* Nonzero means warn if a macro argument is (or would be)
304 stringified with -traditional. */
306 static int warn_stringify;
308 /* Nonzero means warn if there are any trigraphs. */
310 static int warn_trigraphs;
312 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
314 static int warn_undef;
316 /* Nonzero means warn if we find white space where it doesn't belong. */
318 static int warn_white_space;
320 /* Nonzero means warn if #import is used. */
322 static int warn_import = 1;
324 /* Nonzero means turn warnings into errors. */
326 static int warnings_are_errors;
328 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
330 int traditional;
332 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
334 int c89;
336 /* Nonzero causes output not to be done,
337 but directives such as #define that have side effects
338 are still obeyed. */
340 static int no_output;
342 /* Nonzero means we should look for header.gcc files that remap file names. */
343 static int remap;
345 /* Nonzero means this file was included with a -imacros or -include
346 command line and should not be recorded as an include file. */
348 static int no_record_file;
350 /* Nonzero means that we have finished processing the command line options.
351 This flag is used to decide whether or not to issue certain errors
352 and/or warnings. */
354 static int done_initializing = 0;
356 /* Line where a newline was first seen in a string constant. */
358 static int multiline_string_line = 0;
360 /* I/O buffer structure.
361 The `fname' field is nonzero for source files and #include files
362 and for the dummy text used for -D and -U.
363 It is zero for rescanning results of macro expansion
364 and for expanding macro arguments. */
365 #define INPUT_STACK_MAX 400
366 static struct file_buf {
367 char *fname;
368 /* Filename specified with #line directive. */
369 char *nominal_fname;
370 /* The length of nominal_fname, which may contain embedded NULs. */
371 size_t nominal_fname_len;
372 /* Include file description. */
373 struct include_file *inc;
374 /* Record where in the search path this file was found.
375 For #include_next. */
376 struct file_name_list *dir;
377 int lineno;
378 int length;
379 U_CHAR *buf;
380 U_CHAR *bufp;
381 /* Macro that this level is the expansion of.
382 Included so that we can reenable the macro
383 at the end of this level. */
384 struct hashnode *macro;
385 /* Value of if_stack at start of this file.
386 Used to prohibit unmatched #endif (etc) in an include file. */
387 struct if_stack *if_stack;
388 /* Object to be freed at end of input at this level. */
389 U_CHAR *free_ptr;
390 /* True if this is a system header file; see is_system_include. */
391 char system_header_p;
392 } instack[INPUT_STACK_MAX];
394 static int last_error_tick; /* Incremented each time we print it. */
395 static int input_file_stack_tick; /* Incremented when the status changes. */
397 /* Current nesting level of input sources.
398 `instack[indepth]' is the level currently being read. */
399 static int indepth = -1;
400 #define CHECK_DEPTH(code) \
401 if (indepth >= (INPUT_STACK_MAX - 1)) \
403 error_with_line (line_for_error (instack[indepth].lineno), \
404 "macro or `#include' recursion too deep"); \
405 code; \
408 /* Current depth in #include directives that use <...>. */
409 static int system_include_depth = 0;
411 typedef struct file_buf FILE_BUF;
413 /* The output buffer. Its LENGTH field is the amount of room allocated
414 for the buffer, not the number of chars actually present. To get
415 that, subtract outbuf.buf from outbuf.bufp. */
417 #define OUTBUF_SIZE 10 /* initial size of output buffer */
418 static FILE_BUF outbuf;
420 /* Grow output buffer OBUF points at
421 so it can hold at least NEEDED more chars. */
423 #define check_expand(OBUF, NEEDED) \
424 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
425 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
427 struct file_name_list
429 struct file_name_list *next;
430 /* If the following is 1, it is a C-language system include
431 directory. */
432 int c_system_include_path;
433 /* Mapping of file names for this directory. */
434 struct file_name_map *name_map;
435 /* Non-zero if name_map is valid. */
436 int got_name_map;
437 /* The include directory status. */
438 struct stat st;
439 /* The include prefix: "" denotes the working directory,
440 otherwise fname must end in '/'.
441 The actual size is dynamically allocated. */
442 char fname[1];
445 /* #include "file" looks in source file dir, then stack. */
446 /* #include <file> just looks in the stack. */
447 /* -I directories are added to the end, then the defaults are added. */
448 /* The */
449 static struct default_include {
450 char *fname; /* The name of the directory. */
451 char *component; /* The component containing the directory */
452 int cplusplus; /* Only look here if we're compiling C++. */
453 int cxx_aware; /* Includes in this directory don't need to
454 be wrapped in extern "C" when compiling
455 C++. */
456 } include_defaults_array[]
457 #ifdef INCLUDE_DEFAULTS
458 = INCLUDE_DEFAULTS;
459 #else
461 /* Pick up GNU C++ specific include files. */
462 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
463 { OLD_GPLUSPLUS_INCLUDE_DIR, 0, 1, 1 },
464 #ifdef CROSS_COMPILE
465 /* This is the dir for fixincludes. Put it just before
466 the files that we fix. */
467 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
468 /* For cross-compilation, this dir name is generated
469 automatically in Makefile.in. */
470 { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
471 #ifdef TOOL_INCLUDE_DIR
472 /* This is another place that the target system's headers might be. */
473 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 },
474 #endif
475 #else /* not CROSS_COMPILE */
476 #ifdef LOCAL_INCLUDE_DIR
477 /* This should be /usr/local/include and should come before
478 the fixincludes-fixed header files. */
479 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
480 #endif
481 #ifdef TOOL_INCLUDE_DIR
482 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
483 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
484 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 },
485 #endif
486 /* This is the dir for fixincludes. Put it just before
487 the files that we fix. */
488 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
489 /* Some systems have an extra dir of include files. */
490 #ifdef SYSTEM_INCLUDE_DIR
491 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
492 #endif
493 #ifndef STANDARD_INCLUDE_COMPONENT
494 #define STANDARD_INCLUDE_COMPONENT 0
495 #endif
496 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
497 #endif /* not CROSS_COMPILE */
498 { 0, 0, 0, 0 }
500 #endif /* no INCLUDE_DEFAULTS */
502 /* The code looks at the defaults through this pointer, rather than through
503 the constant structure above. This pointer gets changed if an environment
504 variable specifies other defaults. */
505 static struct default_include *include_defaults = include_defaults_array;
507 static struct file_name_list *include = 0; /* First dir to search */
508 /* First dir to search for <file> */
509 /* This is the first element to use for #include <...>.
510 If it is 0, use the entire chain for such includes. */
511 static struct file_name_list *first_bracket_include = 0;
512 /* This is the first element in the chain that corresponds to
513 a directory of system header files. */
514 static struct file_name_list *first_system_include = 0;
515 static struct file_name_list *last_include = 0; /* Last in chain */
517 /* Chain of include directories to put at the end of the other chain. */
518 static struct file_name_list *after_include = 0;
519 static struct file_name_list *last_after_include = 0; /* Last in chain */
521 /* Chain to put at the start of the system include files. */
522 static struct file_name_list *before_system = 0;
523 static struct file_name_list *last_before_system = 0; /* Last in chain */
525 /* Directory prefix that should replace `/usr' in the standard
526 include file directories. */
527 static char *include_prefix;
529 /* Maintain and search list of included files. */
531 struct include_file {
532 struct include_file *next; /* for include_hashtab */
533 struct include_file *next_ino; /* for include_ino_hashtab */
534 char *fname;
535 /* If the following is the empty string, it means #pragma once
536 was seen in this include file, or #import was applied to the file.
537 Otherwise, if it is nonzero, it is a macro name.
538 Don't include the file again if that macro is defined. */
539 U_CHAR *control_macro;
540 /* Nonzero if the dependency on this include file has been output. */
541 int deps_output;
542 struct stat st;
545 /* Hash tables of files already included with #include or #import.
546 include_hashtab is by full name; include_ino_hashtab is by inode number. */
548 #define INCLUDE_HASHSIZE 61
549 static struct include_file *include_hashtab[INCLUDE_HASHSIZE];
550 static struct include_file *include_ino_hashtab[INCLUDE_HASHSIZE];
552 /* Global list of strings read in from precompiled files. This list
553 is kept in the order the strings are read in, with new strings being
554 added at the end through stringlist_tailp. We use this list to output
555 the strings at the end of the run.
557 static STRINGDEF *stringlist;
558 static STRINGDEF **stringlist_tailp = &stringlist;
561 /* Structure returned by create_definition */
562 typedef struct macrodef MACRODEF;
563 struct macrodef
565 struct definition *defn;
566 U_CHAR *symnam;
567 int symlen;
570 enum sharp_token_type {
571 NO_SHARP_TOKEN = 0, /* token not present */
573 SHARP_TOKEN = '#', /* token spelled with # only */
574 WHITE_SHARP_TOKEN, /* token spelled with # and white space */
576 PERCENT_COLON_TOKEN = '%', /* token spelled with %: only */
577 WHITE_PERCENT_COLON_TOKEN /* token spelled with %: and white space */
580 /* Structure allocated for every #define. For a simple replacement
581 such as
582 #define foo bar ,
583 nargs = -1, the `pattern' list is null, and the expansion is just
584 the replacement text. Nargs = 0 means a functionlike macro with no args,
585 e.g.,
586 #define getchar() getc (stdin) .
587 When there are args, the expansion is the replacement text with the
588 args squashed out, and the reflist is a list describing how to
589 build the output from the input: e.g., "3 chars, then the 1st arg,
590 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
591 The chars here come from the expansion. Whatever is left of the
592 expansion after the last arg-occurrence is copied after that arg.
593 Note that the reflist can be arbitrarily long---
594 its length depends on the number of times the arguments appear in
595 the replacement text, not how many args there are. Example:
596 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
597 pattern list
598 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
599 where (x, y) means (nchars, argno). */
601 typedef struct definition DEFINITION;
602 struct definition {
603 int nargs;
604 int length; /* length of expansion string */
605 int predefined; /* True if the macro was builtin or */
606 /* came from the command line */
607 U_CHAR *expansion;
608 int line; /* Line number of definition */
609 char *file; /* File of definition */
610 size_t file_len; /* Length of file (which can contain NULs) */
611 char rest_args; /* Nonzero if last arg. absorbs the rest */
612 struct reflist {
613 struct reflist *next;
615 enum sharp_token_type stringify; /* set if a # operator before arg */
616 enum sharp_token_type raw_before; /* set if a ## operator before arg */
617 enum sharp_token_type raw_after; /* set if a ## operator after arg */
619 char rest_args; /* Nonzero if this arg. absorbs the rest */
620 int nchars; /* Number of literal chars to copy before
621 this arg occurrence. */
622 int argno; /* Number of arg to substitute (origin-0) */
623 } *pattern;
624 union {
625 /* Names of macro args, concatenated in reverse order
626 with comma-space between them.
627 The only use of this is that we warn on redefinition
628 if this differs between the old and new definitions. */
629 U_CHAR *argnames;
630 } args;
633 /* different kinds of things that can appear in the value field
634 of a hash node. Actually, this may be useless now. */
635 union hashval {
636 char *cpval;
637 DEFINITION *defn;
638 KEYDEF *keydef;
642 * special extension string that can be added to the last macro argument to
643 * allow it to absorb the "rest" of the arguments when expanded. Ex:
644 * #define wow(a, b...) process (b, a, b)
645 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
646 * { wow (one, two); } -> { process (two, one, two); }
647 * if this "rest_arg" is used with the concat token '##' and if it is not
648 * supplied then the token attached to with ## will not be outputted. Ex:
649 * #define wow (a, b...) process (b ## , a, ## b)
650 * { wow (1, 2); } -> { process (2, 1, 2); }
651 * { wow (one); } -> { process (one); {
653 static char rest_extension[] = "...";
654 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
656 /* The structure of a node in the hash table. The hash table
657 has entries for all tokens defined by #define directives (type T_MACRO),
658 plus some special tokens like __LINE__ (these each have their own
659 type, and the appropriate code is run when that type of node is seen.
660 It does not contain control words like "#define", which are recognized
661 by a separate piece of code. */
663 /* different flavors of hash nodes --- also used in keyword table */
664 enum node_type {
665 T_DEFINE = 1, /* the `#define' keyword */
666 T_INCLUDE, /* the `#include' keyword */
667 T_INCLUDE_NEXT, /* the `#include_next' keyword */
668 T_IMPORT, /* the `#import' keyword */
669 T_IFDEF, /* the `#ifdef' keyword */
670 T_IFNDEF, /* the `#ifndef' keyword */
671 T_IF, /* the `#if' keyword */
672 T_ELSE, /* `#else' */
673 T_PRAGMA, /* `#pragma' */
674 T_ELIF, /* `#elif' */
675 T_UNDEF, /* `#undef' */
676 T_LINE, /* `#line' */
677 T_ERROR, /* `#error' */
678 T_WARNING, /* `#warning' */
679 T_ENDIF, /* `#endif' */
680 T_SCCS, /* `#sccs', used on system V. */
681 T_IDENT, /* `#ident', used on system V. */
682 T_ASSERT, /* `#assert', taken from system V. */
683 T_UNASSERT, /* `#unassert', taken from system V. */
684 T_SPECLINE, /* special symbol `__LINE__' */
685 T_DATE, /* `__DATE__' */
686 T_FILE, /* `__FILE__' */
687 T_BASE_FILE, /* `__BASE_FILE__' */
688 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
689 T_VERSION, /* `__VERSION__' */
690 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
691 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
692 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
693 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
694 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
695 T_IMMEDIATE_PREFIX_TYPE, /* `__IMMEDIATE_PREFIX__' */
696 T_TIME, /* `__TIME__' */
697 T_CONST, /* Constant value, used by `__STDC__' */
698 T_MACRO, /* macro defined by `#define' */
699 T_DISABLED, /* macro temporarily turned off for rescan */
700 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
701 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
702 T_UNUSED /* Used for something not defined. */
705 struct hashnode {
706 struct hashnode *next; /* double links for easy deletion */
707 struct hashnode *prev;
708 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
709 chain is kept, in case the node is the head
710 of the chain and gets deleted. */
711 enum node_type type; /* type of special token */
712 int length; /* length of token, for quick comparison */
713 U_CHAR *name; /* the actual name */
714 union hashval value; /* pointer to expansion, or whatever */
717 typedef struct hashnode HASHNODE;
719 /* Some definitions for the hash table. The hash function MUST be
720 computed as shown in hashf () below. That is because the rescan
721 loop computes the hash value `on the fly' for most tokens,
722 in order to avoid the overhead of a lot of procedure calls to
723 the hashf () function. Hashf () only exists for the sake of
724 politeness, for use when speed isn't so important. */
726 #define HASHSIZE 1403
727 static HASHNODE *hashtab[HASHSIZE];
728 #define HASHSTEP(old, c) ((old << 2) + c)
729 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
731 /* Symbols to predefine. */
733 #ifdef CPP_PREDEFINES
734 static char *predefs = CPP_PREDEFINES;
735 #else
736 static char *predefs = "";
737 #endif
739 /* We let tm.h override the types used here, to handle trivial differences
740 such as the choice of unsigned int or long unsigned int for size_t.
741 When machines start needing nontrivial differences in the size type,
742 it would be best to do something here to figure out automatically
743 from other information what type to use. */
745 /* The string value for __SIZE_TYPE__. */
747 #ifndef SIZE_TYPE
748 #define SIZE_TYPE "long unsigned int"
749 #endif
751 /* The string value for __PTRDIFF_TYPE__. */
753 #ifndef PTRDIFF_TYPE
754 #define PTRDIFF_TYPE "long int"
755 #endif
757 /* The string value for __WCHAR_TYPE__. */
759 #ifndef WCHAR_TYPE
760 #define WCHAR_TYPE "int"
761 #endif
762 char * wchar_type = WCHAR_TYPE;
763 #undef WCHAR_TYPE
765 /* The string value for __USER_LABEL_PREFIX__ */
767 #ifndef USER_LABEL_PREFIX
768 #define USER_LABEL_PREFIX ""
769 #endif
771 /* The string value for __REGISTER_PREFIX__ */
773 #ifndef REGISTER_PREFIX
774 #define REGISTER_PREFIX ""
775 #endif
777 /* The string value for __IMMEDIATE_PREFIX__ */
779 #ifndef IMMEDIATE_PREFIX
780 #define IMMEDIATE_PREFIX ""
781 #endif
783 /* In the definition of a #assert name, this structure forms
784 a list of the individual values asserted.
785 Each value is itself a list of "tokens".
786 These are strings that are compared by name. */
788 struct tokenlist_list {
789 struct tokenlist_list *next;
790 struct arglist *tokens;
793 struct assertion_hashnode {
794 struct assertion_hashnode *next; /* double links for easy deletion */
795 struct assertion_hashnode *prev;
796 /* also, a back pointer to this node's hash
797 chain is kept, in case the node is the head
798 of the chain and gets deleted. */
799 struct assertion_hashnode **bucket_hdr;
800 int length; /* length of token, for quick comparison */
801 U_CHAR *name; /* the actual name */
802 /* List of token-sequences. */
803 struct tokenlist_list *value;
806 typedef struct assertion_hashnode ASSERTION_HASHNODE;
808 /* Some definitions for the hash table. The hash function MUST be
809 computed as shown in hashf below. That is because the rescan
810 loop computes the hash value `on the fly' for most tokens,
811 in order to avoid the overhead of a lot of procedure calls to
812 the hashf function. hashf only exists for the sake of
813 politeness, for use when speed isn't so important. */
815 #define ASSERTION_HASHSIZE 37
816 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
818 /* Nonzero means inhibit macroexpansion of what seem to be
819 assertion tests, in rescan. For #if. */
820 static int assertions_flag;
822 /* `struct directive' defines one #-directive, including how to handle it. */
824 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
826 struct directive {
827 int length; /* Length of name */
828 int (*func) DO_PROTO; /* Function to handle directive */
829 char *name; /* Name of directive */
830 enum node_type type; /* Code which describes which directive. */
833 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
834 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
836 /* These functions are declared to return int instead of void since they
837 are going to be placed in the table and some old compilers have trouble with
838 pointers to functions returning void. */
840 static int do_assert DO_PROTO;
841 static int do_define DO_PROTO;
842 static int do_elif DO_PROTO;
843 static int do_else DO_PROTO;
844 static int do_endif DO_PROTO;
845 static int do_error DO_PROTO;
846 static int do_ident DO_PROTO;
847 static int do_if DO_PROTO;
848 static int do_include DO_PROTO;
849 static int do_line DO_PROTO;
850 static int do_pragma DO_PROTO;
851 #ifdef SCCS_DIRECTIVE
852 static int do_sccs DO_PROTO;
853 #endif
854 static int do_unassert DO_PROTO;
855 static int do_undef DO_PROTO;
856 static int do_xifdef DO_PROTO;
858 /* Here is the actual list of #-directives, most-often-used first. */
860 static struct directive directive_table[] = {
861 { 6, do_define, "define", T_DEFINE},
862 { 2, do_if, "if", T_IF},
863 { 5, do_xifdef, "ifdef", T_IFDEF},
864 { 6, do_xifdef, "ifndef", T_IFNDEF},
865 { 5, do_endif, "endif", T_ENDIF},
866 { 4, do_else, "else", T_ELSE},
867 { 4, do_elif, "elif", T_ELIF},
868 { 4, do_line, "line", T_LINE},
869 { 7, do_include, "include", T_INCLUDE},
870 { 12, do_include, "include_next", T_INCLUDE_NEXT},
871 { 6, do_include, "import", T_IMPORT},
872 { 5, do_undef, "undef", T_UNDEF},
873 { 5, do_error, "error", T_ERROR},
874 { 7, do_error, "warning", T_WARNING},
875 #ifdef SCCS_DIRECTIVE
876 { 4, do_sccs, "sccs", T_SCCS},
877 #endif
878 { 6, do_pragma, "pragma", T_PRAGMA},
879 { 5, do_ident, "ident", T_IDENT},
880 { 6, do_assert, "assert", T_ASSERT},
881 { 8, do_unassert, "unassert", T_UNASSERT},
882 { -1, 0, "", T_UNUSED},
885 /* When a directive handler is called,
886 this points to the # (or the : of the %:) that started the directive. */
887 U_CHAR *directive_start;
889 /* table to tell if char can be part of a C identifier. */
890 U_CHAR is_idchar[256];
891 /* table to tell if char can be first char of a c identifier. */
892 U_CHAR is_idstart[256];
893 /* table to tell if c is horizontal space. */
894 static U_CHAR is_hor_space[256];
895 /* table to tell if c is horizontal or vertical space. */
896 U_CHAR is_space[256];
898 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
899 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
901 static int errors = 0; /* Error counter for exit code */
903 /* Name of output file, for error messages. */
904 static char *out_fname;
907 /* Stack of conditionals currently in progress
908 (including both successful and failing conditionals). */
910 struct if_stack {
911 struct if_stack *next; /* for chaining to the next stack frame */
912 char *fname; /* copied from input when frame is made */
913 size_t fname_len; /* similarly */
914 int lineno; /* similarly */
915 int if_succeeded; /* true if a leg of this if-group
916 has been passed through rescan */
917 U_CHAR *control_macro; /* For #ifndef at start of file,
918 this is the macro name tested. */
919 enum node_type type; /* type of last directive seen in this group */
921 typedef struct if_stack IF_STACK_FRAME;
922 static IF_STACK_FRAME *if_stack = NULL;
924 /* Buffer of -M output. */
925 static char *deps_buffer;
927 /* Number of bytes allocated in above. */
928 static int deps_allocated_size;
930 /* Number of bytes used. */
931 static int deps_size;
933 /* Number of bytes since the last newline. */
934 static int deps_column;
936 /* Nonzero means -I- has been seen,
937 so don't look for #include "foo" the source-file directory. */
938 static int ignore_srcdir;
940 static int safe_read PROTO((int, char *, int));
941 static void safe_write PROTO((int, char *, int));
943 int main PROTO((int, char **));
945 static void path_include PROTO((char *));
947 static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
949 static void trigraph_pcp PROTO((FILE_BUF *));
950 static void check_white_space PROTO((FILE_BUF *));
952 static void newline_fix PROTO((U_CHAR *));
953 static void name_newline_fix PROTO((U_CHAR *));
955 static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
957 static void rescan PROTO((FILE_BUF *, int));
959 static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
961 static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
963 static struct tm *timestamp PROTO((void));
964 static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
966 static int is_system_include PROTO((char *));
967 static char *base_name PROTO((char *));
968 static int absolute_filename PROTO((char *));
969 static size_t simplify_filename PROTO((char *));
971 static char *read_filename_string PROTO((int, FILE *));
972 static struct file_name_map *read_name_map PROTO((char *));
973 static int open_include_file PROTO((char *, struct file_name_list *, U_CHAR *, struct include_file **));
974 static char *remap_include_file PROTO((char *, struct file_name_list *));
975 static int lookup_ino_include PROTO((struct include_file *));
977 static void finclude PROTO((int, struct include_file *, FILE_BUF *, int, struct file_name_list *));
978 static void record_control_macro PROTO((struct include_file *, U_CHAR *));
980 static char *check_precompiled PROTO((int, struct stat *, char *, char **));
981 static int check_preconditions PROTO((char *));
982 static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *));
983 static void pcstring_used PROTO((HASHNODE *));
984 static void write_output PROTO((void));
985 static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
987 static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
989 static int check_macro_name PROTO((U_CHAR *, int));
990 static int compare_defs PROTO((DEFINITION *, DEFINITION *));
991 static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
993 static DEFINITION *collect_expansion PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
995 int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
996 static int compare_token_lists PROTO((struct arglist *, struct arglist *));
998 static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
999 static void free_token_list PROTO((struct arglist *));
1001 static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
1002 static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
1003 static void delete_assertion PROTO((ASSERTION_HASHNODE *));
1005 static void do_once PROTO((void));
1007 static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
1008 static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
1009 static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
1010 static void validate_else PROTO((U_CHAR *, U_CHAR *));
1012 static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
1013 static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
1014 static char *quote_string PROTO((char *, char *, size_t));
1015 static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
1017 /* Last arg to output_line_directive. */
1018 enum file_change_code {same_file, enter_file, leave_file};
1019 static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
1021 static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
1023 struct argdata;
1024 static int macarg PROTO((struct argdata *, int));
1026 static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, struct hashnode *, int *, int *, int *, int));
1028 static int discard_comments PROTO((U_CHAR *, int, int));
1030 static void change_newlines PROTO((struct argdata *));
1032 char *my_strerror PROTO((int));
1033 static void notice PRINTF_PROTO_1((char *, ...));
1034 static void vnotice PROTO((char *, va_list));
1035 void error PRINTF_PROTO_1((char *, ...));
1036 void verror PROTO((char *, va_list));
1037 static void error_from_errno PROTO((char *));
1038 void warning PRINTF_PROTO_1((char *, ...));
1039 static void vwarning PROTO((char *, va_list));
1040 static void error_with_line PRINTF_PROTO_2((int, char *, ...));
1041 static void verror_with_line PROTO((int, char *, va_list));
1042 static void vwarning_with_line PROTO((int, char *, va_list));
1043 static void warning_with_line PRINTF_PROTO_2((int, char *, ...));
1044 void pedwarn PRINTF_PROTO_1((char *, ...));
1045 void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
1046 static void pedwarn_with_file_and_line PRINTF_PROTO_4((char *, size_t, int, char *, ...));
1047 static void pedwarn_strange_white_space PROTO((int));
1049 static void print_containing_files PROTO((void));
1051 static int line_for_error PROTO((int));
1052 static int grow_outbuf PROTO((FILE_BUF *, int));
1054 static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
1055 HASHNODE *lookup PROTO((U_CHAR *, int, int));
1056 static void delete_macro PROTO((HASHNODE *));
1057 static int hashf PROTO((U_CHAR *, int, int));
1059 static void dump_single_macro PROTO((HASHNODE *, FILE *));
1060 static void dump_all_macros PROTO((void));
1061 static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
1062 static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
1064 static void initialize_char_syntax PROTO((void));
1065 static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
1067 static void make_definition PROTO((char *, FILE_BUF *));
1068 static void make_undef PROTO((char *, FILE_BUF *));
1070 static void make_assertion PROTO((char *, char *));
1072 static struct file_name_list *new_include_prefix PROTO((struct file_name_list *, char *, char *, char *));
1073 static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
1075 static int quote_string_for_make PROTO((char *, char *));
1076 static void deps_output PROTO((char *, int));
1078 void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
1079 void fancy_abort PROTO((void)) __attribute__ ((noreturn));
1080 static void perror_with_name PROTO((char *));
1081 static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
1082 static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
1084 static void memory_full PROTO((void)) __attribute__ ((noreturn));
1085 GENERIC_PTR xmalloc PROTO((size_t));
1086 static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t));
1087 static GENERIC_PTR xcalloc PROTO((size_t, size_t));
1088 static char *savestring PROTO((char *));
1090 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1091 retrying if necessary. If MAX_READ_LEN is defined, read at most
1092 that bytes at a time. Return a negative value if an error occurs,
1093 otherwise return the actual number of bytes read,
1094 which must be LEN unless end-of-file was reached. */
1096 static int
1097 safe_read (desc, ptr, len)
1098 int desc;
1099 char *ptr;
1100 int len;
1102 int left, rcount, nchars;
1104 left = len;
1105 while (left > 0) {
1106 rcount = left;
1107 #ifdef MAX_READ_LEN
1108 if (rcount > MAX_READ_LEN)
1109 rcount = MAX_READ_LEN;
1110 #endif
1111 nchars = read (desc, ptr, rcount);
1112 if (nchars < 0)
1114 #ifdef EINTR
1115 if (errno == EINTR)
1116 continue;
1117 #endif
1118 return nchars;
1120 if (nchars == 0)
1121 break;
1122 ptr += nchars;
1123 left -= nchars;
1125 return len - left;
1128 /* Write LEN bytes at PTR to descriptor DESC,
1129 retrying if necessary, and treating any real error as fatal.
1130 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1132 static void
1133 safe_write (desc, ptr, len)
1134 int desc;
1135 char *ptr;
1136 int len;
1138 int wcount, written;
1140 while (len > 0) {
1141 wcount = len;
1142 #ifdef MAX_WRITE_LEN
1143 if (wcount > MAX_WRITE_LEN)
1144 wcount = MAX_WRITE_LEN;
1145 #endif
1146 written = write (desc, ptr, wcount);
1147 if (written < 0)
1149 #ifdef EINTR
1150 if (errno == EINTR)
1151 continue;
1152 #endif
1153 pfatal_with_name (out_fname);
1155 ptr += written;
1156 len -= written;
1161 main (argc, argv)
1162 int argc;
1163 char **argv;
1165 struct stat st;
1166 char *in_fname;
1167 char *cp;
1168 int f, i;
1169 FILE_BUF *fp;
1171 char **pend_files;
1172 char **pend_defs;
1173 char **pend_undefs;
1174 char **pend_assertions;
1175 char **pend_includes;
1177 /* Record the option used with each element of pend_assertions.
1178 This is preparation for supporting more than one option for making
1179 an assertion. */
1180 char **pend_assertion_options;
1181 int inhibit_predefs = 0;
1182 int no_standard_includes = 0;
1183 int no_standard_cplusplus_includes = 0;
1184 int missing_newline = 0;
1186 /* Non-0 means don't output the preprocessed program. */
1187 int inhibit_output = 0;
1188 /* Non-0 means -v, so print the full set of include dirs. */
1189 int verbose = 0;
1191 /* File name which deps are being written to.
1192 This is 0 if deps are being written to stdout. */
1193 char *deps_file = 0;
1194 /* Fopen file mode to open deps_file with. */
1195 char *deps_mode = "a";
1196 /* Stream on which to print the dependency information. */
1197 FILE *deps_stream = 0;
1198 /* Target-name to write with the dependency information. */
1199 char *deps_target = 0;
1201 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1202 /* Get rid of any avoidable limit on stack size. */
1204 struct rlimit rlim;
1206 /* Set the stack limit huge so that alloca (particularly stringtab
1207 in dbxread.c) does not fail. */
1208 getrlimit (RLIMIT_STACK, &rlim);
1209 rlim.rlim_cur = rlim.rlim_max;
1210 setrlimit (RLIMIT_STACK, &rlim);
1212 #endif
1214 #ifdef SIGPIPE
1215 signal (SIGPIPE, pipe_closed);
1216 #endif
1218 setlocale (LC_MESSAGES, "");
1219 bindtextdomain (PACKAGE, localedir);
1220 textdomain (PACKAGE);
1222 progname = base_name (argv[0]);
1224 #ifdef VMS
1226 /* Remove extension from PROGNAME. */
1227 char *p;
1228 char *s = progname = savestring (progname);
1230 if ((p = rindex (s, ';')) != 0) *p = '\0'; /* strip version number */
1231 if ((p = rindex (s, '.')) != 0 /* strip type iff ".exe" */
1232 && (p[1] == 'e' || p[1] == 'E')
1233 && (p[2] == 'x' || p[2] == 'X')
1234 && (p[3] == 'e' || p[3] == 'E')
1235 && !p[4])
1236 *p = '\0';
1238 #endif
1240 /* Do not invoke xmalloc before this point, since locale and
1241 progname need to be set first, in case a diagnostic is issued. */
1243 pend_files = (char **) xmalloc (argc * sizeof (char *));
1244 pend_defs = (char **) xmalloc (argc * sizeof (char *));
1245 pend_undefs = (char **) xmalloc (argc * sizeof (char *));
1246 pend_assertions = (char **) xmalloc (argc * sizeof (char *));
1247 pend_includes = (char **) xmalloc (argc * sizeof (char *));
1248 pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
1250 in_fname = NULL;
1251 out_fname = NULL;
1253 /* Initialize is_idchar. */
1254 initialize_char_syntax ();
1256 no_line_directives = 0;
1257 no_trigraphs = 1;
1258 dump_macros = dump_none;
1259 no_output = 0;
1260 cplusplus = 0;
1261 cplusplus_comments = 1;
1263 bzero ((char *) pend_files, argc * sizeof (char *));
1264 bzero ((char *) pend_defs, argc * sizeof (char *));
1265 bzero ((char *) pend_undefs, argc * sizeof (char *));
1266 bzero ((char *) pend_assertions, argc * sizeof (char *));
1267 bzero ((char *) pend_includes, argc * sizeof (char *));
1269 #ifdef MULTIBYTE_CHARS
1270 /* Change to the native locale for multibyte conversions. */
1271 setlocale (LC_CTYPE, "");
1272 literal_codeset = getenv ("LANG");
1273 #endif
1275 /* Process switches and find input file name. */
1277 for (i = 1; i < argc; i++) {
1278 if (argv[i][0] != '-') {
1279 if (out_fname != NULL)
1280 fatal ("Usage: %s [switches] input output", argv[0]);
1281 else if (in_fname != NULL)
1282 out_fname = argv[i];
1283 else
1284 in_fname = argv[i];
1285 } else {
1286 switch (argv[i][1]) {
1288 case 'i':
1289 if (!strcmp (argv[i], "-include")) {
1290 if (i + 1 == argc)
1291 fatal ("Filename missing after `-include' option");
1292 else {
1293 simplify_filename (pend_includes[i] = argv[i]);
1294 i++;
1297 if (!strcmp (argv[i], "-imacros")) {
1298 if (i + 1 == argc)
1299 fatal ("Filename missing after `-imacros' option");
1300 else {
1301 simplify_filename (pend_files[i] = argv[i]);
1302 i++;
1305 if (!strcmp (argv[i], "-iprefix")) {
1306 if (i + 1 == argc)
1307 fatal ("Filename missing after `-iprefix' option");
1308 else
1309 include_prefix = argv[++i];
1311 if (!strcmp (argv[i], "-ifoutput")) {
1312 output_conditionals = 1;
1314 if (!strcmp (argv[i], "-isystem")) {
1315 struct file_name_list *dirtmp;
1317 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1318 "", argv[++i])))
1319 break;
1320 dirtmp->c_system_include_path = 1;
1322 if (before_system == 0)
1323 before_system = dirtmp;
1324 else
1325 last_before_system->next = dirtmp;
1326 last_before_system = dirtmp; /* Tail follows the last one */
1328 /* Add directory to end of path for includes,
1329 with the default prefix at the front of its name. */
1330 if (!strcmp (argv[i], "-iwithprefix")) {
1331 struct file_name_list *dirtmp;
1332 char *prefix;
1334 if (include_prefix != 0)
1335 prefix = include_prefix;
1336 else {
1337 prefix = savestring (GCC_INCLUDE_DIR);
1338 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1339 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1340 prefix[strlen (prefix) - 7] = 0;
1343 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1344 prefix, argv[++i])))
1345 break;
1347 if (after_include == 0)
1348 after_include = dirtmp;
1349 else
1350 last_after_include->next = dirtmp;
1351 last_after_include = dirtmp; /* Tail follows the last one */
1353 /* Add directory to main path for includes,
1354 with the default prefix at the front of its name. */
1355 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1356 struct file_name_list *dirtmp;
1357 char *prefix;
1359 if (include_prefix != 0)
1360 prefix = include_prefix;
1361 else {
1362 prefix = savestring (GCC_INCLUDE_DIR);
1363 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1364 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1365 prefix[strlen (prefix) - 7] = 0;
1368 dirtmp = new_include_prefix (NULL_PTR, NULL_PTR, prefix, argv[++i]);
1369 append_include_chain (dirtmp, dirtmp);
1371 /* Add directory to end of path for includes. */
1372 if (!strcmp (argv[i], "-idirafter")) {
1373 struct file_name_list *dirtmp;
1375 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1376 "", argv[++i])))
1377 break;
1379 if (after_include == 0)
1380 after_include = dirtmp;
1381 else
1382 last_after_include->next = dirtmp;
1383 last_after_include = dirtmp; /* Tail follows the last one */
1385 break;
1387 case 'o':
1388 if (out_fname != NULL)
1389 fatal ("Output filename specified twice");
1390 if (i + 1 == argc)
1391 fatal ("Filename missing after -o option");
1392 out_fname = argv[++i];
1393 if (!strcmp (out_fname, "-"))
1394 out_fname = "";
1395 break;
1397 case 'p':
1398 if (!strcmp (argv[i], "-pedantic"))
1399 pedantic = 1;
1400 else if (!strcmp (argv[i], "-pedantic-errors")) {
1401 pedantic = 1;
1402 pedantic_errors = 1;
1403 } else if (!strcmp (argv[i], "-pcp")) {
1404 char *pcp_fname;
1405 if (i + 1 == argc)
1406 fatal ("Filename missing after -pcp option");
1407 pcp_fname = argv[++i];
1408 pcp_outfile
1409 = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1410 ? fopen (pcp_fname, "w")
1411 : stdout);
1412 if (pcp_outfile == 0)
1413 pfatal_with_name (pcp_fname);
1414 no_precomp = 1;
1416 break;
1418 case 't':
1419 if (!strcmp (argv[i], "-traditional")) {
1420 traditional = 1;
1421 cplusplus_comments = 0;
1422 } else if (!strcmp (argv[i], "-trigraphs")) {
1423 no_trigraphs = 0;
1425 break;
1427 case 'l':
1428 if (! strcmp (argv[i], "-lang-c"))
1429 cplusplus = 0, cplusplus_comments = 1, c89 = 0, objc = 0;
1430 if (! strcmp (argv[i], "-lang-c89"))
1431 cplusplus = 0, cplusplus_comments = 0, c89 = 1, objc = 0;
1432 if (! strcmp (argv[i], "-lang-c++"))
1433 cplusplus = 1, cplusplus_comments = 1, c89 = 0, objc = 0;
1434 if (! strcmp (argv[i], "-lang-objc"))
1435 cplusplus = 0, cplusplus_comments = 1, c89 = 0, objc = 1;
1436 if (! strcmp (argv[i], "-lang-objc++"))
1437 cplusplus = 1, cplusplus_comments = 1, c89 = 0, objc = 1;
1438 if (! strcmp (argv[i], "-lang-asm"))
1439 lang_asm = 1;
1440 if (! strcmp (argv[i], "-lint"))
1441 for_lint = 1;
1442 break;
1444 case '+':
1445 cplusplus = 1, cplusplus_comments = 1;
1446 break;
1448 case 'w':
1449 inhibit_warnings = 1;
1450 break;
1452 case 'W':
1453 if (!strcmp (argv[i], "-Wtrigraphs"))
1454 warn_trigraphs = 1;
1455 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1456 warn_trigraphs = 0;
1457 else if (!strcmp (argv[i], "-Wcomment"))
1458 warn_comments = 1;
1459 else if (!strcmp (argv[i], "-Wno-comment"))
1460 warn_comments = 0;
1461 else if (!strcmp (argv[i], "-Wcomments"))
1462 warn_comments = 1;
1463 else if (!strcmp (argv[i], "-Wno-comments"))
1464 warn_comments = 0;
1465 else if (!strcmp (argv[i], "-Wtraditional"))
1466 warn_stringify = 1;
1467 else if (!strcmp (argv[i], "-Wno-traditional"))
1468 warn_stringify = 0;
1469 else if (!strcmp (argv[i], "-Wwhite-space"))
1470 warn_white_space = 1;
1471 else if (!strcmp (argv[i], "-Wno-white-space"))
1472 warn_white_space = 0;
1473 else if (!strcmp (argv[i], "-Wundef"))
1474 warn_undef = 1;
1475 else if (!strcmp (argv[i], "-Wno-undef"))
1476 warn_undef = 0;
1477 else if (!strcmp (argv[i], "-Wimport"))
1478 warn_import = 1;
1479 else if (!strcmp (argv[i], "-Wno-import"))
1480 warn_import = 0;
1481 else if (!strcmp (argv[i], "-Werror"))
1482 warnings_are_errors = 1;
1483 else if (!strcmp (argv[i], "-Wno-error"))
1484 warnings_are_errors = 0;
1485 else if (!strcmp (argv[i], "-Wall"))
1487 warn_trigraphs = 1;
1488 warn_comments = 1;
1489 warn_white_space = 1;
1491 break;
1493 case 'M':
1494 /* The style of the choices here is a bit mixed.
1495 The chosen scheme is a hybrid of keeping all options in one string
1496 and specifying each option in a separate argument:
1497 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1498 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1499 -M[M][G][D file]. This is awkward to handle in specs, and is not
1500 as extensible. */
1501 /* ??? -MG must be specified in addition to one of -M or -MM.
1502 This can be relaxed in the future without breaking anything.
1503 The converse isn't true. */
1505 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1506 if (!strcmp (argv[i], "-MG"))
1508 print_deps_missing_files = 1;
1509 break;
1511 if (!strcmp (argv[i], "-M"))
1512 print_deps = 2;
1513 else if (!strcmp (argv[i], "-MM"))
1514 print_deps = 1;
1515 else if (!strcmp (argv[i], "-MD"))
1516 print_deps = 2;
1517 else if (!strcmp (argv[i], "-MMD"))
1518 print_deps = 1;
1519 /* For -MD and -MMD options, write deps on file named by next arg. */
1520 if (!strcmp (argv[i], "-MD")
1521 || !strcmp (argv[i], "-MMD")) {
1522 if (i + 1 == argc)
1523 fatal ("Filename missing after %s option", argv[i]);
1524 i++;
1525 deps_file = argv[i];
1526 deps_mode = "w";
1527 } else {
1528 /* For -M and -MM, write deps on standard output
1529 and suppress the usual output. */
1530 deps_stream = stdout;
1531 inhibit_output = 1;
1533 break;
1535 case 'd':
1537 char *p = argv[i] + 2;
1538 char c;
1539 while ((c = *p++)) {
1540 /* Arg to -d specifies what parts of macros to dump */
1541 switch (c) {
1542 case 'M':
1543 dump_macros = dump_only;
1544 no_output = 1;
1545 break;
1546 case 'N':
1547 dump_macros = dump_names;
1548 break;
1549 case 'D':
1550 dump_macros = dump_definitions;
1551 break;
1552 case 'I':
1553 dump_includes = 1;
1554 break;
1558 break;
1560 case 'g':
1561 if (argv[i][2] == '3')
1562 debug_output = 1;
1563 break;
1565 case 'v':
1566 notice ("GNU CPP version %s", version_string);
1567 #ifdef TARGET_VERSION
1568 TARGET_VERSION;
1569 #endif
1570 fprintf (stderr, "\n");
1571 verbose = 1;
1572 break;
1574 case 'H':
1575 print_include_names = 1;
1576 break;
1578 case 'D':
1579 if (argv[i][2] != 0)
1580 pend_defs[i] = argv[i] + 2;
1581 else if (i + 1 == argc)
1582 fatal ("Macro name missing after -D option");
1583 else
1584 i++, pend_defs[i] = argv[i];
1585 break;
1587 case 'A':
1589 char *p;
1591 if (argv[i][2] != 0)
1592 p = argv[i] + 2;
1593 else if (i + 1 == argc)
1594 fatal ("Assertion missing after -A option");
1595 else
1596 p = argv[++i];
1598 if (!strcmp (p, "-")) {
1599 /* -A- eliminates all predefined macros and assertions.
1600 Let's include also any that were specified earlier
1601 on the command line. That way we can get rid of any
1602 that were passed automatically in from GCC. */
1603 int j;
1604 inhibit_predefs = 1;
1605 for (j = 0; j < i; j++)
1606 pend_defs[j] = pend_assertions[j] = 0;
1607 } else {
1608 pend_assertions[i] = p;
1609 pend_assertion_options[i] = "-A";
1612 break;
1614 case 'U': /* JF #undef something */
1615 if (argv[i][2] != 0)
1616 pend_undefs[i] = argv[i] + 2;
1617 else if (i + 1 == argc)
1618 fatal ("Macro name missing after -U option");
1619 else
1620 pend_undefs[i] = argv[i+1], i++;
1621 break;
1623 case 'C':
1624 put_out_comments = 1;
1625 break;
1627 case 'E': /* -E comes from cc -E; ignore it. */
1628 break;
1630 case 'P':
1631 no_line_directives = 1;
1632 break;
1634 case '$': /* Don't include $ in identifiers. */
1635 is_idchar['$'] = is_idstart['$'] = 0;
1636 break;
1638 case 'I': /* Add directory to path for includes. */
1640 struct file_name_list *dirtmp;
1641 char *dir = argv[i][2] ? argv[i] + 2 : argv[++i];
1643 if (! ignore_srcdir && !strcmp (dir, "-")) {
1644 ignore_srcdir = 1;
1645 /* Don't use any preceding -I directories for #include <...>. */
1646 first_bracket_include = 0;
1648 else {
1649 dirtmp = new_include_prefix (last_include, NULL_PTR, "", dir);
1650 append_include_chain (dirtmp, dirtmp);
1653 break;
1655 case 'n':
1656 if (!strcmp (argv[i], "-nostdinc"))
1657 /* -nostdinc causes no default include directories.
1658 You must specify all include-file directories with -I. */
1659 no_standard_includes = 1;
1660 else if (!strcmp (argv[i], "-nostdinc++"))
1661 /* -nostdinc++ causes no default C++-specific include directories. */
1662 no_standard_cplusplus_includes = 1;
1663 else if (!strcmp (argv[i], "-noprecomp"))
1664 no_precomp = 1;
1665 break;
1667 case 'r':
1668 if (!strcmp (argv[i], "-remap"))
1669 remap = 1;
1670 break;
1672 case 'u':
1673 /* Sun compiler passes undocumented switch "-undef".
1674 Let's assume it means to inhibit the predefined symbols. */
1675 inhibit_predefs = 1;
1676 break;
1678 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1679 if (in_fname == NULL) {
1680 in_fname = "";
1681 break;
1682 } else if (out_fname == NULL) {
1683 out_fname = "";
1684 break;
1685 } /* else fall through into error */
1687 default:
1688 fatal ("Invalid option `%s'", argv[i]);
1693 /* Add dirs from CPATH after dirs from -I. */
1694 /* There seems to be confusion about what CPATH should do,
1695 so for the moment it is not documented. */
1696 /* Some people say that CPATH should replace the standard include dirs,
1697 but that seems pointless: it comes before them, so it overrides them
1698 anyway. */
1699 cp = getenv ("CPATH");
1700 if (cp && ! no_standard_includes)
1701 path_include (cp);
1703 /* Initialize output buffer */
1705 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1706 outbuf.bufp = outbuf.buf;
1707 outbuf.length = OUTBUF_SIZE;
1709 /* Do partial setup of input buffer for the sake of generating
1710 early #line directives (when -g is in effect). */
1712 fp = &instack[++indepth];
1713 if (in_fname == NULL)
1714 in_fname = "";
1715 fp->nominal_fname = fp->fname = in_fname;
1716 fp->nominal_fname_len = strlen (in_fname);
1717 fp->lineno = 0;
1719 /* In C++, wchar_t is a distinct basic type, and we can expect
1720 __wchar_t to be defined by cc1plus. */
1721 if (cplusplus)
1722 wchar_type = "__wchar_t";
1724 /* Install __LINE__, etc. Must follow initialize_char_syntax
1725 and option processing. */
1726 initialize_builtins (fp, &outbuf);
1728 /* Do standard #defines and assertions
1729 that identify system and machine type. */
1731 if (!inhibit_predefs) {
1732 char *p = (char *) alloca (strlen (predefs) + 1);
1733 strcpy (p, predefs);
1734 while (*p) {
1735 char *q;
1736 while (*p == ' ' || *p == '\t')
1737 p++;
1738 /* Handle -D options. */
1739 if (p[0] == '-' && p[1] == 'D') {
1740 q = &p[2];
1741 while (*p && *p != ' ' && *p != '\t')
1742 p++;
1743 if (*p != 0)
1744 *p++= 0;
1745 if (debug_output)
1746 output_line_directive (fp, &outbuf, 0, same_file);
1747 make_definition (q, &outbuf);
1748 while (*p == ' ' || *p == '\t')
1749 p++;
1750 } else if (p[0] == '-' && p[1] == 'A') {
1751 /* Handle -A options (assertions). */
1752 char *assertion;
1753 char *past_name;
1754 char *value;
1755 char *past_value;
1756 char *termination;
1757 int save_char;
1759 assertion = &p[2];
1760 past_name = assertion;
1761 /* Locate end of name. */
1762 while (*past_name && *past_name != ' '
1763 && *past_name != '\t' && *past_name != '(')
1764 past_name++;
1765 /* Locate `(' at start of value. */
1766 value = past_name;
1767 while (*value && (*value == ' ' || *value == '\t'))
1768 value++;
1769 if (*value++ != '(')
1770 abort ();
1771 while (*value && (*value == ' ' || *value == '\t'))
1772 value++;
1773 past_value = value;
1774 /* Locate end of value. */
1775 while (*past_value && *past_value != ' '
1776 && *past_value != '\t' && *past_value != ')')
1777 past_value++;
1778 termination = past_value;
1779 while (*termination && (*termination == ' ' || *termination == '\t'))
1780 termination++;
1781 if (*termination++ != ')')
1782 abort ();
1783 if (*termination && *termination != ' ' && *termination != '\t')
1784 abort ();
1785 /* Temporarily null-terminate the value. */
1786 save_char = *termination;
1787 *termination = '\0';
1788 /* Install the assertion. */
1789 make_assertion ("-A", assertion);
1790 *termination = (char) save_char;
1791 p = termination;
1792 while (*p == ' ' || *p == '\t')
1793 p++;
1794 } else {
1795 abort ();
1800 /* Now handle the command line options. */
1802 /* Do -U's, -D's and -A's in the order they were seen. */
1803 for (i = 1; i < argc; i++) {
1804 if (pend_undefs[i]) {
1805 if (debug_output)
1806 output_line_directive (fp, &outbuf, 0, same_file);
1807 make_undef (pend_undefs[i], &outbuf);
1809 if (pend_defs[i]) {
1810 if (debug_output)
1811 output_line_directive (fp, &outbuf, 0, same_file);
1812 make_definition (pend_defs[i], &outbuf);
1814 if (pend_assertions[i])
1815 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1818 done_initializing = 1;
1820 { /* Read the appropriate environment variable and if it exists
1821 replace include_defaults with the listed path. */
1822 char *epath = 0;
1823 switch ((objc << 1) + cplusplus)
1825 case 0:
1826 epath = getenv ("C_INCLUDE_PATH");
1827 break;
1828 case 1:
1829 epath = getenv ("CPLUS_INCLUDE_PATH");
1830 break;
1831 case 2:
1832 epath = getenv ("OBJC_INCLUDE_PATH");
1833 break;
1834 case 3:
1835 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
1836 break;
1838 /* If the environment var for this language is set,
1839 add to the default list of include directories. */
1840 if (epath) {
1841 int num_dirs;
1842 char *startp, *endp;
1844 for (num_dirs = 1, startp = epath; *startp; startp++)
1845 if (*startp == PATH_SEPARATOR)
1846 num_dirs++;
1847 include_defaults
1848 = (struct default_include *) xmalloc ((num_dirs
1849 * sizeof (struct default_include))
1850 + sizeof (include_defaults_array));
1851 startp = endp = epath;
1852 num_dirs = 0;
1853 while (1) {
1854 char c = *endp++;
1855 if (c == PATH_SEPARATOR || !c) {
1856 endp[-1] = 0;
1857 include_defaults[num_dirs].fname
1858 = startp == endp ? "." : savestring (startp);
1859 endp[-1] = c;
1860 include_defaults[num_dirs].component = 0;
1861 include_defaults[num_dirs].cplusplus = cplusplus;
1862 include_defaults[num_dirs].cxx_aware = 1;
1863 num_dirs++;
1864 if (!c)
1865 break;
1866 startp = endp;
1869 /* Put the usual defaults back in at the end. */
1870 bcopy ((char *) include_defaults_array,
1871 (char *) &include_defaults[num_dirs],
1872 sizeof (include_defaults_array));
1876 append_include_chain (before_system, last_before_system);
1877 first_system_include = before_system;
1879 /* Unless -fnostdinc,
1880 tack on the standard include file dirs to the specified list */
1881 if (!no_standard_includes) {
1882 struct default_include *p = include_defaults;
1883 char *specd_prefix = include_prefix;
1884 char *default_prefix = savestring (GCC_INCLUDE_DIR);
1885 int default_len = 0;
1886 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1887 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1888 default_len = strlen (default_prefix) - 7;
1889 default_prefix[default_len] = 0;
1891 /* Search "translated" versions of GNU directories.
1892 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1893 if (specd_prefix != 0 && default_len != 0)
1894 for (p = include_defaults; p->fname; p++) {
1895 /* Some standard dirs are only for C++. */
1896 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1897 /* Does this dir start with the prefix? */
1898 if (!strncmp (p->fname, default_prefix, default_len)) {
1899 /* Yes; change prefix and add to search list. */
1900 struct file_name_list *new
1901 = new_include_prefix (NULL_PTR, NULL_PTR, specd_prefix,
1902 p->fname + default_len);
1903 if (new) {
1904 new->c_system_include_path = !p->cxx_aware;
1905 append_include_chain (new, new);
1906 if (first_system_include == 0)
1907 first_system_include = new;
1912 /* Search ordinary names for GNU include directories. */
1913 for (p = include_defaults; p->fname; p++) {
1914 /* Some standard dirs are only for C++. */
1915 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1916 struct file_name_list *new
1917 = new_include_prefix (NULL_PTR, p->component, "", p->fname);
1918 if (new) {
1919 new->c_system_include_path = !p->cxx_aware;
1920 append_include_chain (new, new);
1921 if (first_system_include == 0)
1922 first_system_include = new;
1928 /* Tack the after_include chain at the end of the include chain. */
1929 append_include_chain (after_include, last_after_include);
1930 if (first_system_include == 0)
1931 first_system_include = after_include;
1933 /* With -v, print the list of dirs to search. */
1934 if (verbose) {
1935 struct file_name_list *p;
1936 notice ("#include \"...\" search starts here:\n");
1937 for (p = include; p; p = p->next) {
1938 if (p == first_bracket_include)
1939 notice ("#include <...> search starts here:\n");
1940 if (!p->fname[0])
1941 fprintf (stderr, " .\n");
1942 else if (!strcmp (p->fname, "/") || !strcmp (p->fname, "//"))
1943 fprintf (stderr, " %s\n", p->fname);
1944 else
1945 /* Omit trailing '/'. */
1946 fprintf (stderr, " %.*s\n", (int) strlen (p->fname) - 1, p->fname);
1948 notice ("End of search list.\n");
1951 /* -MG doesn't select the form of output and must be specified with one of
1952 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
1953 inhibit compilation. */
1954 if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
1955 fatal ("-MG must be specified with one of -M or -MM");
1957 /* Either of two environment variables can specify output of deps.
1958 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1959 where OUTPUT_FILE is the file to write deps info to
1960 and DEPS_TARGET is the target to mention in the deps. */
1962 if (print_deps == 0
1963 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1964 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1965 char *spec = getenv ("DEPENDENCIES_OUTPUT");
1966 char *s;
1967 char *output_file;
1969 if (spec == 0) {
1970 spec = getenv ("SUNPRO_DEPENDENCIES");
1971 print_deps = 2;
1973 else
1974 print_deps = 1;
1976 /* Find the space before the DEPS_TARGET, if there is one. */
1977 s = index (spec, ' ');
1978 if (s) {
1979 deps_target = s + 1;
1980 output_file = xmalloc (s - spec + 1);
1981 bcopy (spec, output_file, s - spec);
1982 output_file[s - spec] = 0;
1983 } else {
1984 deps_target = 0;
1985 output_file = spec;
1988 deps_file = output_file;
1989 deps_mode = "a";
1992 /* For -M, print the expected object file name
1993 as the target of this Make-rule. */
1994 if (print_deps) {
1995 deps_allocated_size = 200;
1996 deps_buffer = xmalloc (deps_allocated_size);
1997 deps_buffer[0] = 0;
1998 deps_size = 0;
1999 deps_column = 0;
2001 if (deps_target) {
2002 deps_output (deps_target, ':');
2003 } else if (*in_fname == 0) {
2004 deps_output ("-", ':');
2005 } else {
2006 char *p, *q;
2007 int len;
2009 q = base_name (in_fname);
2011 /* Copy remainder to mungable area. */
2012 p = (char *) alloca (strlen(q) + 8);
2013 strcpy (p, q);
2015 /* Output P, but remove known suffixes. */
2016 len = strlen (p);
2017 q = p + len;
2018 if (len >= 2
2019 && p[len - 2] == '.'
2020 && index("cCsSm", p[len - 1]))
2021 q = p + (len - 2);
2022 else if (len >= 3
2023 && p[len - 3] == '.'
2024 && p[len - 2] == 'c'
2025 && p[len - 1] == 'c')
2026 q = p + (len - 3);
2027 else if (len >= 4
2028 && p[len - 4] == '.'
2029 && p[len - 3] == 'c'
2030 && p[len - 2] == 'x'
2031 && p[len - 1] == 'x')
2032 q = p + (len - 4);
2033 else if (len >= 4
2034 && p[len - 4] == '.'
2035 && p[len - 3] == 'c'
2036 && p[len - 2] == 'p'
2037 && p[len - 1] == 'p')
2038 q = p + (len - 4);
2040 /* Supply our own suffix. */
2041 strcpy (q, OBJECT_SUFFIX);
2043 deps_output (p, ':');
2046 deps_output (in_fname, ' ');
2049 /* Scan the -imacros files before the main input.
2050 Much like #including them, but with no_output set
2051 so that only their macro definitions matter. */
2053 no_output++; no_record_file++;
2054 for (i = 1; i < argc; i++)
2055 if (pend_files[i]) {
2056 struct include_file *inc;
2057 int fd = open_include_file (pend_files[i], NULL_PTR, NULL_PTR, &inc);
2058 if (fd < 0) {
2059 perror_with_name (pend_files[i]);
2060 return FATAL_EXIT_CODE;
2062 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2064 no_output--; no_record_file--;
2066 /* Copy the entire contents of the main input file into
2067 the stacked input buffer previously allocated for it. */
2069 /* JF check for stdin */
2070 if (in_fname == NULL || *in_fname == 0) {
2071 in_fname = "";
2072 f = 0;
2073 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
2074 goto perror;
2076 if (fstat (f, &st) != 0)
2077 pfatal_with_name (in_fname);
2078 fp->nominal_fname = fp->fname = in_fname;
2079 fp->nominal_fname_len = strlen (in_fname);
2080 fp->lineno = 1;
2081 fp->system_header_p = 0;
2082 /* JF all this is mine about reading pipes and ttys */
2083 if (! S_ISREG (st.st_mode)) {
2084 /* Read input from a file that is not a normal disk file.
2085 We cannot preallocate a buffer with the correct size,
2086 so we must read in the file a piece at the time and make it bigger. */
2087 int size;
2088 int bsize;
2089 int cnt;
2091 if (S_ISDIR (st.st_mode))
2092 fatal ("Input file `%s' is a directory", in_fname);
2094 bsize = 2000;
2095 size = 0;
2096 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2097 for (;;) {
2098 cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
2099 if (cnt < 0) goto perror; /* error! */
2100 size += cnt;
2101 if (size != bsize) break; /* End of file */
2102 bsize *= 2;
2103 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2105 fp->length = size;
2106 } else {
2107 /* Read a file whose size we can determine in advance.
2108 For the sake of VMS, st.st_size is just an upper bound. */
2109 size_t s = (size_t) st.st_size;
2110 if (s != st.st_size || s + 2 < s)
2111 memory_full ();
2112 fp->buf = (U_CHAR *) xmalloc (s + 2);
2113 fp->length = safe_read (f, (char *) fp->buf, s);
2114 if (fp->length < 0) goto perror;
2116 fp->bufp = fp->buf;
2117 fp->if_stack = if_stack;
2119 /* Make sure data ends with a newline. And put a null after it. */
2121 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
2122 /* Backslash-newline at end is not good enough. */
2123 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
2124 fp->buf[fp->length++] = '\n';
2125 missing_newline = 1;
2127 fp->buf[fp->length] = '\0';
2129 /* Unless inhibited, convert trigraphs in the input. */
2131 if (!no_trigraphs)
2132 trigraph_pcp (fp);
2134 if (warn_white_space)
2135 check_white_space (fp);
2137 /* Now that we know the input file is valid, open the output. */
2139 if (!out_fname || !strcmp (out_fname, ""))
2140 out_fname = "stdout";
2141 else if (! freopen (out_fname, "w", stdout))
2142 pfatal_with_name (out_fname);
2144 output_line_directive (fp, &outbuf, 0, same_file);
2146 /* Scan the -include files before the main input. */
2148 no_record_file++;
2149 for (i = 1; i < argc; i++)
2150 if (pend_includes[i]) {
2151 struct include_file *inc;
2152 int fd = open_include_file (pend_includes[i], NULL_PTR, NULL_PTR, &inc);
2153 if (fd < 0) {
2154 perror_with_name (pend_includes[i]);
2155 return FATAL_EXIT_CODE;
2157 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2159 no_record_file--;
2161 /* Scan the input, processing macros and directives. */
2163 rescan (&outbuf, 0);
2165 if (missing_newline)
2166 fp->lineno--;
2168 if (pedantic && missing_newline)
2169 pedwarn ("file does not end in newline");
2171 /* Now we have processed the entire input
2172 Write whichever kind of output has been requested. */
2174 if (dump_macros == dump_only)
2175 dump_all_macros ();
2176 else if (! inhibit_output) {
2177 write_output ();
2180 if (print_deps) {
2181 /* Don't actually write the deps file if compilation has failed. */
2182 if (errors == 0) {
2183 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
2184 pfatal_with_name (deps_file);
2185 fputs (deps_buffer, deps_stream);
2186 putc ('\n', deps_stream);
2187 if (deps_file) {
2188 if (ferror (deps_stream) || fclose (deps_stream) != 0)
2189 fatal ("I/O error on output");
2194 if (pcp_outfile && pcp_outfile != stdout
2195 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
2196 fatal ("I/O error on `-pcp' output");
2198 if (ferror (stdout) || fclose (stdout) != 0)
2199 fatal ("I/O error on output");
2201 if (errors)
2202 exit (FATAL_EXIT_CODE);
2203 exit (SUCCESS_EXIT_CODE);
2205 perror:
2206 pfatal_with_name (in_fname);
2207 return 0;
2210 /* Given a colon-separated list of file names PATH,
2211 add all the names to the search path for include files. */
2213 static void
2214 path_include (path)
2215 char *path;
2217 char *p;
2219 p = path;
2221 if (*p)
2222 while (1) {
2223 char *q = p;
2224 char c;
2225 struct file_name_list *dirtmp;
2227 /* Find the end of this name. */
2228 while ((c = *q++) != PATH_SEPARATOR && c)
2229 continue;
2231 q[-1] = 0;
2232 dirtmp = new_include_prefix (last_include, NULL_PTR,
2233 "", p == q ? "." : p);
2234 q[-1] = c;
2235 append_include_chain (dirtmp, dirtmp);
2237 /* Advance past this name. */
2238 p = q;
2239 if (! c)
2240 break;
2244 /* Return the address of the first character in S that equals C.
2245 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2246 Return 0 if there is no such character. Assume that C itself is not '\0'.
2247 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2248 but unfortunately memchr isn't autoconfigured yet. */
2250 static U_CHAR *
2251 index0 (s, c, n)
2252 U_CHAR *s;
2253 int c;
2254 size_t n;
2256 char *p = (char *) s;
2257 for (;;) {
2258 char *q = index (p, c);
2259 if (q)
2260 return (U_CHAR *) q;
2261 else {
2262 size_t l = strlen (p);
2263 if (l == n)
2264 return 0;
2265 l++;
2266 p += l;
2267 n -= l;
2272 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2273 before main CCCP processing. Name `pcp' is also in honor of the
2274 drugs the trigraph designers must have been on.
2276 Using an extra pass through the buffer takes a little extra time,
2277 but is infinitely less hairy than trying to handle trigraphs inside
2278 strings, etc. everywhere, and also makes sure that trigraphs are
2279 only translated in the top level of processing. */
2281 static void
2282 trigraph_pcp (buf)
2283 FILE_BUF *buf;
2285 register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
2286 int len;
2288 fptr = bptr = sptr = buf->buf;
2289 lptr = fptr + buf->length;
2290 while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
2291 if (*++sptr != '?')
2292 continue;
2293 switch (*++sptr) {
2294 case '=':
2295 c = '#';
2296 break;
2297 case '(':
2298 c = '[';
2299 break;
2300 case '/':
2301 c = '\\';
2302 break;
2303 case ')':
2304 c = ']';
2305 break;
2306 case '\'':
2307 c = '^';
2308 break;
2309 case '<':
2310 c = '{';
2311 break;
2312 case '!':
2313 c = '|';
2314 break;
2315 case '>':
2316 c = '}';
2317 break;
2318 case '-':
2319 c = '~';
2320 break;
2321 case '?':
2322 sptr--;
2323 continue;
2324 default:
2325 continue;
2327 len = sptr - fptr - 2;
2329 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2330 C, this will be memmove (). */
2331 if (bptr != fptr && len > 0)
2332 bcopy ((char *) fptr, (char *) bptr, len);
2334 bptr += len;
2335 *bptr++ = c;
2336 fptr = ++sptr;
2338 len = buf->length - (fptr - buf->buf);
2339 if (bptr != fptr && len > 0)
2340 bcopy ((char *) fptr, (char *) bptr, len);
2341 buf->length -= fptr - bptr;
2342 buf->buf[buf->length] = '\0';
2343 if (warn_trigraphs && fptr != bptr)
2344 warning_with_line (0, "%lu trigraph(s) encountered",
2345 (unsigned long) (fptr - bptr) / 2);
2348 /* Warn about white space between backslash and end of line. */
2350 static void
2351 check_white_space (buf)
2352 FILE_BUF *buf;
2354 register U_CHAR *sptr = buf->buf;
2355 register U_CHAR *lptr = sptr + buf->length;
2356 register U_CHAR *nptr;
2357 int line = 0;
2359 nptr = sptr = buf->buf;
2360 lptr = sptr + buf->length;
2361 for (nptr = sptr;
2362 (nptr = index0 (nptr, '\n', (size_t) (lptr - nptr))) != NULL;
2363 nptr ++) {
2364 register U_CHAR *p = nptr;
2365 line++;
2366 for (p = nptr; sptr < p; p--) {
2367 if (! is_hor_space[p[-1]]) {
2368 if (p[-1] == '\\' && p != nptr)
2369 warning_with_line (line,
2370 "`\\' followed by white space at end of line");
2371 break;
2377 /* Move all backslash-newline pairs out of embarrassing places.
2378 Exchange all such pairs following BP
2379 with any potentially-embarrassing characters that follow them.
2380 Potentially-embarrassing characters are / and *
2381 (because a backslash-newline inside a comment delimiter
2382 would cause it not to be recognized).
2383 We assume that *BP == '\\'. */
2385 static void
2386 newline_fix (bp)
2387 U_CHAR *bp;
2389 register U_CHAR *p = bp;
2391 /* First count the backslash-newline pairs here. */
2392 do {
2393 if (p[1] != '\n')
2394 break;
2395 p += 2;
2396 } while (*p == '\\');
2398 if (*p != '/' && *p != '*')
2399 /* What follows the backslash-newlines is not embarrassing. */
2400 return;
2402 /* Copy all potentially embarrassing characters
2403 that follow the backslash-newline pairs
2404 down to where the pairs originally started. */
2406 *bp++ = *p++;
2407 while (*p == '*' || *p == '/');
2409 /* Now write the same number of pairs after the embarrassing chars. */
2410 while (bp < p) {
2411 *bp++ = '\\';
2412 *bp++ = '\n';
2416 /* Like newline_fix but for use within a directive-name.
2417 Move any backslash-newlines up past any following symbol constituents. */
2419 static void
2420 name_newline_fix (bp)
2421 U_CHAR *bp;
2423 register U_CHAR *p = bp;
2425 /* First count the backslash-newline pairs here. */
2426 do {
2427 if (p[1] != '\n')
2428 break;
2429 p += 2;
2430 } while (*p == '\\');
2432 if (!is_idchar[*p])
2433 /* What follows the backslash-newlines is not embarrassing. */
2434 return;
2436 /* Copy all potentially embarrassing characters
2437 that follow the backslash-newline pairs
2438 down to where the pairs originally started. */
2440 *bp++ = *p++;
2441 while (is_idchar[*p]);
2443 /* Now write the same number of pairs after the embarrassing chars. */
2444 while (bp < p) {
2445 *bp++ = '\\';
2446 *bp++ = '\n';
2450 /* Look for lint commands in comments.
2452 When we come in here, ibp points into a comment. Limit is as one expects.
2453 scan within the comment -- it should start, after lwsp, with a lint command.
2454 If so that command is returned as a (constant) string.
2456 Upon return, any arg will be pointed to with argstart and will be
2457 arglen long. Note that we don't parse that arg since it will just
2458 be printed out again. */
2460 static char *
2461 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2462 register U_CHAR *ibp;
2463 register U_CHAR *limit;
2464 U_CHAR **argstart; /* point to command arg */
2465 int *arglen, *cmdlen; /* how long they are */
2467 HOST_WIDE_INT linsize;
2468 register U_CHAR *numptr; /* temp for arg parsing */
2470 *arglen = 0;
2472 SKIP_WHITE_SPACE (ibp);
2474 if (ibp >= limit) return NULL;
2476 linsize = limit - ibp;
2478 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2479 if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
2480 *cmdlen = 10;
2481 return "NOTREACHED";
2483 if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
2484 *cmdlen = 8;
2485 return "ARGSUSED";
2487 if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
2488 *cmdlen = 11;
2489 return "LINTLIBRARY";
2491 if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
2492 *cmdlen = 7;
2493 ibp += 7; linsize -= 7;
2494 if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
2496 /* OK, read a number */
2497 for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
2498 numptr++);
2499 *arglen = numptr - *argstart;
2500 return "VARARGS";
2502 return NULL;
2506 * The main loop of the program.
2508 * Read characters from the input stack, transferring them to the
2509 * output buffer OP.
2511 * Macros are expanded and push levels on the input stack.
2512 * At the end of such a level it is popped off and we keep reading.
2513 * At the end of any other kind of level, we return.
2514 * #-directives are handled, except within macros.
2516 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2517 * and insert them when appropriate. This is set while scanning macro
2518 * arguments before substitution. It is zero when scanning for final output.
2519 * There are two types of Newline markers:
2520 * * Newline - follows a macro name that was not expanded
2521 * because it appeared inside an expansion of the same macro.
2522 * This marker prevents future expansion of that identifier.
2523 * When the input is rescanned into the final output, these are deleted.
2524 * These are also deleted by ## concatenation.
2525 * * Newline Space (or Newline and any other whitespace character)
2526 * stands for a place that tokens must be separated or whitespace
2527 * is otherwise desirable, but where the ANSI standard specifies there
2528 * is no whitespace. This marker turns into a Space (or whichever other
2529 * whitespace char appears in the marker) in the final output,
2530 * but it turns into nothing in an argument that is stringified with #.
2531 * Such stringified arguments are the only place where the ANSI standard
2532 * specifies with precision that whitespace may not appear.
2534 * During this function, IP->bufp is kept cached in IBP for speed of access.
2535 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2536 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2537 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2538 * explicitly, and before RECACHE, since RECACHE uses OBP.
2541 static void
2542 rescan (op, output_marks)
2543 FILE_BUF *op;
2544 int output_marks;
2546 /* Character being scanned in main loop. */
2547 register U_CHAR c;
2549 /* Length of pending accumulated identifier. */
2550 register int ident_length = 0;
2552 /* Hash code of pending accumulated identifier. */
2553 register int hash = 0;
2555 /* Current input level (&instack[indepth]). */
2556 FILE_BUF *ip;
2558 /* Pointer for scanning input. */
2559 register U_CHAR *ibp;
2561 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2562 register U_CHAR *limit;
2564 /* Pointer for storing output. */
2565 register U_CHAR *obp;
2567 /* REDO_CHAR is nonzero if we are processing an identifier
2568 after backing up over the terminating character.
2569 Sometimes we process an identifier without backing up over
2570 the terminating character, if the terminating character
2571 is not special. Backing up is done so that the terminating character
2572 will be dispatched on again once the identifier is dealt with. */
2573 int redo_char = 0;
2575 /* 1 if within an identifier inside of which a concatenation
2576 marker (Newline -) has been seen. */
2577 int concatenated = 0;
2579 /* While scanning a comment or a string constant,
2580 this records the line it started on, for error messages. */
2581 int start_line;
2583 /* Record position of last `real' newline. */
2584 U_CHAR *beg_of_line;
2586 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2588 #define POPMACRO \
2589 do { ip->macro->type = T_MACRO; \
2590 if (ip->free_ptr) free (ip->free_ptr); \
2591 --indepth; } while (0)
2593 /* Reload `rescan's local variables that describe the current
2594 level of the input stack. */
2596 #define RECACHE \
2597 do { ip = &instack[indepth]; \
2598 ibp = ip->bufp; \
2599 limit = ip->buf + ip->length; \
2600 op->bufp = obp; \
2601 check_expand (op, limit - ibp); \
2602 beg_of_line = 0; \
2603 obp = op->bufp; } while (0)
2605 if (no_output && instack[indepth].fname != 0)
2606 skip_if_group (&instack[indepth], 1, NULL);
2608 obp = op->bufp;
2609 RECACHE;
2611 beg_of_line = ibp;
2613 /* Our caller must always put a null after the end of
2614 the input at each input stack level. */
2615 if (*limit != 0)
2616 abort ();
2618 while (1) {
2619 c = *ibp++;
2620 *obp++ = c;
2622 switch (c) {
2623 case '\\':
2624 if (*ibp == '\n' && !ip->macro) {
2625 /* At the top level, always merge lines ending with backslash-newline,
2626 even in middle of identifier. But do not merge lines in a macro,
2627 since backslash might be followed by a newline-space marker. */
2628 ++ibp;
2629 ++ip->lineno;
2630 --obp; /* remove backslash from obuf */
2631 break;
2633 /* If ANSI, backslash is just another character outside a string. */
2634 if (!traditional)
2635 goto randomchar;
2636 /* Otherwise, backslash suppresses specialness of following char,
2637 so copy it here to prevent the switch from seeing it.
2638 But first get any pending identifier processed. */
2639 if (ident_length > 0)
2640 goto specialchar;
2641 if (ibp < limit)
2642 *obp++ = *ibp++;
2643 break;
2645 case '%':
2646 if (ident_length || ip->macro || traditional)
2647 goto randomchar;
2648 while (*ibp == '\\' && ibp[1] == '\n') {
2649 ibp += 2;
2650 ++ip->lineno;
2652 if (*ibp != ':')
2653 break;
2654 /* Treat this %: digraph as if it were #. */
2655 /* Fall through. */
2657 case '#':
2658 if (assertions_flag) {
2659 if (ident_length)
2660 goto specialchar;
2661 /* Copy #foo (bar lose) without macro expansion. */
2662 obp[-1] = '#'; /* In case it was '%'. */
2663 SKIP_WHITE_SPACE (ibp);
2664 while (is_idchar[*ibp])
2665 *obp++ = *ibp++;
2666 SKIP_WHITE_SPACE (ibp);
2667 if (*ibp == '(') {
2668 ip->bufp = ibp;
2669 skip_paren_group (ip);
2670 bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
2671 obp += ip->bufp - ibp;
2672 ibp = ip->bufp;
2674 break;
2677 /* If this is expanding a macro definition, don't recognize
2678 preprocessing directives. */
2679 if (ip->macro != 0)
2680 goto randomchar;
2681 /* If this is expand_into_temp_buffer,
2682 don't recognize them either. Warn about them
2683 only after an actual newline at this level,
2684 not at the beginning of the input level. */
2685 if (! ip->fname) {
2686 if (ip->buf != beg_of_line)
2687 warning ("preprocessing directive not recognized within macro arg");
2688 goto randomchar;
2690 if (ident_length)
2691 goto specialchar;
2694 /* # keyword: a # must be first nonblank char on the line */
2695 if (beg_of_line == 0)
2696 goto randomchar;
2698 U_CHAR *bp;
2700 /* Scan from start of line, skipping whitespace, comments
2701 and backslash-newlines, and see if we reach this #.
2702 If not, this # is not special. */
2703 bp = beg_of_line;
2704 /* If -traditional, require # to be at beginning of line. */
2705 if (!traditional) {
2706 while (1) {
2707 if (is_hor_space[*bp])
2708 bp++;
2709 else if (*bp == '\\' && bp[1] == '\n')
2710 bp += 2;
2711 else if (*bp == '/' && bp[1] == '*') {
2712 bp += 2;
2713 while (1)
2715 if (*bp == '*')
2717 if (bp[1] == '/')
2719 bp += 2;
2720 break;
2723 else
2725 #ifdef MULTIBYTE_CHARS
2726 int length;
2727 length = local_mblen (bp, limit - bp);
2728 if (length > 1)
2729 bp += (length - 1);
2730 #endif
2732 bp++;
2735 /* There is no point in trying to deal with C++ // comments here,
2736 because if there is one, then this # must be part of the
2737 comment and we would never reach here. */
2738 else break;
2740 if (c == '%') {
2741 if (bp[0] != '%')
2742 break;
2743 while (bp[1] == '\\' && bp[2] == '\n')
2744 bp += 2;
2745 if (bp + 1 != ibp)
2746 break;
2747 /* %: appears at start of line; skip past the ':' too. */
2748 bp++;
2749 ibp++;
2752 if (bp + 1 != ibp)
2753 goto randomchar;
2756 /* This # can start a directive. */
2758 --obp; /* Don't copy the '#' */
2760 ip->bufp = ibp;
2761 op->bufp = obp;
2762 if (! handle_directive (ip, op)) {
2763 #ifdef USE_C_ALLOCA
2764 alloca (0);
2765 #endif
2766 /* Not a known directive: treat it as ordinary text.
2767 IP, OP, IBP, etc. have not been changed. */
2768 if (no_output && instack[indepth].fname) {
2769 /* If not generating expanded output,
2770 what we do with ordinary text is skip it.
2771 Discard everything until next # directive. */
2772 skip_if_group (&instack[indepth], 1, 0);
2773 RECACHE;
2774 beg_of_line = ibp;
2775 break;
2777 *obp++ = '#'; /* Copy # (even if it was originally %:). */
2778 /* Don't expand an identifier that could be a macro directive.
2779 (Section 3.8.3 of the ANSI C standard) */
2780 SKIP_WHITE_SPACE (ibp);
2781 if (is_idstart[*ibp])
2783 *obp++ = *ibp++;
2784 while (is_idchar[*ibp])
2785 *obp++ = *ibp++;
2787 goto randomchar;
2789 #ifdef USE_C_ALLOCA
2790 alloca (0);
2791 #endif
2792 /* A # directive has been successfully processed. */
2793 /* If not generating expanded output, ignore everything until
2794 next # directive. */
2795 if (no_output && instack[indepth].fname)
2796 skip_if_group (&instack[indepth], 1, 0);
2797 obp = op->bufp;
2798 RECACHE;
2799 beg_of_line = ibp;
2800 break;
2802 case '\"': /* skip quoted string */
2803 case '\'':
2804 /* A single quoted string is treated like a double -- some
2805 programs (e.g., troff) are perverse this way */
2807 /* Handle any pending identifier;
2808 but the L in L'...' or L"..." is not an identifier. */
2809 if (ident_length) {
2810 if (! (ident_length == 1 && hash == HASHSTEP (0, 'L')))
2811 goto specialchar;
2812 ident_length = hash = 0;
2815 start_line = ip->lineno;
2817 /* Skip ahead to a matching quote. */
2819 while (1) {
2820 if (ibp >= limit) {
2821 if (ip->macro != 0) {
2822 /* try harder: this string crosses a macro expansion boundary.
2823 This can happen naturally if -traditional.
2824 Otherwise, only -D can make a macro with an unmatched quote. */
2825 POPMACRO;
2826 RECACHE;
2827 continue;
2829 if (!traditional) {
2830 error_with_line (line_for_error (start_line),
2831 "unterminated string or character constant");
2832 if (multiline_string_line) {
2833 error_with_line (multiline_string_line,
2834 "possible real start of unterminated constant");
2835 multiline_string_line = 0;
2838 break;
2840 *obp++ = *ibp;
2841 switch (*ibp++) {
2842 case '\n':
2843 if (warn_white_space && ip->fname && is_hor_space[ibp[-2]])
2844 warning ("white space at end of line in string");
2845 ++ip->lineno;
2846 ++op->lineno;
2847 /* Traditionally, end of line ends a string constant with no error.
2848 So exit the loop and record the new line. */
2849 if (traditional) {
2850 beg_of_line = ibp;
2851 goto while2end;
2853 if (c == '\'') {
2854 error_with_line (line_for_error (start_line),
2855 "unterminated character constant");
2856 goto while2end;
2858 if (multiline_string_line == 0) {
2859 if (pedantic)
2860 pedwarn_with_line (line_for_error (start_line),
2861 "string constant runs past end of line");
2862 multiline_string_line = ip->lineno - 1;
2864 break;
2866 case '\\':
2867 if (*ibp == '\n') {
2868 /* Backslash newline is replaced by nothing at all, but
2869 keep the line counts correct. But if we are reading
2870 from a macro, keep the backslash newline, since backslash
2871 newlines have already been processed. */
2872 if (ip->macro) {
2873 *obp++ = '\n';
2874 ++op->lineno;
2875 } else
2876 --obp;
2877 ++ibp;
2878 ++ip->lineno;
2879 } else {
2880 /* ANSI stupidly requires that in \\ the second \
2881 is *not* prevented from combining with a newline. */
2882 if (!ip->macro) {
2883 while (*ibp == '\\' && ibp[1] == '\n') {
2884 *obp++ = *ibp++;
2885 *obp++ = *ibp++;
2886 ++ip->lineno;
2887 ++op->lineno;
2890 *obp++ = *ibp++;
2892 break;
2894 case '\"':
2895 case '\'':
2896 if (ibp[-1] == c)
2897 goto while2end;
2898 break;
2899 #ifdef MULTIBYTE_CHARS
2900 default:
2902 int length;
2903 --ibp;
2904 length = local_mblen (ibp, limit - ibp);
2905 if (length > 0)
2907 --obp;
2908 bcopy (ibp, obp, length);
2909 obp += length;
2910 ibp += length;
2912 else
2913 ++ibp;
2915 break;
2916 #endif
2919 while2end:
2920 break;
2922 case '/':
2923 if (ip->macro != 0)
2924 goto randomchar;
2925 if (*ibp == '\\')
2926 newline_fix (ibp);
2927 if (*ibp != '*'
2928 && !(cplusplus_comments && *ibp == '/'))
2929 goto randomchar;
2930 if (ident_length)
2931 goto specialchar;
2933 if (*ibp == '/') {
2934 /* C++ style comment... */
2935 start_line = ip->lineno;
2937 /* Comments are equivalent to spaces. */
2938 if (! put_out_comments)
2939 obp[-1] = ' ';
2942 U_CHAR *before_bp = ibp;
2944 while (++ibp < limit) {
2945 if (*ibp == '\n') {
2946 if (ibp[-1] != '\\') {
2947 if (put_out_comments) {
2948 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
2949 obp += ibp - before_bp;
2951 break;
2953 if (warn_comments)
2954 warning ("multiline `//' comment");
2955 ++ip->lineno;
2956 /* Copy the newline into the output buffer, in order to
2957 avoid the pain of a #line every time a multiline comment
2958 is seen. */
2959 if (!put_out_comments)
2960 *obp++ = '\n';
2961 ++op->lineno;
2963 else
2965 #ifdef MULTIBYTE_CHARS
2966 int length;
2967 length = local_mblen (ibp, limit - ibp);
2968 if (length > 1)
2969 ibp += (length - 1);
2970 #endif
2973 break;
2977 /* Ordinary C comment. Skip it, optionally copying it to output. */
2979 start_line = ip->lineno;
2981 ++ibp; /* Skip the star. */
2983 /* If this cpp is for lint, we peek inside the comments: */
2984 if (for_lint) {
2985 U_CHAR *argbp;
2986 int cmdlen, arglen;
2987 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2989 if (lintcmd != NULL) {
2990 op->bufp = obp;
2991 check_expand (op, cmdlen + arglen + 14);
2992 obp = op->bufp;
2993 /* I believe it is always safe to emit this newline: */
2994 obp[-1] = '\n';
2995 bcopy ("#pragma lint ", (char *) obp, 13);
2996 obp += 13;
2997 bcopy (lintcmd, (char *) obp, cmdlen);
2998 obp += cmdlen;
3000 if (arglen != 0) {
3001 *(obp++) = ' ';
3002 bcopy (argbp, (char *) obp, arglen);
3003 obp += arglen;
3006 /* OK, now bring us back to the state we were in before we entered
3007 this branch. We need #line because the #pragma's newline always
3008 messes up the line count. */
3009 op->bufp = obp;
3010 output_line_directive (ip, op, 0, same_file);
3011 check_expand (op, limit - ibp + 2);
3012 obp = op->bufp;
3013 *(obp++) = '/';
3017 /* Comments are equivalent to spaces.
3018 Note that we already output the slash; we might not want it.
3019 For -traditional, a comment is equivalent to nothing. */
3020 if (! put_out_comments) {
3021 if (traditional)
3022 obp--;
3023 else
3024 obp[-1] = ' ';
3026 else
3027 *obp++ = '*';
3030 U_CHAR *before_bp = ibp;
3032 for (;;) {
3033 switch (*ibp++) {
3034 case '*':
3035 if (ibp[-2] == '/' && warn_comments)
3036 warning ("`/*' within comment");
3037 if (*ibp == '\\')
3038 newline_fix (ibp);
3039 if (*ibp == '/')
3040 goto comment_end;
3041 break;
3043 case '\n':
3044 ++ip->lineno;
3045 /* Copy the newline into the output buffer, in order to
3046 avoid the pain of a #line every time a multiline comment
3047 is seen. */
3048 if (!put_out_comments)
3049 *obp++ = '\n';
3050 ++op->lineno;
3051 break;
3053 case 0:
3054 if (limit < ibp) {
3055 error_with_line (line_for_error (start_line),
3056 "unterminated comment");
3057 goto limit_reached;
3059 break;
3060 #ifdef MULTIBYTE_CHARS
3061 default:
3063 int length;
3064 length = local_mblen (ibp, limit - ibp);
3065 if (length > 1)
3066 ibp += (length - 1);
3068 break;
3069 #endif
3072 comment_end:
3074 ibp++;
3075 if (put_out_comments) {
3076 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
3077 obp += ibp - before_bp;
3080 break;
3082 case '$':
3083 if (! is_idchar['$'])
3084 goto randomchar;
3085 if (pedantic)
3086 pedwarn ("`$' in identifier");
3087 goto letter;
3089 case '0': case '1': case '2': case '3': case '4':
3090 case '5': case '6': case '7': case '8': case '9':
3091 /* If digit is not part of identifier, it starts a number,
3092 which means that following letters are not an identifier.
3093 "0x5" does not refer to an identifier "x5".
3094 So copy all alphanumerics that follow without accumulating
3095 as an identifier. Periods also, for sake of "3.e7". */
3097 if (ident_length == 0) {
3098 for (;;) {
3099 if (!ip->macro) {
3100 while (ibp[0] == '\\' && ibp[1] == '\n') {
3101 ++ip->lineno;
3102 ibp += 2;
3105 c = *ibp++;
3106 if (!is_idchar[c] && c != '.') {
3107 --ibp;
3108 break;
3110 *obp++ = c;
3111 /* A sign can be part of a preprocessing number
3112 if it follows an `e' or `p'. */
3113 if (c == 'e' || c == 'E' || c == 'p' || c == 'P') {
3114 if (!ip->macro) {
3115 while (ibp[0] == '\\' && ibp[1] == '\n') {
3116 ++ip->lineno;
3117 ibp += 2;
3120 if (*ibp == '+' || *ibp == '-') {
3121 *obp++ = *ibp++;
3122 /* But traditional C does not let the token go past the sign,
3123 and C89 does not allow `p'. */
3124 if (traditional || (c89 && (c == 'p' || c == 'P')))
3125 break;
3129 break;
3131 /* fall through */
3133 case '_':
3134 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3135 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3136 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3137 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3138 case 'y': case 'z':
3139 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3140 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3141 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3142 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3143 case 'Y': case 'Z':
3144 letter:
3145 ident_length++;
3146 /* Compute step of hash function, to avoid a proc call on every token */
3147 hash = HASHSTEP (hash, c);
3148 break;
3150 case '\n':
3151 if (ip->fname == 0 && *ibp == '-') {
3152 /* Newline - inhibits expansion of preceding token.
3153 If expanding a macro arg, we keep the newline -.
3154 In final output, it is deleted.
3155 We recognize Newline - in macro bodies and macro args. */
3156 if (! concatenated) {
3157 ident_length = 0;
3158 hash = 0;
3160 ibp++;
3161 if (!output_marks) {
3162 obp--;
3163 } else {
3164 /* If expanding a macro arg, keep the newline -. */
3165 *obp++ = '-';
3167 break;
3170 /* If reprocessing a macro expansion, newline is a special marker. */
3171 else if (ip->macro != 0) {
3172 /* Newline White is a "funny space" to separate tokens that are
3173 supposed to be separate but without space between.
3174 Here White means any whitespace character.
3175 Newline - marks a recursive macro use that is not
3176 supposed to be expandable. */
3178 if (is_space[*ibp]) {
3179 /* Newline Space does not prevent expansion of preceding token
3180 so expand the preceding token and then come back. */
3181 if (ident_length > 0)
3182 goto specialchar;
3184 /* If generating final output, newline space makes a space. */
3185 if (!output_marks) {
3186 obp[-1] = *ibp++;
3187 /* And Newline Newline makes a newline, so count it. */
3188 if (obp[-1] == '\n')
3189 op->lineno++;
3190 } else {
3191 /* If expanding a macro arg, keep the newline space.
3192 If the arg gets stringified, newline space makes nothing. */
3193 *obp++ = *ibp++;
3195 } else abort (); /* Newline followed by something random? */
3196 break;
3199 /* If there is a pending identifier, handle it and come back here. */
3200 if (ident_length > 0)
3201 goto specialchar;
3203 beg_of_line = ibp;
3205 /* Update the line counts and output a #line if necessary. */
3206 ++ip->lineno;
3207 ++op->lineno;
3208 if (ip->lineno != op->lineno) {
3209 op->bufp = obp;
3210 output_line_directive (ip, op, 1, same_file);
3211 check_expand (op, limit - ibp);
3212 obp = op->bufp;
3214 break;
3216 /* Come here either after (1) a null character that is part of the input
3217 or (2) at the end of the input, because there is a null there. */
3218 case 0:
3219 if (ibp <= limit)
3220 /* Our input really contains a null character. */
3221 goto randomchar;
3223 limit_reached:
3224 /* At end of a macro-expansion level, pop it and read next level. */
3225 if (ip->macro != 0) {
3226 obp--;
3227 ibp--;
3228 /* If traditional, and we have an identifier that ends here,
3229 process it now, so we get the right error for recursion. */
3230 if (traditional && ident_length
3231 && ! is_idchar[*instack[indepth - 1].bufp]) {
3232 redo_char = 1;
3233 goto randomchar;
3235 POPMACRO;
3236 RECACHE;
3237 break;
3240 /* If we don't have a pending identifier,
3241 return at end of input. */
3242 if (ident_length == 0) {
3243 obp--;
3244 ibp--;
3245 op->bufp = obp;
3246 ip->bufp = ibp;
3247 goto ending;
3250 /* If we do have a pending identifier, just consider this null
3251 a special character and arrange to dispatch on it again.
3252 The second time, IDENT_LENGTH will be zero so we will return. */
3254 /* Fall through */
3256 specialchar:
3258 /* Handle the case of a character such as /, ', " or null
3259 seen following an identifier. Back over it so that
3260 after the identifier is processed the special char
3261 will be dispatched on again. */
3263 ibp--;
3264 obp--;
3265 redo_char = 1;
3267 default:
3269 randomchar:
3271 if (ident_length > 0) {
3272 register HASHNODE *hp;
3274 /* We have just seen an identifier end. If it's a macro, expand it.
3276 IDENT_LENGTH is the length of the identifier
3277 and HASH is its hash code.
3279 The identifier has already been copied to the output,
3280 so if it is a macro we must remove it.
3282 If REDO_CHAR is 0, the char that terminated the identifier
3283 has been skipped in the output and the input.
3284 OBP-IDENT_LENGTH-1 points to the identifier.
3285 If the identifier is a macro, we must back over the terminator.
3287 If REDO_CHAR is 1, the terminating char has already been
3288 backed over. OBP-IDENT_LENGTH points to the identifier. */
3290 if (!pcp_outfile || pcp_inside_if) {
3291 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
3292 hp = hp->next) {
3294 if (hp->length == ident_length) {
3295 int obufp_before_macroname;
3296 int op_lineno_before_macroname;
3297 register int i = ident_length;
3298 register U_CHAR *p = hp->name;
3299 register U_CHAR *q = obp - i;
3300 int disabled;
3302 if (! redo_char)
3303 q--;
3305 do { /* All this to avoid a strncmp () */
3306 if (*p++ != *q++)
3307 goto hashcollision;
3308 } while (--i);
3310 /* We found a use of a macro name.
3311 see if the context shows it is a macro call. */
3313 /* Back up over terminating character if not already done. */
3314 if (! redo_char) {
3315 ibp--;
3316 obp--;
3319 /* Save this as a displacement from the beginning of the output
3320 buffer. We can not save this as a position in the output
3321 buffer, because it may get realloc'ed by RECACHE. */
3322 obufp_before_macroname = (obp - op->buf) - ident_length;
3323 op_lineno_before_macroname = op->lineno;
3325 if (hp->type == T_PCSTRING) {
3326 pcstring_used (hp); /* Mark the definition of this key
3327 as needed, ensuring that it
3328 will be output. */
3329 break; /* Exit loop, since the key cannot have a
3330 definition any longer. */
3333 /* Record whether the macro is disabled. */
3334 disabled = hp->type == T_DISABLED;
3336 /* This looks like a macro ref, but if the macro was disabled,
3337 just copy its name and put in a marker if requested. */
3339 if (disabled) {
3340 #if 0
3341 /* This error check caught useful cases such as
3342 #define foo(x,y) bar (x (y,0), y)
3343 foo (foo, baz) */
3344 if (traditional)
3345 error ("recursive use of macro `%s'", hp->name);
3346 #endif
3348 if (output_marks) {
3349 check_expand (op, limit - ibp + 2);
3350 *obp++ = '\n';
3351 *obp++ = '-';
3353 break;
3356 /* If macro wants an arglist, verify that a '(' follows.
3357 first skip all whitespace, copying it to the output
3358 after the macro name. Then, if there is no '(',
3359 decide this is not a macro call and leave things that way. */
3360 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3361 && hp->value.defn->nargs >= 0)
3363 U_CHAR *old_ibp = ibp;
3364 U_CHAR *old_obp = obp;
3365 int old_iln = ip->lineno;
3366 int old_oln = op->lineno;
3368 while (1) {
3369 /* Scan forward over whitespace, copying it to the output. */
3370 if (ibp == limit && ip->macro != 0) {
3371 POPMACRO;
3372 RECACHE;
3373 old_ibp = ibp;
3374 old_obp = obp;
3375 old_iln = ip->lineno;
3376 old_oln = op->lineno;
3378 else if (is_space[*ibp]) {
3379 *obp++ = *ibp++;
3380 if (ibp[-1] == '\n') {
3381 if (ip->macro == 0) {
3382 /* Newline in a file. Count it. */
3383 ++ip->lineno;
3384 ++op->lineno;
3385 } else if (!output_marks) {
3386 /* A newline mark, and we don't want marks
3387 in the output. If it is newline-hyphen,
3388 discard it entirely. Otherwise, it is
3389 newline-whitechar, so keep the whitechar. */
3390 obp--;
3391 if (*ibp == '-')
3392 ibp++;
3393 else {
3394 if (*ibp == '\n')
3395 ++op->lineno;
3396 *obp++ = *ibp++;
3398 } else {
3399 /* A newline mark; copy both chars to the output. */
3400 *obp++ = *ibp++;
3404 else if (ip->macro)
3405 break;
3406 else if (*ibp == '/') {
3407 /* If a comment, copy it unchanged or discard it. */
3408 if (ibp[1] == '\\')
3409 newline_fix (ibp + 1);
3410 if (ibp[1] == '*') {
3411 if (put_out_comments) {
3412 *obp++ = '/';
3413 *obp++ = '*';
3414 } else if (! traditional) {
3415 *obp++ = ' ';
3417 for (ibp += 2; ibp < limit; ibp++) {
3418 /* We need not worry about newline-marks,
3419 since they are never found in comments. */
3420 if (ibp[0] == '*') {
3421 if (ibp[1] == '\\')
3422 newline_fix (ibp + 1);
3423 if (ibp[1] == '/') {
3424 ibp += 2;
3425 if (put_out_comments) {
3426 *obp++ = '*';
3427 *obp++ = '/';
3429 break;
3432 else if (*ibp == '\n') {
3433 /* Newline in a file. Count it. */
3434 ++ip->lineno;
3435 ++op->lineno;
3437 else
3439 #ifdef MULTIBYTE_CHARS
3440 int length;
3441 length = local_mblen (ibp, limit - ibp);
3442 if (length > 1)
3444 if (put_out_comments)
3446 bcopy (ibp, obp, length - 1);
3447 obp += length - 1;
3449 ibp += (length - 1);
3451 #endif
3453 if (put_out_comments)
3454 *obp++ = *ibp;
3456 } else if (ibp[1] == '/' && cplusplus_comments) {
3457 if (put_out_comments) {
3458 *obp++ = '/';
3459 *obp++ = '/';
3460 } else if (! traditional) {
3461 *obp++ = ' ';
3463 for (ibp += 2; ; ibp++)
3465 if (*ibp == '\n')
3467 if (ibp[-1] != '\\')
3468 break;
3470 else
3472 #ifdef MULTIBYTE_CHARS
3473 int length;
3474 length = local_mblen (ibp, limit - ibp);
3475 if (length > 1)
3477 if (put_out_comments)
3479 bcopy (ibp, obp, length - 1);
3480 obp += length - 1;
3482 ibp += (length - 1);
3484 #endif
3486 if (put_out_comments)
3487 *obp++ = *ibp;
3489 } else
3490 break;
3492 else if (ibp[0] == '\\' && ibp[1] == '\n') {
3493 ibp += 2;
3494 ++ip->lineno;
3496 else break;
3498 if (*ibp != '(') {
3499 /* It isn't a macro call.
3500 Put back the space that we just skipped. */
3501 ibp = old_ibp;
3502 obp = old_obp;
3503 ip->lineno = old_iln;
3504 op->lineno = old_oln;
3505 /* Exit the for loop. */
3506 break;
3510 /* This is now known to be a macro call.
3511 Discard the macro name from the output,
3512 along with any following whitespace just copied,
3513 but preserve newlines if not outputting marks since this
3514 is more likely to do the right thing with line numbers. */
3515 obp = op->buf + obufp_before_macroname;
3516 if (output_marks)
3517 op->lineno = op_lineno_before_macroname;
3518 else {
3519 int newlines = op->lineno - op_lineno_before_macroname;
3520 while (0 < newlines--)
3521 *obp++ = '\n';
3524 /* Prevent accidental token-pasting with a character
3525 before the macro call. */
3526 if (!traditional && obp != op->buf) {
3527 switch (obp[-1]) {
3528 case '!': case '%': case '&': case '*':
3529 case '+': case '-': case '.': case '/':
3530 case ':': case '<': case '=': case '>':
3531 case '^': case '|':
3532 /* If we are expanding a macro arg, make a newline marker
3533 to separate the tokens. If we are making real output,
3534 a plain space will do. */
3535 if (output_marks)
3536 *obp++ = '\n';
3537 *obp++ = ' ';
3541 /* Expand the macro, reading arguments as needed,
3542 and push the expansion on the input stack. */
3543 ip->bufp = ibp;
3544 op->bufp = obp;
3545 macroexpand (hp, op);
3547 /* Reexamine input stack, since macroexpand has pushed
3548 a new level on it. */
3549 obp = op->bufp;
3550 RECACHE;
3551 break;
3553 hashcollision:
3555 } /* End hash-table-search loop */
3557 ident_length = hash = 0; /* Stop collecting identifier */
3558 redo_char = 0;
3559 concatenated = 0;
3560 } /* End if (ident_length > 0) */
3561 } /* End switch */
3562 } /* End per-char loop */
3564 /* Come here to return -- but first give an error message
3565 if there was an unterminated successful conditional. */
3566 ending:
3567 if (if_stack != ip->if_stack)
3569 char *str;
3571 switch (if_stack->type)
3573 case T_IF:
3574 str = "if";
3575 break;
3576 case T_IFDEF:
3577 str = "ifdef";
3578 break;
3579 case T_IFNDEF:
3580 str = "ifndef";
3581 break;
3582 case T_ELSE:
3583 str = "else";
3584 break;
3585 case T_ELIF:
3586 str = "elif";
3587 break;
3588 default:
3589 abort ();
3592 error_with_line (line_for_error (if_stack->lineno),
3593 "unterminated `#%s' conditional", str);
3595 if_stack = ip->if_stack;
3599 * Rescan a string into a temporary buffer and return the result
3600 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3602 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3603 * and insert such markers when appropriate. See `rescan' for details.
3604 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3605 * before substitution; it is 0 for other uses.
3607 static FILE_BUF
3608 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3609 U_CHAR *buf, *limit;
3610 int output_marks, assertions;
3612 register FILE_BUF *ip;
3613 FILE_BUF obuf;
3614 int length = limit - buf;
3615 U_CHAR *buf1;
3616 int odepth = indepth;
3617 int save_assertions_flag = assertions_flag;
3619 assertions_flag = assertions;
3621 if (length < 0)
3622 abort ();
3624 /* Set up the input on the input stack. */
3626 buf1 = (U_CHAR *) alloca (length + 1);
3628 register U_CHAR *p1 = buf;
3629 register U_CHAR *p2 = buf1;
3631 while (p1 != limit)
3632 *p2++ = *p1++;
3634 buf1[length] = 0;
3636 /* Set up to receive the output. */
3638 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3639 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3640 obuf.fname = 0;
3641 obuf.macro = 0;
3642 obuf.free_ptr = 0;
3644 CHECK_DEPTH ({return obuf;});
3646 ++indepth;
3648 ip = &instack[indepth];
3649 ip->fname = 0;
3650 ip->nominal_fname = 0;
3651 ip->nominal_fname_len = 0;
3652 ip->inc = 0;
3653 ip->system_header_p = 0;
3654 ip->macro = 0;
3655 ip->free_ptr = 0;
3656 ip->length = length;
3657 ip->buf = ip->bufp = buf1;
3658 ip->if_stack = if_stack;
3660 ip->lineno = obuf.lineno = 1;
3662 /* Scan the input, create the output. */
3663 rescan (&obuf, output_marks);
3665 /* Pop input stack to original state. */
3666 --indepth;
3668 if (indepth != odepth)
3669 abort ();
3671 assertions_flag = save_assertions_flag;
3672 return obuf;
3676 * Process a # directive. Expects IP->bufp to point after the '#', as in
3677 * `#define foo bar'. Passes to the directive handler
3678 * (do_define, do_include, etc.): the addresses of the 1st and
3679 * last chars of the directive (starting immediately after the #
3680 * keyword), plus op and the keyword table pointer. If the directive
3681 * contains comments it is copied into a temporary buffer sans comments
3682 * and the temporary buffer is passed to the directive handler instead.
3683 * Likewise for backslash-newlines.
3685 * Returns nonzero if this was a known # directive.
3686 * Otherwise, returns zero, without advancing the input pointer.
3689 static int
3690 handle_directive (ip, op)
3691 FILE_BUF *ip, *op;
3693 register U_CHAR *bp, *cp;
3694 register struct directive *kt;
3695 register int ident_length;
3696 U_CHAR *resume_p;
3698 /* Nonzero means we must copy the entire directive
3699 to get rid of comments or backslash-newlines. */
3700 int copy_directive = 0;
3702 U_CHAR *ident, *after_ident;
3704 bp = ip->bufp;
3706 /* Record where the directive started. do_xifdef needs this. */
3707 directive_start = bp - 1;
3709 /* Skip whitespace and \-newline. */
3710 while (1) {
3711 if (is_hor_space[*bp]) {
3712 if (*bp != ' ' && *bp != '\t' && pedantic)
3713 pedwarn_strange_white_space (*bp);
3714 bp++;
3715 } else if (*bp == '/') {
3716 if (bp[1] == '\\')
3717 newline_fix (bp + 1);
3718 if (! (bp[1] == '*' || (cplusplus_comments && bp[1] == '/')))
3719 break;
3720 ip->bufp = bp + 2;
3721 skip_to_end_of_comment (ip, &ip->lineno, 0);
3722 bp = ip->bufp;
3723 } else if (*bp == '\\' && bp[1] == '\n') {
3724 bp += 2; ip->lineno++;
3725 } else break;
3728 /* Now find end of directive name.
3729 If we encounter a backslash-newline, exchange it with any following
3730 symbol-constituents so that we end up with a contiguous name. */
3732 cp = bp;
3733 while (1) {
3734 if (is_idchar[*cp])
3735 cp++;
3736 else {
3737 if (*cp == '\\')
3738 name_newline_fix (cp);
3739 if (is_idchar[*cp])
3740 cp++;
3741 else break;
3744 ident_length = cp - bp;
3745 ident = bp;
3746 after_ident = cp;
3748 /* A line of just `#' becomes blank. */
3750 if (ident_length == 0 && *after_ident == '\n') {
3751 ip->bufp = after_ident;
3752 return 1;
3755 if (ident_length == 0 || !is_idstart[*ident]) {
3756 U_CHAR *p = ident;
3757 while (is_idchar[*p]) {
3758 if (*p < '0' || *p > '9')
3759 break;
3760 p++;
3762 /* Handle # followed by a line number. */
3763 if (p != ident && !is_idchar[*p]) {
3764 static struct directive line_directive_table[] = {
3765 { 4, do_line, "line", T_LINE},
3767 if (pedantic)
3768 pedwarn ("`#' followed by integer");
3769 after_ident = ident;
3770 kt = line_directive_table;
3771 goto old_linenum;
3774 /* Avoid error for `###' and similar cases unless -pedantic. */
3775 if (p == ident) {
3776 while (*p == '#' || is_hor_space[*p]) p++;
3777 if (*p == '\n') {
3778 if (pedantic && !lang_asm)
3779 warning ("invalid preprocessing directive");
3780 return 0;
3784 if (!lang_asm)
3785 error ("invalid preprocessing directive name");
3787 return 0;
3791 * Decode the keyword and call the appropriate expansion
3792 * routine, after moving the input pointer up to the next line.
3794 for (kt = directive_table; kt->length > 0; kt++) {
3795 if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
3796 register U_CHAR *buf;
3797 register U_CHAR *limit;
3798 int unterminated;
3799 int junk;
3800 int *already_output;
3802 /* Nonzero means do not delete comments within the directive.
3803 #define needs this when -traditional. */
3804 int keep_comments;
3806 old_linenum:
3808 limit = ip->buf + ip->length;
3809 unterminated = 0;
3810 already_output = 0;
3811 keep_comments = traditional && kt->type == T_DEFINE;
3812 /* #import is defined only in Objective C, or when on the NeXT. */
3813 if (kt->type == T_IMPORT
3814 && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1)))
3815 break;
3817 /* Find the end of this directive (first newline not backslashed
3818 and not in a string or comment).
3819 Set COPY_DIRECTIVE if the directive must be copied
3820 (it contains a backslash-newline or a comment). */
3822 buf = bp = after_ident;
3823 while (bp < limit) {
3824 register U_CHAR c = *bp++;
3825 switch (c) {
3826 case '\\':
3827 if (*bp == '\n') {
3828 ip->lineno++;
3829 copy_directive = 1;
3830 bp++;
3831 } else if (traditional && bp < limit)
3832 bp++;
3833 break;
3835 case '"':
3836 /* "..." is special for #include. */
3837 if (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)) {
3838 while (bp < limit && *bp != '\n') {
3839 if (*bp == '"') {
3840 bp++;
3841 break;
3843 if (*bp == '\\' && bp[1] == '\n') {
3844 ip->lineno++;
3845 copy_directive = 1;
3846 bp++;
3848 bp++;
3850 break;
3852 /* Fall through. */
3853 case '\'':
3854 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_directive, &unterminated);
3855 /* Don't bother calling the directive if we already got an error
3856 message due to unterminated string. Skip everything and pretend
3857 we called the directive. */
3858 if (unterminated) {
3859 if (traditional) {
3860 /* Traditional preprocessing permits unterminated strings. */
3861 ip->bufp = bp;
3862 goto endloop1;
3864 ip->bufp = bp;
3865 return 1;
3867 break;
3869 /* <...> is special for #include. */
3870 case '<':
3871 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
3872 break;
3873 while (bp < limit && *bp != '>' && *bp != '\n') {
3874 if (*bp == '\\' && bp[1] == '\n') {
3875 ip->lineno++;
3876 copy_directive = 1;
3877 bp++;
3879 bp++;
3881 break;
3883 case '/':
3884 if (*bp == '\\')
3885 newline_fix (bp);
3886 if (*bp == '*'
3887 || (cplusplus_comments && *bp == '/')) {
3888 U_CHAR *obp = bp - 1;
3889 ip->bufp = bp + 1;
3890 skip_to_end_of_comment (ip, &ip->lineno, 0);
3891 bp = ip->bufp;
3892 /* No need to copy the directive because of a comment at the end;
3893 just don't include the comment in the directive. */
3894 if (!put_out_comments) {
3895 U_CHAR *p;
3896 for (p = bp; *p == ' ' || *p == '\t'; p++)
3897 continue;
3898 if (*p == '\n') {
3899 bp = obp;
3900 goto endloop1;
3903 /* Don't remove the comments if -traditional. */
3904 if (! keep_comments)
3905 copy_directive++;
3907 break;
3909 case '\f':
3910 case '\r':
3911 case '\v':
3912 if (pedantic)
3913 pedwarn_strange_white_space (c);
3914 break;
3916 case '\n':
3917 --bp; /* Point to the newline */
3918 ip->bufp = bp;
3919 goto endloop1;
3922 ip->bufp = bp;
3924 endloop1:
3925 resume_p = ip->bufp;
3926 /* BP is the end of the directive.
3927 RESUME_P is the next interesting data after the directive.
3928 A comment may come between. */
3930 /* If a directive should be copied through, and -C was given,
3931 pass it through before removing comments. */
3932 if (!no_output && put_out_comments
3933 && ((kt->type == T_DEFINE || kt->type == T_UNDEF)
3934 ? dump_macros == dump_definitions
3935 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
3936 : kt->type == T_PRAGMA)) {
3937 int len;
3939 /* Output directive name. */
3940 check_expand (op, kt->length + 2);
3941 /* Make sure # is at the start of a line */
3942 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
3943 op->lineno++;
3944 *op->bufp++ = '\n';
3946 *op->bufp++ = '#';
3947 bcopy (kt->name, op->bufp, kt->length);
3948 op->bufp += kt->length;
3950 /* Output arguments. */
3951 len = (bp - buf);
3952 check_expand (op, len);
3953 bcopy (buf, (char *) op->bufp, len);
3954 op->bufp += len;
3955 /* Take account of any (escaped) newlines just output. */
3956 while (--len >= 0)
3957 if (buf[len] == '\n')
3958 op->lineno++;
3960 already_output = &junk;
3961 } /* Don't we need a newline or #line? */
3963 if (copy_directive) {
3964 register U_CHAR *xp = buf;
3965 /* Need to copy entire directive into temp buffer before dispatching */
3967 /* room for directive plus some slop */
3968 cp = (U_CHAR *) alloca (2 * (bp - buf) + 5);
3969 buf = cp;
3971 /* Copy to the new buffer, deleting comments
3972 and backslash-newlines (and whitespace surrounding the latter
3973 if outside of char and string constants). */
3975 while (xp < bp) {
3976 register U_CHAR c = *xp++;
3977 *cp++ = c;
3979 switch (c) {
3980 case '\n':
3981 abort (); /* A bare newline should never part of the line. */
3982 break;
3984 /* <...> is special for #include. */
3985 case '<':
3986 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
3987 break;
3988 while (xp < bp && c != '>') {
3989 c = *xp++;
3990 if (c == '\\' && xp < bp && *xp == '\n')
3991 xp++;
3992 else
3993 *cp++ = c;
3995 break;
3997 case '\\':
3998 if (*xp == '\n') {
3999 xp++;
4000 cp--;
4001 if (cp != buf && is_hor_space[cp[-1]]) {
4002 while (cp - 1 != buf && is_hor_space[cp[-2]])
4003 cp--;
4004 SKIP_WHITE_SPACE (xp);
4005 } else if (is_hor_space[*xp]) {
4006 *cp++ = *xp++;
4007 SKIP_WHITE_SPACE (xp);
4009 } else if (traditional && xp < bp) {
4010 *cp++ = *xp++;
4012 break;
4014 case '\'':
4015 case '\"':
4017 register U_CHAR *bp1
4018 = skip_quoted_string (xp - 1, bp, ip->lineno,
4019 NULL_PTR, NULL_PTR, NULL_PTR);
4020 while (xp != bp1) {
4021 if ((*cp++ = *xp++) == '\n') {
4022 if (xp[-2] == '\\')
4023 cp -= 2;
4024 else {
4025 cp[-1] = '\\';
4026 *cp++ = 'n';
4031 break;
4033 case '/':
4034 if (*xp == '*'
4035 || (cplusplus_comments && *xp == '/')) {
4036 ip->bufp = xp + 1;
4037 /* If we already copied the directive through,
4038 already_output != 0 prevents outputting comment now. */
4039 skip_to_end_of_comment (ip, already_output, 0);
4040 if (keep_comments)
4041 while (xp != ip->bufp)
4042 *cp++ = *xp++;
4043 /* Delete or replace the slash. */
4044 else if (traditional)
4045 cp--;
4046 else
4047 cp[-1] = ' ';
4048 xp = ip->bufp;
4053 /* Null-terminate the copy. */
4055 *cp = 0;
4056 } else
4057 cp = bp;
4059 ip->bufp = resume_p;
4061 /* Some directives should be written out for cc1 to process,
4062 just as if they were not defined. And sometimes we're copying
4063 directives through. */
4065 if (!no_output && already_output == 0
4066 && ((kt->type == T_DEFINE || kt->type == T_UNDEF)
4067 ? (int) dump_names <= (int) dump_macros
4068 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
4069 : kt->type == T_PRAGMA)) {
4070 int len;
4072 /* Output directive name. */
4073 check_expand (op, kt->length + 1);
4074 *op->bufp++ = '#';
4075 bcopy (kt->name, (char *) op->bufp, kt->length);
4076 op->bufp += kt->length;
4078 if (kt->type == T_DEFINE && dump_macros == dump_names) {
4079 /* Output `#define name' only. */
4080 U_CHAR *xp = buf;
4081 U_CHAR *yp;
4082 SKIP_WHITE_SPACE (xp);
4083 yp = xp;
4084 while (is_idchar[*xp]) xp++;
4085 len = (xp - yp);
4086 check_expand (op, len + 1);
4087 *op->bufp++ = ' ';
4088 bcopy (yp, (char *) op->bufp, len);
4089 } else {
4090 /* Output entire directive. */
4091 len = (cp - buf);
4092 check_expand (op, len);
4093 bcopy (buf, (char *) op->bufp, len);
4095 op->bufp += len;
4098 /* Call the appropriate directive handler. buf now points to
4099 either the appropriate place in the input buffer, or to
4100 the temp buffer if it was necessary to make one. cp
4101 points to the first char after the contents of the (possibly
4102 copied) directive, in either case. */
4103 (*kt->func) (buf, cp, op, kt);
4104 check_expand (op, ip->length - (ip->bufp - ip->buf));
4106 return 1;
4110 /* It is deliberate that we don't warn about undefined directives.
4111 That is the responsibility of cc1. */
4112 return 0;
4115 static struct tm *
4116 timestamp ()
4118 static struct tm tmbuf;
4119 if (! tmbuf.tm_mday) {
4120 time_t t = time ((time_t *) 0);
4121 struct tm *tm = localtime (&t);
4122 if (tm)
4123 tmbuf = *tm;
4124 else {
4125 /* Use 0000-01-01 00:00:00 if local time is not available. */
4126 tmbuf.tm_year = -1900;
4127 tmbuf.tm_mday = 1;
4130 return &tmbuf;
4133 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4134 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4138 * expand things like __FILE__. Place the expansion into the output
4139 * buffer *without* rescanning.
4142 static void
4143 special_symbol (hp, op)
4144 HASHNODE *hp;
4145 FILE_BUF *op;
4147 char *buf;
4148 int i, len;
4149 int true_indepth;
4150 FILE_BUF *ip = NULL;
4151 struct tm *timebuf;
4153 int paren = 0; /* For special `defined' keyword */
4155 if (pcp_outfile && pcp_inside_if
4156 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
4157 error ("Predefined macro `%s' used inside `#if' during precompilation",
4158 hp->name);
4160 for (i = indepth; i >= 0; i--)
4161 if (instack[i].fname != NULL) {
4162 ip = &instack[i];
4163 break;
4165 if (ip == NULL) {
4166 error ("cccp error: not in any file?!");
4167 return; /* the show must go on */
4170 switch (hp->type) {
4171 case T_FILE:
4172 case T_BASE_FILE:
4174 FILE_BUF *p = hp->type == T_FILE ? ip : &instack[0];
4175 char *string = p->nominal_fname;
4177 if (string)
4179 size_t string_len = p->nominal_fname_len;
4180 buf = (char *) alloca (3 + 4 * string_len);
4181 quote_string (buf, string, string_len);
4183 else
4184 buf = "\"\"";
4186 break;
4189 case T_INCLUDE_LEVEL:
4190 true_indepth = 0;
4191 for (i = indepth; i >= 0; i--)
4192 if (instack[i].fname != NULL)
4193 true_indepth++;
4195 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
4196 sprintf (buf, "%d", true_indepth - 1);
4197 break;
4199 case T_VERSION:
4200 buf = (char *) alloca (3 + strlen (version_string));
4201 sprintf (buf, "\"%s\"", version_string);
4202 break;
4204 #ifndef NO_BUILTIN_SIZE_TYPE
4205 case T_SIZE_TYPE:
4206 buf = SIZE_TYPE;
4207 break;
4208 #endif
4210 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4211 case T_PTRDIFF_TYPE:
4212 buf = PTRDIFF_TYPE;
4213 break;
4214 #endif
4216 case T_WCHAR_TYPE:
4217 buf = wchar_type;
4218 break;
4220 case T_USER_LABEL_PREFIX_TYPE:
4221 buf = USER_LABEL_PREFIX;
4222 break;
4224 case T_REGISTER_PREFIX_TYPE:
4225 buf = REGISTER_PREFIX;
4226 break;
4228 case T_IMMEDIATE_PREFIX_TYPE:
4229 buf = IMMEDIATE_PREFIX;
4230 break;
4232 case T_CONST:
4233 buf = hp->value.cpval;
4234 #ifdef STDC_0_IN_SYSTEM_HEADERS
4235 if (ip->system_header_p
4236 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
4237 && !lookup ((U_CHAR *) "__STRICT_ANSI__", -1, -1))
4238 buf = "0";
4239 #endif
4240 if (pcp_inside_if && pcp_outfile)
4241 /* Output a precondition for this macro use */
4242 fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
4243 break;
4245 case T_SPECLINE:
4246 buf = (char *) alloca (10);
4247 sprintf (buf, "%d", ip->lineno);
4248 break;
4250 case T_DATE:
4251 case T_TIME:
4252 buf = (char *) alloca (20);
4253 timebuf = timestamp ();
4254 if (hp->type == T_DATE)
4255 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
4256 timebuf->tm_mday, timebuf->tm_year + 1900);
4257 else
4258 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
4259 timebuf->tm_sec);
4260 break;
4262 case T_SPEC_DEFINED:
4263 buf = " 0 "; /* Assume symbol is not defined */
4264 ip = &instack[indepth];
4265 SKIP_WHITE_SPACE (ip->bufp);
4266 if (*ip->bufp == '(') {
4267 paren++;
4268 ip->bufp++; /* Skip over the paren */
4269 SKIP_WHITE_SPACE (ip->bufp);
4272 if (!is_idstart[*ip->bufp])
4273 goto oops;
4274 if (ip->bufp[0] == 'L' && (ip->bufp[1] == '\'' || ip->bufp[1] == '"'))
4275 goto oops;
4276 if ((hp = lookup (ip->bufp, -1, -1))) {
4277 if (pcp_outfile && pcp_inside_if
4278 && (hp->type == T_CONST
4279 || (hp->type == T_MACRO && hp->value.defn->predefined)))
4280 /* Output a precondition for this macro use. */
4281 fprintf (pcp_outfile, "#define %s\n", hp->name);
4282 buf = " 1 ";
4284 else
4285 if (pcp_outfile && pcp_inside_if) {
4286 /* Output a precondition for this macro use */
4287 U_CHAR *cp = ip->bufp;
4288 fprintf (pcp_outfile, "#undef ");
4289 while (is_idchar[*cp]) /* Ick! */
4290 fputc (*cp++, pcp_outfile);
4291 putc ('\n', pcp_outfile);
4293 while (is_idchar[*ip->bufp])
4294 ++ip->bufp;
4295 SKIP_WHITE_SPACE (ip->bufp);
4296 if (paren) {
4297 if (*ip->bufp != ')')
4298 goto oops;
4299 ++ip->bufp;
4301 break;
4303 oops:
4305 error ("`defined' without an identifier");
4306 break;
4308 default:
4309 error ("cccp error: invalid special hash type"); /* time for gdb */
4310 abort ();
4312 len = strlen (buf);
4313 check_expand (op, len);
4314 bcopy (buf, (char *) op->bufp, len);
4315 op->bufp += len;
4317 return;
4321 /* Routines to handle #directives */
4323 /* Handle #include and #import.
4324 This function expects to see "fname" or <fname> on the input. */
4326 static int
4327 do_include (buf, limit, op, keyword)
4328 U_CHAR *buf, *limit;
4329 FILE_BUF *op;
4330 struct directive *keyword;
4332 U_CHAR *importing = keyword->type == T_IMPORT ? (U_CHAR *) "" : (U_CHAR *) 0;
4333 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4334 static int import_warning = 0;
4335 char *fname; /* Dynamically allocated fname buffer */
4336 char *pcftry;
4337 char *pcfname;
4338 char *fbeg, *fend; /* Beginning and end of fname */
4339 U_CHAR *fin;
4341 struct file_name_list *search_start = include; /* Chain of dirs to search */
4342 struct file_name_list *dsp; /* First in chain, if #include "..." */
4343 struct file_name_list *searchptr = 0;
4344 size_t flen;
4346 int f = -3; /* file number */
4347 struct include_file *inc = 0;
4349 int retried = 0; /* Have already tried macro
4350 expanding the include line*/
4351 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4352 #ifdef VMS
4353 int vaxc_include = 0; /* 1 for token without punctuation */
4354 #endif
4355 int pcf = -1;
4356 char *pcfbuf;
4357 char *pcfbuflimit;
4358 int pcfnum;
4360 if (pedantic && !instack[indepth].system_header_p)
4362 if (importing)
4363 pedwarn ("ANSI C does not allow `#import'");
4364 if (skip_dirs)
4365 pedwarn ("ANSI C does not allow `#include_next'");
4368 if (importing && warn_import && !inhibit_warnings
4369 && !instack[indepth].system_header_p && !import_warning) {
4370 import_warning = 1;
4371 warning ("using `#import' is not recommended");
4372 notice ("The fact that a certain header file need not be processed more than once\n\
4373 should be indicated in the header file, not where it is used.\n\
4374 The best way to do this is with a conditional of this form:\n\
4376 #ifndef _FOO_H_INCLUDED\n\
4377 #define _FOO_H_INCLUDED\n\
4378 ... <real contents of file> ...\n\
4379 #endif /* Not _FOO_H_INCLUDED */\n\
4381 Then users can use `#include' any number of times.\n\
4382 GNU C automatically avoids processing the file more than once\n\
4383 when it is equipped with such a conditional.\n");
4386 get_filename:
4388 fin = buf;
4389 SKIP_WHITE_SPACE (fin);
4390 /* Discard trailing whitespace so we can easily see
4391 if we have parsed all the significant chars we were given. */
4392 while (limit != fin && is_hor_space[limit[-1]]) limit--;
4393 fbeg = fend = (char *) alloca (limit - fin);
4395 switch (*fin++) {
4396 case '\"':
4398 FILE_BUF *fp;
4399 /* Copy the operand text, concatenating the strings. */
4401 for (;;) {
4402 for (;;) {
4403 if (fin == limit)
4404 goto invalid_include_file_name;
4405 *fend = *fin++;
4406 if (*fend == '"')
4407 break;
4408 fend++;
4410 if (fin == limit)
4411 break;
4412 /* If not at the end, there had better be another string. */
4413 /* Skip just horiz space, and don't go past limit. */
4414 while (fin != limit && is_hor_space[*fin]) fin++;
4415 if (fin != limit && *fin == '\"')
4416 fin++;
4417 else
4418 goto fail;
4422 /* We have "filename". Figure out directory this source
4423 file is coming from and put it on the front of the list. */
4425 /* If -I- was specified, don't search current dir, only spec'd ones. */
4426 if (ignore_srcdir) break;
4428 for (fp = &instack[indepth]; fp >= instack; fp--)
4430 int n;
4431 char *nam;
4433 if ((nam = fp->nominal_fname) != NULL) {
4434 /* Found a named file. Figure out dir of the file,
4435 and put it in front of the search list. */
4436 dsp = ((struct file_name_list *)
4437 alloca (sizeof (struct file_name_list)
4438 + fp->nominal_fname_len));
4439 strcpy (dsp->fname, nam);
4440 simplify_filename (dsp->fname);
4441 nam = base_name (dsp->fname);
4442 *nam = 0;
4443 /* But for efficiency's sake, do not insert the dir
4444 if it matches the search list's first dir. */
4445 dsp->next = search_start;
4446 if (!search_start || strcmp (dsp->fname, search_start->fname)) {
4447 search_start = dsp;
4448 n = nam - dsp->fname;
4449 if (n + INCLUDE_LEN_FUDGE > max_include_len)
4450 max_include_len = n + INCLUDE_LEN_FUDGE;
4452 dsp[0].got_name_map = 0;
4453 break;
4456 break;
4459 case '<':
4460 while (fin != limit && *fin != '>')
4461 *fend++ = *fin++;
4462 if (*fin == '>' && fin + 1 == limit) {
4463 angle_brackets = 1;
4464 /* If -I-, start with the first -I dir after the -I-. */
4465 search_start = first_bracket_include;
4466 break;
4468 goto fail;
4470 default:
4471 #ifdef VMS
4473 * Support '#include xyz' like VAX-C to allow for easy use of all the
4474 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4475 * code from case '<' is repeated here) and generates a warning.
4476 * (Note: macro expansion of `xyz' takes precedence.)
4478 if (retried && isalpha(*(U_CHAR *) (--fbeg))) {
4479 while (fin != limit && (!isspace(*fin)))
4480 *fend++ = *fin++;
4481 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4482 vaxc_include = 1;
4483 if (fin == limit) {
4484 angle_brackets = 1;
4485 /* If -I-, start with the first -I dir after the -I-. */
4486 search_start = first_bracket_include;
4487 break;
4490 #endif
4492 fail:
4493 if (! retried) {
4494 /* Expand buffer and then remove any newline markers.
4495 We can't just tell expand_to_temp_buffer to omit the markers,
4496 since it would put extra spaces in include file names. */
4497 FILE_BUF trybuf;
4498 U_CHAR *src;
4499 int errors_before_expansion = errors;
4500 trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
4501 if (errors != errors_before_expansion) {
4502 free (trybuf.buf);
4503 goto invalid_include_file_name;
4505 src = trybuf.buf;
4506 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
4507 limit = buf;
4508 while (src != trybuf.bufp) {
4509 switch ((*limit++ = *src++)) {
4510 case '\n':
4511 limit--;
4512 src++;
4513 break;
4515 case '\'':
4516 case '\"':
4518 U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
4519 NULL_PTR, NULL_PTR, NULL_PTR);
4520 while (src != src1)
4521 *limit++ = *src++;
4523 break;
4526 *limit = 0;
4527 free (trybuf.buf);
4528 retried = 1;
4529 goto get_filename;
4532 invalid_include_file_name:
4533 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
4534 return 0;
4537 /* For #include_next, skip in the search path
4538 past the dir in which the containing file was found. */
4539 if (skip_dirs) {
4540 FILE_BUF *fp;
4541 for (fp = &instack[indepth]; fp >= instack; fp--)
4542 if (fp->fname != NULL) {
4543 /* fp->dir is null if the containing file was specified
4544 with an absolute file name. In that case, don't skip anything. */
4545 if (fp->dir)
4546 search_start = fp->dir->next;
4547 break;
4551 *fend = 0;
4552 flen = simplify_filename (fbeg);
4554 if (flen == 0)
4556 error ("empty file name in `#%s'", keyword->name);
4557 return 0;
4560 /* Allocate this permanently, because it gets stored in the definitions
4561 of macros. */
4562 fname = xmalloc (max_include_len + flen + 1);
4563 /* + 1 above for terminating null. */
4565 system_include_depth += angle_brackets;
4567 /* If specified file name is absolute, just open it. */
4569 if (absolute_filename (fbeg)) {
4570 strcpy (fname, fbeg);
4571 f = open_include_file (fname, NULL_PTR, importing, &inc);
4572 } else {
4574 struct bypass_dir {
4575 struct bypass_dir *next;
4576 char *fname;
4577 struct file_name_list *searchptr;
4578 } **bypass_slot = 0;
4580 /* Search directory path, trying to open the file.
4581 Copy each filename tried into FNAME. */
4583 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
4585 if (searchptr == first_bracket_include) {
4586 /* Go to bypass directory if we know we've seen this file before. */
4587 static struct bypass_dir *bypass_hashtab[INCLUDE_HASHSIZE];
4588 struct bypass_dir *p;
4589 bypass_slot = &bypass_hashtab[hashf ((U_CHAR *) fbeg, flen,
4590 INCLUDE_HASHSIZE)];
4591 for (p = *bypass_slot; p; p = p->next)
4592 if (!strcmp (fbeg, p->fname)) {
4593 searchptr = p->searchptr;
4594 bypass_slot = 0;
4595 break;
4599 strcpy (fname, searchptr->fname);
4600 strcat (fname, fbeg);
4601 #ifdef VMS
4602 /* Change this 1/2 Unix 1/2 VMS file specification into a
4603 full VMS file specification */
4604 if (searchptr->fname[0]) {
4605 /* Fix up the filename */
4606 hack_vms_include_specification (fname, vaxc_include);
4607 } else {
4608 /* This is a normal VMS filespec, so use it unchanged. */
4609 strcpy (fname, fbeg);
4610 /* if it's '#include filename', add the missing .h */
4611 if (vaxc_include && index(fname,'.')==NULL) {
4612 strcat (fname, ".h");
4615 #endif /* VMS */
4616 f = open_include_file (fname, searchptr, importing, &inc);
4617 if (f != -1) {
4618 if (bypass_slot && searchptr != first_bracket_include) {
4619 /* This is the first time we found this include file,
4620 and we found it after first_bracket_include.
4621 Record its location so that we can bypass to here next time. */
4622 struct bypass_dir *p
4623 = (struct bypass_dir *) xmalloc (sizeof (struct bypass_dir));
4624 p->next = *bypass_slot;
4625 p->fname = fname + strlen (searchptr->fname);
4626 p->searchptr = searchptr;
4627 *bypass_slot = p;
4629 break;
4631 #ifdef VMS
4632 /* Our VMS hacks can produce invalid filespecs, so don't worry
4633 about errors other than EACCES. */
4634 if (errno == EACCES)
4635 break;
4636 #else
4637 if (errno != ENOENT && errno != ENOTDIR)
4638 break;
4639 #endif
4644 if (f < 0) {
4646 if (f == -2) {
4647 /* The file was already included. */
4649 /* If generating dependencies and -MG was specified, we assume missing
4650 files are leaf files, living in the same directory as the source file
4651 or other similar place; these missing files may be generated from
4652 other files and may not exist yet (eg: y.tab.h). */
4653 } else if (print_deps_missing_files
4654 && (system_include_depth != 0) < print_deps)
4656 /* If it was requested as a system header file,
4657 then assume it belongs in the first place to look for such. */
4658 if (angle_brackets)
4660 if (search_start) {
4661 char *p = (char *) alloca (strlen (search_start->fname)
4662 + strlen (fbeg) + 1);
4663 strcpy (p, search_start->fname);
4664 strcat (p, fbeg);
4665 deps_output (p, ' ');
4668 else
4670 /* Otherwise, omit the directory, as if the file existed
4671 in the directory with the source. */
4672 deps_output (fbeg, ' ');
4675 /* If -M was specified, and this header file won't be added to the
4676 dependency list, then don't count this as an error, because we can
4677 still produce correct output. Otherwise, we can't produce correct
4678 output, because there may be dependencies we need inside the missing
4679 file, and we don't know what directory this missing file exists in. */
4680 else if (0 < print_deps && print_deps <= (system_include_depth != 0))
4681 warning ("No include path in which to find %s", fbeg);
4682 else if (f != -3)
4683 error_from_errno (fbeg);
4684 else
4685 error ("No include path in which to find %s", fbeg);
4687 } else {
4689 /* Actually process the file. */
4691 pcftry = (char *) alloca (strlen (fname) + 30);
4692 pcfbuf = 0;
4693 pcfnum = 0;
4695 if (!no_precomp)
4697 do {
4698 sprintf (pcftry, "%s%d", fname, pcfnum++);
4700 pcf = open (pcftry, O_RDONLY, 0666);
4701 if (pcf != -1)
4703 struct stat s;
4705 if (fstat (pcf, &s) != 0)
4706 pfatal_with_name (pcftry);
4707 if (! INO_T_EQ (inc->st.st_ino, s.st_ino)
4708 || inc->st.st_dev != s.st_dev)
4710 pcfbuf = check_precompiled (pcf, &s, fname, &pcfbuflimit);
4711 /* Don't need it any more. */
4712 close (pcf);
4714 else
4716 /* Don't need it at all. */
4717 close (pcf);
4718 break;
4721 } while (pcf != -1 && !pcfbuf);
4724 /* Actually process the file */
4725 if (pcfbuf) {
4726 pcfname = xmalloc (strlen (pcftry) + 1);
4727 strcpy (pcfname, pcftry);
4728 pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) pcfbuflimit,
4729 (U_CHAR *) fname, op);
4731 else
4732 finclude (f, inc, op, is_system_include (fname), searchptr);
4735 system_include_depth -= angle_brackets;
4737 return 0;
4740 /* Return nonzero if the given FILENAME is an absolute pathname which
4741 designates a file within one of the known "system" include file
4742 directories. We assume here that if the given FILENAME looks like
4743 it is the name of a file which resides either directly in a "system"
4744 include file directory, or within any subdirectory thereof, then the
4745 given file must be a "system" include file. This function tells us
4746 if we should suppress pedantic errors/warnings for the given FILENAME.
4748 The value is 2 if the file is a C-language system header file
4749 for which C++ should (on most systems) assume `extern "C"'. */
4751 static int
4752 is_system_include (filename)
4753 register char *filename;
4755 struct file_name_list *searchptr;
4757 for (searchptr = first_system_include; searchptr;
4758 searchptr = searchptr->next)
4759 if (! strncmp (searchptr->fname, filename, strlen (searchptr->fname)))
4760 return searchptr->c_system_include_path + 1;
4761 return 0;
4764 /* Yield the non-directory suffix of a file name. */
4766 static char *
4767 base_name (fname)
4768 char *fname;
4770 char *s = fname;
4771 char *p;
4772 #if defined (__MSDOS__) || defined (_WIN32)
4773 if (isalpha (s[0]) && s[1] == ':') s += 2;
4774 #endif
4775 #ifdef VMS
4776 if ((p = rindex (s, ':'))) s = p + 1; /* Skip device. */
4777 if ((p = rindex (s, ']'))) s = p + 1; /* Skip directory. */
4778 if ((p = rindex (s, '>'))) s = p + 1; /* Skip alternate (int'n'l) dir. */
4779 if (s != fname)
4780 return s;
4781 #endif
4782 if ((p = rindex (s, '/'))) s = p + 1;
4783 #ifdef DIR_SEPARATOR
4784 if ((p = rindex (s, DIR_SEPARATOR))) s = p + 1;
4785 #endif
4786 return s;
4789 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4791 static int
4792 absolute_filename (filename)
4793 char *filename;
4795 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN32__))
4796 if (isalpha (filename[0]) && filename[1] == ':') filename += 2;
4797 #endif
4798 #if defined (__CYGWIN32__)
4799 /* At present, any path that begins with a drive spec is absolute. */
4800 if (isalpha (filename[0]) && filename[1] == ':') return 1;
4801 #endif
4802 if (filename[0] == '/') return 1;
4803 #ifdef DIR_SEPARATOR
4804 if (filename[0] == DIR_SEPARATOR) return 1;
4805 #endif
4806 return 0;
4809 /* Remove unnecessary characters from FILENAME in place,
4810 to avoid unnecessary filename aliasing.
4811 Return the length of the resulting string.
4813 Do only the simplifications allowed by Posix.
4814 It is OK to miss simplifications on non-Posix hosts,
4815 since this merely leads to suboptimal results. */
4817 static size_t
4818 simplify_filename (filename)
4819 char *filename;
4821 register char *from = filename;
4822 register char *to = filename;
4823 char *to0;
4825 /* Remove redundant initial /s. */
4826 if (*from == '/') {
4827 *to++ = '/';
4828 if (*++from == '/') {
4829 if (*++from == '/') {
4830 /* 3 or more initial /s are equivalent to 1 /. */
4831 while (*++from == '/')
4832 continue;
4833 } else {
4834 /* On some hosts // differs from /; Posix allows this. */
4835 static int slashslash_vs_slash;
4836 if (slashslash_vs_slash == 0) {
4837 struct stat s1, s2;
4838 slashslash_vs_slash = ((stat ("/", &s1) == 0 && stat ("//", &s2) == 0
4839 && INO_T_EQ (s1.st_ino, s2.st_ino)
4840 && s1.st_dev == s2.st_dev)
4841 ? 1 : -1);
4843 if (slashslash_vs_slash < 0)
4844 *to++ = '/';
4848 to0 = to;
4850 for (;;) {
4851 if (from[0] == '.' && from[1] == '/')
4852 from += 2;
4853 else {
4854 /* Copy this component and trailing /, if any. */
4855 while ((*to++ = *from++) != '/') {
4856 if (!to[-1]) {
4857 /* Trim . component at end of nonempty name. */
4858 to -= filename <= to - 3 && to[-3] == '/' && to[-2] == '.';
4860 /* Trim unnecessary trailing /s. */
4861 while (to0 < --to && to[-1] == '/')
4862 continue;
4864 *to = 0;
4865 return to - filename;
4870 /* Skip /s after a /. */
4871 while (*from == '/')
4872 from++;
4876 /* The file_name_map structure holds a mapping of file names for a
4877 particular directory. This mapping is read from the file named
4878 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4879 map filenames on a file system with severe filename restrictions,
4880 such as DOS. The format of the file name map file is just a series
4881 of lines with two tokens on each line. The first token is the name
4882 to map, and the second token is the actual name to use. */
4884 struct file_name_map
4886 struct file_name_map *map_next;
4887 char *map_from;
4888 char *map_to;
4891 #define FILE_NAME_MAP_FILE "header.gcc"
4893 /* Read a space delimited string of unlimited length from a stdio
4894 file. */
4896 static char *
4897 read_filename_string (ch, f)
4898 int ch;
4899 FILE *f;
4901 char *alloc, *set;
4902 int len;
4904 len = 20;
4905 set = alloc = xmalloc (len + 1);
4906 if (! is_space[ch])
4908 *set++ = ch;
4909 while ((ch = getc (f)) != EOF && ! is_space[ch])
4911 if (set - alloc == len)
4913 len *= 2;
4914 alloc = xrealloc (alloc, len + 1);
4915 set = alloc + len / 2;
4917 *set++ = ch;
4920 *set = '\0';
4921 ungetc (ch, f);
4922 return alloc;
4925 /* Read the file name map file for DIRNAME.
4926 If DIRNAME is empty, read the map file for the working directory;
4927 otherwise DIRNAME must end in '/'. */
4929 static struct file_name_map *
4930 read_name_map (dirname)
4931 char *dirname;
4933 /* This structure holds a linked list of file name maps, one per
4934 directory. */
4935 struct file_name_map_list
4937 struct file_name_map_list *map_list_next;
4938 char *map_list_name;
4939 struct file_name_map *map_list_map;
4941 static struct file_name_map_list *map_list;
4942 register struct file_name_map_list *map_list_ptr;
4943 char *name;
4944 FILE *f;
4945 size_t dirlen;
4947 for (map_list_ptr = map_list; map_list_ptr;
4948 map_list_ptr = map_list_ptr->map_list_next)
4949 if (! strcmp (map_list_ptr->map_list_name, dirname))
4950 return map_list_ptr->map_list_map;
4952 map_list_ptr = ((struct file_name_map_list *)
4953 xmalloc (sizeof (struct file_name_map_list)));
4954 map_list_ptr->map_list_name = savestring (dirname);
4955 map_list_ptr->map_list_map = NULL;
4957 dirlen = strlen (dirname);
4958 name = (char *) alloca (dirlen + strlen (FILE_NAME_MAP_FILE) + 1);
4959 strcpy (name, dirname);
4960 strcat (name, FILE_NAME_MAP_FILE);
4961 f = fopen (name, "r");
4962 if (!f)
4963 map_list_ptr->map_list_map = NULL;
4964 else
4966 int ch;
4968 while ((ch = getc (f)) != EOF)
4970 char *from, *to;
4971 struct file_name_map *ptr;
4972 size_t tolen;
4974 if (is_space[ch])
4975 continue;
4976 from = read_filename_string (ch, f);
4977 while ((ch = getc (f)) != EOF && is_hor_space[ch])
4979 to = read_filename_string (ch, f);
4981 simplify_filename (from);
4982 tolen = simplify_filename (to);
4984 ptr = ((struct file_name_map *)
4985 xmalloc (sizeof (struct file_name_map)));
4986 ptr->map_from = from;
4988 /* Make the real filename absolute. */
4989 if (absolute_filename (to))
4990 ptr->map_to = to;
4991 else
4993 ptr->map_to = xmalloc (dirlen + tolen + 1);
4994 strcpy (ptr->map_to, dirname);
4995 strcat (ptr->map_to, to);
4996 free (to);
4999 ptr->map_next = map_list_ptr->map_list_map;
5000 map_list_ptr->map_list_map = ptr;
5002 while ((ch = getc (f)) != '\n')
5003 if (ch == EOF)
5004 break;
5006 fclose (f);
5009 map_list_ptr->map_list_next = map_list;
5010 map_list = map_list_ptr;
5012 return map_list_ptr->map_list_map;
5015 /* Try to open include file FILENAME. SEARCHPTR is the directory
5016 being tried from the include file search path.
5017 IMPORTING is "" if we are importing, null otherwise.
5018 Return -2 if found, either a matching name or a matching inode.
5019 Otherwise, open the file and return a file descriptor if successful
5020 or -1 if unsuccessful.
5021 Unless unsuccessful, put a descriptor of the included file into *PINC.
5022 This function maps filenames on file systems based on information read by
5023 read_name_map. */
5025 static int
5026 open_include_file (filename, searchptr, importing, pinc)
5027 char *filename;
5028 struct file_name_list *searchptr;
5029 U_CHAR *importing;
5030 struct include_file **pinc;
5032 char *fname = remap ? remap_include_file (filename, searchptr) : filename;
5033 int fd = -2;
5035 /* Look up FNAME in include_hashtab. */
5036 struct include_file **phead = &include_hashtab[hashf ((U_CHAR *) fname,
5037 strlen (fname),
5038 INCLUDE_HASHSIZE)];
5039 struct include_file *inc, *head = *phead;
5040 for (inc = head; inc; inc = inc->next)
5041 if (!strcmp (fname, inc->fname))
5042 break;
5044 if (!inc
5045 || ! inc->control_macro
5046 || (inc->control_macro[0] && ! lookup (inc->control_macro, -1, -1))) {
5048 fd = open (fname, O_RDONLY, 0);
5050 if (fd < 0)
5051 return fd;
5053 if (!inc) {
5054 /* FNAME was not in include_hashtab; insert a new entry. */
5055 inc = (struct include_file *) xmalloc (sizeof (struct include_file));
5056 inc->next = head;
5057 inc->fname = fname;
5058 inc->control_macro = 0;
5059 inc->deps_output = 0;
5060 if (fstat (fd, &inc->st) != 0)
5061 pfatal_with_name (fname);
5062 *phead = inc;
5064 /* Look for another file with the same inode and device. */
5065 if (lookup_ino_include (inc)
5066 && inc->control_macro
5067 && (!inc->control_macro[0] || lookup (inc->control_macro, -1, -1))) {
5068 close (fd);
5069 fd = -2;
5073 /* For -M, add this file to the dependencies. */
5074 if (! inc->deps_output && (system_include_depth != 0) < print_deps) {
5075 inc->deps_output = 1;
5076 deps_output (fname, ' ');
5079 /* Handle -H option. */
5080 if (print_include_names)
5081 fprintf (stderr, "%*s%s\n", indepth, "", fname);
5084 if (importing)
5085 inc->control_macro = importing;
5087 *pinc = inc;
5088 return fd;
5091 /* Return the remapped name of the the include file FILENAME.
5092 SEARCHPTR is the directory being tried from the include file path. */
5094 static char *
5095 remap_include_file (filename, searchptr)
5096 char *filename;
5097 struct file_name_list *searchptr;
5099 register struct file_name_map *map;
5100 register char *from;
5102 if (searchptr)
5104 if (! searchptr->got_name_map)
5106 searchptr->name_map = read_name_map (searchptr->fname);
5107 searchptr->got_name_map = 1;
5110 /* Check the mapping for the directory we are using. */
5111 from = filename + strlen (searchptr->fname);
5112 for (map = searchptr->name_map; map; map = map->map_next)
5113 if (! strcmp (map->map_from, from))
5114 return map->map_to;
5117 from = base_name (filename);
5119 if (from != filename || !searchptr)
5121 /* Try to find a mapping file for the particular directory we are
5122 looking in. Thus #include <sys/types.h> will look up sys/types.h
5123 in /usr/include/header.gcc and look up types.h in
5124 /usr/include/sys/header.gcc. */
5126 char *dir = (char *) alloca (from - filename + 1);
5127 bcopy (filename, dir, from - filename);
5128 dir[from - filename] = '\0';
5130 for (map = read_name_map (dir); map; map = map->map_next)
5131 if (! strcmp (map->map_from, from))
5132 return map->map_to;
5135 return filename;
5138 /* Insert INC into the include file table, hashed by device and inode number.
5139 If a file with different name but same dev+ino was already in the table,
5140 return 1 and set INC's control macro to the already-known macro. */
5142 static int
5143 lookup_ino_include (inc)
5144 struct include_file *inc;
5146 int hash = ((unsigned) (inc->st.st_dev + INO_T_HASH (inc->st.st_ino))
5147 % INCLUDE_HASHSIZE);
5148 struct include_file *i = include_ino_hashtab[hash];
5149 inc->next_ino = i;
5150 include_ino_hashtab[hash] = inc;
5152 for (; i; i = i->next_ino)
5153 if (INO_T_EQ (inc->st.st_ino, i->st.st_ino)
5154 && inc->st.st_dev == i->st.st_dev) {
5155 inc->control_macro = i->control_macro;
5156 return 1;
5159 return 0;
5162 /* Process file descriptor F, which corresponds to include file INC,
5163 with output to OP.
5164 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5165 "system" include directories (as decided by the `is_system_include'
5166 function above).
5167 DIRPTR is the link in the dir path through which this file was found,
5168 or 0 if the file name was absolute. */
5170 static void
5171 finclude (f, inc, op, system_header_p, dirptr)
5172 int f;
5173 struct include_file *inc;
5174 FILE_BUF *op;
5175 int system_header_p;
5176 struct file_name_list *dirptr;
5178 char *fname = inc->fname;
5179 int i;
5180 FILE_BUF *fp; /* For input stack frame */
5181 int missing_newline = 0;
5183 CHECK_DEPTH (return;);
5185 fp = &instack[indepth + 1];
5186 bzero ((char *) fp, sizeof (FILE_BUF));
5187 fp->nominal_fname = fp->fname = fname;
5188 fp->nominal_fname_len = strlen (fname);
5189 fp->inc = inc;
5190 fp->length = 0;
5191 fp->lineno = 1;
5192 fp->if_stack = if_stack;
5193 fp->system_header_p = system_header_p;
5194 fp->dir = dirptr;
5196 if (S_ISREG (inc->st.st_mode)) {
5197 size_t s = (size_t) inc->st.st_size;
5198 if (s != inc->st.st_size || s + 2 < s)
5199 memory_full ();
5200 fp->buf = (U_CHAR *) xmalloc (s + 2);
5201 fp->bufp = fp->buf;
5203 /* Read the file contents, knowing that s is an upper bound
5204 on the number of bytes we can read. */
5205 fp->length = safe_read (f, (char *) fp->buf, s);
5206 if (fp->length < 0) goto nope;
5208 else if (S_ISDIR (inc->st.st_mode)) {
5209 error ("directory `%s' specified in #include", fname);
5210 close (f);
5211 return;
5212 } else {
5213 /* Cannot count its file size before reading.
5214 First read the entire file into heap and
5215 copy them into buffer on stack. */
5217 int bsize = 2000;
5218 int st_size = 0;
5220 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5222 for (;;) {
5223 i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
5224 if (i < 0)
5225 goto nope; /* error! */
5226 st_size += i;
5227 if (st_size != bsize)
5228 break; /* End of file */
5229 bsize *= 2;
5230 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5232 fp->bufp = fp->buf;
5233 fp->length = st_size;
5236 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5237 /* Backslash-newline at end is not good enough. */
5238 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5239 fp->buf[fp->length++] = '\n';
5240 missing_newline = 1;
5242 fp->buf[fp->length] = '\0';
5244 /* Close descriptor now, so nesting does not use lots of descriptors. */
5245 close (f);
5247 /* Must do this before calling trigraph_pcp, so that the correct file name
5248 will be printed in warning messages. */
5250 indepth++;
5251 input_file_stack_tick++;
5253 if (!no_trigraphs)
5254 trigraph_pcp (fp);
5256 if (warn_white_space)
5257 check_white_space (fp);
5259 output_line_directive (fp, op, 0, enter_file);
5260 rescan (op, 0);
5262 if (missing_newline)
5263 fp->lineno--;
5265 if (pedantic && missing_newline)
5266 pedwarn ("file does not end in newline");
5268 indepth--;
5269 input_file_stack_tick++;
5270 output_line_directive (&instack[indepth], op, 0, leave_file);
5271 free (fp->buf);
5272 return;
5274 nope:
5276 perror_with_name (fname);
5277 close (f);
5278 free (fp->buf);
5281 /* Record that inclusion of the include file INC
5282 should be controlled by the macro named MACRO_NAME.
5283 This means that trying to include the file again
5284 will do something if that macro is defined. */
5286 static void
5287 record_control_macro (inc, macro_name)
5288 struct include_file *inc;
5289 U_CHAR *macro_name;
5291 if (!inc->control_macro || inc->control_macro[0])
5292 inc->control_macro = macro_name;
5295 /* Load the specified precompiled header into core, and verify its
5296 preconditions. PCF indicates the file descriptor to read, which must
5297 be a regular file. *ST is its file status.
5298 FNAME indicates the file name of the original header.
5299 *LIMIT will be set to an address one past the end of the file.
5300 If the preconditions of the file are not satisfied, the buffer is
5301 freed and we return 0. If the preconditions are satisfied, return
5302 the address of the buffer following the preconditions. The buffer, in
5303 this case, should never be freed because various pieces of it will
5304 be referred to until all precompiled strings are output at the end of
5305 the run. */
5307 static char *
5308 check_precompiled (pcf, st, fname, limit)
5309 int pcf;
5310 struct stat *st;
5311 char *fname;
5312 char **limit;
5314 int length = 0;
5315 char *buf;
5316 char *cp;
5318 if (pcp_outfile)
5319 return 0;
5321 if (S_ISREG (st->st_mode))
5323 size_t s = (size_t) st->st_size;
5324 if (s != st->st_size || s + 2 < s)
5325 memory_full ();
5326 buf = xmalloc (s + 2);
5327 length = safe_read (pcf, buf, s);
5328 if (length < 0)
5329 goto nope;
5331 else
5332 abort ();
5334 if (length > 0 && buf[length-1] != '\n')
5335 buf[length++] = '\n';
5336 buf[length] = '\0';
5338 *limit = buf + length;
5340 /* File is in core. Check the preconditions. */
5341 if (!check_preconditions (buf))
5342 goto nope;
5343 for (cp = buf; *cp; cp++)
5345 #ifdef DEBUG_PCP
5346 fprintf (stderr, "Using preinclude %s\n", fname);
5347 #endif
5348 return cp + 1;
5350 nope:
5351 #ifdef DEBUG_PCP
5352 fprintf (stderr, "Cannot use preinclude %s\n", fname);
5353 #endif
5354 free (buf);
5355 return 0;
5358 /* PREC (null terminated) points to the preconditions of a
5359 precompiled header. These are a series of #define and #undef
5360 lines which must match the current contents of the hash
5361 table. */
5363 static int
5364 check_preconditions (prec)
5365 char *prec;
5367 MACRODEF mdef;
5368 char *lineend;
5370 while (*prec) {
5371 lineend = index (prec, '\n');
5373 if (*prec++ != '#') {
5374 error ("Bad format encountered while reading precompiled file");
5375 return 0;
5377 if (!strncmp (prec, "define", 6)) {
5378 HASHNODE *hp;
5380 prec += 6;
5381 mdef = create_definition ((U_CHAR *) prec, (U_CHAR *) lineend, NULL_PTR);
5383 if (mdef.defn == 0)
5384 abort ();
5386 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
5387 || (hp->type != T_MACRO && hp->type != T_CONST)
5388 || (hp->type == T_MACRO
5389 && !compare_defs (mdef.defn, hp->value.defn)
5390 && (mdef.defn->length != 2
5391 || mdef.defn->expansion[0] != '\n'
5392 || mdef.defn->expansion[1] != ' ')))
5393 return 0;
5394 } else if (!strncmp (prec, "undef", 5)) {
5395 char *name;
5396 int len;
5398 prec += 5;
5399 while (is_hor_space[(U_CHAR) *prec])
5400 prec++;
5401 name = prec;
5402 while (is_idchar[(U_CHAR) *prec])
5403 prec++;
5404 len = prec - name;
5406 if (lookup ((U_CHAR *) name, len, -1))
5407 return 0;
5408 } else {
5409 error ("Bad format encountered while reading precompiled file");
5410 return 0;
5412 prec = lineend + 1;
5414 /* They all passed successfully */
5415 return 1;
5418 /* Process the main body of a precompiled file. BUF points to the
5419 string section of the file, following the preconditions. LIMIT is one
5420 character past the end. NAME is the name of the file being read
5421 in. OP is the main output buffer. */
5423 static void
5424 pcfinclude (buf, limit, name, op)
5425 U_CHAR *buf, *limit, *name;
5426 FILE_BUF *op;
5428 FILE_BUF tmpbuf;
5429 int nstrings;
5430 U_CHAR *cp = buf;
5432 /* First in the file comes 4 bytes indicating the number of strings, */
5433 /* in network byte order. (MSB first). */
5434 nstrings = *cp++;
5435 nstrings = (nstrings << 8) | *cp++;
5436 nstrings = (nstrings << 8) | *cp++;
5437 nstrings = (nstrings << 8) | *cp++;
5439 /* Looping over each string... */
5440 while (nstrings--) {
5441 U_CHAR *string_start;
5442 U_CHAR *endofthiskey;
5443 STRINGDEF *str;
5444 int nkeys;
5446 /* Each string starts with a STRINGDEF structure (str), followed */
5447 /* by the text of the string (string_start) */
5449 /* First skip to a longword boundary */
5450 /* ??? Why a 4-byte boundary? On all machines? */
5451 /* NOTE: This works correctly even if size_t
5452 is narrower than a pointer.
5453 Do not try risky measures here to get another type to use!
5454 Do not include stddef.h--it will fail! */
5455 if ((size_t) cp & 3)
5456 cp += 4 - ((size_t) cp & 3);
5458 /* Now get the string. */
5459 str = (STRINGDEF *) (GENERIC_PTR) cp;
5460 string_start = cp += sizeof (STRINGDEF);
5462 for (; *cp; cp++) /* skip the string */
5465 /* We need to macro expand the string here to ensure that the
5466 proper definition environment is in place. If it were only
5467 expanded when we find out it is needed, macros necessary for
5468 its proper expansion might have had their definitions changed. */
5469 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
5470 /* Lineno is already set in the precompiled file */
5471 str->contents = tmpbuf.buf;
5472 str->len = tmpbuf.bufp - tmpbuf.buf;
5473 str->writeflag = 0;
5474 str->filename = name;
5475 str->output_mark = outbuf.bufp - outbuf.buf;
5477 str->chain = 0;
5478 *stringlist_tailp = str;
5479 stringlist_tailp = &str->chain;
5481 /* Next comes a fourbyte number indicating the number of keys
5482 for this string. */
5483 nkeys = *cp++;
5484 nkeys = (nkeys << 8) | *cp++;
5485 nkeys = (nkeys << 8) | *cp++;
5486 nkeys = (nkeys << 8) | *cp++;
5488 /* If this number is -1, then the string is mandatory. */
5489 if (nkeys == -1)
5490 str->writeflag = 1;
5491 else
5492 /* Otherwise, for each key, */
5493 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
5494 KEYDEF *kp = (KEYDEF *) (GENERIC_PTR) cp;
5495 HASHNODE *hp;
5496 U_CHAR *bp;
5498 /* It starts with a KEYDEF structure */
5499 cp += sizeof (KEYDEF);
5501 /* Find the end of the key. At the end of this for loop we
5502 advance CP to the start of the next key using this variable. */
5503 endofthiskey = cp + strlen ((char *) cp);
5504 kp->str = str;
5506 /* Expand the key, and enter it into the hash table. */
5507 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
5508 bp = tmpbuf.buf;
5510 while (is_hor_space[*bp])
5511 bp++;
5512 if (!is_idstart[*bp] || bp == tmpbuf.bufp) {
5513 str->writeflag = 1;
5514 continue;
5517 hp = lookup (bp, -1, -1);
5518 if (hp == NULL) {
5519 kp->chain = 0;
5520 install (bp, -1, T_PCSTRING, (char *) kp, -1);
5522 else if (hp->type == T_PCSTRING) {
5523 kp->chain = hp->value.keydef;
5524 hp->value.keydef = kp;
5526 else
5527 str->writeflag = 1;
5530 /* This output_line_directive serves to switch us back to the current
5531 input file in case some of these strings get output (which will
5532 result in line directives for the header file being output). */
5533 output_line_directive (&instack[indepth], op, 0, enter_file);
5536 /* Called from rescan when it hits a key for strings. Mark them all
5537 used and clean up. */
5539 static void
5540 pcstring_used (hp)
5541 HASHNODE *hp;
5543 KEYDEF *kp;
5545 for (kp = hp->value.keydef; kp; kp = kp->chain)
5546 kp->str->writeflag = 1;
5547 delete_macro (hp);
5550 /* Write the output, interspersing precompiled strings in their
5551 appropriate places. */
5553 static void
5554 write_output ()
5556 STRINGDEF *next_string;
5557 U_CHAR *cur_buf_loc;
5558 int line_directive_len = 80;
5559 char *line_directive = xmalloc (line_directive_len);
5560 int len;
5562 /* In each run through the loop, either cur_buf_loc ==
5563 next_string_loc, in which case we print a series of strings, or
5564 it is less than next_string_loc, in which case we write some of
5565 the buffer. */
5566 cur_buf_loc = outbuf.buf;
5567 next_string = stringlist;
5569 while (cur_buf_loc < outbuf.bufp || next_string) {
5570 if (next_string
5571 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
5572 if (next_string->writeflag) {
5573 len = 4 * strlen ((char *) next_string->filename) + 32;
5574 while (len > line_directive_len)
5575 line_directive = xrealloc (line_directive,
5576 line_directive_len *= 2);
5577 sprintf (line_directive, "\n# %d ", next_string->lineno);
5578 strcpy (quote_string (line_directive + strlen (line_directive),
5579 (char *) next_string->filename,
5580 strlen ((char *) next_string->filename)),
5581 "\n");
5582 safe_write (fileno (stdout), line_directive, strlen (line_directive));
5583 safe_write (fileno (stdout),
5584 (char *) next_string->contents, next_string->len);
5586 next_string = next_string->chain;
5588 else {
5589 len = (next_string
5590 ? (next_string->output_mark
5591 - (cur_buf_loc - outbuf.buf))
5592 : outbuf.bufp - cur_buf_loc);
5594 safe_write (fileno (stdout), (char *) cur_buf_loc, len);
5595 cur_buf_loc += len;
5598 free (line_directive);
5601 /* Pass a directive through to the output file.
5602 BUF points to the contents of the directive, as a contiguous string.
5603 LIMIT points to the first character past the end of the directive.
5604 KEYWORD is the keyword-table entry for the directive. */
5606 static void
5607 pass_thru_directive (buf, limit, op, keyword)
5608 U_CHAR *buf, *limit;
5609 FILE_BUF *op;
5610 struct directive *keyword;
5612 register unsigned keyword_length = keyword->length;
5614 check_expand (op, 1 + keyword_length + (limit - buf));
5615 *op->bufp++ = '#';
5616 bcopy (keyword->name, (char *) op->bufp, keyword_length);
5617 op->bufp += keyword_length;
5618 if (limit != buf && buf[0] != ' ')
5619 *op->bufp++ = ' ';
5620 bcopy ((char *) buf, (char *) op->bufp, limit - buf);
5621 op->bufp += (limit - buf);
5622 #if 0
5623 *op->bufp++ = '\n';
5624 /* Count the line we have just made in the output,
5625 to get in sync properly. */
5626 op->lineno++;
5627 #endif
5630 /* The arglist structure is built by do_define to tell
5631 collect_definition where the argument names begin. That
5632 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5633 would contain pointers to the strings x, y, and z.
5634 Collect_definition would then build a DEFINITION node,
5635 with reflist nodes pointing to the places x, y, and z had
5636 appeared. So the arglist is just convenience data passed
5637 between these two routines. It is not kept around after
5638 the current #define has been processed and entered into the
5639 hash table. */
5641 struct arglist {
5642 struct arglist *next;
5643 U_CHAR *name;
5644 int length;
5645 int argno;
5646 char rest_args;
5649 /* Create a DEFINITION node from a #define directive. Arguments are
5650 as for do_define. */
5652 static MACRODEF
5653 create_definition (buf, limit, op)
5654 U_CHAR *buf, *limit;
5655 FILE_BUF *op;
5657 U_CHAR *bp; /* temp ptr into input buffer */
5658 U_CHAR *symname; /* remember where symbol name starts */
5659 int sym_length; /* and how long it is */
5660 int line = instack[indepth].lineno;
5661 char *file = instack[indepth].nominal_fname;
5662 size_t file_len = instack[indepth].nominal_fname_len;
5663 int rest_args = 0;
5665 DEFINITION *defn;
5666 int arglengths = 0; /* Accumulate lengths of arg names
5667 plus number of args. */
5668 MACRODEF mdef;
5670 bp = buf;
5672 while (is_hor_space[*bp])
5673 bp++;
5675 symname = bp; /* remember where it starts */
5676 sym_length = check_macro_name (bp, 0);
5677 bp += sym_length;
5679 /* Lossage will occur if identifiers or control keywords are broken
5680 across lines using backslash. This is not the right place to take
5681 care of that. */
5683 if (*bp == '(') {
5684 struct arglist *arg_ptrs = NULL;
5685 int argno = 0;
5687 bp++; /* skip '(' */
5688 SKIP_WHITE_SPACE (bp);
5690 /* Loop over macro argument names. */
5691 while (*bp != ')') {
5692 struct arglist *temp;
5694 temp = (struct arglist *) alloca (sizeof (struct arglist));
5695 temp->name = bp;
5696 temp->next = arg_ptrs;
5697 temp->argno = argno++;
5698 temp->rest_args = 0;
5699 arg_ptrs = temp;
5701 if (rest_args)
5702 pedwarn ("another parameter follows `%s'",
5703 rest_extension);
5705 if (!is_idstart[*bp])
5706 pedwarn ("invalid character in macro parameter name");
5708 /* Find the end of the arg name. */
5709 while (is_idchar[*bp]) {
5710 bp++;
5711 /* do we have a "special" rest-args extension here? */
5712 if (limit - bp > REST_EXTENSION_LENGTH
5713 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
5714 rest_args = 1;
5715 temp->rest_args = 1;
5716 break;
5719 temp->length = bp - temp->name;
5720 if (rest_args == 1)
5721 bp += REST_EXTENSION_LENGTH;
5722 arglengths += temp->length + 2;
5723 SKIP_WHITE_SPACE (bp);
5724 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
5725 error ("badly punctuated parameter list in `#define'");
5726 goto nope;
5728 if (*bp == ',') {
5729 bp++;
5730 SKIP_WHITE_SPACE (bp);
5731 /* A comma at this point can only be followed by an identifier. */
5732 if (!is_idstart[*bp]) {
5733 error ("badly punctuated parameter list in `#define'");
5734 goto nope;
5737 if (bp >= limit) {
5738 error ("unterminated parameter list in `#define'");
5739 goto nope;
5742 struct arglist *otemp;
5744 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
5745 if (temp->length == otemp->length
5746 && bcmp (temp->name, otemp->name, temp->length) == 0) {
5747 error ("duplicate argument name `%.*s' in `#define'",
5748 temp->length, temp->name);
5749 goto nope;
5754 ++bp; /* skip paren */
5755 SKIP_WHITE_SPACE (bp);
5756 /* now everything from bp before limit is the definition. */
5757 defn = collect_expansion (bp, limit, argno, arg_ptrs);
5758 defn->rest_args = rest_args;
5760 /* Now set defn->args.argnames to the result of concatenating
5761 the argument names in reverse order
5762 with comma-space between them. */
5763 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
5765 struct arglist *temp;
5766 int i = 0;
5767 for (temp = arg_ptrs; temp; temp = temp->next) {
5768 bcopy (temp->name, &defn->args.argnames[i], temp->length);
5769 i += temp->length;
5770 if (temp->next != 0) {
5771 defn->args.argnames[i++] = ',';
5772 defn->args.argnames[i++] = ' ';
5775 defn->args.argnames[i] = 0;
5777 } else {
5778 /* Simple expansion or empty definition. */
5780 if (bp < limit)
5782 if (is_hor_space[*bp]) {
5783 bp++;
5784 SKIP_WHITE_SPACE (bp);
5785 } else if (sym_length) {
5786 switch (*bp) {
5787 case '!': case '"': case '#': case '%': case '&': case '\'':
5788 case ')': case '*': case '+': case ',': case '-': case '.':
5789 case '/': case ':': case ';': case '<': case '=': case '>':
5790 case '?': case '[': case '\\': case ']': case '^': case '{':
5791 case '|': case '}': case '~':
5792 warning ("missing white space after `#define %.*s'",
5793 sym_length, symname);
5794 break;
5796 default:
5797 pedwarn ("missing white space after `#define %.*s'",
5798 sym_length, symname);
5799 break;
5803 /* Now everything from bp before limit is the definition. */
5804 defn = collect_expansion (bp, limit, -1, NULL_PTR);
5805 defn->args.argnames = (U_CHAR *) "";
5808 defn->line = line;
5809 defn->file = file;
5810 defn->file_len = file_len;
5812 /* OP is null if this is a predefinition */
5813 defn->predefined = !op;
5814 mdef.defn = defn;
5815 mdef.symnam = symname;
5816 mdef.symlen = sym_length;
5818 return mdef;
5820 nope:
5821 mdef.defn = 0;
5822 return mdef;
5825 /* Process a #define directive.
5826 BUF points to the contents of the #define directive, as a contiguous string.
5827 LIMIT points to the first character past the end of the definition.
5828 KEYWORD is the keyword-table entry for #define. */
5830 static int
5831 do_define (buf, limit, op, keyword)
5832 U_CHAR *buf, *limit;
5833 FILE_BUF *op;
5834 struct directive *keyword;
5836 int hashcode;
5837 MACRODEF mdef;
5839 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5840 if (pcp_outfile && op)
5841 pass_thru_directive (buf, limit, op, keyword);
5843 mdef = create_definition (buf, limit, op);
5844 if (mdef.defn == 0)
5845 goto nope;
5847 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
5850 HASHNODE *hp;
5851 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
5852 int ok = 0;
5853 /* Redefining a precompiled key is ok. */
5854 if (hp->type == T_PCSTRING)
5855 ok = 1;
5856 /* Redefining a macro is ok if the definitions are the same. */
5857 else if (hp->type == T_MACRO)
5858 ok = ! compare_defs (mdef.defn, hp->value.defn);
5859 /* Redefining a constant is ok with -D. */
5860 else if (hp->type == T_CONST)
5861 ok = ! done_initializing;
5862 /* Print the warning if it's not ok. */
5863 if (!ok) {
5864 /* If we are passing through #define and #undef directives, do
5865 that for this re-definition now. */
5866 if (debug_output && op)
5867 pass_thru_directive (buf, limit, op, keyword);
5869 pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
5870 if (hp->type == T_MACRO)
5871 pedwarn_with_file_and_line (hp->value.defn->file,
5872 hp->value.defn->file_len,
5873 hp->value.defn->line,
5874 "this is the location of the previous definition");
5876 /* Replace the old definition. */
5877 hp->type = T_MACRO;
5878 hp->value.defn = mdef.defn;
5879 } else {
5880 /* If we are passing through #define and #undef directives, do
5881 that for this new definition now. */
5882 if (debug_output && op)
5883 pass_thru_directive (buf, limit, op, keyword);
5884 install (mdef.symnam, mdef.symlen, T_MACRO,
5885 (char *) mdef.defn, hashcode);
5889 return 0;
5891 nope:
5893 return 1;
5896 /* Check a purported macro name SYMNAME, and yield its length.
5897 ASSERTION is nonzero if this is really for an assertion name. */
5899 static int
5900 check_macro_name (symname, assertion)
5901 U_CHAR *symname;
5902 int assertion;
5904 U_CHAR *p;
5905 int sym_length;
5907 for (p = symname; is_idchar[*p]; p++)
5909 sym_length = p - symname;
5910 if (sym_length == 0
5911 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
5912 error (assertion ? "invalid assertion name" : "invalid macro name");
5913 else if (!is_idstart[*symname]
5914 || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
5915 error ((assertion
5916 ? "invalid assertion name `%.*s'"
5917 : "invalid macro name `%.*s'"),
5918 sym_length, symname);
5919 return sym_length;
5922 /* Return zero if two DEFINITIONs are isomorphic. */
5924 static int
5925 compare_defs (d1, d2)
5926 DEFINITION *d1, *d2;
5928 register struct reflist *a1, *a2;
5929 register U_CHAR *p1 = d1->expansion;
5930 register U_CHAR *p2 = d2->expansion;
5931 int first = 1;
5933 if (d1->nargs != d2->nargs)
5934 return 1;
5935 if (pedantic
5936 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
5937 return 1;
5938 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
5939 a1 = a1->next, a2 = a2->next) {
5940 if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
5941 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
5942 || a1->argno != a2->argno
5943 || a1->stringify != a2->stringify
5944 || a1->raw_before != a2->raw_before
5945 || a1->raw_after != a2->raw_after)
5946 return 1;
5947 first = 0;
5948 p1 += a1->nchars;
5949 p2 += a2->nchars;
5951 if (a1 != a2)
5952 return 1;
5953 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
5954 p2, d2->length - (p2 - d2->expansion), 1))
5955 return 1;
5956 return 0;
5959 /* Return 1 if two parts of two macro definitions are effectively different.
5960 One of the parts starts at BEG1 and has LEN1 chars;
5961 the other has LEN2 chars at BEG2.
5962 Any sequence of whitespace matches any other sequence of whitespace.
5963 FIRST means these parts are the first of a macro definition;
5964 so ignore leading whitespace entirely.
5965 LAST means these parts are the last of a macro definition;
5966 so ignore trailing whitespace entirely. */
5968 static int
5969 comp_def_part (first, beg1, len1, beg2, len2, last)
5970 int first;
5971 U_CHAR *beg1, *beg2;
5972 int len1, len2;
5973 int last;
5975 register U_CHAR *end1 = beg1 + len1;
5976 register U_CHAR *end2 = beg2 + len2;
5977 if (first) {
5978 while (beg1 != end1 && is_space[*beg1]) beg1++;
5979 while (beg2 != end2 && is_space[*beg2]) beg2++;
5981 if (last) {
5982 while (beg1 != end1 && is_space[end1[-1]]) end1--;
5983 while (beg2 != end2 && is_space[end2[-1]]) end2--;
5985 while (beg1 != end1 && beg2 != end2) {
5986 if (is_space[*beg1] && is_space[*beg2]) {
5987 while (beg1 != end1 && is_space[*beg1]) beg1++;
5988 while (beg2 != end2 && is_space[*beg2]) beg2++;
5989 } else if (*beg1 == *beg2) {
5990 beg1++; beg2++;
5991 } else break;
5993 return (beg1 != end1) || (beg2 != end2);
5996 /* Read a replacement list for a macro with parameters.
5997 Build the DEFINITION structure.
5998 Reads characters of text starting at BUF until END.
5999 ARGLIST specifies the formal parameters to look for
6000 in the text of the definition; NARGS is the number of args
6001 in that list, or -1 for a macro name that wants no argument list.
6002 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6003 and NAMELEN is its length in characters.
6005 Note that comments, backslash-newlines, and leading white space
6006 have already been deleted from the argument. */
6008 /* If there is no trailing whitespace, a Newline Space is added at the end
6009 to prevent concatenation that would be contrary to the standard. */
6011 static DEFINITION *
6012 collect_expansion (buf, end, nargs, arglist)
6013 U_CHAR *buf, *end;
6014 int nargs;
6015 struct arglist *arglist;
6017 DEFINITION *defn;
6018 register U_CHAR *p, *limit, *lastp, *exp_p;
6019 struct reflist *endpat = NULL;
6020 /* Pointer to first nonspace after last ## seen. */
6021 U_CHAR *concat = 0;
6022 /* Pointer to first nonspace after last single-# seen. */
6023 U_CHAR *stringify = 0;
6024 /* How those tokens were spelled. */
6025 enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
6026 enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
6027 int maxsize;
6028 int expected_delimiter = '\0';
6030 /* Scan thru the replacement list, ignoring comments and quoted
6031 strings, picking up on the macro calls. It does a linear search
6032 thru the arg list on every potential symbol. Profiling might say
6033 that something smarter should happen. */
6035 if (end < buf)
6036 abort ();
6038 /* Find the beginning of the trailing whitespace. */
6039 limit = end;
6040 p = buf;
6041 while (p < limit && is_space[limit[-1]]) limit--;
6043 /* Allocate space for the text in the macro definition.
6044 Each input char may or may not need 1 byte,
6045 so this is an upper bound.
6046 The extra 3 are for invented trailing newline-marker and final null. */
6047 maxsize = (sizeof (DEFINITION)
6048 + (limit - p) + 3);
6049 defn = (DEFINITION *) xcalloc (1, maxsize);
6051 defn->nargs = nargs;
6052 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
6053 lastp = exp_p;
6055 if (p[0] == '#'
6056 ? p[1] == '#'
6057 : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
6058 error ("`##' at start of macro definition");
6059 p += p[0] == '#' ? 2 : 4;
6062 /* Process the main body of the definition. */
6063 while (p < limit) {
6064 int skipped_arg = 0;
6065 register U_CHAR c = *p++;
6067 *exp_p++ = c;
6069 if (!traditional) {
6070 switch (c) {
6071 case '\'':
6072 case '\"':
6073 if (expected_delimiter != '\0') {
6074 if (c == expected_delimiter)
6075 expected_delimiter = '\0';
6076 } else
6077 expected_delimiter = c;
6078 break;
6080 case '\\':
6081 if (expected_delimiter) {
6082 /* In a string, backslash goes through
6083 and makes next char ordinary. */
6084 *exp_p++ = *p++;
6086 break;
6088 case '%':
6089 if (!expected_delimiter && *p == ':') {
6090 /* %: is not a digraph if preceded by an odd number of '<'s. */
6091 U_CHAR *p0 = p - 1;
6092 while (buf < p0 && p0[-1] == '<')
6093 p0--;
6094 if ((p - p0) & 1) {
6095 /* Treat %:%: as ## and %: as #. */
6096 if (p[1] == '%' && p[2] == ':') {
6097 p += 2;
6098 goto sharp_sharp_token;
6100 if (nargs >= 0) {
6101 p++;
6102 goto sharp_token;
6106 break;
6108 case '#':
6109 /* # is ordinary inside a string. */
6110 if (expected_delimiter)
6111 break;
6112 if (*p == '#') {
6113 sharp_sharp_token:
6114 /* ##: concatenate preceding and following tokens. */
6115 /* Take out the first #, discard preceding whitespace. */
6116 exp_p--;
6117 while (exp_p > lastp && is_hor_space[exp_p[-1]])
6118 --exp_p;
6119 /* Skip the second #. */
6120 p++;
6121 concat_sharp_token_type = c;
6122 if (is_hor_space[*p]) {
6123 concat_sharp_token_type = c + 1;
6124 p++;
6125 SKIP_WHITE_SPACE (p);
6127 concat = p;
6128 if (p == limit)
6129 error ("`##' at end of macro definition");
6130 } else if (nargs >= 0) {
6131 /* Single #: stringify following argument ref.
6132 Don't leave the # in the expansion. */
6133 sharp_token:
6134 exp_p--;
6135 stringify_sharp_token_type = c;
6136 if (is_hor_space[*p]) {
6137 stringify_sharp_token_type = c + 1;
6138 p++;
6139 SKIP_WHITE_SPACE (p);
6141 if (! is_idstart[*p] || nargs == 0
6142 || (*p == 'L' && (p[1] == '\'' || p[1] == '"')))
6143 error ("`#' operator is not followed by a macro argument name");
6144 else
6145 stringify = p;
6147 break;
6149 } else {
6150 /* In -traditional mode, recognize arguments inside strings and
6151 and character constants, and ignore special properties of #.
6152 Arguments inside strings are considered "stringified", but no
6153 extra quote marks are supplied. */
6154 switch (c) {
6155 case '\'':
6156 case '\"':
6157 if (expected_delimiter != '\0') {
6158 if (c == expected_delimiter)
6159 expected_delimiter = '\0';
6160 } else
6161 expected_delimiter = c;
6162 break;
6164 case '\\':
6165 /* Backslash quotes delimiters and itself, but not macro args. */
6166 if (expected_delimiter != 0 && p < limit
6167 && (*p == expected_delimiter || *p == '\\')) {
6168 *exp_p++ = *p++;
6169 continue;
6171 break;
6173 case '/':
6174 if (expected_delimiter != '\0') /* No comments inside strings. */
6175 break;
6176 if (*p == '*') {
6177 /* If we find a comment that wasn't removed by handle_directive,
6178 this must be -traditional. So replace the comment with
6179 nothing at all. */
6180 exp_p--;
6181 while (++p < limit) {
6182 if (p[0] == '*' && p[1] == '/') {
6183 p += 2;
6184 break;
6187 #if 0
6188 /* Mark this as a concatenation-point, as if it had been ##. */
6189 concat = p;
6190 #endif
6192 break;
6196 #ifdef MULTIBYTE_CHARS
6197 /* Handle multibyte characters inside string and character literals. */
6198 if (expected_delimiter != '\0')
6200 int length;
6201 --p;
6202 length = local_mblen (p, limit - p);
6203 if (length > 1)
6205 --exp_p;
6206 bcopy (p, exp_p, length);
6207 p += length;
6208 exp_p += length;
6209 continue;
6211 ++p;
6213 #endif
6215 /* Handle the start of a symbol. */
6216 if (is_idchar[c] && nargs > 0) {
6217 U_CHAR *id_beg = p - 1;
6218 int id_len;
6220 --exp_p;
6221 while (p != limit && is_idchar[*p]) p++;
6222 id_len = p - id_beg;
6224 if (is_idstart[c]
6225 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
6226 register struct arglist *arg;
6228 for (arg = arglist; arg != NULL; arg = arg->next) {
6229 struct reflist *tpat;
6231 if (arg->name[0] == c
6232 && arg->length == id_len
6233 && bcmp (arg->name, id_beg, id_len) == 0) {
6234 enum sharp_token_type tpat_stringify;
6235 if (expected_delimiter) {
6236 if (warn_stringify) {
6237 if (traditional) {
6238 warning ("macro argument `%.*s' is stringified.",
6239 id_len, arg->name);
6240 } else {
6241 warning ("macro arg `%.*s' would be stringified with -traditional.",
6242 id_len, arg->name);
6245 /* If ANSI, don't actually substitute inside a string. */
6246 if (!traditional)
6247 break;
6248 tpat_stringify = SHARP_TOKEN;
6249 } else {
6250 tpat_stringify
6251 = (stringify == id_beg
6252 ? stringify_sharp_token_type : NO_SHARP_TOKEN);
6254 /* make a pat node for this arg and append it to the end of
6255 the pat list */
6256 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
6257 tpat->next = NULL;
6258 tpat->raw_before
6259 = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
6260 tpat->raw_after = NO_SHARP_TOKEN;
6261 tpat->rest_args = arg->rest_args;
6262 tpat->stringify = tpat_stringify;
6264 if (endpat == NULL)
6265 defn->pattern = tpat;
6266 else
6267 endpat->next = tpat;
6268 endpat = tpat;
6270 tpat->argno = arg->argno;
6271 tpat->nchars = exp_p - lastp;
6273 register U_CHAR *p1 = p;
6274 SKIP_WHITE_SPACE (p1);
6275 if (p1[0]=='#'
6276 ? p1[1]=='#'
6277 : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
6278 tpat->raw_after = p1[0] + (p != p1);
6280 lastp = exp_p; /* place to start copying from next time */
6281 skipped_arg = 1;
6282 break;
6287 /* If this was not a macro arg, copy it into the expansion. */
6288 if (! skipped_arg) {
6289 register U_CHAR *lim1 = p;
6290 p = id_beg;
6291 while (p != lim1)
6292 *exp_p++ = *p++;
6293 if (stringify == id_beg)
6294 error ("`#' operator should be followed by a macro argument name");
6299 if (!traditional && expected_delimiter == 0) {
6300 /* If ANSI, put in a newline-space marker to prevent token pasting.
6301 But not if "inside a string" (which in ANSI mode happens only for
6302 -D option). */
6303 *exp_p++ = '\n';
6304 *exp_p++ = ' ';
6307 *exp_p = '\0';
6309 defn->length = exp_p - defn->expansion;
6311 /* Crash now if we overrun the allocated size. */
6312 if (defn->length + 1 > maxsize)
6313 abort ();
6315 #if 0
6316 /* This isn't worth the time it takes. */
6317 /* give back excess storage */
6318 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
6319 #endif
6321 return defn;
6324 static int
6325 do_assert (buf, limit, op, keyword)
6326 U_CHAR *buf, *limit;
6327 FILE_BUF *op;
6328 struct directive *keyword;
6330 U_CHAR *bp; /* temp ptr into input buffer */
6331 U_CHAR *symname; /* remember where symbol name starts */
6332 int sym_length; /* and how long it is */
6333 struct arglist *tokens = NULL;
6335 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6336 pedwarn ("ANSI C does not allow `#assert'");
6338 bp = buf;
6340 while (is_hor_space[*bp])
6341 bp++;
6343 symname = bp; /* remember where it starts */
6344 sym_length = check_macro_name (bp, 1);
6345 bp += sym_length;
6346 /* #define doesn't do this, but we should. */
6347 SKIP_WHITE_SPACE (bp);
6349 /* Lossage will occur if identifiers or control tokens are broken
6350 across lines using backslash. This is not the right place to take
6351 care of that. */
6353 if (*bp != '(') {
6354 error ("missing token-sequence in `#assert'");
6355 return 1;
6359 int error_flag = 0;
6361 bp++; /* skip '(' */
6362 SKIP_WHITE_SPACE (bp);
6364 tokens = read_token_list (&bp, limit, &error_flag);
6365 if (error_flag)
6366 return 1;
6367 if (tokens == 0) {
6368 error ("empty token-sequence in `#assert'");
6369 return 1;
6372 ++bp; /* skip paren */
6373 SKIP_WHITE_SPACE (bp);
6376 /* If this name isn't already an assertion name, make it one.
6377 Error if it was already in use in some other way. */
6380 ASSERTION_HASHNODE *hp;
6381 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6382 struct tokenlist_list *value
6383 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6385 hp = assertion_lookup (symname, sym_length, hashcode);
6386 if (hp == NULL) {
6387 if (sym_length == 7 && ! bcmp (symname, "defined", 7))
6388 error ("`defined' redefined as assertion");
6389 hp = assertion_install (symname, sym_length, hashcode);
6392 /* Add the spec'd token-sequence to the list of such. */
6393 value->tokens = tokens;
6394 value->next = hp->value;
6395 hp->value = value;
6398 return 0;
6401 static int
6402 do_unassert (buf, limit, op, keyword)
6403 U_CHAR *buf, *limit;
6404 FILE_BUF *op;
6405 struct directive *keyword;
6407 U_CHAR *bp; /* temp ptr into input buffer */
6408 U_CHAR *symname; /* remember where symbol name starts */
6409 int sym_length; /* and how long it is */
6411 struct arglist *tokens = NULL;
6412 int tokens_specified = 0;
6414 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6415 pedwarn ("ANSI C does not allow `#unassert'");
6417 bp = buf;
6419 while (is_hor_space[*bp])
6420 bp++;
6422 symname = bp; /* remember where it starts */
6423 sym_length = check_macro_name (bp, 1);
6424 bp += sym_length;
6425 /* #define doesn't do this, but we should. */
6426 SKIP_WHITE_SPACE (bp);
6428 /* Lossage will occur if identifiers or control tokens are broken
6429 across lines using backslash. This is not the right place to take
6430 care of that. */
6432 if (*bp == '(') {
6433 int error_flag = 0;
6435 bp++; /* skip '(' */
6436 SKIP_WHITE_SPACE (bp);
6438 tokens = read_token_list (&bp, limit, &error_flag);
6439 if (error_flag)
6440 return 1;
6441 if (tokens == 0) {
6442 error ("empty token list in `#unassert'");
6443 return 1;
6446 tokens_specified = 1;
6448 ++bp; /* skip paren */
6449 SKIP_WHITE_SPACE (bp);
6453 ASSERTION_HASHNODE *hp;
6454 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6455 struct tokenlist_list *tail, *prev;
6457 hp = assertion_lookup (symname, sym_length, hashcode);
6458 if (hp == NULL)
6459 return 1;
6461 /* If no token list was specified, then eliminate this assertion
6462 entirely. */
6463 if (! tokens_specified) {
6464 struct tokenlist_list *next;
6465 for (tail = hp->value; tail; tail = next) {
6466 next = tail->next;
6467 free_token_list (tail->tokens);
6468 free (tail);
6470 delete_assertion (hp);
6471 } else {
6472 /* If a list of tokens was given, then delete any matching list. */
6474 tail = hp->value;
6475 prev = 0;
6476 while (tail) {
6477 struct tokenlist_list *next = tail->next;
6478 if (compare_token_lists (tail->tokens, tokens)) {
6479 if (prev)
6480 prev->next = next;
6481 else
6482 hp->value = tail->next;
6483 free_token_list (tail->tokens);
6484 free (tail);
6485 } else {
6486 prev = tail;
6488 tail = next;
6493 return 0;
6496 /* Test whether there is an assertion named NAME
6497 and optionally whether it has an asserted token list TOKENS.
6498 NAME is not null terminated; its length is SYM_LENGTH.
6499 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6502 check_assertion (name, sym_length, tokens_specified, tokens)
6503 U_CHAR *name;
6504 int sym_length;
6505 int tokens_specified;
6506 struct arglist *tokens;
6508 ASSERTION_HASHNODE *hp;
6509 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6511 if (pedantic && !instack[indepth].system_header_p)
6512 pedwarn ("ANSI C does not allow testing assertions");
6514 hp = assertion_lookup (name, sym_length, hashcode);
6515 if (hp == NULL)
6516 /* It is not an assertion; just return false. */
6517 return 0;
6519 /* If no token list was specified, then value is 1. */
6520 if (! tokens_specified)
6521 return 1;
6524 struct tokenlist_list *tail;
6526 tail = hp->value;
6528 /* If a list of tokens was given,
6529 then succeed if the assertion records a matching list. */
6531 while (tail) {
6532 if (compare_token_lists (tail->tokens, tokens))
6533 return 1;
6534 tail = tail->next;
6537 /* Fail if the assertion has no matching list. */
6538 return 0;
6542 /* Compare two lists of tokens for equality including order of tokens. */
6544 static int
6545 compare_token_lists (l1, l2)
6546 struct arglist *l1, *l2;
6548 while (l1 && l2) {
6549 if (l1->length != l2->length)
6550 return 0;
6551 if (bcmp (l1->name, l2->name, l1->length))
6552 return 0;
6553 l1 = l1->next;
6554 l2 = l2->next;
6557 /* Succeed if both lists end at the same time. */
6558 return l1 == l2;
6561 /* Read a space-separated list of tokens ending in a close parenthesis.
6562 Return a list of strings, in the order they were written.
6563 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6564 Parse the text starting at *BPP, and update *BPP.
6565 Don't parse beyond LIMIT. */
6567 static struct arglist *
6568 read_token_list (bpp, limit, error_flag)
6569 U_CHAR **bpp;
6570 U_CHAR *limit;
6571 int *error_flag;
6573 struct arglist *token_ptrs = 0;
6574 U_CHAR *bp = *bpp;
6575 int depth = 1;
6577 *error_flag = 0;
6579 /* Loop over the assertion value tokens. */
6580 while (depth > 0) {
6581 struct arglist *temp;
6582 int eofp = 0;
6583 U_CHAR *beg = bp;
6585 /* Find the end of the token. */
6586 if (*bp == '(') {
6587 bp++;
6588 depth++;
6589 } else if (*bp == ')') {
6590 depth--;
6591 if (depth == 0)
6592 break;
6593 bp++;
6594 } else if (*bp == '"' || *bp == '\'')
6595 bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
6596 else
6597 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
6598 && *bp != '"' && *bp != '\'' && bp != limit)
6599 bp++;
6601 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
6602 temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
6603 bcopy ((char *) beg, (char *) temp->name, bp - beg);
6604 temp->name[bp - beg] = 0;
6605 temp->next = token_ptrs;
6606 token_ptrs = temp;
6607 temp->length = bp - beg;
6609 SKIP_WHITE_SPACE (bp);
6611 if (bp >= limit) {
6612 error ("unterminated token sequence in `#assert' or `#unassert'");
6613 *error_flag = -1;
6614 return 0;
6617 *bpp = bp;
6619 /* We accumulated the names in reverse order.
6620 Now reverse them to get the proper order. */
6622 register struct arglist *prev = 0, *this, *next;
6623 for (this = token_ptrs; this; this = next) {
6624 next = this->next;
6625 this->next = prev;
6626 prev = this;
6628 return prev;
6632 static void
6633 free_token_list (tokens)
6634 struct arglist *tokens;
6636 while (tokens) {
6637 struct arglist *next = tokens->next;
6638 free (tokens->name);
6639 free (tokens);
6640 tokens = next;
6644 /* Install a name in the assertion hash table.
6646 If LEN is >= 0, it is the length of the name.
6647 Otherwise, compute the length by scanning the entire name.
6649 If HASH is >= 0, it is the precomputed hash code.
6650 Otherwise, compute the hash code. */
6652 static ASSERTION_HASHNODE *
6653 assertion_install (name, len, hash)
6654 U_CHAR *name;
6655 int len;
6656 int hash;
6658 register ASSERTION_HASHNODE *hp;
6659 register int i, bucket;
6660 register U_CHAR *p, *q;
6662 i = sizeof (ASSERTION_HASHNODE) + len + 1;
6663 hp = (ASSERTION_HASHNODE *) xmalloc (i);
6664 bucket = hash;
6665 hp->bucket_hdr = &assertion_hashtab[bucket];
6666 hp->next = assertion_hashtab[bucket];
6667 assertion_hashtab[bucket] = hp;
6668 hp->prev = NULL;
6669 if (hp->next != NULL)
6670 hp->next->prev = hp;
6671 hp->length = len;
6672 hp->value = 0;
6673 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
6674 p = hp->name;
6675 q = name;
6676 for (i = 0; i < len; i++)
6677 *p++ = *q++;
6678 hp->name[len] = 0;
6679 return hp;
6682 /* Find the most recent hash node for name name (ending with first
6683 non-identifier char) installed by install
6685 If LEN is >= 0, it is the length of the name.
6686 Otherwise, compute the length by scanning the entire name.
6688 If HASH is >= 0, it is the precomputed hash code.
6689 Otherwise, compute the hash code. */
6691 static ASSERTION_HASHNODE *
6692 assertion_lookup (name, len, hash)
6693 U_CHAR *name;
6694 int len;
6695 int hash;
6697 register ASSERTION_HASHNODE *bucket;
6699 bucket = assertion_hashtab[hash];
6700 while (bucket) {
6701 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
6702 return bucket;
6703 bucket = bucket->next;
6705 return NULL;
6708 static void
6709 delete_assertion (hp)
6710 ASSERTION_HASHNODE *hp;
6713 if (hp->prev != NULL)
6714 hp->prev->next = hp->next;
6715 if (hp->next != NULL)
6716 hp->next->prev = hp->prev;
6718 /* Make sure that the bucket chain header that the deleted guy was
6719 on points to the right thing afterwards. */
6720 if (hp == *hp->bucket_hdr)
6721 *hp->bucket_hdr = hp->next;
6723 free (hp);
6727 * interpret #line directive. Remembers previously seen fnames
6728 * in its very own hash table.
6730 #define FNAME_HASHSIZE 37
6732 static int
6733 do_line (buf, limit, op, keyword)
6734 U_CHAR *buf, *limit;
6735 FILE_BUF *op;
6736 struct directive *keyword;
6738 register U_CHAR *bp;
6739 FILE_BUF *ip = &instack[indepth];
6740 FILE_BUF tem;
6741 int new_lineno;
6742 enum file_change_code file_change = same_file;
6744 /* Expand any macros. */
6745 tem = expand_to_temp_buffer (buf, limit, 0, 0);
6747 /* Point to macroexpanded line, which is null-terminated now. */
6748 bp = tem.buf;
6749 limit = tem.bufp;
6750 SKIP_WHITE_SPACE (bp);
6752 if (!isdigit (*bp)) {
6753 error ("invalid format `#line' directive");
6754 return 0;
6757 /* The Newline at the end of this line remains to be processed.
6758 To put the next line at the specified line number,
6759 we must store a line number now that is one less. */
6760 new_lineno = atoi ((char *) bp) - 1;
6762 /* NEW_LINENO is one less than the actual line number here. */
6763 if (pedantic && new_lineno < 0)
6764 pedwarn ("line number out of range in `#line' directive");
6766 /* skip over the line number. */
6767 while (isdigit (*bp))
6768 bp++;
6770 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6771 if (*bp && !is_space[*bp]) {
6772 error ("invalid format `#line' directive");
6773 return;
6775 #endif
6777 SKIP_WHITE_SPACE (bp);
6779 if (*bp == '\"') {
6780 static HASHNODE *fname_table[FNAME_HASHSIZE];
6781 HASHNODE *hp, **hash_bucket;
6782 U_CHAR *fname, *p;
6783 int fname_length;
6785 fname = ++bp;
6787 /* Turn the file name, which is a character string literal,
6788 into a null-terminated string. Do this in place. */
6789 p = bp;
6790 for (;;)
6791 switch ((*p++ = *bp++)) {
6792 case '\\':
6794 char *bpc = (char *) bp;
6795 HOST_WIDE_INT c = parse_escape (&bpc, (HOST_WIDE_INT) (U_CHAR) (-1));
6796 bp = (U_CHAR *) bpc;
6797 if (c < 0)
6798 p--;
6799 else
6800 p[-1] = c;
6802 break;
6804 case '\"':
6805 *--p = 0;
6806 goto fname_done;
6808 fname_done:
6809 fname_length = p - fname;
6811 SKIP_WHITE_SPACE (bp);
6812 if (*bp) {
6813 if (pedantic)
6814 pedwarn ("garbage at end of `#line' directive");
6815 if (*bp == '1')
6816 file_change = enter_file;
6817 else if (*bp == '2')
6818 file_change = leave_file;
6819 else if (*bp == '3')
6820 ip->system_header_p = 1;
6821 else if (*bp == '4')
6822 ip->system_header_p = 2;
6823 else {
6824 error ("invalid format `#line' directive");
6825 return 0;
6828 bp++;
6829 SKIP_WHITE_SPACE (bp);
6830 if (*bp == '3') {
6831 ip->system_header_p = 1;
6832 bp++;
6833 SKIP_WHITE_SPACE (bp);
6835 if (*bp == '4') {
6836 ip->system_header_p = 2;
6837 bp++;
6838 SKIP_WHITE_SPACE (bp);
6840 if (*bp) {
6841 error ("invalid format `#line' directive");
6842 return 0;
6846 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
6847 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
6848 if (hp->length == fname_length &&
6849 bcmp (hp->value.cpval, fname, fname_length) == 0) {
6850 ip->nominal_fname = hp->value.cpval;
6851 ip->nominal_fname_len = fname_length;
6852 break;
6854 if (hp == 0) {
6855 /* Didn't find it; cons up a new one. */
6856 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
6857 hp->next = *hash_bucket;
6858 *hash_bucket = hp;
6860 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
6861 ip->nominal_fname_len = hp->length = fname_length;
6862 bcopy (fname, hp->value.cpval, fname_length + 1);
6864 } else if (*bp) {
6865 error ("invalid format `#line' directive");
6866 return 0;
6869 ip->lineno = new_lineno;
6870 output_line_directive (ip, op, 0, file_change);
6871 check_expand (op, ip->length - (ip->bufp - ip->buf));
6872 return 0;
6875 /* Remove the definition of a symbol from the symbol table.
6876 according to un*x /lib/cpp, it is not an error to undef
6877 something that has no definitions, so it isn't one here either. */
6879 static int
6880 do_undef (buf, limit, op, keyword)
6881 U_CHAR *buf, *limit;
6882 FILE_BUF *op;
6883 struct directive *keyword;
6885 int sym_length;
6886 HASHNODE *hp;
6887 U_CHAR *orig_buf = buf;
6889 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
6890 if (pcp_outfile && op)
6891 pass_thru_directive (buf, limit, op, keyword);
6893 SKIP_WHITE_SPACE (buf);
6894 sym_length = check_macro_name (buf, 0);
6896 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
6897 /* If we are generating additional info for debugging (with -g) we
6898 need to pass through all effective #undef directives. */
6899 if (debug_output && op)
6900 pass_thru_directive (orig_buf, limit, op, keyword);
6901 if (hp->type != T_MACRO)
6902 warning ("undefining `%s'", hp->name);
6903 delete_macro (hp);
6906 if (pedantic) {
6907 buf += sym_length;
6908 SKIP_WHITE_SPACE (buf);
6909 if (buf != limit)
6910 pedwarn ("garbage after `#undef' directive");
6912 return 0;
6915 /* Report an error detected by the program we are processing.
6916 Use the text of the line in the error message. */
6918 static int
6919 do_error (buf, limit, op, keyword)
6920 U_CHAR *buf, *limit;
6921 FILE_BUF *op;
6922 struct directive *keyword;
6924 int length = limit - buf;
6925 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
6926 bcopy ((char *) buf, (char *) copy, length);
6927 copy[length] = 0;
6928 SKIP_WHITE_SPACE (copy);
6930 switch (keyword->type) {
6931 case T_ERROR:
6932 error ("#error %s", copy);
6933 break;
6935 case T_WARNING:
6936 if (pedantic && !instack[indepth].system_header_p)
6937 pedwarn ("ANSI C does not allow `#warning'");
6938 warning ("#warning %s", copy);
6939 break;
6941 default:
6942 abort ();
6945 return 0;
6948 /* Remember the name of the current file being read from so that we can
6949 avoid ever including it again. */
6951 static void
6952 do_once ()
6954 int i;
6956 for (i = indepth; i >= 0; i--)
6957 if (instack[i].inc) {
6958 record_control_macro (instack[i].inc, (U_CHAR *) "");
6959 break;
6963 /* Report program identification. */
6965 static int
6966 do_ident (buf, limit, op, keyword)
6967 U_CHAR *buf, *limit;
6968 FILE_BUF *op;
6969 struct directive *keyword;
6971 FILE_BUF trybuf;
6972 int len;
6974 /* Allow #ident in system headers, since that's not user's fault. */
6975 if (pedantic && !instack[indepth].system_header_p)
6976 pedwarn ("ANSI C does not allow `#ident'");
6978 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
6979 buf = trybuf.buf;
6980 len = trybuf.bufp - buf;
6982 /* Output expanded directive. */
6983 check_expand (op, 7 + len);
6984 bcopy ("#ident ", (char *) op->bufp, 7);
6985 op->bufp += 7;
6986 bcopy ((char *) buf, (char *) op->bufp, len);
6987 op->bufp += len;
6989 free (buf);
6990 return 0;
6993 /* #pragma and its argument line have already been copied to the output file.
6994 Just check for some recognized pragmas that need validation here. */
6996 static int
6997 do_pragma (buf, limit, op, keyword)
6998 U_CHAR *buf, *limit;
6999 FILE_BUF *op;
7000 struct directive *keyword;
7002 SKIP_WHITE_SPACE (buf);
7003 if (!strncmp ((char *) buf, "once", 4)) {
7004 /* Allow #pragma once in system headers, since that's not the user's
7005 fault. */
7006 if (!instack[indepth].system_header_p)
7007 warning ("`#pragma once' is obsolete");
7008 do_once ();
7011 if (!strncmp ((char *) buf, "implementation", 14)) {
7012 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7013 been included yet. */
7015 int h;
7016 U_CHAR *p = buf + 14, *f, *fname;
7017 SKIP_WHITE_SPACE (p);
7018 if (*p != '\"')
7019 return 0;
7021 fname = p + 1;
7022 p = skip_quoted_string (p, limit, 0, NULL_PTR, NULL_PTR, NULL_PTR);
7023 if (p[-1] == '"')
7024 *--p = '\0';
7026 for (h = 0; h < INCLUDE_HASHSIZE; h++) {
7027 struct include_file *inc;
7028 for (inc = include_hashtab[h]; inc; inc = inc->next) {
7029 if (!strcmp (base_name (inc->fname), (char *) fname)) {
7030 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname);
7031 return 0;
7036 return 0;
7039 #if 0
7040 /* This was a fun hack, but #pragma seems to start to be useful.
7041 By failing to recognize it, we pass it through unchanged to cc1. */
7043 /* The behavior of the #pragma directive is implementation defined.
7044 this implementation defines it as follows. */
7046 static int
7047 do_pragma ()
7049 close (0);
7050 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
7051 goto nope;
7052 close (1);
7053 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
7054 goto nope;
7055 execl ("/usr/games/hack", "#pragma", 0);
7056 execl ("/usr/games/rogue", "#pragma", 0);
7057 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7058 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7059 nope:
7060 fatal ("You are in a maze of twisty compiler features, all different");
7062 #endif
7064 #ifdef SCCS_DIRECTIVE
7066 /* Just ignore #sccs, on systems where we define it at all. */
7068 static int
7069 do_sccs (buf, limit, op, keyword)
7070 U_CHAR *buf, *limit;
7071 FILE_BUF *op;
7072 struct directive *keyword;
7074 if (pedantic)
7075 pedwarn ("ANSI C does not allow `#sccs'");
7076 return 0;
7079 #endif /* defined (SCCS_DIRECTIVE) */
7081 /* Handle #if directive by
7082 1) inserting special `defined' keyword into the hash table
7083 that gets turned into 0 or 1 by special_symbol (thus,
7084 if the luser has a symbol called `defined' already, it won't
7085 work inside the #if directive)
7086 2) rescan the input into a temporary output buffer
7087 3) pass the output buffer to the yacc parser and collect a value
7088 4) clean up the mess left from steps 1 and 2.
7089 5) call conditional_skip to skip til the next #endif (etc.),
7090 or not, depending on the value from step 3. */
7092 static int
7093 do_if (buf, limit, op, keyword)
7094 U_CHAR *buf, *limit;
7095 FILE_BUF *op;
7096 struct directive *keyword;
7098 HOST_WIDE_INT value;
7099 FILE_BUF *ip = &instack[indepth];
7101 value = eval_if_expression (buf, limit - buf);
7102 conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
7103 return 0;
7106 /* Handle a #elif directive by not changing if_stack either.
7107 see the comment above do_else. */
7109 static int
7110 do_elif (buf, limit, op, keyword)
7111 U_CHAR *buf, *limit;
7112 FILE_BUF *op;
7113 struct directive *keyword;
7115 HOST_WIDE_INT value;
7116 FILE_BUF *ip = &instack[indepth];
7118 if (if_stack == instack[indepth].if_stack) {
7119 error ("`#elif' not within a conditional");
7120 return 0;
7121 } else {
7122 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7123 error ("`#elif' after `#else'");
7124 fprintf (stderr, " (matches line %d", if_stack->lineno);
7125 if (! (if_stack->fname_len == ip->nominal_fname_len
7126 && !bcmp (if_stack->fname, ip->nominal_fname,
7127 if_stack->fname_len))) {
7128 fprintf (stderr, ", file ");
7129 fwrite (if_stack->fname, sizeof if_stack->fname[0],
7130 if_stack->fname_len, stderr);
7132 fprintf (stderr, ")\n");
7134 if_stack->type = T_ELIF;
7137 if (if_stack->if_succeeded)
7138 skip_if_group (ip, 0, op);
7139 else {
7140 value = eval_if_expression (buf, limit - buf);
7141 if (value == 0)
7142 skip_if_group (ip, 0, op);
7143 else {
7144 ++if_stack->if_succeeded; /* continue processing input */
7145 output_line_directive (ip, op, 1, same_file);
7148 return 0;
7151 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7152 result as a C expression and return the value as an int. */
7154 static HOST_WIDE_INT
7155 eval_if_expression (buf, length)
7156 U_CHAR *buf;
7157 int length;
7159 FILE_BUF temp_obuf;
7160 HASHNODE *save_defined;
7161 HOST_WIDE_INT value;
7163 save_defined = install ((U_CHAR *) "defined", -1, T_SPEC_DEFINED,
7164 NULL_PTR, -1);
7165 pcp_inside_if = 1;
7166 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
7167 pcp_inside_if = 0;
7168 delete_macro (save_defined); /* clean up special symbol */
7170 *temp_obuf.bufp = '\n';
7171 value = parse_c_expression ((char *) temp_obuf.buf,
7172 warn_undef && !instack[indepth].system_header_p);
7174 free (temp_obuf.buf);
7176 return value;
7179 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7180 or don't skip to the #endif/#else/#elif depending on what directive
7181 is actually being processed. */
7183 static int
7184 do_xifdef (buf, limit, op, keyword)
7185 U_CHAR *buf, *limit;
7186 FILE_BUF *op;
7187 struct directive *keyword;
7189 int skip;
7190 FILE_BUF *ip = &instack[indepth];
7191 U_CHAR *end;
7192 int start_of_file = 0;
7193 U_CHAR *control_macro = 0;
7195 /* Detect a #ifndef at start of file (not counting comments). */
7196 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
7197 U_CHAR *p = ip->buf;
7198 while (p != directive_start) {
7199 U_CHAR c = *p++;
7200 if (is_space[c])
7202 /* Make no special provision for backslash-newline here; this is
7203 slower if backslash-newlines are present, but it's correct,
7204 and it's not worth it to tune for the rare backslash-newline. */
7205 else if (c == '/'
7206 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7207 /* Skip this comment. */
7208 int junk = 0;
7209 U_CHAR *save_bufp = ip->bufp;
7210 ip->bufp = p + 1;
7211 p = skip_to_end_of_comment (ip, &junk, 1);
7212 ip->bufp = save_bufp;
7213 } else {
7214 goto fail;
7217 /* If we get here, this conditional is the beginning of the file. */
7218 start_of_file = 1;
7219 fail: ;
7222 /* Discard leading and trailing whitespace. */
7223 SKIP_WHITE_SPACE (buf);
7224 while (limit != buf && is_hor_space[limit[-1]]) limit--;
7226 /* Find the end of the identifier at the beginning. */
7227 for (end = buf; is_idchar[*end]; end++);
7229 if (end == buf) {
7230 skip = (keyword->type == T_IFDEF);
7231 if (! traditional)
7232 pedwarn (end == limit ? "`#%s' with no argument"
7233 : "`#%s' argument starts with punctuation",
7234 keyword->name);
7235 } else {
7236 HASHNODE *hp;
7238 if (! traditional) {
7239 if (isdigit (buf[0]))
7240 pedwarn ("`#%s' argument starts with a digit", keyword->name);
7241 else if (end != limit)
7242 pedwarn ("garbage at end of `#%s' argument", keyword->name);
7245 hp = lookup (buf, end-buf, -1);
7247 if (pcp_outfile) {
7248 /* Output a precondition for this macro. */
7249 if (hp
7250 && (hp->type == T_CONST
7251 || (hp->type == T_MACRO && hp->value.defn->predefined)))
7252 fprintf (pcp_outfile, "#define %s\n", hp->name);
7253 else {
7254 U_CHAR *cp = buf;
7255 fprintf (pcp_outfile, "#undef ");
7256 while (is_idchar[*cp]) /* Ick! */
7257 fputc (*cp++, pcp_outfile);
7258 putc ('\n', pcp_outfile);
7262 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
7263 if (start_of_file && !skip) {
7264 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
7265 bcopy ((char *) buf, (char *) control_macro, end - buf);
7266 control_macro[end - buf] = 0;
7270 conditional_skip (ip, skip, T_IF, control_macro, op);
7271 return 0;
7274 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7275 If this is a #ifndef starting at the beginning of a file,
7276 CONTROL_MACRO is the macro name tested by the #ifndef.
7277 Otherwise, CONTROL_MACRO is 0. */
7279 static void
7280 conditional_skip (ip, skip, type, control_macro, op)
7281 FILE_BUF *ip;
7282 int skip;
7283 enum node_type type;
7284 U_CHAR *control_macro;
7285 FILE_BUF *op;
7287 IF_STACK_FRAME *temp;
7289 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7290 temp->fname = ip->nominal_fname;
7291 temp->fname_len = ip->nominal_fname_len;
7292 temp->lineno = ip->lineno;
7293 temp->next = if_stack;
7294 temp->control_macro = control_macro;
7295 if_stack = temp;
7297 if_stack->type = type;
7299 if (skip != 0) {
7300 skip_if_group (ip, 0, op);
7301 return;
7302 } else {
7303 ++if_stack->if_succeeded;
7304 output_line_directive (ip, &outbuf, 1, same_file);
7308 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7309 Leaves input ptr at the sharp sign found.
7310 If ANY is nonzero, return at next directive of any sort. */
7312 static void
7313 skip_if_group (ip, any, op)
7314 FILE_BUF *ip;
7315 int any;
7316 FILE_BUF *op;
7318 register U_CHAR *bp = ip->bufp, *cp;
7319 register U_CHAR *endb = ip->buf + ip->length;
7320 struct directive *kt;
7321 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
7322 U_CHAR *beg_of_line = bp;
7323 register int ident_length;
7324 U_CHAR *ident, *after_ident;
7325 /* Save info about where the group starts. */
7326 U_CHAR *beg_of_group = bp;
7327 int beg_lineno = ip->lineno;
7328 int skipping_include_directive = 0;
7330 if (output_conditionals && op != 0) {
7331 char *ptr = "#failed\n";
7332 int len = strlen (ptr);
7334 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7336 *op->bufp++ = '\n';
7337 op->lineno++;
7339 check_expand (op, len);
7340 bcopy (ptr, (char *) op->bufp, len);
7341 op->bufp += len;
7342 op->lineno++;
7343 output_line_directive (ip, op, 1, 0);
7346 while (bp < endb) {
7347 switch (*bp++) {
7348 case '/': /* possible comment */
7349 if (*bp == '\\')
7350 newline_fix (bp);
7351 if (*bp == '*'
7352 || (cplusplus_comments && *bp == '/')) {
7353 ip->bufp = ++bp;
7354 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
7356 break;
7357 case '<':
7358 if (skipping_include_directive) {
7359 while (bp < endb && *bp != '>' && *bp != '\n') {
7360 if (*bp == '\\' && bp[1] == '\n') {
7361 ip->lineno++;
7362 bp++;
7364 bp++;
7367 break;
7368 case '\"':
7369 if (skipping_include_directive) {
7370 while (bp < endb && *bp != '\n') {
7371 if (*bp == '"') {
7372 bp++;
7373 break;
7375 if (*bp == '\\' && bp[1] == '\n') {
7376 ip->lineno++;
7377 bp++;
7379 bp++;
7381 break;
7383 /* Fall through. */
7384 case '\'':
7385 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
7386 NULL_PTR, NULL_PTR);
7387 break;
7388 case '\\':
7389 /* Char after backslash loses its special meaning in some cases. */
7390 if (*bp == '\n') {
7391 ++ip->lineno;
7392 bp++;
7393 } else if (traditional && bp < endb)
7394 bp++;
7395 break;
7396 case '\n':
7397 ++ip->lineno;
7398 beg_of_line = bp;
7399 skipping_include_directive = 0;
7400 break;
7401 case '%':
7402 if (beg_of_line == 0 || traditional)
7403 break;
7404 ip->bufp = bp - 1;
7405 while (bp[0] == '\\' && bp[1] == '\n')
7406 bp += 2;
7407 if (*bp == ':')
7408 goto sharp_token;
7409 break;
7410 case '#':
7411 /* # keyword: a # must be first nonblank char on the line */
7412 if (beg_of_line == 0)
7413 break;
7414 ip->bufp = bp - 1;
7415 sharp_token:
7416 /* Scan from start of line, skipping whitespace, comments
7417 and backslash-newlines, and see if we reach this #.
7418 If not, this # is not special. */
7419 bp = beg_of_line;
7420 /* If -traditional, require # to be at beginning of line. */
7421 if (!traditional) {
7422 while (1) {
7423 if (is_hor_space[*bp])
7424 bp++;
7425 else if (*bp == '\\' && bp[1] == '\n')
7426 bp += 2;
7427 else if (*bp == '/' && bp[1] == '*') {
7428 bp += 2;
7429 while (1)
7431 if (*bp == '*')
7433 if (bp[1] == '/')
7435 bp += 2;
7436 break;
7439 else
7441 #ifdef MULTIBYTE_CHARS
7442 int length;
7443 length = local_mblen (bp, endb - bp);
7444 if (length > 1)
7445 bp += (length - 1);
7446 #endif
7448 bp++;
7451 /* There is no point in trying to deal with C++ // comments here,
7452 because if there is one, then this # must be part of the
7453 comment and we would never reach here. */
7454 else break;
7457 if (bp != ip->bufp) {
7458 bp = ip->bufp + 1; /* Reset bp to after the #. */
7459 break;
7462 bp = ip->bufp + 1; /* Point after the '#' */
7463 if (ip->bufp[0] == '%') {
7464 /* Skip past the ':' again. */
7465 while (*bp == '\\') {
7466 ip->lineno++;
7467 bp += 2;
7469 bp++;
7472 /* Skip whitespace and \-newline. */
7473 while (1) {
7474 if (is_hor_space[*bp])
7475 bp++;
7476 else if (*bp == '\\' && bp[1] == '\n')
7477 bp += 2;
7478 else if (*bp == '/') {
7479 if (bp[1] == '\\')
7480 newline_fix (bp + 1);
7481 if (bp[1] == '*') {
7482 for (bp += 2; ; bp++) {
7483 if (*bp == '\n')
7484 ip->lineno++;
7485 else if (*bp == '*') {
7486 if (bp[-1] == '/' && warn_comments)
7487 warning ("`/*' within comment");
7488 if (bp[1] == '\\')
7489 newline_fix (bp + 1);
7490 if (bp[1] == '/')
7491 break;
7493 else
7495 #ifdef MULTIBYTE_CHARS
7496 int length;
7497 length = local_mblen (bp, endb - bp);
7498 if (length > 1)
7499 bp += (length - 1);
7500 #endif
7503 bp += 2;
7504 } else if (bp[1] == '/' && cplusplus_comments) {
7505 for (bp += 2; ; bp++) {
7506 if (*bp == '\n') {
7507 if (bp[-1] != '\\')
7508 break;
7509 if (warn_comments)
7510 warning ("multiline `//' comment");
7511 ip->lineno++;
7513 else
7515 #ifdef MULTIBYTE_CHARS
7516 int length;
7517 length = local_mblen (bp, endb - bp);
7518 if (length > 1)
7519 bp += (length - 1);
7520 #endif
7523 } else
7524 break;
7525 } else
7526 break;
7529 cp = bp;
7531 /* Now find end of directive name.
7532 If we encounter a backslash-newline, exchange it with any following
7533 symbol-constituents so that we end up with a contiguous name. */
7535 while (1) {
7536 if (is_idchar[*bp])
7537 bp++;
7538 else {
7539 if (*bp == '\\')
7540 name_newline_fix (bp);
7541 if (is_idchar[*bp])
7542 bp++;
7543 else break;
7546 ident_length = bp - cp;
7547 ident = cp;
7548 after_ident = bp;
7550 /* A line of just `#' becomes blank. */
7552 if (ident_length == 0 && *after_ident == '\n') {
7553 continue;
7556 if (ident_length == 0 || !is_idstart[*ident]) {
7557 U_CHAR *p = ident;
7558 while (is_idchar[*p]) {
7559 if (*p < '0' || *p > '9')
7560 break;
7561 p++;
7563 /* Handle # followed by a line number. */
7564 if (p != ident && !is_idchar[*p]) {
7565 if (pedantic)
7566 pedwarn ("`#' followed by integer");
7567 continue;
7570 /* Avoid error for `###' and similar cases unless -pedantic. */
7571 if (p == ident) {
7572 while (*p == '#' || is_hor_space[*p]) p++;
7573 if (*p == '\n') {
7574 if (pedantic && !lang_asm)
7575 pedwarn ("invalid preprocessing directive");
7576 continue;
7580 if (!lang_asm && pedantic)
7581 pedwarn ("invalid preprocessing directive name");
7582 continue;
7585 for (kt = directive_table; kt->length >= 0; kt++) {
7586 IF_STACK_FRAME *temp;
7587 if (ident_length == kt->length
7588 && bcmp (cp, kt->name, kt->length) == 0) {
7589 /* If we are asked to return on next directive, do so now. */
7590 if (any)
7591 goto done;
7593 switch (kt->type) {
7594 case T_IF:
7595 case T_IFDEF:
7596 case T_IFNDEF:
7597 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7598 temp->next = if_stack;
7599 if_stack = temp;
7600 temp->lineno = ip->lineno;
7601 temp->fname = ip->nominal_fname;
7602 temp->fname_len = ip->nominal_fname_len;
7603 temp->type = kt->type;
7604 break;
7605 case T_ELSE:
7606 case T_ENDIF:
7607 if (pedantic && if_stack != save_if_stack)
7608 validate_else (bp, endb);
7609 case T_ELIF:
7610 if (if_stack == instack[indepth].if_stack) {
7611 error ("`#%s' not within a conditional", kt->name);
7612 break;
7614 else if (if_stack == save_if_stack)
7615 goto done; /* found what we came for */
7617 if (kt->type != T_ENDIF) {
7618 if (if_stack->type == T_ELSE)
7619 error ("`#else' or `#elif' after `#else'");
7620 if_stack->type = kt->type;
7621 break;
7624 temp = if_stack;
7625 if_stack = if_stack->next;
7626 free (temp);
7627 break;
7629 case T_INCLUDE:
7630 case T_INCLUDE_NEXT:
7631 case T_IMPORT:
7632 skipping_include_directive = 1;
7633 break;
7635 default:
7636 break;
7638 break;
7641 /* Don't let erroneous code go by. */
7642 if (kt->length < 0 && !lang_asm && pedantic)
7643 pedwarn ("invalid preprocessing directive name");
7647 ip->bufp = bp;
7648 /* after this returns, rescan will exit because ip->bufp
7649 now points to the end of the buffer.
7650 rescan is responsible for the error message also. */
7652 done:
7653 if (output_conditionals && op != 0) {
7654 char *ptr = "#endfailed\n";
7655 int len = strlen (ptr);
7657 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7659 *op->bufp++ = '\n';
7660 op->lineno++;
7662 check_expand (op, beg_of_line - beg_of_group);
7663 bcopy ((char *) beg_of_group, (char *) op->bufp,
7664 beg_of_line - beg_of_group);
7665 op->bufp += beg_of_line - beg_of_group;
7666 op->lineno += ip->lineno - beg_lineno;
7667 check_expand (op, len);
7668 bcopy (ptr, (char *) op->bufp, len);
7669 op->bufp += len;
7670 op->lineno++;
7674 /* Handle a #else directive. Do this by just continuing processing
7675 without changing if_stack ; this is so that the error message
7676 for missing #endif's etc. will point to the original #if. It
7677 is possible that something different would be better. */
7679 static int
7680 do_else (buf, limit, op, keyword)
7681 U_CHAR *buf, *limit;
7682 FILE_BUF *op;
7683 struct directive *keyword;
7685 FILE_BUF *ip = &instack[indepth];
7687 if (pedantic) {
7688 SKIP_WHITE_SPACE (buf);
7689 if (buf != limit)
7690 pedwarn ("text following `#else' violates ANSI standard");
7693 if (if_stack == instack[indepth].if_stack) {
7694 error ("`#else' not within a conditional");
7695 return 0;
7696 } else {
7697 /* #ifndef can't have its special treatment for containing the whole file
7698 if it has a #else clause. */
7699 if_stack->control_macro = 0;
7701 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7702 error ("`#else' after `#else'");
7703 fprintf (stderr, " (matches line %d", if_stack->lineno);
7704 if (! (if_stack->fname_len == ip->nominal_fname_len
7705 && !bcmp (if_stack->fname, ip->nominal_fname,
7706 if_stack->fname_len))) {
7707 fprintf (stderr, ", file ");
7708 fwrite (if_stack->fname, sizeof if_stack->fname[0],
7709 if_stack->fname_len, stderr);
7711 fprintf (stderr, ")\n");
7713 if_stack->type = T_ELSE;
7716 if (if_stack->if_succeeded)
7717 skip_if_group (ip, 0, op);
7718 else {
7719 ++if_stack->if_succeeded; /* continue processing input */
7720 output_line_directive (ip, op, 1, same_file);
7722 return 0;
7725 /* Unstack after #endif directive. */
7727 static int
7728 do_endif (buf, limit, op, keyword)
7729 U_CHAR *buf, *limit;
7730 FILE_BUF *op;
7731 struct directive *keyword;
7733 if (pedantic) {
7734 SKIP_WHITE_SPACE (buf);
7735 if (buf != limit)
7736 pedwarn ("text following `#endif' violates ANSI standard");
7739 if (if_stack == instack[indepth].if_stack)
7740 error ("unbalanced `#endif'");
7741 else {
7742 IF_STACK_FRAME *temp = if_stack;
7743 if_stack = if_stack->next;
7744 if (temp->control_macro != 0) {
7745 /* This #endif matched a #ifndef at the start of the file.
7746 See if it is at the end of the file. */
7747 FILE_BUF *ip = &instack[indepth];
7748 U_CHAR *p = ip->bufp;
7749 U_CHAR *ep = ip->buf + ip->length;
7751 while (p != ep) {
7752 U_CHAR c = *p++;
7753 if (!is_space[c]) {
7754 if (c == '/'
7755 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7756 /* Skip this comment. */
7757 int junk = 0;
7758 U_CHAR *save_bufp = ip->bufp;
7759 ip->bufp = p + 1;
7760 p = skip_to_end_of_comment (ip, &junk, 1);
7761 ip->bufp = save_bufp;
7762 } else
7763 goto fail;
7766 /* If we get here, this #endif ends a #ifndef
7767 that contains all of the file (aside from whitespace).
7768 Arrange not to include the file again
7769 if the macro that was tested is defined.
7771 Do not do this for the top-level file in a -include or any
7772 file in a -imacros. */
7773 if (indepth != 0
7774 && ! (indepth == 1 && no_record_file)
7775 && ! (no_record_file && no_output))
7776 record_control_macro (ip->inc, temp->control_macro);
7777 fail: ;
7779 free (temp);
7780 output_line_directive (&instack[indepth], op, 1, same_file);
7782 return 0;
7785 /* When an #else or #endif is found while skipping failed conditional,
7786 if -pedantic was specified, this is called to warn about text after
7787 the directive name. P points to the first char after the directive
7788 name. */
7790 static void
7791 validate_else (p, limit)
7792 register U_CHAR *p;
7793 register U_CHAR *limit;
7795 /* Advance P over whitespace and comments. */
7796 while (1) {
7797 while (*p == '\\' && p[1] == '\n')
7798 p += 2;
7799 if (is_hor_space[*p])
7800 p++;
7801 else if (*p == '/') {
7802 while (p[1] == '\\' && p[2] == '\n')
7803 p += 2;
7804 if (p[1] == '*') {
7805 /* Don't bother warning about unterminated comments
7806 since that will happen later. Just be sure to exit. */
7807 for (p += 2; ; p++) {
7808 if (p == limit)
7809 return;
7810 if (*p == '*') {
7811 while (p[1] == '\\' && p[2] == '\n')
7812 p += 2;
7813 if (p[1] == '/') {
7814 p += 2;
7815 break;
7818 else
7820 #ifdef MULTIBYTE_CHARS
7821 int length;
7822 length = local_mblen (p, limit - p);
7823 if (length > 1)
7824 p += (length - 1);
7825 #endif
7829 else if (cplusplus_comments && p[1] == '/')
7830 return;
7831 else break;
7832 } else break;
7834 if (*p != '\n')
7835 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
7838 /* Skip a comment, assuming the input ptr immediately follows the
7839 initial slash-star. Bump *LINE_COUNTER for each newline.
7840 (The canonical line counter is &ip->lineno.)
7841 Don't use this routine (or the next one) if bumping the line
7842 counter is not sufficient to deal with newlines in the string.
7844 If NOWARN is nonzero, don't warn about slash-star inside a comment.
7845 This feature is useful when processing a comment that is going to
7846 be processed or was processed at another point in the preprocessor,
7847 to avoid a duplicate warning. Likewise for unterminated comment
7848 errors. */
7850 static U_CHAR *
7851 skip_to_end_of_comment (ip, line_counter, nowarn)
7852 register FILE_BUF *ip;
7853 int *line_counter; /* place to remember newlines, or NULL */
7854 int nowarn;
7856 register U_CHAR *limit = ip->buf + ip->length;
7857 register U_CHAR *bp = ip->bufp;
7858 FILE_BUF *op = put_out_comments && !line_counter ? &outbuf : (FILE_BUF *) 0;
7859 int start_line = line_counter ? *line_counter : 0;
7861 /* JF this line_counter stuff is a crock to make sure the
7862 comment is only put out once, no matter how many times
7863 the comment is skipped. It almost works */
7864 if (op) {
7865 *op->bufp++ = '/';
7866 *op->bufp++ = bp[-1];
7868 if (cplusplus_comments && bp[-1] == '/') {
7869 for (; bp < limit; bp++) {
7870 if (*bp == '\n') {
7871 if (bp[-1] != '\\')
7872 break;
7873 if (!nowarn && warn_comments)
7874 warning ("multiline `//' comment");
7875 if (line_counter)
7876 ++*line_counter;
7877 if (op)
7878 ++op->lineno;
7880 else
7882 #ifdef MULTIBYTE_CHARS
7883 int length;
7884 length = local_mblen (bp, limit - bp);
7885 if (length > 1)
7887 if (op)
7889 bcopy (bp, op->bufp, length - 1);
7890 op->bufp += (length - 1);
7892 bp += (length - 1);
7894 #endif
7896 if (op)
7897 *op->bufp++ = *bp;
7899 ip->bufp = bp;
7900 return bp;
7902 while (bp < limit) {
7903 if (op)
7904 *op->bufp++ = *bp;
7905 switch (*bp++) {
7906 case '\n':
7907 /* If this is the end of the file, we have an unterminated comment.
7908 Don't swallow the newline. We are guaranteed that there will be a
7909 trailing newline and various pieces assume it's there. */
7910 if (bp == limit)
7912 --bp;
7913 --limit;
7914 break;
7916 if (line_counter != NULL)
7917 ++*line_counter;
7918 if (op)
7919 ++op->lineno;
7920 break;
7921 case '*':
7922 if (bp[-2] == '/' && !nowarn && warn_comments)
7923 warning ("`/*' within comment");
7924 if (*bp == '\\')
7925 newline_fix (bp);
7926 if (*bp == '/') {
7927 if (op)
7928 *op->bufp++ = '/';
7929 ip->bufp = ++bp;
7930 return bp;
7932 break;
7933 #ifdef MULTIBYTE_CHARS
7934 default:
7936 int length;
7937 bp--;
7938 length = local_mblen (bp, limit - bp);
7939 if (length <= 0)
7940 length = 1;
7941 if (op)
7943 op->bufp--;
7944 bcopy (bp, op->bufp, length);
7945 op->bufp += length;
7947 bp += length;
7949 #endif
7953 if (!nowarn)
7954 error_with_line (line_for_error (start_line), "unterminated comment");
7955 ip->bufp = bp;
7956 return bp;
7959 /* Skip over a quoted string. BP points to the opening quote.
7960 Returns a pointer after the closing quote. Don't go past LIMIT.
7961 START_LINE is the line number of the starting point (but it need
7962 not be valid if the starting point is inside a macro expansion).
7964 The input stack state is not changed.
7966 If COUNT_NEWLINES is nonzero, it points to an int to increment
7967 for each newline passed; also, warn about any white space
7968 just before line end.
7970 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
7971 if we pass a backslash-newline.
7973 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
7975 static U_CHAR *
7976 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
7977 register U_CHAR *bp;
7978 register U_CHAR *limit;
7979 int start_line;
7980 int *count_newlines;
7981 int *backslash_newlines_p;
7982 int *eofp;
7984 register U_CHAR c, match;
7986 match = *bp++;
7987 while (1) {
7988 if (bp >= limit) {
7989 error_with_line (line_for_error (start_line),
7990 "unterminated string or character constant");
7991 error_with_line (multiline_string_line,
7992 "possible real start of unterminated constant");
7993 multiline_string_line = 0;
7994 if (eofp)
7995 *eofp = 1;
7996 break;
7998 c = *bp++;
7999 if (c == '\\') {
8000 while (*bp == '\\' && bp[1] == '\n') {
8001 if (backslash_newlines_p)
8002 *backslash_newlines_p = 1;
8003 if (count_newlines)
8004 ++*count_newlines;
8005 bp += 2;
8007 if (*bp == '\n') {
8008 if (backslash_newlines_p)
8009 *backslash_newlines_p = 1;
8010 if (count_newlines)
8011 ++*count_newlines;
8013 bp++;
8014 } else if (c == '\n') {
8015 if (traditional) {
8016 /* Unterminated strings and character constants are 'valid'. */
8017 bp--; /* Don't consume the newline. */
8018 if (eofp)
8019 *eofp = 1;
8020 break;
8022 if (match == '\'') {
8023 error_with_line (line_for_error (start_line),
8024 "unterminated character constant");
8025 bp--;
8026 if (eofp)
8027 *eofp = 1;
8028 break;
8030 /* If not traditional, then allow newlines inside strings. */
8031 if (count_newlines) {
8032 if (warn_white_space && is_hor_space[bp[-2]])
8033 warning ("white space at end of line in string");
8034 ++*count_newlines;
8036 if (multiline_string_line == 0) {
8037 if (pedantic)
8038 pedwarn_with_line (line_for_error (start_line),
8039 "string constant runs past end of line");
8040 multiline_string_line = start_line;
8042 } else if (c == match)
8043 break;
8044 #ifdef MULTIBYTE_CHARS
8046 int length;
8047 --bp;
8048 length = local_mblen (bp, limit - bp);
8049 if (length <= 0)
8050 length = 1;
8051 bp += length;
8053 #endif
8055 return bp;
8058 /* Place into DST a quoted string representing the string SRC.
8059 SRCLEN is the length of SRC; SRC may contain null bytes.
8060 Return the address of DST's terminating null. */
8062 static char *
8063 quote_string (dst, src, srclen)
8064 char *dst, *src;
8065 size_t srclen;
8067 U_CHAR c;
8068 char *srclim = src + srclen;
8070 *dst++ = '\"';
8071 while (src != srclim)
8072 switch ((c = *src++))
8074 default:
8075 if (isprint (c))
8076 *dst++ = c;
8077 else
8079 sprintf (dst, "\\%03o", c);
8080 dst += 4;
8082 break;
8084 case '\"':
8085 case '\\':
8086 *dst++ = '\\';
8087 *dst++ = c;
8088 break;
8091 *dst++ = '\"';
8092 *dst = '\0';
8093 return dst;
8096 /* Skip across a group of balanced parens, starting from IP->bufp.
8097 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8099 This does not handle newlines, because it's used for the arg of #if,
8100 where there aren't any newlines. Also, backslash-newline can't appear. */
8102 static U_CHAR *
8103 skip_paren_group (ip)
8104 register FILE_BUF *ip;
8106 U_CHAR *limit = ip->buf + ip->length;
8107 U_CHAR *p = ip->bufp;
8108 int depth = 0;
8109 int lines_dummy = 0;
8111 while (p != limit) {
8112 int c = *p++;
8113 switch (c) {
8114 case '(':
8115 depth++;
8116 break;
8118 case ')':
8119 depth--;
8120 if (depth == 0)
8121 return ip->bufp = p;
8122 break;
8124 case '/':
8125 if (*p == '*') {
8126 ip->bufp = p;
8127 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
8128 p = ip->bufp;
8131 case '"':
8132 case '\'':
8134 int eofp = 0;
8135 p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
8136 if (eofp)
8137 return ip->bufp = p;
8139 break;
8143 ip->bufp = p;
8144 return p;
8147 /* Write out a #line directive, for instance, after an #include file.
8148 If CONDITIONAL is nonzero, we can omit the #line if it would
8149 appear to be a no-op, and we can output a few newlines instead
8150 if we want to increase the line number by a small amount.
8151 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8153 static void
8154 output_line_directive (ip, op, conditional, file_change)
8155 FILE_BUF *ip, *op;
8156 int conditional;
8157 enum file_change_code file_change;
8159 int len;
8160 char *line_directive_buf, *line_end;
8162 if (no_line_directives
8163 || ip->fname == NULL
8164 || no_output) {
8165 op->lineno = ip->lineno;
8166 return;
8169 if (conditional) {
8170 if (ip->lineno == op->lineno)
8171 return;
8173 /* If the inherited line number is a little too small,
8174 output some newlines instead of a #line directive. */
8175 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
8176 check_expand (op, 10);
8177 while (ip->lineno > op->lineno) {
8178 *op->bufp++ = '\n';
8179 op->lineno++;
8181 return;
8185 /* Output a positive line number if possible. */
8186 while (ip->lineno <= 0 && ip->bufp - ip->buf < ip->length
8187 && *ip->bufp == '\n') {
8188 ip->lineno++;
8189 ip->bufp++;
8192 line_directive_buf = (char *) alloca (4 * ip->nominal_fname_len + 100);
8193 sprintf (line_directive_buf, "# %d ", ip->lineno);
8194 line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
8195 ip->nominal_fname, ip->nominal_fname_len);
8196 if (file_change != same_file) {
8197 *line_end++ = ' ';
8198 *line_end++ = file_change == enter_file ? '1' : '2';
8200 /* Tell cc1 if following text comes from a system header file. */
8201 if (ip->system_header_p) {
8202 *line_end++ = ' ';
8203 *line_end++ = '3';
8205 #ifndef NO_IMPLICIT_EXTERN_C
8206 /* Tell cc1plus if following text should be treated as C. */
8207 if (ip->system_header_p == 2 && cplusplus) {
8208 *line_end++ = ' ';
8209 *line_end++ = '4';
8211 #endif
8212 *line_end++ = '\n';
8213 len = line_end - line_directive_buf;
8214 check_expand (op, len + 1);
8215 if (op->bufp > op->buf && op->bufp[-1] != '\n')
8216 *op->bufp++ = '\n';
8217 bcopy ((char *) line_directive_buf, (char *) op->bufp, len);
8218 op->bufp += len;
8219 op->lineno = ip->lineno;
8222 /* This structure represents one parsed argument in a macro call.
8223 `raw' points to the argument text as written (`raw_length' is its length).
8224 `expanded' points to the argument's macro-expansion
8225 (its length is `expand_length', and its allocated size is `expand_size').
8226 `stringified_length_bound' is an upper bound on the length
8227 the argument would have if stringified.
8228 `use_count' is the number of times this macro arg is substituted
8229 into the macro. If the actual use count exceeds 10,
8230 the value stored is 10.
8231 `free1' and `free2', if nonzero, point to blocks to be freed
8232 when the macro argument data is no longer needed. */
8234 struct argdata {
8235 U_CHAR *raw, *expanded;
8236 int raw_length, expand_length, expand_size;
8237 int stringified_length_bound;
8238 U_CHAR *free1, *free2;
8239 char newlines;
8240 char use_count;
8243 /* Expand a macro call.
8244 HP points to the symbol that is the macro being called.
8245 Put the result of expansion onto the input stack
8246 so that subsequent input by our caller will use it.
8248 If macro wants arguments, caller has already verified that
8249 an argument list follows; arguments come from the input stack. */
8251 static void
8252 macroexpand (hp, op)
8253 HASHNODE *hp;
8254 FILE_BUF *op;
8256 int nargs;
8257 DEFINITION *defn = hp->value.defn;
8258 register U_CHAR *xbuf;
8259 int xbuf_len;
8260 int start_line = instack[indepth].lineno;
8261 int rest_args, rest_zero;
8263 CHECK_DEPTH (return;);
8265 /* it might not actually be a macro. */
8266 if (hp->type != T_MACRO) {
8267 special_symbol (hp, op);
8268 return;
8271 /* This macro is being used inside a #if, which means it must be */
8272 /* recorded as a precondition. */
8273 if (pcp_inside_if && pcp_outfile && defn->predefined)
8274 dump_single_macro (hp, pcp_outfile);
8276 nargs = defn->nargs;
8278 if (nargs >= 0) {
8279 register int i;
8280 struct argdata *args;
8281 int parse_error = 0;
8283 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
8285 for (i = 0; i < nargs; i++) {
8286 args[i].raw = (U_CHAR *) "";
8287 args[i].expanded = 0;
8288 args[i].raw_length = args[i].expand_length = args[i].expand_size
8289 = args[i].stringified_length_bound = 0;
8290 args[i].free1 = args[i].free2 = 0;
8291 args[i].use_count = 0;
8294 /* Parse all the macro args that are supplied. I counts them.
8295 The first NARGS args are stored in ARGS.
8296 The rest are discarded.
8297 If rest_args is set then we assume macarg absorbed the rest of the args.
8299 i = 0;
8300 rest_args = 0;
8301 do {
8302 /* Discard the open-parenthesis or comma before the next arg. */
8303 ++instack[indepth].bufp;
8304 if (rest_args)
8305 continue;
8306 if (i < nargs || (nargs == 0 && i == 0)) {
8307 /* If we are working on last arg which absorbs rest of args... */
8308 if (i == nargs - 1 && defn->rest_args)
8309 rest_args = 1;
8310 parse_error = macarg (&args[i], rest_args);
8312 else
8313 parse_error = macarg (NULL_PTR, 0);
8314 if (parse_error) {
8315 error_with_line (line_for_error (start_line),
8316 "unterminated macro call");
8317 break;
8319 i++;
8320 } while (*instack[indepth].bufp != ')');
8322 /* If we got one arg but it was just whitespace, call that 0 args. */
8323 if (i == 1) {
8324 register U_CHAR *bp = args[0].raw;
8325 register U_CHAR *lim = bp + args[0].raw_length;
8326 /* cpp.texi says for foo ( ) we provide one argument.
8327 However, if foo wants just 0 arguments, treat this as 0. */
8328 if (nargs == 0)
8329 while (bp != lim && is_space[*bp]) bp++;
8330 if (bp == lim)
8331 i = 0;
8334 /* Don't output an error message if we have already output one for
8335 a parse error above. */
8336 rest_zero = 0;
8337 if (nargs == 0 && i > 0) {
8338 if (! parse_error)
8339 error ("arguments given to macro `%s'", hp->name);
8340 } else if (i < nargs) {
8341 /* traditional C allows foo() if foo wants one argument. */
8342 if (nargs == 1 && i == 0 && traditional)
8344 /* the rest args token is allowed to absorb 0 tokens */
8345 else if (i == nargs - 1 && defn->rest_args)
8346 rest_zero = 1;
8347 else if (parse_error)
8349 else if (i == 0)
8350 error ("macro `%s' used without args", hp->name);
8351 else if (i == 1)
8352 error ("macro `%s' used with just one arg", hp->name);
8353 else
8354 error ("macro `%s' used with only %d args", hp->name, i);
8355 } else if (i > nargs) {
8356 if (! parse_error)
8357 error ("macro `%s' used with too many (%d) args", hp->name, i);
8360 /* Swallow the closeparen. */
8361 ++instack[indepth].bufp;
8363 /* If macro wants zero args, we parsed the arglist for checking only.
8364 Read directly from the macro definition. */
8365 if (nargs == 0) {
8366 xbuf = defn->expansion;
8367 xbuf_len = defn->length;
8368 } else {
8369 register U_CHAR *exp = defn->expansion;
8370 register int offset; /* offset in expansion,
8371 copied a piece at a time */
8372 register int totlen; /* total amount of exp buffer filled so far */
8374 register struct reflist *ap, *last_ap;
8376 /* Macro really takes args. Compute the expansion of this call. */
8378 /* Compute length in characters of the macro's expansion.
8379 Also count number of times each arg is used. */
8380 xbuf_len = defn->length;
8381 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
8382 if (ap->stringify)
8383 xbuf_len += args[ap->argno].stringified_length_bound;
8384 else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
8385 /* Add 4 for two newline-space markers to prevent
8386 token concatenation. */
8387 xbuf_len += args[ap->argno].raw_length + 4;
8388 else {
8389 /* We have an ordinary (expanded) occurrence of the arg.
8390 So compute its expansion, if we have not already. */
8391 if (args[ap->argno].expanded == 0) {
8392 FILE_BUF obuf;
8393 obuf = expand_to_temp_buffer (args[ap->argno].raw,
8394 args[ap->argno].raw + args[ap->argno].raw_length,
8395 1, 0);
8397 args[ap->argno].expanded = obuf.buf;
8398 args[ap->argno].expand_length = obuf.bufp - obuf.buf;
8399 args[ap->argno].expand_size = obuf.length;
8400 args[ap->argno].free2 = obuf.buf;
8402 xbuf_len += args[ap->argno].expand_length;
8403 } else {
8404 /* If the arg appears more than once, its later occurrences
8405 may have newline turned into backslash-'n', which is a
8406 factor of 2 expansion. */
8407 xbuf_len += 2 * args[ap->argno].expand_length;
8409 /* Add 4 for two newline-space markers to prevent
8410 token concatenation. */
8411 xbuf_len += 4;
8413 if (args[ap->argno].use_count < 10)
8414 args[ap->argno].use_count++;
8417 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
8419 /* Generate in XBUF the complete expansion
8420 with arguments substituted in.
8421 TOTLEN is the total size generated so far.
8422 OFFSET is the index in the definition
8423 of where we are copying from. */
8424 offset = totlen = 0;
8425 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
8426 last_ap = ap, ap = ap->next) {
8427 register struct argdata *arg = &args[ap->argno];
8428 int count_before = totlen;
8430 /* Add chars to XBUF. */
8431 for (i = 0; i < ap->nchars; i++, offset++)
8432 xbuf[totlen++] = exp[offset];
8434 /* If followed by an empty rest arg with concatenation,
8435 delete the last run of nonwhite chars. */
8436 if (rest_zero && totlen > count_before
8437 && ((ap->rest_args && ap->raw_before != 0)
8438 || (last_ap != NULL && last_ap->rest_args
8439 && last_ap->raw_after != 0))) {
8440 /* Delete final whitespace. */
8441 while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
8442 totlen--;
8445 /* Delete the nonwhites before them. */
8446 while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
8447 totlen--;
8451 if (ap->stringify != 0) {
8452 int arglen = arg->raw_length;
8453 int escaped = 0;
8454 int in_string = 0;
8455 int c;
8456 i = 0;
8457 while (i < arglen
8458 && (c = arg->raw[i], is_space[c]))
8459 i++;
8460 while (i < arglen
8461 && (c = arg->raw[arglen - 1], is_space[c]))
8462 arglen--;
8463 if (!traditional)
8464 xbuf[totlen++] = '\"'; /* insert beginning quote */
8465 for (; i < arglen; i++) {
8466 c = arg->raw[i];
8468 if (in_string) {
8469 /* Generate nothing for backslash-newline in a string. */
8470 if (c == '\\' && arg->raw[i + 1] == '\n') {
8471 i++;
8472 continue;
8474 } else {
8475 /* Special markers
8476 generate nothing for a stringified argument. */
8477 if (c == '\n') {
8478 i++;
8479 continue;
8482 /* Internal sequences of whitespace are replaced by one space
8483 except within a string or char token. */
8484 if (is_space[c]) {
8485 i++;
8486 while (is_space[(c = arg->raw[i])])
8487 /* Newline markers can occur within a whitespace sequence;
8488 consider them part of the sequence. */
8489 i += (c == '\n') + 1;
8490 i--;
8491 c = ' ';
8495 if (escaped)
8496 escaped = 0;
8497 else {
8498 if (c == '\\')
8499 escaped = 1;
8500 else if (in_string) {
8501 if (c == in_string)
8502 in_string = 0;
8503 else
8505 #ifdef MULTIBYTE_CHARS
8506 int length;
8507 length = local_mblen (arg->raw + i, arglen - i);
8508 if (length > 1)
8510 bcopy (arg->raw + i, xbuf + totlen, length);
8511 i += length - 1;
8512 totlen += length;
8513 continue;
8515 #endif
8517 } else if (c == '\"' || c == '\'')
8518 in_string = c;
8521 /* Escape double-quote, and backslashes in strings.
8522 Newlines in strings are best escaped as \n, since
8523 otherwise backslash-backslash-newline-newline is
8524 mishandled. The C Standard doesn't allow newlines in
8525 strings, so we can escape newlines as we please. */
8526 if (c == '\"'
8527 || (in_string
8528 && (c == '\\'
8529 || (c == '\n' ? (c = 'n', 1) : 0))))
8530 xbuf[totlen++] = '\\';
8531 /* We used to output e.g. \008 for control characters here,
8532 but this doesn't conform to the C Standard.
8533 Just output the characters as-is. */
8534 xbuf[totlen++] = c;
8536 if (!traditional)
8537 xbuf[totlen++] = '\"'; /* insert ending quote */
8538 } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
8539 U_CHAR *p1 = arg->raw;
8540 U_CHAR *l1 = p1 + arg->raw_length;
8541 if (ap->raw_before != 0) {
8542 while (p1 != l1 && is_space[*p1]) p1++;
8543 while (p1 != l1 && is_idchar[*p1])
8544 xbuf[totlen++] = *p1++;
8545 /* Delete any no-reexpansion marker that follows
8546 an identifier at the beginning of the argument
8547 if the argument is concatenated with what precedes it. */
8548 if (p1[0] == '\n' && p1[1] == '-')
8549 p1 += 2;
8550 } else if (!traditional) {
8551 /* Ordinary expanded use of the argument.
8552 Put in newline-space markers to prevent token pasting. */
8553 xbuf[totlen++] = '\n';
8554 xbuf[totlen++] = ' ';
8556 if (ap->raw_after != 0) {
8557 /* Arg is concatenated after: delete trailing whitespace,
8558 whitespace markers, and no-reexpansion markers. */
8559 while (p1 != l1) {
8560 if (is_space[l1[-1]]) l1--;
8561 else if (l1[-1] == '-') {
8562 U_CHAR *p2 = l1 - 1;
8563 /* If a `-' is preceded by an odd number of newlines then it
8564 and the last newline are a no-reexpansion marker. */
8565 while (p2 != p1 && p2[-1] == '\n') p2--;
8566 if ((l1 - 1 - p2) & 1) {
8567 l1 -= 2;
8569 else break;
8571 else break;
8575 bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
8576 totlen += l1 - p1;
8577 if (!traditional && ap->raw_after == 0) {
8578 /* Ordinary expanded use of the argument.
8579 Put in newline-space markers to prevent token pasting. */
8580 xbuf[totlen++] = '\n';
8581 xbuf[totlen++] = ' ';
8583 } else {
8584 /* Ordinary expanded use of the argument.
8585 Put in newline-space markers to prevent token pasting. */
8586 if (!traditional) {
8587 xbuf[totlen++] = '\n';
8588 xbuf[totlen++] = ' ';
8590 bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
8591 arg->expand_length);
8592 totlen += arg->expand_length;
8593 if (!traditional) {
8594 xbuf[totlen++] = '\n';
8595 xbuf[totlen++] = ' ';
8597 /* If a macro argument with newlines is used multiple times,
8598 then only expand the newlines once. This avoids creating output
8599 lines which don't correspond to any input line, which confuses
8600 gdb and gcov. */
8601 if (arg->use_count > 1 && arg->newlines > 0) {
8602 /* Don't bother doing change_newlines for subsequent
8603 uses of arg. */
8604 arg->use_count = 1;
8605 change_newlines (arg);
8609 if (totlen > xbuf_len)
8610 abort ();
8613 /* If there is anything left of the definition after handling
8614 the arg list, copy that in too. */
8616 for (i = offset; i < defn->length; i++) {
8617 /* if we've reached the end of the macro */
8618 if (exp[i] == ')')
8619 rest_zero = 0;
8620 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
8621 && last_ap->raw_after != 0))
8622 xbuf[totlen++] = exp[i];
8625 xbuf[totlen] = 0;
8626 xbuf_len = totlen;
8628 for (i = 0; i < nargs; i++) {
8629 if (args[i].free1 != 0)
8630 free (args[i].free1);
8631 if (args[i].free2 != 0)
8632 free (args[i].free2);
8635 } else {
8636 xbuf = defn->expansion;
8637 xbuf_len = defn->length;
8640 /* Now put the expansion on the input stack
8641 so our caller will commence reading from it. */
8643 register FILE_BUF *ip2;
8645 ip2 = &instack[++indepth];
8647 ip2->fname = 0;
8648 ip2->nominal_fname = 0;
8649 ip2->nominal_fname_len = 0;
8650 ip2->inc = 0;
8651 /* This may not be exactly correct, but will give much better error
8652 messages for nested macro calls than using a line number of zero. */
8653 ip2->lineno = start_line;
8654 ip2->buf = xbuf;
8655 ip2->length = xbuf_len;
8656 ip2->bufp = xbuf;
8657 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
8658 ip2->macro = hp;
8659 ip2->if_stack = if_stack;
8660 ip2->system_header_p = 0;
8662 /* Recursive macro use sometimes works traditionally.
8663 #define foo(x,y) bar (x (y,0), y)
8664 foo (foo, baz) */
8666 if (!traditional)
8667 hp->type = T_DISABLED;
8671 /* Parse a macro argument and store the info on it into *ARGPTR.
8672 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8673 Return nonzero to indicate a syntax error. */
8675 static int
8676 macarg (argptr, rest_args)
8677 register struct argdata *argptr;
8678 int rest_args;
8680 FILE_BUF *ip = &instack[indepth];
8681 int paren = 0;
8682 int lineno0 = ip->lineno;
8683 int comments = 0;
8684 int result = 0;
8686 /* Try to parse as much of the argument as exists at this
8687 input stack level. */
8688 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro,
8689 &paren, &ip->lineno, &comments, rest_args);
8691 /* If we find the end of the argument at this level,
8692 set up *ARGPTR to point at it in the input stack. */
8693 if (!(ip->fname != 0 && (ip->lineno != lineno0 || comments != 0))
8694 && bp != ip->buf + ip->length) {
8695 if (argptr != 0) {
8696 argptr->raw = ip->bufp;
8697 argptr->raw_length = bp - ip->bufp;
8698 argptr->newlines = ip->lineno - lineno0;
8700 ip->bufp = bp;
8701 } else {
8702 /* This input stack level ends before the macro argument does.
8703 We must pop levels and keep parsing.
8704 Therefore, we must allocate a temporary buffer and copy
8705 the macro argument into it. */
8706 int bufsize = bp - ip->bufp;
8707 int extra = ip->lineno - lineno0;
8708 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
8709 int final_start = 0;
8711 bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
8712 ip->bufp = bp;
8714 while (bp == ip->buf + ip->length) {
8715 if (instack[indepth].macro == 0) {
8716 result = 1;
8717 break;
8719 ip->macro->type = T_MACRO;
8720 if (ip->free_ptr)
8721 free (ip->free_ptr);
8722 ip = &instack[--indepth];
8723 lineno0 = ip->lineno;
8724 comments = 0;
8725 bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro, &paren,
8726 &ip->lineno, &comments, rest_args);
8727 final_start = bufsize;
8728 bufsize += bp - ip->bufp;
8729 extra += ip->lineno - lineno0;
8730 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
8731 bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
8732 bp - ip->bufp);
8733 ip->bufp = bp;
8736 /* Now, if arg is actually wanted, record its raw form,
8737 discarding comments and duplicating newlines in whatever
8738 part of it did not come from a macro expansion.
8739 EXTRA space has been preallocated for duplicating the newlines.
8740 FINAL_START is the index of the start of that part. */
8741 if (argptr != 0) {
8742 argptr->raw = buffer;
8743 argptr->raw_length = bufsize;
8744 argptr->free1 = buffer;
8745 argptr->newlines = ip->lineno - lineno0;
8746 if ((argptr->newlines || comments) && ip->fname != 0)
8747 argptr->raw_length
8748 = final_start +
8749 discard_comments (argptr->raw + final_start,
8750 argptr->raw_length - final_start,
8751 argptr->newlines);
8752 argptr->raw[argptr->raw_length] = 0;
8753 if (argptr->raw_length > bufsize + extra)
8754 abort ();
8758 /* If we are not discarding this argument,
8759 macroexpand it and compute its length as stringified.
8760 All this info goes into *ARGPTR. */
8762 if (argptr != 0) {
8763 register U_CHAR *buf, *lim;
8764 register int totlen;
8766 buf = argptr->raw;
8767 lim = buf + argptr->raw_length;
8769 while (buf != lim && is_space[*buf])
8770 buf++;
8771 while (buf != lim && is_space[lim[-1]])
8772 lim--;
8773 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
8774 while (buf != lim) {
8775 register U_CHAR c = *buf++;
8776 totlen++;
8777 /* Internal sequences of whitespace are replaced by one space
8778 in most cases, but not always. So count all the whitespace
8779 in case we need to keep it all. */
8780 #if 0
8781 if (is_space[c])
8782 SKIP_ALL_WHITE_SPACE (buf);
8783 else
8784 #endif
8785 if (c == '\"' || c == '\\' || c == '\n') /* escape these chars */
8786 totlen++;
8788 argptr->stringified_length_bound = totlen;
8790 return result;
8793 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8794 taken from the expansion of MACRO,
8795 counting parens in *DEPTHPTR,
8796 and return if reach LIMIT
8797 or before a `)' that would make *DEPTHPTR negative
8798 or before a comma when *DEPTHPTR is zero.
8799 Single and double quotes are matched and termination
8800 is inhibited within them. Comments also inhibit it.
8801 Value returned is pointer to stopping place.
8803 Increment *NEWLINES each time a newline is passed.
8804 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8805 Set *COMMENTS to 1 if a comment is seen. */
8807 static U_CHAR *
8808 macarg1 (start, limit, macro, depthptr, newlines, comments, rest_args)
8809 U_CHAR *start;
8810 register U_CHAR *limit;
8811 struct hashnode *macro;
8812 int *depthptr, *newlines, *comments;
8813 int rest_args;
8815 register U_CHAR *bp = start;
8817 while (bp < limit) {
8818 switch (*bp) {
8819 case '(':
8820 (*depthptr)++;
8821 break;
8822 case ')':
8823 if (--(*depthptr) < 0)
8824 return bp;
8825 break;
8826 case '\\':
8827 /* Traditionally, backslash makes following char not special. */
8828 if (traditional && bp + 1 < limit && bp[1] != '\n')
8829 bp++;
8830 break;
8831 case '\n':
8832 ++*newlines;
8833 break;
8834 case '/':
8835 if (macro)
8836 break;
8837 if (bp[1] == '\\')
8838 newline_fix (bp + 1);
8839 if (bp[1] == '*') {
8840 *comments = 1;
8841 for (bp += 2; bp < limit; bp++) {
8842 if (*bp == '\n')
8843 ++*newlines;
8844 else if (*bp == '*') {
8845 if (bp[-1] == '/' && warn_comments)
8846 warning ("`/*' within comment");
8847 if (bp[1] == '\\')
8848 newline_fix (bp + 1);
8849 if (bp[1] == '/') {
8850 bp++;
8851 break;
8854 else
8856 #ifdef MULTIBYTE_CHARS
8857 int length;
8858 length = local_mblen (bp, limit - bp);
8859 if (length > 1)
8860 bp += (length - 1);
8861 #endif
8864 } else if (bp[1] == '/' && cplusplus_comments) {
8865 *comments = 1;
8866 for (bp += 2; bp < limit; bp++) {
8867 if (*bp == '\n') {
8868 ++*newlines;
8869 if (bp[-1] != '\\')
8870 break;
8871 if (warn_comments)
8872 warning ("multiline `//' comment");
8874 else
8876 #ifdef MULTIBYTE_CHARS
8877 int length;
8878 length = local_mblen (bp, limit - bp);
8879 if (length > 1)
8880 bp += (length - 1);
8881 #endif
8885 break;
8886 case '\'':
8887 case '\"':
8889 int quotec;
8890 for (quotec = *bp++; bp < limit && *bp != quotec; bp++) {
8891 if (*bp == '\\') {
8892 bp++;
8893 if (*bp == '\n')
8894 ++*newlines;
8895 while (*bp == '\\' && bp[1] == '\n') {
8896 bp += 2;
8897 ++*newlines;
8899 } else if (*bp == '\n') {
8900 if (warn_white_space && is_hor_space[bp[-1]] && ! macro)
8901 warning ("white space at end of line in string");
8902 ++*newlines;
8903 if (quotec == '\'')
8904 break;
8906 else
8908 #ifdef MULTIBYTE_CHARS
8909 int length;
8910 length = local_mblen (bp, limit - bp);
8911 if (length > 1)
8912 bp += (length - 1);
8913 #endif
8917 break;
8918 case ',':
8919 /* if we've returned to lowest level and we aren't absorbing all args */
8920 if ((*depthptr) == 0 && rest_args == 0)
8921 return bp;
8922 break;
8924 bp++;
8927 return bp;
8930 /* Discard comments and duplicate newlines
8931 in the string of length LENGTH at START,
8932 except inside of string constants.
8933 The string is copied into itself with its beginning staying fixed.
8935 NEWLINES is the number of newlines that must be duplicated.
8936 We assume that that much extra space is available past the end
8937 of the string. */
8939 static int
8940 discard_comments (start, length, newlines)
8941 U_CHAR *start;
8942 int length;
8943 int newlines;
8945 register U_CHAR *ibp;
8946 register U_CHAR *obp;
8947 register U_CHAR *limit;
8948 register int c;
8950 /* If we have newlines to duplicate, copy everything
8951 that many characters up. Then, in the second part,
8952 we will have room to insert the newlines
8953 while copying down.
8954 NEWLINES may actually be too large, because it counts
8955 newlines in string constants, and we don't duplicate those.
8956 But that does no harm. */
8957 if (newlines > 0) {
8958 ibp = start + length;
8959 obp = ibp + newlines;
8960 limit = start;
8961 while (limit != ibp)
8962 *--obp = *--ibp;
8965 ibp = start + newlines;
8966 limit = start + length + newlines;
8967 obp = start;
8969 while (ibp < limit) {
8970 *obp++ = c = *ibp++;
8971 switch (c) {
8972 case '\n':
8973 /* Duplicate the newline. */
8974 *obp++ = '\n';
8975 break;
8977 case '\\':
8978 if (*ibp == '\n') {
8979 obp--;
8980 ibp++;
8982 break;
8984 case '/':
8985 if (*ibp == '\\')
8986 newline_fix (ibp);
8987 /* Delete any comment. */
8988 if (cplusplus_comments && ibp[0] == '/') {
8989 /* Comments are equivalent to spaces. */
8990 obp[-1] = ' ';
8991 ibp++;
8992 while (ibp < limit)
8994 if (*ibp == '\n')
8996 if (ibp[-1] != '\\')
8997 break;
8999 else
9001 #ifdef MULTIBYTE_CHARS
9002 int length = local_mblen (ibp, limit - ibp);
9003 if (length > 1)
9004 ibp += (length - 1);
9005 #endif
9007 ibp++;
9009 break;
9011 if (ibp[0] != '*' || ibp + 1 >= limit)
9012 break;
9013 /* Comments are equivalent to spaces.
9014 For -traditional, a comment is equivalent to nothing. */
9015 if (traditional)
9016 obp--;
9017 else
9018 obp[-1] = ' ';
9019 while (++ibp < limit) {
9020 if (ibp[0] == '*') {
9021 if (ibp[1] == '\\')
9022 newline_fix (ibp + 1);
9023 if (ibp[1] == '/') {
9024 ibp += 2;
9025 break;
9028 else
9030 #ifdef MULTIBYTE_CHARS
9031 int length = local_mblen (ibp, limit - ibp);
9032 if (length > 1)
9033 ibp += (length - 1);
9034 #endif
9037 break;
9039 case '\'':
9040 case '\"':
9041 /* Notice and skip strings, so that we don't
9042 think that comments start inside them,
9043 and so we don't duplicate newlines in them. */
9045 int quotec = c;
9046 while (ibp < limit) {
9047 *obp++ = c = *ibp++;
9048 if (c == quotec)
9049 break;
9050 if (c == '\n')
9052 if (quotec == '\'')
9053 break;
9055 else if (c == '\\') {
9056 if (ibp < limit && *ibp == '\n') {
9057 ibp++;
9058 obp--;
9059 } else {
9060 while (*ibp == '\\' && ibp[1] == '\n')
9061 ibp += 2;
9062 if (ibp < limit)
9063 *obp++ = *ibp++;
9066 else
9068 #ifdef MULTIBYTE_CHARS
9069 int length;
9070 ibp--;
9071 length = local_mblen (ibp, limit - ibp);
9072 if (length > 1)
9074 obp--;
9075 bcopy (ibp, obp, length);
9076 ibp += length;
9077 obp += length;
9079 else
9080 ibp++;
9081 #endif
9085 break;
9089 return obp - start;
9092 /* Turn newlines to spaces in the macro argument ARG.
9093 Remove backslash-newline from string constants,
9094 and turn other newlines in string constants to backslash-'n'. */
9096 static void
9097 change_newlines (arg)
9098 struct argdata *arg;
9100 U_CHAR *start = arg->expanded;
9101 int length = arg->expand_length;
9102 register U_CHAR *ibp;
9103 register U_CHAR *obp;
9104 register U_CHAR *limit;
9106 ibp = start;
9107 limit = start + length;
9108 obp = start;
9110 while (ibp < limit) {
9111 switch ((*obp++ = *ibp++)) {
9112 case '\n':
9113 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9114 string. Skip past the newline and its duplicate.
9115 Put a space in the output. */
9116 if (*ibp == '\n')
9118 ibp++;
9119 obp--;
9120 *obp++ = ' ';
9122 break;
9124 case '\'':
9125 case '\"':
9126 /* Notice and skip strings, to handle their newlines properly. */
9128 U_CHAR *ibp1 = skip_quoted_string (ibp - 1, limit, 0,
9129 NULL_PTR, NULL_PTR, NULL_PTR);
9130 while (ibp != ibp1) {
9131 switch ((*obp++ = *ibp++)) {
9132 case '\\':
9133 /* Replace backslash-newline with nothing. */
9134 if (*ibp == '\n') {
9135 ++ibp;
9136 --obp;
9138 break;
9140 case '\n':
9141 /* Replace non-backslashed newline with backslash-'n',
9142 replacing the arg buffer with a new one if this hasn't
9143 been done already. */
9145 if (start == arg->expanded) {
9146 int olength = obp - arg->expanded;
9147 U_CHAR *newbuf;
9148 arg->expand_size = 2 * arg->expand_length;
9149 newbuf = (U_CHAR *) xmalloc (arg->expand_size);
9150 arg->free2 = arg->expanded = newbuf;
9151 obp = newbuf + olength;
9152 bcopy ((char *) start, (char *) newbuf, olength);
9155 obp[-1] = '\\';
9156 *obp++ = 'n';
9157 break;
9159 #ifdef MULTIBYTE_CHARS
9160 default:
9162 int length;
9163 ibp--;
9164 length = local_mblen (ibp, ibp1 - ibp);
9165 if (length > 1)
9167 obp--;
9168 bcopy (ibp, obp, length);
9169 ibp += length;
9170 obp += length;
9172 else
9173 ibp++;
9175 #endif
9179 break;
9183 arg->expand_length = obp - arg->expanded;
9185 if (start != arg->expanded)
9186 free (start);
9189 /* my_strerror - return the descriptive text associated with an
9190 `errno' code. */
9192 char *
9193 my_strerror (errnum)
9194 int errnum;
9196 char *result;
9198 #ifndef VMS
9199 #ifndef HAVE_STRERROR
9200 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
9201 #else
9202 result = strerror (errnum);
9203 #endif
9204 #else /* VMS */
9205 /* VAXCRTL's strerror() takes an optional second argument, which only
9206 matters when the first argument is EVMSERR. However, it's simplest
9207 just to pass it unconditionally. `vaxc$errno' is declared in
9208 <errno.h>, and maintained by the library in parallel with `errno'.
9209 We assume that caller's `errnum' either matches the last setting of
9210 `errno' by the library or else does not have the value `EVMSERR'. */
9212 result = strerror (errnum, vaxc$errno);
9213 #endif
9215 if (!result)
9216 result = "errno = ?";
9218 return result;
9221 /* notice - output message to stderr */
9223 static void
9224 notice (PRINTF_ALIST (msgid))
9225 PRINTF_DCL (msgid)
9227 va_list args;
9229 VA_START (args, msgid);
9230 vnotice (msgid, args);
9231 va_end (args);
9234 static void
9235 vnotice (msgid, args)
9236 char *msgid;
9237 va_list args;
9239 vfprintf (stderr, _(msgid), args);
9242 /* error - print error message and increment count of errors. */
9244 void
9245 error (PRINTF_ALIST (msgid))
9246 PRINTF_DCL (msgid)
9248 va_list args;
9250 VA_START (args, msgid);
9251 verror (msgid, args);
9252 va_end (args);
9255 void
9256 verror (msgid, args)
9257 char *msgid;
9258 va_list args;
9260 int i;
9261 FILE_BUF *ip = NULL;
9263 print_containing_files ();
9265 for (i = indepth; i >= 0; i--)
9266 if (instack[i].fname != NULL) {
9267 ip = &instack[i];
9268 break;
9271 if (ip != NULL) {
9272 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9273 ip->nominal_fname_len, stderr);
9274 fprintf (stderr, ":%d: ", ip->lineno);
9276 vnotice (msgid, args);
9277 fprintf (stderr, "\n");
9278 errors++;
9281 /* Error including a message from `errno'. */
9283 static void
9284 error_from_errno (name)
9285 char *name;
9287 int e = errno;
9288 int i;
9289 FILE_BUF *ip = NULL;
9291 print_containing_files ();
9293 for (i = indepth; i >= 0; i--)
9294 if (instack[i].fname != NULL) {
9295 ip = &instack[i];
9296 break;
9299 if (ip != NULL) {
9300 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9301 ip->nominal_fname_len, stderr);
9302 fprintf (stderr, ":%d: ", ip->lineno);
9305 fprintf (stderr, "%s: %s\n", name, my_strerror (e));
9307 errors++;
9310 /* Print error message but don't count it. */
9312 void
9313 warning (PRINTF_ALIST (msgid))
9314 PRINTF_DCL (msgid)
9316 va_list args;
9318 VA_START (args, msgid);
9319 vwarning (msgid, args);
9320 va_end (args);
9323 static void
9324 vwarning (msgid, args)
9325 char *msgid;
9326 va_list args;
9328 int i;
9329 FILE_BUF *ip = NULL;
9331 if (inhibit_warnings)
9332 return;
9334 if (warnings_are_errors)
9335 errors++;
9337 print_containing_files ();
9339 for (i = indepth; i >= 0; i--)
9340 if (instack[i].fname != NULL) {
9341 ip = &instack[i];
9342 break;
9345 if (ip != NULL) {
9346 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9347 ip->nominal_fname_len, stderr);
9348 fprintf (stderr, ":%d: ", ip->lineno);
9350 notice ("warning: ");
9351 vnotice (msgid, args);
9352 fprintf (stderr, "\n");
9355 static void
9356 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
9357 error_with_line (int line, PRINTF_ALIST (msgid))
9358 #else
9359 error_with_line (line, PRINTF_ALIST (msgid))
9360 int line;
9361 PRINTF_DCL (msgid)
9362 #endif
9364 va_list args;
9366 VA_START (args, msgid);
9367 verror_with_line (line, msgid, args);
9368 va_end (args);
9371 static void
9372 verror_with_line (line, msgid, args)
9373 int line;
9374 char *msgid;
9375 va_list args;
9377 int i;
9378 FILE_BUF *ip = NULL;
9380 print_containing_files ();
9382 for (i = indepth; i >= 0; i--)
9383 if (instack[i].fname != NULL) {
9384 ip = &instack[i];
9385 break;
9388 if (ip != NULL) {
9389 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9390 ip->nominal_fname_len, stderr);
9391 fprintf (stderr, ":%d: ", line);
9393 vnotice (msgid, args);
9394 fprintf (stderr, "\n");
9395 errors++;
9398 static void
9399 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
9400 warning_with_line (int line, PRINTF_ALIST (msgid))
9401 #else
9402 warning_with_line (line, PRINTF_ALIST (msgid))
9403 int line;
9404 PRINTF_DCL (msgid)
9405 #endif
9407 va_list args;
9409 VA_START (args, msgid);
9410 vwarning_with_line (line, msgid, args);
9411 va_end (args);
9414 static void
9415 vwarning_with_line (line, msgid, args)
9416 int line;
9417 char *msgid;
9418 va_list args;
9420 int i;
9421 FILE_BUF *ip = NULL;
9423 if (inhibit_warnings)
9424 return;
9426 if (warnings_are_errors)
9427 errors++;
9429 print_containing_files ();
9431 for (i = indepth; i >= 0; i--)
9432 if (instack[i].fname != NULL) {
9433 ip = &instack[i];
9434 break;
9437 if (ip != NULL) {
9438 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9439 ip->nominal_fname_len, stderr);
9440 fprintf (stderr, line ? ":%d: " : ": ", line);
9442 notice ("warning: ");
9443 vnotice (msgid, args);
9444 fprintf (stderr, "\n");
9447 /* Print an error message and maybe count it. */
9449 void
9450 pedwarn (PRINTF_ALIST (msgid))
9451 PRINTF_DCL (msgid)
9453 va_list args;
9455 VA_START (args, msgid);
9456 if (pedantic_errors)
9457 verror (msgid, args);
9458 else
9459 vwarning (msgid, args);
9460 va_end (args);
9463 void
9464 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
9465 pedwarn_with_line (int line, PRINTF_ALIST (msgid))
9466 #else
9467 pedwarn_with_line (line, PRINTF_ALIST (msgid))
9468 int line;
9469 PRINTF_DCL (msgid)
9470 #endif
9472 va_list args;
9474 VA_START (args, msgid);
9475 if (pedantic_errors)
9476 verror_with_line (line, msgid, args);
9477 else
9478 vwarning_with_line (line, msgid, args);
9479 va_end (args);
9482 /* Report a warning (or an error if pedantic_errors)
9483 giving specified file name and line number, not current. */
9485 static void
9486 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
9487 pedwarn_with_file_and_line (char *file, size_t file_len, int line,
9488 PRINTF_ALIST (msgid))
9489 #else
9490 pedwarn_with_file_and_line (file, file_len, line, PRINTF_ALIST (msgid))
9491 char *file;
9492 size_t file_len;
9493 int line;
9494 PRINTF_DCL (msgid)
9495 #endif
9497 va_list args;
9499 if (!pedantic_errors && inhibit_warnings)
9500 return;
9501 if (file) {
9502 fwrite (file, sizeof file[0], file_len, stderr);
9503 fprintf (stderr, ":%d: ", line);
9505 if (pedantic_errors)
9506 errors++;
9507 if (!pedantic_errors)
9508 notice ("warning: ");
9509 VA_START (args, msgid);
9510 vnotice (msgid, args);
9511 va_end (args);
9512 fprintf (stderr, "\n");
9515 static void
9516 pedwarn_strange_white_space (ch)
9517 int ch;
9519 switch (ch)
9521 case '\f': pedwarn ("formfeed in preprocessing directive"); break;
9522 case '\r': pedwarn ("carriage return in preprocessing directive"); break;
9523 case '\v': pedwarn ("vertical tab in preprocessing directive"); break;
9524 default: abort ();
9528 /* Print the file names and line numbers of the #include
9529 directives which led to the current file. */
9531 static void
9532 print_containing_files ()
9534 FILE_BUF *ip = NULL;
9535 int i;
9536 int first = 1;
9538 /* If stack of files hasn't changed since we last printed
9539 this info, don't repeat it. */
9540 if (last_error_tick == input_file_stack_tick)
9541 return;
9543 for (i = indepth; i >= 0; i--)
9544 if (instack[i].fname != NULL) {
9545 ip = &instack[i];
9546 break;
9549 /* Give up if we don't find a source file. */
9550 if (ip == NULL)
9551 return;
9553 /* Find the other, outer source files. */
9554 for (i--; i >= 0; i--)
9555 if (instack[i].fname != NULL) {
9556 ip = &instack[i];
9557 if (first) {
9558 first = 0;
9559 notice ( "In file included from ");
9560 } else {
9561 notice (",\n from ");
9564 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9565 ip->nominal_fname_len, stderr);
9566 fprintf (stderr, ":%d", ip->lineno);
9568 if (! first)
9569 fprintf (stderr, ":\n");
9571 /* Record we have printed the status as of this time. */
9572 last_error_tick = input_file_stack_tick;
9575 /* Return the line at which an error occurred.
9576 The error is not necessarily associated with the current spot
9577 in the input stack, so LINE says where. LINE will have been
9578 copied from ip->lineno for the current input level.
9579 If the current level is for a file, we return LINE.
9580 But if the current level is not for a file, LINE is meaningless.
9581 In that case, we return the lineno of the innermost file. */
9583 static int
9584 line_for_error (line)
9585 int line;
9587 int i;
9588 int line1 = line;
9590 for (i = indepth; i >= 0; ) {
9591 if (instack[i].fname != 0)
9592 return line1;
9593 i--;
9594 if (i < 0)
9595 return 0;
9596 line1 = instack[i].lineno;
9598 abort ();
9599 /*NOTREACHED*/
9600 return 0;
9604 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9606 * As things stand, nothing is ever placed in the output buffer to be
9607 * removed again except when it's KNOWN to be part of an identifier,
9608 * so flushing and moving down everything left, instead of expanding,
9609 * should work ok.
9612 /* You might think void was cleaner for the return type,
9613 but that would get type mismatch in check_expand in strict ANSI. */
9615 static int
9616 grow_outbuf (obuf, needed)
9617 register FILE_BUF *obuf;
9618 register int needed;
9620 register U_CHAR *p;
9621 int minsize;
9623 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
9624 return 0;
9626 /* Make it at least twice as big as it is now. */
9627 obuf->length *= 2;
9628 /* Make it have at least 150% of the free space we will need. */
9629 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
9630 if (minsize > obuf->length)
9631 obuf->length = minsize;
9633 if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
9634 memory_full ();
9636 obuf->bufp = p + (obuf->bufp - obuf->buf);
9637 obuf->buf = p;
9639 return 0;
9642 /* Symbol table for macro names and special symbols */
9645 * install a name in the main hash table, even if it is already there.
9646 * name stops with first non alphanumeric, except leading '#'.
9647 * caller must check against redefinition if that is desired.
9648 * delete_macro () removes things installed by install () in fifo order.
9649 * this is important because of the `defined' special symbol used
9650 * in #if, and also if pushdef/popdef directives are ever implemented.
9652 * If LEN is >= 0, it is the length of the name.
9653 * Otherwise, compute the length by scanning the entire name.
9655 * If HASH is >= 0, it is the precomputed hash code.
9656 * Otherwise, compute the hash code.
9659 static HASHNODE *
9660 install (name, len, type, value, hash)
9661 U_CHAR *name;
9662 int len;
9663 enum node_type type;
9664 char *value;
9665 int hash;
9667 register HASHNODE *hp;
9668 register int i, bucket;
9669 register U_CHAR *p, *q;
9671 if (len < 0) {
9672 p = name;
9673 while (is_idchar[*p])
9674 p++;
9675 len = p - name;
9678 if (hash < 0)
9679 hash = hashf (name, len, HASHSIZE);
9681 i = sizeof (HASHNODE) + len + 1;
9682 hp = (HASHNODE *) xmalloc (i);
9683 bucket = hash;
9684 hp->bucket_hdr = &hashtab[bucket];
9685 hp->next = hashtab[bucket];
9686 hashtab[bucket] = hp;
9687 hp->prev = NULL;
9688 if (hp->next != NULL)
9689 hp->next->prev = hp;
9690 hp->type = type;
9691 hp->length = len;
9692 hp->value.cpval = value;
9693 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
9694 p = hp->name;
9695 q = name;
9696 for (i = 0; i < len; i++)
9697 *p++ = *q++;
9698 hp->name[len] = 0;
9699 return hp;
9703 * find the most recent hash node for name name (ending with first
9704 * non-identifier char) installed by install
9706 * If LEN is >= 0, it is the length of the name.
9707 * Otherwise, compute the length by scanning the entire name.
9709 * If HASH is >= 0, it is the precomputed hash code.
9710 * Otherwise, compute the hash code.
9713 HASHNODE *
9714 lookup (name, len, hash)
9715 U_CHAR *name;
9716 int len;
9717 int hash;
9719 register U_CHAR *bp;
9720 register HASHNODE *bucket;
9722 if (len < 0) {
9723 for (bp = name; is_idchar[*bp]; bp++) ;
9724 len = bp - name;
9727 if (hash < 0)
9728 hash = hashf (name, len, HASHSIZE);
9730 bucket = hashtab[hash];
9731 while (bucket) {
9732 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
9733 return bucket;
9734 bucket = bucket->next;
9736 return NULL;
9740 * Delete a hash node. Some weirdness to free junk from macros.
9741 * More such weirdness will have to be added if you define more hash
9742 * types that need it.
9745 /* Note that the DEFINITION of a macro is removed from the hash table
9746 but its storage is not freed. This would be a storage leak
9747 except that it is not reasonable to keep undefining and redefining
9748 large numbers of macros many times.
9749 In any case, this is necessary, because a macro can be #undef'd
9750 in the middle of reading the arguments to a call to it.
9751 If #undef freed the DEFINITION, that would crash. */
9753 static void
9754 delete_macro (hp)
9755 HASHNODE *hp;
9758 if (hp->prev != NULL)
9759 hp->prev->next = hp->next;
9760 if (hp->next != NULL)
9761 hp->next->prev = hp->prev;
9763 /* Make sure that the bucket chain header that the deleted guy was
9764 on points to the right thing afterwards. */
9765 if (hp == *hp->bucket_hdr)
9766 *hp->bucket_hdr = hp->next;
9768 #if 0
9769 if (hp->type == T_MACRO) {
9770 DEFINITION *d = hp->value.defn;
9771 struct reflist *ap, *nextap;
9773 for (ap = d->pattern; ap != NULL; ap = nextap) {
9774 nextap = ap->next;
9775 free (ap);
9777 free (d);
9779 #endif
9780 free (hp);
9784 * return hash function on name. must be compatible with the one
9785 * computed a step at a time, elsewhere
9788 static int
9789 hashf (name, len, hashsize)
9790 register U_CHAR *name;
9791 register int len;
9792 int hashsize;
9794 register int r = 0;
9796 while (len--)
9797 r = HASHSTEP (r, *name++);
9799 return MAKE_POS (r) % hashsize;
9803 /* Dump the definition of a single macro HP to OF. */
9805 static void
9806 dump_single_macro (hp, of)
9807 register HASHNODE *hp;
9808 FILE *of;
9810 register DEFINITION *defn = hp->value.defn;
9811 struct reflist *ap;
9812 int offset;
9813 int concat;
9816 /* Print the definition of the macro HP. */
9818 fprintf (of, "#define %s", hp->name);
9820 if (defn->nargs >= 0) {
9821 int i;
9823 fprintf (of, "(");
9824 for (i = 0; i < defn->nargs; i++) {
9825 dump_arg_n (defn, i, of);
9826 if (i + 1 < defn->nargs)
9827 fprintf (of, ", ");
9829 fprintf (of, ")");
9832 fprintf (of, " ");
9834 offset = 0;
9835 concat = 0;
9836 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
9837 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
9838 offset += ap->nchars;
9839 if (!traditional) {
9840 if (ap->nchars != 0)
9841 concat = 0;
9842 if (ap->stringify) {
9843 switch (ap->stringify) {
9844 case SHARP_TOKEN: fprintf (of, "#"); break;
9845 case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
9846 case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
9847 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
9848 default: abort ();
9851 if (ap->raw_before != 0) {
9852 if (concat) {
9853 switch (ap->raw_before) {
9854 case WHITE_SHARP_TOKEN:
9855 case WHITE_PERCENT_COLON_TOKEN:
9856 fprintf (of, " ");
9857 break;
9858 default:
9859 break;
9861 } else {
9862 switch (ap->raw_before) {
9863 case SHARP_TOKEN: fprintf (of, "##"); break;
9864 case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
9865 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
9866 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
9867 default: abort ();
9871 concat = 0;
9873 dump_arg_n (defn, ap->argno, of);
9874 if (!traditional && ap->raw_after != 0) {
9875 switch (ap->raw_after) {
9876 case SHARP_TOKEN: fprintf (of, "##"); break;
9877 case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
9878 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
9879 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
9880 default: abort ();
9882 concat = 1;
9885 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
9886 fprintf (of, "\n");
9889 /* Dump all macro definitions as #defines to stdout. */
9891 static void
9892 dump_all_macros ()
9894 int bucket;
9896 for (bucket = 0; bucket < HASHSIZE; bucket++) {
9897 register HASHNODE *hp;
9899 for (hp = hashtab[bucket]; hp; hp= hp->next) {
9900 if (hp->type == T_MACRO)
9901 dump_single_macro (hp, stdout);
9906 /* Output to OF a substring of a macro definition.
9907 BASE is the beginning of the definition.
9908 Output characters START thru LENGTH.
9909 Unless traditional, discard newlines outside of strings, thus
9910 converting funny-space markers to ordinary spaces. */
9912 static void
9913 dump_defn_1 (base, start, length, of)
9914 U_CHAR *base;
9915 int start;
9916 int length;
9917 FILE *of;
9919 U_CHAR *p = base + start;
9920 U_CHAR *limit = base + start + length;
9922 if (traditional)
9923 fwrite (p, sizeof (*p), length, of);
9924 else {
9925 while (p < limit) {
9926 if (*p == '\"' || *p =='\'') {
9927 U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
9928 NULL_PTR, NULL_PTR);
9929 fwrite (p, sizeof (*p), p1 - p, of);
9930 p = p1;
9931 } else {
9932 if (*p != '\n')
9933 putc (*p, of);
9934 p++;
9940 /* Print the name of argument number ARGNUM of macro definition DEFN
9941 to OF.
9942 Recall that DEFN->args.argnames contains all the arg names
9943 concatenated in reverse order with comma-space in between. */
9945 static void
9946 dump_arg_n (defn, argnum, of)
9947 DEFINITION *defn;
9948 int argnum;
9949 FILE *of;
9951 register U_CHAR *p = defn->args.argnames;
9952 while (argnum + 1 < defn->nargs) {
9953 p = (U_CHAR *) index ((char *) p, ' ') + 1;
9954 argnum++;
9957 while (*p && *p != ',') {
9958 putc (*p, of);
9959 p++;
9963 /* Initialize syntactic classifications of characters. */
9965 static void
9966 initialize_char_syntax ()
9968 register int i;
9971 * Set up is_idchar and is_idstart tables. These should be
9972 * faster than saying (is_alpha (c) || c == '_'), etc.
9973 * Set up these things before calling any routines tthat
9974 * refer to them.
9976 for (i = 'a'; i <= 'z'; i++) {
9977 is_idchar[i - 'a' + 'A'] = 1;
9978 is_idchar[i] = 1;
9979 is_idstart[i - 'a' + 'A'] = 1;
9980 is_idstart[i] = 1;
9982 for (i = '0'; i <= '9'; i++)
9983 is_idchar[i] = 1;
9984 is_idchar['_'] = 1;
9985 is_idstart['_'] = 1;
9986 is_idchar['$'] = 1;
9987 is_idstart['$'] = 1;
9989 /* horizontal space table */
9990 is_hor_space[' '] = 1;
9991 is_hor_space['\t'] = 1;
9992 is_hor_space['\v'] = 1;
9993 is_hor_space['\f'] = 1;
9994 is_hor_space['\r'] = 1;
9996 is_space[' '] = 1;
9997 is_space['\t'] = 1;
9998 is_space['\v'] = 1;
9999 is_space['\f'] = 1;
10000 is_space['\n'] = 1;
10001 is_space['\r'] = 1;
10004 /* Initialize the built-in macros. */
10006 static void
10007 initialize_builtins (inp, outp)
10008 FILE_BUF *inp;
10009 FILE_BUF *outp;
10011 install ((U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
10012 install ((U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
10013 install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
10014 install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
10015 install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
10016 install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
10017 #ifndef NO_BUILTIN_SIZE_TYPE
10018 install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
10019 #endif
10020 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10021 install ((U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
10022 #endif
10023 install ((U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
10024 install ((U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
10025 NULL_PTR, -1);
10026 install ((U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
10027 NULL_PTR, -1);
10028 install ((U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
10029 NULL_PTR, -1);
10030 install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
10031 if (!traditional) {
10032 install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
10033 install ((U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
10035 if (objc)
10036 install ((U_CHAR *) "__OBJC__", -1, T_CONST, "1", -1);
10037 /* This is supplied using a -D by the compiler driver
10038 so that it is present only when truly compiling with GNU C. */
10039 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10040 install ((U_CHAR *) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST, "1", -1);
10042 if (debug_output)
10044 char directive[2048];
10045 U_CHAR *udirective = (U_CHAR *) directive;
10046 register struct directive *dp = &directive_table[0];
10047 struct tm *timebuf = timestamp ();
10049 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
10050 instack[0].nominal_fname);
10051 output_line_directive (inp, outp, 0, same_file);
10052 pass_thru_directive (udirective, &udirective[strlen (directive)],
10053 outp, dp);
10055 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
10056 output_line_directive (inp, outp, 0, same_file);
10057 pass_thru_directive (udirective, &udirective[strlen (directive)],
10058 outp, dp);
10060 #ifndef NO_BUILTIN_SIZE_TYPE
10061 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
10062 output_line_directive (inp, outp, 0, same_file);
10063 pass_thru_directive (udirective, &udirective[strlen (directive)],
10064 outp, dp);
10065 #endif
10067 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10068 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
10069 output_line_directive (inp, outp, 0, same_file);
10070 pass_thru_directive (udirective, &udirective[strlen (directive)],
10071 outp, dp);
10072 #endif
10074 sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
10075 output_line_directive (inp, outp, 0, same_file);
10076 pass_thru_directive (udirective, &udirective[strlen (directive)],
10077 outp, dp);
10079 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
10080 monthnames[timebuf->tm_mon],
10081 timebuf->tm_mday, timebuf->tm_year + 1900);
10082 output_line_directive (inp, outp, 0, same_file);
10083 pass_thru_directive (udirective, &udirective[strlen (directive)],
10084 outp, dp);
10086 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
10087 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
10088 output_line_directive (inp, outp, 0, same_file);
10089 pass_thru_directive (udirective, &udirective[strlen (directive)],
10090 outp, dp);
10092 if (!traditional)
10094 sprintf (directive, " __STDC__ 1");
10095 output_line_directive (inp, outp, 0, same_file);
10096 pass_thru_directive (udirective, &udirective[strlen (directive)],
10097 outp, dp);
10099 if (objc)
10101 sprintf (directive, " __OBJC__ 1");
10102 output_line_directive (inp, outp, 0, same_file);
10103 pass_thru_directive (udirective, &udirective[strlen (directive)],
10104 outp, dp);
10110 * process a given definition string, for initialization
10111 * If STR is just an identifier, define it with value 1.
10112 * If STR has anything after the identifier, then it should
10113 * be identifier=definition.
10116 static void
10117 make_definition (str, op)
10118 char *str;
10119 FILE_BUF *op;
10121 FILE_BUF *ip;
10122 struct directive *kt;
10123 U_CHAR *buf, *p;
10125 p = buf = (U_CHAR *) str;
10126 if (!is_idstart[*p]) {
10127 error ("malformed option `-D %s'", str);
10128 return;
10130 while (is_idchar[*++p])
10132 if (*p == '(') {
10133 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
10135 if (*p++ != ')')
10136 p = (U_CHAR *) str; /* Error */
10138 if (*p == 0) {
10139 buf = (U_CHAR *) alloca (p - buf + 4);
10140 strcpy ((char *)buf, str);
10141 strcat ((char *)buf, " 1");
10142 } else if (*p != '=') {
10143 error ("malformed option `-D %s'", str);
10144 return;
10145 } else {
10146 U_CHAR *q;
10147 /* Copy the entire option so we can modify it. */
10148 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
10149 strncpy ((char *) buf, str, p - (U_CHAR *) str);
10150 /* Change the = to a space. */
10151 buf[p - (U_CHAR *) str] = ' ';
10152 /* Scan for any backslash-newline and remove it. */
10153 p++;
10154 q = &buf[p - (U_CHAR *) str];
10155 while (*p) {
10156 if (*p == '\"' || *p == '\'') {
10157 int unterminated = 0;
10158 U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
10159 NULL_PTR, NULL_PTR, &unterminated);
10160 if (unterminated)
10161 return;
10162 while (p != p1) {
10163 if (*p == '\\' && p[1] == '\n')
10164 p += 2;
10165 else if (*p == '\n')
10167 *q++ = '\\';
10168 *q++ = 'n';
10169 p++;
10171 else
10172 *q++ = *p++;
10174 } else if (*p == '\\' && p[1] == '\n')
10175 p += 2;
10176 /* Change newline chars into newline-markers. */
10177 else if (*p == '\n')
10179 *q++ = '\n';
10180 *q++ = '\n';
10181 p++;
10183 else
10184 *q++ = *p++;
10186 *q = 0;
10189 ip = &instack[++indepth];
10190 ip->nominal_fname = ip->fname = "*Initialization*";
10191 ip->nominal_fname_len = strlen (ip->nominal_fname);
10193 ip->buf = ip->bufp = buf;
10194 ip->length = strlen ((char *) buf);
10195 ip->lineno = 1;
10196 ip->macro = 0;
10197 ip->free_ptr = 0;
10198 ip->if_stack = if_stack;
10199 ip->system_header_p = 0;
10201 for (kt = directive_table; kt->type != T_DEFINE; kt++)
10204 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10205 do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
10206 --indepth;
10209 /* JF, this does the work for the -U option */
10211 static void
10212 make_undef (str, op)
10213 char *str;
10214 FILE_BUF *op;
10216 FILE_BUF *ip;
10217 struct directive *kt;
10219 ip = &instack[++indepth];
10220 ip->nominal_fname = ip->fname = "*undef*";
10221 ip->nominal_fname_len = strlen (ip->nominal_fname);
10223 ip->buf = ip->bufp = (U_CHAR *) str;
10224 ip->length = strlen (str);
10225 ip->lineno = 1;
10226 ip->macro = 0;
10227 ip->free_ptr = 0;
10228 ip->if_stack = if_stack;
10229 ip->system_header_p = 0;
10231 for (kt = directive_table; kt->type != T_UNDEF; kt++)
10234 do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
10235 --indepth;
10238 /* Process the string STR as if it appeared as the body of a #assert.
10239 OPTION is the option name for which STR was the argument. */
10241 static void
10242 make_assertion (option, str)
10243 char *option;
10244 char *str;
10246 FILE_BUF *ip;
10247 struct directive *kt;
10248 U_CHAR *buf, *p, *q;
10250 /* Copy the entire option so we can modify it. */
10251 buf = (U_CHAR *) alloca (strlen (str) + 1);
10252 strcpy ((char *) buf, str);
10253 /* Scan for any backslash-newline and remove it. */
10254 p = q = buf;
10255 while (*p) {
10256 if (*p == '\\' && p[1] == '\n')
10257 p += 2;
10258 else
10259 *q++ = *p++;
10261 *q = 0;
10263 p = buf;
10264 if (!is_idstart[*p]) {
10265 error ("malformed option `%s %s'", option, str);
10266 return;
10268 while (is_idchar[*++p])
10270 SKIP_WHITE_SPACE (p);
10271 if (! (*p == 0 || *p == '(')) {
10272 error ("malformed option `%s %s'", option, str);
10273 return;
10276 ip = &instack[++indepth];
10277 ip->nominal_fname = ip->fname = "*Initialization*";
10278 ip->nominal_fname_len = strlen (ip->nominal_fname);
10280 ip->buf = ip->bufp = buf;
10281 ip->length = strlen ((char *) buf);
10282 ip->lineno = 1;
10283 ip->macro = 0;
10284 ip->free_ptr = 0;
10285 ip->if_stack = if_stack;
10286 ip->system_header_p = 0;
10288 for (kt = directive_table; kt->type != T_ASSERT; kt++)
10291 /* Pass NULL as output ptr to do_define since we KNOW it never does
10292 any output.... */
10293 do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
10294 --indepth;
10297 #ifndef DIR_SEPARATOR
10298 #define DIR_SEPARATOR '/'
10299 #endif
10301 /* The previous include prefix, if any, is PREV_FILE_NAME.
10302 Translate any pathnames with COMPONENT.
10303 Allocate a new include prefix whose name is the
10304 simplified concatenation of PREFIX and NAME,
10305 with a trailing / added if needed.
10306 But return 0 if the include prefix should be ignored,
10307 e.g. because it is a duplicate of PREV_FILE_NAME. */
10309 static struct file_name_list *
10310 new_include_prefix (prev_file_name, component, prefix, name)
10311 struct file_name_list *prev_file_name;
10312 char *component;
10313 char *prefix;
10314 char *name;
10316 if (name == 0)
10317 fatal ("Directory name missing after command line option");
10319 if (*name == 0)
10320 /* Ignore the empty string. */
10321 return 0;
10323 prefix = update_path (prefix, component);
10324 name = update_path (name, component);
10327 struct file_name_list *dir
10328 = ((struct file_name_list *)
10329 xmalloc (sizeof (struct file_name_list)
10330 + strlen (prefix) + strlen (name) + 2));
10331 size_t len;
10332 strcpy (dir->fname, prefix);
10333 strcat (dir->fname, name);
10334 len = simplify_filename (dir->fname);
10336 /* Convert directory name to a prefix. */
10337 if (dir->fname[len - 1] != DIR_SEPARATOR) {
10338 if (len == 1 && dir->fname[len - 1] == '.')
10339 len = 0;
10340 else
10341 dir->fname[len++] = DIR_SEPARATOR;
10342 dir->fname[len] = 0;
10345 /* Ignore a directory whose name matches the previous one. */
10346 if (prev_file_name && !strcmp (prev_file_name->fname, dir->fname)) {
10347 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10348 if (!first_bracket_include)
10349 first_bracket_include = prev_file_name;
10350 free (dir);
10351 return 0;
10354 #ifndef VMS
10355 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10357 /* Add a trailing "." if there is a filename. This increases the number
10358 of systems that can stat directories. We remove it below. */
10359 if (len != 0)
10361 dir->fname[len] = '.';
10362 dir->fname[len + 1] = 0;
10365 /* Ignore a nonexistent directory. */
10366 if (stat (len ? dir->fname : ".", &dir->st) != 0) {
10367 if (errno != ENOENT && errno != ENOTDIR)
10368 error_from_errno (dir->fname);
10369 free (dir);
10370 return 0;
10373 if (len != 0)
10374 dir->fname[len] = 0;
10376 /* Ignore a directory whose identity matches the previous one. */
10377 if (prev_file_name
10378 && INO_T_EQ (prev_file_name->st.st_ino, dir->st.st_ino)
10379 && prev_file_name->st.st_dev == dir->st.st_dev) {
10380 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10381 if (!first_bracket_include)
10382 first_bracket_include = prev_file_name;
10383 free (dir);
10384 return 0;
10386 #endif /* ! VMS */
10388 dir->next = 0;
10389 dir->c_system_include_path = 0;
10390 dir->got_name_map = 0;
10392 return dir;
10396 /* Append a chain of `struct file_name_list's
10397 to the end of the main include chain.
10398 FIRST is the beginning of the chain to append, and LAST is the end. */
10400 static void
10401 append_include_chain (first, last)
10402 struct file_name_list *first, *last;
10404 struct file_name_list *dir;
10406 if (!first || !last)
10407 return;
10409 if (include == 0)
10410 include = first;
10411 else
10412 last_include->next = first;
10414 if (first_bracket_include == 0)
10415 first_bracket_include = first;
10417 for (dir = first; ; dir = dir->next) {
10418 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
10419 if (len > max_include_len)
10420 max_include_len = len;
10421 if (dir == last)
10422 break;
10425 last->next = NULL;
10426 last_include = last;
10429 /* Place into DST a representation of the file named SRC that is suitable
10430 for `make'. Do not null-terminate DST. Return its length. */
10431 static int
10432 quote_string_for_make (dst, src)
10433 char *dst;
10434 char *src;
10436 char *p = src;
10437 int i = 0;
10438 for (;;)
10440 char c = *p++;
10441 switch (c)
10443 case '\0':
10444 case ' ':
10445 case '\t':
10447 /* GNU make uses a weird quoting scheme for white space.
10448 A space or tab preceded by 2N+1 backslashes represents
10449 N backslashes followed by space; a space or tab
10450 preceded by 2N backslashes represents N backslashes at
10451 the end of a file name; and backslashes in other
10452 contexts should not be doubled. */
10453 char *q;
10454 for (q = p - 1; src < q && q[-1] == '\\'; q--)
10456 if (dst)
10457 dst[i] = '\\';
10458 i++;
10461 if (!c)
10462 return i;
10463 if (dst)
10464 dst[i] = '\\';
10465 i++;
10466 goto ordinary_char;
10468 case '$':
10469 if (dst)
10470 dst[i] = c;
10471 i++;
10472 /* Fall through. This can mishandle things like "$(" but
10473 there's no easy fix. */
10474 default:
10475 ordinary_char:
10476 /* This can mishandle characters in the string "\0\n%*?[\\~";
10477 exactly which chars are mishandled depends on the `make' version.
10478 We know of no portable solution for this;
10479 even GNU make 3.76.1 doesn't solve the problem entirely.
10480 (Also, '\0' is mishandled due to our calling conventions.) */
10481 if (dst)
10482 dst[i] = c;
10483 i++;
10484 break;
10490 /* Add output to `deps_buffer' for the -M switch.
10491 STRING points to the text to be output.
10492 SPACER is ':' for targets, ' ' for dependencies. */
10494 static void
10495 deps_output (string, spacer)
10496 char *string;
10497 int spacer;
10499 int size = quote_string_for_make ((char *) 0, string);
10501 if (size == 0)
10502 return;
10504 #ifndef MAX_OUTPUT_COLUMNS
10505 #define MAX_OUTPUT_COLUMNS 72
10506 #endif
10507 if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
10508 && 1 < deps_column) {
10509 bcopy (" \\\n ", &deps_buffer[deps_size], 4);
10510 deps_size += 4;
10511 deps_column = 1;
10512 if (spacer == ' ')
10513 spacer = 0;
10516 if (deps_size + size + 8 > deps_allocated_size) {
10517 deps_allocated_size = (deps_size + size + 50) * 2;
10518 deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
10520 if (spacer == ' ') {
10521 deps_buffer[deps_size++] = ' ';
10522 deps_column++;
10524 quote_string_for_make (&deps_buffer[deps_size], string);
10525 deps_size += size;
10526 deps_column += size;
10527 if (spacer == ':') {
10528 deps_buffer[deps_size++] = ':';
10529 deps_column++;
10531 deps_buffer[deps_size] = 0;
10534 void
10535 fatal (PRINTF_ALIST (msgid))
10536 PRINTF_DCL (msgid)
10538 va_list args;
10540 fprintf (stderr, "%s: ", progname);
10541 VA_START (args, msgid);
10542 vnotice (msgid, args);
10543 va_end (args);
10544 fprintf (stderr, "\n");
10545 exit (FATAL_EXIT_CODE);
10548 /* More 'friendly' abort that prints the line and file.
10549 config.h can #define abort fancy_abort if you like that sort of thing. */
10551 void
10552 fancy_abort ()
10554 fatal ("Internal gcc abort.");
10557 static void
10558 perror_with_name (name)
10559 char *name;
10561 fprintf (stderr, "%s: %s: %s\n", progname, name, my_strerror (errno));
10562 errors++;
10565 static void
10566 pfatal_with_name (name)
10567 char *name;
10569 perror_with_name (name);
10570 #ifdef VMS
10571 exit (vaxc$errno);
10572 #else
10573 exit (FATAL_EXIT_CODE);
10574 #endif
10577 /* Handler for SIGPIPE. */
10579 static void
10580 pipe_closed (signo)
10581 /* If this is missing, some compilers complain. */
10582 int signo;
10584 fatal ("output pipe has been closed");
10587 static void
10588 memory_full ()
10590 fatal ("Memory exhausted.");
10594 GENERIC_PTR
10595 xmalloc (size)
10596 size_t size;
10598 register GENERIC_PTR ptr = (GENERIC_PTR) malloc (size);
10599 if (!ptr)
10600 memory_full ();
10601 return ptr;
10604 static GENERIC_PTR
10605 xrealloc (old, size)
10606 GENERIC_PTR old;
10607 size_t size;
10609 register GENERIC_PTR ptr = (GENERIC_PTR) realloc (old, size);
10610 if (!ptr)
10611 memory_full ();
10612 return ptr;
10615 static GENERIC_PTR
10616 xcalloc (number, size)
10617 size_t number, size;
10619 register size_t total = number * size;
10620 register GENERIC_PTR ptr = (GENERIC_PTR) malloc (total);
10621 if (!ptr)
10622 memory_full ();
10623 bzero (ptr, total);
10624 return ptr;
10627 static char *
10628 savestring (input)
10629 char *input;
10631 size_t size = strlen (input);
10632 char *output = xmalloc (size + 1);
10633 strcpy (output, input);
10634 return output;
10637 #ifdef VMS
10639 /* Under VMS we need to fix up the "include" specification filename so
10640 that everything following the 1st slash is changed into its correct
10641 VMS file specification. */
10643 static void
10644 hack_vms_include_specification (fname, vaxc_include)
10645 char *fname;
10646 int vaxc_include;
10648 register char *cp, *cp1, *cp2;
10649 int f, check_filename_before_returning;
10650 char Local[512];
10652 check_filename_before_returning = 0;
10654 cp = base_name (fname);
10657 * Check if we have a vax-c style '#include filename'
10658 * and add the missing .h
10660 if (vaxc_include && !index (cp,'.'))
10661 strcat (cp, ".h");
10663 cp2 = Local; /* initialize */
10665 /* We are trying to do a number of things here. First of all, we are
10666 trying to hammer the filenames into a standard format, such that later
10667 processing can handle them.
10669 If the file name contains something like [dir.], then it recognizes this
10670 as a root, and strips the ".]". Later processing will add whatever is
10671 needed to get things working properly.
10673 If no device is specified, then the first directory name is taken to be
10674 a device name (or a rooted logical). */
10676 /* See if we found that 1st slash */
10677 if (cp == 0) return; /* Nothing to do!!! */
10678 if (*cp != '/') return; /* Nothing to do!!! */
10679 /* Point to the UNIX filename part (which needs to be fixed!) */
10680 cp1 = cp+1;
10681 /* If the directory spec is not rooted, we can just copy
10682 the UNIX filename part and we are done */
10683 if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
10684 if (cp[-2] != '.') {
10686 * The VMS part ends in a `]', and the preceding character is not a `.'.
10687 * We strip the `]', and then splice the two parts of the name in the
10688 * usual way. Given the default locations for include files in cccp.c,
10689 * we will only use this code if the user specifies alternate locations
10690 * with the /include (-I) switch on the command line. */
10691 cp -= 1; /* Strip "]" */
10692 cp1--; /* backspace */
10693 } else {
10695 * The VMS part has a ".]" at the end, and this will not do. Later
10696 * processing will add a second directory spec, and this would be a syntax
10697 * error. Thus we strip the ".]", and thus merge the directory specs.
10698 * We also backspace cp1, so that it points to a '/'. This inhibits the
10699 * generation of the 000000 root directory spec (which does not belong here
10700 * in this case).
10702 cp -= 2; /* Strip ".]" */
10703 cp1--; }; /* backspace */
10704 } else {
10706 /* We drop in here if there is no VMS style directory specification yet.
10707 * If there is no device specification either, we make the first dir a
10708 * device and try that. If we do not do this, then we will be essentially
10709 * searching the users default directory (as if they did a #include "asdf.h").
10711 * Then all we need to do is to push a '[' into the output string. Later
10712 * processing will fill this in, and close the bracket.
10714 if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec. take first dir */
10715 *cp2++ = '['; /* Open the directory specification */
10718 /* at this point we assume that we have the device spec, and (at least
10719 the opening "[" for a directory specification. We may have directories
10720 specified already */
10722 /* If there are no other slashes then the filename will be
10723 in the "root" directory. Otherwise, we need to add
10724 directory specifications. */
10725 if (index (cp1, '/') == 0) {
10726 /* Just add "000000]" as the directory string */
10727 strcpy (cp2, "000000]");
10728 cp2 += strlen (cp2);
10729 check_filename_before_returning = 1; /* we might need to fool with this later */
10730 } else {
10731 /* As long as there are still subdirectories to add, do them. */
10732 while (index (cp1, '/') != 0) {
10733 /* If this token is "." we can ignore it */
10734 if ((cp1[0] == '.') && (cp1[1] == '/')) {
10735 cp1 += 2;
10736 continue;
10738 /* Add a subdirectory spec. Do not duplicate "." */
10739 if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
10740 *cp2++ = '.';
10741 /* If this is ".." then the spec becomes "-" */
10742 if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
10743 /* Add "-" and skip the ".." */
10744 *cp2++ = '-';
10745 cp1 += 3;
10746 continue;
10748 /* Copy the subdirectory */
10749 while (*cp1 != '/') *cp2++= *cp1++;
10750 cp1++; /* Skip the "/" */
10752 /* Close the directory specification */
10753 if (cp2[-1] == '.') /* no trailing periods */
10754 cp2--;
10755 *cp2++ = ']';
10757 /* Now add the filename */
10758 while (*cp1) *cp2++ = *cp1++;
10759 *cp2 = 0;
10760 /* Now append it to the original VMS spec. */
10761 strcpy (cp, Local);
10763 /* If we put a [000000] in the filename, try to open it first. If this fails,
10764 remove the [000000], and return that name. This provides flexibility
10765 to the user in that they can use both rooted and non-rooted logical names
10766 to point to the location of the file. */
10768 if (check_filename_before_returning) {
10769 f = open (fname, O_RDONLY, 0666);
10770 if (f >= 0) {
10771 /* The file name is OK as it is, so return it as is. */
10772 close (f);
10773 return;
10775 /* The filename did not work. Try to remove the [000000] from the name,
10776 and return it. */
10777 cp = index (fname, '[');
10778 cp2 = index (fname, ']') + 1;
10779 strcpy (cp, cp2); /* this gets rid of it */
10781 return;
10783 #endif /* VMS */
10785 #ifdef VMS
10787 /* The following wrapper functions supply additional arguments to the VMS
10788 I/O routines to optimize performance with file handling. The arguments
10789 are:
10790 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
10791 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
10792 "fop=tef"- Truncate unused portions of file when closing file.
10793 "shr=nil"- Disallow file sharing while file is open. */
10795 static FILE *
10796 VMS_freopen (fname, type, oldfile)
10797 char *fname;
10798 char *type;
10799 FILE *oldfile;
10801 #undef freopen /* Get back the real freopen routine. */
10802 if (strcmp (type, "w") == 0)
10803 return freopen (fname, type, oldfile,
10804 "mbc=16", "deq=64", "fop=tef", "shr=nil");
10805 return freopen (fname, type, oldfile, "mbc=16");
10808 static FILE *
10809 VMS_fopen (fname, type)
10810 char *fname;
10811 char *type;
10813 #undef fopen /* Get back the real fopen routine. */
10814 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
10815 fixed arguments, which matches ANSI's specification but not VAXCRTL's
10816 pre-ANSI implementation. This hack circumvents the mismatch problem. */
10817 FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
10819 if (*type == 'w')
10820 return (*vmslib_fopen) (fname, type, "mbc=32",
10821 "deq=64", "fop=tef", "shr=nil");
10822 else
10823 return (*vmslib_fopen) (fname, type, "mbc=32");
10826 static int
10827 VMS_open (fname, flags, prot)
10828 char *fname;
10829 int flags;
10830 int prot;
10832 #undef open /* Get back the real open routine. */
10833 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
10836 /* more VMS hackery */
10837 #include <fab.h>
10838 #include <nam.h>
10840 extern unsigned long sys$parse(), sys$search();
10842 /* Work around another library bug. If a file is located via a searchlist,
10843 and if the device it's on is not the same device as the one specified
10844 in the first element of that searchlist, then both stat() and fstat()
10845 will fail to return info about it. `errno' will be set to EVMSERR, and
10846 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
10847 We can get around this by fully parsing the filename and then passing
10848 that absolute name to stat().
10850 Without this fix, we can end up failing to find header files, which is
10851 bad enough, but then compounding the problem by reporting the reason for
10852 failure as "normal successful completion." */
10854 #undef fstat /* Get back to the library version. */
10856 static int
10857 VMS_fstat (fd, statbuf)
10858 int fd;
10859 struct stat *statbuf;
10861 int result = fstat (fd, statbuf);
10863 if (result < 0)
10865 FILE *fp;
10866 char nambuf[NAM$C_MAXRSS+1];
10868 if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
10869 result = VMS_stat (nambuf, statbuf);
10870 /* No fclose(fp) here; that would close(fd) as well. */
10873 return result;
10876 static int
10877 VMS_stat (name, statbuf)
10878 const char *name;
10879 struct stat *statbuf;
10881 int result = stat (name, statbuf);
10883 if (result < 0)
10885 struct FAB fab;
10886 struct NAM nam;
10887 char exp_nam[NAM$C_MAXRSS+1], /* expanded name buffer for sys$parse */
10888 res_nam[NAM$C_MAXRSS+1]; /* resultant name buffer for sys$search */
10890 fab = cc$rms_fab;
10891 fab.fab$l_fna = (char *) name;
10892 fab.fab$b_fns = (unsigned char) strlen (name);
10893 fab.fab$l_nam = (void *) &nam;
10894 nam = cc$rms_nam;
10895 nam.nam$l_esa = exp_nam, nam.nam$b_ess = sizeof exp_nam - 1;
10896 nam.nam$l_rsa = res_nam, nam.nam$b_rss = sizeof res_nam - 1;
10897 nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
10898 if (sys$parse (&fab) & 1)
10900 if (sys$search (&fab) & 1)
10902 res_nam[nam.nam$b_rsl] = '\0';
10903 result = stat (res_nam, statbuf);
10905 /* Clean up searchlist context cached by the system. */
10906 nam.nam$b_nop = NAM$M_SYNCHK;
10907 fab.fab$l_fna = 0, fab.fab$b_fns = 0;
10908 (void) sys$parse (&fab);
10912 return result;
10915 static size_t
10916 VMS_fwrite (ptr, size, nitems, stream)
10917 void const *ptr;
10918 size_t size;
10919 size_t nitems;
10920 FILE *stream;
10922 /* VMS fwrite has undesirable results
10923 if STREAM happens to be a record oriented file.
10924 Work around this problem by writing each character individually. */
10925 char const *p = ptr;
10926 size_t bytes = size * nitems;
10927 char *lim = p + bytes;
10929 while (p < lim)
10930 if (putc (*p++, stream) == EOF)
10931 return 0;
10933 return bytes;
10935 #endif /* VMS */