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
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. */
28 #define PRINTF_PROTO(ARGS, m, n) PVPROTO (ARGS) ATTRIBUTE_PRINTF(m, n)
30 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
31 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
32 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
33 #define PRINTF_PROTO_4(ARGS) PRINTF_PROTO(ARGS, 4, 5)
39 #ifdef HAVE_SYS_RESOURCE_H
40 # include <sys/resource.h>
43 typedef unsigned char U_CHAR;
45 #include "gansidecl.h"
48 #ifdef MULTIBYTE_CHARS
51 #endif /* MULTIBYTE_CHARS */
53 #ifndef GET_ENVIRONMENT
54 #define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ENV_VALUE = getenv (ENV_NAME)
57 #ifndef STANDARD_INCLUDE_DIR
58 # define STANDARD_INCLUDE_DIR "/usr/include"
61 /* By default, colon separates directories in a path. */
62 #ifndef PATH_SEPARATOR
63 # define PATH_SEPARATOR ':'
66 /* By default, the suffix for object files is ".o". */
68 # define HAVE_OBJECT_SUFFIX
70 # define OBJECT_SUFFIX ".o"
73 /* VMS-specific definitions */
78 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
79 #define fopen(fname,mode) VMS_fopen (fname,mode)
80 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
81 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
82 static int VMS_fstat (), VMS_stat ();
83 static int VMS_open ();
84 static FILE *VMS_fopen ();
85 static FILE *VMS_freopen ();
86 static int hack_vms_include_specification ();
87 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
88 #define INO_T_HASH(a) 0
89 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
92 /* Windows does not natively support inodes, and neither does MSDOS. */
93 #if (defined (_WIN32) && ! defined (CYGWIN32)) || defined (__MSDOS__)
94 #define INO_T_EQ(a, b) 0
99 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
100 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
102 /* Find the largest host integer type and set its size and type.
103 Watch out: on some crazy hosts `long' is shorter than `int'. */
105 #ifndef HOST_WIDE_INT
107 # include <inttypes.h>
108 # define HOST_WIDE_INT intmax_t
110 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
111 # define HOST_WIDE_INT int
113 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
114 # define HOST_WIDE_INT long
116 # define HOST_WIDE_INT long long
123 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
127 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
131 #define INO_T_EQ(a, b) ((a) == (b))
135 #define INO_T_HASH(a) (a)
138 #ifndef INCLUDE_LEN_FUDGE
139 #define INCLUDE_LEN_FUDGE 0
142 /* External declarations. */
144 extern char *version_string;
145 extern char *update_path PROTO((char *, char *));
146 HOST_WIDE_INT parse_escape PROTO((char **, HOST_WIDE_INT));
147 HOST_WIDE_INT parse_c_expression PROTO((char *, int));
149 /* Name under which this program was invoked. */
151 static char *progname;
153 /* Nonzero means use extra default include directories for C++. */
155 static int cplusplus;
157 /* Nonzero means handle cplusplus style comments */
159 static int cplusplus_comments;
161 /* Nonzero means handle #import, for objective C. */
165 /* Nonzero means this is an assembly file, and allow
166 unknown directives, which could be comments. */
170 /* Current maximum length of directory names in the search path
171 for include files. (Altered as we get more of them.) */
173 static int max_include_len;
175 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
177 static int for_lint = 0;
179 /* Nonzero means copy comments into the output file. */
181 static int put_out_comments = 0;
183 /* Nonzero means don't process the ANSI trigraph sequences. */
185 static int no_trigraphs = 0;
187 /* Nonzero means print the names of included files rather than
188 the preprocessed output. 1 means just the #include "...",
189 2 means #include <...> as well. */
191 static int print_deps = 0;
193 /* Nonzero if missing .h files in -M output are assumed to be generated
194 files and not errors. */
196 static int print_deps_missing_files = 0;
198 /* Nonzero means print names of header files (-H). */
200 static int print_include_names = 0;
202 /* Nonzero means don't output line number information. */
204 static int no_line_directives;
206 /* Nonzero means output the text in failing conditionals,
207 inside #failed ... #endfailed. */
209 static int output_conditionals;
211 /* dump_only means inhibit output of the preprocessed text
212 and instead output the definitions of all user-defined
213 macros in a form suitable for use as input to cccp.
214 dump_names means pass #define and the macro name through to output.
215 dump_definitions means pass the whole definition (plus #define) through
218 static enum {dump_none, dump_only, dump_names, dump_definitions}
219 dump_macros = dump_none;
221 /* Nonzero means pass all #define and #undef directives which we actually
222 process through to the output stream. This feature is used primarily
223 to allow cc1 to record the #defines and #undefs for the sake of
224 debuggers which understand about preprocessor macros, but it may
225 also be useful with -E to figure out how symbols are defined, and
226 where they are defined. */
227 static int debug_output = 0;
229 /* Nonzero means pass #include lines through to the output,
230 even if they are ifdefed out. */
231 static int dump_includes;
233 /* Nonzero indicates special processing used by the pcp program. The
234 special effects of this mode are:
236 Inhibit all macro expansion, except those inside #if directives.
238 Process #define directives normally, and output their contents
241 Output preconditions to pcp_outfile indicating all the relevant
242 preconditions for use of this file in a later cpp run.
244 static FILE *pcp_outfile;
246 /* Nonzero means we are inside an IF during a -pcp run. In this mode
247 macro expansion is done, and preconditions are output for all macro
248 uses requiring them. */
249 static int pcp_inside_if;
251 /* Nonzero means never to include precompiled files.
252 This is 1 since there's no way now to make precompiled files,
253 so it's not worth testing for them. */
254 static int no_precomp = 1;
256 /* Nonzero means give all the error messages the ANSI standard requires. */
260 /* Nonzero means try to make failure to fit ANSI C an error. */
262 static int pedantic_errors;
264 /* Nonzero means don't print warning messages. -w. */
266 static int inhibit_warnings = 0;
268 /* Nonzero means warn if slash-star appears in a slash-star comment,
269 or if newline-backslash appears in a slash-slash comment. */
271 static int warn_comments;
273 /* Nonzero means warn if a macro argument is (or would be)
274 stringified with -traditional. */
276 static int warn_stringify;
278 /* Nonzero means warn if there are any trigraphs. */
280 static int warn_trigraphs;
282 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
284 static int warn_undef;
286 /* Nonzero means warn if #import is used. */
288 static int warn_import = 1;
290 /* Nonzero means turn warnings into errors. */
292 static int warnings_are_errors;
294 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
298 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
302 /* Nonzero causes output not to be done,
303 but directives such as #define that have side effects
306 static int no_output;
308 /* Nonzero means we should look for header.gcc files that remap file names. */
311 /* Nonzero means this file was included with a -imacros or -include
312 command line and should not be recorded as an include file. */
314 static int no_record_file;
316 /* Nonzero means that we have finished processing the command line options.
317 This flag is used to decide whether or not to issue certain errors
320 static int done_initializing = 0;
322 /* Line where a newline was first seen in a string constant. */
324 static int multiline_string_line = 0;
326 /* I/O buffer structure.
327 The `fname' field is nonzero for source files and #include files
328 and for the dummy text used for -D and -U.
329 It is zero for rescanning results of macro expansion
330 and for expanding macro arguments. */
331 #define INPUT_STACK_MAX 400
332 static struct file_buf {
334 /* Filename specified with #line directive. */
336 /* The length of nominal_fname, which may contain embedded NULs. */
337 size_t nominal_fname_len;
338 /* Include file description. */
339 struct include_file *inc;
340 /* Record where in the search path this file was found.
341 For #include_next. */
342 struct file_name_list *dir;
347 /* Macro that this level is the expansion of.
348 Included so that we can reenable the macro
349 at the end of this level. */
350 struct hashnode *macro;
351 /* Value of if_stack at start of this file.
352 Used to prohibit unmatched #endif (etc) in an include file. */
353 struct if_stack *if_stack;
354 /* Object to be freed at end of input at this level. */
356 /* True if this is a system header file; see is_system_include. */
357 char system_header_p;
358 } instack[INPUT_STACK_MAX];
360 static int last_error_tick; /* Incremented each time we print it. */
361 static int input_file_stack_tick; /* Incremented when the status changes. */
363 /* Current nesting level of input sources.
364 `instack[indepth]' is the level currently being read. */
365 static int indepth = -1;
366 #define CHECK_DEPTH(code) \
367 if (indepth >= (INPUT_STACK_MAX - 1)) \
369 error_with_line (line_for_error (instack[indepth].lineno), \
370 "macro or `#include' recursion too deep"); \
374 /* Current depth in #include directives that use <...>. */
375 static int system_include_depth = 0;
377 typedef struct file_buf FILE_BUF;
379 /* The output buffer. Its LENGTH field is the amount of room allocated
380 for the buffer, not the number of chars actually present. To get
381 that, subtract outbuf.buf from outbuf.bufp. */
383 #define OUTBUF_SIZE 10 /* initial size of output buffer */
384 static FILE_BUF outbuf;
386 /* Grow output buffer OBUF points at
387 so it can hold at least NEEDED more chars. */
389 #define check_expand(OBUF, NEEDED) \
390 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
391 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
393 struct file_name_list
395 struct file_name_list *next;
396 /* If the following is 1, it is a C-language system include
398 int c_system_include_path;
399 /* Mapping of file names for this directory. */
400 struct file_name_map *name_map;
401 /* Non-zero if name_map is valid. */
403 /* The include directory status. */
405 /* The include prefix: "" denotes the working directory,
406 otherwise fname must end in '/'.
407 The actual size is dynamically allocated. */
411 /* #include "file" looks in source file dir, then stack. */
412 /* #include <file> just looks in the stack. */
413 /* -I directories are added to the end, then the defaults are added. */
415 static struct default_include {
416 char *fname; /* The name of the directory. */
417 char *component; /* The component containing the directory */
418 int cplusplus; /* Only look here if we're compiling C++. */
419 int cxx_aware; /* Includes in this directory don't need to
420 be wrapped in extern "C" when compiling
422 } include_defaults_array[]
423 #ifdef INCLUDE_DEFAULTS
427 /* Pick up GNU C++ specific include files. */
428 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
429 { OLD_GPLUSPLUS_INCLUDE_DIR, 0, 1, 1 },
431 /* This is the dir for fixincludes. Put it just before
432 the files that we fix. */
433 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
434 /* For cross-compilation, this dir name is generated
435 automatically in Makefile.in. */
436 { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
437 #ifdef TOOL_INCLUDE_DIR
438 /* This is another place that the target system's headers might be. */
439 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 },
441 #else /* not CROSS_COMPILE */
442 #ifdef LOCAL_INCLUDE_DIR
443 /* This should be /usr/local/include and should come before
444 the fixincludes-fixed header files. */
445 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
447 #ifdef TOOL_INCLUDE_DIR
448 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
449 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
450 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 },
452 /* This is the dir for fixincludes. Put it just before
453 the files that we fix. */
454 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
455 /* Some systems have an extra dir of include files. */
456 #ifdef SYSTEM_INCLUDE_DIR
457 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
459 #ifndef STANDARD_INCLUDE_COMPONENT
460 #define STANDARD_INCLUDE_COMPONENT 0
462 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
463 #endif /* not CROSS_COMPILE */
466 #endif /* no INCLUDE_DEFAULTS */
468 /* The code looks at the defaults through this pointer, rather than through
469 the constant structure above. This pointer gets changed if an environment
470 variable specifies other defaults. */
471 static struct default_include *include_defaults = include_defaults_array;
473 static struct file_name_list *include = 0; /* First dir to search */
474 /* First dir to search for <file> */
475 /* This is the first element to use for #include <...>.
476 If it is 0, use the entire chain for such includes. */
477 static struct file_name_list *first_bracket_include = 0;
478 /* This is the first element in the chain that corresponds to
479 a directory of system header files. */
480 static struct file_name_list *first_system_include = 0;
481 static struct file_name_list *last_include = 0; /* Last in chain */
483 /* Chain of include directories to put at the end of the other chain. */
484 static struct file_name_list *after_include = 0;
485 static struct file_name_list *last_after_include = 0; /* Last in chain */
487 /* Chain to put at the start of the system include files. */
488 static struct file_name_list *before_system = 0;
489 static struct file_name_list *last_before_system = 0; /* Last in chain */
491 /* Directory prefix that should replace `/usr' in the standard
492 include file directories. */
493 static char *include_prefix;
495 /* Maintain and search list of included files. */
497 struct include_file {
498 struct include_file *next; /* for include_hashtab */
499 struct include_file *next_ino; /* for include_ino_hashtab */
501 /* If the following is the empty string, it means #pragma once
502 was seen in this include file, or #import was applied to the file.
503 Otherwise, if it is nonzero, it is a macro name.
504 Don't include the file again if that macro is defined. */
505 U_CHAR *control_macro;
506 /* Nonzero if the dependency on this include file has been output. */
511 /* Hash tables of files already included with #include or #import.
512 include_hashtab is by full name; include_ino_hashtab is by inode number. */
514 #define INCLUDE_HASHSIZE 61
515 static struct include_file *include_hashtab[INCLUDE_HASHSIZE];
516 static struct include_file *include_ino_hashtab[INCLUDE_HASHSIZE];
518 /* Global list of strings read in from precompiled files. This list
519 is kept in the order the strings are read in, with new strings being
520 added at the end through stringlist_tailp. We use this list to output
521 the strings at the end of the run.
523 static STRINGDEF *stringlist;
524 static STRINGDEF **stringlist_tailp = &stringlist;
527 /* Structure returned by create_definition */
528 typedef struct macrodef MACRODEF;
531 struct definition *defn;
536 enum sharp_token_type {
537 NO_SHARP_TOKEN = 0, /* token not present */
539 SHARP_TOKEN = '#', /* token spelled with # only */
540 WHITE_SHARP_TOKEN, /* token spelled with # and white space */
542 PERCENT_COLON_TOKEN = '%', /* token spelled with %: only */
543 WHITE_PERCENT_COLON_TOKEN /* token spelled with %: and white space */
546 /* Structure allocated for every #define. For a simple replacement
549 nargs = -1, the `pattern' list is null, and the expansion is just
550 the replacement text. Nargs = 0 means a functionlike macro with no args,
552 #define getchar() getc (stdin) .
553 When there are args, the expansion is the replacement text with the
554 args squashed out, and the reflist is a list describing how to
555 build the output from the input: e.g., "3 chars, then the 1st arg,
556 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
557 The chars here come from the expansion. Whatever is left of the
558 expansion after the last arg-occurrence is copied after that arg.
559 Note that the reflist can be arbitrarily long---
560 its length depends on the number of times the arguments appear in
561 the replacement text, not how many args there are. Example:
562 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
564 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
565 where (x, y) means (nchars, argno). */
567 typedef struct definition DEFINITION;
570 int length; /* length of expansion string */
571 int predefined; /* True if the macro was builtin or */
572 /* came from the command line */
574 int line; /* Line number of definition */
575 char *file; /* File of definition */
576 size_t file_len; /* Length of file (which can contain NULs) */
577 char rest_args; /* Nonzero if last arg. absorbs the rest */
579 struct reflist *next;
581 enum sharp_token_type stringify; /* set if a # operator before arg */
582 enum sharp_token_type raw_before; /* set if a ## operator before arg */
583 enum sharp_token_type raw_after; /* set if a ## operator after arg */
585 char rest_args; /* Nonzero if this arg. absorbs the rest */
586 int nchars; /* Number of literal chars to copy before
587 this arg occurrence. */
588 int argno; /* Number of arg to substitute (origin-0) */
591 /* Names of macro args, concatenated in reverse order
592 with comma-space between them.
593 The only use of this is that we warn on redefinition
594 if this differs between the old and new definitions. */
599 /* different kinds of things that can appear in the value field
600 of a hash node. Actually, this may be useless now. */
608 * special extension string that can be added to the last macro argument to
609 * allow it to absorb the "rest" of the arguments when expanded. Ex:
610 * #define wow(a, b...) process (b, a, b)
611 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
612 * { wow (one, two); } -> { process (two, one, two); }
613 * if this "rest_arg" is used with the concat token '##' and if it is not
614 * supplied then the token attached to with ## will not be outputted. Ex:
615 * #define wow (a, b...) process (b ## , a, ## b)
616 * { wow (1, 2); } -> { process (2, 1, 2); }
617 * { wow (one); } -> { process (one); {
619 static char rest_extension[] = "...";
620 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
622 /* The structure of a node in the hash table. The hash table
623 has entries for all tokens defined by #define directives (type T_MACRO),
624 plus some special tokens like __LINE__ (these each have their own
625 type, and the appropriate code is run when that type of node is seen.
626 It does not contain control words like "#define", which are recognized
627 by a separate piece of code. */
629 /* different flavors of hash nodes --- also used in keyword table */
631 T_DEFINE = 1, /* the `#define' keyword */
632 T_INCLUDE, /* the `#include' keyword */
633 T_INCLUDE_NEXT, /* the `#include_next' keyword */
634 T_IMPORT, /* the `#import' keyword */
635 T_IFDEF, /* the `#ifdef' keyword */
636 T_IFNDEF, /* the `#ifndef' keyword */
637 T_IF, /* the `#if' keyword */
638 T_ELSE, /* `#else' */
639 T_PRAGMA, /* `#pragma' */
640 T_ELIF, /* `#elif' */
641 T_UNDEF, /* `#undef' */
642 T_LINE, /* `#line' */
643 T_ERROR, /* `#error' */
644 T_WARNING, /* `#warning' */
645 T_ENDIF, /* `#endif' */
646 T_SCCS, /* `#sccs', used on system V. */
647 T_IDENT, /* `#ident', used on system V. */
648 T_ASSERT, /* `#assert', taken from system V. */
649 T_UNASSERT, /* `#unassert', taken from system V. */
650 T_SPECLINE, /* special symbol `__LINE__' */
651 T_DATE, /* `__DATE__' */
652 T_FILE, /* `__FILE__' */
653 T_BASE_FILE, /* `__BASE_FILE__' */
654 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
655 T_VERSION, /* `__VERSION__' */
656 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
657 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
658 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
659 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
660 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
661 T_IMMEDIATE_PREFIX_TYPE, /* `__IMMEDIATE_PREFIX__' */
662 T_TIME, /* `__TIME__' */
663 T_CONST, /* Constant value, used by `__STDC__' */
664 T_MACRO, /* macro defined by `#define' */
665 T_DISABLED, /* macro temporarily turned off for rescan */
666 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
667 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
668 T_UNUSED /* Used for something not defined. */
672 struct hashnode *next; /* double links for easy deletion */
673 struct hashnode *prev;
674 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
675 chain is kept, in case the node is the head
676 of the chain and gets deleted. */
677 enum node_type type; /* type of special token */
678 int length; /* length of token, for quick comparison */
679 U_CHAR *name; /* the actual name */
680 union hashval value; /* pointer to expansion, or whatever */
683 typedef struct hashnode HASHNODE;
685 /* Some definitions for the hash table. The hash function MUST be
686 computed as shown in hashf () below. That is because the rescan
687 loop computes the hash value `on the fly' for most tokens,
688 in order to avoid the overhead of a lot of procedure calls to
689 the hashf () function. Hashf () only exists for the sake of
690 politeness, for use when speed isn't so important. */
692 #define HASHSIZE 1403
693 static HASHNODE *hashtab[HASHSIZE];
694 #define HASHSTEP(old, c) ((old << 2) + c)
695 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
697 /* Symbols to predefine. */
699 #ifdef CPP_PREDEFINES
700 static char *predefs = CPP_PREDEFINES;
702 static char *predefs = "";
705 /* We let tm.h override the types used here, to handle trivial differences
706 such as the choice of unsigned int or long unsigned int for size_t.
707 When machines start needing nontrivial differences in the size type,
708 it would be best to do something here to figure out automatically
709 from other information what type to use. */
711 /* The string value for __SIZE_TYPE__. */
714 #define SIZE_TYPE "long unsigned int"
717 /* The string value for __PTRDIFF_TYPE__. */
720 #define PTRDIFF_TYPE "long int"
723 /* The string value for __WCHAR_TYPE__. */
726 #define WCHAR_TYPE "int"
728 char * wchar_type = WCHAR_TYPE;
731 /* The string value for __USER_LABEL_PREFIX__ */
733 #ifndef USER_LABEL_PREFIX
734 #define USER_LABEL_PREFIX ""
737 /* The string value for __REGISTER_PREFIX__ */
739 #ifndef REGISTER_PREFIX
740 #define REGISTER_PREFIX ""
743 /* The string value for __IMMEDIATE_PREFIX__ */
745 #ifndef IMMEDIATE_PREFIX
746 #define IMMEDIATE_PREFIX ""
749 /* In the definition of a #assert name, this structure forms
750 a list of the individual values asserted.
751 Each value is itself a list of "tokens".
752 These are strings that are compared by name. */
754 struct tokenlist_list {
755 struct tokenlist_list *next;
756 struct arglist *tokens;
759 struct assertion_hashnode {
760 struct assertion_hashnode *next; /* double links for easy deletion */
761 struct assertion_hashnode *prev;
762 /* also, a back pointer to this node's hash
763 chain is kept, in case the node is the head
764 of the chain and gets deleted. */
765 struct assertion_hashnode **bucket_hdr;
766 int length; /* length of token, for quick comparison */
767 U_CHAR *name; /* the actual name */
768 /* List of token-sequences. */
769 struct tokenlist_list *value;
772 typedef struct assertion_hashnode ASSERTION_HASHNODE;
774 /* Some definitions for the hash table. The hash function MUST be
775 computed as shown in hashf below. That is because the rescan
776 loop computes the hash value `on the fly' for most tokens,
777 in order to avoid the overhead of a lot of procedure calls to
778 the hashf function. hashf only exists for the sake of
779 politeness, for use when speed isn't so important. */
781 #define ASSERTION_HASHSIZE 37
782 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
784 /* Nonzero means inhibit macroexpansion of what seem to be
785 assertion tests, in rescan. For #if. */
786 static int assertions_flag;
788 /* `struct directive' defines one #-directive, including how to handle it. */
790 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
793 int length; /* Length of name */
794 int (*func) DO_PROTO; /* Function to handle directive */
795 char *name; /* Name of directive */
796 enum node_type type; /* Code which describes which directive. */
799 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
800 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
802 /* These functions are declared to return int instead of void since they
803 are going to be placed in the table and some old compilers have trouble with
804 pointers to functions returning void. */
806 static int do_assert DO_PROTO;
807 static int do_define DO_PROTO;
808 static int do_elif DO_PROTO;
809 static int do_else DO_PROTO;
810 static int do_endif DO_PROTO;
811 static int do_error DO_PROTO;
812 static int do_ident DO_PROTO;
813 static int do_if DO_PROTO;
814 static int do_include DO_PROTO;
815 static int do_line DO_PROTO;
816 static int do_pragma DO_PROTO;
817 #ifdef SCCS_DIRECTIVE
818 static int do_sccs DO_PROTO;
820 static int do_unassert DO_PROTO;
821 static int do_undef DO_PROTO;
822 static int do_warning DO_PROTO;
823 static int do_xifdef DO_PROTO;
825 /* Here is the actual list of #-directives, most-often-used first. */
827 static struct directive directive_table[] = {
828 { 6, do_define, "define", T_DEFINE},
829 { 2, do_if, "if", T_IF},
830 { 5, do_xifdef, "ifdef", T_IFDEF},
831 { 6, do_xifdef, "ifndef", T_IFNDEF},
832 { 5, do_endif, "endif", T_ENDIF},
833 { 4, do_else, "else", T_ELSE},
834 { 4, do_elif, "elif", T_ELIF},
835 { 4, do_line, "line", T_LINE},
836 { 7, do_include, "include", T_INCLUDE},
837 { 12, do_include, "include_next", T_INCLUDE_NEXT},
838 { 6, do_include, "import", T_IMPORT},
839 { 5, do_undef, "undef", T_UNDEF},
840 { 5, do_error, "error", T_ERROR},
841 { 7, do_warning, "warning", T_WARNING},
842 #ifdef SCCS_DIRECTIVE
843 { 4, do_sccs, "sccs", T_SCCS},
845 { 6, do_pragma, "pragma", T_PRAGMA},
846 { 5, do_ident, "ident", T_IDENT},
847 { 6, do_assert, "assert", T_ASSERT},
848 { 8, do_unassert, "unassert", T_UNASSERT},
849 { -1, 0, "", T_UNUSED},
852 /* When a directive handler is called,
853 this points to the # (or the : of the %:) that started the directive. */
854 U_CHAR *directive_start;
856 /* table to tell if char can be part of a C identifier. */
857 U_CHAR is_idchar[256];
858 /* table to tell if char can be first char of a c identifier. */
859 U_CHAR is_idstart[256];
860 /* table to tell if c is horizontal space. */
861 static U_CHAR is_hor_space[256];
862 /* table to tell if c is horizontal or vertical space. */
863 U_CHAR is_space[256];
864 /* names of some characters */
865 static char *char_name[256];
867 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
868 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
870 static int errors = 0; /* Error counter for exit code */
872 /* Name of output file, for error messages. */
873 static char *out_fname;
876 /* Stack of conditionals currently in progress
877 (including both successful and failing conditionals). */
880 struct if_stack *next; /* for chaining to the next stack frame */
881 char *fname; /* copied from input when frame is made */
882 size_t fname_len; /* similarly */
883 int lineno; /* similarly */
884 int if_succeeded; /* true if a leg of this if-group
885 has been passed through rescan */
886 U_CHAR *control_macro; /* For #ifndef at start of file,
887 this is the macro name tested. */
888 enum node_type type; /* type of last directive seen in this group */
890 typedef struct if_stack IF_STACK_FRAME;
891 static IF_STACK_FRAME *if_stack = NULL;
893 /* Buffer of -M output. */
894 static char *deps_buffer;
896 /* Number of bytes allocated in above. */
897 static int deps_allocated_size;
899 /* Number of bytes used. */
900 static int deps_size;
902 /* Number of bytes since the last newline. */
903 static int deps_column;
905 /* Nonzero means -I- has been seen,
906 so don't look for #include "foo" the source-file directory. */
907 static int ignore_srcdir;
909 static int safe_read PROTO((int, char *, int));
910 static void safe_write PROTO((int, char *, int));
911 static void eprint_string PROTO((char *, size_t));
913 int main PROTO((int, char **));
915 static void path_include PROTO((char *));
917 static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
919 static void trigraph_pcp PROTO((FILE_BUF *));
921 static void newline_fix PROTO((U_CHAR *));
922 static void name_newline_fix PROTO((U_CHAR *));
924 static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
926 static void rescan PROTO((FILE_BUF *, int));
928 static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
930 static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
932 static struct tm *timestamp PROTO((void));
933 static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
935 static int is_system_include PROTO((char *));
936 static char *base_name PROTO((char *));
937 static int absolute_filename PROTO((char *));
938 static size_t simplify_filename PROTO((char *));
940 static char *read_filename_string PROTO((int, FILE *));
941 static struct file_name_map *read_name_map PROTO((char *));
942 static int open_include_file PROTO((char *, struct file_name_list *, U_CHAR *, struct include_file **));
943 static char *remap_include_file PROTO((char *, struct file_name_list *));
944 static int lookup_ino_include PROTO((struct include_file *));
946 static void finclude PROTO((int, struct include_file *, FILE_BUF *, int, struct file_name_list *));
947 static void record_control_macro PROTO((struct include_file *, U_CHAR *));
949 static char *check_precompiled PROTO((int, struct stat *, char *, char **));
950 static int check_preconditions PROTO((char *));
951 static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *));
952 static void pcstring_used PROTO((HASHNODE *));
953 static void write_output PROTO((void));
954 static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
956 static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
958 static int check_macro_name PROTO((U_CHAR *, char *));
959 static int compare_defs PROTO((DEFINITION *, DEFINITION *));
960 static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
962 static DEFINITION *collect_expansion PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
964 int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
965 static int compare_token_lists PROTO((struct arglist *, struct arglist *));
967 static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
968 static void free_token_list PROTO((struct arglist *));
970 static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
971 static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
972 static void delete_assertion PROTO((ASSERTION_HASHNODE *));
974 static void do_once PROTO((void));
976 static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
977 static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
978 static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
979 static void validate_else PROTO((U_CHAR *, U_CHAR *));
981 static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
982 static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
983 static char *quote_string PROTO((char *, char *, size_t));
984 static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
986 /* Last arg to output_line_directive. */
987 enum file_change_code {same_file, enter_file, leave_file};
988 static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
990 static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
993 static char *macarg PROTO((struct argdata *, int));
995 static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, struct hashnode *, int *, int *, int *, int));
997 static int discard_comments PROTO((U_CHAR *, int, int));
999 static int change_newlines PROTO((U_CHAR *, int));
1001 static char *my_strerror PROTO((int));
1002 void error PRINTF_PROTO_1((char *, ...));
1003 static void verror PROTO((char *, va_list));
1004 static void error_from_errno PROTO((char *));
1005 void warning PRINTF_PROTO_1((char *, ...));
1006 static void vwarning PROTO((char *, va_list));
1007 static void error_with_line PRINTF_PROTO_2((int, char *, ...));
1008 static void verror_with_line PROTO((int, char *, va_list));
1009 static void vwarning_with_line PROTO((int, char *, va_list));
1010 static void warning_with_line PRINTF_PROTO_2((int, char *, ...));
1011 void pedwarn PRINTF_PROTO_1((char *, ...));
1012 void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
1013 static void pedwarn_with_file_and_line PRINTF_PROTO_4((char *, size_t, int, char *, ...));
1015 static void print_containing_files PROTO((void));
1017 static int line_for_error PROTO((int));
1018 static int grow_outbuf PROTO((FILE_BUF *, int));
1020 static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
1021 HASHNODE *lookup PROTO((U_CHAR *, int, int));
1022 static void delete_macro PROTO((HASHNODE *));
1023 static int hashf PROTO((U_CHAR *, int, int));
1025 static void dump_single_macro PROTO((HASHNODE *, FILE *));
1026 static void dump_all_macros PROTO((void));
1027 static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
1028 static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
1030 static void initialize_char_syntax PROTO((void));
1031 static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
1033 static void make_definition PROTO((char *, FILE_BUF *));
1034 static void make_undef PROTO((char *, FILE_BUF *));
1036 static void make_assertion PROTO((char *, char *));
1038 static struct file_name_list *new_include_prefix PROTO((struct file_name_list *, char *, char *, char *));
1039 static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
1041 static int quote_string_for_make PROTO((char *, char *));
1042 static void deps_output PROTO((char *, int));
1044 static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
1045 void fancy_abort PROTO((void)) __attribute__ ((noreturn));
1046 static void perror_with_name PROTO((char *));
1047 static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
1048 static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
1050 static void memory_full PROTO((void)) __attribute__ ((noreturn));
1051 GENERIC_PTR xmalloc PROTO((size_t));
1052 static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t));
1053 static GENERIC_PTR xcalloc PROTO((size_t, size_t));
1054 static char *savestring PROTO((char *));
1055 static void print_help PROTO((void));
1057 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1058 retrying if necessary. If MAX_READ_LEN is defined, read at most
1059 that bytes at a time. Return a negative value if an error occurs,
1060 otherwise return the actual number of bytes read,
1061 which must be LEN unless end-of-file was reached. */
1064 safe_read (desc, ptr, len)
1069 int left, rcount, nchars;
1075 if (rcount > MAX_READ_LEN)
1076 rcount = MAX_READ_LEN;
1078 nchars = read (desc, ptr, rcount);
1095 /* Write LEN bytes at PTR to descriptor DESC,
1096 retrying if necessary, and treating any real error as fatal.
1097 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1100 safe_write (desc, ptr, len)
1105 int wcount, written;
1109 #ifdef MAX_WRITE_LEN
1110 if (wcount > MAX_WRITE_LEN)
1111 wcount = MAX_WRITE_LEN;
1113 written = write (desc, ptr, wcount);
1120 pfatal_with_name (out_fname);
1127 /* Print a string to stderr, with extra handling in case it contains
1128 embedded NUL characters. Any present are written as is.
1130 Using fwrite for this purpose produces undesireable results on VMS
1131 when stderr happens to be a record oriented file, such as a batch log
1132 file, rather than a stream oriented one. */
1135 eprint_string (string, length)
1139 size_t segment_length;
1142 fprintf(stderr, "%s", string);
1143 length -= (segment_length = strlen(string));
1146 fputc('\0', stderr);
1148 /* Advance past the portion which has already been printed. */
1149 string += segment_length + 1;
1151 } while (length > 0);
1158 printf ("Usage: %s [switches] input output\n", progname);
1159 printf ("Switches:\n");
1160 printf (" -include <file> Include the contents of <file> before other files\n");
1161 printf (" -imacros <file> Accept definition of marcos in <file>\n");
1162 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
1163 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
1164 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
1165 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
1166 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
1167 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
1168 printf (" -nostdinc Do not search the system include directories\n");
1169 printf (" -nostdinc++ Do not search the system include directories for C++\n");
1170 printf (" -o <file> Put output into <file>\n");
1171 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
1172 printf (" -traditional Follow K&R pre-processor behaviour\n");
1173 printf (" -trigraphs Support ANSI C trigraphs\n");
1174 printf (" -lang-c Assume that the input sources are in C\n");
1175 printf (" -lang-c89 Assume that the input sources are in C89\n");
1176 printf (" -lang-c++ Assume that the input sources are in C++\n");
1177 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
1178 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
1179 printf (" -lang-asm Assume that the input sources are in assembler\n");
1180 printf (" -lang-chill Assume that the input sources are in Chill\n");
1181 printf (" -+ Allow parsing of C++ style features\n");
1182 printf (" -w Inhibit warning messages\n");
1183 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
1184 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
1185 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
1186 printf (" -Wno-comment{s} Do not warn about comments\n");
1187 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
1188 printf (" a string if -tradtional is specified\n");
1189 printf (" -Wno-traditional Do not warn about stringification\n");
1190 printf (" -Wundef Warn if an undefined macro is used by #if\n");
1191 printf (" -Wno-undef Do not warn about testing udefined macros\n");
1192 printf (" -Wimport Warn about the use of the #import directive\n");
1193 printf (" -Wno-import Do not warn about the use of #import\n");
1194 printf (" -Werror Treat all warnings as errors\n");
1195 printf (" -Wno-error Do not treat warnings as errors\n");
1196 printf (" -Wall Enable all preprocessor warnings\n");
1197 printf (" -M Generate make dependencies\n");
1198 printf (" -MM As -M, but ignore system header files\n");
1199 printf (" -MD As -M, but put output in a .d file\n");
1200 printf (" -MMD As -MD, but ignore system header files\n");
1201 printf (" -MG Treat missing header file as generated files\n");
1202 printf (" -g Include #define and #undef directives in the output\n");
1203 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
1204 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
1205 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
1206 printf (" -U<macro> Undefine <macro> \n");
1207 printf (" -u or -undef Do not predefine any macros\n");
1208 printf (" -v Display the version number\n");
1209 printf (" -H Print the name of header files as they are used\n");
1210 printf (" -C Do not discard comments\n");
1211 printf (" -dM Display a list of macro definitions active at end\n");
1212 printf (" -dD Preserve macro definitions in output\n");
1213 printf (" -dN As -dD except that only the names are preserved\n");
1214 printf (" -dI Include #include directives in the output\n");
1215 printf (" -ifoutput Describe skipped code blocks in output \n");
1216 printf (" -P Do not generate #line directives\n");
1217 printf (" -$ Do not include '$' in identifiers\n");
1218 printf (" -remap Remap file names when including files.\n");
1219 printf (" -h or --help Display this information\n");
1232 char **pend_files = (char **) xmalloc (argc * sizeof (char *));
1233 char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
1234 char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
1235 char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
1236 char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
1238 /* Record the option used with each element of pend_assertions.
1239 This is preparation for supporting more than one option for making
1241 char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
1242 int inhibit_predefs = 0;
1243 int no_standard_includes = 0;
1244 int no_standard_cplusplus_includes = 0;
1245 int missing_newline = 0;
1247 /* Non-0 means don't output the preprocessed program. */
1248 int inhibit_output = 0;
1249 /* Non-0 means -v, so print the full set of include dirs. */
1252 /* File name which deps are being written to.
1253 This is 0 if deps are being written to stdout. */
1254 char *deps_file = 0;
1255 /* Fopen file mode to open deps_file with. */
1256 char *deps_mode = "a";
1257 /* Stream on which to print the dependency information. */
1258 FILE *deps_stream = 0;
1259 /* Target-name to write with the dependency information. */
1260 char *deps_target = 0;
1262 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1263 /* Get rid of any avoidable limit on stack size. */
1267 /* Set the stack limit huge so that alloca (particularly stringtab
1268 in dbxread.c) does not fail. */
1269 getrlimit (RLIMIT_STACK, &rlim);
1270 rlim.rlim_cur = rlim.rlim_max;
1271 setrlimit (RLIMIT_STACK, &rlim);
1276 signal (SIGPIPE, pipe_closed);
1279 progname = base_name (argv[0]);
1283 /* Remove extension from PROGNAME. */
1285 char *s = progname = savestring (progname);
1287 if ((p = rindex (s, ';')) != 0) *p = '\0'; /* strip version number */
1288 if ((p = rindex (s, '.')) != 0 /* strip type iff ".exe" */
1289 && (p[1] == 'e' || p[1] == 'E')
1290 && (p[2] == 'x' || p[2] == 'X')
1291 && (p[3] == 'e' || p[3] == 'E')
1300 /* Initialize is_idchar. */
1301 initialize_char_syntax ();
1303 no_line_directives = 0;
1305 dump_macros = dump_none;
1308 cplusplus_comments = 1;
1310 bzero ((char *) pend_files, argc * sizeof (char *));
1311 bzero ((char *) pend_defs, argc * sizeof (char *));
1312 bzero ((char *) pend_undefs, argc * sizeof (char *));
1313 bzero ((char *) pend_assertions, argc * sizeof (char *));
1314 bzero ((char *) pend_includes, argc * sizeof (char *));
1316 #ifdef MULTIBYTE_CHARS
1317 /* Change to the native locale for multibyte conversions. */
1318 setlocale (LC_CTYPE, "");
1319 GET_ENVIRONMENT (literal_codeset, "LANG");
1322 /* Process switches and find input file name. */
1324 for (i = 1; i < argc; i++) {
1325 if (argv[i][0] != '-') {
1326 if (out_fname != NULL)
1329 fatal ("Too many arguments");
1331 else if (in_fname != NULL)
1332 out_fname = argv[i];
1336 switch (argv[i][1]) {
1339 if (!strcmp (argv[i], "-include")) {
1343 fatal ("Filename missing after `-include' option");
1345 simplify_filename (pend_includes[temp] = argv[++i]);
1347 if (!strcmp (argv[i], "-imacros")) {
1351 fatal ("Filename missing after `-imacros' option");
1353 simplify_filename (pend_files[temp] = argv[++i]);
1355 if (!strcmp (argv[i], "-iprefix")) {
1357 fatal ("Filename missing after `-iprefix' option");
1359 include_prefix = argv[++i];
1361 if (!strcmp (argv[i], "-ifoutput")) {
1362 output_conditionals = 1;
1364 if (!strcmp (argv[i], "-isystem")) {
1365 struct file_name_list *dirtmp;
1367 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1370 dirtmp->c_system_include_path = 1;
1372 if (before_system == 0)
1373 before_system = dirtmp;
1375 last_before_system->next = dirtmp;
1376 last_before_system = dirtmp; /* Tail follows the last one */
1378 /* Add directory to end of path for includes,
1379 with the default prefix at the front of its name. */
1380 if (!strcmp (argv[i], "-iwithprefix")) {
1381 struct file_name_list *dirtmp;
1384 if (include_prefix != 0)
1385 prefix = include_prefix;
1387 prefix = savestring (GCC_INCLUDE_DIR);
1388 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1389 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1390 prefix[strlen (prefix) - 7] = 0;
1393 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1394 prefix, argv[++i])))
1397 if (after_include == 0)
1398 after_include = dirtmp;
1400 last_after_include->next = dirtmp;
1401 last_after_include = dirtmp; /* Tail follows the last one */
1403 /* Add directory to main path for includes,
1404 with the default prefix at the front of its name. */
1405 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1406 struct file_name_list *dirtmp;
1409 if (include_prefix != 0)
1410 prefix = include_prefix;
1412 prefix = savestring (GCC_INCLUDE_DIR);
1413 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1414 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1415 prefix[strlen (prefix) - 7] = 0;
1418 dirtmp = new_include_prefix (NULL_PTR, NULL_PTR, prefix, argv[++i]);
1419 append_include_chain (dirtmp, dirtmp);
1421 /* Add directory to end of path for includes. */
1422 if (!strcmp (argv[i], "-idirafter")) {
1423 struct file_name_list *dirtmp;
1425 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1429 if (after_include == 0)
1430 after_include = dirtmp;
1432 last_after_include->next = dirtmp;
1433 last_after_include = dirtmp; /* Tail follows the last one */
1438 if (out_fname != NULL)
1439 fatal ("Output filename specified twice");
1441 fatal ("Filename missing after -o option");
1442 out_fname = argv[++i];
1443 if (!strcmp (out_fname, "-"))
1448 if (!strcmp (argv[i], "-pedantic"))
1450 else if (!strcmp (argv[i], "-pedantic-errors")) {
1452 pedantic_errors = 1;
1453 } else if (!strcmp (argv[i], "-pcp")) {
1456 fatal ("Filename missing after -pcp option");
1457 pcp_fname = argv[++i];
1459 = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1460 ? fopen (pcp_fname, "w")
1462 if (pcp_outfile == 0)
1463 pfatal_with_name (pcp_fname);
1469 if (!strcmp (argv[i], "-traditional")) {
1471 cplusplus_comments = 0;
1472 } else if (!strcmp (argv[i], "-trigraphs")) {
1478 if (! strcmp (argv[i], "-lang-c"))
1479 cplusplus = 0, cplusplus_comments = 1, c89 = 0, objc = 0;
1480 if (! strcmp (argv[i], "-lang-c89"))
1481 cplusplus = 0, cplusplus_comments = 0, c89 = 1, objc = 0;
1482 if (! strcmp (argv[i], "-lang-c++"))
1483 cplusplus = 1, cplusplus_comments = 1, c89 = 0, objc = 0;
1484 if (! strcmp (argv[i], "-lang-objc"))
1485 cplusplus = 0, cplusplus_comments = 1, c89 = 0, objc = 1;
1486 if (! strcmp (argv[i], "-lang-objc++"))
1487 cplusplus = 1, cplusplus_comments = 1, c89 = 0, objc = 1;
1488 if (! strcmp (argv[i], "-lang-asm"))
1490 if (! strcmp (argv[i], "-lint"))
1495 cplusplus = 1, cplusplus_comments = 1;
1499 inhibit_warnings = 1;
1503 if (!strcmp (argv[i], "-Wtrigraphs"))
1505 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1507 else if (!strcmp (argv[i], "-Wcomment"))
1509 else if (!strcmp (argv[i], "-Wno-comment"))
1511 else if (!strcmp (argv[i], "-Wcomments"))
1513 else if (!strcmp (argv[i], "-Wno-comments"))
1515 else if (!strcmp (argv[i], "-Wtraditional"))
1517 else if (!strcmp (argv[i], "-Wno-traditional"))
1519 else if (!strcmp (argv[i], "-Wundef"))
1521 else if (!strcmp (argv[i], "-Wno-undef"))
1523 else if (!strcmp (argv[i], "-Wimport"))
1525 else if (!strcmp (argv[i], "-Wno-import"))
1527 else if (!strcmp (argv[i], "-Werror"))
1528 warnings_are_errors = 1;
1529 else if (!strcmp (argv[i], "-Wno-error"))
1530 warnings_are_errors = 0;
1531 else if (!strcmp (argv[i], "-Wall"))
1539 /* The style of the choices here is a bit mixed.
1540 The chosen scheme is a hybrid of keeping all options in one string
1541 and specifying each option in a separate argument:
1542 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1543 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1544 -M[M][G][D file]. This is awkward to handle in specs, and is not
1546 /* ??? -MG must be specified in addition to one of -M or -MM.
1547 This can be relaxed in the future without breaking anything.
1548 The converse isn't true. */
1550 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1551 if (!strcmp (argv[i], "-MG"))
1553 print_deps_missing_files = 1;
1556 if (!strcmp (argv[i], "-M"))
1558 else if (!strcmp (argv[i], "-MM"))
1560 else if (!strcmp (argv[i], "-MD"))
1562 else if (!strcmp (argv[i], "-MMD"))
1564 /* For -MD and -MMD options, write deps on file named by next arg. */
1565 if (!strcmp (argv[i], "-MD")
1566 || !strcmp (argv[i], "-MMD")) {
1568 fatal ("Filename missing after %s option", argv[i]);
1570 deps_file = argv[i];
1573 /* For -M and -MM, write deps on standard output
1574 and suppress the usual output. */
1575 deps_stream = stdout;
1582 char *p = argv[i] + 2;
1584 while ((c = *p++)) {
1585 /* Arg to -d specifies what parts of macros to dump */
1588 dump_macros = dump_only;
1592 dump_macros = dump_names;
1595 dump_macros = dump_definitions;
1606 if (argv[i][2] == '3')
1611 if (strcmp (argv[i], "--help") != 0)
1618 fprintf (stderr, "GNU CPP version %s", version_string);
1619 #ifdef TARGET_VERSION
1622 fprintf (stderr, "\n");
1627 print_include_names = 1;
1631 if (argv[i][2] != 0)
1632 pend_defs[i] = argv[i] + 2;
1633 else if (i + 1 == argc)
1634 fatal ("Macro name missing after -D option");
1636 i++, pend_defs[i] = argv[i];
1643 if (argv[i][2] != 0)
1645 else if (i + 1 == argc)
1646 fatal ("Assertion missing after -A option");
1650 if (!strcmp (p, "-")) {
1651 /* -A- eliminates all predefined macros and assertions.
1652 Let's include also any that were specified earlier
1653 on the command line. That way we can get rid of any
1654 that were passed automatically in from GCC. */
1656 inhibit_predefs = 1;
1657 for (j = 0; j < i; j++)
1658 pend_defs[j] = pend_assertions[j] = 0;
1660 pend_assertions[i] = p;
1661 pend_assertion_options[i] = "-A";
1666 case 'U': /* JF #undef something */
1667 if (argv[i][2] != 0)
1668 pend_undefs[i] = argv[i] + 2;
1669 else if (i + 1 == argc)
1670 fatal ("Macro name missing after -U option");
1672 pend_undefs[i] = argv[i+1], i++;
1676 put_out_comments = 1;
1679 case 'E': /* -E comes from cc -E; ignore it. */
1683 no_line_directives = 1;
1686 case '$': /* Don't include $ in identifiers. */
1687 is_idchar['$'] = is_idstart['$'] = 0;
1690 case 'I': /* Add directory to path for includes. */
1692 struct file_name_list *dirtmp;
1694 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
1696 /* Don't use any preceding -I directories for #include <...>. */
1697 first_bracket_include = 0;
1700 dirtmp = new_include_prefix (last_include, NULL_PTR, "",
1701 argv[i][2] ? argv[i] + 2 : argv[++i]);
1702 append_include_chain (dirtmp, dirtmp);
1708 if (!strcmp (argv[i], "-nostdinc"))
1709 /* -nostdinc causes no default include directories.
1710 You must specify all include-file directories with -I. */
1711 no_standard_includes = 1;
1712 else if (!strcmp (argv[i], "-nostdinc++"))
1713 /* -nostdinc++ causes no default C++-specific include directories. */
1714 no_standard_cplusplus_includes = 1;
1715 else if (!strcmp (argv[i], "-noprecomp"))
1720 if (!strcmp (argv[i], "-remap"))
1725 /* Sun compiler passes undocumented switch "-undef".
1726 Let's assume it means to inhibit the predefined symbols. */
1727 inhibit_predefs = 1;
1730 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1731 if (in_fname == NULL) {
1734 } else if (out_fname == NULL) {
1737 } /* else fall through into error */
1740 fatal ("Invalid option `%s'", argv[i]);
1745 /* Add dirs from CPATH after dirs from -I. */
1746 /* There seems to be confusion about what CPATH should do,
1747 so for the moment it is not documented. */
1748 /* Some people say that CPATH should replace the standard include dirs,
1749 but that seems pointless: it comes before them, so it overrides them
1751 GET_ENVIRONMENT (cp, "CPATH");
1752 if (cp && ! no_standard_includes)
1755 /* Initialize output buffer */
1757 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1758 outbuf.bufp = outbuf.buf;
1759 outbuf.length = OUTBUF_SIZE;
1761 /* Do partial setup of input buffer for the sake of generating
1762 early #line directives (when -g is in effect). */
1764 fp = &instack[++indepth];
1765 if (in_fname == NULL)
1767 fp->nominal_fname = fp->fname = in_fname;
1768 fp->nominal_fname_len = strlen (in_fname);
1771 /* In C++, wchar_t is a distinct basic type, and we can expect
1772 __wchar_t to be defined by cc1plus. */
1774 wchar_type = "__wchar_t";
1776 /* Install __LINE__, etc. Must follow initialize_char_syntax
1777 and option processing. */
1778 initialize_builtins (fp, &outbuf);
1780 /* Do standard #defines and assertions
1781 that identify system and machine type. */
1783 if (!inhibit_predefs) {
1784 char *p = (char *) alloca (strlen (predefs) + 1);
1787 struct dsc$descriptor_s lcl_name;
1789 unsigned short length; /* input length */
1790 unsigned short code; /* item code */
1791 unsigned long dptr; /* data ptr */
1792 unsigned long lptr; /* output length ptr */
1795 unsigned long syi_length;
1798 struct item_list items[] = {
1799 { 16, SYI$_VERSION, 0, 0 },
1803 items[0].dptr = (unsigned long)syi_data;
1804 items[0].lptr = (unsigned long)(&syi_length);
1806 if (SYS$GETSYIW (0, 0, 0, items, NULL, NULL, NULL, NULL) == SS$_NORMAL)
1808 unsigned long vms_version_value;
1812 vms_version_value = 0;
1816 if (ISDIGIT (*vers))
1818 vms_version_value = (*vers - '0') * 10000000;
1824 if (ISDIGIT (*vers))
1826 vms_version_value += (*vers - '0') * 100000;
1830 if (vms_version_value > 0)
1834 sprintf (versbuf, "__VMS_VER=%08ld", vms_version_value);
1836 output_line_directive (fp, &outbuf, 0, same_file);
1837 make_definition (versbuf, &outbuf);
1842 strcpy (p, predefs);
1845 while (*p == ' ' || *p == '\t')
1847 /* Handle -D options. */
1848 if (p[0] == '-' && p[1] == 'D') {
1850 while (*p && *p != ' ' && *p != '\t')
1855 output_line_directive (fp, &outbuf, 0, same_file);
1856 make_definition (q, &outbuf);
1857 while (*p == ' ' || *p == '\t')
1859 } else if (p[0] == '-' && p[1] == 'A') {
1860 /* Handle -A options (assertions). */
1869 past_name = assertion;
1870 /* Locate end of name. */
1871 while (*past_name && *past_name != ' '
1872 && *past_name != '\t' && *past_name != '(')
1874 /* Locate `(' at start of value. */
1876 while (*value && (*value == ' ' || *value == '\t'))
1878 if (*value++ != '(')
1880 while (*value && (*value == ' ' || *value == '\t'))
1883 /* Locate end of value. */
1884 while (*past_value && *past_value != ' '
1885 && *past_value != '\t' && *past_value != ')')
1887 termination = past_value;
1888 while (*termination && (*termination == ' ' || *termination == '\t'))
1890 if (*termination++ != ')')
1892 if (*termination && *termination != ' ' && *termination != '\t')
1894 /* Temporarily null-terminate the value. */
1895 save_char = *termination;
1896 *termination = '\0';
1897 /* Install the assertion. */
1898 make_assertion ("-A", assertion);
1899 *termination = (char) save_char;
1901 while (*p == ' ' || *p == '\t')
1909 /* Now handle the command line options. */
1911 /* Do -U's, -D's and -A's in the order they were seen. */
1912 for (i = 1; i < argc; i++) {
1913 if (pend_undefs[i]) {
1915 output_line_directive (fp, &outbuf, 0, same_file);
1916 make_undef (pend_undefs[i], &outbuf);
1920 output_line_directive (fp, &outbuf, 0, same_file);
1921 make_definition (pend_defs[i], &outbuf);
1923 if (pend_assertions[i])
1924 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1927 done_initializing = 1;
1929 { /* Read the appropriate environment variable and if it exists
1930 replace include_defaults with the listed path. */
1932 switch ((objc << 1) + cplusplus)
1935 GET_ENVIRONMENT (epath, "C_INCLUDE_PATH");
1938 GET_ENVIRONMENT (epath, "CPLUS_INCLUDE_PATH");
1941 GET_ENVIRONMENT (epath, "OBJC_INCLUDE_PATH");
1944 GET_ENVIRONMENT (epath, "OBJCPLUS_INCLUDE_PATH");
1947 /* If the environment var for this language is set,
1948 add to the default list of include directories. */
1951 char *startp, *endp;
1953 for (num_dirs = 1, startp = epath; *startp; startp++)
1954 if (*startp == PATH_SEPARATOR)
1957 = (struct default_include *) xmalloc ((num_dirs
1958 * sizeof (struct default_include))
1959 + sizeof (include_defaults_array));
1960 startp = endp = epath;
1964 if (c == PATH_SEPARATOR || !c) {
1966 include_defaults[num_dirs].fname
1967 = startp == endp ? "." : savestring (startp);
1969 include_defaults[num_dirs].component = 0;
1970 include_defaults[num_dirs].cplusplus = cplusplus;
1971 include_defaults[num_dirs].cxx_aware = 1;
1978 /* Put the usual defaults back in at the end. */
1979 bcopy ((char *) include_defaults_array,
1980 (char *) &include_defaults[num_dirs],
1981 sizeof (include_defaults_array));
1985 append_include_chain (before_system, last_before_system);
1986 first_system_include = before_system;
1988 /* Unless -fnostdinc,
1989 tack on the standard include file dirs to the specified list */
1990 if (!no_standard_includes) {
1991 struct default_include *p = include_defaults;
1992 char *specd_prefix = include_prefix;
1993 char *default_prefix = savestring (GCC_INCLUDE_DIR);
1994 int default_len = 0;
1995 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1996 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1997 default_len = strlen (default_prefix) - 7;
1998 default_prefix[default_len] = 0;
2000 /* Search "translated" versions of GNU directories.
2001 These have /usr/local/lib/gcc... replaced by specd_prefix. */
2002 if (specd_prefix != 0 && default_len != 0)
2003 for (p = include_defaults; p->fname; p++) {
2004 /* Some standard dirs are only for C++. */
2005 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
2006 /* Does this dir start with the prefix? */
2007 if (!strncmp (p->fname, default_prefix, default_len)) {
2008 /* Yes; change prefix and add to search list. */
2009 struct file_name_list *new
2010 = new_include_prefix (NULL_PTR, NULL_PTR, specd_prefix,
2011 p->fname + default_len);
2013 new->c_system_include_path = !p->cxx_aware;
2014 append_include_chain (new, new);
2015 if (first_system_include == 0)
2016 first_system_include = new;
2021 /* Search ordinary names for GNU include directories. */
2022 for (p = include_defaults; p->fname; p++) {
2023 /* Some standard dirs are only for C++. */
2024 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
2025 struct file_name_list *new
2026 = new_include_prefix (NULL_PTR, p->component, "", p->fname);
2028 new->c_system_include_path = !p->cxx_aware;
2029 append_include_chain (new, new);
2030 if (first_system_include == 0)
2031 first_system_include = new;
2037 /* Tack the after_include chain at the end of the include chain. */
2038 append_include_chain (after_include, last_after_include);
2039 if (first_system_include == 0)
2040 first_system_include = after_include;
2042 /* With -v, print the list of dirs to search. */
2044 struct file_name_list *p;
2045 fprintf (stderr, "#include \"...\" search starts here:\n");
2046 for (p = include; p; p = p->next) {
2047 if (p == first_bracket_include)
2048 fprintf (stderr, "#include <...> search starts here:\n");
2050 fprintf (stderr, " .\n");
2051 else if (!strcmp (p->fname, "/") || !strcmp (p->fname, "//"))
2052 fprintf (stderr, " %s\n", p->fname);
2054 /* Omit trailing '/'. */
2055 fprintf (stderr, " %.*s\n", (int) strlen (p->fname) - 1, p->fname);
2057 fprintf (stderr, "End of search list.\n");
2060 /* -MG doesn't select the form of output and must be specified with one of
2061 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
2062 inhibit compilation. */
2063 if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
2064 fatal ("-MG must be specified with one of -M or -MM");
2066 /* Either of two environment variables can specify output of deps.
2067 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
2068 where OUTPUT_FILE is the file to write deps info to
2069 and DEPS_TARGET is the target to mention in the deps. */
2072 && (getenv ("SUNPRO_DEPENDENCIES") != 0
2073 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
2074 char *spec = getenv ("DEPENDENCIES_OUTPUT");
2079 spec = getenv ("SUNPRO_DEPENDENCIES");
2086 /* Find the space before the DEPS_TARGET, if there is one. */
2087 /* This should use index. (mrs) */
2088 while (*s != 0 && *s != ' ') s++;
2090 deps_target = s + 1;
2091 output_file = xmalloc (s - spec + 1);
2092 bcopy (spec, output_file, s - spec);
2093 output_file[s - spec] = 0;
2100 deps_file = output_file;
2104 /* For -M, print the expected object file name
2105 as the target of this Make-rule. */
2107 deps_allocated_size = 200;
2108 deps_buffer = xmalloc (deps_allocated_size);
2114 deps_output (deps_target, ':');
2115 } else if (*in_fname == 0) {
2116 deps_output ("-", ':');
2121 q = base_name (in_fname);
2123 /* Copy remainder to mungable area. */
2124 p = (char *) alloca (strlen(q) + 8);
2127 /* Output P, but remove known suffixes. */
2131 && p[len - 2] == '.'
2132 && index("cCsSm", p[len - 1]))
2135 && p[len - 3] == '.'
2136 && p[len - 2] == 'c'
2137 && p[len - 1] == 'c')
2140 && p[len - 4] == '.'
2141 && p[len - 3] == 'c'
2142 && p[len - 2] == 'x'
2143 && p[len - 1] == 'x')
2146 && p[len - 4] == '.'
2147 && p[len - 3] == 'c'
2148 && p[len - 2] == 'p'
2149 && p[len - 1] == 'p')
2152 /* Supply our own suffix. */
2153 strcpy (q, OBJECT_SUFFIX);
2155 deps_output (p, ':');
2156 deps_output (in_fname, ' ');
2160 /* Scan the -imacros files before the main input.
2161 Much like #including them, but with no_output set
2162 so that only their macro definitions matter. */
2164 no_output++; no_record_file++;
2165 for (i = 1; i < argc; i++)
2166 if (pend_files[i]) {
2167 struct include_file *inc;
2168 int fd = open_include_file (pend_files[i], NULL_PTR, NULL_PTR, &inc);
2170 perror_with_name (pend_files[i]);
2171 return FATAL_EXIT_CODE;
2173 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2175 no_output--; no_record_file--;
2177 /* Copy the entire contents of the main input file into
2178 the stacked input buffer previously allocated for it. */
2180 /* JF check for stdin */
2181 if (in_fname == NULL || *in_fname == 0) {
2184 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
2187 if (fstat (f, &st) != 0)
2188 pfatal_with_name (in_fname);
2189 fp->nominal_fname = fp->fname = in_fname;
2190 fp->nominal_fname_len = strlen (in_fname);
2192 fp->system_header_p = 0;
2193 /* JF all this is mine about reading pipes and ttys */
2194 if (! S_ISREG (st.st_mode)) {
2195 /* Read input from a file that is not a normal disk file.
2196 We cannot preallocate a buffer with the correct size,
2197 so we must read in the file a piece at the time and make it bigger. */
2202 if (S_ISDIR (st.st_mode))
2203 fatal ("Input file `%s' is a directory", in_fname);
2207 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2209 cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
2210 if (cnt < 0) goto perror; /* error! */
2212 if (size != bsize) break; /* End of file */
2214 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2218 /* Read a file whose size we can determine in advance.
2219 For the sake of VMS, st.st_size is just an upper bound. */
2220 size_t s = (size_t) st.st_size;
2221 if (s != st.st_size || s + 2 < s)
2223 fp->buf = (U_CHAR *) xmalloc (s + 2);
2224 fp->length = safe_read (f, (char *) fp->buf, s);
2225 if (fp->length < 0) goto perror;
2228 fp->if_stack = if_stack;
2230 /* Make sure data ends with a newline. And put a null after it. */
2232 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
2233 /* Backslash-newline at end is not good enough. */
2234 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
2235 fp->buf[fp->length++] = '\n';
2236 missing_newline = 1;
2238 fp->buf[fp->length] = '\0';
2240 /* Unless inhibited, convert trigraphs in the input. */
2245 /* Now that we know the input file is valid, open the output. */
2247 if (!out_fname || !strcmp (out_fname, ""))
2248 out_fname = "stdout";
2249 else if (! freopen (out_fname, "w", stdout))
2250 pfatal_with_name (out_fname);
2252 output_line_directive (fp, &outbuf, 0, same_file);
2254 /* Scan the -include files before the main input. */
2257 for (i = 1; i < argc; i++)
2258 if (pend_includes[i]) {
2259 struct include_file *inc;
2260 int fd = open_include_file (pend_includes[i], NULL_PTR, NULL_PTR, &inc);
2262 perror_with_name (pend_includes[i]);
2263 return FATAL_EXIT_CODE;
2265 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2269 /* Scan the input, processing macros and directives. */
2271 rescan (&outbuf, 0);
2273 if (missing_newline)
2276 if (pedantic && missing_newline)
2277 pedwarn ("file does not end in newline");
2279 /* Now we have processed the entire input
2280 Write whichever kind of output has been requested. */
2282 if (dump_macros == dump_only)
2284 else if (! inhibit_output) {
2289 /* Don't actually write the deps file if compilation has failed. */
2291 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
2292 pfatal_with_name (deps_file);
2293 fputs (deps_buffer, deps_stream);
2294 putc ('\n', deps_stream);
2296 if (ferror (deps_stream) || fclose (deps_stream) != 0)
2297 fatal ("I/O error on output");
2302 if (pcp_outfile && pcp_outfile != stdout
2303 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
2304 fatal ("I/O error on `-pcp' output");
2306 if (ferror (stdout) || fclose (stdout) != 0)
2307 fatal ("I/O error on output");
2310 exit (FATAL_EXIT_CODE);
2311 exit (SUCCESS_EXIT_CODE);
2314 pfatal_with_name (in_fname);
2318 /* Given a colon-separated list of file names PATH,
2319 add all the names to the search path for include files. */
2333 struct file_name_list *dirtmp;
2335 /* Find the end of this name. */
2336 while ((c = *q++) != PATH_SEPARATOR && c)
2340 dirtmp = new_include_prefix (last_include, NULL_PTR,
2341 "", p == q ? "." : p);
2343 append_include_chain (dirtmp, dirtmp);
2345 /* Advance past this name. */
2352 /* Return the address of the first character in S that equals C.
2353 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2354 Return 0 if there is no such character. Assume that C itself is not '\0'.
2355 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2356 but unfortunately memchr isn't autoconfigured yet. */
2364 char *p = (char *) s;
2366 char *q = index (p, c);
2368 return (U_CHAR *) q;
2370 size_t l = strlen (p);
2380 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2381 before main CCCP processing. Name `pcp' is also in honor of the
2382 drugs the trigraph designers must have been on.
2384 Using an extra pass through the buffer takes a little extra time,
2385 but is infinitely less hairy than trying to handle trigraphs inside
2386 strings, etc. everywhere, and also makes sure that trigraphs are
2387 only translated in the top level of processing. */
2393 register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
2396 fptr = bptr = sptr = buf->buf;
2397 lptr = fptr + buf->length;
2398 while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
2435 len = sptr - fptr - 2;
2437 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2438 C, this will be memmove (). */
2439 if (bptr != fptr && len > 0)
2440 bcopy ((char *) fptr, (char *) bptr, len);
2446 len = buf->length - (fptr - buf->buf);
2447 if (bptr != fptr && len > 0)
2448 bcopy ((char *) fptr, (char *) bptr, len);
2449 buf->length -= fptr - bptr;
2450 buf->buf[buf->length] = '\0';
2451 if (warn_trigraphs && fptr != bptr)
2452 warning_with_line (0, "%lu trigraph(s) encountered",
2453 (unsigned long) (fptr - bptr) / 2);
2456 /* Move all backslash-newline pairs out of embarrassing places.
2457 Exchange all such pairs following BP
2458 with any potentially-embarrassing characters that follow them.
2459 Potentially-embarrassing characters are / and *
2460 (because a backslash-newline inside a comment delimiter
2461 would cause it not to be recognized). */
2467 register U_CHAR *p = bp;
2469 /* First count the backslash-newline pairs here. */
2471 while (p[0] == '\\' && p[1] == '\n')
2474 /* What follows the backslash-newlines is not embarrassing. */
2476 if (*p != '/' && *p != '*')
2479 /* Copy all potentially embarrassing characters
2480 that follow the backslash-newline pairs
2481 down to where the pairs originally started. */
2483 while (*p == '*' || *p == '/')
2486 /* Now write the same number of pairs after the embarrassing chars. */
2493 /* Like newline_fix but for use within a directive-name.
2494 Move any backslash-newlines up past any following symbol constituents. */
2497 name_newline_fix (bp)
2500 register U_CHAR *p = bp;
2502 /* First count the backslash-newline pairs here. */
2503 while (p[0] == '\\' && p[1] == '\n')
2506 /* What follows the backslash-newlines is not embarrassing. */
2511 /* Copy all potentially embarrassing characters
2512 that follow the backslash-newline pairs
2513 down to where the pairs originally started. */
2515 while (is_idchar[*p])
2518 /* Now write the same number of pairs after the embarrassing chars. */
2525 /* Look for lint commands in comments.
2527 When we come in here, ibp points into a comment. Limit is as one expects.
2528 scan within the comment -- it should start, after lwsp, with a lint command.
2529 If so that command is returned as a (constant) string.
2531 Upon return, any arg will be pointed to with argstart and will be
2532 arglen long. Note that we don't parse that arg since it will just
2533 be printed out again. */
2536 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2537 register U_CHAR *ibp;
2538 register U_CHAR *limit;
2539 U_CHAR **argstart; /* point to command arg */
2540 int *arglen, *cmdlen; /* how long they are */
2542 HOST_WIDE_INT linsize;
2543 register U_CHAR *numptr; /* temp for arg parsing */
2547 SKIP_WHITE_SPACE (ibp);
2549 if (ibp >= limit) return NULL;
2551 linsize = limit - ibp;
2553 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2554 if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
2556 return "NOTREACHED";
2558 if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
2562 if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
2564 return "LINTLIBRARY";
2566 if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
2568 ibp += 7; linsize -= 7;
2569 if ((linsize == 0) || ! ISDIGIT (*ibp)) return "VARARGS";
2571 /* OK, read a number */
2572 for (numptr = *argstart = ibp; (numptr < limit) && ISDIGIT (*numptr);
2574 *arglen = numptr - *argstart;
2581 * The main loop of the program.
2583 * Read characters from the input stack, transferring them to the
2586 * Macros are expanded and push levels on the input stack.
2587 * At the end of such a level it is popped off and we keep reading.
2588 * At the end of any other kind of level, we return.
2589 * #-directives are handled, except within macros.
2591 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2592 * and insert them when appropriate. This is set while scanning macro
2593 * arguments before substitution. It is zero when scanning for final output.
2594 * There are three types of Newline markers:
2595 * * Newline - follows a macro name that was not expanded
2596 * because it appeared inside an expansion of the same macro.
2597 * This marker prevents future expansion of that identifier.
2598 * When the input is rescanned into the final output, these are deleted.
2599 * These are also deleted by ## concatenation.
2600 * * Newline Space (or Newline and any other whitespace character)
2601 * stands for a place that tokens must be separated or whitespace
2602 * is otherwise desirable, but where the ANSI standard specifies there
2603 * is no whitespace. This marker turns into a Space (or whichever other
2604 * whitespace char appears in the marker) in the final output,
2605 * but it turns into nothing in an argument that is stringified with #.
2606 * Such stringified arguments are the only place where the ANSI standard
2607 * specifies with precision that whitespace may not appear.
2609 * During this function, IP->bufp is kept cached in IBP for speed of access.
2610 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2611 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2612 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2613 * explicitly, and before RECACHE, since RECACHE uses OBP.
2617 rescan (op, output_marks)
2621 /* Character being scanned in main loop. */
2624 /* Length of pending accumulated identifier. */
2625 register int ident_length = 0;
2627 /* Hash code of pending accumulated identifier. */
2628 register int hash = 0;
2630 /* Current input level (&instack[indepth]). */
2633 /* Pointer for scanning input. */
2634 register U_CHAR *ibp;
2636 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2637 register U_CHAR *limit;
2639 /* Pointer for storing output. */
2640 register U_CHAR *obp;
2642 /* REDO_CHAR is nonzero if we are processing an identifier
2643 after backing up over the terminating character.
2644 Sometimes we process an identifier without backing up over
2645 the terminating character, if the terminating character
2646 is not special. Backing up is done so that the terminating character
2647 will be dispatched on again once the identifier is dealt with. */
2650 /* 1 if within an identifier inside of which a concatenation
2651 marker (Newline -) has been seen. */
2652 int concatenated = 0;
2654 /* While scanning a comment or a string constant,
2655 this records the line it started on, for error messages. */
2658 /* Record position of last `real' newline. */
2659 U_CHAR *beg_of_line;
2661 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2664 do { ip->macro->type = T_MACRO; \
2665 if (ip->free_ptr) free (ip->free_ptr); \
2666 --indepth; } while (0)
2668 /* Reload `rescan's local variables that describe the current
2669 level of the input stack. */
2672 do { ip = &instack[indepth]; \
2674 limit = ip->buf + ip->length; \
2676 check_expand (op, limit - ibp); \
2678 obp = op->bufp; } while (0)
2680 if (no_output && instack[indepth].fname != 0)
2681 skip_if_group (&instack[indepth], 1, NULL);
2688 /* Our caller must always put a null after the end of
2689 the input at each input stack level. */
2699 if (*ibp == '\n' && !ip->macro) {
2700 /* At the top level, always merge lines ending with backslash-newline,
2701 even in middle of identifier. But do not merge lines in a macro,
2702 since backslash might be followed by a newline-space marker. */
2705 --obp; /* remove backslash from obuf */
2708 /* If ANSI, backslash is just another character outside a string. */
2711 /* Otherwise, backslash suppresses specialness of following char,
2712 so copy it here to prevent the switch from seeing it.
2713 But first get any pending identifier processed. */
2714 if (ident_length > 0)
2721 if (ident_length || ip->macro || traditional)
2723 while (*ibp == '\\' && ibp[1] == '\n') {
2729 /* Treat this %: digraph as if it were #. */
2733 if (assertions_flag) {
2736 /* Copy #foo (bar lose) without macro expansion. */
2737 obp[-1] = '#'; /* In case it was '%'. */
2738 SKIP_WHITE_SPACE (ibp);
2739 while (is_idchar[*ibp])
2741 SKIP_WHITE_SPACE (ibp);
2744 skip_paren_group (ip);
2745 bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
2746 obp += ip->bufp - ibp;
2752 /* If this is expanding a macro definition, don't recognize
2753 preprocessing directives. */
2756 /* If this is expand_into_temp_buffer,
2757 don't recognize them either. Warn about them
2758 only after an actual newline at this level,
2759 not at the beginning of the input level. */
2761 if (ip->buf != beg_of_line)
2762 warning ("preprocessing directive not recognized within macro arg");
2769 /* # keyword: a # must be first nonblank char on the line */
2770 if (beg_of_line == 0)
2775 /* Scan from start of line, skipping whitespace, comments
2776 and backslash-newlines, and see if we reach this #.
2777 If not, this # is not special. */
2779 /* If -traditional, require # to be at beginning of line. */
2782 if (is_hor_space[*bp])
2784 else if (*bp == '\\' && bp[1] == '\n')
2786 else if (*bp == '/' && bp[1] == '*') {
2800 #ifdef MULTIBYTE_CHARS
2802 length = local_mblen (bp, limit - bp);
2810 /* There is no point in trying to deal with C++ // comments here,
2811 because if there is one, then this # must be part of the
2812 comment and we would never reach here. */
2818 while (bp[1] == '\\' && bp[2] == '\n')
2822 /* %: appears at start of line; skip past the ':' too. */
2831 /* This # can start a directive. */
2833 --obp; /* Don't copy the '#' */
2837 if (! handle_directive (ip, op)) {
2841 /* Not a known directive: treat it as ordinary text.
2842 IP, OP, IBP, etc. have not been changed. */
2843 if (no_output && instack[indepth].fname) {
2844 /* If not generating expanded output,
2845 what we do with ordinary text is skip it.
2846 Discard everything until next # directive. */
2847 skip_if_group (&instack[indepth], 1, 0);
2852 *obp++ = '#'; /* Copy # (even if it was originally %:). */
2853 /* Don't expand an identifier that could be a macro directive.
2854 (Section 3.8.3 of the ANSI C standard) */
2855 SKIP_WHITE_SPACE (ibp);
2856 if (is_idstart[*ibp])
2859 while (is_idchar[*ibp])
2867 /* A # directive has been successfully processed. */
2868 /* If not generating expanded output, ignore everything until
2869 next # directive. */
2870 if (no_output && instack[indepth].fname)
2871 skip_if_group (&instack[indepth], 1, 0);
2877 case '\"': /* skip quoted string */
2879 /* A single quoted string is treated like a double -- some
2880 programs (e.g., troff) are perverse this way */
2882 /* Handle any pending identifier;
2883 but the L in L'...' or L"..." is not an identifier. */
2885 if (! (ident_length == 1 && hash == HASHSTEP (0, 'L')))
2887 ident_length = hash = 0;
2890 start_line = ip->lineno;
2892 /* Skip ahead to a matching quote. */
2896 if (ip->macro != 0) {
2897 /* try harder: this string crosses a macro expansion boundary.
2898 This can happen naturally if -traditional.
2899 Otherwise, only -D can make a macro with an unmatched quote. */
2905 error_with_line (line_for_error (start_line),
2906 "unterminated string or character constant");
2907 if (multiline_string_line) {
2908 error_with_line (multiline_string_line,
2909 "possible real start of unterminated constant");
2910 multiline_string_line = 0;
2920 /* Traditionally, end of line ends a string constant with no error.
2921 So exit the loop and record the new line. */
2927 error_with_line (line_for_error (start_line),
2928 "unterminated character constant");
2931 if (multiline_string_line == 0) {
2933 pedwarn_with_line (line_for_error (start_line),
2934 "string constant runs past end of line");
2935 multiline_string_line = ip->lineno - 1;
2941 /* Backslash newline is replaced by nothing at all, but
2942 keep the line counts correct. But if we are reading
2943 from a macro, keep the backslash newline, since backslash
2944 newlines have already been processed. */
2952 /* ANSI stupidly requires that in \\ the second \
2953 is *not* prevented from combining with a newline. */
2955 while (*ibp == '\\' && ibp[1] == '\n') {
2969 #ifdef MULTIBYTE_CHARS
2974 length = local_mblen (ibp, limit - ibp);
2978 bcopy (ibp, obp, length);
2995 if (*ibp == '\\' && ibp[1] == '\n')
2998 && !(cplusplus_comments && *ibp == '/'))
3004 /* C++ style comment... */
3005 start_line = ip->lineno;
3007 /* Comments are equivalent to spaces. */
3008 if (! put_out_comments)
3012 U_CHAR *before_bp = ibp;
3014 while (++ibp < limit) {
3016 if (ibp[-1] != '\\') {
3017 if (put_out_comments) {
3018 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
3019 obp += ibp - before_bp;
3024 warning ("multiline `//' comment");
3026 /* Copy the newline into the output buffer, in order to
3027 avoid the pain of a #line every time a multiline comment
3029 if (!put_out_comments)
3035 #ifdef MULTIBYTE_CHARS
3037 length = local_mblen (ibp, limit - ibp);
3039 ibp += (length - 1);
3047 /* Ordinary C comment. Skip it, optionally copying it to output. */
3049 start_line = ip->lineno;
3051 ++ibp; /* Skip the star. */
3053 /* If this cpp is for lint, we peek inside the comments: */
3057 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
3059 if (lintcmd != NULL) {
3061 check_expand (op, cmdlen + arglen + 14);
3063 /* I believe it is always safe to emit this newline: */
3065 bcopy ("#pragma lint ", (char *) obp, 13);
3067 bcopy (lintcmd, (char *) obp, cmdlen);
3072 bcopy (argbp, (char *) obp, arglen);
3076 /* OK, now bring us back to the state we were in before we entered
3077 this branch. We need #line because the #pragma's newline always
3078 messes up the line count. */
3080 output_line_directive (ip, op, 0, same_file);
3081 check_expand (op, limit - ibp + 2);
3087 /* Comments are equivalent to spaces.
3088 Note that we already output the slash; we might not want it.
3089 For -traditional, a comment is equivalent to nothing. */
3090 if (! put_out_comments) {
3100 U_CHAR *before_bp = ibp;
3105 if (ibp[-2] == '/' && warn_comments)
3106 warning ("`/*' within comment");
3107 if (*ibp == '\\' && ibp[1] == '\n')
3115 /* Copy the newline into the output buffer, in order to
3116 avoid the pain of a #line every time a multiline comment
3118 if (!put_out_comments)
3125 error_with_line (line_for_error (start_line),
3126 "unterminated comment");
3130 #ifdef MULTIBYTE_CHARS
3134 length = local_mblen (ibp, limit - ibp);
3136 ibp += (length - 1);
3145 if (put_out_comments) {
3146 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
3147 obp += ibp - before_bp;
3153 if (! is_idchar['$'])
3156 pedwarn ("`$' in identifier");
3159 case '0': case '1': case '2': case '3': case '4':
3160 case '5': case '6': case '7': case '8': case '9':
3161 /* If digit is not part of identifier, it starts a number,
3162 which means that following letters are not an identifier.
3163 "0x5" does not refer to an identifier "x5".
3164 So copy all alphanumerics that follow without accumulating
3165 as an identifier. Periods also, for sake of "3.e7". */
3167 if (ident_length == 0) {
3170 while (ibp[0] == '\\' && ibp[1] == '\n') {
3176 if (!is_idchar[c] && c != '.') {
3181 /* A sign can be part of a preprocessing number
3182 if it follows an `e' or `p'. */
3183 if (c == 'e' || c == 'E' || c == 'p' || c == 'P') {
3185 while (ibp[0] == '\\' && ibp[1] == '\n') {
3190 if (*ibp == '+' || *ibp == '-') {
3192 /* But traditional C does not let the token go past the sign,
3193 and C89 does not allow `p'. */
3194 if (traditional || (c89 && (c == 'p' || c == 'P')))
3204 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3205 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3206 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3207 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3209 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3210 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3211 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3212 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3216 /* Compute step of hash function, to avoid a proc call on every token */
3217 hash = HASHSTEP (hash, c);
3221 if (ip->fname == 0 && *ibp == '-') {
3222 /* Newline - inhibits expansion of preceding token.
3223 If expanding a macro arg, we keep the newline -.
3224 In final output, it is deleted.
3225 We recognize Newline - in macro bodies and macro args. */
3226 if (! concatenated) {
3231 if (!output_marks) {
3234 /* If expanding a macro arg, keep the newline -. */
3240 /* If reprocessing a macro expansion, newline is a special marker. */
3241 else if (ip->macro != 0) {
3242 /* Newline White is a "funny space" to separate tokens that are
3243 supposed to be separate but without space between.
3244 Here White means any whitespace character.
3245 Newline - marks a recursive macro use that is not
3246 supposed to be expandable. */
3248 if (is_space[*ibp]) {
3249 /* Newline Space does not prevent expansion of preceding token
3250 so expand the preceding token and then come back. */
3251 if (ident_length > 0)
3254 /* If generating final output, newline space makes a space. */
3255 if (!output_marks) {
3257 /* And Newline Newline makes a newline, so count it. */
3258 if (obp[-1] == '\n')
3261 /* If expanding a macro arg, keep the newline space.
3262 If the arg gets stringified, newline space makes nothing. */
3265 } else abort (); /* Newline followed by something random? */
3269 /* If there is a pending identifier, handle it and come back here. */
3270 if (ident_length > 0)
3275 /* Update the line counts and output a #line if necessary. */
3278 if (ip->lineno != op->lineno) {
3280 output_line_directive (ip, op, 1, same_file);
3281 check_expand (op, limit - ibp);
3286 /* Come here either after (1) a null character that is part of the input
3287 or (2) at the end of the input, because there is a null there. */
3290 /* Our input really contains a null character. */
3294 /* At end of a macro-expansion level, pop it and read next level. */
3295 if (ip->macro != 0) {
3298 /* If traditional, and we have an identifier that ends here,
3299 process it now, so we get the right error for recursion. */
3300 if (traditional && ident_length
3301 && ! is_idchar[*instack[indepth - 1].bufp]) {
3310 /* If we don't have a pending identifier,
3311 return at end of input. */
3312 if (ident_length == 0) {
3320 /* If we do have a pending identifier, just consider this null
3321 a special character and arrange to dispatch on it again.
3322 The second time, IDENT_LENGTH will be zero so we will return. */
3328 /* Handle the case of a character such as /, ', " or null
3329 seen following an identifier. Back over it so that
3330 after the identifier is processed the special char
3331 will be dispatched on again. */
3341 if (ident_length > 0) {
3342 register HASHNODE *hp;
3344 /* We have just seen an identifier end. If it's a macro, expand it.
3346 IDENT_LENGTH is the length of the identifier
3347 and HASH is its hash code.
3349 The identifier has already been copied to the output,
3350 so if it is a macro we must remove it.
3352 If REDO_CHAR is 0, the char that terminated the identifier
3353 has been skipped in the output and the input.
3354 OBP-IDENT_LENGTH-1 points to the identifier.
3355 If the identifier is a macro, we must back over the terminator.
3357 If REDO_CHAR is 1, the terminating char has already been
3358 backed over. OBP-IDENT_LENGTH points to the identifier. */
3360 if (!pcp_outfile || pcp_inside_if) {
3361 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
3364 if (hp->length == ident_length) {
3365 int obufp_before_macroname;
3366 int op_lineno_before_macroname;
3367 register int i = ident_length;
3368 register U_CHAR *p = hp->name;
3369 register U_CHAR *q = obp - i;
3375 do { /* All this to avoid a strncmp () */
3380 /* We found a use of a macro name.
3381 see if the context shows it is a macro call. */
3383 /* Back up over terminating character if not already done. */
3389 /* Save this as a displacement from the beginning of the output
3390 buffer. We can not save this as a position in the output
3391 buffer, because it may get realloc'ed by RECACHE. */
3392 obufp_before_macroname = (obp - op->buf) - ident_length;
3393 op_lineno_before_macroname = op->lineno;
3395 if (hp->type == T_PCSTRING) {
3396 pcstring_used (hp); /* Mark the definition of this key
3397 as needed, ensuring that it
3399 break; /* Exit loop, since the key cannot have a
3400 definition any longer. */
3403 /* Record whether the macro is disabled. */
3404 disabled = hp->type == T_DISABLED;
3406 /* This looks like a macro ref, but if the macro was disabled,
3407 just copy its name and put in a marker if requested. */
3411 /* This error check caught useful cases such as
3412 #define foo(x,y) bar (x (y,0), y)
3415 error ("recursive use of macro `%s'", hp->name);
3419 check_expand (op, limit - ibp + 2);
3426 /* If macro wants an arglist, verify that a '(' follows.
3427 first skip all whitespace, copying it to the output
3428 after the macro name. Then, if there is no '(',
3429 decide this is not a macro call and leave things that way. */
3430 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3431 && hp->value.defn->nargs >= 0)
3433 U_CHAR *old_ibp = ibp;
3434 U_CHAR *old_obp = obp;
3435 int old_iln = ip->lineno;
3436 int old_oln = op->lineno;
3439 /* Scan forward over whitespace, copying it to the output. */
3440 if (ibp == limit && ip->macro != 0) {
3445 old_iln = ip->lineno;
3446 old_oln = op->lineno;
3448 else if (is_space[*ibp]) {
3450 if (ibp[-1] == '\n') {
3451 if (ip->macro == 0) {
3452 /* Newline in a file. Count it. */
3455 } else if (!output_marks) {
3456 /* A newline mark, and we don't want marks
3457 in the output. If it is newline-hyphen,
3458 discard it entirely. Otherwise, it is
3459 newline-whitechar, so keep the whitechar. */
3469 /* A newline mark; copy both chars to the output. */
3476 else if (*ibp == '/') {
3477 /* If a comment, copy it unchanged or discard it. */
3478 if (ibp[1] == '\\' && ibp[2] == '\n')
3479 newline_fix (ibp + 1);
3480 if (ibp[1] == '*') {
3481 if (put_out_comments) {
3484 } else if (! traditional) {
3487 for (ibp += 2; ibp < limit; ibp++) {
3488 /* We need not worry about newline-marks,
3489 since they are never found in comments. */
3490 if (ibp[0] == '*') {
3491 if (ibp[1] == '\\' && ibp[2] == '\n')
3492 newline_fix (ibp + 1);
3493 if (ibp[1] == '/') {
3495 if (put_out_comments) {
3502 else if (*ibp == '\n') {
3503 /* Newline in a file. Count it. */
3509 #ifdef MULTIBYTE_CHARS
3511 length = local_mblen (ibp, limit - ibp);
3514 if (put_out_comments)
3516 bcopy (ibp, obp, length - 1);
3519 ibp += (length - 1);
3523 if (put_out_comments)
3526 } else if (ibp[1] == '/' && cplusplus_comments) {
3527 if (put_out_comments) {
3530 } else if (! traditional) {
3533 for (ibp += 2; ; ibp++)
3537 if (ibp[-1] != '\\')
3542 #ifdef MULTIBYTE_CHARS
3544 length = local_mblen (ibp, limit - ibp);
3547 if (put_out_comments)
3549 bcopy (ibp, obp, length - 1);
3552 ibp += (length - 1);
3556 if (put_out_comments)
3562 else if (ibp[0] == '\\' && ibp[1] == '\n') {
3569 /* It isn't a macro call.
3570 Put back the space that we just skipped. */
3573 ip->lineno = old_iln;
3574 op->lineno = old_oln;
3575 /* Exit the for loop. */
3580 /* This is now known to be a macro call.
3581 Discard the macro name from the output,
3582 along with any following whitespace just copied,
3583 but preserve newlines if not outputting marks since this
3584 is more likely to do the right thing with line numbers. */
3585 obp = op->buf + obufp_before_macroname;
3587 op->lineno = op_lineno_before_macroname;
3589 int newlines = op->lineno - op_lineno_before_macroname;
3590 while (0 < newlines--)
3594 /* Prevent accidental token-pasting with a character
3595 before the macro call. */
3596 if (!traditional && obp != op->buf) {
3598 case '!': case '%': case '&': case '*':
3599 case '+': case '-': case '.': case '/':
3600 case ':': case '<': case '=': case '>':
3602 /* If we are expanding a macro arg, make a newline marker
3603 to separate the tokens. If we are making real output,
3604 a plain space will do. */
3611 /* Expand the macro, reading arguments as needed,
3612 and push the expansion on the input stack. */
3615 macroexpand (hp, op);
3617 /* Reexamine input stack, since macroexpand has pushed
3618 a new level on it. */
3625 } /* End hash-table-search loop */
3627 ident_length = hash = 0; /* Stop collecting identifier */
3630 } /* End if (ident_length > 0) */
3632 } /* End per-char loop */
3634 /* Come here to return -- but first give an error message
3635 if there was an unterminated successful conditional. */
3637 if (if_stack != ip->if_stack)
3641 switch (if_stack->type)
3662 error_with_line (line_for_error (if_stack->lineno),
3663 "unterminated `#%s' conditional", str);
3665 if_stack = ip->if_stack;
3669 * Rescan a string into a temporary buffer and return the result
3670 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3672 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3673 * and insert such markers when appropriate. See `rescan' for details.
3674 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3675 * before substitution; it is 0 for other uses.
3678 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3679 U_CHAR *buf, *limit;
3680 int output_marks, assertions;
3682 register FILE_BUF *ip;
3684 int length = limit - buf;
3686 int odepth = indepth;
3687 int save_assertions_flag = assertions_flag;
3689 assertions_flag = assertions;
3694 /* Set up the input on the input stack. */
3696 buf1 = (U_CHAR *) alloca (length + 1);
3698 register U_CHAR *p1 = buf;
3699 register U_CHAR *p2 = buf1;
3706 /* Set up to receive the output. */
3708 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3709 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3710 obuf.nominal_fname = 0;
3717 obuf.system_header_p = 0;
3719 CHECK_DEPTH ({return obuf;});
3723 ip = &instack[indepth];
3725 ip->nominal_fname = 0;
3726 ip->nominal_fname_len = 0;
3728 ip->system_header_p = 0;
3731 ip->length = length;
3732 ip->buf = ip->bufp = buf1;
3733 ip->if_stack = if_stack;
3735 ip->lineno = obuf.lineno = 1;
3737 /* Scan the input, create the output. */
3738 rescan (&obuf, output_marks);
3740 /* Pop input stack to original state. */
3743 if (indepth != odepth)
3746 /* Record the output. */
3747 obuf.length = obuf.bufp - obuf.buf;
3749 assertions_flag = save_assertions_flag;
3754 * Process a # directive. Expects IP->bufp to point after the '#', as in
3755 * `#define foo bar'. Passes to the directive handler
3756 * (do_define, do_include, etc.): the addresses of the 1st and
3757 * last chars of the directive (starting immediately after the #
3758 * keyword), plus op and the keyword table pointer. If the directive
3759 * contains comments it is copied into a temporary buffer sans comments
3760 * and the temporary buffer is passed to the directive handler instead.
3761 * Likewise for backslash-newlines.
3763 * Returns nonzero if this was a known # directive.
3764 * Otherwise, returns zero, without advancing the input pointer.
3768 handle_directive (ip, op)
3771 register U_CHAR *bp, *cp;
3772 register struct directive *kt;
3773 register int ident_length;
3776 /* Nonzero means we must copy the entire directive
3777 to get rid of comments or backslash-newlines. */
3778 int copy_directive = 0;
3780 U_CHAR *ident, *after_ident;
3784 /* Record where the directive started. do_xifdef needs this. */
3785 directive_start = bp - 1;
3787 /* Skip whitespace and \-newline. */
3789 if (is_hor_space[*bp]) {
3790 if (*bp != ' ' && *bp != '\t' && pedantic)
3791 pedwarn ("%s in preprocessing directive", char_name[*bp]);
3793 } else if (*bp == '/') {
3794 if (bp[1] == '\\' && bp[2] == '\n')
3795 newline_fix (bp + 1);
3796 if (! (bp[1] == '*' || (cplusplus_comments && bp[1] == '/')))
3799 skip_to_end_of_comment (ip, &ip->lineno, 0);
3801 } else if (*bp == '\\' && bp[1] == '\n') {
3802 bp += 2; ip->lineno++;
3806 /* Now find end of directive name.
3807 If we encounter a backslash-newline, exchange it with any following
3808 symbol-constituents so that we end up with a contiguous name. */
3815 if (*cp == '\\' && cp[1] == '\n')
3816 name_newline_fix (cp);
3822 ident_length = cp - bp;
3826 /* A line of just `#' becomes blank. */
3828 if (ident_length == 0 && *after_ident == '\n') {
3829 ip->bufp = after_ident;
3833 if (ident_length == 0 || !is_idstart[*ident]) {
3835 while (is_idchar[*p]) {
3836 if (*p < '0' || *p > '9')
3840 /* Handle # followed by a line number. */
3841 if (p != ident && !is_idchar[*p]) {
3842 static struct directive line_directive_table[] = {
3843 { 4, do_line, "line", T_LINE},
3846 pedwarn ("`#' followed by integer");
3847 after_ident = ident;
3848 kt = line_directive_table;
3852 /* Avoid error for `###' and similar cases unless -pedantic. */
3854 while (*p == '#' || is_hor_space[*p]) p++;
3856 if (pedantic && !lang_asm)
3857 warning ("invalid preprocessing directive");
3863 error ("invalid preprocessing directive name");
3869 * Decode the keyword and call the appropriate expansion
3870 * routine, after moving the input pointer up to the next line.
3872 for (kt = directive_table; kt->length > 0; kt++) {
3873 if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
3874 register U_CHAR *buf;
3875 register U_CHAR *limit;
3878 int *already_output;
3880 /* Nonzero means do not delete comments within the directive.
3881 #define needs this when -traditional. */
3886 limit = ip->buf + ip->length;
3889 keep_comments = traditional && kt->type == T_DEFINE;
3890 /* #import is defined only in Objective C, or when on the NeXT. */
3891 if (kt->type == T_IMPORT
3892 && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1)))
3895 /* Find the end of this directive (first newline not backslashed
3896 and not in a string or comment).
3897 Set COPY_DIRECTIVE if the directive must be copied
3898 (it contains a backslash-newline or a comment). */
3900 buf = bp = after_ident;
3901 while (bp < limit) {
3902 register U_CHAR c = *bp++;
3910 } else if (traditional)
3916 /* "..." is special for #include. */
3917 if (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)) {
3918 while (bp < limit && *bp != '\n') {
3923 if (*bp == '\\' && bp[1] == '\n') {
3934 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_directive, &unterminated);
3935 /* Don't bother calling the directive if we already got an error
3936 message due to unterminated string. Skip everything and pretend
3937 we called the directive. */
3940 /* Traditional preprocessing permits unterminated strings. */
3949 /* <...> is special for #include. */
3951 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
3953 while (bp < limit && *bp != '>' && *bp != '\n') {
3954 if (*bp == '\\' && bp[1] == '\n') {
3964 if (*bp == '\\' && bp[1] == '\n')
3967 || (cplusplus_comments && *bp == '/')) {
3968 U_CHAR *obp = bp - 1;
3970 skip_to_end_of_comment (ip, &ip->lineno, 0);
3972 /* No need to copy the directive because of a comment at the end;
3973 just don't include the comment in the directive. */
3974 if (!put_out_comments) {
3976 for (p = bp; *p == ' ' || *p == '\t'; p++)
3983 /* Don't remove the comments if -traditional. */
3984 if (! keep_comments)
3993 pedwarn ("%s in preprocessing directive", char_name[c]);
3997 --bp; /* Point to the newline */
4005 resume_p = ip->bufp;
4006 /* BP is the end of the directive.
4007 RESUME_P is the next interesting data after the directive.
4008 A comment may come between. */
4010 /* If a directive should be copied through, and -C was given,
4011 pass it through before removing comments. */
4012 if (!no_output && put_out_comments
4013 && (kt->type == T_DEFINE ? dump_macros == dump_definitions
4014 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
4015 : kt->type == T_PRAGMA)) {
4018 /* Output directive name. */
4019 check_expand (op, kt->length + 2);
4020 /* Make sure # is at the start of a line */
4021 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
4026 bcopy (kt->name, op->bufp, kt->length);
4027 op->bufp += kt->length;
4029 /* Output arguments. */
4031 check_expand (op, len);
4032 bcopy (buf, (char *) op->bufp, len);
4034 /* Take account of any (escaped) newlines just output. */
4036 if (buf[len] == '\n')
4039 already_output = &junk;
4040 } /* Don't we need a newline or #line? */
4042 if (copy_directive) {
4043 register U_CHAR *xp = buf;
4044 /* Need to copy entire directive into temp buffer before dispatching */
4046 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for directive plus
4050 /* Copy to the new buffer, deleting comments
4051 and backslash-newlines (and whitespace surrounding the latter). */
4054 register U_CHAR c = *xp++;
4059 abort (); /* A bare newline should never part of the line. */
4062 /* <...> is special for #include. */
4064 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
4066 while (xp < bp && c != '>') {
4068 if (c == '\\' && xp < bp && *xp == '\n')
4079 if (cp != buf && is_hor_space[cp[-1]]) {
4080 while (cp - 1 != buf && is_hor_space[cp[-2]])
4082 SKIP_WHITE_SPACE (xp);
4083 } else if (is_hor_space[*xp]) {
4085 SKIP_WHITE_SPACE (xp);
4087 } else if (traditional && xp < bp) {
4095 register U_CHAR *bp1
4096 = skip_quoted_string (xp - 1, bp, ip->lineno,
4097 NULL_PTR, NULL_PTR, NULL_PTR);
4105 || (cplusplus_comments && *xp == '/')) {
4107 /* If we already copied the directive through,
4108 already_output != 0 prevents outputting comment now. */
4109 skip_to_end_of_comment (ip, already_output, 0);
4111 while (xp != ip->bufp)
4113 /* Delete or replace the slash. */
4114 else if (traditional)
4123 /* Null-terminate the copy. */
4129 ip->bufp = resume_p;
4131 /* Some directives should be written out for cc1 to process,
4132 just as if they were not defined. And sometimes we're copying
4133 directives through. */
4135 if (!no_output && already_output == 0
4136 && (kt->type == T_DEFINE ? (int) dump_names <= (int) dump_macros
4137 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
4138 : kt->type == T_PRAGMA)) {
4141 /* Output directive name. */
4142 check_expand (op, kt->length + 1);
4144 bcopy (kt->name, (char *) op->bufp, kt->length);
4145 op->bufp += kt->length;
4147 if (kt->type == T_DEFINE && dump_macros == dump_names) {
4148 /* Output `#define name' only. */
4151 SKIP_WHITE_SPACE (xp);
4153 while (is_idchar[*xp]) xp++;
4155 check_expand (op, len + 1);
4157 bcopy (yp, (char *) op->bufp, len);
4159 /* Output entire directive. */
4161 check_expand (op, len);
4162 bcopy (buf, (char *) op->bufp, len);
4165 } /* Don't we need a newline or #line? */
4167 /* Call the appropriate directive handler. buf now points to
4168 either the appropriate place in the input buffer, or to
4169 the temp buffer if it was necessary to make one. cp
4170 points to the first char after the contents of the (possibly
4171 copied) directive, in either case. */
4172 (*kt->func) (buf, cp, op, kt);
4173 check_expand (op, ip->length - (ip->bufp - ip->buf));
4179 /* It is deliberate that we don't warn about undefined directives.
4180 That is the responsibility of cc1. */
4187 static struct tm *timebuf;
4189 time_t t = time ((time_t *) 0);
4190 timebuf = localtime (&t);
4195 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4196 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4200 * expand things like __FILE__. Place the expansion into the output
4201 * buffer *without* rescanning.
4205 special_symbol (hp, op)
4212 FILE_BUF *ip = NULL;
4215 int paren = 0; /* For special `defined' keyword */
4217 if (pcp_outfile && pcp_inside_if
4218 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
4219 error ("Predefined macro `%s' used inside `#if' during precompilation",
4222 for (i = indepth; i >= 0; i--)
4223 if (instack[i].fname != NULL) {
4228 error ("cccp error: not in any file?!");
4229 return; /* the show must go on */
4236 FILE_BUF *p = hp->type == T_FILE ? ip : &instack[0];
4237 char *string = p->nominal_fname;
4241 size_t string_len = p->nominal_fname_len;
4242 buf = (char *) alloca (3 + 4 * string_len);
4243 quote_string (buf, string, string_len);
4251 case T_INCLUDE_LEVEL:
4253 for (i = indepth; i >= 0; i--)
4254 if (instack[i].fname != NULL)
4257 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
4258 sprintf (buf, "%d", true_indepth - 1);
4262 buf = (char *) alloca (3 + strlen (version_string));
4263 sprintf (buf, "\"%s\"", version_string);
4266 #ifndef NO_BUILTIN_SIZE_TYPE
4272 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4273 case T_PTRDIFF_TYPE:
4282 case T_USER_LABEL_PREFIX_TYPE:
4283 buf = USER_LABEL_PREFIX;
4286 case T_REGISTER_PREFIX_TYPE:
4287 buf = REGISTER_PREFIX;
4290 case T_IMMEDIATE_PREFIX_TYPE:
4291 buf = IMMEDIATE_PREFIX;
4295 buf = hp->value.cpval;
4296 #ifdef STDC_0_IN_SYSTEM_HEADERS
4297 if (ip->system_header_p
4298 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
4299 && !lookup ((U_CHAR *) "__STRICT_ANSI__", -1, -1))
4302 if (pcp_inside_if && pcp_outfile)
4303 /* Output a precondition for this macro use */
4304 fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
4308 buf = (char *) alloca (10);
4309 sprintf (buf, "%d", ip->lineno);
4314 buf = (char *) alloca (20);
4315 timebuf = timestamp ();
4316 if (hp->type == T_DATE)
4317 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
4318 timebuf->tm_mday, timebuf->tm_year + 1900);
4320 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
4324 case T_SPEC_DEFINED:
4325 buf = " 0 "; /* Assume symbol is not defined */
4326 ip = &instack[indepth];
4327 SKIP_WHITE_SPACE (ip->bufp);
4328 if (*ip->bufp == '(') {
4330 ip->bufp++; /* Skip over the paren */
4331 SKIP_WHITE_SPACE (ip->bufp);
4334 if (!is_idstart[*ip->bufp])
4336 if (ip->bufp[0] == 'L' && (ip->bufp[1] == '\'' || ip->bufp[1] == '"'))
4338 if ((hp = lookup (ip->bufp, -1, -1))) {
4339 if (pcp_outfile && pcp_inside_if
4340 && (hp->type == T_CONST
4341 || (hp->type == T_MACRO && hp->value.defn->predefined)))
4342 /* Output a precondition for this macro use. */
4343 fprintf (pcp_outfile, "#define %s\n", hp->name);
4347 if (pcp_outfile && pcp_inside_if) {
4348 /* Output a precondition for this macro use */
4349 U_CHAR *cp = ip->bufp;
4350 fprintf (pcp_outfile, "#undef ");
4351 while (is_idchar[*cp]) /* Ick! */
4352 fputc (*cp++, pcp_outfile);
4353 putc ('\n', pcp_outfile);
4355 while (is_idchar[*ip->bufp])
4357 SKIP_WHITE_SPACE (ip->bufp);
4359 if (*ip->bufp != ')')
4367 error ("`defined' without an identifier");
4371 error ("cccp error: invalid special hash type"); /* time for gdb */
4375 check_expand (op, len);
4376 bcopy (buf, (char *) op->bufp, len);
4383 /* Routines to handle #directives */
4385 /* Handle #include and #import.
4386 This function expects to see "fname" or <fname> on the input. */
4389 do_include (buf, limit, op, keyword)
4390 U_CHAR *buf, *limit;
4392 struct directive *keyword;
4394 U_CHAR *importing = keyword->type == T_IMPORT ? (U_CHAR *) "" : (U_CHAR *) 0;
4395 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4396 static int import_warning = 0;
4397 char *fname; /* Dynamically allocated fname buffer */
4400 char *fbeg, *fend; /* Beginning and end of fname */
4403 struct file_name_list *search_start = include; /* Chain of dirs to search */
4404 struct file_name_list *dsp; /* First in chain, if #include "..." */
4405 struct file_name_list *searchptr = 0;
4408 int f = -3; /* file number */
4409 struct include_file *inc = 0;
4411 int retried = 0; /* Have already tried macro
4412 expanding the include line*/
4413 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4415 int vaxc_include = 0; /* 1 for token without punctuation */
4422 if (pedantic && !instack[indepth].system_header_p)
4425 pedwarn ("ANSI C does not allow `#import'");
4427 pedwarn ("ANSI C does not allow `#include_next'");
4430 if (importing && warn_import && !inhibit_warnings
4431 && !instack[indepth].system_header_p && !import_warning) {
4433 warning ("using `#import' is not recommended");
4434 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
4435 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
4436 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
4437 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
4438 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
4439 fprintf (stderr, " ... <real contents of file> ...\n");
4440 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
4441 fprintf (stderr, "Then users can use `#include' any number of times.\n");
4442 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
4443 fprintf (stderr, "when it is equipped with such a conditional.\n");
4449 SKIP_WHITE_SPACE (fin);
4450 /* Discard trailing whitespace so we can easily see
4451 if we have parsed all the significant chars we were given. */
4452 while (limit != fin && is_hor_space[limit[-1]]) limit--;
4453 fbeg = fend = (char *) alloca (limit - fin);
4459 /* Copy the operand text, concatenating the strings. */
4464 goto invalid_include_file_name;
4472 /* If not at the end, there had better be another string. */
4473 /* Skip just horiz space, and don't go past limit. */
4474 while (fin != limit && is_hor_space[*fin]) fin++;
4475 if (fin != limit && *fin == '\"')
4482 /* We have "filename". Figure out directory this source
4483 file is coming from and put it on the front of the list. */
4485 /* If -I- was specified, don't search current dir, only spec'd ones. */
4486 if (ignore_srcdir) break;
4488 for (fp = &instack[indepth]; fp >= instack; fp--)
4493 if ((nam = fp->nominal_fname) != NULL) {
4494 /* Found a named file. Figure out dir of the file,
4495 and put it in front of the search list. */
4496 dsp = ((struct file_name_list *)
4497 alloca (sizeof (struct file_name_list)
4498 + fp->nominal_fname_len));
4499 strcpy (dsp->fname, nam);
4500 simplify_filename (dsp->fname);
4501 nam = base_name (dsp->fname);
4504 /* for hack_vms_include_specification(), a local
4505 dir specification must start with "./" on VMS. */
4506 if (nam == dsp->fname)
4513 /* But for efficiency's sake, do not insert the dir
4514 if it matches the search list's first dir. */
4515 dsp->next = search_start;
4516 if (!search_start || strcmp (dsp->fname, search_start->fname)) {
4518 n = nam - dsp->fname;
4519 if (n + INCLUDE_LEN_FUDGE > max_include_len)
4520 max_include_len = n + INCLUDE_LEN_FUDGE;
4522 dsp[0].got_name_map = 0;
4530 while (fin != limit && *fin != '>')
4532 if (*fin == '>' && fin + 1 == limit) {
4534 /* If -I-, start with the first -I dir after the -I-. */
4535 search_start = first_bracket_include;
4543 * Support '#include xyz' like VAX-C to allow for easy use of all the
4544 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4545 * code from case '<' is repeated here) and generates a warning.
4546 * (Note: macro expansion of `xyz' takes precedence.)
4548 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4549 the pre-decrement outside of the macro. */
4550 if (retried && (--fin, ISALPHA(*(U_CHAR *) (fin)))) {
4551 while (fin != limit && (!ISSPACE(*fin)))
4553 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4557 /* If -I-, start with the first -I dir after the -I-. */
4558 search_start = first_bracket_include;
4566 /* Expand buffer and then remove any newline markers.
4567 We can't just tell expand_to_temp_buffer to omit the markers,
4568 since it would put extra spaces in include file names. */
4571 int errors_before_expansion = errors;
4572 trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
4573 if (errors != errors_before_expansion) {
4575 goto invalid_include_file_name;
4578 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
4580 while (src != trybuf.bufp) {
4581 switch ((*limit++ = *src++)) {
4590 U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
4591 NULL_PTR, NULL_PTR, NULL_PTR);
4604 invalid_include_file_name:
4605 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
4609 /* For #include_next, skip in the search path
4610 past the dir in which the containing file was found. */
4613 for (fp = &instack[indepth]; fp >= instack; fp--)
4614 if (fp->fname != NULL) {
4615 /* fp->dir is null if the containing file was specified
4616 with an absolute file name. In that case, don't skip anything. */
4618 search_start = fp->dir->next;
4624 flen = simplify_filename (fbeg);
4628 error ("empty file name in `#%s'", keyword->name);
4632 /* Allocate this permanently, because it gets stored in the definitions
4634 fname = xmalloc (max_include_len + flen + 1);
4635 /* + 1 above for terminating null. */
4637 system_include_depth += angle_brackets;
4639 /* If specified file name is absolute, just open it. */
4641 if (absolute_filename (fbeg)) {
4642 strcpy (fname, fbeg);
4643 f = open_include_file (fname, NULL_PTR, importing, &inc);
4647 struct bypass_dir *next;
4649 struct file_name_list *searchptr;
4650 } **bypass_slot = 0;
4652 /* Search directory path, trying to open the file.
4653 Copy each filename tried into FNAME. */
4655 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
4657 if (searchptr == first_bracket_include) {
4658 /* Go to bypass directory if we know we've seen this file before. */
4659 static struct bypass_dir *bypass_hashtab[INCLUDE_HASHSIZE];
4660 struct bypass_dir *p;
4661 bypass_slot = &bypass_hashtab[hashf ((U_CHAR *) fbeg, flen,
4663 for (p = *bypass_slot; p; p = p->next)
4664 if (!strcmp (fbeg, p->fname)) {
4665 searchptr = p->searchptr;
4672 /* Change this 1/2 Unix 1/2 VMS file specification into a
4673 full VMS file specification */
4674 if (searchptr->fname[0])
4676 strcpy (fname, searchptr->fname);
4677 if (fname[strlen (fname) - 1] == ':')
4680 slashp = strchr (fbeg, '/');
4682 /* start at root-dir of logical device if no path given. */
4684 strcat (fname, "[000000]");
4686 strcat (fname, fbeg);
4688 /* Fix up the filename */
4689 hack_vms_include_specification (fname, vaxc_include);
4693 /* This is a normal VMS filespec, so use it unchanged. */
4694 strcpy (fname, fbeg);
4695 /* if it's '#include filename', add the missing .h */
4696 if (vaxc_include && index(fname,'.')==NULL)
4697 strcat (fname, ".h");
4700 strcpy (fname, searchptr->fname);
4701 strcat (fname, fbeg);
4703 f = open_include_file (fname, searchptr, importing, &inc);
4705 if (bypass_slot && searchptr != first_bracket_include) {
4706 /* This is the first time we found this include file,
4707 and we found it after first_bracket_include.
4708 Record its location so that we can bypass to here next time. */
4709 struct bypass_dir *p
4710 = (struct bypass_dir *) xmalloc (sizeof (struct bypass_dir));
4711 p->next = *bypass_slot;
4712 p->fname = fname + strlen (searchptr->fname);
4713 p->searchptr = searchptr;
4719 /* Our VMS hacks can produce invalid filespecs, so don't worry
4720 about errors other than EACCES. */
4721 if (errno == EACCES)
4724 if (errno != ENOENT && errno != ENOTDIR)
4734 /* The file was already included. */
4736 /* If generating dependencies and -MG was specified, we assume missing
4737 files are leaf files, living in the same directory as the source file
4738 or other similar place; these missing files may be generated from
4739 other files and may not exist yet (eg: y.tab.h). */
4740 } else if (print_deps_missing_files
4741 && (system_include_depth != 0) < print_deps)
4743 /* If it was requested as a system header file,
4744 then assume it belongs in the first place to look for such. */
4748 char *p = (char *) alloca (strlen (search_start->fname)
4749 + strlen (fbeg) + 1);
4750 strcpy (p, search_start->fname);
4752 deps_output (p, ' ');
4757 /* Otherwise, omit the directory, as if the file existed
4758 in the directory with the source. */
4759 deps_output (fbeg, ' ');
4762 /* If -M was specified, and this header file won't be added to the
4763 dependency list, then don't count this as an error, because we can
4764 still produce correct output. Otherwise, we can't produce correct
4765 output, because there may be dependencies we need inside the missing
4766 file, and we don't know what directory this missing file exists in. */
4767 else if (0 < print_deps && print_deps <= (system_include_depth != 0))
4768 warning ("No include path in which to find %s", fbeg);
4770 error_from_errno (fbeg);
4772 error ("No include path in which to find %s", fbeg);
4776 /* Actually process the file. */
4778 pcftry = (char *) alloca (strlen (fname) + 30);
4785 sprintf (pcftry, "%s%d", fname, pcfnum++);
4787 pcf = open (pcftry, O_RDONLY, 0666);
4792 if (fstat (pcf, &s) != 0)
4793 pfatal_with_name (pcftry);
4794 if (! INO_T_EQ (inc->st.st_ino, s.st_ino)
4795 || inc->st.st_dev != s.st_dev)
4797 pcfbuf = check_precompiled (pcf, &s, fname, &pcfbuflimit);
4798 /* Don't need it any more. */
4803 /* Don't need it at all. */
4808 } while (pcf != -1 && !pcfbuf);
4811 /* Actually process the file */
4813 pcfname = xmalloc (strlen (pcftry) + 1);
4814 strcpy (pcfname, pcftry);
4815 pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) pcfbuflimit,
4816 (U_CHAR *) fname, op);
4819 finclude (f, inc, op, is_system_include (fname), searchptr);
4822 system_include_depth -= angle_brackets;
4827 /* Return nonzero if the given FILENAME is an absolute pathname which
4828 designates a file within one of the known "system" include file
4829 directories. We assume here that if the given FILENAME looks like
4830 it is the name of a file which resides either directly in a "system"
4831 include file directory, or within any subdirectory thereof, then the
4832 given file must be a "system" include file. This function tells us
4833 if we should suppress pedantic errors/warnings for the given FILENAME.
4835 The value is 2 if the file is a C-language system header file
4836 for which C++ should (on most systems) assume `extern "C"'. */
4839 is_system_include (filename)
4840 register char *filename;
4842 struct file_name_list *searchptr;
4844 for (searchptr = first_system_include; searchptr;
4845 searchptr = searchptr->next)
4846 if (! strncmp (searchptr->fname, filename, strlen (searchptr->fname)))
4847 return searchptr->c_system_include_path + 1;
4851 /* Yield the non-directory suffix of a file name. */
4859 #if defined (__MSDOS__) || defined (_WIN32)
4860 if (ISALPHA (s[0]) && s[1] == ':') s += 2;
4863 if ((p = rindex (s, ':'))) s = p + 1; /* Skip device. */
4864 if ((p = rindex (s, ']'))) s = p + 1; /* Skip directory. */
4865 if ((p = rindex (s, '>'))) s = p + 1; /* Skip alternate (int'n'l) dir. */
4869 if ((p = rindex (s, '/'))) s = p + 1;
4870 #ifdef DIR_SEPARATOR
4871 if ((p = rindex (s, DIR_SEPARATOR))) s = p + 1;
4876 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4879 absolute_filename (filename)
4882 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN32__))
4883 if (ISALPHA (filename[0]) && filename[1] == ':') filename += 2;
4885 #if defined (__CYGWIN32__)
4886 /* At present, any path that begins with a drive spec is absolute. */
4887 if (ISALPHA (filename[0]) && filename[1] == ':') return 1;
4890 if (index (filename, ':') != 0) return 1;
4892 if (filename[0] == '/') return 1;
4893 #ifdef DIR_SEPARATOR
4894 if (filename[0] == DIR_SEPARATOR) return 1;
4899 /* Remove unnecessary characters from FILENAME in place,
4900 to avoid unnecessary filename aliasing.
4901 Return the length of the resulting string.
4903 Do only the simplifications allowed by Posix.
4904 It is OK to miss simplifications on non-Posix hosts,
4905 since this merely leads to suboptimal results. */
4908 simplify_filename (filename)
4911 register char *from = filename;
4912 register char *to = filename;
4915 /* Remove redundant initial /s. */
4918 if (*++from == '/') {
4919 if (*++from == '/') {
4920 /* 3 or more initial /s are equivalent to 1 /. */
4921 while (*++from == '/')
4924 /* On some hosts // differs from /; Posix allows this. */
4925 static int slashslash_vs_slash;
4926 if (slashslash_vs_slash == 0) {
4928 slashslash_vs_slash = ((stat ("/", &s1) == 0 && stat ("//", &s2) == 0
4929 && INO_T_EQ (s1.st_ino, s2.st_ino)
4930 && s1.st_dev == s2.st_dev)
4933 if (slashslash_vs_slash < 0)
4942 if (from[0] == '.' && from[1] == '/')
4947 /* Copy this component and trailing /, if any. */
4948 while ((*to++ = *from++) != '/') {
4950 /* Trim . component at end of nonempty name. */
4951 to -= filename <= to - 3 && to[-3] == '/' && to[-2] == '.';
4953 /* Trim unnecessary trailing /s. */
4954 while (to0 < --to && to[-1] == '/')
4958 return to - filename;
4963 /* Skip /s after a /. */
4964 while (*from == '/')
4969 /* The file_name_map structure holds a mapping of file names for a
4970 particular directory. This mapping is read from the file named
4971 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4972 map filenames on a file system with severe filename restrictions,
4973 such as DOS. The format of the file name map file is just a series
4974 of lines with two tokens on each line. The first token is the name
4975 to map, and the second token is the actual name to use. */
4977 struct file_name_map
4979 struct file_name_map *map_next;
4984 #define FILE_NAME_MAP_FILE "header.gcc"
4986 /* Read a space delimited string of unlimited length from a stdio
4990 read_filename_string (ch, f)
4998 set = alloc = xmalloc (len + 1);
5002 while ((ch = getc (f)) != EOF && ! is_space[ch])
5004 if (set - alloc == len)
5007 alloc = xrealloc (alloc, len + 1);
5008 set = alloc + len / 2;
5018 /* Read the file name map file for DIRNAME.
5019 If DIRNAME is empty, read the map file for the working directory;
5020 otherwise DIRNAME must end in '/'. */
5022 static struct file_name_map *
5023 read_name_map (dirname)
5026 /* This structure holds a linked list of file name maps, one per
5028 struct file_name_map_list
5030 struct file_name_map_list *map_list_next;
5031 char *map_list_name;
5032 struct file_name_map *map_list_map;
5034 static struct file_name_map_list *map_list;
5035 register struct file_name_map_list *map_list_ptr;
5040 for (map_list_ptr = map_list; map_list_ptr;
5041 map_list_ptr = map_list_ptr->map_list_next)
5042 if (! strcmp (map_list_ptr->map_list_name, dirname))
5043 return map_list_ptr->map_list_map;
5045 map_list_ptr = ((struct file_name_map_list *)
5046 xmalloc (sizeof (struct file_name_map_list)));
5047 map_list_ptr->map_list_name = savestring (dirname);
5048 map_list_ptr->map_list_map = NULL;
5050 dirlen = strlen (dirname);
5051 name = (char *) alloca (dirlen + strlen (FILE_NAME_MAP_FILE) + 1);
5052 strcpy (name, dirname);
5053 strcat (name, FILE_NAME_MAP_FILE);
5054 f = fopen (name, "r");
5056 map_list_ptr->map_list_map = NULL;
5061 while ((ch = getc (f)) != EOF)
5064 struct file_name_map *ptr;
5069 from = read_filename_string (ch, f);
5070 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5072 to = read_filename_string (ch, f);
5074 simplify_filename (from);
5075 tolen = simplify_filename (to);
5077 ptr = ((struct file_name_map *)
5078 xmalloc (sizeof (struct file_name_map)));
5079 ptr->map_from = from;
5081 /* Make the real filename absolute. */
5082 if (absolute_filename (to))
5086 ptr->map_to = xmalloc (dirlen + tolen + 1);
5087 strcpy (ptr->map_to, dirname);
5088 strcat (ptr->map_to, to);
5092 ptr->map_next = map_list_ptr->map_list_map;
5093 map_list_ptr->map_list_map = ptr;
5095 while ((ch = getc (f)) != '\n')
5102 map_list_ptr->map_list_next = map_list;
5103 map_list = map_list_ptr;
5105 return map_list_ptr->map_list_map;
5108 /* Try to open include file FILENAME. SEARCHPTR is the directory
5109 being tried from the include file search path.
5110 IMPORTING is "" if we are importing, null otherwise.
5111 Return -2 if found, either a matching name or a matching inode.
5112 Otherwise, open the file and return a file descriptor if successful
5113 or -1 if unsuccessful.
5114 Unless unsuccessful, put a descriptor of the included file into *PINC.
5115 This function maps filenames on file systems based on information read by
5119 open_include_file (filename, searchptr, importing, pinc)
5121 struct file_name_list *searchptr;
5123 struct include_file **pinc;
5125 char *fname = remap ? remap_include_file (filename, searchptr) : filename;
5128 /* Look up FNAME in include_hashtab. */
5129 struct include_file **phead = &include_hashtab[hashf ((U_CHAR *) fname,
5132 struct include_file *inc, *head = *phead;
5133 for (inc = head; inc; inc = inc->next)
5134 if (!strcmp (fname, inc->fname))
5138 || ! inc->control_macro
5139 || (inc->control_macro[0] && ! lookup (inc->control_macro, -1, -1))) {
5141 fd = open (fname, O_RDONLY, 0);
5146 /* if #include <dir/file> fails, try again with hacked spec. */
5147 if (!hack_vms_include_specification (fname, 0))
5149 fd = open (fname, O_RDONLY, 0);
5156 /* FNAME was not in include_hashtab; insert a new entry. */
5157 inc = (struct include_file *) xmalloc (sizeof (struct include_file));
5160 inc->control_macro = 0;
5161 inc->deps_output = 0;
5162 if (fstat (fd, &inc->st) != 0)
5163 pfatal_with_name (fname);
5166 /* Look for another file with the same inode and device. */
5167 if (lookup_ino_include (inc)
5168 && inc->control_macro
5169 && (!inc->control_macro[0] || lookup (inc->control_macro, -1, -1))) {
5175 /* For -M, add this file to the dependencies. */
5176 if (! inc->deps_output && (system_include_depth != 0) < print_deps) {
5177 inc->deps_output = 1;
5178 deps_output (fname, ' ');
5181 /* Handle -H option. */
5182 if (print_include_names)
5183 fprintf (stderr, "%*s%s\n", indepth, "", fname);
5187 inc->control_macro = importing;
5193 /* Return the remapped name of the include file FILENAME.
5194 SEARCHPTR is the directory being tried from the include file path. */
5197 remap_include_file (filename, searchptr)
5199 struct file_name_list *searchptr;
5201 register struct file_name_map *map;
5202 register char *from;
5206 if (! searchptr->got_name_map)
5208 searchptr->name_map = read_name_map (searchptr->fname);
5209 searchptr->got_name_map = 1;
5212 /* Check the mapping for the directory we are using. */
5213 from = filename + strlen (searchptr->fname);
5214 for (map = searchptr->name_map; map; map = map->map_next)
5215 if (! strcmp (map->map_from, from))
5219 from = base_name (filename);
5221 if (from != filename || !searchptr)
5223 /* Try to find a mapping file for the particular directory we are
5224 looking in. Thus #include <sys/types.h> will look up sys/types.h
5225 in /usr/include/header.gcc and look up types.h in
5226 /usr/include/sys/header.gcc. */
5228 char *dir = (char *) alloca (from - filename + 1);
5229 bcopy (filename, dir, from - filename);
5230 dir[from - filename] = '\0';
5232 for (map = read_name_map (dir); map; map = map->map_next)
5233 if (! strcmp (map->map_from, from))
5240 /* Insert INC into the include file table, hashed by device and inode number.
5241 If a file with different name but same dev+ino was already in the table,
5242 return 1 and set INC's control macro to the already-known macro. */
5245 lookup_ino_include (inc)
5246 struct include_file *inc;
5248 int hash = ((unsigned) (inc->st.st_dev + INO_T_HASH (inc->st.st_ino))
5249 % INCLUDE_HASHSIZE);
5250 struct include_file *i = include_ino_hashtab[hash];
5252 include_ino_hashtab[hash] = inc;
5254 for (; i; i = i->next_ino)
5255 if (INO_T_EQ (inc->st.st_ino, i->st.st_ino)
5256 && inc->st.st_dev == i->st.st_dev) {
5257 inc->control_macro = i->control_macro;
5264 /* Process file descriptor F, which corresponds to include file INC,
5266 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5267 "system" include directories (as decided by the `is_system_include'
5269 DIRPTR is the link in the dir path through which this file was found,
5270 or 0 if the file name was absolute. */
5273 finclude (f, inc, op, system_header_p, dirptr)
5275 struct include_file *inc;
5277 int system_header_p;
5278 struct file_name_list *dirptr;
5280 char *fname = inc->fname;
5282 FILE_BUF *fp; /* For input stack frame */
5283 int missing_newline = 0;
5285 CHECK_DEPTH (return;);
5287 fp = &instack[indepth + 1];
5288 bzero ((char *) fp, sizeof (FILE_BUF));
5289 fp->nominal_fname = fp->fname = fname;
5290 fp->nominal_fname_len = strlen (fname);
5294 fp->if_stack = if_stack;
5295 fp->system_header_p = system_header_p;
5298 if (S_ISREG (inc->st.st_mode)) {
5299 size_t s = (size_t) inc->st.st_size;
5300 if (s != inc->st.st_size || s + 2 < s)
5302 fp->buf = (U_CHAR *) xmalloc (s + 2);
5305 /* Read the file contents, knowing that s is an upper bound
5306 on the number of bytes we can read. */
5307 fp->length = safe_read (f, (char *) fp->buf, s);
5308 if (fp->length < 0) goto nope;
5310 else if (S_ISDIR (inc->st.st_mode)) {
5311 error ("directory `%s' specified in #include", fname);
5315 /* Cannot count its file size before reading.
5316 First read the entire file into heap and
5317 copy them into buffer on stack. */
5322 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5325 i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
5327 goto nope; /* error! */
5329 if (st_size != bsize)
5330 break; /* End of file */
5332 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5335 fp->length = st_size;
5338 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5339 /* Backslash-newline at end is not good enough. */
5340 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5341 fp->buf[fp->length++] = '\n';
5342 missing_newline = 1;
5344 fp->buf[fp->length] = '\0';
5346 /* Close descriptor now, so nesting does not use lots of descriptors. */
5349 /* Must do this before calling trigraph_pcp, so that the correct file name
5350 will be printed in warning messages. */
5353 input_file_stack_tick++;
5358 output_line_directive (fp, op, 0, enter_file);
5361 if (missing_newline)
5364 if (pedantic && missing_newline)
5365 pedwarn ("file does not end in newline");
5368 input_file_stack_tick++;
5369 output_line_directive (&instack[indepth], op, 0, leave_file);
5375 perror_with_name (fname);
5380 /* Record that inclusion of the include file INC
5381 should be controlled by the macro named MACRO_NAME.
5382 This means that trying to include the file again
5383 will do something if that macro is defined. */
5386 record_control_macro (inc, macro_name)
5387 struct include_file *inc;
5390 if (!inc->control_macro || inc->control_macro[0])
5391 inc->control_macro = macro_name;
5394 /* Load the specified precompiled header into core, and verify its
5395 preconditions. PCF indicates the file descriptor to read, which must
5396 be a regular file. *ST is its file status.
5397 FNAME indicates the file name of the original header.
5398 *LIMIT will be set to an address one past the end of the file.
5399 If the preconditions of the file are not satisfied, the buffer is
5400 freed and we return 0. If the preconditions are satisfied, return
5401 the address of the buffer following the preconditions. The buffer, in
5402 this case, should never be freed because various pieces of it will
5403 be referred to until all precompiled strings are output at the end of
5407 check_precompiled (pcf, st, fname, limit)
5410 char *fname ATTRIBUTE_UNUSED;
5420 if (S_ISREG (st->st_mode))
5422 size_t s = (size_t) st->st_size;
5423 if (s != st->st_size || s + 2 < s)
5425 buf = xmalloc (s + 2);
5426 length = safe_read (pcf, buf, s);
5433 if (length > 0 && buf[length-1] != '\n')
5434 buf[length++] = '\n';
5437 *limit = buf + length;
5439 /* File is in core. Check the preconditions. */
5440 if (!check_preconditions (buf))
5442 for (cp = buf; *cp; cp++)
5445 fprintf (stderr, "Using preinclude %s\n", fname);
5451 fprintf (stderr, "Cannot use preinclude %s\n", fname);
5457 /* PREC (null terminated) points to the preconditions of a
5458 precompiled header. These are a series of #define and #undef
5459 lines which must match the current contents of the hash
5463 check_preconditions (prec)
5470 lineend = index (prec, '\n');
5472 if (*prec++ != '#') {
5473 error ("Bad format encountered while reading precompiled file");
5476 if (!strncmp (prec, "define", 6)) {
5480 mdef = create_definition ((U_CHAR *) prec, (U_CHAR *) lineend, NULL_PTR);
5485 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
5486 || (hp->type != T_MACRO && hp->type != T_CONST)
5487 || (hp->type == T_MACRO
5488 && !compare_defs (mdef.defn, hp->value.defn)
5489 && (mdef.defn->length != 2
5490 || mdef.defn->expansion[0] != '\n'
5491 || mdef.defn->expansion[1] != ' ')))
5493 } else if (!strncmp (prec, "undef", 5)) {
5498 while (is_hor_space[(U_CHAR) *prec])
5501 while (is_idchar[(U_CHAR) *prec])
5505 if (lookup ((U_CHAR *) name, len, -1))
5508 error ("Bad format encountered while reading precompiled file");
5513 /* They all passed successfully */
5517 /* Process the main body of a precompiled file. BUF points to the
5518 string section of the file, following the preconditions. LIMIT is one
5519 character past the end. NAME is the name of the file being read
5520 in. OP is the main output buffer. */
5523 pcfinclude (buf, limit, name, op)
5524 U_CHAR *buf, *limit, *name;
5531 /* First in the file comes 4 bytes indicating the number of strings, */
5532 /* in network byte order. (MSB first). */
5534 nstrings = (nstrings << 8) | *cp++;
5535 nstrings = (nstrings << 8) | *cp++;
5536 nstrings = (nstrings << 8) | *cp++;
5538 /* Looping over each string... */
5539 while (nstrings--) {
5540 U_CHAR *string_start;
5541 U_CHAR *endofthiskey;
5545 /* Each string starts with a STRINGDEF structure (str), followed */
5546 /* by the text of the string (string_start) */
5548 /* First skip to a longword boundary */
5549 /* ??? Why a 4-byte boundary? On all machines? */
5550 /* NOTE: This works correctly even if size_t
5551 is narrower than a pointer.
5552 Do not try risky measures here to get another type to use!
5553 Do not include stddef.h--it will fail! */
5554 if ((size_t) cp & 3)
5555 cp += 4 - ((size_t) cp & 3);
5557 /* Now get the string. */
5558 str = (STRINGDEF *) (GENERIC_PTR) cp;
5559 string_start = cp += sizeof (STRINGDEF);
5561 for (; *cp; cp++) /* skip the string */
5564 /* We need to macro expand the string here to ensure that the
5565 proper definition environment is in place. If it were only
5566 expanded when we find out it is needed, macros necessary for
5567 its proper expansion might have had their definitions changed. */
5568 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
5569 /* Lineno is already set in the precompiled file */
5570 str->contents = tmpbuf.buf;
5571 str->len = tmpbuf.length;
5573 str->filename = name;
5574 str->output_mark = outbuf.bufp - outbuf.buf;
5577 *stringlist_tailp = str;
5578 stringlist_tailp = &str->chain;
5580 /* Next comes a fourbyte number indicating the number of keys
5583 nkeys = (nkeys << 8) | *cp++;
5584 nkeys = (nkeys << 8) | *cp++;
5585 nkeys = (nkeys << 8) | *cp++;
5587 /* If this number is -1, then the string is mandatory. */
5591 /* Otherwise, for each key, */
5592 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
5593 KEYDEF *kp = (KEYDEF *) (GENERIC_PTR) cp;
5596 /* It starts with a KEYDEF structure */
5597 cp += sizeof (KEYDEF);
5599 /* Find the end of the key. At the end of this for loop we
5600 advance CP to the start of the next key using this variable. */
5601 endofthiskey = cp + strlen ((char *) cp);
5604 /* Expand the key, and enter it into the hash table. */
5605 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
5606 tmpbuf.bufp = tmpbuf.buf;
5608 while (is_hor_space[*tmpbuf.bufp])
5610 if (!is_idstart[*tmpbuf.bufp]
5611 || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
5616 hp = lookup (tmpbuf.bufp, -1, -1);
5619 install (tmpbuf.bufp, -1, T_PCSTRING, (char *) kp, -1);
5621 else if (hp->type == T_PCSTRING) {
5622 kp->chain = hp->value.keydef;
5623 hp->value.keydef = kp;
5629 /* This output_line_directive serves to switch us back to the current
5630 input file in case some of these strings get output (which will
5631 result in line directives for the header file being output). */
5632 output_line_directive (&instack[indepth], op, 0, enter_file);
5635 /* Called from rescan when it hits a key for strings. Mark them all
5636 used and clean up. */
5644 for (kp = hp->value.keydef; kp; kp = kp->chain)
5645 kp->str->writeflag = 1;
5649 /* Write the output, interspersing precompiled strings in their
5650 appropriate places. */
5655 STRINGDEF *next_string;
5656 U_CHAR *cur_buf_loc;
5657 int line_directive_len = 80;
5658 char *line_directive = xmalloc (line_directive_len);
5661 /* In each run through the loop, either cur_buf_loc ==
5662 next_string_loc, in which case we print a series of strings, or
5663 it is less than next_string_loc, in which case we write some of
5665 cur_buf_loc = outbuf.buf;
5666 next_string = stringlist;
5668 while (cur_buf_loc < outbuf.bufp || next_string) {
5670 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
5671 if (next_string->writeflag) {
5672 len = 4 * strlen ((char *) next_string->filename) + 32;
5673 while (len > line_directive_len)
5674 line_directive = xrealloc (line_directive,
5675 line_directive_len *= 2);
5676 sprintf (line_directive, "\n# %d ", next_string->lineno);
5677 strcpy (quote_string (line_directive + strlen (line_directive),
5678 (char *) next_string->filename,
5679 strlen ((char *) next_string->filename)),
5681 safe_write (fileno (stdout), line_directive, strlen (line_directive));
5682 safe_write (fileno (stdout),
5683 (char *) next_string->contents, next_string->len);
5685 next_string = next_string->chain;
5689 ? (next_string->output_mark
5690 - (cur_buf_loc - outbuf.buf))
5691 : outbuf.bufp - cur_buf_loc);
5693 safe_write (fileno (stdout), (char *) cur_buf_loc, len);
5697 free (line_directive);
5700 /* Pass a directive through to the output file.
5701 BUF points to the contents of the directive, as a contiguous string.
5702 LIMIT points to the first character past the end of the directive.
5703 KEYWORD is the keyword-table entry for the directive. */
5706 pass_thru_directive (buf, limit, op, keyword)
5707 U_CHAR *buf, *limit;
5709 struct directive *keyword;
5711 register unsigned keyword_length = keyword->length;
5713 check_expand (op, 1 + keyword_length + (limit - buf));
5715 bcopy (keyword->name, (char *) op->bufp, keyword_length);
5716 op->bufp += keyword_length;
5717 if (limit != buf && buf[0] != ' ')
5719 bcopy ((char *) buf, (char *) op->bufp, limit - buf);
5720 op->bufp += (limit - buf);
5723 /* Count the line we have just made in the output,
5724 to get in sync properly. */
5729 /* The arglist structure is built by do_define to tell
5730 collect_definition where the argument names begin. That
5731 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5732 would contain pointers to the strings x, y, and z.
5733 Collect_definition would then build a DEFINITION node,
5734 with reflist nodes pointing to the places x, y, and z had
5735 appeared. So the arglist is just convenience data passed
5736 between these two routines. It is not kept around after
5737 the current #define has been processed and entered into the
5741 struct arglist *next;
5748 /* Create a DEFINITION node from a #define directive. Arguments are
5749 as for do_define. */
5752 create_definition (buf, limit, op)
5753 U_CHAR *buf, *limit;
5756 U_CHAR *bp; /* temp ptr into input buffer */
5757 U_CHAR *symname; /* remember where symbol name starts */
5758 int sym_length; /* and how long it is */
5759 int line = instack[indepth].lineno;
5760 char *file = instack[indepth].nominal_fname;
5761 size_t file_len = instack[indepth].nominal_fname_len;
5765 int arglengths = 0; /* Accumulate lengths of arg names
5766 plus number of args. */
5771 while (is_hor_space[*bp])
5774 symname = bp; /* remember where it starts */
5775 sym_length = check_macro_name (bp, "macro");
5778 /* Lossage will occur if identifiers or control keywords are broken
5779 across lines using backslash. This is not the right place to take
5783 struct arglist *arg_ptrs = NULL;
5786 bp++; /* skip '(' */
5787 SKIP_WHITE_SPACE (bp);
5789 /* Loop over macro argument names. */
5790 while (*bp != ')') {
5791 struct arglist *temp;
5793 temp = (struct arglist *) alloca (sizeof (struct arglist));
5795 temp->next = arg_ptrs;
5796 temp->argno = argno++;
5797 temp->rest_args = 0;
5801 pedwarn ("another parameter follows `%s'",
5804 if (!is_idstart[*bp])
5805 pedwarn ("invalid character in macro parameter name");
5807 /* Find the end of the arg name. */
5808 while (is_idchar[*bp]) {
5810 /* do we have a "special" rest-args extension here? */
5811 if (limit - bp > REST_EXTENSION_LENGTH
5812 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
5813 if (pedantic && !instack[indepth].system_header_p)
5814 pedwarn ("ANSI C does not allow macro with variable arguments");
5816 temp->rest_args = 1;
5820 temp->length = bp - temp->name;
5822 bp += REST_EXTENSION_LENGTH;
5823 arglengths += temp->length + 2;
5824 SKIP_WHITE_SPACE (bp);
5825 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
5826 error ("badly punctuated parameter list in `#define'");
5831 SKIP_WHITE_SPACE (bp);
5832 /* A comma at this point can only be followed by an identifier. */
5833 if (!is_idstart[*bp]) {
5834 error ("badly punctuated parameter list in `#define'");
5839 error ("unterminated parameter list in `#define'");
5843 struct arglist *otemp;
5845 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
5846 if (temp->length == otemp->length
5847 && bcmp (temp->name, otemp->name, temp->length) == 0) {
5848 error ("duplicate argument name `%.*s' in `#define'",
5849 temp->length, temp->name);
5855 ++bp; /* skip paren */
5856 SKIP_WHITE_SPACE (bp);
5857 /* now everything from bp before limit is the definition. */
5858 defn = collect_expansion (bp, limit, argno, arg_ptrs);
5859 defn->rest_args = rest_args;
5861 /* Now set defn->args.argnames to the result of concatenating
5862 the argument names in reverse order
5863 with comma-space between them. */
5864 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
5866 struct arglist *temp;
5868 for (temp = arg_ptrs; temp; temp = temp->next) {
5869 bcopy (temp->name, &defn->args.argnames[i], temp->length);
5871 if (temp->next != 0) {
5872 defn->args.argnames[i++] = ',';
5873 defn->args.argnames[i++] = ' ';
5876 defn->args.argnames[i] = 0;
5879 /* Simple expansion or empty definition. */
5883 if (is_hor_space[*bp]) {
5885 SKIP_WHITE_SPACE (bp);
5886 } else if (sym_length) {
5888 case '!': case '"': case '#': case '%': case '&': case '\'':
5889 case ')': case '*': case '+': case ',': case '-': case '.':
5890 case '/': case ':': case ';': case '<': case '=': case '>':
5891 case '?': case '[': case '\\': case ']': case '^': case '{':
5892 case '|': case '}': case '~':
5893 warning ("missing white space after `#define %.*s'",
5894 sym_length, symname);
5898 pedwarn ("missing white space after `#define %.*s'",
5899 sym_length, symname);
5904 /* Now everything from bp before limit is the definition. */
5905 defn = collect_expansion (bp, limit, -1, NULL_PTR);
5906 defn->args.argnames = (U_CHAR *) "";
5911 defn->file_len = file_len;
5913 /* OP is null if this is a predefinition */
5914 defn->predefined = !op;
5916 mdef.symnam = symname;
5917 mdef.symlen = sym_length;
5926 /* Process a #define directive.
5927 BUF points to the contents of the #define directive, as a contiguous string.
5928 LIMIT points to the first character past the end of the definition.
5929 KEYWORD is the keyword-table entry for #define. */
5932 do_define (buf, limit, op, keyword)
5933 U_CHAR *buf, *limit;
5935 struct directive *keyword;
5940 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5941 if (pcp_outfile && op)
5942 pass_thru_directive (buf, limit, op, keyword);
5944 mdef = create_definition (buf, limit, op);
5948 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
5952 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
5954 /* Redefining a precompiled key is ok. */
5955 if (hp->type == T_PCSTRING)
5957 /* Redefining a macro is ok if the definitions are the same. */
5958 else if (hp->type == T_MACRO)
5959 ok = ! compare_defs (mdef.defn, hp->value.defn);
5960 /* Redefining a constant is ok with -D. */
5961 else if (hp->type == T_CONST)
5962 ok = ! done_initializing;
5963 /* Print the warning if it's not ok. */
5965 /* If we are passing through #define and #undef directives, do
5966 that for this re-definition now. */
5967 if (debug_output && op)
5968 pass_thru_directive (buf, limit, op, keyword);
5970 pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
5971 if (hp->type == T_MACRO)
5972 pedwarn_with_file_and_line (hp->value.defn->file,
5973 hp->value.defn->file_len,
5974 hp->value.defn->line,
5975 "this is the location of the previous definition");
5977 /* Replace the old definition. */
5979 hp->value.defn = mdef.defn;
5981 /* If we are passing through #define and #undef directives, do
5982 that for this new definition now. */
5983 if (debug_output && op)
5984 pass_thru_directive (buf, limit, op, keyword);
5985 install (mdef.symnam, mdef.symlen, T_MACRO,
5986 (char *) mdef.defn, hashcode);
5997 /* Check a purported macro name SYMNAME, and yield its length.
5998 USAGE is the kind of name this is intended for. */
6001 check_macro_name (symname, usage)
6008 for (p = symname; is_idchar[*p]; p++)
6010 sym_length = p - symname;
6012 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
6013 error ("invalid %s name", usage);
6014 else if (!is_idstart[*symname]
6015 || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
6016 error ("invalid %s name `%.*s'", usage, sym_length, symname);
6020 /* Return zero if two DEFINITIONs are isomorphic. */
6023 compare_defs (d1, d2)
6024 DEFINITION *d1, *d2;
6026 register struct reflist *a1, *a2;
6027 register U_CHAR *p1 = d1->expansion;
6028 register U_CHAR *p2 = d2->expansion;
6031 if (d1->nargs != d2->nargs)
6034 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
6036 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
6037 a1 = a1->next, a2 = a2->next) {
6038 if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
6039 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
6040 || a1->argno != a2->argno
6041 || a1->stringify != a2->stringify
6042 || a1->raw_before != a2->raw_before
6043 || a1->raw_after != a2->raw_after)
6051 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
6052 p2, d2->length - (p2 - d2->expansion), 1))
6057 /* Return 1 if two parts of two macro definitions are effectively different.
6058 One of the parts starts at BEG1 and has LEN1 chars;
6059 the other has LEN2 chars at BEG2.
6060 Any sequence of whitespace matches any other sequence of whitespace.
6061 FIRST means these parts are the first of a macro definition;
6062 so ignore leading whitespace entirely.
6063 LAST means these parts are the last of a macro definition;
6064 so ignore trailing whitespace entirely. */
6067 comp_def_part (first, beg1, len1, beg2, len2, last)
6069 U_CHAR *beg1, *beg2;
6073 register U_CHAR *end1 = beg1 + len1;
6074 register U_CHAR *end2 = beg2 + len2;
6076 while (beg1 != end1 && is_space[*beg1]) beg1++;
6077 while (beg2 != end2 && is_space[*beg2]) beg2++;
6080 while (beg1 != end1 && is_space[end1[-1]]) end1--;
6081 while (beg2 != end2 && is_space[end2[-1]]) end2--;
6083 while (beg1 != end1 && beg2 != end2) {
6084 if (is_space[*beg1] && is_space[*beg2]) {
6085 while (beg1 != end1 && is_space[*beg1]) beg1++;
6086 while (beg2 != end2 && is_space[*beg2]) beg2++;
6087 } else if (*beg1 == *beg2) {
6091 return (beg1 != end1) || (beg2 != end2);
6094 /* Read a replacement list for a macro with parameters.
6095 Build the DEFINITION structure.
6096 Reads characters of text starting at BUF until END.
6097 ARGLIST specifies the formal parameters to look for
6098 in the text of the definition; NARGS is the number of args
6099 in that list, or -1 for a macro name that wants no argument list.
6100 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6101 and NAMELEN is its length in characters.
6103 Note that comments, backslash-newlines, and leading white space
6104 have already been deleted from the argument. */
6106 /* If there is no trailing whitespace, a Newline Space is added at the end
6107 to prevent concatenation that would be contrary to the standard. */
6110 collect_expansion (buf, end, nargs, arglist)
6113 struct arglist *arglist;
6116 register U_CHAR *p, *limit, *lastp, *exp_p;
6117 struct reflist *endpat = NULL;
6118 /* Pointer to first nonspace after last ## seen. */
6120 /* Pointer to first nonspace after last single-# seen. */
6121 U_CHAR *stringify = 0;
6122 /* How those tokens were spelled. */
6123 enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
6124 enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
6126 int expected_delimiter = '\0';
6128 /* Scan thru the replacement list, ignoring comments and quoted
6129 strings, picking up on the macro calls. It does a linear search
6130 thru the arg list on every potential symbol. Profiling might say
6131 that something smarter should happen. */
6136 /* Find the beginning of the trailing whitespace. */
6139 while (p < limit && is_space[limit[-1]]) limit--;
6141 /* Allocate space for the text in the macro definition.
6142 Each input char may or may not need 1 byte,
6143 so this is an upper bound.
6144 The extra 3 are for invented trailing newline-marker and final null. */
6145 maxsize = (sizeof (DEFINITION)
6147 defn = (DEFINITION *) xcalloc (1, maxsize);
6149 defn->nargs = nargs;
6150 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
6155 : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
6156 error ("`##' at start of macro definition");
6157 p += p[0] == '#' ? 2 : 4;
6160 /* Process the main body of the definition. */
6162 int skipped_arg = 0;
6163 register U_CHAR c = *p++;
6171 if (expected_delimiter != '\0') {
6172 if (c == expected_delimiter)
6173 expected_delimiter = '\0';
6175 expected_delimiter = c;
6179 if (p < limit && expected_delimiter) {
6180 /* In a string, backslash goes through
6181 and makes next char ordinary. */
6187 if (!expected_delimiter && *p == ':') {
6188 /* %: is not a digraph if preceded by an odd number of '<'s. */
6190 while (buf < p0 && p0[-1] == '<')
6193 /* Treat %:%: as ## and %: as #. */
6194 if (p[1] == '%' && p[2] == ':') {
6196 goto sharp_sharp_token;
6207 /* # is ordinary inside a string. */
6208 if (expected_delimiter)
6212 /* ##: concatenate preceding and following tokens. */
6213 /* Take out the first #, discard preceding whitespace. */
6215 while (exp_p > lastp && is_hor_space[exp_p[-1]])
6217 /* Skip the second #. */
6219 concat_sharp_token_type = c;
6220 if (is_hor_space[*p]) {
6221 concat_sharp_token_type = c + 1;
6223 SKIP_WHITE_SPACE (p);
6227 error ("`##' at end of macro definition");
6228 } else if (nargs >= 0) {
6229 /* Single #: stringify following argument ref.
6230 Don't leave the # in the expansion. */
6233 stringify_sharp_token_type = c;
6234 if (is_hor_space[*p]) {
6235 stringify_sharp_token_type = c + 1;
6237 SKIP_WHITE_SPACE (p);
6239 if (! is_idstart[*p] || nargs == 0
6240 || (*p == 'L' && (p[1] == '\'' || p[1] == '"')))
6241 error ("`#' operator is not followed by a macro argument name");
6248 /* In -traditional mode, recognize arguments inside strings and
6249 character constants, and ignore special properties of #.
6250 Arguments inside strings are considered "stringified", but no
6251 extra quote marks are supplied. */
6255 if (expected_delimiter != '\0') {
6256 if (c == expected_delimiter)
6257 expected_delimiter = '\0';
6259 expected_delimiter = c;
6263 /* Backslash quotes delimiters and itself, but not macro args. */
6264 if (expected_delimiter != 0 && p < limit
6265 && (*p == expected_delimiter || *p == '\\')) {
6272 if (expected_delimiter != '\0') /* No comments inside strings. */
6275 /* If we find a comment that wasn't removed by handle_directive,
6276 this must be -traditional. So replace the comment with
6279 while (++p < limit) {
6280 if (p[0] == '*' && p[1] == '/') {
6286 /* Mark this as a concatenation-point, as if it had been ##. */
6294 #ifdef MULTIBYTE_CHARS
6295 /* Handle multibyte characters inside string and character literals. */
6296 if (expected_delimiter != '\0')
6300 length = local_mblen (p, limit - p);
6304 bcopy (p, exp_p, length);
6313 /* Handle the start of a symbol. */
6314 if (is_idchar[c] && nargs > 0) {
6315 U_CHAR *id_beg = p - 1;
6319 while (p != limit && is_idchar[*p]) p++;
6320 id_len = p - id_beg;
6323 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
6324 register struct arglist *arg;
6326 for (arg = arglist; arg != NULL; arg = arg->next) {
6327 struct reflist *tpat;
6329 if (arg->name[0] == c
6330 && arg->length == id_len
6331 && bcmp (arg->name, id_beg, id_len) == 0) {
6332 enum sharp_token_type tpat_stringify;
6333 if (expected_delimiter) {
6334 if (warn_stringify) {
6336 warning ("macro argument `%.*s' is stringified.",
6339 warning ("macro arg `%.*s' would be stringified with -traditional.",
6343 /* If ANSI, don't actually substitute inside a string. */
6346 tpat_stringify = SHARP_TOKEN;
6349 = (stringify == id_beg
6350 ? stringify_sharp_token_type : NO_SHARP_TOKEN);
6352 /* make a pat node for this arg and append it to the end of
6354 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
6357 = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
6358 tpat->raw_after = NO_SHARP_TOKEN;
6359 tpat->rest_args = arg->rest_args;
6360 tpat->stringify = tpat_stringify;
6363 defn->pattern = tpat;
6365 endpat->next = tpat;
6368 tpat->argno = arg->argno;
6369 tpat->nchars = exp_p - lastp;
6371 register U_CHAR *p1 = p;
6372 SKIP_WHITE_SPACE (p1);
6375 : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
6376 tpat->raw_after = p1[0] + (p != p1);
6378 lastp = exp_p; /* place to start copying from next time */
6385 /* If this was not a macro arg, copy it into the expansion. */
6386 if (! skipped_arg) {
6387 register U_CHAR *lim1 = p;
6391 if (stringify == id_beg)
6392 error ("`#' operator should be followed by a macro argument name");
6397 if (!traditional && expected_delimiter == 0) {
6398 /* If ANSI, put in a newline-space marker to prevent token pasting.
6399 But not if "inside a string" (which in ANSI mode happens only for
6407 defn->length = exp_p - defn->expansion;
6409 /* Crash now if we overrun the allocated size. */
6410 if (defn->length + 1 > maxsize)
6414 /* This isn't worth the time it takes. */
6415 /* give back excess storage */
6416 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
6423 do_assert (buf, limit, op, keyword)
6424 U_CHAR *buf, *limit;
6425 FILE_BUF *op ATTRIBUTE_UNUSED;
6426 struct directive *keyword ATTRIBUTE_UNUSED;
6428 U_CHAR *bp; /* temp ptr into input buffer */
6429 U_CHAR *symname; /* remember where symbol name starts */
6430 int sym_length; /* and how long it is */
6431 struct arglist *tokens = NULL;
6433 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6434 pedwarn ("ANSI C does not allow `#assert'");
6438 while (is_hor_space[*bp])
6441 symname = bp; /* remember where it starts */
6442 sym_length = check_macro_name (bp, "assertion");
6444 /* #define doesn't do this, but we should. */
6445 SKIP_WHITE_SPACE (bp);
6447 /* Lossage will occur if identifiers or control tokens are broken
6448 across lines using backslash. This is not the right place to take
6452 error ("missing token-sequence in `#assert'");
6459 bp++; /* skip '(' */
6460 SKIP_WHITE_SPACE (bp);
6462 tokens = read_token_list (&bp, limit, &error_flag);
6466 error ("empty token-sequence in `#assert'");
6470 ++bp; /* skip paren */
6471 SKIP_WHITE_SPACE (bp);
6474 /* If this name isn't already an assertion name, make it one.
6475 Error if it was already in use in some other way. */
6478 ASSERTION_HASHNODE *hp;
6479 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6480 struct tokenlist_list *value
6481 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6483 hp = assertion_lookup (symname, sym_length, hashcode);
6485 if (sym_length == 7 && ! bcmp (symname, "defined", 7))
6486 error ("`defined' redefined as assertion");
6487 hp = assertion_install (symname, sym_length, hashcode);
6490 /* Add the spec'd token-sequence to the list of such. */
6491 value->tokens = tokens;
6492 value->next = hp->value;
6500 do_unassert (buf, limit, op, keyword)
6501 U_CHAR *buf, *limit;
6502 FILE_BUF *op ATTRIBUTE_UNUSED;
6503 struct directive *keyword ATTRIBUTE_UNUSED;
6505 U_CHAR *bp; /* temp ptr into input buffer */
6506 U_CHAR *symname; /* remember where symbol name starts */
6507 int sym_length; /* and how long it is */
6509 struct arglist *tokens = NULL;
6510 int tokens_specified = 0;
6512 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6513 pedwarn ("ANSI C does not allow `#unassert'");
6517 while (is_hor_space[*bp])
6520 symname = bp; /* remember where it starts */
6521 sym_length = check_macro_name (bp, "assertion");
6523 /* #define doesn't do this, but we should. */
6524 SKIP_WHITE_SPACE (bp);
6526 /* Lossage will occur if identifiers or control tokens are broken
6527 across lines using backslash. This is not the right place to take
6533 bp++; /* skip '(' */
6534 SKIP_WHITE_SPACE (bp);
6536 tokens = read_token_list (&bp, limit, &error_flag);
6540 error ("empty token list in `#unassert'");
6544 tokens_specified = 1;
6546 ++bp; /* skip paren */
6547 SKIP_WHITE_SPACE (bp);
6551 ASSERTION_HASHNODE *hp;
6552 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6553 struct tokenlist_list *tail, *prev;
6555 hp = assertion_lookup (symname, sym_length, hashcode);
6559 /* If no token list was specified, then eliminate this assertion
6561 if (! tokens_specified) {
6562 struct tokenlist_list *next;
6563 for (tail = hp->value; tail; tail = next) {
6565 free_token_list (tail->tokens);
6568 delete_assertion (hp);
6570 /* If a list of tokens was given, then delete any matching list. */
6575 struct tokenlist_list *next = tail->next;
6576 if (compare_token_lists (tail->tokens, tokens)) {
6580 hp->value = tail->next;
6581 free_token_list (tail->tokens);
6594 /* Test whether there is an assertion named NAME
6595 and optionally whether it has an asserted token list TOKENS.
6596 NAME is not null terminated; its length is SYM_LENGTH.
6597 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6600 check_assertion (name, sym_length, tokens_specified, tokens)
6603 int tokens_specified;
6604 struct arglist *tokens;
6606 ASSERTION_HASHNODE *hp;
6607 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6609 if (pedantic && !instack[indepth].system_header_p)
6610 pedwarn ("ANSI C does not allow testing assertions");
6612 hp = assertion_lookup (name, sym_length, hashcode);
6614 /* It is not an assertion; just return false. */
6617 /* If no token list was specified, then value is 1. */
6618 if (! tokens_specified)
6622 struct tokenlist_list *tail;
6626 /* If a list of tokens was given,
6627 then succeed if the assertion records a matching list. */
6630 if (compare_token_lists (tail->tokens, tokens))
6635 /* Fail if the assertion has no matching list. */
6640 /* Compare two lists of tokens for equality including order of tokens. */
6643 compare_token_lists (l1, l2)
6644 struct arglist *l1, *l2;
6647 if (l1->length != l2->length)
6649 if (bcmp (l1->name, l2->name, l1->length))
6655 /* Succeed if both lists end at the same time. */
6659 /* Read a space-separated list of tokens ending in a close parenthesis.
6660 Return a list of strings, in the order they were written.
6661 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6662 Parse the text starting at *BPP, and update *BPP.
6663 Don't parse beyond LIMIT. */
6665 static struct arglist *
6666 read_token_list (bpp, limit, error_flag)
6671 struct arglist *token_ptrs = 0;
6677 /* Loop over the assertion value tokens. */
6679 struct arglist *temp;
6683 /* Find the end of the token. */
6687 } else if (*bp == ')') {
6692 } else if (*bp == '"' || *bp == '\'')
6693 bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
6695 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
6696 && *bp != '"' && *bp != '\'' && bp != limit)
6699 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
6700 temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
6701 bcopy ((char *) beg, (char *) temp->name, bp - beg);
6702 temp->name[bp - beg] = 0;
6703 temp->next = token_ptrs;
6705 temp->length = bp - beg;
6707 SKIP_WHITE_SPACE (bp);
6710 error ("unterminated token sequence in `#assert' or `#unassert'");
6717 /* We accumulated the names in reverse order.
6718 Now reverse them to get the proper order. */
6720 register struct arglist *prev = 0, *this, *next;
6721 for (this = token_ptrs; this; this = next) {
6731 free_token_list (tokens)
6732 struct arglist *tokens;
6735 struct arglist *next = tokens->next;
6736 free (tokens->name);
6742 /* Install a name in the assertion hash table.
6744 If LEN is >= 0, it is the length of the name.
6745 Otherwise, compute the length by scanning the entire name.
6747 If HASH is >= 0, it is the precomputed hash code.
6748 Otherwise, compute the hash code. */
6750 static ASSERTION_HASHNODE *
6751 assertion_install (name, len, hash)
6756 register ASSERTION_HASHNODE *hp;
6757 register int i, bucket;
6758 register U_CHAR *p, *q;
6760 i = sizeof (ASSERTION_HASHNODE) + len + 1;
6761 hp = (ASSERTION_HASHNODE *) xmalloc (i);
6763 hp->bucket_hdr = &assertion_hashtab[bucket];
6764 hp->next = assertion_hashtab[bucket];
6765 assertion_hashtab[bucket] = hp;
6767 if (hp->next != NULL)
6768 hp->next->prev = hp;
6771 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
6774 for (i = 0; i < len; i++)
6780 /* Find the most recent hash node for name "name" (ending with first
6781 non-identifier char) installed by install
6783 If LEN is >= 0, it is the length of the name.
6784 Otherwise, compute the length by scanning the entire name.
6786 If HASH is >= 0, it is the precomputed hash code.
6787 Otherwise, compute the hash code. */
6789 static ASSERTION_HASHNODE *
6790 assertion_lookup (name, len, hash)
6795 register ASSERTION_HASHNODE *bucket;
6797 bucket = assertion_hashtab[hash];
6799 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
6801 bucket = bucket->next;
6807 delete_assertion (hp)
6808 ASSERTION_HASHNODE *hp;
6811 if (hp->prev != NULL)
6812 hp->prev->next = hp->next;
6813 if (hp->next != NULL)
6814 hp->next->prev = hp->prev;
6816 /* Make sure that the bucket chain header that the deleted guy was
6817 on points to the right thing afterwards. */
6818 if (hp == *hp->bucket_hdr)
6819 *hp->bucket_hdr = hp->next;
6825 * interpret #line directive. Remembers previously seen fnames
6826 * in its very own hash table.
6828 #define FNAME_HASHSIZE 37
6831 do_line (buf, limit, op, keyword)
6832 U_CHAR *buf, *limit;
6834 struct directive *keyword ATTRIBUTE_UNUSED;
6836 register U_CHAR *bp;
6837 FILE_BUF *ip = &instack[indepth];
6840 enum file_change_code file_change = same_file;
6842 /* Expand any macros. */
6843 tem = expand_to_temp_buffer (buf, limit, 0, 0);
6845 /* Point to macroexpanded line, which is null-terminated now. */
6847 SKIP_WHITE_SPACE (bp);
6849 if (!ISDIGIT (*bp)) {
6850 error ("invalid format `#line' directive");
6854 /* The Newline at the end of this line remains to be processed.
6855 To put the next line at the specified line number,
6856 we must store a line number now that is one less. */
6857 new_lineno = atoi ((char *) bp) - 1;
6859 /* NEW_LINENO is one less than the actual line number here. */
6860 if (pedantic && new_lineno < 0)
6861 pedwarn ("line number out of range in `#line' directive");
6863 /* skip over the line number. */
6864 while (ISDIGIT (*bp))
6867 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6868 if (*bp && !is_space[*bp]) {
6869 error ("invalid format `#line' directive");
6874 SKIP_WHITE_SPACE (bp);
6877 static HASHNODE *fname_table[FNAME_HASHSIZE];
6878 HASHNODE *hp, **hash_bucket;
6884 /* Turn the file name, which is a character string literal,
6885 into a null-terminated string. Do this in place. */
6888 switch ((*p++ = *bp++)) {
6890 error ("invalid format `#line' directive");
6895 char *bpc = (char *) bp;
6896 HOST_WIDE_INT c = parse_escape (&bpc, (HOST_WIDE_INT) (U_CHAR) (-1));
6897 bp = (U_CHAR *) bpc;
6910 fname_length = p - fname;
6912 SKIP_WHITE_SPACE (bp);
6915 pedwarn ("garbage at end of `#line' directive");
6917 file_change = enter_file;
6918 else if (*bp == '2')
6919 file_change = leave_file;
6920 else if (*bp == '3')
6921 ip->system_header_p = 1;
6922 else if (*bp == '4')
6923 ip->system_header_p = 2;
6925 error ("invalid format `#line' directive");
6930 SKIP_WHITE_SPACE (bp);
6932 ip->system_header_p = 1;
6934 SKIP_WHITE_SPACE (bp);
6937 ip->system_header_p = 2;
6939 SKIP_WHITE_SPACE (bp);
6942 error ("invalid format `#line' directive");
6947 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
6948 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
6949 if (hp->length == fname_length &&
6950 bcmp (hp->value.cpval, fname, fname_length) == 0) {
6951 ip->nominal_fname = hp->value.cpval;
6952 ip->nominal_fname_len = fname_length;
6956 /* Didn't find it; cons up a new one. */
6957 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
6958 hp->next = *hash_bucket;
6961 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
6962 ip->nominal_fname_len = hp->length = fname_length;
6963 bcopy (fname, hp->value.cpval, fname_length + 1);
6966 error ("invalid format `#line' directive");
6970 ip->lineno = new_lineno;
6971 output_line_directive (ip, op, 0, file_change);
6972 check_expand (op, ip->length - (ip->bufp - ip->buf));
6976 /* Remove the definition of a symbol from the symbol table.
6977 according to un*x /lib/cpp, it is not an error to undef
6978 something that has no definitions, so it isn't one here either. */
6981 do_undef (buf, limit, op, keyword)
6982 U_CHAR *buf, *limit;
6984 struct directive *keyword;
6988 U_CHAR *orig_buf = buf;
6990 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
6991 if (pcp_outfile && op)
6992 pass_thru_directive (buf, limit, op, keyword);
6994 SKIP_WHITE_SPACE (buf);
6995 sym_length = check_macro_name (buf, "macro");
6997 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
6998 /* If we are generating additional info for debugging (with -g) we
6999 need to pass through all effective #undef directives. */
7000 if (debug_output && op)
7001 pass_thru_directive (orig_buf, limit, op, keyword);
7002 if (hp->type != T_MACRO)
7003 warning ("undefining `%s'", hp->name);
7009 SKIP_WHITE_SPACE (buf);
7011 pedwarn ("garbage after `#undef' directive");
7016 /* Report an error detected by the program we are processing.
7017 Use the text of the line in the error message.
7018 (We use error because it prints the filename & line#.) */
7021 do_error (buf, limit, op, keyword)
7022 U_CHAR *buf, *limit;
7023 FILE_BUF *op ATTRIBUTE_UNUSED;
7024 struct directive *keyword ATTRIBUTE_UNUSED;
7026 int length = limit - buf;
7027 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
7028 bcopy ((char *) buf, (char *) copy, length);
7030 SKIP_WHITE_SPACE (copy);
7031 error ("#error %s", copy);
7035 /* Report a warning detected by the program we are processing.
7036 Use the text of the line in the warning message, then continue.
7037 (We use error because it prints the filename & line#.) */
7040 do_warning (buf, limit, op, keyword)
7041 U_CHAR *buf, *limit;
7042 FILE_BUF *op ATTRIBUTE_UNUSED;
7043 struct directive *keyword ATTRIBUTE_UNUSED;
7045 int length = limit - buf;
7046 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
7047 bcopy ((char *) buf, (char *) copy, length);
7049 SKIP_WHITE_SPACE (copy);
7051 if (pedantic && !instack[indepth].system_header_p)
7052 pedwarn ("ANSI C does not allow `#warning'");
7054 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
7055 if -pedantic-errors is given, #warning should cause an error. */
7056 pedwarn ("#warning %s", copy);
7060 /* Remember the name of the current file being read from so that we can
7061 avoid ever including it again. */
7068 for (i = indepth; i >= 0; i--)
7069 if (instack[i].inc) {
7070 record_control_macro (instack[i].inc, (U_CHAR *) "");
7075 /* Report program identification. */
7078 do_ident (buf, limit, op, keyword)
7079 U_CHAR *buf, *limit;
7081 struct directive *keyword ATTRIBUTE_UNUSED;
7086 /* Allow #ident in system headers, since that's not user's fault. */
7087 if (pedantic && !instack[indepth].system_header_p)
7088 pedwarn ("ANSI C does not allow `#ident'");
7090 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
7092 len = trybuf.bufp - buf;
7094 /* Output expanded directive. */
7095 check_expand (op, 7 + len);
7096 bcopy ("#ident ", (char *) op->bufp, 7);
7098 bcopy ((char *) buf, (char *) op->bufp, len);
7105 /* #pragma and its argument line have already been copied to the output file.
7106 Just check for some recognized pragmas that need validation here. */
7109 do_pragma (buf, limit, op, keyword)
7110 U_CHAR *buf, *limit ATTRIBUTE_UNUSED;
7111 FILE_BUF *op ATTRIBUTE_UNUSED;
7112 struct directive *keyword ATTRIBUTE_UNUSED;
7114 SKIP_WHITE_SPACE (buf);
7115 if (!strncmp ((char *) buf, "once", 4)) {
7116 /* Allow #pragma once in system headers, since that's not the user's
7118 if (!instack[indepth].system_header_p)
7119 warning ("`#pragma once' is obsolete");
7123 if (!strncmp ((char *) buf, "implementation", 14)) {
7124 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7125 been included yet. */
7128 U_CHAR *p = buf + 14, *fname;
7129 SKIP_WHITE_SPACE (p);
7134 if ((p = (U_CHAR *) index ((char *) fname, '\"')))
7137 for (h = 0; h < INCLUDE_HASHSIZE; h++) {
7138 struct include_file *inc;
7139 for (inc = include_hashtab[h]; inc; inc = inc->next) {
7140 if (!strcmp (base_name (inc->fname), (char *) fname)) {
7141 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname);
7151 /* This was a fun hack, but #pragma seems to start to be useful.
7152 By failing to recognize it, we pass it through unchanged to cc1. */
7154 /* The behavior of the #pragma directive is implementation defined.
7155 this implementation defines it as follows. */
7161 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
7164 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
7166 execl ("/usr/games/hack", "#pragma", 0);
7167 execl ("/usr/games/rogue", "#pragma", 0);
7168 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7169 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7171 fatal ("You are in a maze of twisty compiler features, all different");
7175 #ifdef SCCS_DIRECTIVE
7177 /* Just ignore #sccs, on systems where we define it at all. */
7180 do_sccs (buf, limit, op, keyword)
7181 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
7182 FILE_BUF *op ATTRIBUTE_UNUSED;
7183 struct directive *keyword ATTRIBUTE_UNUSED;
7186 pedwarn ("ANSI C does not allow `#sccs'");
7190 #endif /* defined (SCCS_DIRECTIVE) */
7192 /* Handle #if directive by
7193 1) inserting special `defined' keyword into the hash table
7194 that gets turned into 0 or 1 by special_symbol (thus,
7195 if the luser has a symbol called `defined' already, it won't
7196 work inside the #if directive)
7197 2) rescan the input into a temporary output buffer
7198 3) pass the output buffer to the yacc parser and collect a value
7199 4) clean up the mess left from steps 1 and 2.
7200 5) call conditional_skip to skip til the next #endif (etc.),
7201 or not, depending on the value from step 3. */
7204 do_if (buf, limit, op, keyword)
7205 U_CHAR *buf, *limit;
7207 struct directive *keyword ATTRIBUTE_UNUSED;
7209 HOST_WIDE_INT value;
7210 FILE_BUF *ip = &instack[indepth];
7212 value = eval_if_expression (buf, limit - buf);
7213 conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
7217 /* Handle a #elif directive by not changing if_stack either.
7218 see the comment above do_else. */
7221 do_elif (buf, limit, op, keyword)
7222 U_CHAR *buf, *limit;
7224 struct directive *keyword ATTRIBUTE_UNUSED;
7226 HOST_WIDE_INT value;
7227 FILE_BUF *ip = &instack[indepth];
7229 if (if_stack == instack[indepth].if_stack) {
7230 error ("`#elif' not within a conditional");
7233 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7234 error ("`#elif' after `#else'");
7235 fprintf (stderr, " (matches line %d", if_stack->lineno);
7236 if (! (if_stack->fname_len == ip->nominal_fname_len
7237 && !bcmp (if_stack->fname, ip->nominal_fname,
7238 if_stack->fname_len))) {
7239 fprintf (stderr, ", file ");
7240 eprint_string (if_stack->fname, if_stack->fname_len);
7242 fprintf (stderr, ")\n");
7244 if_stack->type = T_ELIF;
7247 if (if_stack->if_succeeded)
7248 skip_if_group (ip, 0, op);
7250 value = eval_if_expression (buf, limit - buf);
7252 skip_if_group (ip, 0, op);
7254 ++if_stack->if_succeeded; /* continue processing input */
7255 output_line_directive (ip, op, 1, same_file);
7261 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7262 result as a C expression and return the value as an int. */
7264 static HOST_WIDE_INT
7265 eval_if_expression (buf, length)
7270 HASHNODE *save_defined;
7271 HOST_WIDE_INT value;
7273 save_defined = install ((U_CHAR *) "defined", -1, T_SPEC_DEFINED,
7276 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
7278 delete_macro (save_defined); /* clean up special symbol */
7280 temp_obuf.buf[temp_obuf.length] = '\n';
7281 value = parse_c_expression ((char *) temp_obuf.buf,
7282 warn_undef && !instack[indepth].system_header_p);
7284 free (temp_obuf.buf);
7289 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7290 or don't skip to the #endif/#else/#elif depending on what directive
7291 is actually being processed. */
7294 do_xifdef (buf, limit, op, keyword)
7295 U_CHAR *buf, *limit;
7297 struct directive *keyword;
7300 FILE_BUF *ip = &instack[indepth];
7302 int start_of_file = 0;
7303 U_CHAR *control_macro = 0;
7305 /* Detect a #ifndef at start of file (not counting comments). */
7306 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
7307 U_CHAR *p = ip->buf;
7308 while (p != directive_start) {
7312 /* Make no special provision for backslash-newline here; this is
7313 slower if backslash-newlines are present, but it's correct,
7314 and it's not worth it to tune for the rare backslash-newline. */
7316 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7317 /* Skip this comment. */
7319 U_CHAR *save_bufp = ip->bufp;
7321 p = skip_to_end_of_comment (ip, &junk, 1);
7322 ip->bufp = save_bufp;
7327 /* If we get here, this conditional is the beginning of the file. */
7332 /* Discard leading and trailing whitespace. */
7333 SKIP_WHITE_SPACE (buf);
7334 while (limit != buf && is_hor_space[limit[-1]]) limit--;
7336 /* Find the end of the identifier at the beginning. */
7337 for (end = buf; is_idchar[*end]; end++);
7340 skip = (keyword->type == T_IFDEF);
7342 pedwarn (end == limit ? "`#%s' with no argument"
7343 : "`#%s' argument starts with punctuation",
7348 if (! traditional) {
7349 if (ISDIGIT (buf[0]))
7350 pedwarn ("`#%s' argument starts with a digit", keyword->name);
7351 else if (end != limit)
7352 pedwarn ("garbage at end of `#%s' argument", keyword->name);
7355 hp = lookup (buf, end-buf, -1);
7358 /* Output a precondition for this macro. */
7360 && (hp->type == T_CONST
7361 || (hp->type == T_MACRO && hp->value.defn->predefined)))
7362 fprintf (pcp_outfile, "#define %s\n", hp->name);
7365 fprintf (pcp_outfile, "#undef ");
7366 while (is_idchar[*cp]) /* Ick! */
7367 fputc (*cp++, pcp_outfile);
7368 putc ('\n', pcp_outfile);
7372 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
7373 if (start_of_file && !skip) {
7374 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
7375 bcopy ((char *) buf, (char *) control_macro, end - buf);
7376 control_macro[end - buf] = 0;
7380 conditional_skip (ip, skip, T_IF, control_macro, op);
7384 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7385 If this is a #ifndef starting at the beginning of a file,
7386 CONTROL_MACRO is the macro name tested by the #ifndef.
7387 Otherwise, CONTROL_MACRO is 0. */
7390 conditional_skip (ip, skip, type, control_macro, op)
7393 enum node_type type;
7394 U_CHAR *control_macro;
7397 IF_STACK_FRAME *temp;
7399 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7400 temp->fname = ip->nominal_fname;
7401 temp->fname_len = ip->nominal_fname_len;
7402 temp->lineno = ip->lineno;
7403 temp->next = if_stack;
7404 temp->control_macro = control_macro;
7407 if_stack->type = type;
7410 skip_if_group (ip, 0, op);
7413 ++if_stack->if_succeeded;
7414 output_line_directive (ip, &outbuf, 1, same_file);
7418 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7419 Leaves input ptr at the sharp sign found.
7420 If ANY is nonzero, return at next directive of any sort. */
7423 skip_if_group (ip, any, op)
7428 register U_CHAR *bp = ip->bufp, *cp;
7429 register U_CHAR *endb = ip->buf + ip->length;
7430 struct directive *kt;
7431 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
7432 U_CHAR *beg_of_line = bp;
7433 register int ident_length;
7434 U_CHAR *ident, *after_ident;
7435 /* Save info about where the group starts. */
7436 U_CHAR *beg_of_group = bp;
7437 int beg_lineno = ip->lineno;
7438 int skipping_include_directive = 0;
7440 if (output_conditionals && op != 0) {
7441 char *ptr = "#failed\n";
7442 int len = strlen (ptr);
7444 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7449 check_expand (op, len);
7450 bcopy (ptr, (char *) op->bufp, len);
7453 output_line_directive (ip, op, 1, 0);
7458 case '/': /* possible comment */
7459 if (*bp == '\\' && bp[1] == '\n')
7462 || (cplusplus_comments && *bp == '/')) {
7464 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
7468 if (skipping_include_directive) {
7469 while (bp < endb && *bp != '>' && *bp != '\n') {
7470 if (*bp == '\\' && bp[1] == '\n') {
7479 if (skipping_include_directive) {
7480 while (bp < endb && *bp != '\n') {
7485 if (*bp == '\\' && bp[1] == '\n') {
7495 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
7496 NULL_PTR, NULL_PTR);
7499 /* Char after backslash loses its special meaning in some cases. */
7503 } else if (traditional && bp < endb)
7509 skipping_include_directive = 0;
7512 if (beg_of_line == 0 || traditional)
7515 while (bp[0] == '\\' && bp[1] == '\n')
7521 /* # keyword: a # must be first nonblank char on the line */
7522 if (beg_of_line == 0)
7526 /* Scan from start of line, skipping whitespace, comments
7527 and backslash-newlines, and see if we reach this #.
7528 If not, this # is not special. */
7530 /* If -traditional, require # to be at beginning of line. */
7533 if (is_hor_space[*bp])
7535 else if (*bp == '\\' && bp[1] == '\n')
7537 else if (*bp == '/' && bp[1] == '*') {
7551 #ifdef MULTIBYTE_CHARS
7553 length = local_mblen (bp, endb - bp);
7561 /* There is no point in trying to deal with C++ // comments here,
7562 because if there is one, then this # must be part of the
7563 comment and we would never reach here. */
7567 if (bp != ip->bufp) {
7568 bp = ip->bufp + 1; /* Reset bp to after the #. */
7572 bp = ip->bufp + 1; /* Point after the '#' */
7573 if (ip->bufp[0] == '%') {
7574 /* Skip past the ':' again. */
7575 while (*bp == '\\') {
7582 /* Skip whitespace and \-newline. */
7584 if (is_hor_space[*bp])
7586 else if (*bp == '\\' && bp[1] == '\n')
7588 else if (*bp == '/') {
7589 if (bp[1] == '\\' && bp[2] == '\n')
7590 newline_fix (bp + 1);
7592 for (bp += 2; ; bp++) {
7595 else if (*bp == '*') {
7596 if (bp[-1] == '/' && warn_comments)
7597 warning ("`/*' within comment");
7598 if (bp[1] == '\\' && bp[2] == '\n')
7599 newline_fix (bp + 1);
7605 #ifdef MULTIBYTE_CHARS
7607 length = local_mblen (bp, endb - bp);
7614 } else if (bp[1] == '/' && cplusplus_comments) {
7615 for (bp += 2; ; bp++) {
7620 warning ("multiline `//' comment");
7625 #ifdef MULTIBYTE_CHARS
7627 length = local_mblen (bp, endb - bp);
7641 /* Now find end of directive name.
7642 If we encounter a backslash-newline, exchange it with any following
7643 symbol-constituents so that we end up with a contiguous name. */
7649 if (*bp == '\\' && bp[1] == '\n')
7650 name_newline_fix (bp);
7656 ident_length = bp - cp;
7660 /* A line of just `#' becomes blank. */
7662 if (ident_length == 0 && *after_ident == '\n') {
7666 if (ident_length == 0 || !is_idstart[*ident]) {
7668 while (is_idchar[*p]) {
7669 if (*p < '0' || *p > '9')
7673 /* Handle # followed by a line number. */
7674 if (p != ident && !is_idchar[*p]) {
7676 pedwarn ("`#' followed by integer");
7680 /* Avoid error for `###' and similar cases unless -pedantic. */
7682 while (*p == '#' || is_hor_space[*p]) p++;
7684 if (pedantic && !lang_asm)
7685 pedwarn ("invalid preprocessing directive");
7690 if (!lang_asm && pedantic)
7691 pedwarn ("invalid preprocessing directive name");
7695 for (kt = directive_table; kt->length >= 0; kt++) {
7696 IF_STACK_FRAME *temp;
7697 if (ident_length == kt->length
7698 && bcmp (cp, kt->name, kt->length) == 0) {
7699 /* If we are asked to return on next directive, do so now. */
7707 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7708 temp->next = if_stack;
7710 temp->lineno = ip->lineno;
7711 temp->fname = ip->nominal_fname;
7712 temp->fname_len = ip->nominal_fname_len;
7713 temp->type = kt->type;
7717 if (pedantic && if_stack != save_if_stack)
7718 validate_else (bp, endb);
7720 if (if_stack == instack[indepth].if_stack) {
7721 error ("`#%s' not within a conditional", kt->name);
7724 else if (if_stack == save_if_stack)
7725 goto done; /* found what we came for */
7727 if (kt->type != T_ENDIF) {
7728 if (if_stack->type == T_ELSE)
7729 error ("`#else' or `#elif' after `#else'");
7730 if_stack->type = kt->type;
7735 if_stack = if_stack->next;
7740 case T_INCLUDE_NEXT:
7742 skipping_include_directive = 1;
7751 /* Don't let erroneous code go by. */
7752 if (kt->length < 0 && !lang_asm && pedantic)
7753 pedwarn ("invalid preprocessing directive name");
7758 /* after this returns, rescan will exit because ip->bufp
7759 now points to the end of the buffer.
7760 rescan is responsible for the error message also. */
7763 if (output_conditionals && op != 0) {
7764 char *ptr = "#endfailed\n";
7765 int len = strlen (ptr);
7767 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7772 check_expand (op, beg_of_line - beg_of_group);
7773 bcopy ((char *) beg_of_group, (char *) op->bufp,
7774 beg_of_line - beg_of_group);
7775 op->bufp += beg_of_line - beg_of_group;
7776 op->lineno += ip->lineno - beg_lineno;
7777 check_expand (op, len);
7778 bcopy (ptr, (char *) op->bufp, len);
7784 /* Handle a #else directive. Do this by just continuing processing
7785 without changing if_stack ; this is so that the error message
7786 for missing #endif's etc. will point to the original #if. It
7787 is possible that something different would be better. */
7790 do_else (buf, limit, op, keyword)
7791 U_CHAR *buf, *limit;
7793 struct directive *keyword ATTRIBUTE_UNUSED;
7795 FILE_BUF *ip = &instack[indepth];
7798 SKIP_WHITE_SPACE (buf);
7800 pedwarn ("text following `#else' violates ANSI standard");
7803 if (if_stack == instack[indepth].if_stack) {
7804 error ("`#else' not within a conditional");
7807 /* #ifndef can't have its special treatment for containing the whole file
7808 if it has a #else clause. */
7809 if_stack->control_macro = 0;
7811 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7812 error ("`#else' after `#else'");
7813 fprintf (stderr, " (matches line %d", if_stack->lineno);
7814 if (! (if_stack->fname_len == ip->nominal_fname_len
7815 && !bcmp (if_stack->fname, ip->nominal_fname,
7816 if_stack->fname_len))) {
7817 fprintf (stderr, ", file ");
7818 eprint_string (if_stack->fname, if_stack->fname_len);
7820 fprintf (stderr, ")\n");
7822 if_stack->type = T_ELSE;
7825 if (if_stack->if_succeeded)
7826 skip_if_group (ip, 0, op);
7828 ++if_stack->if_succeeded; /* continue processing input */
7829 output_line_directive (ip, op, 1, same_file);
7834 /* Unstack after #endif directive. */
7837 do_endif (buf, limit, op, keyword)
7838 U_CHAR *buf, *limit;
7840 struct directive *keyword ATTRIBUTE_UNUSED;
7843 SKIP_WHITE_SPACE (buf);
7845 pedwarn ("text following `#endif' violates ANSI standard");
7848 if (if_stack == instack[indepth].if_stack)
7849 error ("unbalanced `#endif'");
7851 IF_STACK_FRAME *temp = if_stack;
7852 if_stack = if_stack->next;
7853 if (temp->control_macro != 0) {
7854 /* This #endif matched a #ifndef at the start of the file.
7855 See if it is at the end of the file. */
7856 FILE_BUF *ip = &instack[indepth];
7857 U_CHAR *p = ip->bufp;
7858 U_CHAR *ep = ip->buf + ip->length;
7864 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7865 /* Skip this comment. */
7867 U_CHAR *save_bufp = ip->bufp;
7869 p = skip_to_end_of_comment (ip, &junk, 1);
7870 ip->bufp = save_bufp;
7875 /* If we get here, this #endif ends a #ifndef
7876 that contains all of the file (aside from whitespace).
7877 Arrange not to include the file again
7878 if the macro that was tested is defined.
7880 Do not do this for the top-level file in a -include or any
7881 file in a -imacros. */
7883 && ! (indepth == 1 && no_record_file)
7884 && ! (no_record_file && no_output))
7885 record_control_macro (ip->inc, temp->control_macro);
7889 output_line_directive (&instack[indepth], op, 1, same_file);
7894 /* When an #else or #endif is found while skipping failed conditional,
7895 if -pedantic was specified, this is called to warn about text after
7896 the directive name. P points to the first char after the directive
7900 validate_else (p, limit)
7902 register U_CHAR *limit;
7904 /* Advance P over whitespace and comments. */
7906 while (*p == '\\' && p[1] == '\n')
7908 if (is_hor_space[*p])
7910 else if (*p == '/') {
7911 while (p[1] == '\\' && p[2] == '\n')
7914 /* Don't bother warning about unterminated comments
7915 since that will happen later. Just be sure to exit. */
7916 for (p += 2; ; p++) {
7920 while (p[1] == '\\' && p[2] == '\n')
7929 #ifdef MULTIBYTE_CHARS
7931 length = local_mblen (p, limit - p);
7938 else if (cplusplus_comments && p[1] == '/')
7944 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
7947 /* Skip a comment, assuming the input ptr immediately follows the
7948 initial slash-star. Bump *LINE_COUNTER for each newline.
7949 (The canonical line counter is &ip->lineno.)
7950 Don't use this routine (or the next one) if bumping the line
7951 counter is not sufficient to deal with newlines in the string.
7953 If NOWARN is nonzero, don't warn about slash-star inside a comment.
7954 This feature is useful when processing a comment that is going to
7955 be processed or was processed at another point in the preprocessor,
7956 to avoid a duplicate warning. Likewise for unterminated comment
7960 skip_to_end_of_comment (ip, line_counter, nowarn)
7961 register FILE_BUF *ip;
7962 int *line_counter; /* place to remember newlines, or NULL */
7965 register U_CHAR *limit = ip->buf + ip->length;
7966 register U_CHAR *bp = ip->bufp;
7967 FILE_BUF *op = put_out_comments && !line_counter ? &outbuf : (FILE_BUF *) 0;
7968 int start_line = line_counter ? *line_counter : 0;
7970 /* JF this line_counter stuff is a crock to make sure the
7971 comment is only put out once, no matter how many times
7972 the comment is skipped. It almost works */
7975 *op->bufp++ = bp[-1];
7977 if (cplusplus_comments && bp[-1] == '/') {
7978 for (; bp < limit; bp++) {
7982 if (!nowarn && warn_comments)
7983 warning ("multiline `//' comment");
7991 #ifdef MULTIBYTE_CHARS
7993 length = local_mblen (bp, limit - bp);
7998 bcopy (bp, op->bufp, length - 1);
7999 op->bufp += (length - 1);
8011 while (bp < limit) {
8016 /* If this is the end of the file, we have an unterminated comment.
8017 Don't swallow the newline. We are guaranteed that there will be a
8018 trailing newline and various pieces assume it's there. */
8025 if (line_counter != NULL)
8031 if (bp[-2] == '/' && !nowarn && warn_comments)
8032 warning ("`/*' within comment");
8033 if (*bp == '\\' && bp[1] == '\n')
8042 #ifdef MULTIBYTE_CHARS
8047 length = local_mblen (bp, limit - bp);
8053 bcopy (bp, op->bufp, length);
8063 error_with_line (line_for_error (start_line), "unterminated comment");
8068 /* Skip over a quoted string. BP points to the opening quote.
8069 Returns a pointer after the closing quote. Don't go past LIMIT.
8070 START_LINE is the line number of the starting point (but it need
8071 not be valid if the starting point is inside a macro expansion).
8073 The input stack state is not changed.
8075 If COUNT_NEWLINES is nonzero, it points to an int to increment
8076 for each newline passed.
8078 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8079 if we pass a backslash-newline.
8081 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8084 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
8085 register U_CHAR *bp;
8086 register U_CHAR *limit;
8088 int *count_newlines;
8089 int *backslash_newlines_p;
8092 register U_CHAR c, match;
8097 error_with_line (line_for_error (start_line),
8098 "unterminated string or character constant");
8099 error_with_line (multiline_string_line,
8100 "possible real start of unterminated constant");
8101 multiline_string_line = 0;
8108 while (*bp == '\\' && bp[1] == '\n') {
8109 if (backslash_newlines_p)
8110 *backslash_newlines_p = 1;
8116 if (backslash_newlines_p)
8117 *backslash_newlines_p = 1;
8122 } else if (c == '\n') {
8124 /* Unterminated strings and character constants are 'valid'. */
8125 bp--; /* Don't consume the newline. */
8130 if (match == '\'') {
8131 error_with_line (line_for_error (start_line),
8132 "unterminated string or character constant");
8138 /* If not traditional, then allow newlines inside strings. */
8141 if (multiline_string_line == 0) {
8143 pedwarn_with_line (line_for_error (start_line),
8144 "string constant runs past end of line");
8145 multiline_string_line = start_line;
8147 } else if (c == match)
8149 #ifdef MULTIBYTE_CHARS
8153 length = local_mblen (bp, limit - bp);
8163 /* Place into DST a quoted string representing the string SRC.
8164 SRCLEN is the length of SRC; SRC may contain null bytes.
8165 Return the address of DST's terminating null. */
8168 quote_string (dst, src, srclen)
8173 char *srclim = src + srclen;
8176 while (src != srclim)
8177 switch ((c = *src++))
8184 sprintf (dst, "\\%03o", c);
8201 /* Skip across a group of balanced parens, starting from IP->bufp.
8202 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8204 This does not handle newlines, because it's used for the arg of #if,
8205 where there aren't any newlines. Also, backslash-newline can't appear. */
8208 skip_paren_group (ip)
8209 register FILE_BUF *ip;
8211 U_CHAR *limit = ip->buf + ip->length;
8212 U_CHAR *p = ip->bufp;
8214 int lines_dummy = 0;
8216 while (p != limit) {
8226 return ip->bufp = p;
8232 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
8240 p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
8242 return ip->bufp = p;
8252 /* Write out a #line directive, for instance, after an #include file.
8253 If CONDITIONAL is nonzero, we can omit the #line if it would
8254 appear to be a no-op, and we can output a few newlines instead
8255 if we want to increase the line number by a small amount.
8256 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8259 output_line_directive (ip, op, conditional, file_change)
8262 enum file_change_code file_change;
8265 char *line_directive_buf, *line_end;
8267 if (no_line_directives
8268 || ip->fname == NULL
8270 op->lineno = ip->lineno;
8275 if (ip->lineno == op->lineno)
8278 /* If the inherited line number is a little too small,
8279 output some newlines instead of a #line directive. */
8280 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
8281 check_expand (op, 10);
8282 while (ip->lineno > op->lineno) {
8290 /* Output a positive line number if possible. */
8291 while (ip->lineno <= 0 && ip->bufp - ip->buf < ip->length
8292 && *ip->bufp == '\n') {
8297 line_directive_buf = (char *) alloca (4 * ip->nominal_fname_len + 100);
8298 sprintf (line_directive_buf, "# %d ", ip->lineno);
8299 line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
8300 ip->nominal_fname, ip->nominal_fname_len);
8301 if (file_change != same_file) {
8303 *line_end++ = file_change == enter_file ? '1' : '2';
8305 /* Tell cc1 if following text comes from a system header file. */
8306 if (ip->system_header_p) {
8310 #ifndef NO_IMPLICIT_EXTERN_C
8311 /* Tell cc1plus if following text should be treated as C. */
8312 if (ip->system_header_p == 2 && cplusplus) {
8318 len = line_end - line_directive_buf;
8319 check_expand (op, len + 1);
8320 if (op->bufp > op->buf && op->bufp[-1] != '\n')
8322 bcopy ((char *) line_directive_buf, (char *) op->bufp, len);
8324 op->lineno = ip->lineno;
8327 /* This structure represents one parsed argument in a macro call.
8328 `raw' points to the argument text as written (`raw_length' is its length).
8329 `expanded' points to the argument's macro-expansion
8330 (its length is `expand_length').
8331 `stringified_length' is the length the argument would have
8333 `use_count' is the number of times this macro arg is substituted
8334 into the macro. If the actual use count exceeds 10,
8335 the value stored is 10.
8336 `free1' and `free2', if nonzero, point to blocks to be freed
8337 when the macro argument data is no longer needed. */
8340 U_CHAR *raw, *expanded;
8341 int raw_length, expand_length;
8342 int stringified_length;
8343 U_CHAR *free1, *free2;
8348 /* Expand a macro call.
8349 HP points to the symbol that is the macro being called.
8350 Put the result of expansion onto the input stack
8351 so that subsequent input by our caller will use it.
8353 If macro wants arguments, caller has already verified that
8354 an argument list follows; arguments come from the input stack. */
8357 macroexpand (hp, op)
8362 DEFINITION *defn = hp->value.defn;
8363 register U_CHAR *xbuf;
8365 int start_line = instack[indepth].lineno;
8366 int rest_args, rest_zero;
8368 CHECK_DEPTH (return;);
8370 /* it might not actually be a macro. */
8371 if (hp->type != T_MACRO) {
8372 special_symbol (hp, op);
8376 /* This macro is being used inside a #if, which means it must be */
8377 /* recorded as a precondition. */
8378 if (pcp_inside_if && pcp_outfile && defn->predefined)
8379 dump_single_macro (hp, pcp_outfile);
8381 nargs = defn->nargs;
8385 struct argdata *args;
8386 char *parse_error = 0;
8388 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
8390 for (i = 0; i < nargs; i++) {
8391 args[i].raw = (U_CHAR *) "";
8392 args[i].expanded = 0;
8393 args[i].raw_length = args[i].expand_length
8394 = args[i].stringified_length = 0;
8395 args[i].free1 = args[i].free2 = 0;
8396 args[i].use_count = 0;
8399 /* Parse all the macro args that are supplied. I counts them.
8400 The first NARGS args are stored in ARGS.
8401 The rest are discarded.
8402 If rest_args is set then we assume macarg absorbed the rest of the args.
8407 /* Discard the open-parenthesis or comma before the next arg. */
8408 ++instack[indepth].bufp;
8411 if (i < nargs || (nargs == 0 && i == 0)) {
8412 /* If we are working on last arg which absorbs rest of args... */
8413 if (i == nargs - 1 && defn->rest_args)
8415 parse_error = macarg (&args[i], rest_args);
8418 parse_error = macarg (NULL_PTR, 0);
8420 error_with_line (line_for_error (start_line), parse_error);
8424 } while (*instack[indepth].bufp != ')');
8426 /* If we got one arg but it was just whitespace, call that 0 args. */
8428 register U_CHAR *bp = args[0].raw;
8429 register U_CHAR *lim = bp + args[0].raw_length;
8430 /* cpp.texi says for foo ( ) we provide one argument.
8431 However, if foo wants just 0 arguments, treat this as 0. */
8433 while (bp != lim && is_space[*bp]) bp++;
8438 /* Don't output an error message if we have already output one for
8439 a parse error above. */
8441 if (nargs == 0 && i > 0) {
8443 error ("arguments given to macro `%s'", hp->name);
8444 } else if (i < nargs) {
8445 /* traditional C allows foo() if foo wants one argument. */
8446 if (nargs == 1 && i == 0 && traditional)
8448 /* the rest args token is allowed to absorb 0 tokens */
8449 else if (i == nargs - 1 && defn->rest_args)
8451 else if (parse_error)
8454 error ("macro `%s' used without args", hp->name);
8456 error ("macro `%s' used with just one arg", hp->name);
8458 error ("macro `%s' used with only %d args", hp->name, i);
8459 } else if (i > nargs) {
8461 error ("macro `%s' used with too many (%d) args", hp->name, i);
8464 /* Swallow the closeparen. */
8465 ++instack[indepth].bufp;
8467 /* If macro wants zero args, we parsed the arglist for checking only.
8468 Read directly from the macro definition. */
8470 xbuf = defn->expansion;
8471 xbuf_len = defn->length;
8473 register U_CHAR *exp = defn->expansion;
8474 register int offset; /* offset in expansion,
8475 copied a piece at a time */
8476 register int totlen; /* total amount of exp buffer filled so far */
8478 register struct reflist *ap, *last_ap;
8480 /* Macro really takes args. Compute the expansion of this call. */
8482 /* Compute length in characters of the macro's expansion.
8483 Also count number of times each arg is used. */
8484 xbuf_len = defn->length;
8485 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
8487 xbuf_len += args[ap->argno].stringified_length;
8488 else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
8489 /* Add 4 for two newline-space markers to prevent
8490 token concatenation. */
8491 xbuf_len += args[ap->argno].raw_length + 4;
8493 /* We have an ordinary (expanded) occurrence of the arg.
8494 So compute its expansion, if we have not already. */
8495 if (args[ap->argno].expanded == 0) {
8497 obuf = expand_to_temp_buffer (args[ap->argno].raw,
8498 args[ap->argno].raw + args[ap->argno].raw_length,
8501 args[ap->argno].expanded = obuf.buf;
8502 args[ap->argno].expand_length = obuf.length;
8503 args[ap->argno].free2 = obuf.buf;
8506 /* Add 4 for two newline-space markers to prevent
8507 token concatenation. */
8508 xbuf_len += args[ap->argno].expand_length + 4;
8510 if (args[ap->argno].use_count < 10)
8511 args[ap->argno].use_count++;
8514 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
8516 /* Generate in XBUF the complete expansion
8517 with arguments substituted in.
8518 TOTLEN is the total size generated so far.
8519 OFFSET is the index in the definition
8520 of where we are copying from. */
8521 offset = totlen = 0;
8522 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
8523 last_ap = ap, ap = ap->next) {
8524 register struct argdata *arg = &args[ap->argno];
8525 int count_before = totlen;
8527 /* Add chars to XBUF. */
8528 for (i = 0; i < ap->nchars; i++, offset++)
8529 xbuf[totlen++] = exp[offset];
8531 /* If followed by an empty rest arg with concatenation,
8532 delete the last run of nonwhite chars. */
8533 if (rest_zero && totlen > count_before
8534 && ((ap->rest_args && ap->raw_before != 0)
8535 || (last_ap != NULL && last_ap->rest_args
8536 && last_ap->raw_after != 0))) {
8537 /* Delete final whitespace. */
8538 while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
8542 /* Delete the nonwhites before them. */
8543 while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
8548 if (ap->stringify != 0) {
8549 int arglen = arg->raw_length;
8555 && (c = arg->raw[i], is_space[c]))
8558 && (c = arg->raw[arglen - 1], is_space[c]))
8561 xbuf[totlen++] = '\"'; /* insert beginning quote */
8562 for (; i < arglen; i++) {
8566 /* Special markers Newline Space
8567 generate nothing for a stringified argument. */
8568 if (c == '\n' && arg->raw[i+1] != '\n') {
8573 /* Internal sequences of whitespace are replaced by one space
8574 except within an string or char token. */
8575 if (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c]) {
8577 /* Note that Newline Space does occur within whitespace
8578 sequences; consider it part of the sequence. */
8579 if (c == '\n' && is_space[arg->raw[i+1]])
8581 else if (c != '\n' && is_space[c])
8596 else if (in_string) {
8601 #ifdef MULTIBYTE_CHARS
8603 length = local_mblen (arg->raw + i, arglen - i);
8606 bcopy (arg->raw + i, xbuf + totlen, length);
8613 } else if (c == '\"' || c == '\'')
8617 /* Escape these chars */
8618 if (c == '\"' || (in_string && c == '\\'))
8619 xbuf[totlen++] = '\\';
8620 /* We used to output e.g. \008 for control characters here,
8621 but this doesn't conform to the C Standard.
8622 Just output the characters as-is. */
8626 xbuf[totlen++] = '\"'; /* insert ending quote */
8627 } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
8628 U_CHAR *p1 = arg->raw;
8629 U_CHAR *l1 = p1 + arg->raw_length;
8630 if (ap->raw_before != 0) {
8631 while (p1 != l1 && is_space[*p1]) p1++;
8632 while (p1 != l1 && is_idchar[*p1])
8633 xbuf[totlen++] = *p1++;
8634 /* Delete any no-reexpansion marker that follows
8635 an identifier at the beginning of the argument
8636 if the argument is concatenated with what precedes it. */
8637 if (p1[0] == '\n' && p1[1] == '-')
8639 } else if (!traditional) {
8640 /* Ordinary expanded use of the argument.
8641 Put in newline-space markers to prevent token pasting. */
8642 xbuf[totlen++] = '\n';
8643 xbuf[totlen++] = ' ';
8645 if (ap->raw_after != 0) {
8646 /* Arg is concatenated after: delete trailing whitespace,
8647 whitespace markers, and no-reexpansion markers. */
8649 if (is_space[l1[-1]]) l1--;
8650 else if (l1[-1] == '-') {
8651 U_CHAR *p2 = l1 - 1;
8652 /* If a `-' is preceded by an odd number of newlines then it
8653 and the last newline are a no-reexpansion marker. */
8654 while (p2 != p1 && p2[-1] == '\n') p2--;
8655 if ((l1 - 1 - p2) & 1) {
8664 bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
8666 if (!traditional && ap->raw_after == 0) {
8667 /* Ordinary expanded use of the argument.
8668 Put in newline-space markers to prevent token pasting. */
8669 xbuf[totlen++] = '\n';
8670 xbuf[totlen++] = ' ';
8673 /* Ordinary expanded use of the argument.
8674 Put in newline-space markers to prevent token pasting. */
8676 xbuf[totlen++] = '\n';
8677 xbuf[totlen++] = ' ';
8679 bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
8680 arg->expand_length);
8681 totlen += arg->expand_length;
8683 xbuf[totlen++] = '\n';
8684 xbuf[totlen++] = ' ';
8686 /* If a macro argument with newlines is used multiple times,
8687 then only expand the newlines once. This avoids creating output
8688 lines which don't correspond to any input line, which confuses
8690 if (arg->use_count > 1 && arg->newlines > 0) {
8691 /* Don't bother doing change_newlines for subsequent
8695 = change_newlines (arg->expanded, arg->expand_length);
8699 if (totlen > xbuf_len)
8703 /* If there is anything left of the definition after handling
8704 the arg list, copy that in too. */
8706 for (i = offset; i < defn->length; i++) {
8707 /* if we've reached the end of the macro */
8710 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
8711 && last_ap->raw_after != 0))
8712 xbuf[totlen++] = exp[i];
8718 for (i = 0; i < nargs; i++) {
8719 if (args[i].free1 != 0)
8720 free (args[i].free1);
8721 if (args[i].free2 != 0)
8722 free (args[i].free2);
8726 xbuf = defn->expansion;
8727 xbuf_len = defn->length;
8730 /* Now put the expansion on the input stack
8731 so our caller will commence reading from it. */
8733 register FILE_BUF *ip2;
8735 ip2 = &instack[++indepth];
8738 ip2->nominal_fname = 0;
8739 ip2->nominal_fname_len = 0;
8741 /* This may not be exactly correct, but will give much better error
8742 messages for nested macro calls than using a line number of zero. */
8743 ip2->lineno = start_line;
8745 ip2->length = xbuf_len;
8747 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
8749 ip2->if_stack = if_stack;
8750 ip2->system_header_p = 0;
8752 /* Recursive macro use sometimes works traditionally.
8753 #define foo(x,y) bar (x (y,0), y)
8757 hp->type = T_DISABLED;
8761 /* Parse a macro argument and store the info on it into *ARGPTR.
8762 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8763 Return nonzero to indicate a syntax error. */
8766 macarg (argptr, rest_args)
8767 register struct argdata *argptr;
8770 FILE_BUF *ip = &instack[indepth];
8776 /* Try to parse as much of the argument as exists at this
8777 input stack level. */
8778 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro,
8779 &paren, &newlines, &comments, rest_args);
8781 /* If we find the end of the argument at this level,
8782 set up *ARGPTR to point at it in the input stack. */
8783 if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
8784 && bp != ip->buf + ip->length) {
8786 argptr->raw = ip->bufp;
8787 argptr->raw_length = bp - ip->bufp;
8788 argptr->newlines = newlines;
8792 /* This input stack level ends before the macro argument does.
8793 We must pop levels and keep parsing.
8794 Therefore, we must allocate a temporary buffer and copy
8795 the macro argument into it. */
8796 int bufsize = bp - ip->bufp;
8797 int extra = newlines;
8798 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
8799 int final_start = 0;
8801 bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
8803 ip->lineno += newlines;
8805 while (bp == ip->buf + ip->length) {
8806 if (instack[indepth].macro == 0) {
8807 result = "unterminated macro call";
8810 ip->macro->type = T_MACRO;
8812 free (ip->free_ptr);
8813 ip = &instack[--indepth];
8816 bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro, &paren,
8817 &newlines, &comments, rest_args);
8818 final_start = bufsize;
8819 bufsize += bp - ip->bufp;
8821 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
8822 bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
8825 ip->lineno += newlines;
8828 /* Now, if arg is actually wanted, record its raw form,
8829 discarding comments and duplicating newlines in whatever
8830 part of it did not come from a macro expansion.
8831 EXTRA space has been preallocated for duplicating the newlines.
8832 FINAL_START is the index of the start of that part. */
8834 argptr->raw = buffer;
8835 argptr->raw_length = bufsize;
8836 argptr->free1 = buffer;
8837 argptr->newlines = newlines;
8838 if ((newlines || comments) && ip->fname != 0)
8841 discard_comments (argptr->raw + final_start,
8842 argptr->raw_length - final_start,
8844 argptr->raw[argptr->raw_length] = 0;
8845 if (argptr->raw_length > bufsize + extra)
8850 /* If we are not discarding this argument,
8851 macroexpand it and compute its length as stringified.
8852 All this info goes into *ARGPTR. */
8855 register U_CHAR *buf, *lim;
8856 register int totlen;
8859 lim = buf + argptr->raw_length;
8861 while (buf != lim && is_space[*buf])
8863 while (buf != lim && is_space[lim[-1]])
8865 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
8866 while (buf != lim) {
8867 register U_CHAR c = *buf++;
8869 /* Internal sequences of whitespace are replaced by one space
8870 in most cases, but not always. So count all the whitespace
8871 in case we need to keep it all. */
8874 SKIP_ALL_WHITE_SPACE (buf);
8877 if (c == '\"' || c == '\\') /* escape these chars */
8880 argptr->stringified_length = totlen;
8885 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8886 taken from the expansion of MACRO,
8887 counting parens in *DEPTHPTR,
8888 and return if reach LIMIT
8889 or before a `)' that would make *DEPTHPTR negative
8890 or before a comma when *DEPTHPTR is zero.
8891 Single and double quotes are matched and termination
8892 is inhibited within them. Comments also inhibit it.
8893 Value returned is pointer to stopping place.
8895 Increment *NEWLINES each time a newline is passed.
8896 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8897 Set *COMMENTS to 1 if a comment is seen. */
8900 macarg1 (start, limit, macro, depthptr, newlines, comments, rest_args)
8902 register U_CHAR *limit;
8903 struct hashnode *macro;
8904 int *depthptr, *newlines, *comments;
8907 register U_CHAR *bp = start;
8909 while (bp < limit) {
8915 if (--(*depthptr) < 0)
8919 /* Traditionally, backslash makes following char not special. */
8920 if (traditional && bp + 1 < limit && bp[1] != '\n')
8929 if (bp[1] == '\\' && bp[2] == '\n')
8930 newline_fix (bp + 1);
8933 for (bp += 2; bp < limit; bp++) {
8936 else if (*bp == '*') {
8937 if (bp[-1] == '/' && warn_comments)
8938 warning ("`/*' within comment");
8939 if (bp[1] == '\\' && bp[2] == '\n')
8940 newline_fix (bp + 1);
8948 #ifdef MULTIBYTE_CHARS
8950 length = local_mblen (bp, limit - bp);
8956 } else if (bp[1] == '/' && cplusplus_comments) {
8958 for (bp += 2; bp < limit; bp++) {
8964 warning ("multiline `//' comment");
8968 #ifdef MULTIBYTE_CHARS
8970 length = local_mblen (bp, limit - bp);
8982 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
8988 while (*bp == '\\' && bp[1] == '\n') {
8993 } else if (*bp == '\n') {
9000 #ifdef MULTIBYTE_CHARS
9002 length = local_mblen (bp, limit - bp);
9011 /* if we've returned to lowest level and we aren't absorbing all args */
9012 if ((*depthptr) == 0 && rest_args == 0)
9022 /* Discard comments and duplicate newlines
9023 in the string of length LENGTH at START,
9024 except inside of string constants.
9025 The string is copied into itself with its beginning staying fixed.
9027 NEWLINES is the number of newlines that must be duplicated.
9028 We assume that that much extra space is available past the end
9032 discard_comments (start, length, newlines)
9037 register U_CHAR *ibp;
9038 register U_CHAR *obp;
9039 register U_CHAR *limit;
9042 /* If we have newlines to duplicate, copy everything
9043 that many characters up. Then, in the second part,
9044 we will have room to insert the newlines
9046 NEWLINES may actually be too large, because it counts
9047 newlines in string constants, and we don't duplicate those.
9048 But that does no harm. */
9050 ibp = start + length;
9051 obp = ibp + newlines;
9053 while (limit != ibp)
9057 ibp = start + newlines;
9058 limit = start + length + newlines;
9061 while (ibp < limit) {
9062 *obp++ = c = *ibp++;
9065 /* Duplicate the newline. */
9077 if (*ibp == '\\' && ibp[1] == '\n')
9079 /* Delete any comment. */
9080 if (cplusplus_comments && ibp[0] == '/') {
9081 /* Comments are equivalent to spaces. */
9088 if (ibp[-1] != '\\')
9093 #ifdef MULTIBYTE_CHARS
9094 int length = local_mblen (ibp, limit - ibp);
9096 ibp += (length - 1);
9103 if (ibp[0] != '*' || ibp + 1 >= limit)
9105 /* Comments are equivalent to spaces.
9106 For -traditional, a comment is equivalent to nothing. */
9111 while (++ibp < limit) {
9112 if (ibp[0] == '*') {
9113 if (ibp[1] == '\\' && ibp[2] == '\n')
9114 newline_fix (ibp + 1);
9115 if (ibp[1] == '/') {
9122 #ifdef MULTIBYTE_CHARS
9123 int length = local_mblen (ibp, limit - ibp);
9125 ibp += (length - 1);
9133 /* Notice and skip strings, so that we don't
9134 think that comments start inside them,
9135 and so we don't duplicate newlines in them. */
9138 while (ibp < limit) {
9139 *obp++ = c = *ibp++;
9147 else if (c == '\\') {
9148 if (ibp < limit && *ibp == '\n') {
9152 while (*ibp == '\\' && ibp[1] == '\n')
9160 #ifdef MULTIBYTE_CHARS
9163 length = local_mblen (ibp, limit - ibp);
9167 bcopy (ibp, obp, length);
9184 /* Turn newlines to spaces in the string of length LENGTH at START,
9185 except inside of string constants.
9186 The string is copied into itself with its beginning staying fixed. */
9189 change_newlines (start, length)
9193 register U_CHAR *ibp;
9194 register U_CHAR *obp;
9195 register U_CHAR *limit;
9199 limit = start + length;
9202 while (ibp < limit) {
9203 *obp++ = c = *ibp++;
9206 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9207 string. Skip past the newline and its duplicate.
9208 Put a space in the output. */
9219 /* Notice and skip strings, so that we don't delete newlines in them. */
9222 while (ibp < limit) {
9223 *obp++ = c = *ibp++;
9226 if (ibp[-2] != '\\')
9236 #ifdef MULTIBYTE_CHARS
9239 length = local_mblen (ibp, limit - ibp);
9243 bcopy (ibp, obp, length);
9260 /* my_strerror - return the descriptive text associated with an
9264 my_strerror (errnum)
9270 #ifndef HAVE_STRERROR
9271 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
9273 result = strerror (errnum);
9276 /* VAXCRTL's strerror() takes an optional second argument, which only
9277 matters when the first argument is EVMSERR. However, it's simplest
9278 just to pass it unconditionally. `vaxc$errno' is declared in
9279 <errno.h>, and maintained by the library in parallel with `errno'.
9280 We assume that caller's `errnum' either matches the last setting of
9281 `errno' by the library or else does not have the value `EVMSERR'. */
9283 result = strerror (errnum, vaxc$errno);
9287 result = "undocumented I/O error";
9292 /* error - print error message and increment count of errors. */
9295 error VPROTO ((char * msg, ...))
9302 VA_START (args, msg);
9305 msg = va_arg (args, char *);
9318 FILE_BUF *ip = NULL;
9320 print_containing_files ();
9322 for (i = indepth; i >= 0; i--)
9323 if (instack[i].fname != NULL) {
9329 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9330 fprintf (stderr, ":%d: ", ip->lineno);
9332 vfprintf (stderr, msg, args);
9333 fprintf (stderr, "\n");
9337 /* Error including a message from `errno'. */
9340 error_from_errno (name)
9345 FILE_BUF *ip = NULL;
9347 print_containing_files ();
9349 for (i = indepth; i >= 0; i--)
9350 if (instack[i].fname != NULL) {
9356 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9357 fprintf (stderr, ":%d: ", ip->lineno);
9360 fprintf (stderr, "%s: %s\n", name, my_strerror (e));
9365 /* Print error message but don't count it. */
9368 warning VPROTO ((char * msg, ...))
9375 VA_START (args, msg);
9378 msg = va_arg (args, char *);
9381 vwarning (msg, args);
9386 vwarning (msg, args)
9391 FILE_BUF *ip = NULL;
9393 if (inhibit_warnings)
9396 if (warnings_are_errors)
9399 print_containing_files ();
9401 for (i = indepth; i >= 0; i--)
9402 if (instack[i].fname != NULL) {
9408 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9409 fprintf (stderr, ":%d: ", ip->lineno);
9411 fprintf (stderr, "warning: ");
9412 vfprintf (stderr, msg, args);
9413 fprintf (stderr, "\n");
9417 error_with_line VPROTO ((int line, char * msg, ...))
9425 VA_START (args, msg);
9428 line = va_arg (args, int);
9429 msg = va_arg (args, char *);
9432 verror_with_line (line, msg, args);
9437 verror_with_line (line, msg, args)
9443 FILE_BUF *ip = NULL;
9445 print_containing_files ();
9447 for (i = indepth; i >= 0; i--)
9448 if (instack[i].fname != NULL) {
9454 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9455 fprintf (stderr, ":%d: ", line);
9457 vfprintf (stderr, msg, args);
9458 fprintf (stderr, "\n");
9463 warning_with_line VPROTO ((int line, char * msg, ...))
9471 VA_START (args, msg);
9474 line = va_arg (args, int);
9475 msg = va_arg (args, char *);
9478 vwarning_with_line (line, msg, args);
9483 vwarning_with_line (line, msg, args)
9489 FILE_BUF *ip = NULL;
9491 if (inhibit_warnings)
9494 if (warnings_are_errors)
9497 print_containing_files ();
9499 for (i = indepth; i >= 0; i--)
9500 if (instack[i].fname != NULL) {
9506 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9507 fprintf (stderr, line ? ":%d: " : ": ", line);
9509 fprintf (stderr, "warning: ");
9510 vfprintf (stderr, msg, args);
9511 fprintf (stderr, "\n");
9514 /* Print an error message and maybe count it. */
9517 pedwarn VPROTO ((char * msg, ...))
9524 VA_START (args, msg);
9527 msg = va_arg (args, char *);
9530 if (pedantic_errors)
9533 vwarning (msg, args);
9538 pedwarn_with_line VPROTO ((int line, char * msg, ...))
9546 VA_START (args, msg);
9549 line = va_arg (args, int);
9550 msg = va_arg (args, char *);
9553 if (pedantic_errors)
9554 verror_with_line (line, msg, args);
9556 vwarning_with_line (line, msg, args);
9560 /* Report a warning (or an error if pedantic_errors)
9561 giving specified file name and line number, not current. */
9564 pedwarn_with_file_and_line VPROTO ((char *file, size_t file_len, int line,
9575 if (!pedantic_errors && inhibit_warnings)
9578 eprint_string (file, file_len);
9579 fprintf (stderr, ":%d: ", line);
9581 if (pedantic_errors)
9583 if (!pedantic_errors)
9584 fprintf (stderr, "warning: ");
9585 VA_START (args, msg);
9588 file = va_arg (args, char *);
9589 file_len = va_arg (args, size_t);
9590 line = va_arg (args, int);
9591 msg = va_arg (args, char *);
9594 vfprintf (stderr, msg, args);
9596 fprintf (stderr, "\n");
9599 /* Print the file names and line numbers of the #include
9600 directives which led to the current file. */
9603 print_containing_files ()
9605 FILE_BUF *ip = NULL;
9609 /* If stack of files hasn't changed since we last printed
9610 this info, don't repeat it. */
9611 if (last_error_tick == input_file_stack_tick)
9614 for (i = indepth; i >= 0; i--)
9615 if (instack[i].fname != NULL) {
9620 /* Give up if we don't find a source file. */
9624 /* Find the other, outer source files. */
9625 for (i--; i >= 0; i--)
9626 if (instack[i].fname != NULL) {
9630 fprintf (stderr, "In file included");
9632 fprintf (stderr, ",\n ");
9635 fprintf (stderr, " from ");
9636 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9637 fprintf (stderr, ":%d", ip->lineno);
9640 fprintf (stderr, ":\n");
9642 /* Record we have printed the status as of this time. */
9643 last_error_tick = input_file_stack_tick;
9646 /* Return the line at which an error occurred.
9647 The error is not necessarily associated with the current spot
9648 in the input stack, so LINE says where. LINE will have been
9649 copied from ip->lineno for the current input level.
9650 If the current level is for a file, we return LINE.
9651 But if the current level is not for a file, LINE is meaningless.
9652 In that case, we return the lineno of the innermost file. */
9655 line_for_error (line)
9661 for (i = indepth; i >= 0; ) {
9662 if (instack[i].fname != 0)
9667 line1 = instack[i].lineno;
9675 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9677 * As things stand, nothing is ever placed in the output buffer to be
9678 * removed again except when it's KNOWN to be part of an identifier,
9679 * so flushing and moving down everything left, instead of expanding,
9683 /* You might think void was cleaner for the return type,
9684 but that would get type mismatch in check_expand in strict ANSI. */
9687 grow_outbuf (obuf, needed)
9688 register FILE_BUF *obuf;
9689 register int needed;
9694 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
9697 /* Make it at least twice as big as it is now. */
9699 /* Make it have at least 150% of the free space we will need. */
9700 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
9701 if (minsize > obuf->length)
9702 obuf->length = minsize;
9704 if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
9707 obuf->bufp = p + (obuf->bufp - obuf->buf);
9713 /* Symbol table for macro names and special symbols */
9716 * install a name in the main hash table, even if it is already there.
9717 * name stops with first non alphanumeric, except leading '#'.
9718 * caller must check against redefinition if that is desired.
9719 * delete_macro () removes things installed by install () in fifo order.
9720 * this is important because of the `defined' special symbol used
9721 * in #if, and also if pushdef/popdef directives are ever implemented.
9723 * If LEN is >= 0, it is the length of the name.
9724 * Otherwise, compute the length by scanning the entire name.
9726 * If HASH is >= 0, it is the precomputed hash code.
9727 * Otherwise, compute the hash code.
9731 install (name, len, type, value, hash)
9734 enum node_type type;
9738 register HASHNODE *hp;
9739 register int i, bucket;
9740 register U_CHAR *p, *q;
9744 while (is_idchar[*p])
9750 hash = hashf (name, len, HASHSIZE);
9752 i = sizeof (HASHNODE) + len + 1;
9753 hp = (HASHNODE *) xmalloc (i);
9755 hp->bucket_hdr = &hashtab[bucket];
9756 hp->next = hashtab[bucket];
9757 hashtab[bucket] = hp;
9759 if (hp->next != NULL)
9760 hp->next->prev = hp;
9763 hp->value.cpval = value;
9764 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
9767 for (i = 0; i < len; i++)
9774 * find the most recent hash node for name "name" (ending with first
9775 * non-identifier char) installed by install
9777 * If LEN is >= 0, it is the length of the name.
9778 * Otherwise, compute the length by scanning the entire name.
9780 * If HASH is >= 0, it is the precomputed hash code.
9781 * Otherwise, compute the hash code.
9785 lookup (name, len, hash)
9790 register U_CHAR *bp;
9791 register HASHNODE *bucket;
9794 for (bp = name; is_idchar[*bp]; bp++) ;
9799 hash = hashf (name, len, HASHSIZE);
9801 bucket = hashtab[hash];
9803 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
9805 bucket = bucket->next;
9811 * Delete a hash node. Some weirdness to free junk from macros.
9812 * More such weirdness will have to be added if you define more hash
9813 * types that need it.
9816 /* Note that the DEFINITION of a macro is removed from the hash table
9817 but its storage is not freed. This would be a storage leak
9818 except that it is not reasonable to keep undefining and redefining
9819 large numbers of macros many times.
9820 In any case, this is necessary, because a macro can be #undef'd
9821 in the middle of reading the arguments to a call to it.
9822 If #undef freed the DEFINITION, that would crash. */
9829 if (hp->prev != NULL)
9830 hp->prev->next = hp->next;
9831 if (hp->next != NULL)
9832 hp->next->prev = hp->prev;
9834 /* Make sure that the bucket chain header that the deleted guy was
9835 on points to the right thing afterwards. */
9836 if (hp == *hp->bucket_hdr)
9837 *hp->bucket_hdr = hp->next;
9840 if (hp->type == T_MACRO) {
9841 DEFINITION *d = hp->value.defn;
9842 struct reflist *ap, *nextap;
9844 for (ap = d->pattern; ap != NULL; ap = nextap) {
9855 * return hash function on name. must be compatible with the one
9856 * computed a step at a time, elsewhere
9860 hashf (name, len, hashsize)
9861 register U_CHAR *name;
9868 r = HASHSTEP (r, *name++);
9870 return MAKE_POS (r) % hashsize;
9874 /* Dump the definition of a single macro HP to OF. */
9877 dump_single_macro (hp, of)
9878 register HASHNODE *hp;
9881 register DEFINITION *defn = hp->value.defn;
9887 /* Print the definition of the macro HP. */
9889 fprintf (of, "#define %s", hp->name);
9891 if (defn->nargs >= 0) {
9895 for (i = 0; i < defn->nargs; i++) {
9896 dump_arg_n (defn, i, of);
9897 if (i + 1 < defn->nargs)
9907 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
9908 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
9909 offset += ap->nchars;
9911 if (ap->nchars != 0)
9913 if (ap->stringify) {
9914 switch (ap->stringify) {
9915 case SHARP_TOKEN: fprintf (of, "#"); break;
9916 case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
9917 case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
9918 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
9922 if (ap->raw_before != 0) {
9924 switch (ap->raw_before) {
9925 case WHITE_SHARP_TOKEN:
9926 case WHITE_PERCENT_COLON_TOKEN:
9933 switch (ap->raw_before) {
9934 case SHARP_TOKEN: fprintf (of, "##"); break;
9935 case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
9936 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
9937 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
9944 dump_arg_n (defn, ap->argno, of);
9945 if (!traditional && ap->raw_after != 0) {
9946 switch (ap->raw_after) {
9947 case SHARP_TOKEN: fprintf (of, "##"); break;
9948 case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
9949 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
9950 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
9956 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
9960 /* Dump all macro definitions as #defines to stdout. */
9967 for (bucket = 0; bucket < HASHSIZE; bucket++) {
9968 register HASHNODE *hp;
9970 for (hp = hashtab[bucket]; hp; hp= hp->next) {
9971 if (hp->type == T_MACRO)
9972 dump_single_macro (hp, stdout);
9977 /* Output to OF a substring of a macro definition.
9978 BASE is the beginning of the definition.
9979 Output characters START thru LENGTH.
9980 Unless traditional, discard newlines outside of strings, thus
9981 converting funny-space markers to ordinary spaces. */
9984 dump_defn_1 (base, start, length, of)
9990 U_CHAR *p = base + start;
9991 U_CHAR *limit = base + start + length;
9994 fwrite (p, sizeof (*p), length, of);
9997 if (*p == '\"' || *p =='\'') {
9998 U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
9999 NULL_PTR, NULL_PTR);
10000 fwrite (p, sizeof (*p), p1 - p, of);
10011 /* Print the name of argument number ARGNUM of macro definition DEFN
10013 Recall that DEFN->args.argnames contains all the arg names
10014 concatenated in reverse order with comma-space in between. */
10017 dump_arg_n (defn, argnum, of)
10022 register U_CHAR *p = defn->args.argnames;
10023 while (argnum + 1 < defn->nargs) {
10024 p = (U_CHAR *) index ((char *) p, ' ') + 1;
10028 while (*p && *p != ',') {
10034 /* Initialize syntactic classifications of characters. */
10037 initialize_char_syntax ()
10042 * Set up is_idchar and is_idstart tables. These should be
10043 * faster than saying (is_alpha (c) || c == '_'), etc.
10044 * Set up these things before calling any routines tthat
10047 for (i = 'a'; i <= 'z'; i++) {
10048 is_idchar[i - 'a' + 'A'] = 1;
10050 is_idstart[i - 'a' + 'A'] = 1;
10053 for (i = '0'; i <= '9'; i++)
10055 is_idchar['_'] = 1;
10056 is_idstart['_'] = 1;
10057 is_idchar['$'] = 1;
10058 is_idstart['$'] = 1;
10060 /* horizontal space table */
10061 is_hor_space[' '] = 1;
10062 is_hor_space['\t'] = 1;
10063 is_hor_space['\v'] = 1;
10064 is_hor_space['\f'] = 1;
10065 is_hor_space['\r'] = 1;
10068 is_space['\t'] = 1;
10069 is_space['\v'] = 1;
10070 is_space['\f'] = 1;
10071 is_space['\n'] = 1;
10072 is_space['\r'] = 1;
10074 char_name['\v'] = "vertical tab";
10075 char_name['\f'] = "formfeed";
10076 char_name['\r'] = "carriage return";
10079 /* Initialize the built-in macros. */
10082 initialize_builtins (inp, outp)
10086 install ((U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
10087 install ((U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
10088 install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
10089 install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
10090 install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
10091 install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
10092 #ifndef NO_BUILTIN_SIZE_TYPE
10093 install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
10095 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10096 install ((U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
10098 install ((U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
10099 install ((U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
10101 install ((U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
10103 install ((U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
10105 install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
10106 if (!traditional) {
10107 install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
10108 install ((U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
10111 install ((U_CHAR *) "__OBJC__", -1, T_CONST, "1", -1);
10112 /* This is supplied using a -D by the compiler driver
10113 so that it is present only when truly compiling with GNU C. */
10114 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10115 install ((U_CHAR *) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST, "1", -1);
10119 char directive[2048];
10120 U_CHAR *udirective = (U_CHAR *) directive;
10121 register struct directive *dp = &directive_table[0];
10122 struct tm *timebuf = timestamp ();
10124 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
10125 instack[0].nominal_fname);
10126 output_line_directive (inp, outp, 0, same_file);
10127 pass_thru_directive (udirective, &udirective[strlen (directive)],
10130 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
10131 output_line_directive (inp, outp, 0, same_file);
10132 pass_thru_directive (udirective, &udirective[strlen (directive)],
10135 #ifndef NO_BUILTIN_SIZE_TYPE
10136 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
10137 output_line_directive (inp, outp, 0, same_file);
10138 pass_thru_directive (udirective, &udirective[strlen (directive)],
10142 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10143 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
10144 output_line_directive (inp, outp, 0, same_file);
10145 pass_thru_directive (udirective, &udirective[strlen (directive)],
10149 sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
10150 output_line_directive (inp, outp, 0, same_file);
10151 pass_thru_directive (udirective, &udirective[strlen (directive)],
10154 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
10155 monthnames[timebuf->tm_mon],
10156 timebuf->tm_mday, timebuf->tm_year + 1900);
10157 output_line_directive (inp, outp, 0, same_file);
10158 pass_thru_directive (udirective, &udirective[strlen (directive)],
10161 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
10162 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
10163 output_line_directive (inp, outp, 0, same_file);
10164 pass_thru_directive (udirective, &udirective[strlen (directive)],
10169 sprintf (directive, " __STDC__ 1");
10170 output_line_directive (inp, outp, 0, same_file);
10171 pass_thru_directive (udirective, &udirective[strlen (directive)],
10176 sprintf (directive, " __OBJC__ 1");
10177 output_line_directive (inp, outp, 0, same_file);
10178 pass_thru_directive (udirective, &udirective[strlen (directive)],
10185 * process a given definition string, for initialization
10186 * If STR is just an identifier, define it with value 1.
10187 * If STR has anything after the identifier, then it should
10188 * be identifier=definition.
10192 make_definition (str, op)
10197 struct directive *kt;
10200 p = buf = (U_CHAR *) str;
10201 if (!is_idstart[*p]) {
10202 error ("malformed option `-D %s'", str);
10205 while (is_idchar[*++p])
10208 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
10211 p = (U_CHAR *) str; /* Error */
10214 buf = (U_CHAR *) alloca (p - buf + 4);
10215 strcpy ((char *)buf, str);
10216 strcat ((char *)buf, " 1");
10217 } else if (*p != '=') {
10218 error ("malformed option `-D %s'", str);
10222 /* Copy the entire option so we can modify it. */
10223 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
10224 strncpy ((char *) buf, str, p - (U_CHAR *) str);
10225 /* Change the = to a space. */
10226 buf[p - (U_CHAR *) str] = ' ';
10227 /* Scan for any backslash-newline and remove it. */
10229 q = &buf[p - (U_CHAR *) str];
10231 if (*p == '\"' || *p == '\'') {
10232 int unterminated = 0;
10233 U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
10234 NULL_PTR, NULL_PTR, &unterminated);
10239 } else if (*p == '\\' && p[1] == '\n')
10241 /* Change newline chars into newline-markers. */
10242 else if (*p == '\n')
10254 ip = &instack[++indepth];
10255 ip->nominal_fname = ip->fname = "*Initialization*";
10256 ip->nominal_fname_len = strlen (ip->nominal_fname);
10258 ip->buf = ip->bufp = buf;
10259 ip->length = strlen ((char *) buf);
10263 ip->if_stack = if_stack;
10264 ip->system_header_p = 0;
10266 for (kt = directive_table; kt->type != T_DEFINE; kt++)
10269 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10270 do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
10274 /* JF, this does the work for the -U option */
10277 make_undef (str, op)
10282 struct directive *kt;
10284 ip = &instack[++indepth];
10285 ip->nominal_fname = ip->fname = "*undef*";
10286 ip->nominal_fname_len = strlen (ip->nominal_fname);
10288 ip->buf = ip->bufp = (U_CHAR *) str;
10289 ip->length = strlen (str);
10293 ip->if_stack = if_stack;
10294 ip->system_header_p = 0;
10296 for (kt = directive_table; kt->type != T_UNDEF; kt++)
10299 do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
10303 /* Process the string STR as if it appeared as the body of a #assert.
10304 OPTION is the option name for which STR was the argument. */
10307 make_assertion (option, str)
10312 struct directive *kt;
10313 U_CHAR *buf, *p, *q;
10315 /* Copy the entire option so we can modify it. */
10316 buf = (U_CHAR *) alloca (strlen (str) + 1);
10317 strcpy ((char *) buf, str);
10318 /* Scan for any backslash-newline and remove it. */
10321 if (*p == '\\' && p[1] == '\n')
10329 if (!is_idstart[*p]) {
10330 error ("malformed option `%s %s'", option, str);
10333 while (is_idchar[*++p])
10335 SKIP_WHITE_SPACE (p);
10336 if (! (*p == 0 || *p == '(')) {
10337 error ("malformed option `%s %s'", option, str);
10341 ip = &instack[++indepth];
10342 ip->nominal_fname = ip->fname = "*Initialization*";
10343 ip->nominal_fname_len = strlen (ip->nominal_fname);
10345 ip->buf = ip->bufp = buf;
10346 ip->length = strlen ((char *) buf);
10350 ip->if_stack = if_stack;
10351 ip->system_header_p = 0;
10353 for (kt = directive_table; kt->type != T_ASSERT; kt++)
10356 /* Pass NULL as output ptr to do_define since we KNOW it never does
10358 do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
10362 #ifndef DIR_SEPARATOR
10363 #define DIR_SEPARATOR '/'
10366 /* The previous include prefix, if any, is PREV_FILE_NAME.
10367 Translate any pathnames with COMPONENT.
10368 Allocate a new include prefix whose name is the
10369 simplified concatenation of PREFIX and NAME,
10370 with a trailing / added if needed.
10371 But return 0 if the include prefix should be ignored,
10372 e.g. because it is a duplicate of PREV_FILE_NAME. */
10374 static struct file_name_list *
10375 new_include_prefix (prev_file_name, component, prefix, name)
10376 struct file_name_list *prev_file_name;
10382 fatal ("Directory name missing after command line option");
10385 /* Ignore the empty string. */
10388 prefix = update_path (prefix, component);
10389 name = update_path (name, component);
10392 struct file_name_list *dir
10393 = ((struct file_name_list *)
10394 xmalloc (sizeof (struct file_name_list)
10395 + strlen (prefix) + strlen (name) + 2));
10397 strcpy (dir->fname, prefix);
10398 strcat (dir->fname, name);
10399 len = simplify_filename (dir->fname);
10401 /* Convert directory name to a prefix. */
10402 if (len && dir->fname[len - 1] != DIR_SEPARATOR) {
10403 if (len == 1 && dir->fname[len - 1] == '.')
10407 /* must be '/', hack_vms_include_specification triggers on it. */
10408 dir->fname[len++] = '/';
10410 dir->fname[len++] = DIR_SEPARATOR;
10412 dir->fname[len] = 0;
10415 /* Ignore a directory whose name matches the previous one. */
10416 if (prev_file_name && !strcmp (prev_file_name->fname, dir->fname)) {
10417 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10418 if (!first_bracket_include)
10419 first_bracket_include = prev_file_name;
10425 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10427 /* Add a trailing "." if there is a filename. This increases the number
10428 of systems that can stat directories. We remove it below. */
10431 dir->fname[len] = '.';
10432 dir->fname[len + 1] = 0;
10435 /* Ignore a nonexistent directory. */
10436 if (stat (len ? dir->fname : ".", &dir->st) != 0) {
10437 if (errno != ENOENT && errno != ENOTDIR)
10438 error_from_errno (dir->fname);
10444 dir->fname[len] = 0;
10446 /* Ignore a directory whose identity matches the previous one. */
10448 && INO_T_EQ (prev_file_name->st.st_ino, dir->st.st_ino)
10449 && prev_file_name->st.st_dev == dir->st.st_dev) {
10450 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10451 if (!first_bracket_include)
10452 first_bracket_include = prev_file_name;
10459 dir->c_system_include_path = 0;
10460 dir->got_name_map = 0;
10466 /* Append a chain of `struct file_name_list's
10467 to the end of the main include chain.
10468 FIRST is the beginning of the chain to append, and LAST is the end. */
10471 append_include_chain (first, last)
10472 struct file_name_list *first, *last;
10474 struct file_name_list *dir;
10476 if (!first || !last)
10482 last_include->next = first;
10484 if (first_bracket_include == 0)
10485 first_bracket_include = first;
10487 for (dir = first; ; dir = dir->next) {
10488 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
10489 if (len > max_include_len)
10490 max_include_len = len;
10496 last_include = last;
10499 /* Place into DST a representation of the file named SRC that is suitable
10500 for `make'. Do not null-terminate DST. Return its length. */
10502 quote_string_for_make (dst, src)
10517 /* GNU make uses a weird quoting scheme for white space.
10518 A space or tab preceded by 2N+1 backslashes represents
10519 N backslashes followed by space; a space or tab
10520 preceded by 2N backslashes represents N backslashes at
10521 the end of a file name; and backslashes in other
10522 contexts should not be doubled. */
10524 for (q = p - 1; src < q && q[-1] == '\\'; q--)
10536 goto ordinary_char;
10542 /* Fall through. This can mishandle things like "$(" but
10543 there's no easy fix. */
10546 /* This can mishandle characters in the string "\0\n%*?[\\~";
10547 exactly which chars are mishandled depends on the `make' version.
10548 We know of no portable solution for this;
10549 even GNU make 3.76.1 doesn't solve the problem entirely.
10550 (Also, '\0' is mishandled due to our calling conventions.) */
10560 /* Add output to `deps_buffer' for the -M switch.
10561 STRING points to the text to be output.
10562 SPACER is ':' for targets, ' ' for dependencies. */
10565 deps_output (string, spacer)
10569 int size = quote_string_for_make ((char *) 0, string);
10574 #ifndef MAX_OUTPUT_COLUMNS
10575 #define MAX_OUTPUT_COLUMNS 72
10577 if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
10578 && 1 < deps_column) {
10579 bcopy (" \\\n ", &deps_buffer[deps_size], 4);
10586 if (deps_size + 2 * size + 8 > deps_allocated_size) {
10587 deps_allocated_size = (deps_size + 2 * size + 50) * 2;
10588 deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
10590 if (spacer == ' ') {
10591 deps_buffer[deps_size++] = ' ';
10594 quote_string_for_make (&deps_buffer[deps_size], string);
10596 deps_column += size;
10597 if (spacer == ':') {
10598 deps_buffer[deps_size++] = ':';
10601 deps_buffer[deps_size] = 0;
10605 fatal VPROTO ((char * msg, ...))
10612 fprintf (stderr, "%s: ", progname);
10613 VA_START (args, msg);
10616 msg = va_arg (args, char *);
10619 vfprintf (stderr, msg, args);
10621 fprintf (stderr, "\n");
10622 exit (FATAL_EXIT_CODE);
10625 /* More 'friendly' abort that prints the line and file.
10626 config.h can #define abort fancy_abort if you like that sort of thing. */
10631 fatal ("Internal gcc abort.");
10635 perror_with_name (name)
10638 fprintf (stderr, "%s: %s: %s\n", progname, name, my_strerror (errno));
10643 pfatal_with_name (name)
10646 perror_with_name (name);
10650 exit (FATAL_EXIT_CODE);
10654 /* Handler for SIGPIPE. */
10657 pipe_closed (signo)
10658 /* If this is missing, some compilers complain. */
10659 int signo ATTRIBUTE_UNUSED;
10661 fatal ("output pipe has been closed");
10667 fatal ("Memory exhausted.");
10675 register GENERIC_PTR ptr = (GENERIC_PTR) malloc (size);
10682 xrealloc (old, size)
10686 register GENERIC_PTR ptr = (GENERIC_PTR) realloc (old, size);
10693 xcalloc (number, size)
10694 size_t number, size;
10696 register size_t total = number * size;
10697 register GENERIC_PTR ptr = (GENERIC_PTR) malloc (total);
10700 bzero (ptr, total);
10708 size_t size = strlen (input);
10709 char *output = xmalloc (size + 1);
10710 strcpy (output, input);
10716 /* Under VMS we need to fix up the "include" specification filename.
10718 Rules for possible conversions
10720 fullname tried paths
10723 ./dir/name [.dir]name
10725 /name [000000]name, name
10726 dir/name dir:[000000]name, dir:name, dir/name
10727 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10728 path:/name path:[000000]name, path:name
10729 path:/dir/name path:[000000.dir]name, path:[dir]name
10730 path:dir/name path:[dir]name
10731 [path]:[dir]name [path.dir]name
10732 path/[dir]name [path.dir]name
10734 The path:/name input is constructed when expanding <> includes.
10736 return 1 if name was changed, 0 else. */
10739 hack_vms_include_specification (fullname, vaxc_include)
10743 register char *basename, *unixname, *local_ptr, *first_slash;
10744 int f, check_filename_before_returning, must_revert;
10747 check_filename_before_returning = 0;
10749 /* See if we can find a 1st slash. If not, there's no path information. */
10750 first_slash = index (fullname, '/');
10751 if (first_slash == 0)
10752 return 0; /* Nothing to do!!! */
10754 /* construct device spec if none given. */
10756 if (index (fullname, ':') == 0)
10759 /* If fullname has a slash, take it as device spec. */
10761 if (first_slash == fullname)
10763 first_slash = index (fullname+1, '/'); /* 2nd slash ? */
10765 *first_slash = ':'; /* make device spec */
10766 for (basename = fullname; *basename != 0; basename++)
10767 *basename = *(basename+1); /* remove leading slash */
10769 else if ((first_slash[-1] != '.') /* keep ':/', './' */
10770 && (first_slash[-1] != ':')
10771 && (first_slash[-1] != ']')) /* or a vms path */
10773 *first_slash = ':';
10775 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
10776 && (first_slash[-1] == '.'))
10780 /* Get part after first ':' (basename[-1] == ':')
10781 or last '/' (basename[-1] == '/'). */
10783 basename = base_name (fullname);
10786 * Check if we have a vax-c style '#include filename'
10787 * and add the missing .h
10790 if (vaxc_include && !index (basename,'.'))
10791 strcat (basename, ".h");
10793 local_ptr = Local; /* initialize */
10795 /* We are trying to do a number of things here. First of all, we are
10796 trying to hammer the filenames into a standard format, such that later
10797 processing can handle them.
10799 If the file name contains something like [dir.], then it recognizes this
10800 as a root, and strips the ".]". Later processing will add whatever is
10801 needed to get things working properly.
10803 If no device is specified, then the first directory name is taken to be
10804 a device name (or a rooted logical). */
10806 /* Point to the UNIX filename part (which needs to be fixed!)
10807 but skip vms path information.
10808 [basename != fullname since first_slash != 0]. */
10810 if ((basename[-1] == ':') /* vms path spec. */
10811 || (basename[-1] == ']')
10812 || (basename[-1] == '>'))
10813 unixname = basename;
10815 unixname = fullname;
10817 if (*unixname == '/')
10820 /* If the directory spec is not rooted, we can just copy
10821 the UNIX filename part and we are done. */
10823 if (((basename - fullname) > 1)
10824 && ( (basename[-1] == ']')
10825 || (basename[-1] == '>')))
10827 if (basename[-2] != '.')
10830 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10831 -> PATH]:/name (basename = '/name', unixname = 'name')
10832 We strip the `]', and then splice the two parts of the name in the
10833 usual way. Given the default locations for include files in cccp.c,
10834 we will only use this code if the user specifies alternate locations
10835 with the /include (-I) switch on the command line. */
10837 basename -= 1; /* Strip "]" */
10838 unixname--; /* backspace */
10843 /* The VMS part has a ".]" at the end, and this will not do. Later
10844 processing will add a second directory spec, and this would be a syntax
10845 error. Thus we strip the ".]", and thus merge the directory specs.
10846 We also backspace unixname, so that it points to a '/'. This inhibits the
10847 generation of the 000000 root directory spec (which does not belong here
10850 basename -= 2; /* Strip ".]" */
10851 unixname--; /* backspace */
10859 /* We drop in here if there is no VMS style directory specification yet.
10860 If there is no device specification either, we make the first dir a
10861 device and try that. If we do not do this, then we will be essentially
10862 searching the users default directory (as if they did a #include "asdf.h").
10864 Then all we need to do is to push a '[' into the output string. Later
10865 processing will fill this in, and close the bracket. */
10867 if ((unixname != fullname) /* vms path spec found. */
10868 && (basename[-1] != ':'))
10869 *local_ptr++ = ':'; /* dev not in spec. take first dir */
10871 *local_ptr++ = '['; /* Open the directory specification */
10874 if (unixname == fullname) /* no vms dir spec. */
10877 if ((first_slash != 0) /* unix dir spec. */
10878 && (*unixname != '/') /* not beginning with '/' */
10879 && (*unixname != '.')) /* or './' or '../' */
10880 *local_ptr++ = '.'; /* dir is local ! */
10883 /* at this point we assume that we have the device spec, and (at least
10884 the opening "[" for a directory specification. We may have directories
10887 If there are no other slashes then the filename will be
10888 in the "root" directory. Otherwise, we need to add
10889 directory specifications. */
10891 if (index (unixname, '/') == 0)
10893 /* if no directories specified yet and none are following. */
10894 if (local_ptr[-1] == '[')
10896 /* Just add "000000]" as the directory string */
10897 strcpy (local_ptr, "000000]");
10898 local_ptr += strlen (local_ptr);
10899 check_filename_before_returning = 1; /* we might need to fool with this later */
10905 /* As long as there are still subdirectories to add, do them. */
10906 while (index (unixname, '/') != 0)
10908 /* If this token is "." we can ignore it
10909 if it's not at the beginning of a path. */
10910 if ((unixname[0] == '.') && (unixname[1] == '/'))
10912 /* remove it at beginning of path. */
10913 if ( ((unixname == fullname) /* no device spec */
10914 && (fullname+2 != basename)) /* starts with ./ */
10916 || ((basename[-1] == ':') /* device spec */
10917 && (unixname-1 == basename))) /* and ./ afterwards */
10918 *local_ptr++ = '.'; /* make '[.' start of path. */
10923 /* Add a subdirectory spec. Do not duplicate "." */
10924 if ( local_ptr[-1] != '.'
10925 && local_ptr[-1] != '['
10926 && local_ptr[-1] != '<')
10927 *local_ptr++ = '.';
10929 /* If this is ".." then the spec becomes "-" */
10930 if ( (unixname[0] == '.')
10931 && (unixname[1] == '.')
10932 && (unixname[2] == '/'))
10934 /* Add "-" and skip the ".." */
10935 if ((local_ptr[-1] == '.')
10936 && (local_ptr[-2] == '['))
10937 local_ptr--; /* prevent [.- */
10938 *local_ptr++ = '-';
10943 /* Copy the subdirectory */
10944 while (*unixname != '/')
10945 *local_ptr++= *unixname++;
10947 unixname++; /* Skip the "/" */
10950 /* Close the directory specification */
10951 if (local_ptr[-1] == '.') /* no trailing periods */
10954 if (local_ptr[-1] == '[') /* no dir needed */
10957 *local_ptr++ = ']';
10960 /* Now add the filename. */
10963 *local_ptr++ = *unixname++;
10966 /* Now append it to the original VMS spec. */
10968 strcpy ((must_revert==1)?fullname:basename, Local);
10970 /* If we put a [000000] in the filename, try to open it first. If this fails,
10971 remove the [000000], and return that name. This provides flexibility
10972 to the user in that they can use both rooted and non-rooted logical names
10973 to point to the location of the file. */
10975 if (check_filename_before_returning)
10977 f = open (fullname, O_RDONLY, 0666);
10980 /* The file name is OK as it is, so return it as is. */
10985 /* The filename did not work. Try to remove the [000000] from the name,
10988 basename = index (fullname, '[');
10989 local_ptr = index (fullname, ']') + 1;
10990 strcpy (basename, local_ptr); /* this gets rid of it */
11000 /* The following wrapper functions supply additional arguments to the VMS
11001 I/O routines to optimize performance with file handling. The arguments
11003 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11004 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11005 "fop=tef"- Truncate unused portions of file when closing file.
11006 "shr=nil"- Disallow file sharing while file is open. */
11009 VMS_freopen (fname, type, oldfile)
11014 #undef freopen /* Get back the real freopen routine. */
11015 if (strcmp (type, "w") == 0)
11016 return freopen (fname, type, oldfile,
11017 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11018 return freopen (fname, type, oldfile, "mbc=16");
11022 VMS_fopen (fname, type)
11026 #undef fopen /* Get back the real fopen routine. */
11027 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11028 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11029 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11030 FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
11033 return (*vmslib_fopen) (fname, type, "mbc=32",
11034 "deq=64", "fop=tef", "shr=nil");
11036 return (*vmslib_fopen) (fname, type, "mbc=32");
11040 VMS_open (fname, flags, prot)
11045 #undef open /* Get back the real open routine. */
11046 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
11049 /* more VMS hackery */
11053 extern unsigned long SYS$PARSE(), SYS$SEARCH();
11055 /* Work around another library bug. If a file is located via a searchlist,
11056 and if the device it's on is not the same device as the one specified
11057 in the first element of that searchlist, then both stat() and fstat()
11058 will fail to return info about it. `errno' will be set to EVMSERR, and
11059 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11060 We can get around this by fully parsing the filename and then passing
11061 that absolute name to stat().
11063 Without this fix, we can end up failing to find header files, which is
11064 bad enough, but then compounding the problem by reporting the reason for
11065 failure as "normal successful completion." */
11067 #undef fstat /* Get back to the library version. */
11070 VMS_fstat (fd, statbuf)
11072 struct stat *statbuf;
11074 int result = fstat (fd, statbuf);
11079 char nambuf[NAM$C_MAXRSS+1];
11081 if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
11082 result = VMS_stat (nambuf, statbuf);
11083 /* No fclose(fp) here; that would close(fd) as well. */
11090 VMS_stat (name, statbuf)
11092 struct stat *statbuf;
11094 int result = stat (name, statbuf);
11100 char exp_nam[NAM$C_MAXRSS+1], /* expanded name buffer for SYS$PARSE */
11101 res_nam[NAM$C_MAXRSS+1]; /* resultant name buffer for SYS$SEARCH */
11104 fab.fab$l_fna = (char *) name;
11105 fab.fab$b_fns = (unsigned char) strlen (name);
11106 fab.fab$l_nam = (void *) &nam;
11108 nam.nam$l_esa = exp_nam, nam.nam$b_ess = sizeof exp_nam - 1;
11109 nam.nam$l_rsa = res_nam, nam.nam$b_rss = sizeof res_nam - 1;
11110 nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
11111 if (SYS$PARSE (&fab) & 1)
11113 if (SYS$SEARCH (&fab) & 1)
11115 res_nam[nam.nam$b_rsl] = '\0';
11116 result = stat (res_nam, statbuf);
11118 /* Clean up searchlist context cached by the system. */
11119 nam.nam$b_nop = NAM$M_SYNCHK;
11120 fab.fab$l_fna = 0, fab.fab$b_fns = 0;
11121 (void) SYS$PARSE (&fab);