1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92, 93, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding! */
24 typedef unsigned char U_CHAR;
28 #include "../src/config.h"
36 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
37 for the sake of machines with limited C compilers. */
40 #endif /* not EMACS */
42 #ifndef STANDARD_INCLUDE_DIR
43 #define STANDARD_INCLUDE_DIR "/usr/include"
46 #ifndef LOCAL_INCLUDE_DIR
47 #define LOCAL_INCLUDE_DIR "/usr/local/include"
50 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
52 #define PTR_INT_TYPE ptrdiff_t
54 #define PTR_INT_TYPE long
64 /* By default, colon separates directories in a path. */
65 #ifndef PATH_SEPARATOR
66 #define PATH_SEPARATOR ':'
69 /* In case config.h defines these. */
74 #include <sys/types.h>
82 #include <sys/time.h> /* for __DATE__ and __TIME__ */
83 #include <sys/resource.h>
90 /* This defines "errno" properly for VMS, and gives us EACCES. */
93 /* VMS-specific definitions */
97 #define O_RDONLY 0 /* Open arg for Read/Only */
98 #define O_WRONLY 1 /* Open arg for Write/Only */
99 #define read(fd,buf,size) VMS_read (fd,buf,size)
100 #define write(fd,buf,size) VMS_write (fd,buf,size)
101 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
102 #define fopen(fname,mode) VMS_fopen (fname,mode)
103 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
104 #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
105 static char * VMS_strncat ();
106 static int VMS_read ();
107 static int VMS_write ();
108 static int VMS_open ();
109 static FILE * VMS_fopen ();
110 static FILE * VMS_freopen ();
111 static void hack_vms_include_specification ();
112 typedef struct { unsigned :16, :16, :16; } vms_ino_t;
113 #define ino_t vms_ino_t
114 #define INCLUDE_LEN_FUDGE 10 /* leave room for VMS syntax conversion */
116 #define BSTRING /* VMS/GCC supplies the bstring routines */
117 #endif /* __GNUC__ */
120 extern char *index ();
121 extern char *rindex ();
129 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
130 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
132 /* Find the largest host integer type and set its size and type. */
134 #ifndef HOST_BITS_PER_WIDE_INT
136 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
137 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
138 #define HOST_WIDE_INT long
140 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
141 #define HOST_WIDE_INT int
147 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
151 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
154 /* Define a generic NULL if one hasn't already been defined. */
161 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
162 #define GENERIC_PTR void *
164 #define GENERIC_PTR char *
169 #define NULL_PTR ((GENERIC_PTR)0)
172 #ifndef INCLUDE_LEN_FUDGE
173 #define INCLUDE_LEN_FUDGE 0
176 /* Forward declarations. */
182 /* External declarations. */
184 extern char *getenv ();
185 extern FILE *fdopen ();
186 extern char *version_string;
187 extern struct tm *localtime ();
189 #ifndef HAVE_STRERROR
191 #if defined(bsd4_4) || defined(__NetBSD__)
192 extern const char *const sys_errlist[];
194 extern char *sys_errlist[];
196 #else /* HAVE_STERRROR */
200 char *strerror (int,...);
202 extern int parse_escape ();
208 /* Forward declarations. */
215 #if defined(USG) || defined(VMS)
223 /* These functions are declared to return int instead of void since they
224 are going to be placed in a table and some old compilers have trouble with
225 pointers to functions returning void. */
227 static int do_define ();
228 static int do_line ();
229 static int do_include ();
230 static int do_undef ();
231 static int do_error ();
232 static int do_pragma ();
233 static int do_ident ();
235 static int do_xifdef ();
236 static int do_else ();
237 static int do_elif ();
238 static int do_endif ();
239 static int do_sccs ();
240 static int do_once ();
241 static int do_assert ();
242 static int do_unassert ();
243 static int do_warning ();
245 static void add_import ();
246 static void append_include_chain ();
247 static void deps_output ();
248 static void make_undef ();
249 static void make_definition ();
250 static void make_assertion ();
251 static void path_include ();
252 static void initialize_builtins ();
253 static void initialize_char_syntax ();
254 static void dump_arg_n ();
255 static void dump_defn_1 ();
256 static void delete_macro ();
257 static void trigraph_pcp ();
258 static void rescan ();
259 static void finclude ();
260 static void validate_else ();
261 static int comp_def_part ();
262 static void error_from_errno ();
263 static void error_with_line ();
265 void pedwarn_with_line ();
266 static void pedwarn_with_file_and_line ();
267 static void fatal ();
269 static void pfatal_with_name ();
270 static void perror_with_name ();
271 static void pipe_closed ();
272 static void print_containing_files ();
273 static int lookup_import ();
274 static int redundant_include_p ();
275 static is_system_include ();
276 static struct file_name_map *read_name_map ();
277 static char *read_filename_string ();
278 static int open_include_file ();
279 static int check_preconditions ();
280 static void pcfinclude ();
281 static void pcstring_used ();
282 static void write_output ();
283 static int check_macro_name ();
284 static int compare_defs ();
285 static int compare_token_lists ();
286 static int eval_if_expression ();
287 static int discard_comments ();
288 static int change_newlines ();
289 static int line_for_error ();
291 static int file_size_and_mode ();
293 static struct arglist *read_token_list ();
294 static void free_token_list ();
296 static struct hashnode *install ();
297 struct hashnode *lookup ();
299 static struct assertion_hashnode *assertion_install ();
300 static struct assertion_hashnode *assertion_lookup ();
302 static char *xrealloc ();
303 static char *xcalloc ();
304 static char *savestring ();
306 static void delete_assertion ();
307 static void macroexpand ();
308 static void dump_all_macros ();
309 static void conditional_skip ();
310 static void skip_if_group ();
311 static void output_line_command ();
313 /* Last arg to output_line_command. */
314 enum file_change_code {same_file, enter_file, leave_file};
316 static int grow_outbuf ();
317 static int handle_directive ();
318 static void memory_full ();
320 static U_CHAR *macarg1 ();
321 static char *macarg ();
323 static U_CHAR *skip_to_end_of_comment ();
324 static U_CHAR *skip_quoted_string ();
325 static U_CHAR *skip_paren_group ();
326 static char *quote_string ();
328 static char *check_precompiled ();
329 /* static struct macrodef create_definition (); [moved below] */
330 static void dump_single_macro ();
331 static void output_dots ();
333 #ifndef FAILURE_EXIT_CODE
334 #define FAILURE_EXIT_CODE 33 /* gnu cc command understands this */
337 #ifndef SUCCESS_EXIT_CODE
338 #define SUCCESS_EXIT_CODE 0 /* 0 means success on Unix. */
341 /* Name under which this program was invoked. */
343 static char *progname;
345 /* Nonzero means use extra default include directories for C++. */
347 static int cplusplus;
349 /* Nonzero means handle cplusplus style comments */
351 static int cplusplus_comments;
353 /* Nonzero means handle #import, for objective C. */
357 /* Nonzero means this is an assembly file, and allow
358 unknown directives, which could be comments. */
362 /* Current maximum length of directory names in the search path
363 for include files. (Altered as we get more of them.) */
365 static int max_include_len;
367 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
369 static int for_lint = 0;
371 /* Nonzero means copy comments into the output file. */
373 static int put_out_comments = 0;
375 /* Nonzero means don't process the ANSI trigraph sequences. */
377 static int no_trigraphs = 0;
379 /* Nonzero means print the names of included files rather than
380 the preprocessed output. 1 means just the #include "...",
381 2 means #include <...> as well. */
383 static int print_deps = 0;
385 /* Nonzero if missing .h files in -M output are assumed to be generated
386 files and not errors. */
388 static int print_deps_missing_files = 0;
390 /* Nonzero means print names of header files (-H). */
392 static int print_include_names = 0;
394 /* Nonzero means don't output line number information. */
396 static int no_line_commands;
398 /* Nonzero means output the text in failing conditionals,
399 inside #failed ... #endfailed. */
401 static int output_conditionals;
403 /* dump_only means inhibit output of the preprocessed text
404 and instead output the definitions of all user-defined
405 macros in a form suitable for use as input to cccp.
406 dump_names means pass #define and the macro name through to output.
407 dump_definitions means pass the whole definition (plus #define) through
410 static enum {dump_none, dump_only, dump_names, dump_definitions}
411 dump_macros = dump_none;
413 /* Nonzero means pass all #define and #undef directives which we actually
414 process through to the output stream. This feature is used primarily
415 to allow cc1 to record the #defines and #undefs for the sake of
416 debuggers which understand about preprocessor macros, but it may
417 also be useful with -E to figure out how symbols are defined, and
418 where they are defined. */
419 static int debug_output = 0;
421 /* Nonzero indicates special processing used by the pcp program. The
422 special effects of this mode are:
424 Inhibit all macro expansion, except those inside #if directives.
426 Process #define directives normally, and output their contents
429 Output preconditions to pcp_outfile indicating all the relevant
430 preconditions for use of this file in a later cpp run.
432 static FILE *pcp_outfile;
434 /* Nonzero means we are inside an IF during a -pcp run. In this mode
435 macro expansion is done, and preconditions are output for all macro
436 uses requiring them. */
437 static int pcp_inside_if;
439 /* Nonzero means never to include precompiled files.
440 This is 1 since there's no way now to make precompiled files,
441 so it's not worth testing for them. */
442 static int no_precomp = 1;
444 /* Nonzero means give all the error messages the ANSI standard requires. */
448 /* Nonzero means try to make failure to fit ANSI C an error. */
450 static int pedantic_errors;
452 /* Nonzero means don't print warning messages. -w. */
454 static int inhibit_warnings = 0;
456 /* Nonzero means warn if slash-star appears in a comment. */
458 static int warn_comments;
460 /* Nonzero means warn if a macro argument is (or would be)
461 stringified with -traditional. */
463 static int warn_stringify;
465 /* Nonzero means warn if there are any trigraphs. */
467 static int warn_trigraphs;
469 /* Nonzero means warn if #import is used. */
471 static int warn_import = 1;
473 /* Nonzero means turn warnings into errors. */
475 static int warnings_are_errors;
477 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
481 /* Nonzero causes output not to be done,
482 but directives such as #define that have side effects
485 static int no_output;
487 /* Nonzero means this file was included with a -imacros or -include
488 command line and should not be recorded as an include file. */
490 static int no_record_file;
492 /* Nonzero means that we have finished processing the command line options.
493 This flag is used to decide whether or not to issue certain errors
496 static int done_initializing = 0;
498 /* Line where a newline was first seen in a string constant. */
500 static int multiline_string_line = 0;
502 /* I/O buffer structure.
503 The `fname' field is nonzero for source files and #include files
504 and for the dummy text used for -D and -U.
505 It is zero for rescanning results of macro expansion
506 and for expanding macro arguments. */
507 #define INPUT_STACK_MAX 400
508 static struct file_buf {
510 /* Filename specified with #line command. */
512 /* Record where in the search path this file was found.
513 For #include_next. */
514 struct file_name_list *dir;
519 /* Macro that this level is the expansion of.
520 Included so that we can reenable the macro
521 at the end of this level. */
522 struct hashnode *macro;
523 /* Value of if_stack at start of this file.
524 Used to prohibit unmatched #endif (etc) in an include file. */
525 struct if_stack *if_stack;
526 /* Object to be freed at end of input at this level. */
528 /* True if this is a header file included using <FILENAME>. */
529 char system_header_p;
530 } instack[INPUT_STACK_MAX];
532 static int last_error_tick; /* Incremented each time we print it. */
533 static int input_file_stack_tick; /* Incremented when the status changes. */
535 /* Current nesting level of input sources.
536 `instack[indepth]' is the level currently being read. */
537 static int indepth = -1;
538 #define CHECK_DEPTH(code) \
539 if (indepth >= (INPUT_STACK_MAX - 1)) \
541 error_with_line (line_for_error (instack[indepth].lineno), \
542 "macro or `#include' recursion too deep"); \
546 /* Current depth in #include directives that use <...>. */
547 static int system_include_depth = 0;
549 typedef struct file_buf FILE_BUF;
551 /* The output buffer. Its LENGTH field is the amount of room allocated
552 for the buffer, not the number of chars actually present. To get
553 that, subtract outbuf.buf from outbuf.bufp. */
555 #define OUTBUF_SIZE 10 /* initial size of output buffer */
556 static FILE_BUF outbuf;
558 /* Grow output buffer OBUF points at
559 so it can hold at least NEEDED more chars. */
561 #define check_expand(OBUF, NEEDED) \
562 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
563 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
565 struct file_name_list
567 struct file_name_list *next;
569 /* If the following is nonzero, it is a macro name.
570 Don't include the file again if that macro is defined. */
571 U_CHAR *control_macro;
572 /* If the following is nonzero, it is a C-language system include
574 int c_system_include_path;
575 /* Mapping of file names for this directory. */
576 struct file_name_map *name_map;
577 /* Non-zero if name_map is valid. */
581 /* #include "file" looks in source file dir, then stack. */
582 /* #include <file> just looks in the stack. */
583 /* -I directories are added to the end, then the defaults are added. */
585 static struct default_include {
586 char *fname; /* The name of the directory. */
587 int cplusplus; /* Only look here if we're compiling C++. */
588 int cxx_aware; /* Includes in this directory don't need to
589 be wrapped in extern "C" when compiling
591 } include_defaults_array[]
592 #ifdef INCLUDE_DEFAULTS
596 /* Pick up GNU C++ specific include files. */
597 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
599 /* This is the dir for fixincludes. Put it just before
600 the files that we fix. */
601 { GCC_INCLUDE_DIR, 0, 0 },
602 /* For cross-compilation, this dir name is generated
603 automatically in Makefile.in. */
604 { CROSS_INCLUDE_DIR, 0, 0 },
605 /* This is another place that the target system's headers might be. */
606 { TOOL_INCLUDE_DIR, 0, 1 },
607 #else /* not CROSS_COMPILE */
608 /* This should be /usr/local/include and should come before
609 the fixincludes-fixed header files. */
610 { LOCAL_INCLUDE_DIR, 0, 1 },
611 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
612 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
613 { TOOL_INCLUDE_DIR, 0, 1 },
614 /* This is the dir for fixincludes. Put it just before
615 the files that we fix. */
616 { GCC_INCLUDE_DIR, 0, 0 },
617 /* Some systems have an extra dir of include files. */
618 #ifdef SYSTEM_INCLUDE_DIR
619 { SYSTEM_INCLUDE_DIR, 0, 0 },
621 { STANDARD_INCLUDE_DIR, 0, 0 },
622 #endif /* not CROSS_COMPILE */
625 #endif /* no INCLUDE_DEFAULTS */
627 /* The code looks at the defaults through this pointer, rather than through
628 the constant structure above. This pointer gets changed if an environment
629 variable specifies other defaults. */
630 static struct default_include *include_defaults = include_defaults_array;
632 static struct file_name_list *include = 0; /* First dir to search */
633 /* First dir to search for <file> */
634 /* This is the first element to use for #include <...>.
635 If it is 0, use the entire chain for such includes. */
636 static struct file_name_list *first_bracket_include = 0;
637 /* This is the first element in the chain that corresponds to
638 a directory of system header files. */
639 static struct file_name_list *first_system_include = 0;
640 static struct file_name_list *last_include = 0; /* Last in chain */
642 /* Chain of include directories to put at the end of the other chain. */
643 static struct file_name_list *after_include = 0;
644 static struct file_name_list *last_after_include = 0; /* Last in chain */
646 /* Chain to put at the start of the system include files. */
647 static struct file_name_list *before_system = 0;
648 static struct file_name_list *last_before_system = 0; /* Last in chain */
650 /* List of included files that contained #pragma once. */
651 static struct file_name_list *dont_repeat_files = 0;
653 /* List of other included files.
654 If ->control_macro if nonzero, the file had a #ifndef
655 around the entire contents, and ->control_macro gives the macro name. */
656 static struct file_name_list *all_include_files = 0;
658 /* Directory prefix that should replace `/usr' in the standard
659 include file directories. */
660 static char *include_prefix;
662 /* Global list of strings read in from precompiled files. This list
663 is kept in the order the strings are read in, with new strings being
664 added at the end through stringlist_tailp. We use this list to output
665 the strings at the end of the run.
667 static STRINGDEF *stringlist;
668 static STRINGDEF **stringlist_tailp = &stringlist;
671 /* Structure returned by create_definition */
672 typedef struct macrodef MACRODEF;
675 struct definition *defn;
680 static struct macrodef create_definition ();
683 /* Structure allocated for every #define. For a simple replacement
686 nargs = -1, the `pattern' list is null, and the expansion is just
687 the replacement text. Nargs = 0 means a functionlike macro with no args,
689 #define getchar() getc (stdin) .
690 When there are args, the expansion is the replacement text with the
691 args squashed out, and the reflist is a list describing how to
692 build the output from the input: e.g., "3 chars, then the 1st arg,
693 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
694 The chars here come from the expansion. Whatever is left of the
695 expansion after the last arg-occurrence is copied after that arg.
696 Note that the reflist can be arbitrarily long---
697 its length depends on the number of times the arguments appear in
698 the replacement text, not how many args there are. Example:
699 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
701 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
702 where (x, y) means (nchars, argno). */
704 typedef struct definition DEFINITION;
707 int length; /* length of expansion string */
708 int predefined; /* True if the macro was builtin or */
709 /* came from the command line */
711 int line; /* Line number of definition */
712 char *file; /* File of definition */
713 char rest_args; /* Nonzero if last arg. absorbs the rest */
715 struct reflist *next;
716 char stringify; /* nonzero if this arg was preceded by a
718 char raw_before; /* Nonzero if a ## operator before arg. */
719 char raw_after; /* Nonzero if a ## operator after arg. */
720 char rest_args; /* Nonzero if this arg. absorbs the rest */
721 int nchars; /* Number of literal chars to copy before
722 this arg occurrence. */
723 int argno; /* Number of arg to substitute (origin-0) */
726 /* Names of macro args, concatenated in reverse order
727 with comma-space between them.
728 The only use of this is that we warn on redefinition
729 if this differs between the old and new definitions. */
734 /* different kinds of things that can appear in the value field
735 of a hash node. Actually, this may be useless now. */
744 * special extension string that can be added to the last macro argument to
745 * allow it to absorb the "rest" of the arguments when expanded. Ex:
746 * #define wow(a, b...) process (b, a, b)
747 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
748 * { wow (one, two); } -> { process (two, one, two); }
749 * if this "rest_arg" is used with the concat token '##' and if it is not
750 * supplied then the token attached to with ## will not be outputted. Ex:
751 * #define wow (a, b...) process (b ## , a, ## b)
752 * { wow (1, 2); } -> { process (2, 1, 2); }
753 * { wow (one); } -> { process (one); {
755 static char rest_extension[] = "...";
756 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
758 /* The structure of a node in the hash table. The hash table
759 has entries for all tokens defined by #define commands (type T_MACRO),
760 plus some special tokens like __LINE__ (these each have their own
761 type, and the appropriate code is run when that type of node is seen.
762 It does not contain control words like "#define", which are recognized
763 by a separate piece of code. */
765 /* different flavors of hash nodes --- also used in keyword table */
767 T_DEFINE = 1, /* the `#define' keyword */
768 T_INCLUDE, /* the `#include' keyword */
769 T_INCLUDE_NEXT, /* the `#include_next' keyword */
770 T_IMPORT, /* the `#import' keyword */
771 T_IFDEF, /* the `#ifdef' keyword */
772 T_IFNDEF, /* the `#ifndef' keyword */
773 T_IF, /* the `#if' keyword */
774 T_ELSE, /* `#else' */
775 T_PRAGMA, /* `#pragma' */
776 T_ELIF, /* `#elif' */
777 T_UNDEF, /* `#undef' */
778 T_LINE, /* `#line' */
779 T_ERROR, /* `#error' */
780 T_WARNING, /* `#warning' */
781 T_ENDIF, /* `#endif' */
782 T_SCCS, /* `#sccs', used on system V. */
783 T_IDENT, /* `#ident', used on system V. */
784 T_ASSERT, /* `#assert', taken from system V. */
785 T_UNASSERT, /* `#unassert', taken from system V. */
786 T_SPECLINE, /* special symbol `__LINE__' */
787 T_DATE, /* `__DATE__' */
788 T_FILE, /* `__FILE__' */
789 T_BASE_FILE, /* `__BASE_FILE__' */
790 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
791 T_VERSION, /* `__VERSION__' */
792 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
793 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
794 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
795 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
796 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
797 T_TIME, /* `__TIME__' */
798 T_CONST, /* Constant value, used by `__STDC__' */
799 T_MACRO, /* macro defined by `#define' */
800 T_DISABLED, /* macro temporarily turned off for rescan */
801 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
802 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
803 T_UNUSED /* Used for something not defined. */
807 struct hashnode *next; /* double links for easy deletion */
808 struct hashnode *prev;
809 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
810 chain is kept, in case the node is the head
811 of the chain and gets deleted. */
812 enum node_type type; /* type of special token */
813 int length; /* length of token, for quick comparison */
814 U_CHAR *name; /* the actual name */
815 union hashval value; /* pointer to expansion, or whatever */
818 typedef struct hashnode HASHNODE;
820 /* Some definitions for the hash table. The hash function MUST be
821 computed as shown in hashf () below. That is because the rescan
822 loop computes the hash value `on the fly' for most tokens,
823 in order to avoid the overhead of a lot of procedure calls to
824 the hashf () function. Hashf () only exists for the sake of
825 politeness, for use when speed isn't so important. */
827 #define HASHSIZE 1403
828 static HASHNODE *hashtab[HASHSIZE];
829 #define HASHSTEP(old, c) ((old << 2) + c)
830 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
832 /* Symbols to predefine. */
834 #ifdef CPP_PREDEFINES
835 static char *predefs = CPP_PREDEFINES;
837 static char *predefs = "";
840 /* We let tm.h override the types used here, to handle trivial differences
841 such as the choice of unsigned int or long unsigned int for size_t.
842 When machines start needing nontrivial differences in the size type,
843 it would be best to do something here to figure out automatically
844 from other information what type to use. */
846 /* The string value for __SIZE_TYPE__. */
849 #define SIZE_TYPE "long unsigned int"
852 /* The string value for __PTRDIFF_TYPE__. */
855 #define PTRDIFF_TYPE "long int"
858 /* The string value for __WCHAR_TYPE__. */
861 #define WCHAR_TYPE "int"
863 char * wchar_type = WCHAR_TYPE;
866 /* The string value for __USER_LABEL_PREFIX__ */
868 #ifndef USER_LABEL_PREFIX
869 #define USER_LABEL_PREFIX ""
872 /* The string value for __REGISTER_PREFIX__ */
874 #ifndef REGISTER_PREFIX
875 #define REGISTER_PREFIX ""
878 /* In the definition of a #assert name, this structure forms
879 a list of the individual values asserted.
880 Each value is itself a list of "tokens".
881 These are strings that are compared by name. */
883 struct tokenlist_list {
884 struct tokenlist_list *next;
885 struct arglist *tokens;
888 struct assertion_hashnode {
889 struct assertion_hashnode *next; /* double links for easy deletion */
890 struct assertion_hashnode *prev;
891 /* also, a back pointer to this node's hash
892 chain is kept, in case the node is the head
893 of the chain and gets deleted. */
894 struct assertion_hashnode **bucket_hdr;
895 int length; /* length of token, for quick comparison */
896 U_CHAR *name; /* the actual name */
897 /* List of token-sequences. */
898 struct tokenlist_list *value;
901 typedef struct assertion_hashnode ASSERTION_HASHNODE;
903 /* Some definitions for the hash table. The hash function MUST be
904 computed as shown in hashf below. That is because the rescan
905 loop computes the hash value `on the fly' for most tokens,
906 in order to avoid the overhead of a lot of procedure calls to
907 the hashf function. hashf only exists for the sake of
908 politeness, for use when speed isn't so important. */
910 #define ASSERTION_HASHSIZE 37
911 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
913 /* Nonzero means inhibit macroexpansion of what seem to be
914 assertion tests, in rescan. For #if. */
915 static int assertions_flag;
917 /* `struct directive' defines one #-directive, including how to handle it. */
920 int length; /* Length of name */
921 int (*func)(); /* Function to handle directive */
922 char *name; /* Name of directive */
923 enum node_type type; /* Code which describes which directive. */
924 char angle_brackets; /* Nonzero => <...> is special. */
925 char traditional_comments; /* Nonzero: keep comments if -traditional. */
926 char pass_thru; /* Copy preprocessed directive to output file. */
929 /* Here is the actual list of #-directives, most-often-used first. */
931 static struct directive directive_table[] = {
932 { 6, do_define, "define", T_DEFINE, 0, 1},
933 { 2, do_if, "if", T_IF},
934 { 5, do_xifdef, "ifdef", T_IFDEF},
935 { 6, do_xifdef, "ifndef", T_IFNDEF},
936 { 5, do_endif, "endif", T_ENDIF},
937 { 4, do_else, "else", T_ELSE},
938 { 4, do_elif, "elif", T_ELIF},
939 { 4, do_line, "line", T_LINE},
940 { 7, do_include, "include", T_INCLUDE, 1},
941 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
942 { 6, do_include, "import", T_IMPORT, 1},
943 { 5, do_undef, "undef", T_UNDEF},
944 { 5, do_error, "error", T_ERROR},
945 { 7, do_warning, "warning", T_WARNING},
946 #ifdef SCCS_DIRECTIVE
947 { 4, do_sccs, "sccs", T_SCCS},
949 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
950 { 5, do_ident, "ident", T_IDENT},
951 { 6, do_assert, "assert", T_ASSERT},
952 { 8, do_unassert, "unassert", T_UNASSERT},
953 { -1, 0, "", T_UNUSED},
956 /* When a directive handler is called,
957 this points to the # that started the directive. */
958 U_CHAR *directive_start;
960 /* table to tell if char can be part of a C identifier. */
961 U_CHAR is_idchar[256];
962 /* table to tell if char can be first char of a c identifier. */
963 U_CHAR is_idstart[256];
964 /* table to tell if c is horizontal space. */
965 U_CHAR is_hor_space[256];
966 /* table to tell if c is horizontal or vertical space. */
967 static U_CHAR is_space[256];
969 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
970 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
972 static int errors = 0; /* Error counter for exit code */
974 /* Name of output file, for error messages. */
975 static char *out_fname;
977 /* Zero means dollar signs are punctuation.
978 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
979 This must be 0 for correct processing of this ANSI C program:
981 #define lose(b) foo (b)
984 static int dollars_in_ident;
985 #ifndef DOLLARS_IN_IDENTIFIERS
986 #define DOLLARS_IN_IDENTIFIERS 1
989 static FILE_BUF expand_to_temp_buffer ();
991 static DEFINITION *collect_expansion ();
993 /* Stack of conditionals currently in progress
994 (including both successful and failing conditionals). */
997 struct if_stack *next; /* for chaining to the next stack frame */
998 char *fname; /* copied from input when frame is made */
999 int lineno; /* similarly */
1000 int if_succeeded; /* true if a leg of this if-group
1001 has been passed through rescan */
1002 U_CHAR *control_macro; /* For #ifndef at start of file,
1003 this is the macro name tested. */
1004 enum node_type type; /* type of last directive seen in this group */
1006 typedef struct if_stack IF_STACK_FRAME;
1007 static IF_STACK_FRAME *if_stack = NULL;
1009 /* Buffer of -M output. */
1010 static char *deps_buffer;
1012 /* Number of bytes allocated in above. */
1013 static int deps_allocated_size;
1015 /* Number of bytes used. */
1016 static int deps_size;
1018 /* Number of bytes since the last newline. */
1019 static int deps_column;
1021 /* Nonzero means -I- has been seen,
1022 so don't look for #include "foo" the source-file directory. */
1023 static int ignore_srcdir;
1025 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1026 retrying if necessary. Return a negative value if an error occurs,
1027 otherwise return the actual number of bytes read,
1028 which must be LEN unless end-of-file was reached. */
1031 safe_read (desc, ptr, len)
1038 int nchars = read (desc, ptr, left);
1055 /* Write LEN bytes at PTR to descriptor DESC,
1056 retrying if necessary, and treating any real error as fatal. */
1059 safe_write (desc, ptr, len)
1065 int written = write (desc, ptr, len);
1072 pfatal_with_name (out_fname);
1090 char **pend_files = (char **) xmalloc (argc * sizeof (char *));
1091 char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
1092 char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
1093 char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
1094 char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
1096 /* Record the option used with each element of pend_assertions.
1097 This is preparation for supporting more than one option for making
1099 char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
1100 int inhibit_predefs = 0;
1101 int no_standard_includes = 0;
1102 int no_standard_cplusplus_includes = 0;
1103 int missing_newline = 0;
1105 /* Non-0 means don't output the preprocessed program. */
1106 int inhibit_output = 0;
1107 /* Non-0 means -v, so print the full set of include dirs. */
1110 /* File name which deps are being written to.
1111 This is 0 if deps are being written to stdout. */
1112 char *deps_file = 0;
1113 /* Fopen file mode to open deps_file with. */
1114 char *deps_mode = "a";
1115 /* Stream on which to print the dependency information. */
1116 FILE *deps_stream = 0;
1117 /* Target-name to write with the dependency information. */
1118 char *deps_target = 0;
1121 /* Get rid of any avoidable limit on stack size. */
1125 /* Set the stack limit huge so that alloca (particularly stringtab
1126 * in dbxread.c) does not fail. */
1127 getrlimit (RLIMIT_STACK, &rlim);
1128 rlim.rlim_cur = rlim.rlim_max;
1129 setrlimit (RLIMIT_STACK, &rlim);
1131 #endif /* RLIMIT_STACK defined */
1134 signal (SIGPIPE, pipe_closed);
1137 p = argv[0] + strlen (argv[0]);
1138 while (p != argv[0] && p[-1] != '/') --p;
1143 /* Remove directories from PROGNAME. */
1146 progname = savestring (argv[0]);
1148 if (!(s = rindex (progname, ']')))
1149 s = rindex (progname, ':');
1151 strcpy (progname, s+1);
1152 if (s = rindex (progname, '.'))
1160 /* Initialize is_idchar to allow $. */
1161 dollars_in_ident = 1;
1162 initialize_char_syntax ();
1163 dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
1165 no_line_commands = 0;
1167 dump_macros = dump_none;
1170 cplusplus_comments = 0;
1172 bzero ((char *) pend_files, argc * sizeof (char *));
1173 bzero ((char *) pend_defs, argc * sizeof (char *));
1174 bzero ((char *) pend_undefs, argc * sizeof (char *));
1175 bzero ((char *) pend_assertions, argc * sizeof (char *));
1176 bzero ((char *) pend_includes, argc * sizeof (char *));
1178 /* Process switches and find input file name. */
1180 for (i = 1; i < argc; i++) {
1181 if (argv[i][0] != '-') {
1182 if (out_fname != NULL)
1183 fatal ("Usage: %s [switches] input output", argv[0]);
1184 else if (in_fname != NULL)
1185 out_fname = argv[i];
1189 switch (argv[i][1]) {
1192 if (!strcmp (argv[i], "-include")) {
1194 fatal ("Filename missing after `-include' option");
1196 pend_includes[i] = argv[i+1], i++;
1198 if (!strcmp (argv[i], "-imacros")) {
1200 fatal ("Filename missing after `-imacros' option");
1202 pend_files[i] = argv[i+1], i++;
1204 if (!strcmp (argv[i], "-iprefix")) {
1206 fatal ("Filename missing after `-iprefix' option");
1208 include_prefix = argv[++i];
1210 if (!strcmp (argv[i], "-ifoutput")) {
1211 output_conditionals = 1;
1213 if (!strcmp (argv[i], "-isystem")) {
1214 struct file_name_list *dirtmp;
1217 fatal ("Filename missing after `-isystem' option");
1219 dirtmp = (struct file_name_list *)
1220 xmalloc (sizeof (struct file_name_list));
1222 dirtmp->control_macro = 0;
1223 dirtmp->c_system_include_path = 1;
1224 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
1225 strcpy (dirtmp->fname, argv[++i]);
1226 dirtmp->got_name_map = 0;
1228 if (before_system == 0)
1229 before_system = dirtmp;
1231 last_before_system->next = dirtmp;
1232 last_before_system = dirtmp; /* Tail follows the last one */
1234 /* Add directory to end of path for includes,
1235 with the default prefix at the front of its name. */
1236 if (!strcmp (argv[i], "-iwithprefix")) {
1237 struct file_name_list *dirtmp;
1240 if (include_prefix != 0)
1241 prefix = include_prefix;
1243 prefix = savestring (GCC_INCLUDE_DIR);
1244 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1245 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1246 prefix[strlen (prefix) - 7] = 0;
1249 dirtmp = (struct file_name_list *)
1250 xmalloc (sizeof (struct file_name_list));
1251 dirtmp->next = 0; /* New one goes on the end */
1252 dirtmp->control_macro = 0;
1253 dirtmp->c_system_include_path = 0;
1255 fatal ("Directory name missing after `-iwithprefix' option");
1257 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
1258 + strlen (prefix) + 1);
1259 strcpy (dirtmp->fname, prefix);
1260 strcat (dirtmp->fname, argv[++i]);
1261 dirtmp->got_name_map = 0;
1263 if (after_include == 0)
1264 after_include = dirtmp;
1266 last_after_include->next = dirtmp;
1267 last_after_include = dirtmp; /* Tail follows the last one */
1269 /* Add directory to main path for includes,
1270 with the default prefix at the front of its name. */
1271 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1272 struct file_name_list *dirtmp;
1275 if (include_prefix != 0)
1276 prefix = include_prefix;
1278 prefix = savestring (GCC_INCLUDE_DIR);
1279 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1280 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1281 prefix[strlen (prefix) - 7] = 0;
1284 dirtmp = (struct file_name_list *)
1285 xmalloc (sizeof (struct file_name_list));
1286 dirtmp->next = 0; /* New one goes on the end */
1287 dirtmp->control_macro = 0;
1288 dirtmp->c_system_include_path = 0;
1290 fatal ("Directory name missing after `-iwithprefixbefore' option");
1292 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
1293 + strlen (prefix) + 1);
1294 strcpy (dirtmp->fname, prefix);
1295 strcat (dirtmp->fname, argv[++i]);
1296 dirtmp->got_name_map = 0;
1298 append_include_chain (dirtmp, dirtmp);
1300 /* Add directory to end of path for includes. */
1301 if (!strcmp (argv[i], "-idirafter")) {
1302 struct file_name_list *dirtmp;
1304 dirtmp = (struct file_name_list *)
1305 xmalloc (sizeof (struct file_name_list));
1306 dirtmp->next = 0; /* New one goes on the end */
1307 dirtmp->control_macro = 0;
1308 dirtmp->c_system_include_path = 0;
1310 fatal ("Directory name missing after `-idirafter' option");
1312 dirtmp->fname = argv[++i];
1313 dirtmp->got_name_map = 0;
1315 if (after_include == 0)
1316 after_include = dirtmp;
1318 last_after_include->next = dirtmp;
1319 last_after_include = dirtmp; /* Tail follows the last one */
1324 if (out_fname != NULL)
1325 fatal ("Output filename specified twice");
1327 fatal ("Filename missing after -o option");
1328 out_fname = argv[++i];
1329 if (!strcmp (out_fname, "-"))
1334 if (!strcmp (argv[i], "-pedantic"))
1336 else if (!strcmp (argv[i], "-pedantic-errors")) {
1338 pedantic_errors = 1;
1339 } else if (!strcmp (argv[i], "-pcp")) {
1342 fatal ("Filename missing after -pcp option");
1343 pcp_fname = argv[++i];
1345 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1346 ? fopen (pcp_fname, "w")
1347 : fdopen (dup (fileno (stdout)), "w"));
1348 if (pcp_outfile == 0)
1349 pfatal_with_name (pcp_fname);
1355 if (!strcmp (argv[i], "-traditional")) {
1357 if (dollars_in_ident > 0)
1358 dollars_in_ident = 1;
1359 } else if (!strcmp (argv[i], "-trigraphs")) {
1365 if (! strcmp (argv[i], "-lang-c"))
1366 cplusplus = 0, cplusplus_comments = 0, objc = 0;
1367 if (! strcmp (argv[i], "-lang-c++"))
1368 cplusplus = 1, cplusplus_comments = 1, objc = 0;
1369 if (! strcmp (argv[i], "-lang-c-c++-comments"))
1370 cplusplus = 0, cplusplus_comments = 1, objc = 0;
1371 if (! strcmp (argv[i], "-lang-objc"))
1372 objc = 1, cplusplus = 0, cplusplus_comments = 1;
1373 if (! strcmp (argv[i], "-lang-objc++"))
1374 objc = 1, cplusplus = 1, cplusplus_comments = 1;
1375 if (! strcmp (argv[i], "-lang-asm"))
1377 if (! strcmp (argv[i], "-lint"))
1382 cplusplus = 1, cplusplus_comments = 1;
1386 inhibit_warnings = 1;
1390 if (!strcmp (argv[i], "-Wtrigraphs"))
1392 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1394 else if (!strcmp (argv[i], "-Wcomment"))
1396 else if (!strcmp (argv[i], "-Wno-comment"))
1398 else if (!strcmp (argv[i], "-Wcomments"))
1400 else if (!strcmp (argv[i], "-Wno-comments"))
1402 else if (!strcmp (argv[i], "-Wtraditional"))
1404 else if (!strcmp (argv[i], "-Wno-traditional"))
1406 else if (!strcmp (argv[i], "-Wimport"))
1408 else if (!strcmp (argv[i], "-Wno-import"))
1410 else if (!strcmp (argv[i], "-Werror"))
1411 warnings_are_errors = 1;
1412 else if (!strcmp (argv[i], "-Wno-error"))
1413 warnings_are_errors = 0;
1414 else if (!strcmp (argv[i], "-Wall"))
1422 /* The style of the choices here is a bit mixed.
1423 The chosen scheme is a hybrid of keeping all options in one string
1424 and specifying each option in a separate argument:
1425 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1426 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1427 -M[M][G][D file]. This is awkward to handle in specs, and is not
1429 /* ??? -MG must be specified in addition to one of -M or -MM.
1430 This can be relaxed in the future without breaking anything.
1431 The converse isn't true. */
1433 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1434 if (!strcmp (argv[i], "-MG"))
1436 print_deps_missing_files = 1;
1439 if (!strcmp (argv[i], "-M"))
1441 else if (!strcmp (argv[i], "-MM"))
1443 else if (!strcmp (argv[i], "-MD"))
1445 else if (!strcmp (argv[i], "-MMD"))
1447 /* For -MD and -MMD options, write deps on file named by next arg. */
1448 if (!strcmp (argv[i], "-MD")
1449 || !strcmp (argv[i], "-MMD")) {
1451 fatal ("Filename missing after %s option", argv[i]);
1453 deps_file = argv[i];
1456 /* For -M and -MM, write deps on standard output
1457 and suppress the usual output. */
1458 deps_stream = stdout;
1465 char *p = argv[i] + 2;
1468 /* Arg to -d specifies what parts of macros to dump */
1471 dump_macros = dump_only;
1475 dump_macros = dump_names;
1478 dump_macros = dump_definitions;
1486 if (argv[i][2] == '3')
1491 fprintf (stderr, "GNU CPP version %s", version_string);
1492 #ifdef TARGET_VERSION
1495 fprintf (stderr, "\n");
1500 print_include_names = 1;
1504 if (argv[i][2] != 0)
1505 pend_defs[i] = argv[i] + 2;
1506 else if (i + 1 == argc)
1507 fatal ("Macro name missing after -D option");
1509 i++, pend_defs[i] = argv[i];
1516 if (argv[i][2] != 0)
1518 else if (i + 1 == argc)
1519 fatal ("Assertion missing after -A option");
1523 if (!strcmp (p, "-")) {
1524 /* -A- eliminates all predefined macros and assertions.
1525 Let's include also any that were specified earlier
1526 on the command line. That way we can get rid of any
1527 that were passed automatically in from GCC. */
1529 inhibit_predefs = 1;
1530 for (j = 0; j < i; j++)
1531 pend_defs[j] = pend_assertions[j] = 0;
1533 pend_assertions[i] = p;
1534 pend_assertion_options[i] = "-A";
1539 case 'U': /* JF #undef something */
1540 if (argv[i][2] != 0)
1541 pend_undefs[i] = argv[i] + 2;
1542 else if (i + 1 == argc)
1543 fatal ("Macro name missing after -U option");
1545 pend_undefs[i] = argv[i+1], i++;
1549 put_out_comments = 1;
1552 case 'E': /* -E comes from cc -E; ignore it. */
1556 no_line_commands = 1;
1559 case '$': /* Don't include $ in identifiers. */
1560 dollars_in_ident = 0;
1563 case 'I': /* Add directory to path for includes. */
1565 struct file_name_list *dirtmp;
1567 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
1569 /* Don't use any preceding -I directories for #include <...>. */
1570 first_bracket_include = 0;
1573 dirtmp = (struct file_name_list *)
1574 xmalloc (sizeof (struct file_name_list));
1575 dirtmp->next = 0; /* New one goes on the end */
1576 dirtmp->control_macro = 0;
1577 dirtmp->c_system_include_path = 0;
1578 if (argv[i][2] != 0)
1579 dirtmp->fname = argv[i] + 2;
1580 else if (i + 1 == argc)
1581 fatal ("Directory name missing after -I option");
1583 dirtmp->fname = argv[++i];
1584 dirtmp->got_name_map = 0;
1585 append_include_chain (dirtmp, dirtmp);
1591 if (!strcmp (argv[i], "-nostdinc"))
1592 /* -nostdinc causes no default include directories.
1593 You must specify all include-file directories with -I. */
1594 no_standard_includes = 1;
1595 else if (!strcmp (argv[i], "-nostdinc++"))
1596 /* -nostdinc++ causes no default C++-specific include directories. */
1597 no_standard_cplusplus_includes = 1;
1598 else if (!strcmp (argv[i], "-noprecomp"))
1603 /* Sun compiler passes undocumented switch "-undef".
1604 Let's assume it means to inhibit the predefined symbols. */
1605 inhibit_predefs = 1;
1608 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1609 if (in_fname == NULL) {
1612 } else if (out_fname == NULL) {
1615 } /* else fall through into error */
1618 fatal ("Invalid option `%s'", argv[i]);
1623 /* Add dirs from CPATH after dirs from -I. */
1624 /* There seems to be confusion about what CPATH should do,
1625 so for the moment it is not documented. */
1626 /* Some people say that CPATH should replace the standard include dirs,
1627 but that seems pointless: it comes before them, so it overrides them
1629 p = (char *) getenv ("CPATH");
1630 if (p != 0 && ! no_standard_includes)
1633 /* Now that dollars_in_ident is known, initialize is_idchar. */
1634 initialize_char_syntax ();
1636 /* Initialize output buffer */
1638 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1639 outbuf.bufp = outbuf.buf;
1640 outbuf.length = OUTBUF_SIZE;
1642 /* Do partial setup of input buffer for the sake of generating
1643 early #line directives (when -g is in effect). */
1645 fp = &instack[++indepth];
1646 if (in_fname == NULL)
1648 fp->nominal_fname = fp->fname = in_fname;
1651 /* In C++, wchar_t is a distinct basic type, and we can expect
1652 __wchar_t to be defined by cc1plus. */
1654 wchar_type = "__wchar_t";
1656 /* Install __LINE__, etc. Must follow initialize_char_syntax
1657 and option processing. */
1658 initialize_builtins (fp, &outbuf);
1660 /* Do standard #defines and assertions
1661 that identify system and machine type. */
1663 if (!inhibit_predefs) {
1664 char *p = (char *) alloca (strlen (predefs) + 1);
1665 strcpy (p, predefs);
1668 while (*p == ' ' || *p == '\t')
1670 /* Handle -D options. */
1671 if (p[0] == '-' && p[1] == 'D') {
1673 while (*p && *p != ' ' && *p != '\t')
1678 output_line_command (fp, &outbuf, 0, same_file);
1679 make_definition (q, &outbuf);
1680 while (*p == ' ' || *p == '\t')
1682 } else if (p[0] == '-' && p[1] == 'A') {
1683 /* Handle -A options (assertions). */
1692 past_name = assertion;
1693 /* Locate end of name. */
1694 while (*past_name && *past_name != ' '
1695 && *past_name != '\t' && *past_name != '(')
1697 /* Locate `(' at start of value. */
1699 while (*value && (*value == ' ' || *value == '\t'))
1701 if (*value++ != '(')
1703 while (*value && (*value == ' ' || *value == '\t'))
1706 /* Locate end of value. */
1707 while (*past_value && *past_value != ' '
1708 && *past_value != '\t' && *past_value != ')')
1710 termination = past_value;
1711 while (*termination && (*termination == ' ' || *termination == '\t'))
1713 if (*termination++ != ')')
1715 if (*termination && *termination != ' ' && *termination != '\t')
1717 /* Temporarily null-terminate the value. */
1718 save_char = *termination;
1719 *termination = '\0';
1720 /* Install the assertion. */
1721 make_assertion ("-A", assertion);
1722 *termination = (char) save_char;
1724 while (*p == ' ' || *p == '\t')
1732 /* Now handle the command line options. */
1734 /* Do -U's, -D's and -A's in the order they were seen. */
1735 for (i = 1; i < argc; i++) {
1736 if (pend_undefs[i]) {
1738 output_line_command (fp, &outbuf, 0, same_file);
1739 make_undef (pend_undefs[i], &outbuf);
1743 output_line_command (fp, &outbuf, 0, same_file);
1744 make_definition (pend_defs[i], &outbuf);
1746 if (pend_assertions[i])
1747 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1750 done_initializing = 1;
1752 { /* read the appropriate environment variable and if it exists
1753 replace include_defaults with the listed path. */
1755 switch ((objc << 1) + cplusplus)
1758 epath = getenv ("C_INCLUDE_PATH");
1761 epath = getenv ("CPLUS_INCLUDE_PATH");
1764 epath = getenv ("OBJC_INCLUDE_PATH");
1767 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
1770 /* If the environment var for this language is set,
1771 add to the default list of include directories. */
1773 char *nstore = (char *) alloca (strlen (epath) + 2);
1775 char *startp, *endp;
1777 for (num_dirs = 1, startp = epath; *startp; startp++)
1778 if (*startp == PATH_SEPARATOR)
1781 = (struct default_include *) xmalloc ((num_dirs
1782 * sizeof (struct default_include))
1783 + sizeof (include_defaults_array));
1784 startp = endp = epath;
1787 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1788 if ((*endp == PATH_SEPARATOR
1789 #if 0 /* Obsolete, now that we use semicolons as the path separator. */
1791 && (endp-startp != 1 || !isalpha (*startp))
1796 strncpy (nstore, startp, endp-startp);
1798 strcpy (nstore, ".");
1800 nstore[endp-startp] = '\0';
1802 include_defaults[num_dirs].fname = savestring (nstore);
1803 include_defaults[num_dirs].cplusplus = cplusplus;
1804 include_defaults[num_dirs].cxx_aware = 1;
1808 endp = startp = endp + 1;
1812 /* Put the usual defaults back in at the end. */
1813 bcopy ((char *) include_defaults_array,
1814 (char *) &include_defaults[num_dirs],
1815 sizeof (include_defaults_array));
1819 append_include_chain (before_system, last_before_system);
1820 first_system_include = before_system;
1822 /* Unless -fnostdinc,
1823 tack on the standard include file dirs to the specified list */
1824 if (!no_standard_includes) {
1825 struct default_include *p = include_defaults;
1826 char *specd_prefix = include_prefix;
1827 char *default_prefix = savestring (GCC_INCLUDE_DIR);
1828 int default_len = 0;
1829 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1830 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1831 default_len = strlen (default_prefix) - 7;
1832 default_prefix[default_len] = 0;
1834 /* Search "translated" versions of GNU directories.
1835 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1836 if (specd_prefix != 0 && default_len != 0)
1837 for (p = include_defaults; p->fname; p++) {
1838 /* Some standard dirs are only for C++. */
1839 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1840 /* Does this dir start with the prefix? */
1841 if (!strncmp (p->fname, default_prefix, default_len)) {
1842 /* Yes; change prefix and add to search list. */
1843 struct file_name_list *new
1844 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1845 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
1846 char *str = (char *) xmalloc (this_len + 1);
1847 strcpy (str, specd_prefix);
1848 strcat (str, p->fname + default_len);
1850 new->control_macro = 0;
1851 new->c_system_include_path = !p->cxx_aware;
1852 new->got_name_map = 0;
1853 append_include_chain (new, new);
1854 if (first_system_include == 0)
1855 first_system_include = new;
1859 /* Search ordinary names for GNU include directories. */
1860 for (p = include_defaults; p->fname; p++) {
1861 /* Some standard dirs are only for C++. */
1862 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1863 struct file_name_list *new
1864 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1865 new->control_macro = 0;
1866 new->c_system_include_path = !p->cxx_aware;
1867 new->fname = p->fname;
1868 new->got_name_map = 0;
1869 append_include_chain (new, new);
1870 if (first_system_include == 0)
1871 first_system_include = new;
1876 /* Tack the after_include chain at the end of the include chain. */
1877 append_include_chain (after_include, last_after_include);
1878 if (first_system_include == 0)
1879 first_system_include = after_include;
1881 /* With -v, print the list of dirs to search. */
1883 struct file_name_list *p;
1884 fprintf (stderr, "#include \"...\" search starts here:\n");
1885 for (p = include; p; p = p->next) {
1886 if (p == first_bracket_include)
1887 fprintf (stderr, "#include <...> search starts here:\n");
1888 fprintf (stderr, " %s\n", p->fname);
1890 fprintf (stderr, "End of search list.\n");
1893 /* Scan the -imacros files before the main input.
1894 Much like #including them, but with no_output set
1895 so that only their macro definitions matter. */
1897 no_output++; no_record_file++;
1898 for (i = 1; i < argc; i++)
1899 if (pend_files[i]) {
1900 int fd = open (pend_files[i], O_RDONLY, 0666);
1902 perror_with_name (pend_files[i]);
1903 return FAILURE_EXIT_CODE;
1905 finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
1907 no_output--; no_record_file--;
1909 /* Copy the entire contents of the main input file into
1910 the stacked input buffer previously allocated for it. */
1912 /* JF check for stdin */
1913 if (in_fname == NULL || *in_fname == 0) {
1916 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
1919 /* -MG doesn't select the form of output and must be specified with one of
1920 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
1921 inhibit compilation. */
1922 if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
1923 fatal ("-MG must be specified with one of -M or -MM");
1925 /* Either of two environment variables can specify output of deps.
1926 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1927 where OUTPUT_FILE is the file to write deps info to
1928 and DEPS_TARGET is the target to mention in the deps. */
1931 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1932 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1933 char *spec = getenv ("DEPENDENCIES_OUTPUT");
1938 spec = getenv ("SUNPRO_DEPENDENCIES");
1945 /* Find the space before the DEPS_TARGET, if there is one. */
1946 /* This should use index. (mrs) */
1947 while (*s != 0 && *s != ' ') s++;
1949 deps_target = s + 1;
1950 output_file = (char *) xmalloc (s - spec + 1);
1951 bcopy (spec, output_file, s - spec);
1952 output_file[s - spec] = 0;
1959 deps_file = output_file;
1963 /* For -M, print the expected object file name
1964 as the target of this Make-rule. */
1966 deps_allocated_size = 200;
1967 deps_buffer = (char *) xmalloc (deps_allocated_size);
1973 deps_output (deps_target, ':');
1974 } else if (*in_fname == 0) {
1975 deps_output ("-", ':');
1980 /* Discard all directory prefixes from filename. */
1981 if ((q = rindex (in_fname, '/')) != NULL)
1986 /* Copy remainder to mungable area. */
1987 p = (char *) alloca (strlen(q) + 8);
1990 /* Output P, but remove known suffixes. */
1994 && p[len - 2] == '.'
1995 && index("cCsSm", p[len - 1]))
1998 && p[len - 3] == '.'
1999 && p[len - 2] == 'c'
2000 && p[len - 1] == 'c')
2003 && p[len - 4] == '.'
2004 && p[len - 3] == 'c'
2005 && p[len - 2] == 'x'
2006 && p[len - 1] == 'x')
2009 && p[len - 4] == '.'
2010 && p[len - 3] == 'c'
2011 && p[len - 2] == 'p'
2012 && p[len - 1] == 'p')
2015 /* Supply our own suffix. */
2022 deps_output (p, ':');
2023 deps_output (in_fname, ' ');
2027 file_size_and_mode (f, &st_mode, &st_size);
2028 fp->nominal_fname = fp->fname = in_fname;
2030 fp->system_header_p = 0;
2031 /* JF all this is mine about reading pipes and ttys */
2032 if (! S_ISREG (st_mode)) {
2033 /* Read input from a file that is not a normal disk file.
2034 We cannot preallocate a buffer with the correct size,
2035 so we must read in the file a piece at the time and make it bigger. */
2042 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2044 cnt = safe_read (f, fp->buf + size, bsize - size);
2045 if (cnt < 0) goto perror; /* error! */
2047 if (size != bsize) break; /* End of file */
2049 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2053 /* Read a file whose size we can determine in advance.
2054 For the sake of VMS, st_size is just an upper bound. */
2055 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
2056 fp->length = safe_read (f, fp->buf, st_size);
2057 if (fp->length < 0) goto perror;
2060 fp->if_stack = if_stack;
2062 /* Make sure data ends with a newline. And put a null after it. */
2064 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
2065 /* Backslash-newline at end is not good enough. */
2066 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
2067 fp->buf[fp->length++] = '\n';
2068 missing_newline = 1;
2070 fp->buf[fp->length] = '\0';
2072 /* Unless inhibited, convert trigraphs in the input. */
2077 /* Now that we know the input file is valid, open the output. */
2079 if (!out_fname || !strcmp (out_fname, ""))
2080 out_fname = "stdout";
2081 else if (! freopen (out_fname, "w", stdout))
2082 pfatal_with_name (out_fname);
2084 output_line_command (fp, &outbuf, 0, same_file);
2086 /* Scan the -include files before the main input. */
2089 for (i = 1; i < argc; i++)
2090 if (pend_includes[i]) {
2091 int fd = open (pend_includes[i], O_RDONLY, 0666);
2093 perror_with_name (pend_includes[i]);
2094 return FAILURE_EXIT_CODE;
2096 finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
2100 /* Scan the input, processing macros and directives. */
2102 rescan (&outbuf, 0);
2104 if (missing_newline)
2107 if (pedantic && missing_newline)
2108 pedwarn ("file does not end in newline");
2110 /* Now we have processed the entire input
2111 Write whichever kind of output has been requested. */
2113 if (dump_macros == dump_only)
2115 else if (! inhibit_output) {
2120 /* Don't actually write the deps file if compilation has failed. */
2122 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
2123 pfatal_with_name (deps_file);
2124 fputs (deps_buffer, deps_stream);
2125 putc ('\n', deps_stream);
2127 if (ferror (deps_stream) || fclose (deps_stream) != 0)
2128 fatal ("I/O error on output");
2133 if (pcp_outfile && pcp_outfile != stdout
2134 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
2135 fatal ("I/O error on `-pcp' output");
2137 if (ferror (stdout) || fclose (stdout) != 0)
2138 fatal ("I/O error on output");
2141 exit (FAILURE_EXIT_CODE);
2142 exit (SUCCESS_EXIT_CODE);
2145 pfatal_with_name (in_fname);
2149 /* Given a colon-separated list of file names PATH,
2150 add all the names to the search path for include files. */
2164 struct file_name_list *dirtmp;
2166 /* Find the end of this name. */
2167 while (*q != 0 && *q != PATH_SEPARATOR) q++;
2169 /* An empty name in the path stands for the current directory. */
2170 name = (char *) xmalloc (2);
2174 /* Otherwise use the directory that is named. */
2175 name = (char *) xmalloc (q - p + 1);
2176 bcopy (p, name, q - p);
2180 dirtmp = (struct file_name_list *)
2181 xmalloc (sizeof (struct file_name_list));
2182 dirtmp->next = 0; /* New one goes on the end */
2183 dirtmp->control_macro = 0;
2184 dirtmp->c_system_include_path = 0;
2185 dirtmp->fname = name;
2186 dirtmp->got_name_map = 0;
2187 append_include_chain (dirtmp, dirtmp);
2189 /* Advance past this name. */
2193 /* Skip the colon. */
2198 /* Return the address of the first character in S that equals C.
2199 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2200 Return 0 if there is no such character. Assume that C itself is not '\0'.
2201 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2202 but unfortunately memchr isn't autoconfigured yet. */
2211 char *q = index (s, c);
2213 return (U_CHAR *) q;
2225 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2226 before main CCCP processing. Name `pcp' is also in honor of the
2227 drugs the trigraph designers must have been on.
2229 Using an extra pass through the buffer takes a little extra time,
2230 but is infinitely less hairy than trying to handle trigraphs inside
2231 strings, etc. everywhere, and also makes sure that trigraphs are
2232 only translated in the top level of processing. */
2238 register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
2241 fptr = bptr = sptr = buf->buf;
2242 lptr = fptr + buf->length;
2243 while ((sptr = (U_CHAR *) index0 (sptr, '?', lptr - sptr)) != NULL) {
2280 len = sptr - fptr - 2;
2282 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2283 C, this will be memmove (). */
2284 if (bptr != fptr && len > 0)
2285 bcopy ((char *) fptr, (char *) bptr, len);
2291 len = buf->length - (fptr - buf->buf);
2292 if (bptr != fptr && len > 0)
2293 bcopy ((char *) fptr, (char *) bptr, len);
2294 buf->length -= fptr - bptr;
2295 buf->buf[buf->length] = '\0';
2296 if (warn_trigraphs && fptr != bptr)
2297 warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
2300 /* Move all backslash-newline pairs out of embarrassing places.
2301 Exchange all such pairs following BP
2302 with any potentially-embarrassing characters that follow them.
2303 Potentially-embarrassing characters are / and *
2304 (because a backslash-newline inside a comment delimiter
2305 would cause it not to be recognized). */
2311 register U_CHAR *p = bp;
2312 register int count = 0;
2314 /* First count the backslash-newline pairs here. */
2320 else if (p[1] == '\r' && p[2] == '\n')
2328 /* What follows the backslash-newlines is not embarrassing. */
2330 if (count == 0 || (*p != '/' && *p != '*'))
2333 /* Copy all potentially embarrassing characters
2334 that follow the backslash-newline pairs
2335 down to where the pairs originally started. */
2337 while (*p == '*' || *p == '/')
2340 /* Now write the same number of pairs after the embarrassing chars. */
2341 while (count-- > 0) {
2347 /* Like newline_fix but for use within a directive-name.
2348 Move any backslash-newlines up past any following symbol constituents. */
2351 name_newline_fix (bp)
2354 register U_CHAR *p = bp;
2355 register int count = 0;
2357 /* First count the backslash-newline pairs here. */
2362 else if (p[1] == '\r' && p[2] == '\n')
2370 /* What follows the backslash-newlines is not embarrassing. */
2372 if (count == 0 || !is_idchar[*p])
2375 /* Copy all potentially embarrassing characters
2376 that follow the backslash-newline pairs
2377 down to where the pairs originally started. */
2379 while (is_idchar[*p])
2382 /* Now write the same number of pairs after the embarrassing chars. */
2383 while (count-- > 0) {
2389 /* Look for lint commands in comments.
2391 When we come in here, ibp points into a comment. Limit is as one expects.
2392 scan within the comment -- it should start, after lwsp, with a lint command.
2393 If so that command is returned as a (constant) string.
2395 Upon return, any arg will be pointed to with argstart and will be
2396 arglen long. Note that we don't parse that arg since it will just
2397 be printed out again.
2401 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2402 register U_CHAR *ibp;
2403 register U_CHAR *limit;
2404 U_CHAR **argstart; /* point to command arg */
2405 int *arglen, *cmdlen; /* how long they are */
2408 register U_CHAR *numptr; /* temp for arg parsing */
2412 SKIP_WHITE_SPACE (ibp);
2414 if (ibp >= limit) return NULL;
2416 linsize = limit - ibp;
2418 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2419 if ((linsize >= 10) && !strncmp (ibp, "NOTREACHED", 10)) {
2421 return "NOTREACHED";
2423 if ((linsize >= 8) && !strncmp (ibp, "ARGSUSED", 8)) {
2427 if ((linsize >= 11) && !strncmp (ibp, "LINTLIBRARY", 11)) {
2429 return "LINTLIBRARY";
2431 if ((linsize >= 7) && !strncmp (ibp, "VARARGS", 7)) {
2433 ibp += 7; linsize -= 7;
2434 if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
2436 /* OK, read a number */
2437 for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
2439 *arglen = numptr - *argstart;
2446 * The main loop of the program.
2448 * Read characters from the input stack, transferring them to the
2451 * Macros are expanded and push levels on the input stack.
2452 * At the end of such a level it is popped off and we keep reading.
2453 * At the end of any other kind of level, we return.
2454 * #-directives are handled, except within macros.
2456 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2457 * and insert them when appropriate. This is set while scanning macro
2458 * arguments before substitution. It is zero when scanning for final output.
2459 * There are three types of Newline markers:
2460 * * Newline - follows a macro name that was not expanded
2461 * because it appeared inside an expansion of the same macro.
2462 * This marker prevents future expansion of that identifier.
2463 * When the input is rescanned into the final output, these are deleted.
2464 * These are also deleted by ## concatenation.
2465 * * Newline Space (or Newline and any other whitespace character)
2466 * stands for a place that tokens must be separated or whitespace
2467 * is otherwise desirable, but where the ANSI standard specifies there
2468 * is no whitespace. This marker turns into a Space (or whichever other
2469 * whitespace char appears in the marker) in the final output,
2470 * but it turns into nothing in an argument that is stringified with #.
2471 * Such stringified arguments are the only place where the ANSI standard
2472 * specifies with precision that whitespace may not appear.
2474 * During this function, IP->bufp is kept cached in IBP for speed of access.
2475 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2476 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2477 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2478 * explicitly, and before RECACHE, since RECACHE uses OBP.
2482 rescan (op, output_marks)
2486 /* Character being scanned in main loop. */
2489 /* Length of pending accumulated identifier. */
2490 register int ident_length = 0;
2492 /* Hash code of pending accumulated identifier. */
2493 register int hash = 0;
2495 /* Current input level (&instack[indepth]). */
2498 /* Pointer for scanning input. */
2499 register U_CHAR *ibp;
2501 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2502 register U_CHAR *limit;
2504 /* Pointer for storing output. */
2505 register U_CHAR *obp;
2507 /* REDO_CHAR is nonzero if we are processing an identifier
2508 after backing up over the terminating character.
2509 Sometimes we process an identifier without backing up over
2510 the terminating character, if the terminating character
2511 is not special. Backing up is done so that the terminating character
2512 will be dispatched on again once the identifier is dealt with. */
2515 /* 1 if within an identifier inside of which a concatenation
2516 marker (Newline -) has been seen. */
2517 int concatenated = 0;
2519 /* While scanning a comment or a string constant,
2520 this records the line it started on, for error messages. */
2523 /* Record position of last `real' newline. */
2524 U_CHAR *beg_of_line;
2526 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2529 do { ip->macro->type = T_MACRO; \
2530 if (ip->free_ptr) free (ip->free_ptr); \
2531 --indepth; } while (0)
2533 /* Reload `rescan's local variables that describe the current
2534 level of the input stack. */
2537 do { ip = &instack[indepth]; \
2539 limit = ip->buf + ip->length; \
2541 check_expand (op, limit - ibp); \
2543 obp = op->bufp; } while (0)
2545 if (no_output && instack[indepth].fname != 0)
2546 skip_if_group (&instack[indepth], 1, NULL);
2553 /* Our caller must always put a null after the end of
2554 the input at each input stack level. */
2567 /* Always merge lines ending with backslash-newline,
2568 even in middle of identifier. */
2571 --obp; /* remove backslash from obuf */
2574 /* Otherwise, backslash suppresses specialness of following char,
2575 so copy it here to prevent the switch from seeing it.
2576 But first get any pending identifier processed. */
2577 if (ident_length > 0)
2583 if (assertions_flag) {
2584 /* Copy #foo (bar lose) without macro expansion. */
2585 SKIP_WHITE_SPACE (ibp);
2586 while (is_idchar[*ibp])
2588 SKIP_WHITE_SPACE (ibp);
2591 skip_paren_group (ip);
2592 bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
2593 obp += ip->bufp - ibp;
2598 /* If this is expanding a macro definition, don't recognize
2599 preprocessor directives. */
2602 /* If this is expand_into_temp_buffer, recognize them
2603 only after an actual newline at this level,
2604 not at the beginning of the input level. */
2605 if (ip->fname == 0 && beg_of_line == ip->buf)
2611 /* # keyword: a # must be first nonblank char on the line */
2612 if (beg_of_line == 0)
2617 /* Scan from start of line, skipping whitespace, comments
2618 and backslash-newlines, and see if we reach this #.
2619 If not, this # is not special. */
2621 /* If -traditional, require # to be at beginning of line. */
2624 if (is_hor_space[*bp])
2626 else if (*bp == '\\' && bp[1] == '\n')
2628 else if (*bp == '/' && bp[1] == '*') {
2630 while (!(*bp == '*' && bp[1] == '/'))
2634 /* There is no point in trying to deal with C++ // comments here,
2635 because if there is one, then this # must be part of the
2636 comment and we would never reach here. */
2643 /* This # can start a directive. */
2645 --obp; /* Don't copy the '#' */
2649 if (! handle_directive (ip, op)) {
2653 /* Not a known directive: treat it as ordinary text.
2654 IP, OP, IBP, etc. have not been changed. */
2655 if (no_output && instack[indepth].fname) {
2656 /* If not generating expanded output,
2657 what we do with ordinary text is skip it.
2658 Discard everything until next # directive. */
2659 skip_if_group (&instack[indepth], 1, 0);
2664 ++obp; /* Copy the '#' after all */
2665 /* Don't expand an identifier that could be a macro directive.
2666 (Section 3.8.3 of the ANSI C standard) */
2667 SKIP_WHITE_SPACE (ibp);
2668 if (is_idstart[*ibp])
2671 while (is_idchar[*ibp])
2679 /* A # directive has been successfully processed. */
2680 /* If not generating expanded output, ignore everything until
2681 next # directive. */
2682 if (no_output && instack[indepth].fname)
2683 skip_if_group (&instack[indepth], 1, 0);
2689 case '\"': /* skip quoted string */
2691 /* A single quoted string is treated like a double -- some
2692 programs (e.g., troff) are perverse this way */
2697 start_line = ip->lineno;
2699 /* Skip ahead to a matching quote. */
2703 if (ip->macro != 0) {
2704 /* try harder: this string crosses a macro expansion boundary.
2705 This can happen naturally if -traditional.
2706 Otherwise, only -D can make a macro with an unmatched quote. */
2712 error_with_line (line_for_error (start_line),
2713 "unterminated string or character constant");
2714 error_with_line (multiline_string_line,
2715 "possible real start of unterminated constant");
2716 multiline_string_line = 0;
2725 /* Traditionally, end of line ends a string constant with no error.
2726 So exit the loop and record the new line. */
2732 error_with_line (line_for_error (start_line),
2733 "unterminated character constant");
2736 if (pedantic && multiline_string_line == 0) {
2737 pedwarn_with_line (line_for_error (start_line),
2738 "string constant runs past end of line");
2740 if (multiline_string_line == 0)
2741 multiline_string_line = ip->lineno - 1;
2748 /* Backslash newline is replaced by nothing at all,
2749 but keep the line counts correct. */
2754 /* ANSI stupidly requires that in \\ the second \
2755 is *not* prevented from combining with a newline. */
2756 while (*ibp == '\\' && ibp[1] == '\n') {
2775 if (*ibp == '\\' && ibp[1] == '\n')
2779 && !(cplusplus_comments && *ibp == '/'))
2787 /* C++ style comment... */
2788 start_line = ip->lineno;
2790 --ibp; /* Back over the slash */
2793 /* Comments are equivalent to spaces. */
2794 if (! put_out_comments)
2797 /* must fake up a comment here */
2802 U_CHAR *before_bp = ibp+2;
2804 while (ibp < limit) {
2805 if (ibp[-1] != '\\' && *ibp == '\n') {
2806 if (put_out_comments) {
2807 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
2808 obp += ibp - before_bp;
2814 /* Copy the newline into the output buffer, in order to
2815 avoid the pain of a #line every time a multiline comment
2817 if (!put_out_comments)
2828 /* Ordinary C comment. Skip it, optionally copying it to output. */
2830 start_line = ip->lineno;
2832 ++ibp; /* Skip the star. */
2834 /* If this cpp is for lint, we peek inside the comments: */
2838 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2840 if (lintcmd != NULL) {
2842 check_expand (op, cmdlen + arglen + 14);
2844 /* I believe it is always safe to emit this newline: */
2846 bcopy ("#pragma lint ", (char *) obp, 13);
2848 bcopy (lintcmd, (char *) obp, cmdlen);
2853 bcopy (argbp, (char *) obp, arglen);
2857 /* OK, now bring us back to the state we were in before we entered
2858 this branch. We need #line because the #pragma's newline always
2859 messes up the line count. */
2861 output_line_command (ip, op, 0, same_file);
2862 check_expand (op, limit - ibp + 2);
2868 /* Comments are equivalent to spaces.
2869 Note that we already output the slash; we might not want it.
2870 For -traditional, a comment is equivalent to nothing. */
2871 if (! put_out_comments) {
2881 U_CHAR *before_bp = ibp;
2883 while (ibp < limit) {
2886 if (warn_comments && ibp < limit && *ibp == '*')
2887 warning ("`/*' within comment");
2890 if (*ibp == '\\' && ibp[1] == '\n')
2892 if (ibp >= limit || *ibp == '/')
2897 /* Copy the newline into the output buffer, in order to
2898 avoid the pain of a #line every time a multiline comment
2900 if (!put_out_comments)
2908 error_with_line (line_for_error (start_line),
2909 "unterminated comment");
2912 if (put_out_comments) {
2913 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
2914 obp += ibp - before_bp;
2921 if (!dollars_in_ident)
2925 case '0': case '1': case '2': case '3': case '4':
2926 case '5': case '6': case '7': case '8': case '9':
2927 /* If digit is not part of identifier, it starts a number,
2928 which means that following letters are not an identifier.
2929 "0x5" does not refer to an identifier "x5".
2930 So copy all alphanumerics that follow without accumulating
2931 as an identifier. Periods also, for sake of "3.e7". */
2933 if (ident_length == 0) {
2934 while (ibp < limit) {
2935 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
2940 /* ".." terminates a preprocessing number. This is useless for C
2941 code but useful for preprocessing other things. */
2942 if (!isalnum (c) && (c != '.' || *ibp == '.') && c != '_') {
2947 /* A sign can be part of a preprocessing number
2948 if it follows an e. */
2949 if (c == 'e' || c == 'E') {
2950 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
2954 if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
2956 /* But traditional C does not let the token go past the sign. */
2967 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2968 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2969 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2970 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2972 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2973 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
2974 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2975 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2979 /* Compute step of hash function, to avoid a proc call on every token */
2980 hash = HASHSTEP (hash, c);
2984 if (ip->fname == 0 && *ibp == '-') {
2985 /* Newline - inhibits expansion of preceding token.
2986 If expanding a macro arg, we keep the newline -.
2987 In final output, it is deleted.
2988 We recognize Newline - in macro bodies and macro args. */
2989 if (! concatenated) {
2994 if (!output_marks) {
2997 /* If expanding a macro arg, keep the newline -. */
3003 /* If reprocessing a macro expansion, newline is a special marker. */
3004 else if (ip->macro != 0) {
3005 /* Newline White is a "funny space" to separate tokens that are
3006 supposed to be separate but without space between.
3007 Here White means any whitespace character.
3008 Newline - marks a recursive macro use that is not
3009 supposed to be expandable. */
3011 if (is_space[*ibp]) {
3012 /* Newline Space does not prevent expansion of preceding token
3013 so expand the preceding token and then come back. */
3014 if (ident_length > 0)
3017 /* If generating final output, newline space makes a space. */
3018 if (!output_marks) {
3020 /* And Newline Newline makes a newline, so count it. */
3021 if (obp[-1] == '\n')
3024 /* If expanding a macro arg, keep the newline space.
3025 If the arg gets stringified, newline space makes nothing. */
3028 } else abort (); /* Newline followed by something random? */
3032 /* If there is a pending identifier, handle it and come back here. */
3033 if (ident_length > 0)
3038 /* Update the line counts and output a #line if necessary. */
3041 if (ip->lineno != op->lineno) {
3043 output_line_command (ip, op, 1, same_file);
3044 check_expand (op, limit - ibp);
3049 /* Come here either after (1) a null character that is part of the input
3050 or (2) at the end of the input, because there is a null there. */
3053 /* Our input really contains a null character. */
3056 /* At end of a macro-expansion level, pop it and read next level. */
3057 if (ip->macro != 0) {
3060 /* If traditional, and we have an identifier that ends here,
3061 process it now, so we get the right error for recursion. */
3062 if (traditional && ident_length
3063 && ! is_idchar[*instack[indepth - 1].bufp]) {
3072 /* If we don't have a pending identifier,
3073 return at end of input. */
3074 if (ident_length == 0) {
3082 /* If we do have a pending identifier, just consider this null
3083 a special character and arrange to dispatch on it again.
3084 The second time, IDENT_LENGTH will be zero so we will return. */
3090 /* Handle the case of a character such as /, ', " or null
3091 seen following an identifier. Back over it so that
3092 after the identifier is processed the special char
3093 will be dispatched on again. */
3103 if (ident_length > 0) {
3104 register HASHNODE *hp;
3106 /* We have just seen an identifier end. If it's a macro, expand it.
3108 IDENT_LENGTH is the length of the identifier
3109 and HASH is its hash code.
3111 The identifier has already been copied to the output,
3112 so if it is a macro we must remove it.
3114 If REDO_CHAR is 0, the char that terminated the identifier
3115 has been skipped in the output and the input.
3116 OBP-IDENT_LENGTH-1 points to the identifier.
3117 If the identifier is a macro, we must back over the terminator.
3119 If REDO_CHAR is 1, the terminating char has already been
3120 backed over. OBP-IDENT_LENGTH points to the identifier. */
3122 if (!pcp_outfile || pcp_inside_if) {
3124 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
3127 if (hp->length == ident_length) {
3128 int obufp_before_macroname;
3129 int op_lineno_before_macroname;
3130 register int i = ident_length;
3131 register U_CHAR *p = hp->name;
3132 register U_CHAR *q = obp - i;
3138 do { /* All this to avoid a strncmp () */
3143 /* We found a use of a macro name.
3144 see if the context shows it is a macro call. */
3146 /* Back up over terminating character if not already done. */
3152 /* Save this as a displacement from the beginning of the output
3153 buffer. We can not save this as a position in the output
3154 buffer, because it may get realloc'ed by RECACHE. */
3155 obufp_before_macroname = (obp - op->buf) - ident_length;
3156 op_lineno_before_macroname = op->lineno;
3158 if (hp->type == T_PCSTRING) {
3159 pcstring_used (hp); /* Mark the definition of this key
3160 as needed, ensuring that it
3162 break; /* Exit loop, since the key cannot have a
3163 definition any longer. */
3166 /* Record whether the macro is disabled. */
3167 disabled = hp->type == T_DISABLED;
3169 /* This looks like a macro ref, but if the macro was disabled,
3170 just copy its name and put in a marker if requested. */
3174 /* This error check caught useful cases such as
3175 #define foo(x,y) bar (x (y,0), y)
3178 error ("recursive use of macro `%s'", hp->name);
3182 check_expand (op, limit - ibp + 2);
3189 /* If macro wants an arglist, verify that a '(' follows.
3190 first skip all whitespace, copying it to the output
3191 after the macro name. Then, if there is no '(',
3192 decide this is not a macro call and leave things that way. */
3193 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3194 && hp->value.defn->nargs >= 0)
3196 U_CHAR *old_ibp = ibp;
3197 U_CHAR *old_obp = obp;
3198 int old_iln = ip->lineno;
3199 int old_oln = op->lineno;
3202 /* Scan forward over whitespace, copying it to the output. */
3203 if (ibp == limit && ip->macro != 0) {
3208 old_iln = ip->lineno;
3209 old_oln = op->lineno;
3211 /* A comment: copy it unchanged or discard it. */
3212 else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
3213 if (put_out_comments) {
3216 } else if (! traditional) {
3220 while (ibp + 1 != limit
3221 && !(ibp[0] == '*' && ibp[1] == '/')) {
3222 /* We need not worry about newline-marks,
3223 since they are never found in comments. */
3225 /* Newline in a file. Count it. */
3229 if (put_out_comments)
3235 if (put_out_comments) {
3240 else if (is_space[*ibp]) {
3242 if (ibp[-1] == '\n') {
3243 if (ip->macro == 0) {
3244 /* Newline in a file. Count it. */
3247 } else if (!output_marks) {
3248 /* A newline mark, and we don't want marks
3249 in the output. If it is newline-hyphen,
3250 discard it entirely. Otherwise, it is
3251 newline-whitechar, so keep the whitechar. */
3261 /* A newline mark; copy both chars to the output. */
3269 /* It isn't a macro call.
3270 Put back the space that we just skipped. */
3273 ip->lineno = old_iln;
3274 op->lineno = old_oln;
3275 /* Exit the for loop. */
3280 /* This is now known to be a macro call.
3281 Discard the macro name from the output,
3282 along with any following whitespace just copied. */
3283 obp = op->buf + obufp_before_macroname;
3284 op->lineno = op_lineno_before_macroname;
3286 /* Prevent accidental token-pasting with a character
3287 before the macro call. */
3288 if (!traditional && obp != op->buf
3289 && (obp[-1] == '-' || obp[1] == '+' || obp[1] == '&'
3290 || obp[-1] == '|' || obp[1] == '<' || obp[1] == '>')) {
3291 /* If we are expanding a macro arg, make a newline marker
3292 to separate the tokens. If we are making real output,
3293 a plain space will do. */
3299 /* Expand the macro, reading arguments as needed,
3300 and push the expansion on the input stack. */
3303 macroexpand (hp, op);
3305 /* Reexamine input stack, since macroexpand has pushed
3306 a new level on it. */
3313 } /* End hash-table-search loop */
3315 ident_length = hash = 0; /* Stop collecting identifier */
3318 } /* End if (ident_length > 0) */
3320 } /* End per-char loop */
3322 /* Come here to return -- but first give an error message
3323 if there was an unterminated successful conditional. */
3325 if (if_stack != ip->if_stack)
3327 char *str = "unknown";
3329 switch (if_stack->type)
3348 error_with_line (line_for_error (if_stack->lineno),
3349 "unterminated `#%s' conditional", str);
3351 if_stack = ip->if_stack;
3355 * Rescan a string into a temporary buffer and return the result
3356 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3358 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3359 * and insert such markers when appropriate. See `rescan' for details.
3360 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3361 * before substitution; it is 0 for other uses.
3364 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3365 U_CHAR *buf, *limit;
3366 int output_marks, assertions;
3368 register FILE_BUF *ip;
3370 int length = limit - buf;
3372 int odepth = indepth;
3373 int save_assertions_flag = assertions_flag;
3375 assertions_flag = assertions;
3380 /* Set up the input on the input stack. */
3382 buf1 = (U_CHAR *) alloca (length + 1);
3384 register U_CHAR *p1 = buf;
3385 register U_CHAR *p2 = buf1;
3392 /* Set up to receive the output. */
3394 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3395 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3400 CHECK_DEPTH ({return obuf;});
3404 ip = &instack[indepth];
3406 ip->nominal_fname = 0;
3407 ip->system_header_p = 0;
3410 ip->length = length;
3411 ip->buf = ip->bufp = buf1;
3412 ip->if_stack = if_stack;
3414 ip->lineno = obuf.lineno = 1;
3416 /* Scan the input, create the output. */
3417 rescan (&obuf, output_marks);
3419 /* Pop input stack to original state. */
3422 if (indepth != odepth)
3425 /* Record the output. */
3426 obuf.length = obuf.bufp - obuf.buf;
3428 assertions_flag = save_assertions_flag;
3433 * Process a # directive. Expects IP->bufp to point after the '#', as in
3434 * `#define foo bar'. Passes to the command handler
3435 * (do_define, do_include, etc.): the addresses of the 1st and
3436 * last chars of the command (starting immediately after the #
3437 * keyword), plus op and the keyword table pointer. If the command
3438 * contains comments it is copied into a temporary buffer sans comments
3439 * and the temporary buffer is passed to the command handler instead.
3440 * Likewise for backslash-newlines.
3442 * Returns nonzero if this was a known # directive.
3443 * Otherwise, returns zero, without advancing the input pointer.
3447 handle_directive (ip, op)
3450 register U_CHAR *bp, *cp;
3451 register struct directive *kt;
3452 register int ident_length;
3455 /* Nonzero means we must copy the entire command
3456 to get rid of comments or backslash-newlines. */
3457 int copy_command = 0;
3459 U_CHAR *ident, *after_ident;
3463 /* Record where the directive started. do_xifdef needs this. */
3464 directive_start = bp - 1;
3466 /* Skip whitespace and \-newline. */
3468 if (is_hor_space[*bp]) {
3469 if ((*bp == '\f' || *bp == '\v') && pedantic)
3470 pedwarn ("%s in preprocessing directive",
3471 *bp == '\f' ? "formfeed" : "vertical tab");
3473 } else if (*bp == '/' && (bp[1] == '*'
3474 || (cplusplus_comments && bp[1] == '/'))) {
3476 skip_to_end_of_comment (ip, &ip->lineno, 0);
3478 } else if (*bp == '\\' && bp[1] == '\n') {
3479 bp += 2; ip->lineno++;
3483 /* Now find end of directive name.
3484 If we encounter a backslash-newline, exchange it with any following
3485 symbol-constituents so that we end up with a contiguous name. */
3492 if (*cp == '\\' && cp[1] == '\n')
3493 name_newline_fix (cp);
3499 ident_length = cp - bp;
3503 /* A line of just `#' becomes blank. */
3505 if (ident_length == 0 && *after_ident == '\n') {
3506 ip->bufp = after_ident;
3510 if (ident_length == 0 || !is_idstart[*ident]) {
3512 while (is_idchar[*p]) {
3513 if (*p < '0' || *p > '9')
3517 /* Handle # followed by a line number. */
3518 if (p != ident && !is_idchar[*p]) {
3519 static struct directive line_directive_table[] = {
3520 { 4, do_line, "line", T_LINE},
3523 pedwarn ("`#' followed by integer");
3524 after_ident = ident;
3525 kt = line_directive_table;
3529 /* Avoid error for `###' and similar cases unless -pedantic. */
3531 while (*p == '#' || is_hor_space[*p]) p++;
3533 if (pedantic && !lang_asm)
3534 warning ("invalid preprocessor directive");
3540 error ("invalid preprocessor directive name");
3546 * Decode the keyword and call the appropriate expansion
3547 * routine, after moving the input pointer up to the next line.
3549 for (kt = directive_table; kt->length > 0; kt++) {
3550 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
3551 register U_CHAR *buf;
3552 register U_CHAR *limit;
3555 int *already_output;
3557 /* Nonzero means do not delete comments within the directive.
3558 #define needs this when -traditional. */
3563 limit = ip->buf + ip->length;
3566 keep_comments = traditional && kt->traditional_comments;
3567 /* #import is defined only in Objective C, or when on the NeXT. */
3568 if (kt->type == T_IMPORT && !(objc || lookup ("__NeXT__", -1, -1)))
3571 /* Find the end of this command (first newline not backslashed
3572 and not in a string or comment).
3573 Set COPY_COMMAND if the command must be copied
3574 (it contains a backslash-newline or a comment). */
3576 buf = bp = after_ident;
3577 while (bp < limit) {
3578 register U_CHAR c = *bp++;
3592 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
3593 /* Don't bother calling the directive if we already got an error
3594 message due to unterminated string. Skip everything and pretend
3595 we called the directive. */
3598 /* Traditional preprocessing permits unterminated strings. */
3607 /* <...> is special for #include. */
3609 if (!kt->angle_brackets)
3611 while (*bp && *bp != '>') bp++;
3615 if (*bp == '\\' && bp[1] == '\n')
3618 || (cplusplus_comments && *bp == '/')) {
3619 U_CHAR *obp = bp - 1;
3621 skip_to_end_of_comment (ip, &ip->lineno, 0);
3623 /* No need to copy the command because of a comment at the end;
3624 just don't include the comment in the directive. */
3625 if (bp == limit || *bp == '\n') {
3629 /* Don't remove the comments if -traditional. */
3630 if (! keep_comments)
3638 pedwarn ("%s in preprocessing directive",
3639 c == '\f' ? "formfeed" : "vertical tab");
3643 --bp; /* Point to the newline */
3651 resume_p = ip->bufp;
3652 /* BP is the end of the directive.
3653 RESUME_P is the next interesting data after the directive.
3654 A comment may come between. */
3656 /* If a directive should be copied through, and -E was given,
3657 pass it through before removing comments. */
3658 if (!no_output && kt->pass_thru && put_out_comments) {
3661 /* Output directive name. */
3662 check_expand (op, kt->length + 2);
3663 /* Make sure # is at the start of a line */
3664 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
3669 bcopy (kt->name, op->bufp, kt->length);
3670 op->bufp += kt->length;
3672 /* Output arguments. */
3674 check_expand (op, len);
3675 bcopy (buf, (char *) op->bufp, len);
3677 /* Take account of any (escaped) newlines just output. */
3679 if (buf[len] == '\n')
3682 already_output = &junk;
3683 } /* Don't we need a newline or #line? */
3686 register U_CHAR *xp = buf;
3687 /* Need to copy entire command into temp buffer before dispatching */
3689 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
3693 /* Copy to the new buffer, deleting comments
3694 and backslash-newlines (and whitespace surrounding the latter). */
3697 register U_CHAR c = *xp++;
3702 abort (); /* A bare newline should never part of the line. */
3705 /* <...> is special for #include. */
3707 if (!kt->angle_brackets)
3709 while (xp < bp && c != '>') {
3711 if (c == '\\' && xp < bp && *xp == '\n')
3722 if (cp != buf && is_space[cp[-1]]) {
3723 while (cp != buf && is_space[cp[-1]]) cp--;
3725 SKIP_WHITE_SPACE (xp);
3726 } else if (is_space[*xp]) {
3728 SKIP_WHITE_SPACE (xp);
3738 register U_CHAR *bp1
3739 = skip_quoted_string (xp - 1, bp, ip->lineno,
3740 NULL_PTR, NULL_PTR, NULL_PTR);
3754 || (cplusplus_comments && *xp == '/')) {
3756 /* If we already copied the command through,
3757 already_output != 0 prevents outputting comment now. */
3758 skip_to_end_of_comment (ip, already_output, 0);
3760 while (xp != ip->bufp)
3762 /* Delete or replace the slash. */
3763 else if (traditional)
3772 /* Null-terminate the copy. */
3778 ip->bufp = resume_p;
3780 /* Some directives should be written out for cc1 to process,
3781 just as if they were not defined. And sometimes we're copying
3782 definitions through. */
3784 if (!no_output && already_output == 0
3786 || (kt->type == T_DEFINE
3787 && (dump_macros == dump_names
3788 || dump_macros == dump_definitions)))) {
3791 /* Output directive name. */
3792 check_expand (op, kt->length + 1);
3794 bcopy (kt->name, (char *) op->bufp, kt->length);
3795 op->bufp += kt->length;
3797 if (kt->pass_thru || dump_macros == dump_definitions) {
3798 /* Output arguments. */
3800 check_expand (op, len);
3801 bcopy (buf, (char *) op->bufp, len);
3803 } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
3806 SKIP_WHITE_SPACE (xp);
3808 while (is_idchar[*xp]) xp++;
3810 check_expand (op, len + 1);
3812 bcopy (yp, op->bufp, len);
3815 } /* Don't we need a newline or #line? */
3817 /* Call the appropriate command handler. buf now points to
3818 either the appropriate place in the input buffer, or to
3819 the temp buffer if it was necessary to make one. cp
3820 points to the first char after the contents of the (possibly
3821 copied) command, in either case. */
3822 (*kt->func) (buf, cp, op, kt);
3823 check_expand (op, ip->length - (ip->bufp - ip->buf));
3829 /* It is deliberate that we don't warn about undefined directives.
3830 That is the responsibility of cc1. */
3837 static struct tm *timebuf;
3839 time_t t = time (0);
3840 timebuf = localtime (&t);
3845 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3846 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3850 * expand things like __FILE__. Place the expansion into the output
3851 * buffer *without* rescanning.
3855 special_symbol (hp, op)
3862 FILE_BUF *ip = NULL;
3865 int paren = 0; /* For special `defined' keyword */
3867 if (pcp_outfile && pcp_inside_if
3868 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
3869 error ("Predefined macro `%s' used inside `#if' during precompilation",
3872 for (i = indepth; i >= 0; i--)
3873 if (instack[i].fname != NULL) {
3878 error ("cccp error: not in any file?!");
3879 return; /* the show must go on */
3887 if (hp->type == T_FILE)
3888 string = ip->nominal_fname;
3890 string = instack[0].nominal_fname;
3894 buf = (char *) alloca (3 + 4 * strlen (string));
3895 quote_string (buf, string);
3903 case T_INCLUDE_LEVEL:
3905 for (i = indepth; i >= 0; i--)
3906 if (instack[i].fname != NULL)
3909 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
3910 sprintf (buf, "%d", true_indepth - 1);
3914 buf = (char *) alloca (3 + strlen (version_string));
3915 sprintf (buf, "\"%s\"", version_string);
3918 #ifndef NO_BUILTIN_SIZE_TYPE
3924 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3925 case T_PTRDIFF_TYPE:
3934 case T_USER_LABEL_PREFIX_TYPE:
3935 buf = USER_LABEL_PREFIX;
3938 case T_REGISTER_PREFIX_TYPE:
3939 buf = REGISTER_PREFIX;
3943 buf = (char *) alloca (4 * sizeof (int));
3944 sprintf (buf, "%d", hp->value.ival);
3945 if (pcp_inside_if && pcp_outfile)
3946 /* Output a precondition for this macro use */
3947 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
3951 buf = (char *) alloca (10);
3952 sprintf (buf, "%d", ip->lineno);
3957 buf = (char *) alloca (20);
3958 timebuf = timestamp ();
3959 if (hp->type == T_DATE)
3960 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3961 timebuf->tm_mday, timebuf->tm_year + 1900);
3963 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3967 case T_SPEC_DEFINED:
3968 buf = " 0 "; /* Assume symbol is not defined */
3969 ip = &instack[indepth];
3970 SKIP_WHITE_SPACE (ip->bufp);
3971 if (*ip->bufp == '(') {
3973 ip->bufp++; /* Skip over the paren */
3974 SKIP_WHITE_SPACE (ip->bufp);
3977 if (!is_idstart[*ip->bufp])
3979 if (hp = lookup (ip->bufp, -1, -1)) {
3980 if (pcp_outfile && pcp_inside_if
3981 && (hp->type == T_CONST
3982 || (hp->type == T_MACRO && hp->value.defn->predefined)))
3983 /* Output a precondition for this macro use. */
3984 fprintf (pcp_outfile, "#define %s\n", hp->name);
3988 if (pcp_outfile && pcp_inside_if) {
3989 /* Output a precondition for this macro use */
3990 U_CHAR *cp = ip->bufp;
3991 fprintf (pcp_outfile, "#undef ");
3992 while (is_idchar[*cp]) /* Ick! */
3993 fputc (*cp++, pcp_outfile);
3994 putc ('\n', pcp_outfile);
3996 while (is_idchar[*ip->bufp])
3998 SKIP_WHITE_SPACE (ip->bufp);
4000 if (*ip->bufp != ')')
4008 error ("`defined' without an identifier");
4012 error ("cccp error: invalid special hash type"); /* time for gdb */
4016 check_expand (op, len);
4017 bcopy (buf, (char *) op->bufp, len);
4024 /* Routines to handle #directives */
4026 /* Handle #include and #import.
4027 This function expects to see "fname" or <fname> on the input. */
4030 do_include (buf, limit, op, keyword)
4031 U_CHAR *buf, *limit;
4033 struct directive *keyword;
4035 int importing = (keyword->type == T_IMPORT);
4036 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4037 static int import_warning = 0;
4038 char *fname; /* Dynamically allocated fname buffer */
4041 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
4043 struct file_name_list *search_start = include; /* Chain of dirs to search */
4044 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4045 struct file_name_list *searchptr = 0;
4048 int f; /* file number */
4050 int retried = 0; /* Have already tried macro
4051 expanding the include line*/
4052 FILE_BUF trybuf; /* It got expanded into here */
4053 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4058 f= -1; /* JF we iz paranoid! */
4060 if (importing && warn_import && !inhibit_warnings
4061 && !instack[indepth].system_header_p && !import_warning) {
4063 warning ("using `#import' is not recommended");
4064 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
4065 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
4066 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
4067 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
4068 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
4069 fprintf (stderr, " ... <real contents of file> ...\n");
4070 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
4071 fprintf (stderr, "Then users can use `#include' any number of times.\n");
4072 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
4073 fprintf (stderr, "when it is equipped with such a conditional.\n");
4079 SKIP_WHITE_SPACE (fbeg);
4080 /* Discard trailing whitespace so we can easily see
4081 if we have parsed all the significant chars we were given. */
4082 while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
4088 /* Copy the operand text, concatenating the strings. */
4091 fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
4093 while (fin != limit) {
4094 while (fin != limit && *fin != '\"')
4099 /* If not at the end, there had better be another string. */
4100 /* Skip just horiz space, and don't go past limit. */
4101 while (fin != limit && is_hor_space[*fin]) fin++;
4102 if (fin != limit && *fin == '\"')
4110 /* We have "filename". Figure out directory this source
4111 file is coming from and put it on the front of the list. */
4113 /* If -I- was specified, don't search current dir, only spec'd ones. */
4114 if (ignore_srcdir) break;
4116 for (fp = &instack[indepth]; fp >= instack; fp--)
4121 if ((nam = fp->nominal_fname) != NULL) {
4122 /* Found a named file. Figure out dir of the file,
4123 and put it in front of the search list. */
4124 dsp[0].next = search_start;
4127 ep = rindex (nam, '/');
4129 ep = rindex (nam, ']');
4130 if (ep == NULL) ep = rindex (nam, '>');
4131 if (ep == NULL) ep = rindex (nam, ':');
4132 if (ep != NULL) ep++;
4136 dsp[0].fname = (char *) alloca (n + 1);
4137 strncpy (dsp[0].fname, nam, n);
4138 dsp[0].fname[n] = '\0';
4139 if (n + INCLUDE_LEN_FUDGE > max_include_len)
4140 max_include_len = n + INCLUDE_LEN_FUDGE;
4142 dsp[0].fname = 0; /* Current directory */
4144 dsp[0].got_name_map = 0;
4153 while (fend != limit && *fend != '>') fend++;
4154 if (*fend == '>' && fend + 1 == limit) {
4156 /* If -I-, start with the first -I dir after the -I-. */
4157 if (first_bracket_include)
4158 search_start = first_bracket_include;
4166 * Support '#include xyz' like VAX-C to allow for easy use of all the
4167 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4168 * code from case '<' is repeated here) and generates a warning.
4170 if (isalpha(*(--fbeg))) {
4172 while (fend != limit && (!isspace(*fend))) fend++;
4173 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4174 if (fend == limit) {
4176 /* If -I-, start with the first -I dir after the -I-. */
4177 if (first_bracket_include)
4178 search_start = first_bracket_include;
4186 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
4189 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
4190 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
4191 bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
4192 limit = buf + (trybuf.bufp - trybuf.buf);
4199 /* For #include_next, skip in the search path
4200 past the dir in which the containing file was found. */
4203 for (fp = &instack[indepth]; fp >= instack; fp--)
4204 if (fp->fname != NULL) {
4205 /* fp->dir is null if the containing file was specified
4206 with an absolute file name. In that case, don't skip anything. */
4208 search_start = fp->dir->next;
4217 error ("empty file name in `#%s'", keyword->name);
4221 /* Allocate this permanently, because it gets stored in the definitions
4223 fname = (char *) xmalloc (max_include_len + flen + 4);
4224 /* + 2 above for slash and terminating null. */
4225 /* + 2 added for '.h' on VMS (to support '#include filename') */
4227 /* If specified file name is absolute, just open it. */
4230 strncpy (fname, fbeg, flen);
4232 if (redundant_include_p (fname))
4235 f = lookup_import (fname, NULL_PTR);
4237 f = open_include_file (fname, NULL_PTR);
4239 return 0; /* Already included this file */
4241 /* Search directory path, trying to open the file.
4242 Copy each filename tried into FNAME. */
4244 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
4245 if (searchptr->fname) {
4246 /* The empty string in a search path is ignored.
4247 This makes it possible to turn off entirely
4248 a standard piece of the list. */
4249 if (searchptr->fname[0] == 0)
4251 strcpy (fname, searchptr->fname);
4252 strcat (fname, "/");
4253 fname[strlen (fname) + flen] = 0;
4257 strncat (fname, fbeg, flen);
4259 /* Change this 1/2 Unix 1/2 VMS file specification into a
4260 full VMS file specification */
4261 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4262 /* Fix up the filename */
4263 hack_vms_include_specification (fname);
4265 /* This is a normal VMS filespec, so use it unchanged. */
4266 strncpy (fname, fbeg, flen);
4268 /* if it's '#include filename', add the missing .h */
4269 if (index(fname,'.')==NULL) {
4270 strcat (fname, ".h");
4275 f = lookup_import (fname, searchptr);
4277 f = open_include_file (fname, searchptr);
4279 return 0; /* Already included this file */
4281 else if (f == -1 && errno == EACCES)
4282 warning ("Header file %s exists, but is not readable", fname);
4284 if (redundant_include_p (fname)) {
4294 /* A file that was not found. */
4296 strncpy (fname, fbeg, flen);
4298 /* If generating dependencies and -MG was specified, we assume missing
4299 files are leaf files, living in the same directory as the source file
4300 or other similar place; these missing files may be generated from
4301 other files and may not exist yet (eg: y.tab.h). */
4302 if (print_deps_missing_files
4303 && print_deps > (angle_brackets || (system_include_depth > 0)))
4305 /* If it was requested as a system header file,
4306 then assume it belongs in the first place to look for such. */
4309 for (searchptr = search_start; searchptr; searchptr = searchptr->next)
4311 if (searchptr->fname)
4315 if (searchptr->fname[0] == 0)
4317 p = xmalloc (strlen (searchptr->fname)
4318 + strlen (fname) + 2);
4319 strcpy (p, searchptr->fname);
4322 deps_output (p, ' ');
4329 /* Otherwise, omit the directory, as if the file existed
4330 in the directory with the source. */
4331 deps_output (fname, ' ');
4334 /* If -M was specified, and this header file won't be added to the
4335 dependency list, then don't count this as an error, because we can
4336 still produce correct output. Otherwise, we can't produce correct
4337 output, because there may be dependencies we need inside the missing
4338 file, and we don't know what directory this missing file exists in. */
4340 && (print_deps <= (angle_brackets || (system_include_depth > 0))))
4341 warning ("No include path in which to find %s", fname);
4342 else if (search_start)
4343 error_from_errno (fname);
4345 error ("No include path in which to find %s", fname);
4349 /* Check to see if this include file is a once-only include file.
4352 struct file_name_list* ptr;
4354 for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
4355 if (!strcmp (ptr->fname, fname)) {
4357 return 0; /* This file was once'd. */
4361 for (ptr = all_include_files; ptr; ptr = ptr->next) {
4362 if (!strcmp (ptr->fname, fname))
4363 break; /* This file was included before. */
4367 /* This is the first time for this file. */
4368 /* Add it to list of files included. */
4370 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
4371 ptr->control_macro = 0;
4372 ptr->c_system_include_path = 0;
4373 ptr->next = all_include_files;
4374 all_include_files = ptr;
4375 ptr->fname = savestring (fname);
4376 ptr->got_name_map = 0;
4378 /* For -M, add this file to the dependencies. */
4379 if (print_deps > (angle_brackets || (system_include_depth > 0)))
4380 deps_output (fname, ' ');
4383 /* Handle -H option. */
4384 if (print_include_names) {
4385 output_dots (stderr, indepth);
4386 fprintf (stderr, "%s\n", fname);
4390 system_include_depth++;
4392 /* Actually process the file. */
4393 add_import (f, fname); /* Record file on "seen" list for #import. */
4395 pcftry = (char *) alloca (strlen (fname) + 30);
4403 sprintf (pcftry, "%s%d", fname, pcfnum++);
4405 pcf = open (pcftry, O_RDONLY, 0666);
4411 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
4413 || stat_f.st_dev != s.st_dev)
4415 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
4416 /* Don't need it any more. */
4421 /* Don't need it at all. */
4426 } while (pcf != -1 && !pcfbuf);
4428 /* Actually process the file */
4430 pcfname = xmalloc (strlen (pcftry) + 1);
4431 strcpy (pcfname, pcftry);
4432 pcfinclude (pcfbuf, pcfbuflimit, fname, op);
4435 finclude (f, fname, op, is_system_include (fname), searchptr);
4438 system_include_depth--;
4443 /* Return nonzero if there is no need to include file NAME
4444 because it has already been included and it contains a conditional
4445 to make a repeated include do nothing. */
4448 redundant_include_p (name)
4451 struct file_name_list *l = all_include_files;
4452 for (; l; l = l->next)
4453 if (! strcmp (name, l->fname)
4455 && lookup (l->control_macro, -1, -1))
4460 /* Return nonzero if the given FILENAME is an absolute pathname which
4461 designates a file within one of the known "system" include file
4462 directories. We assume here that if the given FILENAME looks like
4463 it is the name of a file which resides either directly in a "system"
4464 include file directory, or within any subdirectory thereof, then the
4465 given file must be a "system" include file. This function tells us
4466 if we should suppress pedantic errors/warnings for the given FILENAME.
4468 The value is 2 if the file is a C-language system header file
4469 for which C++ should (on most systems) assume `extern "C"'. */
4472 is_system_include (filename)
4473 register char *filename;
4475 struct file_name_list *searchptr;
4477 for (searchptr = first_system_include; searchptr;
4478 searchptr = searchptr->next)
4479 if (searchptr->fname) {
4480 register char *sys_dir = searchptr->fname;
4481 register unsigned length = strlen (sys_dir);
4483 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
4485 if (searchptr->c_system_include_path)
4494 /* The file_name_map structure holds a mapping of file names for a
4495 particular directory. This mapping is read from the file named
4496 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4497 map filenames on a file system with severe filename restrictions,
4498 such as DOS. The format of the file name map file is just a series
4499 of lines with two tokens on each line. The first token is the name
4500 to map, and the second token is the actual name to use. */
4502 struct file_name_map
4504 struct file_name_map *map_next;
4509 #define FILE_NAME_MAP_FILE "header.gcc"
4511 /* Read a space delimited string of unlimited length from a stdio
4515 read_filename_string (ch, f)
4523 set = alloc = xmalloc (len + 1);
4527 while ((ch = getc (f)) != EOF && ! is_space[ch])
4529 if (set - alloc == len)
4532 alloc = xrealloc (alloc, len + 1);
4533 set = alloc + len / 2;
4543 /* Read the file name map file for DIRNAME. */
4545 static struct file_name_map *
4546 read_name_map (dirname)
4549 /* This structure holds a linked list of file name maps, one per
4551 struct file_name_map_list
4553 struct file_name_map_list *map_list_next;
4554 char *map_list_name;
4555 struct file_name_map *map_list_map;
4557 static struct file_name_map_list *map_list;
4558 register struct file_name_map_list *map_list_ptr;
4562 for (map_list_ptr = map_list; map_list_ptr;
4563 map_list_ptr = map_list_ptr->map_list_next)
4564 if (! strcmp (map_list_ptr->map_list_name, dirname))
4565 return map_list_ptr->map_list_map;
4567 map_list_ptr = ((struct file_name_map_list *)
4568 xmalloc (sizeof (struct file_name_map_list)));
4569 map_list_ptr->map_list_name = savestring (dirname);
4570 map_list_ptr->map_list_map = NULL;
4572 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
4573 strcpy (name, dirname);
4576 strcat (name, FILE_NAME_MAP_FILE);
4577 f = fopen (name, "r");
4579 map_list_ptr->map_list_map = NULL;
4583 int dirlen = strlen (dirname);
4585 while ((ch = getc (f)) != EOF)
4588 struct file_name_map *ptr;
4592 from = read_filename_string (ch, f);
4593 while ((ch = getc (f)) != EOF && is_hor_space[ch])
4595 to = read_filename_string (ch, f);
4597 ptr = ((struct file_name_map *)
4598 xmalloc (sizeof (struct file_name_map)));
4599 ptr->map_from = from;
4601 /* Make the real filename absolute. */
4606 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
4607 strcpy (ptr->map_to, dirname);
4608 ptr->map_to[dirlen] = '/';
4609 strcpy (ptr->map_to + dirlen + 1, to);
4613 ptr->map_next = map_list_ptr->map_list_map;
4614 map_list_ptr->map_list_map = ptr;
4616 while ((ch = getc (f)) != '\n')
4623 map_list_ptr->map_list_next = map_list;
4624 map_list = map_list_ptr;
4626 return map_list_ptr->map_list_map;
4629 /* Try to open include file FILENAME. SEARCHPTR is the directory
4630 being tried from the include file search path. This function maps
4631 filenames on file systems based on information read by
4635 open_include_file (filename, searchptr)
4637 struct file_name_list *searchptr;
4639 register struct file_name_map *map;
4640 register char *from;
4643 if (searchptr && ! searchptr->got_name_map)
4645 searchptr->name_map = read_name_map (searchptr->fname
4646 ? searchptr->fname : ".");
4647 searchptr->got_name_map = 1;
4650 /* First check the mapping for the directory we are using. */
4651 if (searchptr && searchptr->name_map)
4654 if (searchptr->fname)
4655 from += strlen (searchptr->fname) + 1;
4656 for (map = searchptr->name_map; map; map = map->map_next)
4658 if (! strcmp (map->map_from, from))
4660 /* Found a match. */
4661 return open (map->map_to, O_RDONLY, 0666);
4666 /* Try to find a mapping file for the particular directory we are
4667 looking in. Thus #include <sys/types.h> will look up sys/types.h
4668 in /usr/include/header.gcc and look up types.h in
4669 /usr/include/sys/header.gcc. */
4670 p = rindex (filename, '/');
4675 && strlen (searchptr->fname) == p - filename
4676 && ! strncmp (searchptr->fname, filename, p - filename))
4678 /* FILENAME is in SEARCHPTR, which we've already checked. */
4679 return open (filename, O_RDONLY, 0666);
4689 dir = (char *) alloca (p - filename + 1);
4690 bcopy (filename, dir, p - filename);
4691 dir[p - filename] = '\0';
4694 for (map = read_name_map (dir); map; map = map->map_next)
4695 if (! strcmp (map->map_from, from))
4696 return open (map->map_to, O_RDONLY, 0666);
4698 return open (filename, O_RDONLY, 0666);
4701 /* Process the contents of include file FNAME, already open on descriptor F,
4703 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
4704 "system" include directories (as decided by the `is_system_include'
4706 DIRPTR is the link in the dir path through which this file was found,
4707 or 0 if the file name was absolute. */
4710 finclude (f, fname, op, system_header_p, dirptr)
4714 int system_header_p;
4715 struct file_name_list *dirptr;
4720 FILE_BUF *fp; /* For input stack frame */
4721 int missing_newline = 0;
4723 CHECK_DEPTH (return;);
4725 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
4727 perror_with_name (fname);
4732 fp = &instack[indepth + 1];
4733 bzero ((char *) fp, sizeof (FILE_BUF));
4734 fp->nominal_fname = fp->fname = fname;
4737 fp->if_stack = if_stack;
4738 fp->system_header_p = system_header_p;
4741 if (S_ISREG (st_mode)) {
4742 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
4745 /* Read the file contents, knowing that st_size is an upper bound
4746 on the number of bytes we can read. */
4747 fp->length = safe_read (f, fp->buf, st_size);
4748 if (fp->length < 0) goto nope;
4750 else if (S_ISDIR (st_mode)) {
4751 error ("directory `%s' specified in #include", fname);
4755 /* Cannot count its file size before reading.
4756 First read the entire file into heap and
4757 copy them into buffer on stack. */
4762 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
4765 i = safe_read (f, fp->buf + st_size, bsize - st_size);
4767 goto nope; /* error! */
4769 if (st_size != bsize)
4770 break; /* End of file */
4772 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
4775 fp->length = st_size;
4778 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
4779 /* Backslash-newline at end is not good enough. */
4780 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
4781 fp->buf[fp->length++] = '\n';
4782 missing_newline = 1;
4784 fp->buf[fp->length] = '\0';
4786 /* Close descriptor now, so nesting does not use lots of descriptors. */
4789 /* Must do this before calling trigraph_pcp, so that the correct file name
4790 will be printed in warning messages. */
4793 input_file_stack_tick++;
4798 output_line_command (fp, op, 0, enter_file);
4801 if (missing_newline)
4804 if (pedantic && missing_newline)
4805 pedwarn ("file does not end in newline");
4808 input_file_stack_tick++;
4809 output_line_command (&instack[indepth], op, 0, leave_file);
4815 perror_with_name (fname);
4820 /* Record that inclusion of the file named FILE
4821 should be controlled by the macro named MACRO_NAME.
4822 This means that trying to include the file again
4823 will do something if that macro is defined. */
4826 record_control_macro (file, macro_name)
4830 struct file_name_list *new;
4832 for (new = all_include_files; new; new = new->next) {
4833 if (!strcmp (new->fname, file)) {
4834 new->control_macro = macro_name;
4839 /* If the file is not in all_include_files, something's wrong. */
4843 /* Maintain and search list of included files, for #import. */
4845 #define IMPORT_HASH_SIZE 31
4847 struct import_file {
4851 struct import_file *next;
4854 /* Hash table of files already included with #include or #import. */
4856 static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
4858 /* Hash a file name for import_hash_table. */
4866 while (*f) val += *f++;
4867 return (val%IMPORT_HASH_SIZE);
4870 /* Search for file FILENAME in import_hash_table.
4871 Return -2 if found, either a matching name or a matching inode.
4872 Otherwise, open the file and return a file descriptor if successful
4873 or -1 if unsuccessful. */
4876 lookup_import (filename, searchptr)
4878 struct file_name_list *searchptr;
4880 struct import_file *i;
4886 hashval = import_hash (filename);
4888 /* Attempt to find file in list of already included files */
4889 i = import_hash_table[hashval];
4892 if (!strcmp (filename, i->name))
4893 return -2; /* return found */
4896 /* Open it and try a match on inode/dev */
4897 fd = open_include_file (filename, searchptr);
4901 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
4902 i = import_hash_table[h];
4904 /* Compare the inode and the device.
4905 Supposedly on some systems the inode is not a scalar. */
4906 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
4907 && i->dev == sb.st_dev) {
4909 return -2; /* return found */
4914 return fd; /* Not found, return open file */
4917 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
4920 add_import (fd, fname)
4924 struct import_file *i;
4928 hashval = import_hash (fname);
4930 i = (struct import_file *)xmalloc (sizeof (struct import_file));
4931 i->name = (char *)xmalloc (strlen (fname)+1);
4932 strcpy (i->name, fname);
4933 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
4935 i->next = import_hash_table[hashval];
4936 import_hash_table[hashval] = i;
4939 /* Load the specified precompiled header into core, and verify its
4940 preconditions. PCF indicates the file descriptor to read, which must
4941 be a regular file. FNAME indicates the file name of the original
4942 header. *LIMIT will be set to an address one past the end of the file.
4943 If the preconditions of the file are not satisfied, the buffer is
4944 freed and we return 0. If the preconditions are satisfied, return
4945 the address of the buffer following the preconditions. The buffer, in
4946 this case, should never be freed because various pieces of it will
4947 be referred to until all precompiled strings are output at the end of
4951 check_precompiled (pcf, fname, limit)
4965 if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
4968 if (S_ISREG (st_mode))
4970 buf = xmalloc (st_size + 2);
4971 length = safe_read (pcf, buf, st_size);
4978 if (length > 0 && buf[length-1] != '\n')
4979 buf[length++] = '\n';
4982 *limit = buf + length;
4984 /* File is in core. Check the preconditions. */
4985 if (!check_preconditions (buf))
4987 for (cp = buf; *cp; cp++)
4990 fprintf (stderr, "Using preinclude %s\n", fname);
4996 fprintf (stderr, "Cannot use preinclude %s\n", fname);
5002 /* PREC (null terminated) points to the preconditions of a
5003 precompiled header. These are a series of #define and #undef
5004 lines which must match the current contents of the hash
5007 check_preconditions (prec)
5014 lineend = (char *) index (prec, '\n');
5016 if (*prec++ != '#') {
5017 error ("Bad format encountered while reading precompiled file");
5020 if (!strncmp (prec, "define", 6)) {
5024 mdef = create_definition (prec, lineend, NULL_PTR);
5029 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
5030 || (hp->type != T_MACRO && hp->type != T_CONST)
5031 || (hp->type == T_MACRO
5032 && !compare_defs (mdef.defn, hp->value.defn)
5033 && (mdef.defn->length != 2
5034 || mdef.defn->expansion[0] != '\n'
5035 || mdef.defn->expansion[1] != ' ')))
5037 } else if (!strncmp (prec, "undef", 5)) {
5042 while (is_hor_space[(U_CHAR) *prec])
5045 while (is_idchar[(U_CHAR) *prec])
5049 if (lookup (name, len, -1))
5052 error ("Bad format encountered while reading precompiled file");
5057 /* They all passed successfully */
5061 /* Process the main body of a precompiled file. BUF points to the
5062 string section of the file, following the preconditions. LIMIT is one
5063 character past the end. NAME is the name of the file being read
5064 in. OP is the main output buffer */
5066 pcfinclude (buf, limit, name, op)
5067 U_CHAR *buf, *limit, *name;
5074 /* First in the file comes 4 bytes indicating the number of strings, */
5075 /* in network byte order. (MSB first). */
5077 nstrings = (nstrings << 8) | *cp++;
5078 nstrings = (nstrings << 8) | *cp++;
5079 nstrings = (nstrings << 8) | *cp++;
5081 /* Looping over each string... */
5082 while (nstrings--) {
5083 U_CHAR *string_start;
5084 U_CHAR *endofthiskey;
5088 /* Each string starts with a STRINGDEF structure (str), followed */
5089 /* by the text of the string (string_start) */
5091 /* First skip to a longword boundary */
5092 /* ??? Why a 4-byte boundary? On all machines? */
5093 /* NOTE: This works correctly even if HOST_WIDE_INT
5094 is narrower than a pointer.
5095 Do not try risky measures here to get another type to use!
5096 Do not include stddef.h--it will fail! */
5097 if ((HOST_WIDE_INT) cp & 3)
5098 cp += 4 - ((HOST_WIDE_INT) cp & 3);
5100 /* Now get the string. */
5101 str = (STRINGDEF *) cp;
5102 string_start = cp += sizeof (STRINGDEF);
5104 for (; *cp; cp++) /* skip the string */
5107 /* We need to macro expand the string here to ensure that the
5108 proper definition environment is in place. If it were only
5109 expanded when we find out it is needed, macros necessary for
5110 its proper expansion might have had their definitions changed. */
5111 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
5112 /* Lineno is already set in the precompiled file */
5113 str->contents = tmpbuf.buf;
5114 str->len = tmpbuf.length;
5116 str->filename = name;
5117 str->output_mark = outbuf.bufp - outbuf.buf;
5120 *stringlist_tailp = str;
5121 stringlist_tailp = &str->chain;
5123 /* Next comes a fourbyte number indicating the number of keys */
5124 /* for this string. */
5126 nkeys = (nkeys << 8) | *cp++;
5127 nkeys = (nkeys << 8) | *cp++;
5128 nkeys = (nkeys << 8) | *cp++;
5130 /* If this number is -1, then the string is mandatory. */
5134 /* Otherwise, for each key, */
5135 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
5136 KEYDEF *kp = (KEYDEF *) cp;
5139 /* It starts with a KEYDEF structure */
5140 cp += sizeof (KEYDEF);
5142 /* Find the end of the key. At the end of this for loop we
5143 advance CP to the start of the next key using this variable. */
5144 endofthiskey = cp + strlen (cp);
5147 /* Expand the key, and enter it into the hash table. */
5148 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
5149 tmpbuf.bufp = tmpbuf.buf;
5151 while (is_hor_space[*tmpbuf.bufp])
5153 if (!is_idstart[*tmpbuf.bufp]
5154 || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
5159 hp = lookup (tmpbuf.bufp, -1, -1);
5162 install (tmpbuf.bufp, -1, T_PCSTRING, 0, (char *) kp, -1);
5164 else if (hp->type == T_PCSTRING) {
5165 kp->chain = hp->value.keydef;
5166 hp->value.keydef = kp;
5172 /* This output_line_command serves to switch us back to the current
5173 input file in case some of these strings get output (which will
5174 result in line commands for the header file being output). */
5175 output_line_command (&instack[indepth], op, 0, enter_file);
5178 /* Called from rescan when it hits a key for strings. Mark them all */
5179 /* used and clean up. */
5186 for (kp = hp->value.keydef; kp; kp = kp->chain)
5187 kp->str->writeflag = 1;
5191 /* Write the output, interspersing precompiled strings in their */
5192 /* appropriate places. */
5196 STRINGDEF *next_string;
5197 U_CHAR *cur_buf_loc;
5198 int line_command_len = 80;
5199 char *line_command = xmalloc (line_command_len);
5202 /* In each run through the loop, either cur_buf_loc == */
5203 /* next_string_loc, in which case we print a series of strings, or */
5204 /* it is less than next_string_loc, in which case we write some of */
5206 cur_buf_loc = outbuf.buf;
5207 next_string = stringlist;
5209 while (cur_buf_loc < outbuf.bufp || next_string) {
5211 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
5212 if (next_string->writeflag) {
5213 len = 4 * strlen (next_string->filename) + 32;
5214 while (len > line_command_len)
5215 line_command = xrealloc (line_command,
5216 line_command_len *= 2);
5217 sprintf (line_command, "\n# %d ", next_string->lineno);
5218 strcpy (quote_string (line_command + strlen (line_command),
5219 next_string->filename),
5221 safe_write (fileno (stdout), line_command, strlen (line_command));
5222 safe_write (fileno (stdout), next_string->contents, next_string->len);
5224 next_string = next_string->chain;
5228 ? (next_string->output_mark
5229 - (cur_buf_loc - outbuf.buf))
5230 : outbuf.bufp - cur_buf_loc);
5232 safe_write (fileno (stdout), cur_buf_loc, len);
5236 free (line_command);
5239 /* Pass a directive through to the output file.
5240 BUF points to the contents of the directive, as a contiguous string.
5241 LIMIT points to the first character past the end of the directive.
5242 KEYWORD is the keyword-table entry for the directive. */
5245 pass_thru_directive (buf, limit, op, keyword)
5246 U_CHAR *buf, *limit;
5248 struct directive *keyword;
5250 register unsigned keyword_length = keyword->length;
5252 check_expand (op, 1 + keyword_length + (limit - buf));
5254 bcopy (keyword->name, (char *) op->bufp, keyword_length);
5255 op->bufp += keyword_length;
5256 if (limit != buf && buf[0] != ' ')
5258 bcopy ((char *) buf, (char *) op->bufp, limit - buf);
5259 op->bufp += (limit - buf);
5262 /* Count the line we have just made in the output,
5263 to get in sync properly. */
5268 /* The arglist structure is built by do_define to tell
5269 collect_definition where the argument names begin. That
5270 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5271 would contain pointers to the strings x, y, and z.
5272 Collect_definition would then build a DEFINITION node,
5273 with reflist nodes pointing to the places x, y, and z had
5274 appeared. So the arglist is just convenience data passed
5275 between these two routines. It is not kept around after
5276 the current #define has been processed and entered into the
5280 struct arglist *next;
5287 /* Create a DEFINITION node from a #define directive. Arguments are
5288 as for do_define. */
5290 create_definition (buf, limit, op)
5291 U_CHAR *buf, *limit;
5294 U_CHAR *bp; /* temp ptr into input buffer */
5295 U_CHAR *symname; /* remember where symbol name starts */
5296 int sym_length; /* and how long it is */
5297 int line = instack[indepth].lineno;
5298 char *file = instack[indepth].nominal_fname;
5302 int arglengths = 0; /* Accumulate lengths of arg names
5303 plus number of args. */
5308 while (is_hor_space[*bp])
5311 symname = bp; /* remember where it starts */
5312 sym_length = check_macro_name (bp, "macro");
5315 /* Lossage will occur if identifiers or control keywords are broken
5316 across lines using backslash. This is not the right place to take
5320 struct arglist *arg_ptrs = NULL;
5323 bp++; /* skip '(' */
5324 SKIP_WHITE_SPACE (bp);
5326 /* Loop over macro argument names. */
5327 while (*bp != ')') {
5328 struct arglist *temp;
5330 temp = (struct arglist *) alloca (sizeof (struct arglist));
5332 temp->next = arg_ptrs;
5333 temp->argno = argno++;
5334 temp->rest_args = 0;
5338 pedwarn ("another parameter follows `%s'",
5341 if (!is_idstart[*bp])
5342 pedwarn ("invalid character in macro parameter name");
5344 /* Find the end of the arg name. */
5345 while (is_idchar[*bp]) {
5347 /* do we have a "special" rest-args extension here? */
5348 if (limit - bp > REST_EXTENSION_LENGTH &&
5349 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
5351 temp->rest_args = 1;
5355 temp->length = bp - temp->name;
5357 bp += REST_EXTENSION_LENGTH;
5358 arglengths += temp->length + 2;
5359 SKIP_WHITE_SPACE (bp);
5360 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
5361 error ("badly punctuated parameter list in `#define'");
5366 SKIP_WHITE_SPACE (bp);
5369 error ("unterminated parameter list in `#define'");
5373 struct arglist *otemp;
5375 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
5376 if (temp->length == otemp->length &&
5377 strncmp (temp->name, otemp->name, temp->length) == 0) {
5380 name = (U_CHAR *) alloca (temp->length + 1);
5381 (void) strncpy (name, temp->name, temp->length);
5382 name[temp->length] = '\0';
5383 error ("duplicate argument name `%s' in `#define'", name);
5389 ++bp; /* skip paren */
5390 /* Skip spaces and tabs if any. */
5391 while (bp < limit && (*bp == ' ' || *bp == '\t'))
5393 /* now everything from bp before limit is the definition. */
5394 defn = collect_expansion (bp, limit, argno, arg_ptrs);
5395 defn->rest_args = rest_args;
5397 /* Now set defn->args.argnames to the result of concatenating
5398 the argument names in reverse order
5399 with comma-space between them. */
5400 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
5402 struct arglist *temp;
5404 for (temp = arg_ptrs; temp; temp = temp->next) {
5405 bcopy (temp->name, &defn->args.argnames[i], temp->length);
5407 if (temp->next != 0) {
5408 defn->args.argnames[i++] = ',';
5409 defn->args.argnames[i++] = ' ';
5412 defn->args.argnames[i] = 0;
5415 /* Simple expansion or empty definition. */
5421 case '\t': case ' ':
5422 /* Skip spaces and tabs. */
5423 while (++bp < limit && (*bp == ' ' || *bp == '\t'))
5427 case '!': case '"': case '#': case '%': case '&': case '\'':
5428 case ')': case '*': case '+': case ',': case '-': case '.':
5429 case '/': case ':': case ';': case '<': case '=': case '>':
5430 case '?': case '[': case '\\': case ']': case '^': case '{':
5431 case '|': case '}': case '~':
5432 warning ("missing white space after `#define %.*s'",
5433 sym_length, symname);
5437 pedwarn ("missing white space after `#define %.*s'",
5438 sym_length, symname);
5442 /* Now everything from bp before limit is the definition. */
5443 defn = collect_expansion (bp, limit, -1, NULL_PTR);
5444 defn->args.argnames = (U_CHAR *) "";
5450 /* OP is null if this is a predefinition */
5451 defn->predefined = !op;
5453 mdef.symnam = symname;
5454 mdef.symlen = sym_length;
5463 /* Process a #define command.
5464 BUF points to the contents of the #define command, as a contiguous string.
5465 LIMIT points to the first character past the end of the definition.
5466 KEYWORD is the keyword-table entry for #define. */
5469 do_define (buf, limit, op, keyword)
5470 U_CHAR *buf, *limit;
5472 struct directive *keyword;
5477 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
5478 if (pcp_outfile && op)
5479 pass_thru_directive (buf, limit, op, keyword);
5481 mdef = create_definition (buf, limit, op);
5485 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
5489 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
5491 /* Redefining a precompiled key is ok. */
5492 if (hp->type == T_PCSTRING)
5494 /* Redefining a macro is ok if the definitions are the same. */
5495 else if (hp->type == T_MACRO)
5496 ok = ! compare_defs (mdef.defn, hp->value.defn);
5497 /* Redefining a constant is ok with -D. */
5498 else if (hp->type == T_CONST)
5499 ok = ! done_initializing;
5500 /* Print the warning if it's not ok. */
5502 U_CHAR *msg; /* what pain... */
5504 /* If we are passing through #define and #undef directives, do
5505 that for this re-definition now. */
5506 if (debug_output && op)
5507 pass_thru_directive (buf, limit, op, keyword);
5509 msg = (U_CHAR *) alloca (mdef.symlen + 22);
5511 bcopy ((char *) mdef.symnam, (char *) (msg + 1), mdef.symlen);
5512 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
5514 if (hp->type == T_MACRO)
5515 pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
5516 "this is the location of the previous definition");
5518 /* Replace the old definition. */
5520 hp->value.defn = mdef.defn;
5522 /* If we are passing through #define and #undef directives, do
5523 that for this new definition now. */
5524 if (debug_output && op)
5525 pass_thru_directive (buf, limit, op, keyword);
5526 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
5527 (char *) mdef.defn, hashcode);
5538 /* Check a purported macro name SYMNAME, and yield its length.
5539 USAGE is the kind of name this is intended for. */
5542 check_macro_name (symname, usage)
5549 for (p = symname; is_idchar[*p]; p++)
5551 sym_length = p - symname;
5552 if (sym_length == 0)
5553 error ("invalid %s name", usage);
5554 else if (!is_idstart[*symname]) {
5555 U_CHAR *msg; /* what pain... */
5556 msg = (U_CHAR *) alloca (sym_length + 1);
5557 bcopy ((char *) symname, (char *) msg, sym_length);
5558 msg[sym_length] = 0;
5559 error ("invalid %s name `%s'", usage, msg);
5561 if (! strncmp (symname, "defined", 7) && sym_length == 7)
5562 error ("invalid %s name `defined'", usage);
5568 * return zero if two DEFINITIONs are isomorphic
5571 compare_defs (d1, d2)
5572 DEFINITION *d1, *d2;
5574 register struct reflist *a1, *a2;
5575 register U_CHAR *p1 = d1->expansion;
5576 register U_CHAR *p2 = d2->expansion;
5579 if (d1->nargs != d2->nargs)
5581 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
5583 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
5584 a1 = a1->next, a2 = a2->next) {
5585 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
5586 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
5587 || a1->argno != a2->argno
5588 || a1->stringify != a2->stringify
5589 || a1->raw_before != a2->raw_before
5590 || a1->raw_after != a2->raw_after)
5598 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
5599 p2, d2->length - (p2 - d2->expansion), 1))
5604 /* Return 1 if two parts of two macro definitions are effectively different.
5605 One of the parts starts at BEG1 and has LEN1 chars;
5606 the other has LEN2 chars at BEG2.
5607 Any sequence of whitespace matches any other sequence of whitespace.
5608 FIRST means these parts are the first of a macro definition;
5609 so ignore leading whitespace entirely.
5610 LAST means these parts are the last of a macro definition;
5611 so ignore trailing whitespace entirely. */
5614 comp_def_part (first, beg1, len1, beg2, len2, last)
5616 U_CHAR *beg1, *beg2;
5620 register U_CHAR *end1 = beg1 + len1;
5621 register U_CHAR *end2 = beg2 + len2;
5623 while (beg1 != end1 && is_space[*beg1]) beg1++;
5624 while (beg2 != end2 && is_space[*beg2]) beg2++;
5627 while (beg1 != end1 && is_space[end1[-1]]) end1--;
5628 while (beg2 != end2 && is_space[end2[-1]]) end2--;
5630 while (beg1 != end1 && beg2 != end2) {
5631 if (is_space[*beg1] && is_space[*beg2]) {
5632 while (beg1 != end1 && is_space[*beg1]) beg1++;
5633 while (beg2 != end2 && is_space[*beg2]) beg2++;
5634 } else if (*beg1 == *beg2) {
5638 return (beg1 != end1) || (beg2 != end2);
5641 /* Read a replacement list for a macro with parameters.
5642 Build the DEFINITION structure.
5643 Reads characters of text starting at BUF until END.
5644 ARGLIST specifies the formal parameters to look for
5645 in the text of the definition; NARGS is the number of args
5646 in that list, or -1 for a macro name that wants no argument list.
5647 MACRONAME is the macro name itself (so we can avoid recursive expansion)
5648 and NAMELEN is its length in characters.
5650 Note that comments and backslash-newlines have already been deleted
5651 from the argument. */
5653 /* Leading and trailing Space, Tab, etc. are converted to markers
5654 Newline Space, Newline Tab, etc.
5655 Newline Space makes a space in the final output
5656 but is discarded if stringified. (Newline Tab is similar but
5657 makes a Tab instead.)
5659 If there is no trailing whitespace, a Newline Space is added at the end
5660 to prevent concatenation that would be contrary to the standard. */
5663 collect_expansion (buf, end, nargs, arglist)
5666 struct arglist *arglist;
5669 register U_CHAR *p, *limit, *lastp, *exp_p;
5670 struct reflist *endpat = NULL;
5671 /* Pointer to first nonspace after last ## seen. */
5673 /* Pointer to first nonspace after last single-# seen. */
5674 U_CHAR *stringify = 0;
5676 int expected_delimiter = '\0';
5678 /* Scan thru the replacement list, ignoring comments and quoted
5679 strings, picking up on the macro calls. It does a linear search
5680 thru the arg list on every potential symbol. Profiling might say
5681 that something smarter should happen. */
5686 /* Find the beginning of the trailing whitespace. */
5687 /* Find end of leading whitespace. */
5690 while (p < limit && is_space[limit[-1]]) limit--;
5691 while (p < limit && is_space[*p]) p++;
5693 /* Allocate space for the text in the macro definition.
5694 Leading and trailing whitespace chars need 2 bytes each.
5695 Each other input char may or may not need 1 byte,
5696 so this is an upper bound.
5697 The extra 2 are for invented trailing newline-marker and final null. */
5698 maxsize = (sizeof (DEFINITION)
5699 + 2 * (end - limit) + 2 * (p - buf)
5701 defn = (DEFINITION *) xcalloc (1, maxsize);
5703 defn->nargs = nargs;
5704 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
5709 /* Convert leading whitespace to Newline-markers. */
5710 while (p < limit && is_space[*p]) {
5715 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
5716 error ("`##' at start of macro definition");
5720 /* Process the main body of the definition. */
5722 int skipped_arg = 0;
5723 register U_CHAR c = *p++;
5731 if (expected_delimiter != '\0') {
5732 if (c == expected_delimiter)
5733 expected_delimiter = '\0';
5735 expected_delimiter = c;
5738 /* Special hack: if a \# is written in the #define
5739 include a # in the definition. This is useless for C code
5740 but useful for preprocessing other things. */
5743 /* \# quotes a # even outside of strings. */
5744 if (p < limit && *p == '#' && !expected_delimiter) {
5747 } else if (p < limit && expected_delimiter) {
5748 /* In a string, backslash goes through
5749 and makes next char ordinary. */
5755 /* # is ordinary inside a string. */
5756 if (expected_delimiter)
5758 if (p < limit && *p == '#') {
5759 /* ##: concatenate preceding and following tokens. */
5760 /* Take out the first #, discard preceding whitespace. */
5762 while (exp_p > lastp && is_hor_space[exp_p[-1]])
5764 /* Skip the second #. */
5766 /* Discard following whitespace. */
5767 SKIP_WHITE_SPACE (p);
5770 error ("`##' at end of macro definition");
5771 } else if (nargs >= 0) {
5772 /* Single #: stringify following argument ref.
5773 Don't leave the # in the expansion. */
5775 SKIP_WHITE_SPACE (p);
5776 if (p == limit || ! is_idstart[*p])
5777 error ("`#' operator is not followed by a macro argument name");
5784 /* In -traditional mode, recognize arguments inside strings and
5785 and character constants, and ignore special properties of #.
5786 Arguments inside strings are considered "stringified", but no
5787 extra quote marks are supplied. */
5791 if (expected_delimiter != '\0') {
5792 if (c == expected_delimiter)
5793 expected_delimiter = '\0';
5795 expected_delimiter = c;
5799 /* Backslash quotes delimiters and itself, but not macro args. */
5800 if (expected_delimiter != 0 && p < limit
5801 && (*p == expected_delimiter || *p == '\\')) {
5808 if (expected_delimiter != '\0') /* No comments inside strings. */
5811 /* If we find a comment that wasn't removed by handle_directive,
5812 this must be -traditional. So replace the comment with
5816 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
5819 /* Mark this as a concatenation-point, as if it had been ##. */
5827 /* Handle the start of a symbol. */
5828 if (is_idchar[c] && nargs > 0) {
5829 U_CHAR *id_beg = p - 1;
5833 while (p != limit && is_idchar[*p]) p++;
5834 id_len = p - id_beg;
5836 if (is_idstart[c]) {
5837 register struct arglist *arg;
5839 for (arg = arglist; arg != NULL; arg = arg->next) {
5840 struct reflist *tpat;
5842 if (arg->name[0] == c
5843 && arg->length == id_len
5844 && strncmp (arg->name, id_beg, id_len) == 0) {
5845 if (expected_delimiter && warn_stringify) {
5847 warning ("macro argument `%.*s' is stringified.",
5850 warning ("macro arg `%.*s' would be stringified with -traditional.",
5854 /* If ANSI, don't actually substitute inside a string. */
5855 if (!traditional && expected_delimiter)
5857 /* make a pat node for this arg and append it to the end of
5859 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
5861 tpat->raw_before = concat == id_beg;
5862 tpat->raw_after = 0;
5863 tpat->rest_args = arg->rest_args;
5864 tpat->stringify = (traditional ? expected_delimiter != '\0'
5865 : stringify == id_beg);
5868 defn->pattern = tpat;
5870 endpat->next = tpat;
5873 tpat->argno = arg->argno;
5874 tpat->nchars = exp_p - lastp;
5876 register U_CHAR *p1 = p;
5877 SKIP_WHITE_SPACE (p1);
5878 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
5879 tpat->raw_after = 1;
5881 lastp = exp_p; /* place to start copying from next time */
5888 /* If this was not a macro arg, copy it into the expansion. */
5889 if (! skipped_arg) {
5890 register U_CHAR *lim1 = p;
5894 if (stringify == id_beg)
5895 error ("`#' operator should be followed by a macro argument name");
5900 if (!traditional && expected_delimiter == 0) {
5901 /* There is no trailing whitespace, so invent some in ANSI mode.
5902 But not if "inside a string" (which in ANSI mode
5903 happens only for -D option). */
5910 defn->length = exp_p - defn->expansion;
5912 /* Crash now if we overrun the allocated size. */
5913 if (defn->length + 1 > maxsize)
5917 /* This isn't worth the time it takes. */
5918 /* give back excess storage */
5919 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
5926 do_assert (buf, limit, op, keyword)
5927 U_CHAR *buf, *limit;
5929 struct directive *keyword;
5931 U_CHAR *bp; /* temp ptr into input buffer */
5932 U_CHAR *symname; /* remember where symbol name starts */
5933 int sym_length; /* and how long it is */
5934 struct arglist *tokens = NULL;
5936 if (pedantic && done_initializing && !instack[indepth].system_header_p)
5937 pedwarn ("ANSI C does not allow `#assert'");
5941 while (is_hor_space[*bp])
5944 symname = bp; /* remember where it starts */
5945 sym_length = check_macro_name (bp, "assertion");
5947 /* #define doesn't do this, but we should. */
5948 SKIP_WHITE_SPACE (bp);
5950 /* Lossage will occur if identifiers or control tokens are broken
5951 across lines using backslash. This is not the right place to take
5955 error ("missing token-sequence in `#assert'");
5962 bp++; /* skip '(' */
5963 SKIP_WHITE_SPACE (bp);
5965 tokens = read_token_list (&bp, limit, &error_flag);
5969 error ("empty token-sequence in `#assert'");
5973 ++bp; /* skip paren */
5974 SKIP_WHITE_SPACE (bp);
5977 /* If this name isn't already an assertion name, make it one.
5978 Error if it was already in use in some other way. */
5981 ASSERTION_HASHNODE *hp;
5982 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
5983 struct tokenlist_list *value
5984 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
5986 hp = assertion_lookup (symname, sym_length, hashcode);
5988 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
5989 error ("`defined' redefined as assertion");
5990 hp = assertion_install (symname, sym_length, hashcode);
5993 /* Add the spec'd token-sequence to the list of such. */
5994 value->tokens = tokens;
5995 value->next = hp->value;
6003 do_unassert (buf, limit, op, keyword)
6004 U_CHAR *buf, *limit;
6006 struct directive *keyword;
6008 U_CHAR *bp; /* temp ptr into input buffer */
6009 U_CHAR *symname; /* remember where symbol name starts */
6010 int sym_length; /* and how long it is */
6012 struct arglist *tokens = NULL;
6013 int tokens_specified = 0;
6015 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6016 pedwarn ("ANSI C does not allow `#unassert'");
6020 while (is_hor_space[*bp])
6023 symname = bp; /* remember where it starts */
6024 sym_length = check_macro_name (bp, "assertion");
6026 /* #define doesn't do this, but we should. */
6027 SKIP_WHITE_SPACE (bp);
6029 /* Lossage will occur if identifiers or control tokens are broken
6030 across lines using backslash. This is not the right place to take
6036 bp++; /* skip '(' */
6037 SKIP_WHITE_SPACE (bp);
6039 tokens = read_token_list (&bp, limit, &error_flag);
6043 error ("empty token list in `#unassert'");
6047 tokens_specified = 1;
6049 ++bp; /* skip paren */
6050 SKIP_WHITE_SPACE (bp);
6054 ASSERTION_HASHNODE *hp;
6055 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6056 struct tokenlist_list *tail, *prev;
6058 hp = assertion_lookup (symname, sym_length, hashcode);
6062 /* If no token list was specified, then eliminate this assertion
6064 if (! tokens_specified) {
6065 struct tokenlist_list *next;
6066 for (tail = hp->value; tail; tail = next) {
6068 free_token_list (tail->tokens);
6071 delete_assertion (hp);
6073 /* If a list of tokens was given, then delete any matching list. */
6078 struct tokenlist_list *next = tail->next;
6079 if (compare_token_lists (tail->tokens, tokens)) {
6083 hp->value = tail->next;
6084 free_token_list (tail->tokens);
6097 /* Test whether there is an assertion named NAME
6098 and optionally whether it has an asserted token list TOKENS.
6099 NAME is not null terminated; its length is SYM_LENGTH.
6100 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6103 check_assertion (name, sym_length, tokens_specified, tokens)
6106 int tokens_specified;
6107 struct arglist *tokens;
6109 ASSERTION_HASHNODE *hp;
6110 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6112 if (pedantic && !instack[indepth].system_header_p)
6113 pedwarn ("ANSI C does not allow testing assertions");
6115 hp = assertion_lookup (name, sym_length, hashcode);
6117 /* It is not an assertion; just return false. */
6120 /* If no token list was specified, then value is 1. */
6121 if (! tokens_specified)
6125 struct tokenlist_list *tail;
6129 /* If a list of tokens was given,
6130 then succeed if the assertion records a matching list. */
6133 if (compare_token_lists (tail->tokens, tokens))
6138 /* Fail if the assertion has no matching list. */
6143 /* Compare two lists of tokens for equality including order of tokens. */
6146 compare_token_lists (l1, l2)
6147 struct arglist *l1, *l2;
6150 if (l1->length != l2->length)
6152 if (strncmp (l1->name, l2->name, l1->length))
6158 /* Succeed if both lists end at the same time. */
6162 /* Read a space-separated list of tokens ending in a close parenthesis.
6163 Return a list of strings, in the order they were written.
6164 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6165 Parse the text starting at *BPP, and update *BPP.
6166 Don't parse beyond LIMIT. */
6168 static struct arglist *
6169 read_token_list (bpp, limit, error_flag)
6174 struct arglist *token_ptrs = 0;
6180 /* Loop over the assertion value tokens. */
6182 struct arglist *temp;
6186 /* Find the end of the token. */
6190 } else if (*bp == ')') {
6195 } else if (*bp == '"' || *bp == '\'')
6196 bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
6198 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
6199 && *bp != '"' && *bp != '\'' && bp != limit)
6202 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
6203 temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
6204 bcopy ((char *) beg, (char *) temp->name, bp - beg);
6205 temp->name[bp - beg] = 0;
6206 temp->next = token_ptrs;
6208 temp->length = bp - beg;
6210 SKIP_WHITE_SPACE (bp);
6213 error ("unterminated token sequence in `#assert' or `#unassert'");
6220 /* We accumulated the names in reverse order.
6221 Now reverse them to get the proper order. */
6223 register struct arglist *prev = 0, *this, *next;
6224 for (this = token_ptrs; this; this = next) {
6234 free_token_list (tokens)
6235 struct arglist *tokens;
6238 struct arglist *next = tokens->next;
6239 free (tokens->name);
6246 * Install a name in the assertion hash table.
6248 * If LEN is >= 0, it is the length of the name.
6249 * Otherwise, compute the length by scanning the entire name.
6251 * If HASH is >= 0, it is the precomputed hash code.
6252 * Otherwise, compute the hash code.
6254 static ASSERTION_HASHNODE *
6255 assertion_install (name, len, hash)
6260 register ASSERTION_HASHNODE *hp;
6261 register int i, bucket;
6262 register U_CHAR *p, *q;
6264 i = sizeof (ASSERTION_HASHNODE) + len + 1;
6265 hp = (ASSERTION_HASHNODE *) xmalloc (i);
6267 hp->bucket_hdr = &assertion_hashtab[bucket];
6268 hp->next = assertion_hashtab[bucket];
6269 assertion_hashtab[bucket] = hp;
6271 if (hp->next != NULL)
6272 hp->next->prev = hp;
6275 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
6278 for (i = 0; i < len; i++)
6285 * find the most recent hash node for name name (ending with first
6286 * non-identifier char) installed by install
6288 * If LEN is >= 0, it is the length of the name.
6289 * Otherwise, compute the length by scanning the entire name.
6291 * If HASH is >= 0, it is the precomputed hash code.
6292 * Otherwise, compute the hash code.
6294 static ASSERTION_HASHNODE *
6295 assertion_lookup (name, len, hash)
6300 register ASSERTION_HASHNODE *bucket;
6302 bucket = assertion_hashtab[hash];
6304 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
6306 bucket = bucket->next;
6312 delete_assertion (hp)
6313 ASSERTION_HASHNODE *hp;
6316 if (hp->prev != NULL)
6317 hp->prev->next = hp->next;
6318 if (hp->next != NULL)
6319 hp->next->prev = hp->prev;
6321 /* make sure that the bucket chain header that
6322 the deleted guy was on points to the right thing afterwards. */
6323 if (hp == *hp->bucket_hdr)
6324 *hp->bucket_hdr = hp->next;
6330 * interpret #line command. Remembers previously seen fnames
6331 * in its very own hash table.
6333 #define FNAME_HASHSIZE 37
6336 do_line (buf, limit, op, keyword)
6337 U_CHAR *buf, *limit;
6339 struct directive *keyword;
6341 register U_CHAR *bp;
6342 FILE_BUF *ip = &instack[indepth];
6345 enum file_change_code file_change = same_file;
6347 /* Expand any macros. */
6348 tem = expand_to_temp_buffer (buf, limit, 0, 0);
6350 /* Point to macroexpanded line, which is null-terminated now. */
6352 SKIP_WHITE_SPACE (bp);
6354 if (!isdigit (*bp)) {
6355 error ("invalid format `#line' command");
6359 /* The Newline at the end of this line remains to be processed.
6360 To put the next line at the specified line number,
6361 we must store a line number now that is one less. */
6362 new_lineno = atoi (bp) - 1;
6364 /* NEW_LINENO is one less than the actual line number here. */
6365 if (pedantic && new_lineno < 0)
6366 pedwarn ("line number out of range in `#line' command");
6368 /* skip over the line number. */
6369 while (isdigit (*bp))
6372 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6373 if (*bp && !is_space[*bp]) {
6374 error ("invalid format `#line' command");
6379 SKIP_WHITE_SPACE (bp);
6382 static HASHNODE *fname_table[FNAME_HASHSIZE];
6383 HASHNODE *hp, **hash_bucket;
6389 /* Turn the file name, which is a character string literal,
6390 into a null-terminated string. Do this in place. */
6393 switch ((*p++ = *bp++)) {
6395 error ("invalid format `#line' command");
6400 char *bpc = (char *) bp;
6401 int c = parse_escape (&bpc);
6402 bp = (U_CHAR *) bpc;
6415 fname_length = p - fname;
6417 SKIP_WHITE_SPACE (bp);
6420 pedwarn ("garbage at end of `#line' command");
6422 file_change = enter_file;
6423 else if (*bp == '2')
6424 file_change = leave_file;
6425 else if (*bp == '3')
6426 ip->system_header_p = 1;
6427 else if (*bp == '4')
6428 ip->system_header_p = 2;
6430 error ("invalid format `#line' command");
6435 SKIP_WHITE_SPACE (bp);
6437 ip->system_header_p = 1;
6439 SKIP_WHITE_SPACE (bp);
6442 ip->system_header_p = 2;
6444 SKIP_WHITE_SPACE (bp);
6447 error ("invalid format `#line' command");
6453 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
6454 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
6455 if (hp->length == fname_length &&
6456 strncmp (hp->value.cpval, fname, fname_length) == 0) {
6457 ip->nominal_fname = hp->value.cpval;
6461 /* Didn't find it; cons up a new one. */
6462 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
6463 hp->next = *hash_bucket;
6466 hp->length = fname_length;
6467 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
6468 bcopy (fname, hp->value.cpval, fname_length);
6471 error ("invalid format `#line' command");
6475 ip->lineno = new_lineno;
6476 output_line_command (ip, op, 0, file_change);
6477 check_expand (op, ip->length - (ip->bufp - ip->buf));
6482 * remove the definition of a symbol from the symbol table.
6483 * according to un*x /lib/cpp, it is not an error to undef
6484 * something that has no definitions, so it isn't one here either.
6488 do_undef (buf, limit, op, keyword)
6489 U_CHAR *buf, *limit;
6491 struct directive *keyword;
6495 U_CHAR *orig_buf = buf;
6497 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
6498 if (pcp_outfile && op)
6499 pass_thru_directive (buf, limit, op, keyword);
6501 SKIP_WHITE_SPACE (buf);
6502 sym_length = check_macro_name (buf, "macro");
6504 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
6505 /* If we are generating additional info for debugging (with -g) we
6506 need to pass through all effective #undef commands. */
6507 if (debug_output && op)
6508 pass_thru_directive (orig_buf, limit, op, keyword);
6509 if (hp->type != T_MACRO)
6510 warning ("undefining `%s'", hp->name);
6516 SKIP_WHITE_SPACE (buf);
6518 pedwarn ("garbage after `#undef' directive");
6524 * Report an error detected by the program we are processing.
6525 * Use the text of the line in the error message.
6526 * (We use error because it prints the filename & line#.)
6530 do_error (buf, limit, op, keyword)
6531 U_CHAR *buf, *limit;
6533 struct directive *keyword;
6535 int length = limit - buf;
6536 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
6537 bcopy ((char *) buf, (char *) copy, length);
6539 SKIP_WHITE_SPACE (copy);
6540 error ("#error %s", copy);
6545 * Report a warning detected by the program we are processing.
6546 * Use the text of the line in the warning message, then continue.
6547 * (We use error because it prints the filename & line#.)
6551 do_warning (buf, limit, op, keyword)
6552 U_CHAR *buf, *limit;
6554 struct directive *keyword;
6556 int length = limit - buf;
6557 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
6558 bcopy ((char *) buf, (char *) copy, length);
6560 SKIP_WHITE_SPACE (copy);
6561 warning ("#warning %s", copy);
6565 /* Remember the name of the current file being read from so that we can
6566 avoid ever including it again. */
6572 FILE_BUF *ip = NULL;
6574 for (i = indepth; i >= 0; i--)
6575 if (instack[i].fname != NULL) {
6581 struct file_name_list *new;
6583 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
6584 new->next = dont_repeat_files;
6585 dont_repeat_files = new;
6586 new->fname = savestring (ip->fname);
6587 new->control_macro = 0;
6588 new->got_name_map = 0;
6589 new->c_system_include_path = 0;
6594 /* #ident has already been copied to the output file, so just ignore it. */
6597 do_ident (buf, limit)
6598 U_CHAR *buf, *limit;
6602 FILE_BUF *op = &outbuf;
6604 /* Allow #ident in system headers, since that's not user's fault. */
6605 if (pedantic && !instack[indepth].system_header_p)
6606 pedwarn ("ANSI C does not allow `#ident'");
6608 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
6609 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
6610 bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
6611 limit = buf + (trybuf.bufp - trybuf.buf);
6612 len = (limit - buf);
6615 /* Output directive name. */
6616 check_expand (op, 7);
6617 bcopy ("#ident ", (char *) op->bufp, 7);
6620 /* Output the expanded argument line. */
6621 check_expand (op, len);
6622 bcopy ((char *) buf, (char *) op->bufp, len);
6628 /* #pragma and its argument line have already been copied to the output file.
6629 Just check for some recognized pragmas that need validation here. */
6632 do_pragma (buf, limit)
6633 U_CHAR *buf, *limit;
6635 while (*buf == ' ' || *buf == '\t')
6637 if (!strncmp (buf, "once", 4)) {
6638 /* Allow #pragma once in system headers, since that's not the user's
6640 if (!instack[indepth].system_header_p)
6641 warning ("`#pragma once' is obsolete");
6645 if (!strncmp (buf, "implementation", 14)) {
6646 /* Be quiet about `#pragma implementation' for a file only if it hasn't
6647 been included yet. */
6648 struct file_name_list *ptr;
6649 U_CHAR *p = buf + 14, *fname, *inc_fname;
6650 SKIP_WHITE_SPACE (p);
6651 if (*p == '\n' || *p != '\"')
6655 if (p = (U_CHAR *) index (fname, '\"'))
6658 for (ptr = all_include_files; ptr; ptr = ptr->next) {
6659 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
6660 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
6661 if (inc_fname && !strcmp (inc_fname, fname))
6662 warning ("`#pragma implementation' for `%s' appears after file is included",
6671 /* This was a fun hack, but #pragma seems to start to be useful.
6672 By failing to recognize it, we pass it through unchanged to cc1. */
6675 * the behavior of the #pragma directive is implementation defined.
6676 * this implementation defines it as follows.
6683 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
6686 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
6688 execl ("/usr/games/hack", "#pragma", 0);
6689 execl ("/usr/games/rogue", "#pragma", 0);
6690 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
6691 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
6693 fatal ("You are in a maze of twisty compiler features, all different");
6697 /* Just ignore #sccs, on systems where we define it at all. */
6703 pedwarn ("ANSI C does not allow `#sccs'");
6708 * handle #if command by
6709 * 1) inserting special `defined' keyword into the hash table
6710 * that gets turned into 0 or 1 by special_symbol (thus,
6711 * if the luser has a symbol called `defined' already, it won't
6712 * work inside the #if command)
6713 * 2) rescan the input into a temporary output buffer
6714 * 3) pass the output buffer to the yacc parser and collect a value
6715 * 4) clean up the mess left from steps 1 and 2.
6716 * 5) call conditional_skip to skip til the next #endif (etc.),
6717 * or not, depending on the value from step 3.
6721 do_if (buf, limit, op, keyword)
6722 U_CHAR *buf, *limit;
6724 struct directive *keyword;
6727 FILE_BUF *ip = &instack[indepth];
6729 value = eval_if_expression (buf, limit - buf);
6730 conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
6735 * handle a #elif directive by not changing if_stack either.
6736 * see the comment above do_else.
6740 do_elif (buf, limit, op, keyword)
6741 U_CHAR *buf, *limit;
6743 struct directive *keyword;
6746 FILE_BUF *ip = &instack[indepth];
6748 if (if_stack == instack[indepth].if_stack) {
6749 error ("`#elif' not within a conditional");
6752 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
6753 error ("`#elif' after `#else'");
6754 fprintf (stderr, " (matches line %d", if_stack->lineno);
6755 if (if_stack->fname != NULL && ip->fname != NULL &&
6756 strcmp (if_stack->fname, ip->nominal_fname) != 0)
6757 fprintf (stderr, ", file %s", if_stack->fname);
6758 fprintf (stderr, ")\n");
6760 if_stack->type = T_ELIF;
6763 if (if_stack->if_succeeded)
6764 skip_if_group (ip, 0, op);
6766 value = eval_if_expression (buf, limit - buf);
6768 skip_if_group (ip, 0, op);
6770 ++if_stack->if_succeeded; /* continue processing input */
6771 output_line_command (ip, op, 1, same_file);
6778 * evaluate a #if expression in BUF, of length LENGTH,
6779 * then parse the result as a C expression and return the value as an int.
6782 eval_if_expression (buf, length)
6787 HASHNODE *save_defined;
6790 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
6792 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
6794 delete_macro (save_defined); /* clean up special symbol */
6796 value = parse_c_expression (temp_obuf.buf);
6798 free (temp_obuf.buf);
6804 * routine to handle ifdef/ifndef. Try to look up the symbol,
6805 * then do or don't skip to the #endif/#else/#elif depending
6806 * on what directive is actually being processed.
6810 do_xifdef (buf, limit, op, keyword)
6811 U_CHAR *buf, *limit;
6813 struct directive *keyword;
6816 FILE_BUF *ip = &instack[indepth];
6818 int start_of_file = 0;
6819 U_CHAR *control_macro = 0;
6821 /* Detect a #ifndef at start of file (not counting comments). */
6822 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
6823 U_CHAR *p = ip->buf;
6824 while (p != directive_start) {
6828 else if (c == '/' && p != ip->bufp && *p == '*') {
6829 /* Skip this comment. */
6831 U_CHAR *save_bufp = ip->bufp;
6833 p = skip_to_end_of_comment (ip, &junk, 1);
6834 ip->bufp = save_bufp;
6839 /* If we get here, this conditional is the beginning of the file. */
6844 /* Discard leading and trailing whitespace. */
6845 SKIP_WHITE_SPACE (buf);
6846 while (limit != buf && is_hor_space[limit[-1]]) limit--;
6848 /* Find the end of the identifier at the beginning. */
6849 for (end = buf; is_idchar[*end]; end++);
6852 skip = (keyword->type == T_IFDEF);
6854 pedwarn (end == limit ? "`#%s' with no argument"
6855 : "`#%s' argument starts with punctuation",
6860 if (pedantic && buf[0] >= '0' && buf[0] <= '9')
6861 pedwarn ("`#%s' argument starts with a digit", keyword->name);
6862 else if (end != limit && !traditional)
6863 pedwarn ("garbage at end of `#%s' argument", keyword->name);
6865 hp = lookup (buf, end-buf, -1);
6868 /* Output a precondition for this macro. */
6870 (hp->type == T_CONST
6871 || (hp->type == T_MACRO && hp->value.defn->predefined)))
6872 fprintf (pcp_outfile, "#define %s\n", hp->name);
6875 fprintf (pcp_outfile, "#undef ");
6876 while (is_idchar[*cp]) /* Ick! */
6877 fputc (*cp++, pcp_outfile);
6878 putc ('\n', pcp_outfile);
6882 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
6883 if (start_of_file && !skip) {
6884 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
6885 bcopy ((char *) buf, (char *) control_macro, end - buf);
6886 control_macro[end - buf] = 0;
6890 conditional_skip (ip, skip, T_IF, control_macro, op);
6894 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
6895 If this is a #ifndef starting at the beginning of a file,
6896 CONTROL_MACRO is the macro name tested by the #ifndef.
6897 Otherwise, CONTROL_MACRO is 0. */
6900 conditional_skip (ip, skip, type, control_macro, op)
6903 enum node_type type;
6904 U_CHAR *control_macro;
6907 IF_STACK_FRAME *temp;
6909 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
6910 temp->fname = ip->nominal_fname;
6911 temp->lineno = ip->lineno;
6912 temp->next = if_stack;
6913 temp->control_macro = control_macro;
6916 if_stack->type = type;
6919 skip_if_group (ip, 0, op);
6922 ++if_stack->if_succeeded;
6923 output_line_command (ip, &outbuf, 1, same_file);
6928 * skip to #endif, #else, or #elif. adjust line numbers, etc.
6929 * leaves input ptr at the sharp sign found.
6930 * If ANY is nonzero, return at next directive of any sort.
6933 skip_if_group (ip, any, op)
6938 register U_CHAR *bp = ip->bufp, *cp;
6939 register U_CHAR *endb = ip->buf + ip->length;
6940 struct directive *kt;
6941 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
6942 U_CHAR *beg_of_line = bp;
6943 register int ident_length;
6944 U_CHAR *ident, *after_ident;
6945 /* Save info about where the group starts. */
6946 U_CHAR *beg_of_group = bp;
6947 int beg_lineno = ip->lineno;
6949 if (output_conditionals && op != 0) {
6950 char *ptr = "#failed\n";
6951 int len = strlen (ptr);
6953 if (op->bufp > op->buf && op->bufp[-1] != '\n')
6958 check_expand (op, len);
6959 bcopy (ptr, (char *) op->bufp, len);
6962 output_line_command (ip, op, 1, 0);
6967 case '/': /* possible comment */
6968 if (*bp == '\\' && bp[1] == '\n')
6971 || (cplusplus_comments && *bp == '/')) {
6973 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
6978 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
6979 NULL_PTR, NULL_PTR);
6982 /* Char after backslash loses its special meaning. */
6985 ++ip->lineno; /* But do update the line-count. */
6996 /* # keyword: a # must be first nonblank char on the line */
6997 if (beg_of_line == 0)
6999 /* Scan from start of line, skipping whitespace, comments
7000 and backslash-newlines, and see if we reach this #.
7001 If not, this # is not special. */
7003 /* If -traditional, require # to be at beginning of line. */
7006 if (is_hor_space[*bp])
7008 else if (*bp == '\\' && bp[1] == '\n')
7010 else if (*bp == '/' && bp[1] == '*') {
7012 while (!(*bp == '*' && bp[1] == '/'))
7016 /* There is no point in trying to deal with C++ // comments here,
7017 because if there is one, then this # must be part of the
7018 comment and we would never reach here. */
7021 if (bp != ip->bufp) {
7022 bp = ip->bufp + 1; /* Reset bp to after the #. */
7026 bp = ip->bufp + 1; /* Point after the '#' */
7028 /* Skip whitespace and \-newline. */
7030 if (is_hor_space[*bp])
7032 else if (*bp == '\\' && bp[1] == '\n')
7034 else if (*bp == '/' && bp[1] == '*') {
7036 while (!(*bp == '*' && bp[1] == '/')) {
7042 } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
7044 while (bp[-1] == '\\' || *bp != '\n') {
7055 /* Now find end of directive name.
7056 If we encounter a backslash-newline, exchange it with any following
7057 symbol-constituents so that we end up with a contiguous name. */
7063 if (*bp == '\\' && bp[1] == '\n')
7064 name_newline_fix (bp);
7070 ident_length = bp - cp;
7074 /* A line of just `#' becomes blank. */
7076 if (ident_length == 0 && *after_ident == '\n') {
7080 if (ident_length == 0 || !is_idstart[*ident]) {
7082 while (is_idchar[*p]) {
7083 if (*p < '0' || *p > '9')
7087 /* Handle # followed by a line number. */
7088 if (p != ident && !is_idchar[*p]) {
7090 pedwarn ("`#' followed by integer");
7094 /* Avoid error for `###' and similar cases unless -pedantic. */
7096 while (*p == '#' || is_hor_space[*p]) p++;
7098 if (pedantic && !lang_asm)
7099 pedwarn ("invalid preprocessor directive");
7104 if (!lang_asm && pedantic)
7105 pedwarn ("invalid preprocessor directive name");
7109 for (kt = directive_table; kt->length >= 0; kt++) {
7110 IF_STACK_FRAME *temp;
7111 if (ident_length == kt->length
7112 && strncmp (cp, kt->name, kt->length) == 0) {
7113 /* If we are asked to return on next directive, do so now. */
7121 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7122 temp->next = if_stack;
7124 temp->lineno = ip->lineno;
7125 temp->fname = ip->nominal_fname;
7126 temp->type = kt->type;
7130 if (pedantic && if_stack != save_if_stack)
7133 if (if_stack == instack[indepth].if_stack) {
7134 error ("`#%s' not within a conditional", kt->name);
7137 else if (if_stack == save_if_stack)
7138 goto done; /* found what we came for */
7140 if (kt->type != T_ENDIF) {
7141 if (if_stack->type == T_ELSE)
7142 error ("`#else' or `#elif' after `#else'");
7143 if_stack->type = kt->type;
7148 if_stack = if_stack->next;
7155 /* Don't let erroneous code go by. */
7156 if (kt->length < 0 && !lang_asm && pedantic)
7157 pedwarn ("invalid preprocessor directive name");
7162 /* after this returns, rescan will exit because ip->bufp
7163 now points to the end of the buffer.
7164 rescan is responsible for the error message also. */
7167 if (output_conditionals && op != 0) {
7168 char *ptr = "#endfailed\n";
7169 int len = strlen (ptr);
7171 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7176 check_expand (op, beg_of_line - beg_of_group);
7177 bcopy ((char *) beg_of_group, (char *) op->bufp,
7178 beg_of_line - beg_of_group);
7179 op->bufp += beg_of_line - beg_of_group;
7180 op->lineno += ip->lineno - beg_lineno;
7181 check_expand (op, len);
7182 bcopy (ptr, (char *) op->bufp, len);
7189 * handle a #else directive. Do this by just continuing processing
7190 * without changing if_stack ; this is so that the error message
7191 * for missing #endif's etc. will point to the original #if. It
7192 * is possible that something different would be better.
7196 do_else (buf, limit, op, keyword)
7197 U_CHAR *buf, *limit;
7199 struct directive *keyword;
7201 FILE_BUF *ip = &instack[indepth];
7204 SKIP_WHITE_SPACE (buf);
7206 pedwarn ("text following `#else' violates ANSI standard");
7209 if (if_stack == instack[indepth].if_stack) {
7210 error ("`#else' not within a conditional");
7213 /* #ifndef can't have its special treatment for containing the whole file
7214 if it has a #else clause. */
7215 if_stack->control_macro = 0;
7217 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7218 error ("`#else' after `#else'");
7219 fprintf (stderr, " (matches line %d", if_stack->lineno);
7220 if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
7221 fprintf (stderr, ", file %s", if_stack->fname);
7222 fprintf (stderr, ")\n");
7224 if_stack->type = T_ELSE;
7227 if (if_stack->if_succeeded)
7228 skip_if_group (ip, 0, op);
7230 ++if_stack->if_succeeded; /* continue processing input */
7231 output_line_command (ip, op, 1, same_file);
7237 * unstack after #endif command
7241 do_endif (buf, limit, op, keyword)
7242 U_CHAR *buf, *limit;
7244 struct directive *keyword;
7247 SKIP_WHITE_SPACE (buf);
7249 pedwarn ("text following `#endif' violates ANSI standard");
7252 if (if_stack == instack[indepth].if_stack)
7253 error ("unbalanced `#endif'");
7255 IF_STACK_FRAME *temp = if_stack;
7256 if_stack = if_stack->next;
7257 if (temp->control_macro != 0) {
7258 /* This #endif matched a #ifndef at the start of the file.
7259 See if it is at the end of the file. */
7260 FILE_BUF *ip = &instack[indepth];
7261 U_CHAR *p = ip->bufp;
7262 U_CHAR *ep = ip->buf + ip->length;
7272 if (p != ep && *p == '*') {
7273 /* Skip this comment. */
7275 U_CHAR *save_bufp = ip->bufp;
7277 p = skip_to_end_of_comment (ip, &junk, 1);
7278 ip->bufp = save_bufp;
7285 /* If we get here, this #endif ends a #ifndef
7286 that contains all of the file (aside from whitespace).
7287 Arrange not to include the file again
7288 if the macro that was tested is defined.
7290 Do not do this for the top-level file in a -include or any
7291 file in a -imacros. */
7293 && ! (indepth == 1 && no_record_file)
7294 && ! (no_record_file && no_output))
7295 record_control_macro (ip->fname, temp->control_macro);
7299 output_line_command (&instack[indepth], op, 1, same_file);
7304 /* When an #else or #endif is found while skipping failed conditional,
7305 if -pedantic was specified, this is called to warn about text after
7306 the command name. P points to the first char after the command name. */
7312 /* Advance P over whitespace and comments. */
7314 if (*p == '\\' && p[1] == '\n')
7316 if (is_hor_space[*p])
7318 else if (*p == '/') {
7319 if (p[1] == '\\' && p[2] == '\n')
7320 newline_fix (p + 1);
7323 /* Don't bother warning about unterminated comments
7324 since that will happen later. Just be sure to exit. */
7326 if (p[1] == '\\' && p[2] == '\n')
7327 newline_fix (p + 1);
7328 if (*p == '*' && p[1] == '/') {
7335 else if (cplusplus_comments && p[1] == '/') {
7337 while (*p && (*p != '\n' || p[-1] == '\\'))
7342 if (*p && *p != '\n')
7343 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
7346 /* Skip a comment, assuming the input ptr immediately follows the
7347 initial slash-star. Bump *LINE_COUNTER for each newline.
7348 (The canonical line counter is &ip->lineno.)
7349 Don't use this routine (or the next one) if bumping the line
7350 counter is not sufficient to deal with newlines in the string.
7352 If NOWARN is nonzero, don't warn about slash-star inside a comment.
7353 This feature is useful when processing a comment that is going to be
7354 processed or was processed at another point in the preprocessor,
7355 to avoid a duplicate warning. Likewise for unterminated comment errors. */
7358 skip_to_end_of_comment (ip, line_counter, nowarn)
7359 register FILE_BUF *ip;
7360 int *line_counter; /* place to remember newlines, or NULL */
7363 register U_CHAR *limit = ip->buf + ip->length;
7364 register U_CHAR *bp = ip->bufp;
7365 FILE_BUF *op = &outbuf; /* JF */
7366 int output = put_out_comments && !line_counter;
7367 int start_line = line_counter ? *line_counter : 0;
7369 /* JF this line_counter stuff is a crock to make sure the
7370 comment is only put out once, no matter how many times
7371 the comment is skipped. It almost works */
7376 if (cplusplus_comments && bp[-1] == '/') {
7378 while (bp < limit) {
7380 if (*bp == '\n' && bp[-1] != '\\')
7392 while (bp < limit) {
7393 if (bp[-1] != '\\' && *bp == '\n') {
7396 if (*bp == '\n' && line_counter)
7405 while (bp < limit) {
7410 if (warn_comments && !nowarn && bp < limit && *bp == '*')
7411 warning ("`/*' within comment");
7414 /* If this is the end of the file, we have an unterminated comment.
7415 Don't swallow the newline. We are guaranteed that there will be a
7416 trailing newline and various pieces assume it's there. */
7423 if (line_counter != NULL)
7429 if (*bp == '\\' && bp[1] == '\n')
7442 error_with_line (line_for_error (start_line), "unterminated comment");
7448 * Skip over a quoted string. BP points to the opening quote.
7449 * Returns a pointer after the closing quote. Don't go past LIMIT.
7450 * START_LINE is the line number of the starting point (but it need
7451 * not be valid if the starting point is inside a macro expansion).
7453 * The input stack state is not changed.
7455 * If COUNT_NEWLINES is nonzero, it points to an int to increment
7456 * for each newline passed.
7458 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
7459 * if we pass a backslash-newline.
7461 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
7464 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
7465 register U_CHAR *bp;
7466 register U_CHAR *limit;
7468 int *count_newlines;
7469 int *backslash_newlines_p;
7472 register U_CHAR c, match;
7477 error_with_line (line_for_error (start_line),
7478 "unterminated string or character constant");
7479 error_with_line (multiline_string_line,
7480 "possible real start of unterminated constant");
7481 multiline_string_line = 0;
7488 while (*bp == '\\' && bp[1] == '\n') {
7489 if (backslash_newlines_p)
7490 *backslash_newlines_p = 1;
7495 if (*bp == '\n' && count_newlines) {
7496 if (backslash_newlines_p)
7497 *backslash_newlines_p = 1;
7501 } else if (c == '\n') {
7503 /* Unterminated strings and character constants are 'legal'. */
7504 bp--; /* Don't consume the newline. */
7509 if (pedantic || match == '\'') {
7510 error_with_line (line_for_error (start_line),
7511 "unterminated string or character constant");
7517 /* If not traditional, then allow newlines inside strings. */
7520 if (multiline_string_line == 0)
7521 multiline_string_line = start_line;
7522 } else if (c == match)
7528 /* Place into DST a quoted string representing the string SRC.
7529 Return the address of DST's terminating null. */
7531 quote_string (dst, src)
7538 switch ((c = *src++))
7545 sprintf (dst, "\\%03o", c);
7563 /* Skip across a group of balanced parens, starting from IP->bufp.
7564 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
7566 This does not handle newlines, because it's used for the arg of #if,
7567 where there aren't any newlines. Also, backslash-newline can't appear. */
7570 skip_paren_group (ip)
7571 register FILE_BUF *ip;
7573 U_CHAR *limit = ip->buf + ip->length;
7574 U_CHAR *p = ip->bufp;
7576 int lines_dummy = 0;
7578 while (p != limit) {
7588 return ip->bufp = p;
7594 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
7602 p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
7604 return ip->bufp = p;
7615 * write out a #line command, for instance, after an #include file.
7616 * If CONDITIONAL is nonzero, we can omit the #line if it would
7617 * appear to be a no-op, and we can output a few newlines instead
7618 * if we want to increase the line number by a small amount.
7619 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
7623 output_line_command (ip, op, conditional, file_change)
7626 enum file_change_code file_change;
7629 char *line_cmd_buf, *line_end;
7631 if (no_line_commands
7632 || ip->fname == NULL
7634 op->lineno = ip->lineno;
7639 if (ip->lineno == op->lineno)
7642 /* If the inherited line number is a little too small,
7643 output some newlines instead of a #line command. */
7644 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
7645 check_expand (op, 10);
7646 while (ip->lineno > op->lineno) {
7654 /* Don't output a line number of 0 if we can help it. */
7655 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
7656 && *ip->bufp == '\n') {
7661 line_cmd_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
7662 #ifdef OUTPUT_LINE_COMMANDS
7663 sprintf (line_cmd_buf, "#line %d ", ip->lineno);
7665 sprintf (line_cmd_buf, "# %d ", ip->lineno);
7667 line_end = quote_string (line_cmd_buf + strlen (line_cmd_buf),
7669 if (file_change != same_file) {
7671 *line_end++ = file_change == enter_file ? '1' : '2';
7673 /* Tell cc1 if following text comes from a system header file. */
7674 if (ip->system_header_p) {
7678 #ifndef NO_IMPLICIT_EXTERN_C
7679 /* Tell cc1plus if following text should be treated as C. */
7680 if (ip->system_header_p == 2 && cplusplus) {
7686 len = line_end - line_cmd_buf;
7687 check_expand (op, len + 1);
7688 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7690 bcopy ((char *) line_cmd_buf, (char *) op->bufp, len);
7692 op->lineno = ip->lineno;
7695 /* This structure represents one parsed argument in a macro call.
7696 `raw' points to the argument text as written (`raw_length' is its length).
7697 `expanded' points to the argument's macro-expansion
7698 (its length is `expand_length').
7699 `stringified_length' is the length the argument would have
7701 `use_count' is the number of times this macro arg is substituted
7702 into the macro. If the actual use count exceeds 10,
7703 the value stored is 10.
7704 `free1' and `free2', if nonzero, point to blocks to be freed
7705 when the macro argument data is no longer needed. */
7708 U_CHAR *raw, *expanded;
7709 int raw_length, expand_length;
7710 int stringified_length;
7711 U_CHAR *free1, *free2;
7717 /* Expand a macro call.
7718 HP points to the symbol that is the macro being called.
7719 Put the result of expansion onto the input stack
7720 so that subsequent input by our caller will use it.
7722 If macro wants arguments, caller has already verified that
7723 an argument list follows; arguments come from the input stack. */
7726 macroexpand (hp, op)
7731 DEFINITION *defn = hp->value.defn;
7732 register U_CHAR *xbuf;
7734 int start_line = instack[indepth].lineno;
7735 int rest_args, rest_zero;
7737 CHECK_DEPTH (return;);
7739 /* it might not actually be a macro. */
7740 if (hp->type != T_MACRO) {
7741 special_symbol (hp, op);
7745 /* This macro is being used inside a #if, which means it must be */
7746 /* recorded as a precondition. */
7747 if (pcp_inside_if && pcp_outfile && defn->predefined)
7748 dump_single_macro (hp, pcp_outfile);
7750 nargs = defn->nargs;
7754 struct argdata *args;
7755 char *parse_error = 0;
7757 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
7759 for (i = 0; i < nargs; i++) {
7760 args[i].raw = (U_CHAR *) "";
7761 args[i].expanded = 0;
7762 args[i].raw_length = args[i].expand_length
7763 = args[i].stringified_length = 0;
7764 args[i].free1 = args[i].free2 = 0;
7765 args[i].use_count = 0;
7768 /* Parse all the macro args that are supplied. I counts them.
7769 The first NARGS args are stored in ARGS.
7770 The rest are discarded.
7771 If rest_args is set then we assume macarg absorbed the rest of the args.
7776 /* Discard the open-parenthesis or comma before the next arg. */
7777 ++instack[indepth].bufp;
7780 if (i < nargs || (nargs == 0 && i == 0)) {
7781 /* if we are working on last arg which absorbs rest of args... */
7782 if (i == nargs - 1 && defn->rest_args)
7784 parse_error = macarg (&args[i], rest_args);
7787 parse_error = macarg (NULL_PTR, 0);
7789 error_with_line (line_for_error (start_line), parse_error);
7793 } while (*instack[indepth].bufp != ')');
7795 /* If we got one arg but it was just whitespace, call that 0 args. */
7797 register U_CHAR *bp = args[0].raw;
7798 register U_CHAR *lim = bp + args[0].raw_length;
7799 /* cpp.texi says for foo ( ) we provide one argument.
7800 However, if foo wants just 0 arguments, treat this as 0. */
7802 while (bp != lim && is_space[*bp]) bp++;
7807 /* Don't output an error message if we have already output one for
7808 a parse error above. */
7810 if (nargs == 0 && i > 0) {
7812 error ("arguments given to macro `%s'", hp->name);
7813 } else if (i < nargs) {
7814 /* traditional C allows foo() if foo wants one argument. */
7815 if (nargs == 1 && i == 0 && traditional)
7817 /* the rest args token is allowed to absorb 0 tokens */
7818 else if (i == nargs - 1 && defn->rest_args)
7820 else if (parse_error)
7823 error ("macro `%s' used without args", hp->name);
7825 error ("macro `%s' used with just one arg", hp->name);
7827 error ("macro `%s' used with only %d args", hp->name, i);
7828 } else if (i > nargs) {
7830 error ("macro `%s' used with too many (%d) args", hp->name, i);
7833 /* Swallow the closeparen. */
7834 ++instack[indepth].bufp;
7836 /* If macro wants zero args, we parsed the arglist for checking only.
7837 Read directly from the macro definition. */
7839 xbuf = defn->expansion;
7840 xbuf_len = defn->length;
7842 register U_CHAR *exp = defn->expansion;
7843 register int offset; /* offset in expansion,
7844 copied a piece at a time */
7845 register int totlen; /* total amount of exp buffer filled so far */
7847 register struct reflist *ap, *last_ap;
7849 /* Macro really takes args. Compute the expansion of this call. */
7851 /* Compute length in characters of the macro's expansion.
7852 Also count number of times each arg is used. */
7853 xbuf_len = defn->length;
7854 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
7856 xbuf_len += args[ap->argno].stringified_length;
7857 else if (ap->raw_before || ap->raw_after || traditional)
7858 /* Add 4 for two newline-space markers to prevent
7859 token concatenation. */
7860 xbuf_len += args[ap->argno].raw_length + 4;
7862 /* We have an ordinary (expanded) occurrence of the arg.
7863 So compute its expansion, if we have not already. */
7864 if (args[ap->argno].expanded == 0) {
7866 obuf = expand_to_temp_buffer (args[ap->argno].raw,
7867 args[ap->argno].raw + args[ap->argno].raw_length,
7870 args[ap->argno].expanded = obuf.buf;
7871 args[ap->argno].expand_length = obuf.length;
7872 args[ap->argno].free2 = obuf.buf;
7875 /* Add 4 for two newline-space markers to prevent
7876 token concatenation. */
7877 xbuf_len += args[ap->argno].expand_length + 4;
7879 if (args[ap->argno].use_count < 10)
7880 args[ap->argno].use_count++;
7883 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
7885 /* Generate in XBUF the complete expansion
7886 with arguments substituted in.
7887 TOTLEN is the total size generated so far.
7888 OFFSET is the index in the definition
7889 of where we are copying from. */
7890 offset = totlen = 0;
7891 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
7892 last_ap = ap, ap = ap->next) {
7893 register struct argdata *arg = &args[ap->argno];
7894 int count_before = totlen;
7896 /* Add chars to XBUF. */
7897 for (i = 0; i < ap->nchars; i++, offset++)
7898 xbuf[totlen++] = exp[offset];
7900 /* If followed by an empty rest arg with concatenation,
7901 delete the last run of nonwhite chars. */
7902 if (rest_zero && totlen > count_before
7903 && ((ap->rest_args && ap->raw_before)
7904 || (last_ap != NULL && last_ap->rest_args
7905 && last_ap->raw_after))) {
7906 /* Delete final whitespace. */
7907 while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
7911 /* Delete the nonwhites before them. */
7912 while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
7917 if (ap->stringify != 0) {
7918 int arglen = arg->raw_length;
7924 && (c = arg->raw[i], is_space[c]))
7927 && (c = arg->raw[arglen - 1], is_space[c]))
7930 xbuf[totlen++] = '\"'; /* insert beginning quote */
7931 for (; i < arglen; i++) {
7934 /* Special markers Newline Space
7935 generate nothing for a stringified argument. */
7936 if (c == '\n' && arg->raw[i+1] != '\n') {
7941 /* Internal sequences of whitespace are replaced by one space
7942 except within an string or char token. */
7944 && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
7946 /* Note that Newline Space does occur within whitespace
7947 sequences; consider it part of the sequence. */
7948 if (c == '\n' && is_space[arg->raw[i+1]])
7950 else if (c != '\n' && is_space[c])
7967 } else if (c == '\"' || c == '\'')
7971 /* Escape these chars */
7972 if (c == '\"' || (in_string && c == '\\'))
7973 xbuf[totlen++] = '\\';
7977 sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
7982 xbuf[totlen++] = '\"'; /* insert ending quote */
7983 } else if (ap->raw_before || ap->raw_after || traditional) {
7984 U_CHAR *p1 = arg->raw;
7985 U_CHAR *l1 = p1 + arg->raw_length;
7986 if (ap->raw_before) {
7987 while (p1 != l1 && is_space[*p1]) p1++;
7988 while (p1 != l1 && is_idchar[*p1])
7989 xbuf[totlen++] = *p1++;
7990 /* Delete any no-reexpansion marker that follows
7991 an identifier at the beginning of the argument
7992 if the argument is concatenated with what precedes it. */
7993 if (p1[0] == '\n' && p1[1] == '-')
7995 } else if (!traditional) {
7996 /* Ordinary expanded use of the argument.
7997 Put in newline-space markers to prevent token pasting. */
7998 xbuf[totlen++] = '\n';
7999 xbuf[totlen++] = ' ';
8001 if (ap->raw_after) {
8002 /* Arg is concatenated after: delete trailing whitespace,
8003 whitespace markers, and no-reexpansion markers. */
8005 if (is_space[l1[-1]]) l1--;
8006 else if (l1[-1] == '-') {
8007 U_CHAR *p2 = l1 - 1;
8008 /* If a `-' is preceded by an odd number of newlines then it
8009 and the last newline are a no-reexpansion marker. */
8010 while (p2 != p1 && p2[-1] == '\n') p2--;
8011 if ((l1 - 1 - p2) & 1) {
8020 bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
8022 if (!traditional && !ap->raw_after) {
8023 /* Ordinary expanded use of the argument.
8024 Put in newline-space markers to prevent token pasting. */
8025 xbuf[totlen++] = '\n';
8026 xbuf[totlen++] = ' ';
8029 /* Ordinary expanded use of the argument.
8030 Put in newline-space markers to prevent token pasting. */
8032 xbuf[totlen++] = '\n';
8033 xbuf[totlen++] = ' ';
8035 bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
8036 arg->expand_length);
8037 totlen += arg->expand_length;
8039 xbuf[totlen++] = '\n';
8040 xbuf[totlen++] = ' ';
8042 /* If a macro argument with newlines is used multiple times,
8043 then only expand the newlines once. This avoids creating output
8044 lines which don't correspond to any input line, which confuses
8046 if (arg->use_count > 1 && arg->newlines > 0) {
8047 /* Don't bother doing change_newlines for subsequent
8051 = change_newlines (arg->expanded, arg->expand_length);
8055 if (totlen > xbuf_len)
8059 /* if there is anything left of the definition
8060 after handling the arg list, copy that in too. */
8062 for (i = offset; i < defn->length; i++) {
8063 /* if we've reached the end of the macro */
8066 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
8067 && last_ap->raw_after))
8068 xbuf[totlen++] = exp[i];
8074 for (i = 0; i < nargs; i++) {
8075 if (args[i].free1 != 0)
8076 free (args[i].free1);
8077 if (args[i].free2 != 0)
8078 free (args[i].free2);
8082 xbuf = defn->expansion;
8083 xbuf_len = defn->length;
8086 /* Now put the expansion on the input stack
8087 so our caller will commence reading from it. */
8089 register FILE_BUF *ip2;
8091 ip2 = &instack[++indepth];
8094 ip2->nominal_fname = 0;
8095 /* This may not be exactly correct, but will give much better error
8096 messages for nested macro calls than using a line number of zero. */
8097 ip2->lineno = start_line;
8099 ip2->length = xbuf_len;
8101 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
8103 ip2->if_stack = if_stack;
8104 ip2->system_header_p = 0;
8106 /* Recursive macro use sometimes works traditionally.
8107 #define foo(x,y) bar (x (y,0), y)
8111 hp->type = T_DISABLED;
8116 * Parse a macro argument and store the info on it into *ARGPTR.
8117 * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8118 * Return nonzero to indicate a syntax error.
8122 macarg (argptr, rest_args)
8123 register struct argdata *argptr;
8126 FILE_BUF *ip = &instack[indepth];
8131 /* Try to parse as much of the argument as exists at this
8132 input stack level. */
8133 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
8134 &paren, &newlines, &comments, rest_args);
8136 /* If we find the end of the argument at this level,
8137 set up *ARGPTR to point at it in the input stack. */
8138 if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
8139 && bp != ip->buf + ip->length) {
8141 argptr->raw = ip->bufp;
8142 argptr->raw_length = bp - ip->bufp;
8143 argptr->newlines = newlines;
8147 /* This input stack level ends before the macro argument does.
8148 We must pop levels and keep parsing.
8149 Therefore, we must allocate a temporary buffer and copy
8150 the macro argument into it. */
8151 int bufsize = bp - ip->bufp;
8152 int extra = newlines;
8153 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
8154 int final_start = 0;
8156 bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
8158 ip->lineno += newlines;
8160 while (bp == ip->buf + ip->length) {
8161 if (instack[indepth].macro == 0) {
8163 return "unterminated macro call";
8165 ip->macro->type = T_MACRO;
8167 free (ip->free_ptr);
8168 ip = &instack[--indepth];
8171 bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
8172 &newlines, &comments, rest_args);
8173 final_start = bufsize;
8174 bufsize += bp - ip->bufp;
8176 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
8177 bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
8180 ip->lineno += newlines;
8183 /* Now, if arg is actually wanted, record its raw form,
8184 discarding comments and duplicating newlines in whatever
8185 part of it did not come from a macro expansion.
8186 EXTRA space has been preallocated for duplicating the newlines.
8187 FINAL_START is the index of the start of that part. */
8189 argptr->raw = buffer;
8190 argptr->raw_length = bufsize;
8191 argptr->free1 = buffer;
8192 argptr->newlines = newlines;
8193 argptr->comments = comments;
8194 if ((newlines || comments) && ip->fname != 0)
8197 discard_comments (argptr->raw + final_start,
8198 argptr->raw_length - final_start,
8200 argptr->raw[argptr->raw_length] = 0;
8201 if (argptr->raw_length > bufsize + extra)
8206 /* If we are not discarding this argument,
8207 macroexpand it and compute its length as stringified.
8208 All this info goes into *ARGPTR. */
8211 register U_CHAR *buf, *lim;
8212 register int totlen;
8215 lim = buf + argptr->raw_length;
8217 while (buf != lim && is_space[*buf])
8219 while (buf != lim && is_space[lim[-1]])
8221 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
8222 while (buf != lim) {
8223 register U_CHAR c = *buf++;
8225 /* Internal sequences of whitespace are replaced by one space
8226 in most cases, but not always. So count all the whitespace
8227 in case we need to keep it all. */
8230 SKIP_ALL_WHITE_SPACE (buf);
8233 if (c == '\"' || c == '\\') /* escape these chars */
8235 else if (!isprint (c))
8238 argptr->stringified_length = totlen;
8243 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8244 counting parens in *DEPTHPTR,
8245 and return if reach LIMIT
8246 or before a `)' that would make *DEPTHPTR negative
8247 or before a comma when *DEPTHPTR is zero.
8248 Single and double quotes are matched and termination
8249 is inhibited within them. Comments also inhibit it.
8250 Value returned is pointer to stopping place.
8252 Increment *NEWLINES each time a newline is passed.
8253 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8254 Set *COMMENTS to 1 if a comment is seen. */
8257 macarg1 (start, limit, depthptr, newlines, comments, rest_args)
8259 register U_CHAR *limit;
8260 int *depthptr, *newlines, *comments;
8263 register U_CHAR *bp = start;
8265 while (bp < limit) {
8271 if (--(*depthptr) < 0)
8275 /* Traditionally, backslash makes following char not special. */
8276 if (bp + 1 < limit && traditional)
8279 /* But count source lines anyway. */
8288 if (bp[1] == '\\' && bp[2] == '\n')
8289 newline_fix (bp + 1);
8290 if (cplusplus_comments && bp[1] == '/') {
8293 while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
8294 if (*bp == '\n') ++*newlines;
8299 if (bp[1] != '*' || bp + 1 >= limit)
8303 while (bp + 1 < limit) {
8305 && bp[1] == '\\' && bp[2] == '\n')
8306 newline_fix (bp + 1);
8307 if (bp[0] == '*' && bp[1] == '/')
8309 if (*bp == '\n') ++*newlines;
8317 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
8322 while (*bp == '\\' && bp[1] == '\n') {
8325 } else if (*bp == '\n') {
8334 /* if we've returned to lowest level and we aren't absorbing all args */
8335 if ((*depthptr) == 0 && rest_args == 0)
8345 /* Discard comments and duplicate newlines
8346 in the string of length LENGTH at START,
8347 except inside of string constants.
8348 The string is copied into itself with its beginning staying fixed.
8350 NEWLINES is the number of newlines that must be duplicated.
8351 We assume that that much extra space is available past the end
8355 discard_comments (start, length, newlines)
8360 register U_CHAR *ibp;
8361 register U_CHAR *obp;
8362 register U_CHAR *limit;
8365 /* If we have newlines to duplicate, copy everything
8366 that many characters up. Then, in the second part,
8367 we will have room to insert the newlines
8369 NEWLINES may actually be too large, because it counts
8370 newlines in string constants, and we don't duplicate those.
8371 But that does no harm. */
8373 ibp = start + length;
8374 obp = ibp + newlines;
8376 while (limit != ibp)
8380 ibp = start + newlines;
8381 limit = start + length + newlines;
8384 while (ibp < limit) {
8385 *obp++ = c = *ibp++;
8388 /* Duplicate the newline. */
8400 if (*ibp == '\\' && ibp[1] == '\n')
8402 /* Delete any comment. */
8403 if (cplusplus_comments && ibp[0] == '/') {
8404 /* Comments are equivalent to spaces. */
8407 while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
8411 if (ibp[0] != '*' || ibp + 1 >= limit)
8413 /* Comments are equivalent to spaces. */
8416 while (ibp + 1 < limit) {
8418 && ibp[1] == '\\' && ibp[2] == '\n')
8419 newline_fix (ibp + 1);
8420 if (ibp[0] == '*' && ibp[1] == '/')
8429 /* Notice and skip strings, so that we don't
8430 think that comments start inside them,
8431 and so we don't duplicate newlines in them. */
8434 while (ibp < limit) {
8435 *obp++ = c = *ibp++;
8438 if (c == '\n' && quotec == '\'')
8440 if (c == '\\' && ibp < limit) {
8441 while (*ibp == '\\' && ibp[1] == '\n')
8454 /* Turn newlines to spaces in the string of length LENGTH at START,
8455 except inside of string constants.
8456 The string is copied into itself with its beginning staying fixed. */
8459 change_newlines (start, length)
8463 register U_CHAR *ibp;
8464 register U_CHAR *obp;
8465 register U_CHAR *limit;
8469 limit = start + length;
8472 while (ibp < limit) {
8473 *obp++ = c = *ibp++;
8476 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
8477 string. Skip past the newline and its duplicate.
8478 Put a space in the output. */
8489 /* Notice and skip strings, so that we don't delete newlines in them. */
8492 while (ibp < limit) {
8493 *obp++ = c = *ibp++;
8496 if (c == '\n' && quotec == '\'')
8508 * my_strerror - return the descriptive text associated with an `errno' code.
8512 my_strerror (errnum)
8518 #ifndef HAVE_STRERROR
8519 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
8521 result = strerror (errnum);
8524 /* VAXCRTL's strerror() takes an optional second argument, which only
8525 matters when the first argument is EVMSERR. However, it's simplest
8526 just to pass it unconditionally. `vaxc$errno' is declared in
8527 <errno.h>, and maintained by the library in parallel with `errno'.
8528 We assume that caller's `errnum' either matches the last setting of
8529 `errno' by the library or else does not have the value `EVMSERR'. */
8531 result = strerror (errnum, vaxc$errno);
8535 result = "undocumented I/O error";
8541 * error - print error message and increment count of errors.
8545 error (msg, arg1, arg2, arg3)
8547 char *arg1, *arg2, *arg3;
8550 FILE_BUF *ip = NULL;
8552 print_containing_files ();
8554 for (i = indepth; i >= 0; i--)
8555 if (instack[i].fname != NULL) {
8561 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
8562 fprintf (stderr, msg, arg1, arg2, arg3);
8563 fprintf (stderr, "\n");
8567 /* Error including a message from `errno'. */
8570 error_from_errno (name)
8574 FILE_BUF *ip = NULL;
8576 print_containing_files ();
8578 for (i = indepth; i >= 0; i--)
8579 if (instack[i].fname != NULL) {
8585 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
8587 fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
8592 /* Print error message but don't count it. */
8595 warning (msg, arg1, arg2, arg3)
8597 char *arg1, *arg2, *arg3;
8600 FILE_BUF *ip = NULL;
8602 if (inhibit_warnings)
8605 if (warnings_are_errors)
8608 print_containing_files ();
8610 for (i = indepth; i >= 0; i--)
8611 if (instack[i].fname != NULL) {
8617 fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
8618 fprintf (stderr, "warning: ");
8619 fprintf (stderr, msg, arg1, arg2, arg3);
8620 fprintf (stderr, "\n");
8624 error_with_line (line, msg, arg1, arg2, arg3)
8627 char *arg1, *arg2, *arg3;
8630 FILE_BUF *ip = NULL;
8632 print_containing_files ();
8634 for (i = indepth; i >= 0; i--)
8635 if (instack[i].fname != NULL) {
8641 fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
8642 fprintf (stderr, msg, arg1, arg2, arg3);
8643 fprintf (stderr, "\n");
8648 warning_with_line (line, msg, arg1, arg2, arg3)
8651 char *arg1, *arg2, *arg3;
8654 FILE_BUF *ip = NULL;
8656 if (inhibit_warnings)
8659 if (warnings_are_errors)
8662 print_containing_files ();
8664 for (i = indepth; i >= 0; i--)
8665 if (instack[i].fname != NULL) {
8671 fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
8672 fprintf (stderr, "warning: ");
8673 fprintf (stderr, msg, arg1, arg2, arg3);
8674 fprintf (stderr, "\n");
8677 /* print an error message and maybe count it. */
8680 pedwarn (msg, arg1, arg2, arg3)
8682 char *arg1, *arg2, *arg3;
8684 if (pedantic_errors)
8685 error (msg, arg1, arg2, arg3);
8687 warning (msg, arg1, arg2, arg3);
8691 pedwarn_with_line (line, msg, arg1, arg2, arg3)
8694 char *arg1, *arg2, *arg3;
8696 if (pedantic_errors)
8697 error_with_line (line, msg, arg1, arg2, arg3);
8699 warning_with_line (line, msg, arg1, arg2, arg3);
8702 /* Report a warning (or an error if pedantic_errors)
8703 giving specified file name and line number, not current. */
8706 pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
8710 char *arg1, *arg2, *arg3;
8712 if (!pedantic_errors && inhibit_warnings)
8715 fprintf (stderr, "%s:%d: ", file, line);
8716 if (pedantic_errors)
8718 if (!pedantic_errors)
8719 fprintf (stderr, "warning: ");
8720 fprintf (stderr, msg, arg1, arg2, arg3);
8721 fprintf (stderr, "\n");
8724 /* Print the file names and line numbers of the #include
8725 commands which led to the current file. */
8728 print_containing_files ()
8730 FILE_BUF *ip = NULL;
8734 /* If stack of files hasn't changed since we last printed
8735 this info, don't repeat it. */
8736 if (last_error_tick == input_file_stack_tick)
8739 for (i = indepth; i >= 0; i--)
8740 if (instack[i].fname != NULL) {
8745 /* Give up if we don't find a source file. */
8749 /* Find the other, outer source files. */
8750 for (i--; i >= 0; i--)
8751 if (instack[i].fname != NULL) {
8755 fprintf (stderr, "In file included");
8757 fprintf (stderr, ",\n ");
8760 fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
8763 fprintf (stderr, ":\n");
8765 /* Record we have printed the status as of this time. */
8766 last_error_tick = input_file_stack_tick;
8769 /* Return the line at which an error occurred.
8770 The error is not necessarily associated with the current spot
8771 in the input stack, so LINE says where. LINE will have been
8772 copied from ip->lineno for the current input level.
8773 If the current level is for a file, we return LINE.
8774 But if the current level is not for a file, LINE is meaningless.
8775 In that case, we return the lineno of the innermost file. */
8778 line_for_error (line)
8784 for (i = indepth; i >= 0; ) {
8785 if (instack[i].fname != 0)
8790 line1 = instack[i].lineno;
8798 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
8800 * As things stand, nothing is ever placed in the output buffer to be
8801 * removed again except when it's KNOWN to be part of an identifier,
8802 * so flushing and moving down everything left, instead of expanding,
8806 /* You might think void was cleaner for the return type,
8807 but that would get type mismatch in check_expand in strict ANSI. */
8809 grow_outbuf (obuf, needed)
8810 register FILE_BUF *obuf;
8811 register int needed;
8816 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
8819 /* Make it at least twice as big as it is now. */
8821 /* Make it have at least 150% of the free space we will need. */
8822 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
8823 if (minsize > obuf->length)
8824 obuf->length = minsize;
8826 if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
8829 obuf->bufp = p + (obuf->bufp - obuf->buf);
8835 /* Symbol table for macro names and special symbols */
8838 * install a name in the main hash table, even if it is already there.
8839 * name stops with first non alphanumeric, except leading '#'.
8840 * caller must check against redefinition if that is desired.
8841 * delete_macro () removes things installed by install () in fifo order.
8842 * this is important because of the `defined' special symbol used
8843 * in #if, and also if pushdef/popdef directives are ever implemented.
8845 * If LEN is >= 0, it is the length of the name.
8846 * Otherwise, compute the length by scanning the entire name.
8848 * If HASH is >= 0, it is the precomputed hash code.
8849 * Otherwise, compute the hash code.
8852 install (name, len, type, ivalue, value, hash)
8855 enum node_type type;
8860 register HASHNODE *hp;
8861 register int i, bucket;
8862 register U_CHAR *p, *q;
8866 while (is_idchar[*p])
8872 hash = hashf (name, len, HASHSIZE);
8874 i = sizeof (HASHNODE) + len + 1;
8875 hp = (HASHNODE *) xmalloc (i);
8877 hp->bucket_hdr = &hashtab[bucket];
8878 hp->next = hashtab[bucket];
8879 hashtab[bucket] = hp;
8881 if (hp->next != NULL)
8882 hp->next->prev = hp;
8885 if (hp->type == T_CONST)
8886 hp->value.ival = ivalue;
8888 hp->value.cpval = value;
8889 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
8892 for (i = 0; i < len; i++)
8899 * find the most recent hash node for name name (ending with first
8900 * non-identifier char) installed by install
8902 * If LEN is >= 0, it is the length of the name.
8903 * Otherwise, compute the length by scanning the entire name.
8905 * If HASH is >= 0, it is the precomputed hash code.
8906 * Otherwise, compute the hash code.
8909 lookup (name, len, hash)
8914 register U_CHAR *bp;
8915 register HASHNODE *bucket;
8918 for (bp = name; is_idchar[*bp]; bp++) ;
8923 hash = hashf (name, len, HASHSIZE);
8925 bucket = hashtab[hash];
8927 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
8929 bucket = bucket->next;
8935 * Delete a hash node. Some weirdness to free junk from macros.
8936 * More such weirdness will have to be added if you define more hash
8937 * types that need it.
8940 /* Note that the DEFINITION of a macro is removed from the hash table
8941 but its storage is not freed. This would be a storage leak
8942 except that it is not reasonable to keep undefining and redefining
8943 large numbers of macros many times.
8944 In any case, this is necessary, because a macro can be #undef'd
8945 in the middle of reading the arguments to a call to it.
8946 If #undef freed the DEFINITION, that would crash. */
8953 if (hp->prev != NULL)
8954 hp->prev->next = hp->next;
8955 if (hp->next != NULL)
8956 hp->next->prev = hp->prev;
8958 /* make sure that the bucket chain header that
8959 the deleted guy was on points to the right thing afterwards. */
8960 if (hp == *hp->bucket_hdr)
8961 *hp->bucket_hdr = hp->next;
8964 if (hp->type == T_MACRO) {
8965 DEFINITION *d = hp->value.defn;
8966 struct reflist *ap, *nextap;
8968 for (ap = d->pattern; ap != NULL; ap = nextap) {
8979 * return hash function on name. must be compatible with the one
8980 * computed a step at a time, elsewhere
8983 hashf (name, len, hashsize)
8984 register U_CHAR *name;
8991 r = HASHSTEP (r, *name++);
8993 return MAKE_POS (r) % hashsize;
8997 /* Dump the definition of a single macro HP to OF. */
8999 dump_single_macro (hp, of)
9000 register HASHNODE *hp;
9003 register DEFINITION *defn = hp->value.defn;
9009 /* Print the definition of the macro HP. */
9011 fprintf (of, "#define %s", hp->name);
9013 if (defn->nargs >= 0) {
9017 for (i = 0; i < defn->nargs; i++) {
9018 dump_arg_n (defn, i, of);
9019 if (i + 1 < defn->nargs)
9029 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
9030 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
9031 if (ap->nchars != 0)
9033 offset += ap->nchars;
9036 if (ap->raw_before && !concat)
9037 fprintf (of, " ## ");
9039 dump_arg_n (defn, ap->argno, of);
9040 if (ap->raw_after) {
9041 fprintf (of, " ## ");
9045 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
9049 /* Dump all macro definitions as #defines to stdout. */
9056 for (bucket = 0; bucket < HASHSIZE; bucket++) {
9057 register HASHNODE *hp;
9059 for (hp = hashtab[bucket]; hp; hp= hp->next) {
9060 if (hp->type == T_MACRO)
9061 dump_single_macro (hp, stdout);
9066 /* Output to OF a substring of a macro definition.
9067 BASE is the beginning of the definition.
9068 Output characters START thru LENGTH.
9069 Discard newlines outside of strings, thus
9070 converting funny-space markers to ordinary spaces. */
9073 dump_defn_1 (base, start, length, of)
9079 U_CHAR *p = base + start;
9080 U_CHAR *limit = base + start + length;
9083 if (*p == '\"' || *p =='\'') {
9084 U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
9085 NULL_PTR, NULL_PTR);
9086 fwrite (p, p1 - p, 1, of);
9096 /* Print the name of argument number ARGNUM of macro definition DEFN
9098 Recall that DEFN->args.argnames contains all the arg names
9099 concatenated in reverse order with comma-space in between. */
9102 dump_arg_n (defn, argnum, of)
9107 register U_CHAR *p = defn->args.argnames;
9108 while (argnum + 1 < defn->nargs) {
9109 p = (U_CHAR *) index (p, ' ') + 1;
9113 while (*p && *p != ',') {
9119 /* Initialize syntactic classifications of characters. */
9122 initialize_char_syntax ()
9127 * Set up is_idchar and is_idstart tables. These should be
9128 * faster than saying (is_alpha (c) || c == '_'), etc.
9129 * Set up these things before calling any routines tthat
9132 for (i = 'a'; i <= 'z'; i++) {
9133 is_idchar[i - 'a' + 'A'] = 1;
9135 is_idstart[i - 'a' + 'A'] = 1;
9138 for (i = '0'; i <= '9'; i++)
9141 is_idstart['_'] = 1;
9142 is_idchar['$'] = dollars_in_ident;
9143 is_idstart['$'] = dollars_in_ident;
9145 /* horizontal space table */
9146 is_hor_space[' '] = 1;
9147 is_hor_space['\t'] = 1;
9148 is_hor_space['\v'] = 1;
9149 is_hor_space['\f'] = 1;
9150 is_hor_space['\r'] = 1;
9160 /* Initialize the built-in macros. */
9163 initialize_builtins (inp, outp)
9167 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
9168 install ("__DATE__", -1, T_DATE, 0, 0, -1);
9169 install ("__FILE__", -1, T_FILE, 0, 0, -1);
9170 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
9171 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
9172 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
9173 #ifndef NO_BUILTIN_SIZE_TYPE
9174 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
9176 #ifndef NO_BUILTIN_PTRDIFF_TYPE
9177 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
9179 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
9180 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
9181 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
9182 install ("__TIME__", -1, T_TIME, 0, 0, -1);
9184 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
9186 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
9187 /* This is supplied using a -D by the compiler driver
9188 so that it is present only when truly compiling with GNU C. */
9189 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
9193 char directive[2048];
9194 register struct directive *dp = &directive_table[0];
9195 struct tm *timebuf = timestamp ();
9197 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
9198 instack[0].nominal_fname);
9199 output_line_command (inp, outp, 0, same_file);
9200 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
9202 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
9203 output_line_command (inp, outp, 0, same_file);
9204 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
9206 #ifndef NO_BUILTIN_SIZE_TYPE
9207 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
9208 output_line_command (inp, outp, 0, same_file);
9209 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
9212 #ifndef NO_BUILTIN_PTRDIFF_TYPE
9213 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
9214 output_line_command (inp, outp, 0, same_file);
9215 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
9218 sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
9219 output_line_command (inp, outp, 0, same_file);
9220 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
9222 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
9223 monthnames[timebuf->tm_mon],
9224 timebuf->tm_mday, timebuf->tm_year + 1900);
9225 output_line_command (inp, outp, 0, same_file);
9226 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
9228 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
9229 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
9230 output_line_command (inp, outp, 0, same_file);
9231 pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
9235 sprintf (directive, " __STDC__ 1");
9236 output_line_command (inp, outp, 0, same_file);
9237 pass_thru_directive (directive, &directive[strlen (directive)],
9242 sprintf (directive, " __OBJC__ 1");
9243 output_line_command (inp, outp, 0, same_file);
9244 pass_thru_directive (directive, &directive[strlen (directive)],
9251 * process a given definition string, for initialization
9252 * If STR is just an identifier, define it with value 1.
9253 * If STR has anything after the identifier, then it should
9254 * be identifier=definition.
9258 make_definition (str, op)
9263 struct directive *kt;
9268 if (!is_idstart[*p]) {
9269 error ("malformed option `-D %s'", str);
9272 while (is_idchar[*++p])
9275 buf = (U_CHAR *) alloca (p - buf + 4);
9276 strcpy ((char *)buf, str);
9277 strcat ((char *)buf, " 1");
9278 } else if (*p != '=') {
9279 error ("malformed option `-D %s'", str);
9283 /* Copy the entire option so we can modify it. */
9284 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
9285 strncpy (buf, str, p - str);
9286 /* Change the = to a space. */
9288 /* Scan for any backslash-newline and remove it. */
9292 if (*p == '\\' && p[1] == '\n')
9294 /* Change newline chars into newline-markers. */
9295 else if (*p == '\n')
9307 ip = &instack[++indepth];
9308 ip->nominal_fname = ip->fname = "*Initialization*";
9310 ip->buf = ip->bufp = buf;
9311 ip->length = strlen (buf);
9315 ip->if_stack = if_stack;
9316 ip->system_header_p = 0;
9318 for (kt = directive_table; kt->type != T_DEFINE; kt++)
9321 /* Pass NULL instead of OP, since this is a "predefined" macro. */
9322 do_define (buf, buf + strlen (buf), NULL, kt);
9326 /* JF, this does the work for the -U option */
9329 make_undef (str, op)
9334 struct directive *kt;
9336 ip = &instack[++indepth];
9337 ip->nominal_fname = ip->fname = "*undef*";
9339 ip->buf = ip->bufp = str;
9340 ip->length = strlen (str);
9344 ip->if_stack = if_stack;
9345 ip->system_header_p = 0;
9347 for (kt = directive_table; kt->type != T_UNDEF; kt++)
9350 do_undef (str, str + strlen (str), op, kt);
9354 /* Process the string STR as if it appeared as the body of a #assert.
9355 OPTION is the option name for which STR was the argument. */
9358 make_assertion (option, str)
9363 struct directive *kt;
9364 U_CHAR *buf, *p, *q;
9366 /* Copy the entire option so we can modify it. */
9367 buf = (U_CHAR *) alloca (strlen (str) + 1);
9368 strcpy ((char *) buf, str);
9369 /* Scan for any backslash-newline and remove it. */
9372 if (*p == '\\' && p[1] == '\n')
9380 if (!is_idstart[*p]) {
9381 error ("malformed option `%s %s'", option, str);
9384 while (is_idchar[*++p])
9386 while (*p == ' ' || *p == '\t') p++;
9387 if (! (*p == 0 || *p == '(')) {
9388 error ("malformed option `%s %s'", option, str);
9392 ip = &instack[++indepth];
9393 ip->nominal_fname = ip->fname = "*Initialization*";
9395 ip->buf = ip->bufp = buf;
9396 ip->length = strlen (buf);
9400 ip->if_stack = if_stack;
9401 ip->system_header_p = 0;
9403 for (kt = directive_table; kt->type != T_ASSERT; kt++)
9406 /* pass NULL as output ptr to do_define since we KNOW it never
9407 does any output.... */
9408 do_assert (buf, buf + strlen (buf) , NULL_PTR, kt);
9412 /* Append a chain of `struct file_name_list's
9413 to the end of the main include chain.
9414 FIRST is the beginning of the chain to append, and LAST is the end. */
9417 append_include_chain (first, last)
9418 struct file_name_list *first, *last;
9420 struct file_name_list *dir;
9422 if (!first || !last)
9428 last_include->next = first;
9430 if (first_bracket_include == 0)
9431 first_bracket_include = first;
9433 for (dir = first; ; dir = dir->next) {
9434 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
9435 if (len > max_include_len)
9436 max_include_len = len;
9442 last_include = last;
9445 /* Add output to `deps_buffer' for the -M switch.
9446 STRING points to the text to be output.
9447 SPACER is ':' for targets, ' ' for dependencies, zero for text
9448 to be inserted literally. */
9451 deps_output (string, spacer)
9455 int size = strlen (string);
9460 #ifndef MAX_OUTPUT_COLUMNS
9461 #define MAX_OUTPUT_COLUMNS 72
9465 && (deps_column + size) > MAX_OUTPUT_COLUMNS)
9467 deps_output (" \\\n ", 0);
9471 if (deps_size + size + 8 > deps_allocated_size) {
9472 deps_allocated_size = (deps_size + size + 50) * 2;
9473 deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
9475 if (spacer == ' ' && deps_column > 0)
9476 deps_buffer[deps_size++] = ' ';
9477 bcopy (string, &deps_buffer[deps_size], size);
9479 deps_column += size;
9481 deps_buffer[deps_size++] = ':';
9482 deps_buffer[deps_size] = 0;
9485 #if defined(USG) || defined(VMS)
9491 register unsigned length;
9493 while (length-- > 0)
9498 bcopy (b1, b2, length)
9501 register unsigned length;
9503 while (length-- > 0)
9508 bcmp (b1, b2, length) /* This could be a macro! */
9511 register unsigned length;
9513 while (length-- > 0)
9519 #endif /* not BSTRING */
9520 #endif /* USG or VMS */
9527 fprintf (stderr, "%s: ", progname);
9528 fprintf (stderr, str, arg);
9529 fprintf (stderr, "\n");
9530 exit (FAILURE_EXIT_CODE);
9533 /* More 'friendly' abort that prints the line and file.
9534 config.h can #define abort fancy_abort if you like that sort of thing. */
9539 fatal ("Internal gcc abort.");
9543 perror_with_name (name)
9546 fprintf (stderr, "%s: ", progname);
9547 fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
9552 pfatal_with_name (name)
9555 perror_with_name (name);
9559 exit (FAILURE_EXIT_CODE);
9563 /* Handler for SIGPIPE. */
9567 /* If this is missing, some compilers complain. */
9570 fatal ("output pipe has been closed");
9576 fatal ("Memory exhausted.");
9584 register char *ptr = (char *) malloc (size);
9585 if (ptr != 0) return (ptr);
9592 xrealloc (old, size)
9596 register char *ptr = (char *) realloc (old, size);
9597 if (ptr != 0) return (ptr);
9604 xcalloc (number, size)
9605 unsigned number, size;
9607 register unsigned total = number * size;
9608 register char *ptr = (char *) malloc (total);
9613 /* It's not too long, so loop, zeroing by longs.
9614 It must be safe because malloc values are always well aligned. */
9615 register long *zp = (long *) ptr;
9616 register long *zl = (long *) (ptr + total - 4);
9617 register int i = total - 4;
9636 unsigned size = strlen (input);
9637 char *output = xmalloc (size + 1);
9638 strcpy (output, input);
9642 /* Get the file-mode and data size of the file open on FD
9643 and store them in *MODE_POINTER and *SIZE_POINTER. */
9646 file_size_and_mode (fd, mode_pointer, size_pointer)
9649 long int *size_pointer;
9653 if (fstat (fd, &sbuf) < 0) return (-1);
9654 if (mode_pointer) *mode_pointer = sbuf.st_mode;
9655 if (size_pointer) *size_pointer = sbuf.st_size;
9660 output_dots (fd, depth)
9673 /* Under VMS we need to fix up the "include" specification
9674 filename so that everything following the 1st slash is
9675 changed into its correct VMS file specification. */
9678 hack_vms_include_specification (fname)
9681 register char *cp, *cp1, *cp2;
9682 int f, check_filename_before_returning, no_prefix_seen;
9685 check_filename_before_returning = 0;
9688 /* Ignore leading "./"s */
9689 while (fname[0] == '.' && fname[1] == '/') {
9690 strcpy (fname, fname+2);
9691 no_prefix_seen = 1; /* mark this for later */
9693 /* Look for the boundary between the VMS and UNIX filespecs */
9694 cp = rindex (fname, ']'); /* Look for end of dirspec. */
9695 if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto */
9696 if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
9700 cp = index (fname, '/'); /* Look for the "/" */
9704 * Check if we have a vax-c style '#include filename'
9705 * and add the missing .h
9708 if (index(fname,'.') == 0)
9709 strcat(fname, ".h");
9711 if (index(cp,'.') == 0)
9715 cp2 = Local; /* initialize */
9717 /* We are trying to do a number of things here. First of all, we are
9718 trying to hammer the filenames into a standard format, such that later
9719 processing can handle them.
9721 If the file name contains something like [dir.], then it recognizes this
9722 as a root, and strips the ".]". Later processing will add whatever is
9723 needed to get things working properly.
9725 If no device is specified, then the first directory name is taken to be
9726 a device name (or a rooted logical). */
9728 /* See if we found that 1st slash */
9729 if (cp == 0) return; /* Nothing to do!!! */
9730 if (*cp != '/') return; /* Nothing to do!!! */
9731 /* Point to the UNIX filename part (which needs to be fixed!) */
9733 /* If the directory spec is not rooted, we can just copy
9734 the UNIX filename part and we are done */
9735 if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
9736 if (cp[-2] != '.') {
9738 * The VMS part ends in a `]', and the preceding character is not a `.'.
9739 * We strip the `]', and then splice the two parts of the name in the
9740 * usual way. Given the default locations for include files in cccp.c,
9741 * we will only use this code if the user specifies alternate locations
9742 * with the /include (-I) switch on the command line. */
9743 cp -= 1; /* Strip "]" */
9744 cp1--; /* backspace */
9747 * The VMS part has a ".]" at the end, and this will not do. Later
9748 * processing will add a second directory spec, and this would be a syntax
9749 * error. Thus we strip the ".]", and thus merge the directory specs.
9750 * We also backspace cp1, so that it points to a '/'. This inhibits the
9751 * generation of the 000000 root directory spec (which does not belong here
9754 cp -= 2; /* Strip ".]" */
9755 cp1--; }; /* backspace */
9758 /* We drop in here if there is no VMS style directory specification yet.
9759 * If there is no device specification either, we make the first dir a
9760 * device and try that. If we do not do this, then we will be essentially
9761 * searching the users default directory (as if they did a #include "asdf.h").
9763 * Then all we need to do is to push a '[' into the output string. Later
9764 * processing will fill this in, and close the bracket.
9766 if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec. take first dir */
9767 *cp2++ = '['; /* Open the directory specification */
9770 /* at this point we assume that we have the device spec, and (at least
9771 the opening "[" for a directory specification. We may have directories
9772 specified already */
9774 /* If there are no other slashes then the filename will be
9775 in the "root" directory. Otherwise, we need to add
9776 directory specifications. */
9777 if (index (cp1, '/') == 0) {
9778 /* Just add "000000]" as the directory string */
9779 strcpy (cp2, "000000]");
9780 cp2 += strlen (cp2);
9781 check_filename_before_returning = 1; /* we might need to fool with this later */
9783 /* As long as there are still subdirectories to add, do them. */
9784 while (index (cp1, '/') != 0) {
9785 /* If this token is "." we can ignore it */
9786 if ((cp1[0] == '.') && (cp1[1] == '/')) {
9790 /* Add a subdirectory spec. Do not duplicate "." */
9791 if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
9793 /* If this is ".." then the spec becomes "-" */
9794 if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
9795 /* Add "-" and skip the ".." */
9800 /* Copy the subdirectory */
9801 while (*cp1 != '/') *cp2++= *cp1++;
9802 cp1++; /* Skip the "/" */
9804 /* Close the directory specification */
9805 if (cp2[-1] == '.') /* no trailing periods */
9809 /* Now add the filename */
9810 while (*cp1) *cp2++ = *cp1++;
9812 /* Now append it to the original VMS spec. */
9815 /* If we put a [000000] in the filename, try to open it first. If this fails,
9816 remove the [000000], and return that name. This provides flexibility
9817 to the user in that they can use both rooted and non-rooted logical names
9818 to point to the location of the file. */
9820 if (check_filename_before_returning && no_prefix_seen) {
9821 f = open (fname, O_RDONLY, 0666);
9823 /* The file name is OK as it is, so return it as is. */
9827 /* The filename did not work. Try to remove the [000000] from the name,
9829 cp = index (fname, '[');
9830 cp2 = index (fname, ']') + 1;
9831 strcpy (cp, cp2); /* this gets rid of it */
9839 /* These are the read/write replacement routines for
9840 VAX-11 "C". They make read/write behave enough
9841 like their UNIX counterparts that CCCP will work */
9844 read (fd, buf, size)
9849 #undef read /* Get back the REAL read routine */
9851 register int total = 0;
9853 /* Read until the buffer is exhausted */
9855 /* Limit each read to 32KB */
9856 i = (size > (32*1024)) ? (32*1024) : size;
9857 i = read (fd, buf, i);
9859 if (i == 0) return (total);
9862 /* Account for this read */
9871 write (fd, buf, size)
9876 #undef write /* Get back the REAL write routine */
9880 /* Limit individual writes to 32Kb */
9883 j = (i > (32*1024)) ? (32*1024) : i;
9884 if (write (fd, buf, j) < 0) return (-1);
9885 /* Account for the data written */
9892 /* The following wrapper functions supply additional arguments to the VMS
9893 I/O routines to optimize performance with file handling. The arguments
9895 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
9896 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
9897 "fop=tef"- Truncate unused portions of file when closing file.
9898 "shr=nil"- Disallow file sharing while file is open.
9902 freopen (fname, type, oldfile)
9907 #undef freopen /* Get back the REAL fopen routine */
9908 if (strcmp (type, "w") == 0)
9909 return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
9910 return freopen (fname, type, oldfile, "mbc=16");
9918 #undef fopen /* Get back the REAL fopen routine */
9919 if (strcmp (type, "w") == 0)
9920 return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
9921 return fopen (fname, type, "mbc=16");
9925 open (fname, flags, prot)
9930 #undef open /* Get back the REAL open routine */
9931 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
9934 /* Avoid run-time library bug, where copying M out of N+M characters with
9935 N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
9936 gcc-cpp exercises this particular bug. */
9939 strncat (dst, src, cnt)
9944 register char *d = dst, *s = (char *) src;
9945 register int n = cnt; /* convert to _signed_ type */
9947 while (*d) d++; /* advance to end */
9949 if (!(*d++ = *s++)) break;
9950 if (n < 0) *d = '\0';