Only allow allow rotations by a constant amount.
[official-gcc.git] / gcc / tradcpp.c
blob5e88c02298932ea8b55946c384e04126c9a82ca1
1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 1987, 1989, 2000 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
5 Dusted off, polished, and adapted for use as traditional
6 preprocessor only, Zack Weinberg, Jul 2000
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 1, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
26 #include "config.h"
27 #include "system.h"
28 #include "version.h"
29 #include "cppdefault.h"
31 typedef unsigned char U_CHAR;
33 /* Name under which this program was invoked. */
35 char *progname;
37 /* Current maximum length of directory names in the search path
38 for include files. (Altered as we get more of them.) */
40 size_t max_include_len;
42 /* Nonzero means copy comments into the output file. */
44 int put_out_comments = 0;
46 /* Nonzero means print the names of included files rather than
47 the preprocessed output. 1 means just the #include "...",
48 2 means #include <...> as well. */
50 int print_deps = 0;
52 /* Nonzero means don't output line number information. */
54 int no_line_commands;
56 /* Nonzero means inhibit output of the preprocessed text
57 and instead output the definitions of all user-defined macros
58 in a form suitable for use as input to cccp. */
60 int dump_macros;
62 /* Nonzero means don't print warning messages. -w. */
64 int inhibit_warnings = 0;
66 /* Nonzero means warn if slash-star appears in a comment. */
68 int warn_comments;
70 /* Nonzero causes output not to be done,
71 but directives such as #define that have side effects
72 are still obeyed. */
74 int no_output;
76 /* Value of __USER_LABEL_PREFIX__. Target-dependent, also controlled
77 by -f(no-)leading-underscore. */
78 const char *user_label_prefix;
80 /* I/O buffer structure.
81 The `fname' field is nonzero for source files and #include files
82 and for the dummy text used for -D and -U.
83 It is zero for rescanning results of macro expansion
84 and for expanding macro arguments. */
85 #define INPUT_STACK_MAX 200
86 struct file_buf {
87 const char *fname;
88 int lineno;
89 int length;
90 U_CHAR *buf;
91 U_CHAR *bufp;
92 /* Macro that this level is the expansion of.
93 Included so that we can reenable the macro
94 at the end of this level. */
95 struct hashnode *macro;
96 /* Value of if_stack at start of this file.
97 Used to prohibit unmatched #endif (etc) in an include file. */
98 struct if_stack *if_stack;
99 /* Object to be freed at end of input at this level. */
100 U_CHAR *free_ptr;
101 } instack[INPUT_STACK_MAX];
103 typedef struct file_buf FILE_BUF;
105 /* Current nesting level of input sources.
106 `instack[indepth]' is the level currently being read. */
107 int indepth = -1;
108 #define CHECK_DEPTH(code) \
109 if (indepth >= (INPUT_STACK_MAX - 1)) \
111 error_with_line (line_for_error (instack[indepth].lineno), \
112 "macro or #include recursion too deep"); \
113 code; \
116 /* Current depth in #include directives that use <...>. */
117 int system_include_depth = 0;
119 /* The output buffer. Its LENGTH field is the amount of room allocated
120 for the buffer, not the number of chars actually present. To get
121 that, subtract outbuf.buf from outbuf.bufp. */
123 #define OUTBUF_SIZE 10 /* initial size of output buffer */
124 FILE_BUF outbuf;
126 /* Grow output buffer OBUF points at
127 so it can hold at least NEEDED more chars. */
129 #define check_expand(OBUF, NEEDED) do { \
130 if ((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
131 grow_outbuf ((OBUF), (NEEDED)); \
132 } while (0)
134 struct file_name_list
136 struct file_name_list *next;
137 const char *fname;
140 struct file_name_list *include = 0; /* First dir to search */
141 /* First dir to search for <file> */
142 struct file_name_list *first_bracket_include = 0;
143 struct file_name_list *last_include = 0; /* Last in chain */
145 /* List of included files that contained #once. */
146 struct file_name_list *dont_repeat_files = 0;
148 /* List of other included files. */
149 struct file_name_list *all_include_files = 0;
151 /* Structure allocated for every #define. For a simple replacement
152 such as
153 #define foo bar ,
154 nargs = -1, the `pattern' list is null, and the expansion is just
155 the replacement text. Nargs = 0 means a functionlike macro with no args,
156 e.g.,
157 #define getchar() getc (stdin) .
158 When there are args, the expansion is the replacement text with the
159 args squashed out, and the reflist is a list describing how to
160 build the output from the input: e.g., "3 chars, then the 1st arg,
161 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
162 The chars here come from the expansion. Whatever is left of the
163 expansion after the last arg-occurrence is copied after that arg.
164 Note that the reflist can be arbitrarily long---
165 its length depends on the number of times the arguments appear in
166 the replacement text, not how many args there are. Example:
167 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
168 pattern list
169 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
170 where (x, y) means (nchars, argno). */
172 typedef struct definition DEFINITION;
173 struct definition {
174 int nargs;
175 int length; /* length of expansion string */
176 U_CHAR *expansion;
177 struct reflist {
178 struct reflist *next;
179 char stringify; /* nonzero if this arg was preceded by a
180 # operator. */
181 char raw_before; /* Nonzero if a ## operator before arg. */
182 char raw_after; /* Nonzero if a ## operator after arg. */
183 int nchars; /* Number of literal chars to copy before
184 this arg occurrence. */
185 int argno; /* Number of arg to substitute (origin-0) */
186 } *pattern;
187 /* Names of macro args, concatenated in reverse order
188 with comma-space between them.
189 The only use of this is that we warn on redefinition
190 if this differs between the old and new definitions. */
191 U_CHAR *argnames;
194 /* different kinds of things that can appear in the value field
195 of a hash node. Actually, this may be useless now. */
196 union hashval {
197 const char *cpval;
198 DEFINITION *defn;
202 /* The structure of a node in the hash table. The hash table
203 has entries for all tokens defined by #define commands (type T_MACRO),
204 plus some special tokens like __LINE__ (these each have their own
205 type, and the appropriate code is run when that type of node is seen.
206 It does not contain control words like "#define", which are recognized
207 by a separate piece of code. */
209 /* different flavors of hash nodes --- also used in keyword table */
210 enum node_type {
211 T_DEFINE = 1, /* `#define' */
212 T_INCLUDE, /* `#include' */
213 T_IFDEF, /* `#ifdef' */
214 T_IFNDEF, /* `#ifndef' */
215 T_IF, /* `#if' */
216 T_ELSE, /* `#else' */
217 T_ELIF, /* `#elif' */
218 T_UNDEF, /* `#undef' */
219 T_LINE, /* `#line' */
220 T_ENDIF, /* `#endif' */
221 T_SPECLINE, /* special symbol `__LINE__' */
222 T_DATE, /* `__DATE__' */
223 T_FILE, /* `__FILE__' */
224 T_BASE_FILE, /* `__BASE_FILE__' */
225 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
226 T_VERSION, /* `__VERSION__' */
227 T_TIME, /* `__TIME__' */
228 T_CONST, /* Constant value, used by `__STDC__' */
229 T_MACRO, /* macro defined by `#define' */
230 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
231 T_UNUSED /* Used for something not defined. */
234 struct hashnode {
235 struct hashnode *next; /* double links for easy deletion */
236 struct hashnode *prev;
237 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
238 chain is kept, in case the node is the head
239 of the chain and gets deleted. */
240 enum node_type type; /* type of special token */
241 int length; /* length of token, for quick comparison */
242 U_CHAR *name; /* the actual name */
243 union hashval value; /* pointer to expansion, or whatever */
246 typedef struct hashnode HASHNODE;
248 /* Some definitions for the hash table. The hash function MUST be
249 computed as shown in hashf () below. That is because the rescan
250 loop computes the hash value `on the fly' for most tokens,
251 in order to avoid the overhead of a lot of procedure calls to
252 the hashf () function. Hashf () only exists for the sake of
253 politeness, for use when speed isn't so important. */
255 #define HASHSIZE 1403
256 HASHNODE *hashtab[HASHSIZE];
257 #define HASHSTEP(old, c) ((old << 2) + c)
258 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
260 /* `struct directive' defines one #-directive, including how to handle it. */
262 struct directive {
263 int length; /* Length of name */
264 void (*func) PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
265 /* Function to handle directive */
266 const char *name; /* Name of directive */
267 enum node_type type; /* Code which describes which directive. */
270 /* Last arg to output_line_command. */
271 enum file_change_code {same_file, enter_file, leave_file};
273 /* This structure represents one parsed argument in a macro call.
274 `raw' points to the argument text as written (`raw_length' is its length).
275 `expanded' points to the argument's macro-expansion
276 (its length is `expand_length').
277 `stringified_length' is the length the argument would have
278 if stringified.
279 `free1' and `free2', if nonzero, point to blocks to be freed
280 when the macro argument data is no longer needed. */
282 struct argdata {
283 U_CHAR *raw, *expanded;
284 int raw_length, expand_length;
285 int stringified_length;
286 U_CHAR *free1, *free2;
287 char newlines;
288 char comments;
291 /* The arglist structure is built by do_define to tell
292 collect_definition where the argument names begin. That
293 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
294 would contain pointers to the strings x, y, and z.
295 Collect_definition would then build a DEFINITION node,
296 with reflist nodes pointing to the places x, y, and z had
297 appeared. So the arglist is just convenience data passed
298 between these two routines. It is not kept around after
299 the current #define has been processed and entered into the
300 hash table. */
302 struct arglist {
303 struct arglist *next;
304 U_CHAR *name;
305 int length;
306 int argno;
309 /* Function prototypes. */
311 void do_define PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
312 void do_line PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
313 void do_include PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
314 void do_undef PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
315 void do_if PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
316 void do_xifdef PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
317 void do_else PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
318 void do_elif PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
319 void do_endif PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
321 struct hashnode *install PARAMS ((const U_CHAR *, int, enum node_type, int));
322 struct hashnode *lookup PARAMS ((const U_CHAR *, int, int));
323 int hashf PARAMS ((const U_CHAR *, int, int));
324 int compare_defs PARAMS ((DEFINITION *, DEFINITION *));
325 int comp_def_part PARAMS ((int, U_CHAR *, int, U_CHAR *, int, int));
326 void delete_macro PARAMS ((HASHNODE *));
328 /* First arg to v_message. */
329 enum msgtype { WARNING = 0, ERROR, FATAL };
330 void v_message PARAMS ((enum msgtype mtype, int line,
331 const char *msgid, va_list ap));
333 void warning PARAMS ((const char *msgid, ...));
334 void error PARAMS ((const char *msgid, ...));
335 void fatal PARAMS ((const char *msgid, ...)) ATTRIBUTE_NORETURN;
336 void error_with_line PARAMS ((int, const char *msgid, ...));
337 void error_from_errno PARAMS ((const char *msgid));
339 void perror_with_name PARAMS ((const char *msgid));
340 void pfatal_with_name PARAMS ((const char *msgid)) ATTRIBUTE_NORETURN;
341 void fancy_abort PARAMS ((int, const char *)) ATTRIBUTE_NORETURN;
343 int line_for_error PARAMS ((int));
345 /* We know perfectly well which file this is, so we don't need to
346 use __FILE__. */
347 #undef abort
348 #if (GCC_VERSION >= 2007)
349 #define abort() fancy_abort(__LINE__, __FUNCTION__)
350 #else
351 #define abort() fancy_abort(__LINE__, 0);
352 #endif
354 void macroexpand PARAMS ((HASHNODE *, FILE_BUF *));
355 void special_symbol PARAMS ((HASHNODE *, FILE_BUF *));
356 void dump_all_macros PARAMS ((void));
357 void dump_defn_1 PARAMS ((U_CHAR *, int, int));
358 void dump_arg_n PARAMS ((DEFINITION *, int));
359 void conditional_skip PARAMS ((FILE_BUF *, int, enum node_type));
360 void skip_if_group PARAMS ((FILE_BUF *, int));
361 void output_line_command PARAMS ((FILE_BUF *, FILE_BUF *,
362 int, enum file_change_code));
364 int eval_if_expression PARAMS ((U_CHAR *, int));
365 int parse_c_expression PARAMS ((char *)); /* in tradcif.y */
367 void initialize_char_syntax PARAMS ((void));
368 void initialize_builtins PARAMS ((void));
369 void make_definition PARAMS ((U_CHAR *));
370 void make_undef PARAMS ((U_CHAR *));
372 void grow_outbuf PARAMS ((FILE_BUF *, int));
373 int handle_directive PARAMS ((FILE_BUF *, FILE_BUF *));
374 void finclude PARAMS ((int, const char *, FILE_BUF *));
375 void deps_output PARAMS ((const char *, int));
376 void rescan PARAMS ((FILE_BUF *, int));
377 void newline_fix PARAMS ((U_CHAR *));
378 void name_newline_fix PARAMS ((U_CHAR *));
379 U_CHAR *macarg1 PARAMS ((U_CHAR *, U_CHAR *, int *, int *, int *));
380 const char *macarg PARAMS ((struct argdata *));
381 int discard_comments PARAMS ((U_CHAR *, int, int));
382 int file_size_and_mode PARAMS ((int, int *, long *));
384 U_CHAR *skip_to_end_of_comment PARAMS ((FILE_BUF *, int *));
385 U_CHAR *skip_quoted_string PARAMS ((U_CHAR *, U_CHAR *, int,
386 int *, int *, int *));
388 int main PARAMS ((int, char **));
390 /* Convenience. Write U"string" to get an unsigned string constant. */
391 #define U (const unsigned char *)
393 /* Here is the actual list of #-directives, most-often-used first. */
395 struct directive directive_table[] = {
396 { 6, do_define, "define", T_DEFINE },
397 { 7, do_include, "include", T_INCLUDE },
398 { 5, do_endif, "endif", T_ENDIF },
399 { 5, do_xifdef, "ifdef", T_IFDEF },
400 { 2, do_if, "if", T_IF, },
401 { 4, do_else, "else", T_ELSE },
402 { 6, do_xifdef, "ifndef", T_IFNDEF },
403 { 5, do_undef, "undef", T_UNDEF },
404 { 4, do_line, "line", T_LINE },
405 { 4, do_elif, "elif", T_ELIF },
406 { -1, 0, "", T_UNUSED},
409 /* table to tell if char can be part of a C identifier. */
410 U_CHAR is_idchar[256];
411 /* table to tell if char can be first char of a c identifier. */
412 U_CHAR is_idstart[256];
413 /* table to tell if c is horizontal space. */
414 U_CHAR is_hor_space[256];
415 /* table to tell if c is horizontal or vertical space. */
416 U_CHAR is_space[256];
418 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
419 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
421 int errors = 0; /* Error counter for exit code */
423 FILE_BUF expand_to_temp_buffer PARAMS ((U_CHAR *, U_CHAR *, int));
424 DEFINITION *collect_expansion PARAMS ((U_CHAR *, U_CHAR *, int,
425 struct arglist *));
427 /* Stack of conditionals currently in progress
428 (including both successful and failing conditionals). */
430 struct if_stack {
431 struct if_stack *next; /* for chaining to the next stack frame */
432 const char *fname; /* copied from input when frame is made */
433 int lineno; /* similarly */
434 int if_succeeded; /* true if a leg of this if-group
435 has been passed through rescan */
436 enum node_type type; /* type of last directive seen in this group */
438 typedef struct if_stack IF_STACK_FRAME;
439 IF_STACK_FRAME *if_stack = NULL;
441 /* Buffer of -M output. */
443 char *deps_buffer;
445 /* Number of bytes allocated in above. */
446 int deps_allocated_size;
448 /* Number of bytes used. */
449 int deps_size;
451 /* Number of bytes since the last newline. */
452 int deps_column;
454 /* Nonzero means -I- has been seen,
455 so don't look for #include "foo" the source-file directory. */
456 int ignore_srcdir;
459 main (argc, argv)
460 int argc;
461 char **argv;
463 int st_mode;
464 long st_size;
465 const char *in_fname, *out_fname;
466 int f, i;
467 FILE_BUF *fp;
468 const char **pend_files = (const char **) xmalloc (argc * sizeof (char *));
469 const char **pend_defs = (const char **) xmalloc (argc * sizeof (char *));
470 const char **pend_undefs = (const char **) xmalloc (argc * sizeof (char *));
471 int no_standard_includes = 0;
473 /* Non-0 means don't output the preprocessed program. */
474 int inhibit_output = 0;
476 /* Stream on which to print the dependency information. */
477 FILE *deps_stream = 0;
478 /* Target-name to write with the dependency information. */
479 char *deps_target = 0;
481 #ifdef RLIMIT_STACK
482 /* Get rid of any avoidable limit on stack size. */
484 struct rlimit rlim;
486 /* Set the stack limit huge so that alloca (particularly stringtab
487 * in dbxread.c) does not fail. */
488 getrlimit (RLIMIT_STACK, &rlim);
489 rlim.rlim_cur = rlim.rlim_max;
490 setrlimit (RLIMIT_STACK, &rlim);
492 #endif /* RLIMIT_STACK defined */
494 progname = argv[0];
496 in_fname = NULL;
497 out_fname = NULL;
499 /* Initialize is_idchar to allow $. */
500 initialize_char_syntax ();
502 no_line_commands = 0;
503 dump_macros = 0;
504 no_output = 0;
506 max_include_len = cpp_GCC_INCLUDE_DIR_len + 7; /* ??? */
508 memset (pend_files, 0, argc * sizeof (char *));
509 memset (pend_defs, 0, argc * sizeof (char *));
510 memset (pend_undefs, 0, argc * sizeof (char *));
512 /* Process switches and find input file name. */
514 for (i = 1; i < argc; i++) {
515 if (argv[i][0] != '-') {
516 if (out_fname != NULL)
517 fatal ("Usage: %s [switches] input output", argv[0]);
518 else if (in_fname != NULL)
519 out_fname = argv[i];
520 else
521 in_fname = argv[i];
522 } else {
523 switch (argv[i][1]) {
524 case 'A':
525 case 'E':
526 case '$':
527 case 'g':
528 break; /* Ignore for compatibility with ISO/extended cpp. */
530 case 'l':
531 if (!strcmp (argv[i], "-lang-c++")
532 || !strcmp (argv[i], "-lang-objc++"))
533 fatal ("-traditional is not supported in C++");
534 else if (!strcmp (argv[i], "-lang-c89"))
535 fatal ("-traditional and -ansi are mutually exclusive");
536 else if (!strcmp (argv[i], "-lang-objc"))
537 pend_defs[i] = "__OBJC__";
538 else if (!strcmp (argv[i], "-lang-asm"))
539 pend_defs[i] = "__ASSEMBLER__";
540 else if (!strcmp (argv[i], "-lang-fortran"))
541 pend_defs[i] = "_LANGUAGE_FORTRAN";
542 /* All other possibilities ignored. */
543 break;
545 case 'i':
546 if (!strcmp (argv[i], "-include"))
548 if (i + 1 == argc)
549 fatal ("Filename missing after -i option");
550 else
551 pend_files[i] = argv[i+1], i++;
553 else if (!strcmp (argv[i], "-iprefix"))
554 i++; /* Ignore for compatibility */
555 else if (!strcmp (argv[i], "-isystem")
556 || !strcmp (argv[i], "-iwithprefix")
557 || !strcmp (argv[i], "-iwithprefixbefore")
558 || !strcmp (argv[i], "-idirafter"))
559 goto add_include; /* best we can do */
561 break;
563 case 'o':
564 if (out_fname != NULL)
565 fatal ("Output filename specified twice");
566 if (i + 1 == argc)
567 fatal ("Filename missing after -o option");
568 out_fname = argv[++i];
569 if (!strcmp (out_fname, "-"))
570 out_fname = "";
571 break;
573 case 'w':
574 inhibit_warnings = 1;
575 break;
577 case 'W':
578 if (!strcmp (argv[i], "-Wcomments"))
579 warn_comments = 1;
580 else if (!strcmp (argv[i], "-Wcomment"))
581 warn_comments = 1;
582 else if (!strcmp (argv[i], "-Wall")) {
583 warn_comments = 1;
585 break;
587 case 'f':
588 if (!strcmp (argv[i], "-fleading-underscore"))
589 user_label_prefix = "_";
590 else if (!strcmp (argv[i], "-fno-leading-underscore"))
591 user_label_prefix = "";
592 break;
594 case 'M':
595 if (!strcmp (argv[i], "-M"))
596 print_deps = 2;
597 else if (!strcmp (argv[i], "-MM"))
598 print_deps = 1;
599 inhibit_output = 1;
600 break;
602 case 'd':
603 dump_macros = 1;
604 no_output = 1;
605 break;
607 case 'v':
608 fprintf (stderr, "GNU traditional CPP version %s\n", version_string);
609 break;
611 case 'D':
613 char *p;
615 if (argv[i][2] != 0)
616 p = argv[i] + 2;
617 else if (i + 1 == argc)
618 fatal ("Macro name missing after -D option");
619 else
620 p = argv[++i];
622 pend_defs[i] = p;
624 break;
626 case 'U': /* JF #undef something */
627 if (argv[i][2] != 0)
628 pend_undefs[i] = argv[i] + 2;
629 else if (i + 1 == argc)
630 fatal ("Macro name missing after -U option");
631 else
632 pend_undefs[i] = argv[i+1], i++;
633 break;
635 case 'C':
636 put_out_comments = 1;
637 break;
639 case 'p':
640 if (!strcmp (argv[i], "-pedantic"))
641 fatal ("-pedantic and -traditional are mutually exclusive");
642 break;
644 case 't':
645 if (!strcmp (argv[i], "-trigraphs"))
646 fatal ("-trigraphs and -traditional are mutually exclusive");
647 break;
649 case 'P':
650 no_line_commands = 1;
651 break;
653 case 'I': /* Add directory to path for includes. */
654 add_include:
656 struct file_name_list *dirtmp;
658 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
659 ignore_srcdir = 1;
660 else {
661 dirtmp = (struct file_name_list *)
662 xmalloc (sizeof (struct file_name_list));
663 dirtmp->next = 0; /* New one goes on the end */
664 if (include == 0)
665 include = dirtmp;
666 else
667 last_include->next = dirtmp;
668 last_include = dirtmp; /* Tail follows the last one */
669 if (argv[i][1] == 'I' && argv[i][2] != 0)
670 dirtmp->fname = argv[i] + 2;
671 else if (i + 1 == argc)
672 fatal ("Directory name missing after -I option");
673 else
674 dirtmp->fname = argv[++i];
675 if (strlen (dirtmp->fname) > max_include_len)
676 max_include_len = strlen (dirtmp->fname);
677 if (ignore_srcdir && first_bracket_include == 0)
678 first_bracket_include = dirtmp;
681 break;
683 case 'n':
684 /* -nostdinc causes no default include directories.
685 You must specify all include-file directories with -I. */
686 no_standard_includes = 1;
687 break;
689 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
690 if (in_fname == NULL) {
691 in_fname = "";
692 break;
693 } else if (out_fname == NULL) {
694 out_fname = "";
695 break;
696 } /* else fall through into error */
698 default:
699 fatal ("Invalid option `%s'", argv[i]);
704 if (user_label_prefix == 0)
705 user_label_prefix = USER_LABEL_PREFIX;
707 /* Initialize is_idchar. */
708 initialize_char_syntax ();
710 /* Install __LINE__, etc. Must follow initialize_char_syntax
711 and option processing. */
712 initialize_builtins ();
714 /* Do defines specified with -D. */
715 for (i = 1; i < argc; i++)
716 if (pend_defs[i])
717 make_definition ((U_CHAR *)pend_defs[i]);
719 /* Do undefines specified with -U. */
720 for (i = 1; i < argc; i++)
721 if (pend_undefs[i])
722 make_undef ((U_CHAR *)pend_undefs[i]);
724 /* Unless -fnostdinc,
725 tack on the standard include file dirs to the specified list */
726 if (!no_standard_includes) {
727 const struct default_include *di;
728 struct file_name_list *old_last_include = last_include;
729 struct file_name_list *dirtmp;
730 for (di = cpp_include_defaults; di->fname; di++) {
731 if (di->cplusplus)
732 continue;
733 dirtmp = (struct file_name_list *)
734 xmalloc (sizeof (struct file_name_list));
735 dirtmp->next = 0; /* New one goes on the end */
736 if (include == 0)
737 include = dirtmp;
738 else
739 last_include->next = dirtmp;
740 last_include = dirtmp; /* Tail follows the last one */
741 dirtmp->fname = di->fname;
742 if (strlen (dirtmp->fname) > max_include_len)
743 max_include_len = strlen (dirtmp->fname);
746 if (ignore_srcdir && first_bracket_include == 0)
747 first_bracket_include = old_last_include->next;
750 /* Initialize output buffer */
752 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
753 outbuf.bufp = outbuf.buf;
754 outbuf.length = OUTBUF_SIZE;
756 /* Scan the -i files before the main input.
757 Much like #including them, but with no_output set
758 so that only their macro definitions matter. */
760 no_output++;
761 for (i = 1; i < argc; i++)
762 if (pend_files[i]) {
763 int fd = open (pend_files[i], O_RDONLY, 0666);
764 if (fd < 0) {
765 perror_with_name (pend_files[i]);
766 return FATAL_EXIT_CODE;
768 finclude (fd, pend_files[i], &outbuf);
770 no_output--;
772 /* Create an input stack level for the main input file
773 and copy the entire contents of the file into it. */
775 fp = &instack[++indepth];
777 /* JF check for stdin */
778 if (in_fname == NULL || *in_fname == 0) {
779 in_fname = "";
780 f = 0;
781 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
782 goto sys_error;
784 /* Either of two environment variables can specify output of deps.
785 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
786 where OUTPUT_FILE is the file to write deps info to
787 and DEPS_TARGET is the target to mention in the deps. */
789 if (print_deps == 0
790 && (getenv ("SUNPRO_DEPENDENCIES") != 0
791 || getenv ("DEPENDENCIES_OUTPUT") != 0))
793 char *spec = getenv ("DEPENDENCIES_OUTPUT");
794 char *s;
795 char *output_file;
797 if (spec == 0)
799 spec = getenv ("SUNPRO_DEPENDENCIES");
800 print_deps = 2;
802 else
803 print_deps = 1;
805 /* Find the space before the DEPS_TARGET, if there is one. */
806 s = strchr (spec, ' ');
807 if (s)
809 deps_target = s + 1;
810 output_file = (char *) xmalloc (s - spec + 1);
811 memcpy (output_file, spec, s - spec);
812 output_file[s - spec] = 0;
814 else
816 deps_target = 0;
817 output_file = spec;
820 deps_stream = fopen (output_file, "a");
821 if (deps_stream == 0)
822 pfatal_with_name (output_file);
824 /* If the -M option was used, output the deps to standard output. */
825 else if (print_deps)
826 deps_stream = stdout;
828 /* For -M, print the expected object file name
829 as the target of this Make-rule. */
830 if (print_deps) {
831 deps_allocated_size = 200;
832 deps_buffer = (char *) xmalloc (deps_allocated_size);
833 deps_buffer[0] = 0;
834 deps_size = 0;
835 deps_column = 0;
837 if (deps_target) {
838 deps_output (deps_target, 0);
839 deps_output (":", 0);
840 } else if (*in_fname == 0)
841 deps_output ("-: ", 0);
842 else {
843 int len;
844 const char *p = in_fname;
845 const char *p1 = p;
846 /* Discard all directory prefixes from P. */
847 while (*p1) {
848 if (*p1 == '/')
849 p = p1 + 1;
850 p1++;
852 /* Output P, but remove known suffixes. */
853 len = strlen (p);
854 if (p[len - 2] == '.'
855 && (p[len - 1] == 'c' || p[len - 1] == 'C' || p[len - 1] == 'S'))
856 deps_output (p, len - 2);
857 else if (p[len - 3] == '.'
858 && p[len - 2] == 'c'
859 && p[len - 1] == 'c')
860 deps_output (p, len - 3);
861 else
862 deps_output (p, 0);
863 /* Supply our own suffix. */
864 deps_output (".o : ", 0);
865 deps_output (in_fname, 0);
866 deps_output (" ", 0);
870 if (file_size_and_mode (f, &st_mode, &st_size))
871 goto sys_error;
872 fp->fname = in_fname;
873 fp->lineno = 1;
874 /* JF all this is mine about reading pipes and ttys */
875 if (!S_ISREG (st_mode)) {
876 /* Read input from a file that is not a normal disk file.
877 We cannot preallocate a buffer with the correct size,
878 so we must read in the file a piece at the time and make it bigger. */
879 int size;
880 int bsize;
881 int cnt;
882 U_CHAR *bufp;
884 bsize = 2000;
885 size = 0;
886 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
887 bufp = fp->buf;
888 for (;;) {
889 cnt = read (f, bufp, bsize - size);
890 if (cnt < 0) goto sys_error; /* error! */
891 if (cnt == 0) break; /* End of file */
892 size += cnt;
893 bufp += cnt;
894 if (bsize == size) { /* Buffer is full! */
895 bsize *= 2;
896 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
897 bufp = fp->buf + size; /* May have moved */
900 fp->length = size;
901 } else {
902 /* Read a file whose size we can determine in advance.
903 For the sake of VMS, st_size is just an upper bound. */
904 long i;
905 fp->length = 0;
906 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
908 while (st_size > 0) {
909 i = read (f, fp->buf + fp->length, st_size);
910 if (i <= 0) {
911 if (i == 0) break;
912 goto sys_error;
914 fp->length += i;
915 st_size -= i;
918 fp->bufp = fp->buf;
919 fp->if_stack = if_stack;
921 /* Make sure data ends with a newline. And put a null after it. */
923 if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
924 fp->buf[fp->length++] = '\n';
925 fp->buf[fp->length] = '\0';
927 /* Now that we know the input file is valid, open the output. */
929 if (!out_fname || !strcmp (out_fname, ""))
930 out_fname = "stdout";
931 else if (! freopen (out_fname, "w", stdout))
932 pfatal_with_name (out_fname);
934 output_line_command (fp, &outbuf, 0, same_file);
936 /* Scan the input, processing macros and directives. */
938 rescan (&outbuf, 0);
940 /* Now we have processed the entire input
941 Write whichever kind of output has been requested. */
944 if (dump_macros)
945 dump_all_macros ();
946 else if (! inhibit_output && deps_stream != stdout) {
947 if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0)
948 fatal ("I/O error on output");
951 if (print_deps) {
952 fputs (deps_buffer, deps_stream);
953 putc ('\n', deps_stream);
954 if (deps_stream != stdout) {
955 fclose (deps_stream);
956 if (ferror (deps_stream))
957 fatal ("I/O error on output");
961 if (ferror (stdout))
962 fatal ("I/O error on output");
964 if (errors)
965 exit (FATAL_EXIT_CODE);
966 exit (SUCCESS_EXIT_CODE);
968 sys_error:
969 pfatal_with_name (in_fname);
972 /* Move all backslash-newline pairs out of embarrassing places.
973 Exchange all such pairs following BP
974 with any potentially-embarrasing characters that follow them.
975 Potentially-embarrassing characters are / and *
976 (because a backslash-newline inside a comment delimiter
977 would cause it not to be recognized). */
978 void
979 newline_fix (bp)
980 U_CHAR *bp;
982 register U_CHAR *p = bp;
983 register int count = 0;
985 /* First count the backslash-newline pairs here. */
987 while (*p++ == '\\' && *p++ == '\n')
988 count++;
990 p = bp + count * 2;
992 /* Exit if what follows the backslash-newlines is not embarrassing. */
994 if (count == 0 || (*p != '/' && *p != '*'))
995 return;
997 /* Copy all potentially embarrassing characters
998 that follow the backslash-newline pairs
999 down to where the pairs originally started. */
1001 while (*p == '*' || *p == '/')
1002 *bp++ = *p++;
1004 /* Now write the same number of pairs after the embarrassing chars. */
1005 while (count-- > 0) {
1006 *bp++ = '\\';
1007 *bp++ = '\n';
1011 /* Like newline_fix but for use within a directive-name.
1012 Move any backslash-newlines up past any following symbol constituents. */
1013 void
1014 name_newline_fix (bp)
1015 U_CHAR *bp;
1017 register U_CHAR *p = bp;
1018 register int count = 0;
1020 /* First count the backslash-newline pairs here. */
1022 while (*p++ == '\\' && *p++ == '\n')
1023 count++;
1025 p = bp + count * 2;
1027 /* What follows the backslash-newlines is not embarrassing. */
1029 if (count == 0 || !is_idchar[*p])
1030 return;
1032 /* Copy all potentially embarrassing characters
1033 that follow the backslash-newline pairs
1034 down to where the pairs originally started. */
1036 while (is_idchar[*p])
1037 *bp++ = *p++;
1039 /* Now write the same number of pairs after the embarrassing chars. */
1040 while (count-- > 0) {
1041 *bp++ = '\\';
1042 *bp++ = '\n';
1047 * The main loop of the program.
1049 * Read characters from the input stack, transferring them to the
1050 * output buffer OP.
1052 * Macros are expanded and push levels on the input stack.
1053 * At the end of such a level it is popped off and we keep reading.
1054 * At the end of any other kind of level, we return.
1055 * #-directives are handled, except within macros.
1057 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1058 * and insert them when appropriate. This is set while scanning macro
1059 * arguments before substitution. It is zero when scanning for final output.
1060 * There are three types of Newline markers:
1061 * * Newline - follows a macro name that was not expanded
1062 * because it appeared inside an expansion of the same macro.
1063 * This marker prevents future expansion of that identifier.
1064 * When the input is rescanned into the final output, these are deleted.
1065 * These are also deleted by ## concatenation.
1066 * * Newline Space (or Newline and any other whitespace character)
1067 * stands for a place that tokens must be separated or whitespace
1068 * is otherwise desirable, but where the ANSI standard specifies there
1069 * is no whitespace. This marker turns into a Space (or whichever other
1070 * whitespace char appears in the marker) in the final output,
1071 * but it turns into nothing in an argument that is stringified with #.
1072 * Such stringified arguments are the only place where the ANSI standard
1073 * specifies with precision that whitespace may not appear.
1075 * During this function, IP->bufp is kept cached in IBP for speed of access.
1076 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1077 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1078 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1079 * explicitly, and before RECACHE, since RECACHE uses OBP.
1082 void
1083 rescan (op, output_marks)
1084 FILE_BUF *op;
1085 int output_marks;
1087 /* Character being scanned in main loop. */
1088 register U_CHAR c;
1090 /* Length of pending accumulated identifier. */
1091 register int ident_length = 0;
1093 /* Hash code of pending accumulated identifier. */
1094 register int hash = 0;
1096 /* Current input level (&instack[indepth]). */
1097 FILE_BUF *ip;
1099 /* Pointer for scanning input. */
1100 register U_CHAR *ibp;
1102 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1103 register U_CHAR *limit;
1105 /* Pointer for storing output. */
1106 register U_CHAR *obp;
1108 /* REDO_CHAR is nonzero if we are processing an identifier
1109 after backing up over the terminating character.
1110 Sometimes we process an identifier without backing up over
1111 the terminating character, if the terminating character
1112 is not special. Backing up is done so that the terminating character
1113 will be dispatched on again once the identifier is dealt with. */
1114 int redo_char = 0;
1116 /* 1 if within an identifier inside of which a concatenation
1117 marker (Newline -) has been seen. */
1118 int concatenated = 0;
1120 /* While scanning a comment or a string constant,
1121 this records the line it started on, for error messages. */
1122 int start_line;
1124 /* Record position of last `real' newline. */
1125 U_CHAR *beg_of_line;
1127 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1129 #define POPMACRO \
1130 do { ip->macro->type = T_MACRO; \
1131 if (ip->free_ptr) free (ip->free_ptr); \
1132 --indepth; } while (0)
1134 /* Reload `rescan's local variables that describe the current
1135 level of the input stack. */
1137 #define RECACHE \
1138 do { ip = &instack[indepth]; \
1139 ibp = ip->bufp; \
1140 limit = ip->buf + ip->length; \
1141 op->bufp = obp; \
1142 check_expand (op, limit - ibp); \
1143 beg_of_line = 0; \
1144 obp = op->bufp; } while (0)
1146 if (no_output && instack[indepth].fname != 0)
1147 skip_if_group (&instack[indepth], 1);
1149 obp = op->bufp;
1150 RECACHE;
1151 beg_of_line = ibp;
1153 /* Our caller must always put a null after the end of
1154 the input at each input stack level. */
1155 if (*limit != 0)
1156 abort ();
1158 while (1) {
1159 c = *ibp++;
1160 *obp++ = c;
1162 switch (c) {
1163 case '\\':
1164 if (ibp >= limit)
1165 break;
1166 if (*ibp == '\n') {
1167 /* Always merge lines ending with backslash-newline,
1168 even in middle of identifier. */
1169 ++ibp;
1170 ++ip->lineno;
1171 --obp; /* remove backslash from obuf */
1172 break;
1174 /* Otherwise, backslash suppresses specialness of following char,
1175 so copy it here to prevent the switch from seeing it.
1176 But first get any pending identifier processed. */
1177 if (ident_length > 0)
1178 goto specialchar;
1179 *obp++ = *ibp++;
1180 break;
1182 case '#':
1183 /* If this is expanding a macro definition, don't recognize
1184 preprocessor directives. */
1185 if (ip->macro != 0)
1186 goto randomchar;
1187 if (ident_length)
1188 goto specialchar;
1190 /* # keyword: a # must be the first char on the line */
1191 if (beg_of_line == 0)
1192 goto randomchar;
1193 if (beg_of_line + 1 != ibp)
1194 goto randomchar;
1196 /* This # can start a directive. */
1198 --obp; /* Don't copy the '#' */
1200 ip->bufp = ibp;
1201 op->bufp = obp;
1202 if (! handle_directive (ip, op)) {
1203 #ifdef USE_C_ALLOCA
1204 alloca (0);
1205 #endif
1206 /* Not a known directive: treat it as ordinary text.
1207 IP, OP, IBP, etc. have not been changed. */
1208 if (no_output && instack[indepth].fname) {
1209 /* If not generating expanded output,
1210 what we do with ordinary text is skip it.
1211 Discard everything until next # directive. */
1212 skip_if_group (&instack[indepth], 1);
1213 RECACHE;
1214 beg_of_line = ibp;
1215 break;
1217 ++obp; /* Copy the '#' after all */
1218 goto randomchar;
1220 #ifdef USE_C_ALLOCA
1221 alloca (0);
1222 #endif
1223 /* A # directive has been successfully processed. */
1224 /* If not generating expanded output, ignore everything until
1225 next # directive. */
1226 if (no_output && instack[indepth].fname)
1227 skip_if_group (&instack[indepth], 1);
1228 obp = op->bufp;
1229 RECACHE;
1230 beg_of_line = ibp;
1231 break;
1233 case '\"': /* skip quoted string */
1234 case '\'':
1235 /* A single quoted string is treated like a double -- some
1236 programs (e.g., troff) are perverse this way */
1238 if (ident_length)
1239 goto specialchar;
1241 start_line = ip->lineno;
1243 /* Skip ahead to a matching quote. */
1245 while (1) {
1246 if (ibp >= limit) {
1247 if (ip->macro != 0) {
1248 /* try harder: this string crosses a macro expansion boundary */
1249 POPMACRO;
1250 RECACHE;
1251 continue;
1253 break;
1255 *obp++ = *ibp;
1256 switch (*ibp++) {
1257 case '\n':
1258 ++ip->lineno;
1259 ++op->lineno;
1260 /* Traditionally, end of line ends a string constant with no error.
1261 So exit the loop and record the new line. */
1262 beg_of_line = ibp;
1263 goto while2end;
1265 case '\\':
1266 if (ibp >= limit)
1267 break;
1268 if (*ibp == '\n') {
1269 /* Backslash newline is replaced by nothing at all,
1270 but keep the line counts correct. */
1271 --obp;
1272 ++ibp;
1273 ++ip->lineno;
1274 } else {
1275 /* ANSI stupidly requires that in \\ the second \
1276 is *not* prevented from combining with a newline. */
1277 while (*ibp == '\\' && ibp[1] == '\n') {
1278 ibp += 2;
1279 ++ip->lineno;
1281 *obp++ = *ibp++;
1283 break;
1285 case '\"':
1286 case '\'':
1287 if (ibp[-1] == c)
1288 goto while2end;
1289 break;
1292 while2end:
1293 break;
1295 case '/':
1296 if (*ibp == '\\' && ibp[1] == '\n')
1297 newline_fix (ibp);
1298 /* Don't look for comments inside a macro definition. */
1299 if (ip->macro != 0)
1300 goto randomchar;
1301 /* A comment constitutes white space, so it can terminate an identifier.
1302 Process the identifier, if any. */
1303 if (ident_length)
1304 goto specialchar;
1306 if (*ibp != '*')
1307 goto randomchar;
1309 /* We have a comment. Skip it, optionally copying it to output. */
1311 start_line = ip->lineno;
1313 ++ibp; /* Skip the star. */
1315 /* In K+R C, a comment is equivalent to nothing. Note that we
1316 already output the slash; we might not want it. */
1317 if (! put_out_comments)
1318 obp--;
1319 else
1320 *obp++ = '*';
1323 U_CHAR *before_bp = ibp;
1325 while (ibp < limit) {
1326 switch (*ibp++) {
1327 case '/':
1328 if (warn_comments && ibp < limit && *ibp == '*')
1329 warning("`/*' within comment");
1330 break;
1331 case '*':
1332 if (*ibp == '\\' && ibp[1] == '\n')
1333 newline_fix (ibp);
1334 if (ibp >= limit || *ibp == '/')
1335 goto comment_end;
1336 break;
1337 case '\n':
1338 ++ip->lineno;
1339 /* Copy the newline into the output buffer, in order to
1340 avoid the pain of a #line every time a multiline comment
1341 is seen. */
1342 if (!put_out_comments)
1343 *obp++ = '\n';
1344 ++op->lineno;
1347 comment_end:
1349 if (ibp >= limit)
1350 error_with_line (line_for_error (start_line),
1351 "unterminated comment");
1352 else {
1353 ibp++;
1354 if (put_out_comments) {
1355 memcpy (obp, before_bp, ibp - before_bp);
1356 obp += ibp - before_bp;
1360 break;
1362 case '0': case '1': case '2': case '3': case '4':
1363 case '5': case '6': case '7': case '8': case '9':
1364 /* If digit is not part of identifier, it starts a number,
1365 which means that following letters are not an identifier.
1366 "0x5" does not refer to an identifier "x5".
1367 So copy all alphanumerics that follow without accumulating
1368 as an identifier. Periods also, for sake of "3.e7". */
1370 if (ident_length == 0) {
1371 while (ibp < limit) {
1372 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1373 ++ip->lineno;
1374 ibp += 2;
1376 c = *ibp++;
1377 if (!isalnum (c) && c != '.' && c != '_') {
1378 --ibp;
1379 break;
1381 *obp++ = c;
1382 /* A sign can be part of a preprocessing number
1383 if it follows an e. */
1384 if (c == 'e' || c == 'E') {
1385 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1386 ++ip->lineno;
1387 ibp += 2;
1389 if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
1390 *obp++ = *ibp++;
1391 /* Traditional C does not let the token go past the sign. */
1392 break;
1396 break;
1398 /* fall through */
1400 case '_':
1401 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1402 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1403 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1404 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1405 case 'y': case 'z':
1406 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1407 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1408 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1409 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1410 case 'Y': case 'Z':
1411 ident_length++;
1412 /* Compute step of hash function, to avoid a proc call on every token */
1413 hash = HASHSTEP (hash, c);
1414 break;
1416 case '\n':
1417 /* If reprocessing a macro expansion, newline is a special marker. */
1418 if (ip->macro != 0) {
1419 /* Newline White is a "funny space" to separate tokens that are
1420 supposed to be separate but without space between.
1421 Here White means any horizontal whitespace character.
1422 Newline - marks a recursive macro use that is not
1423 supposed to be expandable. */
1425 if (*ibp == '-') {
1426 /* Newline - inhibits expansion of preceding token.
1427 If expanding a macro arg, we keep the newline -.
1428 In final output, it is deleted. */
1429 if (! concatenated) {
1430 ident_length = 0;
1431 hash = 0;
1433 ibp++;
1434 if (!output_marks) {
1435 obp--;
1436 } else {
1437 /* If expanding a macro arg, keep the newline -. */
1438 *obp++ = '-';
1440 } else if (is_space[*ibp]) {
1441 /* Newline Space does not prevent expansion of preceding token
1442 so expand the preceding token and then come back. */
1443 if (ident_length > 0)
1444 goto specialchar;
1446 /* If generating final output, newline space makes a space. */
1447 if (!output_marks) {
1448 obp[-1] = *ibp++;
1449 /* And Newline Newline makes a newline, so count it. */
1450 if (obp[-1] == '\n')
1451 op->lineno++;
1452 } else {
1453 /* If expanding a macro arg, keep the newline space.
1454 If the arg gets stringified, newline space makes nothing. */
1455 *obp++ = *ibp++;
1457 } else abort (); /* Newline followed by something random? */
1458 break;
1461 /* If there is a pending identifier, handle it and come back here. */
1462 if (ident_length > 0)
1463 goto specialchar;
1465 beg_of_line = ibp;
1467 /* Update the line counts and output a #line if necessary. */
1468 ++ip->lineno;
1469 ++op->lineno;
1470 if (ip->lineno != op->lineno) {
1471 op->bufp = obp;
1472 output_line_command (ip, op, 1, same_file);
1473 check_expand (op, ip->length - (ip->bufp - ip->buf));
1474 obp = op->bufp;
1476 break;
1478 /* Come here either after (1) a null character that is part of the input
1479 or (2) at the end of the input, because there is a null there. */
1480 case 0:
1481 if (ibp <= limit)
1482 /* Our input really contains a null character. */
1483 goto randomchar;
1485 /* At end of a macro-expansion level, pop it and read next level. */
1486 if (ip->macro != 0) {
1487 obp--;
1488 ibp--;
1489 /* If we have an identifier that ends here, process it now, so
1490 we get the right error for recursion. */
1491 if (ident_length && ! is_idchar[*instack[indepth - 1].bufp]) {
1492 redo_char = 1;
1493 goto randomchar;
1495 POPMACRO;
1496 RECACHE;
1497 break;
1500 /* If we don't have a pending identifier,
1501 return at end of input. */
1502 if (ident_length == 0) {
1503 obp--;
1504 ibp--;
1505 op->bufp = obp;
1506 ip->bufp = ibp;
1507 goto ending;
1510 /* If we do have a pending identifier, just consider this null
1511 a special character and arrange to dispatch on it again.
1512 The second time, IDENT_LENGTH will be zero so we will return. */
1514 /* Fall through */
1516 specialchar:
1518 /* Handle the case of a character such as /, ', " or null
1519 seen following an identifier. Back over it so that
1520 after the identifier is processed the special char
1521 will be dispatched on again. */
1523 ibp--;
1524 obp--;
1525 redo_char = 1;
1527 default:
1529 randomchar:
1531 if (ident_length > 0) {
1532 register HASHNODE *hp;
1534 /* We have just seen an identifier end. If it's a macro, expand it.
1536 IDENT_LENGTH is the length of the identifier
1537 and HASH is its hash code.
1539 The identifier has already been copied to the output,
1540 so if it is a macro we must remove it.
1542 If REDO_CHAR is 0, the char that terminated the identifier
1543 has been skipped in the output and the input.
1544 OBP-IDENT_LENGTH-1 points to the identifier.
1545 If the identifier is a macro, we must back over the terminator.
1547 If REDO_CHAR is 1, the terminating char has already been
1548 backed over. OBP-IDENT_LENGTH points to the identifier. */
1550 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
1551 hp = hp->next) {
1553 if (hp->length == ident_length) {
1554 U_CHAR *obufp_before_macroname;
1555 int op_lineno_before_macroname;
1556 register int i = ident_length;
1557 register U_CHAR *p = hp->name;
1558 register U_CHAR *q = obp - i;
1560 if (! redo_char)
1561 q--;
1563 do { /* All this to avoid a strncmp () */
1564 if (*p++ != *q++)
1565 goto hashcollision;
1566 } while (--i);
1568 /* We found a use of a macro name.
1569 see if the context shows it is a macro call. */
1571 /* Back up over terminating character if not already done. */
1572 if (! redo_char) {
1573 ibp--;
1574 obp--;
1577 obufp_before_macroname = obp - ident_length;
1578 op_lineno_before_macroname = op->lineno;
1580 /* If macro wants an arglist, verify that a '(' follows.
1581 first skip all whitespace, copying it to the output
1582 after the macro name. Then, if there is no '(',
1583 decide this is not a macro call and leave things that way. */
1584 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1586 while (1) {
1587 /* Scan forward over whitespace, copying it to the output. */
1588 if (ibp == limit && ip->macro != 0) {
1589 POPMACRO;
1590 RECACHE;
1592 /* A comment: copy it unchanged or discard it. */
1593 else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
1594 if (put_out_comments) {
1595 *obp++ = '/';
1596 *obp++ = '*';
1598 ibp += 2;
1599 while (ibp + 1 != limit
1600 && !(ibp[0] == '*' && ibp[1] == '/')) {
1601 /* We need not worry about newline-marks,
1602 since they are never found in comments. */
1603 if (*ibp == '\n') {
1604 /* Newline in a file. Count it. */
1605 ++ip->lineno;
1606 ++op->lineno;
1608 if (put_out_comments)
1609 *obp++ = *ibp++;
1610 else
1611 ibp++;
1613 ibp += 2;
1614 if (put_out_comments) {
1615 *obp++ = '*';
1616 *obp++ = '/';
1619 else if (is_space[*ibp]) {
1620 *obp++ = *ibp++;
1621 if (ibp[-1] == '\n') {
1622 if (ip->macro == 0) {
1623 /* Newline in a file. Count it. */
1624 ++ip->lineno;
1625 ++op->lineno;
1626 } else if (!output_marks) {
1627 /* A newline mark, and we don't want marks
1628 in the output. If it is newline-hyphen,
1629 discard it entirely. Otherwise, it is
1630 newline-whitechar, so keep the whitechar. */
1631 obp--;
1632 if (*ibp == '-')
1633 ibp++;
1634 else {
1635 if (*ibp == '\n')
1636 ++op->lineno;
1637 *obp++ = *ibp++;
1639 } else {
1640 /* A newline mark; copy both chars to the output. */
1641 *obp++ = *ibp++;
1645 else break;
1647 if (*ibp != '(')
1648 break;
1651 /* This is now known to be a macro call.
1652 Discard the macro name from the output,
1653 along with any following whitespace just copied. */
1654 obp = obufp_before_macroname;
1655 op->lineno = op_lineno_before_macroname;
1657 /* Expand the macro, reading arguments as needed,
1658 and push the expansion on the input stack. */
1659 ip->bufp = ibp;
1660 op->bufp = obp;
1661 macroexpand (hp, op);
1663 /* Reexamine input stack, since macroexpand has pushed
1664 a new level on it. */
1665 obp = op->bufp;
1666 RECACHE;
1667 break;
1669 hashcollision:
1671 } /* End hash-table-search loop */
1672 ident_length = hash = 0; /* Stop collecting identifier */
1673 redo_char = 0;
1674 concatenated = 0;
1675 } /* End if (ident_length > 0) */
1676 } /* End switch */
1677 } /* End per-char loop */
1679 /* Come here to return -- but first give an error message
1680 if there was an unterminated successful conditional. */
1681 ending:
1682 if (if_stack != ip->if_stack) {
1683 const char *str;
1684 switch (if_stack->type) {
1685 case T_IF:
1686 str = "if";
1687 break;
1688 case T_IFDEF:
1689 str = "ifdef";
1690 break;
1691 case T_IFNDEF:
1692 str = "ifndef";
1693 break;
1694 case T_ELSE:
1695 str = "else";
1696 break;
1697 case T_ELIF:
1698 str = "elif";
1699 break;
1700 default:
1701 abort ();
1703 error_with_line (line_for_error (if_stack->lineno),
1704 "unterminated #%s conditional", str);
1706 if_stack = ip->if_stack;
1710 * Rescan a string into a temporary buffer and return the result
1711 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1713 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1714 * and insert such markers when appropriate. See `rescan' for details.
1715 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1716 * before substitution; it is 0 for other uses.
1718 FILE_BUF
1719 expand_to_temp_buffer (buf, limit, output_marks)
1720 U_CHAR *buf, *limit;
1721 int output_marks;
1723 register FILE_BUF *ip;
1724 FILE_BUF obuf;
1725 int length = limit - buf;
1726 U_CHAR *buf1;
1727 int odepth = indepth;
1729 if (length < 0)
1730 abort ();
1732 /* Set up the input on the input stack. */
1734 buf1 = (U_CHAR *) alloca (length + 1);
1736 register U_CHAR *p1 = buf;
1737 register U_CHAR *p2 = buf1;
1739 while (p1 != limit)
1740 *p2++ = *p1++;
1742 buf1[length] = 0;
1744 /* Set up to receive the output. */
1746 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
1747 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
1748 obuf.fname = 0;
1749 obuf.macro = 0;
1750 obuf.free_ptr = 0;
1752 CHECK_DEPTH ({return obuf;});
1754 ++indepth;
1756 ip = &instack[indepth];
1757 ip->fname = 0;
1758 ip->macro = 0;
1759 ip->free_ptr = 0;
1760 ip->length = length;
1761 ip->buf = ip->bufp = buf1;
1762 ip->if_stack = if_stack;
1764 ip->lineno = obuf.lineno = 1;
1766 /* Scan the input, create the output. */
1768 rescan (&obuf, output_marks);
1770 /* Pop input stack to original state. */
1771 --indepth;
1773 if (indepth != odepth)
1774 abort ();
1776 /* Record the output. */
1777 obuf.length = obuf.bufp - obuf.buf;
1779 return obuf;
1783 * Process a # directive. Expects IP->bufp to point to the '#', as in
1784 * `#define foo bar'. Passes to the command handler
1785 * (do_define, do_include, etc.): the addresses of the 1st and
1786 * last chars of the command (starting immediately after the #
1787 * keyword), plus op and the keyword table pointer. If the command
1788 * contains comments it is copied into a temporary buffer sans comments
1789 * and the temporary buffer is passed to the command handler instead.
1790 * Likewise for backslash-newlines.
1792 * Returns nonzero if this was a known # directive.
1793 * Otherwise, returns zero, without advancing the input pointer.
1797 handle_directive (ip, op)
1798 FILE_BUF *ip, *op;
1800 register U_CHAR *bp, *cp;
1801 register struct directive *kt;
1802 register int ident_length;
1803 U_CHAR *resume_p;
1805 /* Nonzero means we must copy the entire command
1806 to get rid of comments or backslash-newlines. */
1807 int copy_command = 0;
1809 U_CHAR *ident, *after_ident;
1811 bp = ip->bufp;
1812 /* Skip whitespace and \-newline. */
1813 while (1) {
1814 if (is_hor_space[*bp])
1815 bp++;
1816 else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
1817 ip->bufp = bp;
1818 skip_to_end_of_comment (ip, &ip->lineno);
1819 bp = ip->bufp;
1820 } else if (*bp == '\\' && bp[1] == '\n') {
1821 bp += 2; ip->lineno++;
1822 } else break;
1825 /* Now find end of directive name.
1826 If we encounter a backslash-newline, exchange it with any following
1827 symbol-constituents so that we end up with a contiguous name. */
1829 cp = bp;
1830 while (1) {
1831 if (is_idchar[*cp])
1832 cp++;
1833 else {
1834 if (*cp == '\\' && cp[1] == '\n')
1835 name_newline_fix (cp);
1836 if (is_idchar[*cp])
1837 cp++;
1838 else break;
1841 ident_length = cp - bp;
1842 ident = bp;
1843 after_ident = cp;
1845 /* A line of just `#' becomes blank. */
1847 if (ident_length == 0 && *after_ident == '\n') {
1848 ip->bufp = after_ident;
1849 return 1;
1853 * Decode the keyword and call the appropriate expansion
1854 * routine, after moving the input pointer up to the next line.
1856 for (kt = directive_table; kt->length > 0; kt++) {
1857 if (kt->length == ident_length
1858 && !strncmp (kt->name, (char *)ident, ident_length)) {
1859 register U_CHAR *buf;
1860 register U_CHAR *limit = ip->buf + ip->length;
1861 int unterminated = 0;
1863 /* Nonzero means do not delete comments within the directive.
1864 #define needs this to detect traditional token paste. */
1865 int keep_comments = kt->type == T_DEFINE;
1867 /* Find the end of this command (first newline not backslashed
1868 and not in a string or comment).
1869 Set COPY_COMMAND if the command must be copied
1870 (it contains a backslash-newline or a comment). */
1872 buf = bp = after_ident;
1873 while (bp < limit) {
1874 register U_CHAR c = *bp++;
1875 switch (c) {
1876 case '\\':
1877 if (bp < limit) {
1878 if (*bp == '\n') {
1879 ip->lineno++;
1880 copy_command = 1;
1882 bp++;
1884 break;
1886 case '\'':
1887 case '\"':
1888 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command, &unterminated);
1889 if (unterminated) {
1890 /* Traditional preprocessing permits unterminated strings. */
1891 ip->bufp = bp;
1892 goto endloop1;
1894 break;
1896 /* <...> is special for #include. */
1897 case '<':
1898 if (kt->type != T_INCLUDE)
1899 break;
1900 while (*bp && *bp != '>') bp++;
1901 break;
1903 case '/':
1904 if (*bp == '\\' && bp[1] == '\n')
1905 newline_fix (bp);
1906 if (*bp == '*') {
1907 U_CHAR *obp = bp - 1;
1908 ip->bufp = bp + 1;
1909 skip_to_end_of_comment (ip, &ip->lineno);
1910 bp = ip->bufp;
1911 /* No need to copy the command because of a comment at the end;
1912 just don't include the comment in the directive. */
1913 if (bp == limit || *bp == '\n') {
1914 bp = obp;
1915 goto endloop1;
1917 /* Don't remove the comments if this is #define. */
1918 if (! keep_comments)
1919 copy_command++;
1921 break;
1923 case '\n':
1924 --bp; /* Point to the newline */
1925 ip->bufp = bp;
1926 goto endloop1;
1929 ip->bufp = bp;
1931 endloop1:
1932 resume_p = ip->bufp;
1933 /* BP is the end of the directive.
1934 RESUME_P is the next interesting data after the directive.
1935 A comment may come between. */
1937 if (copy_command) {
1938 register U_CHAR *xp = buf;
1939 /* Need to copy entire command into temp buffer before dispatching */
1941 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
1942 some slop */
1943 buf = cp;
1945 /* Copy to the new buffer, deleting comments
1946 and backslash-newlines (and whitespace surrounding the latter). */
1948 while (xp < bp) {
1949 register U_CHAR c = *xp++;
1950 *cp++ = c;
1952 switch (c) {
1953 case '\n':
1954 break;
1956 /* <...> is special for #include. */
1957 case '<':
1958 if (kt->type != T_INCLUDE)
1959 break;
1960 while (xp < bp && c != '>') {
1961 c = *xp++;
1962 if (c == '\\' && xp < bp && *xp == '\n')
1963 xp++, ip->lineno++;
1964 else
1965 *cp++ = c;
1967 break;
1969 case '\\':
1970 if (*xp == '\n') {
1971 xp++;
1972 cp--;
1973 if (cp != buf && is_space[cp[-1]]) {
1974 while (cp != buf && is_space[cp[-1]]) cp--;
1975 cp++;
1976 SKIP_WHITE_SPACE (xp);
1977 } else if (is_space[*xp]) {
1978 *cp++ = *xp++;
1979 SKIP_WHITE_SPACE (xp);
1981 } else {
1982 *cp++ = *xp++;
1984 break;
1986 case '\'':
1987 case '\"':
1989 register U_CHAR *bp1
1990 = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
1991 while (xp != bp1)
1992 *cp++ = *xp++;
1994 break;
1996 case '/':
1997 if (*xp == '*') {
1998 ip->bufp = xp + 1;
1999 skip_to_end_of_comment (ip, 0);
2000 if (keep_comments)
2001 while (xp != ip->bufp)
2002 *cp++ = *xp++;
2003 /* Delete the slash. */
2004 else
2005 cp--;
2006 xp = ip->bufp;
2011 /* Null-terminate the copy. */
2013 *cp = 0;
2015 else
2016 cp = bp;
2018 ip->bufp = resume_p;
2020 /* Call the appropriate command handler. buf now points to
2021 either the appropriate place in the input buffer, or to
2022 the temp buffer if it was necessary to make one. cp
2023 points to the first char after the contents of the (possibly
2024 copied) command, in either case. */
2025 (*kt->func) (buf, cp, op, kt);
2026 check_expand (op, ip->length - (ip->bufp - ip->buf));
2028 return 1;
2032 return 0;
2035 static const char *const
2036 monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2037 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2040 * expand things like __FILE__. Place the expansion into the output
2041 * buffer *without* rescanning.
2043 void
2044 special_symbol (hp, op)
2045 HASHNODE *hp;
2046 FILE_BUF *op;
2048 char *buf = 0;
2049 time_t t;
2050 int i, len;
2051 int true_indepth;
2052 FILE_BUF *ip = NULL;
2053 static struct tm *timebuf = NULL;
2055 int paren = 0; /* For special `defined' keyword */
2057 for (i = indepth; i >= 0; i--)
2058 if (instack[i].fname != NULL) {
2059 ip = &instack[i];
2060 break;
2062 if (ip == NULL)
2063 fatal ("not in any file?!");
2065 switch (hp->type) {
2066 case T_FILE:
2067 case T_BASE_FILE:
2069 const char *string;
2070 if (hp->type == T_FILE)
2071 string = ip->fname;
2072 else
2073 string = instack[0].fname;
2075 if (string)
2077 buf = (char *) alloca (3 + strlen (string));
2078 sprintf (buf, "\"%s\"", string);
2080 else
2081 strcpy (buf, "\"\"");
2083 break;
2086 case T_INCLUDE_LEVEL:
2087 true_indepth = 0;
2088 for (i = indepth; i >= 0; i--)
2089 if (instack[i].fname != NULL)
2090 true_indepth++;
2092 buf = (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2093 sprintf (buf, "%d", true_indepth - 1);
2094 break;
2096 case T_VERSION:
2097 buf = (char *) alloca (3 + strlen (version_string));
2098 sprintf (buf, "\"%s\"", version_string);
2099 break;
2101 case T_CONST:
2102 buf = (char *) hp->value.cpval;
2103 break;
2105 case T_SPECLINE:
2106 buf = (char *) alloca (10);
2107 sprintf (buf, "%d", ip->lineno);
2108 break;
2110 case T_DATE:
2111 case T_TIME:
2112 if (timebuf == NULL) {
2113 t = time (0);
2114 timebuf = localtime (&t);
2116 buf = (char *) alloca (20);
2117 if (hp->type == T_DATE)
2118 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2119 timebuf->tm_mday, timebuf->tm_year + 1900);
2120 else
2121 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2122 timebuf->tm_sec);
2123 break;
2125 case T_SPEC_DEFINED:
2126 buf = (char *) " 0 "; /* Assume symbol is not defined */
2127 ip = &instack[indepth];
2128 SKIP_WHITE_SPACE (ip->bufp);
2129 if (*ip->bufp == '(') {
2130 paren++;
2131 ip->bufp++; /* Skip over the paren */
2132 SKIP_WHITE_SPACE (ip->bufp);
2135 if (!is_idstart[*ip->bufp])
2136 goto oops;
2137 if (lookup (ip->bufp, -1, -1))
2138 buf = (char *) " 1 ";
2139 while (is_idchar[*ip->bufp])
2140 ++ip->bufp;
2141 SKIP_WHITE_SPACE (ip->bufp);
2142 if (paren) {
2143 if (*ip->bufp != ')')
2144 goto oops;
2145 ++ip->bufp;
2147 break;
2149 oops:
2151 error ("`defined' must be followed by ident or (ident)");
2152 break;
2154 default:
2155 error ("cccp error: invalid special hash type"); /* time for gdb */
2156 abort ();
2158 len = strlen (buf);
2159 check_expand (op, len);
2160 memcpy (op->bufp, buf, len);
2161 op->bufp += len;
2165 /* Routines to handle #directives */
2168 * Process include file by reading it in and calling rescan.
2169 * Expects to see "fname" or <fname> on the input.
2171 void
2172 do_include (buf, limit, op, keyword)
2173 U_CHAR *buf, *limit;
2174 FILE_BUF *op;
2175 struct directive *keyword ATTRIBUTE_UNUSED;
2177 char *fname; /* Dynamically allocated fname buffer */
2178 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
2180 struct file_name_list *stackp = include; /* Chain of dirs to search */
2181 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
2182 int flen;
2184 int f; /* file number */
2186 int retried = 0; /* Have already tried macro
2187 expanding the include line*/
2188 FILE_BUF trybuf; /* It got expanded into here */
2189 int system_header_p = 0; /* 0 for "...", 1 for <...> */
2191 f= -1; /* JF we iz paranoid! */
2193 get_filename:
2195 fbeg = buf;
2196 SKIP_WHITE_SPACE (fbeg);
2197 /* Discard trailing whitespace so we can easily see
2198 if we have parsed all the significant chars we were given. */
2199 while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
2201 switch (*fbeg++) {
2202 case '\"':
2203 fend = fbeg;
2204 while (fend != limit && *fend != '\"')
2205 fend++;
2206 if (*fend == '\"' && fend + 1 == limit) {
2207 FILE_BUF *fp;
2209 /* We have "filename". Figure out directory this source
2210 file is coming from and put it on the front of the list. */
2212 /* If -I- was specified, don't search current dir, only spec'd ones. */
2213 if (ignore_srcdir) break;
2215 for (fp = &instack[indepth]; fp >= instack; fp--)
2217 size_t n;
2218 const char *ep, *nam;
2220 if ((nam = fp->fname) != NULL) {
2221 /* Found a named file. Figure out dir of the file,
2222 and put it in front of the search list. */
2223 dsp[0].next = stackp;
2224 stackp = dsp;
2225 ep = strrchr (nam, '/');
2226 if (ep != NULL) {
2227 char *f;
2228 n = ep - nam;
2229 f = (char *) alloca (n + 1);
2230 strncpy (f, nam, n);
2231 f[n] = '\0';
2232 dsp[0].fname = f;
2233 if (n > max_include_len) max_include_len = n;
2234 } else {
2235 dsp[0].fname = 0; /* Current directory */
2237 break;
2240 break;
2242 goto fail;
2244 case '<':
2245 fend = fbeg;
2246 while (fend != limit && *fend != '>') fend++;
2247 if (*fend == '>' && fend + 1 == limit) {
2248 system_header_p = 1;
2249 /* If -I-, start with the first -I dir after the -I-. */
2250 if (first_bracket_include)
2251 stackp = first_bracket_include;
2252 break;
2254 goto fail;
2256 default:
2257 fail:
2258 if (retried) {
2259 error ("#include expects \"fname\" or <fname>");
2260 return;
2261 } else {
2262 trybuf = expand_to_temp_buffer (buf, limit, 0);
2263 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
2264 memcpy (buf, trybuf.buf, trybuf.bufp - trybuf.buf);
2265 limit = buf + (trybuf.bufp - trybuf.buf);
2266 free (trybuf.buf);
2267 retried++;
2268 goto get_filename;
2272 flen = fend - fbeg;
2273 fname = (char *) alloca (max_include_len + flen + 2);
2274 /* + 2 above for slash and terminating null. */
2276 /* If specified file name is absolute, just open it. */
2278 if (*fbeg == '/') {
2279 strncpy (fname, (char *)fbeg, flen);
2280 fname[flen] = 0;
2281 f = open (fname, O_RDONLY, 0666);
2282 } else {
2283 /* Search directory path, trying to open the file.
2284 Copy each filename tried into FNAME. */
2286 for (; stackp; stackp = stackp->next) {
2287 if (stackp->fname) {
2288 strcpy (fname, stackp->fname);
2289 strcat (fname, "/");
2290 fname[strlen (fname) + flen] = 0;
2291 } else {
2292 fname[0] = 0;
2294 strncat (fname, (char *)fbeg, flen);
2295 if ((f = open (fname, O_RDONLY, 0666)) >= 0)
2296 break;
2300 if (f < 0) {
2301 strncpy (fname, (char *)fbeg, flen);
2302 fname[flen] = 0;
2303 error_from_errno (fname);
2305 /* For -M, add this file to the dependencies. */
2306 if (print_deps > (system_header_p || (system_include_depth > 0))) {
2307 if (system_header_p)
2308 warning ("nonexistent file <%.*s> omitted from dependency output",
2309 fend - fbeg, fbeg);
2310 else
2312 deps_output ((const char *)fbeg, fend - fbeg);
2313 deps_output (" ", 0);
2316 } else {
2318 /* Check to see if this include file is a once-only include file.
2319 If so, give up. */
2321 struct file_name_list* ptr;
2323 for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
2324 if (!strcmp (ptr->fname, fname)) {
2325 close (f);
2326 return; /* This file was once'd. */
2330 for (ptr = all_include_files; ptr; ptr = ptr->next) {
2331 if (!strcmp (ptr->fname, fname))
2332 break; /* This file was included before. */
2335 if (ptr == 0) {
2336 /* This is the first time for this file. */
2337 /* Add it to list of files included. */
2339 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
2340 ptr->next = all_include_files;
2341 all_include_files = ptr;
2342 ptr->fname = xstrdup (fname);
2344 /* For -M, add this file to the dependencies. */
2345 if (print_deps > (system_header_p || (system_include_depth > 0))) {
2346 deps_output (fname, strlen (fname));
2347 deps_output (" ", 0);
2351 if (system_header_p)
2352 system_include_depth++;
2354 /* Actually process the file. */
2355 finclude (f, fname, op);
2357 if (system_header_p)
2358 system_include_depth--;
2360 close (f);
2364 /* Process the contents of include file FNAME, already open on descriptor F,
2365 with output to OP. */
2367 void
2368 finclude (f, fname, op)
2369 int f;
2370 const char *fname;
2371 FILE_BUF *op;
2373 int st_mode;
2374 long st_size;
2375 long i;
2376 FILE_BUF *fp; /* For input stack frame */
2378 CHECK_DEPTH (return;);
2380 if (file_size_and_mode (f, &st_mode, &st_size))
2381 goto nope;
2383 fp = &instack[indepth + 1];
2384 memset (fp, 0, sizeof (FILE_BUF));
2385 fp->fname = fname;
2386 fp->length = 0;
2387 fp->lineno = 1;
2388 fp->if_stack = if_stack;
2390 if (S_ISREG (st_mode)) {
2391 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
2392 fp->bufp = fp->buf;
2394 /* Read the file contents, knowing that st_size is an upper bound
2395 on the number of bytes we can read. */
2396 while (st_size > 0) {
2397 i = read (f, fp->buf + fp->length, st_size);
2398 if (i <= 0) {
2399 if (i == 0) break;
2400 goto nope;
2402 fp->length += i;
2403 st_size -= i;
2406 else {
2407 /* Cannot count its file size before reading. */
2409 U_CHAR *bufp;
2410 U_CHAR *basep;
2411 int bsize = 2000;
2413 st_size = 0;
2414 basep = (U_CHAR *) xmalloc (bsize + 2);
2415 bufp = basep;
2417 for (;;) {
2418 i = read (f, bufp, bsize - st_size);
2419 if (i < 0)
2420 goto nope; /* error! */
2421 if (i == 0)
2422 break; /* End of file */
2423 st_size += i;
2424 bufp += i;
2425 if (bsize == st_size) { /* Buffer is full! */
2426 bsize *= 2;
2427 basep = (U_CHAR *) xrealloc (basep, bsize + 2);
2428 bufp = basep + st_size; /* May have moved */
2431 fp->buf = basep;
2432 fp->bufp = fp->buf;
2433 fp->length = st_size;
2435 close (f);
2437 /* Make sure data ends with a newline. And put a null after it. */
2439 if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
2440 fp->buf[fp->length++] = '\n';
2441 fp->buf[fp->length] = '\0';
2443 indepth++;
2444 output_line_command (fp, op, 0, enter_file);
2445 rescan (op, 0);
2446 indepth--;
2447 output_line_command (&instack[indepth], op, 0, leave_file);
2448 free (fp->buf);
2449 return;
2451 nope:
2452 perror_with_name (fname);
2453 close (f);
2457 /* Process a #define command.
2458 BUF points to the contents of the #define command, as a continguous string.
2459 LIMIT points to the first character past the end of the definition.
2460 KEYWORD is the keyword-table entry for #define. */
2462 void
2463 do_define (buf, limit, op, keyword)
2464 U_CHAR *buf, *limit;
2465 FILE_BUF *op ATTRIBUTE_UNUSED;
2466 struct directive *keyword ATTRIBUTE_UNUSED;
2468 U_CHAR *bp; /* temp ptr into input buffer */
2469 U_CHAR *symname; /* remember where symbol name starts */
2470 int sym_length; /* and how long it is */
2472 DEFINITION *defn;
2473 int arglengths = 0; /* Accumulate lengths of arg names
2474 plus number of args. */
2475 int hashcode;
2477 bp = buf;
2479 while (is_hor_space[*bp])
2480 bp++;
2482 symname = bp; /* remember where it starts */
2483 while (is_idchar[*bp] && bp < limit) {
2484 bp++;
2486 sym_length = bp - symname;
2487 if (sym_length == 0)
2488 error ("invalid macro name");
2489 else if (!is_idstart[*symname]) {
2490 U_CHAR *msg; /* what pain... */
2491 msg = (U_CHAR *) alloca (sym_length + 1);
2492 memcpy (msg, symname, sym_length);
2493 msg[sym_length] = 0;
2494 error ("invalid macro name `%s'", msg);
2495 } else {
2496 if (! strncmp ((char *)symname, "defined", 7) && sym_length == 7)
2497 error ("defining `defined' as a macro");
2500 /* lossage will occur if identifiers or control keywords are broken
2501 across lines using backslash. This is not the right place to take
2502 care of that. */
2504 if (*bp == '(') {
2505 struct arglist *arg_ptrs = NULL;
2506 int argno = 0;
2508 bp++; /* skip '(' */
2509 SKIP_WHITE_SPACE (bp);
2511 /* Loop over macro argument names. */
2512 while (*bp != ')') {
2513 struct arglist *temp;
2515 temp = (struct arglist *) alloca (sizeof (struct arglist));
2516 temp->name = bp;
2517 temp->next = arg_ptrs;
2518 temp->argno = argno++;
2519 arg_ptrs = temp;
2521 if (!is_idstart[*bp])
2522 warning ("parameter name starts with a digit in #define");
2524 /* Find the end of the arg name. */
2525 while (is_idchar[*bp]) {
2526 bp++;
2528 temp->length = bp - temp->name;
2529 arglengths += temp->length + 2;
2530 SKIP_WHITE_SPACE (bp);
2531 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2532 error ("badly punctuated parameter list in #define");
2533 return;
2535 if (*bp == ',') {
2536 bp++;
2537 SKIP_WHITE_SPACE (bp);
2539 if (bp >= limit) {
2540 error ("unterminated parameter list in #define");
2541 return;
2545 ++bp; /* skip paren */
2546 while (is_hor_space[*bp]) /* and leading whitespace */
2547 ++bp;
2548 /* now everything from bp before limit is the definition. */
2549 defn = collect_expansion (bp, limit, argno, arg_ptrs);
2551 /* Now set defn->argnames to the result of concatenating
2552 the argument names in reverse order
2553 with comma-space between them. */
2554 defn->argnames = (U_CHAR *) xmalloc (arglengths + 1);
2556 struct arglist *temp;
2557 int i = 0;
2558 for (temp = arg_ptrs; temp; temp = temp->next) {
2559 memcpy (&defn->argnames[i], temp->name, temp->length);
2560 i += temp->length;
2561 if (temp->next != 0) {
2562 defn->argnames[i++] = ',';
2563 defn->argnames[i++] = ' ';
2566 defn->argnames[i] = 0;
2568 } else {
2569 /* simple expansion or empty definition; skip leading whitespace */
2570 while (is_hor_space[*bp])
2571 ++bp;
2572 /* now everything from bp before limit is the definition. */
2573 defn = collect_expansion (bp, limit, -1, 0);
2574 defn->argnames = (U_CHAR *) "";
2577 hashcode = hashf (symname, sym_length, HASHSIZE);
2580 HASHNODE *hp;
2581 if ((hp = lookup (symname, sym_length, hashcode)) == NULL)
2582 hp = install (symname, sym_length, T_MACRO, hashcode);
2583 else {
2584 if (hp->type != T_MACRO || compare_defs (defn, hp->value.defn))
2585 warning ("\"%.*s\" redefined", sym_length, symname);
2587 /* Replace the old definition. */
2588 hp->type = T_MACRO;
2591 hp->value.defn = defn;
2596 * return zero if two DEFINITIONs are isomorphic
2599 compare_defs (d1, d2)
2600 DEFINITION *d1, *d2;
2602 register struct reflist *a1, *a2;
2603 register U_CHAR *p1 = d1->expansion;
2604 register U_CHAR *p2 = d2->expansion;
2605 int first = 1;
2607 if (d1->nargs != d2->nargs)
2608 return 1;
2609 if (strcmp ((char *)d1->argnames, (char *)d2->argnames))
2610 return 1;
2611 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
2612 a1 = a1->next, a2 = a2->next) {
2613 if (!((a1->nchars == a2->nchars
2614 && ! strncmp ((char *)p1, (char *)p2, a1->nchars))
2615 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2616 || a1->argno != a2->argno
2617 || a1->stringify != a2->stringify
2618 || a1->raw_before != a2->raw_before
2619 || a1->raw_after != a2->raw_after)
2620 return 1;
2621 first = 0;
2622 p1 += a1->nchars;
2623 p2 += a2->nchars;
2625 if (a1 != a2)
2626 return 1;
2627 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2628 p2, d2->length - (p2 - d2->expansion), 1))
2629 return 1;
2630 return 0;
2633 /* Return 1 if two parts of two macro definitions are effectively different.
2634 One of the parts starts at BEG1 and has LEN1 chars;
2635 the other has LEN2 chars at BEG2.
2636 Any sequence of whitespace matches any other sequence of whitespace.
2637 FIRST means these parts are the first of a macro definition;
2638 so ignore leading whitespace entirely.
2639 LAST means these parts are the last of a macro definition;
2640 so ignore trailing whitespace entirely. */
2642 comp_def_part (first, beg1, len1, beg2, len2, last)
2643 int first;
2644 U_CHAR *beg1, *beg2;
2645 int len1, len2;
2646 int last;
2648 register U_CHAR *end1 = beg1 + len1;
2649 register U_CHAR *end2 = beg2 + len2;
2650 if (first) {
2651 while (beg1 != end1 && is_space[*beg1]) beg1++;
2652 while (beg2 != end2 && is_space[*beg2]) beg2++;
2654 if (last) {
2655 while (beg1 != end1 && is_space[end1[-1]]) end1--;
2656 while (beg2 != end2 && is_space[end2[-1]]) end2--;
2658 while (beg1 != end1 && beg2 != end2) {
2659 if (is_space[*beg1] && is_space[*beg2]) {
2660 while (beg1 != end1 && is_space[*beg1]) beg1++;
2661 while (beg2 != end2 && is_space[*beg2]) beg2++;
2662 } else if (*beg1 == *beg2) {
2663 beg1++; beg2++;
2664 } else break;
2666 return (beg1 != end1) || (beg2 != end2);
2669 /* Read a replacement list for a macro with parameters.
2670 Build the DEFINITION structure.
2671 Reads characters of text starting at BUF until LIMIT.
2672 ARGLIST specifies the formal parameters to look for
2673 in the text of the definition; NARGS is the number of args
2674 in that list, or -1 for a macro name that wants no argument list.
2675 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2676 and NAMELEN is its length in characters.
2678 Note that comments and backslash-newlines have already been deleted
2679 from the argument. */
2681 /* Leading and trailing Space, Tab, etc. are converted to markers
2682 Newline Space, Newline Tab, etc.
2683 Newline Space makes a space in the final output
2684 but is discarded if stringified. (Newline Tab is similar but
2685 makes a Tab instead.)
2687 If there is no trailing whitespace, a Newline Space is added at the end
2688 to prevent concatenation that would be contrary to the standard. */
2690 DEFINITION *
2691 collect_expansion (buf, end, nargs, arglist)
2692 U_CHAR *buf, *end;
2693 int nargs;
2694 struct arglist *arglist;
2696 DEFINITION *defn;
2697 register U_CHAR *p, *limit, *lastp, *exp_p;
2698 struct reflist *endpat = NULL;
2699 /* Pointer to first nonspace after last ## seen. */
2700 U_CHAR *concat = 0;
2701 /* Pointer to first nonspace after last single-# seen. */
2702 U_CHAR *stringify = 0;
2703 int maxsize;
2704 int expected_delimiter = '\0';
2706 /* Scan thru the replacement list, ignoring comments and quoted
2707 strings, picking up on the macro calls. It does a linear search
2708 thru the arg list on every potential symbol. Profiling might say
2709 that something smarter should happen. */
2711 if (end < buf)
2712 abort ();
2714 /* Find the beginning of the trailing whitespace. */
2715 /* Find end of leading whitespace. */
2716 limit = end;
2717 p = buf;
2718 while (p < limit && is_space[limit[-1]]) limit--;
2719 while (p < limit && is_space[*p]) p++;
2721 /* Allocate space for the text in the macro definition.
2722 Leading and trailing whitespace chars need 2 bytes each.
2723 Each other input char may or may not need 1 byte,
2724 so this is an upper bound.
2725 The extra 2 are for invented trailing newline-marker and final null. */
2726 maxsize = (sizeof (DEFINITION)
2727 + 2 * (end - limit) + 2 * (p - buf)
2728 + (limit - p) + 3);
2729 defn = (DEFINITION *) xcalloc (1, maxsize);
2731 defn->nargs = nargs;
2732 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
2733 lastp = exp_p;
2735 p = buf;
2737 /* Convert leading whitespace to Newline-markers. */
2738 while (p < limit && is_space[*p]) {
2739 *exp_p++ = '\n';
2740 *exp_p++ = *p++;
2743 /* Process the main body of the definition. */
2744 while (p < limit) {
2745 int skipped_arg = 0;
2746 register U_CHAR c = *p++;
2748 *exp_p++ = c;
2750 /* In -traditional mode, recognize arguments inside strings and
2751 and character constants, and ignore special properties of #.
2752 Arguments inside strings are considered "stringified", but no
2753 extra quote marks are supplied. */
2754 switch (c) {
2755 case '\'':
2756 case '\"':
2757 if (expected_delimiter != '\0') {
2758 if (c == expected_delimiter)
2759 expected_delimiter = '\0';
2760 } else
2761 expected_delimiter = c;
2762 break;
2764 case '\\':
2765 /* Backslash quotes delimiters and itself, but not macro args. */
2766 if (expected_delimiter != 0 && p < limit
2767 && (*p == expected_delimiter || *p == '\\')) {
2768 *exp_p++ = *p++;
2769 continue;
2771 break;
2773 case '/':
2774 if (expected_delimiter != '\0') /* No comments inside strings. */
2775 break;
2776 if (*p == '*') {
2777 /* If we find a comment that wasn't removed by handle_directive,
2778 this must be -traditional. So replace the comment with
2779 nothing at all. */
2780 exp_p--;
2781 p += 1;
2782 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
2783 p++;
2785 break;
2788 if (is_idchar[c] && nargs > 0) {
2789 U_CHAR *id_beg = p - 1;
2790 int id_len;
2792 --exp_p;
2793 while (p != limit && is_idchar[*p]) p++;
2794 id_len = p - id_beg;
2796 if (is_idstart[c]) {
2797 register struct arglist *arg;
2799 for (arg = arglist; arg != NULL; arg = arg->next) {
2800 struct reflist *tpat;
2802 if (arg->name[0] == c
2803 && arg->length == id_len
2804 && strncmp ((char *)arg->name, (char *)id_beg, id_len) == 0) {
2805 /* make a pat node for this arg and append it to the end of
2806 the pat list */
2807 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
2808 tpat->next = NULL;
2809 tpat->raw_before = concat == id_beg;
2810 tpat->raw_after = 0;
2811 tpat->stringify = expected_delimiter != '\0';
2813 if (endpat == NULL)
2814 defn->pattern = tpat;
2815 else
2816 endpat->next = tpat;
2817 endpat = tpat;
2819 tpat->argno = arg->argno;
2820 tpat->nchars = exp_p - lastp;
2822 register U_CHAR *p1 = p;
2823 SKIP_WHITE_SPACE (p1);
2824 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2825 tpat->raw_after = 1;
2827 lastp = exp_p; /* place to start copying from next time */
2828 skipped_arg = 1;
2829 break;
2834 /* If this was not a macro arg, copy it into the expansion. */
2835 if (! skipped_arg) {
2836 register U_CHAR *lim1 = p;
2837 p = id_beg;
2838 while (p != lim1)
2839 *exp_p++ = *p++;
2840 if (stringify == id_beg)
2841 error ("# operator should be followed by a macro argument name");
2846 if (limit < end) {
2847 /* Convert trailing whitespace to Newline-markers. */
2848 while (limit < end && is_space[*limit]) {
2849 *exp_p++ = '\n';
2850 *exp_p++ = *limit++;
2853 *exp_p = '\0';
2855 defn->length = exp_p - defn->expansion;
2857 /* Crash now if we overrun the allocated size. */
2858 if (defn->length + 1 > maxsize)
2859 abort ();
2861 return defn;
2865 * interpret #line command. Remembers previously seen fnames
2866 * in its very own hash table.
2868 #define FNAME_HASHSIZE 37
2869 void
2870 do_line (buf, limit, op, keyword)
2871 U_CHAR *buf, *limit;
2872 FILE_BUF *op;
2873 struct directive *keyword ATTRIBUTE_UNUSED;
2875 register U_CHAR *bp;
2876 FILE_BUF *ip = &instack[indepth];
2877 FILE_BUF tem;
2878 int new_lineno;
2879 enum file_change_code file_change = same_file;
2881 /* Expand any macros. */
2882 tem = expand_to_temp_buffer (buf, limit, 0);
2884 /* Point to macroexpanded line, which is null-terminated now. */
2885 bp = tem.buf;
2886 SKIP_WHITE_SPACE (bp);
2888 if (!isdigit (*bp)) {
2889 error ("invalid format #line command");
2890 return;
2893 /* The Newline at the end of this line remains to be processed.
2894 To put the next line at the specified line number,
2895 we must store a line number now that is one less. */
2896 new_lineno = atoi ((char *)bp) - 1;
2898 /* skip over the line number. */
2899 while (isdigit (*bp))
2900 bp++;
2902 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
2903 if (*bp && !is_space[*bp]) {
2904 error ("invalid format #line command");
2905 return;
2907 #endif
2909 SKIP_WHITE_SPACE (bp);
2911 if (*bp == '\"') {
2912 static HASHNODE *fname_table[FNAME_HASHSIZE];
2913 HASHNODE *hp, **hash_bucket;
2914 U_CHAR *fname;
2915 int fname_length;
2917 fname = ++bp;
2919 while (*bp && *bp != '\"')
2920 bp++;
2921 if (*bp != '\"') {
2922 error ("invalid format #line command");
2923 return;
2926 fname_length = bp - fname;
2928 bp++;
2929 SKIP_WHITE_SPACE (bp);
2930 if (*bp) {
2931 if (*bp == '1')
2932 file_change = enter_file;
2933 else if (*bp == '2')
2934 file_change = leave_file;
2935 else {
2936 error ("invalid format #line command");
2937 return;
2940 bp++;
2941 SKIP_WHITE_SPACE (bp);
2942 if (*bp) {
2943 error ("invalid format #line command");
2944 return;
2948 hash_bucket =
2949 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
2950 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
2951 if (hp->length == fname_length &&
2952 strncmp (hp->value.cpval, (char *)fname, fname_length) == 0) {
2953 ip->fname = hp->value.cpval;
2954 break;
2956 if (hp == 0) {
2957 char *q;
2958 /* Didn't find it; cons up a new one. */
2959 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
2960 hp->next = *hash_bucket;
2961 *hash_bucket = hp;
2963 hp->length = fname_length;
2964 ip->fname = hp->value.cpval = q = ((char *) hp) + sizeof (HASHNODE);
2965 memcpy (q, fname, fname_length);
2967 } else if (*bp) {
2968 error ("invalid format #line command");
2969 return;
2972 ip->lineno = new_lineno;
2973 output_line_command (ip, op, 0, file_change);
2974 check_expand (op, ip->length - (ip->bufp - ip->buf));
2978 * remove all definitions of symbol from symbol table.
2979 * according to un*x /lib/cpp, it is not an error to undef
2980 * something that has no definitions, so it isn't one here either.
2982 void
2983 do_undef (buf, limit, op, keyword)
2984 U_CHAR *buf;
2985 U_CHAR *limit ATTRIBUTE_UNUSED;
2986 FILE_BUF *op ATTRIBUTE_UNUSED;
2987 struct directive *keyword ATTRIBUTE_UNUSED;
2989 HASHNODE *hp;
2991 SKIP_WHITE_SPACE (buf);
2993 if (! strncmp ((char *)buf, "defined", 7) && ! is_idchar[buf[7]])
2994 warning ("undefining `defined'");
2996 while ((hp = lookup (buf, -1, -1)) != NULL) {
2997 if (hp->type != T_MACRO)
2998 warning ("undefining `%s'", hp->name);
2999 delete_macro (hp);
3004 * handle #if command by
3005 * 1) inserting special `defined' keyword into the hash table
3006 * that gets turned into 0 or 1 by special_symbol (thus,
3007 * if the luser has a symbol called `defined' already, it won't
3008 * work inside the #if command)
3009 * 2) rescan the input into a temporary output buffer
3010 * 3) pass the output buffer to the yacc parser and collect a value
3011 * 4) clean up the mess left from steps 1 and 2.
3012 * 5) call conditional_skip to skip til the next #endif (etc.),
3013 * or not, depending on the value from step 3.
3015 void
3016 do_if (buf, limit, op, keyword)
3017 U_CHAR *buf, *limit;
3018 FILE_BUF *op ATTRIBUTE_UNUSED;
3019 struct directive *keyword ATTRIBUTE_UNUSED;
3021 int value;
3022 FILE_BUF *ip = &instack[indepth];
3024 value = eval_if_expression (buf, limit - buf);
3025 conditional_skip (ip, value == 0, T_IF);
3029 * handle a #elif directive by not changing if_stack either.
3030 * see the comment above do_else.
3032 void
3033 do_elif (buf, limit, op, keyword)
3034 U_CHAR *buf, *limit;
3035 FILE_BUF *op;
3036 struct directive *keyword ATTRIBUTE_UNUSED;
3038 int value;
3039 FILE_BUF *ip = &instack[indepth];
3041 if (if_stack == instack[indepth].if_stack) {
3042 error ("#elif not within a conditional");
3043 return;
3044 } else {
3045 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
3046 error ("#elif after #else");
3047 fprintf (stderr, " (matches line %d", if_stack->lineno);
3048 if (if_stack->fname != NULL && ip->fname != NULL &&
3049 strcmp (if_stack->fname, ip->fname) != 0)
3050 fprintf (stderr, ", file %s", if_stack->fname);
3051 fprintf (stderr, ")\n");
3053 if_stack->type = T_ELIF;
3056 if (if_stack->if_succeeded)
3057 skip_if_group (ip, 0);
3058 else {
3059 value = eval_if_expression (buf, limit - buf);
3060 if (value == 0)
3061 skip_if_group (ip, 0);
3062 else {
3063 ++if_stack->if_succeeded; /* continue processing input */
3064 output_line_command (ip, op, 1, same_file);
3070 * evaluate a #if expression in BUF, of length LENGTH,
3071 * then parse the result as a C expression and return the value as an int.
3074 eval_if_expression (buf, length)
3075 U_CHAR *buf;
3076 int length;
3078 FILE_BUF temp_obuf;
3079 HASHNODE *save_defined;
3080 int value;
3082 save_defined = install (U"defined", -1, T_SPEC_DEFINED, -1);
3083 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
3084 delete_macro (save_defined); /* clean up special symbol */
3086 value = parse_c_expression ((char *)temp_obuf.buf);
3088 free (temp_obuf.buf);
3090 return value;
3094 * routine to handle ifdef/ifndef. Try to look up the symbol,
3095 * then do or don't skip to the #endif/#else/#elif depending
3096 * on what directive is actually being processed.
3098 void
3099 do_xifdef (buf, limit, op, keyword)
3100 U_CHAR *buf, *limit;
3101 FILE_BUF *op ATTRIBUTE_UNUSED;
3102 struct directive *keyword;
3104 int skip;
3105 FILE_BUF *ip = &instack[indepth];
3106 U_CHAR *end;
3108 /* Discard leading and trailing whitespace. */
3109 SKIP_WHITE_SPACE (buf);
3110 while (limit != buf && is_hor_space[limit[-1]]) limit--;
3112 /* Find the end of the identifier at the beginning. */
3113 for (end = buf; is_idchar[*end]; end++);
3115 if (end == buf) {
3116 skip = (keyword->type == T_IFDEF);
3117 } else {
3118 skip = (lookup (buf, end-buf, -1) == NULL) ^ (keyword->type == T_IFNDEF);
3121 conditional_skip (ip, skip, T_IF);
3125 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3127 void
3128 conditional_skip (ip, skip, type)
3129 FILE_BUF *ip;
3130 int skip;
3131 enum node_type type;
3133 IF_STACK_FRAME *temp;
3135 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3136 temp->fname = ip->fname;
3137 temp->lineno = ip->lineno;
3138 temp->next = if_stack;
3139 if_stack = temp;
3141 if_stack->type = type;
3143 if (skip != 0) {
3144 skip_if_group (ip, 0);
3145 return;
3146 } else {
3147 ++if_stack->if_succeeded;
3148 output_line_command (ip, &outbuf, 1, same_file);
3153 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3154 * leaves input ptr at the sharp sign found.
3155 * If ANY is nonzero, return at next directive of any sort.
3157 void
3158 skip_if_group (ip, any)
3159 FILE_BUF *ip;
3160 int any;
3162 register U_CHAR *bp = ip->bufp, *cp;
3163 register U_CHAR *endb = ip->buf + ip->length;
3164 struct directive *kt;
3165 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
3166 U_CHAR *beg_of_line = bp;
3168 while (bp < endb) {
3169 switch (*bp++) {
3170 case '/': /* possible comment */
3171 if (*bp == '\\' && bp[1] == '\n')
3172 newline_fix (bp);
3173 if (*bp == '*') {
3174 ip->bufp = ++bp;
3175 bp = skip_to_end_of_comment (ip, &ip->lineno);
3177 break;
3178 case '\"':
3179 case '\'':
3180 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
3181 break;
3182 case '\\':
3183 /* Char after backslash loses its special meaning. */
3184 if (bp < endb) {
3185 if (*bp == '\n')
3186 ++ip->lineno; /* But do update the line-count. */
3187 bp++;
3189 break;
3190 case '\n':
3191 ++ip->lineno;
3192 beg_of_line = bp;
3193 break;
3194 case '#':
3195 ip->bufp = bp - 1;
3197 /* # keyword: a # must be first nonblank char on the line */
3198 if (beg_of_line == 0)
3199 break;
3200 /* Scan from start of line, skipping whitespace, comments
3201 and backslash-newlines, and see if we reach this #.
3202 If not, this # is not special. */
3203 bp = beg_of_line;
3204 while (1) {
3205 if (is_hor_space[*bp])
3206 bp++;
3207 else if (*bp == '\\' && bp[1] == '\n')
3208 bp += 2;
3209 else if (*bp == '/' && bp[1] == '*') {
3210 bp += 2;
3211 while (!(*bp == '*' && bp[1] == '/')) {
3212 if (*bp == '\n')
3213 ip->lineno++;
3214 bp++;
3216 bp += 2;
3218 else break;
3220 if (bp != ip->bufp) {
3221 bp = ip->bufp + 1; /* Reset bp to after the #. */
3222 break;
3225 bp = ip->bufp + 1; /* Point after '#'. */
3227 /* Skip whitespace and \-newline. */
3228 while (1) {
3229 if (is_hor_space[*bp])
3230 bp++;
3231 else if (*bp == '\\' && bp[1] == '\n')
3232 bp += 2;
3233 else if (*bp == '/' && bp[1] == '*') {
3234 bp += 2;
3235 while (!(*bp == '*' && bp[1] == '/'))
3236 bp++;
3237 bp += 2;
3239 else break;
3242 cp = bp;
3244 /* Now find end of directive name.
3245 If we encounter a backslash-newline, exchange it with any following
3246 symbol-constituents so that we end up with a contiguous name. */
3248 while (1) {
3249 if (is_idchar[*bp])
3250 bp++;
3251 else {
3252 if (*bp == '\\' && bp[1] == '\n')
3253 name_newline_fix (bp);
3254 if (is_idchar[*bp])
3255 bp++;
3256 else break;
3260 for (kt = directive_table; kt->length >= 0; kt++) {
3261 IF_STACK_FRAME *temp;
3262 if (strncmp ((char *)cp, kt->name, kt->length) == 0
3263 && !is_idchar[cp[kt->length]]) {
3265 /* If we are asked to return on next directive,
3266 do so now. */
3267 if (any)
3268 return;
3270 switch (kt->type) {
3271 case T_IF:
3272 case T_IFDEF:
3273 case T_IFNDEF:
3274 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3275 temp->next = if_stack;
3276 if_stack = temp;
3277 temp->lineno = ip->lineno;
3278 temp->fname = ip->fname;
3279 temp->type = kt->type;
3280 break;
3281 case T_ELSE:
3282 case T_ENDIF:
3283 case T_ELIF:
3284 if (if_stack == instack[indepth].if_stack) {
3285 error ("#%s not within a conditional", kt->name);
3286 break;
3288 else if (if_stack == save_if_stack)
3289 return; /* found what we came for */
3291 if (kt->type != T_ENDIF) {
3292 if (if_stack->type == T_ELSE)
3293 error ("#else or #elif after #else");
3294 if_stack->type = kt->type;
3295 break;
3298 temp = if_stack;
3299 if_stack = if_stack->next;
3300 free (temp);
3301 break;
3303 default:
3304 /* Anything else is ignored. */
3305 break;
3307 break;
3312 ip->bufp = bp;
3313 /* after this returns, rescan will exit because ip->bufp
3314 now points to the end of the buffer.
3315 rescan is responsible for the error message also. */
3319 * handle a #else directive. Do this by just continuing processing
3320 * without changing if_stack ; this is so that the error message
3321 * for missing #endif's etc. will point to the original #if. It
3322 * is possible that something different would be better.
3324 void
3325 do_else (buf, limit, op, keyword)
3326 U_CHAR *buf ATTRIBUTE_UNUSED;
3327 U_CHAR *limit ATTRIBUTE_UNUSED;
3328 FILE_BUF *op;
3329 struct directive *keyword ATTRIBUTE_UNUSED;
3331 FILE_BUF *ip = &instack[indepth];
3333 if (if_stack == instack[indepth].if_stack) {
3334 error ("#else not within a conditional");
3335 return;
3336 } else {
3337 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
3338 error ("#else after #else");
3339 fprintf (stderr, " (matches line %d", if_stack->lineno);
3340 if (strcmp (if_stack->fname, ip->fname) != 0)
3341 fprintf (stderr, ", file %s", if_stack->fname);
3342 fprintf (stderr, ")\n");
3344 if_stack->type = T_ELSE;
3347 if (if_stack->if_succeeded)
3348 skip_if_group (ip, 0);
3349 else {
3350 ++if_stack->if_succeeded; /* continue processing input */
3351 output_line_command (ip, op, 1, same_file);
3356 * unstack after #endif command
3358 void
3359 do_endif (buf, limit, op, keyword)
3360 U_CHAR *buf ATTRIBUTE_UNUSED;
3361 U_CHAR *limit ATTRIBUTE_UNUSED;
3362 FILE_BUF *op;
3363 struct directive *keyword ATTRIBUTE_UNUSED;
3365 if (if_stack == instack[indepth].if_stack)
3366 error ("unbalanced #endif");
3367 else {
3368 IF_STACK_FRAME *temp = if_stack;
3369 if_stack = if_stack->next;
3370 free (temp);
3371 output_line_command (&instack[indepth], op, 1, same_file);
3376 * Skip a comment, assuming the input ptr immediately follows the
3377 * initial slash-star. Bump line counter as necessary.
3378 * (The canonical line counter is &ip->lineno).
3379 * Don't use this routine (or the next one) if bumping the line
3380 * counter is not sufficient to deal with newlines in the string.
3382 U_CHAR *
3383 skip_to_end_of_comment (ip, line_counter)
3384 register FILE_BUF *ip;
3385 int *line_counter; /* place to remember newlines, or NULL */
3387 register U_CHAR *limit = ip->buf + ip->length;
3388 register U_CHAR *bp = ip->bufp;
3389 FILE_BUF *op = &outbuf; /* JF */
3390 int output = put_out_comments && !line_counter;
3392 /* JF this line_counter stuff is a crock to make sure the
3393 comment is only put out once, no matter how many times
3394 the comment is skipped. It almost works */
3395 if (output) {
3396 *op->bufp++ = '/';
3397 *op->bufp++ = '*';
3399 while (bp < limit) {
3400 if (output)
3401 *op->bufp++ = *bp;
3402 switch (*bp++) {
3403 case '/':
3404 if (warn_comments && bp < limit && *bp == '*')
3405 warning("`/*' within comment");
3406 break;
3407 case '\n':
3408 if (line_counter != NULL)
3409 ++*line_counter;
3410 if (output)
3411 ++op->lineno;
3412 break;
3413 case '*':
3414 if (*bp == '\\' && bp[1] == '\n')
3415 newline_fix (bp);
3416 if (*bp == '/') {
3417 if (output)
3418 *op->bufp++ = '/';
3419 ip->bufp = ++bp;
3420 return bp;
3422 break;
3425 ip->bufp = bp;
3426 return bp;
3430 * Skip over a quoted string. BP points to the opening quote.
3431 * Returns a pointer after the closing quote. Don't go past LIMIT.
3432 * START_LINE is the line number of the starting point (but it need
3433 * not be valid if the starting point is inside a macro expansion).
3435 * The input stack state is not changed.
3437 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3438 * for each newline passed.
3440 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3441 * if we pass a backslash-newline.
3443 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3445 U_CHAR *
3446 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
3447 register U_CHAR *bp;
3448 register U_CHAR *limit;
3449 int start_line;
3450 int *count_newlines;
3451 int *backslash_newlines_p;
3452 int *eofp;
3454 register U_CHAR c, match;
3456 match = *bp++;
3457 while (1) {
3458 if (bp >= limit) {
3459 error_with_line (line_for_error (start_line),
3460 "unterminated string or character constant");
3461 if (eofp)
3462 *eofp = 1;
3463 break;
3465 c = *bp++;
3466 if (c == '\\') {
3467 while (*bp == '\\' && bp[1] == '\n') {
3468 if (backslash_newlines_p)
3469 *backslash_newlines_p = 1;
3470 if (count_newlines)
3471 ++*count_newlines;
3472 bp += 2;
3474 if (*bp == '\n' && count_newlines) {
3475 if (backslash_newlines_p)
3476 *backslash_newlines_p = 1;
3477 ++*count_newlines;
3479 bp++;
3480 } else if (c == '\n') {
3481 /* Unterminated strings and character constants are 'legal'. */
3482 bp--; /* Don't consume the newline. */
3483 if (eofp)
3484 *eofp = 1;
3485 break;
3486 } else if (c == match)
3487 break;
3489 return bp;
3493 * write out a #line command, for instance, after an #include file.
3494 * If CONDITIONAL is nonzero, we can omit the #line if it would
3495 * appear to be a no-op, and we can output a few newlines instead
3496 * if we want to increase the line number by a small amount.
3497 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3500 void
3501 output_line_command (ip, op, conditional, file_change)
3502 FILE_BUF *ip, *op;
3503 int conditional;
3504 enum file_change_code file_change;
3506 int len;
3507 char line_cmd_buf[500];
3509 if (no_line_commands
3510 || ip->fname == NULL
3511 || no_output) {
3512 op->lineno = ip->lineno;
3513 return;
3516 if (conditional) {
3517 if (ip->lineno == op->lineno)
3518 return;
3520 /* If the inherited line number is a little too small,
3521 output some newlines instead of a #line command. */
3522 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
3523 check_expand (op, 10);
3524 while (ip->lineno > op->lineno) {
3525 *op->bufp++ = '\n';
3526 op->lineno++;
3528 return;
3532 sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
3533 if (file_change != same_file)
3534 strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
3535 len = strlen (line_cmd_buf);
3536 line_cmd_buf[len++] = '\n';
3537 check_expand (op, len + 1);
3538 if (op->bufp > op->buf && op->bufp[-1] != '\n')
3539 *op->bufp++ = '\n';
3540 memcpy (op->bufp, line_cmd_buf, len);
3541 op->bufp += len;
3542 op->lineno = ip->lineno;
3546 /* Expand a macro call.
3547 HP points to the symbol that is the macro being called.
3548 Put the result of expansion onto the input stack
3549 so that subsequent input by our caller will use it.
3551 If macro wants arguments, caller has already verified that
3552 an argument list follows; arguments come from the input stack. */
3554 void
3555 macroexpand (hp, op)
3556 HASHNODE *hp;
3557 FILE_BUF *op;
3559 int nargs;
3560 DEFINITION *defn = hp->value.defn;
3561 register U_CHAR *xbuf;
3562 int xbuf_len;
3563 int start_line = instack[indepth].lineno;
3565 CHECK_DEPTH (return;);
3567 /* it might not actually be a macro. */
3568 if (hp->type != T_MACRO) {
3569 special_symbol (hp, op);
3570 return;
3573 nargs = defn->nargs;
3575 if (nargs >= 0) {
3576 register int i;
3577 struct argdata *args;
3578 const char *parse_error = 0;
3580 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
3582 for (i = 0; i < nargs; i++) {
3583 args[i].raw = args[i].expanded = (U_CHAR *) "";
3584 args[i].raw_length = args[i].expand_length
3585 = args[i].stringified_length = 0;
3586 args[i].free1 = args[i].free2 = 0;
3589 /* Parse all the macro args that are supplied. I counts them.
3590 The first NARGS args are stored in ARGS.
3591 The rest are discarded. */
3592 i = 0;
3593 do {
3594 /* Discard the open-parenthesis or comma before the next arg. */
3595 ++instack[indepth].bufp;
3596 parse_error
3597 = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
3598 if (parse_error)
3600 error_with_line (line_for_error (start_line), parse_error);
3601 break;
3603 i++;
3604 } while (*instack[indepth].bufp != ')');
3606 /* If we got one arg but it was just whitespace, call that 0 args. */
3607 if (i == 1) {
3608 register U_CHAR *bp = args[0].raw;
3609 register U_CHAR *lim = bp + args[0].raw_length;
3610 while (bp != lim && is_space[*bp]) bp++;
3611 if (bp == lim)
3612 i = 0;
3615 if (nargs == 0 && i > 0)
3616 error ("arguments given to macro `%s'", hp->name);
3617 else if (i < nargs) {
3618 /* traditional C allows foo() if foo wants one argument. */
3619 if (nargs == 1 && i == 0)
3621 else if (i == 0)
3622 error ("no args to macro `%s'", hp->name);
3623 else if (i == 1)
3624 error ("only 1 arg to macro `%s'", hp->name);
3625 else
3626 error ("only %d args to macro `%s'", i, hp->name);
3627 } else if (i > nargs)
3628 error ("too many (%d) args to macro `%s'", i, hp->name);
3630 /* Swallow the closeparen. */
3631 ++instack[indepth].bufp;
3633 /* If macro wants zero args, we parsed the arglist for checking only.
3634 Read directly from the macro definition. */
3635 if (nargs == 0) {
3636 xbuf = defn->expansion;
3637 xbuf_len = defn->length;
3638 } else {
3639 register U_CHAR *exp = defn->expansion;
3640 register int offset; /* offset in expansion,
3641 copied a piece at a time */
3642 register int totlen; /* total amount of exp buffer filled so far */
3644 register struct reflist *ap;
3646 /* Macro really takes args. Compute the expansion of this call. */
3648 /* Compute length in characters of the macro's expansion. */
3649 xbuf_len = defn->length;
3650 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
3651 if (ap->stringify)
3652 xbuf_len += args[ap->argno].stringified_length;
3653 else
3654 xbuf_len += args[ap->argno].raw_length;
3657 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
3659 /* Generate in XBUF the complete expansion
3660 with arguments substituted in.
3661 TOTLEN is the total size generated so far.
3662 OFFSET is the index in the definition
3663 of where we are copying from. */
3664 offset = totlen = 0;
3665 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
3666 register struct argdata *arg = &args[ap->argno];
3668 for (i = 0; i < ap->nchars; i++)
3669 xbuf[totlen++] = exp[offset++];
3671 if (ap->stringify != 0) {
3672 int arglen = arg->raw_length;
3673 int escaped = 0;
3674 int in_string = 0;
3675 int c;
3676 i = 0;
3677 while (i < arglen
3678 && (c = arg->raw[i], is_space[c]))
3679 i++;
3680 while (i < arglen
3681 && (c = arg->raw[arglen - 1], is_space[c]))
3682 arglen--;
3683 for (; i < arglen; i++) {
3684 c = arg->raw[i];
3686 /* Special markers Newline Space
3687 generate nothing for a stringified argument. */
3688 if (c == '\n' && arg->raw[i+1] != '\n') {
3689 i++;
3690 continue;
3693 /* Internal sequences of whitespace are replaced by one space
3694 except within an string or char token. */
3695 if (! in_string
3696 && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
3697 while (1) {
3698 /* Note that Newline Space does occur within whitespace
3699 sequences; consider it part of the sequence. */
3700 if (c == '\n' && is_space[arg->raw[i+1]])
3701 i += 2;
3702 else if (c != '\n' && is_space[c])
3703 i++;
3704 else break;
3705 c = arg->raw[i];
3707 i--;
3708 c = ' ';
3711 if (escaped)
3712 escaped = 0;
3713 else {
3714 if (c == '\\')
3715 escaped = 1;
3716 if (in_string) {
3717 if (c == in_string)
3718 in_string = 0;
3719 } else if (c == '\"' || c == '\'')
3720 in_string = c;
3723 /* Escape these chars */
3724 if (c == '\"' || (in_string && c == '\\'))
3725 xbuf[totlen++] = '\\';
3726 if (isprint (c))
3727 xbuf[totlen++] = c;
3728 else {
3729 sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
3730 totlen += 4;
3733 } else {
3734 U_CHAR *p1 = arg->raw;
3735 U_CHAR *l1 = p1 + arg->raw_length;
3737 if (ap->raw_before) {
3738 while (p1 != l1 && is_space[*p1]) p1++;
3739 while (p1 != l1 && is_idchar[*p1])
3740 xbuf[totlen++] = *p1++;
3741 /* Delete any no-reexpansion marker that follows
3742 an identifier at the beginning of the argument
3743 if the argument is concatenated with what precedes it. */
3744 if (p1[0] == '\n' && p1[1] == '-')
3745 p1 += 2;
3747 if (ap->raw_after) {
3748 /* Arg is concatenated after: delete trailing whitespace,
3749 whitespace markers, and no-reexpansion markers. */
3750 while (p1 != l1) {
3751 if (is_space[l1[-1]]) l1--;
3752 else if (l1[-1] == '-') {
3753 U_CHAR *p2 = l1 - 1;
3754 /* If a `-' is preceded by an odd number of newlines then it
3755 and the last newline are a no-reexpansion marker. */
3756 while (p2 != p1 && p2[-1] == '\n') p2--;
3757 if ((l1 - 1 - p2) & 1) {
3758 l1 -= 2;
3760 else break;
3762 else break;
3765 memmove (xbuf + totlen, p1, l1 - p1);
3766 totlen += l1 - p1;
3769 if (totlen > xbuf_len)
3770 abort ();
3773 /* if there is anything left of the definition
3774 after handling the arg list, copy that in too. */
3776 for (i = offset; i < defn->length; i++)
3777 xbuf[totlen++] = exp[i];
3779 xbuf[totlen] = 0;
3780 xbuf_len = totlen;
3782 for (i = 0; i < nargs; i++) {
3783 if (args[i].free1 != 0)
3784 free (args[i].free1);
3785 if (args[i].free2 != 0)
3786 free (args[i].free2);
3789 } else {
3790 xbuf = defn->expansion;
3791 xbuf_len = defn->length;
3794 /* Now put the expansion on the input stack
3795 so our caller will commence reading from it. */
3797 register FILE_BUF *ip2;
3799 ip2 = &instack[++indepth];
3801 ip2->fname = 0;
3802 ip2->lineno = 0;
3803 ip2->buf = xbuf;
3804 ip2->length = xbuf_len;
3805 ip2->bufp = xbuf;
3806 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
3807 ip2->macro = hp;
3808 ip2->if_stack = if_stack;
3813 * Parse a macro argument and store the info on it into *ARGPTR.
3814 * Return nonzero to indicate a syntax error.
3817 const char *
3818 macarg (argptr)
3819 register struct argdata *argptr;
3821 FILE_BUF *ip = &instack[indepth];
3822 int paren = 0;
3823 int newlines = 0;
3824 int comments = 0;
3826 /* Try to parse as much of the argument as exists at this
3827 input stack level. */
3828 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
3829 &paren, &newlines, &comments);
3831 /* If we find the end of the argument at this level,
3832 set up *ARGPTR to point at it in the input stack. */
3833 if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
3834 && bp != ip->buf + ip->length) {
3835 if (argptr != 0) {
3836 argptr->raw = ip->bufp;
3837 argptr->raw_length = bp - ip->bufp;
3839 ip->bufp = bp;
3840 } else {
3841 /* This input stack level ends before the macro argument does.
3842 We must pop levels and keep parsing.
3843 Therefore, we must allocate a temporary buffer and copy
3844 the macro argument into it. */
3845 int bufsize = bp - ip->bufp;
3846 int extra = newlines;
3847 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
3848 int final_start = 0;
3850 memcpy (buffer, ip->bufp, bufsize);
3851 ip->bufp = bp;
3852 ip->lineno += newlines;
3854 while (bp == ip->buf + ip->length) {
3855 if (instack[indepth].macro == 0) {
3856 free (buffer);
3857 return "unterminated macro call";
3859 ip->macro->type = T_MACRO;
3860 if (ip->free_ptr)
3861 free (ip->free_ptr);
3862 ip = &instack[--indepth];
3863 newlines = 0;
3864 comments = 0;
3865 bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
3866 &newlines, &comments);
3867 final_start = bufsize;
3868 bufsize += bp - ip->bufp;
3869 extra += newlines;
3870 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
3871 memcpy (buffer + bufsize - (bp - ip->bufp), ip->bufp, bp - ip->bufp);
3872 ip->bufp = bp;
3873 ip->lineno += newlines;
3876 /* Now, if arg is actually wanted, record its raw form,
3877 discarding comments and duplicating newlines in whatever
3878 part of it did not come from a macro expansion.
3879 EXTRA space has been preallocated for duplicating the newlines.
3880 FINAL_START is the index of the start of that part. */
3881 if (argptr != 0) {
3882 argptr->raw = buffer;
3883 argptr->raw_length = bufsize;
3884 argptr->free1 = buffer;
3885 argptr->newlines = newlines;
3886 argptr->comments = comments;
3887 if ((newlines || comments) && ip->fname != 0)
3888 argptr->raw_length
3889 = final_start +
3890 discard_comments (argptr->raw + final_start,
3891 argptr->raw_length - final_start,
3892 newlines);
3893 argptr->raw[argptr->raw_length] = 0;
3894 if (argptr->raw_length > bufsize + extra)
3895 abort ();
3899 /* If we are not discarding this argument,
3900 macroexpand it and compute its length as stringified.
3901 All this info goes into *ARGPTR. */
3903 if (argptr != 0) {
3904 FILE_BUF obuf;
3905 register U_CHAR *buf, *lim;
3906 register int totlen;
3908 obuf = expand_to_temp_buffer (argptr->raw,
3909 argptr->raw + argptr->raw_length,
3912 argptr->expanded = obuf.buf;
3913 argptr->expand_length = obuf.length;
3914 argptr->free2 = obuf.buf;
3916 buf = argptr->raw;
3917 lim = buf + argptr->raw_length;
3919 totlen = 0;
3920 while (buf != lim) {
3921 register U_CHAR c = *buf++;
3922 totlen++;
3923 /* Internal sequences of whitespace are replaced by one space
3924 in most cases, but not always. So count all the whitespace
3925 in case we need to keep it all. */
3926 if (c == '\"' || c == '\\') /* escape these chars */
3927 totlen++;
3928 else if (!isprint (c))
3929 totlen += 3;
3931 argptr->stringified_length = totlen;
3933 return 0;
3936 /* Scan text from START (inclusive) up to LIMIT (exclusive),
3937 counting parens in *DEPTHPTR,
3938 and return if reach LIMIT
3939 or before a `)' that would make *DEPTHPTR negative
3940 or before a comma when *DEPTHPTR is zero.
3941 Single and double quotes are matched and termination
3942 is inhibited within them. Comments also inhibit it.
3943 Value returned is pointer to stopping place.
3945 Increment *NEWLINES each time a newline is passed.
3946 Set *COMMENTS to 1 if a comment is seen. */
3948 U_CHAR *
3949 macarg1 (start, limit, depthptr, newlines, comments)
3950 U_CHAR *start;
3951 register U_CHAR *limit;
3952 int *depthptr, *newlines, *comments;
3954 register U_CHAR *bp = start;
3956 while (bp < limit) {
3957 switch (*bp) {
3958 case '(':
3959 (*depthptr)++;
3960 break;
3961 case ')':
3962 if (--(*depthptr) < 0)
3963 return bp;
3964 break;
3965 case '\\':
3966 /* Traditionally, backslash makes following char not special. */
3967 if (bp + 1 < limit)
3969 bp++;
3970 /* But count source lines anyway. */
3971 if (*bp == '\n')
3972 ++*newlines;
3974 break;
3975 case '\n':
3976 ++*newlines;
3977 break;
3978 case '/':
3979 if (bp[1] == '\\' && bp[2] == '\n')
3980 newline_fix (bp + 1);
3981 if (bp[1] != '*' || bp + 1 >= limit)
3982 break;
3983 *comments = 1;
3984 bp += 2;
3985 while (bp + 1 < limit) {
3986 if (bp[0] == '*'
3987 && bp[1] == '\\' && bp[2] == '\n')
3988 newline_fix (bp + 1);
3989 if (bp[0] == '*' && bp[1] == '/')
3990 break;
3991 if (*bp == '\n') ++*newlines;
3992 bp++;
3994 bp += 1;
3995 break;
3996 case '\'':
3997 case '\"':
3999 int quotec;
4000 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
4001 if (*bp == '\\') {
4002 bp++;
4003 if (*bp == '\n')
4004 ++*newlines;
4005 while (*bp == '\\' && bp[1] == '\n') {
4006 bp += 2;
4008 } else if (*bp == '\n') {
4009 ++*newlines;
4010 if (quotec == '\'')
4011 break;
4015 break;
4016 case ',':
4017 if ((*depthptr) == 0)
4018 return bp;
4019 break;
4021 bp++;
4024 return bp;
4027 /* Discard comments and duplicate newlines
4028 in the string of length LENGTH at START,
4029 except inside of string constants.
4030 The string is copied into itself with its beginning staying fixed.
4032 NEWLINES is the number of newlines that must be duplicated.
4033 We assume that that much extra space is available past the end
4034 of the string. */
4037 discard_comments (start, length, newlines)
4038 U_CHAR *start;
4039 int length;
4040 int newlines;
4042 register U_CHAR *ibp;
4043 register U_CHAR *obp;
4044 register U_CHAR *limit;
4045 register int c;
4047 /* If we have newlines to duplicate, copy everything
4048 that many characters up. Then, in the second part,
4049 we will have room to insert the newlines
4050 while copying down.
4051 NEWLINES may actually be too large, because it counts
4052 newlines in string constants, and we don't duplicate those.
4053 But that does no harm. */
4054 if (newlines > 0) {
4055 ibp = start + length;
4056 obp = ibp + newlines;
4057 limit = start;
4058 while (limit != ibp)
4059 *--obp = *--ibp;
4062 ibp = start + newlines;
4063 limit = start + length + newlines;
4064 obp = start;
4066 while (ibp < limit) {
4067 *obp++ = c = *ibp++;
4068 switch (c) {
4069 case '\n':
4070 /* Duplicate the newline. */
4071 *obp++ = '\n';
4072 break;
4074 case '\\':
4075 if (*ibp == '\n') {
4076 obp--;
4077 ibp++;
4079 break;
4081 case '/':
4082 if (*ibp == '\\' && ibp[1] == '\n')
4083 newline_fix (ibp);
4084 /* Delete any comment. */
4085 if (ibp[0] != '*' || ibp + 1 >= limit)
4086 break;
4087 obp--;
4088 ibp++;
4089 while (ibp + 1 < limit) {
4090 if (ibp[0] == '*'
4091 && ibp[1] == '\\' && ibp[2] == '\n')
4092 newline_fix (ibp + 1);
4093 if (ibp[0] == '*' && ibp[1] == '/')
4094 break;
4095 ibp++;
4097 ibp += 2;
4098 break;
4100 case '\'':
4101 case '\"':
4102 /* Notice and skip strings, so that we don't
4103 think that comments start inside them,
4104 and so we don't duplicate newlines in them. */
4106 int quotec = c;
4107 while (ibp < limit) {
4108 *obp++ = c = *ibp++;
4109 if (c == quotec)
4110 break;
4111 if (c == '\n' && quotec == '\'')
4112 break;
4113 if (c == '\\' && ibp < limit) {
4114 while (*ibp == '\\' && ibp[1] == '\n')
4115 ibp += 2;
4116 *obp++ = *ibp++;
4120 break;
4124 return obp - start;
4128 /* Core error handling routine. */
4129 void
4130 v_message (mtype, line, msgid, ap)
4131 enum msgtype mtype;
4132 int line;
4133 const char *msgid;
4134 va_list ap;
4136 const char *fname = 0;
4137 int i;
4139 if (mtype == WARNING && inhibit_warnings)
4140 return;
4142 for (i = indepth; i >= 0; i--)
4143 if (instack[i].fname != NULL) {
4144 if (line == 0)
4145 line = instack[i].lineno;
4146 fname = instack[i].fname;
4147 break;
4150 if (fname)
4151 fprintf (stderr, "%s:%d: ", fname, line);
4152 else
4153 fprintf (stderr, "%s: ", progname);
4155 if (mtype == WARNING)
4156 fputs ("warning: ", stderr);
4158 vfprintf (stderr, msgid, ap);
4159 putc ('\n', stderr);
4161 if (mtype == ERROR)
4162 errors++;
4166 * error - print error message and increment count of errors.
4168 void
4169 error VPARAMS ((const char *msgid, ...))
4171 #ifndef ANSI_PROTOTYPES
4172 const char *msgid;
4173 #endif
4174 va_list ap;
4176 VA_START(ap, msgid);
4178 #ifndef ANSI_PROTOTYPES
4179 msgid = va_arg (ap, const char *);
4180 #endif
4182 v_message (ERROR, 0, msgid, ap);
4185 void
4186 error_with_line VPARAMS ((int line, const char *msgid, ...))
4188 #ifndef ANSI_PROTOTYPES
4189 int line;
4190 const char *msgid;
4191 #endif
4192 va_list ap;
4194 VA_START(ap, msgid);
4196 #ifndef ANSI_PROTOTYPES
4197 line = va_arg (ap, int);
4198 msgid = va_arg (ap, const char *);
4199 #endif
4201 v_message (ERROR, line, msgid, ap);
4204 /* Error including a message from `errno'. */
4205 void
4206 error_from_errno (name)
4207 const char *name;
4209 error ("%s: %s", name, strerror (errno));
4212 /* Print error message but don't count it. */
4213 void
4214 warning VPARAMS ((const char *msgid, ...))
4216 #ifndef ANSI_PROTOTYPES
4217 const char *msgid;
4218 #endif
4219 va_list ap;
4221 VA_START(ap, msgid);
4223 #ifndef ANSI_PROTOTYPES
4224 msgid = va_arg (ap, const char *);
4225 #endif
4227 v_message (WARNING, 0, msgid, ap);
4230 void
4231 fatal VPARAMS ((const char *msgid, ...))
4233 #ifndef ANSI_PROTOTYPES
4234 const char *msgid;
4235 #endif
4236 va_list ap;
4238 VA_START(ap, msgid);
4240 #ifndef ANSI_PROTOTYPES
4241 msgid = va_arg (ap, const char *);
4242 #endif
4244 v_message (FATAL, 0, msgid, ap);
4245 exit (FATAL_EXIT_CODE);
4248 /* More 'friendly' abort that prints the location at which we died. */
4249 void
4250 fancy_abort (line, func)
4251 int line;
4252 const char *func;
4254 fatal ("Internal error in %s, at tradcpp.c:%d\n\
4255 Please submit a full bug report.\n\
4256 See %s for instructions.", func, line, GCCBUGURL);
4259 void
4260 perror_with_name (name)
4261 const char *name;
4263 fprintf (stderr, "%s: %s: %s\n", progname, name, strerror (errno));
4264 errors++;
4267 void
4268 pfatal_with_name (name)
4269 const char *name;
4271 perror_with_name (name);
4272 exit (FATAL_EXIT_CODE);
4275 /* Return the line at which an error occurred.
4276 The error is not necessarily associated with the current spot
4277 in the input stack, so LINE says where. LINE will have been
4278 copied from ip->lineno for the current input level.
4279 If the current level is for a file, we return LINE.
4280 But if the current level is not for a file, LINE is meaningless.
4281 In that case, we return the lineno of the innermost file. */
4283 line_for_error (line)
4284 int line;
4286 int i;
4287 int line1 = line;
4289 for (i = indepth; i >= 0; ) {
4290 if (instack[i].fname != 0)
4291 return line1;
4292 i--;
4293 if (i < 0)
4294 return 0;
4295 line1 = instack[i].lineno;
4297 return 0;
4301 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4303 * As things stand, nothing is ever placed in the output buffer to be
4304 * removed again except when it's KNOWN to be part of an identifier,
4305 * so flushing and moving down everything left, instead of expanding,
4306 * should work ok.
4309 void
4310 grow_outbuf (obuf, needed)
4311 register FILE_BUF *obuf;
4312 register int needed;
4314 register U_CHAR *p;
4315 int minsize;
4317 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
4318 return;
4320 /* Make it at least twice as big as it is now. */
4321 obuf->length *= 2;
4322 /* Make it have at least 150% of the free space we will need. */
4323 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
4324 if (minsize > obuf->length)
4325 obuf->length = minsize;
4327 p = (U_CHAR *) xrealloc (obuf->buf, obuf->length);
4328 obuf->bufp = p + (obuf->bufp - obuf->buf);
4329 obuf->buf = p;
4332 /* Symbol table for macro names and special symbols */
4335 * install a name in the main hash table, even if it is already there.
4336 * name stops with first non alphanumeric, except leading '#'.
4337 * caller must check against redefinition if that is desired.
4338 * delete_macro () removes things installed by install () in fifo order.
4339 * this is important because of the `defined' special symbol used
4340 * in #if, and also if pushdef/popdef directives are ever implemented.
4342 * If LEN is >= 0, it is the length of the name.
4343 * Otherwise, compute the length by scanning the entire name.
4345 * If HASH is >= 0, it is the precomputed hash code.
4346 * Otherwise, compute the hash code.
4348 * caller must set the value, if any is desired.
4350 HASHNODE *
4351 install (name, len, type, hash)
4352 const U_CHAR *name;
4353 int len;
4354 enum node_type type;
4355 int hash;
4356 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4358 register HASHNODE *hp;
4359 register int bucket;
4360 register const U_CHAR *p;
4361 U_CHAR *q;
4363 if (len < 0) {
4364 p = name;
4365 while (is_idchar[*p])
4366 p++;
4367 len = p - name;
4370 if (hash < 0)
4371 hash = hashf (name, len, HASHSIZE);
4373 hp = (HASHNODE *) xmalloc (sizeof (HASHNODE) + len + 1);
4374 bucket = hash;
4375 hp->bucket_hdr = &hashtab[bucket];
4376 hp->next = hashtab[bucket];
4377 hashtab[bucket] = hp;
4378 hp->prev = NULL;
4379 if (hp->next != NULL)
4380 hp->next->prev = hp;
4381 hp->type = type;
4382 hp->length = len;
4383 hp->name = q = ((U_CHAR *) hp) + sizeof (HASHNODE);
4384 memcpy (q, name, len);
4385 q[len] = 0;
4386 return hp;
4390 * find the most recent hash node for name name (ending with first
4391 * non-identifier char) installed by install
4393 * If LEN is >= 0, it is the length of the name.
4394 * Otherwise, compute the length by scanning the entire name.
4396 * If HASH is >= 0, it is the precomputed hash code.
4397 * Otherwise, compute the hash code.
4399 HASHNODE *
4400 lookup (name, len, hash)
4401 const U_CHAR *name;
4402 int len;
4403 int hash;
4405 register const U_CHAR *bp;
4406 register HASHNODE *bucket;
4408 if (len < 0) {
4409 for (bp = name; is_idchar[*bp]; bp++) ;
4410 len = bp - name;
4413 if (hash < 0)
4414 hash = hashf (name, len, HASHSIZE);
4416 bucket = hashtab[hash];
4417 while (bucket) {
4418 if (bucket->length == len
4419 && strncmp ((char *)bucket->name, (char *)name, len) == 0)
4420 return bucket;
4421 bucket = bucket->next;
4423 return NULL;
4427 * Delete a hash node. Some weirdness to free junk from macros.
4428 * More such weirdness will have to be added if you define more hash
4429 * types that need it.
4432 /* Note that the DEFINITION of a macro is removed from the hash table
4433 but its storage is not freed. This would be a storage leak
4434 except that it is not reasonable to keep undefining and redefining
4435 large numbers of macros many times.
4436 In any case, this is necessary, because a macro can be #undef'd
4437 in the middle of reading the arguments to a call to it.
4438 If #undef freed the DEFINITION, that would crash. */
4439 void
4440 delete_macro (hp)
4441 HASHNODE *hp;
4444 if (hp->prev != NULL)
4445 hp->prev->next = hp->next;
4446 if (hp->next != NULL)
4447 hp->next->prev = hp->prev;
4449 /* make sure that the bucket chain header that
4450 the deleted guy was on points to the right thing afterwards. */
4451 if (hp == *hp->bucket_hdr)
4452 *hp->bucket_hdr = hp->next;
4454 free (hp);
4458 * return hash function on name. must be compatible with the one
4459 * computed a step at a time, elsewhere
4462 hashf (name, len, hashsize)
4463 register const U_CHAR *name;
4464 register int len;
4465 int hashsize;
4467 register int r = 0;
4469 while (len--)
4470 r = HASHSTEP (r, *name++);
4472 return MAKE_POS (r) % hashsize;
4475 /* Dump all macro definitions as #defines to stdout. */
4477 void
4478 dump_all_macros ()
4480 int bucket;
4482 for (bucket = 0; bucket < HASHSIZE; bucket++) {
4483 register HASHNODE *hp;
4485 for (hp = hashtab[bucket]; hp; hp= hp->next) {
4486 if (hp->type == T_MACRO) {
4487 register DEFINITION *defn = hp->value.defn;
4488 struct reflist *ap;
4489 int offset;
4490 int concat;
4493 /* Print the definition of the macro HP. */
4495 printf ("#define %s", hp->name);
4496 if (defn->nargs >= 0) {
4497 int i;
4499 printf ("(");
4500 for (i = 0; i < defn->nargs; i++) {
4501 dump_arg_n (defn, i);
4502 if (i + 1 < defn->nargs)
4503 printf (", ");
4505 printf (")");
4508 printf (" ");
4510 offset = 0;
4511 concat = 0;
4512 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4513 dump_defn_1 (defn->expansion, offset, ap->nchars);
4514 if (ap->nchars != 0)
4515 concat = 0;
4516 offset += ap->nchars;
4517 if (ap->stringify)
4518 printf (" #");
4519 if (ap->raw_before && !concat)
4520 printf (" ## ");
4521 concat = 0;
4522 dump_arg_n (defn, ap->argno);
4523 if (ap->raw_after) {
4524 printf (" ## ");
4525 concat = 1;
4528 dump_defn_1 (defn->expansion, offset, defn->length - offset);
4529 printf ("\n");
4535 /* Output to stdout a substring of a macro definition.
4536 BASE is the beginning of the definition.
4537 Output characters START thru LENGTH.
4538 Discard newlines outside of strings, thus
4539 converting funny-space markers to ordinary spaces. */
4540 void
4541 dump_defn_1 (base, start, length)
4542 U_CHAR *base;
4543 int start;
4544 int length;
4546 U_CHAR *p = base + start;
4547 U_CHAR *limit = base + start + length;
4549 while (p < limit) {
4550 if (*p != '\n')
4551 putchar (*p);
4552 else if (*p == '\"' || *p =='\'') {
4553 U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
4554 fwrite (p, p1 - p, 1, stdout);
4555 p = p1 - 1;
4557 p++;
4561 /* Print the name of argument number ARGNUM of macro definition DEFN.
4562 Recall that DEFN->argnames contains all the arg names
4563 concatenated in reverse order with comma-space in between. */
4564 void
4565 dump_arg_n (defn, argnum)
4566 DEFINITION *defn;
4567 int argnum;
4569 register U_CHAR *p = defn->argnames;
4570 while (argnum + 1 < defn->nargs) {
4571 p = (U_CHAR *) strchr ((char *)p, ' ') + 1;
4572 argnum++;
4575 while (*p && *p != ',') {
4576 putchar (*p);
4577 p++;
4581 /* Initialize syntactic classifications of characters. */
4582 void
4583 initialize_char_syntax ()
4585 register int i;
4588 * Set up is_idchar and is_idstart tables. These should be
4589 * faster than saying (is_alpha (c) || c == '_'), etc.
4590 * Must do set up these things before calling any routines tthat
4591 * refer to them.
4593 for (i = 'a'; i <= 'z'; i++) {
4594 is_idchar[i - 'a' + 'A'] = 1;
4595 is_idchar[i] = 1;
4596 is_idstart[i - 'a' + 'A'] = 1;
4597 is_idstart[i] = 1;
4599 for (i = '0'; i <= '9'; i++)
4600 is_idchar[i] = 1;
4601 is_idchar['_'] = 1;
4602 is_idstart['_'] = 1;
4604 /* horizontal space table */
4605 is_hor_space[' '] = 1;
4606 is_hor_space['\t'] = 1;
4607 is_hor_space['\v'] = 1;
4608 is_hor_space['\f'] = 1;
4609 is_hor_space['\r'] = 1;
4611 is_space[' '] = 1;
4612 is_space['\t'] = 1;
4613 is_space['\v'] = 1;
4614 is_space['\f'] = 1;
4615 is_space['\n'] = 1;
4616 is_space['\r'] = 1;
4619 /* Initialize the built-in macros. */
4620 #define DSC(x) U x, sizeof x - 1
4621 #define install_spec(name, type) \
4622 install(DSC(name), type, -1);
4623 #define install_value(name, val) \
4624 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
4625 void
4626 initialize_builtins ()
4628 HASHNODE *hp;
4630 install_spec ("__BASE_FILE__", T_BASE_FILE);
4631 install_spec ("__DATE__", T_DATE);
4632 install_spec ("__FILE__", T_FILE);
4633 install_spec ("__TIME__", T_TIME);
4634 install_spec ("__VERSION__", T_VERSION);
4635 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL);
4636 install_spec ("__LINE__", T_SPECLINE);
4638 #ifndef NO_BUILTIN_SIZE_TYPE
4639 install_value ("__SIZE_TYPE__", SIZE_TYPE);
4640 #endif
4641 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4642 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE);
4643 #endif
4644 #ifndef NO_BUILTIN_WCHAR_TYPE
4645 install_value ("__WCHAR_TYPE__", WCHAR_TYPE);
4646 #endif
4647 #ifndef NO_BUILTIN_WINT_TYPE
4648 install_value ("__WINT_TYPE__", WINT_TYPE);
4649 #endif
4650 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX);
4651 install_value ("__USER_LABEL_PREFIX__", user_label_prefix);
4653 #undef DSC
4654 #undef install_spec
4655 #undef install_value
4658 * process a given definition string, for initialization
4659 * If STR is just an identifier, define it with value 1.
4660 * If STR has anything after the identifier, then it should
4661 * be identifier-space-definition.
4663 void
4664 make_definition (str)
4665 U_CHAR *str;
4667 FILE_BUF *ip;
4668 struct directive *kt;
4669 U_CHAR *buf, *p;
4670 size_t len = strlen ((char *)str);
4672 p = (U_CHAR *) strchr ((char *)str, '=');
4673 if (p == NULL) {
4674 /* Change -DFOO into #define FOO 1 */
4675 buf = (U_CHAR *) alloca (len + 3);
4676 memcpy (buf, str, len);
4677 memcpy (buf + len, " 1", 3);
4678 len += 2;
4679 } else {
4680 buf = (U_CHAR *) alloca (len + 1);
4681 memcpy (buf, str, len + 1);
4682 buf[p - str] = ' ';
4685 ip = &instack[++indepth];
4686 ip->fname = "*Initialization*";
4688 ip->buf = ip->bufp = buf;
4689 ip->length = len;
4690 ip->lineno = 1;
4691 ip->macro = 0;
4692 ip->free_ptr = 0;
4693 ip->if_stack = if_stack;
4695 for (kt = directive_table; kt->type != T_DEFINE; kt++)
4698 /* pass NULL as output ptr to do_define since we KNOW it never
4699 does any output.... */
4700 do_define (buf, buf + ip->length, NULL, kt);
4701 --indepth;
4704 /* JF, this does the work for the -U option */
4705 void
4706 make_undef (str)
4707 U_CHAR *str;
4709 FILE_BUF *ip;
4710 struct directive *kt;
4712 ip = &instack[++indepth];
4713 ip->fname = "*undef*";
4715 ip->buf = ip->bufp = str;
4716 ip->length = strlen ((char *)str);
4717 ip->lineno = 1;
4718 ip->macro = 0;
4719 ip->free_ptr = 0;
4720 ip->if_stack = if_stack;
4722 for (kt = directive_table; kt->type != T_UNDEF; kt++)
4725 do_undef (str, str + ip->length, NULL, kt);
4726 --indepth;
4729 /* Add output to `deps_buffer' for the -M switch.
4730 STRING points to the text to be output.
4731 SIZE is the number of bytes, or 0 meaning output until a null.
4732 If SIZE is nonzero, we break the line first, if it is long enough. */
4733 void
4734 deps_output (string, size)
4735 const char *string;
4736 int size;
4738 #ifndef MAX_OUTPUT_COLUMNS
4739 #define MAX_OUTPUT_COLUMNS 75
4740 #endif
4741 if (size != 0 && deps_column != 0
4742 && size + deps_column > MAX_OUTPUT_COLUMNS) {
4743 deps_output ("\\\n ", 0);
4744 deps_column = 0;
4747 if (size == 0)
4748 size = strlen (string);
4750 if (deps_size + size + 1 > deps_allocated_size) {
4751 deps_allocated_size = deps_size + size + 50;
4752 deps_allocated_size *= 2;
4753 deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
4755 memcpy (&deps_buffer[deps_size], string, size);
4756 deps_size += size;
4757 deps_column += size;
4758 deps_buffer[deps_size] = 0;
4761 /* Get the file-mode and data size of the file open on FD
4762 and store them in *MODE_POINTER and *SIZE_POINTER. */
4765 file_size_and_mode (fd, mode_pointer, size_pointer)
4766 int fd;
4767 int *mode_pointer;
4768 long *size_pointer;
4770 struct stat sbuf;
4772 if (fstat (fd, &sbuf) < 0) return -1;
4773 if (mode_pointer) *mode_pointer = sbuf.st_mode;
4774 if (size_pointer) *size_pointer = sbuf.st_size;
4775 return 0;