1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 1987, 1989, 1992 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
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"
56 /* By default, colon separates directories in a path. */
57 #ifndef PATH_SEPARATOR
58 #define PATH_SEPARATOR ':'
61 /* In case config.h defines these. */
66 #include <sys/types.h>
74 #include <sys/time.h> /* for __DATE__ and __TIME__ */
75 #include <sys/resource.h>
78 #define rindex strrchr
84 extern char *index ();
85 extern char *rindex ();
87 /* VMS-specific definitions */
90 #include <errno.h> /* This defines "errno" properly */
91 #include <perror.h> /* This defines sys_errlist/sys_nerr properly */
93 #define O_RDONLY 0 /* Open arg for Read/Only */
94 #define O_WRONLY 1 /* Open arg for Write/Only */
95 #define read(fd,buf,size) VMS_read(fd,buf,size)
96 #define write(fd,buf,size) VMS_write(fd,buf,size)
97 #define open(fname,mode,prot) VMS_open(fname,mode,prot)
98 #define fopen(fname,mode) VMS_fopen(fname,mode)
99 #define freopen(fname,mode,ofile) VMS_freopen(fname,mode,ofile)
100 static int VMS_read ();
101 static int VMS_write ();
102 static int VMS_open ();
103 static FILE * VMS_fopen ();
104 static FILE * VMS_freopen ();
105 static void hack_vms_include_specification ();
106 typedef struct { unsigned :16, :16, :16; } vms_ino_t
;
107 #define ino_t vms_ino_t
109 #define BSTRING /* VMS/GCC supplies the bstring routines */
110 #endif /* __GNUC__ */
119 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
120 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
123 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
126 /* Exported declarations. */
132 /* External declarations. */
134 extern char *getenv ();
135 extern FILE *fdopen ();
136 extern char *version_string
;
137 extern struct tm
*localtime ();
139 extern char *sys_errlist
[];
145 /* Forward declarations. */
152 #if defined(USG) || defined(VMS)
160 /* These functions are declared to return int instead of void since they
161 are going to be placed in a table and some old compilers have trouble with
162 pointers to functions returning void. */
164 static int do_define ();
165 static int do_line ();
166 static int do_include ();
167 static int do_undef ();
168 static int do_error ();
169 static int do_pragma ();
170 static int do_ident ();
172 static int do_xifdef ();
173 static int do_else ();
174 static int do_elif ();
175 static int do_endif ();
176 static int do_sccs ();
177 static int do_once ();
178 static int do_assert ();
179 static int do_unassert ();
180 static int do_warning ();
182 static void add_import ();
183 static void deps_output ();
184 static void make_undef ();
185 static void make_definition ();
186 static void make_assertion ();
187 static void path_include ();
188 static void initialize_builtins ();
189 static void initialize_char_syntax ();
190 static void dump_arg_n ();
191 static void dump_defn_1 ();
192 static void delete_macro ();
193 static void trigraph_pcp ();
194 static void rescan ();
195 static void finclude ();
196 static void validate_else ();
197 static int comp_def_part ();
198 static void error_from_errno ();
199 static void error_with_line ();
201 static void pedwarn_with_file_and_line ();
202 static void fatal ();
204 static void pfatal_with_name ();
205 static void perror_with_name ();
206 static void print_containing_files ();
207 static int lookup_import ();
208 static int lookup_include ();
209 static int check_preconditions ();
210 static void pcfinclude ();
211 static void pcstring_used ();
212 static void write_output ();
213 static int check_macro_name ();
214 static int compare_defs ();
215 static int compare_token_lists ();
216 static int eval_if_expression ();
217 static int discard_comments ();
218 static int delete_newlines ();
219 static int line_for_error ();
221 static int file_size_and_mode ();
223 static struct arglist
*read_token_list ();
224 static void free_token_list ();
226 static struct hashnode
*install ();
227 struct hashnode
*lookup ();
229 static struct assertion_hashnode
*assertion_install ();
230 static struct assertion_hashnode
*assertion_lookup ();
232 static char *xrealloc ();
233 static char *xcalloc ();
234 static char *savestring ();
236 static void delete_assertion ();
237 static void macroexpand ();
238 static void dump_all_macros ();
239 static void conditional_skip ();
240 static void skip_if_group ();
241 static void output_line_command ();
243 /* Last arg to output_line_command. */
244 enum file_change_code
{same_file
, enter_file
, leave_file
};
246 static int grow_outbuf ();
247 static int handle_directive ();
248 static void memory_full ();
250 static U_CHAR
*macarg1 ();
251 static char *macarg ();
253 static U_CHAR
*skip_to_end_of_comment ();
254 static U_CHAR
*skip_quoted_string ();
255 static U_CHAR
*skip_paren_group ();
257 static char *check_precompiled ();
258 static struct macrodef
create_definition ();
259 static void dump_single_macro ();
261 #ifndef FAILURE_EXIT_CODE
262 #define FAILURE_EXIT_CODE 33 /* gnu cc command understands this */
265 #ifndef SUCCESS_EXIT_CODE
266 #define SUCCESS_EXIT_CODE 0 /* 0 means success on Unix. */
269 /* Name under which this program was invoked. */
271 static char *progname
;
273 /* Nonzero means handle C++ comment syntax and use
274 extra default include directories for C++. */
276 static int cplusplus
;
278 /* Nonzero means handle #import, for objective C. */
282 /* Nonzero means this is an assembly file, and allow
283 unknown directives, which could be comments. */
287 /* Current maximum length of directory names in the search path
288 for include files. (Altered as we get more of them.) */
290 static int max_include_len
;
292 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
296 /* Nonzero means copy comments into the output file. */
298 static int put_out_comments
= 0;
300 /* Nonzero means don't process the ANSI trigraph sequences. */
302 static int no_trigraphs
= 0;
304 /* Nonzero means print the names of included files rather than
305 the preprocessed output. 1 means just the #include "...",
306 2 means #include <...> as well. */
308 static int print_deps
= 0;
310 /* Nonzero means print names of header files (-H). */
312 static int print_include_names
= 0;
314 /* Nonzero means don't output line number information. */
316 static int no_line_commands
;
318 /* dump_only means inhibit output of the preprocessed text
319 and instead output the definitions of all user-defined
320 macros in a form suitable for use as input to cccp.
321 dump_names means pass #define and the macro name through to output.
322 dump_definitions means pass the whole definition (plus #define) through
325 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
326 dump_macros
= dump_none
;
328 /* Nonzero means pass all #define and #undef directives which we actually
329 process through to the output stream. This feature is used primarily
330 to allow cc1 to record the #defines and #undefs for the sake of
331 debuggers which understand about preprocessor macros, but it may
332 also be useful with -E to figure out how symbols are defined, and
333 where they are defined. */
334 static int debug_output
= 0;
336 /* Nonzero indicates special processing used by the pcp program. The
337 special effects of this mode are:
339 Inhibit all macro expansion, except those inside #if directives.
341 Process #define directives normally, and output their contents
344 Output preconditions to pcp_outfile indicating all the relevant
345 preconditions for use of this file in a later cpp run.
347 static FILE *pcp_outfile
;
349 /* Nonzero means we are inside an IF during a -pcp run. In this mode
350 macro expansion is done, and preconditions are output for all macro
351 uses requiring them. */
352 static int pcp_inside_if
;
354 /* Nonzero means never to include precompiled files. */
355 static int no_precomp
;
357 /* Nonzero means give all the error messages the ANSI standard requires. */
361 /* Nonzero means try to make failure to fit ANSI C an error. */
363 static int pedantic_errors
;
365 /* Nonzero means don't print warning messages. -w. */
367 static int inhibit_warnings
= 0;
369 /* Nonzero means warn if slash-star appears in a comment. */
371 static int warn_comments
;
373 /* Nonzero means warn if a macro argument is (or would be)
374 stringified with -traditional. */
376 static int warn_stringify
;
378 /* Nonzero means warn if there are any trigraphs. */
380 static int warn_trigraphs
;
382 /* Nonzero means warn if #import is used. */
384 static int warn_import
= 1;
386 /* Nonzero means turn warnings into errors. */
388 static int warnings_are_errors
;
390 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
394 /* Nonzero causes output not to be done,
395 but directives such as #define that have side effects
398 static int no_output
;
400 /* Nonzero means that we have finished processing the command line options.
401 This flag is used to decide whether or not to issue certain errors
404 static int done_initializing
= 0;
406 /* I/O buffer structure.
407 The `fname' field is nonzero for source files and #include files
408 and for the dummy text used for -D and -U.
409 It is zero for rescanning results of macro expansion
410 and for expanding macro arguments. */
411 #define INPUT_STACK_MAX 200
412 static struct file_buf
{
414 /* Filename specified with #line command. */
416 /* Record where in the search path this file was found.
417 For #include_next. */
418 struct file_name_list
*dir
;
423 /* Macro that this level is the expansion of.
424 Included so that we can reenable the macro
425 at the end of this level. */
426 struct hashnode
*macro
;
427 /* Value of if_stack at start of this file.
428 Used to prohibit unmatched #endif (etc) in an include file. */
429 struct if_stack
*if_stack
;
430 /* Object to be freed at end of input at this level. */
432 /* True if this is a header file included using <FILENAME>. */
433 char system_header_p
;
434 } instack
[INPUT_STACK_MAX
];
436 static int last_error_tick
; /* Incremented each time we print it. */
437 static int input_file_stack_tick
; /* Incremented when the status changes. */
439 /* Current nesting level of input sources.
440 `instack[indepth]' is the level currently being read. */
441 static int indepth
= -1;
442 #define CHECK_DEPTH(code) \
443 if (indepth >= (INPUT_STACK_MAX - 1)) \
445 error_with_line (line_for_error (instack[indepth].lineno), \
446 "macro or `#include' recursion too deep"); \
450 /* Current depth in #include directives that use <...>. */
451 static int system_include_depth
= 0;
453 typedef struct file_buf FILE_BUF
;
455 /* The output buffer. Its LENGTH field is the amount of room allocated
456 for the buffer, not the number of chars actually present. To get
457 that, subtract outbuf.buf from outbuf.bufp. */
459 #define OUTBUF_SIZE 10 /* initial size of output buffer */
460 static FILE_BUF outbuf
;
462 /* Grow output buffer OBUF points at
463 so it can hold at least NEEDED more chars. */
465 #define check_expand(OBUF, NEEDED) \
466 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
467 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
469 struct file_name_list
471 struct file_name_list
*next
;
473 /* If the following is nonzero, it is a macro name.
474 Don't include the file again if that macro is defined. */
475 U_CHAR
*control_macro
;
478 /* #include "file" looks in source file dir, then stack. */
479 /* #include <file> just looks in the stack. */
480 /* -I directories are added to the end, then the defaults are added. */
481 static struct default_include
{ char *fname
; int cplusplus
; } include_defaults_array
[]
482 #ifdef INCLUDE_DEFAULTS
486 /* Pick up GNU C++ specific include files. */
487 { GPLUSPLUS_INCLUDE_DIR
, 1},
488 { GCC_INCLUDE_DIR
, 0},
490 /* For cross-compilation, this dir name is generated
491 automatically in Makefile.in. */
492 { CROSS_INCLUDE_DIR
, 0 },
493 #else /* not CROSS_COMPILE */
494 { LOCAL_INCLUDE_DIR
, 0},
495 /* Some systems have an extra dir of include files. */
496 #ifdef SYSTEM_INCLUDE_DIR
497 { SYSTEM_INCLUDE_DIR
, 0},
499 { STANDARD_INCLUDE_DIR
, 0},
500 #endif /* not CROSS_COMPILE */
503 #endif /* no INCLUDE_DEFAULTS */
505 /* The code looks at the defaults through this pointer, rather than through
506 the constant structure above. This pointer gets changed if an environment
507 variable specifies other defaults. */
508 static struct default_include
*include_defaults
= include_defaults_array
;
510 static struct file_name_list
*include
= 0; /* First dir to search */
511 /* First dir to search for <file> */
512 static struct file_name_list
*first_bracket_include
= 0;
513 static struct file_name_list
*last_include
= 0; /* Last in chain */
515 /* Chain of include directories to put at the end of the other chain. */
516 static struct file_name_list
*after_include
= 0;
517 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
519 /* List of included files that contained #pragma once. */
520 static struct file_name_list
*dont_repeat_files
= 0;
522 /* List of other included files.
523 If ->control_macro if nonzero, the file had a #ifndef
524 around the entire contents, and ->control_macro gives the macro name. */
525 static struct file_name_list
*all_include_files
= 0;
527 /* Directory prefix that should replace `/usr' in the standard
528 include file directories. */
529 static char *include_prefix
;
531 /* Global list of strings read in from precompiled files. This list
532 is kept in the order the strings are read in, with new strings being
533 added at the end through stringlist_tailp. We use this list to output
534 the strings at the end of the run.
536 static STRINGDEF
*stringlist
;
537 static STRINGDEF
**stringlist_tailp
= &stringlist
;
540 /* Structure returned by create_definition */
541 typedef struct macrodef MACRODEF
;
544 struct definition
*defn
;
550 /* Structure allocated for every #define. For a simple replacement
553 nargs = -1, the `pattern' list is null, and the expansion is just
554 the replacement text. Nargs = 0 means a functionlike macro with no args,
556 #define getchar() getc (stdin) .
557 When there are args, the expansion is the replacement text with the
558 args squashed out, and the reflist is a list describing how to
559 build the output from the input: e.g., "3 chars, then the 1st arg,
560 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
561 The chars here come from the expansion. Whatever is left of the
562 expansion after the last arg-occurrence is copied after that arg.
563 Note that the reflist can be arbitrarily long---
564 its length depends on the number of times the arguments appear in
565 the replacement text, not how many args there are. Example:
566 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
568 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
569 where (x, y) means (nchars, argno). */
571 typedef struct definition DEFINITION
;
574 int length
; /* length of expansion string */
575 int predefined
; /* True if the macro was builtin or */
576 /* came from the command line */
578 int line
; /* Line number of definition */
579 char *file
; /* File of definition */
580 char rest_args
; /* Nonzero if last arg. absorbs the rest */
582 struct reflist
*next
;
583 char stringify
; /* nonzero if this arg was preceded by a
585 char raw_before
; /* Nonzero if a ## operator before arg. */
586 char raw_after
; /* Nonzero if a ## operator after arg. */
587 char rest_args
; /* Nonzero if this arg. absorbs the rest */
588 int nchars
; /* Number of literal chars to copy before
589 this arg occurrence. */
590 int argno
; /* Number of arg to substitute (origin-0) */
593 /* Names of macro args, concatenated in reverse order
594 with comma-space between them.
595 The only use of this is that we warn on redefinition
596 if this differs between the old and new definitions. */
601 /* different kinds of things that can appear in the value field
602 of a hash node. Actually, this may be useless now. */
611 * special extension string that can be added to the last macro argument to
612 * allow it to absorb the "rest" of the arguments when expanded. Ex:
613 * #define wow(a, b...) process(b, a, b)
614 * { wow(1, 2, 3); } -> { process( 2, 3, 1, 2, 3); }
615 * { wow(one, two); } -> { process( two, one, two); }
616 * if this "rest_arg" is used with the concat token '##' and if it is not
617 * supplied then the token attached to with ## will not be outputed. Ex:
618 * #define wow(a, b...) process(b ## , a, ## b)
619 * { wow(1, 2); } -> { process( 2, 1,2); }
620 * { wow(one); } -> { process( one); {
622 static char rest_extension
[] = "...";
623 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
625 /* The structure of a node in the hash table. The hash table
626 has entries for all tokens defined by #define commands (type T_MACRO),
627 plus some special tokens like __LINE__ (these each have their own
628 type, and the appropriate code is run when that type of node is seen.
629 It does not contain control words like "#define", which are recognized
630 by a separate piece of code. */
632 /* different flavors of hash nodes --- also used in keyword table */
634 T_DEFINE
= 1, /* the `#define' keyword */
635 T_INCLUDE
, /* the `#include' keyword */
636 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
637 T_IMPORT
, /* the `#import' keyword */
638 T_IFDEF
, /* the `#ifdef' keyword */
639 T_IFNDEF
, /* the `#ifndef' keyword */
640 T_IF
, /* the `#if' keyword */
641 T_ELSE
, /* `#else' */
642 T_PRAGMA
, /* `#pragma' */
643 T_ELIF
, /* `#elif' */
644 T_UNDEF
, /* `#undef' */
645 T_LINE
, /* `#line' */
646 T_ERROR
, /* `#error' */
647 T_WARNING
, /* `#warning' */
648 T_ENDIF
, /* `#endif' */
649 T_SCCS
, /* `#sccs', used on system V. */
650 T_IDENT
, /* `#ident', used on system V. */
651 T_ASSERT
, /* `#assert', taken from system V. */
652 T_UNASSERT
, /* `#unassert', taken from system V. */
653 T_SPECLINE
, /* special symbol `__LINE__' */
654 T_DATE
, /* `__DATE__' */
655 T_FILE
, /* `__FILE__' */
656 T_BASE_FILE
, /* `__BASE_FILE__' */
657 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
658 T_VERSION
, /* `__VERSION__' */
659 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
660 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
661 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
662 T_TIME
, /* `__TIME__' */
663 T_CONST
, /* Constant value, used by `__STDC__' */
664 T_MACRO
, /* macro defined by `#define' */
665 T_DISABLED
, /* macro temporarily turned off for rescan */
666 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
667 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
668 T_UNUSED
/* Used for something not defined. */
672 struct hashnode
*next
; /* double links for easy deletion */
673 struct hashnode
*prev
;
674 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
675 chain is kept, in case the node is the head
676 of the chain and gets deleted. */
677 enum node_type type
; /* type of special token */
678 int length
; /* length of token, for quick comparison */
679 U_CHAR
*name
; /* the actual name */
680 union hashval value
; /* pointer to expansion, or whatever */
683 typedef struct hashnode HASHNODE
;
685 /* Some definitions for the hash table. The hash function MUST be
686 computed as shown in hashf () below. That is because the rescan
687 loop computes the hash value `on the fly' for most tokens,
688 in order to avoid the overhead of a lot of procedure calls to
689 the hashf () function. Hashf () only exists for the sake of
690 politeness, for use when speed isn't so important. */
692 #define HASHSIZE 1403
693 static HASHNODE
*hashtab
[HASHSIZE
];
694 #define HASHSTEP(old, c) ((old << 2) + c)
695 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
697 /* Symbols to predefine. */
699 #ifdef CPP_PREDEFINES
700 static char *predefs
= CPP_PREDEFINES
;
702 static char *predefs
= "";
705 /* We let tm.h override the types used here, to handle trivial differences
706 such as the choice of unsigned int or long unsigned int for size_t.
707 When machines start needing nontrivial differences in the size type,
708 it would be best to do something here to figure out automatically
709 from other information what type to use. */
711 /* The string value for __size_type__. */
714 #define SIZE_TYPE "long unsigned int"
717 /* The string value for __ptrdiff_type__. */
720 #define PTRDIFF_TYPE "long int"
723 /* The string value for __wchar_type__. */
726 #define WCHAR_TYPE "int"
729 /* In the definition of a #assert name, this structure forms
730 a list of the individual values asserted.
731 Each value is itself a list of "tokens".
732 These are strings that are compared by name. */
734 struct tokenlist_list
{
735 struct tokenlist_list
*next
;
736 struct arglist
*tokens
;
739 struct assertion_hashnode
{
740 struct assertion_hashnode
*next
; /* double links for easy deletion */
741 struct assertion_hashnode
*prev
;
742 /* also, a back pointer to this node's hash
743 chain is kept, in case the node is the head
744 of the chain and gets deleted. */
745 struct assertion_hashnode
**bucket_hdr
;
746 int length
; /* length of token, for quick comparison */
747 U_CHAR
*name
; /* the actual name */
748 /* List of token-sequences. */
749 struct tokenlist_list
*value
;
752 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
754 /* Some definitions for the hash table. The hash function MUST be
755 computed as shown in hashf () below. That is because the rescan
756 loop computes the hash value `on the fly' for most tokens,
757 in order to avoid the overhead of a lot of procedure calls to
758 the hashf () function. Hashf () only exists for the sake of
759 politeness, for use when speed isn't so important. */
761 #define ASSERTION_HASHSIZE 37
762 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
764 /* Nonzero means inhibit macroexpansion of what seem to be
765 assertion tests, in rescan. For #if. */
766 static int assertions_flag
;
768 /* `struct directive' defines one #-directive, including how to handle it. */
771 int length
; /* Length of name */
772 int (*func
)(); /* Function to handle directive */
773 char *name
; /* Name of directive */
774 enum node_type type
; /* Code which describes which directive. */
775 char angle_brackets
; /* Nonzero => <...> is special. */
776 char traditional_comments
; /* Nonzero: keep comments if -traditional. */
777 char pass_thru
; /* Copy preprocessed directive to output file. */
780 /* Here is the actual list of #-directives, most-often-used first. */
782 static struct directive directive_table
[] = {
783 { 6, do_define
, "define", T_DEFINE
, 0, 1},
784 { 2, do_if
, "if", T_IF
},
785 { 5, do_xifdef
, "ifdef", T_IFDEF
},
786 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
787 { 5, do_endif
, "endif", T_ENDIF
},
788 { 4, do_else
, "else", T_ELSE
},
789 { 4, do_elif
, "elif", T_ELIF
},
790 { 4, do_line
, "line", T_LINE
},
791 { 7, do_include
, "include", T_INCLUDE
, 1},
792 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
793 { 6, do_include
, "import", T_IMPORT
, 1},
794 { 5, do_undef
, "undef", T_UNDEF
},
795 { 5, do_error
, "error", T_ERROR
},
796 { 7, do_warning
, "warning", T_WARNING
},
797 #ifdef SCCS_DIRECTIVE
798 { 4, do_sccs
, "sccs", T_SCCS
},
800 { 6, do_pragma
, "pragma", T_PRAGMA
, 0, 0, 1},
801 { 5, do_ident
, "ident", T_IDENT
, 0, 0, 1},
802 { 6, do_assert
, "assert", T_ASSERT
},
803 { 8, do_unassert
, "unassert", T_UNASSERT
},
804 { -1, 0, "", T_UNUSED
},
807 /* When a directive handler is called,
808 this points to the # that started the directive. */
809 U_CHAR
*directive_start
;
811 /* table to tell if char can be part of a C identifier. */
812 U_CHAR is_idchar
[256];
813 /* table to tell if char can be first char of a c identifier. */
814 U_CHAR is_idstart
[256];
815 /* table to tell if c is horizontal space. */
816 U_CHAR is_hor_space
[256];
817 /* table to tell if c is horizontal or vertical space. */
818 static U_CHAR is_space
[256];
820 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
821 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
823 static int errors
= 0; /* Error counter for exit code */
825 /* Zero means dollar signs are punctuation.
826 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
827 This must be 0 for correct processing of this ANSI C program:
829 #define lose(b) foo(b)
832 static int dollars_in_ident
;
833 #ifndef DOLLARS_IN_IDENTIFIERS
834 #define DOLLARS_IN_IDENTIFIERS 1
837 static FILE_BUF
expand_to_temp_buffer ();
839 static DEFINITION
*collect_expansion ();
841 /* Stack of conditionals currently in progress
842 (including both successful and failing conditionals). */
845 struct if_stack
*next
; /* for chaining to the next stack frame */
846 char *fname
; /* copied from input when frame is made */
847 int lineno
; /* similarly */
848 int if_succeeded
; /* true if a leg of this if-group
849 has been passed through rescan */
850 U_CHAR
*control_macro
; /* For #ifndef at start of file,
851 this is the macro name tested. */
852 enum node_type type
; /* type of last directive seen in this group */
854 typedef struct if_stack IF_STACK_FRAME
;
855 static IF_STACK_FRAME
*if_stack
= NULL
;
857 /* Buffer of -M output. */
858 static char *deps_buffer
;
860 /* Number of bytes allocated in above. */
861 static int deps_allocated_size
;
863 /* Number of bytes used. */
864 static int deps_size
;
866 /* Number of bytes since the last newline. */
867 static int deps_column
;
869 /* File name which deps are being written to.
870 This is 0 if deps are being written to stdout. */
871 static char *deps_file
= 0;
873 /* Nonzero means -I- has been seen,
874 so don't look for #include "foo" the source-file directory. */
875 static int ignore_srcdir
;
877 /* Handler for SIGPIPE. */
881 /* If this is missing, some compilers complain. */
884 fatal ("output pipe has been closed");
894 char *in_fname
, *out_fname
;
898 char **pend_files
= (char **) xmalloc (argc
* sizeof (char *));
899 char **pend_defs
= (char **) xmalloc (argc
* sizeof (char *));
900 char **pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
901 char **pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
902 char **pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
904 /* Record the option used with each element of pend_assertions.
905 This is preparation for supporting more than one option for making
907 char **pend_assertion_options
= (char **) xmalloc (argc
* sizeof (char *));
908 int inhibit_predefs
= 0;
909 int no_standard_includes
= 0;
910 int no_standard_cplusplus_includes
= 0;
911 int missing_newline
= 0;
913 /* Non-0 means don't output the preprocessed program. */
914 int inhibit_output
= 0;
916 /* Stream on which to print the dependency information. */
917 FILE *deps_stream
= 0;
918 /* Target-name to write with the dependency information. */
919 char *deps_target
= 0;
922 /* Get rid of any avoidable limit on stack size. */
926 /* Set the stack limit huge so that alloca (particularly stringtab
927 * in dbxread.c) does not fail. */
928 getrlimit (RLIMIT_STACK
, &rlim
);
929 rlim
.rlim_cur
= rlim
.rlim_max
;
930 setrlimit (RLIMIT_STACK
, &rlim
);
932 #endif /* RLIMIT_STACK defined */
937 /* Remove directories from PROGNAME. */
940 progname
= savestring (argv
[0]);
942 if (!(s
= rindex (progname
, ']')))
943 s
= rindex (progname
, ':');
945 strcpy (progname
, s
+1);
946 if (s
= rindex (progname
, '.'))
954 /* Initialize is_idchar to allow $. */
955 dollars_in_ident
= 1;
956 initialize_char_syntax ();
957 dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
> 0;
959 no_line_commands
= 0;
961 dump_macros
= dump_none
;
966 signal (SIGPIPE
, pipe_closed
);
969 for (i
= 0; include_defaults
[i
].fname
; i
++)
970 max_include_len
= MAX (max_include_len
,
971 strlen (include_defaults
[i
].fname
));
972 /* Leave room for making file name longer when converting to VMS syntax. */
974 max_include_len
+= 10;
977 bzero (pend_files
, argc
* sizeof (char *));
978 bzero (pend_defs
, argc
* sizeof (char *));
979 bzero (pend_undefs
, argc
* sizeof (char *));
980 bzero (pend_assertions
, argc
* sizeof (char *));
981 bzero (pend_includes
, argc
* sizeof (char *));
983 /* Process switches and find input file name. */
985 for (i
= 1; i
< argc
; i
++) {
986 if (argv
[i
][0] != '-') {
987 if (out_fname
!= NULL
)
988 fatal ("Usage: %s [switches] input output", argv
[0]);
989 else if (in_fname
!= NULL
)
994 switch (argv
[i
][1]) {
997 if (!strcmp (argv
[i
], "-include")) {
999 fatal ("Filename missing after -include option");
1001 pend_includes
[i
] = argv
[i
+1], i
++;
1003 if (!strcmp (argv
[i
], "-imacros")) {
1005 fatal ("Filename missing after -imacros option");
1007 pend_files
[i
] = argv
[i
+1], i
++;
1009 if (!strcmp (argv
[i
], "-iprefix")) {
1011 fatal ("Filename missing after -iprefix option");
1013 include_prefix
= argv
[++i
];
1015 /* Add directory to end of path for includes. */
1016 if (!strcmp (argv
[i
], "-idirafter")) {
1017 struct file_name_list
*dirtmp
;
1019 dirtmp
= (struct file_name_list
*)
1020 xmalloc (sizeof (struct file_name_list
));
1021 dirtmp
->next
= 0; /* New one goes on the end */
1022 dirtmp
->control_macro
= 0;
1023 if (after_include
== 0)
1024 after_include
= dirtmp
;
1026 last_after_include
->next
= dirtmp
;
1027 last_after_include
= dirtmp
; /* Tail follows the last one */
1030 fatal ("Directory name missing after -idirafter option");
1032 dirtmp
->fname
= argv
[++i
];
1034 if (strlen (dirtmp
->fname
) > max_include_len
)
1035 max_include_len
= strlen (dirtmp
->fname
);
1040 if (out_fname
!= NULL
)
1041 fatal ("Output filename specified twice");
1043 fatal ("Filename missing after -o option");
1044 out_fname
= argv
[++i
];
1045 if (!strcmp (out_fname
, "-"))
1050 if (!strcmp (argv
[i
], "-pedantic"))
1052 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1054 pedantic_errors
= 1;
1055 } else if (!strcmp (argv
[i
], "-pcp")) {
1056 char *pcp_fname
= argv
[++i
];
1058 ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1059 ? fopen (pcp_fname
, "w")
1060 : fdopen (dup (fileno (stdout
)), "w"));
1061 if (pcp_outfile
== 0)
1062 pfatal_with_name (pcp_fname
);
1068 if (!strcmp (argv
[i
], "-traditional")) {
1070 if (dollars_in_ident
> 0)
1071 dollars_in_ident
= 1;
1072 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1078 if (! strcmp (argv
[i
], "-lang-c"))
1079 cplusplus
= 0, objc
= 0;
1080 if (! strcmp (argv
[i
], "-lang-c++"))
1081 cplusplus
= 1, objc
= 0;
1082 if (! strcmp (argv
[i
], "-lang-objc"))
1083 objc
= 1, cplusplus
= 0;
1084 if (! strcmp (argv
[i
], "-lang-objc++"))
1085 objc
= 1, cplusplus
= 1;
1086 if (! strcmp (argv
[i
], "-lang-asm"))
1088 if (! strcmp (argv
[i
], "-lint"))
1097 inhibit_warnings
= 1;
1101 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1103 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1105 else if (!strcmp (argv
[i
], "-Wcomment"))
1107 else if (!strcmp (argv
[i
], "-Wno-comment"))
1109 else if (!strcmp (argv
[i
], "-Wcomments"))
1111 else if (!strcmp (argv
[i
], "-Wno-comments"))
1113 else if (!strcmp (argv
[i
], "-Wtraditional"))
1115 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1117 else if (!strcmp (argv
[i
], "-Wimport"))
1119 else if (!strcmp (argv
[i
], "-Wno-import"))
1121 else if (!strcmp (argv
[i
], "-Werror"))
1122 warnings_are_errors
= 1;
1123 else if (!strcmp (argv
[i
], "-Wno-error"))
1124 warnings_are_errors
= 0;
1125 else if (!strcmp (argv
[i
], "-Wall"))
1133 if (!strcmp (argv
[i
], "-M"))
1135 else if (!strcmp (argv
[i
], "-MM"))
1137 else if (!strcmp (argv
[i
], "-MD"))
1139 else if (!strcmp (argv
[i
], "-MMD"))
1141 /* For -MD and -MMD options, write deps on file named by next arg. */
1142 if (!strcmp (argv
[i
], "-MD")
1143 || !strcmp (argv
[i
], "-MMD")) {
1145 deps_file
= argv
[i
];
1146 deps_stream
= fopen (argv
[i
], "a");
1147 if (deps_stream
== 0)
1148 pfatal_with_name (argv
[i
]);
1150 /* For -M and -MM, write deps on standard output
1151 and suppress the usual output. */
1152 deps_stream
= stdout
;
1159 char *p
= argv
[i
] + 2;
1162 /* Arg to -d specifies what parts of macros to dump */
1165 dump_macros
= dump_only
;
1169 dump_macros
= dump_names
;
1172 dump_macros
= dump_definitions
;
1180 if (argv
[i
][2] == '3')
1185 fprintf (stderr
, "GNU CPP version %s", version_string
);
1186 #ifdef TARGET_VERSION
1189 fprintf (stderr
, "\n");
1193 print_include_names
= 1;
1200 if (argv
[i
][2] != 0)
1202 else if (i
+ 1 == argc
)
1203 fatal ("Macro name missing after -D option");
1215 if (argv
[i
][2] != 0)
1217 else if (i
+ 1 == argc
)
1218 fatal ("Assertion missing after -A option");
1222 if (!strcmp (p
, "-")) {
1223 /* -A- eliminates all predefined macros and assertions.
1224 Let's include also any that were specified earlier
1225 on the command line. That way we can get rid of any
1226 that were passed automatically in from GCC. */
1228 inhibit_predefs
= 1;
1229 for (j
= 0; j
< i
; j
++)
1230 pend_defs
[j
] = pend_assertions
[j
] = 0;
1232 pend_assertions
[i
] = p
;
1233 pend_assertion_options
[i
] = "-A";
1238 case 'U': /* JF #undef something */
1239 if (argv
[i
][2] != 0)
1240 pend_undefs
[i
] = argv
[i
] + 2;
1241 else if (i
+ 1 == argc
)
1242 fatal ("Macro name missing after -U option");
1244 pend_undefs
[i
] = argv
[i
+1], i
++;
1248 put_out_comments
= 1;
1251 case 'E': /* -E comes from cc -E; ignore it. */
1255 no_line_commands
= 1;
1258 case '$': /* Don't include $ in identifiers. */
1259 dollars_in_ident
= 0;
1262 case 'I': /* Add directory to path for includes. */
1264 struct file_name_list
*dirtmp
;
1266 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-"))
1269 dirtmp
= (struct file_name_list
*)
1270 xmalloc (sizeof (struct file_name_list
));
1271 dirtmp
->next
= 0; /* New one goes on the end */
1272 dirtmp
->control_macro
= 0;
1276 last_include
->next
= dirtmp
;
1277 last_include
= dirtmp
; /* Tail follows the last one */
1278 if (argv
[i
][2] != 0)
1279 dirtmp
->fname
= argv
[i
] + 2;
1280 else if (i
+ 1 == argc
)
1281 fatal ("Directory name missing after -I option");
1283 dirtmp
->fname
= argv
[++i
];
1284 if (strlen (dirtmp
->fname
) > max_include_len
)
1285 max_include_len
= strlen (dirtmp
->fname
);
1286 if (ignore_srcdir
&& first_bracket_include
== 0)
1287 first_bracket_include
= dirtmp
;
1293 if (!strcmp (argv
[i
], "-nostdinc"))
1294 /* -nostdinc causes no default include directories.
1295 You must specify all include-file directories with -I. */
1296 no_standard_includes
= 1;
1297 else if (!strcmp (argv
[i
], "-nostdinc++"))
1298 /* -nostdinc++ causes no default C++-specific include directories. */
1299 no_standard_cplusplus_includes
= 1;
1300 else if (!strcmp (argv
[i
], "-noprecomp"))
1305 /* Sun compiler passes undocumented switch "-undef".
1306 Let's assume it means to inhibit the predefined symbols. */
1307 inhibit_predefs
= 1;
1310 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1311 if (in_fname
== NULL
) {
1314 } else if (out_fname
== NULL
) {
1317 } /* else fall through into error */
1320 fatal ("Invalid option `%s'", argv
[i
]);
1325 /* Add dirs from CPATH after dirs from -I. */
1326 /* There seems to be confusion about what CPATH should do,
1327 so for the moment it is not documented. */
1328 /* Some people say that CPATH should replace the standard include dirs,
1329 but that seems pointless: it comes before them, so it overrides them
1331 p
= (char *) getenv ("CPATH");
1332 if (p
!= 0 && ! no_standard_includes
)
1335 /* Now that dollars_in_ident is known, initialize is_idchar. */
1336 initialize_char_syntax ();
1338 /* Initialize output buffer */
1340 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1341 outbuf
.bufp
= outbuf
.buf
;
1342 outbuf
.length
= OUTBUF_SIZE
;
1344 /* Do partial setup of input buffer for the sake of generating
1345 early #line directives (when -g is in effect). */
1347 fp
= &instack
[++indepth
];
1348 if (in_fname
== NULL
)
1350 fp
->nominal_fname
= fp
->fname
= in_fname
;
1353 /* Install __LINE__, etc. Must follow initialize_char_syntax
1354 and option processing. */
1355 initialize_builtins (fp
, &outbuf
);
1357 /* Do standard #defines and assertions
1358 that identify system and machine type. */
1360 if (!inhibit_predefs
) {
1361 char *p
= (char *) alloca (strlen (predefs
) + 1);
1362 strcpy (p
, predefs
);
1365 while (*p
== ' ' || *p
== '\t')
1367 /* Handle -D options. */
1368 if (p
[0] == '-' && p
[1] == 'D') {
1370 while (*p
&& *p
!= ' ' && *p
!= '\t')
1375 output_line_command (fp
, &outbuf
, 0, same_file
);
1376 make_definition (q
, &outbuf
);
1377 while (*p
== ' ' || *p
== '\t')
1379 } else if (p
[0] == '-' && p
[1] == 'A') {
1380 /* Handle -A options (assertions). */
1389 past_name
= assertion
;
1390 /* Locate end of name. */
1391 while (*past_name
&& *past_name
!= ' '
1392 && *past_name
!= '\t' && *past_name
!= '(')
1394 /* Locate `(' at start of value. */
1396 while (*value
&& (*value
== ' ' || *value
== '\t'))
1398 if (*value
++ != '(')
1400 while (*value
&& (*value
== ' ' || *value
== '\t'))
1403 /* Locate end of value. */
1404 while (*past_value
&& *past_value
!= ' '
1405 && *past_value
!= '\t' && *past_value
!= ')')
1407 termination
= past_value
;
1408 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
1410 if (*termination
++ != ')')
1412 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
1414 /* Temporarily null-terminate the value. */
1415 save_char
= *termination
;
1416 *termination
= '\0';
1417 /* Install the assertion. */
1418 make_assertion ("-A", assertion
);
1419 *termination
= (char) save_char
;
1421 while (*p
== ' ' || *p
== '\t')
1429 /* Now handle the command line options. */
1431 /* Do assertions specified with -A. */
1432 for (i
= 1; i
< argc
; i
++)
1433 if (pend_assertions
[i
])
1434 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1436 /* Do defines specified with -D. */
1437 for (i
= 1; i
< argc
; i
++)
1440 output_line_command (fp
, &outbuf
, 0, same_file
);
1441 make_definition (pend_defs
[i
], &outbuf
);
1444 /* Do undefines specified with -U. */
1445 for (i
= 1; i
< argc
; i
++)
1446 if (pend_undefs
[i
]) {
1448 output_line_command (fp
, &outbuf
, 0, same_file
);
1449 make_undef (pend_undefs
[i
], &outbuf
);
1452 done_initializing
= 1;
1454 { /* read the appropriate environment variable and if it exists
1455 replace include_defaults with the listed path. */
1457 switch ((objc
<< 1) + cplusplus
)
1460 epath
= getenv ("C_INCLUDE_PATH");
1463 epath
= getenv ("CPLUS_INCLUDE_PATH");
1466 epath
= getenv ("OBJC_INCLUDE_PATH");
1469 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
1472 /* If the environment var for this language is set,
1473 add to the default list of include directories. */
1475 char *nstore
= (char *) alloca (strlen (epath
) + 2);
1477 char *startp
, *endp
;
1479 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1483 = (struct default_include
*) xmalloc ((num_dirs
1484 * sizeof (struct default_include
))
1485 + sizeof (include_defaults_array
));
1486 startp
= endp
= epath
;
1489 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1490 if ((*endp
== PATH_SEPARATOR
1491 #if 0 /* Obsolete, now that we use semicolons as the path separator. */
1493 && (endp
-startp
!= 1 || !isalpha (*startp
)))
1498 strncpy (nstore
, startp
, endp
-startp
);
1500 strcpy (nstore
, ".");
1502 nstore
[endp
-startp
] = '\0';
1504 max_include_len
= MAX (max_include_len
, endp
-startp
+2);
1505 include_defaults
[num_dirs
].fname
= savestring (nstore
);
1506 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1510 endp
= startp
= endp
+ 1;
1514 /* Put the usual defaults back in at the end. */
1515 bcopy (include_defaults_array
, &include_defaults
[num_dirs
],
1516 sizeof (include_defaults_array
));
1520 /* Unless -fnostdinc,
1521 tack on the standard include file dirs to the specified list */
1522 if (!no_standard_includes
) {
1523 struct default_include
*p
= include_defaults
;
1524 char *specd_prefix
= include_prefix
;
1525 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
1526 int default_len
= 0;
1527 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1528 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1529 default_len
= strlen (default_prefix
) - 7;
1530 default_prefix
[default_len
] = 0;
1532 /* Search "translated" versions of GNU directories.
1533 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1534 if (specd_prefix
!= 0 && default_len
!= 0)
1535 for (p
= include_defaults
; p
->fname
; p
++) {
1536 /* Some standard dirs are only for C++. */
1537 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1538 /* Does this dir start with the prefix? */
1539 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
1540 /* Yes; change prefix and add to search list. */
1541 struct file_name_list
*new
1542 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
1543 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
1544 char *str
= (char *) xmalloc (this_len
+ 1);
1545 strcpy (str
, specd_prefix
);
1546 strcat (str
, p
->fname
+ default_len
);
1548 new->control_macro
= 0;
1550 /* Add elt to tail of list. */
1554 last_include
->next
= new;
1555 /* Make sure list for #include <...> also has the standard dirs. */
1556 if (ignore_srcdir
&& first_bracket_include
== 0)
1557 first_bracket_include
= new;
1558 /* Record new tail. */
1560 /* Update max_include_len if necessary. */
1561 if (this_len
> max_include_len
)
1562 max_include_len
= this_len
;
1566 /* Search ordinary names for GNU include directories. */
1567 for (p
= include_defaults
; p
->fname
; p
++) {
1568 /* Some standard dirs are only for C++. */
1569 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1570 struct file_name_list
*new
1571 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
1572 new->control_macro
= 0;
1573 /* Add elt to tail of list. */
1577 last_include
->next
= new;
1578 /* Make sure list for #include <...> also has the standard dirs. */
1579 if (ignore_srcdir
&& first_bracket_include
== 0)
1580 first_bracket_include
= new;
1581 /* Record new tail. */
1583 new->fname
= p
->fname
;
1588 /* Tack the after_include chain at the end of the include chain. */
1590 last_include
->next
= after_include
;
1592 include
= after_include
;
1593 if (ignore_srcdir
&& first_bracket_include
== 0)
1594 first_bracket_include
= after_include
;
1596 /* Terminate the after_include chain. */
1597 if (last_after_include
)
1598 last_after_include
->next
= 0;
1600 /* Scan the -imacros files before the main input.
1601 Much like #including them, but with no_output set
1602 so that only their macro definitions matter. */
1605 for (i
= 1; i
< argc
; i
++)
1606 if (pend_files
[i
]) {
1607 int fd
= open (pend_files
[i
], O_RDONLY
, 0666);
1609 perror_with_name (pend_files
[i
]);
1610 return FAILURE_EXIT_CODE
;
1612 finclude (fd
, pend_files
[i
], &outbuf
, 0, 0);
1616 /* Copy the entire contents of the main input file into
1617 the stacked input buffer previously allocated for it. */
1619 /* JF check for stdin */
1620 if (in_fname
== NULL
|| *in_fname
== 0) {
1623 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
1626 /* Either of two environment variables can specify output of deps.
1627 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1628 where OUTPUT_FILE is the file to write deps info to
1629 and DEPS_TARGET is the target to mention in the deps. */
1632 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1633 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1634 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
1639 spec
= getenv ("SUNPRO_DEPENDENCIES");
1646 /* Find the space before the DEPS_TARGET, if there is one. */
1647 /* Don't use `index'; that causes trouble on USG. */
1648 while (*s
!= 0 && *s
!= ' ') s
++;
1650 deps_target
= s
+ 1;
1651 output_file
= (char *) xmalloc (s
- spec
+ 1);
1652 bcopy (spec
, output_file
, s
- spec
);
1653 output_file
[s
- spec
] = 0;
1660 deps_file
= output_file
;
1661 deps_stream
= fopen (output_file
, "a");
1662 if (deps_stream
== 0)
1663 pfatal_with_name (output_file
);
1666 /* For -M, print the expected object file name
1667 as the target of this Make-rule. */
1669 deps_allocated_size
= 200;
1670 deps_buffer
= (char *) xmalloc (deps_allocated_size
);
1676 deps_output (deps_target
, 0);
1677 deps_output (":", 0);
1678 } else if (*in_fname
== 0)
1679 deps_output ("-: ", 0);
1684 /* Discard all directory prefixes from P. */
1690 /* Output P, but remove known suffixes. */
1692 if (p
[len
- 2] == '.' && p
[len
- 1] == 'c')
1693 deps_output (p
, len
- 2);
1694 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'C')
1695 deps_output (p
, len
- 2);
1696 else if (p
[len
- 3] == '.'
1697 && p
[len
- 2] == 'c'
1698 && p
[len
- 1] == 'c')
1699 deps_output (p
, len
- 3);
1700 else if (p
[len
- 2] == '.' && p
[len
- 1] == 's')
1701 deps_output (p
, len
- 2);
1702 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'S')
1703 deps_output (p
, len
- 2);
1704 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'm')
1705 deps_output (p
, len
- 2);
1708 /* Supply our own suffix. */
1709 deps_output (".o : ", 0);
1710 deps_output (in_fname
, 0);
1711 deps_output (" ", 0);
1715 file_size_and_mode (f
, &st_mode
, &st_size
);
1716 fp
->nominal_fname
= fp
->fname
= in_fname
;
1718 fp
->system_header_p
= 0;
1719 /* JF all this is mine about reading pipes and ttys */
1720 if (! S_ISREG (st_mode
)) {
1721 /* Read input from a file that is not a normal disk file.
1722 We cannot preallocate a buffer with the correct size,
1723 so we must read in the file a piece at the time and make it bigger. */
1731 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
1734 cnt
= read (f
, bufp
, bsize
- size
);
1735 if (cnt
< 0) goto perror
; /* error! */
1736 if (cnt
== 0) break; /* End of file */
1739 if (bsize
== size
) { /* Buffer is full! */
1741 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
1742 bufp
= fp
->buf
+ size
; /* May have moved */
1747 /* Read a file whose size we can determine in advance.
1748 For the sake of VMS, st_size is just an upper bound. */
1751 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
1753 while (st_size
> 0) {
1754 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
1764 fp
->if_stack
= if_stack
;
1766 /* Make sure data ends with a newline. And put a null after it. */
1768 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
1769 /* Backslash-newline at end is not good enough. */
1770 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
1771 fp
->buf
[fp
->length
++] = '\n';
1772 missing_newline
= 1;
1774 fp
->buf
[fp
->length
] = '\0';
1776 /* Unless inhibited, convert trigraphs in the input. */
1781 /* Now that we know the input file is valid, open the output. */
1783 if (!out_fname
|| !strcmp (out_fname
, ""))
1784 out_fname
= "stdout";
1785 else if (! freopen (out_fname
, "w", stdout
))
1786 pfatal_with_name (out_fname
);
1788 output_line_command (fp
, &outbuf
, 0, same_file
);
1790 /* Scan the -include files before the main input. */
1792 for (i
= 1; i
< argc
; i
++)
1793 if (pend_includes
[i
]) {
1794 int fd
= open (pend_includes
[i
], O_RDONLY
, 0666);
1796 perror_with_name (pend_includes
[i
]);
1797 return FAILURE_EXIT_CODE
;
1799 finclude (fd
, pend_includes
[i
], &outbuf
, 0, 0);
1802 /* Scan the input, processing macros and directives. */
1804 rescan (&outbuf
, 0);
1806 if (pedantic
&& missing_newline
)
1807 pedwarn ("file does not end in newline");
1809 /* Now we have processed the entire input
1810 Write whichever kind of output has been requested. */
1812 if (dump_macros
== dump_only
)
1814 else if (! inhibit_output
) {
1819 /* Don't actually write the deps file if compilation has failed.
1820 Delete it instead. */
1821 if (errors
> 0 && deps_file
!= 0)
1824 fputs (deps_buffer
, deps_stream
);
1825 putc ('\n', deps_stream
);
1826 if (deps_stream
!= stdout
) {
1827 fclose (deps_stream
);
1828 if (ferror (deps_stream
))
1829 fatal ("I/O error on output");
1834 if (ferror (stdout
))
1835 fatal ("I/O error on output");
1838 exit (FAILURE_EXIT_CODE
);
1839 exit (SUCCESS_EXIT_CODE
);
1842 pfatal_with_name (in_fname
);
1846 /* Given a colon-separated list of file names PATH,
1847 add all the names to the search path for include files. */
1861 struct file_name_list
*dirtmp
;
1863 /* Find the end of this name. */
1865 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1866 while (*q
!= 0 && (*q
!= ':' || (q
- p
== 1 && isalpha (*p
)))) q
++;
1868 while (*q
!= 0 && *q
!= ':') q
++;
1871 /* An empty name in the path stands for the current directory. */
1872 name
= (char *) xmalloc (2);
1876 /* Otherwise use the directory that is named. */
1877 name
= (char *) xmalloc (q
- p
+ 1);
1878 bcopy (p
, name
, q
- p
);
1882 dirtmp
= (struct file_name_list
*)
1883 xmalloc (sizeof (struct file_name_list
));
1884 dirtmp
->next
= 0; /* New one goes on the end */
1885 dirtmp
->control_macro
= 0;
1889 last_include
->next
= dirtmp
;
1890 last_include
= dirtmp
; /* Tail follows the last one */
1891 dirtmp
->fname
= name
;
1892 if (strlen (dirtmp
->fname
) > max_include_len
)
1893 max_include_len
= strlen (dirtmp
->fname
);
1894 if (ignore_srcdir
&& first_bracket_include
== 0)
1895 first_bracket_include
= dirtmp
;
1897 /* Advance past this name. */
1901 /* Skip the colon. */
1906 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
1907 before main CCCP processing. Name `pcp' is also in honor of the
1908 drugs the trigraph designers must have been on.
1910 Using an extra pass through the buffer takes a little extra time,
1911 but is infinitely less hairy than trying to handle trigraphs inside
1912 strings, etc. everywhere, and also makes sure that trigraphs are
1913 only translated in the top level of processing. */
1919 register U_CHAR c
, *fptr
, *bptr
, *sptr
;
1922 fptr
= bptr
= sptr
= buf
->buf
;
1923 while ((sptr
= (U_CHAR
*) index (sptr
, '?')) != NULL
) {
1960 len
= sptr
- fptr
- 2;
1961 if (bptr
!= fptr
&& len
> 0)
1962 bcopy (fptr
, bptr
, len
); /* BSD doc says bcopy () works right
1963 for overlapping strings. In ANSI
1964 C, this will be memmove (). */
1969 len
= buf
->length
- (fptr
- buf
->buf
);
1970 if (bptr
!= fptr
&& len
> 0)
1971 bcopy (fptr
, bptr
, len
);
1972 buf
->length
-= fptr
- bptr
;
1973 buf
->buf
[buf
->length
] = '\0';
1974 if (warn_trigraphs
&& fptr
!= bptr
)
1975 warning ("%d trigraph(s) encountered", (fptr
- bptr
) / 2);
1978 /* Move all backslash-newline pairs out of embarrassing places.
1979 Exchange all such pairs following BP
1980 with any potentially-embarrassing characters that follow them.
1981 Potentially-embarrassing characters are / and *
1982 (because a backslash-newline inside a comment delimiter
1983 would cause it not to be recognized). */
1989 register U_CHAR
*p
= bp
;
1990 register int count
= 0;
1992 /* First count the backslash-newline pairs here. */
1998 else if (p
[1] == '\r' && p
[2] == '\n')
2006 /* What follows the backslash-newlines is not embarrassing. */
2008 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
2011 /* Copy all potentially embarrassing characters
2012 that follow the backslash-newline pairs
2013 down to where the pairs originally started. */
2015 while (*p
== '*' || *p
== '/')
2018 /* Now write the same number of pairs after the embarrassing chars. */
2019 while (count
-- > 0) {
2025 /* Like newline_fix but for use within a directive-name.
2026 Move any backslash-newlines up past any following symbol constituents. */
2029 name_newline_fix (bp
)
2032 register U_CHAR
*p
= bp
;
2033 register int count
= 0;
2035 /* First count the backslash-newline pairs here. */
2040 else if (p
[1] == '\r' && p
[2] == '\n')
2048 /* What follows the backslash-newlines is not embarrassing. */
2050 if (count
== 0 || !is_idchar
[*p
])
2053 /* Copy all potentially embarrassing characters
2054 that follow the backslash-newline pairs
2055 down to where the pairs originally started. */
2057 while (is_idchar
[*p
])
2060 /* Now write the same number of pairs after the embarrassing chars. */
2061 while (count
-- > 0) {
2067 /* Look for lint commands in comments.
2069 When we come in here, ibp points into a comment. Limit is as one expects.
2070 scan within the comment -- it should start, after lwsp, with a lint command.
2071 If so that command is returned as a (constant) string.
2073 Upon return, any arg will be pointed to with argstart and will be
2074 arglen long. Note that we don't parse that arg since it will just
2075 be printed out again.
2079 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2080 register U_CHAR
*ibp
;
2081 register U_CHAR
*limit
;
2082 U_CHAR
**argstart
; /* point to command arg */
2083 int *arglen
, *cmdlen
; /* how long they are */
2086 register U_CHAR
*numptr
; /* temp for arg parsing */
2090 SKIP_WHITE_SPACE (ibp
);
2092 if (ibp
>= limit
) return NULL
;
2094 linsize
= limit
- ibp
;
2096 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2097 if ((linsize
>= 10) && !strncmp (ibp
, "NOTREACHED", 10)) {
2099 return "NOTREACHED";
2101 if ((linsize
>= 8) && !strncmp (ibp
, "ARGSUSED", 8)) {
2105 if ((linsize
>= 11) && !strncmp (ibp
, "LINTLIBRARY", 8)) {
2107 return "LINTLIBRARY";
2109 if ((linsize
>= 7) && !strncmp (ibp
, "VARARGS", 7)) {
2111 ibp
+= 7; linsize
-= 7;
2112 if ((linsize
== 0) || ! isdigit (*ibp
)) return "VARARGS";
2114 /* OK, read a number */
2115 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && isdigit (*numptr
);
2117 *arglen
= numptr
- *argstart
;
2124 * The main loop of the program.
2126 * Read characters from the input stack, transferring them to the
2129 * Macros are expanded and push levels on the input stack.
2130 * At the end of such a level it is popped off and we keep reading.
2131 * At the end of any other kind of level, we return.
2132 * #-directives are handled, except within macros.
2134 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2135 * and insert them when appropriate. This is set while scanning macro
2136 * arguments before substitution. It is zero when scanning for final output.
2137 * There are three types of Newline markers:
2138 * * Newline - follows a macro name that was not expanded
2139 * because it appeared inside an expansion of the same macro.
2140 * This marker prevents future expansion of that identifier.
2141 * When the input is rescanned into the final output, these are deleted.
2142 * These are also deleted by ## concatenation.
2143 * * Newline Space (or Newline and any other whitespace character)
2144 * stands for a place that tokens must be separated or whitespace
2145 * is otherwise desirable, but where the ANSI standard specifies there
2146 * is no whitespace. This marker turns into a Space (or whichever other
2147 * whitespace char appears in the marker) in the final output,
2148 * but it turns into nothing in an argument that is stringified with #.
2149 * Such stringified arguments are the only place where the ANSI standard
2150 * specifies with precision that whitespace may not appear.
2152 * During this function, IP->bufp is kept cached in IBP for speed of access.
2153 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2154 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2155 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2156 * explicitly, and before RECACHE, since RECACHE uses OBP.
2160 rescan (op
, output_marks
)
2164 /* Character being scanned in main loop. */
2167 /* Length of pending accumulated identifier. */
2168 register int ident_length
= 0;
2170 /* Hash code of pending accumulated identifier. */
2171 register int hash
= 0;
2173 /* Current input level (&instack[indepth]). */
2176 /* Pointer for scanning input. */
2177 register U_CHAR
*ibp
;
2179 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2180 register U_CHAR
*limit
;
2182 /* Pointer for storing output. */
2183 register U_CHAR
*obp
;
2185 /* REDO_CHAR is nonzero if we are processing an identifier
2186 after backing up over the terminating character.
2187 Sometimes we process an identifier without backing up over
2188 the terminating character, if the terminating character
2189 is not special. Backing up is done so that the terminating character
2190 will be dispatched on again once the identifier is dealt with. */
2193 /* 1 if within an identifier inside of which a concatenation
2194 marker (Newline -) has been seen. */
2195 int concatenated
= 0;
2197 /* While scanning a comment or a string constant,
2198 this records the line it started on, for error messages. */
2201 /* Line where a newline was first seen in a string constant. */
2202 int multiline_string_line
= 0;
2204 /* Record position of last `real' newline. */
2205 U_CHAR
*beg_of_line
;
2207 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2210 do { ip->macro->type = T_MACRO; \
2211 if (ip->free_ptr) free (ip->free_ptr); \
2212 --indepth; } while (0)
2214 /* Reload `rescan's local variables that describe the current
2215 level of the input stack. */
2218 do { ip = &instack[indepth]; \
2220 limit = ip->buf + ip->length; \
2222 check_expand (op, limit - ibp); \
2224 obp = op->bufp; } while (0)
2226 if (no_output
&& instack
[indepth
].fname
!= 0)
2227 skip_if_group (&instack
[indepth
], 1);
2233 /* Our caller must always put a null after the end of
2234 the input at each input stack level. */
2247 /* Always merge lines ending with backslash-newline,
2248 even in middle of identifier. */
2251 --obp
; /* remove backslash from obuf */
2254 /* Otherwise, backslash suppresses specialness of following char,
2255 so copy it here to prevent the switch from seeing it.
2256 But first get any pending identifier processed. */
2257 if (ident_length
> 0)
2263 if (assertions_flag
) {
2264 /* Copy #foo (bar lose) without macro expansion. */
2265 SKIP_WHITE_SPACE (ibp
);
2266 while (is_idchar
[*ibp
])
2268 SKIP_WHITE_SPACE (ibp
);
2271 skip_paren_group (ip
);
2272 bcopy (ibp
, obp
, ip
->bufp
- ibp
);
2273 obp
+= ip
->bufp
- ibp
;
2278 /* If this is expanding a macro definition, don't recognize
2279 preprocessor directives. */
2285 /* # keyword: a # must be first nonblank char on the line */
2286 if (beg_of_line
== 0)
2291 /* Scan from start of line, skipping whitespace, comments
2292 and backslash-newlines, and see if we reach this #.
2293 If not, this # is not special. */
2296 if (is_hor_space
[*bp
])
2298 else if (*bp
== '\\' && bp
[1] == '\n')
2300 else if (*bp
== '/' && bp
[1] == '*') {
2302 while (!(*bp
== '*' && bp
[1] == '/'))
2306 else if ((cplusplus
|| objc
) && *bp
== '/' && bp
[1] == '/') {
2308 while (*bp
++ != '\n') ;
2316 /* This # can start a directive. */
2318 --obp
; /* Don't copy the '#' */
2322 if (! handle_directive (ip
, op
)) {
2326 /* Not a known directive: treat it as ordinary text.
2327 IP, OP, IBP, etc. have not been changed. */
2328 if (no_output
&& instack
[indepth
].fname
) {
2329 /* If not generating expanded output,
2330 what we do with ordinary text is skip it.
2331 Discard everything until next # directive. */
2332 skip_if_group (&instack
[indepth
], 1);
2337 ++obp
; /* Copy the '#' after all */
2343 /* A # directive has been successfully processed. */
2344 /* If not generating expanded output, ignore everything until
2345 next # directive. */
2346 if (no_output
&& instack
[indepth
].fname
)
2347 skip_if_group (&instack
[indepth
], 1);
2353 case '\"': /* skip quoted string */
2355 /* A single quoted string is treated like a double -- some
2356 programs (e.g., troff) are perverse this way */
2361 start_line
= ip
->lineno
;
2363 /* Skip ahead to a matching quote. */
2368 if (ip
->macro
!= 0) {
2369 /* try harder: this string crosses a macro expansion boundary */
2375 error_with_line (line_for_error (start_line
),
2376 "unterminated string or character constant");
2377 error_with_line (multiline_string_line
,
2378 "possible real start of unterminated constant");
2379 multiline_string_line
= 0;
2388 /* Traditionally, end of line ends a string constant with no error.
2389 So exit the loop and record the new line. */
2394 if (pedantic
|| c
== '\'') {
2395 error_with_line (line_for_error (start_line
),
2396 "unterminated string or character constant");
2399 if (multiline_string_line
== 0)
2400 multiline_string_line
= ip
->lineno
- 1;
2407 /* Backslash newline is replaced by nothing at all,
2408 but keep the line counts correct. */
2413 /* ANSI stupidly requires that in \\ the second \
2414 is *not* prevented from combining with a newline. */
2415 while (*ibp
== '\\' && ibp
[1] == '\n') {
2434 if (*ibp
== '\\' && ibp
[1] == '\n')
2438 && !((cplusplus
|| objc
) && *ibp
== '/'))
2446 /* C++ style comment... */
2447 start_line
= ip
->lineno
;
2449 --ibp
; /* Back over the slash */
2452 /* Comments are equivalent to spaces. */
2453 if (! put_out_comments
)
2456 /* must fake up a comment here */
2461 U_CHAR
*before_bp
= ibp
+2;
2463 while (ibp
< limit
) {
2464 if (*ibp
++ == '\n') {
2466 if (put_out_comments
) {
2467 bcopy (before_bp
, obp
, ibp
- before_bp
);
2468 obp
+= ibp
- before_bp
;
2477 /* Ordinary C comment. Skip it, optionally copying it to output. */
2479 start_line
= ip
->lineno
;
2481 ++ibp
; /* Skip the star. */
2483 /* If this cpp is for lint, we peek inside the comments: */
2487 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2489 if (lintcmd
!= NULL
) {
2490 /* I believe it is always safe to emit this newline: */
2492 bcopy ("#pragma lint ", obp
, 13);
2494 bcopy (lintcmd
, obp
, cmdlen
);
2499 bcopy (argbp
, obp
, arglen
);
2503 /* OK, now bring us back to the state we were in before we entered
2504 this branch. We need #line b/c the newline for the pragma
2505 could fuck things up. */
2506 output_line_command (ip
, op
, 0, same_file
);
2507 *(obp
++) = ' '; /* just in case, if comments are copied thru */
2512 /* Comments are equivalent to spaces.
2513 Note that we already output the slash; we might not want it.
2514 For -traditional, a comment is equivalent to nothing. */
2515 if (! put_out_comments
) {
2525 U_CHAR
*before_bp
= ibp
;
2527 while (ibp
< limit
) {
2530 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
2531 warning("`/*' within comment");
2534 if (*ibp
== '\\' && ibp
[1] == '\n')
2536 if (ibp
>= limit
|| *ibp
== '/')
2541 /* Copy the newline into the output buffer, in order to
2542 avoid the pain of a #line every time a multiline comment
2544 if (!put_out_comments
)
2552 error_with_line (line_for_error (start_line
),
2553 "unterminated comment");
2556 if (put_out_comments
) {
2557 bcopy (before_bp
, obp
, ibp
- before_bp
);
2558 obp
+= ibp
- before_bp
;
2565 if (!dollars_in_ident
)
2569 case '0': case '1': case '2': case '3': case '4':
2570 case '5': case '6': case '7': case '8': case '9':
2571 /* If digit is not part of identifier, it starts a number,
2572 which means that following letters are not an identifier.
2573 "0x5" does not refer to an identifier "x5".
2574 So copy all alphanumerics that follow without accumulating
2575 as an identifier. Periods also, for sake of "3.e7". */
2577 if (ident_length
== 0) {
2578 while (ibp
< limit
) {
2579 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
2584 /* ".." terminates a preprocessing number. This is useless for C
2585 code but useful for preprocessing other things. */
2586 if (!isalnum (c
) && (c
!= '.' || *ibp
== '.') && c
!= '_') {
2591 /* A sign can be part of a preprocessing number
2592 if it follows an e. */
2593 if (c
== 'e' || c
== 'E') {
2594 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
2598 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
2600 /* But traditional C does not let the token go past the sign. */
2611 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2612 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2613 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2614 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2616 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2617 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
2618 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2619 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2623 /* Compute step of hash function, to avoid a proc call on every token */
2624 hash
= HASHSTEP (hash
, c
);
2628 /* If reprocessing a macro expansion, newline is a special marker. */
2629 if (ip
->macro
!= 0) {
2630 /* Newline White is a "funny space" to separate tokens that are
2631 supposed to be separate but without space between.
2632 Here White means any whitespace character.
2633 Newline - marks a recursive macro use that is not
2634 supposed to be expandable. */
2637 /* Newline - inhibits expansion of preceding token.
2638 If expanding a macro arg, we keep the newline -.
2639 In final output, it is deleted. */
2640 if (! concatenated
) {
2645 if (!output_marks
) {
2648 /* If expanding a macro arg, keep the newline -. */
2651 } else if (is_space
[*ibp
]) {
2652 /* Newline Space does not prevent expansion of preceding token
2653 so expand the preceding token and then come back. */
2654 if (ident_length
> 0)
2657 /* If generating final output, newline space makes a space. */
2658 if (!output_marks
) {
2660 /* And Newline Newline makes a newline, so count it. */
2661 if (obp
[-1] == '\n')
2664 /* If expanding a macro arg, keep the newline space.
2665 If the arg gets stringified, newline space makes nothing. */
2668 } else abort (); /* Newline followed by something random? */
2672 /* If there is a pending identifier, handle it and come back here. */
2673 if (ident_length
> 0)
2678 /* Update the line counts and output a #line if necessary. */
2681 if (ip
->lineno
!= op
->lineno
) {
2683 output_line_command (ip
, op
, 1, same_file
);
2684 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2689 /* Come here either after (1) a null character that is part of the input
2690 or (2) at the end of the input, because there is a null there. */
2693 /* Our input really contains a null character. */
2696 /* At end of a macro-expansion level, pop it and read next level. */
2697 if (ip
->macro
!= 0) {
2700 /* If traditional, and we have an identifier that ends here,
2701 process it now, so we get the right error for recursion. */
2702 if (traditional
&& ident_length
2703 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
2712 /* If we don't have a pending identifier,
2713 return at end of input. */
2714 if (ident_length
== 0) {
2722 /* If we do have a pending identifier, just consider this null
2723 a special character and arrange to dispatch on it again.
2724 The second time, IDENT_LENGTH will be zero so we will return. */
2730 /* Handle the case of a character such as /, ', " or null
2731 seen following an identifier. Back over it so that
2732 after the identifier is processed the special char
2733 will be dispatched on again. */
2743 if (ident_length
> 0) {
2744 register HASHNODE
*hp
;
2746 /* We have just seen an identifier end. If it's a macro, expand it.
2748 IDENT_LENGTH is the length of the identifier
2749 and HASH is its hash code.
2751 The identifier has already been copied to the output,
2752 so if it is a macro we must remove it.
2754 If REDO_CHAR is 0, the char that terminated the identifier
2755 has been skipped in the output and the input.
2756 OBP-IDENT_LENGTH-1 points to the identifier.
2757 If the identifier is a macro, we must back over the terminator.
2759 If REDO_CHAR is 1, the terminating char has already been
2760 backed over. OBP-IDENT_LENGTH points to the identifier. */
2762 if (!pcp_outfile
|| pcp_inside_if
) {
2764 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
2767 if (hp
->length
== ident_length
) {
2768 int obufp_before_macroname
;
2769 int op_lineno_before_macroname
;
2770 register int i
= ident_length
;
2771 register U_CHAR
*p
= hp
->name
;
2772 register U_CHAR
*q
= obp
- i
;
2778 do { /* All this to avoid a strncmp () */
2783 /* We found a use of a macro name.
2784 see if the context shows it is a macro call. */
2786 /* Back up over terminating character if not already done. */
2792 /* Save this as a displacement from the beginning of the output
2793 buffer. We can not save this as a position in the output
2794 buffer, because it may get realloc'ed by RECACHE. */
2795 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
2796 op_lineno_before_macroname
= op
->lineno
;
2798 if (hp
->type
== T_PCSTRING
) {
2799 pcstring_used (hp
); /* Mark the definition of this key
2800 as needed, ensuring that it
2802 break; /* Exit loop, since the key cannot have a
2803 definition any longer. */
2806 /* Record whether the macro is disabled. */
2807 disabled
= hp
->type
== T_DISABLED
;
2809 /* This looks like a macro ref, but if the macro was disabled,
2810 just copy its name and put in a marker if requested. */
2814 /* This error check caught useful cases such as
2815 #define foo(x,y) bar(x(y,0), y)
2818 error ("recursive use of macro `%s'", hp
->name
);
2822 check_expand (op
, limit
- ibp
+ 2);
2829 /* If macro wants an arglist, verify that a '(' follows.
2830 first skip all whitespace, copying it to the output
2831 after the macro name. Then, if there is no '(',
2832 decide this is not a macro call and leave things that way. */
2833 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
2834 && hp
->value
.defn
->nargs
>= 0)
2836 U_CHAR
*old_ibp
= ibp
;
2837 U_CHAR
*old_obp
= obp
;
2838 int old_iln
= ip
->lineno
;
2839 int old_oln
= op
->lineno
;
2842 /* Scan forward over whitespace, copying it to the output. */
2843 if (ibp
== limit
&& ip
->macro
!= 0) {
2848 old_iln
= ip
->lineno
;
2849 old_oln
= op
->lineno
;
2851 /* A comment: copy it unchanged or discard it. */
2852 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
2853 if (put_out_comments
) {
2856 } else if (! traditional
) {
2860 while (ibp
+ 1 != limit
2861 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
2862 /* We need not worry about newline-marks,
2863 since they are never found in comments. */
2865 /* Newline in a file. Count it. */
2869 if (put_out_comments
)
2875 if (put_out_comments
) {
2880 else if (is_space
[*ibp
]) {
2882 if (ibp
[-1] == '\n') {
2883 if (ip
->macro
== 0) {
2884 /* Newline in a file. Count it. */
2887 } else if (!output_marks
) {
2888 /* A newline mark, and we don't want marks
2889 in the output. If it is newline-hyphen,
2890 discard it entirely. Otherwise, it is
2891 newline-whitechar, so keep the whitechar. */
2901 /* A newline mark; copy both chars to the output. */
2909 /* It isn't a macro call.
2910 Put back the space that we just skipped. */
2913 ip
->lineno
= old_iln
;
2914 op
->lineno
= old_oln
;
2915 /* Exit the for loop. */
2920 /* This is now known to be a macro call.
2921 Discard the macro name from the output,
2922 along with any following whitespace just copied. */
2923 obp
= op
->buf
+ obufp_before_macroname
;
2924 op
->lineno
= op_lineno_before_macroname
;
2926 /* Expand the macro, reading arguments as needed,
2927 and push the expansion on the input stack. */
2930 macroexpand (hp
, op
);
2932 /* Reexamine input stack, since macroexpand has pushed
2933 a new level on it. */
2940 } /* End hash-table-search loop */
2942 ident_length
= hash
= 0; /* Stop collecting identifier */
2945 } /* End if (ident_length > 0) */
2947 } /* End per-char loop */
2949 /* Come here to return -- but first give an error message
2950 if there was an unterminated successful conditional. */
2952 if (if_stack
!= ip
->if_stack
) {
2954 switch (if_stack
->type
) {
2971 error_with_line (line_for_error (if_stack
->lineno
),
2972 "unterminated `#%s' conditional", str
);
2974 if_stack
= ip
->if_stack
;
2978 * Rescan a string into a temporary buffer and return the result
2979 * as a FILE_BUF. Note this function returns a struct, not a pointer.
2981 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
2982 * and insert such markers when appropriate. See `rescan' for details.
2983 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
2984 * before substitution; it is 0 for other uses.
2987 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
2988 U_CHAR
*buf
, *limit
;
2989 int output_marks
, assertions
;
2991 register FILE_BUF
*ip
;
2993 int length
= limit
- buf
;
2995 int odepth
= indepth
;
2996 int save_assertions_flag
= assertions_flag
;
2998 assertions_flag
= assertions
;
3003 /* Set up the input on the input stack. */
3005 buf1
= (U_CHAR
*) alloca (length
+ 1);
3007 register U_CHAR
*p1
= buf
;
3008 register U_CHAR
*p2
= buf1
;
3015 /* Set up to receive the output. */
3017 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3018 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3023 CHECK_DEPTH ({return obuf
;});
3027 ip
= &instack
[indepth
];
3029 ip
->nominal_fname
= 0;
3030 ip
->system_header_p
= 0;
3033 ip
->length
= length
;
3034 ip
->buf
= ip
->bufp
= buf1
;
3035 ip
->if_stack
= if_stack
;
3037 ip
->lineno
= obuf
.lineno
= 1;
3039 /* Scan the input, create the output. */
3040 rescan (&obuf
, output_marks
);
3042 /* Pop input stack to original state. */
3045 if (indepth
!= odepth
)
3048 /* Record the output. */
3049 obuf
.length
= obuf
.bufp
- obuf
.buf
;
3051 assertions_flag
= save_assertions_flag
;
3056 * Process a # directive. Expects IP->bufp to point after the '#', as in
3057 * `#define foo bar'. Passes to the command handler
3058 * (do_define, do_include, etc.): the addresses of the 1st and
3059 * last chars of the command (starting immediately after the #
3060 * keyword), plus op and the keyword table pointer. If the command
3061 * contains comments it is copied into a temporary buffer sans comments
3062 * and the temporary buffer is passed to the command handler instead.
3063 * Likewise for backslash-newlines.
3065 * Returns nonzero if this was a known # directive.
3066 * Otherwise, returns zero, without advancing the input pointer.
3070 handle_directive (ip
, op
)
3073 register U_CHAR
*bp
, *cp
;
3074 register struct directive
*kt
;
3075 register int ident_length
;
3078 /* Nonzero means we must copy the entire command
3079 to get rid of comments or backslash-newlines. */
3080 int copy_command
= 0;
3082 U_CHAR
*ident
, *after_ident
;
3086 /* Record where the directive started. do_xifdef needs this. */
3087 directive_start
= bp
- 1;
3089 /* Skip whitespace and \-newline. */
3091 if (is_hor_space
[*bp
]) {
3092 if ((*bp
== '\f' || *bp
== '\v') && pedantic
)
3093 pedwarn ("%s in preprocessing directive",
3094 *bp
== '\f' ? "formfeed" : "vertical tab");
3096 } else if (*bp
== '/' && bp
[1] == '*') {
3098 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3100 } else if (*bp
== '\\' && bp
[1] == '\n') {
3101 bp
+= 2; ip
->lineno
++;
3105 /* Now find end of directive name.
3106 If we encounter a backslash-newline, exchange it with any following
3107 symbol-constituents so that we end up with a contiguous name. */
3114 if (*cp
== '\\' && cp
[1] == '\n')
3115 name_newline_fix (cp
);
3121 ident_length
= cp
- bp
;
3125 /* A line of just `#' becomes blank. */
3127 if (ident_length
== 0 && *after_ident
== '\n') {
3128 ip
->bufp
= after_ident
;
3132 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3134 while (is_idchar
[*p
]) {
3135 if (*p
< '0' || *p
> '9')
3139 /* Handle # followed by a line number. */
3140 if (p
!= ident
&& !is_idchar
[*p
]) {
3141 static struct directive line_directive_table
[] = {
3142 { 4, do_line
, "line", T_LINE
},
3145 pedwarn ("`#' followed by integer");
3146 after_ident
= ident
;
3147 kt
= line_directive_table
;
3151 /* Avoid error for `###' and similar cases unless -pedantic. */
3153 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3155 if (pedantic
&& !lang_asm
)
3156 warning ("invalid preprocessor directive");
3162 error ("invalid preprocessor directive name");
3168 * Decode the keyword and call the appropriate expansion
3169 * routine, after moving the input pointer up to the next line.
3171 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3172 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
)) {
3173 register U_CHAR
*buf
;
3174 register U_CHAR
*limit
;
3177 int *already_output
= 0;
3179 /* Nonzero means do not delete comments within the directive.
3180 #define needs this when -traditional. */
3185 limit
= ip
->buf
+ ip
->length
;
3187 keep_comments
= traditional
&& kt
->traditional_comments
;
3188 /* #import is defined only in Objective C, or when on the NeXT. */
3189 if (kt
->type
== T_IMPORT
&& !(objc
|| lookup ("__NeXT__", -1, -1)))
3192 /* Find the end of this command (first newline not backslashed
3193 and not in a string or comment).
3194 Set COPY_COMMAND if the command must be copied
3195 (it contains a backslash-newline or a comment). */
3197 buf
= bp
= after_ident
;
3198 while (bp
< limit
) {
3199 register U_CHAR c
= *bp
++;
3213 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
3214 /* Don't bother calling the directive if we already got an error
3215 message due to unterminated string. Skip everything and pretend
3216 we called the directive. */
3219 /* Traditional preprocessing permits unterminated strings. */
3228 /* <...> is special for #include. */
3230 if (!kt
->angle_brackets
)
3232 while (*bp
&& *bp
!= '>') bp
++;
3236 if (*bp
== '\\' && bp
[1] == '\n')
3239 || ((cplusplus
|| objc
) && *bp
== '/')) {
3240 U_CHAR
*obp
= bp
- 1;
3242 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3244 /* No need to copy the command because of a comment at the end;
3245 just don't include the comment in the directive. */
3246 if (bp
== limit
|| *bp
== '\n') {
3250 /* Don't remove the comments if -traditional. */
3251 if (! keep_comments
)
3259 pedwarn ("%s in preprocessing directive",
3260 c
== '\f' ? "formfeed" : "vertical tab");
3264 --bp
; /* Point to the newline */
3272 resume_p
= ip
->bufp
;
3273 /* BP is the end of the directive.
3274 RESUME_P is the next interesting data after the directive.
3275 A comment may come between. */
3277 /* If a directive should be copied through, and -E was given,
3278 pass it through before removing comments. */
3279 if (!no_output
&& kt
->pass_thru
&& put_out_comments
) {
3282 /* Output directive name. */
3283 check_expand (op
, kt
->length
+ 2);
3284 /* Make sure # is at the start of a line */
3285 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3290 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3291 op
->bufp
+= kt
->length
;
3293 /* Output arguments. */
3295 check_expand (op
, len
);
3296 bcopy (buf
, op
->bufp
, len
);
3298 /* Take account of any (escaped) newlines just output. */
3300 if (buf
[len
] == '\n')
3303 already_output
= &junk
;
3304 } /* Don't we need a newline or #line? */
3307 register U_CHAR
*xp
= buf
;
3308 /* Need to copy entire command into temp buffer before dispatching */
3310 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
3314 /* Copy to the new buffer, deleting comments
3315 and backslash-newlines (and whitespace surrounding the latter). */
3318 register U_CHAR c
= *xp
++;
3323 abort (); /* A bare newline should never part of the line. */
3326 /* <...> is special for #include. */
3328 if (!kt
->angle_brackets
)
3330 while (xp
< bp
&& c
!= '>') {
3332 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
3343 if (cp
!= buf
&& is_space
[cp
[-1]]) {
3344 while (cp
!= buf
&& is_space
[cp
[-1]]) cp
--;
3346 SKIP_WHITE_SPACE (xp
);
3347 } else if (is_space
[*xp
]) {
3349 SKIP_WHITE_SPACE (xp
);
3359 register U_CHAR
*bp1
3360 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
, 0, 0, 0);
3374 || ((cplusplus
|| objc
) && *xp
== '/')) {
3376 /* If we already copied the command through,
3377 already_output != 0 prevents outputting comment now. */
3378 skip_to_end_of_comment (ip
, already_output
, 0);
3380 while (xp
!= ip
->bufp
)
3382 /* Delete or replace the slash. */
3383 else if (traditional
)
3392 /* Null-terminate the copy. */
3398 ip
->bufp
= resume_p
;
3400 /* Some directives should be written out for cc1 to process,
3401 just as if they were not defined. And sometimes we're copying
3402 definitions through. */
3404 if (!no_output
&& already_output
== 0
3406 || (kt
->type
== T_DEFINE
3407 && (dump_macros
== dump_names
3408 || dump_macros
== dump_definitions
)))) {
3411 /* Output directive name. */
3412 check_expand (op
, kt
->length
+ 1);
3414 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3415 op
->bufp
+= kt
->length
;
3417 if (kt
->pass_thru
|| dump_macros
== dump_definitions
) {
3418 /* Output arguments. */
3420 check_expand (op
, len
);
3421 bcopy (buf
, op
->bufp
, len
);
3424 } /* Don't we need a newline or #line? */
3426 /* Call the appropriate command handler. buf now points to
3427 either the appropriate place in the input buffer, or to
3428 the temp buffer if it was necessary to make one. cp
3429 points to the first char after the contents of the (possibly
3430 copied) command, in either case. */
3431 (*kt
->func
) (buf
, cp
, op
, kt
);
3432 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3438 /* It is deliberate that we don't warn about undefined directives.
3439 That is the responsibility of cc1. */
3446 static struct tm
*timebuf
;
3448 time_t t
= time (0);
3449 timebuf
= localtime (&t
);
3454 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3455 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3459 * expand things like __FILE__. Place the expansion into the output
3460 * buffer *without* rescanning.
3464 special_symbol (hp
, op
)
3471 FILE_BUF
*ip
= NULL
;
3474 int paren
= 0; /* For special `defined' keyword */
3476 if (pcp_outfile
&& pcp_inside_if
3477 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
3478 error ("Predefined macro `%s' used inside `#if' during precompilation",
3481 for (i
= indepth
; i
>= 0; i
--)
3482 if (instack
[i
].fname
!= NULL
) {
3487 error ("cccp error: not in any file?!");
3488 return; /* the show must go on */
3496 if (hp
->type
== T_FILE
)
3497 string
= ip
->nominal_fname
;
3499 string
= instack
[0].nominal_fname
;
3503 buf
= (char *) alloca (3 + strlen (string
));
3504 sprintf (buf
, "\"%s\"", string
);
3512 case T_INCLUDE_LEVEL
:
3514 for (i
= indepth
; i
>= 0; i
--)
3515 if (instack
[i
].fname
!= NULL
)
3518 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
3519 sprintf (buf
, "%d", true_indepth
- 1);
3523 buf
= (char *) alloca (3 + strlen (version_string
));
3524 sprintf (buf
, "\"%s\"", version_string
);
3528 buf
= (char *) alloca (3 + strlen (SIZE_TYPE
));
3529 sprintf (buf
, "%s", SIZE_TYPE
);
3532 case T_PTRDIFF_TYPE
:
3533 buf
= (char *) alloca (3 + strlen (PTRDIFF_TYPE
));
3534 sprintf (buf
, "%s", PTRDIFF_TYPE
);
3538 buf
= (char *) alloca (3 + strlen (WCHAR_TYPE
));
3539 sprintf (buf
, "%s", WCHAR_TYPE
);
3543 buf
= (char *) alloca (4 * sizeof (int));
3544 sprintf (buf
, "%d", hp
->value
.ival
);
3545 if (pcp_inside_if
&& pcp_outfile
)
3546 /* Output a precondition for this macro use */
3547 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
3551 buf
= (char *) alloca (10);
3552 sprintf (buf
, "%d", ip
->lineno
);
3557 buf
= (char *) alloca (20);
3558 timebuf
= timestamp ();
3559 if (hp
->type
== T_DATE
)
3560 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
3561 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
3563 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
3567 case T_SPEC_DEFINED
:
3568 buf
= " 0 "; /* Assume symbol is not defined */
3569 ip
= &instack
[indepth
];
3570 SKIP_WHITE_SPACE (ip
->bufp
);
3571 if (*ip
->bufp
== '(') {
3573 ip
->bufp
++; /* Skip over the paren */
3574 SKIP_WHITE_SPACE (ip
->bufp
);
3577 if (!is_idstart
[*ip
->bufp
])
3579 if (hp
= lookup (ip
->bufp
, -1, -1)) {
3580 if (pcp_outfile
&& pcp_inside_if
3581 && hp
->value
.defn
->predefined
)
3582 /* Output a precondition for this macro use. */
3583 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
3587 if (pcp_outfile
&& pcp_inside_if
) {
3588 /* Output a precondition for this macro use */
3589 U_CHAR
*cp
= ip
->bufp
;
3590 fprintf (pcp_outfile
, "#undef ");
3591 while (is_idchar
[*cp
]) /* Ick! */
3592 fputc (*cp
++, pcp_outfile
);
3593 putc ('\n', pcp_outfile
);
3595 while (is_idchar
[*ip
->bufp
])
3597 SKIP_WHITE_SPACE (ip
->bufp
);
3599 if (*ip
->bufp
!= ')')
3607 error ("`defined' without an identifier");
3611 error ("cccp error: invalid special hash type"); /* time for gdb */
3615 check_expand (op
, len
);
3616 bcopy (buf
, op
->bufp
, len
);
3623 /* Routines to handle #directives */
3625 /* Handle #include and #import.
3626 This function expects to see "fname" or <fname> on the input. */
3629 do_include (buf
, limit
, op
, keyword
)
3630 U_CHAR
*buf
, *limit
;
3632 struct directive
*keyword
;
3634 int importing
= (keyword
->type
== T_IMPORT
);
3635 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3636 static int import_warning
= 0;
3637 char *fname
; /* Dynamically allocated fname buffer */
3640 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3642 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
3643 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3644 struct file_name_list
*searchptr
;
3647 int f
; /* file number */
3649 int retried
= 0; /* Have already tried macro
3650 expanding the include line*/
3651 FILE_BUF trybuf
; /* It got expanded into here */
3652 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
3657 f
= -1; /* JF we iz paranoid! */
3659 if (importing
&& warn_import
3660 && !instack
[indepth
].system_header_p
&& !import_warning
) {
3662 warning ("using `#import' is not recommended");
3663 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3664 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3665 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3666 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3667 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3668 fprintf (stderr
, " ... <real contents of file> ...\n");
3669 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3670 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3671 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3672 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3678 SKIP_WHITE_SPACE (fbeg
);
3679 /* Discard trailing whitespace so we can easily see
3680 if we have parsed all the significant chars we were given. */
3681 while (limit
!= fbeg
&& is_hor_space
[limit
[-1]]) limit
--;
3686 while (fend
!= limit
&& *fend
!= '\"')
3688 if (*fend
== '\"' && fend
+ 1 == limit
) {
3691 /* We have "filename". Figure out directory this source
3692 file is coming from and put it on the front of the list. */
3694 /* If -I- was specified, don't search current dir, only spec'd ones. */
3695 if (ignore_srcdir
) break;
3697 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
3702 if ((nam
= fp
->nominal_fname
) != NULL
) {
3703 /* Found a named file. Figure out dir of the file,
3704 and put it in front of the search list. */
3705 dsp
[0].next
= search_start
;
3708 ep
= rindex (nam
, '/');
3710 ep
= rindex (nam
, ']');
3711 if (ep
== NULL
) ep
= rindex (nam
, '>');
3712 if (ep
== NULL
) ep
= rindex (nam
, ':');
3713 if (ep
!= NULL
) ep
++;
3717 dsp
[0].fname
= (char *) alloca (n
+ 1);
3718 strncpy (dsp
[0].fname
, nam
, n
);
3719 dsp
[0].fname
[n
] = '\0';
3720 if (n
> max_include_len
) max_include_len
= n
;
3722 dsp
[0].fname
= 0; /* Current directory */
3733 while (fend
!= limit
&& *fend
!= '>') fend
++;
3734 if (*fend
== '>' && fend
+ 1 == limit
) {
3735 system_header_p
= 1;
3736 /* If -I-, start with the first -I dir after the -I-. */
3737 if (first_bracket_include
)
3738 search_start
= first_bracket_include
;
3747 error ("`#import' expects \"fname\" or <fname>");
3749 error ("`#include' expects \"fname\" or <fname>");
3752 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
3753 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
3754 bcopy (trybuf
.buf
, buf
, trybuf
.bufp
- trybuf
.buf
);
3755 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
3762 /* For #include_next, skip in the search path
3763 past the dir in which the containing file was found. */
3766 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
3767 if (fp
->fname
!= NULL
) {
3768 /* fp->dir is null if the containing file was specified
3769 with an absolute file name. In that case, don't skip anything. */
3771 search_start
= fp
->dir
->next
;
3777 /* Allocate this permanently, because it gets stored in the definitions
3779 fname
= (char *) xmalloc (max_include_len
+ flen
+ 2);
3780 /* + 2 above for slash and terminating null. */
3782 /* If specified file name is absolute, just open it. */
3785 strncpy (fname
, fbeg
, flen
);
3787 if (lookup_include (fname
))
3790 f
= lookup_import (fname
);
3792 f
= open (fname
, O_RDONLY
, 0666);
3794 return 0; /* Already included this file */
3796 /* Search directory path, trying to open the file.
3797 Copy each filename tried into FNAME. */
3799 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3800 if (searchptr
->fname
) {
3801 /* The empty string in a search path is ignored.
3802 This makes it possible to turn off entirely
3803 a standard piece of the list. */
3804 if (searchptr
->fname
[0] == 0)
3806 strcpy (fname
, searchptr
->fname
);
3807 strcat (fname
, "/");
3808 fname
[strlen (fname
) + flen
] = 0;
3812 strncat (fname
, fbeg
, flen
);
3814 /* Change this 1/2 Unix 1/2 VMS file specification into a
3815 full VMS file specification */
3816 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3817 /* Fix up the filename */
3818 hack_vms_include_specification (fname
);
3820 /* This is a normal VMS filespec, so use it unchanged. */
3821 strncpy (fname
, fbeg
, flen
);
3826 f
= lookup_import (fname
);
3828 f
= open (fname
, O_RDONLY
, 0666);
3830 return 0; /* Already included this file */
3831 if (lookup_include (fname
)) {
3841 /* A file that was not found. */
3843 strncpy (fname
, fbeg
, flen
);
3845 error_from_errno (fname
);
3847 /* For -M, add this file to the dependencies. */
3848 if (print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
3849 /* Break the line before this. */
3850 deps_output ("", 0);
3852 /* If it was requested as a system header file,
3853 then assume it belongs in the first place to look for such. */
3854 if (system_header_p
) {
3855 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3856 if (searchptr
->fname
) {
3857 if (searchptr
->fname
[0] == 0)
3859 deps_output (searchptr
->fname
, 0);
3860 deps_output ("/", 0);
3865 /* Otherwise, omit the directory, as if the file existed
3866 in the directory with the source. */
3867 deps_output (fbeg
, flen
);
3868 deps_output (" ", 0);
3873 /* Check to see if this include file is a once-only include file.
3876 struct file_name_list
* ptr
;
3878 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3879 if (!strcmp (ptr
->fname
, fname
)) {
3881 return 0; /* This file was once'd. */
3885 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
3886 if (!strcmp (ptr
->fname
, fname
))
3887 break; /* This file was included before. */
3891 /* This is the first time for this file. */
3892 /* Add it to list of files included. */
3894 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3895 ptr
->control_macro
= 0;
3896 ptr
->next
= all_include_files
;
3897 all_include_files
= ptr
;
3898 ptr
->fname
= savestring (fname
);
3900 /* For -M, add this file to the dependencies. */
3901 if (print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
3902 deps_output ("", 0);
3903 deps_output (fname
, 0);
3904 deps_output (" ", 0);
3908 /* Handle -H option. */
3909 if (print_include_names
)
3910 fprintf (stderr
, "%s\n", fname
);
3912 if (system_header_p
)
3913 system_include_depth
++;
3915 /* Actually process the file. */
3916 add_import (f
, fname
); /* Record file on "seen" list for #import. */
3918 pcftry
= (char *) alloca (strlen (fname
) + 30);
3926 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3928 pcf
= open (pcftry
, O_RDONLY
, 0666);
3934 if (bcmp (&stat_f
.st_ino
, &s
.st_ino
, sizeof (s
.st_ino
))
3935 || stat_f
.st_dev
!= s
.st_dev
)
3937 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3938 /* Don't need it any more. */
3943 /* Don't need it at all. */
3948 } while (pcf
!= -1 && !pcfbuf
);
3950 /* Actually process the file */
3952 pcfname
= xmalloc (strlen (pcftry
) + 1);
3953 strcpy (pcfname
, pcftry
);
3954 pcfinclude (pcfbuf
, pcfbuflimit
, fname
, op
);
3957 finclude (f
, fname
, op
, system_header_p
, searchptr
);
3959 if (system_header_p
)
3960 system_include_depth
--;
3965 /* Return nonzero if there is no need to include file NAME
3966 because it has already been included and it contains a conditional
3967 to make a repeated include do nothing. */
3970 lookup_include (name
)
3973 struct file_name_list
*l
= all_include_files
;
3974 for (; l
; l
= l
->next
)
3975 if (! strcmp (name
, l
->fname
)
3977 && lookup (l
->control_macro
, -1, -1))
3982 /* Process the contents of include file FNAME, already open on descriptor F,
3984 SYSTEM_HEADER_P is 1 if this file was specified using <...>.
3985 DIRPTR is the link in the dir path through which this file was found,
3986 or 0 if the file name was absolute. */
3989 finclude (f
, fname
, op
, system_header_p
, dirptr
)
3993 int system_header_p
;
3994 struct file_name_list
*dirptr
;
3999 FILE_BUF
*fp
; /* For input stack frame */
4000 int missing_newline
= 0;
4002 CHECK_DEPTH (return;);
4004 if (file_size_and_mode (f
, &st_mode
, &st_size
) < 0)
4005 goto nope
; /* Impossible? */
4007 fp
= &instack
[indepth
+ 1];
4008 bzero (fp
, sizeof (FILE_BUF
));
4009 fp
->nominal_fname
= fp
->fname
= fname
;
4012 fp
->if_stack
= if_stack
;
4013 fp
->system_header_p
= system_header_p
;
4016 if (S_ISREG (st_mode
)) {
4017 fp
->buf
= (U_CHAR
*) alloca (st_size
+ 2);
4020 /* Read the file contents, knowing that st_size is an upper bound
4021 on the number of bytes we can read. */
4022 while (st_size
> 0) {
4023 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
4033 /* Cannot count its file size before reading.
4034 First read the entire file into heap and
4035 copy them into buffer on stack. */
4042 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
4046 i
= read (f
, bufp
, bsize
- st_size
);
4048 goto nope
; /* error! */
4050 break; /* End of file */
4053 if (bsize
== st_size
) { /* Buffer is full! */
4055 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
4056 bufp
= basep
+ st_size
; /* May have moved */
4059 fp
->buf
= (U_CHAR
*) alloca (st_size
+ 2);
4061 bcopy (basep
, fp
->buf
, st_size
);
4062 fp
->length
= st_size
;
4066 /* Close descriptor now, so nesting does not use lots of descriptors. */
4072 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
4073 /* Backslash-newline at end is not good enough. */
4074 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
4075 fp
->buf
[fp
->length
++] = '\n';
4076 missing_newline
= 1;
4078 fp
->buf
[fp
->length
] = '\0';
4081 input_file_stack_tick
++;
4083 output_line_command (fp
, op
, 0, enter_file
);
4086 if (pedantic
&& missing_newline
)
4087 pedwarn ("file does not end in newline");
4090 input_file_stack_tick
++;
4091 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
4096 perror_with_name (fname
);
4100 /* Record that inclusion of the file named FILE
4101 should be controlled by the macro named MACRO_NAME.
4102 This means that trying to include the file again
4103 will do something if that macro is defined. */
4106 record_control_macro (file
, macro_name
)
4110 struct file_name_list
*new;
4112 for (new = all_include_files
; new; new = new->next
) {
4113 if (!strcmp (new->fname
, file
)) {
4114 new->control_macro
= macro_name
;
4119 /* If the file is not in all_include_files, something's wrong. */
4123 /* Maintain and search list of included files, for #import. */
4125 #define IMPORT_HASH_SIZE 31
4127 struct import_file
{
4131 struct import_file
*next
;
4134 /* Hash table of files already included with #include or #import. */
4136 static struct import_file
*import_hash_table
[IMPORT_HASH_SIZE
];
4138 /* Hash a file name for import_hash_table. */
4146 while (*f
) val
+= *f
++;
4147 return (val
%IMPORT_HASH_SIZE
);
4150 /* Search for file FILENAME in import_hash_table.
4151 Return -2 if found, either a matching name or a matching inode.
4152 Otherwise, open the file and return a file descriptor if successful
4153 or -1 if unsuccessful. */
4156 lookup_import (filename
)
4159 struct import_file
*i
;
4165 hashval
= import_hash (filename
);
4167 /* Attempt to find file in list of already included files */
4168 i
= import_hash_table
[hashval
];
4171 if (!strcmp (filename
, i
->name
))
4172 return -2; /* return found */
4175 /* Open it and try a match on inode/dev */
4176 fd
= open (filename
, O_RDONLY
, 0666);
4180 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
4181 i
= import_hash_table
[h
];
4183 /* Compare the inode and the device.
4184 Supposedly on some systems the inode is not a scalar. */
4185 if (!bcmp (&i
->inode
, &sb
.st_ino
, sizeof (sb
.st_ino
))
4186 && i
->dev
== sb
.st_dev
) {
4188 return -2; /* return found */
4193 return fd
; /* Not found, return open file */
4196 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
4199 add_import (fd
, fname
)
4203 struct import_file
*i
;
4207 hashval
= import_hash (fname
);
4209 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
4210 i
->name
= (char *)xmalloc (strlen (fname
)+1);
4211 strcpy (i
->name
, fname
);
4212 bcopy (&sb
.st_ino
, &i
->inode
, sizeof (sb
.st_ino
));
4214 i
->next
= import_hash_table
[hashval
];
4215 import_hash_table
[hashval
] = i
;
4218 /* Load the specified precompiled header into core, and verify its
4219 preconditions. PCF indicates the file descriptor to read, which must
4220 be a regular file. FNAME indicates the file name of the original
4221 header. *LIMIT will be set to an address one past the end of the file.
4222 If the preconditions of the file are not satisfied, the buffer is
4223 freed and we return 0. If the preconditions are satisfied, return
4224 the address of the buffer following the preconditions. The buffer, in
4225 this case, should never be freed because various pieces of it will
4226 be referred to until all precompiled strings are output at the end of
4230 check_precompiled (pcf
, fname
, limit
)
4246 if (file_size_and_mode (pcf
, &st_mode
, &st_size
) < 0)
4249 if (S_ISREG (st_mode
))
4251 buf
= xmalloc (st_size
+ 2);
4254 i
= read (pcf
, buf
+ length
, st_size
);
4266 if (length
> 0 && buf
[length
-1] != '\n')
4267 buf
[length
++] = '\n';
4270 *limit
= buf
+ length
;
4272 /* File is in core. Check the preconditions. */
4273 if (!check_preconditions (buf
))
4275 for (cp
= buf
; *cp
; cp
++)
4278 fprintf (stderr
, "Using preinclude %s\n", fname
);
4284 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
4290 /* PREC (null terminated) points to the preconditions of a
4291 precompiled header. These are a series of #define and #undef
4292 lines which must match the current contents of the hash
4295 check_preconditions (prec
)
4302 lineend
= (char *) index (prec
, '\n');
4304 if (*prec
++ != '#') {
4305 error ("Bad format encountered while reading precompiled file");
4308 if (!strncmp (prec
, "define", 6)) {
4312 mdef
= create_definition (prec
, lineend
, 0);
4317 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
4318 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
4319 || (hp
->type
== T_MACRO
4320 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
4321 && (mdef
.defn
->length
!= 2
4322 || mdef
.defn
->expansion
[0] != '\n'
4323 || mdef
.defn
->expansion
[1] != ' ')))
4325 } else if (!strncmp (prec
, "undef", 5)) {
4330 while (is_hor_space
[*prec
])
4333 while (is_idchar
[*prec
])
4337 if (lookup (name
, len
, -1))
4340 error ("Bad format encountered while reading precompiled file");
4345 /* They all passed successfully */
4349 /* Process the main body of a precompiled file. BUF points to the
4350 string section of the file, following the preconditions. LIMIT is one
4351 character past the end. NAME is the name of the file being read
4352 in. OP is the main output buffer */
4354 pcfinclude (buf
, limit
, name
, op
)
4355 U_CHAR
*buf
, *limit
, *name
;
4362 /* First in the file comes 4 bytes indicating the number of strings, */
4363 /* in network byte order. (MSB first). */
4365 nstrings
= (nstrings
<< 8) | *cp
++;
4366 nstrings
= (nstrings
<< 8) | *cp
++;
4367 nstrings
= (nstrings
<< 8) | *cp
++;
4369 /* Looping over each string... */
4370 while (nstrings
--) {
4371 U_CHAR
*string_start
;
4372 U_CHAR
*endofthiskey
;
4376 /* Each string starts with a STRINGDEF structure (str), followed */
4377 /* by the text of the string (string_start) */
4379 /* First skip to a longword boundary */
4381 cp
+= 4 - ((int)cp
& 3);
4383 /* Now get the string. */
4384 str
= (STRINGDEF
*) cp
;
4385 string_start
= cp
+= sizeof (STRINGDEF
);
4387 for (; *cp
; cp
++) /* skip the string */
4390 /* We need to macro expand the string here to ensure that the
4391 proper definition environment is in place. If it were only
4392 expanded when we find out it is needed, macros necessary for
4393 its proper expansion might have had their definitions changed. */
4394 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
4395 /* Lineno is already set in the precompiled file */
4396 str
->contents
= tmpbuf
.buf
;
4397 str
->len
= tmpbuf
.length
;
4399 str
->filename
= name
;
4400 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
4403 *stringlist_tailp
= str
;
4404 stringlist_tailp
= &str
->chain
;
4406 /* Next comes a fourbyte number indicating the number of keys */
4407 /* for this string. */
4409 nkeys
= (nkeys
<< 8) | *cp
++;
4410 nkeys
= (nkeys
<< 8) | *cp
++;
4411 nkeys
= (nkeys
<< 8) | *cp
++;
4413 /* If this number is -1, then the string is mandatory. */
4417 /* Otherwise, for each key, */
4418 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
4419 KEYDEF
*kp
= (KEYDEF
*) cp
;
4422 /* It starts with a KEYDEF structure */
4423 cp
+= sizeof (KEYDEF
);
4425 /* Find the end of the key. At the end of this for loop we
4426 advance CP to the start of the next key using this variable. */
4427 endofthiskey
= cp
+ strlen (cp
);
4430 /* Expand the key, and enter it into the hash table. */
4431 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
4432 tmpbuf
.bufp
= tmpbuf
.buf
;
4434 while (is_hor_space
[*tmpbuf
.bufp
])
4436 if (!is_idstart
[*tmpbuf
.bufp
]
4437 || tmpbuf
.bufp
== tmpbuf
.buf
+ tmpbuf
.length
) {
4442 hp
= lookup (tmpbuf
.bufp
, -1, -1);
4445 install (tmpbuf
.bufp
, -1, T_PCSTRING
, (int) kp
, -1);
4447 else if (hp
->type
== T_PCSTRING
) {
4448 kp
->chain
= hp
->value
.keydef
;
4449 hp
->value
.keydef
= kp
;
4455 /* This output_line_command serves to switch us back to the current
4456 input file in case some of these strings get output (which will
4457 result in line commands for the header file being output). */
4458 output_line_command (&instack
[indepth
], op
, 0, enter_file
);
4461 /* Called from rescan when it hits a key for strings. Mark them all */
4462 /* used and clean up. */
4469 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
4470 kp
->str
->writeflag
= 1;
4474 /* Write the output, interspersing precompiled strings in their */
4475 /* appropriate places. */
4479 STRINGDEF
*next_string
;
4480 U_CHAR
*cur_buf_loc
;
4481 int line_command_len
= 80;
4482 char *line_command
= xmalloc (line_command_len
);
4485 /* In each run through the loop, either cur_buf_loc == */
4486 /* next_string_loc, in which case we print a series of strings, or */
4487 /* it is less than next_string_loc, in which case we write some of */
4489 cur_buf_loc
= outbuf
.buf
;
4490 next_string
= stringlist
;
4492 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
4494 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
4495 if (next_string
->writeflag
) {
4496 len
= strlen (next_string
->filename
);
4497 if (len
> line_command_len
)
4498 line_command
= xrealloc (line_command
,
4499 line_command_len
*= 2);
4500 sprintf (line_command
, "\n# %d \"%s\"\n",
4501 next_string
->lineno
, next_string
->filename
);
4502 write (fileno (stdout
), line_command
,
4503 strlen (line_command
));
4504 write (fileno (stdout
),
4505 next_string
->contents
, next_string
->len
);
4507 next_string
= next_string
->chain
;
4511 ? (next_string
->output_mark
4512 - (cur_buf_loc
- outbuf
.buf
))
4513 : outbuf
.bufp
- cur_buf_loc
);
4515 write (fileno (stdout
), cur_buf_loc
, len
);
4521 /* Pass a directive through to the output file.
4522 BUF points to the contents of the directive, as a contiguous string.
4523 LIMIT points to the first character past the end of the directive.
4524 KEYWORD is the keyword-table entry for the directive. */
4527 pass_thru_directive (buf
, limit
, op
, keyword
)
4528 U_CHAR
*buf
, *limit
;
4530 struct directive
*keyword
;
4532 register unsigned keyword_length
= keyword
->length
;
4534 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
4536 bcopy (keyword
->name
, op
->bufp
, keyword_length
);
4537 op
->bufp
+= keyword_length
;
4538 if (limit
!= buf
&& buf
[0] != ' ')
4540 bcopy (buf
, op
->bufp
, limit
- buf
);
4541 op
->bufp
+= (limit
- buf
);
4544 /* Count the line we have just made in the output,
4545 to get in sync properly. */
4550 /* The arglist structure is built by do_define to tell
4551 collect_definition where the argument names begin. That
4552 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
4553 would contain pointers to the strings x, y, and z.
4554 Collect_definition would then build a DEFINITION node,
4555 with reflist nodes pointing to the places x, y, and z had
4556 appeared. So the arglist is just convenience data passed
4557 between these two routines. It is not kept around after
4558 the current #define has been processed and entered into the
4562 struct arglist
*next
;
4569 /* Create a DEFINITION node from a #define directive. Arguments are
4570 as for do_define. */
4572 create_definition (buf
, limit
, op
)
4573 U_CHAR
*buf
, *limit
;
4576 U_CHAR
*bp
; /* temp ptr into input buffer */
4577 U_CHAR
*symname
; /* remember where symbol name starts */
4578 int sym_length
; /* and how long it is */
4579 int line
= instack
[indepth
].lineno
;
4580 char *file
= instack
[indepth
].nominal_fname
;
4584 int arglengths
= 0; /* Accumulate lengths of arg names
4585 plus number of args. */
4590 while (is_hor_space
[*bp
])
4593 symname
= bp
; /* remember where it starts */
4594 sym_length
= check_macro_name (bp
, "macro");
4597 /* Lossage will occur if identifiers or control keywords are broken
4598 across lines using backslash. This is not the right place to take
4602 struct arglist
*arg_ptrs
= NULL
;
4605 bp
++; /* skip '(' */
4606 SKIP_WHITE_SPACE (bp
);
4608 /* Loop over macro argument names. */
4609 while (*bp
!= ')') {
4610 struct arglist
*temp
;
4612 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
4614 temp
->next
= arg_ptrs
;
4615 temp
->argno
= argno
++;
4616 temp
->rest_args
= 0;
4620 pedwarn ("another parameter follows `%s'",
4623 if (!is_idstart
[*bp
])
4624 pedwarn ("invalid character in macro parameter name");
4626 /* Find the end of the arg name. */
4627 while (is_idchar
[*bp
]) {
4629 /* do we have a "special" rest-args extension here? */
4630 if (limit
- bp
> REST_EXTENSION_LENGTH
&&
4631 strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
4633 temp
->rest_args
= 1;
4637 temp
->length
= bp
- temp
->name
;
4639 bp
+= REST_EXTENSION_LENGTH
;
4640 arglengths
+= temp
->length
+ 2;
4641 SKIP_WHITE_SPACE (bp
);
4642 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
4643 error ("badly punctuated parameter list in `#define'");
4648 SKIP_WHITE_SPACE (bp
);
4651 error ("unterminated parameter list in `#define'");
4655 struct arglist
*otemp
;
4657 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
4658 if (temp
->length
== otemp
->length
&&
4659 strncmp(temp
->name
, otemp
->name
, temp
->length
) == 0) {
4662 name
= (U_CHAR
*) alloca(temp
->length
+ 1);
4663 (void) strncpy(name
, temp
->name
, temp
->length
);
4664 name
[temp
->length
] = '\0';
4665 error ("duplicate argument name `%s' in `#define'", name
);
4671 ++bp
; /* skip paren */
4672 /* Skip exactly one space or tab if any. */
4673 if (bp
< limit
&& (*bp
== ' ' || *bp
== '\t')) ++bp
;
4674 /* now everything from bp before limit is the definition. */
4675 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
4676 defn
->rest_args
= rest_args
;
4678 /* Now set defn->args.argnames to the result of concatenating
4679 the argument names in reverse order
4680 with comma-space between them. */
4681 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
4683 struct arglist
*temp
;
4685 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
4686 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
4688 if (temp
->next
!= 0) {
4689 defn
->args
.argnames
[i
++] = ',';
4690 defn
->args
.argnames
[i
++] = ' ';
4693 defn
->args
.argnames
[i
] = 0;
4696 /* simple expansion or empty definition; gobble it */
4697 if (is_hor_space
[*bp
])
4698 ++bp
; /* skip exactly one blank/tab char */
4699 /* now everything from bp before limit is the definition. */
4700 defn
= collect_expansion (bp
, limit
, -1, 0);
4701 defn
->args
.argnames
= (U_CHAR
*) "";
4707 /* OP is null if this is a predefinition */
4708 defn
->predefined
= !op
;
4710 mdef
.symnam
= symname
;
4711 mdef
.symlen
= sym_length
;
4720 /* Process a #define command.
4721 BUF points to the contents of the #define command, as a contiguous string.
4722 LIMIT points to the first character past the end of the definition.
4723 KEYWORD is the keyword-table entry for #define. */
4726 do_define (buf
, limit
, op
, keyword
)
4727 U_CHAR
*buf
, *limit
;
4729 struct directive
*keyword
;
4734 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
4735 if (pcp_outfile
&& op
)
4736 pass_thru_directive (buf
, limit
, op
, keyword
);
4738 mdef
= create_definition (buf
, limit
, op
);
4742 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
4746 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
4748 /* Redefining a precompiled key is ok. */
4749 if (hp
->type
== T_PCSTRING
)
4751 /* Redefining a macro is ok if the definitions are the same. */
4752 else if (hp
->type
== T_MACRO
)
4753 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
4754 /* Redefining a constant is ok with -D. */
4755 else if (hp
->type
== T_CONST
)
4756 ok
= ! done_initializing
;
4757 /* Print the warning if it's not ok. */
4759 U_CHAR
*msg
; /* what pain... */
4761 /* If we are passing through #define and #undef directives, do
4762 that for this re-definition now. */
4763 if (debug_output
&& op
)
4764 pass_thru_directive (buf
, limit
, op
, keyword
);
4766 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
4768 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
4769 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
4771 if (hp
->type
== T_MACRO
)
4772 pedwarn_with_file_and_line (hp
->value
.defn
->file
, hp
->value
.defn
->line
,
4773 "this is the location of the previous definition");
4775 /* Replace the old definition. */
4777 hp
->value
.defn
= mdef
.defn
;
4779 /* If we are passing through #define and #undef directives, do
4780 that for this new definition now. */
4781 if (debug_output
&& op
)
4782 pass_thru_directive (buf
, limit
, op
, keyword
);
4783 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, mdef
.defn
, hashcode
);
4794 /* Check a purported macro name SYMNAME, and yield its length.
4795 USAGE is the kind of name this is intended for. */
4798 check_macro_name (symname
, usage
)
4805 for (p
= symname
; is_idchar
[*p
]; p
++)
4807 sym_length
= p
- symname
;
4808 if (sym_length
== 0)
4809 error ("invalid %s name", usage
);
4810 else if (!is_idstart
[*symname
]) {
4811 U_CHAR
*msg
; /* what pain... */
4812 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
4813 bcopy (symname
, msg
, sym_length
);
4814 msg
[sym_length
] = 0;
4815 error ("invalid %s name `%s'", usage
, msg
);
4817 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
4818 error ("invalid %s name `defined'", usage
);
4824 * return zero if two DEFINITIONs are isomorphic
4827 compare_defs (d1
, d2
)
4828 DEFINITION
*d1
, *d2
;
4830 register struct reflist
*a1
, *a2
;
4831 register U_CHAR
*p1
= d1
->expansion
;
4832 register U_CHAR
*p2
= d2
->expansion
;
4835 if (d1
->nargs
!= d2
->nargs
)
4837 if (strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
4839 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
4840 a1
= a1
->next
, a2
= a2
->next
) {
4841 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
4842 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
4843 || a1
->argno
!= a2
->argno
4844 || a1
->stringify
!= a2
->stringify
4845 || a1
->raw_before
!= a2
->raw_before
4846 || a1
->raw_after
!= a2
->raw_after
)
4854 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
4855 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
4860 /* Return 1 if two parts of two macro definitions are effectively different.
4861 One of the parts starts at BEG1 and has LEN1 chars;
4862 the other has LEN2 chars at BEG2.
4863 Any sequence of whitespace matches any other sequence of whitespace.
4864 FIRST means these parts are the first of a macro definition;
4865 so ignore leading whitespace entirely.
4866 LAST means these parts are the last of a macro definition;
4867 so ignore trailing whitespace entirely. */
4870 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
4872 U_CHAR
*beg1
, *beg2
;
4876 register U_CHAR
*end1
= beg1
+ len1
;
4877 register U_CHAR
*end2
= beg2
+ len2
;
4879 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
4880 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
4883 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
4884 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
4886 while (beg1
!= end1
&& beg2
!= end2
) {
4887 if (is_space
[*beg1
] && is_space
[*beg2
]) {
4888 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
4889 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
4890 } else if (*beg1
== *beg2
) {
4894 return (beg1
!= end1
) || (beg2
!= end2
);
4897 /* Read a replacement list for a macro with parameters.
4898 Build the DEFINITION structure.
4899 Reads characters of text starting at BUF until END.
4900 ARGLIST specifies the formal parameters to look for
4901 in the text of the definition; NARGS is the number of args
4902 in that list, or -1 for a macro name that wants no argument list.
4903 MACRONAME is the macro name itself (so we can avoid recursive expansion)
4904 and NAMELEN is its length in characters.
4906 Note that comments and backslash-newlines have already been deleted
4907 from the argument. */
4909 /* Leading and trailing Space, Tab, etc. are converted to markers
4910 Newline Space, Newline Tab, etc.
4911 Newline Space makes a space in the final output
4912 but is discarded if stringified. (Newline Tab is similar but
4913 makes a Tab instead.)
4915 If there is no trailing whitespace, a Newline Space is added at the end
4916 to prevent concatenation that would be contrary to the standard. */
4919 collect_expansion (buf
, end
, nargs
, arglist
)
4922 struct arglist
*arglist
;
4925 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
4926 struct reflist
*endpat
= NULL
;
4927 /* Pointer to first nonspace after last ## seen. */
4929 /* Pointer to first nonspace after last single-# seen. */
4930 U_CHAR
*stringify
= 0;
4932 int expected_delimiter
= '\0';
4934 /* Scan thru the replacement list, ignoring comments and quoted
4935 strings, picking up on the macro calls. It does a linear search
4936 thru the arg list on every potential symbol. Profiling might say
4937 that something smarter should happen. */
4942 /* Find the beginning of the trailing whitespace. */
4943 /* Find end of leading whitespace. */
4946 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
4947 while (p
< limit
&& is_space
[*p
]) p
++;
4949 /* Allocate space for the text in the macro definition.
4950 Leading and trailing whitespace chars need 2 bytes each.
4951 Each other input char may or may not need 1 byte,
4952 so this is an upper bound.
4953 The extra 2 are for invented trailing newline-marker and final null. */
4954 maxsize
= (sizeof (DEFINITION
)
4955 + 2 * (end
- limit
) + 2 * (p
- buf
)
4957 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
4959 defn
->nargs
= nargs
;
4960 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
4965 /* Convert leading whitespace to Newline-markers. */
4966 while (p
< limit
&& is_space
[*p
]) {
4971 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
4972 error ("`##' at start of macro definition");
4976 /* Process the main body of the definition. */
4978 int skipped_arg
= 0;
4979 register U_CHAR c
= *p
++;
4987 if (expected_delimiter
!= '\0') {
4988 if (c
== expected_delimiter
)
4989 expected_delimiter
= '\0';
4991 expected_delimiter
= c
;
4994 /* Special hack: if a \# is written in the #define
4995 include a # in the definition. This is useless for C code
4996 but useful for preprocessing other things. */
4999 /* \# quotes a # even outside of strings. */
5000 if (p
< limit
&& *p
== '#' && !expected_delimiter
) {
5003 } else if (p
< limit
&& expected_delimiter
) {
5004 /* In a string, backslash goes through
5005 and makes next char ordinary. */
5011 /* # is ordinary inside a string. */
5012 if (expected_delimiter
)
5014 if (p
< limit
&& *p
== '#') {
5015 /* ##: concatenate preceding and following tokens. */
5016 /* Take out the first #, discard preceding whitespace. */
5018 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
5020 /* Skip the second #. */
5022 /* Discard following whitespace. */
5023 SKIP_WHITE_SPACE (p
);
5026 error ("`##' at end of macro definition");
5028 /* Single #: stringify following argument ref.
5029 Don't leave the # in the expansion. */
5031 SKIP_WHITE_SPACE (p
);
5032 if (p
== limit
|| ! is_idstart
[*p
] || nargs
<= 0)
5033 error ("`#' operator is not followed by a macro argument name");
5040 /* In -traditional mode, recognize arguments inside strings and
5041 and character constants, and ignore special properties of #.
5042 Arguments inside strings are considered "stringified", but no
5043 extra quote marks are supplied. */
5047 if (expected_delimiter
!= '\0') {
5048 if (c
== expected_delimiter
)
5049 expected_delimiter
= '\0';
5051 expected_delimiter
= c
;
5055 /* Backslash quotes delimiters and itself, but not macro args. */
5056 if (expected_delimiter
!= 0 && p
< limit
5057 && (*p
== expected_delimiter
|| *p
== '\\')) {
5064 if (expected_delimiter
!= '\0') /* No comments inside strings. */
5067 /* If we find a comment that wasn't removed by handle_directive,
5068 this must be -traditional. So replace the comment with
5072 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
5075 /* Mark this as a concatenation-point, as if it had been ##. */
5083 /* Handle the start of a symbol. */
5084 if (is_idchar
[c
] && nargs
> 0) {
5085 U_CHAR
*id_beg
= p
- 1;
5089 while (p
!= limit
&& is_idchar
[*p
]) p
++;
5090 id_len
= p
- id_beg
;
5092 if (is_idstart
[c
]) {
5093 register struct arglist
*arg
;
5095 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
5096 struct reflist
*tpat
;
5098 if (arg
->name
[0] == c
5099 && arg
->length
== id_len
5100 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
5101 if (expected_delimiter
&& warn_stringify
) {
5103 warning ("macro argument `%.*s' is stringified.",
5106 warning ("macro arg `%.*s' would be stringified with -traditional.",
5110 /* If ANSI, don't actually substitute inside a string. */
5111 if (!traditional
&& expected_delimiter
)
5113 /* make a pat node for this arg and append it to the end of
5115 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
5117 tpat
->raw_before
= concat
== id_beg
;
5118 tpat
->raw_after
= 0;
5119 tpat
->rest_args
= arg
->rest_args
;
5120 tpat
->stringify
= (traditional
? expected_delimiter
!= '\0'
5121 : stringify
== id_beg
);
5124 defn
->pattern
= tpat
;
5126 endpat
->next
= tpat
;
5129 tpat
->argno
= arg
->argno
;
5130 tpat
->nchars
= exp_p
- lastp
;
5132 register U_CHAR
*p1
= p
;
5133 SKIP_WHITE_SPACE (p1
);
5134 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
5135 tpat
->raw_after
= 1;
5137 lastp
= exp_p
; /* place to start copying from next time */
5144 /* If this was not a macro arg, copy it into the expansion. */
5145 if (! skipped_arg
) {
5146 register U_CHAR
*lim1
= p
;
5150 if (stringify
== id_beg
)
5151 error ("`#' operator should be followed by a macro argument name");
5157 /* Convert trailing whitespace to Newline-markers. */
5158 while (limit
< end
&& is_space
[*limit
]) {
5160 *exp_p
++ = *limit
++;
5162 } else if (!traditional
) {
5163 /* There is no trailing whitespace, so invent some. */
5170 defn
->length
= exp_p
- defn
->expansion
;
5172 /* Crash now if we overrun the allocated size. */
5173 if (defn
->length
+ 1 > maxsize
)
5177 /* This isn't worth the time it takes. */
5178 /* give back excess storage */
5179 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
5186 do_assert (buf
, limit
, op
, keyword
)
5187 U_CHAR
*buf
, *limit
;
5189 struct directive
*keyword
;
5191 U_CHAR
*bp
; /* temp ptr into input buffer */
5192 U_CHAR
*symname
; /* remember where symbol name starts */
5193 int sym_length
; /* and how long it is */
5194 struct arglist
*tokens
= NULL
;
5196 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
5197 pedwarn ("ANSI C does not allow `#assert'");
5201 while (is_hor_space
[*bp
])
5204 symname
= bp
; /* remember where it starts */
5205 sym_length
= check_macro_name (bp
, "assertion");
5207 /* #define doesn't do this, but we should. */
5208 SKIP_WHITE_SPACE (bp
);
5210 /* Lossage will occur if identifiers or control tokens are broken
5211 across lines using backslash. This is not the right place to take
5215 error ("missing token-sequence in `#assert'");
5222 bp
++; /* skip '(' */
5223 SKIP_WHITE_SPACE (bp
);
5225 tokens
= read_token_list (&bp
, limit
, &error_flag
);
5229 error ("empty token-sequence in `#assert'");
5233 ++bp
; /* skip paren */
5234 SKIP_WHITE_SPACE (bp
);
5237 /* If this name isn't already an assertion name, make it one.
5238 Error if it was already in use in some other way. */
5241 ASSERTION_HASHNODE
*hp
;
5242 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
5243 struct tokenlist_list
*value
5244 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
5246 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
5248 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
5249 error ("`defined' redefined as assertion");
5250 hp
= assertion_install (symname
, sym_length
, hashcode
);
5253 /* Add the spec'd token-sequence to the list of such. */
5254 value
->tokens
= tokens
;
5255 value
->next
= hp
->value
;
5263 do_unassert (buf
, limit
, op
, keyword
)
5264 U_CHAR
*buf
, *limit
;
5266 struct directive
*keyword
;
5268 U_CHAR
*bp
; /* temp ptr into input buffer */
5269 U_CHAR
*symname
; /* remember where symbol name starts */
5270 int sym_length
; /* and how long it is */
5272 struct arglist
*tokens
= NULL
;
5273 int tokens_specified
= 0;
5275 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
5276 pedwarn ("ANSI C does not allow `#unassert'");
5280 while (is_hor_space
[*bp
])
5283 symname
= bp
; /* remember where it starts */
5284 sym_length
= check_macro_name (bp
, "assertion");
5286 /* #define doesn't do this, but we should. */
5287 SKIP_WHITE_SPACE (bp
);
5289 /* Lossage will occur if identifiers or control tokens are broken
5290 across lines using backslash. This is not the right place to take
5296 bp
++; /* skip '(' */
5297 SKIP_WHITE_SPACE (bp
);
5299 tokens
= read_token_list (&bp
, limit
, &error_flag
);
5303 error ("empty token list in `#unassert'");
5307 tokens_specified
= 1;
5309 ++bp
; /* skip paren */
5310 SKIP_WHITE_SPACE (bp
);
5314 ASSERTION_HASHNODE
*hp
;
5315 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
5316 struct tokenlist_list
*tail
, *prev
;
5318 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
5322 /* If no token list was specified, then eliminate this assertion
5324 if (! tokens_specified
) {
5325 struct tokenlist_list
*next
;
5326 for (tail
= hp
->value
; tail
; tail
= next
) {
5328 free_token_list (tail
->tokens
);
5331 delete_assertion (hp
);
5333 /* If a list of tokens was given, then delete any matching list. */
5338 struct tokenlist_list
*next
= tail
->next
;
5339 if (compare_token_lists (tail
->tokens
, tokens
)) {
5343 hp
->value
= tail
->next
;
5344 free_token_list (tail
->tokens
);
5357 /* Test whether there is an assertion named NAME
5358 and optionally whether it has an asserted token list TOKENS.
5359 NAME is not null terminated; its length is SYM_LENGTH.
5360 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
5363 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
5366 int tokens_specified
;
5367 struct arglist
*tokens
;
5369 ASSERTION_HASHNODE
*hp
;
5370 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
5372 if (pedantic
&& !instack
[indepth
].system_header_p
)
5373 pedwarn ("ANSI C does not allow testing assertions");
5375 hp
= assertion_lookup (name
, sym_length
, hashcode
);
5377 /* It is not an assertion; just return false. */
5380 /* If no token list was specified, then value is 1. */
5381 if (! tokens_specified
)
5385 struct tokenlist_list
*tail
;
5389 /* If a list of tokens was given,
5390 then succeed if the assertion records a matching list. */
5393 if (compare_token_lists (tail
->tokens
, tokens
))
5398 /* Fail if the assertion has no matching list. */
5403 /* Compare two lists of tokens for equality including order of tokens. */
5406 compare_token_lists (l1
, l2
)
5407 struct arglist
*l1
, *l2
;
5410 if (l1
->length
!= l2
->length
)
5412 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
5418 /* Succeed if both lists end at the same time. */
5422 /* Read a space-separated list of tokens ending in a close parenthesis.
5423 Return a list of strings, in the order they were written.
5424 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
5425 Parse the text starting at *BPP, and update *BPP.
5426 Don't parse beyond LIMIT. */
5428 static struct arglist
*
5429 read_token_list (bpp
, limit
, error_flag
)
5434 struct arglist
*token_ptrs
= 0;
5440 /* Loop over the assertion value tokens. */
5442 struct arglist
*temp
;
5446 /* Find the end of the token. */
5450 } else if (*bp
== ')') {
5455 } else if (*bp
== '"' || *bp
== '\'')
5456 bp
= skip_quoted_string (bp
, limit
, 0, 0, 0, &eofp
);
5458 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
5459 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
5462 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
5463 temp
->name
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
5464 bcopy (beg
, temp
->name
, bp
- beg
);
5465 temp
->name
[bp
- beg
] = 0;
5466 temp
->next
= token_ptrs
;
5468 temp
->length
= bp
- beg
;
5470 SKIP_WHITE_SPACE (bp
);
5473 error ("unterminated token sequence in `#assert' or `#unassert'");
5480 /* We accumulated the names in reverse order.
5481 Now reverse them to get the proper order. */
5483 register struct arglist
*prev
= 0, *this, *next
;
5484 for (this = token_ptrs
; this; this = next
) {
5494 free_token_list (tokens
)
5495 struct arglist
*tokens
;
5498 struct arglist
*next
= tokens
->next
;
5499 free (tokens
->name
);
5506 * Install a name in the assertion hash table.
5508 * If LEN is >= 0, it is the length of the name.
5509 * Otherwise, compute the length by scanning the entire name.
5511 * If HASH is >= 0, it is the precomputed hash code.
5512 * Otherwise, compute the hash code.
5514 static ASSERTION_HASHNODE
*
5515 assertion_install (name
, len
, hash
)
5520 register ASSERTION_HASHNODE
*hp
;
5521 register int i
, bucket
;
5522 register U_CHAR
*p
, *q
;
5524 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
5525 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
5527 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
5528 hp
->next
= assertion_hashtab
[bucket
];
5529 assertion_hashtab
[bucket
] = hp
;
5531 if (hp
->next
!= NULL
)
5532 hp
->next
->prev
= hp
;
5535 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
5538 for (i
= 0; i
< len
; i
++)
5545 * find the most recent hash node for name name (ending with first
5546 * non-identifier char) installed by install
5548 * If LEN is >= 0, it is the length of the name.
5549 * Otherwise, compute the length by scanning the entire name.
5551 * If HASH is >= 0, it is the precomputed hash code.
5552 * Otherwise, compute the hash code.
5554 static ASSERTION_HASHNODE
*
5555 assertion_lookup (name
, len
, hash
)
5560 register U_CHAR
*bp
;
5561 register ASSERTION_HASHNODE
*bucket
;
5563 bucket
= assertion_hashtab
[hash
];
5565 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
5567 bucket
= bucket
->next
;
5573 delete_assertion (hp
)
5574 ASSERTION_HASHNODE
*hp
;
5577 if (hp
->prev
!= NULL
)
5578 hp
->prev
->next
= hp
->next
;
5579 if (hp
->next
!= NULL
)
5580 hp
->next
->prev
= hp
->prev
;
5582 /* make sure that the bucket chain header that
5583 the deleted guy was on points to the right thing afterwards. */
5584 if (hp
== *hp
->bucket_hdr
)
5585 *hp
->bucket_hdr
= hp
->next
;
5591 * interpret #line command. Remembers previously seen fnames
5592 * in its very own hash table.
5594 #define FNAME_HASHSIZE 37
5597 do_line (buf
, limit
, op
, keyword
)
5598 U_CHAR
*buf
, *limit
;
5600 struct directive
*keyword
;
5602 register U_CHAR
*bp
;
5603 FILE_BUF
*ip
= &instack
[indepth
];
5606 enum file_change_code file_change
= same_file
;
5608 /* Expand any macros. */
5609 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
5611 /* Point to macroexpanded line, which is null-terminated now. */
5613 SKIP_WHITE_SPACE (bp
);
5615 if (!isdigit (*bp
)) {
5616 error ("invalid format `#line' command");
5620 /* The Newline at the end of this line remains to be processed.
5621 To put the next line at the specified line number,
5622 we must store a line number now that is one less. */
5623 new_lineno
= atoi (bp
) - 1;
5625 /* skip over the line number. */
5626 while (isdigit (*bp
))
5629 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
5630 if (*bp
&& !is_space
[*bp
]) {
5631 error ("invalid format `#line' command");
5636 SKIP_WHITE_SPACE (bp
);
5639 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
5640 HASHNODE
*hp
, **hash_bucket
;
5646 while (*bp
&& *bp
!= '\"')
5649 error ("invalid format `#line' command");
5653 fname_length
= bp
- fname
;
5656 SKIP_WHITE_SPACE (bp
);
5659 file_change
= enter_file
;
5660 else if (*bp
== '2')
5661 file_change
= leave_file
;
5662 else if (*bp
== '3')
5663 ip
->system_header_p
= 1;
5665 error ("invalid format `#line' command");
5670 SKIP_WHITE_SPACE (bp
);
5672 ip
->system_header_p
= 1;
5674 SKIP_WHITE_SPACE (bp
);
5677 error ("invalid format `#line' command");
5683 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
5684 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
5685 if (hp
->length
== fname_length
&&
5686 strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
5687 ip
->nominal_fname
= hp
->value
.cpval
;
5691 /* Didn't find it; cons up a new one. */
5692 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
5693 hp
->next
= *hash_bucket
;
5696 hp
->length
= fname_length
;
5697 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
5698 bcopy (fname
, hp
->value
.cpval
, fname_length
);
5701 error ("invalid format `#line' command");
5705 ip
->lineno
= new_lineno
;
5706 output_line_command (ip
, op
, 0, file_change
);
5707 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
5712 * remove the definition of a symbol from the symbol table.
5713 * according to un*x /lib/cpp, it is not an error to undef
5714 * something that has no definitions, so it isn't one here either.
5718 do_undef (buf
, limit
, op
, keyword
)
5719 U_CHAR
*buf
, *limit
;
5721 struct directive
*keyword
;
5725 U_CHAR
*orig_buf
= buf
;
5727 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
5728 if (pcp_outfile
&& op
)
5729 pass_thru_directive (buf
, limit
, op
, keyword
);
5731 SKIP_WHITE_SPACE (buf
);
5732 sym_length
= check_macro_name (buf
, "macro");
5734 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
5735 /* If we are generating additional info for debugging (with -g) we
5736 need to pass through all effective #undef commands. */
5737 if (debug_output
&& op
)
5738 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
5739 if (hp
->type
!= T_MACRO
)
5740 warning ("undefining `%s'", hp
->name
);
5746 SKIP_WHITE_SPACE (buf
);
5748 pedwarn ("garbage after `#undef' directive");
5754 * Report a fatal error detected by the program we are processing.
5755 * Use the text of the line in the error message, then terminate.
5756 * (We use error() because it prints the filename & line#.)
5760 do_error (buf
, limit
, op
, keyword
)
5761 U_CHAR
*buf
, *limit
;
5763 struct directive
*keyword
;
5765 int length
= limit
- buf
;
5766 char *copy
= (char *) xmalloc (length
+ 1);
5767 bcopy (buf
, copy
, length
);
5769 SKIP_WHITE_SPACE (copy
);
5770 error ("#error %s", copy
);
5771 exit (FAILURE_EXIT_CODE
);
5777 * Report a warning detected by the program we are processing.
5778 * Use the text of the line in the warning message, then continue.
5779 * (We use error() because it prints the filename & line#.)
5783 do_warning (buf
, limit
, op
, keyword
)
5784 U_CHAR
*buf
, *limit
;
5786 struct directive
*keyword
;
5788 int length
= limit
- buf
;
5789 char *copy
= (char *) xmalloc (length
+ 1);
5790 bcopy (buf
, copy
, length
);
5792 SKIP_WHITE_SPACE (copy
);
5793 error ("#warning %s", copy
);
5797 /* Remember the name of the current file being read from so that we can
5798 avoid ever including it again. */
5804 FILE_BUF
*ip
= NULL
;
5806 for (i
= indepth
; i
>= 0; i
--)
5807 if (instack
[i
].fname
!= NULL
) {
5813 struct file_name_list
*new;
5815 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5816 new->next
= dont_repeat_files
;
5817 dont_repeat_files
= new;
5818 new->fname
= savestring (ip
->fname
);
5819 new->control_macro
= 0;
5824 /* #ident has already been copied to the output file, so just ignore it. */
5827 do_ident (buf
, limit
)
5828 U_CHAR
*buf
, *limit
;
5830 /* Allow #ident in system headers, since that's not user's fault. */
5831 if (pedantic
&& !instack
[indepth
].system_header_p
)
5832 pedwarn ("ANSI C does not allow `#ident'");
5836 /* #pragma and its argument line have already been copied to the output file.
5837 Here just check for recognized pragmas. */
5840 do_pragma (buf
, limit
)
5841 U_CHAR
*buf
, *limit
;
5843 while (*buf
== ' ' || *buf
== '\t')
5845 if (!strncmp (buf
, "once", 4)) {
5846 warning ("`#pragma once' is obsolete");
5853 /* This was a fun hack, but #pragma seems to start to be useful.
5854 By failing to recognize it, we pass it through unchanged to cc1. */
5857 * the behavior of the #pragma directive is implementation defined.
5858 * this implementation defines it as follows.
5865 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
5868 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
5870 execl ("/usr/games/hack", "#pragma", 0);
5871 execl ("/usr/games/rogue", "#pragma", 0);
5872 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
5873 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
5875 fatal ("You are in a maze of twisty compiler features, all different");
5879 /* Just ignore #sccs, on systems where we define it at all. */
5885 pedwarn ("ANSI C does not allow `#sccs'");
5890 * handle #if command by
5891 * 1) inserting special `defined' keyword into the hash table
5892 * that gets turned into 0 or 1 by special_symbol (thus,
5893 * if the luser has a symbol called `defined' already, it won't
5894 * work inside the #if command)
5895 * 2) rescan the input into a temporary output buffer
5896 * 3) pass the output buffer to the yacc parser and collect a value
5897 * 4) clean up the mess left from steps 1 and 2.
5898 * 5) call conditional_skip to skip til the next #endif (etc.),
5899 * or not, depending on the value from step 3.
5903 do_if (buf
, limit
, op
, keyword
)
5904 U_CHAR
*buf
, *limit
;
5906 struct directive
*keyword
;
5909 FILE_BUF
*ip
= &instack
[indepth
];
5911 value
= eval_if_expression (buf
, limit
- buf
);
5912 conditional_skip (ip
, value
== 0, T_IF
, 0);
5917 * handle a #elif directive by not changing if_stack either.
5918 * see the comment above do_else.
5922 do_elif (buf
, limit
, op
, keyword
)
5923 U_CHAR
*buf
, *limit
;
5925 struct directive
*keyword
;
5928 FILE_BUF
*ip
= &instack
[indepth
];
5930 if (if_stack
== instack
[indepth
].if_stack
) {
5931 error ("`#elif' not within a conditional");
5934 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
5935 error ("`#elif' after `#else'");
5936 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
5937 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
5938 strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
5939 fprintf (stderr
, ", file %s", if_stack
->fname
);
5940 fprintf (stderr
, ")\n");
5942 if_stack
->type
= T_ELIF
;
5945 if (if_stack
->if_succeeded
)
5946 skip_if_group (ip
, 0);
5948 value
= eval_if_expression (buf
, limit
- buf
);
5950 skip_if_group (ip
, 0);
5952 ++if_stack
->if_succeeded
; /* continue processing input */
5953 output_line_command (ip
, op
, 1, same_file
);
5960 * evaluate a #if expression in BUF, of length LENGTH,
5961 * then parse the result as a C expression and return the value as an int.
5964 eval_if_expression (buf
, length
)
5969 HASHNODE
*save_defined
;
5972 save_defined
= install ("defined", -1, T_SPEC_DEFINED
, 0, -1);
5974 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
5976 delete_macro (save_defined
); /* clean up special symbol */
5978 value
= parse_c_expression (temp_obuf
.buf
);
5980 free (temp_obuf
.buf
);
5986 * routine to handle ifdef/ifndef. Try to look up the symbol,
5987 * then do or don't skip to the #endif/#else/#elif depending
5988 * on what directive is actually being processed.
5992 do_xifdef (buf
, limit
, op
, keyword
)
5993 U_CHAR
*buf
, *limit
;
5995 struct directive
*keyword
;
5998 FILE_BUF
*ip
= &instack
[indepth
];
6000 int start_of_file
= 0;
6001 U_CHAR
*control_macro
= 0;
6003 /* Detect a #ifndef at start of file (not counting comments). */
6004 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
6005 U_CHAR
*p
= ip
->buf
;
6006 while (p
!= directive_start
) {
6014 if (p
!= ip
->bufp
&& *p
== '*') {
6015 /* Skip this comment. */
6017 U_CHAR
*save_bufp
= ip
->bufp
;
6019 p
= skip_to_end_of_comment (ip
, &junk
, 1);
6020 ip
->bufp
= save_bufp
;
6027 /* If we get here, this conditional is the beginning of the file. */
6032 /* Discard leading and trailing whitespace. */
6033 SKIP_WHITE_SPACE (buf
);
6034 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
6036 /* Find the end of the identifier at the beginning. */
6037 for (end
= buf
; is_idchar
[*end
]; end
++);
6040 skip
= (keyword
->type
== T_IFDEF
);
6042 pedwarn (end
== limit
? "`#%s' with no argument"
6043 : "`#%s' argument starts with punctuation",
6048 if (pedantic
&& buf
[0] >= '0' && buf
[0] <= '9')
6049 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
6050 else if (end
!= limit
&& !traditional
)
6051 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
6053 hp
= lookup (buf
, end
-buf
, -1);
6056 /* Output a precondition for this macro. */
6057 if (hp
&& hp
->value
.defn
->predefined
)
6058 fprintf(pcp_outfile
, "#define %s\n", hp
->name
);
6061 fprintf(pcp_outfile
, "#undef ");
6062 while (is_idchar
[*cp
]) /* Ick! */
6063 fputc (*cp
++, pcp_outfile
);
6064 putc ('\n', pcp_outfile
);
6068 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
6069 if (start_of_file
&& !skip
) {
6070 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
6071 bcopy (buf
, control_macro
, end
- buf
);
6072 control_macro
[end
- buf
] = 0;
6076 conditional_skip (ip
, skip
, T_IF
, control_macro
);
6080 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
6081 If this is a #ifndef starting at the beginning of a file,
6082 CONTROL_MACRO is the macro name tested by the #ifndef.
6083 Otherwise, CONTROL_MACRO is 0. */
6086 conditional_skip (ip
, skip
, type
, control_macro
)
6089 enum node_type type
;
6090 U_CHAR
*control_macro
;
6092 IF_STACK_FRAME
*temp
;
6094 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
6095 temp
->fname
= ip
->nominal_fname
;
6096 temp
->lineno
= ip
->lineno
;
6097 temp
->next
= if_stack
;
6098 temp
->control_macro
= control_macro
;
6101 if_stack
->type
= type
;
6104 skip_if_group (ip
, 0);
6107 ++if_stack
->if_succeeded
;
6108 output_line_command (ip
, &outbuf
, 1, same_file
);
6113 * skip to #endif, #else, or #elif. adjust line numbers, etc.
6114 * leaves input ptr at the sharp sign found.
6115 * If ANY is nonzero, return at next directive of any sort.
6118 skip_if_group (ip
, any
)
6122 register U_CHAR
*bp
= ip
->bufp
, *cp
;
6123 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
6124 struct directive
*kt
;
6125 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
6126 U_CHAR
*beg_of_line
= bp
;
6127 register int ident_length
;
6128 U_CHAR
*ident
, *after_ident
;
6132 case '/': /* possible comment */
6133 if (*bp
== '\\' && bp
[1] == '\n')
6136 || ((cplusplus
|| objc
) && *bp
== '/')) {
6138 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
6143 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
, 0, 0);
6146 /* Char after backslash loses its special meaning. */
6149 ++ip
->lineno
; /* But do update the line-count. */
6160 /* # keyword: a # must be first nonblank char on the line */
6161 if (beg_of_line
== 0)
6163 /* Scan from start of line, skipping whitespace, comments
6164 and backslash-newlines, and see if we reach this #.
6165 If not, this # is not special. */
6168 if (is_hor_space
[*bp
])
6170 else if (*bp
== '\\' && bp
[1] == '\n')
6172 else if (*bp
== '/' && bp
[1] == '*') {
6174 while (!(*bp
== '*' && bp
[1] == '/'))
6177 } else if ((cplusplus
|| objc
) && *bp
== '/' && bp
[1] == '/') {
6179 while (*bp
++ != '\n') ;
6183 if (bp
!= ip
->bufp
) {
6184 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
6188 bp
= ip
->bufp
+ 1; /* Point after the '#' */
6190 /* Skip whitespace and \-newline. */
6192 if (is_hor_space
[*bp
])
6194 else if (*bp
== '\\' && bp
[1] == '\n')
6196 else if (*bp
== '/' && bp
[1] == '*') {
6198 while (!(*bp
== '*' && bp
[1] == '/')) {
6204 } else if ((cplusplus
|| objc
) && *bp
== '/' && bp
[1] == '/') {
6206 while (*bp
++ != '\n') ;
6213 /* Now find end of directive name.
6214 If we encounter a backslash-newline, exchange it with any following
6215 symbol-constituents so that we end up with a contiguous name. */
6221 if (*bp
== '\\' && bp
[1] == '\n')
6222 name_newline_fix (bp
);
6228 ident_length
= bp
- cp
;
6232 /* A line of just `#' becomes blank. */
6234 if (ident_length
== 0 && *after_ident
== '\n') {
6238 if (ident_length
== 0 || !is_idstart
[*ident
]) {
6240 while (is_idchar
[*p
]) {
6241 if (*p
< '0' || *p
> '9')
6245 /* Handle # followed by a line number. */
6246 if (p
!= ident
&& !is_idchar
[*p
]) {
6248 pedwarn ("`#' followed by integer");
6252 /* Avoid error for `###' and similar cases unless -pedantic. */
6254 while (*p
== '#' || is_hor_space
[*p
]) p
++;
6256 if (pedantic
&& !lang_asm
)
6257 pedwarn ("invalid preprocessor directive");
6262 if (!lang_asm
&& pedantic
)
6263 pedwarn ("invalid preprocessor directive name");
6267 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
6268 IF_STACK_FRAME
*temp
;
6269 if (ident_length
== kt
->length
6270 && strncmp (cp
, kt
->name
, kt
->length
) == 0) {
6271 /* If we are asked to return on next directive, do so now. */
6279 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
6280 temp
->next
= if_stack
;
6282 temp
->lineno
= ip
->lineno
;
6283 temp
->fname
= ip
->nominal_fname
;
6284 temp
->type
= kt
->type
;
6288 if (pedantic
&& if_stack
!= save_if_stack
)
6291 if (if_stack
== instack
[indepth
].if_stack
) {
6292 error ("`#%s' not within a conditional", kt
->name
);
6295 else if (if_stack
== save_if_stack
)
6296 return; /* found what we came for */
6298 if (kt
->type
!= T_ENDIF
) {
6299 if (if_stack
->type
== T_ELSE
)
6300 error ("`#else' or `#elif' after `#else'");
6301 if_stack
->type
= kt
->type
;
6306 if_stack
= if_stack
->next
;
6313 /* Don't let erroneous code go by. */
6314 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
6315 pedwarn ("invalid preprocessor directive name");
6319 /* after this returns, rescan will exit because ip->bufp
6320 now points to the end of the buffer.
6321 rescan is responsible for the error message also. */
6325 * handle a #else directive. Do this by just continuing processing
6326 * without changing if_stack ; this is so that the error message
6327 * for missing #endif's etc. will point to the original #if. It
6328 * is possible that something different would be better.
6332 do_else (buf
, limit
, op
, keyword
)
6333 U_CHAR
*buf
, *limit
;
6335 struct directive
*keyword
;
6337 FILE_BUF
*ip
= &instack
[indepth
];
6340 SKIP_WHITE_SPACE (buf
);
6342 pedwarn ("text following `#else' violates ANSI standard");
6345 if (if_stack
== instack
[indepth
].if_stack
) {
6346 error ("`#else' not within a conditional");
6349 /* #ifndef can't have its special treatment for containing the whole file
6350 if it has a #else clause. */
6351 if_stack
->control_macro
= 0;
6353 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
6354 error ("`#else' after `#else'");
6355 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
6356 if (strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
6357 fprintf (stderr
, ", file %s", if_stack
->fname
);
6358 fprintf (stderr
, ")\n");
6360 if_stack
->type
= T_ELSE
;
6363 if (if_stack
->if_succeeded
)
6364 skip_if_group (ip
, 0);
6366 ++if_stack
->if_succeeded
; /* continue processing input */
6367 output_line_command (ip
, op
, 1, same_file
);
6373 * unstack after #endif command
6377 do_endif (buf
, limit
, op
, keyword
)
6378 U_CHAR
*buf
, *limit
;
6380 struct directive
*keyword
;
6383 SKIP_WHITE_SPACE (buf
);
6385 pedwarn ("text following `#endif' violates ANSI standard");
6388 if (if_stack
== instack
[indepth
].if_stack
)
6389 error ("unbalanced `#endif'");
6391 IF_STACK_FRAME
*temp
= if_stack
;
6392 if_stack
= if_stack
->next
;
6393 if (temp
->control_macro
!= 0) {
6394 /* This #endif matched a #ifndef at the start of the file.
6395 See if it is at the end of the file. */
6396 FILE_BUF
*ip
= &instack
[indepth
];
6397 U_CHAR
*p
= ip
->bufp
;
6398 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
6408 if (p
!= ep
&& *p
== '*') {
6409 /* Skip this comment. */
6411 U_CHAR
*save_bufp
= ip
->bufp
;
6413 p
= skip_to_end_of_comment (ip
, &junk
, 1);
6414 ip
->bufp
= save_bufp
;
6421 /* If we get here, this #endif ends a #ifndef
6422 that contains all of the file (aside from whitespace).
6423 Arrange not to include the file again
6424 if the macro that was tested is defined. */
6426 record_control_macro (ip
->fname
, temp
->control_macro
);
6430 output_line_command (&instack
[indepth
], op
, 1, same_file
);
6435 /* When an #else or #endif is found while skipping failed conditional,
6436 if -pedantic was specified, this is called to warn about text after
6437 the command name. P points to the first char after the command name. */
6443 /* Advance P over whitespace and comments. */
6445 if (*p
== '\\' && p
[1] == '\n')
6447 if (is_hor_space
[*p
])
6449 else if (*p
== '/') {
6450 if (p
[1] == '\\' && p
[2] == '\n')
6451 newline_fix (p
+ 1);
6454 /* Don't bother warning about unterminated comments
6455 since that will happen later. Just be sure to exit. */
6457 if (p
[1] == '\\' && p
[2] == '\n')
6458 newline_fix (p
+ 1);
6459 if (*p
== '*' && p
[1] == '/') {
6466 else if ((cplusplus
|| objc
) && p
[1] == '/') {
6468 while (*p
&& *p
++ != '\n') ;
6472 if (*p
&& *p
!= '\n')
6473 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
6476 /* Skip a comment, assuming the input ptr immediately follows the
6477 initial slash-star. Bump *LINE_COUNTER for each newline.
6478 (The canonical line counter is &ip->lineno.)
6479 Don't use this routine (or the next one) if bumping the line
6480 counter is not sufficient to deal with newlines in the string.
6482 If NOWARN is nonzero, don't warn about slash-star inside a comment.
6483 This feature is useful when processing a comment that is going to be
6484 processed or was processed at another point in the preprocessor,
6485 to avoid a duplicate warning. */
6487 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
6488 register FILE_BUF
*ip
;
6489 int *line_counter
; /* place to remember newlines, or NULL */
6492 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
6493 register U_CHAR
*bp
= ip
->bufp
;
6494 FILE_BUF
*op
= &outbuf
; /* JF */
6495 int output
= put_out_comments
&& !line_counter
;
6497 /* JF this line_counter stuff is a crock to make sure the
6498 comment is only put out once, no matter how many times
6499 the comment is skipped. It almost works */
6504 if ((cplusplus
|| objc
) && bp
[-1] == '/') {
6507 if ((*op
->bufp
++ = *bp
++) == '\n') {
6515 while (bp
< limit
) {
6516 if (*bp
++ == '\n') {
6525 while (bp
< limit
) {
6530 if (warn_comments
&& !nowarn
&& bp
< limit
&& *bp
== '*')
6531 warning ("`/*' within comment");
6534 if (line_counter
!= NULL
)
6540 if (*bp
== '\\' && bp
[1] == '\n')
6556 * Skip over a quoted string. BP points to the opening quote.
6557 * Returns a pointer after the closing quote. Don't go past LIMIT.
6558 * START_LINE is the line number of the starting point (but it need
6559 * not be valid if the starting point is inside a macro expansion).
6561 * The input stack state is not changed.
6563 * If COUNT_NEWLINES is nonzero, it points to an int to increment
6564 * for each newline passed.
6566 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
6567 * if we pass a backslash-newline.
6569 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
6572 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
6573 register U_CHAR
*bp
;
6574 register U_CHAR
*limit
;
6576 int *count_newlines
;
6577 int *backslash_newlines_p
;
6580 register U_CHAR c
, match
;
6585 error_with_line (line_for_error (start_line
),
6586 "unterminated string or character constant");
6593 while (*bp
== '\\' && bp
[1] == '\n') {
6594 if (backslash_newlines_p
)
6595 *backslash_newlines_p
= 1;
6600 if (*bp
== '\n' && count_newlines
) {
6601 if (backslash_newlines_p
)
6602 *backslash_newlines_p
= 1;
6606 } else if (c
== '\n') {
6608 /* Unterminated strings and character constants are 'legal'. */
6609 bp
--; /* Don't consume the newline. */
6614 if (match
== '\'') {
6615 error_with_line (line_for_error (start_line
),
6616 "unterminated character constant");
6622 if (traditional
) { /* Unterminated strings are 'legal'. */
6627 /* If not traditional, then allow newlines inside strings. */
6630 } else if (c
== match
)
6636 /* Skip across a group of balanced parens, starting from IP->bufp.
6637 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
6639 This does not handle newlines, because it's used for the arg of #if,
6640 where there aren't any newlines. Also, backslash-newline can't appear. */
6643 skip_paren_group (ip
)
6644 register FILE_BUF
*ip
;
6646 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
6647 U_CHAR
*p
= ip
->bufp
;
6649 int lines_dummy
= 0;
6651 while (p
!= limit
) {
6661 return ip
->bufp
= p
;
6667 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
6675 p
= skip_quoted_string (p
- 1, limit
, 0, 0, 0, &eofp
);
6677 return ip
->bufp
= p
;
6688 * write out a #line command, for instance, after an #include file.
6689 * If CONDITIONAL is nonzero, we can omit the #line if it would
6690 * appear to be a no-op, and we can output a few newlines instead
6691 * if we want to increase the line number by a small amount.
6692 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
6696 output_line_command (ip
, op
, conditional
, file_change
)
6699 enum file_change_code file_change
;
6702 char line_cmd_buf
[500];
6704 if (no_line_commands
6705 || ip
->fname
== NULL
6707 op
->lineno
= ip
->lineno
;
6712 if (ip
->lineno
== op
->lineno
)
6715 /* If the inherited line number is a little too small,
6716 output some newlines instead of a #line command. */
6717 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
6718 check_expand (op
, 10);
6719 while (ip
->lineno
> op
->lineno
) {
6727 /* Don't output a line number of 0 if we can help it. */
6728 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
6729 && *ip
->bufp
== '\n') {
6734 #ifdef OUTPUT_LINE_COMMANDS
6735 sprintf (line_cmd_buf
, "#line %d \"%s\"", ip
->lineno
, ip
->nominal_fname
);
6737 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->nominal_fname
);
6739 if (file_change
!= same_file
)
6740 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
6741 /* Tell cc1 if following text comes from a system header file. */
6742 if (ip
->system_header_p
)
6743 strcat (line_cmd_buf
, " 3");
6744 len
= strlen (line_cmd_buf
);
6745 line_cmd_buf
[len
++] = '\n';
6746 check_expand (op
, len
+ 1);
6747 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
6749 bcopy (line_cmd_buf
, op
->bufp
, len
);
6751 op
->lineno
= ip
->lineno
;
6754 /* This structure represents one parsed argument in a macro call.
6755 `raw' points to the argument text as written (`raw_length' is its length).
6756 `expanded' points to the argument's macro-expansion
6757 (its length is `expand_length').
6758 `stringified_length' is the length the argument would have
6760 `use_count' is the number of times this macro arg is substituted
6761 into the macro. If the actual use count exceeds 10,
6762 the value stored is 10.
6763 `free1' and `free2', if nonzero, point to blocks to be freed
6764 when the macro argument data is no longer needed. */
6767 U_CHAR
*raw
, *expanded
;
6768 int raw_length
, expand_length
;
6769 int stringified_length
;
6770 U_CHAR
*free1
, *free2
;
6776 /* Expand a macro call.
6777 HP points to the symbol that is the macro being called.
6778 Put the result of expansion onto the input stack
6779 so that subsequent input by our caller will use it.
6781 If macro wants arguments, caller has already verified that
6782 an argument list follows; arguments come from the input stack. */
6785 macroexpand (hp
, op
)
6790 DEFINITION
*defn
= hp
->value
.defn
;
6791 register U_CHAR
*xbuf
;
6793 int start_line
= instack
[indepth
].lineno
;
6794 int rest_args
, rest_zero
;
6796 CHECK_DEPTH (return;);
6798 /* it might not actually be a macro. */
6799 if (hp
->type
!= T_MACRO
) {
6800 special_symbol (hp
, op
);
6804 /* This macro is being used inside a #if, which means it must be */
6805 /* recorded as a precondition. */
6806 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
6807 dump_single_macro (hp
, pcp_outfile
);
6809 nargs
= defn
->nargs
;
6813 struct argdata
*args
;
6814 char *parse_error
= 0;
6816 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
6818 for (i
= 0; i
< nargs
; i
++) {
6819 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
6820 args
[i
].raw_length
= args
[i
].expand_length
6821 = args
[i
].stringified_length
= 0;
6822 args
[i
].free1
= args
[i
].free2
= 0;
6823 args
[i
].use_count
= 0;
6826 /* Parse all the macro args that are supplied. I counts them.
6827 The first NARGS args are stored in ARGS.
6828 The rest are discarded.
6829 If rest_args is set then we assume macarg absorbed the rest of the args.
6834 /* Discard the open-parenthesis or comma before the next arg. */
6835 ++instack
[indepth
].bufp
;
6838 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
6839 /* if we are working on last arg which absorbes rest of args... */
6840 if (i
== nargs
- 1 && defn
->rest_args
)
6842 parse_error
= macarg (&args
[i
], rest_args
);
6845 parse_error
= macarg (0, 0);
6847 error_with_line (line_for_error (start_line
), parse_error
);
6851 } while (*instack
[indepth
].bufp
!= ')');
6853 /* If we got one arg but it was just whitespace, call that 0 args. */
6855 register U_CHAR
*bp
= args
[0].raw
;
6856 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
6857 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
6863 if (nargs
== 0 && i
> 0)
6864 error ("arguments given to macro `%s'", hp
->name
);
6865 else if (i
< nargs
) {
6866 /* traditional C allows foo() if foo wants one argument. */
6867 if (nargs
== 1 && i
== 0 && traditional
)
6869 /* the rest args token is allowed to absorb 0 tokens */
6870 else if (i
== nargs
- 1 && defn
->rest_args
)
6873 error ("macro `%s' used without args", hp
->name
);
6875 error ("macro `%s' used with just one arg", hp
->name
);
6877 error ("macro `%s' used with only %d args", hp
->name
, i
);
6878 } else if (i
> nargs
)
6879 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
6881 /* Swallow the closeparen. */
6882 ++instack
[indepth
].bufp
;
6884 /* If macro wants zero args, we parsed the arglist for checking only.
6885 Read directly from the macro definition. */
6887 xbuf
= defn
->expansion
;
6888 xbuf_len
= defn
->length
;
6890 register U_CHAR
*exp
= defn
->expansion
;
6891 register int offset
; /* offset in expansion,
6892 copied a piece at a time */
6893 register int totlen
; /* total amount of exp buffer filled so far */
6895 register struct reflist
*ap
, *last_ap
;
6897 /* Macro really takes args. Compute the expansion of this call. */
6899 /* Compute length in characters of the macro's expansion.
6900 Also count number of times each arg is used. */
6901 xbuf_len
= defn
->length
;
6902 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
6904 xbuf_len
+= args
[ap
->argno
].stringified_length
;
6905 else if (ap
->raw_before
|| ap
->raw_after
|| traditional
)
6906 xbuf_len
+= args
[ap
->argno
].raw_length
;
6908 xbuf_len
+= args
[ap
->argno
].expand_length
;
6910 if (args
[ap
->argno
].use_count
< 10)
6911 args
[ap
->argno
].use_count
++;
6914 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
6916 /* Generate in XBUF the complete expansion
6917 with arguments substituted in.
6918 TOTLEN is the total size generated so far.
6919 OFFSET is the index in the definition
6920 of where we are copying from. */
6921 offset
= totlen
= 0;
6922 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
6923 last_ap
= ap
, ap
= ap
->next
) {
6924 register struct argdata
*arg
= &args
[ap
->argno
];
6926 /* add chars to XBUF unless rest_args was zero with concatenation */
6927 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
6928 if (! (rest_zero
&& ((ap
->rest_args
&& ap
->raw_before
)
6929 || (last_ap
!= NULL
&& last_ap
->rest_args
6930 && last_ap
->raw_after
))))
6931 xbuf
[totlen
++] = exp
[offset
];
6933 if (ap
->stringify
!= 0) {
6934 int arglen
= arg
->raw_length
;
6940 && (c
= arg
->raw
[i
], is_space
[c
]))
6943 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
6946 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
6947 for (; i
< arglen
; i
++) {
6950 /* Special markers Newline Space
6951 generate nothing for a stringified argument. */
6952 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
6957 /* Internal sequences of whitespace are replaced by one space
6958 except within an string or char token. */
6960 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
])) {
6962 /* Note that Newline Space does occur within whitespace
6963 sequences; consider it part of the sequence. */
6964 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
6966 else if (c
!= '\n' && is_space
[c
])
6983 } else if (c
== '\"' || c
== '\'')
6987 /* Escape these chars */
6988 if (c
== '\"' || (in_string
&& c
== '\\'))
6989 xbuf
[totlen
++] = '\\';
6993 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
6998 xbuf
[totlen
++] = '\"'; /* insert ending quote */
6999 } else if (ap
->raw_before
|| ap
->raw_after
|| traditional
) {
7000 U_CHAR
*p1
= arg
->raw
;
7001 U_CHAR
*l1
= p1
+ arg
->raw_length
;
7002 if (ap
->raw_before
) {
7003 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
7004 while (p1
!= l1
&& is_idchar
[*p1
])
7005 xbuf
[totlen
++] = *p1
++;
7006 /* Delete any no-reexpansion marker that follows
7007 an identifier at the beginning of the argument
7008 if the argument is concatenated with what precedes it. */
7009 if (p1
[0] == '\n' && p1
[1] == '-')
7012 if (ap
->raw_after
) {
7013 /* Arg is concatenated after: delete trailing whitespace,
7014 whitespace markers, and no-reexpansion markers. */
7016 if (is_space
[l1
[-1]]) l1
--;
7017 else if (l1
[-1] == '-') {
7018 U_CHAR
*p2
= l1
- 1;
7019 /* If a `-' is preceded by an odd number of newlines then it
7020 and the last newline are a no-reexpansion marker. */
7021 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
7022 if ((l1
- 1 - p2
) & 1) {
7030 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
7033 bcopy (arg
->expanded
, xbuf
+ totlen
, arg
->expand_length
);
7034 totlen
+= arg
->expand_length
;
7035 /* If a macro argument with newlines is used multiple times,
7036 then only expand the newlines once. This avoids creating output
7037 lines which don't correspond to any input line, which confuses
7039 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
7040 /* Don't bother doing delete_newlines for subsequent
7044 = delete_newlines (arg
->expanded
, arg
->expand_length
);
7048 if (totlen
> xbuf_len
)
7052 /* if there is anything left of the definition
7053 after handling the arg list, copy that in too. */
7055 for (i
= offset
; i
< defn
->length
; i
++) {
7056 /* if we've reached the end of the macro */
7059 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
7060 && last_ap
->raw_after
))
7061 xbuf
[totlen
++] = exp
[i
];
7067 for (i
= 0; i
< nargs
; i
++) {
7068 if (args
[i
].free1
!= 0)
7069 free (args
[i
].free1
);
7070 if (args
[i
].free2
!= 0)
7071 free (args
[i
].free2
);
7075 xbuf
= defn
->expansion
;
7076 xbuf_len
= defn
->length
;
7079 /* Now put the expansion on the input stack
7080 so our caller will commence reading from it. */
7082 register FILE_BUF
*ip2
;
7084 ip2
= &instack
[++indepth
];
7087 ip2
->nominal_fname
= 0;
7090 ip2
->length
= xbuf_len
;
7092 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
7094 ip2
->if_stack
= if_stack
;
7095 ip2
->system_header_p
= 0;
7097 /* Recursive macro use sometimes works traditionally.
7098 #define foo(x,y) bar(x(y,0), y)
7102 hp
->type
= T_DISABLED
;
7107 * Parse a macro argument and store the info on it into *ARGPTR.
7108 * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
7109 * Return nonzero to indicate a syntax error.
7113 macarg (argptr
, rest_args
)
7114 register struct argdata
*argptr
;
7117 FILE_BUF
*ip
= &instack
[indepth
];
7122 /* Try to parse as much of the argument as exists at this
7123 input stack level. */
7124 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
7125 &paren
, &newlines
, &comments
, rest_args
);
7127 /* If we find the end of the argument at this level,
7128 set up *ARGPTR to point at it in the input stack. */
7129 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
7130 && bp
!= ip
->buf
+ ip
->length
) {
7132 argptr
->raw
= ip
->bufp
;
7133 argptr
->raw_length
= bp
- ip
->bufp
;
7134 argptr
->newlines
= newlines
;
7138 /* This input stack level ends before the macro argument does.
7139 We must pop levels and keep parsing.
7140 Therefore, we must allocate a temporary buffer and copy
7141 the macro argument into it. */
7142 int bufsize
= bp
- ip
->bufp
;
7143 int extra
= newlines
;
7144 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
7145 int final_start
= 0;
7147 bcopy (ip
->bufp
, buffer
, bufsize
);
7149 ip
->lineno
+= newlines
;
7151 while (bp
== ip
->buf
+ ip
->length
) {
7152 if (instack
[indepth
].macro
== 0) {
7154 return "unterminated macro call";
7156 ip
->macro
->type
= T_MACRO
;
7158 free (ip
->free_ptr
);
7159 ip
= &instack
[--indepth
];
7162 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
7163 &newlines
, &comments
, rest_args
);
7164 final_start
= bufsize
;
7165 bufsize
+= bp
- ip
->bufp
;
7167 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
7168 bcopy (ip
->bufp
, buffer
+ bufsize
- (bp
- ip
->bufp
), bp
- ip
->bufp
);
7170 ip
->lineno
+= newlines
;
7173 /* Now, if arg is actually wanted, record its raw form,
7174 discarding comments and duplicating newlines in whatever
7175 part of it did not come from a macro expansion.
7176 EXTRA space has been preallocated for duplicating the newlines.
7177 FINAL_START is the index of the start of that part. */
7179 argptr
->raw
= buffer
;
7180 argptr
->raw_length
= bufsize
;
7181 argptr
->free1
= buffer
;
7182 argptr
->newlines
= newlines
;
7183 argptr
->comments
= comments
;
7184 if ((newlines
|| comments
) && ip
->fname
!= 0)
7187 discard_comments (argptr
->raw
+ final_start
,
7188 argptr
->raw_length
- final_start
,
7190 argptr
->raw
[argptr
->raw_length
] = 0;
7191 if (argptr
->raw_length
> bufsize
+ extra
)
7196 /* If we are not discarding this argument,
7197 macroexpand it and compute its length as stringified.
7198 All this info goes into *ARGPTR. */
7202 register U_CHAR
*buf
, *lim
;
7203 register int totlen
;
7205 obuf
= expand_to_temp_buffer (argptr
->raw
,
7206 argptr
->raw
+ argptr
->raw_length
,
7209 argptr
->expanded
= obuf
.buf
;
7210 argptr
->expand_length
= obuf
.length
;
7211 argptr
->free2
= obuf
.buf
;
7214 lim
= buf
+ argptr
->raw_length
;
7216 while (buf
!= lim
&& is_space
[*buf
])
7218 while (buf
!= lim
&& is_space
[lim
[-1]])
7220 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
7221 while (buf
!= lim
) {
7222 register U_CHAR c
= *buf
++;
7224 /* Internal sequences of whitespace are replaced by one space
7225 in most cases, but not always. So count all the whitespace
7226 in case we need to keep it all. */
7229 SKIP_ALL_WHITE_SPACE (buf
);
7232 if (c
== '\"' || c
== '\\') /* escape these chars */
7234 else if (!isprint (c
))
7237 argptr
->stringified_length
= totlen
;
7242 /* Scan text from START (inclusive) up to LIMIT (exclusive),
7243 counting parens in *DEPTHPTR,
7244 and return if reach LIMIT
7245 or before a `)' that would make *DEPTHPTR negative
7246 or before a comma when *DEPTHPTR is zero.
7247 Single and double quotes are matched and termination
7248 is inhibited within them. Comments also inhibit it.
7249 Value returned is pointer to stopping place.
7251 Increment *NEWLINES each time a newline is passed.
7252 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
7253 Set *COMMENTS to 1 if a comment is seen. */
7256 macarg1 (start
, limit
, depthptr
, newlines
, comments
, rest_args
)
7258 register U_CHAR
*limit
;
7259 int *depthptr
, *newlines
, *comments
;
7262 register U_CHAR
*bp
= start
;
7264 while (bp
< limit
) {
7270 if (--(*depthptr
) < 0)
7274 /* Traditionally, backslash makes following char not special. */
7275 if (bp
+ 1 < limit
&& traditional
)
7278 /* But count source lines anyway. */
7287 if (bp
[1] == '\\' && bp
[2] == '\n')
7288 newline_fix (bp
+ 1);
7289 if ((cplusplus
|| objc
) && bp
[1] == '/') {
7292 while (bp
< limit
&& *bp
++ != '\n') ;
7296 if (bp
[1] != '*' || bp
+ 1 >= limit
)
7300 while (bp
+ 1 < limit
) {
7302 && bp
[1] == '\\' && bp
[2] == '\n')
7303 newline_fix (bp
+ 1);
7304 if (bp
[0] == '*' && bp
[1] == '/')
7306 if (*bp
== '\n') ++*newlines
;
7314 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
7319 while (*bp
== '\\' && bp
[1] == '\n') {
7322 } else if (*bp
== '\n') {
7331 /* if we've returned to lowest level and we aren't absorbing all args */
7332 if ((*depthptr
) == 0 && rest_args
== 0)
7342 /* Discard comments and duplicate newlines
7343 in the string of length LENGTH at START,
7344 except inside of string constants.
7345 The string is copied into itself with its beginning staying fixed.
7347 NEWLINES is the number of newlines that must be duplicated.
7348 We assume that that much extra space is available past the end
7352 discard_comments (start
, length
, newlines
)
7357 register U_CHAR
*ibp
;
7358 register U_CHAR
*obp
;
7359 register U_CHAR
*limit
;
7362 /* If we have newlines to duplicate, copy everything
7363 that many characters up. Then, in the second part,
7364 we will have room to insert the newlines
7366 NEWLINES may actually be too large, because it counts
7367 newlines in string constants, and we don't duplicate those.
7368 But that does no harm. */
7370 ibp
= start
+ length
;
7371 obp
= ibp
+ newlines
;
7373 while (limit
!= ibp
)
7377 ibp
= start
+ newlines
;
7378 limit
= start
+ length
+ newlines
;
7381 while (ibp
< limit
) {
7382 *obp
++ = c
= *ibp
++;
7385 /* Duplicate the newline. */
7397 if (*ibp
== '\\' && ibp
[1] == '\n')
7399 /* Delete any comment. */
7400 if ((cplusplus
|| objc
) && ibp
[0] == '/') {
7403 while (ibp
< limit
&& *ibp
++ != '\n') ;
7406 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
7410 while (ibp
+ 1 < limit
) {
7412 && ibp
[1] == '\\' && ibp
[2] == '\n')
7413 newline_fix (ibp
+ 1);
7414 if (ibp
[0] == '*' && ibp
[1] == '/')
7423 /* Notice and skip strings, so that we don't
7424 think that comments start inside them,
7425 and so we don't duplicate newlines in them. */
7428 while (ibp
< limit
) {
7429 *obp
++ = c
= *ibp
++;
7432 if (c
== '\n' && quotec
== '\'')
7434 if (c
== '\\' && ibp
< limit
) {
7435 while (*ibp
== '\\' && ibp
[1] == '\n')
7448 /* Delete newlines in the string of length LENGTH at START, except inside
7449 of string constants. The string is copied into itself with its beginning
7453 delete_newlines (start
, length
)
7457 register U_CHAR
*ibp
;
7458 register U_CHAR
*obp
;
7459 register U_CHAR
*limit
;
7463 limit
= start
+ length
;
7466 while (ibp
< limit
) {
7467 *obp
++ = c
= *ibp
++;
7470 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
7471 output. Skip past the newline and its duplicate. */
7481 /* Notice and skip strings, so that we don't delete newlines in them. */
7484 while (ibp
< limit
) {
7485 *obp
++ = c
= *ibp
++;
7488 if (c
== '\n' && quotec
== '\'')
7500 * error - print error message and increment count of errors.
7504 error (msg
, arg1
, arg2
, arg3
)
7508 FILE_BUF
*ip
= NULL
;
7510 print_containing_files ();
7512 for (i
= indepth
; i
>= 0; i
--)
7513 if (instack
[i
].fname
!= NULL
) {
7519 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7520 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7521 fprintf (stderr
, "\n");
7525 /* Error including a message from `errno'. */
7528 error_from_errno (name
)
7532 FILE_BUF
*ip
= NULL
;
7534 print_containing_files ();
7536 for (i
= indepth
; i
>= 0; i
--)
7537 if (instack
[i
].fname
!= NULL
) {
7543 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7545 if (errno
< sys_nerr
)
7546 fprintf (stderr
, "%s: %s\n", name
, sys_errlist
[errno
]);
7548 fprintf (stderr
, "%s: undocumented I/O error\n", name
);
7553 /* Print error message but don't count it. */
7556 warning (msg
, arg1
, arg2
, arg3
)
7560 FILE_BUF
*ip
= NULL
;
7562 if (inhibit_warnings
)
7565 if (warnings_are_errors
)
7568 print_containing_files ();
7570 for (i
= indepth
; i
>= 0; i
--)
7571 if (instack
[i
].fname
!= NULL
) {
7577 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7578 fprintf (stderr
, "warning: ");
7579 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7580 fprintf (stderr
, "\n");
7584 error_with_line (line
, msg
, arg1
, arg2
, arg3
)
7589 FILE_BUF
*ip
= NULL
;
7591 print_containing_files ();
7593 for (i
= indepth
; i
>= 0; i
--)
7594 if (instack
[i
].fname
!= NULL
) {
7600 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, line
);
7601 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7602 fprintf (stderr
, "\n");
7606 /* print an error message and maybe count it. */
7609 pedwarn (msg
, arg1
, arg2
, arg3
)
7612 if (pedantic_errors
)
7613 error (msg
, arg1
, arg2
, arg3
);
7615 warning (msg
, arg1
, arg2
, arg3
);
7618 /* Report a warning (or an error if pedantic_errors)
7619 giving specified file name and line number, not current. */
7622 pedwarn_with_file_and_line (file
, line
, msg
, arg1
, arg2
, arg3
)
7628 if (!pedantic_errors
&& inhibit_warnings
)
7631 fprintf (stderr
, "%s:%d: ", file
, line
);
7632 if (pedantic_errors
|| warnings_are_errors
)
7634 if (!pedantic_errors
)
7635 fprintf (stderr
, "warning: ");
7636 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7637 fprintf (stderr
, "\n");
7640 /* Print the file names and line numbers of the #include
7641 commands which led to the current file. */
7644 print_containing_files ()
7646 FILE_BUF
*ip
= NULL
;
7650 /* If stack of files hasn't changed since we last printed
7651 this info, don't repeat it. */
7652 if (last_error_tick
== input_file_stack_tick
)
7655 for (i
= indepth
; i
>= 0; i
--)
7656 if (instack
[i
].fname
!= NULL
) {
7661 /* Give up if we don't find a source file. */
7665 /* Find the other, outer source files. */
7666 for (i
--; i
>= 0; i
--)
7667 if (instack
[i
].fname
!= NULL
) {
7671 fprintf (stderr
, "In file included");
7673 fprintf (stderr
, ",");
7676 fprintf (stderr
, " from %s:%d", ip
->nominal_fname
, ip
->lineno
);
7679 fprintf (stderr
, ":\n");
7681 /* Record we have printed the status as of this time. */
7682 last_error_tick
= input_file_stack_tick
;
7685 /* Return the line at which an error occurred.
7686 The error is not necessarily associated with the current spot
7687 in the input stack, so LINE says where. LINE will have been
7688 copied from ip->lineno for the current input level.
7689 If the current level is for a file, we return LINE.
7690 But if the current level is not for a file, LINE is meaningless.
7691 In that case, we return the lineno of the innermost file. */
7694 line_for_error (line
)
7700 for (i
= indepth
; i
>= 0; ) {
7701 if (instack
[i
].fname
!= 0)
7706 line1
= instack
[i
].lineno
;
7714 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
7716 * As things stand, nothing is ever placed in the output buffer to be
7717 * removed again except when it's KNOWN to be part of an identifier,
7718 * so flushing and moving down everything left, instead of expanding,
7722 /* You might think void was cleaner for the return type,
7723 but that would get type mismatch in check_expand in strict ANSI. */
7725 grow_outbuf (obuf
, needed
)
7726 register FILE_BUF
*obuf
;
7727 register int needed
;
7732 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
7735 /* Make it at least twice as big as it is now. */
7737 /* Make it have at least 150% of the free space we will need. */
7738 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
7739 if (minsize
> obuf
->length
)
7740 obuf
->length
= minsize
;
7742 if ((p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
)) == NULL
)
7745 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
7751 /* Symbol table for macro names and special symbols */
7754 * install a name in the main hash table, even if it is already there.
7755 * name stops with first non alphanumeric, except leading '#'.
7756 * caller must check against redefinition if that is desired.
7757 * delete_macro () removes things installed by install () in fifo order.
7758 * this is important because of the `defined' special symbol used
7759 * in #if, and also if pushdef/popdef directives are ever implemented.
7761 * If LEN is >= 0, it is the length of the name.
7762 * Otherwise, compute the length by scanning the entire name.
7764 * If HASH is >= 0, it is the precomputed hash code.
7765 * Otherwise, compute the hash code.
7768 install (name
, len
, type
, value
, hash
)
7771 enum node_type type
;
7774 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
7776 register HASHNODE
*hp
;
7777 register int i
, bucket
;
7778 register U_CHAR
*p
, *q
;
7782 while (is_idchar
[*p
])
7788 hash
= hashf (name
, len
, HASHSIZE
);
7790 i
= sizeof (HASHNODE
) + len
+ 1;
7791 hp
= (HASHNODE
*) xmalloc (i
);
7793 hp
->bucket_hdr
= &hashtab
[bucket
];
7794 hp
->next
= hashtab
[bucket
];
7795 hashtab
[bucket
] = hp
;
7797 if (hp
->next
!= NULL
)
7798 hp
->next
->prev
= hp
;
7801 hp
->value
.ival
= value
;
7802 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
7805 for (i
= 0; i
< len
; i
++)
7812 * find the most recent hash node for name name (ending with first
7813 * non-identifier char) installed by install
7815 * If LEN is >= 0, it is the length of the name.
7816 * Otherwise, compute the length by scanning the entire name.
7818 * If HASH is >= 0, it is the precomputed hash code.
7819 * Otherwise, compute the hash code.
7822 lookup (name
, len
, hash
)
7827 register U_CHAR
*bp
;
7828 register HASHNODE
*bucket
;
7831 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
7836 hash
= hashf (name
, len
, HASHSIZE
);
7838 bucket
= hashtab
[hash
];
7840 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
7842 bucket
= bucket
->next
;
7848 * Delete a hash node. Some weirdness to free junk from macros.
7849 * More such weirdness will have to be added if you define more hash
7850 * types that need it.
7853 /* Note that the DEFINITION of a macro is removed from the hash table
7854 but its storage is not freed. This would be a storage leak
7855 except that it is not reasonable to keep undefining and redefining
7856 large numbers of macros many times.
7857 In any case, this is necessary, because a macro can be #undef'd
7858 in the middle of reading the arguments to a call to it.
7859 If #undef freed the DEFINITION, that would crash. */
7866 if (hp
->prev
!= NULL
)
7867 hp
->prev
->next
= hp
->next
;
7868 if (hp
->next
!= NULL
)
7869 hp
->next
->prev
= hp
->prev
;
7871 /* make sure that the bucket chain header that
7872 the deleted guy was on points to the right thing afterwards. */
7873 if (hp
== *hp
->bucket_hdr
)
7874 *hp
->bucket_hdr
= hp
->next
;
7877 if (hp
->type
== T_MACRO
) {
7878 DEFINITION
*d
= hp
->value
.defn
;
7879 struct reflist
*ap
, *nextap
;
7881 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
7892 * return hash function on name. must be compatible with the one
7893 * computed a step at a time, elsewhere
7896 hashf (name
, len
, hashsize
)
7897 register U_CHAR
*name
;
7904 r
= HASHSTEP (r
, *name
++);
7906 return MAKE_POS (r
) % hashsize
;
7910 /* Dump the definition of a single macro HP to OF. */
7912 dump_single_macro (hp
, of
)
7913 register HASHNODE
*hp
;
7916 register DEFINITION
*defn
= hp
->value
.defn
;
7922 /* Print the definition of the macro HP. */
7924 fprintf (of
, "#define %s", hp
->name
);
7926 if (defn
->nargs
>= 0) {
7930 for (i
= 0; i
< defn
->nargs
; i
++) {
7931 dump_arg_n (defn
, i
, of
);
7932 if (i
+ 1 < defn
->nargs
)
7942 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
7943 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
7944 if (ap
->nchars
!= 0)
7946 offset
+= ap
->nchars
;
7949 if (ap
->raw_before
&& !concat
)
7950 fprintf (of
, " ## ");
7952 dump_arg_n (defn
, ap
->argno
, of
);
7953 if (ap
->raw_after
) {
7954 fprintf (of
, " ## ");
7958 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
7962 /* Dump all macro definitions as #defines to stdout. */
7969 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
7970 register HASHNODE
*hp
;
7972 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
7973 if (hp
->type
== T_MACRO
)
7974 dump_single_macro (hp
, stdout
);
7979 /* Output to OF a substring of a macro definition.
7980 BASE is the beginning of the definition.
7981 Output characters START thru LENGTH.
7982 Discard newlines outside of strings, thus
7983 converting funny-space markers to ordinary spaces. */
7986 dump_defn_1 (base
, start
, length
, of
)
7992 U_CHAR
*p
= base
+ start
;
7993 U_CHAR
*limit
= base
+ start
+ length
;
7998 else if (*p
== '\"' || *p
=='\'') {
7999 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, 0, 0, 0);
8000 fwrite (p
, p1
- p
, 1, of
);
8007 /* Print the name of argument number ARGNUM of macro definition DEFN
8009 Recall that DEFN->args.argnames contains all the arg names
8010 concatenated in reverse order with comma-space in between. */
8013 dump_arg_n (defn
, argnum
, of
)
8018 register U_CHAR
*p
= defn
->args
.argnames
;
8019 while (argnum
+ 1 < defn
->nargs
) {
8020 p
= (U_CHAR
*) index (p
, ' ') + 1;
8024 while (*p
&& *p
!= ',') {
8030 /* Initialize syntactic classifications of characters. */
8033 initialize_char_syntax ()
8038 * Set up is_idchar and is_idstart tables. These should be
8039 * faster than saying (is_alpha (c) || c == '_'), etc.
8040 * Set up these things before calling any routines tthat
8043 for (i
= 'a'; i
<= 'z'; i
++) {
8044 is_idchar
[i
- 'a' + 'A'] = 1;
8046 is_idstart
[i
- 'a' + 'A'] = 1;
8049 for (i
= '0'; i
<= '9'; i
++)
8052 is_idstart
['_'] = 1;
8053 is_idchar
['$'] = dollars_in_ident
;
8054 is_idstart
['$'] = dollars_in_ident
;
8056 /* horizontal space table */
8057 is_hor_space
[' '] = 1;
8058 is_hor_space
['\t'] = 1;
8059 is_hor_space
['\v'] = 1;
8060 is_hor_space
['\f'] = 1;
8061 is_hor_space
['\r'] = 1;
8071 /* Initialize the built-in macros. */
8074 initialize_builtins (inp
, outp
)
8078 install ("__LINE__", -1, T_SPECLINE
, 0, -1);
8079 install ("__DATE__", -1, T_DATE
, 0, -1);
8080 install ("__FILE__", -1, T_FILE
, 0, -1);
8081 install ("__BASE_FILE__", -1, T_BASE_FILE
, 0, -1);
8082 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, -1);
8083 install ("__VERSION__", -1, T_VERSION
, 0, -1);
8084 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, -1);
8085 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, -1);
8086 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, -1);
8087 install ("__TIME__", -1, T_TIME
, 0, -1);
8089 install ("__STDC__", -1, T_CONST
, STDC_VALUE
, -1);
8091 install ("__OBJC__", -1, T_CONST
, 1, -1);
8092 /* This is supplied using a -D by the compiler driver
8093 so that it is present only when truly compiling with GNU C. */
8094 /* install ("__GNUC__", -1, T_CONST, 2, -1); */
8098 char directive
[2048];
8099 register struct directive
*dp
= &directive_table
[0];
8100 struct tm
*timebuf
= timestamp ();
8102 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
8103 instack
[0].nominal_fname
);
8104 output_line_command (inp
, outp
, 0, same_file
);
8105 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8107 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
8108 output_line_command (inp
, outp
, 0, same_file
);
8109 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8111 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
8112 output_line_command (inp
, outp
, 0, same_file
);
8113 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8115 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
8116 output_line_command (inp
, outp
, 0, same_file
);
8117 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8119 sprintf (directive
, " __WCHAR_TYPE__ %s\n", WCHAR_TYPE
);
8120 output_line_command (inp
, outp
, 0, same_file
);
8121 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8123 sprintf (directive
, " __WCHAR_TYPE__ %s\n", WCHAR_TYPE
);
8124 output_line_command (inp
, outp
, 0, same_file
);
8125 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8127 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
8128 monthnames
[timebuf
->tm_mon
],
8129 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
8130 output_line_command (inp
, outp
, 0, same_file
);
8131 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8133 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
8134 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
8135 output_line_command (inp
, outp
, 0, same_file
);
8136 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8140 sprintf (directive
, " __STDC__ 1");
8141 output_line_command (inp
, outp
, 0, same_file
);
8142 pass_thru_directive (directive
, &directive
[strlen (directive
)],
8147 sprintf (directive
, " __OBJC__ 1");
8148 output_line_command (inp
, outp
, 0, same_file
);
8149 pass_thru_directive (directive
, &directive
[strlen (directive
)],
8156 * process a given definition string, for initialization
8157 * If STR is just an identifier, define it with value 1.
8158 * If STR has anything after the identifier, then it should
8159 * be identifier=definition.
8163 make_definition (str
, op
)
8168 struct directive
*kt
;
8173 if (!is_idstart
[*p
]) {
8174 error ("malformed option `-D %s'", str
);
8177 while (is_idchar
[*++p
])
8180 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
8181 strcpy ((char *)buf
, str
);
8182 strcat ((char *)buf
, " 1");
8183 } else if (*p
!= '=') {
8184 error ("malformed option `-D %s'", str
);
8188 /* Copy the entire option so we can modify it. */
8189 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
8190 strncpy (buf
, str
, p
- str
);
8191 /* Change the = to a space. */
8193 /* Scan for any backslash-newline and remove it. */
8197 if (*p
== '\\' && p
[1] == '\n')
8199 /* Change newline chars into newline-markers. */
8200 else if (*p
== '\n')
8212 ip
= &instack
[++indepth
];
8213 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
8215 ip
->buf
= ip
->bufp
= buf
;
8216 ip
->length
= strlen (buf
);
8220 ip
->if_stack
= if_stack
;
8221 ip
->system_header_p
= 0;
8223 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
8226 do_define (buf
, buf
+ strlen (buf
) , op
, kt
);
8230 /* JF, this does the work for the -U option */
8233 make_undef (str
, op
)
8238 struct directive
*kt
;
8240 ip
= &instack
[++indepth
];
8241 ip
->nominal_fname
= ip
->fname
= "*undef*";
8243 ip
->buf
= ip
->bufp
= str
;
8244 ip
->length
= strlen (str
);
8248 ip
->if_stack
= if_stack
;
8249 ip
->system_header_p
= 0;
8251 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
8254 do_undef (str
, str
+ strlen (str
), op
, kt
);
8258 /* Process the string STR as if it appeared as the body of a #assert.
8259 OPTION is the option name for which STR was the argument. */
8262 make_assertion (option
, str
)
8267 struct directive
*kt
;
8268 U_CHAR
*buf
, *p
, *q
;
8270 /* Copy the entire option so we can modify it. */
8271 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
8272 strcpy ((char *) buf
, str
);
8273 /* Scan for any backslash-newline and remove it. */
8276 if (*p
== '\\' && p
[1] == '\n')
8284 if (!is_idstart
[*p
]) {
8285 error ("malformed option `%s %s'", option
, str
);
8288 while (is_idchar
[*++p
])
8290 while (*p
== ' ' || *p
== '\t') p
++;
8291 if (! (*p
== 0 || *p
== '(')) {
8292 error ("malformed option `%s %s'", option
, str
);
8296 ip
= &instack
[++indepth
];
8297 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
8299 ip
->buf
= ip
->bufp
= buf
;
8300 ip
->length
= strlen (buf
);
8304 ip
->if_stack
= if_stack
;
8305 ip
->system_header_p
= 0;
8307 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
8310 /* pass NULL as output ptr to do_define since we KNOW it never
8311 does any output.... */
8312 do_assert (buf
, buf
+ strlen (buf
) , NULL
, kt
);
8316 /* Add output to `deps_buffer' for the -M switch.
8317 STRING points to the text to be output.
8318 SIZE is the number of bytes, or 0 meaning output until a null.
8319 Outputting the empty string breaks the line if it is long enough. */
8322 deps_output (string
, size
)
8327 size
= strlen (string
);
8329 #ifndef MAX_OUTPUT_COLUMNS
8330 #define MAX_OUTPUT_COLUMNS 75
8332 if (size
== 0 && deps_column
!= 0
8333 && size
+ deps_column
> MAX_OUTPUT_COLUMNS
) {
8334 deps_output ("\\\n ", 0);
8338 if (deps_size
+ size
+ 1 > deps_allocated_size
) {
8339 deps_allocated_size
= deps_size
+ size
+ 50;
8340 deps_allocated_size
*= 2;
8341 deps_buffer
= (char *) xrealloc (deps_buffer
, deps_allocated_size
);
8343 bcopy (string
, &deps_buffer
[deps_size
], size
);
8345 deps_column
+= size
;
8346 deps_buffer
[deps_size
] = 0;
8349 #if defined(USG) || defined(VMS)
8355 register unsigned length
;
8359 long max_str
= 65535;
8361 while (length
> max_str
) {
8362 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
8366 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &length
, b
);
8368 while (length
-- > 0)
8370 #endif /* not VMS */
8374 bcopy (b1
, b2
, length
)
8377 register unsigned length
;
8380 long max_str
= 65535;
8382 while (length
> max_str
) {
8383 (void) LIB$
MOVC3 (&max_str
, b1
, b2
);
8388 (void) LIB$
MOVC3 (&length
, b1
, b2
);
8390 while (length
-- > 0)
8392 #endif /* not VMS */
8396 bcmp (b1
, b2
, length
) /* This could be a macro! */
8399 register unsigned length
;
8402 struct dsc$descriptor_s src1
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b1
};
8403 struct dsc$descriptor_s src2
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b2
};
8405 return STR$
COMPARE (&src1
, &src2
);
8407 while (length
-- > 0)
8412 #endif /* not VMS */
8414 #endif /* not BSTRING */
8415 #endif /* USG or VMS */
8424 fprintf (stderr
, "%s: ", progname
);
8425 fprintf (stderr
, str
, arg
);
8426 fprintf (stderr
, "\n");
8427 exit (FAILURE_EXIT_CODE
);
8430 /* More 'friendly' abort that prints the line and file.
8431 config.h can #define abort fancy_abort if you like that sort of thing. */
8436 fatal ("Internal gcc abort.");
8440 perror_with_name (name
)
8443 fprintf (stderr
, "%s: ", progname
);
8444 if (errno
< sys_nerr
)
8445 fprintf (stderr
, "%s: %s\n", name
, sys_errlist
[errno
]);
8447 fprintf (stderr
, "%s: undocumented I/O error\n", name
);
8452 pfatal_with_name (name
)
8455 perror_with_name (name
);
8459 exit (FAILURE_EXIT_CODE
);
8467 fatal ("Memory exhausted.");
8475 register char *ptr
= (char *) malloc (size
);
8476 if (ptr
!= 0) return (ptr
);
8483 xrealloc (old
, size
)
8487 register char *ptr
= (char *) realloc (old
, size
);
8488 if (ptr
!= 0) return (ptr
);
8495 xcalloc (number
, size
)
8496 unsigned number
, size
;
8498 register unsigned total
= number
* size
;
8499 register char *ptr
= (char *) malloc (total
);
8504 /* It's not too long, so loop, zeroing by longs.
8505 It must be safe because malloc values are always well aligned. */
8506 register long *zp
= (long *) ptr
;
8507 register long *zl
= (long *) (ptr
+ total
- 4);
8508 register int i
= total
- 4;
8527 unsigned size
= strlen (input
);
8528 char *output
= xmalloc (size
+ 1);
8529 strcpy (output
, input
);
8533 /* Get the file-mode and data size of the file open on FD
8534 and store them in *MODE_POINTER and *SIZE_POINTER. */
8537 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
8540 long int *size_pointer
;
8544 if (fstat (fd
, &sbuf
) < 0) return (-1);
8545 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
8546 if (size_pointer
) *size_pointer
= sbuf
.st_size
;
8552 /* Under VMS we need to fix up the "include" specification
8553 filename so that everything following the 1st slash is
8554 changed into its correct VMS file specification. */
8557 hack_vms_include_specification (fname
)
8560 register char *cp
, *cp1
, *cp2
;
8561 int f
, check_filename_before_returning
, no_prefix_seen
;
8564 check_filename_before_returning
= 0;
8567 /* Ignore leading "./"s */
8568 while (fname
[0] == '.' && fname
[1] == '/') {
8569 strcpy (fname
, fname
+2);
8570 no_prefix_seen
= 1; /* mark this for later */
8572 /* Look for the boundary between the VMS and UNIX filespecs */
8573 cp
= rindex (fname
, ']'); /* Look for end of dirspec. */
8574 if (cp
== 0) cp
= rindex (fname
, '>'); /* ... Ditto */
8575 if (cp
== 0) cp
= rindex (fname
, ':'); /* Look for end of devspec. */
8579 cp
= index (fname
, '/'); /* Look for the "/" */
8582 cp2
= Local
; /* initialize */
8584 /* We are trying to do a number of things here. First of all, we are
8585 trying to hammer the filenames into a standard format, such that later
8586 processing can handle them.
8588 If the file name contains something like [dir.], then it recognizes this
8589 as a root, and strips the ".]". Later processing will add whatever is
8590 needed to get things working properly.
8592 If no device is specified, then the first directory name is taken to be
8593 a device name (or a rooted logical). */
8595 /* See if we found that 1st slash */
8596 if (cp
== 0) return; /* Nothing to do!!! */
8597 if (*cp
!= '/') return; /* Nothing to do!!! */
8598 /* Point to the UNIX filename part (which needs to be fixed!) */
8600 /* If the directory spec is not rooted, we can just copy
8601 the UNIX filename part and we are done */
8602 if (((cp
- fname
) > 1) && ((cp
[-1] == ']') || (cp
[-1] == '>'))) {
8603 if (cp
[-2] != '.') {
8605 * The VMS part ends in a `]', and the preceding character is not a `.'.
8606 * We strip the `]', and then splice the two parts of the name in the
8607 * usual way. Given the default locations for include files in cccp.c,
8608 * we will only use this code if the user specifies alternate locations
8609 * with the /include (-I) switch on the command line. */
8610 cp
-= 1; /* Strip "]" */
8611 cp1
--; /* backspace */
8614 * The VMS part has a ".]" at the end, and this will not do. Later
8615 * processing will add a second directory spec, and this would be a syntax
8616 * error. Thus we strip the ".]", and thus merge the directory specs.
8617 * We also backspace cp1, so that it points to a '/'. This inhibits the
8618 * generation of the 000000 root directory spec (which does not belong here
8621 cp
-= 2; /* Strip ".]" */
8622 cp1
--; }; /* backspace */
8625 /* We drop in here if there is no VMS style directory specification yet.
8626 * If there is no device specification either, we make the first dir a
8627 * device and try that. If we do not do this, then we will be essentially
8628 * searching the users default directory (as if they did a #include "asdf.h").
8630 * Then all we need to do is to push a '[' into the output string. Later
8631 * processing will fill this in, and close the bracket.
8633 if(cp
[-1] != ':') *cp2
++ = ':'; /* dev not in spec. take first dir */
8634 *cp2
++ = '['; /* Open the directory specification */
8637 /* at this point we assume that we have the device spec, and (at least
8638 the opening "[" for a directory specification. We may have directories
8639 specified already */
8641 /* If there are no other slashes then the filename will be
8642 in the "root" directory. Otherwise, we need to add
8643 directory specifications. */
8644 if (index (cp1
, '/') == 0) {
8645 /* Just add "000000]" as the directory string */
8646 strcpy (cp2
, "000000]");
8647 cp2
+= strlen (cp2
);
8648 check_filename_before_returning
= 1; /* we might need to fool with this later */
8650 /* As long as there are still subdirectories to add, do them. */
8651 while (index (cp1
, '/') != 0) {
8652 /* If this token is "." we can ignore it */
8653 if ((cp1
[0] == '.') && (cp1
[1] == '/')) {
8657 /* Add a subdirectory spec. Do not duplicate "." */
8658 if (cp2
[-1] != '.' && cp2
[-1] != '[' && cp2
[-1] != '<')
8660 /* If this is ".." then the spec becomes "-" */
8661 if ((cp1
[0] == '.') && (cp1
[1] == '.') && (cp
[2] == '/')) {
8662 /* Add "-" and skip the ".." */
8667 /* Copy the subdirectory */
8668 while (*cp1
!= '/') *cp2
++= *cp1
++;
8669 cp1
++; /* Skip the "/" */
8671 /* Close the directory specification */
8672 if(cp2
[-1] == '.') /* no trailing periods */
8676 /* Now add the filename */
8677 while (*cp1
) *cp2
++ = *cp1
++;
8679 /* Now append it to the original VMS spec. */
8682 /* If we put a [000000] in the filename, try to open it first. If this fails,
8683 remove the [000000], and return that name. This provides flexibility
8684 to the user in that they can use both rooted and non-rooted logical names
8685 to point to the location of the file. */
8687 if (check_filename_before_returning
&& no_prefix_seen
) {
8688 f
= open (fname
, O_RDONLY
, 0666);
8690 /* The file name is OK as it is, so return it as is. */
8694 /* The filename did not work. Try to remove the [000000] from the name,
8696 cp
= index (fname
, '[');
8697 cp2
= index (fname
, ']') + 1;
8698 strcpy (cp
, cp2
); /* this gets rid of it */
8706 /* These are the read/write replacement routines for
8707 VAX-11 "C". They make read/write behave enough
8708 like their UNIX counterparts that CCCP will work */
8711 read (fd
, buf
, size
)
8716 #undef read /* Get back the REAL read routine */
8718 register int total
= 0;
8720 /* Read until the buffer is exhausted */
8722 /* Limit each read to 32KB */
8723 i
= (size
> (32*1024)) ? (32*1024) : size
;
8724 i
= read (fd
, buf
, i
);
8726 if (i
== 0) return (total
);
8729 /* Account for this read */
8738 write (fd
, buf
, size
)
8743 #undef write /* Get back the REAL write routine */
8747 /* Limit individual writes to 32Kb */
8750 j
= (i
> (32*1024)) ? (32*1024) : i
;
8751 if (write (fd
, buf
, j
) < 0) return (-1);
8752 /* Account for the data written */
8759 /* The following wrapper functions supply additional arguments to the VMS
8760 I/O routines to optimize performance with file handling. The arguments
8762 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
8763 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
8764 "fop=tef"- Truncate unused portions of file when closing file.
8765 "shr=nil"- Disallow file sharing while file is open.
8769 freopen (fname
, type
, oldfile
)
8774 #undef freopen /* Get back the REAL fopen routine */
8775 if (strcmp (type
, "w") == 0)
8776 return freopen (fname
, type
, oldfile
, "mbc=16", "deq=64", "fop=tef", "shr=nil");
8777 return freopen (fname
, type
, oldfile
, "mbc=16");
8785 #undef fopen /* Get back the REAL fopen routine */
8786 if (strcmp (type
, "w") == 0)
8787 return fopen (fname
, type
, "mbc=16", "deq=64", "fop=tef", "shr=nil");
8788 return fopen (fname
, type
, "mbc=16");
8792 open (fname
, flags
, prot
)
8797 #undef open /* Get back the REAL open routine */
8798 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");