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 /* In case config.h defines these. */
61 #include <sys/types.h>
69 #include <sys/time.h> /* for __DATE__ and __TIME__ */
70 #include <sys/resource.h>
73 #define rindex strrchr
79 extern char *index ();
80 extern char *rindex ();
82 /* VMS-specific definitions */
85 #include <errno.h> /* This defines "errno" properly */
86 #include <perror.h> /* This defines sys_errlist/sys_nerr properly */
88 #define O_RDONLY 0 /* Open arg for Read/Only */
89 #define O_WRONLY 1 /* Open arg for Write/Only */
90 #define read(fd,buf,size) VMS_read(fd,buf,size)
91 #define write(fd,buf,size) VMS_write(fd,buf,size)
92 #define open(fname,mode,prot) VMS_open(fname,mode,prot)
93 #define fopen(fname,mode) VMS_fopen(fname,mode)
94 #define freopen(fname,mode,ofile) VMS_freopen(fname,mode,ofile)
95 static int VMS_read ();
96 static int VMS_write ();
97 static int VMS_open ();
98 static FILE * VMS_fopen ();
99 static FILE * VMS_freopen ();
100 static void hack_vms_include_specification ();
101 typedef struct { unsigned :16, :16, :16; } vms_ino_t
;
102 #define ino_t vms_ino_t
104 #define BSTRING /* VMS/GCC supplies the bstring routines */
105 #endif /* __GNUC__ */
114 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
115 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
118 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
121 /* Exported declarations. */
127 /* External declarations. */
129 extern char *getenv ();
130 extern FILE *fdopen ();
131 extern char *version_string
;
132 extern struct tm
*localtime ();
134 extern char *sys_errlist
[];
140 /* Forward declarations. */
147 #if defined(USG) || defined(VMS)
155 /* These functions are declared to return int instead of void since they
156 are going to be placed in a table and some old compilers have trouble with
157 pointers to functions returning void. */
159 static int do_define ();
160 static int do_line ();
161 static int do_include ();
162 static int do_undef ();
163 static int do_error ();
164 static int do_pragma ();
165 static int do_ident ();
167 static int do_xifdef ();
168 static int do_else ();
169 static int do_elif ();
170 static int do_endif ();
171 static int do_sccs ();
172 static int do_once ();
173 static int do_assert ();
174 static int do_unassert ();
175 static int do_warning ();
177 static void add_import ();
178 static void deps_output ();
179 static void make_undef ();
180 static void make_definition ();
181 static void make_assertion ();
182 static void path_include ();
183 static void initialize_builtins ();
184 static void initialize_char_syntax ();
185 static void dump_arg_n ();
186 static void dump_defn_1 ();
187 static void delete_macro ();
188 static void trigraph_pcp ();
189 static void rescan ();
190 static void finclude ();
191 static void validate_else ();
192 static int comp_def_part ();
193 static void error_from_errno ();
194 static void error_with_line ();
196 static void pedwarn_with_file_and_line ();
197 static void fatal ();
199 static void pfatal_with_name ();
200 static void perror_with_name ();
201 static void print_containing_files ();
202 static int lookup_import ();
203 static int lookup_include ();
204 static int check_preconditions ();
205 static void pcfinclude ();
206 static void pcstring_used ();
207 static void write_output ();
208 static int check_macro_name ();
209 static int compare_defs ();
210 static int compare_token_lists ();
211 static int eval_if_expression ();
212 static int discard_comments ();
213 static int delete_newlines ();
214 static int line_for_error ();
216 static int file_size_and_mode ();
218 static struct arglist
*read_token_list ();
219 static void free_token_list ();
221 static struct hashnode
*install ();
222 struct hashnode
*lookup ();
224 static struct assertion_hashnode
*assertion_install ();
225 static struct assertion_hashnode
*assertion_lookup ();
227 static char *xrealloc ();
228 static char *xcalloc ();
229 static char *savestring ();
231 static void delete_assertion ();
232 static void macroexpand ();
233 static void dump_all_macros ();
234 static void conditional_skip ();
235 static void skip_if_group ();
236 static void output_line_command ();
238 /* Last arg to output_line_command. */
239 enum file_change_code
{same_file
, enter_file
, leave_file
};
241 static int grow_outbuf ();
242 static int handle_directive ();
243 static void memory_full ();
245 static U_CHAR
*macarg1 ();
246 static char *macarg ();
248 static U_CHAR
*skip_to_end_of_comment ();
249 static U_CHAR
*skip_quoted_string ();
250 static U_CHAR
*skip_paren_group ();
252 static char *check_precompiled ();
253 static struct macrodef
create_definition ();
254 static void dump_single_macro ();
256 #ifndef FAILURE_EXIT_CODE
257 #define FAILURE_EXIT_CODE 33 /* gnu cc command understands this */
260 #ifndef SUCCESS_EXIT_CODE
261 #define SUCCESS_EXIT_CODE 0 /* 0 means success on Unix. */
264 /* Name under which this program was invoked. */
266 static char *progname
;
268 /* Nonzero means handle C++ comment syntax and use
269 extra default include directories for C++. */
271 static int cplusplus
;
273 /* Nonzero means handle #import, for objective C. */
277 /* Nonzero means this is an assembly file, and allow
278 unknown directives, which could be comments. */
282 /* Current maximum length of directory names in the search path
283 for include files. (Altered as we get more of them.) */
285 static int max_include_len
;
287 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
291 /* Nonzero means copy comments into the output file. */
293 static int put_out_comments
= 0;
295 /* Nonzero means don't process the ANSI trigraph sequences. */
297 static int no_trigraphs
= 0;
299 /* Nonzero means print the names of included files rather than
300 the preprocessed output. 1 means just the #include "...",
301 2 means #include <...> as well. */
303 static int print_deps
= 0;
305 /* Nonzero means print names of header files (-H). */
307 static int print_include_names
= 0;
309 /* Nonzero means don't output line number information. */
311 static int no_line_commands
;
313 /* dump_only means inhibit output of the preprocessed text
314 and instead output the definitions of all user-defined
315 macros in a form suitable for use as input to cccp.
316 dump_names means pass #define and the macro name through to output.
317 dump_definitions means pass the whole definition (plus #define) through
320 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
321 dump_macros
= dump_none
;
323 /* Nonzero means pass all #define and #undef directives which we actually
324 process through to the output stream. This feature is used primarily
325 to allow cc1 to record the #defines and #undefs for the sake of
326 debuggers which understand about preprocessor macros, but it may
327 also be useful with -E to figure out how symbols are defined, and
328 where they are defined. */
329 static int debug_output
= 0;
331 /* Holds local startup time. */
332 static struct tm
*timebuf
= NULL
;
334 /* Nonzero indicates special processing used by the pcp program. The
335 special effects of this mode are:
337 Inhibit all macro expansion, except those inside #if directives.
339 Process #define directives normally, and output their contents
342 Output preconditions to pcp_outfile indicating all the relevant
343 preconditions for use of this file in a later cpp run.
345 static FILE *pcp_outfile
;
347 /* Nonzero means we are inside an IF during a -pcp run. In this mode
348 macro expansion is done, and preconditions are output for all macro
349 uses requiring them. */
350 static int pcp_inside_if
;
352 /* Nonzero means never to include precompiled files. */
353 static int no_precomp
;
355 /* Nonzero means give all the error messages the ANSI standard requires. */
359 /* Nonzero means try to make failure to fit ANSI C an error. */
361 static int pedantic_errors
;
363 /* Nonzero means don't print warning messages. -w. */
365 static int inhibit_warnings
= 0;
367 /* Nonzero means warn if slash-star appears in a comment. */
369 static int warn_comments
;
371 /* Nonzero means warn if a macro argument is (or would be)
372 stringified with -traditional. */
374 static int warn_stringify
;
376 /* Nonzero means warn if there are any trigraphs. */
378 static int warn_trigraphs
;
380 /* Nonzero means warn if #import is used. */
382 static int warn_import
= 1;
384 /* Nonzero means turn warnings into errors. */
386 static int warnings_are_errors
;
388 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
392 /* Nonzero causes output not to be done,
393 but directives such as #define that have side effects
396 static int no_output
;
398 /* Nonzero means that we have finished processing the command line options.
399 This flag is used to decide whether or not to issue certain errors
402 static int done_initializing
= 0;
404 /* I/O buffer structure.
405 The `fname' field is nonzero for source files and #include files
406 and for the dummy text used for -D and -U.
407 It is zero for rescanning results of macro expansion
408 and for expanding macro arguments. */
409 #define INPUT_STACK_MAX 200
410 static struct file_buf
{
412 /* Filename specified with #line command. */
414 /* Record where in the search path this file was found.
415 For #include_next. */
416 struct file_name_list
*dir
;
421 /* Macro that this level is the expansion of.
422 Included so that we can reenable the macro
423 at the end of this level. */
424 struct hashnode
*macro
;
425 /* Value of if_stack at start of this file.
426 Used to prohibit unmatched #endif (etc) in an include file. */
427 struct if_stack
*if_stack
;
428 /* Object to be freed at end of input at this level. */
430 /* True if this is a header file included using <FILENAME>. */
431 char system_header_p
;
432 } instack
[INPUT_STACK_MAX
];
434 static int last_error_tick
; /* Incremented each time we print it. */
435 static int input_file_stack_tick
; /* Incremented when the status changes. */
437 /* Current nesting level of input sources.
438 `instack[indepth]' is the level currently being read. */
439 static int indepth
= -1;
440 #define CHECK_DEPTH(code) \
441 if (indepth >= (INPUT_STACK_MAX - 1)) \
443 error_with_line (line_for_error (instack[indepth].lineno), \
444 "macro or `#include' recursion too deep"); \
448 /* Current depth in #include directives that use <...>. */
449 static int system_include_depth
= 0;
451 typedef struct file_buf FILE_BUF
;
453 /* The output buffer. Its LENGTH field is the amount of room allocated
454 for the buffer, not the number of chars actually present. To get
455 that, subtract outbuf.buf from outbuf.bufp. */
457 #define OUTBUF_SIZE 10 /* initial size of output buffer */
458 static FILE_BUF outbuf
;
460 /* Grow output buffer OBUF points at
461 so it can hold at least NEEDED more chars. */
463 #define check_expand(OBUF, NEEDED) \
464 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
465 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
467 struct file_name_list
469 struct file_name_list
*next
;
471 /* If the following is nonzero, it is a macro name.
472 Don't include the file again if that macro is defined. */
473 U_CHAR
*control_macro
;
476 /* #include "file" looks in source file dir, then stack. */
477 /* #include <file> just looks in the stack. */
478 /* -I directories are added to the end, then the defaults are added. */
479 static struct default_include
{ char *fname
; int cplusplus
; } include_defaults_array
[]
480 #ifdef INCLUDE_DEFAULTS
484 /* Pick up GNU C++ specific include files. */
485 { GPLUSPLUS_INCLUDE_DIR
, 1},
486 { GCC_INCLUDE_DIR
, 0},
488 /* For cross-compilation, this dir name is generated
489 automatically in Makefile.in. */
490 { CROSS_INCLUDE_DIR
, 0 },
491 #else /* not CROSS_COMPILE */
492 { LOCAL_INCLUDE_DIR
, 0},
493 /* Some systems have an extra dir of include files. */
494 #ifdef SYSTEM_INCLUDE_DIR
495 { SYSTEM_INCLUDE_DIR
, 0},
497 { STANDARD_INCLUDE_DIR
, 0},
498 #endif /* not CROSS_COMPILE */
501 #endif /* no INCLUDE_DEFAULTS */
503 /* The code looks at the defaults through this pointer, rather than through
504 the constant structure above. This pointer gets changed if an environment
505 variable specifies other defaults. */
506 static struct default_include
*include_defaults
= include_defaults_array
;
508 static struct file_name_list
*include
= 0; /* First dir to search */
509 /* First dir to search for <file> */
510 static struct file_name_list
*first_bracket_include
= 0;
511 static struct file_name_list
*last_include
= 0; /* Last in chain */
513 /* Chain of include directories to put at the end of the other chain. */
514 static struct file_name_list
*after_include
= 0;
515 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
517 /* List of included files that contained #pragma once. */
518 static struct file_name_list
*dont_repeat_files
= 0;
520 /* List of other included files.
521 If ->control_macro if nonzero, the file had a #ifndef
522 around the entire contents, and ->control_macro gives the macro name. */
523 static struct file_name_list
*all_include_files
= 0;
525 /* Global list of strings read in from precompiled files. This list
526 is kept in the order the strings are read in, with new strings being
527 added at the end through stringlist_tailp. We use this list to output
528 the strings at the end of the run.
530 static STRINGDEF
*stringlist
;
531 static STRINGDEF
**stringlist_tailp
= &stringlist
;
534 /* Structure returned by create_definition */
535 typedef struct macrodef MACRODEF
;
538 struct definition
*defn
;
544 /* Structure allocated for every #define. For a simple replacement
547 nargs = -1, the `pattern' list is null, and the expansion is just
548 the replacement text. Nargs = 0 means a functionlike macro with no args,
550 #define getchar() getc (stdin) .
551 When there are args, the expansion is the replacement text with the
552 args squashed out, and the reflist is a list describing how to
553 build the output from the input: e.g., "3 chars, then the 1st arg,
554 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
555 The chars here come from the expansion. Whatever is left of the
556 expansion after the last arg-occurrence is copied after that arg.
557 Note that the reflist can be arbitrarily long---
558 its length depends on the number of times the arguments appear in
559 the replacement text, not how many args there are. Example:
560 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
562 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
563 where (x, y) means (nchars, argno). */
565 typedef struct definition DEFINITION
;
568 int length
; /* length of expansion string */
569 int predefined
; /* True if the macro was builtin or */
570 /* came from the command line */
572 int line
; /* Line number of definition */
573 char *file
; /* File of definition */
575 struct reflist
*next
;
576 char stringify
; /* nonzero if this arg was preceded by a
578 char raw_before
; /* Nonzero if a ## operator before arg. */
579 char raw_after
; /* Nonzero if a ## operator after arg. */
580 int nchars
; /* Number of literal chars to copy before
581 this arg occurrence. */
582 int argno
; /* Number of arg to substitute (origin-0) */
585 /* Names of macro args, concatenated in reverse order
586 with comma-space between them.
587 The only use of this is that we warn on redefinition
588 if this differs between the old and new definitions. */
593 /* different kinds of things that can appear in the value field
594 of a hash node. Actually, this may be useless now. */
603 /* The structure of a node in the hash table. The hash table
604 has entries for all tokens defined by #define commands (type T_MACRO),
605 plus some special tokens like __LINE__ (these each have their own
606 type, and the appropriate code is run when that type of node is seen.
607 It does not contain control words like "#define", which are recognized
608 by a separate piece of code. */
610 /* different flavors of hash nodes --- also used in keyword table */
612 T_DEFINE
= 1, /* the `#define' keyword */
613 T_INCLUDE
, /* the `#include' keyword */
614 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
615 T_IMPORT
, /* the `#import' keyword */
616 T_IFDEF
, /* the `#ifdef' keyword */
617 T_IFNDEF
, /* the `#ifndef' keyword */
618 T_IF
, /* the `#if' keyword */
619 T_ELSE
, /* `#else' */
620 T_PRAGMA
, /* `#pragma' */
621 T_ELIF
, /* `#elif' */
622 T_UNDEF
, /* `#undef' */
623 T_LINE
, /* `#line' */
624 T_ERROR
, /* `#error' */
625 T_WARNING
, /* `#warning' */
626 T_ENDIF
, /* `#endif' */
627 T_SCCS
, /* `#sccs', used on system V. */
628 T_IDENT
, /* `#ident', used on system V. */
629 T_ASSERT
, /* `#assert', taken from system V. */
630 T_UNASSERT
, /* `#unassert', taken from system V. */
631 T_SPECLINE
, /* special symbol `__LINE__' */
632 T_DATE
, /* `__DATE__' */
633 T_FILE
, /* `__FILE__' */
634 T_BASE_FILE
, /* `__BASE_FILE__' */
635 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
636 T_VERSION
, /* `__VERSION__' */
637 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
638 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
639 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
640 T_TIME
, /* `__TIME__' */
641 T_CONST
, /* Constant value, used by `__STDC__' */
642 T_MACRO
, /* macro defined by `#define' */
643 T_DISABLED
, /* macro temporarily turned off for rescan */
644 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
645 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
646 T_UNUSED
/* Used for something not defined. */
650 struct hashnode
*next
; /* double links for easy deletion */
651 struct hashnode
*prev
;
652 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
653 chain is kept, in case the node is the head
654 of the chain and gets deleted. */
655 enum node_type type
; /* type of special token */
656 int length
; /* length of token, for quick comparison */
657 U_CHAR
*name
; /* the actual name */
658 union hashval value
; /* pointer to expansion, or whatever */
661 typedef struct hashnode HASHNODE
;
663 /* Some definitions for the hash table. The hash function MUST be
664 computed as shown in hashf () below. That is because the rescan
665 loop computes the hash value `on the fly' for most tokens,
666 in order to avoid the overhead of a lot of procedure calls to
667 the hashf () function. Hashf () only exists for the sake of
668 politeness, for use when speed isn't so important. */
670 #define HASHSIZE 1403
671 static HASHNODE
*hashtab
[HASHSIZE
];
672 #define HASHSTEP(old, c) ((old << 2) + c)
673 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
675 /* Symbols to predefine. */
677 #ifdef CPP_PREDEFINES
678 static char *predefs
= CPP_PREDEFINES
;
680 static char *predefs
= "";
683 /* We let tm.h override the types used here, to handle trivial differences
684 such as the choice of unsigned int or long unsigned int for size_t.
685 When machines start needing nontrivial differences in the size type,
686 it would be best to do something here to figure out automatically
687 from other information what type to use. */
689 /* The string value for __size_type__. */
692 #define SIZE_TYPE "long unsigned int"
695 /* The string value for __ptrdiff_type__. */
698 #define PTRDIFF_TYPE "long int"
701 /* The string value for __wchar_type__. */
704 #define WCHAR_TYPE "int"
707 /* In the definition of a #assert name, this structure forms
708 a list of the individual values asserted.
709 Each value is itself a list of "tokens".
710 These are strings that are compared by name. */
712 struct tokenlist_list
{
713 struct tokenlist_list
*next
;
714 struct arglist
*tokens
;
717 struct assertion_hashnode
{
718 struct assertion_hashnode
*next
; /* double links for easy deletion */
719 struct assertion_hashnode
*prev
;
720 /* also, a back pointer to this node's hash
721 chain is kept, in case the node is the head
722 of the chain and gets deleted. */
723 struct assertion_hashnode
**bucket_hdr
;
724 int length
; /* length of token, for quick comparison */
725 U_CHAR
*name
; /* the actual name */
726 /* List of token-sequences. */
727 struct tokenlist_list
*value
;
730 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
732 /* Some definitions for the hash table. The hash function MUST be
733 computed as shown in hashf () below. That is because the rescan
734 loop computes the hash value `on the fly' for most tokens,
735 in order to avoid the overhead of a lot of procedure calls to
736 the hashf () function. Hashf () only exists for the sake of
737 politeness, for use when speed isn't so important. */
739 #define ASSERTION_HASHSIZE 37
740 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
742 /* Nonzero means inhibit macroexpansion of what seem to be
743 assertion tests, in rescan. For #if. */
744 static int assertions_flag
;
746 /* `struct directive' defines one #-directive, including how to handle it. */
749 int length
; /* Length of name */
750 int (*func
)(); /* Function to handle directive */
751 char *name
; /* Name of directive */
752 enum node_type type
; /* Code which describes which directive. */
753 char angle_brackets
; /* Nonzero => <...> is special. */
754 char traditional_comments
; /* Nonzero: keep comments if -traditional. */
755 char pass_thru
; /* Copy preprocessed directive to output file. */
758 /* Here is the actual list of #-directives, most-often-used first. */
760 static struct directive directive_table
[] = {
761 { 6, do_define
, "define", T_DEFINE
, 0, 1},
762 { 2, do_if
, "if", T_IF
},
763 { 5, do_xifdef
, "ifdef", T_IFDEF
},
764 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
765 { 5, do_endif
, "endif", T_ENDIF
},
766 { 4, do_else
, "else", T_ELSE
},
767 { 4, do_elif
, "elif", T_ELIF
},
768 { 4, do_line
, "line", T_LINE
},
769 { 7, do_include
, "include", T_INCLUDE
, 1},
770 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
771 { 6, do_include
, "import", T_IMPORT
, 1},
772 { 5, do_undef
, "undef", T_UNDEF
},
773 { 5, do_error
, "error", T_ERROR
},
774 { 7, do_warning
, "warning", T_WARNING
},
775 #ifdef SCCS_DIRECTIVE
776 { 4, do_sccs
, "sccs", T_SCCS
},
778 { 6, do_pragma
, "pragma", T_PRAGMA
, 0, 0, 1},
779 { 5, do_ident
, "ident", T_IDENT
, 0, 0, 1},
780 { 6, do_assert
, "assert", T_ASSERT
},
781 { 8, do_unassert
, "unassert", T_UNASSERT
},
782 { -1, 0, "", T_UNUSED
},
785 /* When a directive handler is called,
786 this points to the # that started the directive. */
787 U_CHAR
*directive_start
;
789 /* table to tell if char can be part of a C identifier. */
790 U_CHAR is_idchar
[256];
791 /* table to tell if char can be first char of a c identifier. */
792 U_CHAR is_idstart
[256];
793 /* table to tell if c is horizontal space. */
794 U_CHAR is_hor_space
[256];
795 /* table to tell if c is horizontal or vertical space. */
796 static U_CHAR is_space
[256];
798 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
799 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
801 static int errors
= 0; /* Error counter for exit code */
803 /* Zero means dollar signs are punctuation.
804 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
805 This must be 0 for correct processing of this ANSI C program:
807 #define lose(b) foo(b)
810 static int dollars_in_ident
;
811 #ifndef DOLLARS_IN_IDENTIFIERS
812 #define DOLLARS_IN_IDENTIFIERS 1
815 static FILE_BUF
expand_to_temp_buffer ();
817 static DEFINITION
*collect_expansion ();
819 /* Stack of conditionals currently in progress
820 (including both successful and failing conditionals). */
823 struct if_stack
*next
; /* for chaining to the next stack frame */
824 char *fname
; /* copied from input when frame is made */
825 int lineno
; /* similarly */
826 int if_succeeded
; /* true if a leg of this if-group
827 has been passed through rescan */
828 U_CHAR
*control_macro
; /* For #ifndef at start of file,
829 this is the macro name tested. */
830 enum node_type type
; /* type of last directive seen in this group */
832 typedef struct if_stack IF_STACK_FRAME
;
833 static IF_STACK_FRAME
*if_stack
= NULL
;
835 /* Buffer of -M output. */
836 static char *deps_buffer
;
838 /* Number of bytes allocated in above. */
839 static int deps_allocated_size
;
841 /* Number of bytes used. */
842 static int deps_size
;
844 /* Number of bytes since the last newline. */
845 static int deps_column
;
847 /* File name which deps are being written to.
848 This is 0 if deps are being written to stdout. */
849 static char *deps_file
= 0;
851 /* Nonzero means -I- has been seen,
852 so don't look for #include "foo" the source-file directory. */
853 static int ignore_srcdir
;
855 /* Handler for SIGPIPE. */
859 /* If this is missing, some compilers complain. */
862 fatal ("output pipe has been closed");
872 char *in_fname
, *out_fname
;
876 char **pend_files
= (char **) xmalloc (argc
* sizeof (char *));
877 char **pend_defs
= (char **) xmalloc (argc
* sizeof (char *));
878 char **pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
879 char **pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
880 char **pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
882 /* Record the option used with each element of pend_assertions.
883 This is preparation for supporting more than one option for making
885 char **pend_assertion_options
= (char **) xmalloc (argc
* sizeof (char *));
886 int inhibit_predefs
= 0;
887 int no_standard_includes
= 0;
888 int no_standard_cplusplus_includes
= 0;
889 int missing_newline
= 0;
891 /* Non-0 means don't output the preprocessed program. */
892 int inhibit_output
= 0;
894 /* Stream on which to print the dependency information. */
895 FILE *deps_stream
= 0;
896 /* Target-name to write with the dependency information. */
897 char *deps_target
= 0;
900 /* Get rid of any avoidable limit on stack size. */
904 /* Set the stack limit huge so that alloca (particularly stringtab
905 * in dbxread.c) does not fail. */
906 getrlimit (RLIMIT_STACK
, &rlim
);
907 rlim
.rlim_cur
= rlim
.rlim_max
;
908 setrlimit (RLIMIT_STACK
, &rlim
);
910 #endif /* RLIMIT_STACK defined */
915 /* Remove directories from PROGNAME. */
918 progname
= savestring (argv
[0]);
920 if (!(s
= rindex (progname
, ']')))
921 s
= rindex (progname
, ':');
923 strcpy (progname
, s
+1);
924 if (s
= rindex (progname
, '.'))
932 /* Initialize is_idchar to allow $. */
933 dollars_in_ident
= 1;
934 initialize_char_syntax ();
935 dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
> 0;
937 no_line_commands
= 0;
939 dump_macros
= dump_none
;
944 signal (SIGPIPE
, pipe_closed
);
947 for (i
= 0; include_defaults
[i
].fname
; i
++)
948 max_include_len
= MAX (max_include_len
,
949 strlen (include_defaults
[i
].fname
));
950 /* Leave room for making file name longer when converting to VMS syntax. */
952 max_include_len
+= 10;
955 bzero (pend_files
, argc
* sizeof (char *));
956 bzero (pend_defs
, argc
* sizeof (char *));
957 bzero (pend_undefs
, argc
* sizeof (char *));
958 bzero (pend_assertions
, argc
* sizeof (char *));
959 bzero (pend_includes
, argc
* sizeof (char *));
961 /* Process switches and find input file name. */
963 for (i
= 1; i
< argc
; i
++) {
964 if (argv
[i
][0] != '-') {
965 if (out_fname
!= NULL
)
966 fatal ("Usage: %s [switches] input output", argv
[0]);
967 else if (in_fname
!= NULL
)
972 switch (argv
[i
][1]) {
975 if (!strcmp (argv
[i
], "-include")) {
977 fatal ("Filename missing after -include option");
979 pend_includes
[i
] = argv
[i
+1], i
++;
981 if (!strcmp (argv
[i
], "-imacros")) {
983 fatal ("Filename missing after -imacros option");
985 pend_files
[i
] = argv
[i
+1], i
++;
987 /* Add directory to end of path for includes. */
988 if (!strcmp (argv
[i
], "-idirafter")) {
989 struct file_name_list
*dirtmp
;
991 dirtmp
= (struct file_name_list
*)
992 xmalloc (sizeof (struct file_name_list
));
993 dirtmp
->next
= 0; /* New one goes on the end */
994 dirtmp
->control_macro
= 0;
995 if (after_include
== 0)
996 after_include
= dirtmp
;
998 last_after_include
->next
= dirtmp
;
999 last_after_include
= dirtmp
; /* Tail follows the last one */
1002 fatal ("Directory name missing after -idirafter option");
1004 dirtmp
->fname
= argv
[++i
];
1006 if (strlen (dirtmp
->fname
) > max_include_len
)
1007 max_include_len
= strlen (dirtmp
->fname
);
1012 if (out_fname
!= NULL
)
1013 fatal ("Output filename specified twice");
1015 fatal ("Filename missing after -o option");
1016 out_fname
= argv
[++i
];
1017 if (!strcmp (out_fname
, "-"))
1022 if (!strcmp (argv
[i
], "-pedantic"))
1024 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1026 pedantic_errors
= 1;
1027 } else if (!strcmp (argv
[i
], "-pcp")) {
1028 char *pcp_fname
= argv
[++i
];
1030 ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1031 ? fopen (pcp_fname
, "w")
1032 : fdopen (dup (fileno (stdout
)), "w"));
1033 if (pcp_outfile
== 0)
1034 pfatal_with_name (pcp_fname
);
1040 if (!strcmp (argv
[i
], "-traditional")) {
1042 if (dollars_in_ident
> 0)
1043 dollars_in_ident
= 1;
1044 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1050 if (! strcmp (argv
[i
], "-lang-c"))
1051 cplusplus
= 0, objc
= 0;
1052 if (! strcmp (argv
[i
], "-lang-c++"))
1053 cplusplus
= 1, objc
= 0;
1054 if (! strcmp (argv
[i
], "-lang-objc"))
1055 objc
= 1, cplusplus
= 0;
1056 if (! strcmp (argv
[i
], "-lang-objc++"))
1057 objc
= 1, cplusplus
= 1;
1058 if (! strcmp (argv
[i
], "-lang-asm"))
1060 if (! strcmp (argv
[i
], "-lint"))
1069 inhibit_warnings
= 1;
1073 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1075 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1077 else if (!strcmp (argv
[i
], "-Wcomment"))
1079 else if (!strcmp (argv
[i
], "-Wno-comment"))
1081 else if (!strcmp (argv
[i
], "-Wcomments"))
1083 else if (!strcmp (argv
[i
], "-Wno-comments"))
1085 else if (!strcmp (argv
[i
], "-Wtraditional"))
1087 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1089 else if (!strcmp (argv
[i
], "-Wimport"))
1091 else if (!strcmp (argv
[i
], "-Wno-import"))
1093 else if (!strcmp (argv
[i
], "-Werror"))
1094 warnings_are_errors
= 1;
1095 else if (!strcmp (argv
[i
], "-Wno-error"))
1096 warnings_are_errors
= 0;
1097 else if (!strcmp (argv
[i
], "-Wall"))
1105 if (!strcmp (argv
[i
], "-M"))
1107 else if (!strcmp (argv
[i
], "-MM"))
1109 else if (!strcmp (argv
[i
], "-MD"))
1111 else if (!strcmp (argv
[i
], "-MMD"))
1113 /* For -MD and -MMD options, write deps on file named by next arg. */
1114 if (!strcmp (argv
[i
], "-MD")
1115 || !strcmp (argv
[i
], "-MMD")) {
1117 deps_file
= argv
[i
];
1118 deps_stream
= fopen (argv
[i
], "a");
1119 if (deps_stream
== 0)
1120 pfatal_with_name (argv
[i
]);
1122 /* For -M and -MM, write deps on standard output
1123 and suppress the usual output. */
1124 deps_stream
= stdout
;
1131 char *p
= argv
[i
] + 2;
1134 /* Arg to -d specifies what parts of macros to dump */
1137 dump_macros
= dump_only
;
1141 dump_macros
= dump_names
;
1144 dump_macros
= dump_definitions
;
1152 if (argv
[i
][2] == '3')
1157 fprintf (stderr
, "GNU CPP version %s", version_string
);
1158 #ifdef TARGET_VERSION
1161 fprintf (stderr
, "\n");
1165 print_include_names
= 1;
1172 if (argv
[i
][2] != 0)
1174 else if (i
+ 1 == argc
)
1175 fatal ("Macro name missing after -D option");
1187 if (argv
[i
][2] != 0)
1189 else if (i
+ 1 == argc
)
1190 fatal ("Assertion missing after -A option");
1194 if (!strcmp (p
, "-")) {
1195 /* -A- eliminates all predefined macros and assertions.
1196 Let's include also any that were specified earlier
1197 on the command line. That way we can get rid of any
1198 that were passed automatically in from GCC. */
1200 inhibit_predefs
= 1;
1201 for (j
= 0; j
< i
; j
++)
1202 pend_defs
[j
] = pend_assertions
[j
] = 0;
1204 pend_assertions
[i
] = p
;
1205 pend_assertion_options
[i
] = "-A";
1210 case 'U': /* JF #undef something */
1211 if (argv
[i
][2] != 0)
1212 pend_undefs
[i
] = argv
[i
] + 2;
1213 else if (i
+ 1 == argc
)
1214 fatal ("Macro name missing after -U option");
1216 pend_undefs
[i
] = argv
[i
+1], i
++;
1220 put_out_comments
= 1;
1223 case 'E': /* -E comes from cc -E; ignore it. */
1227 no_line_commands
= 1;
1230 case '$': /* Don't include $ in identifiers. */
1231 dollars_in_ident
= 0;
1234 case 'I': /* Add directory to path for includes. */
1236 struct file_name_list
*dirtmp
;
1238 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-"))
1241 dirtmp
= (struct file_name_list
*)
1242 xmalloc (sizeof (struct file_name_list
));
1243 dirtmp
->next
= 0; /* New one goes on the end */
1244 dirtmp
->control_macro
= 0;
1248 last_include
->next
= dirtmp
;
1249 last_include
= dirtmp
; /* Tail follows the last one */
1250 if (argv
[i
][2] != 0)
1251 dirtmp
->fname
= argv
[i
] + 2;
1252 else if (i
+ 1 == argc
)
1253 fatal ("Directory name missing after -I option");
1255 dirtmp
->fname
= argv
[++i
];
1256 if (strlen (dirtmp
->fname
) > max_include_len
)
1257 max_include_len
= strlen (dirtmp
->fname
);
1258 if (ignore_srcdir
&& first_bracket_include
== 0)
1259 first_bracket_include
= dirtmp
;
1265 if (!strcmp (argv
[i
], "-nostdinc"))
1266 /* -nostdinc causes no default include directories.
1267 You must specify all include-file directories with -I. */
1268 no_standard_includes
= 1;
1269 else if (!strcmp (argv
[i
], "-nostdinc++"))
1270 /* -nostdinc++ causes no default C++-specific include directories. */
1271 no_standard_cplusplus_includes
= 1;
1272 else if (!strcmp (argv
[i
], "-noprecomp"))
1277 /* Sun compiler passes undocumented switch "-undef".
1278 Let's assume it means to inhibit the predefined symbols. */
1279 inhibit_predefs
= 1;
1282 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1283 if (in_fname
== NULL
) {
1286 } else if (out_fname
== NULL
) {
1289 } /* else fall through into error */
1292 fatal ("Invalid option `%s'", argv
[i
]);
1297 /* Add dirs from CPATH after dirs from -I. */
1298 /* There seems to be confusion about what CPATH should do,
1299 so for the moment it is not documented. */
1300 /* Some people say that CPATH should replace the standard include dirs,
1301 but that seems pointless: it comes before them, so it overrides them
1303 p
= (char *) getenv ("CPATH");
1304 if (p
!= 0 && ! no_standard_includes
)
1307 /* Now that dollars_in_ident is known, initialize is_idchar. */
1308 initialize_char_syntax ();
1310 /* Initialize output buffer */
1312 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1313 outbuf
.bufp
= outbuf
.buf
;
1314 outbuf
.length
= OUTBUF_SIZE
;
1316 /* Do partial setup of input buffer for the sake of generating
1317 early #line directives (when -g is in effect). */
1319 fp
= &instack
[++indepth
];
1320 if (in_fname
== NULL
)
1322 fp
->nominal_fname
= fp
->fname
= in_fname
;
1325 /* Install __LINE__, etc. Must follow initialize_char_syntax
1326 and option processing. */
1327 initialize_builtins (fp
, &outbuf
);
1329 /* Do standard #defines and assertions
1330 that identify system and machine type. */
1332 if (!inhibit_predefs
) {
1333 char *p
= (char *) alloca (strlen (predefs
) + 1);
1334 strcpy (p
, predefs
);
1337 while (*p
== ' ' || *p
== '\t')
1339 /* Handle -D options. */
1340 if (p
[0] == '-' && p
[1] == 'D') {
1342 while (*p
&& *p
!= ' ' && *p
!= '\t')
1347 output_line_command (fp
, &outbuf
, 0, same_file
);
1348 make_definition (q
, &outbuf
);
1349 while (*p
== ' ' || *p
== '\t')
1351 } else if (p
[0] == '-' && p
[1] == 'A') {
1352 /* Handle -A options (assertions). */
1361 past_name
= assertion
;
1362 /* Locate end of name. */
1363 while (*past_name
&& *past_name
!= ' '
1364 && *past_name
!= '\t' && *past_name
!= '(')
1366 /* Locate `(' at start of value. */
1368 while (*value
&& (*value
== ' ' || *value
== '\t'))
1370 if (*value
++ != '(')
1372 while (*value
&& (*value
== ' ' || *value
== '\t'))
1375 /* Locate end of value. */
1376 while (*past_value
&& *past_value
!= ' '
1377 && *past_value
!= '\t' && *past_value
!= ')')
1379 termination
= past_value
;
1380 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
1382 if (*termination
++ != ')')
1384 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
1386 /* Temporarily null-terminate the value. */
1387 save_char
= *termination
;
1388 *termination
= '\0';
1389 /* Install the assertion. */
1390 make_assertion ("-A", assertion
);
1391 *termination
= (char) save_char
;
1393 while (*p
== ' ' || *p
== '\t')
1401 /* Now handle the command line options. */
1403 /* Do assertions specified with -A. */
1404 for (i
= 1; i
< argc
; i
++)
1405 if (pend_assertions
[i
])
1406 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1408 /* Do defines specified with -D. */
1409 for (i
= 1; i
< argc
; i
++)
1412 output_line_command (fp
, &outbuf
, 0, same_file
);
1413 make_definition (pend_defs
[i
], &outbuf
);
1416 /* Do undefines specified with -U. */
1417 for (i
= 1; i
< argc
; i
++)
1418 if (pend_undefs
[i
]) {
1420 output_line_command (fp
, &outbuf
, 0, same_file
);
1421 make_undef (pend_undefs
[i
], &outbuf
);
1424 done_initializing
= 1;
1426 { /* read the appropriate environment variable and if it exists
1427 replace include_defaults with the listed path. */
1429 switch ((objc
<< 1) + cplusplus
)
1432 epath
= getenv ("C_INCLUDE_PATH");
1435 epath
= getenv ("CPLUS_INCLUDE_PATH");
1438 epath
= getenv ("OBJC_INCLUDE_PATH");
1441 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
1444 /* If the environment var for this language is set,
1445 add to the default list of include directories. */
1447 char *nstore
= (char *) alloca (strlen (epath
) + 2);
1449 char *startp
, *endp
;
1451 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1455 = (struct default_include
*) xmalloc ((num_dirs
1456 * sizeof (struct default_include
))
1457 + sizeof (include_defaults_array
));
1458 startp
= endp
= epath
;
1461 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1464 && (endp
-startp
!= 1 || !isalpha (*startp
)))
1468 strncpy (nstore
, startp
, endp
-startp
);
1470 strcpy (nstore
, ".");
1472 nstore
[endp
-startp
] = '\0';
1474 max_include_len
= MAX (max_include_len
, endp
-startp
+2);
1475 include_defaults
[num_dirs
].fname
= savestring (nstore
);
1476 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1480 endp
= startp
= endp
+ 1;
1484 /* Put the usual defaults back in at the end. */
1485 bcopy (include_defaults_array
, &include_defaults
[num_dirs
],
1486 sizeof (include_defaults_array
));
1490 /* Unless -fnostdinc,
1491 tack on the standard include file dirs to the specified list */
1492 if (!no_standard_includes
) {
1493 struct default_include
*p
= include_defaults
;
1494 char *specd_prefix
= getenv ("GCC_EXEC_PREFIX");
1495 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
1496 int default_len
= 0;
1497 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1498 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1499 default_len
= strlen (default_prefix
) - 7;
1500 default_prefix
[default_len
] = 0;
1502 /* Search "translated" versions of GNU directories.
1503 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1504 if (specd_prefix
!= 0 && default_len
!= 0)
1505 for (p
= include_defaults
; p
->fname
; p
++) {
1506 /* Some standard dirs are only for C++. */
1507 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1508 /* Does this dir start with the prefix? */
1509 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
1510 /* Yes; change prefix and add to search list. */
1511 struct file_name_list
*new
1512 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
1513 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
1514 char *str
= (char *) xmalloc (this_len
+ 1);
1515 strcpy (str
, specd_prefix
);
1516 strcat (str
, p
->fname
+ default_len
);
1518 new->control_macro
= 0;
1520 /* Add elt to tail of list. */
1524 last_include
->next
= new;
1525 /* Make sure list for #include <...> also has the standard dirs. */
1526 if (ignore_srcdir
&& first_bracket_include
== 0)
1527 first_bracket_include
= new;
1528 /* Record new tail. */
1530 /* Update max_include_len if necessary. */
1531 if (this_len
> max_include_len
)
1532 max_include_len
= this_len
;
1536 /* Search ordinary names for GNU include directories. */
1537 for (p
= include_defaults
; p
->fname
; p
++) {
1538 /* Some standard dirs are only for C++. */
1539 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1540 struct file_name_list
*new
1541 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
1542 new->control_macro
= 0;
1543 /* Add elt to tail of list. */
1547 last_include
->next
= new;
1548 /* Make sure list for #include <...> also has the standard dirs. */
1549 if (ignore_srcdir
&& first_bracket_include
== 0)
1550 first_bracket_include
= new;
1551 /* Record new tail. */
1553 new->fname
= p
->fname
;
1558 /* Tack the after_include chain at the end of the include chain. */
1560 last_include
->next
= after_include
;
1562 include
= after_include
;
1563 if (ignore_srcdir
&& first_bracket_include
== 0)
1564 first_bracket_include
= after_include
;
1566 /* Terminate the after_include chain. */
1567 if (last_after_include
)
1568 last_after_include
->next
= 0;
1570 /* Scan the -imacros files before the main input.
1571 Much like #including them, but with no_output set
1572 so that only their macro definitions matter. */
1575 for (i
= 1; i
< argc
; i
++)
1576 if (pend_files
[i
]) {
1577 int fd
= open (pend_files
[i
], O_RDONLY
, 0666);
1579 perror_with_name (pend_files
[i
]);
1580 return FAILURE_EXIT_CODE
;
1582 finclude (fd
, pend_files
[i
], &outbuf
, 0, 0);
1586 /* Copy the entire contents of the main input file into
1587 the stacked input buffer previously allocated for it. */
1589 /* JF check for stdin */
1590 if (in_fname
== NULL
|| *in_fname
== 0) {
1593 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
1596 /* Either of two environment variables can specify output of deps.
1597 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1598 where OUTPUT_FILE is the file to write deps info to
1599 and DEPS_TARGET is the target to mention in the deps. */
1602 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1603 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1604 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
1609 spec
= getenv ("SUNPRO_DEPENDENCIES");
1616 /* Find the space before the DEPS_TARGET, if there is one. */
1617 /* Don't use `index'; that causes trouble on USG. */
1618 while (*s
!= 0 && *s
!= ' ') s
++;
1620 deps_target
= s
+ 1;
1621 output_file
= (char *) xmalloc (s
- spec
+ 1);
1622 bcopy (spec
, output_file
, s
- spec
);
1623 output_file
[s
- spec
] = 0;
1630 deps_file
= output_file
;
1631 deps_stream
= fopen (output_file
, "a");
1632 if (deps_stream
== 0)
1633 pfatal_with_name (output_file
);
1636 /* For -M, print the expected object file name
1637 as the target of this Make-rule. */
1639 deps_allocated_size
= 200;
1640 deps_buffer
= (char *) xmalloc (deps_allocated_size
);
1646 deps_output (deps_target
, 0);
1647 deps_output (":", 0);
1648 } else if (*in_fname
== 0)
1649 deps_output ("-: ", 0);
1654 /* Discard all directory prefixes from P. */
1660 /* Output P, but remove known suffixes. */
1662 if (p
[len
- 2] == '.' && p
[len
- 1] == 'c')
1663 deps_output (p
, len
- 2);
1664 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'C')
1665 deps_output (p
, len
- 2);
1666 else if (p
[len
- 3] == '.'
1667 && p
[len
- 2] == 'c'
1668 && p
[len
- 1] == 'c')
1669 deps_output (p
, len
- 3);
1670 else if (p
[len
- 2] == '.' && p
[len
- 1] == 's')
1671 deps_output (p
, len
- 2);
1672 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'S')
1673 deps_output (p
, len
- 2);
1674 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'm')
1675 deps_output (p
, len
- 2);
1678 /* Supply our own suffix. */
1679 deps_output (".o : ", 0);
1680 deps_output (in_fname
, 0);
1681 deps_output (" ", 0);
1685 file_size_and_mode (f
, &st_mode
, &st_size
);
1686 fp
->nominal_fname
= fp
->fname
= in_fname
;
1688 fp
->system_header_p
= 0;
1689 /* JF all this is mine about reading pipes and ttys */
1690 if (! S_ISREG (st_mode
)) {
1691 /* Read input from a file that is not a normal disk file.
1692 We cannot preallocate a buffer with the correct size,
1693 so we must read in the file a piece at the time and make it bigger. */
1701 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
1704 cnt
= read (f
, bufp
, bsize
- size
);
1705 if (cnt
< 0) goto perror
; /* error! */
1706 if (cnt
== 0) break; /* End of file */
1709 if (bsize
== size
) { /* Buffer is full! */
1711 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
1712 bufp
= fp
->buf
+ size
; /* May have moved */
1717 /* Read a file whose size we can determine in advance.
1718 For the sake of VMS, st_size is just an upper bound. */
1721 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
1723 while (st_size
> 0) {
1724 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
1734 fp
->if_stack
= if_stack
;
1736 /* Make sure data ends with a newline. And put a null after it. */
1738 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
1739 /* Backslash-newline at end is not good enough. */
1740 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
1741 fp
->buf
[fp
->length
++] = '\n';
1742 missing_newline
= 1;
1744 fp
->buf
[fp
->length
] = '\0';
1746 /* Unless inhibited, convert trigraphs in the input. */
1751 /* Now that we know the input file is valid, open the output. */
1753 if (!out_fname
|| !strcmp (out_fname
, ""))
1754 out_fname
= "stdout";
1755 else if (! freopen (out_fname
, "w", stdout
))
1756 pfatal_with_name (out_fname
);
1758 output_line_command (fp
, &outbuf
, 0, same_file
);
1760 /* Scan the -include files before the main input. */
1762 for (i
= 1; i
< argc
; i
++)
1763 if (pend_includes
[i
]) {
1764 int fd
= open (pend_includes
[i
], O_RDONLY
, 0666);
1766 perror_with_name (pend_includes
[i
]);
1767 return FAILURE_EXIT_CODE
;
1769 finclude (fd
, pend_includes
[i
], &outbuf
, 0, 0);
1772 /* Scan the input, processing macros and directives. */
1774 rescan (&outbuf
, 0);
1776 if (pedantic
&& missing_newline
)
1777 pedwarn ("file does not end in newline");
1779 /* Now we have processed the entire input
1780 Write whichever kind of output has been requested. */
1782 if (dump_macros
== dump_only
)
1784 else if (! inhibit_output
) {
1789 /* Don't actually write the deps file if compilation has failed.
1790 Delete it instead. */
1791 if (errors
> 0 && deps_file
!= 0)
1794 fputs (deps_buffer
, deps_stream
);
1795 putc ('\n', deps_stream
);
1796 if (deps_stream
!= stdout
) {
1797 fclose (deps_stream
);
1798 if (ferror (deps_stream
))
1799 fatal ("I/O error on output");
1804 if (ferror (stdout
))
1805 fatal ("I/O error on output");
1808 exit (FAILURE_EXIT_CODE
);
1809 exit (SUCCESS_EXIT_CODE
);
1812 pfatal_with_name (in_fname
);
1816 /* Given a colon-separated list of file names PATH,
1817 add all the names to the search path for include files. */
1831 struct file_name_list
*dirtmp
;
1833 /* Find the end of this name. */
1835 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1836 while (*q
!= 0 && (*q
!= ':' || (q
- p
== 1 && isalpha (*p
)))) q
++;
1838 while (*q
!= 0 && *q
!= ':') q
++;
1841 /* An empty name in the path stands for the current directory. */
1842 name
= (char *) xmalloc (2);
1846 /* Otherwise use the directory that is named. */
1847 name
= (char *) xmalloc (q
- p
+ 1);
1848 bcopy (p
, name
, q
- p
);
1852 dirtmp
= (struct file_name_list
*)
1853 xmalloc (sizeof (struct file_name_list
));
1854 dirtmp
->next
= 0; /* New one goes on the end */
1855 dirtmp
->control_macro
= 0;
1859 last_include
->next
= dirtmp
;
1860 last_include
= dirtmp
; /* Tail follows the last one */
1861 dirtmp
->fname
= name
;
1862 if (strlen (dirtmp
->fname
) > max_include_len
)
1863 max_include_len
= strlen (dirtmp
->fname
);
1864 if (ignore_srcdir
&& first_bracket_include
== 0)
1865 first_bracket_include
= dirtmp
;
1867 /* Advance past this name. */
1871 /* Skip the colon. */
1876 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
1877 before main CCCP processing. Name `pcp' is also in honor of the
1878 drugs the trigraph designers must have been on.
1880 Using an extra pass through the buffer takes a little extra time,
1881 but is infinitely less hairy than trying to handle trigraphs inside
1882 strings, etc. everywhere, and also makes sure that trigraphs are
1883 only translated in the top level of processing. */
1889 register U_CHAR c
, *fptr
, *bptr
, *sptr
;
1892 fptr
= bptr
= sptr
= buf
->buf
;
1893 while ((sptr
= (U_CHAR
*) index (sptr
, '?')) != NULL
) {
1930 len
= sptr
- fptr
- 2;
1931 if (bptr
!= fptr
&& len
> 0)
1932 bcopy (fptr
, bptr
, len
); /* BSD doc says bcopy () works right
1933 for overlapping strings. In ANSI
1934 C, this will be memmove (). */
1939 len
= buf
->length
- (fptr
- buf
->buf
);
1940 if (bptr
!= fptr
&& len
> 0)
1941 bcopy (fptr
, bptr
, len
);
1942 buf
->length
-= fptr
- bptr
;
1943 buf
->buf
[buf
->length
] = '\0';
1944 if (warn_trigraphs
&& fptr
!= bptr
)
1945 warning ("%d trigraph(s) encountered", (fptr
- bptr
) / 2);
1948 /* Move all backslash-newline pairs out of embarrassing places.
1949 Exchange all such pairs following BP
1950 with any potentially-embarrassing characters that follow them.
1951 Potentially-embarrassing characters are / and *
1952 (because a backslash-newline inside a comment delimiter
1953 would cause it not to be recognized). */
1959 register U_CHAR
*p
= bp
;
1960 register int count
= 0;
1962 /* First count the backslash-newline pairs here. */
1968 else if (p
[1] == '\r' && p
[2] == '\n')
1976 /* What follows the backslash-newlines is not embarrassing. */
1978 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
1981 /* Copy all potentially embarrassing characters
1982 that follow the backslash-newline pairs
1983 down to where the pairs originally started. */
1985 while (*p
== '*' || *p
== '/')
1988 /* Now write the same number of pairs after the embarrassing chars. */
1989 while (count
-- > 0) {
1995 /* Like newline_fix but for use within a directive-name.
1996 Move any backslash-newlines up past any following symbol constituents. */
1999 name_newline_fix (bp
)
2002 register U_CHAR
*p
= bp
;
2003 register int count
= 0;
2005 /* First count the backslash-newline pairs here. */
2010 else if (p
[1] == '\r' && p
[2] == '\n')
2018 /* What follows the backslash-newlines is not embarrassing. */
2020 if (count
== 0 || !is_idchar
[*p
])
2023 /* Copy all potentially embarrassing characters
2024 that follow the backslash-newline pairs
2025 down to where the pairs originally started. */
2027 while (is_idchar
[*p
])
2030 /* Now write the same number of pairs after the embarrassing chars. */
2031 while (count
-- > 0) {
2037 /* Look for lint commands in comments.
2039 When we come in here, ibp points into a comment. Limit is as one expects.
2040 scan within the comment -- it should start, after lwsp, with a lint command.
2041 If so that command is returned as a (constant) string.
2043 Upon return, any arg will be pointed to with argstart and will be
2044 arglen long. Note that we don't parse that arg since it will just
2045 be printed out again.
2049 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2050 register U_CHAR
*ibp
;
2051 register U_CHAR
*limit
;
2052 U_CHAR
**argstart
; /* point to command arg */
2053 int *arglen
, *cmdlen
; /* how long they are */
2056 register U_CHAR
*numptr
; /* temp for arg parsing */
2060 SKIP_WHITE_SPACE (ibp
);
2062 if (ibp
>= limit
) return NULL
;
2064 linsize
= limit
- ibp
;
2066 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2067 if ((linsize
>= 10) && !strncmp (ibp
, "NOTREACHED", 10)) {
2069 return "NOTREACHED";
2071 if ((linsize
>= 8) && !strncmp (ibp
, "ARGSUSED", 8)) {
2075 if ((linsize
>= 11) && !strncmp (ibp
, "LINTLIBRARY", 8)) {
2077 return "LINTLIBRARY";
2079 if ((linsize
>= 7) && !strncmp (ibp
, "VARARGS", 7)) {
2081 ibp
+= 7; linsize
-= 7;
2082 if ((linsize
== 0) || ! isdigit (*ibp
)) return "VARARGS";
2084 /* OK, read a number */
2085 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && isdigit (*numptr
);
2087 *arglen
= numptr
- *argstart
;
2094 * The main loop of the program.
2096 * Read characters from the input stack, transferring them to the
2099 * Macros are expanded and push levels on the input stack.
2100 * At the end of such a level it is popped off and we keep reading.
2101 * At the end of any other kind of level, we return.
2102 * #-directives are handled, except within macros.
2104 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2105 * and insert them when appropriate. This is set while scanning macro
2106 * arguments before substitution. It is zero when scanning for final output.
2107 * There are three types of Newline markers:
2108 * * Newline - follows a macro name that was not expanded
2109 * because it appeared inside an expansion of the same macro.
2110 * This marker prevents future expansion of that identifier.
2111 * When the input is rescanned into the final output, these are deleted.
2112 * These are also deleted by ## concatenation.
2113 * * Newline Space (or Newline and any other whitespace character)
2114 * stands for a place that tokens must be separated or whitespace
2115 * is otherwise desirable, but where the ANSI standard specifies there
2116 * is no whitespace. This marker turns into a Space (or whichever other
2117 * whitespace char appears in the marker) in the final output,
2118 * but it turns into nothing in an argument that is stringified with #.
2119 * Such stringified arguments are the only place where the ANSI standard
2120 * specifies with precision that whitespace may not appear.
2122 * During this function, IP->bufp is kept cached in IBP for speed of access.
2123 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2124 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2125 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2126 * explicitly, and before RECACHE, since RECACHE uses OBP.
2130 rescan (op
, output_marks
)
2134 /* Character being scanned in main loop. */
2137 /* Length of pending accumulated identifier. */
2138 register int ident_length
= 0;
2140 /* Hash code of pending accumulated identifier. */
2141 register int hash
= 0;
2143 /* Current input level (&instack[indepth]). */
2146 /* Pointer for scanning input. */
2147 register U_CHAR
*ibp
;
2149 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2150 register U_CHAR
*limit
;
2152 /* Pointer for storing output. */
2153 register U_CHAR
*obp
;
2155 /* REDO_CHAR is nonzero if we are processing an identifier
2156 after backing up over the terminating character.
2157 Sometimes we process an identifier without backing up over
2158 the terminating character, if the terminating character
2159 is not special. Backing up is done so that the terminating character
2160 will be dispatched on again once the identifier is dealt with. */
2163 /* 1 if within an identifier inside of which a concatenation
2164 marker (Newline -) has been seen. */
2165 int concatenated
= 0;
2167 /* While scanning a comment or a string constant,
2168 this records the line it started on, for error messages. */
2171 /* Line where a newline was first seen in a string constant. */
2172 int multiline_string_line
= 0;
2174 /* Record position of last `real' newline. */
2175 U_CHAR
*beg_of_line
;
2177 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2180 do { ip->macro->type = T_MACRO; \
2181 if (ip->free_ptr) free (ip->free_ptr); \
2182 --indepth; } while (0)
2184 /* Reload `rescan's local variables that describe the current
2185 level of the input stack. */
2188 do { ip = &instack[indepth]; \
2190 limit = ip->buf + ip->length; \
2192 check_expand (op, limit - ibp); \
2194 obp = op->bufp; } while (0)
2196 if (no_output
&& instack
[indepth
].fname
!= 0)
2197 skip_if_group (&instack
[indepth
], 1);
2203 /* Our caller must always put a null after the end of
2204 the input at each input stack level. */
2217 /* Always merge lines ending with backslash-newline,
2218 even in middle of identifier. */
2221 --obp
; /* remove backslash from obuf */
2224 /* Otherwise, backslash suppresses specialness of following char,
2225 so copy it here to prevent the switch from seeing it.
2226 But first get any pending identifier processed. */
2227 if (ident_length
> 0)
2233 if (assertions_flag
) {
2234 /* Copy #foo (bar lose) without macro expansion. */
2235 SKIP_WHITE_SPACE (ibp
);
2236 while (is_idchar
[*ibp
])
2238 SKIP_WHITE_SPACE (ibp
);
2241 skip_paren_group (ip
);
2242 bcopy (ibp
, obp
, ip
->bufp
- ibp
);
2243 obp
+= ip
->bufp
- ibp
;
2248 /* If this is expanding a macro definition, don't recognize
2249 preprocessor directives. */
2255 /* # keyword: a # must be first nonblank char on the line */
2256 if (beg_of_line
== 0)
2261 /* Scan from start of line, skipping whitespace, comments
2262 and backslash-newlines, and see if we reach this #.
2263 If not, this # is not special. */
2266 if (is_hor_space
[*bp
])
2268 else if (*bp
== '\\' && bp
[1] == '\n')
2270 else if (*bp
== '/' && bp
[1] == '*') {
2272 while (!(*bp
== '*' && bp
[1] == '/'))
2276 else if ((cplusplus
|| objc
) && *bp
== '/' && bp
[1] == '/') {
2278 while (*bp
++ != '\n') ;
2286 /* This # can start a directive. */
2288 --obp
; /* Don't copy the '#' */
2292 if (! handle_directive (ip
, op
)) {
2296 /* Not a known directive: treat it as ordinary text.
2297 IP, OP, IBP, etc. have not been changed. */
2298 if (no_output
&& instack
[indepth
].fname
) {
2299 /* If not generating expanded output,
2300 what we do with ordinary text is skip it.
2301 Discard everything until next # directive. */
2302 skip_if_group (&instack
[indepth
], 1);
2307 ++obp
; /* Copy the '#' after all */
2313 /* A # directive has been successfully processed. */
2314 /* If not generating expanded output, ignore everything until
2315 next # directive. */
2316 if (no_output
&& instack
[indepth
].fname
)
2317 skip_if_group (&instack
[indepth
], 1);
2323 case '\"': /* skip quoted string */
2325 /* A single quoted string is treated like a double -- some
2326 programs (e.g., troff) are perverse this way */
2331 start_line
= ip
->lineno
;
2333 /* Skip ahead to a matching quote. */
2338 if (ip
->macro
!= 0) {
2339 /* try harder: this string crosses a macro expansion boundary */
2345 error_with_line (line_for_error (start_line
),
2346 "unterminated string or character constant");
2347 error_with_line (multiline_string_line
,
2348 "possible real start of unterminated constant");
2349 multiline_string_line
= 0;
2358 /* Traditionally, end of line ends a string constant with no error.
2359 So exit the loop and record the new line. */
2364 if (pedantic
|| c
== '\'') {
2365 error_with_line (line_for_error (start_line
),
2366 "unterminated string or character constant");
2369 if (multiline_string_line
== 0)
2370 multiline_string_line
= ip
->lineno
- 1;
2377 /* Backslash newline is replaced by nothing at all,
2378 but keep the line counts correct. */
2383 /* ANSI stupidly requires that in \\ the second \
2384 is *not* prevented from combining with a newline. */
2385 while (*ibp
== '\\' && ibp
[1] == '\n') {
2404 if (*ibp
== '\\' && ibp
[1] == '\n')
2408 && !((cplusplus
|| objc
) && *ibp
== '/'))
2416 /* C++ style comment... */
2417 start_line
= ip
->lineno
;
2419 --ibp
; /* Back over the slash */
2422 /* Comments are equivalent to spaces. */
2423 if (! put_out_comments
)
2426 /* must fake up a comment here */
2431 U_CHAR
*before_bp
= ibp
+2;
2433 while (ibp
< limit
) {
2434 if (*ibp
++ == '\n') {
2436 if (put_out_comments
) {
2437 bcopy (before_bp
, obp
, ibp
- before_bp
);
2438 obp
+= ibp
- before_bp
;
2447 /* Ordinary C comment. Skip it, optionally copying it to output. */
2449 start_line
= ip
->lineno
;
2451 ++ibp
; /* Skip the star. */
2453 /* If this cpp is for lint, we peek inside the comments: */
2457 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2459 if (lintcmd
!= NULL
) {
2460 /* I believe it is always safe to emit this newline: */
2462 bcopy ("#pragma lint ", obp
, 13);
2464 bcopy (lintcmd
, obp
, cmdlen
);
2469 bcopy (argbp
, obp
, arglen
);
2473 /* OK, now bring us back to the state we were in before we entered
2474 this branch. We need #line b/c the newline for the pragma
2475 could fuck things up. */
2476 output_line_command (ip
, op
, 0, same_file
);
2477 *(obp
++) = ' '; /* just in case, if comments are copied thru */
2482 /* Comments are equivalent to spaces.
2483 Note that we already output the slash; we might not want it.
2484 For -traditional, a comment is equivalent to nothing. */
2485 if (! put_out_comments
) {
2495 U_CHAR
*before_bp
= ibp
;
2497 while (ibp
< limit
) {
2500 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
2501 warning("`/*' within comment");
2504 if (*ibp
== '\\' && ibp
[1] == '\n')
2506 if (ibp
>= limit
|| *ibp
== '/')
2511 /* Copy the newline into the output buffer, in order to
2512 avoid the pain of a #line every time a multiline comment
2514 if (!put_out_comments
)
2522 error_with_line (line_for_error (start_line
),
2523 "unterminated comment");
2526 if (put_out_comments
) {
2527 bcopy (before_bp
, obp
, ibp
- before_bp
);
2528 obp
+= ibp
- before_bp
;
2535 if (!dollars_in_ident
)
2539 case '0': case '1': case '2': case '3': case '4':
2540 case '5': case '6': case '7': case '8': case '9':
2541 /* If digit is not part of identifier, it starts a number,
2542 which means that following letters are not an identifier.
2543 "0x5" does not refer to an identifier "x5".
2544 So copy all alphanumerics that follow without accumulating
2545 as an identifier. Periods also, for sake of "3.e7". */
2547 if (ident_length
== 0) {
2548 while (ibp
< limit
) {
2549 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
2554 /* ".." terminates a preprocessing number. This is useless for C
2555 code but useful for preprocessing other things. */
2556 if (!isalnum (c
) && (c
!= '.' || *ibp
== '.') && c
!= '_') {
2561 /* A sign can be part of a preprocessing number
2562 if it follows an e. */
2563 if (c
== 'e' || c
== 'E') {
2564 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
2568 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
2570 /* But traditional C does not let the token go past the sign. */
2581 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2582 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2583 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2584 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2586 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2587 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
2588 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2589 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2593 /* Compute step of hash function, to avoid a proc call on every token */
2594 hash
= HASHSTEP (hash
, c
);
2598 /* If reprocessing a macro expansion, newline is a special marker. */
2599 if (ip
->macro
!= 0) {
2600 /* Newline White is a "funny space" to separate tokens that are
2601 supposed to be separate but without space between.
2602 Here White means any whitespace character.
2603 Newline - marks a recursive macro use that is not
2604 supposed to be expandable. */
2607 /* Newline - inhibits expansion of preceding token.
2608 If expanding a macro arg, we keep the newline -.
2609 In final output, it is deleted. */
2610 if (! concatenated
) {
2615 if (!output_marks
) {
2618 /* If expanding a macro arg, keep the newline -. */
2621 } else if (is_space
[*ibp
]) {
2622 /* Newline Space does not prevent expansion of preceding token
2623 so expand the preceding token and then come back. */
2624 if (ident_length
> 0)
2627 /* If generating final output, newline space makes a space. */
2628 if (!output_marks
) {
2630 /* And Newline Newline makes a newline, so count it. */
2631 if (obp
[-1] == '\n')
2634 /* If expanding a macro arg, keep the newline space.
2635 If the arg gets stringified, newline space makes nothing. */
2638 } else abort (); /* Newline followed by something random? */
2642 /* If there is a pending identifier, handle it and come back here. */
2643 if (ident_length
> 0)
2648 /* Update the line counts and output a #line if necessary. */
2651 if (ip
->lineno
!= op
->lineno
) {
2653 output_line_command (ip
, op
, 1, same_file
);
2654 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2659 /* Come here either after (1) a null character that is part of the input
2660 or (2) at the end of the input, because there is a null there. */
2663 /* Our input really contains a null character. */
2666 /* At end of a macro-expansion level, pop it and read next level. */
2667 if (ip
->macro
!= 0) {
2670 /* If traditional, and we have an identifier that ends here,
2671 process it now, so we get the right error for recursion. */
2672 if (traditional
&& ident_length
2673 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
2682 /* If we don't have a pending identifier,
2683 return at end of input. */
2684 if (ident_length
== 0) {
2692 /* If we do have a pending identifier, just consider this null
2693 a special character and arrange to dispatch on it again.
2694 The second time, IDENT_LENGTH will be zero so we will return. */
2700 /* Handle the case of a character such as /, ', " or null
2701 seen following an identifier. Back over it so that
2702 after the identifier is processed the special char
2703 will be dispatched on again. */
2713 if (ident_length
> 0) {
2714 register HASHNODE
*hp
;
2716 /* We have just seen an identifier end. If it's a macro, expand it.
2718 IDENT_LENGTH is the length of the identifier
2719 and HASH is its hash code.
2721 The identifier has already been copied to the output,
2722 so if it is a macro we must remove it.
2724 If REDO_CHAR is 0, the char that terminated the identifier
2725 has been skipped in the output and the input.
2726 OBP-IDENT_LENGTH-1 points to the identifier.
2727 If the identifier is a macro, we must back over the terminator.
2729 If REDO_CHAR is 1, the terminating char has already been
2730 backed over. OBP-IDENT_LENGTH points to the identifier. */
2732 if (!pcp_outfile
|| pcp_inside_if
) {
2734 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
2737 if (hp
->length
== ident_length
) {
2738 int obufp_before_macroname
;
2739 int op_lineno_before_macroname
;
2740 register int i
= ident_length
;
2741 register U_CHAR
*p
= hp
->name
;
2742 register U_CHAR
*q
= obp
- i
;
2748 do { /* All this to avoid a strncmp () */
2753 /* We found a use of a macro name.
2754 see if the context shows it is a macro call. */
2756 /* Back up over terminating character if not already done. */
2762 /* Save this as a displacement from the beginning of the output
2763 buffer. We can not save this as a position in the output
2764 buffer, because it may get realloc'ed by RECACHE. */
2765 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
2766 op_lineno_before_macroname
= op
->lineno
;
2768 if (hp
->type
== T_PCSTRING
) {
2769 pcstring_used (hp
); /* Mark the definition of this key
2770 as needed, ensuring that it
2772 break; /* Exit loop, since the key cannot have a
2773 definition any longer. */
2776 /* Record whether the macro is disabled. */
2777 disabled
= hp
->type
== T_DISABLED
;
2779 /* This looks like a macro ref, but if the macro was disabled,
2780 just copy its name and put in a marker if requested. */
2784 /* This error check caught useful cases such as
2785 #define foo(x,y) bar(x(y,0), y)
2788 error ("recursive use of macro `%s'", hp
->name
);
2792 check_expand (op
, limit
- ibp
+ 2);
2799 /* If macro wants an arglist, verify that a '(' follows.
2800 first skip all whitespace, copying it to the output
2801 after the macro name. Then, if there is no '(',
2802 decide this is not a macro call and leave things that way. */
2803 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
2804 && hp
->value
.defn
->nargs
>= 0)
2806 U_CHAR
*old_ibp
= ibp
;
2807 U_CHAR
*old_obp
= obp
;
2808 int old_iln
= ip
->lineno
;
2809 int old_oln
= op
->lineno
;
2812 /* Scan forward over whitespace, copying it to the output. */
2813 if (ibp
== limit
&& ip
->macro
!= 0) {
2818 old_iln
= ip
->lineno
;
2819 old_oln
= op
->lineno
;
2821 /* A comment: copy it unchanged or discard it. */
2822 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
2823 if (put_out_comments
) {
2826 } else if (! traditional
) {
2830 while (ibp
+ 1 != limit
2831 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
2832 /* We need not worry about newline-marks,
2833 since they are never found in comments. */
2835 /* Newline in a file. Count it. */
2839 if (put_out_comments
)
2845 if (put_out_comments
) {
2850 else if (is_space
[*ibp
]) {
2852 if (ibp
[-1] == '\n') {
2853 if (ip
->macro
== 0) {
2854 /* Newline in a file. Count it. */
2857 } else if (!output_marks
) {
2858 /* A newline mark, and we don't want marks
2859 in the output. If it is newline-hyphen,
2860 discard it entirely. Otherwise, it is
2861 newline-whitechar, so keep the whitechar. */
2871 /* A newline mark; copy both chars to the output. */
2879 /* It isn't a macro call.
2880 Put back the space that we just skipped. */
2883 ip
->lineno
= old_iln
;
2884 op
->lineno
= old_oln
;
2885 /* Exit the for loop. */
2890 /* This is now known to be a macro call.
2891 Discard the macro name from the output,
2892 along with any following whitespace just copied. */
2893 obp
= op
->buf
+ obufp_before_macroname
;
2894 op
->lineno
= op_lineno_before_macroname
;
2896 /* Expand the macro, reading arguments as needed,
2897 and push the expansion on the input stack. */
2900 macroexpand (hp
, op
);
2902 /* Reexamine input stack, since macroexpand has pushed
2903 a new level on it. */
2910 } /* End hash-table-search loop */
2912 ident_length
= hash
= 0; /* Stop collecting identifier */
2915 } /* End if (ident_length > 0) */
2917 } /* End per-char loop */
2919 /* Come here to return -- but first give an error message
2920 if there was an unterminated successful conditional. */
2922 if (if_stack
!= ip
->if_stack
) {
2924 switch (if_stack
->type
) {
2941 error_with_line (line_for_error (if_stack
->lineno
),
2942 "unterminated `#%s' conditional", str
);
2944 if_stack
= ip
->if_stack
;
2948 * Rescan a string into a temporary buffer and return the result
2949 * as a FILE_BUF. Note this function returns a struct, not a pointer.
2951 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
2952 * and insert such markers when appropriate. See `rescan' for details.
2953 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
2954 * before substitution; it is 0 for other uses.
2957 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
2958 U_CHAR
*buf
, *limit
;
2959 int output_marks
, assertions
;
2961 register FILE_BUF
*ip
;
2963 int length
= limit
- buf
;
2965 int odepth
= indepth
;
2966 int save_assertions_flag
= assertions_flag
;
2968 assertions_flag
= assertions
;
2973 /* Set up the input on the input stack. */
2975 buf1
= (U_CHAR
*) alloca (length
+ 1);
2977 register U_CHAR
*p1
= buf
;
2978 register U_CHAR
*p2
= buf1
;
2985 /* Set up to receive the output. */
2987 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
2988 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
2993 CHECK_DEPTH ({return obuf
;});
2997 ip
= &instack
[indepth
];
2999 ip
->nominal_fname
= 0;
3000 ip
->system_header_p
= 0;
3003 ip
->length
= length
;
3004 ip
->buf
= ip
->bufp
= buf1
;
3005 ip
->if_stack
= if_stack
;
3007 ip
->lineno
= obuf
.lineno
= 1;
3009 /* Scan the input, create the output. */
3010 rescan (&obuf
, output_marks
);
3012 /* Pop input stack to original state. */
3015 if (indepth
!= odepth
)
3018 /* Record the output. */
3019 obuf
.length
= obuf
.bufp
- obuf
.buf
;
3021 assertions_flag
= save_assertions_flag
;
3026 * Process a # directive. Expects IP->bufp to point after the '#', as in
3027 * `#define foo bar'. Passes to the command handler
3028 * (do_define, do_include, etc.): the addresses of the 1st and
3029 * last chars of the command (starting immediately after the #
3030 * keyword), plus op and the keyword table pointer. If the command
3031 * contains comments it is copied into a temporary buffer sans comments
3032 * and the temporary buffer is passed to the command handler instead.
3033 * Likewise for backslash-newlines.
3035 * Returns nonzero if this was a known # directive.
3036 * Otherwise, returns zero, without advancing the input pointer.
3040 handle_directive (ip
, op
)
3043 register U_CHAR
*bp
, *cp
;
3044 register struct directive
*kt
;
3045 register int ident_length
;
3048 /* Nonzero means we must copy the entire command
3049 to get rid of comments or backslash-newlines. */
3050 int copy_command
= 0;
3052 U_CHAR
*ident
, *after_ident
;
3056 /* Record where the directive started. do_xifdef needs this. */
3057 directive_start
= bp
- 1;
3059 /* Skip whitespace and \-newline. */
3061 if (is_hor_space
[*bp
]) {
3062 if ((*bp
== '\f' || *bp
== '\v') && pedantic
)
3063 pedwarn ("%s in preprocessing directive",
3064 *bp
== '\f' ? "formfeed" : "vertical tab");
3066 } else if (*bp
== '/' && bp
[1] == '*') {
3068 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3070 } else if (*bp
== '\\' && bp
[1] == '\n') {
3071 bp
+= 2; ip
->lineno
++;
3075 /* Now find end of directive name.
3076 If we encounter a backslash-newline, exchange it with any following
3077 symbol-constituents so that we end up with a contiguous name. */
3084 if (*cp
== '\\' && cp
[1] == '\n')
3085 name_newline_fix (cp
);
3091 ident_length
= cp
- bp
;
3095 /* A line of just `#' becomes blank. */
3097 if (ident_length
== 0 && *after_ident
== '\n') {
3098 ip
->bufp
= after_ident
;
3102 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3104 while (is_idchar
[*p
]) {
3105 if (*p
< '0' || *p
> '9')
3109 /* Handle # followed by a line number. */
3110 if (p
!= ident
&& !is_idchar
[*p
]) {
3111 static struct directive line_directive_table
[] = {
3112 { 4, do_line
, "line", T_LINE
},
3115 pedwarn ("`#' followed by integer");
3116 after_ident
= ident
;
3117 kt
= line_directive_table
;
3121 /* Avoid error for `###' and similar cases unless -pedantic. */
3123 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3125 if (pedantic
&& !lang_asm
)
3126 warning ("invalid preprocessor directive");
3132 error ("invalid preprocessor directive name");
3138 * Decode the keyword and call the appropriate expansion
3139 * routine, after moving the input pointer up to the next line.
3141 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3142 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
)) {
3143 register U_CHAR
*buf
;
3144 register U_CHAR
*limit
;
3147 int *already_output
= 0;
3149 /* Nonzero means do not delete comments within the directive.
3150 #define needs this when -traditional. */
3155 limit
= ip
->buf
+ ip
->length
;
3157 keep_comments
= traditional
&& kt
->traditional_comments
;
3158 /* #import is defined only in Objective C, or when on the NeXT. */
3159 if (kt
->type
== T_IMPORT
&& !(objc
|| lookup ("__NeXT__", -1, -1)))
3162 /* Find the end of this command (first newline not backslashed
3163 and not in a string or comment).
3164 Set COPY_COMMAND if the command must be copied
3165 (it contains a backslash-newline or a comment). */
3167 buf
= bp
= after_ident
;
3168 while (bp
< limit
) {
3169 register U_CHAR c
= *bp
++;
3183 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
3184 /* Don't bother calling the directive if we already got an error
3185 message due to unterminated string. Skip everything and pretend
3186 we called the directive. */
3189 /* Traditional preprocessing permits unterminated strings. */
3198 /* <...> is special for #include. */
3200 if (!kt
->angle_brackets
)
3202 while (*bp
&& *bp
!= '>') bp
++;
3206 if (*bp
== '\\' && bp
[1] == '\n')
3209 || ((cplusplus
|| objc
) && *bp
== '/')) {
3210 U_CHAR
*obp
= bp
- 1;
3212 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3214 /* No need to copy the command because of a comment at the end;
3215 just don't include the comment in the directive. */
3216 if (bp
== limit
|| *bp
== '\n') {
3220 /* Don't remove the comments if -traditional. */
3221 if (! keep_comments
)
3229 pedwarn ("%s in preprocessing directive",
3230 c
== '\f' ? "formfeed" : "vertical tab");
3234 --bp
; /* Point to the newline */
3242 resume_p
= ip
->bufp
;
3243 /* BP is the end of the directive.
3244 RESUME_P is the next interesting data after the directive.
3245 A comment may come between. */
3247 /* If a directive should be copied through, and -E was given,
3248 pass it through before removing comments. */
3249 if (!no_output
&& kt
->pass_thru
&& put_out_comments
) {
3252 /* Output directive name. */
3253 check_expand (op
, kt
->length
+ 2);
3254 /* Make sure # is at the start of a line */
3255 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3260 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3261 op
->bufp
+= kt
->length
;
3263 /* Output arguments. */
3265 check_expand (op
, len
);
3266 bcopy (buf
, op
->bufp
, len
);
3268 /* Take account of any (escaped) newlines just output. */
3270 if (buf
[len
] == '\n')
3273 already_output
= &junk
;
3274 } /* Don't we need a newline or #line? */
3277 register U_CHAR
*xp
= buf
;
3278 /* Need to copy entire command into temp buffer before dispatching */
3280 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
3284 /* Copy to the new buffer, deleting comments
3285 and backslash-newlines (and whitespace surrounding the latter). */
3288 register U_CHAR c
= *xp
++;
3293 abort (); /* A bare newline should never part of the line. */
3296 /* <...> is special for #include. */
3298 if (!kt
->angle_brackets
)
3300 while (xp
< bp
&& c
!= '>') {
3302 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
3313 if (cp
!= buf
&& is_space
[cp
[-1]]) {
3314 while (cp
!= buf
&& is_space
[cp
[-1]]) cp
--;
3316 SKIP_WHITE_SPACE (xp
);
3317 } else if (is_space
[*xp
]) {
3319 SKIP_WHITE_SPACE (xp
);
3329 register U_CHAR
*bp1
3330 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
, 0, 0, 0);
3344 || ((cplusplus
|| objc
) && *xp
== '/')) {
3346 /* If we already copied the command through,
3347 already_output != 0 prevents outputting comment now. */
3348 skip_to_end_of_comment (ip
, already_output
, 0);
3350 while (xp
!= ip
->bufp
)
3352 /* Delete or replace the slash. */
3353 else if (traditional
)
3362 /* Null-terminate the copy. */
3368 ip
->bufp
= resume_p
;
3370 /* Some directives should be written out for cc1 to process,
3371 just as if they were not defined. And sometimes we're copying
3372 definitions through. */
3374 if (!no_output
&& already_output
== 0
3376 || (kt
->type
== T_DEFINE
3377 && (dump_macros
== dump_names
3378 || dump_macros
== dump_definitions
)))) {
3381 /* Output directive name. */
3382 check_expand (op
, kt
->length
+ 1);
3384 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3385 op
->bufp
+= kt
->length
;
3387 if (kt
->pass_thru
|| dump_macros
== dump_definitions
) {
3388 /* Output arguments. */
3390 check_expand (op
, len
);
3391 bcopy (buf
, op
->bufp
, len
);
3394 } /* Don't we need a newline or #line? */
3396 /* Call the appropriate command handler. buf now points to
3397 either the appropriate place in the input buffer, or to
3398 the temp buffer if it was necessary to make one. cp
3399 points to the first char after the contents of the (possibly
3400 copied) command, in either case. */
3401 (*kt
->func
) (buf
, cp
, op
, kt
);
3402 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3408 /* It is deliberate that we don't warn about undefined directives.
3409 That is the responsibility of cc1. */
3413 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3414 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3418 * expand things like __FILE__. Place the expansion into the output
3419 * buffer *without* rescanning.
3423 special_symbol (hp
, op
)
3430 FILE_BUF
*ip
= NULL
;
3432 int paren
= 0; /* For special `defined' keyword */
3434 if (pcp_outfile
&& pcp_inside_if
3435 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
3436 error ("Predefined macro `%s' used inside `#if' during precompilation",
3439 for (i
= indepth
; i
>= 0; i
--)
3440 if (instack
[i
].fname
!= NULL
) {
3445 error ("cccp error: not in any file?!");
3446 return; /* the show must go on */
3454 if (hp
->type
== T_FILE
)
3455 string
= ip
->nominal_fname
;
3457 string
= instack
[0].nominal_fname
;
3461 buf
= (char *) alloca (3 + strlen (string
));
3462 sprintf (buf
, "\"%s\"", string
);
3470 case T_INCLUDE_LEVEL
:
3472 for (i
= indepth
; i
>= 0; i
--)
3473 if (instack
[i
].fname
!= NULL
)
3476 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
3477 sprintf (buf
, "%d", true_indepth
- 1);
3481 buf
= (char *) alloca (3 + strlen (version_string
));
3482 sprintf (buf
, "\"%s\"", version_string
);
3486 buf
= (char *) alloca (3 + strlen (SIZE_TYPE
));
3487 sprintf (buf
, "%s", SIZE_TYPE
);
3490 case T_PTRDIFF_TYPE
:
3491 buf
= (char *) alloca (3 + strlen (PTRDIFF_TYPE
));
3492 sprintf (buf
, "%s", PTRDIFF_TYPE
);
3496 buf
= (char *) alloca (3 + strlen (WCHAR_TYPE
));
3497 sprintf (buf
, "%s", WCHAR_TYPE
);
3501 buf
= (char *) alloca (4 * sizeof (int));
3502 sprintf (buf
, "%d", hp
->value
.ival
);
3503 if (pcp_inside_if
&& pcp_outfile
)
3504 /* Output a precondition for this macro use */
3505 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
3509 buf
= (char *) alloca (10);
3510 sprintf (buf
, "%d", ip
->lineno
);
3515 buf
= (char *) alloca (20);
3516 if (hp
->type
== T_DATE
)
3517 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
3518 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
3520 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
3524 case T_SPEC_DEFINED
:
3525 buf
= " 0 "; /* Assume symbol is not defined */
3526 ip
= &instack
[indepth
];
3527 SKIP_WHITE_SPACE (ip
->bufp
);
3528 if (*ip
->bufp
== '(') {
3530 ip
->bufp
++; /* Skip over the paren */
3531 SKIP_WHITE_SPACE (ip
->bufp
);
3534 if (!is_idstart
[*ip
->bufp
])
3536 if (hp
= lookup (ip
->bufp
, -1, -1)) {
3537 if (pcp_outfile
&& pcp_inside_if
3538 && hp
->value
.defn
->predefined
)
3539 /* Output a precondition for this macro use. */
3540 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
3544 if (pcp_outfile
&& pcp_inside_if
) {
3545 /* Output a precondition for this macro use */
3546 U_CHAR
*cp
= ip
->bufp
;
3547 fprintf (pcp_outfile
, "#undef ");
3548 while (is_idchar
[*cp
]) /* Ick! */
3549 fputc (*cp
++, pcp_outfile
);
3550 putc ('\n', pcp_outfile
);
3552 while (is_idchar
[*ip
->bufp
])
3554 SKIP_WHITE_SPACE (ip
->bufp
);
3556 if (*ip
->bufp
!= ')')
3564 error ("`defined' without an identifier");
3568 error ("cccp error: invalid special hash type"); /* time for gdb */
3572 check_expand (op
, len
);
3573 bcopy (buf
, op
->bufp
, len
);
3580 /* Routines to handle #directives */
3582 /* Handle #include and #import.
3583 This function expects to see "fname" or <fname> on the input. */
3586 do_include (buf
, limit
, op
, keyword
)
3587 U_CHAR
*buf
, *limit
;
3589 struct directive
*keyword
;
3591 int importing
= (keyword
->type
== T_IMPORT
);
3592 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3593 static int import_warning
= 0;
3594 char *fname
; /* Dynamically allocated fname buffer */
3597 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3599 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
3600 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3601 struct file_name_list
*searchptr
;
3604 int f
; /* file number */
3606 int retried
= 0; /* Have already tried macro
3607 expanding the include line*/
3608 FILE_BUF trybuf
; /* It got expanded into here */
3609 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
3614 f
= -1; /* JF we iz paranoid! */
3616 if (importing
&& warn_import
3617 && !instack
[indepth
].system_header_p
&& !import_warning
) {
3619 warning ("using `#import' is not recommended");
3620 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3621 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3622 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3623 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3624 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3625 fprintf (stderr
, " ... <real contents of file> ...\n");
3626 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3627 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3628 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3629 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3635 SKIP_WHITE_SPACE (fbeg
);
3636 /* Discard trailing whitespace so we can easily see
3637 if we have parsed all the significant chars we were given. */
3638 while (limit
!= fbeg
&& is_hor_space
[limit
[-1]]) limit
--;
3643 while (fend
!= limit
&& *fend
!= '\"')
3645 if (*fend
== '\"' && fend
+ 1 == limit
) {
3648 /* We have "filename". Figure out directory this source
3649 file is coming from and put it on the front of the list. */
3651 /* If -I- was specified, don't search current dir, only spec'd ones. */
3652 if (ignore_srcdir
) break;
3654 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
3659 if ((nam
= fp
->nominal_fname
) != NULL
) {
3660 /* Found a named file. Figure out dir of the file,
3661 and put it in front of the search list. */
3662 dsp
[0].next
= search_start
;
3665 ep
= rindex (nam
, '/');
3667 ep
= rindex (nam
, ']');
3668 if (ep
== NULL
) ep
= rindex (nam
, '>');
3669 if (ep
== NULL
) ep
= rindex (nam
, ':');
3670 if (ep
!= NULL
) ep
++;
3674 dsp
[0].fname
= (char *) alloca (n
+ 1);
3675 strncpy (dsp
[0].fname
, nam
, n
);
3676 dsp
[0].fname
[n
] = '\0';
3677 if (n
> max_include_len
) max_include_len
= n
;
3679 dsp
[0].fname
= 0; /* Current directory */
3690 while (fend
!= limit
&& *fend
!= '>') fend
++;
3691 if (*fend
== '>' && fend
+ 1 == limit
) {
3692 system_header_p
= 1;
3693 /* If -I-, start with the first -I dir after the -I-. */
3694 if (first_bracket_include
)
3695 search_start
= first_bracket_include
;
3704 error ("`#import' expects \"fname\" or <fname>");
3706 error ("`#include' expects \"fname\" or <fname>");
3709 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
3710 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
3711 bcopy (trybuf
.buf
, buf
, trybuf
.bufp
- trybuf
.buf
);
3712 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
3719 /* For #include_next, skip in the search path
3720 past the dir in which the containing file was found. */
3723 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
3724 if (fp
->fname
!= NULL
) {
3725 /* fp->dir is null if the containing file was specified
3726 with an absolute file name. In that case, don't skip anything. */
3728 search_start
= fp
->dir
->next
;
3734 /* Allocate this permanently, because it gets stored in the definitions
3736 fname
= (char *) xmalloc (max_include_len
+ flen
+ 2);
3737 /* + 2 above for slash and terminating null. */
3739 /* If specified file name is absolute, just open it. */
3742 strncpy (fname
, fbeg
, flen
);
3744 if (lookup_include (fname
))
3747 f
= lookup_import (fname
);
3749 f
= open (fname
, O_RDONLY
, 0666);
3751 return 0; /* Already included this file */
3753 /* Search directory path, trying to open the file.
3754 Copy each filename tried into FNAME. */
3756 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3757 if (searchptr
->fname
) {
3758 /* The empty string in a search path is ignored.
3759 This makes it possible to turn off entirely
3760 a standard piece of the list. */
3761 if (searchptr
->fname
[0] == 0)
3763 strcpy (fname
, searchptr
->fname
);
3764 strcat (fname
, "/");
3765 fname
[strlen (fname
) + flen
] = 0;
3769 strncat (fname
, fbeg
, flen
);
3771 /* Change this 1/2 Unix 1/2 VMS file specification into a
3772 full VMS file specification */
3773 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3774 /* Fix up the filename */
3775 hack_vms_include_specification (fname
);
3777 /* This is a normal VMS filespec, so use it unchanged. */
3778 strncpy (fname
, fbeg
, flen
);
3783 f
= lookup_import (fname
);
3785 f
= open (fname
, O_RDONLY
, 0666);
3787 return 0; /* Already included this file */
3788 if (lookup_include (fname
)) {
3798 /* A file that was not found. */
3800 strncpy (fname
, fbeg
, flen
);
3802 error_from_errno (fname
);
3804 /* For -M, add this file to the dependencies. */
3805 if (print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
3806 /* Break the line before this. */
3807 deps_output ("", 0);
3809 /* If it was requested as a system header file,
3810 then assume it belongs in the first place to look for such. */
3811 if (system_header_p
) {
3812 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3813 if (searchptr
->fname
) {
3814 if (searchptr
->fname
[0] == 0)
3816 deps_output (searchptr
->fname
, 0);
3817 deps_output ("/", 0);
3822 /* Otherwise, omit the directory, as if the file existed
3823 in the directory with the source. */
3824 deps_output (fbeg
, flen
);
3825 deps_output (" ", 0);
3830 /* Check to see if this include file is a once-only include file.
3833 struct file_name_list
* ptr
;
3835 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3836 if (!strcmp (ptr
->fname
, fname
)) {
3838 return 0; /* This file was once'd. */
3842 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
3843 if (!strcmp (ptr
->fname
, fname
))
3844 break; /* This file was included before. */
3848 /* This is the first time for this file. */
3849 /* Add it to list of files included. */
3851 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3852 ptr
->control_macro
= 0;
3853 ptr
->next
= all_include_files
;
3854 all_include_files
= ptr
;
3855 ptr
->fname
= savestring (fname
);
3857 /* For -M, add this file to the dependencies. */
3858 if (print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
3859 deps_output ("", 0);
3860 deps_output (fname
, 0);
3861 deps_output (" ", 0);
3865 /* Handle -H option. */
3866 if (print_include_names
)
3867 fprintf (stderr
, "%s\n", fname
);
3869 if (system_header_p
)
3870 system_include_depth
++;
3872 /* Actually process the file. */
3873 add_import (f
, fname
); /* Record file on "seen" list for #import. */
3875 pcftry
= (char *) alloca (strlen (fname
) + 30);
3883 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3885 pcf
= open (pcftry
, O_RDONLY
, 0666);
3891 if (bcmp (&stat_f
.st_ino
, &s
.st_ino
, sizeof (s
.st_ino
))
3892 || stat_f
.st_dev
!= s
.st_dev
)
3894 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3895 /* Don't need it any more. */
3900 /* Don't need it at all. */
3905 } while (pcf
!= -1 && !pcfbuf
);
3907 /* Actually process the file */
3909 pcfname
= xmalloc (strlen (pcftry
) + 1);
3910 strcpy (pcfname
, pcftry
);
3911 pcfinclude (pcfbuf
, pcfbuflimit
, fname
, op
);
3914 finclude (f
, fname
, op
, system_header_p
, searchptr
);
3916 if (system_header_p
)
3917 system_include_depth
--;
3922 /* Return nonzero if there is no need to include file NAME
3923 because it has already been included and it contains a conditional
3924 to make a repeated include do nothing. */
3927 lookup_include (name
)
3930 struct file_name_list
*l
= all_include_files
;
3931 for (; l
; l
= l
->next
)
3932 if (! strcmp (name
, l
->fname
)
3934 && lookup (l
->control_macro
, -1, -1))
3939 /* Process the contents of include file FNAME, already open on descriptor F,
3941 SYSTEM_HEADER_P is 1 if this file was specified using <...>.
3942 DIRPTR is the link in the dir path through which this file was found,
3943 or 0 if the file name was absolute. */
3946 finclude (f
, fname
, op
, system_header_p
, dirptr
)
3950 int system_header_p
;
3951 struct file_name_list
*dirptr
;
3956 FILE_BUF
*fp
; /* For input stack frame */
3957 int missing_newline
= 0;
3959 CHECK_DEPTH (return;);
3961 if (file_size_and_mode (f
, &st_mode
, &st_size
) < 0)
3962 goto nope
; /* Impossible? */
3964 fp
= &instack
[indepth
+ 1];
3965 bzero (fp
, sizeof (FILE_BUF
));
3966 fp
->nominal_fname
= fp
->fname
= fname
;
3969 fp
->if_stack
= if_stack
;
3970 fp
->system_header_p
= system_header_p
;
3973 if (S_ISREG (st_mode
)) {
3974 fp
->buf
= (U_CHAR
*) alloca (st_size
+ 2);
3977 /* Read the file contents, knowing that st_size is an upper bound
3978 on the number of bytes we can read. */
3979 while (st_size
> 0) {
3980 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
3990 /* Cannot count its file size before reading.
3991 First read the entire file into heap and
3992 copy them into buffer on stack. */
3999 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
4003 i
= read (f
, bufp
, bsize
- st_size
);
4005 goto nope
; /* error! */
4007 break; /* End of file */
4010 if (bsize
== st_size
) { /* Buffer is full! */
4012 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
4013 bufp
= basep
+ st_size
; /* May have moved */
4016 fp
->buf
= (U_CHAR
*) alloca (st_size
+ 2);
4018 bcopy (basep
, fp
->buf
, st_size
);
4019 fp
->length
= st_size
;
4023 /* Close descriptor now, so nesting does not use lots of descriptors. */
4029 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
4030 /* Backslash-newline at end is not good enough. */
4031 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
4032 fp
->buf
[fp
->length
++] = '\n';
4033 missing_newline
= 1;
4035 fp
->buf
[fp
->length
] = '\0';
4038 input_file_stack_tick
++;
4040 output_line_command (fp
, op
, 0, enter_file
);
4043 if (pedantic
&& missing_newline
)
4044 pedwarn ("file does not end in newline");
4047 input_file_stack_tick
++;
4048 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
4053 perror_with_name (fname
);
4057 /* Record that inclusion of the file named FILE
4058 should be controlled by the macro named MACRO_NAME.
4059 This means that trying to include the file again
4060 will do something if that macro is defined. */
4063 record_control_macro (file
, macro_name
)
4067 struct file_name_list
*new;
4069 for (new = all_include_files
; new; new = new->next
) {
4070 if (!strcmp (new->fname
, file
)) {
4071 new->control_macro
= macro_name
;
4076 /* If the file is not in all_include_files, something's wrong. */
4080 /* Maintain and search list of included files, for #import. */
4082 #define IMPORT_HASH_SIZE 31
4084 struct import_file
{
4088 struct import_file
*next
;
4091 /* Hash table of files already included with #include or #import. */
4093 static struct import_file
*import_hash_table
[IMPORT_HASH_SIZE
];
4095 /* Hash a file name for import_hash_table. */
4103 while (*f
) val
+= *f
++;
4104 return (val
%IMPORT_HASH_SIZE
);
4107 /* Search for file FILENAME in import_hash_table.
4108 Return -2 if found, either a matching name or a matching inode.
4109 Otherwise, open the file and return a file descriptor if successful
4110 or -1 if unsuccessful. */
4113 lookup_import (filename
)
4116 struct import_file
*i
;
4122 hashval
= import_hash (filename
);
4124 /* Attempt to find file in list of already included files */
4125 i
= import_hash_table
[hashval
];
4128 if (!strcmp (filename
, i
->name
))
4129 return -2; /* return found */
4132 /* Open it and try a match on inode/dev */
4133 fd
= open (filename
, O_RDONLY
, 0666);
4137 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
4138 i
= import_hash_table
[h
];
4140 /* Compare the inode and the device.
4141 Supposedly on some systems the inode is not a scalar. */
4142 if (!bcmp (&i
->inode
, &sb
.st_ino
, sizeof (sb
.st_ino
))
4143 && i
->dev
== sb
.st_dev
) {
4145 return -2; /* return found */
4150 return fd
; /* Not found, return open file */
4153 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
4156 add_import (fd
, fname
)
4160 struct import_file
*i
;
4164 hashval
= import_hash (fname
);
4166 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
4167 i
->name
= (char *)xmalloc (strlen (fname
)+1);
4168 strcpy (i
->name
, fname
);
4169 bcopy (&sb
.st_ino
, &i
->inode
, sizeof (sb
.st_ino
));
4171 i
->next
= import_hash_table
[hashval
];
4172 import_hash_table
[hashval
] = i
;
4175 /* Load the specified precompiled header into core, and verify its
4176 preconditions. PCF indicates the file descriptor to read, which must
4177 be a regular file. FNAME indicates the file name of the original
4178 header. *LIMIT will be set to an address one past the end of the file.
4179 If the preconditions of the file are not satisfied, the buffer is
4180 freed and we return 0. If the preconditions are satisfied, return
4181 the address of the buffer following the preconditions. The buffer, in
4182 this case, should never be freed because various pieces of it will
4183 be referred to until all precompiled strings are output at the end of
4187 check_precompiled (pcf
, fname
, limit
)
4203 if (file_size_and_mode (pcf
, &st_mode
, &st_size
) < 0)
4206 if (S_ISREG (st_mode
))
4208 buf
= xmalloc (st_size
+ 2);
4211 i
= read (pcf
, buf
+ length
, st_size
);
4223 if (length
> 0 && buf
[length
-1] != '\n')
4224 buf
[length
++] = '\n';
4227 *limit
= buf
+ length
;
4229 /* File is in core. Check the preconditions. */
4230 if (!check_preconditions (buf
))
4232 for (cp
= buf
; *cp
; cp
++)
4235 fprintf (stderr
, "Using preinclude %s\n", fname
);
4241 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
4247 /* PREC (null terminated) points to the preconditions of a
4248 precompiled header. These are a series of #define and #undef
4249 lines which must match the current contents of the hash
4252 check_preconditions (prec
)
4259 lineend
= (char *) index (prec
, '\n');
4261 if (*prec
++ != '#') {
4262 error ("Bad format encountered while reading precompiled file");
4265 if (!strncmp (prec
, "define", 6)) {
4269 mdef
= create_definition (prec
, lineend
, 0);
4274 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
4275 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
4276 || (hp
->type
== T_MACRO
4277 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
4278 && (mdef
.defn
->length
!= 2
4279 || mdef
.defn
->expansion
[0] != '\n'
4280 || mdef
.defn
->expansion
[1] != ' ')))
4282 } else if (!strncmp (prec
, "undef", 5)) {
4287 while (is_hor_space
[*prec
])
4290 while (is_idchar
[*prec
])
4294 if (lookup (name
, len
, -1))
4297 error ("Bad format encountered while reading precompiled file");
4302 /* They all passed successfully */
4306 /* Process the main body of a precompiled file. BUF points to the
4307 string section of the file, following the preconditions. LIMIT is one
4308 character past the end. NAME is the name of the file being read
4309 in. OP is the main output buffer */
4311 pcfinclude (buf
, limit
, name
, op
)
4312 U_CHAR
*buf
, *limit
, *name
;
4319 /* First in the file comes 4 bytes indicating the number of strings, */
4320 /* in network byte order. (MSB first). */
4322 nstrings
= (nstrings
<< 8) | *cp
++;
4323 nstrings
= (nstrings
<< 8) | *cp
++;
4324 nstrings
= (nstrings
<< 8) | *cp
++;
4326 /* Looping over each string... */
4327 while (nstrings
--) {
4328 U_CHAR
*string_start
;
4329 U_CHAR
*endofthiskey
;
4333 /* Each string starts with a STRINGDEF structure (str), followed */
4334 /* by the text of the string (string_start) */
4336 /* First skip to a longword boundary */
4338 cp
+= 4 - ((int)cp
& 3);
4340 /* Now get the string. */
4341 str
= (STRINGDEF
*) cp
;
4342 string_start
= cp
+= sizeof (STRINGDEF
);
4344 for (; *cp
; cp
++) /* skip the string */
4347 /* We need to macro expand the string here to ensure that the
4348 proper definition environment is in place. If it were only
4349 expanded when we find out it is needed, macros necessary for
4350 its proper expansion might have had their definitions changed. */
4351 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
4352 /* Lineno is already set in the precompiled file */
4353 str
->contents
= tmpbuf
.buf
;
4354 str
->len
= tmpbuf
.length
;
4356 str
->filename
= name
;
4357 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
4360 *stringlist_tailp
= str
;
4361 stringlist_tailp
= &str
->chain
;
4363 /* Next comes a fourbyte number indicating the number of keys */
4364 /* for this string. */
4366 nkeys
= (nkeys
<< 8) | *cp
++;
4367 nkeys
= (nkeys
<< 8) | *cp
++;
4368 nkeys
= (nkeys
<< 8) | *cp
++;
4370 /* If this number is -1, then the string is mandatory. */
4374 /* Otherwise, for each key, */
4375 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
4376 KEYDEF
*kp
= (KEYDEF
*) cp
;
4379 /* It starts with a KEYDEF structure */
4380 cp
+= sizeof (KEYDEF
);
4382 /* Find the end of the key. At the end of this for loop we
4383 advance CP to the start of the next key using this variable. */
4384 endofthiskey
= cp
+ strlen (cp
);
4387 /* Expand the key, and enter it into the hash table. */
4388 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
4389 tmpbuf
.bufp
= tmpbuf
.buf
;
4391 while (is_hor_space
[*tmpbuf
.bufp
])
4393 if (!is_idstart
[*tmpbuf
.bufp
]
4394 || tmpbuf
.bufp
== tmpbuf
.buf
+ tmpbuf
.length
) {
4399 hp
= lookup (tmpbuf
.bufp
, -1, -1);
4402 install (tmpbuf
.bufp
, -1, T_PCSTRING
, (int) kp
, -1);
4404 else if (hp
->type
== T_PCSTRING
) {
4405 kp
->chain
= hp
->value
.keydef
;
4406 hp
->value
.keydef
= kp
;
4412 /* This output_line_command serves to switch us back to the current
4413 input file in case some of these strings get output (which will
4414 result in line commands for the header file being output). */
4415 output_line_command (&instack
[indepth
], op
, 0, enter_file
);
4418 /* Called from rescan when it hits a key for strings. Mark them all */
4419 /* used and clean up. */
4426 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
4427 kp
->str
->writeflag
= 1;
4431 /* Write the output, interspersing precompiled strings in their */
4432 /* appropriate places. */
4436 STRINGDEF
*next_string
;
4437 U_CHAR
*cur_buf_loc
;
4438 int line_command_len
= 80;
4439 char *line_command
= xmalloc (line_command_len
);
4442 /* In each run through the loop, either cur_buf_loc == */
4443 /* next_string_loc, in which case we print a series of strings, or */
4444 /* it is less than next_string_loc, in which case we write some of */
4446 cur_buf_loc
= outbuf
.buf
;
4447 next_string
= stringlist
;
4449 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
4451 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
4452 if (next_string
->writeflag
) {
4453 len
= strlen (next_string
->filename
);
4454 if (len
> line_command_len
)
4455 line_command
= xrealloc (line_command
,
4456 line_command_len
*= 2);
4457 sprintf (line_command
, "\n# %d \"%s\"\n",
4458 next_string
->lineno
, next_string
->filename
);
4459 write (fileno (stdout
), line_command
,
4460 strlen (line_command
));
4461 write (fileno (stdout
),
4462 next_string
->contents
, next_string
->len
);
4464 next_string
= next_string
->chain
;
4468 ? (next_string
->output_mark
4469 - (cur_buf_loc
- outbuf
.buf
))
4470 : outbuf
.bufp
- cur_buf_loc
);
4472 write (fileno (stdout
), cur_buf_loc
, len
);
4478 /* Pass a directive through to the output file.
4479 BUF points to the contents of the directive, as a contiguous string.
4480 LIMIT points to the first character past the end of the directive.
4481 KEYWORD is the keyword-table entry for the directive. */
4484 pass_thru_directive (buf
, limit
, op
, keyword
)
4485 U_CHAR
*buf
, *limit
;
4487 struct directive
*keyword
;
4489 register unsigned keyword_length
= keyword
->length
;
4491 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
4493 bcopy (keyword
->name
, op
->bufp
, keyword_length
);
4494 op
->bufp
+= keyword_length
;
4495 if (limit
!= buf
&& buf
[0] != ' ')
4497 bcopy (buf
, op
->bufp
, limit
- buf
);
4498 op
->bufp
+= (limit
- buf
);
4502 /* The arglist structure is built by do_define to tell
4503 collect_definition where the argument names begin. That
4504 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
4505 would contain pointers to the strings x, y, and z.
4506 Collect_definition would then build a DEFINITION node,
4507 with reflist nodes pointing to the places x, y, and z had
4508 appeared. So the arglist is just convenience data passed
4509 between these two routines. It is not kept around after
4510 the current #define has been processed and entered into the
4514 struct arglist
*next
;
4520 /* Create a DEFINITION node from a #define directive. Arguments are
4521 as for do_define. */
4523 create_definition (buf
, limit
, op
)
4524 U_CHAR
*buf
, *limit
;
4527 U_CHAR
*bp
; /* temp ptr into input buffer */
4528 U_CHAR
*symname
; /* remember where symbol name starts */
4529 int sym_length
; /* and how long it is */
4530 int line
= instack
[indepth
].lineno
;
4531 char *file
= instack
[indepth
].nominal_fname
;
4534 int arglengths
= 0; /* Accumulate lengths of arg names
4535 plus number of args. */
4540 while (is_hor_space
[*bp
])
4543 symname
= bp
; /* remember where it starts */
4544 sym_length
= check_macro_name (bp
, "macro");
4547 /* Lossage will occur if identifiers or control keywords are broken
4548 across lines using backslash. This is not the right place to take
4552 struct arglist
*arg_ptrs
= NULL
;
4555 bp
++; /* skip '(' */
4556 SKIP_WHITE_SPACE (bp
);
4558 /* Loop over macro argument names. */
4559 while (*bp
!= ')') {
4560 struct arglist
*temp
;
4562 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
4564 temp
->next
= arg_ptrs
;
4565 temp
->argno
= argno
++;
4568 if (!is_idstart
[*bp
])
4569 pedwarn ("parameter name starts with a digit in `#define'");
4571 /* Find the end of the arg name. */
4572 while (is_idchar
[*bp
]) {
4575 temp
->length
= bp
- temp
->name
;
4576 arglengths
+= temp
->length
+ 2;
4577 SKIP_WHITE_SPACE (bp
);
4578 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
4579 error ("badly punctuated parameter list in `#define'");
4584 SKIP_WHITE_SPACE (bp
);
4587 error ("unterminated parameter list in `#define'");
4591 struct arglist
*otemp
;
4593 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
4594 if (temp
->length
== otemp
->length
&&
4595 strncmp(temp
->name
, otemp
->name
, temp
->length
) == 0) {
4598 name
= (U_CHAR
*) alloca(temp
->length
+ 1);
4599 (void) strncpy(name
, temp
->name
, temp
->length
);
4600 name
[temp
->length
] = '\0';
4601 error ("duplicate argument name `%s' in `#define'", name
);
4607 ++bp
; /* skip paren */
4608 /* Skip exactly one space or tab if any. */
4609 if (bp
< limit
&& (*bp
== ' ' || *bp
== '\t')) ++bp
;
4610 /* now everything from bp before limit is the definition. */
4611 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
4613 /* Now set defn->args.argnames to the result of concatenating
4614 the argument names in reverse order
4615 with comma-space between them. */
4616 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
4618 struct arglist
*temp
;
4620 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
4621 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
4623 if (temp
->next
!= 0) {
4624 defn
->args
.argnames
[i
++] = ',';
4625 defn
->args
.argnames
[i
++] = ' ';
4628 defn
->args
.argnames
[i
] = 0;
4631 /* simple expansion or empty definition; gobble it */
4632 if (is_hor_space
[*bp
])
4633 ++bp
; /* skip exactly one blank/tab char */
4634 /* now everything from bp before limit is the definition. */
4635 defn
= collect_expansion (bp
, limit
, -1, 0);
4636 defn
->args
.argnames
= (U_CHAR
*) "";
4642 /* OP is null if this is a predefinition */
4643 defn
->predefined
= !op
;
4645 mdef
.symnam
= symname
;
4646 mdef
.symlen
= sym_length
;
4655 /* Process a #define command.
4656 BUF points to the contents of the #define command, as a contiguous string.
4657 LIMIT points to the first character past the end of the definition.
4658 KEYWORD is the keyword-table entry for #define. */
4661 do_define (buf
, limit
, op
, keyword
)
4662 U_CHAR
*buf
, *limit
;
4664 struct directive
*keyword
;
4669 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
4670 if (pcp_outfile
&& op
)
4671 pass_thru_directive (buf
, limit
, op
, keyword
);
4673 mdef
= create_definition (buf
, limit
, op
);
4677 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
4681 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
4683 /* Redefining a precompiled key is ok. */
4684 if (hp
->type
== T_PCSTRING
)
4686 /* Redefining a macro is ok if the definitions are the same. */
4687 else if (hp
->type
== T_MACRO
)
4688 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
4689 /* Redefining a constant is ok with -D. */
4690 else if (hp
->type
== T_CONST
)
4691 ok
= ! done_initializing
;
4692 /* Print the warning if it's not ok. */
4694 U_CHAR
*msg
; /* what pain... */
4696 /* If we are passing through #define and #undef directives, do
4697 that for this re-definition now. */
4698 if (debug_output
&& op
)
4699 pass_thru_directive (buf
, limit
, op
, keyword
);
4701 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
4703 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
4704 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
4706 if (hp
->type
== T_MACRO
)
4707 pedwarn_with_file_and_line (hp
->value
.defn
->file
, hp
->value
.defn
->line
,
4708 "this is the location of the previous definition");
4710 /* Replace the old definition. */
4712 hp
->value
.defn
= mdef
.defn
;
4714 /* If we are passing through #define and #undef directives, do
4715 that for this new definition now. */
4716 if (debug_output
&& op
)
4717 pass_thru_directive (buf
, limit
, op
, keyword
);
4718 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, mdef
.defn
, hashcode
);
4729 /* Check a purported macro name SYMNAME, and yield its length.
4730 USAGE is the kind of name this is intended for. */
4733 check_macro_name (symname
, usage
)
4740 for (p
= symname
; is_idchar
[*p
]; p
++)
4742 sym_length
= p
- symname
;
4743 if (sym_length
== 0)
4744 error ("invalid %s name", usage
);
4745 else if (!is_idstart
[*symname
]) {
4746 U_CHAR
*msg
; /* what pain... */
4747 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
4748 bcopy (symname
, msg
, sym_length
);
4749 msg
[sym_length
] = 0;
4750 error ("invalid %s name `%s'", usage
, msg
);
4752 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
4753 error ("invalid %s name `defined'", usage
);
4759 * return zero if two DEFINITIONs are isomorphic
4762 compare_defs (d1
, d2
)
4763 DEFINITION
*d1
, *d2
;
4765 register struct reflist
*a1
, *a2
;
4766 register U_CHAR
*p1
= d1
->expansion
;
4767 register U_CHAR
*p2
= d2
->expansion
;
4770 if (d1
->nargs
!= d2
->nargs
)
4772 if (strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
4774 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
4775 a1
= a1
->next
, a2
= a2
->next
) {
4776 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
4777 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
4778 || a1
->argno
!= a2
->argno
4779 || a1
->stringify
!= a2
->stringify
4780 || a1
->raw_before
!= a2
->raw_before
4781 || a1
->raw_after
!= a2
->raw_after
)
4789 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
4790 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
4795 /* Return 1 if two parts of two macro definitions are effectively different.
4796 One of the parts starts at BEG1 and has LEN1 chars;
4797 the other has LEN2 chars at BEG2.
4798 Any sequence of whitespace matches any other sequence of whitespace.
4799 FIRST means these parts are the first of a macro definition;
4800 so ignore leading whitespace entirely.
4801 LAST means these parts are the last of a macro definition;
4802 so ignore trailing whitespace entirely. */
4805 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
4807 U_CHAR
*beg1
, *beg2
;
4811 register U_CHAR
*end1
= beg1
+ len1
;
4812 register U_CHAR
*end2
= beg2
+ len2
;
4814 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
4815 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
4818 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
4819 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
4821 while (beg1
!= end1
&& beg2
!= end2
) {
4822 if (is_space
[*beg1
] && is_space
[*beg2
]) {
4823 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
4824 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
4825 } else if (*beg1
== *beg2
) {
4829 return (beg1
!= end1
) || (beg2
!= end2
);
4832 /* Read a replacement list for a macro with parameters.
4833 Build the DEFINITION structure.
4834 Reads characters of text starting at BUF until END.
4835 ARGLIST specifies the formal parameters to look for
4836 in the text of the definition; NARGS is the number of args
4837 in that list, or -1 for a macro name that wants no argument list.
4838 MACRONAME is the macro name itself (so we can avoid recursive expansion)
4839 and NAMELEN is its length in characters.
4841 Note that comments and backslash-newlines have already been deleted
4842 from the argument. */
4844 /* Leading and trailing Space, Tab, etc. are converted to markers
4845 Newline Space, Newline Tab, etc.
4846 Newline Space makes a space in the final output
4847 but is discarded if stringified. (Newline Tab is similar but
4848 makes a Tab instead.)
4850 If there is no trailing whitespace, a Newline Space is added at the end
4851 to prevent concatenation that would be contrary to the standard. */
4854 collect_expansion (buf
, end
, nargs
, arglist
)
4857 struct arglist
*arglist
;
4860 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
4861 struct reflist
*endpat
= NULL
;
4862 /* Pointer to first nonspace after last ## seen. */
4864 /* Pointer to first nonspace after last single-# seen. */
4865 U_CHAR
*stringify
= 0;
4867 int expected_delimiter
= '\0';
4869 /* Scan thru the replacement list, ignoring comments and quoted
4870 strings, picking up on the macro calls. It does a linear search
4871 thru the arg list on every potential symbol. Profiling might say
4872 that something smarter should happen. */
4877 /* Find the beginning of the trailing whitespace. */
4878 /* Find end of leading whitespace. */
4881 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
4882 while (p
< limit
&& is_space
[*p
]) p
++;
4884 /* Allocate space for the text in the macro definition.
4885 Leading and trailing whitespace chars need 2 bytes each.
4886 Each other input char may or may not need 1 byte,
4887 so this is an upper bound.
4888 The extra 2 are for invented trailing newline-marker and final null. */
4889 maxsize
= (sizeof (DEFINITION
)
4890 + 2 * (end
- limit
) + 2 * (p
- buf
)
4892 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
4894 defn
->nargs
= nargs
;
4895 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
4900 /* Convert leading whitespace to Newline-markers. */
4901 while (p
< limit
&& is_space
[*p
]) {
4906 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
4907 error ("`##' at start of macro definition");
4911 /* Process the main body of the definition. */
4913 int skipped_arg
= 0;
4914 register U_CHAR c
= *p
++;
4922 if (expected_delimiter
!= '\0') {
4923 if (c
== expected_delimiter
)
4924 expected_delimiter
= '\0';
4926 expected_delimiter
= c
;
4929 /* Special hack: if a \# is written in the #define
4930 include a # in the definition. This is useless for C code
4931 but useful for preprocessing other things. */
4934 /* \# quotes a # even outside of strings. */
4935 if (p
< limit
&& *p
== '#' && !expected_delimiter
) {
4938 } else if (p
< limit
&& expected_delimiter
) {
4939 /* In a string, backslash goes through
4940 and makes next char ordinary. */
4946 /* # is ordinary inside a string. */
4947 if (expected_delimiter
)
4949 if (p
< limit
&& *p
== '#') {
4950 /* ##: concatenate preceding and following tokens. */
4951 /* Take out the first #, discard preceding whitespace. */
4953 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
4955 /* Skip the second #. */
4957 /* Discard following whitespace. */
4958 SKIP_WHITE_SPACE (p
);
4961 error ("`##' at end of macro definition");
4963 /* Single #: stringify following argument ref.
4964 Don't leave the # in the expansion. */
4966 SKIP_WHITE_SPACE (p
);
4967 if (p
== limit
|| ! is_idstart
[*p
] || nargs
<= 0)
4968 error ("`#' operator is not followed by a macro argument name");
4975 /* In -traditional mode, recognize arguments inside strings and
4976 and character constants, and ignore special properties of #.
4977 Arguments inside strings are considered "stringified", but no
4978 extra quote marks are supplied. */
4982 if (expected_delimiter
!= '\0') {
4983 if (c
== expected_delimiter
)
4984 expected_delimiter
= '\0';
4986 expected_delimiter
= c
;
4990 /* Backslash quotes delimiters and itself, but not macro args. */
4991 if (expected_delimiter
!= 0 && p
< limit
4992 && (*p
== expected_delimiter
|| *p
== '\\')) {
4999 if (expected_delimiter
!= '\0') /* No comments inside strings. */
5002 /* If we find a comment that wasn't removed by handle_directive,
5003 this must be -traditional. So replace the comment with
5007 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
5010 /* Mark this as a concatenation-point, as if it had been ##. */
5018 /* Handle the start of a symbol. */
5019 if (is_idchar
[c
] && nargs
> 0) {
5020 U_CHAR
*id_beg
= p
- 1;
5024 while (p
!= limit
&& is_idchar
[*p
]) p
++;
5025 id_len
= p
- id_beg
;
5027 if (is_idstart
[c
]) {
5028 register struct arglist
*arg
;
5030 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
5031 struct reflist
*tpat
;
5033 if (arg
->name
[0] == c
5034 && arg
->length
== id_len
5035 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
5036 if (expected_delimiter
&& warn_stringify
) {
5038 warning ("macro argument `%.*s' is stringified.",
5041 warning ("macro arg `%.*s' would be stringified with -traditional.",
5045 /* If ANSI, don't actually substitute inside a string. */
5046 if (!traditional
&& expected_delimiter
)
5048 /* make a pat node for this arg and append it to the end of
5050 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
5052 tpat
->raw_before
= concat
== id_beg
;
5053 tpat
->raw_after
= 0;
5054 tpat
->stringify
= (traditional
? expected_delimiter
!= '\0'
5055 : stringify
== id_beg
);
5058 defn
->pattern
= tpat
;
5060 endpat
->next
= tpat
;
5063 tpat
->argno
= arg
->argno
;
5064 tpat
->nchars
= exp_p
- lastp
;
5066 register U_CHAR
*p1
= p
;
5067 SKIP_WHITE_SPACE (p1
);
5068 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
5069 tpat
->raw_after
= 1;
5071 lastp
= exp_p
; /* place to start copying from next time */
5078 /* If this was not a macro arg, copy it into the expansion. */
5079 if (! skipped_arg
) {
5080 register U_CHAR
*lim1
= p
;
5084 if (stringify
== id_beg
)
5085 error ("`#' operator should be followed by a macro argument name");
5091 /* Convert trailing whitespace to Newline-markers. */
5092 while (limit
< end
&& is_space
[*limit
]) {
5094 *exp_p
++ = *limit
++;
5096 } else if (!traditional
) {
5097 /* There is no trailing whitespace, so invent some. */
5104 defn
->length
= exp_p
- defn
->expansion
;
5106 /* Crash now if we overrun the allocated size. */
5107 if (defn
->length
+ 1 > maxsize
)
5111 /* This isn't worth the time it takes. */
5112 /* give back excess storage */
5113 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
5120 do_assert (buf
, limit
, op
, keyword
)
5121 U_CHAR
*buf
, *limit
;
5123 struct directive
*keyword
;
5125 U_CHAR
*bp
; /* temp ptr into input buffer */
5126 U_CHAR
*symname
; /* remember where symbol name starts */
5127 int sym_length
; /* and how long it is */
5128 struct arglist
*tokens
= NULL
;
5130 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
5131 pedwarn ("ANSI C does not allow `#assert'");
5135 while (is_hor_space
[*bp
])
5138 symname
= bp
; /* remember where it starts */
5139 sym_length
= check_macro_name (bp
, "assertion");
5141 /* #define doesn't do this, but we should. */
5142 SKIP_WHITE_SPACE (bp
);
5144 /* Lossage will occur if identifiers or control tokens are broken
5145 across lines using backslash. This is not the right place to take
5149 error ("missing token-sequence in `#assert'");
5156 bp
++; /* skip '(' */
5157 SKIP_WHITE_SPACE (bp
);
5159 tokens
= read_token_list (&bp
, limit
, &error_flag
);
5163 error ("empty token-sequence in `#assert'");
5167 ++bp
; /* skip paren */
5168 SKIP_WHITE_SPACE (bp
);
5171 /* If this name isn't already an assertion name, make it one.
5172 Error if it was already in use in some other way. */
5175 ASSERTION_HASHNODE
*hp
;
5176 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
5177 struct tokenlist_list
*value
5178 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
5180 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
5182 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
5183 error ("`defined' redefined as assertion");
5184 hp
= assertion_install (symname
, sym_length
, hashcode
);
5187 /* Add the spec'd token-sequence to the list of such. */
5188 value
->tokens
= tokens
;
5189 value
->next
= hp
->value
;
5197 do_unassert (buf
, limit
, op
, keyword
)
5198 U_CHAR
*buf
, *limit
;
5200 struct directive
*keyword
;
5202 U_CHAR
*bp
; /* temp ptr into input buffer */
5203 U_CHAR
*symname
; /* remember where symbol name starts */
5204 int sym_length
; /* and how long it is */
5206 struct arglist
*tokens
= NULL
;
5207 int tokens_specified
= 0;
5209 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
5210 pedwarn ("ANSI C does not allow `#unassert'");
5214 while (is_hor_space
[*bp
])
5217 symname
= bp
; /* remember where it starts */
5218 sym_length
= check_macro_name (bp
, "assertion");
5220 /* #define doesn't do this, but we should. */
5221 SKIP_WHITE_SPACE (bp
);
5223 /* Lossage will occur if identifiers or control tokens are broken
5224 across lines using backslash. This is not the right place to take
5230 bp
++; /* skip '(' */
5231 SKIP_WHITE_SPACE (bp
);
5233 tokens
= read_token_list (&bp
, limit
, &error_flag
);
5237 error ("empty token list in `#unassert'");
5241 tokens_specified
= 1;
5243 ++bp
; /* skip paren */
5244 SKIP_WHITE_SPACE (bp
);
5248 ASSERTION_HASHNODE
*hp
;
5249 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
5250 struct tokenlist_list
*tail
, *prev
;
5252 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
5256 /* If no token list was specified, then eliminate this assertion
5258 if (! tokens_specified
) {
5259 struct tokenlist_list
*next
;
5260 for (tail
= hp
->value
; tail
; tail
= next
) {
5262 free_token_list (tail
->tokens
);
5265 delete_assertion (hp
);
5267 /* If a list of tokens was given, then delete any matching list. */
5272 struct tokenlist_list
*next
= tail
->next
;
5273 if (compare_token_lists (tail
->tokens
, tokens
)) {
5277 hp
->value
= tail
->next
;
5278 free_token_list (tail
->tokens
);
5291 /* Test whether there is an assertion named NAME
5292 and optionally whether it has an asserted token list TOKENS.
5293 NAME is not null terminated; its length is SYM_LENGTH.
5294 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
5297 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
5300 int tokens_specified
;
5301 struct arglist
*tokens
;
5303 ASSERTION_HASHNODE
*hp
;
5304 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
5306 if (pedantic
&& !instack
[indepth
].system_header_p
)
5307 pedwarn ("ANSI C does not allow testing assertions");
5309 hp
= assertion_lookup (name
, sym_length
, hashcode
);
5311 /* It is not an assertion; just return false. */
5314 /* If no token list was specified, then value is 1. */
5315 if (! tokens_specified
)
5319 struct tokenlist_list
*tail
;
5323 /* If a list of tokens was given,
5324 then succeed if the assertion records a matching list. */
5327 if (compare_token_lists (tail
->tokens
, tokens
))
5332 /* Fail if the assertion has no matching list. */
5337 /* Compare two lists of tokens for equality including order of tokens. */
5340 compare_token_lists (l1
, l2
)
5341 struct arglist
*l1
, *l2
;
5344 if (l1
->length
!= l2
->length
)
5346 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
5352 /* Succeed if both lists end at the same time. */
5356 /* Read a space-separated list of tokens ending in a close parenthesis.
5357 Return a list of strings, in the order they were written.
5358 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
5359 Parse the text starting at *BPP, and update *BPP.
5360 Don't parse beyond LIMIT. */
5362 static struct arglist
*
5363 read_token_list (bpp
, limit
, error_flag
)
5368 struct arglist
*token_ptrs
= 0;
5374 /* Loop over the assertion value tokens. */
5376 struct arglist
*temp
;
5380 /* Find the end of the token. */
5384 } else if (*bp
== ')') {
5389 } else if (*bp
== '"' || *bp
== '\'')
5390 bp
= skip_quoted_string (bp
, limit
, 0, 0, 0, &eofp
);
5392 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
5393 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
5396 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
5397 temp
->name
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
5398 bcopy (beg
, temp
->name
, bp
- beg
);
5399 temp
->name
[bp
- beg
] = 0;
5400 temp
->next
= token_ptrs
;
5402 temp
->length
= bp
- beg
;
5404 SKIP_WHITE_SPACE (bp
);
5407 error ("unterminated token sequence in `#assert' or `#unassert'");
5414 /* We accumulated the names in reverse order.
5415 Now reverse them to get the proper order. */
5417 register struct arglist
*prev
= 0, *this, *next
;
5418 for (this = token_ptrs
; this; this = next
) {
5428 free_token_list (tokens
)
5429 struct arglist
*tokens
;
5432 struct arglist
*next
= tokens
->next
;
5433 free (tokens
->name
);
5440 * Install a name in the assertion hash table.
5442 * If LEN is >= 0, it is the length of the name.
5443 * Otherwise, compute the length by scanning the entire name.
5445 * If HASH is >= 0, it is the precomputed hash code.
5446 * Otherwise, compute the hash code.
5448 static ASSERTION_HASHNODE
*
5449 assertion_install (name
, len
, hash
)
5454 register ASSERTION_HASHNODE
*hp
;
5455 register int i
, bucket
;
5456 register U_CHAR
*p
, *q
;
5458 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
5459 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
5461 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
5462 hp
->next
= assertion_hashtab
[bucket
];
5463 assertion_hashtab
[bucket
] = hp
;
5465 if (hp
->next
!= NULL
)
5466 hp
->next
->prev
= hp
;
5469 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
5472 for (i
= 0; i
< len
; i
++)
5479 * find the most recent hash node for name name (ending with first
5480 * non-identifier char) installed by install
5482 * If LEN is >= 0, it is the length of the name.
5483 * Otherwise, compute the length by scanning the entire name.
5485 * If HASH is >= 0, it is the precomputed hash code.
5486 * Otherwise, compute the hash code.
5488 static ASSERTION_HASHNODE
*
5489 assertion_lookup (name
, len
, hash
)
5494 register U_CHAR
*bp
;
5495 register ASSERTION_HASHNODE
*bucket
;
5497 bucket
= assertion_hashtab
[hash
];
5499 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
5501 bucket
= bucket
->next
;
5507 delete_assertion (hp
)
5508 ASSERTION_HASHNODE
*hp
;
5511 if (hp
->prev
!= NULL
)
5512 hp
->prev
->next
= hp
->next
;
5513 if (hp
->next
!= NULL
)
5514 hp
->next
->prev
= hp
->prev
;
5516 /* make sure that the bucket chain header that
5517 the deleted guy was on points to the right thing afterwards. */
5518 if (hp
== *hp
->bucket_hdr
)
5519 *hp
->bucket_hdr
= hp
->next
;
5525 * interpret #line command. Remembers previously seen fnames
5526 * in its very own hash table.
5528 #define FNAME_HASHSIZE 37
5531 do_line (buf
, limit
, op
, keyword
)
5532 U_CHAR
*buf
, *limit
;
5534 struct directive
*keyword
;
5536 register U_CHAR
*bp
;
5537 FILE_BUF
*ip
= &instack
[indepth
];
5540 enum file_change_code file_change
= same_file
;
5542 /* Expand any macros. */
5543 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
5545 /* Point to macroexpanded line, which is null-terminated now. */
5547 SKIP_WHITE_SPACE (bp
);
5549 if (!isdigit (*bp
)) {
5550 error ("invalid format `#line' command");
5554 /* The Newline at the end of this line remains to be processed.
5555 To put the next line at the specified line number,
5556 we must store a line number now that is one less. */
5557 new_lineno
= atoi (bp
) - 1;
5559 /* skip over the line number. */
5560 while (isdigit (*bp
))
5563 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
5564 if (*bp
&& !is_space
[*bp
]) {
5565 error ("invalid format `#line' command");
5570 SKIP_WHITE_SPACE (bp
);
5573 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
5574 HASHNODE
*hp
, **hash_bucket
;
5580 while (*bp
&& *bp
!= '\"')
5583 error ("invalid format `#line' command");
5587 fname_length
= bp
- fname
;
5590 SKIP_WHITE_SPACE (bp
);
5593 file_change
= enter_file
;
5594 else if (*bp
== '2')
5595 file_change
= leave_file
;
5596 else if (*bp
== '3')
5597 ip
->system_header_p
= 1;
5599 error ("invalid format `#line' command");
5604 SKIP_WHITE_SPACE (bp
);
5606 ip
->system_header_p
= 1;
5608 SKIP_WHITE_SPACE (bp
);
5611 error ("invalid format `#line' command");
5617 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
5618 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
5619 if (hp
->length
== fname_length
&&
5620 strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
5621 ip
->nominal_fname
= hp
->value
.cpval
;
5625 /* Didn't find it; cons up a new one. */
5626 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
5627 hp
->next
= *hash_bucket
;
5630 hp
->length
= fname_length
;
5631 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
5632 bcopy (fname
, hp
->value
.cpval
, fname_length
);
5635 error ("invalid format `#line' command");
5639 ip
->lineno
= new_lineno
;
5640 output_line_command (ip
, op
, 0, file_change
);
5641 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
5646 * remove the definition of a symbol from the symbol table.
5647 * according to un*x /lib/cpp, it is not an error to undef
5648 * something that has no definitions, so it isn't one here either.
5652 do_undef (buf
, limit
, op
, keyword
)
5653 U_CHAR
*buf
, *limit
;
5655 struct directive
*keyword
;
5659 U_CHAR
*orig_buf
= buf
;
5661 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
5662 if (pcp_outfile
&& op
)
5663 pass_thru_directive (buf
, limit
, op
, keyword
);
5665 SKIP_WHITE_SPACE (buf
);
5666 sym_length
= check_macro_name (buf
, "macro");
5668 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
5669 /* If we are generating additional info for debugging (with -g) we
5670 need to pass through all effective #undef commands. */
5671 if (debug_output
&& op
)
5672 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
5673 if (hp
->type
!= T_MACRO
)
5674 warning ("undefining `%s'", hp
->name
);
5680 SKIP_WHITE_SPACE (buf
);
5682 pedwarn ("garbage after `#undef' directive");
5688 * Report a fatal error detected by the program we are processing.
5689 * Use the text of the line in the error message, then terminate.
5690 * (We use error() because it prints the filename & line#.)
5694 do_error (buf
, limit
, op
, keyword
)
5695 U_CHAR
*buf
, *limit
;
5697 struct directive
*keyword
;
5699 int length
= limit
- buf
;
5700 char *copy
= (char *) xmalloc (length
+ 1);
5701 bcopy (buf
, copy
, length
);
5703 SKIP_WHITE_SPACE (copy
);
5704 error ("#error %s", copy
);
5705 exit (FAILURE_EXIT_CODE
);
5711 * Report a warning detected by the program we are processing.
5712 * Use the text of the line in the warning message, then continue.
5713 * (We use error() because it prints the filename & line#.)
5717 do_warning (buf
, limit
, op
, keyword
)
5718 U_CHAR
*buf
, *limit
;
5720 struct directive
*keyword
;
5722 int length
= limit
- buf
;
5723 char *copy
= (char *) xmalloc (length
+ 1);
5724 bcopy (buf
, copy
, length
);
5726 SKIP_WHITE_SPACE (copy
);
5727 error ("#warning %s", copy
);
5731 /* Remember the name of the current file being read from so that we can
5732 avoid ever including it again. */
5738 FILE_BUF
*ip
= NULL
;
5740 for (i
= indepth
; i
>= 0; i
--)
5741 if (instack
[i
].fname
!= NULL
) {
5747 struct file_name_list
*new;
5749 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5750 new->next
= dont_repeat_files
;
5751 dont_repeat_files
= new;
5752 new->fname
= savestring (ip
->fname
);
5753 new->control_macro
= 0;
5758 /* #ident has already been copied to the output file, so just ignore it. */
5761 do_ident (buf
, limit
)
5762 U_CHAR
*buf
, *limit
;
5764 /* Allow #ident in system headers, since that's not user's fault. */
5765 if (pedantic
&& !instack
[indepth
].system_header_p
)
5766 pedwarn ("ANSI C does not allow `#ident'");
5770 /* #pragma and its argument line have already been copied to the output file.
5771 Here just check for recognized pragmas. */
5774 do_pragma (buf
, limit
)
5775 U_CHAR
*buf
, *limit
;
5777 while (*buf
== ' ' || *buf
== '\t')
5779 if (!strncmp (buf
, "once", 4)) {
5780 warning ("`#pragma once' is obsolete");
5787 /* This was a fun hack, but #pragma seems to start to be useful.
5788 By failing to recognize it, we pass it through unchanged to cc1. */
5791 * the behavior of the #pragma directive is implementation defined.
5792 * this implementation defines it as follows.
5799 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
5802 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
5804 execl ("/usr/games/hack", "#pragma", 0);
5805 execl ("/usr/games/rogue", "#pragma", 0);
5806 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
5807 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
5809 fatal ("You are in a maze of twisty compiler features, all different");
5813 /* Just ignore #sccs, on systems where we define it at all. */
5819 pedwarn ("ANSI C does not allow `#sccs'");
5824 * handle #if command by
5825 * 1) inserting special `defined' keyword into the hash table
5826 * that gets turned into 0 or 1 by special_symbol (thus,
5827 * if the luser has a symbol called `defined' already, it won't
5828 * work inside the #if command)
5829 * 2) rescan the input into a temporary output buffer
5830 * 3) pass the output buffer to the yacc parser and collect a value
5831 * 4) clean up the mess left from steps 1 and 2.
5832 * 5) call conditional_skip to skip til the next #endif (etc.),
5833 * or not, depending on the value from step 3.
5837 do_if (buf
, limit
, op
, keyword
)
5838 U_CHAR
*buf
, *limit
;
5840 struct directive
*keyword
;
5843 FILE_BUF
*ip
= &instack
[indepth
];
5845 value
= eval_if_expression (buf
, limit
- buf
);
5846 conditional_skip (ip
, value
== 0, T_IF
, 0);
5851 * handle a #elif directive by not changing if_stack either.
5852 * see the comment above do_else.
5856 do_elif (buf
, limit
, op
, keyword
)
5857 U_CHAR
*buf
, *limit
;
5859 struct directive
*keyword
;
5862 FILE_BUF
*ip
= &instack
[indepth
];
5864 if (if_stack
== instack
[indepth
].if_stack
) {
5865 error ("`#elif' not within a conditional");
5868 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
5869 error ("`#elif' after `#else'");
5870 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
5871 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
5872 strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
5873 fprintf (stderr
, ", file %s", if_stack
->fname
);
5874 fprintf (stderr
, ")\n");
5876 if_stack
->type
= T_ELIF
;
5879 if (if_stack
->if_succeeded
)
5880 skip_if_group (ip
, 0);
5882 value
= eval_if_expression (buf
, limit
- buf
);
5884 skip_if_group (ip
, 0);
5886 ++if_stack
->if_succeeded
; /* continue processing input */
5887 output_line_command (ip
, op
, 1, same_file
);
5894 * evaluate a #if expression in BUF, of length LENGTH,
5895 * then parse the result as a C expression and return the value as an int.
5898 eval_if_expression (buf
, length
)
5903 HASHNODE
*save_defined
;
5906 save_defined
= install ("defined", -1, T_SPEC_DEFINED
, 0, -1);
5908 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
5910 delete_macro (save_defined
); /* clean up special symbol */
5912 value
= parse_c_expression (temp_obuf
.buf
);
5914 free (temp_obuf
.buf
);
5920 * routine to handle ifdef/ifndef. Try to look up the symbol,
5921 * then do or don't skip to the #endif/#else/#elif depending
5922 * on what directive is actually being processed.
5926 do_xifdef (buf
, limit
, op
, keyword
)
5927 U_CHAR
*buf
, *limit
;
5929 struct directive
*keyword
;
5932 FILE_BUF
*ip
= &instack
[indepth
];
5934 int start_of_file
= 0;
5935 U_CHAR
*control_macro
= 0;
5937 /* Detect a #ifndef at start of file (not counting comments). */
5938 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
5939 U_CHAR
*p
= ip
->buf
;
5940 while (p
!= directive_start
) {
5948 if (p
!= ip
->bufp
&& *p
== '*') {
5949 /* Skip this comment. */
5951 U_CHAR
*save_bufp
= ip
->bufp
;
5953 p
= skip_to_end_of_comment (ip
, &junk
, 1);
5954 ip
->bufp
= save_bufp
;
5961 /* If we get here, this conditional is the beginning of the file. */
5966 /* Discard leading and trailing whitespace. */
5967 SKIP_WHITE_SPACE (buf
);
5968 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
5970 /* Find the end of the identifier at the beginning. */
5971 for (end
= buf
; is_idchar
[*end
]; end
++);
5974 skip
= (keyword
->type
== T_IFDEF
);
5976 pedwarn (end
== limit
? "`#%s' with no argument"
5977 : "`#%s' argument starts with punctuation",
5982 if (pedantic
&& buf
[0] >= '0' && buf
[0] <= '9')
5983 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
5984 else if (end
!= limit
&& !traditional
)
5985 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
5987 hp
= lookup (buf
, end
-buf
, -1);
5990 /* Output a precondition for this macro. */
5991 if (hp
&& hp
->value
.defn
->predefined
)
5992 fprintf(pcp_outfile
, "#define %s\n", hp
->name
);
5995 fprintf(pcp_outfile
, "#undef ");
5996 while (is_idchar
[*cp
]) /* Ick! */
5997 fputc (*cp
++, pcp_outfile
);
5998 putc ('\n', pcp_outfile
);
6002 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
6003 if (start_of_file
&& !skip
) {
6004 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
6005 bcopy (buf
, control_macro
, end
- buf
);
6006 control_macro
[end
- buf
] = 0;
6010 conditional_skip (ip
, skip
, T_IF
, control_macro
);
6014 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
6015 If this is a #ifndef starting at the beginning of a file,
6016 CONTROL_MACRO is the macro name tested by the #ifndef.
6017 Otherwise, CONTROL_MACRO is 0. */
6020 conditional_skip (ip
, skip
, type
, control_macro
)
6023 enum node_type type
;
6024 U_CHAR
*control_macro
;
6026 IF_STACK_FRAME
*temp
;
6028 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
6029 temp
->fname
= ip
->nominal_fname
;
6030 temp
->lineno
= ip
->lineno
;
6031 temp
->next
= if_stack
;
6032 temp
->control_macro
= control_macro
;
6035 if_stack
->type
= type
;
6038 skip_if_group (ip
, 0);
6041 ++if_stack
->if_succeeded
;
6042 output_line_command (ip
, &outbuf
, 1, same_file
);
6047 * skip to #endif, #else, or #elif. adjust line numbers, etc.
6048 * leaves input ptr at the sharp sign found.
6049 * If ANY is nonzero, return at next directive of any sort.
6052 skip_if_group (ip
, any
)
6056 register U_CHAR
*bp
= ip
->bufp
, *cp
;
6057 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
6058 struct directive
*kt
;
6059 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
6060 U_CHAR
*beg_of_line
= bp
;
6061 register int ident_length
;
6062 U_CHAR
*ident
, *after_ident
;
6066 case '/': /* possible comment */
6067 if (*bp
== '\\' && bp
[1] == '\n')
6070 || ((cplusplus
|| objc
) && *bp
== '/')) {
6072 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
6077 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
, 0, 0);
6080 /* Char after backslash loses its special meaning. */
6083 ++ip
->lineno
; /* But do update the line-count. */
6094 /* # keyword: a # must be first nonblank char on the line */
6095 if (beg_of_line
== 0)
6097 /* Scan from start of line, skipping whitespace, comments
6098 and backslash-newlines, and see if we reach this #.
6099 If not, this # is not special. */
6102 if (is_hor_space
[*bp
])
6104 else if (*bp
== '\\' && bp
[1] == '\n')
6106 else if (*bp
== '/' && bp
[1] == '*') {
6108 while (!(*bp
== '*' && bp
[1] == '/'))
6111 } else if ((cplusplus
|| objc
) && *bp
== '/' && bp
[1] == '/') {
6113 while (*bp
++ != '\n') ;
6117 if (bp
!= ip
->bufp
) {
6118 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
6122 bp
= ip
->bufp
+ 1; /* Point after the '#' */
6124 /* Skip whitespace and \-newline. */
6126 if (is_hor_space
[*bp
])
6128 else if (*bp
== '\\' && bp
[1] == '\n')
6130 else if (*bp
== '/' && bp
[1] == '*') {
6132 while (!(*bp
== '*' && bp
[1] == '/')) {
6138 } else if ((cplusplus
|| objc
) && *bp
== '/' && bp
[1] == '/') {
6140 while (*bp
++ != '\n') ;
6147 /* Now find end of directive name.
6148 If we encounter a backslash-newline, exchange it with any following
6149 symbol-constituents so that we end up with a contiguous name. */
6155 if (*bp
== '\\' && bp
[1] == '\n')
6156 name_newline_fix (bp
);
6162 ident_length
= bp
- cp
;
6166 /* A line of just `#' becomes blank. */
6168 if (ident_length
== 0 && *after_ident
== '\n') {
6172 if (ident_length
== 0 || !is_idstart
[*ident
]) {
6174 while (is_idchar
[*p
]) {
6175 if (*p
< '0' || *p
> '9')
6179 /* Handle # followed by a line number. */
6180 if (p
!= ident
&& !is_idchar
[*p
]) {
6182 pedwarn ("`#' followed by integer");
6186 /* Avoid error for `###' and similar cases unless -pedantic. */
6188 while (*p
== '#' || is_hor_space
[*p
]) p
++;
6190 if (pedantic
&& !lang_asm
)
6191 pedwarn ("invalid preprocessor directive");
6196 if (!lang_asm
&& pedantic
)
6197 pedwarn ("invalid preprocessor directive name");
6201 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
6202 IF_STACK_FRAME
*temp
;
6203 if (ident_length
== kt
->length
6204 && strncmp (cp
, kt
->name
, kt
->length
) == 0) {
6205 /* If we are asked to return on next directive, do so now. */
6213 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
6214 temp
->next
= if_stack
;
6216 temp
->lineno
= ip
->lineno
;
6217 temp
->fname
= ip
->nominal_fname
;
6218 temp
->type
= kt
->type
;
6222 if (pedantic
&& if_stack
!= save_if_stack
)
6225 if (if_stack
== instack
[indepth
].if_stack
) {
6226 error ("`#%s' not within a conditional", kt
->name
);
6229 else if (if_stack
== save_if_stack
)
6230 return; /* found what we came for */
6232 if (kt
->type
!= T_ENDIF
) {
6233 if (if_stack
->type
== T_ELSE
)
6234 error ("`#else' or `#elif' after `#else'");
6235 if_stack
->type
= kt
->type
;
6240 if_stack
= if_stack
->next
;
6247 /* Don't let erroneous code go by. */
6248 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
6249 pedwarn ("invalid preprocessor directive name");
6253 /* after this returns, rescan will exit because ip->bufp
6254 now points to the end of the buffer.
6255 rescan is responsible for the error message also. */
6259 * handle a #else directive. Do this by just continuing processing
6260 * without changing if_stack ; this is so that the error message
6261 * for missing #endif's etc. will point to the original #if. It
6262 * is possible that something different would be better.
6266 do_else (buf
, limit
, op
, keyword
)
6267 U_CHAR
*buf
, *limit
;
6269 struct directive
*keyword
;
6271 FILE_BUF
*ip
= &instack
[indepth
];
6274 SKIP_WHITE_SPACE (buf
);
6276 pedwarn ("text following `#else' violates ANSI standard");
6279 if (if_stack
== instack
[indepth
].if_stack
) {
6280 error ("`#else' not within a conditional");
6283 /* #ifndef can't have its special treatment for containing the whole file
6284 if it has a #else clause. */
6285 if_stack
->control_macro
= 0;
6287 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
6288 error ("`#else' after `#else'");
6289 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
6290 if (strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
6291 fprintf (stderr
, ", file %s", if_stack
->fname
);
6292 fprintf (stderr
, ")\n");
6294 if_stack
->type
= T_ELSE
;
6297 if (if_stack
->if_succeeded
)
6298 skip_if_group (ip
, 0);
6300 ++if_stack
->if_succeeded
; /* continue processing input */
6301 output_line_command (ip
, op
, 1, same_file
);
6307 * unstack after #endif command
6311 do_endif (buf
, limit
, op
, keyword
)
6312 U_CHAR
*buf
, *limit
;
6314 struct directive
*keyword
;
6317 SKIP_WHITE_SPACE (buf
);
6319 pedwarn ("text following `#endif' violates ANSI standard");
6322 if (if_stack
== instack
[indepth
].if_stack
)
6323 error ("unbalanced `#endif'");
6325 IF_STACK_FRAME
*temp
= if_stack
;
6326 if_stack
= if_stack
->next
;
6327 if (temp
->control_macro
!= 0) {
6328 /* This #endif matched a #ifndef at the start of the file.
6329 See if it is at the end of the file. */
6330 FILE_BUF
*ip
= &instack
[indepth
];
6331 U_CHAR
*p
= ip
->bufp
;
6332 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
6342 if (p
!= ep
&& *p
== '*') {
6343 /* Skip this comment. */
6345 U_CHAR
*save_bufp
= ip
->bufp
;
6347 p
= skip_to_end_of_comment (ip
, &junk
, 1);
6348 ip
->bufp
= save_bufp
;
6355 /* If we get here, this #endif ends a #ifndef
6356 that contains all of the file (aside from whitespace).
6357 Arrange not to include the file again
6358 if the macro that was tested is defined. */
6360 record_control_macro (ip
->fname
, temp
->control_macro
);
6364 output_line_command (&instack
[indepth
], op
, 1, same_file
);
6369 /* When an #else or #endif is found while skipping failed conditional,
6370 if -pedantic was specified, this is called to warn about text after
6371 the command name. P points to the first char after the command name. */
6377 /* Advance P over whitespace and comments. */
6379 if (*p
== '\\' && p
[1] == '\n')
6381 if (is_hor_space
[*p
])
6383 else if (*p
== '/') {
6384 if (p
[1] == '\\' && p
[2] == '\n')
6385 newline_fix (p
+ 1);
6388 /* Don't bother warning about unterminated comments
6389 since that will happen later. Just be sure to exit. */
6391 if (p
[1] == '\\' && p
[2] == '\n')
6392 newline_fix (p
+ 1);
6393 if (*p
== '*' && p
[1] == '/') {
6400 else if ((cplusplus
|| objc
) && p
[1] == '/') {
6402 while (*p
&& *p
++ != '\n') ;
6406 if (*p
&& *p
!= '\n')
6407 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
6410 /* Skip a comment, assuming the input ptr immediately follows the
6411 initial slash-star. Bump *LINE_COUNTER for each newline.
6412 (The canonical line counter is &ip->lineno.)
6413 Don't use this routine (or the next one) if bumping the line
6414 counter is not sufficient to deal with newlines in the string.
6416 If NOWARN is nonzero, don't warn about slash-star inside a comment.
6417 This feature is useful when processing a comment that is going to be
6418 processed or was processed at another point in the preprocessor,
6419 to avoid a duplicate warning. */
6421 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
6422 register FILE_BUF
*ip
;
6423 int *line_counter
; /* place to remember newlines, or NULL */
6426 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
6427 register U_CHAR
*bp
= ip
->bufp
;
6428 FILE_BUF
*op
= &outbuf
; /* JF */
6429 int output
= put_out_comments
&& !line_counter
;
6431 /* JF this line_counter stuff is a crock to make sure the
6432 comment is only put out once, no matter how many times
6433 the comment is skipped. It almost works */
6438 if ((cplusplus
|| objc
) && bp
[-1] == '/') {
6441 if ((*op
->bufp
++ = *bp
++) == '\n') {
6449 while (bp
< limit
) {
6450 if (*bp
++ == '\n') {
6459 while (bp
< limit
) {
6464 if (warn_comments
&& !nowarn
&& bp
< limit
&& *bp
== '*')
6465 warning ("`/*' within comment");
6468 if (line_counter
!= NULL
)
6474 if (*bp
== '\\' && bp
[1] == '\n')
6490 * Skip over a quoted string. BP points to the opening quote.
6491 * Returns a pointer after the closing quote. Don't go past LIMIT.
6492 * START_LINE is the line number of the starting point (but it need
6493 * not be valid if the starting point is inside a macro expansion).
6495 * The input stack state is not changed.
6497 * If COUNT_NEWLINES is nonzero, it points to an int to increment
6498 * for each newline passed.
6500 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
6501 * if we pass a backslash-newline.
6503 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
6506 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
6507 register U_CHAR
*bp
;
6508 register U_CHAR
*limit
;
6510 int *count_newlines
;
6511 int *backslash_newlines_p
;
6514 register U_CHAR c
, match
;
6519 error_with_line (line_for_error (start_line
),
6520 "unterminated string or character constant");
6527 while (*bp
== '\\' && bp
[1] == '\n') {
6528 if (backslash_newlines_p
)
6529 *backslash_newlines_p
= 1;
6534 if (*bp
== '\n' && count_newlines
) {
6535 if (backslash_newlines_p
)
6536 *backslash_newlines_p
= 1;
6540 } else if (c
== '\n') {
6542 /* Unterminated strings and character constants are 'legal'. */
6543 bp
--; /* Don't consume the newline. */
6548 if (match
== '\'') {
6549 error_with_line (line_for_error (start_line
),
6550 "unterminated character constant");
6556 if (traditional
) { /* Unterminated strings are 'legal'. */
6561 /* If not traditional, then allow newlines inside strings. */
6564 } else if (c
== match
)
6570 /* Skip across a group of balanced parens, starting from IP->bufp.
6571 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
6573 This does not handle newlines, because it's used for the arg of #if,
6574 where there aren't any newlines. Also, backslash-newline can't appear. */
6577 skip_paren_group (ip
)
6578 register FILE_BUF
*ip
;
6580 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
6581 U_CHAR
*p
= ip
->bufp
;
6583 int lines_dummy
= 0;
6585 while (p
!= limit
) {
6595 return ip
->bufp
= p
;
6601 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
6609 p
= skip_quoted_string (p
- 1, limit
, 0, 0, 0, &eofp
);
6611 return ip
->bufp
= p
;
6622 * write out a #line command, for instance, after an #include file.
6623 * If CONDITIONAL is nonzero, we can omit the #line if it would
6624 * appear to be a no-op, and we can output a few newlines instead
6625 * if we want to increase the line number by a small amount.
6626 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
6630 output_line_command (ip
, op
, conditional
, file_change
)
6633 enum file_change_code file_change
;
6636 char line_cmd_buf
[500];
6638 if (no_line_commands
6639 || ip
->fname
== NULL
6641 op
->lineno
= ip
->lineno
;
6646 if (ip
->lineno
== op
->lineno
)
6649 /* If the inherited line number is a little too small,
6650 output some newlines instead of a #line command. */
6651 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
6652 check_expand (op
, 10);
6653 while (ip
->lineno
> op
->lineno
) {
6661 /* Don't output a line number of 0 if we can help it. */
6662 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
6663 && *ip
->bufp
== '\n') {
6668 #ifdef OUTPUT_LINE_COMMANDS
6669 sprintf (line_cmd_buf
, "#line %d \"%s\"", ip
->lineno
, ip
->nominal_fname
);
6671 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->nominal_fname
);
6673 if (file_change
!= same_file
)
6674 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
6675 /* Tell cc1 if following text comes from a system header file. */
6676 if (ip
->system_header_p
)
6677 strcat (line_cmd_buf
, " 3");
6678 len
= strlen (line_cmd_buf
);
6679 line_cmd_buf
[len
++] = '\n';
6680 check_expand (op
, len
+ 1);
6681 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
6683 bcopy (line_cmd_buf
, op
->bufp
, len
);
6685 op
->lineno
= ip
->lineno
;
6688 /* This structure represents one parsed argument in a macro call.
6689 `raw' points to the argument text as written (`raw_length' is its length).
6690 `expanded' points to the argument's macro-expansion
6691 (its length is `expand_length').
6692 `stringified_length' is the length the argument would have
6694 `use_count' is the number of times this macro arg is substituted
6695 into the macro. If the actual use count exceeds 10,
6696 the value stored is 10.
6697 `free1' and `free2', if nonzero, point to blocks to be freed
6698 when the macro argument data is no longer needed. */
6701 U_CHAR
*raw
, *expanded
;
6702 int raw_length
, expand_length
;
6703 int stringified_length
;
6704 U_CHAR
*free1
, *free2
;
6710 /* Expand a macro call.
6711 HP points to the symbol that is the macro being called.
6712 Put the result of expansion onto the input stack
6713 so that subsequent input by our caller will use it.
6715 If macro wants arguments, caller has already verified that
6716 an argument list follows; arguments come from the input stack. */
6719 macroexpand (hp
, op
)
6724 DEFINITION
*defn
= hp
->value
.defn
;
6725 register U_CHAR
*xbuf
;
6727 int start_line
= instack
[indepth
].lineno
;
6729 CHECK_DEPTH (return;);
6731 /* it might not actually be a macro. */
6732 if (hp
->type
!= T_MACRO
) {
6733 special_symbol (hp
, op
);
6737 /* This macro is being used inside a #if, which means it must be */
6738 /* recorded as a precondition. */
6739 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
6740 dump_single_macro (hp
, pcp_outfile
);
6742 nargs
= defn
->nargs
;
6746 struct argdata
*args
;
6747 char *parse_error
= 0;
6749 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
6751 for (i
= 0; i
< nargs
; i
++) {
6752 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
6753 args
[i
].raw_length
= args
[i
].expand_length
6754 = args
[i
].stringified_length
= 0;
6755 args
[i
].free1
= args
[i
].free2
= 0;
6756 args
[i
].use_count
= 0;
6759 /* Parse all the macro args that are supplied. I counts them.
6760 The first NARGS args are stored in ARGS.
6761 The rest are discarded. */
6764 /* Discard the open-parenthesis or comma before the next arg. */
6765 ++instack
[indepth
].bufp
;
6767 = macarg ((i
< nargs
|| (nargs
== 0 && i
== 0)) ? &args
[i
] : 0);
6769 error_with_line (line_for_error (start_line
), parse_error
);
6773 } while (*instack
[indepth
].bufp
!= ')');
6775 /* If we got one arg but it was just whitespace, call that 0 args. */
6777 register U_CHAR
*bp
= args
[0].raw
;
6778 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
6779 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
6784 if (nargs
== 0 && i
> 0)
6785 error ("arguments given to macro `%s'", hp
->name
);
6786 else if (i
< nargs
) {
6787 /* traditional C allows foo() if foo wants one argument. */
6788 if (nargs
== 1 && i
== 0 && traditional
)
6791 error ("macro `%s' used without args", hp
->name
);
6793 error ("macro `%s' used with just one arg", hp
->name
);
6795 error ("macro `%s' used with only %d args", hp
->name
, i
);
6796 } else if (i
> nargs
)
6797 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
6799 /* Swallow the closeparen. */
6800 ++instack
[indepth
].bufp
;
6802 /* If macro wants zero args, we parsed the arglist for checking only.
6803 Read directly from the macro definition. */
6805 xbuf
= defn
->expansion
;
6806 xbuf_len
= defn
->length
;
6808 register U_CHAR
*exp
= defn
->expansion
;
6809 register int offset
; /* offset in expansion,
6810 copied a piece at a time */
6811 register int totlen
; /* total amount of exp buffer filled so far */
6813 register struct reflist
*ap
;
6815 /* Macro really takes args. Compute the expansion of this call. */
6817 /* Compute length in characters of the macro's expansion.
6818 Also count number of times each arg is used. */
6819 xbuf_len
= defn
->length
;
6820 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
6822 xbuf_len
+= args
[ap
->argno
].stringified_length
;
6823 else if (ap
->raw_before
|| ap
->raw_after
|| traditional
)
6824 xbuf_len
+= args
[ap
->argno
].raw_length
;
6826 xbuf_len
+= args
[ap
->argno
].expand_length
;
6828 if (args
[ap
->argno
].use_count
< 10)
6829 args
[ap
->argno
].use_count
++;
6832 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
6834 /* Generate in XBUF the complete expansion
6835 with arguments substituted in.
6836 TOTLEN is the total size generated so far.
6837 OFFSET is the index in the definition
6838 of where we are copying from. */
6839 offset
= totlen
= 0;
6840 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
6841 register struct argdata
*arg
= &args
[ap
->argno
];
6843 for (i
= 0; i
< ap
->nchars
; i
++)
6844 xbuf
[totlen
++] = exp
[offset
++];
6846 if (ap
->stringify
!= 0) {
6847 int arglen
= arg
->raw_length
;
6853 && (c
= arg
->raw
[i
], is_space
[c
]))
6856 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
6859 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
6860 for (; i
< arglen
; i
++) {
6863 /* Special markers Newline Space
6864 generate nothing for a stringified argument. */
6865 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
6870 /* Internal sequences of whitespace are replaced by one space
6871 except within an string or char token. */
6873 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
])) {
6875 /* Note that Newline Space does occur within whitespace
6876 sequences; consider it part of the sequence. */
6877 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
6879 else if (c
!= '\n' && is_space
[c
])
6896 } else if (c
== '\"' || c
== '\'')
6900 /* Escape these chars */
6901 if (c
== '\"' || (in_string
&& c
== '\\'))
6902 xbuf
[totlen
++] = '\\';
6906 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
6911 xbuf
[totlen
++] = '\"'; /* insert ending quote */
6912 } else if (ap
->raw_before
|| ap
->raw_after
|| traditional
) {
6913 U_CHAR
*p1
= arg
->raw
;
6914 U_CHAR
*l1
= p1
+ arg
->raw_length
;
6915 if (ap
->raw_before
) {
6916 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
6917 while (p1
!= l1
&& is_idchar
[*p1
])
6918 xbuf
[totlen
++] = *p1
++;
6919 /* Delete any no-reexpansion marker that follows
6920 an identifier at the beginning of the argument
6921 if the argument is concatenated with what precedes it. */
6922 if (p1
[0] == '\n' && p1
[1] == '-')
6925 if (ap
->raw_after
) {
6926 /* Arg is concatenated after: delete trailing whitespace,
6927 whitespace markers, and no-reexpansion markers. */
6929 if (is_space
[l1
[-1]]) l1
--;
6930 else if (l1
[-1] == '-') {
6931 U_CHAR
*p2
= l1
- 1;
6932 /* If a `-' is preceded by an odd number of newlines then it
6933 and the last newline are a no-reexpansion marker. */
6934 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
6935 if ((l1
- 1 - p2
) & 1) {
6943 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
6946 bcopy (arg
->expanded
, xbuf
+ totlen
, arg
->expand_length
);
6947 totlen
+= arg
->expand_length
;
6948 /* If a macro argument with newlines is used multiple times,
6949 then only expand the newlines once. This avoids creating output
6950 lines which don't correspond to any input line, which confuses
6952 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
6953 /* Don't bother doing delete_newlines for subsequent
6957 = delete_newlines (arg
->expanded
, arg
->expand_length
);
6961 if (totlen
> xbuf_len
)
6965 /* if there is anything left of the definition
6966 after handling the arg list, copy that in too. */
6968 for (i
= offset
; i
< defn
->length
; i
++)
6969 xbuf
[totlen
++] = exp
[i
];
6974 for (i
= 0; i
< nargs
; i
++) {
6975 if (args
[i
].free1
!= 0)
6976 free (args
[i
].free1
);
6977 if (args
[i
].free2
!= 0)
6978 free (args
[i
].free2
);
6982 xbuf
= defn
->expansion
;
6983 xbuf_len
= defn
->length
;
6986 /* Now put the expansion on the input stack
6987 so our caller will commence reading from it. */
6989 register FILE_BUF
*ip2
;
6991 ip2
= &instack
[++indepth
];
6994 ip2
->nominal_fname
= 0;
6997 ip2
->length
= xbuf_len
;
6999 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
7001 ip2
->if_stack
= if_stack
;
7002 ip2
->system_header_p
= 0;
7004 /* Recursive macro use sometimes works traditionally.
7005 #define foo(x,y) bar(x(y,0), y)
7009 hp
->type
= T_DISABLED
;
7014 * Parse a macro argument and store the info on it into *ARGPTR.
7015 * Return nonzero to indicate a syntax error.
7020 register struct argdata
*argptr
;
7022 FILE_BUF
*ip
= &instack
[indepth
];
7027 /* Try to parse as much of the argument as exists at this
7028 input stack level. */
7029 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
7030 &paren
, &newlines
, &comments
);
7032 /* If we find the end of the argument at this level,
7033 set up *ARGPTR to point at it in the input stack. */
7034 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
7035 && bp
!= ip
->buf
+ ip
->length
) {
7037 argptr
->raw
= ip
->bufp
;
7038 argptr
->raw_length
= bp
- ip
->bufp
;
7039 argptr
->newlines
= newlines
;
7043 /* This input stack level ends before the macro argument does.
7044 We must pop levels and keep parsing.
7045 Therefore, we must allocate a temporary buffer and copy
7046 the macro argument into it. */
7047 int bufsize
= bp
- ip
->bufp
;
7048 int extra
= newlines
;
7049 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
7050 int final_start
= 0;
7052 bcopy (ip
->bufp
, buffer
, bufsize
);
7054 ip
->lineno
+= newlines
;
7056 while (bp
== ip
->buf
+ ip
->length
) {
7057 if (instack
[indepth
].macro
== 0) {
7059 return "unterminated macro call";
7061 ip
->macro
->type
= T_MACRO
;
7063 free (ip
->free_ptr
);
7064 ip
= &instack
[--indepth
];
7067 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
7068 &newlines
, &comments
);
7069 final_start
= bufsize
;
7070 bufsize
+= bp
- ip
->bufp
;
7072 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
7073 bcopy (ip
->bufp
, buffer
+ bufsize
- (bp
- ip
->bufp
), bp
- ip
->bufp
);
7075 ip
->lineno
+= newlines
;
7078 /* Now, if arg is actually wanted, record its raw form,
7079 discarding comments and duplicating newlines in whatever
7080 part of it did not come from a macro expansion.
7081 EXTRA space has been preallocated for duplicating the newlines.
7082 FINAL_START is the index of the start of that part. */
7084 argptr
->raw
= buffer
;
7085 argptr
->raw_length
= bufsize
;
7086 argptr
->free1
= buffer
;
7087 argptr
->newlines
= newlines
;
7088 argptr
->comments
= comments
;
7089 if ((newlines
|| comments
) && ip
->fname
!= 0)
7092 discard_comments (argptr
->raw
+ final_start
,
7093 argptr
->raw_length
- final_start
,
7095 argptr
->raw
[argptr
->raw_length
] = 0;
7096 if (argptr
->raw_length
> bufsize
+ extra
)
7101 /* If we are not discarding this argument,
7102 macroexpand it and compute its length as stringified.
7103 All this info goes into *ARGPTR. */
7107 register U_CHAR
*buf
, *lim
;
7108 register int totlen
;
7110 obuf
= expand_to_temp_buffer (argptr
->raw
,
7111 argptr
->raw
+ argptr
->raw_length
,
7114 argptr
->expanded
= obuf
.buf
;
7115 argptr
->expand_length
= obuf
.length
;
7116 argptr
->free2
= obuf
.buf
;
7119 lim
= buf
+ argptr
->raw_length
;
7121 while (buf
!= lim
&& is_space
[*buf
])
7123 while (buf
!= lim
&& is_space
[lim
[-1]])
7125 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
7126 while (buf
!= lim
) {
7127 register U_CHAR c
= *buf
++;
7129 /* Internal sequences of whitespace are replaced by one space
7130 in most cases, but not always. So count all the whitespace
7131 in case we need to keep it all. */
7134 SKIP_ALL_WHITE_SPACE (buf
);
7137 if (c
== '\"' || c
== '\\') /* escape these chars */
7139 else if (!isprint (c
))
7142 argptr
->stringified_length
= totlen
;
7147 /* Scan text from START (inclusive) up to LIMIT (exclusive),
7148 counting parens in *DEPTHPTR,
7149 and return if reach LIMIT
7150 or before a `)' that would make *DEPTHPTR negative
7151 or before a comma when *DEPTHPTR is zero.
7152 Single and double quotes are matched and termination
7153 is inhibited within them. Comments also inhibit it.
7154 Value returned is pointer to stopping place.
7156 Increment *NEWLINES each time a newline is passed.
7157 Set *COMMENTS to 1 if a comment is seen. */
7160 macarg1 (start
, limit
, depthptr
, newlines
, comments
)
7162 register U_CHAR
*limit
;
7163 int *depthptr
, *newlines
, *comments
;
7165 register U_CHAR
*bp
= start
;
7167 while (bp
< limit
) {
7173 if (--(*depthptr
) < 0)
7177 /* Traditionally, backslash makes following char not special. */
7178 if (bp
+ 1 < limit
&& traditional
)
7181 /* But count source lines anyway. */
7190 if (bp
[1] == '\\' && bp
[2] == '\n')
7191 newline_fix (bp
+ 1);
7192 if ((cplusplus
|| objc
) && bp
[1] == '/') {
7195 while (bp
< limit
&& *bp
++ != '\n') ;
7199 if (bp
[1] != '*' || bp
+ 1 >= limit
)
7203 while (bp
+ 1 < limit
) {
7205 && bp
[1] == '\\' && bp
[2] == '\n')
7206 newline_fix (bp
+ 1);
7207 if (bp
[0] == '*' && bp
[1] == '/')
7209 if (*bp
== '\n') ++*newlines
;
7217 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
7222 while (*bp
== '\\' && bp
[1] == '\n') {
7225 } else if (*bp
== '\n') {
7234 if ((*depthptr
) == 0)
7244 /* Discard comments and duplicate newlines
7245 in the string of length LENGTH at START,
7246 except inside of string constants.
7247 The string is copied into itself with its beginning staying fixed.
7249 NEWLINES is the number of newlines that must be duplicated.
7250 We assume that that much extra space is available past the end
7254 discard_comments (start
, length
, newlines
)
7259 register U_CHAR
*ibp
;
7260 register U_CHAR
*obp
;
7261 register U_CHAR
*limit
;
7264 /* If we have newlines to duplicate, copy everything
7265 that many characters up. Then, in the second part,
7266 we will have room to insert the newlines
7268 NEWLINES may actually be too large, because it counts
7269 newlines in string constants, and we don't duplicate those.
7270 But that does no harm. */
7272 ibp
= start
+ length
;
7273 obp
= ibp
+ newlines
;
7275 while (limit
!= ibp
)
7279 ibp
= start
+ newlines
;
7280 limit
= start
+ length
+ newlines
;
7283 while (ibp
< limit
) {
7284 *obp
++ = c
= *ibp
++;
7287 /* Duplicate the newline. */
7299 if (*ibp
== '\\' && ibp
[1] == '\n')
7301 /* Delete any comment. */
7302 if ((cplusplus
|| objc
) && ibp
[0] == '/') {
7305 while (ibp
< limit
&& *ibp
++ != '\n') ;
7308 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
7312 while (ibp
+ 1 < limit
) {
7314 && ibp
[1] == '\\' && ibp
[2] == '\n')
7315 newline_fix (ibp
+ 1);
7316 if (ibp
[0] == '*' && ibp
[1] == '/')
7325 /* Notice and skip strings, so that we don't
7326 think that comments start inside them,
7327 and so we don't duplicate newlines in them. */
7330 while (ibp
< limit
) {
7331 *obp
++ = c
= *ibp
++;
7334 if (c
== '\n' && quotec
== '\'')
7336 if (c
== '\\' && ibp
< limit
) {
7337 while (*ibp
== '\\' && ibp
[1] == '\n')
7350 /* Delete newlines in the string of length LENGTH at START, except inside
7351 of string constants. The string is copied into itself with its beginning
7355 delete_newlines (start
, length
)
7359 register U_CHAR
*ibp
;
7360 register U_CHAR
*obp
;
7361 register U_CHAR
*limit
;
7365 limit
= start
+ length
;
7368 while (ibp
< limit
) {
7369 *obp
++ = c
= *ibp
++;
7372 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
7373 output. Skip past the newline and its duplicate. */
7383 /* Notice and skip strings, so that we don't delete newlines in them. */
7386 while (ibp
< limit
) {
7387 *obp
++ = c
= *ibp
++;
7390 if (c
== '\n' && quotec
== '\'')
7402 * error - print error message and increment count of errors.
7406 error (msg
, arg1
, arg2
, arg3
)
7410 FILE_BUF
*ip
= NULL
;
7412 print_containing_files ();
7414 for (i
= indepth
; i
>= 0; i
--)
7415 if (instack
[i
].fname
!= NULL
) {
7421 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7422 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7423 fprintf (stderr
, "\n");
7427 /* Error including a message from `errno'. */
7430 error_from_errno (name
)
7434 FILE_BUF
*ip
= NULL
;
7436 print_containing_files ();
7438 for (i
= indepth
; i
>= 0; i
--)
7439 if (instack
[i
].fname
!= NULL
) {
7445 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7447 if (errno
< sys_nerr
)
7448 fprintf (stderr
, "%s: %s\n", name
, sys_errlist
[errno
]);
7450 fprintf (stderr
, "%s: undocumented I/O error\n", name
);
7455 /* Print error message but don't count it. */
7458 warning (msg
, arg1
, arg2
, arg3
)
7462 FILE_BUF
*ip
= NULL
;
7464 if (inhibit_warnings
)
7467 if (warnings_are_errors
)
7470 print_containing_files ();
7472 for (i
= indepth
; i
>= 0; i
--)
7473 if (instack
[i
].fname
!= NULL
) {
7479 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7480 fprintf (stderr
, "warning: ");
7481 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7482 fprintf (stderr
, "\n");
7486 error_with_line (line
, msg
, arg1
, arg2
, arg3
)
7491 FILE_BUF
*ip
= NULL
;
7493 print_containing_files ();
7495 for (i
= indepth
; i
>= 0; i
--)
7496 if (instack
[i
].fname
!= NULL
) {
7502 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, line
);
7503 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7504 fprintf (stderr
, "\n");
7508 /* print an error message and maybe count it. */
7511 pedwarn (msg
, arg1
, arg2
, arg3
)
7514 if (pedantic_errors
)
7515 error (msg
, arg1
, arg2
, arg3
);
7517 warning (msg
, arg1
, arg2
, arg3
);
7520 /* Report a warning (or an error if pedantic_errors)
7521 giving specified file name and line number, not current. */
7524 pedwarn_with_file_and_line (file
, line
, msg
, arg1
, arg2
, arg3
)
7530 if (!pedantic_errors
&& inhibit_warnings
)
7533 fprintf (stderr
, "%s:%d: ", file
, line
);
7534 if (pedantic_errors
|| warnings_are_errors
)
7536 if (!pedantic_errors
)
7537 fprintf (stderr
, "warning: ");
7538 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7539 fprintf (stderr
, "\n");
7542 /* Print the file names and line numbers of the #include
7543 commands which led to the current file. */
7546 print_containing_files ()
7548 FILE_BUF
*ip
= NULL
;
7552 /* If stack of files hasn't changed since we last printed
7553 this info, don't repeat it. */
7554 if (last_error_tick
== input_file_stack_tick
)
7557 for (i
= indepth
; i
>= 0; i
--)
7558 if (instack
[i
].fname
!= NULL
) {
7563 /* Give up if we don't find a source file. */
7567 /* Find the other, outer source files. */
7568 for (i
--; i
>= 0; i
--)
7569 if (instack
[i
].fname
!= NULL
) {
7573 fprintf (stderr
, "In file included");
7575 fprintf (stderr
, ",");
7578 fprintf (stderr
, " from %s:%d", ip
->nominal_fname
, ip
->lineno
);
7581 fprintf (stderr
, ":\n");
7583 /* Record we have printed the status as of this time. */
7584 last_error_tick
= input_file_stack_tick
;
7587 /* Return the line at which an error occurred.
7588 The error is not necessarily associated with the current spot
7589 in the input stack, so LINE says where. LINE will have been
7590 copied from ip->lineno for the current input level.
7591 If the current level is for a file, we return LINE.
7592 But if the current level is not for a file, LINE is meaningless.
7593 In that case, we return the lineno of the innermost file. */
7596 line_for_error (line
)
7602 for (i
= indepth
; i
>= 0; ) {
7603 if (instack
[i
].fname
!= 0)
7608 line1
= instack
[i
].lineno
;
7616 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
7618 * As things stand, nothing is ever placed in the output buffer to be
7619 * removed again except when it's KNOWN to be part of an identifier,
7620 * so flushing and moving down everything left, instead of expanding,
7624 /* You might think void was cleaner for the return type,
7625 but that would get type mismatch in check_expand in strict ANSI. */
7627 grow_outbuf (obuf
, needed
)
7628 register FILE_BUF
*obuf
;
7629 register int needed
;
7634 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
7637 /* Make it at least twice as big as it is now. */
7639 /* Make it have at least 150% of the free space we will need. */
7640 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
7641 if (minsize
> obuf
->length
)
7642 obuf
->length
= minsize
;
7644 if ((p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
)) == NULL
)
7647 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
7653 /* Symbol table for macro names and special symbols */
7656 * install a name in the main hash table, even if it is already there.
7657 * name stops with first non alphanumeric, except leading '#'.
7658 * caller must check against redefinition if that is desired.
7659 * delete_macro () removes things installed by install () in fifo order.
7660 * this is important because of the `defined' special symbol used
7661 * in #if, and also if pushdef/popdef directives are ever implemented.
7663 * If LEN is >= 0, it is the length of the name.
7664 * Otherwise, compute the length by scanning the entire name.
7666 * If HASH is >= 0, it is the precomputed hash code.
7667 * Otherwise, compute the hash code.
7670 install (name
, len
, type
, value
, hash
)
7673 enum node_type type
;
7676 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
7678 register HASHNODE
*hp
;
7679 register int i
, bucket
;
7680 register U_CHAR
*p
, *q
;
7684 while (is_idchar
[*p
])
7690 hash
= hashf (name
, len
, HASHSIZE
);
7692 i
= sizeof (HASHNODE
) + len
+ 1;
7693 hp
= (HASHNODE
*) xmalloc (i
);
7695 hp
->bucket_hdr
= &hashtab
[bucket
];
7696 hp
->next
= hashtab
[bucket
];
7697 hashtab
[bucket
] = hp
;
7699 if (hp
->next
!= NULL
)
7700 hp
->next
->prev
= hp
;
7703 hp
->value
.ival
= value
;
7704 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
7707 for (i
= 0; i
< len
; i
++)
7714 * find the most recent hash node for name name (ending with first
7715 * non-identifier char) installed by install
7717 * If LEN is >= 0, it is the length of the name.
7718 * Otherwise, compute the length by scanning the entire name.
7720 * If HASH is >= 0, it is the precomputed hash code.
7721 * Otherwise, compute the hash code.
7724 lookup (name
, len
, hash
)
7729 register U_CHAR
*bp
;
7730 register HASHNODE
*bucket
;
7733 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
7738 hash
= hashf (name
, len
, HASHSIZE
);
7740 bucket
= hashtab
[hash
];
7742 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
7744 bucket
= bucket
->next
;
7750 * Delete a hash node. Some weirdness to free junk from macros.
7751 * More such weirdness will have to be added if you define more hash
7752 * types that need it.
7755 /* Note that the DEFINITION of a macro is removed from the hash table
7756 but its storage is not freed. This would be a storage leak
7757 except that it is not reasonable to keep undefining and redefining
7758 large numbers of macros many times.
7759 In any case, this is necessary, because a macro can be #undef'd
7760 in the middle of reading the arguments to a call to it.
7761 If #undef freed the DEFINITION, that would crash. */
7768 if (hp
->prev
!= NULL
)
7769 hp
->prev
->next
= hp
->next
;
7770 if (hp
->next
!= NULL
)
7771 hp
->next
->prev
= hp
->prev
;
7773 /* make sure that the bucket chain header that
7774 the deleted guy was on points to the right thing afterwards. */
7775 if (hp
== *hp
->bucket_hdr
)
7776 *hp
->bucket_hdr
= hp
->next
;
7779 if (hp
->type
== T_MACRO
) {
7780 DEFINITION
*d
= hp
->value
.defn
;
7781 struct reflist
*ap
, *nextap
;
7783 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
7794 * return hash function on name. must be compatible with the one
7795 * computed a step at a time, elsewhere
7798 hashf (name
, len
, hashsize
)
7799 register U_CHAR
*name
;
7806 r
= HASHSTEP (r
, *name
++);
7808 return MAKE_POS (r
) % hashsize
;
7812 /* Dump the definition of a single macro HP to OF. */
7814 dump_single_macro (hp
, of
)
7815 register HASHNODE
*hp
;
7818 register DEFINITION
*defn
= hp
->value
.defn
;
7824 /* Print the definition of the macro HP. */
7826 fprintf (of
, "#define %s", hp
->name
);
7828 if (defn
->nargs
>= 0) {
7832 for (i
= 0; i
< defn
->nargs
; i
++) {
7833 dump_arg_n (defn
, i
, of
);
7834 if (i
+ 1 < defn
->nargs
)
7844 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
7845 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
7846 if (ap
->nchars
!= 0)
7848 offset
+= ap
->nchars
;
7851 if (ap
->raw_before
&& !concat
)
7852 fprintf (of
, " ## ");
7854 dump_arg_n (defn
, ap
->argno
, of
);
7855 if (ap
->raw_after
) {
7856 fprintf (of
, " ## ");
7860 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
7864 /* Dump all macro definitions as #defines to stdout. */
7871 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
7872 register HASHNODE
*hp
;
7874 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
7875 if (hp
->type
== T_MACRO
)
7876 dump_single_macro (hp
, stdout
);
7881 /* Output to OF a substring of a macro definition.
7882 BASE is the beginning of the definition.
7883 Output characters START thru LENGTH.
7884 Discard newlines outside of strings, thus
7885 converting funny-space markers to ordinary spaces. */
7888 dump_defn_1 (base
, start
, length
, of
)
7894 U_CHAR
*p
= base
+ start
;
7895 U_CHAR
*limit
= base
+ start
+ length
;
7900 else if (*p
== '\"' || *p
=='\'') {
7901 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, 0, 0, 0);
7902 fwrite (p
, p1
- p
, 1, of
);
7909 /* Print the name of argument number ARGNUM of macro definition DEFN
7911 Recall that DEFN->args.argnames contains all the arg names
7912 concatenated in reverse order with comma-space in between. */
7915 dump_arg_n (defn
, argnum
, of
)
7920 register U_CHAR
*p
= defn
->args
.argnames
;
7921 while (argnum
+ 1 < defn
->nargs
) {
7922 p
= (U_CHAR
*) index (p
, ' ') + 1;
7926 while (*p
&& *p
!= ',') {
7932 /* Initialize syntactic classifications of characters. */
7935 initialize_char_syntax ()
7940 * Set up is_idchar and is_idstart tables. These should be
7941 * faster than saying (is_alpha (c) || c == '_'), etc.
7942 * Set up these things before calling any routines tthat
7945 for (i
= 'a'; i
<= 'z'; i
++) {
7946 is_idchar
[i
- 'a' + 'A'] = 1;
7948 is_idstart
[i
- 'a' + 'A'] = 1;
7951 for (i
= '0'; i
<= '9'; i
++)
7954 is_idstart
['_'] = 1;
7955 is_idchar
['$'] = dollars_in_ident
;
7956 is_idstart
['$'] = dollars_in_ident
;
7958 /* horizontal space table */
7959 is_hor_space
[' '] = 1;
7960 is_hor_space
['\t'] = 1;
7961 is_hor_space
['\v'] = 1;
7962 is_hor_space
['\f'] = 1;
7963 is_hor_space
['\r'] = 1;
7973 /* Initialize the built-in macros. */
7976 initialize_builtins (inp
, outp
)
7983 timebuf
= localtime (&t
);
7985 install ("__LINE__", -1, T_SPECLINE
, 0, -1);
7986 install ("__DATE__", -1, T_DATE
, 0, -1);
7987 install ("__FILE__", -1, T_FILE
, 0, -1);
7988 install ("__BASE_FILE__", -1, T_BASE_FILE
, 0, -1);
7989 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, -1);
7990 install ("__VERSION__", -1, T_VERSION
, 0, -1);
7991 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, -1);
7992 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, -1);
7993 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, -1);
7994 install ("__TIME__", -1, T_TIME
, 0, -1);
7996 install ("__STDC__", -1, T_CONST
, STDC_VALUE
, -1);
7998 install ("__OBJC__", -1, T_CONST
, 1, -1);
7999 /* This is supplied using a -D by the compiler driver
8000 so that it is present only when truly compiling with GNU C. */
8001 /* install ("__GNUC__", -1, T_CONST, 2, -1); */
8005 char directive
[2048];
8006 register struct directive
*dp
= &directive_table
[0];
8008 sprintf (directive
, " __BASE_FILE__ \"%s\"",
8009 instack
[0].nominal_fname
);
8010 output_line_command (inp
, outp
, 0, same_file
);
8011 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8013 sprintf (directive
, " __VERSION__ \"%s\"", version_string
);
8014 output_line_command (inp
, outp
, 0, same_file
);
8015 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8017 sprintf (directive
, " __SIZE_TYPE__ %s", SIZE_TYPE
);
8018 output_line_command (inp
, outp
, 0, same_file
);
8019 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8021 sprintf (directive
, " __PTRDIFF_TYPE__ %s", PTRDIFF_TYPE
);
8022 output_line_command (inp
, outp
, 0, same_file
);
8023 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8025 sprintf (directive
, " __WCHAR_TYPE__ %s", WCHAR_TYPE
);
8026 output_line_command (inp
, outp
, 0, same_file
);
8027 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8029 sprintf (directive
, " __WCHAR_TYPE__ %s", WCHAR_TYPE
);
8030 output_line_command (inp
, outp
, 0, same_file
);
8031 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8033 sprintf (directive
, " __DATE__ \"%s %2d %4d\"",
8034 monthnames
[timebuf
->tm_mon
],
8035 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
8036 output_line_command (inp
, outp
, 0, same_file
);
8037 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8039 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"",
8040 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
8041 output_line_command (inp
, outp
, 0, same_file
);
8042 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8046 sprintf (directive
, " __STDC__ 1");
8047 output_line_command (inp
, outp
, 0, same_file
);
8048 pass_thru_directive (directive
, &directive
[strlen (directive
)],
8053 sprintf (directive
, " __OBJC__ 1");
8054 output_line_command (inp
, outp
, 0, same_file
);
8055 pass_thru_directive (directive
, &directive
[strlen (directive
)],
8062 * process a given definition string, for initialization
8063 * If STR is just an identifier, define it with value 1.
8064 * If STR has anything after the identifier, then it should
8065 * be identifier=definition.
8069 make_definition (str
, op
)
8074 struct directive
*kt
;
8079 if (!is_idstart
[*p
]) {
8080 error ("malformed option `-D %s'", str
);
8083 while (is_idchar
[*++p
])
8086 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
8087 strcpy ((char *)buf
, str
);
8088 strcat ((char *)buf
, " 1");
8089 } else if (*p
!= '=') {
8090 error ("malformed option `-D %s'", str
);
8094 /* Copy the entire option so we can modify it. */
8095 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
8096 strncpy (buf
, str
, p
- str
);
8097 /* Change the = to a space. */
8099 /* Scan for any backslash-newline and remove it. */
8103 if (*p
== '\\' && p
[1] == '\n')
8105 /* Change newline chars into newline-markers. */
8106 else if (*p
== '\n')
8118 ip
= &instack
[++indepth
];
8119 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
8121 ip
->buf
= ip
->bufp
= buf
;
8122 ip
->length
= strlen (buf
);
8126 ip
->if_stack
= if_stack
;
8127 ip
->system_header_p
= 0;
8129 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
8132 do_define (buf
, buf
+ strlen (buf
) , op
, kt
);
8136 /* JF, this does the work for the -U option */
8139 make_undef (str
, op
)
8144 struct directive
*kt
;
8146 ip
= &instack
[++indepth
];
8147 ip
->nominal_fname
= ip
->fname
= "*undef*";
8149 ip
->buf
= ip
->bufp
= str
;
8150 ip
->length
= strlen (str
);
8154 ip
->if_stack
= if_stack
;
8155 ip
->system_header_p
= 0;
8157 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
8160 do_undef (str
, str
+ strlen (str
), op
, kt
);
8164 /* Process the string STR as if it appeared as the body of a #assert.
8165 OPTION is the option name for which STR was the argument. */
8168 make_assertion (option
, str
)
8173 struct directive
*kt
;
8174 U_CHAR
*buf
, *p
, *q
;
8176 /* Copy the entire option so we can modify it. */
8177 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
8178 strcpy ((char *) buf
, str
);
8179 /* Scan for any backslash-newline and remove it. */
8182 if (*p
== '\\' && p
[1] == '\n')
8190 if (!is_idstart
[*p
]) {
8191 error ("malformed option `%s %s'", option
, str
);
8194 while (is_idchar
[*++p
])
8196 while (*p
== ' ' || *p
== '\t') p
++;
8197 if (! (*p
== 0 || *p
== '(')) {
8198 error ("malformed option `%s %s'", option
, str
);
8202 ip
= &instack
[++indepth
];
8203 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
8205 ip
->buf
= ip
->bufp
= buf
;
8206 ip
->length
= strlen (buf
);
8210 ip
->if_stack
= if_stack
;
8211 ip
->system_header_p
= 0;
8213 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
8216 /* pass NULL as output ptr to do_define since we KNOW it never
8217 does any output.... */
8218 do_assert (buf
, buf
+ strlen (buf
) , NULL
, kt
);
8222 /* Add output to `deps_buffer' for the -M switch.
8223 STRING points to the text to be output.
8224 SIZE is the number of bytes, or 0 meaning output until a null.
8225 Outputting the empty string breaks the line if it is long enough. */
8228 deps_output (string
, size
)
8233 size
= strlen (string
);
8235 #ifndef MAX_OUTPUT_COLUMNS
8236 #define MAX_OUTPUT_COLUMNS 75
8238 if (size
== 0 && deps_column
!= 0
8239 && size
+ deps_column
> MAX_OUTPUT_COLUMNS
) {
8240 deps_output ("\\\n ", 0);
8244 if (deps_size
+ size
+ 1 > deps_allocated_size
) {
8245 deps_allocated_size
= deps_size
+ size
+ 50;
8246 deps_allocated_size
*= 2;
8247 deps_buffer
= (char *) xrealloc (deps_buffer
, deps_allocated_size
);
8249 bcopy (string
, &deps_buffer
[deps_size
], size
);
8251 deps_column
+= size
;
8252 deps_buffer
[deps_size
] = 0;
8255 #if defined(USG) || defined(VMS)
8261 register unsigned length
;
8265 long max_str
= 65535;
8267 while (length
> max_str
) {
8268 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
8272 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &length
, b
);
8274 while (length
-- > 0)
8276 #endif /* not VMS */
8280 bcopy (b1
, b2
, length
)
8283 register unsigned length
;
8286 long max_str
= 65535;
8288 while (length
> max_str
) {
8289 (void) LIB$
MOVC3 (&max_str
, b1
, b2
);
8294 (void) LIB$
MOVC3 (&length
, b1
, b2
);
8296 while (length
-- > 0)
8298 #endif /* not VMS */
8302 bcmp (b1
, b2
, length
) /* This could be a macro! */
8305 register unsigned length
;
8308 struct dsc$descriptor_s src1
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b1
};
8309 struct dsc$descriptor_s src2
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b2
};
8311 return STR$
COMPARE (&src1
, &src2
);
8313 while (length
-- > 0)
8318 #endif /* not VMS */
8320 #endif /* not BSTRING */
8321 #endif /* USG or VMS */
8330 fprintf (stderr
, "%s: ", progname
);
8331 fprintf (stderr
, str
, arg
);
8332 fprintf (stderr
, "\n");
8333 exit (FAILURE_EXIT_CODE
);
8336 /* More 'friendly' abort that prints the line and file.
8337 config.h can #define abort fancy_abort if you like that sort of thing. */
8342 fatal ("Internal gcc abort.");
8346 perror_with_name (name
)
8349 fprintf (stderr
, "%s: ", progname
);
8350 if (errno
< sys_nerr
)
8351 fprintf (stderr
, "%s: %s\n", name
, sys_errlist
[errno
]);
8353 fprintf (stderr
, "%s: undocumented I/O error\n", name
);
8358 pfatal_with_name (name
)
8361 perror_with_name (name
);
8365 exit (FAILURE_EXIT_CODE
);
8373 fatal ("Memory exhausted.");
8381 register char *ptr
= (char *) malloc (size
);
8382 if (ptr
!= 0) return (ptr
);
8389 xrealloc (old
, size
)
8393 register char *ptr
= (char *) realloc (old
, size
);
8394 if (ptr
!= 0) return (ptr
);
8401 xcalloc (number
, size
)
8402 unsigned number
, size
;
8404 register unsigned total
= number
* size
;
8405 register char *ptr
= (char *) malloc (total
);
8410 /* It's not too long, so loop, zeroing by longs.
8411 It must be safe because malloc values are always well aligned. */
8412 register long *zp
= (long *) ptr
;
8413 register long *zl
= (long *) (ptr
+ total
- 4);
8414 register int i
= total
- 4;
8433 unsigned size
= strlen (input
);
8434 char *output
= xmalloc (size
+ 1);
8435 strcpy (output
, input
);
8439 /* Get the file-mode and data size of the file open on FD
8440 and store them in *MODE_POINTER and *SIZE_POINTER. */
8443 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
8446 long int *size_pointer
;
8450 if (fstat (fd
, &sbuf
) < 0) return (-1);
8451 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
8452 if (size_pointer
) *size_pointer
= sbuf
.st_size
;
8458 /* Under VMS we need to fix up the "include" specification
8459 filename so that everything following the 1st slash is
8460 changed into its correct VMS file specification. */
8463 hack_vms_include_specification (fname
)
8466 register char *cp
, *cp1
, *cp2
;
8467 int f
, check_filename_before_returning
, no_prefix_seen
;
8470 check_filename_before_returning
= 0;
8473 /* Ignore leading "./"s */
8474 while (fname
[0] == '.' && fname
[1] == '/') {
8475 strcpy (fname
, fname
+2);
8476 no_prefix_seen
= 1; /* mark this for later */
8478 /* Look for the boundary between the VMS and UNIX filespecs */
8479 cp
= rindex (fname
, ']'); /* Look for end of dirspec. */
8480 if (cp
== 0) cp
= rindex (fname
, '>'); /* ... Ditto */
8481 if (cp
== 0) cp
= rindex (fname
, ':'); /* Look for end of devspec. */
8485 cp
= index (fname
, '/'); /* Look for the "/" */
8488 cp2
= Local
; /* initialize */
8490 /* We are trying to do a number of things here. First of all, we are
8491 trying to hammer the filenames into a standard format, such that later
8492 processing can handle them.
8494 If the file name contains something like [dir.], then it recognizes this
8495 as a root, and strips the ".]". Later processing will add whatever is
8496 needed to get things working properly.
8498 If no device is specified, then the first directory name is taken to be
8499 a device name (or a rooted logical). */
8501 /* See if we found that 1st slash */
8502 if (cp
== 0) return; /* Nothing to do!!! */
8503 if (*cp
!= '/') return; /* Nothing to do!!! */
8504 /* Point to the UNIX filename part (which needs to be fixed!) */
8506 /* If the directory spec is not rooted, we can just copy
8507 the UNIX filename part and we are done */
8508 if (((cp
- fname
) > 1) && ((cp
[-1] == ']') || (cp
[-1] == '>'))) {
8509 if (cp
[-2] != '.') {
8511 * The VMS part ends in a `]', and the preceding character is not a `.'.
8512 * We strip the `]', and then splice the two parts of the name in the
8513 * usual way. Given the default locations for include files in cccp.c,
8514 * we will only use this code if the user specifies alternate locations
8515 * with the /include (-I) switch on the command line. */
8516 cp
-= 1; /* Strip "]" */
8517 cp1
--; /* backspace */
8520 * The VMS part has a ".]" at the end, and this will not do. Later
8521 * processing will add a second directory spec, and this would be a syntax
8522 * error. Thus we strip the ".]", and thus merge the directory specs.
8523 * We also backspace cp1, so that it points to a '/'. This inhibits the
8524 * generation of the 000000 root directory spec (which does not belong here
8527 cp
-= 2; /* Strip ".]" */
8528 cp1
--; }; /* backspace */
8531 /* We drop in here if there is no VMS style directory specification yet.
8532 * If there is no device specification either, we make the first dir a
8533 * device and try that. If we do not do this, then we will be essentially
8534 * searching the users default directory (as if they did a #include "asdf.h").
8536 * Then all we need to do is to push a '[' into the output string. Later
8537 * processing will fill this in, and close the bracket.
8539 if(cp
[-1] != ':') *cp2
++ = ':'; /* dev not in spec. take first dir */
8540 *cp2
++ = '['; /* Open the directory specification */
8543 /* at this point we assume that we have the device spec, and (at least
8544 the opening "[" for a directory specification. We may have directories
8545 specified already */
8547 /* If there are no other slashes then the filename will be
8548 in the "root" directory. Otherwise, we need to add
8549 directory specifications. */
8550 if (index (cp1
, '/') == 0) {
8551 /* Just add "000000]" as the directory string */
8552 strcpy (cp2
, "000000]");
8553 cp2
+= strlen (cp2
);
8554 check_filename_before_returning
= 1; /* we might need to fool with this later */
8556 /* As long as there are still subdirectories to add, do them. */
8557 while (index (cp1
, '/') != 0) {
8558 /* If this token is "." we can ignore it */
8559 if ((cp1
[0] == '.') && (cp1
[1] == '/')) {
8563 /* Add a subdirectory spec. Do not duplicate "." */
8564 if (cp2
[-1] != '.' && cp2
[-1] != '[' && cp2
[-1] != '<')
8566 /* If this is ".." then the spec becomes "-" */
8567 if ((cp1
[0] == '.') && (cp1
[1] == '.') && (cp
[2] == '/')) {
8568 /* Add "-" and skip the ".." */
8573 /* Copy the subdirectory */
8574 while (*cp1
!= '/') *cp2
++= *cp1
++;
8575 cp1
++; /* Skip the "/" */
8577 /* Close the directory specification */
8578 if(cp2
[-1] == '.') /* no trailing periods */
8582 /* Now add the filename */
8583 while (*cp1
) *cp2
++ = *cp1
++;
8585 /* Now append it to the original VMS spec. */
8588 /* If we put a [000000] in the filename, try to open it first. If this fails,
8589 remove the [000000], and return that name. This provides flexibility
8590 to the user in that they can use both rooted and non-rooted logical names
8591 to point to the location of the file. */
8593 if (check_filename_before_returning
&& no_prefix_seen
) {
8594 f
= open (fname
, O_RDONLY
, 0666);
8596 /* The file name is OK as it is, so return it as is. */
8600 /* The filename did not work. Try to remove the [000000] from the name,
8602 cp
= index (fname
, '[');
8603 cp2
= index (fname
, ']') + 1;
8604 strcpy (cp
, cp2
); /* this gets rid of it */
8612 /* These are the read/write replacement routines for
8613 VAX-11 "C". They make read/write behave enough
8614 like their UNIX counterparts that CCCP will work */
8617 read (fd
, buf
, size
)
8622 #undef read /* Get back the REAL read routine */
8624 register int total
= 0;
8626 /* Read until the buffer is exhausted */
8628 /* Limit each read to 32KB */
8629 i
= (size
> (32*1024)) ? (32*1024) : size
;
8630 i
= read (fd
, buf
, i
);
8632 if (i
== 0) return (total
);
8635 /* Account for this read */
8644 write (fd
, buf
, size
)
8649 #undef write /* Get back the REAL write routine */
8653 /* Limit individual writes to 32Kb */
8656 j
= (i
> (32*1024)) ? (32*1024) : i
;
8657 if (write (fd
, buf
, j
) < 0) return (-1);
8658 /* Account for the data written */
8665 /* The following wrapper functions supply additional arguments to the VMS
8666 I/O routines to optimize performance with file handling. The arguments
8668 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
8669 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
8670 "fop=tef"- Truncate unused portions of file when closing file.
8671 "shr=nil"- Disallow file sharing while file is open.
8675 freopen (fname
, type
, oldfile
)
8680 #undef freopen /* Get back the REAL fopen routine */
8681 if (strcmp (type
, "w") == 0)
8682 return freopen (fname
, type
, oldfile
, "mbc=16", "deq=64", "fop=tef", "shr=nil");
8683 return freopen (fname
, type
, oldfile
, "mbc=16");
8691 #undef fopen /* Get back the REAL fopen routine */
8692 if (strcmp (type
, "w") == 0)
8693 return fopen (fname
, type
, "mbc=16", "deq=64", "fop=tef", "shr=nil");
8694 return fopen (fname
, type
, "mbc=16");
8698 open (fname
, flags
, prot
)
8703 #undef open /* Get back the REAL open routine */
8704 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");